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_read_raster_window_input_list(cleantopo_br):
process_zoom = 5
conf = dict(**cleantopo_br.dict)
conf["output"].update(metatiling=1)
with mapchete.open(conf) as mp:
mp.batch_process(process_zoom)
tiles = [
(tile, mp.config.output.get_path(tile))
for tile in mp.config.output_pyramid.tiles_from_bounds(
mp.config.bounds, process_zoom
)
if path_exists(mp.config.output.get_path(tile))
]
upper_tile = next(mp.get_process_tiles(process_zoom - 1))
assert len(tiles) > 1
resampled = resample_from_array(
in_raster=create_mosaic(
[(tile, read_raster_window(path, tile)) for tile, path in tiles]
),
out_tile=upper_tile
)
resampled2 = read_raster_window(
[p for _, p in tiles], upper_tile, src_nodata=0, dst_nodata=0
)
assert resampled.dtype == resampled2.dtype
assert resampled.shape == resampled2.shape
assert np.array_equal(resampled.mask, resampled2.mask)
# TODO slight rounding errors occur
def test_remote_path_exists(http_raster):
assert path_exists(http_raster)
assert not path_exists("http://ungarj.github.io/invalid_file.tif")
def __init__(self, out_path=None, output=None, out_pyramid=None):
# see if lxml is installed before checking all output tiles
from lxml.builder import ElementMaker
self.path = out_path
self._tp = out_pyramid
self._output = output
self._bucket = self.path.split("/")[2] if self.path.startswith("s3://") else None
self.bucket_resource = get_boto3_bucket(self._bucket) if self._bucket else None
logger.debug("initialize VRT writer for %s", self.path)
if path_exists(self.path):
if self._bucket:
key = "/".join(self.path.split("/")[3:])
for obj in self.bucket_resource.objects.filter(Prefix=key):
if obj.key == key:
self._existing = {
k: v for k, v in self._xml_to_entries(
obj.get()['Body'].read().decode()
)
}
else:
with open(self.path) as src:
self._existing = {k: v for k, v in self._xml_to_entries(src.read())}
else:
self._existing = {}
logger.debug("%s existing entries", len(self._existing))
self.new_entries = 0
def _get_tiles_paths(basepath=None, ext=None, pyramid=None, bounds=None, zoom=None):
return [
(_tile, _path)
for _tile, _path in [
(
t,
"%s.%s" % (
os.path.join(*([basepath, str(t.zoom), str(t.row), str(t.col)])), ext
)
)
for t in pyramid.tiles_from_bounds(bounds, zoom)
]
if path_exists(_path)
]
output_tile : ``BufferedTile``
must be member of output ``TilePyramid``
Returns
-------
exists : bool
"""
if process_tile and output_tile: # pragma: no cover
raise ValueError("just one of 'process_tile' and 'output_tile' allowed")
if process_tile:
return any(
path_exists(self.get_path(tile))
for tile in self.pyramid.intersecting(process_tile)
)
if output_tile:
return path_exists(self.get_path(output_tile))
------
FileNotFoundError if file cannot be found.
"""
with warnings.catch_warnings():
warnings.simplefilter("ignore")
try:
with rasterio.Env(
**get_gdal_options(
gdal_opts, is_remote=path_is_remote(input_file, s3=True)
)
):
with rasterio.open(input_file, "r") as src:
return src.read(indexes=indexes, masked=True)
except RasterioIOError as e:
try:
if path_exists(input_file):
raise e
except:
raise e
raise FileNotFoundError("%s not found" % input_file)
bounds.top
),
height=height,
width=width,
count=self.output_params["bands"],
crs=self.pyramid.crs,
**{
k: self.output_params.get(k, GTIFF_DEFAULT_PROFILE[k])
for k in GTIFF_DEFAULT_PROFILE.keys()
}
)
logger.debug("single GTiff profile: %s", self._profile)
if height * width > 20000 * 20000:
raise ValueError("output GeoTIFF too big")
# set up rasterio
if path_exists(self.path):
if self.output_params["mode"] != "overwrite":
raise MapcheteConfigError(
"single GTiff file already exists, use overwrite mode to replace"
)
else:
logger.debug("remove existing file: %s", self.path)
os.remove(self.path)
logger.debug("open output file: %s", self.path)
self.rio_file = rasterio.open(self.path, "w+", **self._profile)
window=vrt.window(*dst_bounds),
out_shape=dst_shape,
indexes=indexes,
masked=True
)
if isinstance(input_file, str):
logger.debug("got file path %s", input_file)
try:
with rasterio.open(input_file, "r") as src:
return _read(
src, indexes, dst_bounds, dst_shape, dst_crs, resampling, src_nodata,
dst_nodata
)
except RasterioIOError as e:
try:
if path_exists(input_file):
raise e
except:
raise e
raise FileNotFoundError("%s not found" % input_file)
else:
logger.debug("assuming file object %s", input_file)
return _read(
input_file, indexes, dst_bounds, dst_shape, dst_crs, resampling,
src_nodata, dst_nodata
)