From 0e8097d33c350284ad3ccbfb3a6068c8645e8bf0 Mon Sep 17 00:00:00 2001 From: SymbolixAU Date: Sat, 22 Sep 2018 14:49:58 +1000 Subject: [PATCH] fixes and closes #32 --- DESCRIPTION | 4 +-- R/geojson_sf.R | 15 ++++++++++- cran-comments.md | 17 ++----------- src/sf_geojson.cpp | 12 +++++---- tests/testthat/test-data_types.R | 43 ++++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 23 deletions(-) create mode 100644 tests/testthat/test-data_types.R diff --git a/DESCRIPTION b/DESCRIPTION index 6b3a221..f2e0b83 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: geojsonsf Type: Package Title: GeoJSON to Simple Feature Converter -Version: 1.1.00001 -Date: 2018-05-25 +Version: 1.1.00002 +Date: 2018-09-22 Authors@R: c( person("David", "Cooley", ,"dcooley@symbolix.com.au", role = c("aut", "cre")) ) diff --git a/R/geojson_sf.R b/R/geojson_sf.R index f88bf8f..3a62114 100644 --- a/R/geojson_sf.R +++ b/R/geojson_sf.R @@ -119,7 +119,10 @@ geojson_sf.default <- function(geojson, expand_geometries = F) rcpp_geojson_to_s sf_geojson <- function(sf, atomise = FALSE) UseMethod("sf_geojson") #' @export -sf_geojson.sf <- function(sf, atomise = FALSE) rcpp_sf_to_geojson(sf, atomise) +sf_geojson.sf <- function(sf, atomise = FALSE) { + sf <- handle_dates( sf ) + rcpp_sf_to_geojson(sf, atomise) +} #' sfc to GeoJSON @@ -145,6 +148,16 @@ sfc_geojson.sfc <- function(sfc) rcpp_sfc_to_geojson(sfc) sf_geojson.default <- function(sf, atomise = FALSE) stop("Expected an sf object") sfc_geojson.default <- function(sfc) stop("Expected an sfc object") +date_columns <- function( sf ) names(which(vapply(sf , function(x) { inherits(x, "Date") | inherits(x, "POSIXct") }, T))) + +handle_dates <- function( x ) { + dte <- date_columns( x ) + x[dte] <- lapply(x[dte], as.character) + return( x ) +} + +return_x <- function( x ) x + is_url <- function(geojson) grepl("^https?://", geojson, useBytes=TRUE) read_url <- function(con) { diff --git a/cran-comments.md b/cran-comments.md index dab4384..edd6fe3 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -1,21 +1,8 @@ ## Release Summary -* Updates v1.0 to v1.1 -* Removed dependencies on undeclared packages from unit tests, as per CRAN request - email "CRAN packages maintained by you" from Kurt Hornik, 2018-05-18 (https://cran.r-project.org/web/checks/check_results_geojsonsf.html) +* Updates v1.1 to v1.2 +* fixed bug in handling `Date` and `Posix` clases * Build Note - Fedora clang & gcc : installed size is 5.5mb / 6.8mb - The builds says the `libs` file is 3.7mb/5.0mb. I can't reduce this file size, all the compiled c++ headers and files are required. * New gcc compiler warning : https://www.stats.ox.ac.uk/pub/bdr/gcc8/geojsonsf.out - this warning references the souce C++ library `rapidjson`. There is an issue on their github page to fix this (https://github.com/Tencent/rapidjson/issues/1205). I will update this package once rapidjson has fixed this warning. - -## Test Environments - -* local OS X 15.6.0 (High Sierra) install, R 3.5.0 -* travis-ci (ubuntu 14.04.5, R Under development r74781) -* win-builder (devel and release) - - -## R CMD check results - -* ERRORS : 0 -* WARNINGS : 0 -* NOTES : 0 diff --git a/src/sf_geojson.cpp b/src/sf_geojson.cpp index e9f41e9..9705341 100644 --- a/src/sf_geojson.cpp +++ b/src/sf_geojson.cpp @@ -16,10 +16,10 @@ void get_column_type(Rcpp::List& sf, Rcpp::String col = property_names[i]; SEXP vec = sf[col]; - if (Rf_isFactor(vec)) { + if (Rf_isFactor( vec ) ) { column_types[i] = "String"; } else { - switch(TYPEOF(vec)) { + switch(TYPEOF( vec ) ) { case REALSXP: column_types[i] = "Number"; break; @@ -188,7 +188,7 @@ void write_geojson(Rcpp::String& geojson, SEXP sfg, //geometry_json[i] = add_geometry_to_stream(sfg); if (geom_type == "POINT") { - Rcpp::NumericVector point = as(sfg); + Rcpp::NumericVector point = as< Rcpp::NumericVector >(sfg); point_to_geojson(geojson, point); } else if (geom_type == "MULTIPOINT") { @@ -307,6 +307,7 @@ void write_geometry(SEXP sfg, Rcpp::String& geojson) { void geometry_vector_to_geojson(Rcpp::StringVector& geometry_json, Rcpp::List& sfc) { + SEXP sfg; for (int i = 0; i < sfc.size(); i++) { Rcpp::String geojson; @@ -374,7 +375,8 @@ Rcpp::StringVector rcpp_sf_to_geojson(Rcpp::List sf, bool atomise) { } get_column_type(sf_copy, property_names, column_types); - Rcpp::List sfc = sf_copy[geom_column]; + + Rcpp::List sfc = sf_copy[ geom_column ]; Rcpp::List properties; @@ -394,7 +396,7 @@ Rcpp::StringVector rcpp_sf_to_geojson(Rcpp::List sf, bool atomise) { // TODO: what if there's a mssing element? } - Rcpp::StringVector geometry_json(sfc.length()); + Rcpp::StringVector geometry_json( sfc.length() ); geometry_vector_to_geojson(geometry_json, sfc); json_mat(_, (json_mat.ncol() - 1) ) = geometry_json; Rcpp::StringVector res(json_mat.nrow()); diff --git a/tests/testthat/test-data_types.R b/tests/testthat/test-data_types.R new file mode 100644 index 0000000..36d6840 --- /dev/null +++ b/tests/testthat/test-data_types.R @@ -0,0 +1,43 @@ +context("datatypes") + +test_that("r types are converted correctly", { + + # character, integer, numeric, logical, posixct, posixlt, date + js <- '{"type":"Point","coordinates":[0,0]}' + sf <- geojson_sf(js) + sf$test <- as.Date("2018-01-01") + expect_equal( + geojsonsf::sf_geojson( sf ), + "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"test\":\"2018-01-01\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[0,0]}}]}" + ) + + sf$test <- as.POSIXct("2018-01-01 01:01:59") + expect_equal( + geojsonsf::sf_geojson( sf ), + "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"test\":\"2018-01-01 01:01:59\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[0,0]}}]}" + ) + + sf$test <- TRUE + expect_equal( + geojsonsf::sf_geojson( sf ), + "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"test\":true},\"geometry\":{\"type\":\"Point\",\"coordinates\":[0,0]}}]}" + ) + + sf$test <- "a" + expect_equal( + geojsonsf::sf_geojson( sf ), + "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"test\":\"a\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[0,0]}}]}" + ) + + sf$test <- 1L + expect_equal( + geojsonsf::sf_geojson( sf ), + "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"test\":1},\"geometry\":{\"type\":\"Point\",\"coordinates\":[0,0]}}]}" + ) + + sf$test <- 1.0 + expect_equal( + geojsonsf::sf_geojson( sf ), + "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"test\":1},\"geometry\":{\"type\":\"Point\",\"coordinates\":[0,0]}}]}" + ) +})