Cartography. Vizualizing Speed Of Movement With Color Scale On Map In Python
I have 3 main values (longitude, latitude and speed) in csv. Using Folium library i can map the location with lon and lat degree.my current code is: import pandas as pd from geopy.
Solution 1:
Try this:
First, write the following two functions
defdraw_polylines(points, speeds, map):
colors = [speed_color(x) for x in speeds]
n = len(colors)
# Need to have a corresponding color for each pointif n != len(points):
raise ValueError
i = 0
j = 1
curr = colors[0]
while i < n and j < n:
if colors[i] != colors[j]:
line = folium.PolyLine(points[i:j], color=curr, weight=2.5, opacity=1)
line.add_to(map)
curr = colors[j]
i = j
j += 1if i < j:
folium.PolyLine(points[i:j], color=curr, weight=2.5, opacity=1).add_to(map)
defspeed_color(speed):
if speed < 0:
raise ValueError
elif speed >= 0and speed < 20:
return'red'elif speed >= 20and speed < 60:
return'yellow'else:
return'green'
Then, after the line
my_map = folium.Map(location=[ave_lt, ave_lg], zoom_start=14)
make this call
draw_polylines(points, df['speed'], my_map)
I tried it on your given data and it seemed to work. Definitely review it for yourself.
Essentially, the line segment colors are decided point by point, using the corresponding speed for each one. If there isn't a speed for every point, or there is a negative speed (write more extensive error testing; data is usually not clean!) this code will throw an error.
Post a Comment for "Cartography. Vizualizing Speed Of Movement With Color Scale On Map In Python"