1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
"""
Raster related functions to be used in Python scripts.
Usage:
::
from grass.script import raster as grass
grass.raster_history(map)
(C) 2008-2011 by the GRASS Development Team
This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.
.. sectionauthor:: Glynn Clements
.. sectionauthor:: Martin Landa <landa.martin gmail.com>
"""
from __future__ import absolute_import
import os
import string
import time
from .core import *
from grass.exceptions import CalledModuleError
from .utils import float_or_dms, parse_key_val
try:
from builtins import unicode
bytes = str
except ImportError:
# python3
unicode = str
def raster_history(map):
"""Set the command history for a raster map to the command used to
invoke the script (interface to `r.support`).
:param str map: map name
:return: True on success
:return: False on failure
"""
current_mapset = gisenv()['MAPSET']
if find_file(name=map)['mapset'] == current_mapset:
run_command('r.support', map=map, history=os.environ['CMDLINE'])
return True
warning(_("Unable to write history for <%(map)s>. "
"Raster map <%(map)s> not found in current mapset." % { 'map': map, 'map': map}))
return False
def raster_info(map):
"""Return information about a raster map (interface to
`r.info -gre`). Example:
>>> raster_info('elevation') # doctest: +ELLIPSIS
{'creator': '"helena"', 'cols': '1500' ... 'south': 215000.0}
:param str map: map name
:return: parsed raster info
"""
def float_or_null(s):
if s == 'NULL':
return None
else:
return float(s)
s = read_command('r.info', flags='gre', map=map)
kv = parse_key_val(s)
for k in ['min', 'max']:
kv[k] = float_or_null(kv[k])
for k in ['north', 'south', 'east', 'west']:
kv[k] = float(kv[k])
for k in ['nsres', 'ewres']:
kv[k] = float_or_dms(kv[k])
return kv
def mapcalc(exp, quiet=False, verbose=False, overwrite=False,
seed=None, env=None, **kwargs):
"""Interface to r.mapcalc.
:param str exp: expression
:param bool quiet: True to run quietly (<tt>--q</tt>)
:param bool verbose: True to run verbosely (<tt>--v</tt>)
:param bool overwrite: True to enable overwriting the output (<tt>--o</tt>)
:param seed: an integer used to seed the random-number generator for the
rand() function, or 'auto' to generate a random seed
:param dict env: dictionary of environment variables for child process
:param kwargs:
"""
if seed == 'auto':
seed = hash((os.getpid(), time.time())) % (2**32)
t = string.Template(exp)
e = t.substitute(**kwargs)
try:
write_command('r.mapcalc', file='-', stdin=e, env=env, seed=seed,
quiet=quiet, verbose=verbose, overwrite=overwrite)
except CalledModuleError:
fatal(_("An error occurred while running r.mapcalc"))
def mapcalc_start(exp, quiet=False, verbose=False, overwrite=False,
seed=None, env=None, **kwargs):
"""Interface to r.mapcalc, doesn't wait for it to finish, returns Popen object.
>>> output = 'newele'
>>> input = 'elevation'
>>> expr1 = '"%s" = "%s" * 10' % (output, input)
>>> expr2 = '...' # etc.
>>> # launch the jobs:
>>> p1 = mapcalc_start(expr1)
>>> p2 = mapcalc_start(expr2)
...
>>> # wait for them to finish:
>>> p1.wait()
0
>>> p2.wait()
1
>>> run_command('g.remove', flags='f', type='raster', name=output)
:param str exp: expression
:param bool quiet: True to run quietly (<tt>--q</tt>)
:param bool verbose: True to run verbosely (<tt>--v</tt>)
:param bool overwrite: True to enable overwriting the output (<tt>--o</tt>)
:param seed: an integer used to seed the random-number generator for the
rand() function, or 'auto' to generate a random seed
:param dict env: dictionary of environment variables for child process
:param kwargs:
:return: Popen object
"""
if seed == 'auto':
seed = hash((os.getpid(), time.time())) % (2**32)
t = string.Template(exp)
e = t.substitute(**kwargs)
p = feed_command('r.mapcalc', file='-', env=env, seed=seed,
quiet=quiet, verbose=verbose, overwrite=overwrite)
p.stdin.write(e)
p.stdin.close()
return p
def raster_what(map, coord, env=None, localized=False):
"""Interface to r.what
>>> raster_what('elevation', [[640000, 228000]])
[{'elevation': {'color': '255:214:000', 'label': '', 'value': '102.479'}}]
:param str map: the map name
:param list coord: a list of list containing all the point that you want
query
:param env:
"""
if isinstance(map, (bytes, unicode)):
map_list = [map]
else:
map_list = map
coord_list = list()
if isinstance(coord, tuple):
coord_list.append('%f,%f' % (coord[0], coord[1]))
else:
for e, n in coord:
coord_list.append('%f,%f' % (e, n))
sep = '|'
# separator '|' not included in command
# because | is causing problems on Windows
# change separator?
ret = read_command('r.what',
flags='rf',
map=','.join(map_list),
coordinates=','.join(coord_list),
null=_("No data"),
quiet=True,
env=env)
data = list()
if not ret:
return data
if localized:
labels = (_("value"), _("label"), _("color"))
else:
labels = ('value', 'label', 'color')
for item in ret.splitlines():
line = item.split(sep)[3:]
for i, map_name in enumerate(map_list):
tmp_dict = {}
tmp_dict[map_name] = {}
for j in range(len(labels)):
tmp_dict[map_name][labels[j]] = line[i*len(labels)+j]
data.append(tmp_dict)
return data
|