-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgeeCenterline.py
539 lines (457 loc) · 20.5 KB
/
geeCenterline.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
import ee
import numpy as np
# -*- coding: utf-8 -*-
"""
Main script for geeRiverCL
geeRiverCl: A river centerline extraction and sediment bar identification
toolbox based on Google Earth Engine
MIT License
Author: Yi Luo, Univ. of Illinois Urbana-Champaign
Contact: yiluo7[at]illinois[dot]edu
"""
def load_file(assetID, bands=None, bandnames=None):
tp = ee.data.getAsset(assetID)['type']
if tp == 'TABLE':
return load_roi(assetID)
if tp == 'IMAGE':
return load_image(assetID, bands, bandnames)
def batch_load(IDlist, bands, bandnames):
uris = ee.List(IDlist)
images = uris.map(ee.Image.loadGeoTIFF).select(bands).rename(bandnames)
collection = ee.ImageCollection(images)
return collection
def load_image(assetID, bands, names):
image = ee.Image(assetID).select(bands).rename(names)
return image
def load_roi(assetID):
roi = ee.FeatureCollection(assetID).geometry()
return roi
def classification(image, thr2=0, thr3=0.2, thr4=0.3, thr5=0.6, numPts=100):
crs = image.projection().crs().getInfo()
ndvi = image.normalizedDifference(['NIR', 'Red'])
scale = image.projection().nominalScale().getInfo()
#minDis
#Three land type simple regions: water, plant, sand (other)
bands = image.bandNames().getInfo()
labels = 'samples'
water = ndvi.lt(thr2)
sand = ndvi.lt(thr4).And(ndvi.gt(thr3)).multiply(2)
vege = ndvi.gt(thr5).multiply(3)
wsv = water.add(sand.add(vege)).selfMask().rename('samples')
samples = wsv.stratifiedSample(**{'numPoints': numPts, 'projection': crs, 'scale':30, 'geometries': True})
training = image.select(bands).sampleRegions(**{
'collection': samples,
'properties': ['samples'],
'scale' : scale
})
trained = ee.Classifier.minimumDistance().train(training, 'samples', bands)
classified = image.select(bands).classify(trained)
return classified
def close(image, radius=1.5, kernelType='circle', units='pixels', iterations=1, kernel=None):
crs = image.projection().crs().getInfo()
scale = image.projection().nominalScale()
image1 = image.focal_max(radius, kernelType, units, iterations, kernel).reproject(crs = crs, scale = scale)\
.focal_min(radius, kernelType, units, iterations, kernel).reproject(crs = crs, scale = scale)
return image1
def noise_removal(image, maxArea, connectedness):
crs = image.projection().crs().getInfo()
scale = image.projection().nominalScale()
image_mask = image.selfMask()
if connectedness == 4:
k = [[0,1,0], [1,1,1], [0,1,0]]
elif connectedness == 8:
k = [[1,1,1], [1,1,1], [1,1,1]]
image1 = image_mask.connectedComponents(ee.Kernel.fixed(3, 3, k), maxArea).\
select('labels').reproject(crs = crs, scale = scale)
image2 = image.add(image1.eq(0).eq(0).unmask()).eq(1).selfMask()
return image2
# river mask from landsat
def mask_hrs(image, r=1.5, i=3):
crs = image.projection().crs().getInfo()
scale = image.projection().nominalScale()
mask = image.focal_max(radius = r, iterations = i).reproject(crs = crs, scale = scale)
return mask
# /*
# Copyright (c) [2018] [Xiao Yang]
#
# Original Author: Xiao Yang, UNC Chapel Hill, United States
# Contact: [email protected]
# Contributing Authors:
# Tamlin Pavelsky, UNC Chapel Hill, United States
# George Allen, Texas A&M, United States
# Genna Dontchyts, Deltares, NL
# */
def hitOrMiss(image, se1, se2):
e1 = image.reduceNeighborhood(ee.Reducer.min(), se1)
e2 = image.Not().reduceNeighborhood(ee.Reducer.min(), se2)
return e1.And(e2)
def splitKernel(kernel, value):
kernel = np.array(kernel)
result = kernel
r = 0
while (r < kernel.shape[0]):
c = 0
while (c < kernel.shape[1]):
if kernel[r][c] == value:
result[r][c] = 1
else:
result[r][c] = 0
c = c + 1
r = r + 1
return result.tolist()
def Skeletonize(image, iterations, method):
se1w = [[2, 2, 2], [0, 1, 0], [1, 1, 1]]
if (method == 2):
se1w = [[2, 2, 2], [0, 1, 0], [0, 1, 0]]
se11 = ee.Kernel.fixed(3, 3, splitKernel(se1w, 1))
se12 = ee.Kernel.fixed(3, 3, splitKernel(se1w, 2))
se2w = [[2, 2, 0], [2, 1, 1], [0, 1, 0]]
if (method == 2):
se2w = [[2, 2, 0], [2, 1, 1], [0, 1, 1]]
se21 = ee.Kernel.fixed(3, 3, splitKernel(se2w, 1))
se22 = ee.Kernel.fixed(3, 3, splitKernel(se2w, 2))
result = image
i = 0
while (i < iterations):
j = 0
while (j < 4):
result = result.subtract(hitOrMiss(result, se11, se12))
se11 = se11.rotate(1)
se12 = se12.rotate(1)
result = result.subtract(hitOrMiss(result, se21, se22))
se21 = se21.rotate(1)
se22 = se22.rotate(1)
j = j + 1
i = i + 1
return result.rename(['clRaw'])
def CalcDistanceMap(img, neighborhoodSize, scale):
imgD2 = img.focal_max(1.5, 'circle', 'pixels', 2)
imgD1 = img.focal_max(1.5, 'circle', 'pixels', 1)
outline = imgD2.subtract(imgD1)
dpixel = outline.fastDistanceTransform(neighborhoodSize).sqrt()
dmeters = dpixel.multiply(scale) #// for a given scale
DM = dmeters.mask(dpixel.lte(neighborhoodSize).And(imgD2))
return DM
def CalcGradientMap(image, gradMethod, scale):
if (gradMethod == 1): # GEE .gradient() method
grad = image.gradient()
dx = grad.select(['x'])
dy = grad.select(['y'])
g = dx.multiply(dx).add(dy.multiply(dy)).sqrt()
if (gradMethod == 2): # Gena's method
k_dx = ee.Kernel.fixed(3, 3, [[ 1.0/8, 0.0, -1.0/8], [ 2.0/8, 0.0, -2.0/8], [ 1.0/8, 0.0, -1.0/8]])
k_dy = ee.Kernel.fixed(3, 3, [[ -1.0/8, -2.0/8, -1.0/8], [ 0.0, 0.0, 0.0], [ 1.0/8, 2.0/8, 1.0/8]])
dx = image.convolve(k_dx)
dy = image.convolve(k_dy)
g = dx.multiply(dx).add(dy.multiply(dy)).divide(scale**2).sqrt()
if (gradMethod == 3): # RivWidth method
k_dx = ee.Kernel.fixed(3, 1, [[-0.5, 0.0, 0.5]])
k_dy = ee.Kernel.fixed(1, 3, [[0.5], [0.0], [-0.5]])
dx = image.convolve(k_dx)
dy = image.convolve(k_dy)
g = dx.multiply(dx).add(dy.multiply(dy)).divide(scale.multiply(scale))
return g
def CalcOnePixelWidthCenterline(img, GM, hGrad):
imgD2 = img.focal_max(1.5, 'circle', 'pixels', 2)
cl = ee.Image(GM).mask(imgD2).lte(hGrad).And(img)
# // apply skeletonization twice
cl1px = Skeletonize(cl, 2, 1)
return cl1px
def ExtractEndpoints(CL1px):
clnot = CL1px.unmask().Not()
k2 = ee.Kernel.fixed(3, 3, [[1, 1, 1], [1, 0, 1], [0, 0, 1]])
k3 = ee.Kernel.fixed(3, 3, [[1, 1, 1], [1, 0, 1], [1, 0, 0]])
result = ee.Image()
# // the for loop removes the identified endpoints from the imput image
i = 0
while (i<4): # rotate kernels
result = result.addBands(clnot.reduceNeighborhood(ee.Reducer.sum(), k2).rename(str(i)+'1'))
result = result.addBands(clnot.reduceNeighborhood(ee.Reducer.sum(), k3).rename(str(i)+'1'))
k2 = k2.rotate(1)
k3 = k3.rotate(1)
i = i + 1
result = result.select(result.bandNames().getInfo()[1:])
result1 = result.reduce('max').eq(6)
endpoints = result1.And(CL1px).selfMask()
return endpoints
def ExtractCorners(CL1px):
se1w = [[0, 0, 1], [1, 1, 1], [0, 1, 0]]
se11 = ee.Kernel.fixed(3, 3, splitKernel(se1w, 1))
se12 = ee.Kernel.fixed(3, 3, splitKernel(se1w, 2))
result = CL1px
# // the for loop removes the identified corners from the imput image
i = 0
while(i < 4): # rotate kernels
result = result.subtract(hitOrMiss(result, se11, se12))
se11 = se11.rotate(1)
se12 = se12.rotate(1)
i = i + 1
cornerPoints = CL1px.subtract(result)
return cornerPoints
def ExtractJoints(CL1px):
k4 = ee.Kernel.fixed(3, 3, [[0, 1, 0], [1, 1, 1], [0, 1, 0]])
k5 = ee.Kernel.fixed(3, 3, [[1, 0, 1], [0, 1, 0], [1, 0, 1]])
k6 = ee.Kernel.fixed(3, 3, [[0, 1, 0], [0, 1, 1], [1, 0, 0]])
k7 = ee.Kernel.fixed(3, 3, [[0, 1, 0], [1, 1, 0], [0, 0, 1]])
i = 0
jt1 = ee.Image()
jt2 = ee.Image()
while (i<4): # rotate kernels
jt1 = jt1.addBands(CL1px.reduceNeighborhood(ee.Reducer.sum(), k6).rename(str(i)))
k6 = k6.rotate(1)
jt2 = jt2.addBands(CL1px.reduceNeighborhood(ee.Reducer.sum(), k7).rename(str(i)))
k7 = k7.rotate(1)
i = i + 1
jt1 = jt1.select(jt1.bandNames().getInfo()[1:])
jt2 = jt2.select(jt2.bandNames().getInfo()[1:])
jt11 = jt1.reduce('max').eq(4)
jt21 = jt2.reduce('max').eq(4)
jts1 = jt11.Or(jt21)
jts2 = CL1px.reduceNeighborhood(ee.Reducer.sum(), k4).gte(4).Or(CL1px.reduceNeighborhood(ee.Reducer.sum(), k5).gte(4)).selfMask()
jts = jts2.unmask().Or(jts1).selfMask()
return jts
def CleanCenterline(cl1px, maxBranchLengthToRemove, scale, iterate=5):
cl1px1 = cl1px
for i in range(iterate):
endsByNeighbors = ExtractEndpoints(cl1px1)
joints = ExtractJoints(cl1px1)
sep = cl1px1.add(joints.unmask().focal_max(1.5, "square")).eq(1).selfMask()
costMap = (sep.cumulativeCost(
source = endsByNeighbors,
maxDistance = maxBranchLengthToRemove,
geodeticDistance = True))
branchMask = costMap.gte(0).unmask(0)
cl1px1 = cl1px1.updateMask(branchMask.Not())
return cl1px1
def CalculateCenterline(imgIn, thre=0.7):
scale = imgIn.projection().nominalScale().getInfo()
crs = imgIn.projection().crs().getInfo()
distM = CalcDistanceMap(imgIn, 60, scale).reproject(crs = crs, scale = scale)
gradM = CalcGradientMap(distM, 2, scale).reproject(crs = crs, scale = scale)
cl1 = CalcOnePixelWidthCenterline(imgIn, gradM, thre).reproject(crs = crs, scale = scale)
return cl1
def CalculateAngle(clCleaned):
"""calculate the orthogonal direction of each pixel of the centerline
"""
w3 = (ee.Kernel.fixed(9, 9, [
[135.0, 126.9, 116.6, 104.0, 90.0, 76.0, 63.4, 53.1, 45.0],
[143.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 36.9],
[153.4, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26.6],
[166.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 14.0],
[180.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1e-5],
[194.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 346.0],
[206.6, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 333.4],
[216.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 323.1],
[225.0, 233.1, 243.4, 256.0, 270.0, 284.0, 296.6, 306.9, 315.0]]))
combinedReducer = ee.Reducer.sum().combine(ee.Reducer.count(), None, True)
clAngle = (clCleaned.mask(clCleaned)
.rename(['clCleaned'])
.reduceNeighborhood(
reducer = combinedReducer,
kernel = w3,
inputWeight = 'kernel',
skipMasked = True))
## mask calculating when there are more than two inputs into the angle calculation
clAngleNorm = (clAngle
.select('clCleaned_sum')
.divide(clAngle.select('clCleaned_count'))
.mask(clAngle.select('clCleaned_count').gt(2).Not()))
## if only one input into the angle calculation, rotate it by 90 degrees to get the orthogonal
clAngleNorm = (clAngleNorm
.where(clAngle.select('clCleaned_count').eq(1), clAngleNorm.add(ee.Image(90))))
return clAngleNorm.rename(['orthDegree'])
def GetWidth(clAngleNorm, segmentInfo, endInfo, DM, crs, bound, scale, sceneID, note):
"""calculate the width of the river at each centerline pixel, measured according to the orthgonal direction of the river
"""
def GetXsectionEnds(f):
xc = ee.Number(f.get('x'))
yc = ee.Number(f.get('y'))
orthRad = ee.Number(f.get('angle')).divide(180).multiply(math.pi)
width = ee.Number(f.get('toBankDistance')).multiply(1.5)
cosRad = width.multiply(orthRad.cos())
sinRad = width.multiply(orthRad.sin())
p1 = ee.Geometry.Point([xc.add(cosRad), yc.add(sinRad)], crs)
p2 = ee.Geometry.Point([xc.subtract(cosRad), yc.subtract(sinRad)], crs)
xlEnds = (ee.Feature(ee.Geometry.MultiPoint([p1, p2]).buffer(30), {
'xc': xc,
'yc': yc,
'longitude': f.get('lon'),
'latitude': f.get('lat'),
'orthogonalDirection': orthRad,
'MLength': width.multiply(2),
'p1': p1,
'p2': p2,
'crs': crs,
'image_id': sceneID,
'note': note
}))
return xlEnds
def SwitchGeometry(f):
return (f
.setGeometry(ee.Geometry.LineString(coords = [f.get('p1'), f.get('p2')], proj = crs, geodesic = False))
.set('p1', None).set('p2', None)) # remove p1 and p2
## convert centerline image to a list. prepare for map function
clPoints = (clAngleNorm.rename(['angle'])
.addBands(ee.Image.pixelCoordinates(crs))
.addBands(ee.Image.pixelLonLat().rename(['lon', 'lat']))
.addBands(DM.rename(['toBankDistance']))
.sample(
region = bound,
scale = scale,
projection = None,
factor = 1,
dropNulls = True
))
## calculate the cross-section lines, returning a featureCollection
xsectionsEnds = clPoints.map(GetXsectionEnds)
## calculate the flags at the xsection line end points
endStat = (endInfo.reduceRegions(
collection = xsectionsEnds,
reducer = ee.Reducer.anyNonZero().combine(ee.Reducer.count(), None, True), # test endpoints type
scale = scale,
crs = crs))
## calculate the width of the river and other flags along the xsection lines
xsections1 = endStat.map(SwitchGeometry)
combinedReducer = ee.Reducer.mean()
xsections = (segmentInfo.reduceRegions(
collection = xsections1,
reducer = combinedReducer,
scale = scale,
crs = crs))
return xsections
def CalculateOrthAngle(imgIn):
cl1px = imgIn.select(['cleanedCL'])
angle = CalculateAngle(cl1px)
imgOut = imgIn.addBands(angle)
return imgOut
def prepExport(f):
f = (f.set({
'width': ee.Number(f.get('MLength')).multiply(f.get('channelMask')),
'endsInWater': ee.Number(f.get('any')).eq(1),
'endsOverEdge': ee.Number(f.get('count')).lt(2)}))
fOut = (ee.Feature(ee.Geometry.Point([f.get('longitude'), f.get('latitude')]), {})
.copyProperties(f, None, ['any', 'count', 'MLength', 'xc', 'yc', 'channelMask']))
return fOut
def CalculateWidth(imgIn):
crs = imgIn.get('crs')
scale = imgIn.get('scale')
imgId = imgIn.get('image_id')
bound = imgIn.select(['riverMask']).geometry()
angle = imgIn.select(['orthDegree'])
infoEnds = imgIn.select(['riverMask'])
infoExport = (imgIn.select('channelMask')
.addBands(imgIn.select('^flag.*'))
.addBands(dem.rename(['flag_elevation'])))
dm = imgIn.select(['distanceMap'])
widths = GetWidth(angle, infoExport, infoEnds, dm, crs, bound, scale, imgId, '').map(prepExport)
return widths
def Zhang_Suen(image, crs, scale):
weights = [[1,1,1], [1,1,1], [1,1,1]]
kernel = ee.Kernel.fixed(weights = weights)
weights1 = [[0,1,0], [0,0,1], [0,1,0]]
kernel1 = ee.Kernel.fixed(weights = weights1)
weights2 = [[0,0,0], [1,0,1], [0,1,0]]
kernel2 = ee.Kernel.fixed(weights = weights2)
weights3 = [[1,0,0], [0,0,0], [0,0,0]]
kernel3 = ee.Kernel.fixed(weights = weights3)
weights4 = [[0,1,0], [0,0,0], [0,0,0]]
kernel4 = ee.Kernel.fixed(weights = weights4)
weights5 = [[0,0,1], [0,0,0], [0,0,0]]
kernel5 = ee.Kernel.fixed(weights = weights5)
weights6 = [[0,0,0], [0,0,1], [0,0,0]]
kernel6 = ee.Kernel.fixed(weights = weights6)
weights7 = [[0,0,0], [0,0,0], [0,0,1]]
kernel7 = ee.Kernel.fixed(weights = weights7)
weights8 = [[0,0,0], [0,0,0], [0,1,0]]
kernel8 = ee.Kernel.fixed(weights = weights8)
weights9 = [[0,0,0], [0,0,0], [1,0,0]]
kernel9 = ee.Kernel.fixed(weights = weights9)
weights10 = [[0,0,0], [1,0,0], [0,0,0]]
kernel10 = ee.Kernel.fixed(weights = weights10)
weights11 = [[0,1,0], [1,0,1], [0,0,0]]
kernel11 = ee.Kernel.fixed(weights = weights11)
weights12 = [[0,1,0], [1,0,0], [0,1,0]]
kernel12 = ee.Kernel.fixed(weights = weights11)
clLS_01p = ee.Image()
clLS_01p = clLS_01p.addBands(image.reduceNeighborhood(ee.Reducer.count(), kernel3).reproject(crs = crs, scale = scale).rename('1'))
clLS_01p = clLS_01p.addBands(image.reduceNeighborhood(ee.Reducer.count(), kernel4).reproject(crs = crs, scale = scale).rename('2'))
clLS_01p = clLS_01p.addBands(image.reduceNeighborhood(ee.Reducer.count(), kernel5).reproject(crs = crs, scale = scale).rename('3'))
clLS_01p = clLS_01p.addBands(image.reduceNeighborhood(ee.Reducer.count(), kernel6).reproject(crs = crs, scale = scale).rename('4'))
clLS_01p = clLS_01p.addBands(image.reduceNeighborhood(ee.Reducer.count(), kernel7).reproject(crs = crs, scale = scale).rename('5'))
clLS_01p = clLS_01p.addBands(image.reduceNeighborhood(ee.Reducer.count(), kernel8).reproject(crs = crs, scale = scale).rename('6'))
clLS_01p = clLS_01p.addBands(image.reduceNeighborhood(ee.Reducer.count(), kernel9).reproject(crs = crs, scale = scale).rename('7'))
clLS_01p = clLS_01p.addBands(image.reduceNeighborhood(ee.Reducer.count(), kernel10).reproject(crs = crs, scale = scale).rename('8'))
clLS_01p = clLS_01p.addBands(image.reduceNeighborhood(ee.Reducer.count(), kernel3).reproject(crs = crs, scale = scale).rename('9'))
clLS_01p = clLS_01p.select(['1', '2', '3', '4', '5', '6', '7', '8', '9'])
clLS_01p = clLS_01p.toArray()
mat = ee.Array([
[10, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 10, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 10, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 10, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 10, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 10, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 10, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 10, 1]
])
A = ee.Image(mat).matrixMultiply(clLS_01p.toArray(1)).arrayProject([0]).eq(ee.Array([1,1,1,1,1,1,1,1]))
A1 = A.arrayDotProduct(ee.Image(ee.Array([1,1,1,1,1,1,1,1])))
clLS_count = image.reduceNeighborhood(ee.Reducer.count(), kernel)
clLS_count1 = image.reduceNeighborhood(ee.Reducer.count(), kernel1)
clLS_count2 = image.reduceNeighborhood(ee.Reducer.count(), kernel2)
clLS = clLS_count.gt(2).And(clLS_count.lt(6)).And(clLS_count1.lt(3)).And(clLS_count2.lt(3)).And(A1.eq(1)).selfMask()
clLS_count11 = image.reduceNeighborhood(ee.Reducer.count(), kernel11)
clLS_count21 = image.reduceNeighborhood(ee.Reducer.count(), kernel12)
clLS1 = clLS_count.gt(2).And(clLS_count.lt(6)).And(clLS_count11.lt(3)).And(clLS_count21.lt(3)).And(A1.eq(1)).selfMask()
clLS2 = clLS.Or(clLS1)
return(clLS1)
def cl(image, n):
crs = image.projection().crs().getInfo()
scale = image.projection().nominalScale().getInfo()
image1 = image
for i in range(n):
remove = Zhang_Suen(image1, crs, scale)
image1 = image1.subtract(remove.unmask()).eq(1).selfMask()
return image1.reproject(crs = crs, scale = scale)
def batch_load_LS(collection, bandnames):
l = list()
bands_in_LS1 = ['SR_B3', 'SR_B2', 'SR_B1', 'SR_B4', 'SR_B5']
bands_in_LS2 = ['SR_B4', 'SR_B3', 'SR_B2', 'SR_B5', 'SR_B6']
for i in collection:
n = int(i.split('/')[1][3])
if n < 8:
l.append(ee.Image(i).select(bands_in_LS1).rename(bandnames))
else:
l.append(ee.Image(i).select(bands_in_LS2).rename(bandnames))
img_c = ee.ImageCollection(l)
return img_c
def ref_cr(img):
return img.multiply(0.0000275).add(-0.2)
def wrap_classification(collection, thr2=0, thr3=0.2, thr4=0.3, thr5=0.6, numPts=100):
thr2=0
thr3=0.2
thr4=0.3
thr5=0.6
numPts=100
def classification(image):
crs = image.projection().crs()
ndvi = image.normalizedDifference(['NIR', 'Red'])
scale = image.projection().nominalScale()
#minDis
#Three land type simple regions: water, plant, sand (other)
bands = image.bandNames()
labels = 'samples'
water = ndvi.lt(thr2)
sand = ndvi.lt(thr4).And(ndvi.gt(thr3)).multiply(2)
vege = ndvi.gt(thr5).multiply(3)
wsv = water.add(sand.add(vege)).selfMask().rename('samples')
samples = wsv.stratifiedSample(**{'numPoints': numPts, 'projection': crs, 'scale':30, 'geometries': True})
training = image.select(bands).sampleRegions(**{
'collection': samples,
'properties': ['samples'],
'scale' : scale
})
trained = ee.Classifier.minimumDistance().train(training, 'samples', bands)
classified = image.select(bands).classify(trained)
return classified
return collection.map(classification)