-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresampler.py
49 lines (40 loc) · 1.89 KB
/
resampler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Script to resample Sentinel-2 images to 10 meters using gdal library
from osgeo import gdal
import matplotlib.pyplot as plt
import os
def resample_to_10m(source_dir):
# Get the path to the directory with the sen2cor .tif files
folder_path = source_dir
# Define the resampling parameters
x_res = 10
y_res = 10
# Create the directory where the resampled files will be stored
output_dir = os.path.join(folder_path, 'Sen2Cor_10m_Resampled')
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Iterate over each .tif file inside the folder with all the sen2cor generated .tif files
for root, dirs, files in os.walk(folder_path):
if 'IMG_DATA' in root:
for file in files:
# Filter by .tif files
if file.endswith(".tif"):
# Absolute path of each .tif file that will be resampled
tif_path = os.path.join(root, file)
print('tif_path: ', tif_path)
# Open the file with gdal library
ds = gdal.Open(tif_path)
# Resample defining the resampling algorithm and output directory
dsRes = gdal.Warp(os.path.join(folder_path, 'Sen2Cor_10m_Resampled', 'resampled_'+file), ds, xRes=x_res, yRes=y_res,
resampleAlg="near")
# To visualize the resampling results
# array = ds.GetRasterBand(1).ReadAsArray()
# plt.figure()
# plt.imshow(array)
# plt.colorbar()
# plt.show()
# Check the generated results in the console
print(dsRes.GetGeoTransform())
print(dsRes.GetProjection())
# Close the datasets
ds = dsRes = None
return output_dir