Bring your own network and demand#
openmobisim can build a network from OpenStreetMap (Build a case), but a study usually has its own network and demand. Both come in through standard files. Every column is described in Input files.
A network in GMNS#
GMNS (the General Modeling Network Specification) is a plain-CSV standard: a node.csv, a link.csv and an optional
config.csv with units. Other tools such as DTALite, osm2gmns and path4gmns read and write it.
network = ms.network_read_gmns("my_network/")
ms.network_write_gmns(network, "exported/") # road/, bike/ and walk/, one GMNS network each
One folder per layer. A folder holding
road/,bike/andwalk/is read as the road network with its bike and walk layers, exactly as written. A single GMNS folder, as other tools write, is read as the road network. Its layers are then derived from it: bikes on every road but motorways, in the road’s direction; walking on every road but motorways and cycleways, both ways.Units are converted on reading: lengths in
m,km,miorft, and speeds inkmhormph, asconfig.csvsays. Capacities are per lane, GMNS’s convention.Ids are renumbered. A network written by openmobisim has its ids as zero-padded indices. After a round trip, give link values and edits by the new ids.
A network as a link table#
network = ms.network_read_table("links.csv", "nodes.csv", length_unit="km", speed_unit="km_h")
Any delimited file works: CSV, TSV, or the TNTP format of the classic benchmark networks. You can also pass rows from a
data frame (df.to_dict("records")). from and to are required; the length, lanes, free-flow speed or time,
capacity and class are optional. A link without a class or lanes takes the defaults of its class. A class the table
does not recognise is treated as unclassified, with a warning.
Check it#
report = ms.network_check(network)
This finds what would make trips fail, such as pieces the cars cannot cross between, and what is worth a look on a map. See Build a case.
Demand#
Trips with their own times and places:
trips = ms.demand_read_trips("trips.csv") # or a trips.parquet path given to Scenario.from_parts
The columns are traveller_id, trip_seq, origin_lon, origin_lat, destination_lon, destination_lat,
departure_time_s and user_class, and optionally weight and mode.
In memory, in any of three forms, the same columns:
trips = [ms.Trip("t1", 0, 4.36, 52.01, 4.37, 52.00, 7 * 3600 + 900, "commuter")] # rows; weight and mode optional
trips = frame # a pandas or polars data frame with the columns
trips = {"traveller_id": [...], "trip_seq": [...], ...} # a dict of columns
scenario = ms.Scenario.from_parts(network, trips, classes=classes)
ms.Trip is a named tuple of one row; a plain tuple of the same fields is the same row. Every row is checked before
the run: a latitude outside ±90, a departure that is not whole seconds after midnight or an unknown mode is refused
with the row’s number and the field.
An origin–destination matrix between zones:
zones = ms.demand_read_zones("zones.csv") # each zone a point
od = ms.demand_read_od("od.csv") # origin_zone, destination_zone, trips, …
trips = ms.demand_from_od(od, zones, start_s=7.5 * 3600, end_s=8.5 * 3600, profile="am_peak", seed=0)
openmobisim.demand_from_od() turns the matrix into whole trips and draws their departures over the period.
It spreads their start and end points around the zone’s point. A matrix row may name its own class, mode and
departure period.
Write the trips out with ms.demand_write_trips(trips, "trips.csv") to share them, or to read them elsewhere.
A worked example: a small town in GMNS#
A network made by hand, as another tool would write it: a grid of 5 by 5 junctions 300 m apart, residential streets
inside and a secondary road around them. Each street is one row with directed false, read as one link each way;
config.csv gives the units.
# A grid town of 5 x 5 junctions, 300 m apart: residential streets inside, a secondary road around it.
from pathlib import Path
import openmobisim as ms
import pandas as pd
folder = Path("town")
folder.mkdir(exist_ok=True)
step_lon, step_lat = 0.0044, 0.0027 # about 300 m at 52° N
nodes = pd.DataFrame(
[{"node_id": 10 * i + j, "x_coord": round(4.35 + j * step_lon, 6), "y_coord": round(52.0 + i * step_lat, 6)}
for i in range(5) for j in range(5)]
)
links = []
for i in range(5):
for j in range(5):
for di, dj in ((0, 1), (1, 0)):
if i + di < 5 and j + dj < 5:
edge = i in (0, 4) and di == 0 or j in (0, 4) and dj == 0
links.append({
"link_id": len(links),
"from_node_id": 10 * i + j,
"to_node_id": 10 * (i + di) + j + dj,
"directed": False, # a two-way street: read as two links
"length": 300,
"facility_type": "secondary" if edge else "residential",
})
nodes.to_csv(folder / "node.csv", index=False)
pd.DataFrame(links).to_csv(folder / "link.csv", index=False)
pd.DataFrame([{"dataset_name": "town", "long_length": "m", "speed": "kmh"}]).to_csv(folder / "config.csv", index=False)
print(len(nodes), "nodes,", len(links), "streets")
25 nodes, 40 streets
Read as one GMNS network, it is the road network, and its bike and walk layers are derived from it:
network = ms.network_read_gmns(folder) # the bike and walk layers derived from the roads
print(network)
report = ms.network_check(network)
report["verdict"]
Network(25 nodes, 80 links)
'pass'
Trips as rows of ms.Trip, between random junctions over an hour. Without a class table a trip’s user_class is only
a label, and a scenario given no modes offers the car alone:
# 2 000 morning trips between random junctions, as rows.
import random
rng = random.Random(1)
corners = list(zip(nodes["x_coord"], nodes["y_coord"], strict=True))
trips = []
for k in range(2000):
(olon, olat), (dlon, dlat) = rng.sample(corners, 2)
trips.append(ms.Trip(f"p{k}", 0, olon, olat, dlon, dlat, 7 * 3600 + rng.randrange(3600), "default"))
trips[0]
Trip(traveller_id='p0', trip_seq=0, origin_lon=4.3676, origin_lat=52.0, destination_lon=4.3632, destination_lat=52.0081, departure_time_s=28671, user_class='default', weight=None, mode=None)
scenario = ms.Scenario.from_parts(network, trips, link_bin_s=900) # per-link results by quarter hour, for the map
run = scenario.run("town")
Run 'town': 0.0 s, window 07:00–10:00, 2,000 of 2,000 trips completed, mean trip 1.5 min, verdict good (run.timings() has every stage)
from openmobisim import viz
viz.map_link(run)
For bikes#
A GMNS bike layer keeps its bike_facility (separated, lane, mixed), which bike routes and the choice model use.
A network without one gets a bike layer derived from its roads. Give the bike layer if you have it: separated tracks
and lanes change routes and mode choice.