craue / CraueGeoBundle

Doctrine functions for calculating geographical distances in your Symfony project.
MIT License
124 stars 15 forks source link

incorrect distance calculation? #19

Closed yivi closed 3 years ago

yivi commented 3 years ago

Can't figure this one out. I apologize in advance when it turns out I was being a moron. :(

For two given points:

The generated distance query (using GEO_DISTANCE(40.3518,-3.5362,40.5596,-3.6261)) is:

SELECT 12742 * ASIN(SQRT(
            POWER(SIN((CAST(40.4256 AS NUMERIC) - CAST(40.5596 AS NUMERIC)) * PI() / 360), 2) +
            COS(40.4256 * PI() / 180) * COS(40.5596 * PI() / 180) *
            POWER(SIN((CAST(-3.5319 AS NUMERIC) - CAST(-3.6261 AS NUMERIC)) * PI() / 360), 2))) AS sclr_0

Output for the above is 16.895781163873654

But actually the distance for these two points is ~24.3: image

image

Caught this because the 7km difference is enough to show incorrect results in the query I'm working on.

Find hard to believe this is actually a bug on the package, yet I cannot see what else is going on here.

yivi commented 3 years ago

As an additional data-point, tried installing https://www.postgresql.org/docs/12/earthdistance.html, and got 24353.442403726403 when running:

SELECT earth_distance(ll_to_earth(40.5596,-3.6261), ll_to_earth(40.3518,-3.5362));
craue commented 3 years ago

Putting these locations in a test (like here) I get a distance of about 24.326 km, so this seems perfectly fine.

yivi commented 3 years ago

I'm sorry, it's obviously something on my end. I'll try to figure it out later today.

My apologies, and thanks.