Git Product home page Git Product logo

lib_mysqludf_haversine's Introduction

lib_mysqludf_haversine

MySQL UDF to compute the Haversine formula

Returns the haversine distance from two points

Note: MySQL 5.7 introduced the native st_distance_sphere function, so this project here is not required anymore.

Input parameters

lat1 (real), lng1 (real), lat2 (real), lng2 (real), type (string - optinal - 'km', 'ft', 'mi')

Output

Distance between the two points. The measurement is returned in kilometers (real) by default, however if you specify the 5th type argument you may return in other measurements.

Install

To compile

gcc -shared -fPIC -o lib_mysqludf_haversine.so lib_mysqludf_haversine.c $(mysql_config --cflags) \
    -lm -DMYSQL_DYNAMIC_PLUGIN

To register this function

At mysql commandline prompt, type:

CREATE FUNCTION haversine_distance RETURNS REAL SONAME 'lib_mysqludf_haversine.so';

To get rid of the function

At mysql commandline prompt, type:

DROP FUNCTION haversine_distance;

To list all installed functions

At mysql commandline prompt, type:

SELECT  FROM mysql.func;

Troubleshooting

If you have permission problems installing function such as:

ERROR 1126 (HY000): Can't open shared library 'lib_mysqludf_haversine.so' 
(errno: 22 /usr/lib/mysql/plugin/lib_mysqludf_haversine.so: failed to map segment from shared object: Permission denied)

Follow this steps:

  1. type: sudo vi /etc/apparmor.d/usr.sbin.mysqld;
  2. add this line: /usr/lib/mysql/plugin/** mr, and save;
  3. type: sudo /etc/init.d/apparmor restart

then register the function in MySQL.

How to use

Once installed, to calculate distance between two Latitude/Longitude points:

SELECT id, name FROM MY_PLACES WHERE haversine_distance(lat1, lng1, lat2, lng2) < 40;

The SELECT will return all the records with a distance less then 40 kilometers.

SELECT id, name FROM MY_PLACES WHERE haversine_distance(lat1, lng1, lat2, lng2, 'ft') < 25;

The SELET will return all the records with a distance less then 25 feet.

lib_mysqludf_haversine's People

Contributors

adampresley avatar cweiske avatar evilworm avatar lucasepe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

lib_mysqludf_haversine's Issues

Crash on null arguments

Hello. I use MariaDB 10.0.20. I installed this great plugin. It works fine, but if any of input parameters is NULL, my MariaDB server restarts and I get «ERROR 2013 (HY000): Lost connection to MySQL server during query» error.
For now I added a workaround with additional «WHERE lat IS NOT NULL AND lon IS NOT NULL and ...» for all lat&lons. But I think it would be great to add such checking to the function itself.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.