Search code examples
jsonpython-3.xgeojsonfolium

Creat map with folium


I have a class project with three friends and we have a problem with a follium map. We would like to change the colours according to a number in the departements dictionnary. For now it fill the map with only the colour of the last department, we think the problem come from the border of the different department. Please help us to repare the code or give us advice the tutorials doesn't really help us.Thanks you, have a nice day.

Code:

import folium
import json
departements={'Ain':37 ,'Aisne':42 ,'Allier':34 ,'Alpes-de-Haute-Provence':47 ,'Hautes-Alpes':45 ,'Alpes-Maritimes':73 ,'Ardèche':36 ,'Ardennes':38,'Ariège':40,'Aube':51,'Aude':48,'Aveyron':26,'Bouches-du-Rhône':77,'Calvados':38,'Cantal':23,'Charente':40,'Charente-Maritime':50,'Cher':40,'Corrèze':31,'Côte-d\'Or':41,'Côtes-d\'Armor':33,'Creuse':23,'Dordogne':28,'Doubs':45,'Drôme':51,'Eure':37,'Eure-et-Loir':38,'Finistère':36,'Gard':54,'Haute-Garonne':64,'Gers':27,'Gironde':53,'Hérault':65,'Ille-et-Vilaine':37,'Indre':34,'Indre-et-Loire':42,'Isère':56,'Jura':33,'Landes':40,'Loir-et-Cher':38,'Loire':46,'Haute-Loire':29,'Loire-Atlantique':54,'Loiret':47,'Lot':34,'Lot-et-Garonne':38,'Lozère':26,'Maine-et-Loire':36,'Manche':23,'Marne':54,'Haute-Marne':41,'Mayenne':31,'Meurthe-et-Moselle':47,'Meuse':37,'Morbihan':34,'Moselle':40,'Nièvre':37,'Nord':43,'Oise':50,'Orne':36,'Pas-de-Calais':45,'Puy-de-Dôme':39,'Pyrénées-Atlantiques':37,'Hautes-Pyrénées':42,'Pyrénées-Orientales':55,'Bas-Rhin':39,'Haut-Rhin':38,'Rhône':50,'Haute-Saône':36,'Saône-et-Loire':35,'Sarthe':39,'Savoie':50,'Haute-Savoie':43,'Paris':59,'Seine-Maritime':46,'Seine-et-Marne':59,'Yvelines':39,'Deux-Sèvres':30,'Somme':45,'Tarn':35,'Tarn-et-Garonne':42,'Var':59,'Vaucluse':55,'Vendée':33,'Vienne':40,'Haute-Vienne':33 ,'Vosges':33 ,'Yonne':56 ,'Territoire de Belfort':44 ,'Essonne':53,'Hauts-de-Seine':64 ,'Seine-Saint-Denis':85 ,'Val-de-Marne':63 ,'Val-d\'Oise':59 }



#loading the geojson file
geo =json.load(open("departements.geojson"))
#création of the map 
map = folium.Map(location = [48.856578, 2.351828], zoom_start = 6)
#colorization of the departments
for i in departements:
    if departements[i]<=30:
        folium.GeoJson(geo,style_function =lambda feature: {'fillColor':"#25fde9",'fillOpacity': 1,'opacity': 1 if (feature['properties']['nom']==i) else 'White'    }).add_to(map)
    elif departements[i]<=45 and departements[i]>30:
        folium.GeoJson(geo,style_function =lambda feature: {'fillColor':"#85bd87",'fillOpacity': 1,'opacity': 1 if (feature['properties']['nom']==i) else 'White'}).add_to(map)
    elif departements[i]<=60 and departements[i]>45:
        folium.GeoJson(geo,style_function =lambda feature: {'fillColor':"#ff8800",'fillOpacity': 1,'opacity': 1 if (feature['properties']['nom']==i) else 'White'}).add_to(map)
    elif departements[i]<=75 and departements[i]>60:
        folium.GeoJson(geo,style_function =lambda feature: {'fillColor':"#cc0002",'fillOpacity': 1,'opacity': 1 if (feature['properties']['nom']==i) else 'White'}).add_to(map)
    elif departements[i]>75:
        folium.GeoJson(geo,style_function =lambda feature: {'fillColor':"#000000",'fillOpacity': 1,'opacity': 1 if (feature['properties']['nom']==i) else 'White'}).add_to(map)
#saving the map
map.save("folium_map.html")

Solution

  • you can add style in json file like below :

    import folium
    import json
    departements={'Corse-du-Sud':41,'Haute-Corse':58,'Ain':37 ,'Aisne':42   ,'Allier':34 ,'Alpes-de-Haute-Provence':47 ,'Hautes-Alpes':45 ,'Alpes-Maritimes':73 ,'Ardèche':36 ,'Ardennes':38,'Ariège':40,'Aube':51,'Aude':48,'Aveyron':26,'Bouches-du-Rhône':77,'Calvados':38,'Cantal':23,'Charente':40,'Charente-Maritime':50,'Cher':40,'Corrèze':31,'Côte-d\'Or':41,'Côtes-d\'Armor':33,'Creuse':23,'Dordogne':28,'Doubs':45,'Drôme':51,'Eure':37,'Eure-et-Loir':38,'Finistère':36,'Gard':54,'Haute-Garonne':64,'Gers':27,'Gironde':53,'Hérault':65,'Ille-et-Vilaine':37,'Indre':34,'Indre-et-Loire':42,'Isère':56,'Jura':33,'Landes':40,'Loir-et-Cher':38,'Loire':46,'Haute-Loire':29,'Loire-Atlantique':54,'Loiret':47,'Lot':34,'Lot-et-Garonne':38,'Lozère':26,'Maine-et-Loire':36,'Manche':23,'Marne':54,'Haute-Marne':41,'Mayenne':31,'Meurthe-et-Moselle':47,'Meuse':37,'Morbihan':34,'Moselle':40,'Nièvre':37,'Nord':43,'Oise':50,'Orne':36,'Pas-de-Calais':45,'Puy-de-Dôme':39,'Pyrénées-Atlantiques':37,'Hautes-Pyrénées':42,'Pyrénées-Orientales':55,'Bas-Rhin':39,'Haut-Rhin':38,'Rhône':50,'Haute-Saône':36,'Saône-et-Loire':35,'Sarthe':39,'Savoie':50,'Haute-Savoie':43,'Paris':59,'Seine-Maritime':46,'Seine-et-Marne':59,'Yvelines':39,'Deux-Sèvres':30,'Somme':45,'Tarn':35,'Tarn-et-Garonne':42,'Var':59,'Vaucluse':55,'Vendée':33,'Vienne':40,'Haute-Vienne':33 ,'Vosges':33 ,'Yonne':56 ,'Territoire de Belfort':44 ,'Essonne':53,'Hauts-de-Seine':64 ,'Seine-Saint-Denis':85 ,'Val-de-Marne':63 ,'Val-d\'Oise':59 }
    
    #loading the geojson file
    with open("departements.geojson", 'r') as f:
        geo = json.load(f)
    
    
    # create map
    My_map = folium.Map(location = [48.856578, 2.351828], zoom_start = 6)
    
    # add color with opacity
    for feature in geo['features']:
        if departements[feature['properties']['nom']]<30:
            feature['properties']["fillColor"]='blue'
            feature['properties']["fillOpacity"]=.5
    
       
        elif departements[feature['properties']['nom']]<45:
            feature['properties']["fillColor"]='green'
            feature['properties']["fillOpacity"]=.75
      
        elif departements[feature['properties']['nom']]<60:
            feature['properties']["fillColor"]='black'
            feature['properties']["fillOpacity"]=.75
        
        elif departements[feature['properties']['nom']]<75:
            feature['properties']["fillColor"]='plum'
            feature['properties']["fillOpacity"]=.75
        
        else:
            feature['properties']["fillColor"]='yellow'
            feature['properties']["fillOpacity"]=.75
    
    # map with style
    folium.GeoJson(geo,name='geojson',style_function=lambda feature: {"fillColor": feature['properties']["fillColor"],"fillOpacity":feature['properties']["fillOpacity"]}).add_to(My_map)
    
    #save map
    My_map.save("carte.html")