def doOrthoByTile(self, rasterList, tileName): allOrtho = [] for i in range(len(rasterList)): raster, tileOrigin = rasterList[i] manifest = raster.getManifest() calibrationApplication = rasterList[i][ 0].GetCalibrationApplication() for calib, dep in calibrationApplication: image = calib.GetParameterValue("out") currentDate = getDateFromS1Raster(image) currentPolar = getPolarFromS1Raster(image) currentPlatform = getPlatformFromS1Raster(image) currentOrbitDirection = getOrbitDirection(manifest) outUTMZone = tileName[0:2] outUTMNorthern = str(int(tileName[2] >= 'N')) workingDirectory = os.path.join(self.outputPreProcess, tileName) if not os.path.exists(workingDirectory): os.makedirs(workingDirectory) inEPSG = 4326 outEPSG = 32600 + int(outUTMZone) if not outUTMNorthern == "0": outEPSG = outEPSG + 100 if self.ManyProjection: [(x, y, dummy)] = converCoord([tileOrigin[0]], inEPSG, outEPSG) [(lrx, lry, dummy)] = converCoord([tileOrigin[2]], inEPSG, outEPSG) uniqueProj = None refRaster = None else: refRaster = FileSearch_AND( self.referencesFolder + "/T" + tileName, True, self.rasterPattern)[0] print("reference raster found : " + refRaster) if self.ManyProjection and outUTMNorthern == "1" and y > 0: y = y - 10000000. lry = lry - 10000000. if self.ManyProjection and outUTMNorthern == "0" and y < 0: y = y + 10000000. lry = lry + 10000000. orthoImageName = currentPlatform+"_"+\ tileName+"_"+\ currentPolar+"_"+\ currentOrbitDirection+"_"+\ currentDate+".tif" inputImage = (calib, dep) if self.wMode: inputImage = calib.GetParameterValue("out") orthoRaster = os.path.join(workingDirectory, orthoImageName) if self.ManyProjection: sizeX = abs(lrx - x) / self.outSpacialRes sizeY = abs(lry - y) / self.outSpacialRes ortho, ortho_dep = OtbAppBank.CreateOrthoRectification({ "in": inputImage, "out": orthoRaster, "ram": self.RAMPerProcess, "outputs.spacingx": self.outSpacialRes, "outputs.spacingy": -self.outSpacialRes, "outputs.sizex": sizeX, "outputs.sizey": sizeY, "opt.gridspacing": self.gridSpacing, "map.utm.zone": outUTMZone, "map.utm.northhem": outUTMNorthern, "outputs.ulx": x, "outputs.uly": y, "elev.dem": self.SRTM, "elev.geoid": self.geoid, "map": "utm" }) else: ortho, ortho_dep = OtbAppBank.CreateSuperimposeApplication( { "inr": refRaster, "inm": inputImage, "pixType": "float", "interpolator": "bco", "ram": self.RAMPerProcess, "io.out": orthoRaster, "elev.dem": self.SRTM, "elev.geoid": self.geoid }) allOrtho.append((ortho, ortho_dep)) return allOrtho
def coregister(insrc, inref, band, bandref, resample=1, step=256, minstep=16, minsiftpoints=40, iterate=1, prec=3, mode=2, datadir=None, pattern='*STACK.tif', datatype='S2', writeFeatures=False, workingDirectory=None): """ register an image / a time series on a reference image Parameters ---------- insrc : string source raster inref : string reference raster band : int band number for the source raster bandref : int band number for the raster reference raster resample : boolean resample to reference raster resolution step : int initial step between the geobins minstep : int minimal step between the geobins when iterates minsiftpoints : int minimal number of sift points to perform the registration iterate : boolean argument to iterate with smaller geobin step to find more sift points prec : int precision between the source and reference image (in source pixel unit) mode : int registration mode, 1 : simple registration ; 2 : time series registration ; 3 : time series cascade registration (to do) datadir : string path to the data directory pattern : string pattern of the STACK files to register writeFeatures : boolean argument to keep temporary files Note ------ This function use the OTB's application **OrthoRectification** and **SuperImpose** more documentation for `OrthoRectification <https://www.orfeo-toolbox.org/Applications/OrthoRectification.html>`_ and `SuperImpose <https://www.orfeo-toolbox.org/Applications/Superimpose.html>`_ """ from Common.FileUtils import ensure_dir pathWd = os.path.dirname( insrc) if not workingDirectory else workingDirectory if os.path.exists(pathWd) == False: ensure_dir(pathWd) srcClip = os.path.join(pathWd, 'tempSrcClip.tif') extractROIApp = OtbAppBank.CreateExtractROIApplication({ "in": insrc, "mode": "fit", "mode.fit.im": inref, "out": srcClip, "pixType": "uint16" }) extractROIApp.ExecuteAndWriteOutput() # #SensorModel generation SensorModel = os.path.join(pathWd, 'SensorModel.geom') PMCMApp = OtbAppBank.CreatePointMatchCoregistrationModel({ "in": srcClip, "band1": band, "inref": inref, "bandref": bandref, "resample": resample, "precision": str(prec), "mfilter": "1", "backmatching": "1", "outgeom": SensorModel, "initgeobinstep": str(step), "mingeobinstep": str(minstep), "minsiftpoints": str(minsiftpoints), "iterate": iterate }) PMCMApp.ExecuteAndWriteOutput() # mode 1 : application on the source image if mode == 1 or mode == 3: outSrc = os.path.join(pathWd, 'temp_file.tif') io_Src = str(srcClip + '?&skipcarto=true&geom=' + SensorModel) ds = gdal.Open(srcClip) prj = ds.GetProjection() gt = ds.GetGeoTransform() srs = osr.SpatialReference() srs.ImportFromWkt(prj) code = srs.GetAuthorityCode(None) gsp = str(int(2 * round(max(abs(gt[1]), abs(gt[5]))))) ds = None orthoRecApp = OtbAppBank.CreateOrthoRectification({ "in": io_Src, "io.out": outSrc, "map": "epsg", "map.epsg.code": code, "opt.gridspacing": gsp, "pixType": "uint16" }) if writeFeatures: orthoRecApp[0].ExecuteAndWriteOutput() else: orthoRecApp[0].Execute() ext = os.path.splitext(insrc)[1] finalOutput = os.path.join( pathWd, os.path.basename(insrc.replace(ext, ext.replace('.', '_COREG.')))) superImposeApp = OtbAppBank.CreateSuperimposeApplication({ "inr": srcClip, "inm": orthoRecApp[0], "out": finalOutput, "pixType": "uint16" }) superImposeApp[0].ExecuteAndWriteOutput() shutil.move(finalOutput, insrc.replace(ext, ext.replace('.', '_COREG.'))) shutil.move(finalOutput.replace(ext, '.geom'), insrc.replace(ext, '_COREG.geom')) # Mask registration if exists masks = glob.glob( os.path.dirname(insrc) + os.sep + 'MASKS' + os.sep + '*BINARY_MASK' + ext) if len(masks) != 0: for mask in masks: srcClip = os.path.join(pathWd, 'tempSrcClip.tif') extractROIApp = OtbAppBank.CreateExtractROIApplication({ "in": mask, "mode": "fit", "mode.fit.im": inref, "out": srcClip, "pixType": "uint16" }) extractROIApp.ExecuteAndWriteOutput() outSrc = os.path.join(pathWd, 'temp_file.tif') io_Src = str(mask + '?&skipcarto=true&geom=' + SensorModel) orthoRecApp = OtbAppBank.CreateOrthoRectification({ "in": io_Src, "io.out": outSrc, "map": "epsg", "map.epsg.code": code, "opt.gridspacing": gsp, "pixType": "uint16" }) if writeFeatures: orthoRecApp[0].ExecuteAndWriteOutput() else: orthoRecApp[0].Execute() ext = os.path.splitext(insrc)[1] finalMask = os.path.join( pathWd, os.path.basename( mask.replace(ext, ext.replace('.', '_COREG.')))) superImposeApp = OtbAppBank.CreateSuperimposeApplication({ "inr": mask, "inm": orthoRecApp[0], "out": finalMask, "pixType": "uint16" }) superImposeApp[0].ExecuteAndWriteOutput() if finalMask != mask.replace(ext, ext.replace('.', '_COREG.')): shutil.move(finalMask, mask.replace(ext, ext.replace('.', '_COREG.'))) shutil.move(finalMask.replace(ext, '.geom'), mask.replace(ext, '_COREG.geom')) if mode == 3: folders = glob.glob(os.path.join(datadir, '*')) vhr_ref = inref if datatype in ['S2', 'S2_S2C']: dates = [ os.path.basename(fld).split('_')[1].split("-")[0] for fld in folders ] ref_date = os.path.basename(insrc).split('_')[1].split("-")[0] elif datatype in ['L5', 'L8']: dates = [ os.path.basename(fld).split('_')[3] for fld in folders ] ref_date = os.path.basename(insrc).split('_')[3] dates.sort() ref_date_ind = dates.index(ref_date) bandref = band clean_dates = [ref_date] for date in reversed(dates[:ref_date_ind]): inref = glob.glob( os.path.join(datadir, '*' + clean_dates[-1] + '*', pattern))[0] insrc = glob.glob( os.path.join(datadir, '*' + date + '*', pattern))[0] srcClip = os.path.join(pathWd, 'srcClip.tif') extractROIApp = OtbAppBank.CreateExtractROIApplication({ "in": insrc, "mode": "fit", "mode.fit.im": inref, "out": srcClip, "pixType": "uint16" }) extractROIApp.ExecuteAndWriteOutput() outSensorModel = os.path.join(pathWd, 'SensorModel_%s.geom' % date) try: PMCMApp = OtbAppBank.CreatePointMatchCoregistrationModel({ "in": srcClip, "band1": band, "inref": inref, "bandref": bandref, "resample": resample, "precision": str(prec), "mfilter": "1", "backmatching": "1", "outgeom": outSensorModel, "initgeobinstep": str(step), "mingeobinstep": str(minstep), "minsiftpoints": str(minsiftpoints), "iterate": iterate }) PMCMApp.ExecuteAndWriteOutput() except RuntimeError: shutil.copy(SensorModel, outSensorModel) logger.warning( 'Coregistration failed, %s will be process with %s' % (insrc, outSensorModel)) continue outSrc = os.path.join(pathWd, 'temp_file.tif') io_Src = str(srcClip + '?&skipcarto=true&geom=' + outSensorModel) ds = gdal.Open(srcClip) prj = ds.GetProjection() gt = ds.GetGeoTransform() srs = osr.SpatialReference() srs.ImportFromWkt(prj) code = srs.GetAuthorityCode(None) gsp = str(int(2 * round(max(abs(gt[1]), abs(gt[5]))))) ds = None try: orthoRecApp = OtbAppBank.CreateOrthoRectification({ "in": io_Src, "io.out": outSrc, "map": "epsg", "map.epsg.code": code, "opt.gridspacing": gsp, "pixType": "uint16" }) if writeFeatures: orthoRecApp[0].ExecuteAndWriteOutput() else: orthoRecApp[0].Execute() except RuntimeError: os.remove(outSensorModel) shutil.copy(SensorModel, outSensorModel) logger.warning( 'Coregistration failed, %s will be process with %s' % (insrc, outSensorModel)) orthoRecApp = OtbAppBank.CreateOrthoRectification({ "in": io_Src, "io.out": outSrc, "map": "epsg", "map.epsg.code": code, "opt.gridspacing": gsp, "pixType": "uint16" }) continue if writeFeatures: orthoRecApp[0].ExecuteAndWriteOutput() else: orthoRecApp[0].Execute() ext = os.path.splitext(insrc)[1] finalOutput = os.path.join( pathWd, os.path.basename( insrc.replace(ext, ext.replace('.', '_COREG.')))) superImposeApp = OtbAppBank.CreateSuperimposeApplication({ "inr": srcClip, "inm": orthoRecApp[0], "out": finalOutput, "pixType": "uint16" }) superImposeApp[0].ExecuteAndWriteOutput() shutil.move(finalOutput, insrc.replace(ext, ext.replace('.', '_COREG.'))) shutil.move(finalOutput.replace(ext, '.geom'), insrc.replace(ext, '_COREG.geom')) # Mask registration if exists masks = glob.glob( os.path.dirname(insrc) + os.sep + 'MASKS' + os.sep + '*BINARY_MASK' + ext) if len(masks) != 0: for mask in masks: srcClip = os.path.join(pathWd, 'srcClip.tif') extractROIApp = OtbAppBank.CreateExtractROIApplication( { "in": mask, "mode": "fit", "mode.fit.im": inref, "out": srcClip, "pixType": "uint16" }) extractROIApp.ExecuteAndWriteOutput() outSrc = os.path.join(pathWd, 'temp_file.tif') io_Src = str(srcClip + '?&skipcarto=true&geom=' + outSensorModel) orthoRecApp = OtbAppBank.CreateOrthoRectification({ "in": io_Src, "io.out": outSrc, "map": "epsg", "map.epsg.code": code, "opt.gridspacing": gsp, "pixType": "uint16" }) if writeFeatures: orthoRecApp[0].ExecuteAndWriteOutput() else: orthoRecApp[0].Execute() ext = os.path.splitext(insrc)[1] finalMask = os.path.join( pathWd, os.path.basename( mask.replace(ext, ext.replace('.', '_COREG.')))) superImposeApp = OtbAppBank.CreateSuperimposeApplication( { "inr": srcClip, "inm": orthoRecApp[0], "out": finalMask, "pixType": "uint16" }) superImposeApp[0].ExecuteAndWriteOutput() shutil.move( finalMask, mask.replace(ext, ext.replace('.', '_COREG.'))) shutil.move(finalMask.replace(ext, '.geom'), mask.replace(ext, '_COREG.geom')) if not writeFeatures and os.path.exists(outSensorModel): os.remove(outSensorModel) if datatype in ['S2', 'S2_S2C']: mtd_file = glob.glob( os.path.join(os.path.dirname(insrc), '*_MTD_ALL*'))[0] cloud_clear = get_S2_Tile_Cloud_Cover(mtd_file) cover = get_S2_Tile_Coverage(mtd_file) if cloud_clear > 0.6 and cover > 0.8: clean_dates.append(date) elif datatype in ['L5', 'L8']: mlt_file = glob.glob( os.path.join(os.path.dirname(insrc), '*_MTL*'))[0] cloud_clear = get_L8_Tile_Cloud_Cover(mlt_file) if cloud_clear > 0.6: clean_dates.append(date) clean_dates = [ref_date] for date in dates[ref_date_ind + 1:]: inref = glob.glob( os.path.join(datadir, '*' + clean_dates[-1] + '*', pattern))[0] insrc = glob.glob( os.path.join(datadir, '*' + date + '*', pattern))[0] srcClip = os.path.join(pathWd, 'srcClip.tif') extractROIApp = OtbAppBank.CreateExtractROIApplication({ "in": insrc, "mode": "fit", "mode.fit.im": inref, "out": srcClip, "pixType": "uint16" }) extractROIApp.ExecuteAndWriteOutput() outSensorModel = os.path.join(pathWd, 'SensorModel_%s.geom' % date) try: PMCMApp = OtbAppBank.CreatePointMatchCoregistrationModel({ "in": srcClip, "band1": band, "inref": inref, "bandref": bandref, "resample": resample, "precision": str(prec), "mfilter": "1", "backmatching": "1", "outgeom": outSensorModel, "initgeobinstep": str(step), "mingeobinstep": str(minstep), "minsiftpoints": str(minsiftpoints), "iterate": iterate }) PMCMApp.ExecuteAndWriteOutput() except RuntimeError: shutil.copy(SensorModel, outSensorModel) logger.warning( 'Coregistration failed, %s will be process with %s' % (insrc, outSensorModel)) continue outSrc = os.path.join(pathWd, 'temp_file.tif') io_Src = str(srcClip + '?&skipcarto=true&geom=' + outSensorModel) ds = gdal.Open(srcClip) prj = ds.GetProjection() gt = ds.GetGeoTransform() srs = osr.SpatialReference() srs.ImportFromWkt(prj) code = srs.GetAuthorityCode(None) gsp = str(int(2 * round(max(abs(gt[1]), abs(gt[5]))))) ds = None try: orthoRecApp = OtbAppBank.CreateOrthoRectification({ "in": io_Src, "io.out": outSrc, "map": "epsg", "map.epsg.code": code, "opt.gridspacing": gsp, "pixType": "uint16" }) if writeFeatures: orthoRecApp[0].ExecuteAndWriteOutput() else: orthoRecApp[0].Execute() except RuntimeError: os.remove(outSensorModel) shutil.copy(SensorModel, outSensorModel) orthoRecApp = OtbAppBank.CreateOrthoRectification({ "in": io_Src, "io.out": outSrc, "map": "epsg", "map.epsg.code": code, "opt.gridspacing": gsp, "pixType": "uint16" }) continue if writeFeatures: orthoRecApp[0].ExecuteAndWriteOutput() else: orthoRecApp[0].Execute() ext = os.path.splitext(insrc)[1] finalOutput = os.path.join( pathWd, os.path.basename( insrc.replace(ext, ext.replace('.', '_COREG.')))) superImposeApp = OtbAppBank.CreateSuperimposeApplication({ "inr": srcClip, "inm": orthoRecApp[0], "out": finalOutput, "pixType": "uint16" }) superImposeApp[0].ExecuteAndWriteOutput() shutil.move(finalOutput, insrc.replace(ext, ext.replace('.', '_COREG.'))) shutil.move(finalOutput.replace(ext, '.geom'), insrc.replace(ext, '_COREG.geom')) # Mask registration if exists masks = glob.glob( os.path.dirname(insrc) + os.sep + 'MASKS' + os.sep + '*BINARY_MASK' + ext) if len(masks) != 0: for mask in masks: srcClip = os.path.join(pathWd, 'srcClip.tif') extractROIApp = OtbAppBank.CreateExtractROIApplication( { "in": mask, "mode": "fit", "mode.fit.im": inref, "out": srcClip, "pixType": "uint16" }) extractROIApp.ExecuteAndWriteOutput() outSrc = os.path.join(pathWd, 'temp_file.tif') io_Src = str(srcClip + '?&skipcarto=true&geom=' + outSensorModel) orthoRecApp = OtbAppBank.CreateOrthoRectification({ "in": io_Src, "io.out": outSrc, "map": "epsg", "map.epsg.code": code, "opt.gridspacing": gsp, "pixType": "uint16" }) if writeFeatures: orthoRecApp[0].ExecuteAndWriteOutput() else: orthoRecApp[0].Execute() ext = os.path.splitext(insrc)[1] finalMask = os.path.join( pathWd, os.basename( mask.replace(ext, ext.replace('.', '_COREG.')))) superImposeApp = OtbAppBank.CreateSuperimposeApplication( { "inr": srcClip, "inm": orthoRecApp[0], "out": finalMask, "pixType": "uint16" }) superImposeApp[0].ExecuteAndWriteOutput() shutil.move( finalMask, mask.replace(ext, ext.replace('.', '_COREG.'))) shutil.move(finalMask.replace(ext, '.geom'), mask.replace(ext, '_COREG.geom')) if writeFeatures == False and os.path.exists(outSensorModel): os.remove(outSensorModel) if datatype in ['S2', 'S2_S2C']: mtd_file = glob.glob( os.path.join(os.path.dirname(insrc), '*_MTD_ALL*'))[0] cloud_clear = get_S2_Tile_Cloud_Cover(mtd_file) cover = get_S2_Tile_Coverage(mtd_file) if cloud_clear > 0.6 and cover > 0.8: clean_dates.append(date) elif datatype in ['L5', 'L8']: mlt_file = glob.glob( os.path.join(os.path.dirname(insrc), '*_MTL*'))[0] cloud_clear = get_L8_Tile_Cloud_Cover(mlt_file) if cloud_clear > 0.6: clean_dates.append(date) if not writeFeatures and os.path.exists(SensorModel): os.remove(SensorModel) # mode 2 : application on the time series elif mode == 2: ext = os.path.splitext(insrc)[1] file_list = glob.glob(datadir + os.sep + '*' + os.sep + pattern) for insrc in file_list: srcClip = os.path.join(pathWd, 'tempSrcClip.tif') extractROIApp = OtbAppBank.CreateExtractROIApplication({ "in": insrc, "mode": "fit", "mode.fit.im": inref, "out": srcClip, "pixType": "uint16" }) extractROIApp.ExecuteAndWriteOutput() outSrc = os.path.join(pathWd, 'temp_file.tif') io_Src = str(srcClip + '?&skipcarto=true&geom=' + SensorModel) ds = gdal.Open(srcClip) prj = ds.GetProjection() gt = ds.GetGeoTransform() srs = osr.SpatialReference() srs.ImportFromWkt(prj) code = srs.GetAuthorityCode(None) gsp = str(int(2 * round(max(abs(gt[1]), abs(gt[5]))))) ds = None orthoRecApp = OtbAppBank.CreateOrthoRectification({ "in": io_Src, "io.out": outSrc, "map": "epsg", "map.epsg.code": code, "opt.gridspacing": gsp, "pixType": "uint16" }) if writeFeatures: orthoRecApp[0].ExecuteAndWriteOutput() else: orthoRecApp[0].Execute() ext = os.path.splitext(insrc)[1] finalOutput = os.path.join( pathWd, os.path.basename( insrc.replace(ext, ext.replace('.', '_COREG.')))) superImposeApp = OtbAppBank.CreateSuperimposeApplication({ "inr": srcClip, "inm": orthoRecApp[0], "out": finalOutput, "pixType": "uint16" }) superImposeApp[0].ExecuteAndWriteOutput() shutil.move(finalOutput, insrc.replace(ext, ext.replace('.', '_COREG.'))) shutil.move(finalOutput.replace(ext, '.geom'), insrc.replace(ext, '_COREG.geom')) # Mask registration if exists masks = glob.glob( os.path.dirname(insrc) + os.sep + 'MASKS' + os.sep + '*BINARY_MASK*' + ext) if len(masks) != 0: for mask in masks: srcClip = os.path.join(pathWd, 'tempSrcClip.tif') extractROIApp = OtbAppBank.CreateExtractROIApplication({ "in": mask, "mode": "fit", "mode.fit.im": inref, "out": srcClip, "pixType": "uint16" }) extractROIApp.ExecuteAndWriteOutput() outSrc = os.path.join(pathWd, 'temp_file.tif') io_Src = str(srcClip + '?&skipcarto=true&geom=' + SensorModel) orthoRecApp = OtbAppBank.CreateOrthoRectification({ "in": io_Src, "io.out": outSrc, "map": "epsg", "map.epsg.code": code, "opt.gridspacing": gsp, "pixType": "uint16" }) if writeFeatures: orthoRecApp[0].ExecuteAndWriteOutput() else: orthoRecApp[0].Execute() ext = os.path.splitext(insrc)[1] finalMask = os.path.join( pathWd, os.path.basename( mask.replace(ext, ext.replace('.', '_COREG.')))) superImposeApp = OtbAppBank.CreateSuperimposeApplication({ "inr": srcClip, "inm": orthoRecApp[0], "out": finalMask, "pixType": "uint16" }) superImposeApp[0].ExecuteAndWriteOutput() shutil.move(finalMask, mask.replace(ext, ext.replace('.', '_COREG.'))) shutil.move(finalMask.replace(ext, '.geom'), mask.replace(ext, '_COREG.geom')) os.remove(srcClip) if not writeFeatures and os.path.exists(SensorModel): os.remove(SensorModel) return None