# GeoSpatial Functions

For manipulating geospatial data, Pinot provides a set of functions for analyzing geometric components, determining spatial relationships, and manipulating geometries. In particular, geospatial functions that begin with the `ST_` prefix support the SQL/MM specification.

Following geospatial functions are available out of the box in Pinot:

### Aggregations

#### [**ST\_Union(geometry\[\] g1\_array) → Geometry**](https://docs.pinot.apache.org/functions/geospatial/stunion)

This aggregate function returns a MULTI geometry or NON-MULTI geometry from a set of geometries. it ignores NULL geometries.

### Constructors

#### [**ST\_GeomFromText(String wkt) → Geometry**](https://docs.pinot.apache.org/functions/geospatial/stgeomfromtext)

Returns a geometry type object from WKT representation, with the optional spatial system reference.

#### [**ST\_GeomFromWKB(bytes wkb) → Geometry**](https://docs.pinot.apache.org/functions/geospatial/stgeomfromwkb)

Returns a geometry type object from WKB representation.

#### [**ST\_Point(double x, double y) → Point**](https://docs.pinot.apache.org/functions/geospatial/stpoint)

Returns a geometry type point object with the given coordinate values.

#### [**ST\_Polygon(String wkt) → Polygon**](https://docs.pinot.apache.org/functions/geospatial/stpolygon)

Returns a geometry type polygon object from [WKT representation](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry).

#### [**ST\_GeogFromWKB(bytes wkb) → Geography**](https://docs.pinot.apache.org/functions/geospatial/stgeogfromwkb)

Creates a geography instance from a [Well-Known Binary geometry representation (WKB)](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary)

#### [**ST\_GeogFromText(String wkt) → Geography**](https://docs.pinot.apache.org/functions/geospatial/stgeogfromtext)

Returns a specified geography value from [Well-Known Text representation or extended (WKT)](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry).

### Measurements

#### **ST\_Area(Geometry/Geography g) → double**

For geometry type, it returns the 2D Euclidean area of a geometry. For geography, returns the area of a polygon or multi-polygon in square meters using a spherical model for Earth.

#### [**ST\_Distance(Geometry/Geography g1, Geometry/Geography g2) → double**](https://docs.pinot.apache.org/functions/geospatial/stdistance)

For geometry type, returns the 2-dimensional cartesian minimum distance (based on spatial ref) between two geometries in projected units. For geography, returns the great-circle distance in meters between two SphericalGeography points. Note that g1, g2 shall have the same type.

#### [**ST\_GeometryType(Geometry g) → String**](https://docs.pinot.apache.org/functions/geospatial/stgeometrytype)

Returns the type of the geometry as a string. e.g.: `ST_Linestring`, `ST_Polygon`,`ST_MultiPolygon` etc.

### Outputs

#### [**ST\_AsBinary(Geometry/Geography g) → bytes**](https://docs.pinot.apache.org/functions/geospatial/stasbinary)

Returns the WKB representation of the geometry.

#### [**ST\_AsText(Geometry/Geography g) → string**](https://docs.pinot.apache.org/functions/geospatial/stastext)

Returns the WKT representation of the geometry/geography.

### Conversion

#### [**toSphericalGeography(Geometry g) → Geography**](https://docs.pinot.apache.org/functions/geospatial/tosphericalgeography)

Converts a Geometry object to a spherical geography object.

#### [**toGeometry(Geography g) → Geometry**](https://docs.pinot.apache.org/functions/geospatial/togeometry)

Converts a spherical geographical object to a Geometry object.

### Relationship

#### [**ST\_Contains(Geometry/Geography, Geometry/Geography) → boolean**](https://docs.pinot.apache.org/functions/geospatial/stcontains)

Returns true if and only if no points of the second geometry/geography lie in the exterior of the first geometry/geography, and at least one point of the interior of the first geometry lies in the interior of the second geometry. **Warning: ST\_Contains on Geography only give close approximation**

#### **ST\_Equals(Geometry, Geometry) → boolean**

Returns true if the given geometries represent the same geometry/geography.

#### **ST\_Within(Geometry, Geometry) → boolean**

Returns true if first geometry is completely inside second geometry.

## Additional Reference Pages

| Function                                                                                     | Function                                                                                     |
| -------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- |
| [ST\_Union](https://docs.pinot.apache.org/functions/geospatial/stunion)                      | [GridDistance](https://docs.pinot.apache.org/functions/geospatial/griddistance)              |
| [GridDisk](https://docs.pinot.apache.org/functions/geospatial/griddisk)                      | [ST\_AsGeoJSON](https://docs.pinot.apache.org/functions/geospatial/st_asgeojson)             |
| [ST\_GeogFromGeoJSON](https://docs.pinot.apache.org/functions/geospatial/st_geogfromgeojson) | [ST\_GeomFromGeoJSON](https://docs.pinot.apache.org/functions/geospatial/st_geomfromgeojson) |
