0

I have some origin data that contains lat-lng coordinates, and when I use osmnx's get_nearest_edges method, I want to filter those coordinates that are not in the given city (San Francisco in this example). Is there any convenient method that implement this feature?

Here is part of my code:

roadId = ox.utils.get_nearest_edges(G, df['longitude'], df['latitude'], method='balltree')
df['startId'] = roadId[:,0]
df['endId'] = roadId[:,1]
startId = roadId[:,0]
endId = roadId[:,1]
gdf_nodes, gdf_edges = ox.graph_to_gdfs(G)
startInfo = gdf_nodes.loc[startId]
endInfo = gdf_nodes.loc[endId]
df['startLat'] = startInfo.loc[:, ['y']].values
df['startLon'] = startInfo.loc[:, ['x']].values
df['endLat'] = endInfo.loc[:, ['y']].values
df['endLon'] = endInfo.loc[:, ['x']].values

The first line's G is from this:

G = ox.graph_from_place('San Francisco, California, USA', network_type='drive')

And the output file is like this:

latitude 37.61549

longitude -122.38821  

startId 65365765

endId 65365766

startLat 37.708957

startLon -122.392803 

endLat 37.708785 

endLon -122.393012

This example is what I want to express, because the road in the result is not in San Francisco, how can I identify it in the code and remove it?

4

1 回答 1

1

You asked two questions. First, how to determine if a pair of lat-lng coordinates is within a city's boundary? Second, how to get the bounding box of a city? Here is how to do both with OSMnx (and shapely, which OSMnx is built on top of):

import osmnx as ox
from shapely.geometry import Point
gdf = ox.gdf_from_place('Piedmont, CA, USA')
geom = gdf.loc[0, 'geometry']

# get the bounding box of the city
geom.bounds

# determine if a point is within the city boundary
coords = (-122.24, 37.82)
geom.intersects(Point(coords))
于 2019-05-23T18:24:18.030 回答