com*_*man 2 python python-import attributeerror shapely
我正在使用克林特·哈里斯的解决方案
import fiona
import shapely
with fiona.open("./areas_shp.shp") as fiona_collection:
shapefile_record = next(iter(fiona_collection))
shape = shapely.geometry.asShape( shapefile_record['geometry'] )
point = shapely.geometry.Point(coords[0])
for point in coords:
if (shape.contains(point)):
print("yay")
Run Code Online (Sandbox Code Playgroud)
在这里,我只是用 shapefile 测试一个坐标,但看起来代码可能已经过时了。我已经更改shapefile_record = fiona_collection.next()为shapefile_record = next(iter(fiona_collection)),但这个错误我似乎无法解决:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-8-18ca8979d01f> in <module>
38 with fiona.open("./areas_shp.shp") as fiona_collection:
39 shapefile_record = next(iter(fiona_collection))
---> 40 shape = shapely.geometry.asShape( shapefile_record['geometry'] )
41 point = shapely.geometry.Point(coords[0])
42 for point in coords:
AttributeError: module 'shapely' has no attribute 'geometry'
Run Code Online (Sandbox Code Playgroud)
您的代码正在 shapely 模块中搜索几何属性,但失败了。要解决此问题,请shapely.geometry按如下方式导入模块。
import shapely.geometry
shape = shapely.geometry.asShape( shapefile_record['geometry'])
Run Code Online (Sandbox Code Playgroud)