python - 解析JSON文件中的坐标值并提取其对应的属性值

标签 python json parsing openstreetmap

我在文件中有这个 JSON:

{
 "type": "FeatureCollection",
  "generator": "overpass-turbo",
  "copyright": "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.",
  "timestamp": "2017-02-23T15:12:02Z",
  "features": [
    {
      "type": "Feature",
      "id": "relation/2264913",
      "properties": {
        "@id": "relation/2264913",
        "admin_level": "10",
        "boundary": "administrative",
        "name": "ABC",
        "type": "boundary"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              12.0397449,
              51.6438084
            ],
            [
              12.0388557,
              51.6403582
            ],
            [
              12.0413471,
              51.640221
            ],
            [
              12.0405388,
              51.6377118
            ],
            [
              12.0423344,
              51.6374845
            ],
            [
              12.0419595,
              51.6343258
            ],
            [
              12.0804867,
              51.6295502
            ],
            [
              12.0698061,
              51.6438756
            ],
            [
              12.0681545,
              51.6437836
            ],
            [
              12.0658655,
              51.6436203
            ],
            [
              12.0632812,
              51.6438909
            ],
            [
              12.0627055,
              51.6442096
            ],
            [
              12.063009,
              51.6448326
            ],
            [
              12.0574821,
              51.6445929
            ],
            [
              12.0557545,
              51.6445538
            ],
            [
              12.0519454,
              51.6441561
            ],
            [
              12.0475772,
              51.6440126
            ],
            [
              12.0397449,
              51.6438084
            ]
          ]
        ]
      }
    },{
      "type": "Feature",
      "id": "relation/6249468",
      "properties": {
        "@id": "relation/6249468",
        "landuse": "farmland",
        "type": "multipolygon"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              12.0473976,
              51.6439141
            ],
            [
              12.0505144,
              51.6439299
            ],
            [
              12.0511152,
              51.6439698
            ],
            [
              12.0516503,
              51.6439931
            ],
            [
              12.0523275,
              51.6440464
            ],
            [
              12.0534621,
              51.6441396
            ],
            [
              12.053843,
              51.6441854
            ],
            [
              12.0543982,
              51.6442436
            ],
            [
              12.0545672,
              51.6442727
            ],
            [
              12.0553759,
              51.6444417
            ],
            [
              12.0554563,
              51.6444667
            ],
            [
              12.055522,
              51.6445033
            ],
            [
              12.0555596,
              51.6445424
            ],
            [
              12.0555851,
              51.6445981
            ],
            [
              12.0556065,
              51.6447213
            ],
            [
              12.0556333,
              51.6448386
            ],
            [
              12.0556588,
              51.6449244
            ],
            [
              12.0557084,
              51.6450334
            ],
            [
              12.055738,
              51.6451124
            ],
            [
              12.0558023,
              51.6453355
            ],
            [
              12.0558868,
              51.645531
            ],
            [
              12.0561852,
              51.645526
            ],
            [
              12.0565098,
              51.6455568
            ],
            [
              12.0570207,
              51.6456001
            ],
            [
              12.0572956,
              51.6456126
            ],
            [
              12.0574405,
              51.6456276
            ],
            [
              12.0575451,
              51.6456534
            ],
            [
              12.057643,
              51.6456966
            ],
            [
              12.057706,
              51.6457466
            ],
            [
              12.0577463,
              51.6458015
            ],
            [
              12.0577744,
              51.6458622
            ],
            [
              12.0577905,
              51.6459288
            ],
            [
              12.0577959,
              51.6459962
            ],
            [
              12.0577851,
              51.6460736
            ],
            [
              12.0577382,
              51.6461768
            ],
            [
              12.057352,
              51.6466353
            ],
            [
              12.0572889,
              51.6467169
            ],
            [
              12.0572313,
              51.6468076
            ],
            [
              12.056904,
              51.6475399
            ],
            [
              12.0566519,
              51.6475282
            ],
            [
              12.0563327,
              51.6474841
            ],
            [
              12.0559586,
              51.6473984
            ],
            [
              12.0556649,
              51.6473202
            ],
            [
              12.0555509,
              51.6472811
            ],
            [
              12.0554449,
              51.6472353
            ],
            [
              12.0553215,
              51.6471962
            ],
            [
              12.052414,
              51.6465338
            ],
            [
              12.0521485,
              51.6464814
            ],
            [
              12.0518749,
              51.6464473
            ],
            [
              12.0515061,
              51.6464065
            ],
            [
              12.0504748,
              51.6462933
            ],
            [
              12.0499759,
              51.6462484
            ],
            [
              12.0495588,
              51.6462309
            ],
            [
              12.049127,
              51.6462193
            ],
            [
              12.0486402,
              51.6461959
            ],
            [
              12.048506,
              51.646166
            ],
            [
              12.0485168,
              51.645898
            ],
            [
              12.0485597,
              51.6457915
            ],
            [
              12.0485396,
              51.6457349
            ],
            [
              12.0484712,
              51.645695
            ],
            [
              12.0484256,
              51.6456351
            ],
            [
              12.0484149,
              51.6453296
            ],
            [
              12.0483988,
              51.6450326
            ],
            [
              12.0483693,
              51.6450109
            ],
            [
              12.0482714,
              51.6450043
            ],
            [
              12.0482767,
              51.6456018
            ],
            [
              12.0483116,
              51.6465022
            ],
            [
              12.0483035,
              51.6465929
            ],
            [
              12.0482807,
              51.6466528
            ],
            [
              12.0482204,
              51.6466936
            ],
            [
              12.0481319,
              51.6467002
            ],
            [
              12.0479904,
              51.6466969
            ],
            [
              12.0472233,
              51.6466503
            ],
            [
              12.0469014,
              51.6465904
            ],
            [
              12.0457454,
              51.6462842
            ],
            [
              12.0473976,
              51.6439141
            ]
          ]
        ]
      }
    },{
      "type": "Feature",
      "id": "way/420433184",
      "properties": {
        "@id": "way/420433184",
        "@relations": [
          {
            "role": "admin_centre",
            "rel": "2264913",
            "reltags": {
              "admin_level": "10",
              "boundary": "administrative",
              "name": "ABC",
              "type": "boundary"
            }
          }
        ]
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          12.0622848,
          51.6370034
        ]
      }
    }
  ]
}

我已经使用 Python 加载了 JSON 文件,如下所示:

  import json
     with open('data.json') as data_file:    
     data = json.load(data_file)

我需要为每个坐标输入找到相应的“@id”属性。

示例:

    Latitude           Longitude          Element ID (need to found)

 1.  12.0534621          51.6441396             6249468    
 2.  12.0473976          51.6439141             6249468    
 3.  12.0622848          51.6370034             420433184   

。 。 。

如何获取对应经纬度的属性值?如果您有 100 行或更多行纬度和经度,您如何迭代执行此操作?

任何帮助都会非常有帮助:)

最佳答案

您可以将此函数映射到纬度/经度列表上,以获取所有 id 的列表,更新后的代码通过考虑多边形与点来工作:

def get_element_id(latitude, longtitude):
  "checks each object for lat/long returns objects id or -1 if not found"

  # some helper lambda functions
  check_coords = lambda arr: [latitude, longtitude] in arr
  get_curr_id = lambda obj: obj["properties"]["@id"].split("/")[1]

  # since features is an array of 2d arrays
  for obj in data["features"]:

    obj_type = obj["geometry"]["type"]
    curr_arr = obj["geometry"]["coordinates"]

    if(obj_type == "Polygon"):
      if(any(map(check_coords, curr_arr))):
        return get_curr_id(obj)

    if(obj_type == "Point"):
      if(check_coords([curr_arr]):
        return get_curr_id(obj)

  # lat/long not found
  return -1

print get_element_id(12.0397449, 51.6438084) # prints 2264913
print get_element_id(12.0658655, 51.6436203) # prints 2264913
print get_element_id(12.052414, 51.6465338)  # prints 6249468
print get_element_id(12.0622848, 51.6370034) # prints 420433184

关于python - 解析JSON文件中的坐标值并提取其对应的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42425431/

相关文章:

设置类变量的Python装饰器

python - 尝试创建 Kubernetes 作业时出错

python - 删除 Pandas 中的第 n 行

ios - Firebase 扇出数据以更新特定字段删除其他兄弟字段

java - 创建类似于java对象层次结构的树

c# - 解析 C# 代码(作为字符串)并插入其他方法

python - 解析财务报表中的负数

MySQL + JSON : Retrieve all data where id = term

javascript - 将 Backbone.js JSON 响应填充到嵌套集合/模型内的嵌套集合中

Java SimpleDateFormat 像 America/Los_Angeles 一样解析时区