def test_load_as_matrix_with_crop(self): """ Test that the image is loaded with the correct crop region. We load two images: the original with a crop specified, and a pre-cropped image. The results of each load should be the same, indicating the correct region from the source image is extracted. """ assert \ GH_FILE_ELEMENT.get_bytes() != GH_CROPPED_FILE_ELEMENT.get_bytes() reader = GdalImageReader( load_method=GdalImageReader.LOAD_METHOD_TEMPFILE) cropped_actual = reader.load_as_matrix(GH_FILE_ELEMENT, pixel_crop=GH_CROPPED_BBOX) cropped_expected = reader.load_as_matrix(GH_CROPPED_FILE_ELEMENT) # noinspection PyTypeChecker numpy.testing.assert_allclose(cropped_actual, cropped_expected)
def test_load_as_matrix_with_crop_not_in_bounds(self): """ Test that error is raised when crop bbox is not fully within the image bounds. """ inst = GdalImageReader() # Nowhere close bb = AxisAlignedBoundingBox([5000, 6000], [7000, 8000]) with pytest.raises(RuntimeError, match=r"Crop provided not within input image\. " r"Image shape: \(512, 600\), crop: "): inst.load_as_matrix(GH_FILE_ELEMENT, pixel_crop=bb) # Outside left side bb = AxisAlignedBoundingBox([-1, 1], [2, 2]) with pytest.raises(RuntimeError, match=r"Crop provided not within input image\. " r"Image shape: \(512, 600\), crop: "): inst.load_as_matrix(GH_FILE_ELEMENT, pixel_crop=bb) # Outside top side bb = AxisAlignedBoundingBox([1, -1], [2, 2]) with pytest.raises(RuntimeError, match=r"Crop provided not within input image\. " r"Image shape: \(512, 600\), crop: "): inst.load_as_matrix(GH_FILE_ELEMENT, pixel_crop=bb) # Outside right side bb = AxisAlignedBoundingBox([400, 400], [513, 600]) with pytest.raises(RuntimeError, match=r"Crop provided not within input image\. " r"Image shape: \(512, 600\), crop: "): inst.load_as_matrix(GH_FILE_ELEMENT, pixel_crop=bb) # Outside bottom side bb = AxisAlignedBoundingBox([400, 400], [512, 601]) with pytest.raises(RuntimeError, match=r"Crop provided not within input image\. " r"Image shape: \(512, 600\), crop: "): inst.load_as_matrix(GH_FILE_ELEMENT, pixel_crop=bb)