Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: ILWIS Driver
4 : * Purpose: GDALDataset driver for ILWIS translator for read/write support.
5 : * Author: Lichun Wang, lichun@itc.nl
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2004, ITC
9 : *
10 : * SPDX-License-Identifier: MIT
11 : ****************************************************************************/
12 :
13 : #ifndef ILWISDATASET_H_INCLUDED
14 : #define ILWISDATASET_H_INCLUDED
15 :
16 : #include "gdal_pam.h"
17 : #include "cpl_csv.h"
18 : #include "ogr_spatialref.h"
19 :
20 : #include <cstdio>
21 : #include <cstdlib>
22 :
23 : #include <map>
24 : #include <string>
25 :
26 : #define shUNDEF -32767
27 : #define iUNDEF -2147483647
28 : #define flUNDEF ((float)-1e38)
29 : #define rUNDEF ((double)-1e308)
30 :
31 : namespace GDAL
32 : {
33 :
34 : enum ilwisStoreType
35 : {
36 : stByte,
37 : stInt,
38 : stLong,
39 : stFloat,
40 : stReal
41 : };
42 :
43 : class ValueRange
44 : {
45 : public:
46 : ValueRange(double min, double max); // step = 1
47 : ValueRange(double min, double max, double step);
48 : explicit ValueRange(const std::string &str);
49 : std::string ToString() const;
50 :
51 : ilwisStoreType get_NeededStoreType() const
52 : {
53 : return st;
54 : }
55 :
56 16884 : double get_rLo() const
57 : {
58 16884 : return _rLo;
59 : }
60 :
61 16792 : double get_rHi() const
62 : {
63 16792 : return _rHi;
64 : }
65 :
66 12 : double get_rStep() const
67 : {
68 12 : return _rStep;
69 : }
70 :
71 0 : double get_rRaw0() const
72 : {
73 0 : return _r0;
74 : }
75 :
76 0 : int get_iDec() const
77 : {
78 0 : return _iDec;
79 : }
80 :
81 : double rValue(int raw) const;
82 : int iRaw(double value) const;
83 :
84 : private:
85 : void init(double rRaw0);
86 : void init();
87 : double _rLo, _rHi;
88 : double _rStep;
89 : int _iDec;
90 : double _r0;
91 : int iRawUndef;
92 : short _iWidth;
93 : ilwisStoreType st;
94 : };
95 :
96 : /************************************************************************/
97 : /* ILWISInfo */
98 : /************************************************************************/
99 :
100 : struct ILWISInfo
101 : {
102 80 : ILWISInfo() : bUseValueRange(false), vr(0, 0), stStoreType(stByte)
103 : {
104 80 : }
105 :
106 : bool bUseValueRange;
107 : ValueRange vr;
108 : ilwisStoreType stStoreType;
109 : std::string stDomain;
110 : };
111 :
112 : /************************************************************************/
113 : /* ILWISRasterBand */
114 : /************************************************************************/
115 :
116 : class ILWISDataset;
117 :
118 : class ILWISRasterBand final : public GDALPamRasterBand
119 : {
120 : friend class ILWISDataset;
121 :
122 : public:
123 : VSILFILE *fpRaw;
124 : ILWISInfo psInfo;
125 : int nSizePerPixel;
126 :
127 : ILWISRasterBand(ILWISDataset *, int, const std::string &sBandNameIn);
128 : ~ILWISRasterBand() override;
129 : CPLErr GetILWISInfo(const std::string &pszFileName);
130 : void ILWISOpen(const std::string &pszFilename);
131 :
132 : CPLErr IReadBlock(int, int, void *) override;
133 : CPLErr IWriteBlock(int, int, void *) override;
134 : double GetNoDataValue(int *pbSuccess) override;
135 :
136 : private:
137 : void FillWithNoData(void *pImage);
138 : void SetValue(void *pImage, int i, double rV);
139 : double GetValue(void *pImage, int i);
140 : void ReadValueDomainProperties(const std::string &pszFileName);
141 : };
142 :
143 : /************************************************************************/
144 : /* ILWISDataset */
145 : /************************************************************************/
146 : class ILWISDataset final : public GDALPamDataset
147 : {
148 : friend class ILWISRasterBand;
149 : CPLString osFileName;
150 : std::string pszIlwFileName;
151 : OGRSpatialReference m_oSRS{};
152 : GDALGeoTransform m_gt{};
153 : int bGeoDirty;
154 : int bNewDataset; /* product of Create() */
155 : std::string pszFileType; // indicating the input dataset: Map/MapList
156 : CPLErr ReadProjection(const std::string &csyFileName);
157 : CPLErr WriteProjection();
158 : void WriteGeoReference();
159 : void CollectTransformCoef(std::string &pszRefFile);
160 :
161 : public:
162 : ILWISDataset();
163 : ~ILWISDataset() override;
164 :
165 : static GDALDataset *Open(GDALOpenInfo *);
166 :
167 : static GDALDataset *CreateCopy(const char *pszFilename,
168 : GDALDataset *poSrcDS, int bStrict,
169 : CSLConstList papszOptions,
170 : GDALProgressFunc pfnProgress,
171 : void *pProgressData);
172 :
173 : static GDALDataset *Create(const char *pszFilename, int nXSize, int nYSize,
174 : int nBands, GDALDataType eType, CSLConstList);
175 :
176 : CPLErr GetGeoTransform(GDALGeoTransform >) const override;
177 : CPLErr SetGeoTransform(const GDALGeoTransform >) override;
178 :
179 : const OGRSpatialReference *GetSpatialRef() const override;
180 : CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override;
181 :
182 : CPLErr FlushCache(bool bAtClosing) override;
183 : };
184 :
185 : // IniFile.h: interface for the IniFile class.
186 : //
187 : //////////////////////////////////////////////////////////////////////
188 :
189 : class CompareAsNum
190 : {
191 : public:
192 : bool operator()(const std::string &, const std::string &) const;
193 : };
194 :
195 : typedef std::map<std::string, std::string> SectionEntries;
196 : typedef std::map<std::string, SectionEntries *> Sections;
197 :
198 : class IniFile final
199 : {
200 : public:
201 : explicit IniFile(const std::string &filename);
202 : ~IniFile();
203 :
204 : void SetKeyValue(const std::string §ion, const std::string &key,
205 : const std::string &value);
206 : std::string GetKeyValue(const std::string §ion, const std::string &key);
207 :
208 : void RemoveKeyValue(const std::string §ion, const std::string &key);
209 : void RemoveSection(const std::string §ion);
210 :
211 : private:
212 : std::string filename;
213 : Sections sections;
214 : bool bChanged;
215 :
216 : void Load();
217 : void Store();
218 : };
219 :
220 : std::string ReadElement(const std::string §ion, const std::string &entry,
221 : const std::string &filename);
222 : bool WriteElement(const std::string &sSection, const std::string &sEntry,
223 : const std::string &fn, const std::string &sValue);
224 : bool WriteElement(const std::string &sSection, const std::string &sEntry,
225 : const std::string &fn, int nValue);
226 : bool WriteElement(const std::string &sSection, const std::string &sEntry,
227 : const std::string &fn, double dValue);
228 :
229 : } // namespace GDAL
230 :
231 : #endif // ILWISDATASET_H_INCLUDED
|