Exemple #1
0
 def test_run_nonexisting_app(self):
     import subprocess
     cmd = "non_existing_app"
     args = [""]
     err = subprocess.CalledProcessError
     with self.assertRaises(err):
         FileSystem.run_external_app(cmd, args)
Exemple #2
0
    def launch_maja(self, maja, wdir, inputdir, outdir, conf):
        """
        Run the MAJA processor for the given input_dir, mode and tile
        :param maja: The full path to the maja executable
        :param wdir: The working dir containing all inputs
        :param inputdir: The input directory containing all necessary files
        :param outdir: The output L2-directory
        :param conf: The full path to the userconf folder
        :return: The return code of Maja
        """
        from Common import FileSystem

        logfile = os.path.join(outdir, "%s.log" % self.l1.base.split(".")[0])
        args = [
            "-w", wdir, "--input", inputdir, "--output", outdir, "--mode",
            "L2" + self.mode, "-ucs", conf, "--TileId", self.tile,
            "--loglevel", self.log_level
        ]
        return FileSystem.run_external_app(maja,
                                           args,
                                           logfile=logfile,
                                           skip_error=self.skip_errors)
Exemple #3
0
 def test_run_app(self):
     import logging
     cmd = "echo"
     args = ["Hello"]
     self.assertEqual(
         FileSystem.run_external_app(cmd, args, log_level=logging.INFO), 0)
Exemple #4
0
def run_tiling(ds_or_path, **kwargs):
    """
    Run the tiling process

    :param ds_or_path: The :class:`gdal.Dataset` or a path to a file on disk to be tiled
    :type ds_or_path: :class:`gdal.Dataset` or str
    :param kwargs: Optional arguments.
    :return: Creates the tiles in the given location.
    """
    cmd_dict = {
        "Verbose": "-v",
        "Quiet": "-q",
        "CreationOptions": "-co",
        "TileSize": "-ps",
        "Overlap": "-overlap",
        "Format": "-of",
        "BandType": "-ot",
        "TileIndexFieldName": "-tileIndexField",
        "TileIndexName": "-tileIndex",
        "CsvDelimiter": "-csvDelim",
        "Source_SRS": "-s_srs",
        "TargetDir": "-targetDir",
        "-ResamplingMethod": "-r",
        "Levels": "-levels",
        "PyramidOnly": "-pyramidOnly",
        "UseDirForEachRow": "-useDirForEachRow"
    }

    gdal.AllRegister()
    gdal.UseExceptions()

    options = []

    if type(ds_or_path) != str:
        tmppath = os.path.join(tempfile.gettempdir(),
                               next(tempfile._get_candidate_names()))
        ds_or_path.write(tmppath)
        del_tmp = True
    else:
        tmppath = ds_or_path
        del_tmp = False
    tx, ty = None, None
    for key in kwargs.keys():
        if key == "TileWidth":
            ty = kwargs[key]
        elif key == "TileHeight":
            tx = kwargs[key]
        else:
            options += [cmd_dict[key], kwargs[key]]
    if not tx or not ty:
        raise ValueError("Must provide tile width and height: (%s, %s)" %
                         (ty, tx))
    options += [cmd_dict["TileSize"], "%s %s" % (ty, tx)]
    options += [tmppath]
    assert FileSystem.run_external_app(
        "gdal_retile.py", options) == 0, "Error running gdal_retile"
    bname = os.path.basename(tmppath)
    outpath = kwargs["TargetDir"]
    # TODO Discard previously written files from `find`:
    files_written = sorted(
        FileSystem.find(r"%s_\d+_\d+.tif" % bname, path=outpath))
    if del_tmp:
        FileSystem.remove_file(tmppath)
    return files_written