Skip to content

Commit 815f48f

Browse files
authored
Merge pull request #66 from geoscript/GSG-65.HPRtree
Add HPRtree spatial index
2 parents 13b7d7f + bbe6441 commit 815f48f

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package geoscript.index
2+
3+
import org.locationtech.jts.index.hprtree.HPRtree as JtsHPRtree
4+
5+
/**
6+
* Create a SpatialIndex using the Hilbert Packed Rtree (HPRtree) spatial index.
7+
* <p><blockquote><pre>
8+
* def index = new HPRtree()
9+
* index.insert(new Bounds(0,0,10,10), new Point(5,5))
10+
* index.insert(new Bounds(2,2,6,6), new Point(4,4))
11+
*
12+
* def results = index.query(new Bounds(1,1,5,5))
13+
* </pre></blockquote></p>
14+
* @author Jared Erickson
15+
*/
16+
class HPRtree extends SpatialIndex {
17+
18+
/**
19+
* Create a SpatialIndex using the HPR Tree spatial index.
20+
*/
21+
HPRtree() {
22+
super(new JtsHPRtree())
23+
}
24+
25+
}
26+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package geoscript.index
2+
3+
import geoscript.geom.Bounds
4+
import geoscript.geom.Point
5+
import org.junit.Test
6+
7+
import static org.junit.Assert.assertEquals
8+
import static org.junit.Assert.assertTrue
9+
10+
/**
11+
* The HPRtreeTestCase
12+
*/
13+
class HPRtreeTestCase {
14+
15+
@Test void index() {
16+
17+
def spatialIndex = new HPRtree()
18+
spatialIndex.insert(new Bounds(0,0,10,10), new Point(5,5))
19+
spatialIndex.insert(new Bounds(2,2,6,6), new Point(4,4))
20+
spatialIndex.insert(new Bounds(20,20,60,60), new Point(30,30))
21+
spatialIndex.insert(new Bounds(22,22,44,44), new Point(32,32))
22+
23+
assertEquals 4, spatialIndex.size
24+
25+
def results = spatialIndex.query(new Bounds(1,1,5,5))
26+
assertEquals 2, results.size()
27+
assertTrue(results[0].toString() == 'POINT (4 4)' || results[0].toString() == 'POINT (5 5)')
28+
assertTrue(results[1].toString() == 'POINT (4 4)' || results[1].toString() == 'POINT (5 5)')
29+
30+
results = spatialIndex.query(new Bounds(25,25,50,55))
31+
assertEquals 2, results.size()
32+
assertTrue(results[0].toString() == 'POINT (30 30)' || results[0].toString() == 'POINT (32 32)')
33+
assertTrue(results[1].toString() == 'POINT (30 30)' || results[1].toString() == 'POINT (32 32)')
34+
}
35+
}
36+

0 commit comments

Comments
 (0)