名称

ST_SetPoint — 用给定的点替换线串的点。

概要

geometry ST_SetPoint(geometry linestring, integer zerobasedposition, geometry point);

描述

用给定的点替换线串的第 N 个点。索引从 0 开始。负索引从后向前计数,因此 -1 是最后一个点。这在尝试维护一个顶点移动时关节的关系的触发器中尤其有用。

可用性: 1.1.0

更新于 2.3.0:负索引

此函数支持 3d,不会删除 z 索引。

示例

--Change first point in line string from -1 3 to -1 1
SELECT ST_AsText(ST_SetPoint('LINESTRING(-1 2,-1 3)', 0, 'POINT(-1 1)'));
	   st_astext
-----------------------
 LINESTRING(-1 1,-1 3)

---Change last point in a line string (lets play with 3d linestring this time)
SELECT ST_AsEWKT(ST_SetPoint(foo.geom, ST_NumPoints(foo.geom) - 1, ST_GeomFromEWKT('POINT(-1 1 3)')))
FROM (SELECT ST_GeomFromEWKT('LINESTRING(-1 2 3,-1 3 4, 5 6 7)') As geom) As foo;
	   st_asewkt
-----------------------
LINESTRING(-1 2 3,-1 3 4,-1 1 3)

SELECT ST_AsText(ST_SetPoint(g, -3, p))
FROM ST_GEomFromText('LINESTRING(0 0, 1 1, 2 2, 3 3, 4 4)') AS g
	, ST_PointN(g,1) as p;
	   st_astext
-----------------------
LINESTRING(0 0,1 1,0 0,3 3,4 4)