Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_reproject_invalid_src_nodata():
"""src_nodata must be in range for data type."""
params = default_reproject_params()
source = np.ones((params.width, params.height), dtype=np.uint8)
out = source.copy()
with pytest.raises(ValueError):
reproject(
source,
out,
src_transform=params.src_transform,
src_crs=params.src_crs,
src_nodata=999999999,
dst_transform=params.dst_transform,
dst_crs=params.dst_crs,
dst_nodata=215,
)
def test_reproject_epsg():
with rasterio.open('tests/data/RGB.byte.tif') as src:
source = src.read(1)
dst_crs = {'init': 'EPSG:3857'}
out = np.empty(src.shape, dtype=np.uint8)
reproject(
source,
out,
src_transform=src.transform,
src_crs=src.crs,
dst_transform=DST_TRANSFORM,
dst_crs=dst_crs,
resampling=Resampling.nearest)
assert (out > 0).sum() == 438113
profile = src.profile.copy()
dst_crs = {'init': 'EPSG:32619'}
# Calculate the ideal dimensions and transformation in the new crs
dst_affine, dst_width, dst_height = calculate_default_transform(
src.crs, dst_crs, src.width, src.height, *src.bounds)
profile['height'] = dst_height
profile['width'] = dst_width
out = np.empty(shape=(dst_height, dst_width), dtype=np.uint8)
# see #614, some resampling methods succeed but produce blank images
out = np.empty(src.shape, dtype=np.uint8)
reproject(
source,
out,
src_transform=src.transform,
src_crs=src.crs,
dst_transform=dst_affine,
dst_crs=dst_crs,
resampling=method)
assert out.mean() > 0
def test_reproject_init_dest_nodata():
"""No pixels should transfer over"""
crs = CRS.from_epsg(4326)
transform = Affine.identity()
source = np.zeros((1, 100, 100))
destination = np.ones((1, 100, 100))
reproject(
source, destination, src_crs=crs, src_transform=transform,
dst_crs=crs, dst_transform=transform,
src_nodata=0, init_dest_nodata=False
)
assert destination.all()
'transform': params.dst_transform
}
with rasterio.open(tiffname, 'w', **kwargs) as dst:
reproject(
source1,
rasterio.band(dst, 1),
src_transform=params.src_transform,
src_crs=params.src_crs,
src_nodata=0.0,
dst_transform=params.dst_transform,
dst_crs=params.dst_crs,
dst_nodata=0.0
)
reproject(
source2,
rasterio.band(dst, 1),
src_transform=params.src_transform,
src_crs=params.src_crs,
src_nodata=0.0,
dst_transform=params.dst_transform,
dst_crs=params.dst_crs,
dst_nodata=0.0,
init_dest_nodata=False
)
# 200s should remain along with 100s
with rasterio.open(tiffname) as src:
data = src.read()
assert data.max() == 200
output_loc = shapely.affinity.affine_transform(self.src_obj,
self.dest_obj)
return output_loc
else:
if isinstance(self.dest_transform, Affine):
xform_mat = [self.dest_transform.a, self.dest_transform.b,
self.dest_transform.d, self.dest_transform.e,
self.dest_transform.xoff,
self.dest_transform.yoff]
else:
xform_mat = self.dest_transform
output_loc = shapely.affinity.affine_transform(self.src_obj,
xform_mat)
return output_loc
elif self.src_type == 'numpy array':
return rasterio.warp.reproject(
self.src_obj, output_loc, src_transform=self.src_transform,
src_crs=self.src_crs, dst_transform=self.dest_transform,
dst_crs=self.dest_crs)
else:
src_data = self.src.read(
window=window,
resampling=getattr(Resampling,
self.resampling),
boundless=True)
dst_transform, width, height = calculate_default_transform(
self.src.crs, CRS.from_epsg(self.dest_crs),
self.src.width, self.src.height, *tb,
dst_height=self.dest_tile_size[0],
dst_width=self.dest_tile_size[1])
tile_data = np.zeros(shape=(src_data.shape[0], height, width),
dtype=src_data.dtype)
rasterio.warp.reproject(
source=src_data,
destination=tile_data,
src_transform=self.src.window_transform(window),
src_crs=self.src.crs,
dst_transform=dst_transform,
dst_crs=CRS.from_epsg(self.dest_crs),
resampling=getattr(Resampling, self.resampling))
if self.nodata:
mask = np.all(tile_data != nodata,
axis=0).astype(np.uint8) * 255
elif self.alpha:
mask = self.src.read(self.alpha, window=window,
resampling=getattr(Resampling,
self.resampling))
else:
"Invalid output dimensions: {0}.".format(
(dst_width, dst_height)))
out_kwargs.update({
'crs': dst_crs,
'transform': dst_transform,
'affine': dst_transform,
'width': dst_width,
'height': dst_height
})
out_kwargs.update(**creation_options)
with rasterio.open(output, 'w', **out_kwargs) as dst:
for i in range(1, src.count + 1):
reproject(
source=rasterio.band(src, i),
destination=rasterio.band(dst, i),
src_transform=src.affine,
src_crs=src.crs,
# src_nodata=#TODO
dst_transform=out_kwargs['transform'],
dst_crs=out_kwargs['crs'],
# dst_nodata=#TODO
resampling=resampling,
num_threads=threads)
top=self.bounds.top)
meta = deepcopy(self.meta)
meta['nodata'] = nodata
meta['width'] = dst_width
meta['height'] = dst_height
meta['transform'] = dst_transform
meta['crs'] = crs
if progress is True:
t = tqdm(total=self.count)
with rasterio.open(file_path, 'w', driver=driver, **meta) as dst:
for i, layer in enumerate(self.iloc):
reproject(
source=rasterio.band(layer.ds, layer.bidx),
destination=rasterio.band(dst, i+1),
resampling=rasterio.enums.Resampling[resampling],
num_threads=n_jobs,
warp_mem_lim=warp_mem_lim)
if progress is True:
t.update()
new_raster = self._newraster(file_path, self.names)
if tfile is not None:
for layer in new_raster.iloc:
layer.close = tfile.close
return new_raster