def bootstrapOtsu(collection,target_date, reductionPolygons, neg_buffer=-1500, # negative buffer for masking potential bad data upper_threshold=-14, # upper limit for water threshold canny_threshold=7, # threshold for canny edge detection canny_sigma=1, # sigma value for gaussian filter canny_lt=7, # lower threshold for canny detection smoothing=100, # amount of smoothing in meters connected_pixels=200, # maximum size of the neighborhood in pixels edge_length=50, # minimum length of edges from canny detection smooth_edges=100, qualityBand=None, reverse=False, reductionScale=90): tDate = ee.Date(target_date) targetColl = collection.filterDate(tDate,tDate.advance(1,'day')) nImgs = targetColl.size().getInfo() if nImgs <= 0: raise EEException('Selected date has no imagery, please try processing another date') collGeom = targetColl.geometry() polygons = reductionPolygons.filterBounds(collGeom) nPolys = polygons.size().getInfo() if nPolys > 0: ids = ee.List(polygons.aggregate_array('id')) random_ids = [] for i in range(3): random_ids.append(random.randint(0, ids.size().subtract(1).getInfo())) random_ids = ee.List(random_ids) def getRandomIds(i): return ids.get(i) ids = random_ids.map(getRandomIds) polygons = polygons.filter(ee.Filter.inList('id', ids)) if qualityBand == None: target = targetColl.mosaic().set('system:footprint', collGeom.dissolve()) target = target.clip(target.geometry().buffer(neg_buffer)) smoothed = target.focal_median(smoothing, 'circle', 'meters') histBand = ee.String(target.bandNames().get(0)) else: target = targetColl.qualityMosaic(qualityBand).set('system:footprint', collGeom.dissolve()) target = target.clip(target.geometry().buffer(neg_buffer)) smoothed = target.focal_median(smoothing, 'circle', 'meters') histBand = ee.String(qualityBand) canny = ee.Algorithms.CannyEdgeDetector(smoothed,canny_threshold,canny_sigma) connected = canny.mask(canny).lt(canny_lt).connectedPixelCount(connected_pixels, True) edges = connected.gte(edge_length) edgeBuffer = edges.focal_max(smooth_edges, 'square', 'meters') histogram_image = smoothed.mask(edgeBuffer) histogram = histogram_image.reduceRegion(ee.Reducer.histogram(255, 2),polygons.geometry(),reductionScale,bestEffort=True) threshold = ee.Number(otsu_function(histogram.get(histBand))).min(upper_threshold) else: threshold = upper_threshold water = smoothed.lt(threshold).clip(geeutils.LAND.geometry()) return water
def overBands(band, i): band = ee.String(band) i = ee.Image(i) array = wrap(image.select([band])) return i.addBands(array)
def add(name, i): name = ee.String(name) i = ee.Image(i) return i.addBands(band.rename(name))
def iteration(band, ini): ini = ee.Image(ini) imgband = ini.select([band]) precision = ee.String(precisions.get(band)) newband = convertPrecision(imgband, precision) return geetools.tools.image.replace(ini, band, newband)
def ren(banda): p = ee.Algorithms.If(pref, pref.cat(sep), "") s = ee.Algorithms.If(suf, sep.cat(suf), "") return ee.String(p).cat(ee.String(banda)).cat(ee.String(s))
def maskCover(image, geometry=None, scale=None, property_name='MASK_COVER', crs=None, crsTransform=None, bestEffort=False, maxPixels=1e13, tileScale=1): """ Percentage of masked pixels (masked/total * 100) as an Image property :param image: ee.Image holding the mask. If the image has more than one band, the first one will be used :type image: ee.Image :param geometry: the value will be computed inside this geometry. If None, will use image boundaries. If unbounded the result will be 0 :type geometry: ee.Geometry or ee.Feature :param scale: the scale of the mask :type scale: int :param property_name: the name of the resulting property :type property_name: str :return: The same parsed image with a new property holding the mask cover percentage :rtype: ee.Image """ # keep only first band imageband = image.select(0) # get projection projection = imageband.projection() if not scale: scale = projection.nominalScale() # get band name band = ee.String(imageband.bandNames().get(0)) # Make an image with all ones ones_i = ee.Image.constant(1).reproject(projection).rename(band) if not geometry: geometry = image.geometry() # manage geometry types if isinstance(geometry, (ee.Feature, ee.FeatureCollection)): geometry = geometry.geometry() unbounded = geometry.isUnbounded() # Get total number of pixels ones = ones_i.reduceRegion( reducer=ee.Reducer.count(), geometry=geometry, scale=scale, maxPixels=maxPixels, crs=crs, crsTransform=crsTransform, bestEffort=bestEffort, tileScale=tileScale, ).get(band) ones = ee.Number(ones) # select first band, unmask and get the inverse mask = imageband.mask() mask_not = mask.Not() image_to_compute = mask.updateMask(mask_not) # Get number of zeros in the given image zeros_in_mask = image_to_compute.reduceRegion( reducer=ee.Reducer.count(), geometry=geometry, scale=scale, maxPixels=maxPixels, crs=crs, crsTransform=crsTransform, bestEffort=bestEffort, tileScale=tileScale).get(band) zeros_in_mask = ee.Number(zeros_in_mask) percentage = zeros_in_mask.divide(ones) # Multiply by 100 cover = percentage.multiply(100) # Return None if geometry is unbounded final = ee.Number(ee.Algorithms.If(unbounded, 0, cover)) return image.set(property_name, final)
def on_export_button_clicked(b): global w_exportname try: MAD = ee.Image(result.get('MAD')).rename(madnames) # threshold nbands = MAD.bandNames().length() chi2 = ee.Image(result.get('chi2')).rename(['chi2']) pval = chi2cdf(chi2, nbands).subtract(1).multiply(-1) tst = pval.gt(ee.Image.constant(0.0001)) MAD = MAD.where(tst, ee.Image.constant(0)) allrhos = ee.Array(result.get('allrhos')).toList().slice(1, -1) # radcal ncmask = chi2cdf(chi2, nbands).lt(ee.Image.constant(0.05)).rename( ['invarpix']) inputlist1 = ee.List.sequence(0, nbands.subtract(1)) first = ee.Dictionary({ 'image': image1.addBands(image2), 'ncmask': ncmask, 'nbands': nbands, 'rect': poly, 'coeffs': ee.List([]), 'normalized': ee.Image() }) result1 = ee.Dictionary(inputlist1.iterate(radcal, first)) coeffs = ee.List(result1.get('coeffs')) sel = ee.List.sequence(1, nbands) normalized = ee.Image(result1.get('normalized')).select(sel) MADs = ee.Image.cat(MAD, chi2, ncmask, image1.clip(poly), image2.clip(poly), normalized) assexport = ee.batch.Export.image.toAsset( MADs, description='assetExportTask', assetId=w_exportname.value, scale=scale, maxPixels=1e9) assexportid = str(assexport.id) w_text.value = 'Exporting change map to %s\n task id: %s' % ( w_exportname.value, assexportid) assexport.start() except Exception as e: w_text.value = 'Error: %s' % e # export metadata to drive ninvar = ee.String( ncmask.reduceRegion(ee.Reducer.sum().unweighted(), scale=scale, maxPixels=1e9).toArray().project([0])) metadata = ee.List(['IR-MAD: '+time.asctime(), 'Platform: '+w_platform.value, 'Asset export name: '+w_exportname.value, 'Timestamps: %s %s'%(timestamp1,timestamp2)]) \ .cat(['Canonical Correlations:']) \ .cat(allrhos) \ .cat(['Radiometric Normalization, Invariant Pixels:']) \ .cat([ninvar]) \ .cat(['Slope, Intercept, R:']) \ .cat(coeffs) fileNamePrefix = w_exportname.value.replace('/', '-') gdexport = ee.batch.Export.table.toDrive( ee.FeatureCollection(metadata.map(makefeature)).merge( ee.Feature(poly)), description='driveExportTask_meta', folder='EarthEngineImages', fileNamePrefix=fileNamePrefix) w_text.value += '\n Exporting metadata to Drive/EarthEngineImages/%s\n task id: %s' % ( fileNamePrefix, str(gdexport.id)) gdexport.start()
def getNewBandNames(prefix): seq = ee.List.sequence(1, bandNames.length()) return seq.map(lambda b: ee.String(prefix).cat(ee.Number(b).int().format()))
def renameBands(band): return ee.String(band).cat('_fitted')
def calc_image_pca(image, region=None, scale=90, max_pixels=1e9): """Principal component analysis decomposition of image bands args: image (ee.Image): image to apply pca to region (ee.Geometry | None, optional): region to sample values for covariance matrix, if set to `None` will use img.geometry(). default = None scale (int, optional): scale at which to perform reduction operations, setting higher will prevent OOM errors. default = 90 max_pixels (int, optional): maximum number of pixels to use in reduction operations. default = 1e9 returns: ee.Image: principal components scaled by eigen values """ bandNames = image.bandNames() out_band_names = ee.List.sequence(1, bandNames.length()).map( lambda x: ee.String("pc_").cat(ee.Number(x).int()) ) # Mean center the data to enable a faster covariance reducer # and an SD stretch of the principal components. meanDict = image.reduceRegion( reducer=ee.Reducer.mean(), geometry=region, scale=scale, maxPixels=max_pixels ) means = ee.Image.constant(meanDict.values(bandNames)) centered = image.subtract(means) # Collapse the bands of the image into a 1D array per pixel. arrays = centered.toArray() # Compute the covariance of the bands within the region. covar = arrays.reduceRegion( reducer=ee.Reducer.centeredCovariance(), geometry=region, scale=scale, maxPixels=max_pixels, ) # Get the 'array' covariance result and cast to an array. # This represents the band-to-band covariance within the region. covarArray = ee.Array(covar.get("array")) # Perform an eigen analysis and slice apart the values and vectors. eigens = covarArray.eigen() # This is a P-length vector of Eigenvalues. eigenValues = eigens.slice(1, 0, 1) # This is a PxP matrix with eigenvectors in rows. eigenVectors = eigens.slice(1, 1) # Convert the array image to 2D arrays for matrix computations. arrayImage = arrays.toArray(1) # Left multiply the image array by the matrix of eigenvectors. principalComponents = ee.Image(eigenVectors).matrixMultiply(arrayImage) # Turn the square roots of the Eigenvalues into a P-band image. sdImage = ( ee.Image(eigenValues.sqrt()).arrayProject([0]).arrayFlatten([out_band_names]) ) # Turn the PCs into a P-band image, normalized by SD. return ( principalComponents # Throw out an an unneeded dimension, [[]] -> []. .arrayProject([0]) # Make the one band array image a multi-band image, [] -> image. .arrayFlatten([out_band_names]) # Normalize the PCs by their SDs. .divide(sdImage) )
def less100(doy): doy = doy.toInt() return ee.String('0').cat(ee.Number(doy).format())
def satInt(img): strings = ee.String(img.get("SATELLITE")).split("_") return img.set({"sat": ee.String(ee.List(strings).get(1))})
def wrap(band, img): img = ee.Image(img) band = ee.String(band) m = mask.select(band) return img.updateMask(m)
def bmaxOtsu(collection,target_date,region, smoothing=100, qualityBand=None, reductionScale=90, initThresh=0, reverse=False, gridSize=0.1, bmaxThresh=0.75, maxBoxes=100, seed=7): def constuctGrid(i): def contructXGrid(j): j = ee.Number(j) box = ee.Feature(ee.Geometry.Rectangle(j,i,j.add(gridSize),i.add(gridSize))) out = ee.Algorithms.If(geom.contains(box.geometry()),box,None) return ee.Feature(out) i = ee.Number(i) out = ee.List.sequence(west,east.subtract(gridSize),gridSize).map(contructXGrid) return out def calcBmax(feature): segment = target.clip(feature) initial = segment.lt(initThresh) p1 = ee.Number(initial.reduceRegion( reducer= ee.Reducer.mean(), geometry= feature.geometry(), bestEffort= True, scale= reductionScale, ).get(histBand)) p1 = ee.Number(ee.Algorithms.If(p1,p1,0.99)) p2 = ee.Number(1).subtract(p1) m = segment.updateMask(initial).rename('m1').addBands( segment.updateMask(initial.Not()).rename('m2') ) mReduced = m.reduceRegion( reducer= ee.Reducer.mean(), geometry= feature.geometry(), bestEffort= True, scale= reductionScale, ) m1 = ee.Number(mReduced.get('m1')) m2 = ee.Number(mReduced.get('m2')) m1 = ee.Number(ee.Algorithms.If(m1,m1,globalLow)) m2 = ee.Number(ee.Algorithms.If(m2,m2,globalHigh)) sigmab = p1.multiply(p2.multiply(m1.subtract(m2).pow(2))) sigmat = ee.Number(segment.reduceRegion( reducer= ee.Reducer.variance(), geometry= feature.geometry(), bestEffort= True, scale= reductionScale, ).get(histBand)) sigmat = ee.Number(ee.Algorithms.If(sigmat,sigmat,2)) bmax = sigmab.divide(sigmat) return feature.set({'bmax':bmax}) tDate = ee.Date(target_date) targetColl = collection.filterDate(tDate,tDate.advance(1,'day')) if qualityBand == None: histBand = ee.String(target.bandNames().get(0)) target = targetColl.mosaic()\ .select(histBand) else: histBand = ee.String(qualityBand) target = targetColl.qualityMosaic(qualityBand)\ .select(histBand) geom = targetColl.map(geeutils.getGeoms).union().geometry()\ .intersection(region,1) theoretical = target.reduceRegion( reducer= ee.Reducer.percentile([10,90]), geometry= geom, bestEffort= True, scale= 5000 ) globalLow = theoretical.get(histBand.cat('_p10')) globalHigh = theoretical.get(histBand.cat('_p90')) bounds = geom.bounds() coords = ee.List(bounds.coordinates().get(0)) gridSize = ee.Number(gridSize) west = ee.Number(ee.List(coords.get(0)).get(0)) south = ee.Number(ee.List(coords.get(0)).get(1)) east = ee.Number(ee.List(coords.get(2)).get(0)) north = ee.Number(ee.List(coords.get(2)).get(1)) west = west.subtract(west.mod(gridSize)) south = south.subtract(south.mod(gridSize)) east = east.add(gridSize.subtract(east.mod(gridSize))) north = north.add(gridSize.subtract(north.mod(gridSize))) grid = ee.FeatureCollection( ee.List.sequence(south,north.subtract(gridSize),gridSize).map(constuctGrid).flatten() ) bmaxes = grid.map(calcBmax).filter(ee.Filter.gt('bmax',bmaxThresh)).randomColumn('random',seed) nBoxes = ee.Number(bmaxes.size()) randomThresh = ee.Number(maxBoxes).divide(nBoxes) selection = bmaxes.filter(ee.Filter.lt('random',randomThresh)) histogram = target.reduceRegion(ee.Reducer.histogram(255, 2)\ .combine('mean', None, True)\ .combine('variance', None,True),selection,reductionScale,bestEffort=True, tileScale=16) threshold = geeutils.otsu_function(histogram.get(histBand.cat('_histogram'))) water = target.gt(threshold).clip(geeutils.LAND.geometry()) return water.rename('water')
def addSuffix(fc, suffix): namesOld = ee.Feature(fc.first()).toDictionary().keys() namesNew = namesOld.map(lambda x: ee.String(x).cat(ee.String(suffix))) return fc.select(namesOld, namesNew)
def _rename_band(val, suffix): return ee.String(val).cat(ee.String("_")).cat(ee.String(suffix))
def compute(image, **kwargs): """ Core function for Mask Percent Score. Has no dependencies in geebap module :param image: ee.Image holding the mask :type image: ee.Image :param geometry: the score will be computed inside this geometry :type geometry: ee.Geometry or ee.Feature :param scale: the scale of the mask :type scale: int :param band_name: the name of the resulting band :type band_name: str :return: An image with one band that holds the percentage of pixels with value 0 (not 1) over the total pixels inside the geometry, and a property with the same name as the assigned for the band with the same percentage :rtype: ee.Image """ geometry = kwargs.get('geometry') scale = kwargs.get('scale', 1000) band_name = kwargs.get('band_name', 'score-maskper') max_pixels = kwargs.get('max_pixels', 1e13) count_zeros = kwargs.get('count_zeros', False) # get band name band = ee.String(image.bandNames().get(0)) # get projection projection = image.select([band]).projection() # Make an image with all ones ones_i = ee.Image.constant(1).reproject(projection).rename(band) # manage geometry types if isinstance(geometry, (ee.Feature, ee.FeatureCollection)): geometry = geometry.geometry() # Get total number of pixels ones = ones_i.reduceRegion(reducer=ee.Reducer.count(), geometry=geometry, scale=scale, maxPixels=max_pixels).get(band) ones = ee.Number(ones) # select first band, unmask and get the inverse mask_image = image.select([band]) if count_zeros: zeros = mask_image.eq(0) mask_image = mask_image.updateMask(zeros.Not()) mask = mask_image.mask() mask_not = mask.Not() image_to_compute = mask.updateMask(mask_not) # Get number of zeros in the given mask_image zeros_in_mask = image_to_compute.reduceRegion( reducer=ee.Reducer.count(), geometry=geometry, scale=scale, maxPixels=max_pixels).get(band) zeros_in_mask = ee.Number(zeros_in_mask) percentage = tools.number.trimDecimals(zeros_in_mask.divide(ones), 4) # Make score inverse to percentage score = ee.Number(1).subtract(percentage) percent_image = ee.Image.constant(score) \ .select([0], [band_name])\ .set(band_name, score).toFloat() return percent_image.clip(geometry)
def class1(ltr, umb_b=0.01, umb_m=0.05): """ Método para aplicar una clasificación al resultado de LandTendr :Parametros: :param umb_b: umbral para determinar el atributo "bajo", el cual va entre 0 y este umbral :type umb_b: float :param umb_m: umbral para determinar los atributos "moderado" y "alto", los cuales van entre umb_bajo y umb_mod, y mayor a umb_mod respectivamente :type umb_m: float :return: Una coleccion de imagenes, donde cada imagen tiene una banda de nombre "cat", que contiene la categoría, a saber: * cat 1: sin grandes cambios (azul) * cat 2: perdida suave (naranja) * cat 3: perdida alta (rojo) * cat 4: ganancia (o recuperacion) suave (amarillo) * cat 5: ganancia alta (verde) * y tres bandas para la visualizacion :rtype: ee.ImageCollection """ col = ltr.slope def categoria(img): d = img.get("system:time_start") adelante = ee.String("slope_after") atras = ee.String("slope_before") umb_bajo = ee.Image.constant(umb_b) umb_mod = ee.Image.constant(umb_m) at = ee.Image(img).select(atras) atAbs = at.abs() ad = ee.Image(img).select(adelante) adAbs = ad.abs() # INTENSIDAD QUIEBRE dif = ad.subtract(at) media = atAbs.add(adAbs).divide(2) # DIRECCION at_dec = at.lte(0) # atras decreciente? at_crec = at.gt(0) # atras creciente? ad_dec = ad.lte(0) # adelante decreciente? ad_crec = ad.gt(0) # adelante creciente? # OTRA OPCION DE CATEGORIAS # INTENSIDAD difAbs = dif.abs() menor_bajo = difAbs.lt(umb_bajo) menor_mod = difAbs.lt(umb_mod) mayor_bajo = difAbs.gte(umb_bajo) mayor_mod = difAbs.gte(umb_mod) int_baja = menor_bajo int_mod = mayor_bajo.And(menor_mod) int_alta = mayor_mod # int_baja = dif.abs().lt(umb_bajo) # int_mod = dif.abs().lt(umb_mod).And(dif.gte(umb_bajo)) # int_alta = dif.abs().gte(umb_mod) cat1 = int_baja # sin grandes cambios cat2 = int_mod.And(dif.lte(0)) # perdida suave cat3 = int_alta.And(dif.lte(0)) # perdida alta cat4 = int_mod.And(dif.gt(0)) # ganancia suave cat5 = int_alta.And(dif.gt(0)) # ganancia alta # ESCALO cat2 = cat2.multiply(2) cat3 = cat3.multiply(3) cat4 = cat4.multiply(4) cat5 = cat5.multiply(5) final = cat1.add(cat2).add(cat3).add(cat4).add(cat5) img = img.addBands(ee.Image(final).select([0], ["cat"])).addBands( ee.Image(at_dec).select([0], ["at_dec"])).addBands( ee.Image(at_crec).select([0], ["at_crec"])).addBands( ee.Image(ad_dec).select([0], ["ad_dec"])).addBands( ee.Image(ad_crec).select([0], ["ad_crec"])).addBands( ee.Image(dif).select([0], ["dif"])) mask = img.neq(0) return img.updateMask(mask).set("system:time_start", d) col = col.map(categoria) # VISUALIZACION DE IMG r = ee.String("viz-red") g = ee.String("viz-green") b = ee.String("viz-blue") def viz(imagen): d = imagen.get("system:time_start") img = ee.Image(imagen) ''' R G B 1 50, 25, 255 azul 2 255, 100, 50 naranja 3 255, 25, 25 rojo 4 255, 255, 50 amarillo 5 50, 150, 50 verde ''' cat1 = img.eq(1) r1 = cat1.multiply(50) g1 = cat1.multiply(25) b1 = cat1.multiply(255) cat2 = img.eq(2) r2 = cat2.multiply(255) g2 = cat2.multiply(100) b2 = cat2.multiply(50) cat3 = img.eq(3) r3 = cat3.multiply(255) g3 = cat3.multiply(25) b3 = cat3.multiply(25) cat4 = img.eq(4) r4 = cat4.multiply(255) g4 = cat4.multiply(255) b4 = cat4.multiply(50) cat5 = img.eq(5) r5 = cat5.multiply(50) g5 = cat5.multiply(150) b5 = cat5.multiply(50) red = r1.add(r2).add(r3).add(r4).add(r5).select([0], [r]).toUint8() green = (g1.add(g2).add(g3).add(g4).add(g5).select([0], [g]).toUint8()) blue = (b1.add(b2).add(b3).add(b4).add(b5).select([0], [b]).toUint8()) return img.addBands(ee.Image([red, green, blue])).set("system:time_start", d) col = col.select("cat").map(viz) return col
def AddAllLags(imgCollection, num_lags=1, filtering_property=None): """ Given a imgCollection it returns a list with the same number of images of the imageCollection where each image contains its bands together with the bands of num_lags images where filtering_property does NOT hold It also add to the metadata of the image the lagged time stamp ("system:time_start_lag_X") :param imgCollection: each img must have system:time_start property :param num_lags: number of lagged elements of each image :param filtering_property: if None no filtering will be done :return: list with images :rtype ee.List """ lags_client = range(1, num_lags+1) bands_server = ee.Image(imgCollection.first()).bandNames() number_of_bands_server = ee.Number(bands_server.size()) total_number_bands = number_of_bands_server.multiply(num_lags + 1) # create zero image with number_of_bands_server x num_lags + 1 bands zeros = ee.List.repeat(0, total_number_bands) bands_for_select = ee.List.sequence(0, total_number_bands.subtract(1)) # sufix_lag_server = ["_lag_1",....,"_lag_n-1","_lag_n"] sufix_lag_server = ee.List(list(map(lambda lg: "_lag_"+str(lg), lags_client))) # sufix_lag_server_initial = ["","_lag_1",....,"_lag_n-1","_lag_n"] sufix_lag_server_initial = ee.List([ee.String("")]).cat(sufix_lag_server) # sufix_lag_server_shifted = ["","_lag_1",....,"_lag_n-1"] sufix_lag_server_shifted = sufix_lag_server_initial.slice(0, num_lags) all_bands = GenerateBandLags(bands_server, sufix_lag_server_initial) # Important constant otherwise doesn't work image_primera = ee.Image.constant(zeros).select(bands_for_select, all_bands) # Set time property name_time_server = sufix_lag_server_initial.map(lambda sufix: ee.String("system:time_start").cat(ee.String(sufix))) zeros_time_start_server = ee.List.repeat(0, num_lags+1) dictio = ee.Dictionary.fromLists(name_time_server, zeros_time_start_server) image_primera = image_primera.set(dictio) if filtering_property is not None: image_primera = image_primera.set(filtering_property, 1) # Create first element for iterate lista_ee = ee.List([image_primera]) def accumulate(image, lista_rec): lista_recibida = ee.List(lista_rec) previous = ee.Image(lista_recibida.get(-1)) bands_with_lags_server = GenerateBandLags(bands_server, sufix_lag_server) sufix_lag_server_shifted_iteration = sufix_lag_server_shifted # if cloud > X bands_with_lags_shifted_server = bands_with_lags_server if filtering_property is not None: sufix_lag_server_shifted_iteration = ee.Algorithms.If(ee.Number(previous.get(filtering_property)), sufix_lag_server, sufix_lag_server_shifted) sufix_lag_server_shifted_iteration = ee.List(sufix_lag_server_shifted_iteration) bands_with_lags_shifted_server = GenerateBandLags(bands_server, sufix_lag_server_shifted_iteration) previous_add = previous.select(bands_with_lags_shifted_server, bands_with_lags_server) image = image.addBands(previous_add) name_time_server_shifted = sufix_lag_server_shifted_iteration.map( lambda sufix: ee.String("system:time_start").cat(ee.String(sufix))) values_time_server_select = name_time_server_shifted.map(lambda field: previous.get(field)) name_time_server_select = sufix_lag_server.map( lambda sufix: ee.String("system:time_start").cat(ee.String(sufix))) dictio_set = ee.Dictionary.fromLists(name_time_server_select, values_time_server_select) image = image.set(dictio_set) return lista_recibida.add(image) lista_retorno = ee.List(imgCollection.iterate(accumulate, lista_ee)).slice(1) return lista_retorno
def categoria(img): d = img.get("system:time_start") adelante = ee.String("slope_after") atras = ee.String("slope_before") umb_bajo = ee.Image.constant(umb_b) umb_mod = ee.Image.constant(umb_m) at = ee.Image(img).select(atras) atAbs = at.abs() ad = ee.Image(img).select(adelante) adAbs = ad.abs() # INTENSIDAD QUIEBRE dif = ad.subtract(at) media = atAbs.add(adAbs).divide(2) # DIRECCION at_dec = at.lte(0) # atras decreciente? at_crec = at.gt(0) # atras creciente? ad_dec = ad.lte(0) # adelante decreciente? ad_crec = ad.gt(0) # adelante creciente? # OTRA OPCION DE CATEGORIAS # INTENSIDAD difAbs = dif.abs() menor_bajo = difAbs.lt(umb_bajo) menor_mod = difAbs.lt(umb_mod) mayor_bajo = difAbs.gte(umb_bajo) mayor_mod = difAbs.gte(umb_mod) int_baja = menor_bajo int_mod = mayor_bajo.And(menor_mod) int_alta = mayor_mod # int_baja = dif.abs().lt(umb_bajo) # int_mod = dif.abs().lt(umb_mod).And(dif.gte(umb_bajo)) # int_alta = dif.abs().gte(umb_mod) cat1 = int_baja # sin grandes cambios cat2 = int_mod.And(dif.lte(0)) # perdida suave cat3 = int_alta.And(dif.lte(0)) # perdida alta cat4 = int_mod.And(dif.gt(0)) # ganancia suave cat5 = int_alta.And(dif.gt(0)) # ganancia alta # ESCALO cat2 = cat2.multiply(2) cat3 = cat3.multiply(3) cat4 = cat4.multiply(4) cat5 = cat5.multiply(5) final = cat1.add(cat2).add(cat3).add(cat4).add(cat5) img = img.addBands(ee.Image(final).select([0], ["cat"])).addBands( ee.Image(at_dec).select([0], ["at_dec"])).addBands( ee.Image(at_crec).select([0], ["at_crec"])).addBands( ee.Image(ad_dec).select([0], ["ad_dec"])).addBands( ee.Image(ad_crec).select([0], ["ad_crec"])).addBands( ee.Image(dif).select([0], ["dif"])) mask = img.neq(0) return img.updateMask(mask).set("system:time_start", d)
def renameBand(val): ind = names.indexOf(val) y = ee.String("y") return y.cat(ee.String(names.get(ind)).slice(0, 4))
def get_name(image): """ Return the image id from 1 image Argument: :param image: """ return ee.String("COPERNICUS/S2/").cat(ee.Image(image).id())
def addSuffix(band): return ee.String(band).cat('_fitted')
def get(self): # get example map example_map = ee.Image( 'users/arjenhaag/SERVIR-Mekong/SWMT_example_months_2') # get content for each monthly image mapid_1 = SurfaceWaterToolStyle( example_map.select(ee.String('water_').cat( ee.String('1')))).getMapId() mapid_2 = SurfaceWaterToolStyle( example_map.select(ee.String('water_').cat( ee.String('2')))).getMapId() mapid_3 = SurfaceWaterToolStyle( example_map.select(ee.String('water_').cat( ee.String('3')))).getMapId() mapid_4 = SurfaceWaterToolStyle( example_map.select(ee.String('water_').cat( ee.String('4')))).getMapId() mapid_5 = SurfaceWaterToolStyle( example_map.select(ee.String('water_').cat( ee.String('5')))).getMapId() mapid_6 = SurfaceWaterToolStyle( example_map.select(ee.String('water_').cat( ee.String('6')))).getMapId() mapid_7 = SurfaceWaterToolStyle( example_map.select(ee.String('water_').cat( ee.String('7')))).getMapId() mapid_8 = SurfaceWaterToolStyle( example_map.select(ee.String('water_').cat( ee.String('8')))).getMapId() mapid_9 = SurfaceWaterToolStyle( example_map.select(ee.String('water_').cat( ee.String('9')))).getMapId() mapid_10 = SurfaceWaterToolStyle( example_map.select(ee.String('water_').cat( ee.String('10')))).getMapId() mapid_11 = SurfaceWaterToolStyle( example_map.select(ee.String('water_').cat( ee.String('11')))).getMapId() mapid_12 = SurfaceWaterToolStyle( example_map.select(ee.String('water_').cat( ee.String('12')))).getMapId() content_1 = {'eeMapId': mapid_1['mapid'], 'eeToken': mapid_1['token']} content_2 = {'eeMapId': mapid_2['mapid'], 'eeToken': mapid_2['token']} content_3 = {'eeMapId': mapid_3['mapid'], 'eeToken': mapid_3['token']} content_4 = {'eeMapId': mapid_4['mapid'], 'eeToken': mapid_4['token']} content_5 = {'eeMapId': mapid_5['mapid'], 'eeToken': mapid_5['token']} content_6 = {'eeMapId': mapid_6['mapid'], 'eeToken': mapid_6['token']} content_7 = {'eeMapId': mapid_7['mapid'], 'eeToken': mapid_7['token']} content_8 = {'eeMapId': mapid_8['mapid'], 'eeToken': mapid_8['token']} content_9 = {'eeMapId': mapid_9['mapid'], 'eeToken': mapid_9['token']} content_10 = { 'eeMapId': mapid_10['mapid'], 'eeToken': mapid_10['token'] } content_11 = { 'eeMapId': mapid_11['mapid'], 'eeToken': mapid_11['token'] } content_12 = { 'eeMapId': mapid_12['mapid'], 'eeToken': mapid_12['token'] } content = { '1': content_1, '2': content_2, '3': content_3, '4': content_4, '5': content_5, '6': content_6, '7': content_7, '8': content_8, '9': content_9, '10': content_10, '11': content_11, '12': content_12, } self.response.headers['Content-Type'] = 'application/json' self.response.out.write(json.dumps(content))
def generalMask(options, reader, qa_band, update_mask=True, add_mask_band=True, add_every_mask=False, all_masks_name='mask'): """ General function to get a bit mask band given a set of options a bit reader and the name of the qa_band :param options: options to decode :param reader: the bit reader :param qa_band: the name of the qa band :param updateMask: whether to update the mask for all options or not :param addBands: whether to add the mask band for all options or not :return: a function to map over a collection """ encoder = decodeBitsEE(reader, qa_band) opt = ee.List(options) clases = ("'{}', "*len(options))[:-2].format(*options) # Property when adding every band msg_eb = "Band called 'mask' is for {} and was computed by geetools" \ " version {} (https://github.com/gee-community/gee_tools)" prop_eb = ee.String(msg_eb.format(clases, __version__)) prop_name_eb = ee.String('{}_band'.format(all_masks_name)) def add_every_bandF(image, encoded): return image.addBands(encoded).set(prop_name_eb, prop_eb) def get_all_mask(encoded): # TODO: put this function in tools initial = encoded.select([ee.String(opt.get(0))]) rest = ee.List(opt.slice(1)) def func(cat, ini): ini = ee.Image(ini) new = encoded.select([cat]) return ee.Image(ini.Or(new)) all_masks = ee.Image(rest.iterate(func, initial)) \ .select([0], [all_masks_name]) mask = all_masks.Not() return mask # 0 0 1 if not add_every_mask and not update_mask and add_mask_band: def wrap(image): encoded = encoder(image).select(opt) mask = get_all_mask(encoded) return image.addBands(mask) # 0 1 0 elif not add_every_mask and update_mask and not add_mask_band: def wrap(image): encoded = encoder(image).select(opt) mask = get_all_mask(encoded) return image.updateMask(mask) # 0 1 1 elif not add_every_mask and update_mask and add_mask_band: def wrap(image): encoded = encoder(image).select(opt) mask = get_all_mask(encoded) return image.updateMask(mask).addBands(mask) # 1 0 0 elif add_every_mask and not update_mask and not add_mask_band: def wrap(image): encoded = encoder(image).select(opt) return add_every_bandF(image, encoded) # 1 0 1 elif add_every_mask and not update_mask and add_mask_band: def wrap(image): encoded = encoder(image).select(opt) mask = get_all_mask(encoded) return add_every_bandF(image, encoded).addBands(mask) # 1 1 0 elif add_every_mask and update_mask and not add_mask_band: def wrap(image): encoded = encoder(image).select(opt) mask = get_all_mask(encoded) updated = image.updateMask(mask) with_bands = add_every_bandF(updated, encoded) return with_bands # 1 1 1 elif add_every_mask and update_mask and add_mask_band: def wrap(image): encoded = encoder(image).select(opt) mask = get_all_mask(encoded) updated = image.updateMask(mask) with_bands = add_every_bandF(updated, encoded) return with_bands.addBands(mask) return wrap
def listval(img, it): id = ee.String(img.id()) values = img.reduceRegion(ee.Reducer.first(), point, scale) return ee.Dictionary(it).set(id, ee.Dictionary(values))
def wrap(band, img): img = ee.Image(img) band = ee.String(band) m = mask.select(band) toapply = m.Not() if negative else m return img.updateMask(toapply)
def set_color(f): c = ee.String(f.get('COLOR')).slice(1) return f \ .set('R', ee.Number.parse(c.slice(0, 2), 16)) \ .set('G', ee.Number.parse(c.slice(2, 4), 16)) \ .set('B', ee.Number.parse(c.slice(4, 6), 16))
import ee import geemap # Create a map centered at (lat, lon). Map = geemap.Map(center=[40, -100], zoom=4) # traditional python string print('Hello world!') # Earth Eninge object print(ee.String('Hello World from Earth Engine!').getInfo()) print(ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318').getInfo()) # Display the map. Map
def wrap(band, filt): band = ee.String(band) filt = ee.Filter(filt) newfilt = ee.Filter.listContains(leftField='_BANDS_', rightValue=band) return ee.Filter.Or(filt, newfilt)