Live roadworks | Open GovData QGIS

Rio Rzepka
Rio Rzepka

July 23, 2023

Live roadworks | Open GovData QGIS

This is a test of the "Autobahn-API", made public in 2021 after the launch of the official navigation app of the Die Autobahn GmBH des Bundes. The API documentation is available on: https://autobahn.api.bund.dev/

The service was used to create a QGIS map showing the live roadworks on mayor motorways in Germany. The line features (blue) were obtained from open data published by the German Federal Agency for Cartography and Geodesy (Bundesamt für Kartographie und Geodäsie, BKG) in 2021, and selected for only major motorways (42003 AX_Strassenachse, BasisDLM). 

import requests 
import json
from qgis.core import (
QgsProject,
QgsVectorLayer,
QgsField,
QgsFeature,
QgsGeometry,
)

layer_name = "API Import Layer"
crs = QgsProject.instance().crs()
layer = QgsVectorLayer("Point?crs={}".format(crs.authid()), layer_name, "memory")
layer.dataProvider().addAttributes([QgsField("id", QVariant.Int), QgsField("name", QVariant.String), QgsField("bemerkung", QVariant.String)])
layer.updateFields()

Autobahnen = []
api_url = "https://verkehr.autobahn.de/o/autobahn/"
response = requests.get(api_url)
api_resp = response.json()

for roads in api_resp["roads"]:
Autobahnen.append(roads)

for Autobahn in Autobahnen:
api_url = "https://verkehr.autobahn.de/o/autobahn/"+Autobahn+"/services/roadworks"
response = requests.get(api_url)
api_data = response.json()
coordinate_list = []

for roadwork in api_data["roadworks"]:
coord = roadwork["coordinate"]
lat = float(coord["lat"])
long = float(coord["long"])
tupel = (lat, long)
coordinate_list.append(tupel)

id_number = 1
for coordinate in coordinate_list:
x, y = coordinate
longitude = y
latitude = x
id = id_number
id_number += 1

geojson_obj = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [longitude, latitude] # Corrected coordinates (longitude, latitude)
},
"properties": {
"id": id,
"name": "Baustelle",
"bemerkung": Autobahn
}
}
]
}

json_str = json.dumps(geojson_obj)

geojson_dict = json.loads(json_str)

geometry = geojson_dict["features"][0]["geometry"]
properties = geojson_dict["features"][0]["properties"]

longitude, latitude = geometry["coordinates"]
point = QgsPointXY(longitude, latitude)

feature = QgsFeature()
feature.setGeometry(QgsGeometry.fromPointXY(point))
feature.setAttributes([properties["id"], properties["name"],         properties["bemerkung"]])

layer.dataProvider().addFeature(feature)

QgsProject.instance().addMapLayer(layer)

The python script for generating the red features indicating roadwork utilizes two requests: The first returns a list of all available motorways (A1-A995), the second provoces a JSON response with all details of roadwork and is looped over the first. The coordinates are retrieved from the JSON response and given to a QGIS Point feature class in each iteration. 

This makes for a simple example of live geo data extraction from a public RESTful API, that can be expanded upon - processing more attibutes, releasing in a web map, adding functionality for interactive use, etc.

Check out Alexander Reelsen's work for a more detailed project involving this service: Visualizing the unofficial autobahn API into the Elastic Stack



Tools used

PythonQGISREST APIs

Plug-ins used

JSONREST Framework

tags

APIopen dataroadworkstraffic

You might also like

Join the community!

We're a place where geospatial professionals showcase their works and discover opportunities.