0

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 ;))

1 Answers1

1
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.

hancho
  • 1,345
  • 3
  • 19
  • 39
  • Thanks for your help! May you also be so kind to help me implement reading the latitudes & longitudes from a csv file? – Luis Paganini May 24 '18 at 21:43
  • There are enough examples on the Internet how to read a file. CSV in particular is one of the easiest file formats. – scai May 29 '18 at 12:27