- Mastering PostGIS
- Dominik Mikiewicz Michal Mackiewicz Tomasz Nycz
- 240字
- 2021-07-02 22:52:25
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.
推薦閱讀
- Unreal Engine:Game Development from A to Z
- Word 2003、Excel 2003、PowerPoint 2003上機(jī)指導(dǎo)與練習(xí)
- 后稀缺:自動(dòng)化與未來(lái)工作
- 腦動(dòng)力:Linux指令速查效率手冊(cè)
- Machine Learning for Cybersecurity Cookbook
- CorelDRAW X4中文版平面設(shè)計(jì)50例
- 基于ARM 32位高速嵌入式微控制器
- Arduino &樂(lè)高創(chuàng)意機(jī)器人制作教程
- CentOS 8 Essentials
- Mastering Game Development with Unreal Engine 4(Second Edition)
- 邊緣智能:關(guān)鍵技術(shù)與落地實(shí)踐
- Photoshop行業(yè)應(yīng)用基礎(chǔ)
- 系統(tǒng)安裝、維護(hù)與數(shù)據(jù)備份技巧
- ARM嵌入式系統(tǒng)開(kāi)發(fā)完全入門(mén)與主流實(shí)踐
- 數(shù)據(jù)庫(kù)基礎(chǔ):Access