Last active
April 4, 2020 08:41
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.DS_Store |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires mapshaper & gdal