Know the direction between points by coordinate latitude and longitude with php

1

Next I have 3 cities that we will call point (A) (B) (C) all with latitude and longitude the topology looks more or less like this:

A = -3.3105, -60.1694

B = -3.3242, -60.6281

C = -3.5970, -61.3930
  

Directions (A) (B) < - Traveling Boat - > (C)

Note: Between point A and C I have a boat traveling, and I have latitude and longitude "coordinated" every 2 minutes of that boat:

Now comes the question that I want to know, if you have to know if it is going from point to point type like this:

(A) --> (B)

(C) --> (B) 

(B) --> (A)

I hope you have understood me, please, if anyone can help me, I really appreciate it!

    
asked by anonymous 04.05.2018 / 12:50

1 answer

3

[Update 2]

Final script (can still be improved):

// Detalhes
$n_pontoA = "Iranduba"; // Nome do ponto A
$n_pontoB = "Manaus";   // Nome do ponto B

// Coordenadas
$c1_barco = '-3.3105, -60.1694'; // Posição anterior do barco
$c2_barco = '-3.3137, -60.3477'; // Posição atual do barco
$c_pontoA = '-3.3242, -60.6281'; // Ponto A
$c_pontoB = '-3.5970, -61.3930'; // Ponto B

function quebra($coordenadas) {

    // Separa os valores
    $v = explode(",",$coordenadas);

    return $v;
}

// https://www.geodatasource.com/developers/php
function distance($lat1, $lon1, $lat2, $lon2, $unit) {

    $theta = $lon1 - $lon2;
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    $unit = strtoupper($unit);

    if ($unit == "K") {
        return ($miles * 1.609344);
    } else if ($unit == "N") {
        return ($miles * 0.8684);
    } else {
        return $miles;
    }
}

function calcula($c1Barco, $c2Barco, $cPontoA, $cPontoB) {

    // Quebra Latitude de Longitude
    $barcoC1 = quebra($c1Barco);
    $barcoC2 = quebra($c2Barco);
    $pontoA = quebra($cPontoA);
    $pontoB = quebra($cPontoB);

    // Calcula a distância entre os pontos
    $db1a = round(distance($barcoC1[0],$barcoC1[1],$pontoA[0],$pontoA[1],"K"),2);
    $db1b = round(distance($barcoC1[0],$barcoC1[1],$pontoB[0],$pontoB[1],"K"),2);
    $db2a = round(distance($barcoC2[0],$barcoC2[1],$pontoA[0],$pontoA[1],"K"),2);
    $db2b = round(distance($barcoC2[0],$barcoC2[1],$pontoB[0],$pontoB[1],"K"),2);

    // Tira a diferença da posição barco x pontos
    $dPA = $db1a - $db2a;
    $dPB = $db1b - $db2b;

    // Verifica a maior distância percorrida
    if ($dPA > $dPB)
        return "A";
    else
        return "B";
}

// Executa a função passando as variáveis
$resultado = calcula($c1_barco, $c2_barco, $c_pontoA, $c_pontoB);

if ($resultado == "A")
    echo "Destino: " . $n_pontoA;
else if ($resultado == "B")
    echo "Destino: " . $n_pontoB;
else
    echo "Erro!";

[Response 1]

First you need to have the distance formula well defined, as they can contain positive and / or negative values, but you can solve it in a simple way.

If you are going to calculate "average speed", it is a bit more complex than it looks, because since you have 3 points, instead of 1, you would have to do the calculation separately.

When you do this, you break the 2 values, do the calculations according to your definition, and then simply make the difference between them.

Example of functions:

// Coordenadas
$A = '-3.3105, -60.1694';
$B = '-3.3242, -60.6281';
$C = '-3.5970, -61.3930';

function distancia($ponto1, $ponto2) {

    // Separa os valores
    $p1 = explode(",",$ponto1);
    $p2 = explode(",",$ponto2);

    // Calcula a diferença
    $dLA = $p1[0] - $p2[0];
    $dLO = $p1[1] - $p2[1];

    echo '<br> LA: ' . $dLA . ' | LO: ' . $dLO;
}

distancia($A,$B);
distancia($B,$A);
distancia($A,$C);

Result:

LA: 0.0137 | LO: 0.4587
LA: -0.0137 | LO: -0.4587
LA: 0.2865 | LO: 1.2236

Function separating each coordinate to work with them outside of a function:

function distancia2($ponto1, $ponto2) {

    // Separa os valores
    $p1 = explode(",",$ponto1);
    $p2 = explode(",",$ponto2);

    // Calcula a diferença
    $d["LA"] = $p1[0] - $p2[0];
    $d["LO"] = $p1[1] - $p2[1];

    return $d;
}

$dA_B = distancia2($A,$B);
$dB_C = distancia2($B,$C);
$dA_C = distancia2($A,$C);

echo '<br>' . $dA_B["LA"];
echo '<br>' . $dB_C["LA"];
echo '<br>' . $dA_C["LA"];
echo '<br>' . $dA_B["LO"];
echo '<br>' . $dB_C["LO"];
echo '<br>' . $dA_C["LO"];
    
04.05.2018 / 13:09