query-geo-boundary
v1.0.0
Published
Query the boundary of an administrative area from OpenStreetMap Nominatim or Overpass as GeoJSON
Maintainers
Readme
QueryGeoBoundary
A Node.js library to query the geo (administrative area) boundary from OSM nominatim and/or overpass.
Usage
import { queryGeoBoundary } from 'query-geo-boundary';
// CommonJS: const { queryGeoBoundary } = require('query-geo-boundary');
const places = await queryGeoBoundary('Zhucheng, Shandong', { userAgent: 'my-app/1.0 ([email protected])' });
for (const place of places) {
console.log(place.display_name, place.geojson);
}The package ships ESM and CommonJS builds with TypeScript declarations. queryGeoBoundary is also the default export; the option and result types are exported as QueryGeoBoundaryOptions and GeoBoundaryPlace.
API
Prototype: queryGeoBoundary(geoName, options)
geoName: name of the geo you want to obtain its boundaryoptions: optional.source:'nominatim'(default, recommended) queries Nominatim once withpolygon_geojson.'overpass'looks the place up on Nominatim, then fetches all matching boundaries from Overpass in onerel(id:...)query. Public Overpass instances are often at capacity: on HTTP 429/5xx, a network error or a 90 s timeout, the next endpoint is tried, and after a failed round over all of them the lookup waits 15 s, then 30 s, before trying again.overpassUrl: Overpass endpoint or endpoints to try in order. Defaults to overpass-api.de, overpass.private.coffee and maps.mail.ru. Point it at your own instance for heavy use, as the public instances ask.userAgent: the User-Agent sent to both services. Defaults toquery-geo-boundary/<version> (+https://github.com/tibetty/QueryGeoBoundary). The Nominatim usage policy asks applications to identify themselves, so set this to your app's name and contact.
Return: a promise of the matching OSM relations (GeoBoundaryPlace[]), as Nominatim describes them, with the boundary under geojson. With the nominatim source, geojson is Nominatim's geometry; with overpass, it is the osm2geojson-lite conversion of the relation. A typical result:
[{
"place_id": 220758967,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright",
"osm_type": "relation",
"osm_id": 3468769,
"lat": "35.9957253",
"lon": "119.4041185",
"class": "boundary",
"type": "administrative",
"display_name": "诸城市, 潍坊市, 山东省, 262200, 中国",
"boundingbox": ["35.7163774", "36.2834811", "118.9952393", "119.7232973"],
"geojson": {"type": "Polygon", "coordinates": [[[118.9952393, 36.0120809], "...", [118.9952393, 36.0120809]]]}
}]Errors: the promise rejects with an Error. HTTP failures name the service and status in the message and set err.status (e.g. 429 when Overpass rate-limits you).
Notice: 'coz it's very common for an administrative uint has the same name with the others, if you want to get the boundary of exactly that place, please supply as much as possible hierarchical adminstrative information in name, e.g., using "Ikeda, Hokkaido" rather than "Ikeda", or you can solve this kind of ambiguity by introducing human interference.
Node.JS Requirement
- Node.js 22+
Dependency
- osm2geojson-lite - a regex and map accelerated xml parsing & geojson reconstruction utility, much faster than xmldom, osmtogeojson in combination to convert an OSM XML to geojson
