def test_pois(): import pytest # download all points of interests from place gdf = ox.pois_from_place(place='Kamppi, Helsinki, Finland') # get all restaurants and schools from place restaurants = ox.pois_from_place(place='Emeryville, California, USA', amenities=['restaurant']) schools = ox.pois_from_place(place='Emeryville, California, USA', amenities=['school']) # get all universities from Boston area (with 2 km buffer to cover also Cambridge) boston_q = "Boston, Massachusetts, United States of America" boston_poly = ox.gdf_from_place(boston_q, buffer_dist=2000) universities = ox.pois_from_polygon(boston_poly.geometry.values[0], amenities=['university']) # by point and by address restaurants = ox.pois_from_point(point=(42.344490, -71.070570), distance=1000, amenities=['restaurant']) restaurants = ox.pois_from_address(address='Emeryville, California, USA', distance=1000, amenities=['restaurant']) # should raise an exception # polygon or -north, south, east, west- should be provided with pytest.raises(ValueError): ox.create_poi_gdf(polygon=None, north=None, south=None, east=None, west=None) gdf = ox.pois_from_place(place='kusatsu, shiga, japan', which_result=2)
def test_pois(): # download all points of interests from place gdf = ox.pois_from_place(place='Kamppi, Helsinki, Finland') # Get all restaurants and schools from place restaurants = ox.pois_from_place(place='Emeryville, California, USA', amenities=['restaurant']) schools = ox.pois_from_place(place='Emeryville, California, USA', amenities=['school']) # Get all Universities from Boston area (with 2 km buffer to cover also Cambridge) boston_q = "Boston, Massachusetts, United States of America" boston_poly = ox.gdf_from_place(boston_q, buffer_dist=2000) universities = ox.pois_from_polygon(boston_poly.geometry.values[0], amenities=['university'])
def test_pois(): # download all points of interests from place gdf = ox.pois_from_place(place='Kamppi, Helsinki, Finland') # get all restaurants and schools from place restaurants = ox.pois_from_place(place='Emeryville, California, USA', amenities=['restaurant']) schools = ox.pois_from_place(place='Emeryville, California, USA', amenities=['school']) # get all universities from Boston area (with 2 km buffer to cover also Cambridge) boston_q = "Boston, Massachusetts, United States of America" boston_poly = ox.gdf_from_place(boston_q, buffer_dist=2000) universities = ox.pois_from_polygon(boston_poly.geometry.values[0], amenities=['university']) # by point and by address restaurants = ox.pois_from_point(point=(42.344490, -71.070570), distance=1000, amenities=['restaurant']) restaurants = ox.pois_from_address(address='Emeryville, California, USA', distance=1000, amenities=['restaurant'])
def test_pois(): tags = { "amenity": True, "landuse": ["retail", "commercial"], "highway": "bus_stop" } gdf = ox.pois_from_place([place1], tags=tags) gdf = ox.pois_from_address(address, tags={"amenity": "school"})
def test_pois(): tags = {"amenity": True, "landuse": ["retail", "commercial"], "highway": "bus_stop"} gdf = ox.pois_from_place(place1, tags=tags) gdf = ox.pois_from_polygon(polygon, tags={"amenity": "library"}) cs = '[out:json][timeout:180][date:"2019-10-28T19:20:00Z"]' gdf = ox.pois_from_address(address, tags={"amenity": "school"}, custom_settings=cs) gdf = ox.pois_from_point( location_point, dist=500, tags={"amenity": "restaurant"}, timeout=200, memory=100000 )
def find_amenities(place_name): area = ox.gdf_from_place(place_name) graph = ox.graph_from_place(place_name) nodes, edges = ox.graph_to_gdfs(graph) buildings = ox.footprints_from_place(place_name) amenities = ox.pois_from_place(place_name, amenities=sustenance_amenities) ax = area.plot(facecolor='white') ax = edges.plot(ax=ax, linewidth=1, edgecolor='#BC8F8F', alpha=0.2) ax = buildings.plot(ax=ax, facecolor='blue', alpha=1) ax = amenities.plot(ax=ax, color='green', alpha=1, markersize=10) ax.figure.savefig('amenities.png') sustenance_amenities_variances = dict(amenities.amenity.value_counts()) wrong_dict_keys = [] for key in sustenance_amenities_variances.keys(): if key not in sustenance_amenities: wrong_dict_keys.append(key) for key in wrong_dict_keys: del sustenance_amenities_variances[key] return sustenance_amenities_variances
def test_pois(): tags = { 'amenity': True, 'landuse': ['retail', 'commercial'], 'highway': 'bus_stop' } gdf = ox.pois_from_place(place='Piedmont, California, USA', tags=tags) poly = ox.gdf_from_place('Boston, MA, USA', buffer_dist=2000) gdf = ox.pois_from_polygon(poly['geometry'].iloc[0], tags={'amenity': 'university'}) gdf = ox.pois_from_address( address='Piedmont, California, USA', tags={'amenity': 'school'}, custom_settings='[out:json][timeout:180][date:"2019-10-28T19:20:00Z"]') gdf = ox.pois_from_point(point=(42.344490, -71.070570), distance=500, tags={'amenity': 'restaurant'}, timeout=200, memory=100000)
# Retrieve the data from OSM graph = ox.graph_from_address(place_name) # fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(12,8)) fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(12,8)) fig, ax = ox.plot_graph(graph) # Convert the graph to GeoDataFrames nodes, edges = ox.graph_to_gdfs(graph) # retrieve buildings from OSM buildings = ox.buildings_from_address(place_name, distance=1000) buildings.plot() # footprint of Kamppi footprint = ox.gdf_from_place(place_name) footprint.plot() # Retrieve Points of Interest from OMS restaurants = ox.pois_from_place(place_name, amenities=['restaurant','bar']) restaurants.plot() # Plot all layers together ax = footprint.plot(facecolor='black') ax= edges.plot(ax=ax, linewidth=1, edgecolor='#BC8F8F') ax = buildings.plot(ax=ax, facecolor='khaki', alpha=0.7) ax = restaurants.plot(ax=ax, color='green', alpha=0.7, markersize=1)
# Retrieve the data from OSM graph = ox.graph_from_address(place_name) type(graph) #Plot the streets fig, ax = ox.plot_graph(graph) # Convert the graph to GeoDatFrames nodes, edges = ox.graph_to_gdfs(graph) #Retrive buildings from OSM buildings = ox.buildings_from_address(place_name, distance=1000) type(buildings) buildings.plot() #Footprint of Kamppi footprint = ox.gdf_from_place(place_name) footprint.plot() # Retrieve Points of Interest from OSM restaurants = ox.pois_from_place(place_name, amenities=["restaurant", "bar"]) type(restaurants) restaurants.plot() # Plot all layers together, first one is the lowest ax = footprint.plot(facecolor="black") ax = edges.plot(ax=ax, linewidth=1, edgecolor = "#BC8F8F") ax = buildings.plot(ax=ax, facecolor="khaki", alpha=0.7) ax = restaurants.plot(ax=ax, color = "green", alpha=0.7, markersize=10)
from typing import List, Set, Dict, Tuple import pandas as pd import geopandas as gpd import pyproj from shapely.wkt import loads, dumps from shapely.geometry import mapping, Point, LineString, MultiPolygon, MultiLineString, MultiPoint from shapely.ops import split, snap, transform from functools import partial from fiona.crs import from_epsg place_name = "Sector 28, Baba Nagar, Faridabád,Haryana,India" graph = ox.graph_from_place(place_name) graph_proj = ox.project_graph(graph) nodes_proj, edges_proj = ox.graph_to_gdfs(graph_proj, nodes=True, edges=True) pois = ox.pois_from_place(place_name) pois_proj = ox.project_gdf(pois) #project pois # randomly pick poi point = pois_proj.iloc[0].geometry def get_closest_point_on_line(line: LineString, point: Point) -> Point: #Finds the closest point on a line to given point and returns it as Point. projected = line.project(point) closest_point = line.interpolate(projected) return closest_point def get_split_lines(line: LineString, point: Point) -> List[LineString]:
def load_objects(a): global healthcare, fire, shops if (a==0): try: healthcare = pd.read_pickle('healthcare.pkl') print('loaded saved healthcare objects from file') except: healthcare = None try: fire = pd.read_pickle('fire.pkl') print('loaded saved fire station objects from file') except: fire = None try: shops = pd.read_pickle('shops.pkl') print('loaded saved shops objects from file') except: shops = None elif (a==1): try: #load from xml tree = ET.parse('network.osm') root = tree.getroot() healthcare_set = [] for i in healthcare_list: healthcare_set.extend(root.findall(".//way/tag/[@k='amenity'][@v='"+i+"']/..")) healthcare_set.extend(root.findall(".//node/tag/[@k='amenity'][@v='"+i+"']/..")) healthcare_n = [] for i in healthcare_set: try: healthcare_n.append(i.find("./nd").get('ref')) except: pass try: healthcare_n.append(i.get('id')) except: pass fire_set = [] for i in fire_list: fire_set.extend(root.findall(".//way/tag/[@k='amenity'][@v='"+i+"']/..")) fire_set.extend(root.findall(".//node/tag/[@k='amenity'][@v='"+i+"']/..")) fire_n = [] for i in fire_set: try: fire_n.append(i.find("./nd").get('ref')) except: pass try: fire_n.append(i.get('id')) except: pass shops_set = [] for i in shop_list: shops_set.extend(root.findall(".//way/tag/[@k='shop'][@v='"+i+"']/..")) shops_set.extend(root.findall(".//node/tag/[@k='shop'][@v='"+i+"']/..")) shops_n = [] for i in fire_set: try: shops_n.append(i.find("./nd").get('ref')) except: pass try: shops_n.append(i.get('id')) except: pass X = [] Y = [] for i in healthcare_n: ans = root.find(".//node[@id='"+i+"']") try: Y.append(ans.get('lat')) X.append(ans.get('lon')) except: pass healthcare = pd.DataFrame({'osmid':healthcare_n,'Latitude':Y,'Longitude':X}) X = [] Y = [] for i in fire_n: ans = root.find(".//node[@id='"+i+"']") try: Y.append(ans.get('lat')) X.append(ans.get('lon')) except: pass fire = pd.DataFrame({'osmid':fire_n,'Latitude':Y,'Longitude':X}) X = [] Y = [] for i in shops_n: ans = root.find(".//node[@id='"+i+"']") try: Y.append(ans.get('lat')) X.append(ans.get('lon')) except: pass shops = pd.DataFrame({'osmid':shops_n,'Latitude':Y,'Longitude':X}) ## a = root.findall(".//way/tag/[@k='amenity']/..") ## b = root.findall(".//way/tag/[@k='amenity']/..") ## c = root.findall(".//way/tag/[@k='shop']/..") ## a_tree = ET.ElementTree(a) ## for ae in a: ## ae.find("./tag/[@v='hospital']/..") print('loaded objects from osm file') except: healthcare = fire = shops = None if (healthcare is None): healthcare = ox.pois_from_place('Ижевск, Россия', {'amenity':healthcare_list}, which_result=1) print('downloaded healthcare objects from internet') if (fire is None): fire = ox.pois_from_place('Ижевск, Россия', {'amenity':fire_list}, which_result=1) print('downloaded fire station objects from internet') if (shops is None): shops = ox.pois_from_place('Ижевск, Россия', {'shop':shop_list}, which_result=1) print('downloaded shops objects from internet') return
nodes_proj = gpd.read_file(nodes_saved) ###get boundary of study area boundary = ox.gdf_from_place(place_name, which_result=3) boundary_proj = boundary.to_crs(edges_proj.crs) ox.save_gdf_shapefile(boundary_proj, filename='boundary') boundary_saved = "data/boundary/boundary.shp" boundary_proj = gpd.read_file(boundary_saved) ###points-of-interest of study area ### hospitals hospitals = ox.pois_from_place(place_name, which_result=3, amenities=['hospital'])[[ 'osmid', 'geometry', 'amenity', 'name', 'element_type' ]] hospitals = hospitals[hospitals.element_type == 'way'] hospitals_proj = hospitals.to_crs(edges_proj.crs) ox.save_gdf_shapefile(hospitals_proj, filename='hospitals') hospitals_saved = "data/hospitals/hospitals.shp" hospitals_proj = gpd.read_file(hospitals_saved) hospitals_proj['centroid'] = hospitals_proj.centroid ###schools schools = ox.pois_from_place(place_name, which_result=3, amenities=[ 'school' ])[['osmid', 'geometry', 'amenity', 'name', 'element_type']]