#'=================================================================================================# #' Using R to Analyse Ecological and Spatial Data # # #' "Calculating movement Metrics" # #'=================================================================================================# #' NB!!!! Set the working directory setwd("C:/Shambala/data/") #' MAKE SURE THE WORKING DIRECTORY IS CORRECT i.e. [1] "C:/R_course/Day3/data" getwd() #'----------------------------------------- Load Packages ---------------------------------------#### library(maptools) library(adehabitatHR) library(move) library(rgdal) library(lubridate) library(plyr) library(stringr) library(ggplot2) library(ggmap) ####------------------------------------- 1- Import Data ---------------------------------------#### #' Import cleaned .csv data #' thanda_cheetah_clean.csv or kruger_buffalo_clean.csv tracking_data <- read.csv("data_clean.csv") #' Import the study area boundry .shp file #' The dsn must not end in '/' #' Thanda_boundary or kruger #study_area <- readOGR(dsn = '.', layer = 'kruger') ####------------------------------ 2- Introduction to 'move' -----------------------------------#### #' Define the timezone, format is default POSIXct after cleaning tracking_data$timestamp <- as.POSIXct(tracking_data$timestamp, tz="Africa/Johannesburg") #' Takes the Longitude and Latitude from ele and saves it as a SpatialPoints data.frame with a projection sp_latlong<- SpatialPoints(tracking_data[, c("lon", "lat")],proj4string=CRS("+proj=longlat +ellps=WGS84")) #' Project the coordinates, this is UTM36 for Kruger sp_proj<- spTransform(sp_latlong, CRS("+init=epsg:32736")) #' Project other layers that you may want to plot #study_area_proj<-spTransform(study_area, projection(sp_proj)) #' Add projected locations to data frame locs<-as.data.frame(sp_proj@coords) tracking_data$x<-locs$lon tracking_data$y<-locs$lat #' Define the data frame as a move object: #' 1) If there is one animal, then a move object is made #' 2) If there are multiple animals - like this example - then a move stack object is made move_obj <- move(x=tracking_data$x, # Longitude y=tracking_data$y, # Latitude time=tracking_data$timestamp, # Timestamp i.e. date and time data=tracking_data, # The data source proj=CRS("+init=epsg:32736"), # The projection of the data source animal=tracking_data$animal) # The column containing the animal unique identifier #' Summary overview move_obj #' Move object structure str(move_obj) #' Plot a move object, the type "l" means line plot(move_obj, type="l") #' Examples of accessing information from the move object you have created timestamps(move_obj)[1:5] # Extract the timestamp for the first five points i.e. 1:5 coordinates(move_obj)[1:10,] # Extract the coordinates for the first ten points i.e 1:10 idData(move_obj) # Extract the animals that make up the move object n.locs(move_obj) # Extract the number of locations for each of the animals #####-------------------------------- 3- Calculate Movement Metrics ------------------------------#### #' Extract properties #' Add Speed to the move object, typically in m.s move_obj$speed <-unlist(lapply(speed(move_obj), c, NA)) # adds an NA to the first row summary(move_obj$speed) #' Add Distance to the move object, typically in m move_obj$distance <-unlist(lapply(distance(move_obj), c, NA)) # adds an NA to the first row summary(move_obj$distance) #' Add Angle to the move object (Angles in degrees (between -180 and 180) move_obj$angle <-unlist(lapply(angle(move_obj), c, NA)) # adds an NA to the first row summary(move_obj$angle) #####--------------------------------- 4- Export the Results -----------------------------------#### #' Convert move object to a data frame, only select columns of interest export <- as.data.frame(move_obj) # Creates a data frame called export from the move object #' Neaten the table up and only select columns you are interested in #' If you get this error "arguments imply differing number of rows:..." it means one of the #' columns does not exist - try re-run all the code in section 3 above export <- data.frame(animal=export$animal, timestamp=export$timestamp, lon=export$lon, lat=export$lat, #utm.easting=export$utm.easting, #utm.northing=export$utm.northing, speed=export$speed, distance=export$distance #angle=export$angle ) #' Prefer speed in km/h export$speed <- export$speed*3.6 # write the dataset to CSV write.csv(as.data.frame(export), "data_metrics.csv") # ...or create a shapefile from the points #' Convert the data points into spatialPointsDataFrame data_spat<- SpatialPointsDataFrame(export[, c("lon", "lat")],data=export) #' Define the projection of the points proj4string(data_spat)<-CRS("+proj=longlat +ellps=WGS84") #' Write to .shp writeOGR(data_spat, ".", "data_metrics", driver="ESRI Shapefile", overwrite_layer = T)