/src/gdal/frmts/netcdf/netcdfwriterconfig.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: netCDF read/write Driver |
4 | | * Purpose: GDAL bindings over netCDF library. |
5 | | * Author: Even Rouault <even.rouault at spatialys.com> |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2016, Even Rouault <even.rouault at spatialys.com> |
9 | | * |
10 | | * SPDX-License-Identifier: MIT |
11 | | ****************************************************************************/ |
12 | | |
13 | | #include "netcdfdataset.h" |
14 | | |
15 | | bool netCDFWriterConfiguration::SetNameValue( |
16 | | CPLXMLNode *psNode, std::map<CPLString, CPLString> &oMap) |
17 | 0 | { |
18 | 0 | const char *pszName = CPLGetXMLValue(psNode, "name", nullptr); |
19 | 0 | const char *pszValue = CPLGetXMLValue(psNode, "value", nullptr); |
20 | 0 | if (pszName != nullptr && pszValue != nullptr) |
21 | 0 | { |
22 | 0 | oMap[pszName] = pszValue; |
23 | 0 | return true; |
24 | 0 | } |
25 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, "Missing name/value"); |
26 | 0 | return false; |
27 | 0 | } |
28 | | |
29 | | bool netCDFWriterConfiguration::Parse(const char *pszFilename) |
30 | 0 | { |
31 | 0 | CPLXMLNode *psRoot = STARTS_WITH(pszFilename, "<Configuration") |
32 | 0 | ? CPLParseXMLString(pszFilename) |
33 | 0 | : CPLParseXMLFile(pszFilename); |
34 | 0 | if (psRoot == nullptr) |
35 | 0 | return false; |
36 | 0 | CPLXMLTreeCloser oCloser(psRoot); |
37 | |
|
38 | 0 | for (CPLXMLNode *psIter = psRoot->psChild; psIter != nullptr; |
39 | 0 | psIter = psIter->psNext) |
40 | 0 | { |
41 | 0 | if (psIter->eType != CXT_Element) |
42 | 0 | continue; |
43 | 0 | if (EQUAL(psIter->pszValue, "DatasetCreationOption")) |
44 | 0 | { |
45 | 0 | SetNameValue(psIter, m_oDatasetCreationOptions); |
46 | 0 | } |
47 | 0 | else if (EQUAL(psIter->pszValue, "LayerCreationOption")) |
48 | 0 | { |
49 | 0 | SetNameValue(psIter, m_oLayerCreationOptions); |
50 | 0 | } |
51 | 0 | else if (EQUAL(psIter->pszValue, "Attribute")) |
52 | 0 | { |
53 | 0 | netCDFWriterConfigAttribute oAtt; |
54 | 0 | if (oAtt.Parse(psIter)) |
55 | 0 | m_aoAttributes.push_back(std::move(oAtt)); |
56 | 0 | } |
57 | 0 | else if (EQUAL(psIter->pszValue, "Field")) |
58 | 0 | { |
59 | 0 | netCDFWriterConfigField oField; |
60 | 0 | if (oField.Parse(psIter)) |
61 | 0 | m_oFields[!oField.m_osName.empty() |
62 | 0 | ? oField.m_osName |
63 | 0 | : CPLString("__") + oField.m_osNetCDFName] = |
64 | 0 | oField; |
65 | 0 | } |
66 | 0 | else if (EQUAL(psIter->pszValue, "Layer")) |
67 | 0 | { |
68 | 0 | netCDFWriterConfigLayer oLayer; |
69 | 0 | if (oLayer.Parse(psIter)) |
70 | 0 | m_oLayers[oLayer.m_osName] = std::move(oLayer); |
71 | 0 | } |
72 | 0 | else |
73 | 0 | { |
74 | 0 | CPLDebug("GDAL_netCDF", "Ignoring %s", psIter->pszValue); |
75 | 0 | } |
76 | 0 | } |
77 | |
|
78 | 0 | m_bIsValid = true; |
79 | |
|
80 | 0 | return true; |
81 | 0 | } |
82 | | |
83 | | bool netCDFWriterConfigAttribute::Parse(CPLXMLNode *psNode) |
84 | 0 | { |
85 | 0 | const char *pszName = CPLGetXMLValue(psNode, "name", nullptr); |
86 | 0 | const char *pszValue = CPLGetXMLValue(psNode, "value", nullptr); |
87 | 0 | const char *pszType = CPLGetXMLValue(psNode, "type", "string"); |
88 | 0 | if (!EQUAL(pszType, "string") && !EQUAL(pszType, "integer") && |
89 | 0 | !EQUAL(pszType, "double")) |
90 | 0 | { |
91 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "type='%s' unsupported", |
92 | 0 | pszType); |
93 | 0 | return false; |
94 | 0 | } |
95 | 0 | if (pszName == nullptr || pszValue == nullptr) |
96 | 0 | { |
97 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, "Missing name/value"); |
98 | 0 | return false; |
99 | 0 | } |
100 | 0 | m_osName = pszName; |
101 | 0 | m_osValue = pszValue; |
102 | 0 | m_osType = pszType; |
103 | 0 | return true; |
104 | 0 | } |
105 | | |
106 | | bool netCDFWriterConfigField::Parse(CPLXMLNode *psNode) |
107 | 0 | { |
108 | 0 | const char *pszName = CPLGetXMLValue(psNode, "name", nullptr); |
109 | 0 | const char *pszNetCDFName = CPLGetXMLValue(psNode, "netcdf_name", pszName); |
110 | 0 | const char *pszMainDim = CPLGetXMLValue(psNode, "main_dim", nullptr); |
111 | 0 | if (pszName == nullptr && pszNetCDFName == nullptr) |
112 | 0 | { |
113 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
114 | 0 | "Bot name and netcdf_name are missing"); |
115 | 0 | return false; |
116 | 0 | } |
117 | 0 | if (pszName != nullptr) |
118 | 0 | m_osName = pszName; |
119 | 0 | if (pszNetCDFName != nullptr) |
120 | 0 | m_osNetCDFName = pszNetCDFName; |
121 | 0 | if (pszMainDim != nullptr) |
122 | 0 | m_osMainDim = pszMainDim; |
123 | |
|
124 | 0 | for (CPLXMLNode *psIter = psNode->psChild; psIter != nullptr; |
125 | 0 | psIter = psIter->psNext) |
126 | 0 | { |
127 | 0 | if (psIter->eType != CXT_Element) |
128 | 0 | continue; |
129 | 0 | if (EQUAL(psIter->pszValue, "Attribute")) |
130 | 0 | { |
131 | 0 | netCDFWriterConfigAttribute oAtt; |
132 | 0 | if (oAtt.Parse(psIter)) |
133 | 0 | m_aoAttributes.push_back(std::move(oAtt)); |
134 | 0 | } |
135 | 0 | else |
136 | 0 | { |
137 | 0 | CPLDebug("GDAL_netCDF", "Ignoring %s", psIter->pszValue); |
138 | 0 | } |
139 | 0 | } |
140 | |
|
141 | 0 | return true; |
142 | 0 | } |
143 | | |
144 | | bool netCDFWriterConfigLayer::Parse(CPLXMLNode *psNode) |
145 | 0 | { |
146 | 0 | const char *pszName = CPLGetXMLValue(psNode, "name", nullptr); |
147 | 0 | const char *pszNetCDFName = CPLGetXMLValue(psNode, "netcdf_name", pszName); |
148 | 0 | if (pszName == nullptr) |
149 | 0 | { |
150 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, "Missing name"); |
151 | 0 | return false; |
152 | 0 | } |
153 | 0 | m_osName = pszName; |
154 | 0 | if (pszNetCDFName != nullptr) |
155 | 0 | m_osNetCDFName = pszNetCDFName; |
156 | |
|
157 | 0 | for (CPLXMLNode *psIter = psNode->psChild; psIter != nullptr; |
158 | 0 | psIter = psIter->psNext) |
159 | 0 | { |
160 | 0 | if (psIter->eType != CXT_Element) |
161 | 0 | continue; |
162 | 0 | if (EQUAL(psIter->pszValue, "LayerCreationOption")) |
163 | 0 | { |
164 | 0 | netCDFWriterConfiguration::SetNameValue(psIter, |
165 | 0 | m_oLayerCreationOptions); |
166 | 0 | } |
167 | 0 | else if (EQUAL(psIter->pszValue, "Attribute")) |
168 | 0 | { |
169 | 0 | netCDFWriterConfigAttribute oAtt; |
170 | 0 | if (oAtt.Parse(psIter)) |
171 | 0 | m_aoAttributes.push_back(std::move(oAtt)); |
172 | 0 | } |
173 | 0 | else if (EQUAL(psIter->pszValue, "Field")) |
174 | 0 | { |
175 | 0 | netCDFWriterConfigField oField; |
176 | 0 | if (oField.Parse(psIter)) |
177 | 0 | m_oFields[!oField.m_osName.empty() |
178 | 0 | ? oField.m_osName |
179 | 0 | : CPLString("__") + oField.m_osNetCDFName] = |
180 | 0 | std::move(oField); |
181 | 0 | } |
182 | 0 | else |
183 | 0 | { |
184 | 0 | CPLDebug("GDAL_netCDF", "Ignoring %s", psIter->pszValue); |
185 | 0 | } |
186 | 0 | } |
187 | |
|
188 | 0 | return true; |
189 | 0 | } |