Section 6: Accessor Functions
We saw earlier how geometry objects are built out of structured lists of coordinates. Once you’ve built your geometries it is sometimes helpful to be able to pull them apart again. The “accessor” functions allow you to do just that. The following sections outline some of the available accessors for difference geometry types.
Multi-geometry
- ST_NumGeometries(geometry)
- Returns the number of geometries included in a multi-geometry or geometry collection.
- ST_GeometryN(geometry, index)
- Returns the geometry at the given index within the multi-geometry or geometry collection, or null if index is greater than the number of sub-geometries. The first geometry is at index 1, the last at the value of index.
Polygon
- ST_NumInteriorRings(geometry)
- Returns the number of interior rings contained in the given polygon.
- ST_NRings(geometry)
- Returns the number of rings, including the exterior ring, in the given polygon.
- ST_ExteriorRing(geometry)
- Returns the exterior ring of the geometry as a Linestring.
- ST_InteriorRingN(geometry, index)
- Returns the ring at the given index within the polygon as a Linestring or null if index is greater than the number of rings. The first interior ring is at index 1, the last at the value of index.
LineString
- ST_NumPoints(geometry)
- Returns the number of points contained in a Linestring geometry.
- ST_NPoints(geometry)
- Like ST_NumPoints but usable against any geometry type.
- ST_PointN(geometry, index)
- Returns the point at the given index within the linestring or null if index is greater than the number of points. The first point is at index 1, the last at the value of index.
Convenience methods are also defined for retrieving the start and end points of a line.
ST_StartPoint(geometry) = ST_PointN(geometry, 0)
ST_EndPoint(geometry) = ST_PointN(geometry, ST_NumPoints(geometry))
Point
- ST_X(geometry) ST_Y(geometry) ST_Z(geometry) ST_M(geometry)
- These functions return the x, y, z, and m coordinate of the given point respectively. In the case of ST_Z and ST_M, null is returned when the coordinate is not available.