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