~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/python/raster.py

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""!@package grass.script.raster
2
 
 
3
 
@brief GRASS Python scripting module (raster functions)
4
 
 
5
 
Raster related functions to be used in Python scripts.
6
 
 
7
 
Usage:
8
 
 
9
 
@code
10
 
from grass.script import raster as grass
11
 
 
12
 
grass.raster_history(map)
13
 
...
14
 
@endcode
15
 
 
16
 
(C) 2008-2009 by the GRASS Development Team
17
 
This program is free software under the GNU General Public
18
 
License (>=v2). Read the file COPYING that comes with GRASS
19
 
for details.
20
 
 
21
 
@author Glynn Clements
22
 
@author Martin Landa <landa.martin gmail.com>
23
 
"""
24
 
 
25
 
import os
26
 
import string
27
 
 
28
 
from core import *
29
 
 
30
 
# i18N
31
 
import gettext
32
 
gettext.install('grasslibs', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
33
 
 
34
 
# add raster history
35
 
 
36
 
def raster_history(map):
37
 
    """!Set the command history for a raster map to the command used to
38
 
    invoke the script (interface to `r.support').
39
 
 
40
 
    @param map map name
41
 
 
42
 
    @return True on success
43
 
    @return False on failure
44
 
    """
45
 
    current_mapset = gisenv()['MAPSET']
46
 
    if find_file(name = map)['mapset'] == current_mapset:
47
 
        run_command('r.support', map = map, history = os.environ['CMDLINE'])
48
 
        return True
49
 
    
50
 
    warning(_("Unable to write history for <%(map)s>. "
51
 
              "Raster map <%(map)s> not found in current mapset." % { 'map' : map, 'map' : map}))
52
 
    return False
53
 
 
54
 
# run "r.info -rgstmpud ..." and parse output
55
 
 
56
 
def raster_info(map):
57
 
    """!Return information about a raster map (interface to
58
 
    `r.info'). Example:
59
 
 
60
 
    \code
61
 
    >>> grass.raster_info('elevation')
62
 
    {'north': 228500.0, 'timestamp': '"none"', 'min': 55.578792572021499,
63
 
    'datatype': 'FCELL', 'max': 156.32986450195301, 'ewres': 10.0,
64
 
    'vertical_datum': '', 'west': 630000.0, 'units': '',
65
 
    'title': 'South-West Wake county: Elevation NED 10m (elev_ned10m)',
66
 
    'east': 645000.0, 'nsres': 10.0, 'south': 215000.0}
67
 
    \endcode
68
 
 
69
 
    @param map map name
70
 
    
71
 
    @return parsed raster info
72
 
    """
73
 
 
74
 
    def float_or_null(s):
75
 
        if s == 'NULL':
76
 
            return None
77
 
        else:
78
 
            return float(s)
79
 
 
80
 
    s = read_command('r.info', flags = 'rgstmpud', map = map)
81
 
    kv = parse_key_val(s)
82
 
    for k in ['min', 'max']:
83
 
        kv[k] = float_or_null(kv[k])
84
 
    for k in ['north', 'south', 'east', 'west']:
85
 
        kv[k] = float(kv[k])
86
 
    for k in ['nsres', 'ewres']:
87
 
        kv[k] = float_or_dms(kv[k])
88
 
    return kv
89
 
 
90
 
# interface to r.mapcalc
91
 
 
92
 
def mapcalc(exp, quiet = False, verbose = False, overwrite = False, **kwargs):
93
 
    """!Interface to r.mapcalc.
94
 
 
95
 
    @param exp expression
96
 
    @param kwargs
97
 
    """
98
 
    t = string.Template(exp)
99
 
    e = t.substitute(**kwargs)
100
 
 
101
 
    env = os.environ.copy()
102
 
    if quiet:
103
 
        env['GRASS_VERBOSE'] = '0'
104
 
    if verbose:
105
 
        env['GRASS_VERBOSE'] = '3'
106
 
    if overwrite:
107
 
        env['GRASS_OVERWRITE'] = '1'
108
 
 
109
 
    if write_command('r.mapcalc', stdin = e, env = env) != 0:
110
 
        fatal(_("An error occurred while running r.mapcalc"))
111
 
 
112
 
 
113
 
def mapcalc_start(exp, quiet = False, verbose = False, overwrite = False, **kwargs):
114
 
    """!Interface to r.mapcalc, doesn't wait for it to finish, returns Popen object.
115
 
 
116
 
    \code
117
 
    >>> expr1 = '"%s" = "%s" * 10' % (output, input)
118
 
    >>> expr2 = '...'   # etc.
119
 
    >>> # launch the jobs:
120
 
    >>> p1 = grass.mapcalc_start(expr1)
121
 
    >>> p2 = grass.mapcalc_start(expr2)   # etc.
122
 
    ...
123
 
    >>> # wait for them to finish:
124
 
    >>> p1.wait()
125
 
    >>> p2.wait()   # etc.
126
 
    \endcode
127
 
 
128
 
    @param exp expression
129
 
    @param kwargs
130
 
    
131
 
    @return Popen object
132
 
    """
133
 
    t = string.Template(exp)
134
 
    e = t.substitute(**kwargs)
135
 
 
136
 
    env = os.environ.copy()
137
 
    if quiet:
138
 
        env['GRASS_VERBOSE'] = '0'
139
 
    if verbose:
140
 
        env['GRASS_VERBOSE'] = '3'
141
 
    if overwrite:
142
 
        env['GRASS_OVERWRITE'] = '1'
143
 
 
144
 
    stdin = e
145
 
    p = feed_command('r.mapcalc', env = env, **kwargs)
146
 
    p.stdin.write(stdin)
147
 
    p.stdin.close()
148
 
 
149
 
    return p