2

I have the following geodataframes, corresponding to a regular grid:

        id   grid_id    geometry
0       48         0    (POLYGON ((2.052457758079306 41.42493869117656...
1       49         1    (POLYGON ((2.052470852112577 41.42403805731954...
2       215        2    (POLYGON ((2.053641274433816 41.42584917461342...
3       216        3    (POLYGON ((2.053654352531821 41.42494854059127...
4       217        4    (POLYGON ((2.053667430035439 41.42404790642426...

and a points geodataframe:

    id_act  geometry
0   4001    POINT (2.183026563657264 41.37459702541483)
1   4003    POINT (2.183012216695291 41.37471411724238)
2   4003    POINT (2.183128113845906 41.37472901746361)
3   3002    POINT (2.182820482338962 41.37482095671629)
4   4003    POINT (2.182945418252172 41.37482221760939)

I'm merging the two dataframes through a spatial join:

id_grid = gpd.sjoin(gdf, grid, how="inner", op='intersects')

but it returns the following AttributeError:

AttributeError: 'NoneType' object has no attribute 'bounds'

the point is that when I call the function:

grid.bounds

it yields:

        minx         miny       maxx         maxy
0   2.052458    41.424038   2.053667    41.424949
1   2.052471    41.423137   2.053681    41.424048
2   2.053641    41.424949   2.054851    41.425859
3   2.053654    41.424048   2.054864    41.424958
4   2.053667    41.423147   2.054877    41.424058
5   2.053681    41.422247   2.054890    41.423157

calling both type(gdf.geometry[0]) or type(grid.geometry[0]) gives:

shapely.geometry.point.Point
shapely.geometry.multipolygon.MultiPolygon

respectively.

Does anyone know where is the mistake?

Rodrigo Vargas
  • 273
  • 3
  • 17
  • what will happen if you do `points.bounds`? It looks like you have empty geometry somewhere in the dataset. – martinfleis Jul 10 '19 at 09:40
  • That's the point!: `AttributeError: 'NoneType' object has no attribute 'bounds'` It yields the same error message. Calling `points.total_bounds`, either. I thought the mistake was in the polygon geodataframe. How could I fix it? – Rodrigo Vargas Jul 15 '19 at 14:19

1 Answers1

10

Higly probably, your points geodataframe contains empty geometry. That's why you'll get 'NoneType'. You can drop them via

points_clean = points[points.geometry.type == 'Point']

It will keep only rows with Point type in geometry column. Then your spatial join should work. You might have an empty geometry in your Polygon layers as well. In that case, just adapt the code above from Point to Polygon or MultiPolygon.

martinfleis
  • 7,124
  • 2
  • 22
  • 30
  • I've tried before your answer with: `points_clean = points.loc[points.is_valid]` and seems to work already. Many thanks – Rodrigo Vargas Jul 16 '19 at 11:07
  • 1
    I'm lost how to apply that to a GeoDataFrame - can you add an example e.g. what you would add to fix the OPs code? – Mike Honey Jun 21 '20 at 10:00