Skip to content

Instantly share code, notes, and snippets.

@pmauduit
Created March 4, 2014 07:25
Show Gist options
  • Save pmauduit/9341801 to your computer and use it in GitHub Desktop.
Save pmauduit/9341801 to your computer and use it in GitHub Desktop.
OSM GPX traces retriever - Shell script
#!/bin/sh
set -x
# Chambéry
#CITY=Chambéry
#NORTH=45.5903
#SOUTH=45.5354
#WEST=5.8621
#EAST=5.9755
# Geneva
CITY=Genève
NORTH=46.2303
SOUTH=46.1783
WEST=6.0846
EAST=6.19888
rm -f ${CITY}.*.gpx
rm -rf ${CITY}
mkdir ${CITY}
# Gets the traces from the OSM API
COUNT=0
BREAK=0
while [ ${BREAK} -eq 0 ]; do
wget -O ${CITY}.${COUNT}.gpx "http://api.openstreetmap.org/api/0.6/trackpoints?bbox=${WEST},${SOUTH},${EAST},${NORTH}&page=${COUNT}"
if [ `cat ${CITY}.${COUNT}.gpx | wc -l` -eq 2 ] ; then BREAK=1 ; fi
COUNT=$(($COUNT + 1))
done
# Setting up the GPX sanitizer
if [ ! -f sanitize ] ; then
echo "GPX sanitizer has not been found. Compiling it ..." ;
cd ./gpx_sanitizer/c_version ;
make install ;
cd ../../ ;
# If still unable to find it, abort.
if [ ! -f sanitize ]; then
echo "Unable to find the GPX sanitizer tool. Aborting." ;
exit ;
fi
fi
# generates GPX -> pgdump
rm huge.sql
# sanitize tracks
for i in ${CITY}.*.gpx ; do
./sanitize $i ;
for j in out_*.gpx ; do
mv $j ${CITY}/`basename $i .gpx`_$j ;
done
done
cd ${CITY}
for i in *.gpx ; do
ogr2ogr -lco DROP_TABLE=OFF -lco CREATE_TABLE=off -f PGDump `basename $i .gpx`.sql $i ;
cat `basename $i .gpx`.sql >> ../huge.sql ;
rm `basename $i .gpx`.sql ;
done
cd ../
# Loads data into PostGIS
dropdb osmposter
createdb osmposter
psql osmposter < osmposter_schema.sql
psql osmposter < huge.sql
rm huge.sql
# Generates a shapefile
rm -rf ${CITY}_out_shp
mkdir ${CITY}_out_shp
cd ${CITY}_out_shp
pgsql2shp osmposter tracks
cd ../
# don't really care actually
# Was meant to get the admin boundary of the city
#
#cat > oapi-${CITY}.req << EOF
#<union>
# <query type="relation">
# <has-kv k="name" v="${CITY}"/>
# <has-kv k="admin_level" v="8"/>
# </query>
# <recurse type="relation-node" into="nodes"/>
# <recurse type="relation-way"/>
# <recurse type="way-node"/>
#</union>
#<print/>
#EOF
#
#wget --post-file=oapi-${CITY}.req --header='Content-Type: text/xml' 'http://api.openstreetmap.fr/oapi/interpreter' -O ${CITY}_bound.osm
#rm -f oapi-${CITY}.req
#osm2pgsql -d osmposter ${CITY}_bound.osm
#rm ${CITY}_bound.osm
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment