Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
width=raster_data.shape[1],
tiled=True,
blockxsize=256,
blockysize=256
)
with rasterio.Env(TIFF_USE_OVR=True):
with rasterio.open(outfile, 'w', **profile) as dst:
dst.write(raster_data, 1)
overviews = [2 ** j for j in range(1, 4)]
dst.build_overviews(overviews, Resampling.nearest)
assert os.path.isfile(f'{outfile}.ovr')
assert not cog.validate(outfile)
outfile = str(tmpdir / 'raster.tif')
raster_data = 1000 * np.random.rand(512, 512).astype(np.uint16)
profile = BASE_PROFILE.copy()
profile.update(
height=raster_data.shape[0],
width=raster_data.shape[1]
)
with rasterio.open(outfile, 'w', **profile) as dst:
dst.write(raster_data, 1)
overviews = [2 ** j for j in range(1, 4)]
dst.build_overviews(overviews, Resampling.nearest)
assert not cog.validate(outfile)
outfile = str(tmpdir / 'raster.tif')
raster_data = 1000 * np.random.rand(512, 512).astype(np.uint16)
profile = BASE_PROFILE.copy()
profile.update(
height=raster_data.shape[0],
width=raster_data.shape[1],
tiled=True,
blockxsize=256,
blockysize=256
)
with rasterio.open(outfile, 'w', **profile) as dst:
dst.write(raster_data, 1)
assert not cog.validate(outfile)
def test_optimize_rasters_small(tiny_raster_file, tmpdir):
from terracotta.cog import validate
from terracotta.scripts import cli
input_pattern = str(tiny_raster_file)
outfile = tmpdir / tiny_raster_file.basename
runner = CliRunner()
result = runner.invoke(cli.cli, ['optimize-rasters', input_pattern, '-o', str(tmpdir)])
assert result.exit_code == 0, format_exception(result)
assert outfile.check()
# validate files
# (small rasters don't need overviews, so input file is valid, too)
assert validate(str(tiny_raster_file))
assert validate(str(outfile))
# check for data integrity
with rasterio.open(str(tiny_raster_file)) as src1, rasterio.open(str(outfile)) as src2:
with warnings.catch_warnings():
warnings.filterwarnings('ignore', 'invalid value encountered.*')
np.testing.assert_array_equal(src1.read(), src2.read())
def test_validate_optimized_small(tmpdir):
from terracotta import cog
outfile = str(tmpdir / 'raster.tif')
raster_data = 1000 * np.random.rand(128, 128).astype(np.uint16)
profile = BASE_PROFILE.copy()
profile.update(
height=raster_data.shape[0],
width=raster_data.shape[1]
)
with rasterio.open(outfile, 'w', **profile) as dst:
dst.write(raster_data, 1)
assert cog.validate(outfile)
height=raster_data.shape[0],
width=raster_data.shape[1],
tiled=True,
blockxsize=256,
blockysize=256
)
with MemoryFile() as mf, mf.open(**profile) as dst:
dst.write(raster_data, 1)
overviews = [2 ** j for j in range(1, 4)]
dst.build_overviews(overviews, Resampling.nearest)
copy(dst, outfile, copy_src_overviews=True, **profile)
assert cog.validate(outfile)
def test_validate_unoptimized(tmpdir):
from terracotta import cog
outfile = str(tmpdir / 'raster.tif')
raster_data = 1000 * np.random.rand(512, 512).astype(np.uint16)
profile = BASE_PROFILE.copy()
profile.update(
height=raster_data.shape[0],
width=raster_data.shape[1]
)
with rasterio.open(outfile, 'w', **profile) as dst:
dst.write(raster_data, 1)
assert not cog.validate(outfile)
flags = ['--compression', compression, '-q']
if in_memory is not None:
flags.append('--in-memory' if in_memory else '--no-in-memory')
if reproject:
flags.append('--reproject')
result = runner.invoke(cli.cli, ['optimize-rasters', input_pattern, '-o', str(tmpdir), *flags])
assert result.exit_code == 0, format_exception(result)
assert outfile.check()
# validate files
assert not validate(str(unoptimized_raster_file))
assert validate(str(outfile))
if reproject:
return
# check for data integrity
with rasterio.open(str(unoptimized_raster_file)) as src1, rasterio.open(str(outfile)) as src2:
with warnings.catch_warnings():
warnings.filterwarnings('ignore', 'invalid value encountered.*')
np.testing.assert_array_equal(src1.read(), src2.read())
"""
import rasterio
from rasterio import warp
from terracotta.cog import validate
row_data: Dict[str, Any] = {}
extra_metadata = extra_metadata or {}
if max_shape is not None and len(max_shape) != 2:
raise ValueError('max_shape argument must contain 2 values')
if use_chunks and max_shape is not None:
raise ValueError('Cannot use both use_chunks and max_shape arguments')
with rasterio.Env(**cls._RIO_ENV_KEYS):
if not validate(raster_path):
warnings.warn(
f'Raster file {raster_path} is not a valid cloud-optimized GeoTIFF. '
'Any interaction with it will be significantly slower. Consider optimizing '
'it through `terracotta optimize-rasters` before ingestion.',
exceptions.PerformanceWarning, stacklevel=3
)
with rasterio.open(raster_path) as src:
if src.nodata is None and not cls._has_alpha_band(src):
warnings.warn(
f'Raster file {raster_path} does not have a valid nodata value, '
'and does not contain an alpha band. No data will be masked.'
)
bounds = warp.transform_bounds(
src.crs, 'epsg:4326', *src.bounds, densify_pts=21