Пример #1
0
def main():
    group = options['group']
    raster = options['raster']
    output = options['output']
    coords = options['coordinates']
    img_fmt = options['format']
    coord_legend = flags['c']
    gnuplot = flags['g']

    global tmp_dir
    tmp_dir = grass.tempdir()

    if not group and not raster:
        grass.fatal(_("Either group= or raster= is required"))

    if group and raster:
        grass.fatal(_("group= and raster= are mutually exclusive"))

    # check if gnuplot is present
    if gnuplot and not grass.find_program('gnuplot', '-V'):
        grass.fatal(_("gnuplot required, please install first"))

    # get data from group listing and set the x-axis labels
    if group:
        # Parse the group list output
        s = grass.read_command('i.group', flags='g', group=group, quiet=True)
        rastermaps = s.splitlines()
    else:
        # get data from list of files and set the x-axis labels
        rastermaps = raster.split(',')

    xlabels = ["'%s' %d" % (n, i + 1) for i, n in enumerate(rastermaps)]
    xlabels = ','.join(xlabels)

    # get y-data for gnuplot-data file
    what = []
    s = grass.read_command('r.what',
                           map=rastermaps,
                           coordinates=coords,
                           null='0',
                           quiet=True)
    if len(s) == 0:
        grass.fatal(_('No data returned from query'))

    for l in s.splitlines():
        f = l.split('|')
        for i, v in enumerate(f):
            if v in ['', '*']:
                f[i] = 0
            else:
                f[i] = float(v)
        what.append(f)

    # build data files
    if gnuplot:
        draw_gnuplot(what, xlabels, output, img_fmt, coord_legend)
    else:
        draw_linegraph(what)
Пример #2
0
def main():
    group = options['group']
    raster = options['raster']
    output = options['output']
    coords = options['coordinates']
    img_fmt = options['format']
    coord_legend = flags['c']
    gnuplot = flags['g']
    
    global tmp_dir
    tmp_dir = grass.tempdir()
    
    if not group and not raster:
        grass.fatal(_("Either group= or raster= is required"))

    if group and raster:
        grass.fatal(_("group= and raster= are mutually exclusive"))

    # check if gnuplot is present
    if gnuplot and not grass.find_program('gnuplot', '-V'):
        grass.fatal(_("gnuplot required, please install first"))

    # get data from group listing and set the x-axis labels
    if group:
        # Parse the group list output
        s = grass.read_command('i.group', flags='g', group=group, quiet=True)
        rastermaps = s.splitlines()
    else:
        # get data from list of files and set the x-axis labels
        rastermaps = raster.split(',')

    xlabels = ["'%s' %d" % (n, i + 1) for i, n in enumerate(rastermaps)]
    xlabels = ','.join(xlabels)

    # get y-data for gnuplot-data file
    what = []
    s = grass.read_command('r.what', map=rastermaps, coordinates=coords,
                           null='0', quiet=True)
    if len(s) == 0:
        grass.fatal(_('No data returned from query'))

    for l in s.splitlines():
        f = l.split('|')
        for i, v in enumerate(f):
            if v in ['', '*']:
                f[i] = 0
            else:
                f[i] = float(v)
        what.append(f)

    # build data files
    if gnuplot:
        draw_gnuplot(what, xlabels, output, img_fmt, coord_legend)
    else:
        draw_linegraph(what)
Пример #3
0
def main():
    infile = options['input']

    # create temporary directory
    global tmp_dir
    tmp_dir = grass.tempdir()
    grass.debug('tmp_dir = %s' % tmp_dir)

    # check if the input file exists
    if not os.path.exists(infile):
        grass.fatal(_("File <%s> not found") % infile)

    # copy the files to tmp dir
    input_base = os.path.basename(infile)
    shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
    os.chdir(tmp_dir)
    tar = tarfile.TarFile.open(name=input_base, mode='r')
    try:
        data_name = tar.getnames()[0]
    except:
        grass.fatal(_("Pack file unreadable"))

    if flags['p']:
        # print proj info and exit
        try:
            for fname in ['PROJ_INFO', 'PROJ_UNITS']:
                f = tar.extractfile(fname)
                sys.stdout.write(f.read())
        except KeyError:
            grass.fatal(
                _("Pack file unreadable: file '{}' missing".format(fname)))
        tar.close()

        return 0

    # set the output name
    if options['output']:
        map_name = options['output']
    else:
        map_name = data_name

    # grass env
    gisenv = grass.gisenv()
    mset_dir = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
                            gisenv['MAPSET'])

    new_dir = os.path.join(mset_dir, 'vector', map_name)

    gfile = grass.find_file(name=map_name, element='vector', mapset='.')
    overwrite = os.getenv('GRASS_OVERWRITE')
    if gfile['file'] and overwrite != '1':
        grass.fatal(_("Vector map <%s> already exists") % map_name)
    elif overwrite == '1' and gfile['file']:
        grass.warning(
            _("Vector map <%s> already exists and will be overwritten") %
            map_name)
        grass.run_command('g.remove',
                          flags='f',
                          quiet=True,
                          type='vector',
                          name=map_name)
        shutil.rmtree(new_dir, True)

    # extract data
    tar.extractall()
    tar.close()
    if os.path.exists(os.path.join(data_name, 'coor')):
        pass
    elif os.path.exists(os.path.join(data_name, 'cell')):
        grass.fatal(
            _("This GRASS GIS pack file contains raster data. Use "
              "r.unpack to unpack <%s>" % map_name))
    else:
        grass.fatal(_("Pack file unreadable"))

    # check projection compatibility in a rather crappy way
    loc_proj = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')
    loc_proj_units = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_UNITS')

    skip_projection_check = False
    if not os.path.exists(os.path.join(tmp_dir, 'PROJ_INFO')):
        if os.path.exists(loc_proj):
            grass.fatal(
                _("PROJ_INFO file is missing, unpack vector map in XY (unprojected) location."
                  ))
        skip_projection_check = True  # XY location

    if not skip_projection_check:
        diff_result_1 = diff_result_2 = None
        if not grass.compare_key_value_text_files(filename_a=os.path.join(
                tmp_dir, 'PROJ_INFO'),
                                                  filename_b=loc_proj,
                                                  proj=True):
            diff_result_1 = diff_files(os.path.join(tmp_dir, 'PROJ_INFO'),
                                       loc_proj)

        if not grass.compare_key_value_text_files(filename_a=os.path.join(
                tmp_dir, 'PROJ_UNITS'),
                                                  filename_b=loc_proj_units,
                                                  units=True):
            diff_result_2 = diff_files(os.path.join(tmp_dir, 'PROJ_UNITS'),
                                       loc_proj_units)

        if diff_result_1 or diff_result_2:
            if flags['o']:
                grass.warning(
                    _("Projection information does not match. Proceeding..."))
            else:
                if diff_result_1:
                    grass.warning(
                        _("Difference between PROJ_INFO file of packed map "
                          "and of current location:\n{diff}").format(
                              diff=''.join(diff_result_1)))
                if diff_result_2:
                    grass.warning(
                        _("Difference between PROJ_UNITS file of packed map "
                          "and of current location:\n{diff}").format(
                              diff=''.join(diff_result_2)))
                grass.fatal(
                    _("Projection of dataset does not appear to match current location."
                      " In case of no significant differences in the projection definitions,"
                      " use the -o flag to ignore them and use"
                      " current location definition."))

    # new db
    fromdb = os.path.join(tmp_dir, 'db.sqlite')
    # copy file
    shutil.copytree(data_name, new_dir)
    # exist fromdb
    if os.path.exists(fromdb):
        # the db connection in the output mapset
        dbconn = grassdb.db_connection(force=True)
        todb = dbconn['database']
        # return all tables
        list_fromtable = grass.read_command('db.tables',
                                            driver='sqlite',
                                            database=fromdb).splitlines()

        # return the list of old connection for extract layer number and key
        dbln = open(os.path.join(new_dir, 'dbln'), 'r')
        dbnlist = dbln.readlines()
        dbln.close()
        # check if dbf or sqlite directory exists
        if dbconn['driver'] == 'dbf' and not os.path.exists(
                os.path.join(mset_dir, 'dbf')):
            os.mkdir(os.path.join(mset_dir, 'dbf'))
        elif dbconn['driver'] == 'sqlite' and not os.path.exists(
                os.path.join(mset_dir, 'sqlite')):
            os.mkdir(os.path.join(mset_dir, 'sqlite'))
        # for each old connection
        for t in dbnlist:
            # it split the line of each connection, to found layer number and key
            if len(t.split('|')) != 1:
                values = t.split('|')
            else:
                values = t.split(' ')

            from_table = values[1]
            layer = values[0].split('/')[0]
            # we need to take care about the table name in case of several layer
            if options["output"]:
                if len(dbnlist) > 1:
                    to_table = "%s_%s" % (map_name, layer)
                else:
                    to_table = map_name
            else:
                to_table = from_table

            grass.verbose(
                _("Coping table <%s> as table <%s>") % (from_table, to_table))

            # copy the table in the default database
            try:
                grass.run_command('db.copy',
                                  to_driver=dbconn['driver'],
                                  to_database=todb,
                                  to_table=to_table,
                                  from_driver='sqlite',
                                  from_database=fromdb,
                                  from_table=from_table)
            except CalledModuleError:
                grass.fatal(
                    _("Unable to copy table <%s> as table <%s>") %
                    (from_table, to_table))

            grass.verbose(
                _("Connect table <%s> to vector map <%s> at layer <%s>") %
                (to_table, map_name, layer))

            # and connect the new tables with the right layer
            try:
                grass.run_command('v.db.connect',
                                  flags='o',
                                  quiet=True,
                                  driver=dbconn['driver'],
                                  database=todb,
                                  map=map_name,
                                  key=values[2],
                                  layer=layer,
                                  table=to_table)
            except CalledModuleError:
                grass.fatal(
                    _("Unable to connect table <%s> to vector map <%s>") %
                    (to_table, map_name))

    grass.message(_("Vector map <%s> successfully unpacked") % map_name)
Пример #4
0
def main():
    infile = options['input']

    # create temporary directory
    global tmp_dir
    tmp_dir = grass.tempdir()
    grass.debug('tmp_dir = %s' % tmp_dir)

    # check if the input file exists
    if not os.path.exists(infile):
        grass.fatal(_("File <%s> not found") % infile)

    # copy the files to tmp dir
    input_base = os.path.basename(infile)
    shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
    os.chdir(tmp_dir)
    tar = tarfile.TarFile.open(name=input_base, mode='r')
    try:
        data_name = tar.getnames()[0]
    except:
        grass.fatal(_("Pack file unreadable"))

    # set the output name
    if options['output']:
        map_name = options['output']
    else:
        map_name = data_name

    # grass env
    gisenv = grass.gisenv()
    mset_dir = os.path.join(gisenv['GISDBASE'],
                            gisenv['LOCATION_NAME'],
                            gisenv['MAPSET'])

    new_dir = os.path.join(mset_dir, 'vector', map_name)

    gfile = grass.find_file(name=map_name, element='vector', mapset='.')
    overwrite = os.getenv('GRASS_OVERWRITE')
    if gfile['file'] and overwrite != '1':
        grass.fatal(_("Vector map <%s> already exists") % map_name)
    elif overwrite == '1' and gfile['file']:
        grass.warning(_("Vector map <%s> already exists and will be overwritten") % map_name)
        grass.run_command('g.remove', flags='f', quiet=True, type='vector',
                          name=map_name)
        shutil.rmtree(new_dir, True)

    # extract data
    tar.extractall()
    if os.path.exists(os.path.join(data_name, 'coor')):
        pass
    elif os.path.exists(os.path.join(data_name, 'cell')):
        grass.fatal(_("This GRASS GIS pack file contains raster data. Use "
                      "r.unpack to unpack <%s>" % map_name))
    else:
        grass.fatal(_("Pack file unreadable"))

    # check projection compatibility in a rather crappy way
    loc_proj = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')
    loc_proj_units = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_UNITS')

    skip_projection_check = False
    if not os.path.exists(os.path.join(tmp_dir, 'PROJ_INFO')):
        if os.path.exists(loc_proj):
            grass.fatal(
                _("PROJ_INFO file is missing, unpack vector map in XY (unprojected) location."))
        skip_projection_check = True  # XY location

    if not skip_projection_check:
        diff_result_1 = diff_result_2 = None
        if not grass.compare_key_value_text_files(filename_a=os.path.join(tmp_dir, 'PROJ_INFO'),
                                                  filename_b=loc_proj, proj=True):
            diff_result_1 = diff_files(os.path.join(tmp_dir, 'PROJ_INFO'),
                                       loc_proj)

        if not grass.compare_key_value_text_files(filename_a=os.path.join(tmp_dir, 'PROJ_UNITS'),
                                                  filename_b=loc_proj_units,
                                                  units=True):
            diff_result_2 = diff_files(os.path.join(tmp_dir, 'PROJ_UNITS'),
                                       loc_proj_units)

        if diff_result_1 or diff_result_2:
            if flags['o']:
                grass.warning(_("Projection information does not match. Proceeding..."))
            else:
                if diff_result_1:
                    grass.warning(_("Difference between PROJ_INFO file of packed map "
                                    "and of current location:\n{diff}").format(diff=''.join(diff_result_1)))
                if diff_result_2:
                    grass.warning(_("Difference between PROJ_UNITS file of packed map "
                                    "and of current location:\n{diff}").format(diff=''.join(diff_result_2)))
                grass.fatal(_("Projection of dataset does not appear to match current location."
                              " In case of no significant differences in the projection definitions,"
                              " use the -o flag to ignore them and use"
                              " current location definition."))

    # new db
    fromdb = os.path.join(tmp_dir, 'db.sqlite')
    # copy file
    shutil.copytree(data_name, new_dir)
    # exist fromdb
    if os.path.exists(fromdb):
        # the db connection in the output mapset
        dbconn = grassdb.db_connection(force=True)
        todb = dbconn['database']
        # return all tables
        list_fromtable = grass.read_command('db.tables', driver='sqlite',
                                            database=fromdb).splitlines()

        # return the list of old connection for extract layer number and key
        dbln = open(os.path.join(new_dir, 'dbln'), 'r')
        dbnlist = dbln.readlines()
        dbln.close()
        # check if dbf or sqlite directory exists
        if dbconn['driver'] == 'dbf' and not os.path.exists(os.path.join(mset_dir, 'dbf')):
            os.mkdir(os.path.join(mset_dir, 'dbf'))
        elif dbconn['driver'] == 'sqlite' and not os.path.exists(os.path.join(mset_dir, 'sqlite')):
            os.mkdir(os.path.join(mset_dir, 'sqlite'))
        # for each old connection
        for t in dbnlist:
            # it split the line of each connection, to found layer number and key
            if len(t.split('|')) != 1:
                values = t.split('|')
            else:
                values = t.split(' ')

            from_table = values[1]
            layer = values[0].split('/')[0]
            # we need to take care about the table name in case of several layer
            if options["output"]:
                if len(dbnlist) > 1:
                    to_table = "%s_%s" % (map_name, layer)
                else:
                    to_table = map_name
            else:
                to_table = from_table

            grass.verbose(_("Coping table <%s> as table <%s>") % (from_table,
                                                                  to_table))

            # copy the table in the default database
            try:
                grass.run_command('db.copy', to_driver=dbconn['driver'],
                                  to_database=todb, to_table=to_table,
                                  from_driver='sqlite',
                                  from_database=fromdb,
                                  from_table=from_table)
            except CalledModuleError:
                grass.fatal(_("Unable to copy table <%s> as table <%s>") % (from_table, to_table))

            grass.verbose(_("Connect table <%s> to vector map <%s> at layer <%s>") %
                           (to_table, map_name, layer))

            # and connect the new tables with the right layer
            try:
                grass.run_command('v.db.connect', flags='o', quiet=True,
                                  driver=dbconn['driver'], database=todb,
                                  map=map_name, key=values[2],
                                  layer=layer, table=to_table)
            except CalledModuleError:
                grass.fatal(_("Unable to connect table <%s> to vector map <%s>") %
                             (to_table, map_name))

    grass.message(_("Vector map <%s> successfully unpacked") % map_name)
Пример #5
0
def main():
    group = options["group"]
    subgroup = options["subgroup"]
    raster = options["raster"]
    output = options["output"]
    coords = options["coordinates"]
    img_fmt = options["format"]
    coord_legend = flags["c"]
    gnuplot = flags["g"]
    textfile = flags["t"]

    global tmp_dir
    tmp_dir = gcore.tempdir()

    if not group and not raster:
        gcore.fatal(_("Either group= or raster= is required"))

    if group and raster:
        gcore.fatal(_("group= and raster= are mutually exclusive"))

    # -t needs an output filename
    if textfile and not output:
        gcore.fatal(_("Writing to text file requires output=filename"))

    # check if gnuplot is present
    if gnuplot and not gcore.find_program("gnuplot", "-V"):
        gcore.fatal(_("gnuplot required, please install first"))

    # get data from group listing and set the x-axis labels
    if group:
        # Parse the group list output
        if subgroup:
            s = gcore.read_command("i.group",
                                   flags="g",
                                   group=group,
                                   subgroup=subgroup,
                                   quiet=True)
        else:
            s = gcore.read_command("i.group",
                                   flags="g",
                                   group=group,
                                   quiet=True)
        rastermaps = s.splitlines()
    else:
        # get data from list of files and set the x-axis labels
        rastermaps = raster.split(",")

    xlabels = ["'%s' %d" % (n, i + 1) for i, n in enumerate(rastermaps)]
    xlabels = ",".join(xlabels)

    # get y-data for gnuplot-data file
    what = []
    s = gcore.read_command("r.what",
                           map=rastermaps,
                           coordinates=coords,
                           null="0",
                           quiet=True)
    if len(s) == 0:
        gcore.fatal(_("No data returned from query"))

    for l in s.splitlines():
        f = l.split("|")
        for i, v in enumerate(f):
            if v in ["", "*"]:
                f[i] = 0
            else:
                f[i] = float(v)
        what.append(f)

    # build data files
    if gnuplot:
        draw_gnuplot(what, xlabels, output, img_fmt, coord_legend)
    elif textfile:
        write2textf(what, output)
    else:
        draw_linegraph(what)
Пример #6
0
            sys.stdout.write('\n'.join(elist))
            sys.stdout.write('\n')
        else:
            grass.info(_("No extension installed"))
        return 0
    else:
        if not options['extension']:
            grass.fatal(_('You need to define an extension name or use -l'))

    if flags['d']:
        if options['operation'] != 'add':
            grass.warning(_("Flag 'd' is relevant only to 'operation=add'. Ignoring this flag."))
        else:
            global remove_tmpdir
            remove_tmpdir = False
    
    if options['operation'] == 'add':
        check_dirs()
        install_extension()
    else: # remove
        remove_extension(flags['f'])
    
    return 0

if __name__ == "__main__":
    options, flags = grass.parser()
    global tmpdir
    tmpdir = grass.tempdir()
    atexit.register(cleanup)
    sys.exit(main())
Пример #7
0
def main():
    infile = options['input']

    global tmp_dir
    tmp_dir = grass.tempdir()
    grass.debug('tmp_dir = {tmpdir}'.format(tmpdir=tmp_dir))

    if not os.path.exists(infile):
        grass.fatal(_('File {name} not found.'.format(name=infile)))

    gisenv = grass.gisenv()
    mset_dir = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
                            gisenv['MAPSET'])
    input_base = os.path.basename(infile)
    shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
    os.chdir(tmp_dir)
    tar = tarfile.TarFile.open(name=input_base, mode='r')
    try:
        data_name = tar.getnames()[0]
    except:
        grass.fatal(_("Pack file unreadable"))

    if options['output']:
        map_name = options['output']
    else:
        map_name = data_name.split('@')[0]

    gfile = grass.find_file(name=map_name, element='cell', mapset='.')
    if gfile['file']:
        if os.environ.get('GRASS_OVERWRITE', '0') != '1':
            grass.fatal(
                _('Raster map <{name}> already exists'.format(name=map_name)))
        else:
            grass.warning(
                _('Raster map <{name}> already exists and will be overwritten'.
                  format(name=map_name)))

    # extract data
    tar.extractall()
    os.chdir(data_name)

    if os.path.exists('cell'):
        pass
    elif os.path.exists('coor'):
        grass.fatal(
            _('This GRASS GIS pack file contains vector data. Use '
              'v.unpack to unpack <{name}>'.format(name=map_name)))
    else:
        grass.fatal(_("Pack file unreadable"))

    # check projection compatibility in a rather crappy way

    if flags['o']:
        grass.warning(
            _("Overriding projection check (using current location's projection)."
              ))

    else:

        diff_result_1 = diff_result_2 = None

        proj_info_file_1 = 'PROJ_INFO'
        proj_info_file_2 = os.path.join(mset_dir, '..', 'PERMANENT',
                                        'PROJ_INFO')

        skip_projection_check = False
        if not os.path.exists(proj_info_file_1):
            if os.path.exists(proj_info_file_2):
                grass.fatal(
                    _("PROJ_INFO file is missing, unpack raster map in XY (unprojected) location."
                      ))
            skip_projection_check = True  # XY location

        if not skip_projection_check:
            if not grass.compare_key_value_text_files(
                    filename_a=proj_info_file_1,
                    filename_b=proj_info_file_2,
                    proj=True):
                diff_result_1 = diff_files(proj_info_file_1, proj_info_file_2)

            proj_units_file_1 = 'PROJ_UNITS'
            proj_units_file_2 = os.path.join(mset_dir, '..', 'PERMANENT',
                                             'PROJ_UNITS')

            if not grass.compare_key_value_text_files(
                    filename_a=proj_units_file_1,
                    filename_b=proj_units_file_2,
                    units=True):
                diff_result_2 = diff_files(proj_units_file_1,
                                           proj_units_file_2)

            if diff_result_1 or diff_result_2:

                if diff_result_1:
                    grass.warning(
                        _("Difference between PROJ_INFO file of packed map "
                          "and of current location:\n{diff}").format(
                              diff=''.join(diff_result_1)))
                if diff_result_2:
                    grass.warning(
                        _("Difference between PROJ_UNITS file of packed map "
                          "and of current location:\n{diff}").format(
                              diff=''.join(diff_result_2)))
                grass.fatal(
                    _("Projection of dataset does not appear to match current location."
                      " In case of no significant differences in the projection definitions,"
                      " use the -o flag to ignore them and use"
                      " current location definition."))

    # install in $MAPSET
    for element in [
            'cats', 'cell', 'cellhd', 'cell_misc', 'colr', 'fcell', 'hist'
    ]:
        if not os.path.exists(element):
            continue
        path = os.path.join(mset_dir, element)
        if not os.path.exists(path):
            os.mkdir(path)
        if element == 'cell_misc':
            path = os.path.join(mset_dir, element, map_name)
            if os.path.exists(path):
                shutil.rmtree(path)
            shutil.copytree('cell_misc', path)
        else:
            shutil.copyfile(element, os.path.join(mset_dir, element, map_name))

    grass.message(_('Raster map <{name}> unpacked'.format(name=map_name)))
Пример #8
0
def main():
    infile = options['input']

    global tmp_dir
    tmp_dir = grass.tempdir()
    grass.debug('tmp_dir = %s' % tmp_dir)

    if not os.path.exists(infile):
        grass.fatal(_("File <%s> not found") % infile)

    gisenv = grass.gisenv()
    mset_dir = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
                            gisenv['MAPSET'])
    input_base = os.path.basename(infile)
    shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
    os.chdir(tmp_dir)
    tar = tarfile.TarFile.open(name=input_base, mode='r')
    try:
        data_name = tar.getnames()[0]
    except:
        grass.fatal(_("Pack file unreadable"))

    if options['output']:
        map_name = options['output']
    else:
        map_name = data_name.split('@')[0]

    gfile = grass.find_file(name=map_name, element='cell', mapset='.')
    if gfile['file']:
        if os.environ.get('GRASS_OVERWRITE', '0') != '1':
            grass.fatal(_("Raster map <%s> already exists") % map_name)
        else:
            grass.warning(
                _("Raster map <%s> already exists and will be overwritten") %
                map_name)

    # extract data
    tar.extractall()
    os.chdir(data_name)

    if os.path.exists('cell'):
        pass
    elif os.path.exists('coor'):
        grass.fatal(
            _("This GRASS GIS pack file contains vector data. Use "
              "v.unpack to unpack <%s>" % map_name))
    else:
        grass.fatal(_("Pack file unreadable"))

    # check projection compatibility in a rather crappy way
    diff_result_1 = diff_result_2 = None
    proj_info_file_1 = 'PROJ_INFO'
    proj_info_file_2 = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')
    if not grass.compare_key_value_text_files(filename_a=proj_info_file_1,
                                              filename_b=proj_info_file_2,
                                              proj=True):
        diff_result_1 = diff_files(proj_info_file_1, proj_info_file_2)

    proj_units_file_1 = 'PROJ_UNITS'
    proj_units_file_2 = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_UNITS')
    if not grass.compare_key_value_text_files(filename_a=proj_units_file_1,
                                              filename_b=proj_units_file_2,
                                              units=True):
        diff_result_2 = diff_files(proj_units_file_1, proj_units_file_2)

    if diff_result_1 or diff_result_2:
        if flags['o']:
            grass.warning(
                _("Projection information does not match. Proceeding..."))
        else:
            if diff_result_1:
                grass.warning(
                    _("Difference between PROJ_INFO file of packed map "
                      "and of current location:\n{diff}").format(
                          diff=''.join(diff_result_1)))
            if diff_result_2:
                grass.warning(
                    _("Difference between PROJ_UNITS file of packed map "
                      "and of current location:\n{diff}").format(
                          diff=''.join(diff_result_2)))
            grass.fatal(_("Projection information does not match. Aborting."))

    # install in $MAPSET
    for element in [
            'cats', 'cell', 'cellhd', 'cell_misc', 'colr', 'fcell', 'hist'
    ]:
        if not os.path.exists(element):
            continue
        path = os.path.join(mset_dir, element)
        if not os.path.exists(path):
            os.mkdir(path)
        if element == 'cell_misc':
            path = os.path.join(mset_dir, element, map_name)
            if os.path.exists(path):
                shutil.rmtree(path)
            shutil.copytree('cell_misc', path)
        else:
            shutil.copyfile(element, os.path.join(mset_dir, element, map_name))

    grass.message(_("Raster map <%s> unpacked") % map_name)
Пример #9
0
def main():
    infile = options['input']
    compression_off = flags['c']
    mapset = None
    if '@' in infile:
        infile, mapset = infile.split('@')

    if options['output']:
        outfile_path, outfile_base = os.path.split(
            os.path.abspath(options['output']))
    else:
        outfile_path, outfile_base = os.path.split(
            os.path.abspath(infile + ".pack"))

    outfile = os.path.join(outfile_path, outfile_base)

    global tmp
    tmp = grass.tempdir()
    tmp_dir = os.path.join(tmp, infile)
    os.mkdir(tmp_dir)
    grass.debug('tmp_dir = %s' % tmp_dir)

    gfile = grass.find_file(name=infile, element='cell', mapset=mapset)
    if not gfile['name']:
        grass.fatal(_("Raster map <%s> not found") % infile)

    if os.path.exists(outfile):
        if os.getenv('GRASS_OVERWRITE'):
            grass.warning(
                _("Pack file <%s> already exists and will be overwritten") %
                outfile)
            try_remove(outfile)
        else:
            grass.fatal(_("option <output>: <%s> exists.") % outfile)

    grass.message(_("Packing <%s> to <%s>...") % (gfile['fullname'], outfile))
    basedir = os.path.sep.join(
        os.path.normpath(gfile['file']).split(os.path.sep)[:-2])
    olddir = os.getcwd()

    # copy elements
    info = grass.parse_command('r.info', flags='e', map=infile)
    vrt_files = {}
    if info['maptype'] == 'virtual':
        map_file = grass.find_file(
            name=infile,
            element='cell_misc',
        )
        if map_file['file']:
            vrt = os.path.join(map_file['file'], 'vrt')
            if os.path.exists(vrt):
                with open(vrt, 'r') as f:
                    for r in f.readlines():
                        map, mapset = r.split('@')
                        map_basedir = os.path.sep.join(
                            os.path.normpath(map_file['file'], ).split(
                                os.path.sep)[:-2], )
                        vrt_files[map] = map_basedir

    for element in [
            'cats',
            'cell',
            'cellhd',
            'cell_misc',
            'colr',
            'fcell',
            'hist',
    ]:
        path = os.path.join(basedir, element, infile)
        if os.path.exists(path):
            grass.debug('copying %s' % path)
            if os.path.isfile(path):
                shutil.copyfile(
                    path,
                    os.path.join(tmp_dir, element),
                )
            else:
                shutil.copytree(
                    path,
                    os.path.join(tmp_dir, element),
                )

        # Copy vrt files
        if vrt_files:
            for f in vrt_files.keys():
                f_tmp_dir = os.path.join(tmp, f)
                if not os.path.exists(f_tmp_dir):
                    os.mkdir(f_tmp_dir)
                path = os.path.join(vrt_files[f], element, f)
                if os.path.exists(path):
                    grass.debug("copying vrt file {}".format(path))
                    if os.path.isfile(path):
                        shutil.copyfile(
                            path,
                            os.path.join(f_tmp_dir, element),
                        )
                    else:
                        shutil.copytree(
                            path,
                            os.path.join(f_tmp_dir, element),
                        )

    if not os.listdir(tmp_dir):
        grass.fatal(_("No raster map components found"))

    # copy projection info
    # (would prefer to use g.proj*, but this way is 5.3 and 5.7 compat)
    gisenv = grass.gisenv()
    for support in ['INFO', 'UNITS', 'EPSG']:
        path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
                            'PERMANENT', 'PROJ_' + support)
        if os.path.exists(path):
            shutil.copyfile(path, os.path.join(tmp_dir, 'PROJ_' + support))

    # pack it all up
    os.chdir(tmp)
    if compression_off:
        tar = tarfile.TarFile.open(name=outfile_base, mode='w:')
    else:
        tar = tarfile.TarFile.open(name=outfile_base, mode='w:gz')
    tar.add(infile, recursive=True)
    if vrt_files:
        for f in vrt_files.keys():
            tar.add(f, recursive=True)

    tar.close()
    try:
        shutil.move(outfile_base, outfile)
    except shutil.Error as e:
        grass.fatal(e)

    os.chdir(olddir)

    grass.verbose(_("Raster map saved to '%s'" % outfile))
Пример #10
0
            sys.stdout.write('\n'.join(elist))
            sys.stdout.write('\n')
        else:
            grass.info(_("No extension installed"))
        return 0
    else:
        if not options['extension']:
            grass.fatal(_('You need to define an extension name or use -l'))

    if flags['d']:
        if options['operation'] != 'add':
            grass.warning(_("Flag 'd' is relevant only to 'operation=add'. Ignoring this flag."))
        else:
            global remove_tmpdir
            remove_tmpdir = False
    
    if options['operation'] == 'add':
        check_dirs()
        install_extension()
    else: # remove
        remove_extension(flags['f'])
    
    return 0

if __name__ == "__main__":
    options, flags = grass.parser()
    global tmpdir
    tmpdir = grass.tempdir()
    atexit.register(cleanup)
    sys.exit(main())
Пример #11
0
def main():
    infile = options["input"]
    compression_off = flags["c"]

    global basedir
    basedir = grass.tempdir()

    # check if vector map exists
    gfile = grass.find_file(infile, element="vector")
    if not gfile["name"]:
        grass.fatal(_("Vector map <%s> not found") % infile)

    # check if input vector map is in the native format
    if vector.vector_info(gfile["fullname"])["format"] != "native":
        grass.fatal(
            _("Unable to pack vector map <%s>. Only native format supported.")
            % gfile["fullname"]
        )

    # split the name if there is the mapset name
    if infile.find("@"):
        infile = infile.split("@")[0]

    # output name
    if options["output"]:
        outfile = options["output"]
    else:
        outfile = infile + ".pack"

    # check if exists the output file
    if os.path.exists(outfile):
        if os.getenv("GRASS_OVERWRITE"):
            grass.warning(
                _("Pack file <%s> already exists and will be overwritten") % outfile
            )
            try_remove(outfile)
        else:
            grass.fatal(_("option <%s>: <%s> exists.") % ("output", outfile))

    # prepare for packing
    grass.verbose(_("Packing <%s>...") % (gfile["fullname"]))

    # write tar file, optional compression
    if compression_off:
        tar = tarfile.open(name=outfile, mode="w:")
    else:
        tar = tarfile.open(name=outfile, mode="w:gz")
    tar.add(gfile["file"], infile)

    # check if exist a db connection for the vector
    db_vect = vector.vector_db(gfile["fullname"])
    if not db_vect:
        grass.verbose(
            _("There is not database connected with vector map <%s>")
            % gfile["fullname"]
        )
    else:
        # for each layer connection save a table in sqlite database
        sqlitedb = os.path.join(basedir, "db.sqlite")
        for i, dbconn in db_vect.items():
            grass.run_command(
                "db.copy",
                from_driver=dbconn["driver"],
                from_database=dbconn["database"],
                from_table=dbconn["table"],
                to_driver="sqlite",
                to_database=sqlitedb,
                to_table=dbconn["table"],
            )
        tar.add(sqlitedb, "db.sqlite")

    # add to the tar file the PROJ files to check when unpack file
    gisenv = grass.gisenv()
    for support in ["INFO", "UNITS", "EPSG"]:
        path = os.path.join(
            gisenv["GISDBASE"], gisenv["LOCATION_NAME"], "PERMANENT", "PROJ_" + support
        )
        if os.path.exists(path):
            tar.add(path, "PROJ_" + support)
    tar.close()

    grass.message(_("Pack file <%s> created") % os.path.join(os.getcwd(), outfile))
Пример #12
0
def main():
    infile = options['input']
    compression_off = flags['c']
    
    global basedir
    basedir = grass.tempdir()
    
    # check if vector map exists
    gfile = grass.find_file(infile, element = 'vector')
    if not gfile['name']:
        grass.fatal(_("Vector map <%s> not found") % infile)
    
    # check if input vector map is in the native format
    if vector.vector_info(gfile['fullname'])['format'] != 'native':
        grass.fatal(_("Unable to pack vector map <%s>. Only native format supported.") % \
                        gfile['fullname'])
    
    # split the name if there is the mapset name
    if infile.find('@'):
        infile = infile.split('@')[0]
    
    # output name
    if options['output']:
        outfile = options['output']
    else:
        outfile = infile + '.pack'
    
    # check if exists the output file
    if os.path.exists(outfile):
        if os.getenv('GRASS_OVERWRITE'):
            grass.warning(_("Pack file <%s> already exists and will be overwritten") % outfile)
            try_remove(outfile)
        else:
            grass.fatal(_("option <%s>: <%s> exists.") % ("output", outfile))
    
    # prepare for packing
    grass.verbose(_("Packing <%s>...") % (gfile['fullname']))
    
    # write tar file, optional compression 
    if compression_off:
        tar = tarfile.open(name = outfile, mode = 'w:')
    else:
        tar = tarfile.open(name = outfile, mode = 'w:gz')
    tar.add(gfile['file'], infile)
    
    # check if exist a db connection for the vector 
    db_vect = vector.vector_db(gfile['fullname'])
    if not db_vect:
        grass.verbose(_('There is not database connected with vector map <%s>') % gfile['fullname'])
    else:
        # for each layer connection save a table in sqlite database
        sqlitedb = os.path.join(basedir, 'db.sqlite')
        for i, dbconn in db_vect.iteritems():
            grass.run_command('db.copy', from_driver = dbconn['driver'], 
                              from_database = dbconn['database'],
                              from_table =  dbconn['table'], 
                              to_driver = 'sqlite', to_database = sqlitedb, 
                              to_table = dbconn['table'])
        tar.add(sqlitedb, 'db.sqlite')
    
    # add to the tar file the PROJ files to check when unpack file    
    gisenv = grass.gisenv()
    for support in ['INFO', 'UNITS']:
        path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
                            'PERMANENT', 'PROJ_' + support)
        if os.path.exists(path):
            tar.add(path, 'PROJ_' + support)
    tar.close()
    
    grass.message(_("Pack file <%s> created") % os.path.join(os.getcwd(), outfile))
Пример #13
0
def main():
    infile = options['input']

    global tmp_dir
    tmp_dir = grass.tempdir()
    grass.debug('tmp_dir = {tmpdir}'.format(tmpdir=tmp_dir))

    if not os.path.exists(infile):
        grass.fatal(_('File {name} not found.'.format(name=infile)))

    gisenv = grass.gisenv()
    mset_dir = os.path.join(gisenv['GISDBASE'],
                            gisenv['LOCATION_NAME'],
                            gisenv['MAPSET'])
    input_base = os.path.basename(infile)
    shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
    os.chdir(tmp_dir)
    tar = tarfile.TarFile.open(name=input_base, mode='r')
    try:
        data_names = [
            tarinfo.name for tarinfo in tar.getmembers()
            if '/' not in tarinfo.name
        ]
    except:
        grass.fatal(_("Pack file unreadable"))

    if flags['p']:
        # print proj info and exit
        try:
            for fname in ['PROJ_INFO', 'PROJ_UNITS']:
                f = tar.extractfile('{}/{}'.format(data_names[0], fname))
                sys.stdout.write(f.read().decode())
        except KeyError:
            grass.fatal(_("Pack file unreadable: file '{}' missing".format(fname)))
        tar.close()

        return 0

    if options['output']:
        map_name = options['output']
    else:
        map_name = data_names[0].split('@')[0]

    gfile = grass.find_file(name=map_name, element='cell', mapset='.')
    if gfile['file']:
        if os.environ.get('GRASS_OVERWRITE', '0') != '1':
            grass.fatal(_('Raster map <{name}> already exists'.format(name=map_name)))
        else:
            grass.warning(
                _('Raster map <{name}> already exists and will be overwritten'.format(name=map_name)))

    # extract data
    tar.extractall()
    tar.close()
    os.chdir(data_names[0])

    if os.path.exists('cell'):
        pass
    elif os.path.exists('coor'):
        grass.fatal(_('This GRASS GIS pack file contains vector data. Use '
                      'v.unpack to unpack <{name}>'.format(name=map_name)))
    else:
        grass.fatal(_("Pack file unreadable"))

    # check projection compatibility in a rather crappy way

    if flags['o']:
        grass.warning(_("Overriding projection check (using current location's projection)."))

    else:

        diff_result_1 = diff_result_2 = None

        proj_info_file_1 = 'PROJ_INFO'
        proj_info_file_2 = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')

        skip_projection_check = False
        if not os.path.exists(proj_info_file_1):
            if os.path.exists(proj_info_file_2):
                grass.fatal(
                    _("PROJ_INFO file is missing, unpack raster map in XY (unprojected) location."))
            skip_projection_check = True  # XY location

        if not skip_projection_check:
            if not grass.compare_key_value_text_files(filename_a=proj_info_file_1,
                                                      filename_b=proj_info_file_2,
                                                      proj=True):
                diff_result_1 = diff_files(proj_info_file_1, proj_info_file_2)

            proj_units_file_1 = 'PROJ_UNITS'
            proj_units_file_2 = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_UNITS')

            if not grass.compare_key_value_text_files(filename_a=proj_units_file_1,
                                                      filename_b=proj_units_file_2,
                                                      units=True):
                diff_result_2 = diff_files(proj_units_file_1, proj_units_file_2)

            if diff_result_1 or diff_result_2:

                if diff_result_1:
                    grass.warning(_("Difference between PROJ_INFO file of packed map "
                                    "and of current location:\n{diff}").format(diff=''.join(diff_result_1)))
                if diff_result_2:
                    grass.warning(_("Difference between PROJ_UNITS file of packed map "
                                    "and of current location:\n{diff}").format(diff=''.join(diff_result_2)))
                grass.fatal(_("Projection of dataset does not appear to match current location."
                              " In case of no significant differences in the projection definitions,"
                              " use the -o flag to ignore them and use"
                              " current location definition."))

    maps = []
    vrt_file = None
    for index, data in enumerate(data_names):

        if index > 0:
            map_name = data

        for element in [
                'cats', 'cell', 'cellhd', 'cell_misc', 'colr', 'fcell',
                'hist'
        ]:
            src_path = os.path.join(tmp_dir, data, element)
            if not os.path.exists(src_path):
                continue

            path = os.path.join(mset_dir, element)
            if not os.path.exists(path):
                os.mkdir(path)

            if element == 'cell_misc':
                if index > 0:
                    maps.append(
                        "{map_name}@{mapset}".format(
                            map_name=map_name, mapset=gisenv['MAPSET'],
                        ),
                    )

                path = os.path.join(
                    mset_dir, element, map_name,
                )
                if index == 0:
                    vrt_file = os.path.join(path, 'vrt')

                if os.path.exists(path):
                    shutil.rmtree(path)
                shutil.copytree(src_path, path)
            else:
                shutil.copyfile(
                    src_path, os.path.join(mset_dir, element, map_name),
                )

    # Update vrt file
    if maps:
        if vrt_file and os.path.exists(vrt_file):
            files = '\n'.join(maps)
            with open(vrt_file, 'w') as f:
                f.write(files)

    grass.message(_('Raster map <{name}> unpacked'.format(name=map_name)))
Пример #14
0
def main():
    infile = options["input"]
    compression_off = flags["c"]
    mapset = None
    if "@" in infile:
        infile, mapset = infile.split("@")

    if options["output"]:
        outfile_path, outfile_base = os.path.split(
            os.path.abspath(options["output"]))
    else:
        outfile_path, outfile_base = os.path.split(
            os.path.abspath(infile + ".pack"))

    outfile = os.path.join(outfile_path, outfile_base)

    global tmp
    tmp = grass.tempdir()
    tmp_dir = os.path.join(tmp, infile)
    os.mkdir(tmp_dir)
    grass.debug("tmp_dir = %s" % tmp_dir)

    gfile = grass.find_file(name=infile, element="cell", mapset=mapset)
    if not gfile["name"]:
        grass.fatal(_("Raster map <%s> not found") % infile)

    if os.path.exists(outfile):
        if os.getenv("GRASS_OVERWRITE"):
            grass.warning(
                _("Pack file <%s> already exists and will be overwritten") %
                outfile)
            try_remove(outfile)
        else:
            grass.fatal(_("option <output>: <%s> exists.") % outfile)

    grass.message(_("Packing <%s> to <%s>...") % (gfile["fullname"], outfile))
    basedir = os.path.sep.join(
        os.path.normpath(gfile["file"]).split(os.path.sep)[:-2])
    olddir = os.getcwd()

    # copy elements
    info = grass.parse_command("r.info", flags="e", map=infile)
    vrt_files = {}
    if info["maptype"] == "virtual":
        map_file = grass.find_file(
            name=infile,
            element="cell_misc",
        )
        if map_file["file"]:
            vrt = os.path.join(map_file["file"], "vrt")
            if os.path.exists(vrt):
                with open(vrt, "r") as f:
                    for r in f.readlines():
                        map, mapset = r.split("@")
                        map_basedir = os.path.sep.join(
                            os.path.normpath(map_file["file"], ).split(
                                os.path.sep)[:-2], )
                        vrt_files[map] = map_basedir

    for element in [
            "cats",
            "cell",
            "cellhd",
            "cell_misc",
            "colr",
            "fcell",
            "hist",
    ]:
        path = os.path.join(basedir, element, infile)
        if os.path.exists(path):
            grass.debug("copying %s" % path)
            if os.path.isfile(path):
                shutil.copyfile(
                    path,
                    os.path.join(tmp_dir, element),
                )
            else:
                shutil.copytree(
                    path,
                    os.path.join(tmp_dir, element),
                )

        # Copy vrt files
        if vrt_files:
            for f in vrt_files.keys():
                f_tmp_dir = os.path.join(tmp, f)
                if not os.path.exists(f_tmp_dir):
                    os.mkdir(f_tmp_dir)
                path = os.path.join(vrt_files[f], element, f)
                if os.path.exists(path):
                    grass.debug("copying vrt file {}".format(path))
                    if os.path.isfile(path):
                        shutil.copyfile(
                            path,
                            os.path.join(f_tmp_dir, element),
                        )
                    else:
                        shutil.copytree(
                            path,
                            os.path.join(f_tmp_dir, element),
                        )

    if not os.listdir(tmp_dir):
        grass.fatal(_("No raster map components found"))

    # copy projection info
    # (would prefer to use g.proj*, but this way is 5.3 and 5.7 compat)
    gisenv = grass.gisenv()
    for support in ["INFO", "UNITS", "EPSG"]:
        path = os.path.join(gisenv["GISDBASE"], gisenv["LOCATION_NAME"],
                            "PERMANENT", "PROJ_" + support)
        if os.path.exists(path):
            shutil.copyfile(path, os.path.join(tmp_dir, "PROJ_" + support))

    # pack it all up
    os.chdir(tmp)
    if compression_off:
        tar = tarfile.TarFile.open(name=outfile_base, mode="w:")
    else:
        tar = tarfile.TarFile.open(name=outfile_base, mode="w:gz")
    tar.add(infile, recursive=True)
    if vrt_files:
        for f in vrt_files.keys():
            tar.add(f, recursive=True)

    tar.close()
    try:
        shutil.move(outfile_base, outfile)
    except shutil.Error as e:
        grass.fatal(e)

    os.chdir(olddir)

    grass.verbose(_("Raster map saved to '%s'" % outfile))
Пример #15
0
def main():
    infile = options['input']
    compression_off = flags['c']

    global basedir
    basedir = grass.tempdir()

    # check if vector map exists
    gfile = grass.find_file(infile, element='vector')
    if not gfile['name']:
        grass.fatal(_("Vector map <%s> not found") % infile)

    # check if input vector map is in the native format
    if vector.vector_info(gfile['fullname'])['format'] != 'native':
        grass.fatal(
            _("Unable to pack vector map <%s>. Only native format supported.")
            % gfile['fullname'])

    # split the name if there is the mapset name
    if infile.find('@'):
        infile = infile.split('@')[0]

    # output name
    if options['output']:
        outfile = options['output']
    else:
        outfile = infile + '.pack'

    # check if exists the output file
    if os.path.exists(outfile):
        if os.getenv('GRASS_OVERWRITE'):
            grass.warning(
                _("Pack file <%s> already exists and will be overwritten") %
                outfile)
            try_remove(outfile)
        else:
            grass.fatal(_("option <%s>: <%s> exists.") % ("output", outfile))

    # prepare for packing
    grass.verbose(_("Packing <%s>...") % (gfile['fullname']))

    # write tar file, optional compression
    if compression_off:
        tar = tarfile.open(name=outfile, mode='w:')
    else:
        tar = tarfile.open(name=outfile, mode='w:gz')
    tar.add(gfile['file'], infile)

    # check if exist a db connection for the vector
    db_vect = vector.vector_db(gfile['fullname'])
    if not db_vect:
        grass.verbose(
            _('There is not database connected with vector map <%s>') %
            gfile['fullname'])
    else:
        # for each layer connection save a table in sqlite database
        sqlitedb = os.path.join(basedir, 'db.sqlite')
        for i, dbconn in db_vect.items():
            grass.run_command('db.copy',
                              from_driver=dbconn['driver'],
                              from_database=dbconn['database'],
                              from_table=dbconn['table'],
                              to_driver='sqlite',
                              to_database=sqlitedb,
                              to_table=dbconn['table'])
        tar.add(sqlitedb, 'db.sqlite')

    # add to the tar file the PROJ files to check when unpack file
    gisenv = grass.gisenv()
    for support in ['INFO', 'UNITS', 'EPSG']:
        path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
                            'PERMANENT', 'PROJ_' + support)
        if os.path.exists(path):
            tar.add(path, 'PROJ_' + support)
    tar.close()

    grass.message(
        _("Pack file <%s> created") % os.path.join(os.getcwd(), outfile))
Пример #16
0
def main():
    infile = options['input']
    compression_off = flags['c']
    mapset = None
    if '@' in infile:
        infile, mapset = infile.split('@')

    if options['output']:
        outfile_path, outfile_base = os.path.split(os.path.abspath(options['output']))
    else:
        outfile_path, outfile_base = os.path.split(os.path.abspath(infile + ".pack"))
    
    outfile = os.path.join(outfile_path, outfile_base)
    
    global tmp
    tmp = grass.tempdir()
    tmp_dir = os.path.join(tmp, infile)
    os.mkdir(tmp_dir)
    grass.debug('tmp_dir = %s' % tmp_dir)
    
    gfile = grass.find_file(name = infile, element = 'cell', mapset = mapset)
    if not gfile['name']:
        grass.fatal(_("Raster map <%s> not found") % infile)
    
    if os.path.exists(outfile):
        if os.getenv('GRASS_OVERWRITE'):
            grass.warning(_("Pack file <%s> already exists and will be overwritten") % outfile)
            try_remove(outfile)
        else:
            grass.fatal(_("option <output>: <%s> exists.") % outfile)
    
    grass.message(_("Packing <%s> to <%s>...") % (gfile['fullname'], outfile))
    basedir = os.path.sep.join(os.path.normpath(gfile['file']).split(os.path.sep)[:-2])
    olddir  = os.getcwd()
    
    # copy elements
    for element in ['cats', 'cell', 'cellhd', 'colr', 'fcell', 'hist']:
        path = os.path.join(basedir, element, infile)
        if os.path.exists(path):
            grass.debug('copying %s' % path)
            shutil.copyfile(path,
                            os.path.join(tmp_dir, element))
            
    if os.path.exists(os.path.join(basedir, 'cell_misc', infile)):
        shutil.copytree(os.path.join(basedir, 'cell_misc', infile),
                        os.path.join(tmp_dir, 'cell_misc'))
        
    if not os.listdir(tmp_dir):
        grass.fatal(_("No raster map components found"))
                    
    # copy projection info
    # (would prefer to use g.proj*, but this way is 5.3 and 5.7 compat)
    gisenv = grass.gisenv()
    for support in ['INFO', 'UNITS', 'EPSG']:
        path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
                            'PERMANENT', 'PROJ_' + support)
        if os.path.exists(path):
            shutil.copyfile(path, os.path.join(tmp_dir, 'PROJ_' + support))
    
    # pack it all up
    os.chdir(tmp)
    if compression_off:
        tar = tarfile.TarFile.open(name = outfile_base, mode = 'w:')
    else:
        tar = tarfile.TarFile.open(name = outfile_base, mode = 'w:gz')
    tar.add(infile, recursive = True)
    tar.close()
    try:
        shutil.move(outfile_base, outfile)
    except shutil.Error as e:
        grass.fatal(e)
        
    os.chdir(olddir)
    
    grass.verbose(_("Raster map saved to '%s'" % outfile))
Пример #17
0
def main():
    infile = options['input']

    global tmp_dir
    tmp_dir = grass.tempdir()
    grass.debug('tmp_dir = {tmpdir}'.format(tmpdir=tmp_dir))

    if not os.path.exists(infile):
        grass.fatal(_('File {name} not found.'.format(name=infile)))

    gisenv = grass.gisenv()
    mset_dir = os.path.join(gisenv['GISDBASE'],
                            gisenv['LOCATION_NAME'],
                            gisenv['MAPSET'])
    input_base = os.path.basename(infile)
    shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
    os.chdir(tmp_dir)
    tar = tarfile.TarFile.open(name=input_base, mode='r')
    try:
        data_name = tar.getnames()[0]
    except:
        grass.fatal(_("Pack file unreadable"))

    if options['output']:
        map_name = options['output']
    else:
        map_name = data_name.split('@')[0]

    gfile = grass.find_file(name=map_name, element='cell', mapset='.')
    if gfile['file']:
        if os.environ.get('GRASS_OVERWRITE', '0') != '1':
            grass.fatal(_('Raster map <{name}> already exists'.format(name=map_name)))
        else:
            grass.warning(_('Raster map <{name}> already exists and will be overwritten'.format(name=map_name)))

    # extract data
    tar.extractall()
    os.chdir(data_name)

    if os.path.exists('cell'):
        pass
    elif os.path.exists('coor'):
        grass.fatal(_('This GRASS GIS pack file contains vector data. Use '
                      'v.unpack to unpack <{name}>'.format(name=map_name)))
    else:
        grass.fatal(_("Pack file unreadable"))

    # check projection compatibility in a rather crappy way

    if flags['o']:
        grass.warning(_("Overriding projection check (using current location's projection)."))
        
    else:
        
        diff_result_1 = diff_result_2 = None
        
        proj_info_file_1 = 'PROJ_INFO'
        proj_info_file_2 = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')

        skip_projection_check = False
        if not os.path.exists(proj_info_file_1):
            if os.path.exists(proj_info_file_2):
                grass.fatal(_("PROJ_INFO file is missing, unpack raster map in XY (unprojected) location."))
            skip_projection_check = True  # XY location

        if not skip_projection_check:
            if not grass.compare_key_value_text_files(filename_a=proj_info_file_1,
                                                      filename_b=proj_info_file_2,
                                                      proj=True):                                                      
                diff_result_1 = diff_files(proj_info_file_1, proj_info_file_2)
        
            proj_units_file_1 = 'PROJ_UNITS'
            proj_units_file_2 = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_UNITS')
        
            if not grass.compare_key_value_text_files(filename_a=proj_units_file_1,
                                                      filename_b=proj_units_file_2,
                                                      units=True):                                                      
                diff_result_2 = diff_files(proj_units_file_1, proj_units_file_2)
        
            if diff_result_1 or diff_result_2:
                
                if diff_result_1:
                    grass.warning(_("Difference between PROJ_INFO file of packed map "
                                    "and of current location:\n{diff}").format(diff=''.join(diff_result_1)))
                if diff_result_2:
                    grass.warning(_("Difference between PROJ_UNITS file of packed map "
                                    "and of current location:\n{diff}").format(diff=''.join(diff_result_2)))
                grass.fatal(_("Projection of dataset does not appear to match current location."
                              " In case of no significant differences in the projection definitions,"
                              " use the -o flag to ignore them and use"
                              " current location definition."))

    # install in $MAPSET
    for element in ['cats', 'cell', 'cellhd', 'cell_misc', 'colr', 'fcell', 'hist']:
        if not os.path.exists(element):
            continue
        path = os.path.join(mset_dir, element)
        if not os.path.exists(path):
            os.mkdir(path)
        if element == 'cell_misc':
            path = os.path.join(mset_dir, element, map_name)
            if os.path.exists(path):
                shutil.rmtree(path)
            shutil.copytree('cell_misc', path)
        else:
            shutil.copyfile(element, os.path.join(mset_dir, element, map_name))

    grass.message(_('Raster map <{name}> unpacked'.format(name=map_name)))
Пример #18
0
def main():
    infile = options["input"]

    global tmp_dir
    tmp_dir = grass.tempdir()
    grass.debug("tmp_dir = {tmpdir}".format(tmpdir=tmp_dir))

    if not os.path.exists(infile):
        grass.fatal(_("File {name} not found.".format(name=infile)))

    gisenv = grass.gisenv()
    mset_dir = os.path.join(gisenv["GISDBASE"], gisenv["LOCATION_NAME"],
                            gisenv["MAPSET"])
    input_base = os.path.basename(infile)
    shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
    os.chdir(tmp_dir)
    tar = tarfile.TarFile.open(name=input_base, mode="r")
    try:
        data_names = [
            tarinfo.name for tarinfo in tar.getmembers()
            if "/" not in tarinfo.name
        ]
    except:
        grass.fatal(_("Pack file unreadable"))

    if flags["p"]:
        # print proj info and exit
        try:
            for fname in ["PROJ_INFO", "PROJ_UNITS"]:
                f = tar.extractfile("{}/{}".format(data_names[0], fname))
                sys.stdout.write(f.read().decode())
        except KeyError:
            grass.fatal(
                _("Pack file unreadable: file '{}' missing".format(fname)))
        tar.close()

        return 0

    if options["output"]:
        map_name = options["output"]
    else:
        map_name = data_names[0].split("@")[0]

    gfile = grass.find_file(name=map_name, element="cell", mapset=".")
    if gfile["file"]:
        if os.environ.get("GRASS_OVERWRITE", "0") != "1":
            grass.fatal(
                _("Raster map <{name}> already exists".format(name=map_name)))
        else:
            grass.warning(
                _("Raster map <{name}> already exists and will be overwritten".
                  format(name=map_name)))

    # extract data
    tar.extractall()
    tar.close()
    os.chdir(data_names[0])

    if os.path.exists("cell"):
        pass
    elif os.path.exists("coor"):
        grass.fatal(
            _("This GRASS GIS pack file contains vector data. Use "
              "v.unpack to unpack <{name}>".format(name=map_name)))
    else:
        grass.fatal(_("Pack file unreadable"))

    # check projection compatibility in a rather crappy way

    if flags["o"]:
        grass.warning(
            _("Overriding projection check (using current location's projection)."
              ))

    else:

        diff_result_1 = diff_result_2 = None

        proj_info_file_1 = "PROJ_INFO"
        proj_info_file_2 = os.path.join(mset_dir, "..", "PERMANENT",
                                        "PROJ_INFO")

        skip_projection_check = False
        if not os.path.exists(proj_info_file_1):
            if os.path.exists(proj_info_file_2):
                grass.fatal(
                    _("PROJ_INFO file is missing, unpack raster map in XY (unprojected) location."
                      ))
            skip_projection_check = True  # XY location

        if not skip_projection_check:
            if not grass.compare_key_value_text_files(
                    filename_a=proj_info_file_1,
                    filename_b=proj_info_file_2,
                    proj=True):
                diff_result_1 = diff_files(proj_info_file_1, proj_info_file_2)

            proj_units_file_1 = "PROJ_UNITS"
            proj_units_file_2 = os.path.join(mset_dir, "..", "PERMANENT",
                                             "PROJ_UNITS")

            if not grass.compare_key_value_text_files(
                    filename_a=proj_units_file_1,
                    filename_b=proj_units_file_2,
                    units=True):
                diff_result_2 = diff_files(proj_units_file_1,
                                           proj_units_file_2)

            if diff_result_1 or diff_result_2:

                if diff_result_1:
                    grass.warning(
                        _("Difference between PROJ_INFO file of packed map "
                          "and of current location:\n{diff}").format(
                              diff="".join(diff_result_1)))
                if diff_result_2:
                    grass.warning(
                        _("Difference between PROJ_UNITS file of packed map "
                          "and of current location:\n{diff}").format(
                              diff="".join(diff_result_2)))
                grass.fatal(
                    _("Projection of dataset does not appear to match current location."
                      " In case of no significant differences in the projection definitions,"
                      " use the -o flag to ignore them and use"
                      " current location definition."))

    maps = []
    vrt_file = None
    for index, data in enumerate(data_names):

        if index > 0:
            map_name = data

        for element in [
                "cats", "cell", "cellhd", "cell_misc", "colr", "fcell", "hist"
        ]:
            src_path = os.path.join(tmp_dir, data, element)
            if not os.path.exists(src_path):
                continue

            path = os.path.join(mset_dir, element)
            if not os.path.exists(path):
                os.mkdir(path)

            if element == "cell_misc":
                if index > 0:
                    maps.append(
                        "{map_name}@{mapset}".format(
                            map_name=map_name,
                            mapset=gisenv["MAPSET"],
                        ), )

                path = os.path.join(
                    mset_dir,
                    element,
                    map_name,
                )
                if index == 0:
                    vrt_file = os.path.join(path, "vrt")

                if os.path.exists(path):
                    shutil.rmtree(path)
                shutil.copytree(src_path, path)
            else:
                shutil.copyfile(
                    src_path,
                    os.path.join(mset_dir, element, map_name),
                )

    # Update vrt file
    if maps:
        if vrt_file and os.path.exists(vrt_file):
            files = "\n".join(maps)
            with open(vrt_file, "w") as f:
                f.write(files)

    grass.message(_("Raster map <{name}> unpacked".format(name=map_name)))
Пример #19
0
def main():
    infile = options['input']
    
    global tmp_dir
    tmp_dir = grass.tempdir()
    grass.debug('tmp_dir = %s' % tmp_dir)
    
    if not os.path.exists(infile):
        grass.fatal(_("File <%s> not found" % infile))
    
    gisenv = grass.gisenv()
    mset_dir = os.path.join(gisenv['GISDBASE'],
                            gisenv['LOCATION_NAME'],
                            gisenv['MAPSET'])
    input_base = os.path.basename(infile)
    shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
    os.chdir(tmp_dir)
    tar = tarfile.TarFile.open(name = input_base, mode = 'r:gz')
    try:
        data_name = tar.getnames()[0]
    except:
        grass.fatal(_("Pack file unreadable"))
    
    if options['output']:
        map_name = options['output']
    else:
        map_name = data_name
    
    gfile = grass.find_file(name = map_name, element = 'cell',
                            mapset = '.')
    overwrite = os.getenv('GRASS_OVERWRITE')
    if gfile['file'] and overwrite != '1':
        grass.fatal(_("Raster map <%s> already exists") % map_name)
    
    # extract data
    tar.extractall()
    os.chdir(data_name)
    
    # check projection compatibility in a rather crappy way
    if not filecmp.cmp('PROJ_INFO', os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')):
        if flags['o']:
            grass.warning(_("Projection information does not match. Proceeding..."))
        else:
            grass.fatal(_("Projection information does not match. Aborting."))
    
    # install in $MAPSET
    for element in ['cats', 'cell', 'cellhd', 'cell_misc', 'colr', 'fcell', 'hist']:
        if not os.path.exists(element):
            continue
        path = os.path.join(mset_dir, element)
        if not os.path.exists(path):
            os.mkdir(path)
        if element == 'cell_misc':
            path = os.path.join(mset_dir, element, map_name)
            if os.path.exists(path):
                shutil.rmtree(path)
            shutil.copytree('cell_misc', path)
        else:
            shutil.copyfile(element, os.path.join(mset_dir, element, map_name))
    
    grass.verbose(_("Raster map saved to <%s>") % map_name)