[GIS] Extracting values from raster to get elevation values in R

elevationrraster

I am trying to generate an isoscape using IsoriX in R. Here how my data looks:

> head(dataC)

  siteID  long   lat       animalID       species tissue.value

1      A -5.88 53.82  CC1_33.2/1_15             C       -18.46

2      A -5.88 53.82  CL1_33.2/1_15             L       -19.98

3      A -5.88 53.82  CT1_33.2/1_15             P       -19.17

4      A -5.88 53.82  STA_33.2/1_15             S       -18.49

5      A -5.88 53.82 MESO_33.2/1_15             M       -27.05

6      B -6.03 53.41    CC1_43/3_15             C       -17.11


> str(dataC)

'data.frame':   322 obs. of  6 variables:

 $ siteID      : Factor w/ 38 levels "A","AA","B","BB",..: 1 1 1 1 1 3 3 3 5 5 ...

 $ long        : num  -5.88 -5.88 -5.88 -5.88 -5.88 -6.03 -6.03 -6.03 -5.87 -5.87 ...

 $ lat         : num  53.8 53.8 53.8 53.8 53.8 ...

 $ animalID    : Factor w/ 322 levels "AA1_36/8_15",..: 138 187 257 318 267 144 272 294 189 273 ...

 $ species     : Factor w/ 7 levels "A","C",..: 2 3 6 7 4 2 4 5 3 4 ...

 $ tissue.value: num  -18.5 -20 -19.2 -18.5 -27.1 ...

However, as the elevation is unknown at the sampling sites, it says it has to be extracted from a high resolution elevation raster using the function extract. However I cannot make it work…

The area I need to get on is the Irish Sea:

>map("worldHires",xlim=c(-6.7,-2.7), ylim=c(52,56), col="gray90", fill=TRUE,  
myborder = 0.0001) 

This is what I am typing:

## Creating Raster

>library(sp)  # classes for spatial data
>library(raster)  # grids, rasters
>library(lattice)
>library(latticeExtra)
>library(RColorBrewer)
>library(rasterVis)  # raster visualisation
>library(maptools)
>library(rgeos)
>library(dismo)
>library(XML)
>library(rgdal)

>mymap <- gmap("England", type = "satellite", filename = "England.gmap")
>plot(mymap)

>select.area <- drawExtent()
### now click 2 times on the map to select Irish Sea
>mymap <- gmap(select.area)
>plot(mymap)

### Save the map as a file in your working directory for future use
>mymap <- gmap(select.area, filename = "IrishSea.gmap")

## Get elevation values

### Read point data, and convert them into spatial points data frame
>pointCoordinates <- read.csv(file.choose(),h=T)
>coordinates(pointCoordinates) <- ~ x + y

### Extract raster value by points
>rasValue <- extract(mymap, coordinates(pointCoordinates))

### Combine raster values with point and save as a CSV file
>combinePointValue <- cbind(pointCoordinates,rasValue)
>write.table(combinePointValue,file= "combinedPointValue.csv", 
            append=FALSE, sep= ",", row.names = FALSE, col.names=TRUE)

Best Answer

I needed to extract values from a raster to get elevation values for the Irish sea, but I have endig using the next code and it worked!

latlong <- read.csv(file.choose(),h=T) ## load the long and lat dataset

data <- SpatialPoints(latlong)

library(marmap) irishsea <- getNOAA.bathy(lon1 = -7.5, lon2 = -2.7, lat1 = 52, lat2 = 56, resolution = 10)

summary(irishsea)

plot(irishsea, image = TRUE) scaleBathy(irishsea, deg = 2, x = "bottomleft", inset = 5)

blues <- colorRampPalette(c("red","purple","blue", "cadetblue1","white")) plot(irishsea, image = TRUE, bpal = blues(100))

plot(irishsea, image = TRUE, bpal = blues(100), deep = c(-9000, -3000, 0), shallow = c(-3000, -10, 0), step = c(1000, 1000, 0), lwd = c(0.8, 0.8, 1), lty = c(1, 1, 1), col = c("lightgrey", "darkgrey", "black"), drawlabel = c(FALSE, FALSE, FALSE))

Creating a custom palette of blues

blues <- c("lightsteelblue4", "lightsteelblue3", "lightsteelblue2", "lightsteelblue1")

greys <- c(grey(0.6), grey(0.93), grey(0.99))

Plotting the bathymetry with different colors for land and sea

plot(irishsea, image = TRUE, land = TRUE, lwd = 0.03, bpal = list(c(0, max(irishsea), greys), c(min(irishsea), 0, blues)))

Making the coastline more visible

plot(irishsea, deep = 0, shallow = 0, step = 0, lwd = 0.4, add = TRUE)

plot(irishsea, n = 1, lwd = 0.4, add = TRUE)

Adding my sample points

plot(data, pch=16, col="red", add=TRUE)

Now I have to extract those data, so let's get the depth for each sampling point

sp <- get.depth(irishsea, latlong[,1:2], locator = FALSE)

Combine depth values with my points and save as a CSV file

write.table(sp,file= "combinedPointValue.csv", append=FALSE, sep= ",", row.names = FALSE, col.names=TRUE)