示例#1
0
文件: test_geom.py 项目: NCPP/ocgis
    def test_get_mask_from_intersects(self):
        poly = wkt.loads(
            'POLYGON((-98.26574367088608142 40.19952531645570559,-98.71764240506330168 39.54825949367089066,-99.26257911392406186 39.16281645569620906,-99.43536392405064817 38.64446202531645724,-98.78409810126584034 38.33876582278481493,-98.23916139240508016 37.71408227848101546,-97.77397151898735217 37.67420886075949937,-97.62776898734178133 38.15268987341772799,-98.39865506329114453 38.52484177215190186,-98.23916139240508016 39.33560126582278826,-97.73409810126582897 39.58813291139241386,-97.52143987341773368 40.27927215189873777,-97.52143987341773368 40.27927215189873777,-98.26574367088608142 40.19952531645570559))'
        )
        desired_mask = np.array([[True, True, False, True],
                                 [True, False, True, True],
                                 [True, True, False, True]])

        dist = OcgDist()
        xdim = dist.create_dimension('x', 4, dist=True)
        ydim = dist.create_dimension('y', 3)
        dist.create_dimension('bounds', 2)
        dist.update_dimension_bounds()

        if MPI_RANK == 0:
            x = self.get_variable_x()
            y = self.get_variable_y()
            grid = Grid(x=x, y=y, abstraction='point', crs=WGS84())
            pa = get_geometry_variable(grid)
        else:
            pa = None
        pa = variable_scatter(pa, dist)

        vm.create_subcomm_by_emptyable('test_get_mask_from_intersects',
                                       pa,
                                       is_current=True)
        if vm.is_null:
            self.assertTrue(pa.is_empty)
            return

        usi = [False]
        if env.USE_SPATIAL_INDEX:
            usi.append(True)

        keywords = dict(use_spatial_index=usi)
        for k in self.iter_product_keywords(keywords):
            ret = pa.get_mask_from_intersects(
                poly, use_spatial_index=k.use_spatial_index)
            desired_mask_local = desired_mask[slice(*ydim.bounds_local),
                                              slice(*xdim.bounds_local)]
            if MPI_RANK > 1:
                self.assertIsNone(ret)
            else:
                self.assertNumpyAll(desired_mask_local, ret)

            # This does not test a parallel operation.
            if MPI_RANK == 0:
                # Test pre-masked values in geometry are okay for intersects operation.
                value = [Point(1, 1), Point(2, 2), Point(3, 3)]
                value = np.ma.array(value,
                                    mask=[False, True, False],
                                    dtype=object)
                pa2 = GeometryVariable(value=value, dimensions='ngeom')
                b = box(0, 0, 5, 5)
                res = pa2.get_mask_from_intersects(
                    b, use_spatial_index=k.use_spatial_index)
                self.assertNumpyAll(res, value.mask)
示例#2
0
文件: test_geom.py 项目: NCPP/ocgis
    def test_init(self):
        row = Variable(value=[2, 3], name='row', dimensions='y')
        col = Variable(value=[4, 5], name='col', dimensions='x')
        grid = Grid(col, row)
        self.assertIsNone(grid.archetype.bounds)

        row = Variable(value=[2, 3], name='row', dimensions='y')
        row.set_extrapolated_bounds('row_bounds', 'bounds')
        col = Variable(value=[4, 5], name='col', dimensions='x')
        col.set_extrapolated_bounds('col_bounds', 'bounds')
        grid = Grid(y=row, x=col)
        self.assertEqual(grid.abstraction, 'polygon')
        poly = get_geometry_variable(grid)
        self.assertEqual(poly.geom_type, 'Polygon')
示例#3
0
文件: test_geom.py 项目: NCPP/ocgis
    def test_init(self):
        row = Variable(value=[2, 3], name='row', dimensions='y')
        col = Variable(value=[4, 5], name='col', dimensions='x')
        grid = Grid(col, row)
        self.assertIsNone(grid.archetype.bounds)

        row = Variable(value=[2, 3], name='row', dimensions='y')
        row.set_extrapolated_bounds('row_bounds', 'bounds')
        col = Variable(value=[4, 5], name='col', dimensions='x')
        col.set_extrapolated_bounds('col_bounds', 'bounds')
        grid = Grid(y=row, x=col)
        self.assertEqual(grid.abstraction, 'polygon')
        poly = get_geometry_variable(grid)
        self.assertEqual(poly.geom_type, 'Polygon')
示例#4
0
文件: test_geom.py 项目: NCPP/ocgis
    def test_get_mask_from_intersects(self):
        poly = wkt.loads(
            'POLYGON((-98.26574367088608142 40.19952531645570559,-98.71764240506330168 39.54825949367089066,-99.26257911392406186 39.16281645569620906,-99.43536392405064817 38.64446202531645724,-98.78409810126584034 38.33876582278481493,-98.23916139240508016 37.71408227848101546,-97.77397151898735217 37.67420886075949937,-97.62776898734178133 38.15268987341772799,-98.39865506329114453 38.52484177215190186,-98.23916139240508016 39.33560126582278826,-97.73409810126582897 39.58813291139241386,-97.52143987341773368 40.27927215189873777,-97.52143987341773368 40.27927215189873777,-98.26574367088608142 40.19952531645570559))')
        desired_mask = np.array([[True, True, False, True],
                                 [True, False, True, True],
                                 [True, True, False, True]])

        dist = OcgDist()
        xdim = dist.create_dimension('x', 4, dist=True)
        ydim = dist.create_dimension('y', 3)
        dist.create_dimension('bounds', 2)
        dist.update_dimension_bounds()

        if MPI_RANK == 0:
            x = self.get_variable_x()
            y = self.get_variable_y()
            grid = Grid(x=x, y=y, abstraction='point', crs=WGS84())
            pa = get_geometry_variable(grid)
        else:
            pa = None
        pa = variable_scatter(pa, dist)

        vm.create_subcomm_by_emptyable('test_get_mask_from_intersects', pa, is_current=True)
        if vm.is_null:
            self.assertTrue(pa.is_empty)
            return

        usi = [False]
        if env.USE_SPATIAL_INDEX:
            usi.append(True)

        keywords = dict(use_spatial_index=usi)
        for k in self.iter_product_keywords(keywords):
            ret = pa.get_mask_from_intersects(poly, use_spatial_index=k.use_spatial_index)
            desired_mask_local = desired_mask[slice(*ydim.bounds_local), slice(*xdim.bounds_local)]
            if MPI_RANK > 1:
                self.assertIsNone(ret)
            else:
                self.assertNumpyAll(desired_mask_local, ret)

            # This does not test a parallel operation.
            if MPI_RANK == 0:
                # Test pre-masked values in geometry are okay for intersects operation.
                value = [Point(1, 1), Point(2, 2), Point(3, 3)]
                value = np.ma.array(value, mask=[False, True, False], dtype=object)
                pa2 = GeometryVariable(value=value, dimensions='ngeom')
                b = box(0, 0, 5, 5)
                res = pa2.get_mask_from_intersects(b, use_spatial_index=k.use_spatial_index)
                self.assertNumpyAll(res, value.mask)
示例#5
0
文件: test_geom.py 项目: NCPP/ocgis
    def test_getitem(self):
        gridxy = self.get_gridxy()
        pa = get_geometry_variable(gridxy)
        self.assertEqual(pa.shape, (4, 3))
        self.assertEqual(pa.ndim, 2)
        self.assertIsNotNone(pa._value)
        sub = pa[2:4, 1]
        self.assertEqual(sub.shape, (2, 1))
        self.assertEqual(sub.get_value().shape, (2, 1))

        # Test slicing with a parent.
        pa = self.get_geometryvariable_with_parent()
        desired_obj = pa.parent['tas']
        self.assertIsNotNone(pa.parent)
        desired = desired_obj[:, 1].get_value()
        self.assertIsNotNone(pa.parent)
        desired_shapes = OrderedDict([('tas', (10, 3)), ('point', (3,))])
        self.assertEqual(pa.parent.shapes, desired_shapes)

        sub = pa[1]
        backref_tas = sub.parent['tas']
        self.assertNumpyAll(backref_tas.get_value(), desired)
        self.assertEqual(backref_tas.shape, (10, 1))
示例#6
0
文件: test_geom.py 项目: NCPP/ocgis
    def test_getitem(self):
        gridxy = self.get_gridxy()
        pa = get_geometry_variable(gridxy)
        self.assertEqual(pa.shape, (4, 3))
        self.assertEqual(pa.ndim, 2)
        self.assertIsNotNone(pa._value)
        sub = pa[2:4, 1]
        self.assertEqual(sub.shape, (2, 1))
        self.assertEqual(sub.get_value().shape, (2, 1))

        # Test slicing with a parent.
        pa = self.get_geometryvariable_with_parent()
        desired_obj = pa.parent['tas']
        self.assertIsNotNone(pa.parent)
        desired = desired_obj[:, 1].get_value()
        self.assertIsNotNone(pa.parent)
        desired_shapes = OrderedDict([('tas', (10, 3)), ('point', (3, ))])
        self.assertEqual(pa.parent.shapes, desired_shapes)

        sub = pa[1]
        backref_tas = sub.parent['tas']
        self.assertNumpyAll(backref_tas.get_value(), desired)
        self.assertEqual(backref_tas.shape, (10, 1))
示例#7
0
文件: test_geom.py 项目: NCPP/ocgis
    def test_get_intersects(self):
        dist = OcgDist()
        dist.create_dimension('x', 5, dist=False)
        dist.create_dimension('y', 5, dist=True)
        dist.update_dimension_bounds()

        if MPI_RANK == 0:
            x = Variable(value=[1, 2, 3, 4, 5], name='x', dimensions=['x'])
            y = Variable(value=[10, 20, 30, 40, 50], name='y', dimensions=['y'])
        else:
            x, y = [None] * 2

        x = variable_scatter(x, dist)
        y = variable_scatter(y, dist)

        if MPI_RANK < 2:
            self.assertTrue(y.dimensions[0].dist)

        grid = Grid(x=x, y=y)
        if not grid.is_empty:
            self.assertTrue(grid.dimensions[0].dist)
        pa = get_geometry_variable(grid)
        if MPI_RANK >= 2:
            self.assertTrue(pa.is_empty)
        polygon = box(2.5, 15, 4.5, 45)
        if not grid.is_empty:
            self.assertTrue(pa.dimensions[0].dist)

        # if MPI_RANK == 0:
        #     self.write_fiona_htmp(GeometryVariable(value=polygon), 'polygon')
        # self.write_fiona_htmp(grid.abstraction_geometry, 'grid-{}'.format(MPI_RANK))

        # Try an empty subset.
        live_ranks = vm.get_live_ranks_from_object(pa)
        vm.create_subcomm('test_get_intersects', live_ranks, is_current=True)
        if not vm.is_null:
            with self.assertRaises(EmptySubsetError):
                pa.get_intersects(Point(-8000, 9000))

            sub, slc = pa.get_intersects(polygon, return_slice=True)
        else:
            sub, slc = [None] * 2

        # self.write_fiona_htmp(sub, 'sub-{}'.format(MPI_RANK))

        if MPI_SIZE == 1:
            self.assertEqual(sub.shape, (3, 2))
        else:
            # This is the non-distributed dimension.
            if MPI_SIZE == 2:
                self.assertEqual(sub.shape[1], 2)
            # This is the distributed dimension.
            if MPI_RANK in live_ranks:
                if MPI_RANK < 5:
                    self.assertNotEqual(sub.shape[0], 3)
                else:
                    self.assertTrue(sub.is_empty)

        if not vm.is_null:
            desired_points_slc = pa.get_distributed_slice(slc).get_value()

            desired_points_manual = [Point(x, y) for x, y in
                                     itertools.product(grid.x.get_value().flat, grid.y.get_value().flat)]
            desired_points_manual = [pt for pt in desired_points_manual if pt.intersects(polygon)]
            for desired_points in [desired_points_manual, desired_points_slc.flat]:
                for pt in desired_points:
                    found = False
                    for pt_actual in sub.get_value().flat:
                        if pt_actual.almost_equals(pt):
                            found = True
                            break
                    self.assertTrue(found)

        # Test w/out an associated grid.
        if not vm.is_null:
            pa = self.get_geometryvariable(dimensions='ngeom')
            polygon = box(0.5, 1.5, 1.5, 2.5)
            sub = pa.get_intersects(polygon)
            self.assertEqual(sub.shape, (1,))
            self.assertEqual(sub.get_value()[0], Point(1, 2))
示例#8
0
文件: test_geom.py 项目: NCPP/ocgis
    def test_get_intersects(self):
        dist = OcgDist()
        dist.create_dimension('x', 5, dist=False)
        dist.create_dimension('y', 5, dist=True)
        dist.update_dimension_bounds()

        if MPI_RANK == 0:
            x = Variable(value=[1, 2, 3, 4, 5], name='x', dimensions=['x'])
            y = Variable(value=[10, 20, 30, 40, 50],
                         name='y',
                         dimensions=['y'])
        else:
            x, y = [None] * 2

        x = variable_scatter(x, dist)
        y = variable_scatter(y, dist)

        if MPI_RANK < 2:
            self.assertTrue(y.dimensions[0].dist)

        grid = Grid(x=x, y=y)
        if not grid.is_empty:
            self.assertTrue(grid.dimensions[0].dist)
        pa = get_geometry_variable(grid)
        if MPI_RANK >= 2:
            self.assertTrue(pa.is_empty)
        polygon = box(2.5, 15, 4.5, 45)
        if not grid.is_empty:
            self.assertTrue(pa.dimensions[0].dist)

        # if MPI_RANK == 0:
        #     self.write_fiona_htmp(GeometryVariable(value=polygon), 'polygon')
        # self.write_fiona_htmp(grid.abstraction_geometry, 'grid-{}'.format(MPI_RANK))

        # Try an empty subset.
        live_ranks = vm.get_live_ranks_from_object(pa)
        vm.create_subcomm('test_get_intersects', live_ranks, is_current=True)
        if not vm.is_null:
            with self.assertRaises(EmptySubsetError):
                pa.get_intersects(Point(-8000, 9000))

            sub, slc = pa.get_intersects(polygon, return_slice=True)
        else:
            sub, slc = [None] * 2

        # self.write_fiona_htmp(sub, 'sub-{}'.format(MPI_RANK))

        if MPI_SIZE == 1:
            self.assertEqual(sub.shape, (3, 2))
        else:
            # This is the non-distributed dimension.
            if MPI_SIZE == 2:
                self.assertEqual(sub.shape[1], 2)
            # This is the distributed dimension.
            if MPI_RANK in live_ranks:
                if MPI_RANK < 5:
                    self.assertNotEqual(sub.shape[0], 3)
                else:
                    self.assertTrue(sub.is_empty)

        if not vm.is_null:
            desired_points_slc = pa.get_distributed_slice(slc).get_value()

            desired_points_manual = [
                Point(x, y)
                for x, y in itertools.product(grid.x.get_value().flat,
                                              grid.y.get_value().flat)
            ]
            desired_points_manual = [
                pt for pt in desired_points_manual if pt.intersects(polygon)
            ]
            for desired_points in [
                    desired_points_manual, desired_points_slc.flat
            ]:
                for pt in desired_points:
                    found = False
                    for pt_actual in sub.get_value().flat:
                        if pt_actual.almost_equals(pt):
                            found = True
                            break
                    self.assertTrue(found)

        # Test w/out an associated grid.
        if not vm.is_null:
            pa = self.get_geometryvariable(dimensions='ngeom')
            polygon = box(0.5, 1.5, 1.5, 2.5)
            sub = pa.get_intersects(polygon)
            self.assertEqual(sub.shape, (1, ))
            self.assertEqual(sub.get_value()[0], Point(1, 2))