Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/map_plot.py
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)
Copy link
Contributor

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 the plot.py script here.

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
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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 .routes[].geometry). Especially for a map plot, it would be better to use that instead of crow-flies lines as we do in plot.py.

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")