1.26 表の空間的な有効化
SDO_GEOMETRY列がないが、ポイントの緯度/経度値などの位置に関連する情報を含んでいるOracle表を使用する場合、SDO_GEOMETRY列を追加し、レコードの既存(および現在以降)の位置に関連する情報を使用してSDO_GEOMETRY列の値を移入することで、表を空間的に有効化することができます。
次は、通常の表を空間的に有効化する基本ステップです。これらは、通常の表に、表の各レコードに関連付けられた位置に関連する値を含む列があることを前提としています。
例1-6 表の空間的な有効化
-- Original table without a spatial geometry column.
CREATE TABLE city_points (
city_id NUMBER PRIMARY KEY,
city_name VARCHAR2(25),
latitude NUMBER,
longitude NUMBER);
-- Original data for the table.
-- (The sample coordinates are for a random point in or near the city.)
INSERT INTO city_points (city_id, city_name, latitude, longitude)
VALUES (1, 'Boston', 42.207905, -71.015625);
INSERT INTO city_points (city_id, city_name, latitude, longitude)
VALUES (2, 'Raleigh', 35.634679, -78.618164);
INSERT INTO city_points (city_id, city_name, latitude, longitude)
VALUES (3, 'San Francisco', 37.661791, -122.453613);
INSERT INTO city_points (city_id, city_name, latitude, longitude)
VALUES (4, 'Memphis', 35.097140, -90.065918);
-- Add a spatial geometry column.
ALTER TABLE city_points ADD (shape SDO_GEOMETRY);
-- Update the table to populate geometry objects using existing
-- latutide and longitude coordinates.
UPDATE city_points SET shape =
SDO_GEOMETRY(LONGITUDE, LATITUDE);
-- Optional Step: Update the USER_SDO_GEOM_METADATA view.
–- By default, Oracle Spatial will automatically create the metadata in
-- the USER_SDO_GEOM_METADATA view using a default tolerance value of 0.05.
–- Run this step only if you prefer a different tolerance value.
–- The following example uses a tolerance value of 0.5.
INSERT INTO user_sdo_geom_metadata VALUES (
'city_points',
'SHAPE',
SDO_DIM_ARRAY(
SDO_DIM_ELEMENT('Longitude',-180,180,0.5),
SDO_DIM_ELEMENT('Latitude',-90,90,0.5)
),
4326
);
-- Create the spatial index.
CREATE INDEX city_points_spatial_idx on city_points(SHAPE)
INDEXTYPE IS MDSYS.SPATIAL_INDEX_V2;
-- Later, add new records to the table, using original INSERT format
-- (latitude and longitude, no spatial geometry object data).
-- Then update to include spatial geometry object information.
-- Tip: For efficiency, keep track of existing and new records, and use
-- a WHERE clause to restrict the UPDATE to new records (not shown here).
INSERT INTO city_points (city_id, city_name, latitude, longitude)
VALUES (5, 'Chicago', 41.848832, -87.648926);
INSERT INTO city_points (city_id, city_name, latitude, longitude)
VALUES (6, 'Miami', 25.755043, -80.200195);
UPDATE city_points SET shape =
SDO_GEOMETRY(LONGITUDE, LATITUDE);
例1-6は、最初SDO_GEOMETRY列を含まないが、各レコードの緯度および経度の値(指定した都市内または近郊のポイント)を含む表(CITY_POINTS)を作成します。これは、表を空間的に有効化し、既存のレコードを更新してSDO_GEOMETRY情報を含めるだけでなく、新しいレコードを挿入してそれらを更新します。
例1-6に関するノートは、次のとおりです。
-
列名が、UPDATE文のジオメトリ・コンストラクタ(この場合SDO_POINT)で正しい順序で指定されているかぎり、元の表にLATITUDEとLONGITUDEの値がこの順序で含まれていなくてもかまいません。(SDO_GEOMETRYオブジェクトでは、ポイントの経度が最初で、次が緯度です。)
-
検証はポイントと関係ないため、ジオメトリの検証はこの例に含まれていません。ただし、他の種類のジオメトリを持つ表を空間的に有効化する場合、最初と追加のジオメトリすべてを検証する必要があります。(検証を実行するには、SDO_GEOM.VALIDATE_LAYER_WITH_CONTEXTまたはSDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXTを使用します。)
親トピック: Spatialの概念