Skip to content

Commit

Permalink
Merge fd01347 into e6faafe
Browse files Browse the repository at this point in the history
  • Loading branch information
bhousel committed Apr 6, 2018
2 parents e6faafe + fd01347 commit c9d60fc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
18 changes: 11 additions & 7 deletions index.js
Expand Up @@ -12,11 +12,11 @@ function whichPolygon(data) {
var coords = feature.geometry.coordinates;

if (feature.geometry.type === 'Polygon') {
bboxes.push(treeItem(coords, feature.properties));
bboxes.push(treeItem(coords, feature.properties, feature.id));

} else if (feature.geometry.type === 'MultiPolygon') {
for (var j = 0; j < coords.length; j++) {
bboxes.push(treeItem(coords[j], feature.properties));
bboxes.push(treeItem(coords[j], feature.properties, feature.id));
}
}
}
Expand All @@ -34,9 +34,9 @@ function whichPolygon(data) {
for (var i = 0; i < result.length; i++) {
if (insidePolygon(result[i].coords, p)) {
if (multi)
output.push(result[i].props);
output.push(result[i]);
else
return result[i].props;
return result[i];
}
}
return multi && output.length ? output : null;
Expand All @@ -53,7 +53,7 @@ function whichPolygon(data) {
});
for (var i = 0; i < result.length; i++) {
if (polygonIntersectsBBox(result[i].coords, bbox)) {
output.push(result[i].props);
output.push(result[i]);
}
}
return output;
Expand Down Expand Up @@ -90,16 +90,20 @@ function rayIntersect(p, p1, p2) {
return ((p1[1] > p[1]) !== (p2[1] > p[1])) && (p[0] < (p2[0] - p1[0]) * (p[1] - p1[1]) / (p2[1] - p1[1]) + p1[0]);
}

function treeItem(coords, props) {
function treeItem(coords, props, id) {
var item = {
minX: Infinity,
minY: Infinity,
maxX: -Infinity,
maxY: -Infinity,
coords: coords,
props: props
properties: props
};

if (id !== undefined) {
item.id = id;
}

for (var i = 0; i < coords[0].length; i++) {
var p = coords[0][i];
item.minX = Math.min(item.minX, p[0]);
Expand Down
14 changes: 8 additions & 6 deletions test/test.js
Expand Up @@ -8,17 +8,17 @@ var data = require('./fixtures/states.json');
var query = whichPolygon(data);

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([-100, 45]).properties.name, "South Dakota");
t.equal(query([-90, 30]).properties.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].properties.name, "South Dakota");

var qq = query.bbox([-104.2, 44, -103, 45]);
var names = qq.map(function (el) { return el.name; }).sort();
var names = qq.map(function (el) { return el.properties.name; }).sort();
t.equal(qq.length, 2);
t.like(names, ["South Dakota", "Wyoming"]);
t.end();
Expand All @@ -27,8 +27,10 @@ test('queries polygons with a bbox', function (t) {
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([7.5, 7.5]).properties.name, "A", "without multi option");
var result = queryOverlapping([7.5, 7.5], true);
t.same([result[0].properties, result[1].properties], [{"name": "A"}, {"name": "B"}], "with multi option");
t.equal(queryOverlapping([-10, -10]), null);
t.end();
});

0 comments on commit c9d60fc

Please sign in to comment.