Search for a suitable apartment¶
Note: All notebooks need the environment dependencies as well as an openrouteservice API key to run
In this notebook we'll provide an example for using different openrouteservice API's to help you look for an apartment.
import folium
from openrouteservice import client
We want to move to San Francisco with our kids and are looking for the perfect location to get a new home. Our geo intuition tells us we have to look at the data to come to this important decision. So we decide to geek it up a bit.
Apartment isochrones¶
There are three different suggested locations for our new home. Let's visualize them, and the 15-minute walking radius on a map:
api_key = 'your_key' # Provide your personal API key
ors = client.Client(key=api_key)
# Set up folium map
map1 = folium.Map(tiles='Stamen Toner', location=([37.738684, -122.450523]), zoom_start=12)
# Set up the apartment dictionary with real coordinates
apartments = {'first': {'location': [-122.430954, 37.792965]},
'second': {'location': [-122.501636, 37.748653]},
'third': {'location': [-122.446629, 37.736928]}
}
# Request of isochrones with 15 minute on foot.
params_iso = {'profile': 'foot-walking',
'range': [900], # 900/60 = 15 minutes
'attributes': ['total_pop'] # Get population count for isochrones
}
for name, apt in apartments.items():
params_iso['locations'] = [apt['location']] # Add apartment coords to request parameters
apt['iso'] = ors.isochrones(**params_iso) # Perform isochrone request
folium.features.GeoJson(apt['iso']).add_to(map1) # Add GeoJson to map
folium.map.Marker(list(reversed(apt['location'])), # reverse coords due to weird folium lat/lon syntax
icon=folium.Icon(color='lightgray',
icon_color='#cc0000',
icon='home',
prefix='fa',
),
popup=name,
).add_to(map1) # Add apartment locations to map
map1
POIs around apartments¶
For the ever-styled foodie parents we are, we need to have the 3 basic things covered: kindergarten, supermarket and hair dresser. Let's see what options we got around our apartments:
# Common request parameters
params_poi = {'request': 'pois',
'sortby': 'distance'}
# POI categories according to
# https://giscience.github.io/openrouteservice/documentation/Places.html
categories_poi = {'kindergarten': [153],
'supermarket': [518],
'hairdresser': [395]}
for name, apt in apartments.items():
apt['categories'] = dict() # Store in pois dict for easier retrieval
params_poi['geojson'] = apt['iso']['features'][0]['geometry']
print("\n{} apartment".format(name))
for typ, category in categories_poi.items():
params_poi['filter_category_ids'] = category
apt['categories'][typ] = dict()
apt['categories'][typ]['geojson'] = ors.places(**params_poi)['features'] # Actual POI request
print(f"\t{typ}: {len(apt['categories'][typ]['geojson'])}")
first apartment {'type': 'FeatureCollection', 'bbox': [-122.42795, 37.782774, -122.42795, 37.782774], 'features': [{'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.42795, 37.782774]}, 'properties': {'osm_id': 256908810, 'osm_type': 2, 'distance': 0.0, 'category_ids': {'153': {'category_name': 'kindergarten', 'category_group': 'education'}}, 'osm_tags': {'name': 'Golden Gate Kindergarten'}}}], 'information': {'attribution': 'openrouteservice.org | OpenStreetMap contributors', 'version': '0.1', 'timestamp': 1637671278, 'query': {'request': 'pois', 'filters': {'category_ids': [153]}, 'geometry': {'geojson': {'coordinates': [[[-122.445337, 37.792922], [-122.444881, 37.789351], [-122.444081, 37.788552], [-122.44151, 37.787075], [-122.438726, 37.785497], [-122.436451, 37.784382], [-122.433286, 37.782666], [-122.428048, 37.781626], [-122.42782, 37.781654], [-122.426023, 37.782688], [-122.424703, 37.783627], [-122.423604, 37.784552], [-122.422552, 37.785645], [-122.419664, 37.788801], [-122.418643, 37.789984], [-122.417421, 37.79178], [-122.416677, 37.792939], [-122.416171, 37.793954], [-122.416212, 37.796162], [-122.416249, 37.796211], [-122.419151, 37.797964], [-122.423673, 37.800414], [-122.427709, 37.80268], [-122.429929, 37.803611], [-122.433147, 37.803505], [-122.433598, 37.803392], [-122.436389, 37.802618], [-122.438463, 37.800448], [-122.441391, 37.797205], [-122.442385, 37.796121], [-122.445337, 37.792922]]], 'type': 'Polygon'}}, 'sortby': 'distance'}}} kindergarten: 1 {'type': 'FeatureCollection', 'bbox': [-122.43527, 37.782711, -122.418255, 37.797229], 'features': [{'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.431464, 37.782711]}, 'properties': {'osm_id': 219058165, 'osm_type': 2, 'distance': 0.0, 'category_ids': {'518': {'category_name': 'supermarket', 'category_group': 'shops'}}, 'osm_tags': {'name': 'Safeway 0995', 'website': 'https://local.safeway.com/ca/san-francisco-995.html', 'opening_hours': '06:00-24:00'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.434504, 37.78854]}, 'properties': {'osm_id': 263224863, 'osm_type': 2, 'distance': 0.0, 'category_ids': {'518': {'category_name': 'supermarket', 'category_group': 'shops'}}, 'osm_tags': {'phone': '415-567-4902', 'wheelchair': 'yes', 'name': "Mollie Stone's"}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.423414, 37.79009]}, 'properties': {'osm_id': 1002186270, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'518': {'category_name': 'supermarket', 'category_group': 'shops'}}, 'osm_tags': {'phone': '+1 415 674 0500', 'name': 'Whole Foods Market'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.421707, 37.796547]}, 'properties': {'osm_id': 1478520948, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'518': {'category_name': 'supermarket', 'category_group': 'shops'}}, 'osm_tags': {'opening_hours': '8:00-21:00', 'website': 'www.realfoodco.com', 'name': 'Real Food Company'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.431032, 37.785255]}, 'properties': {'osm_id': 2184767720, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'518': {'category_name': 'supermarket', 'category_group': 'shops'}}, 'osm_tags': {'name': 'Nijiya Market', 'wheelchair': 'yes', 'phone': '(415) 563-1901'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.420783, 37.790205]}, 'properties': {'osm_id': 2698387925, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'518': {'category_name': 'supermarket', 'category_group': 'shops'}}, 'osm_tags': {'name': 'Golden Veggie Market', 'phone': '(415) 771-9099', 'wheelchair': 'yes'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.418255, 37.794474]}, 'properties': {'osm_id': 4637210183, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'518': {'category_name': 'supermarket', 'category_group': 'shops'}}, 'osm_tags': {'name': 'J H Grocery and Liquor'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.43527, 37.797229]}, 'properties': {'osm_id': 8140177640, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'518': {'category_name': 'supermarket', 'category_group': 'shops'}}, 'osm_tags': {'name': "Luke's Local", 'opening_hours': 'Mo-Su 08:00-21:00', 'website': 'https://lukeslocal.com/'}}}], 'information': {'attribution': 'openrouteservice.org | OpenStreetMap contributors', 'version': '0.1', 'timestamp': 1637671278, 'query': {'request': 'pois', 'filters': {'category_ids': [518]}, 'geometry': {'geojson': {'coordinates': [[[-122.445337, 37.792922], [-122.444881, 37.789351], [-122.444081, 37.788552], [-122.44151, 37.787075], [-122.438726, 37.785497], [-122.436451, 37.784382], [-122.433286, 37.782666], [-122.428048, 37.781626], [-122.42782, 37.781654], [-122.426023, 37.782688], [-122.424703, 37.783627], [-122.423604, 37.784552], [-122.422552, 37.785645], [-122.419664, 37.788801], [-122.418643, 37.789984], [-122.417421, 37.79178], [-122.416677, 37.792939], [-122.416171, 37.793954], [-122.416212, 37.796162], [-122.416249, 37.796211], [-122.419151, 37.797964], [-122.423673, 37.800414], [-122.427709, 37.80268], [-122.429929, 37.803611], [-122.433147, 37.803505], [-122.433598, 37.803392], [-122.436389, 37.802618], [-122.438463, 37.800448], [-122.441391, 37.797205], [-122.442385, 37.796121], [-122.445337, 37.792922]]], 'type': 'Polygon'}}, 'sortby': 'distance'}}} supermarket: 8 {'type': 'FeatureCollection', 'bbox': [-122.437756, 37.784793, -122.418044, 37.80079], 'features': [{'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.421694, 37.7965]}, 'properties': {'osm_id': 2167562318, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'opening_hours': 'Mo-Fr 10:00-20:00; Sa-Su 10:00-19:00', 'website': 'http://www.backstagesf.com/', 'phone': '+1 415-775-1440', 'name': 'Backstage Salon'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.437756, 37.80079]}, 'properties': {'osm_id': 2340006012, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'name': 'Genray Hair Salon'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.42417, 37.796522]}, 'properties': {'osm_id': 2526073573, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'name': 'Solon V'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.42075, 37.79037]}, 'properties': {'osm_id': 3406206893, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'name': 'MANEFRAME Hairdooz'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.421869, 37.795721]}, 'properties': {'osm_id': 3659215685, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'name': "Harry's Hair Studio"}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.420432, 37.788558]}, 'properties': {'osm_id': 4004801648, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'name': 'Barber Hairdress'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.421225, 37.792566]}, 'properties': {'osm_id': 4024246507, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'name': 'New Beauty Hair Salon'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.418044, 37.791052]}, 'properties': {'osm_id': 4064534751, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'name': 'Hair Future'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.422427, 37.797398]}, 'properties': {'osm_id': 4087004997, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'name': 'Rose Hair Design'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.419776, 37.795089]}, 'properties': {'osm_id': 4637158847, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'name': 'Rose Hair Design'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.429135, 37.78519]}, 'properties': {'osm_id': 6693595725, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'name': 'Glam Up'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.429429, 37.78574]}, 'properties': {'osm_id': 6693646154, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'name': 'Arty', 'website': 'https://www.artyhair.com/'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.420626, 37.795305]}, 'properties': {'osm_id': 8819188923, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'opening_hours': 'Mo-Su 09:00-18:00', 'name': 'SoHair Studio'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.432752, 37.797539]}, 'properties': {'osm_id': 9026905858, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'name': 'The Barbershop', 'website': 'https://www.thebarbershopsf.com'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.431914, 37.784793]}, 'properties': {'osm_id': 9200292426, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'name': 'Nepenji', 'website': 'https://nepenji.wordpress.com/', 'phone': '+1 415 9210135'}}}], 'information': {'attribution': 'openrouteservice.org | OpenStreetMap contributors', 'version': '0.1', 'timestamp': 1637671278, 'query': {'request': 'pois', 'filters': {'category_ids': [395]}, 'geometry': {'geojson': {'coordinates': [[[-122.445337, 37.792922], [-122.444881, 37.789351], [-122.444081, 37.788552], [-122.44151, 37.787075], [-122.438726, 37.785497], [-122.436451, 37.784382], [-122.433286, 37.782666], [-122.428048, 37.781626], [-122.42782, 37.781654], [-122.426023, 37.782688], [-122.424703, 37.783627], [-122.423604, 37.784552], [-122.422552, 37.785645], [-122.419664, 37.788801], [-122.418643, 37.789984], [-122.417421, 37.79178], [-122.416677, 37.792939], [-122.416171, 37.793954], [-122.416212, 37.796162], [-122.416249, 37.796211], [-122.419151, 37.797964], [-122.423673, 37.800414], [-122.427709, 37.80268], [-122.429929, 37.803611], [-122.433147, 37.803505], [-122.433598, 37.803392], [-122.436389, 37.802618], [-122.438463, 37.800448], [-122.441391, 37.797205], [-122.442385, 37.796121], [-122.445337, 37.792922]]], 'type': 'Polygon'}}, 'sortby': 'distance'}}} hairdresser: 15 second apartment {'type': 'FeatureCollection', 'bbox': [-122.504213, 37.738033, -122.496208, 37.742273], 'features': [{'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.500968, 37.738033]}, 'properties': {'osm_id': 3996578781, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'153': {'category_name': 'kindergarten', 'category_group': 'education'}}, 'osm_tags': {'name': 'Starlight 2 Christian Preschool'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.500805, 37.738037]}, 'properties': {'osm_id': 3996578783, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'153': {'category_name': 'kindergarten', 'category_group': 'education'}}, 'osm_tags': {'name': 'The Ark Christian Preschool'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.500221, 37.73805]}, 'properties': {'osm_id': 5061064106, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'153': {'category_name': 'kindergarten', 'category_group': 'education'}}, 'osm_tags': {'name': 'Creative Montessori Preschool'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.496208, 37.742273]}, 'properties': {'osm_id': 6874544914, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'153': {'category_name': 'kindergarten', 'category_group': 'education'}}, 'osm_tags': {'website': 'kaiming.org', 'phone': '+1(415)759-8980', 'name': 'Kai Ming Head Start'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.504213, 37.741618]}, 'properties': {'osm_id': 6874544920, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'153': {'category_name': 'kindergarten', 'category_group': 'education'}}, 'osm_tags': {'name': 'Little People Preschool and T K', 'website': 'https://littlepeoplepreschoolsf.com'}}}], 'information': {'attribution': 'openrouteservice.org | OpenStreetMap contributors', 'version': '0.1', 'timestamp': 1637671281, 'query': {'request': 'pois', 'filters': {'category_ids': [153]}, 'geometry': {'geojson': {'coordinates': [[[-122.509544, 37.754617], [-122.509531, 37.754366], [-122.509519, 37.754115], [-122.509383, 37.752777], [-122.508511, 37.747715], [-122.508351, 37.74648], [-122.508194, 37.745242], [-122.507978, 37.743636], [-122.507883, 37.742901], [-122.507788, 37.742166], [-122.507631, 37.741879], [-122.506896, 37.741034], [-122.502135, 37.737969], [-122.501283, 37.737341], [-122.500924, 37.737366], [-122.4989, 37.739166], [-122.494838, 37.742563], [-122.493832, 37.743515], [-122.491265, 37.745931], [-122.489976, 37.74973], [-122.489972, 37.749775], [-122.489992, 37.75009], [-122.491212, 37.752154], [-122.496136, 37.75543], [-122.496852, 37.755941], [-122.501377, 37.759153], [-122.502504, 37.759911], [-122.502863, 37.759886], [-122.505909, 37.75734], [-122.509544, 37.754617]]], 'type': 'Polygon'}}, 'sortby': 'distance'}}} kindergarten: 5 {'type': 'FeatureCollection', 'bbox': [-122.503584, 37.752838, -122.503584, 37.752838], 'features': [{'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.503584, 37.752838]}, 'properties': {'osm_id': 9147266528, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'518': {'category_name': 'supermarket', 'category_group': 'shops'}}, 'osm_tags': {'website': 'https://gussmarket.com/market-locations/noriega-market-san-francisco/', 'phone': '(415) 564-0370', 'opening_hours': 'Mo-Su 08:00-21:00', 'name': "Gus's Community Market"}}}], 'information': {'attribution': 'openrouteservice.org | OpenStreetMap contributors', 'version': '0.1', 'timestamp': 1637671282, 'query': {'request': 'pois', 'filters': {'category_ids': [518]}, 'geometry': {'geojson': {'coordinates': [[[-122.509544, 37.754617], [-122.509531, 37.754366], [-122.509519, 37.754115], [-122.509383, 37.752777], [-122.508511, 37.747715], [-122.508351, 37.74648], [-122.508194, 37.745242], [-122.507978, 37.743636], [-122.507883, 37.742901], [-122.507788, 37.742166], [-122.507631, 37.741879], [-122.506896, 37.741034], [-122.502135, 37.737969], [-122.501283, 37.737341], [-122.500924, 37.737366], [-122.4989, 37.739166], [-122.494838, 37.742563], [-122.493832, 37.743515], [-122.491265, 37.745931], [-122.489976, 37.74973], [-122.489972, 37.749775], [-122.489992, 37.75009], [-122.491212, 37.752154], [-122.496136, 37.75543], [-122.496852, 37.755941], [-122.501377, 37.759153], [-122.502504, 37.759911], [-122.502863, 37.759886], [-122.505909, 37.75734], [-122.509544, 37.754617]]], 'type': 'Polygon'}}, 'sortby': 'distance'}}} supermarket: 1 {'type': 'FeatureCollection', 'bbox': [-122.504588, 37.7418, -122.496307, 37.753159], 'features': [{'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.498495, 37.742177]}, 'properties': {'osm_id': 718375840, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'name': "Stacy's Hair Studio"}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.504588, 37.753159]}, 'properties': {'osm_id': 4214319698, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'name': 'Perfect Cut'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.501252, 37.7418]}, 'properties': {'osm_id': 5061094084, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'name': "Harry's Haircuts"}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.496307, 37.742268]}, 'properties': {'osm_id': 6874544913, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'name': 'Iky Hair Design'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.503194, 37.741962]}, 'properties': {'osm_id': 6874544921, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'wheelchair': 'yes', 'name': 'Carla & Co Hair Studio'}}}], 'information': {'attribution': 'openrouteservice.org | OpenStreetMap contributors', 'version': '0.1', 'timestamp': 1637671282, 'query': {'request': 'pois', 'filters': {'category_ids': [395]}, 'geometry': {'geojson': {'coordinates': [[[-122.509544, 37.754617], [-122.509531, 37.754366], [-122.509519, 37.754115], [-122.509383, 37.752777], [-122.508511, 37.747715], [-122.508351, 37.74648], [-122.508194, 37.745242], [-122.507978, 37.743636], [-122.507883, 37.742901], [-122.507788, 37.742166], [-122.507631, 37.741879], [-122.506896, 37.741034], [-122.502135, 37.737969], [-122.501283, 37.737341], [-122.500924, 37.737366], [-122.4989, 37.739166], [-122.494838, 37.742563], [-122.493832, 37.743515], [-122.491265, 37.745931], [-122.489976, 37.74973], [-122.489972, 37.749775], [-122.489992, 37.75009], [-122.491212, 37.752154], [-122.496136, 37.75543], [-122.496852, 37.755941], [-122.501377, 37.759153], [-122.502504, 37.759911], [-122.502863, 37.759886], [-122.505909, 37.75734], [-122.509544, 37.754617]]], 'type': 'Polygon'}}, 'sortby': 'distance'}}} hairdresser: 5 third apartment {'type': 'FeatureCollection', 'bbox': [-122.451453, 37.731372, -122.451453, 37.731372], 'features': [{'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.451453, 37.731372]}, 'properties': {'osm_id': 5002148263, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'153': {'category_name': 'kindergarten', 'category_group': 'education'}}, 'osm_tags': {'name': 'Wind in the Willows'}}}], 'information': {'attribution': 'openrouteservice.org | OpenStreetMap contributors', 'version': '0.1', 'timestamp': 1637671284, 'query': {'request': 'pois', 'filters': {'category_ids': [153]}, 'geometry': {'geojson': {'coordinates': [[[-122.458523, 37.737212], [-122.458511, 37.736852], [-122.458268, 37.735613], [-122.458199, 37.735259], [-122.454587, 37.732849], [-122.451851, 37.731404], [-122.449047, 37.729569], [-122.444485, 37.729493], [-122.44203, 37.729363], [-122.441918, 37.729363], [-122.439568, 37.731045], [-122.439166, 37.731361], [-122.439053, 37.731452], [-122.438121, 37.732197], [-122.437719, 37.733076], [-122.437374, 37.733959], [-122.437355, 37.734024], [-122.437286, 37.734377], [-122.437359, 37.734712], [-122.438233, 37.735954], [-122.439536, 37.737158], [-122.439895, 37.737177], [-122.443613, 37.738171], [-122.445523, 37.741], [-122.445988, 37.741942], [-122.447797, 37.742419], [-122.451915, 37.745398], [-122.452069, 37.745502], [-122.452245, 37.745425], [-122.453226, 37.744938], [-122.453427, 37.744813], [-122.454145, 37.743993], [-122.454873, 37.741953], [-122.457422, 37.738635], [-122.458523, 37.737212]]], 'type': 'Polygon'}}, 'sortby': 'distance'}}} kindergarten: 1 {'type': 'FeatureCollection', 'bbox': [-122.453698, 37.731171, -122.44994, 37.744856], 'features': [{'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.44994, 37.731171]}, 'properties': {'osm_id': 219059390, 'osm_type': 2, 'distance': 0.0, 'category_ids': {'518': {'category_name': 'supermarket', 'category_group': 'shops'}}, 'osm_tags': {'opening_hours': '06:00-24:00', 'website': 'https://local.safeway.com/ca/san-francisco-759.html', 'name': 'Safeway 0759'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.453698, 37.7438]}, 'properties': {'osm_id': 256912288, 'osm_type': 2, 'distance': 0.0, 'category_ids': {'518': {'category_name': 'supermarket', 'category_group': 'shops'}}, 'osm_tags': {'name': 'Miraloma Market'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.452262, 37.744856]}, 'properties': {'osm_id': 276546465, 'osm_type': 2, 'distance': 0.0, 'category_ids': {'518': {'category_name': 'supermarket', 'category_group': 'shops'}}, 'osm_tags': {'name': "Mollie Stone's", 'opening_hours': '07:00-21:00', 'wheelchair': 'yes'}}}], 'information': {'attribution': 'openrouteservice.org | OpenStreetMap contributors', 'version': '0.1', 'timestamp': 1637671284, 'query': {'request': 'pois', 'filters': {'category_ids': [518]}, 'geometry': {'geojson': {'coordinates': [[[-122.458523, 37.737212], [-122.458511, 37.736852], [-122.458268, 37.735613], [-122.458199, 37.735259], [-122.454587, 37.732849], [-122.451851, 37.731404], [-122.449047, 37.729569], [-122.444485, 37.729493], [-122.44203, 37.729363], [-122.441918, 37.729363], [-122.439568, 37.731045], [-122.439166, 37.731361], [-122.439053, 37.731452], [-122.438121, 37.732197], [-122.437719, 37.733076], [-122.437374, 37.733959], [-122.437355, 37.734024], [-122.437286, 37.734377], [-122.437359, 37.734712], [-122.438233, 37.735954], [-122.439536, 37.737158], [-122.439895, 37.737177], [-122.443613, 37.738171], [-122.445523, 37.741], [-122.445988, 37.741942], [-122.447797, 37.742419], [-122.451915, 37.745398], [-122.452069, 37.745502], [-122.452245, 37.745425], [-122.453226, 37.744938], [-122.453427, 37.744813], [-122.454145, 37.743993], [-122.454873, 37.741953], [-122.457422, 37.738635], [-122.458523, 37.737212]]], 'type': 'Polygon'}}, 'sortby': 'distance'}}} supermarket: 3 {'type': 'FeatureCollection', 'bbox': [-122.452916, 37.731369, -122.451621, 37.744309], 'features': [{'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.452916, 37.744309]}, 'properties': {'osm_id': 3545345794, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'name': 'Hocus Pocus'}}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-122.451621, 37.731369]}, 'properties': {'osm_id': 5002148262, 'osm_type': 1, 'distance': 0.0, 'category_ids': {'395': {'category_name': 'hairdresser', 'category_group': 'service'}}, 'osm_tags': {'name': 'Amazing Look'}}}], 'information': {'attribution': 'openrouteservice.org | OpenStreetMap contributors', 'version': '0.1', 'timestamp': 1637671284, 'query': {'request': 'pois', 'filters': {'category_ids': [395]}, 'geometry': {'geojson': {'coordinates': [[[-122.458523, 37.737212], [-122.458511, 37.736852], [-122.458268, 37.735613], [-122.458199, 37.735259], [-122.454587, 37.732849], [-122.451851, 37.731404], [-122.449047, 37.729569], [-122.444485, 37.729493], [-122.44203, 37.729363], [-122.441918, 37.729363], [-122.439568, 37.731045], [-122.439166, 37.731361], [-122.439053, 37.731452], [-122.438121, 37.732197], [-122.437719, 37.733076], [-122.437374, 37.733959], [-122.437355, 37.734024], [-122.437286, 37.734377], [-122.437359, 37.734712], [-122.438233, 37.735954], [-122.439536, 37.737158], [-122.439895, 37.737177], [-122.443613, 37.738171], [-122.445523, 37.741], [-122.445988, 37.741942], [-122.447797, 37.742419], [-122.451915, 37.745398], [-122.452069, 37.745502], [-122.452245, 37.745425], [-122.453226, 37.744938], [-122.453427, 37.744813], [-122.454145, 37.743993], [-122.454873, 37.741953], [-122.457422, 37.738635], [-122.458523, 37.737212]]], 'type': 'Polygon'}}, 'sortby': 'distance'}}} hairdresser: 2
We already see that the second apartment is missing a supermarket in a 15-minute walking range and continue without it.
# Remove second apartment
del apartments['second']
Routing from apartments to POIs¶
To decide on a place, we would like to know from which apartment we can reach all required POI categories the quickest. So, first we look at the distances from each apartment to the respective POIs.
# Set up common request parameters
params_route = {'profile': 'foot-walking',
'format_out': 'geojson',
'geometry': 'true',
'format': 'geojson',
'instructions': 'false',
}
# Set up dict for font-awesome
style_dict = {'kindergarten': 'child',
'supermarket': 'shopping-cart',
'hairdresser': 'scissors'
}
# Store all routes from all apartments to POIs
for apt in apartments.values():
for cat, pois in apt['categories'].items():
pois['durations'] = []
for poi in pois['geojson']:
poi_coords = poi['geometry']['coordinates']
# Perform actual request
params_route['coordinates'] = [apt['location'],
poi_coords
]
json_route = ors.directions(**params_route)
folium.features.GeoJson(json_route).add_to(map1)
folium.map.Marker(list(reversed(poi_coords)),
icon=folium.Icon(color='white',
icon_color='#1a1aff',
icon=style_dict[cat],
prefix='fa'
)
).add_to(map1)
poi_duration = json_route['features'][0]['properties']['summary']['duration']
pois['durations'].append(poi_duration) # Record durations of routes
map1
The Quickest route to all POIs¶
Now, we only need to determine which apartment is closest to all POI categories.
# Sum up the closest POIs to each apartment
for name, apt in apartments.items():
apt['shortest_sum'] = sum([min(cat['durations']) for cat in apt['categories'].values()])
print(f"{name} apartment: {round(apt['shortest_sum'] / 60, 1)} min"
)
first apartment: 36.0 mins third apartment: 45.9 mins
We got a winner!¶
Finally, it looks like the first apartment is the one where we would need to walk the shortest amount of time to reach a kindergarten, supermarket and a hair dresser. Let's pack those boxes and welcome to San Francisco.