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
"RFalbedo_deciduous_fraction_treecover_50000_cutoff_5_deg1_20200501T185635_summer.pickle", "RFalbedo_deciduous_fraction_treecover_50000_cutoff_5_deg1_20200501T185635_fall.pickle") picklefiles = [pickle_dir + picklefile for picklefile in picklefiles] band_name = 'spr_albedo' outfile = outdir + Handler(Handler(infile).basename).add_to_filename('_output3') Handler(outfile).file_remove_check() # raster contains three bands: 1) decid 2) tree cover 3) land extent mask. # all the bands are in integer format raster = Raster(infile) raster.initialize() raster.bnames = ['decid', 'treecover'] raster.get_stats(True) Opt.cprint(raster.shape) regressor = RFRegressor.load_from_pickle(picklefiles[0]) Opt.cprint(regressor) Opt.cprint(regressor.adjustment) out_raster = RFRegressor.regress_raster(regressor, raster, output_type='mean',
Opt.cprint(rf_regressor1) Opt.cprint(rf_regressor2) bandnames1_ = rf_regressor1.features Opt.cprint('Bands 1 : ') Opt.cprint(bandnames1_) bandnames2_ = rf_regressor2.features Opt.cprint('Bands 2 : ') Opt.cprint(bandnames2_) bandnames_all = bandnames1_ + bandnames2_ # get raster metadata ras = Raster(infile) ras.initialize() tile_size = min([ras.shape[1], ras.shape[2]]) Opt.cprint(ras.shape) Opt.cprint(ras) bandnames = ras.bnames Opt.cprint('Raster bands: "' + '", "'.join(bandnames) + '"') band_order = Sublist(bandnames).sublistfinder(bandnames_all) Opt.cprint('Band order: ' + ', '.join([str(b) for b in band_order])) # re-initialize raster ras.initialize(get_array=True, band_order=band_order)
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))