I’m interested in using OSMnx to query bike-lane information around specific points in cities.
For example, if I was looking at (40.72182, -73.9892) in New York, I want to use OSMnx to display the bike-lane info for the street this point is closest to (Allen Street). With the closest way identified (in this instance, Way: Allen Street (993799930)), I’d like to see the values of the following tags:
cycleway:left
cycleway:right
cycleway:left:buffer
cycleway:right:buffer
I’ve tried the following code, but it seems to be selecting a way that is actually not the most adjacent to the point in question:
`import osmnx as ox
import networkx as nx
def get_nearest_street_info(lat, lon, dist=300):
# Define point as tuple (latitude, longitude)
point = (lat, lon)
# Download the street network for the area around the point
G = ox.graph_from_point(point, dist=dist, network_type='bike')
# Find the nearest node to the point
nearest_node = ox.distance.nearest_nodes(G, lon, lat)
# Find the nearest edge (street) to the point
nearest_edge = ox.distance.nearest_edges(G, lon, lat, return_dist=False)
# Get the edges GeoDataFrame
edges_gdf = ox.graph_to_gdfs(G, nodes=False, edges=True)
# Locate the nearest edge in the GeoDataFrame
nearest_edge_info = edges_gdf.loc[[nearest_edge]]
# Extract information about bike lanes
edge = nearest_edge_info.iloc[0]
info = {
'street': edge['name'],
'highway': edge['highway'],
'cycleway:left': edge.get('cycleway:left', 'No bike lane'),
'cycleway:right': edge.get('cycleway:right', 'No bike lane'),
'cycleway:left:buffer': edge.get('cycleway:left:buffer', 'No buffer'),
'cycleway:right:buffer': edge.get('cycleway:right:buffer', 'No buffer')
}
return info
# Example usage
lat, lon = 40.72182, -73.9892 # Latitude and longitude for point in New York City
nearest_street_info = get_nearest_street_info(lat, lon)
print(f"Street: {nearest_street_info['street']}")
print(f"Highway: {nearest_street_info['highway']}")
print(f"Cycleway Left: {nearest_street_info['cycleway:left']}")
print(f"Cycleway Right: {nearest_street_info['cycleway:right']}")
print(f"Cycleway Left Buffer: {nearest_street_info['cycleway:left:buffer']}")
print(f"Cycleway Right Buffer: {nearest_street_info['cycleway:right:buffer']}")
`
mmredwood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.