-
Notifications
You must be signed in to change notification settings - Fork 0
/
osm2kml.sh
executable file
·303 lines (272 loc) · 8.99 KB
/
osm2kml.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/bin/bash
#
# Copyright (C) 2016, 2017, 2018, 2019, 2020 Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# This is a simple bourne shell script to convert raw OSM data files into
# pretty KML. This way when the KML file is loaded into an offline mapping
# program, all the colors mean something. For example, ski trails and
# bike/hiking trails have difficulty ratings, so this changes the color
# of the trail. For skiing, this matches the colors the resorts use. Same
# for highways. Different types and surfaces have tags signifying what type
# it is.
# load commonly used functions
osmbin="`which $0`"
topdir="`dirname ${osmbin}`"
. "${topdir}/osmshlib/colors.sh" || exit 1
. "${topdir}/osmshlib/styles.sh" || exit 1
. "${topdir}/osmshlib/parse.sh" || exit 1
. "${topdir}/osmshlib/sql.sh" || exit 1
. "${topdir}/osmshlib/kml.sh" || exit 1
#set -o nounset # Treat unset variables as an error
# This is a list of supported subsets of data to extract.
supportedlines="trails piste roads"
supportedpoints="emergency lodging huts wifi waterfall swimming historic camp trailhead peaks hotspring firewater helicopter milestone addresses nws places parcels subdivision"
supported="${supportedlines} ${supportedpoints}"
sets[REC]="camp trails trailhead piste hotspring swimming waterfall roads"
sets[EMERGENCY]="firewater milestone trails trailhead roads camps"
usage()
{
local sup="`echo ${supported} | sed -e 's/ /[:name],/g'`"
cat <<EOF
$0 [options]
--database(-d) database
--subset(-s) (${sup})
--polygon(-p) existing polygon1[:name],poly2[:name],poly3[:name]
--format(-f) kml|kmz|aqm
--title(-t) title
--extra(-e) extra tags
--output(-o) output file name
Multiple polygons or data subsets can be specified. The optionsl :name is used
for the Folder name imstead of defaulting to the polygon or database name.
EOF
exit
}
if test $# -lt 1; then
usage
fi
tourism=""
name=""
dbs="$1"
ns=""
outfile=""
polygon=""
subset="trails"
format="kml"
title=""
extra_tags=""
OPTS="`getopt -o d:h:t:f:s:o:p:e: -l database:,extra:,polygon:,subset:,format:,title:,output:,help`"
while test $# -gt 0; do
case $1 in
-d|--database) database=$2 ;;
-s|--subset) subs=$2 ;;
-f|--format) format=$2 ;;
-p|--polygon) polys=$2, ;;
-t|--title) title=$2 ;;
-o|--output) outfile=$2 ;;
-e|--extra) extra_tags=$2 ;;
-h|--help) usage ;;
--) break ;;
esac
shift
done
title="${title:-${database} `echo ${subs} | sed -e 's/:[a-zA-Z0-9]*//'`}"
# Always use a full path
#if test `dirname ${outfile}`; then
# outfile="$PWD/${outfile}"
#fi
# Process the list of data subsets
declare -a subsets
declare -A subnames
declare -a subtypes
i=1 # array indexes can't start with 0
subs="`echo ${subs} | tr ' ' '_' | tr ',' ' '`"
for sub in ${subs}; do
subnames[${sub}]="${sub}"
subsets[$i]="`echo ${sub} | cut -d ':' -f 1`"
if test "`echo ${supported} | grep -c ${subsets[$i]}`" -eq 0; then
echo "ERROR: ${sub} not supported!"
continue
fi
# There are two classes of data, waypoints and lines, which are
# rendered differently.
case ${subsets[$i]} in
wifi|lodging|huts|waterfall|swimming) subtypes[$i]="waypoint" ;;
firewater|helicopter|emergency|camp|addresses) subtypes[$i]="waypoint" ;;
*) subtypes[${subsets[$i]}]="line" ;;
esac
i="`expr $i + 1`"
done
subnames[helicopter]="Landing Zones"
subnames[hike]="Hike/Bike Trails"
subnames[firewater]="Water Sources"
subnames[wifi]="Wifi Access"
subnames[emergency]="Emergency Buildings"
subnames[lodging]="Lodging"
subnames[hut]="Alpine Huts"
subnames[camp]="Campgrounds"
subnames[places]="Towns"
#subnames[camp]="Campsites"
subnames[addresses]="Addresses"
subnames[roads]="Roads"
subnames[trails]="Hiking Trails"
subnames[milestone]="Mile Markers"
subnames[parcel]="Parcels"
subnames[subdivisions]="Subdivisions"
debug="yes"
#eval "$(get_camp_sites)"
#echo ${camps[KellyDahlCampground]}
#exit 0
# Process the list of polygons
declare -a polygons=()
declare -a polynames=()
i=1 # array indexes can't start with 0
polys="`echo ${polys} | tr ',' ' '`"
for poly in ${polys}; do
polygons[$i]="`echo ${polys} | cut -d ':' -f 1`"
polynames[$i]="`echo ${polys} | cut -d ':' -f 2`"
if test x"${polynames[$i]}" = x; then
polynames[$i]="${poly}"
fi
i="`expr $i + 1`"
done
#tmpdir="$PWD"
tmpdir="/tmp"
outdir="${tmpdir}/osmtmp-$$"
mkdir -p ${outdir}
if test x"${outfile}" = x; then
outfile="./${database}-${subs}.kml"
fi
outfile="`echo ${outfile} | tr -d ' '`"
rm -f ${outdir}/debug.log
# # Make sure the specified polygon is in the database
# if test ${#polygons[@]} -gt 0; then
# for i in ${!#polygons[@]}; do
# echo "Processing ${polynames[$i]}..."
# poly_exists ${polygons[$i]}
# if test $? -gt 0; then
# echo "ERROR: ${polynames[$i]} doesn't exist in planet_osm_polygon!"
# else
# echo "${polynames[$i]} exists in planet_osm_polygon!"
# fi
# done
# fi
# Create the final KML file
kml_file_header "${outfile}" "${title}" "${subs}"
tmpout="${outdir}/campgrounds.out"
rm -f ${tmpout}
cat <<EOF >> ${tmpout}
SELECT DISTINCT tags->'is_in' FROM planet_osm_point WHERE tags->'is_in'!='' AND tourism='camp_site';
EOF
kmlstatus="header"
flevel=0
total=0
# Execute the query. The index is an integer, which can be used in ${subnames} to
# get the full name.
for subset in ${subsets[@]}; do
func="count_${subset}"
count="`${func}`"
if test "${count}" -eq 0; then
echo "No data entries for ${subset} in ${database}"
continue
fi
total=$(expr ${total} + 1)
# Create the SQL command file
sqlcmd="`sql_file ${subset} ${outdir}/${database}-${subset}`"
sqlout="`echo ${sqlcmd} | sed -e 's:\.sql:.tmp:'`"
kmlout="`echo ${sqlcmd} | sed -e 's:\.sql:.kml:'`"
fullname="${subnames[${subset}]}"
# Start the Folder if there is more than one
if test ${#subsets[@]} -gt 0; then
kml_folder_start ${kmlout} "${fullname}"
# kmlstatus="start"
flevel="`expr ${flevel} + 1`"
fi
# execute the query
psql --tuples-only --dbname=${database} --no-align --file=${sqlcmd} --output=${sqlout}
index=0
func="parse_${subset}"
# print a rotating character, so we know it's working
rot[0]="|"
rot[1]="/"
rot[2]="-"
rot[3]="\\"
j=0
echo ""
oldname=""
newname=""
while read line; do
# the ISIN field is only set for nodes using the 'is_in' tag to group things
# like camsites in a campground, or fire hydrants in a city.
newname="${data[ISIN]}"
match=`expr "${oldname}" != "${newname}"`
#echo "FOOBY: ${match} : ${kmlstatus} ${flevel}"
if test "${flevel}" -gt 1 -a ${match} -eq 1; then
kml_folder_end ${kmlout}
# kmlstatus="end"
flevel="`expr ${flevel} - 1`"
fi
if test x"${newname}" != x; then
if test ${match} -eq 1; then
kml_folder_start ${kmlout} "${newname}"
# kmlstatus="start"
flevel="`expr ${flevel} + 1`"
fi
oldname="${newname}" # cache name change
else
oldname="" # no is_in tag in node
fi
id="`echo ${line} | cut -d '|' -f 1`"
# Bleech, ugly hack to fix errors in strings that screw up parsing.
name="`echo ${line} | sed -e 's:Rent|::' | cut -d '|' -f 2 | sed -e 's:\&:and:'`"
way="`echo ${line} | cut -d '|' -f 3`"
out="`${func} "${line}"`"
eval $out
# Some nodes are marked to be ignored, this is primarily used to mark depreciated
# water sources used for fire response.
if test x${data[DISUSED]} != x; then
echo "WARNING: Dropping ${data[NAME]} as it's disused."
continue
fi
kml_placemark ${kmlout} "`declare -p data`"
echo -n -e "\r\t${rot[$j]}"
if test $j -eq 3; then
j=0
else
j=`expr $j + 1`
fi
done < ${sqlout}
while test ${flevel} -gt 0; do
kml_folder_end ${kmlout}
flevel="`expr ${flevel} - 1`"
done
done
cat ${outdir}/*.kml >> "${outfile}"
kml_file_footer "${outfile}"
kmlstatus="footer"
echo "SQL file is ${sqlcmd}"
# Make a KMZ file if specified
if test x"${format}" = x"kmz"; then
#newout="`basename ${outfile} | sed -e 's:\.kml:.kmz:'`"
newout="`echo ${outfile} | sed -e 's:\.kml:.kmz:'`"
(cd ${topdir} && zip -qrj ${newout} icons)
echo "Making KMZ file ${newout}"
zip -q -j ${newout} ${outfile}
echo "KMZ file is ${newout}"
else
echo "KML file is ${outfile}"
fi
# cat ${outdir}/*.sql
# rm -fr ${outdir}/*.tmp ${outdir}/*.sql