Top Banner
Spatial Indexing Kristian Torp Department of Computer Science Aalborg University people.cs.aau.dk/˜torp [email protected] November 19, 2015 daisy.aau.dk Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 1 / 54
97

Spatial Indexing

Jan 26, 2017

Download

Technology

torp42
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Spatial Indexing

Spatial Indexing

Kristian Torp

Department of Computer ScienceAalborg University

people.cs.aau.dk/˜[email protected]

November 19, 2015

daisy.aau.dk

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 1 / 54

Page 2: Spatial Indexing

Outline

1 Introduction

2 Space-filling Curves (SFC)

3 R-TreeMinimum-Bounding Rectangle (MBR)Range Search

4 Quadtrees

5 Summary

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 2 / 54

Page 3: Spatial Indexing

Learning Goals

The ProblemExisting indexes good for scalar values, e.g., 42 and ’Hello, World’

Existing indexes not good complex data, e.g., geometries and timeseries

QuizWhich index structures do you know?

GoalsOverview of spatial indexing techniques

Understand how spatial indexing different from “plain” indexing

Understand the basic spatial indexing techniques

Be able to construct a spatial index on PostgreSQL

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 3 / 54

Page 4: Spatial Indexing

Outline

1 Introduction

2 Space-filling Curves (SFC)

3 R-TreeMinimum-Bounding Rectangle (MBR)Range Search

4 Quadtrees

5 Summary

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 4 / 54

Page 5: Spatial Indexing

Spatial Query Types

c1

c2 c3 c4c5c6

c7c1

c2 c3 c4c5c6

c7

Range Nearest Neighbor

c1

c2 c3 c4c5c6

c7

f1 f2f3

f4c1

c2 c3 c4c5c6

c7

Within Spatial Join

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 5 / 54

Page 6: Spatial Indexing

Spatial Indexing

Purposes of Spatial Index StructureShould speed-up the most typical spatial queries

Should not take up too much space

Should efficiently support modifications

AssumptionsSpatial query only covers small part of the “world”

Spatial data is queried more often then modified

NoteIndexing necessary to make queries run faster

Hashing does not work well for spatial data typesYou can index spatial data using conventional (B+-tree) indexes

It is often slow!Will see how can be used with space-filling curves

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 6 / 54

Page 7: Spatial Indexing

Overview of Spatial Indexing Techniques

TechniquesSpace-filling curves (a set of approaches)

Old wine, recycled bottles

R-trees (a large family of index structures)A generalization of the B+-tree

Quadtrees (a family of index structures)Old wine, new bottles

Other DomainsImage processing, e.g., medical and biometrics

Image recognition, e.g., letters and books

Gaming, e.g., for the terrain, buildings, and roads

Chip design, e.g., integrated circuits, think AMD, ARM, and Intel

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 7 / 54

Page 8: Spatial Indexing

Create Spatial Index

Example (Table with Spatial Data Column)create tab l e landscape (

geo name varchar (20) pr imary key ,geo geometry not n u l l

) ;

Example (Create B+-tree Index on PostgreSQL)create index landscape geo name

on landscape ( geo name ) ;

Example (Create R-tree Index on PostgreSQL)create index landscape geo

on landscape using g i s t ( geo ) ;

NoteGIST = Generalized Search Trees = R-tree = spatial index

From SQL interface not a big think!

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 8 / 54

Page 9: Spatial Indexing

Outline

1 Introduction

2 Space-filling Curves (SFC)

3 R-TreeMinimum-Bounding Rectangle (MBR)Range Search

4 Quadtrees

5 Summary

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 9 / 54

Page 10: Spatial Indexing

The Basic Idea: SFCExample (Division of World)

0 1 2 3 40

1

2

3

4

0 1

23

4

5 6

7 8

9 10

11

1213

14 15

a

b

c

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 10 / 54

Page 11: Spatial Indexing

The Basic Idea: SFCExample (Division of World)

0 1 2 3 40

1

2

3

4

0 1

23

4

5 6

7 8

9 10

11

1213

14 15

a

b

c

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 10 / 54

Page 12: Spatial Indexing

The Basic Idea: SFCExample (Division of World)

0 1 2 3 40

1

2

3

4

0 1

23

4

5 6

7 8

9 10

11

1213

14 15

a

b

c

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 10 / 54

Page 13: Spatial Indexing

The Basic Idea: SFCExample (Division of World)

0 1 2 3 40

1

2

3

4

0 1

23

4

5 6

7 8

9 10

11

1213

14 15

a

b

c

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 10 / 54

Page 14: Spatial Indexing

The Basic Idea: SFCExample (Division of World)

0 1 2 3 40

1

2

3

4

0 1

23

4

5 6

7 8

9 10

11

1213

14 15

a

b

c

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 10 / 54

Page 15: Spatial Indexing

The Basic Idea: SFCExample (Division of World)

0 1 2 3 40

1

2

3

4

0 1

23

4

5 6

7 8

9 10

11

1213

14 15

a

b

c

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 10 / 54

Page 16: Spatial Indexing

SFC for Indexing Spatial Objects

Example (Space-filling Curve)

0 1 2 3 40

1

2

3

4

0 1

23

4

5 6

7 8

9 10

11

1213

14 15

a

b

c

ObjectName Idx Coor

a 10 (3.7, 3.7)b 7 (1.2, 2.7)c 1 (ref)c 2 (ref)c 13 (ref)c 14 (ref)

VariationsThis is a Hilbert space-filling curve

Index and spatial data typically split in two tables

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 11 / 54

Page 17: Spatial Indexing

Spatial Query using SFC

Example (Space-filling Curve)

0 1 2 3 40

1

2

3

4

0 1

23

4

5 6

7 8

9 10

11

1213

14 15

a

b

c

ObjectName Idx Coor

a 10 (3.7, 3.7)b 7 (1.2, 2.7)c 1 (ref)c 2 (ref)c 13 (ref)c 14 (ref)

Example (Spatial Query is Plain SQL Query)s e l e c t d i s t i n c t ObjectNamefrom t a b s f c h i l b e r t −− t ab l e where s p a t i a l index storedwhere idx between 0 and 3

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 12 / 54

Page 18: Spatial Indexing

Hilbert, Peano, and Sierpinski SFCs

Example

Hilbert Peano Sierpinsk

Order= 4 Order = 3 Order = 3

NoteThere are several variations of these SFCs

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 13 / 54

Page 19: Spatial Indexing

SFC Indexing

Example

0 1 2 3 40

1

2

3

4

0 1

23

4

5 6

7 8

9 10

11

1213

14 15

x

yz

vObjectName Idx

x ?y ?z ?v ?

NotePoint, linestring, and polygon supportedBest support for points

Why?

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 14 / 54

Page 20: Spatial Indexing

Motivation for using SFCs

Why?

Reuse B+-tree index structureReuse B+-tree concurrency algorithm

Extremely complicated to make efficient

Some operations can be very efficiently implementedWhen first in main memory

Add spatial index support to a “plain” DBMS

NoteSpace-filling curves can be combined with quadtrees

Space-filling curves can be combined with R-tree

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 15 / 54

Page 21: Spatial Indexing

2D Hilbert Curves of Different OrdersExample (Order 2 to 7)

Large GeometriesHow many index values for a large geometry?

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 16 / 54

Page 22: Spatial Indexing

3D Hilbert Curve

[http://www.math.uwaterloo.ca/ wgilbert/Research/HilbertCurve/HilbertCurve.html]

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 17 / 54

Page 23: Spatial Indexing

Summary: SFC

NoteSpace divisionReduces from nD/3D/2D to 1D (integers)

The numbers are indexed using a B+-tree

There are many variations of space-filling curvesHilbert, Moore, Peano, and Sierpinksi

Very simple to implement

Main disadvantages: Spatial close, index distant

Concurrency control already implemented

Not optimal when index must be stored on disk (block device)

QuizDid you notice anything about the dimension sizes?

Can you give a reason why?

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 18 / 54

Page 24: Spatial Indexing

Outline

1 Introduction

2 Space-filling Curves (SFC)

3 R-TreeMinimum-Bounding Rectangle (MBR)Range Search

4 Quadtrees

5 Summary

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 19 / 54

Page 25: Spatial Indexing

Outline

1 Introduction

2 Space-filling Curves (SFC)

3 R-TreeMinimum-Bounding Rectangle (MBR)Range Search

4 Quadtrees

5 Summary

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 20 / 54

Page 26: Spatial Indexing

Examples of MBRsExample (MBRs for Various Shapes)

0 1 2 3 4 5 6 7 80

1

2

3

4

5

6

7

8

NoteFour coordinates to index all spatial objects in 2D

Leads to false positives

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 21 / 54

Page 27: Spatial Indexing

Examples of MBRsExample (MBRs for Various Shapes)

0 1 2 3 4 5 6 7 80

1

2

3

4

5

6

7

8

NoteFour coordinates to index all spatial objects in 2D

Leads to false positives

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 21 / 54

Page 28: Spatial Indexing

Examples of MBRsExample (MBRs for Various Shapes)

0 1 2 3 4 5 6 7 80

1

2

3

4

5

6

7

8

NoteFour coordinates to index all spatial objects in 2D

Leads to false positives

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 21 / 54

Page 29: Spatial Indexing

Examples of MBRsExample (MBRs for Various Shapes)

0 1 2 3 4 5 6 7 80

1

2

3

4

5

6

7

8

NoteFour coordinates to index all spatial objects in 2D

Leads to false positives

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 21 / 54

Page 30: Spatial Indexing

Examples of MBRsExample (MBRs for Various Shapes)

0 1 2 3 4 5 6 7 80

1

2

3

4

5

6

7

8

NoteFour coordinates to index all spatial objects in 2D

Leads to false positives

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 21 / 54

Page 31: Spatial Indexing

Examples of MBRsExample (MBRs for Various Shapes)

0 1 2 3 4 5 6 7 80

1

2

3

4

5

6

7

8

NoteFour coordinates to index all spatial objects in 2D

Leads to false positivesKristian Torp (Aalborg University) Spatial Indexing November 19, 2015 21 / 54

Page 32: Spatial Indexing

InsertExample (R-tree)

0 1 2 3 4 5 6 7 80

1

2

3

4

5

12

A

NoteAssuming maximum 3 values per node

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 22 / 54

Page 33: Spatial Indexing

InsertExample (R-tree)

0 1 2 3 4 5 6 7 80

1

2

3

4

5

12

3

A

NoteAssuming maximum 3 values per node

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 22 / 54

Page 34: Spatial Indexing

InsertExample (R-tree)

0 1 2 3 4 5 6 7 80

1

2

3

4

5

12

34

A

B

NoteAssuming maximum 3 values per node

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 22 / 54

Page 35: Spatial Indexing

InsertExample (R-tree)

0 1 2 3 4 5 6 7 80

1

2

3

4

5

12

345

A

B

NoteAssuming maximum 3 values per node

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 22 / 54

Page 36: Spatial Indexing

InsertExample (R-tree)

0 1 2 3 4 5 6 7 80

1

2

3

4

5

12

345

6A

B

NoteAssuming maximum 3 values per node

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 22 / 54

Page 37: Spatial Indexing

InsertExample (R-tree)

0 1 2 3 4 5 6 7 80

1

2

3

4

5

12

345

6

7

A

B C

NoteAssuming maximum 3 values per node

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 22 / 54

Page 38: Spatial Indexing

Quiz: Good MBR?

0 1 2 3 4 5 6 7 8012345

1 2

3A

0 1 2 3 4 5 6 7 8012345

1 2

3 A

A B

0 1 2 3 4 5 6 7 8012345

1 2

3

A

0 1 2 3 4 5 6 7 8012345

1 2

3A

C D

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 23 / 54

Page 39: Spatial Indexing

DeleteExample (R-tree)

0 1 2 3 4 5 6 7 80

1

2

3

4

5

12

345

6

7

A

B C

NoteAssuming minimum 2 values and maximum 3 values per node

Delete 1⇒ shrink AKristian Torp (Aalborg University) Spatial Indexing November 19, 2015 24 / 54

Page 40: Spatial Indexing

DeleteExample (R-tree)

0 1 2 3 4 5 6 7 80

1

2

3

4

5

2

345

6

7

A

B C

NoteDelete 6

Underflow in A merge with other MBR at same levelKristian Torp (Aalborg University) Spatial Indexing November 19, 2015 24 / 54

Page 41: Spatial Indexing

DeleteExample (R-tree)

0 1 2 3 4 5 6 7 80

1

2

3

4

5

2

345

7B

C

NoteDelete 3

Underflow in B merge with other MBR at same levelKristian Torp (Aalborg University) Spatial Indexing November 19, 2015 24 / 54

Page 42: Spatial Indexing

DeleteExample (R-tree)

0 1 2 3 4 5 6 7 80

1

2

3

4

5

2

457

B

C

NoteThere are many details

Picking good candidate for merge is hardKristian Torp (Aalborg University) Spatial Indexing November 19, 2015 24 / 54

Page 43: Spatial Indexing

A Complete R-treeExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3

A

45

6

B

78 9

C

I10

11

12

D13

14 15E

16

17

18

FII

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 25 / 54

Page 44: Spatial Indexing

A Complete R-treeExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3A

45

6

B

78 9

C

I10

11

12

D13

14 15E

16

17

18

FII

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 25 / 54

Page 45: Spatial Indexing

A Complete R-treeExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3A

45

6

B

78 9

C

I10

11

12

D13

14 15E

16

17

18

FII

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 25 / 54

Page 46: Spatial Indexing

A Complete R-treeExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3A

45

6

B

78 9

C

I10

11

12

D13

14 15E

16

17

18

FII

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 25 / 54

Page 47: Spatial Indexing

A Complete R-treeExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3A

45

6

B

78 9

C

I10

11

12

D13

14 15E

16

17

18

FII

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 25 / 54

Page 48: Spatial Indexing

A Complete R-treeExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3A

45

6

B

78 9

C

I10

11

12

D13

14 15E

16

17

18

FII

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 25 / 54

Page 49: Spatial Indexing

A Complete R-treeExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3A

45

6

B

78 9

C

I

10

11

12

D13

14 15E

16

17

18

FII

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 25 / 54

Page 50: Spatial Indexing

A Complete R-treeExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3A

45

6

B

78 9

C

I10

11

12

D13

14 15E

16

17

18

FII

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 25 / 54

Page 51: Spatial Indexing

A Complete R-treeExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3A

45

6

B

78 9

C

I10

11

12

D

13

14 15E

16

17

18

FII

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 25 / 54

Page 52: Spatial Indexing

A Complete R-treeExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3A

45

6

B

78 9

C

I10

11

12

D13

14 15

E

16

17

18

FII

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 25 / 54

Page 53: Spatial Indexing

A Complete R-treeExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3A

45

6

B

78 9

C

I10

11

12

D13

14 15E

16

17

18

FII

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 25 / 54

Page 54: Spatial Indexing

A Complete R-treeExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3A

45

6

B

78 9

C

I10

11

12

D13

14 15E

16

17

18

FII

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 25 / 54

Page 55: Spatial Indexing

A Complete R-treeExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3A

45

6

B

78 9

C

I10

11

12

D13

14 15E

16

17

18

F

II

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 25 / 54

Page 56: Spatial Indexing

A Complete R-treeExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3A

45

6

B

78 9

C

I10

11

12

D13

14 15E

16

17

18

FII

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 25 / 54

Page 57: Spatial Indexing

Clustering of Rectangles

Example (Clustering One)

a

b

c

d

Example (Clustering Two)

a

b

c

d

QuestionsWhich clustering of rectangles is best?

What are the evaluation criteria?Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 26 / 54

Page 58: Spatial Indexing

R-tree Node Properties

ParametersM is maximum fanout and determined by page sizem is minimum fanout

Typically 0.4-0.5 of M

R-tree PropertiesThe root node has minimum two children, unless it is a leaf

All non-root nodes have m and M children

Leaf-node entries <MBR, oid> MBR for the data object

Non-leaf node <MBR, ptr> MBR contains all child nodes

All leaf nodes at the same level

NoteR-tree properties similar to B+-tree properties

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 27 / 54

Page 59: Spatial Indexing

R-tree: Online Demo

R-tree Online Demo Appletgis.umb.no/gis/applets/rtree2/jdk1.1/

QuestionsWhat are the m and M?

What happens when you insert within an MBR?

What happens when you insert outside an MBR?

What happens if you delete from an MBR such that there are lessthan m objects?

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 28 / 54

Page 60: Spatial Indexing

Outline

1 Introduction

2 Space-filling Curves (SFC)

3 R-TreeMinimum-Bounding Rectangle (MBR)Range Search

4 Quadtrees

5 Summary

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 29 / 54

Page 61: Spatial Indexing

Filter and Refine

Two-Step Filtering

R-TreeIntermediate

FilterGeometry

Comparison Result

False Hits

Primary Filter Secondary Filter

Two-Step ProcessUse MBR for checking for potential overlap (fast)

Use actual geometry to check for real overlap (slow)

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 30 / 54

Page 62: Spatial Indexing

MBRs for Overlap

O1 O2

O3

O4

O5

Which objects are in the result?Which objects are not in the result?Which objects may be in the result?

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 31 / 54

Page 63: Spatial Indexing

Range SearchExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3

45

6 78 9

A

BC

I

10

11

12

13

14 15

16

17

18

D E

FII

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 32 / 54

Page 64: Spatial Indexing

Range SearchExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3

45

6 78 9

A

BC

I10

11

12

13

14 15

16

17

18

D E

FII

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 32 / 54

Page 65: Spatial Indexing

Range SearchExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3

45

6 78 9

A

BC

I10

11

12

13

14 15

16

17

18

D E

FII

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 32 / 54

Page 66: Spatial Indexing

Range Search, OneExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3

45

6 78 9

A

BC

I10

11

12

13

14 15

16

17

18

D E

FII

Q1

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 33 / 54

Page 67: Spatial Indexing

Range Search, OneExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3

45

6 78 9

A

BC

I10

11

12

13

14 15

16

17

18

D E

FIIQ1

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 33 / 54

Page 68: Spatial Indexing

Range Search, OneExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3

45

6 78 9

A

BC

I10

11

12

13

14 15

16

17

18

D E

FIIQ1

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 33 / 54

Page 69: Spatial Indexing

Range Search, TwoExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3

45

6 78 9

A

BC

I10

11

12

13

14 15

16

17

18

D E

FII

Q2

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 34 / 54

Page 70: Spatial Indexing

Range Search, TwoExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3

45

6 78 9

A

BC

I10

11

12

13

14 15

16

17

18

D E

FII

Q2

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 34 / 54

Page 71: Spatial Indexing

Range Search, TwoExample (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

10

1

2

3

45

6 78 9

A

BC

I10

11

12

13

14 15

16

17

18

D E

FII

Q2

I II - -

A B C -

1 2 3 - 4 5 6 - 7 8 9 -

D E F -

10 11 12 - 13 14 15 - 16 17 18 -

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 34 / 54

Page 72: Spatial Indexing

Quiz: Range Query

Example (R-tree)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160123456789

101 2 3

45

67

8 9 10

11

12

13

1415

16

171819

20 21

22

23

24 25

2627

28 2930

31 32

3334

35

36 37

A B

C D

E FG

H

IJ

K

L

M

I

II

III

IV

Q

QuizWhich MBRs are examined?

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 35 / 54

Page 73: Spatial Indexing

R-tree versus R+-tree and R∗-tree

R∗-treeLike R-tree just insert algorithm changed

Minimize area of each MBR

Minimize overlap between MBRs

Maximize storage usage

Maximize square-ness of MBRs

Overall: Slower modifications, faster search

R+-treeNo overlap between MBRs for non-leaf node

Indexed object may be inserted more than once

Multi-path not needed for search

Overall: Higher tree / faster search

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 36 / 54

Page 74: Spatial Indexing

Summary: R-tree

NoteData division

Multi-level index (a tree!)

There are many variations of the R-tree, e.g., R+-tree and R∗-tree

Very complicated to implement

Concurrency control very complicated to implement

Very good when index must be stored on disk (block device)

Good index structure for in-memory databases

Many (hundreds) of index structures derived from R-tree

PerformanceThe R-tree typically outperforms quadtrees and SFC!

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 37 / 54

Page 75: Spatial Indexing

Outline

1 Introduction

2 Space-filling Curves (SFC)

3 R-TreeMinimum-Bounding Rectangle (MBR)Range Search

4 Quadtrees

5 Summary

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 38 / 54

Page 76: Spatial Indexing

Basic Idea

0 1 2 3 4 5 6 7 80

1

2

3

4

5

6

7

8

1

2 3

4

5

6

7

89

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 39 / 54

Page 77: Spatial Indexing

Basic Idea

0 1 2 3 4 5 6 7 80

1

2

3

4

5

6

7

8

1

2 3

4

5

6

7

89

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 39 / 54

Page 78: Spatial Indexing

Basic Idea

0 1 2 3 4 5 6 7 80

1

2

3

4

5

6

7

8

1

2 3

4

5

6

7

89

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 39 / 54

Page 79: Spatial Indexing

Basic Idea

0 1 2 3 4 5 6 7 80

1

2

3

4

5

6

7

8

1

2 3

4

5

6

7

89

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 39 / 54

Page 80: Spatial Indexing

Basic Idea

0 1 2 3 4 5 6 7 80

1

2

3

4

5

6

7

8

1

2 3

4

5

6

7

89

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 39 / 54

Page 81: Spatial Indexing

Basic Idea

0 1 2 3 4 5 6 7 80

1

2

3

4

5

6

7

8

1

2 3

4

5

6

7

89

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 39 / 54

Page 82: Spatial Indexing

The Quad and Tree in Quadtree

Quad

0 1

2 3

10 11

12 13

30 31

32 33

120 121

122 123

Tree

All

0 1

10 11 12

120 121 122 123

13

2 3

30 31 32 33

NoteOne entry for each point

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 40 / 54

Page 83: Spatial Indexing

Rectangles in a QuadtreeThe Data

0 1

2 3

10 11

12 13120 121

122 123A

B

C

D

EF

G H

The Quadtree

All

0:A,E 1:C,D

10 11 12:F

120 121 122:G 123:H

13

2:A,B 3:B,D

NoteEach rectangle in multiple subtrees

Where is the worst place a rectangle can be placed?Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 41 / 54

Page 84: Spatial Indexing

Quiz: Indexing RectanglesThe Data

0 1

2 3

10 11

12 13

30 31

32 33

120 121

122 123

130 131

132 133

A B

C

D

E

F

Which Index Values?A?

B?

C?

D?

E?

F?

Trade offAccurate (smallest quads)

Space usage (biggest quads)

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 42 / 54

Page 85: Spatial Indexing

Quiz: Indexing RectanglesThe Data

0 1

2 3

10 11

12 13

30 31

32 33

120 121

122 123

130 131

132 133

A B

C

D

E

F

Which Index Values?A?

B?

C?

D?

E?

F?

Trade offAccurate (smallest quads)

Space usage (biggest quads)Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 42 / 54

Page 86: Spatial Indexing

Complicated Polygon in a Quadtree, OneData

0 1

2 3

20 21

22 23

30 31

32 33

10 11

12 13120 121

122 123

210 211

212 213

Index

0 1

2 3

20 21

22 23

30 31

32 33

10 11

12 13120 121

122 123

210 211

212 213

NoteMany entries for a single object

Assuming maximum three levels in indexKristian Torp (Aalborg University) Spatial Indexing November 19, 2015 43 / 54

Page 87: Spatial Indexing

Complicated Polygon in a Quadtree, Two

Data

0 1

2 3

10 11

12 13

20 21

22 23

30 31

32 33

120 121

122 123

Index

0 1

2 3

10 11

12 13

20 21

22 23

30 31

32 33

120 121

122 123

NoteAnother example

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 44 / 54

Page 88: Spatial Indexing

Linestrings in a Quadtree

Data

0 1

2 3

10 11

12 13

20 21

22 23

30 31

32 33

120 121

122 123

130 131

132 133

210 211

212 213

220 221

222 223

230 231

232 233

300 301

302 303

Index

0 1

2 3

10 11

12 13

20 21

22 23

30 31

32 33

120 121

122 123

130 131

132 133

210 211

212 213

220 221

222 223

230 231

232 233

300 301

302 303

NoteLong lines are a challenge for quadtrees

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 45 / 54

Page 89: Spatial Indexing

SQL Server 2008 R2 Spatial Grid

[http://sqlskills.com/blogs/paul/post/SQL-Server-2008-Spatial-indexes.aspx]

NoteMaximum 4 levels

Small = 4x4, Medium = 8x8, Large 16x16

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 46 / 54

Page 90: Spatial Indexing

SQL Server 2008 R2: Complex Object

[http://social.technet.microsoft.com]

QuestionsWhy different granularities?

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 47 / 54

Page 91: Spatial Indexing

Quadtree Demo

PR Quadtree Demodonar.umiacs.umd.edu/quadtree/points/prquad.html

QuestionsWhat can you note about the sizes of the regions when split?

Is the tree balanced?

PMR Quadtree Demodonar.umiacs.umd.edu/quadtree/rectangles/PMR.html

QuestionsWhat happens when you place a rectangle in the middle of an emptyregion?

Is the tree balanced?

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 48 / 54

Page 92: Spatial Indexing

Which Quadtree are Legal?

A B

C DKristian Torp (Aalborg University) Spatial Indexing November 19, 2015 49 / 54

Page 93: Spatial Indexing

Quadtree: Comments

NoteSpace division

Can be used for all types of spatial objectsThere are many variations of the quad tree

The version here is called the PR quadtree

Variation of quadtree is used in SQL Server 2008 R2

Data structure is fairly simple to implement

Concurrency control is fairly simple to implement

Not optimal when index must be stored on disk (block device)

QuizWhy are there multiple levels in a quadtree?Did you notice anything about the dimension sizes?

Can you give a reason why?

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 50 / 54

Page 94: Spatial Indexing

Outline

1 Introduction

2 Space-filling Curves (SFC)

3 R-TreeMinimum-Bounding Rectangle (MBR)Range Search

4 Quadtrees

5 Summary

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 51 / 54

Page 95: Spatial Indexing

Comparison of Indexing Methods

Support for Data Types

SFC Quadtree R-tree

Point Good Good GoodLinestring Okay Okay GoodPolygon Okay Okay Good

Support for Queries

SFC Quadtree R-tree

Point Good Good GoodRange Bad Okay GoodJoin Okay Okay GoodNN Okay Okay Okay

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 52 / 54

Page 96: Spatial Indexing

Summary: Spatial Indexing

NoteThe R-tree is the industry “work horse”

Good 2D and 3DBreaks around∼ 10D, called the dimensional curse

Quadtree used because simple to implementParticular good if all data is in main memoryVery good demo at donar.umiacs.umd.edu/quadtree/index.html

Space-filling curves may be exactly sufficient for your app!Simple to implementMuch faster than full table scans

Important ConceptsData and space division

Minimum bounding rectangle (MBR)

Split

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 53 / 54

Page 97: Spatial Indexing

More Information

Web LinksR-tree at Wikipedia at en.wikipedia.org/wiki/R-tree

Good overview with many links to additional material

R-tree demo at gis.umb.no/gis/applets/rtree2/jdk1.1/

Quadtree and R-Tree demo atdonar.umiacs.umd.edu/quadtree/index.html

What new spatial next SQL Server social.technet.microsoft.com/wiki/contents/articles/4136.aspx

Research RelatedThe original R-tree paperwww-db.deis.unibo.it/courses/SI-LS/papers/Gut84.pdf

The first R∗-tree paper epub.ub.uni-muenchen.de/4256/1/31.pdf

Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 54 / 54