Skip to content

Instantly share code, notes, and snippets.

@TimSC
Created December 13, 2017 23:41
Show Gist options
  • Save TimSC/6870226caa22bfba86094fb00b226bb7 to your computer and use it in GitHub Desktop.
Save TimSC/6870226caa22bfba86094fb00b226bb7 to your computer and use it in GitHub Desktop.
Write shapefile to WKT text
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