Skip to content

Commit

Permalink
changing sfc attr to sfg for SymbolixAU#42
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed Feb 8, 2020
1 parent 2662a79 commit db7accb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
55 changes: 40 additions & 15 deletions inst/include/googlepolylines/encode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "googlepolylines/googlepolylines.h"

#include "sfheaders/df/sfg.hpp"
#include "sfheaders/sfg/sfg_attributes.hpp"

namespace googlepolylines {
namespace encode {
Expand Down Expand Up @@ -75,7 +76,7 @@ namespace encode {
) {
std::ostringstream os;
encode( lons, lats, os );
Rcpp::Rcout << "os" << os.str() << std::endl;
// Rcpp::Rcout << "os" << os.str() << std::endl;
return os.str();
}

Expand All @@ -85,16 +86,14 @@ namespace encode {
if( mat.ncol() < 2 ) {
Rcpp::stop("googlepolylines - expecting at least 2 columns in a matrix");
}

Rcpp::Rcout << "n_row: " << mat.nrow() << std::endl;


Rcpp::NumericVector lons = mat( Rcpp::_, 0 );
Rcpp::NumericVector lats = mat( Rcpp::_, 1 );
return encode( lons, lats );
}

// encode sfg objects
inline std::string encode_point(
inline Rcpp::StringVector encode_point(
Rcpp::NumericVector& sfg
) {

Expand All @@ -108,7 +107,9 @@ namespace encode {
Rcpp::NumericVector lats(1);
lons[0] = sfg[0];
lats[0] = sfg[1];
return encode( lons, lats );
Rcpp::StringVector res(1);
res[0] = encode( lons, lats );
return res;
}

inline Rcpp::StringVector encode_multipoint(
Expand All @@ -119,9 +120,14 @@ namespace encode {
Rcpp::stop("googlepolylines - not enough columns in the matrix");
}
R_xlen_t n = sfg.nrow();
Rcpp::Rcout << "n: " << n << std::endl;
R_xlen_t i;
Rcpp::StringVector res( n );

if( n == 0 ) {
// empty geometry - return empty string
return res;
}

for( i = 0; i < n; ++i ) {
double lon = sfg( i, 0 );
double lat = sfg( i, 1 );
Expand All @@ -140,7 +146,8 @@ namespace encode {

R_xlen_t n = sfg.nrow();
if( n == 0 ) {
return Rcpp::StringVector::create();
Rcpp::StringVector res(0);
return res;
}
return encode( sfg );
}
Expand Down Expand Up @@ -174,6 +181,11 @@ namespace encode {
Rcpp::List polygons( n );
int line_counter = 0;

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

for( i = 0; i < n; ++i ) {
Rcpp::List polygon = sfg[ i ];
polygons[ i ] = encode_polygon( polygon );
Expand Down Expand Up @@ -202,7 +214,8 @@ namespace encode {
}

inline Rcpp::List encode_sfc(
Rcpp::List& sfc
Rcpp::List& sfc,
bool& strip
) {
// Rcpp::Rcout << "encode_sfc " << std::endl;
R_xlen_t n = sfc.size();
Expand All @@ -221,27 +234,39 @@ namespace encode {

// Rcpp::Rcout << "geometry: " << geometry << std::endl;

Rcpp::StringVector sv;

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

if( !strip ) {
//Rcpp::Rcout << "setting sfg attr" << std::endl;
std::string dim = "XY";
sv.attr("sfg") = sfheaders::sfg::sfg_attributes(dim, geometry);
//sv = sfheaders::sfg::attach_sfg_attribute(sv, dim, geometry);
}

res[i] = sv;

}
return res;
}
Expand Down
5 changes: 4 additions & 1 deletion src/encode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ Rcpp::List rcpp_encode_sfc(
Rcpp::List& sfc,
bool strip
) {

//Rcpp::CharacterVector cls_attr = sfc.attr("class");

// R_xlen_t n = sfc.size();
// Rcpp::List res( n );
// R_xlen_t i;
Expand All @@ -314,7 +317,7 @@ Rcpp::List rcpp_encode_sfc(
// res[ i ] = googlepolylines::encode::encode_sfc( sfg );
// }
//return res;
return googlepolylines::encode::encode_sfc( sfc );
return googlepolylines::encode::encode_sfc( sfc, strip );
}

// // [[Rcpp::export]]
Expand Down

0 comments on commit db7accb

Please sign in to comment.