Is there Any Way to Apply Different Projections to Individuals in the Same List

38 views
Skip to first unread message

jasone...@aol.com

unread,
May 10, 2023, 10:49:52 AM5/10/23
to ctmm R user group
Hello,

I have several annual lists of telemetry objects for multiple individuals from different locations. My problem is that most of these individuals were tracked in UTM zone 12, but a few were located in UTM zone 11, and several of my annual datasets contain individuals from both UTM zones. This has resulted in the home range polygons and coordinates of the zone 11 individuals being shifted far to the right when I export the data to google earth for visualization. Is there any way to assign different projections to individuals within the same list of telemetry objects? Here's an example of my work so far using the buffalo data. In this example I'm assuming that the buffalo Toni was tracked in UTM zone 11, while all others were tracked in UTM zone 12. I'm not sure if the place to assign the utm zone 11 projection to Toni would be at lines 75-76 where the telemetry objects are assigned to a projection, or at line 92 where the CRS for the spatial points dataframes is defined. I'm also not quite sure how I would go about assigning a diffreent projection to just Toni. Is there any way to specifically assign Toni to the UTM zone 11 projection? Thanks!

Jason

#adding libraries
library(ctmm)
library(ggplot2)
library(dplyr)
library(stringr)
library(lubridate)
library(sf)
library(tidyverse)
library(sp)
library(scales)
library(tidyr)
library(tibble)

#Load the data
data("buffalo")


#Creating individual dataframes and adding an id column
Cilla <- as.data.frame(buffalo$Cilla)
Cilla$id<-"Cilla"

Gabs<-as.data.frame(buffalo$Gabs)
Gabs$id<-"Gabs"

Mvubu<-as.data.frame(buffalo$Mvubu)
Mvubu$id<-"Mvubu"

Pepper<-as.data.frame(buffalo$Pepper)
Pepper$id<-"Pepper"

Queen<-as.data.frame(buffalo$Queen)
Queen$id<-"Queen"

Toni<-as.data.frame(buffalo$Toni)
Toni$id<-"Toni"

#Combining dataframes 
buffalo.df <- rbind(Cilla,Gabs,Mvubu,Pepper,Queen,Toni)

#adding a year column
buffalo.df$Year<-year(buffalo.df$timestamp)
buffalo.df$Year<-as.factor(buffalo.df$Year)

#adding a utm zone column
buffalo.df$UTM.zone<-"12 +datum=NAD27"

#changing the utm zone of Toni to zone 11
buffalo.df$UTM.zone[which(buffalo.df$id == "Toni")] = "11 +datum=NAD27"

#duplicating the timestamp to split into date and time
buffalo.df$timestamp <- ymd_hms(buffalo.df$timestamp)
buffalo.df$Timestamp<-buffalo.df$timestamp

#Splitting the extra timestamp into a date and time column
buffalo.df <- separate(buffalo.df, timestamp, c("date","time"), sep= " ",                      remove= TRUE, extra= "merge")

#Reordering columns
buffalo.df <- buffalo.df[, c(8,9,1,2,6,7,11,10)]

#Splitting data by year
data2005<-buffalo.df %>%
  filter(Year==2005)
data2006<-buffalo.df %>%
  filter(Year==2006)

#Creating annual telemetry data
Tdata2005<-as.telemetry(data2005, timeformat="auto",timezone="UTC",
                        timeout=Inf,datum="NAD27",
                        na.rm="row",mark.rm=FALSE,keep=FALSE,drop=FALSE)
Tdata2006<-as.telemetry(data2006, timeformat="auto",timezone="UTC",
                        timeout=Inf,datum="NAD27",
                        na.rm="row",mark.rm=FALSE,keep=FALSE,drop=FALSE)

#Assigning projections to telemetry objects
projection(Tdata2005)<- "+proj=utm +zone=12 +datum=NAD27 +units=m +no_defs"
projection(Tdata2006)<- "+proj=utm +zone=12 +datum=NAD27 +units=m +no_defs" 

#Create a list of both telemetry objects
Tdata<-list(Tdata2005,Tdata2006)
names(Tdata)<-c("Tdata2005","Tdata2006")

#Create a list of years
Years<-c("2005","2006")

#Writing spatial points dataframes for point data
points2shape <- function(x, date) {
  xy<-list()
  points<-list()
  for (i in 1:length(x)) {  
    xy <- x[[i]][c(5,6)]
    points[[i]] <- SpatialPointsDataFrame(coords = xy, data = x[[i]],
                                          proj4string = CRS("+proj=utm +zone=12 +datum=NAD27 +units=m +no_defs"))}
  return(points)
}

#applying the spatial points dataframe function to the list of data
points_list<-lapply(seq_along(Tdata), function(i) points2shape(Tdata[[i]], Years[i]))

# adding names
for (i in 1:length(points_list)) {    
  names(points_list)<-names(Tdata)
  names(points_list[[i]])<-names(Tdata[[i]])
}

#Create a function to write shapefiles based on the spatial dataframes
writeshape <- function(x, date) {
  xy<-list()
  points<-list()
  for (i in 1:length(x)) {  
    raster::shapefile(x[[i]],filename=paste(names(x[i]), "(", date, ")", ".jpg", sep = "", overwrite=TRUE))
  }
}

#applying the function to write shapefiles
lapply(seq_along(points_list), function(i) writeshape(points_list[[i]], Years[i]))

Christen Fleming

unread,
May 10, 2023, 4:12:11 PM5/10/23
to ctmm R user group
Hi Jason,

The way that I've coded the package, it shouldn't matter what UTM zone the individuals were tracked in. As long as the zone information is in the data, as.telemetry() should take that data and output a telemetry object in one projection that is, by default, centered on the data. I do not preserve the UTM projection of the imported data exactly for this reason.

It also shouldn't matter what projection the data are in before export. If you use ctmm::writeShapefile() then that projection information, whatever it is, should be retained on export.

Let's isolate where the issue is:
  1. When you import the data and put them into a single projection, such as with projection(DATA) <- median(DATA) , and then plot the data, do they appear to be in the correct locations? You can also plot a raster in the background if you need.
  2. When you export the data with the ctmm export functions (see help('export') ), then do they still appear to be in the correct locations after export?
Best,
Chris

jasone...@aol.com

unread,
May 10, 2023, 8:32:35 PM5/10/23
to ctmm R user group
Hey Chris,

well this is a bit embarrassing but my code seems to be working now. I think the issue is that changing the UTM.zone column of those particular individuals to zone 11 was one of the last edits I made to my script, and I think I forgot to run that specific line when making my shapefiles. Regardless, now the point data for my individuals that were tracked in zone 11 are showing up in google earth right where they should be. Thanks!

best,

     Jason

Reply all
Reply to author
Forward
0 new messages