Skip to content

Commit

Permalink
feat: Merge pull request #15 from uditha-atukorala/master
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Update searchForMultiplePolygons() to return a GeoJSON FeatureCollection
  • Loading branch information
orangejulius committed Aug 19, 2016
2 parents cc250de + 23faa12 commit 6465017
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ PolygonLookup.prototype.searchForMultiplePolygons = function searchForMultiplePo
// keep track of matches to avoid extra expensive calculations if limit reached
var matchesFound = 0;

// return all matching polygons, up to the limit
return polygons.filter(function(polygon) {
// filter matching polygons, up to the limit
polygons = polygons.filter(function(polygon) {
// short circuit if limit reached
if (matchesFound >= limit) {
return false;
Expand All @@ -93,6 +93,12 @@ PolygonLookup.prototype.searchForMultiplePolygons = function searchForMultiplePo
}
return false;
});

// return all matching polygons as a GeoJSON FeatureCollection
return {
type : 'FeatureCollection',
features : polygons,
};
};

/**
Expand All @@ -103,9 +109,9 @@ PolygonLookup.prototype.searchForMultiplePolygons = function searchForMultiplePo
* @param {number} x The x-coordinate of the point.
* @param {number} y The y-coordinate of the point.
* @param {number} [limit] Number of results to return (-1 to return all the results).
* @return {undefined|object|array} If one or more bounding box intersections are
* @return {undefined|object} If one or more bounding box intersections are
* found and limit is undefined, return the first polygon that intersects (`x`, `y`); otherwise,
* `undefined`. If a limit is passed in, return intercecting polygons as an array.
* `undefined`. If a limit is passed in, return intercecting polygons as a GeoJSON FeatureCollection.
*/
PolygonLookup.prototype.search = function search( x, y, limit ){
if (limit === undefined) {
Expand Down
15 changes: 9 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,26 +190,29 @@ tape( 'PolygonLookup.search() respects limit argument.', function ( test ){
var point = [3, 3];
var result = lookup.search(point[0], point[1], 1);

test.equal(result.length, 1, 'array with one element returned');
test.equal(result[0].properties.id, 1, 'first polygon returned');
test.equal(result.type, 'FeatureCollection', 'feature collection returned');
test.equal(result.features.length, 1, 'feature collection with one feature returned');
test.equal(result.features[0].properties.id, 1, 'first polygon returned');
t.end();
});

test.test('point inside polygon 1 and 3 with limit -1 returns array with polygons 1 and 3', function(t) {
var point = [3, 3];
var result = lookup.search(point[0], point[1], -1);

test.equal(result.length, 2, 'array with two elements returned');
test.equal(result[0].properties.id, 1, 'first polygon returned');
test.equal(result[1].properties.id, 3, 'third polygon returned');
test.equal(result.type, 'FeatureCollection', 'feature collection returned');
test.equal(result.features.length, 2, 'feature collection with two features returned');
test.equal(result.features[0].properties.id, 1, 'first polygon returned');
test.equal(result.features[1].properties.id, 3, 'third polygon returned');
t.end();
});

test.test('point inside no polygons with limit -1 returns empty array', function(t) {
var point = [10, 10];
var result = lookup.search(point[0], point[1], -1);

test.equal(result.length, 0, 'empty array returned');
test.equal(result.type, 'FeatureCollection', 'feature collection returned');
test.equal(result.features.length, 0, 'feature collection with no features returned');
t.end();
});

Expand Down

0 comments on commit 6465017

Please sign in to comment.