def test_add_sequence(self): si = SpatialIndex() ids = [1, 2] geoms = [self.geom_michigan, self.geom_michigan] si.add(ids, geoms) ids = list(si._index.intersection(self.geom_michigan.bounds)) self.assertEqual([1, 2], ids)
def test_rtree(self): from ocgis.spatial.index import SpatialIndex geom_mapping = {1: Point(1, 2)} si = SpatialIndex() si.add(1, Point(1, 2)) ret = list(si.iter_intersects(Point(1, 2), geom_mapping)) self.assertEqual(ret, [1])
def test_iter_intersects_with_polygon(self): polygon = self.geom_michigan[1] si = SpatialIndex() points = self.geom_michigan_point_grid ids = list(points.keys()) geoms = [points[i] for i in ids] si.add(ids, geoms) intersects_ids = list(si.iter_intersects(polygon, points)) self.assertEqual(intersects_ids, [67])
def test_iter_intersects(self): points = self.geom_michigan_point_grid si = SpatialIndex() ids = list(points.keys()) geoms = [points[i] for i in ids] si.add(ids, geoms) intersects_ids = list(si.iter_intersects(self.geom_michigan, points)) self.assertEqual( set(intersects_ids), set([ 22, 23, 24, 32, 33, 34, 35, 36, 42, 43, 44, 46, 56, 66, 67, 76 ]))
def test_get_intersection_rtree(self): points = self.geom_michigan_point_grid si = SpatialIndex() ids = list(points.keys()) geoms = [points[i] for i in ids] si.add(ids, geoms) ids = list(si._get_intersection_rtree_(self.geom_michigan)) self.assertEqual( set(ids), set([ 12, 13, 14, 15, 16, 17, 22, 23, 24, 25, 26, 27, 32, 33, 34, 35, 36, 37, 42, 43, 44, 45, 46, 47, 52, 53, 54, 55, 56, 57, 62, 63, 64, 65, 66, 67, 72, 73, 74, 75, 76, 77, 82, 83, 84, 85, 86, 87 ]))
def test_keep_touches(self): points = self.geom_michigan_point_grid si = SpatialIndex() ids = list(points.keys()) geoms = [points[i] for i in ids] si.add(ids, geoms) touch_geom = Point( *mapping(self.geom_michigan)['coordinates'][0][0][3]) si.add(1000, touch_geom) points[1000] = touch_geom for keep_touches in [True, False]: intersects_ids = list( si.iter_intersects(self.geom_michigan, points, keep_touches=keep_touches)) if keep_touches: self.assertIn(1000, intersects_ids) else: self.assertNotIn(1000, intersects_ids)
def get_spatial_index(self, target=None): """ :param target: If this is a boolean array, use this as the add target. Otherwise, use the compressed masked values. :type target: :class:`numpy.ndarray` :return: spatial index for the geometry variable :rtype: :class:`rtree.index.Index` """ # "rtree" is an optional dependency. from ocgis.spatial.index import SpatialIndex # Fill the spatial index with unmasked values only. si = SpatialIndex() # Use compressed masked values if target is not available. if target is None: target = self.get_masked_value().compressed() # Add the geometries to the index. r_add = si.add for idx, geom in iter_array(target, return_value=True): r_add(idx[0], geom) return si
def test_add_point(self): pt = self.geom_michigan.centroid si = SpatialIndex() si.add(1, pt) self.assertEqual(tuple(si._index.bounds), pt.bounds)
def test_add_polygon(self): si = SpatialIndex() si.add(1, self.geom_michigan) self.assertEqual(tuple(si._index.bounds), self.geom_michigan.bounds)
def test_constructor(self): SpatialIndex()