Skip to content

Commit

Permalink
Merge pull request #29 from abettermap/27.slim-geojson
Browse files Browse the repository at this point in the history
Optimize GeoJSON
  • Loading branch information
deidyomega committed Nov 18, 2017
2 parents b567415 + 35addfe commit 74f8fc1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 209 deletions.
37 changes: 37 additions & 0 deletions automation/shp-to-geojson.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash

# -----------------------------
# shp-to-geojson.sh
# Convert shapefiles to GeoJSON
# -----------------------------

SOURCE_SHP="${1:-cerp/static/VoterPrecinct.shp}"
DEST_GEOJSON="${2:-cerp/static/VoterPrecinct.geojson}"
FIELDS_TO_KEEP="PRECINCT,SENATEDIST,HOUSEDIST,COMMDIST,UPDATEDATE"
TOLERANCE=0.00015 # smaller decimal = higher precision

# ogr2ogr cannot overwrite GeoJSON?
rm -f $DEST_GEOJSON

# Convert shp to geojson
ogr2ogr \
-f "GeoJSON" \
-lco COORDINATE_PRECISION=5 \
-simplify $TOLERANCE \
-select $FIELDS_TO_KEEP \
"$DEST_GEOJSON.tmp" \
$SOURCE_SHP

# Check if `json-minify` exists, install if not
if ! type json-minify &> /dev/null; then
npm i -g json-minify
fi

# Minify GeoJSON
json-minify "$DEST_GEOJSON.tmp" > $DEST_GEOJSON

# Delete temp file
rm "$DEST_GEOJSON.tmp"

# topojson example
# topojson $DEST_GEOJSON > "$DEST_GEOJSON.topojson" -p
Loading

0 comments on commit 74f8fc1

Please sign in to comment.