简体   繁体   中英

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 . 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?

My data can be found here .

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 . 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

df2.plot(column='population', legend=True, cmap='Blues', edgecolor='Black')

yields:

我正在寻找的结果

Use axes (ax1) to enable plotting several layers on a common axes. Also use zorder, to arrange the layers, higher values put the layer above the ones with lower values.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM