Tuesday 8 January 2019

Creating Polygon Shapefile from list of X,Y coordinates using Python?


I have a list of X,Y coordinates from a csv file, that represent a polygon. I´m trying to create a Polygon-Shapefile from this list. I´ve been trying around and found a possbility to write the list to a Point-Shapefile.


Unfortunately that is not enough for me. Is there any way to get the coordinates in a Polygon-shapefile straight away?


Following Brad´s suggestions I tried the following code:


for i in list:
w = shapefile.Writer(shapefile.POLYGON)

w.poly(parts=[list])
w.field('F_FLD','C','40')
w.field('S_FLD','C','40')
w.record('First','Polygon')
w.save('C:/Users/.../Desktop/Shape')

Unfortunately I´m getting an error message:


ShapefileException: Failed to write shapefile bounding box. Floats required.

Looks like there is a problem saving the shapefile to disk. This seems to be a similar question, but I couldn´t work out, how to get it going in my case.




Answer



This expands on the answer posted by BradHards:


The error message sounds like is expecting floats where it is not getting them. If your coordinate list is a set of ints, try casting them to floats:


list = [[1,5],[5,5],[5,1],[3,3],[1,1]]
list = [[float(coord) for coord in pair] for pair in list]

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...