PostGIS之路——几何对象编辑(一)

1、ST_AddPoint

     插入一个点到线中,线在点的前面(以0为索引)。第三个参数可以省略或设置-1来插入点。

geometry ST_AddPoint(geometry linestring, geometry point);

geometry ST_AddPoint(geometry linestring, geometry point, integer position);

示例SqL:

SELECT ST_AsEWKT(ST_AddPoint(ST_GeomFromEWKT('LINESTRING(0 0 1, 1 1 1)'), ST_MakePoint

(1, 2, 3)));

2、ST_Affine

    对一个几何对象做三维仿真变换可以做任何事,如平移、旋转、缩放在一个步骤。(注意1.3.4版本中对曲线变化会崩溃,在1.3.4中固定名称,2.0.0以后版本支持了更多类型的几何数据编辑。

geometry ST_Affine(geometry geomA, float a, float b, float c, float d, float e, float f, float g, float h, float i, float xoff, float yoff,

float zoff);

geometry ST_Affine(geometry geomA, float a, float b, float d, float e, float xoff, float yoff);

示例SQL:

--Rotate a 3d line 180 degrees about the z axis. Note this is long-hand for doing ST_Rotate();

SELECT ST_AsEWKT(ST_Affine(the_geom, cos(pi()), -sin(pi()), 0, sin(pi()), cos(pi()), 0,

0, 0, 1, 0, 0, 0)) As using_affine,

ST_AsEWKT(ST_Rotate(the_geom, pi())) As using_rotate

FROM (SELECT ST_GeomFromEWKT('LINESTRING(1 2 3, 1 4 3)') As the_geom) As foo;

 

--Rotate a 3d line 180 degrees in both the x and z axis

SELECT ST_AsEWKT(ST_Affine(the_geom, cos(pi()), -sin(pi()), 0, sin(pi()), cos(pi()), -sin(

pi()), 0, sin(pi()), cos(pi()), 0, 0, 0))

FROM (SELECT ST_GeomFromEWKT('LINESTRING(1 2 3, 1 4 3)') As the_geom) As foo;

 

3、强转函数ST_Force_2D、ST_Force_3D、ST_Force_3DZ、ST_Force_3DM、ST_Force_4D、ST_Force_Collection、ST_ForceRHR

    几何对象的类型转换。

geometry ST_Force_2D(geometry geomA);

geometry ST_Force_3D(geometry geomA);

geometry ST_Force_3DZ(geometry geomA);

geometry ST_Force_3DM(geometry geomA);

geometry ST_Force_4D(geometry geomA);

geometry ST_Force_Collection(geometry geomA);

boolean ST_ForceRHR(geometry g);

示例SQL:

SELECT ST_AsEWKT(ST_Force_4D('MULTILINESTRINGM((0 0 1,0 5 2,5 0 3,0 0 4),(1 1 1,3 1 1,1 3 1,1 1 1))'));

 

SELECT ST_AsEWKT(ST_Force_Collection('POLYHEDRALSURFACE(((0 0 0,0 0 1,0 1 1,0 1 0,0 0 0)),

((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)),

((0 0 0,1 0 0,1 0 1,0 0 1,0 0 0)),

((1 1 0,1 1 1,1 0 1,1 0 0,1 1 0)),

((0 1 0,0 1 1,1 1 1,1 1 0,0 1 0)),

((0 0 1,1 0 1,1 1 1,0 1 1,0 0 1)))'));

 

SELECT ST_AsEWKT(ST_ForceRHR('POLYGON((0 0 2, 5 0 2, 0 5 2, 0 0 2),(1 1 2, 1 3 2, 3 1 2, 1 1 2))'));

4、ST_LineMerge

    合并为线;这个函数只能用于线或多段线,如果给一个多边形或几何集合,将返回一个空的几何集合。

geometry ST_LineMerge(geometry amultilinestring);

示例SQL:

SELECT ST_AsText(ST_LineMerge(ST_GeomFromText('MULTILINESTRING((-29 -27,-30 -29.7,-36 -31,-45 -33),(-45 -33,-46 -32))')));

 

5、ST_CollectionExtract

    给定一个(Milti)几何对象,返回一个(Milti)几何对象,只能包含元素指定类型的。几何形状开头没有指定的类型将被忽略。如果开头没有正确的几何形状类型,将返回一个空的几何集合。只有点、线和多边形的支持。类型对应1是点,2 是线,3是多边形。

geometry ST_CollectionExtract(geometry collection, integer type);

示例SQL:

SELECT ST_AsText(ST_CollectionExtract(ST_GeomFromText('GEOMETRYCOLLECTION(

GEOMETRYCOLLECTION(LINESTRING(0 0, 1 1)),LINESTRING(2 2, 3 3))'),2));

6、ST_CollectionHomogenize

    给定一个几何集合,返回“简单”表示的内容。单例对象将被返回单例。这个几何集合将返回适当的多类型。

geometry ST_CollectionHomogenize(geometry collection);

示例SQL:

SELECT ST_AsText(ST_CollectionHomogenize(’GEOMETRYCOLLECTION(POINT(0 0),POINT(1 1))’));

7、ST_Multi

     几何对象返回为一个Multi几何对象。如果这个几何对象时Multi类型将不改变。

geometry ST_Multi(geometry g1);

示例SQL:

SELECT ST_AsText(ST_Multi(ST_GeomFromText('POLYGON((743238 2967416,743238 2967450,

743265 2967450,743265.625 2967416,743238 2967416))')));

8、ST_RemovePoint

    从线上删除点。有用的将一个封闭的环成一个开放线字符串

geometry ST_RemovePoint(geometry linestring, integer offset);

示例SQL:

UPDATE sometable

SET the_geom = ST_RemovePoint(the_geom, ST_NPoints(the_geom) - 1)

FROM sometable

WHERE ST_IsClosed(the_geom) = true;

posted @ 2013-03-23 13:44  清灵阁主  阅读(1845)  评论(3编辑  收藏  举报