-
Notifications
You must be signed in to change notification settings - Fork 0
/
polyin.sh
executable file
·134 lines (113 loc) · 3.24 KB
/
polyin.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
usage()
{
cat <<EOF
$0 [options]
--database(-d) database
--polygon(-p) existing polygon
--operation(-o) operation to perform (del|piste|fire|trail|) optional
When using --title and --operstion del, you dom't need to specify --polygon
EOF
exit
}
# Minimum number of arguments
if test $# -lt 4; then
usage
fi
osm="yes"
if test x"${osm}" = x"yes"; then
table="planet_osm_polygon"
else
table="boundary"
fi
OPTS="`getopt -o d:h:p:t: -l database:,polygon:operation:title:,help`"
while test $# -gt 0; do
case $1 in
-d|--database) database=$2 ;;
-p|--polygon) poly=$2 ;;
-o|--operation) op=$2 ;;
-t|--title) title=$2 ;;
-h|--help) usage ;;
--) break ;;
esac
shift
done
database="${database:-polygons}"
op="${op:-none}"
poly="${poly}"
name="`echo ${poly} | sed -e 's:\.poly::'`"
name="`basename ${name}`"
title="${title:-${name}}"
name="${name:-${title}}"
exists="`psql --dbname=${database} --command="select name from ${table};" | grep -c "${name}"`"
create_tables()
{
sql="${poly}.sql"
cat <<EOF > ${sql}
EOF
return 0
}
create_sql()
{
# echo "TRACE: $*"
local sql=
declare -A fields=()
fields[name]="${1:-'unknown'}"
fields[geom]="$2"
# landuse, leisure, sport=ski
if test ${exists} -eq 0; then
if test x"${osm}" = x"yes"; then
case ${op} in
ski|piste) sql="INSERT INTO ${table} (name,way,boundary,leisure,osm_id,landuse,tags) VALUES ('${fields[name]}',ST_GeomFromText('LINESTRING(${fields[geom]})',900913), 'administrative', 'sports_centre', 0, 'winter_sports', '"ski"=>"yes"')"
;;
*) sql="INSERT INTO ${table} (name,way,boundary,osm_id) VALUES ('${fields[name]}',ST_GeomFromText('LINESTRING(${fields[geom]})',900913), 'administrative', 0)"
;;
esac
else
sql="INSERT INTO ${table} (name,geom) VALUES ('${fields[name]}',ST_GeomFromText('LINESTRING(${fields[geom]})'));"
# sql="INSERT INTO ${table} (name,polygon) VALUES ('${fields[name]}',ST_MakePolygon(ST_GeomFromText('LINESTRING(${fields[geom]})')));"
fi
else
if test x"${osm}" = x"yes"; then
sql="UPDATE ${table} SET way = ST_GeomFromText('LINESTRING(${fields[geom]})',900913);"
else
sql="UPDATE ${table} SET geom = ST_GeomFromText('LINESTRING(${fields[geom]})',900913);"
fi
fi
echo "${sql}" > /tmp/sql.$$
echo "${sql}"
return 0
}
if test x"${op}" = x"del"; then
psql --dbname=${database} --command="DELETE FROM ${table} WHERE name='${name}'"
exit
fi
#
# main loop
#
lat=0
lon=0
location=
while read line; do
if test x"${line:0:1}" = x"-"; then
lat="`echo ${line} | tr -s ' ' | cut -d ' ' -f 1`"
lon="`echo ${line} | tr -s ' ' | cut -d ' ' -f 2`"
if test x"${location}" = x; then
location="$lat $lon"
else
location="${location},$lat $lon"
fi
fi
done < ${poly}
# (gid, name, perimeter, area, acres, geom)
sql="`create_sql "${name}" "${location}"`"
#echo $sql
if test ${exists} -eq 0; then
echo "Inserting ${name}"
else
echo "Updating ${name}"
fi
out="`psql --dbname=${database} --file=/tmp/sql.$$`"
rm -f /tmp/sql.$$
# select name,ST_AsText(geom) from boundary;
# ND01A_NDFD | LINESTRING(-105.470718 39.999771,-105.467475 39.999769,-105.49719 39.950494,-105.496414 39.95089)