Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将一个比较两个不同数据帧中的shapely对象的函数矢量化?_Python_Pandas_Vectorization_Geopandas_Shapely - Fatal编程技术网

Python 如何将一个比较两个不同数据帧中的shapely对象的函数矢量化?

Python 如何将一个比较两个不同数据帧中的shapely对象的函数矢量化?,python,pandas,vectorization,geopandas,shapely,Python,Pandas,Vectorization,Geopandas,Shapely,我有一个熊猫数据帧和一个geopandas数据帧。在Pandas数据框中,我有一列点,其中包含shapely.geometry点对象。geopandas框架中的几何体列具有多边形对象。我想做的是在Pandas框架中取一个点,测试它是否在geopandas框架中多边形对象的任何内 在pandas框架中的一个新专栏中,我希望看到以下内容。如果点在给定的多边形内(即内调用返回真),我希望点行的新列值为geopandas框架中多边形行中不同列的值 我对这个问题有一个有效的解决方案,但它没有矢量化。有可能

我有一个熊猫数据帧和一个geopandas数据帧。在Pandas数据框中,我有一列点,其中包含
shapely.geometry
对象。geopandas框架中的几何体列具有
多边形
对象。我想做的是在Pandas框架中取一个
,测试它是否在geopandas框架中
多边形
对象的任何

在pandas框架中的一个新专栏中,我希望看到以下内容。如果
在给定的
多边形
内(即
调用返回
),我希望
行的新列值为geopandas框架中
多边形
行中不同列的值

我对这个问题有一个有效的解决方案,但它没有矢量化。有可能将其矢量化吗

例如:

将geopandas导入为gpd
作为pd进口熊猫
从shapely.geometry导入点,多边形
#创建随机帧时,几何图形应该是互斥的
gdf=gpd.GeoDataFrame({'A':[1,2],'geometry':[Polygon([(10,5)、(5,6)]),Polygon([(1,2)、(2,5))]))
#创建随机的熊猫
df=pd.DataFrame({'Foo':['bar','bar'],'Points':[点(4,5),点(1,2)])
#我的非矢量解
df['new']='
对于df.index中的i:
对于gdf.index中的j:
如果df.在[i,'点'].内(gdf.在[j,'几何']):
df.at[i,'new']=gdf.at[j,'A']

这很好,因此当点位于多边形内时,
df['new']
将包含列
gdf['A']
中的任何内容。我希望有一种方法可以将此操作矢量化。

我找到了一种适合我的解决方案。这不是最优雅的,但比循环快得多

def内的矢量化(数组,点):
#创建假值和真值数组
_array=np.array([数组中p的(p)内点])
#当np.where tuple的第一个元素不为空时
如果np.where(_数组)[0].size!=0:
返回np.where(_数组)[0][0]
其他:
返回-1
#创建虚拟值行geopandas frame
#这将在“几何体”列中有一个空多边形对象,在其他任何地方都有NaN
虚拟_值=np.empty((1,gdf.shape[1]))
虚拟_值[:]=np.nan
虚拟值=虚拟值。tolist()[0]
虚拟_值[-1]=多边形()
gdf.loc[-1]=虚拟_值
#在通过调用矢量化函数检索索引的位置使用loc
df['A']=gdf.loc[df['Point'].apply(λx:in_矢量化(gdf['geometry'],x)),'A']到_列表()

您可以计算
多边形
的所有点之间的欧几里德距离。如果距离等于0,这将为您提供一个交点。我的方法如下。请注意,我将从数据帧中获取所有点和多边形点的部分留给您。可能吧,类似于
pandas.Series.toList的函数应提供该功能

import numpy as np
from scipy.spatial.distance import cdist

polygon = [[10,5],[5,6],[1,2],[2,5]]
points = [[4,5],[1,2]]

# return distances between all the items of the two arrays
distances = cdist(polygon,points) 

print(distances)
我们现在要做的就是得到数组中0的索引。如你所见,我们的交点在第三行和第二列,这是多边形的第三项或点的第二项


for i,dist in enumerate(distances.flatten()):
    if dist==0:
        intersect_index = np.unravel_index(i,shape=distances.shape)
        intersect_point = polygon[intersect_index[0]]
        print(intersect_point)
这将为您提供所需的矢量化表单


for i,dist in enumerate(distances.flatten()):
    if dist==0:
        intersect_index = np.unravel_index(i,shape=distances.shape)
        intersect_point = polygon[intersect_index[0]]
        print(intersect_point)
[1,2]