2017-02-23 76 views
0

我有此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或更多的经纬度,你如何迭代呢?

任何帮助将是非常有帮助:)

+0

420433184从何而来? – Goyo

+0

@Goyo,对不起。我以为它被粘贴了,我已经在上面的文件中更新了。 – NMS

+0

这不是有效的JSON。 – Goyo

回答

2

您可以映射在纬度/多头列表中选择此功能让所有的ID的列表继承人的更新代码的工作原理是考虑多边形VS点:

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 
+0

该代码给出类型错误,如下所示: > TypeError:字符串索引必须是整数。 – NMS

+0

我尝试了一些工作,围绕在你的代码: '如果([ '12.0536248','51 0.6494046' ]数据[ “功能”] [1] [ “几何”] [ “坐标”): 打印(“坐标在那里”) else: print(“The coordinates not working”) 但if条件失败。你能建议为什么吗? – NMS

+0

那是因为你更新了json,但是快速提问你的json中最后一个'geometry'键是否是1d数组? “几何”键的值是不一致的,它是这样的吗? –