def rasters_for_testing_chunks():
    rasters = [
        GeoRaster2.open("tests/data/raster/overlap2.tif"),
        GeoRaster2.open("tests/data/raster/overlap2.tif")
    ]
    rasters[0].image
    return rasters
def test_join_use_mask_band():
    # https://github.com/OSGeo/gdal/issues/1148
    # The test checks the pixel at the coordinate of bottom left corner of the r2 raster is unmasked.
    r1 = GeoRaster2.open("tests/data/raster/overlap1.tif")
    r2 = GeoRaster2.open("tests/data/raster/overlap2.tif")
    joined = join([r1, r2])
    assert not joined.get(r2.corners()["bl"]).mask.all()
def test_get_tile_merge_tiles(tile):
    raster1_path = './tests/data/raster/overlap1.tif'
    raster2_path = './tests/data/raster/overlap2.tif'
    raster1 = GeoRaster2.open(raster1_path)
    raster2 = GeoRaster2.open(raster2_path)

    features = [
        GeoFeature(raster1.footprint().reproject(new_crs=WGS84_CRS),
                   {'raster_url': raster1_path, 'created': datetime.now()}),
        GeoFeature(raster2.footprint().reproject(new_crs=WGS84_CRS),
                   {'raster_url': raster2_path, 'created': datetime.now()}),
    ]

    fc = FeatureCollection(features)
    bounds = mercantile.xy_bounds(*tile)
    eroi = GeoVector.from_bounds(xmin=bounds.left, xmax=bounds.right,
                                 ymin=bounds.bottom, ymax=bounds.top,
                                 crs=WEB_MERCATOR_CRS)
    expected_tile = merge_all([raster1.get_tile(*tile), raster2.get_tile(*tile)], roi=eroi)
    merged = fc.get_tile(*tile, sort_by='created')
    if merged is not None:
        assert merged == expected_tile
    else:
        assert expected_tile.image.mask.all()
        assert (expected_tile.image.data == 0).all()
def test_feature_collection_from_rasters():
    rasters_list = [
        GeoRaster2.open("./tests/data/raster/overlap1.tif"),
        GeoRaster2.open("./tests/data/raster/overlap2.tif")
    ]
    fc = FeatureCollection.from_georasters(rasters_list)
    assert fc.is_rasters_collection()
def test_featurecollection_apply_on_rasters_collection():
    rasters_list = [
        GeoRaster2.open("./tests/data/raster/overlap1.tif"),
        GeoRaster2.open("./tests/data/raster/overlap2.tif")
    ]
    fc = FeatureCollection.from_georasters(rasters_list)
    new_fc = fc.apply(prop1=3)
    assert new_fc.is_rasters_collection()
Exemple #6
0
def test_crop_boundless_masked(bounds):
    raster_w_mask = GeoRaster2.open("tests/data/raster/rgb.tif")
    raster_wo_mask = GeoRaster2.open("tests/data/raster/rgb.jp2")

    roi = GeoVector(Polygon.from_bounds(*bounds), WEB_MERCATOR_CRS)
    assert (np.array_equal(
        raster_w_mask.crop(roi).image.mask,
        raster_wo_mask.crop(roi).image.mask))
def check_file(filename):
    try:
        GeoRaster2.open(filename, lazy_load=False)
    except (OSError, GeoRaster2IOError):
        logger.warning(f"Removing file {filename}")
        os.remove(filename)
    else:
        logger.info(f"File {filename} looks correct")
def test_limit_to_bands_off_memory():
    r1 = GeoRaster2.open("tests/data/raster/rgb.tif",
                         band_names=['r', 'g', 'b'],
                         lazy_load=False)
    r2 = GeoRaster2.open("tests/data/raster/rgb.tif",
                         band_names=['r', 'g', 'b'])
    assert r1.limit_to_bands(['b', 'r']) == r2.limit_to_bands(['b', 'r'])
    assert r1.limit_to_bands(['r', 'b']) != r2.limit_to_bands(['b', 'r'])
    assert r2._image is None
Exemple #9
0
def test_read_write_internal_external_mask():
    with TemporaryDirectory() as folder:
        # internal mask (default) leaves no .msk file:
        internal_path = os.path.join(folder, 'internal.tif')
        some_raster_multiband.save(internal_path, factors=default_factors)
        assert not os.path.exists(internal_path + '.msk')

        # external mask leaves .msk file:
        external_path = os.path.join(folder, 'external.tif')
        some_raster_multiband.save(external_path, GDAL_TIFF_INTERNAL_MASK=False, factors=default_factors)
        assert os.path.exists(external_path + '.msk')

        # other than that, both rasters are identical:
        assert GeoRaster2.open(internal_path) == GeoRaster2.open(external_path)
Exemple #10
0
def test_crop_respects_rounding_precision():
    # https://github.com/satellogic/telluric/pull/311
    r1 = GeoRaster2.open("tests/data/raster/non_aligned1.tif", lazy_load=False)
    r2 = GeoRaster2.open("tests/data/raster/non_aligned2.tif", lazy_load=False)
    roi = GeoVector.from_bounds(
        271806.0179640717,
        1438839.4164977344,
        337216.40041283204,
        1519170.2272236191,
        crs=r1.crs,
    )
    r1_cropped = r1.crop(roi)
    r2_cropped = r2.crop(roi)
    assert r1.shape == r2.shape
    assert r1_cropped.shape == r2_cropped.shape
Exemple #11
0
def test_reproject_rpcs():
    # test reprojection when rpcs are defined and input raster has no src_crs
    # TODO: add smaller test data
    raster = GeoRaster2.open("tests/data/raster/grayscale.tif")
    reprojected = raster.reproject(dst_crs=WEB_MERCATOR_CRS, rpcs=raster.rpcs)
    assert reprojected.shape == (1, 2072, 5241)
    assert reprojected.mean()[0] == pytest.approx(724.4861459505134, 1e-4)
Exemple #12
0
def test_read_non_georeferenced():
    crs = CRS(init='epsg:3857')
    affine = Affine(10.0, 0.0, -6425941.63996855,
                    0.0, -10.0, -3169315.69478084)
    raster = GeoRaster2.open('tests/data/raster/no_georef.png', crs=crs, affine=affine, lazy_load=False)
    assert raster.crs == crs
    assert raster.affine == affine
Exemple #13
0
def test_read_write():
    for extension in ['tif', 'png']:
        with TemporaryDirectory() as folder:
            path = os.path.join(folder, 'test.%s' % extension)
            some_raster_multiband.save(path, factors=default_factors)
            read = GeoRaster2.open(path)
            assert read == some_raster_multiband
Exemple #14
0
def test_save_uses_copy():
    band_names = ["b1", "b2", "b3"]
    raster = GeoRaster2.open("tests/data/raster/rgb.tif", band_names=band_names)
    with NamedTemporaryFile(suffix='.tif') as target:
        factors = [2, 4]
        result = raster.save(target.name, overviews=True, factors=factors)
        assert result.band_names == band_names
        assert result.overviews_factors == factors
Exemple #15
0
def test_copy():
    assert some_raster == some_raster.copy()
    assert isinstance(some_raster.copy(mutable=True), MutableGeoRaster)
    raster = GeoRaster2.open('./tests/data/raster/overlap1.tif')
    assert raster == raster.copy()
    # assert raster.copy().not_loaded()
    raster.image
    assert raster == raster.copy()
    assert not raster.copy().not_loaded()
Exemple #16
0
def test_png_thumbnail_has_expected_properties():
    raster = GeoRaster2.open("tests/data/raster/rgb.tif")
    expected_thumbnail = raster.resize(dest_width=512, resampling=Resampling.nearest)
    result_thumbnail = GeoRaster2.from_bytes(
        raster.to_png(transparent=True, thumbnail_size=512, resampling=Resampling.nearest, in_range='image'),
        affine=expected_thumbnail.affine, crs=expected_thumbnail.crs, band_names=expected_thumbnail.band_names
    )

    assert result_thumbnail == expected_thumbnail
Exemple #17
0
def test_crop_boundless_masked(bounds):
    raster_w_mask = GeoRaster2.open("tests/data/raster/rgb.tif")
    raster_wo_mask = GeoRaster2.open("tests/data/raster/rgb.jp2")

    roi = GeoVector(Polygon.from_bounds(*bounds), WEB_MERCATOR_CRS)
    assert (np.array_equal(
        raster_w_mask.crop(roi).image.mask,
        raster_wo_mask.crop(roi).image.mask))

    feature = GeoFeature(roi, properties={'prop1': 1, 'prop2': 2})
    assert (np.array_equal(
        raster_w_mask.crop(feature).image.mask,
        raster_wo_mask.crop(feature).image.mask))

    collection = FeatureCollection([feature])
    assert (np.array_equal(
        raster_w_mask.crop(collection).image.mask,
        raster_wo_mask.crop(collection).image.mask))
Exemple #18
0
def test_save_preserves_nodata():
    expected_nodata = 200
    path = '/vsimem/raster_for_test.tif'
    raster_nodata = GeoRaster2(some_array,
                               nodata=expected_nodata,
                               affine=some_affine,
                               crs=some_crs)
    raster_nodata.save(path)
    assert expected_nodata == GeoRaster2.open(path).nodata_value
Exemple #19
0
def test_virtual_filesystem_raster():
    raster_copy = some_raster.copy()
    in_memory_raster = raster_copy._as_in_memory_geotiff()
    assert len(raster_copy._opened_files) > 0
    virtual_path = in_memory_raster.source_file
    del raster_copy
    in_memory_raster2 = GeoRaster2.open(virtual_path)
    with pytest.raises(GeoRaster2IOError):
        _ = in_memory_raster2.shape
Exemple #20
0
def test_png_thumbnail_has_expected_properties():
    raster = GeoRaster2.open("tests/data/raster/rgb.tif")
    expected_thumbnail = raster.resize(dest_width=512,
                                       resampling=Resampling.nearest)
    result_thumbnail = GeoRaster2.from_bytes(
        raster._repr_png_(),
        affine=expected_thumbnail.affine,
        crs=expected_thumbnail.crs,
        band_names=expected_thumbnail.band_names)

    assert result_thumbnail == expected_thumbnail
Exemple #21
0
def test_empty_from_roi_respects_footprint():
    # See https://github.com/satellogic/telluric/issues/39
    raster = GeoRaster2.open("tests/data/raster/overlap1.tif")

    empty = GeoRaster2.empty_from_roi(shape=raster.shape[1:][::-1], ul_corner=(v[0] for v in raster.corner('ul').xy),
                                      resolution=raster.res_xy(), crs=raster.crs,
                                      band_names=raster.band_names, dtype=raster.dtype)

    empty_simple = GeoRaster2.empty_from_roi(roi=raster.footprint(),
                                             resolution=raster.res_xy(),
                                             band_names=raster.band_names, dtype=raster.dtype)

    assert raster.footprint().almost_equals(empty.footprint())
    assert raster.footprint().almost_equals(empty_simple.footprint())
Exemple #22
0
def test_merge_all_different_crs(crop, recwarn):
    roi = GeoVector(
        Polygon.from_bounds(-6321833, -3092272, -6319273, -3089712),
        WEB_MERCATOR_CRS)
    affine = Affine.translation(-57, -26) * Affine.scale(0.00083, -0.00083)
    expected_resolution = 10
    expected_crs = WEB_MERCATOR_CRS

    # from memory
    raster_0 = make_test_raster(1, [1],
                                height=1200,
                                width=1200,
                                affine=affine,
                                crs=WGS84_CRS)
    result_0 = merge_all([raster_0],
                         roi=roi,
                         dest_resolution=expected_resolution,
                         crs=expected_crs,
                         crop=crop)
    assert (result_0.resolution() == expected_resolution)
    assert (result_0.crs == expected_crs)
    assert (result_0.footprint().envelope.almost_equals(roi.envelope,
                                                        decimal=3))

    # from file
    path = "/vsimem/raster_for_test.tif"
    result_0.save(path)
    raster_1 = GeoRaster2.open(path)
    result_1 = merge_all([raster_1],
                         roi=roi,
                         dest_resolution=expected_resolution,
                         crs=expected_crs,
                         crop=crop)

    assert (result_1.resolution() == expected_resolution)
    assert (result_1.crs == expected_crs)
    assert (result_1.footprint().envelope.almost_equals(roi.envelope,
                                                        decimal=3))
    assert (result_0 == result_1)

    # preserve the original resolution if dest_resolution is not provided
    raster_2 = make_test_raster(1, [1],
                                height=1200,
                                width=1200,
                                affine=affine,
                                crs=WGS84_CRS)
    result_2 = merge_all([raster_2], roi=roi, crs=expected_crs, crop=crop)
    assert pytest.approx(result_2.resolution()) == 97.9691
Exemple #23
0
def test_not_loades():
    raster = GeoRaster2.open("tests/data/raster/rgb.tif")
    assert raster.not_loaded()
    raster.image
    assert not raster.not_loaded()
Exemple #24
0
def test_read_respects_nodata():
    expected_nodata = 100
    path = '/vsimem/raster_for_test.tif'
    some_raster.save(path, nodata=expected_nodata)

    assert expected_nodata == GeoRaster2.open(path).nodata_value
Exemple #25
0
def test_reproject_lazy():
    raster = GeoRaster2.open("tests/data/raster/rgb.tif")
    reprojected = raster.reproject(dst_crs=WGS84_CRS)
    assert reprojected._image is None
    assert reprojected._filename is not None
    assert reprojected._temporary
Exemple #26
0
    def get_raster(self, field_name='raster_url'):
        if self._raster is None:
            self._raster = GeoRaster2.open(self[field_name])

        return self._raster
Exemple #27
0
def test_empty_crs():
    raster = GeoRaster2.open("tests/data/raster/no_georef.png")
    assert raster.crs == CRS()
    assert raster.crs.is_valid is False
Exemple #28
0
def test_blockshapes_for_file_raster():
    raster = GeoRaster2.open("tests/data/raster/rgb.tif")
    assert len(raster.blockshapes) == raster.num_bands
    assert raster.blockshapes == [(27, 100), (27, 100), (27, 100)]
Exemple #29
0
def test_copy_raster_without_crs(recwarn):
    raster = GeoRaster2.open("tests/data/raster/no_georef.png")
    raster_copy = raster.copy_with()
    assert raster_copy.crs == raster.crs
Exemple #30
0
def test_from_assets_to_assets():
    raster = GeoRaster2.open("tests/data/raster/rgb.tif")
    assert raster == GeoRaster2.from_assets(raster.to_assets())