I have a list of named polygons:
import pandas as pd
import geopandas as gp
from shapely.geometry import Polygon
from shapely.geometry import Point
import matplotlib.path as mpltPath
df = gp.GeoDataFrame([['a', Polygon([(1, 0), (1, 1), (2,2), (1,2)])],
['b', Polygon([(1, 1), (2,2), (3,1)])]],
columns = ['name','geometry'])
df = gp.GeoDataFrame(df, geometry = 'geometry')
and a list of points:
points = gp.GeoDataFrame( [['box', Point(1.5, 1.75)],
['cone', Point(3.0,2.0)],
['triangle', Point(2.5,1.25)]],
columns=['id', 'geometry'],
geometry='geometry')
I am trying to find out what points are in what polygons and add a column to the point dataframe with 'True' or 'False'
I've previously been shown the methods here which show some ways of quickly doing this and have got the script:
point = points['geometry']
path = mpltPath.Path(df['geometry'])
points['inside'] = path.contains_points(point)
but I get the error: float() argument must be a string or a number, not 'Polygon'
How do I fix this?
Alternatively I have been trying this method:
points['inside'] = []
for geo1 in df['geometry']:
for geo2 in points['geometry']:
if geo1.contains(geo2):
points['inside'].append('True')
however here I also get an error: Length of values des not match length of index
Any help with either of these would be greatly appreciated!