All Projects → mapbox → Which Polygon

mapbox / Which Polygon

Licence: isc
Index for matching points against a set of GeoJSON polygons

Programming Languages

javascript
184084 projects - #8 most used programming language

A simple index for matching points and bboxes against a set of GeoJSON polygons to find what polygon it belongs to. For example, determining the country of a location given a countries GeoJSON.

Build Status Coverage Status

Example usage

var geojson = require('./countries.json');
var query = whichPolygon(geojson);

query([30.5, 50.5]).admin; // 'Ukraine'

The input GeoJSON must be a feature collection of polygons or multipolygons. The query returns the properties of the matched polygon feature.

By default, the query returns properties of the first polygon that has been found (object or null). To return an array of all found polygon properties, use option multi as the second argument:

query([14.3, 51.2], true); // [{props1}, {props2}, {props3}, ...] || null

Once the index is built, queries are pretty fast — 17 seconds to query 1 million random locations on a Macbook Pro in this particular case.

You can also query all polygons that intersect a given bbox:

var query = whichPolygon(geojson);
var results = query.bbox([30.5, 50.5, 30.51, 50.51]);
results[0].admin; // 'Ukraine'
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].