Skip to content

Commit

Permalink
hmmm..... #38
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed Apr 3, 2020
1 parent 349042a commit 5ef94e3
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 97 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ LinkingTo:
Rcpp (>= 1.0.4.6),
BH (>= 1.72.0-3),
sfheaders
RoxygenNote: 6.1.0
RoxygenNote: 7.1.0
Suggests:
covr,
knitr,
Expand Down
175 changes: 83 additions & 92 deletions inst/include/googlepolylines/encode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

//#include "sfheaders/df/sfg.hpp"
#include "sfheaders/sfg/sfg_attributes.hpp"
#include "sfheaders/utils/lists/list.hpp"

namespace googlepolylines {
namespace encode {
Expand Down Expand Up @@ -89,13 +90,10 @@ namespace encode {

// encode sfg objects
template< int RTYPE >
inline Rcpp::StringVector encode(
inline std::string encode(
Rcpp::Vector< RTYPE >& sfg
) {

//Rcpp::DataFrame df = sfheaders::df::sfg_to_df(sfg);
// but I don't know what type of sfg it is yet...

if( sfg.length() < 2 ) {
Rcpp::stop("googlepolylines - not enough values in a point");
}
Expand All @@ -104,8 +102,9 @@ namespace encode {
lons[0] = sfg[0];
lats[0] = sfg[1];
Rcpp::StringVector res(1);
res[0] = encode( lons, lats );
return res;
return encode( lons, lats );
// res[0] = encode( lons, lats );
// return res;
}

// template< int RTYPE >
Expand Down Expand Up @@ -162,6 +161,7 @@ namespace encode {
Rcpp::IntegerVector iv = Rcpp::as< Rcpp::IntegerVector >( sfg );
return encode( iv );
}
}
case REALSXP: {
if( Rf_isMatrix( sfg ) ) {
Rcpp::NumericMatrix nm = Rcpp::as< Rcpp::NumericMatrix >( sfg );
Expand All @@ -173,85 +173,94 @@ namespace encode {
}
case VECSXP: {
if( Rf_isNewList( sfg ) ) {
Rcpp::Rcout << "new_list" << std::endl;
Rcpp::List lst = Rcpp::as< Rcpp::List >( sfg );
R_xlen_t n = lst.size();
R_xlen_t i;
Rcpp::List res( n ); // to add in a SPLIT_CHAR
for( i = 0; i < n; ++i ) {
// loop and encode?
Rcpp::Rcout << "i: " << i << std::endl;
SEXP sfgi = lst[ i ];
encode( sfgi );
res[ i ] = encode( sfgi );
}
//Rcpp::Rcout << "SPLIT_CHAR" << std::endl;
//res[ n ] = SPLIT_CHAR;
Rcpp::StringVector enc = sfheaders::utils::unlist_list( res );
//return res;
Rcpp::Rcout << "encoded list " << std::endl;

return enc;
}
}
default: {
Rcpp::stop("googlePolylines - numeric values required");
}
}
}
return Rcpp::StringVector::create(); // #nocov - never reaches
}

inline Rcpp::StringVector encode_multilinestring(
Rcpp::List& sfg
) {
int n = sfg.length();
int i;
Rcpp::StringVector res( n );
for( i = 0; i < n; ++i ) {
Rcpp::NumericMatrix line = sfg[ i ];
res[ i ] = encode( line );
}
return res;
}

inline Rcpp::StringVector encode_polygon(
Rcpp::List& sfg
) {
return encode_multilinestring( sfg );
}

inline Rcpp::StringVector encode_multipolygon(
Rcpp::List& sfg
) {

int n = sfg.length();
int i, j;
Rcpp::List polygons( n );
int line_counter = 0;

if( n == 0 ) {
Rcpp::StringVector sv(0);
return sv;
}

std::vector< std::string > lines;

for( i = 0; i < n; ++i ) {
Rcpp::List polygon = sfg[ i ];
line_counter = line_counter + polygon.size() + 1; // to add the SPLIT_CHAR "-" to separate polygons
}

Rcpp::StringVector res( line_counter );

int counter = 0;
for( i = 0; i < polygons.size(); ++i ) {

Rcpp::List polygon = sfg[ i ];

for( j = 0; j < polygon.size(); ++j ) {
Rcpp::NumericMatrix line = polygon[ j ];
res[ counter ] = encode( line );
counter = counter + 1;
}

res[ counter ] = SPLIT_CHAR;
counter = counter + 1;
}
// remove the final SPLIT_CHAR
res.erase( line_counter - 1 );
return res;
}
// inline Rcpp::StringVector encode_multilinestring(
// Rcpp::List& sfg
// ) {
// int n = sfg.length();
// int i;
// Rcpp::StringVector res( n );
//
// for( i = 0; i < n; ++i ) {
// Rcpp::NumericMatrix line = sfg[ i ];
// res[ i ] = encode( line );
// }
// return res;
// }
//
// inline Rcpp::StringVector encode_polygon(
// Rcpp::List& sfg
// ) {
// return encode_multilinestring( sfg );
// }
//
// inline Rcpp::StringVector encode_multipolygon(
// Rcpp::List& sfg
// ) {
//
// int n = sfg.length();
// int i, j;
// Rcpp::List polygons( n );
// int line_counter = 0;
//
// if( n == 0 ) {
// Rcpp::StringVector sv(0);
// return sv;
// }
//
// std::vector< std::string > lines;
//
// for( i = 0; i < n; ++i ) {
// Rcpp::List polygon = sfg[ i ];
// line_counter = line_counter + polygon.size() + 1; // to add the SPLIT_CHAR "-" to separate polygons
// }
//
// Rcpp::StringVector res( line_counter );
//
// int counter = 0;
// for( i = 0; i < polygons.size(); ++i ) {
//
// Rcpp::List polygon = sfg[ i ];
//
// for( j = 0; j < polygon.size(); ++j ) {
// Rcpp::NumericMatrix line = polygon[ j ];
// res[ counter ] = encode( line );
// counter = counter + 1;
// }
//
// res[ counter ] = SPLIT_CHAR;
// counter = counter + 1;
// }
// // remove the final SPLIT_CHAR
// res.erase( line_counter - 1 );
// return res;
// }

inline Rcpp::List encode_sfc(
Rcpp::List& sfc,
Expand All @@ -270,29 +279,11 @@ namespace encode {
cls = sfheaders::sfg::getSfgClass( sfg );
geometry = cls[1];

Rcpp::StringVector sv;

if( geometry == "POINT" ) {
Rcpp::NumericVector nv = Rcpp::as< Rcpp::NumericVector >( sfg );
sv = encode( nv );
} else if ( geometry == "MULTIPOINT" ) {
Rcpp::NumericMatrix nm = Rcpp::as< Rcpp::NumericMatrix >( sfg );
sv = encode( nm );
} else if ( geometry == "LINESTRING" ) {
Rcpp::NumericMatrix nm = Rcpp::as< Rcpp::NumericMatrix >( sfg );
sv = encode( nm );
} else if ( geometry == "MULTILINESTRING" ) {
Rcpp::List mls = Rcpp::as< Rcpp::List >( sfg );
sv = encode_multilinestring( mls );
} else if ( geometry == "POLYGON" ) {
Rcpp::List pl = Rcpp::as< Rcpp::List >( sfg );
sv = encode_polygon( pl );
} else if ( geometry == "MULTIPOLYGON" ) {
Rcpp::List mpl = Rcpp::as< Rcpp::List >( sfg );
sv = encode_multipolygon( mpl );
} else {
Rcpp::stop("googlepolylines - unknown sfg type");
}
Rcpp::StringVector sv = encode( sfg );

// TODO:
// - stop if unknown sfg type (e.g. geometry collection??);
// - or will this loop handle it?

if( !strip ) {
std::string dim = "XY";
Expand Down
3 changes: 1 addition & 2 deletions man/encode.Rd

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

3 changes: 1 addition & 2 deletions man/geometryRow.Rd

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

0 comments on commit 5ef94e3

Please sign in to comment.