GDAL
ogr_p.h
1/******************************************************************************
2 * $Id$
3 *
4 * Project: OpenGIS Simple Features Reference Implementation
5 * Purpose: Some private helper functions and stuff for OGR implementation.
6 * Author: Frank Warmerdam, warmerdam@pobox.com
7 *
8 ******************************************************************************
9 * Copyright (c) 1999, Frank Warmerdam
10 * Copyright (c) 2008-2014, Even Rouault <even dot rouault at spatialys.com>
11 *
12 * SPDX-License-Identifier: MIT
13 ****************************************************************************/
14
15#ifndef OGR_P_H_INCLUDED
16#define OGR_P_H_INCLUDED
17
18/* -------------------------------------------------------------------- */
19/* Include the common portability library ... lets us do lots */
20/* of stuff easily. */
21/* -------------------------------------------------------------------- */
22
23#include "cpl_string.h"
24#include "cpl_conv.h"
25#include "cpl_minixml.h"
26
27#include "ogr_core.h"
28
29#include <limits>
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);
146bool CPL_DLL OGRParseDateTimeYYYYMMDDTHHMMZ(const char *pszInput, size_t nLen,
147 OGRField *psField);
148bool CPL_DLL OGRParseDateTimeYYYYMMDDTHHMMSSZ(const char *pszInput, size_t nLen,
149 OGRField *psField);
150bool CPL_DLL OGRParseDateTimeYYYYMMDDTHHMMSSsssZ(const char *pszInput,
151 size_t nLen,
152 OGRField *psField);
153
154int OGRCompareDate(const OGRField *psFirstTuple,
155 const OGRField *psSecondTuple); /* used by ogr_gensql.cpp and
156 ogrfeaturequery.cpp */
157
158/* General utility option processing. */
159int CPL_DLL OGRGeneralCmdLineProcessor(int nArgc, char ***ppapszArgv,
160 int nOptions);
161
162/************************************************************************/
163/* Support for special attributes (feature query and selection) */
164/************************************************************************/
165#define SPF_FID 0
166#define SPF_OGR_GEOMETRY 1
167#define SPF_OGR_STYLE 2
168#define SPF_OGR_GEOM_WKT 3
169#define SPF_OGR_GEOM_AREA 4
170#define SPECIAL_FIELD_COUNT 5
171
172extern const char *const SpecialFieldNames[SPECIAL_FIELD_COUNT];
173
174/************************************************************************/
175/* Some SRS related stuff, search in SRS data files. */
176/************************************************************************/
177
178OGRErr CPL_DLL OSRGetEllipsoidInfo(int, char **, double *, double *);
179
180/* Fast atof function */
181double OGRFastAtof(const char *pszStr);
182
183OGRErr CPL_DLL OGRCheckPermutation(const int *panPermutation, int nSize);
184
185/* GML related */
186
187OGRGeometry CPL_DLL *GML2OGRGeometry_XMLNode(
188 const CPLXMLNode *psNode, int nPseudoBoolGetSecondaryGeometryOption,
189 int nRecLevel = 0, int nSRSDimension = 0, bool bIgnoreGSG = false,
190 bool bOrientation = true, bool bFaceHoleNegative = false);
191
192/************************************************************************/
193/* PostGIS EWKB encoding */
194/************************************************************************/
195
196OGRGeometry CPL_DLL *OGRGeometryFromEWKB(GByte *pabyWKB, int nLength,
197 int *pnSRID, int bIsPostGIS1_EWKB);
198OGRGeometry CPL_DLL *OGRGeometryFromHexEWKB(const char *pszBytea, int *pnSRID,
199 int bIsPostGIS1_EWKB);
200char CPL_DLL *OGRGeometryToHexEWKB(OGRGeometry *poGeometry, int nSRSId,
201 int nPostGISMajor, int nPostGISMinor);
202
203/************************************************************************/
204/* WKB Type Handling encoding */
205/************************************************************************/
206
207OGRErr CPL_DLL OGRReadWKBGeometryType(const unsigned char *pabyData,
208 OGRwkbVariant wkbVariant,
209 OGRwkbGeometryType *eGeometryType);
210
211/************************************************************************/
212/* WKT Type Handling encoding */
213/************************************************************************/
214
215OGRErr CPL_DLL OGRReadWKTGeometryType(const char *pszWKT,
216 OGRwkbGeometryType *peGeometryType);
217
218/************************************************************************/
219/* Other */
220/************************************************************************/
221
222void CPL_DLL OGRUpdateFieldType(OGRFieldDefn *poFDefn, OGRFieldType eNewType,
223 OGRFieldSubType eNewSubType);
224
225/************************************************************************/
226/* OGRRoundValueIEEE754() */
227/************************************************************************/
228
237inline uint64_t OGRRoundValueIEEE754(uint64_t nVal,
238 int nBitsPrecision) CPL_WARN_UNUSED_RESULT;
239
240inline uint64_t OGRRoundValueIEEE754(uint64_t nVal, int nBitsPrecision)
241{
242 constexpr int MANTISSA_SIZE = std::numeric_limits<double>::digits - 1;
243 constexpr int MAX_EXPONENT = std::numeric_limits<double>::max_exponent;
244#if __cplusplus >= 201703L
245 static_assert(MANTISSA_SIZE == 52);
246 static_assert(MAX_EXPONENT == 1024);
247#endif
248 // Extract the binary exponent from the IEEE754 representation
249 const int nExponent =
250 ((nVal >> MANTISSA_SIZE) & (2 * MAX_EXPONENT - 1)) - (MAX_EXPONENT - 1);
251 // Add 1 to round-up and the desired precision
252 const int nBitsRequired = 1 + nExponent + nBitsPrecision;
253 // Compute number of nullified bits
254 int nNullifiedBits = MANTISSA_SIZE - nBitsRequired;
255 // this will also capture NaN and Inf since nExponent = 1023,
256 // and thus nNullifiedBits < 0
257 if (nNullifiedBits <= 0)
258 return nVal;
259 if (nNullifiedBits >= MANTISSA_SIZE)
260 nNullifiedBits = MANTISSA_SIZE;
261 nVal >>= nNullifiedBits;
262 nVal <<= nNullifiedBits;
263 return nVal;
264}
265
266/************************************************************************/
267/* OGRRoundCoordinatesIEEE754XYValues() */
268/************************************************************************/
269
274template <int SPACING>
275inline void OGRRoundCoordinatesIEEE754XYValues(int nBitsPrecision,
276 GByte *pabyBase, size_t nPoints)
277{
278 // Note: we use SPACING as template for improved code generation.
279
280 if (nBitsPrecision != INT_MIN)
281 {
282 for (size_t i = 0; i < nPoints; i++)
283 {
284 uint64_t nVal;
285
286 memcpy(&nVal, pabyBase + SPACING * i, sizeof(uint64_t));
287 nVal = OGRRoundValueIEEE754(nVal, nBitsPrecision);
288 memcpy(pabyBase + SPACING * i, &nVal, sizeof(uint64_t));
289
290 memcpy(&nVal, pabyBase + sizeof(uint64_t) + SPACING * i,
291 sizeof(uint64_t));
292 nVal = OGRRoundValueIEEE754(nVal, nBitsPrecision);
293 memcpy(pabyBase + sizeof(uint64_t) + SPACING * i, &nVal,
294 sizeof(uint64_t));
295 }
296 }
297}
298
299/************************************************************************/
300/* OGRRoundCoordinatesIEEE754() */
301/************************************************************************/
302
307template <int SPACING>
308inline void OGRRoundCoordinatesIEEE754(int nBitsPrecision, GByte *pabyBase,
309 size_t nPoints)
310{
311 if (nBitsPrecision != INT_MIN)
312 {
313 for (size_t i = 0; i < nPoints; i++)
314 {
315 uint64_t nVal;
316
317 memcpy(&nVal, pabyBase + SPACING * i, sizeof(uint64_t));
318 nVal = OGRRoundValueIEEE754(nVal, nBitsPrecision);
319 memcpy(pabyBase + SPACING * i, &nVal, sizeof(uint64_t));
320 }
321 }
322}
323
324#endif /* ndef OGR_P_H_INCLUDED */
Definition of an attribute of an OGRFeatureDefn.
Definition ogr_feature.h:95
Abstract base class for all geometry classes.
Definition ogr_geometry.h:361
Simple container for a position.
Definition ogr_geometry.h:90
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:964
unsigned char GByte
Unsigned byte type.
Definition cpl_port.h:169
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:388
OGRFieldSubType
List of field subtypes.
Definition ogr_core.h:816
OGRwkbVariant
Output variants of WKB we support.
Definition ogr_core.h:545
OGRFieldType
List of feature field types.
Definition ogr_core.h:788
OGRwkbGeometryType
List of well known binary geometry types.
Definition ogr_core.h:407
int OGRErr
Type for a OGR error.
Definition ogr_core.h:371
Document node structure.
Definition cpl_minixml.h:55
Configuration of the ISO8601 formatting output.
Definition ogr_p.h:137
OGRISO8601Precision ePrecision
Precision of formatting.
Definition ogr_p.h:139
Options for formatting WKT output.
Definition ogr_geometry.h:56
OGRFeature field attribute value union.
Definition ogr_core.h:905