Beispiel #1
0
def test_dem_subtraction():
    """Test that the DEM subtraction script gives reasonable numbers."""
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=DeprecationWarning)
        diff = xdem.spatial_tools.subtract_rasters(
            examples.get_path("longyearbyen_ref_dem"),
            examples.get_path("longyearbyen_tba_dem"))

    assert np.nanmean(np.abs(diff.data)) < 100
Beispiel #2
0
def load_ref_and_diff() -> tuple[gu.georaster.Raster, gu.georaster.Raster, np.ndarray]:
    """Load example files to try coregistration methods with."""

    reference_raster = gu.georaster.Raster(examples.get_path("longyearbyen_ref_dem"))
    glacier_mask = gu.geovector.Vector(examples.get_path("longyearbyen_glacier_outlines"))

    ddem = gu.georaster.Raster(examples.get_path('longyearbyen_ddem'))
    mask = glacier_mask.create_mask(ddem)

    return reference_raster, ddem, mask
Beispiel #3
0
def load_examples(
) -> tuple[gu.georaster.Raster, gu.georaster.Raster, gu.geovector.Vector]:
    """Load example files to try coregistration methods with."""
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        reference_raster = gu.georaster.Raster(
            examples.get_path("longyearbyen_ref_dem"))
        to_be_aligned_raster = gu.georaster.Raster(
            examples.get_path("longyearbyen_tba_dem"))
        glacier_mask = gu.geovector.Vector(
            examples.get_path("longyearbyen_glacier_outlines"))

    return reference_raster, to_be_aligned_raster, glacier_mask
Beispiel #4
0
class TestMerging:
    """
    Test cases for stacking and merging DEMs
    Split a DEM with some overlap, then stack/merge it, and validate bounds and shape.
    """
    dem = gu.georaster.Raster(examples.get_path("longyearbyen_ref_dem"))

    # Find the easting midpoint of the DEM
    x_midpoint = np.mean([dem.bounds.right, dem.bounds.left])
    x_midpoint -= x_midpoint % dem.res[0]

    # Cut the DEM into two DEMs that slightly overlap each other.
    dem1 = dem.copy()
    dem1.crop(
        rio.coords.BoundingBox(
            right=x_midpoint + dem.res[0] * 3, left=dem.bounds.left, top=dem.bounds.top, bottom=dem.bounds.bottom
        )
    )
    dem2 = dem.copy()
    dem2.crop(
        rio.coords.BoundingBox(
            left=x_midpoint - dem.res[0] * 3, right=dem.bounds.right, top=dem.bounds.top, bottom=dem.bounds.bottom
        )
    )

    # To check that use_ref_bounds work - create a DEM that do not cover the whole extent
    dem3 = dem.copy()
    dem3.crop(
        rio.coords.BoundingBox(
            left=x_midpoint - dem.res[0] * 3,
            right=dem.bounds.right - dem.res[0] * 2,
            top=dem.bounds.top,
            bottom=dem.bounds.bottom,
        )
    )

    def test_stack_rasters(self):
        """Test stack_rasters"""
        # Merge the two overlapping DEMs and check that output bounds and shape is correct
        stacked_dem = xdem.spatial_tools.stack_rasters([self.dem1, self.dem2])

        assert stacked_dem.count == 2
        assert self.dem.shape == stacked_dem.shape

        merged_bounds = xdem.spatial_tools.merge_bounding_boxes(
            [self.dem1.bounds, self.dem2.bounds], resolution=self.dem1.res[0]
        )
        assert merged_bounds == stacked_dem.bounds

        # Check that reference works with input Raster
        stacked_dem = xdem.spatial_tools.stack_rasters([self.dem1, self.dem2], reference=self.dem)
        assert self.dem.bounds == stacked_dem.bounds

        # Others than int or gu.Raster should raise a ValueError
        try:
            stacked_dem = xdem.spatial_tools.stack_rasters([self.dem1, self.dem2], reference="a string")
        except ValueError as exception:
            if "reference should be" not in str(exception):
                raise exception

        # Check that use_ref_bounds works - use a DEM that do not cover the whole extent

        # This case should not preserve original extent
        stacked_dem = xdem.spatial_tools.stack_rasters([self.dem1, self.dem3])
        assert stacked_dem.bounds != self.dem.bounds

        # This case should preserve original extent
        stacked_dem2 = xdem.spatial_tools.stack_rasters([self.dem1, self.dem3], reference=self.dem, use_ref_bounds=True)
        assert stacked_dem2.bounds == self.dem.bounds

    def test_merge_rasters(self):
        """Test merge_rasters"""
        # Merge the two overlapping DEMs and check that it closely resembles the initial DEM
        merged_dem = xdem.spatial_tools.merge_rasters([self.dem1, self.dem2])
        assert self.dem.data.shape == merged_dem.data.shape
        assert self.dem.bounds == merged_dem.bounds

        diff = self.dem.data - merged_dem.data

        assert np.abs(np.nanmean(diff)) < 0.0001

        # Check that reference works
        merged_dem2 = xdem.spatial_tools.merge_rasters([self.dem1, self.dem2], reference=self.dem)
        assert merged_dem2 == merged_dem