An open API service indexing awesome lists of open source software.

https://github.com/brownag/rjts

R interface for the JTS Topology Suite
https://github.com/brownag/rjts

Last synced: 2 months ago
JSON representation

R interface for the JTS Topology Suite

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%"
)
```

# {rjts}

[![R-CMD-check Build
Status](https://github.com/brownag/rjts/workflows/R-CMD-check/badge.svg)](https://github.com/brownag/rjts/actions)
[![rjts
Manual](https://img.shields.io/badge/docs-HTML-informational)](https://humus.rocks/rjts/)

The goal of {rjts} is to provide a basic {rJava} wrapper for the JTS Topology Suite <>.

Check out the Javadoc <> to see the tools that are provided via JTS.

## Installation

You can install the development version of {rjts} with {remotes}

``` r
if (!requireNamespace("remotes")) install.packages("remotes")
remotes::install_github("brownag/rjts")
```

Once the package is installed, you will need to install the required JAR files.

```r
library(rjts)
#> rjts (#.#.#) -- R interface to the JTS Topology Suite
#>
#> Unable to load JTS JAR files! Perhaps the package was installed with an empty 'inst/java' directory?
#> - Run `install_jts()` to download libraries from GitHub . Then, re-install the package.
#> - It may be convenient to use a local instance of the GitHub repository to install from. See instructions at
```

You can download and install JAR libraries from GitHub <> with `install_jts()`. You will need to run this installation command in a _local_ instance of the package repository that you can then install the package from (again).

For instance, you can do:

```sh
git clone http://github.com/brownag/rjts.git
Rscript -e "rjts::install_jts('rjts')"
Rscript -e "remotes::install_local('rjts', force = TRUE)"
```

This clones the Git repository, runs `install_jts()` (requires {rjts} be installed from GitHub as above), then runs `remotes::install_local()` to _re-install_ from the local instance of the package (where `"inst/java"` has been populated with relevant JAR files).

```r
library(rjts)

#> rjts (#.#.#) -- R interface to the JTS Topology Suite
#> Added JTS version #.#.# to Java classpath
#> JAR files:
#> jts-core-#.#.#.jar
#> JTSTestBuilder.jar
```

## Example

This is an example showing how to read and write Well-Known Text (WKT) and Well-Known Binary (WKB).

You can perform various basic geometric operations by accessing [`Geometry`](https://locationtech.github.io/jts/javadoc/org/locationtech/jts/geom/Geometry.html) class methods exposed in the `read()` result object. Use `$` to access methods.

```{r example}
library(rjts)

wr <- WKTReader()

# read a MULTIPOLYGON from character vector
x <- wr$read("MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),
((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),
(30 20, 20 15, 20 25, 30 20)))")
x$getGeometryType()

# use the WKTWriter to return Well-Known Text
ww <- WKTWriter()
ww$writeFormatted(x$convexHull())

# use the WKBWriter to return Well-Known Binary
wb <- WKBWriter()
y <- wb$write(x$convexHull())

class(y)
y

# use the WKBReader to convert Well-Known Binary to a JTS Geometry object
wbr <- WKBReader()
wbr$read(y)

# Geometry objects/results also have a toString() method that returns WKT
x$getBoundary()$toString()
x$convexHull()$toString()
```