Skip to content

Instantly share code, notes, and snippets.

@hammeiam
Last active March 9, 2021 19:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hammeiam/2d9f36c35f505206f138 to your computer and use it in GitHub Desktop.
Save hammeiam/2d9f36c35f505206f138 to your computer and use it in GitHub Desktop.
# A command for a mapping project I'm working on.
# Converts one big shapefile of countries into individual files.
# Data file from www.naturalearthdata.com
errors=()
for country in "${countries[@]}"
do
echo "$country"
# skip if we've already processed this country
test -d "$country" && continue
stuck="$country"
# select the country from the shapefile, convert it to geojson
ogr2ogr -f GeoJSON -where "gu_a3 = '$country'" states.json ne_10m_admin_1_states_provinces_lakes.shp && \
# use mapshaper to simplify the geojson file to make it smaller
mapshaper states.json -simplify 30% -o && \
# clean up files
rm states.json && \
mv states-ms.json states.json && \
# convert geojson to topojson, perserve adm1_cod_1 and name
topojson --id-property adm1_cod_1 -p name=NAME -p name -o states.topo.json states.json && \
# make a country folder, add the file to it
mkdir $country && \
mv states.topo.json "$country"/states.topo.json && \
stuck=;
rm states.json;
# if all of the commands don't complete (sans the last rm) then 'stuck' isn't nullified so we know there was an error
[[ -n "$stuck" ]] && echo "error on $stuck" && errors+=("$stuck")
done
# what broke?
echo "${errors[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment