Exemplo n.º 1
0
def import_file(filename, archive, output, region):
    """Extracts one binary file from its archive and import it."""

    # open the archive
    with ZipFile(archive, 'r') as a:

        # create temporary file and directory
        tempdir = grass.tempdir()
        tempfile = os.path.join(tempdir, filename)

        # try to inflate and import the layer

        if os.path.isfile(archive):
            try:
                grass.message("Inflating {} ...".format(filename))
                a.extract(filename, tempdir)
                grass.message("Importing {} as {} ..."
                              .format(filename, output))
                grass.run_command('r.in.bin',  flags='s', input=tempfile,
                                  output=output, bytes=2, anull=-9999,
                                  **region)

            # if file is not present in the archive
            except KeyError:
                grass.warning("Could not find {} in {}. Skipping"
                              .format(filename, archive))

            # make sure temporary files are cleaned
            finally:
                grass.try_remove(tempfile)
                grass.try_rmdir(tempdir)
        else:
            grass.warning("Could not find file {}. Skipping"
                          .format(archive))
Exemplo n.º 2
0
def import_file(filename, archive, output, region):
    """Extracts one binary file from its archive and import it."""

    # open the archive
    with ZipFile(archive, 'r') as a:

        # create temporary file and directory
        tempdir = grass.tempdir()
        tempfile = os.path.join(tempdir, filename)

        # try to inflate and import the layer
        try:
            grass.message("Inflating '%s' ..." % filename)
            a.extract(filename, tempdir)
            grass.message("Importing '%s' as <%s> ..." % (filename, output))
            grass.run_command('r.in.bin',  flags='s', overwrite=True,
                              input=tempfile, output=output,
                              bytes=2, anull=-9999, **region)

        # if file is not present in the archive
        except KeyError:
            grass.fatal("Could not find '%s' in '%s'" % (filename, archive))

        # make sure temporary files are cleaned
        finally:
            grass.try_remove(tempfile)
            grass.try_rmdir(tempdir)
Exemplo n.º 3
0
def cleanup():
    if not in_temp:
        return
    for ext in ['.bil', '.hdr', '.prj', '.hgt.zip']:
        grass.try_remove(tile + ext)
    os.chdir('..')
    grass.try_rmdir(tmpdir)
Exemplo n.º 4
0
def cleanup():
    if not in_temp:
	return
    for ext in ['.bil', '.hdr', '.prj', '.hgt.zip']:
	grass.try_remove(tile + ext)
    os.chdir('..')
    grass.try_rmdir(tmpdir)
Exemplo n.º 5
0
def cleanup():
    if not in_temp:
        return
    for ext in [".bil", ".hdr", ".prj", ".hgt.zip"]:
        grass.try_remove(tile + ext)
    os.chdir("..")
    grass.try_rmdir(tmpdir)
Exemplo n.º 6
0
    def tearDown(self):
        """Remove raster maps from current mapset"""

        # switch to old mapset
        self.runModule(
            "g.mapset",
            mapset=self.currmapset,
            quiet=True,
        )

        tgis.stop_subprocesses()
        tgis.init()

        self.strds_abs.delete()

        self.runModule(
            "g.remove",
            flags="f",
            type="raster",
            name="register_map_1,register_map_2",
            quiet=True,
        )
        grassenv = gscript.gisenv()
        mapset_path = os.path.join(
            grassenv["GISDBASE"], grassenv["LOCATION_NAME"], self.newmapset
        )
        gscript.try_rmdir(mapset_path)
Exemplo n.º 7
0
def cleanup():
    if not in_temp:
        return
    os.chdir(currdir)
    grass.run_command('g.region', region = tmpregionname)
    grass.run_command('g.remove', type = 'region', name = tmpregionname, flags = 'f', quiet = True)
    grass.try_rmdir(tmpdir)
def cleanup():
    if switchloc:
	# switch back to original location+mapset
	gscript.run_command('g.mapset', location=orgenv['LOCATION_NAME'],
					mapset=orgenv['MAPSET'])
    # remove temp location
    if TMPLOC:
        gscript.try_rmdir(os.path.join(GISDBASE, TMPLOC))
Exemplo n.º 9
0
def cleanup():
    if TGTGISRC:
        os.environ['GISRC'] = str(TGTGISRC)
    # remove temp location
    if TMPLOC:
        grass.try_rmdir(os.path.join(GISDBASE, TMPLOC))
    if SRCGISRC:
        grass.try_remove(SRCGISRC)
Exemplo n.º 10
0
def cleanup():
    # remove temp location
    if TMPLOC:
        grass.try_rmdir(os.path.join(GISDBASE, TMPLOC))
    if SRCGISRC:
        grass.try_remove(SRCGISRC)
    if TMP_REG_NAME:
        grass.run_command('g.remove', type='vector', name=TMP_REG_NAME,
                          flags='f', quiet=True)
Exemplo n.º 11
0
    def _createOutputMap(self):
        """!Import downloaded data into GRASS, reproject data if needed
        using ogr2ogr
        """
        # reprojection of downloaded data
        if self.proj_srs != self.proj_location:  # TODO: do it better
            grass.message(_("Reprojecting data..."))
            temp_warpmap = self._temp()

            if int(os.getenv("GRASS_VERBOSE", "2")) <= 2:
                nuldev = open(os.devnull, "w+")
            else:
                nuldev = None

            temp_warpmap = self._temp(directory=True)

            ps = grass.Popen(
                [
                    "ogr2ogr",
                    "-overwrite",
                    "-s_srs",
                    "%s" % self.proj_srs,
                    "-t_srs",
                    "%s" % self.proj_location,
                    "-f",
                    "%s" % self.ogr_drv_format,
                    temp_warpmap,
                    self.temp_map,
                ],
                stdout=nuldev,
            )
            ps.wait()

            if nuldev:
                nuldev.close()

            if ps.returncode != 0:
                grass.fatal(_("%s failed") % "ogr2ogr")
        # downloaded data projection is same as projection of location
        else:
            temp_warpmap = self.temp_map

        grass.message(_("Importing vector map into GRASS..."))
        # importing temp_map into GRASS
        try:
            grass.run_command(
                "v.in.ogr",
                quiet=True,
                overwrite=True,
                input=temp_warpmap,
                output=self.o_output,
            )
        except CalledModuleError:
            grass.fatal(_("%s failed") % "v.in.ogr")

        grass.try_rmdir(temp_warpmap)
        grass.try_remove(self.temp_map)
Exemplo n.º 12
0
def cleanup():
    # remove temp location
    if TMPLOC:
        grass.try_rmdir(os.path.join(GISDBASE, TMPLOC))
    if SRCGISRC:
        grass.try_remove(SRCGISRC)
    if TMP_REG_NAME and grass.find_file(name=TMP_REG_NAME, element='vector',
                                        mapset=grass.gisenv()['MAPSET'])['fullname']:
        grass.run_command('g.remove', type='vector', name=TMP_REG_NAME,
                          flags='f', quiet=True)
Exemplo n.º 13
0
def cleanup():
    # remove temp location
    if TMPLOC:
        grass.try_rmdir(os.path.join(GISDBASE, TMPLOC))
    if SRCGISRC:
        grass.try_remove(SRCGISRC)
    if TMP_REG_NAME and grass.find_file(name=TMP_REG_NAME, element='vector',
                                        mapset=grass.gisenv()['MAPSET'])['fullname']:
        grass.run_command('g.remove', type='vector', name=TMP_REG_NAME,
                          flags='f', quiet=True)
Exemplo n.º 14
0
def cleanup():
    if not in_temp:
        return
    os.chdir(currdir)
    if tmpregionname:
        grass.run_command("g.region", region=tmpregionname)
        grass.run_command("g.remove",
                          type="region",
                          name=tmpregionname,
                          flags="f",
                          quiet=True)
    grass.try_rmdir(tmpdir)
Exemplo n.º 15
0
def cleanup():
    # remove temp location
    if TMPLOC:
        grass.try_rmdir(os.path.join(GISDBASE, TMPLOC))
    if SRCGISRC:
        grass.try_remove(SRCGISRC)
    if TMP_REG_NAME:
        grass.run_command('g.remove',
                          type='vector',
                          name=TMP_REG_NAME,
                          flags='f',
                          quiet=True)
Exemplo n.º 16
0
    def __del__(self):

        # tries to remove temporary files/dirs, all temps should be
        # removoved before, implemented just in case of unexpected
        # stop of module
        for temp in self.temp_to_cleanup:
            if os.path.isdir(temp):
                grass.try_rmdir(temp)
            else:
                grass.try_remove(temp)

        # deletes enviromental variable which overrides region
        if 'GRASS_REGION' in os.environ.keys():
            os.environ.pop('GRASS_REGION')
Exemplo n.º 17
0
def cleanup():
    nuldev = open(os.devnull, 'w')
    kwargs = {'flags': 'f', 'quiet': True, 'stderr': nuldev}
    for rmr in rm_regions:
        if rmr in [x for x in grass.parse_command('g.list', type='region')]:
            grass.run_command('g.remove', type='region', name=rmr, **kwargs)
    for rmv in rm_vectors:
        if grass.find_file(name=rmv, element='vector')['file']:
            grass.run_command('g.remove', type='vector', name=rmv, **kwargs)
    for rmrast in rm_rasters:
        if grass.find_file(name=rmrast, element='raster')['file']:
            grass.run_command('g.remove', type='raster', name=rmrast, **kwargs)
    if tmpfolder:
        grass.try_rmdir(os.path.join(tmpfolder))
Exemplo n.º 18
0
def cleanup():
    if not in_temp:
        return
    os.chdir(currdir)
    if tmpregionname:
        grass.run_command('g.region', region = tmpregionname)
        grass.run_command('g.remove', type = 'region', name = tmpregionname, flags = 'f', quiet = True)
    grass.try_rmdir(tmpdir)
    if TGTGISRC:
        os.environ['GISRC'] = str(TGTGISRC)
    # remove temp location
    if TMPLOC:
        grass.try_rmdir(os.path.join(GISDBASE, TMPLOC))
    if SRCGISRC:
        grass.try_remove(SRCGISRC)
Exemplo n.º 19
0
def cleanup():
    # remove temp location
    if TMPLOC:
        grass.try_rmdir(os.path.join(GISDBASE, TMPLOC))
    if SRCGISRC:
        grass.try_remove(SRCGISRC)
    if (TMP_REG_NAME
            and grass.find_file(name=TMP_REG_NAME,
                                element="vector",
                                mapset=grass.gisenv()["MAPSET"])["fullname"]):
        grass.run_command("g.remove",
                          type="vector",
                          name=TMP_REG_NAME,
                          flags="f",
                          quiet=True)
Exemplo n.º 20
0
    def _createOutputMap(self):
        """!Import downloaded data into GRASS, reproject data if needed
        using ogr2ogr
        """
        # reprojection of downloaded data
        if self.proj_srs != self.proj_location:  # TODO: do it better
            grass.message(_("Reprojecting data..."))
            temp_warpmap = self._temp()

            if int(os.getenv('GRASS_VERBOSE', '2')) <= 2:
                nuldev = file(os.devnull, 'w+')
            else:
                nuldev = None

            temp_warpmap = self._temp(directory=True)

            ps = grass.Popen([
                'ogr2ogr', '-overwrite', '-s_srs',
                '%s' % self.proj_srs, '-t_srs',
                '%s' % self.proj_location, '-f',
                '%s' % self.ogr_drv_format, temp_warpmap, self.temp_map
            ],
                             stdout=nuldev)
            ps.wait()

            if nuldev:
                nuldev.close()

            if ps.returncode != 0:
                grass.fatal(_('%s failed') % 'ogr2ogr')
        # downloaded data projection is same as projection of location
        else:
            temp_warpmap = self.temp_map

        grass.message(_("Importing vector map into GRASS..."))
        # importing temp_map into GRASS
        try:
            grass.run_command('v.in.ogr',
                              quiet=True,
                              overwrite=True,
                              input=temp_warpmap,
                              output=self.o_output)
        except CalledModuleError:
            grass.fatal(_('%s failed') % 'v.in.ogr')

        grass.try_rmdir(temp_warpmap)
        grass.try_remove(self.temp_map)
Exemplo n.º 21
0
def import_file(filename, archive, output, region):
    """Extracts one binary file from its archive and import it."""

    # open the archive
    with ZipFile(archive, 'r') as a:

        # create temporary file and directory
        tempdir = grass.tempdir()
        tempfile = os.path.join(tempdir, filename)

        # try to inflate and import the layer

        if os.path.isfile(archive):
            try:
                grass.message("Inflating {} ...".format(filename))
                a.extract(filename, tempdir)
                grass.message("Importing {} as {} ...".format(
                    filename, output))
                grass.run_command('r.in.bin',
                                  flags='s',
                                  input=tempfile,
                                  output=output,
                                  bytes=2,
                                  anull=-9999,
                                  **region)

            # if file is not present in the archive
            except KeyError:
                grass.warning("Could not find {} in {}. Skipping".format(
                    filename, archive))

            # make sure temporary files are cleaned
            finally:
                grass.try_remove(tempfile)
                grass.try_rmdir(tempdir)
        else:
            grass.warning("Could not find file {}. Skipping".format(archive))
Exemplo n.º 22
0
def cleanup():
    # remove temp location
    if TMPLOC:
        grass.try_rmdir(os.path.join(GISDBASE, TMPLOC))
    if SRCGISRC:
        grass.try_remove(SRCGISRC)
Exemplo n.º 23
0
def cleanup():
    # remove temp location
    if TMPLOC:
        grass.try_rmdir(os.path.join(GISDBASE, TMPLOC))
    if SRCGISRC:
        grass.try_remove(SRCGISRC)