A geovctr is a geometry-like collection of objects. In the geovctrs package, wkt(), wkb(), geo_collection(), geo_xy(), geo_rect(), and geo_segment() are all geovctrs. Extension packages can either use these types or implement the as_geovctr() generic to take advantage of a wide range of processing functions, including coercion, plotting, and summary information.

is_geovctr(x)

as_geovctr(x, ...)

# S3 method for character
as_geovctr(x, ...)

# S3 method for data.frame
as_geovctr(x, ...)

restore_geovctr(x, result, ...)

# S3 method for default
restore_geovctr(x, result, ...)

# S3 method for data.frame
restore_geovctr(x, result, ...)

expect_geovctr(x)

# S3 method for sfc
as_geovctr(x, ...)

# S3 method for sf
as_geovctr(x, ...)

# S3 method for sfc
restore_geovctr(x, result, ...)

# S3 method for sf
restore_geovctr(x, result, ...)

Arguments

x

A (possibly) geovctr

...

Passed to the constructor

result

The result of a transformation operation

Details

This package is intended to allow for a variety of in-memory representations of geometry, as there are many examples where simple geometries can be efficiently parameterized without resorting to storing every coordinate of every vertex (built-in examples include geo_xy(), geo_segment(), and geo_rect()). These types do, however, have unambiguous representations as geometries, and thus should be able to be used wherever a geometry is appropriate.

For an object to be a "geovctr", it must:

  • Be a vctr (vctrs::vec_is() must be TRUE). This ensures that it will work with tibble::tibble() and other tidyverse functions such as tidyr::unnest() and dplyr::group_by() / dplyr::mutate() / dplyr::summarise().

  • Have is_geovctr() return TRUE. This makes it work automatically with functions like geo_bbox() that have a default implementation for something that can be coerced to wkt(), wkb(), or wksxp().

  • Have the ability to be coerced to wkt(), wkb(), and wksxp() using as_wkt(), as_wkb(), and as_wksxp(). These casts power the default implementations of functions like geo_bbox(). In addition, a vctrs::vec_cast() method should be provided so that row-binding and other vector operations work with functions that might return a simpler type.

  • Have the ability to be combined with wkt(), wkb(), and wksxp() using vec_c() in both directions. This helps support processing functions that return a class to be combined with the output of other functions. This might require a vctrs::vec_ptype() implementation for a class.

You can test these expectations for a given object using expect_geovctr().

A secondary class of object is one that could be interpreted as a geovctr, but in most cases can't be. One example of this is a character vector, which could be well-known text, but probably isn't. However, when the user passes it to a function like geo_bbox(), it probably is well-known text. Similarly, a data.frame or tibble::tibble() probably doesn't contain a geometry column, but when passed to a function that operates on geometries, it's likely that it does. The geovctrs package supports these objects with the as_geovctr() generic, which means you can pass these objects anywhere you would pass a first-class geometry vector.

Examples

is_geovctr(wkt())
#> [1] TRUE
is_geovctr(NULL)
#> [1] FALSE
as_geovctr(wkt("POINT (30 10)"))
#> <wk_wkt[1]> #> [1] POINT (30 10)
as_geovctr("POINT (30 10)")
#> <wk_wkt[1]> #> [1] POINT (30 10)
as_geovctr(tibble::tibble(geometry = wkt("POINT (30 10)")))
#> <wk_wkt[1]> #> [1] POINT (30 10)