HakaiInstitute / hakai_guide_to_r

Guidelines and helpfull tips for hakai staff using R to analyze data
0 stars 2 forks source link

More mapping options using Hakai aesthetics #8

Open mawhal opened 5 years ago

mawhal commented 5 years ago

I'm excited about this useful reference for Hakai folks!

I recently was able to grab some GeoTIFF's from the Geospatial Team and wrote some code to add things to the map for a recent presentation.

The tiff is too big to attach here, but it would be cool to access files like these that are useable in R. For instance, the maps on the data page (http://data.hakai.org/) are not easy to use in R, but they make for nice presentation (example attached) LCBD_map.pdf

Here's a code snippet for handling a GeoTIFF. I'm happy to provide more if it seems useful.

# load libraries
library(rgdal)
library(raster)
library(RStoolbox)

# get info on file
GDALinfo( "Data/CalvertRegion150DPI_WebMercator/Calvert_Region_Map_2018_11_01_150dpi.tif" )

# load the tiff as a brick (Red, Green, Blue components are separate layers)
ras <- brick( "Data/CalvertRegion150DPI_WebMercator/Calvert_Region_Map_2018_11_01_150dpi.tif" )
# warning message suggests the file has "rotation". Can override this
ras@rotated <- FALSE

# project coordinates for sites to match geotiff
coordinates( lcbd.xy ) <- c("Long","Lat")
proj4string( lcbd.xy ) <- CRS("+proj=longlat +datum=WGS84")  
res <- spTransform( lcbd.xy, 
                    CRS("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs"))

# crop the map to focus on the area that contains all sites
extent(res)
rasc <- crop( ras, extent(res)*1.5 ) # multiplying by 1.5 gives a buffer 

windows(4,5)
par(mfrow=c(1,1))
plotRGB( rasc, maxpixels=5000000 )
points( res, cex=1.75, pch=21 )
Br-Johnson commented 5 years ago

This is great. It's nice to see an example of how to work with a GeoTIFF as well. Feel free to submit a pull request or if you wanted to just send me the GeoTIFF I'll try it out and put it in the guide. Thanks for sharing!