'Plot polygons with buffer of some radius using folium not working properly

I am trying plot the intersection between a buffer circle and the mesh blocks (or boundaries) within that circle of some radius (in this case, 80 km).

I got the intersection using sjoin() as follows:

intersection_MeshBlock = gpd.sjoin(buffer_df, rest_VIC, how='inner', predicate='intersects')

My buffer variable looks like this: buffer_df And the intersection looks like this: intersection

The problem is I am not able to plot the intersection polygons. Here is the plot I get after I plot it using the polygon plotting in folium:

for _, r in intersection_MeshBlock.iterrows():
    # Without simplifying the representation of each borough,
    # the map might not be displayed
    sim_geo = gpd.GeoSeries(r['geometry']).simplify(tolerance=0.00001)
    geo_j = sim_geo.to_json()
    geo_j = folium.GeoJson(data=geo_j,
                           style_function=lambda x: {'fillColor': 'orange'} )
    folium.Popup(r['SA1_CODE21']).add_to(geo_j)
    geo_j.add_to(m)
m

Plot: color filled maps

What am I doing in wrong ways?

EDIT: I might have solved the issue partially. Now, I am able to plot the polygons inside some buffer radius. This is how my plot looks like: meshblocks, exceeding boundary circle

If you see the image, you will realise that there are certain meshblocks that cross the circular boundary region. How do I get rid of everything which is outside that circular region?



Solution 1:[1]

  • have located some geometry for Melbourne to demonstrate
  • fundamentally, you want to use overlay() not sjoin()
  • generation of folium map is much simpler using GeoPandas 0.10 capability explore()
import geopandas as gpd
import numpy as np
import shapely.geometry
import folium

rest_VIC = gpd.read_file(
    "https://raw.githubusercontent.com/codeforgermany/click_that_hood/main/public/data/melbourne.geojson"
)

# select a point randomly from total bounds of geometry
buffer_df = gpd.GeoDataFrame(
    geometry=[
        shapely.geometry.Point(
            np.random.uniform(*rest_VIC.total_bounds[[0, 2]], size=1)[0],
            np.random.uniform(*rest_VIC.total_bounds[[1, 3]], size=1)[0],
        )
    ],
    crs=rest_VIC.crs,
)

buffer_df = gpd.GeoDataFrame(
    geometry=buffer_df.to_crs(buffer_df.estimate_utm_crs())
    .buffer(8 * 10**3)
    .to_crs(buffer_df.crs)
)


# need overlay not sjoin
intersection_MeshBlock = gpd.overlay(buffer_df, rest_VIC, how="intersection")

m = rest_VIC.explore(name="base", style_kwds={"fill":False}, width=400, height=300)
m = buffer_df.explore(m=m, name="buffer", style_kwds={"fill":False})
m = intersection_MeshBlock.explore(m=m, name="intersection", style_kwds={"fillColor":"orange"})
folium.LayerControl().add_to(m)

m

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rob Raymond