Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print a line/multiline in a new layer over a map using Folium

I have tried Python folium library with impressive results, but there is one feature I am missing, or in any case I can't find: I want to print a multiline in a new layer over the map.

If I check de documentation, I can only find how to add markers and poligon markers. But about printing in a new layer, I can only find examples like this one.

I need something much simplier than that. I guess I could insert a GeoJSON with the multiline info in a similar way, but I haven't been able to even find which format should that GeoJSON have.

Any idea for getting my multiline?

PD: If you don't know how to achieve this using Python/Folium, I will be happy to hear what should I add to the Javascript output to get the multiline using Leaflet (that's what Folium library is using).

like image 272
Roman Rdgz Avatar asked May 28 '14 07:05

Roman Rdgz


3 Answers

Some of the functions in the earlier example are now deprecated; apparently, the preferred method is now something like:

import folium

# Coordinates are 10 points on the great circle from Boston to
# San Francisco.
# Reference: http://williams.best.vwh.net/avform.htm#Intermediate
coordinates = [
    [42.3581, -71.0636],
    [42.82995815, -74.78991444],
    [43.17929819, -78.56603306],
    [43.40320216, -82.37774519],
    [43.49975489, -86.20965845],
    [41.4338549, -108.74485069],
    [40.67471747, -112.29609954],
    [39.8093434, -115.76190821],
    [38.84352776, -119.13665678],
    [37.7833, -122.4167]]

# Create the map and add the line
m = folium.Map(location=[41.9, -97.3], zoom_start=4)
my_PolyLine=folium.PolyLine(locations=coordinates,weight=5)
m.add_child(my_PolyLine)
# m.save('line_example_newer.html')
like image 188
Bill Bradley Avatar answered Nov 15 '22 19:11

Bill Bradley


I finally found a way implemented in Folium in January 2014 and not documented. Its the line method.

Here appears an example provided by the author of this addon.

like image 25
Roman Rdgz Avatar answered Nov 15 '22 19:11

Roman Rdgz


Neither of the above worked for me for adding lines as a new layer to a folium.Map object (using folium 0.11). What works for me is using folium.FeatureGroup:

coords = [[[42.3554025, -71.0728116], [42.3554142, -71.0728438]],
 [[42.3554142, -71.0728438], [42.3554296, -71.0728738]]]
test_map = folium.Map([42.3554025, -71.0728116], tiles='Cartodb Positron', zoom_start=15)
fg = folium.FeatureGroup("Lines")
folium.PolyLine(coords).add_to(fg)
f.add_to(test_map)
folium.LayerControl(position='bottomright').add_to(test_map)
test_map

This prints a map that has a "Lines" layer which, when toggled, will show the lines plotted at the coordinates above.

like image 28
Ben Avatar answered Nov 15 '22 19:11

Ben