Oracle Spatial操作geometry方法

时间:2022-02-28 16:15:46

Oracle Spatial中SDO_GEOMETRY类型:

CREATE TYPE SDO_GEOMETRY AS OBJECT(

SDO_GTYPE NUMBER,--几何类型,如点线面

SDO_SRID NUMBER,--几何的空间参考坐标系

SDO_POINT SDO_POINT_TYPE,--如果几何为点类型,则存储点坐标,否则为空

SDO_ELEM_INFO SDO_ELEM_INFO_ARRAY,--定义如何理解SDO_ORDINATES中的坐标序列

SDO_ORDINATES SDO_ORDINATE_ARRAY--存储实际坐标,以X,Y以及不同点之间以逗号隔开

)

两个对象之间关系:

RELATE

SDO_GEOM.RELATE --确定两个对象的交互方式

eg:

SDO_GEOM.RELATE(geom1 IN SDO_GEOMETRY,dim1 IN SDO_DIM_ARRAY,mask IN VARCHAR2, geom2 IN SDO_GEOMETRY,dim2 IN SDO_DIM_ARRAY) RETURN VARCHAR2;
SDO_GEOM.RELATE(geom1 IN SDO_GEOMETRY,mask IN VARCHAR2,geom2 IN SDO_GEOMETRY, tol IN NUMBER)RETURN VARCHAR2;--tol容许的精度范围

mask:

ANYINTERACT:geom2落在geom1面上,包括边上

CONTAINS:geom2完全包含在geom1几何对象中,并且两个几何对象的边没有交叉

COVEREDBY:geom1完全包含在geom2中,并且两个几何对象的边有一个或多个点相互重叠

COVERS:geom2完全包含在geom1中,并且两个几何对象的边有一个或多个点相互重叠

DISJOINT:两个几何没有重叠交叉点,也没有共同的边

EQUAL:两个几何相等

INSIDE:geom1完全包含在geom2几何对象中,并且两个几何对象的边没有交叉

ON:geom1的边和内部的线完全在geom2上

OVERLAPBDYDISJOINT:两个几何对象交迭,但是边没有交叉

OVERLAPBDYINTERSECT:两个几何对象叫迭,并且边有部分交叉

TOUCH:两个结合对象有共同的边,但没有交叉

WITHIN_DISTANCE

SDO_GEOM.WITHIN_DISTANCE --确定两个几何是否在彼此指定的距离之内

eg:

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;
SDO_GEOM.WITHIN_DISTANCE(geom1 IN SDO_GEOMETRY,dist IN NUMBER,geom2 IN SDO_GEOMETRY,tol IN NUMBER[, units IN VARCHAR2]) RETURN VARCHAR2;

验证:

VALIDATE_GEOMETRY_WITH_CONTEXT

SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT --确定几何是否有效,如果几何无效,则返回上下文信息

eg:

SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT(theGeometry IN SDO_GEOMETRY, theDimInfo IN SDO_DIM_ARRAY,conditional IN VARCHAR2 DEFAULT 'TRUE') RETURN VARCHAR2;
SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT(theGeometry IN SDO_GEOMETRY,tolerance IN NUMBER,conditional IN VARCHAR2 DEFAULT 'TRUE') RETURN VARCHAR2;

VALIDATE_LAYER_WITH_CONTEXT

SDO_GEOM.VALIDATE_LAYER_WITH_CONTEXT --确定存储在列中的所有几何是否有效,并返回有关任何无效几何的上下文信息

eg:

SDO_GEOM.VALIDATE_LAYER_WITH_CONTEXT(geom_table IN VARCHAR2,geom_column IN VARCHAR2,result_table IN VARCHAR2,commit_interval IN NUMBER DEFAULT -1,conditional IN VARCHAR2 DEFAULT 'TRUE');

单对象操作:

SDO_ARC_DENSIFY

SDO_GEOM.SDO_ARC_DENSIFY --简化,将圆弧简化成由直线组成的近似多段线;将圆近似成N多边形

eg:

SDO_GEOM.SDO_ARC_DENSIFY(geom IN SDO_GEOMETRY,dim IN SDO_DIM_ARRAY, params IN VARCHAR2) RETURN SDO_GEOMETRY;
SDO_GEOM.SDO_ARC_DENSIFY(geom IN SDO_GEOMETRY,tol IN NUMBER, params IN VARCHAR2) RETURN SDO_GEOMETRY;

SDO_AREA

SDO_GEOM.SDO_AREA --计算多边形的面积

eg:

SDO_GEOM.SDO_AREA(geom IN SDO_GEOMETRY,dim IN SDO_DIM_ARRAY [, unit IN VARCHAR2]) RETURN NUMBER;
SDO_GEOM.SDO_AREA(geom IN SDO_GEOMETRY,tol IN NUMBER [, unit IN VARCHAR2]) RETURN NUMBER;

SDO_BUFFER

SDO_GEOM.SDO_BUFFER --在几何体外部或内部生成缓冲区

eg:

SDO_GEOM.SDO_BUFFER(geom IN SDO_GEOMETRY,dim IN SDO_DIM_ARRAY,dist IN NUMBER[, params IN VARCHAR2]) RETURN SDO_GEOMETRY;
SDO_GEOM.SDO_BUFFER(geom IN SDO_GEOMETRY,dist IN NUMBER,tol IN NUMBER[, params IN VARCHAR2]) RETURN SDO_GEOMETRY;

SDO_CENTROID

SDO_GEOM.SDO_CENTROID --计算多边形的质心

eg:

SDO_GEOM.SDO_CENTROID(geom1 IN SDO_GEOMETRY,dim1 IN SDO_DIM_ARRAY) RETURN SDO_GEOMETRY;
SDO_GEOM.SDO_CENTROID(geom1 IN SDO_GEOMETRY,tol IN NUMBER) RETURN SDO_GEOMETRY;

SDO_CONVEXHULL

SDO_GEOM.SDO_CONVEXHULL --返回几何对象的凸包的多边形类型对象

eg:

SDO_GEOM.SDO_CONVEXHULL(geom1 IN SDO_GEOMETRY,dim1 IN SDO_DIM_ARRAY) RETURN SDO_GEOMETRY;
SDO_GEOM.SDO_CONVEXHULL(geom1 IN SDO_GEOMETRY, tol IN NUMBER) RETURN SDO_GEOMETRY;

SDO_LENGTH

SDO_GEOM.SDO_LENGTH --计算几何的长度或周长

eg:

SDO_GEOM.SDO_LENGTH(geom IN SDO_GEOMETRY,dim IN SDO_DIM_ARRAY[, unit IN VARCHAR2][, count_shared_edges IN NUMBER]) RETURN NUMBER;
SDO_GEOM.SDO_LENGTH(geom IN SDO_GEOMETRY,tol IN NUMBER[, unit IN VARCHAR2] [, count_shared_edges IN NUMBER]) RETURN NUMBER;

SDO_MAX_MBR_ORDINATE

SDO_GEOM.SDO_MAX_MBR_ORDINATE --返回几何对象最小边界矩形的指定纵坐标的最大值

eg:

SDO_GEOM.SDO_MAX_MBR_ORDINATE(geom IN SDO_GEOMETRY,ordinate_pos IN NUMBER) RETURN NUMBER;
SDO_GEOM.SDO_MAX_MBR_ORDINATE(geom IN SDO_GEOMETRY,dim IN SDO_DIM_ARRAY,ordinate_pos IN NUMBER) RETURN NUMBER;

SDO_MIN_MBR_ORDINATE

SDO_GEOM.SDO_MIN_MBR_ORDINATE --返回几何对象最小边界矩形的指定纵坐标的最小值

eg:

SDO_GEOM.SDO_MIN_MBR_ORDINATE(geom IN SDO_GEOMETRY,ordinate_pos IN NUMBER ) RETURN NUMBER;
SDO_GEOM.SDO_MIN_MBR_ORDINATE(geom IN SDO_GEOMETRY,dim IN SDO_DIM_ARRAY,ordinate_pos IN NUMBER) RETURN NUMBER;

SDO_MBR

SDO_GEOM.SDO_MBR --返回几何对象最小边界矩形

eg:

SDO_GEOM.SDO_MBR(geom IN SDO_GEOMETRY[, dim IN SDO_DIM_ARRAY]) RETURN SDO_GEOMETRY;

SDO_POINTONSURFACE

SDO_GEOM.SDO_POINTONSURFACE --返回一个保证在多边形表面上的点

eg:

SDO_GEOM.SDO_POINTONSURFACE(geom1 IN SDO_GEOMETRY,dim1 IN SDO_DIM_ARRAY) RETURN SDO_GEOMETRY;
SDO_GEOM.SDO_POINTONSURFACE(geom1 IN SDO_GEOMETRY,tol IN NUMBER) RETURN SDO_GEOMETRY;

SDO_VOLUME

SDO_GEOM.SDO_VOLUME --计算三维立体几何体的体积

eg:

SDO_GEOM.SDO_VOLUME(geom IN SDO_GEOMETRY,tol IN NUMBER[, unit IN VARCHAR2]) RETURN NUMBER;

双目标操作:

SDO_CLOSEST_POINTS

SDO_GEOM.SDO_CLOSEST_POINTS --计算两个几何之间的最小距离,并返回距离最小时在几何上的两点

eg:

SDO_GEOM.SDO_CLOSEST_POINTS(geom1 IN SDO_GEOMETRY,geom2 IN SDO_GEOMETRY,tolerance IN NUMBER,unit IN VARCHAR2,dist OUT NUMBER,geoma OUT SDO_GEOMETRY,geomb OUT SDO_GEOMETRY);

SDO_DISTANCE

SDO_GEOM.SDO_DISTANCE --计算两个几何对象之间的距离

eg:

SDO_GEOM.SDO_DISTANCE(geom1 IN SDO_GEOMETRY,dim1 IN SDO_DIM_ARRAY,geom2 IN SDO_GEOMETRY,dim2 IN SDO_DIM_ARRAY[, unit IN VARCHAR2]) RETURN NUMBER;
SDO_GEOM.SDO_DISTANCE(geom1 IN SDO_GEOMETRY,geom2 IN SDO_GEOMETRY,tol IN NUMBER [, unit IN VARCHAR2]) RETURN NUMBER;

SDO_DIFFERENCE

SDO_GEOM.SDO_DIFFERENCE --返回几何对象(两个几何对象的拓扑差异,MINUS操作)

eg:

SDO_GEOM.SDO_DIFFERENCE(geom1 IN SDO_GEOMETRY,dim1 IN SDO_DIM_ARRAY,geom2 IN SDO_GEOMETRY,dim2 IN SDO_DIM_ARRAY) RETURN SDO_GEOMETRY;
SDO_GEOM.SDO_DIFFERENCE(geom1 IN SDO_GEOMETRY,geom2 IN SDO_GEOMETRY, tol IN NUMBER) RETURN SDO_GEOMETRY;

SDO_INTERSECTION

SDO_GEOM.SDO_INTERSECTION --返回几何对象(两个几何对象的拓扑交点,AND操作)

eg:

SDO_GEOM.SDO_INTERSECTION(geom1 IN SDO_GEOMETRY,dim1 IN SDO_DIM_ARRAY,geom2 IN SDO_GEOMETRY,dim2 IN SDO_DIM_ARRAY) RETURN SDO_GEOMETRY;
SDO_GEOM.SDO_INTERSECTION(geom1 IN SDO_GEOMETRY,geom2 IN SDO_GEOMETRY, tol IN NUMBER) RETURN SDO_GEOMETRY;

SDO_UNION

SDO_GEOM.SDO_UNION --返回几何对象(两个几何对象的拓扑结合,OR操作)

eg:

SDO_GEOM.SDO_UNION(geom1 IN SDO_GEOMETRY,dim1 IN SDO_DIM_ARRAY, geom2 IN SDO_GEOMETRY,dim2 IN SDO_DIM_ARRAY) RETURN SDO_GEOMETRY;
SDO_GEOM.SDO_UNION(geom1 IN SDO_GEOMETRY,geom2 IN SDO_GEOMETRY, tol IN NUMBER ) RETURN SDO_GEOMETRY;

SDO_XOR

SDO_GEOM.SDO_XOR --返回几何对象(两个几何对象的拓扑对称差异,XOR操作)

eg:

SDO_GEOM.SDO_XOR(geom1 IN SDO_XOR,dim1 IN SDO_DIM_ARRAY,geom2 IN SDO_GEOMETRY,dim2 IN SDO_DIM_ARRAY) RETURN SDO_GEOMETRY;
SDO_GEOM.SDO_XOR(geom1 IN SDO_GEOMETRY,geom2 IN SDO_GEOMETRY, tol IN NUMBER ) RETURN SDO_GEOMETRY;

实例:

--wkt转geometry
select sdo_geometry(TO_CHAR(SDO_GEOMETRY(2003, NULL, NULL, SDO_ELEM_INFO_ARRAY(1, 1003, 1), SDO_ORDINATE_ARRAY(5, 5, 5, 7, 1, 7, 1, 1, 5, 1, 5, 3, 6, 3, 6, 5, 5, 5)).get_wkt())) from dual
--wkt转geometry
select SDO_UTIL.from_wktgeometry(TO_CHAR(SDO_GEOMETRY(2003, NULL, NULL, SDO_ELEM_INFO_ARRAY(1, 1003, 1), SDO_ORDINATE_ARRAY(5, 5, 5, 7, 1, 7, 1, 1, 5, 1, 5, 3, 6, 3, 6, 5, 5, 5)).get_wkt())) from dual
--geometry转wkt
select TO_CHAR(SDO_GEOMETRY(2003, NULL, NULL, SDO_ELEM_INFO_ARRAY(1, 1003, 1), SDO_ORDINATE_ARRAY(5, 5, 5, 7, 1, 7, 1, 1, 5, 1, 5, 3, 6, 3, 6, 5, 5, 5)).get_wkt()) from dual
--geometry转wkt
select SDO_UTIL.to_wktgeometry_varchar(SDO_GEOMETRY(2003, NULL, NULL, SDO_ELEM_INFO_ARRAY(1, 1003, 1), SDO_ORDINATE_ARRAY(5, 5, 5, 7, 1, 7, 1, 1, 5, 1, 5, 3, 6, 3, 6, 5, 5, 5))) from dual
--geometry转wkt(不受oracle字段只能少于4000个字符长度影响)
select SDO_UTIL.to_wktgeometry(SDO_GEOMETRY(2003, NULL, NULL, SDO_ELEM_INFO_ARRAY(1, 1003, 1), SDO_ORDINATE_ARRAY(5, 5, 5, 7, 1, 7, 1, 1, 5, 1, 5, 3, 6, 3, 6, 5, 5, 5))) from dual
--合并
select SDO_GEOM.SDO_UNION(SDO_GEOMETRY('POLYGON((1 1,5 1,5 5,1 5,1 1))'),SDO_GEOMETRY('POLYGON((2 2,2 3,3 3,3 2,2 2))'),0.001 ) from dual;
--缓冲区
select SDO_UTIL.to_wktgeometry_varchar(SDO_GEOM.SDO_BUFFER(SDO_GEOMETRY('POLYGON((1 1,5 1,5 5,1 5,1 1))'),1000,0.01)) from dual;
--点在面内
select SDO_GEOM.RELATE(SDO_GEOMETRY('POLYGON((1 1,5 1,5 5,1 5,1 1))'),'CONTAINS',SDO_GEOMETRY('POINT(12 2)'), 0.01) from dual;
--对象相交
select SDO_GEOM.RELATE(SDO_GEOMETRY('POLYGON((0 0,0 1,1 1,1 0,0 0))'),'ANYINTERACT',SDO_GEOMETRY('POLYGON((0.5 0.5,0.5 1.5,1.5 1.5,1.5 0.5,0.5 0.5))'), 0.01) from dual;