| 1 |
\name{gridlines} |
| 2 |
\alias{gridlines} |
| 3 |
\alias{gridat} |
| 4 |
\alias{text} |
| 5 |
\alias{text.SpatialPointsDataFrame} |
| 6 |
\alias{labels} |
| 7 |
\alias{labels.SpatialLines} |
| 8 |
|
| 9 |
\title{ Create N-S and E-W grid lines over a geographic region } |
| 10 |
\description{ Create N-S and E-W grid lines over a geographic region; create and plot corresponding labels } |
| 11 |
\usage{ |
| 12 |
gridlines(x, easts = pretty(bbox(x)[1,]), norths = pretty(bbox(x)[2,]), |
| 13 |
ndiscr = 100) |
| 14 |
gridat(x, easts = pretty(bbox(x)[1,]), norths = pretty(bbox(x)[2,]), |
| 15 |
offset = 0.5, side = "WS") |
| 16 |
\method{labels}{SpatialLines}(object, labelCRS, side = 1:2, ...) |
| 17 |
\method{text}{SpatialPointsDataFrame}(x, ...) |
| 18 |
} |
| 19 |
\arguments{ |
| 20 |
\item{x}{object deriving from class \link{Spatial-class}} |
| 21 |
\item{easts}{numeric; east-west values for vertical lines} |
| 22 |
\item{norths}{numeric; north-south values for horizontal lines} |
| 23 |
\item{ndiscr}{integer; number of points used to discretize the line, could |
| 24 |
be set to 2, unless the grid is (re)projected} |
| 25 |
\item{offset}{offset value to be returned, see \link{text}} |
| 26 |
\item{object}{\link{SpatialLines-class} object, as returned by \code{gridlines}} |
| 27 |
\item{labelCRS}{the CRS in which the grid lines were drawn and labels should be |
| 28 |
printed; if missing, the CRS from \code{object} is taken} |
| 29 |
\item{side}{for \code{labels}: integer, indicating side(s) at which gridlines labels will be drawn: |
| 30 |
1=below (S), 2=left (W), 3=above (N), and 4=right (E); |
| 31 |
for \code{gridat}: default \dQuote{WS}, if \dQuote{EN} labels placed on the top and right borders} |
| 32 |
\item{...}{ for \code{labels}: ignored; for \code{text}: arguments passed on |
| 33 |
to \link[graphics]{text}, see below for example use of \code{adj}} |
| 34 |
} |
| 35 |
|
| 36 |
\value{ \code{gridlines} returns an object of class |
| 37 |
\link{SpatialLines-class}, with lines as specified; the return |
| 38 |
object inherits the projection information of \code{x}; \code{gridat} |
| 39 |
returns a SpatialPointsDataFrame with points at the west and south |
| 40 |
ends of the grid lines created by \code{gridlines}, with degree |
| 41 |
labels. |
| 42 |
|
| 43 |
The \code{labels} method for \code{SpatialLines} objects returns a |
| 44 |
\link{SpatialPointsDataFrame-class} object with the parameters needed |
| 45 |
to print labels below and left of the gridlines. The locations for |
| 46 |
the labels are those of \code{proj4string(object)} the labels also |
| 47 |
unless \code{labelCRS} is given, in which case they are in that CRS. |
| 48 |
This object is prepared to be plotted with \code{text}: |
| 49 |
|
| 50 |
The \code{text} method for \code{SpatialPointsDataFrame} puts text labels |
| 51 |
on its coordinates, and takes care of attributes \code{pos}, \code{labels}, |
| 52 |
\code{srt} and \code{offset}; see \link[graphics]{text}. |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
\author{ |
| 57 |
Edzer Pebesma, \email{[email protected]}, using example code |
| 58 |
of Roger Bivand. |
| 59 |
} |
| 60 |
|
| 61 |
\seealso{ \link{spTransform}} |
| 62 |
\examples{ |
| 63 |
|
| 64 |
run <- FALSE |
| 65 |
if (requireNamespace("sf", quietly=TRUE)) run <- TRUE |
| 66 |
|
| 67 |
data(meuse) |
| 68 |
coordinates(meuse) = ~x+y |
| 69 |
plot(meuse) |
| 70 |
plot(gridlines(meuse), add = TRUE) |
| 71 |
text(labels(gridlines(meuse))) |
| 72 |
title("default gridlines within Meuse bounding box") |
| 73 |
|
| 74 |
if (run) { |
| 75 |
proj4string(meuse) <- CRS("+init=epsg:28992") |
| 76 |
crs.longlat <- CRS("+init=epsg:4326") |
| 77 |
meuse_ll <- spTransform(meuse, crs.longlat) |
| 78 |
grd <- gridlines(meuse_ll) |
| 79 |
grd_x <- spTransform(grd, CRS("+init=epsg:28992")) |
| 80 |
|
| 81 |
# labels South and West: |
| 82 |
plot(meuse) |
| 83 |
plot(grd_x, add=TRUE, lty=2) |
| 84 |
grdat_ll <- gridat(meuse_ll) |
| 85 |
grdat_x <- spTransform(grdat_ll, CRS("+init=epsg:28992")) |
| 86 |
text(grdat_x) |
| 87 |
} |
| 88 |
if (run) { |
| 89 |
# labels North and East: |
| 90 |
plot(meuse) |
| 91 |
plot(grd_x, add=TRUE, lty=2) |
| 92 |
grdat_ll <- gridat(meuse_ll, side="EN") |
| 93 |
grdat_x <- spTransform(grdat_ll, CRS("+init=epsg:28992")) |
| 94 |
text(grdat_x) |
| 95 |
} |
| 96 |
if (run) { |
| 97 |
# now using labels: |
| 98 |
plot(meuse) |
| 99 |
plot(grd_x, add=TRUE, lty=2) |
| 100 |
text(labels(grd_x, crs.longlat)) |
| 101 |
} |
| 102 |
if (run) { |
| 103 |
# demonstrate axis labels with angle, both sides: |
| 104 |
sp = SpatialPoints(rbind(c(-101,9), c(-101,55), c(-19,9), c(-19,55)), crs.longlat) |
| 105 |
laea = CRS("+proj=laea +lat_0=30 +lon_0=-40") |
| 106 |
sp.l = spTransform(sp, laea) |
| 107 |
plot(sp.l, expandBB = c(0, 0.05, 0, .05)) |
| 108 |
gl = spTransform(gridlines(sp), laea) |
| 109 |
plot(gl, add = TRUE) |
| 110 |
text(labels(gl, crs.longlat)) |
| 111 |
text(labels(gl, crs.longlat, side = 3:4), col = 'red') |
| 112 |
title("curved text label demo") |
| 113 |
} |
| 114 |
if (run) { |
| 115 |
# polar: |
| 116 |
pts=SpatialPoints(rbind(c(-180,-70),c(0,-70),c(180,-89),c(180,-70)), crs.longlat) |
| 117 |
polar = CRS("+init=epsg:3031") |
| 118 |
gl = spTransform(gridlines(pts, easts = seq(-180,180,20), ndiscr = 100), polar) |
| 119 |
plot(spTransform(pts, polar), expandBB = c(.05,0,.05,0)) |
| 120 |
lines(gl) |
| 121 |
l = labels(gl, crs.longlat, side = 3) |
| 122 |
l$pos = NULL # pos is too simple, use adj: |
| 123 |
text(l, adj = c(0.5, -0.5)) |
| 124 |
l = labels(gl, crs.longlat, side = 4) |
| 125 |
l$srt = 0 # otherwise they end up upside-down |
| 126 |
text(l) |
| 127 |
title("grid line labels on polar projection, epsg 3031") |
| 128 |
} |
| 129 |
\dontrun{ |
| 130 |
if (require(maps)) demo(polar) # adds the map of the antarctic |
| 131 |
} |
| 132 |
|
| 133 |
} |
| 134 |
\keyword{spatial} |