ClickHouse function reference
WKT
Returns a Well-Known Text (WKT) representation of various geometric objects. Syntaxgeo_data
(geometric): One of the following geometric types:Point
Ring
Polygon
MultiPolygon
LineString
MultiLineString
- The function supports various geometric types and returns their corresponding WKT format.
- WKT is a text-based format for representing vector geometry objects, widely used in GIS applications.
readWKTMultiPolygon
Converts a Well-Known Text (WKT) representation of a MultiPolygon into a MultiPolygon data type. Syntax:wkt_string
(String
): A string containing the WKT representation of a MultiPolygon.
MultiPolygon
data type.
Example:
The function expects the input string to start with ‘MULTIPOLYGON’. If the input is invalid or doesn’t represent a MultiPolygon, an exception will be thrown.
readWKTPolygon
Converts a Well-Known Text (WKT) representation of a Polygon into a Polygon data type. Syntax:wkt_string
(String
): The input WKT string representing a Polygon geometry.
Polygon
.
Example:
Polygon
type.
The
readWKTPolygon
function is useful for converting geographic data from WKT format, which is commonly used in GIS applications, into ClickHouse’s native Polygon
type for further spatial analysis or visualization.readWKTPoint
Parses a Well-Known Text (WKT) representation of a Point geometry and returns it as a ClickHouse Point type. Syntaxwkt_string
(String
): A string containing the WKT representation of a Point.
- A Point value in ClickHouse’s internal format. [
Point
]
If the input string is not a valid WKT Point representation, the function will throw an exception.
readWKTLineString
Parses a Well-Known Text (WKT) representation of a LineString geometry and returns it in the internal ClickHouse format as a Ring. Syntax:wkt_string
(String
): The input WKT string representing a LineString geometry.
The function automatically closes the linestring by adding the first point at the end if it’s not already present, ensuring it forms a valid Ring.
readWKTMultiLineString
Parses a Well-Known Text (WKT) representation of a MultiLineString geometry and returns it in the internal ClickHouse format. Syntax:wkt_string
(String
): The input WKT string representing a MultiLineString geometry.
The
readWKTMultiLineString
function is useful for importing geographic data from WKT format into ClickHouse for further spatial analysis or visualization of complex linear features like delivery routes, road networks, or boundaries.readWKTRing
Parses a Well-Known Text (WKT) representation of a Polygon geometry and returns a ring (closed linestring) in the internal ClickHouse format. Syntax:wkt_string
(String
): The input WKT string representing a Polygon geometry.
Ring
]
Example:
readWKTRing
parses the WKT representation of a polygon shaped like a taco and returns it as a closed linestring (ring).
The function expects the input to be a valid WKT representation of a Polygon. If the input is not a valid Polygon WKT, the function may return unexpected results or throw an error.
polygonsWithinSpherical
Checks if one polygon is completely within another polygon on a spherical surface. Syntax:polygon_a
(Polygon
): The first polygon.polygon_b
(Polygon
): The second polygon.
1
ifpolygon_a
is completely withinpolygon_b
.0
otherwise.
UInt8
Example:
This function uses a spherical model of the Earth, which can be less accurate for very large areas but is generally faster to compute than Cartesian alternatives.
polygonsDistanceSpherical
Calculates the minimal distance between two polygons on a sphere. Syntax:polygon1
(Polygon
): The first polygon.polygon2
(Polygon
): The second polygon.
Float64
value.
Example:
- The function interprets coordinates as latitude and longitude on an ideal sphere, which is an approximation of the Earth’s surface.
- This spherical calculation is faster but less precise than geodesic calculations.
- Distances are returned in radians. Multiply by the Earth’s radius to get the distance in your preferred unit.
polygonsDistanceCartesian
Calculates the minimum distance between two polygons in a Cartesian coordinate system. Syntax:polygon1
(Polygon
): The first polygon.polygon2
(Polygon
): The second polygon.
Float64
value.
Example:
This function uses a Cartesian coordinate system, which assumes a flat surface. For more accurate calculations on a spherical surface (like Earth), consider using
polygonsDistanceSpherical
instead.polygonsEqualsCartesian
Checks if two polygons are equal in the Cartesian coordinate system. Syntax:polygon_a
(Polygon
): The first polygon to compare.polygon_b
(Polygon
): The second polygon to compare.
1
if the polygons are equal,0
otherwise. [UInt8
]
1
, indicating that the polygons are considered equal even though the second polygon explicitly closes the loop by repeating the first point.
This function uses the Cartesian coordinate system, which treats coordinates as flat X and Y values. It’s suitable for small areas where Earth’s curvature is negligible, like comparing taco shop delivery zones in a city.
polygonsSymDifferenceSpherical
Calculates the spatial set theoretic symmetric difference (XOR) between two polygons on a sphere. Syntaxpolygon1
(Polygon
): The first polygon.polygon2
(Polygon
): The second polygon.
MultiPolygon
representing the symmetric difference of the input polygons.
Example
The function treats the Earth as a perfect sphere, which may lead to some inaccuracies for very large polygons. For more precise calculations on an ellipsoidal model of the Earth, consider using a specialized geographic information system.
polygonsSymDifferenceCartesian
Calculates the spatial set theoretic symmetric difference (XOR) between two polygons in the Cartesian coordinate system. Syntaxpolygon1
(Polygon
): The first polygon.polygon2
(Polygon
): The second polygon.
MultiPolygon
representing the symmetric difference of the input polygons.
Example
The Cartesian coordinate system treats the Earth as a flat surface, which is suitable for small areas but may introduce inaccuracies for larger regions. For more precise calculations over larger distances, consider using
polygonsSymDifferenceSpherical
.polygonsIntersectionSpherical
Calculates the intersection of two polygons on a spherical surface. Syntaxpolygon1
(Polygon
): The first polygon.polygon2
(Polygon
): The second polygon.
MultiPolygon
representing the intersection of the two input polygons.
Example
- The function assumes a spherical Earth model, which may not be suitable for high-precision calculations.
- Coordinates are interpreted as (longitude, latitude) pairs in degrees.
polygonsIntersectionCartesian
, polygonsUnionSpherical
, polygonsSymDifferenceSpherical
polygonsWithinCartesian
Checks if one polygon is completely within another polygon using Cartesian coordinates. Syntax:polygon_a
(Polygon
): The first polygon to check.polygon_b
(Polygon
): The second polygon to check.
1
ifpolygon_a
is completely withinpolygon_b
.0
otherwise.
UInt8
Example:
This function uses a Cartesian coordinate system, which is suitable for planar geometries but may not be accurate for geographic coordinates on a spherical surface. For geographic calculations, consider using
polygonsWithinSpherical
instead.polygonConvexHullCartesian
Calculates the convex hull of a polygon or multipolygon using Cartesian coordinates. Syntax:polygon
(MultiPolygon
): The input polygon or multipolygon.
The convex hull is the smallest convex polygon that contains all points of the input polygon or multipolygon. It’s useful for simplifying complex shapes or finding the outer boundary of a set of points.This function uses Cartesian coordinates, which means it treats the Earth as a flat surface. For more accurate calculations on a spherical Earth model, consider using spherical geometry functions if available.
polygonAreaSpherical
Calculates the surface area of a polygon on a sphere. Syntaxpolygon
(Array(Array(Float64))
) — A polygon represented as an array of points. Each point is an array of two numbers (longitude and latitude in degrees). The polygon must be closed, meaning the first and last points should be the same.
- The area of the polygon in square meters. (
Float64
)
- The function assumes a spherical Earth model, which may introduce some inaccuracies for very large polygons.
- The polygon should be defined in counter-clockwise order for exterior rings and clockwise for any interior rings (holes).
- For more accurate results with large polygons or in areas near the poles, consider using a more precise geodesic calculation method.
polygonsUnionSpherical
Calculates the union (OR) of two polygons on a sphere. Syntaxpolygon1
(Polygon
): The first polygon.polygon2
(Polygon
): The second polygon.
MultiPolygon
representing the union of the input polygons.
Example
The function treats the coordinates as latitude and longitude on a sphere, making it suitable for geographic calculations. For planar calculations, use
polygonsUnionCartesian
instead.polygonPerimeterSpherical
Calculates the perimeter of a polygon on a spherical surface. Syntax:polygon
(Polygon
): The polygon for which to calculate the perimeter.
Float64
value.
Example:
The
polygonPerimeterSpherical
function assumes the Earth is a perfect sphere, which may introduce some inaccuracies for very large polygons or those near the poles. For more precise calculations on the Earth’s surface, consider using a geodetic library that accounts for the Earth’s ellipsoidal shape.polygonsIntersectionCartesian
Calculates the intersection of polygons in a Cartesian coordinate system. Syntax:polygon1
(Polygon
): The first polygon.polygon2
(Polygon
): The second polygon.
MultiPolygon
representing the intersection of the input polygons.
Example:
This function uses a Cartesian coordinate system, which is suitable for small areas where the Earth’s curvature can be neglected. For larger areas or when higher precision is required, consider using
polygonsIntersectionSpherical
.polygonAreaCartesian
Calculates the area of a polygon in a Cartesian coordinate system. Syntaxpolygon
(Array
): A polygon represented as an array of points. Each point is an array of two numbers (coordinates). The polygon must be closed, meaning the first and last points should be the same.
- The area of the polygon. Type:
Float64
.
- The function assumes a flat (Cartesian) coordinate system. For geographic coordinates on a spherical surface, use
polygonAreaSpherical
instead. - The polygon should be simple (non-self-intersecting) for accurate results.
- The order of points matters: they should define the polygon in a consistent direction (clockwise or counterclockwise).
polygonPerimeterCartesian
Calculates the perimeter of a polygon in a Cartesian coordinate system. Syntax:polygon
(Polygon
): The polygon for which to calculate the perimeter.
Float64
value.
Example:
This function uses a Cartesian coordinate system, which assumes a flat surface. For geographical calculations on a spherical surface (like Earth), consider using
polygonPerimeterSpherical
instead.polygonsUnionCartesian
Calculates the union of polygons in a Cartesian coordinate system. Syntax:polygon1
(Polygon
): The first polygon.polygon2
(Polygon
): The second polygon.
MultiPolygon
representing the union of the input polygons.
Example:
MultiPolygon
that covers the combined area of both territories.
This function uses a Cartesian coordinate system, which is suitable for flat-surface calculations. For geographic calculations on a spherical surface, consider using
polygonsUnionSpherical
instead.