Created
December 13, 2017 23:41
-
-
Save TimSC/6870226caa22bfba86094fb00b226bb7 to your computer and use it in GitHub Desktop.
Write shapefile to WKT text
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
import os | |
import shapefile | |
from shapely.geometry import Polygon | |
if __name__ == "__main__": | |
sf = shapefile.Reader("TM_WORLD_BORDERS-0.3.shp") | |
assert len(sf.shapes()) == len(sf.records()) | |
fieldIndex = {} | |
for i, (fieldName, fieldType, fieldSize, fieldInfo) in enumerate(sf.fields[1:]): | |
fieldIndex[fieldName] = i | |
print fieldIndex | |
iso3Field = fieldIndex['ISO3'] | |
for sp, rc in zip(sf.shapes(), sf.records()): | |
print rc[iso3Field], len(sp.points) | |
ply = Polygon(sp.points) | |
#print "sp.points", sp.points | |
#print ply.wkt | |
#print sp.parts | |
#print sp.shapeType, | |
fi = open(os.path.join("borders", rc[iso3Field]+".txt"), "wt") | |
fi.write(ply.wkt) | |
fi.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment