-
Notifications
You must be signed in to change notification settings - Fork 2
/
st-lims.R
94 lines (90 loc) · 2.08 KB
/
st-lims.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#' Get one-set of the bounding box
#'
#' Return either the x-axis limits
#' or y-axis limits.
#'
#' @param x (bbox/sfc/sf) Either a bounding box or a geometry.
#'
#' @examples
#' library(sf)
#' data(states_map)
#'
#' x <- st_xlim(states_map)
#' x
#'
#' # The crs is preserved
#' st_crs(x)
#' @name bbox_lims
#' @export
st_xlim <- function (x) UseMethod("st_xlim")
#' @name bbox_lims
#' @export
st_xlim.sf <- function (x) {
box <- sf::st_bbox(x)
box_attrs <- attributes(box)
box <- box[c(1, 3)]
box <- structure(box,
class = "xlim",
names = c("xmin", "xmax"),
crs = box_attrs$crs)
box
}
#' @name bbox_lims
#' @export
st_xlim.sfc <- st_xlim.sf
#' @name bbox_lims
#' @export
st_xlim.bbox <- function (x) {
box_attrs <- attributes(x)
xlim <- x[c(1, 3)]
xlim <- structure(xlim,
class = "xlim",
names = c("xmin", "xmax"),
crs = box_attrs$crs)
xlim
}
#' @name bbox_lims
#' @export
st_ylim <- function (x) UseMethod("st_ylim")
#' @name bbox_lims
#' @export
st_ylim.sf <- function (x) {
box <- sf::st_bbox(x)
box_attrs <- attributes(box)
box <- box[c(2, 4)]
box <- structure(box,
class = "ylim",
names = c("ymin", "ymax"),
crs = box_attrs$crs)
box
}
#' @name bbox_lims
#' @export
st_ylim.sfc <- st_ylim.sf
#' @name bbox_lims
#' @export
st_ylim.bbox <- function (x) {
box_attrs <- attributes(x)
ylim <- x[c(2, 4)]
ylim <- structure(ylim,
class = "ylim",
names = c("ymin", "ymax"),
crs = box_attrs$crs)
ylim
}
# Print xlim class object
print.xlim <- function (x) {
px <- structure(x, crs = NULL, class = NULL)
print(px)
invisible(x)
}
# Print ylim class object
print.ylim <- print.xlim
# Fetch crs of xlim object
st_crs.xlim <- function (x) {
if (is.null(attr(x, "crs")))
sf::NA_crs_
else attr(x, "crs")
}
# Fetch crs of ylim object
st_crs.ylim <- st_crs.xlim