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

turf.nearest for linestrings #198

Closed
peterqliu opened this issue Jan 17, 2015 · 9 comments
Closed

turf.nearest for linestrings #198

peterqliu opened this issue Jan 17, 2015 · 9 comments

Comments

@peterqliu
Copy link

For example, when I want to find the closest gas station to the stretch of freeway where I'll likely be low on gas.

This is better than doing turf.nearest on a single point, for two reasons:

  • Turf.nearest on a linestring is likely to return nearer results than on a point, because it's a larger search area.
  • when looking for gas stations, I want to find one that's not too far out of my way, and I'm willing to go a couple more exits before turning off if that one is more convenient. I don't really care where the station is, so I don't have a specific point in mind to search around.
@nevi-me
Copy link

nevi-me commented Jan 24, 2015

The challenge with nearest for linestrings is that a linestring could have only 2 points, making it difficult to run nearest through each sets of coordinates in that point. I solved this problem by taking linestring and 'complexifying' it by adding points on the linestring within a certain distance of each other. Then I can search for nearest in that list of coordinates on the linestring.

I did this in rwt-to/GeoJSON-Tools (MovingGauteng/GeoJSON-Tools@35abcf0), but I'm not promoting that you use the tool. I just saw Turfjs today, and if someone would like to port the 'complexify' method from GeoJSON-Tools, you're welcome to.
We use GeoJSON-Tools internally, and there's lots of work to do, so I haven't committed anything to it in some time now.

@rowanwins
Copy link
Member

Gday @peterqliu

I've had a shot at creating this module, basically you input a collection of points and a linestring, and it returns the nearest point as well as the distance that point is from the line, this is included as a property on the feature. The user can also specify the distance units as a parameter (as per the existing turf distance module) which defaults to kilometers.

It was actually quite simple to do as it builds on a number of existing turf modules, namely point-on-line and distance.

cc @tcql & @morganherlocker . I still need to write some tests but if you're interested in transferring ownership at some stage let me know.

Cheers
Rowan

@peterqliu
Copy link
Author

@rowanwins thank you so much for jumping in here. hoping to see this in the not-too-distance turf future.

@stebogit
Copy link
Collaborator

@rowanwins @DenisCarriere it seems to me that @turf/point-on-line is exactly what this issue refers to, isn't it?
However, wouldn't be nice, for next major release, to improve @turf/nearest so that it could consistently handle all features? We could then drop @turf/point-on-line, which would be redundant.

Thoughts?

@rowanwins
Copy link
Member

Mmm not quite @stebogit . point-on-line only takes a point and a line and works out the nearest point on the line. The idea of this was to pass in a collection of points and work out which point was closest to the line. My first pass module was basically a wrapper to do the iterating through the collection.

It would be neat to bulk out nearest to support other scenarios, even if we did this point-on-line might still have it's place.

@stebogit
Copy link
Collaborator

@rowanwins 👍 of course! Now I got the difference 😄
I'll work on that then.

@morganherlocker
Copy link
Member

How would the case of a parallel line be handled? There is no single closest point.

@rowanwins
Copy link
Member

If in doubt @morganherlocker the answer is c - we'll just place something artibitralily 😜!

@stebogit
Copy link
Collaborator

Implemented with #939

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

5 participants