""" Servicio /GMS/raster/retile create a pyramidal image from vrt """ import os from ...tools.gdal_python import GdalOptions # Fix gdal 2.40 and 3.3 integration problems try: import gdal except ModuleNotFoundError: from osgeo import gdal from tools.system_tools import stream_output_command from monitorT.task import Task class TaskCustom(Task): """ clase interna itasker """ CONTROL = True def post(task, user=None, input=None, output=None, parameters=None): """ Create a pyramidal image from vrt. Parameters ---------- user: str User Id input: str Path to input vrt output: str Path to output pyramid folder parameters: str Command line string options Go to https://gdal.org/programs/gdal_retile.html to get all the options. Excluded -targetDir. Returns ------- output: str Path to output pyramid folder Examples -------- >>> import requests >>> import json >>> # Default parameters -v levels 13 -co TILED=YES -ps 256 256 >>> data = {'user':'test', ... 'input': '/media/almacenamiento_a/almacenamiento_repositorio/Varios/vrt/prueba/test.vrt', ... 'output': '/media/almacenamiento_a/almacenamiento_repositorio/Varios/vrt/prueba/pyramid'} >>> response = requests.post(url='http:///GMS/raster/retile', json=data) '/media/almacenamiento_a/almacenamiento_repositorio/Varios/vrt/prueba/pyramid' """ DEFAULT_PARAMS = "-v -levels 13 -co TILED=YES -ps 256 256" ################################################### # Allow different users repositories # ################################################### # user_data = get_user_data(user) # # user_repo = user_data['repo'] # # if 'repo' in user_data.keys(): # # user_repo = user_data['repo'] # # data_base = StaticBase.load(user_repo) # # else: # # data_base = settings.STATIC_DATABASE[base] # ################################################### # Parse gdal options options = GdalOptions.parse_parameters(parameters, DEFAULT_PARAMS) # Get data for present progress ps = options['ps'].split(' ') levels = options['levels'] input_file = gdal.Open(input) total_x_pixels = input_file.RasterXSize total_y_pixels = input_file.RasterYSize total_files = 0 ndx_0 = total_x_pixels/int(ps[0]) ndy_0 = total_y_pixels / int(ps[1]) total_files += ndx_0 * ndy_0 for level in range(1, int(levels)): n_dx = max(ndx_0/2, 1) n_dy = max(ndy_0/2, 1) total_files += n_dx*n_dy ndx_0 = n_dx ndy_0 = n_dy if not os.path.exists(output): os.system("mkdir -p {}".format(output)) command = "gdal_retile.py {} -targetDir {} {}".format( options.export_params(), output, input) # os.system(command) # os.system(command) output_string = stream_output_command(command) current_file = 0 for line in output_string.stdout: current_file += 1 task.set_progress(current_file / total_files * 90) zero_directory = os.path.join(output, '0') if not os.path.exists(zero_directory): os.mkdir(zero_directory) # os.rename(os.path.join(output, '*.tiff'), zero_directory) os.system('cd {} && mv *.tif {}'.format(output, zero_directory)) return output