GRASS/Simplifying OSM Shapefiles

From OpenStreetMap Wiki
Jump to: navigation, search

GRASS scripting for fun and profit: Load OSM data into PostGIS, then simplify it using the postgis simplify() function. Once that's done, load in the shapefile (, build polylines (this is where you turn something that starts at 500,000 segments into 90,000), then run the v.clean command, which cleans up and simplifies the segments again.

Once this is done, spit the file back out to a shapefile, and remove your temp vars.

This code may eat your children: it's provided here as an example only, and should not be used by someone who does not have at least some familiarity with GRASS.

 rm -f ${TMP}_{shp,shx,prj,dbf}
 pgsql2shp -f $TMP osmtest "select simplify(the_geom, $THRESH) as the_geom, oid as id from segments  where length(simplify(the_geom, $THRESH)) > 0"

 g.remove vect=$TMP -o dsn=. layer=$TMP out=osm_tmp

 g.remove vect=${TMP}_1,${TMP}_2 in=${TMP} out=${TMP}_1
 v.clean in=${TMP}_1 out=${TMP}_2 tool=break,snap,rmdupl,rmdangle,prune thresh=0,$THRESH,$THRESH,$THRESH,$THRESH

 rm -f osm_segment_$THRESH.{shp,shx,prj,dbf}
 v.out.ogr in=${TMP}_2 dsn=. olayer=osm_segment_$THRESH

 g.remove vect=$TMP,${TMP}_1,${TMP}_2
 rm -f ${TMP}_{shp,shx,prj,dbf}