-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass_funs.py
executable file
·299 lines (253 loc) · 10.6 KB
/
class_funs.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
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
143
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
171
172
173
174
175
176
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
##############################
#Callable class functions #
#Maintainer: Christopher Chan#
#Date: 08/10/2020 #
#Version: 0.1.7 #
##############################
import os
import numpy as np
import rasterio
import rasterio as rio
import geopandas as gpd
import earthpy as et
import earthpy.spatial as es
import earthpy.plot as ep
import matplotlib
import matplotlib.pyplot as plt
from osgeo import gdal
from rasterio import Affine
from rasterio.enums import Resampling
from rasterio.mask import mask
from rasterio.plot import show
from rasterio.plot import plotting_extent
class transform():
def jp2tiff(jp2):
'''
Transform jp2 to tiff
Run before using class indices
'''
name1, name2 = jp2.split('.', 1)
print(name1)
path = os.path.join(name1 + '.tif')
src_ds = gdal.Open(jp2)
format = "GTiff"
driver = gdal.GetDriverByName(format)
dst_ds = driver.CreateCopy(path, src_ds, 0)
dst_ds = None
src_ds = None
print("The file is saved in: {}".format(path))
print("\n")
def resampling(raster, upscale_factor):
'''
Resampling image to factor
Run before using class indices
'''
with rasterio.open(raster) as dataset:
# resample data to target shape
data = dataset.read(
out_shape=(
dataset.count,
int(dataset.height * upscale_factor),
int(dataset.width * upscale_factor)
),
resampling=Resampling.bilinear
)
# scale image transform
transform = dataset.transform * dataset.transform.scale(
(dataset.width / data.shape[-1]),
(dataset.height / data.shape[-2])
)
name1, name2 = raster.split('.', 1)
file1 = os.path.join(name1 + "_resamp.tif")
band = rio.open(raster)
profile = band.profile
profile.update(transform=transform, driver='GTiff', height=(dataset.height * upscale_factor), width=(dataset.width * upscale_factor), crs=band.crs)
with rio.open(file1,'w', **profile) as dst:
dst.write(data)
print("The file is Resampled in: {}".format(file1))
print('Before the nROW & nCLO: ',(rio.open(raster)).shape)
print('Now the nROW & nCLO: ',(rio.open(file1)).shape)
print("\n")
def fix(image1, shp_path):
shp = gpd.read_file(shp_path)
band = rio.open(image1)
#plot.show(band)
masked, mask_transform = mask(dataset = band, shapes = shp.geometry, crop = True)
show(masked, transform = mask_transform)
out_meta = band.meta.copy()
out_meta.update({"driver": "GTiff","height": masked.shape[1],
"width": masked.shape[2],"transform": mask_transform})
name1, name2 = image1.rsplit('_', 1)
path_masked = os.path.join(name1 + "_fix.tif")
with rio.open(path_masked, "w", **out_meta) as dest:
dest.write(masked)
print("The file is Cropped in: {}".format(path_masked))
print('Before the nROW & nCLO: ',(rio.open(image1)).shape)
print('Now the nROW & nCLO: ',(rio.open(path_masked)).shape)
print("\n")
def convert(fn, form):
'''
https://numpy.org/doc/stable/user/basics.types.html
'''
t = {'Byte' : gdal.GDT_Byte,
'UInt16' : gdal.GDT_UInt16,
'Int16' : gdal.GDT_Int16,
'UInt32' : gdal.GDT_UInt32,
'Int32' : gdal.GDT_Int32,
'Float32' : gdal.GDT_Float32,
'Float64' : gdal.GDT_Float64}
kwargs = {'format': 'GTiff', 'outputType': t[form]}
# Create output directory and the output path
path_files = os.path.join(fn)
name, name0 = path_files.rsplit('/', 1)
output_dir = os.path.join(name, form)
if os.path.isdir(output_dir) == False:
os.mkdir(output_dir)
band = gdal.Open(fn)
name1, name2 = name0.split('.', 1)
path_masked = os.path.join(name, form, name1 + "_" + form + ".tif")
ds = gdal.Translate(path_masked, band, **kwargs)
# do something with ds if you need
ds = None # close and save ds
plot_band = rio.open(fn)
print("The original file is in: {}".format(fn))
print('Type before: ',(rio.open(fn)).dtypes[0])
show(plot_band)
plot_band1 = rio.open(path_masked)
print("The file is saved in: {}".format(path_masked))
print('Type now: ',(rio.open(path_masked)).dtypes[0])
show(plot_band1)
print("\n")
def stack(path, name, version):
'''
https://earthpy.readthedocs.io/en/latest/gallery_vignettes/plot_raster_stack_crop.html
Atention in the order of the path to create for instance RGB images
stack_path = [RED_path, GREEN_path, BLUE_path]
version = 1 in case you want to plot image as RGB
'''
# Create output directory and the output path
path_files = os.path.join(path[0])
name1, name2 = path_files.rsplit('/', 1)
output_dir = os.path.join(name1, "stack")
if os.path.isdir(output_dir) == False:
os.mkdir(output_dir)
output_name = os.path.join(name1, "stack", name + ".tif")
array, raster_prof = es.stack(path, out_path=output_name)
if version == 0:
name_path = []
for i in range(len(path)):
name3, name4 = path[i].rsplit('/', 1)
name_path.append(name4)
ep.plot_bands(array[i], title=name_path[i],)
plt.show()
elif version == 1:
extent = plotting_extent(array[0], raster_prof["transform"])
fig, ax = plt.subplots(figsize=(12, 12))
ep.plot_rgb(array, ax=ax, stretch=True, extent=extent, str_clip=0.5, title=name,)
plt.show()
else:
return print("No version were stacked. To plot each band select 0 and to plot image compose like RGB select 1")
print("The bands stack are: {}".format(path))
print("\n")
print("The file is saved in: {}".format(output_name))
print("\n")
class indices():
def ndvi(NIR_PATH, RED_PATH):
'''
Normalised Difference Vegetation Index
Calculates NDVI (NIR-RED)/(NIR+RED)
E.g. Sentinel-2: NIR = Band8; RED = Band4
'''
RED = gdal.Open(RED_PATH)
NIR = gdal.Open(NIR_PATH)
RED_link = RED.ReadAsArray().astype(np.float)
NIR_link = NIR.ReadAsArray().astype(np.float)
NDVI = es.normalized_diff(NIR_link, RED_link)
ep.plot_bands(NDVI, cmap="RdYlGn", cols=1, title='NDVI', vmin=-1, vmax=1)
band = rio.open(RED_PATH)
name1, name2 = RED_PATH.rsplit('/', 1)
#print(name1)
path = os.path.join(name1,'NDVI.tif')
#export ndvi image
ndviImage = rio.open(path,'w',driver='Gtiff',
width=band.width,
height = band.height,
count=1, crs=band.crs,
transform=band.transform,
dtype='float64')
ndviImage.write(NDVI,1)
ndviImage.close()
return NDVI
def ndbi(SWIR_PATH, NIR_PATH):
'''
Normaised Difference Built-up Index
Calculates NDBI (SWIR-NIR)/(SWIR+NIR)
E.g. Sentinel-2: SWIR = Band11; NIR = Band8
'''
NIR = gdal.Open(NIR_PATH)
SWIR = gdal.Open(SWIR_PATH)
NIR_link = NIR.ReadAsArray().astype(np.float)
SWIR_link = SWIR.ReadAsArray().astype(np.float)
NDBI = es.normalized_diff(SWIR_link, NIR_link)
ep.plot_bands(NDBI, cmap="RdYlGn", cols=1, title='NDBI', vmin=-1, vmax=1)
band = rio.open(SWIR_PATH)
name1, name2 = SWIR_PATH.rsplit('/', 1)
#print(name1)
path = os.path.join(name1,'NDBI.tif')
#export ndbi image
ndbiImage = rio.open(path,'w',driver='Gtiff',
width=band.width,
height = band.height,
count=1, crs=band.crs,
transform=band.transform,
dtype='float64')
ndbiImage.write(NDBI,1)
ndbiImage.close()
return NDBI
def BU (RED_PATH, NIR_PATH, SWIR_PATH):
'''
Built-up = NDBI - NDVI
'''
from class_funs import indices
BU = indices.ndvi(NIR_PATH, RED_PATH) - indices.ndbi(SWIR_PATH, NIR_PATH)
ep.plot_bands(BU, cmap="RdYlGn", cols=1, title='BU', vmin=-1, vmax=1)
band = rio.open(RED_PATH)
name1, name2 = RED_PATH.rsplit('/', 1)
#print(name1)
path = os.path.join(name1,'BU.tif')
#export BU image
buImage = rio.open(path,'w',driver='Gtiff',
width=band.width,
height = band.height,
count=1, crs=band.crs,
transform=band.transform,
dtype='float64')
buImage.write(BU,1)
buImage.close()
return BU
def sci(SWIR_PATH, nNIR_PATH):
'''
Soil Composition Index
Calculates SCI (SWIR-nNIR)/(SWIR+nNIR)
E.g. Sentinel-2: SWIR = Band 11; narrowNIR = Band8A
'''
nNIR = gdal.Open(nNIR_PATH)
SWIR = gdal.Open(SWIR_PATH)
nNIR_link = nNIR.ReadAsArray().astype(np.float)
SWIR_link = SWIR.ReadAsArray().astype(np.float)
SCI = es.normalized_diff(SWIR_link, nNIR_link)
ep.plot_bands(SCI, cmap="RdYlGn", cols=1, title='SCI', vmin=-1, vmax=1)
band = rio.open(nNIR_PATH)
name1, name2 = nNIR_PATH.rsplit('/', 1)
#print(name1)
path = os.path.join(name1,'SCI.tif')
#export SCI image
sciImage = rio.open(path,'w',driver='Gtiff',
width=band.width,
height = band.height,
count=1, crs=band.crs,
transform=band.transform,
dtype='float64')
sciImage.write(SCI,1)
sciImage.close()
return SCI