[Qgis-developer] How to turn GRASS commands from tabs to a script? (where is toolbox metadata located?)

Stefan Keller sfkeller at gmail.com
Sun Mar 14 12:25:01 EDT 2010


Many thanks to Micha and Stefan!

I'm a GRASS and QGIS noob using GRASS shell (text interface) was what
I thought of initially.
@Micha Silver: I hope that's what you mean by [GRASS commands in the
Toolbox "shell"]?

I'm looking for a generic solution which works the same on Unix and
Windows. (Btw. Cygwin is no option for productive use and delivery -
serious cross-platform project must offer standalone executables).

Still, I am little bit unsettled since I thought it would be easier to
automate a tool like GRASS which is text command based...

See below my solution so far. It's not yet really working because it
seems that only r.in.* commands put itself into the comments section
beginning with line 9 (in hist subdirectory of a GRASS mapset).

Any GRASS wizards around?

-S.

--- FILE: gen-grass-shell-script.py ---
#  gen-grass-shell-script.py
#
# Generates a GRASS shell script from GRASS raster layer descriptions
(to be run from GRASS shell)
#
# Created 2010 by SFK, www.gis.hsr.ch. LGPL.
#
# USAGE:
#  1. from OS shell: > python gen-grass-shell-script.py > gen-mylayers.sh
#  2. edit gen-mylayers.sh if needed
#  3. start GRASS shell (text interface): sh gen-mylayers.sh
#
# TODO: extract commands like aspect from line 8 or so.
# TODO: Cleanup code.

import glob
from datetime import date
import os

# *** edit these constants first **
GISDBASE = d:/gis/labs/raster_lab/grassdata/'
LOCATION = 'gaemse'
MAPSET = 'gaemse'
# *** end of user edit block **

print "#!/bin/sh"
print "# Script generated on " + date.today().strftime("%d %b %Y")
print "# Location: " + LOCATION + ", Map: " + MAPSET
print
for filepath in glob.glob(GISDBASE + LOCATION + '/' + MAPSET + '/hist/*'):
    f = open(filepath)
    i = 0;
    cmd = "";
    for line in f:
        i = i + 1
        line = line.rstrip('\n')
        if (i == 8):
            generatedby = line
        if (i >= 9):
            if (line == ""):
                continue
            if (line[-1] == "\\"):
                cmd = cmd + line[:-1]
            else:
                cmd = cmd + line + " "
    if (i >= 8):
        print "# Layer " + os.path.basename(filepath)
        print "# " + generatedby
        print cmd
        print

# Example output:
# > python gen-grass-shell-script.py > gen-mylayers.sh
# #!/bin/sh
# # Script generated on 14 Mar 2010
# # Location: gaemse, Map: gaemse
#
# # Layer as85
# # generated by r.in.gdal
# r.in.gdal -o input="d:/gis/labs/raster_lab/datasource/as85.txt" output="as85"
#
# # Layer aspect
# # generated by r.slope.aspect
# aspect map elev = my_dhm at user1 zfactor = 1.00 min_slp_allowed = 0.000000
#  ...
---


2010/3/13 Stefan Kiefer <st_kiefer at web.de>:
> What about using grass directly? IF you work with unix like OS you simply
> can write a script for automatisation (i.e. with perl, python or
> shell-script). I think there is no need for bothering qgis for your special
> needs. I belive under Windows is the same because there you also should have
> installed Cygwin when Grass is installed.
>
> cheers
>
> Stefan
>
> Am Freitag, den 12.03.2010, 16:34 +0100 schrieb Stefan Keller:
>
> Dear all
> I'm analyzing some data with grass within qgis. Now there's the grass
> plugin and it's toolbox. This opens nicely a new tab when issueing a
> new grass command, like r.in.ascii, then r.reclass, etc. There's also
> a "Browser" which displays every command as comment (e.g. aspect map
> elev = dhm at mymap
>  zfactor = 1.00 min_slp_allowed = 0.000000").
> Now I want to automate this series of commands using a script (or a
> batch command)!
> How do I do that easiest?
> My approach would be to fetch the comment data from the GRASS plugin
> toolbox browser - but I can't find the location where this metadata is
> stored?
> -S.
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
>


More information about the Qgis-developer mailing list