Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include feature id in results where MultiPolygon #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ PolygonLookup.prototype.loadFeatureCollection = function loadFeatureCollection(
coordinates: childPolys[ ind ]
}
};
if ('id' in poly) {
childPoly.id = poly.id;
}
indexPolygon( childPoly );
}
break;
Expand Down
39 changes: 39 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ tape( 'PolygonLookup.search() respects limit argument.', function ( test ){
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');
test.equal(result.features[0].id, 1, 'feature id retained');
t.end();
});

Expand Down Expand Up @@ -266,16 +267,54 @@ tape( 'undefined geometries are handled gracefully', function(t) {
t.end();
});

tape( 'PolygonLookup.search() using MultiPolygon.', function ( test ){
var collection = {
type: 'FeatureCollection',
features: [
geojsonMultiPoly(
[ [ [ [ 2, 2 ], [ 6, 4 ], [ 4, 7 ] ] ], [ [ [ 3, 0 ], [ 7, 2 ], [ 4, 4 ] ] ] ],
{ id: 1 }
),
]
};

var lookup = new PolygonLookup( collection );

test.test('point inside polygon 1 with no limit specified returns polygon 1', function(t) {
var point = [3, 3];
var result = lookup.search(point[0], point[1]);
t.equal(result.properties.id, 1, 'first polygon returned');
t.equal(result.id, 1, 'id included');
t.end();
});
});

/**
* Convenience function for creating a GeoJSON polygon.
*/
function geojsonPoly( coords, props ){
return {
type: 'Feature',
id: props && props.id,
properties: props || {},
geometry: {
type: 'Polygon',
coordinates: coords
}
};
}

/**
* Convenience function for creating a GeoJSON multipolygon.
*/
function geojsonMultiPoly( coords, props ){
return {
type: 'Feature',
id: props && props.id,
properties: props || {},
geometry: {
type: 'MultiPolygon',
coordinates: coords
}
};
}