|
22 | 22 | #' @param margin numeric; small number to trim a longlat bounding box that touches or
|
23 | 23 | #' crosses +/-180 long or +/-90 latitude.
|
24 | 24 | #' @param ... ignored
|
| 25 | +#' @param labels_function function that computes axis tic labels from coordinates; see Details |
| 26 | +#' @details The \code{labels_function} function takes four arguments, \code{lon}, \code{lat}, \code{crs} and \code{datum}, and returns a character vector of length \code{length(lon) + length(lat)} with labels to be plotted at these \code{lon} and \code{lat} values. |
25 | 27 | #' @return an object of class \code{sf} with additional attributes describing the type
|
26 | 28 | #' (E: meridian, N: parallel) degree value, label, start and end coordinates and angle;
|
27 | 29 | #' see example.
|
|
61 | 63 | #' plot(usa, graticule = st_crs(4326), axes = TRUE, lon = seq(-60,-130,by=-10))
|
62 | 64 | st_graticule = function(x = c(-180,-90,180,90), crs = st_crs(x),
|
63 | 65 | datum = st_crs(4326), ..., lon = NULL, lat = NULL, ndiscr = 100,
|
64 |
| - margin = 0.001) |
| 66 | + margin = 0.001, labels_function = degree_label) |
65 | 67 | {
|
66 | 68 | if (missing(x)) {
|
67 | 69 | crs = datum
|
@@ -151,10 +153,7 @@ st_graticule = function(x = c(-180,-90,180,90), crs = st_crs(x),
|
151 | 153 |
|
152 | 154 | df = data.frame(degree = c(lon, lat))
|
153 | 155 | df$type = c(rep("E", length(lon)), rep("N", length(lat)))
|
154 |
| - df$degree_label = if (is.na(crs) || !isTRUE(st_is_longlat(datum))) |
155 |
| - c(format(lon), format(lat)) |
156 |
| - else |
157 |
| - c(degreeLabelsEW(lon), degreeLabelsNS(lat)) |
| 156 | + df$degree_label = labels_function(lon, lat, crs, datum) |
158 | 157 |
|
159 | 158 | geom = st_sfc(c(long_list, lat_list), crs = datum)
|
160 | 159 |
|
@@ -212,6 +211,13 @@ trim_bb = function(bb = c(-180, -90, 180, 90), margin, wrap=c(-180,180)) {
|
212 | 211 | bb[4] = min(bb[4], 90. * fr)
|
213 | 212 | bb
|
214 | 213 | }
|
| 214 | + |
| 215 | +degree_label = function(lon, lat, crs, datum) { |
| 216 | + if (is.na(crs) || !isTRUE(st_is_longlat(datum))) |
| 217 | + c(format(lon), format(lat)) |
| 218 | + else |
| 219 | + c(degreeLabelsEW(lon), degreeLabelsNS(lat)) |
| 220 | +} |
215 | 221 |
|
216 | 222 | # copied from sp:
|
217 | 223 | degreeLabelsNS = function(x) {
|
|
0 commit comments