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

Add precision variable to geojson_list() #152

Merged
merged 7 commits into from
Oct 14, 2019
71 changes: 44 additions & 27 deletions R/geojson_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#' GeometryCollection.
#' @param group (character) A grouping variable to perform grouping for polygons - doesn't apply
#' for points
#' @param precision desired number of decimal places for the coordinates in the
#' geojson file. Using fewer decimal places can decrease file sizes (at the
#' cost of precision). This changes the underlying precision stored in the data.
#' If you want to change the number of digits displayed use options(digits = <some number>).
#' @param convert_wgs84 Should the input be converted to the \href{https://tools.ietf.org/html/rfc7946}{standard coordinate reference system defined for GeoJSON} (geographic coordinate reference system, using the WGS84 datum, with longitude and latitude units of decimal degrees; EPSG: 4326). Default is \code{FALSE} though this may change in a future package version. This will only work for \code{sf} or \code{Spatial} objects with a CRS already defined. If one is not defined but you know what it is, you may define it in the \code{crs} argument below.
#' @param crs The CRS of the input if it is not already defined. This can be an epsg code as a four or five digit integer or a valid proj4 string. This argument will be ignored if \code{convert_wgs84} is \code{FALSE} or the object already has a CRS.
#' @param ... Ignored
Expand Down Expand Up @@ -81,7 +85,20 @@
#' c(30,40,35,30)))), "2")
#' sp_poly <- SpatialPolygons(list(poly1, poly2), 1:2)
#' geojson_list(sp_poly)
#'
#' # From SpatialPolygons class with precision agreement
#' x_coord <- c(-114.345703125, -114.345703125, -106.61132812499999, -106.61132812499999,-114.345703125)
#' y_coord <- c(39.436192999314095, 43.45291889355468, 43.45291889355468, 39.436192999314095, 39.436192999314095)
#' coords <- cbind(x_coord, y_coord)
#' poly <- Polygon(coords)
#' polys <- Polygons(list(poly), 1)
#' sp_poly2 <- SpatialPolygons(list(polys))
#' geojson_list(sp_poly2, geometry = "polygon", precision = 4)
#'
#' # From SpatialPoints class with precision
#' points <- SpatialPoints(cbind(x_coord,y_coord))
#' geojson_list(points)
#'
#' # From SpatialPolygonsDataFrame class
#' sp_polydf <- as(sp_poly, "SpatialPolygonsDataFrame")
#' geojson_list(input = sp_polydf)
Expand All @@ -91,7 +108,7 @@
#' y <- c(3,2,5,1,4)
#' s <- SpatialPoints(cbind(x,y))
#' geojson_list(s)
#'
#'
#' # From SpatialPointsDataFrame class
#' s <- SpatialPointsDataFrame(cbind(x,y), mtcars[1:5,])
#' geojson_list(s)
Expand Down Expand Up @@ -195,119 +212,119 @@

geojson_list <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type = "FeatureCollection",
convert_wgs84 = FALSE, crs = NULL, ...) {
convert_wgs84 = FALSE, crs = NULL, precision = NULL, ...) {
UseMethod("geojson_list")
}

# spatial classes from sp --------------------------
#' @export
geojson_list.SpatialPolygons <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type = "FeatureCollection",
convert_wgs84 = FALSE, crs = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", convert_wgs84 = convert_wgs84, crs = crs),
convert_wgs84 = FALSE, crs = NULL, precision = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", precision = precision, convert_wgs84 = convert_wgs84, crs = crs),
"SpatialPolygons")
}

#' @export
geojson_list.SpatialPolygonsDataFrame <- function(input, lat = NULL, lon = NULL,
group = NULL, geometry = "point",
type = "FeatureCollection",
convert_wgs84 = FALSE, crs = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", convert_wgs84 = convert_wgs84, crs = crs),
convert_wgs84 = FALSE, crs = NULL, precision = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", precision = precision, convert_wgs84 = convert_wgs84, crs = crs),
"SpatialPolygonsDataFrame")
}

#' @export
geojson_list.SpatialPoints <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type = "FeatureCollection",
convert_wgs84 = FALSE, crs = NULL, ...) {
convert_wgs84 = FALSE, crs = NULL, precision = NULL, ...) {
dat <- SpatialPointsDataFrame(input, data.frame(dat = 1:NROW(input@coords)))
as.geo_list(geojson_rw(dat, target = "list", convert_wgs84 = convert_wgs84, crs = crs), "SpatialPoints")
as.geo_list(geojson_rw(dat, target = "list", precision = precision, convert_wgs84 = convert_wgs84, crs = crs), "SpatialPoints")
}

#' @export
geojson_list.SpatialPointsDataFrame <- function(input, lat = NULL, lon = NULL,
group = NULL, geometry = "point",
type = "FeatureCollection",
convert_wgs84 = FALSE, crs = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", convert_wgs84 = convert_wgs84, crs = crs),
convert_wgs84 = FALSE, crs = NULL, precision = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", precision = precision, convert_wgs84 = convert_wgs84, crs = crs),
"SpatialPointsDataFrame")
}

#' @export
geojson_list.SpatialLines <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type = "FeatureCollection",
convert_wgs84 = FALSE, crs = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", convert_wgs84 = convert_wgs84, crs = crs),
convert_wgs84 = FALSE, crs = NULL, precision = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", precision = precision, convert_wgs84 = convert_wgs84, crs = crs),
"SpatialLines")
}

#' @export
geojson_list.SpatialLinesDataFrame <- function(input, lat = NULL, lon = NULL,
group = NULL, geometry = "point",
type = "FeatureCollection",
convert_wgs84 = FALSE, crs = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", convert_wgs84 = convert_wgs84, crs = crs),
convert_wgs84 = FALSE, crs = NULL, precision = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", precision = precision, convert_wgs84 = convert_wgs84, crs = crs),
"SpatialLinesDataFrame")
}

#' @export
geojson_list.SpatialGrid <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type = "FeatureCollection",
convert_wgs84 = FALSE, crs = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", convert_wgs84 = convert_wgs84, crs = crs),
convert_wgs84 = FALSE, crs = NULL, precision = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", precision = precision, convert_wgs84 = convert_wgs84, crs = crs),
"SpatialGrid")
}

#' @export
geojson_list.SpatialGridDataFrame <- function(input, lat = NULL, lon = NULL,
group = NULL, geometry = "point",
type = "FeatureCollection",
convert_wgs84 = FALSE, crs = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", convert_wgs84 = convert_wgs84, crs = crs),
convert_wgs84 = FALSE, crs = NULL, precision = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", precision = precision, convert_wgs84 = convert_wgs84, crs = crs),
"SpatialGridDataFrame")
}

#' @export
geojson_list.SpatialPixels <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection',
convert_wgs84 = FALSE, crs = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", convert_wgs84 = convert_wgs84, crs = crs),
convert_wgs84 = FALSE, crs = NULL, precision = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", precision = precision, convert_wgs84 = convert_wgs84, crs = crs),
"SpatialPixels")
}

#' @export
geojson_list.SpatialPixelsDataFrame <- function(input, lat = NULL, lon = NULL,
group = NULL, geometry = "point",
type = "FeatureCollection",
convert_wgs84 = FALSE, crs = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", convert_wgs84 = convert_wgs84, crs = crs),
convert_wgs84 = FALSE, crs = NULL, precision = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", precision = precision, convert_wgs84 = convert_wgs84, crs = crs),
"SpatialPixelsDataFrame")
}

# spatial classes from rgeos --------------------------
#' @export
geojson_list.SpatialRings <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection',
convert_wgs84 = FALSE, crs = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", convert_wgs84 = convert_wgs84, crs = crs),
convert_wgs84 = FALSE, crs = NULL, precision = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", precision = precision, convert_wgs84 = convert_wgs84, crs = crs),
"SpatialRings")
}

#' @export
geojson_list.SpatialRingsDataFrame <- function(input, lat = NULL, lon = NULL,
group = NULL, geometry = "point",
type = "FeatureCollection",
convert_wgs84 = FALSE, crs = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", convert_wgs84 = convert_wgs84, crs = crs),
convert_wgs84 = FALSE, crs = NULL, precision = NULL, ...) {
as.geo_list(geojson_rw(input, target = "list", precision = precision, convert_wgs84 = convert_wgs84, crs = crs),
"SpatialRingsDataFrame")
}

#' @export
geojson_list.SpatialCollections <- function(input, lat = NULL, lon = NULL,
group = NULL, geometry = "point",
type = "FeatureCollection",
convert_wgs84 = FALSE, crs = NULL, ...) {
convert_wgs84 = FALSE, crs = NULL, precision = NULL, ...) {
pt <- donotnull(input@pointobj, geojson_rw, target = "list",
convert_wgs84 = convert_wgs84, crs = crs)
ln <- donotnull(input@lineobj, geojson_rw, target = "list",
Expand Down
7 changes: 4 additions & 3 deletions R/zzz.r
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ convert_unsupported_classes <- function(df) {
}

geojson_rw <- function(input, target = c("char", "list"),
convert_wgs84 = FALSE, crs = NULL, ...){
convert_wgs84 = FALSE, crs = NULL,
precision = NULL, ...){

read_fun <- switch(target,
char = geojson_file_to_char,
Expand All @@ -343,14 +344,14 @@ geojson_rw <- function(input, target = c("char", "list"),
if (inherits(input, "SpatialCollections")) {
tmp <- tempfile(fileext = ".geojson")
on.exit(unlink(tmp))
tmp2 <- suppressMessages(geojson_write(input, file = tmp,
tmp2 <- suppressMessages(geojson_write(input, file = tmp, precision = precision,
convert_wgs84 = convert_wgs84, crs = crs))
paths <- vapply(tg_compact(tmp2), "[[", "", "path")
lapply(paths, read_fun, ...)
} else {
tmp <- tempfile(fileext = ".geojson")
on.exit(unlink(tmp))
suppressMessages(geojson_write(input, file = tmp,
suppressMessages(geojson_write(input, file = tmp, precision = precision,
convert_wgs84 = convert_wgs84, crs = crs))
read_fun(tmp, ...)
}
Expand Down
6 changes: 5 additions & 1 deletion man/geojson_list.Rd

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

21 changes: 21 additions & 0 deletions tests/testthat/test-geojson_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ test_that("geojson_list works with numeric inputs", {
)
})

test_that("geojson precision arguement works with sp classes", {
## polygon type
x_coord <- c(-114.345703125, -114.345703125, -106.61132812499999, -106.61132812499999,-114.345703125)
y_coord <- c(39.436192999314095, 43.45291889355468, 43.45291889355468, 39.436192999314095, 39.436192999314095)
coords <- cbind(x_coord, y_coord)
poly <- Polygon(coords)
polys <- Polygons(list(poly), 1)
sp_poly <- SpatialPolygons(list(polys))
expect_equal(
unclass(geojson_list(sp_poly, geometry = "polygon", precision = 4)),
structure(list(type = "FeatureCollection", features = list(structure(list(
type = "Feature", id = as.integer(1), properties = structure(list(dummy = 0)), geometry = structure(list(type = "Polygon",
coordinates = list(list(c(-114.3457, 39.4362
), c(-114.3457, 43.4529), c(-106.6113,
43.4529), c(-106.6113, 39.4362
), c(-114.3457, 39.4362)))), .Names = c("type",
"coordinates"))), .Names = c("type", "id", "properties", "geometry"
)))), .Names = c("type", "features"), from = "SpatialPolygons")
)
})

test_that("geojson_list works with data.frame inputs", {
# From a data.frame to points
expect_equal(
Expand Down