GDAL

ogr_schema_override.h
1/******************************************************************************
2 * Project: OGR_SCHEMA open options handling
3 * Purpose: Class for representing a layer schema override.
4 * Author: Alessandro Pasotti, elpaso@itopen.it
5 *
6 ******************************************************************************
7 * Copyright (c) 2024, Alessandro Pasotti <elpaso at itopen dot it>
8 *
9 * SPDX-License-Identifier: MIT
10 ****************************************************************************/
11
12#ifndef OGR_SCHEMA_OVERRIDE_H_INCLUDED
13#define OGR_SCHEMA_OVERRIDE_H_INCLUDED
14
16
17#include <functional>
18#include <string>
19#include <map>
20#include <optional>
21#include <ogr_api.h>
22#include <ogr_feature.h>
23#include <cpl_vsi.h>
24#include <cpl_json.h>
25
27 class CPL_DLL OGRFieldDefnOverride
28{
29 public:
30 OGRFieldDefnOverride() = default;
31
32 void SetFieldName(const std::string &osName)
33 {
34 m_osName = osName;
35 }
36
37 void SetSrcFieldType(OGRFieldType eType)
38 {
39 m_eSrcType = eType;
40 }
41
42 void SetSrcFieldSubType(OGRFieldSubType eSubType)
43 {
44 m_eSrcSubType = eSubType;
45 }
46
47 void SetFieldType(OGRFieldType eType)
48 {
49 m_eType = eType;
50 }
51
52 void SetFieldSubType(OGRFieldSubType eSubType)
53 {
54 m_eSubType = eSubType;
55 }
56
57 void SetFieldWidth(int nWidth)
58 {
59 m_nWidth = nWidth;
60 }
61
62 void SetFieldPrecision(int nPrecision)
63 {
64 m_nPrecision = nPrecision;
65 }
66
67 std::optional<std::string> GetFieldName() const
68 {
69 return m_osName;
70 }
71
72 std::optional<OGRFieldType> GetSrcFieldType() const
73 {
74 return m_eSrcType;
75 }
76
77 std::optional<OGRFieldSubType> GetSrcFieldSubType() const
78 {
79 return m_eSrcSubType;
80 }
81
82 std::optional<OGRFieldType> GetFieldType() const
83 {
84 return m_eType;
85 }
86
87 std::optional<OGRFieldSubType> GetFieldSubType() const
88 {
89 return m_eSubType;
90 }
91
92 std::optional<int> GetFieldWidth() const
93 {
94 return m_nWidth;
95 }
96
97 std::optional<int> GetFieldPrecision() const
98 {
99 return m_nPrecision;
100 }
101
102 // Considered valid if it carries any change information, otherwise it's considered a no-op
103 bool IsValid() const;
104
105 private:
106 std::optional<std::string> m_osName{};
107 std::optional<OGRFieldType> m_eSrcType{};
108 std::optional<OGRFieldSubType> m_eSrcSubType{};
109 std::optional<OGRFieldType> m_eType{};
110 std::optional<OGRFieldSubType> m_eSubType{};
111 std::optional<int> m_nWidth{};
112 std::optional<int> m_nPrecision{};
113};
114
116 class CPL_DLL OGRLayerSchemaOverride
117{
118 public:
119 OGRLayerSchemaOverride() = default;
120
121 void SetLayerName(const std::string &osLayerName)
122 {
123 m_osLayerName = osLayerName;
124 }
125
126 void AddNamedFieldOverride(const std::string &osFieldName,
127 const OGRFieldDefnOverride &oFieldOverride)
128 {
129 m_oNamedFieldOverrides[osFieldName] = oFieldOverride;
130 }
131
132 void AddUnnamedFieldOverride(const OGRFieldDefnOverride &oFieldOverride)
133 {
134 m_aoUnnamedFieldOverrides.push_back(oFieldOverride);
135 }
136
137 const std::string &GetLayerName() const
138 {
139 return m_osLayerName;
140 }
141
142 const std::map<std::string, OGRFieldDefnOverride> &
143 GetNamedFieldOverrides() const
144 {
145 return m_oNamedFieldOverrides;
146 }
147
148 const std::vector<OGRFieldDefnOverride> &GetUnnamedFieldOverrides() const
149 {
150 return m_aoUnnamedFieldOverrides;
151 }
152
153 bool IsFullOverride() const
154 {
155 return m_bIsFullOverride;
156 }
157
158 void SetFullOverride(bool bIsFullOverride)
159 {
160 m_bIsFullOverride = bIsFullOverride;
161 }
162
163 bool IsValid() const;
164
165 private:
166 std::string m_osLayerName{};
167 std::map<std::string, OGRFieldDefnOverride> m_oNamedFieldOverrides{};
168 std::vector<OGRFieldDefnOverride> m_aoUnnamedFieldOverrides{};
169 bool m_bIsFullOverride = false;
170};
171
172 class GDALDataset;
173
175 class CPL_DLL OGRSchemaOverride
176{
177 public:
178 OGRSchemaOverride() = default;
179
180 void AddLayerOverride(const OGRLayerSchemaOverride &oLayerOverride)
181 {
182 m_aoLayerOverrides.push_back(oLayerOverride);
183 }
184
185 bool LoadFromJSON(const std::string &osJSON);
186
187 const std::vector<OGRLayerSchemaOverride> &GetLayerOverrides() const
188 {
189 return m_aoLayerOverrides;
190 }
191
192 bool IsValid() const;
193
194 // Default implementation to apply the overrides to a dataset
195 bool DefaultApply(
196 GDALDataset *poDS, const char *pszDebugKey,
197 std::function<void(OGRLayer *, int)> callbackWhenRemovingField =
198 [](OGRLayer *, int) {}) const;
199
200 private:
201 std::vector<OGRLayerSchemaOverride> m_aoLayerOverrides{};
202};
203
205
206#endif /* ndef OGR_FEATURE_H_INCLUDED */
GDALDataset
A set of associated raster bands, usually from one file.
Definition gdal_dataset.h:76
OGRLayer
This class represents a layer of simple features, with access methods.
Definition ogrsf_frmts.h:61
cpl_json.h
Interface for read and write JSON documents.
cpl_vsi.h
Standard C Covers.
ogr_api.h
C API and defines for OGRFeature, OGRGeometry, and OGRDataSource related classes.
OGRFieldSubType
OGRFieldSubType
List of field subtypes.
Definition ogr_core.h:799
OGRFieldType
OGRFieldType
List of feature field types.
Definition ogr_core.h:772
ogr_feature.h
Simple feature classes.