FMT 0.9.8
Forest management tools for forest planning
Loading...
Searching...
No Matches
FMTparser.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2019 Gouvernement du Québec
3
4SPDX-License-Identifier: LiLiQ-R-1.1
5License-Filename: LICENSES/EN/LiLiQ-R11unicode.txt
6*/
7
8#ifndef FMTparser_H_INCLUDED
9#define FMTparser_H_INCLUDED
10
11#include <boost/regex.hpp>
12#include <iterator>
13#include <iostream>
14#include <fstream>
15#include <string>
16#include <vector>
17#include <queue>
18#include "FMTutility.hpp"
19#include <array>
20#include <ctime>
21
22
23#include <boost/thread/recursive_mutex.hpp>
24
25
26
27#include "FMTobject.hpp"
28#if defined FMTWITHGDAL
29 class OGRSpatialReference;
30 class GDALDataset;
31 class OGRLayer;
32 class GDALRasterBand;
33 class GDALDriver;
34 class OGRSpatialReference;
35 class OGRCoordinateTransformation;
36#endif
37
38
39
40namespace Core {
41 class FMTtheme;
42 class FMTaction;
43 class FMTyields;
44 class FMTspec;
45 class FMTconstants;
46 template<typename T>
47 class FMTbounds;
48}
49
50namespace Spatial
51{
52 template<typename T>
53 class FMTlayer;
54}
55
56
58namespace Parser
59{
60// DocString: FMTparser
66 {
67 private:
68 // DocString: FMTparser::rxvectortheme
70 const static boost::regex rxvectortheme;
71 // DocString: FMTparser::rxnumber
73 const static boost::regex rxnumber;
74 // DocString: FMTparser::rxremovecomment
76 const static boost::regex rxremovecomment;
77 // DocString: FMTparser::rxvalid
79 const static boost::regex rxvalid;
80 // DocString: FMTparser::rxinclude
82 const static boost::regex rxinclude;
83 // DocString: FMTparser::rxfor
85 const static boost::regex rxfor;
86 // DocString: FMTparser::rxend
88 const static boost::regex rxend;
89 // DocString: FMTparser::primary_sections
91 static std::map<Core::FMTsection, std::string>primary_sections;
92 // DocString: FMTparser::_incomment
94 bool _incomment;
95 // DocString: FMTparser::_forvalues
97 std::queue<std::string>_forvalues;
98 // DocString: FMTparser::_included
100 std::queue<std::string>_included;
101 // DocString: FMTparser::GDALinitialization;
103 static bool GDALinitialization;
104 // DocString: FMTparser::safeGetline
106 std::istream& safeGetline(std::istream& is, std::string& t) const;
107 #ifdef FMTWITHGDAL
108 // DocString: FMTparser::GDALinitialization
112 static void initializeGDAL();
113 #endif
114 protected:
115 // DocString: FMTparser::mtx
117 mutable boost::recursive_mutex mtx;
118 // DocString: FMTparser::FMTsection
121 // DocString: FMTparser::rxayld
123 const static boost::regex rxayld;
124 // DocString: FMTparser::rxaage
126 const static boost::regex rxaage;
127 // DocString: FMTparser::rxayldage
129 const static boost::regex rxayldage;
130 // DocString: FMTparser::rxbounds
132 const static boost::regex rxbounds;
133 // DocString: FMTparser::rxoperators
135 const static boost::regex rxoperators;
136 // DocString: FMTparser::rxprimary
138 const static boost::regex rxprimary;
139 // DocString: FMTparser::_constreplacement
141 mutable int _constreplacement;
142 // DocString: FMTparser::_line
144 mutable int _line;
145 // DocString: FMTparser::_comment
147 std::string _comment;
148 // DocString: FMTparser::_location
150 mutable std::string _location;
151 // DocString: FMTparser::mostrecentfile
153 mutable std::time_t mostrecentfile;
154 // DocString: FMTparser::setsection
158 void setsection(const Core::FMTsection& section) const;
159 #ifdef FMTWITHGDAL
160 // DocString: FMTparser::getallGDALdrivernames
164 std::vector<GDALDriver*> getallGDALdrivers(const char* spatialtype,bool testcreation=true) const;
165 // DocString: FMTparser::getGDALextensions
169 std::vector<std::vector<std::string>>getGDALextensions(const char* spatialtype,bool testcreation = true) const;
170 // DocString: FMTparser::setcategories
174 void setcategories(GDALRasterBand* band, const std::vector<std::string>& categories) const;
175 // DocString: FMTareaparser::getprojtransform
180 OGRCoordinateTransformation* getprojtransform(OGRLayer* baselayer,bool fittoforel =true) const;
181 // DocString: FMTareaparser::gettransformmemlayercopy
186 GDALDataset* gettransformmemlayercopy(OGRLayer* baselayer,const OGRSpatialReference* newreference, const std::string& fieldname) const;
187 // DocString: FMTparser::getFORELspatialref
191 std::unique_ptr<OGRSpatialReference> getFORELspatialref() const;
192 // DocString: FMTparser::createdataset
196 template<typename T>
197 GDALDataset* createdataset(const std::string& location, const Spatial::FMTlayer<T>& layer, const int datatypeid,std::string format = "GTiff") const;
198 // DocString: FMTparser::getdataset
202 GDALDataset* getdataset(const std::string& location) const;
203 // DocString: FMTparser::getvectordataset
207 GDALDataset* getvectordataset(const std::string& location) const;
208 // DocString: FMTparser::getlayer
212 OGRLayer* getlayer(GDALDataset* dataset,int id) const;
213 // DocString: FMTparser::getband
217 GDALRasterBand* getband(GDALDataset* dataset,int bandid=1) const;
218 // DocString: FMTparser::createband
222 GDALRasterBand* createband(GDALDataset* dataset,const std::vector<std::string>& categories,int bandid=1) const;
223 // DocString: FMTparser::getcat
227 std::vector<std::string> getcat(GDALDataset* dataset,int bandid=1) const;
228 // DocString: FMTparser::getoverview
232 GDALRasterBand* getoverview(GDALRasterBand* band,int view=1) const;
233 // DocString: FMTparser::getWSfields
237 void getWSfields(OGRLayer* layer, std::map<int,int>& themes,int& age,int& area,int& lock, std::string agefield="", std::string areafield="", std::string lockfield="") const;
238 // DocString: FMTparser::createvectormemoryds
242 GDALDataset* createvectormemoryds() const;
243 #endif
244 // DocString: FMTparser::getbaseoperators
248 std::array<std::string, 5>getbaseoperators() const;
249 // DocString: FMTparser::sameas
253 std::vector<std::string> sameas(const std::string& allset) const;
254 // DocString: FMTparser::getprimary
258 std::map<Core::FMTsection, std::string> getprimary(const std::string& primarylocation);
259 // DocString: FMTparser::isyld
263 bool isyld(const Core::FMTyields& ylds,const std::string& value, Core::FMTsection section) const;
264 // DocString: FMTparser::isact
268 bool isact(Core::FMTsection section,const std::vector<Core::FMTaction>& actions, std::string action) const;
269 // DocString: FMTparser::setspecs
273 std::string setspecs(Core::FMTsection section, Core::FMTkwor key,const Core::FMTyields& ylds,const Core::FMTconstants& constants, std::vector<Core::FMTspec>& specs, const std::string& line);
274 // DocString: FMTparser::setspec
278 std::string setspec(Core::FMTsection section, Core::FMTkwor key,const Core::FMTyields& ylds,const Core::FMTconstants& constants, Core::FMTspec& spec, const std::string& line);
279 // DocString: FMTparser::from_extension
283 Core::FMTsection from_extension(const std::string& ext) const;
284 // DocString: FMTparser::readcsv
288 std::vector<std::vector<std::string>>readcsv(const std::string& location,const char& separator);
289 public:
290 // DocString: FMTparser::rxseparator
292 const static boost::regex rxseparator;
293 // DocString: FMTparser()
298 // DocString: FMTparser(const FMTparser&)
302 FMTparser(const FMTparser& rhs);
303 // DocString: FMTparser::operator=
307 FMTparser& operator = (const FMTparser& rhs);
308 // DocString: ~FMTparser()
312 virtual ~FMTparser()=default;
313 // DocString: FMTparser::getmostrecentfiletime
317 std::time_t getmostrecentfiletime() const;
318 // DocString: FMTparser::setheader
323 void setheader(const std::string& header);
324 // DocString: FMTparser::tryopening
328 bool tryopening(const std::ifstream& stream, const std::string& location) const;
329 // DocString: FMTparser::tryopening
333 bool tryopening(std::ofstream& stream, const std::string& location) const;
334 // DocString: FMTparser::isvalidfile
338 bool isvalidfile(const std::string& location) const;
339 // DocString: FMTparser::isvalid
343 bool isvalid(const std::string& line) const;
344 // DocString: FMTparser::regexloop
348 std::vector<std::string>regexloop(const boost::regex& cutregex, std::string& str) const;
349 // DocString: FMTparser::spliter
353 std::vector<std::string>spliter(std::string strmask,const boost::regex& xspliter) const;
354 // DocString: FMTparser::clearcomments
358 void clearcomments(std::string& line);
359 // DocString: FMTparser::upper
363 std::string upper(const std::string& lowercases) const;
364 // DocString: FMTparser::getcleanline
368 std::string getcleanline(std::ifstream& stream);
369 // DocString: FMTparser::tryinclude
373 std::queue<std::string> tryinclude(const std::string& line, const std::vector<Core::FMTtheme>& themes, const Core::FMTconstants& cons);
374 // DocString: FMTparser::returninclude
378 std::string returninclude(const std::string& line, const std::vector<Core::FMTtheme>& themes, const Core::FMTconstants& cons);
379 // DocString: FMTparser::getforloops
383 bool getforloops(std::string& line, const std::vector<Core::FMTtheme>& themes, const Core::FMTconstants& cons, std::vector<std::string>& allvalues, std::string& target);
384 // DocString: FMTparser::getcleanlinewfor
388 std::string getcleanlinewfor(std::ifstream& stream,const std::vector<Core::FMTtheme>& themes,const Core::FMTconstants& cons);
389 // DocString: FMTparser::isnum
393 bool isnum(std::string value) const;
394 // DocString: FMTparser::isnum
398 bool isnum(const std::string& value,const Core::FMTconstants& constant,bool throwerror= true) const;
399 // DocString: FMTparser::getnum
403 template<typename T>
404 T getnum(const std::string& value, const Core::FMTconstants& constant, int period = 0) const;
405 // DocString: FMTparser::getnum
409 template<typename T>
410 T getnum(const std::string& value,bool omitnumtest=false) const;
411 // DocString: FMTparser::tryfillnumber
415 template<typename T>
416 bool tryfillnumber(T& number, const std::string& value, const Core::FMTconstants& constant, int period = 0) const;
417 // DocString: FMTparser::bounds
421 template<typename T>
422 Core::FMTbounds<T>bounds(const Core::FMTconstants& constants, const std::string& value, const std::string& ope, Core::FMTsection section) const;
423 #ifdef FMTWITHGDAL
424 // DocString: FMTparser::getWSfields
428 GDALDataset* createOGRdataset(std::string location = std::string(),std::string gdaldrivername = "CSV") const;
429 // DocString: FMTparser::createlayer()
433 OGRLayer* createlayer(GDALDataset* dataset,
434 const std::string& name, std::vector<std::string> creationoptions = std::vector<std::string>()) const;
435 // DocString: FMTparser::getGDALvectordrivernames
439 std::vector<std::string>getGDALvectordrivernames(bool testcreation = true) const;
440 // DocString: FMTparser::getGDALrasterdrivernames
444 std::vector<std::string>getGDALrasterdrivernames(bool testcreation = true) const;
445 // DocString: FMTparser::getGDALvectordriverextensions
449 std::vector<std::vector<std::string>>getGDALvectordriverextensions(bool testcreation = true) const;
450 // DocString: FMTparser::getGDALrasterdriverextensions
454 std::vector<std::vector<std::string>>getGDALrasterdriverextensions(bool testcreation = true) const;
455 #endif
456 };
457
458}
459
460#endif
#define FMTEXPORT
Definition: FMTutility.hpp:92
Definition: FMTbounds.hpp:27
Definition: FMTconstants.hpp:29
Definition: FMTobject.hpp:50
Definition: FMTbounds.hpp:342
Definition: FMTyields.hpp:40
Definition: FMTparser.hpp:66
std::queue< std::string > tryinclude(const std::string &line, const std::vector< Core::FMTtheme > &themes, const Core::FMTconstants &cons)
Core::FMTsection _section
The section in which the child class is in.
Definition: FMTparser.hpp:120
std::string upper(const std::string &lowercases) const
static const boost::regex rxseparator
A regex for splitting general string.
Definition: FMTparser.hpp:292
std::string _comment
Comment found in the actual line read by the parser.
Definition: FMTparser.hpp:147
GDALRasterBand * createband(GDALDataset *dataset, const std::vector< std::string > &categories, int bandid=1) const
std::vector< std::string > spliter(std::string strmask, const boost::regex &xspliter) const
Core::FMTbounds< T > bounds(const Core::FMTconstants &constants, const std::string &value, const std::string &ope, Core::FMTsection section) const
bool isvalidfile(const std::string &location) const
std::vector< std::vector< std::string > > getGDALrasterdriverextensions(bool testcreation=true) const
GDALDataset * createOGRdataset(std::string location=std::string(), std::string gdaldrivername="CSV") const
static const boost::regex rxaage
A regex for capturing age specificaiton.
Definition: FMTparser.hpp:126
std::string getcleanlinewfor(std::ifstream &stream, const std::vector< Core::FMTtheme > &themes, const Core::FMTconstants &cons)
std::time_t getmostrecentfiletime() const
GDALRasterBand * getoverview(GDALRasterBand *band, int view=1) const
bool isnum(const std::string &value, const Core::FMTconstants &constant, bool throwerror=true) const
bool isnum(std::string value) const
std::vector< std::string > getGDALvectordrivernames(bool testcreation=true) const
GDALDataset * createvectormemoryds() const
std::vector< std::vector< std::string > > readcsv(const std::string &location, const char &separator)
bool tryfillnumber(T &number, const std::string &value, const Core::FMTconstants &constant, int period=0) const
std::string returninclude(const std::string &line, const std::vector< Core::FMTtheme > &themes, const Core::FMTconstants &cons)
virtual ~FMTparser()=default
static const boost::regex rxayldage
A regex for capturing age or yield specification.
Definition: FMTparser.hpp:129
std::string setspec(Core::FMTsection section, Core::FMTkwor key, const Core::FMTyields &ylds, const Core::FMTconstants &constants, Core::FMTspec &spec, const std::string &line)
GDALDataset * createdataset(const std::string &location, const Spatial::FMTlayer< T > &layer, const int datatypeid, std::string format="GTiff") const
int _line
Line id at which the parser is reading the values.
Definition: FMTparser.hpp:144
bool isact(Core::FMTsection section, const std::vector< Core::FMTaction > &actions, std::string action) const
std::vector< std::string > getGDALrasterdrivernames(bool testcreation=true) const
int _constreplacement
Number of constant replaced by a numerical number.
Definition: FMTparser.hpp:141
std::vector< std::string > regexloop(const boost::regex &cutregex, std::string &str) const
OGRCoordinateTransformation * getprojtransform(OGRLayer *baselayer, bool fittoforel=true) const
OGRLayer * createlayer(GDALDataset *dataset, const std::string &name, std::vector< std::string > creationoptions=std::vector< std::string >()) const
OGRLayer * getlayer(GDALDataset *dataset, int id) const
void setcategories(GDALRasterBand *band, const std::vector< std::string > &categories) const
std::time_t mostrecentfile
The most recent file time read by this parser.
Definition: FMTparser.hpp:153
static const boost::regex rxoperators
A regex for capturing simple operators.
Definition: FMTparser.hpp:135
void getWSfields(OGRLayer *layer, std::map< int, int > &themes, int &age, int &area, int &lock, std::string agefield="", std::string areafield="", std::string lockfield="") const
static const boost::regex rxprimary
A regex for capturing the primary file.
Definition: FMTparser.hpp:138
T getnum(const std::string &value, bool omitnumtest=false) const
std::string setspecs(Core::FMTsection section, Core::FMTkwor key, const Core::FMTyields &ylds, const Core::FMTconstants &constants, std::vector< Core::FMTspec > &specs, const std::string &line)
void clearcomments(std::string &line)
FMTparser(const FMTparser &rhs)
boost::recursive_mutex mtx
Mutex for multi-threading.
Definition: FMTparser.hpp:117
void setsection(const Core::FMTsection &section) const
bool tryopening(std::ofstream &stream, const std::string &location) const
std::vector< std::vector< std::string > > getGDALvectordriverextensions(bool testcreation=true) const
T getnum(const std::string &value, const Core::FMTconstants &constant, int period=0) const
std::map< Core::FMTsection, std::string > getprimary(const std::string &primarylocation)
std::array< std::string, 5 > getbaseoperators() const
std::string getcleanline(std::ifstream &stream)
std::vector< std::string > sameas(const std::string &allset) const
Core::FMTsection from_extension(const std::string &ext) const
static const boost::regex rxayld
A regex for capturing age/period/yield specification.
Definition: FMTparser.hpp:123
GDALDataset * getvectordataset(const std::string &location) const
std::vector< std::vector< std::string > > getGDALextensions(const char *spatialtype, bool testcreation=true) const
std::unique_ptr< OGRSpatialReference > getFORELspatialref() const
static const boost::regex rxbounds
A regex for capturing bounds for yield or age specification.
Definition: FMTparser.hpp:132
GDALDataset * getdataset(const std::string &location) const
GDALDataset * gettransformmemlayercopy(OGRLayer *baselayer, const OGRSpatialReference *newreference, const std::string &fieldname) const
bool isyld(const Core::FMTyields &ylds, const std::string &value, Core::FMTsection section) const
bool isvalid(const std::string &line) const
bool getforloops(std::string &line, const std::vector< Core::FMTtheme > &themes, const Core::FMTconstants &cons, std::vector< std::string > &allvalues, std::string &target)
std::vector< std::string > getcat(GDALDataset *dataset, int bandid=1) const
std::string _location
File location of the section read by the parser.
Definition: FMTparser.hpp:150
bool tryopening(const std::ifstream &stream, const std::string &location) const
std::vector< GDALDriver * > getallGDALdrivers(const char *spatialtype, bool testcreation=true) const
void setheader(const std::string &header)
GDALRasterBand * getband(GDALDataset *dataset, int bandid=1) const
Definition: FMTlayer.hpp:29
The Core namespace provides classes for simulating stands/strata growth/harvest through time.
Definition: FMTaction.hpp:31
FMTsection
Definition: FMTutility.hpp:23
FMTkwor
Definition: FMTutility.hpp:39
Namespace handling all FMT's parsers. Everything related to I/O should be located in this namespace.
Definition: FMTactionparser.hpp:26
The spatial namespace provides classes for spatialy explicit simulation/optimization based on raster ...
Definition: FMTareaparser.hpp:36