Skip to content

Commit

Permalink
add unit argument and apply conversion for kms
Browse files Browse the repository at this point in the history
multiply instead of divide and move logic in core macro

rename things a bit

update readme
  • Loading branch information
bastienboutonnet committed Feb 27, 2021
1 parent bbba960 commit 250eb00
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Usage:
---
### Date/Time
#### date_spine ([source](macros/datetime/date_spine.sql))
This macro returns the sql required to build a date spine. The spine will include the `start_date` (if it is aligned to the `datepart`), but it will not include the `end_date`.
This macro returns the sql required to build a date spine. The spine will include the `start_date` (if it is aligned to the `datepart`), but it will not include the `end_date`.

Usage:
```
Expand All @@ -111,9 +111,11 @@ Usage:
#### haversine_distance ([source](macros/geo/haversine_distance.sql))
This macro calculates the [haversine distance](http://daynebatten.com/2015/09/latitude-longitude-distance-sql/) between a pair of x/y coordinates.

Optionally takes a `unit` string parameter ('km' or 'mi') which defaults to miles (imperial system).

Usage:
```
{{ dbt_utils.haversine_distance(lat1=<float>,lon1=<float>,lat2=<float>,lon2=<float>) }}
{{ dbt_utils.haversine_distance(lat1=<float>,lon1=<float>,lat2=<float>,lon2=<float>, unit='mi'<sting>) }}
```
---
### Schema Tests
Expand Down
16 changes: 11 additions & 5 deletions macros/geo/haversine_distance.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ This calculates the distance between two sets of latitude and longitude.
The formula is from the following blog post:
http://daynebatten.com/2015/09/latitude-longitude-distance-sql/

The arguments should be float type.
The arguments should be float type.
#}

{% macro haversine_distance(lat1,lon1,lat2,lon2) -%}
{{ return(adapter.dispatch('haversine_distance', packages = dbt_utils._get_utils_namespaces())(lat1,lon1,lat2,lon2)) }}
{% macro haversine_distance(lat1,lon1,lat2,lon2,unit='mi') -%}
{{ return(adapter.dispatch('haversine_distance', packages = dbt_utils._get_utils_namespaces())(lat1,lon1,lat2,lon2,unit)) }}
{% endmacro %}

{% macro default__haversine_distance(lat1,lon1,lat2,lon2) -%}
{% macro default__haversine_distance(lat1,lon1,lat2,lon2,unit='km') -%}
{# vanilla macro is in miles #}
{% set conversion = '' %}
{% if unit == 'km' %}
{# we multiply miles result to get it in kms #}
{% set conversion = '* 1.60934' %}
{% endif %}

2 * 3961 * asin(sqrt((sin(radians(({{lat2}} - {{lat1}}) / 2))) ^ 2 +
cos(radians({{lat1}})) * cos(radians({{lat2}})) *
(sin(radians(({{lon2}} - {{lon1}}) / 2))) ^ 2))
(sin(radians(({{lon2}} - {{lon1}}) / 2))) ^ 2)) {{conversion_rate}}

{%- endmacro %}

0 comments on commit 250eb00

Please sign in to comment.