ClickHouse function reference

WKT

Returns a Well-Known Text (WKT) representation of various geometric objects.

Syntax

WKT(geo_data)

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

SELECT
  wkt(tuple(30.12345, -15.67890)) AS point_wkt,
  wkt(
      [
          [tuple(0.0, 0.0), tuple(10.0, 0.0), tuple(10.0, 10.0), tuple(0.0, 10.0), tuple(0.0, 0.0)]
      ]
  ) AS polygon_wkt;

Result:

| point_wkt                  | polygon_wkt                                      |
|----------------------------|--------------------------------------------------|
| POINT(30.1234 -15.6789)    | POLYGON((0 0,10 0,10 10,0 10,0 0))               |

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:

readWKTMultiPolygon(wkt_string)

Arguments:

  • wkt_string (String): A string containing the WKT representation of a MultiPolygon.

Returns:

A MultiPolygon data type.

Example:

SELECT
	readWKTMultiPolygon('MULTIPOLYGON(((2 0,10 0,10 10,0 10,2 0),(4 4,5 4,5 5,4 5,4 4)),((-10 -10,-10 -9,-9 10,-10 -10)))') AS taco_delivery_zones;

Result:

| taco_delivery_zones                                                                                              |
|------------------------------------------------------------------------------------------------------------------|
| [[[(2,0),(10,0),(10,10),(0,10),(2,0)],[(4,4),(5,4),(5,5),(4,5),(4,4)]],[[(-10,-10),(-10,-9),(-9,10),(-10,-10)]]] |

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:

readWKTPolygon(wkt_string)

Arguments:

  • wkt_string (String): The input WKT string representing a Polygon geometry.

Returns:

A value of type Polygon.

Example:

SELECT
	readWKTPolygon('POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))') AS taco_shape;

Result:

| taco_shape                                        |
|---------------------------------------------------|
| [[(30,10),(40,40),(20,40),(10,20),(30,10)]]       |

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

readWKTPoint(wkt_string)

Arguments

  • wkt_string (String): A string containing the WKT representation of a Point.

Returns

  • A Point value in ClickHouse’s internal format. [Point]

Example

SELECT readWKTPoint('POINT(3.14 2.718)') AS taco_location;

Result:

| taco_location |
|---------------|
| (3.14,2.718)  |

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:

readWKTLineString(wkt_string)

Arguments:

  • wkt_string (String): The input WKT string representing a LineString geometry.

Returns:

A Ring (closed linestring) in ClickHouse’s internal format.

Example:

SELECT readWKTLineString('LINESTRING (1 1, 2 2, 3 3, 4 4)') AS taco_route;

Result:

| taco_route                         |
|------------------------------------|
| [(1,1),(2,2),(3,3),(4,4),(1,1)]    |

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:

readWKTMultiLineString(wkt_string)

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:

SELECT readWKTMultiLineString('MULTILINESTRING ((1 1, 2 2, 3 3), (4 4, 5 5, 6 6))') AS taco_routes;

Result:

| taco_routes                                       |
|---------------------------------------------------|
| [[(1,1),(2,2),(3,3)],[(4,4),(5,5),(6,6)]]         |

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:

readWKTRing(wkt_string)

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:

SELECT readWKTRing('POLYGON ((1 1, 2 2, 3 3, 4 4, 1 1))') AS taco_shape;

Result:

| taco_shape                           |
|--------------------------------------|
| [(1,1),(2,2),(3,3),(4,4),(1,1)]      |

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:

polygonsWithinSpherical(polygon_a, polygon_b)

Arguments:

  • polygon_a (Polygon): The first polygon.
  • polygon_b (Polygon): The second polygon.

Returns:

  • 1 if polygon_a is completely within polygon_b.
  • 0 otherwise.

Type: UInt8

Example:

SELECT
  polygonsWithinSpherical(
      [
          [tuple(1.0, 1.0), tuple(1.0, 2.0), tuple(2.0, 2.0), tuple(2.0, 1.0), tuple(1.0, 1.0)]
      ],
      [
          [tuple(0.0, 0.0), tuple(0.0, 3.0), tuple(3.0, 3.0), tuple(3.0, 0.0), tuple(0.0, 0.0)]
      ]
  ) AS is_within;

Result:

| is_within |
|-----------|
| 1         |

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:

polygonsDistanceSpherical(polygon1, polygon2)

Arguments:

  • polygon1 (Polygon): The first polygon.
  • polygon2 (Polygon): The second polygon.

Returns:

The minimal distance between the two polygons as a Float64 value.

Example:

SELECT
  polygonsDistanceSpherical(
      [
          [tuple(0.0, 0.0), tuple(0.0, 1.0), tuple(1.0, 1.0), tuple(1.0, 0.0)]
      ],
      [
          [tuple(10.0, 10.0), tuple(10.0, 11.0), tuple(11.0, 11.0), tuple(11.0, 10.0)]
      ]
  ) AS distance;

Result:

| distance              |
|-----------------------|
| 0.22151564643675617   |

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:

polygonsDistanceCartesian(polygon1, polygon2)

Arguments:

  • polygon1 (Polygon): The first polygon.
  • polygon2 (Polygon): The second polygon.

Returns:

The minimum distance between the two polygons as a Float64 value.

Example:

SELECT
  polygonsDistanceCartesian(
      [
          [tuple(0.0, 0.0), tuple(0.0, 1.0), tuple(1.0, 1.0), tuple(1.0, 0.0)]
      ], -- Taco truck location
      [
          [tuple(3.0, 3.0), tuple(3.0, 4.0), tuple(4.0, 4.0), tuple(4.0, 3.0)]
      ]  -- Salsa stand location
  ) AS distance_between_taco_spots;

Result:

| distance_between_taco_spots |
|-----------------------------|
| 2.8284271247461903          |

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:

polygonsEqualsCartesian(polygon_a, polygon_b)

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:

SELECT
  polygonsEqualsCartesian(
      [
          [tuple(1.0, 1.0), tuple(1.0, 2.0), tuple(2.0, 2.0), tuple(2.0, 1.0)]
      ],
      [
          [tuple(1.0, 1.0), tuple(1.0, 2.0), tuple(2.0, 2.0), tuple(2.0, 1.0), tuple(1.0, 1.0)]
      ]
  ) AS are_tacos_equal;

Result:

| are_tacos_equal |
|-----------------|
| 1               |

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

polygonsSymDifferenceSpherical(polygon1, polygon2)

Arguments

  • polygon1 (Polygon): The first polygon.
  • polygon2 (Polygon): The second polygon.

Returns

A MultiPolygon representing the symmetric difference of the input polygons.

Example

SELECT
  wkt(polygonsSymDifferenceSpherical(
      [
          [tuple(20.0, 20.0), tuple(50.0, 20.0), tuple(50.0, 50.0), tuple(20.0, 50.0)]
      ],
      [
          [tuple(30.0, 30.0), tuple(60.0, 30.0), tuple(60.0, 60.0), tuple(30.0, 60.0)]
      ]
  )) AS taco_territory_difference;

Result:

| taco_territory_difference                                                                                                                   |
|---------------------------------------------------------------------------------------------------------------------------------------------|
| MULTIPOLYGON(((50 30.7714,50 20,20 20,20 50,30 50.8681,30 30,50 30.7714)),((50 30.7714,50 50,30 50.8681,30 60,60 60,60 30,50 30.7714)))     |

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

polygonsSymDifferenceCartesian(polygon1, polygon2)

Arguments

  • polygon1 (Polygon): The first polygon.
  • polygon2 (Polygon): The second polygon.

Returns

A MultiPolygon representing the symmetric difference of the input polygons.

Example

SELECT
  wkt(polygonsSymDifferenceCartesian(
      [
          [tuple(0.0, 0.0), tuple(0.0, 3.0), tuple(3.0, 3.0), tuple(3.0, 0.0)]
      ],
      [
          [tuple(1.0, 1.0), tuple(1.0, 4.0), tuple(4.0, 4.0), tuple(4.0, 1.0)]
      ]
  )) AS sym_difference;

Result:

| sym_difference                                                                           |
|------------------------------------------------------------------------------------------|
| MULTIPOLYGON(((3 1,3 0,0 0,0 3,1 3,1 1,3 1)),((3 1,3 3,1 3,1 4,4 4,4 1,3 1)))            |

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

polygonsIntersectionSpherical(polygon1, polygon2)

Arguments

  • polygon1 (Polygon): The first polygon.
  • polygon2 (Polygon): The second polygon.

Returns

A MultiPolygon representing the intersection of the two input polygons.

Example

SELECT
	wkt(polygonsIntersectionSpherical(
		readWKTPolygon('POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))'),
		readWKTPolygon('POLYGON((0.5 0.5, 0.5 1.5, 1.5 1.5, 1.5 0.5, 0.5 0.5))')
	)) AS intersection;

Result:

| intersection                                                     |
|------------------------------------------------------------------|
| MULTIPOLYGON(((0.5 1.00004,1 1,1 0.500019,0.5 0.5,0.5 1.00004))) |

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:

polygonsWithinCartesian(polygon_a, polygon_b)

Arguments:

  • polygon_a (Polygon): The first polygon to check.
  • polygon_b (Polygon): The second polygon to check.

Returns:

  • 1 if polygon_a is completely within polygon_b.
  • 0 otherwise.

Type: UInt8

Example:

SELECT
  polygonsWithinCartesian(
      [
          [tuple(2.0, 2.0), tuple(2.0, 3.0), tuple(3.0, 3.0), tuple(3.0, 2.0), tuple(2.0, 2.0)]
      ],
      [
          [tuple(1.0, 1.0), tuple(1.0, 4.0), tuple(4.0, 4.0), tuple(4.0, 1.0), tuple(1.0, 1.0)]
      ]
  ) AS is_within;

Result:

| is_within |
|-----------|
| 1         |

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:

polygonConvexHullCartesian(polygon)

Arguments:

  • polygon (MultiPolygon): The input polygon or multipolygon.

Returns:

A Polygon representing the convex hull of the input.

Example:

SELECT
  wkt(polygonConvexHullCartesian(
      [
          [tuple(0.0, 0.0), tuple(0.0, 5.0), tuple(5.0, 5.0), tuple(5.0, 0.0), tuple(2.0, 3.0)]
      ]
  )) AS convex_hull;

Result:

| convex_hull                      |
|----------------------------------|
| POLYGON((0 0,0 5,5 5,5 0,0 0))   |

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

polygonAreaSpherical(polygon)

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:

SELECT
    round(polygonAreaSpherical([
        [(-99.1, 29.4), (-99.1, 22.0), (-98.0, 822.0), (-98.0, 19.4), (-99.1, 19.4)]
    ]), 2) AS taco_area_m2;

Result:

| taco_area_m2 |
|--------------|
| 0.02       |

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

polygonsUnionSpherical(polygon1, polygon2)

Arguments

  • polygon1 (Polygon): The first polygon.
  • polygon2 (Polygon): The second polygon.

Returns

A MultiPolygon representing the union of the input polygons.

Example

SELECT
	wkt(polygonsUnionSpherical(
		[[(30.0, 10.0), (40.0, 40.0), (20.0, 40.0), (10.0, 20.0), (30.0, 10.0)]],
		[[(15.0, 5.0), (40.0, 10.0), (10.0, 20.0), (5.0, 10.0), (15.0, 5.0)]]
	)) AS union_result;

Result:

| union_result                                                                 |
|------------------------------------------------------------------------------|
| MULTIPOLYGON(((10 20,20 40,40 40,30.9603 13.5242,40 10,15 5,5 10,10 20)))    |

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:

polygonPerimeterSpherical(polygon)

Arguments:

  • polygon (Polygon): The polygon for which to calculate the perimeter.

Returns:

The perimeter of the polygon as a Float64 value.

Example:

SELECT
	round(
		polygonPerimeterSpherical([[
			(30.010654, -15.646227), (30.050238, -15.640129), (30.090029, -15.629381),
			(30.130129, -15.623696), (30.16992, -15.632171), (30.195552, -15.649121),
			(30.207231, -15.653152), (30.223147, -15.649741), (30.231002, -15.644677),
			(30.246091, -15.632068), (30.254876, -15.628864), (30.280094, -15.632275),
			(30.296196, -15.639042), (30.32805, -15.652428), (30.356679, -15.651498),
			(30.396263, -15.635995), (30.39771, -15.716817), (30.39926, -15.812005),
			(30.401327, -15.931688), (30.402568, -16.001244), (30.514809, -16.000418),
			(30.586587, -16.000004), (30.74973, -15.998867), (30.857424, -15.998144)
		]]),
		6
	) AS perimeter;

Result:

| perimeter |
|-----------|
| 0.020906  |

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:

polygonsIntersectionCartesian(polygon1, polygon2)

Arguments:

  • polygon1 (Polygon): The first polygon.
  • polygon2 (Polygon): The second polygon.

Returns:

A MultiPolygon representing the intersection of the input polygons.

Example:

SELECT
  wkt(polygonsIntersectionCartesian(
      [
          [tuple(0.0, 0.0), tuple(0.0, 3.0), tuple(3.0, 3.0), tuple(3.0, 0.0)]  -- Taco truck service area
      ],
      [
          [tuple(2.0, 2.0), tuple(2.0, 5.0), tuple(5.0, 5.0), tuple(5.0, 2.0)]  -- Salsa distribution zone
      ]
  )) AS intersection_zone;

Result:

| intersection_zone                         |
|-------------------------------------------|
| MULTIPOLYGON(((2 3,3 3,3 2,2 2,2 3)))     |

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

polygonAreaCartesian(polygon)

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:

SELECT
  polygonAreaCartesian(
      [
          [tuple(0.0, 0.0), tuple(0.0, 5.0), tuple(5.0, 5.0), tuple(5.0, 0.0), tuple(0.0, 0.0)]
      ]
  ) AS square_area;

Result:

| square_area |
|-------------|
| 25          |

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:

SELECT
  polygonAreaCartesian(
      [
          [tuple(0.0, 0.0), tuple(2.0, 4.0), tuple(4.0, 4.0), tuple(6.0, 0.0), tuple(0.0, 0.0)]
      ]
  ) AS taco_area;

Result:

| taco_area |
|-----------|
| 16        |

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:

polygonPerimeterCartesian(polygon)

Arguments:

  • polygon (Polygon): The polygon for which to calculate the perimeter.

Returns:

The perimeter of the polygon as a Float64 value.

Example:

SELECT
  polygonPerimeterCartesian(
      [
          [tuple(0.0, 0.0), tuple(0.0, 5.0), tuple(5.0, 5.0), tuple(5.0, 0.0), tuple(0.0, 0.0)]
      ]
  ) AS perimeter;

Result:

| perimeter |
|-----------|
| 20        |

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:

polygonsUnionCartesian(polygon1, polygon2)

Arguments:

  • polygon1 (Polygon): The first polygon.
  • polygon2 (Polygon): The second polygon.

Returns:

A MultiPolygon representing the union of the input polygons.

Example:

SELECT
  wkt(polygonsUnionCartesian(
      [
          [tuple(0.0, 0.0), tuple(0.0, 3.0), tuple(3.0, 3.0), tuple(3.0, 0.0)]
      ],
      [
          [tuple(2.0, 2.0), tuple(2.0, 5.0), tuple(5.0, 5.0), tuple(5.0, 2.0)]
      ]
  )) AS taco_territory_union;

Result:

| taco_territory_union                                        |
|-------------------------------------------------------------|
| MULTIPOLYGON(((2 3,2 5,5 5,5 2,3 2,3 0,0 0,0 3,2 3)))       |

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.