0% found this document useful (0 votes)
30 views60 pages

LDB Master Week 4 Spatial Indexing and Querying

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views60 pages

LDB Master Week 4 Spatial Indexing and Querying

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

Spatial indexing and querying

Large Databases
Week 6

Māra Pudāne
[email protected]

10.10.2023.

1
Geometry validation

Rīgas Tehniskā universitāte 2


Validating spatial data
▪ Enables checking whether your input is correct
▪ Done using
SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT and
SDO_GEOM.VALIDATE_LAYER_WITH_CONTEXT subprograms

3
General procedure
▪ Loading data
▪ Running
SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT or
SDO_GEOM.VALIDATE_LAYER_WITH_CONTEXT
▪ Use SDO_MIGRATE.TO_CURRENT to rectify geometry
according to current version
▪ Use SDO_UTIL.RECTIFY_GEOMETRY to fix geometries
▪ However, most work must be done manually

4
Checks type consistency and geometry
consisency
▪ For type consistency, the function checks
– It the SDO_GTYPE is valid
– If the SDO_ETYPE values are consistent with the SDO_GTYPE value
– If the SDO_ELEM_INFO_ARRAY has valid triplet values
▪ For geometry consistency, the function checks
– Polygons have at least four points, which includes the point that closes the poly
– Polygons are not self-crossing
– No two vertices on a line or polygon are the same
– Polygons are oriented correctly
– Points on an arc are not colinear
– Geometries are within the specified bounds of the applicable DIMINFO column value
– etc

Rīgas Tehniskā universitāte 5


SDO_GEOM.VALIDATE_GEOMETRY_WI
TH_CONTEXT
▪ If the geometry is valid, this function returns TRUE
▪ If the geometry is not valid, this function returns the following:
– An Oracle error message number based on the specific reason the geometry is invalid, or FALSE if
the geometry fails for some other reason
– The context of the error (the coordinate, edge, or ring that causes the geometry to be invalid)

Rīgas Tehniskā universitāte 6


Validation – a quick example
▪ SELECT food_deliveries.my_title,
SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT
(food_deliveries.shape, 0.5)
FROM food_deliveries;

Rīgas Tehniskā universitāte 7


Example

Rīgas Tehniskā universitāte 8


Spatial indexing

Rīgas Tehniskā universitāte 9


Spatial indexing is necessary
▪ Spatial indices are used by spatial databases to optimize
spatial queries
▪ Conventional index types do not efficiently handle spatial queries
such as how far two points differ, or whether points fall within a
spatial area of interest
– To find objects within an indexed data space that interact with a
given point or area of interest (window query)
– To find pairs of objects from within two indexed data spaces that
interact spatially with each other (spatial join)

10
Spatial data indexes

Methods for indexing


spatial data

Space-oriented
indexing Data-oriented
indexing
Grid
R-tree
Quadtree
Etc.
etc.

Rīgas Tehniskā universitāte 11


Decomposing the space/plane
▪ The index-creation process decomposes the space into a four-level grid
hierarchy
▪ This grid (or, tiles) are used to approximate geometries

▪ Tesselation rules:
– If an object completely covers a cell, that cell is said to be covered by the object.
– The extent of tessellation of each object depends primarily on the cells-per-object limit of
the spatial index. This limit defines the maximum number of cells that tessellation can
count per object.
– The deepest-cell rule exploits the fact that every lower-level cell belongs to the cell above
it. Therefore, only the deepest-level cells need to be recorded in the index,
minimizing the information that the index needs to store

12
The most common index
▪ B-trees, short for balanced
trees, are the most common
type of database index
▪ B-tree index is an ordered list
of values divided into ranges

Rīgas Tehniskā universitāte 13


Grid
▪ A grid is a regular tesselation or 2-D surface
that divides it into a series of contiguous cells,
which can then be assigned unique identifiers
and used for spatial indexing purposes.

▪ "square" or "rectangular" cells


▪ triangular grids or meshes,
▪ hexagonal grids
▪ etc.

Rīgas Tehniskā universitāte 14


Quadtree
▪ A quadtree is a tree data
structure in which each
internal node has exactly four
children
▪ Quadtrees are most often
used to partition a two-
dimensional space by
recursively subdividing it into
four quadrants or regions.

Rīgas Tehniskā universitāte 15


Octree
▪ Similar to quadtree but
used for 3D space
▪ Often used in 3D graphics
and 3D game engines

Rīgas Tehniskā universitāte 16


R-tree
▪ R-tree. Typically the preferred
method for indexing spatial
data. Objects (shapes, lines
and points) are grouped using
the MBR)

Rīgas Tehniskā universitāte 17


Creating index in Oracle
CREATE INDEX IND_BUILD ON BUILDINGS(BUILDING)
INDEXTYPE IS MDSYS.SPATIAL_INDEX;

CREATE INDEX cola_spatial_idx


ON cola_markets(shape)
INDEXTYPE IS MDSYS.SPATIAL_INDEX
PARAMETERS ('layer_gtype=POLYGON');

Rīgas Tehniskā universitāte 18


Querying spatial data

Rīgas Tehniskā universitāte 19


The two-tier query model

20
Several perspectives
▪ One can query:
– Primary filter
– Secondary filter...

▪ ...and retrieve
– Topological information
– Information on geometries (such as area)

▪ Additionally:
– Querying the non-geometric data

21
Spatial query window

22
Data retrieval from the
primary filter

Rīgas Tehniskā universitāte 23


SDO_FILTER
▪ Retrieves geometries, that are somehow related to area
▪ The primary filter checks to see if the MBRs of the candidate objects interact, not whether the
objects themselves interact

SDO_FILTER
(geometry1 SDO_GEOMETRY,
geometry2 SDO_GEOMETRY,
param VARCHAR2)

Rīgas Tehniskā universitāte 24


Example (1)
Retrieve all geometries related to (4,6), (8,8) dynamic query window

SELECT C.NUM, C.TITLE


FROM Layer_1 C
WHERE SDO_FILTER(C.Geometr, SDO_GEOMETRY(2003, NULL,
NULL, SDO_ELEM_INFO_ARRAY(1,1003,3),
SDO_ORDINATE_ARRAY(4,6, 8,8)) ) = 'TRUE’;

C.NUM C.TITLE
--------------------------------------
5 Geom 5
7 Geom 7

Rīgas Tehniskā universitāte 25


Example (2)
Retrieve all geometries related to windows stored in a table WINDOWS

SELECT A.Feature_ID FROM TARGET A, WINDOWS B


WHERE B.ID = 'WINS_1' AND
sdo_filter(A.shape, B.shape) = 'TRUE';

Rīgas Tehniskā universitāte 26


Example (3)
▪ Get geometries from Layer_1, that are related to geometry G1 from Layer_2

SELECT A.Title
FROM Layer_1 A, Layer_2 B
WHERE B.Title = ‘G1’ AND SDO_FILTER(A.Geometry,
B.Geometry) = 'TRUE';

Rīgas Tehniskā universitāte 27


Secondary filter querying

28
Secondary filter methods
▪ SDO_JOIN: Performs a spatial join based on one or more topological relationships.
▪ SDO_NN: Determines the nearest neighbour geometries to a geometry.
▪ SDO_NN_DISTANCE: Returns the distance of an object returned by the SDO_NN
operator.
▪ SDO_RELATE: Determines whether or not two geometries interact in a specified way.
▪ SDO_WITHIN_DISTANCE: Determines if two geometries are within a specified distance
from one another.

+
More specific filters, such as SDO_EQUAL, etc.

Rīgas Tehniskā universitāte 29


SDO_RELATE
▪ Uses 9-intersection matrix:
– Interior
– Boundary
– Exterior

▪ Retrieves binary topological relationships


by manipulating this matrix

30
Binary topological relationships

Rīgas Tehniskā universitāte 31


Example
SELECT A.Feature_ID FROM TARGET A
WHERE sdo_relate(A.shape, SDO_geometry(2003,NULL,NULL,
SDO_elem_info_array(1,1003,3),
SDO_ordinate_array(x1,y1, x2,y2)), 'mask=anyinteract') = 'TRUE’;

▪ How to implement OR:

'mask=inside+touch'

Rīgas Tehniskā universitāte 32


More specific filters
▪ Provided for conveinience
▪ Perform SDO_RELATE on a specific mask type

SDO_ANYINTERACT(geometry1, geometry2);

Rīgas Tehniskā universitāte 33


SDO_WITHIN_DISTANCE
▪ Used to determine the set of objects in a table that are within n distance units from a
reference object
▪ Only 2D

SDO_WITHIN_DISTANCE
(geometry1 SDO_GEOMETRY,
aGeom SDO_GEOMETRY,
params VARCHAR2);

Rīgas Tehniskā universitāte 34


Example
SELECT A.NUM FROM Layer_1 A
WHERE
SDO_WITHIN_DISTANCE(A.Geometry, sdo_geometry(2003,NULL,NULL,
sdo_elem_info_array(1,1003,3), sdo_ordinate_array(x1,y1,x2,y2)),
'distance = 10') = 'TRUE';

Rīgas Tehniskā universitāte 35


Example with two layers
SELECT A.NUM FROM Layer_1 A, Layer_2 B
WHERE B.NUM = 1 AND
SDO_WITHIN_DISTANCE(A.Geometry_1, B.Geometry_2, 'distance = 10') = 'TRUE';

Rīgas Tehniskā universitāte 36


SDO_NN
▪ Used to identify the nearest neighbours for a geometry
▪ Can be used only if a spatial index has been created on two dimensions of data

SDO_NN
(geometry1 SDO_GEOMETRY,
geometry2 SDO_GEOMETRY,
param VARCHAR2 [, number NUMBER]);

Rīgas Tehniskā universitāte 37


Example
SELECT r.name FROM restaurants r WHERE
SDO_NN(r.geometry, :my_hotel,
'sdo_batch_size=10 distance=2 unit=mile') = 'TRUE'
AND r.cuisine = 'Italian' AND ROWNUM <=2;

Rīgas Tehniskā universitāte 38


Spatial join SDO_JOIN
▪ A spatial join takes place when you compare all geometries of one layer to all geometries of
another layer.
▪ SDO_JOIN is technically not an operator, but a table function. This function is
recommended when you need to perform full table joins.

▪ Returns an object of SDO_ROWIDSET, which consists of a table of objects of


SDO_ROWIDPAIR.

Rīgas Tehniskā universitāte 39


Primary filter join
SELECT /*+ ordered */ a.gid, b.gid
FROM TABLE(SDO_JOIN('PARKS', 'SHAPE',
'HIGHWAYS', 'SHAPE',
'mask=FILTER')) c,
parks a,
highways b
WHERE c.rowid1 = a.rowid AND c.rowid2 = b.rowid;

Rīgas Tehniskā universitāte 40


Secondary filter join
SELECT /*+ ordered */ a.gid, b.gid
FROM TABLE(SDO_JOIN('PARKS', 'SHAPE',
'HIGHWAYS', 'SHAPE',
'mask=ANYINTERACT')) c,
parks a,
highways b
WHERE c.rowid1 = a.rowid AND c.rowid2 = b.rowid;

Rīgas Tehniskā universitāte 41


Spatial aggregate functions

Rīgas Tehniskā universitāte 42


Various spatial aggregate functions
▪ SDO_AGGR_CENTROID: Returns a geometry object that is the centroid ("center of gravity")
of the specified geometry objects.
▪ SDO_AGGR_CONCAT_LINES: Returns a geometry that concatenates the specified line or
multiline geometries.
▪ SDO_AGGR_CONVEXHULL: Returns a geometry object that is the convex hull of the
specified geometry objects.
▪ SDO_AGGR_LRS_CONCAT: Returns an LRS geometry object that concatenates specified
LRS geometry objects.
▪ SDO_AGGR_MBR: Returns the minimum bounding rectangle of the specified geometries.
▪ SDO_AGGR_SET_UNION: Takes a VARRAY of SDO_GEOMETRY objects as input, and
returns the aggregate union of all geometry objects in the array.
▪ SDO_AGGR_UNION: Returns a geometry object that is the topological union (OR operation)
of the specified geometry objects.

43
SDO_GEOM functions

44
Operations
▪ Relationship (True/False) between two objects: RELATE,
WITHIN_DISTANCE
▪ Validation: VALIDATE_GEOMETRY_WITH_CONTEXT,
VALIDATE_LAYER_WITH_CONTEXT
▪ Single-object operations: SDO_ALPHA_SHAPE,
SDO_ARC_DENSIFY, SDO_AREA, SDO_BUFFER,
SDO_CENTROID, SDO_CONVEXHULL, SDO_CONCAVEHULL,
SDO_CONCAVEHULL_BOUNDARY, SDO_LENGTH,
SDO_MAX_MBR_ORDINATE, SDO_MIN_MBR_ORDINATE,
SDO_MBR, SDO_POINTONSURFACE, SDO_TRIANGULATE,
SDO_VOLUME
▪ Two-object operations: SDO_CLOSEST_POINTS, SDO_DISTANCE,
SDO_DIFFERENCE, SDO_INTERSECTION, SDO_UNION,
SDO_XOR
45
SDO_GEOM.SDO_AREA

SELECT c.name, SDO_GEOM.SDO_AREA(c.shape, 0.005) FROM


cola_markets c
WHERE c.name = 'cola_a';

Rīgas Tehniskā universitāte 46


SDO_GEOM.SDO_CLOSEST_POINTS
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);

Rīgas Tehniskā universitāte 47


SDO_GEOM.SDO_CLOSEST_POINTS (1)
DECLARE
cola_c_geom SDO_GEOMETRY;
cola_d_geom SDO_GEOMETRY;
dist NUMBER;
geoma SDO_GEOMETRY;
geomb SDO_GEOMETRY;
BEGIN
SELECT c.shape into cola_c_geom FROM cola_markets c
WHERE c.name = 'cola_c';
SELECT c.shape into cola_d_geom FROM cola_markets c
WHERE c.name = 'cola_d';

Rīgas Tehniskā universitāte 48


SDO_GEOM.SDO_CLOSEST_POINTS (2)
SDO_GEOM.SDO_CLOSEST_POINTS(cola_c_geom,
cola_d_geom, 0.005, NULL,
dist, geoma, geomb);
INSERT INTO cola_markets VALUES(9901, 'geoma', geoma);
INSERT INTO cola_markets VALUES(9902, 'geomb', geomb);
DBMS_OUTPUT.PUT_LINE('dist output parameter value = ' ||
dist);
END;

Rīgas Tehniskā universitāte 49


SDO_GEOM.SDO_LENGTH

SELECT c.name, SDO_GEOM.SDO_LENGTH(c.shape, m.diminfo)


FROM cola_markets c, user_sdo_geom_metadata m
WHERE m.table_name = 'COLA_MARKETS' AND m.column_name = 'SHAPE';

Rīgas Tehniskā universitāte 50


Visualisation

51
Geometry validation in a tool

52
Simple visualization

Rīgas Tehniskā universitāte 53


View/map view

Rīgas Tehniskā universitāte 54


Visualisation with GeoRaptor*

*GeoRaptor might not work with newer versions of SQLDeveloper

Rīgas Tehniskā universitāte 55


After installing

Rīgas Tehniskā universitāte 56


Visualization with programming
language

Rīgas Tehniskā universitāte 57


Practical work
▪ Select the topic for individual work
– Example: Spatial data model in Oracle
▪ Write a theoretical background on the topic with
references (3-4 pages, master thesis formatting)!
▪ Do a practical work – depends on the topic!
– Example:
– Define 10 shapes and three layers!
– Write 8 spatial queries!
Rīgas Tehniskā universitāte 58
Class work

▪ Split into groups of 2-3 (make sure there is at least one


with computer), you can also work alone!

▪ Write a primary and secondary filter query based on the


valid geometries in the script!

Rīgas Tehniskā universitāte 59


Q&A

60

You might also like