renosis
renosis

Reputation: 2722

PHP and GPS Coordinates, getting the total distance along paths made up of several points

I just can't seem to figure out how to make efficient and clean looking code for figuring this out.

I have a string of coordinates, each point is made up of Longitude, Latitude and Altitude naturally (I am not worried about altitude at all right now and I know the function I have for figuring out the footage does not support altitude):

$coordinates = "-82.36554110283872,26.15200821551467,0 -82.420692,26.097404,0 -82.52855700000001,26.186567,0 -82.41250599999999,25.996422,0 -82.50644510379755,26.05431354409091,0" 

I need to find out the distance in feet between each coordinate and add up the total. I have the following function(which works beautifully) to figure out the distance between two points:

function coordDistance($lat1, $lon1, $lat2, $lon2) {
$delta_lat = $lat2 - $lat1;
$delta_lon = $lon2 - $lon1;

$earth_radius = 20908800.00; //Distance around the earth in feet

$alpha  = $delta_lat/2;
$beta   = $delta_lon/2;
$a          = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($lat1)) *    cos(deg2rad($lat2)) * sin(deg2rad($beta)) * sin(deg2rad($beta)) ;
$c      = asin(min(1, sqrt($a)));
$distance = 2*$earth_radius * $c;
$distance = round($distance, 4);

return $distance;
}

The way I am currently breaking down my coordinates string and attempting to pass it to the function is completely ludicrous. Any suggestions on how to split the coordinates and break them down to get the total footage for the path in a nice way?

Upvotes: 2

Views: 1802

Answers (2)

JSuar
JSuar

Reputation: 21091

If I'm reading your input string correctly, first break up all the coordinates into an array using the explode function:

$coordinatesArray = explode(" ", $coordinates);

Now you can pass each coordinate as an array but you would need to update your function signature. For instance,

coordDistance($coordinatesArray[i], $coordinatesArray[i+1],); // in a loop 

Or, you could further breakdown the coordinates to work with your existing function signature.

$coordinateA = explode(",", $coordinatesArray[i]); // again, in a loop
$coordinateB = explode(",", $coordinatesArray[i+1]);

coordDistance($coordinateA[0], $coordinateB[0], $coordinateA[1], $coordinateB[1])

More here: http://php.net/manual/en/function.explode.php

Upvotes: 1

Louis-Philippe Huberdeau
Louis-Philippe Huberdeau

Reputation: 5431

Essentially a variable to accumulate into. Splitting the string on space to get individual point. Looping for n-1. Calculating the distance between i and i+1.

$total = 0;

$points = explode(' ', $coordinates);
$count = count($points);

for ($i = 0; $count - 1 > $i; ++$i) {
    list($lon1, $lat1, $alt1) = explode(',', $points[$i]);
    list($lon2, $lat2, $alt2) = explode(',', $points[$i + 1]);

    $total += coordDistance($lat1, $lon1, $lat2, $lon2);
}

I didn't quite test the code. Seems to me like your coordinate string has an extra space in it after a minus sign.

Make sure lon and lat are read in the right order too.

Upvotes: 1

Related Questions