# oracle spatial之基础知识之四空间索引

1 建立索引之前为空间层插入元数据

名称                                      是否为空? 类型

----------------------------------------- -------- ----------------------------

TABLE_NAME                                 NOT NULL VARCHAR2(32)

COLUMN_NAME                               NOT NULL VARCHAR2(1024)

DIMINFO                                      MDSYS.SDO_DIM_ARRAY

SRID                                               NUMBER

SQL> select * from user_sdo_geom_metadata;

TABLE_NAME                COLUMN_NAME                    DIMINFO       SRID

----------                -----------------------------  -------        ----------

CUSTOMERS                        LOCATION                       8307

US_RESTAURANTS                   LOCATION                       8307

US_INTERSTATES                   GEOM                           8307

(table_name, column_name, srid, diminfo)

VALUES

(

'CUSTOMERS', -- TABLE_NAME

'LOCATION', -- COLUMN_NAME

8307, -- SRID specifying a geodetic coordinate system

SDO_DIM_ARRAY -- DIMINFO attribute for storing dimension bounds, tolerance

(

SDO_DIM_ELEMENT

(

'LONGITUDE', -- DIMENSION NAME for first dimension

-180, -- SDO_LB for the dimension: -180 degrees

180, -- SDO_UB for the dimension: 180 degrees

0.5 -- Tolerance of 0.5 meters (not 0.5 degrees: geodetic SRID)

),

SDO_DIM_ELEMENT

(

'LATITUDE', -- DIMENSION NAME for second dimension

-90, -- SDO_LB for the dimension: -90 degrees

90, -- SDO_UB for the dimension: 90 degrees

0.5 -- Tolerance of 0.5 meters (not 0.5 degrees: geodetic SRID)

)

)

);

DIMINFO域为每个维定义了 边界和容差(TOLERANE).它的值被设置为一个含有两个元素的SDO_DIM_ARRAY对象.

2 创建空间索引

首先删除索引:

DROP INDEX CUSTOMERS_SIDX;

其次创建索引:

CREATE INDEX CUSTOMERS_SIDX ON CUSTOMERS(LOCATION) INDEXTYPE IS MDSYS.SPATIAL_INDEX;

3 空间索引的参数信息

CREATE INDEX ON  ()

INDEXTYPE IS MDSYS.SPATIAL_INDEX

PARAMETERS ('parameter_string');

l       TABLSPACE 参数

CREATE INDEX customers_sidx ON customers(location)

INDEXTYPE IS MDSYS.SPATIAL_INDEX

PARAMETERS ('TABLESPACE=TBS_3');

l       WORK_TABLSPACE 参数

CREATE INDEX customers_sidx ON customers(location)

INDEXTYPE IS MDSYS.SPATIAL_INDEX

PARAMETERS ('WORK_TABLESPACE= TBS_4');

l       SDO_DML_BATCH_SIZE 参数

CREATE INDEX customers_sidx ON customers(location)

INDEXTYPE IS MDSYS.SPATIAL_INDEX

PARAMETERS ('SDO_DML_BATCH_SIZE=5000');

名称                                      是否为空? 类型

----------------------------------------- -------- -------------------

SDO_INDEX_OWNER                                    VARCHAR2(32)

SDO_INDEX_TYPE                                     VARCHAR2(32)

SDO_LEVEL                                          NUMBER

SDO_NUMTILES                                       NUMBER

SDO_MAXLEVEL                                       NUMBER

SDO_COMMIT_INTERVAL                                NUMBER

SDO_INDEX_TABLE                                    VARCHAR2(32)

SDO_INDEX_NAME                                     VARCHAR2(32)

SDO_INDEX_PRIMARY                                  NUMBER

SDO_TSNAME                                         VARCHAR2(32)

SDO_COLUMN_NAME                                  VARCHAR2(2048)

SDO_RTREE_HEIGHT                                   NUMBER

SDO_RTREE_NUM_NODES                                NUMBER

SDO_RTREE_DIMENSIONALITY                           NUMBER

SDO_RTREE_FANOUT                                   NUMBER

SDO_RTREE_ROOT                                     VARCHAR2(32)

SDO_RTREE_SEQ_NAME                                 VARCHAR2(32)

SDO_FIXED_META                                     RAW(255)

SDO_TABLESPACE                                     VARCHAR2(32)

SDO_INITIAL_EXTENT                                 VARCHAR2(32)

SDO_NEXT_EXTENT                                    VARCHAR2(32)

SDO_PCTINCREASE                                    NUMBER

SDO_MIN_EXTENTS                                    NUMBER

SDO_MAX_EXTENTS                                    NUMBER

SDO_INDEX_DIMS                                     NUMBER

SDO_LAYER_GTYPE                                    VARCHAR2(32)

SDO_RTREE_PCTFREE                                  NUMBER

SDO_INDEX_PARTITION                                VARCHAR2(32)

SDO_PARTITIONED                                    NUMBER

SDO_RTREE_QUALITY                                  NUMBER

SDO_INDEX_VERSION                                  NUMBER

SDO_INDEX_GEODETIC                                 VARCHAR2(8)

SDO_INDEX_STATUS                                   VARCHAR2(32)

SDO_NL_INDEX_TABLE                                 VARCHAR2(33)

SDO_DML_BATCH_SIZE                                 NUMBER

SDO_RTREE_ENT_XPND                                 NUMBER

SDO_ROOT_MBR                                       MDSYS.SDO_GEOMETRY

SQL> select sdo_tablespace from user_sdo_index_metadata;

5 空间索引大小需求确定

SELECT sdo_tune.estimate_rtree_index_size

(

'SPATIAL', -- schema name

'CUSTOMERS', -- table name

'LOCATION' -- column name on which the spatial index is to be built

) sz

FROM dual;

6 向表中添加位置信息

第一:创建普通表

CREATE TABLE customers

(

id NUMBER,

datasrc_id NUMBER,

name VARCHAR2(35),

category VARCHAR2(30),

street_number VARCHAR2(5),

street_name VARCHAR2(60),

city VARCHAR2(32),

postal_code VARCHAR2(16),

state VARCHAR2(32),

phone_number VARCHAR2(15),

);

INSERT INTO customers VALUES

(

1, -- id

1, -- datasrc_id

'Pizza Hut' , -- name

'Restaurant', -- restaurant

'134', -- street_number

'12TH STREET', -- street_name

'WASHINGTON', -- city

'20003', -- postal_code

'DC', -- state

NULL, -- phone_number

);

SQL> alter table customers add (location sdo_geometry);

SQL> desc customers;

名称                                      是否为空?  类型

-----------------------------------------               --------      ---------------------

ID                                                   NUMBER

DATASRC_ID                                         NUMBER

NAME                                               VARCHAR2(35)

CATEGORY                                           VARCHAR2(30)

STREET_NUMBER                                     VARCHAR2(5)

STREET_NAME                                        VARCHAR2(60)

CITY                                                  VARCHAR2(32)

POSTAL_CODE                                         VARCHAR2(16)

STATE                                                 VARCHAR2(32)

PHONE_NUMBER                                       VARCHAR2(15)

LOCATION                                              PUBLIC.SDO_GEOMETRY

INSERT INTO customers

(

ID,

DATASRC_ID,

NAME,

CATEGORY,

STREET_NUMBER,

STREET_NAME,

CITY,

POSTAL_CODE,

STATE,

PHONE_NUMBER,

)

VALUES

(

1, -- id

1, -- datasrc_id

'Pizza Hut' , -- name

'Restaurant', -- restaurant

'134', -- street_number

'12TH STREET', -- street_name

'WASHINGTON', -- city

'20003', -- postal_code

'DC', -- state

NULL, -- phone_number

);

SQL> select street_number,street_name,city,state,postal_code from customers where id=1;

STREET_NUMBER     STREET_NAME      CITY                             STATE                            POSTAL_CODE

-------------     --------------   ------------------               -----------------                ------------

134           12TH STREET          WASHINGTON                       DC                               20003

UPDATE customers

SET location =

SDO_GCDR.GEOCODE_AS_GEOMETRY

(

'SPATIAL',

SDO_KEYWORDARRAY

(

street_number || '' || street_name, -- add whitespace between street_number and street_name

city || ',' || state || ' ' || postal_code

),

'US'

) ;

ORACLE SPATIAL 使你能够转换地址(street_number, street_name, citypostal_code)为一个在地球表面上的二维点位置

SDO_GCDR.GEOCODE_AS_GEOMETRY 这个函数分别采用模式名称和地理编码数据集名称作为第一个和最后一个参数

SQL> SELECT location;

2  FROM customers

3  WHERE id=1;

LOCATION(SDO_GTYPE, SDO_SRID, SDO_POINT(X, Y, Z), SDO_ELEM_INFO, SDO_ORDINATES)

--------------------------------------------------------------------------------

SDO_GEOMETRY(2001, 8307, SDO_POINT_TYPE(-77.01324, 38.8936, NULL), NULL, NULL)

UPDATE customers

SET location =

SDO_GEOMETRY

(

2001, -- Specify that location is a point

8307, -- Specify coordinate system id

SDO_POINT_TYPE(-77.06, 38.94, NULL), -- Specify coordinates here

NULL,

NULL

)

WHERE id=1;

INSERT INTO USER_SDO_GEOM_METADATA VALUES

(

'CUSTOMERS', -- TABLE_NAME

'LOCATION', -- COLUMN_NAME

SDO_DIM_ARRAY -- DIMINFO attribute for storing dimension bounds, tolerance

(

SDO_DIM_ELEMENT

(

'LONGITUDE', -- DIMENSION NAME for first dimension

-120, -- SDO_LB for the dimension

120, -- SDO_UB for the dimension

0.5 -- Tolerance of 0.5 meters

),

SDO_DIM_ELEMENT

(

'LATITUDE', -- DIMENSION NAME for second dimension

-70, -- SDO_LB for the dimension

70, -- SDO_UB for the dimension

0.5 -- Tolerance of 0.5 meters

)

),

8307 -- SRID value for specifying a geodetic coordinate system

);

SQL> DESC SDO_GEOMETRY;

名称                                      是否为空? 类型

----------------------------------------- -------- -------------------------

SDO_GTYPE                                          NUMBER

SDO_SRID                                            NUMBER

SDO_POINT                                           MDSYS.SDO_POINT_TYPE

SDO_ELEM_INFO                                      MDSYS.SDO_ELEM_INFO_ARRAY

SDO_ORDINATES                                      MDSYS.SDO_ORDINATE_ARRAY

SDO_POINT仅能够存储 三个坐标（X,YZ.这就是说 在数据是三维或低于三维 的时候才适合。对于饲喂的点，只能使用SDO_ELEM_INFOSDO_ORDINATES属性。

l         查询CUSTOMER表中LOCATION列的SDO_GTYPE

SQL> select ct.location.sdo_gtype from customers ct ;

LOCATION.SDO_GTYPE

------------------

2001

l         SDO_SRID这个属性为几何体规定了空间参考系或是坐标系，选择一个合适的坐标系需要看以下的表

SQL> desc mdsys.cs_srs;

名称                                      是否为空? 类型

----------------------------------------- -------- ---------------------

CS_NAME                                            VARCHAR2(80)

SRID                                      NOT NULL NUMBER(38)

AUTH_SRID                                          NUMBER(38)

AUTH_NAME                                          VARCHAR2(256)

WKTEXT                                             VARCHAR2(2046)

CS_BOUNDS                                          MDSYS.SDO_GEOMETRY

WKTEXT3D                                           VARCHAR2(4000)

l         SDO_POINT这个属性定义了点的坐标，例如客户的位置。这个属性的类型是另一种对象类型SDO_POINT_TYPE.

SQL> DESC SDO_POINT_TYPE;

名称                                      是否为空? 类型

----------------------------------------- -------- ---------

X                                                  NUMBER

Y                                                  NUMBER

Z                                                  NUMBER

2012/2/16

n       构造简单二维几何体的案例来插入数据（充分使用SDO_ELEM_INFOSDO_ORDINATES属性）

CREATE TABLE geometry_examples

(

name VARCHAR2(100),

description VARCHAR2(100),

geom SDO_GEOMETRY

);

SQL> desc geometry_examples;

名称                                      是否为空? 类型

----------------------------------------- -------- --------------------

NAME                                               VARCHAR2(100)

DESCRIPTION                                        VARCHAR2(100)

GEOM                                               PUBLIC.SDO_GEOMETRY

INSERT INTO geometry_examples (name, description, geom) VALUES

(

'POINT',

'2-dimensional Point at coordinates (-79,37) with srid set to 8307',

SDO_GEOMETRY

(

2001, -- SDO_GTYPE format: D00T. Set to 2001 for a 2-dimensional point

8307, -- SDO_SRID (geodetic)

SDO_POINT_TYPE

(

-79, -- ordinate value for Longitude

37, -- ordinate value Latitude

NULL -- no third dimension (only 2 dimensions)

),

NULL,

NULL

)

);

ORACLE SPATIAL要求经度坐标放在第一位，维度坐标放在第二维。

SELECT SDO_GEOMETRY(' POINT(-79 37) ', 8307) geom FROM DUAL;

GEOM(SDO_GTYPE, SDO_SRID, SDO_POINT(X, Y, Z), SDO_ELEM_INFO, SDO_ORDINATES)

----------------------------------------------------------------------------

SDO_GEOMETRY(2001, 8307, SDO_POINT_TYPE(-79, 37, NULL), NULL, NULL)

INSERT INTO geometry_examples VALUES

(

'2-D POINT stored in SDO_ORDINATES',

'2-dimensional Point at coordinates (-79, 37) with srid set to 8307',

SDO_GEOMETRY

(

2001, -- SDO_GTYPE format: D00T. Set to 2001 for as a 2-dimensional point

8307, -- SDO_SRID

NULL, -- SDO_POINT attribute set to NULL

SDO_ELEM_INFO_ARRAY -- SDO_ELEM_INFO attribute (see Table 4-2 for values)

(

1, -- Offset is 1

1, -- Element-type is 1 for a point

1 -- Interpretation specifies # of points. In this case 1.

),

SDO_ORDINATE_ARRAY -- SDO_ORDINATES attribute

(

-79, -- Ordinate value for Longitude

37 -- Ordinate value for Latitude

)

)

);

Offset：永远设置为1，因为在SDO_ORDINATES中只有一个元素

Element-type：与几何体的SDO_GTYPE类型中的T值直接对应

Interpretation：表示一个元素更细微的信息

SDO_ELEM_INFO属性是SDO_ELEM_INFO_ARRAY类型的，这个类型同样是一个数字型VARRAY，最大容量是1048576个数字。

SDO_ORDINATES属性是SDO_ORDINATE_ARRAY类型的，这个想是一个数字型VARRAY(可变长度数组)

INSERT INTO geometry_examples VALUES

(

'4-D POINT',

'4-dimensional Point at (Xa=>2, Ya=>2, Za=>2, La=>2) with srid set to NULL',

SDO_GEOMETRY

(

4001, -- SDO_GTYPE: D00T. Set to 4001 as it is a 4-dimensional point

NULL, -- SDO_SRID

NULL, -- SDO_POINT_TYPE is null

SDO_ELEM_INFO_ARRAY(1,1,1), -- Indicates a point element

SDO_ORDINATE_ARRAY(2,2,2,2) -- Store the four ordinates here

)

);

n       通过直线连接的线串（充分使用SDO_ELEM_INFOSDO_ORDINATES属性）

INSERT INTO geometry_examples VALUES

(

'LINE STRING',

'2-D line string connecting A(Xa=>1,Ya=>1),B(Xb=>2, Yb=>2), C(Xc=>2,Yc=>1)',

SDO_GEOMETRY

(

2002, -- SDO_GTYPE: D00T. Set to 2002 as it is a 2-dimensional line string

32774, -- SDO_SRID

NULL, -- SDO_POINT_TYPE is null

SDO_ELEM_INFO_ARRAY -- SDO_ELEM_INFO attribute (see Table 4-2 for values)

(

1, -- Offset is 1

2, -- Element-type is 2 for a LINE STRING

1 -- Interpretation is 1 if line string is connected by straight lines.

),

SDO_ORDINATE_ARRAY -- SDO_ORDINATES attribute

(

1,1, -- Xa, Ya values

2,2, -- Xb, Yb values

2,1 -- Xc, Yc values

)

)

);

n       通过弧线连接的线串（充分使用SDO_ELEM_INFOSDO_ORDINATES属性）

INSERT INTO geometry_examples VALUES

(

'ARCSTRING',

'2-D arc connecting A(Xa=>1,Ya=>1),B(Xb=>2, Yb=>2), C(Xc=>2,Yc=>1)',

SDO_GEOMETRY

(

2002, -- SDO_GTYPE: D00T. Set to 2002 as it is a 2-dimensional line string

32774, -- SDO_SRID

NULL, -- SDO_POINT_TYPE is null

SDO_ELEM_INFO_ARRAY -- SDO_ELEM_INFO attribute (see Table 4-2 for values)

(

1, -- Offset is 1

2, -- Element-type is 2 for a LINE STRING

2 -- Interpretation is 2 if line string is connected by ARCs.

),

SDO_ORDINATE_ARRAY -- SDO_ORDINATES attribute

(

1,1, -- Xa, Ya values

2,2, -- Xb, Yb values

2,1 -- Xc, Yc values

)

)

);

n       多边形，边界通过直线连接的环（充分使用SDO_ELEM_INFOSDO_ORDINATES属性）

INSERT INTO geometry_examples VALUES

(

'POLYGON',

'2-D polygon connecting A(Xa, Ya), B(Xb, Yb), C(Xc, Yc), D(Xd, Yd)',

SDO_GEOMETRY

(

2003,   -- SDO_GTYPE: D00T. Set to 2003 as it is a 2-dimensional polygon

32774,  -- SDO_SRID

NULL,   -- SDO_POINT_TYPE is null

SDO_ELEM_INFO_ARRAY -- SDO_ELEM_INFO attribute (see Table 4-2 for values)

(

1,      -- Offset is 1

1003,   -- Element-type is 1003 for an outer POLYGON element

1       -- Interpretation is 1 if boundary is connected by straight lines.

),

SDO_ORDINATE_ARRAY -- SDO_ORDINATES attribute

(

1,1,    -- Xa, Ya values

2,-1,   -- Xb, Yb values

3,1,    -- Xc, Yc values

2,2,    -- Xd, Yd values

1,1     -- Xa, Ya values : Repeat first vertex to close the ring

)

)

);

n       圆（充分使用SDO_ELEM_INFOSDO_ORDINATES属性）

INSERT INTO geometry_examples VALUES

(

'CIRCLE POLYGON',

'2-D circle polygon with 3 boundary points A(Xa,Ya), B(Xb,Yb), C(Xc,Yc)',

SDO_GEOMETRY

(

2003,     -- SDO_GTYPE: D00T. Set to 2003 as it is a 2-dimensional polygon

32774,    -- SDO_SRID

NULL,     -- SDO_POINT_TYPE is null

SDO_ELEM_INFO_ARRAY -- SDO_ELEM_INFO attribute (see Table 4-2 for values)

(

1,      -- Offset is 1

1003,   -- Element-type is 1003 for (an outer) POLYGON

4       -- Interpretation is 4 if polygon is a CIRCLE

),

SDO_ORDINATE_ARRAY -- SDO_ORDINATES attribute

(

1,1,    -- Xa, Ya values

3,1,    -- Xb, Yb values

2,2     -- Xc, Yc values

)

)

);

SQL> select ge.geom.sdo_elem_info from  geometry_examples ge;

GEOM.SDO_ELEM_INFO

--------------------------------------------------------------------------------

SDO_ELEM_INFO_ARRAY(1, 1, 1)

SDO_ELEM_INFO_ARRAY(1, 1, 1)

SDO_ELEM_INFO_ARRAY(1, 2, 1)

SDO_ELEM_INFO_ARRAY(1, 2, 2)

SDO_ELEM_INFO_ARRAY(1, 1003, 1)

SDO_ELEM_INFO_ARRAY(1, 1003, 4)

SQL> select ge.geom.sdo_ordinates from  geometry_examples ge;

GEOM.SDO_ORDINATES

--------------------------------------------------------------------------------

SDO_ORDINATE_ARRAY(-79, 37)

SDO_ORDINATE_ARRAY(2, 2, 2, 2)

SDO_ORDINATE_ARRAY(1, 1, 2, 2, 2, 1)

SDO_ORDINATE_ARRAY(1, 1, 2, 2, 2, 1)

SDO_ORDINATE_ARRAY(1, 1, 2, -1, 3, 1, 2, 2, 1, 1)

SDO_ORDINATE_ARRAY(1, 1, 3, 1, 2, 2)

SQL> select ge.geom.sdo_point from  geometry_examples ge;

GEOM.SDO_POINT(X, Y, Z)

--------------------------------------------------------------------------------

SDO_POINT_TYPE(-79, 37, NULL)

SQL> select ge.geom.sdo_srid from  geometry_examples ge;

GEOM.SDO_SRID

-------------

8307

8307

32774

32774

32774

32774

SQL> select ge.geom.sdo_gtype from  geometry_examples ge;

GEOM.SDO_GTYPE

--------------

2001

2001

4001

2002

2002

2003

2003

Getnumelem

Getnumverticces

Getvertices

SELECT SDO_UTIL.GETNUMELEM(geom) nelem

FROM sales_regions

WHERE id=10000;

SELECT SDO_UTIL.GETNUMVERTICES(geom) nverts

FROM sales_regions

WHERE id=10000;

• 博文量
100
• 访问量
194801