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)

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.

  • 1
    Can you give at least a contrived sample of the real data? Is your list a set of ints? If so, have you tried casting them to floats? You will notice in the other question that you link to that the coords are not whole numbers. – Michael - Where's Clay Shirky Feb 5 '14 at 13:57
  • Thanks Michael, that did the trick. My list was not floats. By converting it, I solved the problem. In case you wanna post your comment as an answer, I would accept it. – Martin Feb 5 '14 at 14:03

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]
|improve this answer|||||

From the pyshp documentation page:

>>> # Create a polygon shapefile
>>> w = shapefile.Writer(shapefile.POLYGON)
>>> w.poly(parts=[[[1,5],[5,5],[5,1],[3,3],[1,1]]])
>>> w.field('FIRST_FLD','C','40')
>>> w.field('SECOND_FLD','C','40')
>>> w.record('First','Polygon')
>>> w.save('shapefiles/test/polygon')

Just substitute your list for the parts= argument to Writer.poly, and set whatever fields you would like to associate with your shapefile.

|improve this answer|||||
  • Thanks for your suggestion. Unfortunately I can´t get it working like that. I get the error message: ShapefileException: Failed to write shapefile bounding box. Floats required. I updated my question accordingly – Martin Feb 5 '14 at 11:53

One easy (one time) solution is to use the QuickWKT Plugin.

Transform your list into a EWKT string by adding a the header with the SRID and the type of geometry. Add a comma in the end of each XY pair.

 30 10,
 10 20,
 20 40,
 40 40,
 30 10

Copy + paste the all thing to QuickWKT Plugin's dialog, and press OK.

enter image description here

Your polygon will be created in a memory layer in QGIS. After that, you can do whatever you want with it, including Save as... (Shapefile).

enter image description here

|improve this answer|||||
  • Thanks for your suggestion, but I´m looking for an automated option to include in my python script – Martin Feb 5 '14 at 11:59
  • That's why I said that it's a "one time solution" :-P – Alexandre Neto Feb 5 '14 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.