-
Notifications
You must be signed in to change notification settings - Fork 6
/
RDT.cpp
464 lines (365 loc) · 14.2 KB
/
RDT.cpp
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
// Copyright 2019 ESRI
//
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
//
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
//
// See the Sample code usage restrictions document for further information.
//
#include "RDT.h"
#include "Basemap.h"
#include "Map.h"
#include "MapQuickView.h"
#include "FeatureCollectionLayer.h"
#include "FeatureCollection.h"
#include "FeatureCollectionTable.h"
#include "SimpleMarkerSymbol.h"
#include "SimpleRenderer.h"
#include "ClassBreaksRenderer.h"
#include "QJsonDocument"
#include "QDir"
#include <QUrl>
#include <QThread>
#include <QSettings>
#include <PolylineBuilder.h>
#include <Polyline.h>
using namespace Esri::ArcGISRuntime;
RDT::RDT(QObject* parent /* = nullptr */):
QObject(parent),
m_map(new Map(Basemap::topographic(this), this)),
m_loggedIn(false)
{
QString tenant("https://agave.designsafe-ci.org");
QString storage("designsafe.storage.default");
client = new AgaveCurl(tenant, storage);
QThread* agaveThread = new QThread();
agaveThread->setObjectName("AgaveThread");
client->moveToThread(agaveThread);
connect(agaveThread, &QThread::finished, client, &QObject::deleteLater);
connect(agaveThread, &QThread::finished, agaveThread, &QObject::deleteLater);
agaveThread->start();
m_jobsList = new JobsListModel();
m_jobDetails = new JobDetailsModel();
m_inputs << "agave://designsafe.storage.community/SimCenter/Datasets/AnchorageM7/AnchorageBuildings.zip";
m_inputs << "agave://designsafe.storage.community/SimCenter/Datasets/AnchorageM7/AnchorageM7GMs.zip";
m_rendererModel = new RendererModel();
setupConnections();
}
RDT::~RDT()
{
}
void RDT::addCSVLayer(QString filePath)
{
if (filePath.startsWith("file:///"))
filePath = filePath.remove("file:///");
if(filePath.endsWith("values.txt"))
{
addNetwork(filePath);
return;
}
QFile csvFile(filePath);
csvFile.open(QIODevice::ReadOnly);
auto header = csvFile.readLine();
QList<Field> fields;
auto featureCollection = new FeatureCollection();
for (auto columnName:header.split(','))
{
if(columnName == "LossRatio")
fields.append(Field::createDouble(columnName, "Loss Ratio"));
else
fields.append(Field::createText(columnName, columnName, 16));
}
auto featureCollectionTable = new FeatureCollectionTable(fields, GeometryType::Point, SpatialReference::wgs84());
QList<Feature*> features;
while(!csvFile.atEnd())
{
auto line = csvFile.readLine();
auto columns = line.trimmed().split(',');
auto feature = featureCollectionTable->createFeature();
if(columns.count()<9)
continue;
if(columns[7].isEmpty() || columns[8].isEmpty())
continue;
if(!columns[6].isEmpty())
feature->attributes()->replaceAttribute("LossRatio", columns[6].toDouble());
feature->setGeometry(Point(columns[8].toDouble(), columns[7].toDouble()));
features.append(feature);
}
featureCollectionTable->addFeatures(features);
// Define the renderer
QList<ClassBreak*> classBreaks;
auto greenCircle = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Circle, QColor("Light Green"), 5.0, this);
auto greenClassBreak = new ClassBreak("Low Loss Ratio", "Loss Ratio less than 10%", 0.0, 0.1, greenCircle);
classBreaks.append(greenClassBreak);
auto yellowCircle = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Circle, QColor("Yellow"), 5.0, this);
auto yellowClassBreak = new ClassBreak("Medium Loss Ratio", "Loss Ratio Between 10% and 50%", 0.1, 0.5, yellowCircle);
classBreaks.append(yellowClassBreak);
auto redCircle = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Circle, QColor("Red"), 5.0, this);
auto classBreak = new ClassBreak("High Loss Ratio", "Loss Ratio more than 50%", 0.5, 1.0,redCircle);
classBreaks.append(classBreak);
auto renderer = new ClassBreaksRenderer("LossRatio", classBreaks);
featureCollectionTable->setRenderer(renderer);
featureCollection->tables()->append(featureCollectionTable);
auto csvLayer = new FeatureCollectionLayer(featureCollection);
QFileInfo fileInfo(filePath);
csvLayer->setName(fileInfo.fileName());
csvLayer->setDescription("Imported layer");
m_map->operationalLayers()->append(csvLayer);
}
void RDT::refresh()
{
m_map->load();
}
bool RDT::isLoggedIn()
{
return client->isLoggedIn();
}
void RDT::login(QString username, QString password)
{
emit startLogin(username, password);
}
void RDT::refreshJobs()
{
if(client->isLoggedIn())
emit startJobListUpdate("", "rWHALE-1*");
}
void RDT::getJobDetails(int index)
{
auto jobId = m_jobsList->getJobId(index);
emit startJobDetailsUpdate(jobId);
return;
}
void RDT::loadResultFile(QString outputFile)
{
//client->remoteLSCall(outputFile);
for (auto file: m_jobDetails->getOutputs())
{
auto fileName = file.toObject()["name"].toString();
if(0 == fileName.compare(outputFile))
{
auto path = file.toObject()["path"].toString();
QStringList remoteFiles;
//TODO: we may need to handle files from different storage systems, for now using default
remoteFiles << "system/designsafe.storage.default" + path;
QStringList localFiles;
m_resultsPath = QDir::tempPath() + path;
localFiles << m_resultsPath;
QDir(m_resultsPath.left(m_resultsPath.lastIndexOf('/'))).mkpath(".");
client->downloadFilesCall(remoteFiles, localFiles, this);
}
}
}
void RDT::submitJob(QString job)
{
auto jobDoc = QJsonDocument::fromJson(job.toUtf8());
client->startJobCall(jobDoc.object());
}
void RDT::downloadOutputFile(QString outputFileName, QString filePath)
{
if (filePath.startsWith("file:///"))
filePath = filePath.remove("file:///");
for (auto file: m_jobDetails->getOutputs())
{
auto fileName = file.toObject()["name"].toString();
if(0 == fileName.compare(outputFileName))
{
auto path = file.toObject()["path"].toString();
QStringList remoteFiles;
//TODO: we may need to handle files from different storage systems, for now using default
remoteFiles << "system/designsafe.storage.default" + path;
QStringList localFiles;
localFiles << filePath;
client->downloadFilesCall(remoteFiles, localFiles, nullptr);
}
}
}
void RDT::deleteLayer(int index)
{
m_map->operationalLayers()->removeAt(index);
}
void RDT::moveLayerUp(int index)
{
if (index > 0)
m_map->operationalLayers()->move(index, index - 1);
}
void RDT::moveLayerDown(int index)
{
if (index < m_map->operationalLayers()->size() - 1)
m_map->operationalLayers()->move(index, index + 1);
}
void RDT::setRenderer(int index)
{
auto layer = reinterpret_cast<FeatureCollectionLayer*>(m_map->operationalLayers()->at(index));
auto table = reinterpret_cast<FeatureCollectionTable*>(layer->featureCollection()->tables()->at(0));
m_rendererModel->setRenderer(reinterpret_cast<ClassBreaksRenderer*>(table->renderer()));
emit rendererChanged();
}
QString RDT::username()
{
QSettings settings("SimCenter", "Common");
return settings.value("loginAgave", "").toString();
}
QString RDT::password()
{
QSettings settings("SimCenter", "Common");
return settings.value("passwordAgave", "").toString();
}
void RDT::addNetwork(QString filePath)
{
QFile valuesFile(filePath);
valuesFile.open(QIODevice::ReadOnly);
auto allValues = valuesFile.readAll();
valuesFile.close();
auto values = allValues.split(' ');
qDebug() << values.count();
filePath = filePath.replace("values.txt", "seg_start.txt");
QFile startFile(filePath);
startFile.open(QIODevice::ReadOnly);
QList<QPair<double, double>> startPoints;
while(!startFile.atEnd())
{
auto startLine = startFile.readLine();
startPoints.push_back(qMakePair(startLine.split(' ')[0].toDouble(), startLine.split(' ')[1].toDouble()));
}
qDebug() << startPoints.count();
filePath = filePath.replace("seg_start.txt", "seg_end.txt");
QFile endFile(filePath);
endFile.open(QIODevice::ReadOnly);
QList<QPair<double, double>> endPoints;
while(!endFile.atEnd())
{
auto endLine = endFile.readLine();
endPoints.push_back(qMakePair(endLine.split(' ')[0].toDouble(), endLine.split(' ')[1].toDouble()));
}
qDebug() << endPoints.count();
QList<Field> fields;
auto featureCollection = new FeatureCollection();
fields.append(Field::createDouble("RepairRate", "Repair Rate"));
auto featureCollectionTable = new FeatureCollectionTable(fields, GeometryType::Polyline, SpatialReference::wgs84());
QList<Feature*> features;
for(int i = 0; i < startPoints.count(); i++ )
{
auto feature = featureCollectionTable->createFeature();
feature->attributes()->replaceAttribute("RepairRate", values[i].toDouble());
PolylineBuilder* builder = new PolylineBuilder(SpatialReference::wgs84(), this);
builder->addPoint(startPoints[i].first, startPoints[i].second);
builder->addPoint(endPoints[i].first, endPoints[i].second);
Esri::ArcGISRuntime::Polyline singleLine = builder->toPolyline();
feature->setGeometry(singleLine);
features.append(feature);
}
featureCollectionTable->addFeatures(features);
QList<ClassBreak*> classBreaks;
auto line1 = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor(254, 229, 217), 4.0, this);
auto classbreak1 = new ClassBreak("Very Low Repair Rate", "Repair Rate less than 1e-3", 0.0, 1e-3, line1);
classBreaks.append(classbreak1);
auto line2 = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor(252, 187, 161), 4.0, this);
auto classbreak2 = new ClassBreak("Low Repair Rate", "Repair Rate less than 0.01", 1e-3, 1e-2, line2);
classBreaks.append(classbreak2);
auto line3 = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor(252, 146, 114), 4.0, this);
auto classbreak3 = new ClassBreak("Very Moderate Repair Rate", "Repair Rate less than 0.1", 1e-2, 1e-1, line3);
classBreaks.append(classbreak3);
auto line4 = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor(251, 106, 74), 4.0, this);
auto classbreak4 = new ClassBreak("Moderate Repair Rate", "Repair Rate less than 1", 1e-1, 1e0, line4);
classBreaks.append(classbreak4);
auto line5 = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor(222, 45, 38), 4.0, this);
auto classbreak5 = new ClassBreak("High Repair Rate", "Repair Rate less than 10", 1e0, 1e1, line5);
classBreaks.append(classbreak5);
auto line6 = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor(165, 15, 21), 4.0, this);
auto classbreak6 = new ClassBreak("Very High Repair Rate", "Repair Rate less than 1e10", 1e0, 1e10, line6);
classBreaks.append(classbreak6);
auto renderer = new ClassBreaksRenderer("RepairRate", classBreaks);
featureCollectionTable->setRenderer(renderer);
featureCollection->tables()->append(featureCollectionTable);
auto csvLayer = new FeatureCollectionLayer(featureCollection);
m_map->operationalLayers()->append(csvLayer);
Viewpoint bayArea(37.8272, -122.2913, 500000.0);
m_mapView->setViewpoint(bayArea);
}
MapQuickView* RDT::mapView() const
{
return m_mapView;
}
// Set the view (created in QML)
void RDT::setMapView(MapQuickView* mapView)
{
if (!mapView || mapView == m_mapView)
{
return;
}
m_mapView = mapView;
Viewpoint anchorage(61.2181, -149.9003, 500000.0);
m_map->setInitialViewpoint(anchorage);
m_mapView->setMap(m_map);
connect(m_mapView, &MapQuickView::drawStatusChanged, this, [this](DrawStatus drawStatus)
{
drawStatus == DrawStatus::InProgress ? m_mapDrawing = true : m_mapDrawing = false;
emit mapDrawStatusChanged();
});
emit mapViewChanged();
}
void RDT::setupConnections()
{
// connect(m_mapView, &MapQuickView::dr, this, [this](QDropEvent* dropEvent){
// qDebug() << dropEvent->mimeData();
// });
connect(client, &AgaveCurl::remoteLSReturn, this, [this](QJsonArray remoteFiles){
m_jobDetails->setOutputs(remoteFiles);
});
connect(client, &AgaveCurl::downloadFilesReturn, this, [this](bool result, QObject* sender){
Q_UNUSED(result)
if(sender == this)
{
if(m_resultsPath.endsWith(".csv"))
this->addCSVLayer(m_resultsPath);
else
{
QFile file(m_resultsPath);
file.open(QFile::ReadOnly);
m_textFileContents = file.readAll();
file.close();
emit textFileContentsChanged();
}
}
});
connect(client, &AgaveCurl::getJobListReturn, this, [this](QJsonObject jobs){
m_jobsList->setJobs(jobs);
});
connect(client, &AgaveCurl::getJobDetailsReturn, this, [this](QJsonObject jobDetails){
m_jobDetails->setJob(jobDetails);
client->remoteLSCall(jobDetails["archivePath"].toString());
});
connect(client, &AgaveCurl::startJobReturn, this, [](QString jobreturn){
qDebug() << jobreturn;//TODO: handle job submisstion result
});
connect(client, &AgaveCurl::loginReturn, this, [this](bool loggedIn){
m_loggedIn = loggedIn;
emit loggedInChanged();
});
connect(client, &AgaveCurl::logoutReturn, this, [this](bool loggedOut){
m_loggedIn = !loggedOut;
emit loggedInChanged();
});
connect(this, &RDT::startLogin, client, &AgaveCurl::loginCall);
connect(this, &RDT::startJobListUpdate, client, &AgaveCurl::getJobListCall);
connect(this, &RDT::startJobDetailsUpdate, client, &AgaveCurl::getJobDetailsCall);
}
bool RDT::mapDrawing() const
{
return m_mapDrawing;
}
JobsListModel *RDT::jobsList()
{
return m_jobsList;
}
JobDetailsModel *RDT::jobDetails()
{
return m_jobDetails;
}
LayerListModel *RDT::getLayers()
{
return m_map->operationalLayers();
}