-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportutils.cpp
More file actions
629 lines (536 loc) · 24.1 KB
/
Copy pathimportutils.cpp
File metadata and controls
629 lines (536 loc) · 24.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
// SPDX-FileCopyrightText: 2017 Petros Koutsolampros
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "importutils.hpp"
#include "genlib/stringutils.hpp"
#include "parsers/ntfp.hpp"
#include "parsers/tigerp.hpp"
#include <sstream>
namespace sala {
const int DXFCIRCLERES = 36;
class ImportError : public genlib::BaseException {
public:
ImportError(std::string message) : BaseException(std::move(message)) {}
};
std::vector<ShapeMap> importFile(std::istream &stream, Communicator *communicator,
std::string name, ImportType mapType,
ImportFileType fileType) {
bool parsed = false;
std::vector<ShapeMap> maps;
switch (fileType) {
case CSV: {
auto &shapeMap = maps.emplace_back(name, mapType == sala::ImportType::DATAMAP
? ShapeMap::DATAMAP
: ShapeMap::DRAWINGMAP);
parsed = importTxt(shapeMap, stream, ',');
break;
}
case TSV: {
auto &shapeMap = maps.emplace_back(name, mapType == sala::ImportType::DATAMAP
? ShapeMap::DATAMAP
: ShapeMap::DRAWINGMAP);
parsed = importTxt(shapeMap, stream, '\t');
break;
}
case CAT: {
// separate the stream and the communicator, allowing non-file streams read
return loadCat(communicator->getInFileStream(), communicator, mapType);
}
case RT1: {
// separate the stream and the communicator, allowing non-file streams read
return loadRT1(communicator->GetFileSet(), communicator, mapType);
}
case NTF: {
NtfMap map;
try {
map.open(communicator->GetFileSet(), communicator);
} catch (std::invalid_argument &) {
throw new ImportError("Invalid argument when parsing file");
} catch (std::out_of_range &) {
throw new ImportError("Out of range error when parsing file");
}
if (communicator->IsCancelled()) {
return maps;
}
for (auto layer : map.layers) {
auto &shapeMap = maps.emplace_back(
layer.getName(), mapType == sala::ImportType::DATAMAP ? ShapeMap::DATAMAP
: ShapeMap::DRAWINGMAP);
shapeMap.init(layer.getLineCount(), map.getRegion());
for (const auto &geometry : layer.geometries) {
for (const auto &line : geometry.lines) {
shapeMap.makeLineShape(line);
}
}
}
break;
}
case DXF: {
DxfParser dp;
if (communicator) {
dp = DxfParser(communicator);
try {
stream >> dp;
} catch (std::logic_error &) {
throw new ImportError("Logic error when parsing file");
}
if (communicator->IsCancelled()) {
return maps;
}
} else {
dp.open(stream);
}
for (auto &layer : dp.getLayers()) {
auto &shapeMap = maps.emplace_back(layer.first, mapType == sala::ImportType::DATAMAP
? ShapeMap::DATAMAP
: ShapeMap::DRAWINGMAP);
const DxfLayer &dxfLayer = layer.second;
if (dxfLayer.empty()) {
continue;
}
parsed = importDxfLayer(dxfLayer, shapeMap);
}
break;
}
}
if (parsed) {
return maps;
} else {
return std::vector<ShapeMap>();
}
}
std::vector<ShapeMap> loadCat(std::istream &stream, Communicator *communicator,
ImportType mapType) {
if (communicator) {
long size = communicator->GetInfileSize();
communicator->CommPostMessage(Communicator::NUM_RECORDS, static_cast<size_t>(size));
}
time_t atime = 0;
qtimer(atime, 0);
size_t size = 0;
size_t numlines = 0;
int parsing = 0;
bool first = true;
Point2f currentPoint, minPoint, maxPoint;
while (!stream.eof()) {
std::string inputline;
stream >> inputline;
if (inputline.length() > 1 && inputline[0] != '#') {
if (!parsing) {
if (dXstring::toLower(inputline) == "begin polygon") {
parsing = 1;
} else if (dXstring::toLower(inputline) == "begin polyline") {
parsing = 2;
}
} else if (dXstring::toLower(inputline).substr(0, 3) == "end") {
parsing = 0;
} else {
auto tokens = dXstring::split(inputline, ' ', true);
currentPoint.x = stod(tokens[0]);
currentPoint.y = stod(tokens[1]);
numlines++;
if (first) {
minPoint = currentPoint;
maxPoint = currentPoint;
first = false;
} else {
if (currentPoint.x < minPoint.x) {
minPoint.x = currentPoint.x;
}
if (currentPoint.y < minPoint.y) {
minPoint.y = currentPoint.y;
}
if (currentPoint.x > maxPoint.x) {
maxPoint.x = currentPoint.x;
}
if (currentPoint.y > maxPoint.y) {
maxPoint.y = currentPoint.y;
}
}
}
}
}
std::vector<ShapeMap> maps;
auto &shapeMap = maps.emplace_back("CATDATA", mapType == sala::ImportType::DATAMAP
? ShapeMap::DATAMAP
: ShapeMap::DRAWINGMAP);
shapeMap.init(numlines, Region4f(minPoint, maxPoint));
// in MSVC 6, ios::eof remains set and it needs to be cleared.
// in MSVC 8 it's even worse: it won't even seekg until eof flag has been
// cleared
stream.clear();
stream.seekg(0, std::ios::beg);
parsing = 0;
std::vector<Point2f> points;
while (!stream.eof()) {
std::string inputline;
stream >> inputline;
if (inputline.length() > 1 && inputline[0] != '#') {
if (!parsing) {
if (dXstring::toLower(inputline) == "begin polygon") {
parsing = 1;
} else if (dXstring::toLower(inputline) == "begin polyline") {
parsing = 2;
}
} else if (dXstring::toLower(inputline).substr(0, 3) == "end") {
if (points.size() > 2) {
if (parsing == 1) { // polygon
shapeMap.makePolyShape(points, false);
} else { // polyline
shapeMap.makePolyShape(points, true);
}
} else if (points.size() == 2) {
shapeMap.makeLineShape(Line4f(points[0], points[1]));
}
points.clear();
parsing = 0;
} else {
auto tokens = dXstring::split(inputline, ' ', true);
currentPoint.x = stod(tokens[0]);
currentPoint.y = stod(tokens[1]);
points.push_back(currentPoint);
}
}
size += inputline.length() + 1;
if (communicator) {
if (qtimer(atime, 500)) {
if (communicator->IsCancelled()) {
throw Communicator::CancelledException();
}
communicator->CommPostMessage(Communicator::CURRENT_RECORD, size);
}
}
}
return maps;
}
std::vector<ShapeMap> loadRT1(const std::vector<std::string> &fileset,
Communicator *communicator, ImportType mapType) {
TigerMap map;
try {
map.parse(fileset, communicator);
} catch (std::invalid_argument &) {
throw new ImportError("Invalid argument when parsing file");
} catch (std::out_of_range &) {
throw new ImportError("Out of range error when parsing file");
}
std::vector<ShapeMap> maps;
if (communicator->IsCancelled()) {
return maps;
}
// for each category
for (const auto &val : map.categories) {
auto &shapeMap = maps.emplace_back(val.first, mapType == sala::ImportType::DATAMAP
? ShapeMap::DATAMAP
: ShapeMap::DRAWINGMAP);
shapeMap.init(val.second.chains.size(), map.getRegion());
// for each chains in category:
for (size_t j = 0; j < val.second.chains.size(); j++) {
// for each node pair in each category
for (size_t k = 0; k < val.second.chains[j].lines.size(); k++) {
shapeMap.makeLineShape(val.second.chains[j].lines[k]);
}
}
}
return maps;
}
bool importTxt(ShapeMap &shapeMap, std::istream &stream, char delimiter = '\t') {
Table table = csvToTable(stream, delimiter);
std::vector<std::string> columns;
int xcol = -1, ycol = -1, x1col = -1, y1col = -1, x2col = -1, y2col = -1, refcol = -1;
for (auto const &column : table) {
if (column.first == "x" || column.first == "easting")
xcol = static_cast<int>(columns.size());
else if (column.first == "y" || column.first == "northing")
ycol = static_cast<int>(columns.size());
else if (column.first == "x1")
x1col = static_cast<int>(columns.size());
else if (column.first == "x2")
x2col = static_cast<int>(columns.size());
else if (column.first == "y1")
y1col = static_cast<int>(columns.size());
else if (column.first == "y2")
y2col = static_cast<int>(columns.size());
else if (column.first == "Ref")
refcol = static_cast<int>(columns.size());
columns.push_back(column.first);
}
if (xcol != -1 && ycol != -1 && refcol != -1) {
std::map<int, Point2f> points =
extractPointsWithRefs(table[columns[static_cast<size_t>(xcol)]],
table[columns[static_cast<size_t>(ycol)]],
table[columns[static_cast<size_t>(refcol)]]);
table.erase(table.find(columns[static_cast<size_t>(xcol)]));
table.erase(table.find(columns[static_cast<size_t>(ycol)]));
table.erase(table.find(columns[static_cast<size_t>(refcol)]));
Region4f region;
for (auto &point : points) {
if (region.atZero()) {
region = point.second;
} else {
region = region.runion(point.second);
}
}
shapeMap.init(points.size(), region);
shapeMap.importPointsWithRefs(points, table);
} else if (xcol != -1 && ycol != -1) {
std::vector<Point2f> points = extractPoints(table[columns[static_cast<size_t>(xcol)]],
table[columns[static_cast<size_t>(ycol)]]);
table.erase(table.find(columns[static_cast<size_t>(xcol)]));
table.erase(table.find(columns[static_cast<size_t>(ycol)]));
Region4f region;
for (auto &point : points) {
if (region.atZero()) {
region = point;
} else {
region = region.runion(point);
}
}
shapeMap.init(points.size(), region);
shapeMap.importPoints(points, table);
} else if (x1col != -1 && y1col != -1 && x2col != -1 && y2col != -1 && refcol != -1) {
std::map<int, Line4f> lines =
extractLinesWithRef(table[columns[static_cast<size_t>(x1col)]],
table[columns[static_cast<size_t>(y1col)]],
table[columns[static_cast<size_t>(x2col)]],
table[columns[static_cast<size_t>(y2col)]],
table[columns[static_cast<size_t>(refcol)]]);
table.erase(table.find(columns[static_cast<size_t>(x1col)]));
table.erase(table.find(columns[static_cast<size_t>(y1col)]));
table.erase(table.find(columns[static_cast<size_t>(x2col)]));
table.erase(table.find(columns[static_cast<size_t>(y2col)]));
table.erase(table.find(columns[static_cast<size_t>(refcol)]));
Region4f region;
for (auto &line : lines) {
if (region.atZero()) {
region = line.second;
} else {
region = region.runion(line.second);
}
}
shapeMap.init(lines.size(), region);
shapeMap.importLinesWithRefs(lines, table);
} else if (x1col != -1 && y1col != -1 && x2col != -1 && y2col != -1) {
std::vector<Line4f> lines = extractLines(table[columns[static_cast<size_t>(x1col)]],
table[columns[static_cast<size_t>(y1col)]],
table[columns[static_cast<size_t>(x2col)]],
table[columns[static_cast<size_t>(y2col)]]);
table.erase(table.find(columns[static_cast<size_t>(x1col)]));
table.erase(table.find(columns[static_cast<size_t>(y1col)]));
table.erase(table.find(columns[static_cast<size_t>(x2col)]));
table.erase(table.find(columns[static_cast<size_t>(y2col)]));
Region4f region;
for (auto &line : lines) {
if (region.atZero()) {
region = line;
} else {
region = region.runion(line);
}
}
shapeMap.init(lines.size(), region);
shapeMap.importLines(lines, table);
}
return true;
}
Table csvToTable(std::istream &stream, char delimiter = '\t') {
Table table;
std::vector<std::string> columns;
std::string inputline;
std::getline(stream, inputline);
// check for a matching delimited header line...
auto strings = dXstring::split(inputline, delimiter);
if (strings.size() < 2) {
// throw exception
return table;
}
for (auto &columnName : strings) {
if (!columnName.empty()) {
dXstring::ltrim(columnName, '\"');
dXstring::rtrim(columnName, '\"');
}
table.insert(std::make_pair(columnName, std::vector<std::string>()));
columns.push_back(columnName);
}
while (!stream.eof()) {
std::getline(stream, inputline);
if (!inputline.empty()) {
auto inputStrings = dXstring::split(inputline, delimiter);
if (inputStrings.size() != columns.size()) {
std::stringstream message;
message << "Cells in line " << inputline
<< " not the same number as the columns" << std::flush;
throw genlib::RuntimeException(message.str().c_str());
}
if (!inputStrings.size()) {
continue;
}
for (size_t i = 0; i < inputStrings.size(); i++) {
table[columns[i]].push_back(inputStrings[i]);
}
}
}
return table;
}
std::vector<Line4f> extractLines(ColumnData &x1col, ColumnData &y1col, ColumnData &x2col,
ColumnData &y2col) {
std::vector<Line4f> lines;
for (size_t i = 0; i < x1col.size(); i++) {
double x1 = stod(x1col[i]);
double y1 = stod(y1col[i]);
double x2 = stod(x2col[i]);
double y2 = stod(y2col[i]);
lines.push_back(Line4f(Point2f(x1, y1), Point2f(x2, y2)));
}
return lines;
}
std::map<int, Line4f> extractLinesWithRef(ColumnData &x1col, ColumnData &y1col,
ColumnData &x2col, ColumnData &y2col,
ColumnData &refcol) {
std::map<int, Line4f> lines;
for (size_t i = 0; i < x1col.size(); i++) {
double x1 = stod(x1col[i]);
double y1 = stod(y1col[i]);
double x2 = stod(x2col[i]);
double y2 = stod(y2col[i]);
int ref = stoi(refcol[i]);
lines.insert(std::make_pair(ref, Line4f(Point2f(x1, y1), Point2f(x2, y2))));
}
return lines;
}
std::vector<Point2f> extractPoints(ColumnData &x, ColumnData &y) {
std::vector<Point2f> points;
for (size_t i = 0; i < x.size(); i++) {
points.push_back(Point2f(stod(x[i]), stod(y[i])));
}
return points;
}
std::map<int, Point2f> extractPointsWithRefs(ColumnData &x, ColumnData &y, ColumnData &ref) {
std::map<int, Point2f> points;
for (size_t i = 0; i < x.size(); i++) {
points.insert(std::make_pair(stoi(ref[i]), Point2f(stod(x[i]), stod(y[i]))));
}
return points;
}
bool importDxfLayer(const DxfLayer &dxfLayer, ShapeMap &shapeMap) {
std::vector<Point2f> points;
std::vector<Line4f> lines;
std::vector<Polyline> polylines;
for (size_t jp = 0; jp < dxfLayer.numPoints(); jp++) {
const DxfVertex &dxfPoint = dxfLayer.getPoint(jp);
points.push_back(Point2f(dxfPoint.x, dxfPoint.y));
}
for (size_t j = 0; j < dxfLayer.numLines(); j++) {
const DxfLine &dxfLine = dxfLayer.getLine(j);
Line4f line = Line4f(Point2f(dxfLine.getStart().x, dxfLine.getStart().y),
Point2f(dxfLine.getEnd().x, dxfLine.getEnd().y));
lines.push_back(line);
}
for (size_t k = 0; k < dxfLayer.numPolyLines(); k++) {
const DxfPolyLine &poly = dxfLayer.getPolyLine(k);
std::vector<Point2f> vertices;
for (size_t m = 0; m < poly.numVertices(); m++) {
DxfVertex v = poly.getVertex(m);
vertices.push_back(Point2f(v.x, v.y));
}
polylines.push_back(
sala::Polyline(std::move(vertices), (poly.getAttributes() & DxfPolyLine::CLOSED) ==
DxfPolyLine::CLOSED));
}
for (size_t l = 0; l < dxfLayer.numSplines(); l++) {
const DxfSpline &poly = dxfLayer.getSpline(l);
std::vector<Point2f> vertices;
for (size_t m = 0; m < poly.numVertices(); m++) {
DxfVertex v = poly.getVertex(m);
vertices.push_back(Point2f(v.x, v.y));
}
polylines.push_back(
sala::Polyline(std::move(vertices), (poly.getAttributes() & DxfPolyLine::CLOSED) ==
DxfPolyLine::CLOSED));
}
for (size_t n = 0; n < dxfLayer.numArcs(); n++) {
const DxfArc &circ = dxfLayer.getArc(n);
std::vector<Point2f> vertices;
int segments = static_cast<int>(circ.numSegments(DXFCIRCLERES));
if (segments > 1) {
for (int m = 0; m <= segments; m++) {
DxfVertex v = circ.getVertex(m, segments);
vertices.push_back(Point2f(v.x, v.y));
}
}
polylines.push_back(sala::Polyline(std::move(vertices), false));
}
for (size_t n = 0; n < dxfLayer.numEllipses(); n++) {
const DxfEllipse &ellipse = dxfLayer.getEllipse(n);
std::vector<Point2f> vertices;
auto segments = static_cast<int>(ellipse.numSegments(DXFCIRCLERES));
if (segments > 1) {
for (int m = 0; m <= segments; m++) {
DxfVertex v = ellipse.getVertex(m, segments);
vertices.push_back(Point2f(v.x, v.y));
}
}
polylines.push_back(sala::Polyline(std::move(vertices), false));
}
for (size_t nc = 0; nc < dxfLayer.numCircles(); nc++) {
const DxfCircle &circ = dxfLayer.getCircle(nc);
std::vector<Point2f> vertices;
for (int m = 0; m < DXFCIRCLERES; m++) {
DxfVertex v = circ.getVertex(m, DXFCIRCLERES);
vertices.push_back(Point2f(v.x, v.y));
}
polylines.push_back(sala::Polyline(std::move(vertices), true));
}
DxfVertex layerMin = dxfLayer.getExtMin();
DxfVertex layerMax = dxfLayer.getExtMax();
Region4f region =
Region4f(Point2f(layerMin.x, layerMin.y), Point2f(layerMax.x, layerMax.y));
shapeMap.init(points.size() + lines.size() + polylines.size(), region);
// parameters could be passed in the Table here such as the
// layer/block/colour/linetype etc.
shapeMap.importPoints(points, Table());
shapeMap.importLines(lines, Table());
shapeMap.importPolylines(polylines, Table());
return true;
}
bool importAttributes(AttributeTable &attributes, std::istream &stream, char delimiter = '\t') {
Table table = csvToTable(stream, delimiter);
std::vector<std::string> outColumns;
int refcol = -1;
for (auto const &column : table) {
if (column.first == "Ref")
refcol = static_cast<int>(outColumns.size());
else
outColumns.push_back(column.first);
}
if (table.size() == 0) {
throw genlib::RuntimeException("No usable data found in file");
}
if (refcol == -1) {
throw genlib::RuntimeException("The \"Ref\" column is reqired");
}
if (outColumns.size() < 1) {
throw genlib::RuntimeException("No data found to join");
}
std::vector<AttributeRow *> inRows;
for (const auto &refInFile : table["Ref"]) {
int ref = std::stoi(refInFile);
auto iter = attributes.find(AttributeKey(ref));
if (iter == attributes.end()) {
std::stringstream message;
message << "Key " << ref << "not found in attribute table" << std::flush;
throw genlib::RuntimeException(message.str().c_str());
}
inRows.push_back(&(iter->getRow()));
}
// Up until this point we have not touched the attribute table
// so no need for corrective measures yet
for (const std::string &column : outColumns) {
auto colIdx = attributes.insertOrResetColumn(column);
auto outRowIter = table[column].begin();
for (auto inRowIter = inRows.begin(); inRowIter != inRows.end();
inRowIter++, outRowIter++) {
(*inRowIter)->setValue(colIdx, std::stof(*outRowIter));
}
}
return true;
}
} // namespace sala