Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
---
{
"title": "ST_GEOMETRIES",
"language": "en",
"description": "Returns an array of sub-geometries from a geometry collection."
}
---

## Description

Decomposes a geometry object into an array of its sub-geometries. For collection types (MultiPolygon), it returns each sub-polygon as a separate element in the array. For non-collection types (Point, LineString, Polygon, Circle), it returns a single-element array containing the geometry itself.

## Syntax

```sql
ST_GEOMETRIES( <shape> )
```

## Parameters

| Parameter | Description |
| :--- | :--- |
| `<shape>` | The input geometry, of type GEOMETRY or VARCHAR (in WKT format) that can be converted to GEOMETRY. |

## Return Value

Returns an `ARRAY<STRING>` where each element is an encoded geometry object that can be used with other spatial functions such as `ST_AsText` or `ST_GeometryType`.

`ST_GEOMETRIES` has the following edge cases:
- If the input parameter is `NULL`, returns `NULL`.
- If the input parameter cannot be parsed into a valid geometry object, returns `NULL`.
- For non-collection types (`POINT`, `LINESTRING`, `POLYGON`, `CIRCLE`), returns a single-element array containing the input geometry.
- For `MULTIPOLYGON`, returns an array where each element is one of the sub-polygons.
- Array elements can be passed to other spatial functions for further processing.

## Example

**Geometries of a Point (single-element array)**
```sql
SELECT ST_AsText(ST_GEOMETRIES(ST_Point(1, 2))[1]);
```
```text
+------------------------------------------------+
| ST_AsText(ST_GEOMETRIES(ST_Point(1, 2))[1]) |
+------------------------------------------------+
| POINT (1 2) |
+------------------------------------------------+
```

**Geometries of a Polygon (single-element array)**
```sql
SELECT ST_GeometryType(ST_GEOMETRIES(ST_GeometryFromText('POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))'))[1]);
```
```text
+----------------------------------------------------------------------------------------------------+
| ST_GeometryType(ST_GEOMETRIES(ST_GeometryFromText('POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))'))[1]) |
+----------------------------------------------------------------------------------------------------+
| ST_POLYGON |
+----------------------------------------------------------------------------------------------------+
```

**Geometries of a MultiPolygon (multiple elements)**
```sql
SELECT SIZE(ST_GEOMETRIES(ST_GeometryFromText('MULTIPOLYGON(((0 0, 1 0, 1 1, 0 1, 0 0)),((2 2, 3 2, 3 3, 2 3, 2 2)))')));
```
```text
+----------------------------------------------------------------------------------------------------------------------+
| SIZE(ST_GEOMETRIES(ST_GeometryFromText('MULTIPOLYGON(((0 0, 1 0, 1 1, 0 1, 0 0)),((2 2, 3 2, 3 3, 2 3, 2 2)))))') |
+----------------------------------------------------------------------------------------------------------------------+
| 2 |
+----------------------------------------------------------------------------------------------------------------------+
```

**Accessing individual elements of a MultiPolygon**
```sql
SELECT ST_AsText(ST_GEOMETRIES(ST_GeometryFromText('MULTIPOLYGON(((0 0, 1 0, 1 1, 0 1, 0 0)),((2 2, 3 2, 3 3, 2 3, 2 2)))'))[1]);
```
```text
+----------------------------------------------------------------------------------------------------------------------------------+
| ST_AsText(ST_GEOMETRIES(ST_GeometryFromText('MULTIPOLYGON(((0 0, 1 0, 1 1, 0 1, 0 0)),((2 2, 3 2, 3 3, 2 3, 2 2)))'))[1]) |
+----------------------------------------------------------------------------------------------------------------------------------+
| POLYGON ((1 0, 1 1, 0 1, 0 0, 1 0)) |
+----------------------------------------------------------------------------------------------------------------------------------+
```

```sql
SELECT ST_AsText(ST_GEOMETRIES(ST_GeometryFromText('MULTIPOLYGON(((0 0, 1 0, 1 1, 0 1, 0 0)),((2 2, 3 2, 3 3, 2 3, 2 2)))'))[2]);
```
```text
+----------------------------------------------------------------------------------------------------------------------------------+
| ST_AsText(ST_GEOMETRIES(ST_GeometryFromText('MULTIPOLYGON(((0 0, 1 0, 1 1, 0 1, 0 0)),((2 2, 3 2, 3 3, 2 3, 2 2)))'))[2]) |
+----------------------------------------------------------------------------------------------------------------------------------+
| POLYGON ((3 2, 3 3, 2 3, 2 2, 3 2)) |
+----------------------------------------------------------------------------------------------------------------------------------+
```

**Out-of-bound index (Returns NULL)**
```sql
SELECT ST_GEOMETRIES(ST_GeometryFromText('MULTIPOLYGON(((0 0, 1 0, 1 1, 0 1, 0 0)),((2 2, 3 2, 3 3, 2 3, 2 2)))'))[3];
```
```text
+----------------------------------------------------------------------------------------------------------------------------+
| ST_GEOMETRIES(ST_GeometryFromText('MULTIPOLYGON(((0 0, 1 0, 1 1, 0 1, 0 0)),((2 2, 3 2, 3 3, 2 3, 2 2)))'))[3] |
+----------------------------------------------------------------------------------------------------------------------------+
| NULL |
+----------------------------------------------------------------------------------------------------------------------------+
```

**NULL Parameter**
```sql
SELECT ST_GEOMETRIES(NULL);
```
```text
+----------------------+
| ST_GEOMETRIES(NULL) |
+----------------------+
| NULL |
+----------------------+
```

**Invalid Parameter (Returns NULL)**
```sql
SELECT ST_GEOMETRIES(ST_GeometryFromText('INVALID'));
```
```text
+----------------------------------------------------+
| ST_GEOMETRIES(ST_GeometryFromText('INVALID')) |
+----------------------------------------------------+
| NULL |
+----------------------------------------------------+
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
{
"title": "ST_NUMGEOMETRIES",
"language": "en",
"description": "Returns the number of geometries in a geometry collection."
}
---

## Description

Returns the number of sub-geometries contained in a geometry object. For non-collection types (Point, LineString, Polygon, Circle), it always returns 1. For collection types (MultiPolygon), it returns the number of sub-geometries in the collection.

## Syntax

```sql
ST_NUMGEOMETRIES( <shape> )
```

## Parameters

| Parameter | Description |
| :--- | :--- |
| `<shape>` | The input geometry, of type GEOMETRY or VARCHAR (in WKT format) that can be converted to GEOMETRY. |

## Return Value

Returns a BIGINT value representing the number of sub-geometries in the geometry object.

`ST_NUMGEOMETRIES` has the following edge cases:
- If the input parameter is `NULL`, returns `NULL`.
- If the input parameter cannot be parsed into a valid geometry object, returns `NULL`.
- For non-collection types (`POINT`, `LINESTRING`, `POLYGON`, `CIRCLE`), always returns `1`.
- For `MULTIPOLYGON`, returns the number of polygons in the collection.

## Example

**Number of geometries in a Point**
```sql
SELECT ST_NUMGEOMETRIES(ST_Point(1, 2));
```
```text
+----------------------------------+
| ST_NUMGEOMETRIES(ST_Point(1, 2)) |
+----------------------------------+
| 1 |
+----------------------------------+
```

**Number of geometries in a Polygon**
```sql
SELECT ST_NUMGEOMETRIES(ST_GeometryFromText('POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))'));
```
```text
+------------------------------------------------------------------------------+
| ST_NUMGEOMETRIES(ST_GeometryFromText('POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))')) |
+------------------------------------------------------------------------------+
| 1 |
+------------------------------------------------------------------------------+
```

**Number of geometries in a MultiPolygon with two polygons**
```sql
SELECT ST_NUMGEOMETRIES(ST_GeometryFromText('MULTIPOLYGON(((0 0, 1 0, 1 1, 0 1, 0 0)),((2 2, 3 2, 3 3, 2 3, 2 2)))'));
```
```text
+------------------------------------------------------------------------------------------------------------------+
| ST_NUMGEOMETRIES(ST_GeometryFromText('MULTIPOLYGON(((0 0, 1 0, 1 1, 0 1, 0 0)),((2 2, 3 2, 3 3, 2 3, 2 2)))')) |
+------------------------------------------------------------------------------------------------------------------+
| 2 |
+------------------------------------------------------------------------------------------------------------------+
```

**Number of geometries in a Circle**
```sql
SELECT ST_NUMGEOMETRIES(ST_Circle(0, 0, 100));
```
```text
+----------------------------------------+
| ST_NUMGEOMETRIES(ST_Circle(0, 0, 100)) |
+----------------------------------------+
| 1 |
+----------------------------------------+
```

**NULL Parameter**
```sql
SELECT ST_NUMGEOMETRIES(NULL);
```
```text
+------------------------+
| ST_NUMGEOMETRIES(NULL) |
+------------------------+
| NULL |
+------------------------+
```

**Invalid Parameter (Returns NULL)**
```sql
SELECT ST_NUMGEOMETRIES(ST_GeometryFromText('INVALID'));
```
```text
+--------------------------------------------------+
| ST_NUMGEOMETRIES(ST_GeometryFromText('INVALID')) |
+--------------------------------------------------+
| NULL |
+--------------------------------------------------+
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
---
{
"title": "ST_NUMPOINTS",
"language": "en",
"description": "Returns the total number of points in a geometry object."
}
---

## Description

Returns the total number of vertices (points) in a geometry object. For different geometry types, the counting rules are as follows: a Point returns 1, a LineString returns the number of vertices, a Polygon returns the total number of vertices across all rings (including the closing point), and a MultiPolygon returns the sum of all sub-polygon point counts.

## Syntax

```sql
ST_NUMPOINTS( <shape> )
```

## Parameters

| Parameter | Description |
| :--- | :--- |
| `<shape>` | The input geometry, of type GEOMETRY or VARCHAR (in WKT format) that can be converted to GEOMETRY. |

## Return Value

Returns a BIGINT value representing the total number of points in the geometry object.

`ST_NUMPOINTS` has the following edge cases:
- If the input parameter is `NULL`, returns `NULL`.
- If the input parameter cannot be parsed into a valid geometry object, returns `NULL`.
- For `POINT`, returns `1`.
- For `LINESTRING`, returns the number of vertices in the line.
- For `POLYGON`, returns the total number of vertices across all rings (exterior ring + interior rings). Each ring includes the closing point.
- For `MULTIPOLYGON`, returns the sum of point counts of all sub-polygons.
- For `CIRCLE`, returns `-1` (circles do not have discrete vertices).

## Example

**Number of points in a Point**
```sql
SELECT ST_NUMPOINTS(ST_Point(1, 2));
```
```text
+------------------------------+
| ST_NUMPOINTS(ST_Point(1, 2)) |
+------------------------------+
| 1 |
+------------------------------+
```

**Number of points in a LineString**
```sql
SELECT ST_NUMPOINTS(ST_GeometryFromText('LINESTRING(0 0, 1 1, 2 2)'));
```
```text
+-------------------------------------------------------------------+
| ST_NUMPOINTS(ST_GeometryFromText('LINESTRING(0 0, 1 1, 2 2)')) |
+-------------------------------------------------------------------+
| 3 |
+-------------------------------------------------------------------+
```

**Number of points in a Polygon (square)**
```sql
SELECT ST_NUMPOINTS(ST_GeometryFromText('POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))'));
```
```text
+----------------------------------------------------------------------------+
| ST_NUMPOINTS(ST_GeometryFromText('POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))')) |
+----------------------------------------------------------------------------+
| 5 |
+----------------------------------------------------------------------------+
```

**Number of points in a Polygon with a hole**
```sql
SELECT ST_NUMPOINTS(ST_GeometryFromText('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0),(1 1, 2 1, 2 2, 1 2, 1 1))'));
```
```text
+-----------------------------------------------------------------------------------------------------------+
| ST_NUMPOINTS(ST_GeometryFromText('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0),(1 1, 2 1, 2 2, 1 2, 1 1))')) |
+-----------------------------------------------------------------------------------------------------------+
| 10 |
+-----------------------------------------------------------------------------------------------------------+
```

**Number of points in a MultiPolygon**
```sql
SELECT ST_NUMPOINTS(ST_GeometryFromText('MULTIPOLYGON(((0 0, 1 0, 1 1, 0 1, 0 0)),((2 2, 3 2, 3 3, 2 3, 2 2)))'));
```
```text
+--------------------------------------------------------------------------------------------------------------+
| ST_NUMPOINTS(ST_GeometryFromText('MULTIPOLYGON(((0 0, 1 0, 1 1, 0 1, 0 0)),((2 2, 3 2, 3 3, 2 3, 2 2)))')) |
+--------------------------------------------------------------------------------------------------------------+
| 10 |
+--------------------------------------------------------------------------------------------------------------+
```

**Number of points in a Circle (returns -1)**
```sql
SELECT ST_NUMPOINTS(ST_Circle(0, 0, 100));
```
```text
+--------------------------------------+
| ST_NUMPOINTS(ST_Circle(0, 0, 100)) |
+--------------------------------------+
| -1 |
+--------------------------------------+
```

**NULL Parameter**
```sql
SELECT ST_NUMPOINTS(NULL);
```
```text
+---------------------+
| ST_NUMPOINTS(NULL) |
+---------------------+
| NULL |
+---------------------+
```

**Invalid Parameter (Returns NULL)**
```sql
SELECT ST_NUMPOINTS(ST_GeometryFromText('INVALID'));
```
```text
+------------------------------------------------+
| ST_NUMPOINTS(ST_GeometryFromText('INVALID')) |
+------------------------------------------------+
| NULL |
+------------------------------------------------+
```