Note
Go to the end to download the full example code.
Building a model from GMNS#
In this example we create a model for Marshalltown, Iowa, from GMNS data generated from OSM data.
To create a network file from OSM, you can either use AequilibraE or OSM2GMNS
Imports#
from pathlib import Path
from tempfile import mkdtemp
import pandas as pd
# Figure out where our polaris directory is (so we can get some test data)
from polaris import __file__ as polaris_dir
from polaris.network.create.triggers import create_network_triggers, delete_network_triggers
from polaris.network.network import Network
from polaris.utils.database.db_utils import commit_and_close
polaris_dir = Path(polaris_dir).parent.parent
✅ Friendly exception handler installed! [to disable set FRIENDLY_ERRORS_DISABLED=1 before importing]
Creates a new supply file with the chosen SRID sphinx_gallery_thumbnail_path = ‘../../examples/model_building/marshalltown.png’
data_folder = polaris_dir / "tests" / "data" / "docs" / "marshalltown"
srid = 3857
project_dir = mkdtemp(prefix="polaris_marshalltown_from_gmns_")
Network.create(Path(project_dir) / "Marshalltown-Supply.sqlite", srid=srid, jumpstart=True)
2026-01-15 23:30:10 UTC+0000 - Creating empty db: /tmp/polaris_marshalltown_from_gmns_cb0mdfq0/Marshalltown-Supply.sqlite (jumpstart=True, with_defaults=True)
2026-01-15 23:30:10 UTC+0000 - Creating file at /tmp/polaris_marshalltown_from_gmns_cb0mdfq0/Marshalltown-Supply.sqlite
2026-01-15 23:30:10 UTC+0000 - Adding Spatialite infrastructure to the database
2026-01-15 23:30:10 UTC+0000 - Creating Tables
2026-01-15 23:30:24 UTC+0000 - Creating Tables: Done in 0:00:14.104152 seconds
2026-01-15 23:30:24 UTC+0000 - Creating empty db: /tmp/polaris_marshalltown_from_gmns_cb0mdfq0/Marshalltown-Supply.sqlite (jumpstart=True, with_defaults=True): Done in 0:00:14.163777 seconds
2026-01-15 23:30:24 UTC+0000 - Creating triggers for version 99999999
Opens the newly-created network and import the GMNS network into it
network = Network()
network.open(Path(project_dir) / "Marshalltown-Supply.sqlite")
network.ie.from_gmns(str(data_folder), "epsg:4326")
2026-01-15 23:30:25 UTC+0000 - Working with file on /tmp/polaris_marshalltown_from_gmns_cb0mdfq0/Marshalltown-Supply.sqlite
2026-01-15 23:30:25 UTC+0000 - Importing Nodes
2026-01-15 23:30:43 UTC+0000 - Importing Links
2026-01-15 23:30:47 UTC+0000 - Importing Zones
2026-01-15 23:30:47 UTC+0000 - Importing Locations
2026-01-15 23:30:47 UTC+0000 - No location file to import from GMNS
2026-01-15 23:30:47 UTC+0000 - Finished GMNS import
Unfortunately, Polaris still does not support arbitrary link types due to hard coded parameters in the simulator, so we will have to map them manually.
crosswalk = {
"WALK": "WALKWAY",
"TRANSIT": "BUSWAY",
"RESIDENTIAL": "LOCAL",
"SERVICE": "OTHER",
"SECONDARY": "MINOR",
"CYCLEWAY": "BIKEWAY", # In Polaris, we do not have a type for both bike and walk
"TERTIARY": "COLLECTOR",
"PATH": "WALKWAY",
"UNCLASSIFIED": "OTHER",
"PRIMARY": "MAJOR",
"TRUNK": "PRINCIPAL",
"MOTORWAY": "EXPRESSWAY",
"TRACK": "WALKWAY",
}
with commit_and_close(network.path_to_file, spatial=True) as conn:
for k, v in crosswalk.items():
conn.execute("""UPDATE Link SET "type"=? WHERE "type"=?;""", [v, k])
conn.commit()
conn.execute("DELETE FROM Link_type WHERE link_type=?;", [k])
We also did not did the necessary data imputation into this GMNS network before importing into Polaris, so let’s do it now. This adds a free-flow speed of 10 m/s and 1 lane per direction for most links, which is a TERRIBLE data imputation practice, but suffices for the purpose of an example
sqls = [
"""UPDATE Link SET lanes_ab=min(1, lanes_ab) where "type" !='EXPRESSWAY';""",
"""UPDATE Link SET lanes_ba=min(1, lanes_ba) where "type" !='EXPRESSWAY';""",
"""UPDATE Link SET fspd_ab=min(10, fspd_ab) where lanes_ab>0;""",
"""UPDATE Link SET fspd_ba=min(10, fspd_ba) where lanes_ba>0;""",
]
with commit_and_close(network.path_to_file, spatial=True) as conn:
delete_network_triggers(conn)
for sql in sqls:
conn.execute(sql)
conn.commit()
create_network_triggers(conn)
2026-01-15 23:30:47 UTC+0000 - Deleting triggers
2026-01-15 23:30:47 UTC+0000 - Creating triggers for version 99999999
transit = network.transit
transit.purge()
feed = transit.new_gtfs(
file_path=data_folder / "marshalltownmunicipaltransit-ia-us.zip",
description="Marshalltown impressive PT system",
agency="MTPT",
)
# You can set map-matching on to get all routes following the network you have in your model
# This task is time-consuming, however
feed.set_allow_map_match(False)
feed.set_date("2019-10-08") # We should be a pick date very carefully
feed.path_store.threshold = 5000 # Let's assume we are on a machine with little memory
feed.set_do_raw_shapes(True)
transit.fix_connections_table()
2026-01-15 23:30:48 UTC+0000 - Clearing transit tables
2026-01-15 23:30:48 UTC+0000 - Table "TRANSIT_RAW_SHAPES" does not exist in the network
2026-01-15 23:30:48 UTC+0000 - Creating GTFS feed object for /home/gitlab-runner/builds/polaris/code/polarislib/tests/data/docs/marshalltown/marshalltownmunicipaltransit-ia-us.zip
We can also set a maximum speed to fix possible GTFS nonsense
max_speeds = pd.read_csv(data_folder / "transit_max_speeds.csv")
feed.set_maximum_speeds(max_speeds)
max_speeds.head()
We do the transit import
feed.doWork()
2026-01-15 23:30:48 UTC+0000 - Loading data for 2019-10-08 from the MTPT GTFS feed. This may take some time
2026-01-15 23:30:48 UTC+0000 - Starting deconflict_stop_times
2026-01-15 23:30:49 UTC+0000 - There were a total of 301 segments that were too fast and were corrected
2026-01-15 23:30:49 UTC+0000 - Building data structures
2026-01-15 23:30:49 UTC+0000 - Importing feed for agency MTPT on 2019-10-08
2026-01-15 23:30:49 UTC+0000 - Creating transit raw shapes for agency ID: 2
2026-01-15 23:30:49 UTC+0000 - Finished creating raw shapes
You also MUST rebuild network.active.build()
The network still doesn’t have zones, locations or other things, so we won’t bother about running consistency checks before we close it
network.close()
Total running time of the script: (0 minutes 40.920 seconds)