Skip to content

Commit

Permalink
interleaving #52
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed Nov 2, 2020
1 parent d7f2f40 commit 8e0c2ee
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 38 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: spatialwidget
Type: Package
Title: Formats Spatial Data for Use in Htmlwidgets
Version: 0.2.3001
Date: 2020-08-26
Version: 0.2.4
Date: 2020-11-02
Authors@R: c(
person("David", "Cooley", ,"[email protected]", role = c("aut", "cre"))
)
Expand All @@ -28,7 +28,7 @@ LinkingTo:
rapidjsonr,
Rcpp,
sfheaders (>= 0.2.2)
RoxygenNote: 7.1.0
RoxygenNote: 7.1.1
Suggests:
colourvalues,
covr,
Expand Down
3 changes: 1 addition & 2 deletions inst/include/spatialwidget/colour/colour.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ namespace colour {
// Rcpp::Rcout << "LGLSXP || STRSXP " << std::endl;
Rcpp::StringVector colour_vec = Rcpp::as< Rcpp::StringVector >( palette_type );
Rcpp::String first_colour = colour_vec[0];
std::string first_colour_str = first_colour;
if ( spatialwidget::utils::colour::is_hex( first_colour_str ) ) {
if ( spatialwidget::utils::colour::is_hex( first_colour.get_cstring() ) ) {

Rcpp::StringVector lvls = Rcpp::unique( colour_vec );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace spatialwidget {
namespace construction {


inline void construct_df( Rcpp::List& df, int& nrows ) {

if ( nrows < 1 ) {
Expand Down Expand Up @@ -54,8 +55,8 @@ namespace construction {
// if the param element is length 1; check if it's a column name

Rcpp::String this_param = param_names[i];
//Rcpp::Rcout << "this_param: " << this_param.get_cstring() << std::endl;
//Rcpp::Rcout << "TYPEOF(param) " << TYPEOF( params[i] ) << std::endl;
// Rcpp::Rcout << "this_param: " << this_param.get_cstring() << std::endl;
// Rcpp::Rcout << "TYPEOF(param) " << TYPEOF( params[i] ) << std::endl;

if( TYPEOF( params[i] ) == STRSXP ) {
// it's a string
Expand All @@ -73,9 +74,11 @@ namespace construction {

if ( colIndex >= 0 ) {
// The param_value IS a column name
// Rcpp::Rcout << "colIndex " << colIndex << " this_param: " << this_param.get_cstring() << std::endl;
lst_defaults[ this_param ] = data[ colIndex ];

} else {
// Rcpp::Rcout << "it's not a column name" << std::endl;
// IT's not a column name, but it is still a string
// and needs to be applied to all rows
//SEXP value = param_value;
Expand All @@ -89,7 +92,7 @@ namespace construction {
}
} // TODO( is there an 'else' condition? )

Rcpp::StringVector list_names = lst_defaults.names();
//Rcpp::StringVector list_names = lst_defaults.names();
//Rcpp::Rcout << "list_names: " << list_names << std::endl;

//return lst_defaults;
Expand Down
2 changes: 2 additions & 0 deletions inst/include/spatialwidget/palette/palette.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ namespace palette {
std::string colour_format = "hex"
) {

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

// Rcpp::Rcout << "spw colour_name: " << colour_name.c_str() << std::endl;
//Rcpp::Rcout << "alpha: " << alpha << std::endl;

Expand Down
6 changes: 0 additions & 6 deletions inst/include/spatialwidget/parameters/parameters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ namespace parameters {

Rcpp::List lst_params = construct_params( data, params );

//return lst_params;
//return lst_defaults;


Rcpp::List lst_legend = spatialwidget::legend::construct_legend_list(
lst_params,
params,
Expand Down Expand Up @@ -190,8 +186,6 @@ namespace parameters {
Rcpp::_["legend"] = lst_legend
);

// Rcpp::Rcout << "parameters.hpp done" << std::endl;

return result;
}
} // namespace parameters
Expand Down
73 changes: 52 additions & 21 deletions inst/include/spatialwidget/spatialwidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
// [[Rcpp::depends(sfheaders)]]
#include "sfheaders/sfheaders.hpp"

// [[Rcpp::depends(interleave)]]
#include "interleave/interleave.hpp"

#include "spatialwidget/colour/colour.hpp"
#include "spatialwidget/data_construction/data_construction.hpp"
#include "spatialwidget/geojson/geojson.hpp"
Expand Down Expand Up @@ -397,7 +400,6 @@ namespace api {
Rcpp::StringVector& layer_legend,
int& data_rows,
Rcpp::StringVector& parameter_exclusions,
int digits = -1,
std::string colour_format = "rgb" // can't be hex for columnar data
) {

Expand Down Expand Up @@ -451,7 +453,7 @@ namespace api {
) {

R_xlen_t i, j;
Rcpp::List res(5);
Rcpp::List res(2);

Rcpp::DataFrame data = Rcpp::as< Rcpp::DataFrame >( interleaved["data"] );
Rcpp::IntegerVector repeats = Rcpp::as< Rcpp::IntegerVector >( interleaved["geometry_coordinates"] );
Expand All @@ -461,11 +463,7 @@ namespace api {
Rcpp::NumericVector coordinates = interleaved["coordinates"];
int stride = interleaved["stride"];

// Rcpp::Rcout << "repeats: " << repeats << std::endl;
// Rcpp::Rcout << "total_colours " << total_colours << std::endl;

Rcpp::StringVector data_names = data.names();
// Rcpp::Rcout << "data_name: " << data_names << std::endl;

Rcpp::List lst = spatialwidget::parameters::parameters_to_data(
data,
Expand All @@ -487,7 +485,6 @@ namespace api {
// issue 46
spatialwidget::utils::dates::dates_to_string( df );


Rcpp::NumericVector expanded_index( data_rows );
R_xlen_t counter = 0;

Expand All @@ -509,24 +506,63 @@ namespace api {
Rcpp::String to_find = binary_columns[ i ];
R_xlen_t name_position = geometries::utils::where_is( to_find, binary_names );
SEXP v = df[ name_position ];
geometries::utils::expand_vector( df, v, expanded_index, name_position );
if( TYPEOF( v ) != VECSXP ) {
geometries::utils::expand_vector( df, v, expanded_index, name_position );
} else {
// Lists just need to be un-list
// I'm working on the assumption the list will have the same number of elements
// as coordinates
Rcpp::List lst = Rcpp::as< Rcpp::List >( v );
df[ name_position ] = geometries::utils::unlist_list( lst );

}
}

// Also expand the colours if they are HEX values
// because they haven't gone through colour_values,
// so there is only one colour per geometry
// but we need the colour per coordinate.
// and then convert to RGBA, and then interleave
std::unordered_map< std::string, std::string>::iterator it;
std::string colour_column;
std::string opacity_column;

for ( it = layer_colours.begin(); it != layer_colours.end(); ++it ) {
colour_column = it->first;

SEXP col = df[ colour_column ];
if( spatialwidget::utils::colour::is_hex( col ) ) {
// 1. Fill
R_xlen_t name_position = geometries::utils::where_is( colour_column, binary_names );
geometries::utils::expand_vector( df, col, expanded_index, name_position );
// 2. Convert
// It's a hex, which means it's a STRSXP
SEXP expanded_col = df[ colour_column ];
Rcpp::StringVector sv = Rcpp::as< Rcpp::StringVector >( expanded_col );
Rcpp::IntegerMatrix colour_mat = colourvalues::convert::convert_hex_to_rgb( sv );
Rcpp::NumericMatrix nm = Rcpp::as< Rcpp::NumericMatrix >( colour_mat );
nm = nm / 255;
// 3. Interleave
df[ name_position ] = interleave::interleave( nm );

//lst["data"] = df;
}

//return lst;
}

Rcpp::StringVector js_data = jsonify::api::to_json(
df, false, -1, true, true, "col"

Rcpp::List res_list = Rcpp::List::create(
Rcpp::_["coordinates"] = coordinates,
Rcpp::_["start_indices"] = start_indices,
Rcpp::_["data"] = df,
Rcpp::_["stride"] = stride
);

res[0] = js_data;
res[0] = jsonify::api::to_json(
res_list, false, digits, true, true, "col"
);

SEXP legend = lst[ "legend" ];

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

if ( jsonify_legend ) {
legend = jsonify::api::to_json( legend );

Expand All @@ -536,12 +572,7 @@ namespace api {
res[1] = legend;
}

// Rcpp::Rcout << "4" << std::endl;
res[2] = jsonify::api::to_json( coordinates );
res[3] = jsonify::api::to_json( start_indices );
res[4] = stride;

res.names() = Rcpp::CharacterVector::create("data", "legend", "coordinates", "start_indices", "stride");
res.names() = Rcpp::CharacterVector::create("data", "legend");
return res;
}

Expand Down
10 changes: 10 additions & 0 deletions inst/include/spatialwidget/utils/colour/colour.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ namespace colour {
return res;
}

inline bool is_hex( SEXP x ) {
if( TYPEOF( x ) != STRSXP ) {
return false;
}

Rcpp::StringVector sv = Rcpp::as< Rcpp::StringVector >( x );
Rcpp::String first_colour = sv[0];
return is_hex( first_colour.get_cstring() );
}

} // namespace colour
} // namespace utils
} // namespace spatialwidget
Expand Down
9 changes: 6 additions & 3 deletions tests/testthat/test-colours.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ test_that("colours returned as hex or rgb",{
df <- data.frame(col1 = 1:n)
lst_params <- spatialwidget:::rcpp_construct_params(df, params)
data <- df
data_rows <- nrow( df )
total_colours <- nrow( df )
repeats <- 1L
lst_defaults <- list(fill_colour = rep(1,5))

colour_name <- "fill_colour"
Expand Down Expand Up @@ -41,7 +42,8 @@ test_that("colours returned as hex or rgb",{
df <- data.frame(col1 = 1:n)
lst_params <- spatialwidget:::rcpp_construct_params(df, params)
data <- df
data_rows <- nrow( df )
total_colours <- nrow( df )
repeats <- 1L
lst_defaults <- list(fill_colour = rep(1,5))

colour_name <- "fill_colour"
Expand Down Expand Up @@ -75,7 +77,8 @@ test_that("colours returned as hex or rgb",{
df <- data.frame(col1 = 1:n)
lst_params <- spatialwidget:::rcpp_construct_params(df, params)
data <- df
data_rows <- nrow( df )
total_colours <- nrow( df )
repeats <- 1L
lst_defaults <- list(fill_colour = rep(1,5))

colour_name <- "fill_colour"
Expand Down

0 comments on commit 8e0c2ee

Please sign in to comment.