Skip to content

Instantly share code, notes, and snippets.

@OlegJakushkin
Last active February 12, 2021 11:19
Show Gist options
  • Save OlegJakushkin/506277ed98dd6a7a79c9082a4a158ecd to your computer and use it in GitHub Desktop.
Save OlegJakushkin/506277ed98dd6a7a79c9082a4a158ecd to your computer and use it in GitHub Desktop.
olejak/gis
#!/bin/bash
#
# Requires:
# - gdal_sieve.py
# - ogr2ogr (GDAL)
# - topojson (node.js)
# Grab the relative directory for source file.
SRC_DIR=`dirname $0`
# Which raster to compress.
ORG_FILE="$SRC_DIR/Rome-DEM.tif"
# Final output file.
OUTPUT_FILE="$SRC_DIR/Rome.json"
echo "Processing $ORG_FILE."
# Where to output the new file.
TMP_DIR=./tmp
# The amount of times the file should be passed over.
ITERATIONS=3
# Threshold for each iteration.
THRESHOLD=40
# TopoJSON area threshold for simplification.
TOPO_COMPRESSION=0.005
# Setup internal vars.
_CUR=$THRESHOLD
_COMPRESSION=$(($ITERATIONS * $THRESHOLD))
rm -rf $TMP_DIR
mkdir -p $TMP_DIR
# Start sieve passes.
gdal_sieve.py -st $THRESHOLD -4 $ORG_FILE $TMP_DIR/output-"$THRESHOLD".tiff
while [ $_CUR -le $_COMPRESSION ]; do
let _PREV=$_CUR
let _CUR=$_CUR+$THRESHOLD
echo "Compressing output-$_PREV.tiff into $_CUR.tiff"
gdal_sieve.py -st $THRESHOLD -4 "$TMP_DIR/output-$_PREV.tiff" \
"$TMP_DIR/output-$_CUR.tiff"
rm "$TMP_DIR/output-$_PREV.tiff"
done
# Raster to vector.
gdal_polygonize.py $TMP_DIR/output-"$_CUR".tiff \
-f "ESRI Shapefile" $TMP_DIR vector n
# Change shapefile to geojson without the 0 layer, which is water.
ogr2ogr -f "GeoJSON" -where "n != 0" "$TMP_DIR/geojson.json" $TMP_DIR/vector.shp
# Convert to compressed TopoJSON.
geo2topo -o $OUTPUT_FILE \
--no-stitch-poles \
-s $TOPO_COMPRESSION \
-p -- "$TMP_DIR/geojson.json"
# Clean up.
rm -rf $TMP_DIR
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@OlegJakushkin
Copy link
Author

Shows:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment