def calcUnpix_generic(col, score): """ DO NOT USE. It's a test method """ imgCol = col # tamcol = funciones.execli(imgCol.size().getInfo)() img = imgCol.qualityMosaic(score) if Bap.debug: print(" AFTER qualityMosaic:", img.bandNames().getInfo()) # CONVIERTO LOS VALORES ENMASCARADOS EN 0 # img = tools.mask2zero(img) img = tools.mask2zero(img) return img
def test_mask2(self): masked_img = self.l8SR.updateMask(self.l8SR.select(["cfmask"]).eq(0)) mask2zero = tools.mask2zero(masked_img) val_no_change = tools.get_value(mask2zero, self.p_l8SR_no_cloud, 30)["B1"] val_change = tools.get_value(mask2zero, self.p_l8SR_cloud, 30)["B1"] self.assertEqual(val_no_change, 517) self.assertEqual(val_change, 0) mask2num = tools.mask2number(10)(masked_img) val_no_change_num = tools.get_value(mask2num, self.p_l8SR_no_cloud, 30)["B1"] val_change_num = tools.get_value(mask2num, self.p_l8SR_cloud, 30)["B1"] self.assertEqual(val_no_change_num, 517) self.assertEqual(val_change_num, 10)
def if_true(): # Select pixels with value different to zero ceros = img.select(banda).neq(0) return tools.mask2zero(ceros)
def stretches(ltr, min_threshold=0.05, max_threshold=0.2): """ This method characterize the segmentation stretches and returns as many images as the amount of stretches. Categories are: * 1: no change: -min_threshold <= value <= min_threshold * 2: soft loss: -max_threshold <= value < -min_threshold * 3: steep loss: value < -max_threshold * 4: soft gain: min_threshold < value <= max_threshold * 5: steep gain: max_threshold < value :param min_threshold: divides 'no change' and 'soft change' :type min_threshold: float :param max_threshold: divides 'soft change' and 'steep change' :type max_threshold: float Return a new class called 'Stretches' with the following properties: - img_list: a list of images containing the stretches. Each image has the following bands: - t{n}_slope: slope of stretch n - t{n}_duration: duration of stretch n (in years) - t{n}_cat: category for stretch n - image: an image with unified results. Will have as many bands as found stretches, times 3. In case stretches in one pixel are less than stretches in the whole image, the last will be empty. Example: The whole image has 4 stretches. The pixel that has 2 stretches will have the following values: - t1_slope: value - t2_slope: value - t3_slope: 0 - t4_slope: 0 :rtype: namedtuple """ # Threshold images umb1pos = ee.Image.constant(min_threshold).toFloat() umb2pos = ee.Image.constant(max_threshold).toFloat() umb1neg = ee.Image.constant(-min_threshold).toFloat() umb2neg = ee.Image.constant(-max_threshold).toFloat() # Number of breakpoints in each pixel bkps = ltr.breakpoints img_bkp = bkps.image # Total number of breakpoints total_bkp = bkps.total.getInfo() max_tramos = total_bkp - 1 bandas_a = ["year_" + str(i) for i in range(total_bkp)] bandas_fit = ["fit_" + str(i) for i in range(total_bkp)] bandas = bandas_a + bandas_fit b2b = ltr.break2band().select(bandas) imagen = b2b.addBands(img_bkp) # OBTENGO TANTAS IMAGENES COMO TRAMOS HAYA EN LA COLECCION listaimg = [] for t, bkp in enumerate(range(2, total_bkp + 1)): tramos = t + 1 mask = imagen.select("n_bkp").eq(bkp) masked = imagen.updateMask(mask) img_tramos = ee.Image.constant(0) for id, tramo in enumerate(range(tramos, 0, -1)): # NOMBRE DE LA BANDA nombre_tramo = "t" + str(id + 1) # INDICES INI Y FIN ini = max_tramos - tramo fin = ini + 1 # SELECCIONO LAS BANDAS a_ini = ee.Image(masked.select("year_" + str(ini))).toFloat() a_fin = ee.Image(masked.select("year_" + str(fin))).toFloat() fit_ini = masked.select("fit_" + str(ini)) fit_fin = masked.select("fit_" + str(fin)) # COMPUTO lapso = a_fin.subtract(a_ini) dif_total = fit_fin.subtract(fit_ini) dif_anual = dif_total.divide(lapso) slope = dif_anual.select([0], [nombre_tramo + "_slope"]) duracion = ee.Image(lapso.select( [0], [nombre_tramo + "_duration"])).toUint8() # CATEGORIZACION uno = slope.gte(umb1neg).And(slope.lte(umb1pos)) dos = slope.lt(umb1neg).And(slope.gte(umb2neg)) tres = slope.lt(umb2neg) cuatro = slope.gt(umb1pos).And(slope.lte(umb2pos)) cinco = slope.gt(umb2pos) cat = uno.add(dos.multiply(2)).add(tres.multiply(3)).add( cuatro.multiply(4)).add(cinco.multiply(5)) cat = ee.Image(cat).select([0], [nombre_tramo + "_cat"]).toUint8() img_tramos = img_tramos.addBands(slope).addBands( duracion).addBands(cat) # SACO LA PRIMER BANDA QUE SE CREA ANTES DE INICIAR EL LOOP bandas = img_tramos.bandNames().slice(1) img_tramos = img_tramos.select(bandas) listaimg.append(img_tramos) # CREO UNA IMAGEN VACIA CON TODAS LAS BANDAS bandas_cat = ["t{0}_cat".format(str(n + 1)) for n in range(max_tramos)] bandas_slope = ["t{0}_slope".format(str(n + 1)) for n in range(max_tramos)] bandas_duracion = [ "t{0}_duration".format(str(n + 1)) for n in range(max_tramos) ] bandas_todas = zip(bandas_slope, bandas_duracion, bandas_cat) bandas_todas = list(chain.from_iterable(bandas_todas)) # bandas_todas = bandas_cat+bandas_slope+bandas_duracion lista_ini = ee.List(bandas_todas).slice(1) img_ini = ee.Image.constant(0).select([0], ["t1_slope"]) def addB(item, ini): ini = ee.Image(ini) img = ee.Image.constant(0).select([0], [item]) return ini.addBands(img) img_final = ee.Image(lista_ini.iterate(addB, img_ini)) for tramos, img in enumerate(listaimg): tramos += 1 faltan = max_tramos - tramos # print "faltan", faltan # nombre = "falta{}tramo".format(str(faltan)) if faltan == 0: img = tools.mask2zero(img) img_final = img_final.add(img) # funciones.asset(img, nombre, "users/rprincipe/Pruebas/"+nombre, ltr.region) continue for tramo in range(faltan): tramo += 1 i = tramo + tramos bcat = ee.Image.constant(0).select([0], ["t{0}_cat".format(str(i))]) bslope = ee.Image.constant(0).select([0], ["t{0}_slope".format(str(i))]) bdur = ee.Image.constant(0).select( [0], ["t{0}_duration".format(str(i))]) total = bcat.addBands(bslope).addBands(bdur) img = img.addBands(total) img = tools.mask2zero(img) # funciones.asset(img, nombre, "users/rprincipe/Pruebas/"+nombre, ltr.region) # bandas = img.getInfo()["bands"] # print "tramos:", tramos, [i["id"] for i in bandas] img_final = img_final.add(img) # return resultado = namedtuple("Stretches", ["img_list", "image"]) return resultado(listaimg, img_final)
def classify(ltr, *args, **kwargs): """ Clasificación de los tramos :param args: Mismos argumentos que la función stretches() :param kwargs: Mismos argumentos que la funcion stretches() :return: """ tramos = stretches(*args, **kwargs) lista = tramos.img_list umbral1 = kwargs.get("min_threshold", 0.05) umbral2 = kwargs.get("max_threshold", 0.2) final = ee.Image.constant(0).select([0], ["cat"]) for n, img in enumerate(lista): n += 1 # img = tools.mask2zero(img) img = img.unmask() if n == 1: t1_cat = img.select("t1_cat") t1_duracion = img.select("t1_duration") t1_slope = img.select("t1_slope") t1_caida = t1_duracion.multiply(t1_slope).toFloat() cat = t1_caida.lte(-umbral1).multiply(3).select([0], ["cat"]) # cat = t1_cat.eq(2).Or(t1_cat.eq(3)).multiply(3).select([0],["cat"]) final = final.add(cat) # continue break elif n == 2: t1_cat = img.select("t1_cat") t2_cat = img.select("t2_cat") t1_est = t1_cat.eq(1) t2_caida = t2_cat.eq(3) t2_suave = t2_cat.eq(2) cambio = t1_est.And(t2_caida) posible = t1_est.And(t2_suave).multiply(2) cat = cambio.add(posible).select([0], ["cat"]) final = final.add(cat) # continue break cat0 = ee.Image.constant(0).select([0], ["cat"]) for tramos in range(3, n + 1): n1 = str(tramos - 2) n2 = str(tramos - 1) n3 = str(tramos) t1_cat = img.select("t{0}_cat".format(n1)) t2_cat = img.select("t{0}_cat".format(n2)) t3_cat = img.select("t{0}_cat".format(n3)) t2_slope = img.select("t{0}_slope".format(n2)) t2_duracion = img.select("t{0}_duration".format(n2)) t1_est = t1_cat.eq(1) t1_suave = t1_cat.eq(2) t1_caida = t1_cat.eq(3) t2_caida = t2_cat.eq(3) t2_suave = t2_cat.eq(2) t3_est = t3_cat.eq(1) t3_crec = t3_cat.eq(4) t2_mag = t2_slope.multiply(t2_duracion) cambio1 = t1_est.And(t2_caida).And(t3_est) # stable, cambio2 = t1_suave.And(t2_caida).And(t3_est) cambio3 = t1_est.And(t2_caida).And(t3_crec) cambio4 = t1_suave.And(t2_caida).And(t3_crec) cambio5 = t1_caida.And(t2_suave).And(t3_est) cambio6 = t1_caida.And(t2_suave).And(t3_crec) cambio7 = t1_caida.And(t2_caida).And(t3_est) cambio8 = t1_caida.And(t2_caida).And(t3_crec) cambio = cambio1.Or(cambio2).Or(cambio3).Or(cambio4).Or( cambio5).Or(cambio6).Or(cambio7).Or(cambio8) posible1 = t1_est.And(t2_suave).And(t3_est).multiply(2) # posible2 = t1_est.And(t2_mag.lte(-umbral2)).And(t3_est.Or(t3_crec)).multiply(2) posible = posible1 #.Or(posible2) cat = cambio.add(posible).select([0], ["cat"]) cat = tools.mask2zero(cat) cat0 = cat0.add(cat) final = final.add(cat0) return final