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
35 class OGRGeometry;
36 class OGRFieldDefn;
37 class OGRSpatialReference;
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
65 const char CPL_DLL *OGRWktReadToken(const char *pszInput, char *pszToken);
66
67 const char CPL_DLL *OGRWktReadPoints(const char *pszInput,
68 OGRRawPoint **ppaoPoints, double **ppadfZ,
69 int *pnMaxPoints, int *pnReadPoints);
70
71 const 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
78 void CPL_DLL OGRMakeWktCoordinate(char *, double, double, double, int);
79std::string CPL_DLL OGRMakeWktCoordinate(double, double, double, int,
80 const OGRWktOptions &opts);
81 void CPL_DLL OGRMakeWktCoordinateM(char *, double, double, double, double,
82 OGRBoolean, OGRBoolean);
83std::string CPL_DLL OGRMakeWktCoordinateM(double, double, double, double,
84 OGRBoolean, OGRBoolean,
85 const OGRWktOptions &opts);
86
87#endif
88
89 void 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
98 int 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
108 int CPL_DLL OGRTimezoneToTZFlag(const char *pszTZ,
109 bool bEmitErrorIfUnhandledFormat);
110std::string CPL_DLL OGRTZFlagToTimezone(int nTZFlag,
111 const char *pszUTCRepresentation);
112
113 int CPL_DLL OGRGetDayOfWeek(int day, int month, int year);
114 int CPL_DLL OGRParseXMLDateTime(const char *pszXMLDateTime, OGRField *psField);
115 int CPL_DLL OGRParseRFC822DateTime(const char *pszRFC822DateTime,
116 OGRField *psField);
117 char CPL_DLL *OGRGetRFC822DateTime(const OGRField *psField);
118 char CPL_DLL *OGRGetXMLDateTime(const OGRField *psField);
119 char 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
123 int CPL_DLL
124OGRGetISO8601DateTime(const OGRField *psField, bool bAlwaysMillisecond,
125 char szBuffer[OGR_SIZEOF_ISO8601_DATETIME_BUFFER]);
126
128 enum class OGRISO8601Precision
129{
131 AUTO,
133 MILLISECOND,
135 SECOND,
137 MINUTE
138};
139
141 struct OGRISO8601Format
142{
144 OGRISO8601Precision ePrecision;
145};
146
147 int CPL_DLL
148OGRGetISO8601DateTime(const OGRField *psField, const OGRISO8601Format &sFormat,
149 char szBuffer[OGR_SIZEOF_ISO8601_DATETIME_BUFFER]);
150 char CPL_DLL *OGRGetXML_UTF8_EscapedString(const char *pszString);
151
152#ifdef GDAL_COMPILATION
153 bool CPL_DLL OGRParseDateTimeYYYYMMDDTHHMMZ(std::string_view sInput,
154 OGRField *psField);
155 bool CPL_DLL OGRParseDateTimeYYYYMMDDTHHMMSSZ(std::string_view sInput,
156 OGRField *psField);
157 bool CPL_DLL OGRParseDateTimeYYYYMMDDTHHMMSSsssZ(std::string_view sInput,
158 OGRField *psField);
159#endif
160
161 int OGRCompareDate(const OGRField *psFirstTuple,
162 const OGRField *psSecondTuple); /* used by ogr_gensql.cpp and
163 ogrfeaturequery.cpp */
164
165/* General utility option processing. */
166 int 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
179 extern const char *const SpecialFieldNames[SPECIAL_FIELD_COUNT];
180
181/************************************************************************/
182/* Some SRS related stuff, search in SRS data files. */
183/************************************************************************/
184
185 OGRErr CPL_DLL OSRGetEllipsoidInfo(int, char **, double *, double *);
186
187/* Fast atof function */
188 double OGRFastAtof(const char *pszStr);
189
190 OGRErr CPL_DLL OGRCheckPermutation(const int *panPermutation, int nSize);
191
192/* GML related */
193
194 OGRGeometry 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
201 OGRGeometry 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
213 OGRGeometry CPL_DLL *OGRGeometryFromEWKB(GByte *pabyWKB, int nLength,
214 int *pnSRID, int bIsPostGIS1_EWKB);
215 OGRGeometry CPL_DLL *OGRGeometryFromHexEWKB(const char *pszBytea, int *pnSRID,
216 int bIsPostGIS1_EWKB);
217 char CPL_DLL *OGRGeometryToHexEWKB(OGRGeometry *poGeometry, int nSRSId,
218 int nPostGISMajor, int nPostGISMinor);
219
220/************************************************************************/
221/* WKB Type Handling encoding */
222/************************************************************************/
223
224 OGRErr CPL_DLL OGRReadWKBGeometryType(const unsigned char *pabyData,
225 OGRwkbVariant wkbVariant,
226 OGRwkbGeometryType *eGeometryType);
227
228/************************************************************************/
229/* WKT Type Handling encoding */
230/************************************************************************/
231
232 OGRErr CPL_DLL OGRReadWKTGeometryType(const char *pszWKT,
233 OGRwkbGeometryType *peGeometryType);
234
235/************************************************************************/
236/* Other */
237/************************************************************************/
238
239 void CPL_DLL OGRUpdateFieldType(OGRFieldDefn *poFDefn, OGRFieldType eNewType,
240 OGRFieldSubType eNewSubType);
241
242/************************************************************************/
243/* OGRRoundValueIEEE754() */
244/************************************************************************/
245
254 inline uint64_t OGRRoundValueIEEE754(uint64_t nVal,
255 int nBitsPrecision) CPL_WARN_UNUSED_RESULT;
256
257 inline 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
291 template <int SPACING>
292 inline 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
324 template <int SPACING>
325 inline 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 */
OGRFieldDefn
Definition of an attribute of an OGRFeatureDefn.
Definition ogr_feature.h:69
OGRGeometry
Abstract base class for all geometry classes.
Definition ogr_geometry.h:357
OGRRawPoint
Simple container for a position.
Definition ogr_geometry.h:89
OGRSpatialReference
This class represents an OpenGIS Spatial Reference System, and contains methods for converting betwee...
Definition ogr_spatialref.h:152
OGRWktOptions
Options for formatting WKT output.
Definition ogr_geometry.h:46
cpl_conv.h
Various convenience functions for CPL.
cpl_minixml.h
Definitions for CPL mini XML Parser/Serializer.
CPL_WARN_UNUSED_RESULT
#define CPL_WARN_UNUSED_RESULT
Qualifier to warn when the return value of a function is not used.
Definition cpl_port.h:870
GByte
unsigned char GByte
Unsigned byte type.
Definition cpl_port.h:175
cpl_string.h
Various convenience functions for working with strings and string lists.
ogr_core.h
Core portability services for cross-platform OGR code.
OGRBoolean
int OGRBoolean
Type for a OGR boolean.
Definition ogr_core.h:386
OGRFieldSubType
OGRFieldSubType
List of field subtypes.
Definition ogr_core.h:799
OGRwkbVariant
OGRwkbVariant
Output variants of WKB we support.
Definition ogr_core.h:540
OGRFieldType
OGRFieldType
List of feature field types.
Definition ogr_core.h:772
OGRwkbGeometryType
OGRwkbGeometryType
List of well known binary geometry types.
Definition ogr_core.h:405
OGRErr
int OGRErr
Type for a OGR error.
Definition ogr_core.h:370
CPLXMLNode
Document node structure.
Definition cpl_minixml.h:54
OGRISO8601Format
Configuration of the ISO8601 formatting output.
Definition ogr_p.h:142
OGRISO8601Format::ePrecision
OGRISO8601Precision ePrecision
Precision of formatting.
Definition ogr_p.h:144
OGRField
OGRFeature field attribute value union.
Definition ogr_core.h:886