Пример #1
0
 def test_deepcopy(self):
     gvar = GeometryVariable(value=Point(1, 2), crs=Spherical(), dimensions='d')
     self.assertEqual(gvar.crs, Spherical())
     d = gvar.deepcopy()
     d.crs = WGS84()
     self.assertEqual(gvar.crs, Spherical())
     self.assertFalse(np.may_share_memory(gvar.get_value(), d.get_value()))
     self.assertIsNotNone(d.crs)
Пример #2
0
    def test_prepare(self):
        coords = (-10.0, 40.0, 50.0, 50.0)
        bbox = box(*coords)
        gvar = GeometryVariable(value=bbox, dimensions='geom', crs=Spherical(), is_bbox=True,
                                wrapped_state=WrappedState.UNWRAPPED)

        for _ in range(3):
            prepared = gvar.prepare()
            self.assertNotEqual(id(prepared), id(gvar))
            actual = []
            desired = [(-10.0, 40.0, 50.0, 50.0), (350.0, 40.0, 370.0, 50.0)]
            for g in prepared.get_value().flatten()[0]:
                actual.append(g.bounds)
            self.assertEqual(actual, desired)

        # Test updating the coordinate system.
        update_crs_call_count = {WGS84: 1, Spherical: 0, None: 1}
        wrapped_state = [None, WrappedState.WRAPPED, WrappedState.UNWRAPPED, WrappedState.UNKNOWN]
        keywords = dict(archetype_crs=update_crs_call_count.keys(), wrapped_state=wrapped_state)
        for k in self.iter_product_keywords(keywords):
            dgvar = deepcopy(gvar)
            dgvar.update_crs = mock.Mock()
            dgvar.deepcopy = mock.Mock(return_value=dgvar)
            dgvar.copy = mock.Mock()
            archetype = mock.create_autospec(GeometryVariable, spec_set=True)
            archetype.wrapped_state = k.wrapped_state
            try:
                archetype.crs = k.archetype_crs()
                archetype.crs.wrap_or_unwrap = mock.Mock()
            except TypeError:
                archetype.crs = None
            _ = dgvar.prepare(archetype=archetype)
            desired_count = update_crs_call_count[k.archetype_crs]
            self.assertEqual(dgvar.update_crs.call_count, desired_count)
            if desired_count > 0:
                dgvar.update_crs.assert_called_once_with(archetype.crs)
            if k.archetype_crs is not None and k.wrapped_state not in (WrappedState.UNKNOWN, None):
                archetype.crs.wrap_or_unwrap.assert_called_once_with(archetype.wrapped_state, dgvar)
            else:
                if k.archetype_crs is not None:
                    archetype.crs.wrap_or_unwrap.assert_not_called()
            dgvar.deepcopy.assert_called_once()
            dgvar.copy.assert_not_called()

        # Test identical object is returned if nothing happens.
        gvar = GeometryVariable()
        gvar.deepcopy = mock.Mock(spec=GeometryVariable.deepcopy)
        gvar.copy = mock.Mock(spec=GeometryVariable.copy)
        actual = gvar.prepare()
        self.assertNotEqual(id(actual), id(gvar))
        gvar.deepcopy.assert_not_called()
        gvar.copy.assert_called_once()

        # Test exception for more than one geometry.
        gvar = mock.create_autospec(GeometryVariable, spec_set=True)
        gvar.size = 2
        with self.assertRaises(RequestableFeature):
            GeometryVariable.prepare(gvar)
Пример #3
0
 def test_deepcopy(self):
     gvar = GeometryVariable(value=Point(1, 2),
                             crs=Spherical(),
                             dimensions='d')
     self.assertEqual(gvar.crs, Spherical())
     d = gvar.deepcopy()
     d.crs = WGS84()
     self.assertEqual(gvar.crs, Spherical())
     self.assertFalse(np.may_share_memory(gvar.get_value(), d.get_value()))
     self.assertIsNotNone(d.crs)
Пример #4
0
    def test_prepare(self):
        coords = (-10.0, 40.0, 50.0, 50.0)
        bbox = box(*coords)
        gvar = GeometryVariable(value=bbox,
                                dimensions='geom',
                                crs=Spherical(),
                                is_bbox=True,
                                wrapped_state=WrappedState.UNWRAPPED)

        for _ in range(3):
            prepared = gvar.prepare()
            self.assertNotEqual(id(prepared), id(gvar))
            actual = []
            desired = [(-10.0, 40.0, 50.0, 50.0), (350.0, 40.0, 370.0, 50.0)]
            for g in prepared.get_value().flatten()[0]:
                actual.append(g.bounds)
            self.assertEqual(actual, desired)

        # Test updating the coordinate system.
        update_crs_call_count = {WGS84: 1, Spherical: 0, None: 1}
        wrapped_state = [
            None, WrappedState.WRAPPED, WrappedState.UNWRAPPED,
            WrappedState.UNKNOWN
        ]
        keywords = dict(archetype_crs=update_crs_call_count.keys(),
                        wrapped_state=wrapped_state)
        for k in self.iter_product_keywords(keywords):
            dgvar = deepcopy(gvar)
            dgvar.update_crs = mock.Mock()
            dgvar.deepcopy = mock.Mock(return_value=dgvar)
            dgvar.copy = mock.Mock()
            archetype = mock.create_autospec(GeometryVariable, spec_set=True)
            archetype.wrapped_state = k.wrapped_state
            try:
                archetype.crs = k.archetype_crs()
                archetype.crs.wrap_or_unwrap = mock.Mock()
            except TypeError:
                archetype.crs = None
            _ = dgvar.prepare(archetype=archetype)
            desired_count = update_crs_call_count[k.archetype_crs]
            self.assertEqual(dgvar.update_crs.call_count, desired_count)
            if desired_count > 0:
                dgvar.update_crs.assert_called_once_with(archetype.crs)
            if k.archetype_crs is not None and k.wrapped_state not in (
                    WrappedState.UNKNOWN, None):
                archetype.crs.wrap_or_unwrap.assert_called_once_with(
                    archetype.wrapped_state, dgvar)
            else:
                if k.archetype_crs is not None:
                    archetype.crs.wrap_or_unwrap.assert_not_called()
            dgvar.deepcopy.assert_called_once()
            dgvar.copy.assert_not_called()

        # Test identical object is returned if nothing happens.
        gvar = GeometryVariable()
        gvar.deepcopy = mock.Mock(spec=GeometryVariable.deepcopy)
        gvar.copy = mock.Mock(spec=GeometryVariable.copy)
        actual = gvar.prepare()
        self.assertNotEqual(id(actual), id(gvar))
        gvar.deepcopy.assert_not_called()
        gvar.copy.assert_called_once()

        # Test exception for more than one geometry.
        gvar = mock.create_autospec(GeometryVariable, spec_set=True)
        gvar.size = 2
        with self.assertRaises(RequestableFeature):
            GeometryVariable.prepare(gvar)