Search code examples
python-3.xgeojsonspatialfolium

how can I create GeoJson for folium Python


can anyone help me with the following GeoJSON, I am trying to work on folium Python Library, trying to plot all the **LineStrings**, but I want it with the seperate colors through the id_ so I gave the ids option in my geoJson, and i want to fillcolor with the id option this id has multiple coordinate points, and there are more than 81 unique ids in it, which makes multiple linestring, and i want to seperate those linestrings with colors

My first question is:

  1. is it possible to do such thing what I am trying to with folium Python
  2. if Yes, please help with my GeoJson File to correct it

this image is what I want to do: enter image description here

cords = list(zip(filteredData.lat.tolist(), filteredData.lon.tolist()))
id_= list (zip(filteredData.id.tolist())) 
subVoyage

line = {
    'type': 'Feature',
    'geometry': {
        'type': 'LineString',
        'coordinates': cords,
        'ids': id_
    },
    'properties':{
        "color": 'black',
        "stroke": "red",
        "stroke-opacity": 0.4,
        "stroke-width": 5,
        "fillcolor" : id_,

    }
}

m1 = folium.Map(location=[36.862317, -76.3151], zoom_start=6)

folium.GeoJson(data, style_function=lambda x: {
        'color' : x['properties']['color'],
        'weight' : x['properties']['stroke-width'],
        'opacity': 1,
        'fillColor' : x['properties']['fillcolor'],
        }).add_to(m1)
m1

Solution

  • It's possible as I've done a demo for your case in a notebook (see below code). Your issue seems to be related to the structure of your GeoJSON.

    import folium
    
    lines = {"type":"FeatureCollection","features":[{"type":"Feature","properties":{"color": "#0000ff", "stroke-width": 1},"geometry":{"type":"LineString","coordinates":[[-1.64794921875,46.46813299215554],[-0.94482421875,47.724544549099676],[-0.263671875,47.97521412341618]]}},{"type":"Feature","properties":{"color": "#00ff00", "stroke-width": 1},"geometry":{"type":"LineString","coordinates":[[-0.263671875,47.97521412341618],[1.58203125,48.67645370777654],[1.8896484375,48.32703913063476],[2.3291015625,48.37084770238366]]}},{"type":"Feature","properties":{"color": "#ff0000", "stroke-width": 1},"geometry":{"type":"LineString","coordinates":[[2.3291015625,48.37084770238366],[2.548828125,48.60385760823255],[3.01025390625,48.66194284607006],[3.251953125,48.42920055556841],[3.5815429687499996,48.531157010976706],[3.779296875,48.32703913063476]]}}]}
    
    
    m1 = folium.Map(location=[45, 1], zoom_start=6)
    
    style_function = lambda x: {
        'color' : x['properties']['color'],
        'weight' : x['properties']['stroke-width']
    }
    
    folium.GeoJson(lines, style_function=style_function).add_to(m1)
    
    m1
    

    You can see the result illustrated (Centered on France due to the GeoJSON I've used)

    Folium