Learn R Programming

⚠️There's a newer version (0.2.10) of this package.Take me there.

geometr

Overview

The geometr package provides tools that generate and process easily accessible and tidy geometric shapes (of class geom). Moreover, it aims to improve interoperability of spatial and other geometric classes. Spatial classes are typically a collection of geometric shapes (or their vertices) that are accompanied by various metadata (such as attributes and a coordinate reference system). Most spatial classes are thus conceptually quite similar, yet a common standard lacks for accessing features, vertices or the metadata. Geometr fills this gap by providing tools

  • that produce an identical output for the same metadata of different classes (via so-called getters) and
  • that use an identical input to write to various classes that originally require different input (via so-called setters).

Installation

  1. Install the latest development version from github:
devtools::install_github("EhrmannS/geometr")
  1. The vignette gives an in depth introduction, explains the take on interoperability and discusses the spatial class geom that comes with geometr.

  2. Have fun being a geometer!

Examples

Create a geom

library(geometr)

# ... from other classes
library(sf)
#> Linking to GEOS 3.7.0, GDAL 2.4.0, PROJ 5.2.0
nc_sf <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
nc_geom <- gc_geom(input = nc_sf)

# ... or by hand.
library(tibble)
coords <- tibble(x = c(40, 70, 70, 50),
                 y = c(40, 40, 60, 70))
window <- tibble(x = c(0, 80),
                 y = c(0, 80))
aGeom <- gs_polygon(anchor = coords, window = window)

# The "tiny map" shows where the vertices are concentrated.
nc_geom
#> geom        polygon
#>             108 features | 2529 points
#> crs         +proj=longlat +datum=NAD27 +no_defs
#> attributes  (features) AREA, PERIMETER, CNTY_, CNTY_ID, NAME, FIPS, FIPSNO, CRESS_ID, BIR74, ...
#> tiny map           36.59 
#>                    ◌ ○ ◌ ○        
#>                    ○ ○ ○ ○        
#>                    ◌ ◌ ○ ◌        
#>             -84.32 ◌ ◌ ◌ ◌ -75.46 
#>                    33.88

Metadata of different classes can be extracted in interoperable quality (i.e. the same metadata in the same arrangement).

getTable(x = nc_sf)
#> Registered S3 method overwritten by 'cli':
#>   method     from    
#>   print.boxx spatstat
#> # A tibble: 108 x 16
#>      fid   gid  AREA PERIMETER CNTY_ CNTY_ID NAME  FIPS  FIPSNO CRESS_ID
#>    <int> <int> <dbl>     <dbl> <dbl>   <dbl> <fct> <fct>  <dbl>    <int>
#>  1     1     1 0.114      1.44  1825    1825 Ashe  37009  37009        5
#>  2     2     2 0.061      1.23  1827    1827 Alle… 37005  37005        3
#>  3     3     3 0.143      1.63  1828    1828 Surry 37171  37171       86
#>  4     4     4 0.07       2.97  1831    1831 Curr… 37053  37053       27
#>  5     5     4 0.07       2.97  1831    1831 Curr… 37053  37053       27
#>  6     6     4 0.07       2.97  1831    1831 Curr… 37053  37053       27
#>  7     7     5 0.153      2.21  1832    1832 Nort… 37131  37131       66
#>  8     8     6 0.097      1.67  1833    1833 Hert… 37091  37091       46
#>  9     9     7 0.062      1.55  1834    1834 Camd… 37029  37029       15
#> 10    10     8 0.091      1.28  1835    1835 Gates 37073  37073       37
#> # … with 98 more rows, and 6 more variables: BIR74 <dbl>, SID74 <dbl>,
#> #   NWBIR74 <dbl>, BIR79 <dbl>, SID79 <dbl>, NWBIR79 <dbl>
getTable(x = nc_geom, slot = "feature")
#> # A tibble: 108 x 16
#>      fid   gid  AREA PERIMETER CNTY_ CNTY_ID NAME  FIPS  FIPSNO CRESS_ID
#>    <int> <int> <dbl>     <dbl> <dbl>   <dbl> <fct> <fct>  <dbl>    <int>
#>  1     1     1 0.114      1.44  1825    1825 Ashe  37009  37009        5
#>  2     2     2 0.061      1.23  1827    1827 Alle… 37005  37005        3
#>  3     3     3 0.143      1.63  1828    1828 Surry 37171  37171       86
#>  4     4     4 0.07       2.97  1831    1831 Curr… 37053  37053       27
#>  5     5     4 0.07       2.97  1831    1831 Curr… 37053  37053       27
#>  6     6     4 0.07       2.97  1831    1831 Curr… 37053  37053       27
#>  7     7     5 0.153      2.21  1832    1832 Nort… 37131  37131       66
#>  8     8     6 0.097      1.67  1833    1833 Hert… 37091  37091       46
#>  9     9     7 0.062      1.55  1834    1834 Camd… 37029  37029       15
#> 10    10     8 0.091      1.28  1835    1835 Gates 37073  37073       37
#> # … with 98 more rows, and 6 more variables: BIR74 <dbl>, SID74 <dbl>,
#> #   NWBIR74 <dbl>, BIR79 <dbl>, SID79 <dbl>, NWBIR79 <dbl>

A geom has three attribute tables, one for vertices, one for features and one for groups of features, all of which can be provided with ancillary information (details on data structure of a geom.

getTable(x = nc_geom, slot = "point")
#> # A tibble: 2,529 x 3
#>        x     y   fid
#>    <dbl> <dbl> <int>
#>  1 -81.5  36.2     1
#>  2 -81.5  36.3     1
#>  3 -81.6  36.3     1
#>  4 -81.6  36.3     1
#>  5 -81.7  36.4     1
#>  6 -81.7  36.5     1
#>  7 -81.7  36.5     1
#>  8 -81.7  36.6     1
#>  9 -81.3  36.6     1
#> 10 -81.3  36.5     1
#> # … with 2,519 more rows

Groups of features are called multi* features in other packages. By lumping several closed geometric shapes into one multi* feature, the separate geometric shapes can’t be attributed with ancillary information any more. In a geom, multi* features are separated into distinct (simpler) features, while the attributes of multi* features can be captured by the group attribute table.

nc_geom2 <- gc_geom(input = nc_sf, group = TRUE)
currituck <- getSubset(x = nc_geom2, gid == 4)
getTable(x = currituck, slot = "feature")
#> # A tibble: 3 x 2
#>     fid   gid
#>   <int> <int>
#> 1     4     4
#> 2     5     4
#> 3     6     4
getTable(x = currituck, slot = "group")
#> # A tibble: 1 x 15
#>     gid  AREA PERIMETER CNTY_ CNTY_ID NAME  FIPS  FIPSNO CRESS_ID BIR74
#>   <int> <dbl>     <dbl> <dbl>   <dbl> <fct> <fct>  <dbl>    <int> <dbl>
#> 1     4  0.07      2.97  1831    1831 Curr… 37053  37053       27   508
#> # … with 5 more variables: SID74 <dbl>, NWBIR74 <dbl>, BIR79 <dbl>,
#> #   SID79 <dbl>, NWBIR79 <dbl>

Visualise a geom and use quick options to scale attributes to plot properties.

visualise(`North Carolina` = nc_geom)
visualise(`NC - NWBIR74` = nc_geom, fillcol = NWBIR74)

A geom has the slot @window, which contains a reference window. This reference window can be used or modified in many functions of geometr

visualise(`Currituck` = currituck, window = getExtent(currituck))

Finally, cast a geom to another type simply by providing it in anchor of the respective type

library(magrittr)
boundPoints <- gs_point(anchor = currituck) %>% 
   setWindow(to = getExtent(.))
visualise(`Currituck - boundary vertices`= boundPoints)

Copy Link

Version

Install

install.packages('geometr')

Monthly Downloads

44

Version

0.1.1

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Steffen Ehrmann

Last Published

October 16th, 2019

Functions in geometr (0.1.1)

getHistory

Get the history of a spatial object.
gc_sp

Transform a spatial object to class sp
gc_sf

Transform a spatial object to class sf
geom-class

Geometry class (S4) and methods
geometr

geometr: Generate and Modify Interoperable Geometric Shapes
getWindow

Get the reference window of a spatial object.
getType

Get the type of a spatial object.
.testWindow

Test window for consistency
getExtent

Get the extent (bounding box) of a spatial object.
getCRS

Get the coordinate reference system of a spatial object.
gs_polygon

Create a polygon geom
gtTheme-class

Theme class (S4) and methods
gtSP

Example Spatial objects
getSubset

Get the subset of a spatial object.
getTable

Get the attribute table of a spatial object.
gs_random

Create a geom randomly
gc_ppp

Transform a spatial object to class ppp
gc_grob

Transform a spatial object to a grob
gtTheme

Default visualising theme
gs_tiles

Create a regular tiling geom
gs_voronoi

Create a voronoi tiling geom
gt_skew

Skew geoms
gtRasters

Example RasterStack object
gt_stretch

Stretch geoms
gtSF

Example sf objects
gt_locate

Locate (and identify) clicks
getPoints

Get the table of coordinates of a spatial object.
setWindow

Set the reference window of a spatial object.
setTheme

Create a new theme
gt_translate

Translate geoms
makeLayout

Make the layout of a plot
gs_point

Create a point geom
gt_rotate

Rotate geoms
setTable

Set the attribute table(s) of a spatial object.
setHistory

Set additional entries to the history of an object
gt_reflect

Reflect geoms
gs_line

Create a line geom
gtGeoms

Example geom objects
makeObject

Make the object to a plot
setCRS

Set (or transform) the coordinate reference system of a spatial object.
show,geom-method

Print geom in the console
gtPPP

Example ppp object
visualise

Visualise raster and geom objects
gt_sketch

Sketch geoms
gt_scale

Scale geoms
show,gtTheme-method

Print gtTheme in the console
.rad

Convert degree to radians
.updateOrder

Update column order
.makeTinyMap

Make a tiny map
.testTemplate

Test template for consistency
.getDecimals

Get the number of decimal places
.updateVertices

Update the vertices
.updateWindow

Update the window
.testAnchor

Test anchor for consistency
gc_geom

Transform a spatial object to class geom