名称

ST_GeometryN — 返回几何集合的一个元素。

大纲

geometry ST_GeometryN(geometry geomA, integer n);

描述

Returns the 1-based Nth element of a GEOMETRYCOLLECTION, MULTIPOINT, MULTILINESTRING, MULTICURVE, or MULTIPOLYGON. For a unitary geometry, including a TIN or PolyhedralSurface, an index of 1 returns the input geometry and any other index returns NULL. Use ST_PatchN and ST_NumPatches to access and count the faces of a TIN or PolyhedralSurface.

[注意]

自版本 0.8.0 以来,OGC 规范的索引从 1 开始。 以前的版本将其实现为基于 0。

[注意]

要提取几何图形的所有元素,ST_Dump 效率更高,并且适用于基本几何图形。

增强功能:引入了2.0.0 对多面体曲面、三角形和三角网的支持。

更改:2.0.0 版本。之前的版本对于奇异几何图形会返回 NULL。现在已更改为在 ST_GeometryN(..,1) 情况下返回几何图形。

Changed: 3.6.0 TIN and PolyhedralSurface are treated as unitary geometries and are not decomposed into patches; use ST_PatchN to access faces.

此方法实现了 SQL 1.1 的 OGC 简单功能规范。

该方法实现了SQL/MM规范。 SQL-MM 3: 9.1.5

该函数支持 3d 并且不会丢失 z-index。

此方法支持圆形字符串和曲线。

该函数支持多面体曲面。

此函数支持三角形和不规则三角网面 (TIN)。

标准示例

Extracting a subset of points from a 3D multipoint.

Code
SELECT n, ST_GeometryN(geom, n) As geomewkt
FROM (
VALUES
    (1, 'MULTIPOINT((1 2 7), (3 4 7), (5 6 7), (8 9 10))'::geometry ),
    (2, 'MULTICURVE(CIRCULARSTRING(2.5 2.5,4.5 2.5, 3.5 3.5), (10 11, 12 11))'::geometry )
    ) As foo(id, geom)
    CROSS JOIN generate_series(1,100) n
WHERE n <= ST_NumGeometries(geom)
ORDER BY id, n;
栅格输出
 n |               geomewkt
---+-----------------------------------------
 1 | POINT(1 2 7)
 2 | POINT(3 4 7)
 3 | POINT(5 6 7)
 4 | POINT(8 9 10)
 1 | CIRCULARSTRING(2.5 2.5,4.5 2.5,3.5 3.5)
 2 | LINESTRING(10 11,12 11)
Figure
Geometry figure for visual-st-geometryn-01

Extracting all geometries (useful when you want to assign an id).

Code
SELECT gid, n, ST_GeometryN(geom, n)
FROM sometable CROSS JOIN generate_series(1,100) n
WHERE n <= ST_NumGeometries(geom);

多面体曲面、TIN 和三角形的示例

A PolyhedralSurface or TIN is treated as a single geometry, not as a collection of faces. ST_GeometryN does not decompose the surface into patches, so an index greater than 1 returns NULL. Use ST_PatchN and ST_NumPatches to access individual faces.

This example requests the third element of a PolyhedralSurface.

Code
SELECT ST_GeometryN(p_geom, 3) As geom_ewkt
  FROM (SELECT 'POLYHEDRALSURFACE(
((0 0 0,0 0 1,0 1 1,0 1 0,0 0 0)),
((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)),
((0 0 0,1 0 0,1 0 1,0 0 1,0 0 0)),
((1 1 0,1 1 1,1 0 1,1 0 0,1 1 0)),
((0 1 0,0 1 1,1 1 1,1 1 0,0 1 0)),
((0 0 1,1 0 1,1 1 1,0 1 1,0 0 1))
)'::geometry  AS p_geom )  AS a;
栅格输出
NULL
Figure
Geometry figure for visual-st-geometryn-02

This example requests the second element of a TIN.

Code
SELECT ST_GeometryN(geom, 2) as wkt
  FROM
    (SELECT
       'TIN (((
                0 0 0,
                0 0 1,
                0 1 0,
                0 0 0
            )),((
                0 0 0,
                0 1 0,
                1 1 0,
                0 0 0
            ))
            )'::geometry  AS geom
    ) AS g;
栅格输出
NULL
Figure
Geometry figure for visual-st-geometryn-03