[英]Error: float() argument must be a string or a number, not 'Polygon' when trying to find points in polygons
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' 我试图找出哪些多边形中的点,并使用“ True”或“ 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' 但我得到了错误:float()参数必须是字符串或数字,而不是'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 但是在这里我也得到一个错误:值的长度des与索引的长度不匹配
Any help with either of these would be greatly appreciated! 任何与这些帮助将不胜感激!
I didn't get the "true/false column", but this should do the job: 我没有得到“ true / false列”,但这应该可以完成工作:
points.apply(lambda row: (row['id'], list(map(lambda e: e[0], list(filter(lambda p: p[1].contains(row['geometry']), df.values))))), axis=1)
For each point you get the polygons that contains it, output: 对于每个点,您将得到包含它的多边形,输出:
0 (box, [a])
1 (cone, [])
2 (triangle, [b])
dtype: object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.