Polygon functions
Work with polygonal shapes on a sphere for geofencing and area-based calculations.
ClickHouse function reference
WKT
Returns a Well-Known Text (WKT) representation of various geometric objects.
Syntax
Arguments
geo_data
(geometric): One of the following geometric types:Point
Ring
Polygon
MultiPolygon
LineString
MultiLineString
Returns
A string containing the WKT representation of the input geometric object.
Example
Result:
This example demonstrates converting a Point and a Polygon to their WKT representations.
- 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:
Arguments:
wkt_string
(String
): A string containing the WKT representation of a MultiPolygon.
Returns:
A MultiPolygon
data type.
Example:
Result:
This example defines two taco delivery zones: one with a hole (perhaps a no-delivery area) and another triangular zone.
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:
Arguments:
wkt_string
(String
): The input WKT string representing a Polygon geometry.
Returns:
A value of type Polygon
.
Example:
Result:
In this example, we create a polygon in the shape of a taco shell using WKT format and convert it to a ClickHouse 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.
Syntax
Arguments
wkt_string
(String
): A string containing the WKT representation of a Point.
Returns
- A Point value in ClickHouse’s internal format. [
Point
]
Example
Result:
This example parses a WKT Point representing the location of a taco stand and returns it as a ClickHouse Point type.
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:
Arguments:
wkt_string
(String
): The input WKT string representing a LineString geometry.
Returns:
A Ring (closed linestring) in ClickHouse’s internal format.
Example:
Result:
In this example, we parse a WKT LineString representing a taco delivery route. The function automatically closes the linestring by adding the first point at the end, creating a Ring.
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:
Arguments:
wkt_string
(String
): The input WKT string representing a MultiLineString geometry.
Returns:
The function returns a ClickHouse internal representation of the MultiLineString geometry.
Example:
Result:
In this example, we parse a WKT MultiLineString representing two taco delivery routes. The result is a nested array structure that ClickHouse uses to represent MultiLineString geometries internally.
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:
Arguments:
wkt_string
(String
): The input WKT string representing a Polygon geometry.
Returns:
The function returns a ClickHouse internal representation of the ring (closed linestring) geometry. [Ring
]
Example:
Result:
In this 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:
Arguments:
polygon_a
(Polygon
): The first polygon.polygon_b
(Polygon
): The second polygon.
Returns:
1
ifpolygon_a
is completely withinpolygon_b
.0
otherwise.
Type: UInt8
Example:
Result:
This example checks if a small taco-shaped polygon (1x1 square) is completely within a larger taco platter-shaped polygon (3x3 square).
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:
Arguments:
polygon1
(Polygon
): The first polygon.polygon2
(Polygon
): The second polygon.
Returns:
The minimal distance between the two polygons as a Float64
value.
Example:
Result:
This example calculates the distance between a square-shaped taco stand location and a square-shaped salsa festival area.
- 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:
Arguments:
polygon1
(Polygon
): The first polygon.polygon2
(Polygon
): The second polygon.
Returns:
The minimum distance between the two polygons as a Float64
value.
Example:
Result:
In this example, we calculate the minimum distance between a taco truck and a salsa stand represented as polygons in a Cartesian coordinate system. The result shows that the closest points between the two polygons are approximately 2.83 units apart.
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:
Arguments:
polygon_a
(Polygon
): The first polygon to compare.polygon_b
(Polygon
): The second polygon to compare.
Returns:
1
if the polygons are equal,0
otherwise. [UInt8
]
Example:
Result:
In this example, we compare two polygons representing taco-shaped areas. The function returns 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.
Syntax
Arguments
polygon1
(Polygon
): The first polygon.polygon2
(Polygon
): The second polygon.
Returns
A MultiPolygon
representing the symmetric difference of the input polygons.
Example
Result:
This example calculates the symmetric difference between two taco franchise territories, showing areas exclusive to each franchise but not shared.
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.
Syntax
Arguments
polygon1
(Polygon
): The first polygon.polygon2
(Polygon
): The second polygon.
Returns
A MultiPolygon
representing the symmetric difference of the input polygons.
Example
Result:
This example calculates the symmetric difference between a square taco stand location (0,0 to 3,3) and a slightly overlapping square salsa bar location (1,1 to 4,4). The result shows the areas that belong to either the taco stand or the salsa bar, but not both.
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.
Syntax
Arguments
polygon1
(Polygon
): The first polygon.polygon2
(Polygon
): The second polygon.
Returns
A MultiPolygon
representing the intersection of the two input polygons.
Example
Result:
This example calculates the intersection of two overlapping taco-shaped polygons on a spherical surface.
- 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.
See also: polygonsIntersectionCartesian
, polygonsUnionSpherical
, polygonsSymDifferenceSpherical
polygonsWithinCartesian
Checks if one polygon is completely within another polygon using Cartesian coordinates.
Syntax:
Arguments:
polygon_a
(Polygon
): The first polygon to check.polygon_b
(Polygon
): The second polygon to check.
Returns:
1
ifpolygon_a
is completely withinpolygon_b
.0
otherwise.
Type: UInt8
Example:
Result:
In this example, we check if a small taco-shaped polygon (2x2 square) is completely within a larger taco platter-shaped polygon (4x4 square). The result is 1, indicating that the small taco is indeed inside the platter.
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:
Arguments:
polygon
(MultiPolygon
): The input polygon or multipolygon.
Returns:
A Polygon representing the convex hull of the input.
Example:
Result:
In this example, we calculate the convex hull of a taco-shaped polygon. The result is a simple rectangular polygon that encloses all points of the original shape.
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.
Syntax
Arguments
polygon
(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.
Returns
- The area of the polygon in square meters. (
Float64
)
Example
This example calculates the area of a taco-shaped polygon:
Result:
This calculates the approximate area of a taco-shaped region in Mexico City.
- 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.
Syntax
Arguments
polygon1
(Polygon
): The first polygon.polygon2
(Polygon
): The second polygon.
Returns
A MultiPolygon
representing the union of the input polygons.
Example
Result:
This example unites two taco-shaped polygons, resulting in a larger, combined taco-shaped area.
The function treats the coordinates as latitude and longitude on a sphere, making it suitable for geographic calculations. For planar calculations, use polygonsUnionCartesian
instead.
Here’s the updated markdown with the note in a callout:
polygonPerimeterSpherical
Calculates the perimeter of a polygon on a spherical surface.
Syntax:
Arguments:
polygon
(Polygon
): The polygon for which to calculate the perimeter.
Returns:
The perimeter of the polygon as a Float64
value.
Example:
Result:
This example calculates the perimeter of a polygon representing the outline of a taco-shaped island. The result is rounded to 6 decimal places.
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:
Arguments:
polygon1
(Polygon
): The first polygon.polygon2
(Polygon
): The second polygon.
Returns:
A MultiPolygon
representing the intersection of the input polygons.
Example:
Result:
In this example, we calculate the intersection between a taco truck’s service area and a salsa distribution zone. The result shows the area where customers can get both tacos and salsa.
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.
Syntax
Arguments
polygon
(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.
Returns:
- The area of the polygon. Type:
Float64
.
Example
Calculate the area of a square:
Result:
This example calculates the area of a square with sides of length 5. The result is 25 square units.
Calculate the area of a taco-shaped polygon:
Result:
This example calculates the area of a taco-shaped polygon. The result is 16 square units.
- 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:
Arguments:
polygon
(Polygon
): The polygon for which to calculate the perimeter.
Returns:
The perimeter of the polygon as a Float64
value.
Example:
Result:
This example calculates the perimeter of a square-shaped taco stand plot. The polygon is defined by five points, forming a square with sides of length 5. The perimeter is the sum of all sides: 5 + 5 + 5 + 5 = 20.
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:
Arguments:
polygon1
(Polygon
): The first polygon.polygon2
(Polygon
): The second polygon.
Returns:
A MultiPolygon
representing the union of the input polygons.
Example:
Result:
In this example, we calculate the union of two polygons representing different taco franchise territories. The result is a single 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.
Was this page helpful?