[GRASS-user] Getting Started with GRASS Python Scripts
Martin Lacayo
mlacayo at stanford.edu
Fri May 24 12:06:23 PDT 2013
Hi everyone,
Thank you for all the replies. Based on the information they contained
and piecing together a few other things I put together the following
script mostly as a learning exercise and to summarize for the thread.
Any suggestions would be welcomed. The complete setup and removal of
directories is not working, but for the moment this is sufficient for
me.
-Martin
import sys
import os
import random, string
def random_string(length):
return ''.join(random.choice(string.lowercase) for i in range(length))
#add the path to GRASS
sys.path.append('/usr/lib/grass64/etc/python')
import grass.script
import grass.script.setup
class grasswrapper():
def __init__(self,
dbBase='',
location='',
mapset=''):
self.gisbase = os.environ['GISBASE']
self.gisdb = dbBase
self.loc = location
self.mapset = mapset
grass.script.setup.init(self.gisbase,
self.gisdb,
self.loc,
self.mapset)
if __name__ == '__main__':
#get raster path from first parameter
dataset_uri = sys.argv[1]
#get user's home directory
home = os.path.expanduser("~")
random_name = random_string(6)
#determine GRASS database path
temp_uri = grass.script.parse_command('g.tempfile',
pid = 1).keys()[0]
database_uri = temp_uri.split('.tmp')[0]
location_uri = os.path.join(database_uri, random_name)
#create location with raster's reference system
print 'Creating location %s with raster\'s reference system' % location_uri
grass.script.run_command('g.proj',
'c',
georef = dataset_uri,
location = random_name)
#import raster into GRASS
print 'Importing raster from ', dataset_uri
grass.script.run_command('r.in.gdal',
input = dataset_uri,
output = random_name)
#export raster from GRASS
dataset_out_uri = os.path.join(home,
"%s.tif" % random_name)
print 'Saving raster to ', dataset_out_uri
grass.script.run_command('r.out.tiff',
input = 'dem',
output = dataset_out_uri)
#remove imported raster from GRASS
print 'Removing imported raster.'
grass.script.run_command('g.remove',
rast = random_name)
#remove the location from disk
print 'Removing location %s' % location_uri
os.system("rm -rf %s" % location_uri)
On Thu, May 23, 2013 at 1:49 PM, Martin Lacayo <mlacayo at stanford.edu> wrote:
> Hello,
>
> I am having trouble getting started with GRASS Python scripts. I am
> using GRASS 6.4 on Debian. Could some provide a basic example of a
> Python script that imports a raster and exports the same raster? In
> particular I am interested in seeing the entire setup of a database
> (locally on disk), location, and mapset from start to finish. Every
> example I have found starts with an existing database, location, and
> mapset. I have been able to get past this part by using the GUI, but I
> need to be able to script their creation.
>
> Have I understood correctly that a database can only contain one
> projection, which is set when it is first created, and that besides
> importing and exporting data all processing has to happen with data
> that is in the database?
>
> My ultimate goal is to decrease our development time for our open
> source ecosystem service tools: http://invest-natcap.googlecode.com,
> but more specifically at the moment I am trying to use r.viewshed so I
> do not have to write my own.
>
> Is it possible to script the creation of a database (locally on disk),
> location, and mapset? And how would you remove them afterwards?
>
> Any help or references would be very appreciated.
>
> Thank you,
> Martin
More information about the grass-user
mailing list