SymbolixAU / mapdeck
1
#include <Rcpp.h>
2
#include "mapdeck_defaults.hpp"
3
#include "layers/layer_colours.hpp"
4
#include "spatialwidget/spatialwidget.hpp"
5
#include "sfheaders/df/sf.hpp"
6

7

8 0
Rcpp::List column_defaults(int n) {
9 0
	Rcpp::NumericVector nv = Rcpp::NumericVector(n);  // initalised to 0
10

11 0
	return Rcpp::List::create(
12 0
		_["elevation"] = mapdeck::defaults::default_elevation(n),
13 0
		_["fill_colour"] = mapdeck::defaults::default_fill_colour(n),
14 0
		_["stroke_colour"] = mapdeck::defaults::default_stroke_colour(n),
15 0
		_["stroke_width"] = nv
16
	);
17
}
18

19 1
Rcpp::List scatterplot_defaults(int n) {
20

21 1
	Rcpp::NumericVector nv = Rcpp::NumericVector(n);  // initalised to 0
22

23 1
	return Rcpp::List::create(
24 1
		_["fill_colour"] = mapdeck::defaults::default_fill_colour(n),
25 1
		_["stroke_colour"] = mapdeck::defaults::default_stroke_colour(n),
26 1
		_["stroke_width"] = nv,
27 1
		_["radius"] = mapdeck::defaults::default_radius(n)
28
	);
29
}
30

31 1
Rcpp::List pointcloud_defaults(int n) {
32 1
	return Rcpp::List::create(
33 1
		_["elevation"] = mapdeck::defaults::default_elevation(n),
34 1
		_["fill_colour"] = mapdeck::defaults::default_fill_colour(n)
35
	);
36
}
37

38 0
Rcpp::List grid_defaults(int n) {
39 0
	return Rcpp::List::create();
40
}
41

42

43 1
Rcpp::List get_point_defaults( std::string layer_name, int data_rows ) {
44 1
	if( layer_name == "column" ) {
45 0
		return column_defaults( data_rows );
46 1
	} else if ( layer_name == "scatterplot" ) {
47 1
		return scatterplot_defaults( data_rows );
48 1
	} else if (layer_name == "grid" ) {
49 0
		return grid_defaults( data_rows );
50
	}
51 1
	return pointcloud_defaults( data_rows );
52
}
53

54

55 1
Rcpp::StringVector get_point_legend_colours( std::string layer_name ) {
56

57 1
	Rcpp::StringVector point_legend;
58

59 1
	if( layer_name == "column" || layer_name == "scatterplot" ) {
60 1
		point_legend = mapdeck::layer_colours::fill_stroke_legend;
61 1
	} else if ( layer_name == "pointcloud" ) {
62 1
		point_legend = mapdeck::layer_colours::fill_legend;
63 1
	} else if ( layer_name == "grid" ) {
64 0
		point_legend = mapdeck::layer_colours::no_legend;
65
	}
66 1
	return point_legend;
67
}
68

69 1
std::unordered_map< std::string, std::string > get_point_colours( std::string layer_name ) {
70

71 1
	std::unordered_map< std::string, std::string > point_colours;
72

73 1
	if( layer_name == "column" || layer_name == "scatterplot" ) {
74 1
		point_colours = mapdeck::layer_colours::fill_stroke_colours;
75 1
	} else if ( layer_name == "pointcloud" ) {
76 1
		point_colours = mapdeck::layer_colours::fill_colours;
77 1
	} else if ( layer_name == "grid" ) {
78 0
		point_colours = mapdeck::layer_colours::no_colours;
79
	}
80 1
	return point_colours;
81
}
82

83
// [[Rcpp::export]]
84 1
Rcpp::List rcpp_point_df_columnar(
85
		Rcpp::DataFrame data,
86
		Rcpp::List params,
87
		Rcpp::List geometry_columns,
88
		int digits,
89
		std::string layer_name
90
) {
91

92 1
	int data_rows = data.nrows();
93

94

95 1
	Rcpp::List lst_defaults = get_point_defaults( layer_name, data_rows );
96

97 1
	Rcpp::StringVector point_legend = get_point_legend_colours( layer_name );
98 1
	std::unordered_map< std::string, std::string > point_colours = get_point_colours( layer_name );
99 1
	Rcpp::StringVector parameter_exclusions = Rcpp::StringVector::create("legend","legend_options","palette","na_colour");
100

101 1
	std::string format = "rgb";
102

103 1
	return spatialwidget::api::create_columnar(
104
		data,
105
		params,
106
		lst_defaults,
107
		point_colours,
108
		point_legend,
109
		data_rows,
110
		parameter_exclusions,
111
		geometry_columns,
112
		true,  // jsonify legend
113 1
		digits,
114 1
		format
115
	);
116
}
117

118
// [[Rcpp::export]]
119 1
Rcpp::List rcpp_point_sf_columnar(
120
		Rcpp::DataFrame data,
121
		Rcpp::List params,
122
		Rcpp::List geometry_columns,  // passed in from R; the geometry columns used by the plotting layer (e.g., scatter = x,y, pointcloud = x,y,z)
123
		int digits,
124
		std::string layer_name
125
	){
126

127 1
	Rcpp::DataFrame df = sfheaders::df::sf_to_df( data, true );
128 1
	Rcpp::StringVector geometry_cols = df.attr("sfc_columns");  // will be x, y, (z), (m)
129

130
	// can't directly use `geometry_cols[0]; because it's a CHARSXP,
131
	// but in spatialwidget it looks for STRSXP
132
	// TODO:
133
	// this needs to loop over the geometry_cols, because there may be elevation and time as well, right?
134

135 1
	int n_cols = geometry_cols.length();
136
	int i;
137 1
	Rcpp::StringVector param_names({"lon","lat","elevation","time"});  // need to match those incoming from R
138

139 1
	for( i = 0; i < n_cols; ++i ) {
140 1
		Rcpp::String this_geom = geometry_cols[i];
141 1
		Rcpp::String this_param = param_names[i];
142 1
		params[ this_param ] = this_geom;
143
	}
144

145 1
	return rcpp_point_df_columnar(df, params, geometry_columns, digits, layer_name );
146
}
147

148

149
// // [[Rcpp::export]]
150
// Rcpp::List rcpp_point_geojson(
151
// 		Rcpp::DataFrame data,
152
// 		Rcpp::List params,
153
// 		std::string geometry_columns,
154
// 		int digits,
155
// 		std::string layer_name
156
// ) {
157
//
158
// 	int data_rows = data.nrows();
159
//
160
// 	Rcpp::List lst_defaults = get_point_defaults( layer_name, data_rows );
161
//
162
// 	Rcpp::StringVector point_legend = get_point_legend_colours( layer_name );
163
// 	std::unordered_map< std::string, std::string > point_colours = get_point_colours( layer_name );
164
//
165
// 	Rcpp::StringVector parameter_exclusions = Rcpp::StringVector::create("legend","legend_options","palette","na_colour");
166
//
167
// 	return spatialwidget::api::create_geojson(
168
// 		data,
169
// 		params,
170
// 		lst_defaults,
171
// 		point_colours,
172
// 		point_legend,
173
// 		data_rows,
174
// 		parameter_exclusions,
175
// 		geometry_columns,
176
// 		true,  // jsonify legend
177
// 		digits
178
// 	);
179
// }
180

181
// [[Rcpp::export]]
182 0
Rcpp::List rcpp_point_geojson_df(
183
		Rcpp::DataFrame data,
184
		Rcpp::List params,
185
		Rcpp::List geometry_columns,
186
		int digits,
187
		std::string layer_name
188
) {
189

190 0
	int data_rows = data.nrows();
191

192 0
	Rcpp::List lst_defaults = get_point_defaults( layer_name, data_rows );
193

194 0
	Rcpp::StringVector point_legend = get_point_legend_colours( layer_name );
195 0
	std::unordered_map< std::string, std::string > point_colours = get_point_colours( layer_name );
196

197 0
	Rcpp::StringVector parameter_exclusions = Rcpp::StringVector::create("legend","legend_options","palette","na_colour");
198

199 0
	bool elevation = layer_name == "pointcloud" ? true : false;
200

201

202 0
	if( elevation ) {
203 0
		return spatialwidget::api::create_geojson(
204
			data,
205
			params,
206
			lst_defaults,
207
			point_colours,
208
			point_legend,
209
			data_rows,
210
			parameter_exclusions,
211
			geometry_columns,
212
			true,  // jsonify legend
213
			true,  // elevation
214 0
			digits
215
		);
216
	}
217

218 0
	return spatialwidget::api::create_geojson(
219
		data,
220
		params,
221
		lst_defaults,
222
		point_colours,
223
		point_legend,
224
		data_rows,
225
		parameter_exclusions,
226
		geometry_columns,
227
		true,  // jsonify legend
228 0
		digits
229
	);
230
}
231

232

233
// [[Rcpp::export]]
234 0
Rcpp::List rcpp_point_polyline(
235
		Rcpp::DataFrame data,
236
		Rcpp::List params,
237
		Rcpp::StringVector geometry_columns,
238
		std::string layer_name
239
) {
240

241 0
	int data_rows = data.nrows();
242

243 0
	Rcpp::List lst_defaults = get_point_defaults( layer_name, data_rows );
244

245 0
	Rcpp::StringVector point_legend = get_point_legend_colours( layer_name );
246 0
	std::unordered_map< std::string, std::string > point_colours = get_point_colours( layer_name );
247

248 0
	Rcpp::StringVector parameter_exclusions = Rcpp::StringVector::create("legend","legend_options","palette","na_colour");
249

250 0
	return spatialwidget::api::create_polyline(
251
		data,
252
		params,
253
		lst_defaults,
254
		point_colours,
255
		point_legend,
256
		data_rows,
257
		parameter_exclusions,
258 0
		geometry_columns,
259
		true  // jsonify legend
260
	);
261
}

Read our documentation on viewing source code .

Loading