Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/ropensci/geojson

GeoJSON classes for R
https://github.com/ropensci/geojson

bbox crs geobuf geojson geospatial ndgeojson r r-package rstats spatial

Last synced: 3 months ago
JSON representation

GeoJSON classes for R

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r echo=FALSE}
library("knitr")
hook_output <- knitr::knit_hooks$get("output")
knitr::knit_hooks$set(output = function(x, options) {
lines <- options$output.lines
if (is.null(lines)) {
return(hook_output(x, options)) # pass to default hook
}
x <- unlist(strsplit(x, "\n"))
more <- "..."
if (length(lines)==1) { # first n lines
if (length(x) > lines) {
# truncate the output, but add ....
x <- c(head(x, lines), more)
}
} else {
x <- c(if (abs(lines[1])>1) more else NULL,
x[lines],
if (length(x)>lines[abs(length(lines))]) more else NULL
)
}
# paste these lines together
x <- paste(c(x, ""), collapse = "\n")
hook_output(x, options)
})

knitr::opts_chunk$set(
comment = "#>",
collapse = TRUE,
warning = FALSE,
message = FALSE
)
```

[![R-CMD-check](https://github.com/ropensci/geojson/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/ropensci/geojson/actions/workflows/R-CMD-check.yaml)
[![rstudio mirror downloads](https://cranlogs.r-pkg.org/badges/geojson)](https://github.com/r-hub/cranlogs.app)
[![cran version](https://www.r-pkg.org/badges/version/geojson)](https://cran.r-project.org/package=geojson)

# geojson

`geojson` aims to deal only with geojson data in a lightweight way.

We've defined classes (`S3`) following the [GeoJSON spec][geojsonspec]. These
classes sort of overlap with `sp`'s classes, but not really. There's also some
overlap in GeoJSON classes with Well-Known Text (WKT) classes, but GeoJSON has a
subset of WKT's classes.

The package [geoops](https://github.com/sckott/geoops) supports manipulations on
the classes defined in this package. This package is used within
[geojsonio](https://github.com/ropensci/geojsonio) to make some tasks easier.

## Installation

Stable CRAN version

```{r eval=FALSE}
install.packages("geojson")
```

Dev version

```{r eval=FALSE}
remotes::install_github("ropensci/geojson")
```

```{r}
library("geojson")
```

## geojson class

Essentially a character string with S3 class `geojson` attached to make it
easy to perform operations on

```{r}
x <- "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-99.74,32.45]},\"properties\":{}}]}"
as.geojson(x)
```

## geometrycollection

```{r}
x <- '{
"type": "GeometryCollection",
"geometries": [
{
"type": "Point",
"coordinates": [100.0, 0.0]
},
{
"type": "LineString",
"coordinates": [ [101.0, 0.0], [102.0, 1.0] ]
}
]
}'
(y <- geometrycollection(x))
```

### inspect the object

get the string

```{r}
y[[1]]
```

get the type

```{r}
geo_type(y)
```

pretty print the geojson

```{r}
geo_pretty(y)
```

write to disk

```{r}
geo_write(y, f <- tempfile(fileext = ".geojson"))
jsonlite::fromJSON(f, FALSE)
```

```{r echo=FALSE}
unlink(f)
```

## properties

Add properties

```{r}
x <- '{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]}'
res <- linestring(x) %>% feature() %>% properties_add(population = 1000)
res
```

Get a property

```{r}
properties_get(res, property = 'population')
```

## crs

Add crs

```{r}
crs <- '{
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
}'
z <- x %>% feature() %>% crs_add(crs)
z
```

Get crs

```{r}
crs_get(z)
```

## bbox

Add bbox

```{r}
tt <- x %>% feature() %>% bbox_add()
tt
```

Get bbox

```{r}
bbox_get(tt)
```

## geojson in data.frame's

```{r}
x <- '{ "type": "Point", "coordinates": [100.0, 0.0] }'
(pt <- point(x))
```

```{r}
library("tibble")
tibble(a = 1:5, b = list(pt))
```

```{r}
x <- '{ "type": "MultiLineString",
"coordinates": [ [ [100.0, 0.0], [101.0, 1.0] ], [ [102.0, 2.0], [103.0, 3.0] ] ] }'
(mls <- multilinestring(x))
```

```{r}
tibble(a = 1:5, b = list(mls))
```

```{r}
tibble(a = 1:5, b = list(pt), c = list(mls))
```

## geobuf

Geobuf is a compact binary encoding for geographic data
using protocol buffers (via the [protolite][])
package.

```{r}
file <- system.file("examples/test.pb", package = "geojson")
(json <- from_geobuf(file))
pb <- to_geobuf(json)
class(pb)
f <- tempfile(fileext = ".pb")
to_geobuf(json, f)
from_geobuf(f)

x <- '{ "type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
]
}'
y <- polygon(x)
to_geobuf(y)

x <- '{"type": "MultiPoint", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }'
y <- multipoint(x)
to_geobuf(y)
```

## newline-delimited GeoJSON

read nd-GeoJSON

```{r}
url <- "https://raw.githubusercontent.com/ropensci/geojson/main/inst/examples/ndgeojson1.json"
f <- tempfile(fileext = ".geojsonl")
download.file(url, f)
x <- ndgeo_read(f, verbose = FALSE)
x
```

```{r echo=FALSE}
unlink(f)
```

Write nd-GeoJSON

One would think we could take the output of `ndego_read()` above
and pass to `ndgeo_write()`. However, in this example, the json is too big
for `jqr` to handle, the underlying parser tool. So here's a smaller
example:

```{r}
file <- system.file("examples", "featurecollection2.geojson",
package = "geojson")
str <- paste0(readLines(file), collapse = " ")
(x <- featurecollection(str))
```

```{r}
outfile <- tempfile(fileext = ".geojson")
ndgeo_write(x, outfile)
jsonlite::stream_in(file(outfile))
```

## Meta

* Please [report any issues or bugs](https://github.com/ropensci/geojson/issues).
* License: MIT
* Get citation information for `geojson` in R doing `citation(package = 'geojson')`
* Please note that this project is released with a [Contributor Code of Conduct][coc].
By participating in this project you agree to abide by its terms.

(This was originally setup without requiring any of the
`GEOS/GDAL` stack but now the package sp depends on sf it can't be avoided without overhaul).

[![ropensci_footer](https://ropensci.org/public_images/github_footer.png)](https://ropensci.org)

[geojsonspec]: https://datatracker.ietf.org/doc/html/rfc7946
[jqr]: https://github.com/ropensci/jqr
[jq]: https://github.com/stedolan/jq
[protolite]: https://github.com/jeroen/protolite
[coc]: https://github.com/ropensci/geojson/blob/main/CODE_OF_CONDUCT.md