Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: OpenGIS Simple Features Reference Implementation
4 : * Purpose: Some private helper functions and stuff for OGR implementation.
5 : * Author: Frank Warmerdam, warmerdam@pobox.com
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 1999, Frank Warmerdam
9 : * Copyright (c) 2008-2014, Even Rouault <even dot rouault at spatialys.com>
10 : *
11 : * SPDX-License-Identifier: MIT
12 : ****************************************************************************/
13 :
14 : #ifndef OGR_P_H_INCLUDED
15 : #define OGR_P_H_INCLUDED
16 :
17 : /* -------------------------------------------------------------------- */
18 : /* Include the common portability library ... lets us do lots */
19 : /* of stuff easily. */
20 : /* -------------------------------------------------------------------- */
21 :
22 : #include "cpl_string.h"
23 : #include "cpl_conv.h"
24 : #include "cpl_minixml.h"
25 :
26 : #include "ogr_core.h"
27 :
28 : #include <limits>
29 : #include <string_view>
30 :
31 : class OGRGeometry;
32 : class OGRFieldDefn;
33 :
34 : /* A default name for the default geometry column, instead of '' */
35 : #define OGR_GEOMETRY_DEFAULT_NON_EMPTY_NAME "_ogr_geometry_"
36 :
37 : #ifdef CPL_MSB
38 : #define OGR_SWAP(x) (x == wkbNDR)
39 : #else
40 : #define OGR_SWAP(x) (x == wkbXDR)
41 : #endif
42 :
43 : /* PostGIS 1.X has non standard codes for the following geometry types */
44 : #define POSTGIS15_CURVEPOLYGON 13 /* instead of 10 */
45 : #define POSTGIS15_MULTICURVE 14 /* instead of 11 */
46 : #define POSTGIS15_MULTISURFACE 15 /* instead of 12 */
47 :
48 : /* Has been deprecated. Can only be used in very specific circumstances */
49 : #ifdef GDAL_COMPILATION
50 : #define wkb25DBitInternalUse 0x80000000
51 : #endif
52 :
53 : /* -------------------------------------------------------------------- */
54 : /* helper function for parsing well known text format vector objects.*/
55 : /* -------------------------------------------------------------------- */
56 :
57 : #ifdef OGR_GEOMETRY_H_INCLUDED
58 : #define OGR_WKT_TOKEN_MAX 64
59 :
60 : const char CPL_DLL *OGRWktReadToken(const char *pszInput, char *pszToken);
61 :
62 : const char CPL_DLL *OGRWktReadPoints(const char *pszInput,
63 : OGRRawPoint **ppaoPoints, double **ppadfZ,
64 : int *pnMaxPoints, int *pnReadPoints);
65 :
66 : const char CPL_DLL *
67 : OGRWktReadPointsM(const char *pszInput, OGRRawPoint **ppaoPoints,
68 : double **ppadfZ, double **ppadfM,
69 : int *flags, /* geometry flags, are we expecting Z, M, or both;
70 : may change due to input */
71 : int *pnMaxPoints, int *pnReadPoints);
72 :
73 : void CPL_DLL OGRMakeWktCoordinate(char *, double, double, double, int);
74 : std::string CPL_DLL OGRMakeWktCoordinate(double, double, double, int,
75 : const OGRWktOptions &opts);
76 : void CPL_DLL OGRMakeWktCoordinateM(char *, double, double, double, double, bool,
77 : bool);
78 : std::string CPL_DLL OGRMakeWktCoordinateM(double, double, double, double, bool,
79 : bool, const OGRWktOptions &opts);
80 :
81 : #endif
82 :
83 : void CPL_DLL OGRFormatDouble(char *pszBuffer, int nBufferLen, double dfVal,
84 : char chDecimalSep, int nPrecision = 15,
85 : char chConversionSpecifier = 'f');
86 :
87 : #ifdef OGR_GEOMETRY_H_INCLUDED
88 : std::string CPL_DLL OGRFormatDouble(double val, const OGRWktOptions &opts,
89 : int nDimIdx);
90 : #endif
91 :
92 : int OGRFormatFloat(char *pszBuffer, int nBufferLen, float fVal, int nPrecision,
93 : char chConversionSpecifier);
94 :
95 : /* -------------------------------------------------------------------- */
96 : /* Date-time parsing and processing functions */
97 : /* -------------------------------------------------------------------- */
98 :
99 : /* Internal use by OGR drivers only, CPL_DLL is just there in case */
100 : /* they are compiled as plugins */
101 :
102 : int CPL_DLL OGRTimezoneToTZFlag(const char *pszTZ,
103 : bool bEmitErrorIfUnhandledFormat);
104 : std::string CPL_DLL OGRTZFlagToTimezone(int nTZFlag,
105 : const char *pszUTCRepresentation);
106 :
107 : int CPL_DLL OGRGetDayOfWeek(int day, int month, int year);
108 : int CPL_DLL OGRParseXMLDateTime(const char *pszXMLDateTime, OGRField *psField);
109 : int CPL_DLL OGRParseRFC822DateTime(const char *pszRFC822DateTime,
110 : OGRField *psField);
111 : char CPL_DLL *OGRGetRFC822DateTime(const OGRField *psField);
112 : char CPL_DLL *OGRGetXMLDateTime(const OGRField *psField);
113 : char CPL_DLL *OGRGetXMLDateTime(const OGRField *psField,
114 : bool bAlwaysMillisecond);
115 : // 30 = strlen("YYYY-MM-DDThh:mm:ss.sss+hh:mm") + 1
116 : #define OGR_SIZEOF_ISO8601_DATETIME_BUFFER 30
117 : int CPL_DLL
118 : OGRGetISO8601DateTime(const OGRField *psField, bool bAlwaysMillisecond,
119 : char szBuffer[OGR_SIZEOF_ISO8601_DATETIME_BUFFER]);
120 :
121 : /** Precision of formatting */
122 : enum class OGRISO8601Precision
123 : {
124 : /** Automated mode: millisecond included if non zero, otherwise truncated at second */
125 : AUTO,
126 : /** Always include millisecond */
127 : MILLISECOND,
128 : /** Always include second, but no millisecond */
129 : SECOND,
130 : /** Always include minute, but no second */
131 : MINUTE
132 : };
133 :
134 : /** Configuration of the ISO8601 formatting output */
135 : struct OGRISO8601Format
136 : {
137 : /** Precision of formatting */
138 : OGRISO8601Precision ePrecision;
139 : };
140 :
141 : int CPL_DLL
142 : OGRGetISO8601DateTime(const OGRField *psField, const OGRISO8601Format &sFormat,
143 : char szBuffer[OGR_SIZEOF_ISO8601_DATETIME_BUFFER]);
144 : char CPL_DLL *OGRGetXML_UTF8_EscapedString(const char *pszString);
145 :
146 : #ifdef GDAL_COMPILATION
147 : bool CPL_DLL OGRParseDateTimeYYYYMMDDTHHMMZ(std::string_view sInput,
148 : OGRField *psField);
149 : bool CPL_DLL OGRParseDateTimeYYYYMMDDTHHMMSSZ(std::string_view sInput,
150 : OGRField *psField);
151 : bool CPL_DLL OGRParseDateTimeYYYYMMDDTHHMMSSsssZ(std::string_view sInput,
152 : OGRField *psField);
153 : #endif
154 :
155 : int OGRCompareDate(const OGRField *psFirstTuple,
156 : const OGRField *psSecondTuple); /* used by ogr_gensql.cpp and
157 : ogrfeaturequery.cpp */
158 :
159 : /* General utility option processing. */
160 : int CPL_DLL OGRGeneralCmdLineProcessor(int nArgc, char ***ppapszArgv,
161 : int nOptions);
162 :
163 : /************************************************************************/
164 : /* Support for special attributes (feature query and selection) */
165 : /************************************************************************/
166 : #define SPF_FID 0
167 : #define SPF_OGR_GEOMETRY 1
168 : #define SPF_OGR_STYLE 2
169 : #define SPF_OGR_GEOM_WKT 3
170 : #define SPF_OGR_GEOM_AREA 4
171 : #define SPECIAL_FIELD_COUNT 5
172 :
173 : extern const char *const SpecialFieldNames[SPECIAL_FIELD_COUNT];
174 :
175 : /************************************************************************/
176 : /* Some SRS related stuff, search in SRS data files. */
177 : /************************************************************************/
178 :
179 : OGRErr CPL_DLL OSRGetEllipsoidInfo(int, char **, double *, double *);
180 :
181 : /* Fast atof function */
182 : double OGRFastAtof(const char *pszStr);
183 :
184 : OGRErr CPL_DLL OGRCheckPermutation(const int *panPermutation, int nSize);
185 :
186 : /************************************************************************/
187 : /* PostGIS EWKB encoding */
188 : /************************************************************************/
189 :
190 : OGRGeometry CPL_DLL *OGRGeometryFromEWKB(GByte *pabyWKB, int nLength,
191 : int *pnSRID, int bIsPostGIS1_EWKB);
192 : OGRGeometry CPL_DLL *OGRGeometryFromHexEWKB(const char *pszBytea, int *pnSRID,
193 : int bIsPostGIS1_EWKB);
194 : char CPL_DLL *OGRGeometryToHexEWKB(const OGRGeometry *poGeometry, int nSRSId,
195 : int nPostGISMajor, int nPostGISMinor);
196 :
197 : /************************************************************************/
198 : /* WKB Type Handling encoding */
199 : /************************************************************************/
200 :
201 : OGRErr CPL_DLL OGRReadWKBGeometryType(const unsigned char *pabyData,
202 : OGRwkbVariant wkbVariant,
203 : OGRwkbGeometryType *eGeometryType);
204 :
205 : /************************************************************************/
206 : /* WKT Type Handling encoding */
207 : /************************************************************************/
208 :
209 : OGRErr CPL_DLL OGRReadWKTGeometryType(const char *pszWKT,
210 : OGRwkbGeometryType *peGeometryType);
211 :
212 : /************************************************************************/
213 : /* Other */
214 : /************************************************************************/
215 :
216 : void CPL_DLL OGRUpdateFieldType(OGRFieldDefn *poFDefn, OGRFieldType eNewType,
217 : OGRFieldSubType eNewSubType);
218 :
219 : /************************************************************************/
220 : /* OGRRoundValueIEEE754() */
221 : /************************************************************************/
222 :
223 : /** Set to zero least significants bits of a double precision floating-point
224 : * number (passed as an integer), taking into account a desired bit precision.
225 : *
226 : * @param nVal Integer representation of a IEEE754 double-precision number.
227 : * @param nBitsPrecision Desired precision (number of bits after integral part)
228 : * @return quantized nVal.
229 : * @since GDAL 3.9
230 : */
231 : inline uint64_t OGRRoundValueIEEE754(uint64_t nVal,
232 : int nBitsPrecision) CPL_WARN_UNUSED_RESULT;
233 :
234 104 : inline uint64_t OGRRoundValueIEEE754(uint64_t nVal, int nBitsPrecision)
235 : {
236 104 : constexpr int MANTISSA_SIZE = std::numeric_limits<double>::digits - 1;
237 104 : constexpr int MAX_EXPONENT = std::numeric_limits<double>::max_exponent;
238 : #if __cplusplus >= 201703L
239 : static_assert(MANTISSA_SIZE == 52);
240 : static_assert(MAX_EXPONENT == 1024);
241 : #endif
242 : // Extract the binary exponent from the IEEE754 representation
243 104 : const int nExponent =
244 104 : ((nVal >> MANTISSA_SIZE) & (2 * MAX_EXPONENT - 1)) - (MAX_EXPONENT - 1);
245 : // Add 1 to round-up and the desired precision
246 104 : const int nBitsRequired = 1 + nExponent + nBitsPrecision;
247 : // Compute number of nullified bits
248 104 : int nNullifiedBits = MANTISSA_SIZE - nBitsRequired;
249 : // this will also capture NaN and Inf since nExponent = 1023,
250 : // and thus nNullifiedBits < 0
251 104 : if (nNullifiedBits <= 0)
252 2 : return nVal;
253 102 : if (nNullifiedBits >= MANTISSA_SIZE)
254 2 : nNullifiedBits = MANTISSA_SIZE;
255 102 : nVal >>= nNullifiedBits;
256 102 : nVal <<= nNullifiedBits;
257 102 : return nVal;
258 : }
259 :
260 : /************************************************************************/
261 : /* OGRRoundCoordinatesIEEE754XYValues() */
262 : /************************************************************************/
263 :
264 : /** Quantize XY values.
265 : *
266 : * @since GDAL 3.9
267 : */
268 : template <int SPACING>
269 703576 : inline void OGRRoundCoordinatesIEEE754XYValues(int nBitsPrecision,
270 : GByte *pabyBase, size_t nPoints)
271 : {
272 : // Note: we use SPACING as template for improved code generation.
273 :
274 703576 : if (nBitsPrecision != INT_MIN)
275 : {
276 42 : for (size_t i = 0; i < nPoints; i++)
277 : {
278 : uint64_t nVal;
279 :
280 29 : memcpy(&nVal, pabyBase + SPACING * i, sizeof(uint64_t));
281 29 : nVal = OGRRoundValueIEEE754(nVal, nBitsPrecision);
282 29 : memcpy(pabyBase + SPACING * i, &nVal, sizeof(uint64_t));
283 :
284 29 : memcpy(&nVal, pabyBase + sizeof(uint64_t) + SPACING * i,
285 : sizeof(uint64_t));
286 29 : nVal = OGRRoundValueIEEE754(nVal, nBitsPrecision);
287 29 : memcpy(pabyBase + sizeof(uint64_t) + SPACING * i, &nVal,
288 : sizeof(uint64_t));
289 : }
290 : }
291 703576 : }
292 :
293 : /************************************************************************/
294 : /* OGRRoundCoordinatesIEEE754() */
295 : /************************************************************************/
296 :
297 : /** Quantize Z or M values.
298 : *
299 : * @since GDAL 3.9
300 : */
301 : template <int SPACING>
302 216705 : inline void OGRRoundCoordinatesIEEE754(int nBitsPrecision, GByte *pabyBase,
303 : size_t nPoints)
304 : {
305 216705 : if (nBitsPrecision != INT_MIN)
306 : {
307 52 : for (size_t i = 0; i < nPoints; i++)
308 : {
309 : uint64_t nVal;
310 :
311 34 : memcpy(&nVal, pabyBase + SPACING * i, sizeof(uint64_t));
312 34 : nVal = OGRRoundValueIEEE754(nVal, nBitsPrecision);
313 34 : memcpy(pabyBase + SPACING * i, &nVal, sizeof(uint64_t));
314 : }
315 : }
316 216705 : }
317 :
318 : /* -------------------------------------------------------------------- */
319 : /* helper functions for string escaping. */
320 : /* -------------------------------------------------------------------- */
321 :
322 : /** Replace all occurrences of ch by it repeated twice.
323 : * Typically used for SQL string literal or identifier escaping.
324 : */
325 : std::string CPL_DLL OGRDuplicateCharacter(const std::string &osStr, char ch);
326 :
327 : #endif /* ndef OGR_P_H_INCLUDED */
|