GDAL
ogr_p.h
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
31class OGRGeometry;
32class 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
60const char CPL_DLL *OGRWktReadToken(const char *pszInput, char *pszToken);
61
62const char CPL_DLL *OGRWktReadPoints(const char *pszInput,
63 OGRRawPoint **ppaoPoints, double **ppadfZ,
64 int *pnMaxPoints, int *pnReadPoints);
65
66const char CPL_DLL *
67OGRWktReadPointsM(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
73void CPL_DLL OGRMakeWktCoordinate(char *, double, double, double, int);
74std::string CPL_DLL OGRMakeWktCoordinate(double, double, double, int,
75 const OGRWktOptions &opts);
76void CPL_DLL OGRMakeWktCoordinateM(char *, double, double, double, double,
78std::string CPL_DLL OGRMakeWktCoordinateM(double, double, double, double,
80 const OGRWktOptions &opts);
81
82#endif
83
84void CPL_DLL OGRFormatDouble(char *pszBuffer, int nBufferLen, double dfVal,
85 char chDecimalSep, int nPrecision = 15,
86 char chConversionSpecifier = 'f');
87
88#ifdef OGR_GEOMETRY_H_INCLUDED
89std::string CPL_DLL OGRFormatDouble(double val, const OGRWktOptions &opts,
90 int nDimIdx);
91#endif
92
93int OGRFormatFloat(char *pszBuffer, int nBufferLen, float fVal, int nPrecision,
94 char chConversionSpecifier);
95
96/* -------------------------------------------------------------------- */
97/* Date-time parsing and processing functions */
98/* -------------------------------------------------------------------- */
99
100/* Internal use by OGR drivers only, CPL_DLL is just there in case */
101/* they are compiled as plugins */
102
103int CPL_DLL OGRTimezoneToTZFlag(const char *pszTZ,
104 bool bEmitErrorIfUnhandledFormat);
105std::string CPL_DLL OGRTZFlagToTimezone(int nTZFlag,
106 const char *pszUTCRepresentation);
107
108int CPL_DLL OGRGetDayOfWeek(int day, int month, int year);
109int CPL_DLL OGRParseXMLDateTime(const char *pszXMLDateTime, OGRField *psField);
110int CPL_DLL OGRParseRFC822DateTime(const char *pszRFC822DateTime,
111 OGRField *psField);
112char CPL_DLL *OGRGetRFC822DateTime(const OGRField *psField);
113char CPL_DLL *OGRGetXMLDateTime(const OGRField *psField);
114char CPL_DLL *OGRGetXMLDateTime(const OGRField *psField,
115 bool bAlwaysMillisecond);
116// 30 = strlen("YYYY-MM-DDThh:mm:ss.sss+hh:mm") + 1
117#define OGR_SIZEOF_ISO8601_DATETIME_BUFFER 30
118int CPL_DLL
119OGRGetISO8601DateTime(const OGRField *psField, bool bAlwaysMillisecond,
120 char szBuffer[OGR_SIZEOF_ISO8601_DATETIME_BUFFER]);
121
123enum class OGRISO8601Precision
124{
126 AUTO,
128 MILLISECOND,
130 SECOND,
132 MINUTE
133};
134
137{
139 OGRISO8601Precision ePrecision;
140};
141
142int CPL_DLL
143OGRGetISO8601DateTime(const OGRField *psField, const OGRISO8601Format &sFormat,
144 char szBuffer[OGR_SIZEOF_ISO8601_DATETIME_BUFFER]);
145char CPL_DLL *OGRGetXML_UTF8_EscapedString(const char *pszString);
146
147#ifdef GDAL_COMPILATION
148bool CPL_DLL OGRParseDateTimeYYYYMMDDTHHMMZ(std::string_view sInput,
149 OGRField *psField);
150bool CPL_DLL OGRParseDateTimeYYYYMMDDTHHMMSSZ(std::string_view sInput,
151 OGRField *psField);
152bool CPL_DLL OGRParseDateTimeYYYYMMDDTHHMMSSsssZ(std::string_view sInput,
153 OGRField *psField);
154#endif
155
156int OGRCompareDate(const OGRField *psFirstTuple,
157 const OGRField *psSecondTuple); /* used by ogr_gensql.cpp and
158 ogrfeaturequery.cpp */
159
160bool CPL_DLL OGRIsGeoJSONMediaType(const char *pszMediaType);
161
162/* General utility option processing. */
163int CPL_DLL OGRGeneralCmdLineProcessor(int nArgc, char ***ppapszArgv,
164 int nOptions);
165
166/************************************************************************/
167/* Support for special attributes (feature query and selection) */
168/************************************************************************/
169#define SPF_FID 0
170#define SPF_OGR_GEOMETRY 1
171#define SPF_OGR_STYLE 2
172#define SPF_OGR_GEOM_WKT 3
173#define SPF_OGR_GEOM_AREA 4
174#define SPECIAL_FIELD_COUNT 5
175
176extern const char *const SpecialFieldNames[SPECIAL_FIELD_COUNT];
177
178/************************************************************************/
179/* Some SRS related stuff, search in SRS data files. */
180/************************************************************************/
181
182OGRErr CPL_DLL OSRGetEllipsoidInfo(int, char **, double *, double *);
183
184/* Fast atof function */
185double OGRFastAtof(const char *pszStr);
186
187OGRErr CPL_DLL OGRCheckPermutation(const int *panPermutation, int nSize);
188
189/************************************************************************/
190/* PostGIS EWKB encoding */
191/************************************************************************/
192
193OGRGeometry CPL_DLL *OGRGeometryFromEWKB(GByte *pabyWKB, int nLength,
194 int *pnSRID, int bIsPostGIS1_EWKB);
195OGRGeometry CPL_DLL *OGRGeometryFromHexEWKB(const char *pszBytea, int *pnSRID,
196 int bIsPostGIS1_EWKB);
197char CPL_DLL *OGRGeometryToHexEWKB(const OGRGeometry *poGeometry, int nSRSId,
198 int nPostGISMajor, int nPostGISMinor);
199
200/************************************************************************/
201/* WKB Type Handling encoding */
202/************************************************************************/
203
204OGRErr CPL_DLL OGRReadWKBGeometryType(const unsigned char *pabyData,
205 OGRwkbVariant wkbVariant,
206 OGRwkbGeometryType *eGeometryType);
207
208/************************************************************************/
209/* WKT Type Handling encoding */
210/************************************************************************/
211
212OGRErr CPL_DLL OGRReadWKTGeometryType(const char *pszWKT,
213 OGRwkbGeometryType *peGeometryType);
214
215/************************************************************************/
216/* Other */
217/************************************************************************/
218
219void CPL_DLL OGRUpdateFieldType(OGRFieldDefn *poFDefn, OGRFieldType eNewType,
220 OGRFieldSubType eNewSubType);
221
222/************************************************************************/
223/* OGRRoundValueIEEE754() */
224/************************************************************************/
225
234inline uint64_t OGRRoundValueIEEE754(uint64_t nVal,
235 int nBitsPrecision) CPL_WARN_UNUSED_RESULT;
236
237inline uint64_t OGRRoundValueIEEE754(uint64_t nVal, int nBitsPrecision)
238{
239 constexpr int MANTISSA_SIZE = std::numeric_limits<double>::digits - 1;
240 constexpr int MAX_EXPONENT = std::numeric_limits<double>::max_exponent;
241#if __cplusplus >= 201703L
242 static_assert(MANTISSA_SIZE == 52);
243 static_assert(MAX_EXPONENT == 1024);
244#endif
245 // Extract the binary exponent from the IEEE754 representation
246 const int nExponent =
247 ((nVal >> MANTISSA_SIZE) & (2 * MAX_EXPONENT - 1)) - (MAX_EXPONENT - 1);
248 // Add 1 to round-up and the desired precision
249 const int nBitsRequired = 1 + nExponent + nBitsPrecision;
250 // Compute number of nullified bits
251 int nNullifiedBits = MANTISSA_SIZE - nBitsRequired;
252 // this will also capture NaN and Inf since nExponent = 1023,
253 // and thus nNullifiedBits < 0
254 if (nNullifiedBits <= 0)
255 return nVal;
256 if (nNullifiedBits >= MANTISSA_SIZE)
257 nNullifiedBits = MANTISSA_SIZE;
258 nVal >>= nNullifiedBits;
259 nVal <<= nNullifiedBits;
260 return nVal;
261}
262
263/************************************************************************/
264/* OGRRoundCoordinatesIEEE754XYValues() */
265/************************************************************************/
266
271template <int SPACING>
272inline void OGRRoundCoordinatesIEEE754XYValues(int nBitsPrecision,
273 GByte *pabyBase, size_t nPoints)
274{
275 // Note: we use SPACING as template for improved code generation.
276
277 if (nBitsPrecision != INT_MIN)
278 {
279 for (size_t i = 0; i < nPoints; i++)
280 {
281 uint64_t nVal;
282
283 memcpy(&nVal, pabyBase + SPACING * i, sizeof(uint64_t));
284 nVal = OGRRoundValueIEEE754(nVal, nBitsPrecision);
285 memcpy(pabyBase + SPACING * i, &nVal, sizeof(uint64_t));
286
287 memcpy(&nVal, pabyBase + sizeof(uint64_t) + SPACING * i,
288 sizeof(uint64_t));
289 nVal = OGRRoundValueIEEE754(nVal, nBitsPrecision);
290 memcpy(pabyBase + sizeof(uint64_t) + SPACING * i, &nVal,
291 sizeof(uint64_t));
292 }
293 }
294}
295
296/************************************************************************/
297/* OGRRoundCoordinatesIEEE754() */
298/************************************************************************/
299
304template <int SPACING>
305inline void OGRRoundCoordinatesIEEE754(int nBitsPrecision, GByte *pabyBase,
306 size_t nPoints)
307{
308 if (nBitsPrecision != INT_MIN)
309 {
310 for (size_t i = 0; i < nPoints; i++)
311 {
312 uint64_t nVal;
313
314 memcpy(&nVal, pabyBase + SPACING * i, sizeof(uint64_t));
315 nVal = OGRRoundValueIEEE754(nVal, nBitsPrecision);
316 memcpy(pabyBase + SPACING * i, &nVal, sizeof(uint64_t));
317 }
318 }
319}
320
321/* -------------------------------------------------------------------- */
322/* helper functions for string escaping. */
323/* -------------------------------------------------------------------- */
324
328std::string CPL_DLL OGRDuplicateCharacter(const std::string &osStr, char ch);
329
330#endif /* ndef OGR_P_H_INCLUDED */
Definition of an attribute of an OGRFeatureDefn.
Definition ogr_feature.h:72
Abstract base class for all geometry classes.
Definition ogr_geometry.h:357
Simple container for a position.
Definition ogr_geometry.h:89
Options for formatting WKT output.
Definition ogr_geometry.h:46
Various convenience functions for CPL.
Definitions for CPL mini XML Parser/Serializer.
#define CPL_WARN_UNUSED_RESULT
Qualifier to warn when the return value of a function is not used.
Definition cpl_port.h:1035
unsigned char GByte
Unsigned byte type.
Definition cpl_port.h:165
Various convenience functions for working with strings and string lists.
Core portability services for cross-platform OGR code.
int OGRBoolean
Type for a OGR boolean.
Definition ogr_core.h:404
OGRFieldSubType
List of field subtypes.
Definition ogr_core.h:817
OGRwkbVariant
Output variants of WKB we support.
Definition ogr_core.h:558
OGRFieldType
List of feature field types.
Definition ogr_core.h:790
OGRwkbGeometryType
List of well known binary geometry types.
Definition ogr_core.h:423
int OGRErr
Type for a OGR error.
Definition ogr_core.h:388
Configuration of the ISO8601 formatting output.
Definition ogr_p.h:137
OGRISO8601Precision ePrecision
Precision of formatting.
Definition ogr_p.h:139
OGRFeature field attribute value union.
Definition ogr_core.h:904