pygrass.modules.grid package¶
Submodules¶
pygrass.modules.grid.grid module¶
-
class
pygrass.modules.grid.grid.
GridModule
(cmd, width=None, height=None, overlap=0, processes=None, split=False, debug=False, region=None, move=None, log=False, start_row=0, start_col=0, out_prefix='', mapset_prefix=None, patch_backend=None, *args, **kargs)[source]¶ Bases:
object
Run GRASS raster commands in a multiprocessing mode.
- Parameters
cmd (str) – raster GRASS command, only command staring with r.* are valid.
width (int) – width of the tile, in pixel
height (int) – height of the tile, in pixel.
overlap (int) – overlap between tiles, in pixel.
processes – number of threads, default value is equal to the number of processor available.
split (bool) – if True use r.tile to split all the inputs.
mapset_prefix (str) – if specified created mapsets start with this prefix
patch_backend (None or str) – “r.patch”, “RasterRow”, or None for for default
run (bool) – if False only instantiate the object
args – give all the parameters to the command
kargs – give all the parameters to the command
When patch_backend is None, the RasterRow method is used for patching the result. When patch_backend is “r.patch”, r.patch is used with nprocs=processes. r.patch can only be used when overlap is 0.
>>> grd = GridModule('r.slope.aspect', ... width=500, height=500, overlap=2, ... processes=None, split=False, ... elevation='elevation', ... slope='slope', aspect='aspect', overwrite=True) >>> grd.run()
Temporary mapsets created start with a generated prefix which is unique for each process (includes PID and node name). If more instances of this class are used in parallel from one process with the same module, a custom mapset_prefix needs to be provided.
-
clean_location
(location=None)[source]¶ Remove all created mapsets.
- Parameters
location (Location object) – a Location instance where we are running the analysis
-
estimate_tile_size
()[source]¶ Estimates tile width and height based on number of processes.
Keeps width and height if provided by user. If one dimension is provided the other is computed as the minimum number of tiles to keep all requested processes working (initially). If no dimensions are provided, tiling is 1 column x N rows which speeds up patching.
-
pygrass.modules.grid.grid.
cmd_exe
(args)[source]¶ Create a mapset, and execute a cmd inside.
- Parameters
args (tuple) – is a tuple that contains several information see below
- Returns
None
The puple has to contain:
bbox (dict): a dict with the region parameters (n, s, e, w, etc.) that we want to set before to apply the command.
mapnames (dict): a dictionary to substitute the input if the domain has been split in several tiles.
gisrc_src (str): path of the GISRC file from where we want to copy the groups.
gisrc_dst (str): path of the GISRC file where the groups will be created.
cmd (dict): a dictionary with all the parameter of a GRASS module.
groups (list): a list of strings with the groups that we want to copy in the mapset.
-
pygrass.modules.grid.grid.
copy_groups
(groups, gisrc_src, gisrc_dst, region=None)[source]¶ Copy group from one mapset to another, crop the raster to the region
- Parameters
groups (list of strings) – a list of strings with the group that must be copied from a master to another.
gisrc_src (str) – path of the GISRC file from where we want to copy the groups
gisrc_dst (str) – path of the GISRC file where the groups will be created
region (Region object or dictionary) – a region like object or a dictionary with the region parameters that will be used to crop the rasters of the groups
- Returns
None
-
pygrass.modules.grid.grid.
copy_mapset
(mapset, path)[source]¶ Copy mapset to another place without copying raster and vector data.
- Parameters
mapset (Mapset object) – a Mapset instance to copy
path (str) – path where the new mapset must be copied
- Returns
the instance of the new Mapset.
>>> from grass.script.core import gisenv >>> mname = gisenv()['MAPSET'] >>> mset = Mapset() >>> mset.name == mname True >>> import tempfile as tmp >>> import os >>> path = os.path.join(tmp.gettempdir(), 'my_loc', 'my_mset') >>> copy_mapset(mset, path) Mapset(...) >>> sorted(os.listdir(path)) [...'PERMANENT'...] >>> sorted(os.listdir(os.path.join(path, 'PERMANENT'))) ['DEFAULT_WIND', 'PROJ_INFO', 'PROJ_UNITS', 'VAR', 'WIND'] >>> sorted(os.listdir(os.path.join(path, mname))) [...'WIND'...] >>> import shutil >>> shutil.rmtree(path)
-
pygrass.modules.grid.grid.
copy_rasters
(rasters, gisrc_src, gisrc_dst, region=None)[source]¶ Copy rasters from one mapset to another, crop the raster to the region.
- Parameters
rasters (list) – a list of strings with the raster map that must be copied from a master to another.
gisrc_src (str) – path of the GISRC file from where we want to copy the groups
gisrc_dst (str) – path of the GISRC file where the groups will be created
region (Region object or dictionary) – a region like object or a dictionary with the region parameters that will be used to crop the rasters of the groups
- Returns
None
-
pygrass.modules.grid.grid.
copy_special_mapset_files
(path_src, path_dst)[source]¶ Copy all the special GRASS files that are contained in a mapset to another mapset
- Parameters
path_src (str) – the path to the original mapset
path_dst (str) – the path to the new mapset
-
pygrass.modules.grid.grid.
copy_vectors
(vectors, gisrc_src, gisrc_dst)[source]¶ Copy vectors from one mapset to another, crop the raster to the region.
- Parameters
vectors (list) – a list of strings with the vector map that must be copied from a master to another.
gisrc_src (str) – path of the GISRC file from where we want to copy the groups
gisrc_dst (str) – path of the GISRC file where the groups will be created
- Returns
None
-
pygrass.modules.grid.grid.
get_cmd
(cmdd)[source]¶ Transform a cmd dictionary to a list of parameters. It is useful to pickle a Module class and cnvert into a string that can be used with Popen(get_cmd(cmdd), shell=True).
- Parameters
cmdd (dict) – a module dictionary with all the parameters
>>> slp = Module('r.slope.aspect', ... elevation='ele', slope='slp', aspect='asp', ... overwrite=True, run_=False) >>> get_cmd(slp.get_dict()) ['r.slope.aspect', 'elevation=ele', 'format=degrees', ..., '--o']
-
pygrass.modules.grid.grid.
get_mapset
(gisrc_src, gisrc_dst)[source]¶ Get mapset from a GISRC source to a GISRC destination.
- Parameters
gisrc_src (str) – path to the GISRC source
gisrc_dst (str) – path to the GISRC destination
- Returns
a tuple with Mapset(src), Mapset(dst)
-
pygrass.modules.grid.grid.
read_gisrc
(gisrc)[source]¶ Read a GISRC file and return a tuple with the mapset, location and gisdbase.
- Parameters
gisrc (str) – the path to GISRC file
- Returns
a tuple with the mapset, location and gisdbase
>>> import os >>> from grass.script.core import gisenv >>> genv = gisenv() >>> (read_gisrc(os.environ['GISRC']) == (genv['MAPSET'], ... genv['LOCATION_NAME'], ... genv['GISDBASE'])) True
-
pygrass.modules.grid.grid.
select
(parms, ptype)[source]¶ Select only a certain type of parameters.
- Parameters
parms (DictType parameters) – a DictType parameter with inputs or outputs of a Module class
ptype (str) – String define the type of parameter that we want to select, valid ptype are: ‘raster’, ‘vector’, ‘group’
- Returns
An iterator with the value of the parameter.
>>> slp = Module('r.slope.aspect', ... elevation='ele', slope='slp', aspect='asp', ... run_=False) >>> for rast in select(slp.outputs, 'raster'): ... print(rast) ... slp asp
-
pygrass.modules.grid.grid.
set_region
(region, gisrc_src, gisrc_dst, env)[source]¶ Set a region into two different mapsets.
- Parameters
region (Region object or dictionary) – a region like object or a dictionary with the region parameters that will be used to crop the rasters of the groups
gisrc_src (str) – path of the GISRC file from where we want to copy the groups
gisrc_dst (str) – path of the GISRC file where the groups will be created
env –
- Returns
None
pygrass.modules.grid.patch module¶
Created on Tue Apr 2 18:57:42 2013
@author: pietro
-
pygrass.modules.grid.patch.
get_start_end_index
(bbox_list)[source]¶ Convert a Bounding Box to a list of the index of column start, end, row start and end :param bbox_list: a list of BBox object to convert :type bbox_list: list of BBox object
Deprecated since version 8.3.
-
pygrass.modules.grid.patch.
rpatch_map
(raster, mapset, mset_str, bbox_list, overwrite=False, start_row=0, start_col=0, prefix='')[source]¶ Patch raster using a bounding box list to trim the raster.
- Parameters
raster (str) – the name of output raster
mapset (str) – the name of mapset to use
mset_str (str) –
bbox_list (list of BBox object) – a list of BBox object to convert
overwrite (bool) – overwrite existing raster
start_row (int) – the starting row of original raster
start_col (int) – the starting column of original raster
prefix (str) – the prefix of output raster
-
pygrass.modules.grid.patch.
rpatch_map_r_patch_backend
(raster, mset_str, bbox_list, overwrite=False, start_row=0, start_col=0, prefix='', processes=1)[source]¶ Patch raster using a r.patch. Only use with overlap=0. Will be faster than rpatch_map, since r.patch is parallelized.
- Parameters
raster (str) – the name of output raster
mset_str (str) –
bbox_list (list of BBox object) – a list of BBox object to convert
overwrite (bool) – overwrite existing raster
start_row (int) – the starting row of original raster
start_col (int) – the starting column of original raster
prefix (str) – the prefix of output raster
processes (int) – number of parallel process for r.patch
pygrass.modules.grid.split module¶
Created on Tue Apr 2 19:00:15 2013
@author: pietro
-
pygrass.modules.grid.split.
get_bbox
(reg, row, col, width, height, overlap)[source]¶ Return a Bbox
- Parameters
reg (Region object) – a Region object to split
row (int) – the number of row
col (int) – the number of row
width (int) – the width of tiles
height (int) – the width of tiles
overlap (int) – the value of overlap between tiles
-
pygrass.modules.grid.split.
get_overlap_region_tiles
(region=None, width=100, height=100, overlap=0)[source]¶ Get the Bbox of the overlapped region.
- Parameters
region (Region object) – a Region object to split
width (int) – the width of tiles
height (int) – the width of tiles
overlap (int) – the value of overlap between tiles
-
pygrass.modules.grid.split.
get_tile_start_end_row_col
(reg, row, col, width, height)[source]¶ Return a tile’s starting and ending row and col
- Parameters
reg (Region object) – a Region object to split
row (int) – the number of tiles in a row
col (int) – the number of tiles in a col
width (int) – the width of tiles
height (int) – the width of tiles
-
pygrass.modules.grid.split.
split_region_in_overlapping_tiles
(region=None, width=100, height=100, overlap=0)[source]¶ Split a region into a list of overlapping tiles defined as (N, S, E, W).
- Parameters
region (Region object) – a Region object to split
width (int) – the width of tiles
height (int) – the width of tiles
overlap (int) – the value of overlap between tiles
>>> reg = Region() >>> reg.north = 1350 >>> reg.south = 0 >>> reg.nsres = 1 >>> reg.east = 1500 >>> reg.west = 0 >>> reg.ewres = 1 >>> reg.cols 1500 >>> reg.rows 1350 >>> split_region_in_overlapping_tiles(region=reg, width=1000, height=700, overlap=0) [[Bbox(1350.0, 650.0, 1000.0, 0.0), Bbox(1350.0, 650.0, 1500.0, 1000.0)], [Bbox(650.0, 0.0, 1000.0, 0.0), Bbox(650.0, 0.0, 1500.0, 1000.0)]] >>> split_region_in_overlapping_tiles(region=reg, width=1000, height=700, overlap=10) [[Bbox(1350.0, 640.0, 1010.0, 0.0), Bbox(1350.0, 640.0, 1500.0, 990.0)], [Bbox(660.0, 0.0, 1010.0, 0.0), Bbox(660.0, 0.0, 1500.0, 990.0)]]
-
pygrass.modules.grid.split.
split_region_tiles
(region=None, width=100, height=100)[source]¶ Split a region into a list of tiles defined as (start_row, end_row, start_col, end_col).
- Parameters
region (Region object) – a Region object to split
width (int) – the width of tiles
height (int) – the width of tiles