[英]Deleting inner lines of polygons after dissolving in geopandas
I'm working with a geodataframe of a city where every unit is a district (administrative division).我正在使用一个城市的地理数据框,其中每个单位都是一个地区(行政部门)。 Its plot looks like this:它的情节是这样的:
import geopandas as gpd
df = gpd.read_file('districts_lima.geojson')
df.plot()
Then, I'm merging some geographic units into larger groups, using a variable named zone
.然后,我使用名为zone
的变量将一些地理单位合并为更大的组。 The result is:结果是:
df2 = df.dissolve(by='zone', aggfunc='sum')
df2.plot(column='population', legend=True, cmap='Blues')
My only problem is that when I reproduce the same plot with darker borders, it becomes evident that some of the merged polygons (zones) have inner lines, which are inner district borders from the original geodataframe.我唯一的问题是,当我用较暗的边界重现同一个图时,很明显一些合并的多边形(区域)有内线,这些线是原始地理数据框的内区边界。 This is shown clearly in this plot:这在此图中清楚地显示:
df2.plot(column='population', legend=True, cmap='Blues', edgecolor='black')
Is there a way to use geopandas to delete the inner lines of the polygons so they wouldn't appear in the last plot?有没有办法使用 geopandas 删除多边形的内线,这样它们就不会出现在最后一个图中?
I actually found a good solution that pertains specifically to the fact that my issue is being created after applying the dissolve()
property of geopandas
.我实际上找到了一个很好的解决方案,它特别适用于我的问题是在应用geopandas
dissolve()
属性后geopandas
。 Apparently, the problem was generated by unnoticeable differences in the borderlines of contiguous inner units which prevented the collapsing to delete the interior lines of the resulting polygons.显然,问题是由相邻内部单元的边界线中不明显的差异产生的,这阻止了折叠以删除生成的多边形的内部线。
This is solved by adding a small buffer to widen the polygon lines so those unnoticeable differences are removed and every inner line of the initial polygons actually overlap.这是通过添加一个小缓冲区来加宽多边形线来解决的,这样那些不明显的差异就被消除了,并且初始多边形的每条内线实际上都重叠了。 Specifically, I needed to add:具体来说,我需要添加:
df2['geometry'] = df2['geometry'].buffer(0.0001)
before前
df2 = df.dissolve(by='zone', aggfunc='sum')
So now the plot command所以现在 plot 命令
df2.plot(column='population', legend=True, cmap='Blues', edgecolor='Black')
yields:产量:
Use axes (ax1) to enable plotting several layers on a common axes.使用轴 (ax1) 启用在公共轴上绘制多个图层。 Also use zorder, to arrange the layers, higher values put the layer above the ones with lower values.还可以使用 zorder 来排列图层,较高的值将图层置于较低值的图层之上。
fig, ax1 = plt.subplots(1, 1)
...
# bottom layer (has thick black lines)
df2.plot(column='population', legend=True, cmap='Blues', edgecolor='black', ax=ax1, zorder=5, lw=6)
# top layers (thin boundary lines)
df2.plot(column='population', legend=True, cmap='Blues', ax=ax1, zorder=6)
Hopefully the top layer will hide almost all the bottom layer, but partial external boundary lines.希望顶层将隐藏几乎所有底层,但部分外部边界线。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.