TransWikia.com

Dissolve spatio temporal data with GeoPandas without missing the temporal part

Geographic Information Systems Asked on June 24, 2021

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

One Answer

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

Correct answer by Kadir Şahbaz on June 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP