File: shift_longitude.R

package info (click to toggle)
r-cran-sf 0.9-7%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,796 kB
  • sloc: cpp: 5,333; sh: 18; makefile: 2
file content (51 lines) | stat: -rw-r--r-- 1,523 bytes parent folder | download | duplicates (2)
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
#' Shift or re-center geographical coordinates for a Pacific view
#'
#' @description
#'   All longitudes < 0 are added to 360, to avoid for instance parts of Alaska
#'   being represented on the far left and right of a plot because they have
#'   values straddling 180 degrees. In general, using a projected
#'   coordinate reference system is to be preferred, but this method permits a
#'   geographical coordinate reference system to be used. This is the sf
#'   equivalent of \code{\link[sp:recenter-methods]{recenter}} in the sp package and
#'   ST_ShiftLongitude in PostGIS.
#'
#' @param x object of class sf or sfc
#' @param ... ignored
#'
#' @export
st_shift_longitude = function(x) {
	ll = st_is_longlat(x)
	if (!ll | is.na(ll))
		stop("'st_shift_longitude' requires non-projected geographic coordinates",
			 call. = FALSE)

	UseMethod("st_shift_longitude")
}

#' @name st_shift_longitude
#' @export
#' @examples
#' ## sfc
#' pt1 = st_point(c(-170, 50))
#' pt2 = st_point(c(170, 50))
#' (sfc = st_sfc(pt1, pt2))
#' sfc = st_set_crs(sfc, 4326)
#' st_shift_longitude(sfc)
#'
st_shift_longitude.sfc = function(x, ...) {
	xcrs = st_crs(x)
	g = (x + c(360, 90)) %% c(360) - c(0, 90)
	st_wrap_dateline(st_set_crs(g - c(180, 0), xcrs)) + c(180, 0)
	st_set_crs(g, xcrs)
}

#' @name st_shift_longitude
#' @export
#' @examples
#' ## sf
#' d = st_as_sf(data.frame(id = 1:2, geometry = sfc))
#' st_shift_longitude(d)
st_shift_longitude.sf = function(x, ...) {
	st_geometry(x) = st_shift_longitude(st_geometry(x))
	return(x)
}