Oracle Spatial is composed of a lump of object data types, type methods, operators, functions and processes. A geographic object is stored as a Sdo_geometry object in a field in the table. Spatial indexes are established and maintained by Common DDL and DML statements.
This chapter focuses on some examples of how to create, query, and index spatial data.
Simple Insert, index and query spatial data examples
This section demonstrates a very simple process of creating spatial tables, inserting, indexing, and querying data.
The scene is a soft-drink company that uses geographic information to represent the situation of their products (COLA) in various regions. These can be: market share, competitive pressure, growth potential and so on. The region can be a neighboring city, state, province, or country.
What we are going to do is:
1. Create a table (Cola_markets) to save spatial data
2. Insert data from four (cola_a, Cola_b, Cola_c, Cola_d) regions
3. Upgrade the User_sdo_geom_metadata view to the dimension information for these areas anyway
4. Building a spatial index (COLA_SPATIAL_IDX)
5. Make some spatial queries
[SQL]View PlainCopy
- CREATE TABLE cola_markets (
- mkt_id number PRIMARY KEY,
- name VARCHAR2 (+),
- Shape sdo_geometry);
[SQL]View PlainCopy
- INSERT into cola_markets VALUES (
- 1,
- ' cola_a ',
- Sdo_geometry (
- 2003, --two-dimensional polygon
- NULL,
- NULL,
- Sdo_elem_info_array (1,1003,3), --one rectangle (1003 = exterior)
- Sdo_ordinate_array (5,7) --only 2 points needed to
- --Define rectangle (lower left and upper right) with
- --Cartesian-coordinate data
- )
- );
- --The next and the INSERT statements create areas of interest for
- --Cola B and Cola C. These areas is simple polygons (and not
- --rectangles).
- INSERT into cola_markets VALUES (
- 2,
- ' Cola_b ',
- Sdo_geometry (
- 2003, --two-dimensional polygon
- NULL,
- NULL,
- Sdo_elem_info_array (1,1003,1), --one polygon (exterior polygon ring)
- Sdo_ordinate_array (5,1, 8, 1, 8, 6, 5, 7, 5,1)
- )
- );
- INSERT into cola_markets VALUES (
- 3,
- ' Cola_c ',
- Sdo_geometry (
- 2003, --two-dimensional polygon
- NULL,
- NULL,
- Sdo_elem_info_array (1,1003,1), --one polygon (exterior polygon ring)
- Sdo_ordinate_array (3,3, 6, 3, 6, 5, 4, 5, 3,3)
- )
- );
- --now insert a area of interest for Cola D. This is a
- --circle with a radius of 2. It is completely outside the
- --first three areas of interest.
- INSERT into cola_markets VALUES (
- 4,
- ' Cola_d ',
- Sdo_geometry (
- 2003, --two-dimensional polygon
- NULL,
- NULL,
- Sdo_elem_info_array (1,1003,4), - -One circle
- Sdo_ordinate_array (8,7, 10, 9, 8,11)
- )
- );
You need to write information about the spatial layer of the established table into a user_sdo_geom_metadata
[SQL]View PlainCopy
- INSERT into user_sdo_geom_metadata
- (TABLE_NAME,
- COLUMN_NAME,
- Diminfo,
- SRID)
- VALUES (
- ' Cola_markets ',
- ' shape ',
- Sdo_dim_array ( --20X20 grid
- Sdo_dim_element (' X ', 0, 0.005),
- Sdo_dim_element (' Y ', 0, 0.005)
- ),
- NULL --SRID
- );
[SQL]View PlainCopy
- --SRID
User_sdo_geom_metadata after landing PL/SQL, users→mdsys→objects→views→user_sdo_geom_metadata
Open to view (I have inserted 2 records of the spatial table)
You can see this inserted record, click on any one of the red squares, and it will appear:
[SQL]View PlainCopy
- Sdo_dim_array ( --20X20 grid
- Sdo_dim_element (' X ', 0, 0.005),
- Sdo_dim_element (' Y ', 0, 0.005)
- ),
This is the meaning of SQL, where Sdo_dimname is a two-dimensional maximum and minimum value of the description, SDO_LB is the minimum value above x, Sdo_ub is the maximum value above X, sdo_tolerance refers to the size of the error, This means that data in 0.005 will default to one data.
The fourth step is to create a spatial index (COLA_SPATIAL_IDX)
[SQL]View PlainCopy
- CREATE INDEX Cola_spatial_idx
- On cola_markets (shape)
- Indextype is Mdsys. Spatial_index;
The purpose of indexing is to increase the query speed (in theory, you can not set up the index to query, but the actual basic cannot query out)
When the index is established, the following table is generated in tables:
This is an index table:
Can be found in the system's Index table.
The above is created to complete the Sdo_geometry table, you can start the spatial analysis.
Oracle Create Sdo_geometry Table