检索起点和终点之间的建筑物地址
Retreiving building addresses between origin and destination
我正在使用 OSMNx 并尝试检索起点和终点之间的地址,如下图所示。
- 是否可以提取两点或节点之间的地址?
import osmnx as ox
import networkx as nx
import matplotlib.pyplot as plt
address='45 Stodart St, Colac VIC 3250'
G = ox.graph_from_address(address, distance=500, network_type="drive")
fig1, ax1 = ox.plot_graph(G,node_size=0, edge_linewidth=0.5, dpi=250)
- 是否可以提取两点或节点之间的地址?
Is it possible to extract the addresses between two points or nodes?
是:
import osmnx as ox
ox.config(use_cache=True, log_console=True)
address = '20 W 34th St, Manhattan, NY, USA'
G = ox.graph_from_address(address, dist=500, network_type='drive')
# osmids of nodes at either end of block
# or you could do this programmatically with ox.geocode and ox.get_nearest_node
osmids = [42437644, 42430304]
# bounding box around those nodes, buffered as needed
polygon = ox.graph_to_gdfs(G, edges=False).loc[osmids, 'geometry'].unary_union.envelope
polygon_proj, crs = ox.projection.project_geometry(polygon)
polygon_buff, crs = ox.projection.project_geometry(polygon_proj.buffer(10), crs=crs, to_latlong=True)
# get building footprints and show their addresses
fp = ox.footprints_from_polygon(polygon)
cols = [c for c in fp.columns if 'addr' in c]
fp[cols]
所以是的,这是可能的。但是对于您特定的所需位置是不可能的,因为 OpenStreetMap 不包含那里的建筑物或地址信息。您发布的屏幕截图来自 Google 地图而不是 OpenStreetMap。
正在检索目标区域的所有街道。
import networkx as nx
import osmnx as ox
import geopandas as gpd
import pandas as pd
import csv
import pprint
ox.config(log_console=True, use_cache=True)
G = ox.graph_from_place('Colac, Victoria, Australia', network_type='drive')
G = ox.project_graph(G)
ints = ox.clean_intersections(G)
gdf = gpd.GeoDataFrame(ints, columns=['geometry'], crs=G.graph['crs'])
X = gdf['geometry'].map(lambda pt: pt.coords[0][0])
Y = gdf['geometry'].map(lambda pt: pt.coords[0][1])
nodes = ox.get_nearest_nodes(G, X, Y, method='kdtree')
connections = {}
for n in nodes:
connections[n] = set([])
for nbr in nx.neighbors(G, n):
for d in G.get_edge_data(n, nbr).values():
if 'name' in d:
if type(d['name']) == str:
connections[n].add(d['name'])
elif type(d['name']) == list:
for name in d['name']:
connections[n].add(name)
else:
connections[n].add(None)
else:
connections[n].add(None)
#%%
pprint.pprint(connections)
我正在使用 OSMNx 并尝试检索起点和终点之间的地址,如下图所示。
- 是否可以提取两点或节点之间的地址?
import osmnx as ox
import networkx as nx
import matplotlib.pyplot as plt
address='45 Stodart St, Colac VIC 3250'
G = ox.graph_from_address(address, distance=500, network_type="drive")
fig1, ax1 = ox.plot_graph(G,node_size=0, edge_linewidth=0.5, dpi=250)
- 是否可以提取两点或节点之间的地址?
Is it possible to extract the addresses between two points or nodes?
是:
import osmnx as ox
ox.config(use_cache=True, log_console=True)
address = '20 W 34th St, Manhattan, NY, USA'
G = ox.graph_from_address(address, dist=500, network_type='drive')
# osmids of nodes at either end of block
# or you could do this programmatically with ox.geocode and ox.get_nearest_node
osmids = [42437644, 42430304]
# bounding box around those nodes, buffered as needed
polygon = ox.graph_to_gdfs(G, edges=False).loc[osmids, 'geometry'].unary_union.envelope
polygon_proj, crs = ox.projection.project_geometry(polygon)
polygon_buff, crs = ox.projection.project_geometry(polygon_proj.buffer(10), crs=crs, to_latlong=True)
# get building footprints and show their addresses
fp = ox.footprints_from_polygon(polygon)
cols = [c for c in fp.columns if 'addr' in c]
fp[cols]
所以是的,这是可能的。但是对于您特定的所需位置是不可能的,因为 OpenStreetMap 不包含那里的建筑物或地址信息。您发布的屏幕截图来自 Google 地图而不是 OpenStreetMap。
正在检索目标区域的所有街道。
import networkx as nx
import osmnx as ox
import geopandas as gpd
import pandas as pd
import csv
import pprint
ox.config(log_console=True, use_cache=True)
G = ox.graph_from_place('Colac, Victoria, Australia', network_type='drive')
G = ox.project_graph(G)
ints = ox.clean_intersections(G)
gdf = gpd.GeoDataFrame(ints, columns=['geometry'], crs=G.graph['crs'])
X = gdf['geometry'].map(lambda pt: pt.coords[0][0])
Y = gdf['geometry'].map(lambda pt: pt.coords[0][1])
nodes = ox.get_nearest_nodes(G, X, Y, method='kdtree')
connections = {}
for n in nodes:
connections[n] = set([])
for nbr in nx.neighbors(G, n):
for d in G.get_edge_data(n, nbr).values():
if 'name' in d:
if type(d['name']) == str:
connections[n].add(d['name'])
elif type(d['name']) == list:
for name in d['name']:
connections[n].add(name)
else:
connections[n].add(None)
else:
connections[n].add(None)
#%%
pprint.pprint(connections)