Created
December 7, 2018 08:36
-
-
Save vehrka/e408e7deb00b8722c4e4f6457a998225 to your computer and use it in GitHub Desktop.
Assign isocode to shapes programatically
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
find . -name "*.shp" | sort > shps.txt | |
# In this case: the name of the country is the first word of the shape name and after there is a number | |
sed -ie 's/^\.\/\([a-z_-]*\)_\([0-9]\)/\1,.\/\1_\2/' shps.txt | |
cat shps.txt | header -a cname,shape > shapes.txt | |
csvcut -c 1 shapes.txt | uniq | sort > countries.txt | |
# This works only in Mac | |
pbcopy < countries.txt | |
# Match the countries with the isocodes | |
csvjoin -c cname countries.txt shapes.txt | csvcut -c 1,3 > isoshapes.csv | |
# ZSH to read a file | |
while read line | |
do | |
# Split by the , get the first result | |
ccountry=${line%,*} | |
# Split by the , get the last result | |
shapefile=${line#*,} | |
# Remove the extension | |
base=${shapefile%\.*} | |
# Remove the path (/) | |
base=${base##*/} | |
ogrinfo $shapefile -sql "ALTER TABLE $base ADD COLUMN ccode varchar(20);" | |
ogrinfo $shapefile -dialect sqlite -sql "UPDATE $base SET ccode = '$ccountry' WHERE ccode IS NULL;" | |
done < isoshapes.csv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment