diff --git a/doc/api/index.rst b/doc/api/index.rst index 7ce4903..78b475f 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -9,6 +9,7 @@ API Reference proj feature/index filter + spatial_index function process layer/index diff --git a/doc/api/spatial_index.rst b/doc/api/spatial_index.rst new file mode 100644 index 0000000..31d8fa9 --- /dev/null +++ b/doc/api/spatial_index.rst @@ -0,0 +1,13 @@ +.. module:: spatial_index + :synopsis: Spatial index. + +spatial index +============= + + .. automodule:: geoscript.index + + .. autoclass:: QuadTree + :members: size, insert, query, queryAll, remove + + .. autoclass:: STRtree + :members: size, insert, query \ No newline at end of file diff --git a/geoscript/index/__init__.py b/geoscript/index/__init__.py new file mode 100644 index 0000000..58b5576 --- /dev/null +++ b/geoscript/index/__init__.py @@ -0,0 +1,2 @@ +from quadtree import QuadTree +from strtree import STRtree \ No newline at end of file diff --git a/geoscript/index/quadtree.py b/geoscript/index/quadtree.py new file mode 100644 index 0000000..ca7a946 --- /dev/null +++ b/geoscript/index/quadtree.py @@ -0,0 +1,14 @@ + +from geoscript.index.spatialindex import SpatialIndex +from org.locationtech.jts.index.quadtree import Quadtree as JtsQuadtree + +class QuadTree(SpatialIndex): + + def __init__(self): + SpatialIndex.__init__(self, JtsQuadtree()) + + def queryAll(self): + return self.index.queryAll() + + def remove(self, bounds, item): + return self.index.remove(bounds, item) \ No newline at end of file diff --git a/geoscript/index/spatialindex.py b/geoscript/index/spatialindex.py new file mode 100644 index 0000000..d8c37e1 --- /dev/null +++ b/geoscript/index/spatialindex.py @@ -0,0 +1,13 @@ +class SpatialIndex: + + def __init__(self, index): + self.index = index + + def size(self): + return self.index.size() + + def insert(self, bounds, item): + self.index.insert(bounds, item) + + def query(self, bounds): + return self.index.query(bounds) \ No newline at end of file diff --git a/geoscript/index/strtree.py b/geoscript/index/strtree.py new file mode 100644 index 0000000..e3e02cc --- /dev/null +++ b/geoscript/index/strtree.py @@ -0,0 +1,7 @@ +from geoscript.index.spatialindex import SpatialIndex +from org.locationtech.jts.index.strtree import STRtree as JtsSTRtree + +class STRtree(SpatialIndex): + + def __init__(self): + SpatialIndex.__init__(self, JtsSTRtree()) \ No newline at end of file diff --git a/tests/test_index.py b/tests/test_index.py new file mode 100644 index 0000000..9e4db46 --- /dev/null +++ b/tests/test_index.py @@ -0,0 +1,37 @@ +import unittest +from geoscript.index import STRtree, QuadTree +from geoscript.geom import Bounds, Point + +class IndexTest(unittest.TestCase): + + def testQuadTree(self): + index = QuadTree() + index.insert(Bounds(0,0,10,10), Point(5,5)) + index.insert(Bounds(2,2,6,6), Point(4,4)) + index.insert(Bounds(20,20,60,60), Point(30,30)) + index.insert(Bounds(22,22,44,44), Point(32,32)) + self.assertEqual(4, index.size()) + + results = index.query(Bounds(1,1,5,5)) + self.assertEquals(4, len(results)) + + allResults = index.queryAll() + self.assertEquals(4, len(allResults)) + + isRemoved = index.remove(Bounds(22,22,44,44), Point(32,32)) + self.assertTrue(isRemoved) + + allResults = index.queryAll() + self.assertEquals(3, len(allResults)) + + def testSTRtree(self): + index = STRtree() + index.insert(Bounds(0,0,10,10), Point(5,5)) + index.insert(Bounds(2,2,6,6), Point(4,4)) + index.insert(Bounds(20,20,60,60), Point(30,30)) + index.insert(Bounds(22,22,44,44), Point(32,32)) + self.assertEqual(4, index.size()) + + results = index.query(Bounds(1,1,5,5)) + self.assertEquals(2, len(results)) +