To get a unique merged bounding box (bbox) from multiple relations returned by your Overpass QL query, you need to:
The Overpass API returns each relation with a bounds field if you use out bb;. The bounds field contains the min/max latitude and longitude for that relation.
Example of a relation with a bbox:
json{ "type": "relation", "id": 12345, "bounds": { "minlat": 48.8566, "minlon": 2.3522, "maxlat": 48.8606, "maxlon": 2.3582 } }
To merge all bboxes into one, you need to find the minimum and maximum latitude and longitude across all relations.
minlat among all relations.minlon among all relations.maxlat among all relations.maxlon among all relations.If you want to do this programmatically (e.g., in Python):
pythonimport json # Example Overpass response (replace with your actual response) response = { "elements": [ { "type": "relation", "id": 1, "bounds": {"minlat": 48.8566, "minlon": 2.3522, "maxlat": 48.8606, "maxlon": 2.3582} }, { "type": "relation", "id": 2, "bounds": {"minlat": 48.8556, "minlon": 2.3512, "maxlat": 48.8616, "maxlon": 2.3592} } ] } # Initialize variables minlat = minlon = float('inf') maxlat = maxlon = float('-inf') # Iterate over all relations and update the merged bbox for element in response["elements"]: if "bounds" in element: minlat = min(minlat, element["bounds"]["minlat"]) minlon = min(minlon, element["bounds"]["minlon"]) maxlat = max(maxlat, element["bounds"]["maxlat"]) maxlon = max(maxlon, element["bounds"]["maxlon"]) # Resulting merged bbox merged_bbox = (minlat, minlon, maxlat, maxlon) print(merged_bbox)
The output will be a tuple like:
(48.8556, 2.3512, 48.8616, 2.3592)
This is your merged bbox.
If you want to do this directly in Overpass QL, you can use the union statement and then request the bbox of the union:
ql[out:json][timeout:25]; {{geocodeArea:Marseille}}->.searchArea; ( relation["route"="subway"](area.searchArea); ); out bb; >; out bb;
This will return a single bbox for the union of all relations and their members.
Summary: