Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/program--/tg-r

R interface to the C geometry library `tg`
https://github.com/program--/tg-r

Last synced: 24 days ago
JSON representation

R interface to the C geometry library `tg`

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

# tg

This package provides an interface to the [tg C library](https://github.com/tidwall/tg) similar
to [sf](https://github.com/r-spatial/sf) and [geos](https://github.com/paleolimbot/geos).

## Installation

You can install the development version of tg like so:

``` r
pak::pkg_install("program--/tg")
```

## Example Usage

Reading geometry from WKT:

```{r wkt_example}
tg::as_tg("POINT(1 1)")
```

Reading geometry from a GeoJSON string:
```{r geojson_example}
gjson <- '
{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
},
{ "type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
]
},
"properties": {
"prop0": "value0",
"prop1": 0.0
}
},
{ "type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
[100.0, 1.0], [100.0, 0.0] ]
]

},
"properties": {
"prop0": "value0",
"prop1": {"this": "that"}
}
}
]
}
'

tg_geometry <- tg::as_tg(gjson)
tg_geometry

# Converting to a TG Collection
collection <- tg::as_tgc(tg_geometry)
collection

# Creating a data.frame
data.frame(x = 1:3, geometry = collection)
```