POLYGON

On this page Carat arrow pointing down
Warning:
GA releases for CockroachDB v23.1 are no longer supported. Cockroach Labs will stop providing LTS Assistance Support for v23.1 LTS releases on November 13, 2025. Prior to that date, upgrade to a more recent version to continue receiving support. For more details, refer to the Release Support Policy.

A POLYGON is a shape with a closed exterior that is made up of lines. Polygons can also contain holes. Polygons are often used to represent areas such as countries, states, or municipalities.

The coordinates of each Point and line that make up the Polygon are translated according to the current spatial reference system (denoted by an SRID) to determine what the point "is", or what it "means" relative to the other spatial objects (if any) in the data set.

You can also store a POLYGON with the following additional dimensions:

  • A third dimension coordinate Z (POLYGONZ).
  • A measure coordinate M (POLYGONM).
  • Both a third dimension and a measure coordinate (POLYGONZM).

The Z and M dimensions can be accessed or modified using a number of built-in functions, including:

  • ST_Z
  • ST_M
  • ST_Affine
  • ST_Zmflag
  • ST_MakePoint
  • ST_MakePointM
  • ST_Force3D
  • ST_Force3DZ
  • ST_Force3DM
  • ST_Force4D
  • ST_Snap
  • ST_SnapToGrid
  • ST_RotateZ
  • ST_AddMeasure

Note that CockroachDB's spatial indexing is still based on the 2D coordinate system. This means that:

  • The Z/M dimension is not index accelerated when using spatial predicates.
  • Some spatial functions ignore the Z/M dimension, with transformations discarding the Z/M value.

Examples

Well known text

A Polygon can be created from SQL by calling the st_geomfromtext function on a LineString definition expressed in the Well Known Text (WKT) format as shown below.

icon/buttons/copy
SELECT ST_GeomFromText('POLYGON((0 0, 0 1024, 1024 1024, 1024 0, 0 0))');
                                                                                       st_geomfromtext
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  010300000001000000050000000000000000000000000000000000000000000000000000000000000000009040000000000000904000000000000090400000000000009040000000000000000000000000000000000000000000000000
(1 row)

Polygons with holes

To represent a polygon with holes in WKT, add one or more additional lists of coordinates that define the boundaries of the holes as shown below:

icon/buttons/copy
SELECT ST_GeomFromText('POLYGON((-87.906471 43.038902, -95.992775 36.153980, -75.704722 36.076944, -87.906471 43.038902), (-87.623177 41.881832, -90.199402 38.627003, -82.446732 38.413651, -87.623177 41.881832))');
                                                                                                                                           st_geomfromtext
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  010300000002000000040000006FF1F09E03FA55C0DFDFA0BDFA844540545227A089FF57C0791EDC9DB513424064B14D2A1AED52C0CCCF0D4DD90942406FF1F09E03FA55C0DFDFA0BDFA84454004000000A4A7C821E2E755C07C48F8DEDFF0444073309B00C38C56C038BF61A241504340E884D041979C54C0967A1684F2344340A4A7C821E2E755C07C48F8DEDFF04440
(1 row)

Using a SQL function

You can also use the st_makepolygon function on a LineString that defines the outer boundary of the Polygon, e.g.:

icon/buttons/copy
SELECT ST_MakePolygon('LINESTRING(0 0, 0 1024, 1024 1024, 1024 0, 0 0)');
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  010300000001000000050000000000000000000000000000000000000000000000000000000000000000009040000000000000904000000000000090400000000000009040000000000000000000000000000000000000000000000000
(1 row)

See also


Yes No
On this page

Yes No