将线串转换为点数组?

时间:2016-04-27 05:52:24

标签: postgresql postgis

我有postgis的postgres,我需要将线串转换为点数组

我已经使用此answer进行了检查,我的问题类似,但我还需要将其聚合到数组

来自这个

Linestring((3.584731 60.739211,3.590472 60.738030,3.592740 60.736220))

到这个

["3.584731 60.739211","3.590472 60.738030","3.592740 60.736220"]

我该怎么做?

2 个答案:

答案 0 :(得分:3)

好吧,我不知道你为什么需要这个,但是你去了:

WITH
    route_sequence AS (
      SELECT id, path
      FROM (
        VALUES
          (1, 'Linestring(3.584731 60.739211,3.590472 60.738030,3.592740 60.736220)'),
          (2, 'Linestring(3.584731 60.739211,3.590472 60.738030,3.592740 60.736220)')
      ) AS route_sequence(id, path)
  ),
  rstemp AS (
      SELECT
        route_sequence.id,
        array_agg(
            ST_X(points.geom)::CHARACTER VARYING
            || ' '
            || ST_Y(points.geom)::CHARACTER VARYING
            ORDER BY points.path[1] ASC
        ) AS coordinates
      FROM route_sequence, ST_DumpPoints(path) as points(path, geom)
      GROUP BY route_sequence.id
  )
SELECT *
FROM rstemp
;

我很想知道为什么你需要一个阵列。也许还有另一种方式。为什么不能保留geom生成的初始ST_DumpPoints

答案 1 :(得分:0)

WITH points_text AS (SELECT
                   (st_dumppoints(path)).geom AS points,
                   (st_dumppoints(path)).path AS points_path,
                   rs.id,
                   rcc.checkpoint_id,
                   rcc.route_id,
                   rcc."order",
                   rcc.is_forward,
                   rcc.is_primary
                 FROM route_cacheed_checkpoint AS rcc
                   JOIN route_sequence AS rs ON rs.id = ANY (rcc.next_rsq)
                 WHERE rcc.date = (DATE '2016-04-28') AND rcc.is_primary = TRUE)
SELECT
    array_agg(to_char(ST_X(points), 'FM99.9999990') || to_char(ST_Y(points), '99.9999990') ORDER BY points_path) AS path,
    pt.checkpoint_id,
    pt.route_id,
    pt."order",
    pt.is_forward                 AS is_forward_direction,
    pt.is_primary
FROM points_text AS pt
GROUP BY pt.id, pt.checkpoint_id, pt.route_id, pt."order", pt.is_forward, pt.is_primary
ORDER BY pt.is_forward DESC, pt."order";