Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
window = evaluate(window, height=height, width=width)
height_shape = block_shapes[0][0]
width_shape = block_shapes[0][1]
(row_start, row_stop), (col_start, col_stop) = window.toranges()
row_min = int(row_start // height_shape) * height_shape
row_max = int(row_stop // height_shape) * height_shape + \
(height_shape if row_stop % height_shape != 0 else 0)
col_min = int(col_start // width_shape) * width_shape
col_max = int(col_stop // width_shape) * width_shape + \
(width_shape if col_stop % width_shape != 0 else 0)
return Window(col_min, row_min, col_max - col_min, row_max - row_min)
def test_block_windows_bigger_blocksize(tmpdir, blocksize):
"""Ensure that block sizes greater than raster size are ok"""
tempfile = str(tmpdir.join("test.tif"))
profile = default_gtiff_profile.copy()
profile.update(height=16, width=16, count=1, blockxsize=blocksize, blockysize=blocksize)
with rasterio.open(tempfile, "w", **profile) as dst:
assert dst.is_tiled
for ij, window in dst.block_windows():
dst.write(np.ones((1, 1), dtype="uint8"), 1, window=window)
with rasterio.open(tempfile) as dst:
assert list(dst.block_windows()) == [((0, 0), windows.Window(0, 0, 16, 16))]
assert (dst.read(1) == 1).all()
def test_msk_read_masks(path_rgb_msk_byte_tif):
"""Boundless read of a source with .msk succeeds
Success in this case means that we read a mask that has
invalid pixels around the edges, is appropriately padded,
and has valid data pixels in the center.
"""
with rasterio.open(path_rgb_msk_byte_tif) as src:
msk = src.read_masks(1, boundless=True, window=Window(-200, -200, 1000, 1000), out_shape=((600, 600)))
# Invalid region is padded correctly.
assert not msk[0:195,0:195].any()
# We have the valid data expected in the center.
assert msk.mean() > 90
def test_window_class_intersects_list():
"""A list of Windows intersect"""
assert intersect([Window(0, 0, 10, 10), Window(8, 8, 10, 10)])
def test_round_offsets_no_op_error():
with pytest.raises(WindowError):
Window(0, 0, 1, 1).round_offsets(op='lolwut')
if minimum_tile_cover and cover_ratio < minimum_tile_cover:
raise TileOutsideBounds(
"Dataset covers less than {:.0f}% of tile".format(cover_ratio * 100)
)
if tile_edge_padding > 0 and not _requested_tile_aligned_with_internal_tile(
src_dst, bounds, tilesize
):
vrt_transform = vrt_transform * Affine.translation(
-tile_edge_padding, -tile_edge_padding
)
orig_vrt_height = vrt_height
orig_vrt_width = vrt_width
vrt_height = vrt_height + 2 * tile_edge_padding
vrt_width = vrt_width + 2 * tile_edge_padding
out_window = windows.Window(
col_off=tile_edge_padding,
row_off=tile_edge_padding,
width=orig_vrt_width,
height=orig_vrt_height,
)
vrt_params.update(dict(transform=vrt_transform, width=vrt_width, height=vrt_height))
indexes = indexes if indexes is not None else src_dst.indexes
out_shape = (len(indexes), tilesize, tilesize)
nodata = nodata if nodata is not None else src_dst.nodata
if nodata is not None:
vrt_params.update(dict(nodata=nodata, add_alpha=False, src_nodata=nodata))
if has_alpha_band(src_dst):
kwds['width'], kwds['height'])
src_nodata = kwds.pop('src_nodata', None)
dst_nodata = kwds.pop('dst_nodata', None)
warnings.simplefilter('ignore')
with MemoryFile() as memfile:
with memfile.open(**kwds) as tmp:
# determine window of source raster corresponding to the tile
# image, with small buffer at edges
try:
west, south, east, north = transform_bounds(TILES_CRS, src.crs, ulx, lry, lrx, uly)
tile_window = window_from_bounds(west, south, east, north, transform=src.transform)
adjusted_tile_window = Window(
tile_window.col_off - 1, tile_window.row_off - 1,
tile_window.width + 2, tile_window.height + 2)
tile_window = adjusted_tile_window.round_offsets().round_shape()
# if no data in window, skip processing the tile
if not src.read_masks(1, window=tile_window).any():
return tile, None
except ValueError:
log.info("Tile %r will not be skipped, even if empty. This is harmless.", tile)
reproject(rasterio.band(src, tmp.indexes),
rasterio.band(tmp, tmp.indexes),
src_nodata=src_nodata,
dst_nodata=dst_nodata,
num_threads=1,
if len(key) == 3:
index_range, y, x = key
indexes = list(
range(index_range.start + 1, index_range.stop + 1,
index_range.step or 1))
else:
indexes = 1
y, x = key
chy_off = y.start
chy = y.stop - y.start
chx_off = x.start
chx = x.stop - x.start
self.dataset.write(
item, window=Window(chx_off, chy_off, chx, chy), indexes=indexes)
Returns
-------
pyspatialml.RasterLayer
Returns a single RasterLayer containing the calculated result.
"""
_, tfile = _file_path_tempfile(None)
driver = self.driver
# determine dtype of result based on calc on single pixel
if other is not None:
arr1 = self.read(masked=True, window=Window(0, 0, 1, 1))
try:
arr2 = other.read(masked=True, window=Window(0, 0, 1, 1))
except AttributeError:
arr2 = other
test = function(arr1, arr2)
dtype = test.dtype
else:
dtype = self.dtype
nodata = _get_nodata(dtype)
# open output file with updated metadata
meta = self.meta
meta.update(driver=driver, count=1, dtype=dtype, nodata=nodata)
with rasterio.open(tfile.name, 'w', **meta) as dst:
kwargs = first.profile
kwargs.update(**creation_options)
dtype = dtype or first.meta['dtype']
kwargs['dtype'] = dtype
# Extend snuggs.
snuggs.func_map['read'] = _read_array
snuggs.func_map['band'] = lambda d, i: _get_bands(inputs, sources, d, i)
snuggs.func_map['bands'] = lambda d: _get_bands(inputs, sources, d)
snuggs.func_map['fillnodata'] = lambda *args: fillnodata(*args)
snuggs.func_map['sieve'] = lambda *args: sieve(*args)
# The windows iterator is initialized with a single sample.
# The actual work windows will be added in the second
# iteration of the loop.
work_windows = [(None, Window(0, 0, 16, 16))]
for ij, window in work_windows:
ctxkwds = OrderedDict()
for i, ((name, path), src) in enumerate(zip(inputs, sources)):
# Using the class method instead of instance
# method. Latter raises
#
# TypeError: astype() got an unexpected keyword
# argument 'copy'
#
# possibly something to do with the instance being
# a masked array.
ctxkwds[name or '_i%d' % (i + 1)] = src.read(masked=masked, window=window)