def wrap(img): # Selecciono los pixeles con valor distinto de cero # ceros = img.select([0]).eq(0).Not() ceros = img.neq(0) # ORIGINAL IMAGE img_orig = img # SELECT BANDS img_proc = img.select(bandas) # CONDICION condicion_adentro = (img_proc.gte(mmin) .And(img_proc.lte(mmax))) pout = functions.simple_rename(condicion_adentro, suffix="pout") suma = tools.sumBands(nombre)(pout) final = suma.select(nombre).multiply(ee.Image(incremento)) parametrizada = tools.parametrize(rango_orig, rango_fin)(final) return img_orig.addBands(parametrizada)#.updateMask(ceros)
def test_sumBands(self): newimg = tools.sumBands("added_bands", ("B1", "B2", "B3"))(self.l8SR) vals = tools.get_value(newimg, self.p_l8SR_no_cloud, 30) suma = int(vals["B1"]) + int(vals["B2"]) + int(vals["B3"]) self.assertEqual(vals["added_bands"], suma)
def wrap(img): def compute_score(band, first): score_complete = ee.Image(first) # score_img = score_complete.select([band]) img_band = img.select([band]) min = ee.Dictionary(relation_ee.get(band)).get('min') # could be None max = ee.Dictionary(relation_ee.get(band)).get('max') # could be None score_min = img_band.gte(ee.Image.constant(min)) score_max = img_band.lte(ee.Image.constant(max)) final_score = score_min.And(score_max) # Image with one band return tools.replace(score_complete, band, final_score) scores = ee.Image(bands_ee.iterate(compute_score, score)) # parametrized = scores.multiply(self.step) final_score = tools.sumBands(name=self.name)(scores) \ .divide(ee.Image.constant(length)) return final_score
def collection(self, site, indices=None, normalize=True, bbox=0, force=True): """ Apply masks, filters and scores to the given collection group and return one image collecion with all images and their score bands. :param indices: vegetation indices to include in the final image. If None, no index is calculated :type indices: tuple :param site: Site geometry :type site: ee.Geometry :param normalize: Whether to normalize the final score from 0 to 1 or not :type normalize: bool :return: a namedtuple: - col: the collection with filters, masks and scores applied - dictprop: dict that will go to the metadata of the Image """ ################# DEBUG ######################################### # Get site centroid for debugging purpose geom = site if isinstance(site, ee.Geometry) else site.geometry() centroid = geom.centroid() # Function to get the value of the first image of the collection in # its centroid def get_col_val(col): ''' Values of the first image in the centroid ''' values = tools.imagecollection.get_values(col, centroid, scale=30, side='client') pp.pprint(values) ################################################################# # NamedTuple for the OUTPUT output = namedtuple("ColBap", ("col", "dictprop")) # Si no se pasa una funcion para aplicar antes de los puntajes, se # crea una que devuelva la misma imagen #If a function is not passed to apply before the scores, it # create one that returns the same image if self.fmap is None: fmap = lambda x: x else: fmap = self.fmap # colfinal = ee.ImageCollection() colfinal = ee.List([]) # Obtengo la region del site try: region = site.geometry().bounds().getInfo()['coordinates'][0] except AttributeError: region = site.getInfo()['coordinates'][0] except: raise AttributeError # lista de nombres de los puntajes para sumarlos al final #List of names of the scores to add them to the end scores = self.score_names maxpunt = functools.reduce(lambda i, punt: i + punt.max, self.scores, 0) if self.scores else 1 # Diccionario de cant de imagenes para incluir en las propiedades #Dictionary of images to include in the properties toMetadata = dict() # collist = self.collist() collist = self.colgroup.collections if self.verbose: print("scores:", scores) # print("satellites:", [c.ID for c in collist]) print("satellites:", self.colgroup.ids) for colobj in collist: # Obtengo el ID de la coleccion #I get the collection ID cid = colobj.ID # Obtengo el name abreviado para agregar a los metadatos #I get the abbreviated name to add to the metadata short = colobj.short # Imagen del col_id de la coleccion #Col_id image of the collection bid = colobj.bandIDimg # Add col_id to metadata. # col_id_11 = 'L8TAO' # etc.. toMetadata["col_id_" + str(colobj.col_id)] = short # Collection completa de EE c = colobj.colEE # Filtro por el site if isinstance(site, ee.Feature): site = site.geometry() c2 = c.filterBounds(site) # Renombra las bandas aca? # Rename the bands here? # c2 = c2.map(col.rename()) if self.verbose: print("\nSatellite:", colobj.ID) if self.debug: pp.pprint(colobj.kws) print("SIZE AFTER FILTER SITE:", c2.size().getInfo()) # Filtro por los años #Filter for years for anio in self.date_range: # Creo un nuevo objeto de coleccion con el id col = satcol.Collection.from_id(cid) # puntajes = [] ini = self.season.add_year(anio)[0] end = self.season.add_year(anio)[1] if self.verbose: print("ini:", ini, ",end:", end) # Filtro por fecha # Filter by date c = c2.filterDate(ini, end) if self.debug: n = c.size().getInfo() print("SIZE AFTER FILTER DATE:", n) ## FILTROS ESTABAN ACA # Si despues de los filters no quedan imgs, saltea.. #If there are no imgs left after the filters, skip ... size = c.size().getInfo() if self.verbose: print("size after filters:", size) if size == 0: continue # 1 if self.debug: print("INITIAL CENTROID VALUES", get_col_val(c)) # corto la imagen con la region para minimizar los calculos #I cut the image with the region to minimize the calculations if bbox == 0: def cut(img): return img.clip(site) else: def cut(img): bounds = site.buffer(bbox) return img.clip(bounds) c = c.map(cut) if self.debug: print("AFTER CLIPPING WITH REGION", get_col_val(c)) # MASKS if self.masks: for m in self.masks: if m != None: c = c.map(m.map(col=col, year=anio, colEE=c)) if self.debug: print("AFTER THE MASK " + m.nombre, get_col_val(c)) if self.debug: print("BEFORE THE MASK:", get_col_val(c)) # Transformo los valores enmascarados a cero # Transform the masked values to zero # c = c.map(tools.mask2zero) c = c.map(tools.image.mask2zero) if self.debug: print("After 0 Transform:", get_col_val(c)) # Renombra las bandas con los datos de la coleccion # Rename the bands with the collection data c = c.map(col.rename(drop=True)) # Cambio las bandas en comun de las collections # Change the common bands of the collections bandasrel = [] if self.debug: print("AFTER RENAMING BANDS:", get_col_val(c)) # Escalo a 0-1 c = c.map(col.do_scale()) if self.debug: if c.size().getInfo() > 0: print("AFTER SCALING:", get_col_val(c)) # Indices if indices: for i in indices: f = col.INDICES[i] c = c.map(f) if self.debug: print("SIZE AFTER COMPUTE " + i, c.size().getInfo()) # Antes de aplicar los puntajes, aplico la funcion que pasa # el usuario # Before applying the scores, I apply the function that the user passes c = c.map(fmap) # Puntajes # Scores if self.scores: for p in self.scores: if self.verbose: print("** " + p.name + " **") # Espero el tiempo seteado en cada puntaje # I wait for the time set in each score sleep = p.sleep for t in range(sleep): sys.stdout.write(str(t + 1) + ".") if (t + 1) == sleep: sys.stdout.write('\n') time.sleep(1) c = c.map(p.map(col=col, year=anio, colEE=c, geom=site)) # DEBUG if self.debug and n > 0: print("value:", get_col_val(c)) # Filtros if self.filters: for filter in self.filters: c = filter.apply(c, col=col, anio=self.year) # METODO NUEVO: selecciono las bandas en comun desp de unir # todas las collections usando un metodo distinto # NEW METHOD: I select the bands in common after uniting all the collections using a different method if self.debug: if c.size().getInfo() > 0: print("AFTER SELECTING COMMON BANDS:", get_col_val(c)) # Convierto los valores de las mascaras a 0 # I convert the values of the masks to 0 c = c.map(tools.mask2zero) # Agrego la band de fecha a la imagen # I add the date band to the image c = c.map(date.Date.map()) # Agrego la band col_id de la coleccion def addBandID(img): return img.addBands(bid) c = c.map(addBandID) if self.debug: print("AFTER ADDING col_id BAND:", get_col_val(c)) # Convierto a lista para agregar a la coleccion anterior # I convert to list to add to the previous collection c_list = c.toList(2500) colfinal = colfinal.cat(c_list) # Agrego col id y year al diccionario para propiedades # I add col id and year to the dictionary for properties n_imgs = "n_imgs_{cid}_{a}".format(cid=short, a=anio) toMetadata[n_imgs] = functions.get_size(c) # comprueba que la lista final tenga al menos un elemento # check that the final list has at least one element # s_fin = colfinal.size().getInfo() # 2 s_fin = functions.get_size(colfinal) # DEBUG if self.verbose: print("final collection size:", s_fin) if s_fin > 0: newcol = ee.ImageCollection(colfinal) # Selecciono las bandas en comun de todas las imagenes # I select the common bands of all the images newcol = functions.select_match(newcol) if self.debug: print("BEFORE score:", scores, "\n", get_col_val(c)) # Calcula el puntaje total sumando los puntajes # Calculate the total score by adding the scores ftotal = tools.sumBands("score", scores) newcol = newcol.map(ftotal) if self.debug: print("AFTER score:", get_col_val(c)) if normalize: newcol = newcol.map( tools.parametrize((0, maxpunt), (0, 1), ("score", ))) if self.debug: print("AFTER parametrize:", get_col_val(c)) return output(newcol, toMetadata) elif force: bands_from_col = self.colgroup.bandsrel() bands_from_scores = self.score_names if self.score_names else [ 'score' ] bands_from_indices = list(indices) if indices else [] bands = bands_from_col + bands_from_scores + bands_from_indices img = ee.Image.constant(0).select([0], [bands[0]]) for i, band in enumerate(bands): if i == 0: continue newimg = ee.Image.constant(0).select([0], [band]) img = img.addBands(newimg) return output(ee.ImageCollection([img]), toMetadata) else: return output(None, toMetadata)