Skip to content

Commit

Permalink
added geobuf fxn, example pb file in examples, fill out pkg level man…
Browse files Browse the repository at this point in the history
… file a bit

now importing protolite
bumped dev version
#10
  • Loading branch information
sckott committed Oct 2, 2016
1 parent 67e0bea commit 79238da
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 4 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Expand Up @@ -14,8 +14,9 @@ LazyData: true
Imports:
methods,
sp,
jsonlite (>= 0.9.19),
jqr (>= 0.2.0),
jsonlite (>= 1.1),
protolite (>= 1.5),
jqr (>= 0.2.4),
geojsonlint (>= 0.1.0),
magrittr,
lazyeval
Expand Down
15 changes: 15 additions & 0 deletions NAMESPACE
Expand Up @@ -13,6 +13,9 @@ S3method(featurecollection,character)
S3method(featurecollection,default)
S3method(featurecollection,geofeature)
S3method(featurecollection,list)
S3method(from_geobuf,character)
S3method(from_geobuf,default)
S3method(from_geobuf,raw)
S3method(geo_bbox,character)
S3method(geo_bbox,default)
S3method(geo_bbox,geofeature)
Expand Down Expand Up @@ -50,13 +53,24 @@ S3method(print,geomultipolygon)
S3method(print,geopoint)
S3method(print,geopolygon)
S3method(summary,geojson)
S3method(to_geobuf,character)
S3method(to_geobuf,default)
S3method(to_geobuf,geofeature)
S3method(to_geobuf,geolinestring)
S3method(to_geobuf,geomultilinestring)
S3method(to_geobuf,geomultipoint)
S3method(to_geobuf,geomultipolygon)
S3method(to_geobuf,geopoint)
S3method(to_geobuf,geopolygon)
S3method(to_geobuf,json)
export("%>%")
export(add_bbox)
export(add_crs)
export(add_properties)
export(as.geojson)
export(feature)
export(featurecollection)
export(from_geobuf)
export(geo_bbox)
export(geo_pretty)
export(geo_type)
Expand All @@ -68,6 +82,7 @@ export(multipoint)
export(multipolygon)
export(point)
export(polygon)
export(to_geobuf)
import(methods)
importClassesFrom(sp,SpatialLines)
importClassesFrom(sp,SpatialLinesDataFrame)
Expand Down
131 changes: 131 additions & 0 deletions R/geobuf.R
@@ -0,0 +1,131 @@
#' Geobuf
#'
#' @name geobuf
#' @export
#' @param x (character) a file or raw object for \code{from_geobuf}, and
#' json string for \code{to_geobuf}
#' @param file (character) file to write protobuf to. if NULL, geobuf
#' raw binary returned
#' @param decimals (integer) how many decimals (digits behind the dot) to
#' store for numbers
#' @return for \code{from_geobuf} JSON as a character string, and for
#' \code{to_geobuf} raw or file written to disk
#' @details \code{from_geobuf} uses \code{\link[protolite]{geobuf2json}},
#' while \code{to_geobuf} uses \code{\link[protolite]{json2geobuf}}
#'
#' Note that \pkg{protolite} expects either a \strong{Feature},
#' \strong{FeatureCollection}, or \strong{Geometry} class geojson
#' object, Thus, for \code{to_geobuf} we check the geojson class, and
#' convert to a \strong{Feature} if the class is something other than
#' the acceptable set.
#' @examples
#' file <- system.file("examples/test.pb", package = "geojson")
#' (json <- from_geobuf(file))
#' from_geobuf(file, pretty = TRUE)
#' pb <- to_geobuf(json)
#' f <- tempfile(fileext = ".pb")
#' to_geobuf(json, f)
#' from_geobuf(f)
#'
#' object.size(json)
#' object.size(pb)
#' file.info(file)$size
#' file.info(f)$size
#'
#' file <- system.file("examples/featurecollection1.geojson",
#' package = "geojson")
#' json <- paste0(readLines(file), collapse = "")
#' to_geobuf(json)
#'
#' # other geojson class objects
#' 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)
from_geobuf <- function(x, pretty = FALSE) {
UseMethod("from_geobuf")
}

#' @export
from_geobuf.default <- function(x, pretty = FALSE) {
stop("no 'from_geobuf' for ", class(x), call. = FALSE)
}

#' @export
from_geobuf.raw <- function(x, pretty = FALSE) {
protolite::geobuf2json(x, pretty = pretty)
}

#' @export
from_geobuf.character <- function(x, pretty = FALSE) {
if (!file.exists(x)) stop(x, " does not exist", call. = FALSE)
protolite::geobuf2json(x, pretty = pretty)
}

#' @export
#' @rdname geobuf
to_geobuf <- function(x, file = NULL, decimals = 6) {
UseMethod("to_geobuf")
}

#' @export
to_geobuf.default <- function(x, file = NULL, decimals = 6) {
stop("no 'to_geobuf' method for ", class(x), call. = FALSE)
}

#' @export
to_geobuf.json <- function(x, file = NULL, decimals = 6) {
to_geobuf(unclass(x), file, decimals)
}

#' @export
to_geobuf.character <- function(x, file = NULL, decimals = 6) {
togb(x, file, decimals)
}

#' @export
to_geobuf.geofeature <- function(x, file = NULL, decimals = 6) {
togb(as_feature(x[1]), file, decimals)
}

#' @export
to_geobuf.geolinestring <- function(x, file = NULL, decimals = 6) {
togb(as_feature(x[1]), file, decimals)
}

#' @export
to_geobuf.geomultilinestring <- function(x, file = NULL, decimals = 6) {
togb(as_feature(x[1]), file, decimals)
}

#' @export
to_geobuf.geomultipoint <- function(x, file = NULL, decimals = 6) {
togb(as_feature(x[1]), file, decimals)
}

#' @export
to_geobuf.geomultipolygon <- function(x, file = NULL, decimals = 6) {
togb(as_feature(x[1]), file, decimals)
}

#' @export
to_geobuf.geopoint <- function(x, file = NULL, decimals = 6) {
togb(as_feature(x[1]), file, decimals)
}

#' @export
to_geobuf.geopolygon <- function(x, file = NULL, decimals = 6) {
togb(as_feature(x[1]), file, decimals)
}

togb <- function(x, file = NULL, decimals = 6) {
tmp <- protolite::json2geobuf(x, decimals = decimals)
if (is.null(file)) tmp else writeBin(tmp, con = file)
}
5 changes: 4 additions & 1 deletion R/geojson-package.R
@@ -1,10 +1,13 @@
#' geojson
#' @title geojson
#'
#' @description Classes for GeoJSON to make working with GeoJSON easier
#'
#' @importFrom jsonlite fromJSON toJSON
#' @importFrom jqr jq
#' @importFrom geojsonlint geojson_hint geojson_lint
#' @import methods
#' @name geojson-package
#' @author Scott Chamberlain, Jeroen Ooms
#' @aliases geojson
#' @docType package
#' @keywords package
Expand Down
Binary file added inst/examples/test.pb
Binary file not shown.
72 changes: 72 additions & 0 deletions man/geobuf.Rd

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

5 changes: 4 additions & 1 deletion man/geojson-package.Rd

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

0 comments on commit 79238da

Please sign in to comment.