---
title: st_collect
description: "st\\_collect function in PySpark: Collects an array of Geography or Geometry values into a single multipoint, multilinestring, multipolygon, or geometry collection."
canonical_url: https://docs.databricks.com/gcp/en/pyspark/reference/functions/st_collect
---

# st\_collect

**Applies to:** ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks Runtime 18 LTS and above

> [!NOTE]
>
> Databricks Runtime 18 is newer than Databricks Runtime 18.0, 18.1, and 18.2. Features that would previously have shipped as a later numbered version now ship as dated updates to Databricks Runtime 18 instead. For details, see [About unified release notes](https://docs.databricks.com/gcp/en/release-notes/runtime/18.md#unified-format).

> [!NOTE]
> **Preview**
>
> This feature is in [Public Preview](https://docs.databricks.com/gcp/en/release-notes/release-types.md).

Collects an array of [Geography](https://docs.databricks.com/gcp/en/pyspark/reference/classes/geography.md) or [Geometry](https://docs.databricks.com/gcp/en/pyspark/reference/classes/geometry.md) values into a single multipoint, multilinestring, multipolygon, or geometry collection.

For the corresponding Databricks SQL function, see [`st_collect` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/st_collect.md).

## Syntax

```python
from pyspark.databricks.sql import functions as dbf

dbf.st_collect(col=<col>)
```

## Parameters

| Parameter | Type                          | Description                                                                                                                                                                                                            |
| --------- | ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `col`     | `pyspark.sql.Column` or `str` | An array of [Geography](https://docs.databricks.com/gcp/en/pyspark/reference/classes/geography.md) values, or an array of [Geometry](https://docs.databricks.com/gcp/en/pyspark/reference/classes/geometry.md) values. |

## Returns

`pyspark.sql.Column`: A [Geography](https://docs.databricks.com/gcp/en/pyspark/reference/classes/geography.md) or [Geometry](https://docs.databricks.com/gcp/en/pyspark/reference/classes/geometry.md) value, representing a multipoint, multilinestring, multipolygon, or geometry collection.

Any `None` values in the input array are ignored. The type of the output depends on the types of the non-`None` input geometries:

* If all non-`None` elements are points, returns a multipoint.
* If all non-`None` elements are linestrings, returns a multilinestring.
* If all non-`None` elements are polygons, returns a multipolygon.
* Otherwise, returns a geometry collection.

Each output contains one element per non-`None` array element.

Multi-typed inputs (multipoint, multilinestring, multipolygon) and geometry collection inputs are preserved as elements of the resulting geometry collection; they are not flattened.

The SRID value of the output is the common SRID value of the non-`None` input geometries.

The dimension of the output is the maximum common dimension of the non-`None` input geometries.

If the input array is empty or contains only `None` values, the 2D empty geometry collection is returned. In this case, the SRID of the output is determined as follows:

* If the input array's element type is `GEOGRAPHY(ANY)`, the SRID of the output is `4326`.
* If the input array's element type is `GEOMETRY(ANY)`, the SRID of the output is `0`.
* Otherwise, the SRID of the output is that of the input array's element type.

If any two non-`None` input geometries have different SRID values, the function raises a [ST\_DIFFERENT\_SRID\_VALUES](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#st_different_srid_values) error.

The function returns `None` if the input is `None`.

## Examples

Collects an array of points into a multipoint.

```python
from pyspark.databricks.sql import functions as dbf
from pyspark.sql import functions as sf
df = spark.createDataFrame([('POINT(1 2)', 'POINT(3 4)')], ['wkt1', 'wkt2'])
df.select(dbf.st_astext(dbf.st_collect(sf.array(dbf.st_geomfromtext('wkt1'), dbf.st_geomfromtext('wkt2')))).alias('result')).collect()
```

```Output
[Row(result='MULTIPOINT((1 2),(3 4))')]
```

Collects an array of polygons into a multipolygon.

```python
from pyspark.databricks.sql import functions as dbf
from pyspark.sql import functions as sf
df = spark.createDataFrame([('POLYGON((0 0,10 0,10 10,0 10,0 0))',)], ['wkt'])
df.select(dbf.st_astext(dbf.st_collect(sf.array(dbf.st_geomfromtext('wkt')))).alias('result')).collect()
```

```Output
[Row(result='MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)))')]
```

Collects an array of mixed geometry kinds into a geometry collection.

```python
from pyspark.databricks.sql import functions as dbf
from pyspark.sql import functions as sf
df = spark.createDataFrame([('POLYGON((0 0,10 0,10 10,0 10,0 0))', 'LINESTRING(1 2,3 4)')], ['wkt1', 'wkt2'])
df.select(dbf.st_astext(dbf.st_collect(sf.array(dbf.st_geomfromtext('wkt1'), dbf.st_geomfromtext('wkt2')))).alias('result')).collect()
```

```Output
[Row(result='GEOMETRYCOLLECTION(POLYGON((0 0,10 0,10 10,0 10,0 0)),LINESTRING(1 2,3 4))')]
```

Returns the 2D empty geometry collection for an empty input array.

```python
from pyspark.databricks.sql import functions as dbf
from pyspark.sql import functions as sf
df = spark.range(1)
df.select(dbf.st_astext(dbf.st_collect(sf.array())).alias('result')).collect()
```

```Output
[Row(result='GEOMETRYCOLLECTION EMPTY')]
```

Returns `None` for a `None` input.

```python
from pyspark.databricks.sql import functions as dbf
from pyspark.sql import functions as sf
df = spark.range(1)
df.select(dbf.st_collect(sf.lit(None)).alias('result')).collect()
```

```Output
[Row(result=None)]
```
