Calculate distance between two points directly in SQLite

前端 未结 5 640
北海茫月
北海茫月 2020-12-17 19:15

In my web/MySQL application I have something like this to get distance between two points:

6371 * acos(cos(radians(-19.83996)) * cos(radians(lat)) * cos(radi         


        
5条回答
  •  天命终不由人
    2020-12-17 19:50

    For windows:
    
    Install minGw full options. modify environment variable: system variable path, including
    
    c:\mingw\bin
    
    
    test functionality command:
    
    g++ --version
    
    copy files: extension-functions.c, sqlite3.h, sqlite3ext.h in sqlite3 program directory. Go to sqlite3 directory and compile:
    
    gcc -shared -I "path" -o libsqlitefunctions.so extension-functions.c
    
       (path = path of sqlite3ext.h; i.e. C:\sqlite3)
    
    If the program is built so that loading extensions is permitted, the following will work:
    
       sqlite> SELECT load_extension('./libsqlitefunctions.so');
    
       sqlite> select cos(radians(45));
    
       0.707106781186548
    
    SQLite Distance implementation:
    
    From:  https://www.movable-type.co.uk/scripts/latlong.html https://en.wikipedia.org/wiki/Haversine_formula
    
    Distance
    This uses the ‘haversine’ formula to calculate the great-circle distance between two points – that is, the shortest distance over the earth’s surface – giving an ‘as-the-crow-flies’ distance between the points (ignoring any hills they fly over, of course!).
    
    Haversine
    formula:    a = sin²(Δφ/2) + cos φ1 * cos φ2 * sin²(Δλ/2)
    c = 2 * atan2( √a, √(1−a) )
    c = 2 * 
    d = R * c
    where   φ is latitude, λ is longitude, R is earth’s radius (mean radius = 6,371km);
    note that angles need to be in radians to pass to trig functions!
    JavaScript: 
    const R = 6378136.6 ; // meters equatorial radius
    const φ1 = lat1 * Math.PI/180; // φ, λ in radians
    const φ2 = lat2 * Math.PI/180;
    const Δφ = (lat2-lat1) * Math.PI/180;
    const Δλ = (lon2-lon1) * Math.PI/180;
    
    const a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
              Math.cos(φ1) * Math.cos(φ2) *
              Math.sin(Δλ/2) * Math.sin(Δλ/2);
    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    
    const c = 2 * Math.asen(MIN (1, Math.sqrt(a))); //sqlite implementation
    
    const d = R * c; // in metres
    
    Distance = 2 * R * ASIN( MIN (1, SQRT( SIN( (RADIANS(lat1)-RADIANS(lat2))/2 )^2 + COS( RADIANS(lat1) )*COS( RADIANS(lat2) )*SIN( (RADIANS(long1)-RADIANS(long2))/2 )^2 )))
    
    Physical Properties of Earth https://en.wikipedia.org/wiki/Earth_ellipsoid :Ecuatorial radius: 6378.1366 Kms. Average radius: 6367 Kms
    
    
    Constant = 2 * 6378136.6 = 12756273.2
    
    SQLite query command with coordinates taken from table PAR: 
    
    ROUND (
          12756273.2 * ASIN(
                            MIN (1 , 
                                    SQRT(
                                        POWER( SIN(RADIANS(PAR.Lat1 - PAR.Lat2)/2) , 2) + 
                                        COS(RADIANS(PAR.Lat1)) * COS(RADIANS(PAR.Lat2)) * POWER ( SIN(RADIANS(PAR.Long1 - PAR.Long2)/2) , 2)
                                          )
                                )
                            )
             , 0) AS Distance
    

提交回复
热议问题