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

fix geoproject for polygons with holes #146

Closed
wants to merge 2 commits into from
Closed
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
25 changes: 21 additions & 4 deletions src/project/clockwise.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import {geoArea} from "d3-geo";
import {abs, tau} from "../math.js";

export default function(ring) {
if ((n = ring.length) < 4) return false;
var i = 0,
n,
area = ring[n - 1][1] * ring[0][0] - ring[n - 1][0] * ring[0][1];
while (++i < n) area += ring[i - 1][1] * ring[i][0] - ring[i - 1][0] * ring[i][1];
return area <= 0;
n,
a0 = ring[n - 1][0],
a1 = ring[n - 1][1],
b0 = ring[0][0],
b1 = ring[0][1];
if (abs(a0 - b0) >= 180)
return geoArea({ type: "Polygon", coordinates: [ring] }) < tau;
var area = a1 * b0 - a0 * b1;
while (++i < n) {
a0 = b0;
a1 = b1;
b0 = ring[i][0];
b1 = ring[i][1];
if (abs(a0 - b0) >= 180)
return geoArea({ type: "Polygon", coordinates: [ring] }) < tau;
area += a1 * b0 - a0 * b1;
}
return area >= 0;
}
4 changes: 3 additions & 1 deletion src/project/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {geoContains} from "d3-geo";

export default function(ring, point) {
var x = point[0],
y = point[1],
Expand All @@ -7,5 +9,5 @@ export default function(ring, point) {
pj = ring[j], xj = pj[0], yj = pj[1];
if (((yi > y) ^ (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi)) contains = !contains;
}
return contains;
return contains || geoContains({type:"Polygon", coordinates: [ring]}, point);
}
38 changes: 38 additions & 0 deletions test/project-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var tape = require("tape"),
d3 = Object.assign({}, require("d3-geo"), require("../"));

tape("project(polygon with hole) preserves the hole", function(test) {
var poly = {
type: "Polygon",
coordinates: [
[[0, 0], [0, 4], [4, 4], [4, 0], [0, 0]],
[[1, 1], [2, 1], [2, 2], [1, 2], [1, 1]]
]
};
test.deepEqual(d3.geoProject(poly, d3.geoIdentity()), poly);
test.end();
});

tape("project(polygon & hole on the antimeridian) preserves the polygon", function(test) {
var poly = {
type: "Polygon",
coordinates: [
[[178, 0], [-178, 2], [-178, -2], [178, 0]],
[[179, 0], [-179, -1], [-179, 1], [179, 0]]
]
};
test.deepEqual(d3.geoProject(poly, d3.geoIdentity()), poly);
test.end();
});

tape("project(polygon & hole at the pole) preserves the polygon", function(test) {
var poly = {
type: "Polygon",
coordinates: [
[[180, -85], [-90, -85], [0, -85], [90, -85], [180, -85]],
[[180, -89], [90, -89], [0, -89], [-90, -89], [180, -89]]
]
};
test.deepEqual(d3.geoProject(poly, d3.geoIdentity()), poly);
test.end();
});