Search code examples
pythonjsongeojsonfolium

How to display information in geojson popup - Python


I have an issue with displaying information in folium popup where the information is retrieved from a JSON file. Currently, my codes are retrieving only the last piece of information from my JSON file and inserting it into all popup, thus all my popup are showing only that one piece of information. I can't seem to work it out where each node has its own unique information in their popup. Any help is appreciated.

# reading JSON file
with open('exportBuilding.geojson') as access_json:
    read_content = json.load(access_json)

feature_access = read_content['features']

# Creating Folium Map
m = folium.Map(location=[1.400150, 103.910172], titles="Punggol", zoom_start=17)
nodeData = os.path.join('exportBuilding.geojson')
geo_json = folium.GeoJson(nodeData)

# retrieve all names and store in popup
for feature_data in feature_access:
    property_data = feature_data['properties']
    geo_json.add_child(folium.Popup(property_data['name'))

geo_json.add_to(m)

Solution

  • For anyone who is facing the same issue as me, there is a function in the latest folium called "GeoJsonPopup" where it will retrieve all information you specify from JSON file and display it in the Popup, solving the issue where all nodes will have its own unique individual information.

    Instead of creating a for loop to loop the entire JSON,

    # reading JSON file
    with open('exportBuilding.geojson') as access_json:
        read_content = json.load(access_json)
    
    feature_access = read_content['features']
    
    # Creating Folium Map
    m = folium.Map(location=[1.400150, 103.910172], titles="Punggol", zoom_start=17)
    nodeData = os.path.join('exportBuilding.geojson')
    
    # This is retrieve all information, in this case is name from my JSON file 
    # and display it into my popup, such that all nodes 
    # will have its own unique information.
    geo_json = folium.GeoJson(nodeData, popup=folium.GeoJsonPopup(fields=['name']))
    
    geo_json.add_to(m)