简体   繁体   中英

Extract ids from OpenStreetMap Overpass API output (python list in dictionary)

I'm querying OpenStreetMaps' Overpass Turbo API ( Link ) with Python

import requests
import json

lat='51.404924'
lon='12.444895'

overpass_url = "http://overpass-api.de/api/interpreter"
overpass_query = "[out:json][timeout:600];(way(around:700,"+lat+","+lon+")[landuse=industrial];);out geom;"

response = requests.get(overpass_url, 
                        params={'data': overpass_query})

data = response.json()
for key in data:
      print("dict has", key)

elements=data["elements"]
print(type(elements))

and getting this result (shortened):

{
  "version": 0.6,
  "generator": "Overpass API 0.7.55 579b1eec",
  "elements": [
    {
      "type": "way",
      "id": 12805981,
      "geometry": [
        {
          "lat": 51.410593,
          "lon": 12.4327856
        },
      ],
      "tags": {
        "addr:housenumber": "1",
        "addr:street": "BMW-Allee",
        "landuse": "industrial",
        "name": "BMW Werk 07.10 Leipzig",
      }
    }
  ]
}

How can I get the name and id ? I've thought, that dictionaries can't contain lists , but here it does indeed. Because of this my initial idea

print(data["elements"]["tags"]["name"])

doesn't work.

Thanks for your help (this is my first question ;))

print(data["elements"][0]["tags"]["name"])
print(data['elements'][0]['id'])

Index into the elements array.

Dictionaries cannot have lists for keys, but can have lists for values.

You can use a tuple as a key in a dictionary. Basically anything immutable can be a key in a dict.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM