Repository navigation
Expand file tree
/
Copy pathStringRepresentation.cpp
More file actions
387 lines (350 loc) · 14.1 KB
/
Copy pathStringRepresentation.cpp
File metadata and controls
387 lines (350 loc) · 14.1 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
* @file StringRepresentation.cpp
* @brief TODO: add a brief description.
* @copyright Copyright (C) 2025 ForeFire, Fire Team, SPE, CNRS/Universita di Corsica.
* @license This program is free software; See LICENSE file for details. (See LICENSE file).
* @author Jean‑Baptiste Filippi — 2025
*/
#include "StringRepresentation.h"
#include <iomanip>
#include <limits>
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include <algorithm>
#define GEOJSON_MODE 2
#define JSON_MODE 1
#define FF_MODE 0
#define KML_MODE 3
using namespace std;
namespace libforefire {
// The output buffer, the nesting level and the GeoJSON aggregation state used
// to be statics and file globals here, so two simulations printing at the same
// time wrote into one buffer. They are members now.
//
// For GEOJSON mode we aggregate rings per top-level front. Each top-level
// feature (currentLevel==1) is a vector of rings, where each ring is a vector
// of coordinate strings.
//
// writedOnce is deliberately left shared for now. Unlike the rest, making it
// per-instance is an observable change: output() skips a representation whose
// domain has no front once anything has been written, so a second
// representation in the same session would gain a first output file it does
// not produce today. That is a behaviour question for the coupled runs, not a
// mechanical one, and it belongs with the state work in #175.
bool writedOnce = false;
StringRepresentation::StringRepresentation(FireDomain* fdom) : Visitor() {
lastLevel = -1;
domain = fdom;
outPattern = SimulationParameters::GetInstance()->getParameter("ffOutputsPattern");
setTime(domain->getTime());
updateStep = SimulationParameters::GetInstance()->getDouble("outputsUpdate");
setUpdateTime(domain->getTime());
dumpMode = FF_MODE; // default; will be set by dumpStringRepresentation()
}
StringRepresentation::~StringRepresentation() { }
void StringRepresentation::input() { }
void StringRepresentation::setOutPattern(string s) {
outPattern = s;
}
void StringRepresentation::update() {
setTime(getUpdateTime());
}
void StringRepresentation::timeAdvance() {
if ( updateStep < EPSILONT ) {
setUpdateTime(numeric_limits<double>::infinity());
} else {
setUpdateTime(getTime() + updateStep);
}
}
void StringRepresentation::output() {
if (writedOnce && (domain->getNumFF() == 0))
return;
writedOnce = true;
ostringstream oss;
oss << outPattern << "." << getTime();
ofstream outputfile(oss.str().c_str());
if (outputfile) {
outputfile << dumpStringRepresentation();
} else {
cout << "could not open file " << oss.str() << " for writing domain file " << endl;
}
}
//
// Visitor methods
//
// Visit the domain: outputs the header and resets GEOJSON flags.
void StringRepresentation::visit(FireDomain* fd) {
SimulationParameters *simParam = SimulationParameters::GetInstance();
if (dumpMode == JSON_MODE) {
outputstr << '{' << endl << "\t\"fronts\": [";
lastLevel = 0;
}
else if (dumpMode == GEOJSON_MODE) {
firstGeoFeature = true; // reset before outputting features
double t = simParam->getInt("refTime") + fd->getSimulationTime();
int d = simParam->getInt("refDay");
int y = simParam->getInt("refYear");
outputstr << "{" << endl;
outputstr << "\t\"type\": \"FeatureCollection\"," << endl;
outputstr << "\t\"valid_at\": \""<< SimulationParameters::FormatISODate(t, y, d) << "\"," << endl;
outputstr << "\t\"features\": [" << endl;
}
else if (dumpMode == KML_MODE) {
outputstr << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl;
outputstr << "<kml xmlns=\"http://www.opengis.net/kml/2.2\" "
<< "xmlns:gx=\"http://www.google.com/kml/ext/2.2\" "
<< "xmlns:kml=\"http://www.opengis.net/kml/2.2\" "
<< "xmlns:atom=\"http://www.w3.org/2005/Atom\">" << endl;
outputstr << "<Document>" << endl;
}
else if (dumpMode == FF_MODE) {
outputstr << fd->toString() << endl;
}
}
// Visit a fire front.
void StringRepresentation::visit(FireFront* ff) {
if (dumpMode == FF_MODE) {
for (size_t k = 0; k < currentLevel; k++)
outputstr << " ";
outputstr << ff->toString() << endl;
return;
}
else if (dumpMode == JSON_MODE) {
// (JSON mode unchanged)
SimulationParameters *simParam = SimulationParameters::GetInstance();
if (ff->getDomain()->getSimulationTime() >= ff->getTime()) {
if (lastLevel >= 2)
outputstr << '"';
if (lastLevel >= 1)
outputstr << endl << "\t" << "},";
double t = simParam->getInt("refTime") + ff->getDomain()->getSimulationTime();
int d = simParam->getInt("refDay");
int y = simParam->getInt("refYear");
outputstr.precision(3);
outputstr << endl << "\t{" << endl;
outputstr << "\t\t\"area\": \"" << fixed << (ff->getArea() / 10000.0) << "ha\"," << endl;
outputstr << "\t\t\"date\": \"" << SimulationParameters::FormatISODate(t, y, d) << "\"," << endl;
outputstr << "\t\t\"projection\": \""
<< SimulationParameters::GetInstance()->getParameter("projection") << "\"," << endl;
outputstr << "\t\t\"coordinates\": \"";
lastLevel = 1;
}
}
else if (dumpMode == GEOJSON_MODE) {
// In GEOJSON mode we aggregate rings from nodes.
// For a top–level fire front (currentLevel==1), clear any previous data and start a new feature.
if (currentLevel == 1) {
geojson_current_feature.clear();
geojson_current_feature.push_back(std::vector<std::string>()); // main (outer) ring
}
else if (currentLevel == 2) {
// For an inner boundary (hole), add a new ring.
geojson_current_feature.push_back(std::vector<std::string>());
}
// (No immediate output; nodes will be collected.)
}
else if (dumpMode == KML_MODE) {
// KML mode unchanged from your adjustments.
if (currentLevel == 1) {
outputstr << "<Placemark>" << endl;
outputstr << "<Style>" << endl;
outputstr << " <LineStyle>" << endl;
outputstr << " <color>ff0000ff</color>" << endl;
outputstr << " <width>2</width>" << endl;
outputstr << "</LineStyle>" << endl;
outputstr << "<PolyStyle>" << endl;
outputstr << " <color>22000000</color>" << endl;
outputstr << "</PolyStyle>" << endl;
outputstr << "</Style>" << endl;
}
if (currentLevel % 2 == 1) { // For top-level front (outer boundary)
outputstr << "<Polygon>" << endl;
outputstr << " <outerBoundaryIs>" << endl;
outputstr << " <LinearRing>" << endl;
outputstr << " <coordinates>" << endl;
} else { // For inner boundaries
outputstr << "<innerBoundaryIs>" << endl;
outputstr << " <LinearRing>" << endl;
outputstr << " <coordinates>" << endl;
}
}
}
// Visit a fire node. In GEOJSON mode, add its coordinate to the current ring.
void StringRepresentation::visit(FireNode* fn) {
if (dumpMode == JSON_MODE) {
if (fn->getFront()->getDomain()->getSimulationTime() >= fn->getFront()->getTime()) {
if (lastLevel == 2)
outputstr << ' ';
outputstr.precision(3);
outputstr << fixed << fn->getX() << ',' << fn->getY() << ',' << fn->getSpeed();
lastLevel = 2;
}
return;
}
if (dumpMode == FF_MODE) {
for (size_t k = 0; k < currentLevel; k++)
outputstr << " ";
outputstr << fn->toString() << endl;
return;
}
if (dumpMode == GEOJSON_MODE) {
if (fn->getFront()->getDomain()->getSimulationTime() >= fn->getFront()->getTime()) {
ostringstream coord;
// std::cout<<domain->getRefLongitude()<<" "<< domain->getMetersPerDegreesLon()<<std::endl;
coord << "["
<< fixed << setprecision(5)
<< fn->getLoc().projectLon(domain->getRefLongitude(), domain->getMetersPerDegreesLon()) << ", "
<< fn->getLoc().projectLat(domain->getRefLatitude(), domain->getMetersPerDegreeLat())
<< ", 0]";
if (!geojson_current_feature.empty()) {
// Append coordinate to the current (last) ring.
geojson_current_feature.back().push_back(coord.str());
}
}
return;
}
if (dumpMode == KML_MODE) {
if (fn->getFront()->getDomain()->getSimulationTime() >= fn->getFront()->getTime()) {
outputstr.precision(5);
outputstr << fixed
<< fn->getLoc().projectLon(domain->getRefLongitude(), domain->getMetersPerDegreesLon()) << ","
<< fn->getLoc().projectLat(domain->getRefLatitude(), domain->getMetersPerDegreeLat())
<< ",0 ";
}
return;
}
}
// postVisitInner: called after a FireFront's nodes but before processing its internals.
// For GEOJSON mode, reverse the current ring to mimic the Python [::-1] behavior.
void StringRepresentation::postVisitInner(FireFront* ff) {
if (dumpMode == JSON_MODE) {
// outputstr << "\"" << endl << "\t}";
}
else if (dumpMode == GEOJSON_MODE) {
if (!geojson_current_feature.empty() && !geojson_current_feature.back().empty()) {
std::reverse(geojson_current_feature.back().begin(), geojson_current_feature.back().end());
}
}
else if (dumpMode == KML_MODE) {
if (currentLevel % 2 == 1) { // outer boundary
outputstr << endl << " </coordinates>" << endl;
outputstr << " </LinearRing>" << endl;
outputstr << " </outerBoundaryIs>" << endl;
} else { // inner boundary
outputstr << endl << " </coordinates>" << endl;
outputstr << " </LinearRing>" << endl;
outputstr << " </innerBoundaryIs>" << endl;
}
}
}
// postVisitAll: called at the very end after processing internals.
// For GEOJSON mode, if finishing a top–level FireFront (currentLevel==1), build and output its Feature.
void StringRepresentation::postVisitAll(FireFront* ff) {
if (dumpMode == JSON_MODE) {
// (JSON mode unchanged)
}
else if (dumpMode == GEOJSON_MODE) {
if (currentLevel == 1) {
// Build the GeoJSON Feature from geojson_current_feature.
ostringstream feature;
feature << "\t{" << "\n";
feature << "\t\t\"type\": \"Feature\",\n";
feature << "\t\t\"properties\": { \"numberOfPolygons\": " << geojson_current_feature.size() << " },\n";
feature << "\t\t\"geometry\": {\n";
feature << "\t\t\t\"type\": \"MultiPolygon\",\n";
feature << "\t\t\t\"coordinates\": [ [ ";
bool firstRing = true;
for (const auto &ring : geojson_current_feature) {
if (!firstRing)
feature << ", ";
firstRing = false;
feature << "[";
bool firstCoord = true;
std::string firstCoordinate;
for (const auto &coord : ring) {
if (firstCoord) {
firstCoordinate = coord; // Store the very first coordinate.
firstCoord = false;
} else {
feature << ", "; // Add comma *before* subsequent coordinates
}
feature << coord;
}
// Close the ring by adding the first coordinate again.
if (!firstCoordinate.empty()) { // Make sure the ring wasn't empty.
feature << ", " << firstCoordinate;
}
feature << "]";
}
feature << " ] ]\n";
feature << "\t\t}\n";
feature << "\t}";
if (!firstGeoFeature) {
outputstr << ",\n" << feature.str();
} else {
firstGeoFeature = false;
outputstr << feature.str();
}
}
}
else if (dumpMode == KML_MODE) {
if (currentLevel == 1) { // finishing top-level front
outputstr << endl << " </Polygon>" << endl;
outputstr << " </Placemark>" << endl;
}
}
}
void StringRepresentation::postVisitInner(FireDomain* fd) { }
void StringRepresentation::postVisitAll(FireDomain* fd) { }
string StringRepresentation::dumpStringRepresentation() {
// Set dump mode based on simulation parameters.
string mode = SimulationParameters::GetInstance()->getParameter("dumpMode");
if (mode == "json")
dumpMode = JSON_MODE;
else if (mode == "ff")
dumpMode = FF_MODE;
else if (mode == "geojson")
dumpMode = GEOJSON_MODE;
else if (mode == "kml")
dumpMode = KML_MODE;
currentLevel = 0;
lastLevel = -1;
outputstr.str("");
if (domain != 0)
domain->accept(this);
// Append closing parts for each mode.
if (dumpMode == JSON_MODE) {
if (lastLevel >= 2)
outputstr << '"';
if (lastLevel >= 1)
outputstr << endl << "\t}" << endl;
if (lastLevel >= 0)
outputstr << "\t]" << endl << "}" << endl;
}
else if (dumpMode == GEOJSON_MODE) {
outputstr << "\n\t]" << endl;
outputstr << "}" << endl;
}
else if (dumpMode == KML_MODE) {
outputstr << "</Document>" << endl;
outputstr << "</kml>" << endl;
}
return outputstr.str();
}
void StringRepresentation::increaseLevel() {
currentLevel++;
}
void StringRepresentation::decreaseLevel() {
currentLevel--;
}
size_t StringRepresentation::getLevel() {
return currentLevel;
}
string StringRepresentation::toString() {
ostringstream oss;
oss << "string representation";
return oss.str();
}
} // namespace libforefire