Learn R Programming

wk

The goal of wk is to provide lightweight R, C, and C++ infrastructure for a distributed ecosystem of packages that operate on collections of coordinates. First, wk provides vector classes for points, circles, rectangles, well-known text (WKT), and well-known binary (WKB). Second, wk provides a C API and set of S3 generics for event-based iteration over vectors of geometries.

Installation

You can install the released version of wk from CRAN with:

install.packages("wk")

You can install the development version from GitHub with:

# install.packages("remotes")
remotes::install_github("paleolimbot/wk")

If you can load the package, you’re good to go!

library(wk)

Vector classes

Use wkt() to mark a character vector as containing well-known text, or wkb() to mark a vector as well-known binary. Use xy(), xyz(), xym(), and xyzm() to create vectors of points, and rct() to create vectors of rectangles. These classes have full vctrs support and plot()/format() methods to make them as frictionless as possible working in R and RStudio.

wkt("POINT (30 10)")
#> <wk_wkt[1]>
#> [1] POINT (30 10)
as_wkb(wkt("POINT (30 10)"))
#> <wk_wkb[1]>
#> [1] <POINT (30 10)>
xy(1, 2)
#> <wk_xy[1]>
#> [1] (1 2)
rct(1, 2, 3, 4)
#> <wk_rct[1]>
#> [1] [1 2 3 4]
crc(0, 0, 1)
#> <wk_crc[1]>
#> [1] [0 0, r = 1]

Generics

The wk package is made up of readers, handlers, and filters. Readers parse the various formats supported by the wk package, handlers calculate values based on information from the readers (e.g., translating a vector of geometries into another format), and filters transform information from the readers (e.g., transforming coordinates) on the fly. The wk_handle() and wk_translate() generics power operations for many geometry vector formats without having to explicitly support each one.

C API

The distributed nature of the wk framework is powered by a ~100-line header describing the types of information that parsers typically encounter when reading geometries and the order in which that information is typically organized. Detailed information is available in the C and C++ API article.

wk_debug(
  as_wkt("LINESTRING (1 1, 2 2, 3 3)"),
  wkt_format_handler(max_coords = 2)
)
#> initialize (dirty = 0  -> 1)
#> vector_start: <Unknown type / 0>[1] <0x16d75aac0> => WK_CONTINUE
#>   feature_start (1): <0x16d75aac0>  => WK_CONTINUE
#>     geometry_start (<none>): LINESTRING[UNKNOWN] <0x16d75a950> => WK_CONTINUE
#>       coord (1): <0x16d75a950> (1.000000 1.000000)  => WK_CONTINUE
#>       coord (2): <0x16d75a950> (2.000000 2.000000)  => WK_ABORT_FEATURE
#> vector_end: <0x16d75aac0>
#> deinitialize
#> [1] "LINESTRING (1 1, 2 2..."

sf support

The wk package implements a reader and writer for sfc objects so you can use them wherever you’d use an xy(), rct(), crc(), wkb(), or wkt():

wk_debug(
  sf::st_sfc(sf::st_linestring(rbind(c(1, 1), c(2, 2), c(3, 3)))),
  wkt_format_handler(max_coords = 2)
)
#> initialize (dirty = 0  -> 1)
#> vector_start: LINESTRING B[1] <0x16d75dac8> => WK_CONTINUE
#>   feature_start (1): <0x16d75dac8>  => WK_CONTINUE
#>     geometry_start (<none>): LINESTRING[3] <0x16d75da10> => WK_CONTINUE
#>       coord (1): <0x16d75da10> (1.000000 1.000000)  => WK_CONTINUE
#>       coord (2): <0x16d75da10> (2.000000 2.000000)  => WK_ABORT_FEATURE
#> vector_end: <0x16d75dac8>
#> deinitialize
#> [1] "LINESTRING (1 1, 2 2..."

Lightweight

The wk package has zero dependencies and compiles in ~10 seconds.

Copy Link

Version

Install

install.packages('wk')

Monthly Downloads

297,328

Version

0.9.4

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Dewey Dunnington

Last Published

October 11th, 2024

Functions in wk (0.9.4)

crc

2D Circle Vectors
crc_x

Circle accessors
new_wk_crc

S3 details for crc objects
new_wk_grd

S3 details for grid objects
grd_snap_next

Index snap functions
grd_subset

Subset grid objects
new_wk_rct

S3 details for rct objects
wk-package

wk: Lightweight Well-Known Geometry Parsing
new_wk_wkb

S3 Details for wk_wkb
grd_summary

Grid information
wk_crs_equal

Compare CRS objects
wk_crs

Set and get vector CRS
wk_bbox

2D bounding rectangles
grd_tile

Extract normalized grid tiles
plot.wk_grd_xy

Plot grid objects
wk_count

Count geometry components
wk_chunk_strategy_single

Chunking strategies
rct

2D rectangle vectors
vctrs-methods

Vctrs methods
wk_orient

Orient polygon coordinates
rct_xmin

Rectangle accessors and operators
wk_identity

Copy a geometry vector
grd_extract

Extract values from a grid
grd_cell

Grid cell operators
wk_plot

Plot well-known geometry vectors
wk_is_geodesic

Set and get vector geodesic edge interpolation
grd

Raster-like objects
wkb_translate_wkt

Deprecated functions
wk_trans_affine

Affine transformer
wk_set_z

Set coordinate values
wk_writer.sfc

Write geometry vectors
new_wk_xy

S3 details for xy objects
new_wk_wkt

S3 Details for wk_wkt
grd_tile_template

Compute overview grid tile
handle_wkt_without_vector_size

Test handlers for handling of unknown size vectors
wk_debug

Debug filters and handlers
wk_flatten

Extract simple geometries
wkb

Mark lists of raw vectors as well-known binary
wk_handle.wk_grd_xy

Handler interface for grid objects
wk_crs_inherit

Special CRS values
wk_example

Create example geometry objects
xy

Efficient point vectors
wk_trans_inverse

Generic transform class
wk_trans_explicit

Transform using explicit coordinate values
wk_crs_proj_definition

CRS object generic methods
wk_handle_slice.data.frame

Handle specific regions of objects
xy_x

XY vector extractors
wk_format

Format well-known geometry for printing
wk_handle.wk_crc

Read geometry vectors
wk_linestring

Create lines, polygons, and collections
wkb_to_hex

Convert well-known binary to hex
wk_handle.data.frame

Use data.frame with wk
wk_meta

Extract feature-level meta
wk_vertices

Extract vertices
wk_void

Do nothing
wkt

Mark character vectors as well-known text
wk_problems

Validate well-known binary and well-known text
wk_proj_crs_view

Common CRS Representations
wk_transform

Apply coordinate transformations
wk_translate.sfc

Translate geometry vectors