php-geospatial / geospatial

PHP Extension to handle common geospatial functions.
Other
60 stars 18 forks source link

Add Spherical Law Of Cosines method #15

Open vanderlee opened 8 years ago

vanderlee commented 8 years ago

Spherical Law Of Cosines is commonly used instead of Haversine due to better performance and very close results. Generally, if you need higher accuracy than SLOC, Haversine is probably too inaccurate as well.

See here for an explaination of the quality aspect: http://gis.stackexchange.com/questions/4906/why-is-law-of-cosines-more-preferable-than-haversine-when-calculating-distance-b

See here for documentation: https://en.wikipedia.org/wiki/Spherical_law_of_cosines

If you want, I have PHP code for it, but it's pretty trivial to implement yourself.

vanderlee commented 8 years ago

Is this project still active? If you need, I can provide some code for SLOC myself as I already have working PHP code, but compiling is a bit problematic.

derickr commented 7 years ago

Sorry, I never saw any notifications about this. I use this extension daily, but it hasn't gotten much attention as it "just works". If you can provide the formula, we can see whether we can add this of course!

vanderlee commented 7 years ago

Hi,

This is my SLOC code (using radians): Note that SLOC is only correct when not crossing the 180 meridian.

function cosineRad($latFrom, $lngFrom, $latTo, $lngTo) {
    return acos((sin($latFrom) * sin($latTo)) + (cos($latFrom) * cos($latTo) * cos($lngTo - $lngFrom))) * EARTH_RADIUS;    
}

I don't remember if you already had the equirectangular (see http://www.movable-type.co.uk/scripts/latlong.html) algorithm, but here it is also:

function cosineRad($latFrom, $lngFrom, $latTo, $lngTo) {
    return acos((sin($latFrom) * sin($latTo)) + (cos($latFrom) * cos($latTo) * cos($lngTo - $lngFrom))) * EARTH_RADIUS;    
}

EARTH_RADIUS is the radius in. I used 6371 for testing with kilometers.