Loading...

verticapy.sql.functions.distance

verticapy.sql.functions.distance(lat0: float, lon0: float, lat1: float, lon1: float, radius: float = 6371.009) StringSQL

Returns the distance (in kilometers) between two points.

Parameters

lat0: float

Starting point latitude.

lon0: float

Starting point longitude.

lat1: float

Ending point latitude.

lon1: float

Ending point longitude.

radius: float

Specifies the radius of the curvature of the earth at the midpoint between the starting and ending points.

Returns

StringSQL

SQL string.

Examples

First, let’s import the vDataFrame in order to create a dummy dataset.

from verticapy import vDataFrame

Now, let’s import the VerticaPy SQL functions.

import verticapy.sql.functions as vpf

We can now build a dummy dataset.

df = vDataFrame(
    {
        "name0": ["Paris"],
        "lat0": [48.864716],
        "lon0": [2.349014],
        "name1": ["Tunis"],
        "lat1": [33.892166],
        "lon1": [9.561555],
    },
)

Now, let’s go ahead and apply the function.

df["distance"] = vpf.distance(
    df["lat0"], df["lon0"], df["lat1"], df["lon1"],
)
display(df[["name0", "name1", "distance"]])
Abc
name0
Varchar(5)
100%
...
Abc
name1
Varchar(5)
100%
123
distance
Float(22)
100%
1Paris...Tunis1768.29186661204

Note

It’s crucial to utilize VerticaPy SQL functions in coding, as they can be updated over time with new syntax. While SQL functions typically remain stable, they may vary across platforms or versions. VerticaPy effectively manages these changes, a task not achievable with pure SQL.

See also

vDataFrame.eval() : Evaluates the expression.