简体   繁体   English

错误:尝试在多边形中寻找点时,float()参数必须为字符串或数字,而不是“多边形”

[英]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.

相关问题 错误:float()参数必须是字符串或数字,而不是'AxesSubplot' - Error: float() argument must be a string or a number, not 'AxesSubplot' 尝试处理时间,但是类型错误:float() 参数必须是字符串或数字 - Trying to work with time but, typeerror: float() argument must be a string or a number float参数必须是字符串或数字 - float argument must be a string or a number 为什么我在尝试 plot mpmath.pcfd 时得到“float() argument must be a string or a number, not 'mpc'”? - Why am I getting 'float() argument must be a string or a number, not 'mpc'' when trying to plot mpmath.pcfd? float() 参数必须是字符串或数字,而不是 'Float' - float() argument must be a string or a number, not 'Float' 循环多边形时,Python匀称无法找到多边形内的点 - Python Shapely Cant Find Points Within Polygon When Looping Polygons OneHotEncoding 错误:类型错误:float() 参数必须是字符串或数字,而不是“时间戳” - OneHotEncoding Error:TypeError: float() argument must be a string or a number, not 'Timestamp' 如何解决“ float()参数必须是字符串或数字,而不是'Timestamp'”错误? - How to fix “float() argument must be a string or a number, not 'Timestamp' ” error? 如何解决“ TypeError:float()参数必须为字符串或数字,而不是'zip'的错误” - how to solve the error of “TypeError: float() argument must be a string or a number, not 'zip'” pyspark 1.6.3线性回归错误float()参数必须为字符串或数字 - pyspark 1.6.3 linear regression error float() argument must be a string or number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM