Successfully reported this slideshow.
Upcoming SlideShare
×

# Spatial Indexing

Spatial indexing using Space-Filling Curves (SFC), R-Trees, and Quadtrees. Examples on spatial indexing on PostgreSQL.

• Full Name
Comment goes here.

Are you sure you want to Yes No

### Spatial Indexing

1. 1. Spatial Indexing Kristian Torp Department of Computer Science Aalborg University people.cs.aau.dk/˜torp torp@cs.aau.dk November 19, 2015 daisy.aau.dk Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 1 / 54
2. 2. Outline 1 Introduction 2 Space-ﬁlling Curves (SFC) 3 R-Tree Minimum-Bounding Rectangle (MBR) Range Search 4 Quadtrees 5 Summary Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 2 / 54
3. 3. Learning Goals The Problem Existing indexes good for scalar values, e.g., 42 and ’Hello, World’ Existing indexes not good complex data, e.g., geometries and time series Quiz Which index structures do you know? Goals Overview 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
4. 4. Outline 1 Introduction 2 Space-ﬁlling Curves (SFC) 3 R-Tree Minimum-Bounding Rectangle (MBR) Range Search 4 Quadtrees 5 Summary Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 4 / 54
5. 5. Spatial Query Types c1 c2 c3 c4 c5 c6 c7 c1 c2 c3 c4 c5 c6 c7 Range Nearest Neighbor c1 c2 c3 c4 c5 c6 c7 f1 f2 f3 f4c1 c2 c3 c4 c5 c6 c7 Within Spatial Join Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 5 / 54
6. 6. Spatial Indexing Purposes of Spatial Index Structure Should speed-up the most typical spatial queries Should not take up too much space Should efﬁciently support modiﬁcations Assumptions Spatial query only covers small part of the “world” Spatial data is queried more often then modiﬁed Note Indexing necessary to make queries run faster Hashing does not work well for spatial data types You can index spatial data using conventional (B+-tree) indexes It is often slow! Will see how can be used with space-ﬁlling curves Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 6 / 54
7. 7. Overview of Spatial Indexing Techniques Techniques Space-ﬁlling 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 Domains Image 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
8. 8. Create Spatial Index Example (Table with Spatial Data Column) create table landscape ( geo name varchar (20) primary 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 ) ; Note GIST = 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
9. 9. Outline 1 Introduction 2 Space-ﬁlling Curves (SFC) 3 R-Tree Minimum-Bounding Rectangle (MBR) Range Search 4 Quadtrees 5 Summary Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 9 / 54
10. 10. The Basic Idea: SFC Example (Division of World) 0 1 2 3 4 0 1 2 3 4
11. 11. The Basic Idea: SFC Example (Division of World) 0 1 2 3 4 0 1 2 3 4 0 1 23 4 5 6 7 8 9 10 11 1213 14 15
12. 12. The Basic Idea: SFC Example (Division of World) 0 1 2 3 4 0 1 2 3 4 0 1 23 4 5 6 7 8 9 10 11 1213 14 15
13. 13. The Basic Idea: SFC Example (Division of World) 0 1 2 3 4 0 1 2 3 4 0 1 23 4 5 6 7 8 9 10 11 1213 14 15 a
14. 14. The Basic Idea: SFC Example (Division of World) 0 1 2 3 4 0 1 2 3 4 0 1 23 4 5 6 7 8 9 10 11 1213 14 15 a b
15. 15. The Basic Idea: SFC Example (Division of World) 0 1 2 3 4 0 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
16. 16. SFC for Indexing Spatial Objects Example (Space-ﬁlling Curve) 0 1 2 3 4 0 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) Variations This is a Hilbert space-ﬁlling curve Index and spatial data typically split in two tables Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 11 / 54
17. 17. Spatial Query using SFC Example (Space-ﬁlling Curve) 0 1 2 3 4 0 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) select d i s t i n c t ObjectName from t a b s f c h i l b e r t −− table where s p a t i a l index stored where idx between 0 and 3 Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 12 / 54
18. 18. Hilbert, Peano, and Sierpinski SFCs Example Hilbert Peano Sierpinsk Order= 4 Order = 3 Order = 3 Note There are several variations of these SFCs Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 13 / 54
19. 19. SFC Indexing Example 0 1 2 3 4 0 1 2 3 4 0 1 23 4 5 6 7 8 9 10 11 1213 14 15 x y z v ObjectName Idx x ? y ? z ? v ? Note Point, linestring, and polygon supported Best support for points Why? Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 14 / 54
20. 20. Motivation for using SFCs Why? Reuse B+-tree index structure Reuse B+-tree concurrency algorithm Extremely complicated to make efﬁcient Some operations can be very efﬁciently implemented When ﬁrst in main memory Add spatial index support to a “plain” DBMS Note Space-ﬁlling curves can be combined with quadtrees Space-ﬁlling curves can be combined with R-tree Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 15 / 54
21. 21. 2D Hilbert Curves of Different Orders Example (Order 2 to 7) Large Geometries How many index values for a large geometry? Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 16 / 54
22. 22. 3D Hilbert Curve [http://www.math.uwaterloo.ca/ wgilbert/Research/HilbertCurve/HilbertCurve.html] Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 17 / 54
23. 23. Summary: SFC Note Space division Reduces from nD/3D/2D to 1D (integers) The numbers are indexed using a B+ -tree There are many variations of space-ﬁlling curves Hilbert, 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) Quiz Did you notice anything about the dimension sizes? Can you give a reason why? Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 18 / 54
24. 24. Outline 1 Introduction 2 Space-ﬁlling Curves (SFC) 3 R-Tree Minimum-Bounding Rectangle (MBR) Range Search 4 Quadtrees 5 Summary Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 19 / 54
25. 25. Outline 1 Introduction 2 Space-ﬁlling Curves (SFC) 3 R-Tree Minimum-Bounding Rectangle (MBR) Range Search 4 Quadtrees 5 Summary Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 20 / 54
26. 26. Examples of MBRs Example (MBRs for Various Shapes) 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 Note Four coordinates to index all spatial objects in 2D Leads to false positives
27. 27. Examples of MBRs Example (MBRs for Various Shapes) 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 Note Four coordinates to index all spatial objects in 2D Leads to false positives
28. 28. Examples of MBRs Example (MBRs for Various Shapes) 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 Note Four coordinates to index all spatial objects in 2D Leads to false positives
29. 29. Examples of MBRs Example (MBRs for Various Shapes) 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 Note Four coordinates to index all spatial objects in 2D Leads to false positives
30. 30. Examples of MBRs Example (MBRs for Various Shapes) 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 Note Four coordinates to index all spatial objects in 2D Leads to false positives
31. 31. Examples of MBRs Example (MBRs for Various Shapes) 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 Note Four coordinates to index all spatial objects in 2D Leads to false positives Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 21 / 54
32. 32. Insert Example (R-tree) 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 1 2 A Note Assuming maximum 3 values per node Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 22 / 54
33. 33. Insert Example (R-tree) 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 1 2 3 A Note Assuming maximum 3 values per node Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 22 / 54
34. 34. Insert Example (R-tree) 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 1 2 3 4 A B Note Assuming maximum 3 values per node Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 22 / 54
35. 35. Insert Example (R-tree) 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 1 2 3 45 A B Note Assuming maximum 3 values per node Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 22 / 54
36. 36. Insert Example (R-tree) 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 1 2 3 45 6A B Note Assuming maximum 3 values per node Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 22 / 54
37. 37. Insert Example (R-tree) 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 1 2 3 45 6 7 A B C Note Assuming maximum 3 values per node Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 22 / 54
38. 38. Quiz: Good MBR? 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 1 2 3 A 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 1 2 3 A A B 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 1 2 3 A 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 1 2 3 A C D Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 23 / 54
39. 39. Delete Example (R-tree) 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 1 2 3 45 6 7 A B C Note Assuming minimum 2 values and maximum 3 values per node Delete 1 ⇒ shrink A Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 24 / 54
40. 40. Delete Example (R-tree) 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 2 3 45 6 7 A B C Note Delete 6 Underﬂow in A merge with other MBR at same level Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 24 / 54
41. 41. Delete Example (R-tree) 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 2 3 45 7 B C Note Delete 3 Underﬂow in B merge with other MBR at same level Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 24 / 54
42. 42. Delete Example (R-tree) 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 2 45 7 B C Note There are many details Picking good candidate for merge is hard Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 24 / 54
43. 43. A Complete R-tree Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 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 -
44. 44. A Complete R-tree Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 A 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 -
45. 45. A Complete R-tree Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 A 4 5 6 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 -
46. 46. A Complete R-tree Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 A 4 5 6 B 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 -
47. 47. A Complete R-tree Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 A 4 5 6 B 7 8 9 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 -
48. 48. A Complete R-tree Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 A 4 5 6 B 7 8 9 C 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 -
49. 49. A Complete R-tree Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 A 4 5 6 B 7 8 9 C I 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 -
50. 50. A Complete R-tree Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 A 4 5 6 B 7 8 9 C I 10 11 12 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 -
51. 51. A Complete R-tree Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 A 4 5 6 B 7 8 9 C I 10 11 12 D 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 -
52. 52. A Complete R-tree Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 A 4 5 6 B 7 8 9 C I 10 11 12 D 13 14 15 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 -
53. 53. A Complete R-tree Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 A 4 5 6 B 7 8 9 C I 10 11 12 D 13 14 15 E 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 -
54. 54. A Complete R-tree Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 A 4 5 6 B 7 8 9 C I 10 11 12 D 13 14 15 E 16 17 18 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 -
55. 55. A Complete R-tree Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 A 4 5 6 B 7 8 9 C I 10 11 12 D 13 14 15 E 16 17 18 F 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 -
56. 56. A Complete R-tree Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 A 4 5 6 B 7 8 9 C I 10 11 12 D 13 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
57. 57. Clustering of Rectangles Example (Clustering One) a b c d Example (Clustering Two) a b c d Questions Which clustering of rectangles is best? What are the evaluation criteria? Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 26 / 54
58. 58. R-tree Node Properties Parameters M is maximum fanout and determined by page size m is minimum fanout Typically 0.4-0.5 of M R-tree Properties The 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 Note R-tree properties similar to B+-tree properties Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 27 / 54
59. 59. R-tree: Online Demo R-tree Online Demo Applet gis.umb.no/gis/applets/rtree2/jdk1.1/ Questions What 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 less than m objects? Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 28 / 54
60. 60. Outline 1 Introduction 2 Space-ﬁlling Curves (SFC) 3 R-Tree Minimum-Bounding Rectangle (MBR) Range Search 4 Quadtrees 5 Summary Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 29 / 54
61. 61. Filter and Reﬁne Two-Step Filtering R-Tree Intermediate Filter Geometry Comparison Result False Hits Primary Filter Secondary Filter Two-Step Process Use 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
62. 62. 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
63. 63. Range Search Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 A B C I 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 -
64. 64. Range Search Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 A B C I 10 11 12 13 14 15 16 17 18 D E FII Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 32 / 54
65. 65. Range Search Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 A B C 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
66. 66. Range Search, One Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 A B C 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 -
67. 67. Range Search, One Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 A B C I 10 11 12 13 14 15 16 17 18 D E FIIQ1 Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 33 / 54
68. 68. Range Search, One Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 A B C I 10 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
69. 69. Range Search, Two Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 A B C 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 -
70. 70. Range Search, Two Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 A B C I 10 11 12 13 14 15 16 17 18 D E FII Q2 Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 34 / 54
71. 71. Range Search, Two Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 A B C I 10 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
72. 72. Quiz: Range Query Example (R-tree) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 2930 31 32 33 34 35 36 37 A B C D E F G H I J K L M I II III IV Q Quiz Which MBRs are examined? Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 35 / 54
73. 73. R-tree versus R+ -tree and R∗ -tree R∗ -tree Like 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 modiﬁcations, faster search R+ -tree No 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
74. 74. Summary: R-tree Note Data 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 Performance The R-tree typically outperforms quadtrees and SFC! Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 37 / 54
75. 75. Outline 1 Introduction 2 Space-ﬁlling Curves (SFC) 3 R-Tree Minimum-Bounding Rectangle (MBR) Range Search 4 Quadtrees 5 Summary Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 38 / 54
76. 76. Basic Idea 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 1 2 3 4
77. 77. Basic Idea 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 1 2 3 4
78. 78. Basic Idea 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7
79. 79. Basic Idea 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7
80. 80. Basic Idea 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9
81. 81. Basic Idea 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 39 / 54
82. 82. 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 Note One entry for each point Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 40 / 54
83. 83. Rectangles in a Quadtree The Data 0 1 2 3 10 11 12 13 120 121 122 123 A B C D E F 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 Note Each 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
84. 84. Quiz: Indexing Rectangles The 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? Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 42 / 54
85. 85. Quiz: Indexing Rectangles The 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 off Accurate (smallest quads) Space usage (biggest quads) Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 42 / 54
86. 86. Complicated Polygon in a Quadtree, One Data 0 1 2 3 20 21 22 23 30 31 32 33 10 11 12 13 120 121 122 123 210 211 212 213 Index 0 1 2 3 20 21 22 23 30 31 32 33 10 11 12 13 120 121 122 123 210 211 212 213 Note Many entries for a single object Assuming maximum three levels in index Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 43 / 54
87. 87. 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 Note Another example Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 44 / 54
88. 88. 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 Note Long lines are a challenge for quadtrees Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 45 / 54
89. 89. SQL Server 2008 R2 Spatial Grid [http://sqlskills.com/blogs/paul/post/SQL-Server-2008-Spatial-indexes.aspx] Note Maximum 4 levels Small = 4x4, Medium = 8x8, Large 16x16 Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 46 / 54
90. 90. SQL Server 2008 R2: Complex Object [http://social.technet.microsoft.com] Questions Why different granularities? Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 47 / 54
91. 91. Quadtree Demo PR Quadtree Demo donar.umiacs.umd.edu/quadtree/points/prquad.html Questions What can you note about the sizes of the regions when split? Is the tree balanced? PMR Quadtree Demo donar.umiacs.umd.edu/quadtree/rectangles/PMR.html Questions What happens when you place a rectangle in the middle of an empty region? Is the tree balanced? Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 48 / 54
92. 92. Which Quadtree are Legal? A B C D Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 49 / 54
93. 93. Quadtree: Comments Note Space division Can be used for all types of spatial objects There 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) Quiz Why 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
94. 94. Outline 1 Introduction 2 Space-ﬁlling Curves (SFC) 3 R-Tree Minimum-Bounding Rectangle (MBR) Range Search 4 Quadtrees 5 Summary Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 51 / 54
95. 95. Comparison of Indexing Methods Support for Data Types SFC Quadtree R-tree Point Good Good Good Linestring Okay Okay Good Polygon Okay Okay Good Support for Queries SFC Quadtree R-tree Point Good Good Good Range Bad Okay Good Join Okay Okay Good NN Okay Okay Okay Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 52 / 54
96. 96. Summary: Spatial Indexing Note The R-tree is the industry “work horse” Good 2D and 3D Breaks around∼ 10D, called the dimensional curse Quadtree used because simple to implement Particular good if all data is in main memory Very good demo at donar.umiacs.umd.edu/quadtree/index.html Space-ﬁlling curves may be exactly sufﬁcient for your app! Simple to implement Much faster than full table scans Important Concepts Data and space division Minimum bounding rectangle (MBR) Split Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 53 / 54
97. 97. More Information Web Links R-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 at donar.umiacs.umd.edu/quadtree/index.html What new spatial next SQL Server social.technet.microsoft. com/wiki/contents/articles/4136.aspx Research Related The original R-tree paper www-db.deis.unibo.it/courses/SI-LS/papers/Gut84.pdf The ﬁrst R∗-tree paper epub.ub.uni-muenchen.de/4256/1/31.pdf Kristian Torp (Aalborg University) Spatial Indexing November 19, 2015 54 / 54