[R-sig-Geo] Calculating distance to nest with spDistsN1

Frede Aakmann Tøgersen frtog at vestas.com
Tue Sep 30 08:18:12 CEST 2014


Hi

Using spDists  will also give Anna-Maria the distance between a bird's nest and the endpoints of another birds trips.

Here is a way to achieve what I think Anna-Maria wants. Working with a SpatialPointsDataFrame objects complicates things so I will show how to it with an ordinary data.frame. Also I think that Anna-Maria has coded the trip_no, bird_id, and trip_id variables in a wrong way (see how I think the data should look below).

My assumptions:

1. The coordinates(dat) is the coordinates of end of trips
2. col_lat and col_long is the coordinates of nests
3. My example has two birds with id 1 and 2
4. Bird 1 has 4 trips with id 1,2,3,4 and bird 2 has 3 trips with id 1,2,3

## Here is a somewhat altered version of Anna-Maria's data:
dat <- read.table(text = 
"lon lat Date Time Speed Direction col_lat col_long dist_col trip_no bird_id trip_id DT date_time
7.87248 54.1866 '04.07.2014' '09:25:00' 0.02 314.57 54.1866 7.8724 0.0 1 1 1.1 '04.07.2014 09:25:00' '2014-07-04 09:25:00'
7.87148 54.1869 '04.07.2014' '09:30:00' 19.11 1.31 54.1866 7.8724 0.1 2 1 2.1 '04.07.2014 09:30:00' '2014-07-04 09:30:00'
7.87166 54.1850 '04.07.2014' '09:35:00' 14.98 224.79 54.1866 7.8724 0.2 3 1 3.1 '04.07.2014 09:35:00' '2014-07-04 09:35:00'
7.87168 54.1851 '04.07.2014' '09:35:00' 14.98 224.79 54.1866 7.8724 0.2 4 1 4.1 '04.07.2014 09:35:00' '2014-07-04 09:35:00'
7.88635 54.1569 '04.07.2014' '09:40:00' 11.44 136.97 54.2 7.9 3.4 1 2 1.2 '04.07.2014 09:40:00' '2014-07-04 09:40:00'
7.92096 54.1363 '04.07.2014' '09:45:00' 0.28 262.14 54.2 7.9 6.4 2 2 2.2 '04.07.2014 09:45:00' '2014-07-04 09:45:00'
7.91931 54.1360 '04.07.2014' '09:50:00' 0.42 218.89 54.2 7.9 6.4 3 2 3.2 '04.07.2014 09:50:00' '2014-07-04 09:50:00'",
                  h = TRUE, sep = " ")

## Now for each bird nest find the distances in km to end point of each birds trips:
trip_distances <- sapply(unique(dat$bird_id), function(id)
    spDistsN1(as.matrix(subset(dat, bird_id == id , select = c("lon", "lat"))),
              as.matrix(subset(dat, bird_id == id, select = c("col_long", "col_lat"))[1,]), longlat=T))

## have a look at the result
print(trip_distances)

## unlist the trip_distances and add that as a column to dat. The order of the unlist should match the row order of dat.
dat$trip_distances  <- unlist(trip_distances)

## print dat
print(dat)

Hope that is what Anna-Maria really wants.

Yours sincerely / Med venlig hilsen


Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling

Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
If you have received this e-mail in error please contact the sender. 


> -----Original Message-----
> From: r-sig-geo-bounces at r-project.org [mailto:r-sig-geo-bounces at r-
> project.org] On Behalf Of St John Brown
> Sent: 30. september 2014 03:04
> To: Anna-Marie Corman; R-sig-Geo at r-project.org
> Subject: Re: [R-sig-Geo] Calculating distance to nest with spDistsN1
> 
> Anna-Marie,
> 
> If you look at the documentation for spDistsN1 (i.e. run "?spDistsN1"), you
> will see that the second argument is suppose to be a single 2D point, and not
> many 2D points. As per the documentation descripttion, "the function
> returns a vector of distances between a matrix of 2D points ... and a single 2D
> point."
> 
> I am not 100% sure I understand what you are trying to do, but I think what
> you are looking for is the spDists function. Compare the results of my output
> from using spDistsN1 versus spDists.
> 
> I hope this helps.
> 
> St. John
> 
> make_locations = function(n){
>   lon = runif(n=n,min=7,max=8)
>   lat = runif(n=n,min=50,max=60)
>   locations = matrix(append(lon, lat), nrow=n, ncol=2)
>   colnames(locations) = c("lon", "lat")
>   rownames(locations) = 1:n
>   return(locations)
> }
> 
> set.seed(1)
> flight_locations = make_locations(n=3)
> nest_locations = make_locations(n=3)
> 
> flight_locations
> nest_locations
> 
> spDistsN1(flight_locations, nest_locations[1,], longlat=T)
> spDists(flight_locations, nest_locations, longlat=T)
> 
> 
> On Monday, September 29, 2014 5:47 AM, Anna-Marie Corman
> <annajess at gmx.de> wrote:
> 
> 
> 
> Dear list,
> 
> I have a question regarding the spDistsN1 function. I want to calculate
> the distance of each flight position to the relevant nest position. My
> dataset consists of several birds with a differing number of trips.
> There is a unique trip_id with the first number regarding to the bird
> and the second number regarding to the trip.
> 
>           coordinates       Date     Time Speed Direction col_lat col_long dist_col
> 1 (7.87248, 54.1866) 04.07.2014 09:25:00  0.02    314.57 54.1866   7.8724      0.0
> 2 (7.87148, 54.1869) 04.07.2014 09:30:00 19.11      1.31 54.1866   7.8724      0.1
> 3  (7.87166, 54.185) 04.07.2014 09:35:00 14.98    224.79 54.1866   7.8724      0.2
> 4 (7.88635, 54.1569) 04.07.2014 09:40:00 11.44    136.97 54.1866   7.8724      3.4
> 5 (7.92096, 54.1363) 04.07.2014 09:45:00  0.28    262.14 54.1866   7.8724      6.4
> 6  (7.91931, 54.136) 04.07.2014 09:50:00  0.42    218.89 54.1866   7.8724      6.4
>    trip_no bird_id trip_id                  DT           date_time
> 1       1       1     1.1 04.07.2014 09:25:00 2014-07-04 09:25:00
> 2       1       1     1.1 04.07.2014 09:30:00 2014-07-04 09:30:00
> 3       1       1     1.1 04.07.2014 09:35:00 2014-07-04 09:35:00
> 4       1       1     1.1 04.07.2014 09:40:00 2014-07-04 09:40:00
> 5       1       1     1.1 04.07.2014 09:45:00 2014-07-04 09:45:00
> 6       1       1     1.1 04.07.2014 09:50:00 2014-07-04 09:50:00
> 
> 
> 
> Formerly, when I only had one bird and so only one nest position (here
> col_lat & col_long), I did the following:
> 
> spDistsN1(coordinates(dat at coords), matrix(c(8.3495235,54.7042698), nrow
> = 1), longlat = T)
> 
> and this worked fine.
> 
> But now I want to calculate the distance from nest for each bird
> simultaneously. Must be simple, but I do not get it. I thought I could
> simply use
> 
> spDistsN1(coordinates(dat at coords), matrix(c(dat$col_long,dat$col_lat),
> nrow = 1), longlat = T)
> 
> instead, but it does not work.
> 
> Do you have any idea how to solve the "problem"?
> 
> Thanks a lot in advance.
> 
> Best,
> Anna
> 
>     [[alternative HTML version deleted]]
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list