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 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)