| 1 |
# Copyright (c) 2003-23 by Barry Rowlingson and Roger Bivand |
| 2 |
|
| 3 |
if (!isGeneric("rebuild_CRS")) |
| 4 |
setGeneric("rebuild_CRS", function(obj) |
| 5 |
standardGeneric("rebuild_CRS")) |
| 6 |
|
| 7 |
setMethod("rebuild_CRS", signature(obj = "CRS"), |
| 8 |
function(obj) { |
| 9 |
if (is.null(comment(obj))) { |
| 10 |
obj <- CRS(slot(obj, "projargs")) |
| 11 |
} |
| 12 |
obj |
| 13 |
} |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
"CRS" <- function(projargs=NA_character_, doCheckCRSArgs=TRUE, |
| 18 |
SRS_string=NULL, get_source_if_boundcrs=TRUE, use_cache=TRUE) { |
| 19 |
# cautious change BDR 150424 |
| 20 |
# trap NULL too 200225 |
| 21 |
if (is.null(projargs)) |
| 22 |
warning("CRS: projargs should not be NULL; set to NA") |
| 23 |
if ((is.null(projargs)) || (!is.na(projargs) && !nzchar(projargs))) projargs <- NA_character_ |
| 24 |
# condition added 140301 |
| 25 |
stopifnot(is.logical(doCheckCRSArgs)) |
| 26 |
stopifnot(length(doCheckCRSArgs) == 1L) |
| 27 |
stopifnot(is.logical(get_source_if_boundcrs)) |
| 28 |
stopifnot(length(get_source_if_boundcrs) == 1L) |
| 29 |
stopifnot(is.character(projargs)) |
| 30 |
# CRS_CACHE <- get("CRS_CACHE", envir=.sp_CRS_cache) |
| 31 |
input_projargs <- projargs |
| 32 |
if (is.na(projargs)) { # fast track for trivial CRS() |
| 33 |
if (is.null(SRS_string)) |
| 34 |
return(new("CRS", projargs = NA_character_)) |
| 35 |
} else if (use_cache) { |
| 36 |
res <- .sp_CRS_cache[[input_projargs]] |
| 37 |
if (!is.null(res)) { |
| 38 |
return(res) |
| 39 |
} |
| 40 |
} |
| 41 |
if (requireNamespace("sf", quietly = TRUE)) { |
| 42 |
if ((length(grep("^[ ]*\\+", projargs)) > 0L) && |
| 43 |
!is.null(SRS_string)) projargs <- NA_character_ |
| 44 |
if ((is.na(projargs) && !is.null(SRS_string))) { |
| 45 |
res <- sf::st_crs(SRS_string) |
| 46 |
res <- as(res, "CRS") |
| 47 |
} else { |
| 48 |
res <- sf::st_crs(projargs) |
| 49 |
res1 <- try(as(res, "CRS"), silent=TRUE) |
| 50 |
if (inherits(res1, "try-error")) { |
| 51 |
res <- new("CRS", projargs=projargs) |
| 52 |
warning("invalid PROJ4 string") |
| 53 |
# rbgm workaround for +proj=utm +zone=18 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +# +a=6378137.0 +es=0.006694380022900787 +lon_0=-75d00 +lat_0=0d00 +x_0=500000.0 +y_0=0.0 +k=0.9996 in bgmfiles Final_CAM_Boxes_8.bgm |
| 54 |
} else { |
| 55 |
res <- res1 |
| 56 |
} |
| 57 |
} |
| 58 |
if (!is.na(slot(res, "projargs"))) |
| 59 |
.sp_CRS_cache[[input_projargs]] <- res |
| 60 |
return(res) |
| 61 |
} else { |
| 62 |
if (get("startup_message", envir=.spOptions) != "none") |
| 63 |
warning("sf required") |
| 64 |
} |
| 65 |
if (!is.na(projargs)) { |
| 66 |
if (length(grep("latlon", projargs)) != 0) |
| 67 |
stop("northings must follow eastings: ", projargs) |
| 68 |
if (length(grep("lonlat", projargs)) != 0) { |
| 69 |
projargs <- sub("lon", "long", projargs) |
| 70 |
warning("'lonlat' changed to 'longlat': ", projargs) |
| 71 |
} |
| 72 |
} |
| 73 |
if (is.na(projargs)) { |
| 74 |
uprojargs <- projargs |
| 75 |
} else { |
| 76 |
uprojargs <- paste(unique(unlist(strsplit(projargs, " "))), |
| 77 |
collapse=" ") |
| 78 |
if (length(grep("= ", uprojargs)) != 0) |
| 79 |
stop(paste("No spaces permitted in PROJ4 argument-value pairs:", |
| 80 |
uprojargs)) |
| 81 |
if (length(grep(" [:alnum:]", uprojargs)) != 0) |
| 82 |
stop(paste("PROJ4 argument-value pairs must begin with +:", |
| 83 |
uprojargs)) |
| 84 |
} |
| 85 |
comm <- NULL |
| 86 |
res <- new("CRS", projargs=uprojargs) |
| 87 |
if (!is.null(comm)) comment(res) <- comm |
| 88 |
if (!is.na(slot(res, "projargs"))) .sp_CRS_cache[[input_projargs]] <- res |
| 89 |
|
| 90 |
res |
| 91 |
} |
| 92 |
if (!isGeneric("wkt")) |
| 93 |
setGeneric("wkt", function(obj) |
| 94 |
standardGeneric("wkt")) |
| 95 |
|
| 96 |
setMethod("wkt", signature(obj = "CRS"), |
| 97 |
function(obj) { |
| 98 |
comm <- comment(obj) |
| 99 |
comm |
| 100 |
} |
| 101 |
) |
| 102 |
|
| 103 |
|
| 104 |
"print.CRS" <- function(x, ...) |
| 105 |
{ |
| 106 |
cat("Coordinate Reference System:\n") |
| 107 |
pst <- paste(strwrap(x@projargs), collapse="\n") |
| 108 |
if (nchar(pst) < 40) cat(paste("Deprecated Proj.4 representation:", pst, "\n")) |
| 109 |
else cat(paste("Deprecated Proj.4 representation:\n", pst, "\n")) |
| 110 |
wkt <- wkt(x) |
| 111 |
if (!is.null(wkt)) { |
| 112 |
cat("WKT2 2019 representation:\n") |
| 113 |
cat(wkt, "\n") |
| 114 |
} |
| 115 |
invisible(pst) |
| 116 |
} |
| 117 |
|
| 118 |
setMethod("show", "CRS", function(object) print.CRS(object)) |
| 119 |
|
| 120 |
identicalCRS = function(x, y) { |
| 121 |
if (! missing(y)) { |
| 122 |
if (inherits(x, "ST")) x <- slot(slot(x, "sp"), "proj4string") |
| 123 |
else if (inherits(x, "Raster")) x <- slot(x, "crs") |
| 124 |
else x <- slot(x, "proj4string") |
| 125 |
if (inherits(y, "ST")) y <- slot(slot(y, "sp"), "proj4string") |
| 126 |
else if (inherits(y, "Raster")) y <- slot(y, "crs") |
| 127 |
else y <- slot(y, "proj4string") |
| 128 |
identicalCRS1(rebuild_CRS(x), rebuild_CRS(y)) |
| 129 |
} else { # x has to be list: |
| 130 |
stopifnot(is.list(x)) |
| 131 |
if (inherits(x[[1]], "Tracks")) { |
| 132 |
x <- unlist(lapply(x, function(j) { |
| 133 |
y <- slot(j, "tracks") |
| 134 |
if (!is.null(y)) lapply(y, function(l) |
| 135 |
if (!is.null(l)) slot(l, "sp"))})) |
| 136 |
} |
| 137 |
if (length(x) > 1) { |
| 138 |
if (inherits(x[[1]], "ST")) |
| 139 |
x[[1]] <- slot(slot(x[[1]], "sp"), "proj4string") |
| 140 |
else if (inherits(x[[1]], "Raster")) |
| 141 |
x[[1]] <- slot(x[[1]], "crs") |
| 142 |
else x[[1]] <- slot(x[[1]], "proj4string") |
| 143 |
p1 = rebuild_CRS(x[[1]]) |
| 144 |
!any(!sapply(x[-1], function(p2) { |
| 145 |
if (inherits(p2, "ST")) |
| 146 |
p2 <- slot(slot(p2, "sp"), "proj4string") |
| 147 |
else if (inherits(p2, "Raster")) p2 <- slot(p2, "crs") |
| 148 |
else p2 <- slot(p2, "proj4string") |
| 149 |
identicalCRS1(rebuild_CRS(p2), p1)})) |
| 150 |
} else |
| 151 |
TRUE |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
identicalCRS1 = function(x, y) { |
| 156 |
args_x <- strsplit(x@projargs, " +")[[1]] |
| 157 |
args_y <- strsplit(y@projargs, " +")[[1]] |
| 158 |
setequal(args_x, args_y) |
| 159 |
} |
| 160 |
#https://github.com/inlabru-org/inlabru/issues/178 |
| 161 |
is.na.CRS = function(x) { |
| 162 |
is.na(x@projargs) && is.null(comment(x)) |
| 163 |
} |