question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ENH: Support dissolve(by=None) as unary_union of all geoms

See original GitHub issue

In order to have one unique geometry to deal with, I’m trying to dissolve all many edges that compose a route using the .dissolve() method on a GeoDataFrame instance but it throws me an error (while by=None is the default value);

gdf_unique_route = gdf_route_edges.dissolve() # or gdf_route_edges.dissolve(by=None) 

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-160-808abd5a70f0> in <module>
----> 1 gdf_unique_route = gdf_route_edges.dissolve(by=None)

/usr/local/lib/python3.6/dist-packages/geopandas/geodataframe.py in dissolve(self, by, aggfunc, as_index)
    684         # Process non-spatial component
    685         data = self.drop(labels=self.geometry.name, axis=1)
--> 686         aggregated_data = data.groupby(by=by).agg(aggfunc)
    687 
    688         # Process spatial component

/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py in groupby(self, by, axis, level, as_index, sort, group_keys, squeeze, observed)
   5796 
   5797         if level is None and by is None:
-> 5798             raise TypeError("You have to supply one of 'by' and 'level'")
   5799         axis = self._get_axis_number(axis)
   5800 

TypeError: You have to supply one of 'by' and 'level'

If I use by='attribute1 it works, but I end up with a few geometries instead of only one.
I really want to dissolve them all, regardless of their attributes, so I used the default argument values.

Help on the .dissolve() method is as follows:

Help on method dissolve in module geopandas.geodataframe:

dissolve(by=None, aggfunc='first', as_index=True) method of geopandas.geodataframe.GeoDataFrame instance
    Dissolve geometries within `groupby` into single observation.
    This is accomplished by applying the `unary_union` method
    to all geometries within a groupself.
    
    Observations associated with each `groupby` group will be aggregated
    using the `aggfunc`.
    
    Parameters
    ----------
    by : string, default None
        Column whose values define groups to be dissolved
    aggfunc : function or string, default "first"
        Aggregation function for manipulation of data associated
        with each group. Passed to pandas `groupby.agg` method.
    as_index : boolean, default True
        If true, groupby columns become index of result.
    
    Returns
    -------
    GeoDataFrame

There is no such level arg in there, it’s something used by pandas DataFrame .groupy() method actually (which you probably rely on).

As I was facing this error using GeoPandas so I’m reporting it here, but let me know if it should be reported to Pandas developers.

Thanks a lot!

Environment

Ubuntu 18.04
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
Numpy 1.18.2
Pandas 1.0.3
GeoPandas 0.7.0
Anything useful I missed?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
martinfleiscommented, Feb 3, 2021

It will be part of 0.9.0 release in a few weeks.

1reaction
martinfleiscommented, Apr 16, 2020

Another solution on our side would be to do unary_union automatically under the hood of no by is specified. That makes more sense.

Read more comments on GitHub >

github_iconTop Results From Across the Web

pyqgis - Unary union/Dissolve returns null geometry in QGIS 3
In it, I have polygon geometry objects stored in a list polygonGeoms and I want to dissolve all of it into one geometry....
Read more >
UnaryUnionOp (org.locationtech.jts:jts-core 1.19.0 API)
"Dissolved" means that any duplicate (i.e. coincident) line segments or portions of line segments will be reduced to a single line segment in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found