Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: WAsP Translator
4 : * Purpose: Definition of classes for OGR .map driver.
5 : * Author: Vincent Mora, vincent dot mora at oslandia dot com
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2014, Oslandia <info at oslandia dot com>
9 : *
10 : * Permission is hereby granted, free of charge, to any person obtaining a
11 : * copy of this software and associated documentation files (the "Software"),
12 : * to deal in the Software without restriction, including without limitation
13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 : * and/or sell copies of the Software, and to permit persons to whom the
15 : * Software is furnished to do so, subject to the following conditions:
16 : *
17 : * The above copyright notice and this permission notice shall be included
18 : * in all copies or substantial portions of the Software.
19 : *
20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 : * DEALINGS IN THE SOFTWARE.
27 : ****************************************************************************/
28 :
29 : #ifndef OGR_WASP_H_INCLUDED
30 : #define OGR_WASP_H_INCLUDED
31 :
32 : #include "ogrsf_frmts.h"
33 :
34 : #include <memory>
35 : #include <fstream>
36 : #include <vector>
37 :
38 : /************************************************************************/
39 : /* OGRWAsPLayer */
40 : /************************************************************************/
41 :
42 : class OGRWAsPLayer final : public OGRLayer,
43 : public OGRGetNextFeatureThroughRaw<OGRWAsPLayer>
44 : {
45 : /* stuff for polygon processing */
46 :
47 : /* note if shared ptr are available, replace the ptr in the two structs */
48 : /* and remove deletion of array elements in ~OGRWAsPLayer() */
49 : struct Zone
50 : {
51 : OGREnvelope oEnvelope;
52 : OGRPolygon *poPolygon;
53 : double dfZ;
54 : };
55 :
56 : struct Boundary
57 : {
58 : OGRLineString *poLine;
59 : double dfLeft;
60 : double dfRight;
61 : };
62 :
63 : GDALDataset *m_poDS = nullptr;
64 : const bool bMerge;
65 : std::vector<Zone> oZones;
66 : std::vector<Boundary> oBoundaries;
67 :
68 45 : static bool isEqual(const double &dfRouhness1, const double &dfRouhness2)
69 : {
70 45 : return fabs(dfRouhness1 - dfRouhness2) < 1e-3;
71 : }
72 :
73 : /* end of stuff for polygon processing */
74 :
75 : int iFeatureCount;
76 :
77 : const CPLString sName;
78 : VSILFILE *hFile;
79 :
80 : /* for roughness zone, two fields for linestrings (left/right), one for
81 : * polygons */
82 : /* for elevation, one field (height) */
83 : const CPLString sFirstField;
84 : const CPLString sSecondField;
85 : const CPLString sGeomField;
86 : int iFirstFieldIdx;
87 : int iSecondFieldIdx;
88 : int iGeomFieldIdx;
89 :
90 : OGRFeatureDefn *poLayerDefn;
91 : OGRSpatialReference *poSpatialReference;
92 :
93 : vsi_l_offset iOffsetFeatureBegin;
94 :
95 : enum OpenMode
96 : {
97 : READ_ONLY,
98 : WRITE_ONLY
99 : };
100 :
101 : OpenMode eMode;
102 :
103 : std::unique_ptr<double> pdfTolerance;
104 : std::unique_ptr<double> pdfAdjacentPointTolerance;
105 : std::unique_ptr<double> pdfPointToCircleRadius;
106 :
107 : OGRErr WriteRoughness(OGRLineString *, const double &dfZleft,
108 : const double &dfZright);
109 : OGRErr WriteRoughness(OGRPolygon *, const double &dfZ);
110 : OGRErr WriteRoughness(OGRGeometry *, const double &dfZleft,
111 : const double &dfZright);
112 :
113 : OGRErr WriteElevation(OGRLineString *, const double &dfZ);
114 : OGRErr WriteElevation(OGRGeometry *, const double &dfZ);
115 :
116 : static double AvgZ(OGRLineString *poGeom);
117 : static double AvgZ(OGRPolygon *poGeom);
118 : static double AvgZ(OGRGeometryCollection *poGeom);
119 : static double AvgZ(OGRGeometry *poGeom);
120 :
121 : /* return a simplified line (caller is responsible for resource)
122 : *
123 : * if pdfTolerance is not NULL,
124 : * calls GEOS simplify
125 : *
126 : * if pdfAdjacentPointTolerance is not NULL,
127 : * remove consecutive points that are less than tolerance apart
128 : * in x and y
129 : *
130 : * if pdfPointToCircleRadius is not NULL,
131 : * lines that have been simplified to a point are converted to a 8 pt
132 : * circle
133 : * */
134 : OGRLineString *Simplify(const OGRLineString &line) const;
135 :
136 : OGRFeature *GetNextRawFeature();
137 :
138 : public:
139 : /* For writing */
140 : /* Takes ownership of poTolerance */
141 : OGRWAsPLayer(GDALDataset *poDS, const char *pszName, VSILFILE *hFile,
142 : OGRSpatialReference *poSpatialRef,
143 : const CPLString &sFirstField, const CPLString &sSecondField,
144 : const CPLString &sGeomField, bool bMerge, double *pdfTolerance,
145 : double *pdfAdjacentPointTolerance,
146 : double *pdfPointToCircleRadius);
147 :
148 : /* For reading */
149 : OGRWAsPLayer(GDALDataset *poDS, const char *pszName, VSILFILE *hFile,
150 : OGRSpatialReference *poSpatialRef);
151 :
152 : virtual ~OGRWAsPLayer();
153 :
154 240 : virtual OGRFeatureDefn *GetLayerDefn() override
155 : {
156 240 : return poLayerDefn;
157 : }
158 :
159 : virtual void ResetReading() override;
160 : virtual int TestCapability(const char *) override;
161 :
162 : virtual OGRErr CreateField(const OGRFieldDefn *poField,
163 : int bApproxOK = TRUE) override;
164 : virtual OGRErr CreateGeomField(const OGRGeomFieldDefn *poGeomField,
165 : int bApproxOK = TRUE) override;
166 :
167 : virtual OGRErr ICreateFeature(OGRFeature *poFeature) override;
168 :
169 19 : DEFINE_GET_NEXT_FEATURE_THROUGH_RAW(OGRWAsPLayer)
170 :
171 10 : virtual const char *GetName() override
172 : {
173 10 : return sName.c_str();
174 : }
175 :
176 10 : GDALDataset *GetDataset() override
177 : {
178 10 : return m_poDS;
179 : }
180 : };
181 :
182 : /************************************************************************/
183 : /* OGRWAsPDataSource */
184 : /************************************************************************/
185 :
186 : class OGRWAsPDataSource final : public OGRDataSource
187 : {
188 : CPLString sFilename;
189 : VSILFILE *hFile;
190 : std::unique_ptr<OGRWAsPLayer> oLayer;
191 :
192 : void GetOptions(CPLString &sFirstField, CPLString &sSecondField,
193 : CPLString &sGeomField, bool &bMerge) const;
194 :
195 : public:
196 : /** @note takes ownership of hFile (i.e. responsibility for closing) */
197 : OGRWAsPDataSource(const char *pszName, VSILFILE *hFile);
198 : virtual ~OGRWAsPDataSource();
199 :
200 69 : virtual const char *GetName() override
201 : {
202 69 : return sFilename.c_str();
203 : }
204 :
205 1 : virtual int GetLayerCount() override
206 : {
207 1 : return oLayer.get() ? 1 : 0;
208 : }
209 :
210 : virtual OGRLayer *GetLayer(int) override;
211 : virtual OGRLayer *GetLayerByName(const char *) override;
212 :
213 : OGRLayer *ICreateLayer(const char *pszName,
214 : const OGRGeomFieldDefn *poGeomFieldDefn,
215 : CSLConstList papszOptions) override;
216 :
217 : virtual int TestCapability(const char *) override;
218 : OGRErr Load(bool bSilent = false);
219 : };
220 :
221 : /************************************************************************/
222 : /* OGRWAsPDriver */
223 : /************************************************************************/
224 :
225 : class OGRWAsPDriver final : public OGRSFDriver
226 : {
227 :
228 : public:
229 2308 : ~OGRWAsPDriver()
230 1154 : {
231 2308 : }
232 :
233 2739 : virtual const char *GetName() override
234 : {
235 2739 : return "WAsP";
236 : }
237 :
238 : virtual OGRDataSource *Open(const char *, int) override;
239 :
240 : virtual OGRDataSource *CreateDataSource(const char *pszName,
241 : char ** = nullptr) override;
242 :
243 : virtual OGRErr DeleteDataSource(const char *pszName) override;
244 :
245 : virtual int TestCapability(const char *) override;
246 : };
247 :
248 : #endif /* ndef OGR_WASP_H_INCLUDED */
|