Skip to content

Commit

Permalink
tidying interleaving #78
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed Apr 23, 2020
1 parent 299869f commit b2957fc
Showing 1 changed file with 42 additions and 27 deletions.
69 changes: 42 additions & 27 deletions inst/include/sfheaders/interleave/interleave.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,45 +151,60 @@ namespace interleave {
R_xlen_t n_col = sf.ncol();
R_xlen_t n_geometries = sf.nrow();
R_xlen_t i;
R_xlen_t j;
//R_xlen_t j;

R_xlen_t total_coordinates = 0;
Rcpp::List interleaved = interleave( sfc, total_coordinates );
Rcpp::IntegerVector n_coordinates = interleaved["n_coordinates"];

Rcpp::NumericVector expanded_index( total_coordinates );
R_xlen_t counter = 0;
// Rcpp::NumericVector expanded_index( total_coordinates );
// R_xlen_t counter = 0;
//
// for( i = 0; i < n_geometries; ++i ) {
// R_xlen_t expand_by = n_coordinates[ i ];
//
// for( j = 0; j < expand_by; ++j ) {
// expanded_index[ counter + j ] = i;
// }
// counter = counter + expand_by;
// }
//
// Rcpp::List res( n_col - 1 );
// Rcpp::StringVector res_names( n_col - 1 );
//
// R_xlen_t name_position = 0;
//
// for( i = 0; i < n_col; ++i ) {
//
// if( sf_names[ i ] != geom_column ) {
//
// res_names[ name_position ] = sf_names[ i ];
// SEXP v = sf[ i ];
// sfheaders::df::expand_vector( res, v, expanded_index, name_position );
// name_position += 1;
// }
// }

for( i = 0; i < n_geometries; ++i ) {
R_xlen_t expand_by = n_coordinates[ i ];

for( j = 0; j < expand_by; ++j ) {
expanded_index[ counter + j ] = i;
}
counter = counter + expand_by;
}

Rcpp::List res( n_col - 1 );
Rcpp::StringVector res_names( n_col - 1 );

R_xlen_t name_position = 0;
//res.names() = res_names;
//Rcpp::DataFrame df = sfheaders::utils::make_dataframe( res, total_coordinates, res_names );

for( i = 0; i < n_col; ++i ) {

if( sf_names[ i ] != geom_column ) {
// put all the 'other' columns onto interleaved
// can use 'n_coordinates' to 'exapnd' any of the data columns (e.g. colours when using spatialwidget)
Rcpp::List df( n_col - 1 );
Rcpp::StringVector df_names( n_col - 1 );
R_xlen_t column_idx = 0;

res_names[ name_position ] = sf_names[ i ];
SEXP v = sf[ i ];
sfheaders::df::expand_vector( res, v, expanded_index, name_position );
name_position += 1;
for( i = 0; i < n_col; ++ i ) {
const char* this_name = sf_names[ i ];
if( this_name != geom_column ) {
df[ column_idx ] = sf[ this_name ];
df_names[ column_idx ] = this_name;
column_idx = column_idx + 1;
}
}

//res.names() = res_names;
Rcpp::DataFrame df = sfheaders::utils::make_dataframe( res, total_coordinates, res_names );

// construct final object
interleaved["data"] = df;
interleaved["data"] = sfheaders::utils::make_dataframe( df, n_geometries, df_names );
return interleaved;

}
Expand Down

0 comments on commit b2957fc

Please sign in to comment.