如何判断给定坐标是否在某个城市?

2024-09-28 01:26:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些包含lat lng坐标的原始数据,当我使用osmnx的get\u nearest\u edges方法时,我想过滤那些不在给定城市中的坐标(本例中为旧金山)。有什么方便的方法来实现这个特性吗?你知道吗

以下是我的部分代码:

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

第一行的G来自:

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

输出文件如下:

latitude 37.61549

longitude -122.38821  

startId 65365765

endId 65365766

startLat 37.708957

startLon -122.392803 

endLat 37.708785 

endLon -122.393012

这个例子就是我想表达的,因为结果中的道路不在旧金山,我如何在代码中识别它并删除它?你知道吗


Tags: 方法dfgetlocoxnodesvaluesedges
1条回答
网友
1楼 · 发布于 2024-09-28 01:26:54

你问了两个问题。首先,如何确定一对纬度lng坐标是否在城市边界内?第二,如何得到一个城市的边界框?下面是如何使用OSMnx(以及shapely,OSMnx是在shapely之上构建的):

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))

相关问题 更多 >

    热门问题