SpatialQueries
SpatialQueries
Spatial Queries
Table Of Content
1. Spatial Queries ......................................................................................................................................... 2
1.1. Introduction to spatial queries .......................................................................................................... 3
1.1.1. The information system ............................................................................................................. 3
1.1.2. The basic components of geographical information .................................................................. 4
1.1.3. Query classification ................................................................................................................... 6
1.1.4. Input / Output of a query .......................................................................................................... 9
1.1.5. Questions .................................................................................................................................. 12
1.2. Thematic query ............................................................................................................................... 13
1.2.1. Relational operators ................................................................................................................. 15
1.2.2. Arithmetic operators ................................................................................................................ 16
1.2.3. Logical operators ..................................................................................................................... 17
1.2.4. Combination of operators ........................................................................................................ 20
1.2.5. Formulate queries .................................................................................................................... 22
1.3. Geometric query ............................................................................................................................. 24
1.3.1. The geometric primitives ......................................................................................................... 24
1.3.2. Geometric measurement functions .......................................................................................... 26
1.4. Topological query ........................................................................................................................... 35
1.4.1. Topological relations ............................................................................................................... 35
1.4.2. Topological operators .............................................................................................................. 40
1.5. Summary ......................................................................................................................................... 44
1.6. Glossary .......................................................................................................................................... 45
1.7. Bibliography ................................................................................................................................... 46
1. Spatial Queries
PLEASE NOTE: this lesson has been translated from German to English. However, some of the figures are
still in German.
The aim of spatial queries and their analysis is to detect spatial relationships between elements of one or more
subjects, in order to locate spatial objects. The results of such analysis can be used in decision making. This
lesson is divided into four units. An introduction to the subject and the most important terms are provided in
the first unit. These terms will be discussed in more detail in the other units.
Learning Objectives
• You know the objects of queries and are able to establish a connection between database structure and
query possibilities.
• You are able to formulate a thematic query and to execute a simple and a complex query.
• You understand the basics of a geometric query and know how to perform the most important queries.
• You know the topological relationships between the objects and are able to formulate a topological query.
Citation:
"The deduction of new information from existing spatial data is one of the main tasks of a geoinformation
system.
Spatial analysis comprises analysis and synthesis of spatial data to a unity [...] Every spatial analysis implies
the professional interpretation of the resuslts." (Bill 1999)
Example of a query:
"Which proportion of the inhabitants of Zürich lives more than 200m away from a public transport stop?"
1
Gewährleisten der Widerspruchsfreiheit innerhalb einer Datenbank; d. h., dass der Inhalt einer Datenbank alle vordefinierten
Konsistenzbedingungen ("Constraints") erfüllt.
Example 1:
Example 2:
Subquery
Queries can be performed on the entire data set or a subset of the data. Subsets are generated via
queries. A subquery is a SELECT statement that is incorporated in a SELECT, SELECT...INTO ,
INSERT...INTO , DELETE , UPDATE statement or in another subquery. A subquery is composed of
three parts.
Part Description
Comparison predicate and a comparison operator that compares the
predicate with the result of the subquery.
Expression An expression that is searched for in the result of the
subquery.
SQL statement A SELECT statement according to the usual format of
the SELECT statement. It must be in parentheses.
Example:
SELECT * FROM product WHERE product-ID IN (SELECT product-ID FROM
orders WHERE discount >= .25);
2
"Acronym for Structured Query Language. A syntax for retrieving and manipulating data from a relational database. SQL has
become an industry standard query language in most relational database management systems" , e.g. Oracle, DB2, Access, etc.
between the data involved in the query are given via the topology 3 (geometry) or the tables (theme). Tables
can also be related to each other. The content of the tables can be linked via key attributes. Thus, a query can
be performed over several linked tables.
The result of a query can be presented in different forms. The result should be presented in a form which is
easy for the user to read. Normally, the results are presented as maps, tables, or figures, or in another format
which allows data sharing. The following are examples of data presentation:
1. Digital data transfer
2. Interactive graphics on screen
3. Tables, reports, and similar representations
4. Passive graphics in form of maps
3
Die Topologie beschäftigt sich mit den räumlichen und strukturellen Eigenschaften der geometrischen Objekte unabhängig von ihrer
Ausdehnung und ihrer Form. Die topologischen Eigenschaften äussern sich in Beziehung der Nachbarschaft, des Enthaltenseins, der
Überschneidung und Ähnlichem.
Some examples
Layer 1 Layer 2 Inputs Query Type Res. table Res. graphic
Find the THEM
buildings,
which have
an area of
more than
100m2.
Exercise
What does the spatial information in a GIS mainly consist of?
1.1.5. Questions
Question 1
How can space related information be divided?
Question 2
Which approaches can be used to formulate a query?
Question 3
Describe the inputs which should be used to answer the following questions and what would the outputs
look like:
"Find all buildings which are located on parcels with a minimal area of 1000 m2 and a distance of more than
250m to the highway".
Query language
In a GIS, queries are performed either with SQL or internal query languages. In a standard GIS the commands
are directly entered; in contrast, in a desktop GIS, the commands have to be selected using a dialogue system.
The functionalities of SQL are explained in the "Database Management and Systems Module" (lesson 4:
Structured Query Language SQL).
In the following examples, some user#friendly interfaces for SQL formulations of thematic queries are shown.
With a few clicks the desired SQL commands are compiled. Behind the interface, the SQL syntax is used to
query the database.
Example 1: ArcView Interface
4
Die Abfrage ermittelt räumliche Beziehungen zwischen Elementen eines oder mehrerer Themen, um auf dieser Basis eine Lokalisierung
von Objekten zu erreichen. Die Analyseergebnisse können dann bei konkreten Fragestellungen zur Entscheidungsfindung beitragen.
Query operators
A particular feature that distinguishes a GIS is the possibility to query specific thematic information about
selected objects. The thematic query relies on the analysis of technical data (attribute data). The query is
performed using adequate selecting operators. In the following, three categories of operators 5 are presented:
• Relational operators: Besides the equal sign, relational operators can be used to formulate queries as well.
• Arithmetic operators: These operators are used for numeric attributes. E.g. there is the possibility to
calculate the mean of an attribute or the sum of attribute values from a series of objects.
• Logic operators: Conditions are formulated with logic operators. The semantics (meaning) of these
operators are similar to the meaning "AND", "OR" etc.
5
In search algorithms, operators enable the logical conjunction of search items using keywords like AND, OR, and NOT.
SQL provides the following additional functions for analysis (aggregate functions):
Graphic
Table ''Parcels: tree species (Baumart), stock (Vorrat), and soil type (Bodentyp)''
Graphic
Table ''Parcels: tree species (Baumart), stock (Vorrat), and soil type (Bodentyp)''
To make such queries understandable, Venn diagrams are used. Have a look at the previous table and the
following explanation.
The circles number 1 and 2 graphically represent two conditions: the shaded area represents the true statement,
while the part outside the circle does not correspond to a result.
To explain this situation, the above example is used.
Some examples
In the following, it is shown for each operator how SQL#queries are formulated and how the results are
presented.
INPUT
Graphic
Table
Example 1:
Operator Query SQL
AND Find all parcels that are forested with larch and select ParzelleID, Baumart,
where the stock is greater than 110m3/ha. Vorrat
from Parzelle
where Baumart = "Lärche"and
Vorrat > 110
Example 2:
Operator Query SQL
OR Find all parcels that are forested with larch or select ParzelleID, Baumart,
where the stock is greater than 110m3/ha. Vorrat
from Parzelle
where Baumart = "Lärche"
or Vorrat > 110
Example 3:
Operator Query SQL
XOR Find all parcels that are forested with larch or select ParzelleID, Baumart,
where the stock is greater than 110m3/ha, but Vorrat
which do not meet both of these conditions. from Parzelle
where Baumart = "Lärche"
xor Vorrat > 110
Example 4:
Operator Query SQL
NOT Find all parcels which are forested with larch but select ParzelleID, Baumart,
where the stock is not greater than 110m3/ha. Vorrat
from Parzelle
where Baumart = "Lärche"
not Vorrat > 110
Nested Queries
1 AND (3 OR 2) Select *
from Parcel
where Tree species = "Larch"
and (Density > 80% or Stock > 110 m3/ha)
(2 OR 1) NOT 3 Select *
from Parcel
where (Stock > 110 m3/ha or Tree species =
"Larch")
not Density > 80%
3 OR (2 XOR 1) Select *
from Parcel
where Density > 80% or (Stock > 110 m3/ha
xor Tree species = "Larch")
Applications
Try to solve the following exercises. Consider, in particular, which operator is used:
Select all the roads of the type "Nebenstrasse", where the speed is limited to 50km/h (select the objects by
clicking on the rows of the table).
Only pictures can be viewed in this version! For Flash, animations, movies etc. see online version.
Only screenshots of animations will be displayed. [link]
Select all the roads (all road types), where the speed is limited to 50km/h (select the objects by clicking on
the rows of the table).
Only pictures can be viewed in this version! For Flash, animations, movies etc. see online version.
Only screenshots of animations will be displayed. [link]
Select all the roads, where the speed is limited to 50km/h and which are not "Nebenstrassen" that have no
limited velocity of 50km/h. Select also all the roads which are "Nebenstrasse" and which have no speed limit
of 50km/h (select the objects by clicking on the rows of the table).
Only pictures can be viewed in this version! For Flash, animations, movies etc. see online version.
Only screenshots of animations will be displayed. [link]
Select all the roads, where the speed is limited to 50km/h, but which are not "Nebenstrassen" (select the objects
by clicking on the rows of the table).
Only pictures can be viewed in this version! For Flash, animations, movies etc. see online version.
Only screenshots of animations will be displayed. [link]
Parzelle TABLE
Besitzer TABLE
Preis TABLE
Point
A 2D point is defined by the x# and y#coordinates.
Line
Line segments consist of one or more point pairs. Two points of a segment can be connected by a straight line
or an arc. This means that lines can be made up of straight lines, curves, or a mixture of the two.
Polygon
Polygons (areas) are composed of connected lines that form a closed geometric shape. The enclosed area is
the polygon.
Complex geometries can be modeled as ordered sequences of the geometric primitives.
DESCRIPTION GRAPHIC TABLE
Point
Position, no area.
Defined by the coordinates.
Line
Length, no width.
Defined by several segments,
which always connect two points.
Area
Area and perimeter.
Defined by several segments,
which form a closed polygon.
In RASTER MODELS, all values are stored in a simple array (matrix). In addition, there is a file header,
which contains the following information:
• Number of rows and columns
• Cell size
• Minimum value of x# and y#coordinates
For example:
Ncols 270
Nrows 476
Xcorner 708152.60
Ycorner 121673.90
Cellsize 1
NODATA_Value -9999
DESCRIPTION GRAPHIC TABLE
Point
One cell
Line
Several neighboring cells in which
normally one cell is connected at
the edges and corners with just one
or two neighboring cells.
Area
Group of neighboring cells
connected at the edges and corners.
Vector model
Returns the position of each point from the map as x# and y#coordinates.
Raster model
Example: Block encoding
Value No. Cell Location
1 8 4,2 5,2 6,2
4,3 5,3
4,4 5,4
5,5
2 7 7,2
6,3 7,3
6,4 7,4
6,5 7,5
Distance
Vector model
For vector data, the distance between two objects is calculated according to the Pythagorean Theorem and
corresponds to the shortest distance.
Euclidean distance
Raster model
In the raster model, there are three different approaches to measure the distance between points.
Straight line between point A and point B
Beispiel:
Mit einer Auflösung von 2 cm beträgt die Distanz 11,3 cm.
Euclidean distance
Manhattan distance
Neighborhood
Practical examples
WHERE
In ArcInfo "Measuring Where" returns the x, y position of a selected point on a map.
LENGTH
In ArcInfo "Length" returns the length of a line section, which consists of two or more points.
Question 2:
Select all the trees that are less than 200m away from the collection point.
Size
PERIMETER
Sum of the length of all single sections.
AREA
Sum of the area of the simple geometric
shapes (e.g. triangles and rectangles), into
which the main object can be devided.
PERIMETER
Number of cell edges, which delimit the object, multiplied by the resolution of the
cells.
Example:
Using a cell resolution of 2cm, the perimeter is 32 cm.
AREA
Number of cells, which define the object, mutliplied by the area of one cell..
Example:
Using a cell resolution of 2cm, the area is 60 cm2.
Line buffer
Polygon buffer
Applications
Question 2:
Calculation of the flooded area
Vector data model
Question 3:
Flooding zone of a lake
Vector data model
Boundary
The boundary consists of points or lines that separate the interior from the exterior. The edge of a line consists
of the endpoints. The boundary of a polygon is the line that defines the perimeter.
Interior
The interior of an object consists of points, lines or areas that are in the object but do not belong boundary.
Complement
The complement, also called exterior, consists of the points, lines and areas which are not in the object.
The basic method used to compare two geometrical objects is to analyze the intersections between all the
possible pairs that can be built with the interior, exterior and boundary of these two objects. Based on the
resulting "intersection" matrix, the relationships between the two geometrical objects can be classified.
Two objects, A and B, are given. Both of them are represented by their interior (i), boundary (b) and exterior
(e). There are nine possible relations of these two geometrical objects. They are shown in the following table.
The 4#intersection#matrix is sometimes used as basis for the analysis of topological relations. It is generated
by omitting the components of the exterior. It is less powerful than the 9#intersectionmatrix.
The most important topological relations between objects that are used in GIS applications are listed in
the following sequence. Note that there are three different geometries (point, line, polygon) on which the
topological relations are applied.
Disjoint
There is no intersection area between object A and object B. Test for disjoint.
Meet
Object A and object B meet at the boundary. The boundaries meet, but not the interior. Two geometry objects
meet if the boundaries touch. Test for touch.
Overlap
Object A and object B overlap. Test for intersect (inversion of disjoint).
Overlap with disjoint: The interior of an object intersects the boundary and the interior of the other object,
but the boundaries do not intersect. That is the case if a line starts outside a polygon (area) and ends in the
interior of the polygon.
Overlap with Intersect: The boundaries and the interior of both objects intersect. If a geometry object has to
intersect another geometry object the geometry needs to be part of the dimension of the bigger object. That
means:
• Points
- Cannot intersect with points, lines or areas.
• Lines
- Cannot intersect with points.
- Can intersect with other lines » intersection = point.
- Can intersect with polygons » intersection = lines (or points).
Contains
Object A contains object B. Test whether the initial geometry object encloses a different geometry object. The
interior and the boundary of an object are completely inside of the other object. A geometry object cannot
contain a geometry object of higher order. E.g.:
• Points can not contain lines or polygons.
• Lines can not contain polygons.
Inside
Object B lies inside object A. It is the opposite of "contain". If A is inside B, then B contains A.
Covers
Object A covers object B. The interior of an object is completely inside the other object and the boundaries
intersect. A geometry object can’t include a geometry object of higher dimension. That means:
• Points can not contain lines or polygons.
• Lines can not contain polygons.
Covered by
Object B is covered by object A. It is the opposite of "covers". If A is covered by B, then B covers A.
Equal
Object B and object A match. Test for equality of the initial geometry object and a different geometry object.
The interior and the boundary of an object are lying on the boundary of the other object and vice versa. This
happens when a line falls exactly on the boundary of a polygon. The coordinates of all components have to be
equal. The compared geometry objects must be equal. That means:
• Point = point
• Line = line
• Polygon = polygon
The following table shows the most common topological relations:
poly-poly line-line point-point poly-line poly-point line-point
Disjoint
Meet
Overlap
Contains
Inside
Covers
Covered by
Equal
The following table shows the 9#intersetion#schema and the 4#intersection#schema for some typical
topological relations between two polygons, proposed by Egenhofer et al. (1993). The relations are given by
the values 0 and 1. Every pair has an empty (0) or an occupied (1) intersection.
Topological relation Graphical description 4-intersection-matrix 9-intersection-matrix
Disjoint
Meet
Overlap
Contains
Inside
Covers
Covered by
Equal
There is at least one disadvantage in this model: there is no possibility to conceptually separate different
situations.
GIS. Further topological operators, which are adapted to the corresponding data structure, are developed. In the
following list some of the functions and the corresponding operators, provided by Geomedia, Oracle Spatial
and ArcView, are shown.
TOPOLOGICAL ORACLE GEOMEDIA ARCVIEW
RELATION
Disjoint disjoint - are within a distance of
Meet touch meet -
Overlap overlap by intersect overlap intersect
Contains contains entirely contains completely contains
Inside covers are entirely contained by contains the center of
Covers inside contain have their center in
Coverered by coveredby are contained by are completely within
Equal equal are spatially equal -
Relationships between polygons and other objects are the most frequent. Below are some examples of
topological queries:
LAYERS INPUT QUERY RESULTS
N.1 N. 2 Table Graphically
Find the
buildings,
which lie
completely
within the
forest.
Find the
buildings,
which intersect
the boundary
of the forest.
Find the
buildings,
whose centroid
lies within the
forest.
Application
As described earlier, topological queries refer to the reciprocal locations of objects in space. The following
examples illustrate this concept.
Select the huts that are reached first from the starting point (green point).
Select the huts that are in the forest (dark green area).
Select the areas that touch the forest (dark green area).
1.5. Summary
An information system is a question#response system based on a data set. Such systems contain tools
for the computational analysis of information. If the stored data have a spatial reference, the system is
called geographic information system (GIS). It allows the interrogation and display of attribute values based
on spatial criteria and vice versa. Hence the term data analysis, that includes all those analysis, queries,
evaluations etc. that can be performed on structured and stored geodata. Queries can be performed according
to different approaches: thematic, geometric and topological. Geometry is expressed by the spatial reference,
which is assigned to all objects. They fulfill the requirement of location and extension. The topological
properties are expressed by the relations of neighborhood, containment, and overlapping etc. Besides geometric
characteristics that exist for spatial data, there are also thematic properties. Those properties are stored in
tables. A query can be classified in two ways, depending on the result. In a direct query, there is a subset
extracted from the database and the original data are not modified by this process. In a manipulation, new space
related information elements are generated, which can be used in further analysis operations. The results of
data processing and data manipulation in GIS should be represented in a form which is understandable for the
user, or in a form which enables data sharing.
1.6. Glossary
Abfrage:
Die Abfrage ermittelt räumliche Beziehungen zwischen Elementen eines oder mehrerer Themen, um
auf dieser Basis eine Lokalisierung von Objekten zu erreichen. Die Analyseergebnisse können dann bei
konkreten Fragestellungen zur Entscheidungsfindung beitragen.
Konsistent:
Gewährleisten der Widerspruchsfreiheit innerhalb einer Datenbank; d. h., dass der Inhalt einer Datenbank
alle vordefinierten Konsistenzbedingungen ("Constraints") erfüllt.
Operators:
In search algorithms, operators enable the logical conjunction of search items using keywords like AND,
OR, and NOT.
SQL:
"Acronym for Structured Query Language. A syntax for retrieving and manipulating data from a relational
database. SQL has become an industry standard query language in most relational database management
systems" (ESRI), e.g. Oracle, DB2, Access, etc.
Topologie:
Die Topologie beschäftigt sich mit den räumlichen und strukturellen Eigenschaften der geometrischen
Objekte unabhängig von ihrer Ausdehnung und ihrer Form. Die topologischen Eigenschaften äussern sich
in Beziehung der Nachbarschaft, des Enthaltenseins, der Überschneidung und Ähnlichem. (Carosio 2000)
1.7. Bibliography
• Bartelme, N., 2000. Geoinformatik – Modelle, Strukturen, Funktionen. 3rd. Berlin: Springer.
• Bill, R., 1999. Grundlagen der Geo-Informationssysteme. Karlsruhe: Wichmann Verlag.
• Carosio, A., 2000. Geoinformationssysteme – Band 1. Institut für Geodäsie und Photogrammetrie, ETH
Zürich.
• Egenhofer, M.J., 1993. A model for detailed binary topological relationships. Geomatica, Vol. 47, no.
3-4, 261-273.
• ESRI. ArcInfo Help.
Download: http://www.esri.com
• ESRI. GIS Dictionary: SQL [online]. Available from: http://support.esri.com/en/knowledgebase/
Gisdictionary/browse [Accessed 02.05.2016].
• Longley, P.A.; Goodchild, M.F.; Maguire, D.J.; Rhind D.W., 1999. Geographical Information
Systems. Principles, techniques, applications and management. New York, etc.: John Wiley & Sons.
• Saaty, T.L., 1980. The Analytic Hierarchy Process: Planning Setting Priorities, Resource Allocation.
New York: McGraw-Hill International.
• Worboys, M., Duckham, M., 2004. GIS. A Computing Perspective. Chapter 4. Models of geospatial
information. Boca Raton, etc.: CRC Press.