Ejemplo n.º 1
0
    def test_wrap(self):
        """Test different geometry types are appropriately wrapped."""

        wrapper = GeometryWrapper()
        for desc, geom in self.possible.items():
            unwrapped = wrapper.unwrap(geom)
            wrapped = wrapper.wrap(unwrapped)
            try:
                self.assertTrue(geom.almost_equals(wrapped))
            except AssertionError:
                # the axis multipolygon when wrapped will have an extra polygon as the split portion on the axis will
                # be in two parts
                if desc == 'axis_multipolygon':
                    self.assertNumpyAllClose(np.array(wrapped.bounds),
                                             np.array(geom.bounds))
                    self.assertEqual(len(wrapped), 4)
                # polygon will also be split...
                elif desc == 'axis_polygon':
                    self.assertNumpyAllClose(np.array(wrapped.bounds),
                                             np.array(geom.bounds))
                    self.assertEqual(len(wrapped), 2)
                else:
                    # gv = GeometryVariable.from_shapely(geom, crs=Spherical())
                    # gv.write_vector(os.path.join('/tmp/tw-geom.shp'))
                    # gv = GeometryVariable.from_shapely(wrapped, crs=Spherical())
                    # gv.write_vector(os.path.join('/tmp/tw-wrapped.shp'))
                    raise
Ejemplo n.º 2
0
    def test_unwrap_multipolygon(self):
        """Test unwrapping a multi-polygon with one piece untouched and the other unwrapped."""

        lhs = box(-180, 30, -177, 35)
        rhs = box(160, 30, 180, 35)
        geom = MultiPolygon([lhs, rhs])
        wrapper = GeometryWrapper()
        unwrapped = wrapper.unwrap(geom)
        self.assertEqual(unwrapped.bounds, (160.0, 30.0, 183.0, 35.0))
Ejemplo n.º 3
0
    def wrap_or_unwrap(self, action, target):
        from ocgis.variable.geom import GeometryVariable
        from ocgis.spatial.grid import Grid

        if action not in (WrapAction.WRAP, WrapAction.UNWRAP):
            raise ValueError('"action" not recognized: {}'.format(action))

        if action == WrapAction.WRAP:
            attr = 'wrap'
        else:
            attr = 'unwrap'

        if isinstance(target, GeometryVariable):
            w = GeometryWrapper()
            func = getattr(w, attr)
            target_value = target.get_value()
            for idx, target_geom in iter_array(target_value, use_mask=True, return_value=True,
                                               mask=target.get_mask()):
                target_value.__setitem__(idx, func(target_geom))
        elif isinstance(target, Grid):
            ca = CoordinateArrayWrapper()
            func = getattr(ca, attr)
            func(target.x.get_value())
            target.remove_bounds()
            if target.has_allocated_point:
                getattr(target.get_point(), attr)()
            if target.has_allocated_polygon:
                getattr(target.get_polygon(), attr)()
        else:
            raise NotImplementedError(target)
Ejemplo n.º 4
0
    def test_unwrap(self):
        """Test different geometry types are appropriately unwrapped."""

        wrapper = GeometryWrapper()
        # path = tempfile.mkdtemp()
        for desc, geom in self.possible.items():
            unwrapped = wrapper.unwrap(geom)
            if desc in self.actual_unwrapped:
                try:
                    self.assertTrue(self.actual_unwrapped[desc].almost_equals(
                        unwrapped, decimal=5))
                except:
                    # gv = GeometryVariable.from_shapely(geom, crs=Spherical())
                    # gv.write_vector(os.path.join('/tmp/tw-geom.shp'))
                    # gv = GeometryVariable.from_shapely(unwrapped, crs=Spherical())
                    # gv.write_vector(os.path.join('/tmp/tw-unwrapped.shp'))
                    # gv = GeometryVariable.from_shapely(self.actual_unwrapped[desc], crs=Spherical())
                    # gv.write_vector(os.path.join('/tmp/tw-actual-unwrapped.shp'))
                    # print(unwrapped.wkt)
                    raise
            try:
                self.assertEqual(type(geom), type(unwrapped))
            except AssertionError:
                if desc == 'axis_polygon':
                    # by necessity of being split on the axis, this will come out as a multipolygon
                    self.assertIsInstance(unwrapped, MultiPolygon)
                else:
                    raise

            if isinstance(unwrapped, Polygon):
                coords = np.array(unwrapped.exterior)
            elif isinstance(unwrapped, MultiPolygon):
                coords = np.array(
                    [np.array(u.exterior).min() for u in unwrapped])
            else:
                coords = np.array(unwrapped)
            self.assertFalse(np.any(coords < 0.0))
            if isinstance(unwrapped, (MultiPolygon, Polygon)):
                it = get_iter(unwrapped)
                for polygon in it:
                    self.assertFalse(
                        np.any(np.array(polygon.exterior) > 360.0))
            else:
                self.assertFalse(np.any(np.array(unwrapped) > 360.0))
Ejemplo n.º 5
0
 def test_init(self):
     GeometryWrapper()