Update of the r.forestfrag addon for GRASS GIS

Some time ago I came across this post from Sylla Consult about a script to calculate forest fragmentation index suggested by Riitters et al. (2000). Obviously, it can be used for any land cover type, so perhaps landscape fragmentation index would be a better name. Anyway, the script r.forestfrag.sh is available from the GRASS-addons page.

Unfortunately, it only worked with GRASS 6.4. Because I mostly work in GRASS 7.0 I adapted the script to make it work on GRASS 7.0.  I also added some additional options and changes:

  • Added the option to select the size of the moving window (default is still 3×3)
  • Before the areas statistics (r.report) is run, the output map is trimmed to remove the edge effect of the calculations with the moving window. However, there is the option to keep the output map trimmed or not
  • Added the option to keep some of the intermediate layers (off by default).

I have added the new version of r.forestfrag.sh to https://svn.osgeo.org/grass/grass-addons/grass7/raster/r.forestfrag/ to make it easier for other to check / correct the script. The adapted version doesn’t run on GRASS 6.4 unfortunately, although it should be fairly easy to make it so.

r.forestfrag [raster, forest, fragmentation-index, Riitters]_001

r.forestfrag [raster, forest, fragmentation-index, Riitters]_002

Disclaimer: I only did some limited testing and on Linux only so I am not sure it will work on Windows.

22 thoughts on “Update of the r.forestfrag addon for GRASS GIS

    1. pvanb

      It is a simple index so should be easy enough for you. I haven’t ventured into the world of python yet, but would like to convert this shell script to a GRASS python script. Something for later (if ever).

      1. Hui, i just noticed that SAGA can also calculate the Riitters Fragmentation Index. The tool is under Grid – Analysis –> Fragmentation (Standard). It also works quite well in QGis via Sextante

    2. Hi Curlew, I finally got around reimplementing the script in python. I am just starting with python, so I am sure there is a lot to improve, but it works. Next step is to learn some real python programming (this addon is mostly a wrapper for a series of grass commands).

      1. Cool cool, How is the speed in comparison to the SAGA module? Yeah, more python coding and wrapping is always good. I wanted to look at the scikits library for some time now. A lot of potential for advanced geostatistics, especially with SDM. Too little free time….

      2. Yes, I was looking in that too.. translating the shell script into a python script wasn’t too complicated, but for anything more involving, free time is an issue, as is my lack of python skills.

        I did not do a speed comparison, but I guess SAGA will be faster as generally all is don in-memory (I think). I would be interesting to make a comparison on very large data sets (will do that when time allows – also to see what the difference is between the two different modules in Saga).

  1. darkblue_b

    What does it mean ?

    “It was written for GRASS 6.4 and because of changes in the syntax of some of the functions used by the script, it won’t work in GRASS 7.0.”

    but then

    “It doesn’t run on GRASS 6.4 unfortunately, although it should be fairly easy to make it so.”

    1. pvanb

      It means that the original version was written for GRASS 6.4. The adapted version only works on GRASS 7.0 (for now,I might make it compatible with 6.4 when time allows).

  2. Thanks for improving the script. I made the original GRASS script years ago. Glad to know other people are using it as well. Python versions (for QGIS and GRASS) should be easy to implement for those familiar with the language.

  3. Hi!

    Is the AddOn still there? It keeps showing error when trying to install. Also, I was curious, is there R Script for this fragmentation analysis? I was trying, but without luck.

    1. Are you trying to run the version for GRASS 7? It is a shell script, so you can’t install it using the g.extension. Instead you can download the script and place it in the addon folder (e.g., on Linux normally ~/.grass7/addons/scripts). Don’t forget to set the permission of the file to execute as program. There has been some syntax changes so I just updated the script to reflect those changes. I only did a quick test on GRASS 7.1, so no guarantees, but if you want to try it out, you can download the new version from the addon page. I will look into it more in a week or two.

      As for R, I don’t know if there is a package that implements that function. You can of course run r.forestfrag from R, but that still requires you to start R from within a GRASS session. It shouldn’t be too difficult to write a function in R and the raster package either.

      I have no idea if the script works on Windows. I am planning to port the script to python so it will run cross-platform, but that will take more time.

  4. I am using it on a Windows system and I was trying in GRASS 6. I wasn’t aware it was shell script. But I am trying to work around it, and I will get back to you regarding the same, as soon as possible!


    1. Hi Anusheema, I rewrote the script in python, so now it should work in GRASS 7 for Windows. You can install the addon using the Extension Manager (Settings -> Addon extensions -> Install extensions from addons)

  5. Pingback: Update of r.forestfrag addon for GRASS GIS 7.0 | Ecostudies

  6. Hi Paulo!

    Thanks for writing the Python script for GRASS 7, it works well with my data. Since I am more familiar with the R language, I tried implementing the same in R.

    But this definitely seems more convenient. Thanks again!

    1. If the computation of the fragmentation is just one step in your work flow, and you would like to do all this in R, there is the option to run GRASS commands from within R using the spgrass6 package.

      The easiest way is to first start up GRASS, then on the terminal / command line start up R. You can then use the spgrass6 functions, like execGRASS6() to run grass commands, including addon functions like r.forestfrag. Assuming you have installed r.forestfrag and you want to run it, you can use

      1) library(spgrass6)
      2) execGRASS("r.forestfrag", input="myforestmap", output="fragmentationmap", window=5).
      3a) Use execGRASS("r.out.gdal", ....) to export the created layer as e.g., gtif, or
      3b) use readRAST6("fragmentationmap", ...) to read in the layer in R for further analysis

      You do not need to start R from GRASS; you can also start a temporary GRASS session and database from within R. After starting a R session, load the spgras6 library as above, and then use the initGRASS() function to start a temporary GRASS session (see the manual page for details and required parameters). After it is basically the same as above.

      Another great GIS software that can be used from wihtin R in similar way as GRASS is SAGA GIS (http://www.saga-gis.org/). You will need the RSAGA package, which offers you similar functionality as the spgras6 package. SAGA also has a function to compute the fragmentation index.

      p.s. 1. If you are on Windows, and run into problems getting R to work from GRASS, see this post:

      p.s. 2. The authors of spgrass6 are developing a new package spgrass7 which will better integrate with GRASS GIS 7. See this email thread. I’ll post an update if the package is officially released.

  7. The check for binary values in your code fails on large areas, I’m guessing because the data structure becomes too massive. I tried running it on all of Texas but it fails unless I comment that line out.

      1. I had a look at the code. I assume you were referring to line 159 in the script?

        if min(tstf[:,0]) != 0 or max(tstf[:,0]) != 1

        If your input layer is binary (0 and 1 only), the tstf (a ndarray) should simply consist of two values, e.g.,

        array([[ 0, 26807], [1, 37965]])

        If your map is not binary, the array will be longer of course. How long depends on the number of unique values in your map. If this number is very very large you might run into troubles. The maximum size of this array is equal to the number of cells in your raster, which you will get if each cell has an unique value.

        What is the exact error message you are getting? And do you have a reproducible example you can share? It might also be good to know which GRASS version you are running and what operating system.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s