Exemplo n.º 1
0
    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
Exemplo n.º 2
0
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)