Posts Tagged ‘raster’

Import worldclim data into GRASS

This post is about how to import worldclim data sets into a GRASS project.

WorldClim – Global Climate Data WorldClim is a set of global climate layers (climate grids) with a spatial resolution of about 1 square kilometer. The data can be used for mapping and spatial modeling in a GIS or with other computer programs.

First you need to create a location using the EPSG code corrresponding to the worldclim data. It is 4326. You can find a list of the EPSG codes at

Once the location has been created you can launch GRASS with the location and import worldclim data sets. Worldclim offers different resolutions and you have to choose one of them. Then, define the g.region in GRASS using the selected resolution. For instance, we want data at a resolution of 00:02:30.
In GRASS type:

g.region n=90N s=90S e=180E w=180W res=00:02:30

This matches with the extent of the worldclim data and the desired resolution.

The last step is pretty straightforward. Select the directory where the worldclim data have been downloaded.
For instance:

cd /home/rossi/data/worldclim/ 

Then import the data (here we import the Altitude layer) -s input=alt.bil output=altitude bytes=2 north=90 south=-60 east=180 west=-180 rows=3600 cols=8640 anull=-9999 

Note that the number of columns and rows as well as the east, west, north and south limits are given in the *.bil file available in the download from

It is convenient to import automatically a set of layers using a loop :

for i in $(seq 1 19)
do -s input=bio$i.bil output=bio$i bytes=2 north=90 south=-60 east=180 west=-180 rows=3600 cols=8640 anull=-9999

In that example, we have imported the 19 layers of the BIOCLIM variables using a loop and the GRASS function

Now the worldclim data are available within our GRASS location. These raster layers can be used visualisation, analyses and modelling with GRASS but also using R (see the post “Playing with R within a GRASS environment” from this blog).

Have fun!


Playing with R within a GRASS environment

14 August 2012 1 comment

Although there are some powerful GIS utilities in R, I prefer using GRASS to manage my GIS data while I use R to perform scientific computing.

In fact GRASS and R can be very simply interfaced by means of the R package spgrass6.

Under linux operating system (actually Kubuntu Natty Narwhal) I open the console, launch GRASS by typing grass and then select a location (see GRASS manual for details).


Once GRASS is running, R can be launched from within the GRASS console…

From now on I am working in R and I can use the library spgrass6 in order e.g. to read or write rasters into the GRASS system.

As an example we will consider the case of the pine shoot beetle Tomicus piniperda (Coleoptera: Curculionidae: Scolytinae). Readers can find the whole story in Horn et al. (2012) available here. Tomicus piniperdaT. piniperda is present throughout Europe. Interestingly, it has long been assumed to be present in North Africa too although molecular studies have recently shown that T. piniperda only rarely occurs in these regions (Horn et al., 2006, 2009).

We have a set of localities where T. piniperda was recorded as present or, on the contrary, has been searched and was absent (true absence). GRASS is used to host the data and produce the graphical outputs.

Sites where T. piniperda was either present (black circle) or absent (open circle).

Starting R from the GRASS console and using the dismo package allows us to fit a SDM (Species Distribution Model) and build a raster with the probabilities of presence e.g. using the function predict. This raster layer can be expressed as presence/absence and the resulting data written within the GRASS system using the function writeRAST6 from package spgrass6.
The raster can now be plotted using the GRASS interface and its utilities.

GRASS window showing the occurences of T. piniperda and the associated predicted distribution derived from a SDM run in R

Horn, A., Kerdelhué, C., Lieutier, F., Rossi, J.-P. (2012). Predicting the distribution of the two bark beetles Tomicus destruens and Tomicus piniperda in Europe and the Mediterranean region. Agricultural and Forest Entomology, in press, DOI: 10.1111/j.1461-9563.2012.00576.x.

Horn, A., Roux-Morabito, G., Lieutier, F. & Kerdelhué, C. (2006) Phylogeographic structure and past history of the circum-Mediterranean species Tomicus destruens Woll. (Coleoptera: Scolytinae). Molecular Ecology, 15, 1603–1615.

Horn, A., Stauffer, C., Lieutier, F. & Kerdelhué, C. (2009) Complex postglacial history of the temperate bark beetle Tomicus piniperda L. (Coleoptera, Scolytinae). Heredity, 103, 238–247.