def create_graph(response_jsons, name='unnamed', retain_all=True, bidirectional=False): """ Create a networkx graph from Overpass API HTTP response objects. Parameters ---------- response_jsons : list list of dicts of JSON responses from from the Overpass API name : string the name of the graph retain_all : bool if True, return the entire graph even if it is not connected bidirectional : bool if True, create bidirectional edges for one-way streets Returns ------- networkx multidigraph """ # make sure we got data back from the server requests elements = [] elements.extend(response_jsons['elements']) if len(elements) < 1: raise ox.EmptyOverpassResponse( 'There are no data elements in the response JSON objects') # create the graph as a MultiDiGraph and set the original CRS to default_crs G = nx.MultiDiGraph(name=name, crs=ox.settings.default_crs) # extract nodes and paths from the downloaded osm data nodes = {} paths = {} nodes_temp, paths_temp = parse_osm_nodes_paths(response_jsons) for key, value in nodes_temp.items(): nodes[key] = value for key, value in paths_temp.items(): paths[key] = value # add each osm node to the graph for node, data in nodes.items(): G.add_node(node, **data) # add each osm way (aka, path) to the graph G = ox.add_paths(G, paths, bidirectional=bidirectional) # retain only the largest connected component, if caller did not # set retain_all=True if not retain_all: G = ox.get_largest_component(G) # add length (great circle distance between nodes) attribute to each edge to # use as weight if len(G.edges) > 0: G = ox.add_edge_lengths(G) return G
def route_from_relation(relation, nodes, paths): """ Construct a route given the OSM relation. :param dict relation: OSM relation corresponding to the route. :param dict nodes: a lookup dictionary of the graph nodes. :param dict paths: a lookup dictionary of the graph edges. :returns: a tuple of line name and a multigraph. """ route_nodes = {} route_paths = {} rel_tags = relation["tags"] line = rel_tags.get("line") or rel_tags.get("ref") tags = { "line": line, "name": rel_tags.get("name"), "type": rel_tags.get("route") } route = networkx.MultiDiGraph(name=line, crs=osmnx.settings.default_crs) for member in relation["members"]: key = member["ref"] if member["type"] == "node": route_nodes[key] = nodes[key] if member["type"] == "way": path = paths[key].copy() path.update(**tags) if "nodes" not in path: continue route_paths[key] = path for node_key in path["nodes"]: route_nodes[node_key] = nodes[node_key] if not route_paths: return for key, data in route_nodes.items(): route.add_node(key, **data) route = osmnx.add_paths(route, route_paths) route = osmnx.add_edge_lengths(route) return line, route
import numpy as np import osmnx as ox from flask import Flask, render_template, redirect, url_for,request from flask import make_response,jsonify app = Flask(__name__) #https://github.com/gboeing/osmnx-examples/blob/master/notebooks/12-node-elevations-edge-grades.ipynb api_key = "AIzaSyCm0ZhqcDDR75EnTs4EfSxFIdFIdowigCs" place = 'Amherst' place_query = {'city':'Amherst', 'state':'Massachusetts', 'country':'USA'} G = ox.graph_from_place(place_query, network_type='drive') G = ox.add_node_elevations(G, api_key=api_key) G = ox.add_edge_grades(G) G = ox.add_edge_lengths(G) G_proj = ox.project_graph(G) @app.route('/') def webprint(): return render_template('map_test.html') @app.route('/route', methods=['GET', 'POST']) def route(): route = getRoute((float(request.form['start']), float(request.form['start2'])),(float(request.form['end']),float(request.form['end2'])),float(request.form['distance']),float(request.form['elevation'])) gdf = ox.graph_to_gdfs(G, edges=False) gdf = gdf[['osmid','x','y']] l = [] for x in route: data = gdf.loc[gdf['osmid']==x].values.tolist()