def unionPolygonsWithOGR(filenames, outputDirectory):
    """
    Build up the union of all the geometries of the given masks.

    Keyword arguments:
        filenames -- list of masks filenames
    """
    outputFilename = os.path.join(outputDirectory, "vectorMerged.shp")
    indexClass=0
    for f in filenames:
        base = os.path.basename(os.path.splitext(f)[0])
        #Add class
        # command = u'ogrinfo {} -sql "ALTER TABLE {} ADD COLUMN Class numeric(15)"'.format(f, base)
        command = get_osgeo_command("ogrinfo", [f, "-sql", "ALTER TABLE {} ADD COLUMN Class numeric(15)".format(base)])
        TerreImageProcess().run_process(command)
        # command = u'ogrinfo {} -dialect SQLite -sql "UPDATE {} SET Class = {}"'.format(f, base, indexClass)
        command = get_osgeo_command("ogrinfo", [f, "-dialect", "SQLite", "-sql",
                                                "UPDATE {} SET Class = {}".format(base, indexClass)])
        TerreImageProcess().run_process(command)
        #Add Label
        # command = u'ogrinfo {} -sql "ALTER TABLE {} ADD COLUMN Label character(15)"'.format(f, base)
        command = get_osgeo_command("ogrinfo", [f, "-sql", "ALTER TABLE {} ADD COLUMN Label character(15)".format(base)])
        TerreImageProcess().run_process(command)
        # command = u'ogrinfo {} -dialect SQLite -sql "UPDATE {} SET Label = \'{}\'"'.format(f, base, base)
        command = get_osgeo_command("ogrinfo", [f, "-dialect", "SQLite", "-sql",
                                                "UPDATE {} SET Label = \'{}\'".format(base, base)])
        TerreImageProcess().run_process(command)
        #update output
        # command = u'ogr2ogr -update -append {} {}'.format(outputFilename, f)
        command = get_osgeo_command("ogr2ogr", ["-update", "-append", outputFilename, f])
        TerreImageProcess().run_process(command)
        indexClass+=1

    return outputFilename
def ReprojectVector(inputVectorFileName,  inputImageFileName, epsg_code, output_directory):
    """
    Reprojects the given vector in the coordinate system of the given image
    Args:
        inputVectorFileName:
        inputImageFileName:
        tmpReprojectedVector:

    Returns:

    """

    # use of .qpj instead of incomplete .prj
    if os.path.isfile(os.path.splitext(inputVectorFileName)[0] + ".qpj"):
        shutil.copy(os.path.splitext(inputVectorFileName)[0] + ".qpj", os.path.splitext(inputVectorFileName)[0] + ".prj")
    # test authority code availability
    epsg_vector = get_vector_epsg_with_ogr(inputVectorFileName)
    if not epsg_vector:
        logger.warning("Bad projection !")
        #raise
        # TODO add exception

    tmpReprojectedVector = os.path.join(output_directory, "tmp_reprojected.shp")
    # command = "otbcli_VectorDataReprojection -in.vd {} -out.proj.image.in {} -out.vd {}".format(inputVectorFileName,
    #                                                                                             inputImageFileName,
    #                                                                                             tmpReprojectedVector)
    #command = "ogr2ogr -t_srs {} -s_srs {} {} {}".format(epsg_code, None, tmpReprojectedVector, inputVectorFileName)
    #command = u"ogr2ogr -t_srs EPSG:{} {} {}".format(epsg_code, tmpReprojectedVector, inputVectorFileName)
    command = get_osgeo_command("ogr2ogr", ["-t_srs", "EPSG:{}".format(epsg_code),
                                            tmpReprojectedVector, inputVectorFileName])

    TerreImageProcess().run_process(command)
    return tmpReprojectedVector
Exemple #3
0
def unionPolygonsWithOGR(filenames, outputDirectory):
    """
    Build up the union of all the geometries of the given masks.

    Keyword arguments:
        filenames -- list of masks filenames
    """
    outputFilename = os.path.join(outputDirectory, "vectorMerged.shp")
    indexClass = 0
    for f in filenames:
        base = os.path.basename(os.path.splitext(f)[0])
        #Add class
        # command = u'ogrinfo {} -sql "ALTER TABLE {} ADD COLUMN Class numeric(15)"'.format(f, base)
        command = get_osgeo_command("ogrinfo", [
            f, "-sql",
            "ALTER TABLE {} ADD COLUMN Class numeric(15)".format(base)
        ])
        TerreImageProcess().run_process(command)
        # command = u'ogrinfo {} -dialect SQLite -sql "UPDATE {} SET Class = {}"'.format(f, base, indexClass)
        command = get_osgeo_command("ogrinfo", [
            f, "-dialect", "SQLite", "-sql", "UPDATE {} SET Class = {}".format(
                base, indexClass)
        ])
        TerreImageProcess().run_process(command)
        #Add Label
        # command = u'ogrinfo {} -sql "ALTER TABLE {} ADD COLUMN Label character(15)"'.format(f, base)
        command = get_osgeo_command("ogrinfo", [
            f, "-sql",
            "ALTER TABLE {} ADD COLUMN Label character(15)".format(base)
        ])
        TerreImageProcess().run_process(command)
        # command = u'ogrinfo {} -dialect SQLite -sql "UPDATE {} SET Label = \'{}\'"'.format(f, base, base)
        command = get_osgeo_command("ogrinfo", [
            f, "-dialect", "SQLite", "-sql",
            "UPDATE {} SET Label = \'{}\'".format(base, base)
        ])
        TerreImageProcess().run_process(command)
        #update output
        # command = u'ogr2ogr -update -append {} {}'.format(outputFilename, f)
        command = get_osgeo_command("ogr2ogr",
                                    ["-update", "-append", outputFilename, f])
        TerreImageProcess().run_process(command)
        indexClass += 1

    return outputFilename
def gdal_edit_remove_no_data(image_in):
    """
    Runs gdal_edit to remove the no data value of the given image
    Args:
        image_in:

    Returns:

    """
    #command = u"gdal_edit.py -a_nodata None {}".format(image_in)
    command = get_osgeo_command("gdal_edit.py", ["-a_nodata", "None", image_in])
    TerreImageProcess().run_process(command)
def compute_overviews(filename):
    """
    Runs gdaladdo on the given filename
    """
    if not os.path.isfile(filename + ".ovr"):
        # command = "gdaladdo "
        # command += " -ro "
        # command += "\"" + filename + "\""
        # command += " 2 4 8 16"

        command = get_osgeo_command("gdaladdo", ["-ro", filename, "2 4 8 16"])
        logger.debug("command to run" + command)
        TerreImageProcess().run_process(command)
Exemple #6
0
def gdal_edit_remove_no_data(image_in):
    """
    Runs gdal_edit to remove the no data value of the given image
    Args:
        image_in:

    Returns:

    """
    #command = u"gdal_edit.py -a_nodata None {}".format(image_in)
    command = get_osgeo_command("gdal_edit.py",
                                ["-a_nodata", "None", image_in])
    TerreImageProcess().run_process(command)
Exemple #7
0
def compute_overviews(filename):
    """
    Runs gdaladdo on the given filename
    """
    if not os.path.isfile(filename + ".ovr"):
        # command = "gdaladdo "
        # command += " -ro "
        # command += "\"" + filename + "\""
        # command += " 2 4 8 16"

        command = get_osgeo_command("gdaladdo", ["-ro", filename, "2 4 8 16"])
        logger.debug("command to run" + command)
        TerreImageProcess().run_process(command)
def gdal_translate_remove_no_data(image_in, image_out):
    """
    Runs gdal_translate to remove the no data value of the given image

    Args:
        image_in:
        image_out:

    Returns:

    """
    #command = u"gdal_translate -a_nodata None {} {}".format(image_in, image_out)

    command = get_osgeo_command("gdal_translate", ["-a_nodata", "None", image_in, image_out])
    TerreImageProcess().run_process(command)
Exemple #9
0
def gdal_translate_remove_no_data(image_in, image_out):
    """
    Runs gdal_translate to remove the no data value of the given image

    Args:
        image_in:
        image_out:

    Returns:

    """
    #command = u"gdal_translate -a_nodata None {} {}".format(image_in, image_out)

    command = get_osgeo_command("gdal_translate",
                                ["-a_nodata", "None", image_in, image_out])
    TerreImageProcess().run_process(command)
def IntersectLayers(tmpReprojectedVector, outputImageEnvelopeVector, output_directory):
    """
    Produces the intersection between tmpReprojectedVector and outputImageEnvelopeVector
    Args:
        tmpReprojectedVector:
        outputImageEnvelopeVector:
        outputVectorFileName:

    Returns:

    """
    outputVectorFileName = os.path.join(output_directory, "preprocessed.shp")
    # commandOGR = u'ogr2ogr -f "ESRI Shapefile" -clipsrc {} {} {}'.format(outputImageEnvelopeVector,
    #                                                                     outputVectorFileName,
    #                                                                     tmpReprojectedVector)
    commandOGR = get_osgeo_command("ogr2ogr", ["-f", "ESRI Shapefile", "-clipsrc", outputImageEnvelopeVector,
                                            outputVectorFileName, tmpReprojectedVector])
    TerreImageProcess().run_process(commandOGR)
    return outputVectorFileName
def IntersectLayers(tmpReprojectedVector, outputImageEnvelopeVector,
                    output_directory):
    """
    Produces the intersection between tmpReprojectedVector and outputImageEnvelopeVector
    Args:
        tmpReprojectedVector:
        outputImageEnvelopeVector:
        outputVectorFileName:

    Returns:

    """
    outputVectorFileName = os.path.join(output_directory, "preprocessed.shp")
    # commandOGR = u'ogr2ogr -f "ESRI Shapefile" -clipsrc {} {} {}'.format(outputImageEnvelopeVector,
    #                                                                     outputVectorFileName,
    #                                                                     tmpReprojectedVector)
    commandOGR = get_osgeo_command("ogr2ogr", [
        "-f", "ESRI Shapefile", "-clipsrc", outputImageEnvelopeVector,
        outputVectorFileName, tmpReprojectedVector
    ])
    TerreImageProcess().run_process(commandOGR)
    return outputVectorFileName
def ReprojectVector(inputVectorFileName, inputImageFileName, epsg_code,
                    output_directory):
    """
    Reprojects the given vector in the coordinate system of the given image
    Args:
        inputVectorFileName:
        inputImageFileName:
        tmpReprojectedVector:

    Returns:

    """

    # use of .qpj instead of incomplete .prj
    if os.path.isfile(os.path.splitext(inputVectorFileName)[0] + ".qpj"):
        shutil.copy(
            os.path.splitext(inputVectorFileName)[0] + ".qpj",
            os.path.splitext(inputVectorFileName)[0] + ".prj")
    # test authority code availability
    epsg_vector = get_vector_epsg_with_ogr(inputVectorFileName)
    if not epsg_vector:
        logger.warning("Bad projection !")
        #raise
        # TODO add exception

    tmpReprojectedVector = os.path.join(output_directory,
                                        "tmp_reprojected.shp")
    # command = "otbcli_VectorDataReprojection -in.vd {} -out.proj.image.in {} -out.vd {}".format(inputVectorFileName,
    #                                                                                             inputImageFileName,
    #                                                                                             tmpReprojectedVector)
    #command = "ogr2ogr -t_srs {} -s_srs {} {} {}".format(epsg_code, None, tmpReprojectedVector, inputVectorFileName)
    #command = u"ogr2ogr -t_srs EPSG:{} {} {}".format(epsg_code, tmpReprojectedVector, inputVectorFileName)
    command = get_osgeo_command("ogr2ogr", [
        "-t_srs", "EPSG:{}".format(epsg_code), tmpReprojectedVector,
        inputVectorFileName
    ])

    TerreImageProcess().run_process(command)
    return tmpReprojectedVector
Exemple #13
0
 def test_get_osgeo_command(self):
     app_name = "ogr2ogr"
     args = ["-f", "ESRI Shapefile", "-clipsrc", "mymask.shp", "input", "output"]
     command = get_osgeo_command(app_name, args)
     command_baseline = u'ogr2ogr "-f" "ESRI Shapefile" "-clipsrc" "mymask.shp" "input" "output"'
     self.assertEqual(command, command_baseline)