예제 #1
0
def writeOutputRaster(OTB_App,
                      overwrite=True,
                      workingDirectory=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
예제 #2
0
def getDatesInOtbOutputName(otbObj):
    
    if isinstance(otbObj,str):	
        return int(otbObj.split("/")[-1].split("_")[4].split("t")[0])
    elif type(otbObj)==otb.Application:
        outputParameter = OtbAppBank.getInputParameterOutput(otbObj)
        return int(otbObj.GetParameterValue(outputParameter).split("/")[-1].split("_")[4].split("t")[0])
예제 #3
0
    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
예제 #4
0
def SAR_floatToInt(filterApplication,
                   nb_bands,
                   RAMPerProcess,
                   outputFormat="uint16",
                   db_min=-25,
                   db_max=3):
    """transform float SAR values to integer
    """
    import math
    from Common import OtbAppBank as otbApp
    min_val = str(10.0**(db_min / 10.0))
    max_val = str(10.0**(db_max / 10.0))

    min_val_scale = 0
    max_val_scale = "((2^16)-1)"
    if outputFormat == "uint8":
        max_val_scale = "((2^8)-1)"

    #build expression
    to_db_expression = "10*log10(im1bX)"
    scale_expression = "(({}-{})/({}-{}))*({})+({}-(({}-{})*{})/({}-{}))".format(
        max_val_scale, min_val_scale, db_max, db_min, to_db_expression,
        min_val_scale, max_val_scale, min_val_scale, db_min, db_max, db_min)
    scale_max_val = (2**16) - 1
    scale_min_val = 0
    threshold_expression = "{0}>{1}?{3}:{0}<{2}?{4}:{5}".format(
        to_db_expression, db_max, db_min, scale_max_val, scale_min_val,
        scale_expression)

    expression = ";".join([
        threshold_expression.replace("X", str(i + 1)) for i in range(nb_bands)
    ])

    outputPath = filterApplication.GetParameterValue(
        otbApp.getInputParameterOutput(filterApplication))

    convert = OtbAppBank.CreateBandMathXApplication({
        "il": filterApplication,
        "out": outputPath,
        "exp": expression,
        "ram": str(RAMPerProcess),
        "pixType": outputFormat
    })
    return convert
예제 #5
0
    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