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

simplifyGeometry() simplifies too much for tolerance=0 #2

Open
Wilkins opened this issue Jan 29, 2016 · 1 comment
Open

simplifyGeometry() simplifies too much for tolerance=0 #2

Wilkins opened this issue Jan 29, 2016 · 1 comment

Comments

@Wilkins
Copy link

Wilkins commented Jan 29, 2016

I'm working on a project that use your simplifyGeometry function a lot (https://github.com/Wilkins/gpx-simplify-optimizer).

And I found that sometimes with a tolerance = 0 it simplifies more than it should.

I made a little snippet to reproduce my problem :

var simplifyGeometry = require("./index");

var case1 = [
    [-60.501,47.001,0],
    [-60.323,46.867,0],
    [-60.317,46.861,0], // This value get stripped
    [-60.311,46.855,0],
    [-60.305,46.834,0],
    [-60.606,46.21,0],
    [-60.606,46.21,0],
    [-60.605,46.211,0],
    [-60.6,46.201,0],
    [-60.736,46.057,0],
    [-60.501,47.001,0]
];

// Same as case 1 but narrowed
var case2 = [
    [-60.323,46.867,0],
    [-60.317,46.861,0], // This value get stripped
    [-60.311,46.855,0],
];

var case3 = [
    [45.759,-63.257],
    [45.754,-63.257], // This value get stripped
    [45.743,-63.257]
];

var case4 = [ 
    [45.294,-61.007],
    [45.294,-61.017], // This value get stripped
    [45.294,-61.027]
];


console.log(simplifyGeometry(case1, 0));
console.log(simplifyGeometry(case2, 0));
console.log(simplifyGeometry(case3, 0));
console.log(simplifyGeometry(case4, 0));

I guess this is a geometry reason, but I think it should not strip anything if tolerance=0.
Maybe a simple if (tolerance === 0) { return points; } would correct this particular case.

But maybe it's a more complex geometry problem that need to be solved for other cases.

@seabre
Copy link
Owner

seabre commented May 10, 2016

It's how the algorithm works. This person gives a pretty good explanation of how the tolerance parameter works: http://gis.stackexchange.com/a/11915

PostGIS's implementation is roughly the same as mine. Here's test case 1 as a PostGIS call with tolerance at zero:

SELECT ST_AsText(ST_Simplify(ST_GeomFromText('MULTIPOLYGON (((-60.501 47.001, -60.323 46.867, -60.317 46.861, -60.311 46.855, -60.305 46.834, -60.606 46.21, -60.606 46.21, -60.605 46.211, -60.6 46.201, -60.736 46.057, -60.501 47.001)))'), 0));

And the output:

MULTIPOLYGON(((-60.501 47.001,-60.323 46.867,-60.311 46.855,-60.305 46.834,-60.606 46.21,-60.605 46.211,-60.6 46.201,-60.736 46.057,-60.501 47.001)))

Also test case 4 for good measure:

SELECT ST_AsText(ST_Simplify(ST_GeomFromText('LINESTRING (45.294 -61.007, 45.294 -61.017, 45.294 -61.027)'), 0));

And the output:

LINESTRING(45.294 -61.007,45.294 -61.027)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants