Skip to content

Commit

Permalink
fix aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Jan 31, 2025
1 parent 8632afd commit 6269e24
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 93 deletions.
41 changes: 21 additions & 20 deletions dbMeteoPoints/dbAggregationsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,21 @@ Crit3DAggregationsDbHandler::~Crit3DAggregationsDbHandler()
}


bool Crit3DAggregationsDbHandler::saveAggregationData(int nZones, QString aggrType, QString periodType, QDate startDate, QDate endDate, meteoVariable variable,
std::vector< std::vector<float> > aggregatedValues)
bool Crit3DAggregationsDbHandler::saveAggregationData(const std::vector<int> &idZoneVector, const QString &aggrType,
const QString &periodType, const QDate &startDate,
const QDate &endDate, meteoVariable variable,
const std::vector<std::vector<float> > &aggregatedValues)
{
initAggregatedTables(nZones, aggrType, periodType, startDate, endDate, variable);
initAggregatedTables(idZoneVector, aggrType, periodType, startDate, endDate, variable);
createTmpAggrTable();

int idVariable = getIdfromMeteoVar(variable);
long nrDays = long(startDate.daysTo(endDate)) + 1;
QSqlQuery qry(_db);

// LC warning: the zones start from 1
for (unsigned int zone = 1; zone <= unsigned(nZones); zone++)
for (unsigned int i = 0; i < idZoneVector.size(); i++)
{
QString queryString = QString("REPLACE INTO `%1_%2_%3` VALUES").arg(QString::number(zone), aggrType, periodType);
QString queryString = QString("REPLACE INTO `%1_%2_%3` VALUES").arg(QString::number(idZoneVector[i]), aggrType, periodType);

if (periodType == "H")
{
Expand All @@ -60,9 +61,9 @@ bool Crit3DAggregationsDbHandler::saveAggregationData(int nZones, QString aggrTy
for (int hour = 0; hour < 24; hour++)
{
QString valueString = "NULL";
if (! isEqual(aggregatedValues[day*24 + hour][zone-1], NODATA))
if (! isEqual(aggregatedValues[day*24 + hour][i], NODATA))
{
valueString = QString::number(double(aggregatedValues[day*24 + hour][zone-1]), 'f', 1);
valueString = QString::number(double(aggregatedValues[day*24 + hour][i]), 'f', 1);
}

// the data refers to the past hour
Expand Down Expand Up @@ -91,9 +92,9 @@ bool Crit3DAggregationsDbHandler::saveAggregationData(int nZones, QString aggrTy
for (unsigned int day = 0; day < unsigned(nrDays); day++)
{
QString valueString = "NULL";
if (! isEqual(aggregatedValues[day][zone-1], NODATA))
if (! isEqual(aggregatedValues[day][i], NODATA))
{
valueString = QString::number(double(aggregatedValues[day][zone-1]), 'f', 1);
valueString = QString::number(double(aggregatedValues[day][i]), 'f', 1);
}

QString dateString = startDate.addDays(day).toString("yyyy-MM-dd");
Expand Down Expand Up @@ -213,14 +214,14 @@ bool Crit3DAggregationsDbHandler::getAggregationZonesReference(QString name, QSt
}


void Crit3DAggregationsDbHandler::initAggregatedTables(int numZones, QString aggrType, QString periodType, QDate startDate, QDate endDate, meteoVariable variable)
void Crit3DAggregationsDbHandler::initAggregatedTables(const std::vector<int> &idZoneVector, const QString &aggrType, const QString &periodType, const QDate &startDate, const QDate &endDate, meteoVariable variable)
{
int idVariable = getIdfromMeteoVar(variable);
for (int i = 1; i <= numZones; i++)
for (int i = 0; i < idZoneVector.size(); i++)
{
QString statement = QString("CREATE TABLE IF NOT EXISTS `%1_%2_%3` "
"(date_time TEXT, id_variable INTEGER, value REAL, PRIMARY KEY(date_time,id_variable))")
.arg(i).arg(aggrType, periodType);
.arg(idZoneVector[i]).arg(aggrType, periodType);

QSqlQuery qry(statement, _db);
if(! qry.exec() )
Expand All @@ -230,7 +231,7 @@ void Crit3DAggregationsDbHandler::initAggregatedTables(int numZones, QString agg

statement = QString("DELETE FROM `%1_%2_%3` WHERE date_time >= DATE('%4') "
"AND date_time < DATE('%5', '+1 day') AND id_variable = %6")
.arg(i).arg(aggrType, periodType, startDate.toString("yyyy-MM-dd"), endDate.toString("yyyy-MM-dd")).arg(idVariable);
.arg(idZoneVector[i]).arg(aggrType, periodType, startDate.toString("yyyy-MM-dd"), endDate.toString("yyyy-MM-dd")).arg(idVariable);

qry = QSqlQuery(statement, _db);
if(! qry.exec() )
Expand All @@ -253,18 +254,18 @@ bool Crit3DAggregationsDbHandler::existIdPoint(const QString& idPoint)
}


bool Crit3DAggregationsDbHandler::writeAggregationPointProperties(int nrPoints, QString aggrType,
std::vector <double> lonVector, std::vector <double> latVector)
bool Crit3DAggregationsDbHandler::writeAggregationPointProperties(const QString &aggrType, const std::vector<int> &idZoneVector,
const std::vector<double> &lonVector, const std::vector<double> &latVector)
{
if (! _db.tables().contains(QLatin1String("point_properties")) )
{
return false;
}

QSqlQuery qry(_db);
for (int i = 1; i <= nrPoints; i++)
for (int i = 0; i < idZoneVector.size(); i++)
{
QString id = QString::number(i) + "_" + aggrType;
QString id = QString::number(idZoneVector[i]) + "_" + aggrType;
QString name = id;

if (! existIdPoint(id))
Expand All @@ -274,8 +275,8 @@ bool Crit3DAggregationsDbHandler::writeAggregationPointProperties(int nrPoints,

qry.bindValue(":id_point", id);
qry.bindValue(":name", name);
qry.bindValue(":latitude", latVector[i-1]);
qry.bindValue(":longitude", lonVector[i-1]);
qry.bindValue(":latitude", latVector[i]);
qry.bindValue(":longitude", lonVector[i]);
qry.bindValue(":altitude", 0);
qry.bindValue(":is_active", 1);

Expand Down
11 changes: 7 additions & 4 deletions dbMeteoPoints/dbAggregationsHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@
bool existIdPoint(const QString& idPoint);
bool writeAggregationZonesTable(QString name, QString filename, QString field);
bool getAggregationZonesReference(QString name, QString* filename, QString* field);
void initAggregatedTables(int numZones, QString aggrType, QString periodType, QDate startDate, QDate endDate, meteoVariable variable);
void initAggregatedTables(const std::vector<int> &idZoneVector, const QString &aggrType, const QString &periodType,
const QDate &startDate, const QDate &endDate, meteoVariable variable);

bool writeAggregationPointProperties(int nrPoints, QString aggrType, std::vector <double> lonVector, std::vector <double> latVector);
bool writeAggregationPointProperties(const QString &aggrType, const std::vector<int> &idZoneVector,
const std::vector<double> &lonVector, const std::vector<double> &latVector);

bool saveAggregationData(int nZones, QString aggrType, QString periodType, QDate startDate, QDate endDate,
meteoVariable variable, std::vector< std::vector<float> > aggregatedValues);
bool saveAggregationData(const std::vector<int> &idZoneVector, const QString &aggrType,
const QString &periodType, const QDate &startDate, const QDate &endDate,
meteoVariable variable, const std::vector<std::vector<float>> &aggregatedValues);

bool insertTmpAggr(QDate startDate, QDate endDate, meteoVariable variable, std::vector< std::vector<float> > aggregatedValues, int nZones);
bool saveTmpAggrData(QString aggrType, QString periodType, int nZones);
Expand Down
11 changes: 7 additions & 4 deletions gis/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ bool Crit3DColorScale::classify()
nrIntervals = MAXVALUE(_nrKeyColors -1, 1);
nrStep = _nrColors / nrIntervals;

_nrColors = nrStep * nrIntervals;
color.resize(_nrColors);

for (i = 0; i < nrIntervals; i++)
{
dRed = float(keyColor[i+1].red - keyColor[i].red) / float(nrStep);
Expand All @@ -114,7 +111,13 @@ bool Crit3DColorScale::classify()
color[n].blue = keyColor[i].blue + short(dBlue * float(j));
}
}
color[_nrColors-1] = keyColor[_nrKeyColors -1];

// last colors
int lastIndex = nrStep * nrIntervals;
for (i = lastIndex; i < _nrColors; i++)
{
color[i] = keyColor[_nrKeyColors -1];
}
}

return true;
Expand Down
8 changes: 4 additions & 4 deletions graphics/colorLegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ void ColorLegend::paintEvent(QPaintEvent *event)
int legendWidth = painter.window().width() - BLANK_DX*2;

unsigned int nrStep = this->colorScale->nrColors();
unsigned int nrStepText = MAXVALUE(nrStep / 4, 1);
unsigned int nrStepText = MAXVALUE(round(float(nrStep) / 4.f), 1);

double dx = double(legendWidth) / double(nrStep+1);

double value = this->colorScale->minimum();
double step = (colorScale->maximum() - colorScale->minimum()) / double(nrStep);
double stepText = (colorScale->maximum() - colorScale->minimum()) / double(nrStepText);
double range = (colorScale->maximum() - colorScale->minimum());

QString valueStr;
int nrDigits;
Expand All @@ -71,15 +71,15 @@ void ColorLegend::paintEvent(QPaintEvent *event)
if (value < 0) nrDigits++;

double decimal = fabs(value - round(value));
if ((decimal / stepText) > 1)
if ((decimal / range) > 0.1)
{
// two decimals
valueStr = QString::number(value, 'f', 2);
nrDigits += 2;
}
else
{
if ((decimal / stepText) > 0.1)
if ((decimal / range) > 0.01)
{
// one decimal
valueStr = QString::number(value, 'f', 1);
Expand Down
Loading

0 comments on commit 6269e24

Please sign in to comment.