89 int nRegions = regions.size();
90 for(uint i=0;i<nRegions;i++){
91 regions[i]->setMyPixels();
133 for(uint i=0;i<regIds2.size();i++){
134 int regId = regIds2[i];
141 for(uint j=0; j<fTypes.size(); j++){
146 normal_distribution<double> d(1,sStDev/agr);
147 for (uint z=0;z<rpx.size();z++){
149 double c2 = max(0.4,min(1.6,c));
154 rpx[z]->setSpModifier(c2,j);
166 if((avgExpTypes<0 || avgExpTypes>1) && avgExpTypes != -1){
169 if(avgExpTypesPrices<0 || avgExpTypesPrices>1){
174 normal_distribution<double> exp_distr(avgExpTypes,expTypes_cv *avgExpTypes);
175 normal_distribution<double> expPrices_distr(avgExpTypesPrices,expTypesPrices_cv *avgExpTypesPrices);
177 for (uint z=0;z<rpx.size();z++){
178 if(avgExpTypes == -1){
179 rpx[z]->expType = -1;
185 rpx[z]->expType = c3;
188 rpx[z]->expTypePrices = cPrice;
208 for(uint i=0;i<regIds2.size();i++){
209 int regId = regIds2[i];
212 for (uint z=0;z<rpx.size();z++){
216 normal_distribution<double> d(avg,raStDev);
218 rpx[z]->setValue (
"ra", c);
220 rpx[z]->setValue (
"ra", 0.0);
231 if(!MTHREAD->MD->getBoolSetting("usePixelData")) return;
232 if(!MTHREAD->MD->getBoolSetting("applyAvalCoef")) return;
234 bool applyAvalCoef = MTHREAD->MD->getBoolSetting("applyAvalCoef");
236 vector <int> regIds2 = MTHREAD->MD->getRegionIds(2);
238 for(uint i=0;i<regIds2.size();i++){
239 ModelRegion* reg = MTHREAD->MD->getRegion(regIds2[i]);
240 vector <Pixel*> rpx = MTHREAD->GIS->getAllPlotsByRegion(regIds2[i]);
241 for (uint p=0;p<rpx.size();p++){
243 rpx[p]->avalCoef = rpx[p]->getDoubleValue("avalCoef", true);
267 vector<InputNode> layerNodes = gisDocument.
getNodesByName(
"layer");
269 for (uint i=0; i<layerNodes.size();i++){
271 string nameOrig = layerNodes.at(i).getNodeByName(
"name").getStringContent();
272 string labelOrig = layerNodes.at(i).getNodeByName(
"label").getStringContent();
273 bool isInteger = layerNodes.at(i).getNodeByName(
"isInteger").getBoolContent();
274 bool dynamicContent = layerNodes.at(i).getNodeByName(
"dynamicContent").getBoolContent();
275 bool expandByFt = layerNodes.at(i).getNodeByName(
"expandByFt").getBoolContent();
276 string readAtStart = layerNodes.at(i).getNodeByName(
"readAtStart").getStringContent();
277 if (readAtStart !=
"true")
continue;
278 string dirName = layerNodes.at(i).getNodeByName(
"dirName").getStringContent();
279 string fileName = layerNodes.at(i).getNodeByName(
"fileName").getStringContent();
282 uint endingLoop = expandByFt ? ftIds.size(): 1;
283 for(uint z=0;z<endingLoop;z++){
284 string ftExtension= expandByFt ?
"_"+ftIds[z]:
"";
285 string labelFtExtension= expandByFt ?
" ("+ftIds[z]+
")":
"";
286 string name = nameOrig+ftExtension;
287 string label = labelOrig + labelFtExtension;
289 string fullFileName = ((dirName ==
"") || (fileName==
""))?
"":
MTHREAD->
MD->
getBaseDirectory()+dirName+fileName+ftExtension;
290 addLayer(name,label,isInteger,dynamicContent,fullFileName);
292 vector<InputNode> legendItemsNodes = layerNodes.at(i).getNodesByName(
"legendItem");
293 for (uint j=0; j<legendItemsNodes.size();j++){
294 int lID = legendItemsNodes.at(j).getIntContent();
295 string llabel = legendItemsNodes.at(j).getStringAttributeByName(
"label");
296 int rColor = legendItemsNodes.at(j).getIntAttributeByName(
"rColor");
297 int gColor = legendItemsNodes.at(j).getIntAttributeByName(
"gColor");
298 int bColor = legendItemsNodes.at(j).getIntAttributeByName(
"bColor");
299 double minValue, maxValue;
301 minValue = ((double)lID);
302 maxValue = ((double)lID);
305 minValue = legendItemsNodes.at(j).getDoubleAttributeByName(
"minValue");
306 maxValue = legendItemsNodes.at(j).getDoubleAttributeByName(
"maxValue");
308 addLegendItem(name, lID, llabel, rColor, gColor, bColor, minValue, maxValue);
324 vector<string> files = vector<string>();
325 string fullFilename, filename, fullPath;
331 for (
unsigned int i = 0;i < files.size();i++) {
332 fullFilename = files[i];
333 fullPath = dir+
"/"+fullFilename;
334 filename = fullFilename.substr(0,fullFilename.find_last_of(
"."));
335 addLayer(filename,filename,
false,
false,fullPath,
false);
342 vector<string> files2 = vector<string>();
343 string fullFilename2, filename2, fullPath2;
345 for (
unsigned int i = 0;i < files2.size();i++) {
346 fullFilename2 = files2[i];
347 fullPath2 = dir2+
"/"+fullFilename2;
348 filename2 = fullFilename2.substr(0,fullFilename2.find_last_of(
"."));
349 addLayer(filename2,filename2,
false,
false,fullPath2,
false);
357 vector<string> files2 = vector<string>();
358 string fullFilename2, filename2, fullPath2;
360 for (
unsigned int i = 0;i < files2.size();i++) {
361 fullFilename2 = files2[i];
362 fullPath2 = dir2+
"/"+fullFilename2;
363 filename2 = fullFilename2.substr(0,fullFilename2.find_last_of(
"."));
364 addLayer(filename2,filename2,
false,
false,fullPath2,
false);
369 addLayer(
"pixelIds",
"Pixel IDs",
true,
false,
"",
true);
371 for (
unsigned int i = 0; i < allPx.size();i++) {
372 allPx[i]->changeValue(
"pixelIds", allPx[i]->getID());
449 vector <reclRule*> RRs;
450 for(uint z=0;z<nReclassRules;z++){
475 for(uint z=0;z<nReclassRules;z++){
481 double reclassCoeff = RR->
coeff;
482 value += inputValue * reclassCoeff ;
515Gis::addLayer(
string name_h,
string label_h,
bool isInteger_h,
bool dynamicContent_h,
string fullFileName_h,
bool display_h){
516 if(name_h ==
"forArea_ash"){
525 Layers LAYER (
MTHREAD, name_h, label_h, isInteger_h, dynamicContent_h, fullFileName_h, display_h);
548 msgOut(
MSG_ERROR,
"I could not reset layer "+layerName_h+
" as it doesn't exist!");
562 if (
layerVector.at(i).getName().compare(0, layerName_h.size(),layerName_h )){
579Gis::addLegendItem(
string name_h,
int ID_h,
string label_h,
int rColor_h,
int gColor_h,
int bColor_h,
double minValue_h,
double maxValue_h){
583 layerVector.at(i).addLegendItem(ID_h, label_h, rColor_h, gColor_h, bColor_h, minValue_h, maxValue_h);
587 msgOut(
MSG_ERROR,
"Trying to add a legend item to a layer that doesn't exist.");
607 msgOut(
MSG_ERROR,
"Trying to get statistics (count pixels) of a layer that doesn't exist.");
633 if(fileName ==
"")
continue;
634 QFile file(fileName.c_str());
635 if (!file.open(QFile::ReadOnly)) {
636 cerr <<
"Cannot open file for reading: "
637 << qPrintable(file.errorString()) << endl;
641 QTextStream in(&file);
644 image.fill(qRgb(255, 255, 255));
645 while (!in.atEnd()) {
646 QString line = in.readLine();
647 QStringList fields = line.split(
' ');
649 (fields.at(0)==
"north:" && fields.at(1).toDouble() !=
geoTopY)
650 || ((fields.at(0)==
"south:" || fields.at(0) ==
"yllcorner" ) && fields.at(1).toDouble() !=
geoBottomY)
651 || (fields.at(0)==
"east:" && fields.at(1).toDouble() !=
geoRightX)
652 || ((fields.at(0)==
"west:" || fields.at(0) ==
"xllcorner" ) && fields.at(1).toDouble() !=
geoLeftX)
653 || ((fields.at(0)==
"rows:" || fields.at(0) ==
"nrows" ) && fields.at(1).toInt() !=
yNPixels)
654 || ((fields.at(0)==
"cols:" || fields.at(0) ==
"ncols" ) && fields.at(1).toInt() !=
xNPixels)
657 msgOut(
MSG_ERROR,
"Layer "+layerName+
" has different coordinates. Aborting reading.");
659 }
else if (fields.at(0)==
"null:" || fields.at(0) ==
"NODATA_value" || fields.at(0) ==
"nodata_value" ) {
660 localNoValue = fields.at(1).toDouble();
661 }
else if (fields.size() > 5) {
662 for (
int countColumn=0;countColumn<
xNPixels;countColumn++){
663 inputValue = fields.at(countColumn).toDouble();
664 if (inputValue == localNoValue){
666 pxVector.at((countRow*
xNPixels+countColumn)).changeValue(layerName,outputValue);
667 QColor nocolor(255,255,255);
671 outputValue=
layerVector.at(i).filterExogenousDataset(fields.at(countColumn).toDouble());
672 pxVector.at((countRow*
xNPixels+countColumn)).changeValue(layerName,outputValue);
675 image.setPixel(countColumn,countRow,color.rgb());
701 msgOut (1,
"Update image "+layerName_h+
"...");
706 image.fill(qRgb(255, 255, 255));
718 for (
int countRow=
subYT;countRow<
subYB;countRow++){
719 for (
int countColumn=
subXL;countColumn<
subXR;countColumn++){
720 double value =
pxVector.at((countRow*
xNPixels+countColumn)).getDoubleValue(layerName_h);
721 QColor color =
layerVector.at(layerIndex).getColor(value);
722 image.setPixel(countColumn-
subXL,countRow-
subYT,color.rgb());
732 vector <Pixel* > candidates;
733 vector <uint> counts;
734 for(uint i=0;i<
pxVector.size();i++) counts.push_back(i);
735 random_shuffle(counts.begin(), counts.end());
737 for (uint i=0;i<counts.size();i++){
738 if(
pxVector.at(counts.at(i)).getDoubleValue(layer_h) == layerValue_h ) {
764 vector <Pixel* > candidates;
765 for (uint i=0;i<
pxVector.size();i++){
766 if(
pxVector.at(i).getDoubleValue(layer_h) == layerValue_h){
767 candidates.push_back(&
pxVector.at(i));
771 if (candidates.size()>0){
772 random_shuffle(candidates.begin(), candidates.end());
775 msgOut(outputLevel,
"We can't find any free pixel with "+
d2s(layerValue_h)+
" value on layer "+layer_h+
".");
791 vector <Pixel* > candidates;
792 string valuesToMatch;
796 for (uint j=0;j<layerValues_h.size();j++){
797 valuesToMatch = valuesToMatch +
" " +
i2s(layerValues_h.at(j));
800 for (uint i=0;i<
pxVector.size();i++){
801 z = valuesToMatch.find(
d2s(
pxVector.at(i).getDoubleValue(layer_h)));
803 candidates.push_back(&
pxVector.at(i));
807 if (candidates.size()>0){
808 random_shuffle(candidates.begin(), candidates.end());
811 msgOut(outputLevel,
"We can't find any free plot with the specified values ("+valuesToMatch+
") on layer "+layer_h+
".");
824 vector <Pixel* > candidates;
826 for (uint i=0;i<
pxVector.size();i++){
827 candidates.push_back(&
pxVector.at(i));
829 if (candidates.size()>0){
830 random_shuffle(candidates.begin(), candidates.end());
832 msgOut(outputLevel,
"We can't find any free plot.");
837 for(uint i=0;i<regIds2.size();i++){
842 candidates.insert( candidates.end(), rpx.begin(), rpx.end() );
844 if (candidates.size()>0){
845 random_shuffle(candidates.begin(), candidates.end());
847 msgOut(outputLevel,
"We can't find any free plot.");
856 vector <Pixel*> regionalPixels = region_h.
getMyPixels();
858 random_shuffle(regionalPixels.begin(), regionalPixels.end());
860 return regionalPixels;
873 vector <string> toReturn;
882 vector <Layers*> toReturn;
903 msgOut(
MSG_DEBUG,
"Printing debug information for layer "+layerName_h+
".");
904 for (
int i=min;i<max;i++){
905 value =
pxVector.at(i).getDoubleValue(layerName_h);
910 out =
"Px. "+
i2s(ID)+
" ("+
i2s(X)+
","+
i2s(Y)+
"): "+
d2s(value);
943 if(layerName_h ==
""){
966 if(layerName_h ==
""){
988Gis::unpack(
const string& key,
string& parName,
string& forName,
string& dClass,
int& year)
const{
989 int parNameDelimiter = key.find(
"#",0);
990 int forNameDelimiter = key.find(
"#",parNameDelimiter+1);
991 int dClassDelimiter = key.find(
"#",forNameDelimiter+1);
992 int yearDelimiter = key.find(
"#",dClassDelimiter+1);
993 if (yearDelimiter == string::npos){
996 parName.assign(key,0,parNameDelimiter);
997 forName.assign(key,parNameDelimiter+1,forNameDelimiter-parNameDelimiter-1);
998 dClass.assign(key,forNameDelimiter+1,dClassDelimiter-forNameDelimiter-1);
999 string yearString=
"";
1000 yearString.assign(key,dClassDelimiter+1,yearDelimiter-dClassDelimiter-1);
1001 year =
s2i(yearString);
1007 for(uint i=0;i<
pxVector.size();i++) {
@ DATA_NOW
The required data is for the current year.
@ MSG_CRITICAL_ERROR
Print an error message and stop the model.
@ MSG_ERROR
Print an ERROR message, but don't stop the model.
@ MSG_DEBUG
Print a debug message, normally filtered out.
@ MSG_INFO
Print an INFO message.
ThreadManager * MTHREAD
Pointer to the Thread manager.
int s2i(const string &string_h) const
string to integer conversion
string d2s(const double &double_h) const
double to string conversion
void msgOut(const int &msgCode_h, const string &msg_h, const bool &refreshGUI_h=true) const
Overloaded function to print the output log.
double normSample(const double &avg, const double &stdev, const double &minval=NULL, const double &maxval=NULL) const
Sample from a normal distribution with bounds. Slower (double time, but still you see the diff only a...
void refreshGUI() const
Ping to periodically return the control to the GUI.
string i2s(const int &int_h) const
integer to string conversion
vector< Layers * > getLayerPointers()
Return a vector of pointers of existing layers.
int yNPixels
number of pixels along the Y dimension
void printDebugValues(string layerName_h, int min_h=0, int max_h=0)
int subXL
sub region left X
void addLayer(string name_h, string label_h, bool isInteger_h, bool dynamicContent_h, string fullFileName_h="", bool display_h=true)
Fill a layer with empty values.
void applyForestReclassification()
Apply the forest reclassification with the rules defined in reclRules sheet.
double geoRightX
geo-coordinates of the map right border
vector< Pixel * > getAllPlots(bool masked=true, int outputLevel=MSG_WARNING)
Return the vector (shuffled) of all plots. It is also possible to specify the level in case of failur...
int xNPixels
number of pixels along the X dimension
double noValue
value internally use as novalue (individual layer maps can have other values)
void unpack(const string &key, string &parName, string &forName, string &dClass, int &year) const
double xMetersByPixel
pixel dimension (meters), X
double xyNPixels
total number of pixels
double getXyNPixels() const
Return the number of pixels on Y.
vector< Layers > layerVector
array of Layer objects
double geoLeftX
geo-coordinates of the map left border
Pixel * getRandomPlotByValue(string layer_h, int layerValue__h)
Return a pointer to a plot with a specific value for the specified layer.
void applyStochasticRiskAdversion()
Give to each agend a stochastic risk adversion. For now Pixel = Agent.
double yMetersByPixel
pixel dimension (meters), Y
double getHaByPixel() const
Return the total number of pixels.
Pixel * getPixel(int x_h, int y_h)
void swap(const int &swap_what)
void initLayers()
Init the layers.
void setSpace()
Set the initial space environment, including loading data from files.
int subYT
sub region top Y
void printBinMaps(string layerName_h="")
Save an image in standard png format.
int sub2realID(int id_h)
Transform the ID of a pixel in subregion coordinates to the real (and model used) coordinates.
double geoTopY
geo-coordinates of the map upper border
vector< Pixel > pxVector
array of Pixel objects
int subXR
sub region right X
double getDistance(const Pixel *px1, const Pixel *px2)
void initLayersPixelData()
double geoBottomY
geo-coordinates of the map bottom border
void resetLayer(string layerName_h)
Check if a layer with a certain name is loaded in the model. Used e.g. to check if the dtm layer (opt...
void applySpatialStochasticValues()
Apply stochastic simulation, e.g. regional volume growth s.d. -> tp multipliers.
int subYB
sub region bottom Y
vector< Pixel * > getAllPlotsByRegion(ModelRegion ®ion_h, bool shuffle=false)
Return the vector of all plots by a specific region (main region or subregion), optionally shuffled;.
Gis(ThreadManager *MTHREAD_h)
Constructor.
vector< string > getLayerNames()
Return a vector of the layer ids (as string)
vector< Pixel * > getAllPlotsByValue(string layer_h, int layerValue_h, int outputLevel=MSG_WARNING)
Return the vector (shuffled) of all plots with a specific value for a specified layer....
void printLayers(string layerName_h="")
Print the specified layer or all layers (if param layerName_h is missing).
void loadLayersDataFromFile()
Load the data of a layer its datafile.
Layers * getLayer(const string &layerName_h)
Add a legend item to an existing layer.
void countItems(const string &layerName_h, const bool &debug=false)
Count the pixels within each legend item for the selected layer.
bool layerExist(const string &layerName_h, bool exactMatch=true) const
Return a pointer to a layer given its name.
void addLegendItem(string name_h, int D_h, string label_h, int rColor_h, int gColor_h, int bColor_h, double minValue_h, double maxValue_h)
void filterSubRegion(string layerName_h)
If subregion mode is on, this function place noValues on the selected layer for all out-of-region pix...
void updateImage(string layerName_h)
Add one layer to the system.
Define layer objects at the regional level.
vector< LegendItems > getLegendItems()
void addLegendItems(vector< LegendItems > legendItems_h)
forType * getForType(int position)
bool getBoolSetting(const string &name_h, int position=0, int reg=WORLD) const
string getBaseDirectory() const
double getDoubleSetting(const string &name_h, int position=0, int reg=WORLD) const
vector< string > getForTypeIds(bool all=false)
By default it doesn't return forTypes used only as input.
const double getForData(const string &type_h, const int ®Id_h, const string &forType_h, const string &freeDim_h, const int &year=DATA_NOW)
vector< int > getRegionIds(int level_h, bool excludeResidual=true)
ModelRegion * getRegion(int regId_h)
vector< ModelRegion * > getAllRegions(bool excludeResidual=true)
int getIntSetting(const string &name_h, int position=0, int reg=WORLD) const
string getStringSetting(const string &name_h, int position=0, int reg=WORLD) const
reclRule * getReclRule(int position)
int getFilenamesByDir(const string &dir, vector< string > &files, const string &filter="")
Return a list of files in a directory.
vector< Pixel * > getMyPixels()
void changeValue(const string &layerName_h, const double &value_h, const bool &setNoValueForZero=false)
Change the value of an existing layerMTHREAD->GIS->pack(parName, forName, dClass, year),...
double getDoubleValue(const string &layerName_h, const bool &returnZeroForNoValue=false) const
Return the value for a specific layer.
void setCoordinates(int x_h, int y_h)
Thread manager. Responsable to manage the main thread and "speak" with the GUI.
string getBaseDirectory()
void updateImage(string layerName_h, const QImage &image_h)
Scheduler * SCD
the scheduler object (simulation-loops scheduler)
void treeViewerChangeGeneralPropertyValue(string propertyName, string newValue)
Gis * GIS
GIS information and methods.
ModelData * MD
the model data object
std::mt19937 * gen
used in the sampling from normal distribution
void addLayer(string layerName_h, string layerLabel_h)
IO production matrix between the forest resources and the primary products (struct)