【发布时间】:2019-11-18 06:29:11
【问题描述】:
我有一个带有一组 GPS 纬度/经度坐标的数据框(称为 A)
Lat | Long
28.6752213, 77.09311140000001
我有另一个 CSV(有很多行,超过一百万 - 称之为 B)的形式
这基本上是一个网格,具有 4 个角的纬度/经度坐标。
问题
我需要找到 A 中的每一行,它在 B 中由哪个(非唯一)行界定。如中所示,gps 坐标位于框内,如 B 中的行所述。我有一个函数当给定来自 A 的坐标和 B 中的行时,返回 True/False。
现在我正在使用蛮力方法,遍历整个 B 数据框并检查每一行是否属于该框。但是,这非常低效且非常缓慢。
我确信必须有更好的方法来解决这个问题,因为这是一个常见问题。谁能指点我?
谢谢! :)
编辑:
用于查找特定 gps_coord 是否属于由行定义的框的函数的代码
import matplotlib.path as path
def find_if_point_in_bounding_box(row,gps_coords):
top_left_lat = row['top_left_lat']
top_left_long = row['top_left_long']
top_right_lat = row['top_right_lat']
top_right_long = row['top_right_long']
bottom_left_lat = row['bottom_left_lat']
bottom_left_long = row['bottom_left_long']
bottom_right_lat = row['bottom_right_lat']
bottom_right_long = row['bottom_right_long']
lat,long = gps_coords
# create box
p = path.Path([(top_left_lat, top_left_long),(top_right_lat,top_right_long),(bottom_left_lat,bottom_left_long),(bottom_right_lat,bottom_right_long)])
res = p.contains_points([(lat,long)])[0]
return res
【问题讨论】:
-
为什么会有冗余?框可以由左上角和右下角坐标定义。
top_right_lat和bottorm_right_lat总是相同的值,等等。 -
@VictorRuiz:这不是很有帮助。 GeoPandas 可以很好地处理这些问题。
-
您目前是如何处理逆经的?换句话说,您是否有任何跨越国际日期变更线的边界框(
*_left_lon > 0和*_right_lon < 0)? -
不知道 GeoPandas。但我确信使用数据库和查询比迭代每一行数据以搜索满足某些条件的数据(如果这种条件可以表示为查询)更好。但是您可以使用 GeoPandas 分享如何做到这一点
-
@VictorRuiz:请注意,Pandas 提供的索引就像数据库一样。这些不是要按顺序扫描的简单列表。