-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper.py
342 lines (301 loc) · 12 KB
/
helper.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
"""
Module for calculating a list of vegetation indices from a datacube containing bands without a user having to implement callback functions
"""
from openeo.rest.datacube import DataCube
from openeo.processes import ProcessBuilder, array_modify, power, sqrt, if_, multiply, divide, arccos, add, subtract, linear_scale_range
from shapely.geometry import Point
import numpy as np
import netCDF4 as nc
import glob
import seaborn as sns
from matplotlib.dates import DateFormatter
import geopandas as gpd
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap, BoundaryNorm
import earthpy.plot as ep
import pandas as pd
import rasterio
WL_B04 = 0.6646
WL_B08 = 0.8328
WL_B11 = 1.610
one_over_pi = 1. / np.pi
# source: https://git.vito.be/projects/LCLU/repos/satio/browse/satio/rsindices.py
ndvi = lambda B04, B08: (B08 - B04) / (B08 + B04)
ndmi = lambda B08, B11: (B08 - B11) / (B08 + B11)
ndgi = lambda B03, B04: (B03 - B04) / (B03 + B04)
def anir(B04, B08, B11):
a = sqrt(np.square(WL_B08 - WL_B04) + power(B08 - B04, 2))
b = sqrt(np.square(WL_B11 - WL_B08) + power(B11 - B08, 2))
c = sqrt(np.square(WL_B11 - WL_B04) + power(B11 - B04, 2))
# calculate angle with NIR as reference (ANIR)
site_length = (power(a, 2) + power(b, 2) - power(c, 2)) / (2 * a * b)
site_length = if_(site_length.lt(-1), -1, site_length)
site_length = if_(site_length.gt(1), 1, site_length)
return multiply(one_over_pi, arccos(site_length))
ndre1 = lambda B05, B08: (B08 - B05) / (B08 + B05)
ndre2 = lambda B06, B08: (B08 - B06) / (B08 + B06)
ndre5 = lambda B05, B07: (B07 - B05) / (B07 + B05)
indices = {
"NDVI": [ndvi, (0,1)],
"NDMI": [ndmi, (-1,1)],
"NDGI": [ndgi, (-1,1)],
"ANIR": [anir, (0,1)],
"NDRE1": [ndre1, (-1,1)],
"NDRE2": [ndre2, (-1,1)],
"NDRE5": [ndre5, (-1,1)]
}
def _callback(x: ProcessBuilder, index_list: list, datacube: DataCube, scaling_factor: int) -> ProcessBuilder:
index_values = []
x_res = x
for index_name in index_list:
if index_name not in indices:
raise NotImplementedError("Index " + index_name + " has not been implemented.")
index_fun, index_range = indices[index_name]
band_indices = [
datacube.metadata.get_band_index(band)
for band in index_fun.__code__.co_varnames[:index_fun.__code__.co_argcount]
]
index_result = index_fun(*[x.array_element(i) for i in band_indices])
if scaling_factor is not None:
index_result = index_result.linear_scale_range(*index_range, 0, scaling_factor)
index_values.append(index_result)
if scaling_factor is not None:
x_res = x_res.linear_scale_range(0,8000,0,scaling_factor)
return array_modify(data=x_res, values=index_values, index=len(datacube.metadata._band_dimension.bands))
def compute_indices(datacube: DataCube, index_list: list, scaling_factor: int = None) -> DataCube:
"""
Computes a list of indices from a datacube
param datacube: an instance of openeo.rest.DataCube
param index_list: a list of indices. The following indices are currently implemented: NDVI, NDMI, NDGI, ANIR, NDRE1, NDRE2 and NDRE5
return: the datacube with the indices attached as bands
"""
return datacube.apply_dimension(dimension="bands",
process=lambda x: _callback(x, index_list, datacube, scaling_factor)).rename_labels('bands',
target=datacube.metadata.band_names + index_list)
def lin_scale_range(x,inputMin,inputMax,outputMin,outputMax):
return add(multiply(divide(subtract(x,inputMin), subtract(inputMax, inputMin)), subtract(outputMax, outputMin)), outputMin)
def _random_point_in_shp(shp):
within = False
while not within:
x = np.random.uniform(shp.bounds[0], shp.bounds[2])
y = np.random.uniform(shp.bounds[1], shp.bounds[3])
within = shp.contains(Point(x, y))
return Point(x,y)
def point_sample_fields(crop_samples, nr_iterations):
points = {"name":[], "geometry":[]}
for name,crop_df in crop_samples.items():
for num in range(nr_iterations):
points["name"] += [name]*len(crop_df)
points["geometry"] += np.asarray(crop_df['geometry'].apply(_random_point_in_shp)).tolist()
gpd_points = gpd.GeoDataFrame(points, crs="EPSG:4326")
gpd_points_utm = gpd_points.to_crs("EPSG:32631")
points_per_type = {}
for i in set(gpd_points_utm["name"]):
crop = gpd_points_utm[gpd_points_utm["name"]==i].buffer(1).to_crs("EPSG:4326").to_json()
points_per_type[i] = crop
return points_per_type
def prep_boxplot(year, bands):
df = pd.DataFrame(columns=["Crop type","Date","Band","Iteration nr","Band value"])
for file in glob.glob('.\\data\\300_*\\*.nc'):
ds_orig = nc.Dataset(file)
dt_rng = pd.date_range("01-01-"+str(year), "31-12-"+str(year),freq="MS")
spl = file.split("\\")
f_name = spl[-1].split(".")[0]
crop_type = spl[-2].split("_")[-1]
for band in bands:
try:
ds = ds_orig[band][:]
except:
print("File "+file+" is corrupt. Please remove it from your folder.")
vals = None
if ds.shape[1:3] == (1,2):
vals = np.mean(ds,axis=2).flatten().tolist()
elif ds.shape[1:3] == (2,1):
vals = np.mean(ds,axis=1).flatten().tolist()
elif ds.shape[1:3] == (1,1):
vals = ds.flatten().tolist()
elif ds.shape[1:3] == (2,2):
vals = np.mean(np.mean(ds,axis=1),axis=1).tolist()
else:
print(file)
df = df.append(pd.DataFrame({
"Crop type": crop_type,
"Date": dt_rng,
"Band": band,
"Iteration nr": [f_name]*12,
"Band value": vals
}), ignore_index=True)
df["Band value"] /= 250
return df
def create_boxplots(crop_df=None, year=2019):
bands = ["B08", "B11", "NDVI", "ratio"]
if crop_df is None:
crop_df = prep_boxplot(year, bands)
x_dates = crop_df["Date"].dt.strftime("%m-%d-%y").unique()
for crop in set(crop_df["Crop type"]):
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2,figsize=(18,18))
fig.suptitle(crop,y=0.91)
axes = [ax1, ax2, ax3, ax4]
df_m = crop_df[crop_df["Crop type"]==crop]
for i in range(4):
df_m_n = df_m[df_m["Band"]==bands[i]]
sns.boxplot(ax=axes[i],data=df_m_n, x="Date",y="Band value")
axes[i].set_xticklabels(labels=x_dates, rotation=45, ha='right')
axes[i].title.set_text(str(bands[i])+" per month")
axes[i].set_ylim(0,1)
comb = {
0: "none",
1: "corn",
2: "barley",
3: "corn barley",
4: "sugarbeet",
5: "sugarbeet corn",
6: "sugarbeet barley",
7: "sugarbeet barley corn",
8: "potato",
9: "potato corn",
10: "potato barley",
11: "potato barley corn",
12: "potato sugarbeet",
13: "potato sugarbeet corn",
14: "potato sugarbeet barley",
15: "potato sugarbeet barley corn",
16: "soy",
17: "soy corn",
18: "soy barley",
19: "soy barley corn",
20: "soy sugarbeet",
21: "soy sugarbeet corn",
22: "soy sugarbeet barley",
23: "soy sugarbeet barley corn",
24: "soy potato",
25: "soy potato corn",
26: "soy potato barley",
27: "soy potato barley corn",
28: "soy potato sugarbeet",
29: "soy potato sugarbeet corn",
30: "soy potato sugarbeet barley",
31: "soy potato sugarbeet barley corn"
}
col_palette = ['linen',
'chartreuse',
'tomato',
'olivedrab',
'maroon',
'whitesmoke',
'wheat',
'palevioletred',
'darkturquoise',
'tomato',
'thistle',
'teal',
'darkgoldenrod',
'darkmagenta',
'darkorange',
'sienna',
'black',
'silver',
'tan',
'seagreen',
'mediumspringgreen',
'lightseagreen',
'royalblue',
'mediumpurple',
'plum',
'darkcyan',
'moccasin',
'rosybrown',
'gray',
'sandybrown',
'm',
'navy']
def plot_croptypes(fn='./data/total.tif',only_unique_classes=True):
with rasterio.open(fn,mode="r+",crs=rasterio.crs.CRS({"init": "epsg:4326"})) as dataset:
ds = dataset.read(1)
if only_unique_classes:
ds = np.where(np.isin(ds, [1,2,4,8,16]), ds, 0)
keys = np.unique(ds).astype(int)
height_class_labels = [comb[key] for key in comb.keys() if key in keys]
colors = col_palette[0:len(height_class_labels)]
cmap = ListedColormap(colors)
class_bins = [-0.5]+[i+0.5 for i in keys]
norm = BoundaryNorm(class_bins, len(colors))
f, ax = plt.subplots(figsize=(10, 8))
im = ax.imshow(ds, cmap=cmap, norm=norm)
ep.draw_legend(im, titles=height_class_labels)
ax.set(title="Rule-based crop classification")
ax.set_axis_off()
plt.show()
def get_classification_colors():
cmap = ListedColormap(col_palette)
classification_colors = {x:cmap(x) for x in range(0, len(col_palette))}
return classification_colors
def get_trained_model():
year = 2019
bands = ["B08", "B11", "NDVI", "ratio"]
df = pd.DataFrame(columns=["Crop type","Date","Iteration nr"]+bands)
for file in glob.glob('.\\data\\300_*\\*.nc'):
ds_orig = nc.Dataset(file)
dt_rng = pd.date_range("01-01-"+str(year), "01-01-"+str(year+1),freq="MS")
spl = file.split("\\")
f_name = spl[-1].split(".")[0]
crop_type = spl[-2].split("_")[-1]
df_row = {
"Crop type": crop_type[0:12],
"Date": dt_rng[0:12],
"Iteration nr": [f_name]*12,
}
for band in bands:
try:
ds = ds_orig[band][:]
except:
print("File "+file+" is corrupt. Please remove it from your folder.")
vals = None
if ds.shape[1:3] == (1,2):
vals = np.mean(ds,axis=2).flatten().tolist()
elif ds.shape[1:3] == (2,1):
vals = np.mean(ds,axis=1).flatten().tolist()
elif ds.shape[1:3] == (1,1):
vals = ds.flatten().tolist()
elif ds.shape[1:3] == (2,2):
vals = np.mean(np.mean(ds,axis=1),axis=1).tolist()
else:
print(file)
df_row[band] = vals[0:12] #[x/250 if x is not None else x for x in vals]
df = df.append(pd.DataFrame(df_row), ignore_index=True)
df = df[pd.notnull(df["B08"])]
X = df[["NDVI","B08","B11","ratio"]]
y = df["Crop type"]
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
clf=RandomForestClassifier(n_estimators=100)
clf.fit(X_train,y_train)
return clf
def prep_df(year, bands):
df = pd.DataFrame(columns=["Crop type","Iteration nr"]+bands)
for file in glob.glob('.\\data\\rf_300_*\\*.nc'):
ds_orig = nc.Dataset(file)
spl = file.split("\\")
f_name = spl[-1].split(".")[0]
crop_type = spl[-2].split("_")[-1]
df_row = {
"Crop type": crop_type,
"Iteration nr": f_name,
}
for band in bands:
try:
ds = ds_orig[band][:]
except:
print("File "+file+" is corrupt. Please remove it from your folder.")
vals = None
ds[ds.mask] = np.nan
if ds.shape in [(1,2),(2,1),(1,1),(2,2)]:
vals = np.mean(ds)
else:
print(file)
df_row[band] = vals
#df = df.append(pd.DataFrame(df_row), ignore_index=True) # Deprecated
df = pd.concat([df, pd.DataFrame([df_row])], ignore_index=True)
return df.dropna()