1

I would like to create a polygon shapefile from this output in Jupiter. I can't use ArcPy. Is there any other way?

{'polygon': 'MULTIPOLYGON (((15.0512620 37.463320, 15.0512620 37.4743620, 15.0732421 37.4743652, 15.0732421 37.463379, 15.0512696 37.463379)),((15.0512300 37.463300, 15.0512396 37.4743357, 15.0732365 37.4743452, 15.0732221 37.463579, 15.0512596 37.463579)))'}
2

1 Answer 1

1

Creating a shapefile in Python can be done in more than one way depending on which library you use. PyShp, Fiona and GDAL are all libraries that can create shape files from Python code.

First, parsing the geometry in example above throws an error because the polygons in the data are not "closed". A valid polygon or multipolygon must have identical start and endpoints. If, however, you manually copy the first point of each polygon to the end of each coordinate list then it parses correctly and converts. Also, note that the two polygons are on top of each other with the points varying by a very small amount.

Below are examples using PyShp and Fiona to create a shapefile from a MultiPolygon WKT object.

PyShp

import shapefile
import shapely.wkt

geo = {'polygon': 'MULTIPOLYGON (((15.0512620 37.463320, 15.0512620 37.4743620, 15.0732421 37.4743652, 15.0732421 37.463379, 15.0512696 37.463379, 15.0512620 37.463320)),((15.0512300 37.463300, 15.0512396 37.4743357, 15.0732365 37.4743452, 15.0732221 37.463579, 15.0512596 37.463579, 15.0512300 37.463300)))'}

shapelyObject = shapely.wkt.loads(geo['polygon'])

# POLYGON type=5
w = shapefile.Writer('test.shp', shapeType=5)

polyList = []
for polygon in shapelyObject:
    polyList.append(list(polygon.exterior.coords))

# write feature to shapefile
w.field('name', 'C')
w.poly(polyList)
w.record('polygon1')

w.close()

Fiona

import fiona
import shapely.wkt
from shapely.geometry import mapping

geo = {'polygon': 'MULTIPOLYGON (((15.0512620 37.463320, 15.0512620 37.4743620, 15.0732421 37.4743652, 15.0732421 37.463379, 15.0512696 37.463379, 15.0512620 37.463320)),((15.0512300 37.463300, 15.0512396 37.4743357, 15.0732365 37.4743452, 15.0732221 37.463579, 15.0512596 37.463579, 15.0512300 37.463300)))'}

# convert WKT to shapely geometric object
shapelyObject = shapely.wkt.loads(geo['polygon'])

# define schema
schema = {
    'geometry': 'MultiPolygon',
    'properties': [('Name', 'str')]
}

#open a fiona object
polyShp = fiona.open('poly.shp', mode='w', driver='ESRI Shapefile',
          schema=schema, crs="EPSG:4326")

shape = {
        'geometry': mapping(shapelyObject),
        'properties': {'Name': 'shape'}
}

polyShp.write(shape)

#close fiona object
polyShp.close()

Here is the created shapefile displayed in QGIS.

shapefile displayed in QGIS

1
  • thank you very much!
    – GIS GIS
    Commented Aug 3, 2021 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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