Skip to content

Commit c7abdd7

Browse files
committed
revert 4dd78b5 for layer_sf() stats
1 parent 4dd78b5 commit c7abdd7

File tree

2 files changed

+84
-49
lines changed

2 files changed

+84
-49
lines changed

R/stat-sf-coordinates.R

Lines changed: 67 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,3 @@
1-
#' @rdname stat_sf_coordinates
2-
#' @usage NULL
3-
#' @format NULL
4-
#' @export
5-
StatSfCoordinates <- ggproto(
6-
"StatSfCoordinates", Stat,
7-
8-
compute_layer = function(self, data, params, layout) {
9-
# add coord to the params, so it can be forwarded to compute_group()
10-
params$coord <- layout$coord
11-
ggproto_parent(Stat, self)$compute_layer(data, params, layout)
12-
},
13-
14-
compute_group = function(self, data, scales, coord, fun.geometry = NULL) {
15-
if (is.null(fun.geometry)) {
16-
fun.geometry <- function(x) sf::st_point_on_surface(sf::st_zm(x))
17-
}
18-
19-
points_sfc <- fun.geometry(data$geometry)
20-
21-
if (inherits(coord, "CoordSf")) {
22-
# register bounding box if the coord derives from CoordSf
23-
bbox <- sf::st_bbox(points_sfc)
24-
25-
coord$record_bbox(
26-
xmin = bbox[["xmin"]], xmax = bbox[["xmax"]],
27-
ymin = bbox[["ymin"]], ymax = bbox[["ymax"]]
28-
)
29-
30-
# transform to the coord's default crs if possible
31-
default_crs <- coord$get_default_crs()
32-
if (!(is.null(default_crs) || is.na(default_crs) ||
33-
is.na(sf::st_crs(points_sfc)))) {
34-
points_sfc <- sf::st_transform(points_sfc, default_crs)
35-
}
36-
}
37-
coordinates <- sf::st_coordinates(points_sfc)
38-
data$x <- coordinates[, "X"]
39-
data$y <- coordinates[, "Y"]
40-
41-
data
42-
},
43-
44-
default_aes = aes(x = after_stat(x), y = after_stat(y)),
45-
required_aes = c("geometry")
46-
)
47-
481
#' Extract coordinates from 'sf' objects
492
#'
503
#' `stat_sf_coordinates()` extracts the coordinates from 'sf' objects and
@@ -103,4 +56,70 @@ StatSfCoordinates <- ggproto(
10356
#' will be used. Note that the function may warn about the incorrectness of
10457
#' the result if the data is not projected, but you can ignore this except
10558
#' when you really care about the exact locations.
106-
stat_sf_coordinates <- make_constructor(StatSfCoordinates, geom = "point")
59+
stat_sf_coordinates <- function(mapping = aes(), data = NULL, geom = "point",
60+
position = "identity", na.rm = FALSE,
61+
show.legend = NA, inherit.aes = TRUE,
62+
fun.geometry = NULL,
63+
...) {
64+
layer_sf(
65+
stat = StatSfCoordinates,
66+
data = data,
67+
mapping = mapping,
68+
geom = geom,
69+
position = position,
70+
show.legend = show.legend,
71+
inherit.aes = inherit.aes,
72+
params = list2(
73+
na.rm = na.rm,
74+
fun.geometry = fun.geometry,
75+
...
76+
)
77+
)
78+
}
79+
80+
#' @rdname stat_sf_coordinates
81+
#' @usage NULL
82+
#' @format NULL
83+
#' @export
84+
StatSfCoordinates <- ggproto(
85+
"StatSfCoordinates", Stat,
86+
87+
compute_layer = function(self, data, params, layout) {
88+
# add coord to the params, so it can be forwarded to compute_group()
89+
params$coord <- layout$coord
90+
ggproto_parent(Stat, self)$compute_layer(data, params, layout)
91+
},
92+
93+
compute_group = function(self, data, scales, coord, fun.geometry = NULL) {
94+
if (is.null(fun.geometry)) {
95+
fun.geometry <- function(x) sf::st_point_on_surface(sf::st_zm(x))
96+
}
97+
98+
points_sfc <- fun.geometry(data$geometry)
99+
100+
if (inherits(coord, "CoordSf")) {
101+
# register bounding box if the coord derives from CoordSf
102+
bbox <- sf::st_bbox(points_sfc)
103+
104+
coord$record_bbox(
105+
xmin = bbox[["xmin"]], xmax = bbox[["xmax"]],
106+
ymin = bbox[["ymin"]], ymax = bbox[["ymax"]]
107+
)
108+
109+
# transform to the coord's default crs if possible
110+
default_crs <- coord$get_default_crs()
111+
if (!(is.null(default_crs) || is.na(default_crs) ||
112+
is.na(sf::st_crs(points_sfc)))) {
113+
points_sfc <- sf::st_transform(points_sfc, default_crs)
114+
}
115+
}
116+
coordinates <- sf::st_coordinates(points_sfc)
117+
data$x <- coordinates[, "X"]
118+
data$y <- coordinates[, "Y"]
119+
120+
data
121+
},
122+
123+
default_aes = aes(x = after_stat(x), y = after_stat(y)),
124+
required_aes = c("geometry")
125+
)

R/stat-sf.R

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,21 @@ StatSf <- ggproto("StatSf", Stat,
5959
#' @export
6060
#' @rdname ggsf
6161
#' @inheritParams stat_identity
62-
stat_sf <- make_constructor(StatSf, geom = "rect")
62+
stat_sf <- function(mapping = NULL, data = NULL, geom = "rect",
63+
position = "identity", na.rm = FALSE, show.legend = NA,
64+
inherit.aes = TRUE, ...) {
65+
layer_sf(
66+
stat = StatSf,
67+
data = data,
68+
mapping = mapping,
69+
geom = geom,
70+
position = position,
71+
show.legend = show.legend,
72+
inherit.aes = inherit.aes,
73+
params = list2(
74+
na.rm = na.rm,
75+
...
76+
)
77+
)
78+
}
6379

0 commit comments

Comments
 (0)