How to draw a polygon as an outline of a given geographical entity?


I’m interested in the feature of OSM where, if you search for a geographical entity, the found item will be nicely outlined with a polygon, and the zoom level of the map will be so that the polygon will fit into the viewport, no matter how big or small the entity is.

For example here’s what I mean concerning Uruguay: Relation: ‪Uruguay‬ (‪287072‬) | OpenStreetMap

In a web application I’m building at the moment—which contains a special experts database for connecting people doing research work about matters concerning the Spanish- and Portuguese-speaking world—, I’d like to have exactly this behavior. I already got some nice OSM-related things done, using Leaflet as the frontend Javascript library, however here I’m a bit stuck.

Our experts are able to choose geographical entities of various sizes from a controlled vocabulary—as we like to call it in humanities—as their areas of interest; for example, “Uruguay” as a whole country, or “Montevideo” as a city (in this case the capital of Uruguay), or “Treinta y Tres” as a department (in this case one of Uruguay’s). Each of these entities is associated with a latitude and longitude pair which, to my understanding, points to the geographical center of the respective entity.

Now, I figure that there is a way of getting the (possibly many) latitude-longitude pairs that are needed in order to achieve this, that is, to draw the polygon using Leaflet. Another map application, from the Czech Republic, apparently does it this way, inferring from how they build their URLs. I strongly believe that uses OSM under the hood.

Or is there another way to do it? Please give me a hint in which direction to carry on :blush:

With best regards from the Ibero-American world!

You can get the bounding box of an OSM feature from Nominatim.

1 Like

Thank you very much @Discostu36! You got me on the right track. Indeed it turned out to be a lot easier than I had imagined, thanks to Leaflet and Nominatim :grinning:

Draw country border on leaflet map using JSON coordinates
Auto zoom on multipolygon with leaflet