迭代并单独编写线串(generate_series)
iterate and write separately linestrings (generate_series)
我有一个 table 混合多边形和多边形,我想 运行 一个单一的函数将它们分解成 LineStrings(或 MultiLinestrings)。
我的问题 - 我目前返回的是一组......没有几何图形(?)......
函数目前看起来像这样...(最终基于 Mike T. 帮助)
CREATE OR REPLACE FUNCTION LinesFromPoly2(polygon geometry)
RETURNS SETOF geometry_dump AS
$BODY$DECLARE
m integer;
g geometry;
p geometry_dump%ROWTYPE;
BEGIN
FOR m IN SELECT generate_series(1, ST_NumGeometries()) LOOP
p.path[1] := m;
p.geom := ST_Boundary(ST_GeometryN(, m));
RETURN NEXT p;
END LOOP;
RETURN;
END;$BODY$
LANGUAGE plpgsql ;
呼叫:
SELECT id, name, LinesFromPoly2(the_geom)
FROM public.poly_and_multipoly;
RETURNS:
1|A|({1},)
2|B|({1},)
2|B|({2},)
示例数据:
CREATE TABLE poly_and_multipoly (
"id" SERIAL NOT NULL PRIMARY KEY,
"name" char(1) NOT NULL,
"the_geom" geometry NOT NULL
);
-- add data, A is a polygon, B is a multipolygon
INSERT INTO poly_and_multipoly (name, the_geom) VALUES (
'A', 'POLYGON((7.7 3.8,7.7 5.8,9.0 5.8,7.7 3.8))'::geometry
), (
'B',
'MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))'::geometry
);
您不需要自定义函数来执行您想要的操作。例如,尝试只访问 ST_Dump
的两个成员(path
和 geom
):
SELECT id, name,
(ST_Dump(ST_Boundary(the_geom))).path[1],
ST_AsText((ST_Dump(ST_Boundary(the_geom))).geom)
FROM poly_and_multipoly;
id | name | path | st_astext
----+------+------+-------------------------------------------
1 | A | | LINESTRING(7.7 3.8,7.7 5.8,9 5.8,7.7 3.8)
2 | B | 1 | LINESTRING(0 0,4 0,4 4,0 4,0 0)
2 | B | 2 | LINESTRING(1 1,2 1,2 2,1 2,1 1)
2 | B | 3 | LINESTRING(-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)
(4 rows)
或每个几何零件一个 [MULTI]LINESTRING:
SELECT id, name,
(ST_Dump(the_geom)).path[1],
ST_AsText(ST_Boundary((ST_Dump(the_geom)).geom))
FROM poly_and_multipoly;
id | name | path | st_astext
----+------+------+--------------------------------------------------------------
1 | A | | LINESTRING(7.7 3.8,7.7 5.8,9 5.8,7.7 3.8)
2 | B | 1 | MULTILINESTRING((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1))
2 | B | 2 | LINESTRING(-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)
(3 rows)
但如果您确实想使用 LinesFromPolygon2
函数,解决方法很简单:将几何体分配给 p.geom
,而不是 g
,即
p.geom := ST_Boundary(ST_GeometryN(, m));
我有一个 table 混合多边形和多边形,我想 运行 一个单一的函数将它们分解成 LineStrings(或 MultiLinestrings)。
我的问题 - 我目前返回的是一组......没有几何图形(?)......
函数目前看起来像这样...(最终基于 Mike T. 帮助)
CREATE OR REPLACE FUNCTION LinesFromPoly2(polygon geometry)
RETURNS SETOF geometry_dump AS
$BODY$DECLARE
m integer;
g geometry;
p geometry_dump%ROWTYPE;
BEGIN
FOR m IN SELECT generate_series(1, ST_NumGeometries()) LOOP
p.path[1] := m;
p.geom := ST_Boundary(ST_GeometryN(, m));
RETURN NEXT p;
END LOOP;
RETURN;
END;$BODY$
LANGUAGE plpgsql ;
呼叫:
SELECT id, name, LinesFromPoly2(the_geom)
FROM public.poly_and_multipoly;
RETURNS:
1|A|({1},)
2|B|({1},)
2|B|({2},)
示例数据:
CREATE TABLE poly_and_multipoly (
"id" SERIAL NOT NULL PRIMARY KEY,
"name" char(1) NOT NULL,
"the_geom" geometry NOT NULL
);
-- add data, A is a polygon, B is a multipolygon
INSERT INTO poly_and_multipoly (name, the_geom) VALUES (
'A', 'POLYGON((7.7 3.8,7.7 5.8,9.0 5.8,7.7 3.8))'::geometry
), (
'B',
'MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))'::geometry
);
您不需要自定义函数来执行您想要的操作。例如,尝试只访问 ST_Dump
的两个成员(path
和 geom
):
SELECT id, name,
(ST_Dump(ST_Boundary(the_geom))).path[1],
ST_AsText((ST_Dump(ST_Boundary(the_geom))).geom)
FROM poly_and_multipoly;
id | name | path | st_astext
----+------+------+-------------------------------------------
1 | A | | LINESTRING(7.7 3.8,7.7 5.8,9 5.8,7.7 3.8)
2 | B | 1 | LINESTRING(0 0,4 0,4 4,0 4,0 0)
2 | B | 2 | LINESTRING(1 1,2 1,2 2,1 2,1 1)
2 | B | 3 | LINESTRING(-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)
(4 rows)
或每个几何零件一个 [MULTI]LINESTRING:
SELECT id, name,
(ST_Dump(the_geom)).path[1],
ST_AsText(ST_Boundary((ST_Dump(the_geom)).geom))
FROM poly_and_multipoly;
id | name | path | st_astext
----+------+------+--------------------------------------------------------------
1 | A | | LINESTRING(7.7 3.8,7.7 5.8,9 5.8,7.7 3.8)
2 | B | 1 | MULTILINESTRING((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1))
2 | B | 2 | LINESTRING(-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)
(3 rows)
但如果您确实想使用 LinesFromPolygon2
函数,解决方法很简单:将几何体分配给 p.geom
,而不是 g
,即
p.geom := ST_Boundary(ST_GeometryN(, m));