2

I'd like to convert geometry to WKT with pyshp and pygeoif using this suggestion, but pyshp throws a following error:

Traceback (most recent call last):
  File "E:/!Projects/imScrape/metadata_extractor.py", line 82, in <module>
    wkt = extract_wkt_from_shp(shp_fileobj, shx_fileobj, dbf_fileobj, prj_fileobj)
  File "E:/!Projects/imScrape/metadata_extractor.py", line 36, in extract_wkt_from_shp
    print(pygeoif.geometry.as_shape(geometries[0]))
  File "C:\Users\lobanov\AppData\Local\Programs\Python\Python36\lib\site-packages\pygeoif\geometry.py", line 1163, in as_shape
    raise TypeError('Object does not implement __geo_interface__')
TypeError: Object does not implement __geo_interface__. 

What might be the case?

Here's the code:

shape_file = shapefile.Reader(shp=shp_fileobj, shx=shx_fileobj, dbf=dbf_fileobj)
geometries = shape_file.shapes()
geom = [pygeoif.geometry.as_shape(geometries[0])]
wkt = pygeoif.MultiPolygon(geom)
  • 1
    are any of your geometries complex (e.g. GeometryCollection, Curves, 3D) or null/empty? if pyshp supports geo interface it may not do so for all types, or may be having problems with empty geometries? – Steven Kay Jun 20 '18 at 11:49
  • May be. How do I check for the type and emptyness? – Basile Jun 20 '18 at 11:59
2

Look at Python Geo_interface applications and especially PyShp_geointerface.py

Pyshp geo_interface (GeoJSON-like protocol for geo-spatial (GIS) vector data))

import shapefile
shape_file  = shapefile.Reader("polygons.shp")
# browse geometries
for sr in shape_file.shapes(): 
   print(sr.__geo_interface__) # GeoJSON
{'type': 'Polygon', 'coordinates': (((11.899868449010931, 66.40443537272134), (12.75721845654992, 67.20462871309107), (12.642905122211388, 66.118652036875), (12.414278453534324, 64.26106035387386), (10.899626773548775, 61.83190199918006), (10.64242177128708, 59.94573198259428), (10.55668677053318, 56.544910286022954), (10.385216769025382, 54.001438596990624), (9.32781842639396, 54.001438596990624), (9.32781842639396, 56.65922362036149), (9.69933676299419, 61.48896199616446), (11.899868449010931, 66.40443537272134)),)}
{'type': 'Polygon', 'coordinates': (((13.52883346333501, 68.31918372289175), (14.014665134273772, 69.0622203960922), (14.700545140304964, 68.09055705421468), (14.786280141058862, 55.201728607545206), (14.300448470120102, 53.60134192680576), (13.443098462581112, 55.11599360679131), (13.52883346333501, 68.31918372289175)),)}
{'type': 'Polygon', 'coordinates': (((16.41524515538294, 62.83214367464221), (16.701028491229273, 63.7752286829351), (18.87298184366138, 65.97576036895184), (21.587923534201515, 66.60448370781377), (24.474335226249444, 66.66164037498304), (27.846578589236138, 66.66164037498304), (28.84682026469829, 65.63282036593625), (25.788938571142562, 65.8614470346133), (21.67365853495541, 65.37561536367456), (19.330235181015507, 64.74689202481262), (16.41524515538294, 62.83214367464221)),)}
....

Now you can use any Python library that converts GeoJSON to WKT format ( as pygeoif but also Shapely, Geodaisy, GeoMet and others)

Whith pygeoif

import pygeoif
for sr in shape_file.shapes(): 
     print(pygeoif.geometry.as_shape(sr).wkt)
POLYGON((11.899868449 66.4044353727, 12.7572184565 67.2046287131, 12.6429051222 66.1186520369, 12.4142784535 64.2610603539, 10.8996267735 61.8319019992, 10.6424217713 59.9457319826, 10.5566867705 56.544910286, 10.385216769 54.001438597, 9.32781842639 54.001438597, 9.32781842639 56.6592236204, 9.69933676299 61.4889619962, 11.899868449 66.4044353727))
POLYGON((13.5288334633 68.3191837229, 14.0146651343 69.0622203961, 14.7005451403 68.0905570542, 14.7862801411 55.2017286075, 14.3004484701 53.6013419268, 13.4430984626 55.1159936068, 13.5288334633 68.3191837229))
POLYGON((16.4152451554 62.8321436746, 16.7010284912 63.7752286829, 18.8729818437 65.975760369, 21.5879235342 66.6044837078, 24.4743352262 66.661640375, 27.8465785892 66.661640375, 28.8468202647 65.6328203659, 25.7889385711 65.8614470346, 21.673658535 65.3756153637, 19.330235181 64.7468920248, 16.4152451554 62.8321436746))
...

With Shapely

from shapely.geometry import shape
for sr in shape_file.shapes(): 
  print(shape(sr).wkt)
POLYGON((11.899868449 66.4044353727, 12.7572184565 67.2046287131, 12.6429051222 66.1186520369, 12.4142784535 64.2610603539, 10.8996267735 61.8319019992, 10.6424217713 59.9457319826, 10.5566867705 56.544910286, 10.385216769 54.001438597, 9.32781842639 54.001438597, 9.32781842639 56.6592236204, 9.69933676299 61.4889619962, 11.899868449 66.4044353727))
POLYGON((13.5288334633 68.3191837229, 14.0146651343 69.0622203961, 14.7005451403 68.0905570542, 14.7862801411 55.2017286075, 14.3004484701 53.6013419268, 13.4430984626 55.1159936068, 13.5288334633 68.3191837229))
POLYGON((16.4152451554 62.8321436746, 16.7010284912 63.7752286829, 18.8729818437 65.975760369, 21.5879235342 66.6044837078, 24.4743352262 66.661640375, 27.8465785892 66.661640375, 28.8468202647 65.6328203659, 25.7889385711 65.8614470346, 21.673658535 65.3756153637, 19.330235181 64.7468920248, 16.4152451554 62.8321436746)
...

With Geodaisy

import geodaisy.converters as convert
for sr in shape_file.shapes(): 
     print(convert.geojson_to_wkt(sr.__geo_interface__ ))
POLYGON((11.899868449 66.4044353727, 12.7572184565 67.2046287131, 12.6429051222 66.1186520369, 12.4142784535 64.2610603539, 10.8996267735 61.8319019992, 10.6424217713 59.9457319826, 10.5566867705 56.544910286, 10.385216769 54.001438597, 9.32781842639 54.001438597, 9.32781842639 56.6592236204, 9.69933676299 61.4889619962, 11.899868449 66.4044353727))
POLYGON((13.5288334633 68.3191837229, 14.0146651343 69.0622203961, 14.7005451403 68.0905570542, 14.7862801411 55.2017286075, 14.3004484701 53.6013419268, 13.4430984626 55.1159936068, 13.5288334633 68.3191837229))
POLYGON((16.4152451554 62.8321436746, 16.7010284912 63.7752286829, 18.8729818437 65.975760369, 21.5879235342 66.6044837078, 24.4743352262 66.661640375, 27.8465785892 66.661640375, 28.8468202647 65.6328203659, 25.7889385711 65.8614470346, 21.673658535 65.3756153637, 19.330235181 64.7468920248, 16.4152451554 62.8321436746)
...

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.