2

I have two geodataframes that describes objects in two adjacent tiles. I would like to merge these two dataframes so that I can obtain a single dataframe that contains all the polygons. Using the overlay command I was able to get a dataframe that describes all the polygons however it did not merge the other fields. To elaborate:

P1:

       class     obj_id             geometry
0        0      tile_2200-3000_0    POLYGON ((543390.601 3685451.895, 543335.035 3...
1        0      tile_2200-3000_1    POLYGON ((543296.328 3685553.378, 543266.931 3...
2        0      tile_2200-3000_2    POLYGON ((543405.571 3685445.190, 543408.318 3...

P2:

        class      obj_id            geometry
0        0      tile_2200-3800_0    POLYGON ((543538.995 3685280.603, 543501.395 3...
1        0      tile_2200-3800_1    POLYGON ((543380.903 3685353.924, 543382.869 3...

Overlaying P1 and P2 ( via geopandas.overlay(p1, p2, how="union"))

       class_1         obj_id_1          class_2    obj_id_2             geometry
0        0          tile_2200-3000_0        NaN        NaN           POLYGON ((543390.601 3685451.895, 543335.035 3...
1        0          tile_2200-3000_1        NaN        NaN           POLYGON ((543296.328 3685553.378, 543266.931 3...
2        0          tile_2200-3000_2        NaN        NaN           POLYGON ((543405.571 3685445.190, 543408.318 3...
3        NaN        NaN                      0     tile_2200-3800_0  POLYGON ((543538.995 3685280.603, 543501.395 3...
4        NaN        NaN                      0     tile_2200-3800_1  POLYGON ((543380.903 3685353.924, 543382.869 3...

The overlayed result looks fine, but the class and obj_id columns were not merged. Per the documentation located here about union overlays, I understand this is the expected behaviour, but I would like the merged geodataframe to have a single field for class and obj_id. This way I would be able to easily filter through objects of a specific class while viewing on QGIS.

How can I go about achieving this?

There might be a better way to do this merge process.

1 Answer 1

1

Instead of overlay, I just needed to concat the dataframes using pandas as follows:

geopandas.GeoDataFrame(pd.concat( gpdlist, ignore_index=True), crs=gpds[0].crs)

This creates a geodataframe as I expect:

       class        obj_id              geometry
0        0      tile_2200-3000_0    POLYGON ((543390.601 3685451.895, 543335.035 3...
1        0      tile_2200-3000_1    POLYGON ((543296.328 3685553.378, 543266.931 3...
2        0      tile_2200-3000_2    POLYGON ((543405.571 3685445.190, 543408.318 3...
3        0      tile_2200-3800_0    POLYGON ((543538.995 3685280.603, 543501.395 3...
4        0      tile_2200-3800_1    POLYGON ((543380.903 3685353.924, 543382.869 3...

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.