Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/r-spatial/s2
Spherical Geometry Operators Using the S2 Geometry Library
https://github.com/r-spatial/s2
Last synced: 4 months ago
JSON representation
Spherical Geometry Operators Using the S2 Geometry Library
- Host: GitHub
- URL: https://github.com/r-spatial/s2
- Owner: r-spatial
- Created: 2020-04-21T17:48:53.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-08-22T02:04:53.000Z (6 months ago)
- Last Synced: 2024-09-22T03:39:59.869Z (5 months ago)
- Language: C++
- Homepage: https://r-spatial.github.io/s2/
- Size: 12.3 MB
- Stars: 71
- Watchers: 8
- Forks: 10
- Open Issues: 16
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
Awesome Lists containing this project
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```# s2

[](https://app.codecov.io/gh/r-spatial/s2)
[](https://cran.r-project.org/package=s2)
[](https://www.r-pkg.org/pkg/s2)The s2 R package provides bindings to Google's [S2Geometry](http://s2geometry.io) library. The package exposes an API similar to Google's [BigQuery Geography API](https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions), whose functions also operate on spherical geometries. Package [sf](https://cran.r-project.org/package=sf) uses this package by default for nearly all its geometrical operations on objects with ellipsoidal (unprojected) coordinates; in cases where it doesn't, such as `st_relate()`, it emits a warning.
This package is a complete rewrite of an earlier CRAN package s2 with versions up
to 0.4-2, for which the sources are found [here](https://github.com/spatstat/s2/).## Installation
You can install the released version of s2 from [CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("s2")
```And the development version from [GitHub](https://github.com/) with:
``` r
# install.packages("remotes")
remotes::install_github("r-spatial/s2")
```## Example
The s2 package provides geometry transformers and predicates similar to those found in [GEOS](https://trac.osgeo.org/geos/), except instead of assuming a planar geometry, s2's functions work in latitude and longitude and assume a spherical geometry:
```{r example}
library(s2)s2_contains(
# polygon containing much of the northern hemisphere
"POLYGON ((-63.5 44.6, -149.75 61.20, 116.4 40.2, 13.5 52.51, -63.5 44.6))",
# ...should contain the north pole
"POINT (0 90)"
)
```The [sf package](https://r-spatial.github.io/sf/) uses s2 for geographic coordinates with `sf::sf_use_s2(TRUE)`, and will become the default after sf version 1.0.0. The sf package also supports creating s2 vectors using `as_s2_geography()`:
```{r, warning=FALSE, message = FALSE}
library(dplyr)
library(sf)nc_s2 <- read_sf(system.file("shape/nc.shp", package = "sf")) %>%
mutate(geometry = as_s2_geography(geometry)) %>%
as_tibble() %>%
select(NAME, geometry)nc_s2
```Use accessors to extract information about geometries:
```{r}
nc_s2 %>%
mutate(
area = s2_area(geometry),
perimeter = s2_perimeter(geometry)
)
```Use predicates to subset vectors:
```{r}
nc_s2 %>%
filter(s2_contains(geometry, "POINT (-80.9313 35.6196)"))
```Use transformers to create new geometries:
```{r}
nc_s2 %>%
mutate(geometry = s2_boundary(geometry))
```Finally, use the WKB or WKT exporters to export to sf or some other package:
```{r}
nc_s2 %>%
mutate(geometry = st_as_sfc(s2_as_binary(geometry))) %>%
st_as_sf()
```## Acknowledgment
This project gratefully acknowledges financial [support](https://www.r-consortium.org/projects) from the