Skip to content

Commit

Permalink
Merge commit 'dd0a2616fc29ad1842649d5f083d23f4129909db'
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Aug 9, 2024
2 parents 759aedb + dd0a261 commit 9104935
Show file tree
Hide file tree
Showing 13 changed files with 258 additions and 132 deletions.
2 changes: 1 addition & 1 deletion agrolib/climate/climate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ float thomDailyMean(TObsDataH* hourlyValues, float minimumPercentage)
if ( (float(nData) / 24 * 100) < minimumPercentage)
thomDailyMean = NODATA;
else
thomDailyMean = statistics::mean(thomValues, nData);
thomDailyMean = statistics::mean(thomValues);


return thomDailyMean;
Expand Down
69 changes: 29 additions & 40 deletions agrolib/dbMeteoPoints/dbAggregationsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,11 @@ Crit3DAggregationsDbHandler::~Crit3DAggregationsDbHandler()
}
}

QSqlDatabase Crit3DAggregationsDbHandler::db() const
{
return _db;
}


QString Crit3DAggregationsDbHandler::error() const
{
return _error;
}

std::map<int, meteoVariable> Crit3DAggregationsDbHandler::mapIdMeteoVar() const
{
return _mapIdMeteoVar;
}


bool Crit3DAggregationsDbHandler::saveAggrData(int nZones, QString aggrType, QString periodType, QDate startDate, QDate endDate, meteoVariable variable,
std::vector< std::vector<float> > aggregatedValues, std::vector <double> lonVector, std::vector <double> latVector)
std::vector< std::vector<float> > aggregatedValues)
{
initAggregatedTables(nZones, aggrType, periodType, startDate, endDate, variable);
writePointProperties(nZones, aggrType, lonVector, latVector);

createTmpAggrTable();

Expand Down Expand Up @@ -102,7 +85,6 @@ bool Crit3DAggregationsDbHandler::saveAggrData(int nZones, QString aggrType, QSt

bool Crit3DAggregationsDbHandler::writeAggregationZonesTable(QString name, QString filename, QString field)
{

QSqlQuery qry(_db);

qry.prepare( "INSERT INTO aggregation_zones (name, filename, shape_field)"
Expand All @@ -117,29 +99,28 @@ bool Crit3DAggregationsDbHandler::writeAggregationZonesTable(QString name, QStri
_error = qry.lastError().text();
return false;
}
else
return true;

return true;
}


bool Crit3DAggregationsDbHandler::writeRasterName(QString rasterName)
{
QSqlQuery qry(_db);

qry.prepare( "INSERT INTO zones (name)"
" VALUES (:name)" );

qry.prepare( "INSERT INTO zones (name) VALUES (:name)" );
qry.bindValue(":name", rasterName);

if( !qry.exec() )
if(! qry.exec() )
{
_error = qry.lastError().text();
return false;
}
else
return true;

return true;
}


bool Crit3DAggregationsDbHandler::getRasterName(QString* rasterName)
{
QSqlQuery qry(_db);
Expand All @@ -166,6 +147,7 @@ bool Crit3DAggregationsDbHandler::getRasterName(QString* rasterName)
}
}


bool Crit3DAggregationsDbHandler::getAggregationZonesReference(QString name, QString* filename, QString* field)
{

Expand Down Expand Up @@ -195,30 +177,32 @@ bool Crit3DAggregationsDbHandler::getAggregationZonesReference(QString name, QSt
}
}


void Crit3DAggregationsDbHandler::initAggregatedTables(int numZones, QString aggrType, QString periodType, QDate startDate, QDate endDate, meteoVariable variable)
{

int idVariable = getIdfromMeteoVar(variable);
for (int i = 1; i <= numZones; 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).arg(periodType);
"(date_time TEXT, id_variable INTEGER, value REAL, PRIMARY KEY(date_time,id_variable))")
.arg(i).arg(aggrType, periodType);

QSqlQuery qry(statement, _db);
if( !qry.exec() )
{
_error = qry.lastError().text();
}
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).arg(periodType).arg(startDate.toString("yyyy-MM-dd")).arg(endDate.toString("yyyy-MM-dd")).arg(idVariable);

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);

qry = QSqlQuery(statement, _db);
if( !qry.exec() )
if(! qry.exec() )
{
_error = qry.lastError().text();
}
}

}


Expand All @@ -234,15 +218,16 @@ bool Crit3DAggregationsDbHandler::existIdPoint(const QString& idPoint)
}


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

QSqlQuery qry(_db);
for (int i = 1; i <= numZones; i++)
for (int i = 1; i <= nrPoints; i++)
{
QString id = QString::number(i) + "_" + aggrType;
QString name = id;
Expand All @@ -259,7 +244,7 @@ bool Crit3DAggregationsDbHandler::writePointProperties(int numZones, QString agg
qry.bindValue(":altitude", 0);
qry.bindValue(":is_active", 1);

if( !qry.exec() )
if(! qry.exec() )
{
_error = qry.lastError().text();
return false;
Expand All @@ -270,6 +255,7 @@ bool Crit3DAggregationsDbHandler::writePointProperties(int numZones, QString agg
return true;
}


void Crit3DAggregationsDbHandler::createTmpAggrTable()
{
this->deleteTmpAggrTable();
Expand Down Expand Up @@ -389,16 +375,16 @@ int Crit3DAggregationsDbHandler::getIdfromMeteoVar(meteoVariable meteoVar)
return key;
}


QList<QString> Crit3DAggregationsDbHandler::getAggregations()
{

QSqlQuery qry(_db);

qry.prepare( "SELECT * FROM aggregations");
QString aggregation;
QList<QString> aggregationList;

if( !qry.exec() )
if(! qry.exec() )
{
_error = qry.lastError().text();
}
Expand All @@ -410,13 +396,16 @@ QList<QString> Crit3DAggregationsDbHandler::getAggregations()
aggregationList.append(aggregation);
}
}

if (aggregationList.isEmpty())
{
_error = "name not found";
_error = "aggregation table is empty.";
}

return aggregationList;
}


bool Crit3DAggregationsDbHandler::renameColumn(QString oldColumn, QString newColumn)
{
QSqlQuery qry(_db);
Expand Down
26 changes: 19 additions & 7 deletions agrolib/dbMeteoPoints/dbAggregationsHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,33 @@
public:
Crit3DAggregationsDbHandler(QString dbname);
~Crit3DAggregationsDbHandler();
QSqlDatabase db() const;
QString error() const;

QString error() const { return _error; }
QString name() const { return _db.databaseName(); }
QSqlDatabase db() const { return _db; }

std::map<int, meteoVariable> mapIdMeteoVar() const { return _mapIdMeteoVar; }

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);
bool writePointProperties(int numZones, QString aggrType, std::vector <double> lonVector, std::vector <double> latVector);
bool saveAggrData(int nZones, QString aggrType, QString periodType, QDate startDate, QDate endDate, meteoVariable variable, std::vector< std::vector<float> > aggregatedValues, std::vector<double> lonVector, std::vector<double> latVector);
void createTmpAggrTable();
void deleteTmpAggrTable();

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

bool saveAggrData(int nZones, QString aggrType, QString periodType, QDate startDate, QDate endDate,
meteoVariable variable, 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);

std::vector<float> getAggrData(QString aggrType, QString periodType, int zone, QDate startDate, QDate endDate, meteoVariable variable);
std::map<int, meteoVariable> mapIdMeteoVar() const;

bool loadVariableProperties();
int getIdfromMeteoVar(meteoVariable meteoVar);

QList<QString> getAggregations();

bool writeRasterName(QString rasterName);
bool getRasterName(QString* rasterName);
bool renameColumn(QString oldColumn, QString newColumn);
Expand All @@ -47,6 +56,9 @@
QSqlDatabase _db;
std::map<int, meteoVariable> _mapIdMeteoVar;
QString _error;

void createTmpAggrTable();
void deleteTmpAggrTable();
};

#endif // DBAGGREGATIONSHANDLER_H
Expand Down
63 changes: 40 additions & 23 deletions agrolib/gis/gis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1601,17 +1601,17 @@ namespace gis
isEqual(first.header->llCorner.y, second.header->llCorner.y));
}


void resampleGrid(const gis::Crit3DRasterGrid& oldGrid, gis::Crit3DRasterGrid* newGrid,
gis::Crit3DRasterHeader* header, aggregationMethod elab, float nodataThreshold)
gis::Crit3DRasterHeader* newHeader, aggregationMethod elab, float nodataRatioThreshold)
{
*(newGrid->header) = *header;
*(newGrid->header) = *newHeader;

double resampleFactor = newGrid->header->cellSize / oldGrid.header->cellSize;

double factor = newGrid->header->cellSize / oldGrid.header->cellSize;
int row, col, tmpRow, tmpCol, nrValues, maxValues;
float value, tmpValue;
double x, y;
gis::Crit3DPoint myLL, myUR;
std::vector <float> values;
std::vector<float> values;

newGrid->initializeGrid();

Expand All @@ -1620,42 +1620,56 @@ namespace gis
{
newGrid->value[row][col] = newGrid->header->flag;

value = NODATA;

if (factor < 1 || elab == aggrCenter)
float value = NODATA;
if (resampleFactor < 1. || elab == aggrCenter)
{
double x, y;
newGrid->getXY(row, col, x, y);
oldGrid.getRowCol(x, y, tmpRow, tmpCol);
if (! gis::isOutOfGridRowCol(tmpRow, tmpCol, oldGrid))
{
value = oldGrid.value[tmpRow][tmpCol];
}
}
else
{
newGrid->getXY(row, col, x, y);
myLL.utm.x = x - (newGrid->header->cellSize / 2);
myLL.utm.y = y - (newGrid->header->cellSize / 2);
myUR.utm.x = x + (newGrid->header->cellSize / 2);
myUR.utm.y = y + (newGrid->header->cellSize / 2);
double x0, y0;
newGrid->getXY(row, col, x0, y0);
myLL.utm.x = x0 - (newGrid->header->cellSize / 2);
myLL.utm.y = y0 - (newGrid->header->cellSize / 2);
myUR.utm.x = x0 + (newGrid->header->cellSize / 2);
myUR.utm.y = y0 + (newGrid->header->cellSize / 2);

double step = oldGrid.header->cellSize * 0.5;

values.clear();
maxValues = 0;
double step = oldGrid.header->cellSize * 0.5;
for (x = myLL.utm.x; x <= myUR.utm.x; x += step)
for (y = myLL.utm.y; y <= myUR.utm.y; y += step)

double x = myLL.utm.x;
while(x <= myUR.utm.x)
{
double y = myLL.utm.y;
while(y <= myUR.utm.y)
{
maxValues++;
tmpValue = gis::getValueFromXY(oldGrid, x, y);
float tmpValue = gis::getValueFromXY(oldGrid, x, y);
if (! isEqual(tmpValue, oldGrid.header->flag))
{
values.push_back(tmpValue);
}
}

y += step;
}
x += step;
}
nrValues = int(values.size());

if (maxValues > 0)
{
if ((float(nrValues) / float(maxValues)) > nodataThreshold)
if ((float(nrValues) / float(maxValues)) > nodataRatioThreshold)
{
if (elab == aggrAverage)
value = statistics::mean(values, nrValues);
value = statistics::mean(values);
else if (elab == aggrMedian)
value = sorting::percentile(values, nrValues, 50, true);
else if (elab == aggrPrevailing)
Expand All @@ -1664,14 +1678,17 @@ namespace gis
}
}

if (! isEqual(value, NODATA)) newGrid->value[row][col] = value;
if (! isEqual(value, NODATA))
{
newGrid->value[row][col] = value;
}
}

gis::updateMinMaxRasterGrid(newGrid);
newGrid->isLoaded = true;

}


bool temporalYearlyInterpolation(const gis::Crit3DRasterGrid& firstGrid, const gis::Crit3DRasterGrid& secondGrid,
int myYear, float minValue, float maxValue, gis::Crit3DRasterGrid* outGrid)
{
Expand Down
2 changes: 1 addition & 1 deletion agrolib/gis/gis.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@
float closestDistanceFromGrid(Crit3DPoint myPoint, const gis::Crit3DRasterGrid& dem);
bool compareGrids(const gis::Crit3DRasterGrid& first, const gis::Crit3DRasterGrid& second);
void resampleGrid(const gis::Crit3DRasterGrid& oldGrid, gis::Crit3DRasterGrid* newGrid,
Crit3DRasterHeader* header, aggregationMethod elab, float nodataThreshold);
Crit3DRasterHeader* newHeader, aggregationMethod elab, float nodataRatioThreshold);
bool temporalYearlyInterpolation(const gis::Crit3DRasterGrid& firstGrid, const gis::Crit3DRasterGrid& secondGrid,
int myYear, float minValue, float maxValue, gis::Crit3DRasterGrid* outGrid);
}
Expand Down
Loading

0 comments on commit 9104935

Please sign in to comment.