mjaschen / phpgeo

Simple Yet Powerful Geo Library for PHP
https://phpgeo.marcusjaschen.de
MIT License
1.56k stars 195 forks source link

PerpendicularDistance reports 21k meters on a distance of about 192k #56

Closed acidjazz closed 4 years ago

acidjazz commented 4 years ago

If you look at this GeoJSON demonstration:

http://geojson.io/#id=gist:acidjazz/8cd03298ddf9bf42168a9ded77577dcf&map=9/29.9216/-94.4000

I have a line drawn near Lake Charles, and a point set near Kingwood, about 120 miles away.

The following code results in 21,989 meters for those coordinates:

    $perp = new PerpendicularDistance();
    $line = new Line(
        new Coordinate(29.888044473729,-93.580916446874),
        new Coordinate(29.890583508738,-93.552272621677),
    );

    $point = new Coordinate(29.941561, -95.118365);

    dd($perp->getPerpendicularDistance($point, $line));

This returns 21989.019190836

Now doing a point distance results in 148k-151k, ex:

    dump($line->getPoint1()->getDistance($point, new Haversine()));
    dd($line->getPoint2()->getDistance($point, new Haversine()));

this results in:

148298.123

151043.798

mjaschen commented 4 years ago

Hi,

the perpendicular distance is calculated between a point and a great circle which is defined by the line.

It's impossible to determine the perpendicular distance between the point A and the actual line (between the two defining points P1 and P2) in your example.

The reason: there's no way to construct a „right angle“ between A and any point on the line between P1 and P2. The calculated distance is A to X.

I hope the drawing helps to explain it:

pd

I'll update the documentation to clarify things.

acidjazz commented 4 years ago

@mjaschen very interesting, yes I guess I misunderstood, thanks for the clarification.

I am looking to calculate the distance between a polygon and a point, I thought look through its segments and find the closest one by using this, but now I see where this won't work.

Do you have plans at all to add something like what I'm looking for? It would also be really cool to know where in the segment/line you found to be closest to that point.

Let me know if you want me to close this, or I can close it after you update the documentation

mjaschen commented 4 years ago

I'd like to add this functionality and will create a separate issue for that.

The documentation is updated to clarify the purpose of the perpendicular distance calculation: https://phpgeo.marcusjaschen.de/Calculations/Perpendicular_Distance.html

mjaschen commented 4 years ago

I've just noticed that issue #24 covers this already. I'll take a another look.