-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
164 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#ifndef R_SFHEADERS_INTERLEAVE_H | ||
#define R_SFHEADERS_INTERLEAVE_H | ||
|
||
#include "sfheaders/df/sfc.hpp" | ||
#include <Rcpp.h> | ||
|
||
namespace sfheaders { | ||
namespace interleave { | ||
|
||
template < int RTYPE > | ||
inline SEXP interleave( Rcpp::Matrix< RTYPE >& mat ) { | ||
R_xlen_t n_col = mat.ncol(); | ||
R_xlen_t n_row = mat.nrow(); | ||
Rcpp::Vector< RTYPE > res( n_col * n_row ); | ||
R_xlen_t i, j; | ||
for( i = 0; i < n_row; ++i ) { | ||
for( j = 0; j < n_col; ++j ) { | ||
res[ j ] = mat( i, j ); | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
inline SEXP interleave( Rcpp::List& lst) { | ||
R_xlen_t i; | ||
R_xlen_t n = lst.length(); | ||
|
||
|
||
Rcpp::NumericMatrix sfc_coordinates = sfheaders::df::sfc_n_coordinates( lst ); | ||
R_xlen_t n_geometries = sfc_coordinates.nrow(); | ||
R_xlen_t total_coordinates = sfc_coordinates( n_geometries - 1 , 1 ); | ||
total_coordinates = total_coordinates + 1; | ||
|
||
//Rcpp::Rcout << "sfc_coordinates: " << sfc_coordinates << std::endl; | ||
|
||
for( i = 0; i < n; ++i ) { | ||
//SEXP sfg = sfc[ i ]; | ||
|
||
} | ||
return Rcpp::List::create(); | ||
} | ||
|
||
inline SEXP interleave( SEXP& sfg ) { | ||
switch( TYPEOF ( sfg ) ) { | ||
case INTSXP: { | ||
if( Rf_isMatrix( sfg ) ) { | ||
Rcpp::IntegerMatrix im = Rcpp::as< Rcpp::IntegerMatrix >( sfg ); | ||
return interleave( im ); | ||
} else { | ||
return sfg; // it's already a vector | ||
} | ||
} | ||
case REALSXP: { | ||
if( Rf_isMatrix( sfg ) ) { | ||
Rcpp::NumericMatrix im = Rcpp::as< Rcpp::NumericMatrix >( sfg ); | ||
return interleave( im ); | ||
} else { | ||
return sfg; // it's already a vector | ||
} | ||
} | ||
case VECSXP: { | ||
if( Rf_isNewList( sfg ) ) { | ||
Rcpp::List lst = Rcpp::as< Rcpp::List >( sfg ); | ||
return interleave( lst ); | ||
} | ||
} | ||
default: { | ||
Rcpp::stop("sfheaders - can not interleave this type of object"); | ||
} | ||
} | ||
return Rcpp::List::create(); | ||
} | ||
|
||
} // interleave | ||
} // sfheaders | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include <Rcpp.h> | ||
#include "sfheaders/df/sfc.hpp" | ||
#include "sfheaders/interleave/interleave.hpp" | ||
|
||
|
||
void interleave_geometry(SEXP& sfg, Rcpp::NumericVector& res, R_xlen_t& coordinate_counter ) { | ||
|
||
} | ||
|
||
// [[Rcpp::export]] | ||
SEXP rcpp_interleave( Rcpp::List sfc ) { | ||
|
||
// the input will be a long data.frame | ||
// or an sf object | ||
// if it's a data.frame, it needs id columns and geometry columns | ||
// can probably do this one later? | ||
// and focus on the 'sf', because it doesn't need any extra headers / thought / logic | ||
|
||
//Rcpp::NumericMatrix sfc_coordinates = sfheaders::df::sfc_n_coordinates( sfc ); | ||
|
||
//return sfc_coordinates; | ||
|
||
// the STRIDE depends on the dimension being the same for every pair of coordinates | ||
|
||
|
||
Rcpp::CharacterVector cls; | ||
std::string dim_expected; | ||
std::string dim; | ||
|
||
R_xlen_t n = sfc.length(); | ||
R_xlen_t i; | ||
Rcpp::IntegerVector n_coordinates( n ); | ||
R_xlen_t sfg_count = 0; // move outside loop so it's cumulative | ||
for( i = 0; i < n; ++i ) { | ||
SEXP sfg = sfc[ i ]; | ||
|
||
cls = sfheaders::utils::getSfgClass( sfg ); | ||
if( i == 0 ) { | ||
dim_expected = cls[0]; | ||
} else { | ||
dim = cls[0]; | ||
if ( dim != dim_expected ) { | ||
Rcpp::stop("sfheaders - interleaving only works when all geometries have the same dimension (XY(Z(M)))"); | ||
} | ||
} | ||
|
||
n_coordinates[ i ] = sfg_count; | ||
sfheaders::df::sfg_n_coordinates( sfg, sfg_count ); | ||
} | ||
// the last iteration gives the total number of coordinates | ||
// which will be the length of the vector (* stride (dim) ); | ||
Rcpp::Rcout << "total coordinates: " << sfg_count << std::endl; | ||
|
||
int stride = ( dim_expected == "XYZ" || dim_expected == "XYM" ? 3 : ( dim_expected == "XYZM" ? 4 : 2 ) ); | ||
R_xlen_t total_coordinates = sfg_count * stride; | ||
Rcpp::NumericVector res( total_coordinates ); | ||
R_xlen_t coordinate_counter = 0; | ||
// now fill this vector with all the coorinates from the coordinates | ||
for( i = 0; i < n; ++i ) { | ||
SEXP sfg = sfc[ i ]; | ||
// depending on the GEOMETRY | ||
// get each coordinate | ||
// do I iterate each row? | ||
SEXP temp = sfheaders::interleave::interleave( sfg ); | ||
} | ||
|
||
return res; | ||
} | ||
|
||
|