geopandas颜色最接近的形状相同的颜色

问题描述 投票:0回答:1

我有超过100个多边形geopandas文件和稀疏集(〜其中10),有兴趣的值。有一个简单的办法,我来分配剩余90+的多边形基于最近的非零多边形的值的值?

先感谢您

python geospatial polygon geopandas
1个回答
0
投票

下面的代码表示的算法将与“没有价值”加入多边形到最近的多边形与一个有效的值,使用基于它们的质心空间连接(最近的邻居)。

注意代码是你需要的代码的草稿;它表示通用算法,但你需要完成基于您的数据和变量的函数。

# gdf with all the polygons
gdf1 = gpd.read_file(...)

# calculate a column with the centroid geometry; 
# it will be used later.
# note: this is not the active gdf geometry at this stage
gdf1['geometry_pt'] = gdf1['geometry'].centroid

# set the point geometry as the active geometry
gdf1.set_geometry('geometry_pt')

# filter out the gdf in two gdfs, with/without the value you want
gdf1_yesval = gdf1.loc[gdf1['field1'] != 0]
gdf1_noval  = gdf1.loc[gdf1['field1'] == 0]

# perform a spatial join to assign the closest value to the points with no value
# for this, apply the code in the link below
gdf1_noval_joined = gdf1_noval.apply(...  nearest... gdf1_yesval... )

# do the necessary column operations in the joined gdf 
# to update your desired columns with values from the spatially joined gdf
gdf1_noval_joined['field1'] = gdf1_noval_joined['joinedfieldA']

# delete the unnecessary columns in the joined gdf
gdf1_noval_joined.drop(columns=['joinedfieldA', 'joinedfieldB'], inplace=True)

# concatenate the two gdfs to make one
df2 = pd.concat([gdf1_yesval, gdf1_noval_joined])

# convert it into a gdf again
gdf2 = gpd.GeoDataFrame(df2, geometry='geometry')

其中,最近的邻居加入的功能,说明该链接为:https://gis.stackexchange.com/q/222315/93912

希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.