|
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 |
| - |
48 | 1 | #' Extract coordinates from 'sf' objects
|
49 | 2 | #'
|
50 | 3 | #' `stat_sf_coordinates()` extracts the coordinates from 'sf' objects and
|
@@ -103,4 +56,70 @@ StatSfCoordinates <- ggproto(
|
103 | 56 | #' will be used. Note that the function may warn about the incorrectness of
|
104 | 57 | #' the result if the data is not projected, but you can ignore this except
|
105 | 58 | #' 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 | +) |
0 commit comments