【发布时间】:2019-03-08 02:15:08
【问题描述】:
我是使用geopandas 的新手,所以我有一个相当基本的问题。我想确定地理数据框中相邻地点之间发生了多少边界接触。
我将提供一个例子。以下代码读取预加载的地理框架,随机创建标记为“已处理”的国家/地区,定义一个为其邻国提供函数的函数,然后将结果绘制成与边界国家/地区稍浅的阴影。
import geopandas as gp
import numpy as np
import matplotlib.pyplot as plt
path = gp.datasets.get_path('naturalearth_lowres')
earth = gp.read_file(path)
africa = earth[earth.continent=='Africa']
africa['some_places'] = np.random.randint(0,2,size=africa.shape[0])*2
# Define and apply a function that determines which countries touch which others
def touches(x):
result = 0
if x in africa.loc[africa.some_places==2,'geometry']:
result = 2
else:
for y in africa.loc[africa.some_places==2,'geometry']:
if y.touches(x) :
result = 1
break
else:
continue
return result
africa['touch'] = africa.geometry.apply(touches)
# Plot the main places which are 2, the ones that touch which are 1, and the non-touching 0
fig, ax = plt.subplots()
africa.plot(column='touch', cmap='Blues', linewidth=0.5, ax=ax, edgecolor='.2')
ax.axis('off')
plt.show()
对我来说,这给出了以下地图:
现在的问题是,实际上我不想不加选择地将所有区域都涂成浅蓝色。我 - 理想情况下 - 想要确定受处理国家/地区边界的长度,然后根据您与一个或多个受处理国家共享的边界量来计算您受到的影响程度。 p>
至少,我希望能够丢弃与另一个国家只有 1 或 2 英里边界的地方(或者可能在拐角处相遇)。欢迎任何建议或解决方案!
【问题讨论】:
-
两个相邻多边形的交点将为您提供代表共享边界的匀称线串,然后您可以从中获取长度。类似的东西
state1.intersection(state2).length