Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 47

5

Spatial Queries

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


Objectives
After
After completing
completing this
this lesson,
lesson, you
you should
should
be
be able
able to
to do
do the
the following:
following:
• Describe
Describe aa spatial
spatial query
query and
and aa spatial
spatial join
join
• Explain
Explain the
the query
query model
model
• Describe
Describe and
and compare
compare spatial
spatial operators
operators
and
and functions
functions
• Use
Use the
the spatial
spatial operators
operators and
and functions
functions
to
to perform
perform spatial
spatial queries
queries and
and joins
joins
• Understand
Understand thethe topological
topological relationships
relationships
used
used by
by the
the spatial
spatial operators
operators
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -2
Optimized Query Model

Exact
Secondary Result
Primary Set
Filter
Spatial Layer Filter Reduced
Data Data Set Spatial Exact
Spatial Index Result
Functions
Set

Table where Index retrieves Procedures


coordinates are area of interest that determine
stored (window) exact relationship
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -3
Primary Filter Example - FIXED

sdo_level = 2

• Compares
Compares fixed
fixed sized
sized tiles
tiles that
that approximate
approximate the
the
area
area of
of interest
interest with
with fixed
fixed sized
sized tiles
tiles that
that
approximate
approximate each
each geometry
geometry
• Result
Result is
is not
not exact
exact because
because comparing
comparing
approximations
approximations
Primary Filter Example
(R-tree Index)

•• Compares
Compares MBR
MBR that
that approximates
approximates the
the area
area of
of
interest
interest with
with the
the MBR
MBR that
that approximates
approximates each
each
geometry
geometry
•• Result
Result is
is not
not exact
exact because
because comparing
comparing
approximations
approximations
Spatial Operators vs Functions
• Spatial
Spatial operators:
operators:
–– Take
Take advantage
advantage of
of spatial
spatial indexes
indexes
–– Require
Require that
that spatial
spatial index
index exists
exists on
on the
the first
first
geometry
geometry specified
specified in
in the
the operator
operator
–– Only
Only in
in the
the WHERE
WHERE clause
clause
•• Spatial
Spatial Functions:
Functions:
–– Do
Do not
not take
take advantage
advantage of
of spatial
spatial indexes
indexes
–– Could
Could be
be used
used on
on small
small tables
tables that
that are
are not
not
spatially
spatially indexed
indexed
–– Can
Can be
be used
used in
in the
the SELECT
SELECT list
list or
or WHERE
WHERE
clause
clause
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -6
Spatial Operators vs Functions
Operators Functions
•• SDO_FILTER
SDO_FILTER •• SDO_GEOM.RELATE
SDO_GEOM.RELATE
–– Performs
Performs aa primary
primary filter
filter only
only –– To
To determine
determine the
the relationship
relationship
between
between two
two geometries
geometries
•• SDO_RELATE
SDO_RELATE
–– Performs –– To
To perform
perform aa spatial
spatial query
query
Performs aa primary
primary and
and
secondary without
without using
using aa spatial
spatial index
index (i.e.
(i.e.
secondary filter
filter
on
on aa small
small table)
table)
•• SDO_WITHIN_DISTANCE
SDO_WITHIN_DISTANCE
•• SDO_GEOM.WITHIN_DISTANCE
SDO_GEOM.WITHIN_DISTANCE
–– Generates
Generates aa buffer
buffer around
around aa
geometry –– Generates
Generates aa buffer
buffer around
around aa
geometry and
and performs
performs aa
primary geometry
geometry and
and performs
performs aa
primary and
and optionally
optionally aa
secondary secondary
secondary filter
filter
secondary filter
filter
•• SDO_NN
SDO_NN
–– Returns
Returns nearest
nearest neighbors
neighbors

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -7
The SDO_FILTER operator
boolean
boolean :=
:= MDSYS.SDO_FILTER
MDSYS.SDO_FILTER
(( <geometry-1>,
<geometry-1>,
<geometry-2>,
<geometry-2>,
‘QUERYTYPE=<querytype>
‘QUERYTYPE=<querytype>
[other
[other optional
optional parameters]’
parameters]’
))

•• Performs
Performs an
an approximate
approximate query
query (primary
(primary filter
filter
only)
only)
•• Returns
Returns TRUE
TRUE or
or FALSE
FALSE

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -8
Required arguments
• GEOMETRY-1
GEOMETRY-1
–– Must
Must be
be aa column
column in
in aa table
table
–– Must
Must be
be of
of type
type SDO_GEOMETRY
SDO_GEOMETRY
–– Must
Must be
be indexed
indexed
• GEOMETRY-2
GEOMETRY-2
–– Variable
Variable oror column
column in
in aa table
table
–– Must
Must bebe of
of type
type SDO_GEOMETRY
SDO_GEOMETRY
–– IfIf querytype=JOIN,
querytype=JOIN, must
must be be aa column
column in
in aa table
table and
and indexed
indexed
(very
(very rare)
rare) with
with same
same index
index type
type (Quadtree/Rtree)
(Quadtree/Rtree) and
and
SDO_LEVEL
SDO_LEVEL (if (if Quadtree)
Quadtree)

• QUERYTYPE
QUERYTYPE
–– Valid
Valid values
values are
are WINDOW
WINDOW or
or JOIN
JOIN (very
(very rare)
rare)
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -9
Optional arguments
• IDXTAB1
–– Index
Index table
table toto associate
associate with
with first
first geometry
geometry
in
in operator.
operator.
–– By
By default,
default, the
the primary
primary index
index table
table is
is used.
used.
• IDXTAB2
–– Index
Index table
table to
to associate
associate with
with the
the second
second
geometry
geometry in
in operator
operator
–– By
By default,
default, the
the primary
primary index
index table
table is
is used.
used.
–– Only
Only supported
supported if
if QUERYTYPE=JOIN
QUERYTYPE=JOIN
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -10
Optional arguments (continued)

• LAYER_GTYPE
–– Set
Set to
to POINT
POINT if
if querying
querying POINT
POINT only
only
columns
columns (for
(for optimal
optimal performance,
performance,
quadtree
quadtree indexes
indexes only)
only)
–– Otherwise,
Otherwise, NOTPOINT
NOTPOINT (default)
(default)

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -11
SDO_FILTER Example
• Find
Find all
all cities
cities in
in aa selected
selected rectangular
rectangular area
area
• Result
Result is
is approximate
approximate
select
select c.city,
c.city, c.pop90
c.pop90
from
from cities
cities cc
where
where mdsys.sdo_filter
mdsys.sdo_filter ((
c.location,
c.location,
mdsys.sdo_geometry
mdsys.sdo_geometry (2003,
(2003, null,
null, null,
null,
mdsys.sdo_elem_info_array
mdsys.sdo_elem_info_array (1,1003,3),
(1,1003,3),
mdsys.sdo_ordinate_array
mdsys.sdo_ordinate_array (-109,37,-102,40)),
(-109,37,-102,40)),
'querytype=WINDOW
'querytype=WINDOW layer_gtype=POINT')
layer_gtype=POINT') == 'TRUE';
'TRUE';

Hint #1: All Spatial operators return TRUE or FALSE. When writing
spatial queries always test with = 'TRUE', never <> 'FALSE' or
= 'true'
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -12
Spatial (topological) relationships
B
A A A

B
B

Contains Covers
Touch
Inside Covered by
A A A red B green
B
B

Overlap Overlap
Boundaries Intersect Boundaries Disjoint Equal
A

Disjoint

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -13
Spatial (topological) relationships
(continued)
A A A
B

Contains B B
Inside
Touch
Covers
Covered by
A
B
A
B
Overlap Overlap
Boundaries Intersect Boundaries Disjoint

A
Disjoint
On B

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -14
Spatial (topological) relationships
(cont.)
• ANYINTERACT
– Returns TRUE if not disjoint
– Optimal mask
– In 8.1.6 was only mask to take advantage of
center tile optimization

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -15
The SDO_RELATE operator
MDSYS.SDO_RELATE
MDSYS.SDO_RELATE
(( <geometry-1>,
<geometry-1>,
<geometry-2>,
<geometry-2>,
‘MASK=<mask>
‘MASK=<mask>
QUERYTYPE=<querytype>
QUERYTYPE=<querytype>
[other
[other optional
optional parameters]’
parameters]’
)) == ‘TRUE’
‘TRUE’

• Performs
Performs aa primary
primary and
and secondary
secondary filter
filter
• Returns
Returns TRUE
TRUE oror FALSE
FALSE

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -16
Required arguments
• GEOMETRY-1
GEOMETRY-1
–– Must
Must be
be aa column
column in
in aa table
table
–– Must
Must be
be of
of type
type SDO_GEOMETRY
SDO_GEOMETRY
–– Must
Must be
be indexed
indexed
• GEOMETRY-2
GEOMETRY-2
–– Variable
Variable or
or column
column in
in aa table
table
–– Must
Must be
be of
of type
type SDO_GEOMETRY
SDO_GEOMETRY
–– IfIf querytype=JOIN,
querytype=JOIN, must
must be
be aa column
column inin aa table
table
and
and indexed
indexed (very
(very rare)
rare) with
with same
same index
index type
type
(Quadtree/Rtree)
(Quadtree/Rtree) andand SDO_LEVEL
SDO_LEVEL (if (if Quadtree)
Quadtree)

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -17
Required arguments (cont.)

• MASK
MASK
–– Identify
Identify spatial
spatial relationship
relationship to
to test
test
–– Must
Must be
be UPPER
UPPER CASE
CASE inin 8.1.5,
8.1.5, case
case doesn’t
doesn’t
matter
matter in
in 8.1.6+
8.1.6+
–– OR’ed
OR’ed masks
masks dodo not
not work
work in
in 8.1.5,
8.1.5, fixed
fixed in
in
8.1.6+
8.1.6+ (i.e.
(i.e. INSIDE+COVEREDBY)
INSIDE+COVEREDBY)
•• QUERYTYPE
QUERYTYPE
–– Valid
Valid values
values are
are WINDOW
WINDOW or
or JOIN
JOIN (very
(very rare)
rare)

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -18
Optional arguments
• IDXTAB1
IDXTAB1
–– Index
Index table
table to
to associate
associate with
with first
first geometry
geometry in
in
operator.
operator.
–– By
By default,
default, the
the primary
primary index
index table
table is
is used.
used.
• IDXTAB2
IDXTAB2
–– Index
Index table
table to
to associate
associate with
with the
the second
second
geometry
geometry in
in operator
operator
–– By
By default,
default, the
the primary
primary index
index table
table is
is used.
used.
–– Only
Only supported
supported ifif QUERYTYPE=JOIN
QUERYTYPE=JOIN

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -19
Optional arguments (continued)

• LAYER_GTYPE
LAYER_GTYPE
–– Set
Set to
to POINT
POINT ifif querying
querying POINT
POINT only
only columns
columns
(for
(for optimal
optimal performance)
performance)
–– Otherwise,
Otherwise, NOTPOINT
NOTPOINT (default)
(default)
–– Currently
Currently applicable
applicable only
only to
to Quadtree
Quadtree indexes
indexes

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -20
SDO_RELATE - A window query

• Find
Find all
all counties
counties in
in the
the state
state of
of New
New Hampshire
Hampshire

select
select c.county,
c.county, c.state_abrv
c.state_abrv
from
from counties
counties c,
c, states
states ss
where
where s.state
s.state == 'New
'New Hampshire'
Hampshire'
and
and mdsys.sdo_relate
mdsys.sdo_relate (c.geom,
(c.geom, s.geom,
s.geom,
'mask=INSIDE+COVEREDBY
'mask=INSIDE+COVEREDBY querytype=WINDOW')
querytype=WINDOW') == 'TRUE';
'TRUE';

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -21
SDO_RELATE - A window query
•• Find
Find all
all counties
counties in
in the
the state
state of
of New
New Hampshire
Hampshire
select
select c.county,
c.county, c.state_abrv
c.state_abrv
from
from counties
counties c,
c, states
states ss
where
where s.state
s.state == 'New
'New Hampshire'
Hampshire'
and
and mdsys.sdo_relate
mdsys.sdo_relate (c.geom,
(c.geom, s.geom,
s.geom,
'mask=INSIDE
'mask=INSIDE querytype=WINDOW')
querytype=WINDOW') == 'TRUE'
'TRUE'
union
union all
all
select
select c.county,
c.county, c.state_abrv
c.state_abrv
from
from counties
counties c,
c, states
states ss
where
where s.state
s.state == 'New
'New Hampshire'
Hampshire'
and
and mdsys.sdo_relate
mdsys.sdo_relate (c.geom,
(c.geom, s.geom,
s.geom,
'mask=COVEREDBY
'mask=COVEREDBY querytype=WINDOW')
querytype=WINDOW') == 'TRUE';
'TRUE';
Hint #2: This query uses interior tile optimizations, and may be more optimal
in Oracle Spatial 8.1.7.

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -22
SDO_RELATE - Another window query

• Find
Find all
all counties
counties around
around county
county Passaic
Passaic

select
select c1.county,
c1.county, c1.state_abrv
c1.state_abrv
from
from counties
counties c1,
c1,
counties
counties c2
c2
where
where c2.state
c2.state == 'New
'New Jersey'
Jersey'
and
and c2.county
c2.county == 'Passaic'
'Passaic'
and
and mdsys.sdo_relate
mdsys.sdo_relate (c1.geom,
(c1.geom, c2.geom,
c2.geom,
'mask=TOUCH
'mask=TOUCH querytype=WINDOW')
querytype=WINDOW') == 'TRUE';
'TRUE';

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -23
SDO_RELATE - A window query
• Find
Find all
all cities
cities in
in aa selected
selected rectangular
rectangular area
area
select
select c.city,
c.city, c.pop90
c.pop90
from
from cities
cities cc
where
where mdsys.sdo_relate
mdsys.sdo_relate ((
c.location,
c.location,
mdsys.sdo_geometry
mdsys.sdo_geometry (2003,
(2003, null,
null, null,
null,
mdsys.sdo_elem_info_array
mdsys.sdo_elem_info_array (1,1003,3),
(1,1003,3),
mdsys.sdo_ordinate_array
mdsys.sdo_ordinate_array (-109,37,-102,40)),
(-109,37,-102,40)),
'mask=ANYINTERACT
'mask=ANYINTERACT querytype=WINDOW
querytype=WINDOW
layer_gtype=POINT')='TRUE';
layer_gtype=POINT')='TRUE';
Note: for point in polygon queries use the ANYINTERACT mask if
you don’t mind returning points which fall on the boundary;
ANYINTERACT is a very fast relate operation.

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -24
SDO_RELATE and PL/SQL

• Find
Find total
total population
population in
in aa selected
selected rectangular
rectangular area
area
set
set serveroutput
serveroutput on;
on;
declare
declare
rectangle
rectangle mdsys.sdo_geometry;
mdsys.sdo_geometry;
total_population
total_population number;
number;
begin
begin
rectangle
rectangle :=
:= mdsys.sdo_geometry
mdsys.sdo_geometry (2003,
(2003, null,
null, null,
null,
mdsys.sdo_elem_info_array
mdsys.sdo_elem_info_array (1,1003,3),
(1,1003,3),
mdsys.sdo_ordinate_array
mdsys.sdo_ordinate_array (-109,
(-109, 37,
37, -102,
-102, 40));
40));
select
select sum(c.totpop)
sum(c.totpop) into
into total_population
total_population
from
from counties
counties cc
where
where mdsys.sdo_relate
mdsys.sdo_relate (c.geom,
(c.geom, rectangle,
rectangle,
'mask=ANYINTERACT
'mask=ANYINTERACT querytype=WINDOW')
querytype=WINDOW') == 'TRUE';
'TRUE';
dbms_output.put_line('Population
dbms_output.put_line('Population == '||total_population||'.');
'||total_population||'.');
end;
end;

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -25
SDO_RELATE - window query
• Find all interstates that interact with a county
select
select i.highway
i.highway
from
from interstates
interstates i,
i, counties
counties cc
where
where c.state
c.state == 'New
'New Jersey'
Jersey' and
and c.county
c.county == 'Passaic'
'Passaic'
and
and mdsys.sdo_relate
mdsys.sdo_relate (i.geom,
(i.geom, c.geom,
c.geom,
'mask=ANYINTERACT
'mask=ANYINTERACT querytype=WINDOW')
querytype=WINDOW') == 'TRUE';
'TRUE';

• Find
Find all
all interstates
interstates that
that interact
interact with
with selected
selected counties
counties
select
select /*+
/*+ ordered
ordered */
*/ i.highway
i.highway
from
from counties
counties c,
c, interstates
interstates ii
where
where c.state
c.state == 'Arizona'
'Arizona' and
and c.poppsqmi
c.poppsqmi << 10
10
and
and mdsys.sdo_relate
mdsys.sdo_relate (i.geom,
(i.geom, c.geom,
c.geom,
'mask=ANYINTERACT
'mask=ANYINTERACT querytype=WINDOW')
querytype=WINDOW') == 'TRUE';
'TRUE';
Hint #3: When the query window comes from a table use the ordered optimizer
hint and specify the query window (geometry-2) first in the from clause.
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -26
SDO_RELATE - join query

• Select
Select all
all the
the city,
city, county
county pairs
pairs
select
select city,
city, county
county
from
from counties
counties c,
c, cities
cities ii
where
where mdsys.sdo_relate
mdsys.sdo_relate (i.location,
(i.location, c.geom,
c.geom,

'mask=ANYINTERACT
'mask=ANYINTERACT
querytype=JOIN
querytype=JOIN
LAYER_GTYPE=POINT')
LAYER_GTYPE=POINT') == 'TRUE';
'TRUE';
**NOTE**
**NOTE** In
In 8.1.6
8.1.6 and
and higher
higher analyzing
analyzing and
and computing
computing statistics
statistics onon
both
both the
the index
index tables
tables help
help the
the execution
execution plan
plan of
of aa join
join query.
query.
Do
Do not
not analyze
analyze the
the index
index tables
tables in
in 8.1.5.
8.1.5.

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -27
The SDO_WITHIN_DISTANCE operator
boolean
boolean :=
:= MDSYS.SDO_WITHIN_DISTANCE
MDSYS.SDO_WITHIN_DISTANCE
(( <geometry-1>,
<geometry-1>,
<geometry-2>,
<geometry-2>,
‘DISTANCE=<n>,
‘DISTANCE=<n>,
QUERYTYPE=<querytype>
QUERYTYPE=<querytype>
[other
[other optional
optional parameters]’
parameters]’
))

• Performs
Performs an
an exact
exact or
or approximate
approximate query
query
• Euclidean
Euclidean distance
distance only
only
• Returns
Returns TRUE
TRUE or
or FALSE
FALSE
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -28
Arguments
• GEOMETRY-1
GEOMETRY-1
–– Must
Must be
be aa column
column in
in aa table
table
–– Must
Must be
be of
of type
type SDO_GEOMETRY
SDO_GEOMETRY
–– Must
Must be
be indexed
indexed
• GEOMETRY-2
GEOMETRY-2
–– Variable
Variable or
or column
column in
in aa table
table
–– Must
Must be
be of
of type
type SDO_GEOMETRY
SDO_GEOMETRY
–– Will
Will be
be buffered
buffered by
by distance
distance
• DISTANCE
DISTANCE (required)
(required)
–– The
The distance
distance (expressed
(expressed in
in the
the units
units used
used for
for
the
the coordinate
coordinate system)
system)
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -29
Optional arguments (continued)
• LAYER_GTYPE
LAYER_GTYPE (optional)
(optional)
–– Set
Set to
to POINT
POINT ifif querying
querying POINT
POINT only
only
columns
columns (for
(for optimal
optimal performance,
performance,
quadtree
quadtree indexes
indexes only)
only)
–– Otherwise,
Otherwise, NOTPOINT
NOTPOINT (default)
(default)
• QUERYTYPE
QUERYTYPE (optional)
(optional)
–– If
If FILTER,
FILTER, primary
primary filter
filter query
query only
only
(approximate
(approximate results)
results)
**NOTE**
**NOTE** Does
Does not
not work
work in
in 8.1.5,
8.1.5, fixed
fixed
in
in 8.1.6
8.1.6
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -30
SDO_WITHIN_DISTANCE Examples

• Find
Find all
all cities
cities within
within aa distance
distance from
from an
an interstate
interstate
select
select /*+
/*+ ordered
ordered */
*/ c.city
c.city
from
from interstates
interstates i,
i, cities
cities cc
where
where i.highway
i.highway == 'I
'I 170'
170'
and
and mdsys.sdo_within_distance
mdsys.sdo_within_distance ((
c.location,
c.location, i.geom,
i.geom,
'distance=0.5
'distance=0.5 layer_gtype=POINT')
layer_gtype=POINT') == 'TRUE';
'TRUE';

• Find
Find intersates
intersates within
within aa distance
distance from
from aa city
city
select
select /*+
/*+ ordered
ordered */
*/ i.highway
i.highway
from
from cities
cities c,
c, interstates
interstates ii
where
where c.city
c.city == 'Tampa'
'Tampa'
and
and mdsys.sdo_within_distance
mdsys.sdo_within_distance ((
i.geom,
i.geom, c.location,'distance=0.5')
c.location,'distance=0.5') == 'TRUE';
'TRUE';
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -31
LOCATOR_WITHIN_DISTANCE
Example
• Find
Find all
all cities
cities within
within 50
50 miles
miles of
of Chicago
Chicago
select
select c1.city
c1.city
from
from cities
cities c1,
c1, cities
cities c2
c2
where
where c2.city
c2.city == 'Chicago'
'Chicago'
and
and mdsys.locator_within_distance
mdsys.locator_within_distance ((
c1.location,
c1.location, c2.location,
c2.location,
‘distance=50
‘distance=50 units=MILE')
units=MILE') == 'TRUE';
'TRUE';

•• LOCATOR_WITHIN_DISTANCE
LOCATOR_WITHIN_DISTANCE is is part
part of
of intermedia
intermedia (which
(which isis free)
free)
•• Can
Can execute
execute radius
radius queries
queries against
against point
point only
only layers
layers (don’t
(don’t need
need toto specify
specify
LAYER_GTYPE = POINT, automatically implied).
LAYER_GTYPE = POINT, automatically implied).
•• Has
Has aa UNITS
UNITS parameter
parameter (MILE,
(MILE, FT,
FT, METER),
METER), very
very accurate
accurate in
in 8.1.6+
8.1.6+
•• When
When using
using units,
units, must
must be
be in
in long/lat
long/lat (WGS
(WGS 84,
84, NAD
NAD 83)
83)

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -32
The SDO_NN (Nearest Neighbor)
operator
boolean := MDSYS.SDO_NN
( <geometry-1>,
<geometry-2>,
‘[optional parameter]’
)

• Returns
Returns the
the nearest
nearest neighbors
neighbors to
to aa geometry
geometry
• Can
Can choose
choose the
the number
number of
of nearest
nearest neighbors
neighbors
• Results
Results not
not returned
returned in
in order
order of
of distance
distance
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -33
Arguments
• GEOMETRY-1
GEOMETRY-1
–– Must
Must be
be aa column
column in
in aa table
table
–– Must
Must be
be of
of type
type SDO_GEOMETRY
SDO_GEOMETRY
–– Must
Must be
be indexed
indexed
• GEOMETRY-2
GEOMETRY-2
–– Variable
Variable or
or column
column in
in aa table
table
–– Must
Must be
be of
of type
type SDO_GEOMETRY
SDO_GEOMETRY
• SDO_NUM_RES
SDO_NUM_RES (optional)
(optional)
–– The
The number
number of of nearest
nearest neighbors
neighbors to
to return
return
–– Default
Default is
is 11
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -34
SDO_NN Example

• Find
Find the
the five
five cities
cities nearest
nearest to
to interstate
interstate II 175
175

select
select /*+
/*+ ordered
ordered */
*/
c.city
c.city
from
from interstates
interstates i,
i, cities
cities cc
where
where i.highway
i.highway == 'I
'I 170'
170'
and
and mdsys.sdo_nn
mdsys.sdo_nn ((
c.location,
c.location, i.geom,
i.geom,
'sdo_num_res=5')
'sdo_num_res=5') == 'TRUE';
'TRUE';

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -35
The SDO_GEOM.RELATE Function
boolean
boolean :=
:= MDSYS.SDO_GEOM.RELATE
MDSYS.SDO_GEOM.RELATE
(( <geometry-1>,
<geometry-1>, <diminfo-1>,
<diminfo-1>,
‘<mask>’,
‘<mask>’,
<geometry-2>,
<geometry-2>, <diminfo-2>
<diminfo-2> ))
or
or
boolean
boolean :=
:= MDSYS.SDO_GEOM.RELATE
MDSYS.SDO_GEOM.RELATE
(( <geometry-1>,‘<mask>’,<geometry-2>,<tolerance>
<geometry-1>,‘<mask>’,<geometry-2>,<tolerance> ))

•• Performs
Performs an
an exact
exact query
query (secondary
(secondary filter)
filter)
•• Returns
Returns TRUE
TRUE or
or FALSE
FALSE for
for an
an ANYINTERACT
ANYINTERACT mask
mask
•• Returns
Returns the
the matching
matching relationship
relationship ifif any
any other
other mask
mask
or
or FALSE
FALSE
•• Can
Can be
be used
used in
in the
the select
select list
list
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -36
SDO_GEOM.RELATE Parameters
• GEOMETRY1
GEOMETRY1
• DIMINFO1
DIMINFO1
–– From
From user_sdo_geom_metadata
user_sdo_geom_metadata table
table
• MASK
MASK
–– Mask
Mask for
for operator
operator
• GEOMETRY2
GEOMETRY2
• DIMINFO2
DIMINFO2
–– From
From user_sdo_geom_metadata
user_sdo_geom_metadata table
table
• TOLERANCE
TOLERANCE
–– The
The tolerance
tolerance value
value to
to be
be used
used
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -37
SDO_GEOM.RELATE Function
• Determine
Determine relationship
relationship of
of counties
counties and
and states
states
select
select c.county,
c.county, mdsys.sdo_geom.relate
mdsys.sdo_geom.relate
(s.geom,
(s.geom,
'determine',
'determine',
c.geom,
c.geom, 0.00000005)
0.00000005)
from
from states
states s,
s, counties
counties cc
where
where s.state
s.state == 'New
'New Jersey'
Jersey'
and
and s.state
s.state == c.state;
c.state;

COUNTY
COUNTY RELATIONSHIP
RELATIONSHIP
-------------------------------
------------------------------- --------------
--------------
Atlantic
Atlantic COVERS
COVERS
Cape
Cape May
May COVERS
COVERS
Cumberland
Cumberland COVERS
COVERS
Essex
Essex CONTAINS
CONTAINS

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -38
SDO_GEOM.RELATE Function
• Determine
Determine relationship
relationship of
of states
states and
and counties
counties
(reverse
(reverse relationship
relationship of
of previous
previous slide)
slide)
select
select c.county,
c.county, mdsys.sdo_geom.relate
mdsys.sdo_geom.relate
(c.geom,
(c.geom,
'determine',
'determine',
s.geom,
s.geom, 0.00000005)
0.00000005)
from
from states
states s,
s, counties
counties cc
where
where s.state
s.state == 'New
'New Jersey'
Jersey'
and
and s.state
s.state == c.state;
c.state;

COUNTY
COUNTY RELATIONSHIP
RELATIONSHIP
-------------------------------
------------------------------- --------------
--------------
Atlantic
Atlantic COVEREDBY
COVEREDBY
Cape
Cape May
May COVEREDBY
COVEREDBY
Cumberland
Cumberland COVEREDBY
COVEREDBY
Essex
Essex INSIDE
INSIDE
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -39
SDO_GEOM.RELATE Function

• Find
Find all
all counties
counties around
around New
New Jersey
Jersey
select
select c.county,
c.county, c.state
c.state
from
from states
states s,
s, counties
counties cc
where
where s.state
s.state == 'New
'New Jersey'
Jersey'
and
and mdsys.sdo_geom.relate
mdsys.sdo_geom.relate
(s.geom,
(s.geom,
'touch',
'touch',
c.geom,
c.geom, 0.00000005)
0.00000005) == 'TOUCH';
'TOUCH';

• The
The function
function does
does not
not take
take advantage
advantage of
of
spatial
spatial indexes
indexes !!

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -40
The SDO_GEOM.WITHIN_DISTANCE Function
boolean
boolean :=
:= MDSYS.SDO_GEOM.WITHIN_DISTANCE
MDSYS.SDO_GEOM.WITHIN_DISTANCE
(<geometry-1>,
(<geometry-1>, <diminfo-1>,
<diminfo-1>,
<distance>,
<distance>,
<geometry-2>,
<geometry-2>, <diminfo-2>
<diminfo-2> ))
or
or
boolean
boolean :=
:= MDSYS.SDO_GEOM.WITHIN_DISTANCE
MDSYS.SDO_GEOM.WITHIN_DISTANCE
(<geometry-1>,
(<geometry-1>, <distance>,
<distance>, <geometry-2>,
<geometry-2>,
<tolerance>
<tolerance> ))

• Performs
Performs an
an exact
exact query
query
• Euclidean
Euclidean distance
distance only
only
• Returns
Returns TRUE
TRUE or
or FALSE
FALSE
• Does
Does NOT
NOT use
use the
the spatial
spatial indices
indices
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -41
SDO_GEOM.WITHIN_DISTANCE
Parameters
•• GEOMETRY1
GEOMETRY1
•• DIMINFO1
DIMINFO1
–– From
From user_sdo_geom_metadata
user_sdo_geom_metadata table
table
•• DISTANCE
DISTANCE
–– The
The distance
distance (expressed
(expressed in
in the
the units
units used
used for
for the
the
coordinate
coordinate system)
system)
•• GEOMETRY2
GEOMETRY2
•• DIMINFO2
DIMINFO2
–– From
From user_sdo_geom_metadata
user_sdo_geom_metadata table
table
•• TOLERANCE
TOLERANCE
–– The
The tolerance
tolerance value
value to
to be
be used
used
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -42
SDO_GEOM.WITHIN_DISTANCE
Function
• Find
Find all
all cities
cities within
within aa distance
distance from
from an
an interstate
interstate
select
select c.city
c.city
from
from cities
cities c,
c, interstates
interstates ii
where
where i.highway
i.highway == 'I
'I 170'
170'
and
and mdsys.sdo_geom.within_distance
mdsys.sdo_geom.within_distance ((
c.location,
c.location,
0.5,
0.5,
i.geom,
i.geom, 0.00000005
0.00000005 )) == 'TRUE';
'TRUE';

• The
The function
function does
does not
not take
take advantage
advantage of
of spatial
spatial
indices
indices !!
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -43
SDO_GEOM.WITHIN_DISTANCE
Function
• Find
Find all
all interstates
interstates within
within aa distance
distance from
from aa city
city
select
select i.highway
i.highway
from
from interstates
interstates i,
i, cities
cities cc
where
where c.city
c.city == 'Tampa'
'Tampa'
and
and mdsys.sdo_geom.within_distance
mdsys.sdo_geom.within_distance ((
i.geom,
i.geom,
(select
(select diminfo
diminfo
from
from user_sdo_geom_metadata
user_sdo_geom_metadata
where
where table_name
table_name == 'INTERSTATES'
'INTERSTATES'
and
and column_name
column_name == 'GEOM'),
'GEOM'),
0.5,
0.5,
c.location,
c.location,
(select
(select diminfo
diminfo
from
from user_sdo_geom_metadata
user_sdo_geom_metadata
where
where table_name
table_name == 'CITIES'
'CITIES'
and
and column_name
column_name == 'LOCATION')
'LOCATION') )) == 'TRUE';
'TRUE';

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -44
Syntax for flattening the varrays

SELECT
SELECT **
FROM
FROM TABLE
TABLE (SELECT
(SELECT a.geom.sdo_ordinates
a.geom.sdo_ordinates
FROM
FROM states
states aa
WHERE
WHERE a.state
a.state == 'California');
'California');

SELECT
SELECT **
FROM
FROM TABLE
TABLE (SELECT
(SELECT a.geom.sdo_elem_info
a.geom.sdo_elem_info
FROM
FROM states
states aa
WHERE
WHERE a.state
a.state == 'California');
'California');

• Can
Can only
only flatten
flatten one
one varray
varray at
at aa time
time
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -45
Summary
In
In this
this lesson,
lesson, your
your should
should have
have learned
learned how
how to:
to:
• Describe
Describe aa spatial
spatial query
query and
and aa spatial
spatial join
join
• Explain
Explain the
the query
query model
model
• Describe
Describe and
and compare
compare spatial
spatial operators
operators and
and
functions
functions
• Use
Use the
the spatial
spatial operators
operators and
and functions
functions to
to
perform
perform spatial
spatial queries
queries
• Understand
Understand thethe topological
topological relationships
relationships used
used by
by
the
the spatial
spatial operators
operators
Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -46
Practice 7-1 Overview

This
This practice
practice covers
covers the
the following
following topics:
topics:
• Perform
Perform various
various queries
queries on
on the
the layers
layers
previously
previously loaded
loaded and
and indexed
indexed
–– STATES
STATES
–– COUNTIES
COUNTIES
–– INTERSTATES
INTERSTATES
–– CITIES
CITIES

Copyright  Intergraph Hong Kong Limited, ®

Oracle Corporation, 2002. All rights reserved.


5 -47

You might also like