Search code examples
pythonjsonoverpass-api

Extract parameter "maxspeed" from JSON generated with OverpassAPI in python


This is my JSON:

{
  "version": 0.6,
  "generator": "Overpass API 0.7.56.1004 6cd3eaec",
  "osm3s": {
    "timestamp_osm_base": "2020-05-01T10:56:02Z",
    "copyright": "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL."
  },
  "elements": [

{
  "type": "way",
  "id": 196511254,
  "nodes": [
    247532020,
    3433695944,
    7274513850,
    247527633
  ],
  "tags": {
    "access": "no",
    "access:conditional": "delivery @ (05:00-13:00)",
    "bicycle": "yes",
    "foot": "yes",
    "highway": "residential",
    "maxspeed": "30",
    "maxspeed:zone": "yes",
    "name": "De Keyserlei",
    "note": "access = no according to traffic sign",
    "psv": "yes",
    "surface": "asphalt",
    "wikidata": "Q2207937",
    "wikipedia": "nl:De Keyserlei"
  }
}

  ]
}

I managed to write code, that gets the parameter "elements" and then I can print it. But within "elements" there's also "tags" and after that then there's "maxspeed"

def get_speed_limit():
radius = 1
lat = 51.217473152637275
lon = 4.418572667839149

result = """https://overpass-api.de/api/interpreter?data=[out:json][timeout:25];way(around:"""+str(radius)+""","""+str(lat)+""","""+str(lon)+""")[maxspeed];out;"""
response = ox.requests.get(result)
data = response.json()
elements = data["elements"]
print(elements)

My question is how to extract the deeper parameter: "maxspeed"?


Solution

  • data["elements"][0]["tags"]["maxspeed"]
    

    (if there are more items than one in data["elements"] you'll need to iterate over them)