> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Functions for Working with Geometry

> Documentation for Geometry Functions

## Geometry

Geometry functions allow you to calculate perimeter, area and length for geometric types such as POLYGON, LINESTRING, MULTIPOLYGON, MULTILINESTRING, RING, and POINT. Use geometries in Geometry type. If the input value is `NULL`, all functions below will return 0.

## perimeterCartesian

Calculates the perimeter of the given Geometry object in the Cartesian (flat) coordinate system.

**Syntax**

```sql theme={null}
perimeterCartesian(geom)
```

**Arguments**

* `geom` — Geometry object. [Geometry](https://clickhouse.com/docs/reference/data-types/geo.md).

**Returned values**

* Number — Perimeter of the object in the coordinate system units. [Float64](https://clickhouse.com/docs/reference/data-types/float.md).

**Example**

```sql title="Query" theme={null}
CREATE TABLE IF NOT EXISTS geo_dst (geom Geometry) ENGINE = Memory();
INSERT INTO geo_dst SELECT readWKT('POLYGON((0 0,1 0,1 1,0 1,0 0))');
SELECT perimeterCartesian(geom) FROM geo_dst;
```

```response title="Response" theme={null}
┌─perimeterCartesian(geom)─┐
│ 4.0                      │
└──────────────────────────┘
```

## areaCartesian

Calculates the area of the given Geometry object in the Cartesian coordinate system.

**Syntax**

```sql theme={null}
areaCartesian(geom)
```

**Arguments**

* `geom` — Geometry object. [Geometry](https://clickhouse.com/docs/reference/data-types/geo.md).

**Returned values**

* Number — Area of the object in coordinate system units. [Float64](https://clickhouse.com/docs/reference/data-types/float.md).

**Example**

```sql title="Query" theme={null}
CREATE TABLE IF NOT EXISTS geo_dst (geom Geometry) ENGINE = Memory();
INSERT INTO geo_dst SELECT readWKT('POLYGON((0 0,1 0,1 1,0 1,0 0))');
SELECT areaCartesian(geom) FROM geo_dst;
```

```response title="Response" theme={null}
┌─areaCartesian(geom)─┐
│ -1                  │
└─────────────────────┘
```

## perimeterSpherical

Calculates the perimeter of a Geometry object on the surface of a sphere.

**Syntax**

```sql theme={null}
perimeterSpherical(geom)
```

**Arguments**

* `geom` — Geometry object. [Geometry](https://clickhouse.com/docs/reference/data-types/geo.md).

**Returned values**

* Number — Perimeter on a sphere of radius 1, in radians (coordinates are longitude and latitude in degrees). Multiply by the radius of the sphere to get a length, for example by 6371 for kilometers on the Earth. [Float64](https://clickhouse.com/docs/reference/data-types/float.md).

**Example**

```sql title="Query" theme={null}
CREATE TABLE IF NOT EXISTS geo_dst (geom Geometry) ENGINE = Memory();
INSERT INTO geo_dst SELECT readWKT('LINESTRING(0 0,1 0,1 1,0 1,0 0)');
SELECT perimeterSpherical(geom) FROM geo_dst;
```

```response title="Response" theme={null}
┌─perimeterSpherical(geom)─┐
│      0.06981051179132045 │
└──────────────────────────┘
```

## areaSpherical

Calculates the area of a Geometry object on the surface of a sphere.

**Syntax**

```sql theme={null}
areaSpherical(geom)
```

**Arguments**

* `geom` — Geometry. [Geometry](https://clickhouse.com/docs/reference/data-types/geo.md).

**Returned values**

* Number — Area on a sphere of radius 1, in steradians (coordinates are longitude and latitude in degrees). Multiply by the square of the radius of the sphere to get an area. [Float64](https://clickhouse.com/docs/reference/data-types/float.md).

**Example**

```sql title="Query" theme={null}
CREATE TABLE IF NOT EXISTS geo_dst (geom Geometry) ENGINE = Memory();
INSERT INTO geo_dst SELECT readWKT('POLYGON((0 0,1 0,1 1,0 1,0 0))');
SELECT areaSpherical(geom) FROM geo_dst;
```

```response title="Response" theme={null}
┌─areaSpherical(geom)────┐
│ -0.0003046096848622019 │
└────────────────────────┘
```

## lengthCartesian

Calculates the length of the lines of a Geometry object in the Cartesian (flat) coordinate system: the sum of the distances between consecutive points.
A `MultiLineString` has the total length of its lines, and a `Ring` is measured as the line through its points, as is an array of points without a type name, which has the type `Ring`.
Points, polygons and multipolygons have no length, so the function returns 0 for them: use [perimeterCartesian](#perimetercartesian) for the perimeter of a polygon.
To measure an array of arrays of points, cast it to `MultiLineString`: without a type name, it has the type `Polygon`.

Alias: `ST_Length`.

**Syntax**

```sql theme={null}
lengthCartesian(geom)
```

**Arguments**

* `geom` — Geometry object. [Geometry](https://clickhouse.com/docs/reference/data-types/geo.md).

**Returned values**

* Number — Length of the lines in the coordinate system units. [Float64](https://clickhouse.com/docs/reference/data-types/float.md).

**Example**

```sql title="Query" theme={null}
SELECT lengthCartesian(readWKT('LINESTRING(0 0,3 4,3 10)')) AS length;
```

```response title="Response" theme={null}
┌─length─┐
│     11 │
└────────┘
```

## lengthSpherical

Calculates the length of the lines of a Geometry object on the surface of a sphere, with points given as (longitude, latitude) in degrees.
The result is an angle in radians, which is the length on a sphere of radius 1, like the result of [perimeterSpherical](#perimeterspherical).
Multiply it by the mean radius of the Earth, 6371008.8 meters, for an approximate length in meters, or use [lengthSpheroid](#lengthspheroid) for an accurate one.
Like [lengthCartesian](#lengthcartesian), it returns 0 for points, polygons and multipolygons.

**Syntax**

```sql theme={null}
lengthSpherical(geom)
```

**Arguments**

* `geom` — Geometry object. [Geometry](https://clickhouse.com/docs/reference/data-types/geo.md).

**Returned values**

* Number — Length of the lines in radians. [Float64](https://clickhouse.com/docs/reference/data-types/float.md).

**Example**

```sql title="Query" theme={null}
SELECT lengthSpherical(readWKT('LINESTRING(0 0,1 0)')) AS length;
```

```response title="Response" theme={null}
┌───────────────length─┐
│ 0.017453292519943292 │
└──────────────────────┘
```

## lengthSpheroid

Calculates the length of the lines of a Geometry object in meters on the WGS84 spheroid, the model of the Earth used by GPS, with points given as (longitude, latitude) in degrees.
Each segment is the shortest path between its points on the spheroid (a geodesic), calculated with Karney's algorithm, as in GeographicLib.
Like [lengthCartesian](#lengthcartesian), it returns 0 for points, polygons and multipolygons.
For coordinates stored as (latitude, longitude), such as the input of `ST_Length_Spheroid` in DuckDB, swap them first with [flipCoordinates](https://clickhouse.com/docs/reference/functions/regular-functions/geo/flipCoordinates.md): without it, the result is the length of a different line.
If a coordinate is NaN or infinite, or a latitude is outside \[-90, 90], the result is NaN.

**Syntax**

```sql theme={null}
lengthSpheroid(geom)
```

**Arguments**

* `geom` — Geometry object. [Geometry](https://clickhouse.com/docs/reference/data-types/geo.md).

**Returned values**

* Number — Length of the lines in meters. [Float64](https://clickhouse.com/docs/reference/data-types/float.md).

**Example**

The length of a line from London through Paris to Berlin:

```sql title="Query" theme={null}
SELECT lengthSpheroid(readWKT('LINESTRING(-0.1276 51.5072,2.3522 48.8566,13.405 52.52)')) AS length;
```

```response title="Response" theme={null}
┌─────────────length─┐
│ 1223596.2073957825 │
└────────────────────┘
```
