which-polygon
Advanced tools
Comparing version 2.1.0 to 2.2.0
24
index.js
@@ -26,13 +26,19 @@ 'use strict'; | ||
function query(p) { | ||
var result = tree.search({ | ||
minX: p[0], | ||
minY: p[1], | ||
maxX: p[0], | ||
maxY: p[1] | ||
}); | ||
function query(p, multi) { | ||
var output = [], | ||
result = tree.search({ | ||
minX: p[0], | ||
minY: p[1], | ||
maxX: p[0], | ||
maxY: p[1] | ||
}); | ||
for (var i = 0; i < result.length; i++) { | ||
if (insidePolygon(result[i].coords, p)) return result[i].props; | ||
if (insidePolygon(result[i].coords, p)) { | ||
if (multi) | ||
output.push(result[i].props); | ||
else | ||
return result[i].props; | ||
} | ||
} | ||
return null; | ||
return multi && output.length ? output : null; | ||
} | ||
@@ -39,0 +45,0 @@ |
{ | ||
"name": "which-polygon", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Index for matching points against a set of GeoJSON polygons", | ||
@@ -11,5 +11,5 @@ "main": "index.js", | ||
"devDependencies": { | ||
"eslint": "^3.6.0", | ||
"eslint": "^4.9.0", | ||
"eslint-config-mourner": "^2.0.1", | ||
"tap": "^7.1.2" | ||
"tap": "^10.7.2" | ||
}, | ||
@@ -16,0 +16,0 @@ "scripts": { |
@@ -1,2 +0,2 @@ | ||
A simple index for matching points against a set of GeoJSON polygons to find what polygon a point belongs to. | ||
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. | ||
@@ -20,4 +20,20 @@ | ||
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: | ||
```js | ||
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: | ||
```js | ||
var query = whichPolygon(geojson); | ||
var results = query.bbox([30.5, 50.5, 30.51, 50.51]); | ||
results[0].admin; // 'Ukraine' | ||
``` |
@@ -11,17 +11,25 @@ 'use strict'; | ||
test('queries polygons with a point', function (t) { | ||
t.equal(query([-100, 45]).name, "South Dakota"); | ||
t.equal(query([-90, 30]).name, "Louisiana"); | ||
t.equal(query([-50, 30]), null); | ||
t.end(); | ||
t.equal(query([-100, 45]).name, "South Dakota"); | ||
t.equal(query([-90, 30]).name, "Louisiana"); | ||
t.equal(query([-50, 30]), null); | ||
t.end(); | ||
}); | ||
test('queries polygons with a bbox', function (t) { | ||
t.equal(query.bbox([-100, 45, -99.5, 45.5])[0].name, "South Dakota"); | ||
t.equal(query.bbox([-100, 45, -99.5, 45.5])[0].name, "South Dakota"); | ||
var qq = query.bbox([-104.2, 44, -103, 45]); | ||
var names = qq.map(function (el) { return el.name; }).sort(); | ||
t.equal(qq.length, 2); | ||
t.like(names, ["South Dakota", "Wyoming"]); | ||
t.end(); | ||
var qq = query.bbox([-104.2, 44, -103, 45]); | ||
var names = qq.map(function (el) { return el.name; }).sort(); | ||
t.equal(qq.length, 2); | ||
t.like(names, ["South Dakota", "Wyoming"]); | ||
t.end(); | ||
}); | ||
test('queries overlapping polygons with a point', function (t) { | ||
var dataOverlapping = require('./fixtures/overlapping.json'); | ||
var queryOverlapping = whichPolygon(dataOverlapping); | ||
t.equal(queryOverlapping([7.5, 7.5]).name, "A", "without multi option"); | ||
t.same(queryOverlapping([7.5, 7.5], true), [{"name": "A"}, {"name": "B"}], "with multi option"); | ||
t.equal(queryOverlapping([-10, -10]), null); | ||
t.end(); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
134286
10
401
39