Search code examples
python-3.xgisgeopandas

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()

districts

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')

population by zones

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')

population by zones with borders

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.


Solution

  • 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:

    the result I was looking for