如何使用地ge以查找最近几何索引
这是如何查找最近几何的索引。我需要在geopandas df target_df
中找到geopandas df source> source_df
中的每个点的最接点。我可以使用shapely
进行此操作,但是当我尝试应用
使用geopandas的代码时,我会得到此错误'typeerror:不可用的类型:'geodataframe'。代码如下:
import geopandas as gpd
import shapely
# Return the field COLUMN of the nearest point in TARGET_DF to each point in SOURCE_DF
def nearest(source_df, target_df, trg_column=None):
# Merge target DF points into MultiPoint object
mpts = target_df.geometry.unary_union
# Find the closest point
curr_pt, near_pt = shapely.ops.nearest_points(source_df['geometry'], mpts)
nearest = target_df['geometry'] == near_pt
# Get the corresponding value from TARGET_DF
value = target_df[nearest][trg_column].values[0]
return value
# MAIN program
if __name__ =="__main__":
# Define dataframe of target points
df_targ = gpd.GeoDataFrame([['point A', shapely.geometry.Point(1,1)],
['point B', shapely.geometry.Point(2,2)],
['point C', shapely.geometry.Point(3,3)],
['point D' ,shapely.geometry.Point(4,4)]],
columns=['t_name', 'geometry'])
# Define points to process and store in GeoPandas DF
dx = 0.1
dy = 0.1
pt_list = []
for indx, pt in enumerate(df_targ['geometry']):
new_pt = shapely.geometry.Point(pt.x + dx, pt.y + dy)
nname = "target_%1d" % indx
pt_list.append([nname, new_pt])
df_sorc = gpd.GeoDataFrame(pt_list, columns=['s_name', 'geometry'])
# Find nearest point using shapely
mpts = df_targ.geometry.unary_union # Merge DF points into MultiPoint object
print("Using shapely:")
for pt in df_sorc['geometry']:
curr_pt, near_pt = shapely.ops.nearest_points(pt, mpts)
near_nam = df_targ.loc[df_targ['geometry'] == near_pt, 't_name'].values[0]
print("nearest point to (%3.1f, %3.1f) is %s" % (pt.x, pt.y, near_nam))
# Find nearest point using Geopandas
df_sorc['nearest_pt'] = df_sorc.apply(nearest, df_targ, trg_column='t_name')
print(df_sorc.head())
我尝试更改Apply
语句如下:
df_sorc['nearest_pt'] = df_sorc.apply(nearest, df_targ, trg_column='t_name', axis=1)
但是现在获取此错误:typeError:apply()获得了参数'axis''
的多个值。感谢任何建议。提前致谢
this is an extension of the question at How to find index of nearest geometry. I need to find the closest point in the Geopandas DF target_df
to each point in the Geopandas DF source_df
. I can do this using shapely
but when I try to apply
the shapely code using Geopandas I get this error 'TypeError: unhashable type: 'GeoDataFrame'
. The code is below:
import geopandas as gpd
import shapely
# Return the field COLUMN of the nearest point in TARGET_DF to each point in SOURCE_DF
def nearest(source_df, target_df, trg_column=None):
# Merge target DF points into MultiPoint object
mpts = target_df.geometry.unary_union
# Find the closest point
curr_pt, near_pt = shapely.ops.nearest_points(source_df['geometry'], mpts)
nearest = target_df['geometry'] == near_pt
# Get the corresponding value from TARGET_DF
value = target_df[nearest][trg_column].values[0]
return value
# MAIN program
if __name__ =="__main__":
# Define dataframe of target points
df_targ = gpd.GeoDataFrame([['point A', shapely.geometry.Point(1,1)],
['point B', shapely.geometry.Point(2,2)],
['point C', shapely.geometry.Point(3,3)],
['point D' ,shapely.geometry.Point(4,4)]],
columns=['t_name', 'geometry'])
# Define points to process and store in GeoPandas DF
dx = 0.1
dy = 0.1
pt_list = []
for indx, pt in enumerate(df_targ['geometry']):
new_pt = shapely.geometry.Point(pt.x + dx, pt.y + dy)
nname = "target_%1d" % indx
pt_list.append([nname, new_pt])
df_sorc = gpd.GeoDataFrame(pt_list, columns=['s_name', 'geometry'])
# Find nearest point using shapely
mpts = df_targ.geometry.unary_union # Merge DF points into MultiPoint object
print("Using shapely:")
for pt in df_sorc['geometry']:
curr_pt, near_pt = shapely.ops.nearest_points(pt, mpts)
near_nam = df_targ.loc[df_targ['geometry'] == near_pt, 't_name'].values[0]
print("nearest point to (%3.1f, %3.1f) is %s" % (pt.x, pt.y, near_nam))
# Find nearest point using Geopandas
df_sorc['nearest_pt'] = df_sorc.apply(nearest, df_targ, trg_column='t_name')
print(df_sorc.head())
I tried changing the apply
statement as follows:
df_sorc['nearest_pt'] = df_sorc.apply(nearest, df_targ, trg_column='t_name', axis=1)
but now get this error: TypeError: apply() got multiple values for argument 'axis'
. Would appreciate any suggestions. Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在这里找到答案:
set_axis()获得了参数'axis'axis'>的多个值。问题似乎是
pandas
更改了版本0.21.0中的应用
函数的参数顺序。我将应用
语句更改为“应用”功能以
避免位置参数问题。这给出了预期的行为
I found the answer here:
set_axis() got multiple values for argument 'axis'. The issue seems to be that
Pandas
changed the argument order of theapply
function in version 0.21.0. I changed theapply
statement toand the apply function
def
toto avoid problems with positional arguments. This gives the expected behaviour