Skip to content

Commit d6f127c

Browse files
committed
use native datum for default graticule, rather than the OGC:CRS84
1 parent 353cd13 commit d6f127c

File tree

7 files changed

+28
-9
lines changed

7 files changed

+28
-9
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# version 1.0-24
22

3+
* `st_graticule()` creates graticule by default in the geographic coordinate reference system of arguments `x` or `crs`, and only if nothing is found or provided in `OGC:CRS84`
4+
35
* the figure margins parameter `mar` can be specified in a call to `plot.sf()`; #2558
46

57
* fix class label setting in `[.sf()`; #2557

R/crs.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ fix_crs = function(x) {
9898

9999

100100
#' @name st_crs
101-
#' @param parameters logical; \code{FALSE} by default; if \code{TRUE} return a list of coordinate reference system parameters, with named elements \code{SemiMajor}, \code{InvFlattening}, \code{units_gdal}, \code{IsVertical}, \code{WktPretty}, and \code{Wkt}
101+
#' @param parameters logical; \code{FALSE} by default; only for the `st_crs.sfc()` method: if \code{TRUE} return a classed list of coordinate reference system parameters, with named elements \code{SemiMajor}, \code{InvFlattening}, \code{units_gdal}, \code{IsVertical}, \code{WktPretty}, \code{Wkt}, \code{Name}, \code{proj4string}, \code{epsg}, \code{yx}, \code{ProjJson}, \code{WKT1_ESRI}, \code{srid} (in authority:code form), \code{axes} (a data.frame with columns \code{name} and \code{orientation}), \code{gcs_crs} with the WKT of the base geographic coordinate system, \code{ud_unit}. The list has class \code{crs_parameters}.
102102
#' @export
103103
st_crs.sfc = function(x, ..., parameters = FALSE) {
104104
crs = fix_crs(attr(x, "crs"))
105-
if (parameters) {
105+
if (isTRUE(parameters)) {
106106
p = crs_parameters(crs)
107107
p$ud_unit = crs_ud_unit(crs)
108108
p

R/graticule.R

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
st_datum = function(x) {
2+
stopifnot(inherits(x, c("crs", "sf", "sfc")))
3+
if (inherits(x, "crs"))
4+
x = st_sfc(st_point(), crs = x)
5+
x = st_geometry(x) # in case inherits(x, "sf")
6+
datum = st_crs(st_crs(x, parameters = TRUE)$gcs_crs)
7+
if (is.na(datum))
8+
st_crs('OGC:CRS84') # + message or warning?
9+
else
10+
datum
11+
}
12+
113
#' Compute graticules and their parameters
214
#'
315
#' Compute graticules and their parameters
@@ -15,7 +27,7 @@
1527
#' @export
1628
#' @param x object of class \code{sf}, \code{sfc} or \code{sfg} or numeric vector with bounding box given as (minx, miny, maxx, maxy).
1729
#' @param crs object of class \code{crs}, with the display coordinate reference system
18-
#' @param datum either an object of class \code{crs} with the coordinate reference system for the graticules, or \code{NULL} in which case a grid in the coordinate system of \code{x} is drawn, or \code{NA}, in which case an empty \code{sf} object is returned.
30+
#' @param datum either an object of class \code{crs} with the coordinate reference system for the graticules, or \code{NULL} in which case a grid in the coordinate system of \code{x} is drawn, or \code{NA}, in which case an empty \code{sf} object is returned. If missing and \code{x} has a crs with a datum, the geographic coordinate system (datum) of \code{x} is taken.
1931
#' @param lon numeric; values in degrees East for the meridians, associated with \code{datum}
2032
#' @param lat numeric; values in degrees North for the parallels, associated with \code{datum}
2133
#' @param ndiscr integer; number of points to discretize a parallel or meridian
@@ -61,7 +73,7 @@
6173
#' plot(usa, graticule = st_crs(4326), axes = TRUE, lon = seq(-60,-130,by=-10))
6274
#' }
6375
st_graticule = function(x = c(-180, -90, 180, 90), crs = st_crs(x),
64-
datum = st_crs(4326), ..., lon = NULL, lat = NULL, ndiscr = 100,
76+
datum = st_crs('OGC:CRS84'), ..., lon = NULL, lat = NULL, ndiscr = 100,
6577
margin = 0.001)
6678
{
6779
s2 = sf_use_s2()
@@ -74,6 +86,11 @@ st_graticule = function(x = c(-180, -90, 180, 90), crs = st_crs(x),
7486
lon = seq(-180, 180, by = 20)
7587
if (is.null(lat))
7688
lat = seq(-80, 80, by = 20)
89+
} else if (missing(datum)) {
90+
datum = if (inherits(x, c("sf", "sfc")))
91+
st_datum(x)
92+
else
93+
st_datum(crs)
7794
}
7895

7996
if (is.null(crs))

R/plot.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ plot_sf = function(x, xlim = NULL, ylim = NULL, asp = NA, axes = FALSE, bgc = pa
659659
linAxis = function(side, ..., lon, lat, ndiscr, reset, at) axis(side = side, ...)
660660
if (!missing(graticule) && !identical(graticule, FALSE)) {
661661
g = if (isTRUE(graticule))
662-
st_graticule(pl_reg[c(1,3,2,4)], st_crs(x), st_crs('OGC:CRS84'), ...)
662+
st_graticule(pl_reg[c(1,3,2,4)], st_crs(x), st_datum(x), ...)
663663
else if (inherits(graticule, "crs") && !is.na(graticule))
664664
st_graticule(pl_reg[c(1,3,2,4)], st_crs(x), graticule, ...)
665665
else

man/plot.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/st_crs.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/st_graticule.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)