[R] Calculating distance between spatial points

Tim Clark mudiver1200 at yahoo.com
Fri Jun 26 01:50:42 CEST 2009


Dear List,

I am trying to determine the speed an animal is traveling on each leg of a track.  My data is in longitude and latitude, so I am using the package rgdal to convert it into a spatial points data frame and transform it to UTM.  I would then like to find the difference between successive longitudes and latitudes, find the euclidean distance between points, and compute the speed of the animal on each leg.

My problem is that once I convert the lat and long into a spatial points data frame I can not access the lat and long individually.  As far as I know I need to convert them in order to transform the lat and long to UTM.  Is there a way I can call each variable separately in the sp dataframe?  My code with example data is below.  Any suggestions would be appreciated.

  library(rgdal) 
  date.diff<-(20,30,10,30)
  Long<-c(-156.0540 ,-156.0541 ,-156.0550 ,-156.0640)
  Lat<-c(19.73733,19.73734,19.73743,19.73833) 
   
  SP<-data.frame(Long,Lat)  
  SP<-SpatialPoints(SP,proj4string=CRS("+proj=longlat +ellps=WGS84"))
  SP.utm<-spTransform(SP, CRS("+proj=utm +zone=4 +ellps=WGS84")) 
  
  long.diff<-diff(SP.utm$Long)
  lat.diff<-diff(SP.utm$Lat)
  
  d=(long.diff^2+lat.diff^2)^.5
  speed=d/date.diff


Aloha,

Tim



Tim Clark
Department of Zoology 
University of Hawaii




More information about the R-help mailing list