Skip to content

Commit

Permalink
Merge commit '9b0cf7df7fedbeb7d1a3ffd5f9097973a122ef9c'
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed May 20, 2024
2 parents 50ffb64 + 9b0cf7d commit 4332cc3
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 27 deletions.
2 changes: 1 addition & 1 deletion agrolib/interpolation/interpolation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,7 @@ bool multipleDetrending(std::vector <Crit3DInterpolationDataPoint> &myPoints,
//std::vector <std::vector<double>> parameters;
std::vector<std::function<double(double, std::vector<double>&)>> myFunc;

unsigned int nrMaxStep = 25;
unsigned int nrMaxStep = 100;
if (parameters.empty())
nrMaxStep *= 10;

Expand Down
2 changes: 1 addition & 1 deletion agrolib/mathFunctions/furtherMathFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@ namespace interpolation
parameters[i][j] = parametersMin[i][j] + (truncNormal)*(parametersMax[i][j]-parametersMin[i][j]);
}
}
} while( (counter < nrTrials) && (R2 < 0.8) && (fabs(R2Previous[0]-R2Previous[nrMinima-1]) > deltaR2) );
} while( (counter < nrTrials) && !(R2Previous[0] > 0.8 && R2Previous[nrMinima-1] > 0.8) && (fabs(R2Previous[0]-R2Previous[nrMinima-1]) > deltaR2) );

for (i=0;i<nrPredictors;i++)
{
Expand Down
30 changes: 25 additions & 5 deletions agrolib/shapeHandler/shapeHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "shapeHandler.h"
#include "commonConstants.h"
#include <fstream>
#include <algorithm>
#include <string.h>


Expand Down Expand Up @@ -294,19 +295,37 @@ int Crit3DShapeHandler::isDBFRecordDeleted(int record) const

int Crit3DShapeHandler::getFieldPos(std::string fieldName) const
{
// upper case
std::transform(fieldName.begin(), fieldName.end(), fieldName.begin(), ::toupper);

for (int i = 0; i < m_fields; i++)
if (m_fieldsList.at(unsigned(i)) == fieldName)
{
std::string currentField = m_fieldsList.at(unsigned(i));
// upper case
std::transform(currentField.begin(), currentField.end(), currentField.begin(), ::toupper);

if (currentField == fieldName)
return i;
}

return -1;
}


bool Crit3DShapeHandler::existField(std::string fieldName) const
{
// upper case
std::transform(fieldName.begin(), fieldName.end(), fieldName.begin(), ::toupper);

for (int i = 0; i < m_fields; i++)
if (m_fieldsList.at(unsigned(i)) == fieldName)
{
std::string currentField = m_fieldsList.at(unsigned(i));
// upper case
std::transform(currentField.begin(), currentField.end(), currentField.begin(), ::toupper);

if (currentField == fieldName)
return true;
}

return false;
}
Expand All @@ -326,8 +345,8 @@ DBFFieldType Crit3DShapeHandler::getFieldType(std::string fieldName) const
double Crit3DShapeHandler::getNumericValue(int shapeNumber, std::string fieldName)
{
int fieldPos = getFieldPos(fieldName);

if (fieldPos == -1) return NODATA;
if (fieldPos == -1)
return NODATA;

return getNumericValue(shapeNumber, fieldPos);
}
Expand Down Expand Up @@ -365,7 +384,8 @@ double Crit3DShapeHandler::getNumericValue(int shapeNumber, int fieldPos)
std::string Crit3DShapeHandler::getStringValue(int shapeNumber, std::string fieldName)
{
int fieldPos = getFieldPos(fieldName);
if (fieldPos == -1) return "";
if (fieldPos == -1)
return "";

return readStringAttribute(shapeNumber, fieldPos);
}
Expand Down
72 changes: 54 additions & 18 deletions agrolib/shapeUtilities/unitCropMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,15 @@ bool computeUcmPrevailing(Crit3DShapeHandler &shapeUCM, Crit3DShapeHandler &shap

// add ID CASE
shapeUCM.addField("ID_CASE", FTString, 20, 0);
int idCaseIndex = shapeUCM.getFieldPos("ID_CASE");
int idCasePos = shapeUCM.getFieldPos("ID_CASE");

// add ID CROP
bool existIdCrop = shapeUCM.existField("ID_CROP");
if (! existIdCrop) shapeUCM.addField("ID_CROP", FTString, 5, 0);
int idCropIndex = shapeUCM.getFieldPos("ID_CROP");
if (! existIdCrop)
{
shapeUCM.addField("ID_CROP", FTString, 5, 0);
}
int idCropPos = shapeUCM.getFieldPos("ID_CROP");

// read indexes
int nShape = shapeUCM.getShapeCount();
Expand All @@ -111,30 +114,63 @@ bool computeUcmPrevailing(Crit3DShapeHandler &shapeUCM, Crit3DShapeHandler &shap
return false;
}

// add HECTARES
bool existHectares = shapeUCM.existField("HA");
int hectaresPos = shapeUCM.getFieldPos("HA");
if (! existHectares)
{
existHectares = shapeUCM.existField("HECTARES");
hectaresPos = shapeUCM.getFieldPos("HECTARES");
}
if (! existHectares)
{
shapeUCM.addField("HECTARES", FTDouble, 12, 1);
hectaresPos = shapeUCM.getFieldPos("HECTARES");
}

// FILL ID_CROP and ID_CASE
for (int shapeIndex = 0; shapeIndex < nShape; shapeIndex++)
for (int shapeNr = 0; shapeNr < nShape; shapeNr++)
{
std::string cropStr = shapeUCM.readStringAttribute(shapeIndex, cropIndex);
std::string cropStr = shapeUCM.readStringAttribute(shapeNr, cropIndex);
if (cropStr == "-9999") cropStr = "";

std::string soilStr = shapeUCM.readStringAttribute(shapeIndex, soilIndex);
std::string soilStr = shapeUCM.readStringAttribute(shapeNr, soilIndex);
if (soilStr == "-9999") soilStr = "";

std::string meteoStr = shapeUCM.readStringAttribute(shapeIndex, meteoIndex);
std::string meteoStr = shapeUCM.readStringAttribute(shapeNr, meteoIndex);
if (meteoStr == "-9999") meteoStr = "";

// check ID CASE
std::string caseStr = "";
if (meteoStr != "" && soilStr != "" && cropStr != "")
{
caseStr = "M" + meteoStr + "S" + soilStr + "C" + cropStr;
}
else
{
shapeUCM.deleteRecord(shapeNr);
continue;
}

if (! existIdCrop) shapeUCM.writeStringAttribute(shapeIndex, idCropIndex, cropStr.c_str());
shapeUCM.writeStringAttribute(shapeIndex, idCaseIndex, caseStr.c_str());
// write ID CROP
if (! existIdCrop)
{
shapeUCM.writeStringAttribute(shapeNr, idCropPos, cropStr.c_str());
}

if (caseStr == "")
shapeUCM.deleteRecord(shapeIndex);
// check hectares
double hectares = shapeUCM.readDoubleAttribute(shapeNr, hectaresPos);
if (hectares == 0)
{
shapeUCM.writeDoubleAttribute(shapeNr, hectaresPos, NODATA);
}

// write ID CASE
shapeUCM.writeStringAttribute(shapeNr, idCasePos, caseStr.c_str());
}

if (showInfo) formInfo.close();
if (showInfo)
formInfo.close();
cleanShapeFile(shapeUCM);

return isOk;
Expand All @@ -152,7 +188,7 @@ bool fillUcmIdCase(Crit3DShapeHandler &ucm, std::string idCrop, std::string idSo
int cropIndex = ucm.getFieldPos(idCrop);
int soilIndex = ucm.getFieldPos(idSoil);
int meteoIndex = ucm.getFieldPos(idMeteo);
int idCaseIndex = ucm.getFieldPos("ID_CASE");
int idCasePos = ucm.getFieldPos("ID_CASE");

if (cropIndex == -1 || soilIndex == -1 || meteoIndex == -1)
{
Expand All @@ -173,7 +209,7 @@ bool fillUcmIdCase(Crit3DShapeHandler &ucm, std::string idCrop, std::string idSo
if (meteoStr != "" && soilStr != "" && cropStr != "")
caseStr = "M" + meteoStr + "S" + soilStr + "C" + cropStr;

ucm.writeStringAttribute(shapeIndex, idCaseIndex, caseStr.c_str());
ucm.writeStringAttribute(shapeIndex, idCasePos, caseStr.c_str());

if (caseStr == "")
ucm.deleteRecord(shapeIndex);
Expand Down Expand Up @@ -201,11 +237,11 @@ bool writeUcmListToDb(Crit3DShapeHandler &shapeHandler, QString dbName, QString

double hectares = shapeHandler.getNumericValue(signed(i), "HA");
if (hectares == NODATA)
hectares = shapeHandler.getNumericValue(signed(i), "Hectares");
if (hectares == NODATA)
hectares = shapeHandler.getNumericValue(signed(i), "hectares");
{
hectares = shapeHandler.getNumericValue(signed(i), "HECTARES");
}

if ( !idCase.contains(key) )
if ( ! idCase.contains(key) )
{
idCase << key;
idCrop << QString::fromStdString(shapeHandler.getStringValue(signed(i), "ID_CROP"));
Expand Down
4 changes: 2 additions & 2 deletions agrolib/utilities/computationUnitsDb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ bool ComputationUnitsDB::writeListToCompUnitsTable(QList<QString> &idCase, QList
QList<double> &hectares, QString &error)
{
QSqlQuery qry(db);
qry.prepare("CREATE TABLE computational_units (ID_CASE TEXT, ID_CROP TEXT, ID_METEO TEXT, ID_SOIL TEXT, hectares NUMERIC, PRIMARY KEY(ID_CASE))");
qry.prepare("CREATE TABLE computational_units (ID_CASE TEXT, ID_CROP TEXT, ID_METEO TEXT, ID_SOIL TEXT, HECTARES NUMERIC, PRIMARY KEY(ID_CASE))");
if( !qry.exec() )
{
error = qry.lastError().text();
return false;
}
qry.clear();

QString myQuery = "INSERT INTO computational_units (ID_CASE, ID_CROP, ID_METEO, ID_SOIL, hectares) VALUES ";
QString myQuery = "INSERT INTO computational_units (ID_CASE, ID_CROP, ID_METEO, ID_SOIL, HECTARES) VALUES ";

for (int i = 0; i < idCase.size(); i++)
{
Expand Down

0 comments on commit 4332cc3

Please sign in to comment.