Debian Codegraph @

R/projected.R

raw
1 setMethod("proj4string", signature(obj = "Spatial"),
2 function(obj) {
3 res <- as.character(obj@proj4string@projargs)
4 res
5 }
6 )
7
8 setMethod("wkt", signature(obj = "Spatial"),
9 function(obj) {
10 comm <- comment(slot(obj, "proj4string"))
11 comm
12 }
13 )
14
15
16 ReplProj4string = function(obj, value) {
17 p4str <- value@projargs
18 ll <- FALSE
19 if (!is.na(p4str)) {
20 res <- grep("longlat", p4str, fixed=TRUE)
21 if (length(res) != 0) ll <- TRUE
22 }
23 if (ll) {
24 bb <- bbox(obj)
25 ll_sanity_res <- .ll_sanity(bb)
26 if (!ll_sanity_res) {
27 lst <- sapply(attr(ll_sanity_res, "details"),
28 attr, "out")
29 out <- paste(format(unlist(lst), digits=12), collapse=" ")
30 mess <- paste("Geographical CRS given to",
31 "non-conformant data:", out)
32 if (get_ll_warn())
33 warning(mess)
34 else
35 stop(mess)
36 }
37 }
38 if (!is.na(is.projected(obj)) && !is.na(p4str)) {
39 p4s <- proj4string(obj)
40 if (p4s != p4str) {
41 mess <- paste("A new CRS was assigned to an object with an existing CRS:\n", p4s, "\nwithout reprojecting.\nFor reprojection, use function spTransform", sep="")
42 if (get_ReplCRS_warn()) warning(mess)
43 }
44 }
45 obj@proj4string = value;
46 obj
47 }
48 setReplaceMethod("proj4string", c("Spatial", "character"),
49 function(obj, value) ReplProj4string(obj, CRS(value)))
50 setReplaceMethod("proj4string", c("Spatial", "CRS"), ReplProj4string)
51
52 # split out from proj4string<- and Spatial validity to cover numerical fuzz
53 # RSB 070216
54 .ll_sanity <- function(bb) {
55 TOL <- get_ll_TOL()
56 tol <- .Machine$double.eps ^ TOL
57 W <- bb[1,1] < -180 &&
58 !isTRUE(all.equal((bb[1, 1] - -180), 0, tolerance = tol))
59 if (W) attr(W, "out") <- bb[1,1]
60 E <- bb[1,2] > 360 &&
61 !isTRUE(all.equal((bb[1, 2] - 360), 0, tolerance = tol))
62 if (E) attr(E, "out") <- bb[1,2]
63 S<- bb[2,1] < -90 &&
64 !isTRUE(all.equal((bb[2, 1] - -90), 0, tolerance = tol))
65 if (S) attr(S, "out") <- bb[2,1]
66 N <- bb[2,2] > 90 &&
67 !isTRUE(all.equal((bb[2, 2] - 90), 0, tolerance = tol))
68 if (N) attr(N, "out") <- bb[2,2]
69 res <- !(any(W || E || S || N))
70 attr(res, "details") <- list(W, E, S, N)
71 res
72 }
73 is.projectedSpatial <- function(obj) {
74 p4str <- slot(obj, "proj4string")
75 is.projected(p4str)
76 }
77
78 setMethod("is.projected", signature(obj = "Spatial"), is.projectedSpatial)
79
80 is.projectedCRS <- function(obj) {
81 p4str <- slot(obj, "projargs")
82 wkt2 <- comment(obj)
83 if (is.null(wkt2) && is.na(p4str)) # fast track for default objects
84 return(NA)
85 if (requireNamespace("sf", quietly = TRUE)) {
86 o <- try(sf::st_is_longlat(obj), silent=TRUE)
87 # 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
88 if (inherits(o, "try-error"))
89 length(grep("longlat", p4str, fixed = TRUE)) == 0L
90 else
91 !o
92 } else {
93 if (get("startup_message", envir=.spOptions) != "none")
94 warning("Package sf not available")
95 if (!is.null(wkt2)) # and old rgdal version
96 substring(wkt2, 1, 3) != "GEO"
97 else
98 length(grep("longlat", p4str, fixed = TRUE)) == 0L
99 }
100 }
101
102 setMethod("is.projected", signature(obj = "CRS"), is.projectedCRS)