-1

I am using OSMNx and trying to retrieve the addresses between Origin and destination point as shown in below figure.

  • Is it possible to extract the addresses between two points or Nodes?

enter image description here

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?
MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
Case Msee
  • 405
  • 5
  • 17

2 Answers2

1

Is it possible to extract the addresses between two points or nodes?

Yes:

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]

So yes, this is possible. But it is not possible for your specific desired location, because OpenStreetMap contains no building or address information there. The screenshot you posted is from Google Maps rather than OpenStreetMap.

gboeing
  • 5,691
  • 2
  • 15
  • 41
  • Thank you for the great answer. The above solution worked well for a single street. Actually, I am trying to retrieve all the building addresses of all the streets in a network. For instance, if there are 254 nodes in a network I want to extract all the addresses across these nodes. As a solution, I extracted all the street of the targeted area by following one of your tutorials.The code is attached in below answer. The problem is how can I generate all the addresses using the OSMID and street names. The targetted area is https://www.openstreetmap.org/relation/3145220 – Case Msee Jun 07 '20 at 14:53
0

Retrieving all streets of the targetted area.

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)
Case Msee
  • 405
  • 5
  • 17