Newer
Older
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import glob, os
import rasterio
import numpy as np
from rasterio.enums import Resampling
def resample_SCL(list_L2A, resampled_path, upscale_factor):
for L2A_safe in list_L2A:
print(f'Start resampling SCL of L2A image: {L2A_safe}')
im_file_20m = glob.glob(f'{L2A_safe}/GRANULE/*/IMG_DATA/R20m/*_SCL_20m.jp2')[0]
im_file_20m = im_file_20m.replace('\\','/')
im_file_10m = f'{resampled_path}{os.path.basename(im_file_20m).replace("20m","10m").replace("jp2","tif")}'
if not os.path.isfile(im_file_10m):
# Open file
src = rasterio.open(im_file_20m, "r", driver='JP2OpenJPEG')
# Resample data to target shape
resampled_data = src.read(out_shape = (src.count,
int(src.height * upscale_factor),
int(src.width * upscale_factor)
),
resampling = Resampling.nearest)
# Scale image transform
new_transform = src.transform * src.transform.scale(
(src.width / resampled_data.shape[-1]),
(src.height / resampled_data.shape[-2])
)
# Update metadata
profile = src.profile
profile.update(driver = 'GTiff',
width = src.width*upscale_factor,
height = src.height*upscale_factor,
transform = new_transform)
# Write resampled image
dst = rasterio.open(im_file_10m, "w", **profile)
dst.write(resampled_data)
# Close rasterio objects
src.close()
dst.close()
print(f'--> A new resampled raster file is created : {im_file_10m}')
print('--> All SCL are resampled !')
def resample_bands(list_L2A, resampled_path, upscale_factor, bands_20m_list, resampling_method=Resampling.bilinear):
for L2A_safe in list_L2A:
print(f'Start resampling L2A image: {L2A_safe}')
for band in bands_20m_list:
print("Start resampling band : ", band)
im_file_20m = glob.glob(f'{L2A_safe}/GRANULE/*/IMG_DATA/R20m/*{band}_20m.jp2')[0]
im_file_20m = im_file_20m.replace('\\','/')
im_file_10m = f'{resampled_path}{os.path.basename(im_file_20m).replace("20m","10m").replace("jp2","tif")}'
if not os.path.isfile(im_file_10m):
# Open file
src = rasterio.open(im_file_20m, "r", driver='JP2OpenJPEG')
# Resample data to target shape
resampled_data = src.read(out_shape = (src.count,
int(src.height * upscale_factor),
int(src.width * upscale_factor)
),
resampling = resampling_method)
# Scale image transform
new_transform = src.transform * src.transform.scale(
(src.width / resampled_data.shape[-1]),
(src.height / resampled_data.shape[-2])
)
# Update metadata
profile = src.profile
profile.update(driver = 'GTiff',
width = src.width*upscale_factor,
height = src.height*upscale_factor,
transform = new_transform)
# Write resampled image
dst = rasterio.open(im_file_10m, "w", **profile)
dst.write(resampled_data)
# Close rasterio objects
src.close()
dst.close()
print(f'--> A new resampled raster file is created : {im_file_10m}')
print('--> All reflectances are resampled !')
def clip_imgs(list_im_to_clip, clipped_path, roi_gdf):
for im_file in list_im_to_clip:
im_file_roi = f'{clipped_path}{os.path.basename(im_file)[:-4]}_ROI.tif'
if not os.path.isfile(im_file_roi):
# Open file
src = rasterio.open(im_file, "r")
# Clip the raster to the extent of the shape
out_image, out_transform = rasterio.mask.mask(src,
roi_gdf.geometry,
all_touched=True,
crop=True)
# Update metadata
out_meta = src.meta
out_meta.update(driver='GTiff',
width = out_image.shape[2],
height = out_image.shape[1],
transform = out_transform)
# Write clipped image
dst = rasterio.open(im_file_roi, "w", **out_meta)
dst.write(out_image)
# Close rasterio objects
src.close()
dst.close()
print(f'A new raster file is created : {im_file_roi}')
print('--> All images are clipped !')
def apply_SCL(list_im_ROI, clipped_path, masked_path, nodata_val,values_to_mask = [0,1,2,3,8,9,10,11]):
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
for im_file in list_im_ROI:
# Get date of image
date = os.path.basename(im_file)[7:7+15]
# Get tile of image
tile = os.path.basename(im_file)[0:6]
# Find SCL corresponding to the given reflectances image (same date and same tile)
scl_file = glob.glob(f'{clipped_path}*{tile}*{date}*SCL_10m_ROI.tif')[0]
scl_file = scl_file.replace('\\','/')
im_file_scl = f'{masked_path}{os.path.basename(im_file)[:-4]}_SCL.tif'
if not os.path.isfile(im_file_scl):
# Open SCL and change invalid pixels categories by NaN
src = rasterio.open(scl_file, "r")
# Read file as numpy array
SCL = src.read(1)
src.close()
#print('Scene Classification map')
#show(SCL, cmap='Set3')
SCL = SCL.astype(float)
for i in values_to_mask:
SCL[SCL == i] = np.nan
SCL[~np.isnan(SCL)] = 1
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# Open file
src = rasterio.open(im_file, "r")
# Read file as numpy array
im = src.read(1)
# Update metadata
profile = src.profile
profile.update(dtype=rasterio.int16, # Set to int16 it is lighter than float
nodata=nodata_val, # Set nodata value in metadata
compress='lzw') # Compression option
# Mask image reflectance with SCL
im_SLC = im * SCL
# Change numpy NaN by nodata_val (e.g. -10000)
im_SLC[np.isnan(im_SLC)] = nodata_val
# Change the array's type : from float to integer 16
im_SLC = im_SLC.astype(np.int16)
# Write image
dst = rasterio.open(im_file_scl, 'w', **profile)
dst.write(im_SLC, 1)
# Close rasterio objects
src.close()
dst.close()
print(f'A new raster file is created : {im_file_scl}')
print('--> SCL is applied on all images !')