官术网_书友最值得收藏!

Multi-geometry decomposition

Multi-geometries can be broken down into single parts. There are two ways to do this.

  • The first is to extract components one part at a time, using the ST_GeometryN function.
In computer programming in general, indexes are 0 based, so the first list element will have an index of 0. In PostgreSQL, however, the convention is different and indexes are 1 based. This means that the first element has an index of 1.

For example, extracting the second part from a MultiPoint created by hand can be done using the following:

        SELECT       
ST_AsText(ST_GeometryN(ST_Collect(ARRAY[ST_MakePoint(20,50),
ST_MakePoint(19.95,49.98), ST_MakePoint(19.90,49.96)]),2));

st_astext
--------------------
POINT(19.95 49.98)

The number of Multi-geometry parts can be revealed using the ST_NumGeometries function:

        SELECT
ST_NumGeometries(ST_Collect(ARRAY(ST_MakePoint(20,50),
ST_MakePoint(19.95,49.98), ST_MakePoint(19.90,49.96))));


3
  • The second option is to expand a Multi-geometry into a set of rows using the ST_Dump function. This returns a special compound type, called geometry dump. The geometry dump type consists of two parts: the path, which denotes the position of a component within the Multi-geometry, and the geom being the actual component geometry:
         SELECT
ST_Dump(ST_Collect(ARRAY[ST_MakePoint(20,50),
ST_MakePoint(19.95,49.98), ST_MakePoint(19.90,49.96)]));

This will result in the following:

        st_dump                     
--------------------------------------------------
({1},010100000000000000000034400000000000004940)
({2},01010000003333333333F333403D0AD7A370FD4840)
({3},01010000006666666666E633407B14AE47E1FA4840)
(3 rows)

To extract actual geometries from the geometry dump, the ST_Dump function should be wrapped in parentheses, and its geom part referenced:

        SELECT 
(ST_Dump(ST_Collect(ARRAY[ST_MakePoint(20,50),
ST_MakePoint(19.95,49.98), ST_MakePoint(19.90,49.96)]))).geom;
geom
--------------------------------------------
010100000000000000000034400000000000004940
01010000003333333333F333403D0AD7A370FD4840
01010000006666666666E633407B14AE47E1FA4840

Now each of the MultiPoint's components are accessible as a single-part geometry, which can be inserted into another table or used in another function.

主站蜘蛛池模板: 湘潭市| 呼和浩特市| 九江市| 晋中市| 兴宁市| 新昌县| 鄱阳县| 筠连县| 南康市| 凌云县| 镇安县| 金平| 普宁市| 四平市| 吉木萨尔县| 彝良县| 柏乡县| 赤峰市| 澳门| 科技| 保德县| 丹阳市| 北票市| 弋阳县| 昌邑市| 淮北市| 延庆县| 开平市| 嘉祥县| 灯塔市| 襄樊市| 梁河县| 封开县| 德昌县| 湟中县| 卫辉市| 祁阳县| 桐梓县| 洛南县| 三亚市| 高要市|