Raster manipulation in R

Error message

  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _drutex_create_regex() (line 363 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Notice: Use of undefined constant filter - assumed 'filter' in preg_replace() (line 1 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module(363) : regexp code).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _filter_drutex_process() (line 315 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _drutex_create_regex() (line 363 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Notice: Use of undefined constant filter - assumed 'filter' in preg_replace() (line 1 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module(363) : regexp code).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _filter_drutex_process() (line 315 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _drutex_create_regex() (line 363 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Notice: Use of undefined constant filter - assumed 'filter' in preg_replace() (line 1 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module(363) : regexp code).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _filter_drutex_process() (line 315 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _drutex_create_regex() (line 363 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Notice: Use of undefined constant filter - assumed 'filter' in preg_replace() (line 1 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module(363) : regexp code).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _filter_drutex_process() (line 315 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _drutex_create_regex() (line 363 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Notice: Use of undefined constant filter - assumed 'filter' in preg_replace() (line 1 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module(363) : regexp code).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _filter_drutex_process() (line 315 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _drutex_create_regex() (line 363 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Notice: Use of undefined constant filter - assumed 'filter' in preg_replace() (line 1 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module(363) : regexp code).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _filter_drutex_process() (line 315 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _drutex_create_regex() (line 363 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Notice: Use of undefined constant filter - assumed 'filter' in preg_replace() (line 1 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module(363) : regexp code).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _filter_drutex_process() (line 315 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _drutex_create_regex() (line 363 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _drutex_unhide_all() (line 479 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).
  • Notice: Undefined index: en in drutex_node_view() (line 81 of /home/davemr/mo-seph.com/sites/all/modules/drutex/drutex.module).

As part of the work I'm doing, we wanted to look at how likely certain areas are to be developed. We wanted to separate out areas which would definitely be developed, definitely not be developed and those which were "up for grabs".

There are several different scenarios about future development, which each provide a coherent set of parameters for the model. The model is also stochastic, so there are 30 runs for each scenario to give probabilistic futures. The task is to take the results from many runs under each condition, and produce a map showing probabilities of development.

The "raster" package in R is a great tool for this job ([http://cran.r-project.org/web/packages/raster/index.html Project Page])
It needs installing, either from binaries or with install.packages("raster"), but note it will only automatically install on newer versions of R - I had to upgrade from 2.9.2 to 2.12.0.

The model outputs ESRI raster files (.asc), which the raster package can read easily:

r <- raster( "koper-COMIND-1145454504-landUse-cumulative-2030-30.asc" )

This creates a single "RasterLayer", which is essentially a 2d matrix with extra spatial information - coordinates, cell sizes, projections etc. It can be visualised using plot, which produces something like this:
Raster Plot

In order to look at a collection of these, we can make a "RasterStack", which is a collection of RasterLayers all having the same spatial setup. Assuming that there is a function which gets the random run ids for a given scenario, and a function which returns a filename when given a scenario and a run id, we can create a RasterStack like this:

scenario <- "BAU";
tmp <- lapply( scenarioIDs(scenario), function(x){raster( lucFile( scenario, x ) ) } );
allChange <- stack( tmp );
plot( allChange );

Multiple LUC

Now, this has just given us land use change, which in this case includes several different processes. For this question, we're interested in newly created artificial surfaces. The next step is to extract just the artificial surfaces. First, create a data.frame which maps land use class ids onto whether they are artificial or natural. This can then be used with reclass() to produce rasters which are 1 if new artificial surfaces have been created (and 0 otherwise):

> artificialSurfaces <- c( 1, 2, 8, 9, 10, 14 );
> naturalAreas <- c( 4, 5, 6, 7 );
> classes <- data.frame( class=1:15, artificial=0, natural=0 )
> classes<em class="error">TeX Embedding failed!</em>natural[naturalAreas] <- 1
> classes <- rbind( classes, list( class=0,artificial=0,natural=0 ))
> classes <- rbind( classes, list( class=-1,artificial=0,natural=0 ))
> classes
   class artificial natural
1      1          1       0
2      2          1       0
3      3          0       0
4      4          0       1
5      5          0       1
6      6          0       1
7      7          0       1
8      8          1       0
9      9          1       0
10    10          1       0
11    11          0       0
12    12          0       0
13    13          0       0
14    14          1       0
15    15          0       0
16     0          0       0
17    -1          0       0
development <- reclass( allChange, cbind( classes<img class="teximage" src="/files/tex/59d14b553f6df3d4cf13953aaaf1f1ba4dd6bf80.png" alt="$ class, classes $" />class, classes$artificial ) )

This gives us maps of development locations:
Development Locations

The final stage is to create the map of development probability by location. This is now easy - we can use mean() to produce a map showing the mean of each cell across the runs. This collapses the 30 layer RasterStack into a single RasterLayer:

> probability <- mean( development )
> plot( probability )

Development Probability

This is only scratching the surface of the raster functions in R - it looks like there are some really powerful tools in there!

Project Type: