Professional Documents
Culture Documents
Les05 - Spatial Query
Les05 - Spatial Query
Spatial Queries
Exact
Secondary Result
Primary Set
Filter
Spatial Layer Filter Reduced
Data Data Set Spatial Exact
Spatial Index Result
Functions
Set
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, ®
•• Performs
Performs an
an approximate
approximate query
query (primary
(primary filter
filter
only)
only)
•• Returns
Returns TRUE
TRUE or
or FALSE
FALSE
• QUERYTYPE
QUERYTYPE
–– Valid
Valid values
values are
are WINDOW
WINDOW or
or JOIN
JOIN (very
(very rare)
rare)
Copyright Intergraph Hong Kong Limited, ®
• 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)
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, ®
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
Contains B B
Inside
Touch
Covers
Covered by
A
B
A
B
Overlap Overlap
Boundaries Intersect Boundaries Disjoint
A
Disjoint
On B
• Performs
Performs aa primary
primary and
and secondary
secondary filter
filter
• Returns
Returns TRUE
TRUE oror FALSE
FALSE
• 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)
• 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
• 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';
• 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';
• 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;
• 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, ®
• 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.
• 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, ®
• 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, ®
•• 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)
• 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, ®
• 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';
•• 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, ®
COUNTY
COUNTY RELATIONSHIP
RELATIONSHIP
-------------------------------
------------------------------- --------------
--------------
Atlantic
Atlantic COVERS
COVERS
Cape
Cape May
May COVERS
COVERS
Cumberland
Cumberland COVERS
COVERS
Essex
Essex CONTAINS
CONTAINS
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, ®
• 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 !!
• 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, ®
• The
The function
function does
does not
not take
take advantage
advantage of
of spatial
spatial
indices
indices !!
Copyright Intergraph Hong Kong Limited, ®
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, ®
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