Spatial Operators
spatial operators perform geometry calculations on geospatial data to return a different type of geospatial data.
Returns the diagonal LINESTRING from the minimum point to the maximum point of the bounding box that ST_ENVELOPE returns.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo | POINT, LINESTRING, or POLYGON | A geospatial object to be measured for the diagonal length of its bounding box. |
Example
Output: LINESTRING(1 2,1 3)
Returns a geography that contains all points where the distance from the geography is less than or equal to the specified distance. The function simplifies the geometry before buffering unless you specify the simplification_tolerance parameter with a zero or negative value.
The output of the function is an array of polygons. For POINT and LINESTRING data types, the returned array contains one polygon only.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo | POINT, LINESTRING, or POLYGON | A geospatial object, specified as a point, linestring, or polygon, which the function buffers. |
radius | DOUBLE | Radius, specified as a double, that indicates the distance to buffer the geospatial object in meters. Negative values cause polygons to shrink. Also, negative values cause the result to be empty for points and linestrings. |
num_segments_per_quarter_circle | BIGINT | Optional. The number of segments to use per quarter circle when parts of the buffer are round, specified as a BIGINT. Only use if the join_style or endcap_style value is set to ROUND. Defaults to 8. |
simplification_tolerance | DOUBLE | Optional. Value that the function passes to the ST_SIMPLIFYARRAY function prior to buffering. If you specify a zero or negative value, the function skips simplification of the geometry. |
maximum_radius_error | DOUBLE | Optional. Maximum radius error, specified as a double. The returned buffer might be slightly larger than the radius value by this amount. This value is required because arcs of great circles cannot be parallel. Use this argument for linestrings and polygons only. Defaults to 10.0. |
join_style | STRING | Optional. The type of join to use at the corners where segments of the geometry meet, specified as a string. Supported styles are ROUND, BEVEL and MITER. Use this argument for linestrings and polygons only. Defaults to ROUND. |
miter_limit | DOUBLE | Optional. Miter limit, specified as a double. Use this argument only if the join_style value is set to MITER. A mitered corner is cut off at a distance of miter_ratio * radius from the corner point of the geometry. Use this argument for linestrings and polygons only. Defaults to 1.0. |
endcap_style | STRING | Optional. The type of end cap for use at the ends of linestrings, specified as a string. Supported styles are ROUND, SQUARE, and FLAT. Use this argument for linestrings only. Defaults to ROUND. |
side_style | STRING | Optional. The side, specified as a string, that denotes the side for buffering the geometry. Supported styles are BOTH, LEFT, and RIGHT. Use this argument for linestrings only. Defaults to BOTH. |
Example
Output:
[POLYGON((2.202700241764562E-18 -0.017986411844962975, 1.0179863625702694 -0.0179863616840571, 1.0179892379654352 1.031486286416526, 1.009550207729886 1.0349816974070565, -0.03498171178141835 -0.009548649645081482, -0.03148633724650228 -0.01798632287046757, 2.202700241764562E-18 -0.017986411844962975))]
Returns the two-dimensional POINT of one specified geospatial object that is closest to a second specified geospatial object. This function works with any geospatial type.
If either geospatial argument is empty, the function returns NULL.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo1 | POINT, LINESTRING, or POLYGON | A geospatial object to be computed to determine its closest POINT value relative to geo2. |
geo2 | POINT, LINESTRING, or POLYGON | A second geospatial object to be compared to geo1 to find the closest POINT value of geo1. |
use_spheroid | BOOLEAN | Optional. If you set this value to TRUE, this function uses a spheroid model instead of a spherical model. Defaults to FALSE. |
Example
Output: POINT(2,3)
Returns a POLYGON representing the convex hull of the input geometry.
The convex hull is the smallest convex geometry that encloses the input geometry. If the geo argument is empty, the function returns an empty POLYGON.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo | POINT, LINESTRING, or POLYGON | A geospatial object to be computed for a POLYGON of its convex hull. If geo is a POINT or a single-point LINESTRING or POLYGON, the function returns a POLYGON containing only that point value. If geo is NULL, the function returns NULL. |
Example
Output: POLYGON((0.0 0.0, 1.0 0.0, 1.0 1.0, 0.0 1.0, 0.0 0.0))
Returns an array containing any geospatial objects that are present in the first specified geospatial argument that are not found in the second geospatial argument.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo1 | POINT, LINESTRING, or POLYGON | A geospatial object to be compared to geo2 to find any unique geospatial values. |
geo2 | POINT, LINESTRING, or POLYGON | A second geospatial object used to compare with geo1 to find unique values. The function captures only objects that are unique to geo1, and not those unique to geo2. |
Example
In this example, the function returns LINESTRING(2.0 2.0, 3.0 3.0) because that portion of the first LINESTRING argument is not present in the second argument.
Output: [LINESTRING(2.0 2.0, 3.0 3.0)]
Returns a POLYGON that represents the minimum bounding box for the specified geography. If the specified argument is empty, the function returns an empty POLYGON.
A single POINT argument results in a single POINT.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo | POINT, LINESTRING, or POLYGON | A geospatial object to be computed for the vertices of its bounding box. |
Example
Output: POLYGON((0 0,10 0,10 10,0 10,0 0))
Returns the bounding box of a specified geospatial value, which is expanded by a specified length. The unit of length is in meters.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo | POINT, LINESTRING, or POLYGON | A geospatial object to be computed for the vertices of its bounding box with the added units_to_expand. |
units_to_expand | DOUBLE | An additional distance to expand the bounding box of geo. Specify this argument only in meters. |
Example
Output: POLYGON((0.99 0.99, 1.33 0.99, 1.66 0.99, 2.01 0.99, 2.00 2.00, 1.66 2.00, 1.33 2.00, 0.99 2.00, 0.99 0.99))
Returns a LINESTRING that represents the exterior ring of a provided POLYGON value. If you specify a geography value that is not a POLYGON, the function returns NULL.
Syntax
Parameter | Data Type | Description |
---|---|---|
polygon | POLYGON | A POLYGON object to be computed for a LINESTRING of its perimeter. |
Example
Output: LINESTRING(1.0 1.0, 3.0 1.0, 2.0 2.0, 1.0 1.0)
Returns a new geographic object with the X and Y coordinates switched using the specified argument. The return type is the same as the specified type.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo | POINT, LINESTRING, or POLYGON | A geographic object for switching its X and Y coordinates. |
Example
Output: POINT(3.0 1.0)
Convert a geographic object into a two-dimensional geography. The output representations only have the X and Y coordinates.
The returned type is the same as the specified type.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo | POINT, LINESTRING, or POLYGON | A geographic object to be converted to a two-dimensional object. |
Example
Output: POINT(1.0 1.0)
Returns a LINESTRING representing the interior ring of the specified POLYGON, which is specified by its index. (1-indexed)
Returns NULL if the index is out of range, or if specified a non-POLYGON geography.
Syntax
Parameter | Data Type | Description |
---|---|---|
polygon | POLYGON | A POLYGON object to be computed for its interior ring. |
index | INTEGER | An index that represents the specific interior ring of the polygon value to be captured. Interior-ring indexes are 1-based, meaning the first object has an index of 1. |
Example
Output: LINESTRING(1 2)
Returns the intersection of all geographies in the specified array. All geographies in the array must be the same type. The ST_INTERSECTALL function ignores NULL values in the array. The output geography has the same type as the input geometries.
Syntax
Parameter | Data Type | Description |
---|---|---|
array | ARRAY of POINT, LINESTRING or POLYGON | An array of geographic objects, which all must be the same type. |
Example
Output: POINT[]
Returns a geography that represents the point-set intersection of two geographies.
If the two geographies are different types, the function returns an array of the smallest geography. For example, if geo1 is a POINT , and geo2 is a POLYGON, the function returns an array of POINT values.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo1 | POINT, LINESTRING, or POLYGON | A geographic object to be compared to geo2 to find a set of intersections. |
geo2 | POINT, LINESTRING, or POLYGON | A geographic object to be compared to geo1 to find a set of intersections. |
Example
This example compares two polygons to find any intersections. Because both objects are POLYGON types, the result is also a POLYGON containing all intersection points.
Output: [POLYGON((1, 2))]
Returns the longest LINESTRING between two given geospatial arguments.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo1 | POINT, LINESTRING, or POLYGON | A geospatial object for the determination of the longest LINESTRING relative to geo2. |
geo2 | POINT, LINESTRING, or POLYGON | A geospatial object the determination of the longest LINESTRING relative to geo1. |
use_spheroid | BOOLEAN | Optional. If you set this value to TRUE, this function uses a spheroid model instead of a spherical model. Defaults to FALSE. |
Example
Output: LINESTRING(0.0 3.0, 2.0 3.0)
To use ST_LONGESTLINE as a spatiotemporal command to calculate the longest line between two points at a concurrent time, see the ST_LONGESTLINE function.
Alias for ST_ENVELOPE(ST_LINESTRING(ST_POINT(xmin, ymin), ST_POINT(xmax, ymax))).
Returns a POLYGON with vertices that represent the minimum bounding box for the specified coordinates.
Syntax
Parameter | Data Type | Description |
---|---|---|
xmin | DOUBLE | The minimum x value for the bounding box. |
ymin | DOUBLE | The minimum y value for the bounding box. |
xmax | DOUBLE | The maximum x value for the bounding box. |
ymax | DOUBLE | The maximum y value for the bounding box. |
Example
Output: POLYGON((0.0 0.0, 10.0 0.0, 10.0 10.0, 0.0 10.0, 0.0 0.0))
Returns the smallest circle POLYGON that contains the specified geographic object.
The function requires an additional argument to define the number of segment for inclusion in each quarter of the returned circle.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo | POINT, LINESTRING, or POLYGON | A geospatial object for the determination of the smallest possible circle POLYGON to encompass it. |
num_segs_per_qt_circ | INTEGER | The number of segments to be included in each quarter of the returned circle POLYGON. |
Example
Output: POLYGON((1.49 2.91, 0.27 0.79, 2.72 0.79, 1.49 2.91))
Returns an array of geographies that represents the parts of the union of the geographies in the first array that do not intersect with the union of geographies in the second array.
This operation is not symmetric, meaning ST_MULTIDIFFERENCEARRAY(array1, array2) does not produce the same results as ST_MULTIDIFFERENCEARRAY(array2, array1) unless both arrays are identical.
The function ignores NULL types. For types besides NULL, all geographies in both arrays must be of the same type.
The output geographies are the same type as the input geographies.
Syntax
Parameter | Data Type | Description |
---|---|---|
array1 | ARRAY of POINT, LINESTRING, or POLYGON values. | An array of geographic objects to be compared to the geographic objects in array2. The function returns any values in this array that do not intersect with array2. |
array2 | ARRAY of POINT, LINESTRING, or POLYGON values. | An array of geographic objects to be compared to the geographic objects in array1. The function returns only values that do not intersect in array1. |
Example
Output: ['POINT(0.0 0.0)']
When you specify two arrays of geospatial objects, this function returns an array of any intersections. The function treats each specified array as a union of all its geospatial values.
All geographies in both arrays must be of the same type, not counting NULL values, which the function ignores.
The output geographies are the same type as the input geographies.
Syntax
Parameter | Data Type | Description |
---|---|---|
array1 | ARRAY of POINT, LINESTRING, or POLYGON values. | An array of geographic objects to be compared to the geographic objects in array2 for any intersections. The data types contained in this array must match the data types in array2, unless they are NULL. |
array2 | ARRAY of POINT, LINESTRING, or POLYGON values. | An array of geographic objects to be compared to the geographic objects in array1 for any intersections. The data types contained in this array must match the data types in array1, unless they are NULL. |
Example
Output: ['POINT(1.0 1.0)']
Alias for ST_MULTIUNIONARRAY(array1, array2) - ST_MULTIINTERSECTIONARRAY(array1, array2).
When you specify two arrays of geospatial objects, this function returns an array of any geospatial values that do not intersect. The function treats each specified array as a union of all its geospatial values.
All geographies in both arrays must be of the same type, not counting NULL values, which the function ignores.
The output geographies are the same type as the input geographies.
Syntax
Parameter | Data Type | Description |
---|---|---|
array1 | ARRAY of POINT, LINESTRING, or POLYGON values. | An array of geographic objects to be compared to the geographic objects in array2 for any values that do not intersect. The data types contained in this array must match the data types in array2, unless they are NULL. |
array2 | ARRAY of POINT, LINESTRING, or POLYGON values. | An array of geographic objects to be compared to the geographic objects in array1 for any values that do not intersect. The data types contained in this array must match the data types in array1, unless they are NULL. |
Example
Output: ['POINT(0.0 0.0)','POINT(2.0 2.0)']
When you specify two arrays of geospatial objects, this function returns one array that represents a union of all geographies in both arrays.
All geographies in both arrays must be the same type. The function ignores NULL values in the arrays. The output geographies are the same type as the input geographies.
Syntax
Parameter | Data Type | Description |
---|---|---|
array1 | ARRAY of POINT, LINESTRING, or POLYGON values. | An array of geographic objects to be combined with array2. The data types contained in this array must match the data types in array2, unless they are NULL. |
array2 | ARRAY of POINT, LINESTRING, or POLYGON values. | An array of geographic objects to be combined with array1. The data types contained in this array must match the data types in array1, unless they are NULL. |
Example
Output: ['POINT(1.0 1.0)','POINT(2.0 2.0)','POINT(0.0 0.0)']
Returns the number of rings of the specified POLYGON, including both interior and exterior rings. If you specify a geography object that is not a POLYGON, this function returns 0.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo | POLYGON | An object to be computed to return its total number of rings. |
Example
Output: 1
Returns the number of interior rings of the specified POLYGON. If you specify a geography object that is not a POLYGON, this function returns 0.
Syntax
Parameter | Data Type | Description |
---|---|---|
polygon | POLYGON | An object to be computed to return its total number of interior rings. |
Example
Output: 1
Returns a POINT guaranteed to intersect the specified geospatial object.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo | POINT, LINESTRING, or POLYGON | An object to be computed for an intersection POINT value. |
Example
Output: POINT(1.0 1.0)
Returns a POINT by projecting a distance and an azimuth value from the specified starting POINT value. The distance must be in meters and azimuth must be in radians.
Syntax
Parameter | Data Type | Description |
---|---|---|
point | POINT | A starting point for locating a returned destination by using the distance and azimuth values. |
distance | DOUBLE | A distance value in meters. |
azimuth | DOUBLE | An azimuth value in radians. |
Example
Output: POINT(0.99 3.00)
Returns a new geospatial object with all POINT values rounded to the specified decimal precision.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo | POINT, LINESTRING, or POLYGON | A geospatial object to be rounded to the specified precision value. |
precision | DOUBLE | A decimal value that defines the level of precision for returned values. For example, a precision of 0.01 returns values rounded to the hundredth decimal. Do not use digits other than 0 and 1 to represent the precision. |
Example
Output: POINT(1.2 1.3)
Returns a new geospatial object with no repeated POINT values. The returned type is the same as the specified type.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo | POINT, LINESTRING, or POLYGON | A geospatial object for removal of any duplicate POINT values. |
Example
Output: LINESTRING(1.0 2.0, 2.0 1.0, 1.0 0.0)
Returns a new geospatial object with the vertexes reversed. The returned type is the same as the specified type.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo | POINT, LINESTRING, or POLYGON | A geospatial object for vertexes reversal. |
Example
Output: LINESTRING(1.0 10.0, 1.0 2.0)
Returns a geospatial object that the function modifies to have no segment longer than the specified max_segment_length in meters.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo | POINT, LINESTRING, or POLYGON | A geospatial object for segment modification. |
max_segment_length | DOUBLE | The maximum length of a segment to modify the specified geo object. This value must be in METERS. |
Example
Output: POLYGON((1.0 1.0, 2.0 1.0, 2.0 2.0, 1.49 1.50, 1.0 1.0))
Returns the shortest LINESTRING between two specified geospatial arguments.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo1 | POINT, LINESTRING, or POLYGON | A geospatial object for the determination of the shortest LINESTRING relative to geo2. |
geo2 | POINT, LINESTRING, or POLYGON | A geospatial object for the determination of the shortestLINESTRING relative to geo1. |
use_spheroid | BOOLEAN | Optional. If you set this argument to TRUE, this function uses a spheroid model instead of a spherical model. Defaults to FALSE. |
Example
Output: LINESTRING(0.0 3.0, 4.0 3.0)
To use ST_SHORTESTLINE as a spatiotemporal operator to calculate the shortest line between two points at a concurrent time, see the ST_SHORTESTLINE function in the Spatiotemporal section.
Returns a simplified version of the specified geography, which is either a POINT or LINESTRING.
The function simplifies the input geography by replacing nearly straight edges that contain multiple points with a single straight edge. The input geography does not change by more than the specified tolerance, which you specify as a DOUBLE in meters.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo | POINT or LINESTRING | A geospatial object to be simplified. |
tolerance | DOUBLE | A value that represents the maximum number of meters to simplify in the specified geography. |
Example
Output: LINESTRING(0 0, 2 0)
Returns a POLYGON array that represents a simplified version of the specified geography, which is either a POINT, LINESTRING, or POLYGON.
The function simplifies the input geography by replacing nearly straight edges that contain multiple points with a single straight edge. The input geography does not change by more than the specified tolerance, which you specify as a DOUBLE in meters.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo | POINT, LINESTRING or POLYGON | A geospatial object to be simplified. |
tolerance | DOUBLE | A value that represents the maximum number of meters to simplify in the specified geography. |
Example
In this example, ST_SIMPLIFYARRAY evaluates a POLYGON with an outer ring of 13 points and an inner ring with four points. The example sets a tolerance parameter of 10,000 meters, meaning that is the maximum distance that the function simplifies.
Output: [POLYGON((0.0 0.5000000762285838, 1.0 0.500000076228584, 1.0 1.0, 0.0 1.0, 0.0 0.5000000762285838)), POLYGON((1.0 0.4998004375649452, 0.5 0.4, 0.0 0.4998004375649452, 0.0 0.0, 1.0 0.0, 1.0 0.4998004375649452))]
As shown in the output, the ST_SIMPLIFYARRAY function has reduced the size of the outer POLYGON from 13 points to 5.
Note that the function actually increases the number of points in the inner ring, going from four to six. The increase is due to how close the inner ring is to the edges of the outer ring. Taken altogether, the function still reduced the total number of points in the POLYGON.
Returns a new geography value with all POINT values rounded to the specified precisions. This function is similar to ST_REDUCEPRECISION but it allows you to specify the precision for the x and y coordinates separately.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo | POINT, LINESTRING, or POLYGON | A geospatial object to be rounded to the specified precision values. |
precision_x | DOUBLE | A decimal value to represent the level of precision for the x value. For example, a precision of 0.01 returns values rounded to the hundredth decimal. Do not use digits other than 0 and 1 to represent the precision. |
precision_y | DOUBLE | A decimal value to represent the level of precision for the y value. For example, a precision of 0.1 returns values rounded to the tenth decimal. Do not use digits other than 0 and 1 to represent the precision. |
Example
Output: POINT(1.0 3.1)
Returns a geographic array that contains the parts that are not common between two geographic objects, geo1 and geo2.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo1 | POINT, LINESTRING, or POLYGON | A geographic object to be compared to the points geographic objects in geo2. |
geo2 | POINT, LINESTRING, or POLYGON | A geographic object to be compared to the points geographic objects in geo1. |
Example
Output: [LINESTRING(2.0 2.0, 3.0 3.0)]
Performs a union of the input geography values to produce a geographic array.
Syntax
Parameter | Data Type | Description |
---|---|---|
geo1 | POINT, LINESTRING, or POLYGON | A geographic object to be unified with the geographic values in geo2. |
geo2 | POINT, LINESTRING, or POLYGON | A geographic object to be unified with the geographic values in geo1. |
Example
Output: ['LINESTRING(0.0 0.0, 1.0 1.0, 2.0 2.0, 3.0 3.0)']