You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python比对bbox坐标与字典坐标值并返回对应key的实现方法

坐标范围匹配功能实现方案

核心逻辑说明

遍历存储坐标组的列表,逐组校验用户输入bbox的左下角、右上角是否落在当前坐标组的范围内,匹配成功则返回对应字典的key。

代码实现

示例输入参数

co_list = [
  {
    "data.01": [
      6.9490666,
      47.4897206,
      7.0073678,
      47.5169333
    ]
  },
  {
    "data.02": [
      6.9493157,
      47.4627392,
      7.0075872,
      47.4899521
    ]
  }
]

bb_list = [6.974532, 47.469739, 7.000004, 47.481432]

功能函数

def get_matched_keys(co_list, bb_list):
    # 解包用户输入的bbox坐标
    bx1, by1, bx2, by2 = bb_list
    matched_keys = []
    for co_item in co_list:
        # 取出单键字典的key和对应坐标组
        co_key, coords = next(iter(co_item.items()))
        cx1, cx2, cy1, cy2 = coords
        # 校验左下角/右上角是否落在当前坐标范围内
        check_bottom_left = (cx1 <= bx1 <= cx2) and (cy1 <= by1 <= cy2)
        check_top_right = (cx1 <= bx2 <= cx2) and (cy1 <= by2 <= cy2)
        if check_bottom_left or check_top_right:
            matched_keys.append(co_key)
            # 若只需返回第一个匹配到的key,取消下面注释即可
            # return co_key
    return matched_keys

调用测试

print(get_matched_keys(co_list, bb_list))
# 示例输出:['data.02']

注意事项

  • 上述代码默认坐标组的cx1 <= cx2、cy1 <= cy2,如果你的实际数据中存在坐标值倒序的情况,可以在解包坐标后加一步极值校准:cx1, cx2 = min(cx1, cx2), max(cx1, cx2),同理校准cy值,避免边界判断出错。
  • 函数默认返回所有匹配到的key组成的列表,若仅需第一个匹配结果,取消函数内注释的return语句即可。

内容的提问来源于stack exchange,提问作者beauvery

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 08:24:05