Tag Archives: scripting

Creating a raster layer with a weighted random sample of points (or, my first attempt to create a python script)

I needed to create a raster map layer with a weighted random sample of all raster cells, using the percentage of crop land as weight. I couldn’t find a function to create such a weighted sample, so I decided to create a script to do this for me. Continue reading

Multivariate Environmental Similarity Surfaces (MESS) index in GRASS GIS

The Multivariate Environmental Similarity Surfaces (MESS) is an index that represents how similar a point in space is to a reference set of points, with respect to a set of predictor variables (Elith et al 2010). The function was first implemented as part of the Maxent software package, and is also available in the dismo package for R (see also here and here).

The latter works well on small and medium sized data sets. However, they take a long time to run on larger data sets, e.g., when working with 1km² raster grids covering eastern Africa. I therefore wrote a small R script to compute MESS in GRASS GIS. Continue reading

Start R inside GRASS but keep the terminal prompt

I often run R from within GRASS GIS. For example using R as a scripting language to automate GIS analysis. Or to use statistical models in R on spatial data in GRASS.

I used to open R in the terminal. However, I am using more and more RStudio instead. This has one drawback. When opening RStudio in the terminal (or any other program for that matter), you cannot use that command prompt anymore. Continue reading

R/GRASS connection: more than the sum of its parts

GRASS GIS is a very powerfull GIS offering an extensive set of tools for geospatial data management and analysis, image processing, and spatial modeling. The possibility to directly interact with R further improves the geospatial analysis capabilities of GRASS (see R Spatial View). Continue reading

Finding and renaming long file names with R

I have never given it a though, but there is apparently a limit to the length of file names that can be handled by Windows Explorer of Windows XP. I found out when trying to copy my Zotero folder to a computer with Windows XP. I was getting error message of folders that could not be copied, suggesting that my HD was full. I checked a few of these folders and they all contained one or more files with very long file names (>250 characters). Continue reading

Using the GRASS command g.mlist in R

In this post I was trying to find a way to get a list of GRASS data layers in R. The problem is running the GRASS function ‘g.list’ from R (using system or execGRASS from the spgrass6 package) gives a vector which can not be easily handled in R (i.e., it is not a nice vector with one layer name per vector element).

Finding the solution was a nice exersize in using the R functions gsub() and strsplit(), but today I realized that there is solution that is not only easier but also better and more flexible. Continue reading

Using the GRASS command g.list in R

Running grass commands within R is easy using either the system function or the spgrass6 package. But sometimes the GRASS outputs are not easy to read into a data frame or vector. For example, what if you want a vector with all raster data layers in mapset ‘species’. Continue reading

Finding and removing carriage returns in your SQLite table

The problem with copy data from e.g., an excel or calc sheet into an SQLite database (or any database I reckon) is that the data you copy might include an carriage return. I found this out the hard way after having copied data from a spreadsheet into a attribute table of a GRASS GIS vector layer (which is stored in a SQLite database). Continue reading

MapTiler to create online maps

For a project a few years ago we created a vegetation map for central and southwest Kenya (see http://ecodiv.org/trapnell for more information). The map together with documentation was initially made available on CD-ROM. I also created an online map. Apart from the version mentioned in this post, I also created a version with MapTiler.

MapTiler requires a georeferences image file as input. To export the vegetation map from my GRASS GIS database to a georeferenced image, I used the r.out.tiff function.

r.out.tiff -t input=veg_ethiopia_pnv@vegetation output=vegethiopia.tif

The resulting file can be used in MapTiler to create map mashups for Google Earth, Google Maps or OpenLayer. MapTiler offers a wizard like interface which leads you step by step through the process. Continue reading