def main(): options, flags = gscript.parser() input = options['input'] output = options['output'] if (input is None or input == ""): gscript.error(_("[h.out] ERROR: input is a mandatory parameter.")) exit() exists = False maps_list = utils.findmaps(type='raster') for map in maps_list: if input == map[0]: exists = True break if (not exists): gscript.error(_("[h.out] ERROR: could not find input map.")) exit() if (output is None or output == ""): gscript.error(_("[h.out] ERROR: output is a mandatory parameter.")) exit() rast = raster.RasterRow(input) # Set region (note that it is tricking GRASS to think it is a squared raster) info = gscript.read_command('r.info', map=input, flags='g') info = info.split("\n") print(info[6]) hexASCII = HASC() # This can probably be set from the RasterRow object hexASCII.init( int(info[7].split("=")[1]), #ncols, int(info[6].split("=")[1]), #nrows, int(info[3].split("=")[1]), #xll, int(info[1].split("=")[1]), #yll, "NA") #nodata) r = 0 rast.open() for row in rast: for c in range(0, rast.info.cols): hexASCII.set(c, r, row[c]) gscript.message(_("[h.in] DEBUG: Exporting row: %s" % newRow)) r = r + 1 gscript.message(_("[h.out] SUCCESS: HexASCII raster exported."))
def copy_groups(groups, gisrc_src, gisrc_dst, region=None): """Copy group from one mapset to another, crop the raster to the region :param groups: a list of strings with the group that must be copied from a master to another. :type groups: list of strings :param gisrc_src: path of the GISRC file from where we want to copy the groups :type gisrc_src: str :param gisrc_dst: path of the GISRC file where the groups will be created :type gisrc_dst: str :param region: a region like object or a dictionary with the region parameters that will be used to crop the rasters of the groups :type region: Region object or dictionary :returns: None """ def rmloc(r): return r.split("@")[0] if "@" in r else r env = os.environ.copy() # instantiate modules get_grp = Module("i.group", flags="lg", stdout_=sub.PIPE, run_=False) set_grp = Module("i.group") get_grp.run_ = True src = read_gisrc(gisrc_src) dst = read_gisrc(gisrc_dst) rm = True if src[2] != dst[2] else False all_rasts = [ r[0] for r in findmaps("raster", location=dst[1], gisdbase=dst[2]) ] for grp in groups: # change gisdbase to src env["GISRC"] = gisrc_src get_grp(group=grp, env_=env) rasts = [r for r in get_grp.outputs.stdout.split()] # change gisdbase to dst env["GISRC"] = gisrc_dst rast2cp = [r for r in rasts if rmloc(r) not in all_rasts] if rast2cp: copy_rasters(rast2cp, gisrc_src, gisrc_dst, region=region) set_grp( group=grp, input=[rmloc(r) for r in rasts] if rast2cp or rm else rasts, env_=env, )
def copy_groups(groups, gisrc_src, gisrc_dst, region=None): """Copy group from one mapset to another, crop the raster to the region :param groups: a list of strings with the group that must be copied from a master to another. :type groups: list of strings :param gisrc_src: path of the GISRC file from where we want to copy the groups :type gisrc_src: str :param gisrc_dst: path of the GISRC file where the groups will be created :type gisrc_dst: str :param region: a region like object or a dictionary with the region parameters that will be used to crop the rasters of the groups :type region: Region object or dictionary :returns: None """ env = os.environ.copy() # instantiate modules get_grp = Module('i.group', flags='lg', stdout_=sub.PIPE, run_=False) set_grp = Module('i.group') get_grp.run_ = True rmloc = lambda r: r.split('@')[0] if '@' in r else r src = read_gisrc(gisrc_src) dst = read_gisrc(gisrc_dst) rm = True if src[2] != dst[2] else False all_rasts = [r[0] for r in findmaps('raster', location=dst[1], gisdbase=dst[2])] for grp in groups: # change gisdbase to src env['GISRC'] = gisrc_src get_grp(group=grp, env_=env) rasts = [r for r in get_grp.outputs.stdout.split()] # change gisdbase to dst env['GISRC'] = gisrc_dst rast2cp = [r for r in rasts if rmloc(r) not in all_rasts] if rast2cp: copy_rasters(rast2cp, gisrc_src, gisrc_dst, region=region) set_grp(group=grp, input=[rmloc(r) for r in rasts] if rast2cp or rm else rasts, env_=env)