268 lines
7.7 KiB
C++
Executable File
268 lines
7.7 KiB
C++
Executable File
/**
|
|
* AES Function Topic Client test executable
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
*/
|
|
|
|
#include "aes_convert_mine_area.h"
|
|
|
|
#include <QTimer>
|
|
#include <QLineF>
|
|
#include <nglapplication/nglcoreapplication.h>
|
|
#include <LtsCommandLineParser/LtsCommandLineParser.h>
|
|
|
|
#include <ngldb/dbtransaction.h>
|
|
#include <ngldb/nsqlquery.h>
|
|
#include <ngldb/dbconnection.h>
|
|
#include <ngldb/sql_query_janitor.h>
|
|
#include <QtSql/QSqlError>
|
|
#include <QVariant>
|
|
|
|
|
|
#include <AreaEffectSimulation/aes_db_structs.h>
|
|
#include <LtsCoordTrans/lts_coord_trans.h>
|
|
#include <nglsettings/nglsettings_connections.h>
|
|
#include <nglsettings/nglsettings_settings.h>
|
|
#include <ngltools/ngltools_filesystem.h>
|
|
|
|
namespace AreaEffectSimulation
|
|
{
|
|
|
|
ConvertMineArea::ConvertMineArea(QObject *parent)
|
|
: QObject(parent)
|
|
, m_query(nullptr)
|
|
, m_exerciseId(-1)
|
|
, m_bEmitMessageOnError(false)
|
|
, m_bCanHandleSchemas(true)
|
|
, m_ownDatabaseConnection(true)
|
|
, m_logger(getLogger(QStringLiteral("ConvertMineArea")))
|
|
{}
|
|
|
|
bool ConvertMineArea::init(int exerciseId)
|
|
{
|
|
m_exerciseId = exerciseId;
|
|
|
|
NGL::Settings *s = new NGL::Settings(NGL::findProjectConfigFile());
|
|
const QString dbUser(QStringLiteral("ets_exm"));
|
|
const QString dbPasswd(QStringLiteral("ets11de"));
|
|
const QString hostname(s->databaseHostname());
|
|
const QString driver(s->databaseDriver());
|
|
const QString dbname(s->databaseName());
|
|
|
|
bool ok = initDB(dbUser, dbPasswd, driver, hostname, dbname);
|
|
|
|
if (!ok)
|
|
{
|
|
ERROR_LOG(m_logger) << "Error opening db, exiting..";
|
|
return false;
|
|
}
|
|
|
|
ERROR_LOG(m_logger) << "Database open";
|
|
|
|
return true;
|
|
}
|
|
|
|
bool ConvertMineArea::initDB(const QString &dbUser,
|
|
const QString &dbPasswd,
|
|
const QString &dbDriver,
|
|
const QString &dbHost,
|
|
const QString &dbName)
|
|
{
|
|
m_ownDatabaseConnection = true;
|
|
|
|
m_dbConnection = DBConnection::open(m_logger, dbUser, dbPasswd, dbDriver, dbHost, dbName, schemaName());
|
|
|
|
return initDBInternal();
|
|
}
|
|
|
|
bool ConvertMineArea::initDBInternal()
|
|
{
|
|
QSqlDriver *driver = m_dbConnection.driver();
|
|
if (!driver)
|
|
{
|
|
WARNING_LOG(m_logger) << "No SQL driver loaded - no database available!";
|
|
return false;
|
|
}
|
|
const QString driverName = m_dbConnection.driverName();
|
|
DEBUG_LOG(m_logger) << "Connected to database " << m_dbConnection.databaseName() << "@" << m_dbConnection.hostName()
|
|
<< ", schema: " << schemaName() << ", used driver: " << driverName;
|
|
if (!m_dbConnection.isOpen())
|
|
{
|
|
WARNING_LOG(m_logger) << "Database connection is no open - can not initialize database interface";
|
|
return false;
|
|
}
|
|
if (driverName == QLatin1String("QSQLITE"))
|
|
m_bCanHandleSchemas = false;
|
|
|
|
m_query = new NSqlQuery(m_dbConnection, m_logger);
|
|
|
|
return true;
|
|
}
|
|
|
|
QString ConvertMineArea::schemaName() const
|
|
{
|
|
if (!m_bCanHandleSchemas)
|
|
return QString();
|
|
if (m_exerciseId <= 0)
|
|
return QStringLiteral("aes");
|
|
return DBCommon::getExerciseSchemaName(m_exerciseId);
|
|
}
|
|
|
|
QString ConvertMineArea::dbUser() const
|
|
{
|
|
if (m_exerciseId > 0)
|
|
return QStringLiteral("ets_ex_00");
|
|
else
|
|
return QStringLiteral("ets_") + schemaName();
|
|
}
|
|
|
|
//
|
|
// MineArea
|
|
//
|
|
QString ConvertMineArea::getTableName(const QString &tableName) const
|
|
{
|
|
if (!m_bCanHandleSchemas)
|
|
return tableName;
|
|
if (m_exerciseId > 0)
|
|
return DBCommon::getExerciseTableName(tableName, m_exerciseId);
|
|
return QStringLiteral("aes.") + tableName;
|
|
}
|
|
|
|
bool ConvertMineArea::tryConvertMineArea()
|
|
{
|
|
const QString tableName1(getTableName(QStringLiteral("aes_mine_area")));
|
|
const QString stmt1(QStringLiteral("SELECT id, density FROM ") + tableName1);
|
|
m_query->prepare(stmt1);
|
|
|
|
if (!m_query->exec(m_bEmitMessageOnError))
|
|
{
|
|
m_lastError = m_query->lastError();
|
|
return false;
|
|
}
|
|
|
|
QMap<int,double> densityMap;
|
|
while (m_query->next())
|
|
{
|
|
int i = 0;
|
|
int aesId = m_query->value(i++).toInt();
|
|
double density = m_query->value(i++).toInt();
|
|
densityMap.insert(aesId, density);
|
|
}
|
|
|
|
const QString tableName(getTableName(QStringLiteral("aes_mine_area_segment")));
|
|
const QString stmt(QStringLiteral("SELECT id, mine_area_id, element_number, start_position_x, start_position_y, rotation_angle, mine_count"
|
|
" FROM ") + tableName);
|
|
m_query->prepare(stmt);
|
|
|
|
if (!m_query->exec(m_bEmitMessageOnError))
|
|
{
|
|
m_lastError = m_query->lastError();
|
|
return false;
|
|
}
|
|
|
|
Database::AesMineAreaSegment data;
|
|
QVector<Database::AesMineAreaSegment> vecMineArea;
|
|
while (m_query->next())
|
|
{
|
|
int i = 0;
|
|
data.id = m_query->value(i++).toInt();
|
|
data.mineAreaId = m_query->value(i++).toInt();
|
|
data.elementNumber = m_query->value(i++).toInt();
|
|
data.startPosition.rx() = m_query->value(i++).toDouble();
|
|
data.startPosition.ry() = m_query->value(i++).toDouble();
|
|
|
|
double rotationAngle = m_query->value(i++).toDouble();
|
|
int mineCount = m_query->value(i++).toInt();
|
|
|
|
LtsCoordTrans::UtmCoordinate startPositionUtm = LtsCoordTrans::UtmCoordinate::fromGeographic(data.startPosition);
|
|
|
|
QLineF line;
|
|
line.setP1({startPositionUtm.x, startPositionUtm.y});
|
|
line.setLength(mineCount / densityMap.value(data.mineAreaId));
|
|
line.setAngle(rotationAngle);
|
|
QPointF p2 = line.p2();
|
|
|
|
LtsCoordTrans::UtmCoordinate endPositionUtm = startPositionUtm;
|
|
endPositionUtm.x = p2.x();
|
|
endPositionUtm.y = p2.y();
|
|
|
|
data.endPosition = endPositionUtm.geographic();
|
|
|
|
DEBUG_LOG(m_logger) << "aesId: " << data.mineAreaId << ", id: " << data.id;
|
|
DEBUG_LOG(m_logger) << "EndpositionUtm: x: " << endPositionUtm.x << ", y: " << endPositionUtm.y;
|
|
DEBUG_LOG(m_logger) << "Geographic: x: " << data.endPosition.x() << " y: " << data.endPosition.y();
|
|
|
|
vecMineArea.append(data);
|
|
}
|
|
|
|
const QString stmt3(QStringLiteral("UPDATE ") + tableName +
|
|
QStringLiteral(" SET"
|
|
" end_position_x = :end_position_x, end_position_y = :end_position_y"
|
|
" WHERE id = :id"));
|
|
|
|
if (!m_query->prepare(stmt3))
|
|
{
|
|
m_lastError = m_query->lastError();
|
|
ERROR_LOG(m_logger) << "lastError: " << m_lastError.text();
|
|
return false;
|
|
}
|
|
for ( const Database::AesMineAreaSegment &mineAreaSegment : vecMineArea)
|
|
{
|
|
m_query->bindValue(QStringLiteral(":end_position_x"), mineAreaSegment.endPosition.x());
|
|
m_query->bindValue(QStringLiteral(":end_position_y"), mineAreaSegment.endPosition.y());
|
|
m_query->bindValue(QStringLiteral(":id"), mineAreaSegment.id);
|
|
|
|
if (!m_query->exec(m_bEmitMessageOnError))
|
|
{
|
|
m_lastError = m_query->lastError();
|
|
ERROR_LOG(m_logger) << "lastError: " << m_lastError.text();
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
} // namespace AreaEffectSimulation
|
|
|
|
// ------------------------------------------------------------------
|
|
class CmdLineParser : public LtsCommandLineParser::LtsCommandLineParser
|
|
{
|
|
public:
|
|
CmdLineParser()
|
|
: LtsCommandLineParser()
|
|
{
|
|
}
|
|
|
|
int exerciseId() const { return isSet(QLatin1String("e")) ? value(QLatin1String("e")).toInt() : -1; }
|
|
};
|
|
|
|
// ------------------------------------------------------------------
|
|
int main(int argc, char **argv)
|
|
{
|
|
|
|
NGLCoreApplication app(argc, argv);
|
|
|
|
app.createLogger("ConvertMineArea", 0 );
|
|
|
|
CmdLineParser options;
|
|
if (!options.init())
|
|
return -1;
|
|
|
|
AreaEffectSimulation::ConvertMineArea convMa(nullptr);
|
|
|
|
if (!convMa.init(options.exerciseId()))
|
|
{
|
|
ERROR_LOG(app.baseLogger()) << "Initialisation failed.";
|
|
return -2;
|
|
}
|
|
|
|
if (convMa.tryConvertMineArea())
|
|
ERROR_LOG(app.baseLogger()) << "Conversion for Exercise " << options.exerciseId() << " successful!";
|
|
else
|
|
ERROR_LOG(app.baseLogger()) << "Conversion failed.";
|
|
}
|
|
|