Example #1
0
    def test_xarray(self):

        from salem import open_xr_dataset

        go = get_demo_file('hef_srtm.tif')
        gs = get_demo_file('hef_srtm_subset.tif')

        geo = GeoTiff(go)
        go = open_xr_dataset(go)
        gs = open_xr_dataset(gs)

        gos = go.salem.subset(grid=gs.salem.grid)

        ref = gs['data']
        totest = gos['data']
        np.testing.assert_array_equal(ref.shape, (gos.salem.grid.ny, gos.salem.grid.nx))
        np.testing.assert_array_equal(ref.shape, totest.shape)
        np.testing.assert_array_equal(ref, totest)
        rlon, rlat = geo.grid.ll_coordinates
        tlon, tlat = go.salem.grid.ll_coordinates
        assert_allclose(rlon, tlon)
        assert_allclose(rlat, tlat)
Example #2
0
    def test_subset(self):
        """See if simple operations work well"""

        import shapely.geometry as shpg

        g = Grid(nxny=(3, 3), dxdy=(1, 1), x0y0=(0, 0), proj=wgs84)
        d = GeoDataset(g)
        self.assertTrue(isinstance(d, GeoDataset))
        self.assertEqual(g, d.grid)

        d.set_subset(corners=([0, 0], [2, 2]), crs=wgs84)
        self.assertEqual(g, d.grid)

        d.set_subset()
        self.assertEqual(g, d.grid)

        d.set_subset(margin=-1)
        lon, lat = d.grid.ll_coordinates
        self.assertEqual(lon, 1)
        self.assertEqual(lat, 1)

        d.set_subset(corners=([0.1, 0.1], [1.9, 1.9]), crs=wgs84)
        self.assertEqual(g, d.grid)

        d.set_subset(corners=([0.51, 0.51], [1.9, 1.9]), crs=wgs84)
        self.assertNotEqual(g, d.grid)

        gm = Grid(nxny=(1, 1), dxdy=(1, 1), x0y0=(1, 1), proj=wgs84)
        d.set_subset(corners=([1, 1], [1, 1]), crs=wgs84)
        self.assertEqual(gm, d.grid)

        d.set_subset()
        d.set_roi()
        d.set_roi(corners=([1, 1], [1, 1]), crs=wgs84)
        d.set_subset(toroi=True)
        self.assertEqual(gm, d.grid)

        gm = Grid(nxny=(1, 1), dxdy=(1, 1), x0y0=(2, 2), proj=wgs84)
        d.set_subset(corners=([2, 2], [2, 2]), crs=wgs84)
        self.assertEqual(gm, d.grid)

        with warnings.catch_warnings(record=True) as w:
            # Cause all warnings to always be triggered.
            warnings.simplefilter("always")
            # Trigger a warning.
            d.set_subset(corners=([-4, -4], [5, 5]), crs=wgs84)
            self.assertEqual(g, d.grid)
            # Verify some things
            assert len(w) >= 2

        self.assertRaises(RuntimeError,
                          d.set_subset,
                          corners=([-1, -1], [-1, -1]))
        self.assertRaises(RuntimeError, d.set_subset, corners=([5, 5], [5, 5]))

        shpf = get_demo_file('Hintereisferner.shp')
        reff = get_demo_file('hef_roi.tif')
        d = GeoTiff(reff)
        d.set_roi(shape=shpf)
        ref = d.get_vardata()
        # same errors as IDL: ENVI is just wrong
        self.assertTrue(np.sum(ref != d.roi) < 9)

        g = Grid(nxny=(3, 3),
                 dxdy=(1, 1),
                 x0y0=(0, 0),
                 proj=wgs84,
                 pixel_ref='corner')
        p = shpg.Polygon([(1.5, 1.), (2., 1.5), (1.5, 2.), (1., 1.5)])
        roi = g.region_of_interest(geometry=p)
        np.testing.assert_array_equal([[0, 0, 0], [0, 1, 0], [0, 0, 0]], roi)

        d = GeoDataset(g)
        d.set_roi(corners=([1.1, 1.1], [1.9, 1.9]))
        d.set_subset(toroi=True)
        np.testing.assert_array_equal([[1]], d.roi)
        d.set_subset()
        np.testing.assert_array_equal([[0, 0, 0], [0, 1, 0], [0, 0, 0]], d.roi)
        d.set_roi()
        np.testing.assert_array_equal([[0, 0, 0], [0, 0, 0], [0, 0, 0]], d.roi)

        # Raises
        self.assertRaises(RuntimeError, d.set_subset, toroi=True)
Example #3
0
    def test_subset(self):
        """Open geotiff, do subsets and stuff"""
        go = get_demo_file('hef_srtm.tif')
        gs = get_demo_file('hef_srtm_subset.tif')

        go = GeoTiff(go)
        gs = GeoTiff(gs)

        go.set_roi(grid=gs.grid)
        go.set_subset(toroi=True)
        ref = gs.get_vardata()
        totest = go.get_vardata()
        np.testing.assert_array_equal(ref.shape, (go.grid.ny, go.grid.nx))
        np.testing.assert_array_equal(ref.shape, totest.shape)
        np.testing.assert_array_equal(ref, totest)
        go.set_roi()
        go.set_subset()

        eps = 1e-5
        ex = gs.grid.extent_in_crs(crs=wgs84)  # [left, right, bot, top
        go.set_subset(corners=((ex[0], ex[2] + eps), (ex[1], ex[3] - eps)),
                      crs=wgs84,
                      margin=-2)
        ref = gs.get_vardata()[2:-2, 2:-2]
        totest = go.get_vardata()
        np.testing.assert_array_equal(ref.shape, totest.shape)
        np.testing.assert_array_equal(ref, totest)
        go.set_roi()
        go.set_subset()
Example #4
0
    def test_subset(self):
        """Open geotiff, do subsets and stuff"""
        go = get_demo_file("hef_srtm.tif")
        gs = get_demo_file("hef_srtm_subset.tif")

        go = GeoTiff(go)
        gs = GeoTiff(gs)

        go.set_roi(grid=gs.grid)
        go.set_subset(toroi=True)
        ref = gs.get_vardata()
        totest = go.get_vardata()
        np.testing.assert_array_equal(ref.shape, (go.grid.ny, go.grid.nx))
        np.testing.assert_array_equal(ref.shape, totest.shape)
        np.testing.assert_array_equal(ref, totest)
        go.set_roi()
        go.set_subset()

        eps = 1e-5
        ex = gs.grid.extent_in_crs(crs=wgs84)  # [left, right, bot, top
        go.set_subset(corners=((ex[0], ex[2] + eps), (ex[1], ex[3] - eps)), crs=wgs84, margin=-2)
        ref = gs.get_vardata()[2:-2, 2:-2]
        totest = go.get_vardata()
        np.testing.assert_array_equal(ref.shape, totest.shape)
        np.testing.assert_array_equal(ref, totest)
        go.set_roi()
        go.set_subset()