/src/PROJ/src/iso19111/io.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: PROJ |
4 | | * Purpose: ISO19111:2019 implementation |
5 | | * Author: Even Rouault <even dot rouault at spatialys dot com> |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2018, Even Rouault <even dot rouault at spatialys dot 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 FROM_PROJ_CPP |
30 | | #define FROM_PROJ_CPP |
31 | | #endif |
32 | | |
33 | | #include <algorithm> |
34 | | #include <cassert> |
35 | | #include <cctype> |
36 | | #include <cmath> |
37 | | #include <cstring> |
38 | | #include <limits> |
39 | | #include <list> |
40 | | #include <locale> |
41 | | #include <map> |
42 | | #include <set> |
43 | | #include <sstream> // std::istringstream |
44 | | #include <string> |
45 | | #include <utility> |
46 | | #include <vector> |
47 | | |
48 | | #include "proj/common.hpp" |
49 | | #include "proj/coordinateoperation.hpp" |
50 | | #include "proj/coordinates.hpp" |
51 | | #include "proj/coordinatesystem.hpp" |
52 | | #include "proj/crs.hpp" |
53 | | #include "proj/datum.hpp" |
54 | | #include "proj/io.hpp" |
55 | | #include "proj/metadata.hpp" |
56 | | #include "proj/util.hpp" |
57 | | |
58 | | #include "operation/coordinateoperation_internal.hpp" |
59 | | #include "operation/esriparammappings.hpp" |
60 | | #include "operation/oputils.hpp" |
61 | | #include "operation/parammappings.hpp" |
62 | | |
63 | | #include "proj/internal/coordinatesystem_internal.hpp" |
64 | | #include "proj/internal/datum_internal.hpp" |
65 | | #include "proj/internal/internal.hpp" |
66 | | #include "proj/internal/io_internal.hpp" |
67 | | |
68 | | #include "proj/internal/include_nlohmann_json.hpp" |
69 | | |
70 | | #include "proj_constants.h" |
71 | | |
72 | | #include "proj_json_streaming_writer.hpp" |
73 | | #include "wkt1_parser.h" |
74 | | #include "wkt2_parser.h" |
75 | | |
76 | | // PROJ include order is sensitive |
77 | | // clang-format off |
78 | | #include "proj.h" |
79 | | #include "proj_internal.h" |
80 | | // clang-format on |
81 | | |
82 | | using namespace NS_PROJ::common; |
83 | | using namespace NS_PROJ::coordinates; |
84 | | using namespace NS_PROJ::crs; |
85 | | using namespace NS_PROJ::cs; |
86 | | using namespace NS_PROJ::datum; |
87 | | using namespace NS_PROJ::internal; |
88 | | using namespace NS_PROJ::metadata; |
89 | | using namespace NS_PROJ::operation; |
90 | | using namespace NS_PROJ::util; |
91 | | |
92 | | using json = nlohmann::json; |
93 | | |
94 | | //! @cond Doxygen_Suppress |
95 | | static const std::string emptyString{}; |
96 | | //! @endcond |
97 | | |
98 | | #if 0 |
99 | | namespace dropbox{ namespace oxygen { |
100 | | template<> nn<NS_PROJ::io::DatabaseContextPtr>::~nn() = default; |
101 | | template<> nn<NS_PROJ::io::AuthorityFactoryPtr>::~nn() = default; |
102 | | template<> nn<std::shared_ptr<NS_PROJ::io::IPROJStringExportable>>::~nn() = default; |
103 | | template<> nn<std::unique_ptr<NS_PROJ::io::PROJStringFormatter, std::default_delete<NS_PROJ::io::PROJStringFormatter> > >::~nn() = default; |
104 | | template<> nn<std::unique_ptr<NS_PROJ::io::WKTFormatter, std::default_delete<NS_PROJ::io::WKTFormatter> > >::~nn() = default; |
105 | | template<> nn<std::unique_ptr<NS_PROJ::io::WKTNode, std::default_delete<NS_PROJ::io::WKTNode> > >::~nn() = default; |
106 | | }} |
107 | | #endif |
108 | | |
109 | | NS_PROJ_START |
110 | | namespace io { |
111 | | |
112 | | //! @cond Doxygen_Suppress |
113 | | const char *JSONFormatter::PROJJSON_v0_7 = |
114 | | "https://proj.org/schemas/v0.7/projjson.schema.json"; |
115 | | |
116 | | #define PROJJSON_DEFAULT_VERSION JSONFormatter::PROJJSON_v0_7 |
117 | | |
118 | | //! @endcond |
119 | | |
120 | | // --------------------------------------------------------------------------- |
121 | | |
122 | | //! @cond Doxygen_Suppress |
123 | 13.2M | IWKTExportable::~IWKTExportable() = default; |
124 | | |
125 | | // --------------------------------------------------------------------------- |
126 | | |
127 | 0 | std::string IWKTExportable::exportToWKT(WKTFormatter *formatter) const { |
128 | 0 | _exportToWKT(formatter); |
129 | 0 | return formatter->toString(); |
130 | 0 | } |
131 | | |
132 | | //! @endcond |
133 | | |
134 | | // --------------------------------------------------------------------------- |
135 | | |
136 | | //! @cond Doxygen_Suppress |
137 | | struct WKTFormatter::Private { |
138 | | struct Params { |
139 | | WKTFormatter::Convention convention_ = WKTFormatter::Convention::WKT2; |
140 | | WKTFormatter::Version version_ = WKTFormatter::Version::WKT2; |
141 | | bool multiLine_ = true; |
142 | | bool strict_ = true; |
143 | | int indentWidth_ = 4; |
144 | | bool idOnTopLevelOnly_ = false; |
145 | | bool outputAxisOrder_ = false; |
146 | | bool primeMeridianOmittedIfGreenwich_ = false; |
147 | | bool ellipsoidUnitOmittedIfMetre_ = false; |
148 | | bool primeMeridianOrParameterUnitOmittedIfSameAsAxis_ = false; |
149 | | bool forceUNITKeyword_ = false; |
150 | | bool outputCSUnitOnlyOnceIfSame_ = false; |
151 | | bool primeMeridianInDegree_ = false; |
152 | | bool use2019Keywords_ = false; |
153 | | bool useESRIDialect_ = false; |
154 | | bool allowEllipsoidalHeightAsVerticalCRS_ = false; |
155 | | bool allowLINUNITNode_ = false; |
156 | | OutputAxisRule outputAxis_ = WKTFormatter::OutputAxisRule::YES; |
157 | | }; |
158 | | Params params_{}; |
159 | | DatabaseContextPtr dbContext_{}; |
160 | | |
161 | | int indentLevel_ = 0; |
162 | | int level_ = 0; |
163 | | std::vector<bool> stackHasChild_{}; |
164 | | std::vector<bool> stackHasId_{false}; |
165 | | std::vector<bool> stackEmptyKeyword_{}; |
166 | | std::vector<bool> stackDisableUsage_{}; |
167 | | std::vector<bool> outputUnitStack_{true}; |
168 | | std::vector<bool> outputIdStack_{true}; |
169 | | std::vector<UnitOfMeasureNNPtr> axisLinearUnitStack_{ |
170 | | util::nn_make_shared<UnitOfMeasure>(UnitOfMeasure::METRE)}; |
171 | | std::vector<UnitOfMeasureNNPtr> axisAngularUnitStack_{ |
172 | | util::nn_make_shared<UnitOfMeasure>(UnitOfMeasure::DEGREE)}; |
173 | | bool abridgedTransformation_ = false; |
174 | | bool useDerivingConversion_ = false; |
175 | | std::vector<double> toWGS84Parameters_{}; |
176 | | std::string hDatumExtension_{}; |
177 | | std::string vDatumExtension_{}; |
178 | | crs::GeographicCRSPtr geogCRSOfCompoundCRS_{}; |
179 | | std::string result_{}; |
180 | | |
181 | | // cppcheck-suppress functionStatic |
182 | | void addNewLine(); |
183 | | void addIndentation(); |
184 | | // cppcheck-suppress functionStatic |
185 | | void startNewChild(); |
186 | | }; |
187 | | //! @endcond |
188 | | |
189 | | // --------------------------------------------------------------------------- |
190 | | |
191 | | /** \brief Constructs a new formatter. |
192 | | * |
193 | | * A formatter can be used only once (its internal state is mutated) |
194 | | * |
195 | | * Its default behavior can be adjusted with the different setters. |
196 | | * |
197 | | * @param convention WKT flavor. Defaults to Convention::WKT2 |
198 | | * @param dbContext Database context, to allow queries in it if needed. |
199 | | * This is used for example for WKT1_ESRI output to do name substitutions. |
200 | | * |
201 | | * @return new formatter. |
202 | | */ |
203 | | WKTFormatterNNPtr WKTFormatter::create(Convention convention, |
204 | | // cppcheck-suppress passedByValue |
205 | 0 | DatabaseContextPtr dbContext) { |
206 | 0 | auto ret = NN_NO_CHECK(WKTFormatter::make_unique<WKTFormatter>(convention)); |
207 | 0 | ret->d->dbContext_ = std::move(dbContext); |
208 | 0 | return ret; |
209 | 0 | } |
210 | | |
211 | | // --------------------------------------------------------------------------- |
212 | | |
213 | | /** \brief Constructs a new formatter from another one. |
214 | | * |
215 | | * A formatter can be used only once (its internal state is mutated) |
216 | | * |
217 | | * Its default behavior can be adjusted with the different setters. |
218 | | * |
219 | | * @param other source formatter. |
220 | | * @return new formatter. |
221 | | */ |
222 | 0 | WKTFormatterNNPtr WKTFormatter::create(const WKTFormatterNNPtr &other) { |
223 | 0 | auto f = create(other->d->params_.convention_, other->d->dbContext_); |
224 | 0 | f->d->params_ = other->d->params_; |
225 | 0 | return f; |
226 | 0 | } |
227 | | |
228 | | // --------------------------------------------------------------------------- |
229 | | |
230 | | //! @cond Doxygen_Suppress |
231 | 0 | WKTFormatter::~WKTFormatter() = default; |
232 | | //! @endcond |
233 | | |
234 | | // --------------------------------------------------------------------------- |
235 | | |
236 | | /** \brief Whether to use multi line output or not. */ |
237 | 0 | WKTFormatter &WKTFormatter::setMultiLine(bool multiLine) noexcept { |
238 | 0 | d->params_.multiLine_ = multiLine; |
239 | 0 | return *this; |
240 | 0 | } |
241 | | |
242 | | // --------------------------------------------------------------------------- |
243 | | |
244 | | /** \brief Set number of spaces for each indentation level (defaults to 4). |
245 | | */ |
246 | 0 | WKTFormatter &WKTFormatter::setIndentationWidth(int width) noexcept { |
247 | 0 | d->params_.indentWidth_ = width; |
248 | 0 | return *this; |
249 | 0 | } |
250 | | |
251 | | // --------------------------------------------------------------------------- |
252 | | |
253 | | /** \brief Set whether AXIS nodes should be output. |
254 | | */ |
255 | | WKTFormatter & |
256 | 0 | WKTFormatter::setOutputAxis(OutputAxisRule outputAxisIn) noexcept { |
257 | 0 | d->params_.outputAxis_ = outputAxisIn; |
258 | 0 | return *this; |
259 | 0 | } |
260 | | |
261 | | // --------------------------------------------------------------------------- |
262 | | |
263 | | /** \brief Set whether the formatter should operate on strict more or not. |
264 | | * |
265 | | * The default is strict mode, in which case a FormattingException can be |
266 | | * thrown. |
267 | | * In non-strict mode, a Geographic 3D CRS can be for example exported as |
268 | | * WKT1_GDAL with 3 axes, whereas this is normally not allowed. |
269 | | */ |
270 | 0 | WKTFormatter &WKTFormatter::setStrict(bool strictIn) noexcept { |
271 | 0 | d->params_.strict_ = strictIn; |
272 | 0 | return *this; |
273 | 0 | } |
274 | | |
275 | | // --------------------------------------------------------------------------- |
276 | | |
277 | | /** \brief Returns whether the formatter is in strict mode. */ |
278 | 0 | bool WKTFormatter::isStrict() const noexcept { return d->params_.strict_; } |
279 | | |
280 | | // --------------------------------------------------------------------------- |
281 | | |
282 | | /** \brief Set whether the formatter should export, in WKT1, a Geographic or |
283 | | * Projected 3D CRS as a compound CRS whose vertical part represents an |
284 | | * ellipsoidal height. |
285 | | */ |
286 | | WKTFormatter & |
287 | 0 | WKTFormatter::setAllowEllipsoidalHeightAsVerticalCRS(bool allow) noexcept { |
288 | 0 | d->params_.allowEllipsoidalHeightAsVerticalCRS_ = allow; |
289 | 0 | return *this; |
290 | 0 | } |
291 | | |
292 | | // --------------------------------------------------------------------------- |
293 | | |
294 | | /** \brief Return whether the formatter should export, in WKT1, a Geographic or |
295 | | * Projected 3D CRS as a compound CRS whose vertical part represents an |
296 | | * ellipsoidal height. |
297 | | */ |
298 | 0 | bool WKTFormatter::isAllowedEllipsoidalHeightAsVerticalCRS() const noexcept { |
299 | 0 | return d->params_.allowEllipsoidalHeightAsVerticalCRS_; |
300 | 0 | } |
301 | | |
302 | | // --------------------------------------------------------------------------- |
303 | | |
304 | | /** \brief Set whether the formatter should export, in WKT1_ESRI, a Geographic |
305 | | * 3D CRS with the relatively new (ArcGIS Pro >= 2.7) LINUNIT node. |
306 | | * Defaults to true. |
307 | | * @since PROJ 9.1 |
308 | | */ |
309 | 0 | WKTFormatter &WKTFormatter::setAllowLINUNITNode(bool allow) noexcept { |
310 | 0 | d->params_.allowLINUNITNode_ = allow; |
311 | 0 | return *this; |
312 | 0 | } |
313 | | |
314 | | // --------------------------------------------------------------------------- |
315 | | |
316 | | /** \brief Return whether the formatter should export, in WKT1_ESRI, a |
317 | | * Geographic 3D CRS with the relatively new (ArcGIS Pro >= 2.7) LINUNIT node. |
318 | | * Defaults to true. |
319 | | * @since PROJ 9.1 |
320 | | */ |
321 | 0 | bool WKTFormatter::isAllowedLINUNITNode() const noexcept { |
322 | 0 | return d->params_.allowLINUNITNode_; |
323 | 0 | } |
324 | | |
325 | | // --------------------------------------------------------------------------- |
326 | | |
327 | | /** Returns the WKT string from the formatter. */ |
328 | 0 | const std::string &WKTFormatter::toString() const { |
329 | 0 | if (d->indentLevel_ > 0 || d->level_ > 0) { |
330 | | // For intermediary nodes, the formatter is in a inconsistent |
331 | | // state. |
332 | 0 | throw FormattingException("toString() called on intermediate nodes"); |
333 | 0 | } |
334 | 0 | if (d->axisLinearUnitStack_.size() != 1) |
335 | 0 | throw FormattingException( |
336 | 0 | "Unbalanced pushAxisLinearUnit() / popAxisLinearUnit()"); |
337 | 0 | if (d->axisAngularUnitStack_.size() != 1) |
338 | 0 | throw FormattingException( |
339 | 0 | "Unbalanced pushAxisAngularUnit() / popAxisAngularUnit()"); |
340 | 0 | if (d->outputIdStack_.size() != 1) |
341 | 0 | throw FormattingException("Unbalanced pushOutputId() / popOutputId()"); |
342 | 0 | if (d->outputUnitStack_.size() != 1) |
343 | 0 | throw FormattingException( |
344 | 0 | "Unbalanced pushOutputUnit() / popOutputUnit()"); |
345 | 0 | if (d->stackHasId_.size() != 1) |
346 | 0 | throw FormattingException("Unbalanced pushHasId() / popHasId()"); |
347 | 0 | if (!d->stackDisableUsage_.empty()) |
348 | 0 | throw FormattingException( |
349 | 0 | "Unbalanced pushDisableUsage() / popDisableUsage()"); |
350 | | |
351 | 0 | return d->result_; |
352 | 0 | } |
353 | | |
354 | | //! @cond Doxygen_Suppress |
355 | | |
356 | | // --------------------------------------------------------------------------- |
357 | | |
358 | | WKTFormatter::WKTFormatter(Convention convention) |
359 | 0 | : d(std::make_unique<Private>()) { |
360 | 0 | d->params_.convention_ = convention; |
361 | 0 | switch (convention) { |
362 | 0 | case Convention::WKT2_2019: |
363 | 0 | d->params_.use2019Keywords_ = true; |
364 | 0 | PROJ_FALLTHROUGH; |
365 | 0 | case Convention::WKT2: |
366 | 0 | d->params_.version_ = WKTFormatter::Version::WKT2; |
367 | 0 | d->params_.outputAxisOrder_ = true; |
368 | 0 | break; |
369 | | |
370 | 0 | case Convention::WKT2_2019_SIMPLIFIED: |
371 | 0 | d->params_.use2019Keywords_ = true; |
372 | 0 | PROJ_FALLTHROUGH; |
373 | 0 | case Convention::WKT2_SIMPLIFIED: |
374 | 0 | d->params_.version_ = WKTFormatter::Version::WKT2; |
375 | 0 | d->params_.idOnTopLevelOnly_ = true; |
376 | 0 | d->params_.outputAxisOrder_ = false; |
377 | 0 | d->params_.primeMeridianOmittedIfGreenwich_ = true; |
378 | 0 | d->params_.ellipsoidUnitOmittedIfMetre_ = true; |
379 | 0 | d->params_.primeMeridianOrParameterUnitOmittedIfSameAsAxis_ = true; |
380 | 0 | d->params_.forceUNITKeyword_ = true; |
381 | 0 | d->params_.outputCSUnitOnlyOnceIfSame_ = true; |
382 | 0 | break; |
383 | | |
384 | 0 | case Convention::WKT1_GDAL: |
385 | 0 | d->params_.version_ = WKTFormatter::Version::WKT1; |
386 | 0 | d->params_.outputAxisOrder_ = false; |
387 | 0 | d->params_.forceUNITKeyword_ = true; |
388 | 0 | d->params_.primeMeridianInDegree_ = true; |
389 | 0 | d->params_.outputAxis_ = |
390 | 0 | WKTFormatter::OutputAxisRule::WKT1_GDAL_EPSG_STYLE; |
391 | 0 | break; |
392 | | |
393 | 0 | case Convention::WKT1_ESRI: |
394 | 0 | d->params_.version_ = WKTFormatter::Version::WKT1; |
395 | 0 | d->params_.outputAxisOrder_ = false; |
396 | 0 | d->params_.forceUNITKeyword_ = true; |
397 | 0 | d->params_.primeMeridianInDegree_ = true; |
398 | 0 | d->params_.useESRIDialect_ = true; |
399 | 0 | d->params_.multiLine_ = false; |
400 | 0 | d->params_.outputAxis_ = WKTFormatter::OutputAxisRule::NO; |
401 | 0 | d->params_.allowLINUNITNode_ = true; |
402 | 0 | break; |
403 | | |
404 | 0 | default: |
405 | 0 | assert(false); |
406 | 0 | break; |
407 | 0 | } |
408 | 0 | } |
409 | | |
410 | | // --------------------------------------------------------------------------- |
411 | | |
412 | 0 | WKTFormatter &WKTFormatter::setOutputId(bool outputIdIn) { |
413 | 0 | if (d->indentLevel_ != 0) { |
414 | 0 | throw Exception( |
415 | 0 | "setOutputId() shall only be called when the stack state is empty"); |
416 | 0 | } |
417 | 0 | d->outputIdStack_[0] = outputIdIn; |
418 | 0 | return *this; |
419 | 0 | } |
420 | | |
421 | | // --------------------------------------------------------------------------- |
422 | | |
423 | 0 | void WKTFormatter::Private::addNewLine() { result_ += '\n'; } |
424 | | |
425 | | // --------------------------------------------------------------------------- |
426 | | |
427 | 0 | void WKTFormatter::Private::addIndentation() { |
428 | 0 | result_ += std::string( |
429 | 0 | static_cast<size_t>(indentLevel_) * params_.indentWidth_, ' '); |
430 | 0 | } |
431 | | |
432 | | // --------------------------------------------------------------------------- |
433 | | |
434 | 0 | void WKTFormatter::enter() { |
435 | 0 | if (d->indentLevel_ == 0 && d->level_ == 0) { |
436 | 0 | d->stackHasChild_.push_back(false); |
437 | 0 | } |
438 | 0 | ++d->level_; |
439 | 0 | } |
440 | | |
441 | | // --------------------------------------------------------------------------- |
442 | | |
443 | 0 | void WKTFormatter::leave() { |
444 | 0 | assert(d->level_ > 0); |
445 | 0 | --d->level_; |
446 | 0 | if (d->indentLevel_ == 0 && d->level_ == 0) { |
447 | 0 | d->stackHasChild_.pop_back(); |
448 | 0 | } |
449 | 0 | } |
450 | | |
451 | | // --------------------------------------------------------------------------- |
452 | | |
453 | 0 | bool WKTFormatter::isAtTopLevel() const { |
454 | 0 | return d->level_ == 0 && d->indentLevel_ == 0; |
455 | 0 | } |
456 | | |
457 | | // --------------------------------------------------------------------------- |
458 | | |
459 | 0 | void WKTFormatter::startNode(const std::string &keyword, bool hasId) { |
460 | 0 | if (!d->stackHasChild_.empty()) { |
461 | 0 | d->startNewChild(); |
462 | 0 | } else if (!d->result_.empty()) { |
463 | 0 | d->result_ += ','; |
464 | 0 | if (d->params_.multiLine_ && !keyword.empty()) { |
465 | 0 | d->addNewLine(); |
466 | 0 | } |
467 | 0 | } |
468 | |
|
469 | 0 | if (d->params_.multiLine_) { |
470 | 0 | if ((d->indentLevel_ || d->level_) && !keyword.empty()) { |
471 | 0 | if (!d->result_.empty()) { |
472 | 0 | d->addNewLine(); |
473 | 0 | } |
474 | 0 | d->addIndentation(); |
475 | 0 | } |
476 | 0 | } |
477 | |
|
478 | 0 | if (!keyword.empty()) { |
479 | 0 | d->result_ += keyword; |
480 | 0 | d->result_ += '['; |
481 | 0 | } |
482 | 0 | d->indentLevel_++; |
483 | 0 | d->stackHasChild_.push_back(false); |
484 | 0 | d->stackEmptyKeyword_.push_back(keyword.empty()); |
485 | | |
486 | | // Starting from a node that has a ID, we should emit ID nodes for : |
487 | | // - this node |
488 | | // - and for METHOD&PARAMETER nodes in WKT2, unless idOnTopLevelOnly_ is |
489 | | // set. |
490 | | // For WKT2, all other intermediate nodes shouldn't have ID ("not |
491 | | // recommended") |
492 | 0 | if (!d->params_.idOnTopLevelOnly_ && d->indentLevel_ >= 2 && |
493 | 0 | d->params_.version_ == WKTFormatter::Version::WKT2 && |
494 | 0 | (keyword == WKTConstants::METHOD || |
495 | 0 | keyword == WKTConstants::PARAMETER)) { |
496 | 0 | pushOutputId(d->outputIdStack_[0]); |
497 | 0 | } else if (d->indentLevel_ >= 2 && |
498 | 0 | d->params_.version_ == WKTFormatter::Version::WKT2) { |
499 | 0 | pushOutputId(d->outputIdStack_[0] && !d->stackHasId_.back()); |
500 | 0 | } else { |
501 | 0 | pushOutputId(outputId()); |
502 | 0 | } |
503 | |
|
504 | 0 | d->stackHasId_.push_back(hasId || d->stackHasId_.back()); |
505 | 0 | } |
506 | | |
507 | | // --------------------------------------------------------------------------- |
508 | | |
509 | 0 | void WKTFormatter::endNode() { |
510 | 0 | assert(d->indentLevel_ > 0); |
511 | 0 | d->stackHasId_.pop_back(); |
512 | 0 | popOutputId(); |
513 | 0 | d->indentLevel_--; |
514 | 0 | bool emptyKeyword = d->stackEmptyKeyword_.back(); |
515 | 0 | d->stackEmptyKeyword_.pop_back(); |
516 | 0 | d->stackHasChild_.pop_back(); |
517 | 0 | if (!emptyKeyword) |
518 | 0 | d->result_ += ']'; |
519 | 0 | } |
520 | | |
521 | | // --------------------------------------------------------------------------- |
522 | | |
523 | 0 | WKTFormatter &WKTFormatter::simulCurNodeHasId() { |
524 | 0 | d->stackHasId_.back() = true; |
525 | 0 | return *this; |
526 | 0 | } |
527 | | |
528 | | // --------------------------------------------------------------------------- |
529 | | |
530 | 0 | void WKTFormatter::Private::startNewChild() { |
531 | 0 | assert(!stackHasChild_.empty()); |
532 | 0 | if (stackHasChild_.back()) { |
533 | 0 | result_ += ','; |
534 | 0 | } |
535 | 0 | stackHasChild_.back() = true; |
536 | 0 | } |
537 | | |
538 | | // --------------------------------------------------------------------------- |
539 | | |
540 | 0 | void WKTFormatter::addQuotedString(const char *str) { |
541 | 0 | addQuotedString(std::string(str)); |
542 | 0 | } |
543 | | |
544 | 0 | void WKTFormatter::addQuotedString(const std::string &str) { |
545 | 0 | d->startNewChild(); |
546 | 0 | d->result_ += '"'; |
547 | 0 | d->result_ += replaceAll(str, "\"", "\"\""); |
548 | 0 | d->result_ += '"'; |
549 | 0 | } |
550 | | |
551 | | // --------------------------------------------------------------------------- |
552 | | |
553 | 0 | void WKTFormatter::add(const std::string &str) { |
554 | 0 | d->startNewChild(); |
555 | 0 | d->result_ += str; |
556 | 0 | } |
557 | | |
558 | | // --------------------------------------------------------------------------- |
559 | | |
560 | 0 | void WKTFormatter::add(int number) { |
561 | 0 | d->startNewChild(); |
562 | 0 | d->result_ += internal::toString(number); |
563 | 0 | } |
564 | | |
565 | | // --------------------------------------------------------------------------- |
566 | | |
567 | | #ifdef __MINGW32__ |
568 | | static std::string normalizeExponent(const std::string &in) { |
569 | | // mingw will output 1e-0xy instead of 1e-xy. Fix that |
570 | | auto pos = in.find("e-0"); |
571 | | if (pos == std::string::npos) { |
572 | | return in; |
573 | | } |
574 | | if (pos + 4 < in.size() && isdigit(in[pos + 3]) && isdigit(in[pos + 4])) { |
575 | | return in.substr(0, pos + 2) + in.substr(pos + 3); |
576 | | } |
577 | | return in; |
578 | | } |
579 | | #else |
580 | 981k | static inline std::string normalizeExponent(const std::string &in) { |
581 | 981k | return in; |
582 | 981k | } |
583 | | #endif |
584 | | |
585 | 981k | static inline std::string normalizeSerializedString(const std::string &in) { |
586 | 981k | auto ret(normalizeExponent(in)); |
587 | 981k | return ret; |
588 | 981k | } |
589 | | |
590 | | // --------------------------------------------------------------------------- |
591 | | |
592 | 0 | void WKTFormatter::add(double number, int precision) { |
593 | 0 | d->startNewChild(); |
594 | 0 | if (number == 0.0) { |
595 | 0 | if (d->params_.useESRIDialect_) { |
596 | 0 | d->result_ += "0.0"; |
597 | 0 | } else { |
598 | 0 | d->result_ += '0'; |
599 | 0 | } |
600 | 0 | } else { |
601 | 0 | std::string val( |
602 | 0 | normalizeSerializedString(internal::toString(number, precision))); |
603 | 0 | d->result_ += replaceAll(val, "e", "E"); |
604 | 0 | if (d->params_.useESRIDialect_ && val.find('.') == std::string::npos) { |
605 | 0 | d->result_ += ".0"; |
606 | 0 | } |
607 | 0 | } |
608 | 0 | } |
609 | | |
610 | | // --------------------------------------------------------------------------- |
611 | | |
612 | 0 | void WKTFormatter::pushOutputUnit(bool outputUnitIn) { |
613 | 0 | d->outputUnitStack_.push_back(outputUnitIn); |
614 | 0 | } |
615 | | |
616 | | // --------------------------------------------------------------------------- |
617 | | |
618 | 0 | void WKTFormatter::popOutputUnit() { d->outputUnitStack_.pop_back(); } |
619 | | |
620 | | // --------------------------------------------------------------------------- |
621 | | |
622 | 0 | bool WKTFormatter::outputUnit() const { return d->outputUnitStack_.back(); } |
623 | | |
624 | | // --------------------------------------------------------------------------- |
625 | | |
626 | 0 | void WKTFormatter::pushOutputId(bool outputIdIn) { |
627 | 0 | d->outputIdStack_.push_back(outputIdIn); |
628 | 0 | } |
629 | | |
630 | | // --------------------------------------------------------------------------- |
631 | | |
632 | 0 | void WKTFormatter::popOutputId() { d->outputIdStack_.pop_back(); } |
633 | | |
634 | | // --------------------------------------------------------------------------- |
635 | | |
636 | 0 | bool WKTFormatter::outputId() const { |
637 | 0 | return !d->params_.useESRIDialect_ && d->outputIdStack_.back(); |
638 | 0 | } |
639 | | |
640 | | // --------------------------------------------------------------------------- |
641 | | |
642 | 0 | void WKTFormatter::pushHasId(bool hasId) { d->stackHasId_.push_back(hasId); } |
643 | | |
644 | | // --------------------------------------------------------------------------- |
645 | | |
646 | 0 | void WKTFormatter::popHasId() { d->stackHasId_.pop_back(); } |
647 | | |
648 | | // --------------------------------------------------------------------------- |
649 | | |
650 | 0 | void WKTFormatter::pushDisableUsage() { d->stackDisableUsage_.push_back(true); } |
651 | | |
652 | | // --------------------------------------------------------------------------- |
653 | | |
654 | 0 | void WKTFormatter::popDisableUsage() { d->stackDisableUsage_.pop_back(); } |
655 | | |
656 | | // --------------------------------------------------------------------------- |
657 | | |
658 | 0 | bool WKTFormatter::outputUsage() const { |
659 | 0 | return outputId() && d->stackDisableUsage_.empty(); |
660 | 0 | } |
661 | | |
662 | | // --------------------------------------------------------------------------- |
663 | | |
664 | 0 | void WKTFormatter::pushAxisLinearUnit(const UnitOfMeasureNNPtr &unit) { |
665 | 0 | d->axisLinearUnitStack_.push_back(unit); |
666 | 0 | } |
667 | | // --------------------------------------------------------------------------- |
668 | | |
669 | 0 | void WKTFormatter::popAxisLinearUnit() { d->axisLinearUnitStack_.pop_back(); } |
670 | | |
671 | | // --------------------------------------------------------------------------- |
672 | | |
673 | 0 | const UnitOfMeasureNNPtr &WKTFormatter::axisLinearUnit() const { |
674 | 0 | return d->axisLinearUnitStack_.back(); |
675 | 0 | } |
676 | | |
677 | | // --------------------------------------------------------------------------- |
678 | | |
679 | 0 | void WKTFormatter::pushAxisAngularUnit(const UnitOfMeasureNNPtr &unit) { |
680 | 0 | d->axisAngularUnitStack_.push_back(unit); |
681 | 0 | } |
682 | | // --------------------------------------------------------------------------- |
683 | | |
684 | 0 | void WKTFormatter::popAxisAngularUnit() { d->axisAngularUnitStack_.pop_back(); } |
685 | | |
686 | | // --------------------------------------------------------------------------- |
687 | | |
688 | 0 | const UnitOfMeasureNNPtr &WKTFormatter::axisAngularUnit() const { |
689 | 0 | return d->axisAngularUnitStack_.back(); |
690 | 0 | } |
691 | | |
692 | | // --------------------------------------------------------------------------- |
693 | | |
694 | 0 | WKTFormatter::OutputAxisRule WKTFormatter::outputAxis() const { |
695 | 0 | return d->params_.outputAxis_; |
696 | 0 | } |
697 | | |
698 | | // --------------------------------------------------------------------------- |
699 | | |
700 | 0 | bool WKTFormatter::outputAxisOrder() const { |
701 | 0 | return d->params_.outputAxisOrder_; |
702 | 0 | } |
703 | | |
704 | | // --------------------------------------------------------------------------- |
705 | | |
706 | 0 | bool WKTFormatter::primeMeridianOmittedIfGreenwich() const { |
707 | 0 | return d->params_.primeMeridianOmittedIfGreenwich_; |
708 | 0 | } |
709 | | |
710 | | // --------------------------------------------------------------------------- |
711 | | |
712 | 0 | bool WKTFormatter::ellipsoidUnitOmittedIfMetre() const { |
713 | 0 | return d->params_.ellipsoidUnitOmittedIfMetre_; |
714 | 0 | } |
715 | | |
716 | | // --------------------------------------------------------------------------- |
717 | | |
718 | 0 | bool WKTFormatter::primeMeridianOrParameterUnitOmittedIfSameAsAxis() const { |
719 | 0 | return d->params_.primeMeridianOrParameterUnitOmittedIfSameAsAxis_; |
720 | 0 | } |
721 | | |
722 | | // --------------------------------------------------------------------------- |
723 | | |
724 | 0 | bool WKTFormatter::outputCSUnitOnlyOnceIfSame() const { |
725 | 0 | return d->params_.outputCSUnitOnlyOnceIfSame_; |
726 | 0 | } |
727 | | |
728 | | // --------------------------------------------------------------------------- |
729 | | |
730 | 0 | bool WKTFormatter::forceUNITKeyword() const { |
731 | 0 | return d->params_.forceUNITKeyword_; |
732 | 0 | } |
733 | | |
734 | | // --------------------------------------------------------------------------- |
735 | | |
736 | 0 | bool WKTFormatter::primeMeridianInDegree() const { |
737 | 0 | return d->params_.primeMeridianInDegree_; |
738 | 0 | } |
739 | | |
740 | | // --------------------------------------------------------------------------- |
741 | | |
742 | 0 | bool WKTFormatter::idOnTopLevelOnly() const { |
743 | 0 | return d->params_.idOnTopLevelOnly_; |
744 | 0 | } |
745 | | |
746 | | // --------------------------------------------------------------------------- |
747 | | |
748 | 0 | bool WKTFormatter::topLevelHasId() const { |
749 | 0 | return d->stackHasId_.size() >= 2 && d->stackHasId_[1]; |
750 | 0 | } |
751 | | |
752 | | // --------------------------------------------------------------------------- |
753 | | |
754 | 0 | WKTFormatter::Version WKTFormatter::version() const { |
755 | 0 | return d->params_.version_; |
756 | 0 | } |
757 | | |
758 | | // --------------------------------------------------------------------------- |
759 | | |
760 | 0 | bool WKTFormatter::use2019Keywords() const { |
761 | 0 | return d->params_.use2019Keywords_; |
762 | 0 | } |
763 | | |
764 | | // --------------------------------------------------------------------------- |
765 | | |
766 | 0 | bool WKTFormatter::useESRIDialect() const { return d->params_.useESRIDialect_; } |
767 | | |
768 | | // --------------------------------------------------------------------------- |
769 | | |
770 | 0 | const DatabaseContextPtr &WKTFormatter::databaseContext() const { |
771 | 0 | return d->dbContext_; |
772 | 0 | } |
773 | | |
774 | | // --------------------------------------------------------------------------- |
775 | | |
776 | 0 | void WKTFormatter::setAbridgedTransformation(bool outputIn) { |
777 | 0 | d->abridgedTransformation_ = outputIn; |
778 | 0 | } |
779 | | |
780 | | // --------------------------------------------------------------------------- |
781 | | |
782 | 0 | bool WKTFormatter::abridgedTransformation() const { |
783 | 0 | return d->abridgedTransformation_; |
784 | 0 | } |
785 | | |
786 | | // --------------------------------------------------------------------------- |
787 | | |
788 | 0 | void WKTFormatter::setUseDerivingConversion(bool useDerivingConversionIn) { |
789 | 0 | d->useDerivingConversion_ = useDerivingConversionIn; |
790 | 0 | } |
791 | | |
792 | | // --------------------------------------------------------------------------- |
793 | | |
794 | 0 | bool WKTFormatter::useDerivingConversion() const { |
795 | 0 | return d->useDerivingConversion_; |
796 | 0 | } |
797 | | |
798 | | // --------------------------------------------------------------------------- |
799 | | |
800 | 0 | void WKTFormatter::setTOWGS84Parameters(const std::vector<double> ¶ms) { |
801 | 0 | d->toWGS84Parameters_ = params; |
802 | 0 | } |
803 | | |
804 | | // --------------------------------------------------------------------------- |
805 | | |
806 | 0 | const std::vector<double> &WKTFormatter::getTOWGS84Parameters() const { |
807 | 0 | return d->toWGS84Parameters_; |
808 | 0 | } |
809 | | |
810 | | // --------------------------------------------------------------------------- |
811 | | |
812 | 0 | void WKTFormatter::setVDatumExtension(const std::string &filename) { |
813 | 0 | d->vDatumExtension_ = filename; |
814 | 0 | } |
815 | | |
816 | | // --------------------------------------------------------------------------- |
817 | | |
818 | 0 | const std::string &WKTFormatter::getVDatumExtension() const { |
819 | 0 | return d->vDatumExtension_; |
820 | 0 | } |
821 | | |
822 | | // --------------------------------------------------------------------------- |
823 | | |
824 | 0 | void WKTFormatter::setHDatumExtension(const std::string &filename) { |
825 | 0 | d->hDatumExtension_ = filename; |
826 | 0 | } |
827 | | |
828 | | // --------------------------------------------------------------------------- |
829 | | |
830 | 0 | const std::string &WKTFormatter::getHDatumExtension() const { |
831 | 0 | return d->hDatumExtension_; |
832 | 0 | } |
833 | | |
834 | | // --------------------------------------------------------------------------- |
835 | | |
836 | 0 | void WKTFormatter::setGeogCRSOfCompoundCRS(const crs::GeographicCRSPtr &crs) { |
837 | 0 | d->geogCRSOfCompoundCRS_ = crs; |
838 | 0 | } |
839 | | |
840 | | // --------------------------------------------------------------------------- |
841 | | |
842 | 0 | const crs::GeographicCRSPtr &WKTFormatter::getGeogCRSOfCompoundCRS() const { |
843 | 0 | return d->geogCRSOfCompoundCRS_; |
844 | 0 | } |
845 | | |
846 | | // --------------------------------------------------------------------------- |
847 | | |
848 | 0 | std::string WKTFormatter::morphNameToESRI(const std::string &name) { |
849 | |
|
850 | 0 | for (const auto *suffix : {"(m)", "(ftUS)", "(E-N)", "(N-E)"}) { |
851 | 0 | if (ends_with(name, suffix)) { |
852 | 0 | return morphNameToESRI( |
853 | 0 | name.substr(0, name.size() - strlen(suffix))) + |
854 | 0 | suffix; |
855 | 0 | } |
856 | 0 | } |
857 | | |
858 | 0 | std::string ret; |
859 | 0 | bool insertUnderscore = false; |
860 | | // Replace any special character by underscore, except at the beginning |
861 | | // and of the name where those characters are removed. |
862 | 0 | for (char ch : name) { |
863 | 0 | if (ch == '+' || ch == '-' || (ch >= '0' && ch <= '9') || |
864 | 0 | (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { |
865 | 0 | if (insertUnderscore && !ret.empty()) { |
866 | 0 | ret += '_'; |
867 | 0 | } |
868 | 0 | ret += ch; |
869 | 0 | insertUnderscore = false; |
870 | 0 | } else { |
871 | 0 | insertUnderscore = true; |
872 | 0 | } |
873 | 0 | } |
874 | 0 | return ret; |
875 | 0 | } |
876 | | |
877 | | // --------------------------------------------------------------------------- |
878 | | |
879 | 0 | void WKTFormatter::ingestWKTNode(const WKTNodeNNPtr &node) { |
880 | 0 | startNode(node->value(), true); |
881 | 0 | for (const auto &child : node->children()) { |
882 | 0 | if (!child->children().empty()) { |
883 | 0 | ingestWKTNode(child); |
884 | 0 | } else { |
885 | 0 | add(child->value()); |
886 | 0 | } |
887 | 0 | } |
888 | 0 | endNode(); |
889 | 0 | } |
890 | | |
891 | | //! @endcond |
892 | | |
893 | | // --------------------------------------------------------------------------- |
894 | | |
895 | | //! @cond Doxygen_Suppress |
896 | | |
897 | | static WKTNodeNNPtr |
898 | | null_node(NN_NO_CHECK(std::make_unique<WKTNode>(std::string()))); |
899 | | |
900 | 152k | static inline bool isNull(const WKTNodeNNPtr &node) { |
901 | 152k | return &node == &null_node; |
902 | 152k | } |
903 | | |
904 | | struct WKTNode::Private { |
905 | | std::string value_{}; |
906 | | std::vector<WKTNodeNNPtr> children_{}; |
907 | | |
908 | 60.0k | explicit Private(const std::string &valueIn) : value_(valueIn) {} |
909 | | |
910 | | // cppcheck-suppress functionStatic |
911 | 732k | inline const std::string &value() PROJ_PURE_DEFN { return value_; } |
912 | | |
913 | | // cppcheck-suppress functionStatic |
914 | 54.1k | inline const std::vector<WKTNodeNNPtr> &children() PROJ_PURE_DEFN { |
915 | 54.1k | return children_; |
916 | 54.1k | } |
917 | | |
918 | | // cppcheck-suppress functionStatic |
919 | 8.40k | inline size_t childrenSize() PROJ_PURE_DEFN { return children_.size(); } |
920 | | |
921 | | // cppcheck-suppress functionStatic |
922 | | const WKTNodeNNPtr &lookForChild(const std::string &childName, |
923 | | int occurrence) const noexcept; |
924 | | |
925 | | // cppcheck-suppress functionStatic |
926 | | const WKTNodeNNPtr &lookForChild(const std::string &name) const noexcept; |
927 | | |
928 | | // cppcheck-suppress functionStatic |
929 | | const WKTNodeNNPtr &lookForChild(const std::string &name, |
930 | | const std::string &name2) const noexcept; |
931 | | |
932 | | // cppcheck-suppress functionStatic |
933 | | const WKTNodeNNPtr &lookForChild(const std::string &name, |
934 | | const std::string &name2, |
935 | | const std::string &name3) const noexcept; |
936 | | |
937 | | // cppcheck-suppress functionStatic |
938 | | const WKTNodeNNPtr &lookForChild(const std::string &name, |
939 | | const std::string &name2, |
940 | | const std::string &name3, |
941 | | const std::string &name4) const noexcept; |
942 | | }; |
943 | | |
944 | 794k | #define GP() getPrivate() |
945 | | |
946 | | // --------------------------------------------------------------------------- |
947 | | |
948 | | const WKTNodeNNPtr & |
949 | | WKTNode::Private::lookForChild(const std::string &childName, |
950 | 186 | int occurrence) const noexcept { |
951 | 186 | int occCount = 0; |
952 | 529 | for (const auto &child : children_) { |
953 | 529 | if (ci_equal(child->GP()->value(), childName)) { |
954 | 187 | if (occurrence == occCount) { |
955 | 186 | return child; |
956 | 186 | } |
957 | 1 | occCount++; |
958 | 1 | } |
959 | 529 | } |
960 | 0 | return null_node; |
961 | 186 | } |
962 | | |
963 | | const WKTNodeNNPtr & |
964 | 140k | WKTNode::Private::lookForChild(const std::string &name) const noexcept { |
965 | 538k | for (const auto &child : children_) { |
966 | 538k | const auto &v = child->GP()->value(); |
967 | 538k | if (ci_equal(v, name)) { |
968 | 3.62k | return child; |
969 | 3.62k | } |
970 | 538k | } |
971 | 137k | return null_node; |
972 | 140k | } |
973 | | |
974 | | const WKTNodeNNPtr & |
975 | | WKTNode::Private::lookForChild(const std::string &name, |
976 | 5.11k | const std::string &name2) const noexcept { |
977 | 15.8k | for (const auto &child : children_) { |
978 | 15.8k | const auto &v = child->GP()->value(); |
979 | 15.8k | if (ci_equal(v, name) || ci_equal(v, name2)) { |
980 | 1.86k | return child; |
981 | 1.86k | } |
982 | 15.8k | } |
983 | 3.25k | return null_node; |
984 | 5.11k | } |
985 | | |
986 | | const WKTNodeNNPtr & |
987 | | WKTNode::Private::lookForChild(const std::string &name, |
988 | | const std::string &name2, |
989 | 1.81k | const std::string &name3) const noexcept { |
990 | 3.40k | for (const auto &child : children_) { |
991 | 3.40k | const auto &v = child->GP()->value(); |
992 | 3.40k | if (ci_equal(v, name) || ci_equal(v, name2) || ci_equal(v, name3)) { |
993 | 1.65k | return child; |
994 | 1.65k | } |
995 | 3.40k | } |
996 | 158 | return null_node; |
997 | 1.81k | } |
998 | | |
999 | | const WKTNodeNNPtr &WKTNode::Private::lookForChild( |
1000 | | const std::string &name, const std::string &name2, const std::string &name3, |
1001 | 672 | const std::string &name4) const noexcept { |
1002 | 1.49k | for (const auto &child : children_) { |
1003 | 1.49k | const auto &v = child->GP()->value(); |
1004 | 1.49k | if (ci_equal(v, name) || ci_equal(v, name2) || ci_equal(v, name3) || |
1005 | 1.49k | ci_equal(v, name4)) { |
1006 | 602 | return child; |
1007 | 602 | } |
1008 | 1.49k | } |
1009 | 70 | return null_node; |
1010 | 672 | } |
1011 | | |
1012 | | //! @endcond |
1013 | | |
1014 | | // --------------------------------------------------------------------------- |
1015 | | |
1016 | | /** \brief Instantiate a WKTNode. |
1017 | | * |
1018 | | * @param valueIn the name of the node. |
1019 | | */ |
1020 | | WKTNode::WKTNode(const std::string &valueIn) |
1021 | 60.0k | : d(std::make_unique<Private>(valueIn)) {} |
1022 | | |
1023 | | // --------------------------------------------------------------------------- |
1024 | | |
1025 | | //! @cond Doxygen_Suppress |
1026 | 60.0k | WKTNode::~WKTNode() = default; |
1027 | | //! @endcond |
1028 | | |
1029 | | // --------------------------------------------------------------------------- |
1030 | | |
1031 | | /** \brief Adds a child to the current node. |
1032 | | * |
1033 | | * @param child child to add. This should not be a parent of this node. |
1034 | | */ |
1035 | 56.8k | void WKTNode::addChild(WKTNodeNNPtr &&child) { |
1036 | 56.8k | d->children_.push_back(std::move(child)); |
1037 | 56.8k | } |
1038 | | |
1039 | | // --------------------------------------------------------------------------- |
1040 | | |
1041 | | /** \brief Return the (occurrence-1)th sub-node of name childName. |
1042 | | * |
1043 | | * @param childName name of the child. |
1044 | | * @param occurrence occurrence index (starting at 0) |
1045 | | * @return the child, or nullptr. |
1046 | | */ |
1047 | | const WKTNodePtr &WKTNode::lookForChild(const std::string &childName, |
1048 | 1.48k | int occurrence) const noexcept { |
1049 | 1.48k | int occCount = 0; |
1050 | 8.93k | for (const auto &child : d->children_) { |
1051 | 8.93k | if (ci_equal(child->GP()->value(), childName)) { |
1052 | 118 | if (occurrence == occCount) { |
1053 | 118 | return child; |
1054 | 118 | } |
1055 | 0 | occCount++; |
1056 | 0 | } |
1057 | 8.93k | } |
1058 | 1.36k | return null_node; |
1059 | 1.48k | } |
1060 | | |
1061 | | // --------------------------------------------------------------------------- |
1062 | | |
1063 | | /** \brief Return the count of children of given name. |
1064 | | * |
1065 | | * @param childName name of the children to look for. |
1066 | | * @return count |
1067 | | */ |
1068 | 3.07k | int WKTNode::countChildrenOfName(const std::string &childName) const noexcept { |
1069 | 3.07k | int occCount = 0; |
1070 | 16.5k | for (const auto &child : d->children_) { |
1071 | 16.5k | if (ci_equal(child->GP()->value(), childName)) { |
1072 | 216 | occCount++; |
1073 | 216 | } |
1074 | 16.5k | } |
1075 | 3.07k | return occCount; |
1076 | 3.07k | } |
1077 | | |
1078 | | // --------------------------------------------------------------------------- |
1079 | | |
1080 | | /** \brief Return the value of a node. |
1081 | | */ |
1082 | 0 | const std::string &WKTNode::value() const { return d->value_; } |
1083 | | |
1084 | | // --------------------------------------------------------------------------- |
1085 | | |
1086 | | /** \brief Return the children of a node. |
1087 | | */ |
1088 | 0 | const std::vector<WKTNodeNNPtr> &WKTNode::children() const { |
1089 | 0 | return d->children_; |
1090 | 0 | } |
1091 | | |
1092 | | // --------------------------------------------------------------------------- |
1093 | | |
1094 | | //! @cond Doxygen_Suppress |
1095 | 208k | static size_t skipSpace(const std::string &str, size_t start) { |
1096 | 208k | size_t i = start; |
1097 | 210k | while (i < str.size() && ::isspace(static_cast<unsigned char>(str[i]))) { |
1098 | 2.94k | ++i; |
1099 | 2.94k | } |
1100 | 208k | return i; |
1101 | 208k | } |
1102 | | //! @endcond |
1103 | | |
1104 | | // --------------------------------------------------------------------------- |
1105 | | |
1106 | | //! @cond Doxygen_Suppress |
1107 | | // As used in examples of OGC 12-063r5 |
1108 | | static const std::string startPrintedQuote("\xE2\x80\x9C"); |
1109 | | static const std::string endPrintedQuote("\xE2\x80\x9D"); |
1110 | | //! @endcond |
1111 | | |
1112 | | WKTNodeNNPtr WKTNode::createFrom(const std::string &wkt, size_t indexStart, |
1113 | 60.2k | int recLevel, size_t &indexEnd) { |
1114 | 60.2k | if (recLevel == 16) { |
1115 | 0 | throw ParsingException("too many nesting levels"); |
1116 | 0 | } |
1117 | 60.2k | std::string value; |
1118 | 60.2k | size_t i = skipSpace(wkt, indexStart); |
1119 | 60.2k | if (i == wkt.size()) { |
1120 | 0 | throw ParsingException("whitespace only string"); |
1121 | 0 | } |
1122 | 60.2k | std::string closingStringMarker; |
1123 | 60.2k | bool inString = false; |
1124 | | |
1125 | 499k | for (; i < wkt.size() && |
1126 | 499k | (inString || |
1127 | 499k | (wkt[i] != '[' && wkt[i] != '(' && wkt[i] != ',' && wkt[i] != ']' && |
1128 | 373k | wkt[i] != ')' && !::isspace(static_cast<unsigned char>(wkt[i])))); |
1129 | 439k | ++i) { |
1130 | 439k | if (wkt[i] == '"') { |
1131 | 15.7k | if (!inString) { |
1132 | 7.65k | inString = true; |
1133 | 7.65k | closingStringMarker = "\""; |
1134 | 8.05k | } else if (closingStringMarker == "\"") { |
1135 | 7.90k | if (i + 1 < wkt.size() && wkt[i + 1] == '"') { |
1136 | 285 | i++; |
1137 | 7.62k | } else { |
1138 | 7.62k | inString = false; |
1139 | 7.62k | closingStringMarker.clear(); |
1140 | 7.62k | } |
1141 | 7.90k | } |
1142 | 423k | } else if (i + 3 <= wkt.size() && |
1143 | 423k | wkt.substr(i, 3) == startPrintedQuote) { |
1144 | 132 | if (!inString) { |
1145 | 79 | inString = true; |
1146 | 79 | closingStringMarker = endPrintedQuote; |
1147 | 79 | value += '"'; |
1148 | 79 | i += 2; |
1149 | 79 | continue; |
1150 | 79 | } |
1151 | 423k | } else if (i + 3 <= wkt.size() && |
1152 | 423k | closingStringMarker == endPrintedQuote && |
1153 | 423k | wkt.substr(i, 3) == endPrintedQuote) { |
1154 | 51 | inString = false; |
1155 | 51 | closingStringMarker.clear(); |
1156 | 51 | value += '"'; |
1157 | 51 | i += 2; |
1158 | 51 | continue; |
1159 | 51 | } |
1160 | 438k | value += wkt[i]; |
1161 | 438k | } |
1162 | 60.2k | i = skipSpace(wkt, i); |
1163 | 60.2k | if (i == wkt.size()) { |
1164 | 232 | if (indexStart == 0) { |
1165 | 0 | throw ParsingException("missing ["); |
1166 | 232 | } else { |
1167 | 232 | throw ParsingException("missing , or ]"); |
1168 | 232 | } |
1169 | 232 | } |
1170 | | |
1171 | 60.0k | auto node = NN_NO_CHECK(std::make_unique<WKTNode>(value)); |
1172 | | |
1173 | 60.0k | if (indexStart > 0) { |
1174 | 57.4k | if (wkt[i] == ',') { |
1175 | 21.4k | indexEnd = i + 1; |
1176 | 21.4k | return node; |
1177 | 21.4k | } |
1178 | 35.9k | if (wkt[i] == ']' || wkt[i] == ')') { |
1179 | 17.1k | indexEnd = i; |
1180 | 17.1k | return node; |
1181 | 17.1k | } |
1182 | 35.9k | } |
1183 | 21.4k | if (wkt[i] != '[' && wkt[i] != '(') { |
1184 | 40 | throw ParsingException("missing ["); |
1185 | 40 | } |
1186 | 21.3k | ++i; // skip [ |
1187 | 21.3k | i = skipSpace(wkt, i); |
1188 | 78.8k | while (i < wkt.size() && wkt[i] != ']' && wkt[i] != ')') { |
1189 | 57.4k | size_t indexEndChild; |
1190 | 57.4k | node->addChild(createFrom(wkt, i, recLevel + 1, indexEndChild)); |
1191 | 57.4k | assert(indexEndChild > i); |
1192 | 57.4k | i = indexEndChild; |
1193 | 57.4k | i = skipSpace(wkt, i); |
1194 | 57.4k | if (i < wkt.size() && wkt[i] == ',') { |
1195 | 8.23k | ++i; |
1196 | 8.23k | i = skipSpace(wkt, i); |
1197 | 8.23k | } |
1198 | 57.4k | } |
1199 | 21.3k | if (i == wkt.size() || (wkt[i] != ']' && wkt[i] != ')')) { |
1200 | 62 | throw ParsingException("missing ]"); |
1201 | 62 | } |
1202 | 21.3k | indexEnd = i + 1; |
1203 | 21.3k | return node; |
1204 | 21.3k | } |
1205 | | // --------------------------------------------------------------------------- |
1206 | | |
1207 | | /** \brief Instantiate a WKTNode hierarchy from a WKT string. |
1208 | | * |
1209 | | * @param wkt the WKT string to parse. |
1210 | | * @param indexStart the start index in the wkt string. |
1211 | | * @throw ParsingException if the string cannot be parsed. |
1212 | | */ |
1213 | 0 | WKTNodeNNPtr WKTNode::createFrom(const std::string &wkt, size_t indexStart) { |
1214 | 0 | size_t indexEnd; |
1215 | 0 | return createFrom(wkt, indexStart, 0, indexEnd); |
1216 | 0 | } |
1217 | | |
1218 | | // --------------------------------------------------------------------------- |
1219 | | |
1220 | | //! @cond Doxygen_Suppress |
1221 | 0 | static std::string escapeIfQuotedString(const std::string &str) { |
1222 | 0 | if (str.size() > 2 && str[0] == '"' && str.back() == '"') { |
1223 | 0 | std::string res("\""); |
1224 | 0 | res += replaceAll(str.substr(1, str.size() - 2), "\"", "\"\""); |
1225 | 0 | res += '"'; |
1226 | 0 | return res; |
1227 | 0 | } else { |
1228 | 0 | return str; |
1229 | 0 | } |
1230 | 0 | } |
1231 | | //! @endcond |
1232 | | |
1233 | | // --------------------------------------------------------------------------- |
1234 | | |
1235 | | /** \brief Return a WKT representation of the tree structure. |
1236 | | */ |
1237 | 0 | std::string WKTNode::toString() const { |
1238 | 0 | std::string str(escapeIfQuotedString(d->value_)); |
1239 | 0 | if (!d->children_.empty()) { |
1240 | 0 | str += "["; |
1241 | 0 | bool first = true; |
1242 | 0 | for (auto &child : d->children_) { |
1243 | 0 | if (!first) { |
1244 | 0 | str += ','; |
1245 | 0 | } |
1246 | 0 | first = false; |
1247 | 0 | str += child->toString(); |
1248 | 0 | } |
1249 | 0 | str += "]"; |
1250 | 0 | } |
1251 | 0 | return str; |
1252 | 0 | } |
1253 | | |
1254 | | // --------------------------------------------------------------------------- |
1255 | | |
1256 | | //! @cond Doxygen_Suppress |
1257 | | struct WKTParser::Private { |
1258 | | |
1259 | | struct ci_less_struct { |
1260 | | bool operator()(const std::string &lhs, |
1261 | 20.2k | const std::string &rhs) const noexcept { |
1262 | 20.2k | return ci_less(lhs, rhs); |
1263 | 20.2k | } |
1264 | | }; |
1265 | | |
1266 | | bool strict_ = true; |
1267 | | bool unsetIdentifiersIfIncompatibleDef_ = true; |
1268 | | std::list<std::string> warningList_{}; |
1269 | | std::list<std::string> grammarErrorList_{}; |
1270 | | std::vector<double> toWGS84Parameters_{}; |
1271 | | std::string datumPROJ4Grids_{}; |
1272 | | bool esriStyle_ = false; |
1273 | | bool maybeEsriStyle_ = false; |
1274 | | DatabaseContextPtr dbContext_{}; |
1275 | | crs::GeographicCRSPtr geogCRSOfCompoundCRS_{}; |
1276 | | |
1277 | | static constexpr unsigned int MAX_PROPERTY_SIZE = 1024; |
1278 | | std::vector<std::unique_ptr<PropertyMap>> properties_{}; |
1279 | | |
1280 | 2.58k | Private() = default; |
1281 | 2.58k | ~Private() = default; |
1282 | | Private(const Private &) = delete; |
1283 | | Private &operator=(const Private &) = delete; |
1284 | | |
1285 | | void emitRecoverableWarning(const std::string &warningMsg); |
1286 | | void emitGrammarError(const std::string &errorMsg); |
1287 | | void emitRecoverableMissingUNIT(const std::string &parentNodeName, |
1288 | | const UnitOfMeasure &fallbackUnit); |
1289 | | |
1290 | | BaseObjectNNPtr build(const WKTNodeNNPtr &node); |
1291 | | |
1292 | | IdentifierPtr buildId(const WKTNodeNNPtr &parentNode, |
1293 | | const WKTNodeNNPtr &node, bool tolerant, |
1294 | | bool removeInverseOf); |
1295 | | |
1296 | | PropertyMap &buildProperties(const WKTNodeNNPtr &node, |
1297 | | bool removeInverseOf = false, |
1298 | | bool hasName = true); |
1299 | | |
1300 | | ObjectDomainPtr buildObjectDomain(const WKTNodeNNPtr &node); |
1301 | | |
1302 | | static std::string stripQuotes(const WKTNodeNNPtr &node); |
1303 | | |
1304 | | static double asDouble(const WKTNodeNNPtr &node); |
1305 | | |
1306 | | UnitOfMeasure |
1307 | | buildUnit(const WKTNodeNNPtr &node, |
1308 | | UnitOfMeasure::Type type = UnitOfMeasure::Type::UNKNOWN); |
1309 | | |
1310 | | UnitOfMeasure buildUnitInSubNode( |
1311 | | const WKTNodeNNPtr &node, |
1312 | | common::UnitOfMeasure::Type type = UnitOfMeasure::Type::UNKNOWN); |
1313 | | |
1314 | | EllipsoidNNPtr buildEllipsoid(const WKTNodeNNPtr &node); |
1315 | | |
1316 | | PrimeMeridianNNPtr |
1317 | | buildPrimeMeridian(const WKTNodeNNPtr &node, |
1318 | | const UnitOfMeasure &defaultAngularUnit); |
1319 | | |
1320 | | static optional<std::string> getAnchor(const WKTNodeNNPtr &node); |
1321 | | |
1322 | | static optional<common::Measure> getAnchorEpoch(const WKTNodeNNPtr &node); |
1323 | | |
1324 | | static void parseDynamic(const WKTNodeNNPtr &dynamicNode, |
1325 | | double &frameReferenceEpoch, |
1326 | | util::optional<std::string> &modelName); |
1327 | | |
1328 | | GeodeticReferenceFrameNNPtr |
1329 | | buildGeodeticReferenceFrame(const WKTNodeNNPtr &node, |
1330 | | const PrimeMeridianNNPtr &primeMeridian, |
1331 | | const WKTNodeNNPtr &dynamicNode); |
1332 | | |
1333 | | DatumEnsembleNNPtr buildDatumEnsemble(const WKTNodeNNPtr &node, |
1334 | | const PrimeMeridianPtr &primeMeridian, |
1335 | | bool expectEllipsoid); |
1336 | | |
1337 | | MeridianNNPtr buildMeridian(const WKTNodeNNPtr &node); |
1338 | | CoordinateSystemAxisNNPtr buildAxis(const WKTNodeNNPtr &node, |
1339 | | const UnitOfMeasure &unitIn, |
1340 | | const UnitOfMeasure::Type &unitType, |
1341 | | bool isGeocentric, |
1342 | | int expectedOrderNum); |
1343 | | |
1344 | | CoordinateSystemNNPtr buildCS(const WKTNodeNNPtr &node, /* maybe null */ |
1345 | | const WKTNodeNNPtr &parentNode, |
1346 | | const UnitOfMeasure &defaultAngularUnit); |
1347 | | |
1348 | | GeodeticCRSNNPtr buildGeodeticCRS(const WKTNodeNNPtr &node, |
1349 | | bool forceGeocentricIfNoCs = false); |
1350 | | |
1351 | | CRSNNPtr buildDerivedGeodeticCRS(const WKTNodeNNPtr &node); |
1352 | | |
1353 | | static UnitOfMeasure |
1354 | | guessUnitForParameter(const std::string ¶mName, |
1355 | | const UnitOfMeasure &defaultLinearUnit, |
1356 | | const UnitOfMeasure &defaultAngularUnit); |
1357 | | |
1358 | | void consumeParameters(const WKTNodeNNPtr &node, bool isAbridged, |
1359 | | std::vector<OperationParameterNNPtr> ¶meters, |
1360 | | std::vector<ParameterValueNNPtr> &values, |
1361 | | const UnitOfMeasure &defaultLinearUnit, |
1362 | | const UnitOfMeasure &defaultAngularUnit); |
1363 | | |
1364 | | static std::string getExtensionProj4(const WKTNode::Private *nodeP); |
1365 | | |
1366 | | static void addExtensionProj4ToProp(const WKTNode::Private *nodeP, |
1367 | | PropertyMap &props); |
1368 | | |
1369 | | ConversionNNPtr buildConversion(const WKTNodeNNPtr &node, |
1370 | | const UnitOfMeasure &defaultLinearUnit, |
1371 | | const UnitOfMeasure &defaultAngularUnit); |
1372 | | |
1373 | | static bool hasWebMercPROJ4String(const WKTNodeNNPtr &projCRSNode, |
1374 | | const WKTNodeNNPtr &projectionNode); |
1375 | | |
1376 | | static std::string projectionGetParameter(const WKTNodeNNPtr &projCRSNode, |
1377 | | const char *paramName); |
1378 | | |
1379 | | ConversionNNPtr buildProjection(const GeodeticCRSNNPtr &baseGeodCRS, |
1380 | | const WKTNodeNNPtr &projCRSNode, |
1381 | | const WKTNodeNNPtr &projectionNode, |
1382 | | const UnitOfMeasure &defaultLinearUnit, |
1383 | | const UnitOfMeasure &defaultAngularUnit); |
1384 | | |
1385 | | ConversionNNPtr |
1386 | | buildProjectionStandard(const GeodeticCRSNNPtr &baseGeodCRS, |
1387 | | const WKTNodeNNPtr &projCRSNode, |
1388 | | const WKTNodeNNPtr &projectionNode, |
1389 | | const UnitOfMeasure &defaultLinearUnit, |
1390 | | const UnitOfMeasure &defaultAngularUnit); |
1391 | | |
1392 | | const ESRIMethodMapping * |
1393 | | getESRIMapping(const WKTNodeNNPtr &projCRSNode, |
1394 | | const WKTNodeNNPtr &projectionNode, |
1395 | | std::map<std::string, std::string, ci_less_struct> |
1396 | | &mapParamNameToValue); |
1397 | | |
1398 | | static ConversionNNPtr |
1399 | | buildProjectionFromESRI(const GeodeticCRSNNPtr &baseGeodCRS, |
1400 | | const WKTNodeNNPtr &projCRSNode, |
1401 | | const WKTNodeNNPtr &projectionNode, |
1402 | | const UnitOfMeasure &defaultLinearUnit, |
1403 | | const UnitOfMeasure &defaultAngularUnit, |
1404 | | const ESRIMethodMapping *esriMapping, |
1405 | | std::map<std::string, std::string, ci_less_struct> |
1406 | | &mapParamNameToValue); |
1407 | | |
1408 | | ConversionNNPtr |
1409 | | buildProjectionFromESRI(const GeodeticCRSNNPtr &baseGeodCRS, |
1410 | | const WKTNodeNNPtr &projCRSNode, |
1411 | | const WKTNodeNNPtr &projectionNode, |
1412 | | const UnitOfMeasure &defaultLinearUnit, |
1413 | | const UnitOfMeasure &defaultAngularUnit); |
1414 | | |
1415 | | ProjectedCRSNNPtr buildProjectedCRS(const WKTNodeNNPtr &node); |
1416 | | |
1417 | | VerticalReferenceFrameNNPtr |
1418 | | buildVerticalReferenceFrame(const WKTNodeNNPtr &node, |
1419 | | const WKTNodeNNPtr &dynamicNode); |
1420 | | |
1421 | | TemporalDatumNNPtr buildTemporalDatum(const WKTNodeNNPtr &node); |
1422 | | |
1423 | | EngineeringDatumNNPtr buildEngineeringDatum(const WKTNodeNNPtr &node); |
1424 | | |
1425 | | ParametricDatumNNPtr buildParametricDatum(const WKTNodeNNPtr &node); |
1426 | | |
1427 | | CRSNNPtr buildVerticalCRS(const WKTNodeNNPtr &node); |
1428 | | |
1429 | | DerivedVerticalCRSNNPtr buildDerivedVerticalCRS(const WKTNodeNNPtr &node); |
1430 | | |
1431 | | CRSNNPtr buildCompoundCRS(const WKTNodeNNPtr &node); |
1432 | | |
1433 | | BoundCRSNNPtr buildBoundCRS(const WKTNodeNNPtr &node); |
1434 | | |
1435 | | TemporalCSNNPtr buildTemporalCS(const WKTNodeNNPtr &parentNode); |
1436 | | |
1437 | | TemporalCRSNNPtr buildTemporalCRS(const WKTNodeNNPtr &node); |
1438 | | |
1439 | | DerivedTemporalCRSNNPtr buildDerivedTemporalCRS(const WKTNodeNNPtr &node); |
1440 | | |
1441 | | EngineeringCRSNNPtr buildEngineeringCRS(const WKTNodeNNPtr &node); |
1442 | | |
1443 | | EngineeringCRSNNPtr |
1444 | | buildEngineeringCRSFromLocalCS(const WKTNodeNNPtr &node); |
1445 | | |
1446 | | DerivedEngineeringCRSNNPtr |
1447 | | buildDerivedEngineeringCRS(const WKTNodeNNPtr &node); |
1448 | | |
1449 | | ParametricCSNNPtr buildParametricCS(const WKTNodeNNPtr &parentNode); |
1450 | | |
1451 | | ParametricCRSNNPtr buildParametricCRS(const WKTNodeNNPtr &node); |
1452 | | |
1453 | | DerivedParametricCRSNNPtr |
1454 | | buildDerivedParametricCRS(const WKTNodeNNPtr &node); |
1455 | | |
1456 | | DerivedProjectedCRSNNPtr buildDerivedProjectedCRS(const WKTNodeNNPtr &node); |
1457 | | |
1458 | | CRSPtr buildCRS(const WKTNodeNNPtr &node); |
1459 | | |
1460 | | TransformationNNPtr buildCoordinateOperation(const WKTNodeNNPtr &node); |
1461 | | |
1462 | | PointMotionOperationNNPtr |
1463 | | buildPointMotionOperation(const WKTNodeNNPtr &node); |
1464 | | |
1465 | | ConcatenatedOperationNNPtr |
1466 | | buildConcatenatedOperation(const WKTNodeNNPtr &node); |
1467 | | |
1468 | | CoordinateMetadataNNPtr buildCoordinateMetadata(const WKTNodeNNPtr &node); |
1469 | | }; |
1470 | | //! @endcond |
1471 | | |
1472 | | // --------------------------------------------------------------------------- |
1473 | | |
1474 | 2.58k | WKTParser::WKTParser() : d(std::make_unique<Private>()) {} |
1475 | | |
1476 | | // --------------------------------------------------------------------------- |
1477 | | |
1478 | | //! @cond Doxygen_Suppress |
1479 | 2.58k | WKTParser::~WKTParser() = default; |
1480 | | //! @endcond |
1481 | | |
1482 | | // --------------------------------------------------------------------------- |
1483 | | |
1484 | | /** \brief Set whether parsing should be done in strict mode. |
1485 | | */ |
1486 | 2.58k | WKTParser &WKTParser::setStrict(bool strict) { |
1487 | 2.58k | d->strict_ = strict; |
1488 | 2.58k | return *this; |
1489 | 2.58k | } |
1490 | | |
1491 | | // --------------------------------------------------------------------------- |
1492 | | |
1493 | | /** \brief Set whether object identifiers should be unset when there is |
1494 | | * a contradiction between the definition from WKT and the one from |
1495 | | * the database. |
1496 | | * |
1497 | | * At time of writing, this only applies to the base geographic CRS of a |
1498 | | * projected CRS, when comparing its coordinate system. |
1499 | | */ |
1500 | 0 | WKTParser &WKTParser::setUnsetIdentifiersIfIncompatibleDef(bool unset) { |
1501 | 0 | d->unsetIdentifiersIfIncompatibleDef_ = unset; |
1502 | 0 | return *this; |
1503 | 0 | } |
1504 | | |
1505 | | // --------------------------------------------------------------------------- |
1506 | | |
1507 | | /** \brief Return the list of warnings found during parsing. |
1508 | | * |
1509 | | * \note The list might be non-empty only is setStrict(false) has been called. |
1510 | | */ |
1511 | 0 | std::list<std::string> WKTParser::warningList() const { |
1512 | 0 | return d->warningList_; |
1513 | 0 | } |
1514 | | |
1515 | | // --------------------------------------------------------------------------- |
1516 | | |
1517 | | /** \brief Return the list of grammar errors found during parsing. |
1518 | | * |
1519 | | * Grammar errors are non-compliance issues with respect to the WKT grammar. |
1520 | | * |
1521 | | * \note The list might be non-empty only is setStrict(false) has been called. |
1522 | | * |
1523 | | * @since PROJ 9.5 |
1524 | | */ |
1525 | 0 | std::list<std::string> WKTParser::grammarErrorList() const { |
1526 | 0 | return d->grammarErrorList_; |
1527 | 0 | } |
1528 | | |
1529 | | // --------------------------------------------------------------------------- |
1530 | | |
1531 | | //! @cond Doxygen_Suppress |
1532 | 2.22k | void WKTParser::Private::emitRecoverableWarning(const std::string &errorMsg) { |
1533 | 2.22k | if (strict_) { |
1534 | 0 | throw ParsingException(errorMsg); |
1535 | 2.22k | } else { |
1536 | 2.22k | warningList_.push_back(errorMsg); |
1537 | 2.22k | } |
1538 | 2.22k | } |
1539 | | //! @endcond |
1540 | | |
1541 | | // --------------------------------------------------------------------------- |
1542 | | |
1543 | | //! @cond Doxygen_Suppress |
1544 | 875 | void WKTParser::Private::emitGrammarError(const std::string &errorMsg) { |
1545 | 875 | if (strict_) { |
1546 | 0 | throw ParsingException(errorMsg); |
1547 | 875 | } else { |
1548 | 875 | grammarErrorList_.push_back(errorMsg); |
1549 | 875 | } |
1550 | 875 | } |
1551 | | //! @endcond |
1552 | | |
1553 | | // --------------------------------------------------------------------------- |
1554 | | |
1555 | 9.28k | static double asDouble(const std::string &val) { return c_locale_stod(val); } |
1556 | | |
1557 | | // --------------------------------------------------------------------------- |
1558 | | |
1559 | 128 | PROJ_NO_RETURN static void ThrowNotEnoughChildren(const std::string &nodeName) { |
1560 | 128 | throw ParsingException( |
1561 | 128 | concat("not enough children in ", nodeName, " node")); |
1562 | 128 | } |
1563 | | |
1564 | | // --------------------------------------------------------------------------- |
1565 | | |
1566 | | PROJ_NO_RETURN static void |
1567 | 32 | ThrowNotRequiredNumberOfChildren(const std::string &nodeName) { |
1568 | 32 | throw ParsingException( |
1569 | 32 | concat("not required number of children in ", nodeName, " node")); |
1570 | 32 | } |
1571 | | |
1572 | | // --------------------------------------------------------------------------- |
1573 | | |
1574 | 298 | PROJ_NO_RETURN static void ThrowMissing(const std::string &nodeName) { |
1575 | 298 | throw ParsingException(concat("missing ", nodeName, " node")); |
1576 | 298 | } |
1577 | | |
1578 | | // --------------------------------------------------------------------------- |
1579 | | |
1580 | | PROJ_NO_RETURN static void |
1581 | 0 | ThrowNotExpectedCSType(const std::string &expectedCSType) { |
1582 | 0 | throw ParsingException(concat("CS node is not of type ", expectedCSType)); |
1583 | 0 | } |
1584 | | |
1585 | | // --------------------------------------------------------------------------- |
1586 | | |
1587 | | static ParsingException buildRethrow(const char *funcName, |
1588 | 52 | const std::exception &e) { |
1589 | 52 | std::string res(funcName); |
1590 | 52 | res += ": "; |
1591 | 52 | res += e.what(); |
1592 | 52 | return ParsingException(res); |
1593 | 52 | } |
1594 | | |
1595 | | // --------------------------------------------------------------------------- |
1596 | | |
1597 | | //! @cond Doxygen_Suppress |
1598 | 23.6k | std::string WKTParser::Private::stripQuotes(const WKTNodeNNPtr &node) { |
1599 | 23.6k | return ::stripQuotes(node->GP()->value()); |
1600 | 23.6k | } |
1601 | | |
1602 | | // --------------------------------------------------------------------------- |
1603 | | |
1604 | 7.07k | double WKTParser::Private::asDouble(const WKTNodeNNPtr &node) { |
1605 | 7.07k | return io::asDouble(node->GP()->value()); |
1606 | 7.07k | } |
1607 | | |
1608 | | // --------------------------------------------------------------------------- |
1609 | | |
1610 | | IdentifierPtr WKTParser::Private::buildId(const WKTNodeNNPtr &parentNode, |
1611 | | const WKTNodeNNPtr &node, |
1612 | 1.14k | bool tolerant, bool removeInverseOf) { |
1613 | 1.14k | const auto *nodeP = node->GP(); |
1614 | 1.14k | const auto &nodeChildren = nodeP->children(); |
1615 | 1.14k | if (nodeChildren.size() >= 2) { |
1616 | 999 | auto codeSpace = stripQuotes(nodeChildren[0]); |
1617 | 999 | if (removeInverseOf && starts_with(codeSpace, "INVERSE(") && |
1618 | 999 | codeSpace.back() == ')') { |
1619 | 30 | codeSpace = codeSpace.substr(strlen("INVERSE(")); |
1620 | 30 | codeSpace.resize(codeSpace.size() - 1); |
1621 | 30 | } |
1622 | | |
1623 | 999 | PropertyMap propertiesId; |
1624 | 999 | if (nodeChildren.size() >= 3 && |
1625 | 999 | nodeChildren[2]->GP()->childrenSize() == 0) { |
1626 | 354 | std::string version = stripQuotes(nodeChildren[2]); |
1627 | | |
1628 | | // IAU + 2015 -> IAU_2015 |
1629 | 354 | if (dbContext_) { |
1630 | 296 | std::string codeSpaceOut; |
1631 | 296 | if (dbContext_->getVersionedAuthority(codeSpace, version, |
1632 | 296 | codeSpaceOut)) { |
1633 | 0 | codeSpace = std::move(codeSpaceOut); |
1634 | 0 | version.clear(); |
1635 | 0 | } |
1636 | 296 | } |
1637 | | |
1638 | 354 | if (!version.empty()) { |
1639 | 350 | propertiesId.set(Identifier::VERSION_KEY, version); |
1640 | 350 | } |
1641 | 354 | } |
1642 | | |
1643 | 999 | auto code = stripQuotes(nodeChildren[1]); |
1644 | | |
1645 | | // Prior to PROJ 9.5, when synthetizing an ID for a CONVERSION UTM Zone |
1646 | | // south, we generated a wrong value. Auto-fix that |
1647 | 999 | const auto &parentNodeKeyword(parentNode->GP()->value()); |
1648 | 999 | if (parentNodeKeyword == WKTConstants::CONVERSION && |
1649 | 999 | codeSpace == Identifier::EPSG) { |
1650 | 35 | const auto &parentNodeChildren = parentNode->GP()->children(); |
1651 | 35 | if (!parentNodeChildren.empty()) { |
1652 | 35 | const auto parentNodeName(stripQuotes(parentNodeChildren[0])); |
1653 | 35 | if (ci_starts_with(parentNodeName, "UTM Zone ") && |
1654 | 35 | parentNodeName.find('S') != std::string::npos) { |
1655 | 0 | const int nZone = |
1656 | 0 | atoi(parentNodeName.c_str() + strlen("UTM Zone ")); |
1657 | 0 | if (nZone >= 1 && nZone <= 60) { |
1658 | 0 | code = internal::toString(16100 + nZone); |
1659 | 0 | } |
1660 | 0 | } |
1661 | 35 | } |
1662 | 35 | } |
1663 | | |
1664 | 999 | auto &citationNode = nodeP->lookForChild(WKTConstants::CITATION); |
1665 | 999 | auto &uriNode = nodeP->lookForChild(WKTConstants::URI); |
1666 | | |
1667 | 999 | propertiesId.set(Identifier::CODESPACE_KEY, codeSpace); |
1668 | 999 | bool authoritySet = false; |
1669 | 999 | /*if (!isNull(citationNode))*/ { |
1670 | 999 | const auto *citationNodeP = citationNode->GP(); |
1671 | 999 | if (citationNodeP->childrenSize() == 1) { |
1672 | 23 | authoritySet = true; |
1673 | 23 | propertiesId.set(Identifier::AUTHORITY_KEY, |
1674 | 23 | stripQuotes(citationNodeP->children()[0])); |
1675 | 23 | } |
1676 | 999 | } |
1677 | 999 | if (!authoritySet) { |
1678 | 976 | propertiesId.set(Identifier::AUTHORITY_KEY, codeSpace); |
1679 | 976 | } |
1680 | 999 | /*if (!isNull(uriNode))*/ { |
1681 | 999 | const auto *uriNodeP = uriNode->GP(); |
1682 | 999 | if (uriNodeP->childrenSize() == 1) { |
1683 | 12 | propertiesId.set(Identifier::URI_KEY, |
1684 | 12 | stripQuotes(uriNodeP->children()[0])); |
1685 | 12 | } |
1686 | 999 | } |
1687 | 999 | return Identifier::create(code, propertiesId); |
1688 | 999 | } else if (strict_ || !tolerant) { |
1689 | 6 | ThrowNotEnoughChildren(nodeP->value()); |
1690 | 141 | } else { |
1691 | 141 | std::string msg("not enough children in "); |
1692 | 141 | msg += nodeP->value(); |
1693 | 141 | msg += " node"; |
1694 | 141 | warningList_.emplace_back(std::move(msg)); |
1695 | 141 | } |
1696 | 141 | return nullptr; |
1697 | 1.14k | } |
1698 | | |
1699 | | // --------------------------------------------------------------------------- |
1700 | | |
1701 | | PropertyMap &WKTParser::Private::buildProperties(const WKTNodeNNPtr &node, |
1702 | | bool removeInverseOf, |
1703 | 10.3k | bool hasName) { |
1704 | | |
1705 | 10.3k | if (properties_.size() >= MAX_PROPERTY_SIZE) { |
1706 | 0 | throw ParsingException("MAX_PROPERTY_SIZE reached"); |
1707 | 0 | } |
1708 | 10.3k | properties_.push_back(std::make_unique<PropertyMap>()); |
1709 | 10.3k | auto properties = properties_.back().get(); |
1710 | | |
1711 | 10.3k | std::string authNameFromAlias; |
1712 | 10.3k | std::string codeFromAlias; |
1713 | 10.3k | const auto *nodeP = node->GP(); |
1714 | 10.3k | const auto &nodeChildren = nodeP->children(); |
1715 | | |
1716 | 10.3k | auto identifiers = ArrayOfBaseObject::create(); |
1717 | 34.2k | for (const auto &subNode : nodeChildren) { |
1718 | 34.2k | const auto &subNodeName(subNode->GP()->value()); |
1719 | 34.2k | if (ci_equal(subNodeName, WKTConstants::ID) || |
1720 | 34.2k | ci_equal(subNodeName, WKTConstants::AUTHORITY)) { |
1721 | 1.11k | auto id = buildId(node, subNode, true, removeInverseOf); |
1722 | 1.11k | if (id) { |
1723 | 973 | identifiers->add(NN_NO_CHECK(id)); |
1724 | 973 | } |
1725 | 1.11k | } |
1726 | 34.2k | } |
1727 | | |
1728 | 10.3k | if (hasName && !nodeChildren.empty()) { |
1729 | 9.57k | const auto &nodeName(nodeP->value()); |
1730 | 9.57k | auto name(stripQuotes(nodeChildren[0])); |
1731 | 9.57k | if (removeInverseOf && starts_with(name, "Inverse of ")) { |
1732 | 70 | name = name.substr(strlen("Inverse of ")); |
1733 | 70 | } |
1734 | | |
1735 | 9.57k | if (ends_with(name, " (deprecated)")) { |
1736 | 1 | name.resize(name.size() - strlen(" (deprecated)")); |
1737 | 1 | properties->set(common::IdentifiedObject::DEPRECATED_KEY, true); |
1738 | 1 | } |
1739 | | |
1740 | | // Oracle WKT can contain names like |
1741 | | // "Reseau Geodesique Francais 1993 (EPSG ID 6171)" |
1742 | | // for WKT attributes to the auth_name = "IGN - Paris" |
1743 | | // Strip that suffix from the name and assign a true EPSG code to the |
1744 | | // object |
1745 | 9.57k | if (identifiers->empty()) { |
1746 | 9.29k | const auto pos = name.find(" (EPSG ID "); |
1747 | 9.29k | if (pos != std::string::npos && name.back() == ')') { |
1748 | 142 | const auto code = |
1749 | 142 | name.substr(pos + strlen(" (EPSG ID "), |
1750 | 142 | name.size() - 1 - pos - strlen(" (EPSG ID ")); |
1751 | 142 | name.resize(pos); |
1752 | | |
1753 | 142 | PropertyMap propertiesId; |
1754 | 142 | propertiesId.set(Identifier::CODESPACE_KEY, Identifier::EPSG); |
1755 | 142 | propertiesId.set(Identifier::AUTHORITY_KEY, Identifier::EPSG); |
1756 | 142 | identifiers->add(Identifier::create(code, propertiesId)); |
1757 | 142 | } |
1758 | 9.29k | } |
1759 | | |
1760 | 9.57k | const char *tableNameForAlias = nullptr; |
1761 | 9.57k | if (ci_equal(nodeName, WKTConstants::GEOGCS)) { |
1762 | 1.04k | if (starts_with(name, "GCS_")) { |
1763 | 278 | esriStyle_ = true; |
1764 | 278 | if (name == "GCS_WGS_1984") { |
1765 | 142 | name = "WGS 84"; |
1766 | 142 | } else if (name == "GCS_unknown") { |
1767 | 0 | name = "unknown"; |
1768 | 136 | } else { |
1769 | 136 | tableNameForAlias = "geodetic_crs"; |
1770 | 136 | } |
1771 | 278 | } |
1772 | 8.53k | } else if (esriStyle_ && ci_equal(nodeName, WKTConstants::SPHEROID)) { |
1773 | 308 | if (name == "WGS_1984") { |
1774 | 142 | name = "WGS 84"; |
1775 | 142 | authNameFromAlias = Identifier::EPSG; |
1776 | 142 | codeFromAlias = "7030"; |
1777 | 166 | } else { |
1778 | 166 | tableNameForAlias = "ellipsoid"; |
1779 | 166 | } |
1780 | 308 | } |
1781 | | |
1782 | 9.57k | if (dbContext_ && tableNameForAlias) { |
1783 | 287 | std::string outTableName; |
1784 | 287 | auto authFactory = AuthorityFactory::create(NN_NO_CHECK(dbContext_), |
1785 | 287 | std::string()); |
1786 | 287 | auto officialName = authFactory->getOfficialNameFromAlias( |
1787 | 287 | name, tableNameForAlias, "ESRI", false, outTableName, |
1788 | 287 | authNameFromAlias, codeFromAlias); |
1789 | 287 | if (!officialName.empty()) { |
1790 | 119 | name = std::move(officialName); |
1791 | | |
1792 | | // Clearing authority for geodetic_crs because of |
1793 | | // potential axis order mismatch. |
1794 | 119 | if (strcmp(tableNameForAlias, "geodetic_crs") == 0) { |
1795 | 93 | authNameFromAlias.clear(); |
1796 | 93 | codeFromAlias.clear(); |
1797 | 93 | } |
1798 | 119 | } |
1799 | 287 | } |
1800 | | |
1801 | 9.57k | properties->set(IdentifiedObject::NAME_KEY, name); |
1802 | 9.57k | } |
1803 | | |
1804 | 10.3k | if (identifiers->empty() && !authNameFromAlias.empty()) { |
1805 | 168 | identifiers->add(Identifier::create( |
1806 | 168 | codeFromAlias, |
1807 | 168 | PropertyMap() |
1808 | 168 | .set(Identifier::CODESPACE_KEY, authNameFromAlias) |
1809 | 168 | .set(Identifier::AUTHORITY_KEY, authNameFromAlias))); |
1810 | 168 | } |
1811 | 10.3k | if (!identifiers->empty()) { |
1812 | 598 | properties->set(IdentifiedObject::IDENTIFIERS_KEY, identifiers); |
1813 | 598 | } |
1814 | | |
1815 | 10.3k | auto &remarkNode = nodeP->lookForChild(WKTConstants::REMARK); |
1816 | 10.3k | if (!isNull(remarkNode)) { |
1817 | 46 | const auto &remarkChildren = remarkNode->GP()->children(); |
1818 | 46 | if (remarkChildren.size() == 1) { |
1819 | 40 | properties->set(IdentifiedObject::REMARKS_KEY, |
1820 | 40 | stripQuotes(remarkChildren[0])); |
1821 | 40 | } else { |
1822 | 6 | ThrowNotRequiredNumberOfChildren(remarkNode->GP()->value()); |
1823 | 6 | } |
1824 | 46 | } |
1825 | | |
1826 | 10.3k | ArrayOfBaseObjectNNPtr array = ArrayOfBaseObject::create(); |
1827 | 34.2k | for (const auto &subNode : nodeP->children()) { |
1828 | 34.2k | const auto &subNodeName(subNode->GP()->value()); |
1829 | 34.2k | if (ci_equal(subNodeName, WKTConstants::USAGE)) { |
1830 | 63 | auto objectDomain = buildObjectDomain(subNode); |
1831 | 63 | if (!objectDomain) { |
1832 | 3 | throw ParsingException( |
1833 | 3 | concat("missing children in ", subNodeName, " node")); |
1834 | 3 | } |
1835 | 60 | array->add(NN_NO_CHECK(objectDomain)); |
1836 | 60 | } |
1837 | 34.2k | } |
1838 | 10.3k | if (!array->empty()) { |
1839 | 23 | properties->set(ObjectUsage::OBJECT_DOMAIN_KEY, array); |
1840 | 10.3k | } else { |
1841 | 10.3k | auto objectDomain = buildObjectDomain(node); |
1842 | 10.3k | if (objectDomain) { |
1843 | 219 | properties->set(ObjectUsage::OBJECT_DOMAIN_KEY, |
1844 | 219 | NN_NO_CHECK(objectDomain)); |
1845 | 219 | } |
1846 | 10.3k | } |
1847 | | |
1848 | 10.3k | auto &versionNode = nodeP->lookForChild(WKTConstants::VERSION); |
1849 | 10.3k | if (!isNull(versionNode)) { |
1850 | 18 | const auto &versionChildren = versionNode->GP()->children(); |
1851 | 18 | if (versionChildren.size() == 1) { |
1852 | 16 | properties->set(CoordinateOperation::OPERATION_VERSION_KEY, |
1853 | 16 | stripQuotes(versionChildren[0])); |
1854 | 16 | } else { |
1855 | 2 | ThrowNotRequiredNumberOfChildren(versionNode->GP()->value()); |
1856 | 2 | } |
1857 | 18 | } |
1858 | | |
1859 | 10.3k | return *properties; |
1860 | 10.3k | } |
1861 | | |
1862 | | // --------------------------------------------------------------------------- |
1863 | | |
1864 | | ObjectDomainPtr |
1865 | 10.3k | WKTParser::Private::buildObjectDomain(const WKTNodeNNPtr &node) { |
1866 | | |
1867 | 10.3k | const auto *nodeP = node->GP(); |
1868 | 10.3k | auto &scopeNode = nodeP->lookForChild(WKTConstants::SCOPE); |
1869 | 10.3k | auto &areaNode = nodeP->lookForChild(WKTConstants::AREA); |
1870 | 10.3k | auto &bboxNode = nodeP->lookForChild(WKTConstants::BBOX); |
1871 | 10.3k | auto &verticalExtentNode = |
1872 | 10.3k | nodeP->lookForChild(WKTConstants::VERTICALEXTENT); |
1873 | 10.3k | auto &temporalExtentNode = nodeP->lookForChild(WKTConstants::TIMEEXTENT); |
1874 | 10.3k | if (!isNull(scopeNode) || !isNull(areaNode) || !isNull(bboxNode) || |
1875 | 10.3k | !isNull(verticalExtentNode) || !isNull(temporalExtentNode)) { |
1876 | 314 | optional<std::string> scope; |
1877 | 314 | const auto *scopeNodeP = scopeNode->GP(); |
1878 | 314 | const auto &scopeChildren = scopeNodeP->children(); |
1879 | 314 | if (scopeChildren.size() == 1) { |
1880 | 1 | scope = stripQuotes(scopeChildren[0]); |
1881 | 1 | } |
1882 | 314 | ExtentPtr extent; |
1883 | 314 | if (!isNull(areaNode) || !isNull(bboxNode)) { |
1884 | 282 | util::optional<std::string> description; |
1885 | 282 | std::vector<GeographicExtentNNPtr> geogExtent; |
1886 | 282 | std::vector<VerticalExtentNNPtr> verticalExtent; |
1887 | 282 | std::vector<TemporalExtentNNPtr> temporalExtent; |
1888 | 282 | if (!isNull(areaNode)) { |
1889 | 61 | const auto &areaChildren = areaNode->GP()->children(); |
1890 | 61 | if (areaChildren.size() == 1) { |
1891 | 57 | description = stripQuotes(areaChildren[0]); |
1892 | 57 | } else { |
1893 | 4 | ThrowNotRequiredNumberOfChildren(areaNode->GP()->value()); |
1894 | 4 | } |
1895 | 61 | } |
1896 | 278 | if (!isNull(bboxNode)) { |
1897 | 221 | const auto &bboxChildren = bboxNode->GP()->children(); |
1898 | 221 | if (bboxChildren.size() == 4) { |
1899 | 208 | double south, west, north, east; |
1900 | 208 | try { |
1901 | 208 | south = asDouble(bboxChildren[0]); |
1902 | 208 | west = asDouble(bboxChildren[1]); |
1903 | 208 | north = asDouble(bboxChildren[2]); |
1904 | 208 | east = asDouble(bboxChildren[3]); |
1905 | 208 | } catch (const std::exception &) { |
1906 | 5 | throw ParsingException(concat("not 4 double values in ", |
1907 | 5 | bboxNode->GP()->value(), |
1908 | 5 | " node")); |
1909 | 5 | } |
1910 | 203 | try { |
1911 | 203 | auto bbox = GeographicBoundingBox::create(west, south, |
1912 | 203 | east, north); |
1913 | 203 | geogExtent.emplace_back(bbox); |
1914 | 203 | } catch (const std::exception &e) { |
1915 | 6 | throw ParsingException(concat("Invalid ", |
1916 | 6 | bboxNode->GP()->value(), |
1917 | 6 | " node: ") + |
1918 | 6 | e.what()); |
1919 | 6 | } |
1920 | 203 | } else { |
1921 | 13 | ThrowNotRequiredNumberOfChildren(bboxNode->GP()->value()); |
1922 | 13 | } |
1923 | 221 | } |
1924 | | |
1925 | 254 | if (!isNull(verticalExtentNode)) { |
1926 | 29 | const auto &verticalExtentChildren = |
1927 | 29 | verticalExtentNode->GP()->children(); |
1928 | 29 | const auto verticalExtentChildrenSize = |
1929 | 29 | verticalExtentChildren.size(); |
1930 | 29 | if (verticalExtentChildrenSize == 2 || |
1931 | 29 | verticalExtentChildrenSize == 3) { |
1932 | 26 | double min; |
1933 | 26 | double max; |
1934 | 26 | try { |
1935 | 26 | min = asDouble(verticalExtentChildren[0]); |
1936 | 26 | max = asDouble(verticalExtentChildren[1]); |
1937 | 26 | } catch (const std::exception &) { |
1938 | 2 | throw ParsingException( |
1939 | 2 | concat("not 2 double values in ", |
1940 | 2 | verticalExtentNode->GP()->value(), " node")); |
1941 | 2 | } |
1942 | 24 | UnitOfMeasure unit = UnitOfMeasure::METRE; |
1943 | 24 | if (verticalExtentChildrenSize == 3) { |
1944 | 2 | unit = buildUnit(verticalExtentChildren[2], |
1945 | 2 | UnitOfMeasure::Type::LINEAR); |
1946 | 2 | } |
1947 | 24 | verticalExtent.emplace_back(VerticalExtent::create( |
1948 | 24 | min, max, util::nn_make_shared<UnitOfMeasure>(unit))); |
1949 | 24 | } else { |
1950 | 3 | ThrowNotRequiredNumberOfChildren( |
1951 | 3 | verticalExtentNode->GP()->value()); |
1952 | 3 | } |
1953 | 29 | } |
1954 | | |
1955 | 249 | if (!isNull(temporalExtentNode)) { |
1956 | 50 | const auto &temporalExtentChildren = |
1957 | 50 | temporalExtentNode->GP()->children(); |
1958 | 50 | if (temporalExtentChildren.size() == 2) { |
1959 | 46 | temporalExtent.emplace_back(TemporalExtent::create( |
1960 | 46 | stripQuotes(temporalExtentChildren[0]), |
1961 | 46 | stripQuotes(temporalExtentChildren[1]))); |
1962 | 46 | } else { |
1963 | 4 | ThrowNotRequiredNumberOfChildren( |
1964 | 4 | temporalExtentNode->GP()->value()); |
1965 | 4 | } |
1966 | 50 | } |
1967 | 245 | extent = Extent::create(description, geogExtent, verticalExtent, |
1968 | 245 | temporalExtent) |
1969 | 245 | .as_nullable(); |
1970 | 245 | } |
1971 | 277 | return ObjectDomain::create(scope, extent).as_nullable(); |
1972 | 314 | } |
1973 | | |
1974 | 10.0k | return nullptr; |
1975 | 10.3k | } |
1976 | | |
1977 | | // --------------------------------------------------------------------------- |
1978 | | |
1979 | | UnitOfMeasure WKTParser::Private::buildUnit(const WKTNodeNNPtr &node, |
1980 | 1.82k | UnitOfMeasure::Type type) { |
1981 | 1.82k | const auto *nodeP = node->GP(); |
1982 | 1.82k | const auto &children = nodeP->children(); |
1983 | 1.82k | if ((type != UnitOfMeasure::Type::TIME && children.size() < 2) || |
1984 | 1.82k | (type == UnitOfMeasure::Type::TIME && children.size() < 1)) { |
1985 | 6 | ThrowNotEnoughChildren(nodeP->value()); |
1986 | 6 | } |
1987 | 1.82k | try { |
1988 | 1.82k | std::string unitName(stripQuotes(children[0])); |
1989 | 1.82k | PropertyMap properties(buildProperties(node)); |
1990 | 1.82k | auto &idNode = |
1991 | 1.82k | nodeP->lookForChild(WKTConstants::ID, WKTConstants::AUTHORITY); |
1992 | 1.82k | if (!isNull(idNode) && idNode->GP()->childrenSize() < 2) { |
1993 | 24 | emitRecoverableWarning("not enough children in " + |
1994 | 24 | idNode->GP()->value() + " node"); |
1995 | 24 | } |
1996 | 1.82k | const bool hasValidIdNode = |
1997 | 1.82k | !isNull(idNode) && idNode->GP()->childrenSize() >= 2; |
1998 | | |
1999 | 1.82k | const auto &idNodeChildren(idNode->GP()->children()); |
2000 | 1.82k | std::string codeSpace(hasValidIdNode ? stripQuotes(idNodeChildren[0]) |
2001 | 1.82k | : std::string()); |
2002 | 1.82k | std::string code(hasValidIdNode ? stripQuotes(idNodeChildren[1]) |
2003 | 1.82k | : std::string()); |
2004 | | |
2005 | 1.82k | bool queryDb = true; |
2006 | 1.82k | if (type == UnitOfMeasure::Type::UNKNOWN) { |
2007 | 29 | if (ci_equal(unitName, "METER") || ci_equal(unitName, "METRE")) { |
2008 | 0 | type = UnitOfMeasure::Type::LINEAR; |
2009 | 0 | unitName = "metre"; |
2010 | 0 | if (codeSpace.empty()) { |
2011 | 0 | codeSpace = Identifier::EPSG; |
2012 | 0 | code = "9001"; |
2013 | 0 | queryDb = false; |
2014 | 0 | } |
2015 | 29 | } else if (ci_equal(unitName, "DEGREE") || |
2016 | 29 | ci_equal(unitName, "GRAD")) { |
2017 | 0 | type = UnitOfMeasure::Type::ANGULAR; |
2018 | 0 | } |
2019 | 29 | } |
2020 | | |
2021 | 1.82k | if (esriStyle_ && dbContext_ && queryDb) { |
2022 | 1.08k | std::string outTableName; |
2023 | 1.08k | std::string authNameFromAlias; |
2024 | 1.08k | std::string codeFromAlias; |
2025 | 1.08k | auto authFactory = AuthorityFactory::create(NN_NO_CHECK(dbContext_), |
2026 | 1.08k | std::string()); |
2027 | 1.08k | auto officialName = authFactory->getOfficialNameFromAlias( |
2028 | 1.08k | unitName, "unit_of_measure", "ESRI", false, outTableName, |
2029 | 1.08k | authNameFromAlias, codeFromAlias); |
2030 | 1.08k | if (!officialName.empty()) { |
2031 | 532 | unitName = std::move(officialName); |
2032 | 532 | codeSpace = std::move(authNameFromAlias); |
2033 | 532 | code = std::move(codeFromAlias); |
2034 | 532 | } |
2035 | 1.08k | } |
2036 | | |
2037 | 1.82k | double convFactor = children.size() >= 2 ? asDouble(children[1]) : 0.0; |
2038 | 1.82k | constexpr double US_FOOT_CONV_FACTOR = 12.0 / 39.37; |
2039 | 1.82k | constexpr double REL_ERROR = 1e-10; |
2040 | | // Fix common rounding errors |
2041 | 1.82k | if (std::fabs(convFactor - UnitOfMeasure::DEGREE.conversionToSI()) < |
2042 | 1.82k | REL_ERROR * convFactor) { |
2043 | 568 | convFactor = UnitOfMeasure::DEGREE.conversionToSI(); |
2044 | 1.25k | } else if (std::fabs(convFactor - US_FOOT_CONV_FACTOR) < |
2045 | 1.25k | REL_ERROR * convFactor) { |
2046 | 2 | convFactor = US_FOOT_CONV_FACTOR; |
2047 | 2 | } |
2048 | | |
2049 | 1.82k | return UnitOfMeasure(unitName, convFactor, type, codeSpace, code); |
2050 | 1.82k | } catch (const std::exception &e) { |
2051 | 8 | throw buildRethrow(__FUNCTION__, e); |
2052 | 8 | } |
2053 | 1.82k | } |
2054 | | |
2055 | | // --------------------------------------------------------------------------- |
2056 | | |
2057 | | // node here is a parent node, not a UNIT/LENGTHUNIT/ANGLEUNIT/TIMEUNIT/... node |
2058 | | UnitOfMeasure WKTParser::Private::buildUnitInSubNode(const WKTNodeNNPtr &node, |
2059 | 6.52k | UnitOfMeasure::Type type) { |
2060 | 6.52k | const auto *nodeP = node->GP(); |
2061 | 6.52k | { |
2062 | 6.52k | auto &unitNode = nodeP->lookForChild(WKTConstants::LENGTHUNIT); |
2063 | 6.52k | if (!isNull(unitNode)) { |
2064 | 2 | return buildUnit(unitNode, UnitOfMeasure::Type::LINEAR); |
2065 | 2 | } |
2066 | 6.52k | } |
2067 | | |
2068 | 6.52k | { |
2069 | 6.52k | auto &unitNode = nodeP->lookForChild(WKTConstants::ANGLEUNIT); |
2070 | 6.52k | if (!isNull(unitNode)) { |
2071 | 0 | return buildUnit(unitNode, UnitOfMeasure::Type::ANGULAR); |
2072 | 0 | } |
2073 | 6.52k | } |
2074 | | |
2075 | 6.52k | { |
2076 | 6.52k | auto &unitNode = nodeP->lookForChild(WKTConstants::SCALEUNIT); |
2077 | 6.52k | if (!isNull(unitNode)) { |
2078 | 0 | return buildUnit(unitNode, UnitOfMeasure::Type::SCALE); |
2079 | 0 | } |
2080 | 6.52k | } |
2081 | | |
2082 | 6.52k | { |
2083 | 6.52k | auto &unitNode = nodeP->lookForChild(WKTConstants::TIMEUNIT); |
2084 | 6.52k | if (!isNull(unitNode)) { |
2085 | 3 | return buildUnit(unitNode, UnitOfMeasure::Type::TIME); |
2086 | 3 | } |
2087 | 6.52k | } |
2088 | 6.52k | { |
2089 | 6.52k | auto &unitNode = nodeP->lookForChild(WKTConstants::TEMPORALQUANTITY); |
2090 | 6.52k | if (!isNull(unitNode)) { |
2091 | 8 | return buildUnit(unitNode, UnitOfMeasure::Type::TIME); |
2092 | 8 | } |
2093 | 6.52k | } |
2094 | | |
2095 | 6.51k | { |
2096 | 6.51k | auto &unitNode = nodeP->lookForChild(WKTConstants::PARAMETRICUNIT); |
2097 | 6.51k | if (!isNull(unitNode)) { |
2098 | 2 | return buildUnit(unitNode, UnitOfMeasure::Type::PARAMETRIC); |
2099 | 2 | } |
2100 | 6.51k | } |
2101 | | |
2102 | 6.51k | { |
2103 | 6.51k | auto &unitNode = nodeP->lookForChild(WKTConstants::UNIT); |
2104 | 6.51k | if (!isNull(unitNode)) { |
2105 | 1.80k | return buildUnit(unitNode, type); |
2106 | 1.80k | } |
2107 | 6.51k | } |
2108 | | |
2109 | 4.70k | return UnitOfMeasure::NONE; |
2110 | 6.51k | } |
2111 | | |
2112 | | // --------------------------------------------------------------------------- |
2113 | | |
2114 | 1.55k | EllipsoidNNPtr WKTParser::Private::buildEllipsoid(const WKTNodeNNPtr &node) { |
2115 | 1.55k | const auto *nodeP = node->GP(); |
2116 | 1.55k | const auto &children = nodeP->children(); |
2117 | 1.55k | if (children.size() < 3) { |
2118 | 8 | ThrowNotEnoughChildren(nodeP->value()); |
2119 | 8 | } |
2120 | 1.54k | try { |
2121 | 1.54k | UnitOfMeasure unit = |
2122 | 1.54k | buildUnitInSubNode(node, UnitOfMeasure::Type::LINEAR); |
2123 | 1.54k | if (unit == UnitOfMeasure::NONE) { |
2124 | 1.29k | unit = UnitOfMeasure::METRE; |
2125 | 1.29k | } |
2126 | 1.54k | Length semiMajorAxis(asDouble(children[1]), unit); |
2127 | | // Some WKT in the wild use "inf". Cf SPHEROID["unnamed",6370997,"inf"] |
2128 | | // in https://zenodo.org/record/3878979#.Y_P4g4CZNH4, |
2129 | | // https://zenodo.org/record/5831940#.Y_P4i4CZNH5 |
2130 | | // or https://grasswiki.osgeo.org/wiki/Marine_Science |
2131 | 1.54k | const auto &invFlatteningChild = children[2]; |
2132 | 1.54k | if (invFlatteningChild->GP()->value() == "\"inf\"") { |
2133 | 2 | emitRecoverableWarning("Inverse flattening = \"inf\" is not " |
2134 | 2 | "conformant, but understood"); |
2135 | 2 | } |
2136 | 1.54k | Scale invFlattening(invFlatteningChild->GP()->value() == "\"inf\"" |
2137 | 1.54k | ? 0 |
2138 | 1.54k | : asDouble(invFlatteningChild)); |
2139 | 1.54k | const auto ellpsProperties = buildProperties(node); |
2140 | 1.54k | std::string ellpsName; |
2141 | 1.54k | ellpsProperties.getStringValue(IdentifiedObject::NAME_KEY, ellpsName); |
2142 | 1.54k | const auto celestialBody(Ellipsoid::guessBodyName( |
2143 | 1.54k | dbContext_, semiMajorAxis.getSIValue(), ellpsName)); |
2144 | 1.54k | if (invFlattening.getSIValue() == 0) { |
2145 | 766 | return Ellipsoid::createSphere(ellpsProperties, semiMajorAxis, |
2146 | 766 | celestialBody); |
2147 | 780 | } else { |
2148 | 780 | return Ellipsoid::createFlattenedSphere( |
2149 | 780 | ellpsProperties, semiMajorAxis, invFlattening, celestialBody); |
2150 | 780 | } |
2151 | 1.54k | } catch (const std::exception &e) { |
2152 | 34 | throw buildRethrow(__FUNCTION__, e); |
2153 | 34 | } |
2154 | 1.54k | } |
2155 | | |
2156 | | // --------------------------------------------------------------------------- |
2157 | | |
2158 | | PrimeMeridianNNPtr WKTParser::Private::buildPrimeMeridian( |
2159 | 311 | const WKTNodeNNPtr &node, const UnitOfMeasure &defaultAngularUnit) { |
2160 | 311 | const auto *nodeP = node->GP(); |
2161 | 311 | const auto &children = nodeP->children(); |
2162 | 311 | if (children.size() < 2) { |
2163 | 1 | ThrowNotEnoughChildren(nodeP->value()); |
2164 | 1 | } |
2165 | 310 | auto name = stripQuotes(children[0]); |
2166 | 310 | UnitOfMeasure unit = buildUnitInSubNode(node, UnitOfMeasure::Type::ANGULAR); |
2167 | 310 | if (unit == UnitOfMeasure::NONE) { |
2168 | 310 | unit = defaultAngularUnit; |
2169 | 310 | if (unit == UnitOfMeasure::NONE) { |
2170 | 1 | unit = UnitOfMeasure::DEGREE; |
2171 | 1 | } |
2172 | 310 | } |
2173 | 310 | try { |
2174 | 310 | double angleValue = asDouble(children[1]); |
2175 | | |
2176 | | // Correct for GDAL WKT1 and WKT1-ESRI departure |
2177 | 310 | if (name == "Paris" && std::fabs(angleValue - 2.33722917) < 1e-8 && |
2178 | 310 | unit._isEquivalentTo(UnitOfMeasure::GRAD, |
2179 | 0 | util::IComparable::Criterion::EQUIVALENT)) { |
2180 | 0 | angleValue = 2.5969213; |
2181 | 310 | } else { |
2182 | 310 | static const struct { |
2183 | 310 | const char *name; |
2184 | 310 | int deg; |
2185 | 310 | int min; |
2186 | 310 | double sec; |
2187 | 310 | } primeMeridiansDMS[] = { |
2188 | 310 | {"Lisbon", -9, 7, 54.862}, {"Bogota", -74, 4, 51.3}, |
2189 | 310 | {"Madrid", -3, 41, 14.55}, {"Rome", 12, 27, 8.4}, |
2190 | 310 | {"Bern", 7, 26, 22.5}, {"Jakarta", 106, 48, 27.79}, |
2191 | 310 | {"Ferro", -17, 40, 0}, {"Brussels", 4, 22, 4.71}, |
2192 | 310 | {"Stockholm", 18, 3, 29.8}, {"Athens", 23, 42, 58.815}, |
2193 | 310 | {"Oslo", 10, 43, 22.5}, {"Paris RGS", 2, 20, 13.95}, |
2194 | 310 | {"Paris_RGS", 2, 20, 13.95}}; |
2195 | | |
2196 | | // Current epsg.org output may use the EPSG:9110 "sexagesimal DMS" |
2197 | | // unit and a DD.MMSSsss value, but this will likely be changed to |
2198 | | // use decimal degree. |
2199 | | // Or WKT1 may for example use the Paris RGS decimal degree value |
2200 | | // but with a GEOGCS with UNIT["Grad"] |
2201 | 3.95k | for (const auto &pmDef : primeMeridiansDMS) { |
2202 | 3.95k | if (name == pmDef.name) { |
2203 | 3 | double dmsAsDecimalValue = |
2204 | 3 | (pmDef.deg >= 0 ? 1 : -1) * |
2205 | 3 | (std::abs(pmDef.deg) + pmDef.min / 100. + |
2206 | 3 | pmDef.sec / 10000.); |
2207 | 3 | double dmsAsDecimalDegreeValue = |
2208 | 3 | (pmDef.deg >= 0 ? 1 : -1) * |
2209 | 3 | (std::abs(pmDef.deg) + pmDef.min / 60. + |
2210 | 3 | pmDef.sec / 3600.); |
2211 | 3 | if (std::fabs(angleValue - dmsAsDecimalValue) < 1e-8 || |
2212 | 3 | std::fabs(angleValue - dmsAsDecimalDegreeValue) < |
2213 | 3 | 1e-8) { |
2214 | 0 | angleValue = dmsAsDecimalDegreeValue; |
2215 | 0 | unit = UnitOfMeasure::DEGREE; |
2216 | 0 | } |
2217 | 3 | break; |
2218 | 3 | } |
2219 | 3.95k | } |
2220 | 310 | } |
2221 | | |
2222 | 310 | auto &properties = buildProperties(node); |
2223 | 310 | if (dbContext_ && esriStyle_) { |
2224 | 272 | std::string outTableName; |
2225 | 272 | std::string codeFromAlias; |
2226 | 272 | std::string authNameFromAlias; |
2227 | 272 | auto authFactory = AuthorityFactory::create(NN_NO_CHECK(dbContext_), |
2228 | 272 | std::string()); |
2229 | 272 | auto officialName = authFactory->getOfficialNameFromAlias( |
2230 | 272 | name, "prime_meridian", "ESRI", false, outTableName, |
2231 | 272 | authNameFromAlias, codeFromAlias); |
2232 | 272 | if (!officialName.empty()) { |
2233 | 0 | properties.set(IdentifiedObject::NAME_KEY, officialName); |
2234 | 0 | if (!authNameFromAlias.empty()) { |
2235 | 0 | auto identifiers = ArrayOfBaseObject::create(); |
2236 | 0 | identifiers->add(Identifier::create( |
2237 | 0 | codeFromAlias, |
2238 | 0 | PropertyMap() |
2239 | 0 | .set(Identifier::CODESPACE_KEY, authNameFromAlias) |
2240 | 0 | .set(Identifier::AUTHORITY_KEY, |
2241 | 0 | authNameFromAlias))); |
2242 | 0 | properties.set(IdentifiedObject::IDENTIFIERS_KEY, |
2243 | 0 | identifiers); |
2244 | 0 | } |
2245 | 0 | } |
2246 | 272 | } |
2247 | | |
2248 | 310 | Angle angle(angleValue, unit); |
2249 | 310 | return PrimeMeridian::create(properties, angle); |
2250 | 310 | } catch (const std::exception &e) { |
2251 | 5 | throw buildRethrow(__FUNCTION__, e); |
2252 | 5 | } |
2253 | 310 | } |
2254 | | |
2255 | | // --------------------------------------------------------------------------- |
2256 | | |
2257 | 1.71k | optional<std::string> WKTParser::Private::getAnchor(const WKTNodeNNPtr &node) { |
2258 | | |
2259 | 1.71k | auto &anchorNode = node->GP()->lookForChild(WKTConstants::ANCHOR); |
2260 | 1.71k | if (anchorNode->GP()->childrenSize() == 1) { |
2261 | 0 | return optional<std::string>( |
2262 | 0 | stripQuotes(anchorNode->GP()->children()[0])); |
2263 | 0 | } |
2264 | 1.71k | return optional<std::string>(); |
2265 | 1.71k | } |
2266 | | |
2267 | | // --------------------------------------------------------------------------- |
2268 | | |
2269 | | optional<common::Measure> |
2270 | 1.69k | WKTParser::Private::getAnchorEpoch(const WKTNodeNNPtr &node) { |
2271 | | |
2272 | 1.69k | auto &anchorEpochNode = node->GP()->lookForChild(WKTConstants::ANCHOREPOCH); |
2273 | 1.69k | if (anchorEpochNode->GP()->childrenSize() == 1) { |
2274 | 0 | try { |
2275 | 0 | double value = asDouble(anchorEpochNode->GP()->children()[0]); |
2276 | 0 | return optional<common::Measure>( |
2277 | 0 | common::Measure(value, common::UnitOfMeasure::YEAR)); |
2278 | 0 | } catch (const std::exception &e) { |
2279 | 0 | throw buildRethrow(__FUNCTION__, e); |
2280 | 0 | } |
2281 | 0 | } |
2282 | 1.69k | return optional<common::Measure>(); |
2283 | 1.69k | } |
2284 | | // --------------------------------------------------------------------------- |
2285 | | |
2286 | | static const PrimeMeridianNNPtr & |
2287 | | fixupPrimeMeridan(const EllipsoidNNPtr &ellipsoid, |
2288 | 1.62k | const PrimeMeridianNNPtr &pm) { |
2289 | 1.62k | return (ellipsoid->celestialBody() != Ellipsoid::EARTH && |
2290 | 1.62k | pm.get() == PrimeMeridian::GREENWICH.get()) |
2291 | 1.62k | ? PrimeMeridian::REFERENCE_MERIDIAN |
2292 | 1.62k | : pm; |
2293 | 1.62k | } |
2294 | | |
2295 | | // --------------------------------------------------------------------------- |
2296 | | |
2297 | | GeodeticReferenceFrameNNPtr WKTParser::Private::buildGeodeticReferenceFrame( |
2298 | | const WKTNodeNNPtr &node, const PrimeMeridianNNPtr &primeMeridian, |
2299 | 1.08k | const WKTNodeNNPtr &dynamicNode) { |
2300 | 1.08k | const auto *nodeP = node->GP(); |
2301 | 1.08k | auto &ellipsoidNode = |
2302 | 1.08k | nodeP->lookForChild(WKTConstants::ELLIPSOID, WKTConstants::SPHEROID); |
2303 | 1.08k | if (isNull(ellipsoidNode)) { |
2304 | 66 | ThrowMissing(WKTConstants::ELLIPSOID); |
2305 | 66 | } |
2306 | 1.01k | auto &properties = buildProperties(node); |
2307 | | |
2308 | | // do that before buildEllipsoid() so that esriStyle_ can be set |
2309 | 1.01k | auto name = stripQuotes(nodeP->children()[0]); |
2310 | | |
2311 | 1.01k | const auto identifyFromName = [&](const std::string &l_name) { |
2312 | 121 | if (dbContext_) { |
2313 | 112 | auto authFactory = AuthorityFactory::create(NN_NO_CHECK(dbContext_), |
2314 | 112 | std::string()); |
2315 | 112 | auto res = authFactory->createObjectsFromName( |
2316 | 112 | l_name, |
2317 | 112 | {AuthorityFactory::ObjectType::GEODETIC_REFERENCE_FRAME}, true, |
2318 | 112 | 1); |
2319 | 112 | if (!res.empty()) { |
2320 | 59 | bool foundDatumName = false; |
2321 | 59 | const auto &refDatum = res.front(); |
2322 | 59 | if (metadata::Identifier::isEquivalentName( |
2323 | 59 | l_name.c_str(), refDatum->nameStr().c_str())) { |
2324 | 13 | foundDatumName = true; |
2325 | 46 | } else if (refDatum->identifiers().size() == 1) { |
2326 | 46 | const auto &id = refDatum->identifiers()[0]; |
2327 | 46 | const auto aliases = |
2328 | 46 | authFactory->databaseContext()->getAliases( |
2329 | 46 | *id->codeSpace(), id->code(), refDatum->nameStr(), |
2330 | 46 | "geodetic_datum", std::string()); |
2331 | 46 | for (const auto &alias : aliases) { |
2332 | 6 | if (metadata::Identifier::isEquivalentName( |
2333 | 6 | l_name.c_str(), alias.c_str())) { |
2334 | 0 | foundDatumName = true; |
2335 | 0 | break; |
2336 | 0 | } |
2337 | 6 | } |
2338 | 46 | } |
2339 | 59 | if (foundDatumName) { |
2340 | 13 | properties.set(IdentifiedObject::NAME_KEY, |
2341 | 13 | refDatum->nameStr()); |
2342 | 13 | if (!properties.get(Identifier::CODESPACE_KEY) && |
2343 | 13 | refDatum->identifiers().size() == 1) { |
2344 | 13 | const auto &id = refDatum->identifiers()[0]; |
2345 | 13 | auto identifiers = ArrayOfBaseObject::create(); |
2346 | 13 | identifiers->add(Identifier::create( |
2347 | 13 | id->code(), PropertyMap() |
2348 | 13 | .set(Identifier::CODESPACE_KEY, |
2349 | 13 | *id->codeSpace()) |
2350 | 13 | .set(Identifier::AUTHORITY_KEY, |
2351 | 13 | *id->codeSpace()))); |
2352 | 13 | properties.set(IdentifiedObject::IDENTIFIERS_KEY, |
2353 | 13 | identifiers); |
2354 | 13 | } |
2355 | 13 | return true; |
2356 | 13 | } |
2357 | 59 | } else { |
2358 | | // Get official name from database if AUTHORITY is present |
2359 | 53 | auto &idNode = nodeP->lookForChild(WKTConstants::AUTHORITY); |
2360 | 53 | if (!isNull(idNode)) { |
2361 | 2 | try { |
2362 | 2 | auto id = buildId(node, idNode, false, false); |
2363 | 2 | auto authFactory2 = AuthorityFactory::create( |
2364 | 2 | NN_NO_CHECK(dbContext_), *id->codeSpace()); |
2365 | 2 | auto dbDatum = |
2366 | 2 | authFactory2->createGeodeticDatum(id->code()); |
2367 | 2 | properties.set(IdentifiedObject::NAME_KEY, |
2368 | 2 | dbDatum->nameStr()); |
2369 | 2 | return true; |
2370 | 2 | } catch (const std::exception &) { |
2371 | 2 | } |
2372 | 2 | } |
2373 | 53 | } |
2374 | 112 | } |
2375 | 108 | return false; |
2376 | 121 | }; |
2377 | | |
2378 | | // Remap GDAL WGS_1984 to EPSG v9 "World Geodetic System 1984" official |
2379 | | // name. |
2380 | | // Also remap EPSG v10 datum ensemble names to non-ensemble EPSG v9 |
2381 | 1.01k | bool nameSet = false; |
2382 | 1.01k | if (name == "WGS_1984" || name == "World Geodetic System 1984 ensemble") { |
2383 | 0 | nameSet = true; |
2384 | 0 | properties.set(IdentifiedObject::NAME_KEY, |
2385 | 0 | GeodeticReferenceFrame::EPSG_6326->nameStr()); |
2386 | 1.01k | } else if (name == "European Terrestrial Reference System 1989 ensemble") { |
2387 | 0 | nameSet = true; |
2388 | 0 | properties.set(IdentifiedObject::NAME_KEY, |
2389 | 0 | "European Terrestrial Reference System 1989"); |
2390 | 0 | } |
2391 | | |
2392 | | // If we got hints this might be a ESRI WKT, then check in the DB to |
2393 | | // confirm |
2394 | 1.01k | std::string officialName; |
2395 | 1.01k | std::string authNameFromAlias; |
2396 | 1.01k | std::string codeFromAlias; |
2397 | 1.01k | if (!nameSet && maybeEsriStyle_ && dbContext_ && |
2398 | 1.01k | !(starts_with(name, "D_") || esriStyle_)) { |
2399 | 457 | std::string outTableName; |
2400 | 457 | auto authFactory = |
2401 | 457 | AuthorityFactory::create(NN_NO_CHECK(dbContext_), std::string()); |
2402 | 457 | officialName = authFactory->getOfficialNameFromAlias( |
2403 | 457 | name, "geodetic_datum", "ESRI", false, outTableName, |
2404 | 457 | authNameFromAlias, codeFromAlias); |
2405 | 457 | if (!officialName.empty()) { |
2406 | 0 | maybeEsriStyle_ = false; |
2407 | 0 | esriStyle_ = true; |
2408 | 0 | } |
2409 | 457 | } |
2410 | | |
2411 | 1.01k | if (!nameSet && (starts_with(name, "D_") || esriStyle_)) { |
2412 | 310 | esriStyle_ = true; |
2413 | 310 | const char *tableNameForAlias = nullptr; |
2414 | 310 | if (name == "D_WGS_1984") { |
2415 | 142 | name = "World Geodetic System 1984"; |
2416 | 142 | authNameFromAlias = Identifier::EPSG; |
2417 | 142 | codeFromAlias = "6326"; |
2418 | 168 | } else if (name == "D_ETRS_1989") { |
2419 | 0 | name = "European Terrestrial Reference System 1989"; |
2420 | 0 | authNameFromAlias = Identifier::EPSG; |
2421 | 0 | codeFromAlias = "6258"; |
2422 | 168 | } else if (name == "D_unknown") { |
2423 | 0 | name = "unknown"; |
2424 | 168 | } else if (name == "D_Unknown_based_on_WGS_84_ellipsoid") { |
2425 | 0 | name = "Unknown based on WGS 84 ellipsoid"; |
2426 | 168 | } else { |
2427 | 168 | tableNameForAlias = "geodetic_datum"; |
2428 | 168 | } |
2429 | | |
2430 | 310 | bool setNameAndId = true; |
2431 | 310 | if (dbContext_ && tableNameForAlias) { |
2432 | 158 | if (officialName.empty()) { |
2433 | 158 | std::string outTableName; |
2434 | 158 | auto authFactory = AuthorityFactory::create( |
2435 | 158 | NN_NO_CHECK(dbContext_), std::string()); |
2436 | 158 | officialName = authFactory->getOfficialNameFromAlias( |
2437 | 158 | name, tableNameForAlias, "ESRI", false, outTableName, |
2438 | 158 | authNameFromAlias, codeFromAlias); |
2439 | 158 | } |
2440 | 158 | if (officialName.empty()) { |
2441 | 66 | if (starts_with(name, "D_")) { |
2442 | | // For the case of "D_GDA2020" where there is no D_GDA2020 |
2443 | | // ESRI alias, so just try without the D_ prefix. |
2444 | 40 | const auto nameWithoutDPrefix = name.substr(2); |
2445 | 40 | if (identifyFromName(nameWithoutDPrefix)) { |
2446 | 0 | setNameAndId = |
2447 | 0 | false; // already done in identifyFromName() |
2448 | 0 | } |
2449 | 40 | } |
2450 | 92 | } else { |
2451 | 92 | if (primeMeridian->nameStr() != |
2452 | 92 | PrimeMeridian::GREENWICH->nameStr()) { |
2453 | 0 | auto nameWithPM = |
2454 | 0 | officialName + " (" + primeMeridian->nameStr() + ")"; |
2455 | 0 | if (dbContext_->isKnownName(nameWithPM, "geodetic_datum")) { |
2456 | 0 | officialName = std::move(nameWithPM); |
2457 | 0 | } |
2458 | 0 | } |
2459 | 92 | name = std::move(officialName); |
2460 | 92 | } |
2461 | 158 | } |
2462 | | |
2463 | 310 | if (setNameAndId) { |
2464 | 310 | properties.set(IdentifiedObject::NAME_KEY, name); |
2465 | 310 | if (!authNameFromAlias.empty()) { |
2466 | 234 | auto identifiers = ArrayOfBaseObject::create(); |
2467 | 234 | identifiers->add(Identifier::create( |
2468 | 234 | codeFromAlias, |
2469 | 234 | PropertyMap() |
2470 | 234 | .set(Identifier::CODESPACE_KEY, authNameFromAlias) |
2471 | 234 | .set(Identifier::AUTHORITY_KEY, authNameFromAlias))); |
2472 | 234 | properties.set(IdentifiedObject::IDENTIFIERS_KEY, identifiers); |
2473 | 234 | } |
2474 | 310 | } |
2475 | 706 | } else if (!nameSet && name.find('_') != std::string::npos) { |
2476 | | // Likely coming from WKT1 |
2477 | 81 | identifyFromName(name); |
2478 | 81 | } |
2479 | | |
2480 | 1.01k | auto ellipsoid = buildEllipsoid(ellipsoidNode); |
2481 | 1.01k | const auto &primeMeridianModified = |
2482 | 1.01k | fixupPrimeMeridan(ellipsoid, primeMeridian); |
2483 | | |
2484 | 1.01k | auto &TOWGS84Node = nodeP->lookForChild(WKTConstants::TOWGS84); |
2485 | 1.01k | if (!isNull(TOWGS84Node)) { |
2486 | 4 | const auto &TOWGS84Children = TOWGS84Node->GP()->children(); |
2487 | 4 | const size_t TOWGS84Size = TOWGS84Children.size(); |
2488 | 4 | if (TOWGS84Size == 3 || TOWGS84Size == 7) { |
2489 | 3 | try { |
2490 | 8 | for (const auto &child : TOWGS84Children) { |
2491 | 8 | toWGS84Parameters_.push_back(asDouble(child)); |
2492 | 8 | } |
2493 | | |
2494 | 3 | if (TOWGS84Size == 7 && dbContext_) { |
2495 | 0 | dbContext_->toWGS84AutocorrectWrongValues( |
2496 | 0 | toWGS84Parameters_[0], toWGS84Parameters_[1], |
2497 | 0 | toWGS84Parameters_[2], toWGS84Parameters_[3], |
2498 | 0 | toWGS84Parameters_[4], toWGS84Parameters_[5], |
2499 | 0 | toWGS84Parameters_[6]); |
2500 | 0 | } |
2501 | | |
2502 | 11 | for (size_t i = TOWGS84Size; i < 7; ++i) { |
2503 | 8 | toWGS84Parameters_.push_back(0.0); |
2504 | 8 | } |
2505 | 3 | } catch (const std::exception &) { |
2506 | 1 | throw ParsingException("Invalid TOWGS84 node"); |
2507 | 1 | } |
2508 | 3 | } else { |
2509 | 1 | throw ParsingException("Invalid TOWGS84 node"); |
2510 | 1 | } |
2511 | 4 | } |
2512 | | |
2513 | 1.01k | auto &extensionNode = nodeP->lookForChild(WKTConstants::EXTENSION); |
2514 | 1.01k | const auto &extensionChildren = extensionNode->GP()->children(); |
2515 | 1.01k | if (extensionChildren.size() == 2) { |
2516 | 29 | if (ci_equal(stripQuotes(extensionChildren[0]), "PROJ4_GRIDS")) { |
2517 | 1 | datumPROJ4Grids_ = stripQuotes(extensionChildren[1]); |
2518 | 1 | } |
2519 | 29 | } |
2520 | | |
2521 | 1.01k | if (!isNull(dynamicNode)) { |
2522 | 17 | double frameReferenceEpoch = 0.0; |
2523 | 17 | util::optional<std::string> modelName; |
2524 | 17 | parseDynamic(dynamicNode, frameReferenceEpoch, modelName); |
2525 | 17 | return DynamicGeodeticReferenceFrame::create( |
2526 | 17 | properties, ellipsoid, getAnchor(node), primeMeridianModified, |
2527 | 17 | common::Measure(frameReferenceEpoch, common::UnitOfMeasure::YEAR), |
2528 | 17 | modelName); |
2529 | 17 | } |
2530 | | |
2531 | 997 | return GeodeticReferenceFrame::create(properties, ellipsoid, |
2532 | 997 | getAnchor(node), getAnchorEpoch(node), |
2533 | 997 | primeMeridianModified); |
2534 | 1.01k | } |
2535 | | |
2536 | | // --------------------------------------------------------------------------- |
2537 | | |
2538 | | DatumEnsembleNNPtr |
2539 | | WKTParser::Private::buildDatumEnsemble(const WKTNodeNNPtr &node, |
2540 | | const PrimeMeridianPtr &primeMeridian, |
2541 | 182 | bool expectEllipsoid) { |
2542 | 182 | const auto *nodeP = node->GP(); |
2543 | 182 | auto &ellipsoidNode = |
2544 | 182 | nodeP->lookForChild(WKTConstants::ELLIPSOID, WKTConstants::SPHEROID); |
2545 | 182 | if (expectEllipsoid && isNull(ellipsoidNode)) { |
2546 | 14 | ThrowMissing(WKTConstants::ELLIPSOID); |
2547 | 14 | } |
2548 | | |
2549 | 168 | std::vector<DatumNNPtr> datums; |
2550 | 2.76k | for (const auto &subNode : nodeP->children()) { |
2551 | 2.76k | if (ci_equal(subNode->GP()->value(), WKTConstants::MEMBER)) { |
2552 | 796 | if (subNode->GP()->childrenSize() == 0) { |
2553 | 3 | throw ParsingException("Invalid MEMBER node"); |
2554 | 3 | } |
2555 | 793 | if (expectEllipsoid) { |
2556 | 538 | datums.emplace_back(GeodeticReferenceFrame::create( |
2557 | 538 | buildProperties(subNode), buildEllipsoid(ellipsoidNode), |
2558 | 538 | optional<std::string>(), |
2559 | 538 | primeMeridian ? NN_NO_CHECK(primeMeridian) |
2560 | 538 | : PrimeMeridian::GREENWICH)); |
2561 | 538 | } else { |
2562 | 255 | datums.emplace_back( |
2563 | 255 | VerticalReferenceFrame::create(buildProperties(subNode))); |
2564 | 255 | } |
2565 | 793 | } |
2566 | 2.76k | } |
2567 | | |
2568 | 165 | auto &accuracyNode = nodeP->lookForChild(WKTConstants::ENSEMBLEACCURACY); |
2569 | 165 | auto &accuracyNodeChildren = accuracyNode->GP()->children(); |
2570 | 165 | if (accuracyNodeChildren.empty()) { |
2571 | 110 | ThrowMissing(WKTConstants::ENSEMBLEACCURACY); |
2572 | 110 | } |
2573 | 55 | auto accuracy = |
2574 | 55 | PositionalAccuracy::create(accuracyNodeChildren[0]->GP()->value()); |
2575 | | |
2576 | 55 | try { |
2577 | 55 | return DatumEnsemble::create(buildProperties(node), datums, accuracy); |
2578 | 55 | } catch (const util::Exception &e) { |
2579 | 5 | throw buildRethrow(__FUNCTION__, e); |
2580 | 5 | } |
2581 | 55 | } |
2582 | | |
2583 | | // --------------------------------------------------------------------------- |
2584 | | |
2585 | 0 | MeridianNNPtr WKTParser::Private::buildMeridian(const WKTNodeNNPtr &node) { |
2586 | 0 | const auto *nodeP = node->GP(); |
2587 | 0 | const auto &children = nodeP->children(); |
2588 | 0 | if (children.size() < 2) { |
2589 | 0 | ThrowNotEnoughChildren(nodeP->value()); |
2590 | 0 | } |
2591 | 0 | UnitOfMeasure unit = buildUnitInSubNode(node, UnitOfMeasure::Type::ANGULAR); |
2592 | 0 | try { |
2593 | 0 | double angleValue = asDouble(children[0]); |
2594 | 0 | Angle angle(angleValue, unit); |
2595 | 0 | return Meridian::create(angle); |
2596 | 0 | } catch (const std::exception &e) { |
2597 | 0 | throw buildRethrow(__FUNCTION__, e); |
2598 | 0 | } |
2599 | 0 | } |
2600 | | |
2601 | | // --------------------------------------------------------------------------- |
2602 | | |
2603 | 11 | PROJ_NO_RETURN static void ThrowParsingExceptionMissingUNIT() { |
2604 | 11 | throw ParsingException("buildCS: missing UNIT"); |
2605 | 11 | } |
2606 | | |
2607 | | // --------------------------------------------------------------------------- |
2608 | | |
2609 | | CoordinateSystemAxisNNPtr |
2610 | | WKTParser::Private::buildAxis(const WKTNodeNNPtr &node, |
2611 | | const UnitOfMeasure &unitIn, |
2612 | | const UnitOfMeasure::Type &unitType, |
2613 | 186 | bool isGeocentric, int expectedOrderNum) { |
2614 | 186 | const auto *nodeP = node->GP(); |
2615 | 186 | const auto &children = nodeP->children(); |
2616 | 186 | if (children.size() < 2) { |
2617 | 11 | ThrowNotEnoughChildren(nodeP->value()); |
2618 | 11 | } |
2619 | | |
2620 | 175 | auto &orderNode = nodeP->lookForChild(WKTConstants::ORDER); |
2621 | 175 | if (!isNull(orderNode)) { |
2622 | 10 | const auto &orderNodeChildren = orderNode->GP()->children(); |
2623 | 10 | if (orderNodeChildren.size() != 1) { |
2624 | 3 | ThrowNotEnoughChildren(WKTConstants::ORDER); |
2625 | 3 | } |
2626 | 7 | const auto &order = orderNodeChildren[0]->GP()->value(); |
2627 | 7 | int orderNum; |
2628 | 7 | try { |
2629 | 7 | orderNum = std::stoi(order); |
2630 | 7 | } catch (const std::exception &) { |
2631 | 1 | throw ParsingException( |
2632 | 1 | concat("buildAxis: invalid ORDER value: ", order)); |
2633 | 1 | } |
2634 | 6 | if (orderNum != expectedOrderNum) { |
2635 | 3 | throw ParsingException( |
2636 | 3 | concat("buildAxis: did not get expected ORDER value: ", order)); |
2637 | 3 | } |
2638 | 6 | } |
2639 | | |
2640 | | // The axis designation in WK2 can be: "name", "(abbrev)" or "name |
2641 | | // (abbrev)" |
2642 | 168 | std::string axisDesignation(stripQuotes(children[0])); |
2643 | 168 | size_t sepPos = axisDesignation.find(" ("); |
2644 | 168 | std::string axisName; |
2645 | 168 | std::string abbreviation; |
2646 | 168 | if (sepPos != std::string::npos && axisDesignation.back() == ')') { |
2647 | 9 | axisName = CoordinateSystemAxis::normalizeAxisName( |
2648 | 9 | axisDesignation.substr(0, sepPos)); |
2649 | 9 | abbreviation = axisDesignation.substr(sepPos + 2); |
2650 | 9 | abbreviation.resize(abbreviation.size() - 1); |
2651 | 159 | } else if (!axisDesignation.empty() && axisDesignation[0] == '(' && |
2652 | 159 | axisDesignation.back() == ')') { |
2653 | 18 | abbreviation = axisDesignation.substr(1, axisDesignation.size() - 2); |
2654 | 18 | if (abbreviation == AxisAbbreviation::E) { |
2655 | 0 | axisName = AxisName::Easting; |
2656 | 18 | } else if (abbreviation == AxisAbbreviation::N) { |
2657 | 0 | axisName = AxisName::Northing; |
2658 | 18 | } else if (abbreviation == AxisAbbreviation::lat) { |
2659 | 0 | axisName = AxisName::Latitude; |
2660 | 18 | } else if (abbreviation == AxisAbbreviation::lon) { |
2661 | 0 | axisName = AxisName::Longitude; |
2662 | 0 | } |
2663 | 141 | } else { |
2664 | 141 | axisName = CoordinateSystemAxis::normalizeAxisName(axisDesignation); |
2665 | 141 | if (axisName == AxisName::Latitude) { |
2666 | 0 | abbreviation = AxisAbbreviation::lat; |
2667 | 141 | } else if (axisName == AxisName::Longitude) { |
2668 | 0 | abbreviation = AxisAbbreviation::lon; |
2669 | 141 | } else if (axisName == AxisName::Ellipsoidal_height) { |
2670 | 0 | abbreviation = AxisAbbreviation::h; |
2671 | 0 | } |
2672 | 141 | } |
2673 | 168 | const std::string &dirString = children[1]->GP()->value(); |
2674 | 168 | const AxisDirection *direction = AxisDirection::valueOf(dirString); |
2675 | | |
2676 | | // WKT2, geocentric CS: axis names are omitted |
2677 | 168 | if (axisName.empty()) { |
2678 | 24 | if (direction == &AxisDirection::GEOCENTRIC_X && |
2679 | 24 | abbreviation == AxisAbbreviation::X) { |
2680 | 0 | axisName = AxisName::Geocentric_X; |
2681 | 24 | } else if (direction == &AxisDirection::GEOCENTRIC_Y && |
2682 | 24 | abbreviation == AxisAbbreviation::Y) { |
2683 | 0 | axisName = AxisName::Geocentric_Y; |
2684 | 24 | } else if (direction == &AxisDirection::GEOCENTRIC_Z && |
2685 | 24 | abbreviation == AxisAbbreviation::Z) { |
2686 | 0 | axisName = AxisName::Geocentric_Z; |
2687 | 0 | } |
2688 | 24 | } |
2689 | | |
2690 | | // WKT1 |
2691 | 168 | if (!direction && isGeocentric && axisName == AxisName::Geocentric_X) { |
2692 | 18 | abbreviation = AxisAbbreviation::X; |
2693 | 18 | direction = &AxisDirection::GEOCENTRIC_X; |
2694 | 150 | } else if (!direction && isGeocentric && |
2695 | 150 | axisName == AxisName::Geocentric_Y) { |
2696 | 0 | abbreviation = AxisAbbreviation::Y; |
2697 | 0 | direction = &AxisDirection::GEOCENTRIC_Y; |
2698 | 150 | } else if (isGeocentric && axisName == AxisName::Geocentric_Z && |
2699 | 150 | (dirString == AxisDirectionWKT1::NORTH.toString() || |
2700 | 9 | dirString == AxisDirectionWKT1::OTHER.toString())) { |
2701 | 0 | abbreviation = AxisAbbreviation::Z; |
2702 | 0 | direction = &AxisDirection::GEOCENTRIC_Z; |
2703 | 150 | } else if (dirString == AxisDirectionWKT1::OTHER.toString()) { |
2704 | 0 | direction = &AxisDirection::UNSPECIFIED; |
2705 | 150 | } else if (dirString == "UNKNOWN") { |
2706 | | // Found in WKT1 of NSIDC's EASE-Grid Sea Ice Age datasets. |
2707 | | // Cf https://github.com/OSGeo/gdal/issues/7210 |
2708 | 0 | emitRecoverableWarning("UNKNOWN is not a valid direction name."); |
2709 | 0 | direction = &AxisDirection::UNSPECIFIED; |
2710 | 0 | } |
2711 | | |
2712 | 168 | if (!direction) { |
2713 | 79 | throw ParsingException( |
2714 | 79 | concat("unhandled axis direction: ", children[1]->GP()->value())); |
2715 | 79 | } |
2716 | 89 | UnitOfMeasure unit(buildUnitInSubNode(node)); |
2717 | 89 | if (unit == UnitOfMeasure::NONE) { |
2718 | | // If no unit in the AXIS node, use the one potentially coming from |
2719 | | // the CS. |
2720 | 77 | unit = unitIn; |
2721 | 77 | if (unit == UnitOfMeasure::NONE && |
2722 | 77 | unitType != UnitOfMeasure::Type::NONE && |
2723 | 77 | unitType != UnitOfMeasure::Type::TIME) { |
2724 | 11 | ThrowParsingExceptionMissingUNIT(); |
2725 | 11 | } |
2726 | 77 | } |
2727 | | |
2728 | 78 | auto &meridianNode = nodeP->lookForChild(WKTConstants::MERIDIAN); |
2729 | | |
2730 | 78 | util::optional<double> minVal; |
2731 | 78 | auto &axisMinValueNode = nodeP->lookForChild(WKTConstants::AXISMINVALUE); |
2732 | 78 | if (!isNull(axisMinValueNode)) { |
2733 | 0 | const auto &axisMinValueNodeChildren = |
2734 | 0 | axisMinValueNode->GP()->children(); |
2735 | 0 | if (axisMinValueNodeChildren.size() != 1) { |
2736 | 0 | ThrowNotEnoughChildren(WKTConstants::AXISMINVALUE); |
2737 | 0 | } |
2738 | 0 | const auto &val = axisMinValueNodeChildren[0]; |
2739 | 0 | try { |
2740 | 0 | minVal = asDouble(val); |
2741 | 0 | } catch (const std::exception &) { |
2742 | 0 | throw ParsingException(concat( |
2743 | 0 | "buildAxis: invalid AXISMINVALUE value: ", val->GP()->value())); |
2744 | 0 | } |
2745 | 0 | } |
2746 | | |
2747 | 78 | util::optional<double> maxVal; |
2748 | 78 | auto &axisMaxValueNode = nodeP->lookForChild(WKTConstants::AXISMAXVALUE); |
2749 | 78 | if (!isNull(axisMaxValueNode)) { |
2750 | 1 | const auto &axisMaxValueNodeChildren = |
2751 | 1 | axisMaxValueNode->GP()->children(); |
2752 | 1 | if (axisMaxValueNodeChildren.size() != 1) { |
2753 | 1 | ThrowNotEnoughChildren(WKTConstants::AXISMAXVALUE); |
2754 | 1 | } |
2755 | 0 | const auto &val = axisMaxValueNodeChildren[0]; |
2756 | 0 | try { |
2757 | 0 | maxVal = asDouble(val); |
2758 | 0 | } catch (const std::exception &) { |
2759 | 0 | throw ParsingException(concat( |
2760 | 0 | "buildAxis: invalid AXISMAXVALUE value: ", val->GP()->value())); |
2761 | 0 | } |
2762 | 0 | } |
2763 | | |
2764 | 77 | util::optional<RangeMeaning> rangeMeaning; |
2765 | 77 | auto &rangeMeaningNode = nodeP->lookForChild(WKTConstants::RANGEMEANING); |
2766 | 77 | if (!isNull(rangeMeaningNode)) { |
2767 | 9 | const auto &rangeMeaningNodeChildren = |
2768 | 9 | rangeMeaningNode->GP()->children(); |
2769 | 9 | if (rangeMeaningNodeChildren.size() != 1) { |
2770 | 2 | ThrowNotEnoughChildren(WKTConstants::RANGEMEANING); |
2771 | 2 | } |
2772 | 7 | const std::string &val = rangeMeaningNodeChildren[0]->GP()->value(); |
2773 | 7 | const RangeMeaning *meaning = RangeMeaning::valueOf(val); |
2774 | 7 | if (meaning == nullptr) { |
2775 | 7 | throw ParsingException( |
2776 | 7 | concat("buildAxis: invalid RANGEMEANING value: ", val)); |
2777 | 7 | } |
2778 | 0 | rangeMeaning = util::optional<RangeMeaning>(*meaning); |
2779 | 0 | } |
2780 | | |
2781 | 68 | return CoordinateSystemAxis::create( |
2782 | 68 | buildProperties(node).set(IdentifiedObject::NAME_KEY, axisName), |
2783 | 68 | abbreviation, *direction, unit, minVal, maxVal, rangeMeaning, |
2784 | 68 | !isNull(meridianNode) ? buildMeridian(meridianNode).as_nullable() |
2785 | 68 | : nullptr); |
2786 | 77 | } |
2787 | | |
2788 | | // --------------------------------------------------------------------------- |
2789 | | |
2790 | | static const PropertyMap emptyPropertyMap{}; |
2791 | | |
2792 | | // --------------------------------------------------------------------------- |
2793 | | |
2794 | 4 | PROJ_NO_RETURN static void ThrowParsingException(const std::string &msg) { |
2795 | 4 | throw ParsingException(msg); |
2796 | 4 | } |
2797 | | |
2798 | | // --------------------------------------------------------------------------- |
2799 | | |
2800 | | static ParsingException |
2801 | 23 | buildParsingExceptionInvalidAxisCount(const std::string &csType) { |
2802 | 23 | return ParsingException( |
2803 | 23 | concat("buildCS: invalid CS axis count for ", csType)); |
2804 | 23 | } |
2805 | | |
2806 | | // --------------------------------------------------------------------------- |
2807 | | |
2808 | | void WKTParser::Private::emitRecoverableMissingUNIT( |
2809 | 1.34k | const std::string &parentNodeName, const UnitOfMeasure &fallbackUnit) { |
2810 | 1.34k | std::string msg("buildCS: missing UNIT in "); |
2811 | 1.34k | msg += parentNodeName; |
2812 | 1.34k | if (!strict_ && fallbackUnit == UnitOfMeasure::METRE) { |
2813 | 918 | msg += ". Assuming metre"; |
2814 | 918 | } else if (!strict_ && fallbackUnit == UnitOfMeasure::DEGREE) { |
2815 | 431 | msg += ". Assuming degree"; |
2816 | 431 | } |
2817 | 1.34k | emitRecoverableWarning(msg); |
2818 | 1.34k | } |
2819 | | |
2820 | | // --------------------------------------------------------------------------- |
2821 | | |
2822 | | CoordinateSystemNNPtr |
2823 | | WKTParser::Private::buildCS(const WKTNodeNNPtr &node, /* maybe null */ |
2824 | | const WKTNodeNNPtr &parentNode, |
2825 | 2.57k | const UnitOfMeasure &defaultAngularUnit) { |
2826 | 2.57k | bool isGeocentric = false; |
2827 | 2.57k | std::string csType; |
2828 | 2.57k | const int numberOfAxis = |
2829 | 2.57k | parentNode->countChildrenOfName(WKTConstants::AXIS); |
2830 | 2.57k | int axisCount = numberOfAxis; |
2831 | 2.57k | const auto &parentNodeName = parentNode->GP()->value(); |
2832 | 2.57k | if (!isNull(node)) { |
2833 | 31 | const auto *nodeP = node->GP(); |
2834 | 31 | const auto &children = nodeP->children(); |
2835 | 31 | if (children.size() < 2) { |
2836 | 8 | ThrowNotEnoughChildren(nodeP->value()); |
2837 | 8 | } |
2838 | 23 | csType = children[0]->GP()->value(); |
2839 | 23 | try { |
2840 | 23 | axisCount = std::stoi(children[1]->GP()->value()); |
2841 | 23 | } catch (const std::exception &) { |
2842 | 4 | ThrowParsingException(concat("buildCS: invalid CS axis count: ", |
2843 | 4 | children[1]->GP()->value())); |
2844 | 4 | } |
2845 | 2.54k | } else { |
2846 | 2.54k | const char *csTypeCStr = CartesianCS::WKT2_TYPE; |
2847 | 2.54k | if (ci_equal(parentNodeName, WKTConstants::GEOCCS)) { |
2848 | | // csTypeCStr = CartesianCS::WKT2_TYPE; |
2849 | 98 | isGeocentric = true; |
2850 | 98 | if (axisCount == 0) { |
2851 | 59 | auto unit = |
2852 | 59 | buildUnitInSubNode(parentNode, UnitOfMeasure::Type::LINEAR); |
2853 | 59 | if (unit == UnitOfMeasure::NONE) { |
2854 | 51 | unit = UnitOfMeasure::METRE; |
2855 | 51 | emitRecoverableMissingUNIT(parentNodeName, unit); |
2856 | 51 | } |
2857 | 59 | return CartesianCS::createGeocentric(unit); |
2858 | 59 | } |
2859 | 2.44k | } else if (ci_equal(parentNodeName, WKTConstants::GEOGCS)) { |
2860 | 867 | csTypeCStr = EllipsoidalCS::WKT2_TYPE; |
2861 | 867 | if (axisCount == 0) { |
2862 | | // Missing axis with GEOGCS ? Presumably Long/Lat order |
2863 | | // implied |
2864 | 866 | auto unit = buildUnitInSubNode(parentNode, |
2865 | 866 | UnitOfMeasure::Type::ANGULAR); |
2866 | 866 | if (unit == UnitOfMeasure::NONE) { |
2867 | 431 | unit = defaultAngularUnit; |
2868 | 431 | emitRecoverableMissingUNIT(parentNodeName, unit); |
2869 | 431 | } |
2870 | | |
2871 | | // ESRI WKT for geographic 3D CRS |
2872 | 866 | auto &linUnitNode = |
2873 | 866 | parentNode->GP()->lookForChild(WKTConstants::LINUNIT); |
2874 | 866 | if (!isNull(linUnitNode)) { |
2875 | 1 | return EllipsoidalCS:: |
2876 | 1 | createLongitudeLatitudeEllipsoidalHeight( |
2877 | 1 | unit, buildUnit(linUnitNode, |
2878 | 1 | UnitOfMeasure::Type::LINEAR)); |
2879 | 1 | } |
2880 | | |
2881 | | // WKT1 --> long/lat |
2882 | 865 | return EllipsoidalCS::createLongitudeLatitude(unit); |
2883 | 866 | } |
2884 | 1.57k | } else if (ci_equal(parentNodeName, WKTConstants::BASEGEODCRS) || |
2885 | 1.57k | ci_equal(parentNodeName, WKTConstants::BASEGEOGCRS)) { |
2886 | 0 | csTypeCStr = EllipsoidalCS::WKT2_TYPE; |
2887 | 0 | if (axisCount == 0) { |
2888 | 0 | auto unit = buildUnitInSubNode(parentNode, |
2889 | 0 | UnitOfMeasure::Type::ANGULAR); |
2890 | 0 | if (unit == UnitOfMeasure::NONE) { |
2891 | 0 | unit = defaultAngularUnit; |
2892 | 0 | } |
2893 | | // WKT2 --> presumably lat/long |
2894 | 0 | return EllipsoidalCS::createLatitudeLongitude(unit); |
2895 | 0 | } |
2896 | 1.57k | } else if (ci_equal(parentNodeName, WKTConstants::PROJCS) || |
2897 | 1.57k | ci_equal(parentNodeName, WKTConstants::BASEPROJCRS) || |
2898 | 1.57k | ci_equal(parentNodeName, WKTConstants::BASEENGCRS)) { |
2899 | 629 | csTypeCStr = CartesianCS::WKT2_TYPE; |
2900 | 629 | if (axisCount == 0) { |
2901 | 628 | auto unit = |
2902 | 628 | buildUnitInSubNode(parentNode, UnitOfMeasure::Type::LINEAR); |
2903 | 628 | if (unit == UnitOfMeasure::NONE) { |
2904 | 339 | unit = UnitOfMeasure::METRE; |
2905 | 339 | if (ci_equal(parentNodeName, WKTConstants::PROJCS)) { |
2906 | 339 | emitRecoverableMissingUNIT(parentNodeName, unit); |
2907 | 339 | } |
2908 | 339 | } |
2909 | 628 | return CartesianCS::createEastingNorthing(unit); |
2910 | 628 | } |
2911 | 949 | } else if (ci_equal(parentNodeName, WKTConstants::VERT_CS) || |
2912 | 949 | ci_equal(parentNodeName, WKTConstants::VERTCS) || |
2913 | 949 | ci_equal(parentNodeName, WKTConstants::BASEVERTCRS)) { |
2914 | 597 | csTypeCStr = VerticalCS::WKT2_TYPE; |
2915 | | |
2916 | 597 | bool downDirection = false; |
2917 | 597 | if (ci_equal(parentNodeName, WKTConstants::VERTCS)) // ESRI |
2918 | 579 | { |
2919 | 2.74k | for (const auto &childNode : parentNode->GP()->children()) { |
2920 | 2.74k | const auto &childNodeChildren = childNode->GP()->children(); |
2921 | 2.74k | if (childNodeChildren.size() == 2 && |
2922 | 2.74k | ci_equal(childNode->GP()->value(), |
2923 | 243 | WKTConstants::PARAMETER) && |
2924 | 2.74k | childNodeChildren[0]->GP()->value() == |
2925 | 22 | "\"Direction\"") { |
2926 | 0 | const auto ¶mValue = |
2927 | 0 | childNodeChildren[1]->GP()->value(); |
2928 | 0 | try { |
2929 | 0 | double val = asDouble(childNodeChildren[1]); |
2930 | 0 | if (val == 1.0) { |
2931 | | // ok |
2932 | 0 | } else if (val == -1.0) { |
2933 | 0 | downDirection = true; |
2934 | 0 | } |
2935 | 0 | } catch (const std::exception &) { |
2936 | 0 | throw ParsingException( |
2937 | 0 | concat("unhandled parameter value type : ", |
2938 | 0 | paramValue)); |
2939 | 0 | } |
2940 | 0 | } |
2941 | 2.74k | } |
2942 | 579 | } |
2943 | | |
2944 | 597 | if (axisCount == 0) { |
2945 | 516 | auto unit = |
2946 | 516 | buildUnitInSubNode(parentNode, UnitOfMeasure::Type::LINEAR); |
2947 | 516 | if (unit == UnitOfMeasure::NONE) { |
2948 | 447 | unit = UnitOfMeasure::METRE; |
2949 | 447 | if (ci_equal(parentNodeName, WKTConstants::VERT_CS) || |
2950 | 447 | ci_equal(parentNodeName, WKTConstants::VERTCS)) { |
2951 | 447 | emitRecoverableMissingUNIT(parentNodeName, unit); |
2952 | 447 | } |
2953 | 447 | } |
2954 | 516 | if (downDirection) { |
2955 | 0 | return VerticalCS::create( |
2956 | 0 | util::PropertyMap(), |
2957 | 0 | CoordinateSystemAxis::create( |
2958 | 0 | util::PropertyMap().set(IdentifiedObject::NAME_KEY, |
2959 | 0 | "depth"), |
2960 | 0 | "D", AxisDirection::DOWN, unit)); |
2961 | 0 | } |
2962 | 516 | return VerticalCS::createGravityRelatedHeight(unit); |
2963 | 516 | } |
2964 | 597 | } else if (ci_equal(parentNodeName, WKTConstants::LOCAL_CS)) { |
2965 | 352 | if (axisCount == 0) { |
2966 | 286 | auto unit = |
2967 | 286 | buildUnitInSubNode(parentNode, UnitOfMeasure::Type::LINEAR); |
2968 | 286 | if (unit == UnitOfMeasure::NONE) { |
2969 | 285 | unit = UnitOfMeasure::METRE; |
2970 | 285 | } |
2971 | 286 | return CartesianCS::createEastingNorthing(unit); |
2972 | 286 | } else if (axisCount == 1) { |
2973 | 57 | csTypeCStr = VerticalCS::WKT2_TYPE; |
2974 | 57 | } else if (axisCount == 2 || axisCount == 3) { |
2975 | 6 | csTypeCStr = CartesianCS::WKT2_TYPE; |
2976 | 6 | } else { |
2977 | 3 | throw ParsingException( |
2978 | 3 | "buildCS: unexpected AXIS count for LOCAL_CS"); |
2979 | 3 | } |
2980 | 352 | } else if (ci_equal(parentNodeName, WKTConstants::BASEPARAMCRS)) { |
2981 | 0 | csTypeCStr = ParametricCS::WKT2_TYPE; |
2982 | 0 | if (axisCount == 0) { |
2983 | 0 | auto unit = |
2984 | 0 | buildUnitInSubNode(parentNode, UnitOfMeasure::Type::LINEAR); |
2985 | 0 | if (unit == UnitOfMeasure::NONE) { |
2986 | 0 | unit = UnitOfMeasure("unknown", 1, |
2987 | 0 | UnitOfMeasure::Type::PARAMETRIC); |
2988 | 0 | } |
2989 | 0 | return ParametricCS::create( |
2990 | 0 | emptyPropertyMap, |
2991 | 0 | CoordinateSystemAxis::create( |
2992 | 0 | PropertyMap().set(IdentifiedObject::NAME_KEY, |
2993 | 0 | "unknown parametric"), |
2994 | 0 | std::string(), AxisDirection::UNSPECIFIED, unit)); |
2995 | 0 | } |
2996 | 0 | } else if (ci_equal(parentNodeName, WKTConstants::BASETIMECRS)) { |
2997 | 0 | csTypeCStr = TemporalCS::WKT2_2015_TYPE; |
2998 | 0 | if (axisCount == 0) { |
2999 | 0 | auto unit = |
3000 | 0 | buildUnitInSubNode(parentNode, UnitOfMeasure::Type::TIME); |
3001 | 0 | if (unit == UnitOfMeasure::NONE) { |
3002 | 0 | unit = |
3003 | 0 | UnitOfMeasure("unknown", 1, UnitOfMeasure::Type::TIME); |
3004 | 0 | } |
3005 | 0 | return DateTimeTemporalCS::create( |
3006 | 0 | emptyPropertyMap, |
3007 | 0 | CoordinateSystemAxis::create( |
3008 | 0 | PropertyMap().set(IdentifiedObject::NAME_KEY, |
3009 | 0 | "unknown temporal"), |
3010 | 0 | std::string(), AxisDirection::FUTURE, unit)); |
3011 | 0 | } |
3012 | 0 | } else { |
3013 | | // Shouldn't happen normally |
3014 | 0 | throw ParsingException( |
3015 | 0 | concat("buildCS: unexpected parent node: ", parentNodeName)); |
3016 | 0 | } |
3017 | 185 | csType = csTypeCStr; |
3018 | 185 | } |
3019 | | |
3020 | 204 | if (axisCount != 1 && axisCount != 2 && axisCount != 3) { |
3021 | 19 | throw buildParsingExceptionInvalidAxisCount(csType); |
3022 | 19 | } |
3023 | 185 | if (numberOfAxis != axisCount) { |
3024 | 0 | throw ParsingException("buildCS: declared number of axis by CS node " |
3025 | 0 | "and number of AXIS are inconsistent"); |
3026 | 0 | } |
3027 | | |
3028 | 185 | const auto unitType = |
3029 | 185 | ci_equal(csType, EllipsoidalCS::WKT2_TYPE) |
3030 | 185 | ? UnitOfMeasure::Type::ANGULAR |
3031 | 185 | : ci_equal(csType, OrdinalCS::WKT2_TYPE) ? UnitOfMeasure::Type::NONE |
3032 | 184 | : ci_equal(csType, ParametricCS::WKT2_TYPE) |
3033 | 184 | ? UnitOfMeasure::Type::PARAMETRIC |
3034 | 184 | : ci_equal(csType, CartesianCS::WKT2_TYPE) || |
3035 | 184 | ci_equal(csType, VerticalCS::WKT2_TYPE) || |
3036 | 184 | ci_equal(csType, AffineCS::WKT2_TYPE) |
3037 | 184 | ? UnitOfMeasure::Type::LINEAR |
3038 | 184 | : (ci_equal(csType, TemporalCS::WKT2_2015_TYPE) || |
3039 | 0 | ci_equal(csType, DateTimeTemporalCS::WKT2_2019_TYPE) || |
3040 | 0 | ci_equal(csType, TemporalCountCS::WKT2_2019_TYPE) || |
3041 | 0 | ci_equal(csType, TemporalMeasureCS::WKT2_2019_TYPE)) |
3042 | 0 | ? UnitOfMeasure::Type::TIME |
3043 | 0 | : UnitOfMeasure::Type::UNKNOWN; |
3044 | 185 | UnitOfMeasure unit = buildUnitInSubNode(parentNode, unitType); |
3045 | | |
3046 | 185 | if (unit == UnitOfMeasure::NONE) { |
3047 | 177 | if (ci_equal(parentNodeName, WKTConstants::VERT_CS) || |
3048 | 177 | ci_equal(parentNodeName, WKTConstants::VERTCS)) { |
3049 | 81 | unit = UnitOfMeasure::METRE; |
3050 | 81 | emitRecoverableMissingUNIT(parentNodeName, unit); |
3051 | 81 | } |
3052 | 177 | } |
3053 | | |
3054 | 185 | std::vector<CoordinateSystemAxisNNPtr> axisList; |
3055 | 371 | for (int i = 0; i < axisCount; i++) { |
3056 | 186 | axisList.emplace_back( |
3057 | 186 | buildAxis(parentNode->GP()->lookForChild(WKTConstants::AXIS, i), |
3058 | 186 | unit, unitType, isGeocentric, i + 1)); |
3059 | 186 | } |
3060 | | |
3061 | 185 | const PropertyMap &csMap = emptyPropertyMap; |
3062 | 185 | if (ci_equal(csType, EllipsoidalCS::WKT2_TYPE)) { |
3063 | 0 | if (axisCount == 2) { |
3064 | 0 | return EllipsoidalCS::create(csMap, axisList[0], axisList[1]); |
3065 | 0 | } else if (axisCount == 3) { |
3066 | 0 | return EllipsoidalCS::create(csMap, axisList[0], axisList[1], |
3067 | 0 | axisList[2]); |
3068 | 0 | } |
3069 | 185 | } else if (ci_equal(csType, CartesianCS::WKT2_TYPE)) { |
3070 | 4 | if (axisCount == 2) { |
3071 | 0 | return CartesianCS::create(csMap, axisList[0], axisList[1]); |
3072 | 4 | } else if (axisCount == 3) { |
3073 | 0 | return CartesianCS::create(csMap, axisList[0], axisList[1], |
3074 | 0 | axisList[2]); |
3075 | 0 | } |
3076 | 181 | } else if (ci_equal(csType, AffineCS::WKT2_TYPE)) { |
3077 | 0 | if (axisCount == 2) { |
3078 | 0 | return AffineCS::create(csMap, axisList[0], axisList[1]); |
3079 | 0 | } else if (axisCount == 3) { |
3080 | 0 | return AffineCS::create(csMap, axisList[0], axisList[1], |
3081 | 0 | axisList[2]); |
3082 | 0 | } |
3083 | 181 | } else if (ci_equal(csType, VerticalCS::WKT2_TYPE)) { |
3084 | 62 | if (axisCount == 1) { |
3085 | 62 | return VerticalCS::create(csMap, axisList[0]); |
3086 | 62 | } |
3087 | 119 | } else if (ci_equal(csType, SphericalCS::WKT2_TYPE)) { |
3088 | 0 | if (axisCount == 2) { |
3089 | | // Extension to ISO19111 to support (planet)-ocentric CS with |
3090 | | // geocentric latitude |
3091 | 0 | return SphericalCS::create(csMap, axisList[0], axisList[1]); |
3092 | 0 | } else if (axisCount == 3) { |
3093 | 0 | return SphericalCS::create(csMap, axisList[0], axisList[1], |
3094 | 0 | axisList[2]); |
3095 | 0 | } |
3096 | 119 | } else if (ci_equal(csType, OrdinalCS::WKT2_TYPE)) { // WKT2-2019 |
3097 | 0 | return OrdinalCS::create(csMap, axisList); |
3098 | 119 | } else if (ci_equal(csType, ParametricCS::WKT2_TYPE)) { |
3099 | 0 | if (axisCount == 1) { |
3100 | 0 | return ParametricCS::create(csMap, axisList[0]); |
3101 | 0 | } |
3102 | 119 | } else if (ci_equal(csType, TemporalCS::WKT2_2015_TYPE)) { |
3103 | 0 | if (axisCount == 1) { |
3104 | 0 | if (isNull( |
3105 | 0 | parentNode->GP()->lookForChild(WKTConstants::TIMEUNIT)) && |
3106 | 0 | isNull(parentNode->GP()->lookForChild(WKTConstants::UNIT))) { |
3107 | 0 | return DateTimeTemporalCS::create(csMap, axisList[0]); |
3108 | 0 | } else { |
3109 | | // Default to TemporalMeasureCS |
3110 | | // TemporalCount could also be possible |
3111 | 0 | return TemporalMeasureCS::create(csMap, axisList[0]); |
3112 | 0 | } |
3113 | 0 | } |
3114 | 119 | } else if (ci_equal(csType, DateTimeTemporalCS::WKT2_2019_TYPE)) { |
3115 | 0 | if (axisCount == 1) { |
3116 | 0 | return DateTimeTemporalCS::create(csMap, axisList[0]); |
3117 | 0 | } |
3118 | 119 | } else if (ci_equal(csType, TemporalCountCS::WKT2_2019_TYPE)) { |
3119 | 0 | if (axisCount == 1) { |
3120 | 0 | return TemporalCountCS::create(csMap, axisList[0]); |
3121 | 0 | } |
3122 | 119 | } else if (ci_equal(csType, TemporalMeasureCS::WKT2_2019_TYPE)) { |
3123 | 0 | if (axisCount == 1) { |
3124 | 0 | return TemporalMeasureCS::create(csMap, axisList[0]); |
3125 | 0 | } |
3126 | 119 | } else { |
3127 | 119 | throw ParsingException(concat("unhandled CS type: ", csType)); |
3128 | 119 | } |
3129 | 4 | throw buildParsingExceptionInvalidAxisCount(csType); |
3130 | 185 | } |
3131 | | |
3132 | | // --------------------------------------------------------------------------- |
3133 | | |
3134 | | std::string |
3135 | 2.30k | WKTParser::Private::getExtensionProj4(const WKTNode::Private *nodeP) { |
3136 | 2.30k | auto &extensionNode = nodeP->lookForChild(WKTConstants::EXTENSION); |
3137 | 2.30k | const auto &extensionChildren = extensionNode->GP()->children(); |
3138 | 2.30k | if (extensionChildren.size() == 2) { |
3139 | 157 | if (ci_equal(stripQuotes(extensionChildren[0]), "PROJ4")) { |
3140 | 148 | return stripQuotes(extensionChildren[1]); |
3141 | 148 | } |
3142 | 157 | } |
3143 | 2.15k | return std::string(); |
3144 | 2.30k | } |
3145 | | |
3146 | | // --------------------------------------------------------------------------- |
3147 | | |
3148 | | void WKTParser::Private::addExtensionProj4ToProp(const WKTNode::Private *nodeP, |
3149 | 1.56k | PropertyMap &props) { |
3150 | 1.56k | const auto extensionProj4(getExtensionProj4(nodeP)); |
3151 | 1.56k | if (!extensionProj4.empty()) { |
3152 | 45 | props.set("EXTENSION_PROJ4", extensionProj4); |
3153 | 45 | } |
3154 | 1.56k | } |
3155 | | |
3156 | | // --------------------------------------------------------------------------- |
3157 | | |
3158 | | GeodeticCRSNNPtr |
3159 | | WKTParser::Private::buildGeodeticCRS(const WKTNodeNNPtr &node, |
3160 | 1.16k | bool forceGeocentricIfNoCs) { |
3161 | 1.16k | const auto *nodeP = node->GP(); |
3162 | 1.16k | auto &datumNode = nodeP->lookForChild( |
3163 | 1.16k | WKTConstants::DATUM, WKTConstants::GEODETICDATUM, WKTConstants::TRF); |
3164 | 1.16k | auto &ensembleNode = nodeP->lookForChild(WKTConstants::ENSEMBLE); |
3165 | 1.16k | if (isNull(datumNode) && isNull(ensembleNode)) { |
3166 | 10 | throw ParsingException("Missing DATUM or ENSEMBLE node"); |
3167 | 10 | } |
3168 | | |
3169 | | // Do that now so that esriStyle_ can be set before buildPrimeMeridian() |
3170 | 1.15k | auto props = buildProperties(node); |
3171 | | |
3172 | 1.15k | auto &dynamicNode = nodeP->lookForChild(WKTConstants::DYNAMIC); |
3173 | | |
3174 | 1.15k | auto &csNode = nodeP->lookForChild(WKTConstants::CS_); |
3175 | 1.15k | const auto &nodeName = nodeP->value(); |
3176 | 1.15k | if (isNull(csNode) && !ci_equal(nodeName, WKTConstants::GEOGCS) && |
3177 | 1.15k | !ci_equal(nodeName, WKTConstants::GEOCCS) && |
3178 | 1.15k | !ci_equal(nodeName, WKTConstants::BASEGEODCRS) && |
3179 | 1.15k | !ci_equal(nodeName, WKTConstants::BASEGEOGCRS)) { |
3180 | 2 | ThrowMissing(WKTConstants::CS_); |
3181 | 2 | } |
3182 | | |
3183 | 1.15k | auto &primeMeridianNode = |
3184 | 1.15k | nodeP->lookForChild(WKTConstants::PRIMEM, WKTConstants::PRIMEMERIDIAN); |
3185 | 1.15k | if (isNull(primeMeridianNode)) { |
3186 | | // PRIMEM is required in WKT1 |
3187 | 844 | if (ci_equal(nodeName, WKTConstants::GEOGCS) || |
3188 | 844 | ci_equal(nodeName, WKTConstants::GEOCCS)) { |
3189 | 844 | emitRecoverableWarning(nodeName + " should have a PRIMEM node"); |
3190 | 844 | } |
3191 | 844 | } |
3192 | | |
3193 | 1.15k | auto angularUnit = |
3194 | 1.15k | buildUnitInSubNode(node, ci_equal(nodeName, WKTConstants::GEOGCS) |
3195 | 1.15k | ? UnitOfMeasure::Type::ANGULAR |
3196 | 1.15k | : UnitOfMeasure::Type::UNKNOWN); |
3197 | 1.15k | if (angularUnit.type() != UnitOfMeasure::Type::ANGULAR) { |
3198 | 690 | angularUnit = UnitOfMeasure::NONE; |
3199 | 690 | } |
3200 | | |
3201 | 1.15k | auto primeMeridian = |
3202 | 1.15k | !isNull(primeMeridianNode) |
3203 | 1.15k | ? buildPrimeMeridian(primeMeridianNode, angularUnit) |
3204 | 1.15k | : PrimeMeridian::GREENWICH; |
3205 | 1.15k | if (angularUnit == UnitOfMeasure::NONE) { |
3206 | 690 | angularUnit = primeMeridian->longitude().unit(); |
3207 | 690 | } |
3208 | | |
3209 | 1.15k | addExtensionProj4ToProp(nodeP, props); |
3210 | | |
3211 | | // No explicit AXIS node ? (WKT1) |
3212 | 1.15k | if (isNull(nodeP->lookForChild(WKTConstants::AXIS))) { |
3213 | 1.08k | props.set("IMPLICIT_CS", true); |
3214 | 1.08k | } |
3215 | | |
3216 | 1.15k | const std::string crsName = stripQuotes(nodeP->children()[0]); |
3217 | 1.15k | if (esriStyle_ && dbContext_) { |
3218 | 302 | std::string outTableName; |
3219 | 302 | std::string authNameFromAlias; |
3220 | 302 | std::string codeFromAlias; |
3221 | 302 | auto authFactory = |
3222 | 302 | AuthorityFactory::create(NN_NO_CHECK(dbContext_), std::string()); |
3223 | 302 | auto officialName = authFactory->getOfficialNameFromAlias( |
3224 | 302 | crsName, "geodetic_crs", "ESRI", false, outTableName, |
3225 | 302 | authNameFromAlias, codeFromAlias); |
3226 | 302 | if (!officialName.empty()) { |
3227 | 235 | props.set(IdentifiedObject::NAME_KEY, officialName); |
3228 | 235 | } |
3229 | 302 | } |
3230 | | |
3231 | 1.15k | auto datum = |
3232 | 1.15k | !isNull(datumNode) |
3233 | 1.15k | ? buildGeodeticReferenceFrame(datumNode, primeMeridian, dynamicNode) |
3234 | 988 | .as_nullable() |
3235 | 1.15k | : nullptr; |
3236 | 1.15k | auto datumEnsemble = |
3237 | 1.15k | !isNull(ensembleNode) |
3238 | 1.15k | ? buildDatumEnsemble(ensembleNode, primeMeridian, true) |
3239 | 139 | .as_nullable() |
3240 | 1.15k | : nullptr; |
3241 | 1.15k | auto cs = buildCS(csNode, node, angularUnit); |
3242 | | |
3243 | | // If there's no CS[] node, typically for a BASEGEODCRS of a projected CRS, |
3244 | | // in a few rare cases, this might be a Geocentric CRS, and thus a |
3245 | | // Cartesian CS, and not the ellipsoidalCS we assumed above. The only way |
3246 | | // to figure that is to resolve the CRS from its code... |
3247 | 1.15k | if (isNull(csNode) && dbContext_ && |
3248 | 1.15k | ci_equal(nodeName, WKTConstants::BASEGEODCRS)) { |
3249 | 0 | const auto &nodeChildren = nodeP->children(); |
3250 | 0 | for (const auto &subNode : nodeChildren) { |
3251 | 0 | const auto &subNodeName(subNode->GP()->value()); |
3252 | 0 | if (ci_equal(subNodeName, WKTConstants::ID) || |
3253 | 0 | ci_equal(subNodeName, WKTConstants::AUTHORITY)) { |
3254 | 0 | auto id = buildId(node, subNode, true, false); |
3255 | 0 | if (id) { |
3256 | 0 | try { |
3257 | 0 | auto authFactory = AuthorityFactory::create( |
3258 | 0 | NN_NO_CHECK(dbContext_), *id->codeSpace()); |
3259 | 0 | auto dbCRS = authFactory->createGeodeticCRS(id->code()); |
3260 | 0 | cs = dbCRS->coordinateSystem(); |
3261 | 0 | } catch (const util::Exception &) { |
3262 | 0 | } |
3263 | 0 | } |
3264 | 0 | } |
3265 | 0 | } |
3266 | 0 | } |
3267 | 1.15k | if (forceGeocentricIfNoCs && isNull(csNode) && |
3268 | 1.15k | ci_equal(nodeName, WKTConstants::BASEGEODCRS)) { |
3269 | 0 | cs = cs::CartesianCS::createGeocentric(UnitOfMeasure::METRE); |
3270 | 0 | } |
3271 | | |
3272 | 1.15k | auto ellipsoidalCS = nn_dynamic_pointer_cast<EllipsoidalCS>(cs); |
3273 | 1.15k | if (ellipsoidalCS) { |
3274 | 865 | if (ci_equal(nodeName, WKTConstants::GEOCCS)) { |
3275 | 0 | throw ParsingException("ellipsoidal CS not expected in GEOCCS"); |
3276 | 0 | } |
3277 | 865 | try { |
3278 | 865 | auto crs = GeographicCRS::create(props, datum, datumEnsemble, |
3279 | 865 | NN_NO_CHECK(ellipsoidalCS)); |
3280 | | // In case of missing CS node, or to check it, query the coordinate |
3281 | | // system from the DB if possible (typically for the baseCRS of a |
3282 | | // ProjectedCRS) |
3283 | 865 | if (!crs->identifiers().empty() && dbContext_) { |
3284 | 6 | GeographicCRSPtr dbCRS; |
3285 | 6 | try { |
3286 | 6 | const auto &id = crs->identifiers()[0]; |
3287 | 6 | auto authFactory = AuthorityFactory::create( |
3288 | 6 | NN_NO_CHECK(dbContext_), *id->codeSpace()); |
3289 | 6 | dbCRS = authFactory->createGeographicCRS(id->code()) |
3290 | 6 | .as_nullable(); |
3291 | 6 | } catch (const util::Exception &) { |
3292 | 6 | } |
3293 | 6 | if (dbCRS && |
3294 | 6 | (!isNull(csNode) || |
3295 | 0 | node->countChildrenOfName(WKTConstants::AXIS) != 0) && |
3296 | 6 | !ellipsoidalCS->_isEquivalentTo( |
3297 | 0 | dbCRS->coordinateSystem().get(), |
3298 | 0 | util::IComparable::Criterion::EQUIVALENT)) { |
3299 | 0 | if (unsetIdentifiersIfIncompatibleDef_) { |
3300 | 0 | emitRecoverableWarning( |
3301 | 0 | "Coordinate system of GeographicCRS in the WKT " |
3302 | 0 | "definition is different from the one of the " |
3303 | 0 | "authority. Unsetting the identifier to avoid " |
3304 | 0 | "confusion"); |
3305 | 0 | props.unset(Identifier::CODESPACE_KEY); |
3306 | 0 | props.unset(Identifier::AUTHORITY_KEY); |
3307 | 0 | props.unset(IdentifiedObject::IDENTIFIERS_KEY); |
3308 | 0 | } |
3309 | 0 | crs = GeographicCRS::create(props, datum, datumEnsemble, |
3310 | 0 | NN_NO_CHECK(ellipsoidalCS)); |
3311 | 6 | } else if (dbCRS) { |
3312 | 0 | auto csFromDB = dbCRS->coordinateSystem(); |
3313 | 0 | auto csFromDBAltered = csFromDB; |
3314 | 0 | if (!isNull(nodeP->lookForChild(WKTConstants::UNIT))) { |
3315 | 0 | csFromDBAltered = |
3316 | 0 | csFromDB->alterAngularUnit(angularUnit); |
3317 | 0 | if (unsetIdentifiersIfIncompatibleDef_ && |
3318 | 0 | !csFromDBAltered->_isEquivalentTo( |
3319 | 0 | csFromDB.get(), |
3320 | 0 | util::IComparable::Criterion::EQUIVALENT)) { |
3321 | 0 | emitRecoverableWarning( |
3322 | 0 | "Coordinate system of GeographicCRS in the WKT " |
3323 | 0 | "definition is different from the one of the " |
3324 | 0 | "authority. Unsetting the identifier to avoid " |
3325 | 0 | "confusion"); |
3326 | 0 | props.unset(Identifier::CODESPACE_KEY); |
3327 | 0 | props.unset(Identifier::AUTHORITY_KEY); |
3328 | 0 | props.unset(IdentifiedObject::IDENTIFIERS_KEY); |
3329 | 0 | } |
3330 | 0 | } |
3331 | 0 | crs = GeographicCRS::create(props, datum, datumEnsemble, |
3332 | 0 | csFromDBAltered); |
3333 | 0 | } |
3334 | 6 | } |
3335 | 865 | return crs; |
3336 | 865 | } catch (const util::Exception &e) { |
3337 | 0 | throw ParsingException(std::string("buildGeodeticCRS: ") + |
3338 | 0 | e.what()); |
3339 | 0 | } |
3340 | 865 | } else if (ci_equal(nodeName, WKTConstants::GEOGCRS) || |
3341 | 289 | ci_equal(nodeName, WKTConstants::GEOGRAPHICCRS) || |
3342 | 289 | ci_equal(nodeName, WKTConstants::BASEGEOGCRS)) { |
3343 | | // This is a WKT2-2019 GeographicCRS. An ellipsoidal CS is expected |
3344 | 0 | throw ParsingException(concat("ellipsoidal CS expected, but found ", |
3345 | 0 | cs->getWKT2Type(true))); |
3346 | 0 | } |
3347 | | |
3348 | 289 | auto cartesianCS = nn_dynamic_pointer_cast<CartesianCS>(cs); |
3349 | 289 | if (cartesianCS) { |
3350 | 59 | if (cartesianCS->axisList().size() != 3) { |
3351 | 0 | throw ParsingException( |
3352 | 0 | "Cartesian CS for a GeodeticCRS should have 3 axis"); |
3353 | 0 | } |
3354 | 59 | try { |
3355 | 59 | return GeodeticCRS::create(props, datum, datumEnsemble, |
3356 | 59 | NN_NO_CHECK(cartesianCS)); |
3357 | 59 | } catch (const util::Exception &e) { |
3358 | 0 | throw ParsingException(std::string("buildGeodeticCRS: ") + |
3359 | 0 | e.what()); |
3360 | 0 | } |
3361 | 59 | } |
3362 | | |
3363 | 230 | auto sphericalCS = nn_dynamic_pointer_cast<SphericalCS>(cs); |
3364 | 230 | if (sphericalCS) { |
3365 | 0 | try { |
3366 | 0 | return GeodeticCRS::create(props, datum, datumEnsemble, |
3367 | 0 | NN_NO_CHECK(sphericalCS)); |
3368 | 0 | } catch (const util::Exception &e) { |
3369 | 0 | throw ParsingException(std::string("buildGeodeticCRS: ") + |
3370 | 0 | e.what()); |
3371 | 0 | } |
3372 | 0 | } |
3373 | | |
3374 | 230 | throw ParsingException( |
3375 | 230 | concat("unhandled CS type: ", cs->getWKT2Type(true))); |
3376 | 230 | } |
3377 | | |
3378 | | // --------------------------------------------------------------------------- |
3379 | | |
3380 | 4 | CRSNNPtr WKTParser::Private::buildDerivedGeodeticCRS(const WKTNodeNNPtr &node) { |
3381 | 4 | const auto *nodeP = node->GP(); |
3382 | 4 | auto &baseGeodCRSNode = nodeP->lookForChild(WKTConstants::BASEGEODCRS, |
3383 | 4 | WKTConstants::BASEGEOGCRS); |
3384 | | // given the constraints enforced on calling code path |
3385 | 4 | assert(!isNull(baseGeodCRSNode)); |
3386 | | |
3387 | 4 | auto &derivingConversionNode = |
3388 | 4 | nodeP->lookForChild(WKTConstants::DERIVINGCONVERSION); |
3389 | 4 | if (isNull(derivingConversionNode)) { |
3390 | 4 | ThrowMissing(WKTConstants::DERIVINGCONVERSION); |
3391 | 4 | } |
3392 | 0 | auto derivingConversion = buildConversion( |
3393 | 0 | derivingConversionNode, UnitOfMeasure::NONE, UnitOfMeasure::NONE); |
3394 | |
|
3395 | 0 | auto &csNode = nodeP->lookForChild(WKTConstants::CS_); |
3396 | 0 | if (isNull(csNode)) { |
3397 | 0 | ThrowMissing(WKTConstants::CS_); |
3398 | 0 | } |
3399 | 0 | auto cs = buildCS(csNode, node, UnitOfMeasure::NONE); |
3400 | |
|
3401 | 0 | bool forceGeocentricIfNoCs = false; |
3402 | 0 | auto cartesianCS = nn_dynamic_pointer_cast<CartesianCS>(cs); |
3403 | 0 | if (cartesianCS) { |
3404 | 0 | if (cartesianCS->axisList().size() != 3) { |
3405 | 0 | throw ParsingException( |
3406 | 0 | "Cartesian CS for a GeodeticCRS should have 3 axis"); |
3407 | 0 | } |
3408 | 0 | const int methodCode = derivingConversion->method()->getEPSGCode(); |
3409 | 0 | if ((methodCode == EPSG_CODE_METHOD_COORDINATE_FRAME_GEOCENTRIC || |
3410 | 0 | methodCode == |
3411 | 0 | EPSG_CODE_METHOD_COORDINATE_FRAME_FULL_MATRIX_GEOCENTRIC || |
3412 | 0 | methodCode == EPSG_CODE_METHOD_POSITION_VECTOR_GEOCENTRIC || |
3413 | 0 | methodCode == EPSG_CODE_METHOD_GEOCENTRIC_TRANSLATION_GEOCENTRIC || |
3414 | 0 | methodCode == |
3415 | 0 | EPSG_CODE_METHOD_TIME_DEPENDENT_POSITION_VECTOR_GEOCENTRIC || |
3416 | 0 | methodCode == |
3417 | 0 | EPSG_CODE_METHOD_TIME_DEPENDENT_COORDINATE_FRAME_GEOCENTRIC) && |
3418 | 0 | nodeP->lookForChild(WKTConstants::BASEGEODCRS) != nullptr) { |
3419 | 0 | forceGeocentricIfNoCs = true; |
3420 | 0 | } |
3421 | 0 | } |
3422 | 0 | auto baseGeodCRS = buildGeodeticCRS(baseGeodCRSNode, forceGeocentricIfNoCs); |
3423 | |
|
3424 | 0 | auto ellipsoidalCS = nn_dynamic_pointer_cast<EllipsoidalCS>(cs); |
3425 | 0 | if (ellipsoidalCS) { |
3426 | |
|
3427 | 0 | if (ellipsoidalCS->axisList().size() == 3 && |
3428 | 0 | baseGeodCRS->coordinateSystem()->axisList().size() == 2) { |
3429 | 0 | baseGeodCRS = |
3430 | 0 | NN_NO_CHECK(util::nn_dynamic_pointer_cast<GeodeticCRS>( |
3431 | 0 | baseGeodCRS->promoteTo3D(std::string(), dbContext_))); |
3432 | 0 | } |
3433 | |
|
3434 | 0 | return DerivedGeographicCRS::create(buildProperties(node), baseGeodCRS, |
3435 | 0 | derivingConversion, |
3436 | 0 | NN_NO_CHECK(ellipsoidalCS)); |
3437 | 0 | } else if (ci_equal(nodeP->value(), WKTConstants::GEOGCRS)) { |
3438 | | // This is a WKT2-2019 GeographicCRS. An ellipsoidal CS is expected |
3439 | 0 | throw ParsingException(concat("ellipsoidal CS expected, but found ", |
3440 | 0 | cs->getWKT2Type(true))); |
3441 | 0 | } |
3442 | | |
3443 | 0 | if (cartesianCS) { |
3444 | 0 | return DerivedGeodeticCRS::create(buildProperties(node), baseGeodCRS, |
3445 | 0 | derivingConversion, |
3446 | 0 | NN_NO_CHECK(cartesianCS)); |
3447 | 0 | } |
3448 | | |
3449 | 0 | auto sphericalCS = nn_dynamic_pointer_cast<SphericalCS>(cs); |
3450 | 0 | if (sphericalCS) { |
3451 | 0 | return DerivedGeodeticCRS::create(buildProperties(node), baseGeodCRS, |
3452 | 0 | derivingConversion, |
3453 | 0 | NN_NO_CHECK(sphericalCS)); |
3454 | 0 | } |
3455 | | |
3456 | 0 | throw ParsingException( |
3457 | 0 | concat("unhandled CS type: ", cs->getWKT2Type(true))); |
3458 | 0 | } |
3459 | | |
3460 | | // --------------------------------------------------------------------------- |
3461 | | |
3462 | | UnitOfMeasure WKTParser::Private::guessUnitForParameter( |
3463 | | const std::string ¶mName, const UnitOfMeasure &defaultLinearUnit, |
3464 | 2.85k | const UnitOfMeasure &defaultAngularUnit) { |
3465 | 2.85k | UnitOfMeasure unit; |
3466 | | // scale must be first because of 'Scale factor on pseudo standard parallel' |
3467 | 2.85k | if (ci_find(paramName, "scale") != std::string::npos || |
3468 | 2.85k | ci_find(paramName, "scaling factor") != std::string::npos) { |
3469 | 78 | unit = UnitOfMeasure::SCALE_UNITY; |
3470 | 2.77k | } else if (ci_find(paramName, "latitude") != std::string::npos || |
3471 | 2.77k | ci_find(paramName, "longitude") != std::string::npos || |
3472 | 2.77k | ci_find(paramName, "meridian") != std::string::npos || |
3473 | 2.77k | ci_find(paramName, "parallel") != std::string::npos || |
3474 | 2.77k | ci_find(paramName, "azimuth") != std::string::npos || |
3475 | 2.77k | ci_find(paramName, "angle") != std::string::npos || |
3476 | 2.77k | ci_find(paramName, "heading") != std::string::npos || |
3477 | 2.77k | ci_find(paramName, "rotation") != std::string::npos) { |
3478 | 725 | unit = defaultAngularUnit; |
3479 | 2.05k | } else if (ci_find(paramName, "easting") != std::string::npos || |
3480 | 2.05k | ci_find(paramName, "northing") != std::string::npos || |
3481 | 2.05k | ci_find(paramName, "height") != std::string::npos) { |
3482 | 643 | unit = defaultLinearUnit; |
3483 | 643 | } |
3484 | 2.85k | return unit; |
3485 | 2.85k | } |
3486 | | |
3487 | | // --------------------------------------------------------------------------- |
3488 | | |
3489 | | static bool |
3490 | 553 | isEPSGCodeForInterpolationParameter(const OperationParameterNNPtr ¶meter) { |
3491 | 553 | const auto &name = parameter->nameStr(); |
3492 | 553 | const auto epsgCode = parameter->getEPSGCode(); |
3493 | 553 | return name == EPSG_NAME_PARAMETER_EPSG_CODE_FOR_INTERPOLATION_CRS || |
3494 | 553 | epsgCode == EPSG_CODE_PARAMETER_EPSG_CODE_FOR_INTERPOLATION_CRS || |
3495 | 553 | name == EPSG_NAME_PARAMETER_EPSG_CODE_FOR_HORIZONTAL_CRS || |
3496 | 553 | epsgCode == EPSG_CODE_PARAMETER_EPSG_CODE_FOR_HORIZONTAL_CRS; |
3497 | 553 | } |
3498 | | |
3499 | | // --------------------------------------------------------------------------- |
3500 | | |
3501 | 300 | static bool isIntegerParameter(const OperationParameterNNPtr ¶meter) { |
3502 | 300 | return isEPSGCodeForInterpolationParameter(parameter); |
3503 | 300 | } |
3504 | | |
3505 | | // --------------------------------------------------------------------------- |
3506 | | |
3507 | | void WKTParser::Private::consumeParameters( |
3508 | | const WKTNodeNNPtr &node, bool isAbridged, |
3509 | | std::vector<OperationParameterNNPtr> ¶meters, |
3510 | | std::vector<ParameterValueNNPtr> &values, |
3511 | | const UnitOfMeasure &defaultLinearUnit, |
3512 | 315 | const UnitOfMeasure &defaultAngularUnit) { |
3513 | 1.93k | for (const auto &childNode : node->GP()->children()) { |
3514 | 1.93k | const auto &childNodeChildren = childNode->GP()->children(); |
3515 | 1.93k | if (ci_equal(childNode->GP()->value(), WKTConstants::PARAMETER)) { |
3516 | 481 | if (childNodeChildren.size() < 2) { |
3517 | 10 | ThrowNotEnoughChildren(childNode->GP()->value()); |
3518 | 10 | } |
3519 | 471 | parameters.push_back( |
3520 | 471 | OperationParameter::create(buildProperties(childNode))); |
3521 | 471 | const auto ¶mValue = childNodeChildren[1]->GP()->value(); |
3522 | 471 | if (!paramValue.empty() && paramValue[0] == '"') { |
3523 | 150 | values.push_back( |
3524 | 150 | ParameterValue::create(stripQuotes(childNodeChildren[1]))); |
3525 | 321 | } else { |
3526 | 321 | try { |
3527 | 321 | double val = asDouble(childNodeChildren[1]); |
3528 | 321 | auto unit = buildUnitInSubNode(childNode); |
3529 | 321 | if (unit == UnitOfMeasure::NONE) { |
3530 | 300 | const auto ¶mName = |
3531 | 300 | childNodeChildren[0]->GP()->value(); |
3532 | 300 | unit = guessUnitForParameter( |
3533 | 300 | paramName, defaultLinearUnit, defaultAngularUnit); |
3534 | 300 | } |
3535 | | |
3536 | 321 | if (isAbridged) { |
3537 | 0 | const auto ¶mName = parameters.back()->nameStr(); |
3538 | 0 | int paramEPSGCode = 0; |
3539 | 0 | const auto ¶mIds = parameters.back()->identifiers(); |
3540 | 0 | if (paramIds.size() == 1 && |
3541 | 0 | ci_equal(*(paramIds[0]->codeSpace()), |
3542 | 0 | Identifier::EPSG)) { |
3543 | 0 | paramEPSGCode = ::atoi(paramIds[0]->code().c_str()); |
3544 | 0 | } |
3545 | 0 | const common::UnitOfMeasure *pUnit = nullptr; |
3546 | 0 | if (OperationParameterValue::convertFromAbridged( |
3547 | 0 | paramName, val, pUnit, paramEPSGCode)) { |
3548 | 0 | unit = *pUnit; |
3549 | 0 | parameters.back() = OperationParameter::create( |
3550 | 0 | buildProperties(childNode) |
3551 | 0 | .set(Identifier::CODESPACE_KEY, |
3552 | 0 | Identifier::EPSG) |
3553 | 0 | .set(Identifier::CODE_KEY, paramEPSGCode)); |
3554 | 0 | } |
3555 | 0 | } |
3556 | | |
3557 | 321 | if (isIntegerParameter(parameters.back())) { |
3558 | 31 | values.push_back(ParameterValue::create( |
3559 | 31 | std::stoi(childNodeChildren[1]->GP()->value()))); |
3560 | 290 | } else { |
3561 | 290 | values.push_back( |
3562 | 290 | ParameterValue::create(Measure(val, unit))); |
3563 | 290 | } |
3564 | 321 | } catch (const std::exception &) { |
3565 | 26 | throw ParsingException(concat( |
3566 | 26 | "unhandled parameter value type : ", paramValue)); |
3567 | 26 | } |
3568 | 321 | } |
3569 | 1.45k | } else if (ci_equal(childNode->GP()->value(), |
3570 | 1.45k | WKTConstants::PARAMETERFILE)) { |
3571 | 39 | if (childNodeChildren.size() < 2) { |
3572 | 2 | ThrowNotEnoughChildren(childNode->GP()->value()); |
3573 | 2 | } |
3574 | 37 | parameters.push_back( |
3575 | 37 | OperationParameter::create(buildProperties(childNode))); |
3576 | 37 | values.push_back(ParameterValue::createFilename( |
3577 | 37 | stripQuotes(childNodeChildren[1]))); |
3578 | 37 | } |
3579 | 1.93k | } |
3580 | 315 | } |
3581 | | |
3582 | | // --------------------------------------------------------------------------- |
3583 | | |
3584 | | static CRSPtr dealWithEPSGCodeForInterpolationCRSParameter( |
3585 | | DatabaseContextPtr &dbContext, |
3586 | | std::vector<OperationParameterNNPtr> ¶meters, |
3587 | | std::vector<ParameterValueNNPtr> &values); |
3588 | | |
3589 | | ConversionNNPtr |
3590 | | WKTParser::Private::buildConversion(const WKTNodeNNPtr &node, |
3591 | | const UnitOfMeasure &defaultLinearUnit, |
3592 | 327 | const UnitOfMeasure &defaultAngularUnit) { |
3593 | 327 | auto &methodNode = node->GP()->lookForChild(WKTConstants::METHOD, |
3594 | 327 | WKTConstants::PROJECTION); |
3595 | 327 | if (isNull(methodNode)) { |
3596 | 10 | ThrowMissing(WKTConstants::METHOD); |
3597 | 10 | } |
3598 | 317 | if (methodNode->GP()->childrenSize() == 0) { |
3599 | 2 | ThrowNotEnoughChildren(WKTConstants::METHOD); |
3600 | 2 | } |
3601 | | |
3602 | 315 | std::vector<OperationParameterNNPtr> parameters; |
3603 | 315 | std::vector<ParameterValueNNPtr> values; |
3604 | 315 | consumeParameters(node, false, parameters, values, defaultLinearUnit, |
3605 | 315 | defaultAngularUnit); |
3606 | | |
3607 | 315 | auto interpolationCRS = dealWithEPSGCodeForInterpolationCRSParameter( |
3608 | 315 | dbContext_, parameters, values); |
3609 | | |
3610 | 315 | auto &convProps = buildProperties(node); |
3611 | 315 | auto &methodProps = buildProperties(methodNode); |
3612 | 315 | std::string convName; |
3613 | 315 | std::string methodName; |
3614 | 315 | if (convProps.getStringValue(IdentifiedObject::NAME_KEY, convName) && |
3615 | 315 | methodProps.getStringValue(IdentifiedObject::NAME_KEY, methodName) && |
3616 | 315 | starts_with(convName, "Inverse of ") && |
3617 | 315 | starts_with(methodName, "Inverse of ")) { |
3618 | | |
3619 | 35 | auto &invConvProps = buildProperties(node, true); |
3620 | 35 | auto &invMethodProps = buildProperties(methodNode, true); |
3621 | 35 | auto conv = NN_NO_CHECK(util::nn_dynamic_pointer_cast<Conversion>( |
3622 | 35 | Conversion::create(invConvProps, invMethodProps, parameters, values) |
3623 | 35 | ->inverse())); |
3624 | 35 | if (interpolationCRS) |
3625 | 0 | conv->setInterpolationCRS(interpolationCRS); |
3626 | 35 | return conv; |
3627 | 35 | } |
3628 | 280 | auto conv = Conversion::create(convProps, methodProps, parameters, values); |
3629 | 280 | if (interpolationCRS) |
3630 | 0 | conv->setInterpolationCRS(interpolationCRS); |
3631 | 280 | return conv; |
3632 | 315 | } |
3633 | | |
3634 | | // --------------------------------------------------------------------------- |
3635 | | |
3636 | | static CRSPtr dealWithEPSGCodeForInterpolationCRSParameter( |
3637 | | DatabaseContextPtr &dbContext, |
3638 | | std::vector<OperationParameterNNPtr> ¶meters, |
3639 | 277 | std::vector<ParameterValueNNPtr> &values) { |
3640 | | // Transform EPSG hacky PARAMETER["EPSG code for Interpolation CRS", |
3641 | | // crs_epsg_code] into proper interpolation CRS |
3642 | 277 | if (dbContext != nullptr) { |
3643 | 510 | for (size_t i = 0; i < parameters.size(); ++i) { |
3644 | 253 | if (isEPSGCodeForInterpolationParameter(parameters[i])) { |
3645 | 6 | const int code = values[i]->integerValue(); |
3646 | 6 | try { |
3647 | 6 | auto authFactory = AuthorityFactory::create( |
3648 | 6 | NN_NO_CHECK(dbContext), Identifier::EPSG); |
3649 | 6 | auto interpolationCRS = |
3650 | 6 | authFactory |
3651 | 6 | ->createGeographicCRS(internal::toString(code)) |
3652 | 6 | .as_nullable(); |
3653 | 6 | parameters.erase(parameters.begin() + i); |
3654 | 6 | values.erase(values.begin() + i); |
3655 | 6 | return interpolationCRS; |
3656 | 6 | } catch (const util::Exception &) { |
3657 | 6 | } |
3658 | 6 | } |
3659 | 253 | } |
3660 | 257 | } |
3661 | 277 | return nullptr; |
3662 | 277 | } |
3663 | | |
3664 | | // --------------------------------------------------------------------------- |
3665 | | |
3666 | | TransformationNNPtr |
3667 | 15 | WKTParser::Private::buildCoordinateOperation(const WKTNodeNNPtr &node) { |
3668 | 15 | const auto *nodeP = node->GP(); |
3669 | 15 | auto &methodNode = nodeP->lookForChild(WKTConstants::METHOD); |
3670 | 15 | if (isNull(methodNode)) { |
3671 | 5 | ThrowMissing(WKTConstants::METHOD); |
3672 | 5 | } |
3673 | 10 | if (methodNode->GP()->childrenSize() == 0) { |
3674 | 3 | ThrowNotEnoughChildren(WKTConstants::METHOD); |
3675 | 3 | } |
3676 | | |
3677 | 7 | auto &sourceCRSNode = nodeP->lookForChild(WKTConstants::SOURCECRS); |
3678 | 7 | if (/*isNull(sourceCRSNode) ||*/ sourceCRSNode->GP()->childrenSize() != 1) { |
3679 | 7 | ThrowMissing(WKTConstants::SOURCECRS); |
3680 | 7 | } |
3681 | 0 | auto sourceCRS = buildCRS(sourceCRSNode->GP()->children()[0]); |
3682 | 0 | if (!sourceCRS) { |
3683 | 0 | throw ParsingException("Invalid content in SOURCECRS node"); |
3684 | 0 | } |
3685 | | |
3686 | 0 | auto &targetCRSNode = nodeP->lookForChild(WKTConstants::TARGETCRS); |
3687 | 0 | if (/*isNull(targetCRSNode) ||*/ targetCRSNode->GP()->childrenSize() != 1) { |
3688 | 0 | ThrowMissing(WKTConstants::TARGETCRS); |
3689 | 0 | } |
3690 | 0 | auto targetCRS = buildCRS(targetCRSNode->GP()->children()[0]); |
3691 | 0 | if (!targetCRS) { |
3692 | 0 | throw ParsingException("Invalid content in TARGETCRS node"); |
3693 | 0 | } |
3694 | | |
3695 | 0 | auto &interpolationCRSNode = |
3696 | 0 | nodeP->lookForChild(WKTConstants::INTERPOLATIONCRS); |
3697 | 0 | CRSPtr interpolationCRS; |
3698 | 0 | if (/*!isNull(interpolationCRSNode) && */ interpolationCRSNode->GP() |
3699 | 0 | ->childrenSize() == 1) { |
3700 | 0 | interpolationCRS = buildCRS(interpolationCRSNode->GP()->children()[0]); |
3701 | 0 | } |
3702 | |
|
3703 | 0 | std::vector<OperationParameterNNPtr> parameters; |
3704 | 0 | std::vector<ParameterValueNNPtr> values; |
3705 | 0 | const auto &defaultLinearUnit = UnitOfMeasure::NONE; |
3706 | 0 | const auto &defaultAngularUnit = UnitOfMeasure::NONE; |
3707 | 0 | consumeParameters(node, false, parameters, values, defaultLinearUnit, |
3708 | 0 | defaultAngularUnit); |
3709 | |
|
3710 | 0 | if (interpolationCRS == nullptr) |
3711 | 0 | interpolationCRS = dealWithEPSGCodeForInterpolationCRSParameter( |
3712 | 0 | dbContext_, parameters, values); |
3713 | |
|
3714 | 0 | std::vector<PositionalAccuracyNNPtr> accuracies; |
3715 | 0 | auto &accuracyNode = nodeP->lookForChild(WKTConstants::OPERATIONACCURACY); |
3716 | 0 | if (/*!isNull(accuracyNode) && */ accuracyNode->GP()->childrenSize() == 1) { |
3717 | 0 | accuracies.push_back(PositionalAccuracy::create( |
3718 | 0 | stripQuotes(accuracyNode->GP()->children()[0]))); |
3719 | 0 | } |
3720 | |
|
3721 | 0 | return Transformation::create(buildProperties(node), NN_NO_CHECK(sourceCRS), |
3722 | 0 | NN_NO_CHECK(targetCRS), interpolationCRS, |
3723 | 0 | buildProperties(methodNode), parameters, |
3724 | 0 | values, accuracies); |
3725 | 0 | } |
3726 | | |
3727 | | // --------------------------------------------------------------------------- |
3728 | | |
3729 | | PointMotionOperationNNPtr |
3730 | 2 | WKTParser::Private::buildPointMotionOperation(const WKTNodeNNPtr &node) { |
3731 | 2 | const auto *nodeP = node->GP(); |
3732 | 2 | auto &methodNode = nodeP->lookForChild(WKTConstants::METHOD); |
3733 | 2 | if (isNull(methodNode)) { |
3734 | 2 | ThrowMissing(WKTConstants::METHOD); |
3735 | 2 | } |
3736 | 0 | if (methodNode->GP()->childrenSize() == 0) { |
3737 | 0 | ThrowNotEnoughChildren(WKTConstants::METHOD); |
3738 | 0 | } |
3739 | | |
3740 | 0 | auto &sourceCRSNode = nodeP->lookForChild(WKTConstants::SOURCECRS); |
3741 | 0 | if (sourceCRSNode->GP()->childrenSize() != 1) { |
3742 | 0 | ThrowMissing(WKTConstants::SOURCECRS); |
3743 | 0 | } |
3744 | 0 | auto sourceCRS = buildCRS(sourceCRSNode->GP()->children()[0]); |
3745 | 0 | if (!sourceCRS) { |
3746 | 0 | throw ParsingException("Invalid content in SOURCECRS node"); |
3747 | 0 | } |
3748 | | |
3749 | 0 | std::vector<OperationParameterNNPtr> parameters; |
3750 | 0 | std::vector<ParameterValueNNPtr> values; |
3751 | 0 | const auto &defaultLinearUnit = UnitOfMeasure::NONE; |
3752 | 0 | const auto &defaultAngularUnit = UnitOfMeasure::NONE; |
3753 | 0 | consumeParameters(node, false, parameters, values, defaultLinearUnit, |
3754 | 0 | defaultAngularUnit); |
3755 | |
|
3756 | 0 | std::vector<PositionalAccuracyNNPtr> accuracies; |
3757 | 0 | auto &accuracyNode = nodeP->lookForChild(WKTConstants::OPERATIONACCURACY); |
3758 | 0 | if (/*!isNull(accuracyNode) && */ accuracyNode->GP()->childrenSize() == 1) { |
3759 | 0 | accuracies.push_back(PositionalAccuracy::create( |
3760 | 0 | stripQuotes(accuracyNode->GP()->children()[0]))); |
3761 | 0 | } |
3762 | |
|
3763 | 0 | return PointMotionOperation::create( |
3764 | 0 | buildProperties(node), NN_NO_CHECK(sourceCRS), |
3765 | 0 | buildProperties(methodNode), parameters, values, accuracies); |
3766 | 0 | } |
3767 | | |
3768 | | // --------------------------------------------------------------------------- |
3769 | | |
3770 | | ConcatenatedOperationNNPtr |
3771 | 4 | WKTParser::Private::buildConcatenatedOperation(const WKTNodeNNPtr &node) { |
3772 | | |
3773 | 4 | const auto *nodeP = node->GP(); |
3774 | 4 | auto &sourceCRSNode = nodeP->lookForChild(WKTConstants::SOURCECRS); |
3775 | 4 | if (/*isNull(sourceCRSNode) ||*/ sourceCRSNode->GP()->childrenSize() != 1) { |
3776 | 4 | ThrowMissing(WKTConstants::SOURCECRS); |
3777 | 4 | } |
3778 | 0 | auto sourceCRS = buildCRS(sourceCRSNode->GP()->children()[0]); |
3779 | 0 | if (!sourceCRS) { |
3780 | 0 | throw ParsingException("Invalid content in SOURCECRS node"); |
3781 | 0 | } |
3782 | | |
3783 | 0 | auto &targetCRSNode = nodeP->lookForChild(WKTConstants::TARGETCRS); |
3784 | 0 | if (/*isNull(targetCRSNode) ||*/ targetCRSNode->GP()->childrenSize() != 1) { |
3785 | 0 | ThrowMissing(WKTConstants::TARGETCRS); |
3786 | 0 | } |
3787 | 0 | auto targetCRS = buildCRS(targetCRSNode->GP()->children()[0]); |
3788 | 0 | if (!targetCRS) { |
3789 | 0 | throw ParsingException("Invalid content in TARGETCRS node"); |
3790 | 0 | } |
3791 | | |
3792 | 0 | std::vector<CoordinateOperationNNPtr> operations; |
3793 | 0 | for (const auto &childNode : nodeP->children()) { |
3794 | 0 | if (ci_equal(childNode->GP()->value(), WKTConstants::STEP)) { |
3795 | 0 | if (childNode->GP()->childrenSize() != 1) { |
3796 | 0 | throw ParsingException("Invalid content in STEP node"); |
3797 | 0 | } |
3798 | 0 | auto op = nn_dynamic_pointer_cast<CoordinateOperation>( |
3799 | 0 | build(childNode->GP()->children()[0])); |
3800 | 0 | if (!op) { |
3801 | 0 | throw ParsingException("Invalid content in STEP node"); |
3802 | 0 | } |
3803 | 0 | operations.emplace_back(NN_NO_CHECK(op)); |
3804 | 0 | } |
3805 | 0 | } |
3806 | | |
3807 | 0 | ConcatenatedOperation::fixSteps( |
3808 | 0 | NN_NO_CHECK(sourceCRS), NN_NO_CHECK(targetCRS), operations, dbContext_, |
3809 | 0 | /* fixDirectionAllowed = */ true); |
3810 | |
|
3811 | 0 | std::vector<PositionalAccuracyNNPtr> accuracies; |
3812 | 0 | auto &accuracyNode = nodeP->lookForChild(WKTConstants::OPERATIONACCURACY); |
3813 | 0 | if (/*!isNull(accuracyNode) && */ accuracyNode->GP()->childrenSize() == 1) { |
3814 | 0 | accuracies.push_back(PositionalAccuracy::create( |
3815 | 0 | stripQuotes(accuracyNode->GP()->children()[0]))); |
3816 | 0 | } |
3817 | |
|
3818 | 0 | try { |
3819 | 0 | return ConcatenatedOperation::create(buildProperties(node), operations, |
3820 | 0 | accuracies); |
3821 | 0 | } catch (const InvalidOperation &e) { |
3822 | 0 | throw ParsingException( |
3823 | 0 | std::string("Cannot build concatenated operation: ") + e.what()); |
3824 | 0 | } |
3825 | 0 | } |
3826 | | |
3827 | | // --------------------------------------------------------------------------- |
3828 | | |
3829 | | bool WKTParser::Private::hasWebMercPROJ4String( |
3830 | 613 | const WKTNodeNNPtr &projCRSNode, const WKTNodeNNPtr &projectionNode) { |
3831 | 613 | if (projectionNode->GP()->childrenSize() == 0) { |
3832 | 2 | ThrowNotEnoughChildren(WKTConstants::PROJECTION); |
3833 | 2 | } |
3834 | 611 | const std::string wkt1ProjectionName = |
3835 | 611 | stripQuotes(projectionNode->GP()->children()[0]); |
3836 | | |
3837 | 611 | auto &extensionNode = projCRSNode->lookForChild(WKTConstants::EXTENSION); |
3838 | | |
3839 | 611 | if (metadata::Identifier::isEquivalentName(wkt1ProjectionName.c_str(), |
3840 | 611 | "Mercator_1SP") && |
3841 | 611 | projCRSNode->countChildrenOfName("center_latitude") == 0) { |
3842 | | |
3843 | | // Hack to detect the hacky way of encodign webmerc in GDAL WKT1 |
3844 | | // with a EXTENSION["PROJ4", "+proj=merc +a=6378137 +b=6378137 |
3845 | | // +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m |
3846 | | // +nadgrids=@null +wktext +no_defs"] node |
3847 | 0 | if (extensionNode && extensionNode->GP()->childrenSize() == 2 && |
3848 | 0 | ci_equal(stripQuotes(extensionNode->GP()->children()[0]), |
3849 | 0 | "PROJ4")) { |
3850 | 0 | std::string projString = |
3851 | 0 | stripQuotes(extensionNode->GP()->children()[1]); |
3852 | 0 | if (projString.find("+proj=merc") != std::string::npos && |
3853 | 0 | projString.find("+a=6378137") != std::string::npos && |
3854 | 0 | projString.find("+b=6378137") != std::string::npos && |
3855 | 0 | projString.find("+lon_0=0") != std::string::npos && |
3856 | 0 | projString.find("+x_0=0") != std::string::npos && |
3857 | 0 | projString.find("+y_0=0") != std::string::npos && |
3858 | 0 | projString.find("+nadgrids=@null") != std::string::npos && |
3859 | 0 | (projString.find("+lat_ts=") == std::string::npos || |
3860 | 0 | projString.find("+lat_ts=0") != std::string::npos) && |
3861 | 0 | (projString.find("+k=") == std::string::npos || |
3862 | 0 | projString.find("+k=1") != std::string::npos) && |
3863 | 0 | (projString.find("+units=") == std::string::npos || |
3864 | 0 | projString.find("+units=m") != std::string::npos)) { |
3865 | 0 | return true; |
3866 | 0 | } |
3867 | 0 | } |
3868 | 0 | } |
3869 | 611 | return false; |
3870 | 611 | } |
3871 | | |
3872 | | // --------------------------------------------------------------------------- |
3873 | | |
3874 | | static const MethodMapping * |
3875 | | selectSphericalOrEllipsoidal(const MethodMapping *mapping, |
3876 | 4.14k | const GeodeticCRSNNPtr &baseGeodCRS) { |
3877 | 4.14k | if (mapping->epsg_code == |
3878 | 4.14k | EPSG_CODE_METHOD_LAMBERT_CYLINDRICAL_EQUAL_AREA_SPHERICAL || |
3879 | 4.14k | mapping->epsg_code == EPSG_CODE_METHOD_LAMBERT_CYLINDRICAL_EQUAL_AREA) { |
3880 | 67 | mapping = getMapping( |
3881 | 67 | baseGeodCRS->ellipsoid()->isSphere() |
3882 | 67 | ? EPSG_CODE_METHOD_LAMBERT_CYLINDRICAL_EQUAL_AREA_SPHERICAL |
3883 | 67 | : EPSG_CODE_METHOD_LAMBERT_CYLINDRICAL_EQUAL_AREA); |
3884 | 4.08k | } else if (mapping->epsg_code == |
3885 | 4.08k | EPSG_CODE_METHOD_LAMBERT_AZIMUTHAL_EQUAL_AREA_SPHERICAL || |
3886 | 4.08k | mapping->epsg_code == |
3887 | 4.08k | EPSG_CODE_METHOD_LAMBERT_AZIMUTHAL_EQUAL_AREA) { |
3888 | 194 | mapping = getMapping( |
3889 | 194 | baseGeodCRS->ellipsoid()->isSphere() |
3890 | 194 | ? EPSG_CODE_METHOD_LAMBERT_AZIMUTHAL_EQUAL_AREA_SPHERICAL |
3891 | 194 | : EPSG_CODE_METHOD_LAMBERT_AZIMUTHAL_EQUAL_AREA); |
3892 | 3.88k | } else if (mapping->epsg_code == |
3893 | 3.88k | EPSG_CODE_METHOD_EQUIDISTANT_CYLINDRICAL_SPHERICAL || |
3894 | 3.88k | mapping->epsg_code == EPSG_CODE_METHOD_EQUIDISTANT_CYLINDRICAL) { |
3895 | 59 | mapping = |
3896 | 59 | getMapping(baseGeodCRS->ellipsoid()->isSphere() |
3897 | 59 | ? EPSG_CODE_METHOD_EQUIDISTANT_CYLINDRICAL_SPHERICAL |
3898 | 59 | : EPSG_CODE_METHOD_EQUIDISTANT_CYLINDRICAL); |
3899 | 59 | } |
3900 | 4.14k | return mapping; |
3901 | 4.14k | } |
3902 | | |
3903 | | // --------------------------------------------------------------------------- |
3904 | | |
3905 | | const ESRIMethodMapping *WKTParser::Private::getESRIMapping( |
3906 | | const WKTNodeNNPtr &projCRSNode, const WKTNodeNNPtr &projectionNode, |
3907 | 819 | std::map<std::string, std::string, ci_less_struct> &mapParamNameToValue) { |
3908 | 819 | const std::string esriProjectionName = |
3909 | 819 | stripQuotes(projectionNode->GP()->children()[0]); |
3910 | | |
3911 | | // Lambert_Conformal_Conic or Krovak may map to different WKT2 methods |
3912 | | // depending |
3913 | | // on the parameters / their values |
3914 | 819 | const auto esriMappings = getMappingsFromESRI(esriProjectionName); |
3915 | 819 | if (esriMappings.empty()) { |
3916 | 327 | return nullptr; |
3917 | 327 | } |
3918 | | |
3919 | | // Build a map of present parameters |
3920 | 4.18k | for (const auto &childNode : projCRSNode->GP()->children()) { |
3921 | 4.18k | if (ci_equal(childNode->GP()->value(), WKTConstants::PARAMETER)) { |
3922 | 1.88k | const auto &childNodeChildren = childNode->GP()->children(); |
3923 | 1.88k | if (childNodeChildren.size() < 2) { |
3924 | 9 | ThrowNotEnoughChildren(WKTConstants::PARAMETER); |
3925 | 9 | } |
3926 | 1.87k | const std::string parameterName(stripQuotes(childNodeChildren[0])); |
3927 | 1.87k | const auto ¶mValue = childNodeChildren[1]->GP()->value(); |
3928 | 1.87k | mapParamNameToValue[parameterName] = paramValue; |
3929 | 1.87k | } |
3930 | 4.18k | } |
3931 | | |
3932 | | // Compare parameters present with the ones expected in the mapping |
3933 | 483 | const ESRIMethodMapping *esriMapping = nullptr; |
3934 | 483 | int bestMatchCount = -1; |
3935 | 767 | for (const auto &mapping : esriMappings) { |
3936 | 767 | int matchCount = 0; |
3937 | 767 | int unmatchCount = 0; |
3938 | 5.33k | for (const auto *param = mapping->params; param->esri_name; ++param) { |
3939 | 4.61k | auto iter = mapParamNameToValue.find(param->esri_name); |
3940 | 4.61k | if (iter != mapParamNameToValue.end()) { |
3941 | 1.25k | if (param->wkt2_name == nullptr) { |
3942 | 59 | bool ok = true; |
3943 | 59 | try { |
3944 | 59 | if (io::asDouble(param->fixed_value) == |
3945 | 59 | io::asDouble(iter->second)) { |
3946 | 15 | matchCount++; |
3947 | 44 | } else { |
3948 | 44 | ok = false; |
3949 | 44 | } |
3950 | 59 | } catch (const std::exception &) { |
3951 | 10 | ok = false; |
3952 | 10 | } |
3953 | 59 | if (!ok) { |
3954 | 44 | matchCount = -1; |
3955 | 44 | break; |
3956 | 44 | } |
3957 | 1.19k | } else { |
3958 | 1.19k | matchCount++; |
3959 | 1.19k | } |
3960 | 3.35k | } else if (param->is_fixed_value) { |
3961 | 109 | mapParamNameToValue[param->esri_name] = param->fixed_value; |
3962 | 3.24k | } else { |
3963 | 3.24k | unmatchCount++; |
3964 | 3.24k | } |
3965 | 4.61k | } |
3966 | 767 | if (matchCount > bestMatchCount && |
3967 | 767 | !(maybeEsriStyle_ && unmatchCount >= matchCount)) { |
3968 | 310 | esriMapping = mapping; |
3969 | 310 | bestMatchCount = matchCount; |
3970 | 310 | } |
3971 | 767 | } |
3972 | | |
3973 | 483 | return esriMapping; |
3974 | 483 | } |
3975 | | |
3976 | | // --------------------------------------------------------------------------- |
3977 | | |
3978 | | ConversionNNPtr WKTParser::Private::buildProjectionFromESRI( |
3979 | | const GeodeticCRSNNPtr &baseGeodCRS, const WKTNodeNNPtr &projCRSNode, |
3980 | | const WKTNodeNNPtr &projectionNode, const UnitOfMeasure &defaultLinearUnit, |
3981 | | const UnitOfMeasure &defaultAngularUnit, |
3982 | | const ESRIMethodMapping *esriMapping, |
3983 | 307 | std::map<std::string, std::string, ci_less_struct> &mapParamNameToValue) { |
3984 | 307 | std::map<std::string, const char *> mapWKT2NameToESRIName; |
3985 | 1.68k | for (const auto *param = esriMapping->params; param->esri_name; ++param) { |
3986 | 1.37k | if (param->wkt2_name) { |
3987 | 1.31k | mapWKT2NameToESRIName[param->wkt2_name] = param->esri_name; |
3988 | 1.31k | } |
3989 | 1.37k | } |
3990 | | |
3991 | 307 | const std::string esriProjectionName = |
3992 | 307 | stripQuotes(projectionNode->GP()->children()[0]); |
3993 | 307 | const char *projectionMethodWkt2Name = esriMapping->wkt2_name; |
3994 | 307 | if (ci_equal(esriProjectionName, "Krovak")) { |
3995 | 16 | const std::string projCRSName = |
3996 | 16 | stripQuotes(projCRSNode->GP()->children()[0]); |
3997 | 16 | if (projCRSName.find("_East_North") != std::string::npos) { |
3998 | 0 | projectionMethodWkt2Name = EPSG_NAME_METHOD_KROVAK_NORTH_ORIENTED; |
3999 | 0 | } |
4000 | 16 | } |
4001 | | |
4002 | 307 | const auto *wkt2_mapping = getMapping(projectionMethodWkt2Name); |
4003 | 307 | if (ci_equal(esriProjectionName, "Stereographic")) { |
4004 | 24 | try { |
4005 | 24 | const auto iterLatitudeOfOrigin = |
4006 | 24 | mapParamNameToValue.find("Latitude_Of_Origin"); |
4007 | 24 | if (iterLatitudeOfOrigin != mapParamNameToValue.end() && |
4008 | 24 | std::fabs(io::asDouble(iterLatitudeOfOrigin->second)) == 90.0) { |
4009 | 12 | wkt2_mapping = |
4010 | 12 | getMapping(EPSG_CODE_METHOD_POLAR_STEREOGRAPHIC_VARIANT_A); |
4011 | 12 | } |
4012 | 24 | } catch (const std::exception &) { |
4013 | 0 | } |
4014 | 24 | } |
4015 | 307 | assert(wkt2_mapping); |
4016 | | |
4017 | 307 | wkt2_mapping = selectSphericalOrEllipsoidal(wkt2_mapping, baseGeodCRS); |
4018 | | |
4019 | 307 | PropertyMap propertiesMethod; |
4020 | 307 | propertiesMethod.set(IdentifiedObject::NAME_KEY, wkt2_mapping->wkt2_name); |
4021 | 307 | if (wkt2_mapping->epsg_code != 0) { |
4022 | 136 | propertiesMethod.set(Identifier::CODE_KEY, wkt2_mapping->epsg_code); |
4023 | 136 | propertiesMethod.set(Identifier::CODESPACE_KEY, Identifier::EPSG); |
4024 | 136 | } |
4025 | | |
4026 | 307 | std::vector<OperationParameterNNPtr> parameters; |
4027 | 307 | std::vector<ParameterValueNNPtr> values; |
4028 | | |
4029 | 307 | if (wkt2_mapping->epsg_code == EPSG_CODE_METHOD_EQUIDISTANT_CYLINDRICAL && |
4030 | 307 | ci_equal(esriProjectionName, "Plate_Carree")) { |
4031 | | // Add a fixed Latitude of 1st parallel = 0 so as to have all |
4032 | | // parameters expected by Equidistant Cylindrical. |
4033 | 1 | mapWKT2NameToESRIName[EPSG_NAME_PARAMETER_LATITUDE_1ST_STD_PARALLEL] = |
4034 | 1 | "Standard_Parallel_1"; |
4035 | 1 | mapParamNameToValue["Standard_Parallel_1"] = "0"; |
4036 | 306 | } else if ((wkt2_mapping->epsg_code == |
4037 | 306 | EPSG_CODE_METHOD_HOTINE_OBLIQUE_MERCATOR_VARIANT_A || |
4038 | 306 | wkt2_mapping->epsg_code == |
4039 | 301 | EPSG_CODE_METHOD_HOTINE_OBLIQUE_MERCATOR_VARIANT_B) && |
4040 | 306 | !ci_equal(esriProjectionName, |
4041 | 5 | "Rectified_Skew_Orthomorphic_Natural_Origin") && |
4042 | 306 | !ci_equal(esriProjectionName, |
4043 | 0 | "Rectified_Skew_Orthomorphic_Center")) { |
4044 | | // ESRI WKT lacks the angle to skew grid |
4045 | | // Take it from the azimuth value |
4046 | 0 | mapWKT2NameToESRIName |
4047 | 0 | [EPSG_NAME_PARAMETER_ANGLE_RECTIFIED_TO_SKEW_GRID] = "Azimuth"; |
4048 | 0 | } |
4049 | | |
4050 | 1.57k | for (int i = 0; wkt2_mapping->params[i] != nullptr; i++) { |
4051 | 1.28k | const auto *paramMapping = wkt2_mapping->params[i]; |
4052 | | |
4053 | 1.28k | auto iter = mapWKT2NameToESRIName.find(paramMapping->wkt2_name); |
4054 | 1.28k | if (iter == mapWKT2NameToESRIName.end()) { |
4055 | 12 | continue; |
4056 | 12 | } |
4057 | 1.26k | const auto &esriParamName = iter->second; |
4058 | 1.26k | auto iter2 = mapParamNameToValue.find(esriParamName); |
4059 | 1.26k | auto mapParamNameToValueEnd = mapParamNameToValue.end(); |
4060 | 1.26k | if (iter2 == mapParamNameToValueEnd) { |
4061 | | // In case we don't find a direct match, try the aliases |
4062 | 153 | for (iter2 = mapParamNameToValue.begin(); |
4063 | 317 | iter2 != mapParamNameToValueEnd; ++iter2) { |
4064 | 169 | if (areEquivalentParameters(iter2->first, esriParamName)) { |
4065 | 5 | break; |
4066 | 5 | } |
4067 | 169 | } |
4068 | 153 | if (iter2 == mapParamNameToValueEnd) { |
4069 | 148 | continue; |
4070 | 148 | } |
4071 | 153 | } |
4072 | | |
4073 | 1.12k | PropertyMap propertiesParameter; |
4074 | 1.12k | propertiesParameter.set(IdentifiedObject::NAME_KEY, |
4075 | 1.12k | paramMapping->wkt2_name); |
4076 | 1.12k | if (paramMapping->epsg_code != 0) { |
4077 | 1.07k | propertiesParameter.set(Identifier::CODE_KEY, |
4078 | 1.07k | paramMapping->epsg_code); |
4079 | 1.07k | propertiesParameter.set(Identifier::CODESPACE_KEY, |
4080 | 1.07k | Identifier::EPSG); |
4081 | 1.07k | } |
4082 | 1.12k | parameters.push_back(OperationParameter::create(propertiesParameter)); |
4083 | | |
4084 | 1.12k | try { |
4085 | 1.12k | double val = io::asDouble(iter2->second); |
4086 | 1.12k | auto unit = guessUnitForParameter( |
4087 | 1.12k | paramMapping->wkt2_name, defaultLinearUnit, defaultAngularUnit); |
4088 | 1.12k | values.push_back(ParameterValue::create(Measure(val, unit))); |
4089 | 1.12k | } catch (const std::exception &) { |
4090 | 10 | throw ParsingException( |
4091 | 10 | concat("unhandled parameter value type : ", iter2->second)); |
4092 | 10 | } |
4093 | 1.12k | } |
4094 | | |
4095 | 297 | return Conversion::create( |
4096 | 297 | PropertyMap().set(IdentifiedObject::NAME_KEY, |
4097 | 297 | esriProjectionName == "Gauss_Kruger" |
4098 | 297 | ? "unnnamed (Gauss Kruger)" |
4099 | 297 | : "unnamed"), |
4100 | 297 | propertiesMethod, parameters, values) |
4101 | 297 | ->identify(); |
4102 | 307 | } |
4103 | | |
4104 | | // --------------------------------------------------------------------------- |
4105 | | |
4106 | | ConversionNNPtr WKTParser::Private::buildProjectionFromESRI( |
4107 | | const GeodeticCRSNNPtr &baseGeodCRS, const WKTNodeNNPtr &projCRSNode, |
4108 | | const WKTNodeNNPtr &projectionNode, const UnitOfMeasure &defaultLinearUnit, |
4109 | 585 | const UnitOfMeasure &defaultAngularUnit) { |
4110 | | |
4111 | 585 | std::map<std::string, std::string, ci_less_struct> mapParamNameToValue; |
4112 | 585 | const auto esriMapping = |
4113 | 585 | getESRIMapping(projCRSNode, projectionNode, mapParamNameToValue); |
4114 | 585 | if (esriMapping == nullptr) { |
4115 | 282 | return buildProjectionStandard(baseGeodCRS, projCRSNode, projectionNode, |
4116 | 282 | defaultLinearUnit, defaultAngularUnit); |
4117 | 282 | } |
4118 | | |
4119 | 303 | return buildProjectionFromESRI(baseGeodCRS, projCRSNode, projectionNode, |
4120 | 303 | defaultLinearUnit, defaultAngularUnit, |
4121 | 303 | esriMapping, mapParamNameToValue); |
4122 | 585 | } |
4123 | | |
4124 | | // --------------------------------------------------------------------------- |
4125 | | |
4126 | | ConversionNNPtr WKTParser::Private::buildProjection( |
4127 | | const GeodeticCRSNNPtr &baseGeodCRS, const WKTNodeNNPtr &projCRSNode, |
4128 | | const WKTNodeNNPtr &projectionNode, const UnitOfMeasure &defaultLinearUnit, |
4129 | 611 | const UnitOfMeasure &defaultAngularUnit) { |
4130 | 611 | if (projectionNode->GP()->childrenSize() == 0) { |
4131 | 0 | ThrowNotEnoughChildren(WKTConstants::PROJECTION); |
4132 | 0 | } |
4133 | 611 | if (esriStyle_ || maybeEsriStyle_) { |
4134 | 585 | return buildProjectionFromESRI(baseGeodCRS, projCRSNode, projectionNode, |
4135 | 585 | defaultLinearUnit, defaultAngularUnit); |
4136 | 585 | } |
4137 | 26 | return buildProjectionStandard(baseGeodCRS, projCRSNode, projectionNode, |
4138 | 26 | defaultLinearUnit, defaultAngularUnit); |
4139 | 611 | } |
4140 | | |
4141 | | // --------------------------------------------------------------------------- |
4142 | | |
4143 | | std::string |
4144 | | WKTParser::Private::projectionGetParameter(const WKTNodeNNPtr &projCRSNode, |
4145 | 0 | const char *paramName) { |
4146 | 0 | for (const auto &childNode : projCRSNode->GP()->children()) { |
4147 | 0 | if (ci_equal(childNode->GP()->value(), WKTConstants::PARAMETER)) { |
4148 | 0 | const auto &childNodeChildren = childNode->GP()->children(); |
4149 | 0 | if (childNodeChildren.size() == 2 && |
4150 | 0 | metadata::Identifier::isEquivalentName( |
4151 | 0 | stripQuotes(childNodeChildren[0]).c_str(), paramName)) { |
4152 | 0 | return childNodeChildren[1]->GP()->value(); |
4153 | 0 | } |
4154 | 0 | } |
4155 | 0 | } |
4156 | 0 | return std::string(); |
4157 | 0 | } |
4158 | | |
4159 | | // --------------------------------------------------------------------------- |
4160 | | |
4161 | | ConversionNNPtr WKTParser::Private::buildProjectionStandard( |
4162 | | const GeodeticCRSNNPtr &baseGeodCRS, const WKTNodeNNPtr &projCRSNode, |
4163 | | const WKTNodeNNPtr &projectionNode, const UnitOfMeasure &defaultLinearUnit, |
4164 | 308 | const UnitOfMeasure &defaultAngularUnit) { |
4165 | 308 | std::string wkt1ProjectionName = |
4166 | 308 | stripQuotes(projectionNode->GP()->children()[0]); |
4167 | | |
4168 | 308 | std::vector<OperationParameterNNPtr> parameters; |
4169 | 308 | std::vector<ParameterValueNNPtr> values; |
4170 | 308 | bool tryToIdentifyWKT1Method = true; |
4171 | | |
4172 | 308 | auto &extensionNode = projCRSNode->lookForChild(WKTConstants::EXTENSION); |
4173 | 308 | const auto &extensionChildren = extensionNode->GP()->children(); |
4174 | | |
4175 | 308 | bool gdal_3026_hack = false; |
4176 | 308 | if (metadata::Identifier::isEquivalentName(wkt1ProjectionName.c_str(), |
4177 | 308 | "Mercator_1SP") && |
4178 | 308 | projectionGetParameter(projCRSNode, "center_latitude").empty()) { |
4179 | | |
4180 | | // Hack for https://trac.osgeo.org/gdal/ticket/3026 |
4181 | 0 | std::string lat0( |
4182 | 0 | projectionGetParameter(projCRSNode, "latitude_of_origin")); |
4183 | 0 | if (!lat0.empty() && lat0 != "0" && lat0 != "0.0") { |
4184 | 0 | wkt1ProjectionName = "Mercator_2SP"; |
4185 | 0 | gdal_3026_hack = true; |
4186 | 0 | } else { |
4187 | | // The latitude of origin, which should always be zero, is |
4188 | | // missing |
4189 | | // in GDAL WKT1, but provisionned in the EPSG Mercator_1SP |
4190 | | // definition, |
4191 | | // so add it manually. |
4192 | 0 | PropertyMap propertiesParameter; |
4193 | 0 | propertiesParameter.set(IdentifiedObject::NAME_KEY, |
4194 | 0 | "Latitude of natural origin"); |
4195 | 0 | propertiesParameter.set(Identifier::CODE_KEY, 8801); |
4196 | 0 | propertiesParameter.set(Identifier::CODESPACE_KEY, |
4197 | 0 | Identifier::EPSG); |
4198 | 0 | parameters.push_back( |
4199 | 0 | OperationParameter::create(propertiesParameter)); |
4200 | 0 | values.push_back( |
4201 | 0 | ParameterValue::create(Measure(0, UnitOfMeasure::DEGREE))); |
4202 | 0 | } |
4203 | |
|
4204 | 308 | } else if (metadata::Identifier::isEquivalentName( |
4205 | 308 | wkt1ProjectionName.c_str(), "Polar_Stereographic")) { |
4206 | 75 | std::map<std::string, Measure> mapParameters; |
4207 | 1.56k | for (const auto &childNode : projCRSNode->GP()->children()) { |
4208 | 1.56k | const auto &childNodeChildren = childNode->GP()->children(); |
4209 | 1.56k | if (ci_equal(childNode->GP()->value(), WKTConstants::PARAMETER) && |
4210 | 1.56k | childNodeChildren.size() == 2) { |
4211 | 653 | const std::string wkt1ParameterName( |
4212 | 653 | stripQuotes(childNodeChildren[0])); |
4213 | 653 | try { |
4214 | 653 | double val = asDouble(childNodeChildren[1]); |
4215 | 653 | auto unit = guessUnitForParameter(wkt1ParameterName, |
4216 | 653 | defaultLinearUnit, |
4217 | 653 | defaultAngularUnit); |
4218 | 653 | mapParameters.insert(std::pair<std::string, Measure>( |
4219 | 653 | tolower(wkt1ParameterName), Measure(val, unit))); |
4220 | 653 | } catch (const std::exception &) { |
4221 | 41 | } |
4222 | 653 | } |
4223 | 1.56k | } |
4224 | | |
4225 | 75 | Measure latitudeOfOrigin = mapParameters["latitude_of_origin"]; |
4226 | 75 | Measure centralMeridian = mapParameters["central_meridian"]; |
4227 | 75 | Measure scaleFactorFromMap = mapParameters["scale_factor"]; |
4228 | 75 | Measure scaleFactor((scaleFactorFromMap.unit() == UnitOfMeasure::NONE) |
4229 | 75 | ? Measure(1.0, UnitOfMeasure::SCALE_UNITY) |
4230 | 75 | : scaleFactorFromMap); |
4231 | 75 | Measure falseEasting = mapParameters["false_easting"]; |
4232 | 75 | Measure falseNorthing = mapParameters["false_northing"]; |
4233 | 75 | if (latitudeOfOrigin.unit() != UnitOfMeasure::NONE && |
4234 | 75 | scaleFactor.getSIValue() == 1.0) { |
4235 | 2 | return Conversion::createPolarStereographicVariantB( |
4236 | 2 | PropertyMap().set(IdentifiedObject::NAME_KEY, "unnamed"), |
4237 | 2 | Angle(latitudeOfOrigin.value(), latitudeOfOrigin.unit()), |
4238 | 2 | Angle(centralMeridian.value(), centralMeridian.unit()), |
4239 | 2 | Length(falseEasting.value(), falseEasting.unit()), |
4240 | 2 | Length(falseNorthing.value(), falseNorthing.unit())); |
4241 | 2 | } |
4242 | | |
4243 | 73 | if (latitudeOfOrigin.unit() != UnitOfMeasure::NONE && |
4244 | 73 | std::fabs(std::fabs(latitudeOfOrigin.convertToUnit( |
4245 | 0 | UnitOfMeasure::DEGREE)) - |
4246 | 0 | 90.0) < 1e-10) { |
4247 | 0 | return Conversion::createPolarStereographicVariantA( |
4248 | 0 | PropertyMap().set(IdentifiedObject::NAME_KEY, "unnamed"), |
4249 | 0 | Angle(latitudeOfOrigin.value(), latitudeOfOrigin.unit()), |
4250 | 0 | Angle(centralMeridian.value(), centralMeridian.unit()), |
4251 | 0 | Scale(scaleFactor.value(), scaleFactor.unit()), |
4252 | 0 | Length(falseEasting.value(), falseEasting.unit()), |
4253 | 0 | Length(falseNorthing.value(), falseNorthing.unit())); |
4254 | 0 | } |
4255 | | |
4256 | 73 | tryToIdentifyWKT1Method = false; |
4257 | | // Import GDAL PROJ4 extension nodes |
4258 | 233 | } else if (extensionChildren.size() == 2 && |
4259 | 233 | ci_equal(stripQuotes(extensionChildren[0]), "PROJ4")) { |
4260 | 45 | std::string projString = stripQuotes(extensionChildren[1]); |
4261 | 45 | if (starts_with(projString, "+proj=")) { |
4262 | 39 | if (projString.find(" +type=crs") == std::string::npos) { |
4263 | 34 | projString += " +type=crs"; |
4264 | 34 | } |
4265 | 39 | try { |
4266 | 39 | auto projObj = |
4267 | 39 | PROJStringParser().createFromPROJString(projString); |
4268 | 39 | auto projObjCrs = |
4269 | 39 | nn_dynamic_pointer_cast<ProjectedCRS>(projObj); |
4270 | 39 | if (projObjCrs) { |
4271 | 0 | return projObjCrs->derivingConversion(); |
4272 | 0 | } |
4273 | 39 | } catch (const io::ParsingException &) { |
4274 | 38 | } |
4275 | 39 | } |
4276 | 45 | } |
4277 | | |
4278 | 306 | std::string projectionName(std::move(wkt1ProjectionName)); |
4279 | 306 | const MethodMapping *mapping = |
4280 | 306 | tryToIdentifyWKT1Method ? getMappingFromWKT1(projectionName) : nullptr; |
4281 | | |
4282 | 306 | if (!mapping) { |
4283 | | // Sometimes non-WKT1:ESRI looking WKT can actually use WKT1:ESRI |
4284 | | // projection definitions |
4285 | 234 | std::map<std::string, std::string, ci_less_struct> mapParamNameToValue; |
4286 | 234 | const auto esriMapping = |
4287 | 234 | getESRIMapping(projCRSNode, projectionNode, mapParamNameToValue); |
4288 | 234 | if (esriMapping != nullptr) { |
4289 | 11 | return buildProjectionFromESRI( |
4290 | 11 | baseGeodCRS, projCRSNode, projectionNode, defaultLinearUnit, |
4291 | 11 | defaultAngularUnit, esriMapping, mapParamNameToValue); |
4292 | 11 | } |
4293 | 234 | } |
4294 | | |
4295 | 295 | if (mapping) { |
4296 | 72 | mapping = selectSphericalOrEllipsoidal(mapping, baseGeodCRS); |
4297 | 223 | } else if (metadata::Identifier::isEquivalentName( |
4298 | 223 | projectionName.c_str(), "Lambert Conformal Conic")) { |
4299 | | // Lambert Conformal Conic or Lambert_Conformal_Conic are respectively |
4300 | | // used by Oracle WKT and Trimble for either LCC 1SP or 2SP, so we |
4301 | | // have to look at parameters to figure out the variant. |
4302 | 28 | bool found2ndStdParallel = false; |
4303 | 28 | bool foundScaleFactor = false; |
4304 | 246 | for (const auto &childNode : projCRSNode->GP()->children()) { |
4305 | 246 | if (ci_equal(childNode->GP()->value(), WKTConstants::PARAMETER)) { |
4306 | 73 | const auto &childNodeChildren = childNode->GP()->children(); |
4307 | 73 | if (childNodeChildren.size() < 2) { |
4308 | 0 | ThrowNotEnoughChildren(WKTConstants::PARAMETER); |
4309 | 0 | } |
4310 | 73 | const std::string wkt1ParameterName( |
4311 | 73 | stripQuotes(childNodeChildren[0])); |
4312 | 73 | if (metadata::Identifier::isEquivalentName( |
4313 | 73 | wkt1ParameterName.c_str(), WKT1_STANDARD_PARALLEL_2)) { |
4314 | 1 | found2ndStdParallel = true; |
4315 | 72 | } else if (metadata::Identifier::isEquivalentName( |
4316 | 72 | wkt1ParameterName.c_str(), WKT1_SCALE_FACTOR)) { |
4317 | 10 | foundScaleFactor = true; |
4318 | 10 | } |
4319 | 73 | } |
4320 | 246 | } |
4321 | 28 | if (found2ndStdParallel && !foundScaleFactor) { |
4322 | 1 | mapping = getMapping(EPSG_CODE_METHOD_LAMBERT_CONIC_CONFORMAL_2SP); |
4323 | 27 | } else if (!found2ndStdParallel && foundScaleFactor) { |
4324 | 8 | mapping = getMapping(EPSG_CODE_METHOD_LAMBERT_CONIC_CONFORMAL_1SP); |
4325 | 19 | } else if (found2ndStdParallel && foundScaleFactor) { |
4326 | | // Not sure if that happens |
4327 | 0 | mapping = getMapping( |
4328 | 0 | EPSG_CODE_METHOD_LAMBERT_CONIC_CONFORMAL_2SP_MICHIGAN); |
4329 | 0 | } |
4330 | 28 | } |
4331 | | |
4332 | | // For Krovak, we need to look at axis to decide between the Krovak and |
4333 | | // Krovak East-North Oriented methods |
4334 | 295 | if (ci_equal(projectionName, "Krovak") && |
4335 | 295 | projCRSNode->countChildrenOfName(WKTConstants::AXIS) == 2 && |
4336 | 295 | &buildAxis(projCRSNode->GP()->lookForChild(WKTConstants::AXIS, 0), |
4337 | 0 | defaultLinearUnit, UnitOfMeasure::Type::LINEAR, false, 1) |
4338 | 0 | ->direction() == &AxisDirection::SOUTH && |
4339 | 295 | &buildAxis(projCRSNode->GP()->lookForChild(WKTConstants::AXIS, 1), |
4340 | 0 | defaultLinearUnit, UnitOfMeasure::Type::LINEAR, false, 2) |
4341 | 0 | ->direction() == &AxisDirection::WEST) { |
4342 | 0 | mapping = getMapping(EPSG_CODE_METHOD_KROVAK); |
4343 | 0 | } |
4344 | | |
4345 | 295 | PropertyMap propertiesMethod; |
4346 | 295 | if (mapping) { |
4347 | 81 | projectionName = mapping->wkt2_name; |
4348 | 81 | if (mapping->epsg_code != 0) { |
4349 | 70 | propertiesMethod.set(Identifier::CODE_KEY, mapping->epsg_code); |
4350 | 70 | propertiesMethod.set(Identifier::CODESPACE_KEY, Identifier::EPSG); |
4351 | 70 | } |
4352 | 81 | } |
4353 | 295 | propertiesMethod.set(IdentifiedObject::NAME_KEY, projectionName); |
4354 | | |
4355 | 295 | std::vector<bool> foundParameters; |
4356 | 295 | if (mapping) { |
4357 | 81 | size_t countParams = 0; |
4358 | 599 | while (mapping->params[countParams] != nullptr) { |
4359 | 518 | ++countParams; |
4360 | 518 | } |
4361 | 81 | foundParameters.resize(countParams); |
4362 | 81 | } |
4363 | | |
4364 | 2.44k | for (const auto &childNode : projCRSNode->GP()->children()) { |
4365 | 2.44k | if (ci_equal(childNode->GP()->value(), WKTConstants::PARAMETER)) { |
4366 | 997 | const auto &childNodeChildren = childNode->GP()->children(); |
4367 | 997 | if (childNodeChildren.size() < 2) { |
4368 | 39 | ThrowNotEnoughChildren(WKTConstants::PARAMETER); |
4369 | 39 | } |
4370 | 958 | const auto ¶mValue = childNodeChildren[1]->GP()->value(); |
4371 | | |
4372 | 958 | PropertyMap propertiesParameter; |
4373 | 958 | const std::string wkt1ParameterName( |
4374 | 958 | stripQuotes(childNodeChildren[0])); |
4375 | 958 | std::string parameterName(wkt1ParameterName); |
4376 | 958 | if (gdal_3026_hack) { |
4377 | 0 | if (ci_equal(parameterName, "latitude_of_origin")) { |
4378 | 0 | parameterName = "standard_parallel_1"; |
4379 | 0 | } else if (ci_equal(parameterName, "scale_factor") && |
4380 | 0 | paramValue == "1") { |
4381 | 0 | continue; |
4382 | 0 | } |
4383 | 0 | } |
4384 | 958 | auto *paramMapping = |
4385 | 958 | mapping ? getMappingFromWKT1(mapping, parameterName) : nullptr; |
4386 | 958 | if (mapping && |
4387 | 958 | mapping->epsg_code == EPSG_CODE_METHOD_MERCATOR_VARIANT_B && |
4388 | 958 | ci_equal(parameterName, "latitude_of_origin")) { |
4389 | | // Some illegal formulations of Mercator_2SP have a unexpected |
4390 | | // latitude_of_origin parameter. We accept it on import, but |
4391 | | // do not accept it when exporting to PROJ string, unless it is |
4392 | | // zero. |
4393 | | // No need to try to update foundParameters[] as this is a |
4394 | | // unexpected one. |
4395 | 0 | parameterName = EPSG_NAME_PARAMETER_LATITUDE_OF_NATURAL_ORIGIN; |
4396 | 0 | propertiesParameter.set( |
4397 | 0 | Identifier::CODE_KEY, |
4398 | 0 | EPSG_CODE_PARAMETER_LATITUDE_OF_NATURAL_ORIGIN); |
4399 | 0 | propertiesParameter.set(Identifier::CODESPACE_KEY, |
4400 | 0 | Identifier::EPSG); |
4401 | 958 | } else if (mapping && paramMapping) { |
4402 | 37 | for (size_t idx = 0; mapping->params[idx] != nullptr; ++idx) { |
4403 | 37 | if (mapping->params[idx] == paramMapping) { |
4404 | 14 | foundParameters[idx] = true; |
4405 | 14 | break; |
4406 | 14 | } |
4407 | 37 | } |
4408 | 14 | parameterName = paramMapping->wkt2_name; |
4409 | 14 | if (paramMapping->epsg_code != 0) { |
4410 | 14 | propertiesParameter.set(Identifier::CODE_KEY, |
4411 | 14 | paramMapping->epsg_code); |
4412 | 14 | propertiesParameter.set(Identifier::CODESPACE_KEY, |
4413 | 14 | Identifier::EPSG); |
4414 | 14 | } |
4415 | 14 | } |
4416 | 958 | propertiesParameter.set(IdentifiedObject::NAME_KEY, parameterName); |
4417 | 958 | parameters.push_back( |
4418 | 958 | OperationParameter::create(propertiesParameter)); |
4419 | 958 | try { |
4420 | 958 | double val = io::asDouble(paramValue); |
4421 | 958 | auto unit = guessUnitForParameter( |
4422 | 958 | wkt1ParameterName, defaultLinearUnit, defaultAngularUnit); |
4423 | 958 | values.push_back(ParameterValue::create(Measure(val, unit))); |
4424 | 958 | } catch (const std::exception &) { |
4425 | 125 | throw ParsingException( |
4426 | 125 | concat("unhandled parameter value type : ", paramValue)); |
4427 | 125 | } |
4428 | 958 | } |
4429 | 2.44k | } |
4430 | | |
4431 | | // Add back important parameters that should normally be present, but |
4432 | | // are sometimes missing. Currently we only deal with Scale factor at |
4433 | | // natural origin. This is to avoid a default value of 0 to slip in later. |
4434 | | // But such WKT should be considered invalid. |
4435 | 131 | if (mapping) { |
4436 | 168 | for (size_t idx = 0; mapping->params[idx] != nullptr; ++idx) { |
4437 | 144 | if (!foundParameters[idx] && |
4438 | 144 | mapping->params[idx]->epsg_code == |
4439 | 142 | EPSG_CODE_PARAMETER_SCALE_FACTOR_AT_NATURAL_ORIGIN) { |
4440 | | |
4441 | 6 | emitRecoverableWarning( |
4442 | 6 | "The WKT string lacks a value " |
4443 | 6 | "for " EPSG_NAME_PARAMETER_SCALE_FACTOR_AT_NATURAL_ORIGIN |
4444 | 6 | ". Default it to 1."); |
4445 | | |
4446 | 6 | PropertyMap propertiesParameter; |
4447 | 6 | propertiesParameter.set( |
4448 | 6 | Identifier::CODE_KEY, |
4449 | 6 | EPSG_CODE_PARAMETER_SCALE_FACTOR_AT_NATURAL_ORIGIN); |
4450 | 6 | propertiesParameter.set(Identifier::CODESPACE_KEY, |
4451 | 6 | Identifier::EPSG); |
4452 | 6 | propertiesParameter.set( |
4453 | 6 | IdentifiedObject::NAME_KEY, |
4454 | 6 | EPSG_NAME_PARAMETER_SCALE_FACTOR_AT_NATURAL_ORIGIN); |
4455 | 6 | parameters.push_back( |
4456 | 6 | OperationParameter::create(propertiesParameter)); |
4457 | 6 | values.push_back(ParameterValue::create( |
4458 | 6 | Measure(1.0, UnitOfMeasure::SCALE_UNITY))); |
4459 | 6 | } |
4460 | 144 | } |
4461 | 24 | } |
4462 | | |
4463 | 131 | if (mapping && (mapping->epsg_code == |
4464 | 24 | EPSG_CODE_METHOD_HOTINE_OBLIQUE_MERCATOR_VARIANT_A || |
4465 | 24 | mapping->epsg_code == |
4466 | 22 | EPSG_CODE_METHOD_HOTINE_OBLIQUE_MERCATOR_VARIANT_B)) { |
4467 | | // Special case when importing some GDAL WKT of Hotine Oblique Mercator |
4468 | | // that have a Azimuth parameter but lacks the Rectified Grid Angle. |
4469 | | // We have code in the exportToPROJString() to deal with that situation, |
4470 | | // but also adds the rectified grid angle from the azimuth on import. |
4471 | 2 | bool foundAngleRecifiedToSkewGrid = false; |
4472 | 2 | bool foundAzimuth = false; |
4473 | 16 | for (size_t idx = 0; mapping->params[idx] != nullptr; ++idx) { |
4474 | 14 | if (foundParameters[idx] && |
4475 | 14 | mapping->params[idx]->epsg_code == |
4476 | 0 | EPSG_CODE_PARAMETER_ANGLE_RECTIFIED_TO_SKEW_GRID) { |
4477 | 0 | foundAngleRecifiedToSkewGrid = true; |
4478 | 14 | } else if (foundParameters[idx] && |
4479 | 14 | mapping->params[idx]->epsg_code == |
4480 | 0 | EPSG_CODE_PARAMETER_AZIMUTH_PROJECTION_CENTRE) { |
4481 | 0 | foundAzimuth = true; |
4482 | 0 | } |
4483 | 14 | } |
4484 | 2 | if (!foundAngleRecifiedToSkewGrid && foundAzimuth) { |
4485 | 0 | for (size_t idx = 0; idx < parameters.size(); ++idx) { |
4486 | 0 | if (parameters[idx]->getEPSGCode() == |
4487 | 0 | EPSG_CODE_PARAMETER_AZIMUTH_PROJECTION_CENTRE) { |
4488 | 0 | PropertyMap propertiesParameter; |
4489 | 0 | propertiesParameter.set( |
4490 | 0 | Identifier::CODE_KEY, |
4491 | 0 | EPSG_CODE_PARAMETER_ANGLE_RECTIFIED_TO_SKEW_GRID); |
4492 | 0 | propertiesParameter.set(Identifier::CODESPACE_KEY, |
4493 | 0 | Identifier::EPSG); |
4494 | 0 | propertiesParameter.set( |
4495 | 0 | IdentifiedObject::NAME_KEY, |
4496 | 0 | EPSG_NAME_PARAMETER_ANGLE_RECTIFIED_TO_SKEW_GRID); |
4497 | 0 | parameters.push_back( |
4498 | 0 | OperationParameter::create(propertiesParameter)); |
4499 | 0 | values.push_back(values[idx]); |
4500 | 0 | } |
4501 | 0 | } |
4502 | 0 | } |
4503 | 2 | } |
4504 | | |
4505 | 131 | return Conversion::create( |
4506 | 131 | PropertyMap().set(IdentifiedObject::NAME_KEY, "unnamed"), |
4507 | 131 | propertiesMethod, parameters, values) |
4508 | 131 | ->identify(); |
4509 | 295 | } |
4510 | | |
4511 | | // --------------------------------------------------------------------------- |
4512 | | |
4513 | | static ProjectedCRSNNPtr createPseudoMercator(const PropertyMap &props, |
4514 | 0 | const cs::CartesianCSNNPtr &cs) { |
4515 | 0 | auto conversion = Conversion::createPopularVisualisationPseudoMercator( |
4516 | 0 | PropertyMap().set(IdentifiedObject::NAME_KEY, "unnamed"), Angle(0), |
4517 | 0 | Angle(0), Length(0), Length(0)); |
4518 | 0 | return ProjectedCRS::create(props, GeographicCRS::EPSG_4326, conversion, |
4519 | 0 | cs); |
4520 | 0 | } |
4521 | | |
4522 | | // --------------------------------------------------------------------------- |
4523 | | |
4524 | | ProjectedCRSNNPtr |
4525 | 700 | WKTParser::Private::buildProjectedCRS(const WKTNodeNNPtr &node) { |
4526 | | |
4527 | 700 | const auto *nodeP = node->GP(); |
4528 | 700 | auto &conversionNode = nodeP->lookForChild(WKTConstants::CONVERSION); |
4529 | 700 | auto &projectionNode = nodeP->lookForChild(WKTConstants::PROJECTION); |
4530 | 700 | if (isNull(conversionNode) && isNull(projectionNode)) { |
4531 | 52 | ThrowMissing(WKTConstants::CONVERSION); |
4532 | 52 | } |
4533 | | |
4534 | 648 | auto &baseGeodCRSNode = |
4535 | 648 | nodeP->lookForChild(WKTConstants::BASEGEODCRS, |
4536 | 648 | WKTConstants::BASEGEOGCRS, WKTConstants::GEOGCS); |
4537 | 648 | if (isNull(baseGeodCRSNode)) { |
4538 | 3 | throw ParsingException( |
4539 | 3 | "Missing BASEGEODCRS / BASEGEOGCRS / GEOGCS node"); |
4540 | 3 | } |
4541 | 645 | auto baseGeodCRS = buildGeodeticCRS(baseGeodCRSNode); |
4542 | | |
4543 | 645 | auto props = buildProperties(node); |
4544 | | |
4545 | 645 | auto &csNode = nodeP->lookForChild(WKTConstants::CS_); |
4546 | 645 | const auto &nodeValue = nodeP->value(); |
4547 | 645 | if (isNull(csNode) && !ci_equal(nodeValue, WKTConstants::PROJCS) && |
4548 | 645 | !ci_equal(nodeValue, WKTConstants::BASEPROJCRS)) { |
4549 | 0 | ThrowMissing(WKTConstants::CS_); |
4550 | 0 | } |
4551 | | |
4552 | 645 | const std::string projCRSName = stripQuotes(nodeP->children()[0]); |
4553 | | |
4554 | 645 | auto cs = [this, &projCRSName, &nodeP, &csNode, &node, &nodeValue, |
4555 | 645 | &conversionNode]() -> CoordinateSystemNNPtr { |
4556 | 630 | if (isNull(csNode) && ci_equal(nodeValue, WKTConstants::BASEPROJCRS) && |
4557 | 630 | !isNull(conversionNode)) { |
4558 | | // A BASEPROJCRS (as of WKT2 18-010r11) normally lacks an explicit |
4559 | | // CS[] which cause issues to properly instantiate it. So we first |
4560 | | // start by trying to identify the BASEPROJCRS by its id or name. |
4561 | | // And fallback to exploring the conversion parameters to infer the |
4562 | | // CS AXIS unit from the linear parameter unit... Not fully bullet |
4563 | | // proof. |
4564 | 0 | if (dbContext_) { |
4565 | | // Get official name from database if ID is present |
4566 | 0 | auto &idNode = nodeP->lookForChild(WKTConstants::ID); |
4567 | 0 | if (!isNull(idNode)) { |
4568 | 0 | try { |
4569 | 0 | auto id = buildId(node, idNode, false, false); |
4570 | 0 | auto authFactory = AuthorityFactory::create( |
4571 | 0 | NN_NO_CHECK(dbContext_), *id->codeSpace()); |
4572 | 0 | auto projCRS = |
4573 | 0 | authFactory->createProjectedCRS(id->code()); |
4574 | 0 | return projCRS->coordinateSystem(); |
4575 | 0 | } catch (const std::exception &) { |
4576 | 0 | } |
4577 | 0 | } |
4578 | | |
4579 | 0 | auto authFactory = AuthorityFactory::create( |
4580 | 0 | NN_NO_CHECK(dbContext_), std::string()); |
4581 | 0 | auto res = authFactory->createObjectsFromName( |
4582 | 0 | projCRSName, {AuthorityFactory::ObjectType::PROJECTED_CRS}, |
4583 | 0 | false, 2); |
4584 | 0 | if (res.size() == 1) { |
4585 | 0 | auto projCRS = |
4586 | 0 | dynamic_cast<const ProjectedCRS *>(res.front().get()); |
4587 | 0 | if (projCRS) { |
4588 | 0 | return projCRS->coordinateSystem(); |
4589 | 0 | } |
4590 | 0 | } |
4591 | 0 | } |
4592 | | |
4593 | 0 | auto conv = buildConversion(conversionNode, UnitOfMeasure::METRE, |
4594 | 0 | UnitOfMeasure::DEGREE); |
4595 | 0 | UnitOfMeasure linearUOM = UnitOfMeasure::NONE; |
4596 | 0 | for (const auto &genOpParamvalue : conv->parameterValues()) { |
4597 | 0 | auto opParamvalue = |
4598 | 0 | dynamic_cast<const operation::OperationParameterValue *>( |
4599 | 0 | genOpParamvalue.get()); |
4600 | 0 | if (opParamvalue) { |
4601 | 0 | const auto ¶meterValue = opParamvalue->parameterValue(); |
4602 | 0 | if (parameterValue->type() == |
4603 | 0 | operation::ParameterValue::Type::MEASURE) { |
4604 | 0 | const auto &measure = parameterValue->value(); |
4605 | 0 | const auto &unit = measure.unit(); |
4606 | 0 | if (unit.type() == UnitOfMeasure::Type::LINEAR) { |
4607 | 0 | if (linearUOM == UnitOfMeasure::NONE) { |
4608 | 0 | linearUOM = unit; |
4609 | 0 | } else if (linearUOM != unit) { |
4610 | 0 | linearUOM = UnitOfMeasure::NONE; |
4611 | 0 | break; |
4612 | 0 | } |
4613 | 0 | } |
4614 | 0 | } |
4615 | 0 | } |
4616 | 0 | } |
4617 | 0 | if (linearUOM != UnitOfMeasure::NONE) { |
4618 | 0 | return CartesianCS::createEastingNorthing(linearUOM); |
4619 | 0 | } |
4620 | 0 | } |
4621 | 630 | return buildCS(csNode, node, UnitOfMeasure::NONE); |
4622 | 630 | }(); |
4623 | 645 | auto cartesianCS = nn_dynamic_pointer_cast<CartesianCS>(cs); |
4624 | | |
4625 | 645 | if (esriStyle_ && dbContext_) { |
4626 | 293 | if (cartesianCS) { |
4627 | 293 | std::string outTableName; |
4628 | 293 | std::string authNameFromAlias; |
4629 | 293 | std::string codeFromAlias; |
4630 | 293 | auto authFactory = AuthorityFactory::create(NN_NO_CHECK(dbContext_), |
4631 | 293 | std::string()); |
4632 | 293 | auto officialName = authFactory->getOfficialNameFromAlias( |
4633 | 293 | projCRSName, "projected_crs", "ESRI", false, outTableName, |
4634 | 293 | authNameFromAlias, codeFromAlias); |
4635 | 293 | if (!officialName.empty()) { |
4636 | | // Special case for https://github.com/OSGeo/PROJ/issues/2086 |
4637 | | // The name of the CRS to identify is |
4638 | | // NAD_1983_HARN_StatePlane_Colorado_North_FIPS_0501 |
4639 | | // whereas it should be |
4640 | | // NAD_1983_HARN_StatePlane_Colorado_North_FIPS_0501_Feet |
4641 | 0 | constexpr double US_FOOT_CONV_FACTOR = 12.0 / 39.37; |
4642 | 0 | if (projCRSName.find("_FIPS_") != std::string::npos && |
4643 | 0 | projCRSName.find("_Feet") == std::string::npos && |
4644 | 0 | std::fabs( |
4645 | 0 | cartesianCS->axisList()[0]->unit().conversionToSI() - |
4646 | 0 | US_FOOT_CONV_FACTOR) < 1e-10 * US_FOOT_CONV_FACTOR) { |
4647 | 0 | auto officialNameFromFeet = |
4648 | 0 | authFactory->getOfficialNameFromAlias( |
4649 | 0 | projCRSName + "_Feet", "projected_crs", "ESRI", |
4650 | 0 | false, outTableName, authNameFromAlias, |
4651 | 0 | codeFromAlias); |
4652 | 0 | if (!officialNameFromFeet.empty()) { |
4653 | 0 | officialName = std::move(officialNameFromFeet); |
4654 | 0 | } |
4655 | 0 | } |
4656 | |
|
4657 | 0 | props.set(IdentifiedObject::NAME_KEY, officialName); |
4658 | 0 | } |
4659 | 293 | } |
4660 | 293 | } |
4661 | | |
4662 | 645 | if (isNull(conversionNode) && hasWebMercPROJ4String(node, projectionNode) && |
4663 | 645 | cartesianCS) { |
4664 | 0 | toWGS84Parameters_.clear(); |
4665 | 0 | return createPseudoMercator(props, NN_NO_CHECK(cartesianCS)); |
4666 | 0 | } |
4667 | | |
4668 | | // WGS_84_Pseudo_Mercator: Particular case for corrupted ESRI WKT generated |
4669 | | // by older GDAL versions |
4670 | | // https://trac.osgeo.org/gdal/changeset/30732 |
4671 | | // WGS_1984_Web_Mercator: deprecated ESRI:102113 |
4672 | 645 | if (cartesianCS && (metadata::Identifier::isEquivalentName( |
4673 | 623 | projCRSName.c_str(), "WGS_84_Pseudo_Mercator") || |
4674 | 623 | metadata::Identifier::isEquivalentName( |
4675 | 623 | projCRSName.c_str(), "WGS_1984_Web_Mercator"))) { |
4676 | 0 | toWGS84Parameters_.clear(); |
4677 | 0 | return createPseudoMercator(props, NN_NO_CHECK(cartesianCS)); |
4678 | 0 | } |
4679 | | |
4680 | | // For WKT2, if there is no explicit parameter unit, use metre for linear |
4681 | | // units and degree for angular units |
4682 | 645 | const UnitOfMeasure linearUnit( |
4683 | 645 | !isNull(conversionNode) |
4684 | 645 | ? UnitOfMeasure::METRE |
4685 | 645 | : buildUnitInSubNode(node, UnitOfMeasure::Type::LINEAR)); |
4686 | 645 | const auto &angularUnit = |
4687 | 645 | !isNull(conversionNode) |
4688 | 645 | ? UnitOfMeasure::DEGREE |
4689 | 645 | : baseGeodCRS->coordinateSystem()->axisList()[0]->unit(); |
4690 | | |
4691 | 645 | auto conversion = |
4692 | 645 | !isNull(conversionNode) |
4693 | 645 | ? buildConversion(conversionNode, linearUnit, angularUnit) |
4694 | 645 | : buildProjection(baseGeodCRS, node, projectionNode, linearUnit, |
4695 | 633 | angularUnit); |
4696 | | |
4697 | | // No explicit AXIS node ? (WKT1) |
4698 | 645 | if (isNull(nodeP->lookForChild(WKTConstants::AXIS))) { |
4699 | 440 | props.set("IMPLICIT_CS", true); |
4700 | 440 | } |
4701 | | |
4702 | 645 | if (isNull(csNode) && node->countChildrenOfName(WKTConstants::AXIS) == 0) { |
4703 | | |
4704 | 440 | const auto methodCode = conversion->method()->getEPSGCode(); |
4705 | | // Krovak south oriented ? |
4706 | 440 | if (methodCode == EPSG_CODE_METHOD_KROVAK) { |
4707 | 11 | cartesianCS = |
4708 | 11 | CartesianCS::create( |
4709 | 11 | PropertyMap(), |
4710 | 11 | CoordinateSystemAxis::create( |
4711 | 11 | util::PropertyMap().set(IdentifiedObject::NAME_KEY, |
4712 | 11 | AxisName::Southing), |
4713 | 11 | emptyString, AxisDirection::SOUTH, linearUnit), |
4714 | 11 | CoordinateSystemAxis::create( |
4715 | 11 | util::PropertyMap().set(IdentifiedObject::NAME_KEY, |
4716 | 11 | AxisName::Westing), |
4717 | 11 | emptyString, AxisDirection::WEST, linearUnit)) |
4718 | 11 | .as_nullable(); |
4719 | 429 | } else if (methodCode == |
4720 | 429 | EPSG_CODE_METHOD_POLAR_STEREOGRAPHIC_VARIANT_A || |
4721 | 429 | methodCode == |
4722 | 417 | EPSG_CODE_METHOD_LAMBERT_AZIMUTHAL_EQUAL_AREA) { |
4723 | | // It is likely that the ESRI definition of EPSG:32661 (UPS North) & |
4724 | | // EPSG:32761 (UPS South) uses the easting-northing order, instead |
4725 | | // of the EPSG northing-easting order. |
4726 | | // Same for WKT1_GDAL |
4727 | 17 | const double lat0 = conversion->parameterValueNumeric( |
4728 | 17 | EPSG_CODE_PARAMETER_LATITUDE_OF_NATURAL_ORIGIN, |
4729 | 17 | common::UnitOfMeasure::DEGREE); |
4730 | 17 | if (std::fabs(lat0 - 90) < 1e-10) { |
4731 | 9 | cartesianCS = |
4732 | 9 | CartesianCS::createNorthPoleEastingSouthNorthingSouth( |
4733 | 9 | linearUnit) |
4734 | 9 | .as_nullable(); |
4735 | 9 | } else if (std::fabs(lat0 - -90) < 1e-10) { |
4736 | 8 | cartesianCS = |
4737 | 8 | CartesianCS::createSouthPoleEastingNorthNorthingNorth( |
4738 | 8 | linearUnit) |
4739 | 8 | .as_nullable(); |
4740 | 8 | } |
4741 | 412 | } else if (methodCode == |
4742 | 412 | EPSG_CODE_METHOD_POLAR_STEREOGRAPHIC_VARIANT_B) { |
4743 | 7 | const double lat_ts = conversion->parameterValueNumeric( |
4744 | 7 | EPSG_CODE_PARAMETER_LATITUDE_STD_PARALLEL, |
4745 | 7 | common::UnitOfMeasure::DEGREE); |
4746 | 7 | if (lat_ts > 0) { |
4747 | 5 | cartesianCS = |
4748 | 5 | CartesianCS::createNorthPoleEastingSouthNorthingSouth( |
4749 | 5 | linearUnit) |
4750 | 5 | .as_nullable(); |
4751 | 5 | } else if (lat_ts < 0) { |
4752 | 1 | cartesianCS = |
4753 | 1 | CartesianCS::createSouthPoleEastingNorthNorthingNorth( |
4754 | 1 | linearUnit) |
4755 | 1 | .as_nullable(); |
4756 | 1 | } |
4757 | 405 | } else if (methodCode == |
4758 | 405 | EPSG_CODE_METHOD_TRANSVERSE_MERCATOR_SOUTH_ORIENTATED) { |
4759 | 0 | cartesianCS = |
4760 | 0 | CartesianCS::createWestingSouthing(linearUnit).as_nullable(); |
4761 | 0 | } |
4762 | 440 | } |
4763 | 645 | if (!cartesianCS) { |
4764 | 0 | ThrowNotExpectedCSType(CartesianCS::WKT2_TYPE); |
4765 | 0 | } |
4766 | | |
4767 | 645 | if (cartesianCS->axisList().size() == 3 && |
4768 | 645 | baseGeodCRS->coordinateSystem()->axisList().size() == 2) { |
4769 | 0 | baseGeodCRS = NN_NO_CHECK(util::nn_dynamic_pointer_cast<GeodeticCRS>( |
4770 | 0 | baseGeodCRS->promoteTo3D(std::string(), dbContext_))); |
4771 | 0 | } |
4772 | | |
4773 | 645 | addExtensionProj4ToProp(nodeP, props); |
4774 | | |
4775 | 645 | return ProjectedCRS::create(props, baseGeodCRS, conversion, |
4776 | 645 | NN_NO_CHECK(cartesianCS)); |
4777 | 645 | } |
4778 | | |
4779 | | // --------------------------------------------------------------------------- |
4780 | | |
4781 | | void WKTParser::Private::parseDynamic(const WKTNodeNNPtr &dynamicNode, |
4782 | | double &frameReferenceEpoch, |
4783 | 20 | util::optional<std::string> &modelName) { |
4784 | 20 | auto &frameEpochNode = dynamicNode->lookForChild(WKTConstants::FRAMEEPOCH); |
4785 | 20 | const auto &frameEpochChildren = frameEpochNode->GP()->children(); |
4786 | 20 | if (frameEpochChildren.empty()) { |
4787 | 5 | ThrowMissing(WKTConstants::FRAMEEPOCH); |
4788 | 5 | } |
4789 | 15 | try { |
4790 | 15 | frameReferenceEpoch = asDouble(frameEpochChildren[0]); |
4791 | 15 | } catch (const std::exception &) { |
4792 | 3 | throw ParsingException("Invalid FRAMEEPOCH node"); |
4793 | 3 | } |
4794 | 12 | auto &modelNode = dynamicNode->GP()->lookForChild( |
4795 | 12 | WKTConstants::MODEL, WKTConstants::VELOCITYGRID); |
4796 | 12 | const auto &modelChildren = modelNode->GP()->children(); |
4797 | 12 | if (modelChildren.size() == 1) { |
4798 | 0 | modelName = stripQuotes(modelChildren[0]); |
4799 | 0 | } |
4800 | 12 | } |
4801 | | |
4802 | | // --------------------------------------------------------------------------- |
4803 | | |
4804 | | VerticalReferenceFrameNNPtr WKTParser::Private::buildVerticalReferenceFrame( |
4805 | 727 | const WKTNodeNNPtr &node, const WKTNodeNNPtr &dynamicNode) { |
4806 | | |
4807 | 727 | if (!isNull(dynamicNode)) { |
4808 | 3 | double frameReferenceEpoch = 0.0; |
4809 | 3 | util::optional<std::string> modelName; |
4810 | 3 | parseDynamic(dynamicNode, frameReferenceEpoch, modelName); |
4811 | 3 | return DynamicVerticalReferenceFrame::create( |
4812 | 3 | buildProperties(node), getAnchor(node), |
4813 | 3 | optional<RealizationMethod>(), |
4814 | 3 | common::Measure(frameReferenceEpoch, common::UnitOfMeasure::YEAR), |
4815 | 3 | modelName); |
4816 | 3 | } |
4817 | | |
4818 | | // WKT1 VERT_DATUM has a datum type after the datum name |
4819 | 724 | const auto *nodeP = node->GP(); |
4820 | 724 | const std::string &name(nodeP->value()); |
4821 | 724 | auto &props = buildProperties(node); |
4822 | 724 | const auto &children = nodeP->children(); |
4823 | | |
4824 | 724 | if (esriStyle_ && dbContext_ && !children.empty()) { |
4825 | 2 | std::string outTableName; |
4826 | 2 | std::string authNameFromAlias; |
4827 | 2 | std::string codeFromAlias; |
4828 | 2 | auto authFactory = |
4829 | 2 | AuthorityFactory::create(NN_NO_CHECK(dbContext_), std::string()); |
4830 | 2 | const std::string datumName = stripQuotes(children[0]); |
4831 | 2 | auto officialName = authFactory->getOfficialNameFromAlias( |
4832 | 2 | datumName, "vertical_datum", "ESRI", false, outTableName, |
4833 | 2 | authNameFromAlias, codeFromAlias); |
4834 | 2 | if (!officialName.empty()) { |
4835 | 0 | props.set(IdentifiedObject::NAME_KEY, officialName); |
4836 | 0 | } |
4837 | 2 | } |
4838 | | |
4839 | 724 | if (ci_equal(name, WKTConstants::VERT_DATUM)) { |
4840 | 18 | if (children.size() >= 2) { |
4841 | 14 | props.set("VERT_DATUM_TYPE", children[1]->GP()->value()); |
4842 | 14 | } |
4843 | 18 | } |
4844 | | |
4845 | 724 | return VerticalReferenceFrame::create(props, getAnchor(node), |
4846 | 724 | getAnchorEpoch(node)); |
4847 | 727 | } |
4848 | | |
4849 | | // --------------------------------------------------------------------------- |
4850 | | |
4851 | | TemporalDatumNNPtr |
4852 | 20 | WKTParser::Private::buildTemporalDatum(const WKTNodeNNPtr &node) { |
4853 | 20 | const auto *nodeP = node->GP(); |
4854 | 20 | auto &calendarNode = nodeP->lookForChild(WKTConstants::CALENDAR); |
4855 | 20 | std::string calendar = TemporalDatum::CALENDAR_PROLEPTIC_GREGORIAN; |
4856 | 20 | const auto &calendarChildren = calendarNode->GP()->children(); |
4857 | 20 | if (calendarChildren.size() == 1) { |
4858 | 0 | calendar = stripQuotes(calendarChildren[0]); |
4859 | 0 | } |
4860 | | |
4861 | 20 | auto &timeOriginNode = nodeP->lookForChild(WKTConstants::TIMEORIGIN); |
4862 | 20 | std::string originStr; |
4863 | 20 | const auto &timeOriginNodeChildren = timeOriginNode->GP()->children(); |
4864 | 20 | if (timeOriginNodeChildren.size() == 1) { |
4865 | 3 | originStr = stripQuotes(timeOriginNodeChildren[0]); |
4866 | 3 | } |
4867 | 20 | auto origin = DateTime::create(originStr); |
4868 | 20 | return TemporalDatum::create(buildProperties(node), origin, calendar); |
4869 | 20 | } |
4870 | | |
4871 | | // --------------------------------------------------------------------------- |
4872 | | |
4873 | | EngineeringDatumNNPtr |
4874 | 8 | WKTParser::Private::buildEngineeringDatum(const WKTNodeNNPtr &node) { |
4875 | 8 | return EngineeringDatum::create(buildProperties(node), getAnchor(node)); |
4876 | 8 | } |
4877 | | |
4878 | | // --------------------------------------------------------------------------- |
4879 | | |
4880 | | ParametricDatumNNPtr |
4881 | 5 | WKTParser::Private::buildParametricDatum(const WKTNodeNNPtr &node) { |
4882 | 5 | return ParametricDatum::create(buildProperties(node), getAnchor(node)); |
4883 | 5 | } |
4884 | | |
4885 | | // --------------------------------------------------------------------------- |
4886 | | |
4887 | | static CRSNNPtr |
4888 | | createBoundCRSSourceTransformationCRS(const crs::CRSPtr &sourceCRS, |
4889 | 6 | const crs::CRSPtr &targetCRS) { |
4890 | 6 | CRSPtr sourceTransformationCRS; |
4891 | 6 | if (dynamic_cast<GeographicCRS *>(targetCRS.get())) { |
4892 | 6 | GeographicCRSPtr sourceGeographicCRS = |
4893 | 6 | sourceCRS->extractGeographicCRS(); |
4894 | 6 | sourceTransformationCRS = sourceGeographicCRS; |
4895 | 6 | if (sourceGeographicCRS) { |
4896 | 0 | const auto &sourceDatum = sourceGeographicCRS->datum(); |
4897 | 0 | if (sourceDatum != nullptr && sourceGeographicCRS->primeMeridian() |
4898 | 0 | ->longitude() |
4899 | 0 | .getSIValue() != 0.0) { |
4900 | 0 | sourceTransformationCRS = |
4901 | 0 | GeographicCRS::create( |
4902 | 0 | util::PropertyMap().set( |
4903 | 0 | common::IdentifiedObject::NAME_KEY, |
4904 | 0 | sourceGeographicCRS->nameStr() + |
4905 | 0 | " (with Greenwich prime meridian)"), |
4906 | 0 | datum::GeodeticReferenceFrame::create( |
4907 | 0 | util::PropertyMap().set( |
4908 | 0 | common::IdentifiedObject::NAME_KEY, |
4909 | 0 | sourceDatum->nameStr() + |
4910 | 0 | " (with Greenwich prime meridian)"), |
4911 | 0 | sourceDatum->ellipsoid(), |
4912 | 0 | util::optional<std::string>(), |
4913 | 0 | datum::PrimeMeridian::GREENWICH), |
4914 | 0 | sourceGeographicCRS->coordinateSystem()) |
4915 | 0 | .as_nullable(); |
4916 | 0 | } |
4917 | 6 | } else { |
4918 | 6 | auto vertSourceCRS = |
4919 | 6 | std::dynamic_pointer_cast<VerticalCRS>(sourceCRS); |
4920 | 6 | if (!vertSourceCRS) { |
4921 | 0 | throw ParsingException( |
4922 | 0 | "Cannot find GeographicCRS or VerticalCRS in sourceCRS"); |
4923 | 0 | } |
4924 | 6 | const auto &axis = vertSourceCRS->coordinateSystem()->axisList()[0]; |
4925 | 6 | if (axis->unit() == common::UnitOfMeasure::METRE && |
4926 | 6 | &(axis->direction()) == &AxisDirection::UP) { |
4927 | 2 | sourceTransformationCRS = sourceCRS; |
4928 | 4 | } else { |
4929 | 4 | std::string sourceTransformationCRSName( |
4930 | 4 | vertSourceCRS->nameStr()); |
4931 | 4 | if (ends_with(sourceTransformationCRSName, " (ftUS)")) { |
4932 | 0 | sourceTransformationCRSName.resize( |
4933 | 0 | sourceTransformationCRSName.size() - strlen(" (ftUS)")); |
4934 | 0 | } |
4935 | 4 | if (ends_with(sourceTransformationCRSName, " depth")) { |
4936 | 0 | sourceTransformationCRSName.resize( |
4937 | 0 | sourceTransformationCRSName.size() - strlen(" depth")); |
4938 | 0 | } |
4939 | 4 | if (!ends_with(sourceTransformationCRSName, " height")) { |
4940 | 4 | sourceTransformationCRSName += " height"; |
4941 | 4 | } |
4942 | 4 | sourceTransformationCRS = |
4943 | 4 | VerticalCRS::create( |
4944 | 4 | PropertyMap().set(IdentifiedObject::NAME_KEY, |
4945 | 4 | sourceTransformationCRSName), |
4946 | 4 | vertSourceCRS->datum(), vertSourceCRS->datumEnsemble(), |
4947 | 4 | VerticalCS::createGravityRelatedHeight( |
4948 | 4 | common::UnitOfMeasure::METRE)) |
4949 | 4 | .as_nullable(); |
4950 | 4 | } |
4951 | 6 | } |
4952 | 6 | } else { |
4953 | 0 | sourceTransformationCRS = sourceCRS; |
4954 | 0 | } |
4955 | 6 | return NN_NO_CHECK(sourceTransformationCRS); |
4956 | 6 | } |
4957 | | |
4958 | | // --------------------------------------------------------------------------- |
4959 | | |
4960 | 672 | CRSNNPtr WKTParser::Private::buildVerticalCRS(const WKTNodeNNPtr &node) { |
4961 | 672 | const auto *nodeP = node->GP(); |
4962 | 672 | const auto &nodeValue = nodeP->value(); |
4963 | 672 | auto &vdatumNode = |
4964 | 672 | nodeP->lookForChild(WKTConstants::VDATUM, WKTConstants::VERT_DATUM, |
4965 | 672 | WKTConstants::VERTICALDATUM, WKTConstants::VRF); |
4966 | 672 | auto &ensembleNode = nodeP->lookForChild(WKTConstants::ENSEMBLE); |
4967 | | // like in ESRI VERTCS["WGS_1984",DATUM["D_WGS_1984", |
4968 | | // SPHEROID["WGS_1984",6378137.0,298.257223563]], |
4969 | | // PARAMETER["Vertical_Shift",0.0], |
4970 | | // PARAMETER["Direction",1.0],UNIT["Meter",1.0] |
4971 | 672 | auto &geogDatumNode = ci_equal(nodeValue, WKTConstants::VERTCS) |
4972 | 672 | ? nodeP->lookForChild(WKTConstants::DATUM) |
4973 | 672 | : null_node; |
4974 | 672 | if (isNull(vdatumNode) && isNull(geogDatumNode) && isNull(ensembleNode)) { |
4975 | 20 | throw ParsingException("Missing VDATUM or ENSEMBLE node"); |
4976 | 20 | } |
4977 | | |
4978 | 3.09k | for (const auto &childNode : nodeP->children()) { |
4979 | 3.09k | const auto &childNodeChildren = childNode->GP()->children(); |
4980 | 3.09k | if (childNodeChildren.size() == 2 && |
4981 | 3.09k | ci_equal(childNode->GP()->value(), WKTConstants::PARAMETER) && |
4982 | 3.09k | childNodeChildren[0]->GP()->value() == "\"Vertical_Shift\"") { |
4983 | 0 | esriStyle_ = true; |
4984 | 0 | break; |
4985 | 0 | } |
4986 | 3.09k | } |
4987 | | |
4988 | 652 | auto &dynamicNode = nodeP->lookForChild(WKTConstants::DYNAMIC); |
4989 | 652 | auto vdatum = |
4990 | 652 | !isNull(geogDatumNode) |
4991 | 652 | ? VerticalReferenceFrame::create( |
4992 | 14 | PropertyMap() |
4993 | 14 | .set(IdentifiedObject::NAME_KEY, |
4994 | 14 | buildGeodeticReferenceFrame(geogDatumNode, |
4995 | 14 | PrimeMeridian::GREENWICH, |
4996 | 14 | null_node) |
4997 | 14 | ->nameStr()) |
4998 | 14 | .set("VERT_DATUM_TYPE", "2002")) |
4999 | 14 | .as_nullable() |
5000 | 652 | : !isNull(vdatumNode) |
5001 | 638 | ? buildVerticalReferenceFrame(vdatumNode, dynamicNode).as_nullable() |
5002 | 638 | : nullptr; |
5003 | 652 | auto datumEnsemble = |
5004 | 652 | !isNull(ensembleNode) |
5005 | 652 | ? buildDatumEnsemble(ensembleNode, nullptr, false).as_nullable() |
5006 | 652 | : nullptr; |
5007 | | |
5008 | 652 | auto &csNode = nodeP->lookForChild(WKTConstants::CS_); |
5009 | 652 | if (isNull(csNode) && !ci_equal(nodeValue, WKTConstants::VERT_CS) && |
5010 | 652 | !ci_equal(nodeValue, WKTConstants::VERTCS) && |
5011 | 652 | !ci_equal(nodeValue, WKTConstants::BASEVERTCRS)) { |
5012 | 1 | ThrowMissing(WKTConstants::CS_); |
5013 | 1 | } |
5014 | 651 | auto verticalCS = nn_dynamic_pointer_cast<VerticalCS>( |
5015 | 651 | buildCS(csNode, node, UnitOfMeasure::NONE)); |
5016 | 651 | if (!verticalCS) { |
5017 | 0 | ThrowNotExpectedCSType(VerticalCS::WKT2_TYPE); |
5018 | 0 | } |
5019 | | |
5020 | 651 | if (vdatum && vdatum->getWKT1DatumType() == "2002" && |
5021 | 651 | &(verticalCS->axisList()[0]->direction()) == &(AxisDirection::UP)) { |
5022 | 13 | verticalCS = |
5023 | 13 | VerticalCS::create( |
5024 | 13 | util::PropertyMap(), |
5025 | 13 | CoordinateSystemAxis::create( |
5026 | 13 | util::PropertyMap().set(IdentifiedObject::NAME_KEY, |
5027 | 13 | "ellipsoidal height"), |
5028 | 13 | "h", AxisDirection::UP, verticalCS->axisList()[0]->unit())) |
5029 | 13 | .as_nullable(); |
5030 | 13 | } |
5031 | | |
5032 | 651 | auto &props = buildProperties(node); |
5033 | | |
5034 | 651 | if (esriStyle_ && dbContext_) { |
5035 | 19 | std::string outTableName; |
5036 | 19 | std::string authNameFromAlias; |
5037 | 19 | std::string codeFromAlias; |
5038 | 19 | auto authFactory = |
5039 | 19 | AuthorityFactory::create(NN_NO_CHECK(dbContext_), std::string()); |
5040 | 19 | const std::string vertCRSName = stripQuotes(nodeP->children()[0]); |
5041 | 19 | auto officialName = authFactory->getOfficialNameFromAlias( |
5042 | 19 | vertCRSName, "vertical_crs", "ESRI", false, outTableName, |
5043 | 19 | authNameFromAlias, codeFromAlias); |
5044 | 19 | if (!officialName.empty()) { |
5045 | 0 | props.set(IdentifiedObject::NAME_KEY, officialName); |
5046 | 0 | } |
5047 | 19 | } |
5048 | | |
5049 | | // Deal with Lidar WKT1 VertCRS that embeds geoid model in CRS name, |
5050 | | // following conventions from |
5051 | | // https://pubs.usgs.gov/tm/11b4/pdf/tm11-B4.pdf |
5052 | | // page 9 |
5053 | 651 | if (ci_equal(nodeValue, WKTConstants::VERT_CS) || |
5054 | 651 | ci_equal(nodeValue, WKTConstants::VERTCS)) { |
5055 | 569 | std::string name; |
5056 | 569 | if (props.getStringValue(IdentifiedObject::NAME_KEY, name)) { |
5057 | 569 | std::string geoidName; |
5058 | 569 | for (const char *prefix : |
5059 | 569 | {"NAVD88 - ", "NAVD88 via ", "NAVD88 height - ", |
5060 | 2.12k | "NAVD88 height (ftUS) - "}) { |
5061 | 2.12k | if (starts_with(name, prefix)) { |
5062 | 71 | geoidName = name.substr(strlen(prefix)); |
5063 | 71 | auto pos = geoidName.find_first_of(" ("); |
5064 | 71 | if (pos != std::string::npos) { |
5065 | 54 | geoidName.resize(pos); |
5066 | 54 | } |
5067 | 71 | break; |
5068 | 71 | } |
5069 | 2.12k | } |
5070 | 569 | if (!geoidName.empty()) { |
5071 | 71 | const auto &axis = verticalCS->axisList()[0]; |
5072 | 71 | const auto &dir = axis->direction(); |
5073 | 71 | if (dir == cs::AxisDirection::UP) { |
5074 | 60 | if (axis->unit() == common::UnitOfMeasure::METRE) { |
5075 | 46 | props.set(IdentifiedObject::NAME_KEY, "NAVD88 height"); |
5076 | 46 | props.set(Identifier::CODE_KEY, 5703); |
5077 | 46 | props.set(Identifier::CODESPACE_KEY, Identifier::EPSG); |
5078 | 46 | } else if (axis->unit().name() == "US survey foot") { |
5079 | 0 | props.set(IdentifiedObject::NAME_KEY, |
5080 | 0 | "NAVD88 height (ftUS)"); |
5081 | 0 | props.set(Identifier::CODE_KEY, 6360); |
5082 | 0 | props.set(Identifier::CODESPACE_KEY, Identifier::EPSG); |
5083 | 0 | } |
5084 | 60 | } |
5085 | 71 | PropertyMap propsModel; |
5086 | 71 | propsModel.set(IdentifiedObject::NAME_KEY, toupper(geoidName)); |
5087 | 71 | PropertyMap propsDatum; |
5088 | 71 | propsDatum.set(IdentifiedObject::NAME_KEY, |
5089 | 71 | "North American Vertical Datum 1988"); |
5090 | 71 | propsDatum.set(Identifier::CODE_KEY, 5103); |
5091 | 71 | propsDatum.set(Identifier::CODESPACE_KEY, Identifier::EPSG); |
5092 | 71 | vdatum = |
5093 | 71 | VerticalReferenceFrame::create(propsDatum).as_nullable(); |
5094 | 71 | const auto dummyCRS = |
5095 | 71 | VerticalCRS::create(PropertyMap(), vdatum, datumEnsemble, |
5096 | 71 | NN_NO_CHECK(verticalCS)); |
5097 | 71 | const auto model(Transformation::create( |
5098 | 71 | propsModel, dummyCRS, dummyCRS, nullptr, |
5099 | 71 | OperationMethod::create( |
5100 | 71 | PropertyMap(), std::vector<OperationParameterNNPtr>()), |
5101 | 71 | {}, {})); |
5102 | 71 | props.set("GEOID_MODEL", model); |
5103 | 71 | } |
5104 | 569 | } |
5105 | 569 | } |
5106 | | |
5107 | 651 | auto &geoidModelNode = nodeP->lookForChild(WKTConstants::GEOIDMODEL); |
5108 | 651 | if (!isNull(geoidModelNode)) { |
5109 | 83 | ArrayOfBaseObjectNNPtr arrayModels = ArrayOfBaseObject::create(); |
5110 | 467 | for (const auto &childNode : nodeP->children()) { |
5111 | 467 | const auto &childNodeChildren = childNode->GP()->children(); |
5112 | 467 | if (childNodeChildren.size() >= 1 && |
5113 | 467 | ci_equal(childNode->GP()->value(), WKTConstants::GEOIDMODEL)) { |
5114 | 89 | auto &propsModel = buildProperties(childNode); |
5115 | 89 | const auto dummyCRS = |
5116 | 89 | VerticalCRS::create(PropertyMap(), vdatum, datumEnsemble, |
5117 | 89 | NN_NO_CHECK(verticalCS)); |
5118 | 89 | const auto model(Transformation::create( |
5119 | 89 | propsModel, dummyCRS, dummyCRS, nullptr, |
5120 | 89 | OperationMethod::create( |
5121 | 89 | PropertyMap(), std::vector<OperationParameterNNPtr>()), |
5122 | 89 | {}, {})); |
5123 | 89 | arrayModels->add(model); |
5124 | 89 | } |
5125 | 467 | } |
5126 | 83 | props.set("GEOID_MODEL", arrayModels); |
5127 | 83 | } |
5128 | | |
5129 | 651 | auto crs = nn_static_pointer_cast<CRS>(VerticalCRS::create( |
5130 | 651 | props, vdatum, datumEnsemble, NN_NO_CHECK(verticalCS))); |
5131 | | |
5132 | 651 | if (!isNull(vdatumNode)) { |
5133 | 546 | auto &extensionNode = vdatumNode->lookForChild(WKTConstants::EXTENSION); |
5134 | 546 | const auto &extensionChildren = extensionNode->GP()->children(); |
5135 | 546 | if (extensionChildren.size() == 2) { |
5136 | 7 | if (ci_equal(stripQuotes(extensionChildren[0]), "PROJ4_GRIDS")) { |
5137 | 6 | const auto gridName(stripQuotes(extensionChildren[1])); |
5138 | | // This is the expansion of EPSG:5703 by old GDAL versions. |
5139 | | // See |
5140 | | // https://trac.osgeo.org/metacrs/changeset?reponame=&new=2281%40geotiff%2Ftrunk%2Flibgeotiff%2Fcsv%2Fvertcs.override.csv&old=1893%40geotiff%2Ftrunk%2Flibgeotiff%2Fcsv%2Fvertcs.override.csv |
5141 | | // It is unlikely that the user really explicitly wants this. |
5142 | 6 | if (gridName != "g2003conus.gtx,g2003alaska.gtx," |
5143 | 6 | "g2003h01.gtx,g2003p01.gtx" && |
5144 | 6 | gridName != "g2012a_conus.gtx,g2012a_alaska.gtx," |
5145 | 6 | "g2012a_guam.gtx,g2012a_hawaii.gtx," |
5146 | 6 | "g2012a_puertorico.gtx,g2012a_samoa.gtx") { |
5147 | 6 | auto geogCRS = |
5148 | 6 | geogCRSOfCompoundCRS_ && |
5149 | 6 | geogCRSOfCompoundCRS_->primeMeridian() |
5150 | 0 | ->longitude() |
5151 | 0 | .getSIValue() == 0 && |
5152 | 6 | geogCRSOfCompoundCRS_->coordinateSystem() |
5153 | 0 | ->axisList()[0] |
5154 | 0 | ->unit() == UnitOfMeasure::DEGREE |
5155 | 6 | ? geogCRSOfCompoundCRS_->promoteTo3D(std::string(), |
5156 | 0 | dbContext_) |
5157 | 6 | : GeographicCRS::EPSG_4979; |
5158 | | |
5159 | 6 | auto sourceTransformationCRS = |
5160 | 6 | createBoundCRSSourceTransformationCRS( |
5161 | 6 | crs.as_nullable(), geogCRS.as_nullable()); |
5162 | 6 | auto transformation = Transformation:: |
5163 | 6 | createGravityRelatedHeightToGeographic3D( |
5164 | 6 | PropertyMap().set( |
5165 | 6 | IdentifiedObject::NAME_KEY, |
5166 | 6 | sourceTransformationCRS->nameStr() + " to " + |
5167 | 6 | geogCRS->nameStr() + " ellipsoidal height"), |
5168 | 6 | sourceTransformationCRS, geogCRS, nullptr, gridName, |
5169 | 6 | std::vector<PositionalAccuracyNNPtr>()); |
5170 | 6 | return nn_static_pointer_cast<CRS>( |
5171 | 6 | BoundCRS::create(crs, geogCRS, transformation)); |
5172 | 6 | } |
5173 | 6 | } |
5174 | 7 | } |
5175 | 546 | } |
5176 | | |
5177 | 645 | return crs; |
5178 | 651 | } |
5179 | | |
5180 | | // --------------------------------------------------------------------------- |
5181 | | |
5182 | | DerivedVerticalCRSNNPtr |
5183 | 1 | WKTParser::Private::buildDerivedVerticalCRS(const WKTNodeNNPtr &node) { |
5184 | 1 | const auto *nodeP = node->GP(); |
5185 | 1 | auto &baseVertCRSNode = nodeP->lookForChild(WKTConstants::BASEVERTCRS); |
5186 | | // given the constraints enforced on calling code path |
5187 | 1 | assert(!isNull(baseVertCRSNode)); |
5188 | | |
5189 | 1 | auto baseVertCRS_tmp = buildVerticalCRS(baseVertCRSNode); |
5190 | 1 | auto baseVertCRS = NN_NO_CHECK(baseVertCRS_tmp->extractVerticalCRS()); |
5191 | | |
5192 | 1 | auto &derivingConversionNode = |
5193 | 1 | nodeP->lookForChild(WKTConstants::DERIVINGCONVERSION); |
5194 | 1 | if (isNull(derivingConversionNode)) { |
5195 | 0 | ThrowMissing(WKTConstants::DERIVINGCONVERSION); |
5196 | 0 | } |
5197 | 1 | auto derivingConversion = buildConversion( |
5198 | 1 | derivingConversionNode, UnitOfMeasure::NONE, UnitOfMeasure::NONE); |
5199 | | |
5200 | 1 | auto &csNode = nodeP->lookForChild(WKTConstants::CS_); |
5201 | 1 | if (isNull(csNode)) { |
5202 | 0 | ThrowMissing(WKTConstants::CS_); |
5203 | 0 | } |
5204 | 1 | auto cs = buildCS(csNode, node, UnitOfMeasure::NONE); |
5205 | | |
5206 | 1 | auto verticalCS = nn_dynamic_pointer_cast<VerticalCS>(cs); |
5207 | 1 | if (!verticalCS) { |
5208 | 0 | throw ParsingException( |
5209 | 0 | concat("vertical CS expected, but found ", cs->getWKT2Type(true))); |
5210 | 0 | } |
5211 | | |
5212 | 1 | return DerivedVerticalCRS::create(buildProperties(node), baseVertCRS, |
5213 | 1 | derivingConversion, |
5214 | 1 | NN_NO_CHECK(verticalCS)); |
5215 | 1 | } |
5216 | | |
5217 | | // --------------------------------------------------------------------------- |
5218 | | |
5219 | 280 | CRSNNPtr WKTParser::Private::buildCompoundCRS(const WKTNodeNNPtr &node) { |
5220 | 280 | std::vector<CRSNNPtr> components; |
5221 | 280 | bool bFirstNode = true; |
5222 | 1.96k | for (const auto &child : node->GP()->children()) { |
5223 | 1.96k | auto crs = buildCRS(child); |
5224 | 1.96k | if (crs) { |
5225 | 734 | if (bFirstNode) { |
5226 | 238 | geogCRSOfCompoundCRS_ = crs->extractGeographicCRS(); |
5227 | 238 | bFirstNode = false; |
5228 | 238 | } |
5229 | 734 | components.push_back(NN_NO_CHECK(crs)); |
5230 | 734 | } |
5231 | 1.96k | } |
5232 | | |
5233 | 280 | if (ci_equal(node->GP()->value(), WKTConstants::COMPD_CS)) { |
5234 | 178 | return CompoundCRS::createLax(buildProperties(node), components, |
5235 | 178 | dbContext_); |
5236 | 178 | } else { |
5237 | 102 | return CompoundCRS::create(buildProperties(node), components); |
5238 | 102 | } |
5239 | 280 | } |
5240 | | |
5241 | | // --------------------------------------------------------------------------- |
5242 | | |
5243 | | static TransformationNNPtr buildTransformationForBoundCRS( |
5244 | | DatabaseContextPtr &dbContext, |
5245 | | const util::PropertyMap &abridgedNodeProperties, |
5246 | | const util::PropertyMap &methodNodeProperties, const CRSNNPtr &sourceCRS, |
5247 | | const CRSNNPtr &targetCRS, std::vector<OperationParameterNNPtr> ¶meters, |
5248 | 0 | std::vector<ParameterValueNNPtr> &values) { |
5249 | |
|
5250 | 0 | auto interpolationCRS = dealWithEPSGCodeForInterpolationCRSParameter( |
5251 | 0 | dbContext, parameters, values); |
5252 | |
|
5253 | 0 | const auto sourceTransformationCRS( |
5254 | 0 | createBoundCRSSourceTransformationCRS(sourceCRS, targetCRS)); |
5255 | 0 | auto transformation = Transformation::create( |
5256 | 0 | abridgedNodeProperties, sourceTransformationCRS, targetCRS, |
5257 | 0 | interpolationCRS, methodNodeProperties, parameters, values, |
5258 | 0 | std::vector<PositionalAccuracyNNPtr>()); |
5259 | | |
5260 | | // If the transformation is a "Geographic3D to GravityRelatedHeight" one, |
5261 | | // then the sourceCRS is expected to be a GeographicCRS and the target a |
5262 | | // VerticalCRS. Due to how things work in a BoundCRS, we have the opposite, |
5263 | | // so use our "GravityRelatedHeight to Geographic3D" method instead. |
5264 | 0 | if (Transformation::isGeographic3DToGravityRelatedHeight( |
5265 | 0 | transformation->method(), true) && |
5266 | 0 | dynamic_cast<VerticalCRS *>(sourceTransformationCRS.get()) && |
5267 | 0 | dynamic_cast<GeographicCRS *>(targetCRS.get())) { |
5268 | 0 | auto fileParameter = transformation->parameterValue( |
5269 | 0 | EPSG_NAME_PARAMETER_GEOID_CORRECTION_FILENAME, |
5270 | 0 | EPSG_CODE_PARAMETER_GEOID_CORRECTION_FILENAME); |
5271 | 0 | if (fileParameter && |
5272 | 0 | fileParameter->type() == ParameterValue::Type::FILENAME) { |
5273 | 0 | const auto &filename = fileParameter->valueFile(); |
5274 | |
|
5275 | 0 | transformation = |
5276 | 0 | Transformation::createGravityRelatedHeightToGeographic3D( |
5277 | 0 | abridgedNodeProperties, sourceTransformationCRS, targetCRS, |
5278 | 0 | interpolationCRS, filename, |
5279 | 0 | std::vector<PositionalAccuracyNNPtr>()); |
5280 | 0 | } |
5281 | 0 | } |
5282 | 0 | return transformation; |
5283 | 0 | } |
5284 | | |
5285 | | // --------------------------------------------------------------------------- |
5286 | | |
5287 | 9 | BoundCRSNNPtr WKTParser::Private::buildBoundCRS(const WKTNodeNNPtr &node) { |
5288 | 9 | const auto *nodeP = node->GP(); |
5289 | 9 | auto &abridgedNode = |
5290 | 9 | nodeP->lookForChild(WKTConstants::ABRIDGEDTRANSFORMATION); |
5291 | 9 | if (isNull(abridgedNode)) { |
5292 | 3 | ThrowNotEnoughChildren(WKTConstants::ABRIDGEDTRANSFORMATION); |
5293 | 3 | } |
5294 | | |
5295 | 6 | auto &methodNode = abridgedNode->GP()->lookForChild(WKTConstants::METHOD); |
5296 | 6 | if (isNull(methodNode)) { |
5297 | 6 | ThrowMissing(WKTConstants::METHOD); |
5298 | 6 | } |
5299 | 0 | if (methodNode->GP()->childrenSize() == 0) { |
5300 | 0 | ThrowNotEnoughChildren(WKTConstants::METHOD); |
5301 | 0 | } |
5302 | | |
5303 | 0 | auto &sourceCRSNode = nodeP->lookForChild(WKTConstants::SOURCECRS); |
5304 | 0 | const auto &sourceCRSNodeChildren = sourceCRSNode->GP()->children(); |
5305 | 0 | if (sourceCRSNodeChildren.size() != 1) { |
5306 | 0 | ThrowNotEnoughChildren(WKTConstants::SOURCECRS); |
5307 | 0 | } |
5308 | 0 | auto sourceCRS = buildCRS(sourceCRSNodeChildren[0]); |
5309 | 0 | if (!sourceCRS) { |
5310 | 0 | throw ParsingException("Invalid content in SOURCECRS node"); |
5311 | 0 | } |
5312 | | |
5313 | 0 | auto &targetCRSNode = nodeP->lookForChild(WKTConstants::TARGETCRS); |
5314 | 0 | const auto &targetCRSNodeChildren = targetCRSNode->GP()->children(); |
5315 | 0 | if (targetCRSNodeChildren.size() != 1) { |
5316 | 0 | ThrowNotEnoughChildren(WKTConstants::TARGETCRS); |
5317 | 0 | } |
5318 | 0 | auto targetCRS = buildCRS(targetCRSNodeChildren[0]); |
5319 | 0 | if (!targetCRS) { |
5320 | 0 | throw ParsingException("Invalid content in TARGETCRS node"); |
5321 | 0 | } |
5322 | | |
5323 | 0 | std::vector<OperationParameterNNPtr> parameters; |
5324 | 0 | std::vector<ParameterValueNNPtr> values; |
5325 | 0 | const auto &defaultLinearUnit = UnitOfMeasure::NONE; |
5326 | 0 | const auto &defaultAngularUnit = UnitOfMeasure::NONE; |
5327 | 0 | consumeParameters(abridgedNode, true, parameters, values, defaultLinearUnit, |
5328 | 0 | defaultAngularUnit); |
5329 | |
|
5330 | 0 | const auto nnSourceCRS = NN_NO_CHECK(sourceCRS); |
5331 | 0 | const auto nnTargetCRS = NN_NO_CHECK(targetCRS); |
5332 | 0 | const auto transformation = buildTransformationForBoundCRS( |
5333 | 0 | dbContext_, buildProperties(abridgedNode), buildProperties(methodNode), |
5334 | 0 | nnSourceCRS, nnTargetCRS, parameters, values); |
5335 | |
|
5336 | 0 | return BoundCRS::create(buildProperties(node, false, false), |
5337 | 0 | NN_NO_CHECK(sourceCRS), NN_NO_CHECK(targetCRS), |
5338 | 0 | transformation); |
5339 | 0 | } |
5340 | | |
5341 | | // --------------------------------------------------------------------------- |
5342 | | |
5343 | | TemporalCSNNPtr |
5344 | 8 | WKTParser::Private::buildTemporalCS(const WKTNodeNNPtr &parentNode) { |
5345 | | |
5346 | 8 | auto &csNode = parentNode->GP()->lookForChild(WKTConstants::CS_); |
5347 | 8 | if (isNull(csNode) && |
5348 | 8 | !ci_equal(parentNode->GP()->value(), WKTConstants::BASETIMECRS)) { |
5349 | 4 | ThrowMissing(WKTConstants::CS_); |
5350 | 4 | } |
5351 | 4 | auto cs = buildCS(csNode, parentNode, UnitOfMeasure::NONE); |
5352 | 4 | auto temporalCS = nn_dynamic_pointer_cast<TemporalCS>(cs); |
5353 | 4 | if (!temporalCS) { |
5354 | 0 | ThrowNotExpectedCSType(TemporalCS::WKT2_2015_TYPE); |
5355 | 0 | } |
5356 | 4 | return NN_NO_CHECK(temporalCS); |
5357 | 4 | } |
5358 | | |
5359 | | // --------------------------------------------------------------------------- |
5360 | | |
5361 | | TemporalCRSNNPtr |
5362 | 15 | WKTParser::Private::buildTemporalCRS(const WKTNodeNNPtr &node) { |
5363 | 15 | auto &datumNode = |
5364 | 15 | node->GP()->lookForChild(WKTConstants::TDATUM, WKTConstants::TIMEDATUM); |
5365 | 15 | if (isNull(datumNode)) { |
5366 | 7 | throw ParsingException("Missing TDATUM / TIMEDATUM node"); |
5367 | 7 | } |
5368 | | |
5369 | 8 | return TemporalCRS::create(buildProperties(node), |
5370 | 8 | buildTemporalDatum(datumNode), |
5371 | 8 | buildTemporalCS(node)); |
5372 | 15 | } |
5373 | | |
5374 | | // --------------------------------------------------------------------------- |
5375 | | |
5376 | | DerivedTemporalCRSNNPtr |
5377 | 2 | WKTParser::Private::buildDerivedTemporalCRS(const WKTNodeNNPtr &node) { |
5378 | 2 | const auto *nodeP = node->GP(); |
5379 | 2 | auto &baseCRSNode = nodeP->lookForChild(WKTConstants::BASETIMECRS); |
5380 | | // given the constraints enforced on calling code path |
5381 | 2 | assert(!isNull(baseCRSNode)); |
5382 | | |
5383 | 2 | auto &derivingConversionNode = |
5384 | 2 | nodeP->lookForChild(WKTConstants::DERIVINGCONVERSION); |
5385 | 2 | if (isNull(derivingConversionNode)) { |
5386 | 2 | ThrowNotEnoughChildren(WKTConstants::DERIVINGCONVERSION); |
5387 | 2 | } |
5388 | | |
5389 | 0 | return DerivedTemporalCRS::create( |
5390 | 0 | buildProperties(node), buildTemporalCRS(baseCRSNode), |
5391 | 0 | buildConversion(derivingConversionNode, UnitOfMeasure::NONE, |
5392 | 0 | UnitOfMeasure::NONE), |
5393 | 0 | buildTemporalCS(node)); |
5394 | 2 | } |
5395 | | |
5396 | | // --------------------------------------------------------------------------- |
5397 | | |
5398 | | EngineeringCRSNNPtr |
5399 | 8 | WKTParser::Private::buildEngineeringCRS(const WKTNodeNNPtr &node) { |
5400 | 8 | const auto *nodeP = node->GP(); |
5401 | 8 | auto &datumNode = nodeP->lookForChild(WKTConstants::EDATUM, |
5402 | 8 | WKTConstants::ENGINEERINGDATUM); |
5403 | 8 | if (isNull(datumNode)) { |
5404 | 2 | throw ParsingException("Missing EDATUM / ENGINEERINGDATUM node"); |
5405 | 2 | } |
5406 | | |
5407 | 6 | auto &csNode = nodeP->lookForChild(WKTConstants::CS_); |
5408 | 6 | if (isNull(csNode) && !ci_equal(nodeP->value(), WKTConstants::BASEENGCRS)) { |
5409 | 6 | ThrowMissing(WKTConstants::CS_); |
5410 | 6 | } |
5411 | | |
5412 | 0 | auto cs = buildCS(csNode, node, UnitOfMeasure::NONE); |
5413 | 0 | return EngineeringCRS::create(buildProperties(node), |
5414 | 0 | buildEngineeringDatum(datumNode), cs); |
5415 | 6 | } |
5416 | | |
5417 | | // --------------------------------------------------------------------------- |
5418 | | |
5419 | | EngineeringCRSNNPtr |
5420 | 352 | WKTParser::Private::buildEngineeringCRSFromLocalCS(const WKTNodeNNPtr &node) { |
5421 | 352 | auto &datumNode = node->GP()->lookForChild(WKTConstants::LOCAL_DATUM); |
5422 | 352 | auto cs = buildCS(null_node, node, UnitOfMeasure::NONE); |
5423 | 352 | auto datum = EngineeringDatum::create( |
5424 | 352 | !isNull(datumNode) |
5425 | 352 | ? buildProperties(datumNode) |
5426 | 352 | : |
5427 | | // In theory OGC 01-009 mandates LOCAL_DATUM, but GDAL |
5428 | | // has a tradition of emitting just LOCAL_CS["foo"] |
5429 | 352 | []() { |
5430 | 286 | PropertyMap map; |
5431 | 286 | map.set(IdentifiedObject::NAME_KEY, UNKNOWN_ENGINEERING_DATUM); |
5432 | 286 | return map; |
5433 | 286 | }()); |
5434 | 352 | return EngineeringCRS::create(buildProperties(node), datum, cs); |
5435 | 352 | } |
5436 | | |
5437 | | // --------------------------------------------------------------------------- |
5438 | | |
5439 | | DerivedEngineeringCRSNNPtr |
5440 | 0 | WKTParser::Private::buildDerivedEngineeringCRS(const WKTNodeNNPtr &node) { |
5441 | 0 | const auto *nodeP = node->GP(); |
5442 | 0 | auto &baseEngCRSNode = nodeP->lookForChild(WKTConstants::BASEENGCRS); |
5443 | | // given the constraints enforced on calling code path |
5444 | 0 | assert(!isNull(baseEngCRSNode)); |
5445 | |
|
5446 | 0 | auto baseEngCRS = buildEngineeringCRS(baseEngCRSNode); |
5447 | |
|
5448 | 0 | auto &derivingConversionNode = |
5449 | 0 | nodeP->lookForChild(WKTConstants::DERIVINGCONVERSION); |
5450 | 0 | if (isNull(derivingConversionNode)) { |
5451 | 0 | ThrowNotEnoughChildren(WKTConstants::DERIVINGCONVERSION); |
5452 | 0 | } |
5453 | 0 | auto derivingConversion = buildConversion( |
5454 | 0 | derivingConversionNode, UnitOfMeasure::NONE, UnitOfMeasure::NONE); |
5455 | |
|
5456 | 0 | auto &csNode = nodeP->lookForChild(WKTConstants::CS_); |
5457 | 0 | if (isNull(csNode)) { |
5458 | 0 | ThrowMissing(WKTConstants::CS_); |
5459 | 0 | } |
5460 | 0 | auto cs = buildCS(csNode, node, UnitOfMeasure::NONE); |
5461 | |
|
5462 | 0 | return DerivedEngineeringCRS::create(buildProperties(node), baseEngCRS, |
5463 | 0 | derivingConversion, cs); |
5464 | 0 | } |
5465 | | |
5466 | | // --------------------------------------------------------------------------- |
5467 | | |
5468 | | ParametricCSNNPtr |
5469 | 0 | WKTParser::Private::buildParametricCS(const WKTNodeNNPtr &parentNode) { |
5470 | |
|
5471 | 0 | auto &csNode = parentNode->GP()->lookForChild(WKTConstants::CS_); |
5472 | 0 | if (isNull(csNode) && |
5473 | 0 | !ci_equal(parentNode->GP()->value(), WKTConstants::BASEPARAMCRS)) { |
5474 | 0 | ThrowMissing(WKTConstants::CS_); |
5475 | 0 | } |
5476 | 0 | auto cs = buildCS(csNode, parentNode, UnitOfMeasure::NONE); |
5477 | 0 | auto parametricCS = nn_dynamic_pointer_cast<ParametricCS>(cs); |
5478 | 0 | if (!parametricCS) { |
5479 | 0 | ThrowNotExpectedCSType(ParametricCS::WKT2_TYPE); |
5480 | 0 | } |
5481 | 0 | return NN_NO_CHECK(parametricCS); |
5482 | 0 | } |
5483 | | |
5484 | | // --------------------------------------------------------------------------- |
5485 | | |
5486 | | ParametricCRSNNPtr |
5487 | 12 | WKTParser::Private::buildParametricCRS(const WKTNodeNNPtr &node) { |
5488 | 12 | auto &datumNode = node->GP()->lookForChild(WKTConstants::PDATUM, |
5489 | 12 | WKTConstants::PARAMETRICDATUM); |
5490 | 12 | if (isNull(datumNode)) { |
5491 | 12 | throw ParsingException("Missing PDATUM / PARAMETRICDATUM node"); |
5492 | 12 | } |
5493 | | |
5494 | 0 | return ParametricCRS::create(buildProperties(node), |
5495 | 0 | buildParametricDatum(datumNode), |
5496 | 0 | buildParametricCS(node)); |
5497 | 12 | } |
5498 | | |
5499 | | // --------------------------------------------------------------------------- |
5500 | | |
5501 | | DerivedParametricCRSNNPtr |
5502 | 4 | WKTParser::Private::buildDerivedParametricCRS(const WKTNodeNNPtr &node) { |
5503 | 4 | const auto *nodeP = node->GP(); |
5504 | 4 | auto &baseParamCRSNode = nodeP->lookForChild(WKTConstants::BASEPARAMCRS); |
5505 | | // given the constraints enforced on calling code path |
5506 | 4 | assert(!isNull(baseParamCRSNode)); |
5507 | | |
5508 | 4 | auto &derivingConversionNode = |
5509 | 4 | nodeP->lookForChild(WKTConstants::DERIVINGCONVERSION); |
5510 | 4 | if (isNull(derivingConversionNode)) { |
5511 | 4 | ThrowNotEnoughChildren(WKTConstants::DERIVINGCONVERSION); |
5512 | 4 | } |
5513 | | |
5514 | 0 | return DerivedParametricCRS::create( |
5515 | 0 | buildProperties(node), buildParametricCRS(baseParamCRSNode), |
5516 | 0 | buildConversion(derivingConversionNode, UnitOfMeasure::NONE, |
5517 | 0 | UnitOfMeasure::NONE), |
5518 | 0 | buildParametricCS(node)); |
5519 | 4 | } |
5520 | | |
5521 | | // --------------------------------------------------------------------------- |
5522 | | |
5523 | | DerivedProjectedCRSNNPtr |
5524 | 6 | WKTParser::Private::buildDerivedProjectedCRS(const WKTNodeNNPtr &node) { |
5525 | 6 | const auto *nodeP = node->GP(); |
5526 | 6 | auto &baseProjCRSNode = nodeP->lookForChild(WKTConstants::BASEPROJCRS); |
5527 | 6 | if (isNull(baseProjCRSNode)) { |
5528 | 6 | ThrowNotEnoughChildren(WKTConstants::BASEPROJCRS); |
5529 | 6 | } |
5530 | 0 | auto baseProjCRS = buildProjectedCRS(baseProjCRSNode); |
5531 | |
|
5532 | 0 | auto &conversionNode = |
5533 | 0 | nodeP->lookForChild(WKTConstants::DERIVINGCONVERSION); |
5534 | 0 | if (isNull(conversionNode)) { |
5535 | 0 | ThrowNotEnoughChildren(WKTConstants::DERIVINGCONVERSION); |
5536 | 0 | } |
5537 | | |
5538 | 0 | auto linearUnit = buildUnitInSubNode(node); |
5539 | 0 | const auto &angularUnit = |
5540 | 0 | baseProjCRS->baseCRS()->coordinateSystem()->axisList()[0]->unit(); |
5541 | |
|
5542 | 0 | auto conversion = buildConversion(conversionNode, linearUnit, angularUnit); |
5543 | |
|
5544 | 0 | auto &csNode = nodeP->lookForChild(WKTConstants::CS_); |
5545 | 0 | if (isNull(csNode) && !ci_equal(nodeP->value(), WKTConstants::PROJCS)) { |
5546 | 0 | ThrowMissing(WKTConstants::CS_); |
5547 | 0 | } |
5548 | 0 | auto cs = buildCS(csNode, node, UnitOfMeasure::NONE); |
5549 | |
|
5550 | 0 | if (cs->axisList().size() == 3 && |
5551 | 0 | baseProjCRS->coordinateSystem()->axisList().size() == 2) { |
5552 | 0 | baseProjCRS = NN_NO_CHECK(util::nn_dynamic_pointer_cast<ProjectedCRS>( |
5553 | 0 | baseProjCRS->promoteTo3D(std::string(), dbContext_))); |
5554 | 0 | } |
5555 | |
|
5556 | 0 | return DerivedProjectedCRS::create(buildProperties(node), baseProjCRS, |
5557 | 0 | conversion, cs); |
5558 | 0 | } |
5559 | | |
5560 | | // --------------------------------------------------------------------------- |
5561 | | |
5562 | | CoordinateMetadataNNPtr |
5563 | 6 | WKTParser::Private::buildCoordinateMetadata(const WKTNodeNNPtr &node) { |
5564 | 6 | const auto *nodeP = node->GP(); |
5565 | | |
5566 | 6 | const auto &l_children = nodeP->children(); |
5567 | 6 | if (l_children.empty()) { |
5568 | 0 | ThrowNotEnoughChildren(WKTConstants::COORDINATEMETADATA); |
5569 | 0 | } |
5570 | | |
5571 | 6 | auto crs = buildCRS(l_children[0]); |
5572 | 6 | if (!crs) { |
5573 | 4 | throw ParsingException("Invalid content in CRS node"); |
5574 | 4 | } |
5575 | | |
5576 | 2 | auto &epochNode = nodeP->lookForChild(WKTConstants::EPOCH); |
5577 | 2 | if (!isNull(epochNode)) { |
5578 | 0 | const auto &epochChildren = epochNode->GP()->children(); |
5579 | 0 | if (epochChildren.empty()) { |
5580 | 0 | ThrowMissing(WKTConstants::EPOCH); |
5581 | 0 | } |
5582 | 0 | double coordinateEpoch; |
5583 | 0 | try { |
5584 | 0 | coordinateEpoch = asDouble(epochChildren[0]); |
5585 | 0 | } catch (const std::exception &) { |
5586 | 0 | throw ParsingException("Invalid EPOCH node"); |
5587 | 0 | } |
5588 | 0 | return CoordinateMetadata::create(NN_NO_CHECK(crs), coordinateEpoch, |
5589 | 0 | dbContext_); |
5590 | 0 | } |
5591 | | |
5592 | 2 | return CoordinateMetadata::create(NN_NO_CHECK(crs)); |
5593 | 2 | } |
5594 | | |
5595 | | // --------------------------------------------------------------------------- |
5596 | | |
5597 | 4.18k | static bool isGeodeticCRS(const std::string &name) { |
5598 | 4.18k | return ci_equal(name, WKTConstants::GEODCRS) || // WKT2 |
5599 | 4.18k | ci_equal(name, WKTConstants::GEODETICCRS) || // WKT2 |
5600 | 4.18k | ci_equal(name, WKTConstants::GEOGCRS) || // WKT2 2019 |
5601 | 4.18k | ci_equal(name, WKTConstants::GEOGRAPHICCRS) || // WKT2 2019 |
5602 | 4.18k | ci_equal(name, WKTConstants::GEOGCS) || // WKT1 |
5603 | 4.18k | ci_equal(name, WKTConstants::GEOCCS); // WKT1 |
5604 | 4.18k | } |
5605 | | |
5606 | | // --------------------------------------------------------------------------- |
5607 | | |
5608 | 4.18k | CRSPtr WKTParser::Private::buildCRS(const WKTNodeNNPtr &node) { |
5609 | 4.18k | const auto *nodeP = node->GP(); |
5610 | 4.18k | const std::string &name(nodeP->value()); |
5611 | | |
5612 | 4.18k | const auto applyHorizontalBoundCRSParams = [&](const CRSNNPtr &crs) { |
5613 | 763 | if (!toWGS84Parameters_.empty()) { |
5614 | 2 | auto ret = BoundCRS::createFromTOWGS84(crs, toWGS84Parameters_); |
5615 | 2 | toWGS84Parameters_.clear(); |
5616 | 2 | return util::nn_static_pointer_cast<CRS>(ret); |
5617 | 761 | } else if (!datumPROJ4Grids_.empty()) { |
5618 | 1 | auto ret = BoundCRS::createFromNadgrids(crs, datumPROJ4Grids_); |
5619 | 1 | datumPROJ4Grids_.clear(); |
5620 | 1 | return util::nn_static_pointer_cast<CRS>(ret); |
5621 | 1 | } |
5622 | 760 | return crs; |
5623 | 763 | }; |
5624 | | |
5625 | 4.18k | if (isGeodeticCRS(name)) { |
5626 | 525 | if (!isNull(nodeP->lookForChild(WKTConstants::BASEGEOGCRS, |
5627 | 525 | WKTConstants::BASEGEODCRS))) { |
5628 | 4 | return util::nn_static_pointer_cast<CRS>( |
5629 | 4 | applyHorizontalBoundCRSParams(buildDerivedGeodeticCRS(node))); |
5630 | 521 | } else { |
5631 | 521 | return util::nn_static_pointer_cast<CRS>( |
5632 | 521 | applyHorizontalBoundCRSParams(buildGeodeticCRS(node))); |
5633 | 521 | } |
5634 | 525 | } |
5635 | | |
5636 | 3.65k | if (ci_equal(name, WKTConstants::PROJCS) || |
5637 | 3.65k | ci_equal(name, WKTConstants::PROJCRS) || |
5638 | 3.65k | ci_equal(name, WKTConstants::PROJECTEDCRS)) { |
5639 | | // Get the EXTENSION "PROJ4" node before attempting to call |
5640 | | // buildProjectedCRS() since formulations of WKT1_GDAL from GDAL 2.x |
5641 | | // with the netCDF driver and the lack the required UNIT[] node |
5642 | 734 | std::string projString = getExtensionProj4(nodeP); |
5643 | 734 | if (!projString.empty() && |
5644 | 734 | (starts_with(projString, "+proj=ob_tran +o_proj=longlat") || |
5645 | 103 | starts_with(projString, "+proj=ob_tran +o_proj=lonlat") || |
5646 | 103 | starts_with(projString, "+proj=ob_tran +o_proj=latlong") || |
5647 | 103 | starts_with(projString, "+proj=ob_tran +o_proj=latlon"))) { |
5648 | | // Those are not a projected CRS, but a DerivedGeographic one... |
5649 | 70 | if (projString.find(" +type=crs") == std::string::npos) { |
5650 | 70 | projString += " +type=crs"; |
5651 | 70 | } |
5652 | 70 | try { |
5653 | 70 | auto projObj = |
5654 | 70 | PROJStringParser().createFromPROJString(projString); |
5655 | 70 | auto crs = nn_dynamic_pointer_cast<CRS>(projObj); |
5656 | 70 | if (crs) { |
5657 | 31 | return util::nn_static_pointer_cast<CRS>( |
5658 | 31 | applyHorizontalBoundCRSParams(NN_NO_CHECK(crs))); |
5659 | 31 | } |
5660 | 70 | } catch (const io::ParsingException &) { |
5661 | 32 | } |
5662 | 70 | } |
5663 | 700 | return util::nn_static_pointer_cast<CRS>( |
5664 | 700 | applyHorizontalBoundCRSParams(buildProjectedCRS(node))); |
5665 | 734 | } |
5666 | | |
5667 | 2.92k | if (ci_equal(name, WKTConstants::VERT_CS) || |
5668 | 2.92k | ci_equal(name, WKTConstants::VERTCS) || |
5669 | 2.92k | ci_equal(name, WKTConstants::VERTCRS) || |
5670 | 2.92k | ci_equal(name, WKTConstants::VERTICALCRS)) { |
5671 | 562 | if (!isNull(nodeP->lookForChild(WKTConstants::BASEVERTCRS))) { |
5672 | 1 | return util::nn_static_pointer_cast<CRS>( |
5673 | 1 | buildDerivedVerticalCRS(node)); |
5674 | 561 | } else { |
5675 | 561 | return util::nn_static_pointer_cast<CRS>(buildVerticalCRS(node)); |
5676 | 561 | } |
5677 | 562 | } |
5678 | | |
5679 | 2.36k | if (ci_equal(name, WKTConstants::COMPD_CS) || |
5680 | 2.36k | ci_equal(name, WKTConstants::COMPOUNDCRS)) { |
5681 | 280 | return util::nn_static_pointer_cast<CRS>(buildCompoundCRS(node)); |
5682 | 280 | } |
5683 | | |
5684 | 2.08k | if (ci_equal(name, WKTConstants::BOUNDCRS)) { |
5685 | 9 | return util::nn_static_pointer_cast<CRS>(buildBoundCRS(node)); |
5686 | 9 | } |
5687 | | |
5688 | 2.07k | if (ci_equal(name, WKTConstants::TIMECRS)) { |
5689 | 17 | if (!isNull(nodeP->lookForChild(WKTConstants::BASETIMECRS))) { |
5690 | 2 | return util::nn_static_pointer_cast<CRS>( |
5691 | 2 | buildDerivedTemporalCRS(node)); |
5692 | 15 | } else { |
5693 | 15 | return util::nn_static_pointer_cast<CRS>(buildTemporalCRS(node)); |
5694 | 15 | } |
5695 | 17 | } |
5696 | | |
5697 | 2.05k | if (ci_equal(name, WKTConstants::DERIVEDPROJCRS)) { |
5698 | 6 | return util::nn_static_pointer_cast<CRS>( |
5699 | 6 | buildDerivedProjectedCRS(node)); |
5700 | 6 | } |
5701 | | |
5702 | 2.05k | if (ci_equal(name, WKTConstants::ENGCRS) || |
5703 | 2.05k | ci_equal(name, WKTConstants::ENGINEERINGCRS)) { |
5704 | 8 | if (!isNull(nodeP->lookForChild(WKTConstants::BASEENGCRS))) { |
5705 | 0 | return util::nn_static_pointer_cast<CRS>( |
5706 | 0 | buildDerivedEngineeringCRS(node)); |
5707 | 8 | } else { |
5708 | 8 | return util::nn_static_pointer_cast<CRS>(buildEngineeringCRS(node)); |
5709 | 8 | } |
5710 | 8 | } |
5711 | | |
5712 | 2.04k | if (ci_equal(name, WKTConstants::LOCAL_CS)) { |
5713 | 352 | return util::nn_static_pointer_cast<CRS>( |
5714 | 352 | buildEngineeringCRSFromLocalCS(node)); |
5715 | 352 | } |
5716 | | |
5717 | 1.69k | if (ci_equal(name, WKTConstants::PARAMETRICCRS)) { |
5718 | 16 | if (!isNull(nodeP->lookForChild(WKTConstants::BASEPARAMCRS))) { |
5719 | 4 | return util::nn_static_pointer_cast<CRS>( |
5720 | 4 | buildDerivedParametricCRS(node)); |
5721 | 12 | } else { |
5722 | 12 | return util::nn_static_pointer_cast<CRS>(buildParametricCRS(node)); |
5723 | 12 | } |
5724 | 16 | } |
5725 | | |
5726 | 1.67k | return nullptr; |
5727 | 1.69k | } |
5728 | | |
5729 | | // --------------------------------------------------------------------------- |
5730 | | |
5731 | 2.03k | BaseObjectNNPtr WKTParser::Private::build(const WKTNodeNNPtr &node) { |
5732 | 2.03k | const auto *nodeP = node->GP(); |
5733 | 2.03k | const std::string &name(nodeP->value()); |
5734 | | |
5735 | 2.03k | auto crs = buildCRS(node); |
5736 | 2.03k | if (crs) { |
5737 | 651 | return util::nn_static_pointer_cast<BaseObject>(NN_NO_CHECK(crs)); |
5738 | 651 | } |
5739 | | |
5740 | | // Datum handled by caller code WKTParser::createFromWKT() |
5741 | | |
5742 | 1.37k | if (ci_equal(name, WKTConstants::ENSEMBLE)) { |
5743 | 7 | return util::nn_static_pointer_cast<BaseObject>(buildDatumEnsemble( |
5744 | 7 | node, PrimeMeridian::GREENWICH, |
5745 | 7 | !isNull(nodeP->lookForChild(WKTConstants::ELLIPSOID)))); |
5746 | 7 | } |
5747 | | |
5748 | 1.37k | if (ci_equal(name, WKTConstants::VDATUM) || |
5749 | 1.37k | ci_equal(name, WKTConstants::VERT_DATUM) || |
5750 | 1.37k | ci_equal(name, WKTConstants::VERTICALDATUM) || |
5751 | 1.37k | ci_equal(name, WKTConstants::VRF)) { |
5752 | 125 | return util::nn_static_pointer_cast<BaseObject>( |
5753 | 125 | buildVerticalReferenceFrame(node, null_node)); |
5754 | 125 | } |
5755 | | |
5756 | 1.24k | if (ci_equal(name, WKTConstants::TDATUM) || |
5757 | 1.24k | ci_equal(name, WKTConstants::TIMEDATUM)) { |
5758 | 12 | return util::nn_static_pointer_cast<BaseObject>( |
5759 | 12 | buildTemporalDatum(node)); |
5760 | 12 | } |
5761 | | |
5762 | 1.23k | if (ci_equal(name, WKTConstants::EDATUM) || |
5763 | 1.23k | ci_equal(name, WKTConstants::ENGINEERINGDATUM)) { |
5764 | 8 | return util::nn_static_pointer_cast<BaseObject>( |
5765 | 8 | buildEngineeringDatum(node)); |
5766 | 8 | } |
5767 | | |
5768 | 1.22k | if (ci_equal(name, WKTConstants::PDATUM) || |
5769 | 1.22k | ci_equal(name, WKTConstants::PARAMETRICDATUM)) { |
5770 | 5 | return util::nn_static_pointer_cast<BaseObject>( |
5771 | 5 | buildParametricDatum(node)); |
5772 | 5 | } |
5773 | | |
5774 | 1.22k | if (ci_equal(name, WKTConstants::ELLIPSOID) || |
5775 | 1.22k | ci_equal(name, WKTConstants::SPHEROID)) { |
5776 | 5 | return util::nn_static_pointer_cast<BaseObject>(buildEllipsoid(node)); |
5777 | 5 | } |
5778 | | |
5779 | 1.21k | if (ci_equal(name, WKTConstants::COORDINATEOPERATION)) { |
5780 | 15 | auto transf = buildCoordinateOperation(node); |
5781 | | |
5782 | 15 | const char *prefixes[] = { |
5783 | 15 | "PROJ-based operation method: ", |
5784 | 15 | "PROJ-based operation method (approximate): "}; |
5785 | 15 | for (const char *prefix : prefixes) { |
5786 | 0 | if (starts_with(transf->method()->nameStr(), prefix)) { |
5787 | 0 | auto projString = |
5788 | 0 | transf->method()->nameStr().substr(strlen(prefix)); |
5789 | 0 | return util::nn_static_pointer_cast<BaseObject>( |
5790 | 0 | PROJBasedOperation::create( |
5791 | 0 | PropertyMap(), projString, transf->sourceCRS(), |
5792 | 0 | transf->targetCRS(), |
5793 | 0 | transf->coordinateOperationAccuracies())); |
5794 | 0 | } |
5795 | 0 | } |
5796 | | |
5797 | 15 | return util::nn_static_pointer_cast<BaseObject>(transf); |
5798 | 15 | } |
5799 | | |
5800 | 1.20k | if (ci_equal(name, WKTConstants::CONVERSION)) { |
5801 | 315 | auto conv = |
5802 | 315 | buildConversion(node, UnitOfMeasure::METRE, UnitOfMeasure::DEGREE); |
5803 | | |
5804 | 315 | if (starts_with(conv->method()->nameStr(), |
5805 | 315 | "PROJ-based operation method: ")) { |
5806 | 4 | auto projString = conv->method()->nameStr().substr( |
5807 | 4 | strlen("PROJ-based operation method: ")); |
5808 | 4 | return util::nn_static_pointer_cast<BaseObject>( |
5809 | 4 | PROJBasedOperation::create(PropertyMap(), projString, nullptr, |
5810 | 4 | nullptr, {})); |
5811 | 4 | } |
5812 | | |
5813 | 311 | return util::nn_static_pointer_cast<BaseObject>(conv); |
5814 | 315 | } |
5815 | | |
5816 | 887 | if (ci_equal(name, WKTConstants::CONCATENATEDOPERATION)) { |
5817 | 4 | return util::nn_static_pointer_cast<BaseObject>( |
5818 | 4 | buildConcatenatedOperation(node)); |
5819 | 4 | } |
5820 | | |
5821 | 883 | if (ci_equal(name, WKTConstants::POINTMOTIONOPERATION)) { |
5822 | 2 | return util::nn_static_pointer_cast<BaseObject>( |
5823 | 2 | buildPointMotionOperation(node)); |
5824 | 2 | } |
5825 | | |
5826 | 881 | if (ci_equal(name, WKTConstants::ID) || |
5827 | 881 | ci_equal(name, WKTConstants::AUTHORITY)) { |
5828 | 30 | return util::nn_static_pointer_cast<BaseObject>( |
5829 | 30 | NN_NO_CHECK(buildId(node, node, false, false))); |
5830 | 30 | } |
5831 | | |
5832 | 851 | if (ci_equal(name, WKTConstants::COORDINATEMETADATA)) { |
5833 | 6 | return util::nn_static_pointer_cast<BaseObject>( |
5834 | 6 | buildCoordinateMetadata(node)); |
5835 | 6 | } |
5836 | | |
5837 | 845 | throw ParsingException(concat("unhandled keyword: ", name)); |
5838 | 851 | } |
5839 | | //! @endcond |
5840 | | |
5841 | | // --------------------------------------------------------------------------- |
5842 | | |
5843 | | //! @cond Doxygen_Suppress |
5844 | | class JSONParser { |
5845 | | DatabaseContextPtr dbContext_{}; |
5846 | | std::string deformationModelName_{}; |
5847 | | |
5848 | | static std::string getString(const json &j, const char *key); |
5849 | | static json getObject(const json &j, const char *key); |
5850 | | static json getArray(const json &j, const char *key); |
5851 | | static int getInteger(const json &j, const char *key); |
5852 | | static double getNumber(const json &j, const char *key); |
5853 | | static UnitOfMeasure getUnit(const json &j, const char *key); |
5854 | | static std::string getName(const json &j); |
5855 | | static std::string getType(const json &j); |
5856 | | static Length getLength(const json &j, const char *key); |
5857 | | static Measure getMeasure(const json &j); |
5858 | | |
5859 | | IdentifierNNPtr buildId(const json &parentJ, const json &j, |
5860 | | bool removeInverseOf); |
5861 | | static ObjectDomainPtr buildObjectDomain(const json &j); |
5862 | | PropertyMap buildProperties(const json &j, bool removeInverseOf = false, |
5863 | | bool nameRequired = true); |
5864 | | |
5865 | | GeographicCRSNNPtr buildGeographicCRS(const json &j); |
5866 | | GeodeticCRSNNPtr buildGeodeticCRS(const json &j); |
5867 | | ProjectedCRSNNPtr buildProjectedCRS(const json &j); |
5868 | | ConversionNNPtr buildConversion(const json &j); |
5869 | | DatumEnsembleNNPtr buildDatumEnsemble(const json &j); |
5870 | | GeodeticReferenceFrameNNPtr buildGeodeticReferenceFrame(const json &j); |
5871 | | VerticalReferenceFrameNNPtr buildVerticalReferenceFrame(const json &j); |
5872 | | DynamicGeodeticReferenceFrameNNPtr |
5873 | | buildDynamicGeodeticReferenceFrame(const json &j); |
5874 | | DynamicVerticalReferenceFrameNNPtr |
5875 | | buildDynamicVerticalReferenceFrame(const json &j); |
5876 | | EllipsoidNNPtr buildEllipsoid(const json &j); |
5877 | | PrimeMeridianNNPtr buildPrimeMeridian(const json &j); |
5878 | | CoordinateSystemNNPtr buildCS(const json &j); |
5879 | | MeridianNNPtr buildMeridian(const json &j); |
5880 | | CoordinateSystemAxisNNPtr buildAxis(const json &j); |
5881 | | VerticalCRSNNPtr buildVerticalCRS(const json &j); |
5882 | | CRSNNPtr buildCRS(const json &j); |
5883 | | CompoundCRSNNPtr buildCompoundCRS(const json &j); |
5884 | | BoundCRSNNPtr buildBoundCRS(const json &j); |
5885 | | TransformationNNPtr buildTransformation(const json &j); |
5886 | | PointMotionOperationNNPtr buildPointMotionOperation(const json &j); |
5887 | | ConcatenatedOperationNNPtr buildConcatenatedOperation(const json &j); |
5888 | | CoordinateMetadataNNPtr buildCoordinateMetadata(const json &j); |
5889 | | |
5890 | | void buildGeodeticDatumOrDatumEnsemble(const json &j, |
5891 | | GeodeticReferenceFramePtr &datum, |
5892 | | DatumEnsemblePtr &datumEnsemble); |
5893 | | |
5894 | 5 | static util::optional<std::string> getAnchor(const json &j) { |
5895 | 5 | util::optional<std::string> anchor; |
5896 | 5 | if (j.contains("anchor")) { |
5897 | 0 | anchor = getString(j, "anchor"); |
5898 | 0 | } |
5899 | 5 | return anchor; |
5900 | 5 | } |
5901 | | |
5902 | 0 | static util::optional<common::Measure> getAnchorEpoch(const json &j) { |
5903 | 0 | if (j.contains("anchor_epoch")) { |
5904 | 0 | return util::optional<common::Measure>(common::Measure( |
5905 | 0 | getNumber(j, "anchor_epoch"), common::UnitOfMeasure::YEAR)); |
5906 | 0 | } |
5907 | 0 | return util::optional<common::Measure>(); |
5908 | 0 | } |
5909 | | |
5910 | 7 | EngineeringDatumNNPtr buildEngineeringDatum(const json &j) { |
5911 | 7 | return EngineeringDatum::create(buildProperties(j), getAnchor(j)); |
5912 | 7 | } |
5913 | | |
5914 | 0 | ParametricDatumNNPtr buildParametricDatum(const json &j) { |
5915 | 0 | return ParametricDatum::create(buildProperties(j), getAnchor(j)); |
5916 | 0 | } |
5917 | | |
5918 | 0 | TemporalDatumNNPtr buildTemporalDatum(const json &j) { |
5919 | 0 | auto calendar = getString(j, "calendar"); |
5920 | 0 | auto origin = DateTime::create(j.contains("time_origin") |
5921 | 0 | ? getString(j, "time_origin") |
5922 | 0 | : std::string()); |
5923 | 0 | return TemporalDatum::create(buildProperties(j), origin, calendar); |
5924 | 0 | } |
5925 | | |
5926 | | template <class TargetCRS, class DatumBuilderType, |
5927 | | class CSClass = CoordinateSystem> |
5928 | | util::nn<std::shared_ptr<TargetCRS>> buildCRS(const json &j, |
5929 | 0 | DatumBuilderType f) { |
5930 | 0 | auto datum = (this->*f)(getObject(j, "datum")); |
5931 | 0 | auto cs = buildCS(getObject(j, "coordinate_system")); |
5932 | 0 | auto csCast = util::nn_dynamic_pointer_cast<CSClass>(cs); |
5933 | 0 | if (!csCast) { |
5934 | 0 | throw ParsingException("coordinate_system not of expected type"); |
5935 | 0 | } |
5936 | 0 | return TargetCRS::create(buildProperties(j), datum, |
5937 | 0 | NN_NO_CHECK(csCast)); |
5938 | 0 | } Unexecuted instantiation: dropbox::oxygen::nn<std::__1::shared_ptr<osgeo::proj::crs::EngineeringCRS> > osgeo::proj::io::JSONParser::buildCRS<osgeo::proj::crs::EngineeringCRS, dropbox::oxygen::nn<std::__1::shared_ptr<osgeo::proj::datum::EngineeringDatum> > (osgeo::proj::io::JSONParser::*)(proj_nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long, unsigned long, double, std::__1::allocator, proj_nlohmann::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > const&), osgeo::proj::cs::CoordinateSystem>(proj_nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long, unsigned long, double, std::__1::allocator, proj_nlohmann::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > const&, dropbox::oxygen::nn<std::__1::shared_ptr<osgeo::proj::datum::EngineeringDatum> > (osgeo::proj::io::JSONParser::*)(proj_nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long, unsigned long, double, std::__1::allocator, proj_nlohmann::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > const&)) Unexecuted instantiation: dropbox::oxygen::nn<std::__1::shared_ptr<osgeo::proj::crs::ParametricCRS> > osgeo::proj::io::JSONParser::buildCRS<osgeo::proj::crs::ParametricCRS, dropbox::oxygen::nn<std::__1::shared_ptr<osgeo::proj::datum::ParametricDatum> > (osgeo::proj::io::JSONParser::*)(proj_nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long, unsigned long, double, std::__1::allocator, proj_nlohmann::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > const&), osgeo::proj::cs::ParametricCS>(proj_nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long, unsigned long, double, std::__1::allocator, proj_nlohmann::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > const&, dropbox::oxygen::nn<std::__1::shared_ptr<osgeo::proj::datum::ParametricDatum> > (osgeo::proj::io::JSONParser::*)(proj_nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long, unsigned long, double, std::__1::allocator, proj_nlohmann::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > const&)) Unexecuted instantiation: dropbox::oxygen::nn<std::__1::shared_ptr<osgeo::proj::crs::TemporalCRS> > osgeo::proj::io::JSONParser::buildCRS<osgeo::proj::crs::TemporalCRS, dropbox::oxygen::nn<std::__1::shared_ptr<osgeo::proj::datum::TemporalDatum> > (osgeo::proj::io::JSONParser::*)(proj_nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long, unsigned long, double, std::__1::allocator, proj_nlohmann::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > const&), osgeo::proj::cs::TemporalCS>(proj_nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long, unsigned long, double, std::__1::allocator, proj_nlohmann::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > const&, dropbox::oxygen::nn<std::__1::shared_ptr<osgeo::proj::datum::TemporalDatum> > (osgeo::proj::io::JSONParser::*)(proj_nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long, unsigned long, double, std::__1::allocator, proj_nlohmann::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > const&)) |
5939 | | |
5940 | | template <class TargetCRS, class BaseCRS, class CSClass = CoordinateSystem> |
5941 | 0 | util::nn<std::shared_ptr<TargetCRS>> buildDerivedCRS(const json &j) { |
5942 | 0 | auto baseCRSObj = create(getObject(j, "base_crs")); |
5943 | 0 | auto baseCRS = util::nn_dynamic_pointer_cast<BaseCRS>(baseCRSObj); |
5944 | 0 | if (!baseCRS) { |
5945 | 0 | throw ParsingException("base_crs not of expected type"); |
5946 | 0 | } |
5947 | 0 | auto cs = buildCS(getObject(j, "coordinate_system")); |
5948 | 0 | auto csCast = util::nn_dynamic_pointer_cast<CSClass>(cs); |
5949 | 0 | if (!csCast) { |
5950 | 0 | throw ParsingException("coordinate_system not of expected type"); |
5951 | 0 | } |
5952 | 0 | auto conv = buildConversion(getObject(j, "conversion")); |
5953 | 0 | return TargetCRS::create(buildProperties(j), NN_NO_CHECK(baseCRS), conv, |
5954 | 0 | NN_NO_CHECK(csCast)); |
5955 | 0 | } Unexecuted instantiation: dropbox::oxygen::nn<std::__1::shared_ptr<osgeo::proj::crs::DerivedGeographicCRS> > osgeo::proj::io::JSONParser::buildDerivedCRS<osgeo::proj::crs::DerivedGeographicCRS, osgeo::proj::crs::GeodeticCRS, osgeo::proj::cs::EllipsoidalCS>(proj_nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long, unsigned long, double, std::__1::allocator, proj_nlohmann::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > const&) Unexecuted instantiation: dropbox::oxygen::nn<std::__1::shared_ptr<osgeo::proj::crs::DerivedProjectedCRS> > osgeo::proj::io::JSONParser::buildDerivedCRS<osgeo::proj::crs::DerivedProjectedCRS, osgeo::proj::crs::ProjectedCRS, osgeo::proj::cs::CoordinateSystem>(proj_nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long, unsigned long, double, std::__1::allocator, proj_nlohmann::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > const&) Unexecuted instantiation: dropbox::oxygen::nn<std::__1::shared_ptr<osgeo::proj::crs::DerivedVerticalCRS> > osgeo::proj::io::JSONParser::buildDerivedCRS<osgeo::proj::crs::DerivedVerticalCRS, osgeo::proj::crs::VerticalCRS, osgeo::proj::cs::VerticalCS>(proj_nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long, unsigned long, double, std::__1::allocator, proj_nlohmann::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > const&) Unexecuted instantiation: dropbox::oxygen::nn<std::__1::shared_ptr<osgeo::proj::crs::DerivedCRSTemplate<osgeo::proj::crs::DerivedEngineeringCRSTraits> > > osgeo::proj::io::JSONParser::buildDerivedCRS<osgeo::proj::crs::DerivedCRSTemplate<osgeo::proj::crs::DerivedEngineeringCRSTraits>, osgeo::proj::crs::EngineeringCRS, osgeo::proj::cs::CoordinateSystem>(proj_nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long, unsigned long, double, std::__1::allocator, proj_nlohmann::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > const&) Unexecuted instantiation: dropbox::oxygen::nn<std::__1::shared_ptr<osgeo::proj::crs::DerivedCRSTemplate<osgeo::proj::crs::DerivedParametricCRSTraits> > > osgeo::proj::io::JSONParser::buildDerivedCRS<osgeo::proj::crs::DerivedCRSTemplate<osgeo::proj::crs::DerivedParametricCRSTraits>, osgeo::proj::crs::ParametricCRS, osgeo::proj::cs::ParametricCS>(proj_nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long, unsigned long, double, std::__1::allocator, proj_nlohmann::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > const&) Unexecuted instantiation: dropbox::oxygen::nn<std::__1::shared_ptr<osgeo::proj::crs::DerivedCRSTemplate<osgeo::proj::crs::DerivedTemporalCRSTraits> > > osgeo::proj::io::JSONParser::buildDerivedCRS<osgeo::proj::crs::DerivedCRSTemplate<osgeo::proj::crs::DerivedTemporalCRSTraits>, osgeo::proj::crs::TemporalCRS, osgeo::proj::cs::TemporalCS>(proj_nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long, unsigned long, double, std::__1::allocator, proj_nlohmann::adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > const&) |
5956 | | |
5957 | | public: |
5958 | 80 | JSONParser() = default; |
5959 | | |
5960 | 80 | JSONParser &attachDatabaseContext(const DatabaseContextPtr &dbContext) { |
5961 | 80 | dbContext_ = dbContext; |
5962 | 80 | return *this; |
5963 | 80 | } |
5964 | | |
5965 | | BaseObjectNNPtr create(const json &j); |
5966 | | }; |
5967 | | |
5968 | | // --------------------------------------------------------------------------- |
5969 | | |
5970 | 115 | std::string JSONParser::getString(const json &j, const char *key) { |
5971 | 115 | if (!j.contains(key)) { |
5972 | 22 | throw ParsingException(std::string("Missing \"") + key + "\" key"); |
5973 | 22 | } |
5974 | 93 | auto v = j[key]; |
5975 | 93 | if (!v.is_string()) { |
5976 | 38 | throw ParsingException(std::string("The value of \"") + key + |
5977 | 38 | "\" should be a string"); |
5978 | 38 | } |
5979 | 55 | return v.get<std::string>(); |
5980 | 93 | } |
5981 | | |
5982 | | // --------------------------------------------------------------------------- |
5983 | | |
5984 | 30 | json JSONParser::getObject(const json &j, const char *key) { |
5985 | 30 | if (!j.contains(key)) { |
5986 | 2 | throw ParsingException(std::string("Missing \"") + key + "\" key"); |
5987 | 2 | } |
5988 | 28 | auto v = j[key]; |
5989 | 28 | if (!v.is_object()) { |
5990 | 1 | throw ParsingException(std::string("The value of \"") + key + |
5991 | 1 | "\" should be a object"); |
5992 | 1 | } |
5993 | 27 | return v.get<json>(); |
5994 | 28 | } |
5995 | | |
5996 | | // --------------------------------------------------------------------------- |
5997 | | |
5998 | 0 | json JSONParser::getArray(const json &j, const char *key) { |
5999 | 0 | if (!j.contains(key)) { |
6000 | 0 | throw ParsingException(std::string("Missing \"") + key + "\" key"); |
6001 | 0 | } |
6002 | 0 | auto v = j[key]; |
6003 | 0 | if (!v.is_array()) { |
6004 | 0 | throw ParsingException(std::string("The value of \"") + key + |
6005 | 0 | "\" should be a array"); |
6006 | 0 | } |
6007 | 0 | return v.get<json>(); |
6008 | 0 | } |
6009 | | |
6010 | | // --------------------------------------------------------------------------- |
6011 | | |
6012 | 0 | int JSONParser::getInteger(const json &j, const char *key) { |
6013 | 0 | if (!j.contains(key)) { |
6014 | 0 | throw ParsingException(std::string("Missing \"") + key + "\" key"); |
6015 | 0 | } |
6016 | 0 | auto v = j[key]; |
6017 | 0 | if (!v.is_number()) { |
6018 | 0 | throw ParsingException(std::string("The value of \"") + key + |
6019 | 0 | "\" should be an integer"); |
6020 | 0 | } |
6021 | 0 | const double dbl = v.get<double>(); |
6022 | 0 | if (!(dbl >= std::numeric_limits<int>::min() && |
6023 | 0 | dbl <= std::numeric_limits<int>::max() && |
6024 | 0 | static_cast<int>(dbl) == dbl)) { |
6025 | 0 | throw ParsingException(std::string("The value of \"") + key + |
6026 | 0 | "\" should be an integer"); |
6027 | 0 | } |
6028 | 0 | return static_cast<int>(dbl); |
6029 | 0 | } |
6030 | | |
6031 | | // --------------------------------------------------------------------------- |
6032 | | |
6033 | 0 | double JSONParser::getNumber(const json &j, const char *key) { |
6034 | 0 | if (!j.contains(key)) { |
6035 | 0 | throw ParsingException(std::string("Missing \"") + key + "\" key"); |
6036 | 0 | } |
6037 | 0 | auto v = j[key]; |
6038 | 0 | if (!v.is_number()) { |
6039 | 0 | throw ParsingException(std::string("The value of \"") + key + |
6040 | 0 | "\" should be a number"); |
6041 | 0 | } |
6042 | 0 | return v.get<double>(); |
6043 | 0 | } |
6044 | | |
6045 | | // --------------------------------------------------------------------------- |
6046 | | |
6047 | 0 | UnitOfMeasure JSONParser::getUnit(const json &j, const char *key) { |
6048 | 0 | if (!j.contains(key)) { |
6049 | 0 | throw ParsingException(std::string("Missing \"") + key + "\" key"); |
6050 | 0 | } |
6051 | 0 | auto v = j[key]; |
6052 | 0 | if (v.is_string()) { |
6053 | 0 | auto vStr = v.get<std::string>(); |
6054 | 0 | for (const auto &unit : {UnitOfMeasure::METRE, UnitOfMeasure::DEGREE, |
6055 | 0 | UnitOfMeasure::SCALE_UNITY}) { |
6056 | 0 | if (vStr == unit.name()) |
6057 | 0 | return unit; |
6058 | 0 | } |
6059 | 0 | throw ParsingException("Unknown unit name: " + vStr); |
6060 | 0 | } |
6061 | 0 | if (!v.is_object()) { |
6062 | 0 | throw ParsingException(std::string("The value of \"") + key + |
6063 | 0 | "\" should be a string or an object"); |
6064 | 0 | } |
6065 | 0 | auto typeStr = getType(v); |
6066 | 0 | UnitOfMeasure::Type type = UnitOfMeasure::Type::UNKNOWN; |
6067 | 0 | if (typeStr == "LinearUnit") { |
6068 | 0 | type = UnitOfMeasure::Type::LINEAR; |
6069 | 0 | } else if (typeStr == "AngularUnit") { |
6070 | 0 | type = UnitOfMeasure::Type::ANGULAR; |
6071 | 0 | } else if (typeStr == "ScaleUnit") { |
6072 | 0 | type = UnitOfMeasure::Type::SCALE; |
6073 | 0 | } else if (typeStr == "TimeUnit") { |
6074 | 0 | type = UnitOfMeasure::Type::TIME; |
6075 | 0 | } else if (typeStr == "ParametricUnit") { |
6076 | 0 | type = UnitOfMeasure::Type::PARAMETRIC; |
6077 | 0 | } else if (typeStr == "Unit") { |
6078 | 0 | type = UnitOfMeasure::Type::UNKNOWN; |
6079 | 0 | } else { |
6080 | 0 | throw ParsingException("Unsupported value of \"type\""); |
6081 | 0 | } |
6082 | 0 | auto nameStr = getName(v); |
6083 | 0 | auto convFactor = getNumber(v, "conversion_factor"); |
6084 | 0 | std::string authorityStr; |
6085 | 0 | std::string codeStr; |
6086 | 0 | if (v.contains("authority") && v.contains("code")) { |
6087 | 0 | authorityStr = getString(v, "authority"); |
6088 | 0 | auto code = v["code"]; |
6089 | 0 | if (code.is_string()) { |
6090 | 0 | codeStr = code.get<std::string>(); |
6091 | 0 | } else if (code.is_number_integer()) { |
6092 | 0 | codeStr = internal::toString(code.get<int>()); |
6093 | 0 | } else { |
6094 | 0 | throw ParsingException("Unexpected type for value of \"code\""); |
6095 | 0 | } |
6096 | 0 | } |
6097 | 0 | return UnitOfMeasure(nameStr, convFactor, type, authorityStr, codeStr); |
6098 | 0 | } |
6099 | | |
6100 | | // --------------------------------------------------------------------------- |
6101 | | |
6102 | 8 | std::string JSONParser::getName(const json &j) { return getString(j, "name"); } |
6103 | | |
6104 | | // --------------------------------------------------------------------------- |
6105 | | |
6106 | 0 | std::string JSONParser::getType(const json &j) { return getString(j, "type"); } |
6107 | | |
6108 | | // --------------------------------------------------------------------------- |
6109 | | |
6110 | 0 | Length JSONParser::getLength(const json &j, const char *key) { |
6111 | 0 | if (!j.contains(key)) { |
6112 | 0 | throw ParsingException(std::string("Missing \"") + key + "\" key"); |
6113 | 0 | } |
6114 | 0 | auto v = j[key]; |
6115 | 0 | if (v.is_number()) { |
6116 | 0 | return Length(v.get<double>(), UnitOfMeasure::METRE); |
6117 | 0 | } |
6118 | 0 | if (v.is_object()) { |
6119 | 0 | return Length(getMeasure(v)); |
6120 | 0 | } |
6121 | 0 | throw ParsingException(std::string("The value of \"") + key + |
6122 | 0 | "\" should be a number or an object"); |
6123 | 0 | } |
6124 | | |
6125 | | // --------------------------------------------------------------------------- |
6126 | | |
6127 | 0 | Measure JSONParser::getMeasure(const json &j) { |
6128 | 0 | return Measure(getNumber(j, "value"), getUnit(j, "unit")); |
6129 | 0 | } |
6130 | | |
6131 | | // --------------------------------------------------------------------------- |
6132 | | |
6133 | 5 | ObjectDomainPtr JSONParser::buildObjectDomain(const json &j) { |
6134 | 5 | optional<std::string> scope; |
6135 | 5 | if (j.contains("scope")) { |
6136 | 0 | scope = getString(j, "scope"); |
6137 | 0 | } |
6138 | 5 | std::string area; |
6139 | 5 | if (j.contains("area")) { |
6140 | 0 | area = getString(j, "area"); |
6141 | 0 | } |
6142 | 5 | std::vector<GeographicExtentNNPtr> geogExtent; |
6143 | 5 | if (j.contains("bbox")) { |
6144 | 0 | auto bbox = getObject(j, "bbox"); |
6145 | 0 | double south = getNumber(bbox, "south_latitude"); |
6146 | 0 | double west = getNumber(bbox, "west_longitude"); |
6147 | 0 | double north = getNumber(bbox, "north_latitude"); |
6148 | 0 | double east = getNumber(bbox, "east_longitude"); |
6149 | 0 | try { |
6150 | 0 | geogExtent.emplace_back( |
6151 | 0 | GeographicBoundingBox::create(west, south, east, north)); |
6152 | 0 | } catch (const std::exception &e) { |
6153 | 0 | throw ParsingException( |
6154 | 0 | std::string("Invalid bbox node: ").append(e.what())); |
6155 | 0 | } |
6156 | 0 | } |
6157 | | |
6158 | 5 | std::vector<VerticalExtentNNPtr> verticalExtent; |
6159 | 5 | if (j.contains("vertical_extent")) { |
6160 | 0 | const auto vertical_extent = getObject(j, "vertical_extent"); |
6161 | 0 | const auto min = getNumber(vertical_extent, "minimum"); |
6162 | 0 | const auto max = getNumber(vertical_extent, "maximum"); |
6163 | 0 | const auto unit = vertical_extent.contains("unit") |
6164 | 0 | ? getUnit(vertical_extent, "unit") |
6165 | 0 | : UnitOfMeasure::METRE; |
6166 | 0 | verticalExtent.emplace_back(VerticalExtent::create( |
6167 | 0 | min, max, util::nn_make_shared<UnitOfMeasure>(unit))); |
6168 | 0 | } |
6169 | | |
6170 | 5 | std::vector<TemporalExtentNNPtr> temporalExtent; |
6171 | 5 | if (j.contains("temporal_extent")) { |
6172 | 0 | const auto temporal_extent = getObject(j, "temporal_extent"); |
6173 | 0 | const auto start = getString(temporal_extent, "start"); |
6174 | 0 | const auto end = getString(temporal_extent, "end"); |
6175 | 0 | temporalExtent.emplace_back(TemporalExtent::create(start, end)); |
6176 | 0 | } |
6177 | | |
6178 | 5 | if (scope.has_value() || !area.empty() || !geogExtent.empty() || |
6179 | 5 | !verticalExtent.empty() || !temporalExtent.empty()) { |
6180 | 0 | util::optional<std::string> description; |
6181 | 0 | if (!area.empty()) |
6182 | 0 | description = area; |
6183 | 0 | ExtentPtr extent; |
6184 | 0 | if (description.has_value() || !geogExtent.empty() || |
6185 | 0 | !verticalExtent.empty() || !temporalExtent.empty()) { |
6186 | 0 | extent = Extent::create(description, geogExtent, verticalExtent, |
6187 | 0 | temporalExtent) |
6188 | 0 | .as_nullable(); |
6189 | 0 | } |
6190 | 0 | return ObjectDomain::create(scope, extent).as_nullable(); |
6191 | 0 | } |
6192 | 5 | return nullptr; |
6193 | 5 | } |
6194 | | |
6195 | | // --------------------------------------------------------------------------- |
6196 | | |
6197 | | IdentifierNNPtr JSONParser::buildId(const json &parentJ, const json &j, |
6198 | 0 | bool removeInverseOf) { |
6199 | |
|
6200 | 0 | PropertyMap propertiesId; |
6201 | 0 | auto codeSpace(getString(j, "authority")); |
6202 | 0 | if (removeInverseOf && starts_with(codeSpace, "INVERSE(") && |
6203 | 0 | codeSpace.back() == ')') { |
6204 | 0 | codeSpace = codeSpace.substr(strlen("INVERSE(")); |
6205 | 0 | codeSpace.resize(codeSpace.size() - 1); |
6206 | 0 | } |
6207 | |
|
6208 | 0 | std::string version; |
6209 | 0 | if (j.contains("version")) { |
6210 | 0 | auto versionJ = j["version"]; |
6211 | 0 | if (versionJ.is_string()) { |
6212 | 0 | version = versionJ.get<std::string>(); |
6213 | 0 | } else if (versionJ.is_number()) { |
6214 | 0 | const double dblVersion = versionJ.get<double>(); |
6215 | 0 | if (dblVersion >= std::numeric_limits<int>::min() && |
6216 | 0 | dblVersion <= std::numeric_limits<int>::max() && |
6217 | 0 | static_cast<int>(dblVersion) == dblVersion) { |
6218 | 0 | version = internal::toString(static_cast<int>(dblVersion)); |
6219 | 0 | } else { |
6220 | 0 | version = internal::toString(dblVersion, /*precision=*/15); |
6221 | 0 | } |
6222 | 0 | } else { |
6223 | 0 | throw ParsingException("Unexpected type for value of \"version\""); |
6224 | 0 | } |
6225 | 0 | } |
6226 | | |
6227 | | // IAU + 2015 -> IAU_2015 |
6228 | 0 | if (dbContext_ && !version.empty()) { |
6229 | 0 | std::string codeSpaceOut; |
6230 | 0 | if (dbContext_->getVersionedAuthority(codeSpace, version, |
6231 | 0 | codeSpaceOut)) { |
6232 | 0 | codeSpace = std::move(codeSpaceOut); |
6233 | 0 | version.clear(); |
6234 | 0 | } |
6235 | 0 | } |
6236 | |
|
6237 | 0 | propertiesId.set(metadata::Identifier::CODESPACE_KEY, codeSpace); |
6238 | 0 | propertiesId.set(metadata::Identifier::AUTHORITY_KEY, codeSpace); |
6239 | 0 | if (!j.contains("code")) { |
6240 | 0 | throw ParsingException("Missing \"code\" key"); |
6241 | 0 | } |
6242 | 0 | std::string code; |
6243 | 0 | auto codeJ = j["code"]; |
6244 | 0 | if (codeJ.is_string()) { |
6245 | 0 | code = codeJ.get<std::string>(); |
6246 | 0 | } else if (codeJ.is_number_integer()) { |
6247 | 0 | code = internal::toString(codeJ.get<int>()); |
6248 | 0 | } else { |
6249 | 0 | throw ParsingException("Unexpected type for value of \"code\""); |
6250 | 0 | } |
6251 | | |
6252 | | // Prior to PROJ 9.5, when synthetizing an ID for a CONVERSION UTM Zone |
6253 | | // south, we generated a wrong value. Auto-fix that |
6254 | 0 | if (parentJ.contains("type") && getType(parentJ) == "Conversion" && |
6255 | 0 | codeSpace == Identifier::EPSG && parentJ.contains("name")) { |
6256 | 0 | const auto parentNodeName(getName(parentJ)); |
6257 | 0 | if (ci_starts_with(parentNodeName, "UTM Zone ") && |
6258 | 0 | parentNodeName.find('S') != std::string::npos) { |
6259 | 0 | const int nZone = |
6260 | 0 | atoi(parentNodeName.c_str() + strlen("UTM Zone ")); |
6261 | 0 | if (nZone >= 1 && nZone <= 60) { |
6262 | 0 | code = internal::toString(16100 + nZone); |
6263 | 0 | } |
6264 | 0 | } |
6265 | 0 | } |
6266 | |
|
6267 | 0 | if (!version.empty()) { |
6268 | 0 | propertiesId.set(Identifier::VERSION_KEY, version); |
6269 | 0 | } |
6270 | |
|
6271 | 0 | if (j.contains("authority_citation")) { |
6272 | 0 | propertiesId.set(Identifier::AUTHORITY_KEY, |
6273 | 0 | getString(j, "authority_citation")); |
6274 | 0 | } |
6275 | |
|
6276 | 0 | if (j.contains("uri")) { |
6277 | 0 | propertiesId.set(Identifier::URI_KEY, getString(j, "uri")); |
6278 | 0 | } |
6279 | |
|
6280 | 0 | return Identifier::create(code, propertiesId); |
6281 | 0 | } |
6282 | | |
6283 | | // --------------------------------------------------------------------------- |
6284 | | |
6285 | | PropertyMap JSONParser::buildProperties(const json &j, bool removeInverseOf, |
6286 | 8 | bool nameRequired) { |
6287 | 8 | PropertyMap map; |
6288 | | |
6289 | 8 | if (j.contains("name") || nameRequired) { |
6290 | 8 | std::string name(getName(j)); |
6291 | 8 | if (removeInverseOf && starts_with(name, "Inverse of ")) { |
6292 | 0 | name = name.substr(strlen("Inverse of ")); |
6293 | 0 | } |
6294 | 8 | map.set(IdentifiedObject::NAME_KEY, name); |
6295 | 8 | } |
6296 | | |
6297 | 8 | if (j.contains("ids")) { |
6298 | 0 | auto idsJ = getArray(j, "ids"); |
6299 | 0 | auto identifiers = ArrayOfBaseObject::create(); |
6300 | 0 | for (const auto &idJ : idsJ) { |
6301 | 0 | if (!idJ.is_object()) { |
6302 | 0 | throw ParsingException( |
6303 | 0 | "Unexpected type for value of \"ids\" child"); |
6304 | 0 | } |
6305 | 0 | identifiers->add(buildId(j, idJ, removeInverseOf)); |
6306 | 0 | } |
6307 | 0 | map.set(IdentifiedObject::IDENTIFIERS_KEY, identifiers); |
6308 | 8 | } else if (j.contains("id")) { |
6309 | 0 | auto idJ = getObject(j, "id"); |
6310 | 0 | auto identifiers = ArrayOfBaseObject::create(); |
6311 | 0 | identifiers->add(buildId(j, idJ, removeInverseOf)); |
6312 | 0 | map.set(IdentifiedObject::IDENTIFIERS_KEY, identifiers); |
6313 | 0 | } |
6314 | | |
6315 | 8 | if (j.contains("remarks")) { |
6316 | 0 | map.set(IdentifiedObject::REMARKS_KEY, getString(j, "remarks")); |
6317 | 0 | } |
6318 | | |
6319 | 8 | if (j.contains("usages")) { |
6320 | 0 | ArrayOfBaseObjectNNPtr array = ArrayOfBaseObject::create(); |
6321 | 0 | auto usages = j["usages"]; |
6322 | 0 | if (!usages.is_array()) { |
6323 | 0 | throw ParsingException("Unexpected type for value of \"usages\""); |
6324 | 0 | } |
6325 | 0 | for (const auto &usage : usages) { |
6326 | 0 | if (!usage.is_object()) { |
6327 | 0 | throw ParsingException( |
6328 | 0 | "Unexpected type for value of \"usages\" child"); |
6329 | 0 | } |
6330 | 0 | auto objectDomain = buildObjectDomain(usage); |
6331 | 0 | if (!objectDomain) { |
6332 | 0 | throw ParsingException("missing children in \"usages\" child"); |
6333 | 0 | } |
6334 | 0 | array->add(NN_NO_CHECK(objectDomain)); |
6335 | 0 | } |
6336 | 0 | if (!array->empty()) { |
6337 | 0 | map.set(ObjectUsage::OBJECT_DOMAIN_KEY, array); |
6338 | 0 | } |
6339 | 8 | } else { |
6340 | 8 | auto objectDomain = buildObjectDomain(j); |
6341 | 8 | if (objectDomain) { |
6342 | 0 | map.set(ObjectUsage::OBJECT_DOMAIN_KEY, NN_NO_CHECK(objectDomain)); |
6343 | 0 | } |
6344 | 8 | } |
6345 | | |
6346 | 8 | return map; |
6347 | 8 | } |
6348 | | |
6349 | | // --------------------------------------------------------------------------- |
6350 | | |
6351 | | BaseObjectNNPtr JSONParser::create(const json &j) |
6352 | | |
6353 | 107 | { |
6354 | 107 | if (!j.is_object()) { |
6355 | 0 | throw ParsingException("JSON object expected"); |
6356 | 0 | } |
6357 | 107 | auto type = getString(j, "type"); |
6358 | 107 | if (type == "GeographicCRS") { |
6359 | 29 | return buildGeographicCRS(j); |
6360 | 29 | } |
6361 | 78 | if (type == "GeodeticCRS") { |
6362 | 0 | return buildGeodeticCRS(j); |
6363 | 0 | } |
6364 | 78 | if (type == "ProjectedCRS") { |
6365 | 0 | return buildProjectedCRS(j); |
6366 | 0 | } |
6367 | 78 | if (type == "VerticalCRS") { |
6368 | 0 | return buildVerticalCRS(j); |
6369 | 0 | } |
6370 | 78 | if (type == "CompoundCRS") { |
6371 | 0 | return buildCompoundCRS(j); |
6372 | 0 | } |
6373 | 78 | if (type == "BoundCRS") { |
6374 | 0 | return buildBoundCRS(j); |
6375 | 0 | } |
6376 | 78 | if (type == "EngineeringCRS") { |
6377 | 0 | return buildCRS<EngineeringCRS>(j, &JSONParser::buildEngineeringDatum); |
6378 | 0 | } |
6379 | 78 | if (type == "ParametricCRS") { |
6380 | 0 | return buildCRS<ParametricCRS, |
6381 | 0 | decltype(&JSONParser::buildParametricDatum), |
6382 | 0 | ParametricCS>(j, &JSONParser::buildParametricDatum); |
6383 | 0 | } |
6384 | 78 | if (type == "TemporalCRS") { |
6385 | 0 | return buildCRS<TemporalCRS, decltype(&JSONParser::buildTemporalDatum), |
6386 | 0 | TemporalCS>(j, &JSONParser::buildTemporalDatum); |
6387 | 0 | } |
6388 | 78 | if (type == "DerivedGeodeticCRS") { |
6389 | 0 | auto baseCRSObj = create(getObject(j, "base_crs")); |
6390 | 0 | auto baseCRS = util::nn_dynamic_pointer_cast<GeodeticCRS>(baseCRSObj); |
6391 | 0 | if (!baseCRS) { |
6392 | 0 | throw ParsingException("base_crs not of expected type"); |
6393 | 0 | } |
6394 | 0 | auto cs = buildCS(getObject(j, "coordinate_system")); |
6395 | 0 | auto conv = buildConversion(getObject(j, "conversion")); |
6396 | 0 | auto csCartesian = util::nn_dynamic_pointer_cast<CartesianCS>(cs); |
6397 | 0 | if (csCartesian) |
6398 | 0 | return DerivedGeodeticCRS::create(buildProperties(j), |
6399 | 0 | NN_NO_CHECK(baseCRS), conv, |
6400 | 0 | NN_NO_CHECK(csCartesian)); |
6401 | 0 | auto csSpherical = util::nn_dynamic_pointer_cast<SphericalCS>(cs); |
6402 | 0 | if (csSpherical) |
6403 | 0 | return DerivedGeodeticCRS::create(buildProperties(j), |
6404 | 0 | NN_NO_CHECK(baseCRS), conv, |
6405 | 0 | NN_NO_CHECK(csSpherical)); |
6406 | 0 | throw ParsingException("coordinate_system not of expected type"); |
6407 | 0 | } |
6408 | 78 | if (type == "DerivedGeographicCRS") { |
6409 | 0 | return buildDerivedCRS<DerivedGeographicCRS, GeodeticCRS, |
6410 | 0 | EllipsoidalCS>(j); |
6411 | 0 | } |
6412 | 78 | if (type == "DerivedProjectedCRS") { |
6413 | 0 | return buildDerivedCRS<DerivedProjectedCRS, ProjectedCRS>(j); |
6414 | 0 | } |
6415 | 78 | if (type == "DerivedVerticalCRS") { |
6416 | 0 | return buildDerivedCRS<DerivedVerticalCRS, VerticalCRS, VerticalCS>(j); |
6417 | 0 | } |
6418 | 78 | if (type == "DerivedEngineeringCRS") { |
6419 | 0 | return buildDerivedCRS<DerivedEngineeringCRS, EngineeringCRS>(j); |
6420 | 0 | } |
6421 | 78 | if (type == "DerivedParametricCRS") { |
6422 | 0 | return buildDerivedCRS<DerivedParametricCRS, ParametricCRS, |
6423 | 0 | ParametricCS>(j); |
6424 | 0 | } |
6425 | 78 | if (type == "DerivedTemporalCRS") { |
6426 | 0 | return buildDerivedCRS<DerivedTemporalCRS, TemporalCRS, TemporalCS>(j); |
6427 | 0 | } |
6428 | 78 | if (type == "DatumEnsemble") { |
6429 | 0 | return buildDatumEnsemble(j); |
6430 | 0 | } |
6431 | 78 | if (type == "GeodeticReferenceFrame") { |
6432 | 0 | return buildGeodeticReferenceFrame(j); |
6433 | 0 | } |
6434 | 78 | if (type == "VerticalReferenceFrame") { |
6435 | 1 | return buildVerticalReferenceFrame(j); |
6436 | 1 | } |
6437 | 77 | if (type == "DynamicGeodeticReferenceFrame") { |
6438 | 0 | return buildDynamicGeodeticReferenceFrame(j); |
6439 | 0 | } |
6440 | 77 | if (type == "DynamicVerticalReferenceFrame") { |
6441 | 0 | return buildDynamicVerticalReferenceFrame(j); |
6442 | 0 | } |
6443 | 77 | if (type == "EngineeringDatum") { |
6444 | 7 | return buildEngineeringDatum(j); |
6445 | 7 | } |
6446 | 70 | if (type == "ParametricDatum") { |
6447 | 0 | return buildParametricDatum(j); |
6448 | 0 | } |
6449 | 70 | if (type == "TemporalDatum") { |
6450 | 0 | return buildTemporalDatum(j); |
6451 | 0 | } |
6452 | 70 | if (type == "Ellipsoid") { |
6453 | 0 | return buildEllipsoid(j); |
6454 | 0 | } |
6455 | 70 | if (type == "PrimeMeridian") { |
6456 | 0 | return buildPrimeMeridian(j); |
6457 | 0 | } |
6458 | 70 | if (type == "CoordinateSystem") { |
6459 | 0 | return buildCS(j); |
6460 | 0 | } |
6461 | 70 | if (type == "Conversion") { |
6462 | 0 | return buildConversion(j); |
6463 | 0 | } |
6464 | 70 | if (type == "Transformation") { |
6465 | 0 | return buildTransformation(j); |
6466 | 0 | } |
6467 | 70 | if (type == "PointMotionOperation") { |
6468 | 0 | return buildPointMotionOperation(j); |
6469 | 0 | } |
6470 | 70 | if (type == "ConcatenatedOperation") { |
6471 | 0 | return buildConcatenatedOperation(j); |
6472 | 0 | } |
6473 | 70 | if (type == "CoordinateMetadata") { |
6474 | 1 | return buildCoordinateMetadata(j); |
6475 | 1 | } |
6476 | 69 | if (type == "Axis") { |
6477 | 0 | return buildAxis(j); |
6478 | 0 | } |
6479 | 69 | throw ParsingException("Unsupported value of \"type\""); |
6480 | 69 | } |
6481 | | |
6482 | | // --------------------------------------------------------------------------- |
6483 | | |
6484 | | void JSONParser::buildGeodeticDatumOrDatumEnsemble( |
6485 | | const json &j, GeodeticReferenceFramePtr &datum, |
6486 | 29 | DatumEnsemblePtr &datumEnsemble) { |
6487 | 29 | if (j.contains("datum")) { |
6488 | 28 | auto datumJ = getObject(j, "datum"); |
6489 | | |
6490 | 28 | if (j.contains("deformation_models")) { |
6491 | 0 | auto deformationModelsJ = getArray(j, "deformation_models"); |
6492 | 0 | if (!deformationModelsJ.empty()) { |
6493 | 0 | const auto &deformationModelJ = deformationModelsJ[0]; |
6494 | 0 | deformationModelName_ = getString(deformationModelJ, "name"); |
6495 | | // We can handle only one for now |
6496 | 0 | } |
6497 | 0 | } |
6498 | | |
6499 | 28 | datum = util::nn_dynamic_pointer_cast<GeodeticReferenceFrame>( |
6500 | 28 | create(datumJ)); |
6501 | 28 | if (!datum) { |
6502 | 0 | throw ParsingException("datum of wrong type"); |
6503 | 0 | } |
6504 | | |
6505 | 28 | deformationModelName_.clear(); |
6506 | 28 | } else { |
6507 | 1 | datumEnsemble = |
6508 | 1 | buildDatumEnsemble(getObject(j, "datum_ensemble")).as_nullable(); |
6509 | 1 | } |
6510 | 29 | } |
6511 | | |
6512 | | // --------------------------------------------------------------------------- |
6513 | | |
6514 | 29 | GeographicCRSNNPtr JSONParser::buildGeographicCRS(const json &j) { |
6515 | 29 | GeodeticReferenceFramePtr datum; |
6516 | 29 | DatumEnsemblePtr datumEnsemble; |
6517 | 29 | buildGeodeticDatumOrDatumEnsemble(j, datum, datumEnsemble); |
6518 | 29 | auto csJ = getObject(j, "coordinate_system"); |
6519 | 29 | auto ellipsoidalCS = |
6520 | 29 | util::nn_dynamic_pointer_cast<EllipsoidalCS>(buildCS(csJ)); |
6521 | 29 | if (!ellipsoidalCS) { |
6522 | 0 | throw ParsingException("expected an ellipsoidal CS"); |
6523 | 0 | } |
6524 | 29 | return GeographicCRS::create(buildProperties(j), datum, datumEnsemble, |
6525 | 29 | NN_NO_CHECK(ellipsoidalCS)); |
6526 | 29 | } |
6527 | | |
6528 | | // --------------------------------------------------------------------------- |
6529 | | |
6530 | 0 | GeodeticCRSNNPtr JSONParser::buildGeodeticCRS(const json &j) { |
6531 | 0 | GeodeticReferenceFramePtr datum; |
6532 | 0 | DatumEnsemblePtr datumEnsemble; |
6533 | 0 | buildGeodeticDatumOrDatumEnsemble(j, datum, datumEnsemble); |
6534 | 0 | auto csJ = getObject(j, "coordinate_system"); |
6535 | 0 | auto cs = buildCS(csJ); |
6536 | 0 | auto props = buildProperties(j); |
6537 | 0 | auto cartesianCS = nn_dynamic_pointer_cast<CartesianCS>(cs); |
6538 | 0 | if (cartesianCS) { |
6539 | 0 | if (cartesianCS->axisList().size() != 3) { |
6540 | 0 | throw ParsingException( |
6541 | 0 | "Cartesian CS for a GeodeticCRS should have 3 axis"); |
6542 | 0 | } |
6543 | 0 | try { |
6544 | 0 | return GeodeticCRS::create(props, datum, datumEnsemble, |
6545 | 0 | NN_NO_CHECK(cartesianCS)); |
6546 | 0 | } catch (const util::Exception &e) { |
6547 | 0 | throw ParsingException(std::string("buildGeodeticCRS: ") + |
6548 | 0 | e.what()); |
6549 | 0 | } |
6550 | 0 | } |
6551 | | |
6552 | 0 | auto sphericalCS = nn_dynamic_pointer_cast<SphericalCS>(cs); |
6553 | 0 | if (sphericalCS) { |
6554 | 0 | try { |
6555 | 0 | return GeodeticCRS::create(props, datum, datumEnsemble, |
6556 | 0 | NN_NO_CHECK(sphericalCS)); |
6557 | 0 | } catch (const util::Exception &e) { |
6558 | 0 | throw ParsingException(std::string("buildGeodeticCRS: ") + |
6559 | 0 | e.what()); |
6560 | 0 | } |
6561 | 0 | } |
6562 | 0 | throw ParsingException("expected a Cartesian or spherical CS"); |
6563 | 0 | } |
6564 | | |
6565 | | // --------------------------------------------------------------------------- |
6566 | | |
6567 | 0 | ProjectedCRSNNPtr JSONParser::buildProjectedCRS(const json &j) { |
6568 | 0 | auto jBaseCRS = getObject(j, "base_crs"); |
6569 | 0 | auto jBaseCS = getObject(jBaseCRS, "coordinate_system"); |
6570 | 0 | auto baseCS = buildCS(jBaseCS); |
6571 | 0 | auto baseCRS = dynamic_cast<EllipsoidalCS *>(baseCS.get()) != nullptr |
6572 | 0 | ? util::nn_static_pointer_cast<GeodeticCRS>( |
6573 | 0 | buildGeographicCRS(jBaseCRS)) |
6574 | 0 | : buildGeodeticCRS(jBaseCRS); |
6575 | 0 | auto csJ = getObject(j, "coordinate_system"); |
6576 | 0 | auto cartesianCS = util::nn_dynamic_pointer_cast<CartesianCS>(buildCS(csJ)); |
6577 | 0 | if (!cartesianCS) { |
6578 | 0 | throw ParsingException("expected a Cartesian CS"); |
6579 | 0 | } |
6580 | 0 | auto conv = buildConversion(getObject(j, "conversion")); |
6581 | 0 | return ProjectedCRS::create(buildProperties(j), baseCRS, conv, |
6582 | 0 | NN_NO_CHECK(cartesianCS)); |
6583 | 0 | } |
6584 | | |
6585 | | // --------------------------------------------------------------------------- |
6586 | | |
6587 | 0 | VerticalCRSNNPtr JSONParser::buildVerticalCRS(const json &j) { |
6588 | 0 | VerticalReferenceFramePtr datum; |
6589 | 0 | DatumEnsemblePtr datumEnsemble; |
6590 | 0 | if (j.contains("datum")) { |
6591 | 0 | auto datumJ = getObject(j, "datum"); |
6592 | |
|
6593 | 0 | if (j.contains("deformation_models")) { |
6594 | 0 | auto deformationModelsJ = getArray(j, "deformation_models"); |
6595 | 0 | if (!deformationModelsJ.empty()) { |
6596 | 0 | const auto &deformationModelJ = deformationModelsJ[0]; |
6597 | 0 | deformationModelName_ = getString(deformationModelJ, "name"); |
6598 | | // We can handle only one for now |
6599 | 0 | } |
6600 | 0 | } |
6601 | |
|
6602 | 0 | datum = util::nn_dynamic_pointer_cast<VerticalReferenceFrame>( |
6603 | 0 | create(datumJ)); |
6604 | 0 | if (!datum) { |
6605 | 0 | throw ParsingException("datum of wrong type"); |
6606 | 0 | } |
6607 | 0 | } else { |
6608 | 0 | datumEnsemble = |
6609 | 0 | buildDatumEnsemble(getObject(j, "datum_ensemble")).as_nullable(); |
6610 | 0 | } |
6611 | 0 | auto csJ = getObject(j, "coordinate_system"); |
6612 | 0 | auto verticalCS = util::nn_dynamic_pointer_cast<VerticalCS>(buildCS(csJ)); |
6613 | 0 | if (!verticalCS) { |
6614 | 0 | throw ParsingException("expected a vertical CS"); |
6615 | 0 | } |
6616 | | |
6617 | 0 | const auto buildGeoidModel = [this, &datum, &datumEnsemble, |
6618 | 0 | &verticalCS](const json &geoidModelJ) { |
6619 | 0 | auto propsModel = buildProperties(geoidModelJ); |
6620 | 0 | const auto dummyCRS = VerticalCRS::create( |
6621 | 0 | PropertyMap(), datum, datumEnsemble, NN_NO_CHECK(verticalCS)); |
6622 | 0 | CRSPtr interpolationCRS; |
6623 | 0 | if (geoidModelJ.contains("interpolation_crs")) { |
6624 | 0 | auto interpolationCRSJ = |
6625 | 0 | getObject(geoidModelJ, "interpolation_crs"); |
6626 | 0 | interpolationCRS = buildCRS(interpolationCRSJ).as_nullable(); |
6627 | 0 | } |
6628 | 0 | return Transformation::create( |
6629 | 0 | propsModel, dummyCRS, |
6630 | 0 | GeographicCRS::EPSG_4979, // arbitrarily chosen. Ignored, |
6631 | 0 | interpolationCRS, |
6632 | 0 | OperationMethod::create(PropertyMap(), |
6633 | 0 | std::vector<OperationParameterNNPtr>()), |
6634 | 0 | {}, {}); |
6635 | 0 | }; |
6636 | |
|
6637 | 0 | auto props = buildProperties(j); |
6638 | 0 | if (j.contains("geoid_model")) { |
6639 | 0 | auto geoidModelJ = getObject(j, "geoid_model"); |
6640 | 0 | props.set("GEOID_MODEL", buildGeoidModel(geoidModelJ)); |
6641 | 0 | } else if (j.contains("geoid_models")) { |
6642 | 0 | auto geoidModelsJ = getArray(j, "geoid_models"); |
6643 | 0 | auto geoidModels = ArrayOfBaseObject::create(); |
6644 | 0 | for (const auto &geoidModelJ : geoidModelsJ) { |
6645 | 0 | geoidModels->add(buildGeoidModel(geoidModelJ)); |
6646 | 0 | } |
6647 | 0 | props.set("GEOID_MODEL", geoidModels); |
6648 | 0 | } |
6649 | |
|
6650 | 0 | return VerticalCRS::create(props, datum, datumEnsemble, |
6651 | 0 | NN_NO_CHECK(verticalCS)); |
6652 | 0 | } |
6653 | | |
6654 | | // --------------------------------------------------------------------------- |
6655 | | |
6656 | 0 | CRSNNPtr JSONParser::buildCRS(const json &j) { |
6657 | 0 | auto crs = util::nn_dynamic_pointer_cast<CRS>(create(j)); |
6658 | 0 | if (crs) { |
6659 | 0 | return NN_NO_CHECK(crs); |
6660 | 0 | } |
6661 | 0 | throw ParsingException("Object is not a CRS"); |
6662 | 0 | } |
6663 | | |
6664 | | // --------------------------------------------------------------------------- |
6665 | | |
6666 | 0 | CompoundCRSNNPtr JSONParser::buildCompoundCRS(const json &j) { |
6667 | 0 | auto componentsJ = getArray(j, "components"); |
6668 | 0 | std::vector<CRSNNPtr> components; |
6669 | 0 | for (const auto &componentJ : componentsJ) { |
6670 | 0 | if (!componentJ.is_object()) { |
6671 | 0 | throw ParsingException( |
6672 | 0 | "Unexpected type for a \"components\" child"); |
6673 | 0 | } |
6674 | 0 | components.push_back(buildCRS(componentJ)); |
6675 | 0 | } |
6676 | 0 | return CompoundCRS::create(buildProperties(j), components); |
6677 | 0 | } |
6678 | | |
6679 | | // --------------------------------------------------------------------------- |
6680 | | |
6681 | 0 | ConversionNNPtr JSONParser::buildConversion(const json &j) { |
6682 | 0 | auto methodJ = getObject(j, "method"); |
6683 | 0 | auto convProps = buildProperties(j); |
6684 | 0 | auto methodProps = buildProperties(methodJ); |
6685 | 0 | if (!j.contains("parameters")) { |
6686 | 0 | return Conversion::create(convProps, methodProps, {}, {}); |
6687 | 0 | } |
6688 | | |
6689 | 0 | auto parametersJ = getArray(j, "parameters"); |
6690 | 0 | std::vector<OperationParameterNNPtr> parameters; |
6691 | 0 | std::vector<ParameterValueNNPtr> values; |
6692 | 0 | for (const auto ¶m : parametersJ) { |
6693 | 0 | if (!param.is_object()) { |
6694 | 0 | throw ParsingException( |
6695 | 0 | "Unexpected type for a \"parameters\" child"); |
6696 | 0 | } |
6697 | 0 | parameters.emplace_back( |
6698 | 0 | OperationParameter::create(buildProperties(param))); |
6699 | 0 | if (isIntegerParameter(parameters.back())) { |
6700 | 0 | values.emplace_back( |
6701 | 0 | ParameterValue::create(getInteger(param, "value"))); |
6702 | 0 | } else { |
6703 | 0 | values.emplace_back(ParameterValue::create(getMeasure(param))); |
6704 | 0 | } |
6705 | 0 | } |
6706 | | |
6707 | 0 | auto interpolationCRS = dealWithEPSGCodeForInterpolationCRSParameter( |
6708 | 0 | dbContext_, parameters, values); |
6709 | |
|
6710 | 0 | std::string convName; |
6711 | 0 | std::string methodName; |
6712 | 0 | if (convProps.getStringValue(IdentifiedObject::NAME_KEY, convName) && |
6713 | 0 | methodProps.getStringValue(IdentifiedObject::NAME_KEY, methodName) && |
6714 | 0 | starts_with(convName, "Inverse of ") && |
6715 | 0 | starts_with(methodName, "Inverse of ")) { |
6716 | |
|
6717 | 0 | auto invConvProps = buildProperties(j, true); |
6718 | 0 | auto invMethodProps = buildProperties(methodJ, true); |
6719 | 0 | auto conv = NN_NO_CHECK(util::nn_dynamic_pointer_cast<Conversion>( |
6720 | 0 | Conversion::create(invConvProps, invMethodProps, parameters, values) |
6721 | 0 | ->inverse())); |
6722 | 0 | if (interpolationCRS) |
6723 | 0 | conv->setInterpolationCRS(interpolationCRS); |
6724 | 0 | return conv; |
6725 | 0 | } |
6726 | 0 | auto conv = Conversion::create(convProps, methodProps, parameters, values); |
6727 | 0 | if (interpolationCRS) |
6728 | 0 | conv->setInterpolationCRS(interpolationCRS); |
6729 | 0 | return conv; |
6730 | 0 | } |
6731 | | |
6732 | | // --------------------------------------------------------------------------- |
6733 | | |
6734 | 0 | BoundCRSNNPtr JSONParser::buildBoundCRS(const json &j) { |
6735 | |
|
6736 | 0 | auto sourceCRS = buildCRS(getObject(j, "source_crs")); |
6737 | 0 | auto targetCRS = buildCRS(getObject(j, "target_crs")); |
6738 | 0 | auto transformationJ = getObject(j, "transformation"); |
6739 | 0 | auto methodJ = getObject(transformationJ, "method"); |
6740 | 0 | auto parametersJ = getArray(transformationJ, "parameters"); |
6741 | 0 | std::vector<OperationParameterNNPtr> parameters; |
6742 | 0 | std::vector<ParameterValueNNPtr> values; |
6743 | 0 | for (const auto ¶m : parametersJ) { |
6744 | 0 | if (!param.is_object()) { |
6745 | 0 | throw ParsingException( |
6746 | 0 | "Unexpected type for a \"parameters\" child"); |
6747 | 0 | } |
6748 | 0 | parameters.emplace_back( |
6749 | 0 | OperationParameter::create(buildProperties(param))); |
6750 | 0 | if (param.contains("value")) { |
6751 | 0 | auto v = param["value"]; |
6752 | 0 | if (v.is_string()) { |
6753 | 0 | values.emplace_back( |
6754 | 0 | ParameterValue::createFilename(v.get<std::string>())); |
6755 | 0 | continue; |
6756 | 0 | } |
6757 | 0 | } |
6758 | 0 | values.emplace_back(ParameterValue::create(getMeasure(param))); |
6759 | 0 | } |
6760 | | |
6761 | 0 | const auto transformation = [&]() { |
6762 | | // Unofficial extension / mostly for testing purposes. |
6763 | | // Allow to explicitly specify the source_crs of the transformation of |
6764 | | // the boundCRS if it is not the source_crs of the BoundCRS. Cf |
6765 | | // https://github.com/OSGeo/PROJ/issues/3428 use case |
6766 | 0 | if (transformationJ.contains("source_crs")) { |
6767 | 0 | auto sourceTransformationCRS = |
6768 | 0 | buildCRS(getObject(transformationJ, "source_crs")); |
6769 | 0 | auto interpolationCRS = |
6770 | 0 | dealWithEPSGCodeForInterpolationCRSParameter( |
6771 | 0 | dbContext_, parameters, values); |
6772 | 0 | return Transformation::create( |
6773 | 0 | buildProperties(transformationJ), sourceTransformationCRS, |
6774 | 0 | targetCRS, interpolationCRS, buildProperties(methodJ), |
6775 | 0 | parameters, values, std::vector<PositionalAccuracyNNPtr>()); |
6776 | 0 | } |
6777 | | |
6778 | 0 | return buildTransformationForBoundCRS( |
6779 | 0 | dbContext_, buildProperties(transformationJ), |
6780 | 0 | buildProperties(methodJ), sourceCRS, targetCRS, parameters, values); |
6781 | 0 | }(); |
6782 | |
|
6783 | 0 | return BoundCRS::create(buildProperties(j, |
6784 | 0 | /* removeInverseOf= */ false, |
6785 | 0 | /* nameRequired=*/false), |
6786 | 0 | sourceCRS, targetCRS, transformation); |
6787 | 0 | } |
6788 | | |
6789 | | // --------------------------------------------------------------------------- |
6790 | | |
6791 | 0 | TransformationNNPtr JSONParser::buildTransformation(const json &j) { |
6792 | |
|
6793 | 0 | auto sourceCRS = buildCRS(getObject(j, "source_crs")); |
6794 | 0 | auto targetCRS = buildCRS(getObject(j, "target_crs")); |
6795 | 0 | auto methodJ = getObject(j, "method"); |
6796 | 0 | auto parametersJ = getArray(j, "parameters"); |
6797 | 0 | std::vector<OperationParameterNNPtr> parameters; |
6798 | 0 | std::vector<ParameterValueNNPtr> values; |
6799 | 0 | for (const auto ¶m : parametersJ) { |
6800 | 0 | if (!param.is_object()) { |
6801 | 0 | throw ParsingException( |
6802 | 0 | "Unexpected type for a \"parameters\" child"); |
6803 | 0 | } |
6804 | 0 | parameters.emplace_back( |
6805 | 0 | OperationParameter::create(buildProperties(param))); |
6806 | 0 | if (param.contains("value")) { |
6807 | 0 | auto v = param["value"]; |
6808 | 0 | if (v.is_string()) { |
6809 | 0 | values.emplace_back( |
6810 | 0 | ParameterValue::createFilename(v.get<std::string>())); |
6811 | 0 | continue; |
6812 | 0 | } |
6813 | 0 | } |
6814 | 0 | values.emplace_back(ParameterValue::create(getMeasure(param))); |
6815 | 0 | } |
6816 | 0 | CRSPtr interpolationCRS; |
6817 | 0 | if (j.contains("interpolation_crs")) { |
6818 | 0 | interpolationCRS = |
6819 | 0 | buildCRS(getObject(j, "interpolation_crs")).as_nullable(); |
6820 | 0 | } |
6821 | 0 | std::vector<PositionalAccuracyNNPtr> accuracies; |
6822 | 0 | if (j.contains("accuracy")) { |
6823 | 0 | accuracies.push_back( |
6824 | 0 | PositionalAccuracy::create(getString(j, "accuracy"))); |
6825 | 0 | } |
6826 | |
|
6827 | 0 | return Transformation::create(buildProperties(j), sourceCRS, targetCRS, |
6828 | 0 | interpolationCRS, buildProperties(methodJ), |
6829 | 0 | parameters, values, accuracies); |
6830 | 0 | } |
6831 | | |
6832 | | // --------------------------------------------------------------------------- |
6833 | | |
6834 | 0 | PointMotionOperationNNPtr JSONParser::buildPointMotionOperation(const json &j) { |
6835 | |
|
6836 | 0 | auto sourceCRS = buildCRS(getObject(j, "source_crs")); |
6837 | 0 | auto methodJ = getObject(j, "method"); |
6838 | 0 | auto parametersJ = getArray(j, "parameters"); |
6839 | 0 | std::vector<OperationParameterNNPtr> parameters; |
6840 | 0 | std::vector<ParameterValueNNPtr> values; |
6841 | 0 | for (const auto ¶m : parametersJ) { |
6842 | 0 | if (!param.is_object()) { |
6843 | 0 | throw ParsingException( |
6844 | 0 | "Unexpected type for a \"parameters\" child"); |
6845 | 0 | } |
6846 | 0 | parameters.emplace_back( |
6847 | 0 | OperationParameter::create(buildProperties(param))); |
6848 | 0 | if (param.contains("value")) { |
6849 | 0 | auto v = param["value"]; |
6850 | 0 | if (v.is_string()) { |
6851 | 0 | values.emplace_back( |
6852 | 0 | ParameterValue::createFilename(v.get<std::string>())); |
6853 | 0 | continue; |
6854 | 0 | } |
6855 | 0 | } |
6856 | 0 | values.emplace_back(ParameterValue::create(getMeasure(param))); |
6857 | 0 | } |
6858 | 0 | std::vector<PositionalAccuracyNNPtr> accuracies; |
6859 | 0 | if (j.contains("accuracy")) { |
6860 | 0 | accuracies.push_back( |
6861 | 0 | PositionalAccuracy::create(getString(j, "accuracy"))); |
6862 | 0 | } |
6863 | |
|
6864 | 0 | return PointMotionOperation::create(buildProperties(j), sourceCRS, |
6865 | 0 | buildProperties(methodJ), parameters, |
6866 | 0 | values, accuracies); |
6867 | 0 | } |
6868 | | |
6869 | | // --------------------------------------------------------------------------- |
6870 | | |
6871 | | ConcatenatedOperationNNPtr |
6872 | 0 | JSONParser::buildConcatenatedOperation(const json &j) { |
6873 | |
|
6874 | 0 | auto sourceCRS = buildCRS(getObject(j, "source_crs")); |
6875 | 0 | auto targetCRS = buildCRS(getObject(j, "target_crs")); |
6876 | 0 | auto stepsJ = getArray(j, "steps"); |
6877 | 0 | std::vector<CoordinateOperationNNPtr> operations; |
6878 | 0 | for (const auto &stepJ : stepsJ) { |
6879 | 0 | if (!stepJ.is_object()) { |
6880 | 0 | throw ParsingException("Unexpected type for a \"steps\" child"); |
6881 | 0 | } |
6882 | 0 | auto op = nn_dynamic_pointer_cast<CoordinateOperation>(create(stepJ)); |
6883 | 0 | if (!op) { |
6884 | 0 | throw ParsingException("Invalid content in a \"steps\" child"); |
6885 | 0 | } |
6886 | 0 | operations.emplace_back(NN_NO_CHECK(op)); |
6887 | 0 | } |
6888 | | |
6889 | 0 | ConcatenatedOperation::fixSteps(sourceCRS, targetCRS, operations, |
6890 | 0 | dbContext_, |
6891 | 0 | /* fixDirectionAllowed = */ true); |
6892 | |
|
6893 | 0 | std::vector<PositionalAccuracyNNPtr> accuracies; |
6894 | 0 | if (j.contains("accuracy")) { |
6895 | 0 | accuracies.push_back( |
6896 | 0 | PositionalAccuracy::create(getString(j, "accuracy"))); |
6897 | 0 | } |
6898 | |
|
6899 | 0 | try { |
6900 | 0 | return ConcatenatedOperation::create(buildProperties(j), operations, |
6901 | 0 | accuracies); |
6902 | 0 | } catch (const InvalidOperation &e) { |
6903 | 0 | throw ParsingException( |
6904 | 0 | std::string("Cannot build concatenated operation: ") + e.what()); |
6905 | 0 | } |
6906 | 0 | } |
6907 | | |
6908 | | // --------------------------------------------------------------------------- |
6909 | | |
6910 | 1 | CoordinateMetadataNNPtr JSONParser::buildCoordinateMetadata(const json &j) { |
6911 | | |
6912 | 1 | auto crs = buildCRS(getObject(j, "crs")); |
6913 | 1 | if (j.contains("coordinateEpoch")) { |
6914 | 0 | auto jCoordinateEpoch = j["coordinateEpoch"]; |
6915 | 0 | if (jCoordinateEpoch.is_number()) { |
6916 | 0 | return CoordinateMetadata::create( |
6917 | 0 | crs, jCoordinateEpoch.get<double>(), dbContext_); |
6918 | 0 | } |
6919 | 0 | throw ParsingException( |
6920 | 0 | "Unexpected type for value of \"coordinateEpoch\""); |
6921 | 0 | } |
6922 | 1 | return CoordinateMetadata::create(crs); |
6923 | 1 | } |
6924 | | |
6925 | | // --------------------------------------------------------------------------- |
6926 | | |
6927 | 0 | MeridianNNPtr JSONParser::buildMeridian(const json &j) { |
6928 | 0 | if (!j.contains("longitude")) { |
6929 | 0 | throw ParsingException("Missing \"longitude\" key"); |
6930 | 0 | } |
6931 | 0 | auto longitude = j["longitude"]; |
6932 | 0 | if (longitude.is_number()) { |
6933 | 0 | return Meridian::create( |
6934 | 0 | Angle(longitude.get<double>(), UnitOfMeasure::DEGREE)); |
6935 | 0 | } else if (longitude.is_object()) { |
6936 | 0 | return Meridian::create(Angle(getMeasure(longitude))); |
6937 | 0 | } |
6938 | 0 | throw ParsingException("Unexpected type for value of \"longitude\""); |
6939 | 0 | } |
6940 | | |
6941 | | // --------------------------------------------------------------------------- |
6942 | | |
6943 | 0 | CoordinateSystemAxisNNPtr JSONParser::buildAxis(const json &j) { |
6944 | 0 | auto dirString = getString(j, "direction"); |
6945 | 0 | auto abbreviation = getString(j, "abbreviation"); |
6946 | 0 | const UnitOfMeasure unit( |
6947 | 0 | j.contains("unit") |
6948 | 0 | ? getUnit(j, "unit") |
6949 | 0 | : UnitOfMeasure(std::string(), 1.0, UnitOfMeasure::Type::NONE)); |
6950 | 0 | auto direction = AxisDirection::valueOf(dirString); |
6951 | 0 | if (!direction) { |
6952 | 0 | throw ParsingException(concat("unhandled axis direction: ", dirString)); |
6953 | 0 | } |
6954 | 0 | auto meridian = j.contains("meridian") |
6955 | 0 | ? buildMeridian(getObject(j, "meridian")).as_nullable() |
6956 | 0 | : nullptr; |
6957 | |
|
6958 | 0 | util::optional<double> minVal; |
6959 | 0 | if (j.contains("minimum_value")) { |
6960 | 0 | minVal = getNumber(j, "minimum_value"); |
6961 | 0 | } |
6962 | |
|
6963 | 0 | util::optional<double> maxVal; |
6964 | 0 | if (j.contains("maximum_value")) { |
6965 | 0 | maxVal = getNumber(j, "maximum_value"); |
6966 | 0 | } |
6967 | |
|
6968 | 0 | util::optional<RangeMeaning> rangeMeaning; |
6969 | 0 | if (j.contains("range_meaning")) { |
6970 | 0 | const auto val = getString(j, "range_meaning"); |
6971 | 0 | const RangeMeaning *meaning = RangeMeaning::valueOf(val); |
6972 | 0 | if (meaning == nullptr) { |
6973 | 0 | throw ParsingException( |
6974 | 0 | concat("buildAxis: invalid range_meaning value: ", val)); |
6975 | 0 | } |
6976 | 0 | rangeMeaning = util::optional<RangeMeaning>(*meaning); |
6977 | 0 | } |
6978 | | |
6979 | 0 | return CoordinateSystemAxis::create(buildProperties(j), abbreviation, |
6980 | 0 | *direction, unit, minVal, maxVal, |
6981 | 0 | rangeMeaning, meridian); |
6982 | 0 | } |
6983 | | |
6984 | | // --------------------------------------------------------------------------- |
6985 | | |
6986 | 0 | CoordinateSystemNNPtr JSONParser::buildCS(const json &j) { |
6987 | 0 | auto subtype = getString(j, "subtype"); |
6988 | 0 | if (!j.contains("axis")) { |
6989 | 0 | throw ParsingException("Missing \"axis\" key"); |
6990 | 0 | } |
6991 | 0 | auto jAxisList = j["axis"]; |
6992 | 0 | if (!jAxisList.is_array()) { |
6993 | 0 | throw ParsingException("Unexpected type for value of \"axis\""); |
6994 | 0 | } |
6995 | 0 | std::vector<CoordinateSystemAxisNNPtr> axisList; |
6996 | 0 | for (const auto &axis : jAxisList) { |
6997 | 0 | if (!axis.is_object()) { |
6998 | 0 | throw ParsingException( |
6999 | 0 | "Unexpected type for value of a \"axis\" member"); |
7000 | 0 | } |
7001 | 0 | axisList.emplace_back(buildAxis(axis)); |
7002 | 0 | } |
7003 | 0 | const PropertyMap &csMap = emptyPropertyMap; |
7004 | 0 | const auto axisCount = axisList.size(); |
7005 | 0 | if (subtype == EllipsoidalCS::WKT2_TYPE) { |
7006 | 0 | if (axisCount == 2) { |
7007 | 0 | return EllipsoidalCS::create(csMap, axisList[0], axisList[1]); |
7008 | 0 | } |
7009 | 0 | if (axisCount == 3) { |
7010 | 0 | return EllipsoidalCS::create(csMap, axisList[0], axisList[1], |
7011 | 0 | axisList[2]); |
7012 | 0 | } |
7013 | 0 | throw ParsingException("Expected 2 or 3 axis"); |
7014 | 0 | } |
7015 | 0 | if (subtype == CartesianCS::WKT2_TYPE) { |
7016 | 0 | if (axisCount == 2) { |
7017 | 0 | return CartesianCS::create(csMap, axisList[0], axisList[1]); |
7018 | 0 | } |
7019 | 0 | if (axisCount == 3) { |
7020 | 0 | return CartesianCS::create(csMap, axisList[0], axisList[1], |
7021 | 0 | axisList[2]); |
7022 | 0 | } |
7023 | 0 | throw ParsingException("Expected 2 or 3 axis"); |
7024 | 0 | } |
7025 | 0 | if (subtype == AffineCS::WKT2_TYPE) { |
7026 | 0 | if (axisCount == 2) { |
7027 | 0 | return AffineCS::create(csMap, axisList[0], axisList[1]); |
7028 | 0 | } |
7029 | 0 | if (axisCount == 3) { |
7030 | 0 | return AffineCS::create(csMap, axisList[0], axisList[1], |
7031 | 0 | axisList[2]); |
7032 | 0 | } |
7033 | 0 | throw ParsingException("Expected 2 or 3 axis"); |
7034 | 0 | } |
7035 | 0 | if (subtype == VerticalCS::WKT2_TYPE) { |
7036 | 0 | if (axisCount == 1) { |
7037 | 0 | return VerticalCS::create(csMap, axisList[0]); |
7038 | 0 | } |
7039 | 0 | throw ParsingException("Expected 1 axis"); |
7040 | 0 | } |
7041 | 0 | if (subtype == SphericalCS::WKT2_TYPE) { |
7042 | 0 | if (axisCount == 2) { |
7043 | | // Extension to ISO19111 to support (planet)-ocentric CS with |
7044 | | // geocentric latitude |
7045 | 0 | return SphericalCS::create(csMap, axisList[0], axisList[1]); |
7046 | 0 | } else if (axisCount == 3) { |
7047 | 0 | return SphericalCS::create(csMap, axisList[0], axisList[1], |
7048 | 0 | axisList[2]); |
7049 | 0 | } |
7050 | 0 | throw ParsingException("Expected 2 or 3 axis"); |
7051 | 0 | } |
7052 | 0 | if (subtype == OrdinalCS::WKT2_TYPE) { |
7053 | 0 | return OrdinalCS::create(csMap, axisList); |
7054 | 0 | } |
7055 | 0 | if (subtype == ParametricCS::WKT2_TYPE) { |
7056 | 0 | if (axisCount == 1) { |
7057 | 0 | return ParametricCS::create(csMap, axisList[0]); |
7058 | 0 | } |
7059 | 0 | throw ParsingException("Expected 1 axis"); |
7060 | 0 | } |
7061 | 0 | if (subtype == DateTimeTemporalCS::WKT2_2019_TYPE) { |
7062 | 0 | if (axisCount == 1) { |
7063 | 0 | return DateTimeTemporalCS::create(csMap, axisList[0]); |
7064 | 0 | } |
7065 | 0 | throw ParsingException("Expected 1 axis"); |
7066 | 0 | } |
7067 | 0 | if (subtype == TemporalCountCS::WKT2_2019_TYPE) { |
7068 | 0 | if (axisCount == 1) { |
7069 | 0 | return TemporalCountCS::create(csMap, axisList[0]); |
7070 | 0 | } |
7071 | 0 | throw ParsingException("Expected 1 axis"); |
7072 | 0 | } |
7073 | 0 | if (subtype == TemporalMeasureCS::WKT2_2019_TYPE) { |
7074 | 0 | if (axisCount == 1) { |
7075 | 0 | return TemporalMeasureCS::create(csMap, axisList[0]); |
7076 | 0 | } |
7077 | 0 | throw ParsingException("Expected 1 axis"); |
7078 | 0 | } |
7079 | 0 | throw ParsingException("Unhandled value for subtype"); |
7080 | 0 | } |
7081 | | |
7082 | | // --------------------------------------------------------------------------- |
7083 | | |
7084 | 0 | DatumEnsembleNNPtr JSONParser::buildDatumEnsemble(const json &j) { |
7085 | 0 | auto membersJ = getArray(j, "members"); |
7086 | 0 | std::vector<DatumNNPtr> datums; |
7087 | 0 | const bool hasEllipsoid(j.contains("ellipsoid")); |
7088 | 0 | for (const auto &memberJ : membersJ) { |
7089 | 0 | if (!memberJ.is_object()) { |
7090 | 0 | throw ParsingException( |
7091 | 0 | "Unexpected type for value of a \"members\" member"); |
7092 | 0 | } |
7093 | 0 | auto datumName(getName(memberJ)); |
7094 | 0 | bool datumAdded = false; |
7095 | 0 | if (dbContext_ && memberJ.contains("id")) { |
7096 | 0 | auto id = getObject(memberJ, "id"); |
7097 | 0 | auto authority = getString(id, "authority"); |
7098 | 0 | auto authFactory = |
7099 | 0 | AuthorityFactory::create(NN_NO_CHECK(dbContext_), authority); |
7100 | 0 | auto code = id["code"]; |
7101 | 0 | std::string codeStr; |
7102 | 0 | if (code.is_string()) { |
7103 | 0 | codeStr = code.get<std::string>(); |
7104 | 0 | } else if (code.is_number_integer()) { |
7105 | 0 | codeStr = internal::toString(code.get<int>()); |
7106 | 0 | } else { |
7107 | 0 | throw ParsingException("Unexpected type for value of \"code\""); |
7108 | 0 | } |
7109 | 0 | try { |
7110 | 0 | datums.push_back(authFactory->createDatum(codeStr)); |
7111 | 0 | datumAdded = true; |
7112 | 0 | } catch (const std::exception &) { |
7113 | | // Silently ignore, as this isn't necessary an error. |
7114 | | // If an older PROJ version parses a DatumEnsemble object of |
7115 | | // a more recent PROJ version where the datum ensemble got |
7116 | | // a new member, it might be unknown from the older PROJ. |
7117 | 0 | } |
7118 | 0 | } |
7119 | | |
7120 | 0 | if (dbContext_ && !datumAdded) { |
7121 | 0 | auto authFactory = AuthorityFactory::create(NN_NO_CHECK(dbContext_), |
7122 | 0 | std::string()); |
7123 | 0 | auto list = authFactory->createObjectsFromName( |
7124 | 0 | datumName, {AuthorityFactory::ObjectType::DATUM}, |
7125 | 0 | false /* approximate=false*/); |
7126 | 0 | if (!list.empty()) { |
7127 | 0 | auto datum = util::nn_dynamic_pointer_cast<Datum>(list.front()); |
7128 | 0 | if (!datum) |
7129 | 0 | throw ParsingException( |
7130 | 0 | "DatumEnsemble member is not a datum"); |
7131 | 0 | datums.push_back(NN_NO_CHECK(datum)); |
7132 | 0 | datumAdded = true; |
7133 | 0 | } |
7134 | 0 | } |
7135 | | |
7136 | 0 | if (!datumAdded) { |
7137 | | // Fallback if no db match |
7138 | 0 | if (hasEllipsoid) { |
7139 | 0 | datums.emplace_back(GeodeticReferenceFrame::create( |
7140 | 0 | buildProperties(memberJ), |
7141 | 0 | buildEllipsoid(getObject(j, "ellipsoid")), |
7142 | 0 | optional<std::string>(), PrimeMeridian::GREENWICH)); |
7143 | 0 | } else { |
7144 | 0 | datums.emplace_back( |
7145 | 0 | VerticalReferenceFrame::create(buildProperties(memberJ))); |
7146 | 0 | } |
7147 | 0 | } |
7148 | 0 | } |
7149 | 0 | return DatumEnsemble::create( |
7150 | 0 | buildProperties(j), datums, |
7151 | 0 | PositionalAccuracy::create(getString(j, "accuracy"))); |
7152 | 0 | } |
7153 | | |
7154 | | // --------------------------------------------------------------------------- |
7155 | | |
7156 | | GeodeticReferenceFrameNNPtr |
7157 | 0 | JSONParser::buildGeodeticReferenceFrame(const json &j) { |
7158 | 0 | auto ellipsoidJ = getObject(j, "ellipsoid"); |
7159 | 0 | auto pm = j.contains("prime_meridian") |
7160 | 0 | ? buildPrimeMeridian(getObject(j, "prime_meridian")) |
7161 | 0 | : PrimeMeridian::GREENWICH; |
7162 | 0 | return GeodeticReferenceFrame::create(buildProperties(j), |
7163 | 0 | buildEllipsoid(ellipsoidJ), |
7164 | 0 | getAnchor(j), getAnchorEpoch(j), pm); |
7165 | 0 | } |
7166 | | |
7167 | | // --------------------------------------------------------------------------- |
7168 | | |
7169 | | DynamicGeodeticReferenceFrameNNPtr |
7170 | 0 | JSONParser::buildDynamicGeodeticReferenceFrame(const json &j) { |
7171 | 0 | auto ellipsoidJ = getObject(j, "ellipsoid"); |
7172 | 0 | auto pm = j.contains("prime_meridian") |
7173 | 0 | ? buildPrimeMeridian(getObject(j, "prime_meridian")) |
7174 | 0 | : PrimeMeridian::GREENWICH; |
7175 | 0 | Measure frameReferenceEpoch(getNumber(j, "frame_reference_epoch"), |
7176 | 0 | UnitOfMeasure::YEAR); |
7177 | 0 | optional<std::string> deformationModel; |
7178 | 0 | if (j.contains("deformation_model")) { |
7179 | | // Before PROJJSON v0.5 / PROJ 9.1 |
7180 | 0 | deformationModel = getString(j, "deformation_model"); |
7181 | 0 | } else if (!deformationModelName_.empty()) { |
7182 | 0 | deformationModel = deformationModelName_; |
7183 | 0 | } |
7184 | 0 | return DynamicGeodeticReferenceFrame::create( |
7185 | 0 | buildProperties(j), buildEllipsoid(ellipsoidJ), getAnchor(j), pm, |
7186 | 0 | frameReferenceEpoch, deformationModel); |
7187 | 0 | } |
7188 | | |
7189 | | // --------------------------------------------------------------------------- |
7190 | | |
7191 | | VerticalReferenceFrameNNPtr |
7192 | 1 | JSONParser::buildVerticalReferenceFrame(const json &j) { |
7193 | 1 | return VerticalReferenceFrame::create(buildProperties(j), getAnchor(j), |
7194 | 1 | getAnchorEpoch(j)); |
7195 | 1 | } |
7196 | | |
7197 | | // --------------------------------------------------------------------------- |
7198 | | |
7199 | | DynamicVerticalReferenceFrameNNPtr |
7200 | 0 | JSONParser::buildDynamicVerticalReferenceFrame(const json &j) { |
7201 | 0 | Measure frameReferenceEpoch(getNumber(j, "frame_reference_epoch"), |
7202 | 0 | UnitOfMeasure::YEAR); |
7203 | 0 | optional<std::string> deformationModel; |
7204 | 0 | if (j.contains("deformation_model")) { |
7205 | | // Before PROJJSON v0.5 / PROJ 9.1 |
7206 | 0 | deformationModel = getString(j, "deformation_model"); |
7207 | 0 | } else if (!deformationModelName_.empty()) { |
7208 | 0 | deformationModel = deformationModelName_; |
7209 | 0 | } |
7210 | 0 | return DynamicVerticalReferenceFrame::create( |
7211 | 0 | buildProperties(j), getAnchor(j), util::optional<RealizationMethod>(), |
7212 | 0 | frameReferenceEpoch, deformationModel); |
7213 | 0 | } |
7214 | | |
7215 | | // --------------------------------------------------------------------------- |
7216 | | |
7217 | 0 | PrimeMeridianNNPtr JSONParser::buildPrimeMeridian(const json &j) { |
7218 | 0 | if (!j.contains("longitude")) { |
7219 | 0 | throw ParsingException("Missing \"longitude\" key"); |
7220 | 0 | } |
7221 | 0 | auto longitude = j["longitude"]; |
7222 | 0 | if (longitude.is_number()) { |
7223 | 0 | return PrimeMeridian::create( |
7224 | 0 | buildProperties(j), |
7225 | 0 | Angle(longitude.get<double>(), UnitOfMeasure::DEGREE)); |
7226 | 0 | } else if (longitude.is_object()) { |
7227 | 0 | return PrimeMeridian::create(buildProperties(j), |
7228 | 0 | Angle(getMeasure(longitude))); |
7229 | 0 | } |
7230 | 0 | throw ParsingException("Unexpected type for value of \"longitude\""); |
7231 | 0 | } |
7232 | | |
7233 | | // --------------------------------------------------------------------------- |
7234 | | |
7235 | 0 | EllipsoidNNPtr JSONParser::buildEllipsoid(const json &j) { |
7236 | 0 | if (j.contains("semi_major_axis")) { |
7237 | 0 | auto semiMajorAxis = getLength(j, "semi_major_axis"); |
7238 | 0 | const auto ellpsProperties = buildProperties(j); |
7239 | 0 | std::string ellpsName; |
7240 | 0 | ellpsProperties.getStringValue(IdentifiedObject::NAME_KEY, ellpsName); |
7241 | 0 | const auto celestialBody(Ellipsoid::guessBodyName( |
7242 | 0 | dbContext_, semiMajorAxis.getSIValue(), ellpsName)); |
7243 | 0 | if (j.contains("semi_minor_axis")) { |
7244 | 0 | return Ellipsoid::createTwoAxis(ellpsProperties, semiMajorAxis, |
7245 | 0 | getLength(j, "semi_minor_axis"), |
7246 | 0 | celestialBody); |
7247 | 0 | } else if (j.contains("inverse_flattening")) { |
7248 | 0 | return Ellipsoid::createFlattenedSphere( |
7249 | 0 | ellpsProperties, semiMajorAxis, |
7250 | 0 | Scale(getNumber(j, "inverse_flattening")), celestialBody); |
7251 | 0 | } else { |
7252 | 0 | throw ParsingException( |
7253 | 0 | "Missing semi_minor_axis or inverse_flattening"); |
7254 | 0 | } |
7255 | 0 | } else if (j.contains("radius")) { |
7256 | 0 | auto radius = getLength(j, "radius"); |
7257 | 0 | const auto celestialBody( |
7258 | 0 | Ellipsoid::guessBodyName(dbContext_, radius.getSIValue())); |
7259 | 0 | return Ellipsoid::createSphere(buildProperties(j), radius, |
7260 | 0 | celestialBody); |
7261 | 0 | } |
7262 | 0 | throw ParsingException("Missing semi_major_axis or radius"); |
7263 | 0 | } |
7264 | | |
7265 | | //! @endcond |
7266 | | |
7267 | | // --------------------------------------------------------------------------- |
7268 | | |
7269 | | //! @cond Doxygen_Suppress |
7270 | | |
7271 | | // import a CRS encoded as OGC Best Practice document 11-135. |
7272 | | |
7273 | | static const char *const crsURLPrefixes[] = { |
7274 | | "http://opengis.net/def/crs", "https://opengis.net/def/crs", |
7275 | | "http://www.opengis.net/def/crs", "https://www.opengis.net/def/crs", |
7276 | | "www.opengis.net/def/crs", |
7277 | | }; |
7278 | | |
7279 | 3.44k | static bool isCRSURL(const std::string &text) { |
7280 | 17.1k | for (const auto crsURLPrefix : crsURLPrefixes) { |
7281 | 17.1k | if (starts_with(text, crsURLPrefix)) { |
7282 | 34 | return true; |
7283 | 34 | } |
7284 | 17.1k | } |
7285 | 3.41k | return false; |
7286 | 3.44k | } |
7287 | | |
7288 | | static CRSNNPtr importFromCRSURL(const std::string &text, |
7289 | 32 | const DatabaseContextNNPtr &dbContext) { |
7290 | | // e.g http://www.opengis.net/def/crs/EPSG/0/4326 |
7291 | 32 | std::vector<std::string> parts; |
7292 | 122 | for (const auto crsURLPrefix : crsURLPrefixes) { |
7293 | 122 | if (starts_with(text, crsURLPrefix)) { |
7294 | 32 | parts = split(text.substr(strlen(crsURLPrefix)), '/'); |
7295 | 32 | break; |
7296 | 32 | } |
7297 | 122 | } |
7298 | | |
7299 | | // e.g |
7300 | | // "http://www.opengis.net/def/crs-compound?1=http://www.opengis.net/def/crs/EPSG/0/4326&2=http://www.opengis.net/def/crs/EPSG/0/3855" |
7301 | 32 | if (!parts.empty() && starts_with(parts[0], "-compound?")) { |
7302 | 11 | parts = split(text.substr(text.find('?') + 1), '&'); |
7303 | 11 | std::map<int, std::string> mapParts; |
7304 | 11 | for (const auto &part : parts) { |
7305 | 11 | const auto queryParam = split(part, '='); |
7306 | 11 | if (queryParam.size() != 2) { |
7307 | 8 | throw ParsingException("invalid OGC CRS URL"); |
7308 | 8 | } |
7309 | 3 | try { |
7310 | 3 | mapParts[std::stoi(queryParam[0])] = queryParam[1]; |
7311 | 3 | } catch (const std::exception &) { |
7312 | 3 | throw ParsingException("invalid OGC CRS URL"); |
7313 | 3 | } |
7314 | 3 | } |
7315 | 0 | std::vector<CRSNNPtr> components; |
7316 | 0 | std::string name; |
7317 | 0 | for (size_t i = 1; i <= mapParts.size(); ++i) { |
7318 | 0 | const auto iter = mapParts.find(static_cast<int>(i)); |
7319 | 0 | if (iter == mapParts.end()) { |
7320 | 0 | throw ParsingException("invalid OGC CRS URL"); |
7321 | 0 | } |
7322 | 0 | components.emplace_back(importFromCRSURL(iter->second, dbContext)); |
7323 | 0 | if (!name.empty()) { |
7324 | 0 | name += " + "; |
7325 | 0 | } |
7326 | 0 | name += components.back()->nameStr(); |
7327 | 0 | } |
7328 | 0 | return CompoundCRS::create( |
7329 | 0 | util::PropertyMap().set(IdentifiedObject::NAME_KEY, name), |
7330 | 0 | components); |
7331 | 0 | } |
7332 | | |
7333 | 21 | if (parts.size() < 4) { |
7334 | 19 | throw ParsingException("invalid OGC CRS URL"); |
7335 | 19 | } |
7336 | | |
7337 | 2 | const auto &auth_name = parts[1]; |
7338 | 2 | const auto &code = parts[3]; |
7339 | 2 | try { |
7340 | 2 | auto factoryCRS = AuthorityFactory::create(dbContext, auth_name); |
7341 | 2 | return factoryCRS->createCoordinateReferenceSystem(code, true); |
7342 | 2 | } catch (...) { |
7343 | 2 | const auto &version = parts[2]; |
7344 | 2 | if (version.empty() || version == "0") { |
7345 | 1 | const auto authoritiesFromAuthName = |
7346 | 1 | dbContext->getVersionedAuthoritiesFromName(auth_name); |
7347 | 1 | for (const auto &authNameVersioned : authoritiesFromAuthName) { |
7348 | 0 | try { |
7349 | 0 | auto factoryCRS = |
7350 | 0 | AuthorityFactory::create(dbContext, authNameVersioned); |
7351 | 0 | return factoryCRS->createCoordinateReferenceSystem(code, |
7352 | 0 | true); |
7353 | 0 | } catch (...) { |
7354 | 0 | } |
7355 | 0 | } |
7356 | 1 | throw; |
7357 | 1 | } |
7358 | 1 | std::string authNameWithVersion; |
7359 | 1 | if (!dbContext->getVersionedAuthority(auth_name, version, |
7360 | 1 | authNameWithVersion)) { |
7361 | 1 | throw; |
7362 | 1 | } |
7363 | 0 | auto factoryCRS = |
7364 | 0 | AuthorityFactory::create(dbContext, authNameWithVersion); |
7365 | 0 | return factoryCRS->createCoordinateReferenceSystem(code, true); |
7366 | 1 | } |
7367 | 2 | } |
7368 | | |
7369 | | // --------------------------------------------------------------------------- |
7370 | | |
7371 | | /* Import a CRS encoded as WMSAUTO string. |
7372 | | * |
7373 | | * Note that the WMS 1.3 specification does not include the |
7374 | | * units code, while apparently earlier specs do. We try to |
7375 | | * guess around this. |
7376 | | * |
7377 | | * (code derived from GDAL's importFromWMSAUTO()) |
7378 | | */ |
7379 | | |
7380 | 171 | static CRSNNPtr importFromWMSAUTO(const std::string &text) { |
7381 | | |
7382 | 171 | int nUnitsId = 9001; |
7383 | 171 | double dfRefLong; |
7384 | 171 | double dfRefLat = 0.0; |
7385 | | |
7386 | 171 | assert(ci_starts_with(text, "AUTO:")); |
7387 | 171 | const auto parts = split(text.substr(strlen("AUTO:")), ','); |
7388 | | |
7389 | 171 | try { |
7390 | 171 | constexpr int AUTO_MOLLWEIDE = 42005; |
7391 | 171 | if (parts.size() == 4) { |
7392 | 7 | nUnitsId = std::stoi(parts[1]); |
7393 | 7 | dfRefLong = c_locale_stod(parts[2]); |
7394 | 7 | dfRefLat = c_locale_stod(parts[3]); |
7395 | 164 | } else if (parts.size() == 3 && std::stoi(parts[0]) == AUTO_MOLLWEIDE) { |
7396 | 3 | nUnitsId = std::stoi(parts[1]); |
7397 | 3 | dfRefLong = c_locale_stod(parts[2]); |
7398 | 161 | } else if (parts.size() == 3) { |
7399 | 104 | dfRefLong = c_locale_stod(parts[1]); |
7400 | 104 | dfRefLat = c_locale_stod(parts[2]); |
7401 | 104 | } else if (parts.size() == 2 && std::stoi(parts[0]) == AUTO_MOLLWEIDE) { |
7402 | 14 | dfRefLong = c_locale_stod(parts[1]); |
7403 | 43 | } else { |
7404 | 43 | throw ParsingException("invalid WMS AUTO CRS definition"); |
7405 | 43 | } |
7406 | | |
7407 | 128 | const auto getConversion = [dfRefLong, dfRefLat, &parts]() { |
7408 | 67 | const int nProjId = std::stoi(parts[0]); |
7409 | 67 | switch (nProjId) { |
7410 | 1 | case 42001: // Auto UTM |
7411 | 1 | if (!(dfRefLong >= -180 && dfRefLong < 180)) { |
7412 | 0 | throw ParsingException("invalid WMS AUTO CRS definition: " |
7413 | 0 | "invalid longitude"); |
7414 | 0 | } |
7415 | 1 | return Conversion::createUTM( |
7416 | 1 | util::PropertyMap(), |
7417 | 1 | static_cast<int>(floor((dfRefLong + 180.0) / 6.0)) + 1, |
7418 | 1 | dfRefLat >= 0.0); |
7419 | | |
7420 | 10 | case 42002: // Auto TM (strangely very UTM-like). |
7421 | 10 | return Conversion::createTransverseMercator( |
7422 | 10 | util::PropertyMap(), common::Angle(0), |
7423 | 10 | common::Angle(dfRefLong), common::Scale(0.9996), |
7424 | 10 | common::Length(500000), |
7425 | 10 | common::Length((dfRefLat >= 0.0) ? 0.0 : 10000000.0)); |
7426 | | |
7427 | 17 | case 42003: // Auto Orthographic. |
7428 | 17 | return Conversion::createOrthographic( |
7429 | 17 | util::PropertyMap(), common::Angle(dfRefLat), |
7430 | 17 | common::Angle(dfRefLong), common::Length(0), |
7431 | 17 | common::Length(0)); |
7432 | | |
7433 | 10 | case 42004: // Auto Equirectangular |
7434 | 10 | return Conversion::createEquidistantCylindrical( |
7435 | 10 | util::PropertyMap(), common::Angle(dfRefLat), |
7436 | 10 | common::Angle(dfRefLong), common::Length(0), |
7437 | 10 | common::Length(0)); |
7438 | | |
7439 | 8 | case 42005: // MSVC 2015 thinks that AUTO_MOLLWEIDE is not constant |
7440 | 8 | return Conversion::createMollweide( |
7441 | 8 | util::PropertyMap(), common::Angle(dfRefLong), |
7442 | 8 | common::Length(0), common::Length(0)); |
7443 | | |
7444 | 21 | default: |
7445 | 21 | throw ParsingException("invalid WMS AUTO CRS definition: " |
7446 | 21 | "unsupported projection id"); |
7447 | 67 | } |
7448 | 67 | }; |
7449 | | |
7450 | 128 | const auto getUnits = [nUnitsId]() -> const UnitOfMeasure & { |
7451 | 46 | switch (nUnitsId) { |
7452 | 43 | case 9001: |
7453 | 43 | return UnitOfMeasure::METRE; |
7454 | | |
7455 | 0 | case 9002: |
7456 | 0 | return UnitOfMeasure::FOOT; |
7457 | | |
7458 | 0 | case 9003: |
7459 | 0 | return UnitOfMeasure::US_FOOT; |
7460 | | |
7461 | 3 | default: |
7462 | 3 | throw ParsingException("invalid WMS AUTO CRS definition: " |
7463 | 3 | "unsupported units code"); |
7464 | 46 | } |
7465 | 46 | }; |
7466 | | |
7467 | 128 | return crs::ProjectedCRS::create( |
7468 | 128 | util::PropertyMap().set(IdentifiedObject::NAME_KEY, "unnamed"), |
7469 | 128 | crs::GeographicCRS::EPSG_4326, getConversion(), |
7470 | 128 | cs::CartesianCS::createEastingNorthing(getUnits())); |
7471 | | |
7472 | 171 | } catch (const std::exception &) { |
7473 | 128 | throw ParsingException("invalid WMS AUTO CRS definition"); |
7474 | 128 | } |
7475 | 171 | } |
7476 | | |
7477 | | // --------------------------------------------------------------------------- |
7478 | | |
7479 | | static BaseObjectNNPtr createFromURNPart(const DatabaseContextPtr &dbContext, |
7480 | | const std::string &type, |
7481 | | const std::string &authName, |
7482 | | const std::string &version, |
7483 | 24 | const std::string &code) { |
7484 | 24 | if (!dbContext) { |
7485 | 1 | throw ParsingException("no database context specified"); |
7486 | 1 | } |
7487 | 23 | try { |
7488 | 23 | auto factory = |
7489 | 23 | AuthorityFactory::create(NN_NO_CHECK(dbContext), authName); |
7490 | 23 | if (type == "crs") { |
7491 | 0 | return factory->createCoordinateReferenceSystem(code); |
7492 | 0 | } |
7493 | 23 | if (type == "coordinateOperation") { |
7494 | 0 | return factory->createCoordinateOperation(code, true); |
7495 | 0 | } |
7496 | 23 | if (type == "datum") { |
7497 | 0 | return factory->createDatum(code); |
7498 | 0 | } |
7499 | 23 | if (type == "ensemble") { |
7500 | 0 | return factory->createDatumEnsemble(code); |
7501 | 0 | } |
7502 | 23 | if (type == "ellipsoid") { |
7503 | 0 | return factory->createEllipsoid(code); |
7504 | 0 | } |
7505 | 23 | if (type == "meridian") { |
7506 | 0 | return factory->createPrimeMeridian(code); |
7507 | 0 | } |
7508 | | // Extension of OGC URN syntax to CoordinateMetadata |
7509 | 23 | if (type == "coordinateMetadata") { |
7510 | 0 | return factory->createCoordinateMetadata(code); |
7511 | 0 | } |
7512 | 23 | throw ParsingException(concat("unhandled object type: ", type)); |
7513 | 23 | } catch (...) { |
7514 | 23 | if (version.empty()) { |
7515 | 12 | const auto authoritiesFromAuthName = |
7516 | 12 | dbContext->getVersionedAuthoritiesFromName(authName); |
7517 | 12 | for (const auto &authNameVersioned : authoritiesFromAuthName) { |
7518 | 0 | try { |
7519 | 0 | return createFromURNPart(dbContext, type, authNameVersioned, |
7520 | 0 | std::string(), code); |
7521 | 0 | } catch (...) { |
7522 | 0 | } |
7523 | 0 | } |
7524 | 12 | throw; |
7525 | 12 | } |
7526 | 11 | std::string authNameWithVersion; |
7527 | 11 | if (!dbContext->getVersionedAuthority(authName, version, |
7528 | 11 | authNameWithVersion)) { |
7529 | 11 | throw; |
7530 | 11 | } |
7531 | 0 | return createFromURNPart(dbContext, type, authNameWithVersion, |
7532 | 0 | std::string(), code); |
7533 | 11 | } |
7534 | 23 | } |
7535 | | |
7536 | | // --------------------------------------------------------------------------- |
7537 | | |
7538 | | static BaseObjectNNPtr createFromUserInput(const std::string &text, |
7539 | | const DatabaseContextPtr &dbContext, |
7540 | | bool usePROJ4InitRules, |
7541 | | PJ_CONTEXT *ctx, |
7542 | 22.9k | bool ignoreCoordinateEpoch) { |
7543 | 22.9k | std::size_t idxFirstCharNotSpace = text.find_first_not_of(" \t\r\n"); |
7544 | 22.9k | if (idxFirstCharNotSpace > 0 && idxFirstCharNotSpace != std::string::npos) { |
7545 | 443 | return createFromUserInput(text.substr(idxFirstCharNotSpace), dbContext, |
7546 | 443 | usePROJ4InitRules, ctx, |
7547 | 443 | ignoreCoordinateEpoch); |
7548 | 443 | } |
7549 | | |
7550 | | // Parse strings like "ITRF2014 @ 2025.0" |
7551 | 22.5k | const auto posAt = text.find('@'); |
7552 | 22.5k | if (!ignoreCoordinateEpoch && posAt != std::string::npos) { |
7553 | | |
7554 | | // Try first as if belonged to the name |
7555 | 4.24k | try { |
7556 | 4.24k | return createFromUserInput(text, dbContext, usePROJ4InitRules, ctx, |
7557 | 4.24k | /* ignoreCoordinateEpoch = */ true); |
7558 | 4.24k | } catch (...) { |
7559 | 2.36k | } |
7560 | | |
7561 | 2.36k | std::string leftPart = text.substr(0, posAt); |
7562 | 2.63k | while (!leftPart.empty() && leftPart.back() == ' ') |
7563 | 275 | leftPart.resize(leftPart.size() - 1); |
7564 | 2.36k | const auto nonSpacePos = text.find_first_not_of(' ', posAt + 1); |
7565 | 2.36k | if (nonSpacePos != std::string::npos) { |
7566 | 1.83k | auto obj = |
7567 | 1.83k | createFromUserInput(leftPart, dbContext, usePROJ4InitRules, ctx, |
7568 | 1.83k | /* ignoreCoordinateEpoch = */ true); |
7569 | 1.83k | auto crs = nn_dynamic_pointer_cast<CRS>(obj); |
7570 | 1.83k | if (crs) { |
7571 | 131 | double epoch; |
7572 | 131 | try { |
7573 | 131 | epoch = c_locale_stod(text.substr(nonSpacePos)); |
7574 | 131 | } catch (const std::exception &) { |
7575 | 60 | throw ParsingException("non-numeric value after @"); |
7576 | 60 | } |
7577 | 71 | try { |
7578 | 71 | return CoordinateMetadata::create(NN_NO_CHECK(crs), epoch, |
7579 | 71 | dbContext); |
7580 | 71 | } catch (const std::exception &e) { |
7581 | 10 | throw ParsingException( |
7582 | 10 | std::string( |
7583 | 10 | "CoordinateMetadata::create() failed with: ") + |
7584 | 10 | e.what()); |
7585 | 10 | } |
7586 | 71 | } |
7587 | 1.83k | } |
7588 | 2.36k | } |
7589 | | |
7590 | 20.5k | if (!text.empty() && text[0] == '{') { |
7591 | 1.87k | json j; |
7592 | 1.87k | try { |
7593 | 1.87k | j = json::parse(text); |
7594 | 1.87k | } catch (const std::exception &e) { |
7595 | 1.79k | throw ParsingException(e.what()); |
7596 | 1.79k | } |
7597 | 80 | return JSONParser().attachDatabaseContext(dbContext).create(j); |
7598 | 1.87k | } |
7599 | | |
7600 | 18.6k | if (!ci_starts_with(text, "step proj=") && |
7601 | 18.6k | !ci_starts_with(text, "step +proj=")) { |
7602 | 1.59M | for (const auto &wktConstant : WKTConstants::constants()) { |
7603 | 1.59M | if (ci_starts_with(text, wktConstant)) { |
7604 | 3.03k | for (auto wkt = text.c_str() + wktConstant.size(); *wkt != '\0'; |
7605 | 3.02k | ++wkt) { |
7606 | 3.02k | if (isspace(static_cast<unsigned char>(*wkt))) |
7607 | 352 | continue; |
7608 | 2.67k | if (*wkt == '[') { |
7609 | 2.58k | return WKTParser() |
7610 | 2.58k | .attachDatabaseContext(dbContext) |
7611 | 2.58k | .setStrict(false) |
7612 | 2.58k | .createFromWKT(text); |
7613 | 2.58k | } |
7614 | 92 | break; |
7615 | 2.67k | } |
7616 | 2.68k | } |
7617 | 1.59M | } |
7618 | 17.2k | } |
7619 | | |
7620 | 16.0k | const char *textWithoutPlusPrefix = text.c_str(); |
7621 | 16.0k | if (textWithoutPlusPrefix[0] == '+') |
7622 | 1.39k | textWithoutPlusPrefix++; |
7623 | | |
7624 | 16.0k | if (strncmp(textWithoutPlusPrefix, "proj=", strlen("proj=")) == 0 || |
7625 | 16.0k | text.find(" +proj=") != std::string::npos || |
7626 | 16.0k | text.find(" proj=") != std::string::npos || |
7627 | 16.0k | strncmp(textWithoutPlusPrefix, "init=", strlen("init=")) == 0 || |
7628 | 16.0k | text.find(" +init=") != std::string::npos || |
7629 | 16.0k | text.find(" init=") != std::string::npos || |
7630 | 16.0k | strncmp(textWithoutPlusPrefix, "title=", strlen("title=")) == 0) { |
7631 | 11.2k | return PROJStringParser() |
7632 | 11.2k | .attachDatabaseContext(dbContext) |
7633 | 11.2k | .attachContext(ctx) |
7634 | 11.2k | .setUsePROJ4InitRules(ctx != nullptr |
7635 | 11.2k | ? (proj_context_get_use_proj4_init_rules( |
7636 | 11.2k | ctx, false) == TRUE) |
7637 | 11.2k | : usePROJ4InitRules) |
7638 | 11.2k | .createFromPROJString(text); |
7639 | 11.2k | } |
7640 | | |
7641 | 4.83k | if (isCRSURL(text) && dbContext) { |
7642 | 32 | return importFromCRSURL(text, NN_NO_CHECK(dbContext)); |
7643 | 32 | } |
7644 | | |
7645 | 4.80k | if (ci_starts_with(text, "AUTO:")) { |
7646 | 143 | return importFromWMSAUTO(text); |
7647 | 143 | } |
7648 | | |
7649 | 4.66k | auto tokens = split(text, ':'); |
7650 | 4.66k | if (tokens.size() == 2) { |
7651 | 98 | if (!dbContext) { |
7652 | 6 | throw ParsingException("no database context specified"); |
7653 | 6 | } |
7654 | 92 | DatabaseContextNNPtr dbContextNNPtr(NN_NO_CHECK(dbContext)); |
7655 | 92 | const auto &authName = tokens[0]; |
7656 | 92 | const auto &code = tokens[1]; |
7657 | 92 | auto factory = AuthorityFactory::create(dbContextNNPtr, authName); |
7658 | 92 | try { |
7659 | 92 | return factory->createCoordinateReferenceSystem(code); |
7660 | 92 | } catch (...) { |
7661 | | |
7662 | | // Convenience for well-known misused code |
7663 | | // See https://github.com/OSGeo/PROJ/issues/1730 |
7664 | 92 | if (ci_equal(authName, "EPSG") && code == "102100") { |
7665 | 0 | factory = AuthorityFactory::create(dbContextNNPtr, "ESRI"); |
7666 | 0 | return factory->createCoordinateReferenceSystem(code); |
7667 | 0 | } |
7668 | | |
7669 | 92 | const auto authoritiesFromAuthName = |
7670 | 92 | dbContextNNPtr->getVersionedAuthoritiesFromName(authName); |
7671 | 92 | for (const auto &authNameVersioned : authoritiesFromAuthName) { |
7672 | 0 | factory = |
7673 | 0 | AuthorityFactory::create(dbContextNNPtr, authNameVersioned); |
7674 | 0 | try { |
7675 | 0 | return factory->createCoordinateReferenceSystem(code); |
7676 | 0 | } catch (...) { |
7677 | 0 | } |
7678 | 0 | } |
7679 | | |
7680 | 92 | const auto allAuthorities = dbContextNNPtr->getAuthorities(); |
7681 | 709 | for (const auto &authCandidate : allAuthorities) { |
7682 | 709 | if (ci_equal(authCandidate, authName)) { |
7683 | 16 | factory = |
7684 | 16 | AuthorityFactory::create(dbContextNNPtr, authCandidate); |
7685 | 16 | try { |
7686 | 16 | return factory->createCoordinateReferenceSystem(code); |
7687 | 16 | } catch (...) { |
7688 | | // EPSG:4326+3855 |
7689 | 16 | auto tokensCode = split(code, '+'); |
7690 | 16 | if (tokensCode.size() == 2) { |
7691 | 6 | auto crs1(factory->createCoordinateReferenceSystem( |
7692 | 6 | tokensCode[0], false)); |
7693 | 6 | auto crs2(factory->createCoordinateReferenceSystem( |
7694 | 6 | tokensCode[1], false)); |
7695 | 6 | return CompoundCRS::createLax( |
7696 | 6 | util::PropertyMap().set( |
7697 | 6 | IdentifiedObject::NAME_KEY, |
7698 | 6 | crs1->nameStr() + " + " + crs2->nameStr()), |
7699 | 6 | {crs1, crs2}, dbContext); |
7700 | 6 | } |
7701 | 10 | throw; |
7702 | 16 | } |
7703 | 16 | } |
7704 | 709 | } |
7705 | 76 | throw; |
7706 | 92 | } |
7707 | 4.56k | } else if (tokens.size() == 3) { |
7708 | | // ESRI:103668+EPSG:5703 ... compound |
7709 | 27 | auto tokensCenter = split(tokens[1], '+'); |
7710 | 27 | if (tokensCenter.size() == 2) { |
7711 | 6 | if (!dbContext) { |
7712 | 2 | throw ParsingException("no database context specified"); |
7713 | 2 | } |
7714 | 4 | DatabaseContextNNPtr dbContextNNPtr(NN_NO_CHECK(dbContext)); |
7715 | | |
7716 | 4 | const auto &authName1 = tokens[0]; |
7717 | 4 | const auto &code1 = tokensCenter[0]; |
7718 | 4 | const auto &authName2 = tokensCenter[1]; |
7719 | 4 | const auto &code2 = tokens[2]; |
7720 | | |
7721 | 4 | auto factory1 = AuthorityFactory::create(dbContextNNPtr, authName1); |
7722 | 4 | auto crs1 = factory1->createCoordinateReferenceSystem(code1, false); |
7723 | 4 | auto factory2 = AuthorityFactory::create(dbContextNNPtr, authName2); |
7724 | 4 | auto crs2 = factory2->createCoordinateReferenceSystem(code2, false); |
7725 | 4 | return CompoundCRS::createLax( |
7726 | 4 | util::PropertyMap().set(IdentifiedObject::NAME_KEY, |
7727 | 4 | crs1->nameStr() + " + " + |
7728 | 4 | crs2->nameStr()), |
7729 | 4 | {crs1, crs2}, dbContext); |
7730 | 6 | } |
7731 | 27 | } |
7732 | | |
7733 | 4.55k | if (starts_with(text, "urn:ogc:def:crs,")) { |
7734 | 11 | if (!dbContext) { |
7735 | 2 | throw ParsingException("no database context specified"); |
7736 | 2 | } |
7737 | 9 | auto tokensComma = split(text, ','); |
7738 | 9 | if (tokensComma.size() == 4 && starts_with(tokensComma[1], "crs:") && |
7739 | 9 | starts_with(tokensComma[2], "cs:") && |
7740 | 9 | starts_with(tokensComma[3], "coordinateOperation:")) { |
7741 | | // OGC 07-092r2: para 7.5.4 |
7742 | | // URN combined references for projected or derived CRSs |
7743 | 0 | const auto &crsPart = tokensComma[1]; |
7744 | 0 | const auto tokensCRS = split(crsPart, ':'); |
7745 | 0 | if (tokensCRS.size() != 4) { |
7746 | 0 | throw ParsingException( |
7747 | 0 | concat("invalid crs component: ", crsPart)); |
7748 | 0 | } |
7749 | 0 | auto factoryCRS = |
7750 | 0 | AuthorityFactory::create(NN_NO_CHECK(dbContext), tokensCRS[1]); |
7751 | 0 | auto baseCRS = |
7752 | 0 | factoryCRS->createCoordinateReferenceSystem(tokensCRS[3], true); |
7753 | |
|
7754 | 0 | const auto &csPart = tokensComma[2]; |
7755 | 0 | auto tokensCS = split(csPart, ':'); |
7756 | 0 | if (tokensCS.size() != 4) { |
7757 | 0 | throw ParsingException( |
7758 | 0 | concat("invalid cs component: ", csPart)); |
7759 | 0 | } |
7760 | 0 | auto factoryCS = |
7761 | 0 | AuthorityFactory::create(NN_NO_CHECK(dbContext), tokensCS[1]); |
7762 | 0 | auto cs = factoryCS->createCoordinateSystem(tokensCS[3]); |
7763 | |
|
7764 | 0 | const auto &opPart = tokensComma[3]; |
7765 | 0 | auto tokensOp = split(opPart, ':'); |
7766 | 0 | if (tokensOp.size() != 4) { |
7767 | 0 | throw ParsingException( |
7768 | 0 | concat("invalid coordinateOperation component: ", opPart)); |
7769 | 0 | } |
7770 | 0 | auto factoryOp = |
7771 | 0 | AuthorityFactory::create(NN_NO_CHECK(dbContext), tokensOp[1]); |
7772 | 0 | auto op = factoryOp->createCoordinateOperation(tokensOp[3], true); |
7773 | |
|
7774 | 0 | const auto &baseName = baseCRS->nameStr(); |
7775 | 0 | std::string name(baseName); |
7776 | 0 | auto geogCRS = |
7777 | 0 | util::nn_dynamic_pointer_cast<GeographicCRS>(baseCRS); |
7778 | 0 | if (geogCRS && |
7779 | 0 | geogCRS->coordinateSystem()->axisList().size() == 3 && |
7780 | 0 | baseName.find("3D") == std::string::npos) { |
7781 | 0 | name += " (3D)"; |
7782 | 0 | } |
7783 | 0 | name += " / "; |
7784 | 0 | name += op->nameStr(); |
7785 | 0 | auto props = |
7786 | 0 | util::PropertyMap().set(IdentifiedObject::NAME_KEY, name); |
7787 | |
|
7788 | 0 | if (auto conv = util::nn_dynamic_pointer_cast<Conversion>(op)) { |
7789 | 0 | auto convNN = NN_NO_CHECK(conv); |
7790 | 0 | if (geogCRS != nullptr) { |
7791 | 0 | auto geogCRSNN = NN_NO_CHECK(geogCRS); |
7792 | 0 | if (CartesianCSPtr ccs = |
7793 | 0 | util::nn_dynamic_pointer_cast<CartesianCS>(cs)) { |
7794 | 0 | return ProjectedCRS::create(props, geogCRSNN, convNN, |
7795 | 0 | NN_NO_CHECK(ccs)); |
7796 | 0 | } |
7797 | 0 | if (EllipsoidalCSPtr ecs = |
7798 | 0 | util::nn_dynamic_pointer_cast<EllipsoidalCS>(cs)) { |
7799 | 0 | return DerivedGeographicCRS::create( |
7800 | 0 | props, geogCRSNN, convNN, NN_NO_CHECK(ecs)); |
7801 | 0 | } |
7802 | 0 | } else if (dynamic_cast<GeodeticCRS *>(baseCRS.get()) && |
7803 | 0 | dynamic_cast<CartesianCS *>(cs.get())) { |
7804 | 0 | return DerivedGeodeticCRS::create( |
7805 | 0 | props, |
7806 | 0 | NN_NO_CHECK(util::nn_dynamic_pointer_cast<GeodeticCRS>( |
7807 | 0 | baseCRS)), |
7808 | 0 | convNN, |
7809 | 0 | NN_NO_CHECK( |
7810 | 0 | util::nn_dynamic_pointer_cast<CartesianCS>(cs))); |
7811 | 0 | } else if (auto pcrs = |
7812 | 0 | util::nn_dynamic_pointer_cast<ProjectedCRS>( |
7813 | 0 | baseCRS)) { |
7814 | 0 | return DerivedProjectedCRS::create(props, NN_NO_CHECK(pcrs), |
7815 | 0 | convNN, cs); |
7816 | 0 | } else if (auto vertBaseCRS = |
7817 | 0 | util::nn_dynamic_pointer_cast<VerticalCRS>( |
7818 | 0 | baseCRS)) { |
7819 | 0 | if (auto vertCS = |
7820 | 0 | util::nn_dynamic_pointer_cast<VerticalCS>(cs)) { |
7821 | 0 | const int methodCode = convNN->method()->getEPSGCode(); |
7822 | 0 | std::string newName(baseName); |
7823 | 0 | std::string unitNameSuffix; |
7824 | 0 | for (const char *suffix : {" (ft)", " (ftUS)"}) { |
7825 | 0 | if (ends_with(newName, suffix)) { |
7826 | 0 | unitNameSuffix = suffix; |
7827 | 0 | newName.resize(newName.size() - strlen(suffix)); |
7828 | 0 | break; |
7829 | 0 | } |
7830 | 0 | } |
7831 | 0 | bool newNameOk = false; |
7832 | 0 | if (methodCode == |
7833 | 0 | EPSG_CODE_METHOD_CHANGE_VERTICAL_UNIT_NO_CONV_FACTOR || |
7834 | 0 | methodCode == |
7835 | 0 | EPSG_CODE_METHOD_CHANGE_VERTICAL_UNIT) { |
7836 | 0 | const auto &unitName = |
7837 | 0 | vertCS->axisList()[0]->unit().name(); |
7838 | 0 | if (unitName == UnitOfMeasure::METRE.name()) { |
7839 | 0 | newNameOk = true; |
7840 | 0 | } else if (unitName == UnitOfMeasure::FOOT.name()) { |
7841 | 0 | newName += " (ft)"; |
7842 | 0 | newNameOk = true; |
7843 | 0 | } else if (unitName == |
7844 | 0 | UnitOfMeasure::US_FOOT.name()) { |
7845 | 0 | newName += " (ftUS)"; |
7846 | 0 | newNameOk = true; |
7847 | 0 | } |
7848 | 0 | } else if (methodCode == |
7849 | 0 | EPSG_CODE_METHOD_HEIGHT_DEPTH_REVERSAL) { |
7850 | 0 | if (ends_with(newName, " height")) { |
7851 | 0 | newName.resize(newName.size() - |
7852 | 0 | strlen(" height")); |
7853 | 0 | newName += " depth"; |
7854 | 0 | newName += unitNameSuffix; |
7855 | 0 | newNameOk = true; |
7856 | 0 | } else if (ends_with(newName, " depth")) { |
7857 | 0 | newName.resize(newName.size() - |
7858 | 0 | strlen(" depth")); |
7859 | 0 | newName += " height"; |
7860 | 0 | newName += unitNameSuffix; |
7861 | 0 | newNameOk = true; |
7862 | 0 | } |
7863 | 0 | } |
7864 | 0 | if (newNameOk) { |
7865 | 0 | props.set(IdentifiedObject::NAME_KEY, newName); |
7866 | 0 | } |
7867 | 0 | return DerivedVerticalCRS::create( |
7868 | 0 | props, NN_NO_CHECK(vertBaseCRS), convNN, |
7869 | 0 | NN_NO_CHECK(vertCS)); |
7870 | 0 | } |
7871 | 0 | } |
7872 | 0 | } |
7873 | | |
7874 | 0 | throw ParsingException("unsupported combination of baseCRS, CS " |
7875 | 0 | "and coordinateOperation for a " |
7876 | 0 | "DerivedCRS"); |
7877 | 0 | } |
7878 | | |
7879 | | // OGC 07-092r2: para 7.5.2 |
7880 | | // URN combined references for compound coordinate reference systems |
7881 | 9 | std::vector<CRSNNPtr> components; |
7882 | 9 | std::string name; |
7883 | 9 | for (size_t i = 1; i < tokensComma.size(); i++) { |
7884 | 9 | tokens = split(tokensComma[i], ':'); |
7885 | 9 | if (tokens.size() != 4) { |
7886 | 6 | throw ParsingException( |
7887 | 6 | concat("invalid crs component: ", tokensComma[i])); |
7888 | 6 | } |
7889 | 3 | const auto &type = tokens[0]; |
7890 | 3 | auto factory = |
7891 | 3 | AuthorityFactory::create(NN_NO_CHECK(dbContext), tokens[1]); |
7892 | 3 | const auto &code = tokens[3]; |
7893 | 3 | if (type == "crs") { |
7894 | 0 | auto crs(factory->createCoordinateReferenceSystem(code, false)); |
7895 | 0 | components.emplace_back(crs); |
7896 | 0 | if (!name.empty()) { |
7897 | 0 | name += " + "; |
7898 | 0 | } |
7899 | 0 | name += crs->nameStr(); |
7900 | 3 | } else { |
7901 | 3 | throw ParsingException( |
7902 | 3 | concat("unexpected object type: ", type)); |
7903 | 3 | } |
7904 | 3 | } |
7905 | 0 | return CompoundCRS::create( |
7906 | 0 | util::PropertyMap().set(IdentifiedObject::NAME_KEY, name), |
7907 | 0 | components); |
7908 | 9 | } |
7909 | | |
7910 | | // OGC 07-092r2: para 7.5.3 |
7911 | | // 7.5.3 URN combined references for concatenated operations |
7912 | 4.54k | if (starts_with(text, "urn:ogc:def:coordinateOperation,")) { |
7913 | 7 | if (!dbContext) { |
7914 | 2 | throw ParsingException("no database context specified"); |
7915 | 2 | } |
7916 | 5 | auto tokensComma = split(text, ','); |
7917 | 5 | std::vector<CoordinateOperationNNPtr> components; |
7918 | 5 | for (size_t i = 1; i < tokensComma.size(); i++) { |
7919 | 5 | tokens = split(tokensComma[i], ':'); |
7920 | 5 | if (tokens.size() != 4) { |
7921 | 4 | throw ParsingException(concat( |
7922 | 4 | "invalid coordinateOperation component: ", tokensComma[i])); |
7923 | 4 | } |
7924 | 1 | const auto &type = tokens[0]; |
7925 | 1 | auto factory = |
7926 | 1 | AuthorityFactory::create(NN_NO_CHECK(dbContext), tokens[1]); |
7927 | 1 | const auto &code = tokens[3]; |
7928 | 1 | if (type == "coordinateOperation") { |
7929 | 0 | auto op(factory->createCoordinateOperation(code, false)); |
7930 | 0 | components.emplace_back(op); |
7931 | 1 | } else { |
7932 | 1 | throw ParsingException( |
7933 | 1 | concat("unexpected object type: ", type)); |
7934 | 1 | } |
7935 | 1 | } |
7936 | 0 | return ConcatenatedOperation::createComputeMetadata(components, true); |
7937 | 5 | } |
7938 | | |
7939 | | // urn:ogc:def:crs:EPSG::4326 |
7940 | 4.54k | if (tokens.size() == 7 && tolower(tokens[0]) == "urn") { |
7941 | | |
7942 | 3 | const std::string type(tokens[3] == "CRS" ? "crs" : tokens[3]); |
7943 | 3 | const auto &authName = tokens[4]; |
7944 | 3 | const auto &version = tokens[5]; |
7945 | 3 | const auto &code = tokens[6]; |
7946 | 3 | return createFromURNPart(dbContext, type, authName, version, code); |
7947 | 3 | } |
7948 | | |
7949 | | // urn:ogc:def:crs:OGC::AUTO42001:-117:33 |
7950 | 4.53k | if (tokens.size() > 7 && tokens[0] == "urn" && tokens[4] == "OGC" && |
7951 | 4.53k | ci_starts_with(tokens[6], "AUTO")) { |
7952 | 28 | const auto textAUTO = text.substr(text.find(":AUTO") + 5); |
7953 | 28 | return importFromWMSAUTO("AUTO:" + replaceAll(textAUTO, ":", ",")); |
7954 | 28 | } |
7955 | | |
7956 | | // Legacy urn:opengis:crs:EPSG:0:4326 (note the missing def: compared to |
7957 | | // above) |
7958 | 4.50k | if (tokens.size() == 6 && tokens[0] == "urn" && tokens[2] != "def") { |
7959 | 9 | const auto &type = tokens[2]; |
7960 | 9 | const auto &authName = tokens[3]; |
7961 | 9 | const auto &version = tokens[4]; |
7962 | 9 | const auto &code = tokens[5]; |
7963 | 9 | return createFromURNPart(dbContext, type, authName, version, code); |
7964 | 9 | } |
7965 | | |
7966 | | // Legacy urn:x-ogc:def:crs:EPSG:4326 (note the missing version) |
7967 | 4.50k | if (tokens.size() == 6 && tokens[0] == "urn") { |
7968 | 12 | const auto &type = tokens[3]; |
7969 | 12 | const auto &authName = tokens[4]; |
7970 | 12 | const auto &code = tokens[5]; |
7971 | 12 | return createFromURNPart(dbContext, type, authName, std::string(), |
7972 | 12 | code); |
7973 | 12 | } |
7974 | | |
7975 | 4.48k | if (dbContext) { |
7976 | 3.03k | auto factory = |
7977 | 3.03k | AuthorityFactory::create(NN_NO_CHECK(dbContext), std::string()); |
7978 | | |
7979 | 3.03k | const auto searchObject = |
7980 | 3.03k | [&factory]( |
7981 | 3.03k | const std::string &objectName, bool approximateMatch, |
7982 | 3.03k | const std::vector<AuthorityFactory::ObjectType> &objectTypes) |
7983 | 8.06k | -> IdentifiedObjectPtr { |
7984 | 8.06k | constexpr size_t limitResultCount = 10; |
7985 | 8.06k | auto res = factory->createObjectsFromName( |
7986 | 8.06k | objectName, objectTypes, approximateMatch, limitResultCount); |
7987 | 8.06k | if (res.size() == 1) { |
7988 | 541 | return res.front().as_nullable(); |
7989 | 541 | } |
7990 | 7.52k | if (res.size() > 1) { |
7991 | 1.06k | if (objectTypes.size() == 1 && |
7992 | 1.06k | objectTypes[0] == AuthorityFactory::ObjectType::CRS) { |
7993 | 1.10k | for (size_t ndim = 2; ndim <= 3; ndim++) { |
7994 | 3.97k | for (const auto &obj : res) { |
7995 | 3.97k | auto crs = |
7996 | 3.97k | dynamic_cast<crs::GeographicCRS *>(obj.get()); |
7997 | 3.97k | if (crs && |
7998 | 3.97k | crs->coordinateSystem()->axisList().size() == |
7999 | 1.02k | ndim) { |
8000 | 616 | return obj.as_nullable(); |
8001 | 616 | } |
8002 | 3.97k | } |
8003 | 944 | } |
8004 | 776 | } |
8005 | | |
8006 | | // If there's exactly only one object whose name is equivalent |
8007 | | // to the user input, return it. |
8008 | 1.34k | for (int pass = 0; pass <= 1; ++pass) { |
8009 | 901 | IdentifiedObjectPtr identifiedObj; |
8010 | 6.97k | for (const auto &obj : res) { |
8011 | 6.97k | if (Identifier::isEquivalentName( |
8012 | 6.97k | obj->nameStr().c_str(), objectName.c_str(), |
8013 | 6.97k | /* biggerDifferencesAllowed = */ pass == 1)) { |
8014 | 5 | if (identifiedObj == nullptr) { |
8015 | 5 | identifiedObj = obj.as_nullable(); |
8016 | 5 | } else { |
8017 | 0 | identifiedObj = nullptr; |
8018 | 0 | break; |
8019 | 0 | } |
8020 | 5 | } |
8021 | 6.97k | } |
8022 | 901 | if (identifiedObj) { |
8023 | 5 | return identifiedObj; |
8024 | 5 | } |
8025 | 901 | } |
8026 | | |
8027 | 446 | std::string msg("several objects matching this name: "); |
8028 | 446 | bool first = true; |
8029 | 2.99k | for (const auto &obj : res) { |
8030 | 2.99k | if (msg.size() > 200) { |
8031 | 251 | msg += ", ..."; |
8032 | 251 | break; |
8033 | 251 | } |
8034 | 2.74k | if (!first) { |
8035 | 2.29k | msg += ", "; |
8036 | 2.29k | } |
8037 | 2.74k | first = false; |
8038 | 2.74k | msg += obj->nameStr(); |
8039 | 2.74k | } |
8040 | 446 | throw ParsingException(msg); |
8041 | 451 | } |
8042 | 6.46k | return nullptr; |
8043 | 7.52k | }; |
8044 | | |
8045 | 3.03k | const auto searchCRS = [&searchObject](const std::string &objectName) { |
8046 | 55 | const auto objectTypes = std::vector<AuthorityFactory::ObjectType>{ |
8047 | 55 | AuthorityFactory::ObjectType::CRS}; |
8048 | 55 | { |
8049 | 55 | constexpr bool approximateMatch = false; |
8050 | 55 | auto ret = |
8051 | 55 | searchObject(objectName, approximateMatch, objectTypes); |
8052 | 55 | if (ret) |
8053 | 0 | return ret; |
8054 | 55 | } |
8055 | | |
8056 | 55 | constexpr bool approximateMatch = true; |
8057 | 55 | return searchObject(objectName, approximateMatch, objectTypes); |
8058 | 55 | }; |
8059 | | |
8060 | | // strings like "WGS 84 + EGM96 height" |
8061 | 3.03k | CompoundCRSPtr compoundCRS; |
8062 | 3.03k | try { |
8063 | 3.03k | const auto tokensCompound = split(text, " + "); |
8064 | 3.03k | if (tokensCompound.size() == 2) { |
8065 | 28 | auto obj1 = searchCRS(tokensCompound[0]); |
8066 | 28 | auto obj2 = searchCRS(tokensCompound[1]); |
8067 | 28 | auto crs1 = std::dynamic_pointer_cast<CRS>(obj1); |
8068 | 28 | auto crs2 = std::dynamic_pointer_cast<CRS>(obj2); |
8069 | 28 | if (crs1 && crs2) { |
8070 | 1 | compoundCRS = |
8071 | 1 | CompoundCRS::create( |
8072 | 1 | util::PropertyMap().set(IdentifiedObject::NAME_KEY, |
8073 | 1 | crs1->nameStr() + " + " + |
8074 | 1 | crs2->nameStr()), |
8075 | 1 | {NN_NO_CHECK(crs1), NN_NO_CHECK(crs2)}) |
8076 | 1 | .as_nullable(); |
8077 | 1 | } |
8078 | 28 | } |
8079 | 3.03k | } catch (const std::exception &) { |
8080 | 5 | } |
8081 | | |
8082 | | // First pass: exact match on CRS objects |
8083 | | // Second pass: exact match on other objects |
8084 | | // Third pass: approximate match on CRS objects |
8085 | | // Fourth pass: approximate match on other objects |
8086 | | // But only allow approximate matching if the size of the text is |
8087 | | // large enough (>= 5), otherwise we get a lot of false positives: |
8088 | | // "foo" -> "Amersfoort", "bar" -> "Barbados 1938" |
8089 | | // Also only accept approximate matching if the ratio between the |
8090 | | // input and match size is not too small, so that "omerc" doesn't match |
8091 | | // with "WGS 84 / Pseudo-Mercator" |
8092 | 3.03k | const int maxNumberPasses = text.size() <= 4 ? 2 : 4; |
8093 | 10.1k | for (int pass = 0; pass < maxNumberPasses; ++pass) { |
8094 | 7.95k | const bool approximateMatch = (pass >= 2); |
8095 | 7.95k | auto ret = searchObject( |
8096 | 7.95k | text, approximateMatch, |
8097 | 7.95k | (pass == 0 || pass == 2) |
8098 | 7.95k | ? std::vector< |
8099 | 4.47k | AuthorityFactory::ObjectType>{AuthorityFactory:: |
8100 | 4.47k | ObjectType::CRS} |
8101 | 7.95k | : std::vector<AuthorityFactory::ObjectType>{ |
8102 | 3.48k | AuthorityFactory::ObjectType::ELLIPSOID, |
8103 | 3.48k | AuthorityFactory::ObjectType::DATUM, |
8104 | 3.48k | AuthorityFactory::ObjectType::DATUM_ENSEMBLE, |
8105 | 3.48k | AuthorityFactory::ObjectType::COORDINATE_OPERATION}); |
8106 | 7.95k | if (ret) { |
8107 | 1.15k | if (!approximateMatch || |
8108 | 1.15k | ret->nameStr().size() < 2 * text.size()) |
8109 | 877 | return NN_NO_CHECK(ret); |
8110 | 1.15k | } |
8111 | 7.08k | if (compoundCRS) { |
8112 | 0 | if (!approximateMatch || |
8113 | 0 | compoundCRS->nameStr().size() < 2 * text.size()) |
8114 | 0 | return NN_NO_CHECK(compoundCRS); |
8115 | 0 | } |
8116 | 7.08k | } |
8117 | 3.03k | } |
8118 | | |
8119 | 3.61k | throw ParsingException("unrecognized format / unknown name"); |
8120 | 4.48k | } |
8121 | | //! @endcond |
8122 | | |
8123 | | // --------------------------------------------------------------------------- |
8124 | | |
8125 | | /** \brief Instantiate a sub-class of BaseObject from a user specified text. |
8126 | | * |
8127 | | * The text can be a: |
8128 | | * <ul> |
8129 | | * <li>WKT string</li> |
8130 | | * <li>PROJ string</li> |
8131 | | * <li>database code, prefixed by its authority. e.g. "EPSG:4326"</li> |
8132 | | * <li>OGC URN. e.g. "urn:ogc:def:crs:EPSG::4326", |
8133 | | * "urn:ogc:def:coordinateOperation:EPSG::1671", |
8134 | | * "urn:ogc:def:ellipsoid:EPSG::7001" |
8135 | | * or "urn:ogc:def:datum:EPSG::6326"</li> |
8136 | | * <li> OGC URN combining references for compound coordinate reference systems |
8137 | | * e.g. "urn:ogc:def:crs,crs:EPSG::2393,crs:EPSG::5717" |
8138 | | * We also accept a custom abbreviated syntax EPSG:2393+5717 |
8139 | | * or ESRI:103668+EPSG:5703 |
8140 | | * </li> |
8141 | | * <li> OGC URN combining references for references for projected or derived |
8142 | | * CRSs |
8143 | | * e.g. for Projected 3D CRS "UTM zone 31N / WGS 84 (3D)" |
8144 | | * "urn:ogc:def:crs,crs:EPSG::4979,cs:PROJ::ENh,coordinateOperation:EPSG::16031" |
8145 | | * </li> |
8146 | | * <li>Extension of OGC URN for CoordinateMetadata. |
8147 | | * e.g. |
8148 | | * "urn:ogc:def:coordinateMetadata:NRCAN::NAD83_CSRS_1997_MTM11_HT2_1997"</li> |
8149 | | * <li> OGC URN combining references for concatenated operations |
8150 | | * e.g. |
8151 | | * "urn:ogc:def:coordinateOperation,coordinateOperation:EPSG::3895,coordinateOperation:EPSG::1618"</li> |
8152 | | * <li>OGC URL for a single CRS. e.g. |
8153 | | * "http://www.opengis.net/def/crs/EPSG/0/4326"</li> |
8154 | | * <li>OGC URL for a compound |
8155 | | * CRS. e.g |
8156 | | * "http://www.opengis.net/def/crs-compound?1=http://www.opengis.net/def/crs/EPSG/0/4326&2=http://www.opengis.net/def/crs/EPSG/0/3855"</li> |
8157 | | * <li>an Object name. e.g "WGS 84", "WGS 84 / UTM zone 31N". In that case as |
8158 | | * uniqueness is not guaranteed, the function may apply heuristics to |
8159 | | * determine the appropriate best match.</li> |
8160 | | * <li>a CRS name and a coordinate epoch, separated with '@'. For example |
8161 | | * "ITRF2014@2025.0". (added in PROJ 9.2)</li> |
8162 | | * <li>a compound CRS made from two object names separated with " + ". |
8163 | | * e.g. "WGS 84 + EGM96 height"</li> |
8164 | | * <li>PROJJSON string</li> |
8165 | | * </ul> |
8166 | | * |
8167 | | * @param text One of the above mentioned text format |
8168 | | * @param dbContext Database context, or nullptr (in which case database |
8169 | | * lookups will not work) |
8170 | | * @param usePROJ4InitRules When set to true, |
8171 | | * init=epsg:XXXX syntax will be allowed and will be interpreted according to |
8172 | | * PROJ.4 and PROJ.5 rules, that is geodeticCRS will have longitude, latitude |
8173 | | * order and will expect/output coordinates in radians. ProjectedCRS will have |
8174 | | * easting, northing axis order (except the ones with Transverse Mercator South |
8175 | | * Orientated projection). In that mode, the epsg:XXXX syntax will be also |
8176 | | * interpreted the same way. |
8177 | | * @throw ParsingException if the string cannot be parsed. |
8178 | | */ |
8179 | | BaseObjectNNPtr createFromUserInput(const std::string &text, |
8180 | | const DatabaseContextPtr &dbContext, |
8181 | 284 | bool usePROJ4InitRules) { |
8182 | 284 | return createFromUserInput(text, dbContext, usePROJ4InitRules, nullptr, |
8183 | 284 | /* ignoreCoordinateEpoch = */ false); |
8184 | 284 | } |
8185 | | |
8186 | | // --------------------------------------------------------------------------- |
8187 | | |
8188 | | /** \brief Instantiate a sub-class of BaseObject from a user specified text. |
8189 | | * |
8190 | | * The text can be a: |
8191 | | * <ul> |
8192 | | * <li>WKT string</li> |
8193 | | * <li>PROJ string</li> |
8194 | | * <li>database code, prefixed by its authority. e.g. "EPSG:4326"</li> |
8195 | | * <li>OGC URN. e.g. "urn:ogc:def:crs:EPSG::4326", |
8196 | | * "urn:ogc:def:coordinateOperation:EPSG::1671", |
8197 | | * "urn:ogc:def:ellipsoid:EPSG::7001" |
8198 | | * or "urn:ogc:def:datum:EPSG::6326"</li> |
8199 | | * <li> OGC URN combining references for compound coordinate reference systems |
8200 | | * e.g. "urn:ogc:def:crs,crs:EPSG::2393,crs:EPSG::5717" |
8201 | | * We also accept a custom abbreviated syntax EPSG:2393+5717 |
8202 | | * </li> |
8203 | | * <li> OGC URN combining references for references for projected or derived |
8204 | | * CRSs |
8205 | | * e.g. for Projected 3D CRS "UTM zone 31N / WGS 84 (3D)" |
8206 | | * "urn:ogc:def:crs,crs:EPSG::4979,cs:PROJ::ENh,coordinateOperation:EPSG::16031" |
8207 | | * </li> |
8208 | | * <li>Extension of OGC URN for CoordinateMetadata. |
8209 | | * e.g. |
8210 | | * "urn:ogc:def:coordinateMetadata:NRCAN::NAD83_CSRS_1997_MTM11_HT2_1997"</li> |
8211 | | * <li> OGC URN combining references for concatenated operations |
8212 | | * e.g. |
8213 | | * "urn:ogc:def:coordinateOperation,coordinateOperation:EPSG::3895,coordinateOperation:EPSG::1618"</li> |
8214 | | * <li>an Object name. e.g "WGS 84", "WGS 84 / UTM zone 31N". In that case as |
8215 | | * uniqueness is not guaranteed, the function may apply heuristics to |
8216 | | * determine the appropriate best match.</li> |
8217 | | * <li>a compound CRS made from two object names separated with " + ". |
8218 | | * e.g. "WGS 84 + EGM96 height"</li> |
8219 | | * <li>PROJJSON string</li> |
8220 | | * </ul> |
8221 | | * |
8222 | | * @param text One of the above mentioned text format |
8223 | | * @param ctx PROJ context |
8224 | | * @throw ParsingException if the string cannot be parsed. |
8225 | | */ |
8226 | 16.1k | BaseObjectNNPtr createFromUserInput(const std::string &text, PJ_CONTEXT *ctx) { |
8227 | 16.1k | DatabaseContextPtr dbContext; |
8228 | 16.1k | try { |
8229 | 16.1k | if (ctx != nullptr) { |
8230 | | // Only connect to proj.db if needed |
8231 | 16.1k | if (text.find("proj=") == std::string::npos || |
8232 | 16.1k | text.find("init=") != std::string::npos) { |
8233 | 6.35k | dbContext = |
8234 | 6.35k | ctx->get_cpp_context()->getDatabaseContext().as_nullable(); |
8235 | 6.35k | } |
8236 | 16.1k | } |
8237 | 16.1k | } catch (const std::exception &) { |
8238 | 0 | } |
8239 | 16.1k | return createFromUserInput(text, dbContext, false, ctx, |
8240 | 16.1k | /* ignoreCoordinateEpoch = */ false); |
8241 | 16.1k | } |
8242 | | |
8243 | | // --------------------------------------------------------------------------- |
8244 | | |
8245 | | /** \brief Instantiate a sub-class of BaseObject from a WKT string. |
8246 | | * |
8247 | | * By default, validation is strict (to the extent of the checks that are |
8248 | | * actually implemented. Currently only WKT1 strict grammar is checked), and |
8249 | | * any issue detected will cause an exception to be thrown, unless |
8250 | | * setStrict(false) is called priorly. |
8251 | | * |
8252 | | * In non-strict mode, non-fatal issues will be recovered and simply listed |
8253 | | * in warningList(). This does not prevent more severe errors to cause an |
8254 | | * exception to be thrown. |
8255 | | * |
8256 | | * @throw ParsingException if the string cannot be parsed. |
8257 | | */ |
8258 | 2.58k | BaseObjectNNPtr WKTParser::createFromWKT(const std::string &wkt) { |
8259 | | |
8260 | 2.58k | const auto dialect = guessDialect(wkt); |
8261 | 2.58k | d->maybeEsriStyle_ = (dialect == WKTGuessedDialect::WKT1_ESRI); |
8262 | 2.58k | if (d->maybeEsriStyle_) { |
8263 | 1.46k | if (wkt.find("PARAMETER[\"X_Scale\",") != std::string::npos) { |
8264 | 55 | d->esriStyle_ = true; |
8265 | 55 | d->maybeEsriStyle_ = false; |
8266 | 55 | } |
8267 | 1.46k | } |
8268 | | |
8269 | 2.58k | const auto build = [this, &wkt]() -> BaseObjectNNPtr { |
8270 | 2.58k | size_t indexEnd; |
8271 | 2.58k | WKTNodeNNPtr root = WKTNode::createFrom(wkt, 0, 0, indexEnd); |
8272 | 2.58k | const std::string &name(root->GP()->value()); |
8273 | 2.58k | if (ci_equal(name, WKTConstants::DATUM) || |
8274 | 2.58k | ci_equal(name, WKTConstants::GEODETICDATUM) || |
8275 | 2.58k | ci_equal(name, WKTConstants::TRF)) { |
8276 | | |
8277 | 92 | auto primeMeridian = PrimeMeridian::GREENWICH; |
8278 | 92 | if (indexEnd < wkt.size()) { |
8279 | 77 | indexEnd = skipSpace(wkt, indexEnd); |
8280 | 77 | if (indexEnd < wkt.size() && wkt[indexEnd] == ',') { |
8281 | 58 | ++indexEnd; |
8282 | 58 | indexEnd = skipSpace(wkt, indexEnd); |
8283 | 58 | if (indexEnd < wkt.size() && |
8284 | 58 | ci_starts_with(wkt.c_str() + indexEnd, |
8285 | 55 | WKTConstants::PRIMEM.c_str())) { |
8286 | 32 | primeMeridian = d->buildPrimeMeridian( |
8287 | 32 | WKTNode::createFrom(wkt, indexEnd, 0, indexEnd), |
8288 | 32 | UnitOfMeasure::DEGREE); |
8289 | 32 | } |
8290 | 58 | } |
8291 | 77 | } |
8292 | 92 | return d->buildGeodeticReferenceFrame(root, primeMeridian, |
8293 | 92 | null_node); |
8294 | 2.49k | } else if (ci_equal(name, WKTConstants::GEOGCS) || |
8295 | 2.49k | ci_equal(name, WKTConstants::PROJCS)) { |
8296 | | // Parse implicit compoundCRS from ESRI that is |
8297 | | // "PROJCS[...],VERTCS[...]" or "GEOGCS[...],VERTCS[...]" |
8298 | 996 | if (indexEnd < wkt.size()) { |
8299 | 699 | indexEnd = skipSpace(wkt, indexEnd); |
8300 | 699 | if (indexEnd < wkt.size() && wkt[indexEnd] == ',') { |
8301 | 245 | ++indexEnd; |
8302 | 245 | indexEnd = skipSpace(wkt, indexEnd); |
8303 | 245 | if (indexEnd < wkt.size() && |
8304 | 245 | ci_starts_with(wkt.c_str() + indexEnd, |
8305 | 242 | WKTConstants::VERTCS.c_str())) { |
8306 | 183 | auto horizCRS = d->buildCRS(root); |
8307 | 183 | if (horizCRS) { |
8308 | 159 | auto vertCRS = |
8309 | 159 | d->buildVerticalCRS(WKTNode::createFrom( |
8310 | 159 | wkt, indexEnd, 0, indexEnd)); |
8311 | 159 | return CompoundCRS::createLax( |
8312 | 159 | util::PropertyMap().set( |
8313 | 159 | IdentifiedObject::NAME_KEY, |
8314 | 159 | horizCRS->nameStr() + " + " + |
8315 | 159 | vertCRS->nameStr()), |
8316 | 159 | {NN_NO_CHECK(horizCRS), vertCRS}, |
8317 | 159 | d->dbContext_); |
8318 | 159 | } |
8319 | 183 | } |
8320 | 245 | } |
8321 | 699 | } |
8322 | 996 | } |
8323 | 2.33k | return d->build(root); |
8324 | 2.58k | }; |
8325 | | |
8326 | 2.58k | auto obj = build(); |
8327 | | |
8328 | 2.58k | if (dialect == WKTGuessedDialect::WKT1_GDAL || |
8329 | 2.58k | dialect == WKTGuessedDialect::WKT1_ESRI) { |
8330 | 753 | auto errorMsg = pj_wkt1_parse(wkt); |
8331 | 753 | if (!errorMsg.empty()) { |
8332 | 460 | d->emitGrammarError(errorMsg); |
8333 | 460 | } |
8334 | 1.83k | } else if (dialect == WKTGuessedDialect::WKT2_2015 || |
8335 | 1.83k | dialect == WKTGuessedDialect::WKT2_2019) { |
8336 | 464 | auto errorMsg = pj_wkt2_parse(wkt); |
8337 | 464 | if (!errorMsg.empty()) { |
8338 | 415 | d->emitGrammarError(errorMsg); |
8339 | 415 | } |
8340 | 464 | } |
8341 | | |
8342 | 2.58k | return obj; |
8343 | 2.58k | } |
8344 | | |
8345 | | // --------------------------------------------------------------------------- |
8346 | | |
8347 | | /** \brief Attach a database context, to allow queries in it if needed. |
8348 | | */ |
8349 | | WKTParser & |
8350 | 2.58k | WKTParser::attachDatabaseContext(const DatabaseContextPtr &dbContext) { |
8351 | 2.58k | d->dbContext_ = dbContext; |
8352 | 2.58k | return *this; |
8353 | 2.58k | } |
8354 | | |
8355 | | // --------------------------------------------------------------------------- |
8356 | | |
8357 | | /** \brief Guess the "dialect" of the WKT string. |
8358 | | */ |
8359 | | WKTParser::WKTGuessedDialect |
8360 | 2.58k | WKTParser::guessDialect(const std::string &inputWkt) noexcept { |
8361 | | |
8362 | | // cppcheck complains (rightly) that the method could be static |
8363 | 2.58k | (void)this; |
8364 | | |
8365 | 2.58k | std::string wkt = inputWkt; |
8366 | 2.58k | std::size_t idxFirstCharNotSpace = wkt.find_first_not_of(" \t\r\n"); |
8367 | 2.58k | if (idxFirstCharNotSpace > 0 && idxFirstCharNotSpace != std::string::npos) { |
8368 | 0 | wkt = wkt.substr(idxFirstCharNotSpace); |
8369 | 0 | } |
8370 | 2.58k | if (ci_starts_with(wkt, WKTConstants::VERTCS)) { |
8371 | 182 | return WKTGuessedDialect::WKT1_ESRI; |
8372 | 182 | } |
8373 | 2.40k | const std::string *const wkt1_keywords[] = { |
8374 | 2.40k | &WKTConstants::GEOCCS, &WKTConstants::GEOGCS, &WKTConstants::COMPD_CS, |
8375 | 2.40k | &WKTConstants::PROJCS, &WKTConstants::VERT_CS, &WKTConstants::LOCAL_CS}; |
8376 | 10.2k | for (const auto &pointerKeyword : wkt1_keywords) { |
8377 | 10.2k | if (ci_starts_with(wkt, *pointerKeyword)) { |
8378 | | |
8379 | 1.56k | if ((ci_find(wkt, "GEOGCS[\"GCS_") != std::string::npos || |
8380 | 1.56k | (!ci_starts_with(wkt, WKTConstants::LOCAL_CS) && |
8381 | 1.27k | ci_find(wkt, "AXIS[") == std::string::npos && |
8382 | 1.27k | ci_find(wkt, "AUTHORITY[") == std::string::npos)) && |
8383 | | // WKT1:GDAL and WKT1:ESRI have both a |
8384 | | // Hotine_Oblique_Mercator_Azimuth_Center If providing a |
8385 | | // WKT1:GDAL without AXIS, we may wrongly detect it as WKT1:ESRI |
8386 | | // and skip the rectified_grid_angle parameter cf |
8387 | | // https://github.com/OSGeo/PROJ/issues/3279 |
8388 | 1.56k | ci_find(wkt, "PARAMETER[\"rectified_grid_angle") == |
8389 | 1.28k | std::string::npos) { |
8390 | 1.28k | return WKTGuessedDialect::WKT1_ESRI; |
8391 | 1.28k | } |
8392 | | |
8393 | 278 | return WKTGuessedDialect::WKT1_GDAL; |
8394 | 1.56k | } |
8395 | 10.2k | } |
8396 | | |
8397 | 842 | const std::string *const wkt2_2019_only_keywords[] = { |
8398 | 842 | &WKTConstants::GEOGCRS, |
8399 | | // contained in previous one |
8400 | | // &WKTConstants::BASEGEOGCRS, |
8401 | 842 | &WKTConstants::CONCATENATEDOPERATION, &WKTConstants::USAGE, |
8402 | 842 | &WKTConstants::DYNAMIC, &WKTConstants::FRAMEEPOCH, &WKTConstants::MODEL, |
8403 | 842 | &WKTConstants::VELOCITYGRID, &WKTConstants::ENSEMBLE, |
8404 | 842 | &WKTConstants::DERIVEDPROJCRS, &WKTConstants::BASEPROJCRS, |
8405 | 842 | &WKTConstants::GEOGRAPHICCRS, &WKTConstants::TRF, &WKTConstants::VRF, |
8406 | 842 | &WKTConstants::POINTMOTIONOPERATION}; |
8407 | | |
8408 | 11.4k | for (const auto &pointerKeyword : wkt2_2019_only_keywords) { |
8409 | 11.4k | auto pos = ci_find(wkt, *pointerKeyword); |
8410 | 11.4k | if (pos != std::string::npos && |
8411 | 11.4k | wkt[pos + pointerKeyword->size()] == '[') { |
8412 | 194 | return WKTGuessedDialect::WKT2_2019; |
8413 | 194 | } |
8414 | 11.4k | } |
8415 | 648 | static const char *const wkt2_2019_only_substrings[] = { |
8416 | 648 | "CS[TemporalDateTime,", |
8417 | 648 | "CS[TemporalCount,", |
8418 | 648 | "CS[TemporalMeasure,", |
8419 | 648 | }; |
8420 | 1.92k | for (const auto &substrings : wkt2_2019_only_substrings) { |
8421 | 1.92k | if (ci_find(wkt, substrings) != std::string::npos) { |
8422 | 16 | return WKTGuessedDialect::WKT2_2019; |
8423 | 16 | } |
8424 | 1.92k | } |
8425 | | |
8426 | 23.0k | for (const auto &wktConstant : WKTConstants::constants()) { |
8427 | 23.0k | if (ci_starts_with(wkt, wktConstant)) { |
8428 | 636 | for (auto wktPtr = wkt.c_str() + wktConstant.size(); |
8429 | 826 | *wktPtr != '\0'; ++wktPtr) { |
8430 | 826 | if (isspace(static_cast<unsigned char>(*wktPtr))) |
8431 | 190 | continue; |
8432 | 636 | if (*wktPtr == '[') { |
8433 | 632 | return WKTGuessedDialect::WKT2_2015; |
8434 | 632 | } |
8435 | 4 | break; |
8436 | 636 | } |
8437 | 636 | } |
8438 | 23.0k | } |
8439 | | |
8440 | 0 | return WKTGuessedDialect::NOT_WKT; |
8441 | 632 | } |
8442 | | |
8443 | | // --------------------------------------------------------------------------- |
8444 | | |
8445 | | //! @cond Doxygen_Suppress |
8446 | | FormattingException::FormattingException(const char *message) |
8447 | 224 | : Exception(message) {} |
8448 | | |
8449 | | // --------------------------------------------------------------------------- |
8450 | | |
8451 | | FormattingException::FormattingException(const std::string &message) |
8452 | 315 | : Exception(message) {} |
8453 | | |
8454 | | // --------------------------------------------------------------------------- |
8455 | | |
8456 | 0 | FormattingException::FormattingException(const FormattingException &) = default; |
8457 | | |
8458 | | // --------------------------------------------------------------------------- |
8459 | | |
8460 | 539 | FormattingException::~FormattingException() = default; |
8461 | | |
8462 | | // --------------------------------------------------------------------------- |
8463 | | |
8464 | 3 | void FormattingException::Throw(const char *msg) { |
8465 | 3 | throw FormattingException(msg); |
8466 | 3 | } |
8467 | | |
8468 | | // --------------------------------------------------------------------------- |
8469 | | |
8470 | 0 | void FormattingException::Throw(const std::string &msg) { |
8471 | 0 | throw FormattingException(msg); |
8472 | 0 | } |
8473 | | |
8474 | | // --------------------------------------------------------------------------- |
8475 | | |
8476 | 4.86k | ParsingException::ParsingException(const char *message) : Exception(message) {} |
8477 | | |
8478 | | // --------------------------------------------------------------------------- |
8479 | | |
8480 | | ParsingException::ParsingException(const std::string &message) |
8481 | 3.31k | : Exception(message) {} |
8482 | | |
8483 | | // --------------------------------------------------------------------------- |
8484 | | |
8485 | 0 | ParsingException::ParsingException(const ParsingException &) = default; |
8486 | | |
8487 | | // --------------------------------------------------------------------------- |
8488 | | |
8489 | 8.17k | ParsingException::~ParsingException() = default; |
8490 | | |
8491 | | // --------------------------------------------------------------------------- |
8492 | | |
8493 | 1.19M | IPROJStringExportable::~IPROJStringExportable() = default; |
8494 | | |
8495 | | // --------------------------------------------------------------------------- |
8496 | | |
8497 | | std::string IPROJStringExportable::exportToPROJString( |
8498 | 110k | PROJStringFormatter *formatter) const { |
8499 | 110k | const bool bIsCRS = dynamic_cast<const crs::CRS *>(this) != nullptr; |
8500 | 110k | if (bIsCRS) { |
8501 | 0 | formatter->setCRSExport(true); |
8502 | 0 | } |
8503 | 110k | _exportToPROJString(formatter); |
8504 | 110k | if (formatter->getAddNoDefs() && bIsCRS) { |
8505 | 0 | if (!formatter->hasParam("no_defs")) { |
8506 | 0 | formatter->addParam("no_defs"); |
8507 | 0 | } |
8508 | 0 | } |
8509 | 110k | if (bIsCRS) { |
8510 | 0 | if (!formatter->hasParam("type")) { |
8511 | 0 | formatter->addParam("type", "crs"); |
8512 | 0 | } |
8513 | 0 | formatter->setCRSExport(false); |
8514 | 0 | } |
8515 | 110k | return formatter->toString(); |
8516 | 110k | } |
8517 | | //! @endcond |
8518 | | |
8519 | | // --------------------------------------------------------------------------- |
8520 | | |
8521 | | //! @cond Doxygen_Suppress |
8522 | | |
8523 | | struct Step { |
8524 | | std::string name{}; |
8525 | | bool isInit = false; |
8526 | | bool inverted{false}; |
8527 | | |
8528 | | struct KeyValue { |
8529 | | std::string key{}; |
8530 | | std::string value{}; |
8531 | | bool usedByParser = false; // only for PROJStringParser used |
8532 | | |
8533 | 2.30M | explicit KeyValue(const std::string &keyIn) : key(keyIn) {} |
8534 | | |
8535 | | KeyValue(const char *keyIn, const std::string &valueIn); |
8536 | | |
8537 | | KeyValue(const std::string &keyIn, const std::string &valueIn) |
8538 | 7.45M | : key(keyIn), value(valueIn) {} |
8539 | | |
8540 | | // cppcheck-suppress functionStatic |
8541 | 6.25M | bool keyEquals(const char *otherKey) const noexcept { |
8542 | 6.25M | return key == otherKey; |
8543 | 6.25M | } |
8544 | | |
8545 | | // cppcheck-suppress functionStatic |
8546 | 1.97M | bool equals(const char *otherKey, const char *otherVal) const noexcept { |
8547 | 1.97M | return key == otherKey && value == otherVal; |
8548 | 1.97M | } |
8549 | | |
8550 | 1.03k | bool operator==(const KeyValue &other) const noexcept { |
8551 | 1.03k | return key == other.key && value == other.value; |
8552 | 1.03k | } |
8553 | | |
8554 | 125k | bool operator!=(const KeyValue &other) const noexcept { |
8555 | 125k | return key != other.key || value != other.value; |
8556 | 125k | } |
8557 | | }; |
8558 | | |
8559 | | std::vector<KeyValue> paramValues{}; |
8560 | | |
8561 | 51.1k | bool hasKey(const char *keyName) const { |
8562 | 582k | for (const auto &kv : paramValues) { |
8563 | 582k | if (kv.key == keyName) { |
8564 | 23 | return true; |
8565 | 23 | } |
8566 | 582k | } |
8567 | 51.1k | return false; |
8568 | 51.1k | } |
8569 | | }; |
8570 | | |
8571 | | Step::KeyValue::KeyValue(const char *keyIn, const std::string &valueIn) |
8572 | 94.5k | : key(keyIn), value(valueIn) {} |
8573 | | |
8574 | | struct PROJStringFormatter::Private { |
8575 | | PROJStringFormatter::Convention convention_ = |
8576 | | PROJStringFormatter::Convention::PROJ_5; |
8577 | | std::vector<double> toWGS84Parameters_{}; |
8578 | | std::string vDatumExtension_{}; |
8579 | | std::string geoidCRSValue_{}; |
8580 | | std::string hDatumExtension_{}; |
8581 | | crs::GeographicCRSPtr geogCRSOfCompoundCRS_{}; |
8582 | | |
8583 | | std::list<Step> steps_{}; |
8584 | | std::vector<Step::KeyValue> globalParamValues_{}; |
8585 | | |
8586 | | struct InversionStackElt { |
8587 | | std::list<Step>::iterator startIter{}; |
8588 | | bool iterValid = false; |
8589 | | bool currentInversionState = false; |
8590 | | }; |
8591 | | std::vector<InversionStackElt> inversionStack_{InversionStackElt()}; |
8592 | | bool omitProjLongLatIfPossible_ = false; |
8593 | | std::vector<bool> omitZUnitConversion_{false}; |
8594 | | std::vector<bool> omitHorizontalConversionInVertTransformation_{false}; |
8595 | | DatabaseContextPtr dbContext_{}; |
8596 | | bool useApproxTMerc_ = false; |
8597 | | bool addNoDefs_ = true; |
8598 | | bool coordOperationOptimizations_ = false; |
8599 | | bool crsExport_ = false; |
8600 | | bool legacyCRSToCRSContext_ = false; |
8601 | | bool multiLine_ = false; |
8602 | | bool normalizeOutput_ = false; |
8603 | | int indentWidth_ = 2; |
8604 | | int indentLevel_ = 0; |
8605 | | int maxLineLength_ = 80; |
8606 | | |
8607 | | std::string result_{}; |
8608 | | |
8609 | | // cppcheck-suppress functionStatic |
8610 | | void appendToResult(const char *str); |
8611 | | |
8612 | | // cppcheck-suppress functionStatic |
8613 | | void addStep(); |
8614 | | }; |
8615 | | |
8616 | | //! @endcond |
8617 | | |
8618 | | // --------------------------------------------------------------------------- |
8619 | | |
8620 | | //! @cond Doxygen_Suppress |
8621 | | PROJStringFormatter::PROJStringFormatter(Convention conventionIn, |
8622 | | const DatabaseContextPtr &dbContext) |
8623 | 347k | : d(std::make_unique<Private>()) { |
8624 | 347k | d->convention_ = conventionIn; |
8625 | 347k | d->dbContext_ = dbContext; |
8626 | 347k | } |
8627 | | //! @endcond |
8628 | | |
8629 | | // --------------------------------------------------------------------------- |
8630 | | |
8631 | | //! @cond Doxygen_Suppress |
8632 | 347k | PROJStringFormatter::~PROJStringFormatter() = default; |
8633 | | //! @endcond |
8634 | | |
8635 | | // --------------------------------------------------------------------------- |
8636 | | |
8637 | | /** \brief Constructs a new formatter. |
8638 | | * |
8639 | | * A formatter can be used only once (its internal state is mutated) |
8640 | | * |
8641 | | * Its default behavior can be adjusted with the different setters. |
8642 | | * |
8643 | | * @param conventionIn PROJ string flavor. Defaults to Convention::PROJ_5 |
8644 | | * @param dbContext Database context (can help to find alternative grid names). |
8645 | | * May be nullptr |
8646 | | * @return new formatter. |
8647 | | */ |
8648 | | PROJStringFormatterNNPtr |
8649 | | PROJStringFormatter::create(Convention conventionIn, |
8650 | 347k | DatabaseContextPtr dbContext) { |
8651 | 347k | return NN_NO_CHECK(PROJStringFormatter::make_unique<PROJStringFormatter>( |
8652 | 347k | conventionIn, dbContext)); |
8653 | 347k | } |
8654 | | |
8655 | | // --------------------------------------------------------------------------- |
8656 | | |
8657 | | /** \brief Set whether approximate Transverse Mercator or UTM should be used */ |
8658 | 0 | void PROJStringFormatter::setUseApproxTMerc(bool flag) { |
8659 | 0 | d->useApproxTMerc_ = flag; |
8660 | 0 | } |
8661 | | |
8662 | | // --------------------------------------------------------------------------- |
8663 | | |
8664 | | /** \brief Whether to use multi line output or not. */ |
8665 | | PROJStringFormatter & |
8666 | 0 | PROJStringFormatter::setMultiLine(bool multiLine) noexcept { |
8667 | 0 | d->multiLine_ = multiLine; |
8668 | 0 | return *this; |
8669 | 0 | } |
8670 | | |
8671 | | // --------------------------------------------------------------------------- |
8672 | | |
8673 | | /** \brief Set number of spaces for each indentation level (defaults to 2). |
8674 | | */ |
8675 | | PROJStringFormatter & |
8676 | 0 | PROJStringFormatter::setIndentationWidth(int width) noexcept { |
8677 | 0 | d->indentWidth_ = width; |
8678 | 0 | return *this; |
8679 | 0 | } |
8680 | | |
8681 | | // --------------------------------------------------------------------------- |
8682 | | |
8683 | | /** \brief Set the maximum size of a line (when multiline output is enable). |
8684 | | * Can be set to 0 for unlimited length. |
8685 | | */ |
8686 | | PROJStringFormatter & |
8687 | 0 | PROJStringFormatter::setMaxLineLength(int maxLineLength) noexcept { |
8688 | 0 | d->maxLineLength_ = maxLineLength; |
8689 | 0 | return *this; |
8690 | 0 | } |
8691 | | |
8692 | | // --------------------------------------------------------------------------- |
8693 | | |
8694 | | /** \brief Returns the PROJ string. */ |
8695 | 236k | const std::string &PROJStringFormatter::toString() const { |
8696 | | |
8697 | 236k | assert(d->inversionStack_.size() == 1); |
8698 | | |
8699 | 236k | d->result_.clear(); |
8700 | | |
8701 | 236k | auto &steps = d->steps_; |
8702 | | |
8703 | 236k | if (d->normalizeOutput_) { |
8704 | | // Sort +key=value options of each step in lexicographic order. |
8705 | 12.4k | for (auto &step : steps) { |
8706 | 11.4k | std::sort(step.paramValues.begin(), step.paramValues.end(), |
8707 | 4.36M | [](const Step::KeyValue &a, const Step::KeyValue &b) { |
8708 | 4.36M | return a.key < b.key; |
8709 | 4.36M | }); |
8710 | 11.4k | } |
8711 | 12.4k | } |
8712 | | |
8713 | 3.72M | for (auto iter = steps.begin(); iter != steps.end();) { |
8714 | | // Remove no-op helmert |
8715 | 3.48M | auto &step = *iter; |
8716 | 3.48M | const auto paramCount = step.paramValues.size(); |
8717 | 3.48M | if (step.name == "helmert" && (paramCount == 3 || paramCount == 8) && |
8718 | 3.48M | step.paramValues[0].equals("x", "0") && |
8719 | 3.48M | step.paramValues[1].equals("y", "0") && |
8720 | 3.48M | step.paramValues[2].equals("z", "0") && |
8721 | 3.48M | (paramCount == 3 || |
8722 | 11.6k | (step.paramValues[3].equals("rx", "0") && |
8723 | 2.74k | step.paramValues[4].equals("ry", "0") && |
8724 | 2.74k | step.paramValues[5].equals("rz", "0") && |
8725 | 2.74k | step.paramValues[6].equals("s", "0") && |
8726 | 11.1k | step.paramValues[7].keyEquals("convention")))) { |
8727 | 11.1k | iter = steps.erase(iter); |
8728 | 3.47M | } else if (d->coordOperationOptimizations_ && |
8729 | 3.47M | step.name == "unitconvert" && paramCount == 2 && |
8730 | 3.47M | step.paramValues[0].keyEquals("xy_in") && |
8731 | 3.47M | step.paramValues[1].keyEquals("xy_out") && |
8732 | 3.47M | step.paramValues[0].value == step.paramValues[1].value) { |
8733 | 30 | iter = steps.erase(iter); |
8734 | 3.47M | } else if (step.name == "push" && step.inverted) { |
8735 | 33.6k | step.name = "pop"; |
8736 | 33.6k | step.inverted = false; |
8737 | 33.6k | ++iter; |
8738 | 3.44M | } else if (step.name == "pop" && step.inverted) { |
8739 | 38.7k | step.name = "push"; |
8740 | 38.7k | step.inverted = false; |
8741 | 38.7k | ++iter; |
8742 | 3.40M | } else if (step.name == "noop" && steps.size() > 1) { |
8743 | 1.40k | iter = steps.erase(iter); |
8744 | 3.39M | } else { |
8745 | 3.39M | ++iter; |
8746 | 3.39M | } |
8747 | 3.48M | } |
8748 | | |
8749 | 3.47M | for (auto &step : steps) { |
8750 | 3.47M | if (!step.inverted) { |
8751 | 1.93M | continue; |
8752 | 1.93M | } |
8753 | | |
8754 | 1.53M | const auto paramCount = step.paramValues.size(); |
8755 | | |
8756 | | // axisswap order=2,1 (or 1,-2) is its own inverse |
8757 | 1.53M | if (step.name == "axisswap" && paramCount == 1 && |
8758 | 1.53M | (step.paramValues[0].equals("order", "2,1") || |
8759 | 341k | step.paramValues[0].equals("order", "1,-2"))) { |
8760 | 339k | step.inverted = false; |
8761 | 339k | continue; |
8762 | 339k | } |
8763 | | |
8764 | | // axisswap inv order=2,-1 ==> axisswap order -2,1 |
8765 | 1.19M | if (step.name == "axisswap" && paramCount == 1 && |
8766 | 1.19M | step.paramValues[0].equals("order", "2,-1")) { |
8767 | 277 | step.inverted = false; |
8768 | 277 | step.paramValues[0] = Step::KeyValue("order", "-2,1"); |
8769 | 277 | continue; |
8770 | 277 | } |
8771 | | |
8772 | | // axisswap order=1,2,-3 is its own inverse |
8773 | 1.19M | if (step.name == "axisswap" && paramCount == 1 && |
8774 | 1.19M | step.paramValues[0].equals("order", "1,2,-3")) { |
8775 | 0 | step.inverted = false; |
8776 | 0 | continue; |
8777 | 0 | } |
8778 | | |
8779 | | // handle unitconvert inverse |
8780 | 1.19M | if (step.name == "unitconvert" && paramCount == 2 && |
8781 | 1.19M | step.paramValues[0].keyEquals("xy_in") && |
8782 | 1.19M | step.paramValues[1].keyEquals("xy_out")) { |
8783 | 472k | std::swap(step.paramValues[0].value, step.paramValues[1].value); |
8784 | 472k | step.inverted = false; |
8785 | 472k | continue; |
8786 | 472k | } |
8787 | | |
8788 | 724k | if (step.name == "unitconvert" && paramCount == 2 && |
8789 | 724k | step.paramValues[0].keyEquals("z_in") && |
8790 | 724k | step.paramValues[1].keyEquals("z_out")) { |
8791 | 6.66k | std::swap(step.paramValues[0].value, step.paramValues[1].value); |
8792 | 6.66k | step.inverted = false; |
8793 | 6.66k | continue; |
8794 | 6.66k | } |
8795 | | |
8796 | 717k | if (step.name == "unitconvert" && paramCount == 4 && |
8797 | 717k | step.paramValues[0].keyEquals("xy_in") && |
8798 | 717k | step.paramValues[1].keyEquals("z_in") && |
8799 | 717k | step.paramValues[2].keyEquals("xy_out") && |
8800 | 717k | step.paramValues[3].keyEquals("z_out")) { |
8801 | 36.9k | std::swap(step.paramValues[0].value, step.paramValues[2].value); |
8802 | 36.9k | std::swap(step.paramValues[1].value, step.paramValues[3].value); |
8803 | 36.9k | step.inverted = false; |
8804 | 36.9k | continue; |
8805 | 36.9k | } |
8806 | 717k | } |
8807 | | |
8808 | 236k | { |
8809 | 236k | auto iterCur = steps.begin(); |
8810 | 236k | if (iterCur != steps.end()) { |
8811 | 229k | ++iterCur; |
8812 | 229k | } |
8813 | 4.08M | while (iterCur != steps.end()) { |
8814 | | |
8815 | 3.84M | assert(iterCur != steps.begin()); |
8816 | 3.84M | auto iterPrev = std::prev(iterCur); |
8817 | 3.84M | auto &prevStep = *iterPrev; |
8818 | 3.84M | auto &curStep = *iterCur; |
8819 | | |
8820 | 3.84M | const auto curStepParamCount = curStep.paramValues.size(); |
8821 | 3.84M | const auto prevStepParamCount = prevStep.paramValues.size(); |
8822 | | |
8823 | 3.84M | const auto deletePrevAndCurIter = [&steps, &iterPrev, &iterCur]() { |
8824 | 813k | iterCur = steps.erase(iterPrev, std::next(iterCur)); |
8825 | 813k | if (iterCur != steps.begin()) |
8826 | 728k | iterCur = std::prev(iterCur); |
8827 | 813k | if (iterCur == steps.begin() && iterCur != steps.end()) |
8828 | 155k | ++iterCur; |
8829 | 813k | }; |
8830 | | |
8831 | | // longlat (or its inverse) with ellipsoid only is a no-op |
8832 | | // do that only for an internal step |
8833 | 3.84M | if (std::next(iterCur) != steps.end() && |
8834 | 3.84M | curStep.name == "longlat" && curStepParamCount == 1 && |
8835 | 3.84M | curStep.paramValues[0].keyEquals("ellps")) { |
8836 | 378k | iterCur = steps.erase(iterCur); |
8837 | 378k | continue; |
8838 | 378k | } |
8839 | | |
8840 | | // push v_x followed by pop v_x is a no-op. |
8841 | 3.47M | if (curStep.name == "pop" && prevStep.name == "push" && |
8842 | 3.47M | !curStep.inverted && !prevStep.inverted && |
8843 | 3.47M | curStepParamCount == 1 && prevStepParamCount == 1 && |
8844 | 3.47M | curStep.paramValues[0].key == prevStep.paramValues[0].key) { |
8845 | 6.64k | deletePrevAndCurIter(); |
8846 | 6.64k | continue; |
8847 | 6.64k | } |
8848 | | |
8849 | | // pop v_x followed by push v_x is, almost, a no-op. For our |
8850 | | // purposes, |
8851 | | // we consider it as a no-op for better pipeline optimizations. |
8852 | 3.46M | if (curStep.name == "push" && prevStep.name == "pop" && |
8853 | 3.46M | !curStep.inverted && !prevStep.inverted && |
8854 | 3.46M | curStepParamCount == 1 && prevStepParamCount == 1 && |
8855 | 3.46M | curStep.paramValues[0].key == prevStep.paramValues[0].key) { |
8856 | 6.43k | deletePrevAndCurIter(); |
8857 | 6.43k | continue; |
8858 | 6.43k | } |
8859 | | |
8860 | | // unitconvert (xy) followed by its inverse is a no-op |
8861 | 3.45M | if (curStep.name == "unitconvert" && |
8862 | 3.45M | prevStep.name == "unitconvert" && !curStep.inverted && |
8863 | 3.45M | !prevStep.inverted && curStepParamCount == 2 && |
8864 | 3.45M | prevStepParamCount == 2 && |
8865 | 3.45M | curStep.paramValues[0].keyEquals("xy_in") && |
8866 | 3.45M | prevStep.paramValues[0].keyEquals("xy_in") && |
8867 | 3.45M | curStep.paramValues[1].keyEquals("xy_out") && |
8868 | 3.45M | prevStep.paramValues[1].keyEquals("xy_out") && |
8869 | 3.45M | curStep.paramValues[0].value == prevStep.paramValues[1].value && |
8870 | 3.45M | curStep.paramValues[1].value == prevStep.paramValues[0].value) { |
8871 | 334k | deletePrevAndCurIter(); |
8872 | 334k | continue; |
8873 | 334k | } |
8874 | | |
8875 | | // unitconvert (z) followed by its inverse is a no-op |
8876 | 3.12M | if (curStep.name == "unitconvert" && |
8877 | 3.12M | prevStep.name == "unitconvert" && !curStep.inverted && |
8878 | 3.12M | !prevStep.inverted && curStepParamCount == 2 && |
8879 | 3.12M | prevStepParamCount == 2 && |
8880 | 3.12M | curStep.paramValues[0].keyEquals("z_in") && |
8881 | 3.12M | prevStep.paramValues[0].keyEquals("z_in") && |
8882 | 3.12M | curStep.paramValues[1].keyEquals("z_out") && |
8883 | 3.12M | prevStep.paramValues[1].keyEquals("z_out") && |
8884 | 3.12M | curStep.paramValues[0].value == prevStep.paramValues[1].value && |
8885 | 3.12M | curStep.paramValues[1].value == prevStep.paramValues[0].value) { |
8886 | 828 | deletePrevAndCurIter(); |
8887 | 828 | continue; |
8888 | 828 | } |
8889 | | |
8890 | | // unitconvert (xyz) followed by its inverse is a no-op |
8891 | 3.12M | if (curStep.name == "unitconvert" && |
8892 | 3.12M | prevStep.name == "unitconvert" && !curStep.inverted && |
8893 | 3.12M | !prevStep.inverted && curStepParamCount == 4 && |
8894 | 3.12M | prevStepParamCount == 4 && |
8895 | 3.12M | curStep.paramValues[0].keyEquals("xy_in") && |
8896 | 3.12M | prevStep.paramValues[0].keyEquals("xy_in") && |
8897 | 3.12M | curStep.paramValues[1].keyEquals("z_in") && |
8898 | 3.12M | prevStep.paramValues[1].keyEquals("z_in") && |
8899 | 3.12M | curStep.paramValues[2].keyEquals("xy_out") && |
8900 | 3.12M | prevStep.paramValues[2].keyEquals("xy_out") && |
8901 | 3.12M | curStep.paramValues[3].keyEquals("z_out") && |
8902 | 3.12M | prevStep.paramValues[3].keyEquals("z_out") && |
8903 | 3.12M | curStep.paramValues[0].value == prevStep.paramValues[2].value && |
8904 | 3.12M | curStep.paramValues[1].value == prevStep.paramValues[3].value && |
8905 | 3.12M | curStep.paramValues[2].value == prevStep.paramValues[0].value && |
8906 | 3.12M | curStep.paramValues[3].value == prevStep.paramValues[1].value) { |
8907 | 11.3k | deletePrevAndCurIter(); |
8908 | 11.3k | continue; |
8909 | 11.3k | } |
8910 | | |
8911 | 3.11M | const auto deletePrevIter = [&steps, &iterPrev, &iterCur]() { |
8912 | 20.8k | steps.erase(iterPrev, iterCur); |
8913 | 20.8k | if (iterCur != steps.begin()) |
8914 | 17.8k | iterCur = std::prev(iterCur); |
8915 | 20.8k | if (iterCur == steps.begin()) |
8916 | 4.35k | ++iterCur; |
8917 | 20.8k | }; |
8918 | | |
8919 | | // combine unitconvert (xy) and unitconvert (z) |
8920 | 3.11M | bool changeDone = false; |
8921 | 9.31M | for (int k = 0; k < 2; ++k) { |
8922 | 6.21M | auto &first = (k == 0) ? curStep : prevStep; |
8923 | 6.21M | auto &second = (k == 0) ? prevStep : curStep; |
8924 | 6.21M | if (first.name == "unitconvert" && |
8925 | 6.21M | second.name == "unitconvert" && !first.inverted && |
8926 | 6.21M | !second.inverted && first.paramValues.size() == 2 && |
8927 | 6.21M | second.paramValues.size() == 2 && |
8928 | 6.21M | second.paramValues[0].keyEquals("xy_in") && |
8929 | 6.21M | second.paramValues[1].keyEquals("xy_out") && |
8930 | 6.21M | first.paramValues[0].keyEquals("z_in") && |
8931 | 6.21M | first.paramValues[1].keyEquals("z_out")) { |
8932 | | |
8933 | 10.3k | const std::string xy_in(second.paramValues[0].value); |
8934 | 10.3k | const std::string xy_out(second.paramValues[1].value); |
8935 | 10.3k | const std::string z_in(first.paramValues[0].value); |
8936 | 10.3k | const std::string z_out(first.paramValues[1].value); |
8937 | | |
8938 | 10.3k | iterCur->paramValues.clear(); |
8939 | 10.3k | iterCur->paramValues.emplace_back( |
8940 | 10.3k | Step::KeyValue("xy_in", xy_in)); |
8941 | 10.3k | iterCur->paramValues.emplace_back( |
8942 | 10.3k | Step::KeyValue("z_in", z_in)); |
8943 | 10.3k | iterCur->paramValues.emplace_back( |
8944 | 10.3k | Step::KeyValue("xy_out", xy_out)); |
8945 | 10.3k | iterCur->paramValues.emplace_back( |
8946 | 10.3k | Step::KeyValue("z_out", z_out)); |
8947 | | |
8948 | 10.3k | deletePrevIter(); |
8949 | 10.3k | changeDone = true; |
8950 | 10.3k | break; |
8951 | 10.3k | } |
8952 | 6.21M | } |
8953 | 3.11M | if (changeDone) { |
8954 | 10.3k | continue; |
8955 | 10.3k | } |
8956 | | |
8957 | | // +step +proj=unitconvert +xy_in=X1 +xy_out=X2 |
8958 | | // +step +proj=unitconvert +xy_in=X2 +z_in=Z1 +xy_out=X1 +z_out=Z2 |
8959 | | // ==> step +proj=unitconvert +z_in=Z1 +z_out=Z2 |
8960 | 9.22M | for (int k = 0; k < 2; ++k) { |
8961 | 6.17M | auto &first = (k == 0) ? curStep : prevStep; |
8962 | 6.17M | auto &second = (k == 0) ? prevStep : curStep; |
8963 | 6.17M | if (first.name == "unitconvert" && |
8964 | 6.17M | second.name == "unitconvert" && !first.inverted && |
8965 | 6.17M | !second.inverted && first.paramValues.size() == 4 && |
8966 | 6.17M | second.paramValues.size() == 2 && |
8967 | 6.17M | first.paramValues[0].keyEquals("xy_in") && |
8968 | 6.17M | first.paramValues[1].keyEquals("z_in") && |
8969 | 6.17M | first.paramValues[2].keyEquals("xy_out") && |
8970 | 6.17M | first.paramValues[3].keyEquals("z_out") && |
8971 | 6.17M | second.paramValues[0].keyEquals("xy_in") && |
8972 | 6.17M | second.paramValues[1].keyEquals("xy_out") && |
8973 | 6.17M | first.paramValues[0].value == second.paramValues[1].value && |
8974 | 6.17M | first.paramValues[2].value == second.paramValues[0].value) { |
8975 | 56.0k | const std::string z_in(first.paramValues[1].value); |
8976 | 56.0k | const std::string z_out(first.paramValues[3].value); |
8977 | 56.0k | if (z_in != z_out) { |
8978 | 8.37k | iterCur->paramValues.clear(); |
8979 | 8.37k | iterCur->paramValues.emplace_back( |
8980 | 8.37k | Step::KeyValue("z_in", z_in)); |
8981 | 8.37k | iterCur->paramValues.emplace_back( |
8982 | 8.37k | Step::KeyValue("z_out", z_out)); |
8983 | 8.37k | deletePrevIter(); |
8984 | 47.6k | } else { |
8985 | 47.6k | deletePrevAndCurIter(); |
8986 | 47.6k | } |
8987 | 56.0k | changeDone = true; |
8988 | 56.0k | break; |
8989 | 56.0k | } |
8990 | 6.17M | } |
8991 | 3.10M | if (changeDone) { |
8992 | 56.0k | continue; |
8993 | 56.0k | } |
8994 | | |
8995 | | // +step +proj=unitconvert +xy_in=X1 +z_in=Z1 +xy_out=X2 +z_out=Z2 |
8996 | | // +step +proj=unitconvert +z_in=Z2 +z_out=Z3 |
8997 | | // ==> +step +proj=unitconvert +xy_in=X1 +z_in=Z1 +xy_out=X2 |
8998 | | // +z_out=Z3 |
8999 | 3.04M | if (prevStep.name == "unitconvert" && |
9000 | 3.04M | curStep.name == "unitconvert" && !prevStep.inverted && |
9001 | 3.04M | !curStep.inverted && prevStep.paramValues.size() == 4 && |
9002 | 3.04M | curStep.paramValues.size() == 2 && |
9003 | 3.04M | prevStep.paramValues[0].keyEquals("xy_in") && |
9004 | 3.04M | prevStep.paramValues[1].keyEquals("z_in") && |
9005 | 3.04M | prevStep.paramValues[2].keyEquals("xy_out") && |
9006 | 3.04M | prevStep.paramValues[3].keyEquals("z_out") && |
9007 | 3.04M | curStep.paramValues[0].keyEquals("z_in") && |
9008 | 3.04M | curStep.paramValues[1].keyEquals("z_out") && |
9009 | 3.04M | prevStep.paramValues[3].value == curStep.paramValues[0].value) { |
9010 | 530 | const std::string xy_in(prevStep.paramValues[0].value); |
9011 | 530 | const std::string z_in(prevStep.paramValues[1].value); |
9012 | 530 | const std::string xy_out(prevStep.paramValues[2].value); |
9013 | 530 | const std::string z_out(curStep.paramValues[1].value); |
9014 | | |
9015 | 530 | iterCur->paramValues.clear(); |
9016 | 530 | iterCur->paramValues.emplace_back( |
9017 | 530 | Step::KeyValue("xy_in", xy_in)); |
9018 | 530 | iterCur->paramValues.emplace_back(Step::KeyValue("z_in", z_in)); |
9019 | 530 | iterCur->paramValues.emplace_back( |
9020 | 530 | Step::KeyValue("xy_out", xy_out)); |
9021 | 530 | iterCur->paramValues.emplace_back( |
9022 | 530 | Step::KeyValue("z_out", z_out)); |
9023 | | |
9024 | 530 | deletePrevIter(); |
9025 | 530 | continue; |
9026 | 530 | } |
9027 | | |
9028 | | // +step +proj=unitconvert +z_in=Z1 +z_out=Z2 |
9029 | | // +step +proj=unitconvert +xy_in=X1 +z_in=Z2 +xy_out=X2 +z_out=Z3 |
9030 | | // ==> +step +proj=unitconvert +xy_in=X1 +z_in=Z1 +xy_out=X2 |
9031 | | // +z_out=Z3 |
9032 | 3.04M | if (prevStep.name == "unitconvert" && |
9033 | 3.04M | curStep.name == "unitconvert" && !prevStep.inverted && |
9034 | 3.04M | !curStep.inverted && prevStep.paramValues.size() == 2 && |
9035 | 3.04M | curStep.paramValues.size() == 4 && |
9036 | 3.04M | prevStep.paramValues[0].keyEquals("z_in") && |
9037 | 3.04M | prevStep.paramValues[1].keyEquals("z_out") && |
9038 | 3.04M | curStep.paramValues[0].keyEquals("xy_in") && |
9039 | 3.04M | curStep.paramValues[1].keyEquals("z_in") && |
9040 | 3.04M | curStep.paramValues[2].keyEquals("xy_out") && |
9041 | 3.04M | curStep.paramValues[3].keyEquals("z_out") && |
9042 | 3.04M | prevStep.paramValues[1].value == curStep.paramValues[1].value) { |
9043 | 142 | const std::string xy_in(curStep.paramValues[0].value); |
9044 | 142 | const std::string z_in(prevStep.paramValues[0].value); |
9045 | 142 | const std::string xy_out(curStep.paramValues[2].value); |
9046 | 142 | const std::string z_out(curStep.paramValues[3].value); |
9047 | | |
9048 | 142 | iterCur->paramValues.clear(); |
9049 | 142 | iterCur->paramValues.emplace_back( |
9050 | 142 | Step::KeyValue("xy_in", xy_in)); |
9051 | 142 | iterCur->paramValues.emplace_back(Step::KeyValue("z_in", z_in)); |
9052 | 142 | iterCur->paramValues.emplace_back( |
9053 | 142 | Step::KeyValue("xy_out", xy_out)); |
9054 | 142 | iterCur->paramValues.emplace_back( |
9055 | 142 | Step::KeyValue("z_out", z_out)); |
9056 | | |
9057 | 142 | deletePrevIter(); |
9058 | 142 | continue; |
9059 | 142 | } |
9060 | | |
9061 | | // +step +proj=unitconvert +xy_in=X1 +z_in=Z1 +xy_out=X2 +z_out=Z2 |
9062 | | // +step +proj=unitconvert +xy_in=X2 +xy_out=X3 |
9063 | | // ==> +step +proj=unitconvert +xy_in=X1 +z_in=Z1 +xy_out=X3 |
9064 | | // +z_out=Z2 |
9065 | 3.04M | if (prevStep.name == "unitconvert" && |
9066 | 3.04M | curStep.name == "unitconvert" && !prevStep.inverted && |
9067 | 3.04M | !curStep.inverted && prevStep.paramValues.size() == 4 && |
9068 | 3.04M | curStep.paramValues.size() == 2 && |
9069 | 3.04M | prevStep.paramValues[0].keyEquals("xy_in") && |
9070 | 3.04M | prevStep.paramValues[1].keyEquals("z_in") && |
9071 | 3.04M | prevStep.paramValues[2].keyEquals("xy_out") && |
9072 | 3.04M | prevStep.paramValues[3].keyEquals("z_out") && |
9073 | 3.04M | curStep.paramValues[0].keyEquals("xy_in") && |
9074 | 3.04M | curStep.paramValues[1].keyEquals("xy_out") && |
9075 | 3.04M | prevStep.paramValues[2].value == curStep.paramValues[0].value) { |
9076 | 12 | const std::string xy_in(prevStep.paramValues[0].value); |
9077 | 12 | const std::string z_in(prevStep.paramValues[1].value); |
9078 | 12 | const std::string xy_out(curStep.paramValues[1].value); |
9079 | 12 | const std::string z_out(prevStep.paramValues[3].value); |
9080 | | |
9081 | 12 | iterCur->paramValues.clear(); |
9082 | 12 | iterCur->paramValues.emplace_back( |
9083 | 12 | Step::KeyValue("xy_in", xy_in)); |
9084 | 12 | iterCur->paramValues.emplace_back(Step::KeyValue("z_in", z_in)); |
9085 | 12 | iterCur->paramValues.emplace_back( |
9086 | 12 | Step::KeyValue("xy_out", xy_out)); |
9087 | 12 | iterCur->paramValues.emplace_back( |
9088 | 12 | Step::KeyValue("z_out", z_out)); |
9089 | | |
9090 | 12 | deletePrevIter(); |
9091 | 12 | continue; |
9092 | 12 | } |
9093 | | |
9094 | | // clang-format off |
9095 | | // A bit odd. Used to simplify geog3d_feet -> EPSG:6318+6360 |
9096 | | // of https://github.com/OSGeo/PROJ/issues/3938 |
9097 | | // where we get originally |
9098 | | // +step +proj=unitconvert +xy_in=deg +z_in=ft +xy_out=rad +z_out=us-ft |
9099 | | // +step +proj=unitconvert +xy_in=rad +z_in=m +xy_out=deg +z_out=m |
9100 | | // and want it simplified as: |
9101 | | // +step +proj=unitconvert +xy_in=deg +z_in=ft +xy_out=deg +z_out=us-ft |
9102 | | // |
9103 | | // More generally: |
9104 | | // +step +proj=unitconvert +xy_in=X1 +z_in=Z1 +xy_out=X2 +z_out=Z2 |
9105 | | // +step +proj=unitconvert +xy_in=X2 +z_in=Z3 +xy_out=X3 +z_out=Z3 |
9106 | | // ==> +step +proj=unitconvert +xy_in=X1 +z_in=Z1 +xy_out=X3 +z_out=Z2 |
9107 | | // clang-format on |
9108 | 3.04M | if (prevStep.name == "unitconvert" && |
9109 | 3.04M | curStep.name == "unitconvert" && !prevStep.inverted && |
9110 | 3.04M | !curStep.inverted && prevStep.paramValues.size() == 4 && |
9111 | 3.04M | curStep.paramValues.size() == 4 && |
9112 | 3.04M | prevStep.paramValues[0].keyEquals("xy_in") && |
9113 | 3.04M | prevStep.paramValues[1].keyEquals("z_in") && |
9114 | 3.04M | prevStep.paramValues[2].keyEquals("xy_out") && |
9115 | 3.04M | prevStep.paramValues[3].keyEquals("z_out") && |
9116 | 3.04M | curStep.paramValues[0].keyEquals("xy_in") && |
9117 | 3.04M | curStep.paramValues[1].keyEquals("z_in") && |
9118 | 3.04M | curStep.paramValues[2].keyEquals("xy_out") && |
9119 | 3.04M | curStep.paramValues[3].keyEquals("z_out") && |
9120 | 3.04M | prevStep.paramValues[2].value == curStep.paramValues[0].value && |
9121 | 3.04M | curStep.paramValues[1].value == curStep.paramValues[3].value) { |
9122 | 728 | const std::string xy_in(prevStep.paramValues[0].value); |
9123 | 728 | const std::string z_in(prevStep.paramValues[1].value); |
9124 | 728 | const std::string xy_out(curStep.paramValues[2].value); |
9125 | 728 | const std::string z_out(prevStep.paramValues[3].value); |
9126 | | |
9127 | 728 | iterCur->paramValues.clear(); |
9128 | 728 | iterCur->paramValues.emplace_back( |
9129 | 728 | Step::KeyValue("xy_in", xy_in)); |
9130 | 728 | iterCur->paramValues.emplace_back(Step::KeyValue("z_in", z_in)); |
9131 | 728 | iterCur->paramValues.emplace_back( |
9132 | 728 | Step::KeyValue("xy_out", xy_out)); |
9133 | 728 | iterCur->paramValues.emplace_back( |
9134 | 728 | Step::KeyValue("z_out", z_out)); |
9135 | | |
9136 | 728 | deletePrevIter(); |
9137 | 728 | continue; |
9138 | 728 | } |
9139 | | |
9140 | | // clang-format off |
9141 | | // Variant of above |
9142 | | // +step +proj=unitconvert +xy_in=X1 +z_in=Z1 +xy_out=X2 +z_out=Z1 |
9143 | | // +step +proj=unitconvert +xy_in=X2 +z_in=Z2 +xy_out=X3 +z_out=Z3 |
9144 | | // ==> +step +proj=unitconvert +xy_in=X1 +z_in=Z2 +xy_out=X3 +z_out=Z3 |
9145 | | // clang-format on |
9146 | 3.04M | if (prevStep.name == "unitconvert" && |
9147 | 3.04M | curStep.name == "unitconvert" && !prevStep.inverted && |
9148 | 3.04M | !curStep.inverted && prevStep.paramValues.size() == 4 && |
9149 | 3.04M | curStep.paramValues.size() == 4 && |
9150 | 3.04M | prevStep.paramValues[0].keyEquals("xy_in") && |
9151 | 3.04M | prevStep.paramValues[1].keyEquals("z_in") && |
9152 | 3.04M | prevStep.paramValues[2].keyEquals("xy_out") && |
9153 | 3.04M | prevStep.paramValues[3].keyEquals("z_out") && |
9154 | 3.04M | curStep.paramValues[0].keyEquals("xy_in") && |
9155 | 3.04M | curStep.paramValues[1].keyEquals("z_in") && |
9156 | 3.04M | curStep.paramValues[2].keyEquals("xy_out") && |
9157 | 3.04M | curStep.paramValues[3].keyEquals("z_out") && |
9158 | 3.04M | prevStep.paramValues[1].value == |
9159 | 1.26k | prevStep.paramValues[3].value && |
9160 | 3.04M | curStep.paramValues[0].value == prevStep.paramValues[2].value) { |
9161 | 640 | const std::string xy_in(prevStep.paramValues[0].value); |
9162 | 640 | const std::string z_in(curStep.paramValues[1].value); |
9163 | 640 | const std::string xy_out(curStep.paramValues[2].value); |
9164 | 640 | const std::string z_out(curStep.paramValues[3].value); |
9165 | | |
9166 | 640 | iterCur->paramValues.clear(); |
9167 | 640 | iterCur->paramValues.emplace_back( |
9168 | 640 | Step::KeyValue("xy_in", xy_in)); |
9169 | 640 | iterCur->paramValues.emplace_back(Step::KeyValue("z_in", z_in)); |
9170 | 640 | iterCur->paramValues.emplace_back( |
9171 | 640 | Step::KeyValue("xy_out", xy_out)); |
9172 | 640 | iterCur->paramValues.emplace_back( |
9173 | 640 | Step::KeyValue("z_out", z_out)); |
9174 | | |
9175 | 640 | deletePrevIter(); |
9176 | 640 | continue; |
9177 | 640 | } |
9178 | | |
9179 | | // unitconvert (1), axisswap order=2,1, unitconvert(2) ==> |
9180 | | // axisswap order=2,1, unitconvert (1), unitconvert(2) which |
9181 | | // will get further optimized by previous case |
9182 | 3.04M | if (std::next(iterCur) != steps.end() && |
9183 | 3.04M | prevStep.name == "unitconvert" && curStep.name == "axisswap" && |
9184 | 3.04M | curStepParamCount == 1 && |
9185 | 3.04M | curStep.paramValues[0].equals("order", "2,1")) { |
9186 | 316k | auto iterNext = std::next(iterCur); |
9187 | 316k | auto &nextStep = *iterNext; |
9188 | 316k | if (nextStep.name == "unitconvert") { |
9189 | 2.24k | std::swap(*iterPrev, *iterCur); |
9190 | 2.24k | ++iterCur; |
9191 | 2.24k | continue; |
9192 | 2.24k | } |
9193 | 316k | } |
9194 | | |
9195 | | // axisswap order=2,1 followed by itself is a no-op |
9196 | 3.03M | if (curStep.name == "axisswap" && prevStep.name == "axisswap" && |
9197 | 3.03M | curStepParamCount == 1 && prevStepParamCount == 1 && |
9198 | 3.03M | curStep.paramValues[0].equals("order", "2,1") && |
9199 | 3.03M | prevStep.paramValues[0].equals("order", "2,1")) { |
9200 | 357k | deletePrevAndCurIter(); |
9201 | 357k | continue; |
9202 | 357k | } |
9203 | | |
9204 | | // axisswap order=2,-1 followed by axisswap order=-2,1 is a no-op |
9205 | 2.68M | if (curStep.name == "axisswap" && prevStep.name == "axisswap" && |
9206 | 2.68M | curStepParamCount == 1 && prevStepParamCount == 1 && |
9207 | 2.68M | !prevStep.inverted && |
9208 | 2.68M | prevStep.paramValues[0].equals("order", "2,-1") && |
9209 | 2.68M | !curStep.inverted && |
9210 | 2.68M | curStep.paramValues[0].equals("order", "-2,1")) { |
9211 | 46 | deletePrevAndCurIter(); |
9212 | 46 | continue; |
9213 | 46 | } |
9214 | | |
9215 | | // axisswap order=2,-1 followed by axisswap order=1,-2 is |
9216 | | // equivalent to axisswap order=2,1 |
9217 | 2.68M | if (curStep.name == "axisswap" && prevStep.name == "axisswap" && |
9218 | 2.68M | curStepParamCount == 1 && prevStepParamCount == 1 && |
9219 | 2.68M | !prevStep.inverted && |
9220 | 2.68M | prevStep.paramValues[0].equals("order", "2,-1") && |
9221 | 2.68M | !curStep.inverted && |
9222 | 2.68M | curStep.paramValues[0].equals("order", "1,-2")) { |
9223 | 0 | prevStep.inverted = false; |
9224 | 0 | prevStep.paramValues[0] = Step::KeyValue("order", "2,1"); |
9225 | | // Delete this iter |
9226 | 0 | iterCur = steps.erase(iterCur); |
9227 | 0 | continue; |
9228 | 0 | } |
9229 | | |
9230 | | // axisswap order=2,1 followed by axisswap order=2,-1 is |
9231 | | // equivalent to axisswap order=1,-2 |
9232 | | // Same for axisswap order=-2,1 followed by axisswap order=2,1 |
9233 | 2.68M | if (curStep.name == "axisswap" && prevStep.name == "axisswap" && |
9234 | 2.68M | curStepParamCount == 1 && prevStepParamCount == 1 && |
9235 | 2.68M | ((prevStep.paramValues[0].equals("order", "2,1") && |
9236 | 686 | !curStep.inverted && |
9237 | 686 | curStep.paramValues[0].equals("order", "2,-1")) || |
9238 | 686 | (prevStep.paramValues[0].equals("order", "-2,1") && |
9239 | 686 | !prevStep.inverted && |
9240 | 686 | curStep.paramValues[0].equals("order", "2,1")))) { |
9241 | | |
9242 | 8 | prevStep.inverted = false; |
9243 | 8 | prevStep.paramValues[0] = Step::KeyValue("order", "1,-2"); |
9244 | | // Delete this iter |
9245 | 8 | iterCur = steps.erase(iterCur); |
9246 | 8 | continue; |
9247 | 8 | } |
9248 | | |
9249 | | // axisswap order=2,1, unitconvert, axisswap order=2,1 -> can |
9250 | | // suppress axisswap |
9251 | 2.68M | if (std::next(iterCur) != steps.end() && |
9252 | 2.68M | prevStep.name == "axisswap" && curStep.name == "unitconvert" && |
9253 | 2.68M | prevStepParamCount == 1 && |
9254 | 2.68M | prevStep.paramValues[0].equals("order", "2,1")) { |
9255 | 31.4k | auto iterNext = std::next(iterCur); |
9256 | 31.4k | auto &nextStep = *iterNext; |
9257 | 31.4k | if (nextStep.name == "axisswap" && |
9258 | 31.4k | nextStep.paramValues.size() == 1 && |
9259 | 31.4k | nextStep.paramValues[0].equals("order", "2,1")) { |
9260 | 1.01k | steps.erase(iterPrev); |
9261 | 1.01k | steps.erase(iterNext); |
9262 | | // Coverity complains about invalid usage of iterCur |
9263 | | // due to the above erase(iterNext). To the best of our |
9264 | | // understanding, this is a false-positive. |
9265 | | // coverity[use_iterator] |
9266 | 1.01k | if (iterCur != steps.begin()) |
9267 | 1.00k | iterCur = std::prev(iterCur); |
9268 | 1.01k | if (iterCur == steps.begin()) |
9269 | 2 | ++iterCur; |
9270 | 1.01k | continue; |
9271 | 1.01k | } |
9272 | 31.4k | } |
9273 | | |
9274 | | // for practical purposes WGS84 and GRS80 ellipsoids are |
9275 | | // equivalents (cartesian transform between both lead to differences |
9276 | | // of the order of 1e-14 deg..). |
9277 | | // No need to do a cart roundtrip for that... |
9278 | | // and actually IGNF uses the GRS80 definition for the WGS84 datum |
9279 | 2.68M | if (curStep.name == "cart" && prevStep.name == "cart" && |
9280 | 2.68M | curStep.inverted == !prevStep.inverted && |
9281 | 2.68M | curStepParamCount == 1 && prevStepParamCount == 1 && |
9282 | 2.68M | ((curStep.paramValues[0].equals("ellps", "WGS84") && |
9283 | 38.1k | prevStep.paramValues[0].equals("ellps", "GRS80")) || |
9284 | 38.1k | (curStep.paramValues[0].equals("ellps", "GRS80") && |
9285 | 33.3k | prevStep.paramValues[0].equals("ellps", "WGS84")))) { |
9286 | 6.07k | deletePrevAndCurIter(); |
9287 | 6.07k | continue; |
9288 | 6.07k | } |
9289 | | |
9290 | 2.67M | if (curStep.name == "helmert" && prevStep.name == "helmert" && |
9291 | 2.67M | !curStep.inverted && !prevStep.inverted && |
9292 | 2.67M | curStepParamCount == 3 && |
9293 | 2.67M | curStepParamCount == prevStepParamCount) { |
9294 | 11.0k | std::map<std::string, double> leftParamsMap; |
9295 | 11.0k | std::map<std::string, double> rightParamsMap; |
9296 | 11.0k | try { |
9297 | 32.8k | for (const auto &kv : prevStep.paramValues) { |
9298 | 32.8k | leftParamsMap[kv.key] = c_locale_stod(kv.value); |
9299 | 32.8k | } |
9300 | 32.6k | for (const auto &kv : curStep.paramValues) { |
9301 | 32.6k | rightParamsMap[kv.key] = c_locale_stod(kv.value); |
9302 | 32.6k | } |
9303 | 11.0k | } catch (const std::invalid_argument &) { |
9304 | 202 | break; |
9305 | 202 | } |
9306 | 10.8k | const std::string x("x"); |
9307 | 10.8k | const std::string y("y"); |
9308 | 10.8k | const std::string z("z"); |
9309 | 10.8k | if (leftParamsMap.find(x) != leftParamsMap.end() && |
9310 | 10.8k | leftParamsMap.find(y) != leftParamsMap.end() && |
9311 | 10.8k | leftParamsMap.find(z) != leftParamsMap.end() && |
9312 | 10.8k | rightParamsMap.find(x) != rightParamsMap.end() && |
9313 | 10.8k | rightParamsMap.find(y) != rightParamsMap.end() && |
9314 | 10.8k | rightParamsMap.find(z) != rightParamsMap.end()) { |
9315 | | |
9316 | 10.3k | const double xSum = leftParamsMap[x] + rightParamsMap[x]; |
9317 | 10.3k | const double ySum = leftParamsMap[y] + rightParamsMap[y]; |
9318 | 10.3k | const double zSum = leftParamsMap[z] + rightParamsMap[z]; |
9319 | 10.3k | if (xSum == 0.0 && ySum == 0.0 && zSum == 0.0) { |
9320 | 1.12k | deletePrevAndCurIter(); |
9321 | 9.25k | } else { |
9322 | 9.25k | prevStep.paramValues[0] = |
9323 | 9.25k | Step::KeyValue("x", internal::toString(xSum)); |
9324 | 9.25k | prevStep.paramValues[1] = |
9325 | 9.25k | Step::KeyValue("y", internal::toString(ySum)); |
9326 | 9.25k | prevStep.paramValues[2] = |
9327 | 9.25k | Step::KeyValue("z", internal::toString(zSum)); |
9328 | | |
9329 | | // Delete this iter |
9330 | 9.25k | iterCur = steps.erase(iterCur); |
9331 | 9.25k | } |
9332 | 10.3k | continue; |
9333 | 10.3k | } |
9334 | 10.8k | } |
9335 | | |
9336 | | // Helmert followed by its inverse is a no-op |
9337 | 2.66M | if (curStep.name == "helmert" && prevStep.name == "helmert" && |
9338 | 2.66M | !curStep.inverted && !prevStep.inverted && |
9339 | 2.66M | curStepParamCount == prevStepParamCount) { |
9340 | 977 | std::set<std::string> leftParamsSet; |
9341 | 977 | std::set<std::string> rightParamsSet; |
9342 | 977 | std::map<std::string, std::string> leftParamsMap; |
9343 | 977 | std::map<std::string, std::string> rightParamsMap; |
9344 | 4.42k | for (const auto &kv : prevStep.paramValues) { |
9345 | 4.42k | leftParamsSet.insert(kv.key); |
9346 | 4.42k | leftParamsMap[kv.key] = kv.value; |
9347 | 4.42k | } |
9348 | 4.42k | for (const auto &kv : curStep.paramValues) { |
9349 | 4.42k | rightParamsSet.insert(kv.key); |
9350 | 4.42k | rightParamsMap[kv.key] = kv.value; |
9351 | 4.42k | } |
9352 | 977 | if (leftParamsSet == rightParamsSet) { |
9353 | 261 | bool doErase = true; |
9354 | 261 | try { |
9355 | 610 | for (const auto ¶m : leftParamsSet) { |
9356 | 610 | if (param == "convention" || param == "t_epoch" || |
9357 | 610 | param == "t_obs") { |
9358 | 107 | if (leftParamsMap[param] != |
9359 | 107 | rightParamsMap[param]) { |
9360 | 78 | doErase = false; |
9361 | 78 | break; |
9362 | 78 | } |
9363 | 503 | } else if (c_locale_stod(leftParamsMap[param]) != |
9364 | 503 | -c_locale_stod(rightParamsMap[param])) { |
9365 | 138 | doErase = false; |
9366 | 138 | break; |
9367 | 138 | } |
9368 | 610 | } |
9369 | 261 | } catch (const std::invalid_argument &) { |
9370 | 28 | break; |
9371 | 28 | } |
9372 | 233 | if (doErase) { |
9373 | 17 | deletePrevAndCurIter(); |
9374 | 17 | continue; |
9375 | 17 | } |
9376 | 233 | } |
9377 | 977 | } |
9378 | | |
9379 | | // The following should be optimized as a no-op |
9380 | | // +step +proj=helmert +x=25 +y=-141 +z=-78.5 +rx=0 +ry=-0.35 |
9381 | | // +rz=-0.736 +s=0 +convention=coordinate_frame |
9382 | | // +step +inv +proj=helmert +x=25 +y=-141 +z=-78.5 +rx=0 +ry=0.35 |
9383 | | // +rz=0.736 +s=0 +convention=position_vector |
9384 | 2.66M | if (curStep.name == "helmert" && prevStep.name == "helmert" && |
9385 | 2.66M | ((curStep.inverted && !prevStep.inverted) || |
9386 | 12.2k | (!curStep.inverted && prevStep.inverted)) && |
9387 | 2.66M | curStepParamCount == prevStepParamCount) { |
9388 | 5.80k | std::set<std::string> leftParamsSet; |
9389 | 5.80k | std::set<std::string> rightParamsSet; |
9390 | 5.80k | std::map<std::string, std::string> leftParamsMap; |
9391 | 5.80k | std::map<std::string, std::string> rightParamsMap; |
9392 | 26.9k | for (const auto &kv : prevStep.paramValues) { |
9393 | 26.9k | leftParamsSet.insert(kv.key); |
9394 | 26.9k | leftParamsMap[kv.key] = kv.value; |
9395 | 26.9k | } |
9396 | 26.9k | for (const auto &kv : curStep.paramValues) { |
9397 | 26.9k | rightParamsSet.insert(kv.key); |
9398 | 26.9k | rightParamsMap[kv.key] = kv.value; |
9399 | 26.9k | } |
9400 | 5.80k | if (leftParamsSet == rightParamsSet) { |
9401 | 5.36k | bool doErase = true; |
9402 | 5.36k | try { |
9403 | 8.88k | for (const auto ¶m : leftParamsSet) { |
9404 | 8.88k | if (param == "convention") { |
9405 | | // Convention must be different |
9406 | 1.08k | if (leftParamsMap[param] == |
9407 | 1.08k | rightParamsMap[param]) { |
9408 | 1.06k | doErase = false; |
9409 | 1.06k | break; |
9410 | 1.06k | } |
9411 | 7.79k | } else if (param == "rx" || param == "ry" || |
9412 | 7.79k | param == "rz" || param == "drx" || |
9413 | 7.79k | param == "dry" || param == "drz") { |
9414 | | // Rotational parameters should have opposite |
9415 | | // value |
9416 | 253 | if (c_locale_stod(leftParamsMap[param]) != |
9417 | 253 | -c_locale_stod(rightParamsMap[param])) { |
9418 | 72 | doErase = false; |
9419 | 72 | break; |
9420 | 72 | } |
9421 | 7.54k | } else { |
9422 | | // Non rotational parameters should have the |
9423 | | // same value |
9424 | 7.54k | if (leftParamsMap[param] != |
9425 | 7.54k | rightParamsMap[param]) { |
9426 | 3.40k | doErase = false; |
9427 | 3.40k | break; |
9428 | 3.40k | } |
9429 | 7.54k | } |
9430 | 8.88k | } |
9431 | 5.36k | } catch (const std::invalid_argument &) { |
9432 | 49 | break; |
9433 | 49 | } |
9434 | 5.31k | if (doErase) { |
9435 | 773 | deletePrevAndCurIter(); |
9436 | 773 | continue; |
9437 | 773 | } |
9438 | 5.31k | } |
9439 | 5.80k | } |
9440 | | |
9441 | | // Optimize patterns like Krovak (South West) to Krovak East North |
9442 | | // (also applies to Modified Krovak) |
9443 | | // +step +inv +proj=krovak +axis=swu +lat_0=49.5 |
9444 | | // +lon_0=24.8333333333333 |
9445 | | // +alpha=30.2881397527778 +k=0.9999 +x_0=0 +y_0=0 +ellps=bessel |
9446 | | // +step +proj=krovak +lat_0=49.5 +lon_0=24.8333333333333 |
9447 | | // +alpha=30.2881397527778 +k=0.9999 +x_0=0 +y_0=0 +ellps=bessel |
9448 | | // as: |
9449 | | // +step +proj=axisswap +order=-2,-1 |
9450 | | // Also applies for the symmetrical case where +axis=swu is on the |
9451 | | // second step. |
9452 | 2.66M | if (curStep.inverted != prevStep.inverted && |
9453 | 2.66M | curStep.name == prevStep.name && |
9454 | 2.66M | ((curStepParamCount + 1 == prevStepParamCount && |
9455 | 87.4k | prevStep.paramValues[0].equals("axis", "swu")) || |
9456 | 87.4k | (prevStepParamCount + 1 == curStepParamCount && |
9457 | 87.2k | curStep.paramValues[0].equals("axis", "swu")))) { |
9458 | 186 | const auto &swStep = (curStepParamCount < prevStepParamCount) |
9459 | 186 | ? prevStep |
9460 | 186 | : curStep; |
9461 | 186 | const auto &enStep = (curStepParamCount < prevStepParamCount) |
9462 | 186 | ? curStep |
9463 | 186 | : prevStep; |
9464 | | // Check if all remaining parameters (except leading axis=swu |
9465 | | // in swStep) are identical. |
9466 | 186 | bool allSame = true; |
9467 | 186 | for (size_t j = 0; |
9468 | 214 | j < std::min(curStepParamCount, prevStepParamCount); j++) { |
9469 | 72 | if (enStep.paramValues[j] != swStep.paramValues[j + 1]) { |
9470 | 44 | allSame = false; |
9471 | 44 | break; |
9472 | 44 | } |
9473 | 72 | } |
9474 | 186 | if (allSame) { |
9475 | 142 | iterCur->inverted = false; |
9476 | 142 | iterCur->name = "axisswap"; |
9477 | 142 | iterCur->paramValues.clear(); |
9478 | 142 | iterCur->paramValues.emplace_back( |
9479 | 142 | Step::KeyValue("order", "-2,-1")); |
9480 | | |
9481 | 142 | deletePrevIter(); |
9482 | 142 | continue; |
9483 | 142 | } |
9484 | 186 | } |
9485 | | |
9486 | | // detect a step and its inverse |
9487 | 2.66M | if (curStep.inverted != prevStep.inverted && |
9488 | 2.66M | curStep.name == prevStep.name && |
9489 | 2.66M | curStepParamCount == prevStepParamCount) { |
9490 | 77.3k | bool allSame = true; |
9491 | 141k | for (size_t j = 0; j < curStepParamCount; j++) { |
9492 | 101k | if (curStep.paramValues[j] != prevStep.paramValues[j]) { |
9493 | 36.7k | allSame = false; |
9494 | 36.7k | break; |
9495 | 36.7k | } |
9496 | 101k | } |
9497 | 77.3k | if (allSame) { |
9498 | 40.5k | deletePrevAndCurIter(); |
9499 | 40.5k | continue; |
9500 | 40.5k | } |
9501 | 77.3k | } |
9502 | | |
9503 | 2.62M | ++iterCur; |
9504 | 2.62M | } |
9505 | 236k | } |
9506 | | |
9507 | 236k | { |
9508 | 236k | auto iterCur = steps.begin(); |
9509 | 236k | if (iterCur != steps.end()) { |
9510 | 225k | ++iterCur; |
9511 | 225k | } |
9512 | 1.44M | while (iterCur != steps.end()) { |
9513 | | |
9514 | 1.21M | assert(iterCur != steps.begin()); |
9515 | 1.21M | auto iterPrev = std::prev(iterCur); |
9516 | 1.21M | auto &prevStep = *iterPrev; |
9517 | 1.21M | auto &curStep = *iterCur; |
9518 | | |
9519 | 1.21M | const auto curStepParamCount = curStep.paramValues.size(); |
9520 | 1.21M | const auto prevStepParamCount = prevStep.paramValues.size(); |
9521 | | |
9522 | | // +step +proj=hgridshift +grids=grid_A |
9523 | | // +step +proj=vgridshift [...] <== curStep |
9524 | | // +step +inv +proj=hgridshift +grids=grid_A |
9525 | | // ==> |
9526 | | // +step +proj=push +v_1 +v_2 |
9527 | | // +step +proj=hgridshift +grids=grid_A +omit_inv |
9528 | | // +step +proj=vgridshift [...] |
9529 | | // +step +inv +proj=hgridshift +grids=grid_A +omit_fwd |
9530 | | // +step +proj=pop +v_1 +v_2 |
9531 | 1.21M | if (std::next(iterCur) != steps.end() && |
9532 | 1.21M | prevStep.name == "hgridshift" && prevStepParamCount == 1 && |
9533 | 1.21M | curStep.name == "vgridshift") { |
9534 | 2.47k | auto iterNext = std::next(iterCur); |
9535 | 2.47k | auto &nextStep = *iterNext; |
9536 | 2.47k | if (nextStep.name == "hgridshift" && |
9537 | 2.47k | nextStep.inverted != prevStep.inverted && |
9538 | 2.47k | nextStep.paramValues.size() == 1 && |
9539 | 2.47k | prevStep.paramValues[0] == nextStep.paramValues[0]) { |
9540 | 857 | Step pushStep; |
9541 | 857 | pushStep.name = "push"; |
9542 | 857 | pushStep.paramValues.emplace_back("v_1"); |
9543 | 857 | pushStep.paramValues.emplace_back("v_2"); |
9544 | 857 | steps.insert(iterPrev, pushStep); |
9545 | | |
9546 | 857 | prevStep.paramValues.emplace_back("omit_inv"); |
9547 | | |
9548 | 857 | nextStep.paramValues.emplace_back("omit_fwd"); |
9549 | | |
9550 | 857 | Step popStep; |
9551 | 857 | popStep.name = "pop"; |
9552 | 857 | popStep.paramValues.emplace_back("v_1"); |
9553 | 857 | popStep.paramValues.emplace_back("v_2"); |
9554 | 857 | steps.insert(std::next(iterNext), popStep); |
9555 | | |
9556 | 857 | continue; |
9557 | 857 | } |
9558 | 2.47k | } |
9559 | | |
9560 | | // +step +proj=unitconvert +xy_in=rad +xy_out=deg |
9561 | | // +step +proj=axisswap +order=2,1 |
9562 | | // +step +proj=push +v_1 +v_2 |
9563 | | // +step +proj=axisswap +order=2,1 |
9564 | | // +step +proj=unitconvert +xy_in=deg +xy_out=rad |
9565 | | // +step +proj=vgridshift ... |
9566 | | // +step +proj=unitconvert +xy_in=rad +xy_out=deg |
9567 | | // +step +proj=axisswap +order=2,1 |
9568 | | // +step +proj=pop +v_1 +v_2 |
9569 | | // ==> |
9570 | | // +step +proj=vgridshift ... |
9571 | | // +step +proj=unitconvert +xy_in=rad +xy_out=deg |
9572 | | // +step +proj=axisswap +order=2,1 |
9573 | 1.21M | if (prevStep.name == "unitconvert" && prevStepParamCount == 2 && |
9574 | 1.21M | prevStep.paramValues[0].equals("xy_in", "rad") && |
9575 | 1.21M | prevStep.paramValues[1].equals("xy_out", "deg") && |
9576 | 1.21M | curStep.name == "axisswap" && curStepParamCount == 1 && |
9577 | 1.21M | curStep.paramValues[0].equals("order", "2,1")) { |
9578 | 10.1k | auto iterNext = std::next(iterCur); |
9579 | 10.1k | bool ok = false; |
9580 | 10.1k | if (iterNext != steps.end()) { |
9581 | 2.03k | auto &nextStep = *iterNext; |
9582 | 2.03k | if (nextStep.name == "push" && |
9583 | 2.03k | nextStep.paramValues.size() == 2 && |
9584 | 2.03k | nextStep.paramValues[0].keyEquals("v_1") && |
9585 | 2.03k | nextStep.paramValues[1].keyEquals("v_2")) { |
9586 | 0 | ok = true; |
9587 | 0 | iterNext = std::next(iterNext); |
9588 | 0 | } |
9589 | 2.03k | } |
9590 | 10.1k | ok &= iterNext != steps.end(); |
9591 | 10.1k | if (ok) { |
9592 | 0 | ok = false; |
9593 | 0 | auto &nextStep = *iterNext; |
9594 | 0 | if (nextStep.name == "axisswap" && |
9595 | 0 | nextStep.paramValues.size() == 1 && |
9596 | 0 | nextStep.paramValues[0].equals("order", "2,1")) { |
9597 | 0 | ok = true; |
9598 | 0 | iterNext = std::next(iterNext); |
9599 | 0 | } |
9600 | 0 | } |
9601 | 10.1k | ok &= iterNext != steps.end(); |
9602 | 10.1k | if (ok) { |
9603 | 0 | ok = false; |
9604 | 0 | auto &nextStep = *iterNext; |
9605 | 0 | if (nextStep.name == "unitconvert" && |
9606 | 0 | nextStep.paramValues.size() == 2 && |
9607 | 0 | nextStep.paramValues[0].equals("xy_in", "deg") && |
9608 | 0 | nextStep.paramValues[1].equals("xy_out", "rad")) { |
9609 | 0 | ok = true; |
9610 | 0 | iterNext = std::next(iterNext); |
9611 | 0 | } |
9612 | 0 | } |
9613 | 10.1k | auto iterVgridshift = iterNext; |
9614 | 10.1k | ok &= iterNext != steps.end(); |
9615 | 10.1k | if (ok) { |
9616 | 0 | ok = false; |
9617 | 0 | auto &nextStep = *iterNext; |
9618 | 0 | if (nextStep.name == "vgridshift") { |
9619 | 0 | ok = true; |
9620 | 0 | iterNext = std::next(iterNext); |
9621 | 0 | } |
9622 | 0 | } |
9623 | 10.1k | ok &= iterNext != steps.end(); |
9624 | 10.1k | if (ok) { |
9625 | 0 | ok = false; |
9626 | 0 | auto &nextStep = *iterNext; |
9627 | 0 | if (nextStep.name == "unitconvert" && |
9628 | 0 | nextStep.paramValues.size() == 2 && |
9629 | 0 | nextStep.paramValues[0].equals("xy_in", "rad") && |
9630 | 0 | nextStep.paramValues[1].equals("xy_out", "deg")) { |
9631 | 0 | ok = true; |
9632 | 0 | iterNext = std::next(iterNext); |
9633 | 0 | } |
9634 | 0 | } |
9635 | 10.1k | ok &= iterNext != steps.end(); |
9636 | 10.1k | if (ok) { |
9637 | 0 | ok = false; |
9638 | 0 | auto &nextStep = *iterNext; |
9639 | 0 | if (nextStep.name == "axisswap" && |
9640 | 0 | nextStep.paramValues.size() == 1 && |
9641 | 0 | nextStep.paramValues[0].equals("order", "2,1")) { |
9642 | 0 | ok = true; |
9643 | 0 | iterNext = std::next(iterNext); |
9644 | 0 | } |
9645 | 0 | } |
9646 | 10.1k | ok &= iterNext != steps.end(); |
9647 | 10.1k | if (ok) { |
9648 | 0 | ok = false; |
9649 | 0 | auto &nextStep = *iterNext; |
9650 | 0 | if (nextStep.name == "pop" && |
9651 | 0 | nextStep.paramValues.size() == 2 && |
9652 | 0 | nextStep.paramValues[0].keyEquals("v_1") && |
9653 | 0 | nextStep.paramValues[1].keyEquals("v_2")) { |
9654 | 0 | ok = true; |
9655 | | // iterNext = std::next(iterNext); |
9656 | 0 | } |
9657 | 0 | } |
9658 | 10.1k | if (ok) { |
9659 | 0 | steps.erase(iterPrev, iterVgridshift); |
9660 | 0 | steps.erase(iterNext, std::next(iterNext)); |
9661 | 0 | iterPrev = std::prev(iterVgridshift); |
9662 | 0 | iterCur = iterVgridshift; |
9663 | 0 | continue; |
9664 | 0 | } |
9665 | 10.1k | } |
9666 | | |
9667 | | // +step +proj=axisswap +order=2,1 |
9668 | | // +step +proj=unitconvert +xy_in=deg +xy_out=rad |
9669 | | // +step +proj=vgridshift ... |
9670 | | // +step +proj=unitconvert +xy_in=rad +xy_out=deg |
9671 | | // +step +proj=axisswap +order=2,1 |
9672 | | // +step +proj=push +v_1 +v_2 |
9673 | | // +step +proj=axisswap +order=2,1 |
9674 | | // +step +proj=unitconvert +xy_in=deg +xy_out=rad |
9675 | | // ==> |
9676 | | // +step +proj=push +v_1 +v_2 |
9677 | | // +step +proj=axisswap +order=2,1 |
9678 | | // +step +proj=unitconvert +xy_in=deg +xy_out=rad |
9679 | | // +step +proj=vgridshift ... |
9680 | | |
9681 | 1.21M | if (prevStep.name == "axisswap" && prevStepParamCount == 1 && |
9682 | 1.21M | prevStep.paramValues[0].equals("order", "2,1") && |
9683 | 1.21M | curStep.name == "unitconvert" && curStepParamCount == 2 && |
9684 | 1.21M | !curStep.inverted && |
9685 | 1.21M | curStep.paramValues[0].equals("xy_in", "deg") && |
9686 | 1.21M | curStep.paramValues[1].equals("xy_out", "rad")) { |
9687 | 15.1k | auto iterNext = std::next(iterCur); |
9688 | 15.1k | bool ok = false; |
9689 | 15.1k | auto iterVgridshift = iterNext; |
9690 | 15.1k | if (iterNext != steps.end()) { |
9691 | 15.1k | auto &nextStep = *iterNext; |
9692 | 15.1k | if (nextStep.name == "vgridshift") { |
9693 | 3.36k | ok = true; |
9694 | 3.36k | iterNext = std::next(iterNext); |
9695 | 3.36k | } |
9696 | 15.1k | } |
9697 | 15.1k | ok &= iterNext != steps.end(); |
9698 | 15.1k | if (ok) { |
9699 | 3.36k | ok = false; |
9700 | 3.36k | auto &nextStep = *iterNext; |
9701 | 3.36k | if (nextStep.name == "unitconvert" && !nextStep.inverted && |
9702 | 3.36k | nextStep.paramValues.size() == 2 && |
9703 | 3.36k | nextStep.paramValues[0].equals("xy_in", "rad") && |
9704 | 3.36k | nextStep.paramValues[1].equals("xy_out", "deg")) { |
9705 | 853 | ok = true; |
9706 | 853 | iterNext = std::next(iterNext); |
9707 | 853 | } |
9708 | 3.36k | } |
9709 | 15.1k | ok &= iterNext != steps.end(); |
9710 | 15.1k | if (ok) { |
9711 | 239 | ok = false; |
9712 | 239 | auto &nextStep = *iterNext; |
9713 | 239 | if (nextStep.name == "axisswap" && |
9714 | 239 | nextStep.paramValues.size() == 1 && |
9715 | 239 | nextStep.paramValues[0].equals("order", "2,1")) { |
9716 | 206 | ok = true; |
9717 | 206 | iterNext = std::next(iterNext); |
9718 | 206 | } |
9719 | 239 | } |
9720 | 15.1k | auto iterPush = iterNext; |
9721 | 15.1k | ok &= iterNext != steps.end(); |
9722 | 15.1k | if (ok) { |
9723 | 115 | ok = false; |
9724 | 115 | auto &nextStep = *iterNext; |
9725 | 115 | if (nextStep.name == "push" && |
9726 | 115 | nextStep.paramValues.size() == 2 && |
9727 | 115 | nextStep.paramValues[0].keyEquals("v_1") && |
9728 | 115 | nextStep.paramValues[1].keyEquals("v_2")) { |
9729 | 0 | ok = true; |
9730 | 0 | iterNext = std::next(iterNext); |
9731 | 0 | } |
9732 | 115 | } |
9733 | 15.1k | ok &= iterNext != steps.end(); |
9734 | 15.1k | if (ok) { |
9735 | 0 | ok = false; |
9736 | 0 | auto &nextStep = *iterNext; |
9737 | 0 | if (nextStep.name == "axisswap" && |
9738 | 0 | nextStep.paramValues.size() == 1 && |
9739 | 0 | nextStep.paramValues[0].equals("order", "2,1")) { |
9740 | 0 | ok = true; |
9741 | 0 | iterNext = std::next(iterNext); |
9742 | 0 | } |
9743 | 0 | } |
9744 | 15.1k | ok &= iterNext != steps.end(); |
9745 | 15.1k | if (ok) { |
9746 | 0 | ok = false; |
9747 | 0 | auto &nextStep = *iterNext; |
9748 | 0 | if (nextStep.name == "unitconvert" && |
9749 | 0 | nextStep.paramValues.size() == 2 && |
9750 | 0 | !nextStep.inverted && |
9751 | 0 | nextStep.paramValues[0].equals("xy_in", "deg") && |
9752 | 0 | nextStep.paramValues[1].equals("xy_out", "rad")) { |
9753 | 0 | ok = true; |
9754 | | // iterNext = std::next(iterNext); |
9755 | 0 | } |
9756 | 0 | } |
9757 | | |
9758 | 15.1k | if (ok) { |
9759 | 0 | Step stepVgridshift(*iterVgridshift); |
9760 | 0 | steps.erase(iterPrev, iterPush); |
9761 | 0 | steps.insert(std::next(iterNext), |
9762 | 0 | std::move(stepVgridshift)); |
9763 | 0 | iterPrev = iterPush; |
9764 | 0 | iterCur = std::next(iterPush); |
9765 | 0 | continue; |
9766 | 0 | } |
9767 | 15.1k | } |
9768 | | |
9769 | 1.21M | ++iterCur; |
9770 | 1.21M | } |
9771 | 236k | } |
9772 | | |
9773 | 236k | { |
9774 | 236k | auto iterCur = steps.begin(); |
9775 | 236k | if (iterCur != steps.end()) { |
9776 | 225k | ++iterCur; |
9777 | 225k | } |
9778 | 1.44M | while (iterCur != steps.end()) { |
9779 | | |
9780 | 1.21M | assert(iterCur != steps.begin()); |
9781 | 1.21M | auto iterPrev = std::prev(iterCur); |
9782 | 1.21M | auto &prevStep = *iterPrev; |
9783 | 1.21M | auto &curStep = *iterCur; |
9784 | | |
9785 | 1.21M | const auto curStepParamCount = curStep.paramValues.size(); |
9786 | 1.21M | const auto prevStepParamCount = prevStep.paramValues.size(); |
9787 | | |
9788 | 1.21M | const auto deletePrevAndCurIter = [&steps, &iterPrev, &iterCur]() { |
9789 | 173 | iterCur = steps.erase(iterPrev, std::next(iterCur)); |
9790 | 173 | if (iterCur != steps.begin()) |
9791 | 173 | iterCur = std::prev(iterCur); |
9792 | 173 | if (iterCur == steps.begin() && iterCur != steps.end()) |
9793 | 37 | ++iterCur; |
9794 | 173 | }; |
9795 | | |
9796 | | // axisswap order=2,1 followed by itself is a no-op |
9797 | 1.21M | if (curStep.name == "axisswap" && prevStep.name == "axisswap" && |
9798 | 1.21M | curStepParamCount == 1 && prevStepParamCount == 1 && |
9799 | 1.21M | curStep.paramValues[0].equals("order", "2,1") && |
9800 | 1.21M | prevStep.paramValues[0].equals("order", "2,1")) { |
9801 | 2 | deletePrevAndCurIter(); |
9802 | 2 | continue; |
9803 | 2 | } |
9804 | | |
9805 | | // detect a step and its inverse |
9806 | 1.21M | if (curStep.inverted != prevStep.inverted && |
9807 | 1.21M | curStep.name == prevStep.name && |
9808 | 1.21M | curStepParamCount == prevStepParamCount) { |
9809 | 19.5k | bool allSame = true; |
9810 | 24.3k | for (size_t j = 0; j < curStepParamCount; j++) { |
9811 | 24.1k | if (curStep.paramValues[j] != prevStep.paramValues[j]) { |
9812 | 19.3k | allSame = false; |
9813 | 19.3k | break; |
9814 | 19.3k | } |
9815 | 24.1k | } |
9816 | 19.5k | if (allSame) { |
9817 | 171 | deletePrevAndCurIter(); |
9818 | 171 | continue; |
9819 | 171 | } |
9820 | 19.5k | } |
9821 | | |
9822 | 1.21M | ++iterCur; |
9823 | 1.21M | } |
9824 | 236k | } |
9825 | | |
9826 | 236k | if (steps.size() > 1 || |
9827 | 236k | (steps.size() == 1 && |
9828 | 39.7k | (steps.front().inverted || steps.front().hasKey("omit_inv") || |
9829 | 28.9k | steps.front().hasKey("omit_fwd") || |
9830 | 200k | !d->globalParamValues_.empty()))) { |
9831 | 200k | d->appendToResult("+proj=pipeline"); |
9832 | | |
9833 | 512k | for (const auto ¶mValue : d->globalParamValues_) { |
9834 | 512k | d->appendToResult("+"); |
9835 | 512k | d->result_ += paramValue.key; |
9836 | 512k | if (!paramValue.value.empty()) { |
9837 | 282k | d->result_ += '='; |
9838 | 282k | d->result_ += |
9839 | 282k | pj_double_quote_string_param_if_needed(paramValue.value); |
9840 | 282k | } |
9841 | 512k | } |
9842 | | |
9843 | 200k | if (d->multiLine_) { |
9844 | 0 | d->indentLevel_++; |
9845 | 0 | } |
9846 | 200k | } |
9847 | | |
9848 | 1.43M | for (const auto &step : steps) { |
9849 | 1.43M | std::string curLine; |
9850 | 1.43M | if (!d->result_.empty()) { |
9851 | 1.41M | if (d->multiLine_) { |
9852 | 0 | curLine = std::string(static_cast<size_t>(d->indentLevel_) * |
9853 | 0 | d->indentWidth_, |
9854 | 0 | ' '); |
9855 | 0 | curLine += "+step"; |
9856 | 1.41M | } else { |
9857 | 1.41M | curLine = " +step"; |
9858 | 1.41M | } |
9859 | 1.41M | } |
9860 | 1.43M | if (step.inverted) { |
9861 | 452k | curLine += " +inv"; |
9862 | 452k | } |
9863 | 1.43M | if (!step.name.empty()) { |
9864 | 1.41M | if (!curLine.empty()) |
9865 | 1.39M | curLine += ' '; |
9866 | 1.41M | curLine += step.isInit ? "+init=" : "+proj="; |
9867 | 1.41M | curLine += step.name; |
9868 | 1.41M | } |
9869 | 3.94M | for (const auto ¶mValue : step.paramValues) { |
9870 | 3.94M | std::string newKV = "+"; |
9871 | 3.94M | newKV += paramValue.key; |
9872 | 3.94M | if (!paramValue.value.empty()) { |
9873 | 2.70M | newKV += '='; |
9874 | 2.70M | newKV += |
9875 | 2.70M | pj_double_quote_string_param_if_needed(paramValue.value); |
9876 | 2.70M | } |
9877 | 3.94M | if (d->maxLineLength_ > 0 && d->multiLine_ && |
9878 | 3.94M | curLine.size() + newKV.size() > |
9879 | 0 | static_cast<size_t>(d->maxLineLength_)) { |
9880 | 0 | if (!d->result_.empty()) |
9881 | 0 | d->result_ += '\n'; |
9882 | 0 | d->result_ += curLine; |
9883 | 0 | curLine = std::string(static_cast<size_t>(d->indentLevel_) * |
9884 | 0 | d->indentWidth_ + |
9885 | 0 | strlen("+step "), |
9886 | 0 | ' '); |
9887 | 3.94M | } else { |
9888 | 3.94M | if (!curLine.empty()) |
9889 | 3.94M | curLine += ' '; |
9890 | 3.94M | } |
9891 | 3.94M | curLine += newKV; |
9892 | 3.94M | } |
9893 | 1.43M | if (d->multiLine_ && !d->result_.empty()) |
9894 | 0 | d->result_ += '\n'; |
9895 | 1.43M | d->result_ += curLine; |
9896 | 1.43M | } |
9897 | | |
9898 | 236k | if (d->result_.empty()) { |
9899 | 10.8k | d->appendToResult("+proj=noop"); |
9900 | 10.8k | } |
9901 | | |
9902 | 236k | return d->result_; |
9903 | 236k | } |
9904 | | |
9905 | | // --------------------------------------------------------------------------- |
9906 | | |
9907 | | //! @cond Doxygen_Suppress |
9908 | | |
9909 | 667k | PROJStringFormatter::Convention PROJStringFormatter::convention() const { |
9910 | 667k | return d->convention_; |
9911 | 667k | } |
9912 | | |
9913 | | // --------------------------------------------------------------------------- |
9914 | | |
9915 | | // Return the number of steps in the pipeline. |
9916 | | // Note: this value will change after calling toString() that will run |
9917 | | // optimizations. |
9918 | 33.8k | size_t PROJStringFormatter::getStepCount() const { return d->steps_.size(); } |
9919 | | |
9920 | | // --------------------------------------------------------------------------- |
9921 | | |
9922 | 8.50k | bool PROJStringFormatter::getUseApproxTMerc() const { |
9923 | 8.50k | return d->useApproxTMerc_; |
9924 | 8.50k | } |
9925 | | |
9926 | | // --------------------------------------------------------------------------- |
9927 | | |
9928 | 423k | void PROJStringFormatter::setCoordinateOperationOptimizations(bool enable) { |
9929 | 423k | d->coordOperationOptimizations_ = enable; |
9930 | 423k | } |
9931 | | |
9932 | | // --------------------------------------------------------------------------- |
9933 | | |
9934 | 723k | void PROJStringFormatter::Private::appendToResult(const char *str) { |
9935 | 723k | if (!result_.empty()) { |
9936 | 512k | result_ += ' '; |
9937 | 512k | } |
9938 | 723k | result_ += str; |
9939 | 723k | } |
9940 | | |
9941 | | // --------------------------------------------------------------------------- |
9942 | | |
9943 | | static void |
9944 | | PROJStringSyntaxParser(const std::string &projString, std::vector<Step> &steps, |
9945 | | std::vector<Step::KeyValue> &globalParamValues, |
9946 | 262k | std::string &title) { |
9947 | 262k | std::vector<std::string> tokens; |
9948 | | |
9949 | 262k | bool hasProj = false; |
9950 | 262k | bool hasInit = false; |
9951 | 262k | bool hasPipeline = false; |
9952 | | |
9953 | 262k | std::string projStringModified(projString); |
9954 | | |
9955 | | // Special case for "+title=several words +foo=bar" |
9956 | 262k | if (starts_with(projStringModified, "+title=") && |
9957 | 262k | projStringModified.size() > 7 && projStringModified[7] != '"') { |
9958 | 382 | const auto plusPos = projStringModified.find(" +", 1); |
9959 | 382 | const auto spacePos = projStringModified.find(' '); |
9960 | 382 | if (plusPos != std::string::npos && spacePos != std::string::npos && |
9961 | 382 | spacePos < plusPos) { |
9962 | 258 | std::string tmp("+title="); |
9963 | 258 | tmp += pj_double_quote_string_param_if_needed( |
9964 | 258 | projStringModified.substr(7, plusPos - 7)); |
9965 | 258 | tmp += projStringModified.substr(plusPos); |
9966 | 258 | projStringModified = std::move(tmp); |
9967 | 258 | } |
9968 | 382 | } |
9969 | | |
9970 | 262k | size_t argc = pj_trim_argc(&projStringModified[0]); |
9971 | 262k | char **argv = pj_trim_argv(argc, &projStringModified[0]); |
9972 | 7.40M | for (size_t i = 0; i < argc; i++) { |
9973 | 7.14M | std::string token(argv[i]); |
9974 | 7.14M | if (!hasPipeline && token == "proj=pipeline") { |
9975 | 207k | hasPipeline = true; |
9976 | 6.93M | } else if (!hasProj && starts_with(token, "proj=")) { |
9977 | 255k | hasProj = true; |
9978 | 6.67M | } else if (!hasInit && starts_with(token, "init=")) { |
9979 | 4.89k | hasInit = true; |
9980 | 4.89k | } |
9981 | 7.14M | tokens.emplace_back(token); |
9982 | 7.14M | } |
9983 | 262k | free(argv); |
9984 | | |
9985 | 262k | if (!hasPipeline) { |
9986 | 54.7k | if (hasProj || hasInit) { |
9987 | 48.3k | steps.push_back(Step()); |
9988 | 48.3k | } |
9989 | | |
9990 | 622k | for (auto &word : tokens) { |
9991 | 622k | if (starts_with(word, "proj=") && !hasInit && |
9992 | 622k | steps.back().name.empty()) { |
9993 | 47.7k | assert(hasProj); |
9994 | 47.7k | auto stepName = word.substr(strlen("proj=")); |
9995 | 47.7k | steps.back().name = std::move(stepName); |
9996 | 574k | } else if (starts_with(word, "init=")) { |
9997 | 790 | assert(hasInit); |
9998 | 790 | auto initName = word.substr(strlen("init=")); |
9999 | 790 | steps.back().name = std::move(initName); |
10000 | 790 | steps.back().isInit = true; |
10001 | 574k | } else if (word == "inv") { |
10002 | 3.73k | if (!steps.empty()) { |
10003 | 3.72k | steps.back().inverted = true; |
10004 | 3.72k | } |
10005 | 570k | } else if (starts_with(word, "title=")) { |
10006 | 928 | title = word.substr(strlen("title=")); |
10007 | 569k | } else if (word != "step") { |
10008 | 560k | const auto pos = word.find('='); |
10009 | 560k | const auto key = word.substr(0, pos); |
10010 | | |
10011 | 560k | Step::KeyValue pair( |
10012 | 560k | (pos != std::string::npos) |
10013 | 560k | ? Step::KeyValue(key, word.substr(pos + 1)) |
10014 | 560k | : Step::KeyValue(key)); |
10015 | 560k | if (steps.empty()) { |
10016 | 510 | globalParamValues.push_back(std::move(pair)); |
10017 | 559k | } else { |
10018 | 559k | steps.back().paramValues.push_back(std::move(pair)); |
10019 | 559k | } |
10020 | 560k | } |
10021 | 622k | } |
10022 | 54.7k | return; |
10023 | 54.7k | } |
10024 | | |
10025 | 207k | bool inPipeline = false; |
10026 | 207k | bool invGlobal = false; |
10027 | 6.51M | for (auto &word : tokens) { |
10028 | 6.51M | if (word == "proj=pipeline") { |
10029 | 208k | if (inPipeline) { |
10030 | 176 | throw ParsingException("nested pipeline not supported"); |
10031 | 176 | } |
10032 | 207k | inPipeline = true; |
10033 | 6.30M | } else if (word == "step") { |
10034 | 994k | if (!inPipeline) { |
10035 | 6 | throw ParsingException("+step found outside pipeline"); |
10036 | 6 | } |
10037 | 994k | steps.push_back(Step()); |
10038 | 5.31M | } else if (word == "inv") { |
10039 | 302k | if (steps.empty()) { |
10040 | 3.01k | invGlobal = true; |
10041 | 299k | } else { |
10042 | 299k | steps.back().inverted = true; |
10043 | 299k | } |
10044 | 5.01M | } else if (inPipeline && !steps.empty() && starts_with(word, "proj=") && |
10045 | 5.01M | steps.back().name.empty()) { |
10046 | 954k | auto stepName = word.substr(strlen("proj=")); |
10047 | 954k | steps.back().name = std::move(stepName); |
10048 | 4.05M | } else if (inPipeline && !steps.empty() && starts_with(word, "init=") && |
10049 | 4.05M | steps.back().name.empty()) { |
10050 | 1.86k | auto initName = word.substr(strlen("init=")); |
10051 | 1.86k | steps.back().name = std::move(initName); |
10052 | 1.86k | steps.back().isInit = true; |
10053 | 4.05M | } else if (!inPipeline && starts_with(word, "title=")) { |
10054 | 926 | title = word.substr(strlen("title=")); |
10055 | 4.05M | } else { |
10056 | 4.05M | const auto pos = word.find('='); |
10057 | 4.05M | auto key = word.substr(0, pos); |
10058 | 4.05M | Step::KeyValue pair((pos != std::string::npos) |
10059 | 4.05M | ? Step::KeyValue(key, word.substr(pos + 1)) |
10060 | 4.05M | : Step::KeyValue(key)); |
10061 | 4.05M | if (steps.empty()) { |
10062 | 848k | globalParamValues.emplace_back(std::move(pair)); |
10063 | 3.20M | } else { |
10064 | 3.20M | steps.back().paramValues.emplace_back(std::move(pair)); |
10065 | 3.20M | } |
10066 | 4.05M | } |
10067 | 6.51M | } |
10068 | 207k | if (invGlobal) { |
10069 | 32.6k | for (auto &step : steps) { |
10070 | 32.6k | step.inverted = !step.inverted; |
10071 | 32.6k | } |
10072 | 2.84k | std::reverse(steps.begin(), steps.end()); |
10073 | 2.84k | } |
10074 | 207k | } |
10075 | | |
10076 | | // --------------------------------------------------------------------------- |
10077 | | |
10078 | | void PROJStringFormatter::ingestPROJString( |
10079 | | const std::string &str) // throw ParsingException |
10080 | 250k | { |
10081 | 250k | std::vector<Step> steps; |
10082 | 250k | std::string title; |
10083 | 250k | PROJStringSyntaxParser(str, steps, d->globalParamValues_, title); |
10084 | 250k | d->steps_.insert(d->steps_.end(), steps.begin(), steps.end()); |
10085 | 250k | } |
10086 | | |
10087 | | // --------------------------------------------------------------------------- |
10088 | | |
10089 | 11.3k | void PROJStringFormatter::setCRSExport(bool b) { d->crsExport_ = b; } |
10090 | | |
10091 | | // --------------------------------------------------------------------------- |
10092 | | |
10093 | 2.05M | bool PROJStringFormatter::getCRSExport() const { return d->crsExport_; } |
10094 | | |
10095 | | // --------------------------------------------------------------------------- |
10096 | | |
10097 | 690k | void PROJStringFormatter::startInversion() { |
10098 | 690k | PROJStringFormatter::Private::InversionStackElt elt; |
10099 | 690k | elt.startIter = d->steps_.end(); |
10100 | 690k | if (elt.startIter != d->steps_.begin()) { |
10101 | 480k | elt.iterValid = true; |
10102 | 480k | --elt.startIter; // point to the last valid element |
10103 | 480k | } else { |
10104 | 209k | elt.iterValid = false; |
10105 | 209k | } |
10106 | 690k | elt.currentInversionState = |
10107 | 690k | !d->inversionStack_.back().currentInversionState; |
10108 | 690k | d->inversionStack_.push_back(elt); |
10109 | 690k | } |
10110 | | |
10111 | | // --------------------------------------------------------------------------- |
10112 | | |
10113 | 690k | void PROJStringFormatter::stopInversion() { |
10114 | 690k | assert(!d->inversionStack_.empty()); |
10115 | 690k | auto startIter = d->inversionStack_.back().startIter; |
10116 | 690k | if (!d->inversionStack_.back().iterValid) { |
10117 | 209k | startIter = d->steps_.begin(); |
10118 | 480k | } else { |
10119 | 480k | ++startIter; // advance after the last valid element we marked above |
10120 | 480k | } |
10121 | | // Invert the inversion status of the steps between the start point and |
10122 | | // the current end of steps |
10123 | 3.12M | for (auto iter = startIter; iter != d->steps_.end(); ++iter) { |
10124 | 2.43M | iter->inverted = !iter->inverted; |
10125 | 4.52M | for (auto ¶mValue : iter->paramValues) { |
10126 | 4.52M | if (paramValue.key == "omit_fwd") |
10127 | 213 | paramValue.key = "omit_inv"; |
10128 | 4.52M | else if (paramValue.key == "omit_inv") |
10129 | 251 | paramValue.key = "omit_fwd"; |
10130 | 4.52M | } |
10131 | 2.43M | } |
10132 | | // And reverse the order of steps in that range as well. |
10133 | 690k | std::reverse(startIter, d->steps_.end()); |
10134 | 690k | d->inversionStack_.pop_back(); |
10135 | 690k | } |
10136 | | |
10137 | | // --------------------------------------------------------------------------- |
10138 | | |
10139 | 0 | bool PROJStringFormatter::isInverted() const { |
10140 | 0 | return d->inversionStack_.back().currentInversionState; |
10141 | 0 | } |
10142 | | |
10143 | | // --------------------------------------------------------------------------- |
10144 | | |
10145 | 3.06M | void PROJStringFormatter::Private::addStep() { steps_.emplace_back(Step()); } |
10146 | | |
10147 | | // --------------------------------------------------------------------------- |
10148 | | |
10149 | 3.03M | void PROJStringFormatter::addStep(const char *stepName) { |
10150 | 3.03M | d->addStep(); |
10151 | 3.03M | d->steps_.back().name.assign(stepName); |
10152 | 3.03M | } |
10153 | | |
10154 | | // --------------------------------------------------------------------------- |
10155 | | |
10156 | 30.4k | void PROJStringFormatter::addStep(const std::string &stepName) { |
10157 | 30.4k | d->addStep(); |
10158 | 30.4k | d->steps_.back().name = stepName; |
10159 | 30.4k | } |
10160 | | |
10161 | | // --------------------------------------------------------------------------- |
10162 | | |
10163 | 169k | void PROJStringFormatter::setCurrentStepInverted(bool inverted) { |
10164 | 169k | assert(!d->steps_.empty()); |
10165 | 169k | d->steps_.back().inverted = inverted; |
10166 | 169k | } |
10167 | | |
10168 | | // --------------------------------------------------------------------------- |
10169 | | |
10170 | 0 | bool PROJStringFormatter::hasParam(const char *paramName) const { |
10171 | 0 | if (!d->steps_.empty()) { |
10172 | 0 | for (const auto ¶mValue : d->steps_.back().paramValues) { |
10173 | 0 | if (paramValue.keyEquals(paramName)) { |
10174 | 0 | return true; |
10175 | 0 | } |
10176 | 0 | } |
10177 | 0 | } |
10178 | 0 | return false; |
10179 | 0 | } |
10180 | | |
10181 | | // --------------------------------------------------------------------------- |
10182 | | |
10183 | 19.4k | void PROJStringFormatter::addNoDefs(bool b) { d->addNoDefs_ = b; } |
10184 | | |
10185 | | // --------------------------------------------------------------------------- |
10186 | | |
10187 | 109k | bool PROJStringFormatter::getAddNoDefs() const { return d->addNoDefs_; } |
10188 | | |
10189 | | // --------------------------------------------------------------------------- |
10190 | | |
10191 | 282k | void PROJStringFormatter::addParam(const std::string ¶mName) { |
10192 | 282k | if (d->steps_.empty()) { |
10193 | 0 | d->addStep(); |
10194 | 0 | } |
10195 | 282k | d->steps_.back().paramValues.push_back(Step::KeyValue(paramName)); |
10196 | 282k | } |
10197 | | |
10198 | | // --------------------------------------------------------------------------- |
10199 | | |
10200 | 826 | void PROJStringFormatter::addParam(const char *paramName, int val) { |
10201 | 826 | addParam(std::string(paramName), val); |
10202 | 826 | } |
10203 | | |
10204 | 826 | void PROJStringFormatter::addParam(const std::string ¶mName, int val) { |
10205 | 826 | addParam(paramName, internal::toString(val)); |
10206 | 826 | } |
10207 | | |
10208 | | // --------------------------------------------------------------------------- |
10209 | | |
10210 | 981k | static std::string formatToString(double val) { |
10211 | 981k | if (std::abs(val * 10 - std::round(val * 10)) < 1e-8) { |
10212 | | // For the purpose of |
10213 | | // https://www.epsg-registry.org/export.htm?wkt=urn:ogc:def:crs:EPSG::27561 |
10214 | | // Latitude of natural of origin to be properly rounded from 55 grad |
10215 | | // to |
10216 | | // 49.5 deg |
10217 | 809k | val = std::round(val * 10) / 10; |
10218 | 809k | } |
10219 | 981k | return normalizeSerializedString(internal::toString(val)); |
10220 | 981k | } |
10221 | | |
10222 | | // --------------------------------------------------------------------------- |
10223 | | |
10224 | 973k | void PROJStringFormatter::addParam(const char *paramName, double val) { |
10225 | 973k | addParam(std::string(paramName), val); |
10226 | 973k | } |
10227 | | |
10228 | 981k | void PROJStringFormatter::addParam(const std::string ¶mName, double val) { |
10229 | 981k | addParam(paramName, formatToString(val)); |
10230 | 981k | } |
10231 | | |
10232 | | // --------------------------------------------------------------------------- |
10233 | | |
10234 | | void PROJStringFormatter::addParam(const char *paramName, |
10235 | 25 | const std::vector<double> &vals) { |
10236 | 25 | std::string paramValue; |
10237 | 200 | for (size_t i = 0; i < vals.size(); ++i) { |
10238 | 175 | if (i > 0) { |
10239 | 150 | paramValue += ','; |
10240 | 150 | } |
10241 | 175 | paramValue += formatToString(vals[i]); |
10242 | 175 | } |
10243 | 25 | addParam(paramName, paramValue); |
10244 | 25 | } |
10245 | | |
10246 | | // --------------------------------------------------------------------------- |
10247 | | |
10248 | 1.83M | void PROJStringFormatter::addParam(const char *paramName, const char *val) { |
10249 | 1.83M | addParam(std::string(paramName), val); |
10250 | 1.83M | } |
10251 | | |
10252 | | void PROJStringFormatter::addParam(const char *paramName, |
10253 | 2.00M | const std::string &val) { |
10254 | 2.00M | addParam(std::string(paramName), val); |
10255 | 2.00M | } |
10256 | | |
10257 | | void PROJStringFormatter::addParam(const std::string ¶mName, |
10258 | 1.83M | const char *val) { |
10259 | 1.83M | addParam(paramName, std::string(val)); |
10260 | 1.83M | } |
10261 | | |
10262 | | // --------------------------------------------------------------------------- |
10263 | | |
10264 | | void PROJStringFormatter::addParam(const std::string ¶mName, |
10265 | 4.86M | const std::string &val) { |
10266 | 4.86M | if (d->steps_.empty()) { |
10267 | 461 | d->addStep(); |
10268 | 461 | } |
10269 | 4.86M | d->steps_.back().paramValues.push_back(Step::KeyValue(paramName, val)); |
10270 | 4.86M | } |
10271 | | |
10272 | | // --------------------------------------------------------------------------- |
10273 | | |
10274 | | void PROJStringFormatter::setTOWGS84Parameters( |
10275 | 432 | const std::vector<double> ¶ms) { |
10276 | 432 | d->toWGS84Parameters_ = params; |
10277 | 432 | } |
10278 | | |
10279 | | // --------------------------------------------------------------------------- |
10280 | | |
10281 | 486k | const std::vector<double> &PROJStringFormatter::getTOWGS84Parameters() const { |
10282 | 486k | return d->toWGS84Parameters_; |
10283 | 486k | } |
10284 | | |
10285 | | // --------------------------------------------------------------------------- |
10286 | | |
10287 | 31.3k | std::set<std::string> PROJStringFormatter::getUsedGridNames() const { |
10288 | 31.3k | std::set<std::string> res; |
10289 | 185k | for (const auto &step : d->steps_) { |
10290 | 459k | for (const auto ¶m : step.paramValues) { |
10291 | 459k | if (param.keyEquals("grids") || param.keyEquals("file")) { |
10292 | 22.2k | const auto gridNames = split(param.value, ","); |
10293 | 59.5k | for (const auto &gridName : gridNames) { |
10294 | 59.5k | res.insert(gridName); |
10295 | 59.5k | } |
10296 | 22.2k | } |
10297 | 459k | } |
10298 | 185k | } |
10299 | 31.3k | return res; |
10300 | 31.3k | } |
10301 | | |
10302 | | // --------------------------------------------------------------------------- |
10303 | | |
10304 | 128k | bool PROJStringFormatter::requiresPerCoordinateInputTime() const { |
10305 | 290k | for (const auto &step : d->steps_) { |
10306 | 290k | if (step.name == "set" && !step.inverted) { |
10307 | 23.2k | for (const auto ¶m : step.paramValues) { |
10308 | 23.2k | if (param.keyEquals("v_4")) { |
10309 | 0 | return false; |
10310 | 0 | } |
10311 | 23.2k | } |
10312 | 287k | } else if (step.name == "helmert") { |
10313 | 14.4k | for (const auto ¶m : step.paramValues) { |
10314 | 14.4k | if (param.keyEquals("t_epoch")) { |
10315 | 28 | return true; |
10316 | 28 | } |
10317 | 14.4k | } |
10318 | 284k | } else if (step.name == "deformation") { |
10319 | 16.9k | for (const auto ¶m : step.paramValues) { |
10320 | 16.9k | if (param.keyEquals("t_epoch")) { |
10321 | 3.69k | return true; |
10322 | 3.69k | } |
10323 | 16.9k | } |
10324 | 276k | } else if (step.name == "defmodel") { |
10325 | 1 | return true; |
10326 | 1 | } |
10327 | 290k | } |
10328 | 124k | return false; |
10329 | 128k | } |
10330 | | |
10331 | | // --------------------------------------------------------------------------- |
10332 | | |
10333 | | void PROJStringFormatter::setVDatumExtension(const std::string &filename, |
10334 | 688 | const std::string &geoidCRSValue) { |
10335 | 688 | d->vDatumExtension_ = filename; |
10336 | 688 | d->geoidCRSValue_ = geoidCRSValue; |
10337 | 688 | } |
10338 | | |
10339 | | // --------------------------------------------------------------------------- |
10340 | | |
10341 | 630 | const std::string &PROJStringFormatter::getVDatumExtension() const { |
10342 | 630 | return d->vDatumExtension_; |
10343 | 630 | } |
10344 | | |
10345 | | // --------------------------------------------------------------------------- |
10346 | | |
10347 | 630 | const std::string &PROJStringFormatter::getGeoidCRSValue() const { |
10348 | 630 | return d->geoidCRSValue_; |
10349 | 630 | } |
10350 | | |
10351 | | // --------------------------------------------------------------------------- |
10352 | | |
10353 | 367 | void PROJStringFormatter::setHDatumExtension(const std::string &filename) { |
10354 | 367 | d->hDatumExtension_ = filename; |
10355 | 367 | } |
10356 | | |
10357 | | // --------------------------------------------------------------------------- |
10358 | | |
10359 | 486k | const std::string &PROJStringFormatter::getHDatumExtension() const { |
10360 | 486k | return d->hDatumExtension_; |
10361 | 486k | } |
10362 | | |
10363 | | // --------------------------------------------------------------------------- |
10364 | | |
10365 | | void PROJStringFormatter::setGeogCRSOfCompoundCRS( |
10366 | 331 | const crs::GeographicCRSPtr &crs) { |
10367 | 331 | d->geogCRSOfCompoundCRS_ = crs; |
10368 | 331 | } |
10369 | | |
10370 | | // --------------------------------------------------------------------------- |
10371 | | |
10372 | | const crs::GeographicCRSPtr & |
10373 | 879 | PROJStringFormatter::getGeogCRSOfCompoundCRS() const { |
10374 | 879 | return d->geogCRSOfCompoundCRS_; |
10375 | 879 | } |
10376 | | |
10377 | | // --------------------------------------------------------------------------- |
10378 | | |
10379 | 172k | void PROJStringFormatter::setOmitProjLongLatIfPossible(bool omit) { |
10380 | 172k | assert(d->omitProjLongLatIfPossible_ ^ omit); |
10381 | 172k | d->omitProjLongLatIfPossible_ = omit; |
10382 | 172k | } |
10383 | | |
10384 | | // --------------------------------------------------------------------------- |
10385 | | |
10386 | 450k | bool PROJStringFormatter::omitProjLongLatIfPossible() const { |
10387 | 450k | return d->omitProjLongLatIfPossible_; |
10388 | 450k | } |
10389 | | |
10390 | | // --------------------------------------------------------------------------- |
10391 | | |
10392 | 298k | void PROJStringFormatter::pushOmitZUnitConversion() { |
10393 | 298k | d->omitZUnitConversion_.push_back(true); |
10394 | 298k | } |
10395 | | |
10396 | | // --------------------------------------------------------------------------- |
10397 | | |
10398 | 298k | void PROJStringFormatter::popOmitZUnitConversion() { |
10399 | 298k | assert(d->omitZUnitConversion_.size() > 1); |
10400 | 298k | d->omitZUnitConversion_.pop_back(); |
10401 | 298k | } |
10402 | | |
10403 | | // --------------------------------------------------------------------------- |
10404 | | |
10405 | 655k | bool PROJStringFormatter::omitZUnitConversion() const { |
10406 | 655k | return d->omitZUnitConversion_.back(); |
10407 | 655k | } |
10408 | | |
10409 | | // --------------------------------------------------------------------------- |
10410 | | |
10411 | 119k | void PROJStringFormatter::pushOmitHorizontalConversionInVertTransformation() { |
10412 | 119k | d->omitHorizontalConversionInVertTransformation_.push_back(true); |
10413 | 119k | } |
10414 | | |
10415 | | // --------------------------------------------------------------------------- |
10416 | | |
10417 | 119k | void PROJStringFormatter::popOmitHorizontalConversionInVertTransformation() { |
10418 | 119k | assert(d->omitHorizontalConversionInVertTransformation_.size() > 1); |
10419 | 119k | d->omitHorizontalConversionInVertTransformation_.pop_back(); |
10420 | 119k | } |
10421 | | |
10422 | | // --------------------------------------------------------------------------- |
10423 | | |
10424 | 268k | bool PROJStringFormatter::omitHorizontalConversionInVertTransformation() const { |
10425 | 268k | return d->omitHorizontalConversionInVertTransformation_.back(); |
10426 | 268k | } |
10427 | | |
10428 | | // --------------------------------------------------------------------------- |
10429 | | |
10430 | 11.3k | void PROJStringFormatter::setLegacyCRSToCRSContext(bool legacyContext) { |
10431 | 11.3k | d->legacyCRSToCRSContext_ = legacyContext; |
10432 | 11.3k | } |
10433 | | |
10434 | | // --------------------------------------------------------------------------- |
10435 | | |
10436 | 386k | bool PROJStringFormatter::getLegacyCRSToCRSContext() const { |
10437 | 386k | return d->legacyCRSToCRSContext_; |
10438 | 386k | } |
10439 | | |
10440 | | // --------------------------------------------------------------------------- |
10441 | | |
10442 | | /** Asks for a "normalized" output during toString(), aimed at comparing two |
10443 | | * strings for equivalence. |
10444 | | * |
10445 | | * This consists for now in sorting the +key=value option in lexicographic |
10446 | | * order. |
10447 | | */ |
10448 | 12.4k | PROJStringFormatter &PROJStringFormatter::setNormalizeOutput() { |
10449 | 12.4k | d->normalizeOutput_ = true; |
10450 | 12.4k | return *this; |
10451 | 12.4k | } |
10452 | | |
10453 | | // --------------------------------------------------------------------------- |
10454 | | |
10455 | 584k | const DatabaseContextPtr &PROJStringFormatter::databaseContext() const { |
10456 | 584k | return d->dbContext_; |
10457 | 584k | } |
10458 | | |
10459 | | //! @endcond |
10460 | | |
10461 | | // --------------------------------------------------------------------------- |
10462 | | |
10463 | | //! @cond Doxygen_Suppress |
10464 | | |
10465 | | struct PROJStringParser::Private { |
10466 | | DatabaseContextPtr dbContext_{}; |
10467 | | PJ_CONTEXT *ctx_{}; |
10468 | | bool usePROJ4InitRules_ = false; |
10469 | | std::vector<std::string> warningList_{}; |
10470 | | |
10471 | | std::string projString_{}; |
10472 | | |
10473 | | std::vector<Step> steps_{}; |
10474 | | std::vector<Step::KeyValue> globalParamValues_{}; |
10475 | | std::string title_{}; |
10476 | | |
10477 | | bool ignoreNadgrids_ = false; |
10478 | | |
10479 | | template <class T> |
10480 | | // cppcheck-suppress functionStatic |
10481 | 75.8k | bool hasParamValue(Step &step, const T key) { |
10482 | 148k | for (auto &pair : globalParamValues_) { |
10483 | 148k | if (ci_equal(pair.key, key)) { |
10484 | 1.44k | pair.usedByParser = true; |
10485 | 1.44k | return true; |
10486 | 1.44k | } |
10487 | 148k | } |
10488 | 164k | for (auto &pair : step.paramValues) { |
10489 | 164k | if (ci_equal(pair.key, key)) { |
10490 | 6.99k | pair.usedByParser = true; |
10491 | 6.99k | return true; |
10492 | 6.99k | } |
10493 | 164k | } |
10494 | 67.3k | return false; |
10495 | 74.3k | } |
10496 | | |
10497 | | template <class T> |
10498 | | // cppcheck-suppress functionStatic |
10499 | 1.89k | const std::string &getGlobalParamValue(T key) { |
10500 | 12.1k | for (auto &pair : globalParamValues_) { |
10501 | 12.1k | if (ci_equal(pair.key, key)) { |
10502 | 1.12k | pair.usedByParser = true; |
10503 | 1.12k | return pair.value; |
10504 | 1.12k | } |
10505 | 12.1k | } |
10506 | 778 | return emptyString; |
10507 | 1.89k | } |
10508 | | |
10509 | | template <class T> |
10510 | | // cppcheck-suppress functionStatic |
10511 | 371k | const std::string &getParamValue(Step &step, const T key) { |
10512 | 571k | for (auto &pair : globalParamValues_) { |
10513 | 571k | if (ci_equal(pair.key, key)) { |
10514 | 6.90k | pair.usedByParser = true; |
10515 | 6.90k | return pair.value; |
10516 | 6.90k | } |
10517 | 571k | } |
10518 | 772k | for (auto &pair : step.paramValues) { |
10519 | 772k | if (ci_equal(pair.key, key)) { |
10520 | 33.3k | pair.usedByParser = true; |
10521 | 33.3k | return pair.value; |
10522 | 33.3k | } |
10523 | 772k | } |
10524 | 331k | return emptyString; |
10525 | 364k | } std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const& osgeo::proj::io::PROJStringParser::Private::getParamValue<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(osgeo::proj::io::Step&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) Line | Count | Source | 10511 | 78.9k | const std::string &getParamValue(Step &step, const T key) { | 10512 | 133k | for (auto &pair : globalParamValues_) { | 10513 | 133k | if (ci_equal(pair.key, key)) { | 10514 | 307 | pair.usedByParser = true; | 10515 | 307 | return pair.value; | 10516 | 307 | } | 10517 | 133k | } | 10518 | 182k | for (auto &pair : step.paramValues) { | 10519 | 182k | if (ci_equal(pair.key, key)) { | 10520 | 3.13k | pair.usedByParser = true; | 10521 | 3.13k | return pair.value; | 10522 | 3.13k | } | 10523 | 182k | } | 10524 | 75.4k | return emptyString; | 10525 | 78.5k | } |
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const& osgeo::proj::io::PROJStringParser::Private::getParamValue<char const*>(osgeo::proj::io::Step&, char const*) Line | Count | Source | 10511 | 292k | const std::string &getParamValue(Step &step, const T key) { | 10512 | 438k | for (auto &pair : globalParamValues_) { | 10513 | 438k | if (ci_equal(pair.key, key)) { | 10514 | 6.60k | pair.usedByParser = true; | 10515 | 6.60k | return pair.value; | 10516 | 6.60k | } | 10517 | 438k | } | 10518 | 589k | for (auto &pair : step.paramValues) { | 10519 | 589k | if (ci_equal(pair.key, key)) { | 10520 | 30.2k | pair.usedByParser = true; | 10521 | 30.2k | return pair.value; | 10522 | 30.2k | } | 10523 | 589k | } | 10524 | 255k | return emptyString; | 10525 | 286k | } |
|
10526 | | |
10527 | 1.83k | static const std::string &getParamValueK(Step &step) { |
10528 | 3.78k | for (auto &pair : step.paramValues) { |
10529 | 3.78k | if (ci_equal(pair.key, "k") || ci_equal(pair.key, "k_0")) { |
10530 | 40 | pair.usedByParser = true; |
10531 | 40 | return pair.value; |
10532 | 40 | } |
10533 | 3.78k | } |
10534 | 1.79k | return emptyString; |
10535 | 1.83k | } |
10536 | | |
10537 | | // cppcheck-suppress functionStatic |
10538 | 15.5k | bool hasUnusedParameters(const Step &step) const { |
10539 | 15.5k | if (steps_.size() == 1) { |
10540 | 13.2k | for (const auto &pair : step.paramValues) { |
10541 | 13.2k | if (pair.key != "no_defs" && !pair.usedByParser) { |
10542 | 4.34k | return true; |
10543 | 4.34k | } |
10544 | 13.2k | } |
10545 | 13.1k | } |
10546 | 11.1k | return false; |
10547 | 15.5k | } |
10548 | | |
10549 | | // cppcheck-suppress functionStatic |
10550 | | std::string guessBodyName(double a); |
10551 | | |
10552 | | PrimeMeridianNNPtr buildPrimeMeridian(Step &step); |
10553 | | GeodeticReferenceFrameNNPtr buildDatum(Step &step, |
10554 | | const std::string &title); |
10555 | | GeodeticCRSNNPtr buildGeodeticCRS(int iStep, int iUnitConvert, |
10556 | | int iAxisSwap, bool ignorePROJAxis); |
10557 | | GeodeticCRSNNPtr buildGeocentricCRS(int iStep, int iUnitConvert); |
10558 | | CRSNNPtr buildProjectedCRS(int iStep, const GeodeticCRSNNPtr &geogCRS, |
10559 | | int iUnitConvert, int iAxisSwap); |
10560 | | CRSNNPtr buildBoundOrCompoundCRSIfNeeded(int iStep, CRSNNPtr crs); |
10561 | | UnitOfMeasure buildUnit(Step &step, const std::string &unitsParamName, |
10562 | | const std::string &toMeterParamName); |
10563 | | |
10564 | | enum class AxisType { REGULAR, NORTH_POLE, SOUTH_POLE }; |
10565 | | |
10566 | | std::vector<CoordinateSystemAxisNNPtr> |
10567 | | processAxisSwap(Step &step, const UnitOfMeasure &unit, int iAxisSwap, |
10568 | | AxisType axisType, bool ignorePROJAxis); |
10569 | | |
10570 | | EllipsoidalCSNNPtr buildEllipsoidalCS(int iStep, int iUnitConvert, |
10571 | | int iAxisSwap, bool ignorePROJAxis); |
10572 | | |
10573 | | SphericalCSNNPtr buildSphericalCS(int iStep, int iUnitConvert, |
10574 | | int iAxisSwap, bool ignorePROJAxis); |
10575 | | }; |
10576 | | |
10577 | | //! @endcond |
10578 | | |
10579 | | // --------------------------------------------------------------------------- |
10580 | | |
10581 | 11.3k | PROJStringParser::PROJStringParser() : d(std::make_unique<Private>()) {} |
10582 | | |
10583 | | // --------------------------------------------------------------------------- |
10584 | | |
10585 | | //! @cond Doxygen_Suppress |
10586 | 11.3k | PROJStringParser::~PROJStringParser() = default; |
10587 | | //! @endcond |
10588 | | |
10589 | | // --------------------------------------------------------------------------- |
10590 | | |
10591 | | /** \brief Attach a database context, to allow queries in it if needed. |
10592 | | */ |
10593 | | PROJStringParser & |
10594 | 11.2k | PROJStringParser::attachDatabaseContext(const DatabaseContextPtr &dbContext) { |
10595 | 11.2k | d->dbContext_ = dbContext; |
10596 | 11.2k | return *this; |
10597 | 11.2k | } |
10598 | | |
10599 | | // --------------------------------------------------------------------------- |
10600 | | |
10601 | | //! @cond Doxygen_Suppress |
10602 | 11.2k | PROJStringParser &PROJStringParser::attachContext(PJ_CONTEXT *ctx) { |
10603 | 11.2k | d->ctx_ = ctx; |
10604 | 11.2k | return *this; |
10605 | 11.2k | } |
10606 | | //! @endcond |
10607 | | |
10608 | | // --------------------------------------------------------------------------- |
10609 | | |
10610 | | /** \brief Set how init=epsg:XXXX syntax should be interpreted. |
10611 | | * |
10612 | | * @param enable When set to true, |
10613 | | * init=epsg:XXXX syntax will be allowed and will be interpreted according to |
10614 | | * PROJ.4 and PROJ.5 rules, that is geodeticCRS will have longitude, latitude |
10615 | | * order and will expect/output coordinates in radians. ProjectedCRS will have |
10616 | | * easting, northing axis order (except the ones with Transverse Mercator South |
10617 | | * Orientated projection). |
10618 | | */ |
10619 | 11.2k | PROJStringParser &PROJStringParser::setUsePROJ4InitRules(bool enable) { |
10620 | 11.2k | d->usePROJ4InitRules_ = enable; |
10621 | 11.2k | return *this; |
10622 | 11.2k | } |
10623 | | |
10624 | | // --------------------------------------------------------------------------- |
10625 | | |
10626 | | /** \brief Return the list of warnings found during parsing. |
10627 | | */ |
10628 | 0 | std::vector<std::string> PROJStringParser::warningList() const { |
10629 | 0 | return d->warningList_; |
10630 | 0 | } |
10631 | | |
10632 | | // --------------------------------------------------------------------------- |
10633 | | |
10634 | | //! @cond Doxygen_Suppress |
10635 | | |
10636 | | // --------------------------------------------------------------------------- |
10637 | | |
10638 | | static const struct LinearUnitDesc { |
10639 | | const char *projName; |
10640 | | const char *convToMeter; |
10641 | | const char *name; |
10642 | | int epsgCode; |
10643 | | } linearUnitDescs[] = { |
10644 | | {"mm", "0.001", "millimetre", 1025}, |
10645 | | {"cm", "0.01", "centimetre", 1033}, |
10646 | | {"m", "1.0", "metre", 9001}, |
10647 | | {"meter", "1.0", "metre", 9001}, // alternative |
10648 | | {"metre", "1.0", "metre", 9001}, // alternative |
10649 | | {"ft", "0.3048", "foot", 9002}, |
10650 | | {"us-ft", "0.3048006096012192", "US survey foot", 9003}, |
10651 | | {"fath", "1.8288", "fathom", 9014}, |
10652 | | {"kmi", "1852", "nautical mile", 9030}, |
10653 | | {"us-ch", "20.11684023368047", "US survey chain", 9033}, |
10654 | | {"us-mi", "1609.347218694437", "US survey mile", 9035}, |
10655 | | {"km", "1000.0", "kilometre", 9036}, |
10656 | | {"ind-ft", "0.30479841", "Indian foot (1937)", 9081}, |
10657 | | {"ind-yd", "0.91439523", "Indian yard (1937)", 9085}, |
10658 | | {"mi", "1609.344", "Statute mile", 9093}, |
10659 | | {"yd", "0.9144", "yard", 9096}, |
10660 | | {"ch", "20.1168", "chain", 9097}, |
10661 | | {"link", "0.201168", "link", 9098}, |
10662 | | {"dm", "0.1", "decimetre", 0}, // no EPSG equivalent |
10663 | | {"in", "0.0254", "inch", 0}, // no EPSG equivalent |
10664 | | {"us-in", "0.025400050800101", "US survey inch", 0}, // no EPSG equivalent |
10665 | | {"us-yd", "0.914401828803658", "US survey yard", 0}, // no EPSG equivalent |
10666 | | {"ind-ch", "20.11669506", "Indian chain", 0}, // no EPSG equivalent |
10667 | | }; |
10668 | | |
10669 | 2.52k | static const LinearUnitDesc *getLinearUnits(const std::string &projName) { |
10670 | 12.8k | for (const auto &desc : linearUnitDescs) { |
10671 | 12.8k | if (desc.projName == projName) |
10672 | 2.48k | return &desc; |
10673 | 12.8k | } |
10674 | 36 | return nullptr; |
10675 | 2.52k | } |
10676 | | |
10677 | 254 | static const LinearUnitDesc *getLinearUnits(double toMeter) { |
10678 | 5.76k | for (const auto &desc : linearUnitDescs) { |
10679 | 5.76k | if (std::fabs(c_locale_stod(desc.convToMeter) - toMeter) < |
10680 | 5.76k | 1e-10 * toMeter) { |
10681 | 4 | return &desc; |
10682 | 4 | } |
10683 | 5.76k | } |
10684 | 250 | return nullptr; |
10685 | 254 | } |
10686 | | |
10687 | | // --------------------------------------------------------------------------- |
10688 | | |
10689 | 2.48k | static UnitOfMeasure _buildUnit(const LinearUnitDesc *unitsMatch) { |
10690 | 2.48k | std::string unitsCode; |
10691 | 2.48k | if (unitsMatch->epsgCode) { |
10692 | 2.47k | std::ostringstream buffer; |
10693 | 2.47k | buffer.imbue(std::locale::classic()); |
10694 | 2.47k | buffer << unitsMatch->epsgCode; |
10695 | 2.47k | unitsCode = buffer.str(); |
10696 | 2.47k | } |
10697 | 2.48k | return UnitOfMeasure( |
10698 | 2.48k | unitsMatch->name, c_locale_stod(unitsMatch->convToMeter), |
10699 | 2.48k | UnitOfMeasure::Type::LINEAR, |
10700 | 2.48k | unitsMatch->epsgCode ? Identifier::EPSG : std::string(), unitsCode); |
10701 | 2.48k | } |
10702 | | |
10703 | | // --------------------------------------------------------------------------- |
10704 | | |
10705 | 250 | static UnitOfMeasure _buildUnit(double to_meter_value) { |
10706 | | // TODO: look-up in EPSG catalog |
10707 | 250 | if (to_meter_value == 0) { |
10708 | 0 | throw ParsingException("invalid unit value"); |
10709 | 0 | } |
10710 | 250 | return UnitOfMeasure("unknown", to_meter_value, |
10711 | 250 | UnitOfMeasure::Type::LINEAR); |
10712 | 250 | } |
10713 | | |
10714 | | // --------------------------------------------------------------------------- |
10715 | | |
10716 | | UnitOfMeasure |
10717 | | PROJStringParser::Private::buildUnit(Step &step, |
10718 | | const std::string &unitsParamName, |
10719 | 31.0k | const std::string &toMeterParamName) { |
10720 | 31.0k | UnitOfMeasure unit = UnitOfMeasure::METRE; |
10721 | 31.0k | const LinearUnitDesc *unitsMatch = nullptr; |
10722 | 31.0k | const auto &projUnits = getParamValue(step, unitsParamName); |
10723 | 31.0k | if (!projUnits.empty()) { |
10724 | 2.51k | unitsMatch = getLinearUnits(projUnits); |
10725 | 2.51k | if (unitsMatch == nullptr) { |
10726 | 30 | throw ParsingException("unhandled " + unitsParamName + "=" + |
10727 | 30 | projUnits); |
10728 | 30 | } |
10729 | 2.51k | } |
10730 | | |
10731 | 31.0k | const auto &toMeter = getParamValue(step, toMeterParamName); |
10732 | 31.0k | if (!toMeter.empty()) { |
10733 | 265 | double to_meter_value; |
10734 | 265 | try { |
10735 | 265 | to_meter_value = c_locale_stod(toMeter); |
10736 | 265 | } catch (const std::invalid_argument &) { |
10737 | 11 | throw ParsingException("invalid value for " + toMeterParamName); |
10738 | 11 | } |
10739 | 254 | unitsMatch = getLinearUnits(to_meter_value); |
10740 | 254 | if (unitsMatch == nullptr) { |
10741 | 250 | unit = _buildUnit(to_meter_value); |
10742 | 250 | } |
10743 | 254 | } |
10744 | | |
10745 | 31.0k | if (unitsMatch) { |
10746 | 2.48k | unit = _buildUnit(unitsMatch); |
10747 | 2.48k | } |
10748 | | |
10749 | 31.0k | return unit; |
10750 | 31.0k | } |
10751 | | |
10752 | | // --------------------------------------------------------------------------- |
10753 | | |
10754 | | static const struct DatumDesc { |
10755 | | const char *projName; |
10756 | | const char *gcsName; |
10757 | | int gcsCode; |
10758 | | const char *datumName; |
10759 | | int datumCode; |
10760 | | const char *ellipsoidName; |
10761 | | int ellipsoidCode; |
10762 | | double a; |
10763 | | double rf; |
10764 | | } datumDescs[] = { |
10765 | | {"GGRS87", "GGRS87", 4121, "Greek Geodetic Reference System 1987", 6121, |
10766 | | "GRS 1980", 7019, 6378137, 298.257222101}, |
10767 | | {"potsdam", "DHDN", 4314, "Deutsches Hauptdreiecksnetz", 6314, |
10768 | | "Bessel 1841", 7004, 6377397.155, 299.1528128}, |
10769 | | {"carthage", "Carthage", 4223, "Carthage", 6223, "Clarke 1880 (IGN)", 7011, |
10770 | | 6378249.2, 293.4660213}, |
10771 | | {"hermannskogel", "MGI", 4312, "Militar-Geographische Institut", 6312, |
10772 | | "Bessel 1841", 7004, 6377397.155, 299.1528128}, |
10773 | | {"ire65", "TM65", 4299, "TM65", 6299, "Airy Modified 1849", 7002, |
10774 | | 6377340.189, 299.3249646}, |
10775 | | {"nzgd49", "NZGD49", 4272, "New Zealand Geodetic Datum 1949", 6272, |
10776 | | "International 1924", 7022, 6378388, 297}, |
10777 | | {"OSGB36", "OSGB 1936", 4277, "OSGB 1936", 6277, "Airy 1830", 7001, |
10778 | | 6377563.396, 299.3249646}, |
10779 | | }; |
10780 | | |
10781 | | // --------------------------------------------------------------------------- |
10782 | | |
10783 | 26.0k | static bool isGeographicStep(const std::string &name) { |
10784 | 26.0k | return name == "longlat" || name == "lonlat" || name == "latlong" || |
10785 | 26.0k | name == "latlon"; |
10786 | 26.0k | } |
10787 | | |
10788 | | // --------------------------------------------------------------------------- |
10789 | | |
10790 | 7.93k | static bool isGeocentricStep(const std::string &name) { |
10791 | 7.93k | return name == "geocent" || name == "cart"; |
10792 | 7.93k | } |
10793 | | |
10794 | | // --------------------------------------------------------------------------- |
10795 | | |
10796 | 17.4k | static bool isTopocentricStep(const std::string &name) { |
10797 | 17.4k | return name == "topocentric"; |
10798 | 17.4k | } |
10799 | | |
10800 | | // --------------------------------------------------------------------------- |
10801 | | |
10802 | 8.57k | static bool isProjectedStep(const std::string &name) { |
10803 | 8.57k | if (name == "etmerc" || name == "utm" || |
10804 | 8.57k | !getMappingsFromPROJName(name).empty()) { |
10805 | 2.94k | return true; |
10806 | 2.94k | } |
10807 | | // IMPROVE ME: have a better way of distinguishing projections from |
10808 | | // other |
10809 | | // transformations. |
10810 | 5.62k | if (name == "pipeline" || name == "geoc" || name == "deformation" || |
10811 | 5.62k | name == "helmert" || name == "hgridshift" || name == "molodensky" || |
10812 | 5.62k | name == "vgridshift") { |
10813 | 190 | return false; |
10814 | 190 | } |
10815 | 5.43k | const auto *operations = proj_list_operations(); |
10816 | 501k | for (int i = 0; operations[i].id != nullptr; ++i) { |
10817 | 501k | if (name == operations[i].id) { |
10818 | 4.97k | return true; |
10819 | 4.97k | } |
10820 | 501k | } |
10821 | 460 | return false; |
10822 | 5.43k | } |
10823 | | |
10824 | | // --------------------------------------------------------------------------- |
10825 | | |
10826 | 29.6k | static PropertyMap createMapWithUnknownName() { |
10827 | 29.6k | return PropertyMap().set(common::IdentifiedObject::NAME_KEY, "unknown"); |
10828 | 29.6k | } |
10829 | | |
10830 | | // --------------------------------------------------------------------------- |
10831 | | |
10832 | 24.3k | PrimeMeridianNNPtr PROJStringParser::Private::buildPrimeMeridian(Step &step) { |
10833 | | |
10834 | 24.3k | PrimeMeridianNNPtr pm = PrimeMeridian::GREENWICH; |
10835 | 24.3k | const auto &pmStr = getParamValue(step, "pm"); |
10836 | 24.3k | if (!pmStr.empty()) { |
10837 | 2.36k | char *end; |
10838 | 2.36k | double pmValue = dmstor(pmStr.c_str(), &end) * RAD_TO_DEG; |
10839 | 2.36k | if (pmValue != HUGE_VAL && *end == '\0') { |
10840 | 2.26k | pm = PrimeMeridian::create(createMapWithUnknownName(), |
10841 | 2.26k | Angle(pmValue)); |
10842 | 2.26k | } else { |
10843 | 108 | bool found = false; |
10844 | 108 | if (pmStr == "paris") { |
10845 | 27 | found = true; |
10846 | 27 | pm = PrimeMeridian::PARIS; |
10847 | 27 | } |
10848 | 108 | auto proj_prime_meridians = proj_list_prime_meridians(); |
10849 | 986 | for (int i = 0; !found && proj_prime_meridians[i].id != nullptr; |
10850 | 954 | i++) { |
10851 | 954 | if (pmStr == proj_prime_meridians[i].id) { |
10852 | 76 | found = true; |
10853 | 76 | std::string name = static_cast<char>(::toupper(pmStr[0])) + |
10854 | 76 | pmStr.substr(1); |
10855 | 76 | pmValue = dmstor(proj_prime_meridians[i].defn, nullptr) * |
10856 | 76 | RAD_TO_DEG; |
10857 | 76 | pm = PrimeMeridian::create( |
10858 | 76 | PropertyMap().set(IdentifiedObject::NAME_KEY, name), |
10859 | 76 | Angle(pmValue)); |
10860 | 76 | break; |
10861 | 76 | } |
10862 | 954 | } |
10863 | 108 | if (!found) { |
10864 | 5 | throw ParsingException("unknown pm " + pmStr); |
10865 | 5 | } |
10866 | 108 | } |
10867 | 2.36k | } |
10868 | 24.3k | return pm; |
10869 | 24.3k | } |
10870 | | |
10871 | | // --------------------------------------------------------------------------- |
10872 | | |
10873 | 640 | std::string PROJStringParser::Private::guessBodyName(double a) { |
10874 | | |
10875 | 640 | auto ret = Ellipsoid::guessBodyName(dbContext_, a); |
10876 | 640 | if (ret == NON_EARTH_BODY && dbContext_ == nullptr && ctx_ != nullptr) { |
10877 | 47 | dbContext_ = |
10878 | 47 | ctx_->get_cpp_context()->getDatabaseContext().as_nullable(); |
10879 | 47 | if (dbContext_) { |
10880 | 47 | ret = Ellipsoid::guessBodyName(dbContext_, a); |
10881 | 47 | } |
10882 | 47 | } |
10883 | 640 | return ret; |
10884 | 640 | } |
10885 | | |
10886 | | // --------------------------------------------------------------------------- |
10887 | | |
10888 | | GeodeticReferenceFrameNNPtr |
10889 | 15.5k | PROJStringParser::Private::buildDatum(Step &step, const std::string &title) { |
10890 | | |
10891 | 15.5k | std::string ellpsStr = getParamValue(step, "ellps"); |
10892 | 15.5k | const auto &datumStr = getParamValue(step, "datum"); |
10893 | 15.5k | const auto &RStr = getParamValue(step, "R"); |
10894 | 15.5k | const auto &aStr = getParamValue(step, "a"); |
10895 | 15.5k | const auto &bStr = getParamValue(step, "b"); |
10896 | 15.5k | const auto &rfStr = getParamValue(step, "rf"); |
10897 | 15.5k | const auto &fStr = getParamValue(step, "f"); |
10898 | 15.5k | const auto &esStr = getParamValue(step, "es"); |
10899 | 15.5k | const auto &eStr = getParamValue(step, "e"); |
10900 | 15.5k | double a = -1.0; |
10901 | 15.5k | double b = -1.0; |
10902 | 15.5k | double rf = -1.0; |
10903 | 15.5k | const util::optional<std::string> optionalEmptyString{}; |
10904 | 15.5k | const bool numericParamPresent = |
10905 | 15.5k | !RStr.empty() || !aStr.empty() || !bStr.empty() || !rfStr.empty() || |
10906 | 15.5k | !fStr.empty() || !esStr.empty() || !eStr.empty(); |
10907 | | |
10908 | 15.5k | if (!numericParamPresent && ellpsStr.empty() && datumStr.empty() && |
10909 | 15.5k | (step.name == "krovak" || step.name == "mod_krovak")) { |
10910 | 596 | ellpsStr = "bessel"; |
10911 | 596 | } |
10912 | | |
10913 | 15.5k | PrimeMeridianNNPtr pm(buildPrimeMeridian(step)); |
10914 | 15.5k | PropertyMap grfMap; |
10915 | | |
10916 | 15.5k | const auto &nadgrids = getParamValue(step, "nadgrids"); |
10917 | 15.5k | const auto &towgs84 = getParamValue(step, "towgs84"); |
10918 | 15.5k | std::string datumNameSuffix; |
10919 | 15.5k | if (!nadgrids.empty()) { |
10920 | 1.10k | datumNameSuffix = " using nadgrids=" + nadgrids; |
10921 | 14.4k | } else if (!towgs84.empty()) { |
10922 | 1.12k | datumNameSuffix = " using towgs84=" + towgs84; |
10923 | 1.12k | } |
10924 | | |
10925 | | // It is arguable that we allow the prime meridian of a datum defined by |
10926 | | // its name to be overridden, but this is found at least in a regression |
10927 | | // test |
10928 | | // of GDAL. So let's keep the ellipsoid part of the datum in that case and |
10929 | | // use the specified prime meridian. |
10930 | 15.5k | const auto overridePmIfNeeded = |
10931 | 15.5k | [&pm, &datumNameSuffix](const GeodeticReferenceFrameNNPtr &grf) { |
10932 | 13.2k | if (pm->_isEquivalentTo(PrimeMeridian::GREENWICH.get())) { |
10933 | 12.3k | return grf; |
10934 | 12.3k | } else { |
10935 | 901 | return GeodeticReferenceFrame::create( |
10936 | 901 | PropertyMap().set(IdentifiedObject::NAME_KEY, |
10937 | 901 | UNKNOWN_BASED_ON + |
10938 | 901 | grf->ellipsoid()->nameStr() + |
10939 | 901 | " ellipsoid" + datumNameSuffix), |
10940 | 901 | grf->ellipsoid(), grf->anchorDefinition(), pm); |
10941 | 901 | } |
10942 | 13.2k | }; |
10943 | | |
10944 | | // R take precedence |
10945 | 15.5k | if (!RStr.empty()) { |
10946 | 79 | double R; |
10947 | 79 | try { |
10948 | 79 | R = c_locale_stod(RStr); |
10949 | 79 | } catch (const std::invalid_argument &) { |
10950 | 16 | throw ParsingException("Invalid R value"); |
10951 | 16 | } |
10952 | 63 | auto ellipsoid = Ellipsoid::createSphere(createMapWithUnknownName(), |
10953 | 63 | Length(R), guessBodyName(R)); |
10954 | 63 | return GeodeticReferenceFrame::create( |
10955 | 63 | grfMap.set(IdentifiedObject::NAME_KEY, |
10956 | 63 | title.empty() ? "unknown" + datumNameSuffix : title), |
10957 | 63 | ellipsoid, optionalEmptyString, fixupPrimeMeridan(ellipsoid, pm)); |
10958 | 79 | } |
10959 | | |
10960 | 15.4k | if (!datumStr.empty()) { |
10961 | 1.68k | auto l_datum = [&datumStr, &overridePmIfNeeded, &grfMap, |
10962 | 1.68k | &optionalEmptyString, &pm]() { |
10963 | 1.68k | if (datumStr == "WGS84") { |
10964 | 17 | return overridePmIfNeeded(GeodeticReferenceFrame::EPSG_6326); |
10965 | 1.66k | } else if (datumStr == "NAD83") { |
10966 | 295 | return overridePmIfNeeded(GeodeticReferenceFrame::EPSG_6269); |
10967 | 1.37k | } else if (datumStr == "NAD27") { |
10968 | 707 | return overridePmIfNeeded(GeodeticReferenceFrame::EPSG_6267); |
10969 | 707 | } else { |
10970 | | |
10971 | 3.61k | for (const auto &datumDesc : datumDescs) { |
10972 | 3.61k | if (datumStr == datumDesc.projName) { |
10973 | 648 | (void)datumDesc.gcsName; // to please cppcheck |
10974 | 648 | (void)datumDesc.gcsCode; // to please cppcheck |
10975 | 648 | auto ellipsoid = Ellipsoid::createFlattenedSphere( |
10976 | 648 | grfMap |
10977 | 648 | .set(IdentifiedObject::NAME_KEY, |
10978 | 648 | datumDesc.ellipsoidName) |
10979 | 648 | .set(Identifier::CODESPACE_KEY, |
10980 | 648 | Identifier::EPSG) |
10981 | 648 | .set(Identifier::CODE_KEY, |
10982 | 648 | datumDesc.ellipsoidCode), |
10983 | 648 | Length(datumDesc.a), Scale(datumDesc.rf)); |
10984 | 648 | return GeodeticReferenceFrame::create( |
10985 | 648 | grfMap |
10986 | 648 | .set(IdentifiedObject::NAME_KEY, |
10987 | 648 | datumDesc.datumName) |
10988 | 648 | .set(Identifier::CODESPACE_KEY, |
10989 | 648 | Identifier::EPSG) |
10990 | 648 | .set(Identifier::CODE_KEY, datumDesc.datumCode), |
10991 | 648 | ellipsoid, optionalEmptyString, pm); |
10992 | 648 | } |
10993 | 3.61k | } |
10994 | 664 | } |
10995 | 16 | throw ParsingException("unknown datum " + datumStr); |
10996 | 1.68k | }(); |
10997 | 1.68k | if (!numericParamPresent) { |
10998 | 1.35k | return l_datum; |
10999 | 1.35k | } |
11000 | 329 | a = l_datum->ellipsoid()->semiMajorAxis().getSIValue(); |
11001 | 329 | rf = l_datum->ellipsoid()->computedInverseFlattening(); |
11002 | 329 | } |
11003 | | |
11004 | 13.7k | else if (!ellpsStr.empty()) { |
11005 | 1.20k | auto l_datum = [&ellpsStr, &title, &grfMap, &optionalEmptyString, &pm, |
11006 | 1.20k | &datumNameSuffix]() { |
11007 | 1.20k | if (ellpsStr == "WGS84") { |
11008 | 24 | return GeodeticReferenceFrame::create( |
11009 | 24 | grfMap.set(IdentifiedObject::NAME_KEY, |
11010 | 24 | title.empty() ? std::string(UNKNOWN_BASED_ON) |
11011 | 16 | .append("WGS 84 ellipsoid") |
11012 | 16 | .append(datumNameSuffix) |
11013 | 24 | : title), |
11014 | 24 | Ellipsoid::WGS84, optionalEmptyString, pm); |
11015 | 1.18k | } else if (ellpsStr == "GRS80") { |
11016 | 33 | return GeodeticReferenceFrame::create( |
11017 | 33 | grfMap.set(IdentifiedObject::NAME_KEY, |
11018 | 33 | title.empty() ? std::string(UNKNOWN_BASED_ON) |
11019 | 14 | .append("GRS 1980 ellipsoid") |
11020 | 14 | .append(datumNameSuffix) |
11021 | 33 | : title), |
11022 | 33 | Ellipsoid::GRS1980, optionalEmptyString, pm); |
11023 | 1.15k | } else { |
11024 | 1.15k | auto proj_ellps = proj_list_ellps(); |
11025 | 16.0k | for (int i = 0; proj_ellps[i].id != nullptr; i++) { |
11026 | 15.9k | if (ellpsStr == proj_ellps[i].id) { |
11027 | 1.13k | assert(strncmp(proj_ellps[i].major, "a=", 2) == 0); |
11028 | 1.13k | const double a_iter = |
11029 | 1.13k | c_locale_stod(proj_ellps[i].major + 2); |
11030 | 1.13k | EllipsoidPtr ellipsoid; |
11031 | 1.13k | PropertyMap ellpsMap; |
11032 | 1.13k | if (strncmp(proj_ellps[i].ell, "b=", 2) == 0) { |
11033 | 35 | const double b_iter = |
11034 | 35 | c_locale_stod(proj_ellps[i].ell + 2); |
11035 | 35 | ellipsoid = |
11036 | 35 | Ellipsoid::createTwoAxis( |
11037 | 35 | ellpsMap.set(IdentifiedObject::NAME_KEY, |
11038 | 35 | proj_ellps[i].name), |
11039 | 35 | Length(a_iter), Length(b_iter)) |
11040 | 35 | .as_nullable(); |
11041 | 1.10k | } else { |
11042 | 1.10k | assert(strncmp(proj_ellps[i].ell, "rf=", 3) == 0); |
11043 | 1.10k | const double rf_iter = |
11044 | 1.10k | c_locale_stod(proj_ellps[i].ell + 3); |
11045 | 1.10k | ellipsoid = |
11046 | 1.10k | Ellipsoid::createFlattenedSphere( |
11047 | 1.10k | ellpsMap.set(IdentifiedObject::NAME_KEY, |
11048 | 1.10k | proj_ellps[i].name), |
11049 | 1.10k | Length(a_iter), Scale(rf_iter)) |
11050 | 1.10k | .as_nullable(); |
11051 | 1.10k | } |
11052 | 1.13k | return GeodeticReferenceFrame::create( |
11053 | 1.13k | grfMap.set(IdentifiedObject::NAME_KEY, |
11054 | 1.13k | title.empty() |
11055 | 1.13k | ? std::string(UNKNOWN_BASED_ON) |
11056 | 1.03k | .append(proj_ellps[i].name) |
11057 | 1.03k | .append(" ellipsoid") |
11058 | 1.03k | .append(datumNameSuffix) |
11059 | 1.13k | : title), |
11060 | 1.13k | NN_NO_CHECK(ellipsoid), optionalEmptyString, pm); |
11061 | 1.13k | } |
11062 | 15.9k | } |
11063 | 16 | throw ParsingException("unknown ellipsoid " + ellpsStr); |
11064 | 1.15k | } |
11065 | 1.20k | }(); |
11066 | 1.20k | if (!numericParamPresent) { |
11067 | 1.13k | return l_datum; |
11068 | 1.13k | } |
11069 | 76 | a = l_datum->ellipsoid()->semiMajorAxis().getSIValue(); |
11070 | 76 | if (l_datum->ellipsoid()->semiMinorAxis().has_value()) { |
11071 | 18 | b = l_datum->ellipsoid()->semiMinorAxis()->getSIValue(); |
11072 | 58 | } else { |
11073 | 58 | rf = l_datum->ellipsoid()->computedInverseFlattening(); |
11074 | 58 | } |
11075 | 76 | } |
11076 | | |
11077 | 12.9k | if (!aStr.empty()) { |
11078 | 315 | try { |
11079 | 315 | a = c_locale_stod(aStr); |
11080 | 315 | } catch (const std::invalid_argument &) { |
11081 | 25 | throw ParsingException("Invalid a value"); |
11082 | 25 | } |
11083 | 315 | } |
11084 | | |
11085 | 12.9k | const auto createGRF = [&grfMap, &title, &optionalEmptyString, |
11086 | 12.9k | &datumNameSuffix, |
11087 | 12.9k | &pm](const EllipsoidNNPtr &ellipsoid) { |
11088 | 577 | std::string datumName(title); |
11089 | 577 | if (title.empty()) { |
11090 | 488 | if (ellipsoid->nameStr() != "unknown") { |
11091 | 61 | datumName = UNKNOWN_BASED_ON; |
11092 | 61 | datumName += ellipsoid->nameStr(); |
11093 | 61 | datumName += " ellipsoid"; |
11094 | 427 | } else { |
11095 | 427 | datumName = "unknown"; |
11096 | 427 | } |
11097 | 488 | datumName += datumNameSuffix; |
11098 | 488 | } |
11099 | 577 | return GeodeticReferenceFrame::create( |
11100 | 577 | grfMap.set(IdentifiedObject::NAME_KEY, datumName), ellipsoid, |
11101 | 577 | optionalEmptyString, fixupPrimeMeridan(ellipsoid, pm)); |
11102 | 577 | }; |
11103 | | |
11104 | 12.9k | if (a > 0 && (b > 0 || !bStr.empty())) { |
11105 | 153 | if (!bStr.empty()) { |
11106 | 135 | try { |
11107 | 135 | b = c_locale_stod(bStr); |
11108 | 135 | } catch (const std::invalid_argument &) { |
11109 | 4 | throw ParsingException("Invalid b value"); |
11110 | 4 | } |
11111 | 135 | } |
11112 | 149 | auto ellipsoid = |
11113 | 149 | Ellipsoid::createTwoAxis(createMapWithUnknownName(), Length(a), |
11114 | 149 | Length(b), guessBodyName(a)) |
11115 | 149 | ->identify(); |
11116 | 149 | return createGRF(ellipsoid); |
11117 | 153 | } |
11118 | | |
11119 | 12.7k | else if (a > 0 && (rf >= 0 || !rfStr.empty())) { |
11120 | 319 | if (!rfStr.empty()) { |
11121 | 16 | try { |
11122 | 16 | rf = c_locale_stod(rfStr); |
11123 | 16 | } catch (const std::invalid_argument &) { |
11124 | 6 | throw ParsingException("Invalid rf value"); |
11125 | 6 | } |
11126 | 16 | } |
11127 | 313 | auto ellipsoid = Ellipsoid::createFlattenedSphere( |
11128 | 313 | createMapWithUnknownName(), Length(a), Scale(rf), |
11129 | 313 | guessBodyName(a)) |
11130 | 313 | ->identify(); |
11131 | 313 | return createGRF(ellipsoid); |
11132 | 319 | } |
11133 | | |
11134 | 12.4k | else if (a > 0 && !fStr.empty()) { |
11135 | 7 | double f; |
11136 | 7 | try { |
11137 | 7 | f = c_locale_stod(fStr); |
11138 | 7 | } catch (const std::invalid_argument &) { |
11139 | 5 | throw ParsingException("Invalid f value"); |
11140 | 5 | } |
11141 | 2 | auto ellipsoid = Ellipsoid::createFlattenedSphere( |
11142 | 2 | createMapWithUnknownName(), Length(a), |
11143 | 2 | Scale(f != 0.0 ? 1.0 / f : 0.0), guessBodyName(a)) |
11144 | 2 | ->identify(); |
11145 | 2 | return createGRF(ellipsoid); |
11146 | 7 | } |
11147 | | |
11148 | 12.4k | else if (a > 0 && !eStr.empty()) { |
11149 | 10 | double e; |
11150 | 10 | try { |
11151 | 10 | e = c_locale_stod(eStr); |
11152 | 10 | } catch (const std::invalid_argument &) { |
11153 | 10 | throw ParsingException("Invalid e value"); |
11154 | 10 | } |
11155 | 0 | double alpha = asin(e); /* angular eccentricity */ |
11156 | 0 | double f = 1 - cos(alpha); /* = 1 - sqrt (1 - es); */ |
11157 | 0 | auto ellipsoid = Ellipsoid::createFlattenedSphere( |
11158 | 0 | createMapWithUnknownName(), Length(a), |
11159 | 0 | Scale(f != 0.0 ? 1.0 / f : 0.0), guessBodyName(a)) |
11160 | 0 | ->identify(); |
11161 | 0 | return createGRF(ellipsoid); |
11162 | 10 | } |
11163 | | |
11164 | 12.4k | else if (a > 0 && !esStr.empty()) { |
11165 | 1 | double es; |
11166 | 1 | try { |
11167 | 1 | es = c_locale_stod(esStr); |
11168 | 1 | } catch (const std::invalid_argument &) { |
11169 | 1 | throw ParsingException("Invalid es value"); |
11170 | 1 | } |
11171 | 0 | double f = 1 - sqrt(1 - es); |
11172 | 0 | auto ellipsoid = Ellipsoid::createFlattenedSphere( |
11173 | 0 | createMapWithUnknownName(), Length(a), |
11174 | 0 | Scale(f != 0.0 ? 1.0 / f : 0.0), guessBodyName(a)) |
11175 | 0 | ->identify(); |
11176 | 0 | return createGRF(ellipsoid); |
11177 | 1 | } |
11178 | | |
11179 | | // If only a is specified, create a sphere |
11180 | 12.4k | if (a > 0 && bStr.empty() && rfStr.empty() && eStr.empty() && |
11181 | 12.4k | esStr.empty()) { |
11182 | 113 | auto ellipsoid = Ellipsoid::createSphere(createMapWithUnknownName(), |
11183 | 113 | Length(a), guessBodyName(a)); |
11184 | 113 | return createGRF(ellipsoid); |
11185 | 113 | } |
11186 | | |
11187 | 12.3k | if (!bStr.empty() && aStr.empty()) { |
11188 | 6 | throw ParsingException("b found, but a missing"); |
11189 | 6 | } |
11190 | | |
11191 | 12.3k | if (!rfStr.empty() && aStr.empty()) { |
11192 | 1 | throw ParsingException("rf found, but a missing"); |
11193 | 1 | } |
11194 | | |
11195 | 12.3k | if (!fStr.empty() && aStr.empty()) { |
11196 | 4 | throw ParsingException("f found, but a missing"); |
11197 | 4 | } |
11198 | | |
11199 | 12.3k | if (!eStr.empty() && aStr.empty()) { |
11200 | 5 | throw ParsingException("e found, but a missing"); |
11201 | 5 | } |
11202 | | |
11203 | 12.3k | if (!esStr.empty() && aStr.empty()) { |
11204 | 3 | throw ParsingException("es found, but a missing"); |
11205 | 3 | } |
11206 | | |
11207 | 12.2k | return overridePmIfNeeded(GeodeticReferenceFrame::EPSG_6326); |
11208 | 12.3k | } |
11209 | | |
11210 | | // --------------------------------------------------------------------------- |
11211 | | |
11212 | | static const MeridianPtr nullMeridian{}; |
11213 | | |
11214 | | static CoordinateSystemAxisNNPtr |
11215 | | createAxis(const std::string &name, const std::string &abbreviation, |
11216 | | const AxisDirection &direction, const common::UnitOfMeasure &unit, |
11217 | 88.2k | const MeridianPtr &meridian = nullMeridian) { |
11218 | 88.2k | return CoordinateSystemAxis::create( |
11219 | 88.2k | PropertyMap().set(IdentifiedObject::NAME_KEY, name), abbreviation, |
11220 | 88.2k | direction, unit, meridian); |
11221 | 88.2k | } |
11222 | | |
11223 | | std::vector<CoordinateSystemAxisNNPtr> |
11224 | | PROJStringParser::Private::processAxisSwap(Step &step, |
11225 | | const UnitOfMeasure &unit, |
11226 | | int iAxisSwap, AxisType axisType, |
11227 | 22.0k | bool ignorePROJAxis) { |
11228 | 22.0k | assert(iAxisSwap < 0 || ci_equal(steps_[iAxisSwap].name, "axisswap")); |
11229 | | |
11230 | 22.0k | const bool isGeographic = unit.type() == UnitOfMeasure::Type::ANGULAR; |
11231 | 22.0k | const bool isSpherical = isGeographic && hasParamValue(step, "geoc"); |
11232 | 22.0k | const auto &eastName = isSpherical ? "Planetocentric longitude" |
11233 | 22.0k | : isGeographic ? AxisName::Longitude |
11234 | 21.8k | : AxisName::Easting; |
11235 | 22.0k | const auto &eastAbbev = isSpherical ? "V" |
11236 | 22.0k | : isGeographic ? AxisAbbreviation::lon |
11237 | 21.8k | : AxisAbbreviation::E; |
11238 | 22.0k | const auto &eastDir = |
11239 | 22.0k | isGeographic ? AxisDirection::EAST |
11240 | 22.0k | : (axisType == AxisType::NORTH_POLE) ? AxisDirection::SOUTH |
11241 | 7.46k | : (axisType == AxisType::SOUTH_POLE) ? AxisDirection::NORTH |
11242 | 7.42k | : AxisDirection::EAST; |
11243 | 22.0k | CoordinateSystemAxisNNPtr east = createAxis( |
11244 | 22.0k | eastName, eastAbbev, eastDir, unit, |
11245 | 22.0k | (!isGeographic && |
11246 | 22.0k | (axisType == AxisType::NORTH_POLE || axisType == AxisType::SOUTH_POLE)) |
11247 | 22.0k | ? Meridian::create(Angle(90, UnitOfMeasure::DEGREE)).as_nullable() |
11248 | 22.0k | : nullMeridian); |
11249 | | |
11250 | 22.0k | const auto &northName = isSpherical ? "Planetocentric latitude" |
11251 | 22.0k | : isGeographic ? AxisName::Latitude |
11252 | 21.8k | : AxisName::Northing; |
11253 | 22.0k | const auto &northAbbev = isSpherical ? "U" |
11254 | 22.0k | : isGeographic ? AxisAbbreviation::lat |
11255 | 21.8k | : AxisAbbreviation::N; |
11256 | 22.0k | const auto &northDir = isGeographic ? AxisDirection::NORTH |
11257 | 22.0k | : (axisType == AxisType::NORTH_POLE) |
11258 | 7.46k | ? AxisDirection::SOUTH |
11259 | | /*: (axisType == AxisType::SOUTH_POLE) |
11260 | | ? AxisDirection::NORTH*/ |
11261 | 7.46k | : AxisDirection::NORTH; |
11262 | 22.0k | const CoordinateSystemAxisNNPtr north = createAxis( |
11263 | 22.0k | northName, northAbbev, northDir, unit, |
11264 | 22.0k | isGeographic ? nullMeridian |
11265 | 22.0k | : (axisType == AxisType::NORTH_POLE) |
11266 | 7.46k | ? Meridian::create(Angle(180, UnitOfMeasure::DEGREE)).as_nullable() |
11267 | 7.46k | : (axisType == AxisType::SOUTH_POLE) |
11268 | 7.42k | ? Meridian::create(Angle(0, UnitOfMeasure::DEGREE)).as_nullable() |
11269 | 7.42k | : nullMeridian); |
11270 | | |
11271 | 22.0k | CoordinateSystemAxisNNPtr west = |
11272 | 22.0k | createAxis(isSpherical ? "Planetocentric longitude" |
11273 | 22.0k | : isGeographic ? AxisName::Longitude |
11274 | 21.8k | : AxisName::Westing, |
11275 | 22.0k | isSpherical ? "V" |
11276 | 22.0k | : isGeographic ? AxisAbbreviation::lon |
11277 | 21.8k | : std::string(), |
11278 | 22.0k | AxisDirection::WEST, unit); |
11279 | | |
11280 | 22.0k | CoordinateSystemAxisNNPtr south = |
11281 | 22.0k | createAxis(isSpherical ? "Planetocentric latitude" |
11282 | 22.0k | : isGeographic ? AxisName::Latitude |
11283 | 21.8k | : AxisName::Southing, |
11284 | 22.0k | isSpherical ? "U" |
11285 | 22.0k | : isGeographic ? AxisAbbreviation::lat |
11286 | 21.8k | : std::string(), |
11287 | 22.0k | AxisDirection::SOUTH, unit); |
11288 | | |
11289 | 22.0k | std::vector<CoordinateSystemAxisNNPtr> axis{east, north}; |
11290 | | |
11291 | 22.0k | const auto &axisStr = getParamValue(step, "axis"); |
11292 | 22.0k | if (!ignorePROJAxis && !axisStr.empty()) { |
11293 | 298 | if (axisStr.size() == 3) { |
11294 | 866 | for (int i = 0; i < 2; i++) { |
11295 | 591 | if (axisStr[i] == 'n') { |
11296 | 136 | axis[i] = north; |
11297 | 455 | } else if (axisStr[i] == 's') { |
11298 | 149 | axis[i] = south; |
11299 | 306 | } else if (axisStr[i] == 'e') { |
11300 | 150 | axis[i] = east; |
11301 | 156 | } else if (axisStr[i] == 'w') { |
11302 | 133 | axis[i] = west; |
11303 | 133 | } else { |
11304 | 23 | throw ParsingException("Unhandled axis=" + axisStr); |
11305 | 23 | } |
11306 | 591 | } |
11307 | 298 | } else { |
11308 | 0 | throw ParsingException("Unhandled axis=" + axisStr); |
11309 | 0 | } |
11310 | 21.7k | } else if (iAxisSwap >= 0) { |
11311 | 71 | auto &stepAxisSwap = steps_[iAxisSwap]; |
11312 | 71 | const auto &orderStr = getParamValue(stepAxisSwap, "order"); |
11313 | 71 | auto orderTab = split(orderStr, ','); |
11314 | 71 | if (orderTab.size() != 2) { |
11315 | 10 | throw ParsingException("Unhandled order=" + orderStr); |
11316 | 10 | } |
11317 | 61 | if (stepAxisSwap.inverted) { |
11318 | 0 | throw ParsingException("Unhandled +inv for +proj=axisswap"); |
11319 | 0 | } |
11320 | | |
11321 | 160 | for (size_t i = 0; i < 2; i++) { |
11322 | 114 | if (orderTab[i] == "1") { |
11323 | 16 | axis[i] = east; |
11324 | 98 | } else if (orderTab[i] == "-1") { |
11325 | 51 | axis[i] = west; |
11326 | 51 | } else if (orderTab[i] == "2") { |
11327 | 2 | axis[i] = north; |
11328 | 45 | } else if (orderTab[i] == "-2") { |
11329 | 30 | axis[i] = south; |
11330 | 30 | } else { |
11331 | 15 | throw ParsingException("Unhandled order=" + orderStr); |
11332 | 15 | } |
11333 | 114 | } |
11334 | 21.6k | } else if ((step.name == "krovak" || step.name == "mod_krovak") && |
11335 | 21.6k | hasParamValue(step, "czech")) { |
11336 | 48 | axis[0] = std::move(west); |
11337 | 48 | axis[1] = std::move(south); |
11338 | 48 | } |
11339 | 22.0k | return axis; |
11340 | 22.0k | } |
11341 | | |
11342 | | // --------------------------------------------------------------------------- |
11343 | | |
11344 | | EllipsoidalCSNNPtr PROJStringParser::Private::buildEllipsoidalCS( |
11345 | 14.4k | int iStep, int iUnitConvert, int iAxisSwap, bool ignorePROJAxis) { |
11346 | 14.4k | auto &step = steps_[iStep]; |
11347 | 14.4k | assert(iUnitConvert < 0 || |
11348 | 14.4k | ci_equal(steps_[iUnitConvert].name, "unitconvert")); |
11349 | | |
11350 | 14.4k | UnitOfMeasure angularUnit = UnitOfMeasure::DEGREE; |
11351 | 14.4k | if (iUnitConvert >= 0) { |
11352 | 34 | auto &stepUnitConvert = steps_[iUnitConvert]; |
11353 | 34 | const std::string *xy_in = &getParamValue(stepUnitConvert, "xy_in"); |
11354 | 34 | const std::string *xy_out = &getParamValue(stepUnitConvert, "xy_out"); |
11355 | 34 | if (stepUnitConvert.inverted) { |
11356 | 2 | std::swap(xy_in, xy_out); |
11357 | 2 | } |
11358 | 34 | if (iUnitConvert < iStep) { |
11359 | 21 | std::swap(xy_in, xy_out); |
11360 | 21 | } |
11361 | 34 | if (xy_in->empty() || xy_out->empty() || *xy_in != "rad" || |
11362 | 34 | (*xy_out != "rad" && *xy_out != "deg" && *xy_out != "grad")) { |
11363 | 25 | throw ParsingException("unhandled values for xy_in and/or xy_out"); |
11364 | 25 | } |
11365 | 9 | if (*xy_out == "rad") { |
11366 | 9 | angularUnit = UnitOfMeasure::RADIAN; |
11367 | 9 | } else if (*xy_out == "grad") { |
11368 | 0 | angularUnit = UnitOfMeasure::GRAD; |
11369 | 0 | } |
11370 | 9 | } |
11371 | | |
11372 | 14.3k | std::vector<CoordinateSystemAxisNNPtr> axis = processAxisSwap( |
11373 | 14.3k | step, angularUnit, iAxisSwap, AxisType::REGULAR, ignorePROJAxis); |
11374 | 14.3k | CoordinateSystemAxisNNPtr up = CoordinateSystemAxis::create( |
11375 | 14.3k | util::PropertyMap().set(IdentifiedObject::NAME_KEY, |
11376 | 14.3k | AxisName::Ellipsoidal_height), |
11377 | 14.3k | AxisAbbreviation::h, AxisDirection::UP, |
11378 | 14.3k | buildUnit(step, "vunits", "vto_meter")); |
11379 | | |
11380 | 14.3k | return (!hasParamValue(step, "geoidgrids") && |
11381 | 14.3k | (hasParamValue(step, "vunits") || hasParamValue(step, "vto_meter"))) |
11382 | 14.3k | ? EllipsoidalCS::create(emptyPropertyMap, axis[0], axis[1], up) |
11383 | 14.3k | : EllipsoidalCS::create(emptyPropertyMap, axis[0], axis[1]); |
11384 | 14.4k | } |
11385 | | |
11386 | | // --------------------------------------------------------------------------- |
11387 | | |
11388 | | SphericalCSNNPtr PROJStringParser::Private::buildSphericalCS( |
11389 | 241 | int iStep, int iUnitConvert, int iAxisSwap, bool ignorePROJAxis) { |
11390 | 241 | auto &step = steps_[iStep]; |
11391 | 241 | assert(iUnitConvert < 0 || |
11392 | 241 | ci_equal(steps_[iUnitConvert].name, "unitconvert")); |
11393 | | |
11394 | 241 | UnitOfMeasure angularUnit = UnitOfMeasure::DEGREE; |
11395 | 241 | if (iUnitConvert >= 0) { |
11396 | 32 | auto &stepUnitConvert = steps_[iUnitConvert]; |
11397 | 32 | const std::string *xy_in = &getParamValue(stepUnitConvert, "xy_in"); |
11398 | 32 | const std::string *xy_out = &getParamValue(stepUnitConvert, "xy_out"); |
11399 | 32 | if (stepUnitConvert.inverted) { |
11400 | 1 | std::swap(xy_in, xy_out); |
11401 | 1 | } |
11402 | 32 | if (iUnitConvert < iStep) { |
11403 | 20 | std::swap(xy_in, xy_out); |
11404 | 20 | } |
11405 | 32 | if (xy_in->empty() || xy_out->empty() || *xy_in != "rad" || |
11406 | 32 | (*xy_out != "rad" && *xy_out != "deg" && *xy_out != "grad")) { |
11407 | 32 | throw ParsingException("unhandled values for xy_in and/or xy_out"); |
11408 | 32 | } |
11409 | 0 | if (*xy_out == "rad") { |
11410 | 0 | angularUnit = UnitOfMeasure::RADIAN; |
11411 | 0 | } else if (*xy_out == "grad") { |
11412 | 0 | angularUnit = UnitOfMeasure::GRAD; |
11413 | 0 | } |
11414 | 0 | } |
11415 | | |
11416 | 209 | std::vector<CoordinateSystemAxisNNPtr> axis = processAxisSwap( |
11417 | 209 | step, angularUnit, iAxisSwap, AxisType::REGULAR, ignorePROJAxis); |
11418 | | |
11419 | 209 | return SphericalCS::create(emptyPropertyMap, axis[0], axis[1]); |
11420 | 241 | } |
11421 | | |
11422 | | // --------------------------------------------------------------------------- |
11423 | | |
11424 | | static double getNumericValue(const std::string ¶mValue, |
11425 | 3.50k | bool *pHasError = nullptr) { |
11426 | 3.50k | bool success; |
11427 | 3.50k | double value = c_locale_stod(paramValue, success); |
11428 | 3.50k | if (pHasError) |
11429 | 280 | *pHasError = !success; |
11430 | 3.50k | return value; |
11431 | 3.50k | } |
11432 | | |
11433 | | // --------------------------------------------------------------------------- |
11434 | | namespace { |
11435 | 13.6k | template <class T> inline void ignoreRetVal(T) {} |
11436 | | } // namespace |
11437 | | |
11438 | | GeodeticCRSNNPtr PROJStringParser::Private::buildGeodeticCRS( |
11439 | 13.6k | int iStep, int iUnitConvert, int iAxisSwap, bool ignorePROJAxis) { |
11440 | 13.6k | auto &step = steps_[iStep]; |
11441 | | |
11442 | 13.6k | const bool l_isGeographicStep = isGeographicStep(step.name); |
11443 | 13.6k | const auto &title = l_isGeographicStep ? title_ : emptyString; |
11444 | | |
11445 | | // units=m is often found in the wild. |
11446 | | // No need to create a extension string for this |
11447 | 13.6k | ignoreRetVal(hasParamValue(step, "units")); |
11448 | | |
11449 | 13.6k | auto datum = buildDatum(step, title); |
11450 | | |
11451 | 13.6k | auto props = PropertyMap().set(IdentifiedObject::NAME_KEY, |
11452 | 13.6k | title.empty() ? "unknown" : title); |
11453 | | |
11454 | 13.6k | if (l_isGeographicStep && |
11455 | 13.6k | (hasUnusedParameters(step) || |
11456 | 6.28k | getNumericValue(getParamValue(step, "lon_0")) != 0.0)) { |
11457 | 4.44k | props.set("EXTENSION_PROJ4", projString_); |
11458 | 4.44k | } |
11459 | 13.6k | props.set("IMPLICIT_CS", true); |
11460 | | |
11461 | 13.6k | if (!hasParamValue(step, "geoc")) { |
11462 | 13.2k | auto cs = |
11463 | 13.2k | buildEllipsoidalCS(iStep, iUnitConvert, iAxisSwap, ignorePROJAxis); |
11464 | | |
11465 | 13.2k | return GeographicCRS::create(props, datum, cs); |
11466 | 13.2k | } else { |
11467 | 357 | auto cs = |
11468 | 357 | buildSphericalCS(iStep, iUnitConvert, iAxisSwap, ignorePROJAxis); |
11469 | | |
11470 | 357 | return GeodeticCRS::create(props, datum, cs); |
11471 | 357 | } |
11472 | 13.6k | } |
11473 | | |
11474 | | // --------------------------------------------------------------------------- |
11475 | | |
11476 | | GeodeticCRSNNPtr |
11477 | 1.85k | PROJStringParser::Private::buildGeocentricCRS(int iStep, int iUnitConvert) { |
11478 | 1.85k | auto &step = steps_[iStep]; |
11479 | | |
11480 | 1.85k | assert(isGeocentricStep(step.name) || isTopocentricStep(step.name)); |
11481 | 1.85k | assert(iUnitConvert < 0 || |
11482 | 1.85k | ci_equal(steps_[iUnitConvert].name, "unitconvert")); |
11483 | | |
11484 | 1.85k | const auto &title = title_; |
11485 | | |
11486 | 1.85k | auto datum = buildDatum(step, title); |
11487 | | |
11488 | 1.85k | UnitOfMeasure unit = buildUnit(step, "units", ""); |
11489 | 1.85k | if (iUnitConvert >= 0) { |
11490 | 21 | auto &stepUnitConvert = steps_[iUnitConvert]; |
11491 | 21 | const std::string *xy_in = &getParamValue(stepUnitConvert, "xy_in"); |
11492 | 21 | const std::string *xy_out = &getParamValue(stepUnitConvert, "xy_out"); |
11493 | 21 | const std::string *z_in = &getParamValue(stepUnitConvert, "z_in"); |
11494 | 21 | const std::string *z_out = &getParamValue(stepUnitConvert, "z_out"); |
11495 | 21 | if (stepUnitConvert.inverted) { |
11496 | 0 | std::swap(xy_in, xy_out); |
11497 | 0 | std::swap(z_in, z_out); |
11498 | 0 | } |
11499 | 21 | if (xy_in->empty() || xy_out->empty() || *xy_in != "m" || |
11500 | 21 | *z_in != "m" || *xy_out != *z_out) { |
11501 | 21 | throw ParsingException( |
11502 | 21 | "unhandled values for xy_in, z_in, xy_out or z_out"); |
11503 | 21 | } |
11504 | | |
11505 | 0 | const LinearUnitDesc *unitsMatch = nullptr; |
11506 | 0 | try { |
11507 | 0 | double to_meter_value = c_locale_stod(*xy_out); |
11508 | 0 | unitsMatch = getLinearUnits(to_meter_value); |
11509 | 0 | if (unitsMatch == nullptr) { |
11510 | 0 | unit = _buildUnit(to_meter_value); |
11511 | 0 | } |
11512 | 0 | } catch (const std::invalid_argument &) { |
11513 | 0 | unitsMatch = getLinearUnits(*xy_out); |
11514 | 0 | if (!unitsMatch) { |
11515 | 0 | throw ParsingException( |
11516 | 0 | "unhandled values for xy_in, z_in, xy_out or z_out"); |
11517 | 0 | } |
11518 | 0 | unit = _buildUnit(unitsMatch); |
11519 | 0 | } |
11520 | 0 | } |
11521 | | |
11522 | 1.83k | auto props = PropertyMap().set(IdentifiedObject::NAME_KEY, |
11523 | 1.83k | title.empty() ? "unknown" : title); |
11524 | 1.83k | auto cs = CartesianCS::createGeocentric(unit); |
11525 | | |
11526 | 1.83k | if (hasUnusedParameters(step)) { |
11527 | 78 | props.set("EXTENSION_PROJ4", projString_); |
11528 | 78 | } |
11529 | | |
11530 | 1.83k | return GeodeticCRS::create(props, datum, cs); |
11531 | 1.85k | } |
11532 | | |
11533 | | // --------------------------------------------------------------------------- |
11534 | | |
11535 | | CRSNNPtr |
11536 | | PROJStringParser::Private::buildBoundOrCompoundCRSIfNeeded(int iStep, |
11537 | 14.9k | CRSNNPtr crs) { |
11538 | 14.9k | auto &step = steps_[iStep]; |
11539 | 14.9k | const auto &nadgrids = getParamValue(step, "nadgrids"); |
11540 | 14.9k | const auto &towgs84 = getParamValue(step, "towgs84"); |
11541 | | // nadgrids has the priority over towgs84 |
11542 | 14.9k | if (!ignoreNadgrids_ && !nadgrids.empty()) { |
11543 | 1.09k | crs = BoundCRS::createFromNadgrids(crs, nadgrids); |
11544 | 13.8k | } else if (!towgs84.empty()) { |
11545 | 1.11k | std::vector<double> towgs84Values; |
11546 | 1.11k | const auto tokens = split(towgs84, ','); |
11547 | 3.68k | for (const auto &str : tokens) { |
11548 | 3.68k | try { |
11549 | 3.68k | towgs84Values.push_back(c_locale_stod(str)); |
11550 | 3.68k | } catch (const std::invalid_argument &) { |
11551 | 142 | throw ParsingException("Non numerical value in towgs84 clause"); |
11552 | 142 | } |
11553 | 3.68k | } |
11554 | | |
11555 | 970 | if (towgs84Values.size() == 7 && dbContext_) { |
11556 | 6 | if (dbContext_->toWGS84AutocorrectWrongValues( |
11557 | 6 | towgs84Values[0], towgs84Values[1], towgs84Values[2], |
11558 | 6 | towgs84Values[3], towgs84Values[4], towgs84Values[5], |
11559 | 6 | towgs84Values[6])) { |
11560 | 0 | for (auto &pair : step.paramValues) { |
11561 | 0 | if (ci_equal(pair.key, "towgs84")) { |
11562 | 0 | pair.value.clear(); |
11563 | 0 | for (int i = 0; i < 7; ++i) { |
11564 | 0 | if (i > 0) |
11565 | 0 | pair.value += ','; |
11566 | 0 | pair.value += internal::toString(towgs84Values[i]); |
11567 | 0 | } |
11568 | 0 | break; |
11569 | 0 | } |
11570 | 0 | } |
11571 | 0 | } |
11572 | 6 | } |
11573 | | |
11574 | 970 | crs = BoundCRS::createFromTOWGS84(crs, towgs84Values); |
11575 | 970 | } |
11576 | | |
11577 | 14.8k | const auto &geoidgrids = getParamValue(step, "geoidgrids"); |
11578 | 14.8k | if (!geoidgrids.empty()) { |
11579 | 5.94k | auto vdatum = VerticalReferenceFrame::create( |
11580 | 5.94k | PropertyMap().set(common::IdentifiedObject::NAME_KEY, |
11581 | 5.94k | "unknown using geoidgrids=" + geoidgrids)); |
11582 | | |
11583 | 5.94k | const UnitOfMeasure unit = buildUnit(step, "vunits", "vto_meter"); |
11584 | | |
11585 | 5.94k | auto vcrs = |
11586 | 5.94k | VerticalCRS::create(createMapWithUnknownName(), vdatum, |
11587 | 5.94k | VerticalCS::createGravityRelatedHeight(unit)); |
11588 | | |
11589 | 5.94k | CRSNNPtr geogCRS = GeographicCRS::EPSG_4979; // default |
11590 | 5.94k | const auto &geoid_crs = getParamValue(step, "geoid_crs"); |
11591 | 5.94k | if (!geoid_crs.empty()) { |
11592 | 1.84k | if (geoid_crs == "WGS84") { |
11593 | | // nothing to do |
11594 | 1.84k | } else if (geoid_crs == "horizontal_crs") { |
11595 | 1.79k | auto geogCRSOfCompoundCRS = crs->extractGeographicCRS(); |
11596 | 1.79k | if (geogCRSOfCompoundCRS && |
11597 | 1.79k | geogCRSOfCompoundCRS->primeMeridian() |
11598 | 1.78k | ->longitude() |
11599 | 1.78k | .getSIValue() == 0 && |
11600 | 1.79k | geogCRSOfCompoundCRS->coordinateSystem() |
11601 | 1.71k | ->axisList()[0] |
11602 | 1.71k | ->unit() == UnitOfMeasure::DEGREE) { |
11603 | 1.71k | geogCRS = geogCRSOfCompoundCRS->promoteTo3D(std::string(), |
11604 | 1.71k | nullptr); |
11605 | 1.71k | } else if (geogCRSOfCompoundCRS) { |
11606 | 68 | auto geogCRSOfCompoundCRSDatum = |
11607 | 68 | geogCRSOfCompoundCRS->datumNonNull(nullptr); |
11608 | 68 | geogCRS = GeographicCRS::create( |
11609 | 68 | createMapWithUnknownName(), |
11610 | 68 | datum::GeodeticReferenceFrame::create( |
11611 | 68 | util::PropertyMap().set( |
11612 | 68 | common::IdentifiedObject::NAME_KEY, |
11613 | 68 | geogCRSOfCompoundCRSDatum->nameStr() + |
11614 | 68 | " (with Greenwich prime meridian)"), |
11615 | 68 | geogCRSOfCompoundCRSDatum->ellipsoid(), |
11616 | 68 | util::optional<std::string>(), |
11617 | 68 | datum::PrimeMeridian::GREENWICH), |
11618 | 68 | EllipsoidalCS::createLongitudeLatitudeEllipsoidalHeight( |
11619 | 68 | UnitOfMeasure::DEGREE, UnitOfMeasure::METRE)); |
11620 | 68 | } |
11621 | 1.79k | } else { |
11622 | 50 | throw ParsingException("Unsupported value for geoid_crs: " |
11623 | 50 | "should be 'WGS84' or 'horizontal_crs'"); |
11624 | 50 | } |
11625 | 1.84k | } |
11626 | 5.89k | auto transformation = |
11627 | 5.89k | Transformation::createGravityRelatedHeightToGeographic3D( |
11628 | 5.89k | PropertyMap().set(IdentifiedObject::NAME_KEY, |
11629 | 5.89k | "unknown to " + geogCRS->nameStr() + |
11630 | 5.89k | " ellipsoidal height"), |
11631 | 5.89k | VerticalCRS::create(createMapWithUnknownName(), vdatum, |
11632 | 5.89k | VerticalCS::createGravityRelatedHeight( |
11633 | 5.89k | common::UnitOfMeasure::METRE)), |
11634 | 5.89k | geogCRS, nullptr, geoidgrids, |
11635 | 5.89k | std::vector<PositionalAccuracyNNPtr>()); |
11636 | 5.89k | auto boundvcrs = BoundCRS::create(vcrs, geogCRS, transformation); |
11637 | | |
11638 | 5.89k | crs = CompoundCRS::create(createMapWithUnknownName(), |
11639 | 5.89k | std::vector<CRSNNPtr>{crs, boundvcrs}); |
11640 | 5.89k | } |
11641 | | |
11642 | 14.8k | return crs; |
11643 | 14.8k | } |
11644 | | |
11645 | | // --------------------------------------------------------------------------- |
11646 | | |
11647 | | static double getAngularValue(const std::string ¶mValue, |
11648 | 2.63k | bool *pHasError = nullptr) { |
11649 | 2.63k | char *endptr = nullptr; |
11650 | 2.63k | double value = dmstor(paramValue.c_str(), &endptr) * RAD_TO_DEG; |
11651 | 2.63k | if (value == HUGE_VAL || endptr != paramValue.c_str() + paramValue.size()) { |
11652 | 192 | if (pHasError) |
11653 | 159 | *pHasError = true; |
11654 | 192 | return 0.0; |
11655 | 192 | } |
11656 | 2.44k | if (pHasError) |
11657 | 2.23k | *pHasError = false; |
11658 | 2.44k | return value; |
11659 | 2.63k | } |
11660 | | |
11661 | | // --------------------------------------------------------------------------- |
11662 | | |
11663 | 22.3k | static bool is_in_stringlist(const std::string &str, const char *stringlist) { |
11664 | 22.3k | if (str.empty()) |
11665 | 944 | return false; |
11666 | 21.3k | const char *haystack = stringlist; |
11667 | 254k | while (true) { |
11668 | 254k | const char *res = strstr(haystack, str.c_str()); |
11669 | 254k | if (res == nullptr) |
11670 | 18.7k | return false; |
11671 | 235k | if ((res == stringlist || res[-1] == ',') && |
11672 | 235k | (res[str.size()] == ',' || res[str.size()] == '\0')) |
11673 | 2.69k | return true; |
11674 | 232k | haystack += str.size(); |
11675 | 232k | } |
11676 | 21.3k | } |
11677 | | |
11678 | | // --------------------------------------------------------------------------- |
11679 | | |
11680 | | CRSNNPtr |
11681 | | PROJStringParser::Private::buildProjectedCRS(int iStep, |
11682 | | const GeodeticCRSNNPtr &geodCRS, |
11683 | 8.84k | int iUnitConvert, int iAxisSwap) { |
11684 | 8.84k | auto &step = steps_[iStep]; |
11685 | 8.84k | const auto mappings = getMappingsFromPROJName(step.name); |
11686 | 8.84k | const MethodMapping *mapping = mappings.empty() ? nullptr : mappings[0]; |
11687 | | |
11688 | 8.84k | bool foundStrictlyMatchingMapping = false; |
11689 | 8.84k | if (mappings.size() >= 2) { |
11690 | | // To distinguish for example +ortho from +ortho +f=0 |
11691 | 2.09k | bool allMappingsHaveAuxParam = true; |
11692 | 5.32k | for (const auto *mappingIter : mappings) { |
11693 | 5.32k | if (mappingIter->proj_name_aux == nullptr) { |
11694 | 3.55k | allMappingsHaveAuxParam = false; |
11695 | 3.55k | } |
11696 | 5.32k | if (mappingIter->proj_name_aux != nullptr && |
11697 | 5.32k | strchr(mappingIter->proj_name_aux, '=') == nullptr && |
11698 | 5.32k | hasParamValue(step, mappingIter->proj_name_aux)) { |
11699 | 33 | foundStrictlyMatchingMapping = true; |
11700 | 33 | mapping = mappingIter; |
11701 | 33 | break; |
11702 | 5.29k | } else if (mappingIter->proj_name_aux != nullptr && |
11703 | 5.29k | strchr(mappingIter->proj_name_aux, '=') != nullptr) { |
11704 | 1.24k | const auto tokens = split(mappingIter->proj_name_aux, '='); |
11705 | 1.24k | if (tokens.size() == 2 && |
11706 | 1.24k | getParamValue(step, tokens[0]) == tokens[1]) { |
11707 | 62 | foundStrictlyMatchingMapping = true; |
11708 | 62 | mapping = mappingIter; |
11709 | 62 | break; |
11710 | 62 | } |
11711 | 1.24k | } |
11712 | 5.32k | } |
11713 | 2.09k | if (allMappingsHaveAuxParam && !foundStrictlyMatchingMapping) { |
11714 | 37 | mapping = nullptr; |
11715 | 37 | } |
11716 | 2.09k | } |
11717 | | |
11718 | 8.84k | if (mapping && !foundStrictlyMatchingMapping) { |
11719 | 3.76k | mapping = selectSphericalOrEllipsoidal(mapping, geodCRS); |
11720 | 3.76k | } |
11721 | | |
11722 | 8.84k | assert(isProjectedStep(step.name)); |
11723 | 8.84k | assert(iUnitConvert < 0 || |
11724 | 8.84k | ci_equal(steps_[iUnitConvert].name, "unitconvert")); |
11725 | | |
11726 | 8.84k | const auto &title = title_; |
11727 | | |
11728 | 8.84k | if (!buildPrimeMeridian(step)->longitude()._isEquivalentTo( |
11729 | 8.84k | geodCRS->primeMeridian()->longitude(), |
11730 | 8.84k | util::IComparable::Criterion::EQUIVALENT)) { |
11731 | 2 | throw ParsingException("inconsistent pm values between projectedCRS " |
11732 | 2 | "and its base geographicalCRS"); |
11733 | 2 | } |
11734 | | |
11735 | 8.84k | auto axisType = AxisType::REGULAR; |
11736 | 8.84k | bool bWebMercator = false; |
11737 | 8.84k | std::string webMercatorName("WGS 84 / Pseudo-Mercator"); |
11738 | | |
11739 | 8.84k | if (step.name == "tmerc" && |
11740 | 8.84k | ((getParamValue(step, "axis") == "wsu" && iAxisSwap < 0) || |
11741 | 313 | (iAxisSwap > 0 && |
11742 | 301 | getParamValue(steps_[iAxisSwap], "order") == "-1,-2"))) { |
11743 | 24 | mapping = |
11744 | 24 | getMapping(EPSG_CODE_METHOD_TRANSVERSE_MERCATOR_SOUTH_ORIENTATED); |
11745 | 8.81k | } else if (step.name == "etmerc") { |
11746 | 8 | mapping = getMapping(EPSG_CODE_METHOD_TRANSVERSE_MERCATOR); |
11747 | 8.81k | } else if (step.name == "lcc") { |
11748 | 89 | const auto &lat_0 = getParamValue(step, "lat_0"); |
11749 | 89 | const auto &lat_1 = getParamValue(step, "lat_1"); |
11750 | 89 | const auto &lat_2 = getParamValue(step, "lat_2"); |
11751 | 89 | const auto &k = getParamValueK(step); |
11752 | 89 | if (lat_2.empty() && !lat_0.empty() && !lat_1.empty()) { |
11753 | 15 | if (lat_0 == lat_1 || |
11754 | | // For some reason with gcc 5.3.1-14ubuntu2 32bit, the following |
11755 | | // comparison returns false even if lat_0 == lat_1. Smells like |
11756 | | // a compiler bug |
11757 | 15 | getAngularValue(lat_0) == getAngularValue(lat_1)) { |
11758 | 11 | mapping = |
11759 | 11 | getMapping(EPSG_CODE_METHOD_LAMBERT_CONIC_CONFORMAL_1SP); |
11760 | 11 | } else { |
11761 | 4 | mapping = getMapping( |
11762 | 4 | EPSG_CODE_METHOD_LAMBERT_CONIC_CONFORMAL_1SP_VARIANT_B); |
11763 | 4 | } |
11764 | 74 | } else if (!k.empty() && getNumericValue(k) != 1.0) { |
11765 | 4 | mapping = getMapping( |
11766 | 4 | EPSG_CODE_METHOD_LAMBERT_CONIC_CONFORMAL_2SP_MICHIGAN); |
11767 | 70 | } else { |
11768 | 70 | mapping = getMapping(EPSG_CODE_METHOD_LAMBERT_CONIC_CONFORMAL_2SP); |
11769 | 70 | } |
11770 | 8.72k | } else if (step.name == "aeqd" && hasParamValue(step, "guam")) { |
11771 | 14 | mapping = getMapping(EPSG_CODE_METHOD_GUAM_PROJECTION); |
11772 | 8.70k | } else if (step.name == "geos" && getParamValue(step, "sweep") == "x") { |
11773 | 8 | mapping = |
11774 | 8 | getMapping(PROJ_WKT2_NAME_METHOD_GEOSTATIONARY_SATELLITE_SWEEP_X); |
11775 | 8.70k | } else if (step.name == "geos") { |
11776 | 7 | mapping = |
11777 | 7 | getMapping(PROJ_WKT2_NAME_METHOD_GEOSTATIONARY_SATELLITE_SWEEP_Y); |
11778 | 8.69k | } else if (step.name == "omerc") { |
11779 | 54 | if (hasParamValue(step, "no_rot")) { |
11780 | 0 | mapping = nullptr; |
11781 | 54 | } else if (hasParamValue(step, "no_uoff") || |
11782 | 54 | hasParamValue(step, "no_off")) { |
11783 | 6 | mapping = |
11784 | 6 | getMapping(EPSG_CODE_METHOD_HOTINE_OBLIQUE_MERCATOR_VARIANT_A); |
11785 | 48 | } else if (hasParamValue(step, "lat_1") && |
11786 | 48 | hasParamValue(step, "lon_1") && |
11787 | 48 | hasParamValue(step, "lat_2") && |
11788 | 48 | hasParamValue(step, "lon_2")) { |
11789 | 1 | mapping = getMapping( |
11790 | 1 | PROJ_WKT2_NAME_METHOD_HOTINE_OBLIQUE_MERCATOR_TWO_POINT_NATURAL_ORIGIN); |
11791 | 47 | } else { |
11792 | 47 | mapping = |
11793 | 47 | getMapping(EPSG_CODE_METHOD_HOTINE_OBLIQUE_MERCATOR_VARIANT_B); |
11794 | 47 | } |
11795 | 8.63k | } else if (step.name == "somerc") { |
11796 | 52 | mapping = |
11797 | 52 | getMapping(EPSG_CODE_METHOD_HOTINE_OBLIQUE_MERCATOR_VARIANT_B); |
11798 | 52 | if (!hasParamValue(step, "alpha") && !hasParamValue(step, "gamma") && |
11799 | 52 | !hasParamValue(step, "lonc")) { |
11800 | 27 | step.paramValues.emplace_back(Step::KeyValue("alpha", "90")); |
11801 | 27 | step.paramValues.emplace_back(Step::KeyValue("gamma", "90")); |
11802 | 27 | step.paramValues.emplace_back( |
11803 | 27 | Step::KeyValue("lonc", getParamValue(step, "lon_0"))); |
11804 | 27 | } |
11805 | 8.58k | } else if (step.name == "krovak" && |
11806 | 8.58k | ((iAxisSwap < 0 && getParamValue(step, "axis") == "swu" && |
11807 | 685 | !hasParamValue(step, "czech")) || |
11808 | 685 | (iAxisSwap > 0 && |
11809 | 667 | getParamValue(steps_[iAxisSwap], "order") == "-2,-1" && |
11810 | 667 | !hasParamValue(step, "czech")))) { |
11811 | 18 | mapping = getMapping(EPSG_CODE_METHOD_KROVAK); |
11812 | 8.56k | } else if (step.name == "krovak" && iAxisSwap < 0 && |
11813 | 8.56k | hasParamValue(step, "czech") && !hasParamValue(step, "axis")) { |
11814 | 8 | mapping = getMapping(EPSG_CODE_METHOD_KROVAK); |
11815 | 8.56k | } else if (step.name == "mod_krovak" && |
11816 | 8.56k | ((iAxisSwap < 0 && getParamValue(step, "axis") == "swu" && |
11817 | 48 | !hasParamValue(step, "czech")) || |
11818 | 48 | (iAxisSwap > 0 && |
11819 | 48 | getParamValue(steps_[iAxisSwap], "order") == "-2,-1" && |
11820 | 48 | !hasParamValue(step, "czech")))) { |
11821 | 0 | mapping = getMapping(EPSG_CODE_METHOD_KROVAK_MODIFIED); |
11822 | 8.56k | } else if (step.name == "mod_krovak" && iAxisSwap < 0 && |
11823 | 8.56k | hasParamValue(step, "czech") && !hasParamValue(step, "axis")) { |
11824 | 16 | mapping = getMapping(EPSG_CODE_METHOD_KROVAK_MODIFIED); |
11825 | 8.54k | } else if (step.name == "merc") { |
11826 | 159 | if (hasParamValue(step, "a") && hasParamValue(step, "b") && |
11827 | 159 | getParamValue(step, "a") == getParamValue(step, "b") && |
11828 | 159 | (!hasParamValue(step, "lat_ts") || |
11829 | 8 | getAngularValue(getParamValue(step, "lat_ts")) == 0.0) && |
11830 | 159 | getNumericValue(getParamValueK(step)) == 1.0 && |
11831 | 159 | getParamValue(step, "nadgrids") == "@null") { |
11832 | 0 | mapping = getMapping( |
11833 | 0 | EPSG_CODE_METHOD_POPULAR_VISUALISATION_PSEUDO_MERCATOR); |
11834 | 0 | for (size_t i = 0; i < step.paramValues.size(); ++i) { |
11835 | 0 | if (ci_equal(step.paramValues[i].key, "nadgrids")) { |
11836 | 0 | ignoreNadgrids_ = true; |
11837 | 0 | break; |
11838 | 0 | } |
11839 | 0 | } |
11840 | 0 | if (getNumericValue(getParamValue(step, "a")) == 6378137 && |
11841 | 0 | getAngularValue(getParamValue(step, "lon_0")) == 0.0 && |
11842 | 0 | getAngularValue(getParamValue(step, "lat_0")) == 0.0 && |
11843 | 0 | getAngularValue(getParamValue(step, "x_0")) == 0.0 && |
11844 | 0 | getAngularValue(getParamValue(step, "y_0")) == 0.0) { |
11845 | 0 | bWebMercator = true; |
11846 | 0 | if (hasParamValue(step, "units") && |
11847 | 0 | getParamValue(step, "units") != "m") { |
11848 | 0 | webMercatorName += |
11849 | 0 | " (unit " + getParamValue(step, "units") + ')'; |
11850 | 0 | } |
11851 | 0 | } |
11852 | 159 | } else if (hasParamValue(step, "lat_ts")) { |
11853 | 28 | if (hasParamValue(step, "R_C") && |
11854 | 28 | !geodCRS->ellipsoid()->isSphere() && |
11855 | 28 | getAngularValue(getParamValue(step, "lat_ts")) != 0) { |
11856 | 0 | throw ParsingException("lat_ts != 0 not supported for " |
11857 | 0 | "spherical Mercator on an ellipsoid"); |
11858 | 0 | } |
11859 | 28 | mapping = getMapping(EPSG_CODE_METHOD_MERCATOR_VARIANT_B); |
11860 | 131 | } else if (hasParamValue(step, "R_C")) { |
11861 | 9 | const auto &k = getParamValueK(step); |
11862 | 9 | if (!k.empty() && getNumericValue(k) != 1.0) { |
11863 | 2 | if (geodCRS->ellipsoid()->isSphere()) { |
11864 | 0 | mapping = getMapping(EPSG_CODE_METHOD_MERCATOR_VARIANT_A); |
11865 | 2 | } else { |
11866 | 2 | throw ParsingException( |
11867 | 2 | "k_0 != 1 not supported for spherical Mercator on an " |
11868 | 2 | "ellipsoid"); |
11869 | 2 | } |
11870 | 7 | } else { |
11871 | 7 | mapping = getMapping(EPSG_CODE_METHOD_MERCATOR_SPHERICAL); |
11872 | 7 | } |
11873 | 122 | } else { |
11874 | 122 | mapping = getMapping(EPSG_CODE_METHOD_MERCATOR_VARIANT_A); |
11875 | 122 | } |
11876 | 8.38k | } else if (step.name == "stere") { |
11877 | 234 | if (hasParamValue(step, "lat_0") && |
11878 | 234 | std::fabs(std::fabs(getAngularValue(getParamValue(step, "lat_0"))) - |
11879 | 94 | 90.0) < 1e-10) { |
11880 | 74 | const double lat_0 = getAngularValue(getParamValue(step, "lat_0")); |
11881 | 74 | if (lat_0 > 0) { |
11882 | 57 | axisType = AxisType::NORTH_POLE; |
11883 | 57 | } else { |
11884 | 17 | axisType = AxisType::SOUTH_POLE; |
11885 | 17 | } |
11886 | 74 | const auto &lat_ts = getParamValue(step, "lat_ts"); |
11887 | 74 | const auto &k = getParamValueK(step); |
11888 | 74 | if (!lat_ts.empty() && |
11889 | 74 | std::fabs(getAngularValue(lat_ts) - lat_0) > 1e-10 && |
11890 | 74 | !k.empty() && std::fabs(getNumericValue(k) - 1) > 1e-10) { |
11891 | 5 | throw ParsingException("lat_ts != lat_0 and k != 1 not " |
11892 | 5 | "supported for Polar Stereographic"); |
11893 | 5 | } |
11894 | 69 | if (!lat_ts.empty() && |
11895 | 69 | (k.empty() || std::fabs(getNumericValue(k) - 1) < 1e-10)) { |
11896 | 21 | mapping = |
11897 | 21 | getMapping(EPSG_CODE_METHOD_POLAR_STEREOGRAPHIC_VARIANT_B); |
11898 | 48 | } else { |
11899 | 48 | mapping = |
11900 | 48 | getMapping(EPSG_CODE_METHOD_POLAR_STEREOGRAPHIC_VARIANT_A); |
11901 | 48 | } |
11902 | 160 | } else { |
11903 | 160 | mapping = getMapping(PROJ_WKT2_NAME_METHOD_STEREOGRAPHIC); |
11904 | 160 | } |
11905 | 8.15k | } else if (step.name == "laea") { |
11906 | 185 | if (hasParamValue(step, "lat_0") && |
11907 | 185 | std::fabs(std::fabs(getAngularValue(getParamValue(step, "lat_0"))) - |
11908 | 10 | 90.0) < 1e-10) { |
11909 | 6 | const double lat_0 = getAngularValue(getParamValue(step, "lat_0")); |
11910 | 6 | if (lat_0 > 0) { |
11911 | 4 | axisType = AxisType::NORTH_POLE; |
11912 | 4 | } else { |
11913 | 2 | axisType = AxisType::SOUTH_POLE; |
11914 | 2 | } |
11915 | 6 | } |
11916 | 7.96k | } else if (step.name == "ortho") { |
11917 | 63 | const std::string &k = getParamValueK(step); |
11918 | 63 | if ((!k.empty() && getNumericValue(k) != 1.0) || |
11919 | 63 | (hasParamValue(step, "alpha") && |
11920 | 63 | getNumericValue(getParamValue(step, "alpha")) != 0.0)) { |
11921 | 4 | mapping = getMapping(EPSG_CODE_METHOD_LOCAL_ORTHOGRAPHIC); |
11922 | 4 | } |
11923 | 63 | } |
11924 | | |
11925 | 8.83k | UnitOfMeasure unit = buildUnit(step, "units", "to_meter"); |
11926 | 8.83k | if (iUnitConvert >= 0) { |
11927 | 24 | auto &stepUnitConvert = steps_[iUnitConvert]; |
11928 | 24 | const std::string *xy_in = &getParamValue(stepUnitConvert, "xy_in"); |
11929 | 24 | const std::string *xy_out = &getParamValue(stepUnitConvert, "xy_out"); |
11930 | 24 | if (stepUnitConvert.inverted) { |
11931 | 1 | std::swap(xy_in, xy_out); |
11932 | 1 | } |
11933 | 24 | if (xy_in->empty() || xy_out->empty() || *xy_in != "m") { |
11934 | 24 | if (step.name != "ob_tran") { |
11935 | 18 | throw ParsingException( |
11936 | 18 | "unhandled values for xy_in and/or xy_out"); |
11937 | 18 | } |
11938 | 24 | } |
11939 | | |
11940 | 6 | const LinearUnitDesc *unitsMatch = nullptr; |
11941 | 6 | try { |
11942 | 6 | double to_meter_value = c_locale_stod(*xy_out); |
11943 | 6 | unitsMatch = getLinearUnits(to_meter_value); |
11944 | 6 | if (unitsMatch == nullptr) { |
11945 | 0 | unit = _buildUnit(to_meter_value); |
11946 | 0 | } |
11947 | 6 | } catch (const std::invalid_argument &) { |
11948 | 6 | unitsMatch = getLinearUnits(*xy_out); |
11949 | 6 | if (!unitsMatch) { |
11950 | 6 | if (step.name != "ob_tran") { |
11951 | 0 | throw ParsingException( |
11952 | 0 | "unhandled values for xy_in and/or xy_out"); |
11953 | 0 | } |
11954 | 6 | } else { |
11955 | 0 | unit = _buildUnit(unitsMatch); |
11956 | 0 | } |
11957 | 6 | } |
11958 | 6 | } |
11959 | | |
11960 | 8.81k | ConversionPtr conv; |
11961 | | |
11962 | 8.81k | auto mapWithUnknownName = createMapWithUnknownName(); |
11963 | | |
11964 | 8.81k | if (step.name == "utm") { |
11965 | 57 | const int zone = std::atoi(getParamValue(step, "zone").c_str()); |
11966 | 57 | const bool north = !hasParamValue(step, "south"); |
11967 | 57 | conv = |
11968 | 57 | Conversion::createUTM(emptyPropertyMap, zone, north).as_nullable(); |
11969 | 8.76k | } else if (mapping) { |
11970 | | |
11971 | 3.90k | auto methodMap = |
11972 | 3.90k | PropertyMap().set(IdentifiedObject::NAME_KEY, mapping->wkt2_name); |
11973 | 3.90k | if (mapping->epsg_code) { |
11974 | 1.97k | methodMap.set(Identifier::CODESPACE_KEY, Identifier::EPSG) |
11975 | 1.97k | .set(Identifier::CODE_KEY, mapping->epsg_code); |
11976 | 1.97k | } |
11977 | 3.90k | std::vector<OperationParameterNNPtr> parameters; |
11978 | 3.90k | std::vector<ParameterValueNNPtr> values; |
11979 | 21.7k | for (int i = 0; mapping->params[i] != nullptr; i++) { |
11980 | 17.9k | const auto *param = mapping->params[i]; |
11981 | 17.9k | std::string proj_name(param->proj_name ? param->proj_name : ""); |
11982 | 17.9k | const std::string *paramValue = |
11983 | 17.9k | (proj_name == "k" || proj_name == "k_0") ? &getParamValueK(step) |
11984 | 17.9k | : !proj_name.empty() ? &getParamValue(step, proj_name) |
11985 | 16.4k | : &emptyString; |
11986 | 17.9k | double value = 0; |
11987 | 17.9k | if (!paramValue->empty()) { |
11988 | 444 | bool hasError = false; |
11989 | 444 | if (param->unit_type == UnitOfMeasure::Type::ANGULAR) { |
11990 | 401 | value = getAngularValue(*paramValue, &hasError); |
11991 | 401 | } else { |
11992 | 43 | value = getNumericValue(*paramValue, &hasError); |
11993 | 43 | } |
11994 | 444 | if (hasError) { |
11995 | 105 | throw ParsingException("invalid value for " + proj_name); |
11996 | 105 | } |
11997 | 444 | } |
11998 | | // For omerc, if gamma is missing, the default value is |
11999 | | // alpha |
12000 | 17.5k | else if (step.name == "omerc" && proj_name == "gamma") { |
12001 | 49 | paramValue = &getParamValue(step, "alpha"); |
12002 | 49 | if (!paramValue->empty()) { |
12003 | 0 | value = getAngularValue(*paramValue); |
12004 | 0 | } |
12005 | 17.4k | } else if (step.name == "krovak" || step.name == "mod_krovak") { |
12006 | | // Keep it in sync with defaults of krovak.cpp |
12007 | 5.06k | if (param->epsg_code == |
12008 | 5.06k | EPSG_CODE_PARAMETER_LATITUDE_PROJECTION_CENTRE) { |
12009 | 729 | value = 49.5; |
12010 | 4.33k | } else if (param->epsg_code == |
12011 | 4.33k | EPSG_CODE_PARAMETER_LONGITUDE_OF_ORIGIN) { |
12012 | 723 | value = 24.833333333333333333; |
12013 | 3.61k | } else if (param->epsg_code == |
12014 | 3.61k | EPSG_CODE_PARAMETER_COLATITUDE_CONE_AXIS) { |
12015 | 724 | value = 30.28813975277777776; |
12016 | 2.89k | } else if ( |
12017 | 2.89k | param->epsg_code == |
12018 | 2.89k | EPSG_CODE_PARAMETER_LATITUDE_PSEUDO_STANDARD_PARALLEL) { |
12019 | 724 | value = 78.5; |
12020 | 2.16k | } else if ( |
12021 | 2.16k | param->epsg_code == |
12022 | 2.16k | EPSG_CODE_PARAMETER_SCALE_FACTOR_PSEUDO_STANDARD_PARALLEL) { |
12023 | 724 | value = 0.9999; |
12024 | 724 | } |
12025 | 12.3k | } else if (step.name == "cea" && proj_name == "lat_ts") { |
12026 | 58 | paramValue = &getParamValueK(step); |
12027 | 58 | if (!paramValue->empty()) { |
12028 | 4 | bool hasError = false; |
12029 | 4 | const double k = getNumericValue(*paramValue, &hasError); |
12030 | 4 | if (hasError) { |
12031 | 3 | throw ParsingException("invalid value for k/k_0"); |
12032 | 3 | } |
12033 | 1 | if (k >= 0 && k <= 1) { |
12034 | 1 | const double es = |
12035 | 1 | geodCRS->ellipsoid()->squaredEccentricity(); |
12036 | 1 | if (es < 0 || es == 1) { |
12037 | 0 | throw ParsingException("Invalid flattening"); |
12038 | 0 | } |
12039 | 1 | value = |
12040 | 1 | Angle(acos(k * sqrt((1 - es) / (1 - k * k * es))), |
12041 | 1 | UnitOfMeasure::RADIAN) |
12042 | 1 | .convertToUnit(UnitOfMeasure::DEGREE); |
12043 | 1 | } else { |
12044 | 0 | throw ParsingException("k/k_0 should be in [0,1]"); |
12045 | 0 | } |
12046 | 1 | } |
12047 | 12.3k | } else if (param->unit_type == UnitOfMeasure::Type::SCALE) { |
12048 | 791 | value = 1; |
12049 | 11.5k | } else if (step.name == "peirce_q" && proj_name == "lat_0") { |
12050 | 21 | value = 90; |
12051 | 21 | } |
12052 | | |
12053 | 17.8k | PropertyMap propertiesParameter; |
12054 | 17.8k | propertiesParameter.set(IdentifiedObject::NAME_KEY, |
12055 | 17.8k | param->wkt2_name); |
12056 | 17.8k | if (param->epsg_code) { |
12057 | 17.2k | propertiesParameter.set(Identifier::CODE_KEY, param->epsg_code); |
12058 | 17.2k | propertiesParameter.set(Identifier::CODESPACE_KEY, |
12059 | 17.2k | Identifier::EPSG); |
12060 | 17.2k | } |
12061 | 17.8k | parameters.push_back( |
12062 | 17.8k | OperationParameter::create(propertiesParameter)); |
12063 | | // In PROJ convention, angular parameters are always in degree |
12064 | | // and linear parameters always in metre. |
12065 | 17.8k | double valRounded = |
12066 | 17.8k | param->unit_type == UnitOfMeasure::Type::LINEAR |
12067 | 17.8k | ? Length(value, UnitOfMeasure::METRE).convertToUnit(unit) |
12068 | 17.8k | : value; |
12069 | 17.8k | if (std::fabs(valRounded - std::round(valRounded)) < 1e-8) { |
12070 | 14.2k | valRounded = std::round(valRounded); |
12071 | 14.2k | } |
12072 | 17.8k | values.push_back(ParameterValue::create( |
12073 | 17.8k | Measure(valRounded, |
12074 | 17.8k | param->unit_type == UnitOfMeasure::Type::ANGULAR |
12075 | 17.8k | ? UnitOfMeasure::DEGREE |
12076 | 17.8k | : param->unit_type == UnitOfMeasure::Type::LINEAR ? unit |
12077 | 9.13k | : param->unit_type == UnitOfMeasure::Type::SCALE |
12078 | 1.51k | ? UnitOfMeasure::SCALE_UNITY |
12079 | 1.51k | : UnitOfMeasure::NONE))); |
12080 | 17.8k | } |
12081 | | |
12082 | 3.79k | if (step.name == "tmerc" && hasParamValue(step, "approx")) { |
12083 | 2 | methodMap.set("proj_method", "tmerc approx"); |
12084 | 3.79k | } else if (step.name == "utm" && hasParamValue(step, "approx")) { |
12085 | 0 | methodMap.set("proj_method", "utm approx"); |
12086 | 0 | } |
12087 | | |
12088 | 3.79k | conv = Conversion::create(mapWithUnknownName, methodMap, parameters, |
12089 | 3.79k | values) |
12090 | 3.79k | .as_nullable(); |
12091 | 4.86k | } else { |
12092 | 4.86k | std::vector<OperationParameterNNPtr> parameters; |
12093 | 4.86k | std::vector<ParameterValueNNPtr> values; |
12094 | 4.86k | std::string methodName = "PROJ " + step.name; |
12095 | 17.9k | for (const auto ¶m : step.paramValues) { |
12096 | 17.9k | if (is_in_stringlist(param.key, |
12097 | 17.9k | "wktext,no_defs,datum,ellps,a,b,R,f,rf," |
12098 | 17.9k | "towgs84,nadgrids,geoidgrids," |
12099 | 17.9k | "units,to_meter,vunits,vto_meter,type")) { |
12100 | 2.45k | continue; |
12101 | 2.45k | } |
12102 | 15.4k | if (param.value.empty()) { |
12103 | 7.43k | methodName += " " + param.key; |
12104 | 8.06k | } else if (isalpha(param.value[0])) { |
12105 | 5.84k | methodName += " " + param.key + "=" + param.value; |
12106 | 5.84k | } else { |
12107 | 2.22k | parameters.push_back(OperationParameter::create( |
12108 | 2.22k | PropertyMap().set(IdentifiedObject::NAME_KEY, param.key))); |
12109 | 2.22k | bool hasError = false; |
12110 | 2.22k | if (is_in_stringlist(param.key, "x_0,y_0,h,h_0")) { |
12111 | 69 | double value = getNumericValue(param.value, &hasError); |
12112 | 69 | values.push_back(ParameterValue::create( |
12113 | 69 | Measure(value, UnitOfMeasure::METRE))); |
12114 | 2.15k | } else if (is_in_stringlist( |
12115 | 2.15k | param.key, |
12116 | 2.15k | "k,k_0," |
12117 | 2.15k | "north_square,south_square," // rhealpix |
12118 | 2.15k | "n,m," // sinu |
12119 | 2.15k | "q," // urm5 |
12120 | 2.15k | "path,lsat," // lsat |
12121 | 2.15k | "W,M," // hammer |
12122 | 2.15k | "aperture,resolution," // isea |
12123 | 2.15k | )) { |
12124 | 164 | double value = getNumericValue(param.value, &hasError); |
12125 | 164 | values.push_back(ParameterValue::create( |
12126 | 164 | Measure(value, UnitOfMeasure::SCALE_UNITY))); |
12127 | 1.99k | } else { |
12128 | 1.99k | double value = getAngularValue(param.value, &hasError); |
12129 | 1.99k | values.push_back(ParameterValue::create( |
12130 | 1.99k | Measure(value, UnitOfMeasure::DEGREE))); |
12131 | 1.99k | } |
12132 | 2.22k | if (hasError) { |
12133 | 99 | throw ParsingException("invalid value for " + param.key); |
12134 | 99 | } |
12135 | 2.22k | } |
12136 | 15.4k | } |
12137 | 4.76k | conv = Conversion::create( |
12138 | 4.76k | mapWithUnknownName, |
12139 | 4.76k | PropertyMap().set(IdentifiedObject::NAME_KEY, methodName), |
12140 | 4.76k | parameters, values) |
12141 | 4.76k | .as_nullable(); |
12142 | | |
12143 | 4.76k | for (const char *substr : |
12144 | 4.76k | {"PROJ ob_tran o_proj=longlat", "PROJ ob_tran o_proj=lonlat", |
12145 | 16.7k | "PROJ ob_tran o_proj=latlon", "PROJ ob_tran o_proj=latlong"}) { |
12146 | 16.7k | if (starts_with(methodName, substr)) { |
12147 | 1.12k | auto geogCRS = |
12148 | 1.12k | util::nn_dynamic_pointer_cast<GeographicCRS>(geodCRS); |
12149 | 1.12k | if (geogCRS) { |
12150 | 1.11k | return DerivedGeographicCRS::create( |
12151 | 1.11k | PropertyMap().set(IdentifiedObject::NAME_KEY, |
12152 | 1.11k | "unnamed"), |
12153 | 1.11k | NN_NO_CHECK(geogCRS), NN_NO_CHECK(conv), |
12154 | 1.11k | buildEllipsoidalCS(iStep, iUnitConvert, iAxisSwap, |
12155 | 1.11k | false)); |
12156 | 1.11k | } |
12157 | 1.12k | } |
12158 | 16.7k | } |
12159 | 4.76k | } |
12160 | | |
12161 | 7.50k | std::vector<CoordinateSystemAxisNNPtr> axis = |
12162 | 7.50k | processAxisSwap(step, unit, iAxisSwap, axisType, false); |
12163 | | |
12164 | 7.50k | auto csGeodCRS = geodCRS->coordinateSystem(); |
12165 | 7.50k | auto cs = csGeodCRS->axisList().size() == 2 |
12166 | 7.50k | ? CartesianCS::create(emptyPropertyMap, axis[0], axis[1]) |
12167 | 7.50k | : CartesianCS::create(emptyPropertyMap, axis[0], axis[1], |
12168 | 1.28k | csGeodCRS->axisList()[2]); |
12169 | 7.50k | if (isTopocentricStep(step.name)) { |
12170 | 16 | cs = CartesianCS::create( |
12171 | 16 | emptyPropertyMap, |
12172 | 16 | createAxis("topocentric East", "U", AxisDirection::EAST, unit), |
12173 | 16 | createAxis("topocentric North", "V", AxisDirection::NORTH, unit), |
12174 | 16 | createAxis("topocentric Up", "W", AxisDirection::UP, unit)); |
12175 | 16 | } |
12176 | | |
12177 | 7.50k | auto props = PropertyMap().set(IdentifiedObject::NAME_KEY, |
12178 | 7.50k | title.empty() ? "unknown" : title); |
12179 | 7.50k | if (hasUnusedParameters(step)) { |
12180 | 1.17k | props.set("EXTENSION_PROJ4", projString_); |
12181 | 1.17k | } |
12182 | | |
12183 | 7.50k | props.set("IMPLICIT_CS", true); |
12184 | | |
12185 | 7.50k | CRSNNPtr crs = |
12186 | 7.50k | bWebMercator |
12187 | 7.50k | ? createPseudoMercator( |
12188 | 0 | props.set(IdentifiedObject::NAME_KEY, webMercatorName), cs) |
12189 | 7.50k | : ProjectedCRS::create(props, geodCRS, NN_NO_CHECK(conv), cs); |
12190 | | |
12191 | 7.50k | return crs; |
12192 | 8.81k | } |
12193 | | |
12194 | | //! @endcond |
12195 | | |
12196 | | // --------------------------------------------------------------------------- |
12197 | | |
12198 | | //! @cond Doxygen_Suppress |
12199 | | static const metadata::ExtentPtr nullExtent{}; |
12200 | | |
12201 | 0 | static const metadata::ExtentPtr &getExtent(const crs::CRS *crs) { |
12202 | 0 | const auto &domains = crs->domains(); |
12203 | 0 | if (!domains.empty()) { |
12204 | 0 | return domains[0]->domainOfValidity(); |
12205 | 0 | } |
12206 | 0 | return nullExtent; |
12207 | 0 | } |
12208 | | |
12209 | | //! @endcond |
12210 | | |
12211 | | namespace { |
12212 | | struct PJContextHolder { |
12213 | | PJ_CONTEXT *ctx_; |
12214 | | bool bFree_; |
12215 | | |
12216 | 504 | PJContextHolder(PJ_CONTEXT *ctx, bool bFree) : ctx_(ctx), bFree_(bFree) {} |
12217 | 504 | ~PJContextHolder() { |
12218 | 504 | if (bFree_) |
12219 | 1 | proj_context_destroy(ctx_); |
12220 | 504 | } |
12221 | | PJContextHolder(const PJContextHolder &) = delete; |
12222 | | PJContextHolder &operator=(const PJContextHolder &) = delete; |
12223 | | }; |
12224 | | } // namespace |
12225 | | |
12226 | | // --------------------------------------------------------------------------- |
12227 | | |
12228 | | /** \brief Instantiate a sub-class of BaseObject from a PROJ string. |
12229 | | * |
12230 | | * The projString must contain +type=crs for the object to be detected as a |
12231 | | * CRS instead of a CoordinateOperation. |
12232 | | * |
12233 | | * @throw ParsingException if the string cannot be parsed. |
12234 | | */ |
12235 | | BaseObjectNNPtr |
12236 | 11.7k | PROJStringParser::createFromPROJString(const std::string &projString) { |
12237 | | |
12238 | | // In some abnormal situations involving init=epsg:XXXX syntax, we could |
12239 | | // have infinite loop |
12240 | 11.7k | if (d->ctx_ && |
12241 | 11.7k | d->ctx_->projStringParserCreateFromPROJStringRecursionCounter == 2) { |
12242 | 0 | throw ParsingException( |
12243 | 0 | "Infinite recursion in PROJStringParser::createFromPROJString()"); |
12244 | 0 | } |
12245 | | |
12246 | 11.7k | d->steps_.clear(); |
12247 | 11.7k | d->title_.clear(); |
12248 | 11.7k | d->globalParamValues_.clear(); |
12249 | 11.7k | d->projString_ = projString; |
12250 | 11.7k | PROJStringSyntaxParser(projString, d->steps_, d->globalParamValues_, |
12251 | 11.7k | d->title_); |
12252 | | |
12253 | 11.7k | if (d->steps_.empty()) { |
12254 | 216 | const auto &vunits = d->getGlobalParamValue("vunits"); |
12255 | 216 | const auto &vto_meter = d->getGlobalParamValue("vto_meter"); |
12256 | 216 | if (!vunits.empty() || !vto_meter.empty()) { |
12257 | 52 | Step fakeStep; |
12258 | 52 | if (!vunits.empty()) { |
12259 | 48 | fakeStep.paramValues.emplace_back( |
12260 | 48 | Step::KeyValue("vunits", vunits)); |
12261 | 48 | } |
12262 | 52 | if (!vto_meter.empty()) { |
12263 | 4 | fakeStep.paramValues.emplace_back( |
12264 | 4 | Step::KeyValue("vto_meter", vto_meter)); |
12265 | 4 | } |
12266 | 52 | auto vdatum = |
12267 | 52 | VerticalReferenceFrame::create(createMapWithUnknownName()); |
12268 | 52 | auto vcrs = VerticalCRS::create( |
12269 | 52 | createMapWithUnknownName(), vdatum, |
12270 | 52 | VerticalCS::createGravityRelatedHeight( |
12271 | 52 | d->buildUnit(fakeStep, "vunits", "vto_meter"))); |
12272 | 52 | return vcrs; |
12273 | 52 | } |
12274 | 216 | } |
12275 | | |
12276 | 11.6k | const bool isGeocentricCRS = |
12277 | 11.6k | ((d->steps_.size() == 1 && |
12278 | 11.6k | d->getParamValue(d->steps_[0], "type") == "crs") || |
12279 | 11.6k | (d->steps_.size() == 2 && d->steps_[1].name == "unitconvert")) && |
12280 | 11.6k | !d->steps_[0].inverted && isGeocentricStep(d->steps_[0].name); |
12281 | | |
12282 | 11.6k | const bool isTopocentricCRS = |
12283 | 11.6k | (d->steps_.size() == 1 && isTopocentricStep(d->steps_[0].name) && |
12284 | 11.6k | d->getParamValue(d->steps_[0], "type") == "crs"); |
12285 | | |
12286 | | // +init=xxxx:yyyy syntax |
12287 | 11.6k | if (d->steps_.size() == 1 && d->steps_[0].isInit && |
12288 | 11.6k | !d->steps_[0].inverted) { |
12289 | | |
12290 | 504 | auto ctx = d->ctx_ ? d->ctx_ : proj_context_create(); |
12291 | 504 | if (!ctx) { |
12292 | 0 | throw ParsingException("out of memory"); |
12293 | 0 | } |
12294 | 504 | PJContextHolder contextHolder(ctx, ctx != d->ctx_); |
12295 | | |
12296 | | // Those used to come from a text init file |
12297 | | // We only support them in compatibility mode |
12298 | 504 | const std::string &stepName = d->steps_[0].name; |
12299 | 504 | if (ci_starts_with(stepName, "epsg:") || |
12300 | 504 | ci_starts_with(stepName, "IGNF:")) { |
12301 | | |
12302 | 3 | struct BackupContextErrno { |
12303 | 3 | PJ_CONTEXT *m_ctxt = nullptr; |
12304 | 3 | int m_last_errno = 0; |
12305 | | |
12306 | 3 | explicit BackupContextErrno(PJ_CONTEXT *ctxtIn) |
12307 | 3 | : m_ctxt(ctxtIn), m_last_errno(m_ctxt->last_errno) { |
12308 | 3 | m_ctxt->debug_level = PJ_LOG_ERROR; |
12309 | 3 | } |
12310 | | |
12311 | 3 | ~BackupContextErrno() { m_ctxt->last_errno = m_last_errno; } |
12312 | | |
12313 | 3 | BackupContextErrno(const BackupContextErrno &) = delete; |
12314 | 3 | BackupContextErrno & |
12315 | 3 | operator=(const BackupContextErrno &) = delete; |
12316 | 3 | }; |
12317 | | |
12318 | 3 | BackupContextErrno backupContextErrno(ctx); |
12319 | | |
12320 | 3 | bool usePROJ4InitRules = d->usePROJ4InitRules_; |
12321 | 3 | if (!usePROJ4InitRules) { |
12322 | 3 | usePROJ4InitRules = |
12323 | 3 | proj_context_get_use_proj4_init_rules(ctx, FALSE) == TRUE; |
12324 | 3 | } |
12325 | 3 | if (!usePROJ4InitRules) { |
12326 | 3 | throw ParsingException("init=epsg:/init=IGNF: syntax not " |
12327 | 3 | "supported in non-PROJ4 emulation mode"); |
12328 | 3 | } |
12329 | | |
12330 | 0 | char unused[256]; |
12331 | 0 | std::string initname(stepName); |
12332 | 0 | initname.resize(initname.find(':')); |
12333 | 0 | int file_found = |
12334 | 0 | pj_find_file(ctx, initname.c_str(), unused, sizeof(unused)); |
12335 | |
|
12336 | 0 | if (!file_found) { |
12337 | 0 | auto obj = createFromUserInput(stepName, d->dbContext_, true); |
12338 | 0 | auto crs = dynamic_cast<CRS *>(obj.get()); |
12339 | |
|
12340 | 0 | bool hasSignificantParamValues = false; |
12341 | 0 | bool hasOver = false; |
12342 | 0 | for (const auto &kv : d->steps_[0].paramValues) { |
12343 | 0 | if (kv.key == "over") { |
12344 | 0 | hasOver = true; |
12345 | 0 | } else if (!((kv.key == "type" && kv.value == "crs") || |
12346 | 0 | kv.key == "wktext" || kv.key == "no_defs")) { |
12347 | 0 | hasSignificantParamValues = true; |
12348 | 0 | break; |
12349 | 0 | } |
12350 | 0 | } |
12351 | |
|
12352 | 0 | if (crs && !hasSignificantParamValues) { |
12353 | 0 | PropertyMap properties; |
12354 | 0 | properties.set(IdentifiedObject::NAME_KEY, |
12355 | 0 | d->title_.empty() ? crs->nameStr() |
12356 | 0 | : d->title_); |
12357 | 0 | if (hasOver) { |
12358 | 0 | properties.set("OVER", true); |
12359 | 0 | } |
12360 | 0 | const auto &extent = getExtent(crs); |
12361 | 0 | if (extent) { |
12362 | 0 | properties.set( |
12363 | 0 | common::ObjectUsage::DOMAIN_OF_VALIDITY_KEY, |
12364 | 0 | NN_NO_CHECK(extent)); |
12365 | 0 | } |
12366 | 0 | auto geogCRS = dynamic_cast<GeographicCRS *>(crs); |
12367 | 0 | if (geogCRS) { |
12368 | 0 | const auto &cs = geogCRS->coordinateSystem(); |
12369 | | // Override with longitude latitude in degrees |
12370 | 0 | return GeographicCRS::create( |
12371 | 0 | properties, geogCRS->datum(), |
12372 | 0 | geogCRS->datumEnsemble(), |
12373 | 0 | cs->axisList().size() == 2 |
12374 | 0 | ? EllipsoidalCS::createLongitudeLatitude( |
12375 | 0 | UnitOfMeasure::DEGREE) |
12376 | 0 | : EllipsoidalCS:: |
12377 | 0 | createLongitudeLatitudeEllipsoidalHeight( |
12378 | 0 | UnitOfMeasure::DEGREE, |
12379 | 0 | cs->axisList()[2]->unit())); |
12380 | 0 | } |
12381 | 0 | auto projCRS = dynamic_cast<ProjectedCRS *>(crs); |
12382 | 0 | if (projCRS) { |
12383 | | // Override with easting northing order |
12384 | 0 | const auto conv = projCRS->derivingConversion(); |
12385 | 0 | if (conv->method()->getEPSGCode() != |
12386 | 0 | EPSG_CODE_METHOD_TRANSVERSE_MERCATOR_SOUTH_ORIENTATED) { |
12387 | 0 | return ProjectedCRS::create( |
12388 | 0 | properties, projCRS->baseCRS(), conv, |
12389 | 0 | CartesianCS::createEastingNorthing( |
12390 | 0 | projCRS->coordinateSystem() |
12391 | 0 | ->axisList()[0] |
12392 | 0 | ->unit())); |
12393 | 0 | } |
12394 | 0 | } |
12395 | 0 | return obj; |
12396 | 0 | } |
12397 | 0 | auto projStringExportable = |
12398 | 0 | dynamic_cast<IPROJStringExportable *>(crs); |
12399 | 0 | if (projStringExportable) { |
12400 | 0 | std::string expanded; |
12401 | 0 | if (!d->title_.empty()) { |
12402 | 0 | expanded = "title="; |
12403 | 0 | expanded += |
12404 | 0 | pj_double_quote_string_param_if_needed(d->title_); |
12405 | 0 | } |
12406 | 0 | for (const auto &pair : d->steps_[0].paramValues) { |
12407 | 0 | if (!expanded.empty()) |
12408 | 0 | expanded += ' '; |
12409 | 0 | expanded += '+'; |
12410 | 0 | expanded += pair.key; |
12411 | 0 | if (!pair.value.empty()) { |
12412 | 0 | expanded += '='; |
12413 | 0 | expanded += pj_double_quote_string_param_if_needed( |
12414 | 0 | pair.value); |
12415 | 0 | } |
12416 | 0 | } |
12417 | 0 | expanded += ' '; |
12418 | 0 | expanded += projStringExportable->exportToPROJString( |
12419 | 0 | PROJStringFormatter::create().get()); |
12420 | 0 | return createFromPROJString(expanded); |
12421 | 0 | } |
12422 | 0 | } |
12423 | 0 | } |
12424 | | |
12425 | 501 | paralist *init = pj_mkparam(("init=" + d->steps_[0].name).c_str()); |
12426 | 501 | if (!init) { |
12427 | 0 | throw ParsingException("out of memory"); |
12428 | 0 | } |
12429 | 501 | ctx->projStringParserCreateFromPROJStringRecursionCounter++; |
12430 | 501 | paralist *list = pj_expand_init(ctx, init); |
12431 | 501 | ctx->projStringParserCreateFromPROJStringRecursionCounter--; |
12432 | 501 | if (!list) { |
12433 | 126 | free(init); |
12434 | 126 | throw ParsingException("cannot expand " + projString); |
12435 | 126 | } |
12436 | 375 | std::string expanded; |
12437 | 375 | if (!d->title_.empty()) { |
12438 | 84 | expanded = |
12439 | 84 | "title=" + pj_double_quote_string_param_if_needed(d->title_); |
12440 | 84 | } |
12441 | 375 | bool first = true; |
12442 | 375 | bool has_init_term = false; |
12443 | 2.99k | for (auto t = list; t;) { |
12444 | 2.61k | if (!expanded.empty()) { |
12445 | 2.03k | expanded += ' '; |
12446 | 2.03k | } |
12447 | 2.61k | if (first) { |
12448 | | // first parameter is the init= itself |
12449 | 375 | first = false; |
12450 | 2.24k | } else if (starts_with(t->param, "init=")) { |
12451 | 0 | has_init_term = true; |
12452 | 2.24k | } else { |
12453 | 2.24k | expanded += t->param; |
12454 | 2.24k | } |
12455 | | |
12456 | 2.61k | auto n = t->next; |
12457 | 2.61k | free(t); |
12458 | 2.61k | t = n; |
12459 | 2.61k | } |
12460 | 8.06k | for (const auto &pair : d->steps_[0].paramValues) { |
12461 | 8.06k | expanded += " +"; |
12462 | 8.06k | expanded += pair.key; |
12463 | 8.06k | if (!pair.value.empty()) { |
12464 | 5.57k | expanded += '='; |
12465 | 5.57k | expanded += pj_double_quote_string_param_if_needed(pair.value); |
12466 | 5.57k | } |
12467 | 8.06k | } |
12468 | | |
12469 | 375 | if (!has_init_term) { |
12470 | 375 | return createFromPROJString(expanded); |
12471 | 375 | } |
12472 | 375 | } |
12473 | | |
12474 | 11.1k | int iFirstGeogStep = -1; |
12475 | 11.1k | int iSecondGeogStep = -1; |
12476 | 11.1k | int iProjStep = -1; |
12477 | 11.1k | int iFirstUnitConvert = -1; |
12478 | 11.1k | int iSecondUnitConvert = -1; |
12479 | 11.1k | int iFirstAxisSwap = -1; |
12480 | 11.1k | int iSecondAxisSwap = -1; |
12481 | 11.1k | bool unexpectedStructure = d->steps_.empty(); |
12482 | 22.6k | for (int i = 0; i < static_cast<int>(d->steps_.size()); i++) { |
12483 | 12.4k | const auto &stepName = d->steps_[i].name; |
12484 | 12.4k | if (isGeographicStep(stepName)) { |
12485 | 3.53k | if (iFirstGeogStep < 0) { |
12486 | 3.50k | iFirstGeogStep = i; |
12487 | 3.50k | } else if (iSecondGeogStep < 0) { |
12488 | 22 | iSecondGeogStep = i; |
12489 | 22 | } else { |
12490 | 3 | unexpectedStructure = true; |
12491 | 3 | break; |
12492 | 3 | } |
12493 | 8.87k | } else if (ci_equal(stepName, "unitconvert")) { |
12494 | 180 | if (iFirstUnitConvert < 0) { |
12495 | 145 | iFirstUnitConvert = i; |
12496 | 145 | } else if (iSecondUnitConvert < 0) { |
12497 | 30 | iSecondUnitConvert = i; |
12498 | 30 | } else { |
12499 | 5 | unexpectedStructure = true; |
12500 | 5 | break; |
12501 | 5 | } |
12502 | 8.69k | } else if (ci_equal(stepName, "axisswap")) { |
12503 | 123 | if (iFirstAxisSwap < 0) { |
12504 | 109 | iFirstAxisSwap = i; |
12505 | 109 | } else if (iSecondAxisSwap < 0) { |
12506 | 10 | iSecondAxisSwap = i; |
12507 | 10 | } else { |
12508 | 4 | unexpectedStructure = true; |
12509 | 4 | break; |
12510 | 4 | } |
12511 | 8.57k | } else if (isProjectedStep(stepName)) { |
12512 | 7.92k | if (iProjStep >= 0) { |
12513 | 223 | unexpectedStructure = true; |
12514 | 223 | break; |
12515 | 223 | } |
12516 | 7.69k | iProjStep = i; |
12517 | 7.69k | } else { |
12518 | 650 | unexpectedStructure = true; |
12519 | 650 | break; |
12520 | 650 | } |
12521 | 12.4k | } |
12522 | | |
12523 | 11.1k | if (!d->steps_.empty()) { |
12524 | | // CRS candidate |
12525 | 10.9k | if ((d->steps_.size() == 1 && |
12526 | 10.9k | d->getParamValue(d->steps_[0], "type") != "crs") || |
12527 | 10.9k | (d->steps_.size() > 1 && d->getGlobalParamValue("type") != "crs")) { |
12528 | 2.24k | unexpectedStructure = true; |
12529 | 2.24k | } |
12530 | 10.9k | } |
12531 | | |
12532 | 11.1k | struct Logger { |
12533 | 11.1k | std::string msg{}; |
12534 | | |
12535 | | // cppcheck-suppress functionStatic |
12536 | 11.1k | void setMessage(const char *msgIn) noexcept { |
12537 | 2.16k | try { |
12538 | 2.16k | msg = msgIn; |
12539 | 2.16k | } catch (const std::exception &) { |
12540 | 0 | } |
12541 | 2.16k | } |
12542 | | |
12543 | 11.1k | static void log(void *user_data, int level, const char *msg) { |
12544 | 2.16k | if (level == PJ_LOG_ERROR) { |
12545 | 2.16k | static_cast<Logger *>(user_data)->setMessage(msg); |
12546 | 2.16k | } |
12547 | 2.16k | } |
12548 | 11.1k | }; |
12549 | | |
12550 | | // If the structure is not recognized, then try to instantiate the |
12551 | | // pipeline, and if successful, wrap it in a PROJBasedOperation |
12552 | 11.1k | Logger logger; |
12553 | 11.1k | bool valid; |
12554 | | |
12555 | 11.1k | auto pj_context = d->ctx_ ? d->ctx_ : proj_context_create(); |
12556 | 11.1k | if (!pj_context) { |
12557 | 0 | throw ParsingException("out of memory"); |
12558 | 0 | } |
12559 | | |
12560 | | // Backup error logger and level, and install temporary handler |
12561 | 11.1k | auto old_logger = pj_context->logger; |
12562 | 11.1k | auto old_logger_app_data = pj_context->logger_app_data; |
12563 | 11.1k | auto log_level = proj_log_level(pj_context, PJ_LOG_ERROR); |
12564 | 11.1k | proj_log_func(pj_context, &logger, Logger::log); |
12565 | | |
12566 | 11.1k | if (pj_context != d->ctx_) { |
12567 | 100 | proj_context_use_proj4_init_rules(pj_context, d->usePROJ4InitRules_); |
12568 | 100 | } |
12569 | 11.1k | pj_context->projStringParserCreateFromPROJStringRecursionCounter++; |
12570 | 11.1k | auto pj = pj_create_internal( |
12571 | 11.1k | pj_context, (projString.find("type=crs") != std::string::npos |
12572 | 11.1k | ? projString + " +disable_grid_presence_check" |
12573 | 11.1k | : projString) |
12574 | 11.1k | .c_str()); |
12575 | 11.1k | pj_context->projStringParserCreateFromPROJStringRecursionCounter--; |
12576 | 11.1k | valid = pj != nullptr; |
12577 | | |
12578 | | // Restore initial error logger and level |
12579 | 11.1k | proj_log_level(pj_context, log_level); |
12580 | 11.1k | pj_context->logger = old_logger; |
12581 | 11.1k | pj_context->logger_app_data = old_logger_app_data; |
12582 | | |
12583 | | // Remove parameters not understood by PROJ. |
12584 | 11.1k | if (valid && d->steps_.size() == 1) { |
12585 | 8.43k | std::vector<Step::KeyValue> newParamValues{}; |
12586 | 8.43k | std::set<std::string> foundKeys; |
12587 | 8.43k | auto &step = d->steps_[0]; |
12588 | | |
12589 | 46.2k | for (auto &kv : step.paramValues) { |
12590 | 46.2k | bool recognizedByPROJ = false; |
12591 | 46.2k | if (foundKeys.find(kv.key) != foundKeys.end()) { |
12592 | 3.20k | continue; |
12593 | 3.20k | } |
12594 | 43.0k | foundKeys.insert(kv.key); |
12595 | 43.0k | if ((step.name == "krovak" || step.name == "mod_krovak") && |
12596 | 43.0k | kv.key == "alpha") { |
12597 | | // We recognize it in our CRS parsing code |
12598 | 4 | recognizedByPROJ = true; |
12599 | 43.0k | } else { |
12600 | 337k | for (auto cur = pj->params; cur; cur = cur->next) { |
12601 | 337k | const char *equal = strchr(cur->param, '='); |
12602 | 337k | if (equal && static_cast<size_t>(equal - cur->param) == |
12603 | 176k | kv.key.size()) { |
12604 | 44.3k | if (memcmp(cur->param, kv.key.c_str(), kv.key.size()) == |
12605 | 44.3k | 0) { |
12606 | 24.7k | recognizedByPROJ = (cur->used == 1); |
12607 | 24.7k | break; |
12608 | 24.7k | } |
12609 | 293k | } else if (strcmp(cur->param, kv.key.c_str()) == 0) { |
12610 | 18.2k | recognizedByPROJ = (cur->used == 1); |
12611 | 18.2k | break; |
12612 | 18.2k | } |
12613 | 337k | } |
12614 | 43.0k | } |
12615 | 43.0k | if (!recognizedByPROJ && kv.key == "geoid_crs") { |
12616 | 3.14k | for (auto &pair : step.paramValues) { |
12617 | 3.14k | if (ci_equal(pair.key, "geoidgrids")) { |
12618 | 562 | recognizedByPROJ = true; |
12619 | 562 | break; |
12620 | 562 | } |
12621 | 3.14k | } |
12622 | 644 | } |
12623 | 43.0k | if (recognizedByPROJ) { |
12624 | 13.1k | newParamValues.emplace_back(kv); |
12625 | 13.1k | } |
12626 | 43.0k | } |
12627 | 8.43k | step.paramValues = std::move(newParamValues); |
12628 | | |
12629 | 8.43k | d->projString_.clear(); |
12630 | 8.43k | if (!step.name.empty()) { |
12631 | 8.42k | d->projString_ += step.isInit ? "+init=" : "+proj="; |
12632 | 8.42k | d->projString_ += step.name; |
12633 | 8.42k | } |
12634 | 13.1k | for (const auto ¶mValue : step.paramValues) { |
12635 | 13.1k | if (!d->projString_.empty()) { |
12636 | 13.1k | d->projString_ += ' '; |
12637 | 13.1k | } |
12638 | 13.1k | d->projString_ += '+'; |
12639 | 13.1k | d->projString_ += paramValue.key; |
12640 | 13.1k | if (!paramValue.value.empty()) { |
12641 | 10.9k | d->projString_ += '='; |
12642 | 10.9k | d->projString_ += |
12643 | 10.9k | pj_double_quote_string_param_if_needed(paramValue.value); |
12644 | 10.9k | } |
12645 | 13.1k | } |
12646 | 8.43k | } |
12647 | | |
12648 | 11.1k | proj_destroy(pj); |
12649 | | |
12650 | 11.1k | if (!valid) { |
12651 | 1.48k | const int l_errno = proj_context_errno(pj_context); |
12652 | 1.48k | std::string msg("Error " + toString(l_errno) + " (" + |
12653 | 1.48k | proj_errno_string(l_errno) + ")"); |
12654 | 1.48k | if (!logger.msg.empty()) { |
12655 | 1.41k | msg += ": "; |
12656 | 1.41k | msg += logger.msg; |
12657 | 1.41k | } |
12658 | 1.48k | logger.msg = std::move(msg); |
12659 | 1.48k | } |
12660 | | |
12661 | 11.1k | if (pj_context != d->ctx_) { |
12662 | 100 | proj_context_destroy(pj_context); |
12663 | 100 | } |
12664 | | |
12665 | 11.1k | if (!valid) { |
12666 | 1.48k | throw ParsingException(logger.msg); |
12667 | 1.48k | } |
12668 | | |
12669 | 9.66k | if (isGeocentricCRS) { |
12670 | | // First run is dry run to mark all recognized/unrecognized tokens |
12671 | 1.89k | for (int iter = 0; iter < 2; iter++) { |
12672 | 1.83k | auto obj = d->buildBoundOrCompoundCRSIfNeeded( |
12673 | 1.83k | 0, d->buildGeocentricCRS(0, (d->steps_.size() == 2 && |
12674 | 1.83k | d->steps_[1].name == "unitconvert") |
12675 | 1.83k | ? 1 |
12676 | 1.83k | : -1)); |
12677 | 1.83k | if (iter == 1) { |
12678 | 889 | return nn_static_pointer_cast<BaseObject>(obj); |
12679 | 889 | } |
12680 | 1.83k | } |
12681 | 948 | } |
12682 | | |
12683 | 8.77k | if (isTopocentricCRS) { |
12684 | | // First run is dry run to mark all recognized/unrecognized tokens |
12685 | 26 | for (int iter = 0; iter < 2; iter++) { |
12686 | 20 | auto obj = d->buildBoundOrCompoundCRSIfNeeded( |
12687 | 20 | 0, |
12688 | 20 | d->buildProjectedCRS(0, d->buildGeocentricCRS(0, -1), -1, -1)); |
12689 | 20 | if (iter == 1) { |
12690 | 7 | return nn_static_pointer_cast<BaseObject>(obj); |
12691 | 7 | } |
12692 | 20 | } |
12693 | 13 | } |
12694 | | |
12695 | 8.76k | if (!unexpectedStructure) { |
12696 | 7.20k | if (iFirstGeogStep == 0 && !d->steps_[iFirstGeogStep].inverted && |
12697 | 7.20k | iSecondGeogStep < 0 && iProjStep < 0 && |
12698 | 7.20k | (iFirstUnitConvert < 0 || iSecondUnitConvert < 0) && |
12699 | 7.20k | (iFirstAxisSwap < 0 || iSecondAxisSwap < 0)) { |
12700 | | // First run is dry run to mark all recognized/unrecognized tokens |
12701 | 4.80k | for (int iter = 0; iter < 2; iter++) { |
12702 | 4.70k | auto obj = d->buildBoundOrCompoundCRSIfNeeded( |
12703 | 4.70k | 0, d->buildGeodeticCRS(iFirstGeogStep, iFirstUnitConvert, |
12704 | 4.70k | iFirstAxisSwap, false)); |
12705 | 4.70k | if (iter == 1) { |
12706 | 2.29k | return nn_static_pointer_cast<BaseObject>(obj); |
12707 | 2.29k | } |
12708 | 4.70k | } |
12709 | 2.40k | } |
12710 | 4.90k | if (iProjStep >= 0 && !d->steps_[iProjStep].inverted && |
12711 | 4.90k | (iFirstGeogStep < 0 || iFirstGeogStep + 1 == iProjStep) && |
12712 | 4.90k | iSecondGeogStep < 0) { |
12713 | 4.76k | if (iFirstGeogStep < 0) |
12714 | 3.93k | iFirstGeogStep = iProjStep; |
12715 | | // First run is dry run to mark all recognized/unrecognized tokens |
12716 | 9.53k | for (int iter = 0; iter < 2; iter++) { |
12717 | 8.95k | auto obj = d->buildBoundOrCompoundCRSIfNeeded( |
12718 | 8.95k | iProjStep, |
12719 | 8.95k | d->buildProjectedCRS( |
12720 | 8.95k | iProjStep, |
12721 | 8.95k | d->buildGeodeticCRS(iFirstGeogStep, |
12722 | 8.95k | iFirstUnitConvert < iFirstGeogStep |
12723 | 8.95k | ? iFirstUnitConvert |
12724 | 8.95k | : -1, |
12725 | 8.95k | iFirstAxisSwap < iFirstGeogStep |
12726 | 8.95k | ? iFirstAxisSwap |
12727 | 8.95k | : -1, |
12728 | 8.95k | true), |
12729 | 8.95k | iFirstUnitConvert < iFirstGeogStep ? iSecondUnitConvert |
12730 | 8.95k | : iFirstUnitConvert, |
12731 | 8.95k | iFirstAxisSwap < iFirstGeogStep ? iSecondAxisSwap |
12732 | 8.95k | : iFirstAxisSwap)); |
12733 | 8.95k | if (iter == 1) { |
12734 | 4.18k | return nn_static_pointer_cast<BaseObject>(obj); |
12735 | 4.18k | } |
12736 | 8.95k | } |
12737 | 4.76k | } |
12738 | 4.90k | } |
12739 | | |
12740 | 2.27k | auto props = PropertyMap(); |
12741 | 2.27k | if (!d->title_.empty()) { |
12742 | 48 | props.set(IdentifiedObject::NAME_KEY, d->title_); |
12743 | 48 | } |
12744 | 2.27k | return operation::SingleOperation::createPROJBased(props, projString, |
12745 | 2.27k | nullptr, nullptr, {}); |
12746 | 8.76k | } |
12747 | | |
12748 | | // --------------------------------------------------------------------------- |
12749 | | |
12750 | | //! @cond Doxygen_Suppress |
12751 | | struct JSONFormatter::Private { |
12752 | | CPLJSonStreamingWriter writer_{nullptr, nullptr}; |
12753 | | DatabaseContextPtr dbContext_{}; |
12754 | | |
12755 | | std::vector<bool> stackHasId_{false}; |
12756 | | std::vector<bool> outputIdStack_{true}; |
12757 | | bool allowIDInImmediateChild_ = false; |
12758 | | bool omitTypeInImmediateChild_ = false; |
12759 | | bool abridgedTransformation_ = false; |
12760 | | bool abridgedTransformationWriteSourceCRS_ = false; |
12761 | | std::string schema_ = PROJJSON_DEFAULT_VERSION; |
12762 | | |
12763 | | // cppcheck-suppress functionStatic |
12764 | 0 | void pushOutputId(bool outputIdIn) { outputIdStack_.push_back(outputIdIn); } |
12765 | | |
12766 | | // cppcheck-suppress functionStatic |
12767 | 0 | void popOutputId() { outputIdStack_.pop_back(); } |
12768 | | }; |
12769 | | //! @endcond |
12770 | | |
12771 | | // --------------------------------------------------------------------------- |
12772 | | |
12773 | | /** \brief Constructs a new formatter. |
12774 | | * |
12775 | | * A formatter can be used only once (its internal state is mutated) |
12776 | | * |
12777 | | * @return new formatter. |
12778 | | */ |
12779 | | JSONFormatterNNPtr JSONFormatter::create( // cppcheck-suppress passedByValue |
12780 | 0 | DatabaseContextPtr dbContext) { |
12781 | 0 | auto ret = NN_NO_CHECK(JSONFormatter::make_unique<JSONFormatter>()); |
12782 | 0 | ret->d->dbContext_ = std::move(dbContext); |
12783 | 0 | return ret; |
12784 | 0 | } |
12785 | | |
12786 | | // --------------------------------------------------------------------------- |
12787 | | |
12788 | | /** \brief Whether to use multi line output or not. */ |
12789 | 0 | JSONFormatter &JSONFormatter::setMultiLine(bool multiLine) noexcept { |
12790 | 0 | d->writer_.SetPrettyFormatting(multiLine); |
12791 | 0 | return *this; |
12792 | 0 | } |
12793 | | |
12794 | | // --------------------------------------------------------------------------- |
12795 | | |
12796 | | /** \brief Set number of spaces for each indentation level (defaults to 4). |
12797 | | */ |
12798 | 0 | JSONFormatter &JSONFormatter::setIndentationWidth(int width) noexcept { |
12799 | 0 | d->writer_.SetIndentationSize(width); |
12800 | 0 | return *this; |
12801 | 0 | } |
12802 | | |
12803 | | // --------------------------------------------------------------------------- |
12804 | | |
12805 | | /** \brief Set the value of the "$schema" key in the top level object. |
12806 | | * |
12807 | | * If set to empty string, it will not be written. |
12808 | | */ |
12809 | 0 | JSONFormatter &JSONFormatter::setSchema(const std::string &schema) noexcept { |
12810 | 0 | d->schema_ = schema; |
12811 | 0 | return *this; |
12812 | 0 | } |
12813 | | |
12814 | | // --------------------------------------------------------------------------- |
12815 | | |
12816 | | //! @cond Doxygen_Suppress |
12817 | | |
12818 | 0 | JSONFormatter::JSONFormatter() : d(std::make_unique<Private>()) {} |
12819 | | |
12820 | | // --------------------------------------------------------------------------- |
12821 | | |
12822 | 0 | JSONFormatter::~JSONFormatter() = default; |
12823 | | |
12824 | | // --------------------------------------------------------------------------- |
12825 | | |
12826 | 0 | CPLJSonStreamingWriter *JSONFormatter::writer() const { return &(d->writer_); } |
12827 | | |
12828 | | // --------------------------------------------------------------------------- |
12829 | | |
12830 | 0 | const DatabaseContextPtr &JSONFormatter::databaseContext() const { |
12831 | 0 | return d->dbContext_; |
12832 | 0 | } |
12833 | | |
12834 | | // --------------------------------------------------------------------------- |
12835 | | |
12836 | 0 | bool JSONFormatter::outputId() const { return d->outputIdStack_.back(); } |
12837 | | |
12838 | | // --------------------------------------------------------------------------- |
12839 | | |
12840 | 0 | bool JSONFormatter::outputUsage(bool calledBeforeObjectContext) const { |
12841 | 0 | return outputId() && |
12842 | 0 | d->outputIdStack_.size() == (calledBeforeObjectContext ? 1U : 2U); |
12843 | 0 | } |
12844 | | |
12845 | | // --------------------------------------------------------------------------- |
12846 | | |
12847 | 0 | void JSONFormatter::setAllowIDInImmediateChild() { |
12848 | 0 | d->allowIDInImmediateChild_ = true; |
12849 | 0 | } |
12850 | | |
12851 | | // --------------------------------------------------------------------------- |
12852 | | |
12853 | 0 | void JSONFormatter::setOmitTypeInImmediateChild() { |
12854 | 0 | d->omitTypeInImmediateChild_ = true; |
12855 | 0 | } |
12856 | | |
12857 | | // --------------------------------------------------------------------------- |
12858 | | |
12859 | | JSONFormatter::ObjectContext::ObjectContext(JSONFormatter &formatter, |
12860 | | const char *objectType, bool hasId) |
12861 | 0 | : m_formatter(formatter) { |
12862 | 0 | m_formatter.d->writer_.StartObj(); |
12863 | 0 | if (m_formatter.d->outputIdStack_.size() == 1 && |
12864 | 0 | !m_formatter.d->schema_.empty()) { |
12865 | 0 | m_formatter.d->writer_.AddObjKey("$schema"); |
12866 | 0 | m_formatter.d->writer_.Add(m_formatter.d->schema_); |
12867 | 0 | } |
12868 | 0 | if (objectType && !m_formatter.d->omitTypeInImmediateChild_) { |
12869 | 0 | m_formatter.d->writer_.AddObjKey("type"); |
12870 | 0 | m_formatter.d->writer_.Add(objectType); |
12871 | 0 | } |
12872 | 0 | m_formatter.d->omitTypeInImmediateChild_ = false; |
12873 | | // All intermediate nodes shouldn't have ID if a parent has an ID |
12874 | | // unless explicitly enabled. |
12875 | 0 | if (m_formatter.d->allowIDInImmediateChild_) { |
12876 | 0 | m_formatter.d->pushOutputId(m_formatter.d->outputIdStack_[0]); |
12877 | 0 | m_formatter.d->allowIDInImmediateChild_ = false; |
12878 | 0 | } else { |
12879 | 0 | m_formatter.d->pushOutputId(m_formatter.d->outputIdStack_[0] && |
12880 | 0 | !m_formatter.d->stackHasId_.back()); |
12881 | 0 | } |
12882 | |
|
12883 | 0 | m_formatter.d->stackHasId_.push_back(hasId || |
12884 | 0 | m_formatter.d->stackHasId_.back()); |
12885 | 0 | } |
12886 | | |
12887 | | // --------------------------------------------------------------------------- |
12888 | | |
12889 | 0 | JSONFormatter::ObjectContext::~ObjectContext() { |
12890 | 0 | m_formatter.d->writer_.EndObj(); |
12891 | 0 | m_formatter.d->stackHasId_.pop_back(); |
12892 | 0 | m_formatter.d->popOutputId(); |
12893 | 0 | } |
12894 | | |
12895 | | // --------------------------------------------------------------------------- |
12896 | | |
12897 | 0 | void JSONFormatter::setAbridgedTransformation(bool outputIn) { |
12898 | 0 | d->abridgedTransformation_ = outputIn; |
12899 | 0 | } |
12900 | | |
12901 | | // --------------------------------------------------------------------------- |
12902 | | |
12903 | 0 | bool JSONFormatter::abridgedTransformation() const { |
12904 | 0 | return d->abridgedTransformation_; |
12905 | 0 | } |
12906 | | |
12907 | | // --------------------------------------------------------------------------- |
12908 | | |
12909 | 0 | void JSONFormatter::setAbridgedTransformationWriteSourceCRS(bool writeCRS) { |
12910 | 0 | d->abridgedTransformationWriteSourceCRS_ = writeCRS; |
12911 | 0 | } |
12912 | | |
12913 | | // --------------------------------------------------------------------------- |
12914 | | |
12915 | 0 | bool JSONFormatter::abridgedTransformationWriteSourceCRS() const { |
12916 | 0 | return d->abridgedTransformationWriteSourceCRS_; |
12917 | 0 | } |
12918 | | |
12919 | | //! @endcond |
12920 | | |
12921 | | // --------------------------------------------------------------------------- |
12922 | | |
12923 | | /** \brief Return the serialized JSON. |
12924 | | */ |
12925 | 0 | const std::string &JSONFormatter::toString() const { |
12926 | 0 | return d->writer_.GetString(); |
12927 | 0 | } |
12928 | | |
12929 | | // --------------------------------------------------------------------------- |
12930 | | |
12931 | | //! @cond Doxygen_Suppress |
12932 | 11.6M | IJSONExportable::~IJSONExportable() = default; |
12933 | | |
12934 | | // --------------------------------------------------------------------------- |
12935 | | |
12936 | 0 | std::string IJSONExportable::exportToJSON(JSONFormatter *formatter) const { |
12937 | 0 | _exportToJSON(formatter); |
12938 | 0 | return formatter->toString(); |
12939 | 0 | } |
12940 | | |
12941 | | //! @endcond |
12942 | | |
12943 | | } // namespace io |
12944 | | NS_PROJ_END |