如果输入前一点,我如何获得PostGIS线串中的下一个点

时间:2012-10-15 10:35:05

标签: postgresql postgis

我有PostgreSQL8.4 + PostGIS。

我在一列中有一个带有线串几何的表。我有一点这个线串,我想得到下一个点。为此,我想使用函数ST_NPointsST_PointN。为了使用这个我必须排序所有点找到我的观点,然后我将知道下一点它是我需要的。

这是我的表:

-- Table: filedata

-- DROP TABLE filedata;

CREATE TABLE filedata
(
  num serial NOT NULL,
 id integer,
 mydata character(25),
 the_geom geometry,
 CONSTRAINT filedata_pkey PRIMARY KEY (num)
)
WITH (
OIDS=FALSE
);
ALTER TABLE filedata OWNER TO postgres;

示例linestring:

"LINESTRING(60.7014631515719 56.8441322356241,60.7023117507097 56.8445673405349,60.702948200063 56.8447993944193,60.703902874093 56.8448574076656,60.706236521722 56.8447993944193,60.7094187684889 56.8449444273664,60.7121236782406 56.8450894597515,60.715571112238 56.8452925041466,60.718382096882 56.8454085290207,60.7204505572805 56.8453505166286,60.7222538304482 56.8450314468649,60.7246405155233 56.8444513130533,60.7260194891224 56.8440742212539,60.7260194891224 56.8440742212539,60.7260194891224 56.8440742212539,60.7260194891224 56.844045214035)"

1 个答案:

答案 0 :(得分:2)

假设您指的是线串顺序中的下一个,您可以执行以下操作:

WITH 
  -- The input data; this would be from the `filedata` table normally
  geom(line) AS (VALUES ('LINESTRING(60.7014631515719 56.8441322356241,60.7023117507097 56.8445673405349,60.702948200063 56.8447993944193,60.703902874093 56.8448574076656,60.706236521722 56.8447993944193,60.7094187684889 56.8449444273664,60.7121236782406 56.8450894597515,60.715571112238 56.8452925041466,60.718382096882 56.8454085290207,60.7204505572805 56.8453505166286,60.7222538304482 56.8450314468649,60.7246405155233 56.8444513130533,60.7260194891224 56.8440742212539,60.7260194891224 56.8440742212539,60.7260194891224 56.8440742212539,60.7260194891224 56.844045214035)'::geometry)),
  -- Create a set of (index,point) rows
  --
  point_series(line,n) AS (
      SELECT line, generate_series(1, ST_NPoints(line)) FROM geom
  ),
  -- Annotate that row set to include the prior point in each row,
  -- ie (n, point_n, point_n_minus_one)
  --
  lagged_points(pointidx, point, lagpoint) AS (
    SELECT n, ST_PointN(line,n) AS pointn,
           lag(ST_PointN(line,n)) OVER () AS point_n1
    FROM point_series
  )
-- Now SELECT the point we want, the point after
-- the one specified in the WHERE clause
SELECT astext(point)
FROM lagged_points
WHERE lagpoint = 'POINT(60.7014631515719 56.8441322356241)'::geometry;

如果PostGIS提供了将geometry转换为点数组的方法,这将会非常简单。如果它具有相当于intarray的idx功能,它也会更简单。