def RastersToSqlitePoint(path, vecteur, field, outname, ram, rtype, rasters, maskmer=None, split=None): timeinit = time.time() # Rasters concatenation if len(rasters) > 1: concatApp = OtbAppBank.CreateConcatenateImagesApplication({ "il": rasters, "ram": ram, "pixType": rtype }) concatApp.Execute() classif = OtbAppBank.CreateBandMathApplication({ "il": rasters[0], "exp": "im1b1", "ram": ram, "pixType": rtype }) classif.Execute() else: concatApp = OtbAppBank.CreateBandMathApplication({ "il": rasters, "exp": "im1b1", "ram": ram, "pixType": rtype }) concatApp.Execute() timeconcat = time.time() print " ".join([ " : ".join(["Raster concatenation", str(timeconcat - timeinit)]), "seconds" ]) # Stats and sample selection if len(rasters) == 1: classif = concatApp outsqlite = sampleSelection(path, classif, vecteur, field, ram, split, maskmer) # Stats extraction outtmp = os.path.join(path, os.path.basename(outname)) sampleExtraction(concatApp, outsqlite, field, outtmp, split, ram) shutil.copyfile(outtmp, outname)
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")
def clumpAndStackClassif(path, raster, outpath, ram, float64=False, exe64="", logger=logger): begin_clump = time.time() # split path and file name of outfilename out = os.path.dirname(outpath) outfilename = os.path.basename(outpath) # Clump Classif with OTB segmentation algorithm clumpAppli = OtbAppBank.CreateClumpApplication({ "in": raster, "filter.cc.expr": 'distance<1', "ram": str(0.2 * float(ram)), "pixType": 'uint32', "mode": "raster", "filter": "cc", "mode.raster.out": os.path.join(path, 'clump.tif') }) if not float64: clumpAppli.Execute() clumptime = time.time() logger.info(" ".join([ " : ".join( ["Input raster well clumped : ", str(clumptime - begin_clump)]), "seconds" ])) # Add 300 to all clump ID bandMathAppli = OtbAppBank.CreateBandMathApplication({ "il": clumpAppli, "exp": 'im1b1+300', "ram": str(0.2 * float(ram)), "pixType": 'uint32', "out": os.path.join(path, 'clump300.tif') }) bandMathAppli.Execute() dataRamAppli = OtbAppBank.CreateBandMathApplication({ "il": raster, "exp": 'im1b1', "ram": str(0.2 * float(ram)), "pixType": 'uint8' }) dataRamAppli.Execute() concatImages = OtbAppBank.CreateConcatenateImagesApplication({ "il": [dataRamAppli, bandMathAppli], "ram": str(0.2 * float(ram)), "pixType": 'uint32', "out": os.path.join(path, outfilename) }) concatImages.ExecuteAndWriteOutput() concattime = time.time() logger.info(" ".join([ " : ".join([ "Regularized and Clumped rasters concatenation : ", str(concattime - clumptime) ]), "seconds" ])) shutil.copyfile(os.path.join(path, outfilename), os.path.join(out, outfilename)) else: clumpAppli.ExecuteAndWriteOutput() command = '%s/iota2BandMath %s "%s" %s %s'%(exe64, \ os.path.join(path, 'clump.tif'), \ "im1b1+300", \ os.path.join(path, 'clump300.tif'), \ 10) try: Utils.run(command) clumptime = time.time() logger.info(" ".join([ " : ".join([ "Input raster well clumped : ", str(clumptime - begin_clump) ]), "seconds" ])) except: logger.error( "Application 'iota2BandMath' for 64 bits does not exist, please change 64 bits binaries path" ) sys.exit() command = '%s/iota2ConcatenateImages %s %s %s %s'%((exe64, raster, \ os.path.join(path, 'clump300.tif'), \ os.path.join(path, outfilename), 10)) try: Utils.run(command) concattime = time.time() logger.info(" ".join([" : ".join(["Regularized and Clumped rasters concatenation : ", \ str(concattime - clumptime)]), "seconds"])) shutil.copyfile(os.path.join(path, outfilename), os.path.join(out, outfilename)) os.remove(os.path.join(path, 'clump.tif')) os.remove(os.path.join(path, 'clump300.tif')) except: logger.error( "Application 'iota2ConcatenateImages' for 64 bits does not exist, please change 64 bits binaries path" ) sys.exit() command = "gdal_translate -q -b 2 -ot Uint32 %s %s" % (os.path.join( path, outfilename), os.path.join(path, "clump32bits.tif")) Utils.run(command) shutil.copy(os.path.join(path, "clump32bits.tif"), out) os.remove(os.path.join(path, "clump32bits.tif")) if os.path.exists(os.path.join(path, outfilename)): os.remove(os.path.join(path, outfilename)) clumptime = time.time() logger.info(" ".join( [" : ".join(["Clump : ", str(clumptime - begin_clump)]), "seconds"]))