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

geoQuantize should not repeat identical coordinates in LineStrings #141

Merged
merged 1 commit into from
Jan 31, 2019
Merged
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
21 changes: 19 additions & 2 deletions src/quantize.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,24 @@ export default function(input, digits) {
return input.map(quantizePoint);
}

function quantizePointsNoDuplicates(input) {
var point0 = quantizePoint(input[0]);
var output = [point0];
for (var i = 1; i < input.length; i++) {
var point = quantizePoint(input[i]);
if (point.length > 2 || point[0] != point0[0] || point[1] != point0[1]) {
output.push(point);
point0 = point;
}
}
if (output.length === 1 && input.length > 1) {
output.push(quantizePoint(input[input.length - 1]));
}
return output;
}

function quantizePolygon(input) {
return input.map(quantizePoints);
return input.map(quantizePointsNoDuplicates);
}

function quantizeGeometry(input) {
Expand All @@ -23,7 +39,8 @@ export default function(input, digits) {
switch (input.type) {
case "GeometryCollection": output = {type: "GeometryCollection", geometries: input.geometries.map(quantizeGeometry)}; break;
case "Point": output = {type: "Point", coordinates: quantizePoint(input.coordinates)}; break;
case "MultiPoint": case "LineString": output = {type: input.type, coordinates: quantizePoints(input.coordinates)}; break;
case "MultiPoint": output = {type: input.type, coordinates: quantizePoints(input.coordinates)}; break;
case "LineString": output = {type: input.type, coordinates: quantizePointsNoDuplicates(input.coordinates)}; break;
case "MultiLineString": case "Polygon": output = {type: input.type, coordinates: quantizePolygon(input.coordinates)}; break;
case "MultiPolygon": output = {type: "MultiPolygon", coordinates: input.coordinates.map(quantizePolygon)}; break;
default: return input;
Expand Down
22 changes: 22 additions & 0 deletions test/quantize-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@ tape("quantize(LineString) quantizes coordinates", function(test) {
test.end();
});

tape("quantize(LineString) does not repeat coordinates", function(test) {
test.deepEqual(d3.geoQuantize({
type: "LineString",
coordinates: [[0, 0], [0.3, 0.3], [1, 1]]
}, 0), {
type: "LineString",
coordinates: [[0, 0], [1, 1]]
});
test.end();
});

tape("quantize() does not return an invalid LineString", function(test) {
test.deepEqual(d3.geoQuantize({
type: "LineString",
coordinates: [[0, 0], [0.3, 0.3]]
}, 0), {
type: "LineString",
coordinates: [[0, 0], [0, 0]]
});
test.end();
});

tape("quantize(MultiLineString) quantizes coordinates", function(test) {
test.deepEqual(d3.geoQuantize({
type: "MultiLineString",
Expand Down