AttributeError 'list' object has no attribute '_geom'
Why ? Because polygon_list_B is an empty list and not a Shapely geometry in
if polygon_A.intersects(polygon_list_B):
(you don't need == True
, it is implicit)
A possible solution is to iterate through all pairs of geometries in polygon_list_A to find the intersections (as in Finding the common borders between polygons in the same shapefile)
import itertools
polygon_list_B = []
for geom1,geom2 in itertools.combinations(polygon_list_A, 2):
if geom1.intersects(geom2):
polygon_list_B.append(geom1)
polygon_list_A.remove(geom1)
polygon_list_A -> dark green colour, polygon_list_B -> light green colour
If you only want the polygons without intersection
polygon_list_B = []
for geom1,geom2 in itertools.combinations(polygon_list_A, 2):
if geom1.intersects(geom2):
polygon_list_B.append(geom1)
polygon_list_B.append(geom2)
result = [geom for geom in polygon_list_A if geom not in polygon_list_B]
result -> dark green colour, polygon_list_B -> light green colour
Try:
import geopandas as gpd
import pandas as pd
polys = gpd.read_file(r"C:\Test\Shapes.shp")
points = gpd.read_file(r"C:\Test\POI.shp")
dfsjoin = gpd.sjoin(polys, points) #Spatial join Points to polygons
dfpivot = pd.pivot_table(dfsjoin,index='PolyID',columns='Food',aggfunc={'Food':len})
dfpivot.columns = dfpivot.columns.droplevel()
dfpolynew = polys.merge(dfpivot, how='left', on='PolyID')
Data:
Best Answer
Assuming you want to keep the grid polygons intact and just select out those that intersect with the other polygons and not just the part of each grid polygon that intersects with the other polygons.
You can use a spatial join (
sjoin
with predicate = "inner") or a spatial selection (intersects
):