我需要结合2个查询postgis

时间:2014-06-27 09:39:43

标签: postgresql postgis

我有2个查询:

第一次查询:

SELECT ST_AsText( ST_MakeLine(sp) )
FROM
(SELECT
  ST_PointN(geom, generate_series(1, ST_NPoints(geom))) as sp
FROM
   -- extract the individual linestrings
  (SELECT (ST_Dump(ST_Boundary(geom))).geom
   FROM geometriess
   ) AS linestrings
) AS segments;

表中有:" POLYGON((0 0,1 0,1 1,0 1,0 0))"

在此查询之后将有:" LINESTRING(0 0,1 0,1 1,0 1,0 0)"

第二个查询:

 with line as (select geom from geometries)
select ST_X(ST_PointN(geom,num)) as x, 
       ST_Y(ST_PointN(geom,num)) as y 
from line,
   (select generate_series(1, (select ST_NumPoints(geom) from line)) as num) 
as series;

它将线串分割为点x和y。

我需要将它们结合起来,但我不知道如何。

1 个答案:

答案 0 :(得分:0)

以下测试表创建两个具有不同点数的多边形,将它们组合在一起,然后使用带有st_npoints的generate_series抓取边界的每个点以迭代每个多边形。

create table test (id serial, geom geometry);
insert into test (geom) values (ST_GeomFromText('POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))'));
insert into test (geom) values (ST_GeomFromText('POLYGON((100 100, 100 200, 200 200, 250 300, 200 100, 100 100))'));


with dumped as (select (st_dump(geom)) as dump from 
      (select st_collect(geom) as geom from test) g),
    points as (select (dump).path, st_npoints((dump).geom) as npoints, (dump).geom from dumped),
    polygons as (select path[1] as polygonid, 
                 st_boundary(geom) as geom, 
                 generate_series(1,npoints) as x from points)
 select polygonid, 
        st_x(st_pointn(geom,x)), 
        st_y(st_pointn(geom,x)) 
 from polygons;

返回:

 polygonid | st_x | st_y 
-----------+------+------
     1 |    0 |    0
     1 |    0 |    1
     1 |    1 |    1
     1 |    1 |    0
     1 |    0 |    0
     2 |  100 |  100
     2 |  100 |  200
     2 |  200 |  200
     2 |  250 |  300
     2 |  200 |  100
     2 |  100 |  100