Skip to content
Prev 24439 / 29559 Next

Bearing angle of UTM projected SpatialLines

On Wed, 25 May 2016, Eduardo Diez wrote:

            
#This was:

library(sp)
l1c <- cbind(c(642236.2, 643005.6), c(6197243, 6197948))
lgc <- cbind(c(-61.45336, -61.44511), c(-34.35639, -34.34994))
l1 <- SpatialLines(list(Lines(list(Line(l1c)), "1")),
  proj4string=CRS("+proj=utm +zone=20 +south +datum=WGS84 +units=m +no_defs
  +ellps=WGS84 +towgs84=0,0,0"))
l1.geo <- SpatialLines(list(Lines(list(Line(lgc)), "1")),
  proj4string=CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84
  +towgs84=0,0,0"))

#but the lack of precision (I asked for binary) in the coordinates means 
#that Arc's:

#Angle given by Linear Directional Mean for l1:
#47.53076

#becomes:

ds <- apply(coordinates(l1)[[1]][[1]], 2, diff)
atan2(ds[1], ds[2])*180/pi
atan2(ds[1], ds[2]-0.7343)*180/pi

#because the rounded difference in Northings is off by 0.7m.

#The spherical/ellipsoidal calculations are:

library(maptools)
gzAzimuth(from = l1.geo at lines[[1]]@Lines[[1]]@coords[1,], to =
  l1.geo at lines[[1]]@Lines[[1]]@coords[2,], type="snyder_sphere")

library(geosphere)
bearing(p1 = l1.geo at lines[[1]]@Lines[[1]]@coords[1,], p2 =
  l1.geo at lines[[1]]@Lines[[1]]@coords[2,], sphere=TRUE)

#equal as spherical, with this differeing a little depending where you are 
#on the globe:

bearing(p1 = l1.geo at lines[[1]]@Lines[[1]]@coords[1,], p2 =
  l1.geo at lines[[1]]@Lines[[1]]@coords[2,], sphere=FALSE)

#obviously one is spherical, the other ellipsoidal. Neither are planar, so 
#are not the same thing anyway.

If anyone feels like contributing a function to accept objects rather than 
raw coordinates for gzAzimuth, and restricting to geographical 
coordinates, adding a planar direction function, and an LDM function for 
Lines and SpatialLines objects, or any of these, I'd be grateful.

Hope this clarifies,

Roger