-
Notifications
You must be signed in to change notification settings - Fork 5
/
cast.R
60 lines (55 loc) · 2.08 KB
/
cast.R
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
52
53
54
55
56
57
58
59
60
#' Cast geometries to another type
#'
#' @param x an object of class `rsgeo`
#' @param to a character scalar of the target geometry type. Must be one of
#' `"point"`, `"multipoint"`, `"polygon"`, `"multipolygon"`, `"linestring"`,
#' or `"multilinestring"`.
#'
#' @details
#'
#' The below conversions are made available. The left hand column indicates
#' the originating vector class and the right hand column indicates the
#' class that it will can be cast to.
#'
#' Note that correctness of conversions will not be checked or verified. If you
#' cast an `rs_MULTIPOINT` to an `rs_POLYGON`, the validity of the polygon
#' cannot be guaranteed.
#'
#' Conversions from an `rs_POLYGON` into an `rs_LINESTRING` will result in only
#' the exterior ring of the polygon ignoring any interior rings if there are any.
#'
#' | From | To |
#' | ----------- | ----------- |
#' | `rs_POINT` | `rs_MULTIPOINT` |
#' | `rs_MULTIPOINT` | `rs_POLYGON`, `rs_MULTIPOLYGON`, `rs_LINESTRING`, `rs_MULTILINESTRING` |
#' | `rs_POLYGON` | `rs_MULTIPOINT`, `rs_MULTIPOLYGON`, `rs_LINESTRING`, `rs_MULTILINESTRING` |
#' | `rs_MULTIPOLYGON` | `rs_MULTIPOINT`, `rs_MULTILINESTRING` |
#' | `rs_LINESTRING` | `rs_MULTIPOINT`, `rs_MULTILINESTRING`, `rs_POLYGON` |
#' | `rs_MULTILINESTRING` | `rs_MULTIPOINT`, `rs_MULTIPOLYGON` |
#'
#' @examples
#' ply <- geom_polygon(c(0, 1, 1, 0, 0), c(0, 0, 1, 1, 0))
#' cast_geoms(ply, "linestring")
#' cast_geoms(ply, "multipoint")
#' @export
#' @returns
#' An object of class `rsgeo`
cast_geoms <- function(x, to) {
stopifnot(
"`to` must be length 1" = length(to) == 1,
"Invalid `to` value"= tolower(to) %in% geom_types,
"Must be an `rsgeo` vector" = inherits(x, "rsgeo")
)
cls <- class(x)[[1]]
from <- tolower(substr(cls, 4, nchar(cls)))
switch(
from,
"point" = cast_points(x, to),
"multipoint" = cast_multipoints(x, to),
"linestring" = cast_linestrings(x, to),
"multilinestring" = cast_multilinestrings(x, to),
"polygon" = cast_polygons(x, to),
"multipolygon" = cast_multipolygons(x, to),
stop("No casting method for provided geometry type")
)
}