Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

module 'shapely' has no attribute 'geometry' error

I'm using Clint Harris' solution from this

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")

Here I'm just testing one coordinate with a shapefile, but it appears the code may be out of date. I changed shapefile_record = fiona_collection.next() to shapefile_record = next(iter(fiona_collection)) already but this error I cannot seem to solve:

---------------------------------------------------------------------------
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'
like image 913
compsciman Avatar asked Jan 25 '26 15:01

compsciman


1 Answers

Your code is searching for geometry attribute in shapely module and it is failing. To solve this, import the shapely.geometry module as below.

import shapely.geometry

shape = shapely.geometry.asShape( shapefile_record['geometry'])
like image 61
Mithilesh_Kunal Avatar answered Jan 28 '26 04:01

Mithilesh_Kunal