# [R] Calculating distance between spatial points

Nicolas Korber nicolas.korber at bluewin.ch
Sun Aug 2 09:03:53 CEST 2009

```Hello,
to call each variable separately, you can coerce your sp object back to a
dataframe. The code below should to the job:

DF.utm <- as.data.frame(SP.utm)
long.diff<-diff(DF.utm\$Long)

Nicolas

Tim Clark wrote:
>
>
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

--
View this message in context: http://www.nabble.com/Calculating-distance-between-spatial-points-tp24212875p24775433.html
Sent from the R help mailing list archive at Nabble.com.

```