Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: OGR
4 : * Purpose: WKB geometry related methods
5 : * Author: Even Rouault <even dot rouault at spatialys.com>
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2022, Even Rouault <even dot rouault at spatialys.com>
9 : *
10 : * Permission is hereby granted, free of charge, to any person obtaining a
11 : * copy of this software and associated documentation files (the "Software"),
12 : * to deal in the Software without restriction, including without limitation
13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 : * and/or sell copies of the Software, and to permit persons to whom the
15 : * Software is furnished to do so, subject to the following conditions:
16 : *
17 : * The above copyright notice and this permission notice shall be included
18 : * in all copies or substantial portions of the Software.
19 : *
20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 : * DEALINGS IN THE SOFTWARE.
27 : ****************************************************************************/
28 :
29 : #ifndef OGR_WKB_H_INCLUDED
30 : #define OGR_WKB_H_INCLUDED
31 :
32 : #include "cpl_port.h"
33 : #include "ogr_core.h"
34 :
35 : bool CPL_DLL OGRWKBGetGeomType(const GByte *pabyWkb, size_t nWKBSize,
36 : bool &bNeedSwap, uint32_t &nType);
37 : bool OGRWKBPolygonGetArea(const GByte *&pabyWkb, size_t &nWKBSize,
38 : double &dfArea);
39 : bool OGRWKBMultiPolygonGetArea(const GByte *&pabyWkb, size_t &nWKBSize,
40 : double &dfArea);
41 :
42 : bool CPL_DLL OGRWKBGetBoundingBox(const GByte *pabyWkb, size_t nWKBSize,
43 : OGREnvelope3D &sEnvelope);
44 :
45 : bool CPL_DLL OGRWKBGetBoundingBox(const GByte *pabyWkb, size_t nWKBSize,
46 : OGREnvelope &sEnvelope);
47 :
48 : bool CPL_DLL OGRWKBIntersectsPessimistic(const GByte *pabyWkb, size_t nWKBSize,
49 : const OGREnvelope &sEnvelope);
50 :
51 : void CPL_DLL OGRWKBFixupCounterClockWiseExternalRing(GByte *pabyWkb,
52 : size_t nWKBSize);
53 :
54 : /** Modifies a PostGIS-style Extended WKB geometry to a regular WKB one.
55 : * pabyEWKB will be modified in place.
56 : * The return value will be either at the beginning of pabyEWKB or 4 bytes
57 : * later, and thus has the same lifetime of pabyEWKB. The function returns in
58 : * nWKBSizeOut the length of the returned WKB pointer. pnSRIDOut may be NULL, or
59 : * if not NULL, the function will return in it the SRID, if present, or INT_MIN
60 : * if not present.
61 : */
62 : const GByte CPL_DLL *WKBFromEWKB(GByte *pabyEWKB, size_t nEWKBSize,
63 : size_t &nWKBSizeOut, int *pnSRIDOut);
64 :
65 : /************************************************************************/
66 : /* OGRAppendBuffer */
67 : /************************************************************************/
68 :
69 : /** Append buffer that can be grown dynamically. */
70 36 : class CPL_DLL OGRAppendBuffer
71 : {
72 : public:
73 : /** Constructor */
74 : OGRAppendBuffer();
75 :
76 : /** Destructor */
77 : virtual ~OGRAppendBuffer();
78 :
79 : /** Return the pointer at which nItemSize bytes can be written,
80 : * or nullptr in case of error.
81 : */
82 138 : inline void *GetPtrForNewBytes(size_t nItemSize)
83 : {
84 138 : if (nItemSize > m_nCapacity - m_nSize)
85 : {
86 4 : if (!Grow(nItemSize))
87 0 : return nullptr;
88 : }
89 138 : void *pRet = static_cast<GByte *>(m_pRawBuffer) + m_nSize;
90 138 : m_nSize += nItemSize;
91 138 : return pRet;
92 : }
93 :
94 : /** Return the number of valid bytes in the buffer. */
95 159 : inline size_t GetSize() const
96 : {
97 159 : return m_nSize;
98 : }
99 :
100 : protected:
101 : /** Capacity of the buffer (ie number of bytes allocated). */
102 : size_t m_nCapacity = 0;
103 :
104 : /** Number of valid bytes in the buffer. */
105 : size_t m_nSize = 0;
106 :
107 : /** Raw buffer pointer. */
108 : void *m_pRawBuffer = nullptr;
109 :
110 : /** Extend the capacity of m_pRawBuffer to be at least m_nSize + nItemSize
111 : * large.
112 : */
113 : virtual bool Grow(size_t nItemSize) = 0;
114 :
115 : private:
116 : OGRAppendBuffer(const OGRAppendBuffer &) = delete;
117 : OGRAppendBuffer &operator=(const OGRAppendBuffer &) = delete;
118 : };
119 :
120 : /************************************************************************/
121 : /* OGRWKTToWKBTranslator */
122 : /************************************************************************/
123 :
124 : /** Translate WKT geometry to WKB geometry and append it to a buffer */
125 : class CPL_DLL OGRWKTToWKBTranslator
126 : {
127 : OGRAppendBuffer &m_oAppendBuffer;
128 : bool m_bCanUseStrtod = false;
129 :
130 : public:
131 : /** Constructor */
132 : explicit OGRWKTToWKBTranslator(OGRAppendBuffer &oAppendBuffer);
133 :
134 : /** Translate the WKT geometry starting at pabyWKTStart and of length nLength.
135 : *
136 : * If pabyWKTStart[nLength] can be dereferenced and temporarily modified,
137 : * set bCanAlterByteAfter to true, which will optimize performance.
138 : *
139 : * Returns the number of bytes of the generated WKB, or -1 in case of error.
140 : */
141 : size_t TranslateWKT(void *pabyWKTStart, size_t nLength,
142 : bool bCanAlterByteAfter);
143 : };
144 :
145 : #endif // OGR_WKB_H_INCLUDED
|