NMSD
NMSD

Reputation: 494

Parsing co-ordinate values in a JSON file and extract their corresponding attribute value

I have this JSON in a file:

{
 "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
        ]
      }
    }
  ]
}

I have loaded the JSON file using Python as shown below:

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

I need to find the corresponding "@id" attribute for each of the co-ordinates inputs.

Example:

    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   

. . .

How do I get the attribute values of the corresponding latitude and longitude? How do you do it iteratively if you have some 100 or more rows of latitude and longitude?

Any help would be really helpful :)

Upvotes: 1

Views: 3213

Answers (1)

Jonathan Portorreal
Jonathan Portorreal

Reputation: 2836

You could map this function over the list of lat/longs to get a list of all the id's heres the updated code works by considering polygon vs point:

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

Upvotes: 2

Related Questions