![Page 1: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/1.jpg)
R for Macroecology
GIS + R = Awesome
![Page 2: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/2.jpg)
R and GIS My point of view
The best approach is to do everything in R, interacting with GIS software only when necessary
But, You can also take the opposite approach, through
integrated R functionality within GRASS GIS
![Page 3: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/3.jpg)
Today’s plan Geographic data types in R Handling huge files R in GIS
![Page 4: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/4.jpg)
Geographic data in R Data types
Vector Raster
Packages maptools raster
![Page 5: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/5.jpg)
Package maptools readShapePoly() reads in a GIS shape file
Can be plotted Various functions for converting among
formats Merge polygons
![Page 6: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/6.jpg)
Package raster the raster package has everything you need
for handling rasters
Read, write, plot, all kinds of queries and manipulations
![Page 7: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/7.jpg)
What is a raster object? A raster contains
A vector of values A size (nrow, ncol) Spatial information (extent, projection, datum)
A raster can have some of these things missing (for example, no data values, or no projection)
![Page 8: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/8.jpg)
What is a raster object?> mat = raster(“MAT.tif”)> matclass : RasterLayer dimensions : 2882, 2880, 8300160 (nrow, ncol,
ncell)resolution : 0.004166667, 0.004166667 (x, y)extent : 0, 12, 48, 60.00833 (xmin, xmax, ymin,
ymax)projection : +proj=longlat +ellps=WGS84 +datum=WGS84
+no_defs +towgs84=0,0,0 values : C:/Users/brody/Documents/Teaching/R for
Macroecology/Week 4/MAT.tif min : ? max : ?
Where’s the data?
![Page 9: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/9.jpg)
Raster objects are different! Normal objects are stored in memory, for fast
access
Raster objects are not always When you define a raster object R looks at the
summary information and remembers the hard drive locations
Small rasters often do reside in memory
Advantages and disadvantages
![Page 10: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/10.jpg)
The structure of a raster object Stored as a big vector
123456789....n
1 2 3 4 5 6 7 8
9 . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . n
ncol = 8
![Page 11: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/11.jpg)
Create a new raster> newRaster = raster(nrows = 10,ncols = 6,xmn = 0,xmx =
6,ymn = 50,ymx = 60,crs = "+proj=longlat +datum=WGS84")
> newRasterclass : RasterLayer dimensions : 10, 6, 60 (nrow, ncol, ncell)resolution : 1, 1 (x, y)extent : 0, 6, 50, 60 (xmin, xmax, ymin, ymax)projection : +proj=longlat +datum=WGS84 +ellps=WGS84
+towgs84=0,0,0 values : none
![Page 12: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/12.jpg)
Create a new raster> newRaster = setValues(newRaster,1:60)> plot(newRaster)
![Page 13: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/13.jpg)
> newRaster[22][1] 22
> newRaster[2,4][1] 10
> getValues(newRaster)[12][1] 12
Getting values from a raster
![Page 14: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/14.jpg)
Plotting a raster plot()
xlim and ylim control plotting window (just like usual)
col specifies the color palette (this works a bit differently)
subsample (defaults to TRUE) determines whether or not to plot every pixel (if TRUE, only plots at most maxpixel pixels)
colors rbg(), rainbow(), heat.colors(), terrain.colors(), topo.colors()
I also like the colors in fBasics package Can also use image()
Similar, but no scale bar
![Page 15: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/15.jpg)
Plotting examples
plot(newRaster,col = rgb(seq(0,1,0.2),0.5,0.5))
plot(newRaster,maxpixels = 7)
plot(newRaster,xlim = c(2,5),ylim = c(52,59),col = rainbow(50))
![Page 16: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/16.jpg)
A few useful ways to explore rasters zoom()
Opens a new active plotting window with the selected region
click() Queries a value, if xy = TRUE, also returns the x
and y coordinates
![Page 17: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/17.jpg)
Practice with this stuff
![Page 18: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/18.jpg)
Seeing a function Many of R’s functions are written in R!
You can define your own variations on core functions
> lmfunction (formula, data, subset, weights, na.action, method = "qr", model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, contrasts = NULL, offset, ...)
{ ret.x <- x ret.y <- y cl <- match.call()
...}
![Page 19: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/19.jpg)
Timing your code We are getting to the point where running
time can be limiting There are often multiple possible solutions, it
can be worth your time to try to find the fastest
Sys.time()> S = Sys.time()> x = rnorm(1000000)> E = Sys.time()> E-STime difference of 0.375 secs
![Page 20: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/20.jpg)
Handling giant rasters Huge rasters can be a pain to work with They can regularly be larger than your RAM
R solves this by not reading into RAM
But – if you are doing lots of queries on a raster, it can be much faster to convert it to a matrix first
Do what you want to do, and then use rm() to free up the memory that the matrix was using
![Page 21: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/21.jpg)
Raster data in different formats
RasterSlow accessLittle RAM usage
1 2 3 4 5 6 7 8 9 10 11 1213 14 15 16 17 1819 20 21 22 23 2425 26 27 28 29 3031 32 33 34 35 3637 38 39 40 41 4243 44 45 46 47 4849 50 51 52 53 5455 56 57 58 59 60
MatrixFast accessHeavy RAM usageRows and columns match raster
123456....60
VectorFast accessHeavy RAM usageEntries in same order as raster
![Page 22: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/22.jpg)
Raster data in different formats
1 2 3 4 5 6 7 8 9 10 11 1213 14 15 16 17 1819 20 21 22 23 2425 26 27 28 29 3031 32 33 34 35 3637 38 39 40 41 4243 44 45 46 47 4849 50 51 52 53 5455 56 57 58 59 60
123456....60
as.matrix() getValues()
![Page 23: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/23.jpg)
Raster data in different formats
1 2 3 4 5 6 7 8 9 10 11 1213 14 15 16 17 1819 20 21 22 23 2425 26 27 28 29 3031 32 33 34 35 3637 38 39 40 41 4243 44 45 46 47 4849 50 51 52 53 5455 56 57 58 59 60
123456....60
as.matrix() getValues()raster()
setValues()
![Page 24: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/24.jpg)
Raster data in different formats
1 2 3 4 5 6 7 8 9 10 11 1213 14 15 16 17 1819 20 21 22 23 2425 26 27 28 29 3031 32 33 34 35 3637 38 39 40 41 4243 44 45 46 47 4849 50 51 52 53 5455 56 57 58 59 60
123456....60
as.matrix() getValues()raster()
setValues()
as.matrix(,by.row = T)
as.vector(t())
![Page 25: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/25.jpg)
Hard drive management It can be a pain to have large rasters clogging
up your hard drive
It would be nice to be able to store them zipped, unzip them automatically when you need them in R, and then delete the unzipped data
Fortunately, that’s easy!
![Page 26: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/26.jpg)
unzipping and deleting R function unzip()
You point it to the file you want unzipped, and it does it
Then read in the unzipped file
When you are done with it, use file.remove() to delete it, leaving just the compressed data behind, to save space BE VERY CAREFUL!
![Page 27: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/27.jpg)
Remote data You can do even better – if data are stored at
stable URLs, you can download, unzip, work on and delete data all from R
download.file(url,filename) unzip(filename) do stuff file.remove(filename)
![Page 28: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/28.jpg)
Command lines Anything you can run on a command line can
be done from R, using shell()
Do file manipulations, run scripts, run executables
![Page 29: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/29.jpg)
Linking R and GIS File formats Generating Arc scripts in R Running R within GRASS GIS
![Page 30: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/30.jpg)
File formats writeRaster()
tif, img, grd, asc, sdat, rst, nc, envi, bil For me, I have found that tif seems to produce
hassle-free integration with Arc and others
write.shapefile() (package shapefiles) Also has functions for shp, shx and dbf specifically
![Page 31: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/31.jpg)
Arc scripts from R It’s probably not the best way, but if there is
something you want to do in Arc, you can generate the script using R
![Page 32: R for Macroecology GIS + R = Awesome. R and GIS My point of view The best approach is to do everything in R, interacting with GIS software only when](https://reader035.vdocument.in/reader035/viewer/2022062421/56649c575503460f948ff4b8/html5/thumbnails/32.jpg)
R and GRASS GRASS is a free GIS
R commands can be accessed from within GRASS using the spgrass6 package in R
Lets you do any statistical thing you’d like from within a (fairly) standard GIS