Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

replace dependency on rgdal with sf (issue #38) #43

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Imports:
mapdata,
TMB,
MatrixModels,
rgdal,
sf,
ThorsonUtilities,
abind,
TMBhelper
Expand All @@ -47,6 +47,6 @@ Additional_repositories: https://www.math.ntnu.no/inla/R/stable
License: GPL-2
LazyData: yes
BuildVignettes: yes
RoxygenNote: 5.0.1
RoxygenNote: 6.0.1
URL: http://github.com/nwfsc-assess/geostatistical_delta-GLMM
BugReports: http://github.com/nwfsc-assess/geostatistical_delta-GLMM/issues
29 changes: 16 additions & 13 deletions R/Convert_LL_to_EastNorth_Fn.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

#' Convert from Lat-Long to Eastings-Northings using WGS
#'
#' \code{Convert_LL_to_EastNorth_Fn} converts from Latitude-Longitude to World Geodetic System Eastings-Northings for a given location
#'
#' @param Lat vector of latitudes
#' @param Lon vector of longitudes
#' @param crs EPSG reference for coordinate reference system (CRS) defining Eastings-Northings after transformation

#' @author James Thorson, Sophie Mormede
#'
#' @return A data frame with the following columns
#' \describe{
#' \item{E_km}{The eastings for each value of Lon (in kilometers)}
Expand All @@ -17,22 +17,25 @@
Convert_LL_to_EastNorth_Fn <-
function( Lon, Lat, crs=NA ){
# SEE: https://github.com/nwfsc-assess/geostatistical_delta-GLMM/issues/25#issuecomment-345825230

# SEE: https://github.com/nwfsc-assess/geostatistical_delta-GLMM/issues/38

# Attach package
require(rgdal)
on.exit( detach("package:rgdal") )

require(sf)
on.exit( detach("package:sf") )
# Transform
dstart<-data.frame(lon=Lon, lat=Lat) # that's the object
coordinates(dstart) <- c("lon", "lat")
proj4string(dstart) <- CRS("+init=epsg:4326") # that's the lat long projection
CRS.new <- CRS(crs) # that's the eastings and northings projection
dstart.t <- spTransform(dstart, CRS.new) # here's where you transform

dstart<-as.matrix(dstart)

dstart <- st_multipoint(dstart)
dstart <- st_sfc(dstart, crs = 4326)
dstart.t<- st_transform(dstart, crs)
dstart.t<-st_coordinates(dstart.t)

# Clean up
dstart.t = cbind( "E_km"=dstart.t@coords[,"lon"]/1000, "N_km"=dstart.t@coords[,'lat']/1000 )
dstart.t = cbind( "E_km"=dstart.t[,1]/1000, "N_km"=dstart.t[,2]/1000 )
attr(dstart.t,"zone") = crs

# Return results
return( dstart.t )
}