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.

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

2 Casa 15m Perspectiva Lateral Izquierda.
Repaso: Unidad 2 Lección 2
1 A B C
Scenario: EOT/EOT-R/COT Resident admitted March 10th Admitted for PT and OT following knee replacement for patient with CHF, COPD, shortness of breath.
Simplifications of Context-Free Grammars
Variations of the Turing Machine
AP STUDY SESSION 2.
1
Slide 1Fig 39-CO, p Slide 2Fig 39-1, p.1246.
Select from the most commonly used minutes below.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
STATISTICS HYPOTHESES TEST (I)
STATISTICS INTERVAL ESTIMATION Professor Ke-Sheng Cheng Department of Bioenvironmental Systems Engineering National Taiwan University.
David Burdett May 11, 2004 Package Binding for WS CDL.
Local Customization Chapter 2. Local Customization 2-2 Objectives Customization Considerations Types of Data Elements Location for Locally Defined Data.
Create an Application Title 1Y - Youth Chapter 5.
CALENDAR.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt BlendsDigraphsShort.
1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.
The 5S numbers game..
Media-Monitoring Final Report April - May 2010 News.
Break Time Remaining 10:00.
Turing Machines.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
PP Test Review Sections 6-1 to 6-6
1 The Blue Café by Chris Rea My world is miles of endless roads.
EIS Bridge Tool and Staging Tables September 1, 2009 Instructor: Way Poteat Slide: 1.
Bellwork Do the following problem on a ½ sheet of paper and turn in.
INTRODUCTION Lesson 1 – Microsoft Word Word Basics
K ONTRAK PERKULIAHAN I Made Gatot K, ST. MT 1. PENILAIAN Kehadiran min 75 % : 5 % Tugas: 20 % Diskusi / Presentasi: 20 % UTS: 25 % UAS: 30 % TOTAL: 100%
1 The Royal Doulton Company The Royal Doulton Company is an English company producing tableware and collectables, dating to Operating originally.
Operating Systems Operating Systems - Winter 2010 Chapter 3 – Input/Output Vrije Universiteit Amsterdam.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
TESOL International Convention Presentation- ESL Instruction: Developing Your Skills to Become a Master Conductor by Beth Clifton Crumpler by.
Numerical Analysis 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
GIS Lecture 8 Spatial Data Processing.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
1..
Adding Up In Chunks.
MaK_Full ahead loaded 1 Alarm Page Directory (F11)
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
Artificial Intelligence
Before Between After.
Subtraction: Adding UP
1 Lab 17-1 ONLINE LESSON. 2 If viewing this lesson in Powerpoint Use down or up arrows to navigate.
Bell Busters! Unit 1 #1-61. Purposes of Government 1. Purposes of government 2. Preamble to the Constitution 3. Domestic tranquility 4. Common defense.
: 3 00.
5 minutes.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Types of selection structures
Speak Up for Safety Dr. Susan Strauss Harassment & Bullying Consultant November 9, 2012.
1 Titre de la diapositive SDMO Industries – Training Département MICS KERYS 09- MICS KERYS – WEBSITE.
Essential Cell Biology
Converting a Fraction to %
Numerical Analysis 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Clock will move after 1 minute
famous photographer Ara Guler famous photographer ARA GULER.
PSSA Preparation.
Physics for Scientists & Engineers, 3rd Edition
Select a time to count down from the clock above
Copyright Tim Morris/St Stephen's School
1.step PMIT start + initial project data input Concept Concept.
9. Two Functions of Two Random Variables
A Data Warehouse Mining Tool Stephen Turner Chris Frala
1 Dr. Scott Schaefer Least Squares Curves, Rational Representations, Splines and Continuity.
Presentation transcript:

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 necessary  But,  You can also take the opposite approach, through integrated R functionality within GRASS GIS

Today’s plan  Geographic data types in R  Handling huge files  R in GIS

Geographic data in R  Data types  Vector  Raster  Packages  maptools  raster

Package maptools  readShapePoly() reads in a GIS shape file  Can be plotted  Various functions for converting among formats  Merge polygons

Package raster  the raster package has everything you need for handling rasters  Read, write, plot, all kinds of queries and manipulations

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)

What is a raster object? > mat = raster(“MAT.tif”) > mat class : RasterLayer dimensions : 2882, 2880, (nrow, ncol, ncell) resolution : , (x, y) extent : 0, 12, 48, (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?

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

The structure of a raster object  Stored as a big vector n n n ncol = 8

Create a new raster > newRaster = raster(nrows = 10,ncols = 6,xmn = 0,xmx = 6,ymn = 50,ymx = 60,crs = "+proj=longlat +datum=WGS84") > newRaster class : 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

Create a new raster > newRaster = setValues(newRaster,1:60) > plot(newRaster)

> newRaster[22] [1] 22 > newRaster[2,4] [1] 10 > getValues(newRaster)[12] [1] 12 Getting values from a raster

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

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))

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

Practice with this stuff

Seeing a function  Many of R’s functions are written in R!  You can define your own variations on core functions > lm function (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()... }

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( ) > E = Sys.time() > E-S Time difference of secs

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

Raster data in different formats Raster Slow access Little RAM usage Matrix Fast access Heavy RAM usage Rows and columns match raster Vector Fast access Heavy RAM usage Entries in same order as raster

Raster data in different formats as.matrix() getValues()

Raster data in different formats as.matrix() getValues() raster() setValues()

Raster data in different formats as.matrix() getValues() raster() setValues() as.matrix(,by.row = T) as.vector(t())

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!

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!

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)

Command lines  Anything you can run on a command line can be done from R, using shell()  Do file manipulations, run scripts, run executables

Linking R and GIS  File formats  Generating Arc scripts in R  Running R within GRASS GIS

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

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

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