def generateSamples_simple(folderSample, workingDirectory, trainShape, pathWd, featuresPath, samplesOptions, pathConf, dataField): bindingPython = Config(file(pathConf)).GlobChain.bindingPython dataField = Config(file(pathConf)).chain.dataField outputPath = Config(file(pathConf)).chain.outputPath userFeatPath = Config(file(pathConf)).chain.userFeatPath if userFeatPath == "None": userFeatPath = None tmpFolder = outputPath + "/TMPFOLDER" if not os.path.exists(tmpFolder): os.mkdir(tmpFolder) # Sensors S2 = Sensors.Sentinel_2("", Opath(tmpFolder), pathConf, "") L8 = Sensors.Landsat8("", Opath(tmpFolder), pathConf, "") L5 = Sensors.Landsat5("", Opath(tmpFolder), pathConf, "") # shutil.rmtree(tmpFolder, ignore_errors=True) SensorsList = [S2, L8, L5] stats = workingDirectory + "/" + trainShape.split("/")[-1].replace( ".shp", "_stats.xml") tile = trainShape.split("/")[-1].split("_")[0] stack = fu.getFeatStackName(pathConf) feat = featuresPath + "/" + tile + "/Final/" + stack if bindingPython == "True": feat = fu.FileSearch_AND(featuresPath + "/" + tile + "/tmp/", True, "ST_MASK")[0] os.environ["ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS"] = "1" cmd = "otbcli_PolygonClassStatistics -in " + feat + " -vec " + trainShape + " -out " + stats + " -field " + dataField print cmd os.system(cmd) verifPolyStats(stats) sampleSelection = workingDirectory + "/" + trainShape.split( "/")[-1].replace(".shp", "_SampleSel.sqlite") cmd = "otbcli_SampleSelection -out " + sampleSelection + " " + samplesOptions + " -field " + dataField + " -in " + feat + " -vec " + trainShape + " -instats " + stats print cmd os.system(cmd) # if pathWd:shutil.copy(sampleSelection,folderSample) os.environ["ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS"] = "5" samples = workingDirectory + "/" + trainShape.split("/")[-1].replace( ".shp", "_Samples.sqlite") if bindingPython == "True": sampleExtr = otb.Registry.CreateApplication("SampleExtraction") sampleExtr.SetParameterString("vec", sampleSelection) sampleExtr.SetParameterString("field", dataField) sampleExtr.SetParameterString("out", samples) AllRefl = sorted( fu.FileSearch_AND(featuresPath + "/" + tile + "/tmp/", True, "REFL.tif")) AllMask = sorted( fu.FileSearch_AND(featuresPath + "/" + tile + "/tmp/", True, "MASK.tif")) datesInterp = sorted( fu.FileSearch_AND(featuresPath + "/" + tile + "/tmp/", True, "DatesInterp")) realDates = sorted( fu.FileSearch_AND(featuresPath + "/" + tile + "/tmp/", True, "imagesDate")) print AllRefl print AllMask print datesInterp print realDates # gapFill + feat features = [] concatSensors = otb.Registry.CreateApplication("ConcatenateImages") for refl, mask, datesInterp, realDates in zip(AllRefl, AllMask, datesInterp, realDates): gapFill = otb.Registry.CreateApplication( "ImageTimeSeriesGapFilling") nbDate = fu.getNbDateInTile(realDates) nbReflBands = fu.getRasterNbands(refl) comp = int(nbReflBands) / int(nbDate) print datesInterp if not isinstance(comp, int): raise Exception("unvalid component by date (not integer) : " + comp) gapFill.SetParameterString("in", refl) gapFill.SetParameterString("mask", mask) gapFill.SetParameterString("comp", str(comp)) gapFill.SetParameterString("it", "linear") gapFill.SetParameterString("id", realDates) gapFill.SetParameterString("od", datesInterp) gapFill.Execute() # gapFill.SetParameterString("out","/ptmp/vincenta/tmp/TestGapFill.tif") # gapFill.ExecuteAndWriteOutput() # pause = raw_input("Pause1") # featExtr = otb.Registry.CreateApplication("iota2FeatureExtraction") # featExtr.SetParameterInputImage("in",gapFill.GetParameterOutputImage("out")) # featExtr.SetParameterString("comp",str(comp)) # for currentSensor in SensorsList: # if currentSensor.name in refl: # red = str(currentSensor.bands["BANDS"]["red"]) # nir = str(currentSensor.bands["BANDS"]["NIR"]) # swir = str(currentSensor.bands["BANDS"]["SWIR"]) # featExtr.SetParameterString("red",red) # featExtr.SetParameterString("nir",nir) # featExtr.SetParameterString("swir",swir) # featExtr.SetParameterString("ram","256") # featExtr.Execute() # features.append(featExtr) concatSensors.AddImageToParameterInputImageList( "il", gapFill.GetParameterOutputImage("out")) features.append(gapFill) # sensors Concatenation + sampleExtraction sampleExtr = otb.Registry.CreateApplication("SampleExtraction") sampleExtr.SetParameterString("ram", "1024") sampleExtr.SetParameterString("vec", sampleSelection) sampleExtr.SetParameterString("field", dataField) sampleExtr.SetParameterString("out", samples) if len(AllRefl) > 1: concatSensors.Execute() allFeatures = concatSensors.GetParameterOutputImage("out") else: allFeatures = features[0].GetParameterOutputImage("out") if userFeatPath: print "Add user features" userFeat_arbo = Config(file(pathConf)).userFeat.arbo userFeat_pattern = (Config( file(pathConf)).userFeat.patterns).split(",") concatFeatures = otb.Registry.CreateApplication( "ConcatenateImages") userFeatures = fu.getUserFeatInTile(userFeatPath, tile, userFeat_arbo, userFeat_pattern) concatFeatures.SetParameterStringList("il", userFeatures) concatFeatures.Execute() concatAllFeatures = otb.Registry.CreateApplication( "ConcatenateImages") concatAllFeatures.AddImageToParameterInputImageList( "il", allFeatures) concatAllFeatures.AddImageToParameterInputImageList( "il", concatFeatures.GetParameterOutputImage("out")) concatAllFeatures.Execute() allFeatures = concatAllFeatures.GetParameterOutputImage("out") sampleExtr.SetParameterInputImage("in", allFeatures) sampleExtr.ExecuteAndWriteOutput() # cmd = "otbcli_SampleExtraction -field "+dataField+" -out "+samples+" -vec "+sampleSelection+" -in /ptmp/vincenta/tmp/TestGapFill.tif" # print cmd # pause = raw_input("Pause") # os.system(cmd) else: cmd = "otbcli_SampleExtraction -field " + dataField + " -out " + samples + " -vec " + sampleSelection + " -in " + feat print cmd os.system(cmd) if pathWd: shutil.copy( samples, folderSample + "/" + trainShape.split("/")[-1].replace(".shp", "_Samples.sqlite")) os.remove(sampleSelection) os.remove(stats)
def launchClassification(tempFolderSerie, Classifmask, model, stats, outputClassif, confmap, pathWd, pathConf, pixType): outputClassif = outputClassif.replace(".tif", "_TMP.tif") confmap = confmap.replace(".tif", "_TMP.tif") os.environ["ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS"] = "5" featuresPath = Config(file(pathConf)).chain.featuresPath outputPath = Config(file(pathConf)).chain.outputPath tile = outputClassif.split("/")[-1].split("_")[1] userFeatPath = Config(file(pathConf)).chain.userFeatPath if userFeatPath == "None": userFeatPath = None AllRefl = sorted( fu.FileSearch_AND(featuresPath + "/" + tile + "/tmp/", True, "REFL.tif")) AllMask = sorted( fu.FileSearch_AND(featuresPath + "/" + tile + "/tmp/", True, "MASK.tif")) datesInterp = sorted( fu.FileSearch_AND(featuresPath + "/" + tile + "/tmp/", True, "DatesInterp")) realDates = sorted( fu.FileSearch_AND(featuresPath + "/" + tile + "/tmp/", True, "imagesDate")) tmpFolder = outputPath + "/TMPFOLDER_" + tile if not os.path.exists(tmpFolder): os.mkdir(tmpFolder) # Sensors S2 = Sensors.Sentinel_2("", Opath(tmpFolder), pathConf, "") L8 = Sensors.Landsat8("", Opath(tmpFolder), pathConf, "") L5 = Sensors.Landsat5("", Opath(tmpFolder), pathConf, "") # shutil.rmtree(tmpFolder, ignore_errors=True) SensorsList = [S2, L8, L5] # gapFill + feat features = [] concatSensors = otb.Registry.CreateApplication("ConcatenateImages") for refl, mask, datesInterp, realDates in zip(AllRefl, AllMask, datesInterp, realDates): gapFill = otb.Registry.CreateApplication("ImageTimeSeriesGapFilling") nbDate = fu.getNbDateInTile(realDates) nbReflBands = fu.getRasterNbands(refl) comp = int(nbReflBands) / int(nbDate) if not isinstance(comp, int): raise Exception("unvalid component by date (not integer) : " + comp) gapFill.SetParameterString("in", refl) gapFill.SetParameterString("mask", mask) gapFill.SetParameterString("comp", str(comp)) gapFill.SetParameterString("it", "linear") gapFill.SetParameterString("id", realDates) gapFill.SetParameterString("od", datesInterp) # gapFill.SetParameterString("ram","1024") gapFill.Execute() # featExtr = otb.Registry.CreateApplication("iota2FeatureExtraction") # featExtr.SetParameterInputImage("in",gapFill.GetParameterOutputImage("out")) # featExtr.SetParameterString("comp",str(comp)) # for currentSensor in SensorsList: # if currentSensor.name in refl: # red = str(currentSensor.bands["BANDS"]["red"]) # nir = str(currentSensor.bands["BANDS"]["NIR"]) # swir = str(currentSensor.bands["BANDS"]["SWIR"]) # featExtr.SetParameterString("red",red) # featExtr.SetParameterString("nir",nir) # featExtr.SetParameterString("swir",swir) # featExtr.Execute() # features.append(featExtr) concatSensors.AddImageToParameterInputImageList( "il", gapFill.GetParameterOutputImage("out")) features.append(gapFill) classifier = otb.Registry.CreateApplication("ImageClassifier") classifier.SetParameterString("mask", Classifmask) if stats: classifier.SetParameterString("imstat", stats) classifier.SetParameterString("out", outputClassif) classifier.SetParameterString("model", model) classifier.SetParameterString("confmap", confmap) classifier.SetParameterString("ram", "512") print "AllRefl" print AllRefl # if len(AllRefl) >1: # concatSensors.Execute() # classifier.SetParameterInputImage("in",concatSensors.GetParameterOutputImage("out")) # else: # classifier.SetParameterInputImage("in",features[0].GetParameterOutputImage("out")) # classifier.ExecuteAndWriteOutput() if len(AllRefl) > 1: concatSensors.Execute() allFeatures = concatSensors.GetParameterOutputImage("out") else: allFeatures = features[0].GetParameterOutputImage("out") if userFeatPath: print "Add user features" userFeat_arbo = Config(file(pathConf)).userFeat.arbo userFeat_pattern = (Config( file(pathConf)).userFeat.patterns).split(",") concatFeatures = otb.Registry.CreateApplication("ConcatenateImages") userFeatures = fu.getUserFeatInTile(userFeatPath, tile, userFeat_arbo, userFeat_pattern) concatFeatures.SetParameterStringList("il", userFeatures) concatFeatures.Execute() concatAllFeatures = otb.Registry.CreateApplication("ConcatenateImages") concatAllFeatures.AddImageToParameterInputImageList("il", allFeatures) concatAllFeatures.AddImageToParameterInputImageList( "il", concatFeatures.GetParameterOutputImage("out")) concatAllFeatures.Execute() allFeatures = concatAllFeatures.GetParameterOutputImage("out") classifier.SetParameterInputImage("in", allFeatures) classifier.ExecuteAndWriteOutput() expr = "im2b1>=1?im1b1:0" cmd = 'otbcli_BandMath -il ' + outputClassif + ' ' + Classifmask + ' -out ' + outputClassif.replace( "_TMP.tif", ".tif") + ' -exp "' + expr + '"' print cmd os.system(cmd) cmd = 'otbcli_BandMath -il ' + confmap + ' ' + Classifmask + ' -out ' + confmap.replace( "_TMP.tif", ".tif") + ' -exp "' + expr + '"' print cmd os.system(cmd) if pathWd: shutil.copy(outputClassif.replace("_TMP.tif", ".tif"), outputPath + "/classif") if pathWd: shutil.copy(confmap.replace("_TMP.tif", ".tif"), outputPath + "/classif") os.remove(outputClassif) os.remove(confmap)