Created
November 2, 2014 17:37
-
-
Save max-nova/487a82de00651a33f2c2 to your computer and use it in GitHub Desktop.
Work-around for http://trac.osgeo.org/grass/ticket/2468
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import tempfile | |
import shutil | |
import grass.script as grass | |
# first do: | |
# mkdir /tmp/grasstest | |
# wget http://grass.osgeo.org/sampledata/north_carolina/nc_spm_latest.zip | |
# unzip *.zip | |
GRASS_DB = '/tmp/grasstest/' | |
loc_name = 'nc_spm_08' | |
rast_name = 'elev_ned_30m' | |
def set_gisrc(grass_db, location, mapset): | |
fd, gisrc = tempfile.mkstemp() | |
os.write(fd, "GISDBASE: %s\n" % grass_db) | |
os.write(fd, "LOCATION_NAME: %s\n" % location) | |
os.write(fd, "MAPSET: %s\n" % mapset) | |
os.close(fd) | |
os.environ['GISRC'] = gisrc | |
return gisrc | |
def export(loc, name, out_fn='/tmp/exported.tif'): | |
gisrc = set_gisrc(GRASS_DB, loc, 'PERMANENT') | |
cmd = 'r.out.gdal' | |
kwargs = { | |
'quiet': True, | |
'flags': 'c', | |
'input': name, | |
'output': out_fn | |
} | |
process = grass.pipe_command(cmd, **kwargs) | |
stdout, stderr = process.communicate() | |
os.remove(gisrc) | |
def run(): | |
i = 0 | |
while True: | |
i += 1 | |
print 'Iteration %s' % i | |
work_dir = tempfile.mkdtemp() | |
out_fn = os.path.join(work_dir, 'rast.tif') | |
export(loc_name, rast_name, out_fn) | |
shutil.rmtree(work_dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since this can pop out in some search, it might be useful to note that unless you are switching Mapset (
GISDBASE/LOCATION_NAME/MAPSET
) there is no need for repetitive setting ofGISRC
variable and file. It this case the repetitive calls are just an overhead but in case ofgrass.script.init
function it can lead to issues (http://trac.osgeo.org/grass/ticket/2468) but it is important to note that usinginit
function repetitively (https://gist.github.com/max-nova/5cd876bc7bfefe5b7c9e) is a misuse of API and the question is what we are trying to achieve.It might be also useful to mention that
grass.script.pipe_command
function accepts Popen's env parameter (https://docs.python.org/2/library/subprocess.html#subprocess.Popen) which can be used to pass a custom environment to the subprocess without changing environment of the calling process or other called subprocesses. The code would be something like:Just to follow what it in the sample code, I would suggest to use an object of a custom class to keep the environment in between calls of
export
which would be a method of this class.The very recent development version of GRASS GIS also has improved
grass.script.setup
module withinit
andwrite_gisrc
functions and documentation (http://grass.osgeo.org/grass71/manuals/libpython/script.html#module-script.setup). But again, it's important to know what is the goal. Perhaps even the latest addition to thegrass
command might be useful (https://trac.osgeo.org/grass/ticket/2579, http://grass.osgeo.org/grass71/manuals/grass7.html#exec-interface-example).