datasette-haversine

Datasette plugin that adds a custom SQL function for haversine distances

APACHE-2.0 License

Downloads
261
Stars
2

datasette-haversine

Datasette plugin that adds a custom SQL function for haversine distances

Install this plugin in the same environment as Datasette to enable the haversine() SQL function.

datasette install datasette-haversine

The plugin is built on top of the haversine library.

haversine() to calculate distances

select haversine(lat1, lon1, lat2, lon2);

This will return the distance in kilometers between the point defined by (lat1, lon1) and the point defined by (lat2, lon2).

Demo

You can try this function here using Datasette Lite.

select haversine(
  40.7128, -74.0060, -- New York
  37.7749, -122.4194, -- San Francisco
  'mi'
) as distance_in_miles;

Custom units

By default haversine() returns results in km. You can pass an optional third argument to get results in a different unit:

  • ft for feet
  • m for meters
  • in for inches
  • mi for miles
  • nmi for nautical miles
  • km for kilometers (the default)
select haversine(lat1, lon1, lat2, lon2, 'mi');
Package Rankings
Top 19.4% on Pypi.org
Badges
Extracted from project README
PyPI Changelog Tests License
Related Projects