Turfjs / turf

A modular geospatial engine written in JavaScript and TypeScript
https://turfjs.org/
MIT License
9.3k stars 941 forks source link

point-on-line results are distorted #344

Closed mourner closed 7 years ago

mourner commented 8 years ago

The point-on-line module doesn't return correct results. Take a look at the official example with a connection line added:

image

As you can see, the line isn't perpendicular, so the snapped point isn't actually the closest one. This happens because the lineIntersects routine that's used to calculate the intersection point between a line segment and a line perpendicular to it from the point does the calculation on latitude/longitude values as if they were cartesian coordinates, while they are not — at a local scale, the ratio between latitude and longitude deltas is approximately cos(latitude), not 1.

On higher latitudes, the distortion will be more pronounced. Here's how the same example looks on latitude ~78:

image

cc @morganherlocker @tmcw @tcql

DenisCarriere commented 7 years ago

@mourner Would refactoring @turf/point-on-line lineIntersects() with @turf/line-intersect solve this issue?

rowanwins commented 7 years ago

Gday @DenisCarriere & @mourner

It looks like replacing the lineIntersects function with @turf/line-intersect will address the issue. The screenshot below is taken at 80degs latitude.

Repalced.

Will tidy up the rejigged code and will put in a pull request, a couple of tests to fix up, but other than that it was a pretty straightforward replacement.

DenisCarriere commented 7 years ago

👍 Good catch, @turf/line-intersect does sound like it would fix this issue (with some "rejigging").

DenisCarriere commented 7 years ago

👍 Looks like this got fixed in https://github.com/Turfjs/turf/issues/689.

Added a test case for it: test/in/line-northern-latitude-#344.geojson

image