-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Script that generates a map plot of a VROOM solution #60
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
from folium.features import DivIcon | ||
import folium | ||
import json | ||
import sys | ||
|
||
# Given a VROOM solution with latlng locations, generates a map of the paths of the vehicles (up to 15 vehicles) | ||
def plot_map_solution(filepath): | ||
f = open(filepath, 'r+') | ||
vroom_res = json.load(f) | ||
|
||
colors = ['red', 'blue', 'green', 'purple', 'orange', 'darkred', 'lightred', 'darkblue', 'darkgreen', 'cadetblue', 'darkpurple', 'white', 'pink', 'lightblue', 'lightgreen'] | ||
|
||
first_step = vroom_res['routes'][0]['steps'][0] | ||
center_lat = first_step['location'][1] | ||
center_lng = first_step['location'][0] | ||
|
||
map_center = [center_lat, center_lng] | ||
map = folium.Map(location=map_center, zoom_start=10) | ||
Comment on lines
+15
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Depending on the problem extent, we may want a different center and zoom. For example for a country-scale problem, the solution does not fit in the initial view by default. It would be possible to expand a bounding box while going through the routes and steps, then after that fit the map zoom to this bounding box. |
||
|
||
for i, path in enumerate(vroom_res['routes']): | ||
steps = path['steps'] | ||
coords = [] | ||
for j, point in enumerate(steps): | ||
current_lat = point['location'][1] | ||
current_lng = point['location'][0] | ||
|
||
folium.Marker( | ||
location=(current_lat, current_lng), | ||
icon=DivIcon(icon_size=(20,20), | ||
icon_anchor=(0,0), | ||
html=f'<div style="font-size: 12pt">{(j+1) if j != 0 else "START"}</div>') | ||
).add_to(map) | ||
|
||
coords.append((current_lat, current_lng)) | ||
|
||
folium.PolyLine(locations=coords, color=colors[i]).add_to(map) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In most real setups, one wants to get the geometry of the route in output (see I don't know if folium has support for polyline format (that is how the geometry is encoded in the response) but there is probably an easy way to decode that in python and then feed the array of coordinates to folium for the actual route geometry. |
||
|
||
return map | ||
|
||
if __name__ == '__main__': | ||
for f in sys.argv[1:]: | ||
print(f"Plotting {f}...") | ||
mymap = plot_map_solution(f) | ||
filename = f.split('.')[0] | ||
mymap.save(f"{filename}_map_plot.html") | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the 15 limit is related to the size of the
colors
list. It would be possible to loop through that list, pretty much as we do it in theplot.py
script here.