From 79238da792bf0c674a096cb706c411e866c96473 Mon Sep 17 00:00:00 2001 From: Scott Chamberlain Date: Sat, 1 Oct 2016 22:41:43 -0700 Subject: [PATCH] added geobuf fxn, example pb file in examples, fill out pkg level man file a bit now importing protolite bumped dev version #10 --- DESCRIPTION | 5 +- NAMESPACE | 15 +++++ R/geobuf.R | 131 +++++++++++++++++++++++++++++++++++++++++ R/geojson-package.R | 5 +- inst/examples/test.pb | Bin 0 -> 577 bytes man/geobuf.Rd | 72 ++++++++++++++++++++++ man/geojson-package.Rd | 5 +- 7 files changed, 229 insertions(+), 4 deletions(-) create mode 100644 R/geobuf.R create mode 100644 inst/examples/test.pb create mode 100644 man/geobuf.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 02b961e..859e4c4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 diff --git a/NAMESPACE b/NAMESPACE index cb2742b..723f9b7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -50,6 +53,16 @@ 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) @@ -57,6 +70,7 @@ export(add_properties) export(as.geojson) export(feature) export(featurecollection) +export(from_geobuf) export(geo_bbox) export(geo_pretty) export(geo_type) @@ -68,6 +82,7 @@ export(multipoint) export(multipolygon) export(point) export(polygon) +export(to_geobuf) import(methods) importClassesFrom(sp,SpatialLines) importClassesFrom(sp,SpatialLinesDataFrame) diff --git a/R/geobuf.R b/R/geobuf.R new file mode 100644 index 0000000..b2ccfb9 --- /dev/null +++ b/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) +} diff --git a/R/geojson-package.R b/R/geojson-package.R index fa05491..2caacac 100644 --- a/R/geojson-package.R +++ b/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 diff --git a/inst/examples/test.pb b/inst/examples/test.pb new file mode 100644 index 0000000000000000000000000000000000000000..f747b61489edb9c7b32d9d37ce495fac0ea7dd81 GIT binary patch literal 577 zcmYjNv5wO~5S?AGH}=?Qb&exNhT>#|2$Df@fII0Let{Bkiom%T&MRX_&gLYHA|W~o zIzE68L08al6{4V~x|TLd$=VlL(QL8v-pqS%7tKXgF2=~`POA3;9KouhOx!55A24-MSoqekSSRT+-oO+J_&%efoR7*$kvT?5=#$`buYF zpT^N7di*G!%7cmc5WXqP@Z|JqbQB#=*OdoA2&IO>m}zp}>si+RSRCn^u4`%oK0#s8 zz}^b*2mU?(xz4sBk|R^;Qtf4=L#Q}vU5D0yFYu1VJj#r~`>i(t{62gaS7h{)^!*xM z_L}X7*?do+jj7JFWq1;*^F)Q~iUByJ8IfsKrbSH=@IKzPOpjAyP)38nR*V|;&g73^ z;MSlc6eukowqL5*8bU|E!@SmMbO7Cuz-+df#