Zone Mapping

Zone Mapping#

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

print("REFERENCE: The user experience of this notebook will look like this")
REFERENCE: The user experience of this notebook will look like this

Imports#

from pathlib import Path
import ipywidgets as widgets
from IPython.display import display
from ipywidgets import GridspecLayout
from ipywidgets import Layout
from polaris.analyze.mapping.zone_maps import ZoneMap
from polaris.runs.scenario_utils import get_last_iteration

Open the project and get the OSM class sphinx_gallery_thumbnail_path = ‘../../examples/result_analysis/zone_map_example_plot_zone_mapping.gif’

last_iter = get_last_iteration(Path("/tmp/Bloomington"))
netfile = last_iter / "Bloomington-Supply.sqlite"
demand_file = last_iter / "Bloomington-Demand.sqlite"
# netfile = r"D:\Argonne\converged_models\Bloomington\Bloomington_iteration_4/Bloomington-Supply.sqlite"
# demand_file = r"D:\Argonne\converged_models\Bloomington\Bloomington_iteration_4/Bloomington-Demand.sqlite"
zm = ZoneMap(netfile, demand_file)

Map config#

# Now we need configure the user interface, which allows the user interact with all the
# Jupyter GUI elements that will control the map
def set_slider(q):
    with output:
        if time_period.value == "CUSTOM":
            return
        interval.value = list(zm.am.period_definition[time_period.value])


def choose_mode(q):
    with output:
        mode_share.disabled = modes.value == "ALL"
        if mode_share.disabled:
            mode_share.value = False
        zm.am.set_mode(modes.value, mode_share.value)


def format_map(q):
    with output:
        zm.set_height_variable(height.value)
        zm.set_color_variable(color.value)
        zm.set_tool_tip(list(set(list(tooltip.value) + [height.value, color.value])))
        zm.set_trip_mode(modes.value, mode_share.value)
        time_period.value = dict((new_val, new_k) for new_k, new_val in zm.am.period_definition.items()).get(
            tuple(interval.value), "CUSTOM"
        )


height = widgets.Dropdown(
    options=zm.stats_variables, value="trips", description="Map height", layout=Layout(height="auto", width="auto")
)
color = widgets.Dropdown(
    options=zm.stats_variables, value="trips", description="Map color", layout=Layout(height="auto", width="auto")
)
modes = widgets.Dropdown(
    options=zm.am.modes, value="ALL", description="Trip Mode", layout=Layout(height="auto", width="auto")
)
mode_share = widgets.Checkbox(value=False, description="Mode share", disabled=True)

trip_types = widgets.Dropdown(
    options=zm.am.types, value="ALL", description="Trip Purpose", layout=Layout(height="auto", width="auto")
)
interval = widgets.IntRangeSlider(
    value=[0, 24], min=0, max=24, step=1, description="Day interval", layout=Layout(height="auto", width="auto")
)
time_period = widgets.Dropdown(
    options=list(zm.am.period_definition.keys()) + ["CUSTOM"],
    value="DAY",
    description="Time period",
    layout=Layout(height="auto", width="auto"),
)

tooltip = widgets.SelectMultiple(
    options=zm.stats_variables,
    value=["pop_persons", "employment_total", "trips"],
    rows=8,
    description="ToolTip",
    layout=Layout(height="auto", width="auto"),
)

modes.observe(choose_mode)

time_period.observe(set_slider)
for qwidget in [height, color, trip_types, interval, tooltip, mode_share]:
    qwidget.observe(format_map)

output = widgets.Output()
grid = GridspecLayout(6, 3, height="auto", width="900px")

grid[0, :2] = height
grid[1, :2] = color
grid[2, 0] = modes
grid[2, 1] = mode_share

for i, wdgt in enumerate([trip_types, time_period, interval]):
    grid[3 + i, :2] = wdgt

grid[:6, 2:] = tooltip

display(grid, output)
GridspecLayout(children=(Dropdown(description='Map height', index=31, layout=Layout(grid_area='widget001', height='auto', width='auto'), options=('zone', 'x', 'y', 'z', 'area_type', 'area', 'entertainment_area', 'industrial_area', 'institutional_area', 'mixed_use_area', 'office_area', 'other_area', 'residential_area', 'retail_area', 'school_area', 'pop_households', 'pop_persons', 'pop_group_quarters', 'employment_total', 'employment_retail', 'employment_government', 'employment_manufacturing', 'employment_services', 'employment_industrial', 'employment_other', 'percent_white', 'percent_black', 'hh_inc_avg', 'electric_grid_transmission', 'electricity_provider', 'geo', 'trips'), value='trips'), Dropdown(description='Map color', index=31, layout=Layout(grid_area='widget002', height='auto', width='auto'), options=('zone', 'x', 'y', 'z', 'area_type', 'area', 'entertainment_area', 'industrial_area', 'institutional_area', 'mixed_use_area', 'office_area', 'other_area', 'residential_area', 'retail_area', 'school_area', 'pop_households', 'pop_persons', 'pop_group_quarters', 'employment_total', 'employment_retail', 'employment_government', 'employment_manufacturing', 'employment_services', 'employment_industrial', 'employment_other', 'percent_white', 'percent_black', 'hh_inc_avg', 'electric_grid_transmission', 'electricity_provider', 'geo', 'trips'), value='trips'), Dropdown(description='Trip Mode', layout=Layout(grid_area='widget003', height='auto', width='auto'), options=('ALL', 'SCHOOLBUS', 'BIKE', 'SOV', 'BUS', 'WALK', 'HOV', 'TAXI'), value='ALL'), Checkbox(value=False, description='Mode share', disabled=True, layout=Layout(grid_area='widget004')), Dropdown(description='Trip Purpose', layout=Layout(grid_area='widget005', height='auto', width='auto'), options=('ALL', 'HOME', 'SCHOOL', 'ERRANDS', 'PICKUP-DROPOFF', 'WORK', 'LEISURE', 'EAT OUT', 'SHOP-MAJOR', 'SOCIAL', 'SHOP-OTHER', 'PERSONAL', 'HEALTHCARE', 'SERVICE', 'RELIGIOUS-CIVIC', 'PART_WORK', 'WORK AT HOME'), value='ALL'), Dropdown(description='Time period', layout=Layout(grid_area='widget006', height='auto', width='auto'), options=('DAY', 'NIGHT', 'AMPEAK', 'AMOFFPEAK', 'PMOFFPEAK', 'PMPEAK', 'EVENING', 'CUSTOM'), value='DAY'), IntRangeSlider(value=(0, 24), description='Day interval', layout=Layout(grid_area='widget007', height='auto', width='auto'), max=24), SelectMultiple(description='ToolTip', index=(16, 18, 31), layout=Layout(grid_area='widget008', height='auto', width='auto'), options=('zone', 'x', 'y', 'z', 'area_type', 'area', 'entertainment_area', 'industrial_area', 'institutional_area', 'mixed_use_area', 'office_area', 'other_area', 'residential_area', 'retail_area', 'school_area', 'pop_households', 'pop_persons', 'pop_group_quarters', 'employment_total', 'employment_retail', 'employment_government', 'employment_manufacturing', 'employment_services', 'employment_industrial', 'employment_other', 'percent_white', 'percent_black', 'hh_inc_avg', 'electric_grid_transmission', 'electricity_provider', 'geo', 'trips'), rows=8, value=('pop_persons', 'employment_total', 'trips'))), layout=Layout(grid_template_areas='"widget001 widget001 widget008"\n"widget002 widget002 widget008"\n"widget003 widget004 widget008"\n"widget005 widget005 widget008"\n"widget006 widget006 widget008"\n"widget007 widget007 widget008"', grid_template_columns='repeat(3, 1fr)', grid_template_rows='repeat(6, 1fr)', height='auto', width='900px')) Output()

Map#

# * After changing the setup above, re-run the cell below to update the map
# * The map can be configured through the GUI panel, accessible at the upper-left corner of the map
m = zm.build_map()
m
User Guide: https://docs.kepler.gl/docs/keplergl-jupyter

KeplerGl(config={'version': 'v1', 'config': {'visState': {'filters': [], 'layers': [{'id': 'ny462um', 'type': 'geojson', 'config': {'dataId': 'zones', 'label': 'zones', 'color': [237, 233, 250], 'highlightColor': [252, 242, 26, 255], 'columns': {'geojson': 'geo'}, 'isVisible': True, 'visConfig': {'opacity': 0.8, 'strokeOpacity': 0.01, 'thickness': 0.5, 'strokeColor': [37, 67, 37], 'colorRange': {'name': 'ColorBrewer YlGn-6', 'type': 'sequential', 'category': 'ColorBrewer', 'colors': ['#ffffcc', '#d9f0a3', '#addd8e', '#78c679', '#31a354', '#006837'], 'reversed': False}, 'strokeColorRange': {'name': 'Global Warming', 'type': 'sequential', 'category': 'Uber', 'colors': ['#5A1846', '#900C3F', '#C70039', '#E3611C', '#F1920E', '#FFC300']}, 'radius': 10, 'sizeRange': [0, 10], 'radiusRange': [0, 50], 'heightRange': [0, 500], 'elevationScale': 17, 'enableElevationZoomFactor': True, 'stroked': True, 'filled': True, 'enable3d': True, 'wireframe': False}, 'hidden': False, 'textLabel': [{'field': None, 'color': [255, 255, 255], 'size': 18, 'offset': [0, 0], 'anchor': 'start', 'alignment': 'center'}]}, 'visualChannels': {'colorField': {'name': 'pop_persons', 'type': 'integer'}, 'colorScale': 'quantile', 'strokeColorField': None, 'strokeColorScale': 'quantile', 'sizeField': None, 'sizeScale': 'linear', 'heightField': {'name': 'total_trips', 'type': 'integer'}, 'heightScale': 'linear', 'radiusField': None, 'radiusScale': 'linear'}}], 'interactionConfig': {'tooltip': {'fieldsToShow': {'zones': []}, 'compareMode': False, 'compareType': 'absolute', 'enabled': True}, 'brush': {'size': 0.5, 'enabled': False}, 'geocoder': {'enabled': False}, 'coordinate': {'enabled': False}}, 'layerBlending': 'normal', 'splitMaps': [], 'animationConfig': {'currentTime': None, 'speed': 1}}, 'mapState': {'bearing': 24, 'dragRotate': True, 'latitude': 40.49490153143537, 'longitude': -88.98501677675743, 'pitch': 50, 'zoom': 9.096117833833905, 'isSplit': False}, 'mapStyle': {'styleType': 'muted', 'topLayerGroups': {}, 'visibleLayerGroups': {'label': True, 'road': True, 'border': False, 'building': True, 'water': True, 'land': True, '3d building': False}, 'threeDBuildingColor': [9.665468314072013, 17.18305478057247, 31.1442867897876], 'mapStyles': {}}}}, data={'zones':      zone  ...  trips
0      11  ...  807.0
1      22  ...  846.0
2      25  ...  854.0
3      27  ...  157.0
4      28  ...  123.0
..    ...  ...    ...
180   287  ...  371.0
181   292  ...  830.0
182   293  ...  985.0
183   296  ...  560.0
184   297  ...  356.0

[185 rows x 32 columns]}, height=900)

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

Gallery generated by Sphinx-Gallery