def sampleExtraction(raster, sample, field, outname, split, ram='128'): timesample = time.time() # Sample extraction outfile = os.path.splitext(str(outname))[0] + split + os.path.splitext( str(outname))[1] otbParams = { 'in': raster, 'vec': sample, 'field': field.lower(), 'out': outfile, 'ram': ram } extractApp = OtbAppBank.CreateSampleExtractionApplication(otbParams) extractApp.ExecuteAndWriteOutput() timeextract = time.time() print " ".join([ " : ".join(["Sample extraction", str(timeextract - timesample)]), "seconds" ])
def extractStats(vectorIn, pathConf, wD=None): dataField = Config(open(pathConf)).chain.dataField iota2Folder = Config(open(pathConf)).chain.outputPath tileToCompute = vectorIn.split("/")[-1].split("_")[0] modelToCompute = vectorIn.split("/")[-1].split("_")[2].split("f")[0] seed = vectorIn.split("/")[-1].split("_")[3].replace("seed", "") workingDirectory = iota2Folder + "/final/TMP" shapeMode = vectorIn.split("/")[-1].split("_")[-1].split(".")[ 0] #'learn' or 'val' if wD: workingDirectory = wD try: refImg = fut.FileSearch_AND(iota2Folder + "/final/TMP", True, tileToCompute, ".tif")[0] except: raise Exception("reference image can not be found in " + iota2Folder + "/final/TMP") statsFile = workingDirectory + "/" + tileToCompute + "_stats_model_" + modelToCompute + ".xml" stats = otbApp.CreatePolygonClassStatisticsApplication({"in":refImg, "vec":vectorIn,\ "out":statsFile, "field":dataField}) stats.ExecuteAndWriteOutput() selVector = workingDirectory + "/" + tileToCompute + "_selection_model_" + modelToCompute + ".sqlite" sampleS = otbApp.CreateSampleSelectionApplication({"in":refImg, "vec":vectorIn, "out":selVector,\ "instats":statsFile, "strategy":"all",\ "field":dataField}) sampleS.ExecuteAndWriteOutput() classificationRaster = fut.FileSearch_AND( iota2Folder + "/final/TMP", True, tileToCompute + "_seed_" + seed + ".tif")[0] validity = fut.FileSearch_AND(iota2Folder + "/final/TMP", True, tileToCompute + "_Cloud.tif")[0] confiance = fut.FileSearch_AND( iota2Folder + "/final/TMP", True, tileToCompute + "_GlobalConfidence_seed_" + seed + ".tif")[0] stack = [classificationRaster, validity, confiance] dataStack = otbApp.CreateConcatenateImagesApplication({ "il": stack, "ram": '1000', "pixType": "uint8", "out": "" }) dataStack.Execute() outSampleExtraction = workingDirectory + "/" + tileToCompute + "_extraction_model_" + modelToCompute + "_" + shapeMode + ".sqlite" extraction = otbApp.CreateSampleExtractionApplication({"in":dataStack, "vec":selVector,\ "field":dataField, " out":outSampleExtraction,\ "outfield":"list",\ "outfield.list.names":["predictedClass", "validity", "confidence"]}) extraction.ExecuteAndWriteOutput() conn = lite.connect(outSampleExtraction) cursor = conn.cursor() SQL = "alter table output add column TILE TEXT" cursor.execute(SQL) SQL = "update output set TILE='" + tileToCompute + "'" cursor.execute(SQL) SQL = "alter table output add column MODEL TEXT" cursor.execute(SQL) SQL = "update output set MODEL='" + modelToCompute + "'" cursor.execute(SQL) conn.commit() os.remove(statsFile) os.remove(selVector) if wD: shutil.copy(outSampleExtraction, iota2Folder + "/final/TMP")