def rastToVectRecode(path, classif, vector, outputName, ram = "10000", dtype = "uint8", valvect = 255, valrastout = 255): """ Convert vector in raster file and change background value Parameters ---------- path : string working directory classif : string path to landcover classification vector : string vector file to rasterize outputName : string output filename and path ram : string ram for OTB applications dtype : string pixel type of the output raster valvect : integer value of vector to search valrastout : integer value to use to recode """ # Empty raster bmapp = OtbAppBank.CreateBandMathApplication({"il": classif, "exp": "im1b1*0", "ram": str(1 * float(ram)), "pixType": dtype, "out": os.path.join(path, 'temp.tif')}) #bandMathAppli.ExecuteAndWriteOutput() p = mp.Process(target=executeApp, args=[bmapp]) p.start() p.join() # Burn tifMasqueMerRecode = os.path.join(path, 'masque_mer_recode.tif') rastApp = OtbAppBank.CreateRasterizationApplication({"in" : vector, "im" : os.path.join(path, 'temp.tif'), "background": 1, "out": tifMasqueMerRecode}) #bandMathAppli.ExecuteAndWriteOutput() p = mp.Process(target=executeApp, args=[rastApp]) p.start() p.join() # Differenciate inland water and sea water bandMathAppli = OtbAppBank.CreateBandMathApplication({"il": [classif, tifMasqueMerRecode], "exp": "(im2b1=={})?im1b1:{}".format(valvect, valrastout), "ram": str(1 * float(ram)), "pixType": dtype, "out": outputName}) #bandMathAppli.ExecuteAndWriteOutput() p = mp.Process(target=executeApp, args=[bandMathAppli]) p.start() p.join() os.remove(tifMasqueMerRecode)
def maskSampleSelection(path, raster, maskmer, ram): tifMasqueMer = os.path.join(path, 'masque_mer.tif') bmapp = OtbAppBank.CreateBandMathApplication({ "il": raster, "exp": "im1b1*0", "ram": ram, "pixType": 'uint8', "out": tifMasqueMer }) bmapp.ExecuteAndWriteOutput() maskmerbuff = os.path.join( path, os.path.splitext(os.path.basename(maskmer))[0] + '_buff.shp') BufferOgr.bufferPoly(maskmer, maskmerbuff, 500) tifMasqueMerRecode = os.path.join(path, 'masque_mer_recode.tif') rastApp = OtbAppBank.CreateRasterizationApplication( maskmerbuff, tifMasqueMer, 1, tifMasqueMerRecode) rastApp.Execute() #command = "gdal_rasterize -burn 1 %s %s"%(maskmerbuff, tifMasqueMer) #os.system(command) out = os.path.join(path, 'mask.tif') bmapp = OtbAppBank.CreateBandMathApplication({ "il": [raster, rastApp], "exp": "((im1b1==0) || (im1b1==51)) && (im2b1==0)?0:1", "ram": ram, "pixType": 'uint8', "out": out }) bmapp.ExecuteAndWriteOutput() return out
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 generateBorderMask(data_img, out_mask, RAMPerProcess=4000): """ """ threshold = 0.0011 mask = OtbAppBank.CreateBandMathApplication({ "il": data_img, "exp": "im1b1<{}?1:0".format(threshold), "ram": str(RAMPerProcess), "pixType": 'uint8' }) mask.Execute() borderMask = OtbAppBank.CreateBinaryMorphologicalOperation({ "in": mask, "out": out_mask, "ram": str(RAMPerProcess), "pixType": "uint8", "filter": "opening", "ballxradius": 5, "ballyradius": 5 }) dep = mask return borderMask, dep
def generateBorderMask(self, AllOrtho): print("Generate Mask ...") masks = [] for currentOrtho, _ in AllOrtho: outputParameter = OtbAppBank.getInputParameterOutput(currentOrtho) if "vv" not in currentOrtho.GetParameterValue(outputParameter): continue workingDirectory = os.path.split( currentOrtho.GetParameterValue(outputParameter))[0] nameBorderMask = os.path.split( currentOrtho.GetParameterValue(outputParameter))[1].replace( ".tif", "_BorderMask.tif") nameBorderMaskTMP = os.path.split( currentOrtho.GetParameterValue(outputParameter))[1].replace( ".tif", "_BorderMask_TMP.tif") bandMathMask = os.path.join(workingDirectory, nameBorderMaskTMP) currentOrtho_out = currentOrtho if self.wMode: currentOrtho_out.GetParameterValue(outputParameter) maskBM = OtbAppBank.CreateBandMathApplication({ "il": currentOrtho_out, "exp": "im1b1<0.0011?1:0", "ram": str(self.RAMPerProcess), "pixType": 'uint8', "out": bandMathMask }) if self.wMode: maskBM.ExecuteAndWriteOutput() else: maskBM.Execute() borderMaskOut = os.path.join(workingDirectory, nameBorderMask) maskBM_out = maskBM if self.wMode: maskBM_out.GetParameterValue("out") borderMask = OtbAppBank.CreateBinaryMorphologicalOperation({ "in": maskBM, "out": borderMaskOut, "ram": str(self.RAMPerProcess), "pixType": "uint8", "filter": "opening", "ballxradius": 5, "ballyradius": 5 }) masks.append((borderMask, maskBM)) return masks
def doCalibrationCmd(self, rawRaster): """ OUT : allCmdOrho [list of otb application list to Execute or ExecuteAndWriteOutput] allCmdCalib [all dependence to run ortho] """ allCmdCalib = [] allCmdOrtho = [] for i in range(len(rawRaster)): for image in rawRaster[i].GetImageList(): calibrate = image.replace(".tiff", "_calibrate.tiff") image_OK = image.replace(".tiff", "_OrthoReady.tiff") if os.path.exists(image_OK) == True: continue calib = OtbAppBank.CreateSarCalibration({ "in": image, "out": calibrate, "lut": "gamma", "ram": str(self.RAMPerProcess) }) if self.wMode: calib.ExecuteAndWriteOutput() else: calib.Execute() allCmdCalib.append(calib) calib_out = calib if self.wMode: calib_out = calib.GetParameterValue("out") expression = 'im1b1<' + str(self.borderThreshold) + '?' + str( self.borderThreshold) + ':im1b1 ' orthoRdy = OtbAppBank.CreateBandMathApplication({ "il": calib_out, "exp": expression, "ram": str(self.RAMPerProcess), "pixType": "float", "out": image_OK }) allCmdOrtho.append(orthoRdy) return allCmdOrtho, allCmdCalib
def maskOTBBandMathOutput(path, raster, exp, ram, output, dstnodata=0): outbm = os.path.join(path, "mask.tif") bandMathAppli = OtbAppBank.CreateBandMathApplication({ "il": raster, "exp": exp, "ram": str(ram), "pixType": "uint8", "out": outbm }) p = mp.Process(target=executeApp, args=[bandMathAppli]) p.start() p.join() gdal.Warp(output, outbm, dstNodata=dstnodata, multithread=True, format="GTiff", \ warpOptions=[["NUM_THREADS=ALL_CPUS"],["OVERWRITE=TRUE"]]) os.remove(outbm) return output
def LaunchSARreprojection(rasterList, refRaster=None, tileName=None, SRTM=None, geoid=None, output_directory=None, RAMPerProcess=None, workingDirectory=None): """must be use with multiprocessing.Pool """ def writeOutputRaster_2(OTB_App, overwrite=True, workingDirectory=None, dep=None, logger=logger): """ """ import shutil from Common import OtbAppBank as otbApp out_param = otbApp.getInputParameterOutput(OTB_App) out_raster = OTB_App.GetParameterValue(out_param) launch_write = True if os.path.exists(out_raster.split("?")[0]) and not overwrite: launch_write = False if workingDirectory is None and launch_write: OTB_App.ExecuteAndWriteOutput() elif launch_write: out_raster_dir, out_raster_name = os.path.split(out_raster) out_workingDir = os.path.join(workingDirectory, out_raster_name) out_workingDir = out_workingDir.split("?")[0] OTB_App.SetParameterString(out_param, out_workingDir) OTB_App.ExecuteAndWriteOutput() shutil.copy(out_workingDir, out_raster.split("?")[0]) if os.path.exists(out_workingDir.replace(".tif", ".geom")): shutil.copy(out_workingDir.replace(".tif", ".geom"), out_raster.replace(".tif", ".geom").split("?")[0]) if not launch_write: logger.info("{} already exists and will not be overwrited".format( out_raster)) OTB_App = None return out_raster date_position = 4 all_superI_vv = [] all_superI_vh = [] all_acquisition_date_vv = [] all_acquisition_date_vh = [] dates = [] all_dep = [] for date_to_Concatenate in rasterList: #Calibration + Superimpose vv, vh = date_to_Concatenate.GetImageList() SAR_directory, SAR_name = os.path.split(vv) currentPlatform = getPlatformFromS1Raster(vv) manifest = date_to_Concatenate.getManifest() currentOrbitDirection = getOrbitDirection(manifest) acquisition_date = SAR_name.split("-")[date_position] dates.append(acquisition_date) calib_vv = OtbAppBank.CreateSarCalibration({ "in": vv, "lut": "gamma", "ram": str(RAMPerProcess) }) calib_vh = OtbAppBank.CreateSarCalibration({ "in": vh, "lut": "gamma", "ram": str(RAMPerProcess) }) calib_vv.Execute() calib_vh.Execute() all_dep.append(calib_vv) all_dep.append(calib_vh) orthoImageName_vv = "{}_{}_{}_{}_{}".format(currentPlatform, tileName, "vv", currentOrbitDirection, acquisition_date) super_vv, super_vv_dep = OtbAppBank.CreateSuperimposeApplication({ "inr": refRaster, "inm": calib_vv, "pixType": "float", "interpolator": "bco", "ram": RAMPerProcess, "elev.dem": SRTM, "elev.geoid": geoid }) orthoImageName_vh = "{}_{}_{}_{}_{}".format(currentPlatform, tileName, "vh", currentOrbitDirection, acquisition_date) super_vh, super_vh_dep = OtbAppBank.CreateSuperimposeApplication({ "inr": refRaster, "inm": calib_vh, "pixType": "float", "interpolator": "bco", "ram": RAMPerProcess, "elev.dem": SRTM, "elev.geoid": geoid }) super_vv.Execute() super_vh.Execute() all_dep.append(super_vv) all_dep.append(super_vh) all_superI_vv.append(super_vv) all_superI_vh.append(super_vh) all_acquisition_date_vv.append(orthoImageName_vv) all_acquisition_date_vh.append(orthoImageName_vh) all_acquisition_date_vv = "_".join(sorted(all_acquisition_date_vv)) all_acquisition_date_vh = "_".join(sorted(all_acquisition_date_vh)) #Concatenate thanks to a BandMath vv_exp = ",".join( ["im{}b1".format(i + 1) for i in range(len(all_superI_vv))]) vv_exp = "max({})".format(vv_exp) SAR_vv = os.path.join(output_directory, all_acquisition_date_vv + ".tif") concatAppli_vv = OtbAppBank.CreateBandMathApplication({ "il": all_superI_vv, "exp": vv_exp, "out": SAR_vv, "ram": str(RAMPerProcess), "pixType": "float" }) vh_exp = ",".join( ["im{}b1".format(i + 1) for i in range(len(all_superI_vh))]) vh_exp = "max({})".format(vh_exp) SAR_vh = os.path.join(output_directory, all_acquisition_date_vh + ".tif") concatAppli_vh = OtbAppBank.CreateBandMathApplication({ "il": all_superI_vh, "exp": vh_exp, "out": SAR_vh, "ram": str(RAMPerProcess), "pixType": "float" }) ortho_path = writeOutputRaster_2(concatAppli_vv, overwrite=False, workingDirectory=workingDirectory, dep=all_dep) ortho_path = writeOutputRaster_2(concatAppli_vh, overwrite=False, workingDirectory=workingDirectory, dep=all_dep) #from the results generate a mask super_vv = os.path.join(output_directory, all_acquisition_date_vv + ".tif") border_mask = super_vv.replace(".tif", "_BorderMask.tif") mask_app, _ = generateBorderMask(super_vv, border_mask, RAMPerProcess=RAMPerProcess) mask_path = writeOutputRaster(mask_app, overwrite=False, workingDirectory=workingDirectory) mask_path_geom = mask_path.replace(".tif", ".geom") if os.path.exists(mask_path_geom): os.remove(mask_path_geom) return (SAR_vv, SAR_vh)
def concatenateImage(self, orthoList, maskList, tile): """ Concatenate Ortho at the same date """ def sortByFirstElem(MyList): from collections import defaultdict """ Example 1: MyList = [(1,2),(1,1),(6,1),(1,4),(6,7)] print sortByElem(MyList) >> [(1, [2, 1, 4]), (6, [1, 7])] Example 2: MyList = [((1,6),2),((1,6),1),((1,2),1),((1,6),4),((1,2),7)] print sortByElem(MyList) >> [((1, 2), [1, 7]), ((1, 6), [2, 1, 4])] """ d = defaultdict(list) for k, v in MyList: d[k].append(v) return list(d.items()) def findTilesToConcatenate(applicationList): """ OUT: listOfList: Example [[r1,r2],[r3,r4],... mean r1 and r2 must be concatenates together same for r3,r4 """ concatenate = [] names = [(currentName.split("_")[-1].split("t")[0], currentName) for currentName in OtbAppBank.unPackFirst(applicationList) ] names = sortByFirstElem(names) toConcat = [ rasterList for currentDate, rasterList in names if len(rasterList) > 2 ] for dateToConcat in toConcat: tmp = [(currentRaster.split("_")[2], currentRaster) for currentRaster in dateToConcat] tmp = sortByFirstElem(tmp)[::-1] #VV first then VH for pol, rasters in tmp: concatenate.append(rasters) Filter = [] for ToConcat in concatenate: sat = [CToConcat.split("_")[0] for CToConcat in ToConcat] if not sat.count(sat[0]) == len(sat): continue Filter.append(ToConcat) return Filter def findMasksToConcatenate(maskList): concatenate = [] names = [ os.path.split(mask.GetParameterValue("out"))[-1].split("?")[0] for mask, dep in maskList ] nameDate = [(name.split("_")[4].split("t")[0], name) for name in names] nameDate = sortByFirstElem(nameDate) for date, maskList in nameDate: if len(maskList) > 1: concatenate.append(maskList) #check if all masks comes from the same satellite maskFilter = [] for masksToConcat in concatenate: sat = [ CmasksToConcat.split("_")[0] for CmasksToConcat in masksToConcat ] if not sat.count(sat[0]) == len(sat): continue maskFilter.append(masksToConcat) return maskFilter print("concatenate") allOrtho = [] allMasks = [] imageList = [(os.path.split( currentOrtho.GetParameterValue( OtbAppBank.getInputParameterOutput(currentOrtho)))[-1].split( "?")[0], currentOrtho, _) for currentOrtho, _ in orthoList] imageList.sort() rastersToConcat = findTilesToConcatenate(imageList) #fill ortho for rasters in rastersToConcat: tmp = [] name = [] for pol in rasters: name.append(pol.replace(".tif", "")) for currentOrtho, _ in orthoList: outputParameter = OtbAppBank.getInputParameterOutput( currentOrtho) if pol in currentOrtho.GetParameterValue(outputParameter): if self.wMode == False: tmp.append((currentOrtho, _)) else: tmp.append( currentOrtho.GetParameterValue( OtbAppBank.getInputParameterOutput( currentOrtho))) name = "_".join(name) + ".tif" outputImage = os.path.join(self.outputPreProcess, tile, name + "?&writegeom=false") concatAppli = OtbAppBank.CreateBandMathApplication({ "il": tmp, "exp": "max(im1b1,im2b1)", "ram": str(self.RAMPerProcess), "pixType": "float", "out": outputImage }) allOrtho.append(concatAppli) for currentOrtho, _ in orthoList: outputParameter = OtbAppBank.getInputParameterOutput(currentOrtho) currentName = os.path.split( currentOrtho.GetParameterValue(outputParameter))[-1].split( "?")[0] if not currentName in [ currentRaster for currentRasters in rastersToConcat for currentRaster in currentRasters ]: allOrtho.append(currentOrtho) #fill masks if not maskList: return allOrtho, [] masksToConcat = findMasksToConcatenate(maskList) for mask in masksToConcat: tmp_m = [] maskName = [] for dateMask in mask: maskName.append(dateMask) for currentMask, _ in maskList: if dateMask in currentMask.GetParameterValue("out"): if self.wMode == False: tmp_m.append((currentMask, _)) else: tmp_m.append(currentMask.GetParameterValue("out")) maskName = "_".join([ elem.replace(".tif", "").replace("_BorderMask", "") for elem in maskName ]) + "_BorderMask.tif" outputImage = os.path.join(self.outputPreProcess, tile, maskName) concatAppliM = OtbAppBank.CreateBandMathApplication({ "il": tmp_m, "exp": "max(im1b1,im2b1)", "ram": str(self.RAMPerProcess), "pixType": "uint8", "out": outputImage + "?&writegeom=false" }) allMasks.append((concatAppliM, "")) for currentMask, _ in maskList: currentName = os.path.split( currentMask.GetParameterValue("out"))[-1].split("?")[0] if not currentName in [ currentRaster for currentRasters in masksToConcat for currentRaster in currentRasters ]: allMasks.append((currentMask, _)) return allOrtho, allMasks
def regularisation(raster, threshold, nbcores, path, ram = "128"): filetodelete = [] # First regularisation in connection 8, second in connection 4 init_regul = time.time() # A mask for each regularization rule # Agricultuture bandMathAppli = OtbAppBank.CreateBandMathApplication({"il": raster, "exp": '(im1b1==11 || im1b1==12)?im1b1:0', "ram": str(0.2 * float(ram)), "pixType": "uint8", "out": os.path.join(path, 'mask_1.tif')}) bandMathAppli.ExecuteAndWriteOutput() filetodelete.append(os.path.join(path, 'mask_1.tif')) # Forest bandMathAppli = OtbAppBank.CreateBandMathApplication({"il": raster, "exp": '(im1b1==31 || im1b1==32)?im1b1:0', "ram": str(0.2 * float(ram)), "pixType": "uint8", "out": os.path.join(path, 'mask_2.tif')}) bandMathAppli.ExecuteAndWriteOutput() filetodelete.append(os.path.join(path, 'mask_2.tif')) # Urban bandMathAppli = OtbAppBank.CreateBandMathApplication({"il": raster, "exp": '(im1b1==41 || im1b1==42 || im1b1==43)?im1b1:0', "ram": str(0.2 * float(ram)), "pixType": "uint8", "out": os.path.join(path, 'mask_3.tif')}) bandMathAppli.ExecuteAndWriteOutput() filetodelete.append(os.path.join(path, 'mask_3.tif')) # Open natural areas bandMathAppli = OtbAppBank.CreateBandMathApplication({"il": raster, "exp": '(im1b1==34 || im1b1==36 || im1b1==211)?im1b1:0', "ram": str(0.2 * float(ram)), "pixType": "uint8", "out": os.path.join(path, 'mask_4.tif')}) bandMathAppli.ExecuteAndWriteOutput() filetodelete.append(os.path.join(path, 'mask_4.tif')) # Bare soil bandMathAppli = OtbAppBank.CreateBandMathApplication({"il": raster, "exp": '(im1b1==45 || im1b1==46)?im1b1:0', "ram": str(0.2 * float(ram)), "pixType": "uint8", "out": os.path.join(path, 'mask_5.tif')}) bandMathAppli.ExecuteAndWriteOutput() filetodelete.append(os.path.join(path, 'mask_5.tif')) # Perennial agriculture bandMathAppli = OtbAppBank.CreateBandMathApplication({"il": raster, "exp": '(im1b1==221 || im1b1==222)?im1b1:0', "ram": str(0.2 * float(ram)), "pixType": "uint8", "out": os.path.join(path, 'mask_6.tif')}) bandMathAppli.ExecuteAndWriteOutput() filetodelete.append(os.path.join(path, 'mask_6.tif')) # Road bandMathAppli = OtbAppBank.CreateBandMathApplication({"il": raster, "exp": '(im1b1==44)?im1b1:0', "ram": str(0.2 * float(ram)), "pixType": "uint8", "out": os.path.join(path, 'mask_7.tif')}) bandMathAppli.ExecuteAndWriteOutput() filetodelete.append(os.path.join(path, 'mask_7.tif')) # Water bandMathAppli = OtbAppBank.CreateBandMathApplication({"il": raster, "exp": '(im1b1==51)?im1b1:0', "ram": str(0.2 * float(ram)), "pixType": "uint8", "out": os.path.join(path, 'mask_8.tif')}) bandMathAppli.ExecuteAndWriteOutput() filetodelete.append(os.path.join(path, 'mask_8.tif')) # Snow and glacier bandMathAppli = OtbAppBank.CreateBandMathApplication({"il": raster, "exp": '(im1b1==53)?im1b1:0', "ram": str(0.2 * float(ram)), "pixType": "uint8", "out": os.path.join(path, 'mask_9.tif')}) bandMathAppli.ExecuteAndWriteOutput() filetodelete.append(os.path.join(path, 'mask_9.tif')) for i in range(9): command = "gdalwarp -q -multi -wo NUM_THREADS=%s -dstnodata 0 %s/mask_%s.tif %s/mask_nd_%s.tif"%(nbcores, \ path, \ str(i + 1), \ path, \ str(i + 1)) Utils.run(command) filetodelete.append("%s/mask_nd_%s.tif"%(path, str(i + 1))) masktime = time.time() print(" ".join([" : ".join(["Masks generation for adaptive rules", str(masktime - init_regul)]), "seconds"])) # Two successive regularisation (8 neighbors then 4 neighbors) for i in range(2): if i == 0: connexion = 8 else : connexion = 4 # Tiles number to treat in parralel pool = Pool(processes = 6) iterable = (np.arange(6)).tolist() function = partial(gdal_sieve, threshold, connexion, path) pool.map(function, iterable) pool.close() pool.join() for j in range(6): command = "gdalwarp -q -multi -wo NUM_THREADS=%s -dstnodata 0 %s/mask_%s_%s.tif %s/mask_nd_%s_%s.tif"%(nbcores, \ path, \ str(j + 1), \ str(connexion), \ path, \ str(j + 1), \ str(connexion)) Utils.run(command) for j in range(6): os.remove(path + "/mask_%s_%s.tif"%(str(j + 1),str(connexion))) for j in range(6): os.remove(path + "/mask_nd_%s_8.tif"%(str(j + 1))) adaptativetime = time.time() print(" ".join([" : ".join(["Adaptative regularizations", str(adaptativetime - masktime)]), "seconds"])) # Fusion of rule-based regularisation rastersList = [os.path.join(path, "mask_nd_1_4.tif"), os.path.join(path, "mask_nd_2_4.tif"), os.path.join(path, "mask_nd_3_4.tif"), \ os.path.join(path, "mask_nd_4_4.tif"), os.path.join(path, "mask_nd_5_4.tif"), os.path.join(path, "mask_nd_6_4.tif"), \ os.path.join(path, "mask_nd_7.tif"), os.path.join(path, "mask_nd_8.tif"), os.path.join(path, "mask_nd_9.tif")] bandMathAppli = OtbAppBank.CreateBandMathApplication({"il": rastersList, "exp": 'im1b1+im2b1+\ im3b1+im4b1+\ im5b1+im6b1+\ im7b1+im8b1+\ im9b1', "ram": str(0.2 * float(ram)), "pixType": "uint8", "out": os.path.join(path, 'mask_regul_adapt.tif')}) bandMathAppli.ExecuteAndWriteOutput() for filemask in rastersList: os.remove(filemask) command = "gdalwarp -q -multi -wo NUM_THREADS=" command += "%s -dstnodata 0 %s/mask_regul_adapt.tif %s/mask_nd_regul_adapt.tif"%(nbcores, \ path, \ path) Utils.run(command) filetodelete.append("%s/mask_regul_adapt.tif"%(path)) # Regularisation based on majority voting # 8 neighbors command = "gdal_sieve.py -q -8 -st " command += "%s %s/mask_nd_regul_adapt.tif %s/mask_regul_adapt_0.tif" %(threshold, \ path, \ path) Utils.run(command) filetodelete.append("%s/mask_nd_regul_adapt.tif"%(path)) command = "gdalwarp -q -multi -wo NUM_THREADS=" command += "%s -dstnodata 0 %s/mask_regul_adapt_0.tif %s/mask_nd_regul_adapt_0.tif"%(nbcores, \ path, \ path) Utils.run(command) filetodelete.append("%s/mask_regul_adapt_0.tif"%(path)) # 4 neighbors command = "gdal_sieve.py -q -4 -st " command += "%s %s/mask_nd_regul_adapt_0.tif %s/regul_adapt_maj.tif" %(threshold, \ path, \ path) Utils.run(command) filetodelete.append("%s/mask_nd_regul_adapt_0.tif"%(path)) out_classif_sieve = "%s/regul_adapt_maj.tif"%(path) majoritytime = time.time() print(" ".join([" : ".join(["Majority voting regularization", str(majoritytime - adaptativetime)]), "seconds"])) for filetodel in filetodelete: if os.path.exists(filetodel): os.remove(filetodel) end_regul = time.time() - init_regul return out_classif_sieve, end_regul
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"]))