Geographical coordinates
Work with latitude and longitude coordinates using specialized functions.
ClickHouse function reference
greatCircleDistance
Calculates the distance between two points on the Earth’s surface using the great-circle formula.
Syntax:
Arguments:
lon1Deg
(Float64
): Longitude of the first point in degrees. Range: [-180°, 180°].lat1Deg
(Float64
): Latitude of the first point in degrees. Range: [-90°, 90°].lon2Deg
(Float64
): Longitude of the second point in degrees. Range: [-180°, 180°].lat2Deg
(Float64
): Latitude of the second point in degrees. Range: [-90°, 90°].
Positive values correspond to North latitude and East longitude, and negative values correspond to South latitude and West longitude.
Returns:
The distance between two points on the Earth’s surface, in meters. [Float64
]
Exception:
Throws an exception when input parameter values fall outside the specified ranges.
Example:
Result:
This example calculates the great-circle distance between the Los Angeles Taco Festival and the Mexico City Taco Festival, returning the result in meters.
While greatCircleDistance
provides a good approximation, for more precise calculations on the Earth’s surface, consider using the geoDistance
function, which uses the WGS-84 ellipsoid model.
geoDistance
Calculates the distance between two points on the Earth’s surface using the WGS-84 ellipsoid model.
Syntax:
Arguments:
lon1Deg
(Float64
): Longitude of the first point in degrees. Range: [-180°, 180°].lat1Deg
(Float64
): Latitude of the first point in degrees. Range: [-90°, 90°].lon2Deg
(Float64
): Longitude of the second point in degrees. Range: [-180°, 180°].lat2Deg
(Float64
): Latitude of the second point in degrees. Range: [-90°, 90°].
Positive values correspond to North latitude and East longitude, and negative values correspond to South latitude and West longitude.
Returns:
The distance between two points on the Earth’s surface, in meters. [Float64
]
Exception:
Throws an exception when input parameter values fall outside the specified ranges.
- This function is more precise than
greatCircleDistance
as it uses the WGS-84 ellipsoid model instead of a sphere. - For close points, it calculates the distance using planar approximation with the metric on the tangent plane at the midpoint of the coordinates.
- The performance is the same as
greatCircleDistance
(no performance drawback).
Example:
Result:
This example calculates the distance between a taco stand in Mexico City and a taco truck in Los Angeles, returning the result in meters.
It is recommended to use geoDistance
over greatCircleDistance
for more accurate calculations of distances on Earth.
greatCircleAngle
Calculates the central angle between two points on the Earth’s surface using the great-circle formula.
Syntax:
Arguments:
lon1Deg
(Float64
): Longitude of the first point in degrees. Range: [-180°, 180°].lat1Deg
(Float64
): Latitude of the first point in degrees. Range: [-90°, 90°].lon2Deg
(Float64
): Longitude of the second point in degrees. Range: [-180°, 180°].lat2Deg
(Float64
): Latitude of the second point in degrees. Range: [-90°, 90°].
Positive values correspond to North latitude and East longitude, and negative values correspond to South latitude and West longitude.
Returns:
The central angle between two points in degrees. [Float64
]
Example:
Result:
This example calculates the central angle between two taco trucks, one in Los Angeles and another in New York. The result is approximately 40.7 degrees, representing the angular distance between these two locations on the Earth’s surface.
The greatCircleAngle
function is useful for calculating angular distances on a spherical model of the Earth. For more precise distance calculations that take into account the Earth’s ellipsoidal shape, consider using the geoDistance
function.
pointInEllipses
Checks whether a point belongs to at least one of the specified ellipses on a 2D plane.
Syntax:
Arguments:
x, y
(Float64
): Coordinates of the point to check.xi, yi
(Float64
): Coordinates of the center of the i-th ellipse.ai, bi
(Float64
): Lengths of the semi-major and semi-minor axes of the i-th ellipse.
The function accepts 2 + 4n arguments, where n is the number of ellipses.
Returns:
1
if the point is inside at least one of the ellipses, 0
otherwise. [UInt8
]
Example:
Result:
In this example, we check if the point (5, 5) is within either of two ellipses representing “taco zones”. The result 1
indicates that the point is inside at least one of the ellipses, suggesting it’s in a taco zone.
The function uses the Cartesian coordinate system. Make sure to convert geographical coordinates to an appropriate projection if working with map data.
Syntax:
Arguments:
(x, y)
(Tuple(Float64, Float64)
): Coordinates of a point on the plane.[(a, b), (c, d) ...]
(Array(Tuple(Float64, Float64))
): Polygon vertices.- Each vertex is represented by a pair of coordinates
(a, b)
. - Vertices should be specified in a clockwise or counterclockwise order.
- The minimum number of vertices is 3.
- The polygon must be constant.
- Each vertex is represented by a pair of coordinates
Returns:
1
if the point is inside the polygon,0
if it is not. (UInt8
)
- If the point is on the polygon boundary, the function may return either 0 or 1.
- The function supports polygons with holes (cut out sections). Add polygons that define the cut out sections using additional arguments of the function.
- The function does not support non-simply-connected polygons.
Example:
Result:
In this example, we check if the point (3, 3) is inside a square-shaped “taco zone” defined by the vertices (0, 0), (0, 5), (5, 5), and (5, 0). The result 1 indicates that the point is inside the taco zone.