sunfruit[請?jiān)L問http://www.fruitres.cn]

          --我相信JAVA能走得更遠(yuǎn) QQ:316228067

          Oracle Spatial詳解

          Step1. 創(chuàng)建一張表,其中shape用來存放空間數(shù)據(jù)
          CREATE TABLE mylake (
          ??? feature_id NUMBER PRIMARY KEY,
          ??? name VARCHAR2(32),
          ??? shape MDSYS.SDO_GEOMETRY);

          Step2. 在user_sdo_geom_metadata 表中插入新記錄,用于描述空間字段
          INSERT INTO user_sdo_geom_metadata VALUES (
          ??? 'mylake',????//---表名
          ??? 'shape',????//---字段名
          ??? MDSYS.SDO_DIM_ARRAY(???
          ??????? MDSYS.SDO_DIM_ELEMENT('X', 0, 100, 0.05),????//---X維最小,最大值和容忍度。
          ??????? MDSYS.SDO_DIM_ELEMENT('Y', 0, 100, 0.05)????//---Y維最小,最大值和容忍度
          ??? ),
          ??? NULL????//---坐標(biāo)系,缺省為笛卡爾坐標(biāo)系
          );

          Step3. 創(chuàng)建空間索引
          CREATE INDEX mylake_idx ON mylake(shape)
          ??? INDEXTYPE IS MDSYS.SPATIAL_INDEX

          Step4. 插入空間數(shù)據(jù)
          Oracle Spatial用MDSYS.SDO_GEOMETRY來存儲空間數(shù)據(jù),定義為:CREATE TYPE sdo_geometry AS OBJECT (
          ??? SDO_GTYPE NUMBER,
          ??? SDO_SRID NUMBER,
          ??? SDO_POINT SDO_POINT_TYPE,
          ??? SDO_ELEM_INFO MDSYS.SDO_ELEM_INFO_ARRAY,
          ??? SDO_ORDINATES MDSYS.SDO_ORDINATE_ARRAY);
          SDO_GTYPE:用四個數(shù)字定義了所有的形狀
          ??????????? 第一位:維數(shù)
          ??????????? 第二位:線性表示。用于3,4維數(shù)據(jù),二維為0
          ??????????? 最后兩位:
          ValueGeometryDescription
          00UNKNOWN_GEOMETRYSpatial ignores this value
          01POINTA single point element
          02LINE or CURVEContains one line string element that may be linear, curved or both
          03POLYGONContains one polygon element with or without other polygon elements in it
          04COLLECTIONA heterogeneous collection of elements
          05MULTIPOINTContains one or more points
          06MULTILINE or MULTICURVEContains one or more line string elements
          07MULTIPOLYGONContains multiple polygon elements that maybe disjoint

          SDO_SRID:坐標(biāo)系,NULL為笛卡爾坐標(biāo)系。
          SDO_POINT:
          Oracle Spatial也可定義單個的點(diǎn),SDO_POINT的定義:
          ??? CREATE TYPE sdo_point_type AS OBJECT (X NUMBER,Y NUMBER,Z NUMBER);
          ??? 如何是二維,Z為NULL。
          SDO_ELEM_INFO:
          每三個值描述一個元素。
          ????????????????第一個值:第一個頂點(diǎn)在SDO_ORDINATES_ARR開始位置
          ????????????????第二個值:元素類型
          ????????????????第三個值:頂點(diǎn)連接方式:1-通過直線連接,2-通過圓弧連接

          ??? 定義為
          ??? CREATE TYPE sdo_elem_info_array AS VARRAY (1048576) of NUMBER;
          SDO_ORDINATES:幾何圖形所有頂點(diǎn)列表。定義為
          ??? CREATE TYPE sdo_ordinate_array AS VARRAY (1048576) of NUMBER;

          FONT color=#003366>// 插入包含一個島嶼的湖泊
          INSERT INTO mylake VALUES(
          ??? 10,?
          ??? 'Lake Calhoun',?
          ??? MDSYS.SDO_GEOMETRY(
          ??????? 2003,
          ??????? NULL,
          ??????? NULL,
          ??????? MDSYS.SDO_ELEM_INFO_ARRAY(1,1003,1, 19,2003,1),
          ??????? MDSYS.SDO_ORDINATE_ARRAY(0,0, 10,0, 10,10, 0,10, 0,0, 4,4, 6,4, 6,6, 4,6, 4,4)
          ??? ));

          // 插入兩艘小船
          INSERT INTO mylake VALUES(
          ??? 11,?
          ??? 'The Windswept',?
          ??? MDSYS.SDO_GEOMETRY(
          ??????? 2003,
          ??????? NULL,
          ??????? NULL,
          ??????? MDSYS.SDO_ELEM_INFO_ARRAY(1,1003,1),
          ??????? MDSYS.SDO_ORDINATE_ARRAY(2,2, 3,2, 3,2, 2,3, 2,2)
          ??? )
          );

          INSERT INTO mylake VALUES(
          ??? 12,?
          ??? 'Blue Crest',?
          ??? MDSYS.SDO_GEOMETRY(
          ??????? 2003,
          ??????? NULL,
          ??????? NULL,
          ??????? MDSYS.SDO_ELEM_INFO_ARRAY(1,1003,1),
          ??????? MDSYS.SDO_ORDINATE_ARRAY(7,7, 8,7, 8,7, 7,8, 7,7)
          ??? )
          );

          Step4. 查詢
          Oracle Spatial查詢數(shù)據(jù)包括二個處理過程:
          1.只通過索引查詢候選項(xiàng)。通過函數(shù)SDO_FILTER實(shí)現(xiàn):
          SDO_FILTER(geometry1 MDSYS.SDO_GEOMETRY,?geometry2 MDSYS.SDO_GEOMETRY, params VARCHAR2)
          geometry1:
          必須是被索引的幾何數(shù)據(jù)
          geometry2:不一定是表中的空間字段,也不要求被索引
          params:Filter類型
          ??????? querytype=WINDOW:geometry2不要求來自表
          ??????? querytype=JOIN:geometry2必須來自表

          SELECT name boat_name
          FROM mylake t
          WHERE feature_id = 12
          AND SDO_FILTER(t.shape, mdsys.sdo_geometry(2003,NULL,NULL,
          ??? mdsys.sdo_elem_info_array(1,1003,1),
          ??? mdsys.sdo_ordinate_array(2,2, 5,2, 5,5, 2,5, 2,2)),
          ??? 'querytype=WINDOW') = 'TRUE';


          2.再檢查每個候選項(xiàng)是否和條件精確匹配。通過函數(shù)SDO_RELATE實(shí)現(xiàn):
          SDO_RELATE(geometry1 MDSYS.SDO_GEOMETRY, geometry2 MDSYS.SDO_GEOMETRY, params VARCHAR2)
          params:
          masktype類型

        1. DISJOINT — the boundaries and interiors do not intersect
        2. TOUCH — the boundaries intersect but the interiors do not intersect
        3. OVERLAPBDYDISJOINT — the interior of one object intersects the boundary and interior of the other object, but the two boundaries do not intersect. This relationship occurs, for example, when a line originates outside a polygon and ends inside that polygon.
        4. OVERLAPBDYINTERSECT — the boundaries and interiors of the two objects intersect
        5. EQUAL — the two objects have the same boundary and interior
        6. CONTAINS — the interior and boundary of one object is completely contained in the interior of the other object
        7. COVERS — the interior of one object is completely contained in the interior of the other object and their boundaries intersect
        8. INSIDE — the opposite of CONTAINS. A INSIDE B implies B CONTAINS A.
        9. COVEREDBY — the opposite of COVERS. A COVEREDBY B implies B COVERS A.
        10. ON — the interior and boundary of one object is on the boundary of the other object (and the second object covers the first object). This relationship occurs, for example, when a line is on the boundary of a polygon.
        11. ANYINTERACT — the objects are non-disjoint.

          // 選擇在定義矩形內(nèi)的所有小船
          SELECT name boat_name
          FROM mylake t
          WHERE feature_id = 12
          AND SDO_FILTER(t.shape, mdsys.sdo_geometry(2003,NULL,NULL,
          ??? mdsys.sdo_elem_info_array(1,1003,1),
          ??? mdsys.sdo_ordinate_array(2,2, 5,2, 5,5, 2,5, 2,2)),
          ??? 'querytype=WINDOW') = 'TRUE'
          AND SDO_RELATE(t.shape, mdsys.sdo_geometry(2003,NULL,NULL,
          ??? mdsys.sdo_elem_info_array(1,1003,1),
          ??? mdsys.sdo_ordinate_array(2,2, 5,2, 5,5, 2,5, 2,2)),
          ??? 'masktype=INSIDE querytype=WINDOW') = 'TRUE'

          // masktype可聯(lián)合使用
          SELECT feature_id id
          FROM mylake t
          WHERE feature_id = 12
          AND SDO_FILTER(t.shape, mdsys.sdo_geometry(2003,NULL,NULL,
          ??? mdsys.sdo_elem_info_array(1,1003,1),
          ??? mdsys.sdo_ordinate_array(2,2, 5,2, 5,5, 2,5, 2,2)),
          ??? 'querytype=WINDOW') = 'TRUE'
          AND SDO_RELATE(t.shape, mdsys.sdo_geometry(2003,NULL,NULL,
          ??? mdsys.sdo_elem_info_array(1,1003,1),
          ??? mdsys.sdo_ordinate_array(2,2, 5,2, 5,5, 2,5, 2,2)),
          ??? 'masktype=INSIDE+TOUCH querytype=WINDOW') = 'TRUE'

          Oracle Spatial 提供的其他查詢函數(shù):
          QueryDescription
          SDO_NNNearest neighbor
          SDO_SDO_WITHIN_DISTANCEAll geometries with a certain distance
          FunctionsDescription
          SDO_GEOM.SDO_MBRThe minimum bounding rectangle for a geometry
          SDO_GEOM.SDO_DISTANCEThe distance between two geometries
          SDO_GEOM.SDO_INTERSECTIONProvides the intersection point of two geometries

        12. posted on 2006-07-19 15:21 sunfruit 閱讀(1503) 評論(1)  編輯  收藏 所屬分類: 數(shù)據(jù)庫

          評論

          # re: Oracle Spatial詳解 2008-12-24 13:32 czh

          SDO_RELATE 其中的有個參數(shù)'masktype==INSIDE+TOUCH '應(yīng)該修改為'mask==INSIDE+TOUCH ' ,否則總是報(bào)錯。 我在oracle 9i下遇到這個問題,其他平臺沒有試過。  回復(fù)  更多評論   

          主站蜘蛛池模板: 呼玛县| 洪洞县| 忻城县| 西安市| 洪湖市| 安图县| 广州市| 兴海县| 揭东县| 隆化县| 呼玛县| 巴彦县| 荥阳市| 浦北县| 临潭县| 隆化县| 屏东县| 耿马| 正蓝旗| 塘沽区| 蒲城县| 西安市| 玉山县| 平陆县| 元朗区| 贞丰县| 海宁市| 深州市| 绥宁县| 弋阳县| 华池县| 高青县| 巴青县| 牙克石市| 新宁县| 梁平县| 大渡口区| 福安市| 南昌市| 乐安县| 漯河市|