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