Skip to content

Instantly share code, notes, and snippets.

@albert-decatur
Last active August 29, 2015 14:24
Show Gist options
  • Save albert-decatur/d215db83dca806bc9c46 to your computer and use it in GitHub Desktop.
Save albert-decatur/d215db83dca806bc9c46 to your computer and use it in GitHub Desktop.
simplify shapefile, convert to geojson, remove null features

simplify shapefile

convert to geojson

remove null features

prerequisites

  • the incomparable mapshaper
  • jq, your favorite JSON filter
  • moreutils, hugely underrated GNU utils
# define temporary geojson for mapshaper to export to
json=$(mktemp --suffix .geojson)
# simplify your shapefile and save to tmp geojson
mapshaper example.shp -simplify 1% cartesian -o format=geojson $json
# remove null features from tmp geojson with jq
cat $( echo $json | sed 's:[.]geojson$:-ms.geojson:g') |\
jq '.|[.features[]|select(.geometry.type != null)]' |\
# add back JSON for features - I am positive jq can do this better than sed! just haven't gotten it yet
sed '1 i\{"features":' |\
sed '$ s:$:,:g;$ s:$:"type"\:"FeatureCollection"}:g'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment