3

I am wondering if there's a clever way to use the dissolve() method of GeoPandas with a spatio temporal geodataframe without losing the temporal part.

I have a dataset where for different days of the year the same geometries could repeat. I need to aggregate (dissolve) these geometries by ID but I need to do it for each day.

My geodataframe looks like this:

geometry DATE ID VOL
POINT 2020-01-01 1 100
POINT 2020-01-01 1 50
POINT 2020-01-01 2 50
POINT 2020-01-01 2 50
POINT 2020-01-02 1 70
POINT 2020-01-02 1 40
POINT 2020-01-02 2 30
POINT 2020-01-02 2 70
...

My desired output should be:

geometry DATE ID VOL
MULTIPOINT 2020-01-01 1 150
MULTIPOINT 2020-01-01 2 100
MULTIPOINT 2020-01-02 1 110
MULTIPOINT 2020-01-02 2 100

With gdf.dissolve(by='ID', aggfunc='sum') the result is missing the temporal information, and I understand why, but, is there a way I can aggregate preserving it?

geometry ID VOL
MULTIPOINT 1 260
MULTIPOINT 2 200

Here is the code in order to replicate the test:

import geopandas as gpd
from shapely.geometry import Point

d = {
    'geometry': [Point(1,2), Point(2,1), Point(3,4), Point(4,3),
                 Point(1,2), Point(2,1), Point(3,4), Point(4,3)],
    'DATE': ['2020-01-01', '2020-01-01', '2020-01-01', '2020-01-01',
             '2020-01-02', '2020-01-02', '2020-01-02', '2020-01-02'],
    'ID': [1, 1, 2, 2, 1, 1, 2, 2],
    'VOL': [100, 50, 50, 50, 70, 40, 30, 70 ]
}

gdf = gpd.GeoDataFrame(d, crs="EPSG:4326")
dissolved = gdf.dissolve(by='ID', aggfunc='sum')
3

Change the last line as:

dissolved = gdf.dissolve(by=['DATE', 'ID'], aggfunc='sum').reset_index()

Result:

   DATE         ID  geometry               VOL
0  2020-01-01   1   MULTIPOINT (1 2, 2 1)  150
1  2020-01-01   2   MULTIPOINT (3 4, 4 3)  100
2  2020-01-02   1   MULTIPOINT (1 2, 2 1)  110
3  2020-01-02   2   MULTIPOINT (3 4, 4 3)  100
4
  • Thank you so much!
    – umbe1987
    Jan 14 at 12:05
  • Ehi, thanks again for the answer. I had the opportunity to run the dissolve command against my real dataset (>3 millions of records). All the commands I run like read, merge, sjoin take some seconds to finish. The dissolve hangs indefinitely (so far, I wasn't able to see it ending...). I really don't know why, might be an index issue? (maybe I will dig more into it, but if you already had similar issues please let me know if you can.
    – umbe1987
    Jan 15 at 10:02
  • 1
    I tested the script on a small dataset. Check these searches: geopandas+spatial+index, geopandas+dissolve. Jan 15 at 10:16
  • Thanks for the links. I ended up separating the spatial and temporal parts. This way, I can dissolve my geometries once and not as many times as the days in a year, making my dataset much lighter (i.e, 7893 records, against 4478913). Yeah, it was probably too much for dissolve to handle it in a relatively small amount of time ;) Your answer still remains correct of course and very much appreciated and I will use it (a lot!).
    – umbe1987
    Jan 15 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.