which-polygon
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/which-polygon package

2.2.1 • Public • Published

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'

Package Sidebar

Install

npm i which-polygon

Weekly Downloads

22,312

Version

2.2.1

License

ISC

Unpacked Size

53.4 kB

Total Files

9

Last publish

Collaborators

  • mbx-npm-ci-production
  • mbx-npm-ci-staging
  • mbx-npm-advanced-actions-production
  • mbx-npm-advanced-actions-staging
  • mbx-npm-09-production
  • mbx-npm-08-production
  • mbx-npm-07-production
  • mbx-npm-06-production
  • mbx-npm-05-production
  • mbx-npm-04-production
  • mbx-npm-03-production
  • mbx-npm-02-production
  • mbx-npm-01-production
  • mbx-npm-02-staging
  • mapbox-npm-01
  • mapbox-npm-02
  • mapbox-npm-07
  • mapbox-npm-03
  • mapbox-npm-04
  • mapbox-npm-09
  • mapbox-npm-05
  • mapbox-npm-06
  • mapbox-npm-08
  • mapbox-npm-advanced-actions
  • mapbox-npm-ci
  • mapbox-npm
  • mapbox-admin
  • mapbox-machine-user