From ea8b661aeca87ca30d01bccad6fae415a1fbd248 Mon Sep 17 00:00:00 2001 From: SymbolixAU Date: Fri, 3 Apr 2020 12:27:43 +1100 Subject: [PATCH] some fixes and template arguments for #38 --- DESCRIPTION | 4 +-- inst/include/googlepolylines/encode.hpp | 39 ++++++++++++++----------- src/googlePolylines.cpp | 12 ++++---- src/wkt.cpp | 19 ++++++------ 4 files changed, 40 insertions(+), 34 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 516e648bf..bc65b2085 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -16,8 +16,8 @@ Depends: R (>= 3.3.0) Imports: Rcpp (>= 0.12.13) LinkingTo: - Rcpp, - BH, + Rcpp (>= 1.0.4.6), + BH (>= 1.72.0-3), sfheaders RoxygenNote: 6.1.0 Suggests: diff --git a/inst/include/googlepolylines/encode.hpp b/inst/include/googlepolylines/encode.hpp index c3fc2431f..14c6934cf 100644 --- a/inst/include/googlepolylines/encode.hpp +++ b/inst/include/googlepolylines/encode.hpp @@ -4,7 +4,7 @@ #include #include "googlepolylines/googlepolylines.h" -#include "sfheaders/df/sfg.hpp" +//#include "sfheaders/df/sfg.hpp" #include "sfheaders/sfg/sfg_attributes.hpp" namespace googlepolylines { @@ -37,9 +37,10 @@ namespace encode { encode_number(os, ui); } + template < int RTYPE > inline void encode( - Rcpp::NumericVector& lons, - Rcpp::NumericVector& lats, + Rcpp::Vector< RTYPE >& lons, + Rcpp::Vector< RTYPE >& lats, std::ostringstream& os ) { int plat = 0; @@ -63,31 +64,33 @@ namespace encode { } } + template< int RTYPE > inline std::string encode( - Rcpp::NumericVector& lons, - Rcpp::NumericVector& lats + Rcpp::Vector< RTYPE >& lons, + Rcpp::Vector< RTYPE >& lats ) { std::ostringstream os; encode( lons, lats, os ); - // Rcpp::Rcout << "os" << os.str() << std::endl; return os.str(); } + template< int RTYPE > inline std::string encode( - Rcpp::NumericMatrix& mat + Rcpp::Matrix< RTYPE >& mat ) { if( mat.ncol() < 2 ) { Rcpp::stop("googlepolylines - expecting at least 2 columns in a matrix"); } - Rcpp::NumericVector lons = mat( Rcpp::_, 0 ); - Rcpp::NumericVector lats = mat( Rcpp::_, 1 ); + Rcpp::Vector< RTYPE > lons = mat( Rcpp::_, 0 ); + Rcpp::Vector< RTYPE > lats = mat( Rcpp::_, 1 ); return encode( lons, lats ); } // encode sfg objects + template< int RTYPE > inline Rcpp::StringVector encode_point( - Rcpp::NumericVector& sfg + Rcpp::Vector< RTYPE >& sfg ) { //Rcpp::DataFrame df = sfheaders::df::sfg_to_df(sfg); @@ -96,8 +99,8 @@ namespace encode { if( sfg.length() < 2 ) { Rcpp::stop("googlepolylines - not enough values in a point"); } - Rcpp::NumericVector lons(1); - Rcpp::NumericVector lats(1); + Rcpp::Vector< RTYPE > lons(1); + Rcpp::Vector< RTYPE > lats(1); lons[0] = sfg[0]; lats[0] = sfg[1]; Rcpp::StringVector res(1); @@ -105,8 +108,9 @@ namespace encode { return res; } + template< int RTYPE > inline Rcpp::StringVector encode_multipoint( - Rcpp::NumericMatrix& sfg + Rcpp::Matrix< RTYPE >& sfg ) { if( sfg.ncol() < 2 ) { @@ -124,8 +128,8 @@ namespace encode { for( i = 0; i < n; ++i ) { double lon = sfg( i, 0 ); double lat = sfg( i, 1 ); - Rcpp::NumericVector lons(1); - Rcpp::NumericVector lats(1); + Rcpp::Vector< RTYPE > lons(1); + Rcpp::Vector< RTYPE > lats(1); lons[0] = lon; lats[0] = lat; res[i] = encode( lons, lats ); @@ -133,8 +137,9 @@ namespace encode { return res; } + template< int RTYPE > inline Rcpp::StringVector encode_linestring( - Rcpp::NumericMatrix& sfg + Rcpp::Matrix< RTYPE >& sfg ) { R_xlen_t n = sfg.nrow(); @@ -221,7 +226,7 @@ namespace encode { for( i = 0; i < n; ++i ) { SEXP sfg = sfc[ i ]; - cls = sfheaders::df::getSfgClass( sfg ); + cls = sfheaders::sfg::getSfgClass( sfg ); geometry = cls[1]; Rcpp::StringVector sv; diff --git a/src/googlePolylines.cpp b/src/googlePolylines.cpp index 1dc51c7f6..4bee84fa9 100644 --- a/src/googlePolylines.cpp +++ b/src/googlePolylines.cpp @@ -22,24 +22,24 @@ Rcpp::List rcpp_decode_polyline_list( // a 2-column data.frame? // probably - size_t n = encodedList.size(); + R_xlen_t n = encodedList.size(); Rcpp::List output(n); Rcpp::CharacterVector sfg_dim; std::vector pointsLat; std::vector pointsLon; std::vector col_headers; - - for (size_t i = 0; i < n; i++) { + R_xlen_t i; + for( i = 0; i < n; ++i ) { Rcpp::StringVector polylines = encodedList[i]; sfg_dim = polylines.attr( attribute ); col_headers = get_col_headers(sfg_dim[0]); - size_t pn = polylines.size(); + R_xlen_t pn = polylines.size(); Rcpp::List polyline_output(pn); - for (size_t j = 0; j < pn; j++ ) { + for (R_xlen_t j = 0; j < pn; j++ ) { // If polylines[j] is NA, assign a data frame of NA values if( Rcpp::StringVector::is_na( polylines[j] ) ) { @@ -177,7 +177,7 @@ std::string encode_polyline(){ std::ostringstream os; - for(unsigned int i = 0; i < global_vars::lats.size(); i++){ + for(R_xlen_t i = 0; i < global_vars::lats.size(); i++){ late5 = global_vars::lats[i] * 1e5; lone5 = global_vars::lons[i] * 1e5; diff --git a/src/wkt.cpp b/src/wkt.cpp index 6532ce482..47cfb7c96 100644 --- a/src/wkt.cpp +++ b/src/wkt.cpp @@ -107,14 +107,15 @@ void coordSeparateWKT(std::ostringstream& os) { // [[Rcpp::export]] Rcpp::StringVector rcpp_polyline_to_wkt(Rcpp::List sfencoded) { - unsigned int nrow = sfencoded.size(); + R_xlen_t nrow = sfencoded.size(); Rcpp::StringVector res(nrow); std::string stdspl; Rcpp::CharacterVector cls; Rcpp::StringVector pl; Rcpp::String spl; - - for (size_t i = 0; i < nrow; i++ ){ + R_xlen_t i; + + for ( i = 0; i < nrow; i++ ){ std::ostringstream os; stdspl.clear(); @@ -128,9 +129,9 @@ Rcpp::StringVector rcpp_polyline_to_wkt(Rcpp::List sfencoded) { } beginWKT(os, cls); - unsigned int n = pl.size(); + R_xlen_t n = pl.size(); - for(size_t j = 0; j < n; j ++ ) { + for(R_xlen_t j = 0; j < n; j ++ ) { spl = pl[j]; @@ -211,7 +212,7 @@ void ReplaceStringInPlace(std::string& subject, const std::string& search, * Finds the 'GEOMETRY' text */ std::string geomFromWKT(std::string& pl) { - size_t s = pl.find_first_of("("); + R_xlen_t s = pl.find_first_of("("); std::string geom = pl.substr(0, s); boost::trim(geom); // pl.replace(0, s, ""); @@ -312,7 +313,7 @@ void encode_wkt_multi_polygon(MultiPolygon const& mpl, std::ostringstream& os) { // [[Rcpp::export]] Rcpp::List rcpp_wkt_to_polyline(Rcpp::StringVector wkt) { - size_t n = wkt.length(); + R_xlen_t n = wkt.length(); Rcpp::String r_wkt; std::string str_wkt; std::string geomType; @@ -320,8 +321,8 @@ Rcpp::List rcpp_wkt_to_polyline(Rcpp::StringVector wkt) { Rcpp::CharacterVector sv; Rcpp::List resultPolylines(n); - int lastItem; - unsigned int i; + R_xlen_t lastItem; + R_xlen_t i; for (i = 0; i < n; i++ ) {