Page 1 of 1

Calculo de distancias por GPS con funciones del MYSQL

PostPosted: Sat Jan 20, 2018 11:15 pm
by Busmatic_wpb
Muy estimados amigos.
Quiero compartir una rutina de calculo de distanacias con datos del GPS, pueda que le sirva alguien por ahi.. ami me trabaja de maravillas con mis sistemas.

function CalculaDistancias()

// esta vatiables son recogidas de la string GPRMC
Latitud1 := ALLTRIM(STR(Op:GPGGA_cLat))
Longitud1 :="-"+ALLTRIM(STR(Op:GPGGA_cLong))

// dos datos de prueba
Latitud2:="10.203574"
Longitud2:="-83.782274"

try

oP:GpSDistancia := oCon:oKon:Query("SELECT (acos(sin(radians('"+Latitud1+"')) * sin(radians('"+Latitud2+"')) + cos(radians('"+Latitud1+"')) * cos(radians('"+Latitud2+"')) *
cos(radians('"+Longitud1+"') - radians('"+Longitud2+"'))) * 6378) as distanciaPunto1Punto2")
CATCH oError
MsgStop( "No se ha podido calcular la distancia error en datos ")
RETURN
END

oP:GpSDistancia :REFRESH()
Op:GpSresultado := oP:GpSDistancia :FieldGet(1)

Alert(Op:GpSresultado )

Return

*********************************************************************************

Veamos la soluciónd e una manera más limpia:

Radio de la Tierra: 6378 km.

PUNTO 1 PUNTO 2

latitud LATITUD_1 LATITUD_2
longitud LONGITUD_1 LONGITUD_2


SELECT (acos(sin(radians(LATITUD_1)) * sin(radians(LATITUD_2)) +
cos(radians(LATITUD_1)) * cos(radians(LATITUD_2)) *
cos(radians(LONGITUD_1) - radians(LONGITUD_2))) * 6378) as
distanciaPunto1Punto2;

*******************************************************************************

Re: Calculo de distancias por GPS con funciones del MYSQL

PostPosted: Sun Jan 21, 2018 7:15 am
by Antonio Linares
Oscar,

Cómo creas Op ?

Re: Calculo de distancias por GPS con funciones del MYSQL

PostPosted: Sun Jan 21, 2018 6:05 pm
by Busmatic_wpb
Perdon de me fue.
Si Op: una clase que anda en el foro y se llama Public.prg con varios aportes incluyendo Aportes: [ER] Ray Islas.
Simplemente es crear variables las cuales puedes controlar su contenido ya hacerlas publicas mas ordenado y contraldo.

Trabaja muy bien ahora el que quiera un resultado en millas debe de cambiar Radio de la Tierra: 6378 km. por millas..
espero que les sirva.

Re: Calculo de distancias por GPS con funciones del MYSQL

PostPosted: Sun Jan 21, 2018 7:13 pm
by xmanuel
Está muy bien pero el que no trabaje con mysql tendría que enlazar alguna lib como Eagle1 o HDO para MySQL...
no sería más fácil usar la lib hbct.lib de Harbour?

Creo que el SELECT de MySQL se podría traducir por:
Code: Select all  Expand view

function miDistancia( lat1, lng1, lat2, lng2 )
resturn( acos( sin( dtor( lat1 ) ) * sin( dtor( lat2 ) ) + cos( dtor( lat1 ) ) * cos( dtor( lat2 ) ) * cos( dtor( lng1 ) - dtor( lng2 ) ) ) * 6371 )
 


Recordad que aqui at1, lng1, lat2, lng2 tienen que ser numericos y no pueden ir entrecomillados por lo que el ejemplo de Busmatic_wpb pasaria a ser:
Code: Select all  Expand view

// esta vatiables son recogidas de la string GPRMC  <--- Ya no debería ser string sino numeric
Latitud1 := Op:GPGGA_cLat
Longitud1 := Op:GPGGA_cLong

// dos datos de prueba
Latitud2 := 10,203574   // Ojo con la , o . decimal
Longitud2 := -83,782274


oP:GpSDistancia := miDistancia( Latitud1, Longitud1, Latitud2, Longitud2 )

oP:GpSDistancia :REFRESH()

Alert( str( Op:GpSresultado )  )

 


:oops:

Re: Calculo de distancias por GPS con funciones del MYSQL

PostPosted: Mon Jan 22, 2018 8:56 am
by nageswaragunupudi
If using MySql server version 5.7.1 or later, we can consider using MySql function
Code: Select all  Expand view

ST_Distance_Sphere( POINT( lat1, long1 ), POINT( lat2, long3 ) [, radius] )
 

which returns the distance in meters. If the 3rd parameter is ommitted, MySql uses Earth's radius by default.

Instead of storing lattitude and longitude separately in Table, we can use SPATIAL datatype POINT.

This is a sample that will be released with FWH 18.01;
Code: Select all  Expand view

/*
* mariageo.prg
* SPATIAL dataype POINT and Distance Calculations
*
* Requires MySql server 5.7.1 or above
*/


#include "fivewin.ch"

//----------------------------------------------------------------------------//

function Main()

   local oCn, oRs, cSql

   oCn   := FW_DemoDB( 1 ) // Provide connection to your MySql 5.7.1 server

TEXT INTO cSql
   CREATE TABLE citylatlong (
   id INT AUTO_INCREMENT PRIMARY KEY,
   city VARCHAR(20),
   pt POINT )
ENDTEXT

   if oCn:TableExists( "citylatlong" )
      oCn:DropTable( "citylatlong" )
   endif

   oCn:Execute( cSql )

TEXT INTO cSql
   INSERT INTO citylatlong
   ( city, pt ) VALUES
   ( "Hyd",    POINT( 78.4867, 17.3850 ) ),
   ( "Mumbai", POINT( 72.8777, 19.0760 ) ),
   ( "Delhi",  POINT( 77.1025, 20.7041 ) ),
   ( "London", POINT(  0.1278, 51.5074 ) ),
   ( "Paris",  POINT(  2.3522, 48.8566 ) ),
   ( "Madrid", POINT(  3.7038, 40.4168 ) )
ENDTEXT

   oCn:Execute( cSql )

   ocn:Execute( "DROP FUNCTION IF EXISTS distance_between" )
TEXT INTO cSql
   CREATE FUNCTION distance_between( city1 VARCHAR(20), city2 VARCHAR(20) )
   RETURNS DOUBLE
   BEGIN
   DECLARE p1 POINT;
   DECLARE p2 POINT;

   SELECT pt INTO p1 FROM citylatlong WHERE city = city1;
   SELECT pt INTO p2 FROM citylatlong WHERE city = city2;

   RETURN ST_Distance_Sphere( p1, p2 );
   END;
ENDTEXT
   oCn:Execute( cSql )

   oRs   := oCn:RowSet( "SELECT id, city, X( pt ) AS Longitude, Y( pt ) AS Lattitude from citylatlong" )
   XBROWSER oRs SETUP ( oBrw:aCols[ 3 ]:cEditPicture := "9999.999999", oBrw:aCols[ 4 ]:cEditPicture := "999.999999" )
   oRs:Close()

   ? oCn:distance_between( "London", "Paris" ) // --> 334575.4 meters

   oCn:Close()

return nil

//----------------------------------------------------------------------------//
 


Image

This also creates a stored function on the Server called "distance_between( city1, city2 )" and using FWMARIADB library we can call the function by

Code: Select all  Expand view

   ? oCn:distance_between( "Paris", "Madrid" )
 

as if it is built-in method.

Re: Calculo de distancias por GPS con funciones del MYSQL

PostPosted: Mon Jan 22, 2018 12:14 pm
by nageswaragunupudi
SELECT (acos (sin (radians (LATITUD_1)) * sin (radians (LATITUD_2)) +
cos (radians (LATITUD_1)) * cos (radians (LATITUD_2)) *
cos (radians ( LONGITUD_1) - radians (LONGITUDE_2))) * 6378) as
distancePoint1Point2;

Should Earth's radius be taken as 6371 instead of 6378 ?

Re: Calculo de distancias por GPS con funciones del MYSQL

PostPosted: Mon Jan 22, 2018 10:50 pm
by Busmatic_wpb
Compañeros
Gracias por compartir con nosotros su conocimiento, gracias

My friends
Thank you very much indeed as we learn from others, thank you very much for sharing.

Xmanuel gracias por compartir dime esta funcion es para Xharbour ?? o solo para Harbour. , hbct.lib

Re: Calculo de distancias por GPS con funciones del MYSQL

PostPosted: Thu Jan 25, 2018 1:18 am
by xmanuel
Perdona el retraso...
Está en los dos:
Harbour -> hbct.lib
xHarbour -> ct.lib