8#ifndef FMTparser_Hm_included
9#define FMTparser_Hm_included
11#include <boost/regex.hpp>
23#include <boost/thread/recursive_mutex.hpp>
28#if defined FMTWITHGDAL
29 class OGRSpatialReference;
34 class OGRSpatialReference;
35 class OGRCoordinateTransformation;
59 class UnitTestFMTParser;
118 std::string location = std::string(),
119 std::string gdaldrivername =
"CSV")
const;
129 const std::string& name, std::vector<std::string> creationoptions = std::vector<std::string>())
const;
166 bool tryOpening(
const std::ifstream& stream,
const std::string& location)
const;
174 bool tryOpening(std::ofstream& stream,
const std::string& location)
const;
219 FMTLineInfo(
const std::string p_line,
int p_number,
const std::string& p_file);
229 mutable boost::recursive_mutex
m_MTX;
280 std::vector<GDALDriver*>
_getAllGDALDrivers(
const char* spatialtype,
bool testcreation =
true)
const;
288 std::vector<std::vector<std::string>>
getGDALExtensions(
const char* spatialtype,
bool testcreation =
true)
const;
295 void _setCategories(GDALRasterBand* band,
const std::vector<std::string>& categories)
const;
303 OGRCoordinateTransformation*
_getProjTransform(OGRLayer* baselayer,
bool fittoforel =
true)
const;
312 GDALDataset*
_getTransFormMemLayerCopy(OGRLayer* baselayer,
const OGRSpatialReference* newreference,
const std::string& fieldname)
const;
352 OGRLayer*
_getLayer(GDALDataset* dataset,
int id)
const;
360 GDALRasterBand*
_getBand(GDALDataset* dataset,
int bandid = 1)
const;
369 GDALRasterBand*
_createBand(GDALDataset* dataset,
const std::vector<std::string>& categories,
int bandid = 1)
const;
377 std::vector<std::string>
_getCat(GDALDataset* dataset,
int bandid = 1)
const;
385 GDALRasterBand*
_getOverview(GDALRasterBand* band,
int view = 1)
const;
398 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;
428 std::vector<std::string>
regexLoop(
const boost::regex& cutregex, std::string& str)
const;
436 std::vector<std::string>
spliter(std::string strmask,
const boost::regex& xspliter)
const;
449 std::string
_upper(
const std::string& lowercases)
const;
459 std::queue<FMTLineInfo>p_ForOut)
const;
469 virtual std::queue<FMTLineInfo>
_getCleanLinewfor(std::ifstream& p_stream,
const std::vector<Core::FMTTheme>& p_themes,
487 const std::vector<Core::FMTTheme>& p_themes,
489 std::queue<FMTLineInfo>p_AllLines)
const;
526 T
_getNum(
const std::string& value,
bool omitnumtest =
false)
const;
563 std::vector<std::string>
_sameAs(
const std::string& allset)
const;
570 std::map<Core::FMTsection, std::string>
_getPrimary(
const std::string& primarylocation);
622 std::vector<std::vector<std::string>>
readCsv(
const std::string& location,
const char& separator);
632 const std::string& p_upper,
643 const std::string& p_lower,
644 const std::string& p_upper,
655 const std::string& p_periods,
665 std::map<std::string, std::vector<std::string>>
_getForLoops(
const std::string& p_line,
666 const std::vector<Core::FMTTheme>& p_themes,
677 std::queue<FMTLineInfo>& p_queue)
const;
698 std::string
_getLine(std::queue<FMTLineInfo>& p_Lines)
const;
700 friend class Testing::UnitTestFMTParser;
703 const static boost::regex m_VECTOR_THEME;
706 const static boost::regex m_NUMBER;
709 const static boost::regex m_REMOVE_COMMENT;
712 const static boost::regex m_VALID;
715 const static boost::regex m_INCLUDE;
718 const static boost::regex m_FOR;
721 const static boost::regex m_END;
724 const static boost::regex m_PERIODS;
727 static std::map<Core::FMTsection, std::string>primarym_sections;
730 mutable bool m_inComment;
739 static bool gdalInitialization;
747 std::istream& _safeGetline(std::istream& is, std::string& t)
const;
753 static void _initializeGDAL();
762 std::map<std::string, std::vector<std::string>>_queryDatabase(
763 const std::string& p_DataBaseLocation,
764 const std::string& p_VariableName,
765 const std::string& p_Query)
const;
773 std::string _getAbsolutePath(std::string p_Path)
const;
780 FMTLineInfo _setForLoopLines(std::queue<FMTLineInfo>& p_queue)
const;
788 std::string _processConstants(std::string p_input,
#define FMTEXPORT
Definition: FMTutility.h:125
Definition: FMTParser.h:48
Class holding the constants defined in the constants section of a model, used by the parsers.
Definition: FMTConstants.h:28
Base class of multiple FMT classes holding a shared exception handler pointer and logger.
Definition: FMTObject.h:49
FMTList containing multiple yield handlers as seen in the yield section, holding the information rela...
Definition: FMTYields.h:44
Helper holding a line value, its number and its file, used during parsing.
Definition: FMTParser.h:210
int m_lineNumber
Definition: FMTParser.h:221
std::string m_file
Definition: FMTParser.h:222
FMTLineInfo(const std::string p_line, int p_number, const std::string &p_file)
Construct a line info from a line, its number and its file.
std::string m_lineValue
Definition: FMTParser.h:220
Main parent class of all the parsers in FMT.
Definition: FMTParser.h:72
GDALDataset * _getDataset(const std::string &location) const
Open a GDAL dataset in read only from a location, throwing on error.
GDALRasterBand * _createBand(GDALDataset *dataset, const std::vector< std::string > &categories, int bandid=1) const
Create a GDAL raster band in a dataset with categories.
std::vector< std::string > _sameAs(const std::string &allset) const
Handle the _sameAs keyword.
std::map< Core::FMTsection, std::string > _getPrimary(const std::string &primarylocation)
Return the file locations of the different sections from a primary file.
GDALDataset * _getVectorDataset(const std::string &location) const
Open a GDAL vector dataset in read only from a location, throwing on error.
std::time_t m_MostRecentFile
The most recent file time read by this parser.
Definition: FMTParser.h:265
std::vector< std::string > spliter(std::string strmask, const boost::regex &xspliter) const
Split a string into multiple strings.
static const boost::regex m_AAGE
A regex for capturing age specificaiton.
Definition: FMTParser.h:238
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)
Set the specifications of one or multiple specs and return the rest of the line.
FMTParser()
Default constructor for FMTParser.
std::string getCleanLine(const std::string &p_input) const
Clean a line and make it ready to be read by a parser.
std::vector< std::string > getGDALRasterDriverNames(bool testcreation=true) const
Return a vector of GDAL raster driver names.
GDALDataset * _createVectorMemoryDs() const
Create a vector memory dataset.
std::unique_ptr< OGRSpatialReference > _getFORELSpatialRef() const
Return the spatial reference used for FORELs in Quebec.
std::map< std::string, std::vector< std::string > > _getForLoops(const std::string &p_line, const std::vector< Core::FMTTheme > &p_themes, const Core::FMTConstants &p_cons) const
Fill up the for loops queue with the targeted variables.
Core::FMTsection m_section
The section in which the child class is in.
Definition: FMTParser.h:232
OGRCoordinateTransformation * _getProjTransform(OGRLayer *baselayer, bool fittoforel=true) const
Return a coordinate transformation for a layer, optionally fitting to FOREL.
std::vector< GDALDriver * > _getAllGDALDrivers(const char *spatialtype, bool testcreation=true) const
Return the GDAL drivers for a given spatial type.
std::vector< std::vector< std::string > > getGDALVectorDriverExtensions(bool testcreation=true) const
Return a vector of GDAL vector driver extensions organized by driver.
std::string getCleanLine(std::istream &stream) const
Clean a line and make it ready to be read by a parser.
std::string m_location
File location of the section read by the parser.
Definition: FMTParser.h:262
T _getNum(const std::string &value, const Core::FMTConstants &constant, int period=0) const
Return a numeric value from a string based on the constants and a period.
std::queue< FMTLineInfo > _getAllLines(std::ifstream &p_stream) const
Get all the lines of a file.
GDALRasterBand * _getBand(GDALDataset *dataset, int bandid=1) const
Open a GDAL raster band from a dataset.
bool _isNum(std::string value) const
Return true if the value is a number.
Core::FMTBounds< T > bounds(const Core::FMTConstants &constants, const std::string &value, const std::string &ope, Core::FMTsection section) const
Return a FMTBounds from a value based on the constants and a section.
bool _isValidFile(const std::string &location) const
Check if a file location is valid.
std::vector< std::string > _getCat(GDALDataset *dataset, int bandid=1) const
Return the categories of a dataset for a given band.
std::time_t getMostRecentFiletime() const
Return the most recent file time of the files read by the parser.
GDALDataset * _getTransFormMemLayerCopy(OGRLayer *baselayer, const OGRSpatialReference *newreference, const std::string &fieldname) const
Create a memory dataset copying a layer with a new field, optionally with a new spatial reference.
bool _setPeriods(Core::FMTSpec &p_spec, const std::string &p_periods, const Core::FMTConstants &p_constants) const
Set the periods of a spec from a parsed period string.
static const boost::regex m_AYLD
A regex for capturing age/period/yield specification.
Definition: FMTParser.h:235
bool _isValid(const std::string &line) const
Check if a line is valid and clean it.
FMTParser(const FMTParser &rhs)
Copy constructor for FMTParser.
void _processForLoops(const std::vector< Core::FMTTheme > &p_themes, const Core::FMTConstants &p_constants, std::queue< FMTLineInfo > &p_queue) const
Unroll the for loops in the queue and set the results in the queue.
virtual std::queue< FMTLineInfo > _tryInclude(const std::vector< Core::FMTTheme > &p_themes, const Core::FMTConstants &p_cons, std::queue< FMTLineInfo >p_ForOut) const
Return the next lines to be read by the parser, considering included files.
void _setSection(const Core::FMTsection §ion) const
Set the section member of the parser.
bool _isYld(const Core::FMTYields &p_ylds, const std::string &p_value, Core::FMTsection pm_section, bool p_throwError=true) const
Check if a value is a valid yield.
bool tryOpening(std::ofstream &stream, const std::string &location) const
Open an output stream at a given location.
std::string _setSpec(Core::FMTsection section, Core::FMTkwor key, const Core::FMTYields &ylds, const Core::FMTConstants &constants, Core::FMTSpec &spec, const std::string &line)
Set the specification of a spec and return the rest of the line.
std::string createSubDirectory(const std::string &p_Directory, const std::string &p_SubDirectory)
Create a sub directory in directory.
bool tryOpening(const std::ifstream &stream, const std::string &location) const
Open an input stream at a given location.
static const boost::regex m_OPERATORS
A regex for capturing simple operators.
Definition: FMTParser.h:247
std::vector< std::vector< std::string > > readCsv(const std::string &location, const char &separator)
Read a CSV file from a location using a separator.
bool _isAct(Core::FMTsection section, const std::vector< Core::FMTAction > &actions, std::string action) const
Check if a value is an action.
void _setPeriodWithBounds(Core::FMTSpec &p_spec, const std::string &p_lower, const std::string &p_upper, const Core::FMTConstants &p_constants) const
Set the periods of a spec from a lower and upper bound.
std::array< std::string, 5 > getBaseOperators() const
Return the operators normally found in the different sections.
static const boost::regex m_PRIMARY
A regex for capturing the primary file.
Definition: FMTParser.h:250
std::string _upper(const std::string &lowercases) const
Convert a string to upper case.
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
Fill up the field ids of the themes, age, area and lock of a layer.
T _getNum(const std::string &value, bool omitnumtest=false) const
Return a numeric value from a string.
void _clearComments(std::string &line) const
Clear the comments from a line and fill up the comment member.
OGRLayer * createLayer(GDALDataset *dataset, const std::string &name, std::vector< std::string > creationoptions=std::vector< std::string >()) const
Create a layer in a dataset, deleting any existing layer with the same name.
std::vector< std::string > getGDALVectorDriverNames(bool testcreation=true) const
Return a vector of GDAL vector driver names.
boost::recursive_mutex m_MTX
Mutex for multi-threading.
Definition: FMTParser.h:229
bool _isForLoops(const std::string &p_line) const
Return true if a line is a for loop header.
std::vector< std::vector< std::string > > getGDALExtensions(const char *spatialtype, bool testcreation=true) const
Return the GDAL extensions organized by driver for a given spatial type.
void setHeader(const std::string &header)
Set a string as the header written when the file is opened in write mode, replacing the comment attri...
GDALRasterBand * _getOverview(GDALRasterBand *band, int view=1) const
Return an overview of a raster band.
std::vector< std::vector< std::string > > getGDALRasterDriverExtensions(bool testcreation=true) const
Return a vector of GDAL raster driver extensions organized by driver.
OGRLayer * _getLayer(GDALDataset *dataset, int id) const
Open a GDAL layer from a vector dataset by its id, throwing on error.
std::queue< FMTLineInfo > _processForLoopsNInclude(const std::vector< Core::FMTTheme > &p_themes, const Core::FMTConstants &p_cons, std::queue< FMTLineInfo >p_AllLines) const
Process the for loops and include lines.
static Core::FMTsection fromExtension(const std::string &p_Extension)
Return the FMTsection matching the extension of a file.
static const boost::regex m_AYLD_AGE
A regex for capturing age or yield specification.
Definition: FMTParser.h:241
std::vector< std::string > regexLoop(const boost::regex &cutregex, std::string &str) const
Capture a for loop from a line and return the variables to loop on.
bool _isNum(const std::string &value, const Core::FMTConstants &constant, bool throwerror=true) const
Return true if the value is a number, raising if it is not a valid number when throwerror is true.
GDALDataset * _createDataset(const std::string &location, const Spatial::FMTLayer< T > &layer, const int datatypeid, std::string format="GTiff") const
Create an empty GDAL dataset for a layer.
virtual std::queue< FMTLineInfo > _getCleanLinewfor(std::ifstream &p_stream, const std::vector< Core::FMTTheme > &p_themes, const Core::FMTConstants &p_cons) const
Get a clean line considering for loops when reading the file.
int m_constrePlacement
Number of constant replaced by a numerical number.
Definition: FMTParser.h:253
Core::FMTPerBounds _getPerBound(const std::string &p_lower, const std::string &p_upper, const Core::FMTConstants &p_constants) const
Return the period bound of an input.
void _setCategories(GDALRasterBand *band, const std::vector< std::string > &categories) const
Write the categories in the band information.
int m_line
Line id at which the parser is reading the values.
Definition: FMTParser.h:256
GDALDataset * createOGRDataset(std::string location=std::string(), std::string gdaldrivername="CSV") const
Create an OGR vector dataset based on a driver name and a location.
static const boost::regex m_BOUNDS
A regex for capturing bounds for yield or age specification.
Definition: FMTParser.h:244
std::string _getLine(std::queue< FMTLineInfo > &p_Lines) const
Get and pop the front line of a queue.
bool _tryFillNumber(T &number, const std::string &value, const Core::FMTConstants &constant, int period=0) const
Try to get a number from a string, returning false on failure.
static const boost::regex m_SEPARATOR
A regex for splitting general string.
Definition: FMTParser.h:226
bool _isForLoopsEnd(const std::string &p_line) const
Return true if a line is the end of a for loop.
std::string m_comment
Comment found in the actual line read by the parser.
Definition: FMTParser.h:259
virtual ~FMTParser()=default
Default destructor for FMTParser.
Definition: FMTParser.h:54
The Core namespace provides classes for simulating stands/strata growth/harvest through time.
Definition: FMTAction.h:34
FMTsection
Enumerator describing the sections of a Woodstock model.
Definition: FMTutility.h:32
FMTkwor
Enumerator describing the source or target keyword of a transition.
Definition: FMTutility.h:52
Namespace handling all FMT's parsers. Everything related to I/O should be located in this namespace.
Definition: FMTActionParser.h:27
Definition: FMTAreaParser.h:40
Definition: FMTParser.h:58