def _get_tile_data_(_filelist, _outfile, _band_order, _tile_specs, _composite_type): _vrtfile = '/vsimem/' + Handler(_outfile).basename.split('.tif')[0] + '_tmp_layerstack.vrt' _outfile = '/vsimem/' + Handler(_outfile).basename.split('.tif')[0] + '_tmp_layerstack.tif' _mraster = MultiRaster(_filelist) _layerstack_vrt = _mraster.layerstack(return_vrt=True, outfile=_outfile) _lras = Raster('_tmp_layerstack') _lras.datasource = _layerstack_vrt _lras.initialize() _lras.make_tile_grid(*_tile_specs) for _ii in range(_lras.ntiles): yield _filelist, _outfile, _band_order, _lras.tile_grid[_ii], _composite_type
def _tile_process_(args): _filelist, _outfile, _band_order, _tile_dict, _composite_type = args _tile_coords = _tile_dict['block_coords'] _xmin, _ymin = _tile_dict['first_pixel'] _x, _y, _cols, _rows = _tile_coords _outfile = '/vsimem/' + Handler(_outfile).basename.split('.tif')[0] + \ '_{}.tif'.format('_'.join([str(j) for j in _tile_coords])) _mraster = MultiRaster(_filelist) _layerstack_vrt = _mraster.layerstack(return_vrt=True, outfile=_outfile) _lras = Raster('_tmp_layerstack') _lras.datasource = _layerstack_vrt _lras.initialize() _tile_arr = _lras.get_tile(_band_order, _tile_coords).copy() if _composite_type == 'mean': _temp_arr = np.apply_along_axis(lambda x: np.mean(x[x != _lras.nodatavalue]) if (x[x != _lras.nodatavalue]).shape[0] > 0 else _lras.nodatavalue, 0, _tile_arr) elif _composite_type == 'median': _temp_arr = np.apply_along_axis(lambda x: np.median(x[x != _lras.nodatavalue]) if (x[x != _lras.nodatavalue]).shape[0] > 0 else _lras.nodatavalue, 0, _tile_arr) elif _composite_type == 'max': _temp_arr = np.apply_along_axis(lambda x: np.max(x[x != _lras.nodatavalue]) if (x[x != _lras.nodatavalue]).shape[0] > 0 else _lras.nodatavalue, 0, _tile_arr) elif _composite_type == 'min': _temp_arr = np.apply_along_axis(lambda x: np.min(x[x != _lras.nodatavalue]) if (x[x != _lras.nodatavalue]).shape[0] > 0 else _lras.nodatavalue, 0, _tile_arr) elif 'pctl' in _composite_type: pctl = int(_composite_type.split('_')[1]) _temp_arr = np.apply_along_axis(lambda x: np.percentile(x[x != _lras.nodatavalue], pctl) if (x[x != _lras.nodatavalue]).shape[0] > 0 else _lras.nodatavalue, 0, _tile_arr) else: _temp_arr = None _lras = None _mraster = None Handler(_outfile).file_delete() _tile_arr = None return (_y-_ymin), ((_y-_ymin) + _rows), (_x-_xmin), ((_x-_xmin) + _cols), _temp_arr
filelist = list(all_files[i] for i in file_loc_on_list.tolist()) for file_ in filelist: Opt.cprint(file_) Opt.cprint(outfile) mraster = MultiRaster(filelist=filelist) Opt.cprint(mraster) ls_vrt = mraster.layerstack(return_vrt=True, outfile=outfile) lras = Raster('tmp_layerstack') lras.datasource = ls_vrt lras.initialize() xmin, xmax, ymin, ymax = lras.get_pixel_bounds(image_bounds, 'crs') lras.transform = (image_bounds[0], lras.transform[1], lras.transform[2], image_bounds[3], lras.transform[4], lras.transform[5]) lras.shape = [1, (ymax-ymin), (xmax-xmin)] lras.make_tile_grid(*tile_specs) print(len(lras.tile_grid))