Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code to replicate a bug in grass.pipe_command
import os
import tempfile
import shutil
import grass.script as grass
import grass.script.setup as gsetup
# first do:
# mkdir /tmp/grasstest
# wget http://grass.osgeo.org/sampledata/north_carolina/nc_spm_latest.zip
# unzip *.zip
GIS_BASE = '/usr/lib/grass64/'
GRASS_DB = '/tmp/grasstest/'
loc_name = 'nc_spm_08'
rast_name = 'elev_ned_30m'
def export(loc, name, out_fn='/tmp/exported.tif'):
gsetup.init(GIS_BASE, 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()
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)
run()
@wenzeslaus
Copy link

wenzeslaus commented Jun 11, 2015

Since this can pop out in some search, it might be useful to note that the problem was not in pipe_command but in subprocess call hitting operating system limit for environment + parameters. The environment (env vars + their values) was full of paths accumulated from gsetup.init calls.

The solution is not to call gsetup.init repetitively (it is enough just to all it once and then switch Mapsets if needed). See also https://trac.osgeo.org/grass/ticket/2468 and https://gist.github.com/max-nova/487a82de00651a33f2c2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment