forked from r-spatial/mapview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrewCppFun.cpp
More file actions
226 lines (166 loc) · 6.27 KB
/
Copy pathbrewCppFun.cpp
File metadata and controls
226 lines (166 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <Rcpp.h>
using namespace Rcpp;
#include <string>
#include <fstream>
#include <streambuf>
////////////////////////////////////////////////////////////////////////////////
// Replace string with another string (function taken from /////////////////////
// https://stackoverflow.com/questions/2896600/how-to-replace-all-occurrences-of-a-character-in-string) ////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// [[Rcpp::export]]
std::string gsubC(const std::string& pattern, const std::string& replacement,
std::string x) {
size_t start_pos = 0;
while((start_pos = x.find(pattern, start_pos)) != std::string::npos) {
x.replace(start_pos, pattern.length(), replacement);
start_pos += replacement.length();
}
return x;
}
////////////////////////////////////////////////////////////////////////////////
// standard pattern used for odd column indices ////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// [[Rcpp::export]]
std::string brewPopupRowC(std::string colname, std::string value) {
std::string chColString;
chColString = std::string("<td>") + colname + "</td>";
std::ostringstream ssVal;
ssVal << value;
std::string chValString;
chValString = std::string("<td>") + ssVal.str() + "</td>";
std::string chOutString;
chOutString = std::string("<tr>" + chColString + chValString + "</tr>");
return chOutString;
}
////////////////////////////////////////////////////////////////////////////////
// alternative pattern used for even column indices ////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// [[Rcpp::export]]
std::string brewPopupRowAltC(std::string colname, std::string value) {
std::string chColString;
chColString = std::string("<td>") + colname + "</td>";
std::ostringstream ssVal;
ssVal << value;
std::string chValString;
chValString = std::string("<td>") + ssVal.str() + "</td>";
std::string chOutString;
chOutString = std::string("<tr class=\'alt\'>" + chColString + chValString + "</tr>");
return chOutString;
}
////////////////////////////////////////////////////////////////////////////////
// standard pattern used for coordinates ///////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// [[Rcpp::export]]
std::string brewPopupCoords(std::string colname, std::string value) {
std::string chColString;
chColString = std::string("<td>") + colname + "</td>";
std::ostringstream ssVal;
ssVal << value;
std::string chValString;
chValString = std::string("<td>") + ssVal.str() + "</td>";
std::string chOutString;
chOutString = std::string("<tr class=\'coord\'>" + chColString + chValString + "</tr>");
return chOutString;
}
////////////////////////////////////////////////////////////////////////////////
// merge alternating string patterns per row ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// [[Rcpp::export]]
std::string mergePopupRows(CharacterVector names, CharacterVector values) {
int nSize = values.size();
std::string chOut = std::string("");
std::ostringstream ssName;
std::ostringstream ssValue;
for (int i = 0; i < nSize; i++) {
ssName << names[i];
ssValue << values[i];
// coords
if (names[i] == "Longitude" | names[i] == "Latitude") {
chOut = chOut + brewPopupCoords(ssName.str(), ssValue.str());
} else {
// even variable columns
if (i%2 == 0) {
chOut = chOut + brewPopupRowC(ssName.str(), ssValue.str());
// odd variable columns
} else {
chOut = chOut + brewPopupRowAltC(ssName.str(), ssValue.str());
}
}
ssName.str(std::string());
ssValue.str(std::string());
}
return chOut;
}
////////////////////////////////////////////////////////////////////////////////
// merge alternating string patterns per row ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// [[Rcpp::export]]
std::string createTemplate(std::string tmpPath) {
std::ifstream file(tmpPath.c_str());
std::string line, data;
std::ostringstream ssLines;
// import lines iteratively
while(std::getline(file, line))
{
std::stringstream linestream(line);
std::getline(linestream, data, '\n');
ssLines << data;
}
return ssLines.str();
}
////////////////////////////////////////////////////////////////////////////////
// Create list with string patterns per row ////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// [[Rcpp::export]]
List listPopupTemplates(CharacterMatrix x, CharacterVector names,
std::string tmpPath) {
// number of rows and columns
int nRows = x.nrow();
int nCols = x.ncol();
// intermediary variables
CharacterVector chVal(nCols);
std::string chStr;
// output list
List lsOut(nRows);
// import template
std::string chTemplate = createTemplate(tmpPath);
std::string chTmp = chTemplate;
// create strings for each single row
for (int i = 0; i < nRows; i++) {
chVal = x(i, _);
chStr = mergePopupRows(names, chVal);
chTmp = gsubC("<%=pop%>", chStr, chTmp);
lsOut[i] = chTmp;
// reset intermediary string
chTmp = chTemplate;
}
return lsOut;
}
// /*** R
// # odd version
// identical(
// mapview:::brewPopupRow("carat", .5),
// mapview:::brewPopupRowC("carat", .5)
// )
//
// # even version
// identical(
// mapview:::brewPopupRowAlt("price", 23.75),
// mapview:::brewPopupRowAltC("price", 23.75)
// )
// */
////////////////////////////////////////////////////////////////////////////////
// convert all columns in a data.frame to 'character'///////////////////////////
////////////////////////////////////////////////////////////////////////////////
// [[Rcpp::export]]
CharacterMatrix df2String(DataFrame x) {
int nCols = x.size();
CharacterVector chContents = as<CharacterVector>(x[1]);
int nRows = chContents.size();
CharacterMatrix chOut(nRows, nCols);
for (int i = 0; i < nCols; i++) {
chContents = as<CharacterVector>(x[i]);
chOut(_, i) = chContents;
}
return chOut;
}