Matt
Matt

Reputation: 55

Folium + GeoPandas plot rendering Issue: missing geometries (despite existing)

Can someone help me understand why my approach for creating a basemap in folium with geopandas is problematic?

I have aggregated counts that I want to pass as a popup using folium and geopandas. I have a dummy example dataframe (gdf_raw) that has the 9 regions of interest, the geometry of the region (polygons), and a popup_info column with dummy entries. This one, while not clean, at least renders the base map.

But when I try using it on my intended counts with agg_gdf, I get the following error: "Cannot render objects with any missing geometries" f": {data!r}"

I don't understand why this is problematic though. The agg_gdf it has the same columns as the original, plus a column for date and a column for the count. Even if I remove those columns it errors as if geometry is missing, but from what I can tell the rows are populated with their appropriate polygons.

Any insight as to what I'm doing wrong?

Problematic Code:

folium.GeoJson(
    agg_gdf,
    name='geojson',
    style_function=lambda feature: {
        'fillColor': region_colors[feature["properties"]["region"]],
        'color': 'black',
        'weight': 2,
        'fillOpacity': 0.5,
    },

    tooltip=folium.features.GeoJsonTooltip(
        fields=["region"],
        aliases=["Region:"],
        localize=True,
    ),
    popup=folium.features.GeoJsonPopup(
        fields=["popup_info"],
        aliases=["Region:"],
        localize=True,
    ),

).add_to(base_map)

Code that works Fine:

folium.GeoJson(
gdf_raw,
name='geojson',
style_function=lambda feature: {
    'fillColor': region_colors[feature["properties"]["region"]],
    'color': 'black',
    'weight': 2,
    'fillOpacity': 0.5,
},

tooltip=folium.features.GeoJsonTooltip(
    fields=["region"],
    aliases=["Region:"],
    localize=True,
),
popup=folium.features.GeoJsonPopup(
    fields=["region"],
    aliases=["Region:"],
    localize=True,
),
).add_to(base_map)

GPD that yields error GPD that is problematic Example Plot

Upvotes: 0

Views: 28

Answers (1)

Matt
Matt

Reputation: 55

Eureka!

2 issues to note here for newbies such as myself:

  1. always double check during aggregation and groupby methods that you still have a geopandas dataframe instead of a pandas dataframe
  2. geopandas cannot accept datetime objects.

Adding this to the top fixed it:

agg_gdf = gpd.GeoDataFrame(agg_gdf, geometry='geometry')
agg_gdf.crs = "EPSG:4326"
agg_gdf.date = agg_gdf.date.astype(str)

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 

Upvotes: 0

Related Questions