Browse Source

add some sql functions

master
Markus Bergholz 1 year ago
parent
commit
0b65a6f9a3
5 changed files with 52 additions and 0 deletions
  1. 4
    0
      mariadb/deg2rad.sql
  2. 16
    0
      mariadb/distance.sql
  3. 12
    0
      mariadb/gauss.sql
  4. 16
    0
      mariadb/get_bearing.sql
  5. 4
    0
      mariadb/rad2deg.sql

+ 4
- 0
mariadb/deg2rad.sql View File

@@ -0,0 +1,4 @@
CREATE OR REPLACE FUNCTION `deg2rad`(
deg DOUBLE
) RETURNS double
RETURN deg * pi()/ 180;

+ 16
- 0
mariadb/distance.sql View File

@@ -0,0 +1,16 @@
CREATE OR REPLACE FUNCTION `distance`(
lon1 DOUBLE,
lat1 DOUBLE,
lon2 DOUBLE,
lat2 DOUBLE
) RETURNS double
BEGIN
SET @earth_radius = 6371;
SET @dLat = deg2rad(lat2-lat1);
SET @dLon = deg2rad(lon2-lon1);
SET lat1 = deg2rad(lat1);
SET lat2 = deg2rad(lat2);
SET @a = SIN(@dLat/2) * SIN(@dLat/2) + SIN(@dLon/2) * SIN(@dLon/2) * COS(lat1) * COS(lat2);
SET @c = 2 * ATAN2(SQRT(@a), SQRT(1-@a));
RETURN @earth_radius * @c;
END;

+ 12
- 0
mariadb/gauss.sql View File

@@ -0,0 +1,12 @@
CREATE OR REPLACE FUNCTION `gauss`(
x DOUBLE,
c DOUBLE,
sig DOUBLE
) RETURNS double
RETURN EXP(
(
- 1 * POWER(( x - c ), 2 )/(
2 * POWER( sig, 2 )
)
)
);

+ 16
- 0
mariadb/get_bearing.sql View File

@@ -0,0 +1,16 @@
CREATE OR REPLACE FUNCTION `get_bearing`(
lon1 DOUBLE,
lat1 DOUBLE,
lon2 DOUBLE,
lat2 DOUBLE
) RETURNS double
BEGIN
SET @rlat1 = deg2rad(lat1);
SET @rlat2 = deg2rad(lat2);
SET @rlon1 = deg2rad(lon1);
SET @rlon2 = deg2rad(lon2);
SET @dlon = deg2rad(lon2 - lon1);
SET @b = atan2(sin(@dlon) * cos(@rlat2), cos(@rlat1) * sin(@rlat2) - sin(@rlat1) * cos(@rlat2) * cos(@dlon));
SET @bd = rad2deg(@b);
RETURN MOD(@bd + 360, 360);
END;

+ 4
- 0
mariadb/rad2deg.sql View File

@@ -0,0 +1,4 @@
CREATE OR REPLACE FUNCTION `rad2deg`(
rad DOUBLE
) RETURNS double
RETURN rad * 180.0 / pi();

Loading…
Cancel
Save