2

I have a geodata frame that looks something like this:

Index Side UniqueID GroupName geometry
0 Blue S1A1B1-D1 Group1 POINT(##.#####, ##.#####)
1 Blue S1A2B1-D1 Group2 POINT(##.#####, ##.#####)
2 Blue S1A1B2-D1 Group1 POINT(##.#####, ##.#####)
3 Blue S1A1B1-D2 Group2 POINT(##.#####, ##.#####)
4 Blue S3A1B1-D1 Group1 POINT(##.#####, ##.#####)
...

I want to create polygons based off the groupname, but retain my ability to use/access/see the groupname column?

I tried:

res = gdf.dissolve("GroupName").convex_hull

Which produced a series of the unique polygons. But I can't really do anything with them? They plot correctly, so that's good. I can see the name on the row

geometry
Group1   POLYGON((##.###### ##.#######, ##.##### ##.#####)...)
Group2   POLYGON((##.###### ##.#######, ##.##### ##.#####)...)

I need to be able to filter which ones are being displayed, by name (ideally side as well, but I can handle that so long as I can at least get to the name), on a dash mapbox.

But I can't seem to access the groupname part of the output?

list(res.select_dtypes('geometry'))[0]

was suggested elsewhere but just spits out 0

1 Answer 1

3

You need to turn your res GeoSeries object into a GeoDataFrame and convert the series index back to a column.

In one line

res = gpd.GeoDataFrame(gdf.dissolve("GroupName").convex_hull).reset_index()

Explanation

import geopandas as gpd

gdf = gpd.read_file("points.shp")
res = gdf.dissolve("GroupName").convex_hull

print(type(res))
# geopandas.geoseries.GeoSeries

## Make it a GDF
res = gpd.GeoDataFrame(gdf.dissolve("GroupName").convex_hull)

## Show GroupName is actually the index 
print(res.index)
# Index(['Group1', 'Group2', 'Group3'], dtype='object', name='GroupName')

# Turn index back into column
res = res.reset_index()

print(gdf.head())
#    id GroupName  geometry
# 0   1    Group1  POINT (xxxx yyyy)

print(res.head())
#   GroupName                                                  0
# 0    Group1  POLYGON ((xxxx yyyy, xxxx yyyy, ...
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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