Search code examples
pythonpython-3.xdata-visualizationgeojsonfolium

Highlight one specific country in Folium


I have a map drawn by folium as follow:

m = folium.Map(location = [51.1657,10.4515], zoom_start=6, min_zoom = 5, max_zoom = 7)

enter image description here

How can I get rid of neighbor countries and just keep Germany? Or alternatively neighbor countries become fade, blur,pale or something like this.


Solution

  • As long as you have a json file containing the geometry (coordinates) for the country of interest, you can add a GeoJson layer:

    import folium
    import json
    
    with open('datasets/world-countries.json') as handle:
        country_geo = json.loads(handle.read())
    
    for i in country_geo['features']:
        if i['properties']['name'] == 'Germany':
            country = i
            break
    
    m = folium.Map(location = [51.1657,10.4515],
                   zoom_start=6,
                   min_zoom = 5,
                   max_zoom = 7)
    
    
    folium.GeoJson(country,
                   name='germany').add_to(m)
    
    folium.LayerControl().add_to(m)
    
    m
    

    and you get:

    enter image description here