How to get outer border of 3 adjacent cities?

Hi, I’m looking for a solution to get the outer border of multiple adjacent cities. Would that be possible with Overpass Turbo?

Possibily, but keep it simple if you can: get the geometry and disolve it with a SIG (QGIS for instance).

1 Like

Here’s an example of getting the perimeter around the three adjacent administrative areas of Rome, Fiumicino, and Anguillara Sabazia.

[out:json][timeout:30];

// Get each boundary relation, recursing down to its member ways and storing them in a set.
relation(id:41485);
way(r)->.rome; // includes the inner ways around Vatican City
// way(r:"outer")->.rome; // alternative that excludes the ways around Vatican City
relation(id:41575);
way(r)->.fiumicino;
relation(id:41722);
way(r)->.anguillara_sabazia;

// Combine the ways into a single set.
(way.rome; way.fiumicino; way.anguillara_sabazia;)->.combo;

// Iteratively remove ways from the set that belong to two or more boundary relations.
// Subtract from the combo once for each combination of two sets.
(way.combo; - way.rome.fiumicino;)->.combo;
(way.combo; - way.rome.anguillara_sabazia;)->.combo;
(way.combo; - way.fiumicino.anguillara_sabazia;);

// Uncomment to output a single GeoJSON feature.
// make Feature ::geom = gcat(geom());

out geom;

You can uncomment the make Feature line to coalesce the output into a single geometry. This allows some map libraries such as Mapbox and MapLibre to render it without a seam or overlap between each way. You won’t be able to visualize the result in Overpass turbo, but you can see it in Ultra instead.

4 Likes

Here’s a solution using Postpass (or with your own local PostgreSQL database if you’ve imported OSM using the same schema). It gives you a GeoJSON polygon (not the perimeter).

SELECT 
   st_union(geom) as geom
FROM 
   postpass_polygon
WHERE 
   osm_type='R' 
AND
   osm_id in (41485, 41575, 41722)

You can see that in action here on Overpass Turbo, or you can run it from the command line as

curl -o output.geojson -g https://postpass.geofabrik.de/api/0.2/interpreter \
--data-urlencode "data=
SELECT st_union(geom) as geom 
FROM postpass_polygon 
WHERE osm_type='R' AND osm_id in (41485, 41575, 41722)"

Add st_boundary(...) around your st_union to export the boundary as a set of two linear rings instead.

2 Likes