From c9fa07c26d2c8d3f90b680a204a973276e39a7c9 Mon Sep 17 00:00:00 2001 From: ftomei Date: Wed, 21 Aug 2024 13:04:00 +0200 Subject: [PATCH] update code --- crit3dDate/crit3dDate.cpp | 21 +---- crit3dDate/crit3dDate.h | 11 +-- crit3dDate/crit3dDate.pro | 1 - crit3dDate/crit3dTime.cpp | 4 +- crop/crop.cpp | 2 +- dbMeteoPoints/dbMeteoPointsHandler.cpp | 119 +++++++++++++------------ dbMeteoPoints/dbMeteoPointsHandler.h | 12 +-- meteo/meteoPoint.cpp | 2 +- project/project.cpp | 20 ++--- waterTable/waterTable.h | 2 +- 10 files changed, 90 insertions(+), 104 deletions(-) diff --git a/crit3dDate/crit3dDate.cpp b/crit3dDate/crit3dDate.cpp index 09b83f14e..6826f41d0 100644 --- a/crit3dDate/crit3dDate.cpp +++ b/crit3dDate/crit3dDate.cpp @@ -303,7 +303,7 @@ Crit3DDate getDateFromJulianDay(long julianDay) } -std::string Crit3DDate::toStdString() +std::string Crit3DDate::toISOString() const { char myStr[11]; sprintf (myStr, "%d-%02d-%02d", this->year, this->month, this->day); @@ -312,24 +312,6 @@ std::string Crit3DDate::toStdString() } -std::string Crit3DDate::toStdString() const -{ - char myStr[11]; - sprintf (myStr, "%d-%02d-%02d", this->year, this->month, this->day); - - return std::string(myStr); -} - - -std::string Crit3DDate::toString() -{ - char myStr[9]; - sprintf (myStr, "%d%02d%02d", this->year, this->month, this->day); - - return std::string(myStr); -} - - std::string Crit3DDate::toString() const { char myStr[9]; @@ -337,4 +319,3 @@ std::string Crit3DDate::toString() const return std::string(myStr); } - diff --git a/crit3dDate/crit3dDate.h b/crit3dDate/crit3dDate.h index d4c8d39e2..b8b91344a 100644 --- a/crit3dDate/crit3dDate.h +++ b/crit3dDate/crit3dDate.h @@ -45,11 +45,12 @@ void setDate(int myDay, int myMonth, int myYear); bool isNullDate(); void setNullDate(); + Crit3DDate addDays(long offset) const; int daysTo(const Crit3DDate& myDate) const; - std::string toStdString(); - std::string toStdString() const; - std::string toString(); + + std::string toISOString() const; + std::string toString() const; }; @@ -81,8 +82,8 @@ int getMinutes() const; int getSeconds() const; - std::string toISOString(); - std::string toString(); + std::string toISOString() const; + std::string toString() const; }; bool isLeapYear(int year); diff --git a/crit3dDate/crit3dDate.pro b/crit3dDate/crit3dDate.pro index 39b3ba1f9..1e040b726 100644 --- a/crit3dDate/crit3dDate.pro +++ b/crit3dDate/crit3dDate.pro @@ -13,7 +13,6 @@ CONFIG += staticlib DEFINES += _CRT_SECURE_NO_WARNINGS CONFIG += debug_and_release -CONFIG += c++11 c++14 c++17 unix:{ CONFIG(debug, debug|release) { diff --git a/crit3dDate/crit3dTime.cpp b/crit3dDate/crit3dTime.cpp index b5aba8780..f298c05d1 100644 --- a/crit3dDate/crit3dTime.cpp +++ b/crit3dDate/crit3dTime.cpp @@ -135,7 +135,7 @@ Crit3DTime Crit3DTime::addSeconds(long mySeconds) const } -std::string Crit3DTime::toISOString() +std::string Crit3DTime::toISOString() const { char myStr[17]; sprintf (myStr, "%d-%02d-%02d %02d:%02d", this->date.year, this->date.month, this->date.day, this->getHour(), this->getMinutes()); @@ -144,7 +144,7 @@ std::string Crit3DTime::toISOString() } -std::string Crit3DTime::toString() +std::string Crit3DTime::toString() const { char myStr[13]; sprintf (myStr, "%d%02d%02dT%02d%02d", this->date.year, this->date.month, this->date.day, this->getHour(), this->getMinutes()); diff --git a/crop/crop.cpp b/crop/crop.cpp index 5bdd9fb43..a189f5733 100644 --- a/crop/crop.cpp +++ b/crop/crop.cpp @@ -470,7 +470,7 @@ bool Crit3DCrop::dailyUpdate(const Crit3DDate &myDate, double latitude, const st double dailyDD = getDailyDegreeIncrease(tmin, tmax, currentDoy); if (isEqual(dailyDD, NODATA)) { - myError = "Error in computing degree days for " + myDate.toStdString(); + myError = "Error in computing degree days for " + myDate.toISOString(); return false; } degreeDays += dailyDD; diff --git a/dbMeteoPoints/dbMeteoPointsHandler.cpp b/dbMeteoPoints/dbMeteoPointsHandler.cpp index 2c5dfc0d3..3c5a4cabd 100644 --- a/dbMeteoPoints/dbMeteoPointsHandler.cpp +++ b/dbMeteoPoints/dbMeteoPointsHandler.cpp @@ -391,12 +391,12 @@ QDateTime Crit3DMeteoPointsDbHandler::getLastDate(frequencyType frequency, std:: bool Crit3DMeteoPointsDbHandler::existData(Crit3DMeteoPoint *meteoPoint, frequencyType myFreq) { - QSqlQuery myQuery(_db); + QSqlQuery query(_db); QString tableName = QString::fromStdString(meteoPoint->id) + ((myFreq == daily) ? "_D" : "_H"); QString statement = QString( "SELECT 1 FROM `%1`").arg(tableName); - if (myQuery.exec(statement)) - if (myQuery.next()) + if (query.exec(statement)) + if (query.next()) return true; return false; @@ -517,7 +517,7 @@ bool Crit3DMeteoPointsDbHandler::deleteAllPointsFromDataset(QList datas } -bool Crit3DMeteoPointsDbHandler::loadDailyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint *meteoPoint) +bool Crit3DMeteoPointsDbHandler::loadDailyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint &meteoPoint) { // check dates if (firstDate > lastDate) @@ -527,11 +527,11 @@ bool Crit3DMeteoPointsDbHandler::loadDailyData(const Crit3DDate &firstDate, cons } int numberOfDays = difference(firstDate, lastDate) + 1; - meteoPoint->initializeObsDataD(numberOfDays, firstDate); + meteoPoint.initializeObsDataD(numberOfDays, firstDate); - QString firstDateStr = QString::fromStdString(firstDate.toStdString()); - QString lastDateStr = QString::fromStdString(lastDate.toStdString()); - QString tableName = QString::fromStdString(meteoPoint->id) + "_D"; + QString firstDateStr = QString::fromStdString(firstDate.toISOString()); + QString lastDateStr = QString::fromStdString(lastDate.toISOString()); + QString tableName = QString::fromStdString(meteoPoint.id) + "_D"; QString statement; if (numberOfDays == 1) @@ -544,31 +544,34 @@ bool Crit3DMeteoPointsDbHandler::loadDailyData(const Crit3DDate &firstDate, cons .arg(tableName, firstDateStr, lastDateStr); } - QSqlQuery myQuery(_db); - if( myQuery.exec(statement) ) + QSqlQuery query(_db); + if(! query.exec(statement)) { - while (myQuery.next()) - { - QString dateStr = myQuery.value(0).toString(); - QDate d = QDate::fromString(dateStr, "yyyy-MM-dd"); + return false; + errorStr = query.lastError().text(); + } - int idVar = myQuery.value(1).toInt(); - meteoVariable variable = _mapIdMeteoVar.at(idVar); + while (query.next()) + { + // date + QString dateStr = query.value(0).toString(); + QDate d = QDate::fromString(dateStr, "yyyy-MM-dd"); - float value = myQuery.value(2).toFloat(); + // variable + int idVar = query.value(1).toInt(); + meteoVariable variable = _mapIdMeteoVar.at(idVar); - meteoPoint->setMeteoPointValueD(Crit3DDate(d.day(), d.month(), d.year()), variable, value); - } - return true; - } - else - { - return false; + // value + float value = query.value(2).toFloat(); + + meteoPoint.setMeteoPointValueD(Crit3DDate(d.day(), d.month(), d.year()), variable, value); } + + return true; } -bool Crit3DMeteoPointsDbHandler::loadHourlyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint *meteoPoint) +bool Crit3DMeteoPointsDbHandler::loadHourlyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint &meteoPoint) { // check dates if (firstDate > lastDate) @@ -580,11 +583,11 @@ bool Crit3DMeteoPointsDbHandler::loadHourlyData(const Crit3DDate &firstDate, con // initialize obs data int numberOfDays = difference(firstDate, lastDate) + 1; int myHourlyFraction = 1; - meteoPoint->initializeObsDataH(myHourlyFraction, numberOfDays, firstDate); + meteoPoint.initializeObsDataH(myHourlyFraction, numberOfDays, firstDate); - QString startDateStr = QString::fromStdString(firstDate.toStdString()); - QString endDateStr = QString::fromStdString(lastDate.toStdString()); - QString tableName = QString::fromStdString(meteoPoint->id) + "_H"; + QString startDateStr = QString::fromStdString(firstDate.toISOString()); + QString endDateStr = QString::fromStdString(lastDate.toISOString()); + QString tableName = QString::fromStdString(meteoPoint.id) + "_H"; QString statement = QString( "SELECT * FROM `%1` WHERE date_time >= DATETIME('%2 01:00:00') AND date_time <= DATETIME('%3 00:00:00', '+1 day')") .arg(tableName, startDateStr, endDateStr); @@ -615,13 +618,13 @@ bool Crit3DMeteoPointsDbHandler::loadHourlyData(const Crit3DDate &firstDate, con if (variable != noMeteoVar) { float value = qry.value(2).toFloat(); - meteoPoint->setMeteoPointValueH(myDate, d.time().hour(), d.time().minute(), variable, value); + meteoPoint.setMeteoPointValueH(myDate, d.time().hour(), d.time().minute(), variable, value); // copy scalar intensity to vector intensity (instantaneous values are equivalent, following WMO) // should be removed when hourly averages are available if (variable == windScalarIntensity) { - meteoPoint->setMeteoPointValueH(myDate, d.time().hour(), d.time().minute(), windVectorIntensity, value); + meteoPoint.setMeteoPointValueH(myDate, d.time().hour(), d.time().minute(), windVectorIntensity, value); } } } @@ -631,7 +634,8 @@ bool Crit3DMeteoPointsDbHandler::loadHourlyData(const Crit3DDate &firstDate, con } -std::vector Crit3DMeteoPointsDbHandler::loadDailyVar(QString *myError, meteoVariable variable, Crit3DDate dateStart, Crit3DDate dateEnd, QDate* firstDateDB, Crit3DMeteoPoint *meteoPoint) +std::vector Crit3DMeteoPointsDbHandler::loadDailyVar(meteoVariable variable, const Crit3DDate &dateStart, const Crit3DDate &dateEnd, + const Crit3DMeteoPoint &meteoPoint, QDate &firstDateDB) { QString dateStr; QDate d, previousDate; @@ -640,32 +644,32 @@ std::vector Crit3DMeteoPointsDbHandler::loadDailyVar(QString *myError, me bool firstRow = true; int idVar = getIdfromMeteoVar(variable); - QString startDate = QString::fromStdString(dateStart.toStdString()); - QString endDate = QString::fromStdString(dateEnd.toStdString()); + QString startDate = QString::fromStdString(dateStart.toISOString()); + QString endDate = QString::fromStdString(dateEnd.toISOString()); - QSqlQuery myQuery(_db); + QSqlQuery query(_db); - QString tableName = QString::fromStdString(meteoPoint->id) + "_D"; + QString tableName = QString::fromStdString(meteoPoint.id) + "_D"; QString statement = QString( "SELECT * FROM `%1` WHERE `%2` = %3 AND date_time >= DATE('%4') AND date_time < DATE('%5', '+1 day')") - .arg(tableName).arg(FIELD_METEO_VARIABLE).arg(idVar).arg(startDate).arg(endDate); + .arg(tableName, FIELD_METEO_VARIABLE).arg(idVar).arg(startDate, endDate); - if( !myQuery.exec(statement) ) + if(! query.exec(statement)) { - *myError = myQuery.lastError().text(); + errorStr = query.lastError().text(); return dailyVarList; } else { - while (myQuery.next()) + while (query.next()) { if (firstRow) { - dateStr = myQuery.value(0).toString(); - *firstDateDB = QDate::fromString(dateStr, "yyyy-MM-dd"); - previousDate = *firstDateDB; + dateStr = query.value(0).toString(); + firstDateDB = QDate::fromString(dateStr, "yyyy-MM-dd"); + previousDate = firstDateDB; - value = myQuery.value(2).toFloat(); + value = query.value(2).toFloat(); dailyVarList.push_back(value); @@ -673,28 +677,27 @@ std::vector Crit3DMeteoPointsDbHandler::loadDailyVar(QString *myError, me } else { - dateStr = myQuery.value(0).toString(); + dateStr = query.value(0).toString(); d = QDate::fromString(dateStr, "yyyy-MM-dd"); int missingDate = previousDate.daysTo(d); - for (int i =1; i Crit3DMeteoPointsDbHandler::exportAllDataVar(QString *myError, frequencyType freq, meteoVariable variable, QString id, QDateTime myFirstTime, QDateTime myLastTime, std::vector &dateStr) { QString myDateStr; @@ -706,7 +709,7 @@ std::vector Crit3DMeteoPointsDbHandler::exportAllDataVar(QString *myError int idVar = getIdfromMeteoVar(variable); - QSqlQuery myQuery(_db); + QSqlQuery query(_db); QString tableName; QString startDate; QString endDate; @@ -735,18 +738,18 @@ std::vector Crit3DMeteoPointsDbHandler::exportAllDataVar(QString *myError return allDataVarList; } - if( !myQuery.exec(statement) ) + if( !query.exec(statement) ) { - *myError = myQuery.lastError().text(); + *myError = query.lastError().text(); return allDataVarList; } else { - while (myQuery.next()) + while (query.next()) { if (freq == daily) { - if (! getValue(myQuery.value(0), &date)) + if (! getValue(query.value(0), &date)) { *myError = "Missing fieldTime"; return allDataVarList; @@ -755,7 +758,7 @@ std::vector Crit3DMeteoPointsDbHandler::exportAllDataVar(QString *myError } else if (freq == hourly) { - if (! getValue(myQuery.value(0), &dateTime)) + if (! getValue(query.value(0), &dateTime)) { *myError = "Missing fieldTime"; return allDataVarList; @@ -764,7 +767,7 @@ std::vector Crit3DMeteoPointsDbHandler::exportAllDataVar(QString *myError myDateStr = dateTime.date().toString("yyyy-MM-dd") + " " + dateTime.time().toString("hh:mm"); } dateStr.push_back(myDateStr); - value = myQuery.value(2).toFloat(); + value = query.value(2).toFloat(); allDataVarList.push_back(value); } } @@ -783,8 +786,8 @@ std::vector Crit3DMeteoPointsDbHandler::loadHourlyVar(QString *myError, m bool firstRow = true; int idVar = getIdfromMeteoVar(variable); - QString startDate = QString::fromStdString(dateStart.toStdString()); - QString endDate = QString::fromStdString(dateEnd.toStdString()); + QString startDate = QString::fromStdString(dateStart.toISOString()); + QString endDate = QString::fromStdString(dateEnd.toISOString()); QSqlQuery qry(_db); diff --git a/dbMeteoPoints/dbMeteoPointsHandler.h b/dbMeteoPoints/dbMeteoPointsHandler.h index 43c859f82..75985a4df 100644 --- a/dbMeteoPoints/dbMeteoPointsHandler.h +++ b/dbMeteoPoints/dbMeteoPointsHandler.h @@ -64,13 +64,15 @@ const gis::Crit3DGisSettings& gisSettings, QString& errorString); bool getPropertiesGivenId(QString id, Crit3DMeteoPoint* meteoPoint, const gis::Crit3DGisSettings& gisSettings, QString& errorString); - bool loadDailyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint *meteoPoint); - std::vector loadDailyVar(QString *myError, meteoVariable variable, - Crit3DDate dateStart, Crit3DDate dateEnd, - QDate* firstDateDB, Crit3DMeteoPoint *meteoPoint); + + bool loadDailyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint &meteoPoint); + + std::vector loadDailyVar(meteoVariable variable, const Crit3DDate &dateStart, const Crit3DDate &dateEnd, + const Crit3DMeteoPoint &meteoPoint, QDate &firstDateDB); + std::vector exportAllDataVar(QString *myError, frequencyType freq, meteoVariable variable, QString id, QDateTime myFirstTime, QDateTime myLastTime, std::vector &dateStr); - bool loadHourlyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint *meteoPoint); + bool loadHourlyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint &meteoPoint); std::vector loadHourlyVar(QString *myError, meteoVariable variable, Crit3DDate dateStart, Crit3DDate dateEnd, diff --git a/meteo/meteoPoint.cpp b/meteo/meteoPoint.cpp index 9dd857d93..e7bd8305d 100644 --- a/meteo/meteoPoint.cpp +++ b/meteo/meteoPoint.cpp @@ -1329,7 +1329,7 @@ bool Crit3DMeteoPoint::getDailyDataCsv_TPrec(std::string &outStr) for (int i = 0; i < int(obsDataD.size()); i++) { // Date - outStr += obsDataD[i].date.toStdString() + ","; + outStr += obsDataD[i].date.toISOString() + ","; if (obsDataD[i].tMin != NODATA) { diff --git a/project/project.cpp b/project/project.cpp index 266c4f2d4..b4eba4714 100644 --- a/project/project.cpp +++ b/project/project.cpp @@ -1492,10 +1492,10 @@ bool Project::loadMeteoPointsData(const QDate& firstDate, const QDate& lastDate, } if (loadHourly) - if (meteoPointsDbHandler->loadHourlyData(getCrit3DDate(firstDate), getCrit3DDate(lastDate), &(meteoPoints[i]))) isData = true; + if (meteoPointsDbHandler->loadHourlyData(getCrit3DDate(firstDate), getCrit3DDate(lastDate), meteoPoints[i])) isData = true; if (loadDaily) - if (meteoPointsDbHandler->loadDailyData(getCrit3DDate(firstDate), getCrit3DDate(lastDate), &(meteoPoints[i]))) isData = true; + if (meteoPointsDbHandler->loadDailyData(getCrit3DDate(firstDate), getCrit3DDate(lastDate), meteoPoints[i])) isData = true; } if (showInfo) closeProgressBar(); @@ -1532,10 +1532,10 @@ bool Project::loadMeteoPointsData(const QDate &firstDate, const QDate &lastDate, if (meteoPoints[i].dataset == dataset.toStdString()) { if (loadHourly) - if (meteoPointsDbHandler->loadHourlyData(getCrit3DDate(firstDate), getCrit3DDate(lastDate), &(meteoPoints[i]))) isData = true; + if (meteoPointsDbHandler->loadHourlyData(getCrit3DDate(firstDate), getCrit3DDate(lastDate), meteoPoints[i])) isData = true; if (loadDaily) - if (meteoPointsDbHandler->loadDailyData(getCrit3DDate(firstDate), getCrit3DDate(lastDate), &(meteoPoints[i]))) isData = true; + if (meteoPointsDbHandler->loadDailyData(getCrit3DDate(firstDate), getCrit3DDate(lastDate), meteoPoints[i])) isData = true; } } @@ -3388,8 +3388,8 @@ void Project::showMeteoWidgetPoint(std::string idMeteoPoint, std::string namePoi if (isAppend) { - meteoPointsDbHandler->loadDailyData(getCrit3DDate(firstDaily), getCrit3DDate(lastDaily), &mp); - meteoPointsDbHandler->loadHourlyData(getCrit3DDate(firstHourly.date()), getCrit3DDate(lastHourly.date()), &mp); + meteoPointsDbHandler->loadDailyData(getCrit3DDate(firstDaily), getCrit3DDate(lastDaily), mp); + meteoPointsDbHandler->loadHourlyData(getCrit3DDate(firstHourly.date()), getCrit3DDate(lastHourly.date()), mp); meteoWidgetPointList[meteoWidgetPointList.size()-1]->drawMeteoPoint(mp, isAppend); } else @@ -3407,8 +3407,8 @@ void Project::showMeteoWidgetPoint(std::string idMeteoPoint, std::string namePoi meteoWidgetPoint->setMeteoWidgetID(meteoWidgetId); meteoWidgetPointList.append(meteoWidgetPoint); QObject::connect(meteoWidgetPoint, SIGNAL(closeWidgetPoint(int)), this, SLOT(deleteMeteoWidgetPoint(int))); - meteoPointsDbHandler->loadDailyData(getCrit3DDate(firstDaily), getCrit3DDate(lastDaily), &mp); - meteoPointsDbHandler->loadHourlyData(getCrit3DDate(firstHourly.date()), getCrit3DDate(lastHourly.date()), &mp); + meteoPointsDbHandler->loadDailyData(getCrit3DDate(firstDaily), getCrit3DDate(lastDaily), mp); + meteoPointsDbHandler->loadHourlyData(getCrit3DDate(firstHourly.date()), getCrit3DDate(lastHourly.date()), mp); if (hasDailyData) { @@ -4292,7 +4292,7 @@ bool Project::exportMeteoPointsDailyDataCsv(bool isTPrec, QDate firstDate, QDate if (checkId) { // read data - if (! meteoPointsDbHandler->loadDailyData(getCrit3DDate(firstDate), getCrit3DDate(lastDate), &(meteoPoints[i]))) + if (! meteoPointsDbHandler->loadDailyData(getCrit3DDate(firstDate), getCrit3DDate(lastDate), meteoPoints[i])) { errorString = "Error in reading point id: " + id; return false; @@ -4840,7 +4840,7 @@ bool Project::waterTableAssignNearestMeteoPoint(bool isMeteoGridLoaded, double w { if (myDistance < minimumDistance || minimumDistance == NODATA) { - meteoPointsDbHandler->loadDailyData(getCrit3DDate(firstMeteoDate), getCrit3DDate(lastDate), &(meteoPoints[i])); + meteoPointsDbHandler->loadDailyData(getCrit3DDate(firstMeteoDate), getCrit3DDate(lastDate), meteoPoints[i]); if (waterTableAssignMeteoData(&meteoPoints[i], firstMeteoDate)) { minimumDistance = myDistance; diff --git a/waterTable/waterTable.h b/waterTable/waterTable.h index f6c9ece7c..77fdff227 100644 --- a/waterTable/waterTable.h +++ b/waterTable/waterTable.h @@ -12,7 +12,7 @@ #endif #include -#define MAXWELLDISTANCE 5000 // distanza max: 5 km +#define MAXWELLDISTANCE 5000 // maximum distance: 5 km #define WATERTABLE_MAXDELTADAYS 90 class WaterTable