diff --git a/2.combine_mask_and_fragmentation_tiles.py b/2.combine_mask_and_fragmentation_tiles.py new file mode 100644 index 0000000000000000000000000000000000000000..16c9cd58984b0a2cd288649d16d08ca3619931f3 --- /dev/null +++ b/2.combine_mask_and_fragmentation_tiles.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- +""" +Created on Thu Oct 19 16:25:58 2023 + +@author: no67wuwu + + +work: combine the rasters to get the final fragmentation with land + +""" +#%% +import sys +sys.path.insert(0,r"I:\biocon\Emmanuel_Oceguera\projects\2023_03_NaturaConnect\tools") +import MacPyver as mp +#%% +import os +from glob import glob +import numpy as np + +frag_path = r"S:\Emmanuel_OcegueraConchas\fragmentation_estonia_test\frag_raster" +land_path = r"S:\Emmanuel_OcegueraConchas\fragmentation_estonia_test\land_raster" +out_path = r"S:\Emmanuel_OcegueraConchas\fragmentation_estonia_test\frag_and_land" + +# Get a list of tile filenames +tiles = [x[-15:] for x in glob(frag_path + os.sep + '*.tif')] + +for t in tiles: + frag_p = frag_path + os.sep + 'frag_tile_' + t + land_p = land_path + os.sep + 'mask_tile_' + t + + frag = mp.raster.tiff.read_tif(frag_p) + land = mp.raster.tiff.read_tif(land_p) + + out = np.where((frag==9) & (land==1), 9, land) + + mp.raster.tiff.write_tif(frag_p, out_path + os.sep + 'lf_comb_' + t, out, 6, nodata=0, option="COMPRESS=DEFLATE") + +#%% +# # Import necessary libraries +# import os +# from glob import glob +# import numpy as np + +# # Define the paths for input rasters and output directory +# frag_path = r"S:\Emmanuel_OcegueraConchas\Fragmentation\frag_raster" +# land_path = r"S:\Emmanuel_OcegueraConchas\Fragmentation\land_raster" +# out_path = r"S:\Emmanuel_OcegueraConchas\Fragmentation\frag_land" + +# # Get a list of tile filenames +# tiles_2 = [os.path.basename(x) for x in glob(os.path.join(frag_path, '*.tif'))] + +# # Loop through the tiles and combine the rasters +# for t in tiles: +# frag_p = os.path.join(frag_path, 'frag_tile_' + t) +# land_p = os.path.join(land_path, 'mask_tile_' + t) +# out_p = os.path.join(out_path, 'lf_comb_' + t) + +# # Read the frag and land rasters +# frag = mp.raster.tiff.read_tif(frag_p) +# land = mp.raster.tiff.read_tif(land_p) + +# # Combine the rasters +# out = np.where((frag == 9) & (land == 1), 9, land) + +# # Write the combined raster to the output directory +# mp.raster.tiff.write_tif(frag_p, out_p, out, 6, nodata=0, option="COMPRESS=DEFLATE") diff --git a/3.generate_list_valid_tiles.py b/3.generate_list_valid_tiles.py new file mode 100644 index 0000000000000000000000000000000000000000..b4da4d33cae2e9e356f34a14f9e133f89dddc06d --- /dev/null +++ b/3.generate_list_valid_tiles.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- +""" +Created on Mon Oct 23 16:05:56 2023 + +@author: no67wuwu +""" + +import os + +import subprocess + +from glob import glob + + +# Define the function +def get_stdout(cmd, verbose=False): + """ + send command to the commandline and fetch the return + + oprion verbose: will also print the return + + from http://blog.kagesenshi.org/2008/02/teeing-python-subprocesspopen-output.html + """ + p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + stdout = [] + while True: + line = p.stdout.readline() + line = line.decode('utf-8') # Decode the bytes into a string + line = line.replace('\n','').replace('\r','') + if line != '': + stdout.append(line) + if verbose: + print(line), + if line == '' and p.poll() != None: #p.poll is checking if the process is still running + #is it is running it returns None + break + return stdout + +# Define the pathe where all the combined files are +# path = r'S:\Emmanuel_OcegueraConchas\Fragmentation\frag_and_land\*.tif' +path = r"S:\Emmanuel_OcegueraConchas\fragmentation_estonia_test\frag_and_land\*.tif" + +# get the list of all the files +files = glob(path) + +# Define the gdal command +cmd = 'gdalinfo -stats %s' + +# Open a text file in writing modus. This file will be used to store the paths of the tif files that meet cetain criteria +vrt_imgs = open(r'S:\Emmanuel_OcegueraConchas\fragmentation_estonia_test\valid_tiles.txt', 'w') + +for f in files: + stats = get_stdout(cmd %f) + if not stats[0].startswith('ERROR'): + vrt_imgs.write(f+'\n') + else: + print ('not considered: %s' %f.split(os.sep)[-1]) + +vrt_imgs.close()