Skip to content

Instantly share code, notes, and snippets.

@henryjameslau
Last active April 4, 2020 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henryjameslau/750474a16001bb77a1b7587047e63223 to your computer and use it in GitHub Desktop.
Save henryjameslau/750474a16001bb77a1b7587047e63223 to your computer and use it in GitHub Desktop.
Bash script to take a zip file from the ONS geoportal and return a topojson with fields renamed as AREACD and AREANM
#!/bin/bash
# bash script to process shapefiles from the ONS geoportal
# to run, use sh process_shapefile.sh test.zip
echo "Filename: $1"
FILEN=`echo $1 | sed 's/\.zip$//'`
echo $FILEN
# first unzip the file into it a folder
unzip $1 -d "$FILEN"
#get codes
cd $FILEN
for shp in *shp
do
# LAYERNAME=`echo $shp | sed 's/\.shp$//'`
ogr2ogr -f geojson temp.geojson $shp -nln LAYER
code=$(ogrinfo -so temp.geojson LAYER | grep -i cd | cut -d: -f1)
name=$(ogrinfo -so temp.geojson LAYER | grep -i nm | grep -i -v 'nmw' | cut -d: -f1)
echo $code
echo $name
# this will drop and rename the fields
#https://gis.stackexchange.com/questions/58541/how-to-rename-field-names-in-a-shapefile-from-the-commandline
ogr2ogr -f "ESRI Shapefile" geog.shp temp.geojson -sql "SELECT $code AS AREACD, $name AS AREANM from LAYER"
#output as geojson
#https://github.com/mbloch/mapshaper/wiki/Command-Reference
rm temp.geojson
mapshaper geog.shp -proj wgs84 -o format=topojson geog.json
done
@henryjameslau
Copy link
Author

Requires mapshaper & gdal

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