def test_get_network_methods(): import geopandas as gpd north, south, east, west = 37.79, 37.78, -122.41, -122.43 with httmock.HTTMock(get_mock_response_content('overpass-response-6.json.gz')): G1 = ox.graph_from_bbox(north, south, east, west, network_type='drive_service') with httmock.HTTMock(get_mock_response_content()): G1 = ox.graph_from_bbox(north, south, east, west, network_type='drive_service', truncate_by_edge=True) location_point = (37.791427, -122.410018) bbox = ox.bbox_from_point(location_point, project_utm=True) with httmock.HTTMock(get_mock_response_content('overpass-response-8.json.gz')): G2 = ox.graph_from_point(location_point, distance=750, distance_type='bbox', network_type='drive') with httmock.HTTMock(get_mock_response_content('overpass-response-9.json.gz')): G3 = ox.graph_from_point(location_point, distance=500, distance_type='network') with httmock.HTTMock(get_mock_response_content('overpass-response-10.json.gz')): G4 = ox.graph_from_address(address='350 5th Ave, New York, NY', distance=1000, distance_type='network', network_type='bike') places = ['Los Altos, California, USA', {'city':'Los Altos Hills', 'state':'California'}, 'Loyola, California'] with httmock.HTTMock(get_mock_response_content('overpass-response-11.json.gz')): G5 = ox.graph_from_place(places, network_type='all', clean_periphery=False) calif = gpd.read_file('tests/input_data/ZillowNeighborhoods-CA') mission_district = calif[(calif['CITY']=='San Francisco') & (calif['NAME']=='Mission')] polygon = mission_district['geometry'].iloc[0] with httmock.HTTMock(get_mock_response_content('overpass-response-12.json.gz')): G6 = ox.graph_from_polygon(polygon, network_type='walk')
def test_get_network_methods(): from shapely import wkt # graph from bounding box north, south, east, west = 37.79, 37.78, -122.41, -122.43 G1 = ox.graph_from_bbox(north, south, east, west, network_type='drive_service') G1 = ox.graph_from_bbox(north, south, east, west, network_type='drive_service', truncate_by_edge=True) # graph from point location_point = (37.791427, -122.410018) bbox = ox.bbox_from_point(location_point, project_utm=True) G2 = ox.graph_from_point(location_point, distance=750, distance_type='bbox', network_type='drive') G3 = ox.graph_from_point(location_point, distance=500, distance_type='network') # graph from address G4 = ox.graph_from_address(address='350 5th Ave, New York, NY', distance=1000, distance_type='network', network_type='bike') # graph from list of places places = ['Los Altos, California, USA', {'city':'Los Altos Hills', 'state':'California'}, 'Loyola, California'] G5 = ox.graph_from_place(places, network_type='all', clean_periphery=False) # graph from polygon polygon = wkt.loads('POLYGON ((-122.418083 37.754154, -122.418082 37.766028, -122.410909 37.766028, -122.410908 37.754154, -122.418083 37.754154))') G6 = ox.graph_from_polygon(polygon, network_type='walk') # test custom query filter filtr = ('["area"!~"yes"]' '["highway"!~"motor|proposed|construction|abandoned|platform|raceway"]' '["foot"!~"no"]' '["service"!~"private"]' '["access"!~"private"]') G = ox.graph_from_point(location_point, network_type='walk', custom_filter=filtr)
def test_get_network_methods(): # graph from bounding box _ = ox.utils_geo.bbox_from_point(location_point, project_utm=True, return_crs=True) north, south, east, west = ox.utils_geo.bbox_from_point(location_point, dist=500) G = ox.graph_from_bbox(north, south, east, west, network_type="drive") G = ox.graph_from_bbox(north, south, east, west, network_type="drive_service", truncate_by_edge=True) # truncate graph by bounding box north, south, east, west = ox.utils_geo.bbox_from_point(location_point, dist=400) G = ox.truncate.truncate_graph_bbox(G, north, south, east, west) # graph from address G = ox.graph_from_address(address=address, dist=500, dist_type="bbox", network_type="bike") # graph from list of places G = ox.graph_from_place([place1], network_type="drive", clean_periphery=False) # graph from polygon G = ox.graph_from_polygon(polygon, network_type="walk", truncate_by_edge=True) # test custom query filter cf = ( '["highway"]' '["area"!~"yes"]' '["highway"!~"motor|proposed|construction|abandoned|platform|raceway"]' '["foot"!~"no"]' '["service"!~"private"]' '["access"!~"private"]') G = ox.graph_from_point(location_point, dist=500, custom_filter=cf, dist_type="bbox", network_type="all") G = ox.graph_from_point( location_point, dist=500, dist_type="network", network_type="all_private", )
def test_get_network_methods(): # graph from bounding box north, south, east, west = ox.utils_geo.bbox_from_point(location_point, dist=500) G = ox.graph_from_bbox(north, south, east, west, network_type="drive") G = ox.graph_from_bbox(north, south, east, west, network_type="drive_service", truncate_by_edge=True) # graph from address G = ox.graph_from_address(address=address, dist=500, dist_type="bbox", network_type="bike") # graph from list of places G = ox.graph_from_place([place1], network_type="drive", clean_periphery=False) # graph from polygon G = ox.graph_from_polygon(polygon, network_type="walk", truncate_by_edge=True) # test custom query filter cf = ( '["area"!~"yes"]' '["highway"!~"motor|proposed|construction|abandoned|platform|raceway"]' '["foot"!~"no"]' '["service"!~"private"]' '["access"!~"private"]') G = ox.graph_from_point(location_point, dist=500, custom_filter=cf, dist_type="bbox", network_type="all") # test custom settings cs = '[out:json][timeout:180][date:"2019-10-28T19:20:00Z"]' G = ox.graph_from_point( location_point, dist=500, custom_settings=cs, dist_type="network", network_type="all_private", )
def load_grid_edges(grid_x, grid_y, crs): x1 = grid_x - GRID_SIZE / 2 x2 = grid_x + GRID_SIZE + GRID_SIZE / 2 y1 = grid_y - GRID_SIZE / 2 y2 = grid_y + GRID_SIZE + GRID_SIZE / 2 ls = LineString([ [x1, y1], [x2, y1], [x2, y2], [x1, y2], [x1, y1], ]) t = gpd.GeoDataFrame(geometry=[ls], crs=crs) tll = ox.project_gdf(t, to_latlong=True) west, south, east, north = tll.total_bounds try: tgp = ox.project_graph(ox.graph_from_bbox( north, south, east, west, truncate_by_edge=True, simplify=True, clean_periphery=False, network_type='all', retain_all=True )) tnp, tep = ox.graph_to_gdfs(tgp, nodes=True, edges=True) return tep except ox.core.EmptyOverpassResponse: pass return gpd.GeoDataFrame(geometry=[])
def calculateRoutes(self, frm, to): if isinstance(frm, tuple) and isinstance(to, tuple): #center = self.GetCenter(frm, to) north, south, east, west = self.GetBBox(frm, to) try: self.G = ox.graph_from_bbox(north, south, east, west, truncate_by_edge=True) #self.G = ox.graph_from_point(center, truncate_by_edge=True) except Exception as e: print("GraphManager: Error when get graph.\n%s" % (str(e))) return [] self.calculateWeight() fromNode = ox.get_nearest_node(self.G, frm) print("GraphCanvas: From nearest node {}.".format(fromNode)) toNode = ox.get_nearest_node(self.G, to) print("GraphCanvas: To nearest node {}.".format(toNode)) routes = [] for i in range(4): routes.append( nx.shortest_path(self.G, fromNode, toNode, weight='type%d' % (i))) if self.columsLen >= 10: routes.append( nx.shortest_path(self.G, fromNode, toNode, weight='type10')) return routes return []
def get_rivers(bbox, incl_streams=True): infra = '["waterway"="river"]' if incl_streams: infra = infra + '["waterway"="stream"]' G = ox.graph_from_bbox(*bbox, custom_filter=infra, simplify=True, retain_all=True, clean_periphery=False) rivers = [] for u, v, data in G.edges(keys=False, data=True): name = "" if "name" in data: name = data["name"] if "geometry" in data: pts = np.array(data["geometry"].xy) else: node = G.nodes pts = [[node[u]['x'], node[v]['x']], [node[u]['y'], node[v]['y']]] pts = np.array(pts) line = Line(name, pts, color="g", type="river") rivers.append(line) return rivers, G
def get_pistes_osmnx(bbox): infra = '["piste:type"="downhill"]' G = ox.graph_from_bbox(*bbox, custom_filter=infra) clrs = ["g", "g", "b", "r", "k", "w"] trail_types = ["novice", "easy", "intermediate", "advanced", "expert"] pistes = [] for u, v, data in G.edges(keys=False, data=True): name, diff = "", "" if "name" in data: name = data["name"] if "geometry" in data: pts = np.array(data["geometry"].xy) else: node = G.nodes pts = [[node[u]['x'], node[v]['x']], [node[u]['y'], node[v]['y']]] pts = np.array(pts) if "piste:difficulty" in data: diff = data["piste:difficulty"] idx = trail_types.index(diff) if diff in trail_types else -1 line = Line(name, pts, color=clrs[idx], type=diff) pistes.append(line) return pistes
def download_graph(loc_list, **kwargs): simplify = kwargs.get("simplify",True) # get rid of non-important nodes? auto_bbox = kwargs.get("auto_bbox",True) # auto query polygon bbox show_plot = kwargs.get("show_plot",True) distance = kwargs.get("distance",20000) print("osmnx download_graph()...") if isinstance(loc_list,list) and isinstance(loc_list[0],float) and len(loc_list)>2: # [lats,latn,lngw,lnge] north=loc_list[1] south=loc_list[0] east=loc_list[3] west=loc_list[2] G=ox.graph_from_bbox(north,south,east,west,network_type='drive',simplify=simplify) elif isinstance(loc_list,list) and isinstance(loc_list[0],float) and len(loc_list)==2:# [lat,lng] G=ox.graph_from_point(loc_list, distance=distance,network_type='drive',simplify=simplify) elif isinstance(loc_list, str) or (isinstance(loc_list,list) and isinstance(loc_list[0], str)): if auto_bbox: # addr or [addr1,addr2,] use auto bbox G=ox.graph_from_place(loc_list,network_type='drive',simplify=simplify)# No-distance arg else: # addr or [addr1,addr2,] use distance G=ox.graph_from_address(loc_list,network_type='drive',distance=distance,simplify=simplify) else: print(__file__+" download_graph() input error: ",loc_list) return None if show_plot and iprint and Has_Display: if iprint>=2: print("download_graph showing downloaded graph...") fig, ax = ox.plot_graph(G) if not simplify: simplify_graph(G) if iprint>=2: print ox.basic_stats(G) return G
def make_graph_and_trace(): # north, south, east, west = 37.79, 37.78, -122.41, -122.43 p = Polygon([[-122.27465629577637, 37.7983230783235], [-122.26096630096434, 37.7983230783235], [-122.26096630096434, 37.80761398306056], [-122.27465629577637, 37.80761398306056], [-122.27465629577637, 37.7983230783235]]) west, south, east, north = p.bounds G1 = ox.graph_from_bbox(north, south, east, west) # make meter projected G1 = ox.project_graph(G1) print('Converting to this projection:', G1.graph['crs']) project_to_meter = Transformer.from_crs(CRS.from_epsg(4326), G1.graph['crs'], always_xy=True) # create trace path and reproject trace = LineString([[-122.2701072692871, 37.798865645037765], [-122.26969957351685, 37.79971339755073], [-122.26935625076295, 37.800222044388384], [-122.26892709732056, 37.80079850657009], [-122.26845502853392, 37.80135800967933], [-122.26856231689453, 37.801646236899785], [-122.26939916610718, 37.80208705282608], [-122.27008581161499, 37.80235832285767], [-122.27113723754883, 37.8026635004523]]) trace = transform(project_to_meter.transform, LineString(trace)) return G1, trace
def load_graph(place): """ load graph networkx :param place: tuple (name [string], (north_lat [float], south_lat [float], east_lon [float], west_lon [float])) :return g: networkx graph """ print(' Loading graph ...') name, bbox = place try: with open('./graphs_dir/{0}.pkl'.format(name), 'rb') as f: graph = pickle.load(f) except FileNotFoundError: if bbox is None: graph = ox.graph_from_place(name, network_type='drive') else: graph = ox.graph_from_bbox(bbox[0], bbox[1], bbox[3], bbox[2], network_type='drive') graph = max(nx.strongly_connected_component_subgraphs(graph), key=len) try: with open('./graphs_dir/{0}.pkl'.format(name), 'wb') as f: pickle.dump(graph, f, protocol=4) except MemoryError: print(' Warning: Dump unsuccessful ...') print(' Done!') return graph
def InitialSetup(): custom_walk = ('["area"!~"yes"]["highway"="footway"]["foot"!~"no"]["service"!~"private"]{}').format(ox.settings.default_access) global factor, img,image_map_horizontal,image_map_vertical ,nodes,edges, fig,ax,G_projected,threshold,G G = ox.graph_from_bbox(top, bottom,right, left,custom_filter= custom_walk)#network_type="walk")# ## G_projected = ox.core.graph_from_file("centralcampus.xml", network_type = 'walk',simplify=True,retain_all=False) ##Uncomment this for the original stuff G_projected = ox.project_graph(G) ox.plot_graph(G_projected,save = True,filename = "maps", show = False, node_color='#999999',node_edgecolor='#999999',edge_color='#999999',bgcolor = "#000000", node_zorder=3,dpi=200, edge_linewidth=8,use_geom=True) ox.simplify.clean_intersections(G,tolerance=100) nodes, edges = ox.graph_to_gdfs(G) fig,ax = pyplot.subplots() ax.axis ('OFF') img = Image.open('images/maps.png').convert('L').rotate(angle) img.save('lowered.png') height_compensation = 0 width_compensation = 0 w,h = img.size image_size_w,image_size_h=img.size image_map_horizontal = float(left-right)/image_size_w image_map_vertical = float(top-bottom)/image_size_h if img.width >= image_size and img.height >= image_size: if img.height < img.width: print "factor denom height is ",str(img.height) factor = float(image_size) / img.height else: print "factor denom width is ",str(img.width) factor = float(image_size) / img.width PrintImg(img)
def _get_graph_not_memoized(latitude: float, longitude: float, radius: float, google_maps_api_key: str) -> igraph.Graph: # calculate extent of the graph radius_latlong = radius / 60 north_lat = latitude + radius_latlong south_lat = latitude - radius_latlong east_long = longitude + radius_latlong west_long = longitude - radius_latlong # download graph graph = osmnx.graph_from_bbox(north_lat, south_lat, east_long, west_long, network_type='walk') graph = osmnx.add_node_elevations(graph, api_key=google_maps_api_key) graph = osmnx.add_edge_grades(graph) # convert node labels graph = networkx.relabel.convert_node_labels_to_integers(graph) # populate igraph graph_ig = igraph.Graph(directed=True) graph_ig.add_vertices(list(graph.nodes())) graph_ig.add_edges(list(graph.edges())) for attr in ('osmid', 'x', 'y'): graph_ig.vs[attr] = list( networkx.get_node_attributes(graph, attr).values()) for attr in ('length', 'grade'): graph_ig.es[attr] = list( networkx.get_edge_attributes(graph, attr).values()) graph_ig.es['streetname'] = [ str(s) for s in networkx.get_edge_attributes(graph, 'name').values() ] return graph_ig
def _get_cycleways(self): """ Creates all cycleways in a city (networkx.MultiDiGraph). """ useful_tags = ox.settings.useful_tags_way + ['cycleway'] ox.config(use_cache=True, log_console=True, useful_tags_way=useful_tags) if self.query_type == 'city': if self.city_limits is True: cycleways = ox.graph_from_place(query=self.city_name, network_type='bike', simplify=False) else: cycleways = ox.graph_from_bbox(self.north, self.south, self.east, self.west, network_type='bike', simplify=False) elif self.query_type == 'address': cycleways = ox.graph_from_address(self.address, network_type='bike', dist=self.distance) non_cycleways = [ (u, v, k) for u, v, k, d in cycleways.edges(keys=True, data=True) if not ('cycleway' in d or d['highway'] == 'cycleway') ] cycleways.remove_edges_from(non_cycleways) cycleways = ox.utils_graph.remove_isolated_nodes(cycleways) return cycleways
def download_osm_layer(area, mode): """Download a single-mode layer from OSM Parameters ---------- area : str or list String of geocoded place or list of [north, south, east, west] mode : str Mode choice of {‘walk’, ‘bike’, ‘drive’, ‘drive_service’, ‘all’, ‘all_private’, ‘none’} Returns ------- layer : networkx multidigraph OSM map layer of specific mode """ if isinstance(area, str): layer = ox.graph_from_place(area, network_type=mode) elif isinstance(area, list) and len(area) == 4: layer = ox.graph_from_bbox(area[0], area[1], area[2], area[3], network_type=mode) else: raise Exception('Graph area not geocoded place nor bounding box') return layer
def from_osm_bbox(cls, bbox): """Grab OpenStreetMap road data within bounding box. NOTE: Put this in a separate "data" module? """ G = ox.graph_from_bbox(*bbox, network_type='walk') return cls(G)
def generate_graph_from_coords(orig_coords: tuple, dest_coords: tuple): """Prüft, ob bereits ein Graph zu den angegebenen Koordinaten existiert und holt diesen oder erstellt diesen je nachdem. Der Graph enthält mind. die Koordinaten sowie ein wenig Abstand zum Rand von den Koordinaten aus. """ key = str( hash(orig_coords[0] + dest_coords[0] + orig_coords[1] + dest_coords[1])) if graphs.get(key, "-1") != "-1": print("**** graph fetched ****") return graphs[key] else: # using this, the bbox gets slighty bigger resulting in a more usable graph if orig_coords[0] > dest_coords[0]: north = orig_coords[0] * 1.00015 south = dest_coords[0] * 0.99985 else: north = dest_coords[0] * 1.00015 south = orig_coords[0] * 0.99985 if orig_coords[1] > dest_coords[1]: west = dest_coords[1] * 0.9993 east = orig_coords[1] * 1.0007 else: west = orig_coords[1] * 0.9993 east = dest_coords[1] * 1.0007 G = ox.graph_from_bbox(north, south, east, west) graphs[key] = G print("**** graph created ****") return G
def __get_from_osmnx(self, tile: Tile) -> gpd.GeoDataFrame: bbox: LngLatBbox = bounds(tile) west, south, east, north = bbox west -= LAT_LNG_BUFFER east += LAT_LNG_BUFFER north += LAT_LNG_BUFFER south -= LAT_LNG_BUFFER try: graph = ox.graph_from_bbox(north, south, east, west, simplify=False, retain_all=True) gdfs = ox.graph_to_gdfs(graph) for gdf in gdfs: self.cache = self.cache.append(gdf, ignore_index=True, sort=False) except ox.core.EmptyOverpassResponse: pass except ValueError as e: if "not enough values" in str(e): print( f"[WARNING] Could not load tile {tile}! Assuming it is empty..." ) else: raise e self.cached_tiles |= {tile} self.cached_tiles = set(simplify(*self.cached_tiles)) return self.__get_from_cache(tile)
def plot_edge_grades(): files_map = '../data/maps/m43.96267779776494_m19.944747838679202_m43.929659815391865_m19.905049264605925.graphml' G = ox.load_graphml(files_map) max_lat = -12.934333867695516 min_lat = -12.961083555974895 max_lon = -38.473331269107426 min_lon = -38.49996781691653 name_geotiff = '../data/maps/19S45_ZN.tif' G = ox.graph_from_bbox(max_lat, min_lat, max_lon, min_lon, network_type='all') G = Graph.set_node_elevation(G, name_geotiff) # deadends = [(u, v) for u, v, k, data in G.edges(keys=True, data=True) if data['highway'] == ''] # print(deadends) # G2.remove_nodes_from(deadends) G = Graph.edge_grades(G) G_proj = ox.project_graph(G) ec = ox.plot.get_edge_colors_by_attr(G_proj, "grade", cmap="plasma", num_bins=5, equal_size=True) fig, ax = ox.plot_graph(G_proj, edge_color=ec, edge_linewidth=0.5, node_size=0, bgcolor="w")
def test_nearest_edge(): # test in closest edge section sheik_sayed_dubai = [25.09, 25.06, 55.16, 55.11] location_coordinates = (25.071764, 55.138978) G = ox.graph_from_bbox(*sheik_sayed_dubai, simplify=False, retain_all=True, network_type='drive') geometry, u, v = ox.get_nearest_edge(G, location_coordinates)
def get_graph(place): string = place.split(',')[0] print('Fetching graph data for {}'.format(place)) poly = get_polygon(string) if poly == -1: gdf = ox.gdf_from_place('{}'.format(string)) G = ox.graph_from_bbox(gdf.bbox_north, gdf.bbox_south, gdf.bbox_east, gdf.bbox_west) val = 0 else: try: G = nx.read_gpickle('data/{a}/{a}'.format(a=string)) val = 1 except FileNotFoundError: G = ox.graph_from_polygon(poly, network_type='drive') val = 0 G = ox.project_graph(G) print('Writing graph file') try: os.mkdir('data/{}'.format(string)) except FileExistsError: pass if val != 1: nx.write_gpickle(G, path='data/{a}/{a}'.format(a=string)) return G
def test_nearest_edges(): from pyproj import Proj # test in closest edge section sheik_sayed_dubai = [25.09, 25.06, 55.16, 55.11] location_coordinates = (25.071764, 55.138978) G = ox.graph_from_bbox(*sheik_sayed_dubai, simplify=False, retain_all=True, network_type='drive') # Unprojected ne1 = ox.get_nearest_edges( G, X=[location_coordinates[1], location_coordinates[1]], Y=[location_coordinates[0], location_coordinates[0]], method='balltree', dist=0.0001) # Projected G2 = ox.project_graph(G) crs = Proj(G2.graph['crs']) projected_point = crs(location_coordinates[1], location_coordinates[0]) ne2 = ox.get_nearest_edges(G2, X=[projected_point[0], projected_point[0]], Y=[projected_point[1], projected_point[1]], method='kdtree', dist=10) assert (ne1 == ne2).all()
def setup_graph_bbox(self, bbox): print('+' * 15, 'setup graph from bounding box', '+' * 15) graph_name = 'graph_' + '_'.join( [str(v) for l in bbox.values() for v in l]).replace('.', 'p') + '.pkl' pickle_path = self.path_pickle + os.sep + graph_name if os.path.isfile(pickle_path): # Read previously downloaded graph print('Reading pickle file', os.path.basename(pickle_path)) self.graph = pickle.load(open(pickle_path, 'rb')) else: print('setting up graph for bounding box', bbox, ': downloading', end=' ... ') self.graph = osmnx.graph_from_bbox( south=bbox['lat'][0], north=bbox['lat'][1], west=bbox['lon'][0], east=bbox['lon'][1], network_type='all' ) # could use bike but this might be to restrictive print('converting to undirected graph', end=' ... ') self.graph = osmnx.get_undirected( self.graph ) # forget the direction, this might be to restrictive # print('projecting to UTM', end=' ... ') # self.graph = osmnx.project_graph(self.graph) # convert to x,y projection for faster euclidean calculations print('saving to', pickle_path) pickle.dump(self.graph, open(pickle_path, 'bw')) print('-' * 42)
def convert_graph_from_osmnx(self, bbox, log, start_from = -1): if log: logging.basicConfig(filename='create_graph_log.txt', level=logging.INFO) logging.info('start...') G = ox.graph_from_bbox(bbox[0], bbox[1], bbox[2], bbox[3]) all_nodes = G.nodes() count = len(all_nodes) start = time.time() for i, osm_id in enumerate(G.nodes()): if i < start_from: continue node = G.nodes[osm_id] self.add_one_document(node) for nei_osm_id in G.neighbors(osm_id): nei_node = G.nodes[nei_osm_id] self.add_one_document(nei_node) dist = self.calcDistance(node['x'], node['y'], nei_node['x'], nei_node['y']) self.add_neighbors(node, nei_node, dist) if log: logging.info(f"[{i}/{count}]... {time.time() - start} sec") if log: logging.info("create indexes") self.mongoDB.create_index('id', pymongo.ASCENDING) self.mongoDB.create_index('coordinates', pymongo.GEOSPHERE) if log: logging.info(f"[{count}/{count}]... complete {time.time() - start} sec")
def graph_scenario(stop_points, geotiff_name, ad_weights, file_name_osm): max_lat, min_lat, max_lon, min_lon = Coordinates.create_osmnx(stop_points) # Scenario graph (paths are edges and junctions are nodes) G = ox.graph_from_bbox(max_lat, min_lat, max_lon, min_lon, network_type='all') G, nodes_coordinates, nodes_mass_increment = Graph.configure_graph(G, geotiff_name, stop_points, ad_weights, file_name_osm) return G, nodes_coordinates, nodes_mass_increment
def test_something(self): osmnx_bellver_graph = osmnx.graph_from_bbox(39.5713, 39.5573, 2.6257, 2.6023) expected_edges = osmnx_bellver_graph.edges(data=True) bellver_graph = Graph(39.5713, 39.5573, 2.6257, 2.6023) edges = bellver_graph.get_edges() self.assertEqual(type(edges), type(expected_edges)) self.assertEqual(True, True)
def osm_downloader(boundary=None, osm_path=None, regenerating_shp=False): """ Download drive network within a certain geographical boundary. :param boundary: geodataframe of the area for downloading the network. :param osm_path: file path to save the network in .shp and .graphml. :param regenerating_shp: if needs to regenerating the shape file. :return: None """ minx, miny, maxx, maxy = boundary.geometry.total_bounds new_network = osm_path + 'drive.graphml' new_network_shp = osm_path + 'drive.shp' def shp_processor(G, new_network_shp): print('Processing graphml to GeoDataframe...') gdf_n = ox.graph_to_gdfs(G) edge = gdf_n[1] edge = edge.loc[:, [ 'geometry', 'highway', 'junction', 'length', 'maxspeed', 'name', 'oneway', 'osmid', 'u', 'v', 'width', 'lanes' ]] fields = ['osmid', 'u', 'v', 'length', 'maxspeed', 'oneway'] df_inter = pd.DataFrame() for f in fields: df_inter[f] = edge[f].astype(str) gdf_edge = gpd.GeoDataFrame(df_inter, geometry=edge["geometry"]) gdf_edge = gdf_edge.rename(columns={ 'osmid': 'osm_id', 'maxspeed': 'max_speed' }) print('Saving as shp...') gdf_edge.to_file(new_network_shp) if not os.path.exists(new_network): print('Downloading graphml...') G = ox.graph_from_bbox(maxy, miny, maxx, minx, network_type='drive') print('Saving as graphml...') ox.save_graphml(G, filepath=new_network) if not os.path.exists(new_network_shp): shp_processor(G, new_network_shp) else: if regenerating_shp: print('Loading graphml...') G = ox.load_graphml(new_network) shp_processor(G, new_network_shp) if not os.path.exists(new_network_shp): print('Loading graphml...') G = ox.load_graphml(new_network) shp_processor(G, new_network_shp) else: print('Drive networks exist. Skip downloading.')
def __init__(self, place): self.place = place if str != type(place): place = json.dumps(place) _ghsh = utils.calculateMD5ForString(place) graph_file = os.path.join('cache', '{0}.graph.p'.format(_ghsh)) edges_file = os.path.join('cache', '{0}.edges.p'.format(_ghsh)) # nodes_file = os.path.join('cache', '{0}.nodes.p'.format(_ghsh)) if os.path.exists(graph_file): Logger.debug("Loading graph from cached file") self.Graph = pickle.load(open(graph_file, "rb")) self.Edges = pickle.load(open(edges_file, "rb")) # self.Nodes = pickle.load( open( nodes_file, "rb" ) ) else: Logger.debug("Building graph from osm data") if str == type(self.place): try: self.Graph = ox.graph_from_place(self.place, network_type='drive', simplify=False, retain_all=True) except: self.Graph = ox.graph_from_place(self.place, network_type='drive', simplify=False, retain_all=True, which_result=2) else: # try: self.Graph = ox.graph_from_bbox(self.place['north'], self.place['south'], self.place['east'], self.place['west'], network_type='drive', simplify=False, retain_all=True) # except: # self.Graph = ox.graph_from_bbox( # self.place['north'], # self.place['south'], # self.place['east'], # self.place['west'], # network_type='drive', simplify=False, retain_all=True, which_result=2) self.Edges = ox.graph_to_gdfs(self.Graph, nodes=False, edges=True) # self.Nodes = ox.graph_to_gdfs(self.Graph, nodes=True, edges=false) Logger.debug("Saving graph to cached file") pickle.dump(self.Graph, open(graph_file, "wb")) pickle.dump(self.Edges, open(edges_file, "wb"))
def get_map(xmin, ymin, xmax, ymax): try: return ox.graph_from_bbox(ymax, ymin, xmax, xmin, network_type='drive') except Exception: ymax = ymax * 1.0002 ymin = ymin * 0.9998 xmax = xmax + (xmax * -0.0002) xmin = xmin * 1.0002 return get_map(xmin, ymin, xmax, ymax)
def map_matching_node( move_data: Union[PandasMoveDataFrame, DaskMoveDataFrame, PandasDiscreteMoveDataFrame], inplace: Optional[bool] = True, bbox: Optional[Tuple[float, float, float, float]] = None, place: Optional[Text] = None, G: Optional[MultiDiGraph] = None ) -> Optional[DataFrame]: """ Generate Map matching using the graph nodes Parameters ---------- move_data : MoveDataFrame The input trajectories data inplace: bool, optional if set to true the original dataframe will be altered, otherwise the alteration will be made in a copy, that will be returned, by default True bbox : tuple, optional The bounding box as (north, east, south, west), by default None place : string, optional The query to geocode to get place boundary polygon, by default None G : MultiDiGraph, optional The input graph, by default None Returns ------- move_data : MoveDataFrame A copy of the original dataframe or None """ if(G is None): if(bbox is None): bbox = move_data.get_bbox() G = ox.graph_from_bbox( bbox[0], bbox[2], bbox[1], bbox[3], network_type='all_private' ) elif(place is not None): G = ox.footprints_from_place(query=place, tags={'network_type': 'all_private'}) if not inplace: move_data = move_data[:] nodes = ox.get_nearest_nodes( G, X=move_data['lon'], Y=move_data['lat'], method='kdtree' ) gdf_nodes = ox.graph_to_gdfs(G, edges=False) df_nodes = gdf_nodes.loc[nodes] move_data['lat'] = list(df_nodes.y) move_data['lon'] = list(df_nodes.x) move_data['geometry'] = list(df_nodes.geometry) if not inplace: return move_data
def test_nearest_edges(): from pyproj import Proj # test in closest edge section sheik_sayed_dubai = [25.09, 25.06, 55.16, 55.11] location_coordinates = (25.071764, 55.138978) G = ox.graph_from_bbox(*sheik_sayed_dubai, simplify=False, retain_all=True, network_type='drive') # Unprojected ne1 = ox.get_nearest_edges(G, X=[location_coordinates[1], location_coordinates[1]], Y=[location_coordinates[0], location_coordinates[0]], method='balltree', dist=0.0001) # Projected G2 = ox.project_graph(G) crs = Proj(G2.graph['crs']) projected_point = crs(location_coordinates[1], location_coordinates[0]) ne2 = ox.get_nearest_edges(G2, X=[projected_point[0], projected_point[0]], Y=[projected_point[1], projected_point[1]], method='kdtree', dist=10) assert (ne1 == ne2).all()