def getNearestBusStop(bus_node, bus_node_coord): bus_node_id = BusG.nodes[bus_node]['osmid'] query_str = '[out:json][timeout:180];node(' + str( bus_node_id) + ');node(around:100)[highway=bus_stop];out;' response_json = ox.overpass_request(data={'data': query_str}, timeout=180) nearbyG = create_graph(response_json, name='unnamed') nearbyG.remove_node(bus_node_id) try: nearest_node = ox.get_nearest_node(nearbyG, bus_node_coord) except: print("Error in getting nearest bustop") return -1 return nearest_node
def get_point_locations(poly, query): #returns a dictionary overpass_url = "http://overpass-api.de/api/interpreter" #check this is good before production poly_str = ox.get_polygons_coordinates(poly)[0] services = [] print('Querying OSM for locations...') data = ox.overpass_request(data={'data': query.format(poly=poly_str)}, timeout=900) for element in data['elements']: if element['type'] == 'node': services.append((element['lat'], element['lon'])) elif 'center' in element: services.append( (element['center']['lat'], element['center']['lon'])) return services
train_query_str = '[out:json][timeout:180];(relation["network"="Singapore Rail"]["route"="monorail"]' \ '(1.3920,103.8955,1.4191,103.9218); >;);out;' train_response_json = overpass_request(data={'data': train_query_str}, timeout=180) train_graph = create_graph(train_response_json, name='unnamed') G_train = ox.truncate_graph_polygon(train_graph, polygon, truncate_by_edge=True, retain_all=True) gdf_nodes, gdf_edges = ox.graph_to_gdfs(G_train) gdf_nodes.to_csv('data/train_nodes.csv') gdf_edges.to_csv('data/train_edges.csv') busstop_query_str = '[out:json];(node["highway"="bus_stop"](1.3891,103.8872,1.4222,103.9261);>;);out;' bus_response_json = ox.overpass_request(data={'data': busstop_query_str}, timeout=180) G_bus = create_graph(bus_response_json) G_bus = ox.truncate_graph_polygon(G_bus, polygon, truncate_by_edge=True, retain_all=True) # ======================================= GET TUPLE OF DF DATA ======================================= # # Get Walk edge walk_df = pd.read_csv('data/walk_edges.csv') col = walk_df[['u', 'v', 'length', 'oneway']] walk_edges = [tuple(x) for x in col.values] walk_df = pd.read_csv('data/walk_nodes.csv') col = walk_df[['y', 'x', 'osmid']] walk_nodes = [tuple(x) for x in col.values]
unique_osmid_list = list(range(1, 999999)) random.shuffle(unique_osmid_list) start_coord = (1.406246, 103.906023) end_coord = (1.396753, 103.910344) pm = fo.Map(location=centreCoordinate, zoom_start=15, control_scale=True) fo.Marker(start_coord, popup="start", icon=fo.Icon(color='red', icon='info-sign')).add_to(pm) fo.Marker(end_coord, popup="end", icon=fo.Icon(color='red', icon='info-sign')).add_to(pm) busstop_Query = '[out:json];(node["highway"="bus_stop"](1.3891,103.8872,1.4222,103.9261);>;);out;' responsejson_Busstop = ox.overpass_request(data={'data': busstop_Query}, timeout=180) busstop_Graph = create_graph(responsejson_Busstop) busstop_Graph = ox.truncate_graph_polygon(busstop_Graph, polygon, truncate_by_edge=True, retain_all=True) osm_node, rubbish = ox.graph_to_gdfs(busstop_Graph) busstop_start_osm = ox.geo_utils.get_nearest_node(busstop_Graph, start_coord) busstop_end_osm = ox.geo_utils.get_nearest_node(busstop_Graph, end_coord) osm_busstop_coord = get_busstop_coord_osm(osm_node) start_busstop = int( osm_node[osm_node['osmid'] == busstop_end_osm]["asset_ref"].values[0])
def pois_from_polygon(polygon, timeout=180, maxsize=''): """ Get OSM points of interest with a polygon. This is a recreation of :py:func:`osmnx.pois_from_polygon` with one crucial difference. `osmnx` targets the nodes, the ways and the relations based on "amenity" tag. This is useful but it misses pretty much all the grocery stores (don't have an amenity, but have a "shop" tag) and all the fitness centers (no amenity, but have a "leisure" tag). Here we use a slightly different approach: we find the ways and the relations with an "amenity" tag, but also fetch all the named nodes (i.e. those with the tag "name" defined). This covers pretty much everything except for occasional nameless locations. :param shapely.geometry.Polygon: the polygon that will be used to limit the POI search. :param int timeout: timeout for the API request. """ west, south, east, north = polygon.bounds query_template = ( '{settings};(' '(node["name"]({south:.6f},{west:.6f},{north:.6f},{east:.6f});(._;>;););' '(node["amenity"]({south:.6f},{west:.6f},{north:.6f},{east:.6f});(._;>;););' '(way["amenity"]({south:.6f},{west:.6f},{north:.6f},{east:.6f});(._;>;););' '(relation["amenity"]({south:.6f},{west:.6f},{north:.6f},{east:.6f});(._;>;););' ');out;') query = query_template.format( north=north, south=south, east=east, west=west, settings=osmnx.settings.default_overpass_query_settings.format( timeout=timeout, maxsize=maxsize)) responses = osmnx.overpass_request(data={"data": query}, timeout=timeout) # NOTE: Starting from here and until the end of the function we # copy the code from osmnx.pois.create_poi_gdf() with very # superficial changes. Please refer to LICENSE_OSMNX.md for the # original licence and copyright attribution of the copied code. # --- BORROWED FROM osmnx.pois.create_poi_gdf() ----------------------- # Parse coordinates from all the nodes in the response coords = osmnx.parse_nodes_coords(responses) # POI nodes poi_nodes = {} # POI ways poi_ways = {} # A list of POI relations relations = [] for result in responses['elements']: if result['type'] == 'node' and 'tags' in result: poi = osmnx.parse_osm_node(response=result) # Add element_type poi['element_type'] = 'node' # Add to 'pois' poi_nodes[result['id']] = poi elif result['type'] == 'way': # Parse POI area Polygon poi_area = osmnx.parse_polygonal_poi(coords=coords, response=result) if poi_area: # Add element_type poi_area['element_type'] = 'way' # Add to 'poi_ways' poi_ways[result['id']] = poi_area elif result['type'] == 'relation': # Add relation to a relation list (needs to be parsed # after all nodes and ways have been parsed) relations.append(result) # Create GeoDataFrames gdf_nodes = geopandas.GeoDataFrame(poi_nodes).T gdf_nodes.crs = osmnx.settings.default_crs gdf_ways = geopandas.GeoDataFrame(poi_ways).T gdf_ways.crs = osmnx.settings.default_crs # Parse relations (MultiPolygons) from 'ways' gdf_ways = osmnx.parse_osm_relations(relations=relations, osm_way_df=gdf_ways) # Combine GeoDataFrames gdf = gdf_nodes.append(gdf_ways, sort=False) if polygon: gdf = gdf.loc[gdf['geometry'].centroid.within(polygon) == True] return gdf