Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue70 #73

Merged
merged 5 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Generated by roxygen2: do not edit by hand

S3method(df_geojson,data.frame)
S3method(geojson_sf,character)
S3method(geojson_sf,connection)
S3method(geojson_sf,default)
S3method(geojson_sf,geojson)
S3method(geojson_sf,numeric)
S3method(geojson_sfc,character)
S3method(geojson_sfc,connection)
S3method(geojson_sfc,default)
S3method(geojson_sfc,geojson)
S3method(geojson_to_sf,character)
S3method(geojson_to_sf,connection)
S3method(geojson_to_sf,default)
S3method(geojson_to_sf,geojson)
S3method(geojson_to_sf,numeric)
S3method(geojson_to_sfc,character)
S3method(geojson_to_sfc,connection)
S3method(geojson_to_sfc,default)
S3method(geojson_to_sfc,geojson)
S3method(print,geojson)
S3method(sf_geojson,default)
S3method(sf_geojson,sf)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

## v1.3.2

* `crs` and `proj4string` arguments to `geojson_sf` and `geojson_sfc`
* fix crash due to factor levels [issue 62](https://github.com/SymbolixAU/geojsonsf/issues/62)
* fix invalid GeoJSON when NA elements [issue 63](https://github.com/SymbolixAU/geojsonsf/issues/63)

Expand Down
74 changes: 60 additions & 14 deletions R/geojson_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#'
#' @param geojson string or vector of GeoJSON, or a URL or file pointing to a geojson file
#' @param expand_geometries logical indicating whether to unnest GEOMETRYCOLLECTION rows. see details
#' @param crs coordiante reference system. See Details
#' @param proj4string proj4string. See Details
#'
#' @details
#' specifying \code{expand_geometries = TRUE} will expand individual \code{GEOMETRYCOLLECTION}
Expand All @@ -13,6 +15,15 @@
#' The \code{GEOMETRYCOLLECTION} information is not kept when using \code{expand_geometries = TRUE}. Therefore,
#' it is not possible to reconstruct the \code{GEOMETRYCOLLECTION} after unnesting it.
#'
#' Geojson specification RFC7946 \link{https://tools.ietf.org/html/rfc7946#page-12}
#' says all CRS should be 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
#'
#' \code{geojson_sfc} and \code{geojson_sf} automatically set the CRS to WGS 84.
#' The fields \code{crs} and \code{proj4string} let you to overwrite the defaults.
#'
#' @examples
#'
#' ## character string of GeoJSON
Expand All @@ -35,31 +46,40 @@
#' }
#'
#' @export
geojson_sfc <- function(geojson, expand_geometries = FALSE) UseMethod("geojson_sfc")
geojson_sfc <- function(geojson, expand_geometries = FALSE, crs = NULL, proj4string = NULL) {
sfc <- geojson_to_sfc( geojson, expand_geometries )
if( !is.null( crs ) | !is.null( proj4string ) ) {
sfc <- set_crs( sfc, crs, proj4string )
}
return( sfc )
}

geojson_to_sfc <- function( geojson, expand_geometries ) UseMethod("geojson_to_sfc")

#' @export
geojson_sfc.geojson <- function( geojson, expand_geometries = FALSE) geojson_sfc.character(geojson, expand_geometries)
geojson_to_sfc.geojson <- function( geojson, expand_geometries = FALSE, crs = NULL, proj4string = NULL) {
geojson_to_sfc.character(geojson, expand_geometries)
}

#' @export
geojson_sfc.character <- function(geojson, expand_geometries = FALSE) {
geojson_to_sfc.character <- function(geojson, expand_geometries = FALSE, crs = NULL, proj4string = NULL) {

if(length(geojson) > 1) {
return(rcpp_geojson_to_sfc(geojson, expand_geometries))
}
if (is_url(geojson)) {
return(geojson_sfc(curl::curl(geojson), expand_geometries))
return(geojson_to_sfc(curl::curl(geojson), expand_geometries))
} else if (file.exists(geojson) ) {
return(rcpp_read_sfc_file(normalizePath(geojson), expand_geometries))
}
return(rcpp_geojson_to_sfc(geojson, expand_geometries))
}

#' @export
geojson_sfc.connection <- function(geojson, expand_geometries = FALSE) geojson_sfc(read_url(geojson), expand_geometries)
geojson_to_sfc.connection <- function(geojson, expand_geometries = FALSE, crs = NULL, proj4string = NULL) geojson_sfc(read_url(geojson), expand_geometries)

#' @export
geojson_sfc.default <- function(geojson, expand_geometries = FALSE) rcpp_geojson_to_sfc(geojson, expand_geometries)
geojson_to_sfc.default <- function(geojson, expand_geometries = FALSE, crs = NULL, proj4string = NULL) rcpp_geojson_to_sfc(geojson, expand_geometries)

#' Geojson to sf
#'
Expand All @@ -83,39 +103,65 @@ geojson_sfc.default <- function(geojson, expand_geometries = FALSE) rcpp_geojson
#'
#' @inherit geojson_sfc params details
#' @export
geojson_sf <- function(geojson, expand_geometries = FALSE) UseMethod("geojson_sf")
geojson_sf <- function(geojson, expand_geometries = FALSE, crs = NULL, proj4string = NULL ) {
sf <- geojson_to_sf( geojson, expand_geometries )
if( !is.null( crs ) | !is.null( proj4string ) ) {
sf[["geometry"]] <- set_crs( sf[["geometry"]], crs, proj4string )
}
return( sf )
}

geojson_to_sf <- function( geojson, expand_geometries ) {
UseMethod("geojson_to_sf")
}

#' @export
geojson_sf.geojson <- function( geojson, expand_geometries = FALSE) geojson_sf.character(geojson, expand_geometries)
geojson_to_sf.geojson <- function( geojson, expand_geometries = FALSE) {
geojson_to_sf.character(geojson, expand_geometries)
}

#' @export
geojson_sf.character <- function(geojson, expand_geometries = FALSE) {
geojson_to_sf.character <- function(geojson, expand_geometries = FALSE) {

if(length(geojson) > 1) {
return(rcpp_geojson_to_sf(geojson, expand_geometries))
}
if (is_url(geojson)) {
return(geojson_sf(curl::curl(geojson), expand_geometries))
return(geojson_to_sf(curl::curl(geojson), expand_geometries))
} else if (file.exists(geojson) ) {
return(rcpp_read_sf_file(normalizePath(geojson), expand_geometries))
}
return(rcpp_geojson_to_sf(geojson, expand_geometries))
}

#' @export
geojson_sf.connection <- function(geojson, expand_geometries = F) geojson_sf(read_url(geojson), expand_geometries)
geojson_to_sf.connection <- function(geojson, expand_geometries = F) {
geojson_sf(read_url(geojson), expand_geometries)
}

#' @export
geojson_sf.numeric <- function(geojson, ... ) stop("Numeric vectors are not valid GeoJSON")
geojson_to_sf.numeric <- function(geojson, ... ) stop("Numeric vectors are not valid GeoJSON")

#' @export
geojson_sf.default <- function(geojson, expand_geometries = F) rcpp_geojson_to_sf(geojson, expand_geometries)

geojson_to_sf.default <- function(geojson, expand_geometries = F) {
rcpp_geojson_to_sf(geojson, expand_geometries)
}



date_columns <- function( sf ) names(which(vapply(sf , function(x) { inherits(x, "Date") | inherits(x, "POSIXct") }, T)))

set_crs <- function(sfc, crs, proj4string ) {
crs <- list(
epsg = ifelse(is.null(crs),NA_integer_,crs)
, proj4string = ifelse(is.null(proj4string),"",proj4string)
)
attr( crs, "class" ) <- "crs"
attr( sfc, "crs" ) <- crs
return( sfc )
}


handle_dates <- function( x ) {
dte <- date_columns( x )
x[dte] <- lapply(as.data.frame(x)[dte], as.character)
Expand Down
24 changes: 18 additions & 6 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ A simple, low-dependency and **fast** converter between GeoJSON and Simple Featu

---

**v1.2.1**
**v1.3.2**

Converts

Expand All @@ -41,6 +41,16 @@ Converts

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

Expand Down Expand Up @@ -144,7 +154,7 @@ sf_geojson(sf, simplify = F)

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, warning=FALSE}
```{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 = "")
Expand All @@ -160,6 +170,11 @@ microbenchmark(
},
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
```

### Does it work?
Expand All @@ -184,10 +199,7 @@ google_map() %>%
knitr::include_graphics("./man/figures/GeoJSONSF.png")
```

```{r}
sf <- sf::st_read(geo, quiet = T)
plot(st_geometry(sf[!sf$STATE %in% c("02", "15", "72"), ]))
```




Expand Down
37 changes: 20 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Simple Feature objects in R.

-----

**v1.2.1**
**v1.3.2**

Converts

Expand All @@ -37,6 +37,19 @@ 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
Expand Down Expand Up @@ -154,8 +167,7 @@ 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
string of GeoJSON of 3,221 counties in the US in to an `sf` object

``` r
myurl <- "http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json"
Expand All @@ -173,13 +185,11 @@ microbenchmark(
},
times = 2
)
# Unit: milliseconds
# expr min lq mean median uq max
# geojsonsf 709.2268 709.2268 722.0626 722.0626 734.8984 734.8984
# sf 1867.6840 1867.6840 1958.7968 1958.7968 2049.9097 2049.9097
# neval
# 2
# 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
```

### Does it work?
Expand All @@ -205,10 +215,3 @@ google_map() %>%
```

<img src="./man/figures/GeoJSONSF.png" width="100%" />

``` r
sf <- sf::st_read(geo, quiet = T)
plot(st_geometry(sf[!sf$STATE %in% c("02", "15", "72"), ]))
```

<img src="man/figures/README-unnamed-chunk-12-1.png" width="100%" />
16 changes: 15 additions & 1 deletion man/geojson_sf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion man/geojson_sfc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading