-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainingData.py
561 lines (395 loc) · 18.3 KB
/
trainingData.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# -*- coding: utf-8 -*-
"""
ExportLandsatSRComposite.py, SERVIR-Mekong (2017-07-30)
export landsat composites with gapfilling
________________________________________________________________________________
Usage
------
$ python model.py {options}
{options} include:
--year (-y) : required
: year to create the Landsat composite image for
: in format YYYY
--user (-u) : user account used to create the composite
: changes the ~/.config/earthengine/credentials file
: dictionary is called to get credentials
: options are servirmekong, servir-mekong, ate, biplov .. default is servir-mekong
Example Usage
-------------
1) export surface reflectance composite for dryhot season of 2000 to assets:
$ python model.py -y 2000 -u Quyen
"""
import ee
import logging
import time
import math
from usercredentials import addUserCredentials
import argparse
import numpy as np
class environment(object):
def __init__(self):
"""Initialize the environment."""
# Initialize the Earth Engine object, using the authentication credentials.
#ee.Initialize()
self.timeString = time.strftime("%Y%m%d_%H%M%S")
self.assetName = "test_primitives"
self.userID = "projects/servir-mekong/temp/"
self.pixSize = 30
self.nModels = 10
# Load Mekong study region (Myanmar, Thailand, Laos, Vietnam, Cambodia)
mekongBuffer = ee.FeatureCollection('ft:1LEGeqwlBCAlN61ie5ol24NdUDqB1MgpFR_sJNWQJ');
mekongRegion = mekongBuffer.geometry();
self.studyArea = mekongRegion;
self.trainingDataSet = ee.FeatureCollection("mytrainingData")
self.classFieldName = 'land_class';
self.modelType = 'RF'
class indices():
def __init__(self):
# list with functions to call for each index
self.functionList = {"ND_blue_green" : self.ND_blue_green, \
"ND_blue_red" : self.ND_blue_red, \
"ND_blue_nir" : self.ND_blue_nir, \
"ND_blue_swir1" : self.ND_blue_swir1, \
"ND_blue_swir2" : self.ND_blue_swir2, \
"ND_green_red" : self.ND_green_red, \
"ND_green_nir" : self.ND_green_nir, \
"ND_green_swir1" : self.ND_green_swir1, \
"ND_green_swir2" : self.ND_green_swir2, \
"ND_red_swir1" : self.ND_red_swir1, \
"ND_red_swir2" : self.ND_red_swir2, \
"ND_nir_red" : self.ND_nir_red, \
"ND_nir_swir1" : self.ND_nir_swir1, \
"ND_nir_swir2" : self.ND_nir_swir2, \
"ND_swir1_swir2" : self.ND_swir1_swir2, \
"R_swir1_nir" : self.R_swir1_nir, \
"R_red_swir1" : self.R_red_swir1, \
"EVI" : self.EVI, \
"SAVI" : self.SAVI, \
"IBI" : self.IBI}
def addAllTasselCapIndices(self,img):
""" Function to get all tasselCap indices """
def getTasseledCap(img):
"""Function to compute the Tasseled Cap transformation and return an image"""
logging.info('get tasselcap for computed images')
coefficients = ee.Array([
[0.3037, 0.2793, 0.4743, 0.5585, 0.5082, 0.1863],
[-0.2848, -0.2435, -0.5436, 0.7243, 0.0840, -0.1800],
[0.1509, 0.1973, 0.3279, 0.3406, -0.7112, -0.4572],
[-0.8242, 0.0849, 0.4392, -0.0580, 0.2012, -0.2768],
[-0.3280, 0.0549, 0.1075, 0.1855, -0.4357, 0.8085],
[0.1084, -0.9022, 0.4120, 0.0573, -0.0251, 0.0238]
]);
bands=ee.List(['blue','green','red','nir','swir1','swir2'])
# Make an Array Image, with a 1-D Array per pixel.
arrayImage1D = img.select(bands).toArray()
# Make an Array Image with a 2-D Array per pixel, 6x1.
arrayImage2D = arrayImage1D.toArray(1)
componentsImage = ee.Image(coefficients).matrixMultiply(arrayImage2D).arrayProject([0]).arrayFlatten([['brightness', 'greenness', 'wetness', 'fourth', 'fifth', 'sixth']]).float();
# Get a multi-band image with TC-named bands.
return img.addBands(componentsImage);
def addTCAngles(img):
""" Function to add Tasseled Cap angles and distances to an image. Assumes image has bands: 'brightness', 'greenness', and 'wetness'."""
logging.info('add tasseled cap angles')
# Select brightness, greenness, and wetness bands
brightness = img.select('brightness');
greenness = img.select('greenness');
wetness = img.select('wetness');
# Calculate Tasseled Cap angles and distances
tcAngleBG = brightness.atan2(greenness).divide(math.pi).rename(['tcAngleBG']);
tcAngleGW = greenness.atan2(wetness).divide(math.pi).rename(['tcAngleGW']);
tcAngleBW = brightness.atan2(wetness).divide(math.pi).rename(['tcAngleBW']);
tcDistBG = brightness.hypot(greenness).rename(['tcDistBG']);
tcDistGW = greenness.hypot(wetness).rename(['tcDistGW']);
tcDistBW = brightness.hypot(wetness).rename(['tcDistBW']);
img = img.addBands(tcAngleBG).addBands(tcAngleGW).addBands(tcAngleBW).addBands(tcDistBG).addBands(tcDistGW).addBands(tcDistBW);
return img;
img = getTasseledCap(img)
img = addTCAngles(img)
return img
def ND_blue_green(self,img):
img = img.addBands(img.normalizedDifference(['blue','green']).rename(['ND_blue_green']));
return img
def ND_blue_red(self,img):
img = img.addBands(img.normalizedDifference(['blue','red']).rename(['ND_blue_red']));
return img
def ND_blue_nir(self,img):
img = img.addBands(img.normalizedDifference(['blue','nir']).rename(['ND_blue_nir']));
return img
def ND_blue_swir1(self,img):
img = img.addBands(img.normalizedDifference(['blue','swir1']).rename(['ND_blue_swir1']));
return img
def ND_blue_swir2(self,img):
img = img.addBands(img.normalizedDifference(['blue','swir2']).rename(['ND_blue_swir2']));
return img
def ND_green_red(self,img):
img = img.addBands(img.normalizedDifference(['green','red']).rename(['ND_green_red']));
return img
def ND_green_nir(self,img):
img = img.addBands(img.normalizedDifference(['green','nir']).rename(['ND_green_nir'])); # NDWBI
return img
def ND_green_swir1(self,img):
img = img.addBands(img.normalizedDifference(['green','swir1']).rename(['ND_green_swir1'])); # NDSI, MNDWI
return img
def ND_green_swir2(self,img):
img = img.addBands(img.normalizedDifference(['green','swir2']).rename(['ND_green_swir2']));
return img
def ND_red_swir1(self,img):
img = img.addBands(img.normalizedDifference(['red','swir1']).rename(['ND_red_swir1']));
return img
def ND_red_swir2(self,img):
img = img.addBands(img.normalizedDifference(['red','swir2']).rename(['ND_red_swir2']));
return img
def ND_nir_red(self,img):
img = img.addBands(img.normalizedDifference(['nir','red']).rename(['ND_nir_red'])); # NDVI
return img
def ND_nir_swir1(self,img):
img = img.addBands(img.normalizedDifference(['nir','swir1']).rename(['ND_nir_swir1'])); # NDWI, LSWI, -NDBI
return img
def ND_nir_swir2(self,img):
img = img.addBands(img.normalizedDifference(['nir','swir2']).rename(['ND_nir_swir2'])); # NBR, MNDVI
return img
def ND_swir1_swir2(self,img):
img = img.addBands(img.normalizedDifference(['swir1','swir2']).rename(['ND_swir1_swir2']));
return img
def R_swir1_nir(self,img):
# Add ratios
img = img.addBands(img.select('swir1').divide(img.select('nir')).rename(['R_swir1_nir'])); # ratio 5/4
return img
def R_red_swir1(self,img):
img = img.addBands(img.select('red').divide(img.select('swir1')).rename(['R_red_swir1'])); # ratio 3/5
return img
def EVI(self,img):
#Add Enhanced Vegetation Index (EVI)
evi = img.expression(
'2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {
'NIR': img.select('nir'),
'RED': img.select('red'),
'BLUE': img.select('blue')
}).float();
img = img.addBands(evi.rename(['EVI']));
return img
def SAVI(self,img):
# Add Soil Adjust Vegetation Index (SAVI)
# using L = 0.5;
savi = img.expression(
'(NIR - RED) * (1 + 0.5)/(NIR + RED + 0.5)', {
'NIR': img.select('nir'),
'RED': img.select('red')
}).float();
img = img.addBands(savi.rename(['SAVI']));
return img
def IBI(self,img):
# Add Index-Based Built-Up Index (IBI)
ibi_a = img.expression(
'2*SWIR1/(SWIR1 + NIR)', {
'SWIR1': img.select('swir1'),
'NIR': img.select('nir')
}).rename(['IBI_A']);
ibi_b = img.expression(
'(NIR/(NIR + RED)) + (GREEN/(GREEN + SWIR1))', {
'NIR': img.select('nir'),
'RED': img.select('red'),
'GREEN': img.select('green'),
'SWIR1': img.select('swir1')
}).rename(['IBI_B']);
ibi_a = ibi_a.addBands(ibi_b);
ibi = ibi_a.normalizedDifference(['IBI_A','IBI_B']);
img = img.addBands(ibi.rename(['IBI']));
return img
class trainingData():
def __init__(self):
"""Initialize the Surfrace Reflectance app."""
# import the log library
import logging
# get the environment
self.env = environment()
# get object with indices
self.indices = indices()
def createTrainingSample(self,trainData,composite,y):
date = ee.Date.fromYMD(y,1,1)
trainingData_yr = ee.FeatureCollection(trainData) #.filter(ee.Filter.eq('Year',y));
print trainingData_yr.size().getInfo()
training_yr = composite.sampleRegions(trainingData_yr, [self.env.classFieldName], self.env.pixSize)
# Aggregate training data from each year into one collection
training = ee.FeatureCollection(training_yr) #.flatten();
#task = ee.batch.Export.table.toDrive(training,"Barren" + str(y));
#task.start()
#print training.first().getInfo()
return training
class primitives():
def __init__(self):
"""Initialize the Surfrace Reflectance app."""
# import the log library
import logging
# get the environment
self.env = environment()
# get object with indices
self.indices = indices()
self.train = trainingData()
def importData(self):
print "import data"
def getIndices(self,img,covariates):
""" add indices to image"""
# no need to add indices that are already there
indices = self.removeDuplicates(covariates,img.bandNames().getInfo())
for item in indices:
img = self.indices.functionList[item](img)
return img
def createIndices(self,spring,summer,autumn,winter):
"""Calculate the urban, builtup cropland rice and barren primitives """
covariates = ["ND_blue_green","ND_blue_red","ND_blue_nir","ND_blue_swir1","ND_blue_swir2","ND_green_red","ND_green_nir","ND_green_swir1","ND_green_swir2","ND_red_swir1","ND_red_swir2","ND_nir_red","ND_nir_swir1","ND_nir_swir2","ND_swir1_swir2","R_swir1_nir","R_red_swir1","EVI","SAVI","IBI"]
spring = self.ScaleBands(spring)
spring = self.indices.addAllTasselCapIndices(spring)
spring = self.getIndices(spring,covariates)
summer = self.ScaleBands(summer)
summer = self.indices.addAllTasselCapIndices(summer)
summer = self.getIndices(summer,covariates)
autumn = self.ScaleBands(autumn)
autumn = self.indices.addAllTasselCapIndices(autumn)
autumn = self.getIndices(autumn,covariates)
winter = self.ScaleBands(winter)
winter = self.indices.addAllTasselCapIndices(winter)
winter = self.getIndices(winter,covariates)
# Get the JRC water
water = ee.Image('JRC/GSW1_0/GlobalSurfaceWater').mask(ee.Image(1));
# rename the bands
spring = self.renameImageBands(spring,"spring")
autumn = self.renameImageBands(autumn,"autumn")
summer = self.renameImageBands(summer,"summer")
winter = self.renameImageBands(winter,"winter")
# construct the composites
composite = spring.addBands(summer).addBands(autumn).addBands(winter).addBands(water);
composite = self.addTopography(composite);
composite = self.addJRC(composite)
allIndices = ["ND_blue_green","ND_blue_red","ND_blue_nir","ND_blue_swir1", \
"ND_blue_swir2","ND_green_red","ND_green_nir","ND_green_swir1", \
"ND_green_swir2","ND_red_swir1","ND_red_swir2","ND_nir_red", \
"ND_nir_swir1","ND_nir_swir2","ND_swir1_swir2","R_swir1_nir",
"R_red_swir1","EVI","SAVI","IBI", \
"blue","green","red","nir","swir1","swir2",\
"blue_stdDev","green_stdDev","red_stdDev","nir_stdDev",\
"swir1_stdDev","swir2_stdDev","ND_nir_swir2_stdDev",\
"ND_green_swir1_stdDev","ND_nir_red_stdDev","thermal",\
"thermal_stdDev",'brightness','greenness','wetness',\
'tcAngleBG','tcAngleGW','tcAngleBW','tcDistBG','tcDistGW'
]
jrcBands = ['occurrence','change_abs','change_norm','seasonality','transition','max_extent']
elevationBands = ['eastness','northness','elevation','slope','aspect']
nightLights = ['stable_lights']
# combine all training bands
trainingBands = self.renameBands(allIndices,"spring") + self.renameBands(allIndices,"summer") + self.renameBands(allIndices,"autumn") + self.renameBands(allIndices,"winter") + jrcBands + elevationBands #
# select training bands
composite = composite.select(trainingBands)
return composite
def addTopography(self,img):
""" Function to add 30m SRTM elevation and derived slope, aspect, eastness, and
northness to an image. Elevation is in meters, slope is between 0 and 90 deg,
aspect is between 0 and 359 deg. Eastness and northness are unitless and are
between -1 and 1. """
# Import SRTM elevation data
elevation = ee.Image("USGS/SRTMGL1_003");
# Calculate slope, aspect, and hillshade
topo = ee.Algorithms.Terrain(elevation);
# From aspect (a), calculate eastness (sin a), northness (cos a)
deg2rad = ee.Number(math.pi).divide(180);
aspect = topo.select(['aspect']);
aspect_rad = aspect.multiply(deg2rad);
eastness = aspect_rad.sin().rename(['eastness']).float();
northness = aspect_rad.cos().rename(['northness']).float();
# Add topography bands to image
topo = topo.select(['elevation','slope','aspect']).addBands(eastness).addBands(northness);
img = img.addBands(topo);
return img;
def addJRC(self,img):
""" Function to add JRC Water layers: 'occurrence', 'change_abs',
'change_norm', 'seasonality','transition', 'max_extent' """
jrcImage = ee.Image("JRC/GSW1_0/GlobalSurfaceWater")
img = img.addBands(jrcImage.select(['occurrence']).rename(['occurrence']))
img = img.addBands(jrcImage.select(['change_abs']).rename(['change_abs']))
img = img.addBands(jrcImage.select(['change_norm']).rename(['change_norm']))
img = img.addBands(jrcImage.select(['seasonality']).rename(['seasonality']))
img = img.addBands(jrcImage.select(['transition']).rename(['transition']))
img = img.addBands(jrcImage.select(['max_extent']).rename(['max_extent']))
return img
def newCollectionToImage(self,collection):
""" Helper function to convert image collection into stack of image bands"""
def createStack(img,prev):
return ee.Image(prev).addBands(img)
stack = ee.Image(collection.iterate(createStack, ee.Image(1)));
stack = stack.select(ee.List.sequence(1, stack.bandNames().size().subtract(1)));
return stack;
def removeDuplicates(self,covariateList,bands):
""" function to remove duplicates, i.e. existing bands do not need to be calculated """
return [elem for elem in covariateList if elem not in bands]
def ScaleBands(self,img):
"""Landsat is scaled by factor 0.0001 """
thermalBand = ee.List(['thermal','thermal_stdDev'])
gapfillBand = ee.List(['gapfill'])
thermal = ee.Image(img).select(thermalBand).divide(10)
gapfill = ee.Image(img).select(gapfillBand)
otherBands = ee.Image(img).bandNames().removeAll(thermalBand)
otherBands = otherBands.removeAll(gapfillBand)
scaled = ee.Image(img).select(otherBands).multiply(0.0001)
image = ee.Image(scaled.addBands(thermal).addBands(gapfill))
return ee.Image(image.copyProperties(img))
def renameImageBands(self,image,prefix):
""" Function to add a prefix to all bands in an image """
bandNames = list(image.bandNames().getInfo());
newNames = []
for band in bandNames:
bandName = prefix + "_" + band
newNames.append(bandName)
image = image.rename(ee.List(newNames));
return image
def renameBands(self,bandNames,prefix):
""" Function to add a prefix to all bands in an image """
newNames = []
for band in bandNames:
bandName = prefix + "_" + band
newNames.append(bandName)
return newNames
def ExportToAsset(self,name,img):
"""export to asset """
outputName = self.env.userID + name + self.env.assetName + self.env.timeString
logging.info('export image to asset: ' + str(outputName))
#startDate = ee.Date.fromYMD(self.env.startYear,1,1)
#endDate = ee.Date.fromYMD(self.env.endYear,12,31)
#image = ee.Image(img).set({'system:time_start':startDate.millis(), \
region = ee.Geometry.Polygon(img.geometry().getInfo()['coordinates'])
task_ordered = ee.batch.Export.image.toAsset(image=ee.Image(img), description=self.env.assetName, assetId=outputName,region=region['coordinates'], maxPixels=1e13,scale=self.env.pixSize)
# start task
task_ordered.start()
if __name__ == "__main__":
# set argument parsing object
parser = argparse.ArgumentParser(description="Create primitive composite using Google Earth Engine.")
parser.add_argument('--year','-y', type=str,required=True, \
help="Year to perform the ats correction and save to asset format in 'YYYY'")
parser.add_argument('--user','-u', type=str, default="servir-mekong",choices=['servir-mekong','servirmekong',"ate","biplov","quyen","atesig"], \
help="specify user account to run task")
args = parser.parse_args() # get arguments
# user account to run task on
userName = args.user
year = args.year
# create a new file in ~/.config/earthengine/credentials with token of user
addUserCredentials(userName)
ee.Initialize()
tableData = ee.FeatureCollection("users/apoortinga/ReferenceData2")
years = [2016]
counter = 0
for y in years:
# import the images
spring = ee.Image("users/apoortinga/JordanImagery/spring" + str(y) + "_"+ str(y)+"Median")
summer = ee.Image("users/apoortinga/JordanImagery/summer"+ str(y)+"_" + str(y)+"Median")
autumn = ee.Image("users/apoortinga/JordanImagery/autumn"+ str(y)+"_" + str(y)+"Median")
winter = ee.Image("users/apoortinga/JordanImagery/winter"+ str(y-1)+"_" + str(y)+"Median")
composite = ee.Image(primitives().createIndices(spring,summer,autumn,winter))
print composite.bandNames().getInfo()
if counter == 0:
reference = trainingData().createTrainingSample(tableData,composite,y)
if counter > 0:
train = trainingData().createTrainingSample(tableData,composite,y)
print counter
reference = reference.merge(train)
counter+=1
task = ee.batch.Export.table.toDrive(reference,"training3");
task.start()