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

Distance and Ramer–Douglas–Peucker functions. #791

Merged
merged 3 commits into from
Mar 21, 2018
Merged

Conversation

manthey
Copy link
Contributor

@manthey manthey commented Mar 6, 2018

Move distance functions from the lineFeature to the util module, making the function names more explicit. Add a rdpLineSimply function for reducing the complexity of a line or polygon. Add a rdpSimplifyData method to the line and polygon features for doing this while setting data.

manthey added 2 commits March 15, 2018 08:38
Move distance functions from the lineFeature to the util module, making
the function names more explicit.  Add a `rdpLineSimply` function for
reducing the complexity of a line or polygon.  Add a `rdpSimplifyData`
method to the line and polygon features for doing this while setting
data.
@manthey manthey force-pushed the distance-functions branch from c4a53de to c30916a Compare March 15, 2018 12:38
*
* @param {array} data A new data array.
* @param {number} [tolerance] The maximum variation allowed in map.gcs
* units. A value of zero will only remove perfectly colinear points. If
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering if the unit should be pixels for the tolerance?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It defaults to 0.5 pixel at the current zoom. But if you are overriding this, you might not want the tolerance to be dependent on the current scale of the map. So this can be made into current zoom pixels via map.unitsPerPixel(map.zoom()) * <tolerance in pixels>, or it can be in gcs units.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so the user has to pass t = map.unitsPerPixel(map.zoom()) * or else we will use map.unitsPerPixel(map.zoom()) * 0.5?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. As other examples, perhaps you want to zoom in to level 16 and control the tolerance to 0.25 pixels visible there, in which case you would use map.unitsPerPixel(16) * 0.25, or perhaps you want limit the tolerance to some 50 meters, in which case this is just 50. By not forcing it to be in current pixels, it gives flexibility.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I am wondering if we could auto compute it given some initial parameter in a separate PR (as a new feature), not needed now, but wanted to mention it.

* @param {geo.geoPosition} pt2 The second point.
* @returns {number} The distance squared.
*/
distance2dSquared: function (pt1, pt2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just have distance2d? It may be worth using turfjs? (https://turfjs.org/)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we return the non-squared versions, there is a lot of needless sqrt function calls, since mostly we are trying to find the smallest value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

turfjs internally does everything on a spheroid. Their distance function, for instance, is much heavier because of this, always making multiple trigonometric function calls. Since we are usually already in a 2d spatial domain, this is vastly slower (and doesn't extend to pixel-space examples in a rational manner, or ellipsoids that are highly eccentric, etc.).

Copy link
Member

@aashish24 aashish24 Mar 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, perhaps in the future we could consider it if we need more of these functions.

* @param {geo.geoPosition} line2 The other end of the line.
* @returns {number} The distance squared.
*/
distance2dToLineSquared: function (pt, line1, line2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here I think distance2dToLine is probably more useful?


// use pixel space for ease of picking tolerance values in tests
map.gcs('+proj=longlat +axis=enu');
map.ingcs('+proj=longlat +axis=esu');
Copy link
Member

@aashish24 aashish24 Mar 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice that we are using different gcs here

Copy link
Member

@aashish24 aashish24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly looks good to, few points / comments only.

@manthey manthey merged commit 3b4070c into master Mar 21, 2018
@manthey manthey deleted the distance-functions branch March 21, 2018 18:48
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

Successfully merging this pull request may close these issues.

2 participants