Search code examples
htmldata-bindingpopupgeojsonfolium

how do I bind Geojson Geometries to HTML popup in Folium


Hi this is my first post (obviously newB)-the end goal is to be able to click on the polygon and have the poup generate from the geojson file (gdf).

I know the answer will be obvious but I just cant seem to figure it out.

....................

gdf=geopandas.read_file("D:\ALK_gis\Map\Folium\\UL.geojson" ) #create data frame

lot=list(gdf["CLOT"]) #create list of data from GDF
proj=list(gdf["Project"])#create list of data from GDF
covid=list(gdf["COVid"])#create list of data from GDF
geom=list(gdf["geometry"])#create list of data from GDF

fg1=folium.FeatureGroup(name="cadastre") #create a feature group for pop up not sure if this is correct

for (lot,po,uid) in zip(lot,proj,covid): #bind instances and ref in HTML
    pg = folium.Html( f'''<!DOCTYPE html>
    <html><head></head> 
    <Body style="background-color:grey;width: 302px">
    <h1 style="color:rgb(236, 77, 77);background-color: oldlace;bottom:0px;padding-bottom:0px;margin-bottom: 0px; margin-top: 5px; margin-left:2px;width: 298px;">Lot Details</h1>
    <table style="margin-left:0px;">
        <tbody>
            <tr><td style= background-color:oldlace;width:100px> <span style= columns:#343434;3434>CovID</span> </td>
                <td style= background-color:oldlace;width:200px> <span style= color:#343434> {uid}</span> </tr> 
            <tr><td style= background-color:oldlace;width:100px> <span style= columns:#343434;3434>Lot#</span> </td>
                <td style= background-color:oldlace;width:200px> <span style= color:#343434> {lot}</span> </tr>
            <tr><td style= background-color:oldlace;width:100px> <span style= columns:#343434;3434>Project</span> </td>
                <td style= background-color:oldlace;width:200px> <span style= color:#343434> {po}</span> </tr> 
        </tbody>
    </table></Body>''', script=True) 

    pup1=folium.Popup(pg, max_width=2650)
    #not sure if marker is the way to go here
    # fg1.add_child(folium.Marker.(location=gdf["geometry"],popup=pup1)) 

#or bind it some how here 
folium.GeoJson(data=gdf["geometry"]).add_child(pup1).add_to(m)


m.save("Folium/folium.html")

I have the HTML sorted and is poping up how id like on the map but not populating the data. I need to bind the data to the geometry (noted in Blue).

folium HTML popup


Solution

  • My solution:

    gdf=gp.read_file("D:\ALK_gis\Map\Folium\\1UL.shp") #create data frame
    
    gdf_temp=gmc=folium.GeoJson(data=gdf)
    
    fg1=folium.FeatureGroup(name="cadastre")
    
    for feature in gdf_temp.data['features']:
        gmc=folium.GeoJson(feature)
        v1=feature["properties"]['Project']
        v2=feature["properties"]['COVid']
        v3=feature["properties"]['Area']
        pg = folium.Html( f'''<!DOCTYPE html><html><head></head> <Body style="background-color:grey;width: 302px">
     <h1 style="color:rgb(236, 77, 77);background-color: oldlace;bottom:0px;padding-bottom:0px;margin-bottom: 0px; margin-top: 5px; margin-left:2px;width: 298px;">Lot Details</h1>
     <table style="margin-left:0px;">
        <tbody>
             <tr><td style= background-color:oldlace;width:100px> <span style= columns:#343434;3434>Project:</span> </td>
                <td style= background-color:oldlace;width:200px> <span style= color:#343434> {v1}</span> </tr>
             <tr><td style= background-color:oldlace;width:100px> <span style= columns:#343434;3434>CovID:</span> </td>
                <td style= background-color:oldlace;width:200px> <span style= color:#343434> {v2}</span> </tr> 
             <tr><td style= background-color:oldlace;width:100px> <span style= columns:#343434;3434>Area:</span> </td>
                <td style= background-color:oldlace;width:200px> <span style= color:#343434> {v3}</span> </tr>
        </tbody></table></Body>''', script=True) #create HTML
        pup1=folium.Popup(html=pg).add_to(gmc)
        gmc.add_to(fg1)
    
    m.add_child(fg1)
    
    folium.LayerControl().add_to(m)
    
    m.save(outfile="folium.html")