From a64bf39d1fc4e6c27c431e673ac875ab56ac2450 Mon Sep 17 00:00:00 2001 From: SymbolixAU Date: Thu, 30 Aug 2018 12:20:59 +1000 Subject: [PATCH] empty points go to 0,0 - is this correct #32 --- R/scratch.R | 63 +++++++++++++++++++++++++++++++++++++++++++++++++- R/sfencoded.R | 21 +++++++++++------ src/encode.cpp | 16 +++++++------ 3 files changed, 85 insertions(+), 15 deletions(-) diff --git a/R/scratch.R b/R/scratch.R index 0e2c3e2e8..7ca1a1849 100644 --- a/R/scratch.R +++ b/R/scratch.R @@ -24,26 +24,87 @@ # ept <- sf::st_sfc(sf::st_point()) # pt <- sf::st_sfc(sf::st_point(c(1,1))) # +# sfept <- sf::st_sf(geometry = ept) +# sfpt <- sf::st_sf(geometry = pt) +# # emp <- sf::st_sfc(sf::st_multipoint()) # mp <- sf::st_sfc(sf::st_multipoint(matrix(1:4, ncol = 2))) # +# sfemp <- sf::st_sf(geometry = emp) +# sfmp <- sf::st_sf(geometry = mp ) +# # el <- sf::st_sfc(sf::st_linestring()) # l <- sf::st_sfc(sf::st_linestring(matrix(1:4, ncol = 2))) # +# sfel <- sf::st_sf(geometry = el ) +# sfl <- sf::st_sf(geometry = l ) +# # eml <- sf::st_sfc(sf::st_multilinestring()) # ml <- sf::st_sfc(sf::st_multilinestring(x = list(matrix(1:4, ncol = 2)))) # +# sfeml <- sf::st_sf(geometry = eml ) +# sfml <- sf::st_sf(geometry = ml ) +# # epl <- sf::st_sfc(sf::st_polygon()) -# pl <- sf::st_sfc(sf::st_multipolygon(x = list(sf::st_polygon(list(x = matrix(c(0,0,1,0,1,1,0,1,0,0), ncol = 2, byrow = T)))))) +# pl <- sf::st_sfc(sf::st_polygon(list(x = matrix(c(0,0,1,0,1,1,0,1,0,0), ncol = 2, byrow = T)))) +# +# sfepl <- sf::st_sf(geometry = epl) +# sfpl <- sf::st_sf(geometry = pl) +# +# empl <- sf::st_sfc(sf::st_multipolygon()) +# mpl <- sf::st_sfc(sf::st_multipolygon(x = list(sf::st_polygon(list(x = matrix(c(0,0,1,0,1,1,0,1,0,0), ncol = 2, byrow = T)))))) +# +# sfempl <- sf::st_sf(geometry = empl) +# sfmpl <- sf::st_sf(geometry = mpl) +# +# # # encode(ept) # encode(pt) +# +# encode(sfept) +# encode(sfpt) +# # encode(emp) # encode(mp) +# +# encode( sfemp ) +# encode( sfmp ) +# # encode(el) # encode(l) +# +# encode( sfel ) +# encode( sfl ) +# # encode(eml) # encode(ml) +# +# encode( sfeml ) +# encode( sfml ) +# # encode(epl) # encode(pl) # +# encode( sfepl ) +# encode( sfpl ) +# +# encode(empl) +# encode(mpl) +# +# encode( sfempl ) +# encode( sfmpl ) +# +# +# library(googleway) +# set_key(read.dcf("~/Documents/.googleAPI", fields = "GOOGLE_MAP_KEY")) +# +# google_map() %>% +# add_markers(sfemp) +# +# google_map() %>% +# add_polylines(data = sfel) +# +# google_map() %>% +# add_polylines(data = sfeml) +# diff --git a/R/sfencoded.R b/R/sfencoded.R index f2bafbd91..898d6e45f 100644 --- a/R/sfencoded.R +++ b/R/sfencoded.R @@ -170,17 +170,24 @@ printSfEncodedPrefix <- function(e, encType) { if(encType == "sfencoded") { e <- vapply(e, function(z) { - paste0( - attr(z, "sfc")[2], ": ", - substr(z[1], 1, pmin(nchar(z[1]), 20)), - "..." + m <- pmin(nchar(z[1]), 20) + a <- attr(z, "sfc")[2] + ifelse(is.na(m), paste0(a, ": EMPTY"), + paste0( + a, ": ", + substr(z[1], 1, m) + , ifelse(m >= 20, "...", "") + ) ) }, "" ) } else { e <- vapply(e, function(z) { - paste0( - substr(z[1], 1, pmin(nchar(z[1]), 20)), - "..." + m <- pmin(nchar(z[1]), 20) + ifelse(is.na(m), "EMPTY", + paste0( + substr(z[1], 1, m), + ifelse(m >= 20, "...", "") + ) ) }, "" ) } diff --git a/src/encode.cpp b/src/encode.cpp index 08047b619..0948f4589 100644 --- a/src/encode.cpp +++ b/src/encode.cpp @@ -298,15 +298,14 @@ void write_data(std::ostringstream& os, std::ostringstream& oszm, Rcpp::Characte Rcpp::List rcpp_encodeSfGeometry(Rcpp::List sfc, bool strip){ Rcpp::CharacterVector cls_attr = sfc.attr("class"); - - int db = 0; - + Rcpp::CharacterVector sfg_dim; int dim_divisor; Rcpp::List output(sfc.size()); Rcpp::List output_zm(sfc.size()); int lastItem; + Rcpp::List thisSfc; // TODO(empty geometries should not enter this list and return something?) @@ -317,11 +316,14 @@ Rcpp::List rcpp_encodeSfGeometry(Rcpp::List sfc, bool strip){ Rcpp::checkUserInterrupt(); sfg_dim = getSfClass(sfc[i]); - - make_dim_divisor(sfg_dim[0], &dim_divisor); + thisSfc = sfc[i]; + + if (thisSfc.size() > 0 ) { - write_data(os, oszm, sfg_dim, dim_divisor, sfc[i], cls_attr[0], 0); - + make_dim_divisor(sfg_dim[0], &dim_divisor); + + write_data(os, oszm, sfg_dim, dim_divisor, sfc[i], cls_attr[0], 0); + } std::string str = os.str(); // std::string zmstr = oszm.str();