Esempio n. 1
0
def genTileEnvPrio(ObjListTile, out, tmpFile, proj):

    buff = 600  #offset in order to manage nodata in image's border

    ObjListTile.reverse()
    listSHP = [
        createRasterFootprint(c_ObjListTile.getPath(),
                              tmpFile + "/" + c_ObjListTile.getName() + ".shp")
        for c_ObjListTile in ObjListTile
    ]

    for env, currentTile in zip(listSHP, ObjListTile):
        currentTile.setEnvelope(env)
        currentTile.setPriorityEnv(env.replace(".shp", "_PRIO.shp"))
        fu.cpShapeFile(env.replace(".shp", ""),
                       env.replace(".shp", "") + "_PRIO",
                       [".prj", ".shp", ".dbf", ".shx"])

    for i in range(len(ObjListTile)):
        currentTileEnv = ObjListTile[i].getEnvelope()
        for j in range(1 + i, len(ObjListTile)):
            NextTileEnv = ObjListTile[j].getEnvelope()
            if IsIntersect(currentTileEnv, NextTileEnv):

                InterName = ObjListTile[i].getName(
                ) + "_inter_" + ObjListTile[j].getName()
                intersection = fu.ClipVectorData(ObjListTile[i].getEnvelope(), ObjListTile[j].getEnvelope(),\
                                                                 tmpFile, InterName)
                notDiag = erodeInter(ObjListTile[i], ObjListTile[j],
                                     intersection, buff, proj)
                if notDiag:
                    tmpName = ObjListTile[i].getName() + "_TMP"
                    subtractShape(ObjListTile[i].getPriorityEnv(),
                                  intersection, tmpFile, tmpName, proj)

                    fu.removeShape(ObjListTile[i].getPriorityEnv().replace(".shp", ""),\
                                                       [".prj", ".shp", ".dbf", ".shx"])
                    fu.cpShapeFile(tmpFile+"/"+tmpName.replace(".shp", ""),\
                                                       ObjListTile[i].getPriorityEnv().replace(".shp", ""),\
                                                       [".prj", ".shp", ".dbf", ".shx"])
                    fu.removeShape(tmpFile + "/" + tmpName.replace(".shp", ""),
                                   [".prj", ".shp", ".dbf", ".shx"])

    ObjListTile.reverse()
    for i in range(len(ObjListTile)):
        currentTileEnv = ObjListTile[i].getEnvelope()
        for j in range(1 + i, len(ObjListTile)):
            NextTileEnv = ObjListTile[j].getEnvelope()
            if IsIntersect(currentTileEnv, NextTileEnv):
                if diag(ObjListTile[i], ObjListTile[j]):
                    InterName = ObjListTile[i].getName(
                    ) + "_inter_" + ObjListTile[j].getName()
                    intersection = fu.ClipVectorData(ObjListTile[i].getEnvelope(), ObjListTile[j].getEnvelope(),\
                                                                         tmpFile, InterName)
                    erodeDiag(ObjListTile[i], ObjListTile[j], intersection,
                              buff, tmpFile, proj)
                else:
                    tmpName = ObjListTile[i].getName() + "_TMP"
                    subtractShape(ObjListTile[i].getPriorityEnv(), ObjListTile[j].getPriorityEnv(),\
                                                      tmpFile, tmpName, proj)

                    fu.removeShape(ObjListTile[i].getPriorityEnv().replace(".shp", ""),\
                                                       [".prj", ".shp", ".dbf", ".shx"])
                    fu.cpShapeFile(tmpFile+"/"+tmpName.replace(".shp", ""),\
                                                       ObjListTile[i].getPriorityEnv().replace(".shp", ""),\
                                                       [".prj", ".shp", ".dbf", ".shx"])
                    fu.removeShape(tmpFile + "/" + tmpName.replace(".shp", ""),
                                   [".prj", ".shp", ".dbf", ".shx"])
Esempio n. 2
0
def launchClassification(model, path_cfg, output_path: str,
                         classifier_name: str, classification_mode: str,
                         nomenclature_path: str, stat, pathToRT, pathToImg,
                         pathToRegion, fieldRegion, pathToCmdClassif, pathOut,
                         RAM, pathWd):
    """
    Parameters
    ----------
    output_path : str
        iota2 output directory

    """
    scriptPath = os.path.join(fu.get_iota2_project_dir(), "iota2")
    pixType = fu.getOutputPixType(nomenclature_path)
    AllCmd = []

    maskFiles = pathOut + "/MASK"
    if not os.path.exists(maskFiles):
        run("mkdir " + maskFiles)

    if pathToRegion is None:
        pathToRegion = os.path.join(output_path, "MyRegion.shp")

    shpRName = pathToRegion.split("/")[-1].replace(".shp", "")

    AllModel_tmp = fu.FileSearch_AND(model, True, "model", ".txt")
    AllModel = fu.fileSearchRegEx(model + "/*model*.txt")

    for currentFile in AllModel_tmp:
        if currentFile not in AllModel:
            os.remove(currentFile)

    for path in AllModel:
        model = path.split("/")[-1].split("_")[1]
        tiles = fu.getListTileFromModel(
            model, output_path + "/config_model/configModel.cfg")
        model_Mask = model
        if re.search('model_.*f.*_', path.split("/")[-1]):
            model_Mask = path.split("/")[-1].split("_")[1].split("f")[0]
        seed = path.split("/")[-1].split("_")[-1].replace(".txt", "")
        suffix = ""
        if "SAR.txt" in os.path.basename(path):
            seed = path.split("/")[-1].split("_")[-2]
            suffix = "_SAR"
        tilesToEvaluate = tiles

        #construction du string de sortie
        for tile in tilesToEvaluate:
            pathToFeat = fu.FileSearch_AND(pathToImg + "/" + tile + "/tmp/",
                                           True, "MaskCommunSL", ".tif")[0]
            maskSHP = pathToRT + "/" + shpRName + "_region_" + model_Mask + "_" + tile + ".shp"
            maskTif = shpRName + "_region_" + model_Mask + "_" + tile + ".tif"
            CmdConfidenceMap = ""
            confidenceMap_name = "{}_model_{}_confidence_seed_{}{}.tif".format(
                tile, model, seed, suffix)
            CmdConfidenceMap = " -confmap " + os.path.join(
                pathOut, confidenceMap_name)

            if not os.path.exists(maskFiles + "/" + maskTif):
                pathToMaskCommun = pathToImg + "/" + tile + "/tmp/" + "MaskCommunSL" + ".shp"
                #cas cluster
                if pathWd != None:
                    maskFiles = pathWd
                nameOut = fu.ClipVectorData(maskSHP, pathToMaskCommun,
                                            maskFiles,
                                            maskTif.replace(".tif", ""))
                cmdRaster = "otbcli_Rasterization -in "+nameOut+" -mode attribute -mode.attribute.field "+\
                        fieldRegion+" -im "+pathToFeat+" -out "+maskFiles+"/"+maskTif
                if "fusion" in classification_mode:
                    cmdRaster = "otbcli_Rasterization -in "+nameOut+" -mode binary -mode.binary.foreground 1 -im "+\
                                pathToFeat+" -out "+maskFiles+"/"+maskTif
                run(cmdRaster)
                if pathWd != None:
                    run("cp " + pathWd + "/" + maskTif + " " + pathOut +
                        "/MASK")
                    os.remove(pathWd + "/" + maskTif)

            out = pathOut + "/Classif_" + tile + "_model_" + model + "_seed_" + seed + suffix + ".tif"

            #hpc case
            if pathWd != None:
                out = "$TMPDIR/Classif_" + tile + "_model_" + model + "_seed_" + seed + suffix + ".tif"
                CmdConfidenceMap = " -confmap $TMPDIR/" + confidenceMap_name

            appli = "python " + scriptPath + "/Classification/ImageClassifier.py -conf " + path_cfg + " "
            pixType_cmd = " -pixType " + pixType
            if pathWd != None:
                pixType_cmd = pixType_cmd + " --wd $TMPDIR "
            cmd = appli + " -in " + pathToFeat + " -model " + path + " -mask " + pathOut + "/MASK/" + maskTif + " -out " + out + " " + pixType_cmd + " -ram " + str(
                RAM) + " " + CmdConfidenceMap

            # add stats if svm
            if "svm" in classifier_name.lower():
                model_statistics = os.path.join(
                    stat, "Model_{}_seed_{}.xml".format(model, seed))
                cmd = "{} -imstat {}".format(cmd, model_statistics)
            AllCmd.append(cmd)
    fu.writeCmds(pathToCmdClassif + "/class.txt", AllCmd)
    return AllCmd