Retrieving model tables#

Reading SQLite tables into Pandas DataFrames, as well as SpatiaLite tables into GeoDataFrames are frequent operations when working with Polaris models. This example demonstrates how to retrieve model data using the Polaris API.

sphinx_gallery_thumbnail_path = ‘../../examples/working_with_models/tables.png’

from pathlib import Path

from polaris import Polaris

# Let's open the model
pol = Polaris.from_dir(Path("/tmp/Grid"))
pol.network.tables.get("Link_type")
link_type rank use_codes turn_pocket_length turn_pockets minimum_speed speed_limit alternative_labels notes
0 FREEWAY 10 ANY|AUTO|TRUCK|BUS|SOV|HOV2|HOV3|HOV4|LIGHTTRU... 200.0 1 25 33 None None
1 EXPRESSWAY 20 ANY|AUTO|TRUCK|BUS|SOV|HOV2|HOV3|HOV4|LIGHTTRU... 200.0 1 25 33 XPRESSWAY None
2 PRINCIPAL 30 ANY|WALK|BICYCLE|AUTO|TRUCK|BUS|SOV|HOV2|HOV3|... 50.0 1 10 28 None None
3 MAJOR 40 ANY|WALK|BICYCLE|AUTO|TRUCK|BUS|SOV|HOV2|HOV3|... 50.0 1 10 28 PRIARTER None
4 MINOR 50 ANY|WALK|BICYCLE|AUTO|TRUCK|BUS|SOV|HOV2|HOV3|... 50.0 1 10 28 SECARTER None
5 COLLECTOR 60 ANY|WALK|BICYCLE|AUTO|TRUCK|BUS|SOV|HOV2|HOV3|... 50.0 1 10 27 None None
6 LOCAL_THRU 70 ANY|WALK|BICYCLE|AUTO|TRUCK|BUS|SOV|HOV2|HOV3|... 50.0 0 10 22 THRU|LOCAL_ACCESS None
7 LOCAL 80 ANY|WALK|BICYCLE|AUTO|TRUCK|BUS|SOV|HOV2|HOV3|... 50.0 0 10 22 None None
8 FRONTAGE 90 ANY|WALK|BICYCLE|AUTO|TRUCK|BUS|SOV|HOV2|HOV3|... 50.0 1 10 28 None None
9 RAMP 100 ANY|AUTO|TRUCK|BUS|SOV|HOV2|HOV3|HOV4|LIGHTTRU... 50.0 0 15 33 None None
10 BRIDGE 110 ANY|WALK|BICYCLE|AUTO|TRUCK|BUS|SOV|HOV2|HOV3|... 50.0 1 10 33 None None
11 TUNNEL 120 ANY|WALK|BICYCLE|AUTO|TRUCK|BUS|SOV|HOV2|HOV3|... 50.0 0 10 33 None None
12 EXTERNAL 130 ANY|WALK|BICYCLE|AUTO|TRUCK|BUS|RAIL|SOV|HOV2|... 50.0 0 25 33 ZONECONN None
13 WALKWAY 200 ANY|WALK|RESTRICTED|NONE 50.0 0 0 10 None None
14 BIKEWAY 210 ANY|BICYCLE|RESTRICTED|NONE 50.0 0 0 10 None None
15 BUSWAY 220 ANY|BUS|RESTRICTED|NONE 50.0 0 10 10 None None
16 LIGHTRAIL 300 ANY|RAIL|RESTRICTED|NONE 50.0 0 0 20 None None
17 HEAVYRAIL 310 ANY|RAIL|RESTRICTED|NONE 50.0 0 0 20 None None
18 FERRY 400 ANY|RESTRICTED|NONE 50.0 0 0 20 None None
19 OTHER 500 ANY|WALK|BICYCLE|AUTO|TRUCK|BUS|RAIL|SOV|HOV2|... 50.0 1 10 27 None None
20 WALK 700 any 50.0 0 0 10 None None
21 TRANSIT 600 any 50.0 0 0 10 None None


pol.network.tables.get("Node")
node x y z control_type zone geo
0 1 431767.449826 4.596443e+06 None signal 4 POINT (431767.45 4596443.125)
1 2 411760.762253 4.596370e+06 None signal 8 POINT (411760.762 4596370.319)
2 3 411731.243096 4.589465e+06 None None 7 POINT (411731.243 4589465.163)
3 4 431958.954788 4.589262e+06 None None 4 POINT (431958.955 4589261.689)
4 5 411751.863484 4.594289e+06 None signal 7 POINT (411751.863 4594288.709)
... ... ... ... ... ... ... ...
419 420 433610.079400 4.601625e+06 None None 3 POINT (433610.079 4601624.578)
420 421 432734.746401 4.587611e+06 None None 4 POINT (432734.746 4587610.632)
421 422 433882.199144 4.588717e+06 None None 4 POINT (433882.199 4588716.865)
422 423 409605.267168 4.588894e+06 None None 8 POINT (409605.267 4588894.107)
423 424 410834.263914 4.587793e+06 None None 7 POINT (410834.264 4587792.599)

424 rows × 7 columns



We can also use the DataTableAccess class to access the tables without first opening the model

from polaris.utils.database.data_table_access import DataTableAccess

tables = DataTableAccess(Path("/tmp/Grid/Grid-Supply.sqlite"))
tables.get("node")
node x y z control_type zone geo
0 1 431767.449826 4.596443e+06 None signal 4 POINT (431767.45 4596443.125)
1 2 411760.762253 4.596370e+06 None signal 8 POINT (411760.762 4596370.319)
2 3 411731.243096 4.589465e+06 None None 7 POINT (411731.243 4589465.163)
3 4 431958.954788 4.589262e+06 None None 4 POINT (431958.955 4589261.689)
4 5 411751.863484 4.594289e+06 None signal 7 POINT (411751.863 4594288.709)
... ... ... ... ... ... ... ...
419 420 433610.079400 4.601625e+06 None None 3 POINT (433610.079 4601624.578)
420 421 432734.746401 4.587611e+06 None None 4 POINT (432734.746 4587610.632)
421 422 433882.199144 4.588717e+06 None None 4 POINT (433882.199 4588716.865)
422 423 409605.267168 4.588894e+06 None None 8 POINT (409605.267 4588894.107)
423 424 410834.263914 4.587793e+06 None None 7 POINT (410834.264 4587792.599)

424 rows × 7 columns



This same API is available for the demand and freight databases as well#

pol.freight.tables.get(“Establishments”) pol.demand.tables.get(“Activity”)

pol.close()

Total running time of the script: (0 minutes 0.087 seconds)

Gallery generated by Sphinx-Gallery