-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathREADME.Rmd
182 lines (126 loc) · 4.71 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
---
title: geojsonsf
output: github_document
always_allow_html: yes
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "# ",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/geojsonsf)](https://CRAN.R-project.org/package=geojsonsf)
![downloads](http://cranlogs.r-pkg.org/badges/grand-total/geojsonsf)
[![CRAN RStudio mirror downloads](http://cranlogs.r-pkg.org/badges/geojsonsf)](https://CRAN.R-project.org/package=geojsonsf)
[![Github Stars](https://img.shields.io/github/stars/SymbolixAU/geojsonsf.svg?style=social&label=Github)](https://github.com/SymbolixAU/geojsonsf)
[![R build status](https://github.com/symbolixau/geojsonsf/workflows/R-CMD-check/badge.svg)](https://github.com/symbolixau/geojsonsf/actions)
[![Coverage Status](https://codecov.io/github/SymbolixAU/geojsonsf/coverage.svg?branch=master)](https://codecov.io/github/SymbolixAU/geojsonsf?branch=master)
--
## geojsonsf
A simple, low-dependency and **fast** converter between GeoJSON and Simple Feature objects in R.
---
**v1.3.2**
Converts
- GeoJSON --> `sf`
- GeoJSON --> `sfc`
- `sf` --> GeoJSON
- `sfc` --> GeoJSON
- GeoJSON --> Well-known text
- data.frame --> GeoJSON (POINT only)
As per GeoJSON ([RFC 7946 specification)](https://tools.ietf.org/html/rfc7946#page-11), foreign members are ignored, and nested objects and arrays inside the `properties` object are converted to string/characters.
Also, as per the specification, **CRS**
> The coordinate reference system for all GeoJSON coordinates is a
geographic coordinate reference system, using the World Geodetic
System 1984 (WGS 84) [WGS84] datum, with longitude and latitude units
of decimal degrees. This is equivalent to the coordinate reference
system identified by the Open Geospatial Consortium (OGC) URN
urn:ogc:def:crs:OGC::CRS84
From **v1.3.2**, if your coordinates are in a different CRS you can specify the CRS & proj4string values in the `geojson_sf()` and `geojson_sfc()` functions.
## Installation
Install the CRAN version with
```{r, eval = F}
install.packages("geojsonsf")
```
To install the development version
```{r gh-installation, eval = FALSE}
# install.packages("devtools")
devtools::install_github("SymbolixAU/geojsonsf")
```
## Why did you build it?
To quickly parse between GeoJSON and `sf` objects, and to handle cases not supported by `sf`, e.g. arrays of geometries
```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(geojsonsf)
library(sf) ## Loaded for sf print methods
```
## What do you mean, 'cases not supported'
For example, `sf` can't read an array of GeoJSON objects, so I wanted to make this work
```{r}
js <- c(
'[
{"type":"Point","coordinates":[0,0]},
{"type":"LineString","coordinates":[[-1,-1],[1,1]]},
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {"id":1},
"geometry": {"type": "Point", "coordinates": [100.0, 0.0]}
}
]
}
]'
)
sf <- geojson_sf( js )
sf
```
And going the other way you can also return a vector of GeoJSON
```{r}
js <- sf_geojson( sf, atomise = T )
js
```
### What's the benefit of 'atomising'?
It's useful for when you work with geospatial databases and want an individual record for each individual feature.
### What happens if you don't `atomise`?
You get a single GeoJSON object
```{r}
sf_geojson( sf )
```
### Can you remove the properites and just return the geometries
Yes. Call `sfc_geojson()` on the `sfc` object.
```{r}
sfc_geojson( sf$geometry )
```
### If I have an `sf` object without any properties, why does it 'atomise' by default?
```{r}
sf$id <- NULL
sf_geojson( sf )
```
The `simplify` argument is `TRUE` by default, and it will try and 'simplify' the GeoJSON. If there are no properties in the `sf` object, then the GeoJSON won't have any properties.
However, if you set `simplify = FALSE` you'll get a FeatureCollection with an empty properties field.
```{r}
sf_geojson(sf, simplify = F)
```
### How fast is it?
This benchmark shows a comparison with `library(sf)` for converting a string of GeoJSON of 3,221 counties in the US in to an `sf` object
```{r,eval= FALSE}
myurl <- "http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json"
geo <- readLines(myurl)
geo <- paste0(geo, collapse = "")
library(microbenchmark)
microbenchmark(
geojsonsf = {
geojson_sf(geo)
},
sf = {
sf::st_read(geo, quiet = T)
},
times = 2
)
#Unit: milliseconds
# expr min lq mean median uq max neval
# geojsonsf 709.2268 709.2268 722.0626 722.0626 734.8984 734.8984 2
# sf 1867.6840 1867.6840 1958.7968 1958.7968 2049.9097 2049.9097 2
```