def test_gdal_calc_py_5(): if gdalnumeric_not_available: gdaltest.post_reason('gdalnumeric is not available, skipping all tests') return 'skip' script_path = test_py_scripts.get_py_script('gdal_calc') if script_path is None: return 'skip' backup_sys_path = sys.path sys.path.insert(0, script_path) import gdal_calc shutil.copy('../gcore/data/stefan_full_rgba.tif', 'tmp/test_gdal_calc_py.tif') gdal_calc.Calc('A', A='tmp/test_gdal_calc_py.tif', overwrite=True, quiet=True, outfile='tmp/test_gdal_calc_py_5_1.tif') gdal_calc.Calc('A', A='tmp/test_gdal_calc_py.tif', A_band=2, overwrite=True, quiet=True, outfile='tmp/test_gdal_calc_py_5_2.tif') gdal_calc.Calc('Z', Z='tmp/test_gdal_calc_py.tif', Z_band=2, overwrite=True, quiet=True, outfile='tmp/test_gdal_calc_py_5_3.tif') sys.path = backup_sys_path ds1 = gdal.Open('tmp/test_gdal_calc_py_5_1.tif') ds2 = gdal.Open('tmp/test_gdal_calc_py_5_2.tif') ds3 = gdal.Open('tmp/test_gdal_calc_py_5_3.tif') if ds1 is None: gdaltest.post_reason('ds1 not found') return 'fail' if ds2 is None: gdaltest.post_reason('ds2 not found') return 'fail' if ds3 is None: gdaltest.post_reason('ds3 not found') return 'fail' if ds1.GetRasterBand(1).Checksum() != 12603: gdaltest.post_reason('ds1 wrong checksum') return 'fail' if ds2.GetRasterBand(1).Checksum() != 58561: gdaltest.post_reason('ds2 wrong checksum') return 'fail' if ds3.GetRasterBand(1).Checksum() != 58561: gdaltest.post_reason('ds3 wrong checksum') return 'fail' ds1 = None ds2 = None ds3 = None return 'success'
def norm_estandar(path_raster, path_raster_n): min,max = raster_min_max(path_raster) clp_norm ='(A - ' + str(min) + ') / (' + str(max) + ' - ' +str(min) +')' gdal_calc.Calc(calc=clp_norm, A=path_raster, outfile=path_raster_n)
def test_gdal_calc_py_6(): if gdalnumeric_not_available: gdaltest.post_reason('gdalnumeric is not available, skipping all tests') return 'skip' script_path = test_py_scripts.get_py_script('gdal_calc') if script_path is None: return 'skip' backup_sys_path = sys.path sys.path.insert(0, script_path) import gdal_calc gdal.Translate('tmp/test_gdal_calc_py.tif', '../gcore/data/byte.tif', options = '-a_nodata 74') gdal_calc.Calc('A', A='tmp/test_gdal_calc_py.tif', overwrite=True, quiet=True, outfile='tmp/test_gdal_calc_py_6.tif', NoDataValue = 1) sys.path = backup_sys_path ds = gdal.Open('tmp/test_gdal_calc_py_6.tif') cs = ds.GetRasterBand(1).Checksum() if cs != 4673: gdaltest.post_reason('failure') print(cs) return 'fail' (min, max) = ds.GetRasterBand(1).ComputeRasterMinMax() if min != 90 or max != 255: gdaltest.post_reason('failure') print(min) print(max) return 'fail' return 'success'
def test_gdal_calc_py_6(): if gdalnumeric_not_available: pytest.skip('gdalnumeric is not available, skipping all tests') script_path = test_py_scripts.get_py_script('gdal_calc') if script_path is None: pytest.skip() backup_sys_path = sys.path sys.path.insert(0, script_path) import gdal_calc gdal.Translate('tmp/test_gdal_calc_py.tif', '../gcore/data/byte.tif', options='-a_nodata 74') gdal_calc.Calc('A', A='tmp/test_gdal_calc_py.tif', overwrite=True, quiet=True, outfile='tmp/test_gdal_calc_py_6.tif', NoDataValue=1) sys.path = backup_sys_path ds = gdal.Open('tmp/test_gdal_calc_py_6.tif') cs = ds.GetRasterBand(1).Checksum() assert cs == 4673 result = ds.GetRasterBand(1).ComputeRasterMinMax() assert result == (90, 255)
def norm_max(path_raster, path_raster_n): min, max = raster_min_max(path_raster) no_data = raster_nodata(path_raster) clp_norm = '(A ) / (' + str(max) + ')' gdal_calc.Calc(calc=clp_norm, A=path_raster, outfile=path_raster_n, NoDataValue=no_data)
def norm_max(path_raster, path_raster_n, max): #min,max,no_data = raster_min_max(path_raster) #ec_norm = '(A -'+str(min)+')/ ('+str(max)+'-' + str(min)+')' ec_norm = '(A) / (' + str(max) + ')' gdal_calc.Calc(calc=ec_norm, A=path_raster, outfile=path_raster_n, type='Float32', NoDataValue=-9999.0, quiet=True)
def test_gdal_calc_py_5(): if gdalnumeric_not_available: pytest.skip('gdalnumeric is not available, skipping all tests') script_path = test_py_scripts.get_py_script('gdal_calc') if script_path is None: pytest.skip() backup_sys_path = sys.path sys.path.insert(0, script_path) import gdal_calc shutil.copy('../gcore/data/stefan_full_rgba.tif', 'tmp/test_gdal_calc_py.tif') gdal_calc.Calc('A', A='tmp/test_gdal_calc_py.tif', overwrite=True, quiet=True, outfile='tmp/test_gdal_calc_py_5_1.tif') gdal_calc.Calc('A', A='tmp/test_gdal_calc_py.tif', A_band=2, overwrite=True, quiet=True, outfile='tmp/test_gdal_calc_py_5_2.tif') gdal_calc.Calc('Z', Z='tmp/test_gdal_calc_py.tif', Z_band=2, overwrite=True, quiet=True, outfile='tmp/test_gdal_calc_py_5_3.tif') gdal_calc.Calc(['A', 'Z'], A='tmp/test_gdal_calc_py.tif', Z='tmp/test_gdal_calc_py.tif', Z_band=2, overwrite=True, quiet=True, outfile='tmp/test_gdal_calc_py_5_4.tif') sys.path = backup_sys_path ds1 = gdal.Open('tmp/test_gdal_calc_py_5_1.tif') ds2 = gdal.Open('tmp/test_gdal_calc_py_5_2.tif') ds3 = gdal.Open('tmp/test_gdal_calc_py_5_3.tif') ds4 = gdal.Open('tmp/test_gdal_calc_py_5_4.tif') assert ds1 is not None, 'ds1 not found' assert ds2 is not None, 'ds2 not found' assert ds3 is not None, 'ds3 not found' assert ds4 is not None, 'ds4 not found' assert ds1.GetRasterBand(1).Checksum() == 12603, 'ds1 wrong checksum' assert ds2.GetRasterBand(1).Checksum() == 58561, 'ds2 wrong checksum' assert ds3.GetRasterBand(1).Checksum() == 58561, 'ds3 wrong checksum' assert ds4.GetRasterBand(1).Checksum() == 12603, 'ds4#1 wrong checksum' assert ds4.GetRasterBand(2).Checksum() == 58561, 'ds4#2 wrong checksum' ds1 = None ds2 = None ds3 = None ds4 = None
def norm_l_decreciente(path_raster, path_raster_n, min, max): #min,max,no_data = raster_min_max(path_raster) #ec_norm = '(A -'+str(min)+')/ ('+str(max)+'-' + str(min)+')' ec = '((-A * ((' + max + ' - ' + min + ') /' + max + ')) /( ' + max + ' - ' + min + ')) + ((' + max + '+' + min + ')/' + max + ')' print(ec) gdal_calc.Calc(calc=ec, A=path_raster, outfile=path_raster_n, type='Float32', NoDataValue=-9999.0, quiet=True)
def merge_dir(dir_input): logging.debug("Merging {}".format(dir_input)) # HACK: for some reason output tiles were both being called 'probability' import importlib importlib.reload(gr) TILE_SIZE = str(1024) file_tmp = dir_input + '_tmp.tif' file_out = dir_input + '.tif' file_int = dir_input + '_int.tif' co = list( itertools.chain.from_iterable( map(lambda x: ['-co', x], CREATION_OPTIONS))) files = [] for region in os.listdir(dir_input): dir_region = os.path.join(dir_input, region) files = files + [ os.path.join(dir_region, x) for x in sorted(os.listdir(dir_region)) if x.endswith('.tif') ] gm.main(['', '-n', '0', '-a_nodata', '0'] + co + ['-o', file_tmp] + files) #gm.main(['', '-n', '0', '-a_nodata', '0', '-co', 'COMPRESS=DEFLATE', '-co', 'ZLEVEL=9', '-co', 'TILED=YES', '-o', file_tmp] + files) shutil.move(file_tmp, file_out) logging.debug("Calculating...") gdal_calc.Calc(A=file_out, outfile=file_tmp, calc='A*100', NoDataValue=0, type='Byte', creation_options=CREATION_OPTIONS, quiet=True) shutil.move(file_tmp, file_int) dir_tile = os.path.join(dir_input, 'tiled') if os.path.exists(dir_tile): logging.debug('Removing {}'.format(dir_tile)) shutil.rmtree(dir_tile) import subprocess file_cr = dir_input + '_cr.tif' logging.debug("Applying symbology...") subprocess.run( 'gdaldem color-relief {} /FireGUARD/FireSTARR/col.txt {} -alpha -co COMPRESS=LZW -co TILED=YES' .format(file_int, file_cr), shell=True) dir_tile = common.ensure_dir(dir_tile) subprocess.run( 'python /usr/local/bin/gdal2tiles.py -a 0 -z 5-12 {} {} --processes={}' .format(file_cr, dir_tile, os.cpu_count()), shell=True)
def crea_capa(ecuacion, rasters_input, salida): path_A = '' path_B = '' path_C = '' path_D = '' path_E = '' path_F = '' path_G = '' path_H = '' total_raster = len(rasters_input) no_data = -9999 # raster_nodata(rasters_input[0]) for a, b in zip(range(total_raster), rasters_input): if a == 0: path_A = b elif a == 1: path_B = b elif a == 2: path_C = b elif a == 3: path_D = b elif a == 4: path_E = b elif a == 5: path_F = b elif a == 6: path_G = b elif a == 7: path_H = b if total_raster == 1: gdal_calc.Calc(calc=ecuacion, A=path_A, outfile=salida, NoDataValue=no_data) if total_raster == 2: gdal_calc.Calc(calc=ecuacion, A=path_A, B=path_B, outfile=salida, NoDataValue=no_data) if total_raster == 3: gdal_calc.Calc(calc=ecuacion, A=path_A, B=path_B, C=path_C, outfile=salida, NoDataValue=no_data) if total_raster == 4: gdal_calc.Calc(calc=ecuacion, A=path_A, B=path_B, C=path_C, D=path_D, outfile=salida, NoDataValue=no_data) if total_raster == 5: gdal_calc.Calc(calc=ecuacion, A=path_A, B=path_B, C=path_C, D=path_D, E=path_E, outfile=salida, NoDataValue=no_data) if total_raster == 6: gdal_calc.Calc(calc=ecuacion, A=path_A, B=path_B, C=path_C, D=path_D, E=path_E, F=path_F, outfile=salida, NoDataValue=no_data) if total_raster == 7: gdal_calc.Calc(calc=ecuacion, A=path_A, B=path_B, C=path_C, D=path_D, E=path_E, F=path_F, G=path_G, outfile=salida, NoDataValue=no_data) if total_raster == 8: gdal_calc.Calc(calc=ecuacion, A=path_A, B=path_B, C=path_C, D=path_D, E=path_E, F=path_F, G=path_G, H=path_H, outfile=salida, NoDataValue=no_data)
def norm_inversa(path_raster, path_raster_n): min,max = raster_min_max(path_raster) norm ='(1 - A' ') / (' + '1 - ' +str(min) +')' gdal_calc.Calc(calc=norm, A=path_raster, outfile=path_raster_n)
path = result_path + dirLoc + '/' if not os.path.exists(path): os.mkdir(path) src_path = tiles_path + dirLoc + '/Sigma0_filtered_db.data/' # going through the images liste_vv = {} liste_vh = {} for file in os.listdir(src_path): if file.endswith('.img'): date = translateDate(file[-16:-7]) pol = file[7:9] if pol == 'VV': liste_vv[date] = file else: liste_vh[date] = file # vv-vh calculation and GTiff conversion for (date, vh) in liste_vh.items(): vv = liste_vv[date] output_vv = path + 'sigma0_db_vv_' + dirLoc + '_' + date + '.tif' output_vh = path + 'sigma0_db_vh_' + dirLoc + '_' + date + '.tif' output_vvvh = path + 'sigma0_db_vvvh_' + dirLoc + '_' + date + '.tif' gdal_calc.Calc(calc="B-A", A=src_path + vh, B=src_path + vv, outfile=output_vvvh, overwrite=True) gdal.Translate(output_vh, src_path + vh, format="GTiff") gdal.Translate(output_vv, src_path + vv, format="GTiff") print_('rm ' + tiles_path + dirLoc) shutil.rmtree(tiles_path + dirLoc)
in_file = in_dir + variable + '_avg_' + str(year) + '_extracted.tif' output = out_dir + variable + '_1yrAvg_' + str(year) + '.tif' gdal_calc.Calc(calc=avg1year, A=in_file, A_band=1, B=in_file, B_band=2, C=in_file, C_band=3, D=in_file, D_band=4, E=in_file, E_band=5, F=in_file, F_band=6, G=in_file, G_band=7, H=in_file, H_band=8, I=in_file, I_band=9, J=in_file, J_band=10, K=in_file, K_band=11, L=in_file, L_band=12, outfile=output, format='GTiff', NoDataValue=None) # Calculate 15-year averages
def crea_capa(ecuacion, rasters_input, salida): path_A = '' path_B = '' path_C = '' path_D = '' path_E = '' path_F = '' path_G = '' path_H = '' total_raster = len(rasters_input) for a, b in zip(range(total_raster), rasters_input): if a == 0: path_A = b elif a == 1: path_B = b elif a == 2: path_C = b elif a == 3: path_D = b elif a == 4: path_E = b elif a == 5: path_F = b elif a == 6: path_G = b elif a == 7: path_H = b if total_raster == 1: gdal_calc.Calc(calc=ecuacion, A=path_A, quiet=True, NoDataValue=-9999.0, outfile=salida) if total_raster == 2: gdal_calc.Calc(calc=ecuacion, A=path_A, B=path_B, quiet=True, NoDataValue=-9999.0, outfile=salida) if total_raster == 3: gdal_calc.Calc(calc=ecuacion, A=path_A, B=path_B, C=path_C, quiet=True, NoDataValue=-9999.0, outfile=salida) if total_raster == 4: gdal_calc.Calc(calc=ecuacion, A=path_A, B=path_B, C=path_C, D=path_D, quiet=True, NoDataValue=-9999.0, outfile=salida) if total_raster == 5: gdal_calc.Calc(calc=ecuacion, A=path_A, B=path_B, C=path_C, D=path_D, E=path_E, quiet=True, NoDataValue=-9999.0, outfile=salida) if total_raster == 6: gdal_calc.Calc(calc=ecuacion, A=path_A, B=path_B, C=path_C, D=path_D, E=path_E, F=path_F, quiet=True, NoDataValue=-9999.0, outfile=salida) if total_raster == 7: gdal_calc.Calc(calc=ecuacion, A=path_A, B=path_B, C=path_C, D=path_D, E=path_E, F=path_F, G=path_G, quiet=True, NoDataValue=-9999.0, outfile=salida) if total_raster == 8: gdal_calc.Calc(calc=ecuacion, A=path_A, B=path_B, C=path_C, D=path_D, E=path_E, F=path_F, G=path_G, H=path_H, quiet=True, NoDataValue=-9999.0, outfile=salida)