0

I am trying to print out the total distance in latitude between multiple points in php and printing out the result so I can then dump it into a mysql database. For example I have

 MULTILINESTRING ((166.282008076887 -50.4981757000558,166.282014047837 -50.4981149924515,166.282009048641 -50.4981449926728,166.282021047737 -50.498071992073,166.281791047443 -50.4979599921101,166.281661047662 -50.4978739926141,166.281637048376 -50.4978479925945))

I can't get past exploding it at , then again at a space so I have $points[0] and $point[1] but I am stuck beyond that. I know how to get the distance between two points but how do I go beyond that?

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
Jason G
  • 1
  • 1
  • 1
  • 1
    https://stackoverflow.com/a/10054282/2954326 – Tom Aug 19 '18 at 08:36
  • _"I know how to get the distance between two points"_ - where's the problem then? Just repeat that calculation for each point-pair. – Jeff Aug 19 '18 at 08:37

1 Answers1

0

You can split the string into Lat-Long pairs by splitting them around the comma delimiter.

    $str="166.282008076887 -50.4981757000558,166.282014047837 -50.4981149924515,166.282009048641 -50.4981449926728,166.282021047737 -50.498071992073,166.281791047443 -50.4979599921101,166.281661047662 -50.4978739926141,166.281637048376 -50.4978479925945";



$latLongArray=explode(",",$str);
echo "<pre>";
print_r($latLongArray);
echo "</pre>";

The above splits the string into space separated Lat/Long pairs as follows -

Array
(
    [0] => 166.282008076887 -50.4981757000558
    [1] => 166.282014047837 -50.4981149924515
    [2] => 166.282009048641 -50.4981449926728
    [3] => 166.282021047737 -50.498071992073
    [4] => 166.281791047443 -50.4979599921101
    [5] => 166.281661047662 -50.4978739926141
    [6] => 166.281637048376 -50.4978479925945
)

Next, you split them along the space, and save each lat/long array pair in a new array. You can use an associative array or json, the choice is yours.

$finalArray=array();
        for($i=0;$i<count($latLongArray);$i++){
            $tempStr=str_replace(" ",",",$latLongArray[$i]);
            $tempArr=explode(",",$tempStr);
            array_push($finalArray,$tempArr);
        }


echo "<pre>";
        print_r($finalArray);
        echo "</pre>";

This prints the Lat/Long pairs saved in the final array -

Array
(
    [0] => Array
        (
            [0] => 166.282008076887
            [1] => -50.4981757000558
        )

    [1] => Array
        (
            [0] => 166.282014047837
            [1] => -50.4981149924515
        )

    [2] => Array
        (
            [0] => 166.282009048641
            [1] => -50.4981449926728
        )

    [3] => Array
        (
            [0] => 166.282021047737
            [1] => -50.498071992073
        )

    [4] => Array
        (
            [0] => 166.281791047443
            [1] => -50.4979599921101
        )

    [5] => Array
        (
            [0] => 166.281661047662
            [1] => -50.4978739926141
        )

    [6] => Array
        (
            [0] => 166.281637048376
            [1] => -50.4978479925945
        )

)

Hope this helps.

Added later --

$totalDistance=0;
for($i=0;$i<count($finalArray);$i++){
$totalDistance+= calculateDistance($finalArray[$i],$finalArray[$i+1]);
}
echo $totalDistance;

function calculateDistance($pointA,$pointB){
// calculate distance between the two points
// ie, $pointA->Lat/Long, $pointB->Lat/Long
}
Venkat D
  • 127
  • 1
  • 10
  • that's exactly how far the OP already got: _"I can't get past exploding it at , then again at a space so I have..."_ – Jeff Aug 19 '18 at 11:18
  • Assuming the previous Lat/Log pair array, you mean to say you want to calculate distance between Array[0] Lat/Log pairs and Array[6] Lat/Long pairs? This can be achieved by setting Array[0] as the starting point and adding the distance between A[0], A[0]+A[1], ... all the way to A[6]? – Venkat D Aug 19 '18 at 12:44
  • |Array[0] as the starting point and adding the distance between A[0], A[0]+A[1], ... all the way to A[6]? it is not uniform though, there could be 6 or there could be 1000. – Jason G Aug 19 '18 at 18:21
  • Array ( [0] => 166.282008076887 -50.4981757000558 [1] => 166.282014047837 -50.4981149924515 [2] => 166.282009048641 -50.4981449926728 [3] => 166.282021047737 -50.498071992073 [4] => 166.281791047443 -50.4979599921101 [5] => 166.281661047662 -50.4978739926141 [6] => 166.281637048376 -50.4978479925945 ) and how do I get the distance from 1 - 2, 2-3, 3-4, 4-5, 5-6 as there could be 6 or 1000+ that is what I am asking – Jason G Aug 19 '18 at 18:24
  • I was assuming you know how to calculate the distance between two points - ie, A[0] and A[1]. Pass A[0] and A[1] to a method that calculates and returns the distance between the two points. Loop through the "points" array, increment it by one so that the next iteration gives you A[1], A[2] so on and so forth till you reach the end of the array. Doesn't matter whether your array has 6 points or 600 as you are iterating through the array values from A[0] till ... A[N]. – Venkat D Aug 21 '18 at 06:44