S2 geometry functions
Leverage Google’s S2 Geometry Library for spatial indexing and geometric operations on a sphere.
ClickHouse function reference
geoToS2
Converts geographical coordinates to an S2 cell index.
Syntax:
Arguments:
lon
(Float64
): Longitude of the point.lat
(Float64
): Latitude of the point.
Returns:
- S2 cell index (
UInt64
).
Example:
Result:
This example converts the geographical coordinates of Austin, Texas to its corresponding S2 cell index.
S2 is a hierarchical geocoding system that represents the Earth’s surface as a hierarchy of cells. It’s useful for spatial indexing and querying in geographical applications.
s2ToGeo
Converts an S2 cell index to geographic coordinates (longitude, latitude).
Syntax:
Arguments:
s2Index
(UInt64
): The S2 cell index to convert.
Returns:
A tuple containing two Float64
values:
- Longitude
- Latitude
Example:
Result:
In this example, we convert an S2 cell index to geographic coordinates, which could represent the location of a taco truck.
The S2 indexing system is used for efficient geospatial indexing and querying. It’s particularly useful for location-based services and geospatial analysis in ClickHouse.
s2GetNeighbors
Returns the S2 neighbor indexes corresponding to the provided S2 index. Each cell in the S2 system is a quadrilateral bounded by four geodesics, so each cell has 4 neighbors.
Syntax:
Arguments:
s2index
(UInt64
): The S2 index for which to find neighbors.
Returns:
An array of 4 neighbor indexes: [s2index1, s2index2, s2index3, s2index4]
(Array(UInt64)
).
Example:
Result:
In this example, we find the S2 neighbors of a taco truck location in Austin, Texas. The function returns an array of 4 S2 indexes representing the neighboring cells.
The order of the returned neighbors is consistent but not guaranteed to be in any particular orientation relative to the input cell.
This function is useful for spatial analysis, such as finding nearby locations or implementing efficient geospatial search algorithms.
s2CellsIntersect
Determines if two S2 cells intersect.
Syntax:
Arguments:
s2Index1
(UInt64
): The first S2 cell index.s2Index2
(UInt64
): The second S2 cell index.
Returns:
1
if the cells intersect,0
otherwise. [UInt8
]
Example:
Result:
In this example, we check if the S2 cell containing Austin, TX intersects with the S2 cell of a nearby taco truck. The result 0
indicates that the cells don’t intersect, suggesting the taco truck is not within the specified area.
This function is useful for efficient geospatial queries, such as determining if two areas on Earth overlap or are adjacent.
s2CapContains
Determines if a spherical cap contains a given S2 point.
Syntax:
Arguments:
center
(UInt64
): S2 point index representing the center of the cap.degrees
(Float64
): Radius of the cap in degrees.point
(UInt64
): S2 point index to check for containment.
Returns:
1
if the cap contains the S2 point. [UInt8
]0
if the cap does not contain the S2 point. [UInt8
]
Example:
Result:
In this example, we check if a nearby taco stand is within a 1-degree radius of a Taco Stands in Mexico City. The result 1
indicates that the point is indeed within the specified cap.
The S2 geometry system represents geographical points as 64-bit integers. Use the geoToS2
function to convert latitude and longitude to S2 point indexes.
s2CapUnion
Determines the smallest cap that contains the given two input caps. A cap represents a portion of the sphere that has been cut off by a plane. It is defined by a point on a sphere and a radius in degrees.
Syntax:
Arguments:
center1
,center2
(UInt64
): S2 point indexes corresponding to the two input caps.radius1
,radius2
(Float64
): Radius of the two input caps in degrees.
Returns:
center
(UInt64
): S2 point index corresponding to the center of the smallest cap containing the two input caps.radius
(Float64
): Radius of the smallest cap containing the two input caps.
Example:
Result:
In this example, we calculate the smallest cap that contains two areas in Mexico City, potentially representing a taco delivery zone. The result includes the center point of the new cap (as an S2 index) and its radius in degrees.
s2RectAdd
Increases the size of a bounding rectangle to include a given S2 point.
Syntax:
Arguments:
s2PointLow
(UInt64
): Low S2 point index of the rectangle.s2PointHigh
(UInt64
): High S2 point index of the rectangle.s2Point
(UInt64
): Target S2 point index to include in the rectangle.
Returns:
A tuple containing two UInt64
values:
- Low S2 cell id of the expanded rectangle.
- High S2 cell id of the expanded rectangle.
Example:
Result:
In this example, we expand the bounding rectangle of a taco shop to include a new taco truck location. The function returns the new low and high S2 cell ids that define the expanded rectangle.
The S2 cell ids returned by this function can be converted back to geographic coordinates using the s2ToGeo
function if needed.
s2RectContains
Determines if a given rectangle contains an S2 point. In the S2 system, a rectangle is represented by a type of S2Region called an S2LatLngRect that represents a rectangle in latitude-longitude space.
Syntax:
Arguments:
s2PointLow
(UInt64
): Low S2 point index corresponding to the rectangle.s2PointHigh
(UInt64
): High S2 point index corresponding to the rectangle.s2Point
(UInt64
): Target S2 point index to check for containment.
Returns:
1
if the rectangle contains the given S2 point.0
if the rectangle doesn’t contain the given S2 point.
Example:
Result:
In this example, we check if a taco truck’s location (represented by an S2 point) is within a rectangle defined by the S2 indexes of Taco Stands and Burrito Cart. The result 0
indicates that the taco truck is not within this “fast-casual Mexican food” rectangle.
This function is useful for efficient geospatial queries, such as determining if a point of interest falls within a specific geographical area defined by two corner points.
s2RectUnion
Returns the smallest rectangle containing the union of two given rectangles in the S2 coordinate system.
Syntax:
Arguments:
s2Rect1PointLow
,s2Rect1PointHi
(UInt64
): Low and High S2 point indexes corresponding to the first rectangle.s2Rect2PointLow
,s2Rect2PointHi
(UInt64
): Low and High S2 point indexes corresponding to the second rectangle.
Returns:
- A tuple containing two
UInt64
values:s2UnionRect2PointLow
— Low S2 cell id corresponding to the union rectangle.s2UnionRect2PointHi
— High S2 cell id corresponding to the union rectangle.
Example:
Result:
This example calculates the smallest rectangle that contains both a taco stand in Mexico City and one in San Francisco. The result is represented by two S2 cell ids defining the corners of the rectangle.
In the S2 system, a rectangle is represented by a type of S2Region called an S2LatLngRect, which represents a rectangle in latitude-longitude space.
s2RectIntersection
Returns the smallest rectangle containing the intersection of two given rectangles in the S2 coordinate system.
Syntax:
Arguments:
s2Rect1PointLow
,s2Rect1PointHi
(UInt64
): Low and High S2 point indexes corresponding to the first rectangle.s2Rect2PointLow
,s2Rect2PointHi
(UInt64
): Low and High S2 point indexes corresponding to the second rectangle.
Returns:
- A tuple containing two
UInt64
values:- Low S2 cell id corresponding to the intersection rectangle.
- High S2 cell id corresponding to the intersection rectangle.
Example:
Result:
This example calculates the intersection of two rectangles representing areas around a Taco Stands and a nearby taco stand in Toluca, Mexico. The result represents the overlapping area where taco enthusiasts might compare offerings from both establishments.
In the S2 system, a rectangle is represented by a type of S2Region called S2LatLngRect, which represents a rectangle in latitude-longitude space.