def test_memory_crop_returns_resized_resolution(self): coords = mercantile.xy_bounds(*tiles[18]) shape = GeoVector(Polygon.from_bounds(*coords), WEB_MERCATOR_CRS) raster = self.read_only_virtual_geo_raster() cropped = raster.crop(shape, MERCATOR_RESOLUTION_MAPPING[18]) self.assertEqual(cropped.shape, (raster.num_bands, 256, 256)) self.assertAlmostEqual(cropped.affine[0], MERCATOR_RESOLUTION_MAPPING[18], 2)
def test_fails_with_empty_raster_for_tile_out_of_raster_area_with_no_tile_size( self): vr = self.read_only_virtual_geo_raster() roi = GeoVector.from_xyz(16384, 16383, 15) r = vr.crop(roi) self.assertTrue((r.image.data == 0).all()) self.assertTrue((r.image.mask).all()) self.assertEqual(r.image.shape, (3, 1223, 1223))
def test_memory_crop_returns_resized_resolution(self): coords = mercantile.xy_bounds(*tiles[15]) shape = GeoVector(Polygon.from_bounds(*coords), WEB_MERCATOR_CRS) raster = self.metric_raster() cropped = raster.crop(shape, mercator_zoom_to_resolution[15]) self.assertEqual(cropped.shape, (raster.num_bands, 256, 256)) self.assertAlmostEqual(cropped.affine[0], mercator_zoom_to_resolution[15], 2)
def test_get_entire_all_raster(self): vr = self.read_only_virtual_geo_raster() roi = GeoVector.from_xyz(37108, 25248, 16) r = vr.crop(roi) self.assertFalse((r.image.data == 0).all()) self.assertFalse((r.image.mask).all()) self.assertEqual(r.shape, (3, 612, 612))
def test_small_read_only_virtual_geo_raster_wgs84_crop(): # See https://github.com/satellogic/telluric/issues/61 roi = GeoVector.from_bounds(xmin=0, ymin=0, xmax=2, ymax=2, crs=WGS84_CRS) resolution = 1.0 # deg / px raster = GeoRaster2.empty_from_roi(roi, resolution) assert raster.crop(roi) == raster.crop(roi, raster.resolution())
def test_crop_use_get_window_for_a_not_loaded_image(self, mock_get_window): coords = mercantile.xy_bounds(*tiles[15]) shape = GeoVector(Polygon.from_bounds(*coords), WEB_MERCATOR_CRS) raster = self.read_only_virtual_geo_raster() with NamedTemporaryFile(mode='w+b', suffix=".tif") as rf: raster.save(rf.name) raster = GeoRaster2.open(rf.name) raster.crop(shape, MERCATOR_RESOLUTION_MAPPING[15]) assert mock_get_window.called_once
def test_geographic_crop_with_resize(self): coords = mercantile.xy_bounds(*tiles[17]) raster = self.read_only_virtual_geo_raster_wgs84() vector = GeoVector(Polygon.from_bounds(*coords), crs=WEB_MERCATOR_CRS) x_ex_res, y_ex_res = convert_resolution_from_meters_to_deg( self.metric_affine[6], MERCATOR_RESOLUTION_MAPPING[17]) cropped = raster.crop(vector, (x_ex_res, y_ex_res)) self.assertAlmostEqual(cropped.affine[0], x_ex_res) self.assertAlmostEqual(abs(cropped.affine[4]), y_ex_res, 6)
def test_crop_use_get_window_for_a_not_loaded_image(self, mock_get_window): coords = mercantile.xy_bounds(*tiles[15]) shape = GeoVector(Polygon.from_bounds(*coords), WEB_MERCATOR_CRS) raster = self.metric_raster() with NamedTemporaryFile(mode='w+b', suffix=".tif") as rf: raster.save(rf.name) raster = GeoRaster2.open(rf.name) raster.crop(shape, mercator_zoom_to_resolution[15]) assert mock_get_window.called_once
def test_geographic_crop_with_resize(self): coords = mercantile.xy_bounds(*tiles[17]) raster = self.geographic_raster() vector = GeoVector(Polygon.from_bounds(*coords), crs=self.metric_crs).reproject(self.geographic_crs) cropped = raster.crop(vector, mercator_zoom_to_resolution[17]) x_ex_res, y_ex_res = convert_resolution_from_meters_to_deg( self.metric_affine[6], mercator_zoom_to_resolution[17]) self.assertAlmostEqual(cropped.affine[0], x_ex_res) self.assertAlmostEqual(abs(cropped.affine[4]), y_ex_res, 6)
def test_crop_returns_full_resolution_as_default(self): coords = mercantile.xy_bounds(*tiles[17]) shape = GeoVector(Polygon.from_bounds(*coords), WEB_MERCATOR_CRS) raster = self.read_only_virtual_geo_raster() _, win = raster._vector_to_raster_bounds(shape) cropped = raster.crop(shape) self.assertEqual( cropped.shape, (raster.num_bands, round(win.height), round(win.width))) self.assertEqual(cropped.affine[0], raster.affine[0])
def test_crop_and_get_tile_do_the_same_when_image_is_populated(self): coords = mercantile.xy_bounds(*tiles[15]) shape = GeoVector(Polygon.from_bounds(*coords), WEB_MERCATOR_CRS) raster = self.read_only_virtual_geo_raster() with NamedTemporaryFile(mode='w+b', suffix=".tif") as rf: raster.save(rf.name) raster = GeoRaster2.open(rf.name) tile15 = raster.get_tile(*tiles[15]) raster._populate_from_rasterio_object(read_image=True) cropped_15 = raster.crop(shape, MERCATOR_RESOLUTION_MAPPING[15]) self.assertEqual(tile15, cropped_15)
def test_crop_and_get_tile_do_the_same(self): coords = mercantile.xy_bounds(*tiles[15]) shape = GeoVector(Polygon.from_bounds(*coords), WEB_MERCATOR_CRS) raster = self.metric_raster() with NamedTemporaryFile(mode='w+b', suffix=".tif") as rf: raster.save(rf.name) raster2 = GeoRaster2.open(rf.name) tile15 = raster2.get_tile(*tiles[15]) # load the image data raster2.image cropped15 = raster2.crop(shape, mercator_zoom_to_resolution[15]) self.assertEqual(tile15, cropped15)
def test_crop_in_memory_and_off_memory_without_resizing_are_the_same(self): coords = mercantile.xy_bounds(*tiles[18]) shape = GeoVector(Polygon.from_bounds(*coords), WEB_MERCATOR_CRS) raster = self.read_only_virtual_geo_raster() with NamedTemporaryFile(mode='w+b', suffix=".tif") as rf: raster.save(rf.name) raster2 = GeoRaster2.open(rf.name) off_memory_crop = raster2.crop(shape) # load the image data raster2.image in_memory_crop = raster2.crop(shape) self.assertEqual(off_memory_crop, in_memory_crop)
def test_crop_and_get_tile_do_the_same_when_image_is_populated_first_for_low_zoom( self): coords = mercantile.xy_bounds(*tiles[11]) shape = GeoVector(Polygon.from_bounds(*coords), WEB_MERCATOR_CRS) raster = self.metric_raster() with NamedTemporaryFile(mode='w+b', suffix=".tif") as rf: raster.save(rf.name) raster = GeoRaster2.open(rf.name) raster._populate_from_rasterio_object(read_image=True) tile11 = raster.get_tile(*tiles[11]) cropped_11 = raster.crop(shape, mercator_zoom_to_resolution[11]) self.assertEqual(tile11, cropped_11)
def test_crop_and_get_tile_do_the_same(self): coords = mercantile.xy_bounds(*tiles[15]) shape = GeoVector(Polygon.from_bounds(*coords), WEB_MERCATOR_CRS) raster = self.read_only_virtual_geo_raster() with NamedTemporaryFile(mode='w+b', suffix=".tif") as rf: raster.save(rf.name) raster2 = GeoRaster2.open(rf.name) tile15 = raster2.get_tile(*tiles[15]) # load the image data raster2.image cropped15 = raster2.crop(shape, MERCATOR_RESOLUTION_MAPPING[15]) self.assertEqual(tile15, cropped15)
def test_rasters_covering_different_overlapping_areas_on_y(): affine_a = Affine.translation(1, 2) * Affine.scale(1, -1) raster_a = make_test_raster(1, [1], height=20, width=20, affine=affine_a) affine_b = Affine.translation(1, -9) * Affine.scale(1, -1) raster_b = make_test_raster(2, [1], height=20, width=20, affine=affine_b) roi = GeoVector.from_bounds(xmin=1, ymin=-29, xmax=21, ymax=2, crs=constants.WEB_MERCATOR_CRS) rasters = [raster_a, raster_b] merged = merge_all(rasters, roi) assert(merged.affine.almost_equals(affine_a)) assert(not merged.image.mask.all()) assert((merged.image.data[0, 0:20, 0:20] == 1).all()) assert((merged.image.data[0, 21:30, 0:20] == 2).all())
def test_crop_of_rasters_with_opposite_affine_and_data_return_the_same( self): array = np.arange(0, 20).reshape(1, 4, 5) array2 = np.arange(19, -1, -1).reshape(1, 4, 5) array2.sort() image1 = np.ma.array(array, mask=False) image2 = np.ma.array(array2, mask=False) aff2 = Affine.translation(0, -8) * Affine.scale(2, 2) aff = Affine.scale(2, -2) r1 = GeoRaster2(image=image1, affine=aff, crs=WEB_MERCATOR_CRS) r2 = GeoRaster2(image=image2, affine=aff2, crs=WEB_MERCATOR_CRS) # r1 == r2 # doesn't work, see https://github.com/satellogic/telluric/issues/79 roi = GeoVector(Polygon.from_bounds(0, 0, 3, -3), crs=WEB_MERCATOR_CRS) r1c = r1.crop(roi) r2c = r2.crop(roi) # r1c == r2c # doesn't work, see https://github.com/satellogic/telluric/issues/79 # currently this is the only way to test the result is the same assert r2c.to_png() == r1c.to_png()
def test_crop_raises_error_for_impossible_transformation(self): raster = self.metric_raster() vector = GeoVector(Polygon.from_bounds(-180, -90, 180, 90), crs=self.geographic_crs) with self.assertRaises(GeoRaster2Error): raster.crop(vector)
def test_crop_use_crop_image_for_a_loaded_image(self, mock__crop): coords = mercantile.xy_bounds(*tiles[15]) shape = GeoVector(Polygon.from_bounds(*coords), WEB_MERCATOR_CRS) raster = self.metric_raster() raster.crop(shape, mercator_zoom_to_resolution[15]) assert mock__crop.called_once
def test_crop_raises_error_for_impossible_transformation(self): raster = self.read_only_virtual_geo_raster() vector = GeoVector(Polygon.from_bounds(-180, -90, 180, 90), crs=WGS84_CRS) with self.assertRaises(GeoRaster2Error): raster.crop(vector)
def test_construction_mutable_raster(): raster = MutableGeoRaster.empty_from_roi( GeoVector.from_xyz(300, 300, 13), resolution=MERCATOR_RESOLUTION_MAPPING[13]) assert isinstance(raster, MutableGeoRaster)
def test_crop_use_crop_image_for_a_loaded_image(self, mock__crop): coords = mercantile.xy_bounds(*tiles[15]) shape = GeoVector(Polygon.from_bounds(*coords), WEB_MERCATOR_CRS) raster = self.read_only_virtual_geo_raster() raster.crop(shape, MERCATOR_RESOLUTION_MAPPING[15]) assert mock__crop.called_once