Skip to content

Commit

Permalink
clean up for new PROJ
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsumner committed Apr 17, 2024
1 parent b0eee7e commit adb2d23
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 77 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: reproj
Type: Package
Title: Coordinate System Transformations for Generic Map Data
Version: 0.6.0
Version: 0.6.0.9001
Authors@R: person("Michael D.","Sumner", role = c("aut", "cre"), email =
"mdsumner@gmail.com", comment=c(ORCID = "0000-0002-2471-7511"))
Description: Transform coordinates from a specified source to a specified
Expand All @@ -23,8 +23,8 @@ Imports:
Suggests:
testthat,
covr,
PROJ (>= 0.4.5)
RoxygenNote: 7.2.3
PROJ (>= 0.4.5.9004)
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
Encoding: UTF-8
SystemRequirements: PROJ (>= 4.4.6)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# reproj dev

* PROJ proj_trans returns a matrix with columns matching input, so we align to that here.

# reproj 0.6.0

* Fix drop problem in 1 row reproj_xy and reproj_xyz.
Expand Down
2 changes: 1 addition & 1 deletion R/reproj-extent.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
xc <- seq(xl[1L], xl[2L], length.out = dm[1L] + 1L)
yc <- seq(yl[1L], yl[2L], length.out = dm[2L] + 1L)

cbind(xc, rep(yc, each = length(xc)))
cbind(x = xc, y = rep(yc, each = length(xc)))

}

Expand Down
57 changes: 31 additions & 26 deletions R/reproj-package.r → R/reproj-package.R
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
#' Reproject data from source to target coordinate system.
#'
#' reproj provides helpers for easily reprojecting generic data, by depending
#' on a reprojection engine (proj4 for now).
#'
#' The function `reproj` is designed to take an input data set `x` and then a
#' `target` coordinate system specification. The `source` argument is not positional
#' (must be named) and must be provided.
#' Currently the coordinate system may be a 'PROJ string' or 'EPSG code' either
#' as a number or text.
#'
#' Methods are provided for data frame and matrix, add S3 methods for
#' you classes in your own package. For classed objects, or objects with a known
#' method for finding the 'source' coordinate system your method can provide
#' that logic.
#'
#' See [reproj] for global options to control assumptions about data that
#' is input in longitude latitude form.
#'
#' There is an option set at start up `reproj.mock.noproj6` which is designed
#' for testing the support in the PROJ package. Even if this package is
#' functional this option can be set to true so that reproj falls-back to
#' use the proj4 package instead.
#' @name reproj-package
#' @docType package
NULL
#' @keywords internal
"_PACKAGE"

## usethis namespace: start
## usethis namespace: end
NULL

#' Reproject data from source to target coordinate system.
#'
#' reproj provides helpers for easily reprojecting generic data, by depending
#' on a reprojection engine (proj4 for now).
#'
#' The function `reproj` is designed to take an input data set `x` and then a
#' `target` coordinate system specification. The `source` argument is not positional
#' (must be named) and must be provided.
#' The coordinate system string may be anything accepted by the PROJ library (libproj).
#'
#' Methods are provided for data frame and matrix, add S3 methods for
#' you classes in your own package. For classed objects, or objects with a known
#' method for finding the 'source' coordinate system your method can provide
#' that logic.
#'
#' See [reproj] for global options to control assumptions about data that
#' is input in longitude latitude form.
#'
#' There is an option set at start up `reproj.mock.noproj6` which is designed
#' for testing the support in the PROJ package. Even if this package is
#' functional this option can be set to true so that reproj falls-back to
#' use the proj4 package instead.
#' @name reproj-package
NULL
44 changes: 7 additions & 37 deletions R/reproj.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ reproj <- function(x, target, ..., source = NULL, four = FALSE) {
#' @rdname reproj
#' @export
reproj.matrix <- function(x, target, ..., source = NULL, four = FALSE) {
nms <- colnames(x)
## make sure all columns have names, or none
if (any(nzchar(nms))) colnames(x) <- NULL
if (isTRUE(four)) {
stop("argument 'four' is not available currently")
}
Expand All @@ -131,46 +134,11 @@ reproj.matrix <- function(x, target, ..., source = NULL, four = FALSE) {

if (.ok_PROJ()) {

if (dim(x)[2L] == 2L) {
out <- PROJ::proj_trans(x, target = target, ..., source = source)
out <- cbind(do.call(cbind, out), 0)

}
if (dim(x)[2L] == 3L) {
out <- PROJ::proj_trans(x[,1:2, drop = FALSE], target = target, ..., source = source,
z_ = x[, 3L, drop = TRUE])
out <- do.call(cbind, out)
if (!four) out <- out[ , 1:3, drop = FALSE]
}
if (dim(x)[2L] > 3L) {
out <- PROJ::proj_trans(x[,1:2, drop = FALSE], target = target, ..., source = source,
z_ = x[, 3L, drop = TRUE],
t_ = x[, 4L, drop = TRUE])
out <- do.call(cbind, out)
if (!four) out <- out[ , 1:3, drop = FALSE]
}


if (four) {
if (dim(out)[2] == 2) {
out <- cbind(out, 0, 0)
}
if (dim(out)[2] == 3) {
out <- cbind(out, 0)
}
}
} else {




srcmult <- if (is_ll(source)) {pi/180} else {1}
tarmult <- if(is_ll(target)) {180/pi} else {1}

x[, 1:2] <- x[,1:2] * srcmult
out <- proj4::ptransform(x, source, target, ...)
out[,1:2] <- out[, 1:2] * tarmult
if (four) warning("argument 'four' is ignored when PROJ version 6 not available")
}
}
out
}

Expand All @@ -190,6 +158,8 @@ reproj_xy <- function(x, target, ..., source = NULL) {
#' @rdname reproj
#' @export
reproj_xyz <- function(x, target, ..., source = NULL) {
if (ncol(x) > 3L) x <- x[,1:3, drop = FALSE]
if (ncol(x) == 2L) x <- cbind(x, 0.0)
reproj(x, target = target, source = source, ...)[,1:3, drop = FALSE]
}

Expand Down
10 changes: 5 additions & 5 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
ok_lon_lat <- function(x, ...) UseMethod("ok_lon_lat")
ok_lon_lat.numeric <- function(x, ...) {
ok_lon_lat <- function(x, ...) {
if (is.null(dim(x))) {
## assume it's an extent
x[1] > -365 &&
x[2] < 365 &&
x[3] > -91 && x[4] < 91
} else {
ok_lon_lat(c(range(x[, 1L, drop = TRUE], na.rm = TRUE), range(x[, 2L, drop = TRUE], na.rm = TRUE)))
}

ok_lon_lat.matrix <- function(x, ...) {
ok_lon_lat(c(range(x[, 1L], na.rm = TRUE), range(x[, 2L], na.rm = TRUE)))
}


Expand Down
23 changes: 19 additions & 4 deletions man/reproj-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/test-reproj-PROJ.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ test_that("sc works", {
})

test_that("geocentric works", {
xyz <- reproj::reproj(cbind(147, -42), target = "+proj=geocent", source = "EPSG:4326")
xyz <- reproj::reproj(cbind(147, -42, 0), target = "+proj=geocent", source = "EPSG:4326")
expect_equal(c(xyz > 0), c(FALSE, TRUE, FALSE))
})

0 comments on commit adb2d23

Please sign in to comment.