1ebaecc14Sdanielk1977 2ebaecc14Sdanielk1977This directory contains an SQLite extension that implements a virtual 3ebaecc14Sdanielk1977table type that allows users to create, query and manipulate r-tree[1] 4ebaecc14Sdanielk1977data structures inside of SQLite databases. Users create, populate 5ebaecc14Sdanielk1977and query r-tree structures using ordinary SQL statements. 6ebaecc14Sdanielk1977 7ebaecc14Sdanielk1977 1. SQL Interface 8ebaecc14Sdanielk1977 9ebaecc14Sdanielk1977 1.1 Table Creation 10ebaecc14Sdanielk1977 1.2 Data Manipulation 11ebaecc14Sdanielk1977 1.3 Data Querying 12ebaecc14Sdanielk1977 1.4 Introspection and Analysis 13ebaecc14Sdanielk1977 14ebaecc14Sdanielk1977 2. Compilation and Deployment 15ebaecc14Sdanielk1977 16ebaecc14Sdanielk1977 3. References 17ebaecc14Sdanielk1977 18ebaecc14Sdanielk1977 19ebaecc14Sdanielk19771. SQL INTERFACE 20ebaecc14Sdanielk1977 21ebaecc14Sdanielk1977 1.1 Table Creation. 22ebaecc14Sdanielk1977 23ebaecc14Sdanielk1977 All r-tree virtual tables have an odd number of columns between 24ebaecc14Sdanielk1977 3 and 11. Unlike regular SQLite tables, r-tree tables are strongly 25ebaecc14Sdanielk1977 typed. 26ebaecc14Sdanielk1977 27ebaecc14Sdanielk1977 The leftmost column is always the pimary key and contains 64-bit 28ebaecc14Sdanielk1977 integer values. Each subsequent column contains a 32-bit real 29ebaecc14Sdanielk1977 value. For each pair of real values, the first (leftmost) must be 30*0d287cf7Sdrh less than or equal to the second. R-tree tables may be 31ebaecc14Sdanielk1977 constructed using the following syntax: 32ebaecc14Sdanielk1977 33ebaecc14Sdanielk1977 CREATE VIRTUAL TABLE <name> USING rtree(<column-names>) 34ebaecc14Sdanielk1977 35ebaecc14Sdanielk1977 For example: 36ebaecc14Sdanielk1977 37ebaecc14Sdanielk1977 CREATE VIRTUAL TABLE boxes USING rtree(boxno, xmin, xmax, ymin, ymax); 3872e87f44Sdrh INSERT INTO boxes VALUES(1, 1.0, 3.0, 2.0, 4.0); 39ebaecc14Sdanielk1977 40ebaecc14Sdanielk1977 Constructing a virtual r-tree table <name> creates the following three 41ebaecc14Sdanielk1977 real tables in the database to store the data structure: 42ebaecc14Sdanielk1977 43ebaecc14Sdanielk1977 <name>_node 44ebaecc14Sdanielk1977 <name>_rowid 45ebaecc14Sdanielk1977 <name>_parent 46ebaecc14Sdanielk1977 47ebaecc14Sdanielk1977 Dropping or modifying the contents of these tables directly will 48ebaecc14Sdanielk1977 corrupt the r-tree structure. To delete an r-tree from a database, 49ebaecc14Sdanielk1977 use a regular DROP TABLE statement: 50ebaecc14Sdanielk1977 51ebaecc14Sdanielk1977 DROP TABLE <name>; 52ebaecc14Sdanielk1977 53ebaecc14Sdanielk1977 Dropping the main r-tree table automatically drops the automatically 54ebaecc14Sdanielk1977 created tables. 55ebaecc14Sdanielk1977 56ebaecc14Sdanielk1977 1.2 Data Manipulation (INSERT, UPDATE, DELETE). 57ebaecc14Sdanielk1977 58ebaecc14Sdanielk1977 The usual INSERT, UPDATE or DELETE syntax is used to manipulate data 59ebaecc14Sdanielk1977 stored in an r-tree table. Please note the following: 60ebaecc14Sdanielk1977 61ebaecc14Sdanielk1977 * Inserting a NULL value into the primary key column has the 62ebaecc14Sdanielk1977 same effect as inserting a NULL into an INTEGER PRIMARY KEY 63ebaecc14Sdanielk1977 column of a regular table. The system automatically assigns 64ebaecc14Sdanielk1977 an unused integer key value to the new record. Usually, this 65ebaecc14Sdanielk1977 is one greater than the largest primary key value currently 66ebaecc14Sdanielk1977 present in the table. 67ebaecc14Sdanielk1977 68ebaecc14Sdanielk1977 * Attempting to insert a duplicate primary key value fails with 69ebaecc14Sdanielk1977 an SQLITE_CONSTRAINT error. 70ebaecc14Sdanielk1977 71ebaecc14Sdanielk1977 * Attempting to insert or modify a record such that the value 72ebaecc14Sdanielk1977 stored in the (N*2)th column is greater than that stored in 73ebaecc14Sdanielk1977 the (N*2+1)th column fails with an SQLITE_CONSTRAINT error. 74ebaecc14Sdanielk1977 75ebaecc14Sdanielk1977 * When a record is inserted, values are always converted to 76ebaecc14Sdanielk1977 the required type (64-bit integer or 32-bit real) as if they 77ebaecc14Sdanielk1977 were part of an SQL CAST expression. Non-numeric strings are 78ebaecc14Sdanielk1977 converted to zero. 79ebaecc14Sdanielk1977 80ebaecc14Sdanielk1977 1.3 Queries. 81ebaecc14Sdanielk1977 82ebaecc14Sdanielk1977 R-tree tables may be queried using all of the same SQL syntax supported 8358f1c8b7Sdrh by regular tables. However, some query patterns are more efficient 84ebaecc14Sdanielk1977 than others. 85ebaecc14Sdanielk1977 86ebaecc14Sdanielk1977 R-trees support fast lookup by primary key value (O(logN), like 87ebaecc14Sdanielk1977 regular tables). 88ebaecc14Sdanielk1977 89ebaecc14Sdanielk1977 Any combination of equality and range (<, <=, >, >=) constraints 90ebaecc14Sdanielk1977 on spatial data columns may be used to optimize other queries. This 91ebaecc14Sdanielk1977 is the key advantage to using r-tree tables instead of creating 92ebaecc14Sdanielk1977 indices on regular tables. 93ebaecc14Sdanielk1977 94ebaecc14Sdanielk1977 1.4 Introspection and Analysis. 95ebaecc14Sdanielk1977 96ebaecc14Sdanielk1977 TODO: Describe rtreenode() and rtreedepth() functions. 97ebaecc14Sdanielk1977 98ebaecc14Sdanielk1977 99ebaecc14Sdanielk19772. COMPILATION AND USAGE 100ebaecc14Sdanielk1977 10158f1c8b7Sdrh The easiest way to compile and use the RTREE extension is to build 102ebaecc14Sdanielk1977 and use it as a dynamically loadable SQLite extension. To do this 103ebaecc14Sdanielk1977 using gcc on *nix: 104ebaecc14Sdanielk1977 105ebaecc14Sdanielk1977 gcc -shared rtree.c -o libSqliteRtree.so 106ebaecc14Sdanielk1977 107ebaecc14Sdanielk1977 You may need to add "-I" flags so that gcc can find sqlite3ext.h 10858f1c8b7Sdrh and sqlite3.h. The resulting shared lib, libSqliteRtree.so, may be 109ebaecc14Sdanielk1977 loaded into sqlite in the same way as any other dynamicly loadable 110ebaecc14Sdanielk1977 extension. 111ebaecc14Sdanielk1977 112ebaecc14Sdanielk1977 113ebaecc14Sdanielk19773. REFERENCES 114ebaecc14Sdanielk1977 115ebaecc14Sdanielk1977 [1] Atonin Guttman, "R-trees - A Dynamic Index Structure For Spatial 116ebaecc14Sdanielk1977 Searching", University of California Berkeley, 1984. 117ebaecc14Sdanielk1977 118ebaecc14Sdanielk1977 [2] Norbert Beckmann, Hans-Peter Kriegel, Ralf Schneider, Bernhard Seeger, 119ebaecc14Sdanielk1977 "The R*-tree: An Efficient and Robust Access Method for Points and 120ebaecc14Sdanielk1977 Rectangles", Universitaet Bremen, 1990. 121