Oracle_spatial的函数介绍

2010年12月28日 Oracle_spatial的函数介绍

 

Oracle_spatial的函数

sdo_Geom包的函数:

用于表示两个几何对象的关系(结果为True/False)的函数:RELATEWITHIN_DISTANCE

验证的函数:VALIDATE_GEOMETRY_WITH_CONTEXT

VALIDATE_LAYER_WITH_CONTEXT

单个对象操作的函数:SDO_ARC_DENSIFY SDO_AREASDO_BUFFERSDO_

CENTROIDSDO_CONVEXHULL SDO_LENGTHSDO_MAX_MBR_

ORDINATE SDO_MIN_MBR_ORDINATESDO_MBRSDO_POINTONSURFACE

需两个对象操作操作的函数:SDO_DISTANCESDO_DIFFERENCESDO_INTERSECTIONSDO_UNIONSDO_XOR

1sdo_Geom.Relate

sdo_Geom.Relate(sdo_Geometry1, ‘MASK’, sod_Geometry2, tolerance ):用于判断一个几何体与另一个几何体的关系,我们用于判断当前点是否在某一个面(省份面、县市面、乡镇面)上。
  参数说明:
    sdo_Geometry1sdo_Geometry2为空间数据对应的几何对象。
    Tolerance: 容许的精度范围;
      MASK参数:
    Anyinteract: sdo_Geometry2落在sdo_Geometry1面上包括在边上。
    Contains: sdo_Geometry2完全包含在sdo_Geometry1几何对象中,并且两个几何对象的边没有交叉。
    Coveredby: sdo_Geometry1完全包含在sdo_Geometry2中,并且这两个几何对象的边有一个或多个点相互重叠。
    Covers: sdo_Geometry2完全包含在sdo_Geometry1中,并且这两个几何对象的边有一个或多个点相互重叠。
    Disjoint: 两个几何没有重叠交叉点,也没有共同的边。
    Equal: 两个几何是相等的。
    Inside: sdo_Geometry1完全包含在sdo_Geometry2几何对象中,并且两个几何对象的边没有交叉。
    On: sdo_Geometry1的边和内部的线完全在sdo_Geometry2上。
    Overlapbdydisjoint: 两个几何对象交迭,但是边没有交叉。
    Overlapbdyintersect: 两个几何对象交迭,并且边有部分交叉。
    Touch: 两个几何对象有共同的边,但没有交叉。

 

SELECT ct.gwm_fid, ct.name

 FROM i_exch_s ct, sales_regions comp

 WHERE SDO_GEOM.RELATE(ct.gwm_geometry, 'INSIDE', comp.geom, 0.5) = 'INSIDE'

   AND comp.gwm_fid = 50076218

 ORDER BY ct.gwm_fid;

 

SELECT ct.gwm_fid, ct.name

 FROM i_exch_s ct, sales_regions comp

 WHERE SDO_GEOM.RELATE(ct.gwm_geometry, 'ANYINTERACT', comp.geom, 0.5) =

       'INSIDE'

   AND comp.gwm_fid = 50076218

 ORDER BY ct.gwm_fid;

 

--RELATE函数补充SDO_RELATE操作符

 SELECT sra.gwm_fid,

        SDO_GEOM.RELATE(sra.geom, 'DETERMINE', srb.geom, 0.5) relationship

   FROM sales_regions srb, sales_regions sra

 WHERE srb.gwm_fid = 50076218

    AND sra.gwm_fid <> 50076218

    AND SDO_RELATE(sra.geom,

                   srb.geom,

                   'mask=TOUCH+OVERLAPBDYDISJOINT+OVERLAPBDYINTERSECT') =

        'TRUE'

 ORDER BY sra.gwm_fid;

2SDO_GEOM. WITHIN_DISTANCE

SDO_GEOM.WITHIN_DISTANCE(

geom1 IN SDO_GEOMETRY,

dim1 IN SDO_DIM_ARRAY,

dist IN NUMBER,

geom2 IN SDO_GEOMETRY,

dim2 IN SDO_DIM_ARRAY

[, units IN VARCHAR2]

) RETURN VARCHAR2;

or

SDO_GEOM.WITHIN_DISTANCE(

geom1 IN SDO_GEOMETRY,

dist IN NUMBER,

geom2 IN SDO_GEOMETRY,

tol IN NUMBER

[, units IN VARCHAR2]

) RETURN VARCHAR2;

  参数说明:
    sdo_Geometry1sdo_Geometry2为空间数据对应的几何对象。
    Tolerance: 容许的精度范围;
    Dist: 指定的距离;
    Unit: 用于表示距离的单位,可能是Unit=M/ Unit=KM等长度单位,但必须是SDO_DIST_UNITS表中列举出来的单位之一。

 

SELECT SDO_GEOM.WITHIN_DISTANCE(c_b.shape,

                                m.diminfo,

                                1,

                                c_d.shape,

                                m.diminfo)

 FROM cola_markets c_b, cola_markets c_d, user_sdo_geom_metadata m

 WHERE m.table_name = 'COLA_MARKETS'

   AND m.column_name = 'SHAPE'

   AND c_b.name = 'cola_b'

   AND c_d.name = 'cola_d';

 

SELECT sdo_geom.within_distance(c.shape,50,d.shape,'0.5')

 FROM cola_markets c,cola_markets d

 WHERE c.name = 'cola_d' and d.name='cola_c';

3sdo_Geom.SDO_BUFFER

 SDO_BUFFER   
(  
geometry            IN SDO_GEOMETRY,  
 distance            IN NUMBER,  
 tolerance       IN NUMBER  
[, params       IN VARCHAR2]  
 )  
 RETURNS an SDO_GEOMETRY

其中

geometry是一个参数,表示将被缓冲的SDO_GEOMETRY对象。

distance是一个参数,表示缓冲输入的几何体的数值距离。

tolerance是一个参数,表示容差。

params是一个可选的第四个参数,表示两个参数:unit=<value_string>和arc_ tolerance=<value_number>。

unit=<value_string>参数表示距离的单位。你可以通过查阅MDSYS.SDO_DIST_UNITS表来获得单位的可能取值。

如果几何体是大地测量的(也就是说,如果几何体的SDO_SRID被赋值为大地测量SRID,如8307或者8625),那么 arc_tolerance=<value_number>参数就是必须的。在大地测量的空间里,弧度是不允许的。然而,它们可以近似地用线 表示。弧线的容差参数表示弧线与它的近似线的最大距离。

注:弧线容差通常要大于几何体的容差。

在大地测量数据中,容差是以米为单位来指定的。而arc_tolerance总是使用parameter_string中指定的单位。

例子:

--创建表

CREATE TABLE sales_regions AS 

SELECT gwm_fid,

       SDO_GEOM.SDO_BUFFER(b.gwm_geometry,

                            0.25,

                            0.5,

                            'arc_tolerance=0.005') geom

 FROM i_exch_s b where rownum<10;

由于i_exch_s表中的srid为null,所以arc_tolerance不能指定单位

 

--插元数据

INSERT INTO user_sdo_geom_metadata

 SELECT 'SALES_REGIONS', 'GEOM', diminfo, srid

    FROM user_sdo_geom_metadata

   WHERE table_name = 'I_EXCH_S'

--建空间索引

 CREATE INDEX sr_sidx ON sales_regions(geom) INDEXTYPE IS mdsys.spatial_index;

 

4sdo_Geom.SDO_DISTANCE

SDO_DISTANCE函数的语法如下:

SDO_DISTANCE  

(  

geometry1       IN SDO_GEOMETRY,  

geometry2       IN SDO_GEOMETRY,  

tolerance       IN NUMBER  

[, params       IN VARCHAR2]  

)  

RETURNS a NUMBER 

其中

geometry1和geometry2是起始的两个参数,它们表示SDO_GEOMETRY对象。

tolerance表示数据集的容差。对于大地测量的数据,它们通常是0.5或者0.1(0.5或者是0.1)。对于非大地测量的数据,它将被设置为合适的值,来避免四舍五入引起的错误。

params是可选的第四个参数,是形如'unit=<value_string>'的字符串。这个参数指定了返回距离的单位。可以通过查看MDSYS.SDO_DIST_UNITS表获得可能的单位值。

-- Return the distance between two geometries.

SELECT SDO_GEOM.SDO_DISTANCE(c_b.shape, c_d.shape, 0.005)

 FROM cola_markets c_b, cola_markets c_d

 WHERE c_b.name = 'cola_b'

   AND c_d.name = 'cola_d';

 

SELECT ct.gwm_fid, ct.name

   FROM i_exch_s comp, l_interest_s ct

 WHERE comp.gwm_fid = 52065726

    AND SDO_GEOM.SDO_DISTANCE(ct.gwm_geometry, comp.gwm_geometry, 0.5 /*',unit=mile'*/) < 50

 ORDER BY ct.gwm_fid;

 

SQL中使用带有SDO_WITHIN_DISTANCE空间操作符的SDO_DISTANCE函数

SELECT ct.gwm_fid,

       ct.name,

       SDO_GEOM.SDO_DISTANCE(ct.gwm_geometry,

                             comp.gwm_geometry,

                             0.5 /*,'unit=yard'*/) distance

 FROM i_exch_s comp, l_interest_s ct

 WHERE comp.gwm_fid = 52065726

   AND SDO_WITHIN_DISTANCE(ct.gwm_geometry,

                           comp.gwm_geometry,

                           'distance=50') = 'TRUE'

 ORDER BY ct.gwm_fid;

对于三维的大地测量几何体,计算的距离通常是二维的距离

 

 

5几何组合函数(sdo_Geom.

A SDO_INTERSECTION B:返回A和B共有的区域。

A SDO_UNION B:返回A和B覆盖的区域的并。

A SDO_DIFFERENCE B:返回被A覆盖的但是不被B覆盖的区域。

A SDO_XOR B:返回A和B不相交的区域。这个函数与(A SDO_UNION B) SDO_DIFFERENCE (A SDO_INTERSECTION B)是等同的。

每一个函数都有如下语法:

SDO_<set_theory_fn> 

(  

Geometry_A      IN SDO_GEOMETRY,  

Geometry_B      IN SDO_GEOMETRY,  

Tolerance           IN NUMBER  

)  

RETURNS SDO_GEOMETRY 

 

Geometry_A Geometry_BSDO_GEOMETRY对象(拥有相同的SRID)

Tolerance是几何对象的容差值

注:

Oracle 11g中,几何函数只适用于二维几何体。你不能对两个三维对象进行并操作

 

5.1 SDO_INTERSECTION

CREATE TABLE sales_intersection_zones AS

SELECT sra.gwm_fid id1,

       srb.gwm_fid id2,

       SDO_GEOM.SDO_INTERSECTION(sra.geom, srb.geom, 0.5) intsxn_geom

 FROM sales_regions srb, sales_regions sra

 WHERE sra.gwm_fid <> srb.gwm_fid

   AND SDO_RELATE(sra.geom, srb.geom, 'mask=anyinteract') = 'TRUE';

5.2 SDO_UNION

SELECT count(*)

 FROM (SELECT SDO_GEOM.SDO_UNION(sra.geom, srb.geom, 0.5) geom

          FROM sales_regions srb, sales_regions sra

         WHERE sra.gwm_fid = 50076211

           and srb.gwm_fid = 50076218) srb,

       i_exch_s sra

 WHERE SDO_RELATE(sra.gwm_geometry, srb.geom, 'mask=anyinteract') = 'TRUE';

 

CREATE TABLE sales_region_coverage (coverage SDO_GEOMETRY); 

DECLARE

 coverage SDO_GEOMETRY := NULL

BEGIN

 FOR g IN (SELECT geom FROM sales_regions) LOOP 

 coverage := SDO_GEOM.SDO_UNION(coverage, g.geom, 0.5); 

 END LOOP

 INSERT INTO sales_region_coverage values (coverage); 

 COMMIT

END

 

5.3 SDO_DIFFERENCE

SDO_DIFFERENCE函数是从第一个几何体中减去第二个几何体。结果,它返回的是只属于第一个几何体的区域。注意,它只有在以下情况下才有意义:

第一个和第二个几何体都必须有区域(也就是说,区域为多边形,多重多边形等)

第二个几何体是一个多边形或者一条线并且第一个几何体是一条线。

第一个几何体是一个点。

如果这些条件不满足,SDO_DIFFERENCE操作的结果是返回第一个几何体。

 

--竞争对手区域2和销售区域6SDO_DIFFERENCE操作

CREATE TABLE exclusive_region_for_comp_2 AS

SELECT SDO_GEOM.SDO_DIFFERENCE(b.geom, a.geom, 0.5) geom

 FROM sales_regions sr, competitors_sales_regions csr

 WHERE csr.id = 2

   and sr.id = 6;

 

--在竞争对手的独属区域内确定客户

SELECT ct.id, ct.name

 FROM exclusive_region_for_comp_2 excl, customers ct

 WHERE SDO_RELATE(ct.location, excl.geom, 'mask=anyinteract') = 'TRUE'

 ORDER BY ct.id;

--将以上两个合并为一个

SELECT ct.id, ct.name

 FROM sales_regions sr, competitors_sales_regions csr, customers ct

 WHERE csr.id = 2

   AND sr.id = 6

   AND SDO_RELATE(ct.location,

                  SDO_GEOM.SDO_DIFFERENCE(csr.geom, sr.geom, 0.5),

                  'mask=anyinteract') = 'TRUE'

 ORDER BY ct.id;

 

5.4 SDO_XOR

--不被共享的客户

SELECT count(*)

 FROM (SELECT SDO_GEOM.SDO_XOR(a.geom, b.geom, 0.5) geom

          FROM sales_regions srb, sales_regions sra

         WHERE sra.id = 51

           and srb.id = 43) srb,

       customers sra

 WHERE SDO_RELATE(sra.location, srb.geom, 'mask=anyinteract') = 'TRUE';

 

6几何分析函数(sdo_Geom.

二维的或者三维的几何体上使用这些函数。这些函数有如下的PL/SQL通用语法

Function_name  

(  

Geometry                                IN SDO_GEOMETRY,  

tolerance                           IN NUMBER  

[, units_params IN VARCHAR2]  

)  

RETURN  NUMBER 

其中

Geometry表示将被分析的几何体。

Tolerance表示在这个分析中的容差。

units_params是可选的第三个参数,表示返回的面积、长度和体积的单位。这个参数的 形式是'unit=<value_string>'。可以查看MDSYS.SDO_DIST_UNITS表的length函数和 MDSYS.SDO_AREA_UNITS表的面积函数获得这些单位的可能取值

 

6.1 SDO_AREA

select name,sdo_geom.sdo_area(a.gwm_geometry,0.05) from p_region_area a;

 

6.2 SDO_LENGTH

此函数返回一条线的长度和多边形、平面和立方体的周长。对于点,这个函数返回0

select name,sdo_geom.sdo_length(a.gwm_geometry,0.05) from p_region_area a;

 

6.3 SDO_VOLUME

如果输入的几何体是三维的立方体或者是多重立方体,那么这个函数将一个几何体和一个容差值作为参数并且返回体积。对所有其他的几何体类型,这个函数返回0

 

7MBR最小边界矩形 函数 sdo_Geom.

7.1 SDO_MBR

SDO_GEOM.SDO_MBR(

geom IN SDO_GEOMETRY

[, dim IN SDO_DIM_ARRAY]

) RETURN SDO_GEOMETRY;

 

l         SDO_MBR函数把SDO_GEOMETRY作为一个参数并且计算这个几何体的MBR。它返回的是一个SDO_GEOMETRY对象。

l         如果输入的是一个点,那么SDO_MBR函数返回的是一个点几何体。

l         如果输入的是一条平行于XY轴的线串,那么函数返回一个线性几何体。

否则,函数返回输入几何体的MBR,将它作为一个SDO_GEOMETRY对象。

l         对于一个输入的三维对象,SDO_MBR函数返回一个范围,也就是说,返回这个三

维几何体在三个维度上的最小值和最大值。

-- Return the minimum bounding rectangle of cola_d (a circle).

SELECT SDO_GEOM.SDO_MBR(c.shape, m.diminfo)

 FROM cola_markets c, user_sdo_geom_metadata m

 WHERE m.table_name = 'COLA_MARKETS'

   AND m.column_name = 'SHAPE'

   AND c.name = 'cola_d';

 

:

SDO_AGGR_UNION, SDO_AGGR_CENTROIDSDO_CONVEXHULL这些函数只能在二维几何体上使用。

7.2SDO_MIN_MBR_ORDINATE SDO_MAX_MBR_ORDINATE

除了获得两个维数上的范围,有时你可能对获得指定的维数上的范围感兴趣。你可以通过SDO_MIN_MBR_ORDINATESDO_MAX_MBR_ORDINATE函数来获得指定的维数上的范围,这两个函数返回指定维数上几何体的最小和最大坐标值。

也可以在三维对象上使用这些函数。

SELECT SDO_GEOM.SDO_MIN_MBR_ORDINATE(sr.geom, 1) min_extent,

       SDO_GEOM.SDO_MAX_MBR_ORDINATE(sr.geom, 1) max_extent

 FROM sales_regions sr;

 

SELECT SDO_GEOM.SDO_MIN_MBR_ORDINATE(sr.geom, 3) min_extent,

       SDO_GEOM.SDO_MAX_MBR_ORDINATE(sr.geom, 3) max_extent

 FROM sales_regions sr;

8各种几何分析函数 sdo_Geom.

<Function_name>  

(  

Geometry            IN SDO_GEOMETRY,  

Tolerance       IN NUMBER  

)  

RETURENS SDO_GEOMETRY 

 

8.1 SDO_CONVEXHULL

--该函数可用于简化面的顶点(与原来的面仍保持近似)

select name,sdo_geom.sdo_convexhull(a.gwm_geometry,0.5) from p_region_area a;

 

Oracle 11g中,SDO_CONVEXHULL函数只适用于二维的几何体

8.2 SDO_CENTROID

Oracle 11g中,SDO_CENTROID函数只适用于二维的几何体

点返回的仍是一个点,线返回的是空值,面返回的是该面的质心。

8.3 SDO_POINTONSURFACE

既然一个多边形的质心有可能在也有可能不在这个多边形内,那么在几何体表面的其他点放置一个标签也可能是有用的。在创建多边形地图的一些类型时,这个也是必要的。你可以通过使用SDO_POINTONSURFACE函数来得到这样的点。

也可以在三维的几何体上使用这个函数。

SDO_POINTONSURFACE函数的唯一保证是返回的点在穿过的多边形的边界上或者内部(在目前的实现中,它实际返回的是多边形几何体的SDO_ORDINATE_ARRAY中的第一个点)

SDO_GEOM.SDO_POINTONSURFACE(

geom1 IN SDO_GEOMETRY,

dim1 IN SDO_DIM_ARRAY

) RETURN SDO_GEOMETRY;

or

SDO_GEOM.SDO_POINTONSURFACE(

geom1 IN SDO_GEOMETRY,

tol IN NUMBER

) RETURN SDO_GEOMETRY;

9、聚合函数   非免费 sdo_Geom.

SDO_AGGR_MBR 可用于三维几何体;SDO_AGGR_UNIONSDO_AGGR_CENTROIDSDO_CONVEXHULL函数只适用于二维的几何体

9.1 聚合MBR函数(SDO_AGGR_MBR

假设你想找到SDO_GEOMETRY对象集覆盖的范围(通常,在创建索引之前,需要使用这个信息来填充USER_SDO_GEOM_METADATA视图)

 

9.2 SDO_AGGR_UNION

聚合函数SDO_AGGR_UNION计算几何体集合的并。并以SDO_GEOMETRY对象返回。

9.3 SDO_AGGR_CONVEXHULL

如果所有输入的几何体的所有顶点是共线的或者如果只有一个顶点(一个点),那么SDO_AGGR_CONVEXHULL返回空值

9.4 SDO_AGGR_CENTROID

SDO_AGGR_CENTROID函数允许你计算客户任意组合的质心

10SDO_GEOM.SDO_ARC_DENSIFY

SDO_GEOM.SDO_ARC_DENSIFY(

geom IN SDO_GEOMETRY,

dim IN SDO_DIM_ARRAY

params IN VARCHAR2

) RETURN SDO_GEOMETRY;

or

SDO_GEOM.SDO_ARC_DENSIFY(

geom IN SDO_GEOMETRY,

tol IN NUMBER

params IN VARCHAR2

) RETURN SDO_GEOMETRY;

 

-- Arc densification of the circle cola_d

SELECT c.name,

       SDO_GEOM.SDO_ARC_DENSIFY(c.shape, m.diminfo, 'arc_tolerance=0.05')

 FROM cola_markets c, user_sdo_geom_metadata m

 WHERE m.table_name = 'COLA_MARKETS'

   AND m.column_name = 'SHAPE'

   AND c.name = 'cola_d';

10、验证函数(sdo_Geom.

10.1 VALIDATE_GEOMETRY_WITH_CONTEXT

SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT(

Geometry IN SDO_GEOMETRY,

DimInfo IN SDO_DIM_ARRAY --该参数指定了维度(范围)和容差值的信息

) RETURN VARCHAR2;

or

SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT(

Geometry IN SDO_GEOMETRY,

tolerance IN NUMBER

) RETURN VARCHAR2;

10.2 VALIDATE_LAYER_WITH_CONTEXT

SDO_GEOM.VALIDATE_LAYER_WITH_CONTEXT(

geom_table IN VARCHAR2, --验证的表

geom_column IN VARCHAR2, --验证的列

result_table IN VARCHAR2   --验证结果存放的表

[, commit_interval IN NUMBER]); --验证结果每隔多少个提交一次

 

sdo_util包的函数

1sdo_util.remove_duplicate_vertices

删除重复的顶点

SDO_UTIL.REMOVE_DUPLICATE_VERTICES

geometry IN SDO_GEOMETRY,

tolerance IN NUMBER

) RETURN SDO_GEOMETRY;

2sdo_util.extract(只能提取二维)

SDO_UTIL.EXTRACT(

geometry IN SDO_GEOMETRY, --提取的对象

element IN NUMBER        --提取哪个元素

[, ring IN NUMBER]         --环号,可选

) RETURN SDO_GEOMETRY;

3sdo_util.append

    在给定的容差值内,这个函数将两个输入的几何体合成为单个几何体。

SDO_UTIL.APPEND(

geom1 IN SDO_GEOMETRY,

geom2 IN SDO_GEOMETRY

) RETURN SDO_GEOMETRY

4sdo_util.GetNumElem

获取元素的数目(即由几个元素组成)

SDO_UTIL.GETNUMELEM(

geometry IN SDO_GEOMETRY

) RETURN NUMBER;

5sdo_util.GetNumVertices

获取对象的顶点数目

SDO_UTIL.GETNUMVERTICES(

geometry IN SDO_GEOMETRY

) RETURN NUMBER;

6sdo_util.getvertices

SDO_UTIL.GETVERTICES(

geometry IN SDO_GEOMETRY

) RETURN VERTEX_SET_TYPE;

 

7SDO_UTIL.CIRCLE_POLYGON

返回一个圆

SDO_UTIL.CIRCLE_POLYGON(

center_longitude IN NUMBER,

center_latitude IN NUMBER,

radius IN NUMBER,

arc_tolerance IN NUMBER

) RETURN SDO_GEOMETRY;

SELECT SDO_UTIL.CIRCLE_POLYGON(-71.34937, 42.46101, 100, 5) FROM DUAL;

 

8SDO_UTIL.ELLIPSE_POLYGON

返回一个椭圆

SDO_UTIL.ELLIPSE_POLYGON(

center_longitude IN NUMBER,

center_latitude IN NUMBER,

semi_major_axis IN NUMBER,

semi_minor_axis IN NUMBER,

azimuth IN NUMBER,

arc_tolerance IN NUMBER

) RETURN SDO_GEOMETRY;

SELECT SDO_UTIL.ELLIPSE_POLYGON(-71.34937, 42.46101, 100, 50, 90, 5)FROM DUAL;

9SDO_UTIL.CONVERT_UNIT

单位转换

SDO_UTIL.CONVERT_UNIT(

input_value IN NUMBER,

from_unit IN VARCHAR2,

to_unit IN VARCHAR2

) RETURN NUMBER;

SELECT SDO_UTIL.CONVERT_UNIT(1, 'Radian', 'Degree') FROM DUAL;

10wktwkbgeometry互转与验证

Ø         SDO_UTIL.FROM_WKBGEOMETRY

Ø         SDO_UTIL.FROM_WKTGEOMETRY

Ø         SDO_UTIL.TO_WKBGEOMETRY

Ø         SDO_UTIL.TO_WKTGEOMETRY

Ø         SDO_UTIL.VALIDATE_WKBGEOMETRY

Ø         SDO_UTIL.VALIDATE_WKTGEOMETRY

 

 

DECLARE

wkbgeom BLOB;

wktgeom CLOB;

val_result VARCHAR2(5);

geom_result SDO_GEOMETRY;

geom SDO_GEOMETRY;

BEGIN

SELECT c.shape INTO geom FROM cola_markets c WHERE c.name = 'cola_b';

-- To WBT/WKT geometry

wkbgeom := SDO_UTIL.TO_WKBGEOMETRY(geom);

wktgeom := SDO_UTIL.TO_WKTGEOMETRY(geom);

DBMS_OUTPUT.PUT_LINE('To WKT geometry result = ' || TO_CHAR(wktgeom));

-- From WBT/WKT geometry

geom_result := SDO_UTIL.FROM_WKBGEOMETRY(wkbgeom);

geom_result := SDO_UTIL.FROM_WKTGEOMETRY(wktgeom);

-- Validate WBT/WKT geometry

val_result := SDO_UTIL.VALIDATE_WKBGEOMETRY(wkbgeom);

DBMS_OUTPUT.PUT_LINE('WKB validation result = ' || val_result);

val_result := SDO_UTIL.VALIDATE_WKTGEOMETRY(wktgeom);

DBMS_OUTPUT.PUT_LINE('WKT validation result = ' || val_result);

END;

11GMLGEOMETRY转换

 

SDO_UTIL.TO_GMLGEOMETRY(

thegeom IN SDO_GEOMETRY

) RETURN CLOB;

SELECT TO_CHAR(SDO_UTIL.TO_GMLGEOMETRY(shape)) AS GmlGeometry

 FROM COLA_MARKETS c

 WHERE c.name = 'cola_b';

12SDO_UTIL.SIMPLIFY

根据输入的阈值来简化输入的对象

SDO_UTIL.SIMPLIFY(

geometry IN SDO_GEOMETRY,

threshold IN NUMBER

tolerance IN NUMBER DEFAULT 0.0000005

) RETURN SDO_GEOMETRY;

 

SELECT SDO_UTIL.SIMPLIFY(

SDO_GEOMETRY(

3302, -- line string, 3 dimensions (X,Y,M), 3rd is linear ref. dimension

NULL,

NULL,

SDO_ELEM_INFO_ARRAY(1,2,1), -- one line string, straight segments

SDO_ORDINATE_ARRAY(

2,2,0, -- Starting point - Exit1; 0 is measure from start.

2,4,2, -- Exit2; 2 is measure from start.

8,4,8, -- Exit3; 8 is measure from start.

12,4,12, -- Exit4; 12 is measure from start.

12,10,NULL, -- Not an exit; measure automatically calculated and filled.

8,10,22, -- Exit5; 22 is measure from start.

5,14,27) -- Ending point (Exit6); 27 is measure from start.

),

6, -- threshold value for geometry simplification

0.5 -- tolerance

) FROM DUAL;

13SDO_UTIL.RECTIFY_GEOMETRY

纠正有错误的对象并正确输出

SDO_UTIL.RECTIFY_GEOMETRY(

geometry IN SDO_GEOMETRY,

tolerance IN NUMBER

) RETURN SDO_GEOMETRY;

SELECT SDO_UTIL.RECTIFY_GEOMETRY(shape, 0.005)

FROM COLA_MARKETS c WHERE c.name = 'cola_b';

 

14SDO_UTIL.PREPARE_FOR_TTSSDO_UTIL.INITIALIZE_INDEXES_FOR_TTS

在不同的Oracle数据库之间传输数据可通过表空间。而为了保证表空间上的表的空间索引也被传输,需要执行这些函数SDO_UTIL.PREPARE_FOR_TTS(在传输表空间前执行)SDO_UTIL.INITIALIZE_INDEXES_FOR_TTS(在表空间导出后执行)

例子:创建一个dmp文件,从一个源数据库中传输表空间tbs

首先:从一个源数据库中传输表空间tbs

Sqlplus spatial/ spatial

Execute SDO_UTIL.PREPARE_FOR_TTS(‘TBS’)

Connect system/manager as sysdba

Execute dbms_tts.transport_set_check(‘TBS’,true);

Alter tablespace TBS read only;

Exit;

 

Exp spatial/ spatial transport_ tablespace=y tablespaces=TBS file=trans_ts.dmp

 

然后导入

ixp spatial/ spatial transport_ tablespace=y file=trans_ts.dmp datafiles=’sdo_tts.dbf’ tablespaces=tbs

 

Sqlplus sys/password

Alter tablespace TBS read write;

Connect spatial/ spatial;

Exec SDO_UTIL.INITIALIZE_INDEXES_FOR_TTS

 

 

15sdo_util.extract3d

提取三维对象的元素

16sdo_util.sdo_concat_lines

连接两个线串

SDO_UTIL.CONCAT_LINES(

geom1 IN SDO_GEOMETRY,

geom2 IN SDO_GEOMETRY

) RETURN SDO_GEOMETRY;

17sdo_util.sdo_reverse_linestring

倒转一条线串中的顶点的顺序

SDO_UTIL.REVERSE_LINESTRING(

geometry IN SDO_GEOMETRY

) RETURN SDO_GEOMETRY;

18sdo_util.sdo_polygontoline

将多边形转换为线串几何体

SDO_UTIL.POLYGONTOLINE(

geometry IN SDO_GEOMETRY

) RETURN SDO_GEOMETRY;

 

1518非免费

 

sdo_lrs

Subprograms for Creating and Editing Geometric Segments

1SDO_LRS.DEFINE_GEOM_SEGMENT(过程)

语法:

SDO_LRS.DEFINE_GEOM_SEGMENT(

geom_segment IN OUT SDO_GEOMETRY

[, start_measure IN NUMBER,

end_measure IN NUMBER]);

or

SDO_LRS.DEFINE_GEOM_SEGMENT(

geom_segment IN OUT SDO_GEOMETRY,

dim_array IN SDO_DIM_ARRAY

[, start_measure IN NUMBER,

end_measure IN NUMBER]);

 

 

    功能:根据起始点和终止点测量值计算出没有测量的点

例子:

-- Test the LRS procedures.

DECLARE

geom_segment SDO_GEOMETRY;

line_string SDO_GEOMETRY;

dim_array SDO_DIM_ARRAY;

result_geom_1 SDO_GEOMETRY;

result_geom_2 SDO_GEOMETRY;

result_geom_3 SDO_GEOMETRY;

BEGIN

SELECT a.route_geometry into geom_segment FROM lrs_routes a

WHERE a.route_name = 'Route1';

SELECT m.diminfo into dim_array from

user_sdo_geom_metadata m

WHERE m.table_name = 'LRS_ROUTES' AND m.column_name = 'ROUTE_GEOMETRY';

-- Define the LRS segment for Route1. This will populate any null measures.

-- No need to specify start and end measures, because they are already defined in the geometry.

SDO_LRS.DEFINE_GEOM_SEGMENT (geom_segment, dim_array);

SELECT a.route_geometry INTO line_string FROM lrs_routes a

WHERE a.route_name = 'Route1';

-- Split Route1 into two segments.

SDO_LRS.SPLIT_GEOM_SEGMENT(line_string,dim_array,5,result_geom_1,result_geom_2);

-- Concatenate the segments that were just split.

result_geom_3 := SDO_LRS.CONCATENATE_GEOM_SEGMENTS(result_geom_1, dim_array,

result_geom_2, dim_array);

-- Update and insert geometries into table, to display later.

UPDATE lrs_routes a SET a.route_geometry = geom_segment

WHERE a.route_id = 1;

INSERT INTO lrs_routes VALUES(

11,

'result_geom_1',

result_geom_1

);

INSERT INTO lrs_routes VALUES(

12,

'result_geom_2',

result_geom_2

);

INSERT INTO lrs_routes VALUES(

13,

'result_geom_3',

result_geom_3

);

END;

 

2SDO_LRS.REDEFINE_GEOM_SEGMENT(过程)

语法:

SDO_LRS.REDEFINE_GEOM_SEGMENT(

geom_segment IN OUT SDO_GEOMETRY

[, start_measure IN NUMBER,

end_measure IN NUMBER]);

or

SDO_LRS.REDEFINE_GEOM_SEGMENT(

geom_segment IN OUT SDO_GEOMETRY,

dim_array IN SDO_DIM_ARRAY

[, start_measure IN NUMBER,

end_measure IN NUMBER]);

功能:重新定义几何体(可进行单位转换)

例子:

-- Redefine geometric segment to "convert" miles to kilometers

DECLARE

geom_segment SDO_GEOMETRY;

dim_array SDO_DIM_ARRAY;

BEGIN

SELECT a.route_geometry into geom_segment FROM lrs_routes a WHERE a.route_name = 'Route1';

SELECT m.diminfo into dim_array from user_sdo_geom_metadata m WHERE m.table_name = 'LRS_ROUTES' AND m.column_name = 'ROUTE_GEOMETRY';

-- "Convert" mile measures to kilometers (27 * 1.609 = 43.443).

SDO_LRS.REDEFINE_GEOM_SEGMENT (geom_segment,

dim_array,

0, -- Zero starting measure: LRS segment starts at start of route.

43.443); -- End of LRS segment. 27 miles = 43.443 kilometers.

-- Update and insert geometries into table, to display later.

UPDATE lrs_routes a SET a.route_geometry = geom_segment WHERE a.route_id = 1;

END;

 

3SDO_LRS.CLIP_GEOM_SEGMENT

语法:

SDO_LRS.CLIP_GEOM_SEGMENT(

geom_segment IN SDO_GEOMETRY,

start_measure IN NUMBER,

end_measure IN NUMBER,

tolerance IN NUMBER DEFAULT 1.0e-8

) RETURN SDO_GEOMETRY;

or

SDO_LRS.CLIP_GEOM_SEGMENT(

geom_segment IN SDO_GEOMETRY,

dim_array IN SDO_DIM_ARRAY,

start_measure IN NUMBER,

end_measure IN NUMBER

) RETURN SDO_GEOMETRY;

功能:截取指定的一段

例子:

-- Clip a piece of Route1.

SELECT SDO_LRS.CLIP_GEOM_SEGMENT(route_geometry, 5, 10) FROM lrs_routes WHERE route_id = 1;

 

4SDO_LRS.DYNAMIC_SEGMENT

语法:

SDO_LRS.DYNAMIC_SEGMENT(

geom_segment IN SDO_GEOMETRY,

start_measure IN NUMBER,

end_measure IN NUMBER,

tolerance IN NUMBER DEFAULT 1.0e-8

) RETURN SDO_GEOMETRY;

or

SDO_LRS.DYNAMIC_SEGMENT(

geom_segment IN SDO_GEOMETRY,

dim_array IN SDO_DIM_ARRAY,

start_measure IN NUMBER,

end_measure IN NUMBER

) RETURN SDO_GEOMETRY;

用法与SDO_LRS.CLIP_GEOM_SEGMENT一样

5 SDO_LRS.CONCATENATE_GEOM_SEGMENTS

语法:

SDO_LRS.CONCATENATE_GEOM_SEGMENTS(

geom_segment_1 IN SDO_GEOMETRY,

geom_segment_2 IN SDO_GEOMETRY,

tolerance IN NUMBER DEFAULT 1.0e-8

) RETURN SDO_GEOMETRY;

or

SDO_LRS.CONCATENATE_GEOM_SEGMENTS(

geom_segment_1 IN SDO_GEOMETRY,

dim_array_1 IN SDO_DIM_ARRAY,

geom_segment_2 IN SDO_GEOMETRY,

dim_array_2 IN SDO_DIM_ARRAY

) RETURN SDO_GEOMETRY;

功能:连接线串,例子见1

6SDO_LRS.OFFSET_GEOM_SEGMENT

语法:

SDO_LRS.OFFSET_GEOM_SEGMENT(

geom_segment IN SDO_GEOMETRY,

start_measure IN NUMBER,

end_measure IN NUMBER,

offset IN NUMBER,

tolerance IN NUMBER DEFAULT 1.0e-8

[, unit IN VARCHAR2]

) RETURN SDO_GEOMETRY;

or

SDO_LRS.OFFSET_GEOM_SEGMENT(

geom_segment IN SDO_GEOMETRY,

dim_array IN SDO_DIM_ARRAY,

start_measure IN NUMBER,

end_measure IN NUMBER,

offset IN NUMBER

[, unit IN VARCHAR2]

) RETURN SDO_GEOMETRY;

功能:指定的线段内进行偏移

例子:

SELECT SDO_LRS.OFFSET_GEOM_SEGMENT(a.route_geometry, m.diminfo, 5, 10, 2)

 FROM lrs_routes a, user_sdo_geom_metadata m

 WHERE m.table_name = 'LRS_ROUTES'

   AND m.column_name = 'ROUTE_GEOMETRY'

   AND a.route_id = 1;

7SDO_LRS.SPLIT_GEOM_SEGMENT(过程)

语法:

SDO_LRS.SPLIT_GEOM_SEGMENT(

geom_segment IN SDO_GEOMETRY,

split_measure IN NUMBER,

segment_1 OUT SDO_GEOMETRY,

segment_2 OUT SDO_GEOMETRY);

or

SDO_LRS.SPLIT_GEOM_SEGMENT(

geom_segment IN SDO_GEOMETRY,

dim_array IN SDO_DIM_ARRAY,

split_measure IN NUMBER,

segment_1 OUT SDO_GEOMETRY,

segment_2 OUT SDO_GEOMETRY);

功能:将一个几何体分为两个

例子见1

8SDO_LRS.RESET_MEASURE(过程)

语法:

SDO_LRS.RESET_MEASURE(

geom_segment IN OUT SDO_GEOMETRY

[, dim_array IN SDO_DIM_ARRAY]);

功能:将起始点到终止点的测量值都清空

例子:

-- Reset geometric segment measures.

DECLARE

geom_segment SDO_GEOMETRY;

BEGIN

SELECT a.route_geometry into geom_segment FROM lrs_routes a

WHERE a.route_name = 'Route1';

SDO_LRS.RESET_MEASURE (geom_segment);

-- Update and insert geometries into table, to display later.

UPDATE lrs_routes a SET a.route_geometry = geom_segment

WHERE a.route_id = 1;

END;

9SDO_LRS.SET_PT_MEASURE

语法:

SDO_LRS.SET_PT_MEASURE(

geom_segment IN OUT SDO_GEOMETRY,

point IN SDO_GEOMETRY,

measure IN NUMBER) RETURN VARCHAR2;

or

SDO_LRS.SET_PT_MEASURE(

geom_segment IN OUT SDO_GEOMETRY,

dim_array IN SDO_DIM_ARRAY,

point IN SDO_GEOMETRY,

pt_dim_array IN SDO_DIM_ARRAY,

measure IN NUMBER) RETURN VARCHAR2;

or

SDO_LRS.SET_PT_MEASURE(

point IN OUT SDO_GEOMETRY,

measure IN NUMBER) RETURN VARCHAR2;

or

SDO_LRS.SET_PT_MEASURE(

point IN OUT SDO_GEOMETRY,

dim_array IN SDO_DIM_ARRAY,

measure IN NUMBER) RETURN VARCHAR2;

功能:将测量值设为一个特定的点

-- Set the measure value of point 8,10 to 20 (originally 22).

DECLARE

geom_segment SDO_GEOMETRY;

dim_array SDO_DIM_ARRAY;

result VARCHAR2(32);

BEGIN

SELECT a.route_geometry into geom_segment FROM lrs_routes a

WHERE a.route_name = 'Route1';

SELECT m.diminfo into dim_array from

user_sdo_geom_metadata m

WHERE m.table_name = 'LRS_ROUTES' AND m.column_name = 'ROUTE_GEOMETRY';

-- Set the measure value of point 8,10 to 20 (originally 22).

result := SDO_LRS.SET_PT_MEASURE (geom_segment,

SDO_GEOMETRY(3301, NULL, NULL,

             SDO_ELEM_INFO_ARRAY(1, 1, 1),

             SDO_ORDINATE_ARRAY(8, 10, 22)),

20);

-- Display the result.

DBMS_OUTPUT.PUT_LINE('Returned value = ' || result);

END;

10SDO_LRS.REVERSE_MEASURE

11SDO_LRS.TRANSLATE_MEASURE

12SDO_LRS.REVERSE_GEOMETRY

Subprograms for Querying and Validating Geometric Segments

13SDO_LRS.VALID_GEOM_SEGMENT

14SDO_LRS.VALID_LRS_PT

15SDO_LRS.VALID_MEASURE  

16SDO_LRS.CONNECTED_GEOM_SEGMENTS

17SDO_LRS.GEOM_SEGMENT_LENGTH  

18SDO_LRS.GEOM_SEGMENT_START_PT

19SDO_LRS.GEOM_SEGMENT_END_PT

20SDO_LRS.GEOM_SEGMENT_START_MEASURE

21SDO_LRS.GEOM_SEGMENT_END_MEASURE

22SDO_LRS.GET_MEASURE  

23SDO_LRS.GET_NEXT_SHAPE_PT

24SDO_LRS.GET_NEXT_SHAPE_PT_MEASURE

25SDO_LRS.GET_PREV_SHAPE_PT  

26SDO_LRS.GET_PREV_SHAPE_PT_MEASURE

27SDO_LRS.IS_GEOM_SEGMENT_DEFINED

28SDO_LRS.IS_MEASURE_DECREASING

29SDO_LRS.IS_MEASURE_INCREASING

30SDO_LRS.IS_SHAPE_PT_MEASURE

31SDO_LRS.MEASURE_RANGE

32SDO_LRS.MEASURE_TO_PERCENTAGE

33SDO_LRS.PERCENTAGE_TO_MEASURE

34SDO_LRS.LOCATE_PT

35SDO_LRS.PROJECT_PT

36SDO_LRS.FIND_LRS_DIM_POS

37SDO_LRS.FIND_MEASURE

38SDO_LRS.FIND_OFFSET

39SDO_LRS.VALIDATE_LRS_GEOMETRY

Subprograms for Converting Geometric Segments

40SDO_LRS.CONVERT_TO_LRS_DIM_ARRAY

41SDO_LRS.CONVERT_TO_LRS_GEOM

42SDO_LRS.CONVERT_TO_LRS_LAYER

43SDO_LRS.CONVERT_TO_STD_DIM_ARRAY

44SDO_LRS.CONVERT_TO_STD_GEOM

45SDO_LRS.CONVERT_TO_STD_LAYER

四、SDO_MIGRATE

SDO_MIGRATE.TO_CURRENT

Format (Any Object-Relational Model Implementation to Current)

SDO_MIGRATE.TO_CURRENT(

tabname IN VARCHAR2

[, column_name IN VARCHAR2]);

or

SDO_MIGRATE.TO_CURRENT(

tabname IN VARCHAR2,

column_name IN VARCHAR2

[, commit_int IN NUMBER]);

Format (Single Object-Relational Model Geometry to Current)

SDO_MIGRATE.TO_CURRENT(

geom IN SDO_GEOMETRY,

dim IN SDO_DIM_ARRAY

) RETURN SDO_GEOMETRY;

Format (Any Relational Model Implementation to Current)

SDO_MIGRATE.TO_CURRENT(

layer IN VARCHAR2,

newtabname IN VARCHAR2,

gidcolumn IN VARCHAR2,

geocolname IN VARCHAR2,

layer_gtype IN VARCHAR2,

updateflag IN VARCHAR2);

 

EXECUTE SDO_MIGRATE.TO_CURRENT('ROADS');

 

 

你可能感兴趣的:(oracle)