Documentation for OpenFLUID 2.2.0
GrassGISProxy.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 /**
33  @file GrassGISProxy.hpp
34 
35  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
36 */
37 
38 
39 #ifndef __OPENFLUID_UTILS_GRASSGISPROXY_HPP__
40 #define __OPENFLUID_UTILS_GRASSGISPROXY_HPP__
41 
42 
43 #include <map>
44 #include <vector>
45 
48 #include <openfluid/dllexport.hpp>
49 
50 
51 namespace openfluid { namespace utils {
52 
53 
54 class OPENFLUID_API GrassGISProxy : public ProgramProxy<GrassGISProxy>
55 {
56  private:
57 
58  std::string m_GISBase;
59 
60  std::string m_Location;
61 
62  std::string m_Mapset;
63 
64  std::vector<std::string> m_JobLines;
65 
66  std::string m_OutputFile;
67 
68  std::string m_ErrorFile;
69 
70  static void findGrassGISProgram();
71 
72  static std::string getCommandLine(const std::string& Command,
73  const std::map<std::string,std::string>& Arguments = {},
74  const std::vector<std::string>& Options = {});
75 
76  static std::string writeJobFile(const std::vector<std::string>& Lines, const std::string& DirPath);
77 
78  int executeGrassJob(const std::string& JobFilePath) const;
79 
80  int executeGrassJobReturningData(const std::string& JobFilePath, std::vector<std::string>& Lines) const;
81 
82 
83  public:
84 
85  GrassGISProxy() = delete;
86 
87  GrassGISProxy(const std::string& GISBase, const std::string& Location, const std::string& Mapset = "PERMANENT");
88 
89  virtual ~GrassGISProxy();
90 
91  static bool isAvailable();
92 
93  /**
94  Sets the current location to use for next GRASS execution
95  @param[in] Location the location directory
96  */
97  void setLocation(const std::string& Location)
98  {
99  m_Location = Location;
100  }
101 
102  /**
103  Sets the current mapset to use for next GRASS execution
104  @param[in] Mapset the mapset directory
105  */
106  void setMapset(const std::string& Mapset)
107  {
108  m_Mapset = Mapset;
109  }
110 
111  /**
112  Clears all appended tasks.
113  */
114  void clearTasks()
115  {
116  m_JobLines.clear();
117  }
118 
119  /**
120  Appends a task to the current GRASS job.
121  @param[in] Command the GRASS command to execute
122  @param[in] Arguments a key-value map of arguments to pass to the GRASS command
123  @param[in] Options options to pass to the GRASS command
124  */
125  void appendTask(const std::string& Command,
126  const std::map<std::string,std::string>& Arguments = {},
127  const std::vector<std::string>& Options = {});
128 
129  /**
130  Runs a GRASS job made of appended tasks. Once finished, the task queue is cleared.
131  @return the exit code of the execution
132  */
133  int runJob();
134 
135  /**
136  Runs a single GRASS task.
137  @param[in] Command the GRASS command to execute
138  @param[in] Arguments a key-value map of arguments to pass to the GRASS command
139  @param[in] Options options to pass to the GRASS command
140  @return the exit code of the execution
141  */
142  int runSingleTask(const std::string& Command,
143  const std::map<std::string,std::string>& Arguments = {},
144  const std::vector<std::string>& Options = {}) const;
145 
146  /**
147  Sets the file path where the standard output stream will be redirected.
148  If not set or empty, the stream is redirected to a temporary file.
149  @param[in] FullPath the full path for the output file
150  */
151  void setOutputFile(const std::string& FullPath)
152  {
153  m_OutputFile = FullPath;
154  }
155 
156  /**
157  Sets the file path where the standard error stream will be redirected.
158  If not set or empty, the stream is redirected to a temporary file.
159  @param[in] FullPath the full path for the error file
160  */
161  void setErrorFile(const std::string& FullPath)
162  {
163  m_ErrorFile = FullPath;
164  }
165 
166  /**
167  Returns the current command lines of the job
168  @return a list of command lines
169  */
170  const std::vector<std::string>& jobLines() const
171  {
172  return m_JobLines;
173  }
174 
175 
176  /**
177  Returns true if the location already exists
178  @return true if the location already exists, false otherwise
179  */
180  bool isLocationExist() const;
181 
182 
183  /**
184  Creates the location defined for the current GRASS session, with the given EPSG code
185  @param[in] EPSG the EPSG code for the location
186  @return true if the location has been created, false if it could not be created
187  or if it already exists
188  */
189  bool createLocation(const std::string& EPSG) const;
190 
191  /**
192  Returns the region informations as a key-value map,
193  given by the results of the "g.region -gucep" GRASS command
194  @return a key-value map about containing region informations
195  */
196  std::map<std::string,double> region() const;
197 
198  /**
199  Returns the environment informations as a key-value map,
200  given by the results of the "g.gisenv -n --q" GRASS command
201  @return a key-value map about containing environment informations
202  */
203  std::map<std::string,std::string> gisenv() const;
204 
205  /**
206  Returns the list of mapsets in the current location,
207  given by the results of the "g.mapsets separator='\n' -l" GRASS command
208  @return a vector of strings containing the mapsets list
209  */
210  std::vector<std::string> mapsets() const;
211 
212 };
213 
214 
215 } } // namespaces
216 
217 
218 #endif /* __OPENFLUID_UTILS_GRASSGISPROXY_HPP__ */
219 
Definition: GrassGISProxy.hpp:55
void appendTask(const std::string &Command, const std::map< std::string, std::string > &Arguments={}, const std::vector< std::string > &Options={})
bool createLocation(const std::string &EPSG) const
GrassGISProxy(const std::string &GISBase, const std::string &Location, const std::string &Mapset="PERMANENT")
const std::vector< std::string > & jobLines() const
Definition: GrassGISProxy.hpp:170
std::map< std::string, double > region() const
int runSingleTask(const std::string &Command, const std::map< std::string, std::string > &Arguments={}, const std::vector< std::string > &Options={}) const
void setMapset(const std::string &Mapset)
Definition: GrassGISProxy.hpp:106
void setLocation(const std::string &Location)
Definition: GrassGISProxy.hpp:97
void clearTasks()
Definition: GrassGISProxy.hpp:114
void setOutputFile(const std::string &FullPath)
Definition: GrassGISProxy.hpp:151
std::map< std::string, std::string > gisenv() const
void setErrorFile(const std::string &FullPath)
Definition: GrassGISProxy.hpp:161
std::vector< std::string > mapsets() const
Definition: ProgramProxy.hpp:54
#define OPENFLUID_API
Definition: dllexport.hpp:86
Definition: ApplicationException.hpp:47