Skip to content

Commit

Permalink
fixes and closes #32
Browse files Browse the repository at this point in the history
  • Loading branch information
SymbolixAU committed Sep 22, 2018
1 parent 0353c16 commit 0e8097d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 23 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", ,"[email protected]", role = c("aut", "cre"))
)
Expand Down
15 changes: 14 additions & 1 deletion R/geojson_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
17 changes: 2 additions & 15 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -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
12 changes: 7 additions & 5 deletions src/sf_geojson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Rcpp::NumericVector>(sfg);
Rcpp::NumericVector point = as< Rcpp::NumericVector >(sfg);
point_to_geojson(geojson, point);

} else if (geom_type == "MULTIPOINT") {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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());
Expand Down
43 changes: 43 additions & 0 deletions tests/testthat/test-data_types.R
Original file line number Diff line number Diff line change
@@ -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]}}]}"
)
})

0 comments on commit 0e8097d

Please sign in to comment.