Ejemplo n.º 1
0
def sampleSelection(path,
                    raster,
                    vecteur,
                    field,
                    ram='128',
                    split=None,
                    mask=None):

    timeinit = time.time()

    # polygon class stats (stats.xml)
    outxml = os.path.join(path, 'stats' + str(split) + '.xml')
    otbParams = {
        'in': raster,
        'vec': vecteur,
        'field': field,
        'out': outxml,
        'ram': ram
    }
    statsApp = OtbAppBank.CreatePolygonClassStatisticsApplication(otbParams)
    statsApp.ExecuteAndWriteOutput()

    shutil.copy(os.path.join(path, 'stats.xml'),
                '/work/OT/theia/oso/vincent/vectorisation/')

    timestats = time.time()
    print " ".join([
        " : ".join(["Stats calculation",
                    str(timestats - timeinit)]), "seconds"
    ])
    if mask is not None:
        mask = maskSampleSelection(path, raster, mask, ram)
    else:
        mask = None

    # Sample selection
    outsqlite = os.path.join(path, 'sample_selection' + str(split) + '.sqlite')
    if mask is None:
        otbParams = {'in':raster, 'vec':vecteur, 'field':field, 'instats': outxml, \
                     'out':outsqlite, 'ram':ram, 'strategy':'all', 'sampler':'random'}
    else:
        otbParams = {'in':raster, 'vec':vecteur, 'field':field, 'instats': outxml, \
                     'out':outsqlite, 'mask':mask, 'ram':ram, 'strategy':'all', 'sampler':'random'}
    sampleApp = OtbAppBank.CreateSampleSelectionApplication(otbParams)
    sampleApp.ExecuteAndWriteOutput()

    shutil.copy(os.path.join(path, outsqlite),
                '/work/OT/theia/oso/vincent/vectorisation/')

    timesample = time.time()
    print " ".join([
        " : ".join(["Sample selection",
                    str(timesample - timestats)]), "seconds"
    ])

    return outsqlite
Ejemplo n.º 2
0
def extractStats(vectorIn, pathConf, wD=None):

    dataField = Config(open(pathConf)).chain.dataField
    iota2Folder = Config(open(pathConf)).chain.outputPath

    tileToCompute = vectorIn.split("/")[-1].split("_")[0]
    modelToCompute = vectorIn.split("/")[-1].split("_")[2].split("f")[0]
    seed = vectorIn.split("/")[-1].split("_")[3].replace("seed", "")
    workingDirectory = iota2Folder + "/final/TMP"
    shapeMode = vectorIn.split("/")[-1].split("_")[-1].split(".")[
        0]  #'learn' or 'val'
    if wD:
        workingDirectory = wD

    try:
        refImg = fut.FileSearch_AND(iota2Folder + "/final/TMP", True,
                                    tileToCompute, ".tif")[0]
    except:
        raise Exception("reference image can not be found in " + iota2Folder +
                        "/final/TMP")

    statsFile = workingDirectory + "/" + tileToCompute + "_stats_model_" + modelToCompute + ".xml"
    stats = otbApp.CreatePolygonClassStatisticsApplication({"in":refImg, "vec":vectorIn,\
                                                            "out":statsFile, "field":dataField})
    stats.ExecuteAndWriteOutput()

    selVector = workingDirectory + "/" + tileToCompute + "_selection_model_" + modelToCompute + ".sqlite"
    sampleS = otbApp.CreateSampleSelectionApplication({"in":refImg, "vec":vectorIn, "out":selVector,\
                                                       "instats":statsFile, "strategy":"all",\
                                                       "field":dataField})
    sampleS.ExecuteAndWriteOutput()

    classificationRaster = fut.FileSearch_AND(
        iota2Folder + "/final/TMP", True,
        tileToCompute + "_seed_" + seed + ".tif")[0]
    validity = fut.FileSearch_AND(iota2Folder + "/final/TMP", True,
                                  tileToCompute + "_Cloud.tif")[0]
    confiance = fut.FileSearch_AND(
        iota2Folder + "/final/TMP", True,
        tileToCompute + "_GlobalConfidence_seed_" + seed + ".tif")[0]

    stack = [classificationRaster, validity, confiance]
    dataStack = otbApp.CreateConcatenateImagesApplication({
        "il": stack,
        "ram": '1000',
        "pixType": "uint8",
        "out": ""
    })
    dataStack.Execute()

    outSampleExtraction = workingDirectory + "/" + tileToCompute + "_extraction_model_" + modelToCompute + "_" + shapeMode + ".sqlite"

    extraction = otbApp.CreateSampleExtractionApplication({"in":dataStack, "vec":selVector,\
                                                           "field":dataField, " out":outSampleExtraction,\
                                                           "outfield":"list",\
                                                           "outfield.list.names":["predictedClass", "validity", "confidence"]})
    extraction.ExecuteAndWriteOutput()

    conn = lite.connect(outSampleExtraction)
    cursor = conn.cursor()
    SQL = "alter table output add column TILE TEXT"
    cursor.execute(SQL)
    SQL = "update output set TILE='" + tileToCompute + "'"
    cursor.execute(SQL)

    SQL = "alter table output add column MODEL TEXT"
    cursor.execute(SQL)
    SQL = "update output set MODEL='" + modelToCompute + "'"
    cursor.execute(SQL)

    conn.commit()

    os.remove(statsFile)
    os.remove(selVector)
    if wD:
        shutil.copy(outSampleExtraction, iota2Folder + "/final/TMP")