FFSM++  1.1.0
French Forest Sector Model ++
Gis.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2015 by Laboratoire d'Economie Forestière *
3  * http://ffsm-project.org *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version, given the compliance with the *
9  * exceptions listed in the file COPYING that is distribued together *
10  * with this file. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 #ifndef STDGIS_H
23 #define STDGIS_H
24 
25 #include <cstdlib>
26 #include <list>
27 #include <string>
28 #include <vector>
29 #include <stdexcept>
30 #include <fstream>
31 #include <iostream>
32 #include <sstream>
33 
34 // regmas headers..
35 #include "BaseClass.h"
36 #include "ModelData.h"
37 #include "Layers.h"
38 #include "Pixel.h"
39 #include "ModelRegion.h"
40 
41 using namespace std;
42 
43 struct lUseCats;
44 struct reclassRules;
45 class Pixel;
46 class Agent_space;
47 class QImage;
48 
49 
50 /// Class to manage the spatial dimension
51 /**
52 Gis class is responsable to provide all methods for spatial analysis.
53 <br>It is equipped with two important vectors:
54  - pxVector contains the array of all pixels on the screen
55  - layerVector contains the layer objects
56 <br>Along the model, IDs of pixels are assigned from left to right, from top to down:
57 <br> --->
58 <br> /
59 <br> --->
60 <br> /
61 <br> --->
62 <p>Pixel origin (0,0) on the top left corner is also the system used by the underlying libraries, but put attencion that instead geographical coordinates, if we are on the North emisfere, are increasing along the up-right direction.
63 
64 @author Antonello Lobianco
65 */
66 
67 class Gis: public BaseClass{
68 
69 public:
70  Gis(ThreadManager* MTHREAD_h); ///< Constructor
71  ~Gis();
72  /// Set the initial space environment, including loading data from files
73  void setSpace();
74  /// Init the layers
75  void initLayers();
76  void initLayersPixelData();
77  void initLayersModelData(const int& year_h=DATA_NOW);
78  /// Apply the forest reclassification with the rules defined in reclRules sheet
79  void applyForestReclassification();
80  /// If subregion mode is on, this function place noValues on the selected layer for all out-of-region pixels
81  void filterSubRegion(string layerName_h);
82  ///< Update the image behind a layer to the GUI;
83  void updateImage(string layerName_h);
84  ///< Add one layer to the system
85  void addLayer(string name_h, string label_h, bool isInteger_h, bool dynamicContent_h, string fullFileName_h = "", bool display_h=true);
86  ///< Fill a layer with empty values
87  void resetLayer(string layerName_h);
88  ///< Check if a layer with a certain name is loaded in the model. Used e.g. to check if the dtm layer (optional) exist.
89  bool layerExist(const string & layerName_h, bool exactMatch = true) const;
90  ///< Return a pointer to a layer given its name
91  Layers* getLayer(const string& layerName_h);
92  ///< Add a legend item to an existing layer
93  void addLegendItem (
94  string name_h,
95  int D_h,
96  string label_h,
97  int rColor_h,
98  int gColor_h,
99  int bColor_h,
100  double minValue_h,
101  double maxValue_h );
102  /// Count the pixels within each legend item for the selected layer
103  void countItems(const string& layerName_h, const bool& debug=false);
104  /// Return a pointer to a plot with a specific value for the specified layer
105  Pixel* getRandomPlotByValue(string layer_h, int layerValue__h);
106  /// Return the vector (shuffled) of all plots with a specific value for a specified layer. It is also possible to specify the level in case of failure
107  vector <Pixel*> getAllPlotsByValue(string layer_h, int layerValue_h, int outputLevel=MSG_WARNING);
108  /// Return the vector (shuffled) of all plots with specific values for a specified layer. It is also possible to specify the level in case of failure
109  vector <Pixel*> getAllPlotsByValue(string layer_h, vector<int> layerValues_h, int outputLevel=MSG_WARNING);
110  /// Return the vector (shuffled) of all plots. It is also possible to specify the level in case of failure
111  vector <Pixel*> getAllPlots(bool masked=true, int outputLevel=MSG_WARNING);
112  /// Return the vector of all plots by a specific region (main region or subregion), optionally shuffled;
113  vector <Pixel*> getAllPlotsByRegion(ModelRegion &region_h, bool shuffle=false);
114  vector <Pixel*> getAllPlotsByRegion(int regId_h, bool shuffle=false);
115  /// Return a vector of the layer ids (as string)
116  vector <string> getLayerNames();
117  /// Return a vector of pointers of existing layers
118  vector <Layers*> getLayerPointers();
119  /// Print the specified layer or all layers (if param layerName_h is missing). @see Layers::print()
120  void printLayers(string layerName_h="");
121  /// Save an image in standard png format
122  void printBinMaps(string layerName_h="");
123 
124 
125  ///< Print debug information (for each pixel in the requested interval, their values on the specified layer)
126  void printDebugValues (string layerName_h, int min_h=0, int max_h=0);
127  double getDistance(const Pixel* px1, const Pixel* px2);
128 
129  int getXNPixels() const {return xNPixels;}; ///< Return the number of pixels on X
130  int getYNPixels() const {return yNPixels;}; ///< Return the number of pixels on Y
131  double getXyNPixels()const {return xyNPixels;}; ///< Return the total number of pixels
132  double getHaByPixel() const {return ((xMetersByPixel*yMetersByPixel)/10000) ;};
133  double getNoValue() const {return noValue;};
134  Pixel* getPixel(int x_h, int y_h){return &pxVector.at(x_h+y_h*xNPixels);}; ///< Return a pixel pointer from its coordinates
135  Pixel* getPixel(int ID_h){return &pxVector.at(ID_h);}; ///< Return a pixel pointer from its ID
136  double getGeoTopY() const {return geoTopY;};
137  double getGeoBottomY() const {return geoBottomY;};
138  double getGeoLeftX() const {return geoLeftX;};
139  double getGeoRightX() const {return geoRightX;};
140  double getXMetersByPixel() const {return xMetersByPixel;};
141  double getYMetersByPixel() const {return yMetersByPixel;};
142  int getSubXL() const {return subXL;};
143  int getSubXR() const {return subXR;};
144  int getSubYT() const {return subYT;};
145  int getSubYB() const {return subYB;};
146  /// Transform the ID of a pixel in subregion coordinates to the real (and model used) coordinates
147  int sub2realID(int id_h);
148  string pack(const string& parName, const string& forName, const string& dClass, const int& year) const {return parName+"#"+forName+"#"+dClass+"#"+i2s(year)+"#";};
149  void unpack(const string& key, string& parName, string& forName, string& dClass, int& year) const;
150 
151  void swap(const int &swap_what);
152 
153 private:
154  void loadLayersDataFromFile(); ///< Load the data of a layer its datafile
155  void applySpatialStochasticValues(); ///< Apply stochastic simulation, e.g. regional volume growth s.d. -> tp multipliers
156  void applyStochasticRiskAdversion(); ///< Give to each agend a stochastic risk adversion. For now Pixel = Agent
157  //void cachePixelValues(); ///< For computational reasons cache some values in constant layers directly as properties of the pixel object
158  vector <Pixel> pxVector; ///< array of Pixel objects
159  vector <Layers> layerVector; ///< array of Layer objects
160  vector <double> lUseTotals; ///< totals, in ha, of area in the region for each type (cached values)
161  int xNPixels; ///< number of pixels along the X dimension
162  int yNPixels; ///< number of pixels along the Y dimension
163  double xyNPixels; ///< total number of pixels
164  double xMetersByPixel; ///< pixel dimension (meters), X
165  double yMetersByPixel; ///< pixel dimension (meters), Y
166  double geoLeftX; ///< geo-coordinates of the map left border
167  double geoTopY; ///< geo-coordinates of the map upper border
168  double geoRightX; ///< geo-coordinates of the map right border
169  double geoBottomY; ///< geo-coordinates of the map bottom border
170  double noValue; ///< value internally use as novalue (individual layer maps can have other values)
171  int subXL; ///< sub region left X
172  int subXR; ///< sub region right X
173  int subYT; ///< sub region top Y
174  int subYB; ///< sub region bottom Y
175 
176 
177 
178 };
179 
180 #endif
int yNPixels
number of pixels along the Y dimension
Definition: Gis.h:162
double getYMetersByPixel() const
Definition: Gis.h:141
string pack(const string &parName, const string &forName, const string &dClass, const int &year) const
Definition: Gis.h:148
The required data is for the current year.
Definition: BaseClass.h:73
double getGeoLeftX() const
Definition: Gis.h:138
int subXR
sub region right X
Definition: Gis.h:172
double noValue
value internally use as novalue (individual layer maps can have other values)
Definition: Gis.h:170
double geoBottomY
geo-coordinates of the map bottom border
Definition: Gis.h:169
double geoTopY
geo-coordinates of the map upper border
Definition: Gis.h:167
vector< Pixel > pxVector
array of Pixel objects
Definition: Gis.h:158
double getGeoBottomY() const
Definition: Gis.h:137
double geoLeftX
geo-coordinates of the map left border
Definition: Gis.h:166
double getNoValue() const
Definition: Gis.h:133
STL namespace.
double getXyNPixels() const
Return the number of pixels on Y.
Definition: Gis.h:131
int getSubYT() const
Definition: Gis.h:144
Thread manager. Responsable to manage the main thread and "speak" with the GUI.
Definition: ThreadManager.h:65
double geoRightX
geo-coordinates of the map right border
Definition: Gis.h:168
double getXMetersByPixel() const
Definition: Gis.h:140
double xMetersByPixel
pixel dimension (meters), X
Definition: Gis.h:164
int subXL
sub region left X
Definition: Gis.h:171
int getYNPixels() const
Return the number of pixels on X.
Definition: Gis.h:130
double getHaByPixel() const
Return the total number of pixels.
Definition: Gis.h:132
int subYT
sub region top Y
Definition: Gis.h:173
Define layer objects at the regional level.
Definition: Layers.h:49
vector< double > lUseTotals
totals, in ha, of area in the region for each type (cached values)
Definition: Gis.h:160
Pixel-level class.
Definition: Pixel.h:47
int xNPixels
number of pixels along the X dimension
Definition: Gis.h:161
int getSubYB() const
Definition: Gis.h:145
Print a WARNING message.
Definition: BaseClass.h:60
int getSubXR() const
Definition: Gis.h:143
Base class for the regmas application.
Definition: BaseClass.h:239
int subYB
sub region bottom Y
Definition: Gis.h:174
Class to manage the spatial dimension.
Definition: Gis.h:67
int getSubXL() const
Definition: Gis.h:142
Pixel * getPixel(int x_h, int y_h)
Definition: Gis.h:134
double xyNPixels
total number of pixels
Definition: Gis.h:163
int getXNPixels() const
Definition: Gis.h:129
double getGeoTopY() const
Return a pixel pointer from its ID.
Definition: Gis.h:136
double getGeoRightX() const
Definition: Gis.h:139
This file is the header of BaseClass and it is included by ALL compiled code.
Pixel * getPixel(int ID_h)
Return a pixel pointer from its coordinates.
Definition: Gis.h:135
vector< Layers > layerVector
array of Layer objects
Definition: Gis.h:159
double yMetersByPixel
pixel dimension (meters), Y
Definition: Gis.h:165