Skip to main content
Geo functions compute distances, angles, spatial containment, and H3 grid indexes from latitude/longitude coordinates. These functions run on ClickHouse only and are rejected on PostgreSQL.

greatCircleDistance

Returns the distance between two points on the surface of the Earth, in meters, using the great-circle (spherical) model. Faster but slightly less accurate than geoDistance (ClickHouse only). Syntax
Arguments
  • lon1Deg, lon2Deg — Longitude of each point, in degrees, in the range [-180, 180].
  • lat1Deg, lat2Deg — Latitude of each point, in degrees, in the range [-90, 90].
Returned value
  • The distance between the two points, in meters (Float64). Roughly 111,195 m per degree of latitude.
Example
Users within 50 km of a city center

geoDistance

Returns the geodesic distance between two points on the WGS-84 ellipsoid, in meters. This is more accurate than greatCircleDistance for real-world distances (ClickHouse only). Syntax
Arguments
  • lon1Deg, lon2Deg — Longitude of each point, in degrees, in the range [-180, 180].
  • lat1Deg, lat2Deg — Latitude of each point, in degrees, in the range [-90, 90].
Returned value
  • The distance between the two points, in meters (Float64).
Example
Distance from each user to a fixed store

pointInPolygon

Returns whether a point lies inside a polygon defined by its vertices. Behavior on the polygon boundary is undefined (ClickHouse only). Syntax
Arguments
  • (x, y) — The point to test, as a tuple of coordinates (Tuple).
  • [(a, b), (c, d), ...] — The polygon, as an array of vertex tuples forming a closed ring (Array of Tuple).
Returned value
  • 1 if the point is inside the polygon, 0 otherwise (UInt8).
Example
Users inside a bounding-box polygon

greatCircleAngle

Returns the central angle between two points on a sphere, in degrees, using the great-circle (haversine) formula (ClickHouse only). Syntax
Arguments
  • lon1Deg, lon2Deg — Longitude of each point, in degrees.
  • lat1Deg, lat2Deg — Latitude of each point, in degrees.
Returned value
  • The central angle between the two points, in degrees (Float64).
Example
Central angle between two coordinates

geoToH3

Returns the H3 cell index for the given latitude, longitude, and resolution. H3 is a hierarchical hexagonal geospatial grid, useful for bucketing points into cells (ClickHouse only). Argument order is (lat, lon, resolution) on ClickHouse v25.5 and later; earlier versions took (lon, lat, resolution). Syntax
Arguments
  • lat — Latitude of the point, in degrees (Float64).
  • lon — Longitude of the point, in degrees (Float64).
  • resolution — H3 grid resolution, in the range [0, 15], where 0 is coarsest and 15 is finest (UInt8).
Returned value
  • The H3 cell index (UInt64), or 0 on error.
Example
Bucket users into H3 cells at resolution 7

h3IsValid

Returns whether the given number is a valid H3 cell index (ClickHouse only). Syntax
Arguments
  • h3index — The H3 cell index to check (UInt64).
Returned value
  • 1 if the value is a valid H3 index, 0 otherwise (UInt8).
Example
Keep only valid H3 cells