-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRTreeTests.java
More file actions
96 lines (74 loc) · 2.81 KB
/
RTreeTests.java
File metadata and controls
96 lines (74 loc) · 2.81 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.almworks.sqlite4java;
public class RTreeTests extends SQLiteConnectionFixture {
public void testRTree() throws SQLiteException {
SQLiteConnection connection = memDb().open();
connection.exec("create virtual table rtree_example using rtree(id, minX, maxX, minY, maxY)");
final int count = 1234;
Box surroundingBox = new Box(-100, 100, -100, 100);
fillRTree(connection, count, surroundingBox);
int realCount = countObjectsInBox(connection, surroundingBox);
assertEquals(count, realCount);
}
private void fillRTree(SQLiteConnection connection, int count, Box surroundingBox) throws SQLiteException {
SQLiteStatement fillStatement = connection.prepare("insert into rtree_example values (?,?,?,?,?)");
for (int i = 0; i < count; i++) {
Box newNode = generateNode(surroundingBox);
fillStatement.bind(1, i);
fillStatement.bind(2, newNode.getMinX());
fillStatement.bind(3, newNode.getMaxX());
fillStatement.bind(4, newNode.getMinY());
fillStatement.bind(5, newNode.getMaxY());
fillStatement.step();
fillStatement.reset();
}
fillStatement.dispose();
}
private Box generateNode(Box surroundingBox) {
double nodeHeight = surroundingBox.getHeight() / 1e6;
double nodeWidth = surroundingBox.getWidth() / 1e6;
double newCenterX = Math.random() * surroundingBox.getWidth() + surroundingBox.getMinX();
double newCenterY = Math.random() * surroundingBox.getHeight() + surroundingBox.getMinY();
return new Box(newCenterX - nodeWidth, newCenterX + nodeWidth, newCenterY - nodeHeight, newCenterY + nodeHeight);
}
private int countObjectsInBox(SQLiteConnection connection, Box surroundingBox) throws SQLiteException {
SQLiteStatement selectStatement = connection.prepare("select count(*) from rtree_example where minX >= ? and maxX <= ? and minY >=? and maxY <= ?");
selectStatement.bind(1, surroundingBox.getMinX());
selectStatement.bind(2, surroundingBox.getMaxX());
selectStatement.bind(3, surroundingBox.getMinY());
selectStatement.bind(4, surroundingBox.getMaxY());
selectStatement.step();
int count = selectStatement.columnInt(0);
selectStatement.dispose();
return count;
}
private class Box {
double myMinX;
double myMinY;
double myMaxX;
double myMaxY;
Box(double minX, double maxX, double minY, double maxY) {
myMinX = minX;
myMinY = minY;
myMaxX = maxX;
myMaxY = maxY;
}
public double getMinX() {
return myMinX;
}
public double getMinY() {
return myMinY;
}
public double getMaxX() {
return myMaxX;
}
public double getMaxY() {
return myMaxY;
}
public double getWidth() {
return myMaxX - myMinX;
}
public double getHeight() {
return myMaxY - myMinY;
}
}
}