Skip to content

Commit

Permalink
#85 rework geojson_json to readLines instead of fromJSON/toJSON
Browse files Browse the repository at this point in the history
printing of json is now more cumbersome, but could rework which print statement is used to minify on print
  • Loading branch information
sckott committed Jun 3, 2016
1 parent f8e5e40 commit 368cf76
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
27 changes: 13 additions & 14 deletions R/geojson_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,84 +184,83 @@ geojson_json <- function(input, lat = NULL, lon = NULL, group = NULL,
#' @export
geojson_json.SpatialPolygons <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection', ...) {
to_json(geojson_rw(input), ...)
class_json(geojson_rw_(input), ...)
}

#' @export
geojson_json.SpatialPolygonsDataFrame <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection', ...) {
to_json(geojson_rw(input), ...)
class_json(geojson_rw_(input), ...)
}

#' @export
geojson_json.SpatialPoints <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection', ...) {
dat <- SpatialPointsDataFrame(input, data.frame(dat = 1:NROW(input@coords)))
to_json(geojson_rw(dat))
class_json(geojson_rw_(dat))
}

#' @export
geojson_json.SpatialPointsDataFrame <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection', ...) {
to_json(geojson_rw(input), ...)
class_json(geojson_rw_(input), ...)
}

#' @export
geojson_json.SpatialLines <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection', ...) {
to_json(geojson_rw(input), ...)
class_json(geojson_rw_(input), ...)
}

#' @export
geojson_json.SpatialLinesDataFrame <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection', ...) {
to_json(geojson_rw(input), ...)
class_json(geojson_rw_(input), ...)
}

#' @export
geojson_json.SpatialGrid <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection', ...) {
to_json(geojson_rw(input), ...)
class_json(geojson_rw_(input), ...)
}

#' @export
geojson_json.SpatialGridDataFrame <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection', ...) {
to_json(geojson_rw(input), ...)
class_json(geojson_rw_(input), ...)
}

#' @export
geojson_json.SpatialPixels <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection', ...) {
to_json(geojson_rw(input), ...)
class_json(geojson_rw_(input), ...)
}

#' @export
geojson_json.SpatialPixelsDataFrame <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection', ...) {
to_json(geojson_rw(input), ...)
class_json(geojson_rw_(input), ...)
}

# spatial classes from rgeos --------------------------
#' @export
geojson_json.SpatialRings <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection', ...) {
to_json(geojson_rw(input), ...)
class_json(geojson_rw_(input), ...)
}

#' @export
geojson_json.SpatialRingsDataFrame <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection', ...) {
to_json(geojson_rw(input), ...)
class_json(geojson_rw_(input), ...)
}

#' @export
geojson_json.SpatialCollections <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection', ...) {
lapply(geojson_rw(input), to_json)
lapply(geojson_rw_(input, ...), class_json)
}


# regular R classes --------------------------
#' @export
geojson_json.numeric <- function(input, lat = NULL, lon = NULL, group = NULL,
Expand Down
23 changes: 22 additions & 1 deletion R/zzz.r
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ to_json <- function(x, ...) {
class = c('json','geo_json'))
}

class_json <- function(x, ...) {
structure(x, class = c('json','geo_json'))
}

list_to_geo_list <- function(x, lat, lon, geometry = "point", type = "FeatureCollection", unnamed = FALSE, group=NULL){
nn <- switch(type, FeatureCollection = "features", GeometryCollection = "geometries")
geom <- capwords(match.arg(geometry, c("point", "polygon")))
Expand Down Expand Up @@ -297,7 +301,7 @@ convert_ordered <- function(df) {
}

geojson_rw <- function(input, ...){
if (is(input, "SpatialCollections")) {
if (inherits(input, "SpatialCollections")) {
tmp <- tempfile(fileext = ".geojson")
tmp2 <- suppressMessages(geojson_write(input, file = tmp))
paths <- vapply(tg_compact(tmp2), "[[", "", "path")
Expand All @@ -309,6 +313,23 @@ geojson_rw <- function(input, ...){
}
}

geojson_rw_ <- function(input, ...){
if (inherits(input, "SpatialCollections")) {
tmp <- tempfile(fileext = ".geojson")
tmp2 <- suppressMessages(geojson_write(input, file = tmp))
paths <- vapply(tg_compact(tmp2), "[[", "", "path")
lapply(paths, readbyline, ...)
} else {
tmp <- tempfile(fileext = ".geojson")
suppressMessages(geojson_write(input, file = tmp))
readbyline(tmp, ...)
}
}

readbyline <- function(x, minify = TRUE, ...) {
paste0(readLines(x, ...), collapse = "\n")
}

capwords <- function(s, strict = FALSE, onlyfirst = FALSE) {
cap <- function(s) paste(toupper(substring(s, 1, 1)),
{s <- substring(s, 2); if (strict) tolower(s) else s}, sep = "", collapse = " " )
Expand Down
1 change: 1 addition & 0 deletions geojsonio.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ AutoAppendNewline: Yes
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageCheckArgs: --as-cran
2 changes: 1 addition & 1 deletion man/projections.Rd

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

0 comments on commit 368cf76

Please sign in to comment.