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#ifdef OGR_P_WITH_SRS_CACHE
25#include "cpl_mem_cache.h"
26#endif
27#include "cpl_minixml.h"
28
29#include "ogr_core.h"
30
31#include <limits>
32#include <memory>
33#include <string_view>
34
35class OGRGeometry;
36class OGRFieldDefn;
38
39/* A default name for the default geometry column, instead of '' */
40#define OGR_GEOMETRY_DEFAULT_NON_EMPTY_NAME "_ogr_geometry_"
41
42#ifdef CPL_MSB
43#define OGR_SWAP(x) (x == wkbNDR)
44#else
45#define OGR_SWAP(x) (x == wkbXDR)
46#endif
47
48/* PostGIS 1.X has non standard codes for the following geometry types */
49#define POSTGIS15_CURVEPOLYGON 13 /* instead of 10 */
50#define POSTGIS15_MULTICURVE 14 /* instead of 11 */
51#define POSTGIS15_MULTISURFACE 15 /* instead of 12 */
52
53/* Has been deprecated. Can only be used in very specific circumstances */
54#ifdef GDAL_COMPILATION
55#define wkb25DBitInternalUse 0x80000000
56#endif
57
58/* -------------------------------------------------------------------- */
59/* helper function for parsing well known text format vector objects.*/
60/* -------------------------------------------------------------------- */
61
62#ifdef OGR_GEOMETRY_H_INCLUDED
63#define OGR_WKT_TOKEN_MAX 64
64
65const char CPL_DLL *OGRWktReadToken(const char *pszInput, char *pszToken);
66
67const char CPL_DLL *OGRWktReadPoints(const char *pszInput,
68 OGRRawPoint **ppaoPoints, double **ppadfZ,
69 int *pnMaxPoints, int *pnReadPoints);
70
71const char CPL_DLL *
72OGRWktReadPointsM(const char *pszInput, OGRRawPoint **ppaoPoints,
73 double **ppadfZ, double **ppadfM,
74 int *flags, /* geometry flags, are we expecting Z, M, or both;
75 may change due to input */
76 int *pnMaxPoints, int *pnReadPoints);
77
78void CPL_DLL OGRMakeWktCoordinate(char *, double, double, double, int);
79std::string CPL_DLL OGRMakeWktCoordinate(double, double, double, int,
80 const OGRWktOptions &opts);
81void CPL_DLL OGRMakeWktCoordinateM(char *, double, double, double, double,
83std::string CPL_DLL OGRMakeWktCoordinateM(double, double, double, double,
85 const OGRWktOptions &opts);
86
87#endif
88
89void CPL_DLL OGRFormatDouble(char *pszBuffer, int nBufferLen, double dfVal,
90 char chDecimalSep, int nPrecision = 15,
91 char chConversionSpecifier = 'f');
92
93#ifdef OGR_GEOMETRY_H_INCLUDED
94std::string CPL_DLL OGRFormatDouble(double val, const OGRWktOptions &opts,
95 int nDimIdx);
96#endif
97
98int OGRFormatFloat(char *pszBuffer, int nBufferLen, float fVal, int nPrecision,
99 char chConversionSpecifier);
100
101/* -------------------------------------------------------------------- */
102/* Date-time parsing and processing functions */
103/* -------------------------------------------------------------------- */
104
105/* Internal use by OGR drivers only, CPL_DLL is just there in case */
106/* they are compiled as plugins */
107
108int CPL_DLL OGRTimezoneToTZFlag(const char *pszTZ,
109 bool bEmitErrorIfUnhandledFormat);
110std::string CPL_DLL OGRTZFlagToTimezone(int nTZFlag,
111 const char *pszUTCRepresentation);
112
113int CPL_DLL OGRGetDayOfWeek(int day, int month, int year);
114int CPL_DLL OGRParseXMLDateTime(const char *pszXMLDateTime, OGRField *psField);
115int CPL_DLL OGRParseRFC822DateTime(const char *pszRFC822DateTime,
116 OGRField *psField);
117char CPL_DLL *OGRGetRFC822DateTime(const OGRField *psField);
118char CPL_DLL *OGRGetXMLDateTime(const OGRField *psField);
119char CPL_DLL *OGRGetXMLDateTime(const OGRField *psField,
120 bool bAlwaysMillisecond);
121// 30 = strlen("YYYY-MM-DDThh:mm:ss.sss+hh:mm") + 1
122#define OGR_SIZEOF_ISO8601_DATETIME_BUFFER 30
123int CPL_DLL
124OGRGetISO8601DateTime(const OGRField *psField, bool bAlwaysMillisecond,
125 char szBuffer[OGR_SIZEOF_ISO8601_DATETIME_BUFFER]);
126
128enum class OGRISO8601Precision
129{
131 AUTO,
133 MILLISECOND,
135 SECOND,
137 MINUTE
138};
139
142{
144 OGRISO8601Precision ePrecision;
145};
146
147int CPL_DLL
148OGRGetISO8601DateTime(const OGRField *psField, const OGRISO8601Format &sFormat,
149 char szBuffer[OGR_SIZEOF_ISO8601_DATETIME_BUFFER]);
150char CPL_DLL *OGRGetXML_UTF8_EscapedString(const char *pszString);
151
152#ifdef GDAL_COMPILATION
153bool CPL_DLL OGRParseDateTimeYYYYMMDDTHHMMZ(std::string_view sInput,
154 OGRField *psField);
155bool CPL_DLL OGRParseDateTimeYYYYMMDDTHHMMSSZ(std::string_view sInput,
156 OGRField *psField);
157bool CPL_DLL OGRParseDateTimeYYYYMMDDTHHMMSSsssZ(std::string_view sInput,
158 OGRField *psField);
159#endif
160
161int OGRCompareDate(const OGRField *psFirstTuple,
162 const OGRField *psSecondTuple); /* used by ogr_gensql.cpp and
163 ogrfeaturequery.cpp */
164
165/* General utility option processing. */
166int CPL_DLL OGRGeneralCmdLineProcessor(int nArgc, char ***ppapszArgv,
167 int nOptions);
168
169/************************************************************************/
170/* Support for special attributes (feature query and selection) */
171/************************************************************************/
172#define SPF_FID 0
173#define SPF_OGR_GEOMETRY 1
174#define SPF_OGR_STYLE 2
175#define SPF_OGR_GEOM_WKT 3
176#define SPF_OGR_GEOM_AREA 4
177#define SPECIAL_FIELD_COUNT 5
178
179extern const char *const SpecialFieldNames[SPECIAL_FIELD_COUNT];
180
181/************************************************************************/
182/* Some SRS related stuff, search in SRS data files. */
183/************************************************************************/
184
185OGRErr CPL_DLL OSRGetEllipsoidInfo(int, char **, double *, double *);
186
187/* Fast atof function */
188double OGRFastAtof(const char *pszStr);
189
190OGRErr CPL_DLL OGRCheckPermutation(const int *panPermutation, int nSize);
191
192/* GML related */
193
194OGRGeometry CPL_DLL *GML2OGRGeometry_XMLNode(
195 const CPLXMLNode *psNode, int nPseudoBoolGetSecondaryGeometryOption,
196 int nRecLevel = 0, int nSRSDimension = 0, bool bIgnoreGSG = false,
197 bool bOrientation = true, bool bFaceHoleNegative = false,
198 const char *pszId = nullptr);
199
200#ifdef OGR_P_WITH_SRS_CACHE
201OGRGeometry CPL_DLL *GML2OGRGeometry_XMLNode(
202 const CPLXMLNode *psNode, int nPseudoBoolGetSecondaryGeometryOption,
203 lru11::Cache<std::string, std::shared_ptr<OGRSpatialReference>> &oSRSCache,
204 int nRecLevel = 0, int nSRSDimension = 0, bool bIgnoreGSG = false,
205 bool bOrientation = true, bool bFaceHoleNegative = false,
206 const char *pszId = nullptr);
207#endif
208
209/************************************************************************/
210/* PostGIS EWKB encoding */
211/************************************************************************/
212
213OGRGeometry CPL_DLL *OGRGeometryFromEWKB(GByte *pabyWKB, int nLength,
214 int *pnSRID, int bIsPostGIS1_EWKB);
215OGRGeometry CPL_DLL *OGRGeometryFromHexEWKB(const char *pszBytea, int *pnSRID,
216 int bIsPostGIS1_EWKB);
217char CPL_DLL *OGRGeometryToHexEWKB(OGRGeometry *poGeometry, int nSRSId,
218 int nPostGISMajor, int nPostGISMinor);
219
220/************************************************************************/
221/* WKB Type Handling encoding */
222/************************************************************************/
223
224OGRErr CPL_DLL OGRReadWKBGeometryType(const unsigned char *pabyData,
225 OGRwkbVariant wkbVariant,
226 OGRwkbGeometryType *eGeometryType);
227
228/************************************************************************/
229/* WKT Type Handling encoding */
230/************************************************************************/
231
232OGRErr CPL_DLL OGRReadWKTGeometryType(const char *pszWKT,
233 OGRwkbGeometryType *peGeometryType);
234
235/************************************************************************/
236/* Other */
237/************************************************************************/
238
239void CPL_DLL OGRUpdateFieldType(OGRFieldDefn *poFDefn, OGRFieldType eNewType,
240 OGRFieldSubType eNewSubType);
241
242/************************************************************************/
243/* OGRRoundValueIEEE754() */
244/************************************************************************/
245
254inline uint64_t OGRRoundValueIEEE754(uint64_t nVal,
255 int nBitsPrecision) CPL_WARN_UNUSED_RESULT;
256
257inline uint64_t OGRRoundValueIEEE754(uint64_t nVal, int nBitsPrecision)
258{
259 constexpr int MANTISSA_SIZE = std::numeric_limits<double>::digits - 1;
260 constexpr int MAX_EXPONENT = std::numeric_limits<double>::max_exponent;
261#if __cplusplus >= 201703L
262 static_assert(MANTISSA_SIZE == 52);
263 static_assert(MAX_EXPONENT == 1024);
264#endif
265 // Extract the binary exponent from the IEEE754 representation
266 const int nExponent =
267 ((nVal >> MANTISSA_SIZE) & (2 * MAX_EXPONENT - 1)) - (MAX_EXPONENT - 1);
268 // Add 1 to round-up and the desired precision
269 const int nBitsRequired = 1 + nExponent + nBitsPrecision;
270 // Compute number of nullified bits
271 int nNullifiedBits = MANTISSA_SIZE - nBitsRequired;
272 // this will also capture NaN and Inf since nExponent = 1023,
273 // and thus nNullifiedBits < 0
274 if (nNullifiedBits <= 0)
275 return nVal;
276 if (nNullifiedBits >= MANTISSA_SIZE)
277 nNullifiedBits = MANTISSA_SIZE;
278 nVal >>= nNullifiedBits;
279 nVal <<= nNullifiedBits;
280 return nVal;
281}
282
283/************************************************************************/
284/* OGRRoundCoordinatesIEEE754XYValues() */
285/************************************************************************/
286
291template <int SPACING>
292inline void OGRRoundCoordinatesIEEE754XYValues(int nBitsPrecision,
293 GByte *pabyBase, size_t nPoints)
294{
295 // Note: we use SPACING as template for improved code generation.
296
297 if (nBitsPrecision != INT_MIN)
298 {
299 for (size_t i = 0; i < nPoints; i++)
300 {
301 uint64_t nVal;
302
303 memcpy(&nVal, pabyBase + SPACING * i, sizeof(uint64_t));
304 nVal = OGRRoundValueIEEE754(nVal, nBitsPrecision);
305 memcpy(pabyBase + SPACING * i, &nVal, sizeof(uint64_t));
306
307 memcpy(&nVal, pabyBase + sizeof(uint64_t) + SPACING * i,
308 sizeof(uint64_t));
309 nVal = OGRRoundValueIEEE754(nVal, nBitsPrecision);
310 memcpy(pabyBase + sizeof(uint64_t) + SPACING * i, &nVal,
311 sizeof(uint64_t));
312 }
313 }
314}
315
316/************************************************************************/
317/* OGRRoundCoordinatesIEEE754() */
318/************************************************************************/
319
324template <int SPACING>
325inline void OGRRoundCoordinatesIEEE754(int nBitsPrecision, GByte *pabyBase,
326 size_t nPoints)
327{
328 if (nBitsPrecision != INT_MIN)
329 {
330 for (size_t i = 0; i < nPoints; i++)
331 {
332 uint64_t nVal;
333
334 memcpy(&nVal, pabyBase + SPACING * i, sizeof(uint64_t));
335 nVal = OGRRoundValueIEEE754(nVal, nBitsPrecision);
336 memcpy(pabyBase + SPACING * i, &nVal, sizeof(uint64_t));
337 }
338 }
339}
340
341/* -------------------------------------------------------------------- */
342/* helper functions for string escaping. */
343/* -------------------------------------------------------------------- */
344
348std::string CPL_DLL OGRDuplicateCharacter(const std::string &osStr, char ch);
349
350#endif /* ndef OGR_P_H_INCLUDED */
Definition of an attribute of an OGRFeatureDefn.
Definition ogr_feature.h:69
Abstract base class for all geometry classes.
Definition ogr_geometry.h:357
Simple container for a position.
Definition ogr_geometry.h:89
This class represents an OpenGIS Spatial Reference System, and contains methods for converting betwee...
Definition ogr_spatialref.h:152
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:870
unsigned char GByte
Unsigned byte type.
Definition cpl_port.h:175
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:386
OGRFieldSubType
List of field subtypes.
Definition ogr_core.h:799
OGRwkbVariant
Output variants of WKB we support.
Definition ogr_core.h:540
OGRFieldType
List of feature field types.
Definition ogr_core.h:772
OGRwkbGeometryType
List of well known binary geometry types.
Definition ogr_core.h:405
int OGRErr
Type for a OGR error.
Definition ogr_core.h:370
Document node structure.
Definition cpl_minixml.h:54
Configuration of the ISO8601 formatting output.
Definition ogr_p.h:142
OGRISO8601Precision ePrecision
Precision of formatting.
Definition ogr_p.h:144
OGRFeature field attribute value union.
Definition ogr_core.h:886