Note
Go to the end to download the full example code.
Desire and Delaunay lines#
Creating Desire and Delaunay Lines
Creating county-to-county matrices#
sphinx_gallery_thumbnail_path = ‘../../examples/result_analysis/delaunay_lines.png’
from pathlib import Path
import numpy as np
from polaris.analyze.mapping.flow_lines import delaunay_assignment, desire_lines
from polaris.analyze.trip_metrics import TripMetrics
Polaris Studio enables you to create Desire and Delaunay lines for trip matrices between counties or between zones. Let’s show that using the Bloomington model as an example.
project_dir = Path("/tmp/Bloomington")
last_iter = TripMetrics(project_dir / "Bloomington-Supply.sqlite", project_dir / "Bloomington-Demand.sqlite")
# Let's get all the trips for the last iteration
matrix_zones = last_iter.trip_matrix(from_start_time=0, to_start_time=24 * 3600, aggregation="zone")
# And let's see what modes we have:
matrix_zones.matrices
['BUS', 'SOV', 'TAXI']
We can plot the zone-to-zone flows as Delaunay lines#
flows is a GeoDataFrame, so you can save it to a file for use elsewhere if you’d like
flows = delaunay_assignment(project_dir / "Bloomington-Supply.sqlite", aggregation="zone", matrix=matrix_zones)
flows.explore(
style_kwds={
"style_function": lambda x: {
"weight": x["properties"]["SOV_tot"] / flows["SOV_tot"].max() * 10 # scale as needed
}
},
tiles="CartoDB positron",
attr="Map tiles by Stamen Design, CC BY 3.0 — Map data © OpenStreetMap contributors",
)
delaunay : 0%| | 0/185 [00:00<?, ?it/s]
Equilibrium Assignment : 0%| | 0/250 [00:00<?, ?it/s]
Desire lines#
To mix it up a little, we can plot desire lines.
dls = desire_lines(project_dir / "Bloomington-Supply.sqlite", aggregation="zone", matrix=matrix_zones)
But we plot only flows larger than 50 trips, or the map will be impossibly heavy to render/navigate
dls2 = dls[dls["SOV_tot"] >= np.partition(dls["SOV_tot"].to_numpy(), -50)[-50]]
dls2.explore(
style_kwds={
"style_function": lambda x: {
"weight": x["properties"]["SOV_tot"] / dls["SOV_tot"].max() * 20 # scale as needed
}
},
tiles="CartoDB positron",
attr="Map tiles by Stamen Design, CC BY 3.0 — Map data © OpenStreetMap contributors",
)
You can always simply plot the GeoDataFrame using the plot method, but it will not be interactive.
dls.plot(linewidth=5 * dls["SOV_tot"] / dls["SOV_tot"].max(), color="red")

<Axes: >
# If we wanted, we could've done this all for county flows too
# matrix_counties = last_iter.trip_matrix(from_start_time=0, to_start_time=24 * 3600, aggregation="county")
# dls = desire_lines(project_dir / "Bloomington-Supply.sqlite", aggregation="county", matrix=matrix_counties)
# flows = delaunay_assignment(project_dir / "Bloomington-Supply.sqlite", aggregation="county", matrix=matrix_counties)
Total running time of the script: (0 minutes 2.562 seconds)