Spatial Databases: Esteban ZIM Anyi
Spatial Databases: Esteban ZIM Anyi
Esteban ZIMÁNYI
Department of Computer & Decision Engineering (CoDE)
Université Libre de Bruxelles
[email protected]
_ SQL/MM
_ Representative Systems
_ Summary
2
Foreword
Spatial Databases
_ A database that needs to store and query spatial objects, e.g.
• Point: a house, a monument
• Line: a road segment, a road network
• Polygon: a county, a voting area
4
Spatial Database Management Systems
_ A Database Management System that manages data existing in some space
_ 2D or 2.5D
• Integrated circuits: VLSI design
• Geographic space (surface of the Earth): GIS, urban planning
_ 2.5 D: Elevation
_ 3D
• Medicine: Brain models
• Biological research: Molecule structures
• Architecture: CAD
• Ground models: Geology
_ Supporting technology able to manage large collections of geometric objects
_ Major commercial and open-source DBMSs provide spatial support
_ Additional spatial data types and operations can be defined in spatial database
_ SQL was extended to support spatial data types and operations, e.g., OGC Simple Features for SQL
6
Application Areas
_ Street network-based
• Vehicle routing and scheduling (cars, planes, trains)
• Location analysis, ...
_ Natural resource-based
• Management of areas: agricultural lands, forests, recreation resources, wildlife habitat analysis,
migration routes planning...
• Environmental impact analysis
• Toxic facility siting
• Groundwater modeling, ...
_ Land parcel-based
• Zoning, subdivision plan review
• Environmental impact statements
• Water quality management
• Facility management: electricity, gaz, clean water, used water, network
_ Address queries
_ Perform operations
8
Example of Queries
_ On a subway map of Brussels, what is the shortest way from here to the Grand Place?
⇒ shortest path algorithm
_ Overlay the land use map with the map of districts in Belgium
_ Given the map of a neighborhood, find the best spot for opening a drugstore (based on a given set of
optimality criteria) ⇒ allocation problem
_ Connection between system elements is geography, e.g., location, proximity, spatial distribution
_ Common purpose: decision making for managing use of land, resources, ocean data, transportation,
geomarketing, urban planning, etc.
_ Many commercial and open source systems, but limited temporal support
10
GIS as a Set of Subsystems
_ Data processing
• Data acquisition (from maps, images): input, store
_ Data analysis
• Retrieval, analysis (answers to queries, complex statistical analyses)
_ Information use
• Users: Researchers, planners, managers
• Interaction needed between GIS group and users to plan analytical procedures and data structures
_ Management system
• Organizational role: separate unit in a resource management
• Agency offering spatial database and analysis services
• System manager, system operator, system analysts, digitizer operators
11
_ Remote sensing: Techniques for image acquisition and processing (space images)
_ Statistics: Statistical techniques for analysis + errors and uncertainty in GIS data
_ Mathematics: (Computational) geometry and graph theory for analysis of spatial data
12
Contributing Disciplines and Technologies (2)
_ Computer Science
• Computer Aided Design (CAD): Software, techniques for data input, display, representation, visu-
alization
• Computer graphics: Hardware, software for handling graphic objects
• Artificial Intelligence (AI): Emulate human intelligence and decision making (computer = “ex-
pert”).
• Database Management Systems (DBMS): Representing, handling large volumes of data (access,
updates)
13
Application Programs
Files
_ Not modular
_ Not reusable
_ Not extensible
_ Not friendly
_ Very efficient
14
GIS Architectures: Loosely Coupled Approach
Application Programs
Standard SQL
Relational Geometric
DBMS Processing
DB Files
15
Application Programs
Standard SQL
+ ADT Spatial
DBMS
DB
_ Modular
_ Reusable
16
Main Database Issues
_ Data modeling
_ Query optimization
_ Spatial access methods define index structure to accelerate the retrieval of objects
_ Versioning
17
_ SQL/MM
_ Representative Systems
_ Summary
18
Projected Coordinates Systems
_ Why focus on projections?
• Going from 3 to 2 dimensions for Earth representation always involves a projection
• Information on the projection is essential for applications analyzing spatial relationships
• Choice of the projection can influence the results
_ The Earth is a complex surface whose shape and dimensions can not be described with mathematical
formulas
_ Two main reference surfaces are used to approximate the shape of the Earth: the ellipsoid and the
Geoid
19
The Geoid
_ It is the equipotential surface of the Earth’s gravity field which best fits the global mean sea level and
extended through the continents
_ Since the mathematical description of the geoid is unknown, it is impossible to identify mathematical
relationships for moving from the Earth to the map
20
Ellipsoid
21
_ The physical Earth has excursions of +8,000 m (Mount Everest) and -11,000 m (Mariana Trench)
_ The geoid’s total variation goes from -107m to +85 m compared to a perfect mathematical ellipsoid
22
Latitude and Longitude
_ Measures of the angles (in degrees) from the center of the Earth to a point on the Earth’s surface
_ Latitude measure angles in a North-South direction: the Equator is at an angle of 0
_ Longitude measures angles in an East-West direction: the Prime Meridian is at angle of 0
_ Prime Meridian: imaginary line running from the North to the South Pole through Greenwich, England
_ For example, the location of Brussels, Belgium is 50.8411◦ N, 4.3564◦ E
_ Longitude and latitude are not uniform units of measure
_ Only along the Equator the distance represented by one degree of longitude approximates the distance
represented by one degree of latitude
23
Map Projections
_ To produce a map, the curved reference surface of the Earth, approximated by an ellipsoid or a sphere,
is transformed into the flat plane of the map by means of a map projection
_ A point on the reference surface of the Earth with geographic coordinates (φ, λ) is transformed into
Cartesian (or map) coordinates (x, y) representing positions on the map plane
_ Each projection causes deformations
_ Map projections can be categorised in four ways: Class, Angle, Fit and Properties
24
Map Projections: Shape of Projection Surface
_ Shape of the projection surface, commonly either a flat plane, a cylinder or a cone
_ Cones and cylinders are not flat shapes, but they can be rolled flat without introducing additional
distortion
• Cylindrical: coordinates are projected onto a rolled cylinder
• Conical: coordinates are projected onto a rolled cone
• Azimuthal: coordinates are projected directly onto a flat planar surface
_ Azimuthal projections work best for circular areas (e.g., the poles)
_ Cylindrical projections work best for rectangular areas (e.g., world maps)
_ Conical projections work best for triangle shaped areas (e.g., continents)
25
_ This refers to the alignment of the projection surface, measured as the angle between the main axis of
the earth and the main symmetry axis of the projection surface.
• Normal: the two axes are parallel
• Transverse: the two axes are perpendicular
• Oblique: the two axes are at some other angle
_ Ideally the plane of projection is aligned as closely as possible with the main axis of the area to be
mapped. This helps to minimise distortion and scale error.
26
Map Projections: Fit
_ A measure of how closely the projection surface fits the surface of the Earth
• Tangent: the projection surface touches the surface of the Earth
• Secant: the projection surface slices through the Earth
_ Distortion occurs wherever the projection surface is not touching or intersecting the surface of the
Earth
_ Secant projections usually reduce scale error because the two surfaces intersect in more places and the
overall fit tends to be closer
_ A globe is the only way to represent the entire Earth without any significant scale error
27
_ Conformal map projections are recommended for navigational charts and topographic maps
_ Equidistant map projections should be used when measuring distances from a point: air routes, radio
propagation strength, radiation dispersal
28
Conformal Cylindrical Projection: Mercator Projection
29
30
Cylindrical Equal-Area Projection: Behrmann Projection
31
32
True-direction Azimuthal Projection
33
Parameters
Ellipsoid Identity GRS80
_ Projection plane: Conical semi-major axis a 6,378,137 m
flattening f 1/298.257222101
_ Direction of the projection axis: Normal Standard Parallels ϕ1 49◦ 50’ N
ϕ2 51◦ 10’ N
_ Nature of the contact: Secant
Origine Origin Latitude 50◦ 47’ 52” 134 N
_ Nature of the deformation: Conformal Central meridian 4◦ 21’ 33” 177 E
Origin Coordinates x0 649,328 m
y0 665,262 m
34
Spatial Databases: Topics
_ Introduction
_ SQL/MM
_ Representative Systems
_ Summary
35
_ Multiple representations
• different scales, different viewpoints
36
Interaction Requirements
_ Visual interactions
• map displays
• information visualization
• graphical queries on maps
_ Multiple instantiations
• a building may be a school and a church
• a road segment may be also a segment of a hiking trail
37
Practical Requirements
_ Huge data sets
• Collecting new data is expensive
• Reusing highly heterogeneous existing data sets is a must ... but is very difficult!
• Integration requires understanding, hence a conceptual model
_ Interoperability
38
Why Conceptual Modeling?
_ Focuses on the application
_ Technology independent
• portability, durability
_ User oriented
39
_ Formal definition
40
Orthogonality: What is Spatial
41
River
river_name
course
catchments (1,n)
reservoirs (1,n)
River In Catchment
(1,n) (1,n)
river_name catchment_no
reservoirs (1,n)
42
MADS Spatial Data Types: Type Hierarchy
Geo
partition
Simple s Complex
Geo Geo c
partition
Point PointBag
Surface
Line Surface LineBag
Bag
_ Spatial data types are topologically closed: all geometries include their boundary
43
Line
44
MADS Spatial Data Types (2)
Surface
_ Defined by 1 exterior boundary and 0 or more interior boundaries defining its holes
45
Surface/
Surface
46
Topological Predicates
_ Specify how two geometries relate to each other
_ Based on the definition of their boundary, interior, and exterior, denoted by I(x), B(x), and E(x)
_ Dim(x): maximum dimension (-1, 0, 1, or 2) of x, -1 corresponds to the dimension of the empty set
_ Dense notation use a string of 9 characters to represent the cells of the matrix
47
48
Topological Predicates (2)
Adjacent
_ The last example does not satisfies the predicate, their intersection is at the interior of both geometries
Touches
49
Overlaps
50
Topological Predicates (4)
Contains/Within
Disjoint/Intersects
_ a disjoint b ⇔ a ∩ b = ∅ ⇔ ¬a intersects b
Equals
_ a equals b ⇔ a ∩ b = a ∧ a ∩ b = b ⇔ (a − b) ∪ (b − a) = ∅
51
_ a covers b ⇔ a ∩ b = b ⇔ b − a = ∅
Encloses/Surrounded
_ Definition is quite involved, depends of whether a is a (set of) line(s) or a (set of) surface(s)
52
Spatial Objects
Netherlands
14.8
Ireland Denmark
3.6 5.1
Belgium
9.9
Britain
57.2 Germany
Luxembourg 78.5 Switzerland
6.3
Country 0.4
name Austria
population France 7.4
58
Italy
57.5
Portugal
10.5
Spain
39
53
5.1
Spatial Attributes
3.6 14.8
Client Country Road
57.2
name name name
address population 9.9 responsible
78.5
location capital stations (1,n)
rivers (1,n) 0.4
7.4
6.3
58 attributes
_ Both non-spatial and spatial object types can have spatial
_ Domain of spatial attribute: a spatial type (point, line, surface, . 57.5
. .)
_ Spatial attributes can be multi-valued
10.5 39
_ A spatial attribute of a spatial object type may induce a topological constraint
• The capital of a country is located within the geometry of its country
_ This is not necessarily the case
• A given country will Figure
keep either
1.3 the full projection:
Theme geometry aoftheme
the rivers flowing
of countries and through it of western
population
Europe
or these geometries will (a) and projection
be projected on the attribute
to the section flowingpopulation
through (b).
the country
• This depends on application semantics
_ The conceptual schema must explicitly state these topological constraints
54
Spatial Complex Attributes
Country
name
population
capital
name
location
provinces (1,n)
name
location
_ It is usual to keep both thematic (alphanumeric) and location data for attributes, e.g., capital
_ This will allow to print both the name and the location of capitals/rivers/roads/... in a map
_ However, in real maps the toponyms (names of objects appearing in a map) have also a location
• There are precise cartographic rules for placing them, this is a semi-automatic process
55
_ Has implications in the way of accessing the instances of the concept (e.g. the buildings)
• As spatial objects: the application can access a building one by one
• As spatial attributes: the access to a building must be made through the land plot containing it
56
Generalization: Inheriting Spatiality
Country Antenna
name owner
capital type
_ For simple inheritance it is not necessary to restate the geometry in the subtype (but see later)
57
Road Road
Land Plot Town 1/10 000 1/10 000
_ Refinement: restricts the inherited property, value remains the same in the supertype and the subtype
_ In redefinition and overloading an instance of the subtype has both a locally defined spatiality and the
inherited one
58
Generalization: Multiple Inheritance
Public
Building
Facility
Public
Building
_ Several policies have been proposed for solving this issue in the OO community
59
Spatial/Non-Spatial Relationships
Surveyor Avalanche
Observed
SSN (1,n) (1,n) cause
name damages
GeoLocalized
Monument
Photo
Depicts
number (1,n) (1,n) name
takenBy constructionDate
_ Spatiality of relationship types is orthogonal to the fact that linked object types are spatial
_ If a spatial relationship type relates spatial type(s), spatial constraints may restrict the geometries
60
Topological Relationships
Country River
Traversed
name (1,n) (1,n) name
capital length
61
Spatial Aggregation
isComposedOf isComponentOf
Country State
Composes
name (1,n) (1,1) name
capital population
_ Usually, aggregation has exclusive semantics (stated by cardinalities in the component role)
_ Usually, the spatiality of the aggregation is partitioned into the spatiality of the components
_ It is not the case for the second example, where the spatiality of Antena corresponds to its coverage
• The same location can be covered by several antennas
• Spatiality of the aggregation is the spatial union of the spatiality of the antennas
62
Space- and Time-Varying Attributes
63
Composes Composes
64
Spatial Databases: Topics
_ Introduction
_ Representative Systems
_ Summary
65
Representation Models
_ Representation of infinite point sets of the Euclidean space in a computer
66
Belgium Map: Vector
67
68
Belgium Map: Satellite
69
70
So far, spatial objects have been represented at a rather abstract level,
using primitives such as points or edges. We now study the practical
implementation of geometric information. To state it briefly, the dif-
ficulty to overcome is the representation of infinite point sets of the
Euclidean space in a computer.
There exist different representation modes to solve the problem, ei-
ther by approximating the continuous space by a discrete one (tessella-
tion mode) or by constructing appropriate data structures (vector mode
and half-plane representation). For instance, in a tessellation mode a
city is represented as a set of cells that cover the city’s interior, whereas
in the vector mode it will be represented as a list of points describing the
boundary of a polygon.
2.2.1 Tessellation
A cellular decomposition of the plane (usually, a grid) serves as a ba- 2.2 Representation Modes 39
sis for representing the geometry. The partitioning of the embedding
space into disjoint cells defines a discrete model, sometimes called spatial
resolution model, tiling, or meshes in the field of computer graphics.
Second, there is no apparent distinction between a polyline structure
Raster Model: Tesselation
The approach can be further divided into fixed (or regular) and vari-
and a polygon structure. It is up to the software that manipulates ge-
able (or irregular) tessellation modes. A fixed representation model uses ometric data to interpret properly the structure, and to check that the
a regular grid or raster, which is a collection of polygonal units of equal representation is valid; that is, to verify that the polyline is closed for
_ Decomposition of the plane into polygonal units
size. A variable spatial resolution model handles units of decomposition the polygon.
of various sizes. The size of the units may also change according to the This remark holds for other possible constraints on the polygon type,
level of resolution. Figure 2.3 depicts two cases of regular tessellation,
36 such convexity.
C as 2 Representation
Neither can we guarantee
of Spatial that a polygon is simple, in
Objects
_ May be regular or irregular, depending on whether the polygonal units are of equal size that the foregoing data structure does not prevent two nonconsecutive
edges to intersect. The structure is unfortunately not powerful enough
to ensure the correctness of the representation (i.e., the satisfiability of
such constraints).
The same situation holds for regions. Again, the structure is rel-
atively permissive. A polygon can be contained in another, and two
polygons can be adjacent, overlapping, or disconnected.
Figure 2.6 shows the representation of polygon P in vector mode.
It is described by an ordered list of pairs of coordinates, such as the
(a) (b) (a) (b)
following:
Figure 2.3
Regular
Regular tessellations: grid squares (a) and hexagonal cells (b). Figure 2.4 Irregular
Irregular tessellations: cadastral zones (a) and Thiessen poly-
gons (b). < [4, 4], [6, 1], [3, 0], [0, 2], [2, 2] >
_ Regular tesselation used for remote sensing data (e.g.,Figure
satellite
one with showsimages)
2.7square cells and one
examples with hexagonal
of polylines cells.vector
using this Figure 2.4 shows
represen-
two For
tation. examples of irregular
the sake tessellations.
of simplicity, we use aThe first different
slightly (Figure 2.4a) consid-
notation,
ers a cadastral
referred
_ Irregular tesselation used for zoning in social, demographic
second
integers
orofpartitioning
to as “vertex
(Figure
instead
notation”ofinthe
economic
2.4b)
givingshows
theplane
data into various
following.
theiracoordinates,
partitioningasinto
zones,
We index
ThiessenWe
previously.
whereas
vertices withthe
can see3
polygons.
In a raster
the following representation,
in Figure 2.7. the rectangular 2D space is partitioned
_ A spatial object is represented by the smallest subset of
intopixels thatofcontains
a finite number itUsually the space is decom-
elementary cells.
posed according to a regular 2D grid of N × M rectangular cells,
2.2 Representation Modes 37
whose sides are parallel to the space coordinate axes. The cells are called
y
pixels. A pixel has an address in the plane, which usually is a pair (x, y)
5 where x ≤ N is the column of the cell in the grid, and y ≤ M is
12 13 14
the row. (4,4)
z
y y
y
z x z x x
(a) (b) (c)
Figure
_ No assumption is made on the distribution and location of the vertices of2.9theProgression
triangles of a triangulated
sample (a), triangulation (b), and TIN (c).
irregular network (TIN): point
y
z x x
72
(b) (c)
y
Abstract Data Types 77 3.3 Spatial Abstract Data Types
Nodes Nodes
Topological Spaghetti
Spaghetti Topological
Topological Spaghetti
Spatial data model a1 a4 Spatial data model
Node
Type support with topological or spaghetti structures. n1Figure 3.4 Type support with topological or spaghetti structures.
a2
a3
Arc Node
eration implementation depends on the representation and that the operation implementation depends on the representa
t choice provides a better support to topological operations.Network that the first choice provides a better support to topological op
instance, whether o 2 is adjacent to o 3 is easily answered, Asking, Topological
_ Three comonly used representations: Spaghetti, Network, for instance, whether o 2 is adjacent to o 3 is easily a
equires a complex, costly, anddiffer
_ Mainly lessinrobust computation
the expression with relationships
of topological whereas itamong
requires a complex,
the component costly, and less robust computat
objects
ructures. spaghetti structures.
73
depicts a (non-exhaustive) set of possible choices for geomet- Figure 3.5 depicts a (non-exhaustive) set of possible choices for
here is a trade-off between the modeling power captured by ric types. There is a trade-off between the modeling power cap
and the constraints imposed on the chosen representation. a definition and the constraints imposed on the chosen repres
Spaghetti Model
_ Geometry of any object described independently of other objects
_ No topology is stored in the model, all topological relationships must be computed on demand
_ Advantages
• Simplicity
• Provides the end user with easy input of new objects into the collection
(e) _ Drawbacks(f ) (d) (e) (f )
Candidates for geometric • types:
Lack ofpoints
explicity
(a),information about
polyline (b), topological relationships among spatial objects
complex Figure 3.5 Candidates for geometric types: points (a), polyline (b)
polygon (d), polygon set (e), andsharing
• No mixed of
(f ).information ⇒ redundancy, problem
polyline (c),large
with polygon (d), inconsistency
data sets, polygon set (e), and mixed (f ).
74
Network Model
_ Destined for network (graph)-based applications
• transportation services, utility management (electricity, telephone, . . .)
_ Nodes allow efficent line connectivity tests and network computations (e.g., shortest paths)
_ Planar network: each edge intersection is recorded as a node, even if does not correspond to a real-
world entity
75
Topological Model
_ Similar to the network model, except that the network is plannar
_ Induces a planar subdivision into adjacent polygons, some of which may not correspond to actual
geographic objects
_ Node: represented by a point and the (possibly empty) list of arcs starting/ending at it
• Isolated point: identifies location of point features such as towers, point of interest, . . .
_ Arc: features its ending points, list of vertices and two polygons having the arc as common boundary
_ Polygon: represented by a list of arcs, each arc being shared with a neighbor polygon
_ Drawbacks: Some database objects have no semantics in real-world, complexity of the structure may
slow down some operations
76
Spatial Databases: Topics
_ Introduction
y SQL/MM
_ Representative Systems
_ Summary
77
ST_Curve ST_Polyhedral
Polygon Surface
ST_Polygon ST_TIN
ST_Triangle
ST_Multi ST_Multi
LineString Polygon
78
ST Geometry
_ Represent 0D, 1D, and 2D geometries that exist in 2D (R2 ), 3D (R3 ) or 4D coordinate space (R4 )
_ The m coordinate of a point representing arbitrary measurement: key to supporting linear networking
applications such as street routing, transportation, pipeline, . . .
_ All locations in a geometry are in the same spatial reference system (SRS)
_ Geometric calculations are done in the SRS of the first geometry in the parameter list of a routine
_ If a routine returns a geometry or measurement (e.g., length or area), the value is in the SRS of the
first geometry in the parameter list
79
_ ST_SRID: observes and mutates the spatial reference system identifier of a geometry
80
Methods on ST Geometry: Metadata (2)
_ ST_IsSimple: tests if a geometry has no anomalous geometric points
LineString MultilineString
Polygons
81
p p
q
q
_ ST_Buffer: returns the geometry that represents all points whose distance from any point of a geom-
etry is less than or equal to a specified distance
82
Methods on ST Geometry: Spatial Analysis (2)
_ ST_Union: returns the geometry that represents the point set union of two geometries
_ ST_Intersection: returns the geometry that represents the point set intersection of two geometries
_ ST_Difference: returns the geometry that represents the point set difference of two geometries
_ ST_SymDifference: returns the geometry that represents the point set symmetric difference of two
geometries
83
_ ST_AsText: returns the well-known text representation for the specified geometry
_ ST_WKBToSQL: returns the geometry for the specified well-known binary representation
_ ST_AsBinary: returns the well-known binary representation for the specified geometry
_ ST_GeomFromWKB: returns a geometry, which is transformed from a BINARY LARGE OBJECT value
that represents its well-known binary representation
84
Boundary, Interior, Exterior
_ Boundary of a geometry: set of geometries of the next lower dimension
• ST_Point or ST_MultiPoint value: empty set
• ST_Curve: start and end ST_Point values if nonclosed, empty set if closed
• ST_MultiCurve: ST_Point values that are in the boundaries of an odd number of its element
ST_Curve values
• ST_Polygon value: its set of linear rings
• ST_MultiPolygon value: set of linear rings of its ST_Polygon values
• Arbitrary collection of geometries whose interiors are disjoint: geometries drawn from the bound-
aries of the element geometries by application of the mod 2 union rule
• The domain of geometries considered consists of those values that are topologically closed
_ Interior of a geometry: points that are left when the boundary points are removed
85
Spatial Relationships
_ ST_Equals: tests if a geometry is spatially equal to another geometry
_ ST_Relate: tests if a geometry is spatially related to another geometry by testing for intersections
between their interior, boundary and exterior as specified by the intersection matrix
• a.ST_Disjoint(b) ⇔ (I(a) ∩ I(b) = ∅) ∧ (I(a) ∩ B(b) = ∅) ∧
(B(a) ∩ I(b) = ∅) ∧ (B(a) ∩ B(b) = ∅) ⇔ a.ST_Relate(b,’FF*FF****’)
86
Conceptual Schema: Example
Highway
Country
highway_code
country_code highway_name
country_name highway_type
Composes Composes
87
88
Reference Schemas (2)
89
90
Reference Queries: Alphanumerical Criteria (1)
_ Number of inhabitants in the county of San Francisco
select population
from County
where county_name = ’San Francisco’
91
92
Reference Queries: Spatial Criteria (1)
_ Counties adjacent to the county of San Francisco in the same state
select c1.county_name
from County c1, County c2
where c2.county_name = ’San Francisco’
and c1.state_code = c2.state_code
and ST_Touches(c1.geometry, c2.geometry)
_ Display of the State of California (supposing that the State table is no spatial)
select ST_Union(c.geometry)
from County c, State s
where s.state_code = c.state_code
and s.state_name = ’California’
93
_ Length of Interstate 99
select sum (ST_Length(s.geometry))
from Highway h1, HighwaySection h2, Section s
where h1.highway_name = ’I99’
and h1.highway_code = h2.highway_code
and h2.section_code = s.section_code
94
Reference Queries: Spatial Criteria (3)
_ All highways going through the State of California
select distinct h1.highway_name
from State s1, Highway h1, HighwaySection h2, Section s2
where s1.state_name = ’California’
and h1.highway_code = h2.highway_code
and h2.section_code = s2.section_code
and ST_Overlaps(s2.geometry, s1.geometry)
_ Display of all residential areas in the county of San Jose
select ST_Intersection(l.geometry, c.geometry)
from County c, LandUse l
where c.county_name = ’San Jose’
and l.land_use_type = ’residential area’
and ST_Overlaps(l.geometry, c.geometry)
_ Overlay the map of administrative units and land use
select county_name, land_use_type, ST_Intersection(c.geometry, l.geometry)
from County c, LandUse l
where ST_Overlaps(c.geometry, l.geometry)
95
_ Part of counties that are within a given rectangle on the screen (clipping)
select ST_Intersection(geometry, @rectangle)
from County
where ST_Overlaps(geometry, @rectangle)
96
Reference Queries: Interactive Queries (2)
_ Description of the highway section pointed to on the screen
select section_name, number_lanes
from Section
where ST_Contains(geometry, @point)
97
SQL/MM: Conclusion
_ SQL/MM provides a standard way to declare and manipulate geometries
_ These methods can be combined in SQL queries and programs with standard ones
98
Spatial Databases: Topics
_ Introduction
_ SQL/MM
_ Representative Systems
y Oracle
_ Summary
99
Oracle Locator
_ Included in all editions of the database
_ Geometric queries
_ Proximity search
_ Distance calculation
_ Multiple projections
_ Conversion of projections
100
Oracle Spatial
_ Advanced functions: Option of Oracle Database Enterprise Edition
_ = Locator + ...
• Geometric transformations
• Spatial aggregations
• Dynamic segmentation
• Measures
• Network modeling
• Topology
• Raster
• Geocoder
• Spatial Data Mining
• 3D Types (LIDAR, TINS)
• Web Services (WFS, CSW, OpenLS)
101
102
Oracle Topological Model
103
104
Oracle Geocoding
105
Oracle MapViewer
106
Oracle: Geometry Type
_ Creation of spatial tables
CREATE TABLE Cells (
Cell_id NUMBER,
Cell_name VARCHAR2(32),
Cell_type NUMBER,
Location SDO_GEOMETRY,
Covered_area SDO_GEOMETRY);
107
Point Line string Arc line string Compound line string Self-crossing
line string
not valid
108
Geometrical Primitives: Points
_ Points (X1, Y1)
_ 2, 3, or 4 dimensions
109
110
Geometrical Primitives: Polygones
111
112
Oracle: Element
113
Oracle: Geometry
114
Oracle: Layer
115
116
SDO GTYPE
117
SDO SRID
118
SDO POINT
_ Object type SDO_POINT_TYPE
x NUMBER
y NUMBER
z NUMBER
_ Example of use
INSERT INTO TELEPHONE_POLES (col-1, ..., col-n, geom)
VALUES (attribute-1, ..., attribute-n,
SDO_GEOMETRY (
2001, 8307,
SDO_POINT_TYPE (-75.2,43.7,null),
null, null)
);
119
SDO ORDINATES
_ Object type SDO_ORDINATE_ARRAY
VARRAY (1048576) OF NUMBER
120
SDO ELEM INFO
_ Object type SDO_ELEM_INFO_ARRAY
VARRAY (1048576) OF NUMBER
121
122
Line Examples
123
124
Polygon Examples (2)
125
126
Multi-Polygon and Polygon with Hole Examples
127
Constructing a Line
INSERT INTO LINES (col-1, ..., col-n, geom) VALUES (
attribute_1, ..., attribute_n,
SDO_GEOMETRY (
2002, 8307, null,
SDO_ELEM_INFO_ARRAY (1,2,1),
SDO_ORDINATE_ARRAY (
10,10, 20,25, 30,10, 40,10))
);
128
Metadata
_ Defines the boundaries of a layer
• Minimum and maximum coordinates for each dimension
129
Constructing Geometries
_ Standard constructor
INSERT INTO TELEPHONE_POLES (col-1, ..., col-n, geom)
VALUES (attribute-1, ..., attribute-n,
SDO_GEOMETRY (
2001, 8307,
SDO_POINT_TYPE (-75.2,43.7,null),
null, null)
);
_ Well-known Text (WKT) constructor
INSERT INTO TELEPHONE_POLES (col-1, ..., col-n, geom)
VALUES (attribute-1, ..., attribute-n,
SDO_GEOMETRY (’POINT (-75.2 43.7)’,8307)
);
_ Well-known Binary (WKB) constructor
INSERT INTO TELEPHONE_POLES (col-1, ..., col-n, geom)
VALUES (attribute-1, ..., attribute-n,
SDO_GEOMETRY (:my_blob,8307)
);
130
Geometry Extraction: WKT Format
SELECT c.geom.get_wkt()
FROM us_counties c
WHERE county = ’Denver’;
POLYGON (
(-105.052597 39.791199, -105.064606 39.789928, ...
... -105.024757 39.790947,-105.052597 39.791199),
(-104.933578 39.698139, -104.936104 39.698299, ...
... -104.9338 39.696701, -104.933578 39.698139))
131
132
Geometry Extraction: GML Format
<gml:Point srsName="SDO:8307
xmlns:gml="http://www.opengis.net/gml">
<gml:coordinates decimal="." cs="," ts=" ">
-104.872655,39.768035
</gml:coordinates>
</gml:Point>
133
SELECT xmlelement(
"City",
xmlattributes(
’http://www.opengis.net/gml’ as "xmlns:gml"),
xmlforest(
city as "Name",
pop90 as "Population",
xmltype( sdo_util.to_gmlgeometry(location) )
as "gml:geometryProperty")
) AS theXMLElements
FROM us_cities
WHERE state_abrv = (’CO’);
134
Generation of XML documents: XMLForest (2)
<City xmlns:gml="http://www.opengis.net/gml">
<Name>Denver</Name>
<Population>467610</Population>
<gml:geometryProperty><gml:Point srsName="SDO:8307"
xmlns:gml="http://www.opengis.net/gml">
<gml:coordinates decimal="." cs="," ts=" ">
-104.872655,39.768035 </gml:coordinates>
</gml:Point></gml:geometryProperty>
</City>
...
<City xmlns:gml="http://www.opengis.net/gml">
<Name>Lakewood</Name>
<Population>126481</Population>
<gml:geometryProperty><gml:Point srsName="SDO:8307"
xmlns:gml="http://www.opengis.net/gml">
<gml:coordinates decimal="." cs="," ts=" ">
-105.113556,39.6952 </gml:coordinates>
</gml:Point></gml:geometryProperty>
</City>
135
_ Class JGeometry
136
Reading Geometries
137
138
Extracting Information from Geometries (2)
// Point
double gPoint[] = geom.getPoint();
// Element info array
int gElemInfo[] = geom.getElemInfo();
// Ordinates array
double gOrdinates[] = geom.getOrdinatesArray();
// First and last point
double[] gFirstPoint = geom.getFirstPoint();
double[] gLastPoint = geom.getLastPoint();
// MBR
double[] gMBR = geom.getMBR();
// Java Shape
Shape gShape = geom.createShape();
139
// Point
JGeometry geom = new JGeometry(10,5, 8307);
// Point (3D)
JGeometry geom = new JGeometry(10,5,3, 8307);
// Rectangle
JGeometry geom = new JGeometry(10,135, 20,140, 8307);
// Any geometry (compound linestring)
JGeometry geom = new JGeometry(
2002,8307,
new int[] {1,4,3, 1,2,1, 3,2,2, 7,2,1},
new double[] {10,45, 20,45, 23,48, 20,51, 10,51});
140
Constructing Geometries (2)
// Point
JGeometry geom = JGeometry.createPoint(
new double[] {10,5}, 2, 8307);
// Linestring
JGeometry geom = JGeometry.createLinearLineString(
new double[] {10,25, 20,30, 25,25, 30,30}, 2, 8307);
// Simple polygon
JGeometry geom = JGeometry.createLinearPolygon(
new double[] {10,105, 15,105, 20,110, 10,110, 10,105}, 2, 8307);
// Polygon with voids
JGeometry geom = JGeometry.createLinearPolygon(
new double[][] {{50,105, 55,105, 60,110, 50,110, 50,105},
{52,106, 54,106, 54,108, 52,108, 52,106}}, 2, 8307);
141
// Multi-point
JGeometry geom = JGeometry.createMultiPoint(
new double[][] {{50,5}, {55,7}, {60,5}}, 2, 8307);
// Multi-linestring
JGeometry geom = JGeometry.createLinearMultiLineString(
new double[][] {{50,15, 55,15}, {60,15, 65,15}}, 2, 8307);
// Circle (using 3 points on the circumference)
geom = JGeometry.createCircle(15,145, 10,150, 20,150, 8307);
// Circle (using a center point and radius)
geom = JGeometry.createCircle(10,150, 5, 8307);
142
Writing Geometries
143
Spatial Indexes
_ R-tree indexing
• Tree rectangles (MBR)
• Indexing in 2 or 3 dimensions
_ Quad-tree indexing
• Use of a regular grid
• No longer documented from 10g
• Do not use, unless exceptions ...
_ A spatial index must exist before we can ask spa-
tial queries on a table!
144
R-Tree Index
145
_ Identifying the SDO_INDEX_TABLE that stores the spatial index on the customers table
SELECT SDO_INDEX_TABLE FROM USER_SDO_INDEX_INFO
WHERE TABLE_NAME = ’CUSTOMERS’ AND COLUMN_NAME=’LOCATION’;
SDO_INDEX_TABLE
--------------------------------
MDRT_D81F$
_ Index deletion
DROP INDEX <index_name>;
146
How to Find Information about Spatial Indexes
_ USER_INDEXES
• INDEX_TYPE = ’DOMAIN’ and ITYP_NAME = ’SPATIAL_INDEX’
_ USER_SDO_INDEX_INFO
• Column SDO_INDEX_TABLE identifies the physical table containing the index proprement: table
whose name is of the form MDRT_xxxx$
_ USER_SDO_INDEX_METADATA
• As above, but with more details: number of nodes, node size, height of the index tree, etc.
147
148
Optimized Query Execution Model
149
150
Topological Predicates
_ Select objects by their topological relationship with another object
• SDO_INSIDE
• SDO_CONTAINS
• SDO_COVERS
• SDO_COVEREDBY
• SDO_OVERLAPS
• SDO_TOUCH
• SDO_EQUAL
• SDO_ANYINTERACT
_ Example
WHERE SDO_INSIDE ( <geometry-1>, <geometry-2> ) = ’TRUE’
151
_ Or a combination: ’INSIDE+COVEREDBY’
152
Topological Operators
153
Example Queries
_ Which parks are entirely contained in the state of Wyoming?
SELECT p.name
FROM us_parks p, us_states s
WHERE s.state = ’Wyoming’
AND SDO_INSIDE (p.geom, s.geom) = ’TRUE’;
_ Equivalent to
AND SDO_RELATE(p.geom,s.geom,’MASK=INSIDE’) = ’TRUE’;
154
Example Queries
_ In which competing jurisdictions is my client?
SELECT s.id, s.name
FROM customers c, competitors_sales_regions s
WHERE c.id = 5514 AND SDO_CONTAINS (s.geom, c.location) = ’TRUE’;
155
156
Queries Based on Distance
_ Select objects according to distance from another object
_ Operator SDO_WITHIN_DISTANCE
SDO_WITHIN_DISTANCE(
<geometry-1>, <geometry-2>,
’DISTANCE=distance UNIT=unit’ ) = ’TRUE
_ If no unit is specified, the distance is expressed in the unit of the coordinate system (if projected)
157
158
Examples of Research on Distance (2)
_ How many customers in each category are located within 1/4 mile of my office number 77?
SELECT customer_grade, COUNT(*)
FROM branches b, customers c
WHERE b.id=77
AND SDO_WITHIN_DISTANCE (
c.location, b.location,
’DISTANCE=0.25 UNIT=MILE’)=’TRUE’
GROUP BY customer_grade;
159
160
Example of Proximity Search
_ What is the nearest office to this client?
161
162
Classifying Results by Distance
_ What are my five customers closest to this competitor?
SELECT c.id, c.name, c.customer_grade,
SDO_NN_DISTANCE(1) distance
FROM competitors co, customers c
WHERE co.id=1
AND SDO_NN (
c.location, co.location,
’SDO_NUM_RES=5’, 1)=’TRUE’
ORDER BY distance;
163
_ The 5 closest customers are read, and then those of type ’GOLD’ are selected
164
Additional Selection Criteria: Correct Query
_ What are my five ’GOLD’ clients closest to this competitor?
SELECT c.id, c.name, c.customer_grade
FROM competitors co, customers c
WHERE co.id=1
AND SDO_NN (
c.location, co.location)=’TRUE’
AND c.customer_grade = ’GOLD’
AND ROWNUM <= 5;
165
166
SDO JOIN Function
SDO_JOIN( table_name-1, column_name-1,
table_name-2, column_name-2
[,’parameters’] [, preserve_join_order])
RETURN SDO_ROWIDSET;
167
168
SDO JOIN Function Example: Distance Predicate
_ Find all gold customers who are less than 500 meters from one of our branches in San Francisco
SELECT DISTINCT c.id, c.name, b.id
FROM customers c,
branches b,
TABLE(SDO_JOIN(
’CUSTOMERS’, ’LOCATION’,
’BRANCHES’, ’LOCATION’,
’DISTANCE=500 UNIT=METER’)) j
WHERE j.rowid1 = c.rowid
AND j.rowid2 = b.rowid
AND c.customer_grade = ’GOLD’
AND b.city = ’SAN FRANCISCO’;
169
Spatial Functions
170
Calculations: Length, Area and Distance
171
Calculations: Examples
_ What is the total area of Yellowstone National Park?
SELECT sdo_geom.sdo_area(geom,0.005,’unit=sq_km’)
FROM us_parks
WHERE name = ’Yellowstone NP’;
172
Generating Objects
_ SDO_BUFFER(g,size): Generates a buffer size chosen
• The dimension (size) can be negative for an internal buffer
173
Combining Objects
_ SDO_DIFFERENCE(g1,g2): Produces an object that represents the geometric difference of the two
given objects
_ SDO_XOR(g1,g2): Produces an object that represents the symmetric difference of the two given ob-
jects
• Equivalent to the union minus the intersection
174
Combining Objects: Example
_ What is the area occupied by the Yellowstone Park in the state it occupies?
SELECT s.state, sdo_geom.sdo_area (
sdo_geom.sdo_intersection (s.geom, p.geom, 0.5),
0.5, ’unit=sq_km’) area
FROM us_states s, us_parks p
WHERE SDO_ANYINTERACT (s.geom, p.geom) = ’TRUE’
AND p.name = ’Yellowstone NP’;
STATE AREA
-------------------------- ----------
Wyoming 8100.75346
Montana 640.295989
Idaho 154.659879;
175
Spatial Aggregation
176
Aggregation Examples
_ Find the focal point of all our customers in Daly City
SELECT SDO_AGGR_CENTROID(SDOAGGRTYPE(location,0.5)) center
FROM customers
WHERE city = ’DALY CITY’;
_ Calculate the number of customers in each zip code, and calculate the focal point for these clients
SELECT COUNT(*), postal_code,
SDO_AGGR_CENTROID(SDOAGGRTYPE(location,0.5)) center
FROM customers
GROUP BY postal_code;
177
_ Since 1990s, major DBMS have been extended with spatial capabilities
• Oracle Spatial was first introduced into version 8g
• Open source implementations exist for many years
• SQL Server introduced spatial support in version 2008
_ Since the 1990s the Open Geo Spatial Consortium (OGC) has been working on defining standards
• These standards are also international standards (ISO)
178