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@supagro.inra.fr>
36 */
37 
38 
39 
40 #ifndef __OPENFLUID_UTILS_GRASSGISPROXY_HPP__
41 #define __OPENFLUID_UTILS_GRASSGISPROXY_HPP__
42 
43 
44 #include <map>
45 #include <vector>
46 
47 #include <QStringList>
48 
50 #include <openfluid/dllexport.hpp>
51 
52 
53 namespace openfluid { namespace utils {
54 
55 
56 class OPENFLUID_API GrassGISProxy : public ProgramProxy<GrassGISProxy>
57 {
58  private:
59 
60  QString m_GISBase;
61 
62  QString m_Location;
63 
64  QString m_Mapset;
65 
66  QStringList m_JobLines;
67 
68  QString m_OutputFile;
69 
70  QString m_ErrorFile;
71 
72  static void findGrassGISProgram();
73 
74  static QString getCommandLine(const QString& Command,
75  const std::map<QString,QString> Arguments = {},
76  const std::vector<QString> Options = {});
77 
78  static QString writeJobFile(const QStringList& Lines, const QString& DirPath);
79 
80  int executeGrassJob(const QString& JobFilePath) const;
81 
82  int executeGrassJobReturningData(const QString& JobFilePath, QStringList& Lines) const;
83 
84 
85  public:
86 
87  GrassGISProxy(const QString& GISBase, const QString& Location, const QString& 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 QString& Location)
98  { m_Location = Location; };
99 
100  /**
101  Sets the current mapset to use for next GRASS execution
102  @param[in] Mapset the mapset directory
103  */
104  void setMapset(const QString& Mapset)
105  { m_Mapset = Mapset; }
106 
107  /**
108  Clears all appended tasks.
109  */
110  void clearTasks()
111  { m_JobLines.clear(); };
112 
113  /**
114  Appends a task to the current GRASS job.
115  @param[in] Command the GRASS command to execute
116  @param[in] Arguments a key-value map of arguments to pass to the GRASS command
117  @param[in] Options options to pass to the GRASS command
118  */
119  void appendTask(const QString& Command,
120  const std::map<QString,QString> Arguments = {},
121  const std::vector<QString> Options = {});
122 
123  /**
124  Runs a GRASS job made of appended tasks. Once finished, the task queue is cleared.
125  @return the exit code of the execution
126  */
127  int runJob();
128 
129  /**
130  Runs a single GRASS task.
131  @param[in] Command the GRASS command to execute
132  @param[in] Arguments a key-value map of arguments to pass to the GRASS command
133  @param[in] Options options to pass to the GRASS command
134  @return the exit code of the execution
135  */
136  int runSingleTask(const QString& Command,
137  const std::map<QString,QString> Arguments = {},
138  const std::vector<QString> Options = {}) const;
139 
140  /**
141  Sets the file path where the standard output stream will be redirected.
142  If not set or empty, the stream is redirected to a temporary file.
143  @param[in] FullPath the full path for the output file
144  */
145  void setOutputFile(const QString& FullPath)
146  { m_OutputFile = FullPath; }
147 
148  /**
149  Sets the file path where the standard error stream will be redirected.
150  If not set or empty, the stream is redirected to a temporary file.
151  @param[in] FullPath the full path for the error file
152  */
153  void setErrorFile(const QString& FullPath)
154  { m_ErrorFile = FullPath; }
155 
156  /**
157  Returns the current command lines of the job
158  @return a list of command lines
159  */
160  const QStringList& jobLines() const
161  { return m_JobLines; }
162 
163 
164  /**
165  Returns true if the location already exists
166  @return true if the location already exists, false otherwise
167  */
168  bool isLocationExist() const;
169 
170 
171  /**
172  Creates the location defined for the current GRASS session, with the given EPSG code
173  @param[in] EPSG the EPSG code for the location
174  @return true if the location has been created, false if it could not be created
175  or if it already exists
176  */
177  bool createLocation(const QString& EPSG) const;
178 
179  /**
180  Returns the region informations as a key-value map,
181  given by the results of the "g.region -gucep" GRASS command
182  @return a key-value map about containing region informations
183  */
184  std::map<std::string,double> region() const;
185 
186  /**
187  Returns the environment informations as a key-value map,
188  given by the results of the "g.gisenv -n --q" GRASS command
189  @return a key-value map about containing environment informations
190  */
191  std::map<std::string,std::string> gisenv() const;
192 
193  /**
194  Returns the list of mapsets in the current location,
195  given by the results of the "g.mapsets separator='\n' -l" GRASS command
196  @return a vector of strings containing the mapsets list
197  */
198  std::vector<std::string> mapsets() const;
199 
200 
201 };
202 
203 
204 } } // namespaces
205 
206 
207 #endif /* __OPENFLUID_UTILS_GRASSGISPROXY_HPP__ */
208 
void clearTasks()
Definition: GrassGISProxy.hpp:110
void setOutputFile(const QString &FullPath)
Definition: GrassGISProxy.hpp:145
Definition: ProgramProxy.hpp:51
void setErrorFile(const QString &FullPath)
Definition: GrassGISProxy.hpp:153
Definition: GrassGISProxy.hpp:56
void setMapset(const QString &Mapset)
Definition: GrassGISProxy.hpp:104
void setLocation(const QString &Location)
Definition: GrassGISProxy.hpp:97
const QStringList & jobLines() const
Definition: GrassGISProxy.hpp:160
Definition: ApplicationException.hpp:47
#define OPENFLUID_API
Definition: dllexport.hpp:87