Creation of locations from Overture Maps data

Creation of locations from Overture Maps data#

On this example we show how to use polaris to query Overture Maps and retrieve specific types of amenities to represent as locations on a Polaris Model.

Imports#

import pandas as pd

from polaris.network.consistency.network_objects.location import Location
from polaris.utils.database.db_utils import commit_and_close

sphinx_gallery_thumbnail_path = ‘../../examples/model_building/map_import_osm_locations.png’

from tests.create_test_files import use_test_network

net = Network() net.open(“my_model_path/Chicago-Supply.sqlite”)

net = use_test_network("walk_link_tests")

# Let's delete what's already in the locations table so we can see exactly what we have added
net.conn.execute("DELETE FROM Location")
net.conn.commit()

open_data = net.open_data

Configuring the model#

We need to make sure that all land uses needed for the locations we are creating are in the model

medical_lu = "INSERT OR IGNORE INTO land_use(land_use, is_home, is_work,is_school,is_discretionary,notes) VALUES('MEDICAL',0,1,0,1,'');"
hotel_lu = "INSERT OR IGNORE INTO land_use(land_use, is_home, is_work,is_school,is_discretionary,notes) VALUES('HOTEL',0,1,0,1,'');"
school_lu = "INSERT OR IGNORE INTO land_use(land_use, is_home, is_work,is_school,is_discretionary,notes) VALUES('EDUCATION',0,1,1,0,'');"
univ_lu = "INSERT OR IGNORE INTO land_use(land_use, is_home, is_work,is_school,is_discretionary,notes) VALUES('HIGHER_EDUCATION',0,1,1,0,'');"
for sql in [medical_lu, hotel_lu, school_lu, univ_lu]:
    net.conn.execute(sql)
net.conn.commit()

Hospitals#

hospitals = open_data.get_pois("MEDICAL")
hospitals.head()

Adds these hospitals to the model

land_use = "MEDICAL"
max_loc = net.conn.execute("Select coalesce(max(location) + 1, 1) from Location").fetchone()[0]
with commit_and_close(net.path_to_file, spatial=True) as conn:

    for _, hosp in hospitals.iterrows():
        loc = Location(max_loc, net.geotools, net.tables, conn)
        loc.land_use = land_use
        loc.geo = hosp.geo

        loc.avg_parking_cost = loc.stop_flag = loc.offset = loc.setback = loc.x = loc.y = 0
        loc.truck_org = loc.truck_des = loc.auto_org = loc.auto_des = 1
        loc.transit = loc.area_type = loc.lu_area = loc.popsyn_region = loc.anchored = loc.tod_distance = 0
        loc.link = net.geotools.get_link_for_point_by_mode(loc.geo, ["AUTO"])
        loc.save(conn)
        max_loc += 1

Schools#

Now let’s add schools to the model

for land_use in ["EDUCATION_PREK", "EDUCATION_K_8", "EDUCATION_9_12", "HIGHER_EDUCATION"]:
    added_schools = []
    schools = open_data.get_pois(land_use)
    with commit_and_close(net.path_to_file, spatial=True) as conn:
        for _, sch in schools.iterrows():
            loc = Location(max_loc, net.geotools, net.tables, conn)
            loc.land_use = land_use
            loc.geo = sch.geo
            zone = net.geotools.get_geo_item("zone", loc.geo)
            added_schools.append(zone)
            loc.avg_parking_cost = loc.stop_flag = loc.offset = loc.setback = loc.x = loc.y = 0
            loc.truck_org = loc.truck_des = loc.auto_org = loc.auto_des = 1
            loc.transit = loc.area_type = loc.lu_area = loc.popsyn_region = loc.anchored = loc.tod_distance = 0
            loc.link = net.geotools.get_link_for_point_by_mode(loc.geo, ["AUTO"])
            loc.save(conn)
            max_loc += 1
    print(f"Added {len(schools)} {land_use} in {len(set(added_schools))} different zones")

Hotels#

hotels = []
land_use = "HOTEL"
hot_data = open_data.get_pois(land_use)

Now let’s add hotels to the model

with commit_and_close(net.path_to_file, spatial=True) as conn:
    for _, hotel in hot_data.iterrows():
        loc = Location(max_loc, net.geotools, net.tables, conn)
        loc.land_use = land_use
        loc.geo = hotel.geo

        loc.avg_parking_cost = loc.stop_flag = loc.offset = loc.setback = loc.x = loc.y = 0
        loc.truck_org = loc.truck_des = loc.auto_org = loc.auto_des = 1
        loc.transit = loc.area_type = loc.lu_area = loc.popsyn_region = loc.anchored = loc.tod_distance = 0
        loc.link = net.geotools.get_link_for_point_by_mode(loc.geo, ["AUTO"])
        loc.save(conn)
        max_loc += 1
print(f"Added {len(hotels)} hotels to the model")

Visualizing the results#

from polaris.utils.database.data_table_access import DataTableAccess
import folium
import geopandas as gpd

locations = DataTableAccess(net.path_to_file).get("location", net.conn)

js = locations[["location", "land_use", "geo"]]
js = gpd.GeoDataFrame(js, geometry="geo", crs=4326)

map = folium.Map(location=[js.geo.y.mean(), js.geo.x.mean()], zoom_start=9)

criteria = [
    ["HOTEL", "HOTEL", "bed", "blue", "Hotels"],
    ["MEDICAL", "MEDICAL", "hospital-o", "green", "Hospitals"],
    ["EDUCATION", "EDUCATION", "bicycle", "green", "Schools"],
    ["Universities", "HIGHER_EDUCATION", "graduation-cap", "blue", "Universities"],
]

for crit, filter, icon, color, name in criteria:
    map = js[js.land_use == filter].explore(
        m=map, color=color, tool_tip=crit, marker_kwds={"icon": icon, "prefix": "fa", "color": color}, name=name
    )

folium.LayerControl().add_to(map)
map

Closes project#

print("REFERENCE: You can keep navigating the map after closing the project")
net.close(False)

Gallery generated by Sphinx-Gallery