Geospatial Modelling Environment

Strategic commands, functions and clauses

Powerful techniques to help you automate commands and process your data more efficiently

Strategic commands

There are 5 important features of GME that I strongly recommend you learn about before using GME. They will change the way you use GME, design geospatial analyses, organise your data, and share analysis ideas with others. They are fundamental tools for efficient data analysis. If you use R, no doubt you will recognize the value of these features already. While some of them can be used individually, exploiting the power of GME involves using them together. The few minutes it takes you to read this section is time well spent. I summarize the features here, but more detailed syntax help is provided for each one in the Command Reference section.

1. For loops. These are a basic programming construct that allows you to repeat one or more commands many times. An "index variable" (usually a single letter) is used to control the number of iterations. Importantly, this index variable can be used within the block of commands in the loop to alter the arguments to the commands. For loops can be nested. An example is provided below.

2. Variable assignment for simple data types. This allows you to define your own variables, and set them to equal a simple data type: text (strings), integers, double precision numbers, or one dimensional vectors of strings or numbers. This feature enables you to write easily re-usable scripts: you only need to set the variables once at the beginning of your script, and then you can refer to the variable name in all subsequent commands. One obvious way that this is useful is if you want to run the same set of commands on similar datasets in many folders, or on different drives or computers - you only need to change the path variable once rather than search and replace the incorrect path in every single command. (Also see the ls() command when using variable assignment).

3. r.eval() function. This function evaluates R code, retrieves the output of that code, and embeds it into the GME command string. This provides you with a way of linking the phenomenal power of R with GME. Sometimes the r.eval() functions is simply a matter of convenience , for instance, rather than typing a 50 value sequence by hand, e.g. c(0, 0.02, 0.04 ... 0.98, 1), we could type r.eval(seq(0,1,0.02)). More importantly, it allows you to access much more powerful R code to parameterize your GME commands. For instance, you might use the optimisation commands in R to generate a maximum likelihood parameter value.

4. paste() function. This function is similar to the R paste function. It allows you to construct strings (text) dynamically. This is particularly important within for loops. For instance, assuming we have set the variable called path (path <- "C:\data\rasters\"), and we have written a for loop as for(i in 1:3){...}, then this paste function: paste(path, "tmband", i, ".tif") evaluates to: "C:\data\rasters\tmband1.tif" in the first loop. A more complete example is provided below.

5. The "where" clause. This allows you to specify a subset of records/vector features to process based on a simple logical statement (an SQL statement). This makes data management easier (you don't have to split datasets apart to run analyses), and more importantly it provides you with an important mechanism for repeating an analysis on different subsets of data.


Here is an example that brings all these elements together. Imagine you wish to create kernel density estimates for each individual in each month, based on a database of GPS telemetry locations. In this example we assume there are 10 animals with ID numbers 1001-1010, and we want to process points for each month:

inpath <- "C:\data\"

outpath <- "C:\output\"

for (i in 1001:1010) {

for (j in 1:12) {

kde(in=paste(inpath, "telemetry.shp"), out=paste(outpath, "kde_an", i, "_m", j, ".img"), bandwidth=1000, cellsize=r.eval((250000-120000)/2000), where=paste("ANIMALID=", i, " AND MONTH=", j));



These few lines of code generate 120 kde commands. How does this work? There are two loops: one loop that corresponds to the animal ID numbers as is identified with the variable 'i', and a second nested loop corresponding to the month with variable 'j'. Note that there are three 'paste' commands embedded within the kde command, two of which use the two index variables i and j. The r.eval() statement is simplistic and contrived, but does illustrate how it can be used.

In the first iteration of the loop, i=1001 and j=1, and the resulting command becomes:

kde(in="C:\data\telemetry.shp", out="C:\output\kde_an1001_m1.img", bandwidth=1250, cellsize=65, where="ANIMALID=1001 AND MONTH=1");

The second iteration of the loop would result in:

kde(in="C:\data\telemetry.shp", out="C:\output\kde_an1001_m2.img", bandwidth=1250, cellsize=65, where="ANIMALID=1001 AND MONTH=2");

and so on, until the final iteration, which yields:

kde(in="C:\data\telemetry.shp", out="C:\output\kde_an1010_m12.img", bandwidth=1250, cellsize=65, where="ANIMALID=1010 AND MONTH=12");

Do you want even more power to construct GME scripts? Consider using R (or Python, or any other programming/scripting language) directly to write GME commands to a text file, and run them using the 'run' command. For instance, R has a vast array of commands that can be used to manipulate and create text.

Useful tip. There is one other feature of GME that is very useful if you are using any of the above strategic commands: the setparameter command (see syntax help in Command Reference section) can be used to tell GME to interpret command text but not to run it. The interpreted commands are displayed in the output window, so you can check for errors in your script.


Please consider making a purchase to support the continued development of these tools  Read more...

Tips on how to use this interface efficiently 


Open Source GIS

Copyright © 2001-2014 Hawthorne L. Beyer, Ph.D., Spatial Ecology LLC    Connect on LinkedIn