Changing the Projection system for a model

Changing the Projection system for a model#

In this example we show how to change the projection system for a Polaris model.

Image credit to: https://ubc-library-rc.github.io/map-projections/content/diffs-geo-proj.html

from pathlib import Path
from tempfile import gettempdir

from polaris import Polaris
from polaris.network.network import Network
from polaris.project.project_restorer import create_db_from_csv
from polaris.utils.database.db_utils import commit_and_close
from polaris.utils.database.standard_database import DatabaseType
from polaris.utils.testing.temp_model import TempModel

sphinx_gallery_thumbnail_path = ‘../../examples/editing_models/projections.jpg’

model_path = TempModel("Grid")
model = Polaris.from_dir(model_path)
model.upgrade()
old_net = model.network

WARNING

BE CAREFUL WHEN CHOOSING A PROJECTION SYSTEM. It must be projected in meters for Polaris to function properly

reprojected_folder = Path(gettempdir()) / "new_projection"

# Let's say we choose the projection system 5069
old_net.ie.dump(reprojected_folder, target_crs=5069)
new_net_file = Path(gettempdir()) / "Grid-Supply_reprojected.sqlite"

create_db_from_csv(new_net_file, reprojected_folder, DatabaseType.Supply, overwrite=True)
/tmp/new_projection/ev_charging_station_plug_types.csv
/tmp/new_projection/location_links.csv
/tmp/new_projection/transit_stops.csv
/tmp/new_projection/transit_trips_schedule.csv
/tmp/new_projection/transit_routes.csv
/tmp/new_projection/migrations.csv
/tmp/new_projection/area_type.csv
/tmp/new_projection/transit_patterns.csv
/tmp/new_projection/location.csv
/tmp/new_projection/ev_charging_station_plugs.csv
/tmp/new_projection/timing_nested_records.csv
/tmp/new_projection/zone.csv
/tmp/new_projection/phasing_nested_records.csv
/tmp/new_projection/transit_bike.csv
/tmp/new_projection/settings.csv
/tmp/new_projection/use_code.csv
/tmp/new_projection/micromobility_operators.csv
/tmp/new_projection/micromobility_agencies.csv
/tmp/new_projection/connection.csv
/tmp/new_projection/popsyn_region.csv
/tmp/new_projection/transit_fare_attributes.csv
/tmp/new_projection/counties.csv
/tmp/new_projection/parking.csv
/tmp/new_projection/ev_charging_station_service_bays.csv
/tmp/new_projection/transit_walk.csv
/tmp/new_projection/link_type.csv
/tmp/new_projection/timing.csv
/tmp/new_projection/ev_charging_stations.csv
/tmp/new_projection/transit_links.csv
/tmp/new_projection/link.csv
/tmp/new_projection/signal_nested_records.csv
/tmp/new_projection/electricity_grid_transmission.csv
/tmp/new_projection/transit_pattern_links.csv
/tmp/new_projection/transit_fare_rules.csv
/tmp/new_projection/transit_agencies.csv
/tmp/new_projection/transit_zones.csv
/tmp/new_projection/phasing.csv
/tmp/new_projection/transit_trips.csv
/tmp/new_projection/electricity_provider.csv
/tmp/new_projection/transit_modes.csv
/tmp/new_projection/location_parking.csv
/tmp/new_projection/link_overrides.csv
/tmp/new_projection/sign.csv
/tmp/new_projection/node.csv
/tmp/new_projection/micromobility_docks.csv
/tmp/new_projection/signal.csv
/tmp/new_projection/pocket.csv
/tmp/new_projection/about_model.csv
/tmp/new_projection/land_use.csv

We also need to update the length of a few elements in the network to be consistent with the new projection

with commit_and_close(new_net_file, spatial=True) as conn:
    for table in ("Link", "Road_Connectors", "Transit_Links", "Transit_Bike", "Transit_Walk"):
        conn.execute(f"update {table} set length=round(ST_Length(geo), 8)")

Compute the overlay between the zone and the block group geometries

new_net = Network.from_file(new_net_file)

Let’s compare the distances now

new_lengths = new_net.tables.get("Link")[["link", "length"]].set_index("link")
new_lengths.rename(columns={"length": "new_length"}, inplace=True)

old_lengths = old_net.tables.get("Link")[["link", "length"]].set_index("link")
old_lengths.rename(columns={"length": "old_length"}, inplace=True)

old_lengths.join(new_lengths).plot.scatter("old_length", "new_length")
plot changing model projection
<Axes: xlabel='old_length', ylabel='new_length'>
new_net.close()
old_net.close()

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

Gallery generated by Sphinx-Gallery