Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: OpenGIS Simple Features Reference Implementation
4 : * Purpose: The OGRSpatialReference class.
5 : * Author: Frank Warmerdam, warmerdam@pobox.com
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 1999, Les Technologies SoftMap Inc.
9 : * Copyright (c) 2008-2018, Even Rouault <even.rouault at spatialys.com>
10 : *
11 : * SPDX-License-Identifier: MIT
12 : ****************************************************************************/
13 :
14 : #include "cpl_port.h"
15 : #include "ogr_spatialref.h"
16 :
17 : #include <cmath>
18 : #include <cstddef>
19 : #include <cstdio>
20 : #include <cstdlib>
21 : #include <cstring>
22 : #include <limits>
23 : #include <string>
24 : #include <mutex>
25 : #include <set>
26 : #include <vector>
27 :
28 : #include "cpl_atomic_ops.h"
29 : #include "cpl_conv.h"
30 : #include "cpl_csv.h"
31 : #include "cpl_error.h"
32 : #include "cpl_error_internal.h"
33 : #include "cpl_http.h"
34 : #include "cpl_json.h"
35 : #include "cpl_multiproc.h"
36 : #include "cpl_string.h"
37 : #include "cpl_vsi.h"
38 : #include "ogr_core.h"
39 : #include "ogr_p.h"
40 : #include "ogr_proj_p.h"
41 : #include "ogr_srs_api.h"
42 : #include "ogrmitabspatialref.h"
43 :
44 : #include "proj.h"
45 : #include "proj_experimental.h"
46 : #include "proj_constants.h"
47 :
48 : bool GDALThreadLocalDatasetCacheIsInDestruction();
49 :
50 : // Exists since 8.0.1
51 : #ifndef PROJ_AT_LEAST_VERSION
52 : #define PROJ_COMPUTE_VERSION(maj, min, patch) \
53 : ((maj)*10000 + (min)*100 + (patch))
54 : #define PROJ_VERSION_NUMBER \
55 : PROJ_COMPUTE_VERSION(PROJ_VERSION_MAJOR, PROJ_VERSION_MINOR, \
56 : PROJ_VERSION_PATCH)
57 : #define PROJ_AT_LEAST_VERSION(maj, min, patch) \
58 : (PROJ_VERSION_NUMBER >= PROJ_COMPUTE_VERSION(maj, min, patch))
59 : #endif
60 :
61 : #define STRINGIFY(s) #s
62 : #define XSTRINGIFY(s) STRINGIFY(s)
63 :
64 : struct OGRSpatialReference::Private
65 : {
66 : struct Listener : public OGR_SRSNode::Listener
67 : {
68 : OGRSpatialReference::Private *m_poObj = nullptr;
69 :
70 220492 : explicit Listener(OGRSpatialReference::Private *poObj) : m_poObj(poObj)
71 : {
72 220522 : }
73 :
74 : Listener(const Listener &) = delete;
75 : Listener &operator=(const Listener &) = delete;
76 :
77 : void notifyChange(OGR_SRSNode *) override;
78 : };
79 :
80 : OGRSpatialReference *m_poSelf = nullptr;
81 : PJ *m_pj_crs = nullptr;
82 :
83 : // Temporary state used for object construction
84 : PJ_TYPE m_pjType = PJ_TYPE_UNKNOWN;
85 : CPLString m_osPrimeMeridianName{};
86 : CPLString m_osAngularUnits{};
87 : CPLString m_osLinearUnits{};
88 : CPLString m_osAxisName[3]{};
89 :
90 : std::vector<std::string> m_wktImportWarnings{};
91 : std::vector<std::string> m_wktImportErrors{};
92 : CPLString m_osAreaName{};
93 : CPLString m_celestialBodyName{};
94 :
95 : bool m_bIsThreadSafe = false;
96 : bool m_bNodesChanged = false;
97 : bool m_bNodesWKT2 = false;
98 : OGR_SRSNode *m_poRoot = nullptr;
99 :
100 : double dfFromGreenwich = 0.0;
101 : double dfToMeter = 0.0;
102 : double dfToDegrees = 0.0;
103 : double m_dfAngularUnitToRadian = 0.0;
104 :
105 : int nRefCount = 1;
106 : int bNormInfoSet = FALSE;
107 :
108 : PJ *m_pj_geod_base_crs_temp = nullptr;
109 : PJ *m_pj_proj_crs_cs_temp = nullptr;
110 :
111 : bool m_pj_crs_modified_during_demote = false;
112 : PJ *m_pj_bound_crs_target = nullptr;
113 : PJ *m_pj_bound_crs_co = nullptr;
114 : PJ *m_pj_crs_backup = nullptr;
115 : OGR_SRSNode *m_poRootBackup = nullptr;
116 :
117 : bool m_bMorphToESRI = false;
118 : bool m_bHasCenterLong = false;
119 :
120 : std::shared_ptr<Listener> m_poListener{};
121 :
122 : std::recursive_mutex m_mutex{};
123 :
124 : OSRAxisMappingStrategy m_axisMappingStrategy = OAMS_AUTHORITY_COMPLIANT;
125 : std::vector<int> m_axisMapping{1, 2, 3};
126 :
127 : double m_coordinateEpoch = 0; // as decimal year
128 :
129 : explicit Private(OGRSpatialReference *poSelf);
130 : ~Private();
131 : Private(const Private &) = delete;
132 : Private &operator=(const Private &) = delete;
133 :
134 2 : void SetThreadSafe()
135 : {
136 2 : m_bIsThreadSafe = true;
137 2 : }
138 :
139 : void clear();
140 : void setPjCRS(PJ *pj_crsIn, bool doRefreshAxisMapping = true);
141 : void setRoot(OGR_SRSNode *poRoot);
142 : void refreshProjObj();
143 : void nodesChanged();
144 : void refreshRootFromProjObj();
145 : void invalidateNodes();
146 :
147 : void setMorphToESRI(bool b);
148 :
149 : PJ *getGeodBaseCRS();
150 : PJ *getProjCRSCoordSys();
151 :
152 : const char *getProjCRSName();
153 : OGRErr replaceConversionAndUnref(PJ *conv);
154 :
155 : void demoteFromBoundCRS();
156 : void undoDemoteFromBoundCRS();
157 :
158 1104650 : PJ_CONTEXT *getPROJContext()
159 : {
160 1104650 : return OSRGetProjTLSContext();
161 : }
162 :
163 : const char *nullifyTargetKeyIfPossible(const char *pszTargetKey);
164 :
165 : void refreshAxisMapping();
166 :
167 : // This structures enables locking during calls to OGRSpatialReference
168 : // public methods. Locking is only needed for instances of
169 : // OGRSpatialReference that have been asked to be thread-safe at
170 : // construction.
171 : // The lock is not just for a single call to OGRSpatialReference::Private,
172 : // but for the series of calls done by a OGRSpatialReference method.
173 : // We need a recursive mutex, because some OGRSpatialReference methods
174 : // may call other ones.
175 : struct OptionalLockGuard
176 : {
177 : Private &m_private;
178 :
179 4892750 : explicit OptionalLockGuard(Private *p) : m_private(*p)
180 : {
181 4892750 : if (m_private.m_bIsThreadSafe)
182 3798 : m_private.m_mutex.lock();
183 4892750 : }
184 :
185 4893170 : ~OptionalLockGuard()
186 4893170 : {
187 4893170 : if (m_private.m_bIsThreadSafe)
188 3798 : m_private.m_mutex.unlock();
189 4893170 : }
190 : };
191 :
192 4892800 : inline OptionalLockGuard GetOptionalLockGuard()
193 : {
194 4892800 : return OptionalLockGuard(this);
195 : }
196 : };
197 :
198 2021440 : void OGRSpatialReference::Private::Listener::notifyChange(OGR_SRSNode *)
199 : {
200 2021440 : m_poObj->nodesChanged();
201 2021440 : }
202 :
203 : #define TAKE_OPTIONAL_LOCK() \
204 : auto lock = d->GetOptionalLockGuard(); \
205 : CPL_IGNORE_RET_VAL(lock)
206 :
207 220491 : static OSRAxisMappingStrategy GetDefaultAxisMappingStrategy()
208 : {
209 : const char *pszDefaultAMS =
210 220491 : CPLGetConfigOption("OSR_DEFAULT_AXIS_MAPPING_STRATEGY", nullptr);
211 220664 : if (pszDefaultAMS)
212 : {
213 1 : if (EQUAL(pszDefaultAMS, "AUTHORITY_COMPLIANT"))
214 0 : return OAMS_AUTHORITY_COMPLIANT;
215 1 : else if (EQUAL(pszDefaultAMS, "TRADITIONAL_GIS_ORDER"))
216 1 : return OAMS_TRADITIONAL_GIS_ORDER;
217 : else
218 : {
219 0 : CPLError(CE_Failure, CPLE_AppDefined,
220 : "Illegal value for OSR_DEFAULT_AXIS_MAPPING_STRATEGY = %s",
221 : pszDefaultAMS);
222 : }
223 : }
224 220663 : return OAMS_AUTHORITY_COMPLIANT;
225 : }
226 :
227 220585 : OGRSpatialReference::Private::Private(OGRSpatialReference *poSelf)
228 : : m_poSelf(poSelf),
229 220585 : m_poListener(std::shared_ptr<Listener>(new Listener(this)))
230 : {
231 : // Get the default value for m_axisMappingStrategy from the
232 : // OSR_DEFAULT_AXIS_MAPPING_STRATEGY configuration option, if set.
233 220272 : m_axisMappingStrategy = GetDefaultAxisMappingStrategy();
234 220664 : }
235 :
236 878907 : OGRSpatialReference::Private::~Private()
237 : {
238 : // In case we destroy the object not in the thread that created it,
239 : // we need to reassign the PROJ context. Having the context bundled inside
240 : // PJ* deeply sucks...
241 219761 : PJ_CONTEXT *pj_context_to_destroy = nullptr;
242 : PJ_CONTEXT *ctxt;
243 219761 : if (GDALThreadLocalDatasetCacheIsInDestruction())
244 : {
245 183 : pj_context_to_destroy = proj_context_create();
246 183 : ctxt = pj_context_to_destroy;
247 : }
248 : else
249 : {
250 219558 : ctxt = getPROJContext();
251 : }
252 :
253 219777 : proj_assign_context(m_pj_crs, ctxt);
254 219775 : proj_destroy(m_pj_crs);
255 :
256 219772 : proj_assign_context(m_pj_geod_base_crs_temp, ctxt);
257 219772 : proj_destroy(m_pj_geod_base_crs_temp);
258 :
259 219771 : proj_assign_context(m_pj_proj_crs_cs_temp, ctxt);
260 219772 : proj_destroy(m_pj_proj_crs_cs_temp);
261 :
262 219770 : proj_assign_context(m_pj_bound_crs_target, ctxt);
263 219771 : proj_destroy(m_pj_bound_crs_target);
264 :
265 219771 : proj_assign_context(m_pj_bound_crs_co, ctxt);
266 219770 : proj_destroy(m_pj_bound_crs_co);
267 :
268 219771 : proj_assign_context(m_pj_crs_backup, ctxt);
269 219770 : proj_destroy(m_pj_crs_backup);
270 :
271 219772 : delete m_poRootBackup;
272 219773 : delete m_poRoot;
273 219773 : proj_context_destroy(pj_context_to_destroy);
274 219733 : }
275 :
276 114151 : void OGRSpatialReference::Private::clear()
277 : {
278 114151 : proj_assign_context(m_pj_crs, getPROJContext());
279 114151 : proj_destroy(m_pj_crs);
280 114151 : m_pj_crs = nullptr;
281 :
282 114151 : delete m_poRoot;
283 114151 : m_poRoot = nullptr;
284 114151 : m_bNodesChanged = false;
285 :
286 114151 : m_wktImportWarnings.clear();
287 114151 : m_wktImportErrors.clear();
288 :
289 114151 : m_pj_crs_modified_during_demote = false;
290 114151 : m_pjType = PJ_TYPE_UNKNOWN;
291 114151 : m_osPrimeMeridianName.clear();
292 114151 : m_osAngularUnits.clear();
293 114151 : m_osLinearUnits.clear();
294 :
295 114151 : bNormInfoSet = FALSE;
296 114151 : dfFromGreenwich = 1.0;
297 114151 : dfToMeter = 1.0;
298 114151 : dfToDegrees = 1.0;
299 114151 : m_dfAngularUnitToRadian = 0.0;
300 :
301 114151 : m_bMorphToESRI = false;
302 114151 : m_bHasCenterLong = false;
303 :
304 114151 : m_coordinateEpoch = 0.0;
305 114151 : }
306 :
307 25445 : void OGRSpatialReference::Private::setRoot(OGR_SRSNode *poRoot)
308 : {
309 25445 : m_poRoot = poRoot;
310 25445 : if (m_poRoot)
311 : {
312 25445 : m_poRoot->RegisterListener(m_poListener);
313 : }
314 25445 : nodesChanged();
315 25445 : }
316 :
317 174469 : void OGRSpatialReference::Private::setPjCRS(PJ *pj_crsIn,
318 : bool doRefreshAxisMapping)
319 : {
320 174469 : auto ctxt = getPROJContext();
321 :
322 : #if PROJ_AT_LEAST_VERSION(9, 2, 0)
323 : if (proj_get_type(pj_crsIn) == PJ_TYPE_COORDINATE_METADATA)
324 : {
325 : const double dfEpoch =
326 : proj_coordinate_metadata_get_epoch(ctxt, pj_crsIn);
327 : if (!std::isnan(dfEpoch))
328 : {
329 : m_poSelf->SetCoordinateEpoch(dfEpoch);
330 : }
331 : auto crs = proj_get_source_crs(ctxt, pj_crsIn);
332 : proj_destroy(pj_crsIn);
333 : pj_crsIn = crs;
334 : }
335 : #endif
336 :
337 174470 : proj_assign_context(m_pj_crs, ctxt);
338 174470 : proj_destroy(m_pj_crs);
339 174470 : m_pj_crs = pj_crsIn;
340 174470 : if (m_pj_crs)
341 : {
342 174419 : m_pjType = proj_get_type(m_pj_crs);
343 : }
344 174470 : if (m_pj_crs_backup)
345 : {
346 21 : m_pj_crs_modified_during_demote = true;
347 : }
348 174470 : invalidateNodes();
349 174468 : if (doRefreshAxisMapping)
350 : {
351 174447 : refreshAxisMapping();
352 : }
353 174470 : }
354 :
355 649668 : void OGRSpatialReference::Private::refreshProjObj()
356 : {
357 649668 : if (m_bNodesChanged && m_poRoot)
358 : {
359 8026 : char *pszWKT = nullptr;
360 8026 : m_poRoot->exportToWkt(&pszWKT);
361 8026 : auto poRootBackup = m_poRoot;
362 8026 : m_poRoot = nullptr;
363 8026 : const double dfCoordinateEpochBackup = m_coordinateEpoch;
364 8026 : clear();
365 8026 : m_coordinateEpoch = dfCoordinateEpochBackup;
366 8026 : m_bHasCenterLong = strstr(pszWKT, "CENTER_LONG") != nullptr;
367 :
368 8026 : const char *const options[] = {
369 : "STRICT=NO",
370 : #if PROJ_AT_LEAST_VERSION(9, 1, 0)
371 : "UNSET_IDENTIFIERS_IF_INCOMPATIBLE_DEF=NO",
372 : #endif
373 : nullptr
374 : };
375 8026 : PROJ_STRING_LIST warnings = nullptr;
376 8026 : PROJ_STRING_LIST errors = nullptr;
377 8026 : setPjCRS(proj_create_from_wkt(getPROJContext(), pszWKT, options,
378 : &warnings, &errors));
379 15662 : for (auto iter = warnings; iter && *iter; ++iter)
380 : {
381 7636 : m_wktImportWarnings.push_back(*iter);
382 : }
383 8240 : for (auto iter = errors; iter && *iter; ++iter)
384 : {
385 214 : m_wktImportErrors.push_back(*iter);
386 : }
387 8026 : proj_string_list_destroy(warnings);
388 8026 : proj_string_list_destroy(errors);
389 :
390 8026 : CPLFree(pszWKT);
391 :
392 8026 : m_poRoot = poRootBackup;
393 8026 : m_bNodesChanged = false;
394 : }
395 649668 : }
396 :
397 27550 : void OGRSpatialReference::Private::refreshRootFromProjObj()
398 : {
399 27550 : CPLAssert(m_poRoot == nullptr);
400 :
401 27550 : if (m_pj_crs)
402 : {
403 50802 : CPLStringList aosOptions;
404 25401 : if (!m_bMorphToESRI)
405 : {
406 25397 : aosOptions.SetNameValue("OUTPUT_AXIS", "YES");
407 25397 : aosOptions.SetNameValue("MULTILINE", "NO");
408 : }
409 25401 : aosOptions.SetNameValue("STRICT", "NO");
410 :
411 : const char *pszWKT;
412 : {
413 25401 : CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
414 25401 : pszWKT = proj_as_wkt(getPROJContext(), m_pj_crs,
415 25401 : m_bMorphToESRI ? PJ_WKT1_ESRI : PJ_WKT1_GDAL,
416 25401 : aosOptions.List());
417 25401 : m_bNodesWKT2 = false;
418 : }
419 25401 : if (!m_bMorphToESRI && pszWKT == nullptr)
420 : {
421 70 : pszWKT = proj_as_wkt(getPROJContext(), m_pj_crs, PJ_WKT2_2018,
422 70 : aosOptions.List());
423 70 : m_bNodesWKT2 = true;
424 : }
425 25401 : if (pszWKT)
426 : {
427 25401 : auto root = new OGR_SRSNode();
428 25401 : setRoot(root);
429 25401 : root->importFromWkt(&pszWKT);
430 25401 : m_bNodesChanged = false;
431 : }
432 : }
433 27550 : }
434 :
435 201801 : static bool isNorthEastAxisOrder(PJ_CONTEXT *ctx, PJ *cs)
436 : {
437 201801 : const char *pszName1 = nullptr;
438 201801 : const char *pszDirection1 = nullptr;
439 201801 : proj_cs_get_axis_info(ctx, cs, 0, &pszName1, nullptr, &pszDirection1,
440 : nullptr, nullptr, nullptr, nullptr);
441 201802 : const char *pszName2 = nullptr;
442 201802 : const char *pszDirection2 = nullptr;
443 201802 : proj_cs_get_axis_info(ctx, cs, 1, &pszName2, nullptr, &pszDirection2,
444 : nullptr, nullptr, nullptr, nullptr);
445 201802 : if (pszDirection1 && EQUAL(pszDirection1, "north") && pszDirection2 &&
446 88754 : EQUAL(pszDirection2, "east"))
447 : {
448 87575 : return true;
449 : }
450 114227 : if (pszDirection1 && pszDirection2 &&
451 114225 : ((EQUAL(pszDirection1, "north") && EQUAL(pszDirection2, "north")) ||
452 113063 : (EQUAL(pszDirection1, "south") && EQUAL(pszDirection2, "south"))) &&
453 2321 : pszName1 && STARTS_WITH_CI(pszName1, "northing") && pszName2 &&
454 855 : STARTS_WITH_CI(pszName2, "easting"))
455 : {
456 855 : return true;
457 : }
458 113372 : return false;
459 : }
460 :
461 258829 : void OGRSpatialReference::Private::refreshAxisMapping()
462 : {
463 258829 : if (!m_pj_crs || m_axisMappingStrategy == OAMS_CUSTOM)
464 57429 : return;
465 :
466 201400 : bool doUndoDemote = false;
467 201400 : if (m_pj_crs_backup == nullptr)
468 : {
469 201379 : doUndoDemote = true;
470 201379 : demoteFromBoundCRS();
471 : }
472 201402 : const auto ctxt = getPROJContext();
473 201402 : PJ *horizCRS = nullptr;
474 201402 : int axisCount = 0;
475 201402 : if (m_pjType == PJ_TYPE_VERTICAL_CRS)
476 : {
477 218 : axisCount = 1;
478 : }
479 201184 : else if (m_pjType == PJ_TYPE_COMPOUND_CRS)
480 : {
481 1100 : horizCRS = proj_crs_get_sub_crs(ctxt, m_pj_crs, 0);
482 1100 : if (horizCRS && proj_get_type(horizCRS) == PJ_TYPE_BOUND_CRS)
483 : {
484 222 : auto baseCRS = proj_get_source_crs(ctxt, horizCRS);
485 222 : if (baseCRS)
486 : {
487 222 : proj_destroy(horizCRS);
488 222 : horizCRS = baseCRS;
489 : }
490 : }
491 :
492 1100 : auto vertCRS = proj_crs_get_sub_crs(ctxt, m_pj_crs, 1);
493 1100 : if (vertCRS)
494 : {
495 1097 : if (proj_get_type(vertCRS) == PJ_TYPE_BOUND_CRS)
496 : {
497 391 : auto baseCRS = proj_get_source_crs(ctxt, vertCRS);
498 391 : if (baseCRS)
499 : {
500 391 : proj_destroy(vertCRS);
501 391 : vertCRS = baseCRS;
502 : }
503 : }
504 :
505 1097 : auto cs = proj_crs_get_coordinate_system(ctxt, vertCRS);
506 1097 : if (cs)
507 : {
508 1097 : axisCount += proj_cs_get_axis_count(ctxt, cs);
509 1097 : proj_destroy(cs);
510 : }
511 1097 : proj_destroy(vertCRS);
512 : }
513 : }
514 : else
515 : {
516 200084 : horizCRS = m_pj_crs;
517 : }
518 :
519 201402 : bool bSwitchForGisFriendlyOrder = false;
520 201402 : if (horizCRS)
521 : {
522 201181 : auto cs = proj_crs_get_coordinate_system(ctxt, horizCRS);
523 201180 : if (cs)
524 : {
525 201181 : int nHorizCSAxisCount = proj_cs_get_axis_count(ctxt, cs);
526 201181 : axisCount += nHorizCSAxisCount;
527 201181 : if (nHorizCSAxisCount >= 2)
528 : {
529 201170 : bSwitchForGisFriendlyOrder = isNorthEastAxisOrder(ctxt, cs);
530 : }
531 201181 : proj_destroy(cs);
532 : }
533 : }
534 201400 : if (horizCRS != m_pj_crs)
535 : {
536 1318 : proj_destroy(horizCRS);
537 : }
538 201400 : if (doUndoDemote)
539 : {
540 201379 : undoDemoteFromBoundCRS();
541 : }
542 :
543 201399 : m_axisMapping.resize(axisCount);
544 201402 : if (m_axisMappingStrategy == OAMS_AUTHORITY_COMPLIANT ||
545 60449 : !bSwitchForGisFriendlyOrder)
546 : {
547 524342 : for (int i = 0; i < axisCount; i++)
548 : {
549 350027 : m_axisMapping[i] = i + 1;
550 174315 : }
551 : }
552 : else
553 : {
554 27086 : m_axisMapping[0] = 2;
555 27086 : m_axisMapping[1] = 1;
556 27086 : if (axisCount == 3)
557 : {
558 338 : m_axisMapping[2] = 3;
559 : }
560 : }
561 : }
562 :
563 2046890 : void OGRSpatialReference::Private::nodesChanged()
564 : {
565 2046890 : m_bNodesChanged = true;
566 2046890 : }
567 :
568 174756 : void OGRSpatialReference::Private::invalidateNodes()
569 : {
570 174756 : delete m_poRoot;
571 174756 : m_poRoot = nullptr;
572 174756 : m_bNodesChanged = false;
573 174756 : }
574 :
575 287 : void OGRSpatialReference::Private::setMorphToESRI(bool b)
576 : {
577 287 : invalidateNodes();
578 287 : m_bMorphToESRI = b;
579 287 : }
580 :
581 568508 : void OGRSpatialReference::Private::demoteFromBoundCRS()
582 : {
583 568508 : CPLAssert(m_pj_bound_crs_target == nullptr);
584 568508 : CPLAssert(m_pj_bound_crs_co == nullptr);
585 568508 : CPLAssert(m_poRootBackup == nullptr);
586 568508 : CPLAssert(m_pj_crs_backup == nullptr);
587 :
588 568508 : m_pj_crs_modified_during_demote = false;
589 :
590 568508 : if (m_pjType == PJ_TYPE_BOUND_CRS)
591 : {
592 2733 : auto baseCRS = proj_get_source_crs(getPROJContext(), m_pj_crs);
593 2733 : m_pj_bound_crs_target = proj_get_target_crs(getPROJContext(), m_pj_crs);
594 2733 : m_pj_bound_crs_co =
595 2733 : proj_crs_get_coordoperation(getPROJContext(), m_pj_crs);
596 :
597 2733 : m_poRootBackup = m_poRoot;
598 2733 : m_poRoot = nullptr;
599 2733 : m_pj_crs_backup = m_pj_crs;
600 2733 : m_pj_crs = baseCRS;
601 2733 : m_pjType = proj_get_type(m_pj_crs);
602 : }
603 568508 : }
604 :
605 568509 : void OGRSpatialReference::Private::undoDemoteFromBoundCRS()
606 : {
607 568509 : if (m_pj_bound_crs_target)
608 : {
609 2733 : CPLAssert(m_poRoot == nullptr);
610 2733 : CPLAssert(m_pj_crs);
611 2733 : if (!m_pj_crs_modified_during_demote)
612 : {
613 2713 : proj_destroy(m_pj_crs);
614 2713 : m_pj_crs = m_pj_crs_backup;
615 2713 : m_pjType = proj_get_type(m_pj_crs);
616 2713 : m_poRoot = m_poRootBackup;
617 : }
618 : else
619 : {
620 20 : delete m_poRootBackup;
621 20 : m_poRootBackup = nullptr;
622 20 : proj_destroy(m_pj_crs_backup);
623 20 : m_pj_crs_backup = nullptr;
624 20 : setPjCRS(proj_crs_create_bound_crs(getPROJContext(), m_pj_crs,
625 20 : m_pj_bound_crs_target,
626 20 : m_pj_bound_crs_co),
627 : false);
628 : }
629 : }
630 :
631 568509 : m_poRootBackup = nullptr;
632 568509 : m_pj_crs_backup = nullptr;
633 568509 : proj_destroy(m_pj_bound_crs_target);
634 568506 : m_pj_bound_crs_target = nullptr;
635 568506 : proj_destroy(m_pj_bound_crs_co);
636 568506 : m_pj_bound_crs_co = nullptr;
637 568506 : m_pj_crs_modified_during_demote = false;
638 568506 : }
639 :
640 124829 : const char *OGRSpatialReference::Private::nullifyTargetKeyIfPossible(
641 : const char *pszTargetKey)
642 : {
643 124829 : if (pszTargetKey)
644 : {
645 56793 : demoteFromBoundCRS();
646 56793 : if ((m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
647 28968 : m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS) &&
648 27903 : EQUAL(pszTargetKey, "GEOGCS"))
649 : {
650 6932 : pszTargetKey = nullptr;
651 : }
652 49861 : else if (m_pjType == PJ_TYPE_GEOCENTRIC_CRS &&
653 20 : EQUAL(pszTargetKey, "GEOCCS"))
654 : {
655 0 : pszTargetKey = nullptr;
656 : }
657 49861 : else if (m_pjType == PJ_TYPE_PROJECTED_CRS &&
658 27583 : EQUAL(pszTargetKey, "PROJCS"))
659 : {
660 3886 : pszTargetKey = nullptr;
661 : }
662 45975 : else if (m_pjType == PJ_TYPE_VERTICAL_CRS &&
663 4 : EQUAL(pszTargetKey, "VERT_CS"))
664 : {
665 2 : pszTargetKey = nullptr;
666 : }
667 56793 : undoDemoteFromBoundCRS();
668 : }
669 124829 : return pszTargetKey;
670 : }
671 :
672 9032 : PJ *OGRSpatialReference::Private::getGeodBaseCRS()
673 : {
674 9032 : if (m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
675 8979 : m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS)
676 : {
677 53 : return m_pj_crs;
678 : }
679 :
680 8979 : auto ctxt = getPROJContext();
681 8979 : if (m_pjType == PJ_TYPE_PROJECTED_CRS)
682 : {
683 4117 : proj_assign_context(m_pj_geod_base_crs_temp, ctxt);
684 4117 : proj_destroy(m_pj_geod_base_crs_temp);
685 4117 : m_pj_geod_base_crs_temp = proj_crs_get_geodetic_crs(ctxt, m_pj_crs);
686 4117 : return m_pj_geod_base_crs_temp;
687 : }
688 :
689 4862 : proj_assign_context(m_pj_geod_base_crs_temp, ctxt);
690 4862 : proj_destroy(m_pj_geod_base_crs_temp);
691 4862 : auto cs = proj_create_ellipsoidal_2D_cs(ctxt, PJ_ELLPS2D_LATITUDE_LONGITUDE,
692 : nullptr, 0);
693 4862 : m_pj_geod_base_crs_temp = proj_create_geographic_crs(
694 : ctxt, "WGS 84", "World Geodetic System 1984", "WGS 84",
695 : SRS_WGS84_SEMIMAJOR, SRS_WGS84_INVFLATTENING, SRS_PM_GREENWICH, 0.0,
696 : SRS_UA_DEGREE, CPLAtof(SRS_UA_DEGREE_CONV), cs);
697 4862 : proj_destroy(cs);
698 :
699 4862 : return m_pj_geod_base_crs_temp;
700 : }
701 :
702 5065 : PJ *OGRSpatialReference::Private::getProjCRSCoordSys()
703 : {
704 5065 : auto ctxt = getPROJContext();
705 5065 : if (m_pjType == PJ_TYPE_PROJECTED_CRS)
706 : {
707 4103 : proj_assign_context(m_pj_proj_crs_cs_temp, ctxt);
708 4103 : proj_destroy(m_pj_proj_crs_cs_temp);
709 4103 : m_pj_proj_crs_cs_temp =
710 4103 : proj_crs_get_coordinate_system(getPROJContext(), m_pj_crs);
711 4103 : return m_pj_proj_crs_cs_temp;
712 : }
713 :
714 962 : proj_assign_context(m_pj_proj_crs_cs_temp, ctxt);
715 962 : proj_destroy(m_pj_proj_crs_cs_temp);
716 962 : m_pj_proj_crs_cs_temp = proj_create_cartesian_2D_cs(
717 : ctxt, PJ_CART2D_EASTING_NORTHING, nullptr, 0);
718 962 : return m_pj_proj_crs_cs_temp;
719 : }
720 :
721 5116 : const char *OGRSpatialReference::Private::getProjCRSName()
722 : {
723 5116 : if (m_pjType == PJ_TYPE_PROJECTED_CRS)
724 : {
725 4118 : return proj_get_name(m_pj_crs);
726 : }
727 :
728 998 : return "unnamed";
729 : }
730 :
731 1370 : OGRErr OGRSpatialReference::Private::replaceConversionAndUnref(PJ *conv)
732 : {
733 1370 : refreshProjObj();
734 :
735 1370 : demoteFromBoundCRS();
736 :
737 : auto projCRS =
738 1370 : proj_create_projected_crs(getPROJContext(), getProjCRSName(),
739 1370 : getGeodBaseCRS(), conv, getProjCRSCoordSys());
740 1370 : proj_destroy(conv);
741 :
742 1370 : setPjCRS(projCRS);
743 :
744 1370 : undoDemoteFromBoundCRS();
745 1370 : return OGRERR_NONE;
746 : }
747 :
748 : /************************************************************************/
749 : /* ToPointer() */
750 : /************************************************************************/
751 :
752 24969 : static inline OGRSpatialReference *ToPointer(OGRSpatialReferenceH hSRS)
753 : {
754 24969 : return OGRSpatialReference::FromHandle(hSRS);
755 : }
756 :
757 : /************************************************************************/
758 : /* ToHandle() */
759 : /************************************************************************/
760 :
761 4187 : static inline OGRSpatialReferenceH ToHandle(OGRSpatialReference *poSRS)
762 : {
763 4187 : return OGRSpatialReference::ToHandle(poSRS);
764 : }
765 :
766 : /************************************************************************/
767 : /* OGRsnPrintDouble() */
768 : /************************************************************************/
769 :
770 : void OGRsnPrintDouble(char *pszStrBuf, size_t size, double dfValue);
771 :
772 126 : void OGRsnPrintDouble(char *pszStrBuf, size_t size, double dfValue)
773 :
774 : {
775 126 : CPLsnprintf(pszStrBuf, size, "%.16g", dfValue);
776 :
777 126 : const size_t nLen = strlen(pszStrBuf);
778 :
779 : // The following hack is intended to truncate some "precision" in cases
780 : // that appear to be roundoff error.
781 126 : if (nLen > 15 && (strcmp(pszStrBuf + nLen - 6, "999999") == 0 ||
782 8 : strcmp(pszStrBuf + nLen - 6, "000001") == 0))
783 : {
784 0 : CPLsnprintf(pszStrBuf, size, "%.15g", dfValue);
785 : }
786 :
787 : // Force to user periods regardless of locale.
788 126 : if (strchr(pszStrBuf, ',') != nullptr)
789 : {
790 0 : char *const pszDelim = strchr(pszStrBuf, ',');
791 0 : *pszDelim = '.';
792 : }
793 126 : }
794 :
795 : /************************************************************************/
796 : /* OGRSpatialReference() */
797 : /************************************************************************/
798 :
799 : /**
800 : * \brief Constructor.
801 : *
802 : * This constructor takes an optional string argument which if passed
803 : * should be a WKT representation of an SRS. Passing this is equivalent
804 : * to not passing it, and then calling importFromWkt() with the WKT string.
805 : *
806 : * Note that newly created objects are given a reference count of one.
807 : *
808 : * Starting with GDAL 3.0, coordinates associated with a OGRSpatialReference
809 : * object are assumed to be in the order of the axis of the CRS definition
810 : (which
811 : * for example means latitude first, longitude second for geographic CRS
812 : belonging
813 : * to the EPSG authority). It is possible to define a data axis to CRS axis
814 : * mapping strategy with the SetAxisMappingStrategy() method.
815 : *
816 : * Starting with GDAL 3.5, the OSR_DEFAULT_AXIS_MAPPING_STRATEGY configuration
817 : * option can be set to "TRADITIONAL_GIS_ORDER" / "AUTHORITY_COMPLIANT" (the
818 : later
819 : * being the default value when the option is not set) to control the value of
820 : the
821 : * data axis to CRS axis mapping strategy when a OSRSpatialReference object is
822 : * created. Calling SetAxisMappingStrategy() will override this default value.
823 :
824 : * The C function OSRNewSpatialReference() does the same thing as this
825 : * constructor.
826 : *
827 : * @param pszWKT well known text definition to which the object should
828 : * be initialized, or NULL (the default).
829 : */
830 :
831 218328 : OGRSpatialReference::OGRSpatialReference(const char *pszWKT)
832 218328 : : d(new Private(this))
833 : {
834 218085 : if (pszWKT != nullptr)
835 280 : importFromWkt(pszWKT);
836 218085 : }
837 :
838 : /************************************************************************/
839 : /* OSRNewSpatialReference() */
840 : /************************************************************************/
841 :
842 : /**
843 : * \brief Constructor.
844 : *
845 : * Starting with GDAL 3.0, coordinates associated with a OGRSpatialReference
846 : * object are assumed to be in the order of the axis of the CRS definition
847 : * (which for example means latitude first, longitude second for geographic CRS
848 : * belonging to the EPSG authority). It is possible to define a data axis to CRS
849 : * axis mapping strategy with the SetAxisMappingStrategy() method.
850 : *
851 : * Starting with GDAL 3.5, the OSR_DEFAULT_AXIS_MAPPING_STRATEGY configuration
852 : * option can be set to "TRADITIONAL_GIS_ORDER" / "AUTHORITY_COMPLIANT" (the
853 : * later being the default value when the option is not set) to control the
854 : * value of the data axis to CRS axis mapping strategy when a
855 : * OSRSpatialReference object is created. Calling SetAxisMappingStrategy() will
856 : * override this default value.
857 : *
858 : * This function is the same as OGRSpatialReference::OGRSpatialReference()
859 : */
860 2928 : OGRSpatialReferenceH CPL_STDCALL OSRNewSpatialReference(const char *pszWKT)
861 :
862 : {
863 2928 : OGRSpatialReference *poSRS = new OGRSpatialReference();
864 :
865 2928 : if (pszWKT != nullptr && strlen(pszWKT) > 0)
866 : {
867 98 : if (poSRS->importFromWkt(pszWKT) != OGRERR_NONE)
868 : {
869 0 : delete poSRS;
870 0 : poSRS = nullptr;
871 : }
872 : }
873 :
874 2928 : return ToHandle(poSRS);
875 : }
876 :
877 : /************************************************************************/
878 : /* OGRSpatialReference() */
879 : /************************************************************************/
880 :
881 : /** Copy constructor. See also Clone().
882 : * @param oOther other spatial reference
883 : */
884 2303 : OGRSpatialReference::OGRSpatialReference(const OGRSpatialReference &oOther)
885 2303 : : d(new Private(this))
886 : {
887 2303 : *this = oOther;
888 2303 : }
889 :
890 : /************************************************************************/
891 : /* OGRSpatialReference() */
892 : /************************************************************************/
893 :
894 : /** Move constructor.
895 : * @param oOther other spatial reference
896 : */
897 29 : OGRSpatialReference::OGRSpatialReference(OGRSpatialReference &&oOther)
898 29 : : d(std::move(oOther.d))
899 : {
900 29 : }
901 :
902 : /************************************************************************/
903 : /* ~OGRSpatialReference() */
904 : /************************************************************************/
905 :
906 : /**
907 : * \brief OGRSpatialReference destructor.
908 : *
909 : * The C function OSRDestroySpatialReference() does the same thing as this
910 : * method. Preferred C++ method : OGRSpatialReference::DestroySpatialReference()
911 : *
912 : * @deprecated
913 : */
914 :
915 274808 : OGRSpatialReference::~OGRSpatialReference()
916 :
917 : {
918 274768 : }
919 :
920 : /************************************************************************/
921 : /* DestroySpatialReference() */
922 : /************************************************************************/
923 :
924 : /**
925 : * \brief OGRSpatialReference destructor.
926 : *
927 : * This static method will destroy a OGRSpatialReference. It is
928 : * equivalent to calling delete on the object, but it ensures that the
929 : * deallocation is properly executed within the OGR libraries heap on
930 : * platforms where this can matter (win32).
931 : *
932 : * This function is the same as OSRDestroySpatialReference()
933 : *
934 : * @param poSRS the object to delete
935 : *
936 : * @since GDAL 1.7.0
937 : */
938 :
939 0 : void OGRSpatialReference::DestroySpatialReference(OGRSpatialReference *poSRS)
940 : {
941 0 : delete poSRS;
942 0 : }
943 :
944 : /************************************************************************/
945 : /* OSRDestroySpatialReference() */
946 : /************************************************************************/
947 :
948 : /**
949 : * \brief OGRSpatialReference destructor.
950 : *
951 : * This function is the same as OGRSpatialReference::~OGRSpatialReference()
952 : * and OGRSpatialReference::DestroySpatialReference()
953 : *
954 : * @param hSRS the object to delete
955 : */
956 8965 : void CPL_STDCALL OSRDestroySpatialReference(OGRSpatialReferenceH hSRS)
957 :
958 : {
959 8965 : delete ToPointer(hSRS);
960 8965 : }
961 :
962 : /************************************************************************/
963 : /* Clear() */
964 : /************************************************************************/
965 :
966 : /**
967 : * \brief Wipe current definition.
968 : *
969 : * Returns OGRSpatialReference to a state with no definition, as it
970 : * exists when first created. It does not affect reference counts.
971 : */
972 :
973 106125 : void OGRSpatialReference::Clear()
974 :
975 : {
976 106125 : d->clear();
977 106125 : }
978 :
979 : /************************************************************************/
980 : /* operator=() */
981 : /************************************************************************/
982 :
983 : /** Assignment operator.
984 : * @param oSource SRS to assign to *this
985 : * @return *this
986 : */
987 : OGRSpatialReference &
988 25400 : OGRSpatialReference::operator=(const OGRSpatialReference &oSource)
989 :
990 : {
991 25400 : if (&oSource != this)
992 : {
993 25399 : Clear();
994 : #ifdef CPPCHECK
995 : // Otherwise cppcheck would protest that nRefCount isn't modified
996 : d->nRefCount = (d->nRefCount + 1) - 1;
997 : #endif
998 :
999 25399 : oSource.d->refreshProjObj();
1000 25399 : if (oSource.d->m_pj_crs)
1001 25105 : d->setPjCRS(proj_clone(d->getPROJContext(), oSource.d->m_pj_crs));
1002 25399 : if (oSource.d->m_axisMappingStrategy == OAMS_TRADITIONAL_GIS_ORDER)
1003 11697 : SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
1004 13702 : else if (oSource.d->m_axisMappingStrategy == OAMS_CUSTOM)
1005 112 : SetDataAxisToSRSAxisMapping(oSource.d->m_axisMapping);
1006 :
1007 25399 : d->m_coordinateEpoch = oSource.d->m_coordinateEpoch;
1008 : }
1009 :
1010 25400 : return *this;
1011 : }
1012 :
1013 : /************************************************************************/
1014 : /* operator=() */
1015 : /************************************************************************/
1016 :
1017 : /** Move assignment operator.
1018 : * @param oSource SRS to assign to *this
1019 : * @return *this
1020 : */
1021 : OGRSpatialReference &
1022 4379 : OGRSpatialReference::operator=(OGRSpatialReference &&oSource)
1023 :
1024 : {
1025 4379 : if (&oSource != this)
1026 : {
1027 4378 : d = std::move(oSource.d);
1028 : }
1029 :
1030 4379 : return *this;
1031 : }
1032 :
1033 : /************************************************************************/
1034 : /* AssignAndSetThreadSafe() */
1035 : /************************************************************************/
1036 :
1037 : /** Assignment method, with thread-safety.
1038 : *
1039 : * Same as an assignment operator, but asking also that the *this instance
1040 : * becomes thread-safe.
1041 : *
1042 : * @param oSource SRS to assign to *this
1043 : * @return *this
1044 : * @since 3.10
1045 : */
1046 :
1047 : OGRSpatialReference &
1048 2 : OGRSpatialReference::AssignAndSetThreadSafe(const OGRSpatialReference &oSource)
1049 : {
1050 2 : *this = oSource;
1051 2 : d->SetThreadSafe();
1052 2 : return *this;
1053 : }
1054 :
1055 : /************************************************************************/
1056 : /* Reference() */
1057 : /************************************************************************/
1058 :
1059 : /**
1060 : * \brief Increments the reference count by one.
1061 : *
1062 : * The reference count is used keep track of the number of OGRGeometry objects
1063 : * referencing this SRS.
1064 : *
1065 : * The method does the same thing as the C function OSRReference().
1066 : *
1067 : * @return the updated reference count.
1068 : */
1069 :
1070 4044570 : int OGRSpatialReference::Reference()
1071 :
1072 : {
1073 4044570 : return CPLAtomicInc(&d->nRefCount);
1074 : }
1075 :
1076 : /************************************************************************/
1077 : /* OSRReference() */
1078 : /************************************************************************/
1079 :
1080 : /**
1081 : * \brief Increments the reference count by one.
1082 : *
1083 : * This function is the same as OGRSpatialReference::Reference()
1084 : */
1085 964 : int OSRReference(OGRSpatialReferenceH hSRS)
1086 :
1087 : {
1088 964 : VALIDATE_POINTER1(hSRS, "OSRReference", 0);
1089 :
1090 964 : return ToPointer(hSRS)->Reference();
1091 : }
1092 :
1093 : /************************************************************************/
1094 : /* Dereference() */
1095 : /************************************************************************/
1096 :
1097 : /**
1098 : * \brief Decrements the reference count by one.
1099 : *
1100 : * The method does the same thing as the C function OSRDereference().
1101 : *
1102 : * @return the updated reference count.
1103 : */
1104 :
1105 4084100 : int OGRSpatialReference::Dereference()
1106 :
1107 : {
1108 4084100 : if (d->nRefCount <= 0)
1109 0 : CPLDebug("OSR",
1110 : "Dereference() called on an object with refcount %d,"
1111 : "likely already destroyed!",
1112 0 : d->nRefCount);
1113 4084100 : return CPLAtomicDec(&d->nRefCount);
1114 : }
1115 :
1116 : /************************************************************************/
1117 : /* OSRDereference() */
1118 : /************************************************************************/
1119 :
1120 : /**
1121 : * \brief Decrements the reference count by one.
1122 : *
1123 : * This function is the same as OGRSpatialReference::Dereference()
1124 : */
1125 0 : int OSRDereference(OGRSpatialReferenceH hSRS)
1126 :
1127 : {
1128 0 : VALIDATE_POINTER1(hSRS, "OSRDereference", 0);
1129 :
1130 0 : return ToPointer(hSRS)->Dereference();
1131 : }
1132 :
1133 : /************************************************************************/
1134 : /* GetReferenceCount() */
1135 : /************************************************************************/
1136 :
1137 : /**
1138 : * \brief Fetch current reference count.
1139 : *
1140 : * @return the current reference count.
1141 : */
1142 180 : int OGRSpatialReference::GetReferenceCount() const
1143 : {
1144 180 : return d->nRefCount;
1145 : }
1146 :
1147 : /************************************************************************/
1148 : /* Release() */
1149 : /************************************************************************/
1150 :
1151 : /**
1152 : * \brief Decrements the reference count by one, and destroy if zero.
1153 : *
1154 : * The method does the same thing as the C function OSRRelease().
1155 : */
1156 :
1157 4081320 : void OGRSpatialReference::Release()
1158 :
1159 : {
1160 4081320 : if (Dereference() <= 0)
1161 39509 : delete this;
1162 4081320 : }
1163 :
1164 : /************************************************************************/
1165 : /* OSRRelease() */
1166 : /************************************************************************/
1167 :
1168 : /**
1169 : * \brief Decrements the reference count by one, and destroy if zero.
1170 : *
1171 : * This function is the same as OGRSpatialReference::Release()
1172 : */
1173 6110 : void OSRRelease(OGRSpatialReferenceH hSRS)
1174 :
1175 : {
1176 6110 : VALIDATE_POINTER0(hSRS, "OSRRelease");
1177 :
1178 6110 : ToPointer(hSRS)->Release();
1179 : }
1180 :
1181 85409 : OGR_SRSNode *OGRSpatialReference::GetRoot()
1182 : {
1183 85409 : TAKE_OPTIONAL_LOCK();
1184 :
1185 85409 : if (!d->m_poRoot)
1186 : {
1187 24843 : d->refreshRootFromProjObj();
1188 : }
1189 170818 : return d->m_poRoot;
1190 : }
1191 :
1192 7454 : const OGR_SRSNode *OGRSpatialReference::GetRoot() const
1193 : {
1194 7454 : TAKE_OPTIONAL_LOCK();
1195 :
1196 7454 : if (!d->m_poRoot)
1197 : {
1198 2707 : d->refreshRootFromProjObj();
1199 : }
1200 14908 : return d->m_poRoot;
1201 : }
1202 :
1203 : /************************************************************************/
1204 : /* SetRoot() */
1205 : /************************************************************************/
1206 :
1207 : /**
1208 : * \brief Set the root SRS node.
1209 : *
1210 : * If the object has an existing tree of OGR_SRSNodes, they are destroyed
1211 : * as part of assigning the new root. Ownership of the passed OGR_SRSNode is
1212 : * is assumed by the OGRSpatialReference.
1213 : *
1214 : * @param poNewRoot object to assign as root.
1215 : */
1216 :
1217 44 : void OGRSpatialReference::SetRoot(OGR_SRSNode *poNewRoot)
1218 :
1219 : {
1220 44 : if (d->m_poRoot != poNewRoot)
1221 : {
1222 44 : delete d->m_poRoot;
1223 44 : d->setRoot(poNewRoot);
1224 : }
1225 44 : }
1226 :
1227 : /************************************************************************/
1228 : /* GetAttrNode() */
1229 : /************************************************************************/
1230 :
1231 : /**
1232 : * \brief Find named node in tree.
1233 : *
1234 : * This method does a pre-order traversal of the node tree searching for
1235 : * a node with this exact value (case insensitive), and returns it. Leaf
1236 : * nodes are not considered, under the assumption that they are just
1237 : * attribute value nodes.
1238 : *
1239 : * If a node appears more than once in the tree (such as UNIT for instance),
1240 : * the first encountered will be returned. Use GetNode() on a subtree to be
1241 : * more specific.
1242 : *
1243 : * @param pszNodePath the name of the node to search for. May contain multiple
1244 : * components such as "GEOGCS|UNIT".
1245 : *
1246 : * @return a pointer to the node found, or NULL if none.
1247 : */
1248 :
1249 82319 : OGR_SRSNode *OGRSpatialReference::GetAttrNode(const char *pszNodePath)
1250 :
1251 : {
1252 82319 : if (strchr(pszNodePath, '|') == nullptr)
1253 : {
1254 : // Fast path
1255 44841 : OGR_SRSNode *poNode = GetRoot();
1256 44841 : if (poNode)
1257 43640 : poNode = poNode->GetNode(pszNodePath);
1258 44841 : return poNode;
1259 : }
1260 :
1261 : char **papszPathTokens =
1262 37478 : CSLTokenizeStringComplex(pszNodePath, "|", TRUE, FALSE);
1263 :
1264 37478 : if (CSLCount(papszPathTokens) < 1)
1265 : {
1266 0 : CSLDestroy(papszPathTokens);
1267 0 : return nullptr;
1268 : }
1269 :
1270 37478 : OGR_SRSNode *poNode = GetRoot();
1271 113478 : for (int i = 0; poNode != nullptr && papszPathTokens[i] != nullptr; i++)
1272 : {
1273 76000 : poNode = poNode->GetNode(papszPathTokens[i]);
1274 : }
1275 :
1276 37478 : CSLDestroy(papszPathTokens);
1277 :
1278 37478 : return poNode;
1279 : }
1280 :
1281 : /**
1282 : * \brief Find named node in tree.
1283 : *
1284 : * This method does a pre-order traversal of the node tree searching for
1285 : * a node with this exact value (case insensitive), and returns it. Leaf
1286 : * nodes are not considered, under the assumption that they are just
1287 : * attribute value nodes.
1288 : *
1289 : * If a node appears more than once in the tree (such as UNIT for instance),
1290 : * the first encountered will be returned. Use GetNode() on a subtree to be
1291 : * more specific.
1292 : *
1293 : * @param pszNodePath the name of the node to search for. May contain multiple
1294 : * components such as "GEOGCS|UNIT".
1295 : *
1296 : * @return a pointer to the node found, or NULL if none.
1297 : */
1298 :
1299 : const OGR_SRSNode *
1300 74556 : OGRSpatialReference::GetAttrNode(const char *pszNodePath) const
1301 :
1302 : {
1303 : OGR_SRSNode *poNode =
1304 74556 : const_cast<OGRSpatialReference *>(this)->GetAttrNode(pszNodePath);
1305 :
1306 74556 : return poNode;
1307 : }
1308 :
1309 : /************************************************************************/
1310 : /* GetAttrValue() */
1311 : /************************************************************************/
1312 :
1313 : /**
1314 : * \brief Fetch indicated attribute of named node.
1315 : *
1316 : * This method uses GetAttrNode() to find the named node, and then extracts
1317 : * the value of the indicated child. Thus a call to GetAttrValue("UNIT",1)
1318 : * would return the second child of the UNIT node, which is normally the
1319 : * length of the linear unit in meters.
1320 : *
1321 : * This method does the same thing as the C function OSRGetAttrValue().
1322 : *
1323 : * @param pszNodeName the tree node to look for (case insensitive).
1324 : * @param iAttr the child of the node to fetch (zero based).
1325 : *
1326 : * @return the requested value, or NULL if it fails for any reason.
1327 : */
1328 :
1329 22739 : const char *OGRSpatialReference::GetAttrValue(const char *pszNodeName,
1330 : int iAttr) const
1331 :
1332 : {
1333 22739 : const OGR_SRSNode *poNode = GetAttrNode(pszNodeName);
1334 22739 : if (poNode == nullptr)
1335 : {
1336 9928 : if (d->m_bNodesWKT2 && EQUAL(pszNodeName, "PROJECTION"))
1337 : {
1338 14 : return GetAttrValue("METHOD", iAttr);
1339 : }
1340 9914 : else if (d->m_bNodesWKT2 && EQUAL(pszNodeName, "PROJCS|PROJECTION"))
1341 : {
1342 0 : return GetAttrValue("PROJCRS|METHOD", iAttr);
1343 : }
1344 9914 : else if (d->m_bNodesWKT2 && EQUAL(pszNodeName, "PROJCS"))
1345 : {
1346 1 : return GetAttrValue("PROJCRS", iAttr);
1347 : }
1348 9913 : return nullptr;
1349 : }
1350 :
1351 12811 : if (iAttr < 0 || iAttr >= poNode->GetChildCount())
1352 0 : return nullptr;
1353 :
1354 12811 : return poNode->GetChild(iAttr)->GetValue();
1355 : }
1356 :
1357 : /************************************************************************/
1358 : /* OSRGetAttrValue() */
1359 : /************************************************************************/
1360 :
1361 : /**
1362 : * \brief Fetch indicated attribute of named node.
1363 : *
1364 : * This function is the same as OGRSpatialReference::GetAttrValue()
1365 : */
1366 34 : const char *CPL_STDCALL OSRGetAttrValue(OGRSpatialReferenceH hSRS,
1367 : const char *pszKey, int iChild)
1368 :
1369 : {
1370 34 : VALIDATE_POINTER1(hSRS, "OSRGetAttrValue", nullptr);
1371 :
1372 34 : return ToPointer(hSRS)->GetAttrValue(pszKey, iChild);
1373 : }
1374 :
1375 : /************************************************************************/
1376 : /* GetName() */
1377 : /************************************************************************/
1378 :
1379 : /**
1380 : * \brief Return the CRS name.
1381 : *
1382 : * The returned value is only short lived and should not be used after other
1383 : * calls to methods on this object.
1384 : *
1385 : * @since GDAL 3.0
1386 : */
1387 :
1388 5391 : const char *OGRSpatialReference::GetName() const
1389 : {
1390 10782 : TAKE_OPTIONAL_LOCK();
1391 :
1392 5391 : d->refreshProjObj();
1393 5391 : if (!d->m_pj_crs)
1394 113 : return nullptr;
1395 5278 : const char *pszName = proj_get_name(d->m_pj_crs);
1396 : #if PROJ_VERSION_NUMBER == PROJ_COMPUTE_VERSION(8, 2, 0)
1397 : if (d->m_pjType == PJ_TYPE_BOUND_CRS && EQUAL(pszName, "SOURCECRS"))
1398 : {
1399 : // Work around a bug of PROJ 8.2.0 (fixed in 8.2.1)
1400 : PJ *baseCRS = proj_get_source_crs(d->getPROJContext(), d->m_pj_crs);
1401 : if (baseCRS)
1402 : {
1403 : pszName = proj_get_name(baseCRS);
1404 : // pszName still remains valid after proj_destroy(), since
1405 : // d->m_pj_crs keeps a reference to the base CRS C++ object.
1406 : proj_destroy(baseCRS);
1407 : }
1408 : }
1409 : #endif
1410 5278 : return pszName;
1411 : }
1412 :
1413 : /************************************************************************/
1414 : /* OSRGetName() */
1415 : /************************************************************************/
1416 :
1417 : /**
1418 : * \brief Return the CRS name.
1419 : *
1420 : * The returned value is only short lived and should not be used after other
1421 : * calls to methods on this object.
1422 : *
1423 : * @since GDAL 3.0
1424 : */
1425 43 : const char *OSRGetName(OGRSpatialReferenceH hSRS)
1426 :
1427 : {
1428 43 : VALIDATE_POINTER1(hSRS, "OSRGetName", nullptr);
1429 :
1430 43 : return ToPointer(hSRS)->GetName();
1431 : }
1432 :
1433 : /************************************************************************/
1434 : /* GetCelestialBodyName() */
1435 : /************************************************************************/
1436 :
1437 : /**
1438 : * \brief Return the name of the celestial body of this CRS.
1439 : *
1440 : * e.g. "Earth" for an Earth CRS
1441 : *
1442 : * The returned value is only short lived and should not be used after other
1443 : * calls to methods on this object.
1444 : *
1445 : * @since GDAL 3.12 and PROJ 8.1
1446 : */
1447 :
1448 3 : const char *OGRSpatialReference::GetCelestialBodyName() const
1449 : {
1450 : #if PROJ_AT_LEAST_VERSION(8, 1, 0)
1451 :
1452 : TAKE_OPTIONAL_LOCK();
1453 :
1454 : d->refreshProjObj();
1455 : if (!d->m_pj_crs)
1456 : return nullptr;
1457 : d->demoteFromBoundCRS();
1458 : const char *name =
1459 : proj_get_celestial_body_name(d->getPROJContext(), d->m_pj_crs);
1460 : if (name)
1461 : {
1462 : d->m_celestialBodyName = name;
1463 : }
1464 : d->undoDemoteFromBoundCRS();
1465 : return d->m_celestialBodyName.c_str();
1466 : #else
1467 3 : if (std::fabs(GetSemiMajor(nullptr) - SRS_WGS84_SEMIMAJOR) <=
1468 : 0.05 * SRS_WGS84_SEMIMAJOR)
1469 3 : return "Earth";
1470 0 : const char *pszAuthName = GetAuthorityName(nullptr);
1471 0 : if (pszAuthName && EQUAL(pszAuthName, "EPSG"))
1472 0 : return "Earth";
1473 0 : return nullptr;
1474 : #endif
1475 : }
1476 :
1477 : /************************************************************************/
1478 : /* OSRGetCelestialBodyName() */
1479 : /************************************************************************/
1480 :
1481 : /**
1482 : * \brief Return the name of the celestial body of this CRS.
1483 : *
1484 : * e.g. "Earth" for an Earth CRS
1485 : *
1486 : * The returned value is only short lived and should not be used after other
1487 : * calls to methods on this object.
1488 : *
1489 : * @since GDAL 3.12 and PROJ 8.1
1490 : */
1491 :
1492 1 : const char *OSRGetCelestialBodyName(OGRSpatialReferenceH hSRS)
1493 :
1494 : {
1495 1 : VALIDATE_POINTER1(hSRS, "GetCelestialBodyName", nullptr);
1496 :
1497 1 : return ToPointer(hSRS)->GetCelestialBodyName();
1498 : }
1499 :
1500 : /************************************************************************/
1501 : /* Clone() */
1502 : /************************************************************************/
1503 :
1504 : /**
1505 : * \brief Make a duplicate of this OGRSpatialReference.
1506 : *
1507 : * This method is the same as the C function OSRClone().
1508 : *
1509 : * @return a new SRS, which becomes the responsibility of the caller.
1510 : */
1511 :
1512 30672 : OGRSpatialReference *OGRSpatialReference::Clone() const
1513 :
1514 : {
1515 30672 : OGRSpatialReference *poNewRef = new OGRSpatialReference();
1516 :
1517 30672 : TAKE_OPTIONAL_LOCK();
1518 :
1519 30671 : d->refreshProjObj();
1520 30672 : if (d->m_pj_crs != nullptr)
1521 30619 : poNewRef->d->setPjCRS(proj_clone(d->getPROJContext(), d->m_pj_crs));
1522 30672 : if (d->m_bHasCenterLong && d->m_poRoot)
1523 : {
1524 0 : poNewRef->d->setRoot(d->m_poRoot->Clone());
1525 : }
1526 30672 : poNewRef->d->m_axisMapping = d->m_axisMapping;
1527 30671 : poNewRef->d->m_axisMappingStrategy = d->m_axisMappingStrategy;
1528 30672 : poNewRef->d->m_coordinateEpoch = d->m_coordinateEpoch;
1529 61343 : return poNewRef;
1530 : }
1531 :
1532 : /************************************************************************/
1533 : /* OSRClone() */
1534 : /************************************************************************/
1535 :
1536 : /**
1537 : * \brief Make a duplicate of this OGRSpatialReference.
1538 : *
1539 : * This function is the same as OGRSpatialReference::Clone()
1540 : */
1541 1081 : OGRSpatialReferenceH CPL_STDCALL OSRClone(OGRSpatialReferenceH hSRS)
1542 :
1543 : {
1544 1081 : VALIDATE_POINTER1(hSRS, "OSRClone", nullptr);
1545 :
1546 1081 : return ToHandle(ToPointer(hSRS)->Clone());
1547 : }
1548 :
1549 : /************************************************************************/
1550 : /* dumpReadable() */
1551 : /************************************************************************/
1552 :
1553 : /** Dump pretty wkt to stdout, mostly for debugging.
1554 : */
1555 0 : void OGRSpatialReference::dumpReadable()
1556 :
1557 : {
1558 0 : char *pszPrettyWkt = nullptr;
1559 :
1560 0 : const char *const apszOptions[] = {"FORMAT=WKT2", "MULTILINE=YES", nullptr};
1561 0 : exportToWkt(&pszPrettyWkt, apszOptions);
1562 0 : printf("%s\n", pszPrettyWkt); /*ok*/
1563 0 : CPLFree(pszPrettyWkt);
1564 0 : }
1565 :
1566 : /************************************************************************/
1567 : /* exportToPrettyWkt() */
1568 : /************************************************************************/
1569 :
1570 : /**
1571 : * Convert this SRS into a nicely formatted WKT 1 string for display to a
1572 : * person.
1573 : *
1574 : * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
1575 : * Issues</a> page for implementation details of WKT 1 in OGR.
1576 : *
1577 : * Note that the returned WKT string should be freed with
1578 : * CPLFree() when no longer needed. It is the responsibility of the caller.
1579 : *
1580 : * The WKT version can be overridden by using the OSR_WKT_FORMAT configuration
1581 : * option. Valid values are the one of the FORMAT option of
1582 : * exportToWkt( char ** ppszResult, const char* const* papszOptions ) const
1583 : *
1584 : * This method is the same as the C function OSRExportToPrettyWkt().
1585 : *
1586 : * @param ppszResult the resulting string is returned in this pointer.
1587 : * @param bSimplify TRUE if the AXIS, AUTHORITY and EXTENSION nodes should be
1588 : * stripped off.
1589 : *
1590 : * @return OGRERR_NONE if successful.
1591 : */
1592 :
1593 58 : OGRErr OGRSpatialReference::exportToPrettyWkt(char **ppszResult,
1594 : int bSimplify) const
1595 :
1596 : {
1597 116 : CPLStringList aosOptions;
1598 58 : aosOptions.SetNameValue("MULTILINE", "YES");
1599 58 : if (bSimplify)
1600 : {
1601 0 : aosOptions.SetNameValue("FORMAT", "WKT1_SIMPLE");
1602 : }
1603 116 : return exportToWkt(ppszResult, aosOptions.List());
1604 : }
1605 :
1606 : /************************************************************************/
1607 : /* OSRExportToPrettyWkt() */
1608 : /************************************************************************/
1609 :
1610 : /**
1611 : * \brief Convert this SRS into a nicely formatted WKT 1 string for display to a
1612 : * person.
1613 : *
1614 : * The WKT version can be overridden by using the OSR_WKT_FORMAT configuration
1615 : * option. Valid values are the one of the FORMAT option of
1616 : * exportToWkt( char ** ppszResult, const char* const* papszOptions ) const
1617 : *
1618 : * This function is the same as OGRSpatialReference::exportToPrettyWkt().
1619 : */
1620 :
1621 56 : OGRErr CPL_STDCALL OSRExportToPrettyWkt(OGRSpatialReferenceH hSRS,
1622 : char **ppszReturn, int bSimplify)
1623 :
1624 : {
1625 56 : VALIDATE_POINTER1(hSRS, "OSRExportToPrettyWkt", OGRERR_FAILURE);
1626 :
1627 56 : *ppszReturn = nullptr;
1628 :
1629 56 : return ToPointer(hSRS)->exportToPrettyWkt(ppszReturn, bSimplify);
1630 : }
1631 :
1632 : /************************************************************************/
1633 : /* exportToWkt() */
1634 : /************************************************************************/
1635 :
1636 : /**
1637 : * \brief Convert this SRS into WKT 1 format.
1638 : *
1639 : * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
1640 : * Issues</a> page for implementation details of WKT 1 in OGR.
1641 : *
1642 : * Note that the returned WKT string should be freed with
1643 : * CPLFree() when no longer needed. It is the responsibility of the caller.
1644 : *
1645 : * The WKT version can be overridden by using the OSR_WKT_FORMAT configuration
1646 : * option. Valid values are the one of the FORMAT option of
1647 : * exportToWkt( char ** ppszResult, const char* const* papszOptions ) const
1648 : *
1649 : * This method is the same as the C function OSRExportToWkt().
1650 : *
1651 : * @param ppszResult the resulting string is returned in this pointer.
1652 : *
1653 : * @return OGRERR_NONE if successful.
1654 : */
1655 :
1656 12406 : OGRErr OGRSpatialReference::exportToWkt(char **ppszResult) const
1657 :
1658 : {
1659 12406 : return exportToWkt(ppszResult, nullptr);
1660 : }
1661 :
1662 : /************************************************************************/
1663 : /* GDAL_proj_crs_create_bound_crs_to_WGS84() */
1664 : /************************************************************************/
1665 :
1666 563 : static PJ *GDAL_proj_crs_create_bound_crs_to_WGS84(PJ_CONTEXT *ctx, PJ *pj,
1667 : bool onlyIfEPSGCode,
1668 : bool canModifyHorizPart)
1669 : {
1670 563 : PJ *ret = nullptr;
1671 563 : if (proj_get_type(pj) == PJ_TYPE_COMPOUND_CRS)
1672 : {
1673 13 : auto horizCRS = proj_crs_get_sub_crs(ctx, pj, 0);
1674 13 : auto vertCRS = proj_crs_get_sub_crs(ctx, pj, 1);
1675 13 : if (horizCRS && proj_get_type(horizCRS) != PJ_TYPE_BOUND_CRS &&
1676 26 : vertCRS &&
1677 10 : (!onlyIfEPSGCode || proj_get_id_auth_name(horizCRS, 0) != nullptr))
1678 : {
1679 : auto boundHoriz =
1680 : canModifyHorizPart
1681 3 : ? proj_crs_create_bound_crs_to_WGS84(ctx, horizCRS, nullptr)
1682 3 : : proj_clone(ctx, horizCRS);
1683 : auto boundVert =
1684 3 : proj_crs_create_bound_crs_to_WGS84(ctx, vertCRS, nullptr);
1685 3 : if (boundHoriz && boundVert)
1686 : {
1687 3 : ret = proj_create_compound_crs(ctx, proj_get_name(pj),
1688 : boundHoriz, boundVert);
1689 : }
1690 3 : proj_destroy(boundHoriz);
1691 3 : proj_destroy(boundVert);
1692 : }
1693 13 : proj_destroy(horizCRS);
1694 13 : proj_destroy(vertCRS);
1695 : }
1696 1060 : else if (proj_get_type(pj) != PJ_TYPE_BOUND_CRS &&
1697 510 : (!onlyIfEPSGCode || proj_get_id_auth_name(pj, 0) != nullptr))
1698 : {
1699 240 : ret = proj_crs_create_bound_crs_to_WGS84(ctx, pj, nullptr);
1700 : }
1701 563 : return ret;
1702 : }
1703 :
1704 : /************************************************************************/
1705 : /* exportToWkt() */
1706 : /************************************************************************/
1707 :
1708 : /**
1709 : * Convert this SRS into a WKT string.
1710 : *
1711 : * Note that the returned WKT string should be freed with
1712 : * CPLFree() when no longer needed. It is the responsibility of the caller.
1713 : *
1714 : * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
1715 : * Issues</a> page for implementation details of WKT 1 in OGR.
1716 : *
1717 : * @param ppszResult the resulting string is returned in this pointer.
1718 : * @param papszOptions NULL terminated list of options, or NULL. Currently
1719 : * supported options are
1720 : * <ul>
1721 : * <li>MULTILINE=YES/NO. Defaults to NO.</li>
1722 : * <li>FORMAT=SFSQL/WKT1_SIMPLE/WKT1/WKT1_GDAL/WKT1_ESRI/WKT2_2015/WKT2_2018/WKT2/DEFAULT.
1723 : * If SFSQL, a WKT1 string without AXIS, TOWGS84, AUTHORITY or EXTENSION
1724 : * node is returned.
1725 : * If WKT1_SIMPLE, a WKT1 string without AXIS, AUTHORITY or EXTENSION
1726 : * node is returned.
1727 : * WKT1 is an alias of WKT1_GDAL.
1728 : * WKT2 will default to the latest revision implemented (currently
1729 : * WKT2_2018) WKT2_2019 can be used as an alias of WKT2_2018 since GDAL 3.2
1730 : * </li>
1731 : * <li>ALLOW_ELLIPSOIDAL_HEIGHT_AS_VERTICAL_CRS=YES/NO. Default is NO. If set
1732 : * to YES and FORMAT=WKT1_GDAL, a Geographic 3D CRS or a Projected 3D CRS will
1733 : * be exported as a compound CRS whose vertical part represents an ellipsoidal
1734 : * height (for example for use with LAS 1.4 WKT1).
1735 : * Requires PROJ 7.2.1 and GDAL 3.2.1.</li>
1736 : * </ul>
1737 : *
1738 : * Starting with GDAL 3.0.3, if the OSR_ADD_TOWGS84_ON_EXPORT_TO_WKT1
1739 : * configuration option is set to YES, when exporting to WKT1_GDAL, this method
1740 : * will try to add a TOWGS84[] node, if there's none attached yet to the SRS and
1741 : * if the SRS has a EPSG code. See the AddGuessedTOWGS84() method for how this
1742 : * TOWGS84[] node may be added.
1743 : *
1744 : * @return OGRERR_NONE if successful.
1745 : * @since GDAL 3.0
1746 : */
1747 :
1748 16936 : OGRErr OGRSpatialReference::exportToWkt(char **ppszResult,
1749 : const char *const *papszOptions) const
1750 : {
1751 : // In the past calling this method was thread-safe, even if we never
1752 : // guaranteed it. Now proj_as_wkt() will cache the result internally,
1753 : // so this is no longer thread-safe.
1754 33873 : std::lock_guard oLock(d->m_mutex);
1755 :
1756 16937 : d->refreshProjObj();
1757 16937 : if (!d->m_pj_crs)
1758 : {
1759 21 : *ppszResult = CPLStrdup("");
1760 21 : return OGRERR_FAILURE;
1761 : }
1762 :
1763 16916 : if (d->m_bHasCenterLong && d->m_poRoot && !d->m_bMorphToESRI)
1764 : {
1765 0 : return d->m_poRoot->exportToWkt(ppszResult);
1766 : }
1767 :
1768 16916 : auto ctxt = d->getPROJContext();
1769 16916 : auto wktFormat = PJ_WKT1_GDAL;
1770 : const char *pszFormat =
1771 16916 : CSLFetchNameValueDef(papszOptions, "FORMAT",
1772 : CPLGetConfigOption("OSR_WKT_FORMAT", "DEFAULT"));
1773 16916 : if (EQUAL(pszFormat, "DEFAULT"))
1774 14252 : pszFormat = "";
1775 :
1776 16916 : if (EQUAL(pszFormat, "WKT1_ESRI") || d->m_bMorphToESRI)
1777 : {
1778 668 : wktFormat = PJ_WKT1_ESRI;
1779 : }
1780 16248 : else if (EQUAL(pszFormat, "WKT1") || EQUAL(pszFormat, "WKT1_GDAL") ||
1781 15410 : EQUAL(pszFormat, "WKT1_SIMPLE") || EQUAL(pszFormat, "SFSQL"))
1782 : {
1783 843 : wktFormat = PJ_WKT1_GDAL;
1784 : }
1785 15405 : else if (EQUAL(pszFormat, "WKT2_2015"))
1786 : {
1787 287 : wktFormat = PJ_WKT2_2015;
1788 : }
1789 15118 : else if (EQUAL(pszFormat, "WKT2") || EQUAL(pszFormat, "WKT2_2018") ||
1790 14782 : EQUAL(pszFormat, "WKT2_2019"))
1791 : {
1792 1126 : wktFormat = PJ_WKT2_2018;
1793 : }
1794 13992 : else if (pszFormat[0] == '\0')
1795 : {
1796 : // cppcheck-suppress knownConditionTrueFalse
1797 13992 : if (IsDerivedGeographic())
1798 : {
1799 2 : wktFormat = PJ_WKT2_2018;
1800 : }
1801 27350 : else if ((IsGeographic() || IsProjected()) && !IsCompound() &&
1802 13360 : GetAxesCount() == 3)
1803 : {
1804 56 : wktFormat = PJ_WKT2_2018;
1805 : }
1806 : }
1807 : else
1808 : {
1809 0 : CPLError(CE_Failure, CPLE_AppDefined, "Unsupported value for FORMAT");
1810 0 : *ppszResult = CPLStrdup("");
1811 0 : return OGRERR_FAILURE;
1812 : }
1813 :
1814 33832 : CPLStringList aosOptions;
1815 16916 : if (wktFormat != PJ_WKT1_ESRI)
1816 : {
1817 16248 : aosOptions.SetNameValue("OUTPUT_AXIS", "YES");
1818 : }
1819 : aosOptions.SetNameValue(
1820 16916 : "MULTILINE", CSLFetchNameValueDef(papszOptions, "MULTILINE", "NO"));
1821 :
1822 16916 : const char *pszAllowEllpsHeightAsVertCS = CSLFetchNameValue(
1823 : papszOptions, "ALLOW_ELLIPSOIDAL_HEIGHT_AS_VERTICAL_CRS");
1824 16916 : if (pszAllowEllpsHeightAsVertCS)
1825 : {
1826 : aosOptions.SetNameValue("ALLOW_ELLIPSOIDAL_HEIGHT_AS_VERTICAL_CRS",
1827 0 : pszAllowEllpsHeightAsVertCS);
1828 : }
1829 :
1830 16916 : PJ *boundCRS = nullptr;
1831 31693 : if (wktFormat == PJ_WKT1_GDAL &&
1832 14777 : CPLTestBool(CSLFetchNameValueDef(
1833 : papszOptions, "ADD_TOWGS84_ON_EXPORT_TO_WKT1",
1834 : CPLGetConfigOption("OSR_ADD_TOWGS84_ON_EXPORT_TO_WKT1", "NO"))))
1835 : {
1836 0 : boundCRS = GDAL_proj_crs_create_bound_crs_to_WGS84(
1837 0 : d->getPROJContext(), d->m_pj_crs, true, true);
1838 : }
1839 :
1840 33832 : CPLErrorAccumulator oErrorAccumulator;
1841 : const char *pszWKT;
1842 : {
1843 16916 : auto oAccumulator = oErrorAccumulator.InstallForCurrentScope();
1844 16916 : CPL_IGNORE_RET_VAL(oAccumulator);
1845 16915 : pszWKT = proj_as_wkt(ctxt, boundCRS ? boundCRS : d->m_pj_crs, wktFormat,
1846 16915 : aosOptions.List());
1847 : }
1848 16917 : for (const auto &oError : oErrorAccumulator.GetErrors())
1849 : {
1850 32 : if (pszFormat[0] == '\0' &&
1851 14 : (oError.msg.find("Unsupported conversion method") !=
1852 2 : std::string::npos ||
1853 2 : oError.msg.find("can only be exported to WKT2") !=
1854 0 : std::string::npos ||
1855 0 : oError.msg.find("can only be exported since WKT2:2019") !=
1856 : std::string::npos))
1857 : {
1858 14 : CPLErrorReset();
1859 : // If we cannot export in the default mode (WKT1), retry with WKT2
1860 14 : pszWKT = proj_as_wkt(ctxt, boundCRS ? boundCRS : d->m_pj_crs,
1861 14 : PJ_WKT2_2018, aosOptions.List());
1862 14 : break;
1863 : }
1864 2 : CPLError(oError.type, oError.no, "%s", oError.msg.c_str());
1865 : }
1866 :
1867 16915 : if (!pszWKT)
1868 : {
1869 2 : *ppszResult = CPLStrdup("");
1870 2 : proj_destroy(boundCRS);
1871 2 : return OGRERR_FAILURE;
1872 : }
1873 :
1874 16913 : if (EQUAL(pszFormat, "SFSQL") || EQUAL(pszFormat, "WKT1_SIMPLE"))
1875 : {
1876 4 : OGR_SRSNode oRoot;
1877 5 : oRoot.importFromWkt(&pszWKT);
1878 5 : oRoot.StripNodes("AXIS");
1879 5 : if (EQUAL(pszFormat, "SFSQL"))
1880 : {
1881 3 : oRoot.StripNodes("TOWGS84");
1882 : }
1883 5 : oRoot.StripNodes("AUTHORITY");
1884 5 : oRoot.StripNodes("EXTENSION");
1885 : OGRErr eErr;
1886 5 : if (CPLTestBool(CSLFetchNameValueDef(papszOptions, "MULTILINE", "NO")))
1887 2 : eErr = oRoot.exportToPrettyWkt(ppszResult, 1);
1888 : else
1889 3 : eErr = oRoot.exportToWkt(ppszResult);
1890 5 : proj_destroy(boundCRS);
1891 5 : return eErr;
1892 : }
1893 :
1894 16909 : *ppszResult = CPLStrdup(pszWKT);
1895 :
1896 : #if !(PROJ_AT_LEAST_VERSION(9, 5, 0))
1897 16909 : if (wktFormat == PJ_WKT2_2018)
1898 : {
1899 : // Works around bug fixed per https://github.com/OSGeo/PROJ/pull/4166
1900 : // related to a wrong EPSG code assigned to UTM South conversions
1901 1184 : char *pszPtr = strstr(*ppszResult, "CONVERSION[\"UTM zone ");
1902 1184 : if (pszPtr)
1903 : {
1904 285 : pszPtr += strlen("CONVERSION[\"UTM zone ");
1905 285 : const int nZone = atoi(pszPtr);
1906 854 : while (*pszPtr >= '0' && *pszPtr <= '9')
1907 569 : ++pszPtr;
1908 285 : if (nZone >= 1 && nZone <= 60 && *pszPtr == 'S' &&
1909 1 : pszPtr[1] == '"' && pszPtr[2] == ',')
1910 : {
1911 1 : pszPtr += 3;
1912 1 : int nLevel = 0;
1913 1 : bool bInString = false;
1914 : // Find the ID node corresponding to this CONVERSION node
1915 480 : while (*pszPtr)
1916 : {
1917 480 : if (bInString)
1918 : {
1919 197 : if (*pszPtr == '"' && pszPtr[1] == '"')
1920 : {
1921 0 : ++pszPtr;
1922 : }
1923 197 : else if (*pszPtr == '"')
1924 : {
1925 17 : bInString = false;
1926 : }
1927 : }
1928 283 : else if (nLevel == 0 && STARTS_WITH_CI(pszPtr, "ID["))
1929 : {
1930 1 : if (STARTS_WITH_CI(pszPtr, CPLSPrintf("ID[\"EPSG\",%d]",
1931 : 17000 + nZone)))
1932 : {
1933 1 : CPLAssert(pszPtr[11] == '7');
1934 1 : CPLAssert(pszPtr[12] == '0');
1935 1 : pszPtr[11] = '6';
1936 1 : pszPtr[12] = '1';
1937 : }
1938 1 : break;
1939 : }
1940 282 : else if (*pszPtr == '"')
1941 : {
1942 17 : bInString = true;
1943 : }
1944 265 : else if (*pszPtr == '[')
1945 : {
1946 17 : ++nLevel;
1947 : }
1948 248 : else if (*pszPtr == ']')
1949 : {
1950 17 : --nLevel;
1951 : }
1952 :
1953 479 : ++pszPtr;
1954 : }
1955 : }
1956 : }
1957 : }
1958 : #endif
1959 :
1960 16909 : proj_destroy(boundCRS);
1961 16909 : return OGRERR_NONE;
1962 : }
1963 :
1964 : /************************************************************************/
1965 : /* exportToWkt() */
1966 : /************************************************************************/
1967 :
1968 : /**
1969 : * Convert this SRS into a WKT string.
1970 : *
1971 : * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
1972 : * Issues</a> page for implementation details of WKT 1 in OGR.
1973 : *
1974 : * @param papszOptions NULL terminated list of options, or NULL. Currently
1975 : * supported options are
1976 : * <ul>
1977 : * <li>MULTILINE=YES/NO. Defaults to NO.</li>
1978 : * <li>FORMAT=SFSQL/WKT1_SIMPLE/WKT1/WKT1_GDAL/WKT1_ESRI/WKT2_2015/WKT2_2018/WKT2/DEFAULT.
1979 : * If SFSQL, a WKT1 string without AXIS, TOWGS84, AUTHORITY or EXTENSION
1980 : * node is returned.
1981 : * If WKT1_SIMPLE, a WKT1 string without AXIS, AUTHORITY or EXTENSION
1982 : * node is returned.
1983 : * WKT1 is an alias of WKT1_GDAL.
1984 : * WKT2 will default to the latest revision implemented (currently
1985 : * WKT2_2019)
1986 : * </li>
1987 : * <li>ALLOW_ELLIPSOIDAL_HEIGHT_AS_VERTICAL_CRS=YES/NO. Default is NO. If set
1988 : * to YES and FORMAT=WKT1_GDAL, a Geographic 3D CRS or a Projected 3D CRS will
1989 : * be exported as a compound CRS whose vertical part represents an ellipsoidal
1990 : * height (for example for use with LAS 1.4 WKT1).
1991 : * Requires PROJ 7.2.1.</li>
1992 : * </ul>
1993 : *
1994 : * If the OSR_ADD_TOWGS84_ON_EXPORT_TO_WKT1
1995 : * configuration option is set to YES, when exporting to WKT1_GDAL, this method
1996 : * will try to add a TOWGS84[] node, if there's none attached yet to the SRS and
1997 : * if the SRS has a EPSG code. See the AddGuessedTOWGS84() method for how this
1998 : * TOWGS84[] node may be added.
1999 : *
2000 : * @return a non-empty string if successful.
2001 : * @since GDAL 3.9
2002 : */
2003 :
2004 : std::string
2005 195 : OGRSpatialReference::exportToWkt(const char *const *papszOptions) const
2006 : {
2007 195 : std::string osWKT;
2008 195 : char *pszWKT = nullptr;
2009 195 : if (exportToWkt(&pszWKT, papszOptions) == OGRERR_NONE)
2010 195 : osWKT = pszWKT;
2011 195 : CPLFree(pszWKT);
2012 390 : return osWKT;
2013 : }
2014 :
2015 : /************************************************************************/
2016 : /* OSRExportToWkt() */
2017 : /************************************************************************/
2018 :
2019 : /**
2020 : * \brief Convert this SRS into WKT 1 format.
2021 : *
2022 : * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2023 : * Issues</a> page for implementation details of WKT in OGR.
2024 : *
2025 : * The WKT version can be overridden by using the OSR_WKT_FORMAT configuration
2026 : * option. Valid values are the one of the FORMAT option of
2027 : * exportToWkt( char ** ppszResult, const char* const* papszOptions ) const
2028 : *
2029 : * This function is the same as OGRSpatialReference::exportToWkt().
2030 : */
2031 :
2032 924 : OGRErr CPL_STDCALL OSRExportToWkt(OGRSpatialReferenceH hSRS, char **ppszReturn)
2033 :
2034 : {
2035 924 : VALIDATE_POINTER1(hSRS, "OSRExportToWkt", OGRERR_FAILURE);
2036 :
2037 924 : *ppszReturn = nullptr;
2038 :
2039 924 : return ToPointer(hSRS)->exportToWkt(ppszReturn);
2040 : }
2041 :
2042 : /************************************************************************/
2043 : /* OSRExportToWktEx() */
2044 : /************************************************************************/
2045 :
2046 : /**
2047 : * \brief Convert this SRS into WKT format.
2048 : *
2049 : * This function is the same as OGRSpatialReference::exportToWkt(char **
2050 : * ppszResult,const char* const* papszOptions ) const
2051 : *
2052 : * @since GDAL 3.0
2053 : */
2054 :
2055 1328 : OGRErr OSRExportToWktEx(OGRSpatialReferenceH hSRS, char **ppszReturn,
2056 : const char *const *papszOptions)
2057 : {
2058 1328 : VALIDATE_POINTER1(hSRS, "OSRExportToWktEx", OGRERR_FAILURE);
2059 :
2060 1328 : *ppszReturn = nullptr;
2061 :
2062 1328 : return ToPointer(hSRS)->exportToWkt(ppszReturn, papszOptions);
2063 : }
2064 :
2065 : /************************************************************************/
2066 : /* exportToPROJJSON() */
2067 : /************************************************************************/
2068 :
2069 : /**
2070 : * Convert this SRS into a PROJJSON string.
2071 : *
2072 : * Note that the returned JSON string should be freed with
2073 : * CPLFree() when no longer needed. It is the responsibility of the caller.
2074 : *
2075 : * @param ppszResult the resulting string is returned in this pointer.
2076 : * @param papszOptions NULL terminated list of options, or NULL. Currently
2077 : * supported options are
2078 : * <ul>
2079 : * <li>MULTILINE=YES/NO. Defaults to YES</li>
2080 : * <li>INDENTATION_WIDTH=number. Defaults to 2 (when multiline output is
2081 : * on).</li>
2082 : * <li>SCHEMA=string. URL to PROJJSON schema. Can be set to empty string to
2083 : * disable it.</li>
2084 : * </ul>
2085 : *
2086 : * @return OGRERR_NONE if successful.
2087 : * @since GDAL 3.1 and PROJ 6.2
2088 : */
2089 :
2090 2351 : OGRErr OGRSpatialReference::exportToPROJJSON(
2091 : char **ppszResult, CPL_UNUSED const char *const *papszOptions) const
2092 : {
2093 4702 : TAKE_OPTIONAL_LOCK();
2094 :
2095 2351 : d->refreshProjObj();
2096 2351 : if (!d->m_pj_crs)
2097 : {
2098 0 : *ppszResult = nullptr;
2099 0 : return OGRERR_FAILURE;
2100 : }
2101 :
2102 : const char *pszPROJJSON =
2103 2351 : proj_as_projjson(d->getPROJContext(), d->m_pj_crs, papszOptions);
2104 :
2105 2351 : if (!pszPROJJSON)
2106 : {
2107 0 : *ppszResult = CPLStrdup("");
2108 0 : return OGRERR_FAILURE;
2109 : }
2110 :
2111 2351 : *ppszResult = CPLStrdup(pszPROJJSON);
2112 :
2113 : #if !(PROJ_AT_LEAST_VERSION(9, 5, 0))
2114 : {
2115 : // Works around bug fixed per https://github.com/OSGeo/PROJ/pull/4166
2116 : // related to a wrong EPSG code assigned to UTM South conversions
2117 2351 : char *pszPtr = strstr(*ppszResult, "\"name\": \"UTM zone ");
2118 2351 : if (pszPtr)
2119 : {
2120 242 : pszPtr += strlen("\"name\": \"UTM zone ");
2121 242 : const int nZone = atoi(pszPtr);
2122 725 : while (*pszPtr >= '0' && *pszPtr <= '9')
2123 483 : ++pszPtr;
2124 242 : if (nZone >= 1 && nZone <= 60 && *pszPtr == 'S' && pszPtr[1] == '"')
2125 : {
2126 4 : pszPtr += 2;
2127 4 : int nLevel = 0;
2128 4 : bool bInString = false;
2129 : // Find the id node corresponding to this conversion node
2130 5299 : while (*pszPtr)
2131 : {
2132 5299 : if (bInString)
2133 : {
2134 1950 : if (*pszPtr == '\\')
2135 : {
2136 0 : ++pszPtr;
2137 : }
2138 1950 : else if (*pszPtr == '"')
2139 : {
2140 244 : bInString = false;
2141 : }
2142 : }
2143 3349 : else if (nLevel == 0 && STARTS_WITH(pszPtr, "\"id\": {"))
2144 : {
2145 4 : const char *pszNextEndCurl = strchr(pszPtr, '}');
2146 : const char *pszAuthEPSG =
2147 4 : strstr(pszPtr, "\"authority\": \"EPSG\"");
2148 4 : char *pszCode = strstr(
2149 : pszPtr, CPLSPrintf("\"code\": %d", 17000 + nZone));
2150 4 : if (pszAuthEPSG && pszCode && pszNextEndCurl &&
2151 4 : pszNextEndCurl - pszAuthEPSG > 0 &&
2152 4 : pszNextEndCurl - pszCode > 0)
2153 : {
2154 4 : CPLAssert(pszCode[9] == '7');
2155 4 : CPLAssert(pszCode[10] == '0');
2156 4 : pszCode[9] = '6';
2157 4 : pszCode[10] = '1';
2158 : }
2159 4 : break;
2160 : }
2161 3345 : else if (*pszPtr == '"')
2162 : {
2163 244 : bInString = true;
2164 : }
2165 3101 : else if (*pszPtr == '{' || *pszPtr == '[')
2166 : {
2167 60 : ++nLevel;
2168 : }
2169 3041 : else if (*pszPtr == '}' || *pszPtr == ']')
2170 : {
2171 60 : --nLevel;
2172 : }
2173 :
2174 5295 : ++pszPtr;
2175 : }
2176 : }
2177 : }
2178 : }
2179 : #endif
2180 :
2181 2351 : return OGRERR_NONE;
2182 : }
2183 :
2184 : /************************************************************************/
2185 : /* OSRExportToPROJJSON() */
2186 : /************************************************************************/
2187 :
2188 : /**
2189 : * \brief Convert this SRS into PROJJSON format.
2190 : *
2191 : * This function is the same as OGRSpatialReference::exportToPROJJSON() const
2192 : *
2193 : * @since GDAL 3.1 and PROJ 6.2
2194 : */
2195 :
2196 71 : OGRErr OSRExportToPROJJSON(OGRSpatialReferenceH hSRS, char **ppszReturn,
2197 : const char *const *papszOptions)
2198 : {
2199 71 : VALIDATE_POINTER1(hSRS, "OSRExportToPROJJSON", OGRERR_FAILURE);
2200 :
2201 71 : *ppszReturn = nullptr;
2202 :
2203 71 : return ToPointer(hSRS)->exportToPROJJSON(ppszReturn, papszOptions);
2204 : }
2205 :
2206 : /************************************************************************/
2207 : /* importFromWkt() */
2208 : /************************************************************************/
2209 :
2210 : /**
2211 : * \brief Import from WKT string.
2212 : *
2213 : * This method will wipe the existing SRS definition, and
2214 : * reassign it based on the contents of the passed WKT string. Only as
2215 : * much of the input string as needed to construct this SRS is consumed from
2216 : * the input string, and the input string pointer
2217 : * is then updated to point to the remaining (unused) input.
2218 : *
2219 : * Starting with PROJ 9.2, if invoked on a COORDINATEMETADATA[] construct,
2220 : * the CRS contained in it will be used to fill the OGRSpatialReference object,
2221 : * and the coordinate epoch potentially present used as the coordinate epoch
2222 : * property of the OGRSpatialReference object.
2223 : *
2224 : * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2225 : * Issues</a> page for implementation details of WKT in OGR.
2226 : *
2227 : * This method is the same as the C function OSRImportFromWkt().
2228 : *
2229 : * @param ppszInput Pointer to pointer to input. The pointer is updated to
2230 : * point to remaining unused input text.
2231 : *
2232 : * @return OGRERR_NONE if import succeeds, or OGRERR_CORRUPT_DATA if it
2233 : * fails for any reason.
2234 : * @since GDAL 2.3
2235 : */
2236 :
2237 16966 : OGRErr OGRSpatialReference::importFromWkt(const char **ppszInput)
2238 :
2239 : {
2240 16966 : return importFromWkt(ppszInput, nullptr);
2241 : }
2242 :
2243 : /************************************************************************/
2244 : /* importFromWkt() */
2245 : /************************************************************************/
2246 :
2247 : /*! @cond Doxygen_Suppress */
2248 :
2249 21 : OGRErr OGRSpatialReference::importFromWkt(const char *pszInput,
2250 : CSLConstList papszOptions)
2251 :
2252 : {
2253 21 : return importFromWkt(&pszInput, papszOptions);
2254 : }
2255 :
2256 16987 : OGRErr OGRSpatialReference::importFromWkt(const char **ppszInput,
2257 : CSLConstList papszOptions)
2258 :
2259 : {
2260 33974 : TAKE_OPTIONAL_LOCK();
2261 :
2262 16987 : if (!ppszInput || !*ppszInput)
2263 0 : return OGRERR_FAILURE;
2264 :
2265 16987 : if (strlen(*ppszInput) > 100 * 1000 &&
2266 0 : CPLTestBool(CPLGetConfigOption("OSR_IMPORT_FROM_WKT_LIMIT", "YES")))
2267 : {
2268 0 : CPLError(CE_Failure, CPLE_NotSupported,
2269 : "Suspiciously large input for importFromWkt(). Rejecting it. "
2270 : "You can remove this limitation by definition the "
2271 : "OSR_IMPORT_FROM_WKT_LIMIT configuration option to NO.");
2272 0 : return OGRERR_FAILURE;
2273 : }
2274 :
2275 16987 : Clear();
2276 :
2277 16987 : bool canCache = false;
2278 16987 : auto tlsCache = OSRGetProjTLSCache();
2279 33974 : std::string osWkt;
2280 16987 : if (**ppszInput)
2281 : {
2282 16432 : osWkt = *ppszInput;
2283 16432 : auto cachedObj = tlsCache->GetPJForWKT(osWkt);
2284 16432 : if (cachedObj)
2285 : {
2286 14606 : d->setPjCRS(cachedObj);
2287 : }
2288 : else
2289 : {
2290 3652 : CPLStringList aosOptions(papszOptions);
2291 1826 : if (aosOptions.FetchNameValue("STRICT") == nullptr)
2292 1826 : aosOptions.SetNameValue("STRICT", "NO");
2293 1826 : PROJ_STRING_LIST warnings = nullptr;
2294 1826 : PROJ_STRING_LIST errors = nullptr;
2295 1826 : auto ctxt = d->getPROJContext();
2296 1826 : auto pj = proj_create_from_wkt(ctxt, *ppszInput, aosOptions.List(),
2297 : &warnings, &errors);
2298 1826 : d->setPjCRS(pj);
2299 :
2300 1875 : for (auto iter = warnings; iter && *iter; ++iter)
2301 : {
2302 49 : d->m_wktImportWarnings.push_back(*iter);
2303 : }
2304 2062 : for (auto iter = errors; iter && *iter; ++iter)
2305 : {
2306 236 : d->m_wktImportErrors.push_back(*iter);
2307 236 : if (!d->m_pj_crs)
2308 : {
2309 34 : CPLError(CE_Failure, CPLE_AppDefined, "%s", *iter);
2310 : }
2311 : }
2312 1826 : if (warnings == nullptr && errors == nullptr)
2313 : {
2314 1548 : canCache = true;
2315 : }
2316 1826 : proj_string_list_destroy(warnings);
2317 1826 : proj_string_list_destroy(errors);
2318 : }
2319 : }
2320 16987 : if (!d->m_pj_crs)
2321 589 : return OGRERR_CORRUPT_DATA;
2322 :
2323 : // Only accept CRS objects
2324 16398 : if (!proj_is_crs(d->m_pj_crs))
2325 : {
2326 0 : Clear();
2327 0 : return OGRERR_CORRUPT_DATA;
2328 : }
2329 :
2330 16398 : if (canCache)
2331 : {
2332 1548 : tlsCache->CachePJForWKT(osWkt, d->m_pj_crs);
2333 : }
2334 :
2335 16398 : if (strstr(*ppszInput, "CENTER_LONG"))
2336 : {
2337 0 : auto poRoot = new OGR_SRSNode();
2338 0 : d->setRoot(poRoot);
2339 0 : const char *pszTmp = *ppszInput;
2340 0 : poRoot->importFromWkt(&pszTmp);
2341 0 : d->m_bHasCenterLong = true;
2342 : }
2343 :
2344 : // TODO? we don't really update correctly since we assume that the
2345 : // passed string is only WKT.
2346 16398 : *ppszInput += strlen(*ppszInput);
2347 16398 : return OGRERR_NONE;
2348 :
2349 : #if no_longer_implemented_for_now
2350 : /* -------------------------------------------------------------------- */
2351 : /* The following seems to try and detect and unconsumed */
2352 : /* VERTCS[] coordinate system definition (ESRI style) and to */
2353 : /* import and attach it to the existing root. Likely we will */
2354 : /* need to extend this somewhat to bring it into an acceptable */
2355 : /* OGRSpatialReference organization at some point. */
2356 : /* -------------------------------------------------------------------- */
2357 : if (strlen(*ppszInput) > 0 && strstr(*ppszInput, "VERTCS"))
2358 : {
2359 : if (((*ppszInput)[0]) == ',')
2360 : (*ppszInput)++;
2361 : OGR_SRSNode *poNewChild = new OGR_SRSNode();
2362 : poRoot->AddChild(poNewChild);
2363 : return poNewChild->importFromWkt(ppszInput);
2364 : }
2365 : #endif
2366 : }
2367 :
2368 : /*! @endcond */
2369 :
2370 : /**
2371 : * \brief Import from WKT string.
2372 : *
2373 : * This method will wipe the existing SRS definition, and
2374 : * reassign it based on the contents of the passed WKT string. Only as
2375 : * much of the input string as needed to construct this SRS is consumed from
2376 : * the input string, and the input string pointer
2377 : * is then updated to point to the remaining (unused) input.
2378 : *
2379 : * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2380 : * Issues</a> page for implementation details of WKT in OGR.
2381 : *
2382 : * This method is the same as the C function OSRImportFromWkt().
2383 : *
2384 : * @param ppszInput Pointer to pointer to input. The pointer is updated to
2385 : * point to remaining unused input text.
2386 : *
2387 : * @return OGRERR_NONE if import succeeds, or OGRERR_CORRUPT_DATA if it
2388 : * fails for any reason.
2389 : * @deprecated GDAL 2.3. Use importFromWkt(const char**) or importFromWkt(const
2390 : * char*)
2391 : */
2392 :
2393 0 : OGRErr OGRSpatialReference::importFromWkt(char **ppszInput)
2394 :
2395 : {
2396 0 : return importFromWkt(const_cast<const char **>(ppszInput));
2397 : }
2398 :
2399 : /**
2400 : * \brief Import from WKT string.
2401 : *
2402 : * This method will wipe the existing SRS definition, and
2403 : * reassign it based on the contents of the passed WKT string. Only as
2404 : * much of the input string as needed to construct this SRS is consumed from
2405 : * the input string, and the input string pointer
2406 : * is then updated to point to the remaining (unused) input.
2407 : *
2408 : * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2409 : * Issues</a> page for implementation details of WKT in OGR.
2410 : *
2411 : * @param pszInput Input WKT
2412 : *
2413 : * @return OGRERR_NONE if import succeeds, or OGRERR_CORRUPT_DATA if it
2414 : * fails for any reason.
2415 : * @since GDAL 2.3
2416 : */
2417 :
2418 16678 : OGRErr OGRSpatialReference::importFromWkt(const char *pszInput)
2419 : {
2420 16678 : return importFromWkt(&pszInput);
2421 : }
2422 :
2423 : /************************************************************************/
2424 : /* Validate() */
2425 : /************************************************************************/
2426 :
2427 : /**
2428 : * \brief Validate CRS imported with importFromWkt() or with modified with
2429 : * direct node manipulations. Otherwise the CRS should be always valid.
2430 : *
2431 : * This method attempts to verify that the spatial reference system is
2432 : * well formed, and consists of known tokens. The validation is not
2433 : * comprehensive.
2434 : *
2435 : * This method is the same as the C function OSRValidate().
2436 : *
2437 : * @return OGRERR_NONE if all is fine, OGRERR_CORRUPT_DATA if the SRS is
2438 : * not well formed, and OGRERR_UNSUPPORTED_SRS if the SRS is well formed,
2439 : * but contains non-standard PROJECTION[] values.
2440 : */
2441 :
2442 116 : OGRErr OGRSpatialReference::Validate() const
2443 :
2444 : {
2445 232 : TAKE_OPTIONAL_LOCK();
2446 :
2447 154 : for (const auto &str : d->m_wktImportErrors)
2448 : {
2449 38 : CPLDebug("OGRSpatialReference::Validate", "%s", str.c_str());
2450 : }
2451 116 : for (const auto &str : d->m_wktImportWarnings)
2452 : {
2453 0 : CPLDebug("OGRSpatialReference::Validate", "%s", str.c_str());
2454 : }
2455 116 : if (!d->m_pj_crs || !d->m_wktImportErrors.empty())
2456 : {
2457 37 : return OGRERR_CORRUPT_DATA;
2458 : }
2459 79 : if (!d->m_wktImportWarnings.empty())
2460 : {
2461 0 : return OGRERR_UNSUPPORTED_SRS;
2462 : }
2463 79 : return OGRERR_NONE;
2464 : }
2465 :
2466 : /************************************************************************/
2467 : /* OSRValidate() */
2468 : /************************************************************************/
2469 : /**
2470 : * \brief Validate SRS tokens.
2471 : *
2472 : * This function is the same as the C++ method OGRSpatialReference::Validate().
2473 : */
2474 114 : OGRErr OSRValidate(OGRSpatialReferenceH hSRS)
2475 :
2476 : {
2477 114 : VALIDATE_POINTER1(hSRS, "OSRValidate", OGRERR_FAILURE);
2478 :
2479 114 : return OGRSpatialReference::FromHandle(hSRS)->Validate();
2480 : }
2481 :
2482 : /************************************************************************/
2483 : /* OSRImportFromWkt() */
2484 : /************************************************************************/
2485 :
2486 : /**
2487 : * \brief Import from WKT string.
2488 : *
2489 : * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2490 : * Issues</a> page for implementation details of WKT in OGR.
2491 : *
2492 : * This function is the same as OGRSpatialReference::importFromWkt().
2493 : */
2494 :
2495 288 : OGRErr OSRImportFromWkt(OGRSpatialReferenceH hSRS, char **ppszInput)
2496 :
2497 : {
2498 288 : VALIDATE_POINTER1(hSRS, "OSRImportFromWkt", OGRERR_FAILURE);
2499 :
2500 288 : return ToPointer(hSRS)->importFromWkt(const_cast<const char **>(ppszInput));
2501 : }
2502 :
2503 : /************************************************************************/
2504 : /* SetNode() */
2505 : /************************************************************************/
2506 :
2507 : /**
2508 : * \brief Set attribute value in spatial reference.
2509 : *
2510 : * Missing intermediate nodes in the path will be created if not already
2511 : * in existence. If the attribute has no children one will be created and
2512 : * assigned the value otherwise the zeroth child will be assigned the value.
2513 : *
2514 : * This method does the same as the C function OSRSetAttrValue().
2515 : *
2516 : * @param pszNodePath full path to attribute to be set. For instance
2517 : * "PROJCS|GEOGCS|UNIT".
2518 : *
2519 : * @param pszNewNodeValue value to be assigned to node, such as "meter".
2520 : * This may be NULL if you just want to force creation of the intermediate
2521 : * path.
2522 : *
2523 : * @return OGRERR_NONE on success.
2524 : */
2525 :
2526 589 : OGRErr OGRSpatialReference::SetNode(const char *pszNodePath,
2527 : const char *pszNewNodeValue)
2528 :
2529 : {
2530 1178 : TAKE_OPTIONAL_LOCK();
2531 :
2532 : char **papszPathTokens =
2533 589 : CSLTokenizeStringComplex(pszNodePath, "|", TRUE, FALSE);
2534 :
2535 589 : if (CSLCount(papszPathTokens) < 1)
2536 : {
2537 0 : CSLDestroy(papszPathTokens);
2538 0 : return OGRERR_FAILURE;
2539 : }
2540 :
2541 1028 : if (GetRoot() == nullptr ||
2542 439 : !EQUAL(papszPathTokens[0], GetRoot()->GetValue()))
2543 : {
2544 272 : if (EQUAL(papszPathTokens[0], "PROJCS") &&
2545 118 : CSLCount(papszPathTokens) == 1)
2546 : {
2547 118 : CSLDestroy(papszPathTokens);
2548 118 : return SetProjCS(pszNewNodeValue);
2549 : }
2550 : else
2551 : {
2552 36 : SetRoot(new OGR_SRSNode(papszPathTokens[0]));
2553 : }
2554 : }
2555 :
2556 471 : OGR_SRSNode *poNode = GetRoot();
2557 729 : for (int i = 1; papszPathTokens[i] != nullptr; i++)
2558 : {
2559 258 : int j = 0; // Used after for.
2560 :
2561 645 : for (; j < poNode->GetChildCount(); j++)
2562 : {
2563 585 : if (EQUAL(poNode->GetChild(j)->GetValue(), papszPathTokens[i]))
2564 : {
2565 198 : poNode = poNode->GetChild(j);
2566 198 : j = -1;
2567 198 : break;
2568 : }
2569 : }
2570 :
2571 258 : if (j != -1)
2572 : {
2573 60 : OGR_SRSNode *poNewNode = new OGR_SRSNode(papszPathTokens[i]);
2574 60 : poNode->AddChild(poNewNode);
2575 60 : poNode = poNewNode;
2576 : }
2577 : }
2578 :
2579 471 : CSLDestroy(papszPathTokens);
2580 :
2581 471 : if (pszNewNodeValue != nullptr)
2582 : {
2583 471 : if (poNode->GetChildCount() > 0)
2584 375 : poNode->GetChild(0)->SetValue(pszNewNodeValue);
2585 : else
2586 96 : poNode->AddChild(new OGR_SRSNode(pszNewNodeValue));
2587 : };
2588 471 : return OGRERR_NONE;
2589 : }
2590 :
2591 : /************************************************************************/
2592 : /* OSRSetAttrValue() */
2593 : /************************************************************************/
2594 :
2595 : /**
2596 : * \brief Set attribute value in spatial reference.
2597 : *
2598 : * This function is the same as OGRSpatialReference::SetNode()
2599 : */
2600 1 : OGRErr CPL_STDCALL OSRSetAttrValue(OGRSpatialReferenceH hSRS,
2601 : const char *pszPath, const char *pszValue)
2602 :
2603 : {
2604 1 : VALIDATE_POINTER1(hSRS, "OSRSetAttrValue", OGRERR_FAILURE);
2605 :
2606 1 : return ToPointer(hSRS)->SetNode(pszPath, pszValue);
2607 : }
2608 :
2609 : /************************************************************************/
2610 : /* SetNode() */
2611 : /************************************************************************/
2612 :
2613 : /**
2614 : * \brief Set attribute value in spatial reference.
2615 : *
2616 : * Missing intermediate nodes in the path will be created if not already
2617 : * in existence. If the attribute has no children one will be created and
2618 : * assigned the value otherwise the zeroth child will be assigned the value.
2619 : *
2620 : * This method does the same as the C function OSRSetAttrValue().
2621 : *
2622 : * @param pszNodePath full path to attribute to be set. For instance
2623 : * "PROJCS|GEOGCS|UNIT".
2624 : *
2625 : * @param dfValue value to be assigned to node.
2626 : *
2627 : * @return OGRERR_NONE on success.
2628 : */
2629 :
2630 0 : OGRErr OGRSpatialReference::SetNode(const char *pszNodePath, double dfValue)
2631 :
2632 : {
2633 0 : char szValue[64] = {'\0'};
2634 :
2635 0 : if (std::abs(dfValue - static_cast<int>(dfValue)) == 0.0)
2636 0 : snprintf(szValue, sizeof(szValue), "%d", static_cast<int>(dfValue));
2637 : else
2638 0 : OGRsnPrintDouble(szValue, sizeof(szValue), dfValue);
2639 :
2640 0 : return SetNode(pszNodePath, szValue);
2641 : }
2642 :
2643 : /************************************************************************/
2644 : /* SetAngularUnits() */
2645 : /************************************************************************/
2646 :
2647 : /**
2648 : * \brief Set the angular units for the geographic coordinate system.
2649 : *
2650 : * This method creates a UNIT subnode with the specified values as a
2651 : * child of the GEOGCS node.
2652 : *
2653 : * This method does the same as the C function OSRSetAngularUnits().
2654 : *
2655 : * @param pszUnitsName the units name to be used. Some preferred units
2656 : * names can be found in ogr_srs_api.h such as SRS_UA_DEGREE.
2657 : *
2658 : * @param dfInRadians the value to multiple by an angle in the indicated
2659 : * units to transform to radians. Some standard conversion factors can
2660 : * be found in ogr_srs_api.h.
2661 : *
2662 : * @return OGRERR_NONE on success.
2663 : */
2664 :
2665 1202 : OGRErr OGRSpatialReference::SetAngularUnits(const char *pszUnitsName,
2666 : double dfInRadians)
2667 :
2668 : {
2669 2404 : TAKE_OPTIONAL_LOCK();
2670 :
2671 1202 : d->bNormInfoSet = FALSE;
2672 :
2673 1202 : d->refreshProjObj();
2674 1202 : if (!d->m_pj_crs)
2675 0 : return OGRERR_FAILURE;
2676 1202 : auto geodCRS = proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
2677 1202 : if (!geodCRS)
2678 0 : return OGRERR_FAILURE;
2679 1202 : proj_destroy(geodCRS);
2680 1202 : d->demoteFromBoundCRS();
2681 1202 : d->setPjCRS(proj_crs_alter_cs_angular_unit(d->getPROJContext(), d->m_pj_crs,
2682 : pszUnitsName, dfInRadians,
2683 : nullptr, nullptr));
2684 1202 : d->undoDemoteFromBoundCRS();
2685 :
2686 1202 : d->m_osAngularUnits = pszUnitsName;
2687 1202 : d->m_dfAngularUnitToRadian = dfInRadians;
2688 :
2689 1202 : return OGRERR_NONE;
2690 : }
2691 :
2692 : /************************************************************************/
2693 : /* OSRSetAngularUnits() */
2694 : /************************************************************************/
2695 :
2696 : /**
2697 : * \brief Set the angular units for the geographic coordinate system.
2698 : *
2699 : * This function is the same as OGRSpatialReference::SetAngularUnits()
2700 : */
2701 41 : OGRErr OSRSetAngularUnits(OGRSpatialReferenceH hSRS, const char *pszUnits,
2702 : double dfInRadians)
2703 :
2704 : {
2705 41 : VALIDATE_POINTER1(hSRS, "OSRSetAngularUnits", OGRERR_FAILURE);
2706 :
2707 41 : return ToPointer(hSRS)->SetAngularUnits(pszUnits, dfInRadians);
2708 : }
2709 :
2710 : /************************************************************************/
2711 : /* GetAngularUnits() */
2712 : /************************************************************************/
2713 :
2714 : /**
2715 : * \brief Fetch angular geographic coordinate system units.
2716 : *
2717 : * If no units are available, a value of "degree" and SRS_UA_DEGREE_CONV
2718 : * will be assumed. This method only checks directly under the GEOGCS node
2719 : * for units.
2720 : *
2721 : * This method does the same thing as the C function OSRGetAngularUnits().
2722 : *
2723 : * @param ppszName a pointer to be updated with the pointer to the units name.
2724 : * The returned value remains internal to the OGRSpatialReference and should
2725 : * not be freed, or modified. It may be invalidated on the next
2726 : * OGRSpatialReference call.
2727 : *
2728 : * @return the value to multiply by angular distances to transform them to
2729 : * radians.
2730 : * @since GDAL 2.3.0
2731 : */
2732 :
2733 7280 : double OGRSpatialReference::GetAngularUnits(const char **ppszName) const
2734 :
2735 : {
2736 14560 : TAKE_OPTIONAL_LOCK();
2737 :
2738 7280 : d->refreshProjObj();
2739 :
2740 7280 : if (!d->m_osAngularUnits.empty())
2741 : {
2742 2100 : if (ppszName != nullptr)
2743 146 : *ppszName = d->m_osAngularUnits.c_str();
2744 2100 : return d->m_dfAngularUnitToRadian;
2745 : }
2746 :
2747 : do
2748 : {
2749 5180 : if (d->m_pj_crs == nullptr || d->m_pjType == PJ_TYPE_ENGINEERING_CRS)
2750 : {
2751 113 : break;
2752 : }
2753 :
2754 : auto geodCRS =
2755 5069 : proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
2756 5069 : if (!geodCRS)
2757 : {
2758 0 : break;
2759 : }
2760 : auto coordSys =
2761 5069 : proj_crs_get_coordinate_system(d->getPROJContext(), geodCRS);
2762 5069 : proj_destroy(geodCRS);
2763 5069 : if (!coordSys)
2764 : {
2765 0 : break;
2766 : }
2767 5069 : if (proj_cs_get_type(d->getPROJContext(), coordSys) !=
2768 : PJ_CS_TYPE_ELLIPSOIDAL)
2769 : {
2770 2 : proj_destroy(coordSys);
2771 2 : break;
2772 : }
2773 :
2774 5067 : double dfConvFactor = 0.0;
2775 5067 : const char *pszUnitName = nullptr;
2776 5067 : if (!proj_cs_get_axis_info(d->getPROJContext(), coordSys, 0, nullptr,
2777 : nullptr, nullptr, &dfConvFactor,
2778 : &pszUnitName, nullptr, nullptr))
2779 : {
2780 0 : proj_destroy(coordSys);
2781 0 : break;
2782 : }
2783 :
2784 5067 : d->m_osAngularUnits = pszUnitName;
2785 :
2786 5067 : proj_destroy(coordSys);
2787 5067 : d->m_dfAngularUnitToRadian = dfConvFactor;
2788 : } while (false);
2789 :
2790 5180 : if (d->m_osAngularUnits.empty())
2791 : {
2792 113 : d->m_osAngularUnits = "degree";
2793 113 : d->m_dfAngularUnitToRadian = CPLAtof(SRS_UA_DEGREE_CONV);
2794 : }
2795 :
2796 5180 : if (ppszName != nullptr)
2797 3022 : *ppszName = d->m_osAngularUnits.c_str();
2798 5180 : return d->m_dfAngularUnitToRadian;
2799 : }
2800 :
2801 : /**
2802 : * \brief Fetch angular geographic coordinate system units.
2803 : *
2804 : * If no units are available, a value of "degree" and SRS_UA_DEGREE_CONV
2805 : * will be assumed. This method only checks directly under the GEOGCS node
2806 : * for units.
2807 : *
2808 : * This method does the same thing as the C function OSRGetAngularUnits().
2809 : *
2810 : * @param ppszName a pointer to be updated with the pointer to the units name.
2811 : * The returned value remains internal to the OGRSpatialReference and should
2812 : * not be freed, or modified. It may be invalidated on the next
2813 : * OGRSpatialReference call.
2814 : *
2815 : * @return the value to multiply by angular distances to transform them to
2816 : * radians.
2817 : * @deprecated GDAL 2.3.0. Use GetAngularUnits(const char**) const.
2818 : */
2819 :
2820 0 : double OGRSpatialReference::GetAngularUnits(char **ppszName) const
2821 :
2822 : {
2823 0 : return GetAngularUnits(const_cast<const char **>(ppszName));
2824 : }
2825 :
2826 : /************************************************************************/
2827 : /* OSRGetAngularUnits() */
2828 : /************************************************************************/
2829 :
2830 : /**
2831 : * \brief Fetch angular geographic coordinate system units.
2832 : *
2833 : * This function is the same as OGRSpatialReference::GetAngularUnits()
2834 : */
2835 1 : double OSRGetAngularUnits(OGRSpatialReferenceH hSRS, char **ppszName)
2836 :
2837 : {
2838 1 : VALIDATE_POINTER1(hSRS, "OSRGetAngularUnits", 0);
2839 :
2840 1 : return ToPointer(hSRS)->GetAngularUnits(
2841 1 : const_cast<const char **>(ppszName));
2842 : }
2843 :
2844 : /************************************************************************/
2845 : /* SetLinearUnitsAndUpdateParameters() */
2846 : /************************************************************************/
2847 :
2848 : /**
2849 : * \brief Set the linear units for the projection.
2850 : *
2851 : * This method creates a UNIT subnode with the specified values as a
2852 : * child of the PROJCS or LOCAL_CS node. It works the same as the
2853 : * SetLinearUnits() method, but it also updates all existing linear
2854 : * projection parameter values from the old units to the new units.
2855 : *
2856 : * @param pszName the units name to be used. Some preferred units
2857 : * names can be found in ogr_srs_api.h such as SRS_UL_METER, SRS_UL_FOOT
2858 : * and SRS_UL_US_FOOT.
2859 : *
2860 : * @param dfInMeters the value to multiple by a length in the indicated
2861 : * units to transform to meters. Some standard conversion factors can
2862 : * be found in ogr_srs_api.h.
2863 : *
2864 : * @param pszUnitAuthority Unit authority name. Or nullptr
2865 : *
2866 : * @param pszUnitCode Unit code. Or nullptr
2867 : *
2868 : * @return OGRERR_NONE on success.
2869 : */
2870 :
2871 39 : OGRErr OGRSpatialReference::SetLinearUnitsAndUpdateParameters(
2872 : const char *pszName, double dfInMeters, const char *pszUnitAuthority,
2873 : const char *pszUnitCode)
2874 :
2875 : {
2876 78 : TAKE_OPTIONAL_LOCK();
2877 :
2878 39 : if (dfInMeters <= 0.0)
2879 0 : return OGRERR_FAILURE;
2880 :
2881 39 : d->refreshProjObj();
2882 39 : if (!d->m_pj_crs)
2883 0 : return OGRERR_FAILURE;
2884 :
2885 39 : d->demoteFromBoundCRS();
2886 39 : if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
2887 : {
2888 78 : d->setPjCRS(proj_crs_alter_parameters_linear_unit(
2889 39 : d->getPROJContext(), d->m_pj_crs, pszName, dfInMeters,
2890 : pszUnitAuthority, pszUnitCode, true));
2891 : }
2892 39 : d->setPjCRS(proj_crs_alter_cs_linear_unit(d->getPROJContext(), d->m_pj_crs,
2893 : pszName, dfInMeters,
2894 : pszUnitAuthority, pszUnitCode));
2895 39 : d->undoDemoteFromBoundCRS();
2896 :
2897 39 : d->m_osLinearUnits = pszName;
2898 39 : d->dfToMeter = dfInMeters;
2899 :
2900 39 : return OGRERR_NONE;
2901 : }
2902 :
2903 : /************************************************************************/
2904 : /* OSRSetLinearUnitsAndUpdateParameters() */
2905 : /************************************************************************/
2906 :
2907 : /**
2908 : * \brief Set the linear units for the projection.
2909 : *
2910 : * This function is the same as
2911 : * OGRSpatialReference::SetLinearUnitsAndUpdateParameters()
2912 : */
2913 1 : OGRErr OSRSetLinearUnitsAndUpdateParameters(OGRSpatialReferenceH hSRS,
2914 : const char *pszUnits,
2915 : double dfInMeters)
2916 :
2917 : {
2918 1 : VALIDATE_POINTER1(hSRS, "OSRSetLinearUnitsAndUpdateParameters",
2919 : OGRERR_FAILURE);
2920 :
2921 1 : return ToPointer(hSRS)->SetLinearUnitsAndUpdateParameters(pszUnits,
2922 1 : dfInMeters);
2923 : }
2924 :
2925 : /************************************************************************/
2926 : /* SetLinearUnits() */
2927 : /************************************************************************/
2928 :
2929 : /**
2930 : * \brief Set the linear units for the projection.
2931 : *
2932 : * This method creates a UNIT subnode with the specified values as a
2933 : * child of the PROJCS, GEOCCS, GEOGCS or LOCAL_CS node. When called on a
2934 : * Geographic 3D CRS the vertical axis units will be set.
2935 : *
2936 : * This method does the same as the C function OSRSetLinearUnits().
2937 : *
2938 : * @param pszUnitsName the units name to be used. Some preferred units
2939 : * names can be found in ogr_srs_api.h such as SRS_UL_METER, SRS_UL_FOOT
2940 : * and SRS_UL_US_FOOT.
2941 : *
2942 : * @param dfInMeters the value to multiple by a length in the indicated
2943 : * units to transform to meters. Some standard conversion factors can
2944 : * be found in ogr_srs_api.h.
2945 : *
2946 : * @return OGRERR_NONE on success.
2947 : */
2948 :
2949 6956 : OGRErr OGRSpatialReference::SetLinearUnits(const char *pszUnitsName,
2950 : double dfInMeters)
2951 :
2952 : {
2953 6956 : return SetTargetLinearUnits(nullptr, pszUnitsName, dfInMeters);
2954 : }
2955 :
2956 : /************************************************************************/
2957 : /* OSRSetLinearUnits() */
2958 : /************************************************************************/
2959 :
2960 : /**
2961 : * \brief Set the linear units for the projection.
2962 : *
2963 : * This function is the same as OGRSpatialReference::SetLinearUnits()
2964 : */
2965 7 : OGRErr OSRSetLinearUnits(OGRSpatialReferenceH hSRS, const char *pszUnits,
2966 : double dfInMeters)
2967 :
2968 : {
2969 7 : VALIDATE_POINTER1(hSRS, "OSRSetLinearUnits", OGRERR_FAILURE);
2970 :
2971 7 : return ToPointer(hSRS)->SetLinearUnits(pszUnits, dfInMeters);
2972 : }
2973 :
2974 : /************************************************************************/
2975 : /* SetTargetLinearUnits() */
2976 : /************************************************************************/
2977 :
2978 : /**
2979 : * \brief Set the linear units for the projection.
2980 : *
2981 : * This method creates a UNIT subnode with the specified values as a
2982 : * child of the target node.
2983 : *
2984 : * This method does the same as the C function OSRSetTargetLinearUnits().
2985 : *
2986 : * @param pszTargetKey the keyword to set the linear units for.
2987 : * i.e. "PROJCS" or "VERT_CS"
2988 : *
2989 : * @param pszUnitsName the units name to be used. Some preferred units
2990 : * names can be found in ogr_srs_api.h such as SRS_UL_METER, SRS_UL_FOOT
2991 : * and SRS_UL_US_FOOT.
2992 : *
2993 : * @param dfInMeters the value to multiple by a length in the indicated
2994 : * units to transform to meters. Some standard conversion factors can
2995 : * be found in ogr_srs_api.h.
2996 : *
2997 : * @param pszUnitAuthority Unit authority name. Or nullptr
2998 : *
2999 : * @param pszUnitCode Unit code. Or nullptr
3000 : *
3001 : * @return OGRERR_NONE on success.
3002 : *
3003 : * @since OGR 1.9.0
3004 : */
3005 :
3006 10993 : OGRErr OGRSpatialReference::SetTargetLinearUnits(const char *pszTargetKey,
3007 : const char *pszUnitsName,
3008 : double dfInMeters,
3009 : const char *pszUnitAuthority,
3010 : const char *pszUnitCode)
3011 :
3012 : {
3013 21986 : TAKE_OPTIONAL_LOCK();
3014 :
3015 10993 : if (dfInMeters <= 0.0)
3016 0 : return OGRERR_FAILURE;
3017 :
3018 10993 : d->refreshProjObj();
3019 10993 : pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
3020 10993 : if (pszTargetKey == nullptr)
3021 : {
3022 10993 : if (!d->m_pj_crs)
3023 0 : return OGRERR_FAILURE;
3024 :
3025 10993 : d->demoteFromBoundCRS();
3026 10993 : if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
3027 : {
3028 16230 : d->setPjCRS(proj_crs_alter_parameters_linear_unit(
3029 8115 : d->getPROJContext(), d->m_pj_crs, pszUnitsName, dfInMeters,
3030 : pszUnitAuthority, pszUnitCode, false));
3031 : }
3032 21986 : d->setPjCRS(proj_crs_alter_cs_linear_unit(
3033 10993 : d->getPROJContext(), d->m_pj_crs, pszUnitsName, dfInMeters,
3034 : pszUnitAuthority, pszUnitCode));
3035 10993 : d->undoDemoteFromBoundCRS();
3036 :
3037 10993 : d->m_osLinearUnits = pszUnitsName;
3038 10993 : d->dfToMeter = dfInMeters;
3039 :
3040 10993 : return OGRERR_NONE;
3041 : }
3042 :
3043 0 : OGR_SRSNode *poCS = GetAttrNode(pszTargetKey);
3044 :
3045 0 : if (poCS == nullptr)
3046 0 : return OGRERR_FAILURE;
3047 :
3048 0 : char szValue[128] = {'\0'};
3049 0 : if (dfInMeters < std::numeric_limits<int>::max() &&
3050 0 : dfInMeters > std::numeric_limits<int>::min() &&
3051 0 : dfInMeters == static_cast<int>(dfInMeters))
3052 0 : snprintf(szValue, sizeof(szValue), "%d", static_cast<int>(dfInMeters));
3053 : else
3054 0 : OGRsnPrintDouble(szValue, sizeof(szValue), dfInMeters);
3055 :
3056 0 : OGR_SRSNode *poUnits = nullptr;
3057 0 : if (poCS->FindChild("UNIT") >= 0)
3058 : {
3059 0 : poUnits = poCS->GetChild(poCS->FindChild("UNIT"));
3060 0 : if (poUnits->GetChildCount() < 2)
3061 0 : return OGRERR_FAILURE;
3062 0 : poUnits->GetChild(0)->SetValue(pszUnitsName);
3063 0 : poUnits->GetChild(1)->SetValue(szValue);
3064 0 : if (poUnits->FindChild("AUTHORITY") != -1)
3065 0 : poUnits->DestroyChild(poUnits->FindChild("AUTHORITY"));
3066 : }
3067 : else
3068 : {
3069 0 : poUnits = new OGR_SRSNode("UNIT");
3070 0 : poUnits->AddChild(new OGR_SRSNode(pszUnitsName));
3071 0 : poUnits->AddChild(new OGR_SRSNode(szValue));
3072 :
3073 0 : poCS->AddChild(poUnits);
3074 : }
3075 :
3076 0 : return OGRERR_NONE;
3077 : }
3078 :
3079 : /************************************************************************/
3080 : /* OSRSetLinearUnits() */
3081 : /************************************************************************/
3082 :
3083 : /**
3084 : * \brief Set the linear units for the target node.
3085 : *
3086 : * This function is the same as OGRSpatialReference::SetTargetLinearUnits()
3087 : *
3088 : * @since OGR 1.9.0
3089 : */
3090 1 : OGRErr OSRSetTargetLinearUnits(OGRSpatialReferenceH hSRS,
3091 : const char *pszTargetKey, const char *pszUnits,
3092 : double dfInMeters)
3093 :
3094 : {
3095 1 : VALIDATE_POINTER1(hSRS, "OSRSetTargetLinearUnits", OGRERR_FAILURE);
3096 :
3097 1 : return ToPointer(hSRS)->SetTargetLinearUnits(pszTargetKey, pszUnits,
3098 1 : dfInMeters);
3099 : }
3100 :
3101 : /************************************************************************/
3102 : /* GetLinearUnits() */
3103 : /************************************************************************/
3104 :
3105 : /**
3106 : * \brief Fetch linear projection units.
3107 : *
3108 : * If no units are available, a value of "Meters" and 1.0 will be assumed.
3109 : * This method only checks directly under the PROJCS, GEOCCS, GEOGCS or
3110 : * LOCAL_CS node for units. When called on a Geographic 3D CRS the vertical
3111 : * axis units will be returned.
3112 : *
3113 : * This method does the same thing as the C function OSRGetLinearUnits()
3114 : *
3115 : * @param ppszName a pointer to be updated with the pointer to the units name.
3116 : * The returned value remains internal to the OGRSpatialReference and should
3117 : * not be freed, or modified. It may be invalidated on the next
3118 : * OGRSpatialReference call.
3119 : *
3120 : * @return the value to multiply by linear distances to transform them to
3121 : * meters.
3122 : * @deprecated GDAL 2.3.0. Use GetLinearUnits(const char**) const.
3123 : */
3124 :
3125 0 : double OGRSpatialReference::GetLinearUnits(char **ppszName) const
3126 :
3127 : {
3128 0 : return GetTargetLinearUnits(nullptr, const_cast<const char **>(ppszName));
3129 : }
3130 :
3131 : /**
3132 : * \brief Fetch linear projection units.
3133 : *
3134 : * If no units are available, a value of "Meters" and 1.0 will be assumed.
3135 : * This method only checks directly under the PROJCS, GEOCCS or LOCAL_CS node
3136 : * for units.
3137 : *
3138 : * This method does the same thing as the C function OSRGetLinearUnits()
3139 : *
3140 : * @param ppszName a pointer to be updated with the pointer to the units name.
3141 : * The returned value remains internal to the OGRSpatialReference and should
3142 : * not be freed, or modified. It may be invalidated on the next
3143 : * OGRSpatialReference call.
3144 : *
3145 : * @return the value to multiply by linear distances to transform them to
3146 : * meters.
3147 : * @since GDAL 2.3.0
3148 : */
3149 :
3150 18698 : double OGRSpatialReference::GetLinearUnits(const char **ppszName) const
3151 :
3152 : {
3153 18698 : return GetTargetLinearUnits(nullptr, ppszName);
3154 : }
3155 :
3156 : /************************************************************************/
3157 : /* OSRGetLinearUnits() */
3158 : /************************************************************************/
3159 :
3160 : /**
3161 : * \brief Fetch linear projection units.
3162 : *
3163 : * This function is the same as OGRSpatialReference::GetLinearUnits()
3164 : */
3165 227 : double OSRGetLinearUnits(OGRSpatialReferenceH hSRS, char **ppszName)
3166 :
3167 : {
3168 227 : VALIDATE_POINTER1(hSRS, "OSRGetLinearUnits", 0);
3169 :
3170 227 : return ToPointer(hSRS)->GetLinearUnits(const_cast<const char **>(ppszName));
3171 : }
3172 :
3173 : /************************************************************************/
3174 : /* GetTargetLinearUnits() */
3175 : /************************************************************************/
3176 :
3177 : /**
3178 : * \brief Fetch linear units for target.
3179 : *
3180 : * If no units are available, a value of "Meters" and 1.0 will be assumed.
3181 : *
3182 : * This method does the same thing as the C function OSRGetTargetLinearUnits()
3183 : *
3184 : * @param pszTargetKey the key to look on. i.e. "PROJCS" or "VERT_CS". Might be
3185 : * NULL, in which case PROJCS will be implied (and if not found, LOCAL_CS,
3186 : * GEOCCS, GEOGCS and VERT_CS are looked up)
3187 : * @param ppszName a pointer to be updated with the pointer to the units name.
3188 : * The returned value remains internal to the OGRSpatialReference and should not
3189 : * be freed, or modified. It may be invalidated on the next
3190 : * OGRSpatialReference call. ppszName can be set to NULL.
3191 : *
3192 : * @return the value to multiply by linear distances to transform them to
3193 : * meters.
3194 : *
3195 : * @since OGR 1.9.0
3196 : * @deprecated GDAL 2.3.0. Use GetTargetLinearUnits(const char*, const char**)
3197 : * const.
3198 : */
3199 :
3200 18849 : double OGRSpatialReference::GetTargetLinearUnits(const char *pszTargetKey,
3201 : const char **ppszName) const
3202 :
3203 : {
3204 37698 : TAKE_OPTIONAL_LOCK();
3205 :
3206 18849 : d->refreshProjObj();
3207 :
3208 18849 : pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
3209 18849 : if (pszTargetKey == nullptr)
3210 : {
3211 : // Use cached result if available
3212 18758 : if (!d->m_osLinearUnits.empty())
3213 : {
3214 8124 : if (ppszName)
3215 7386 : *ppszName = d->m_osLinearUnits.c_str();
3216 8124 : return d->dfToMeter;
3217 : }
3218 :
3219 : while (true)
3220 : {
3221 10634 : if (d->m_pj_crs == nullptr)
3222 : {
3223 244 : break;
3224 : }
3225 :
3226 10390 : d->demoteFromBoundCRS();
3227 10390 : PJ *coordSys = nullptr;
3228 10390 : if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
3229 : {
3230 30 : for (int iComponent = 0; iComponent < 2; iComponent++)
3231 : {
3232 30 : auto subCRS = proj_crs_get_sub_crs(d->getPROJContext(),
3233 30 : d->m_pj_crs, iComponent);
3234 30 : if (subCRS && proj_get_type(subCRS) == PJ_TYPE_BOUND_CRS)
3235 : {
3236 : auto temp =
3237 0 : proj_get_source_crs(d->getPROJContext(), subCRS);
3238 0 : proj_destroy(subCRS);
3239 0 : subCRS = temp;
3240 : }
3241 60 : if (subCRS &&
3242 30 : (proj_get_type(subCRS) == PJ_TYPE_PROJECTED_CRS ||
3243 16 : proj_get_type(subCRS) == PJ_TYPE_ENGINEERING_CRS ||
3244 12 : proj_get_type(subCRS) == PJ_TYPE_VERTICAL_CRS))
3245 : {
3246 24 : coordSys = proj_crs_get_coordinate_system(
3247 : d->getPROJContext(), subCRS);
3248 24 : proj_destroy(subCRS);
3249 24 : break;
3250 : }
3251 6 : else if (subCRS)
3252 : {
3253 6 : proj_destroy(subCRS);
3254 : }
3255 : }
3256 24 : if (coordSys == nullptr)
3257 : {
3258 0 : d->undoDemoteFromBoundCRS();
3259 0 : break;
3260 : }
3261 : }
3262 : else
3263 : {
3264 10366 : coordSys = proj_crs_get_coordinate_system(d->getPROJContext(),
3265 10366 : d->m_pj_crs);
3266 : }
3267 :
3268 10390 : d->undoDemoteFromBoundCRS();
3269 10390 : if (!coordSys)
3270 : {
3271 0 : break;
3272 : }
3273 10390 : auto csType = proj_cs_get_type(d->getPROJContext(), coordSys);
3274 :
3275 10390 : if (csType != PJ_CS_TYPE_CARTESIAN &&
3276 2162 : csType != PJ_CS_TYPE_VERTICAL &&
3277 0 : csType != PJ_CS_TYPE_ELLIPSOIDAL &&
3278 : csType != PJ_CS_TYPE_SPHERICAL)
3279 : {
3280 0 : proj_destroy(coordSys);
3281 0 : break;
3282 : }
3283 :
3284 10390 : int axis = 0;
3285 :
3286 10390 : if (csType == PJ_CS_TYPE_ELLIPSOIDAL ||
3287 : csType == PJ_CS_TYPE_SPHERICAL)
3288 : {
3289 : const int axisCount =
3290 2162 : proj_cs_get_axis_count(d->getPROJContext(), coordSys);
3291 :
3292 2162 : if (axisCount == 3)
3293 : {
3294 4 : axis = 2;
3295 : }
3296 : else
3297 : {
3298 2158 : proj_destroy(coordSys);
3299 2158 : break;
3300 : }
3301 : }
3302 :
3303 8232 : double dfConvFactor = 0.0;
3304 8232 : const char *pszUnitName = nullptr;
3305 8232 : if (!proj_cs_get_axis_info(d->getPROJContext(), coordSys, axis,
3306 : nullptr, nullptr, nullptr, &dfConvFactor,
3307 : &pszUnitName, nullptr, nullptr))
3308 : {
3309 0 : proj_destroy(coordSys);
3310 0 : break;
3311 : }
3312 :
3313 8232 : d->m_osLinearUnits = pszUnitName;
3314 8232 : d->dfToMeter = dfConvFactor;
3315 8232 : if (ppszName)
3316 1132 : *ppszName = d->m_osLinearUnits.c_str();
3317 :
3318 8232 : proj_destroy(coordSys);
3319 8232 : return dfConvFactor;
3320 : }
3321 :
3322 2402 : d->m_osLinearUnits = "unknown";
3323 2402 : d->dfToMeter = 1.0;
3324 :
3325 2402 : if (ppszName != nullptr)
3326 2220 : *ppszName = d->m_osLinearUnits.c_str();
3327 2402 : return 1.0;
3328 : }
3329 :
3330 91 : const OGR_SRSNode *poCS = GetAttrNode(pszTargetKey);
3331 :
3332 91 : if (ppszName != nullptr)
3333 37 : *ppszName = "unknown";
3334 :
3335 91 : if (poCS == nullptr)
3336 53 : return 1.0;
3337 :
3338 114 : for (int iChild = 0; iChild < poCS->GetChildCount(); iChild++)
3339 : {
3340 114 : const OGR_SRSNode *poChild = poCS->GetChild(iChild);
3341 :
3342 114 : if (EQUAL(poChild->GetValue(), "UNIT") && poChild->GetChildCount() >= 2)
3343 : {
3344 38 : if (ppszName != nullptr)
3345 37 : *ppszName = poChild->GetChild(0)->GetValue();
3346 :
3347 38 : return CPLAtof(poChild->GetChild(1)->GetValue());
3348 : }
3349 : }
3350 :
3351 0 : return 1.0;
3352 : }
3353 :
3354 : /**
3355 : * \brief Fetch linear units for target.
3356 : *
3357 : * If no units are available, a value of "Meters" and 1.0 will be assumed.
3358 : *
3359 : * This method does the same thing as the C function OSRGetTargetLinearUnits()
3360 : *
3361 : * @param pszTargetKey the key to look on. i.e. "PROJCS" or "VERT_CS". Might be
3362 : * NULL, in which case PROJCS will be implied (and if not found, LOCAL_CS,
3363 : * GEOCCS and VERT_CS are looked up)
3364 : * @param ppszName a pointer to be updated with the pointer to the units name.
3365 : * The returned value remains internal to the OGRSpatialReference and should not
3366 : * be freed, or modified. It may be invalidated on the next
3367 : * OGRSpatialReference call. ppszName can be set to NULL.
3368 : *
3369 : * @return the value to multiply by linear distances to transform them to
3370 : * meters.
3371 : *
3372 : * @since GDAL 2.3.0
3373 : */
3374 :
3375 0 : double OGRSpatialReference::GetTargetLinearUnits(const char *pszTargetKey,
3376 : char **ppszName) const
3377 :
3378 : {
3379 0 : return GetTargetLinearUnits(pszTargetKey,
3380 0 : const_cast<const char **>(ppszName));
3381 : }
3382 :
3383 : /************************************************************************/
3384 : /* OSRGetTargetLinearUnits() */
3385 : /************************************************************************/
3386 :
3387 : /**
3388 : * \brief Fetch linear projection units.
3389 : *
3390 : * This function is the same as OGRSpatialReference::GetTargetLinearUnits()
3391 : *
3392 : * @since OGR 1.9.0
3393 : */
3394 4 : double OSRGetTargetLinearUnits(OGRSpatialReferenceH hSRS,
3395 : const char *pszTargetKey, char **ppszName)
3396 :
3397 : {
3398 4 : VALIDATE_POINTER1(hSRS, "OSRGetTargetLinearUnits", 0);
3399 :
3400 4 : return ToPointer(hSRS)->GetTargetLinearUnits(
3401 4 : pszTargetKey, const_cast<const char **>(ppszName));
3402 : }
3403 :
3404 : /************************************************************************/
3405 : /* GetPrimeMeridian() */
3406 : /************************************************************************/
3407 :
3408 : /**
3409 : * \brief Fetch prime meridian info.
3410 : *
3411 : * Returns the offset of the prime meridian from greenwich in degrees,
3412 : * and the prime meridian name (if requested). If no PRIMEM value exists
3413 : * in the coordinate system definition a value of "Greenwich" and an
3414 : * offset of 0.0 is assumed.
3415 : *
3416 : * If the prime meridian name is returned, the pointer is to an internal
3417 : * copy of the name. It should not be freed, altered or depended on after
3418 : * the next OGR call.
3419 : *
3420 : * This method is the same as the C function OSRGetPrimeMeridian().
3421 : *
3422 : * @param ppszName return location for prime meridian name. If NULL, name
3423 : * is not returned.
3424 : *
3425 : * @return the offset to the GEOGCS prime meridian from greenwich in decimal
3426 : * degrees.
3427 : * @deprecated GDAL 2.3.0. Use GetPrimeMeridian(const char**) const.
3428 : */
3429 :
3430 1372 : double OGRSpatialReference::GetPrimeMeridian(const char **ppszName) const
3431 :
3432 : {
3433 2744 : TAKE_OPTIONAL_LOCK();
3434 :
3435 1372 : d->refreshProjObj();
3436 :
3437 1372 : if (!d->m_osPrimeMeridianName.empty())
3438 : {
3439 57 : if (ppszName != nullptr)
3440 1 : *ppszName = d->m_osPrimeMeridianName.c_str();
3441 57 : return d->dfFromGreenwich;
3442 : }
3443 :
3444 : while (true)
3445 : {
3446 1315 : if (!d->m_pj_crs)
3447 0 : break;
3448 :
3449 1315 : auto pm = proj_get_prime_meridian(d->getPROJContext(), d->m_pj_crs);
3450 1315 : if (!pm)
3451 0 : break;
3452 :
3453 1315 : d->m_osPrimeMeridianName = proj_get_name(pm);
3454 1315 : if (ppszName)
3455 30 : *ppszName = d->m_osPrimeMeridianName.c_str();
3456 1315 : double dfLongitude = 0.0;
3457 1315 : double dfConvFactor = 0.0;
3458 1315 : proj_prime_meridian_get_parameters(
3459 : d->getPROJContext(), pm, &dfLongitude, &dfConvFactor, nullptr);
3460 1315 : proj_destroy(pm);
3461 2630 : d->dfFromGreenwich =
3462 1315 : dfLongitude * dfConvFactor / CPLAtof(SRS_UA_DEGREE_CONV);
3463 1315 : return d->dfFromGreenwich;
3464 : }
3465 :
3466 0 : d->m_osPrimeMeridianName = SRS_PM_GREENWICH;
3467 0 : d->dfFromGreenwich = 0.0;
3468 0 : if (ppszName != nullptr)
3469 0 : *ppszName = d->m_osPrimeMeridianName.c_str();
3470 0 : return d->dfFromGreenwich;
3471 : }
3472 :
3473 : /**
3474 : * \brief Fetch prime meridian info.
3475 : *
3476 : * Returns the offset of the prime meridian from greenwich in degrees,
3477 : * and the prime meridian name (if requested). If no PRIMEM value exists
3478 : * in the coordinate system definition a value of "Greenwich" and an
3479 : * offset of 0.0 is assumed.
3480 : *
3481 : * If the prime meridian name is returned, the pointer is to an internal
3482 : * copy of the name. It should not be freed, altered or depended on after
3483 : * the next OGR call.
3484 : *
3485 : * This method is the same as the C function OSRGetPrimeMeridian().
3486 : *
3487 : * @param ppszName return location for prime meridian name. If NULL, name
3488 : * is not returned.
3489 : *
3490 : * @return the offset to the GEOGCS prime meridian from greenwich in decimal
3491 : * degrees.
3492 : * @since GDAL 2.3.0
3493 : */
3494 :
3495 0 : double OGRSpatialReference::GetPrimeMeridian(char **ppszName) const
3496 :
3497 : {
3498 0 : return GetPrimeMeridian(const_cast<const char **>(ppszName));
3499 : }
3500 :
3501 : /************************************************************************/
3502 : /* OSRGetPrimeMeridian() */
3503 : /************************************************************************/
3504 :
3505 : /**
3506 : * \brief Fetch prime meridian info.
3507 : *
3508 : * This function is the same as OGRSpatialReference::GetPrimeMeridian()
3509 : */
3510 0 : double OSRGetPrimeMeridian(OGRSpatialReferenceH hSRS, char **ppszName)
3511 :
3512 : {
3513 0 : VALIDATE_POINTER1(hSRS, "OSRGetPrimeMeridian", 0);
3514 :
3515 0 : return ToPointer(hSRS)->GetPrimeMeridian(
3516 0 : const_cast<const char **>(ppszName));
3517 : }
3518 :
3519 : /************************************************************************/
3520 : /* SetGeogCS() */
3521 : /************************************************************************/
3522 :
3523 : /**
3524 : * \brief Set geographic coordinate system.
3525 : *
3526 : * This method is used to set the datum, ellipsoid, prime meridian and
3527 : * angular units for a geographic coordinate system. It can be used on its
3528 : * own to establish a geographic spatial reference, or applied to a
3529 : * projected coordinate system to establish the underlying geographic
3530 : * coordinate system.
3531 : *
3532 : * This method does the same as the C function OSRSetGeogCS().
3533 : *
3534 : * @param pszGeogName user visible name for the geographic coordinate system
3535 : * (not to serve as a key).
3536 : *
3537 : * @param pszDatumName key name for this datum. The OpenGIS specification
3538 : * lists some known values, and otherwise EPSG datum names with a standard
3539 : * transformation are considered legal keys.
3540 : *
3541 : * @param pszSpheroidName user visible spheroid name (not to serve as a key)
3542 : *
3543 : * @param dfSemiMajor the semi major axis of the spheroid.
3544 : *
3545 : * @param dfInvFlattening the inverse flattening for the spheroid.
3546 : * This can be computed from the semi minor axis as
3547 : * 1/f = 1.0 / (1.0 - semiminor/semimajor).
3548 : *
3549 : * @param pszPMName the name of the prime meridian (not to serve as a key)
3550 : * If this is NULL a default value of "Greenwich" will be used.
3551 : *
3552 : * @param dfPMOffset the longitude of Greenwich relative to this prime
3553 : * meridian. Always in Degrees
3554 : *
3555 : * @param pszAngularUnits the angular units name (see ogr_srs_api.h for some
3556 : * standard names). If NULL a value of "degrees" will be assumed.
3557 : *
3558 : * @param dfConvertToRadians value to multiply angular units by to transform
3559 : * them to radians. A value of SRS_UA_DEGREE_CONV will be used if
3560 : * pszAngularUnits is NULL.
3561 : *
3562 : * @return OGRERR_NONE on success.
3563 : */
3564 :
3565 9018 : OGRErr OGRSpatialReference::SetGeogCS(
3566 : const char *pszGeogName, const char *pszDatumName,
3567 : const char *pszSpheroidName, double dfSemiMajor, double dfInvFlattening,
3568 : const char *pszPMName, double dfPMOffset, const char *pszAngularUnits,
3569 : double dfConvertToRadians)
3570 :
3571 : {
3572 18036 : TAKE_OPTIONAL_LOCK();
3573 :
3574 9018 : d->bNormInfoSet = FALSE;
3575 9018 : d->m_osAngularUnits.clear();
3576 9018 : d->m_dfAngularUnitToRadian = 0.0;
3577 9018 : d->m_osPrimeMeridianName.clear();
3578 9018 : d->dfFromGreenwich = 0.0;
3579 :
3580 : /* -------------------------------------------------------------------- */
3581 : /* For a geocentric coordinate system we want to set the datum */
3582 : /* and ellipsoid based on the GEOGCS. Create the GEOGCS in a */
3583 : /* temporary srs and use the copy method which has special */
3584 : /* handling for GEOCCS. */
3585 : /* -------------------------------------------------------------------- */
3586 9018 : if (IsGeocentric())
3587 : {
3588 4 : OGRSpatialReference oGCS;
3589 :
3590 2 : oGCS.SetGeogCS(pszGeogName, pszDatumName, pszSpheroidName, dfSemiMajor,
3591 : dfInvFlattening, pszPMName, dfPMOffset, pszAngularUnits,
3592 : dfConvertToRadians);
3593 2 : return CopyGeogCSFrom(&oGCS);
3594 : }
3595 :
3596 9016 : auto cs = proj_create_ellipsoidal_2D_cs(
3597 : d->getPROJContext(), PJ_ELLPS2D_LATITUDE_LONGITUDE, pszAngularUnits,
3598 : dfConvertToRadians);
3599 : // Prime meridian expressed in Degree
3600 9016 : auto obj = proj_create_geographic_crs(
3601 : d->getPROJContext(), pszGeogName, pszDatumName, pszSpheroidName,
3602 : dfSemiMajor, dfInvFlattening, pszPMName, dfPMOffset, nullptr, 0.0, cs);
3603 9016 : proj_destroy(cs);
3604 :
3605 13578 : if (d->m_pj_crs == nullptr || d->m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
3606 4562 : d->m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS)
3607 : {
3608 4454 : d->setPjCRS(obj);
3609 : }
3610 4562 : else if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
3611 : {
3612 9124 : d->setPjCRS(
3613 4562 : proj_crs_alter_geodetic_crs(d->getPROJContext(), d->m_pj_crs, obj));
3614 4562 : proj_destroy(obj);
3615 : }
3616 : else
3617 : {
3618 0 : proj_destroy(obj);
3619 : }
3620 :
3621 9016 : return OGRERR_NONE;
3622 : }
3623 :
3624 : /************************************************************************/
3625 : /* OSRSetGeogCS() */
3626 : /************************************************************************/
3627 :
3628 : /**
3629 : * \brief Set geographic coordinate system.
3630 : *
3631 : * This function is the same as OGRSpatialReference::SetGeogCS()
3632 : */
3633 18 : OGRErr OSRSetGeogCS(OGRSpatialReferenceH hSRS, const char *pszGeogName,
3634 : const char *pszDatumName, const char *pszSpheroidName,
3635 : double dfSemiMajor, double dfInvFlattening,
3636 : const char *pszPMName, double dfPMOffset,
3637 : const char *pszAngularUnits, double dfConvertToRadians)
3638 :
3639 : {
3640 18 : VALIDATE_POINTER1(hSRS, "OSRSetGeogCS", OGRERR_FAILURE);
3641 :
3642 18 : return ToPointer(hSRS)->SetGeogCS(pszGeogName, pszDatumName,
3643 : pszSpheroidName, dfSemiMajor,
3644 : dfInvFlattening, pszPMName, dfPMOffset,
3645 18 : pszAngularUnits, dfConvertToRadians);
3646 : }
3647 :
3648 : /************************************************************************/
3649 : /* SetWellKnownGeogCS() */
3650 : /************************************************************************/
3651 :
3652 : /**
3653 : * \brief Set a GeogCS based on well known name.
3654 : *
3655 : * This may be called on an empty OGRSpatialReference to make a geographic
3656 : * coordinate system, or on something with an existing PROJCS node to
3657 : * set the underlying geographic coordinate system of a projected coordinate
3658 : * system.
3659 : *
3660 : * The following well known text values are currently supported,
3661 : * Except for "EPSG:n", the others are without dependency on EPSG data files:
3662 : * <ul>
3663 : * <li> "EPSG:n": where n is the code a Geographic coordinate reference system.
3664 : * <li> "WGS84": same as "EPSG:4326" (axis order lat/long).
3665 : * <li> "WGS72": same as "EPSG:4322" (axis order lat/long).
3666 : * <li> "NAD83": same as "EPSG:4269" (axis order lat/long).
3667 : * <li> "NAD27": same as "EPSG:4267" (axis order lat/long).
3668 : * <li> "CRS84", "CRS:84": same as "WGS84" but with axis order long/lat.
3669 : * <li> "CRS72", "CRS:72": same as "WGS72" but with axis order long/lat.
3670 : * <li> "CRS27", "CRS:27": same as "NAD27" but with axis order long/lat.
3671 : * </ul>
3672 : *
3673 : * @param pszName name of well known geographic coordinate system.
3674 : * @return OGRERR_NONE on success, or OGRERR_FAILURE if the name isn't
3675 : * recognised, the target object is already initialized, or an EPSG value
3676 : * can't be successfully looked up.
3677 : */
3678 :
3679 2725 : OGRErr OGRSpatialReference::SetWellKnownGeogCS(const char *pszName)
3680 :
3681 : {
3682 5450 : TAKE_OPTIONAL_LOCK();
3683 :
3684 : /* -------------------------------------------------------------------- */
3685 : /* Check for EPSG authority numbers. */
3686 : /* -------------------------------------------------------------------- */
3687 2725 : if (STARTS_WITH_CI(pszName, "EPSG:") || STARTS_WITH_CI(pszName, "EPSGA:"))
3688 : {
3689 84 : OGRSpatialReference oSRS2;
3690 42 : const OGRErr eErr = oSRS2.importFromEPSG(atoi(pszName + 5));
3691 42 : if (eErr != OGRERR_NONE)
3692 0 : return eErr;
3693 :
3694 42 : if (!oSRS2.IsGeographic())
3695 0 : return OGRERR_FAILURE;
3696 :
3697 42 : return CopyGeogCSFrom(&oSRS2);
3698 : }
3699 :
3700 : /* -------------------------------------------------------------------- */
3701 : /* Check for simple names. */
3702 : /* -------------------------------------------------------------------- */
3703 2683 : const char *pszWKT = nullptr;
3704 :
3705 2683 : if (EQUAL(pszName, "WGS84"))
3706 : {
3707 2091 : pszWKT = SRS_WKT_WGS84_LAT_LONG;
3708 : }
3709 592 : else if (EQUAL(pszName, "CRS84") || EQUAL(pszName, "CRS:84"))
3710 : {
3711 414 : pszWKT =
3712 : "GEOGCRS[\"WGS 84 (CRS84)\",DATUM[\"World Geodetic System 1984\","
3713 : "ELLIPSOID[\"WGS "
3714 : "84\",6378137,298.257223563,LENGTHUNIT[\"metre\",1]]],"
3715 : "PRIMEM[\"Greenwich\",0,ANGLEUNIT[\"degree\",0.0174532925199433]],"
3716 : "CS[ellipsoidal,2],AXIS[\"geodetic longitude (Lon)\",east,ORDER[1],"
3717 : "ANGLEUNIT[\"degree\",0.0174532925199433]],"
3718 : "AXIS[\"geodetic latitude (Lat)\",north,ORDER[2],"
3719 : "ANGLEUNIT[\"degree\",0.0174532925199433]],"
3720 : "USAGE[SCOPE[\"unknown\"],AREA[\"World\"],BBOX[-90,-180,90,180]],"
3721 : "ID[\"OGC\",\"CRS84\"]]";
3722 : }
3723 178 : else if (EQUAL(pszName, "WGS72"))
3724 19 : pszWKT =
3725 : "GEOGCS[\"WGS 72\",DATUM[\"WGS_1972\","
3726 : "SPHEROID[\"WGS 72\",6378135,298.26,AUTHORITY[\"EPSG\",\"7043\"]],"
3727 : "AUTHORITY[\"EPSG\",\"6322\"]],"
3728 : "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3729 : "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3730 : "AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],"
3731 : "AUTHORITY[\"EPSG\",\"4322\"]]";
3732 :
3733 159 : else if (EQUAL(pszName, "NAD27"))
3734 131 : pszWKT =
3735 : "GEOGCS[\"NAD27\",DATUM[\"North_American_Datum_1927\","
3736 : "SPHEROID[\"Clarke 1866\",6378206.4,294.9786982138982,"
3737 : "AUTHORITY[\"EPSG\",\"7008\"]],AUTHORITY[\"EPSG\",\"6267\"]],"
3738 : "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3739 : "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3740 : "AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],"
3741 : "AUTHORITY[\"EPSG\",\"4267\"]]";
3742 :
3743 28 : else if (EQUAL(pszName, "CRS27") || EQUAL(pszName, "CRS:27"))
3744 0 : pszWKT =
3745 : "GEOGCS[\"NAD27\",DATUM[\"North_American_Datum_1927\","
3746 : "SPHEROID[\"Clarke 1866\",6378206.4,294.9786982138982,"
3747 : "AUTHORITY[\"EPSG\",\"7008\"]],AUTHORITY[\"EPSG\",\"6267\"]],"
3748 : "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3749 : "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3750 : "AXIS[\"Longitude\",EAST],AXIS[\"Latitude\",NORTH]]";
3751 :
3752 28 : else if (EQUAL(pszName, "NAD83"))
3753 24 : pszWKT =
3754 : "GEOGCS[\"NAD83\",DATUM[\"North_American_Datum_1983\","
3755 : "SPHEROID[\"GRS 1980\",6378137,298.257222101,"
3756 : "AUTHORITY[\"EPSG\",\"7019\"]],"
3757 : "AUTHORITY[\"EPSG\",\"6269\"]],"
3758 : "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3759 : "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3760 : "AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],AUTHORITY["
3761 : "\"EPSG\",\"4269\"]]";
3762 :
3763 4 : else if (EQUAL(pszName, "CRS83") || EQUAL(pszName, "CRS:83"))
3764 0 : pszWKT =
3765 : "GEOGCS[\"NAD83\",DATUM[\"North_American_Datum_1983\","
3766 : "SPHEROID[\"GRS 1980\",6378137,298.257222101,"
3767 : "AUTHORITY[\"EPSG\",\"7019\"]],"
3768 : "AUTHORITY[\"EPSG\",\"6269\"]],"
3769 : "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3770 : "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3771 : "AXIS[\"Longitude\",EAST],AXIS[\"Latitude\",NORTH]]";
3772 :
3773 : else
3774 4 : return OGRERR_FAILURE;
3775 :
3776 : /* -------------------------------------------------------------------- */
3777 : /* Import the WKT */
3778 : /* -------------------------------------------------------------------- */
3779 5358 : OGRSpatialReference oSRS2;
3780 2679 : const OGRErr eErr = oSRS2.importFromWkt(pszWKT);
3781 2679 : if (eErr != OGRERR_NONE)
3782 0 : return eErr;
3783 :
3784 : /* -------------------------------------------------------------------- */
3785 : /* Copy over. */
3786 : /* -------------------------------------------------------------------- */
3787 2679 : return CopyGeogCSFrom(&oSRS2);
3788 : }
3789 :
3790 : /************************************************************************/
3791 : /* OSRSetWellKnownGeogCS() */
3792 : /************************************************************************/
3793 :
3794 : /**
3795 : * \brief Set a GeogCS based on well known name.
3796 : *
3797 : * This function is the same as OGRSpatialReference::SetWellKnownGeogCS()
3798 : */
3799 130 : OGRErr OSRSetWellKnownGeogCS(OGRSpatialReferenceH hSRS, const char *pszName)
3800 :
3801 : {
3802 130 : VALIDATE_POINTER1(hSRS, "OSRSetWellKnownGeogCS", OGRERR_FAILURE);
3803 :
3804 130 : return ToPointer(hSRS)->SetWellKnownGeogCS(pszName);
3805 : }
3806 :
3807 : /************************************************************************/
3808 : /* CopyGeogCSFrom() */
3809 : /************************************************************************/
3810 :
3811 : /**
3812 : * \brief Copy GEOGCS from another OGRSpatialReference.
3813 : *
3814 : * The GEOGCS information is copied into this OGRSpatialReference from another.
3815 : * If this object has a PROJCS root already, the GEOGCS is installed within
3816 : * it, otherwise it is installed as the root.
3817 : *
3818 : * @param poSrcSRS the spatial reference to copy the GEOGCS information from.
3819 : *
3820 : * @return OGRERR_NONE on success or an error code.
3821 : */
3822 :
3823 3313 : OGRErr OGRSpatialReference::CopyGeogCSFrom(const OGRSpatialReference *poSrcSRS)
3824 :
3825 : {
3826 6626 : TAKE_OPTIONAL_LOCK();
3827 :
3828 3313 : d->bNormInfoSet = FALSE;
3829 3313 : d->m_osAngularUnits.clear();
3830 3313 : d->m_dfAngularUnitToRadian = 0.0;
3831 3313 : d->m_osPrimeMeridianName.clear();
3832 3313 : d->dfFromGreenwich = 0.0;
3833 :
3834 3313 : d->refreshProjObj();
3835 3313 : poSrcSRS->d->refreshProjObj();
3836 3313 : if (!poSrcSRS->d->m_pj_crs)
3837 : {
3838 0 : return OGRERR_FAILURE;
3839 : }
3840 : auto geodCRS =
3841 3313 : proj_crs_get_geodetic_crs(d->getPROJContext(), poSrcSRS->d->m_pj_crs);
3842 3313 : if (!geodCRS)
3843 : {
3844 0 : return OGRERR_FAILURE;
3845 : }
3846 :
3847 : /* -------------------------------------------------------------------- */
3848 : /* Handle geocentric coordinate systems specially. We just */
3849 : /* want to copy the DATUM. */
3850 : /* -------------------------------------------------------------------- */
3851 3313 : if (d->m_pjType == PJ_TYPE_GEOCENTRIC_CRS)
3852 : {
3853 3 : auto datum = proj_crs_get_datum(d->getPROJContext(), geodCRS);
3854 : #if PROJ_VERSION_MAJOR > 7 || \
3855 : (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
3856 : if (datum == nullptr)
3857 : {
3858 : datum = proj_crs_get_datum_ensemble(d->getPROJContext(), geodCRS);
3859 : }
3860 : #endif
3861 3 : if (datum == nullptr)
3862 : {
3863 0 : proj_destroy(geodCRS);
3864 0 : return OGRERR_FAILURE;
3865 : }
3866 :
3867 3 : const char *pszUnitName = nullptr;
3868 3 : double unitConvFactor = GetLinearUnits(&pszUnitName);
3869 :
3870 3 : auto pj_crs = proj_create_geocentric_crs_from_datum(
3871 3 : d->getPROJContext(), proj_get_name(d->m_pj_crs), datum, pszUnitName,
3872 : unitConvFactor);
3873 3 : proj_destroy(datum);
3874 :
3875 3 : d->setPjCRS(pj_crs);
3876 : }
3877 :
3878 3310 : else if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
3879 : {
3880 324 : auto pj_crs = proj_crs_alter_geodetic_crs(d->getPROJContext(),
3881 324 : d->m_pj_crs, geodCRS);
3882 324 : d->setPjCRS(pj_crs);
3883 : }
3884 :
3885 : else
3886 : {
3887 2986 : d->setPjCRS(proj_clone(d->getPROJContext(), geodCRS));
3888 : }
3889 :
3890 : // Apply TOWGS84 of source CRS
3891 3313 : if (poSrcSRS->d->m_pjType == PJ_TYPE_BOUND_CRS)
3892 : {
3893 : auto target =
3894 1 : proj_get_target_crs(d->getPROJContext(), poSrcSRS->d->m_pj_crs);
3895 1 : auto co = proj_crs_get_coordoperation(d->getPROJContext(),
3896 1 : poSrcSRS->d->m_pj_crs);
3897 1 : d->setPjCRS(proj_crs_create_bound_crs(d->getPROJContext(), d->m_pj_crs,
3898 : target, co));
3899 1 : proj_destroy(target);
3900 1 : proj_destroy(co);
3901 : }
3902 :
3903 3313 : proj_destroy(geodCRS);
3904 :
3905 3313 : return OGRERR_NONE;
3906 : }
3907 :
3908 : /************************************************************************/
3909 : /* OSRCopyGeogCSFrom() */
3910 : /************************************************************************/
3911 :
3912 : /**
3913 : * \brief Copy GEOGCS from another OGRSpatialReference.
3914 : *
3915 : * This function is the same as OGRSpatialReference::CopyGeogCSFrom()
3916 : */
3917 1 : OGRErr OSRCopyGeogCSFrom(OGRSpatialReferenceH hSRS,
3918 : const OGRSpatialReferenceH hSrcSRS)
3919 :
3920 : {
3921 1 : VALIDATE_POINTER1(hSRS, "OSRCopyGeogCSFrom", OGRERR_FAILURE);
3922 1 : VALIDATE_POINTER1(hSrcSRS, "OSRCopyGeogCSFrom", OGRERR_FAILURE);
3923 :
3924 1 : return ToPointer(hSRS)->CopyGeogCSFrom(ToPointer(hSrcSRS));
3925 : }
3926 :
3927 : /************************************************************************/
3928 : /* SET_FROM_USER_INPUT_LIMITATIONS_get() */
3929 : /************************************************************************/
3930 :
3931 : /** Limitations for OGRSpatialReference::SetFromUserInput().
3932 : *
3933 : * Currently ALLOW_NETWORK_ACCESS=NO and ALLOW_FILE_ACCESS=NO.
3934 : */
3935 : const char *const OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS[] = {
3936 : "ALLOW_NETWORK_ACCESS=NO", "ALLOW_FILE_ACCESS=NO", nullptr};
3937 :
3938 : /**
3939 : * \brief Return OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS
3940 : */
3941 2488 : CSLConstList OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS_get()
3942 : {
3943 2488 : return SET_FROM_USER_INPUT_LIMITATIONS;
3944 : }
3945 :
3946 : /************************************************************************/
3947 : /* RemoveIDFromMemberOfEnsembles() */
3948 : /************************************************************************/
3949 :
3950 : // cppcheck-suppress constParameterReference
3951 198 : static void RemoveIDFromMemberOfEnsembles(CPLJSONObject &obj)
3952 : {
3953 : // Remove "id" from members of datum ensembles for compatibility with
3954 : // older PROJ versions
3955 : // Cf https://github.com/opengeospatial/geoparquet/discussions/110
3956 : // and https://github.com/OSGeo/PROJ/pull/3221
3957 198 : if (obj.GetType() == CPLJSONObject::Type::Object)
3958 : {
3959 243 : for (auto &subObj : obj.GetChildren())
3960 : {
3961 191 : RemoveIDFromMemberOfEnsembles(subObj);
3962 : }
3963 : }
3964 162 : else if (obj.GetType() == CPLJSONObject::Type::Array &&
3965 162 : obj.GetName() == "members")
3966 : {
3967 51 : for (auto &subObj : obj.ToArray())
3968 : {
3969 44 : if (subObj.GetType() == CPLJSONObject::Type::Object)
3970 : {
3971 43 : subObj.Delete("id");
3972 : }
3973 : }
3974 : }
3975 198 : }
3976 :
3977 : /************************************************************************/
3978 : /* SetFromUserInput() */
3979 : /************************************************************************/
3980 :
3981 : /**
3982 : * \brief Set spatial reference from various text formats.
3983 : *
3984 : * This method will examine the provided input, and try to deduce the
3985 : * format, and then use it to initialize the spatial reference system. It
3986 : * may take the following forms:
3987 : *
3988 : * <ol>
3989 : * <li> Well Known Text definition - passed on to importFromWkt().
3990 : * <li> "EPSG:n" - number passed on to importFromEPSG().
3991 : * <li> "EPSGA:n" - number passed on to importFromEPSGA().
3992 : * <li> "AUTO:proj_id,unit_id,lon0,lat0" - WMS auto projections.
3993 : * <li> "urn:ogc:def:crs:EPSG::n" - ogc urns
3994 : * <li> PROJ.4 definitions - passed on to importFromProj4().
3995 : * <li> filename - file read for WKT, XML or PROJ.4 definition.
3996 : * <li> well known name accepted by SetWellKnownGeogCS(), such as NAD27, NAD83,
3997 : * WGS84 or WGS72.
3998 : * <li> "IGNF:xxxx", "ESRI:xxxx", etc. from definitions from the PROJ database;
3999 : * <li> PROJJSON (PROJ >= 6.2)
4000 : * </ol>
4001 : *
4002 : * It is expected that this method will be extended in the future to support
4003 : * XML and perhaps a simplified "minilanguage" for indicating common UTM and
4004 : * State Plane definitions.
4005 : *
4006 : * This method is intended to be flexible, but by its nature it is
4007 : * imprecise as it must guess information about the format intended. When
4008 : * possible applications should call the specific method appropriate if the
4009 : * input is known to be in a particular format.
4010 : *
4011 : * This method does the same thing as the OSRSetFromUserInput() function.
4012 : *
4013 : * @param pszDefinition text definition to try to deduce SRS from.
4014 : *
4015 : * @return OGRERR_NONE on success, or an error code if the name isn't
4016 : * recognised, the definition is corrupt, or an EPSG value can't be
4017 : * successfully looked up.
4018 : */
4019 :
4020 16667 : OGRErr OGRSpatialReference::SetFromUserInput(const char *pszDefinition)
4021 : {
4022 16667 : return SetFromUserInput(pszDefinition, nullptr);
4023 : }
4024 :
4025 : /**
4026 : * \brief Set spatial reference from various text formats.
4027 : *
4028 : * This method will examine the provided input, and try to deduce the
4029 : * format, and then use it to initialize the spatial reference system. It
4030 : * may take the following forms:
4031 : *
4032 : * <ol>
4033 : * <li> Well Known Text definition - passed on to importFromWkt().
4034 : * <li> "EPSG:n" - number passed on to importFromEPSG().
4035 : * <li> "EPSGA:n" - number passed on to importFromEPSGA().
4036 : * <li> "AUTO:proj_id,unit_id,lon0,lat0" - WMS auto projections.
4037 : * <li> "urn:ogc:def:crs:EPSG::n" - ogc urns
4038 : * <li> PROJ.4 definitions - passed on to importFromProj4().
4039 : * <li> filename - file read for WKT, XML or PROJ.4 definition.
4040 : * <li> well known name accepted by SetWellKnownGeogCS(), such as NAD27, NAD83,
4041 : * WGS84 or WGS72.
4042 : * <li> "IGNF:xxxx", "ESRI:xxxx", etc. from definitions from the PROJ database;
4043 : * <li> PROJJSON (PROJ >= 6.2)
4044 : * </ol>
4045 : *
4046 : * It is expected that this method will be extended in the future to support
4047 : * XML and perhaps a simplified "minilanguage" for indicating common UTM and
4048 : * State Plane definitions.
4049 : *
4050 : * This method is intended to be flexible, but by its nature it is
4051 : * imprecise as it must guess information about the format intended. When
4052 : * possible applications should call the specific method appropriate if the
4053 : * input is known to be in a particular format.
4054 : *
4055 : * This method does the same thing as the OSRSetFromUserInput() and
4056 : * OSRSetFromUserInputEx() functions.
4057 : *
4058 : * @param pszDefinition text definition to try to deduce SRS from.
4059 : *
4060 : * @param papszOptions NULL terminated list of options, or NULL.
4061 : * <ol>
4062 : * <li> ALLOW_NETWORK_ACCESS=YES/NO.
4063 : * Whether http:// or https:// access is allowed. Defaults to YES.
4064 : * <li> ALLOW_FILE_ACCESS=YES/NO.
4065 : * Whether reading a file using the Virtual File System layer is allowed
4066 : * (can also involve network access). Defaults to YES.
4067 : * </ol>
4068 : *
4069 : * @return OGRERR_NONE on success, or an error code if the name isn't
4070 : * recognised, the definition is corrupt, or an EPSG value can't be
4071 : * successfully looked up.
4072 : */
4073 :
4074 22833 : OGRErr OGRSpatialReference::SetFromUserInput(const char *pszDefinition,
4075 : CSLConstList papszOptions)
4076 : {
4077 45666 : TAKE_OPTIONAL_LOCK();
4078 :
4079 : // Skip leading white space
4080 22835 : while (isspace(static_cast<unsigned char>(*pszDefinition)))
4081 2 : pszDefinition++;
4082 :
4083 22833 : if (STARTS_WITH_CI(pszDefinition, "ESRI::"))
4084 : {
4085 1 : pszDefinition += 6;
4086 : }
4087 :
4088 : /* -------------------------------------------------------------------- */
4089 : /* Is it a recognised syntax? */
4090 : /* -------------------------------------------------------------------- */
4091 22833 : const char *const wktKeywords[] = {
4092 : // WKT1
4093 : "GEOGCS", "GEOCCS", "PROJCS", "VERT_CS", "COMPD_CS", "LOCAL_CS",
4094 : // WKT2"
4095 : "GEODCRS", "GEOGCRS", "GEODETICCRS", "GEOGRAPHICCRS", "PROJCRS",
4096 : "PROJECTEDCRS", "VERTCRS", "VERTICALCRS", "COMPOUNDCRS", "ENGCRS",
4097 : "ENGINEERINGCRS", "BOUNDCRS", "DERIVEDPROJCRS", "COORDINATEMETADATA"};
4098 321948 : for (const char *keyword : wktKeywords)
4099 : {
4100 307489 : if (STARTS_WITH_CI(pszDefinition, keyword))
4101 : {
4102 8374 : return importFromWkt(pszDefinition);
4103 : }
4104 : }
4105 :
4106 14459 : const bool bStartsWithEPSG = STARTS_WITH_CI(pszDefinition, "EPSG:");
4107 14459 : if (bStartsWithEPSG || STARTS_WITH_CI(pszDefinition, "EPSGA:"))
4108 : {
4109 8951 : OGRErr eStatus = OGRERR_NONE;
4110 :
4111 8951 : if (strchr(pszDefinition, '+') || strchr(pszDefinition, '@'))
4112 : {
4113 : // Use proj_create() as it allows things like EPSG:3157+4617
4114 : // that are not normally supported by the below code that
4115 : // builds manually a compound CRS
4116 62 : PJ *pj = proj_create(d->getPROJContext(), pszDefinition);
4117 62 : if (!pj)
4118 : {
4119 1 : return OGRERR_FAILURE;
4120 : }
4121 61 : Clear();
4122 61 : d->setPjCRS(pj);
4123 61 : return OGRERR_NONE;
4124 : }
4125 : else
4126 : {
4127 : eStatus =
4128 8889 : importFromEPSG(atoi(pszDefinition + (bStartsWithEPSG ? 5 : 6)));
4129 : }
4130 :
4131 8889 : return eStatus;
4132 : }
4133 :
4134 5508 : if (STARTS_WITH_CI(pszDefinition, "urn:ogc:def:crs:") ||
4135 4836 : STARTS_WITH_CI(pszDefinition, "urn:ogc:def:crs,crs:") ||
4136 4835 : STARTS_WITH_CI(pszDefinition, "urn:x-ogc:def:crs:") ||
4137 4777 : STARTS_WITH_CI(pszDefinition, "urn:opengis:crs:") ||
4138 4777 : STARTS_WITH_CI(pszDefinition, "urn:opengis:def:crs:") ||
4139 4777 : STARTS_WITH_CI(pszDefinition, "urn:ogc:def:coordinateMetadata:"))
4140 731 : return importFromURN(pszDefinition);
4141 :
4142 4777 : if (STARTS_WITH_CI(pszDefinition, "http://opengis.net/def/crs") ||
4143 4775 : STARTS_WITH_CI(pszDefinition, "https://opengis.net/def/crs") ||
4144 4774 : STARTS_WITH_CI(pszDefinition, "http://www.opengis.net/def/crs") ||
4145 1141 : STARTS_WITH_CI(pszDefinition, "https://www.opengis.net/def/crs") ||
4146 1140 : STARTS_WITH_CI(pszDefinition, "www.opengis.net/def/crs"))
4147 3637 : return importFromCRSURL(pszDefinition);
4148 :
4149 1140 : if (STARTS_WITH_CI(pszDefinition, "AUTO:"))
4150 1 : return importFromWMSAUTO(pszDefinition);
4151 :
4152 : // WMS/WCS OGC codes like OGC:CRS84.
4153 1139 : if (EQUAL(pszDefinition, "OGC:CRS84"))
4154 114 : return SetWellKnownGeogCS(pszDefinition + 4);
4155 :
4156 1025 : if (STARTS_WITH_CI(pszDefinition, "CRS:"))
4157 1 : return SetWellKnownGeogCS(pszDefinition);
4158 :
4159 1024 : if (STARTS_WITH_CI(pszDefinition, "DICT:") && strstr(pszDefinition, ","))
4160 : {
4161 0 : char *pszFile = CPLStrdup(pszDefinition + 5);
4162 0 : char *pszCode = strstr(pszFile, ",") + 1;
4163 :
4164 0 : pszCode[-1] = '\0';
4165 :
4166 0 : OGRErr err = importFromDict(pszFile, pszCode);
4167 0 : CPLFree(pszFile);
4168 :
4169 0 : return err;
4170 : }
4171 :
4172 1024 : if (EQUAL(pszDefinition, "NAD27") || EQUAL(pszDefinition, "NAD83") ||
4173 1020 : EQUAL(pszDefinition, "WGS84") || EQUAL(pszDefinition, "WGS72"))
4174 : {
4175 303 : Clear();
4176 303 : return SetWellKnownGeogCS(pszDefinition);
4177 : }
4178 :
4179 : // PROJJSON
4180 721 : if (pszDefinition[0] == '{' && strstr(pszDefinition, "\"type\"") &&
4181 55 : (strstr(pszDefinition, "GeodeticCRS") ||
4182 55 : strstr(pszDefinition, "GeographicCRS") ||
4183 23 : strstr(pszDefinition, "ProjectedCRS") ||
4184 0 : strstr(pszDefinition, "VerticalCRS") ||
4185 0 : strstr(pszDefinition, "BoundCRS") ||
4186 0 : strstr(pszDefinition, "CompoundCRS") ||
4187 0 : strstr(pszDefinition, "DerivedGeodeticCRS") ||
4188 0 : strstr(pszDefinition, "DerivedGeographicCRS") ||
4189 0 : strstr(pszDefinition, "DerivedProjectedCRS") ||
4190 0 : strstr(pszDefinition, "DerivedVerticalCRS") ||
4191 0 : strstr(pszDefinition, "EngineeringCRS") ||
4192 0 : strstr(pszDefinition, "DerivedEngineeringCRS") ||
4193 0 : strstr(pszDefinition, "ParametricCRS") ||
4194 0 : strstr(pszDefinition, "DerivedParametricCRS") ||
4195 0 : strstr(pszDefinition, "TemporalCRS") ||
4196 0 : strstr(pszDefinition, "DerivedTemporalCRS")))
4197 : {
4198 : PJ *pj;
4199 55 : if (strstr(pszDefinition, "datum_ensemble") != nullptr)
4200 : {
4201 : // PROJ < 9.0.1 doesn't like a datum_ensemble whose member have
4202 : // a unknown id.
4203 7 : CPLJSONDocument oCRSDoc;
4204 7 : if (!oCRSDoc.LoadMemory(pszDefinition))
4205 0 : return OGRERR_CORRUPT_DATA;
4206 7 : CPLJSONObject oCRSRoot = oCRSDoc.GetRoot();
4207 7 : RemoveIDFromMemberOfEnsembles(oCRSRoot);
4208 7 : pj = proj_create(d->getPROJContext(), oCRSRoot.ToString().c_str());
4209 : }
4210 : else
4211 : {
4212 48 : pj = proj_create(d->getPROJContext(), pszDefinition);
4213 : }
4214 55 : if (!pj)
4215 : {
4216 2 : return OGRERR_FAILURE;
4217 : }
4218 53 : Clear();
4219 53 : d->setPjCRS(pj);
4220 53 : return OGRERR_NONE;
4221 : }
4222 :
4223 666 : if (strstr(pszDefinition, "+proj") != nullptr ||
4224 240 : strstr(pszDefinition, "+init") != nullptr)
4225 426 : return importFromProj4(pszDefinition);
4226 :
4227 240 : if (STARTS_WITH_CI(pszDefinition, "http://") ||
4228 240 : STARTS_WITH_CI(pszDefinition, "https://"))
4229 : {
4230 1 : if (CPLTestBool(CSLFetchNameValueDef(papszOptions,
4231 : "ALLOW_NETWORK_ACCESS", "YES")))
4232 0 : return importFromUrl(pszDefinition);
4233 :
4234 1 : CPLError(CE_Failure, CPLE_AppDefined,
4235 : "Cannot import %s due to ALLOW_NETWORK_ACCESS=NO",
4236 : pszDefinition);
4237 1 : return OGRERR_FAILURE;
4238 : }
4239 :
4240 239 : if (EQUAL(pszDefinition, "osgb:BNG"))
4241 : {
4242 13 : return importFromEPSG(27700);
4243 : }
4244 :
4245 : // Used by German CityGML files
4246 226 : if (EQUAL(pszDefinition, "urn:adv:crs:ETRS89_UTM32*DE_DHHN92_NH"))
4247 : {
4248 : // "ETRS89 / UTM Zone 32N + DHHN92 height"
4249 0 : return SetFromUserInput("EPSG:25832+5783");
4250 : }
4251 226 : else if (EQUAL(pszDefinition, "urn:adv:crs:ETRS89_UTM32*DE_DHHN2016_NH"))
4252 : {
4253 : // "ETRS89 / UTM Zone 32N + DHHN2016 height"
4254 4 : return SetFromUserInput("EPSG:25832+7837");
4255 : }
4256 :
4257 : // Deal with IGNF:xxx, ESRI:xxx, etc from the PROJ database
4258 222 : const char *pszDot = strrchr(pszDefinition, ':');
4259 222 : if (pszDot)
4260 : {
4261 109 : CPLString osPrefix(pszDefinition, pszDot - pszDefinition);
4262 : auto authorities =
4263 109 : proj_get_authorities_from_database(d->getPROJContext());
4264 109 : if (authorities)
4265 : {
4266 109 : std::set<std::string> aosCandidateAuthorities;
4267 236 : for (auto iter = authorities; *iter; ++iter)
4268 : {
4269 233 : if (*iter == osPrefix)
4270 : {
4271 106 : aosCandidateAuthorities.clear();
4272 106 : aosCandidateAuthorities.insert(*iter);
4273 106 : break;
4274 : }
4275 : // Deal with "IAU_2015" as authority in the list and input
4276 : // "IAU:code"
4277 127 : else if (strncmp(*iter, osPrefix.c_str(), osPrefix.size()) ==
4278 127 : 0 &&
4279 0 : (*iter)[osPrefix.size()] == '_')
4280 : {
4281 0 : aosCandidateAuthorities.insert(*iter);
4282 : }
4283 : // Deal with "IAU_2015" as authority in the list and input
4284 : // "IAU:2015:code"
4285 254 : else if (osPrefix.find(':') != std::string::npos &&
4286 127 : osPrefix.size() == strlen(*iter) &&
4287 127 : CPLString(osPrefix).replaceAll(':', '_') == *iter)
4288 : {
4289 0 : aosCandidateAuthorities.clear();
4290 0 : aosCandidateAuthorities.insert(*iter);
4291 0 : break;
4292 : }
4293 : }
4294 :
4295 109 : proj_string_list_destroy(authorities);
4296 :
4297 109 : if (!aosCandidateAuthorities.empty())
4298 : {
4299 106 : auto obj = proj_create_from_database(
4300 : d->getPROJContext(),
4301 106 : aosCandidateAuthorities.rbegin()->c_str(), pszDot + 1,
4302 : PJ_CATEGORY_CRS, false, nullptr);
4303 106 : if (!obj)
4304 : {
4305 3 : return OGRERR_FAILURE;
4306 : }
4307 103 : Clear();
4308 103 : d->setPjCRS(obj);
4309 103 : return OGRERR_NONE;
4310 : }
4311 : }
4312 : }
4313 :
4314 : /* -------------------------------------------------------------------- */
4315 : /* Try to open it as a file. */
4316 : /* -------------------------------------------------------------------- */
4317 116 : if (!CPLTestBool(
4318 : CSLFetchNameValueDef(papszOptions, "ALLOW_FILE_ACCESS", "YES")))
4319 : {
4320 : VSIStatBufL sStat;
4321 24 : if (STARTS_WITH(pszDefinition, "/vsi") ||
4322 12 : VSIStatExL(pszDefinition, &sStat, VSI_STAT_EXISTS_FLAG) == 0)
4323 : {
4324 0 : CPLError(CE_Failure, CPLE_AppDefined,
4325 : "Cannot import %s due to ALLOW_FILE_ACCESS=NO",
4326 : pszDefinition);
4327 0 : return OGRERR_FAILURE;
4328 : }
4329 : // We used to silently return an error without a CE_Failure message
4330 : // Cf https://github.com/Toblerity/Fiona/issues/1063
4331 12 : return OGRERR_CORRUPT_DATA;
4332 : }
4333 :
4334 208 : CPLConfigOptionSetter oSetter("CPL_ALLOW_VSISTDIN", "NO", true);
4335 104 : VSILFILE *const fp = VSIFOpenL(pszDefinition, "rt");
4336 104 : if (fp == nullptr)
4337 101 : return OGRERR_CORRUPT_DATA;
4338 :
4339 3 : const size_t nBufMax = 100000;
4340 3 : char *const pszBuffer = static_cast<char *>(CPLMalloc(nBufMax));
4341 3 : const size_t nBytes = VSIFReadL(pszBuffer, 1, nBufMax - 1, fp);
4342 3 : VSIFCloseL(fp);
4343 :
4344 3 : if (nBytes == nBufMax - 1)
4345 : {
4346 0 : CPLDebug("OGR",
4347 : "OGRSpatialReference::SetFromUserInput(%s), opened file "
4348 : "but it is to large for our generous buffer. Is it really "
4349 : "just a WKT definition?",
4350 : pszDefinition);
4351 0 : CPLFree(pszBuffer);
4352 0 : return OGRERR_FAILURE;
4353 : }
4354 :
4355 3 : pszBuffer[nBytes] = '\0';
4356 :
4357 3 : char *pszBufPtr = pszBuffer;
4358 3 : while (pszBufPtr[0] == ' ' || pszBufPtr[0] == '\n')
4359 0 : pszBufPtr++;
4360 :
4361 3 : OGRErr err = OGRERR_NONE;
4362 3 : if (pszBufPtr[0] == '<')
4363 0 : err = importFromXML(pszBufPtr);
4364 3 : else if ((strstr(pszBuffer, "+proj") != nullptr ||
4365 3 : strstr(pszBuffer, "+init") != nullptr) &&
4366 0 : (strstr(pszBuffer, "EXTENSION") == nullptr &&
4367 0 : strstr(pszBuffer, "extension") == nullptr))
4368 0 : err = importFromProj4(pszBufPtr);
4369 : else
4370 : {
4371 3 : if (STARTS_WITH_CI(pszBufPtr, "ESRI::"))
4372 : {
4373 0 : pszBufPtr += 6;
4374 : }
4375 :
4376 : // coverity[tainted_data]
4377 3 : err = importFromWkt(pszBufPtr);
4378 : }
4379 :
4380 3 : CPLFree(pszBuffer);
4381 :
4382 3 : return err;
4383 : }
4384 :
4385 : /************************************************************************/
4386 : /* OSRSetFromUserInput() */
4387 : /************************************************************************/
4388 :
4389 : /**
4390 : * \brief Set spatial reference from various text formats.
4391 : *
4392 : * This function is the same as OGRSpatialReference::SetFromUserInput()
4393 : *
4394 : * \see OSRSetFromUserInputEx() for a variant allowing to pass options.
4395 : */
4396 288 : OGRErr CPL_STDCALL OSRSetFromUserInput(OGRSpatialReferenceH hSRS,
4397 : const char *pszDef)
4398 :
4399 : {
4400 288 : VALIDATE_POINTER1(hSRS, "OSRSetFromUserInput", OGRERR_FAILURE);
4401 :
4402 288 : return ToPointer(hSRS)->SetFromUserInput(pszDef);
4403 : }
4404 :
4405 : /************************************************************************/
4406 : /* OSRSetFromUserInputEx() */
4407 : /************************************************************************/
4408 :
4409 : /**
4410 : * \brief Set spatial reference from various text formats.
4411 : *
4412 : * This function is the same as OGRSpatialReference::SetFromUserInput().
4413 : *
4414 : * @since GDAL 3.9
4415 : */
4416 942 : OGRErr OSRSetFromUserInputEx(OGRSpatialReferenceH hSRS, const char *pszDef,
4417 : CSLConstList papszOptions)
4418 :
4419 : {
4420 942 : VALIDATE_POINTER1(hSRS, "OSRSetFromUserInputEx", OGRERR_FAILURE);
4421 :
4422 942 : return ToPointer(hSRS)->SetFromUserInput(pszDef, papszOptions);
4423 : }
4424 :
4425 : /************************************************************************/
4426 : /* ImportFromUrl() */
4427 : /************************************************************************/
4428 :
4429 : /**
4430 : * \brief Set spatial reference from a URL.
4431 : *
4432 : * This method will download the spatial reference at a given URL and
4433 : * feed it into SetFromUserInput for you.
4434 : *
4435 : * This method does the same thing as the OSRImportFromUrl() function.
4436 : *
4437 : * @param pszUrl text definition to try to deduce SRS from.
4438 : *
4439 : * @return OGRERR_NONE on success, or an error code with the curl
4440 : * error message if it is unable to download data.
4441 : */
4442 :
4443 5 : OGRErr OGRSpatialReference::importFromUrl(const char *pszUrl)
4444 :
4445 : {
4446 10 : TAKE_OPTIONAL_LOCK();
4447 :
4448 5 : if (!STARTS_WITH_CI(pszUrl, "http://") &&
4449 3 : !STARTS_WITH_CI(pszUrl, "https://"))
4450 : {
4451 2 : CPLError(CE_Failure, CPLE_AppDefined,
4452 : "The given string is not recognized as a URL"
4453 : "starting with 'http://' -- %s",
4454 : pszUrl);
4455 2 : return OGRERR_FAILURE;
4456 : }
4457 :
4458 : /* -------------------------------------------------------------------- */
4459 : /* Fetch the result. */
4460 : /* -------------------------------------------------------------------- */
4461 3 : CPLErrorReset();
4462 :
4463 6 : std::string osUrl(pszUrl);
4464 : // We have historically supported "http://spatialreference.org/ref/AUTHNAME/CODE/"
4465 : // as a valid URL since we used a "Accept: application/x-ogcwkt" header
4466 : // to query WKT. To allow a static server to be used, rather append a
4467 : // "ogcwkt/" suffix.
4468 2 : for (const char *pszPrefix : {"https://spatialreference.org/ref/",
4469 5 : "http://spatialreference.org/ref/"})
4470 : {
4471 5 : if (STARTS_WITH(pszUrl, pszPrefix))
4472 : {
4473 : const CPLStringList aosTokens(
4474 6 : CSLTokenizeString2(pszUrl + strlen(pszPrefix), "/", 0));
4475 3 : if (aosTokens.size() == 2)
4476 : {
4477 2 : osUrl = "https://spatialreference.org/ref/";
4478 2 : osUrl += aosTokens[0]; // authority
4479 2 : osUrl += '/';
4480 2 : osUrl += aosTokens[1]; // code
4481 2 : osUrl += "/ogcwkt/";
4482 : }
4483 3 : break;
4484 : }
4485 : }
4486 :
4487 3 : const char *pszTimeout = "TIMEOUT=10";
4488 3 : char *apszOptions[] = {const_cast<char *>(pszTimeout), nullptr};
4489 :
4490 3 : CPLHTTPResult *psResult = CPLHTTPFetch(osUrl.c_str(), apszOptions);
4491 :
4492 : /* -------------------------------------------------------------------- */
4493 : /* Try to handle errors. */
4494 : /* -------------------------------------------------------------------- */
4495 :
4496 3 : if (psResult == nullptr)
4497 0 : return OGRERR_FAILURE;
4498 6 : if (psResult->nDataLen == 0 || CPLGetLastErrorNo() != 0 ||
4499 3 : psResult->pabyData == nullptr)
4500 : {
4501 0 : if (CPLGetLastErrorNo() == 0)
4502 : {
4503 0 : CPLError(CE_Failure, CPLE_AppDefined,
4504 : "No data was returned from the given URL");
4505 : }
4506 0 : CPLHTTPDestroyResult(psResult);
4507 0 : return OGRERR_FAILURE;
4508 : }
4509 :
4510 3 : if (psResult->nStatus != 0)
4511 : {
4512 0 : CPLError(CE_Failure, CPLE_AppDefined, "Curl reports error: %d: %s",
4513 : psResult->nStatus, psResult->pszErrBuf);
4514 0 : CPLHTTPDestroyResult(psResult);
4515 0 : return OGRERR_FAILURE;
4516 : }
4517 :
4518 3 : const char *pszData = reinterpret_cast<const char *>(psResult->pabyData);
4519 3 : if (STARTS_WITH_CI(pszData, "http://") ||
4520 3 : STARTS_WITH_CI(pszData, "https://"))
4521 : {
4522 0 : CPLError(CE_Failure, CPLE_AppDefined,
4523 : "The data that was downloaded also starts with 'http://' "
4524 : "and cannot be passed into SetFromUserInput. Is this "
4525 : "really a spatial reference definition? ");
4526 0 : CPLHTTPDestroyResult(psResult);
4527 0 : return OGRERR_FAILURE;
4528 : }
4529 3 : if (OGRERR_NONE != SetFromUserInput(pszData))
4530 : {
4531 0 : CPLHTTPDestroyResult(psResult);
4532 0 : return OGRERR_FAILURE;
4533 : }
4534 :
4535 3 : CPLHTTPDestroyResult(psResult);
4536 3 : return OGRERR_NONE;
4537 : }
4538 :
4539 : /************************************************************************/
4540 : /* OSRimportFromUrl() */
4541 : /************************************************************************/
4542 :
4543 : /**
4544 : * \brief Set spatial reference from a URL.
4545 : *
4546 : * This function is the same as OGRSpatialReference::importFromUrl()
4547 : */
4548 3 : OGRErr OSRImportFromUrl(OGRSpatialReferenceH hSRS, const char *pszUrl)
4549 :
4550 : {
4551 3 : VALIDATE_POINTER1(hSRS, "OSRImportFromUrl", OGRERR_FAILURE);
4552 :
4553 3 : return ToPointer(hSRS)->importFromUrl(pszUrl);
4554 : }
4555 :
4556 : /************************************************************************/
4557 : /* importFromURNPart() */
4558 : /************************************************************************/
4559 3828 : OGRErr OGRSpatialReference::importFromURNPart(const char *pszAuthority,
4560 : const char *pszCode,
4561 : const char *pszURN)
4562 : {
4563 : #if PROJ_AT_LEAST_VERSION(8, 1, 0)
4564 : (void)this;
4565 : (void)pszAuthority;
4566 : (void)pszCode;
4567 : (void)pszURN;
4568 : return OGRERR_FAILURE;
4569 : #else
4570 : /* -------------------------------------------------------------------- */
4571 : /* Is this an EPSG code? Note that we import it with EPSG */
4572 : /* preferred axis ordering for geographic coordinate systems. */
4573 : /* -------------------------------------------------------------------- */
4574 3828 : if (STARTS_WITH_CI(pszAuthority, "EPSG"))
4575 3526 : return importFromEPSGA(atoi(pszCode));
4576 :
4577 : /* -------------------------------------------------------------------- */
4578 : /* Is this an IAU code? Lets try for the IAU2000 dictionary. */
4579 : /* -------------------------------------------------------------------- */
4580 302 : if (STARTS_WITH_CI(pszAuthority, "IAU"))
4581 0 : return importFromDict("IAU2000.wkt", pszCode);
4582 :
4583 : /* -------------------------------------------------------------------- */
4584 : /* Is this an OGC code? */
4585 : /* -------------------------------------------------------------------- */
4586 302 : if (!STARTS_WITH_CI(pszAuthority, "OGC"))
4587 : {
4588 1 : CPLError(CE_Failure, CPLE_AppDefined,
4589 : "URN %s has unrecognized authority.", pszURN);
4590 1 : return OGRERR_FAILURE;
4591 : }
4592 :
4593 301 : if (STARTS_WITH_CI(pszCode, "CRS84"))
4594 289 : return SetWellKnownGeogCS(pszCode);
4595 12 : else if (STARTS_WITH_CI(pszCode, "CRS83"))
4596 0 : return SetWellKnownGeogCS(pszCode);
4597 12 : else if (STARTS_WITH_CI(pszCode, "CRS27"))
4598 0 : return SetWellKnownGeogCS(pszCode);
4599 12 : else if (STARTS_WITH_CI(pszCode, "84")) // urn:ogc:def:crs:OGC:2:84
4600 10 : return SetWellKnownGeogCS("CRS84");
4601 :
4602 : /* -------------------------------------------------------------------- */
4603 : /* Handle auto codes. We need to convert from format */
4604 : /* AUTO42001:99:8888 to format AUTO:42001,99,8888. */
4605 : /* -------------------------------------------------------------------- */
4606 2 : else if (STARTS_WITH_CI(pszCode, "AUTO"))
4607 : {
4608 2 : char szWMSAuto[100] = {'\0'};
4609 :
4610 2 : if (strlen(pszCode) > sizeof(szWMSAuto) - 2)
4611 0 : return OGRERR_FAILURE;
4612 :
4613 2 : snprintf(szWMSAuto, sizeof(szWMSAuto), "AUTO:%s", pszCode + 4);
4614 28 : for (int i = 5; szWMSAuto[i] != '\0'; i++)
4615 : {
4616 26 : if (szWMSAuto[i] == ':')
4617 4 : szWMSAuto[i] = ',';
4618 : }
4619 :
4620 2 : return importFromWMSAUTO(szWMSAuto);
4621 : }
4622 :
4623 : /* -------------------------------------------------------------------- */
4624 : /* Not a recognise OGC item. */
4625 : /* -------------------------------------------------------------------- */
4626 0 : CPLError(CE_Failure, CPLE_AppDefined, "URN %s value not supported.",
4627 : pszURN);
4628 :
4629 0 : return OGRERR_FAILURE;
4630 : #endif
4631 : }
4632 :
4633 : /************************************************************************/
4634 : /* importFromURN() */
4635 : /* */
4636 : /* See OGC recommendation paper 06-023r1 or later for details. */
4637 : /************************************************************************/
4638 :
4639 : /**
4640 : * \brief Initialize from OGC URN.
4641 : *
4642 : * Initializes this spatial reference from a coordinate system defined
4643 : * by an OGC URN prefixed with "urn:ogc:def:crs:" per recommendation
4644 : * paper 06-023r1. Currently EPSG and OGC authority values are supported,
4645 : * including OGC auto codes, but not including CRS1 or CRS88 (NAVD88).
4646 : *
4647 : * This method is also support through SetFromUserInput() which can
4648 : * normally be used for URNs.
4649 : *
4650 : * @param pszURN the urn string.
4651 : *
4652 : * @return OGRERR_NONE on success or an error code.
4653 : */
4654 :
4655 792 : OGRErr OGRSpatialReference::importFromURN(const char *pszURN)
4656 :
4657 : {
4658 792 : constexpr const char *EPSG_URN_CRS_PREFIX = "urn:ogc:def:crs:EPSG::";
4659 1496 : if (STARTS_WITH(pszURN, EPSG_URN_CRS_PREFIX) &&
4660 704 : CPLGetValueType(pszURN + strlen(EPSG_URN_CRS_PREFIX)) ==
4661 : CPL_VALUE_INTEGER)
4662 : {
4663 702 : return importFromEPSG(atoi(pszURN + strlen(EPSG_URN_CRS_PREFIX)));
4664 : }
4665 :
4666 180 : TAKE_OPTIONAL_LOCK();
4667 :
4668 : #if PROJ_AT_LEAST_VERSION(8, 1, 0)
4669 :
4670 : // PROJ 8.2.0 has support for IAU codes now.
4671 : #if !PROJ_AT_LEAST_VERSION(8, 2, 0)
4672 : /* -------------------------------------------------------------------- */
4673 : /* Is this an IAU code? Lets try for the IAU2000 dictionary. */
4674 : /* -------------------------------------------------------------------- */
4675 : const char *pszIAU = strstr(pszURN, "IAU");
4676 : if (pszIAU)
4677 : {
4678 : const char *pszCode = strchr(pszIAU, ':');
4679 : if (pszCode)
4680 : {
4681 : ++pszCode;
4682 : if (*pszCode == ':')
4683 : ++pszCode;
4684 : return importFromDict("IAU2000.wkt", pszCode);
4685 : }
4686 : }
4687 : #endif
4688 :
4689 : if (strlen(pszURN) >= 1000)
4690 : {
4691 : CPLError(CE_Failure, CPLE_AppDefined, "Too long input string");
4692 : return OGRERR_CORRUPT_DATA;
4693 : }
4694 : auto obj = proj_create(d->getPROJContext(), pszURN);
4695 : if (!obj)
4696 : {
4697 : return OGRERR_FAILURE;
4698 : }
4699 : Clear();
4700 : d->setPjCRS(obj);
4701 : return OGRERR_NONE;
4702 : #else
4703 90 : const char *pszCur = nullptr;
4704 :
4705 90 : if (STARTS_WITH_CI(pszURN, "urn:ogc:def:crs:"))
4706 25 : pszCur = pszURN + 16;
4707 65 : else if (STARTS_WITH_CI(pszURN, "urn:ogc:def:crs,crs:"))
4708 1 : pszCur = pszURN + 20;
4709 64 : else if (STARTS_WITH_CI(pszURN, "urn:x-ogc:def:crs:"))
4710 62 : pszCur = pszURN + 18;
4711 2 : else if (STARTS_WITH_CI(pszURN, "urn:opengis:crs:"))
4712 0 : pszCur = pszURN + 16;
4713 2 : else if (STARTS_WITH_CI(pszURN, "urn:opengis:def:crs:"))
4714 0 : pszCur = pszURN + 20;
4715 : else
4716 : {
4717 2 : CPLError(CE_Failure, CPLE_AppDefined, "URN %s not a supported format.",
4718 : pszURN);
4719 2 : return OGRERR_FAILURE;
4720 : }
4721 :
4722 : /* -------------------------------------------------------------------- */
4723 : /* Clear any existing definition. */
4724 : /* -------------------------------------------------------------------- */
4725 88 : Clear();
4726 :
4727 : /* -------------------------------------------------------------------- */
4728 : /* Find code (ignoring version) out of string like: */
4729 : /* */
4730 : /* authority:[version]:code */
4731 : /* -------------------------------------------------------------------- */
4732 88 : const char *pszAuthority = pszCur;
4733 :
4734 : // skip authority
4735 421 : while (*pszCur != ':' && *pszCur)
4736 333 : pszCur++;
4737 88 : if (*pszCur == ':')
4738 88 : pszCur++;
4739 :
4740 : // skip version
4741 88 : const char *pszBeforeVersion = pszCur;
4742 398 : while (*pszCur != ':' && *pszCur)
4743 310 : pszCur++;
4744 88 : if (*pszCur == ':')
4745 60 : pszCur++;
4746 : else
4747 : // We come here in the case, the content to parse is authority:code
4748 : // (instead of authority::code) which is probably illegal according to
4749 : // http://www.opengeospatial.org/ogcUrnPolicy but such content is found
4750 : // for example in what is returned by GeoServer.
4751 28 : pszCur = pszBeforeVersion;
4752 :
4753 88 : const char *pszCode = pszCur;
4754 :
4755 88 : const char *pszComma = strchr(pszCur, ',');
4756 88 : if (pszComma == nullptr)
4757 87 : return importFromURNPart(pszAuthority, pszCode, pszURN);
4758 :
4759 : // There's a second part with the vertical SRS.
4760 1 : pszCur = pszComma + 1;
4761 1 : if (!STARTS_WITH(pszCur, "crs:"))
4762 : {
4763 0 : CPLError(CE_Failure, CPLE_AppDefined, "URN %s not a supported format.",
4764 : pszURN);
4765 0 : return OGRERR_FAILURE;
4766 : }
4767 :
4768 1 : pszCur += 4;
4769 :
4770 1 : char *pszFirstCode = CPLStrdup(pszCode);
4771 1 : pszFirstCode[pszComma - pszCode] = '\0';
4772 1 : OGRErr eStatus = importFromURNPart(pszAuthority, pszFirstCode, pszURN);
4773 1 : CPLFree(pszFirstCode);
4774 :
4775 : // Do we want to turn this into a compound definition
4776 : // with a vertical datum?
4777 1 : if (eStatus != OGRERR_NONE)
4778 0 : return eStatus;
4779 :
4780 : /* -------------------------------------------------------------------- */
4781 : /* Find code (ignoring version) out of string like: */
4782 : /* */
4783 : /* authority:[version]:code */
4784 : /* -------------------------------------------------------------------- */
4785 1 : pszAuthority = pszCur;
4786 :
4787 : // skip authority
4788 5 : while (*pszCur != ':' && *pszCur)
4789 4 : pszCur++;
4790 1 : if (*pszCur == ':')
4791 1 : pszCur++;
4792 :
4793 : // skip version
4794 1 : pszBeforeVersion = pszCur;
4795 1 : while (*pszCur != ':' && *pszCur)
4796 0 : pszCur++;
4797 1 : if (*pszCur == ':')
4798 1 : pszCur++;
4799 : else
4800 0 : pszCur = pszBeforeVersion;
4801 :
4802 1 : pszCode = pszCur;
4803 :
4804 2 : OGRSpatialReference oVertSRS;
4805 1 : eStatus = oVertSRS.importFromURNPart(pszAuthority, pszCode, pszURN);
4806 1 : if (eStatus == OGRERR_NONE)
4807 : {
4808 1 : OGRSpatialReference oHorizSRS(*this);
4809 :
4810 1 : Clear();
4811 :
4812 1 : oHorizSRS.d->refreshProjObj();
4813 1 : oVertSRS.d->refreshProjObj();
4814 1 : if (!oHorizSRS.d->m_pj_crs || !oVertSRS.d->m_pj_crs)
4815 0 : return OGRERR_FAILURE;
4816 :
4817 1 : const char *pszHorizName = proj_get_name(oHorizSRS.d->m_pj_crs);
4818 1 : const char *pszVertName = proj_get_name(oVertSRS.d->m_pj_crs);
4819 :
4820 2 : CPLString osName = pszHorizName ? pszHorizName : "";
4821 1 : osName += " + ";
4822 1 : osName += pszVertName ? pszVertName : "";
4823 :
4824 1 : SetCompoundCS(osName, &oHorizSRS, &oVertSRS);
4825 : }
4826 :
4827 1 : return eStatus;
4828 : #endif
4829 : }
4830 :
4831 : /************************************************************************/
4832 : /* importFromCRSURL() */
4833 : /* */
4834 : /* See OGC Best Practice document 11-135 for details. */
4835 : /************************************************************************/
4836 :
4837 : /**
4838 : * \brief Initialize from OGC URL.
4839 : *
4840 : * Initializes this spatial reference from a coordinate system defined
4841 : * by an OGC URL prefixed with "http://opengis.net/def/crs" per best practice
4842 : * paper 11-135. Currently EPSG and OGC authority values are supported,
4843 : * including OGC auto codes, but not including CRS1 or CRS88 (NAVD88).
4844 : *
4845 : * This method is also supported through SetFromUserInput() which can
4846 : * normally be used for URLs.
4847 : *
4848 : * @param pszURL the URL string.
4849 : *
4850 : * @return OGRERR_NONE on success or an error code.
4851 : */
4852 :
4853 3740 : OGRErr OGRSpatialReference::importFromCRSURL(const char *pszURL)
4854 :
4855 : {
4856 7480 : TAKE_OPTIONAL_LOCK();
4857 :
4858 : #if PROJ_AT_LEAST_VERSION(8, 1, 0)
4859 : if (strlen(pszURL) >= 10000)
4860 : {
4861 : CPLError(CE_Failure, CPLE_AppDefined, "Too long input string");
4862 : return OGRERR_CORRUPT_DATA;
4863 : }
4864 :
4865 : PJ *obj;
4866 : #if !PROJ_AT_LEAST_VERSION(9, 2, 0)
4867 : if (STARTS_WITH(pszURL, "http://www.opengis.net/def/crs/IAU/0/"))
4868 : {
4869 : obj = proj_create(
4870 : d->getPROJContext(),
4871 : CPLSPrintf("IAU:%s",
4872 : pszURL +
4873 : strlen("http://www.opengis.net/def/crs/IAU/0/")));
4874 : }
4875 : else
4876 : #endif
4877 : {
4878 : obj = proj_create(d->getPROJContext(), pszURL);
4879 : }
4880 : if (!obj)
4881 : {
4882 : return OGRERR_FAILURE;
4883 : }
4884 : Clear();
4885 : d->setPjCRS(obj);
4886 : return OGRERR_NONE;
4887 : #else
4888 3740 : const char *pszCur = nullptr;
4889 :
4890 3740 : if (STARTS_WITH_CI(pszURL, "http://opengis.net/def/crs"))
4891 2 : pszCur = pszURL + 26;
4892 3738 : else if (STARTS_WITH_CI(pszURL, "https://opengis.net/def/crs"))
4893 1 : pszCur = pszURL + 27;
4894 3737 : else if (STARTS_WITH_CI(pszURL, "http://www.opengis.net/def/crs"))
4895 3736 : pszCur = pszURL + 30;
4896 1 : else if (STARTS_WITH_CI(pszURL, "https://www.opengis.net/def/crs"))
4897 1 : pszCur = pszURL + 31;
4898 0 : else if (STARTS_WITH_CI(pszURL, "www.opengis.net/def/crs"))
4899 0 : pszCur = pszURL + 23;
4900 : else
4901 : {
4902 0 : CPLError(CE_Failure, CPLE_AppDefined, "URL %s not a supported format.",
4903 : pszURL);
4904 0 : return OGRERR_FAILURE;
4905 : }
4906 :
4907 3740 : if (*pszCur == '\0')
4908 : {
4909 0 : CPLError(CE_Failure, CPLE_AppDefined, "URL %s malformed.", pszURL);
4910 0 : return OGRERR_FAILURE;
4911 : }
4912 :
4913 : /* -------------------------------------------------------------------- */
4914 : /* Clear any existing definition. */
4915 : /* -------------------------------------------------------------------- */
4916 3740 : Clear();
4917 :
4918 3740 : if (STARTS_WITH_CI(pszCur, "-compound?1="))
4919 : {
4920 : /* --------------------------------------------------------------------
4921 : */
4922 : /* It's a compound CRS, of the form: */
4923 : /* */
4924 : /* http://opengis.net/def/crs-compound?1=URL1&2=URL2&3=URL3&.. */
4925 : /* --------------------------------------------------------------------
4926 : */
4927 1 : pszCur += 12;
4928 :
4929 : // Extract each component CRS URL.
4930 1 : int iComponentUrl = 2;
4931 :
4932 2 : CPLString osName = "";
4933 1 : Clear();
4934 :
4935 3 : while (iComponentUrl != -1)
4936 : {
4937 2 : char searchStr[15] = {};
4938 2 : snprintf(searchStr, sizeof(searchStr), "&%d=", iComponentUrl);
4939 :
4940 2 : const char *pszUrlEnd = strstr(pszCur, searchStr);
4941 :
4942 : // Figure out the next component URL.
4943 2 : char *pszComponentUrl = nullptr;
4944 :
4945 2 : if (pszUrlEnd)
4946 : {
4947 1 : size_t nLen = pszUrlEnd - pszCur;
4948 1 : pszComponentUrl = static_cast<char *>(CPLMalloc(nLen + 1));
4949 1 : strncpy(pszComponentUrl, pszCur, nLen);
4950 1 : pszComponentUrl[nLen] = '\0';
4951 :
4952 1 : ++iComponentUrl;
4953 1 : pszCur += nLen + strlen(searchStr);
4954 : }
4955 : else
4956 : {
4957 1 : if (iComponentUrl == 2)
4958 : {
4959 0 : CPLError(CE_Failure, CPLE_AppDefined,
4960 : "Compound CRS URLs must have at least two "
4961 : "component CRSs.");
4962 0 : return OGRERR_FAILURE;
4963 : }
4964 : else
4965 : {
4966 1 : pszComponentUrl = CPLStrdup(pszCur);
4967 : // no more components
4968 1 : iComponentUrl = -1;
4969 : }
4970 : }
4971 :
4972 2 : OGRSpatialReference oComponentSRS;
4973 2 : OGRErr eStatus = oComponentSRS.importFromCRSURL(pszComponentUrl);
4974 :
4975 2 : CPLFree(pszComponentUrl);
4976 2 : pszComponentUrl = nullptr;
4977 :
4978 2 : if (eStatus == OGRERR_NONE)
4979 : {
4980 2 : if (osName.length() != 0)
4981 : {
4982 1 : osName += " + ";
4983 : }
4984 2 : osName += oComponentSRS.GetRoot()->GetValue();
4985 2 : SetNode("COMPD_CS", osName);
4986 2 : GetRoot()->AddChild(oComponentSRS.GetRoot()->Clone());
4987 : }
4988 : else
4989 0 : return eStatus;
4990 : }
4991 :
4992 1 : return OGRERR_NONE;
4993 : }
4994 :
4995 : /* -------------------------------------------------------------------- */
4996 : /* It's a normal CRS URL, of the form: */
4997 : /* */
4998 : /* http://opengis.net/def/crs/AUTHORITY/VERSION/CODE */
4999 : /* -------------------------------------------------------------------- */
5000 3739 : ++pszCur;
5001 3739 : const char *pszAuthority = pszCur;
5002 :
5003 : // skip authority
5004 118409 : while (*pszCur != '/' && *pszCur)
5005 114670 : pszCur++;
5006 3739 : if (*pszCur == '/')
5007 3738 : pszCur++;
5008 :
5009 : // skip version
5010 8041 : while (*pszCur != '/' && *pszCur)
5011 4302 : pszCur++;
5012 3739 : if (*pszCur == '/')
5013 3738 : pszCur++;
5014 :
5015 3739 : const char *pszCode = pszCur;
5016 :
5017 3739 : return importFromURNPart(pszAuthority, pszCode, pszURL);
5018 : #endif
5019 : }
5020 :
5021 : /************************************************************************/
5022 : /* importFromWMSAUTO() */
5023 : /************************************************************************/
5024 :
5025 : /**
5026 : * \brief Initialize from WMSAUTO string.
5027 : *
5028 : * Note that the WMS 1.3 specification does not include the
5029 : * units code, while apparently earlier specs do. We try to
5030 : * guess around this.
5031 : *
5032 : * @param pszDefinition the WMSAUTO string
5033 : *
5034 : * @return OGRERR_NONE on success or an error code.
5035 : */
5036 3 : OGRErr OGRSpatialReference::importFromWMSAUTO(const char *pszDefinition)
5037 :
5038 : {
5039 6 : TAKE_OPTIONAL_LOCK();
5040 :
5041 : #if PROJ_AT_LEAST_VERSION(8, 1, 0)
5042 : if (strlen(pszDefinition) >= 10000)
5043 : {
5044 : CPLError(CE_Failure, CPLE_AppDefined, "Too long input string");
5045 : return OGRERR_CORRUPT_DATA;
5046 : }
5047 :
5048 : auto obj = proj_create(d->getPROJContext(), pszDefinition);
5049 : if (!obj)
5050 : {
5051 : return OGRERR_FAILURE;
5052 : }
5053 : Clear();
5054 : d->setPjCRS(obj);
5055 : return OGRERR_NONE;
5056 : #else
5057 : int nProjId, nUnitsId;
5058 3 : double dfRefLong, dfRefLat = 0.0;
5059 :
5060 : /* -------------------------------------------------------------------- */
5061 : /* Tokenize */
5062 : /* -------------------------------------------------------------------- */
5063 3 : if (STARTS_WITH_CI(pszDefinition, "AUTO:"))
5064 3 : pszDefinition += 5;
5065 :
5066 : char **papszTokens =
5067 3 : CSLTokenizeStringComplex(pszDefinition, ",", FALSE, TRUE);
5068 :
5069 3 : if (CSLCount(papszTokens) == 4)
5070 : {
5071 0 : nProjId = atoi(papszTokens[0]);
5072 0 : nUnitsId = atoi(papszTokens[1]);
5073 0 : dfRefLong = CPLAtof(papszTokens[2]);
5074 0 : dfRefLat = CPLAtof(papszTokens[3]);
5075 : }
5076 3 : else if (CSLCount(papszTokens) == 3 && atoi(papszTokens[0]) == 42005)
5077 : {
5078 0 : nProjId = atoi(papszTokens[0]);
5079 0 : nUnitsId = atoi(papszTokens[1]);
5080 0 : dfRefLong = CPLAtof(papszTokens[2]);
5081 0 : dfRefLat = 0.0;
5082 : }
5083 3 : else if (CSLCount(papszTokens) == 3)
5084 : {
5085 2 : nProjId = atoi(papszTokens[0]);
5086 2 : nUnitsId = 9001;
5087 2 : dfRefLong = CPLAtof(papszTokens[1]);
5088 2 : dfRefLat = CPLAtof(papszTokens[2]);
5089 : }
5090 1 : else if (CSLCount(papszTokens) == 2 && atoi(papszTokens[0]) == 42005)
5091 : {
5092 0 : nProjId = atoi(papszTokens[0]);
5093 0 : nUnitsId = 9001;
5094 0 : dfRefLong = CPLAtof(papszTokens[1]);
5095 : }
5096 : else
5097 : {
5098 1 : CSLDestroy(papszTokens);
5099 1 : CPLError(CE_Failure, CPLE_AppDefined,
5100 : "AUTO projection has wrong number of arguments, expected\n"
5101 : "AUTO:proj_id,units_id,ref_long,ref_lat or"
5102 : "AUTO:proj_id,ref_long,ref_lat");
5103 1 : return OGRERR_FAILURE;
5104 : }
5105 :
5106 2 : CSLDestroy(papszTokens);
5107 2 : papszTokens = nullptr;
5108 :
5109 : /* -------------------------------------------------------------------- */
5110 : /* Build coordsys. */
5111 : /* -------------------------------------------------------------------- */
5112 2 : Clear();
5113 :
5114 : /* -------------------------------------------------------------------- */
5115 : /* Set WGS84. */
5116 : /* -------------------------------------------------------------------- */
5117 2 : SetWellKnownGeogCS("WGS84");
5118 :
5119 2 : switch (nProjId)
5120 : {
5121 2 : case 42001: // Auto UTM
5122 2 : SetUTM(static_cast<int>(floor((dfRefLong + 180.0) / 6.0)) + 1,
5123 : dfRefLat >= 0.0);
5124 2 : break;
5125 :
5126 0 : case 42002: // Auto TM (strangely very UTM-like).
5127 0 : SetTM(0, dfRefLong, 0.9996, 500000.0,
5128 : (dfRefLat >= 0.0) ? 0.0 : 10000000.0);
5129 0 : break;
5130 :
5131 0 : case 42003: // Auto Orthographic.
5132 0 : SetOrthographic(dfRefLat, dfRefLong, 0.0, 0.0);
5133 0 : break;
5134 :
5135 0 : case 42004: // Auto Equirectangular
5136 0 : SetEquirectangular(dfRefLat, dfRefLong, 0.0, 0.0);
5137 0 : break;
5138 :
5139 0 : case 42005:
5140 0 : SetMollweide(dfRefLong, 0.0, 0.0);
5141 0 : break;
5142 :
5143 0 : default:
5144 0 : CPLError(CE_Failure, CPLE_AppDefined,
5145 : "Unsupported projection id in importFromWMSAUTO(): %d",
5146 : nProjId);
5147 0 : return OGRERR_FAILURE;
5148 : }
5149 :
5150 : /* -------------------------------------------------------------------- */
5151 : /* Set units. */
5152 : /* -------------------------------------------------------------------- */
5153 :
5154 2 : switch (nUnitsId)
5155 : {
5156 2 : case 9001:
5157 2 : SetTargetLinearUnits(nullptr, SRS_UL_METER, 1.0, "EPSG", "9001");
5158 2 : break;
5159 :
5160 0 : case 9002:
5161 0 : SetTargetLinearUnits(nullptr, "Foot", 0.3048, "EPSG", "9002");
5162 0 : break;
5163 :
5164 0 : case 9003:
5165 0 : SetTargetLinearUnits(nullptr, "US survey foot",
5166 : CPLAtof(SRS_UL_US_FOOT_CONV), "EPSG", "9003");
5167 0 : break;
5168 :
5169 0 : default:
5170 0 : CPLError(CE_Failure, CPLE_AppDefined,
5171 : "Unsupported units code (%d).", nUnitsId);
5172 0 : return OGRERR_FAILURE;
5173 : break;
5174 : }
5175 :
5176 2 : return OGRERR_NONE;
5177 : #endif
5178 : }
5179 :
5180 : /************************************************************************/
5181 : /* GetSemiMajor() */
5182 : /************************************************************************/
5183 :
5184 : /**
5185 : * \brief Get spheroid semi major axis (in metres starting with GDAL 3.0)
5186 : *
5187 : * This method does the same thing as the C function OSRGetSemiMajor().
5188 : *
5189 : * @param pnErr if non-NULL set to OGRERR_FAILURE if semi major axis
5190 : * can be found.
5191 : *
5192 : * @return semi-major axis, or SRS_WGS84_SEMIMAJOR if it can't be found.
5193 : */
5194 :
5195 6144 : double OGRSpatialReference::GetSemiMajor(OGRErr *pnErr) const
5196 :
5197 : {
5198 12288 : TAKE_OPTIONAL_LOCK();
5199 :
5200 6144 : if (pnErr != nullptr)
5201 3305 : *pnErr = OGRERR_FAILURE;
5202 :
5203 6144 : d->refreshProjObj();
5204 6144 : if (!d->m_pj_crs)
5205 111 : return SRS_WGS84_SEMIMAJOR;
5206 :
5207 6033 : auto ellps = proj_get_ellipsoid(d->getPROJContext(), d->m_pj_crs);
5208 6033 : if (!ellps)
5209 5 : return SRS_WGS84_SEMIMAJOR;
5210 :
5211 6028 : double dfSemiMajor = 0.0;
5212 6028 : proj_ellipsoid_get_parameters(d->getPROJContext(), ellps, &dfSemiMajor,
5213 : nullptr, nullptr, nullptr);
5214 6028 : proj_destroy(ellps);
5215 :
5216 6028 : if (dfSemiMajor > 0)
5217 : {
5218 6028 : if (pnErr != nullptr)
5219 3191 : *pnErr = OGRERR_NONE;
5220 6028 : return dfSemiMajor;
5221 : }
5222 :
5223 0 : return SRS_WGS84_SEMIMAJOR;
5224 : }
5225 :
5226 : /************************************************************************/
5227 : /* OSRGetSemiMajor() */
5228 : /************************************************************************/
5229 :
5230 : /**
5231 : * \brief Get spheroid semi major axis.
5232 : *
5233 : * This function is the same as OGRSpatialReference::GetSemiMajor()
5234 : */
5235 76 : double OSRGetSemiMajor(OGRSpatialReferenceH hSRS, OGRErr *pnErr)
5236 :
5237 : {
5238 76 : VALIDATE_POINTER1(hSRS, "OSRGetSemiMajor", 0);
5239 :
5240 76 : return ToPointer(hSRS)->GetSemiMajor(pnErr);
5241 : }
5242 :
5243 : /************************************************************************/
5244 : /* GetInvFlattening() */
5245 : /************************************************************************/
5246 :
5247 : /**
5248 : * \brief Get spheroid inverse flattening.
5249 : *
5250 : * This method does the same thing as the C function OSRGetInvFlattening().
5251 : *
5252 : * @param pnErr if non-NULL set to OGRERR_FAILURE if no inverse flattening
5253 : * can be found.
5254 : *
5255 : * @return inverse flattening, or SRS_WGS84_INVFLATTENING if it can't be found.
5256 : */
5257 :
5258 4269 : double OGRSpatialReference::GetInvFlattening(OGRErr *pnErr) const
5259 :
5260 : {
5261 8538 : TAKE_OPTIONAL_LOCK();
5262 :
5263 4269 : if (pnErr != nullptr)
5264 3210 : *pnErr = OGRERR_FAILURE;
5265 :
5266 4269 : d->refreshProjObj();
5267 4269 : if (!d->m_pj_crs)
5268 111 : return SRS_WGS84_INVFLATTENING;
5269 :
5270 4158 : auto ellps = proj_get_ellipsoid(d->getPROJContext(), d->m_pj_crs);
5271 4158 : if (!ellps)
5272 2 : return SRS_WGS84_INVFLATTENING;
5273 :
5274 4156 : double dfInvFlattening = -1.0;
5275 4156 : proj_ellipsoid_get_parameters(d->getPROJContext(), ellps, nullptr, nullptr,
5276 : nullptr, &dfInvFlattening);
5277 4156 : proj_destroy(ellps);
5278 :
5279 4156 : if (dfInvFlattening >= 0.0)
5280 : {
5281 4156 : if (pnErr != nullptr)
5282 3099 : *pnErr = OGRERR_NONE;
5283 4156 : return dfInvFlattening;
5284 : }
5285 :
5286 0 : return SRS_WGS84_INVFLATTENING;
5287 : }
5288 :
5289 : /************************************************************************/
5290 : /* OSRGetInvFlattening() */
5291 : /************************************************************************/
5292 :
5293 : /**
5294 : * \brief Get spheroid inverse flattening.
5295 : *
5296 : * This function is the same as OGRSpatialReference::GetInvFlattening()
5297 : */
5298 10 : double OSRGetInvFlattening(OGRSpatialReferenceH hSRS, OGRErr *pnErr)
5299 :
5300 : {
5301 10 : VALIDATE_POINTER1(hSRS, "OSRGetInvFlattening", 0);
5302 :
5303 10 : return ToPointer(hSRS)->GetInvFlattening(pnErr);
5304 : }
5305 :
5306 : /************************************************************************/
5307 : /* GetEccentricity() */
5308 : /************************************************************************/
5309 :
5310 : /**
5311 : * \brief Get spheroid eccentricity
5312 : *
5313 : * @return eccentricity (or -1 in case of error)
5314 : * @since GDAL 2.3
5315 : */
5316 :
5317 0 : double OGRSpatialReference::GetEccentricity() const
5318 :
5319 : {
5320 0 : OGRErr eErr = OGRERR_NONE;
5321 0 : const double dfInvFlattening = GetInvFlattening(&eErr);
5322 0 : if (eErr != OGRERR_NONE)
5323 : {
5324 0 : return -1.0;
5325 : }
5326 0 : if (dfInvFlattening == 0.0)
5327 0 : return 0.0;
5328 0 : if (dfInvFlattening < 0.5)
5329 0 : return -1.0;
5330 0 : return sqrt(2.0 / dfInvFlattening -
5331 0 : 1.0 / (dfInvFlattening * dfInvFlattening));
5332 : }
5333 :
5334 : /************************************************************************/
5335 : /* GetSquaredEccentricity() */
5336 : /************************************************************************/
5337 :
5338 : /**
5339 : * \brief Get spheroid squared eccentricity
5340 : *
5341 : * @return squared eccentricity (or -1 in case of error)
5342 : * @since GDAL 2.3
5343 : */
5344 :
5345 0 : double OGRSpatialReference::GetSquaredEccentricity() const
5346 :
5347 : {
5348 0 : OGRErr eErr = OGRERR_NONE;
5349 0 : const double dfInvFlattening = GetInvFlattening(&eErr);
5350 0 : if (eErr != OGRERR_NONE)
5351 : {
5352 0 : return -1.0;
5353 : }
5354 0 : if (dfInvFlattening == 0.0)
5355 0 : return 0.0;
5356 0 : if (dfInvFlattening < 0.5)
5357 0 : return -1.0;
5358 0 : return 2.0 / dfInvFlattening - 1.0 / (dfInvFlattening * dfInvFlattening);
5359 : }
5360 :
5361 : /************************************************************************/
5362 : /* GetSemiMinor() */
5363 : /************************************************************************/
5364 :
5365 : /**
5366 : * \brief Get spheroid semi minor axis.
5367 : *
5368 : * This method does the same thing as the C function OSRGetSemiMinor().
5369 : *
5370 : * @param pnErr if non-NULL set to OGRERR_FAILURE if semi minor axis
5371 : * can be found.
5372 : *
5373 : * @return semi-minor axis, or WGS84 semi minor if it can't be found.
5374 : */
5375 :
5376 642 : double OGRSpatialReference::GetSemiMinor(OGRErr *pnErr) const
5377 :
5378 : {
5379 642 : const double dfSemiMajor = GetSemiMajor(pnErr);
5380 642 : const double dfInvFlattening = GetInvFlattening(pnErr);
5381 :
5382 642 : return OSRCalcSemiMinorFromInvFlattening(dfSemiMajor, dfInvFlattening);
5383 : }
5384 :
5385 : /************************************************************************/
5386 : /* OSRGetSemiMinor() */
5387 : /************************************************************************/
5388 :
5389 : /**
5390 : * \brief Get spheroid semi minor axis.
5391 : *
5392 : * This function is the same as OGRSpatialReference::GetSemiMinor()
5393 : */
5394 4 : double OSRGetSemiMinor(OGRSpatialReferenceH hSRS, OGRErr *pnErr)
5395 :
5396 : {
5397 4 : VALIDATE_POINTER1(hSRS, "OSRGetSemiMinor", 0);
5398 :
5399 4 : return ToPointer(hSRS)->GetSemiMinor(pnErr);
5400 : }
5401 :
5402 : /************************************************************************/
5403 : /* SetLocalCS() */
5404 : /************************************************************************/
5405 :
5406 : /**
5407 : * \brief Set the user visible LOCAL_CS name.
5408 : *
5409 : * This method is the same as the C function OSRSetLocalCS().
5410 : *
5411 : * This method will ensure a LOCAL_CS node is created as the root,
5412 : * and set the provided name on it. It must be used before SetLinearUnits().
5413 : *
5414 : * @param pszName the user visible name to assign. Not used as a key.
5415 : *
5416 : * @return OGRERR_NONE on success.
5417 : */
5418 :
5419 2877 : OGRErr OGRSpatialReference::SetLocalCS(const char *pszName)
5420 :
5421 : {
5422 5754 : TAKE_OPTIONAL_LOCK();
5423 :
5424 2877 : if (d->m_pjType == PJ_TYPE_UNKNOWN ||
5425 0 : d->m_pjType == PJ_TYPE_ENGINEERING_CRS)
5426 : {
5427 2877 : d->setPjCRS(proj_create_engineering_crs(d->getPROJContext(), pszName));
5428 : }
5429 : else
5430 : {
5431 0 : CPLDebug("OGR",
5432 : "OGRSpatialReference::SetLocalCS(%s) failed. "
5433 : "It appears an incompatible object already exists.",
5434 : pszName);
5435 0 : return OGRERR_FAILURE;
5436 : }
5437 :
5438 2877 : return OGRERR_NONE;
5439 : }
5440 :
5441 : /************************************************************************/
5442 : /* OSRSetLocalCS() */
5443 : /************************************************************************/
5444 :
5445 : /**
5446 : * \brief Set the user visible LOCAL_CS name.
5447 : *
5448 : * This function is the same as OGRSpatialReference::SetLocalCS()
5449 : */
5450 1 : OGRErr OSRSetLocalCS(OGRSpatialReferenceH hSRS, const char *pszName)
5451 :
5452 : {
5453 1 : VALIDATE_POINTER1(hSRS, "OSRSetLocalCS", OGRERR_FAILURE);
5454 :
5455 1 : return ToPointer(hSRS)->SetLocalCS(pszName);
5456 : }
5457 :
5458 : /************************************************************************/
5459 : /* SetGeocCS() */
5460 : /************************************************************************/
5461 :
5462 : /**
5463 : * \brief Set the user visible GEOCCS name.
5464 : *
5465 : * This method is the same as the C function OSRSetGeocCS().
5466 :
5467 : * This method will ensure a GEOCCS node is created as the root,
5468 : * and set the provided name on it. If used on a GEOGCS coordinate system,
5469 : * the DATUM and PRIMEM nodes from the GEOGCS will be transferred over to
5470 : * the GEOGCS.
5471 : *
5472 : * @param pszName the user visible name to assign. Not used as a key.
5473 : *
5474 : * @return OGRERR_NONE on success.
5475 : *
5476 : * @since OGR 1.9.0
5477 : */
5478 :
5479 6 : OGRErr OGRSpatialReference::SetGeocCS(const char *pszName)
5480 :
5481 : {
5482 12 : TAKE_OPTIONAL_LOCK();
5483 :
5484 6 : OGRErr eErr = OGRERR_NONE;
5485 6 : d->refreshProjObj();
5486 6 : d->demoteFromBoundCRS();
5487 6 : if (d->m_pjType == PJ_TYPE_UNKNOWN)
5488 : {
5489 3 : d->setPjCRS(proj_create_geocentric_crs(
5490 : d->getPROJContext(), pszName, "World Geodetic System 1984",
5491 : "WGS 84", SRS_WGS84_SEMIMAJOR, SRS_WGS84_INVFLATTENING,
5492 : SRS_PM_GREENWICH, 0.0, SRS_UA_DEGREE, CPLAtof(SRS_UA_DEGREE_CONV),
5493 : "Metre", 1.0));
5494 : }
5495 3 : else if (d->m_pjType == PJ_TYPE_GEOCENTRIC_CRS)
5496 : {
5497 1 : d->setPjCRS(proj_alter_name(d->getPROJContext(), d->m_pj_crs, pszName));
5498 : }
5499 3 : else if (d->m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
5500 1 : d->m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS)
5501 : {
5502 1 : auto datum = proj_crs_get_datum(d->getPROJContext(), d->m_pj_crs);
5503 : #if PROJ_VERSION_MAJOR > 7 || \
5504 : (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
5505 : if (datum == nullptr)
5506 : {
5507 : datum =
5508 : proj_crs_get_datum_ensemble(d->getPROJContext(), d->m_pj_crs);
5509 : }
5510 : #endif
5511 1 : if (datum == nullptr)
5512 : {
5513 0 : d->undoDemoteFromBoundCRS();
5514 0 : return OGRERR_FAILURE;
5515 : }
5516 :
5517 1 : auto pj_crs = proj_create_geocentric_crs_from_datum(
5518 1 : d->getPROJContext(), proj_get_name(d->m_pj_crs), datum, nullptr,
5519 : 0.0);
5520 1 : d->setPjCRS(pj_crs);
5521 :
5522 1 : proj_destroy(datum);
5523 : }
5524 : else
5525 : {
5526 1 : CPLDebug("OGR",
5527 : "OGRSpatialReference::SetGeocCS(%s) failed. "
5528 : "It appears an incompatible object already exists.",
5529 : pszName);
5530 1 : eErr = OGRERR_FAILURE;
5531 : }
5532 6 : d->undoDemoteFromBoundCRS();
5533 :
5534 6 : return eErr;
5535 : }
5536 :
5537 : /************************************************************************/
5538 : /* OSRSetGeocCS() */
5539 : /************************************************************************/
5540 :
5541 : /**
5542 : * \brief Set the user visible PROJCS name.
5543 : *
5544 : * This function is the same as OGRSpatialReference::SetGeocCS()
5545 : *
5546 : * @since OGR 1.9.0
5547 : */
5548 4 : OGRErr OSRSetGeocCS(OGRSpatialReferenceH hSRS, const char *pszName)
5549 :
5550 : {
5551 4 : VALIDATE_POINTER1(hSRS, "OSRSetGeocCS", OGRERR_FAILURE);
5552 :
5553 4 : return ToPointer(hSRS)->SetGeocCS(pszName);
5554 : }
5555 :
5556 : /************************************************************************/
5557 : /* SetVertCS() */
5558 : /************************************************************************/
5559 :
5560 : /**
5561 : * \brief Set the user visible VERT_CS name.
5562 : *
5563 : * This method is the same as the C function OSRSetVertCS().
5564 :
5565 : * This method will ensure a VERT_CS node is created if needed. If the
5566 : * existing coordinate system is GEOGCS or PROJCS rooted, then it will be
5567 : * turned into a COMPD_CS.
5568 : *
5569 : * @param pszVertCSName the user visible name of the vertical coordinate
5570 : * system. Not used as a key.
5571 : *
5572 : * @param pszVertDatumName the user visible name of the vertical datum. It
5573 : * is helpful if this matches the EPSG name.
5574 : *
5575 : * @param nVertDatumType the OGC vertical datum type. Ignored
5576 : *
5577 : * @return OGRERR_NONE on success.
5578 : *
5579 : * @since OGR 1.9.0
5580 : */
5581 :
5582 1 : OGRErr OGRSpatialReference::SetVertCS(const char *pszVertCSName,
5583 : const char *pszVertDatumName,
5584 : int nVertDatumType)
5585 :
5586 : {
5587 1 : TAKE_OPTIONAL_LOCK();
5588 :
5589 1 : CPL_IGNORE_RET_VAL(nVertDatumType);
5590 :
5591 1 : d->refreshProjObj();
5592 :
5593 1 : auto vertCRS = proj_create_vertical_crs(d->getPROJContext(), pszVertCSName,
5594 : pszVertDatumName, nullptr, 0.0);
5595 :
5596 : /* -------------------------------------------------------------------- */
5597 : /* Handle the case where we want to make a compound coordinate */
5598 : /* system. */
5599 : /* -------------------------------------------------------------------- */
5600 1 : if (IsProjected() || IsGeographic())
5601 : {
5602 1 : auto compoundCRS = proj_create_compound_crs(
5603 1 : d->getPROJContext(), nullptr, d->m_pj_crs, vertCRS);
5604 1 : proj_destroy(vertCRS);
5605 1 : d->setPjCRS(compoundCRS);
5606 : }
5607 : else
5608 : {
5609 0 : d->setPjCRS(vertCRS);
5610 : }
5611 2 : return OGRERR_NONE;
5612 : }
5613 :
5614 : /************************************************************************/
5615 : /* OSRSetVertCS() */
5616 : /************************************************************************/
5617 :
5618 : /**
5619 : * \brief Setup the vertical coordinate system.
5620 : *
5621 : * This function is the same as OGRSpatialReference::SetVertCS()
5622 : *
5623 : * @since OGR 1.9.0
5624 : */
5625 0 : OGRErr OSRSetVertCS(OGRSpatialReferenceH hSRS, const char *pszVertCSName,
5626 : const char *pszVertDatumName, int nVertDatumType)
5627 :
5628 : {
5629 0 : VALIDATE_POINTER1(hSRS, "OSRSetVertCS", OGRERR_FAILURE);
5630 :
5631 0 : return ToPointer(hSRS)->SetVertCS(pszVertCSName, pszVertDatumName,
5632 0 : nVertDatumType);
5633 : }
5634 :
5635 : /************************************************************************/
5636 : /* SetCompoundCS() */
5637 : /************************************************************************/
5638 :
5639 : /**
5640 : * \brief Setup a compound coordinate system.
5641 : *
5642 : * This method is the same as the C function OSRSetCompoundCS().
5643 :
5644 : * This method is replace the current SRS with a COMPD_CS coordinate system
5645 : * consisting of the passed in horizontal and vertical coordinate systems.
5646 : *
5647 : * @param pszName the name of the compound coordinate system.
5648 : *
5649 : * @param poHorizSRS the horizontal SRS (PROJCS or GEOGCS).
5650 : *
5651 : * @param poVertSRS the vertical SRS (VERT_CS).
5652 : *
5653 : * @return OGRERR_NONE on success.
5654 : */
5655 :
5656 92 : OGRErr OGRSpatialReference::SetCompoundCS(const char *pszName,
5657 : const OGRSpatialReference *poHorizSRS,
5658 : const OGRSpatialReference *poVertSRS)
5659 :
5660 : {
5661 184 : TAKE_OPTIONAL_LOCK();
5662 :
5663 : /* -------------------------------------------------------------------- */
5664 : /* Verify these are legal horizontal and vertical coordinate */
5665 : /* systems. */
5666 : /* -------------------------------------------------------------------- */
5667 92 : if (!poVertSRS->IsVertical())
5668 : {
5669 0 : CPLError(CE_Failure, CPLE_AppDefined,
5670 : "SetCompoundCS() fails, vertical component is not VERT_CS.");
5671 0 : return OGRERR_FAILURE;
5672 : }
5673 92 : if (!poHorizSRS->IsProjected() && !poHorizSRS->IsGeographic())
5674 : {
5675 0 : CPLError(CE_Failure, CPLE_AppDefined,
5676 : "SetCompoundCS() fails, horizontal component is not PROJCS or "
5677 : "GEOGCS.");
5678 0 : return OGRERR_FAILURE;
5679 : }
5680 :
5681 : /* -------------------------------------------------------------------- */
5682 : /* Replace with compound srs. */
5683 : /* -------------------------------------------------------------------- */
5684 92 : Clear();
5685 :
5686 92 : auto compoundCRS = proj_create_compound_crs(d->getPROJContext(), pszName,
5687 92 : poHorizSRS->d->m_pj_crs,
5688 92 : poVertSRS->d->m_pj_crs);
5689 92 : d->setPjCRS(compoundCRS);
5690 :
5691 92 : return OGRERR_NONE;
5692 : }
5693 :
5694 : /************************************************************************/
5695 : /* OSRSetCompoundCS() */
5696 : /************************************************************************/
5697 :
5698 : /**
5699 : * \brief Setup a compound coordinate system.
5700 : *
5701 : * This function is the same as OGRSpatialReference::SetCompoundCS()
5702 : */
5703 8 : OGRErr OSRSetCompoundCS(OGRSpatialReferenceH hSRS, const char *pszName,
5704 : OGRSpatialReferenceH hHorizSRS,
5705 : OGRSpatialReferenceH hVertSRS)
5706 :
5707 : {
5708 8 : VALIDATE_POINTER1(hSRS, "OSRSetCompoundCS", OGRERR_FAILURE);
5709 8 : VALIDATE_POINTER1(hHorizSRS, "OSRSetCompoundCS", OGRERR_FAILURE);
5710 8 : VALIDATE_POINTER1(hVertSRS, "OSRSetCompoundCS", OGRERR_FAILURE);
5711 :
5712 16 : return ToPointer(hSRS)->SetCompoundCS(pszName, ToPointer(hHorizSRS),
5713 16 : ToPointer(hVertSRS));
5714 : }
5715 :
5716 : /************************************************************************/
5717 : /* SetProjCS() */
5718 : /************************************************************************/
5719 :
5720 : /**
5721 : * \brief Set the user visible PROJCS name.
5722 : *
5723 : * This method is the same as the C function OSRSetProjCS().
5724 : *
5725 : * This method will ensure a PROJCS node is created as the root,
5726 : * and set the provided name on it. If used on a GEOGCS coordinate system,
5727 : * the GEOGCS node will be demoted to be a child of the new PROJCS root.
5728 : *
5729 : * @param pszName the user visible name to assign. Not used as a key.
5730 : *
5731 : * @return OGRERR_NONE on success.
5732 : */
5733 :
5734 4391 : OGRErr OGRSpatialReference::SetProjCS(const char *pszName)
5735 :
5736 : {
5737 4391 : TAKE_OPTIONAL_LOCK();
5738 :
5739 4391 : d->refreshProjObj();
5740 4391 : d->demoteFromBoundCRS();
5741 4391 : if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
5742 : {
5743 474 : d->setPjCRS(proj_alter_name(d->getPROJContext(), d->m_pj_crs, pszName));
5744 : }
5745 : else
5746 : {
5747 3917 : auto dummyConv = proj_create_conversion(d->getPROJContext(), nullptr,
5748 : nullptr, nullptr, nullptr,
5749 : nullptr, nullptr, 0, nullptr);
5750 3917 : auto cs = proj_create_cartesian_2D_cs(
5751 : d->getPROJContext(), PJ_CART2D_EASTING_NORTHING, nullptr, 0);
5752 :
5753 3917 : auto projCRS = proj_create_projected_crs(
5754 3917 : d->getPROJContext(), pszName, d->getGeodBaseCRS(), dummyConv, cs);
5755 3917 : proj_destroy(dummyConv);
5756 3917 : proj_destroy(cs);
5757 :
5758 3917 : d->setPjCRS(projCRS);
5759 : }
5760 4391 : d->undoDemoteFromBoundCRS();
5761 8782 : return OGRERR_NONE;
5762 : }
5763 :
5764 : /************************************************************************/
5765 : /* OSRSetProjCS() */
5766 : /************************************************************************/
5767 :
5768 : /**
5769 : * \brief Set the user visible PROJCS name.
5770 : *
5771 : * This function is the same as OGRSpatialReference::SetProjCS()
5772 : */
5773 1 : OGRErr OSRSetProjCS(OGRSpatialReferenceH hSRS, const char *pszName)
5774 :
5775 : {
5776 1 : VALIDATE_POINTER1(hSRS, "OSRSetProjCS", OGRERR_FAILURE);
5777 :
5778 1 : return ToPointer(hSRS)->SetProjCS(pszName);
5779 : }
5780 :
5781 : /************************************************************************/
5782 : /* SetProjection() */
5783 : /************************************************************************/
5784 :
5785 : /**
5786 : * \brief Set a projection name.
5787 : *
5788 : * This method is the same as the C function OSRSetProjection().
5789 : *
5790 : * @param pszProjection the projection name, which should be selected from
5791 : * the macros in ogr_srs_api.h, such as SRS_PT_TRANSVERSE_MERCATOR.
5792 : *
5793 : * @return OGRERR_NONE on success.
5794 : */
5795 :
5796 23 : OGRErr OGRSpatialReference::SetProjection(const char *pszProjection)
5797 :
5798 : {
5799 46 : TAKE_OPTIONAL_LOCK();
5800 :
5801 23 : OGR_SRSNode *poGeogCS = nullptr;
5802 :
5803 23 : if (GetRoot() != nullptr && EQUAL(d->m_poRoot->GetValue(), "GEOGCS"))
5804 : {
5805 4 : poGeogCS = d->m_poRoot;
5806 4 : d->m_poRoot = nullptr;
5807 : }
5808 :
5809 23 : if (!GetAttrNode("PROJCS"))
5810 : {
5811 11 : SetNode("PROJCS", "unnamed");
5812 : }
5813 :
5814 23 : const OGRErr eErr = SetNode("PROJCS|PROJECTION", pszProjection);
5815 23 : if (eErr != OGRERR_NONE)
5816 0 : return eErr;
5817 :
5818 23 : if (poGeogCS != nullptr)
5819 4 : d->m_poRoot->InsertChild(poGeogCS, 1);
5820 :
5821 23 : return OGRERR_NONE;
5822 : }
5823 :
5824 : /************************************************************************/
5825 : /* OSRSetProjection() */
5826 : /************************************************************************/
5827 :
5828 : /**
5829 : * \brief Set a projection name.
5830 : *
5831 : * This function is the same as OGRSpatialReference::SetProjection()
5832 : */
5833 0 : OGRErr OSRSetProjection(OGRSpatialReferenceH hSRS, const char *pszProjection)
5834 :
5835 : {
5836 0 : VALIDATE_POINTER1(hSRS, "OSRSetProjection", OGRERR_FAILURE);
5837 :
5838 0 : return ToPointer(hSRS)->SetProjection(pszProjection);
5839 : }
5840 :
5841 : /************************************************************************/
5842 : /* GetWKT2ProjectionMethod() */
5843 : /************************************************************************/
5844 :
5845 : /**
5846 : * \brief Returns info on the projection method, based on WKT2 naming
5847 : * conventions.
5848 : *
5849 : * The returned strings are short lived and should be considered to be
5850 : * invalidated by any further call to the GDAL API.
5851 : *
5852 : * @param[out] ppszMethodName Pointer to a string that will receive the
5853 : * projection method name.
5854 : * @param[out] ppszMethodAuthName null pointer, or pointer to a string that will
5855 : * receive the name of the authority that defines the projection method.
5856 : * *ppszMethodAuthName may be nullptr if the projection method is not linked to
5857 : * an authority.
5858 : * @param[out] ppszMethodCode null pointer, or pointer to a string that will
5859 : * receive the code that defines the projection method.
5860 : * *ppszMethodCode may be nullptr if the projection method is not linked to
5861 : * an authority.
5862 : *
5863 : * @return OGRERR_NONE on success.
5864 : */
5865 : OGRErr
5866 1 : OGRSpatialReference::GetWKT2ProjectionMethod(const char **ppszMethodName,
5867 : const char **ppszMethodAuthName,
5868 : const char **ppszMethodCode) const
5869 : {
5870 2 : TAKE_OPTIONAL_LOCK();
5871 :
5872 1 : auto conv = proj_crs_get_coordoperation(d->getPROJContext(), d->m_pj_crs);
5873 1 : if (!conv)
5874 0 : return OGRERR_FAILURE;
5875 1 : const char *pszTmpMethodName = "";
5876 1 : const char *pszTmpMethodAuthName = "";
5877 1 : const char *pszTmpMethodCode = "";
5878 1 : int ret = proj_coordoperation_get_method_info(
5879 : d->getPROJContext(), conv, &pszTmpMethodName, &pszTmpMethodAuthName,
5880 : &pszTmpMethodCode);
5881 : // "Internalize" temporary strings returned by PROJ
5882 1 : CPLAssert(pszTmpMethodName);
5883 1 : if (ppszMethodName)
5884 1 : *ppszMethodName = CPLSPrintf("%s", pszTmpMethodName);
5885 1 : if (ppszMethodAuthName)
5886 0 : *ppszMethodAuthName = pszTmpMethodAuthName
5887 0 : ? CPLSPrintf("%s", pszTmpMethodAuthName)
5888 0 : : nullptr;
5889 1 : if (ppszMethodCode)
5890 0 : *ppszMethodCode =
5891 0 : pszTmpMethodCode ? CPLSPrintf("%s", pszTmpMethodCode) : nullptr;
5892 1 : proj_destroy(conv);
5893 1 : return ret ? OGRERR_NONE : OGRERR_FAILURE;
5894 : }
5895 :
5896 : /************************************************************************/
5897 : /* SetProjParm() */
5898 : /************************************************************************/
5899 :
5900 : /**
5901 : * \brief Set a projection parameter value.
5902 : *
5903 : * Adds a new PARAMETER under the PROJCS with the indicated name and value.
5904 : *
5905 : * This method is the same as the C function OSRSetProjParm().
5906 : *
5907 : * Please check https://gdal.org/proj_list pages for
5908 : * legal parameter names for specific projections.
5909 : *
5910 : *
5911 : * @param pszParamName the parameter name, which should be selected from
5912 : * the macros in ogr_srs_api.h, such as SRS_PP_CENTRAL_MERIDIAN.
5913 : *
5914 : * @param dfValue value to assign.
5915 : *
5916 : * @return OGRERR_NONE on success.
5917 : */
5918 :
5919 129 : OGRErr OGRSpatialReference::SetProjParm(const char *pszParamName,
5920 : double dfValue)
5921 :
5922 : {
5923 258 : TAKE_OPTIONAL_LOCK();
5924 :
5925 129 : OGR_SRSNode *poPROJCS = GetAttrNode("PROJCS");
5926 :
5927 129 : if (poPROJCS == nullptr)
5928 3 : return OGRERR_FAILURE;
5929 :
5930 126 : char szValue[64] = {'\0'};
5931 126 : OGRsnPrintDouble(szValue, sizeof(szValue), dfValue);
5932 :
5933 : /* -------------------------------------------------------------------- */
5934 : /* Try to find existing parameter with this name. */
5935 : /* -------------------------------------------------------------------- */
5936 1030 : for (int iChild = 0; iChild < poPROJCS->GetChildCount(); iChild++)
5937 : {
5938 943 : OGR_SRSNode *poParam = poPROJCS->GetChild(iChild);
5939 :
5940 1242 : if (EQUAL(poParam->GetValue(), "PARAMETER") &&
5941 1242 : poParam->GetChildCount() == 2 &&
5942 299 : EQUAL(poParam->GetChild(0)->GetValue(), pszParamName))
5943 : {
5944 39 : poParam->GetChild(1)->SetValue(szValue);
5945 39 : return OGRERR_NONE;
5946 : }
5947 : }
5948 :
5949 : /* -------------------------------------------------------------------- */
5950 : /* Otherwise create a new parameter and append. */
5951 : /* -------------------------------------------------------------------- */
5952 87 : OGR_SRSNode *poParam = new OGR_SRSNode("PARAMETER");
5953 87 : poParam->AddChild(new OGR_SRSNode(pszParamName));
5954 87 : poParam->AddChild(new OGR_SRSNode(szValue));
5955 :
5956 87 : poPROJCS->AddChild(poParam);
5957 :
5958 87 : return OGRERR_NONE;
5959 : }
5960 :
5961 : /************************************************************************/
5962 : /* OSRSetProjParm() */
5963 : /************************************************************************/
5964 :
5965 : /**
5966 : * \brief Set a projection parameter value.
5967 : *
5968 : * This function is the same as OGRSpatialReference::SetProjParm()
5969 : */
5970 0 : OGRErr OSRSetProjParm(OGRSpatialReferenceH hSRS, const char *pszParamName,
5971 : double dfValue)
5972 :
5973 : {
5974 0 : VALIDATE_POINTER1(hSRS, "OSRSetProjParm", OGRERR_FAILURE);
5975 :
5976 0 : return ToPointer(hSRS)->SetProjParm(pszParamName, dfValue);
5977 : }
5978 :
5979 : /************************************************************************/
5980 : /* FindProjParm() */
5981 : /************************************************************************/
5982 :
5983 : /**
5984 : * \brief Return the child index of the named projection parameter on
5985 : * its parent PROJCS node.
5986 : *
5987 : * @param pszParameter projection parameter to look for
5988 : * @param poPROJCS projection CS node to look in. If NULL is passed,
5989 : * the PROJCS node of the SpatialReference object will be searched.
5990 : *
5991 : * @return the child index of the named projection parameter. -1 on failure
5992 : */
5993 4566 : int OGRSpatialReference::FindProjParm(const char *pszParameter,
5994 : const OGR_SRSNode *poPROJCS) const
5995 :
5996 : {
5997 9132 : TAKE_OPTIONAL_LOCK();
5998 :
5999 4566 : if (poPROJCS == nullptr)
6000 0 : poPROJCS = GetAttrNode("PROJCS");
6001 :
6002 4566 : if (poPROJCS == nullptr)
6003 0 : return -1;
6004 :
6005 : /* -------------------------------------------------------------------- */
6006 : /* Search for requested parameter. */
6007 : /* -------------------------------------------------------------------- */
6008 4566 : bool bIsWKT2 = false;
6009 29516 : for (int iChild = 0; iChild < poPROJCS->GetChildCount(); iChild++)
6010 : {
6011 29009 : const OGR_SRSNode *poParameter = poPROJCS->GetChild(iChild);
6012 :
6013 29009 : if (poParameter->GetChildCount() >= 2)
6014 : {
6015 19918 : const char *pszValue = poParameter->GetValue();
6016 33193 : if (EQUAL(pszValue, "PARAMETER") &&
6017 13275 : EQUAL(poPROJCS->GetChild(iChild)->GetChild(0)->GetValue(),
6018 : pszParameter))
6019 : {
6020 4059 : return iChild;
6021 : }
6022 15859 : else if (EQUAL(pszValue, "METHOD"))
6023 : {
6024 41 : bIsWKT2 = true;
6025 : }
6026 : }
6027 : }
6028 :
6029 : /* -------------------------------------------------------------------- */
6030 : /* Try similar names, for selected parameters. */
6031 : /* -------------------------------------------------------------------- */
6032 507 : if (EQUAL(pszParameter, SRS_PP_LATITUDE_OF_ORIGIN))
6033 : {
6034 232 : if (bIsWKT2)
6035 : {
6036 8 : int iChild = FindProjParm(
6037 : EPSG_NAME_PARAMETER_LATITUDE_OF_NATURAL_ORIGIN, poPROJCS);
6038 8 : if (iChild == -1)
6039 3 : iChild = FindProjParm(
6040 : EPSG_NAME_PARAMETER_LATITUDE_PROJECTION_CENTRE, poPROJCS);
6041 8 : return iChild;
6042 : }
6043 224 : return FindProjParm(SRS_PP_LATITUDE_OF_CENTER, poPROJCS);
6044 : }
6045 :
6046 275 : if (EQUAL(pszParameter, SRS_PP_CENTRAL_MERIDIAN))
6047 : {
6048 32 : if (bIsWKT2)
6049 : {
6050 9 : int iChild = FindProjParm(
6051 : EPSG_NAME_PARAMETER_LONGITUDE_OF_NATURAL_ORIGIN, poPROJCS);
6052 9 : if (iChild == -1)
6053 0 : iChild = FindProjParm(
6054 : EPSG_NAME_PARAMETER_LONGITUDE_PROJECTION_CENTRE, poPROJCS);
6055 9 : return iChild;
6056 : }
6057 23 : int iChild = FindProjParm(SRS_PP_LONGITUDE_OF_CENTER, poPROJCS);
6058 23 : if (iChild == -1)
6059 0 : iChild = FindProjParm(SRS_PP_LONGITUDE_OF_ORIGIN, poPROJCS);
6060 23 : return iChild;
6061 : }
6062 :
6063 243 : return -1;
6064 : }
6065 :
6066 : /************************************************************************/
6067 : /* GetProjParm() */
6068 : /************************************************************************/
6069 :
6070 : /**
6071 : * \brief Fetch a projection parameter value.
6072 : *
6073 : * NOTE: This code should be modified to translate non degree angles into
6074 : * degrees based on the GEOGCS unit. This has not yet been done.
6075 : *
6076 : * This method is the same as the C function OSRGetProjParm().
6077 : *
6078 : * @param pszName the name of the parameter to fetch, from the set of
6079 : * SRS_PP codes in ogr_srs_api.h.
6080 : *
6081 : * @param dfDefaultValue the value to return if this parameter doesn't exist.
6082 : *
6083 : * @param pnErr place to put error code on failure. Ignored if NULL.
6084 : *
6085 : * @return value of parameter.
6086 : */
6087 :
6088 4549 : double OGRSpatialReference::GetProjParm(const char *pszName,
6089 : double dfDefaultValue,
6090 : OGRErr *pnErr) const
6091 :
6092 : {
6093 9098 : TAKE_OPTIONAL_LOCK();
6094 :
6095 4549 : d->refreshProjObj();
6096 4549 : GetRoot(); // force update of d->m_bNodesWKT2
6097 :
6098 4549 : if (pnErr != nullptr)
6099 3582 : *pnErr = OGRERR_NONE;
6100 :
6101 : /* -------------------------------------------------------------------- */
6102 : /* Find the desired parameter. */
6103 : /* -------------------------------------------------------------------- */
6104 : const OGR_SRSNode *poPROJCS =
6105 4549 : GetAttrNode(d->m_bNodesWKT2 ? "CONVERSION" : "PROJCS");
6106 4549 : if (poPROJCS == nullptr)
6107 : {
6108 250 : if (pnErr != nullptr)
6109 250 : *pnErr = OGRERR_FAILURE;
6110 250 : return dfDefaultValue;
6111 : }
6112 :
6113 4299 : const int iChild = FindProjParm(pszName, poPROJCS);
6114 4299 : if (iChild == -1)
6115 : {
6116 240 : if (IsProjected() && GetAxesCount() == 3)
6117 : {
6118 3 : OGRSpatialReference *poSRSTmp = Clone();
6119 3 : poSRSTmp->DemoteTo2D(nullptr);
6120 : const double dfRet =
6121 3 : poSRSTmp->GetProjParm(pszName, dfDefaultValue, pnErr);
6122 3 : delete poSRSTmp;
6123 3 : return dfRet;
6124 : }
6125 :
6126 237 : if (pnErr != nullptr)
6127 215 : *pnErr = OGRERR_FAILURE;
6128 237 : return dfDefaultValue;
6129 : }
6130 :
6131 4059 : const OGR_SRSNode *poParameter = poPROJCS->GetChild(iChild);
6132 4059 : return CPLAtof(poParameter->GetChild(1)->GetValue());
6133 : }
6134 :
6135 : /************************************************************************/
6136 : /* OSRGetProjParm() */
6137 : /************************************************************************/
6138 :
6139 : /**
6140 : * \brief Fetch a projection parameter value.
6141 : *
6142 : * This function is the same as OGRSpatialReference::GetProjParm()
6143 : */
6144 90 : double OSRGetProjParm(OGRSpatialReferenceH hSRS, const char *pszName,
6145 : double dfDefaultValue, OGRErr *pnErr)
6146 :
6147 : {
6148 90 : VALIDATE_POINTER1(hSRS, "OSRGetProjParm", 0);
6149 :
6150 90 : return ToPointer(hSRS)->GetProjParm(pszName, dfDefaultValue, pnErr);
6151 : }
6152 :
6153 : /************************************************************************/
6154 : /* GetNormProjParm() */
6155 : /************************************************************************/
6156 :
6157 : /**
6158 : * \brief Fetch a normalized projection parameter value.
6159 : *
6160 : * This method is the same as GetProjParm() except that the value of
6161 : * the parameter is "normalized" into degrees or meters depending on
6162 : * whether it is linear or angular.
6163 : *
6164 : * This method is the same as the C function OSRGetNormProjParm().
6165 : *
6166 : * @param pszName the name of the parameter to fetch, from the set of
6167 : * SRS_PP codes in ogr_srs_api.h.
6168 : *
6169 : * @param dfDefaultValue the value to return if this parameter doesn't exist.
6170 : *
6171 : * @param pnErr place to put error code on failure. Ignored if NULL.
6172 : *
6173 : * @return value of parameter.
6174 : */
6175 :
6176 3557 : double OGRSpatialReference::GetNormProjParm(const char *pszName,
6177 : double dfDefaultValue,
6178 : OGRErr *pnErr) const
6179 :
6180 : {
6181 7114 : TAKE_OPTIONAL_LOCK();
6182 :
6183 3557 : GetNormInfo();
6184 :
6185 3557 : OGRErr nError = OGRERR_NONE;
6186 3557 : double dfRawResult = GetProjParm(pszName, dfDefaultValue, &nError);
6187 3557 : if (pnErr != nullptr)
6188 0 : *pnErr = nError;
6189 :
6190 : // If we got the default just return it unadjusted.
6191 3557 : if (nError != OGRERR_NONE)
6192 465 : return dfRawResult;
6193 :
6194 3092 : if (d->dfToDegrees != 1.0 && IsAngularParameter(pszName))
6195 8 : dfRawResult *= d->dfToDegrees;
6196 :
6197 3092 : if (d->dfToMeter != 1.0 && IsLinearParameter(pszName))
6198 5 : return dfRawResult * d->dfToMeter;
6199 :
6200 3087 : return dfRawResult;
6201 : }
6202 :
6203 : /************************************************************************/
6204 : /* OSRGetNormProjParm() */
6205 : /************************************************************************/
6206 :
6207 : /**
6208 : * \brief This function is the same as OGRSpatialReference::
6209 : *
6210 : * This function is the same as OGRSpatialReference::GetNormProjParm()
6211 : */
6212 1 : double OSRGetNormProjParm(OGRSpatialReferenceH hSRS, const char *pszName,
6213 : double dfDefaultValue, OGRErr *pnErr)
6214 :
6215 : {
6216 1 : VALIDATE_POINTER1(hSRS, "OSRGetNormProjParm", 0);
6217 :
6218 1 : return ToPointer(hSRS)->GetNormProjParm(pszName, dfDefaultValue, pnErr);
6219 : }
6220 :
6221 : /************************************************************************/
6222 : /* SetNormProjParm() */
6223 : /************************************************************************/
6224 :
6225 : /**
6226 : * \brief Set a projection parameter with a normalized value.
6227 : *
6228 : * This method is the same as SetProjParm() except that the value of
6229 : * the parameter passed in is assumed to be in "normalized" form (decimal
6230 : * degrees for angular values, meters for linear values. The values are
6231 : * converted in a form suitable for the GEOGCS and linear units in effect.
6232 : *
6233 : * This method is the same as the C function OSRSetNormProjParm().
6234 : *
6235 : * @param pszName the parameter name, which should be selected from
6236 : * the macros in ogr_srs_api.h, such as SRS_PP_CENTRAL_MERIDIAN.
6237 : *
6238 : * @param dfValue value to assign.
6239 : *
6240 : * @return OGRERR_NONE on success.
6241 : */
6242 :
6243 91 : OGRErr OGRSpatialReference::SetNormProjParm(const char *pszName, double dfValue)
6244 :
6245 : {
6246 182 : TAKE_OPTIONAL_LOCK();
6247 :
6248 91 : GetNormInfo();
6249 :
6250 91 : if (d->dfToDegrees != 0.0 &&
6251 91 : (d->dfToDegrees != 1.0 || d->dfFromGreenwich != 0.0) &&
6252 0 : IsAngularParameter(pszName))
6253 : {
6254 0 : dfValue /= d->dfToDegrees;
6255 : }
6256 95 : else if (d->dfToMeter != 1.0 && d->dfToMeter != 0.0 &&
6257 4 : IsLinearParameter(pszName))
6258 4 : dfValue /= d->dfToMeter;
6259 :
6260 182 : return SetProjParm(pszName, dfValue);
6261 : }
6262 :
6263 : /************************************************************************/
6264 : /* OSRSetNormProjParm() */
6265 : /************************************************************************/
6266 :
6267 : /**
6268 : * \brief Set a projection parameter with a normalized value.
6269 : *
6270 : * This function is the same as OGRSpatialReference::SetNormProjParm()
6271 : */
6272 0 : OGRErr OSRSetNormProjParm(OGRSpatialReferenceH hSRS, const char *pszParamName,
6273 : double dfValue)
6274 :
6275 : {
6276 0 : VALIDATE_POINTER1(hSRS, "OSRSetNormProjParm", OGRERR_FAILURE);
6277 :
6278 0 : return ToPointer(hSRS)->SetNormProjParm(pszParamName, dfValue);
6279 : }
6280 :
6281 : /************************************************************************/
6282 : /* SetTM() */
6283 : /************************************************************************/
6284 :
6285 428 : OGRErr OGRSpatialReference::SetTM(double dfCenterLat, double dfCenterLong,
6286 : double dfScale, double dfFalseEasting,
6287 : double dfFalseNorthing)
6288 :
6289 : {
6290 856 : TAKE_OPTIONAL_LOCK();
6291 :
6292 428 : return d->replaceConversionAndUnref(
6293 : proj_create_conversion_transverse_mercator(
6294 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale,
6295 856 : dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6296 : }
6297 :
6298 : /************************************************************************/
6299 : /* OSRSetTM() */
6300 : /************************************************************************/
6301 :
6302 1 : OGRErr OSRSetTM(OGRSpatialReferenceH hSRS, double dfCenterLat,
6303 : double dfCenterLong, double dfScale, double dfFalseEasting,
6304 : double dfFalseNorthing)
6305 :
6306 : {
6307 1 : VALIDATE_POINTER1(hSRS, "OSRSetTM", OGRERR_FAILURE);
6308 :
6309 1 : return ToPointer(hSRS)->SetTM(dfCenterLat, dfCenterLong, dfScale,
6310 1 : dfFalseEasting, dfFalseNorthing);
6311 : }
6312 :
6313 : /************************************************************************/
6314 : /* SetTMVariant() */
6315 : /************************************************************************/
6316 :
6317 0 : OGRErr OGRSpatialReference::SetTMVariant(const char *pszVariantName,
6318 : double dfCenterLat,
6319 : double dfCenterLong, double dfScale,
6320 : double dfFalseEasting,
6321 : double dfFalseNorthing)
6322 :
6323 : {
6324 0 : TAKE_OPTIONAL_LOCK();
6325 :
6326 0 : SetProjection(pszVariantName);
6327 0 : SetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, dfCenterLat);
6328 0 : SetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, dfCenterLong);
6329 0 : SetNormProjParm(SRS_PP_SCALE_FACTOR, dfScale);
6330 0 : SetNormProjParm(SRS_PP_FALSE_EASTING, dfFalseEasting);
6331 0 : SetNormProjParm(SRS_PP_FALSE_NORTHING, dfFalseNorthing);
6332 :
6333 0 : return OGRERR_NONE;
6334 : }
6335 :
6336 : /************************************************************************/
6337 : /* OSRSetTMVariant() */
6338 : /************************************************************************/
6339 :
6340 0 : OGRErr OSRSetTMVariant(OGRSpatialReferenceH hSRS, const char *pszVariantName,
6341 : double dfCenterLat, double dfCenterLong, double dfScale,
6342 : double dfFalseEasting, double dfFalseNorthing)
6343 :
6344 : {
6345 0 : VALIDATE_POINTER1(hSRS, "OSRSetTMVariant", OGRERR_FAILURE);
6346 :
6347 0 : return ToPointer(hSRS)->SetTMVariant(pszVariantName, dfCenterLat,
6348 : dfCenterLong, dfScale, dfFalseEasting,
6349 0 : dfFalseNorthing);
6350 : }
6351 :
6352 : /************************************************************************/
6353 : /* SetTMSO() */
6354 : /************************************************************************/
6355 :
6356 3 : OGRErr OGRSpatialReference::SetTMSO(double dfCenterLat, double dfCenterLong,
6357 : double dfScale, double dfFalseEasting,
6358 : double dfFalseNorthing)
6359 :
6360 : {
6361 6 : TAKE_OPTIONAL_LOCK();
6362 :
6363 3 : auto conv = proj_create_conversion_transverse_mercator_south_oriented(
6364 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale, dfFalseEasting,
6365 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6366 :
6367 3 : const char *pszName = nullptr;
6368 3 : double dfConvFactor = GetTargetLinearUnits(nullptr, &pszName);
6369 3 : CPLString osName = pszName ? pszName : "";
6370 :
6371 3 : d->refreshProjObj();
6372 :
6373 3 : d->demoteFromBoundCRS();
6374 :
6375 3 : auto cs = proj_create_cartesian_2D_cs(
6376 : d->getPROJContext(), PJ_CART2D_WESTING_SOUTHING,
6377 3 : !osName.empty() ? osName.c_str() : nullptr, dfConvFactor);
6378 : auto projCRS =
6379 3 : proj_create_projected_crs(d->getPROJContext(), d->getProjCRSName(),
6380 3 : d->getGeodBaseCRS(), conv, cs);
6381 3 : proj_destroy(conv);
6382 3 : proj_destroy(cs);
6383 :
6384 3 : d->setPjCRS(projCRS);
6385 :
6386 3 : d->undoDemoteFromBoundCRS();
6387 :
6388 6 : return OGRERR_NONE;
6389 : }
6390 :
6391 : /************************************************************************/
6392 : /* OSRSetTMSO() */
6393 : /************************************************************************/
6394 :
6395 0 : OGRErr OSRSetTMSO(OGRSpatialReferenceH hSRS, double dfCenterLat,
6396 : double dfCenterLong, double dfScale, double dfFalseEasting,
6397 : double dfFalseNorthing)
6398 :
6399 : {
6400 0 : VALIDATE_POINTER1(hSRS, "OSRSetTMSO", OGRERR_FAILURE);
6401 :
6402 0 : return ToPointer(hSRS)->SetTMSO(dfCenterLat, dfCenterLong, dfScale,
6403 0 : dfFalseEasting, dfFalseNorthing);
6404 : }
6405 :
6406 : /************************************************************************/
6407 : /* SetTPED() */
6408 : /************************************************************************/
6409 :
6410 1 : OGRErr OGRSpatialReference::SetTPED(double dfLat1, double dfLong1,
6411 : double dfLat2, double dfLong2,
6412 : double dfFalseEasting,
6413 : double dfFalseNorthing)
6414 :
6415 : {
6416 2 : TAKE_OPTIONAL_LOCK();
6417 :
6418 1 : return d->replaceConversionAndUnref(
6419 : proj_create_conversion_two_point_equidistant(
6420 : d->getPROJContext(), dfLat1, dfLong1, dfLat2, dfLong2,
6421 2 : dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6422 : }
6423 :
6424 : /************************************************************************/
6425 : /* OSRSetTPED() */
6426 : /************************************************************************/
6427 :
6428 0 : OGRErr OSRSetTPED(OGRSpatialReferenceH hSRS, double dfLat1, double dfLong1,
6429 : double dfLat2, double dfLong2, double dfFalseEasting,
6430 : double dfFalseNorthing)
6431 :
6432 : {
6433 0 : VALIDATE_POINTER1(hSRS, "OSRSetTPED", OGRERR_FAILURE);
6434 :
6435 0 : return ToPointer(hSRS)->SetTPED(dfLat1, dfLong1, dfLat2, dfLong2,
6436 0 : dfFalseEasting, dfFalseNorthing);
6437 : }
6438 :
6439 : /************************************************************************/
6440 : /* SetTMG() */
6441 : /************************************************************************/
6442 :
6443 0 : OGRErr OGRSpatialReference::SetTMG(double dfCenterLat, double dfCenterLong,
6444 : double dfFalseEasting,
6445 : double dfFalseNorthing)
6446 :
6447 : {
6448 0 : TAKE_OPTIONAL_LOCK();
6449 :
6450 0 : return d->replaceConversionAndUnref(
6451 : proj_create_conversion_tunisia_mapping_grid(
6452 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
6453 0 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6454 : }
6455 :
6456 : /************************************************************************/
6457 : /* OSRSetTMG() */
6458 : /************************************************************************/
6459 :
6460 0 : OGRErr OSRSetTMG(OGRSpatialReferenceH hSRS, double dfCenterLat,
6461 : double dfCenterLong, double dfFalseEasting,
6462 : double dfFalseNorthing)
6463 :
6464 : {
6465 0 : VALIDATE_POINTER1(hSRS, "OSRSetTMG", OGRERR_FAILURE);
6466 :
6467 0 : return ToPointer(hSRS)->SetTMG(dfCenterLat, dfCenterLong, dfFalseEasting,
6468 0 : dfFalseNorthing);
6469 : }
6470 :
6471 : /************************************************************************/
6472 : /* SetACEA() */
6473 : /************************************************************************/
6474 :
6475 40 : OGRErr OGRSpatialReference::SetACEA(double dfStdP1, double dfStdP2,
6476 : double dfCenterLat, double dfCenterLong,
6477 : double dfFalseEasting,
6478 : double dfFalseNorthing)
6479 :
6480 : {
6481 80 : TAKE_OPTIONAL_LOCK();
6482 :
6483 : // Note different order of parameters. The one in PROJ is conformant with
6484 : // EPSG
6485 40 : return d->replaceConversionAndUnref(
6486 : proj_create_conversion_albers_equal_area(
6487 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfStdP1, dfStdP2,
6488 80 : dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6489 : }
6490 :
6491 : /************************************************************************/
6492 : /* OSRSetACEA() */
6493 : /************************************************************************/
6494 :
6495 0 : OGRErr OSRSetACEA(OGRSpatialReferenceH hSRS, double dfStdP1, double dfStdP2,
6496 : double dfCenterLat, double dfCenterLong,
6497 : double dfFalseEasting, double dfFalseNorthing)
6498 :
6499 : {
6500 0 : VALIDATE_POINTER1(hSRS, "OSRSetACEA", OGRERR_FAILURE);
6501 :
6502 0 : return ToPointer(hSRS)->SetACEA(dfStdP1, dfStdP2, dfCenterLat, dfCenterLong,
6503 0 : dfFalseEasting, dfFalseNorthing);
6504 : }
6505 :
6506 : /************************************************************************/
6507 : /* SetAE() */
6508 : /************************************************************************/
6509 :
6510 21 : OGRErr OGRSpatialReference::SetAE(double dfCenterLat, double dfCenterLong,
6511 : double dfFalseEasting, double dfFalseNorthing)
6512 :
6513 : {
6514 42 : TAKE_OPTIONAL_LOCK();
6515 :
6516 21 : return d->replaceConversionAndUnref(
6517 : proj_create_conversion_azimuthal_equidistant(
6518 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
6519 42 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6520 : }
6521 :
6522 : /************************************************************************/
6523 : /* OSRSetAE() */
6524 : /************************************************************************/
6525 :
6526 0 : OGRErr OSRSetAE(OGRSpatialReferenceH hSRS, double dfCenterLat,
6527 : double dfCenterLong, double dfFalseEasting,
6528 : double dfFalseNorthing)
6529 :
6530 : {
6531 0 : VALIDATE_POINTER1(hSRS, "OSRSetACEA", OGRERR_FAILURE);
6532 :
6533 0 : return ToPointer(hSRS)->SetAE(dfCenterLat, dfCenterLong, dfFalseEasting,
6534 0 : dfFalseNorthing);
6535 : }
6536 :
6537 : /************************************************************************/
6538 : /* SetBonne() */
6539 : /************************************************************************/
6540 :
6541 1 : OGRErr OGRSpatialReference::SetBonne(double dfStdP1, double dfCentralMeridian,
6542 : double dfFalseEasting,
6543 : double dfFalseNorthing)
6544 :
6545 : {
6546 2 : TAKE_OPTIONAL_LOCK();
6547 :
6548 1 : return d->replaceConversionAndUnref(proj_create_conversion_bonne(
6549 : d->getPROJContext(), dfStdP1, dfCentralMeridian, dfFalseEasting,
6550 2 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6551 : }
6552 :
6553 : /************************************************************************/
6554 : /* OSRSetBonne() */
6555 : /************************************************************************/
6556 :
6557 0 : OGRErr OSRSetBonne(OGRSpatialReferenceH hSRS, double dfStdP1,
6558 : double dfCentralMeridian, double dfFalseEasting,
6559 : double dfFalseNorthing)
6560 :
6561 : {
6562 0 : VALIDATE_POINTER1(hSRS, "OSRSetBonne", OGRERR_FAILURE);
6563 :
6564 0 : return ToPointer(hSRS)->SetBonne(dfStdP1, dfCentralMeridian, dfFalseEasting,
6565 0 : dfFalseNorthing);
6566 : }
6567 :
6568 : /************************************************************************/
6569 : /* SetCEA() */
6570 : /************************************************************************/
6571 :
6572 4 : OGRErr OGRSpatialReference::SetCEA(double dfStdP1, double dfCentralMeridian,
6573 : double dfFalseEasting,
6574 : double dfFalseNorthing)
6575 :
6576 : {
6577 8 : TAKE_OPTIONAL_LOCK();
6578 :
6579 4 : return d->replaceConversionAndUnref(
6580 : proj_create_conversion_lambert_cylindrical_equal_area(
6581 : d->getPROJContext(), dfStdP1, dfCentralMeridian, dfFalseEasting,
6582 8 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6583 : }
6584 :
6585 : /************************************************************************/
6586 : /* OSRSetCEA() */
6587 : /************************************************************************/
6588 :
6589 0 : OGRErr OSRSetCEA(OGRSpatialReferenceH hSRS, double dfStdP1,
6590 : double dfCentralMeridian, double dfFalseEasting,
6591 : double dfFalseNorthing)
6592 :
6593 : {
6594 0 : VALIDATE_POINTER1(hSRS, "OSRSetCEA", OGRERR_FAILURE);
6595 :
6596 0 : return ToPointer(hSRS)->SetCEA(dfStdP1, dfCentralMeridian, dfFalseEasting,
6597 0 : dfFalseNorthing);
6598 : }
6599 :
6600 : /************************************************************************/
6601 : /* SetCS() */
6602 : /************************************************************************/
6603 :
6604 5 : OGRErr OGRSpatialReference::SetCS(double dfCenterLat, double dfCenterLong,
6605 : double dfFalseEasting, double dfFalseNorthing)
6606 :
6607 : {
6608 10 : TAKE_OPTIONAL_LOCK();
6609 :
6610 5 : return d->replaceConversionAndUnref(proj_create_conversion_cassini_soldner(
6611 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
6612 10 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6613 : }
6614 :
6615 : /************************************************************************/
6616 : /* OSRSetCS() */
6617 : /************************************************************************/
6618 :
6619 0 : OGRErr OSRSetCS(OGRSpatialReferenceH hSRS, double dfCenterLat,
6620 : double dfCenterLong, double dfFalseEasting,
6621 : double dfFalseNorthing)
6622 :
6623 : {
6624 0 : VALIDATE_POINTER1(hSRS, "OSRSetCS", OGRERR_FAILURE);
6625 :
6626 0 : return ToPointer(hSRS)->SetCS(dfCenterLat, dfCenterLong, dfFalseEasting,
6627 0 : dfFalseNorthing);
6628 : }
6629 :
6630 : /************************************************************************/
6631 : /* SetEC() */
6632 : /************************************************************************/
6633 :
6634 7 : OGRErr OGRSpatialReference::SetEC(double dfStdP1, double dfStdP2,
6635 : double dfCenterLat, double dfCenterLong,
6636 : double dfFalseEasting, double dfFalseNorthing)
6637 :
6638 : {
6639 14 : TAKE_OPTIONAL_LOCK();
6640 :
6641 : // Note: different order of arguments
6642 7 : return d->replaceConversionAndUnref(
6643 : proj_create_conversion_equidistant_conic(
6644 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfStdP1, dfStdP2,
6645 14 : dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6646 : }
6647 :
6648 : /************************************************************************/
6649 : /* OSRSetEC() */
6650 : /************************************************************************/
6651 :
6652 0 : OGRErr OSRSetEC(OGRSpatialReferenceH hSRS, double dfStdP1, double dfStdP2,
6653 : double dfCenterLat, double dfCenterLong, double dfFalseEasting,
6654 : double dfFalseNorthing)
6655 :
6656 : {
6657 0 : VALIDATE_POINTER1(hSRS, "OSRSetEC", OGRERR_FAILURE);
6658 :
6659 0 : return ToPointer(hSRS)->SetEC(dfStdP1, dfStdP2, dfCenterLat, dfCenterLong,
6660 0 : dfFalseEasting, dfFalseNorthing);
6661 : }
6662 :
6663 : /************************************************************************/
6664 : /* SetEckert() */
6665 : /************************************************************************/
6666 :
6667 10 : OGRErr OGRSpatialReference::SetEckert(int nVariation, // 1-6.
6668 : double dfCentralMeridian,
6669 : double dfFalseEasting,
6670 : double dfFalseNorthing)
6671 :
6672 : {
6673 20 : TAKE_OPTIONAL_LOCK();
6674 :
6675 : PJ *conv;
6676 10 : if (nVariation == 1)
6677 : {
6678 1 : conv = proj_create_conversion_eckert_i(
6679 : d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6680 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6681 : }
6682 9 : else if (nVariation == 2)
6683 : {
6684 1 : conv = proj_create_conversion_eckert_ii(
6685 : d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6686 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6687 : }
6688 8 : else if (nVariation == 3)
6689 : {
6690 1 : conv = proj_create_conversion_eckert_iii(
6691 : d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6692 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6693 : }
6694 7 : else if (nVariation == 4)
6695 : {
6696 3 : conv = proj_create_conversion_eckert_iv(
6697 : d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6698 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6699 : }
6700 4 : else if (nVariation == 5)
6701 : {
6702 1 : conv = proj_create_conversion_eckert_v(
6703 : d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6704 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6705 : }
6706 3 : else if (nVariation == 6)
6707 : {
6708 3 : conv = proj_create_conversion_eckert_vi(
6709 : d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6710 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6711 : }
6712 : else
6713 : {
6714 0 : CPLError(CE_Failure, CPLE_AppDefined,
6715 : "Unsupported Eckert variation (%d).", nVariation);
6716 0 : return OGRERR_UNSUPPORTED_SRS;
6717 : }
6718 :
6719 10 : return d->replaceConversionAndUnref(conv);
6720 : }
6721 :
6722 : /************************************************************************/
6723 : /* OSRSetEckert() */
6724 : /************************************************************************/
6725 :
6726 0 : OGRErr OSRSetEckert(OGRSpatialReferenceH hSRS, int nVariation,
6727 : double dfCentralMeridian, double dfFalseEasting,
6728 : double dfFalseNorthing)
6729 :
6730 : {
6731 0 : VALIDATE_POINTER1(hSRS, "OSRSetEckert", OGRERR_FAILURE);
6732 :
6733 0 : return ToPointer(hSRS)->SetEckert(nVariation, dfCentralMeridian,
6734 0 : dfFalseEasting, dfFalseNorthing);
6735 : }
6736 :
6737 : /************************************************************************/
6738 : /* SetEckertIV() */
6739 : /* */
6740 : /* Deprecated */
6741 : /************************************************************************/
6742 :
6743 2 : OGRErr OGRSpatialReference::SetEckertIV(double dfCentralMeridian,
6744 : double dfFalseEasting,
6745 : double dfFalseNorthing)
6746 :
6747 : {
6748 2 : return SetEckert(4, dfCentralMeridian, dfFalseEasting, dfFalseNorthing);
6749 : }
6750 :
6751 : /************************************************************************/
6752 : /* OSRSetEckertIV() */
6753 : /************************************************************************/
6754 :
6755 0 : OGRErr OSRSetEckertIV(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
6756 : double dfFalseEasting, double dfFalseNorthing)
6757 :
6758 : {
6759 0 : VALIDATE_POINTER1(hSRS, "OSRSetEckertIV", OGRERR_FAILURE);
6760 :
6761 0 : return ToPointer(hSRS)->SetEckertIV(dfCentralMeridian, dfFalseEasting,
6762 0 : dfFalseNorthing);
6763 : }
6764 :
6765 : /************************************************************************/
6766 : /* SetEckertVI() */
6767 : /* */
6768 : /* Deprecated */
6769 : /************************************************************************/
6770 :
6771 2 : OGRErr OGRSpatialReference::SetEckertVI(double dfCentralMeridian,
6772 : double dfFalseEasting,
6773 : double dfFalseNorthing)
6774 :
6775 : {
6776 2 : return SetEckert(6, dfCentralMeridian, dfFalseEasting, dfFalseNorthing);
6777 : }
6778 :
6779 : /************************************************************************/
6780 : /* OSRSetEckertVI() */
6781 : /************************************************************************/
6782 :
6783 0 : OGRErr OSRSetEckertVI(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
6784 : double dfFalseEasting, double dfFalseNorthing)
6785 :
6786 : {
6787 0 : VALIDATE_POINTER1(hSRS, "OSRSetEckertVI", OGRERR_FAILURE);
6788 :
6789 0 : return ToPointer(hSRS)->SetEckertVI(dfCentralMeridian, dfFalseEasting,
6790 0 : dfFalseNorthing);
6791 : }
6792 :
6793 : /************************************************************************/
6794 : /* SetEquirectangular() */
6795 : /************************************************************************/
6796 :
6797 2 : OGRErr OGRSpatialReference::SetEquirectangular(double dfCenterLat,
6798 : double dfCenterLong,
6799 : double dfFalseEasting,
6800 : double dfFalseNorthing)
6801 :
6802 : {
6803 4 : TAKE_OPTIONAL_LOCK();
6804 :
6805 2 : if (dfCenterLat == 0.0)
6806 : {
6807 0 : return d->replaceConversionAndUnref(
6808 : proj_create_conversion_equidistant_cylindrical(
6809 : d->getPROJContext(), 0.0, dfCenterLong, dfFalseEasting,
6810 0 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6811 : }
6812 :
6813 : // Non-standard extension with non-zero latitude of origin
6814 2 : SetProjection(SRS_PT_EQUIRECTANGULAR);
6815 2 : SetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, dfCenterLat);
6816 2 : SetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, dfCenterLong);
6817 2 : SetNormProjParm(SRS_PP_FALSE_EASTING, dfFalseEasting);
6818 2 : SetNormProjParm(SRS_PP_FALSE_NORTHING, dfFalseNorthing);
6819 :
6820 2 : return OGRERR_NONE;
6821 : }
6822 :
6823 : /************************************************************************/
6824 : /* OSRSetEquirectangular() */
6825 : /************************************************************************/
6826 :
6827 0 : OGRErr OSRSetEquirectangular(OGRSpatialReferenceH hSRS, double dfCenterLat,
6828 : double dfCenterLong, double dfFalseEasting,
6829 : double dfFalseNorthing)
6830 :
6831 : {
6832 0 : VALIDATE_POINTER1(hSRS, "OSRSetEquirectangular", OGRERR_FAILURE);
6833 :
6834 0 : return ToPointer(hSRS)->SetEquirectangular(dfCenterLat, dfCenterLong,
6835 0 : dfFalseEasting, dfFalseNorthing);
6836 : }
6837 :
6838 : /************************************************************************/
6839 : /* SetEquirectangular2() */
6840 : /* Generalized form */
6841 : /************************************************************************/
6842 :
6843 175 : OGRErr OGRSpatialReference::SetEquirectangular2(double dfCenterLat,
6844 : double dfCenterLong,
6845 : double dfStdParallel1,
6846 : double dfFalseEasting,
6847 : double dfFalseNorthing)
6848 :
6849 : {
6850 350 : TAKE_OPTIONAL_LOCK();
6851 :
6852 175 : if (dfCenterLat == 0.0)
6853 : {
6854 170 : return d->replaceConversionAndUnref(
6855 : proj_create_conversion_equidistant_cylindrical(
6856 : d->getPROJContext(), dfStdParallel1, dfCenterLong,
6857 170 : dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6858 : }
6859 :
6860 : // Non-standard extension with non-zero latitude of origin
6861 5 : SetProjection(SRS_PT_EQUIRECTANGULAR);
6862 5 : SetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, dfCenterLat);
6863 5 : SetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, dfCenterLong);
6864 5 : SetNormProjParm(SRS_PP_STANDARD_PARALLEL_1, dfStdParallel1);
6865 5 : SetNormProjParm(SRS_PP_FALSE_EASTING, dfFalseEasting);
6866 5 : SetNormProjParm(SRS_PP_FALSE_NORTHING, dfFalseNorthing);
6867 :
6868 5 : return OGRERR_NONE;
6869 : }
6870 :
6871 : /************************************************************************/
6872 : /* OSRSetEquirectangular2() */
6873 : /************************************************************************/
6874 :
6875 3 : OGRErr OSRSetEquirectangular2(OGRSpatialReferenceH hSRS, double dfCenterLat,
6876 : double dfCenterLong, double dfStdParallel1,
6877 : double dfFalseEasting, double dfFalseNorthing)
6878 :
6879 : {
6880 3 : VALIDATE_POINTER1(hSRS, "OSRSetEquirectangular2", OGRERR_FAILURE);
6881 :
6882 3 : return ToPointer(hSRS)->SetEquirectangular2(dfCenterLat, dfCenterLong,
6883 : dfStdParallel1, dfFalseEasting,
6884 3 : dfFalseNorthing);
6885 : }
6886 :
6887 : /************************************************************************/
6888 : /* SetGS() */
6889 : /************************************************************************/
6890 :
6891 5 : OGRErr OGRSpatialReference::SetGS(double dfCentralMeridian,
6892 : double dfFalseEasting, double dfFalseNorthing)
6893 :
6894 : {
6895 5 : return d->replaceConversionAndUnref(proj_create_conversion_gall(
6896 : d->getPROJContext(), dfCentralMeridian, dfFalseEasting, dfFalseNorthing,
6897 5 : nullptr, 0.0, nullptr, 0.0));
6898 : }
6899 :
6900 : /************************************************************************/
6901 : /* OSRSetGS() */
6902 : /************************************************************************/
6903 :
6904 2 : OGRErr OSRSetGS(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
6905 : double dfFalseEasting, double dfFalseNorthing)
6906 :
6907 : {
6908 2 : VALIDATE_POINTER1(hSRS, "OSRSetGS", OGRERR_FAILURE);
6909 :
6910 2 : return ToPointer(hSRS)->SetGS(dfCentralMeridian, dfFalseEasting,
6911 2 : dfFalseNorthing);
6912 : }
6913 :
6914 : /************************************************************************/
6915 : /* SetGH() */
6916 : /************************************************************************/
6917 :
6918 0 : OGRErr OGRSpatialReference::SetGH(double dfCentralMeridian,
6919 : double dfFalseEasting, double dfFalseNorthing)
6920 :
6921 : {
6922 0 : TAKE_OPTIONAL_LOCK();
6923 :
6924 0 : return d->replaceConversionAndUnref(proj_create_conversion_goode_homolosine(
6925 : d->getPROJContext(), dfCentralMeridian, dfFalseEasting, dfFalseNorthing,
6926 0 : nullptr, 0.0, nullptr, 0.0));
6927 : }
6928 :
6929 : /************************************************************************/
6930 : /* OSRSetGH() */
6931 : /************************************************************************/
6932 :
6933 0 : OGRErr OSRSetGH(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
6934 : double dfFalseEasting, double dfFalseNorthing)
6935 :
6936 : {
6937 0 : VALIDATE_POINTER1(hSRS, "OSRSetGH", OGRERR_FAILURE);
6938 :
6939 0 : return ToPointer(hSRS)->SetGH(dfCentralMeridian, dfFalseEasting,
6940 0 : dfFalseNorthing);
6941 : }
6942 :
6943 : /************************************************************************/
6944 : /* SetIGH() */
6945 : /************************************************************************/
6946 :
6947 0 : OGRErr OGRSpatialReference::SetIGH()
6948 :
6949 : {
6950 0 : TAKE_OPTIONAL_LOCK();
6951 :
6952 0 : return d->replaceConversionAndUnref(
6953 : proj_create_conversion_interrupted_goode_homolosine(
6954 0 : d->getPROJContext(), 0.0, 0.0, 0.0, nullptr, 0.0, nullptr, 0.0));
6955 : }
6956 :
6957 : /************************************************************************/
6958 : /* OSRSetIGH() */
6959 : /************************************************************************/
6960 :
6961 0 : OGRErr OSRSetIGH(OGRSpatialReferenceH hSRS)
6962 :
6963 : {
6964 0 : VALIDATE_POINTER1(hSRS, "OSRSetIGH", OGRERR_FAILURE);
6965 :
6966 0 : return ToPointer(hSRS)->SetIGH();
6967 : }
6968 :
6969 : /************************************************************************/
6970 : /* SetGEOS() */
6971 : /************************************************************************/
6972 :
6973 3 : OGRErr OGRSpatialReference::SetGEOS(double dfCentralMeridian,
6974 : double dfSatelliteHeight,
6975 : double dfFalseEasting,
6976 : double dfFalseNorthing)
6977 :
6978 : {
6979 6 : TAKE_OPTIONAL_LOCK();
6980 :
6981 3 : return d->replaceConversionAndUnref(
6982 : proj_create_conversion_geostationary_satellite_sweep_y(
6983 : d->getPROJContext(), dfCentralMeridian, dfSatelliteHeight,
6984 6 : dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6985 : }
6986 :
6987 : /************************************************************************/
6988 : /* OSRSetGEOS() */
6989 : /************************************************************************/
6990 :
6991 0 : OGRErr OSRSetGEOS(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
6992 : double dfSatelliteHeight, double dfFalseEasting,
6993 : double dfFalseNorthing)
6994 :
6995 : {
6996 0 : VALIDATE_POINTER1(hSRS, "OSRSetGEOS", OGRERR_FAILURE);
6997 :
6998 0 : return ToPointer(hSRS)->SetGEOS(dfCentralMeridian, dfSatelliteHeight,
6999 0 : dfFalseEasting, dfFalseNorthing);
7000 : }
7001 :
7002 : /************************************************************************/
7003 : /* SetGaussSchreiberTMercator() */
7004 : /************************************************************************/
7005 :
7006 0 : OGRErr OGRSpatialReference::SetGaussSchreiberTMercator(double dfCenterLat,
7007 : double dfCenterLong,
7008 : double dfScale,
7009 : double dfFalseEasting,
7010 : double dfFalseNorthing)
7011 :
7012 : {
7013 0 : TAKE_OPTIONAL_LOCK();
7014 :
7015 0 : return d->replaceConversionAndUnref(
7016 : proj_create_conversion_gauss_schreiber_transverse_mercator(
7017 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale,
7018 0 : dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
7019 : }
7020 :
7021 : /************************************************************************/
7022 : /* OSRSetGaussSchreiberTMercator() */
7023 : /************************************************************************/
7024 :
7025 0 : OGRErr OSRSetGaussSchreiberTMercator(OGRSpatialReferenceH hSRS,
7026 : double dfCenterLat, double dfCenterLong,
7027 : double dfScale, double dfFalseEasting,
7028 : double dfFalseNorthing)
7029 :
7030 : {
7031 0 : VALIDATE_POINTER1(hSRS, "OSRSetGaussSchreiberTMercator", OGRERR_FAILURE);
7032 :
7033 0 : return ToPointer(hSRS)->SetGaussSchreiberTMercator(
7034 0 : dfCenterLat, dfCenterLong, dfScale, dfFalseEasting, dfFalseNorthing);
7035 : }
7036 :
7037 : /************************************************************************/
7038 : /* SetGnomonic() */
7039 : /************************************************************************/
7040 :
7041 2 : OGRErr OGRSpatialReference::SetGnomonic(double dfCenterLat, double dfCenterLong,
7042 : double dfFalseEasting,
7043 : double dfFalseNorthing)
7044 :
7045 : {
7046 4 : TAKE_OPTIONAL_LOCK();
7047 :
7048 2 : return d->replaceConversionAndUnref(proj_create_conversion_gnomonic(
7049 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7050 4 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
7051 : }
7052 :
7053 : /************************************************************************/
7054 : /* OSRSetGnomonic() */
7055 : /************************************************************************/
7056 :
7057 0 : OGRErr OSRSetGnomonic(OGRSpatialReferenceH hSRS, double dfCenterLat,
7058 : double dfCenterLong, double dfFalseEasting,
7059 : double dfFalseNorthing)
7060 :
7061 : {
7062 0 : VALIDATE_POINTER1(hSRS, "OSRSetGnomonic", OGRERR_FAILURE);
7063 :
7064 0 : return ToPointer(hSRS)->SetGnomonic(dfCenterLat, dfCenterLong,
7065 0 : dfFalseEasting, dfFalseNorthing);
7066 : }
7067 :
7068 : /************************************************************************/
7069 : /* SetHOMAC() */
7070 : /************************************************************************/
7071 :
7072 : /**
7073 : * \brief Set an Hotine Oblique Mercator Azimuth Center projection using
7074 : * azimuth angle.
7075 : *
7076 : * This projection corresponds to EPSG projection method 9815, also
7077 : * sometimes known as hotine oblique mercator (variant B).
7078 : *
7079 : * This method does the same thing as the C function OSRSetHOMAC().
7080 : *
7081 : * @param dfCenterLat Latitude of the projection origin.
7082 : * @param dfCenterLong Longitude of the projection origin.
7083 : * @param dfAzimuth Azimuth, measured clockwise from North, of the projection
7084 : * centerline.
7085 : * @param dfRectToSkew Angle from Rectified to Skew Grid
7086 : * @param dfScale Scale factor applies to the projection origin.
7087 : * @param dfFalseEasting False easting.
7088 : * @param dfFalseNorthing False northing.
7089 : *
7090 : * @return OGRERR_NONE on success.
7091 : */
7092 :
7093 4 : OGRErr OGRSpatialReference::SetHOMAC(double dfCenterLat, double dfCenterLong,
7094 : double dfAzimuth, double dfRectToSkew,
7095 : double dfScale, double dfFalseEasting,
7096 : double dfFalseNorthing)
7097 :
7098 : {
7099 8 : TAKE_OPTIONAL_LOCK();
7100 :
7101 4 : return d->replaceConversionAndUnref(
7102 : proj_create_conversion_hotine_oblique_mercator_variant_b(
7103 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfAzimuth,
7104 : dfRectToSkew, dfScale, dfFalseEasting, dfFalseNorthing, nullptr,
7105 8 : 0.0, nullptr, 0.0));
7106 : }
7107 :
7108 : /************************************************************************/
7109 : /* OSRSetHOMAC() */
7110 : /************************************************************************/
7111 :
7112 : /**
7113 : * \brief Set an Oblique Mercator projection using azimuth angle.
7114 : *
7115 : * This is the same as the C++ method OGRSpatialReference::SetHOMAC()
7116 : */
7117 0 : OGRErr OSRSetHOMAC(OGRSpatialReferenceH hSRS, double dfCenterLat,
7118 : double dfCenterLong, double dfAzimuth, double dfRectToSkew,
7119 : double dfScale, double dfFalseEasting,
7120 : double dfFalseNorthing)
7121 :
7122 : {
7123 0 : VALIDATE_POINTER1(hSRS, "OSRSetHOMAC", OGRERR_FAILURE);
7124 :
7125 0 : return ToPointer(hSRS)->SetHOMAC(dfCenterLat, dfCenterLong, dfAzimuth,
7126 : dfRectToSkew, dfScale, dfFalseEasting,
7127 0 : dfFalseNorthing);
7128 : }
7129 :
7130 : /************************************************************************/
7131 : /* SetHOM() */
7132 : /************************************************************************/
7133 :
7134 : /**
7135 : * \brief Set a Hotine Oblique Mercator projection using azimuth angle.
7136 : *
7137 : * This projection corresponds to EPSG projection method 9812, also
7138 : * sometimes known as hotine oblique mercator (variant A)..
7139 : *
7140 : * This method does the same thing as the C function OSRSetHOM().
7141 : *
7142 : * @param dfCenterLat Latitude of the projection origin.
7143 : * @param dfCenterLong Longitude of the projection origin.
7144 : * @param dfAzimuth Azimuth, measured clockwise from North, of the projection
7145 : * centerline.
7146 : * @param dfRectToSkew Angle from Rectified to Skew Grid
7147 : * @param dfScale Scale factor applies to the projection origin.
7148 : * @param dfFalseEasting False easting.
7149 : * @param dfFalseNorthing False northing.
7150 : *
7151 : * @return OGRERR_NONE on success.
7152 : */
7153 :
7154 13 : OGRErr OGRSpatialReference::SetHOM(double dfCenterLat, double dfCenterLong,
7155 : double dfAzimuth, double dfRectToSkew,
7156 : double dfScale, double dfFalseEasting,
7157 : double dfFalseNorthing)
7158 :
7159 : {
7160 26 : TAKE_OPTIONAL_LOCK();
7161 :
7162 13 : return d->replaceConversionAndUnref(
7163 : proj_create_conversion_hotine_oblique_mercator_variant_a(
7164 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfAzimuth,
7165 : dfRectToSkew, dfScale, dfFalseEasting, dfFalseNorthing, nullptr,
7166 26 : 0.0, nullptr, 0.0));
7167 : }
7168 :
7169 : /************************************************************************/
7170 : /* OSRSetHOM() */
7171 : /************************************************************************/
7172 : /**
7173 : * \brief Set a Hotine Oblique Mercator projection using azimuth angle.
7174 : *
7175 : * This is the same as the C++ method OGRSpatialReference::SetHOM()
7176 : */
7177 0 : OGRErr OSRSetHOM(OGRSpatialReferenceH hSRS, double dfCenterLat,
7178 : double dfCenterLong, double dfAzimuth, double dfRectToSkew,
7179 : double dfScale, double dfFalseEasting, double dfFalseNorthing)
7180 :
7181 : {
7182 0 : VALIDATE_POINTER1(hSRS, "OSRSetHOM", OGRERR_FAILURE);
7183 :
7184 0 : return ToPointer(hSRS)->SetHOM(dfCenterLat, dfCenterLong, dfAzimuth,
7185 : dfRectToSkew, dfScale, dfFalseEasting,
7186 0 : dfFalseNorthing);
7187 : }
7188 :
7189 : /************************************************************************/
7190 : /* SetHOM2PNO() */
7191 : /************************************************************************/
7192 :
7193 : /**
7194 : * \brief Set a Hotine Oblique Mercator projection using two points on
7195 : * projection centerline.
7196 : *
7197 : * This method does the same thing as the C function OSRSetHOM2PNO().
7198 : *
7199 : * @param dfCenterLat Latitude of the projection origin.
7200 : * @param dfLat1 Latitude of the first point on center line.
7201 : * @param dfLong1 Longitude of the first point on center line.
7202 : * @param dfLat2 Latitude of the second point on center line.
7203 : * @param dfLong2 Longitude of the second point on center line.
7204 : * @param dfScale Scale factor applies to the projection origin.
7205 : * @param dfFalseEasting False easting.
7206 : * @param dfFalseNorthing False northing.
7207 : *
7208 : * @return OGRERR_NONE on success.
7209 : */
7210 :
7211 3 : OGRErr OGRSpatialReference::SetHOM2PNO(double dfCenterLat, double dfLat1,
7212 : double dfLong1, double dfLat2,
7213 : double dfLong2, double dfScale,
7214 : double dfFalseEasting,
7215 : double dfFalseNorthing)
7216 :
7217 : {
7218 6 : TAKE_OPTIONAL_LOCK();
7219 :
7220 3 : return d->replaceConversionAndUnref(
7221 : proj_create_conversion_hotine_oblique_mercator_two_point_natural_origin(
7222 : d->getPROJContext(), dfCenterLat, dfLat1, dfLong1, dfLat2, dfLong2,
7223 : dfScale, dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr,
7224 6 : 0.0));
7225 : }
7226 :
7227 : /************************************************************************/
7228 : /* OSRSetHOM2PNO() */
7229 : /************************************************************************/
7230 : /**
7231 : * \brief Set a Hotine Oblique Mercator projection using two points on
7232 : * projection centerline.
7233 : *
7234 : * This is the same as the C++ method OGRSpatialReference::SetHOM2PNO()
7235 : */
7236 0 : OGRErr OSRSetHOM2PNO(OGRSpatialReferenceH hSRS, double dfCenterLat,
7237 : double dfLat1, double dfLong1, double dfLat2,
7238 : double dfLong2, double dfScale, double dfFalseEasting,
7239 : double dfFalseNorthing)
7240 :
7241 : {
7242 0 : VALIDATE_POINTER1(hSRS, "OSRSetHOM2PNO", OGRERR_FAILURE);
7243 :
7244 0 : return ToPointer(hSRS)->SetHOM2PNO(dfCenterLat, dfLat1, dfLong1, dfLat2,
7245 : dfLong2, dfScale, dfFalseEasting,
7246 0 : dfFalseNorthing);
7247 : }
7248 :
7249 : /************************************************************************/
7250 : /* SetLOM() */
7251 : /************************************************************************/
7252 :
7253 : /**
7254 : * \brief Set a Laborde Oblique Mercator projection.
7255 : *
7256 : * @param dfCenterLat Latitude of the projection origin.
7257 : * @param dfCenterLong Longitude of the projection origin.
7258 : * @param dfAzimuth Azimuth, measured clockwise from North, of the projection
7259 : * centerline.
7260 : * @param dfScale Scale factor on the initiali line
7261 : * @param dfFalseEasting False easting.
7262 : * @param dfFalseNorthing False northing.
7263 : *
7264 : * @return OGRERR_NONE on success.
7265 : */
7266 :
7267 0 : OGRErr OGRSpatialReference::SetLOM(double dfCenterLat, double dfCenterLong,
7268 : double dfAzimuth, double dfScale,
7269 : double dfFalseEasting,
7270 : double dfFalseNorthing)
7271 :
7272 : {
7273 0 : TAKE_OPTIONAL_LOCK();
7274 :
7275 0 : return d->replaceConversionAndUnref(
7276 : proj_create_conversion_laborde_oblique_mercator(
7277 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfAzimuth, dfScale,
7278 0 : dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
7279 : }
7280 :
7281 : /************************************************************************/
7282 : /* SetIWMPolyconic() */
7283 : /************************************************************************/
7284 :
7285 0 : OGRErr OGRSpatialReference::SetIWMPolyconic(double dfLat1, double dfLat2,
7286 : double dfCenterLong,
7287 : double dfFalseEasting,
7288 : double dfFalseNorthing)
7289 :
7290 : {
7291 0 : TAKE_OPTIONAL_LOCK();
7292 :
7293 0 : return d->replaceConversionAndUnref(
7294 : proj_create_conversion_international_map_world_polyconic(
7295 : d->getPROJContext(), dfCenterLong, dfLat1, dfLat2, dfFalseEasting,
7296 0 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
7297 : }
7298 :
7299 : /************************************************************************/
7300 : /* OSRSetIWMPolyconic() */
7301 : /************************************************************************/
7302 :
7303 0 : OGRErr OSRSetIWMPolyconic(OGRSpatialReferenceH hSRS, double dfLat1,
7304 : double dfLat2, double dfCenterLong,
7305 : double dfFalseEasting, double dfFalseNorthing)
7306 :
7307 : {
7308 0 : VALIDATE_POINTER1(hSRS, "OSRSetIWMPolyconic", OGRERR_FAILURE);
7309 :
7310 0 : return ToPointer(hSRS)->SetIWMPolyconic(dfLat1, dfLat2, dfCenterLong,
7311 0 : dfFalseEasting, dfFalseNorthing);
7312 : }
7313 :
7314 : /************************************************************************/
7315 : /* SetKrovak() */
7316 : /************************************************************************/
7317 :
7318 : /** Krovak east-north projection.
7319 : *
7320 : * Note that dfAzimuth and dfPseudoStdParallel1 are ignored when exporting
7321 : * to PROJ and should be respectively set to 30.28813972222222 and 78.5
7322 : */
7323 3 : OGRErr OGRSpatialReference::SetKrovak(double dfCenterLat, double dfCenterLong,
7324 : double dfAzimuth,
7325 : double dfPseudoStdParallel1,
7326 : double dfScale, double dfFalseEasting,
7327 : double dfFalseNorthing)
7328 :
7329 : {
7330 6 : TAKE_OPTIONAL_LOCK();
7331 :
7332 3 : return d->replaceConversionAndUnref(
7333 : proj_create_conversion_krovak_north_oriented(
7334 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfAzimuth,
7335 : dfPseudoStdParallel1, dfScale, dfFalseEasting, dfFalseNorthing,
7336 6 : nullptr, 0.0, nullptr, 0.0));
7337 : }
7338 :
7339 : /************************************************************************/
7340 : /* OSRSetKrovak() */
7341 : /************************************************************************/
7342 :
7343 0 : OGRErr OSRSetKrovak(OGRSpatialReferenceH hSRS, double dfCenterLat,
7344 : double dfCenterLong, double dfAzimuth,
7345 : double dfPseudoStdParallel1, double dfScale,
7346 : double dfFalseEasting, double dfFalseNorthing)
7347 :
7348 : {
7349 0 : VALIDATE_POINTER1(hSRS, "OSRSetKrovak", OGRERR_FAILURE);
7350 :
7351 0 : return ToPointer(hSRS)->SetKrovak(dfCenterLat, dfCenterLong, dfAzimuth,
7352 : dfPseudoStdParallel1, dfScale,
7353 0 : dfFalseEasting, dfFalseNorthing);
7354 : }
7355 :
7356 : /************************************************************************/
7357 : /* SetLAEA() */
7358 : /************************************************************************/
7359 :
7360 17 : OGRErr OGRSpatialReference::SetLAEA(double dfCenterLat, double dfCenterLong,
7361 : double dfFalseEasting,
7362 : double dfFalseNorthing)
7363 :
7364 : {
7365 34 : TAKE_OPTIONAL_LOCK();
7366 :
7367 17 : auto conv = proj_create_conversion_lambert_azimuthal_equal_area(
7368 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7369 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
7370 :
7371 17 : const char *pszName = nullptr;
7372 17 : double dfConvFactor = GetTargetLinearUnits(nullptr, &pszName);
7373 17 : CPLString osName = pszName ? pszName : "";
7374 :
7375 17 : d->refreshProjObj();
7376 :
7377 17 : d->demoteFromBoundCRS();
7378 :
7379 17 : auto cs = proj_create_cartesian_2D_cs(
7380 : d->getPROJContext(),
7381 17 : std::fabs(dfCenterLat - 90) < 1e-10 && dfCenterLong == 0
7382 : ? PJ_CART2D_NORTH_POLE_EASTING_SOUTH_NORTHING_SOUTH
7383 0 : : std::fabs(dfCenterLat - -90) < 1e-10 && dfCenterLong == 0
7384 14 : ? PJ_CART2D_SOUTH_POLE_EASTING_NORTH_NORTHING_NORTH
7385 : : PJ_CART2D_EASTING_NORTHING,
7386 17 : !osName.empty() ? osName.c_str() : nullptr, dfConvFactor);
7387 : auto projCRS =
7388 17 : proj_create_projected_crs(d->getPROJContext(), d->getProjCRSName(),
7389 17 : d->getGeodBaseCRS(), conv, cs);
7390 17 : proj_destroy(conv);
7391 17 : proj_destroy(cs);
7392 :
7393 17 : d->setPjCRS(projCRS);
7394 :
7395 17 : d->undoDemoteFromBoundCRS();
7396 :
7397 34 : return OGRERR_NONE;
7398 : }
7399 :
7400 : /************************************************************************/
7401 : /* OSRSetLAEA() */
7402 : /************************************************************************/
7403 :
7404 0 : OGRErr OSRSetLAEA(OGRSpatialReferenceH hSRS, double dfCenterLat,
7405 : double dfCenterLong, double dfFalseEasting,
7406 : double dfFalseNorthing)
7407 :
7408 : {
7409 0 : VALIDATE_POINTER1(hSRS, "OSRSetLAEA", OGRERR_FAILURE);
7410 :
7411 0 : return ToPointer(hSRS)->SetLAEA(dfCenterLat, dfCenterLong, dfFalseEasting,
7412 0 : dfFalseNorthing);
7413 : }
7414 :
7415 : /************************************************************************/
7416 : /* SetLCC() */
7417 : /************************************************************************/
7418 :
7419 151 : OGRErr OGRSpatialReference::SetLCC(double dfStdP1, double dfStdP2,
7420 : double dfCenterLat, double dfCenterLong,
7421 : double dfFalseEasting,
7422 : double dfFalseNorthing)
7423 :
7424 : {
7425 302 : TAKE_OPTIONAL_LOCK();
7426 :
7427 151 : return d->replaceConversionAndUnref(
7428 : proj_create_conversion_lambert_conic_conformal_2sp(
7429 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfStdP1, dfStdP2,
7430 302 : dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0));
7431 : }
7432 :
7433 : /************************************************************************/
7434 : /* OSRSetLCC() */
7435 : /************************************************************************/
7436 :
7437 1 : OGRErr OSRSetLCC(OGRSpatialReferenceH hSRS, double dfStdP1, double dfStdP2,
7438 : double dfCenterLat, double dfCenterLong, double dfFalseEasting,
7439 : double dfFalseNorthing)
7440 :
7441 : {
7442 1 : VALIDATE_POINTER1(hSRS, "OSRSetLCC", OGRERR_FAILURE);
7443 :
7444 1 : return ToPointer(hSRS)->SetLCC(dfStdP1, dfStdP2, dfCenterLat, dfCenterLong,
7445 1 : dfFalseEasting, dfFalseNorthing);
7446 : }
7447 :
7448 : /************************************************************************/
7449 : /* SetLCC1SP() */
7450 : /************************************************************************/
7451 :
7452 10 : OGRErr OGRSpatialReference::SetLCC1SP(double dfCenterLat, double dfCenterLong,
7453 : double dfScale, double dfFalseEasting,
7454 : double dfFalseNorthing)
7455 :
7456 : {
7457 20 : TAKE_OPTIONAL_LOCK();
7458 :
7459 10 : return d->replaceConversionAndUnref(
7460 : proj_create_conversion_lambert_conic_conformal_1sp(
7461 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale,
7462 20 : dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0));
7463 : }
7464 :
7465 : /************************************************************************/
7466 : /* OSRSetLCC1SP() */
7467 : /************************************************************************/
7468 :
7469 0 : OGRErr OSRSetLCC1SP(OGRSpatialReferenceH hSRS, double dfCenterLat,
7470 : double dfCenterLong, double dfScale, double dfFalseEasting,
7471 : double dfFalseNorthing)
7472 :
7473 : {
7474 0 : VALIDATE_POINTER1(hSRS, "OSRSetLCC1SP", OGRERR_FAILURE);
7475 :
7476 0 : return ToPointer(hSRS)->SetLCC1SP(dfCenterLat, dfCenterLong, dfScale,
7477 0 : dfFalseEasting, dfFalseNorthing);
7478 : }
7479 :
7480 : /************************************************************************/
7481 : /* SetLCCB() */
7482 : /************************************************************************/
7483 :
7484 2 : OGRErr OGRSpatialReference::SetLCCB(double dfStdP1, double dfStdP2,
7485 : double dfCenterLat, double dfCenterLong,
7486 : double dfFalseEasting,
7487 : double dfFalseNorthing)
7488 :
7489 : {
7490 4 : TAKE_OPTIONAL_LOCK();
7491 :
7492 2 : return d->replaceConversionAndUnref(
7493 : proj_create_conversion_lambert_conic_conformal_2sp_belgium(
7494 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfStdP1, dfStdP2,
7495 4 : dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0));
7496 : }
7497 :
7498 : /************************************************************************/
7499 : /* OSRSetLCCB() */
7500 : /************************************************************************/
7501 :
7502 0 : OGRErr OSRSetLCCB(OGRSpatialReferenceH hSRS, double dfStdP1, double dfStdP2,
7503 : double dfCenterLat, double dfCenterLong,
7504 : double dfFalseEasting, double dfFalseNorthing)
7505 :
7506 : {
7507 0 : VALIDATE_POINTER1(hSRS, "OSRSetLCCB", OGRERR_FAILURE);
7508 :
7509 0 : return ToPointer(hSRS)->SetLCCB(dfStdP1, dfStdP2, dfCenterLat, dfCenterLong,
7510 0 : dfFalseEasting, dfFalseNorthing);
7511 : }
7512 :
7513 : /************************************************************************/
7514 : /* SetMC() */
7515 : /************************************************************************/
7516 :
7517 4 : OGRErr OGRSpatialReference::SetMC(double dfCenterLat, double dfCenterLong,
7518 : double dfFalseEasting, double dfFalseNorthing)
7519 :
7520 : {
7521 8 : TAKE_OPTIONAL_LOCK();
7522 :
7523 : (void)dfCenterLat; // ignored
7524 :
7525 4 : return d->replaceConversionAndUnref(
7526 : proj_create_conversion_miller_cylindrical(
7527 : d->getPROJContext(), dfCenterLong, dfFalseEasting, dfFalseNorthing,
7528 8 : nullptr, 0, nullptr, 0));
7529 : }
7530 :
7531 : /************************************************************************/
7532 : /* OSRSetMC() */
7533 : /************************************************************************/
7534 :
7535 0 : OGRErr OSRSetMC(OGRSpatialReferenceH hSRS, double dfCenterLat,
7536 : double dfCenterLong, double dfFalseEasting,
7537 : double dfFalseNorthing)
7538 :
7539 : {
7540 0 : VALIDATE_POINTER1(hSRS, "OSRSetMC", OGRERR_FAILURE);
7541 :
7542 0 : return ToPointer(hSRS)->SetMC(dfCenterLat, dfCenterLong, dfFalseEasting,
7543 0 : dfFalseNorthing);
7544 : }
7545 :
7546 : /************************************************************************/
7547 : /* SetMercator() */
7548 : /************************************************************************/
7549 :
7550 59 : OGRErr OGRSpatialReference::SetMercator(double dfCenterLat, double dfCenterLong,
7551 : double dfScale, double dfFalseEasting,
7552 : double dfFalseNorthing)
7553 :
7554 : {
7555 118 : TAKE_OPTIONAL_LOCK();
7556 :
7557 59 : if (dfCenterLat != 0.0 && dfScale == 1.0)
7558 : {
7559 : // Not sure this is correct, but this is how it has been used
7560 : // historically
7561 0 : return SetMercator2SP(dfCenterLat, 0.0, dfCenterLong, dfFalseEasting,
7562 0 : dfFalseNorthing);
7563 : }
7564 59 : return d->replaceConversionAndUnref(
7565 : proj_create_conversion_mercator_variant_a(
7566 : d->getPROJContext(),
7567 : dfCenterLat, // should be zero
7568 : dfCenterLong, dfScale, dfFalseEasting, dfFalseNorthing, nullptr, 0,
7569 59 : nullptr, 0));
7570 : }
7571 :
7572 : /************************************************************************/
7573 : /* OSRSetMercator() */
7574 : /************************************************************************/
7575 :
7576 2 : OGRErr OSRSetMercator(OGRSpatialReferenceH hSRS, double dfCenterLat,
7577 : double dfCenterLong, double dfScale,
7578 : double dfFalseEasting, double dfFalseNorthing)
7579 :
7580 : {
7581 2 : VALIDATE_POINTER1(hSRS, "OSRSetMercator", OGRERR_FAILURE);
7582 :
7583 2 : return ToPointer(hSRS)->SetMercator(dfCenterLat, dfCenterLong, dfScale,
7584 2 : dfFalseEasting, dfFalseNorthing);
7585 : }
7586 :
7587 : /************************************************************************/
7588 : /* SetMercator2SP() */
7589 : /************************************************************************/
7590 :
7591 31 : OGRErr OGRSpatialReference::SetMercator2SP(double dfStdP1, double dfCenterLat,
7592 : double dfCenterLong,
7593 : double dfFalseEasting,
7594 : double dfFalseNorthing)
7595 :
7596 : {
7597 31 : if (dfCenterLat == 0.0)
7598 : {
7599 30 : return d->replaceConversionAndUnref(
7600 : proj_create_conversion_mercator_variant_b(
7601 : d->getPROJContext(), dfStdP1, dfCenterLong, dfFalseEasting,
7602 30 : dfFalseNorthing, nullptr, 0, nullptr, 0));
7603 : }
7604 :
7605 1 : TAKE_OPTIONAL_LOCK();
7606 :
7607 1 : SetProjection(SRS_PT_MERCATOR_2SP);
7608 :
7609 1 : SetNormProjParm(SRS_PP_STANDARD_PARALLEL_1, dfStdP1);
7610 1 : SetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, dfCenterLat);
7611 1 : SetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, dfCenterLong);
7612 1 : SetNormProjParm(SRS_PP_FALSE_EASTING, dfFalseEasting);
7613 1 : SetNormProjParm(SRS_PP_FALSE_NORTHING, dfFalseNorthing);
7614 :
7615 1 : return OGRERR_NONE;
7616 : }
7617 :
7618 : /************************************************************************/
7619 : /* OSRSetMercator2SP() */
7620 : /************************************************************************/
7621 :
7622 1 : OGRErr OSRSetMercator2SP(OGRSpatialReferenceH hSRS, double dfStdP1,
7623 : double dfCenterLat, double dfCenterLong,
7624 : double dfFalseEasting, double dfFalseNorthing)
7625 :
7626 : {
7627 1 : VALIDATE_POINTER1(hSRS, "OSRSetMercator2SP", OGRERR_FAILURE);
7628 :
7629 1 : return ToPointer(hSRS)->SetMercator2SP(dfStdP1, dfCenterLat, dfCenterLong,
7630 1 : dfFalseEasting, dfFalseNorthing);
7631 : }
7632 :
7633 : /************************************************************************/
7634 : /* SetMollweide() */
7635 : /************************************************************************/
7636 :
7637 3 : OGRErr OGRSpatialReference::SetMollweide(double dfCentralMeridian,
7638 : double dfFalseEasting,
7639 : double dfFalseNorthing)
7640 :
7641 : {
7642 6 : TAKE_OPTIONAL_LOCK();
7643 :
7644 3 : return d->replaceConversionAndUnref(proj_create_conversion_mollweide(
7645 : d->getPROJContext(), dfCentralMeridian, dfFalseEasting, dfFalseNorthing,
7646 6 : nullptr, 0, nullptr, 0));
7647 : }
7648 :
7649 : /************************************************************************/
7650 : /* OSRSetMollweide() */
7651 : /************************************************************************/
7652 :
7653 0 : OGRErr OSRSetMollweide(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
7654 : double dfFalseEasting, double dfFalseNorthing)
7655 :
7656 : {
7657 0 : VALIDATE_POINTER1(hSRS, "OSRSetMollweide", OGRERR_FAILURE);
7658 :
7659 0 : return ToPointer(hSRS)->SetMollweide(dfCentralMeridian, dfFalseEasting,
7660 0 : dfFalseNorthing);
7661 : }
7662 :
7663 : /************************************************************************/
7664 : /* SetNZMG() */
7665 : /************************************************************************/
7666 :
7667 7 : OGRErr OGRSpatialReference::SetNZMG(double dfCenterLat, double dfCenterLong,
7668 : double dfFalseEasting,
7669 : double dfFalseNorthing)
7670 :
7671 : {
7672 14 : TAKE_OPTIONAL_LOCK();
7673 :
7674 7 : return d->replaceConversionAndUnref(
7675 : proj_create_conversion_new_zealand_mapping_grid(
7676 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7677 14 : dfFalseNorthing, nullptr, 0, nullptr, 0));
7678 : }
7679 :
7680 : /************************************************************************/
7681 : /* OSRSetNZMG() */
7682 : /************************************************************************/
7683 :
7684 0 : OGRErr OSRSetNZMG(OGRSpatialReferenceH hSRS, double dfCenterLat,
7685 : double dfCenterLong, double dfFalseEasting,
7686 : double dfFalseNorthing)
7687 :
7688 : {
7689 0 : VALIDATE_POINTER1(hSRS, "OSRSetNZMG", OGRERR_FAILURE);
7690 :
7691 0 : return ToPointer(hSRS)->SetNZMG(dfCenterLat, dfCenterLong, dfFalseEasting,
7692 0 : dfFalseNorthing);
7693 : }
7694 :
7695 : /************************************************************************/
7696 : /* SetOS() */
7697 : /************************************************************************/
7698 :
7699 6 : OGRErr OGRSpatialReference::SetOS(double dfOriginLat, double dfCMeridian,
7700 : double dfScale, double dfFalseEasting,
7701 : double dfFalseNorthing)
7702 :
7703 : {
7704 12 : TAKE_OPTIONAL_LOCK();
7705 :
7706 6 : return d->replaceConversionAndUnref(
7707 : proj_create_conversion_oblique_stereographic(
7708 : d->getPROJContext(), dfOriginLat, dfCMeridian, dfScale,
7709 12 : dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0));
7710 : }
7711 :
7712 : /************************************************************************/
7713 : /* OSRSetOS() */
7714 : /************************************************************************/
7715 :
7716 0 : OGRErr OSRSetOS(OGRSpatialReferenceH hSRS, double dfOriginLat,
7717 : double dfCMeridian, double dfScale, double dfFalseEasting,
7718 : double dfFalseNorthing)
7719 :
7720 : {
7721 0 : VALIDATE_POINTER1(hSRS, "OSRSetOS", OGRERR_FAILURE);
7722 :
7723 0 : return ToPointer(hSRS)->SetOS(dfOriginLat, dfCMeridian, dfScale,
7724 0 : dfFalseEasting, dfFalseNorthing);
7725 : }
7726 :
7727 : /************************************************************************/
7728 : /* SetOrthographic() */
7729 : /************************************************************************/
7730 :
7731 7 : OGRErr OGRSpatialReference::SetOrthographic(double dfCenterLat,
7732 : double dfCenterLong,
7733 : double dfFalseEasting,
7734 : double dfFalseNorthing)
7735 :
7736 : {
7737 14 : TAKE_OPTIONAL_LOCK();
7738 :
7739 7 : return d->replaceConversionAndUnref(proj_create_conversion_orthographic(
7740 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7741 14 : dfFalseNorthing, nullptr, 0, nullptr, 0));
7742 : }
7743 :
7744 : /************************************************************************/
7745 : /* OSRSetOrthographic() */
7746 : /************************************************************************/
7747 :
7748 1 : OGRErr OSRSetOrthographic(OGRSpatialReferenceH hSRS, double dfCenterLat,
7749 : double dfCenterLong, double dfFalseEasting,
7750 : double dfFalseNorthing)
7751 :
7752 : {
7753 1 : VALIDATE_POINTER1(hSRS, "OSRSetOrthographic", OGRERR_FAILURE);
7754 :
7755 1 : return ToPointer(hSRS)->SetOrthographic(dfCenterLat, dfCenterLong,
7756 1 : dfFalseEasting, dfFalseNorthing);
7757 : }
7758 :
7759 : /************************************************************************/
7760 : /* SetPolyconic() */
7761 : /************************************************************************/
7762 :
7763 7 : OGRErr OGRSpatialReference::SetPolyconic(double dfCenterLat,
7764 : double dfCenterLong,
7765 : double dfFalseEasting,
7766 : double dfFalseNorthing)
7767 :
7768 : {
7769 14 : TAKE_OPTIONAL_LOCK();
7770 :
7771 : // note: it seems that by some definitions this should include a
7772 : // scale_factor parameter.
7773 7 : return d->replaceConversionAndUnref(
7774 : proj_create_conversion_american_polyconic(
7775 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7776 14 : dfFalseNorthing, nullptr, 0, nullptr, 0));
7777 : }
7778 :
7779 : /************************************************************************/
7780 : /* OSRSetPolyconic() */
7781 : /************************************************************************/
7782 :
7783 0 : OGRErr OSRSetPolyconic(OGRSpatialReferenceH hSRS, double dfCenterLat,
7784 : double dfCenterLong, double dfFalseEasting,
7785 : double dfFalseNorthing)
7786 :
7787 : {
7788 0 : VALIDATE_POINTER1(hSRS, "OSRSetPolyconic", OGRERR_FAILURE);
7789 :
7790 0 : return ToPointer(hSRS)->SetPolyconic(dfCenterLat, dfCenterLong,
7791 0 : dfFalseEasting, dfFalseNorthing);
7792 : }
7793 :
7794 : /************************************************************************/
7795 : /* SetPS() */
7796 : /************************************************************************/
7797 :
7798 : /** Sets a Polar Stereographic projection.
7799 : *
7800 : * Two variants are possible:
7801 : * - Polar Stereographic Variant A: dfCenterLat must be +/- 90° and is
7802 : * interpreted as the latitude of origin, combined with the scale factor
7803 : * - Polar Stereographic Variant B: dfCenterLat is different from +/- 90° and
7804 : * is interpreted as the latitude of true scale. In that situation, dfScale
7805 : * must be set to 1 (it is ignored in the projection parameters)
7806 : */
7807 30 : OGRErr OGRSpatialReference::SetPS(double dfCenterLat, double dfCenterLong,
7808 : double dfScale, double dfFalseEasting,
7809 : double dfFalseNorthing)
7810 :
7811 : {
7812 60 : TAKE_OPTIONAL_LOCK();
7813 :
7814 : PJ *conv;
7815 30 : if (dfScale == 1.0 && std::abs(std::abs(dfCenterLat) - 90) > 1e-8)
7816 : {
7817 20 : conv = proj_create_conversion_polar_stereographic_variant_b(
7818 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7819 : dfFalseNorthing, nullptr, 0, nullptr, 0);
7820 : }
7821 : else
7822 : {
7823 10 : conv = proj_create_conversion_polar_stereographic_variant_a(
7824 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale,
7825 : dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0);
7826 : }
7827 :
7828 30 : const char *pszName = nullptr;
7829 30 : double dfConvFactor = GetTargetLinearUnits(nullptr, &pszName);
7830 30 : CPLString osName = pszName ? pszName : "";
7831 :
7832 30 : d->refreshProjObj();
7833 :
7834 30 : d->demoteFromBoundCRS();
7835 :
7836 30 : auto cs = proj_create_cartesian_2D_cs(
7837 : d->getPROJContext(),
7838 : dfCenterLat > 0 ? PJ_CART2D_NORTH_POLE_EASTING_SOUTH_NORTHING_SOUTH
7839 : : PJ_CART2D_SOUTH_POLE_EASTING_NORTH_NORTHING_NORTH,
7840 30 : !osName.empty() ? osName.c_str() : nullptr, dfConvFactor);
7841 : auto projCRS =
7842 30 : proj_create_projected_crs(d->getPROJContext(), d->getProjCRSName(),
7843 30 : d->getGeodBaseCRS(), conv, cs);
7844 30 : proj_destroy(conv);
7845 30 : proj_destroy(cs);
7846 :
7847 30 : d->setPjCRS(projCRS);
7848 :
7849 30 : d->undoDemoteFromBoundCRS();
7850 :
7851 60 : return OGRERR_NONE;
7852 : }
7853 :
7854 : /************************************************************************/
7855 : /* OSRSetPS() */
7856 : /************************************************************************/
7857 :
7858 1 : OGRErr OSRSetPS(OGRSpatialReferenceH hSRS, double dfCenterLat,
7859 : double dfCenterLong, double dfScale, double dfFalseEasting,
7860 : double dfFalseNorthing)
7861 :
7862 : {
7863 1 : VALIDATE_POINTER1(hSRS, "OSRSetPS", OGRERR_FAILURE);
7864 :
7865 1 : return ToPointer(hSRS)->SetPS(dfCenterLat, dfCenterLong, dfScale,
7866 1 : dfFalseEasting, dfFalseNorthing);
7867 : }
7868 :
7869 : /************************************************************************/
7870 : /* SetRobinson() */
7871 : /************************************************************************/
7872 :
7873 4 : OGRErr OGRSpatialReference::SetRobinson(double dfCenterLong,
7874 : double dfFalseEasting,
7875 : double dfFalseNorthing)
7876 :
7877 : {
7878 8 : TAKE_OPTIONAL_LOCK();
7879 :
7880 4 : return d->replaceConversionAndUnref(proj_create_conversion_robinson(
7881 : d->getPROJContext(), dfCenterLong, dfFalseEasting, dfFalseNorthing,
7882 8 : nullptr, 0, nullptr, 0));
7883 : }
7884 :
7885 : /************************************************************************/
7886 : /* OSRSetRobinson() */
7887 : /************************************************************************/
7888 :
7889 0 : OGRErr OSRSetRobinson(OGRSpatialReferenceH hSRS, double dfCenterLong,
7890 : double dfFalseEasting, double dfFalseNorthing)
7891 :
7892 : {
7893 0 : VALIDATE_POINTER1(hSRS, "OSRSetRobinson", OGRERR_FAILURE);
7894 :
7895 0 : return ToPointer(hSRS)->SetRobinson(dfCenterLong, dfFalseEasting,
7896 0 : dfFalseNorthing);
7897 : }
7898 :
7899 : /************************************************************************/
7900 : /* SetSinusoidal() */
7901 : /************************************************************************/
7902 :
7903 33 : OGRErr OGRSpatialReference::SetSinusoidal(double dfCenterLong,
7904 : double dfFalseEasting,
7905 : double dfFalseNorthing)
7906 :
7907 : {
7908 66 : TAKE_OPTIONAL_LOCK();
7909 :
7910 33 : return d->replaceConversionAndUnref(proj_create_conversion_sinusoidal(
7911 : d->getPROJContext(), dfCenterLong, dfFalseEasting, dfFalseNorthing,
7912 66 : nullptr, 0, nullptr, 0));
7913 : }
7914 :
7915 : /************************************************************************/
7916 : /* OSRSetSinusoidal() */
7917 : /************************************************************************/
7918 :
7919 1 : OGRErr OSRSetSinusoidal(OGRSpatialReferenceH hSRS, double dfCenterLong,
7920 : double dfFalseEasting, double dfFalseNorthing)
7921 :
7922 : {
7923 1 : VALIDATE_POINTER1(hSRS, "OSRSetSinusoidal", OGRERR_FAILURE);
7924 :
7925 1 : return ToPointer(hSRS)->SetSinusoidal(dfCenterLong, dfFalseEasting,
7926 1 : dfFalseNorthing);
7927 : }
7928 :
7929 : /************************************************************************/
7930 : /* SetStereographic() */
7931 : /************************************************************************/
7932 :
7933 2 : OGRErr OGRSpatialReference::SetStereographic(double dfOriginLat,
7934 : double dfCMeridian, double dfScale,
7935 : double dfFalseEasting,
7936 : double dfFalseNorthing)
7937 :
7938 : {
7939 4 : TAKE_OPTIONAL_LOCK();
7940 :
7941 2 : return d->replaceConversionAndUnref(proj_create_conversion_stereographic(
7942 : d->getPROJContext(), dfOriginLat, dfCMeridian, dfScale, dfFalseEasting,
7943 4 : dfFalseNorthing, nullptr, 0, nullptr, 0));
7944 : }
7945 :
7946 : /************************************************************************/
7947 : /* OSRSetStereographic() */
7948 : /************************************************************************/
7949 :
7950 0 : OGRErr OSRSetStereographic(OGRSpatialReferenceH hSRS, double dfOriginLat,
7951 : double dfCMeridian, double dfScale,
7952 : double dfFalseEasting, double dfFalseNorthing)
7953 :
7954 : {
7955 0 : VALIDATE_POINTER1(hSRS, "OSRSetStereographic", OGRERR_FAILURE);
7956 :
7957 0 : return ToPointer(hSRS)->SetStereographic(dfOriginLat, dfCMeridian, dfScale,
7958 0 : dfFalseEasting, dfFalseNorthing);
7959 : }
7960 :
7961 : /************************************************************************/
7962 : /* SetSOC() */
7963 : /* */
7964 : /* NOTE: This definition isn't really used in practice any more */
7965 : /* and should be considered deprecated. It seems that swiss */
7966 : /* oblique mercator is now define as Hotine_Oblique_Mercator */
7967 : /* with an azimuth of 90 and a rectified_grid_angle of 90. See */
7968 : /* EPSG:2056 and Bug 423. */
7969 : /************************************************************************/
7970 :
7971 2 : OGRErr OGRSpatialReference::SetSOC(double dfLatitudeOfOrigin,
7972 : double dfCentralMeridian,
7973 : double dfFalseEasting,
7974 : double dfFalseNorthing)
7975 :
7976 : {
7977 4 : TAKE_OPTIONAL_LOCK();
7978 :
7979 2 : return d->replaceConversionAndUnref(
7980 : proj_create_conversion_hotine_oblique_mercator_variant_b(
7981 : d->getPROJContext(), dfLatitudeOfOrigin, dfCentralMeridian, 90.0,
7982 : 90.0, 1.0, dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr,
7983 4 : 0.0));
7984 : #if 0
7985 : SetProjection( SRS_PT_SWISS_OBLIQUE_CYLINDRICAL );
7986 : SetNormProjParm( SRS_PP_LATITUDE_OF_CENTER, dfLatitudeOfOrigin );
7987 : SetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, dfCentralMeridian );
7988 : SetNormProjParm( SRS_PP_FALSE_EASTING, dfFalseEasting );
7989 : SetNormProjParm( SRS_PP_FALSE_NORTHING, dfFalseNorthing );
7990 :
7991 : return OGRERR_NONE;
7992 : #endif
7993 : }
7994 :
7995 : /************************************************************************/
7996 : /* OSRSetSOC() */
7997 : /************************************************************************/
7998 :
7999 0 : OGRErr OSRSetSOC(OGRSpatialReferenceH hSRS, double dfLatitudeOfOrigin,
8000 : double dfCentralMeridian, double dfFalseEasting,
8001 : double dfFalseNorthing)
8002 :
8003 : {
8004 0 : VALIDATE_POINTER1(hSRS, "OSRSetSOC", OGRERR_FAILURE);
8005 :
8006 0 : return ToPointer(hSRS)->SetSOC(dfLatitudeOfOrigin, dfCentralMeridian,
8007 0 : dfFalseEasting, dfFalseNorthing);
8008 : }
8009 :
8010 : /************************************************************************/
8011 : /* SetVDG() */
8012 : /************************************************************************/
8013 :
8014 2 : OGRErr OGRSpatialReference::SetVDG(double dfCMeridian, double dfFalseEasting,
8015 : double dfFalseNorthing)
8016 :
8017 : {
8018 4 : TAKE_OPTIONAL_LOCK();
8019 :
8020 2 : return d->replaceConversionAndUnref(proj_create_conversion_van_der_grinten(
8021 : d->getPROJContext(), dfCMeridian, dfFalseEasting, dfFalseNorthing,
8022 4 : nullptr, 0, nullptr, 0));
8023 : }
8024 :
8025 : /************************************************************************/
8026 : /* OSRSetVDG() */
8027 : /************************************************************************/
8028 :
8029 0 : OGRErr OSRSetVDG(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
8030 : double dfFalseEasting, double dfFalseNorthing)
8031 :
8032 : {
8033 0 : VALIDATE_POINTER1(hSRS, "OSRSetVDG", OGRERR_FAILURE);
8034 :
8035 0 : return ToPointer(hSRS)->SetVDG(dfCentralMeridian, dfFalseEasting,
8036 0 : dfFalseNorthing);
8037 : }
8038 :
8039 : /************************************************************************/
8040 : /* SetUTM() */
8041 : /************************************************************************/
8042 :
8043 : /**
8044 : * \brief Set UTM projection definition.
8045 : *
8046 : * This will generate a projection definition with the full set of
8047 : * transverse mercator projection parameters for the given UTM zone.
8048 : * If no PROJCS[] description is set yet, one will be set to look
8049 : * like "UTM Zone %d, {Northern, Southern} Hemisphere".
8050 : *
8051 : * This method is the same as the C function OSRSetUTM().
8052 : *
8053 : * @param nZone UTM zone.
8054 : *
8055 : * @param bNorth TRUE for northern hemisphere, or FALSE for southern
8056 : * hemisphere.
8057 : *
8058 : * @return OGRERR_NONE on success.
8059 : */
8060 :
8061 318 : OGRErr OGRSpatialReference::SetUTM(int nZone, int bNorth)
8062 :
8063 : {
8064 636 : TAKE_OPTIONAL_LOCK();
8065 :
8066 318 : if (nZone < 0 || nZone > 60)
8067 : {
8068 0 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid zone: %d", nZone);
8069 0 : return OGRERR_FAILURE;
8070 : }
8071 :
8072 318 : return d->replaceConversionAndUnref(
8073 318 : proj_create_conversion_utm(d->getPROJContext(), nZone, bNorth));
8074 : }
8075 :
8076 : /************************************************************************/
8077 : /* OSRSetUTM() */
8078 : /************************************************************************/
8079 :
8080 : /**
8081 : * \brief Set UTM projection definition.
8082 : *
8083 : * This is the same as the C++ method OGRSpatialReference::SetUTM()
8084 : */
8085 19 : OGRErr OSRSetUTM(OGRSpatialReferenceH hSRS, int nZone, int bNorth)
8086 :
8087 : {
8088 19 : VALIDATE_POINTER1(hSRS, "OSRSetUTM", OGRERR_FAILURE);
8089 :
8090 19 : return ToPointer(hSRS)->SetUTM(nZone, bNorth);
8091 : }
8092 :
8093 : /************************************************************************/
8094 : /* GetUTMZone() */
8095 : /* */
8096 : /* Returns zero if it isn't UTM. */
8097 : /************************************************************************/
8098 :
8099 : /**
8100 : * \brief Get utm zone information.
8101 : *
8102 : * This is the same as the C function OSRGetUTMZone().
8103 : *
8104 : * In SWIG bindings (Python, Java, etc) the GetUTMZone() method returns a
8105 : * zone which is negative in the southern hemisphere instead of having the
8106 : * pbNorth flag used in the C and C++ interface.
8107 : *
8108 : * @param pbNorth pointer to in to set to TRUE if northern hemisphere, or
8109 : * FALSE if southern.
8110 : *
8111 : * @return UTM zone number or zero if this isn't a UTM definition.
8112 : */
8113 :
8114 592 : int OGRSpatialReference::GetUTMZone(int *pbNorth) const
8115 :
8116 : {
8117 1184 : TAKE_OPTIONAL_LOCK();
8118 :
8119 592 : if (IsProjected() && GetAxesCount() == 3)
8120 : {
8121 1 : OGRSpatialReference *poSRSTmp = Clone();
8122 1 : poSRSTmp->DemoteTo2D(nullptr);
8123 1 : const int nZone = poSRSTmp->GetUTMZone(pbNorth);
8124 1 : delete poSRSTmp;
8125 1 : return nZone;
8126 : }
8127 :
8128 591 : const char *pszProjection = GetAttrValue("PROJECTION");
8129 :
8130 591 : if (pszProjection == nullptr ||
8131 517 : !EQUAL(pszProjection, SRS_PT_TRANSVERSE_MERCATOR))
8132 268 : return 0;
8133 :
8134 323 : if (GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0) != 0.0)
8135 5 : return 0;
8136 :
8137 318 : if (GetProjParm(SRS_PP_SCALE_FACTOR, 1.0) != 0.9996)
8138 15 : return 0;
8139 :
8140 303 : if (fabs(GetNormProjParm(SRS_PP_FALSE_EASTING, 0.0) - 500000.0) > 0.001)
8141 3 : return 0;
8142 :
8143 300 : const double dfFalseNorthing = GetNormProjParm(SRS_PP_FALSE_NORTHING, 0.0);
8144 :
8145 300 : if (dfFalseNorthing != 0.0 && fabs(dfFalseNorthing - 10000000.0) > 0.001)
8146 0 : return 0;
8147 :
8148 300 : if (pbNorth != nullptr)
8149 235 : *pbNorth = (dfFalseNorthing == 0);
8150 :
8151 : const double dfCentralMeridian =
8152 300 : GetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, 0.0);
8153 300 : const double dfZone = (dfCentralMeridian + 186.0) / 6.0;
8154 :
8155 600 : if (dfCentralMeridian < -177.00001 || dfCentralMeridian > 177.000001 ||
8156 900 : std::isnan(dfZone) ||
8157 300 : std::abs(dfZone - static_cast<int>(dfZone) - 0.5) > 0.00001)
8158 0 : return 0;
8159 :
8160 300 : return static_cast<int>(dfZone);
8161 : }
8162 :
8163 : /************************************************************************/
8164 : /* OSRGetUTMZone() */
8165 : /************************************************************************/
8166 :
8167 : /**
8168 : * \brief Get utm zone information.
8169 : *
8170 : * This is the same as the C++ method OGRSpatialReference::GetUTMZone()
8171 : */
8172 6 : int OSRGetUTMZone(OGRSpatialReferenceH hSRS, int *pbNorth)
8173 :
8174 : {
8175 6 : VALIDATE_POINTER1(hSRS, "OSRGetUTMZone", 0);
8176 :
8177 6 : return ToPointer(hSRS)->GetUTMZone(pbNorth);
8178 : }
8179 :
8180 : /************************************************************************/
8181 : /* SetWagner() */
8182 : /************************************************************************/
8183 :
8184 0 : OGRErr OGRSpatialReference::SetWagner(int nVariation, // 1--7.
8185 : double dfCenterLat, double dfFalseEasting,
8186 : double dfFalseNorthing)
8187 :
8188 : {
8189 0 : TAKE_OPTIONAL_LOCK();
8190 :
8191 : PJ *conv;
8192 0 : if (nVariation == 1)
8193 : {
8194 0 : conv = proj_create_conversion_wagner_i(d->getPROJContext(), 0.0,
8195 : dfFalseEasting, dfFalseNorthing,
8196 : nullptr, 0.0, nullptr, 0.0);
8197 : }
8198 0 : else if (nVariation == 2)
8199 : {
8200 0 : conv = proj_create_conversion_wagner_ii(d->getPROJContext(), 0.0,
8201 : dfFalseEasting, dfFalseNorthing,
8202 : nullptr, 0.0, nullptr, 0.0);
8203 : }
8204 0 : else if (nVariation == 3)
8205 : {
8206 0 : conv = proj_create_conversion_wagner_iii(
8207 : d->getPROJContext(), dfCenterLat, 0.0, dfFalseEasting,
8208 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
8209 : }
8210 0 : else if (nVariation == 4)
8211 : {
8212 0 : conv = proj_create_conversion_wagner_iv(d->getPROJContext(), 0.0,
8213 : dfFalseEasting, dfFalseNorthing,
8214 : nullptr, 0.0, nullptr, 0.0);
8215 : }
8216 0 : else if (nVariation == 5)
8217 : {
8218 0 : conv = proj_create_conversion_wagner_v(d->getPROJContext(), 0.0,
8219 : dfFalseEasting, dfFalseNorthing,
8220 : nullptr, 0.0, nullptr, 0.0);
8221 : }
8222 0 : else if (nVariation == 6)
8223 : {
8224 0 : conv = proj_create_conversion_wagner_vi(d->getPROJContext(), 0.0,
8225 : dfFalseEasting, dfFalseNorthing,
8226 : nullptr, 0.0, nullptr, 0.0);
8227 : }
8228 0 : else if (nVariation == 7)
8229 : {
8230 0 : conv = proj_create_conversion_wagner_vii(
8231 : d->getPROJContext(), 0.0, dfFalseEasting, dfFalseNorthing, nullptr,
8232 : 0.0, nullptr, 0.0);
8233 : }
8234 : else
8235 : {
8236 0 : CPLError(CE_Failure, CPLE_AppDefined,
8237 : "Unsupported Wagner variation (%d).", nVariation);
8238 0 : return OGRERR_UNSUPPORTED_SRS;
8239 : }
8240 :
8241 0 : return d->replaceConversionAndUnref(conv);
8242 : }
8243 :
8244 : /************************************************************************/
8245 : /* OSRSetWagner() */
8246 : /************************************************************************/
8247 :
8248 0 : OGRErr OSRSetWagner(OGRSpatialReferenceH hSRS, int nVariation,
8249 : double dfCenterLat, double dfFalseEasting,
8250 : double dfFalseNorthing)
8251 :
8252 : {
8253 0 : VALIDATE_POINTER1(hSRS, "OSRSetWagner", OGRERR_FAILURE);
8254 :
8255 0 : return ToPointer(hSRS)->SetWagner(nVariation, dfCenterLat, dfFalseEasting,
8256 0 : dfFalseNorthing);
8257 : }
8258 :
8259 : /************************************************************************/
8260 : /* SetQSC() */
8261 : /************************************************************************/
8262 :
8263 0 : OGRErr OGRSpatialReference::SetQSC(double dfCenterLat, double dfCenterLong)
8264 : {
8265 0 : TAKE_OPTIONAL_LOCK();
8266 :
8267 0 : return d->replaceConversionAndUnref(
8268 : proj_create_conversion_quadrilateralized_spherical_cube(
8269 : d->getPROJContext(), dfCenterLat, dfCenterLong, 0.0, 0.0, nullptr,
8270 0 : 0, nullptr, 0));
8271 : }
8272 :
8273 : /************************************************************************/
8274 : /* OSRSetQSC() */
8275 : /************************************************************************/
8276 :
8277 0 : OGRErr OSRSetQSC(OGRSpatialReferenceH hSRS, double dfCenterLat,
8278 : double dfCenterLong)
8279 :
8280 : {
8281 0 : VALIDATE_POINTER1(hSRS, "OSRSetQSC", OGRERR_FAILURE);
8282 :
8283 0 : return ToPointer(hSRS)->SetQSC(dfCenterLat, dfCenterLong);
8284 : }
8285 :
8286 : /************************************************************************/
8287 : /* SetSCH() */
8288 : /************************************************************************/
8289 :
8290 0 : OGRErr OGRSpatialReference::SetSCH(double dfPegLat, double dfPegLong,
8291 : double dfPegHeading, double dfPegHgt)
8292 :
8293 : {
8294 0 : TAKE_OPTIONAL_LOCK();
8295 :
8296 0 : return d->replaceConversionAndUnref(
8297 : proj_create_conversion_spherical_cross_track_height(
8298 : d->getPROJContext(), dfPegLat, dfPegLong, dfPegHeading, dfPegHgt,
8299 0 : nullptr, 0, nullptr, 0));
8300 : }
8301 :
8302 : /************************************************************************/
8303 : /* OSRSetSCH() */
8304 : /************************************************************************/
8305 :
8306 0 : OGRErr OSRSetSCH(OGRSpatialReferenceH hSRS, double dfPegLat, double dfPegLong,
8307 : double dfPegHeading, double dfPegHgt)
8308 :
8309 : {
8310 0 : VALIDATE_POINTER1(hSRS, "OSRSetSCH", OGRERR_FAILURE);
8311 :
8312 0 : return ToPointer(hSRS)->SetSCH(dfPegLat, dfPegLong, dfPegHeading, dfPegHgt);
8313 : }
8314 :
8315 : /************************************************************************/
8316 : /* SetVerticalPerspective() */
8317 : /************************************************************************/
8318 :
8319 3 : OGRErr OGRSpatialReference::SetVerticalPerspective(
8320 : double dfTopoOriginLat, double dfTopoOriginLon, double dfTopoOriginHeight,
8321 : double dfViewPointHeight, double dfFalseEasting, double dfFalseNorthing)
8322 : {
8323 6 : TAKE_OPTIONAL_LOCK();
8324 :
8325 3 : return d->replaceConversionAndUnref(
8326 : proj_create_conversion_vertical_perspective(
8327 : d->getPROJContext(), dfTopoOriginLat, dfTopoOriginLon,
8328 : dfTopoOriginHeight, dfViewPointHeight, dfFalseEasting,
8329 6 : dfFalseNorthing, nullptr, 0, nullptr, 0));
8330 : }
8331 :
8332 : /************************************************************************/
8333 : /* OSRSetVerticalPerspective() */
8334 : /************************************************************************/
8335 :
8336 1 : OGRErr OSRSetVerticalPerspective(OGRSpatialReferenceH hSRS,
8337 : double dfTopoOriginLat, double dfTopoOriginLon,
8338 : double dfTopoOriginHeight,
8339 : double dfViewPointHeight,
8340 : double dfFalseEasting, double dfFalseNorthing)
8341 :
8342 : {
8343 1 : VALIDATE_POINTER1(hSRS, "OSRSetVerticalPerspective", OGRERR_FAILURE);
8344 :
8345 1 : return ToPointer(hSRS)->SetVerticalPerspective(
8346 : dfTopoOriginLat, dfTopoOriginLon, dfTopoOriginHeight, dfViewPointHeight,
8347 1 : dfFalseEasting, dfFalseNorthing);
8348 : }
8349 :
8350 : /************************************************************************/
8351 : /* SetDerivedGeogCRSWithPoleRotationGRIBConvention() */
8352 : /************************************************************************/
8353 :
8354 2 : OGRErr OGRSpatialReference::SetDerivedGeogCRSWithPoleRotationGRIBConvention(
8355 : const char *pszCRSName, double dfSouthPoleLat, double dfSouthPoleLon,
8356 : double dfAxisRotation)
8357 : {
8358 4 : TAKE_OPTIONAL_LOCK();
8359 :
8360 2 : d->refreshProjObj();
8361 2 : if (!d->m_pj_crs)
8362 0 : return OGRERR_FAILURE;
8363 2 : if (d->m_pjType != PJ_TYPE_GEOGRAPHIC_2D_CRS)
8364 0 : return OGRERR_FAILURE;
8365 2 : auto ctxt = d->getPROJContext();
8366 2 : auto conv = proj_create_conversion_pole_rotation_grib_convention(
8367 : ctxt, dfSouthPoleLat, dfSouthPoleLon, dfAxisRotation, nullptr, 0);
8368 2 : auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
8369 4 : d->setPjCRS(proj_create_derived_geographic_crs(ctxt, pszCRSName,
8370 2 : d->m_pj_crs, conv, cs));
8371 2 : proj_destroy(conv);
8372 2 : proj_destroy(cs);
8373 2 : return OGRERR_NONE;
8374 : }
8375 :
8376 : /************************************************************************/
8377 : /* SetDerivedGeogCRSWithPoleRotationNetCDFCFConvention() */
8378 : /************************************************************************/
8379 :
8380 3 : OGRErr OGRSpatialReference::SetDerivedGeogCRSWithPoleRotationNetCDFCFConvention(
8381 : const char *pszCRSName, double dfGridNorthPoleLat,
8382 : double dfGridNorthPoleLon, double dfNorthPoleGridLon)
8383 : {
8384 3 : TAKE_OPTIONAL_LOCK();
8385 :
8386 : #if PROJ_VERSION_MAJOR > 8 || \
8387 : (PROJ_VERSION_MAJOR == 8 && PROJ_VERSION_MINOR >= 2)
8388 : d->refreshProjObj();
8389 : if (!d->m_pj_crs)
8390 : return OGRERR_FAILURE;
8391 : if (d->m_pjType != PJ_TYPE_GEOGRAPHIC_2D_CRS)
8392 : return OGRERR_FAILURE;
8393 : auto ctxt = d->getPROJContext();
8394 : auto conv = proj_create_conversion_pole_rotation_netcdf_cf_convention(
8395 : ctxt, dfGridNorthPoleLat, dfGridNorthPoleLon, dfNorthPoleGridLon,
8396 : nullptr, 0);
8397 : auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
8398 : d->setPjCRS(proj_create_derived_geographic_crs(ctxt, pszCRSName,
8399 : d->m_pj_crs, conv, cs));
8400 : proj_destroy(conv);
8401 : proj_destroy(cs);
8402 : return OGRERR_NONE;
8403 : #else
8404 : (void)pszCRSName;
8405 3 : SetProjection("Rotated_pole");
8406 3 : SetExtension(
8407 : "PROJCS", "PROJ4",
8408 : CPLSPrintf("+proj=ob_tran +o_proj=longlat +lon_0=%.17g +o_lon_p=%.17g "
8409 : "+o_lat_p=%.17g +a=%.17g +b=%.17g "
8410 : "+to_meter=0.0174532925199433 "
8411 : "+wktext",
8412 : 180.0 + dfGridNorthPoleLon, dfNorthPoleGridLon,
8413 : dfGridNorthPoleLat, GetSemiMajor(nullptr),
8414 : GetSemiMinor(nullptr)));
8415 6 : return OGRERR_NONE;
8416 : #endif
8417 : }
8418 :
8419 : /************************************************************************/
8420 : /* SetAuthority() */
8421 : /************************************************************************/
8422 :
8423 : /**
8424 : * \brief Set the authority for a node.
8425 : *
8426 : * This method is the same as the C function OSRSetAuthority().
8427 : *
8428 : * @param pszTargetKey the partial or complete path to the node to
8429 : * set an authority on. i.e. "PROJCS", "GEOGCS" or "GEOGCS|UNIT".
8430 : *
8431 : * @param pszAuthority authority name, such as "EPSG".
8432 : *
8433 : * @param nCode code for value with this authority.
8434 : *
8435 : * @return OGRERR_NONE on success.
8436 : */
8437 :
8438 11444 : OGRErr OGRSpatialReference::SetAuthority(const char *pszTargetKey,
8439 : const char *pszAuthority, int nCode)
8440 :
8441 : {
8442 22888 : TAKE_OPTIONAL_LOCK();
8443 :
8444 11444 : d->refreshProjObj();
8445 11444 : pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
8446 :
8447 11444 : if (pszTargetKey == nullptr)
8448 : {
8449 262 : if (!d->m_pj_crs)
8450 0 : return OGRERR_FAILURE;
8451 262 : CPLString osCode;
8452 262 : osCode.Printf("%d", nCode);
8453 262 : d->demoteFromBoundCRS();
8454 262 : d->setPjCRS(proj_alter_id(d->getPROJContext(), d->m_pj_crs,
8455 : pszAuthority, osCode.c_str()));
8456 262 : d->undoDemoteFromBoundCRS();
8457 262 : return OGRERR_NONE;
8458 : }
8459 :
8460 11182 : d->demoteFromBoundCRS();
8461 11182 : if (d->m_pjType == PJ_TYPE_PROJECTED_CRS && EQUAL(pszTargetKey, "GEOGCS"))
8462 : {
8463 3695 : CPLString osCode;
8464 3695 : osCode.Printf("%d", nCode);
8465 : auto newGeogCRS =
8466 3695 : proj_alter_id(d->getPROJContext(), d->getGeodBaseCRS(),
8467 : pszAuthority, osCode.c_str());
8468 :
8469 : auto conv =
8470 3695 : proj_crs_get_coordoperation(d->getPROJContext(), d->m_pj_crs);
8471 :
8472 3695 : auto projCRS = proj_create_projected_crs(
8473 : d->getPROJContext(), d->getProjCRSName(), newGeogCRS, conv,
8474 3695 : d->getProjCRSCoordSys());
8475 :
8476 : // Preserve existing id on the PROJCRS
8477 3695 : const char *pszProjCRSAuthName = proj_get_id_auth_name(d->m_pj_crs, 0);
8478 3695 : const char *pszProjCRSCode = proj_get_id_code(d->m_pj_crs, 0);
8479 3695 : if (pszProjCRSAuthName && pszProjCRSCode)
8480 : {
8481 : auto projCRSWithId =
8482 0 : proj_alter_id(d->getPROJContext(), projCRS, pszProjCRSAuthName,
8483 : pszProjCRSCode);
8484 0 : proj_destroy(projCRS);
8485 0 : projCRS = projCRSWithId;
8486 : }
8487 :
8488 3695 : proj_destroy(newGeogCRS);
8489 3695 : proj_destroy(conv);
8490 :
8491 3695 : d->setPjCRS(projCRS);
8492 3695 : d->undoDemoteFromBoundCRS();
8493 3695 : return OGRERR_NONE;
8494 : }
8495 7487 : d->undoDemoteFromBoundCRS();
8496 :
8497 : /* -------------------------------------------------------------------- */
8498 : /* Find the node below which the authority should be put. */
8499 : /* -------------------------------------------------------------------- */
8500 7487 : OGR_SRSNode *poNode = GetAttrNode(pszTargetKey);
8501 :
8502 7487 : if (poNode == nullptr)
8503 0 : return OGRERR_FAILURE;
8504 :
8505 : /* -------------------------------------------------------------------- */
8506 : /* If there is an existing AUTHORITY child blow it away before */
8507 : /* trying to set a new one. */
8508 : /* -------------------------------------------------------------------- */
8509 7487 : int iOldChild = poNode->FindChild("AUTHORITY");
8510 7487 : if (iOldChild != -1)
8511 5 : poNode->DestroyChild(iOldChild);
8512 :
8513 : /* -------------------------------------------------------------------- */
8514 : /* Create a new authority node. */
8515 : /* -------------------------------------------------------------------- */
8516 7487 : char szCode[32] = {};
8517 :
8518 7487 : snprintf(szCode, sizeof(szCode), "%d", nCode);
8519 :
8520 7487 : OGR_SRSNode *poAuthNode = new OGR_SRSNode("AUTHORITY");
8521 7487 : poAuthNode->AddChild(new OGR_SRSNode(pszAuthority));
8522 7487 : poAuthNode->AddChild(new OGR_SRSNode(szCode));
8523 :
8524 7487 : poNode->AddChild(poAuthNode);
8525 :
8526 7487 : return OGRERR_NONE;
8527 : }
8528 :
8529 : /************************************************************************/
8530 : /* OSRSetAuthority() */
8531 : /************************************************************************/
8532 :
8533 : /**
8534 : * \brief Set the authority for a node.
8535 : *
8536 : * This function is the same as OGRSpatialReference::SetAuthority().
8537 : */
8538 0 : OGRErr OSRSetAuthority(OGRSpatialReferenceH hSRS, const char *pszTargetKey,
8539 : const char *pszAuthority, int nCode)
8540 :
8541 : {
8542 0 : VALIDATE_POINTER1(hSRS, "OSRSetAuthority", OGRERR_FAILURE);
8543 :
8544 0 : return ToPointer(hSRS)->SetAuthority(pszTargetKey, pszAuthority, nCode);
8545 : }
8546 :
8547 : /************************************************************************/
8548 : /* GetAuthorityCode() */
8549 : /************************************************************************/
8550 :
8551 : /**
8552 : * \brief Get the authority code for a node.
8553 : *
8554 : * This method is used to query an AUTHORITY[] node from within the
8555 : * WKT tree, and fetch the code value.
8556 : *
8557 : * While in theory values may be non-numeric, for the EPSG authority all
8558 : * code values should be integral.
8559 : *
8560 : * This method is the same as the C function OSRGetAuthorityCode().
8561 : *
8562 : * @param pszTargetKey the partial or complete path to the node to
8563 : * get an authority from. i.e. "PROJCS", "GEOGCS", "GEOGCS|UNIT" or NULL to
8564 : * search for an authority node on the root element.
8565 : *
8566 : * @return value code from authority node, or NULL on failure. The value
8567 : * returned is internal and should not be freed or modified.
8568 : */
8569 :
8570 : const char *
8571 27979 : OGRSpatialReference::GetAuthorityCode(const char *pszTargetKey) const
8572 :
8573 : {
8574 55958 : TAKE_OPTIONAL_LOCK();
8575 :
8576 27979 : d->refreshProjObj();
8577 27979 : const char *pszInputTargetKey = pszTargetKey;
8578 27979 : pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
8579 27979 : if (pszTargetKey == nullptr)
8580 : {
8581 20278 : if (!d->m_pj_crs)
8582 : {
8583 14 : return nullptr;
8584 : }
8585 20264 : d->demoteFromBoundCRS();
8586 20264 : auto ret = proj_get_id_code(d->m_pj_crs, 0);
8587 20264 : if (ret == nullptr && d->m_pjType == PJ_TYPE_PROJECTED_CRS)
8588 : {
8589 1091 : auto ctxt = d->getPROJContext();
8590 1091 : auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
8591 1091 : if (cs)
8592 : {
8593 1091 : const int axisCount = proj_cs_get_axis_count(ctxt, cs);
8594 1091 : proj_destroy(cs);
8595 1091 : if (axisCount == 3)
8596 : {
8597 : // This might come from a COMPD_CS with a VERT_DATUM type =
8598 : // 2002 in which case, using the WKT1 representation will
8599 : // enable us to recover the EPSG code.
8600 14 : pszTargetKey = pszInputTargetKey;
8601 : }
8602 : }
8603 : }
8604 20264 : d->undoDemoteFromBoundCRS();
8605 20264 : if (ret != nullptr || pszTargetKey == nullptr)
8606 : {
8607 20264 : return ret;
8608 : }
8609 : }
8610 :
8611 : // Special key for that context
8612 7705 : else if (EQUAL(pszTargetKey, "HORIZCRS") &&
8613 4 : d->m_pjType == PJ_TYPE_COMPOUND_CRS)
8614 : {
8615 4 : auto ctxt = d->getPROJContext();
8616 4 : auto crs = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
8617 4 : if (crs)
8618 : {
8619 4 : const char *ret = proj_get_id_code(crs, 0);
8620 4 : if (ret)
8621 4 : ret = CPLSPrintf("%s", ret);
8622 4 : proj_destroy(crs);
8623 4 : return ret;
8624 : }
8625 : }
8626 7701 : else if (EQUAL(pszTargetKey, "VERTCRS") &&
8627 4 : d->m_pjType == PJ_TYPE_COMPOUND_CRS)
8628 : {
8629 4 : auto ctxt = d->getPROJContext();
8630 4 : auto crs = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 1);
8631 4 : if (crs)
8632 : {
8633 4 : const char *ret = proj_get_id_code(crs, 0);
8634 4 : if (ret)
8635 4 : ret = CPLSPrintf("%s", ret);
8636 4 : proj_destroy(crs);
8637 4 : return ret;
8638 : }
8639 : }
8640 :
8641 : /* -------------------------------------------------------------------- */
8642 : /* Find the node below which the authority should be put. */
8643 : /* -------------------------------------------------------------------- */
8644 7693 : const OGR_SRSNode *poNode = GetAttrNode(pszTargetKey);
8645 :
8646 7693 : if (poNode == nullptr)
8647 103 : return nullptr;
8648 :
8649 : /* -------------------------------------------------------------------- */
8650 : /* Fetch AUTHORITY child if there is one. */
8651 : /* -------------------------------------------------------------------- */
8652 7590 : if (poNode->FindChild("AUTHORITY") == -1)
8653 183 : return nullptr;
8654 :
8655 7407 : poNode = poNode->GetChild(poNode->FindChild("AUTHORITY"));
8656 :
8657 : /* -------------------------------------------------------------------- */
8658 : /* Create a new authority node. */
8659 : /* -------------------------------------------------------------------- */
8660 7407 : if (poNode->GetChildCount() < 2)
8661 0 : return nullptr;
8662 :
8663 7407 : return poNode->GetChild(1)->GetValue();
8664 : }
8665 :
8666 : /************************************************************************/
8667 : /* OSRGetAuthorityCode() */
8668 : /************************************************************************/
8669 :
8670 : /**
8671 : * \brief Get the authority code for a node.
8672 : *
8673 : * This function is the same as OGRSpatialReference::GetAuthorityCode().
8674 : */
8675 721 : const char *OSRGetAuthorityCode(OGRSpatialReferenceH hSRS,
8676 : const char *pszTargetKey)
8677 :
8678 : {
8679 721 : VALIDATE_POINTER1(hSRS, "OSRGetAuthorityCode", nullptr);
8680 :
8681 721 : return ToPointer(hSRS)->GetAuthorityCode(pszTargetKey);
8682 : }
8683 :
8684 : /************************************************************************/
8685 : /* GetAuthorityName() */
8686 : /************************************************************************/
8687 :
8688 : /**
8689 : * \brief Get the authority name for a node.
8690 : *
8691 : * This method is used to query an AUTHORITY[] node from within the
8692 : * WKT tree, and fetch the authority name value.
8693 : *
8694 : * The most common authority is "EPSG".
8695 : *
8696 : * This method is the same as the C function OSRGetAuthorityName().
8697 : *
8698 : * @param pszTargetKey the partial or complete path to the node to
8699 : * get an authority from. i.e. "PROJCS", "GEOGCS", "GEOGCS|UNIT" or NULL to
8700 : * search for an authority node on the root element.
8701 : *
8702 : * @return value code from authority node, or NULL on failure. The value
8703 : * returned is internal and should not be freed or modified.
8704 : */
8705 :
8706 : const char *
8707 49087 : OGRSpatialReference::GetAuthorityName(const char *pszTargetKey) const
8708 :
8709 : {
8710 98174 : TAKE_OPTIONAL_LOCK();
8711 :
8712 49087 : d->refreshProjObj();
8713 49087 : const char *pszInputTargetKey = pszTargetKey;
8714 49087 : pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
8715 49087 : if (pszTargetKey == nullptr)
8716 : {
8717 22088 : if (!d->m_pj_crs)
8718 : {
8719 15 : return nullptr;
8720 : }
8721 22073 : d->demoteFromBoundCRS();
8722 22073 : auto ret = proj_get_id_auth_name(d->m_pj_crs, 0);
8723 22073 : if (ret == nullptr && d->m_pjType == PJ_TYPE_PROJECTED_CRS)
8724 : {
8725 809 : auto ctxt = d->getPROJContext();
8726 809 : auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
8727 809 : if (cs)
8728 : {
8729 809 : const int axisCount = proj_cs_get_axis_count(ctxt, cs);
8730 809 : proj_destroy(cs);
8731 809 : if (axisCount == 3)
8732 : {
8733 : // This might come from a COMPD_CS with a VERT_DATUM type =
8734 : // 2002 in which case, using the WKT1 representation will
8735 : // enable us to recover the EPSG code.
8736 14 : pszTargetKey = pszInputTargetKey;
8737 : }
8738 : }
8739 : }
8740 22073 : d->undoDemoteFromBoundCRS();
8741 22073 : if (ret != nullptr || pszTargetKey == nullptr)
8742 : {
8743 22073 : return ret;
8744 : }
8745 : }
8746 :
8747 : // Special key for that context
8748 27003 : else if (EQUAL(pszTargetKey, "HORIZCRS") &&
8749 4 : d->m_pjType == PJ_TYPE_COMPOUND_CRS)
8750 : {
8751 4 : auto ctxt = d->getPROJContext();
8752 4 : auto crs = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
8753 4 : if (crs)
8754 : {
8755 4 : const char *ret = proj_get_id_auth_name(crs, 0);
8756 4 : if (ret)
8757 4 : ret = CPLSPrintf("%s", ret);
8758 4 : proj_destroy(crs);
8759 4 : return ret;
8760 : }
8761 : }
8762 26999 : else if (EQUAL(pszTargetKey, "VERTCRS") &&
8763 4 : d->m_pjType == PJ_TYPE_COMPOUND_CRS)
8764 : {
8765 4 : auto ctxt = d->getPROJContext();
8766 4 : auto crs = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 1);
8767 4 : if (crs)
8768 : {
8769 4 : const char *ret = proj_get_id_auth_name(crs, 0);
8770 4 : if (ret)
8771 4 : ret = CPLSPrintf("%s", ret);
8772 4 : proj_destroy(crs);
8773 4 : return ret;
8774 : }
8775 : }
8776 :
8777 : /* -------------------------------------------------------------------- */
8778 : /* Find the node below which the authority should be put. */
8779 : /* -------------------------------------------------------------------- */
8780 26991 : const OGR_SRSNode *poNode = GetAttrNode(pszTargetKey);
8781 :
8782 26991 : if (poNode == nullptr)
8783 11318 : return nullptr;
8784 :
8785 : /* -------------------------------------------------------------------- */
8786 : /* Fetch AUTHORITY child if there is one. */
8787 : /* -------------------------------------------------------------------- */
8788 15673 : if (poNode->FindChild("AUTHORITY") == -1)
8789 1424 : return nullptr;
8790 :
8791 14249 : poNode = poNode->GetChild(poNode->FindChild("AUTHORITY"));
8792 :
8793 : /* -------------------------------------------------------------------- */
8794 : /* Create a new authority node. */
8795 : /* -------------------------------------------------------------------- */
8796 14249 : if (poNode->GetChildCount() < 2)
8797 0 : return nullptr;
8798 :
8799 14249 : return poNode->GetChild(0)->GetValue();
8800 : }
8801 :
8802 : /************************************************************************/
8803 : /* OSRGetAuthorityName() */
8804 : /************************************************************************/
8805 :
8806 : /**
8807 : * \brief Get the authority name for a node.
8808 : *
8809 : * This function is the same as OGRSpatialReference::GetAuthorityName().
8810 : */
8811 210 : const char *OSRGetAuthorityName(OGRSpatialReferenceH hSRS,
8812 : const char *pszTargetKey)
8813 :
8814 : {
8815 210 : VALIDATE_POINTER1(hSRS, "OSRGetAuthorityName", nullptr);
8816 :
8817 210 : return ToPointer(hSRS)->GetAuthorityName(pszTargetKey);
8818 : }
8819 :
8820 : /************************************************************************/
8821 : /* GetOGCURN() */
8822 : /************************************************************************/
8823 :
8824 : /**
8825 : * \brief Get a OGC URN string describing the CRS, when possible
8826 : *
8827 : * This method assumes that the CRS has a top-level identifier, or is
8828 : * a compound CRS whose horizontal and vertical parts have a top-level
8829 : * identifier.
8830 : *
8831 : * @return a string to free with CPLFree(), or nullptr when no result can be
8832 : * generated
8833 : *
8834 : * @since GDAL 3.5
8835 : */
8836 :
8837 60 : char *OGRSpatialReference::GetOGCURN() const
8838 :
8839 : {
8840 120 : TAKE_OPTIONAL_LOCK();
8841 :
8842 60 : const char *pszAuthName = GetAuthorityName(nullptr);
8843 60 : const char *pszAuthCode = GetAuthorityCode(nullptr);
8844 60 : if (pszAuthName && pszAuthCode)
8845 57 : return CPLStrdup(
8846 57 : CPLSPrintf("urn:ogc:def:crs:%s::%s", pszAuthName, pszAuthCode));
8847 3 : if (d->m_pjType != PJ_TYPE_COMPOUND_CRS)
8848 2 : return nullptr;
8849 1 : auto horizCRS = proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
8850 1 : auto vertCRS = proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 1);
8851 1 : char *pszRet = nullptr;
8852 1 : if (horizCRS && vertCRS)
8853 : {
8854 1 : auto horizAuthName = proj_get_id_auth_name(horizCRS, 0);
8855 1 : auto horizAuthCode = proj_get_id_code(horizCRS, 0);
8856 1 : auto vertAuthName = proj_get_id_auth_name(vertCRS, 0);
8857 1 : auto vertAuthCode = proj_get_id_code(vertCRS, 0);
8858 1 : if (horizAuthName && horizAuthCode && vertAuthName && vertAuthCode)
8859 : {
8860 1 : pszRet = CPLStrdup(CPLSPrintf(
8861 : "urn:ogc:def:crs,crs:%s::%s,crs:%s::%s", horizAuthName,
8862 : horizAuthCode, vertAuthName, vertAuthCode));
8863 : }
8864 : }
8865 1 : proj_destroy(horizCRS);
8866 1 : proj_destroy(vertCRS);
8867 1 : return pszRet;
8868 : }
8869 :
8870 : /************************************************************************/
8871 : /* StripVertical() */
8872 : /************************************************************************/
8873 :
8874 : /**
8875 : * \brief Convert a compound cs into a horizontal CS.
8876 : *
8877 : * If this SRS is of type COMPD_CS[] then the vertical CS and the root COMPD_CS
8878 : * nodes are stripped resulting and only the horizontal coordinate system
8879 : * portion remains (normally PROJCS, GEOGCS or LOCAL_CS).
8880 : *
8881 : * If this is not a compound coordinate system then nothing is changed.
8882 : *
8883 : * This method is the same as the C function OSRStripVertical().
8884 : *
8885 : * @since OGR 1.8.0
8886 : */
8887 :
8888 44 : OGRErr OGRSpatialReference::StripVertical()
8889 :
8890 : {
8891 88 : TAKE_OPTIONAL_LOCK();
8892 :
8893 44 : d->refreshProjObj();
8894 44 : d->demoteFromBoundCRS();
8895 44 : if (!d->m_pj_crs || d->m_pjType != PJ_TYPE_COMPOUND_CRS)
8896 : {
8897 0 : d->undoDemoteFromBoundCRS();
8898 0 : return OGRERR_NONE;
8899 : }
8900 44 : auto horizCRS = proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
8901 44 : if (!horizCRS)
8902 : {
8903 0 : d->undoDemoteFromBoundCRS();
8904 0 : return OGRERR_FAILURE;
8905 : }
8906 :
8907 44 : bool reuseExistingBoundCRS = false;
8908 44 : if (d->m_pj_bound_crs_target)
8909 : {
8910 4 : auto type = proj_get_type(d->m_pj_bound_crs_target);
8911 8 : reuseExistingBoundCRS = type == PJ_TYPE_GEOCENTRIC_CRS ||
8912 8 : type == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
8913 : type == PJ_TYPE_GEOGRAPHIC_3D_CRS;
8914 : }
8915 :
8916 44 : if (reuseExistingBoundCRS)
8917 : {
8918 4 : auto newBoundCRS = proj_crs_create_bound_crs(
8919 4 : d->getPROJContext(), horizCRS, d->m_pj_bound_crs_target,
8920 4 : d->m_pj_bound_crs_co);
8921 4 : proj_destroy(horizCRS);
8922 4 : d->undoDemoteFromBoundCRS();
8923 4 : d->setPjCRS(newBoundCRS);
8924 : }
8925 : else
8926 : {
8927 40 : d->undoDemoteFromBoundCRS();
8928 40 : d->setPjCRS(horizCRS);
8929 : }
8930 :
8931 44 : return OGRERR_NONE;
8932 : }
8933 :
8934 : /************************************************************************/
8935 : /* OSRStripVertical() */
8936 : /************************************************************************/
8937 : /**
8938 : * \brief Convert a compound cs into a horizontal CS.
8939 : *
8940 : * This function is the same as the C++ method
8941 : * OGRSpatialReference::StripVertical().
8942 : */
8943 1 : OGRErr OSRStripVertical(OGRSpatialReferenceH hSRS)
8944 :
8945 : {
8946 1 : VALIDATE_POINTER1(hSRS, "OSRStripVertical", OGRERR_FAILURE);
8947 :
8948 1 : return OGRSpatialReference::FromHandle(hSRS)->StripVertical();
8949 : }
8950 :
8951 : /************************************************************************/
8952 : /* StripTOWGS84IfKnownDatumAndAllowed() */
8953 : /************************************************************************/
8954 :
8955 : /**
8956 : * \brief Remove TOWGS84 information if the CRS has a known horizontal datum
8957 : * and this is allowed by the user.
8958 : *
8959 : * The default behavior is to remove TOWGS84 information if the CRS has a
8960 : * known horizontal datum. This can be disabled by setting the
8961 : * OSR_STRIP_TOWGS84 configuration option to NO.
8962 : *
8963 : * @return true if TOWGS84 has been removed.
8964 : * @since OGR 3.1.0
8965 : */
8966 :
8967 8730 : bool OGRSpatialReference::StripTOWGS84IfKnownDatumAndAllowed()
8968 : {
8969 8730 : if (CPLTestBool(CPLGetConfigOption("OSR_STRIP_TOWGS84", "YES")))
8970 : {
8971 8727 : if (StripTOWGS84IfKnownDatum())
8972 : {
8973 72 : CPLDebug("OSR", "TOWGS84 information has been removed. "
8974 : "It can be kept by setting the OSR_STRIP_TOWGS84 "
8975 : "configuration option to NO");
8976 72 : return true;
8977 : }
8978 : }
8979 8658 : return false;
8980 : }
8981 :
8982 : /************************************************************************/
8983 : /* StripTOWGS84IfKnownDatum() */
8984 : /************************************************************************/
8985 :
8986 : /**
8987 : * \brief Remove TOWGS84 information if the CRS has a known horizontal datum
8988 : *
8989 : * @return true if TOWGS84 has been removed.
8990 : * @since OGR 3.1.0
8991 : */
8992 :
8993 8733 : bool OGRSpatialReference::StripTOWGS84IfKnownDatum()
8994 :
8995 : {
8996 17466 : TAKE_OPTIONAL_LOCK();
8997 :
8998 8733 : d->refreshProjObj();
8999 8733 : if (!d->m_pj_crs || d->m_pjType != PJ_TYPE_BOUND_CRS)
9000 : {
9001 8641 : return false;
9002 : }
9003 92 : auto ctxt = d->getPROJContext();
9004 92 : auto baseCRS = proj_get_source_crs(ctxt, d->m_pj_crs);
9005 92 : if (proj_get_type(baseCRS) == PJ_TYPE_COMPOUND_CRS)
9006 : {
9007 3 : proj_destroy(baseCRS);
9008 3 : return false;
9009 : }
9010 :
9011 : // Known base CRS code ? Return base CRS
9012 89 : const char *pszCode = proj_get_id_code(baseCRS, 0);
9013 89 : if (pszCode)
9014 : {
9015 2 : d->setPjCRS(baseCRS);
9016 2 : return true;
9017 : }
9018 :
9019 87 : auto datum = proj_crs_get_datum(ctxt, baseCRS);
9020 : #if PROJ_VERSION_MAJOR > 7 || \
9021 : (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
9022 : if (datum == nullptr)
9023 : {
9024 : datum = proj_crs_get_datum_ensemble(ctxt, baseCRS);
9025 : }
9026 : #endif
9027 87 : if (!datum)
9028 : {
9029 0 : proj_destroy(baseCRS);
9030 0 : return false;
9031 : }
9032 :
9033 : // Known datum code ? Return base CRS
9034 87 : pszCode = proj_get_id_code(datum, 0);
9035 87 : if (pszCode)
9036 : {
9037 3 : proj_destroy(datum);
9038 3 : d->setPjCRS(baseCRS);
9039 3 : return true;
9040 : }
9041 :
9042 84 : const char *name = proj_get_name(datum);
9043 84 : if (EQUAL(name, "unknown"))
9044 : {
9045 1 : proj_destroy(datum);
9046 1 : proj_destroy(baseCRS);
9047 1 : return false;
9048 : }
9049 83 : const PJ_TYPE type = PJ_TYPE_GEODETIC_REFERENCE_FRAME;
9050 : PJ_OBJ_LIST *list =
9051 83 : proj_create_from_name(ctxt, nullptr, name, &type, 1, false, 1, nullptr);
9052 :
9053 83 : bool knownDatumName = false;
9054 83 : if (list)
9055 : {
9056 83 : if (proj_list_get_count(list) == 1)
9057 : {
9058 70 : knownDatumName = true;
9059 : }
9060 83 : proj_list_destroy(list);
9061 : }
9062 :
9063 83 : proj_destroy(datum);
9064 83 : if (knownDatumName)
9065 : {
9066 70 : d->setPjCRS(baseCRS);
9067 70 : return true;
9068 : }
9069 13 : proj_destroy(baseCRS);
9070 13 : return false;
9071 : }
9072 :
9073 : /************************************************************************/
9074 : /* IsCompound() */
9075 : /************************************************************************/
9076 :
9077 : /**
9078 : * \brief Check if coordinate system is compound.
9079 : *
9080 : * This method is the same as the C function OSRIsCompound().
9081 : *
9082 : * @return TRUE if this is rooted with a COMPD_CS node.
9083 : */
9084 :
9085 39221 : int OGRSpatialReference::IsCompound() const
9086 :
9087 : {
9088 39221 : TAKE_OPTIONAL_LOCK();
9089 :
9090 39222 : d->refreshProjObj();
9091 39222 : d->demoteFromBoundCRS();
9092 39222 : bool isCompound = d->m_pjType == PJ_TYPE_COMPOUND_CRS;
9093 39222 : d->undoDemoteFromBoundCRS();
9094 78444 : return isCompound;
9095 : }
9096 :
9097 : /************************************************************************/
9098 : /* OSRIsCompound() */
9099 : /************************************************************************/
9100 :
9101 : /**
9102 : * \brief Check if the coordinate system is compound.
9103 : *
9104 : * This function is the same as OGRSpatialReference::IsCompound().
9105 : */
9106 5 : int OSRIsCompound(OGRSpatialReferenceH hSRS)
9107 :
9108 : {
9109 5 : VALIDATE_POINTER1(hSRS, "OSRIsCompound", 0);
9110 :
9111 5 : return ToPointer(hSRS)->IsCompound();
9112 : }
9113 :
9114 : /************************************************************************/
9115 : /* IsProjected() */
9116 : /************************************************************************/
9117 :
9118 : /**
9119 : * \brief Check if projected coordinate system.
9120 : *
9121 : * This method is the same as the C function OSRIsProjected().
9122 : *
9123 : * @return TRUE if this contains a PROJCS node indicating a it is a
9124 : * projected coordinate system. Also if it is a CompoundCRS made of a
9125 : * ProjectedCRS
9126 : */
9127 :
9128 40258 : int OGRSpatialReference::IsProjected() const
9129 :
9130 : {
9131 40258 : TAKE_OPTIONAL_LOCK();
9132 :
9133 40258 : d->refreshProjObj();
9134 40258 : d->demoteFromBoundCRS();
9135 40258 : bool isProjected = d->m_pjType == PJ_TYPE_PROJECTED_CRS;
9136 40258 : if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
9137 : {
9138 : auto horizCRS =
9139 142 : proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
9140 142 : if (horizCRS)
9141 : {
9142 142 : auto horizCRSType = proj_get_type(horizCRS);
9143 142 : isProjected = horizCRSType == PJ_TYPE_PROJECTED_CRS;
9144 142 : if (horizCRSType == PJ_TYPE_BOUND_CRS)
9145 : {
9146 6 : auto base = proj_get_source_crs(d->getPROJContext(), horizCRS);
9147 6 : if (base)
9148 : {
9149 6 : isProjected = proj_get_type(base) == PJ_TYPE_PROJECTED_CRS;
9150 6 : proj_destroy(base);
9151 : }
9152 : }
9153 142 : proj_destroy(horizCRS);
9154 : }
9155 : }
9156 40258 : d->undoDemoteFromBoundCRS();
9157 80516 : return isProjected;
9158 : }
9159 :
9160 : /************************************************************************/
9161 : /* OSRIsProjected() */
9162 : /************************************************************************/
9163 : /**
9164 : * \brief Check if projected coordinate system.
9165 : *
9166 : * This function is the same as OGRSpatialReference::IsProjected().
9167 : */
9168 413 : int OSRIsProjected(OGRSpatialReferenceH hSRS)
9169 :
9170 : {
9171 413 : VALIDATE_POINTER1(hSRS, "OSRIsProjected", 0);
9172 :
9173 413 : return ToPointer(hSRS)->IsProjected();
9174 : }
9175 :
9176 : /************************************************************************/
9177 : /* IsGeocentric() */
9178 : /************************************************************************/
9179 :
9180 : /**
9181 : * \brief Check if geocentric coordinate system.
9182 : *
9183 : * This method is the same as the C function OSRIsGeocentric().
9184 : *
9185 : * @return TRUE if this contains a GEOCCS node indicating a it is a
9186 : * geocentric coordinate system.
9187 : *
9188 : * @since OGR 1.9.0
9189 : */
9190 :
9191 16566 : int OGRSpatialReference::IsGeocentric() const
9192 :
9193 : {
9194 16566 : TAKE_OPTIONAL_LOCK();
9195 :
9196 16566 : d->refreshProjObj();
9197 16566 : d->demoteFromBoundCRS();
9198 16566 : bool isGeocentric = d->m_pjType == PJ_TYPE_GEOCENTRIC_CRS;
9199 16566 : d->undoDemoteFromBoundCRS();
9200 33132 : return isGeocentric;
9201 : }
9202 :
9203 : /************************************************************************/
9204 : /* OSRIsGeocentric() */
9205 : /************************************************************************/
9206 : /**
9207 : * \brief Check if geocentric coordinate system.
9208 : *
9209 : * This function is the same as OGRSpatialReference::IsGeocentric().
9210 : *
9211 : * @since OGR 1.9.0
9212 : */
9213 2 : int OSRIsGeocentric(OGRSpatialReferenceH hSRS)
9214 :
9215 : {
9216 2 : VALIDATE_POINTER1(hSRS, "OSRIsGeocentric", 0);
9217 :
9218 2 : return ToPointer(hSRS)->IsGeocentric();
9219 : }
9220 :
9221 : /************************************************************************/
9222 : /* IsEmpty() */
9223 : /************************************************************************/
9224 :
9225 : /**
9226 : * \brief Return if the SRS is not set.
9227 : */
9228 :
9229 108766 : bool OGRSpatialReference::IsEmpty() const
9230 : {
9231 108766 : TAKE_OPTIONAL_LOCK();
9232 :
9233 108757 : d->refreshProjObj();
9234 217527 : return d->m_pj_crs == nullptr;
9235 : }
9236 :
9237 : /************************************************************************/
9238 : /* IsGeographic() */
9239 : /************************************************************************/
9240 :
9241 : /**
9242 : * \brief Check if geographic coordinate system.
9243 : *
9244 : * This method is the same as the C function OSRIsGeographic().
9245 : *
9246 : * @return TRUE if this spatial reference is geographic ... that is the
9247 : * root is a GEOGCS node. Also if it is a CompoundCRS made of a
9248 : * GeographicCRS
9249 : */
9250 :
9251 51509 : int OGRSpatialReference::IsGeographic() const
9252 :
9253 : {
9254 51509 : TAKE_OPTIONAL_LOCK();
9255 :
9256 51510 : d->refreshProjObj();
9257 51510 : d->demoteFromBoundCRS();
9258 74181 : bool isGeog = d->m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
9259 22671 : d->m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS;
9260 51510 : if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
9261 : {
9262 : auto horizCRS =
9263 291 : proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
9264 291 : if (horizCRS)
9265 : {
9266 291 : auto horizCRSType = proj_get_type(horizCRS);
9267 291 : isGeog = horizCRSType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
9268 : horizCRSType == PJ_TYPE_GEOGRAPHIC_3D_CRS;
9269 291 : if (horizCRSType == PJ_TYPE_BOUND_CRS)
9270 : {
9271 13 : auto base = proj_get_source_crs(d->getPROJContext(), horizCRS);
9272 13 : if (base)
9273 : {
9274 13 : horizCRSType = proj_get_type(base);
9275 13 : isGeog = horizCRSType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
9276 : horizCRSType == PJ_TYPE_GEOGRAPHIC_3D_CRS;
9277 13 : proj_destroy(base);
9278 : }
9279 : }
9280 291 : proj_destroy(horizCRS);
9281 : }
9282 : }
9283 51510 : d->undoDemoteFromBoundCRS();
9284 103020 : return isGeog;
9285 : }
9286 :
9287 : /************************************************************************/
9288 : /* OSRIsGeographic() */
9289 : /************************************************************************/
9290 : /**
9291 : * \brief Check if geographic coordinate system.
9292 : *
9293 : * This function is the same as OGRSpatialReference::IsGeographic().
9294 : */
9295 334 : int OSRIsGeographic(OGRSpatialReferenceH hSRS)
9296 :
9297 : {
9298 334 : VALIDATE_POINTER1(hSRS, "OSRIsGeographic", 0);
9299 :
9300 334 : return ToPointer(hSRS)->IsGeographic();
9301 : }
9302 :
9303 : /************************************************************************/
9304 : /* IsDerivedGeographic() */
9305 : /************************************************************************/
9306 :
9307 : /**
9308 : * \brief Check if the CRS is a derived geographic coordinate system.
9309 : * (for example a rotated long/lat grid)
9310 : *
9311 : * This method is the same as the C function OSRIsDerivedGeographic().
9312 : *
9313 : * @since GDAL 3.1.0 and PROJ 6.3.0
9314 : */
9315 :
9316 14709 : int OGRSpatialReference::IsDerivedGeographic() const
9317 :
9318 : {
9319 14709 : TAKE_OPTIONAL_LOCK();
9320 :
9321 14709 : d->refreshProjObj();
9322 14709 : d->demoteFromBoundCRS();
9323 24041 : const bool isGeog = d->m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
9324 9332 : d->m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS;
9325 : const bool isDerivedGeographic =
9326 14709 : isGeog && proj_is_derived_crs(d->getPROJContext(), d->m_pj_crs);
9327 14709 : d->undoDemoteFromBoundCRS();
9328 29418 : return isDerivedGeographic ? TRUE : FALSE;
9329 : }
9330 :
9331 : /************************************************************************/
9332 : /* OSRIsDerivedGeographic() */
9333 : /************************************************************************/
9334 : /**
9335 : * \brief Check if the CRS is a derived geographic coordinate system.
9336 : * (for example a rotated long/lat grid)
9337 : *
9338 : * This function is the same as OGRSpatialReference::IsDerivedGeographic().
9339 : */
9340 1 : int OSRIsDerivedGeographic(OGRSpatialReferenceH hSRS)
9341 :
9342 : {
9343 1 : VALIDATE_POINTER1(hSRS, "OSRIsDerivedGeographic", 0);
9344 :
9345 1 : return ToPointer(hSRS)->IsDerivedGeographic();
9346 : }
9347 :
9348 : /************************************************************************/
9349 : /* IsDerivedProjected() */
9350 : /************************************************************************/
9351 :
9352 : /**
9353 : * \brief Check if the CRS is a derived projected coordinate system.
9354 : *
9355 : * This method is the same as the C function OSRIsDerivedGeographic().
9356 : *
9357 : * @since GDAL 3.9.0 (and may only return non-zero starting with PROJ 9.2.0)
9358 : */
9359 :
9360 0 : int OGRSpatialReference::IsDerivedProjected() const
9361 :
9362 : {
9363 : #if PROJ_AT_LEAST_VERSION(9, 2, 0)
9364 : TAKE_OPTIONAL_LOCK();
9365 : d->refreshProjObj();
9366 : d->demoteFromBoundCRS();
9367 : const bool isDerivedProjected =
9368 : d->m_pjType == PJ_TYPE_DERIVED_PROJECTED_CRS;
9369 : d->undoDemoteFromBoundCRS();
9370 : return isDerivedProjected ? TRUE : FALSE;
9371 : #else
9372 0 : return FALSE;
9373 : #endif
9374 : }
9375 :
9376 : /************************************************************************/
9377 : /* OSRIsDerivedProjected() */
9378 : /************************************************************************/
9379 : /**
9380 : * \brief Check if the CRS is a derived projected coordinate system.
9381 : *
9382 : * This function is the same as OGRSpatialReference::IsDerivedProjected().
9383 : *
9384 : * @since GDAL 3.9.0 (and may only return non-zero starting with PROJ 9.2.0)
9385 : */
9386 0 : int OSRIsDerivedProjected(OGRSpatialReferenceH hSRS)
9387 :
9388 : {
9389 0 : VALIDATE_POINTER1(hSRS, "OSRIsDerivedProjected", 0);
9390 :
9391 0 : return ToPointer(hSRS)->IsDerivedProjected();
9392 : }
9393 :
9394 : /************************************************************************/
9395 : /* IsLocal() */
9396 : /************************************************************************/
9397 :
9398 : /**
9399 : * \brief Check if local coordinate system.
9400 : *
9401 : * This method is the same as the C function OSRIsLocal().
9402 : *
9403 : * @return TRUE if this spatial reference is local ... that is the
9404 : * root is a LOCAL_CS node.
9405 : */
9406 :
9407 7546 : int OGRSpatialReference::IsLocal() const
9408 :
9409 : {
9410 7546 : TAKE_OPTIONAL_LOCK();
9411 7546 : d->refreshProjObj();
9412 15092 : return d->m_pjType == PJ_TYPE_ENGINEERING_CRS;
9413 : }
9414 :
9415 : /************************************************************************/
9416 : /* OSRIsLocal() */
9417 : /************************************************************************/
9418 : /**
9419 : * \brief Check if local coordinate system.
9420 : *
9421 : * This function is the same as OGRSpatialReference::IsLocal().
9422 : */
9423 8 : int OSRIsLocal(OGRSpatialReferenceH hSRS)
9424 :
9425 : {
9426 8 : VALIDATE_POINTER1(hSRS, "OSRIsLocal", 0);
9427 :
9428 8 : return ToPointer(hSRS)->IsLocal();
9429 : }
9430 :
9431 : /************************************************************************/
9432 : /* IsVertical() */
9433 : /************************************************************************/
9434 :
9435 : /**
9436 : * \brief Check if vertical coordinate system.
9437 : *
9438 : * This method is the same as the C function OSRIsVertical().
9439 : *
9440 : * @return TRUE if this contains a VERT_CS node indicating a it is a
9441 : * vertical coordinate system. Also if it is a CompoundCRS made of a
9442 : * VerticalCRS
9443 : *
9444 : * @since OGR 1.8.0
9445 : */
9446 :
9447 8413 : int OGRSpatialReference::IsVertical() const
9448 :
9449 : {
9450 8413 : TAKE_OPTIONAL_LOCK();
9451 8413 : d->refreshProjObj();
9452 8413 : d->demoteFromBoundCRS();
9453 8413 : bool isVertical = d->m_pjType == PJ_TYPE_VERTICAL_CRS;
9454 8413 : if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
9455 : {
9456 : auto vertCRS =
9457 32 : proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 1);
9458 32 : if (vertCRS)
9459 : {
9460 32 : const auto vertCRSType = proj_get_type(vertCRS);
9461 32 : isVertical = vertCRSType == PJ_TYPE_VERTICAL_CRS;
9462 32 : if (vertCRSType == PJ_TYPE_BOUND_CRS)
9463 : {
9464 0 : auto base = proj_get_source_crs(d->getPROJContext(), vertCRS);
9465 0 : if (base)
9466 : {
9467 0 : isVertical = proj_get_type(base) == PJ_TYPE_VERTICAL_CRS;
9468 0 : proj_destroy(base);
9469 : }
9470 : }
9471 32 : proj_destroy(vertCRS);
9472 : }
9473 : }
9474 8413 : d->undoDemoteFromBoundCRS();
9475 16826 : return isVertical;
9476 : }
9477 :
9478 : /************************************************************************/
9479 : /* OSRIsVertical() */
9480 : /************************************************************************/
9481 : /**
9482 : * \brief Check if vertical coordinate system.
9483 : *
9484 : * This function is the same as OGRSpatialReference::IsVertical().
9485 : *
9486 : * @since OGR 1.8.0
9487 : */
9488 0 : int OSRIsVertical(OGRSpatialReferenceH hSRS)
9489 :
9490 : {
9491 0 : VALIDATE_POINTER1(hSRS, "OSRIsVertical", 0);
9492 :
9493 0 : return ToPointer(hSRS)->IsVertical();
9494 : }
9495 :
9496 : /************************************************************************/
9497 : /* IsDynamic() */
9498 : /************************************************************************/
9499 :
9500 : /**
9501 : * \brief Check if a CRS is a dynamic CRS.
9502 : *
9503 : * A dynamic CRS relies on a dynamic datum, that is a datum that is not
9504 : * plate-fixed.
9505 : *
9506 : * This method is the same as the C function OSRIsDynamic().
9507 : *
9508 : * @return true if the CRS is dynamic
9509 : *
9510 : * @since OGR 3.4.0
9511 : *
9512 : * @see HasPointMotionOperation()
9513 : */
9514 :
9515 13767 : bool OGRSpatialReference::IsDynamic() const
9516 :
9517 : {
9518 13767 : TAKE_OPTIONAL_LOCK();
9519 13767 : bool isDynamic = false;
9520 13767 : d->refreshProjObj();
9521 13767 : d->demoteFromBoundCRS();
9522 13767 : auto ctxt = d->getPROJContext();
9523 13767 : PJ *horiz = nullptr;
9524 13767 : if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
9525 : {
9526 96 : horiz = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
9527 : }
9528 13671 : else if (d->m_pj_crs)
9529 : {
9530 13589 : horiz = proj_clone(ctxt, d->m_pj_crs);
9531 : }
9532 13767 : if (horiz && proj_get_type(horiz) == PJ_TYPE_BOUND_CRS)
9533 : {
9534 6 : auto baseCRS = proj_get_source_crs(ctxt, horiz);
9535 6 : if (baseCRS)
9536 : {
9537 6 : proj_destroy(horiz);
9538 6 : horiz = baseCRS;
9539 : }
9540 : }
9541 13767 : auto datum = horiz ? proj_crs_get_datum(ctxt, horiz) : nullptr;
9542 13767 : if (datum)
9543 : {
9544 13663 : const auto type = proj_get_type(datum);
9545 13663 : isDynamic = type == PJ_TYPE_DYNAMIC_GEODETIC_REFERENCE_FRAME ||
9546 : type == PJ_TYPE_DYNAMIC_VERTICAL_REFERENCE_FRAME;
9547 13663 : if (!isDynamic)
9548 : {
9549 13663 : const char *auth_name = proj_get_id_auth_name(datum, 0);
9550 13663 : const char *code = proj_get_id_code(datum, 0);
9551 13663 : if (auth_name && code && EQUAL(auth_name, "EPSG") &&
9552 13201 : EQUAL(code, "6326"))
9553 : {
9554 8260 : isDynamic = true;
9555 : }
9556 : }
9557 13663 : proj_destroy(datum);
9558 : }
9559 : #if PROJ_VERSION_MAJOR > 7 || \
9560 : (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
9561 : else
9562 : {
9563 : auto ensemble =
9564 : horiz ? proj_crs_get_datum_ensemble(ctxt, horiz) : nullptr;
9565 : if (ensemble)
9566 : {
9567 : auto member = proj_datum_ensemble_get_member(ctxt, ensemble, 0);
9568 : if (member)
9569 : {
9570 : const auto type = proj_get_type(member);
9571 : isDynamic = type == PJ_TYPE_DYNAMIC_GEODETIC_REFERENCE_FRAME ||
9572 : type == PJ_TYPE_DYNAMIC_VERTICAL_REFERENCE_FRAME;
9573 : proj_destroy(member);
9574 : }
9575 : proj_destroy(ensemble);
9576 : }
9577 : }
9578 : #endif
9579 13767 : proj_destroy(horiz);
9580 13767 : d->undoDemoteFromBoundCRS();
9581 27534 : return isDynamic;
9582 : }
9583 :
9584 : /************************************************************************/
9585 : /* OSRIsDynamic() */
9586 : /************************************************************************/
9587 : /**
9588 : * \brief Check if a CRS is a dynamic CRS.
9589 : *
9590 : * A dynamic CRS relies on a dynamic datum, that is a datum that is not
9591 : * plate-fixed.
9592 : *
9593 : * This function is the same as OGRSpatialReference::IsDynamic().
9594 : *
9595 : * @since OGR 3.4.0
9596 : */
9597 0 : int OSRIsDynamic(OGRSpatialReferenceH hSRS)
9598 :
9599 : {
9600 0 : VALIDATE_POINTER1(hSRS, "OSRIsDynamic", 0);
9601 :
9602 0 : return ToPointer(hSRS)->IsDynamic();
9603 : }
9604 :
9605 : /************************************************************************/
9606 : /* HasPointMotionOperation() */
9607 : /************************************************************************/
9608 :
9609 : /**
9610 : * \brief Check if a CRS has at least an associated point motion operation.
9611 : *
9612 : * Some CRS are not formally declared as dynamic, but may behave as such
9613 : * in practice due to the presence of point motion operation, to perform
9614 : * coordinate epoch changes within the CRS. Typically NAD83(CSRS)v7
9615 : *
9616 : * @return true if the CRS has at least an associated point motion operation.
9617 : *
9618 : * @since OGR 3.8.0 and PROJ 9.4.0
9619 : *
9620 : * @see IsDynamic()
9621 : */
9622 :
9623 5 : bool OGRSpatialReference::HasPointMotionOperation() const
9624 :
9625 : {
9626 : #if PROJ_VERSION_MAJOR > 9 || \
9627 : (PROJ_VERSION_MAJOR == 9 && PROJ_VERSION_MINOR >= 4)
9628 : TAKE_OPTIONAL_LOCK();
9629 : d->refreshProjObj();
9630 : d->demoteFromBoundCRS();
9631 : auto ctxt = d->getPROJContext();
9632 : auto res =
9633 : CPL_TO_BOOL(proj_crs_has_point_motion_operation(ctxt, d->m_pj_crs));
9634 : d->undoDemoteFromBoundCRS();
9635 : return res;
9636 : #else
9637 5 : return false;
9638 : #endif
9639 : }
9640 :
9641 : /************************************************************************/
9642 : /* OSRHasPointMotionOperation() */
9643 : /************************************************************************/
9644 :
9645 : /**
9646 : * \brief Check if a CRS has at least an associated point motion operation.
9647 : *
9648 : * Some CRS are not formally declared as dynamic, but may behave as such
9649 : * in practice due to the presence of point motion operation, to perform
9650 : * coordinate epoch changes within the CRS. Typically NAD83(CSRS)v7
9651 : *
9652 : * This function is the same as OGRSpatialReference::HasPointMotionOperation().
9653 : *
9654 : * @since OGR 3.8.0 and PROJ 9.4.0
9655 : */
9656 0 : int OSRHasPointMotionOperation(OGRSpatialReferenceH hSRS)
9657 :
9658 : {
9659 0 : VALIDATE_POINTER1(hSRS, "OSRHasPointMotionOperation", 0);
9660 :
9661 0 : return ToPointer(hSRS)->HasPointMotionOperation();
9662 : }
9663 :
9664 : /************************************************************************/
9665 : /* CloneGeogCS() */
9666 : /************************************************************************/
9667 :
9668 : /**
9669 : * \brief Make a duplicate of the GEOGCS node of this OGRSpatialReference
9670 : * object.
9671 : *
9672 : * @return a new SRS, which becomes the responsibility of the caller.
9673 : */
9674 4028 : OGRSpatialReference *OGRSpatialReference::CloneGeogCS() const
9675 :
9676 : {
9677 8056 : TAKE_OPTIONAL_LOCK();
9678 4028 : d->refreshProjObj();
9679 4028 : if (d->m_pj_crs)
9680 : {
9681 4028 : if (d->m_pjType == PJ_TYPE_ENGINEERING_CRS)
9682 0 : return nullptr;
9683 :
9684 : auto geodCRS =
9685 4028 : proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
9686 4028 : if (geodCRS)
9687 : {
9688 4028 : OGRSpatialReference *poNewSRS = new OGRSpatialReference();
9689 4028 : if (d->m_pjType == PJ_TYPE_BOUND_CRS)
9690 : {
9691 : PJ *hub_crs =
9692 13 : proj_get_target_crs(d->getPROJContext(), d->m_pj_crs);
9693 13 : PJ *co = proj_crs_get_coordoperation(d->getPROJContext(),
9694 13 : d->m_pj_crs);
9695 13 : auto temp = proj_crs_create_bound_crs(d->getPROJContext(),
9696 : geodCRS, hub_crs, co);
9697 13 : proj_destroy(geodCRS);
9698 13 : geodCRS = temp;
9699 13 : proj_destroy(hub_crs);
9700 13 : proj_destroy(co);
9701 : }
9702 :
9703 : /* --------------------------------------------------------------------
9704 : */
9705 : /* We have to reconstruct the GEOGCS node for geocentric */
9706 : /* coordinate systems. */
9707 : /* --------------------------------------------------------------------
9708 : */
9709 4028 : if (proj_get_type(geodCRS) == PJ_TYPE_GEOCENTRIC_CRS)
9710 : {
9711 0 : auto datum = proj_crs_get_datum(d->getPROJContext(), geodCRS);
9712 : #if PROJ_VERSION_MAJOR > 7 || \
9713 : (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
9714 : if (datum == nullptr)
9715 : {
9716 : datum = proj_crs_get_datum_ensemble(d->getPROJContext(),
9717 : geodCRS);
9718 : }
9719 : #endif
9720 0 : if (datum)
9721 : {
9722 0 : auto cs = proj_create_ellipsoidal_2D_cs(
9723 : d->getPROJContext(), PJ_ELLPS2D_LATITUDE_LONGITUDE,
9724 : nullptr, 0);
9725 0 : auto temp = proj_create_geographic_crs_from_datum(
9726 : d->getPROJContext(), "unnamed", datum, cs);
9727 0 : proj_destroy(datum);
9728 0 : proj_destroy(cs);
9729 0 : proj_destroy(geodCRS);
9730 0 : geodCRS = temp;
9731 : }
9732 : }
9733 :
9734 4028 : poNewSRS->d->setPjCRS(geodCRS);
9735 4028 : if (d->m_axisMappingStrategy == OAMS_TRADITIONAL_GIS_ORDER)
9736 2669 : poNewSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
9737 4028 : return poNewSRS;
9738 : }
9739 : }
9740 0 : return nullptr;
9741 : }
9742 :
9743 : /************************************************************************/
9744 : /* OSRCloneGeogCS() */
9745 : /************************************************************************/
9746 : /**
9747 : * \brief Make a duplicate of the GEOGCS node of this OGRSpatialReference
9748 : * object.
9749 : *
9750 : * This function is the same as OGRSpatialReference::CloneGeogCS().
9751 : */
9752 150 : OGRSpatialReferenceH CPL_STDCALL OSRCloneGeogCS(OGRSpatialReferenceH hSource)
9753 :
9754 : {
9755 150 : VALIDATE_POINTER1(hSource, "OSRCloneGeogCS", nullptr);
9756 :
9757 150 : return ToHandle(ToPointer(hSource)->CloneGeogCS());
9758 : }
9759 :
9760 : /************************************************************************/
9761 : /* IsSameGeogCS() */
9762 : /************************************************************************/
9763 :
9764 : /**
9765 : * \brief Do the GeogCS'es match?
9766 : *
9767 : * This method is the same as the C function OSRIsSameGeogCS().
9768 : *
9769 : * @param poOther the SRS being compared against.
9770 : *
9771 : * @return TRUE if they are the same or FALSE otherwise.
9772 : */
9773 :
9774 8038 : int OGRSpatialReference::IsSameGeogCS(const OGRSpatialReference *poOther) const
9775 :
9776 : {
9777 8038 : return IsSameGeogCS(poOther, nullptr);
9778 : }
9779 :
9780 : /**
9781 : * \brief Do the GeogCS'es match?
9782 : *
9783 : * This method is the same as the C function OSRIsSameGeogCS().
9784 : *
9785 : * @param poOther the SRS being compared against.
9786 : * @param papszOptions options. ignored
9787 : *
9788 : * @return TRUE if they are the same or FALSE otherwise.
9789 : */
9790 :
9791 8038 : int OGRSpatialReference::IsSameGeogCS(const OGRSpatialReference *poOther,
9792 : const char *const *papszOptions) const
9793 :
9794 : {
9795 16076 : TAKE_OPTIONAL_LOCK();
9796 :
9797 8038 : CPL_IGNORE_RET_VAL(papszOptions);
9798 :
9799 8038 : d->refreshProjObj();
9800 8038 : poOther->d->refreshProjObj();
9801 8038 : if (!d->m_pj_crs || !poOther->d->m_pj_crs)
9802 0 : return FALSE;
9803 8038 : if (d->m_pjType == PJ_TYPE_ENGINEERING_CRS ||
9804 8038 : d->m_pjType == PJ_TYPE_VERTICAL_CRS ||
9805 24114 : poOther->d->m_pjType == PJ_TYPE_ENGINEERING_CRS ||
9806 8038 : poOther->d->m_pjType == PJ_TYPE_VERTICAL_CRS)
9807 : {
9808 0 : return FALSE;
9809 : }
9810 :
9811 8038 : auto geodCRS = proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
9812 : auto otherGeodCRS =
9813 8038 : proj_crs_get_geodetic_crs(d->getPROJContext(), poOther->d->m_pj_crs);
9814 8038 : if (!geodCRS || !otherGeodCRS)
9815 : {
9816 0 : proj_destroy(geodCRS);
9817 0 : proj_destroy(otherGeodCRS);
9818 0 : return FALSE;
9819 : }
9820 :
9821 8038 : int ret = proj_is_equivalent_to(
9822 : geodCRS, otherGeodCRS, PJ_COMP_EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS);
9823 :
9824 8038 : proj_destroy(geodCRS);
9825 8038 : proj_destroy(otherGeodCRS);
9826 8038 : return ret;
9827 : }
9828 :
9829 : /************************************************************************/
9830 : /* OSRIsSameGeogCS() */
9831 : /************************************************************************/
9832 :
9833 : /**
9834 : * \brief Do the GeogCS'es match?
9835 : *
9836 : * This function is the same as OGRSpatialReference::IsSameGeogCS().
9837 : */
9838 0 : int OSRIsSameGeogCS(OGRSpatialReferenceH hSRS1, OGRSpatialReferenceH hSRS2)
9839 :
9840 : {
9841 0 : VALIDATE_POINTER1(hSRS1, "OSRIsSameGeogCS", 0);
9842 0 : VALIDATE_POINTER1(hSRS2, "OSRIsSameGeogCS", 0);
9843 :
9844 0 : return ToPointer(hSRS1)->IsSameGeogCS(ToPointer(hSRS2));
9845 : }
9846 :
9847 : /************************************************************************/
9848 : /* IsSameVertCS() */
9849 : /************************************************************************/
9850 :
9851 : /**
9852 : * \brief Do the VertCS'es match?
9853 : *
9854 : * This method is the same as the C function OSRIsSameVertCS().
9855 : *
9856 : * @param poOther the SRS being compared against.
9857 : *
9858 : * @return TRUE if they are the same or FALSE otherwise.
9859 : */
9860 :
9861 0 : int OGRSpatialReference::IsSameVertCS(const OGRSpatialReference *poOther) const
9862 :
9863 : {
9864 0 : TAKE_OPTIONAL_LOCK();
9865 :
9866 : /* -------------------------------------------------------------------- */
9867 : /* Does the datum name match? */
9868 : /* -------------------------------------------------------------------- */
9869 0 : const char *pszThisValue = this->GetAttrValue("VERT_DATUM");
9870 0 : const char *pszOtherValue = poOther->GetAttrValue("VERT_DATUM");
9871 :
9872 0 : if (pszThisValue == nullptr || pszOtherValue == nullptr ||
9873 0 : !EQUAL(pszThisValue, pszOtherValue))
9874 0 : return FALSE;
9875 :
9876 : /* -------------------------------------------------------------------- */
9877 : /* Do the units match? */
9878 : /* -------------------------------------------------------------------- */
9879 0 : pszThisValue = this->GetAttrValue("VERT_CS|UNIT", 1);
9880 0 : if (pszThisValue == nullptr)
9881 0 : pszThisValue = "1.0";
9882 :
9883 0 : pszOtherValue = poOther->GetAttrValue("VERT_CS|UNIT", 1);
9884 0 : if (pszOtherValue == nullptr)
9885 0 : pszOtherValue = "1.0";
9886 :
9887 0 : if (std::abs(CPLAtof(pszOtherValue) - CPLAtof(pszThisValue)) > 0.00000001)
9888 0 : return FALSE;
9889 :
9890 0 : return TRUE;
9891 : }
9892 :
9893 : /************************************************************************/
9894 : /* OSRIsSameVertCS() */
9895 : /************************************************************************/
9896 :
9897 : /**
9898 : * \brief Do the VertCS'es match?
9899 : *
9900 : * This function is the same as OGRSpatialReference::IsSameVertCS().
9901 : */
9902 0 : int OSRIsSameVertCS(OGRSpatialReferenceH hSRS1, OGRSpatialReferenceH hSRS2)
9903 :
9904 : {
9905 0 : VALIDATE_POINTER1(hSRS1, "OSRIsSameVertCS", 0);
9906 0 : VALIDATE_POINTER1(hSRS2, "OSRIsSameVertCS", 0);
9907 :
9908 0 : return ToPointer(hSRS1)->IsSameVertCS(ToPointer(hSRS2));
9909 : }
9910 :
9911 : /************************************************************************/
9912 : /* IsSame() */
9913 : /************************************************************************/
9914 :
9915 : /**
9916 : * \brief Do these two spatial references describe the same system ?
9917 : *
9918 : * @param poOtherSRS the SRS being compared to.
9919 : *
9920 : * @return TRUE if equivalent or FALSE otherwise.
9921 : */
9922 :
9923 6598 : int OGRSpatialReference::IsSame(const OGRSpatialReference *poOtherSRS) const
9924 :
9925 : {
9926 6598 : return IsSame(poOtherSRS, nullptr);
9927 : }
9928 :
9929 : /**
9930 : * \brief Do these two spatial references describe the same system ?
9931 : *
9932 : * This also takes into account the data axis to CRS axis mapping by default
9933 : *
9934 : * @param poOtherSRS the SRS being compared to.
9935 : * @param papszOptions options. NULL or NULL terminated list of options.
9936 : * Currently supported options are:
9937 : * <ul>
9938 : * <li>IGNORE_DATA_AXIS_TO_SRS_AXIS_MAPPING=YES/NO. Defaults to NO</li>
9939 : * <li>CRITERION=STRICT/EQUIVALENT/EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS.
9940 : * Defaults to EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS.</li>
9941 : * <li>IGNORE_COORDINATE_EPOCH=YES/NO. Defaults to NO</li>
9942 : * </ul>
9943 : *
9944 : * @return TRUE if equivalent or FALSE otherwise.
9945 : */
9946 :
9947 16679 : int OGRSpatialReference::IsSame(const OGRSpatialReference *poOtherSRS,
9948 : const char *const *papszOptions) const
9949 :
9950 : {
9951 33358 : TAKE_OPTIONAL_LOCK();
9952 :
9953 16679 : d->refreshProjObj();
9954 16679 : poOtherSRS->d->refreshProjObj();
9955 16679 : if (!d->m_pj_crs || !poOtherSRS->d->m_pj_crs)
9956 51 : return d->m_pj_crs == poOtherSRS->d->m_pj_crs;
9957 16628 : if (!CPLTestBool(CSLFetchNameValueDef(
9958 : papszOptions, "IGNORE_DATA_AXIS_TO_SRS_AXIS_MAPPING", "NO")))
9959 : {
9960 16035 : if (d->m_axisMapping != poOtherSRS->d->m_axisMapping)
9961 2519 : return false;
9962 : }
9963 :
9964 14109 : if (!CPLTestBool(CSLFetchNameValueDef(papszOptions,
9965 : "IGNORE_COORDINATE_EPOCH", "NO")))
9966 : {
9967 13752 : if (d->m_coordinateEpoch != poOtherSRS->d->m_coordinateEpoch)
9968 27 : return false;
9969 : }
9970 :
9971 14082 : bool reboundSelf = false;
9972 14082 : bool reboundOther = false;
9973 14133 : if (d->m_pjType == PJ_TYPE_BOUND_CRS &&
9974 51 : poOtherSRS->d->m_pjType != PJ_TYPE_BOUND_CRS)
9975 : {
9976 14 : d->demoteFromBoundCRS();
9977 14 : reboundSelf = true;
9978 : }
9979 28099 : else if (d->m_pjType != PJ_TYPE_BOUND_CRS &&
9980 14031 : poOtherSRS->d->m_pjType == PJ_TYPE_BOUND_CRS)
9981 : {
9982 28 : poOtherSRS->d->demoteFromBoundCRS();
9983 28 : reboundOther = true;
9984 : }
9985 :
9986 14082 : PJ_COMPARISON_CRITERION criterion =
9987 : PJ_COMP_EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS;
9988 14082 : const char *pszCriterion = CSLFetchNameValueDef(
9989 : papszOptions, "CRITERION", "EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS");
9990 14082 : if (EQUAL(pszCriterion, "STRICT"))
9991 0 : criterion = PJ_COMP_STRICT;
9992 14082 : else if (EQUAL(pszCriterion, "EQUIVALENT"))
9993 7429 : criterion = PJ_COMP_EQUIVALENT;
9994 6653 : else if (!EQUAL(pszCriterion, "EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS"))
9995 : {
9996 0 : CPLError(CE_Warning, CPLE_NotSupported,
9997 : "Unsupported value for CRITERION: %s", pszCriterion);
9998 : }
9999 : int ret =
10000 14082 : proj_is_equivalent_to(d->m_pj_crs, poOtherSRS->d->m_pj_crs, criterion);
10001 14082 : if (reboundSelf)
10002 14 : d->undoDemoteFromBoundCRS();
10003 14082 : if (reboundOther)
10004 28 : poOtherSRS->d->undoDemoteFromBoundCRS();
10005 :
10006 14082 : return ret;
10007 : }
10008 :
10009 : /************************************************************************/
10010 : /* OSRIsSame() */
10011 : /************************************************************************/
10012 :
10013 : /**
10014 : * \brief Do these two spatial references describe the same system ?
10015 : *
10016 : * This function is the same as OGRSpatialReference::IsSame().
10017 : */
10018 32 : int OSRIsSame(OGRSpatialReferenceH hSRS1, OGRSpatialReferenceH hSRS2)
10019 :
10020 : {
10021 32 : VALIDATE_POINTER1(hSRS1, "OSRIsSame", 0);
10022 32 : VALIDATE_POINTER1(hSRS2, "OSRIsSame", 0);
10023 :
10024 32 : return ToPointer(hSRS1)->IsSame(ToPointer(hSRS2));
10025 : }
10026 :
10027 : /************************************************************************/
10028 : /* OSRIsSameEx() */
10029 : /************************************************************************/
10030 :
10031 : /**
10032 : * \brief Do these two spatial references describe the same system ?
10033 : *
10034 : * This function is the same as OGRSpatialReference::IsSame().
10035 : */
10036 601 : int OSRIsSameEx(OGRSpatialReferenceH hSRS1, OGRSpatialReferenceH hSRS2,
10037 : const char *const *papszOptions)
10038 : {
10039 601 : VALIDATE_POINTER1(hSRS1, "OSRIsSame", 0);
10040 601 : VALIDATE_POINTER1(hSRS2, "OSRIsSame", 0);
10041 :
10042 601 : return ToPointer(hSRS1)->IsSame(ToPointer(hSRS2), papszOptions);
10043 : }
10044 :
10045 : /************************************************************************/
10046 : /* convertToOtherProjection() */
10047 : /************************************************************************/
10048 :
10049 : /**
10050 : * \brief Convert to another equivalent projection
10051 : *
10052 : * Currently implemented:
10053 : * <ul>
10054 : * <li>SRS_PT_MERCATOR_1SP to SRS_PT_MERCATOR_2SP</li>
10055 : * <li>SRS_PT_MERCATOR_2SP to SRS_PT_MERCATOR_1SP</li>
10056 : * <li>SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP to
10057 : * SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP</li>
10058 : * <li>SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP to
10059 : * SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP</li>
10060 : * </ul>
10061 : *
10062 : * @param pszTargetProjection target projection.
10063 : * @param papszOptions lists of options. None supported currently.
10064 : * @return a new SRS, or NULL in case of error.
10065 : *
10066 : * @since GDAL 2.3
10067 : */
10068 89 : OGRSpatialReference *OGRSpatialReference::convertToOtherProjection(
10069 : const char *pszTargetProjection,
10070 : CPL_UNUSED const char *const *papszOptions) const
10071 : {
10072 178 : TAKE_OPTIONAL_LOCK();
10073 :
10074 89 : if (pszTargetProjection == nullptr)
10075 1 : return nullptr;
10076 : int new_code;
10077 88 : if (EQUAL(pszTargetProjection, SRS_PT_MERCATOR_1SP))
10078 : {
10079 6 : new_code = EPSG_CODE_METHOD_MERCATOR_VARIANT_A;
10080 : }
10081 82 : else if (EQUAL(pszTargetProjection, SRS_PT_MERCATOR_2SP))
10082 : {
10083 5 : new_code = EPSG_CODE_METHOD_MERCATOR_VARIANT_B;
10084 : }
10085 77 : else if (EQUAL(pszTargetProjection, SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP))
10086 : {
10087 65 : new_code = EPSG_CODE_METHOD_LAMBERT_CONIC_CONFORMAL_1SP;
10088 : }
10089 12 : else if (EQUAL(pszTargetProjection, SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP))
10090 : {
10091 11 : new_code = EPSG_CODE_METHOD_LAMBERT_CONIC_CONFORMAL_2SP;
10092 : }
10093 : else
10094 : {
10095 1 : return nullptr;
10096 : }
10097 :
10098 87 : d->refreshProjObj();
10099 87 : d->demoteFromBoundCRS();
10100 87 : OGRSpatialReference *poNewSRS = nullptr;
10101 87 : if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
10102 : {
10103 : auto conv =
10104 86 : proj_crs_get_coordoperation(d->getPROJContext(), d->m_pj_crs);
10105 86 : auto new_conv = proj_convert_conversion_to_other_method(
10106 : d->getPROJContext(), conv, new_code, nullptr);
10107 86 : proj_destroy(conv);
10108 86 : if (new_conv)
10109 : {
10110 : auto geodCRS =
10111 72 : proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
10112 72 : auto cs = proj_crs_get_coordinate_system(d->getPROJContext(),
10113 72 : d->m_pj_crs);
10114 72 : if (geodCRS && cs)
10115 : {
10116 72 : auto new_proj_crs = proj_create_projected_crs(
10117 72 : d->getPROJContext(), proj_get_name(d->m_pj_crs), geodCRS,
10118 : new_conv, cs);
10119 72 : proj_destroy(new_conv);
10120 72 : if (new_proj_crs)
10121 : {
10122 72 : poNewSRS = new OGRSpatialReference();
10123 :
10124 72 : if (d->m_pj_bound_crs_target && d->m_pj_bound_crs_co)
10125 : {
10126 9 : auto boundCRS = proj_crs_create_bound_crs(
10127 : d->getPROJContext(), new_proj_crs,
10128 9 : d->m_pj_bound_crs_target, d->m_pj_bound_crs_co);
10129 9 : if (boundCRS)
10130 : {
10131 9 : proj_destroy(new_proj_crs);
10132 9 : new_proj_crs = boundCRS;
10133 : }
10134 : }
10135 :
10136 72 : poNewSRS->d->setPjCRS(new_proj_crs);
10137 : }
10138 : }
10139 72 : proj_destroy(geodCRS);
10140 72 : proj_destroy(cs);
10141 : }
10142 : }
10143 87 : d->undoDemoteFromBoundCRS();
10144 87 : return poNewSRS;
10145 : }
10146 :
10147 : /************************************************************************/
10148 : /* OSRConvertToOtherProjection() */
10149 : /************************************************************************/
10150 :
10151 : /**
10152 : * \brief Convert to another equivalent projection
10153 : *
10154 : * Currently implemented:
10155 : * <ul>
10156 : * <li>SRS_PT_MERCATOR_1SP to SRS_PT_MERCATOR_2SP</li>
10157 : * <li>SRS_PT_MERCATOR_2SP to SRS_PT_MERCATOR_1SP</li>
10158 : * <li>SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP to
10159 : * SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP</li>
10160 : * <li>SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP to
10161 : * SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP</li>
10162 : * </ul>
10163 : *
10164 : * @param hSRS source SRS
10165 : * @param pszTargetProjection target projection.
10166 : * @param papszOptions lists of options. None supported currently.
10167 : * @return a new SRS, or NULL in case of error.
10168 : *
10169 : * @since GDAL 2.3
10170 : */
10171 : OGRSpatialReferenceH
10172 28 : OSRConvertToOtherProjection(OGRSpatialReferenceH hSRS,
10173 : const char *pszTargetProjection,
10174 : const char *const *papszOptions)
10175 : {
10176 28 : VALIDATE_POINTER1(hSRS, "OSRConvertToOtherProjection", nullptr);
10177 28 : return ToHandle(ToPointer(hSRS)->convertToOtherProjection(
10178 28 : pszTargetProjection, papszOptions));
10179 : }
10180 :
10181 : /************************************************************************/
10182 : /* OSRFindMatches() */
10183 : /************************************************************************/
10184 :
10185 : /**
10186 : * \brief Try to identify a match between the passed SRS and a related SRS
10187 : * in a catalog.
10188 : *
10189 : * Matching may be partial, or may fail.
10190 : * Returned entries will be sorted by decreasing match confidence (first
10191 : * entry has the highest match confidence).
10192 : *
10193 : * The exact way matching is done may change in future versions. Starting with
10194 : * GDAL 3.0, it relies on PROJ' proj_identify() function.
10195 : *
10196 : * This function is the same as OGRSpatialReference::FindMatches().
10197 : *
10198 : * @param hSRS SRS to match
10199 : * @param papszOptions NULL terminated list of options or NULL
10200 : * @param pnEntries Output parameter. Number of values in the returned array.
10201 : * @param ppanMatchConfidence Output parameter (or NULL). *ppanMatchConfidence
10202 : * will be allocated to an array of *pnEntries whose values between 0 and 100
10203 : * indicate the confidence in the match. 100 is the highest confidence level.
10204 : * The array must be freed with CPLFree().
10205 : *
10206 : * @return an array of SRS that match the passed SRS, or NULL. Must be freed
10207 : * with OSRFreeSRSArray()
10208 : *
10209 : * @since GDAL 2.3
10210 : */
10211 10 : OGRSpatialReferenceH *OSRFindMatches(OGRSpatialReferenceH hSRS,
10212 : char **papszOptions, int *pnEntries,
10213 : int **ppanMatchConfidence)
10214 : {
10215 10 : if (pnEntries)
10216 10 : *pnEntries = 0;
10217 10 : if (ppanMatchConfidence)
10218 10 : *ppanMatchConfidence = nullptr;
10219 10 : VALIDATE_POINTER1(hSRS, "OSRFindMatches", nullptr);
10220 :
10221 10 : OGRSpatialReference *poSRS = ToPointer(hSRS);
10222 10 : return poSRS->FindMatches(papszOptions, pnEntries, ppanMatchConfidence);
10223 : }
10224 :
10225 : /************************************************************************/
10226 : /* OSRFreeSRSArray() */
10227 : /************************************************************************/
10228 :
10229 : /**
10230 : * \brief Free return of OSRIdentifyMatches()
10231 : *
10232 : * @param pahSRS array of SRS (must be NULL terminated)
10233 : * @since GDAL 2.3
10234 : */
10235 192 : void OSRFreeSRSArray(OGRSpatialReferenceH *pahSRS)
10236 : {
10237 192 : if (pahSRS != nullptr)
10238 : {
10239 1739 : for (int i = 0; pahSRS[i] != nullptr; ++i)
10240 : {
10241 1565 : OSRRelease(pahSRS[i]);
10242 : }
10243 174 : CPLFree(pahSRS);
10244 : }
10245 192 : }
10246 :
10247 : /************************************************************************/
10248 : /* FindBestMatch() */
10249 : /************************************************************************/
10250 :
10251 : /**
10252 : * \brief Try to identify the best match between the passed SRS and a related
10253 : * SRS in a catalog.
10254 : *
10255 : * This is a wrapper over OGRSpatialReference::FindMatches() that takes care
10256 : * of filtering its output.
10257 : * Only matches whose confidence is greater or equal to nMinimumMatchConfidence
10258 : * will be considered. If there is a single match, it is returned.
10259 : * If there are several matches, only return the one under the
10260 : * pszPreferredAuthority, if there is a single one under that authority.
10261 : *
10262 : * @param nMinimumMatchConfidence Minimum match confidence (value between 0 and
10263 : * 100). If set to 0, 90 is used.
10264 : * @param pszPreferredAuthority Preferred CRS authority. If set to nullptr,
10265 : * "EPSG" is used.
10266 : * @param papszOptions NULL terminated list of options or NULL. No option is
10267 : * defined at time of writing.
10268 : *
10269 : * @return a new OGRSpatialReference* object to free with Release(), or nullptr
10270 : *
10271 : * @since GDAL 3.6
10272 : * @see OGRSpatialReference::FindMatches()
10273 : */
10274 : OGRSpatialReference *
10275 1381 : OGRSpatialReference::FindBestMatch(int nMinimumMatchConfidence,
10276 : const char *pszPreferredAuthority,
10277 : CSLConstList papszOptions) const
10278 : {
10279 2762 : TAKE_OPTIONAL_LOCK();
10280 :
10281 1381 : CPL_IGNORE_RET_VAL(papszOptions); // ignored for now.
10282 :
10283 1381 : if (nMinimumMatchConfidence == 0)
10284 0 : nMinimumMatchConfidence = 90;
10285 1381 : if (pszPreferredAuthority == nullptr)
10286 200 : pszPreferredAuthority = "EPSG";
10287 :
10288 : // Try to identify the CRS with the database
10289 1381 : int nEntries = 0;
10290 1381 : int *panConfidence = nullptr;
10291 : OGRSpatialReferenceH *pahSRS =
10292 1381 : FindMatches(nullptr, &nEntries, &panConfidence);
10293 1381 : if (nEntries == 1 && panConfidence[0] >= nMinimumMatchConfidence)
10294 : {
10295 2444 : std::vector<double> adfTOWGS84(7);
10296 1222 : if (GetTOWGS84(&adfTOWGS84[0], 7) != OGRERR_NONE)
10297 : {
10298 1221 : adfTOWGS84.clear();
10299 : }
10300 :
10301 1222 : auto poSRS = OGRSpatialReference::FromHandle(pahSRS[0]);
10302 :
10303 : auto poBaseGeogCRS =
10304 1222 : std::unique_ptr<OGRSpatialReference>(poSRS->CloneGeogCS());
10305 :
10306 : // If the base geographic SRS of the SRS is EPSG:4326
10307 : // with TOWGS84[0,0,0,0,0,0], then just use the official
10308 : // SRS code
10309 : // Same with EPSG:4258 (ETRS89), since it's the only known
10310 : // TOWGS84[] style transformation to WGS 84, and given the
10311 : // "fuzzy" nature of both ETRS89 and WGS 84, there's little
10312 : // chance that a non-NULL TOWGS84[] will emerge.
10313 1222 : const char *pszAuthorityName = nullptr;
10314 1222 : const char *pszAuthorityCode = nullptr;
10315 1222 : const char *pszBaseAuthorityName = nullptr;
10316 1222 : const char *pszBaseAuthorityCode = nullptr;
10317 2444 : if (adfTOWGS84 == std::vector<double>(7) &&
10318 1 : (pszAuthorityName = poSRS->GetAuthorityName(nullptr)) != nullptr &&
10319 1 : EQUAL(pszAuthorityName, "EPSG") &&
10320 1 : (pszAuthorityCode = poSRS->GetAuthorityCode(nullptr)) != nullptr &&
10321 1 : (pszBaseAuthorityName = poBaseGeogCRS->GetAuthorityName(nullptr)) !=
10322 1 : nullptr &&
10323 1 : EQUAL(pszBaseAuthorityName, "EPSG") &&
10324 1 : (pszBaseAuthorityCode = poBaseGeogCRS->GetAuthorityCode(nullptr)) !=
10325 2445 : nullptr &&
10326 1 : (EQUAL(pszBaseAuthorityCode, "4326") ||
10327 1 : EQUAL(pszBaseAuthorityCode, "4258")))
10328 : {
10329 1 : poSRS->importFromEPSG(atoi(pszAuthorityCode));
10330 : }
10331 :
10332 1222 : CPLFree(pahSRS);
10333 1222 : CPLFree(panConfidence);
10334 :
10335 1222 : return poSRS;
10336 : }
10337 : else
10338 : {
10339 : // If there are several matches >= nMinimumMatchConfidence, take the
10340 : // only one that is under pszPreferredAuthority
10341 159 : int iBestEntry = -1;
10342 1674 : for (int i = 0; i < nEntries; i++)
10343 : {
10344 1515 : if (panConfidence[i] >= nMinimumMatchConfidence)
10345 : {
10346 : const char *pszAuthName =
10347 3 : OGRSpatialReference::FromHandle(pahSRS[i])
10348 3 : ->GetAuthorityName(nullptr);
10349 3 : if (pszAuthName != nullptr &&
10350 3 : EQUAL(pszAuthName, pszPreferredAuthority))
10351 : {
10352 3 : if (iBestEntry < 0)
10353 3 : iBestEntry = i;
10354 : else
10355 : {
10356 0 : iBestEntry = -1;
10357 0 : break;
10358 : }
10359 : }
10360 : }
10361 : }
10362 159 : if (iBestEntry >= 0)
10363 : {
10364 3 : auto poRet = OGRSpatialReference::FromHandle(pahSRS[0])->Clone();
10365 3 : OSRFreeSRSArray(pahSRS);
10366 3 : CPLFree(panConfidence);
10367 3 : return poRet;
10368 : }
10369 : }
10370 156 : OSRFreeSRSArray(pahSRS);
10371 156 : CPLFree(panConfidence);
10372 156 : return nullptr;
10373 : }
10374 :
10375 : /************************************************************************/
10376 : /* SetTOWGS84() */
10377 : /************************************************************************/
10378 :
10379 : /**
10380 : * \brief Set the Bursa-Wolf conversion to WGS84.
10381 : *
10382 : * This will create the TOWGS84 node as a child of the DATUM. It will fail
10383 : * if there is no existing DATUM node. It will replace
10384 : * an existing TOWGS84 node if there is one.
10385 : *
10386 : * The parameters have the same meaning as EPSG transformation 9606
10387 : * (Position Vector 7-param. transformation).
10388 : *
10389 : * This method is the same as the C function OSRSetTOWGS84().
10390 : *
10391 : * @param dfDX X child in meters.
10392 : * @param dfDY Y child in meters.
10393 : * @param dfDZ Z child in meters.
10394 : * @param dfEX X rotation in arc seconds (optional, defaults to zero).
10395 : * @param dfEY Y rotation in arc seconds (optional, defaults to zero).
10396 : * @param dfEZ Z rotation in arc seconds (optional, defaults to zero).
10397 : * @param dfPPM scaling factor (parts per million).
10398 : *
10399 : * @return OGRERR_NONE on success.
10400 : */
10401 :
10402 104 : OGRErr OGRSpatialReference::SetTOWGS84(double dfDX, double dfDY, double dfDZ,
10403 : double dfEX, double dfEY, double dfEZ,
10404 : double dfPPM)
10405 :
10406 : {
10407 208 : TAKE_OPTIONAL_LOCK();
10408 :
10409 104 : d->refreshProjObj();
10410 104 : if (d->m_pj_crs == nullptr)
10411 : {
10412 0 : return OGRERR_FAILURE;
10413 : }
10414 :
10415 : // Remove existing BoundCRS
10416 104 : if (d->m_pjType == PJ_TYPE_BOUND_CRS)
10417 : {
10418 0 : auto baseCRS = proj_get_source_crs(d->getPROJContext(), d->m_pj_crs);
10419 0 : if (!baseCRS)
10420 0 : return OGRERR_FAILURE;
10421 0 : d->setPjCRS(baseCRS);
10422 : }
10423 :
10424 : PJ_PARAM_DESCRIPTION params[7];
10425 :
10426 104 : params[0].name = EPSG_NAME_PARAMETER_X_AXIS_TRANSLATION;
10427 104 : params[0].auth_name = "EPSG";
10428 104 : params[0].code = XSTRINGIFY(EPSG_CODE_PARAMETER_X_AXIS_TRANSLATION);
10429 104 : params[0].value = dfDX;
10430 104 : params[0].unit_name = "metre";
10431 104 : params[0].unit_conv_factor = 1.0;
10432 104 : params[0].unit_type = PJ_UT_LINEAR;
10433 :
10434 104 : params[1].name = EPSG_NAME_PARAMETER_Y_AXIS_TRANSLATION;
10435 104 : params[1].auth_name = "EPSG";
10436 104 : params[1].code = XSTRINGIFY(EPSG_CODE_PARAMETER_Y_AXIS_TRANSLATION);
10437 104 : params[1].value = dfDY;
10438 104 : params[1].unit_name = "metre";
10439 104 : params[1].unit_conv_factor = 1.0;
10440 104 : params[1].unit_type = PJ_UT_LINEAR;
10441 :
10442 104 : params[2].name = EPSG_NAME_PARAMETER_Z_AXIS_TRANSLATION;
10443 104 : params[2].auth_name = "EPSG";
10444 104 : params[2].code = XSTRINGIFY(EPSG_CODE_PARAMETER_Z_AXIS_TRANSLATION);
10445 104 : params[2].value = dfDZ;
10446 104 : params[2].unit_name = "metre";
10447 104 : params[2].unit_conv_factor = 1.0;
10448 104 : params[2].unit_type = PJ_UT_LINEAR;
10449 :
10450 104 : params[3].name = EPSG_NAME_PARAMETER_X_AXIS_ROTATION;
10451 104 : params[3].auth_name = "EPSG";
10452 104 : params[3].code = XSTRINGIFY(EPSG_CODE_PARAMETER_X_AXIS_ROTATION);
10453 104 : params[3].value = dfEX;
10454 104 : params[3].unit_name = "arc-second";
10455 104 : params[3].unit_conv_factor = 1. / 3600 * M_PI / 180;
10456 104 : params[3].unit_type = PJ_UT_ANGULAR;
10457 :
10458 104 : params[4].name = EPSG_NAME_PARAMETER_Y_AXIS_ROTATION;
10459 104 : params[4].auth_name = "EPSG";
10460 104 : params[4].code = XSTRINGIFY(EPSG_CODE_PARAMETER_Y_AXIS_ROTATION);
10461 104 : params[4].value = dfEY;
10462 104 : params[4].unit_name = "arc-second";
10463 104 : params[4].unit_conv_factor = 1. / 3600 * M_PI / 180;
10464 104 : params[4].unit_type = PJ_UT_ANGULAR;
10465 :
10466 104 : params[5].name = EPSG_NAME_PARAMETER_Z_AXIS_ROTATION;
10467 104 : params[5].auth_name = "EPSG";
10468 104 : params[5].code = XSTRINGIFY(EPSG_CODE_PARAMETER_Z_AXIS_ROTATION);
10469 104 : params[5].value = dfEZ;
10470 104 : params[5].unit_name = "arc-second";
10471 104 : params[5].unit_conv_factor = 1. / 3600 * M_PI / 180;
10472 104 : params[5].unit_type = PJ_UT_ANGULAR;
10473 :
10474 104 : params[6].name = EPSG_NAME_PARAMETER_SCALE_DIFFERENCE;
10475 104 : params[6].auth_name = "EPSG";
10476 104 : params[6].code = XSTRINGIFY(EPSG_CODE_PARAMETER_SCALE_DIFFERENCE);
10477 104 : params[6].value = dfPPM;
10478 104 : params[6].unit_name = "parts per million";
10479 104 : params[6].unit_conv_factor = 1e-6;
10480 104 : params[6].unit_type = PJ_UT_SCALE;
10481 :
10482 : auto sourceCRS =
10483 104 : proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
10484 104 : if (!sourceCRS)
10485 : {
10486 0 : return OGRERR_FAILURE;
10487 : }
10488 :
10489 104 : const auto sourceType = proj_get_type(sourceCRS);
10490 :
10491 104 : auto targetCRS = proj_create_from_database(
10492 : d->getPROJContext(), "EPSG",
10493 : sourceType == PJ_TYPE_GEOGRAPHIC_2D_CRS ? "4326"
10494 : : sourceType == PJ_TYPE_GEOGRAPHIC_3D_CRS ? "4979"
10495 : : "4978",
10496 : PJ_CATEGORY_CRS, false, nullptr);
10497 104 : if (!targetCRS)
10498 : {
10499 0 : proj_destroy(sourceCRS);
10500 0 : return OGRERR_FAILURE;
10501 : }
10502 :
10503 208 : CPLString osMethodCode;
10504 : osMethodCode.Printf("%d",
10505 : sourceType == PJ_TYPE_GEOGRAPHIC_2D_CRS
10506 : ? EPSG_CODE_METHOD_POSITION_VECTOR_GEOGRAPHIC_2D
10507 : : sourceType == PJ_TYPE_GEOGRAPHIC_3D_CRS
10508 0 : ? EPSG_CODE_METHOD_POSITION_VECTOR_GEOGRAPHIC_3D
10509 104 : : EPSG_CODE_METHOD_POSITION_VECTOR_GEOCENTRIC);
10510 :
10511 104 : auto transf = proj_create_transformation(
10512 : d->getPROJContext(), "Transformation to WGS84", nullptr, nullptr,
10513 : sourceCRS, targetCRS, nullptr,
10514 : sourceType == PJ_TYPE_GEOGRAPHIC_2D_CRS
10515 : ? EPSG_NAME_METHOD_POSITION_VECTOR_GEOGRAPHIC_2D
10516 : : sourceType == PJ_TYPE_GEOGRAPHIC_3D_CRS
10517 0 : ? EPSG_NAME_METHOD_POSITION_VECTOR_GEOGRAPHIC_3D
10518 : : EPSG_NAME_METHOD_POSITION_VECTOR_GEOCENTRIC,
10519 : "EPSG", osMethodCode.c_str(), 7, params, -1);
10520 104 : proj_destroy(sourceCRS);
10521 104 : if (!transf)
10522 : {
10523 0 : proj_destroy(targetCRS);
10524 0 : return OGRERR_FAILURE;
10525 : }
10526 :
10527 104 : auto newBoundCRS = proj_crs_create_bound_crs(
10528 104 : d->getPROJContext(), d->m_pj_crs, targetCRS, transf);
10529 104 : proj_destroy(transf);
10530 104 : proj_destroy(targetCRS);
10531 104 : if (!newBoundCRS)
10532 : {
10533 0 : return OGRERR_FAILURE;
10534 : }
10535 :
10536 104 : d->setPjCRS(newBoundCRS);
10537 104 : return OGRERR_NONE;
10538 : }
10539 :
10540 : /************************************************************************/
10541 : /* OSRSetTOWGS84() */
10542 : /************************************************************************/
10543 :
10544 : /**
10545 : * \brief Set the Bursa-Wolf conversion to WGS84.
10546 : *
10547 : * This function is the same as OGRSpatialReference::SetTOWGS84().
10548 : */
10549 4 : OGRErr OSRSetTOWGS84(OGRSpatialReferenceH hSRS, double dfDX, double dfDY,
10550 : double dfDZ, double dfEX, double dfEY, double dfEZ,
10551 : double dfPPM)
10552 :
10553 : {
10554 4 : VALIDATE_POINTER1(hSRS, "OSRSetTOWGS84", OGRERR_FAILURE);
10555 :
10556 4 : return ToPointer(hSRS)->SetTOWGS84(dfDX, dfDY, dfDZ, dfEX, dfEY, dfEZ,
10557 4 : dfPPM);
10558 : }
10559 :
10560 : /************************************************************************/
10561 : /* GetTOWGS84() */
10562 : /************************************************************************/
10563 :
10564 : /**
10565 : * \brief Fetch TOWGS84 parameters, if available.
10566 : *
10567 : * The parameters have the same meaning as EPSG transformation 9606
10568 : * (Position Vector 7-param. transformation).
10569 : *
10570 : * @param padfCoeff array into which up to 7 coefficients are placed.
10571 : * @param nCoeffCount size of padfCoeff - defaults to 7.
10572 : *
10573 : * @return OGRERR_NONE on success, or OGRERR_FAILURE if there is no
10574 : * TOWGS84 node available.
10575 : */
10576 :
10577 4566 : OGRErr OGRSpatialReference::GetTOWGS84(double *padfCoeff, int nCoeffCount) const
10578 :
10579 : {
10580 9132 : TAKE_OPTIONAL_LOCK();
10581 :
10582 4566 : d->refreshProjObj();
10583 4566 : if (d->m_pjType != PJ_TYPE_BOUND_CRS)
10584 4518 : return OGRERR_FAILURE;
10585 :
10586 48 : memset(padfCoeff, 0, sizeof(double) * nCoeffCount);
10587 :
10588 48 : auto transf = proj_crs_get_coordoperation(d->getPROJContext(), d->m_pj_crs);
10589 48 : int success = proj_coordoperation_get_towgs84_values(
10590 : d->getPROJContext(), transf, padfCoeff, nCoeffCount, false);
10591 48 : proj_destroy(transf);
10592 :
10593 48 : return success ? OGRERR_NONE : OGRERR_FAILURE;
10594 : }
10595 :
10596 : /************************************************************************/
10597 : /* OSRGetTOWGS84() */
10598 : /************************************************************************/
10599 :
10600 : /**
10601 : * \brief Fetch TOWGS84 parameters, if available.
10602 : *
10603 : * This function is the same as OGRSpatialReference::GetTOWGS84().
10604 : */
10605 10 : OGRErr OSRGetTOWGS84(OGRSpatialReferenceH hSRS, double *padfCoeff,
10606 : int nCoeffCount)
10607 :
10608 : {
10609 10 : VALIDATE_POINTER1(hSRS, "OSRGetTOWGS84", OGRERR_FAILURE);
10610 :
10611 10 : return ToPointer(hSRS)->GetTOWGS84(padfCoeff, nCoeffCount);
10612 : }
10613 :
10614 : /************************************************************************/
10615 : /* IsAngularParameter() */
10616 : /************************************************************************/
10617 :
10618 : /** Is the passed projection parameter an angular one?
10619 : *
10620 : * @return TRUE or FALSE
10621 : */
10622 :
10623 : /* static */
10624 10 : int OGRSpatialReference::IsAngularParameter(const char *pszParameterName)
10625 :
10626 : {
10627 10 : if (STARTS_WITH_CI(pszParameterName, "long") ||
10628 10 : STARTS_WITH_CI(pszParameterName, "lati") ||
10629 7 : EQUAL(pszParameterName, SRS_PP_CENTRAL_MERIDIAN) ||
10630 4 : STARTS_WITH_CI(pszParameterName, "standard_parallel") ||
10631 2 : EQUAL(pszParameterName, SRS_PP_AZIMUTH) ||
10632 2 : EQUAL(pszParameterName, SRS_PP_RECTIFIED_GRID_ANGLE))
10633 8 : return TRUE;
10634 :
10635 2 : return FALSE;
10636 : }
10637 :
10638 : /************************************************************************/
10639 : /* IsLongitudeParameter() */
10640 : /************************************************************************/
10641 :
10642 : /** Is the passed projection parameter an angular longitude
10643 : * (relative to a prime meridian)?
10644 : *
10645 : * @return TRUE or FALSE
10646 : */
10647 :
10648 : /* static */
10649 0 : int OGRSpatialReference::IsLongitudeParameter(const char *pszParameterName)
10650 :
10651 : {
10652 0 : if (STARTS_WITH_CI(pszParameterName, "long") ||
10653 0 : EQUAL(pszParameterName, SRS_PP_CENTRAL_MERIDIAN))
10654 0 : return TRUE;
10655 :
10656 0 : return FALSE;
10657 : }
10658 :
10659 : /************************************************************************/
10660 : /* IsLinearParameter() */
10661 : /************************************************************************/
10662 :
10663 : /** Is the passed projection parameter an linear one measured in meters or
10664 : * some similar linear measure.
10665 : *
10666 : * @return TRUE or FALSE
10667 : */
10668 :
10669 : /* static */
10670 47 : int OGRSpatialReference::IsLinearParameter(const char *pszParameterName)
10671 :
10672 : {
10673 47 : if (STARTS_WITH_CI(pszParameterName, "false_") ||
10674 38 : EQUAL(pszParameterName, SRS_PP_SATELLITE_HEIGHT))
10675 9 : return TRUE;
10676 :
10677 38 : return FALSE;
10678 : }
10679 :
10680 : /************************************************************************/
10681 : /* GetNormInfo() */
10682 : /************************************************************************/
10683 :
10684 : /**
10685 : * \brief Set the internal information for normalizing linear, and angular
10686 : * values.
10687 : */
10688 3648 : void OGRSpatialReference::GetNormInfo() const
10689 :
10690 : {
10691 3648 : TAKE_OPTIONAL_LOCK();
10692 :
10693 3648 : if (d->bNormInfoSet)
10694 2588 : return;
10695 :
10696 : /* -------------------------------------------------------------------- */
10697 : /* Initialize values. */
10698 : /* -------------------------------------------------------------------- */
10699 1060 : d->bNormInfoSet = TRUE;
10700 :
10701 1060 : d->dfFromGreenwich = GetPrimeMeridian(nullptr);
10702 1060 : d->dfToMeter = GetLinearUnits(nullptr);
10703 1060 : d->dfToDegrees = GetAngularUnits(nullptr) / CPLAtof(SRS_UA_DEGREE_CONV);
10704 1060 : if (fabs(d->dfToDegrees - 1.0) < 0.000000001)
10705 1057 : d->dfToDegrees = 1.0;
10706 : }
10707 :
10708 : /************************************************************************/
10709 : /* GetExtension() */
10710 : /************************************************************************/
10711 :
10712 : /**
10713 : * \brief Fetch extension value.
10714 : *
10715 : * Fetch the value of the named EXTENSION item for the identified
10716 : * target node.
10717 : *
10718 : * @param pszTargetKey the name or path to the parent node of the EXTENSION.
10719 : * @param pszName the name of the extension being fetched.
10720 : * @param pszDefault the value to return if the extension is not found.
10721 : *
10722 : * @return node value if successful or pszDefault on failure.
10723 : */
10724 :
10725 10158 : const char *OGRSpatialReference::GetExtension(const char *pszTargetKey,
10726 : const char *pszName,
10727 : const char *pszDefault) const
10728 :
10729 : {
10730 20316 : TAKE_OPTIONAL_LOCK();
10731 :
10732 : /* -------------------------------------------------------------------- */
10733 : /* Find the target node. */
10734 : /* -------------------------------------------------------------------- */
10735 : const OGR_SRSNode *poNode =
10736 10158 : pszTargetKey == nullptr ? GetRoot() : GetAttrNode(pszTargetKey);
10737 :
10738 10158 : if (poNode == nullptr)
10739 2229 : return nullptr;
10740 :
10741 : /* -------------------------------------------------------------------- */
10742 : /* Fetch matching EXTENSION if there is one. */
10743 : /* -------------------------------------------------------------------- */
10744 58424 : for (int i = poNode->GetChildCount() - 1; i >= 0; i--)
10745 : {
10746 50517 : const OGR_SRSNode *poChild = poNode->GetChild(i);
10747 :
10748 50541 : if (EQUAL(poChild->GetValue(), "EXTENSION") &&
10749 24 : poChild->GetChildCount() >= 2)
10750 : {
10751 24 : if (EQUAL(poChild->GetChild(0)->GetValue(), pszName))
10752 22 : return poChild->GetChild(1)->GetValue();
10753 : }
10754 : }
10755 :
10756 7907 : return pszDefault;
10757 : }
10758 :
10759 : /************************************************************************/
10760 : /* SetExtension() */
10761 : /************************************************************************/
10762 : /**
10763 : * \brief Set extension value.
10764 : *
10765 : * Set the value of the named EXTENSION item for the identified
10766 : * target node.
10767 : *
10768 : * @param pszTargetKey the name or path to the parent node of the EXTENSION.
10769 : * @param pszName the name of the extension being fetched.
10770 : * @param pszValue the value to set
10771 : *
10772 : * @return OGRERR_NONE on success
10773 : */
10774 :
10775 20 : OGRErr OGRSpatialReference::SetExtension(const char *pszTargetKey,
10776 : const char *pszName,
10777 : const char *pszValue)
10778 :
10779 : {
10780 40 : TAKE_OPTIONAL_LOCK();
10781 :
10782 : /* -------------------------------------------------------------------- */
10783 : /* Find the target node. */
10784 : /* -------------------------------------------------------------------- */
10785 20 : OGR_SRSNode *poNode = nullptr;
10786 :
10787 20 : if (pszTargetKey == nullptr)
10788 0 : poNode = GetRoot();
10789 : else
10790 20 : poNode = GetAttrNode(pszTargetKey);
10791 :
10792 20 : if (poNode == nullptr)
10793 0 : return OGRERR_FAILURE;
10794 :
10795 : /* -------------------------------------------------------------------- */
10796 : /* Fetch matching EXTENSION if there is one. */
10797 : /* -------------------------------------------------------------------- */
10798 151 : for (int i = poNode->GetChildCount() - 1; i >= 0; i--)
10799 : {
10800 137 : OGR_SRSNode *poChild = poNode->GetChild(i);
10801 :
10802 143 : if (EQUAL(poChild->GetValue(), "EXTENSION") &&
10803 6 : poChild->GetChildCount() >= 2)
10804 : {
10805 6 : if (EQUAL(poChild->GetChild(0)->GetValue(), pszName))
10806 : {
10807 6 : poChild->GetChild(1)->SetValue(pszValue);
10808 6 : return OGRERR_NONE;
10809 : }
10810 : }
10811 : }
10812 :
10813 : /* -------------------------------------------------------------------- */
10814 : /* Create a new EXTENSION node. */
10815 : /* -------------------------------------------------------------------- */
10816 14 : OGR_SRSNode *poAuthNode = new OGR_SRSNode("EXTENSION");
10817 14 : poAuthNode->AddChild(new OGR_SRSNode(pszName));
10818 14 : poAuthNode->AddChild(new OGR_SRSNode(pszValue));
10819 :
10820 14 : poNode->AddChild(poAuthNode);
10821 :
10822 14 : return OGRERR_NONE;
10823 : }
10824 :
10825 : /************************************************************************/
10826 : /* OSRCleanup() */
10827 : /************************************************************************/
10828 :
10829 : static void CleanupSRSWGS84Mutex();
10830 :
10831 : /**
10832 : * \brief Cleanup cached SRS related memory.
10833 : *
10834 : * This function will attempt to cleanup any cache spatial reference
10835 : * related information, such as cached tables of coordinate systems.
10836 : *
10837 : * This function should not be called concurrently with any other GDAL/OGR
10838 : * function. It is meant at being called once before process termination
10839 : * (typically from the main thread). CPLCleanupTLS() might be used to clean
10840 : * thread-specific resources before thread termination.
10841 : */
10842 1118 : void OSRCleanup(void)
10843 :
10844 : {
10845 1118 : OGRCTDumpStatistics();
10846 1118 : CSVDeaccess(nullptr);
10847 1118 : CleanupSRSWGS84Mutex();
10848 1118 : OSRCTCleanCache();
10849 1118 : OSRCleanupTLSContext();
10850 1118 : }
10851 :
10852 : /************************************************************************/
10853 : /* GetAxesCount() */
10854 : /************************************************************************/
10855 :
10856 : /**
10857 : * \brief Return the number of axis of the coordinate system of the CRS.
10858 : *
10859 : * @since GDAL 3.0
10860 : */
10861 35846 : int OGRSpatialReference::GetAxesCount() const
10862 : {
10863 71692 : TAKE_OPTIONAL_LOCK();
10864 :
10865 35846 : int axisCount = 0;
10866 35846 : d->refreshProjObj();
10867 35846 : if (d->m_pj_crs == nullptr)
10868 : {
10869 0 : return 0;
10870 : }
10871 35846 : d->demoteFromBoundCRS();
10872 35845 : auto ctxt = d->getPROJContext();
10873 35846 : if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
10874 : {
10875 29 : for (int i = 0;; i++)
10876 : {
10877 87 : auto subCRS = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, i);
10878 87 : if (!subCRS)
10879 29 : break;
10880 58 : if (proj_get_type(subCRS) == PJ_TYPE_BOUND_CRS)
10881 : {
10882 17 : auto baseCRS = proj_get_source_crs(ctxt, subCRS);
10883 17 : if (baseCRS)
10884 : {
10885 17 : proj_destroy(subCRS);
10886 17 : subCRS = baseCRS;
10887 : }
10888 : }
10889 58 : auto cs = proj_crs_get_coordinate_system(ctxt, subCRS);
10890 58 : if (cs)
10891 : {
10892 58 : axisCount += proj_cs_get_axis_count(ctxt, cs);
10893 58 : proj_destroy(cs);
10894 : }
10895 58 : proj_destroy(subCRS);
10896 58 : }
10897 : }
10898 : else
10899 : {
10900 35817 : auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
10901 35817 : if (cs)
10902 : {
10903 35817 : axisCount = proj_cs_get_axis_count(ctxt, cs);
10904 35817 : proj_destroy(cs);
10905 : }
10906 : }
10907 35846 : d->undoDemoteFromBoundCRS();
10908 35846 : return axisCount;
10909 : }
10910 :
10911 : /************************************************************************/
10912 : /* OSRGetAxesCount() */
10913 : /************************************************************************/
10914 :
10915 : /**
10916 : * \brief Return the number of axis of the coordinate system of the CRS.
10917 : *
10918 : * This method is the equivalent of the C++ method
10919 : * OGRSpatialReference::GetAxesCount()
10920 : *
10921 : * @since GDAL 3.1
10922 : */
10923 6 : int OSRGetAxesCount(OGRSpatialReferenceH hSRS)
10924 :
10925 : {
10926 6 : VALIDATE_POINTER1(hSRS, "OSRGetAxesCount", 0);
10927 :
10928 6 : return ToPointer(hSRS)->GetAxesCount();
10929 : }
10930 :
10931 : /************************************************************************/
10932 : /* GetAxis() */
10933 : /************************************************************************/
10934 :
10935 : /**
10936 : * \brief Fetch the orientation of one axis.
10937 : *
10938 : * Fetches the request axis (iAxis - zero based) from the
10939 : * indicated portion of the coordinate system (pszTargetKey) which
10940 : * should be either "GEOGCS" or "PROJCS".
10941 : *
10942 : * No CPLError is issued on routine failures (such as not finding the AXIS).
10943 : *
10944 : * This method is equivalent to the C function OSRGetAxis().
10945 : *
10946 : * @param pszTargetKey the coordinate system part to query ("PROJCS" or
10947 : * "GEOGCS").
10948 : * @param iAxis the axis to query (0 for first, 1 for second, 2 for third).
10949 : * @param peOrientation location into which to place the fetch orientation, may
10950 : * be NULL.
10951 : * @param pdfConvUnit (GDAL >= 3.4) Location into which to place axis conversion
10952 : * factor. May be NULL. Only set if pszTargetKey == NULL
10953 : *
10954 : * @return the name of the axis or NULL on failure.
10955 : */
10956 :
10957 6478 : const char *OGRSpatialReference::GetAxis(const char *pszTargetKey, int iAxis,
10958 : OGRAxisOrientation *peOrientation,
10959 : double *pdfConvUnit) const
10960 :
10961 : {
10962 12956 : TAKE_OPTIONAL_LOCK();
10963 :
10964 6478 : if (peOrientation != nullptr)
10965 6385 : *peOrientation = OAO_Other;
10966 6478 : if (pdfConvUnit != nullptr)
10967 85 : *pdfConvUnit = 0;
10968 :
10969 6478 : d->refreshProjObj();
10970 6478 : if (d->m_pj_crs == nullptr)
10971 : {
10972 1 : return nullptr;
10973 : }
10974 :
10975 6477 : pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
10976 6477 : if (pszTargetKey == nullptr && iAxis <= 2)
10977 : {
10978 6477 : auto ctxt = d->getPROJContext();
10979 :
10980 6477 : int iAxisModified = iAxis;
10981 :
10982 6477 : d->demoteFromBoundCRS();
10983 :
10984 6477 : PJ *cs = nullptr;
10985 6477 : if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
10986 : {
10987 134 : auto horizCRS = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
10988 134 : if (horizCRS)
10989 : {
10990 134 : if (proj_get_type(horizCRS) == PJ_TYPE_BOUND_CRS)
10991 : {
10992 6 : auto baseCRS = proj_get_source_crs(ctxt, horizCRS);
10993 6 : if (baseCRS)
10994 : {
10995 6 : proj_destroy(horizCRS);
10996 6 : horizCRS = baseCRS;
10997 : }
10998 : }
10999 134 : cs = proj_crs_get_coordinate_system(ctxt, horizCRS);
11000 134 : proj_destroy(horizCRS);
11001 134 : if (cs)
11002 : {
11003 134 : if (iAxisModified >= proj_cs_get_axis_count(ctxt, cs))
11004 : {
11005 44 : iAxisModified -= proj_cs_get_axis_count(ctxt, cs);
11006 44 : proj_destroy(cs);
11007 44 : cs = nullptr;
11008 : }
11009 : }
11010 : }
11011 :
11012 134 : if (cs == nullptr)
11013 : {
11014 44 : auto vertCRS = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 1);
11015 44 : if (vertCRS)
11016 : {
11017 44 : if (proj_get_type(vertCRS) == PJ_TYPE_BOUND_CRS)
11018 : {
11019 30 : auto baseCRS = proj_get_source_crs(ctxt, vertCRS);
11020 30 : if (baseCRS)
11021 : {
11022 30 : proj_destroy(vertCRS);
11023 30 : vertCRS = baseCRS;
11024 : }
11025 : }
11026 :
11027 44 : cs = proj_crs_get_coordinate_system(ctxt, vertCRS);
11028 44 : proj_destroy(vertCRS);
11029 : }
11030 : }
11031 : }
11032 : else
11033 : {
11034 6343 : cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
11035 : }
11036 :
11037 6477 : if (cs)
11038 : {
11039 6477 : const char *pszName = nullptr;
11040 6477 : const char *pszOrientation = nullptr;
11041 6477 : double dfConvFactor = 0.0;
11042 6477 : proj_cs_get_axis_info(ctxt, cs, iAxisModified, &pszName, nullptr,
11043 : &pszOrientation, &dfConvFactor, nullptr,
11044 : nullptr, nullptr);
11045 :
11046 6477 : if (pdfConvUnit != nullptr)
11047 : {
11048 85 : *pdfConvUnit = dfConvFactor;
11049 : }
11050 :
11051 6477 : if (pszName && pszOrientation)
11052 : {
11053 6477 : d->m_osAxisName[iAxis] = pszName;
11054 6477 : if (peOrientation)
11055 : {
11056 6384 : if (EQUAL(pszOrientation, "NORTH"))
11057 4020 : *peOrientation = OAO_North;
11058 2364 : else if (EQUAL(pszOrientation, "EAST"))
11059 2321 : *peOrientation = OAO_East;
11060 43 : else if (EQUAL(pszOrientation, "SOUTH"))
11061 32 : *peOrientation = OAO_South;
11062 11 : else if (EQUAL(pszOrientation, "WEST"))
11063 0 : *peOrientation = OAO_West;
11064 11 : else if (EQUAL(pszOrientation, "UP"))
11065 1 : *peOrientation = OAO_Up;
11066 10 : else if (EQUAL(pszOrientation, "DOWN"))
11067 0 : *peOrientation = OAO_Down;
11068 : }
11069 6477 : proj_destroy(cs);
11070 6477 : d->undoDemoteFromBoundCRS();
11071 6477 : return d->m_osAxisName[iAxis].c_str();
11072 : }
11073 0 : proj_destroy(cs);
11074 : }
11075 0 : d->undoDemoteFromBoundCRS();
11076 : }
11077 :
11078 : /* -------------------------------------------------------------------- */
11079 : /* Find the target node. */
11080 : /* -------------------------------------------------------------------- */
11081 0 : const OGR_SRSNode *poNode = nullptr;
11082 :
11083 0 : if (pszTargetKey == nullptr)
11084 0 : poNode = GetRoot();
11085 : else
11086 0 : poNode = GetAttrNode(pszTargetKey);
11087 :
11088 0 : if (poNode == nullptr)
11089 0 : return nullptr;
11090 :
11091 : /* -------------------------------------------------------------------- */
11092 : /* Find desired child AXIS. */
11093 : /* -------------------------------------------------------------------- */
11094 0 : const OGR_SRSNode *poAxis = nullptr;
11095 0 : const int nChildCount = poNode->GetChildCount();
11096 :
11097 0 : for (int iChild = 0; iChild < nChildCount; iChild++)
11098 : {
11099 0 : const OGR_SRSNode *poChild = poNode->GetChild(iChild);
11100 :
11101 0 : if (!EQUAL(poChild->GetValue(), "AXIS"))
11102 0 : continue;
11103 :
11104 0 : if (iAxis == 0)
11105 : {
11106 0 : poAxis = poChild;
11107 0 : break;
11108 : }
11109 0 : iAxis--;
11110 : }
11111 :
11112 0 : if (poAxis == nullptr)
11113 0 : return nullptr;
11114 :
11115 0 : if (poAxis->GetChildCount() < 2)
11116 0 : return nullptr;
11117 :
11118 : /* -------------------------------------------------------------------- */
11119 : /* Extract name and orientation if possible. */
11120 : /* -------------------------------------------------------------------- */
11121 0 : if (peOrientation != nullptr)
11122 : {
11123 0 : const char *pszOrientation = poAxis->GetChild(1)->GetValue();
11124 :
11125 0 : if (EQUAL(pszOrientation, "NORTH"))
11126 0 : *peOrientation = OAO_North;
11127 0 : else if (EQUAL(pszOrientation, "EAST"))
11128 0 : *peOrientation = OAO_East;
11129 0 : else if (EQUAL(pszOrientation, "SOUTH"))
11130 0 : *peOrientation = OAO_South;
11131 0 : else if (EQUAL(pszOrientation, "WEST"))
11132 0 : *peOrientation = OAO_West;
11133 0 : else if (EQUAL(pszOrientation, "UP"))
11134 0 : *peOrientation = OAO_Up;
11135 0 : else if (EQUAL(pszOrientation, "DOWN"))
11136 0 : *peOrientation = OAO_Down;
11137 0 : else if (EQUAL(pszOrientation, "OTHER"))
11138 0 : *peOrientation = OAO_Other;
11139 : else
11140 : {
11141 0 : CPLDebug("OSR", "Unrecognized orientation value '%s'.",
11142 : pszOrientation);
11143 : }
11144 : }
11145 :
11146 0 : return poAxis->GetChild(0)->GetValue();
11147 : }
11148 :
11149 : /************************************************************************/
11150 : /* OSRGetAxis() */
11151 : /************************************************************************/
11152 :
11153 : /**
11154 : * \brief Fetch the orientation of one axis.
11155 : *
11156 : * This method is the equivalent of the C++ method OGRSpatialReference::GetAxis
11157 : */
11158 13 : const char *OSRGetAxis(OGRSpatialReferenceH hSRS, const char *pszTargetKey,
11159 : int iAxis, OGRAxisOrientation *peOrientation)
11160 :
11161 : {
11162 13 : VALIDATE_POINTER1(hSRS, "OSRGetAxis", nullptr);
11163 :
11164 13 : return ToPointer(hSRS)->GetAxis(pszTargetKey, iAxis, peOrientation);
11165 : }
11166 :
11167 : /************************************************************************/
11168 : /* OSRAxisEnumToName() */
11169 : /************************************************************************/
11170 :
11171 : /**
11172 : * \brief Return the string representation for the OGRAxisOrientation
11173 : * enumeration.
11174 : *
11175 : * For example "NORTH" for OAO_North.
11176 : *
11177 : * @return an internal string
11178 : */
11179 392 : const char *OSRAxisEnumToName(OGRAxisOrientation eOrientation)
11180 :
11181 : {
11182 392 : if (eOrientation == OAO_North)
11183 196 : return "NORTH";
11184 196 : if (eOrientation == OAO_East)
11185 196 : return "EAST";
11186 0 : if (eOrientation == OAO_South)
11187 0 : return "SOUTH";
11188 0 : if (eOrientation == OAO_West)
11189 0 : return "WEST";
11190 0 : if (eOrientation == OAO_Up)
11191 0 : return "UP";
11192 0 : if (eOrientation == OAO_Down)
11193 0 : return "DOWN";
11194 0 : if (eOrientation == OAO_Other)
11195 0 : return "OTHER";
11196 :
11197 0 : return "UNKNOWN";
11198 : }
11199 :
11200 : /************************************************************************/
11201 : /* SetAxes() */
11202 : /************************************************************************/
11203 :
11204 : /**
11205 : * \brief Set the axes for a coordinate system.
11206 : *
11207 : * Set the names, and orientations of the axes for either a projected
11208 : * (PROJCS) or geographic (GEOGCS) coordinate system.
11209 : *
11210 : * This method is equivalent to the C function OSRSetAxes().
11211 : *
11212 : * @param pszTargetKey either "PROJCS" or "GEOGCS", must already exist in SRS.
11213 : * @param pszXAxisName name of first axis, normally "Long" or "Easting".
11214 : * @param eXAxisOrientation normally OAO_East.
11215 : * @param pszYAxisName name of second axis, normally "Lat" or "Northing".
11216 : * @param eYAxisOrientation normally OAO_North.
11217 : *
11218 : * @return OGRERR_NONE on success or an error code.
11219 : */
11220 :
11221 196 : OGRErr OGRSpatialReference::SetAxes(const char *pszTargetKey,
11222 : const char *pszXAxisName,
11223 : OGRAxisOrientation eXAxisOrientation,
11224 : const char *pszYAxisName,
11225 : OGRAxisOrientation eYAxisOrientation)
11226 :
11227 : {
11228 392 : TAKE_OPTIONAL_LOCK();
11229 :
11230 : /* -------------------------------------------------------------------- */
11231 : /* Find the target node. */
11232 : /* -------------------------------------------------------------------- */
11233 196 : OGR_SRSNode *poNode = nullptr;
11234 :
11235 196 : if (pszTargetKey == nullptr)
11236 196 : poNode = GetRoot();
11237 : else
11238 0 : poNode = GetAttrNode(pszTargetKey);
11239 :
11240 196 : if (poNode == nullptr)
11241 0 : return OGRERR_FAILURE;
11242 :
11243 : /* -------------------------------------------------------------------- */
11244 : /* Strip any existing AXIS children. */
11245 : /* -------------------------------------------------------------------- */
11246 588 : while (poNode->FindChild("AXIS") >= 0)
11247 392 : poNode->DestroyChild(poNode->FindChild("AXIS"));
11248 :
11249 : /* -------------------------------------------------------------------- */
11250 : /* Insert desired axes */
11251 : /* -------------------------------------------------------------------- */
11252 196 : OGR_SRSNode *poAxis = new OGR_SRSNode("AXIS");
11253 :
11254 196 : poAxis->AddChild(new OGR_SRSNode(pszXAxisName));
11255 196 : poAxis->AddChild(new OGR_SRSNode(OSRAxisEnumToName(eXAxisOrientation)));
11256 :
11257 196 : poNode->AddChild(poAxis);
11258 :
11259 196 : poAxis = new OGR_SRSNode("AXIS");
11260 :
11261 196 : poAxis->AddChild(new OGR_SRSNode(pszYAxisName));
11262 196 : poAxis->AddChild(new OGR_SRSNode(OSRAxisEnumToName(eYAxisOrientation)));
11263 :
11264 196 : poNode->AddChild(poAxis);
11265 :
11266 196 : return OGRERR_NONE;
11267 : }
11268 :
11269 : /************************************************************************/
11270 : /* OSRSetAxes() */
11271 : /************************************************************************/
11272 : /**
11273 : * \brief Set the axes for a coordinate system.
11274 : *
11275 : * This method is the equivalent of the C++ method OGRSpatialReference::SetAxes
11276 : */
11277 0 : OGRErr OSRSetAxes(OGRSpatialReferenceH hSRS, const char *pszTargetKey,
11278 : const char *pszXAxisName,
11279 : OGRAxisOrientation eXAxisOrientation,
11280 : const char *pszYAxisName,
11281 : OGRAxisOrientation eYAxisOrientation)
11282 : {
11283 0 : VALIDATE_POINTER1(hSRS, "OSRSetAxes", OGRERR_FAILURE);
11284 :
11285 0 : return ToPointer(hSRS)->SetAxes(pszTargetKey, pszXAxisName,
11286 : eXAxisOrientation, pszYAxisName,
11287 0 : eYAxisOrientation);
11288 : }
11289 :
11290 : /************************************************************************/
11291 : /* OSRExportToMICoordSys() */
11292 : /************************************************************************/
11293 : /**
11294 : * \brief Export coordinate system in Mapinfo style CoordSys format.
11295 : *
11296 : * This method is the equivalent of the C++ method
11297 : * OGRSpatialReference::exportToMICoordSys
11298 : */
11299 5 : OGRErr OSRExportToMICoordSys(OGRSpatialReferenceH hSRS, char **ppszReturn)
11300 :
11301 : {
11302 5 : VALIDATE_POINTER1(hSRS, "OSRExportToMICoordSys", OGRERR_FAILURE);
11303 :
11304 5 : *ppszReturn = nullptr;
11305 :
11306 5 : return ToPointer(hSRS)->exportToMICoordSys(ppszReturn);
11307 : }
11308 :
11309 : /************************************************************************/
11310 : /* exportToMICoordSys() */
11311 : /************************************************************************/
11312 :
11313 : /**
11314 : * \brief Export coordinate system in Mapinfo style CoordSys format.
11315 : *
11316 : * Note that the returned WKT string should be freed with
11317 : * CPLFree() when no longer needed. It is the responsibility of the caller.
11318 : *
11319 : * This method is the same as the C function OSRExportToMICoordSys().
11320 : *
11321 : * @param ppszResult pointer to which dynamically allocated Mapinfo CoordSys
11322 : * definition will be assigned.
11323 : *
11324 : * @return OGRERR_NONE on success, OGRERR_FAILURE on failure,
11325 : * OGRERR_UNSUPPORTED_OPERATION if MITAB library was not linked in.
11326 : */
11327 :
11328 7 : OGRErr OGRSpatialReference::exportToMICoordSys(char **ppszResult) const
11329 :
11330 : {
11331 7 : *ppszResult = MITABSpatialRef2CoordSys(this);
11332 7 : if (*ppszResult != nullptr && strlen(*ppszResult) > 0)
11333 7 : return OGRERR_NONE;
11334 :
11335 0 : return OGRERR_FAILURE;
11336 : }
11337 :
11338 : /************************************************************************/
11339 : /* OSRImportFromMICoordSys() */
11340 : /************************************************************************/
11341 : /**
11342 : * \brief Import Mapinfo style CoordSys definition.
11343 : *
11344 : * This method is the equivalent of the C++ method
11345 : * OGRSpatialReference::importFromMICoordSys
11346 : */
11347 :
11348 3 : OGRErr OSRImportFromMICoordSys(OGRSpatialReferenceH hSRS,
11349 : const char *pszCoordSys)
11350 :
11351 : {
11352 3 : VALIDATE_POINTER1(hSRS, "OSRImportFromMICoordSys", OGRERR_FAILURE);
11353 :
11354 3 : return ToPointer(hSRS)->importFromMICoordSys(pszCoordSys);
11355 : }
11356 :
11357 : /************************************************************************/
11358 : /* importFromMICoordSys() */
11359 : /************************************************************************/
11360 :
11361 : /**
11362 : * \brief Import Mapinfo style CoordSys definition.
11363 : *
11364 : * The OGRSpatialReference is initialized from the passed Mapinfo style CoordSys
11365 : * definition string.
11366 : *
11367 : * This method is the equivalent of the C function OSRImportFromMICoordSys().
11368 : *
11369 : * @param pszCoordSys Mapinfo style CoordSys definition string.
11370 : *
11371 : * @return OGRERR_NONE on success, OGRERR_FAILURE on failure,
11372 : * OGRERR_UNSUPPORTED_OPERATION if MITAB library was not linked in.
11373 : */
11374 :
11375 17 : OGRErr OGRSpatialReference::importFromMICoordSys(const char *pszCoordSys)
11376 :
11377 : {
11378 17 : OGRSpatialReference *poResult = MITABCoordSys2SpatialRef(pszCoordSys);
11379 :
11380 17 : if (poResult == nullptr)
11381 0 : return OGRERR_FAILURE;
11382 :
11383 17 : *this = *poResult;
11384 17 : delete poResult;
11385 :
11386 17 : return OGRERR_NONE;
11387 : }
11388 :
11389 : /************************************************************************/
11390 : /* OSRCalcInvFlattening() */
11391 : /************************************************************************/
11392 :
11393 : /**
11394 : * \brief Compute inverse flattening from semi-major and semi-minor axis
11395 : *
11396 : * @param dfSemiMajor Semi-major axis length.
11397 : * @param dfSemiMinor Semi-minor axis length.
11398 : *
11399 : * @return inverse flattening, or 0 if both axis are equal.
11400 : * @since GDAL 2.0
11401 : */
11402 :
11403 8137 : double OSRCalcInvFlattening(double dfSemiMajor, double dfSemiMinor)
11404 : {
11405 8137 : if (fabs(dfSemiMajor - dfSemiMinor) < 1e-1)
11406 27 : return 0;
11407 8110 : if (dfSemiMajor <= 0 || dfSemiMinor <= 0 || dfSemiMinor > dfSemiMajor)
11408 : {
11409 0 : CPLError(CE_Failure, CPLE_IllegalArg,
11410 : "OSRCalcInvFlattening(): Wrong input values");
11411 0 : return 0;
11412 : }
11413 :
11414 8110 : return dfSemiMajor / (dfSemiMajor - dfSemiMinor);
11415 : }
11416 :
11417 : /************************************************************************/
11418 : /* OSRCalcInvFlattening() */
11419 : /************************************************************************/
11420 :
11421 : /**
11422 : * \brief Compute semi-minor axis from semi-major axis and inverse flattening.
11423 : *
11424 : * @param dfSemiMajor Semi-major axis length.
11425 : * @param dfInvFlattening Inverse flattening or 0 for sphere.
11426 : *
11427 : * @return semi-minor axis
11428 : * @since GDAL 2.0
11429 : */
11430 :
11431 646 : double OSRCalcSemiMinorFromInvFlattening(double dfSemiMajor,
11432 : double dfInvFlattening)
11433 : {
11434 646 : if (fabs(dfInvFlattening) < 0.000000000001)
11435 101 : return dfSemiMajor;
11436 545 : if (dfSemiMajor <= 0.0 || dfInvFlattening <= 1.0)
11437 : {
11438 0 : CPLError(CE_Failure, CPLE_IllegalArg,
11439 : "OSRCalcSemiMinorFromInvFlattening(): Wrong input values");
11440 0 : return dfSemiMajor;
11441 : }
11442 :
11443 545 : return dfSemiMajor * (1.0 - 1.0 / dfInvFlattening);
11444 : }
11445 :
11446 : /************************************************************************/
11447 : /* GetWGS84SRS() */
11448 : /************************************************************************/
11449 :
11450 : static OGRSpatialReference *poSRSWGS84 = nullptr;
11451 : static CPLMutex *hMutex = nullptr;
11452 :
11453 : /**
11454 : * \brief Returns an instance of a SRS object with WGS84 WKT.
11455 : *
11456 : * Note: the instance will have
11457 : * SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER)
11458 : *
11459 : * The reference counter of the returned object is not increased by this
11460 : * operation.
11461 : *
11462 : * @return instance.
11463 : * @since GDAL 2.0
11464 : */
11465 :
11466 823 : OGRSpatialReference *OGRSpatialReference::GetWGS84SRS()
11467 : {
11468 823 : CPLMutexHolderD(&hMutex);
11469 823 : if (poSRSWGS84 == nullptr)
11470 : {
11471 4 : poSRSWGS84 = new OGRSpatialReference(SRS_WKT_WGS84_LAT_LONG);
11472 4 : poSRSWGS84->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
11473 : }
11474 1646 : return poSRSWGS84;
11475 : }
11476 :
11477 : /************************************************************************/
11478 : /* CleanupSRSWGS84Mutex() */
11479 : /************************************************************************/
11480 :
11481 1118 : static void CleanupSRSWGS84Mutex()
11482 : {
11483 1118 : if (hMutex != nullptr)
11484 : {
11485 2 : poSRSWGS84->Release();
11486 2 : poSRSWGS84 = nullptr;
11487 2 : CPLDestroyMutex(hMutex);
11488 2 : hMutex = nullptr;
11489 : }
11490 1118 : }
11491 :
11492 : /************************************************************************/
11493 : /* OSRImportFromProj4() */
11494 : /************************************************************************/
11495 : /**
11496 : * \brief Import PROJ coordinate string.
11497 : *
11498 : * This function is the same as OGRSpatialReference::importFromProj4().
11499 : */
11500 186 : OGRErr OSRImportFromProj4(OGRSpatialReferenceH hSRS, const char *pszProj4)
11501 :
11502 : {
11503 186 : VALIDATE_POINTER1(hSRS, "OSRImportFromProj4", OGRERR_FAILURE);
11504 :
11505 186 : return OGRSpatialReference::FromHandle(hSRS)->importFromProj4(pszProj4);
11506 : }
11507 :
11508 : /************************************************************************/
11509 : /* importFromProj4() */
11510 : /************************************************************************/
11511 :
11512 : /**
11513 : * \brief Import PROJ coordinate string.
11514 : *
11515 : * The OGRSpatialReference is initialized from the passed PROJs style
11516 : * coordinate system string.
11517 : *
11518 : * Example:
11519 : * pszProj4 = "+proj=utm +zone=11 +datum=WGS84"
11520 : *
11521 : * It is also possible to import "+init=epsg:n" style definitions. Those are
11522 : * a legacy syntax that should be avoided in the future. In particular they will
11523 : * result in CRS objects whose axis order might not correspond to the official
11524 : * EPSG axis order.
11525 : *
11526 : * This method is the equivalent of the C function OSRImportFromProj4().
11527 : *
11528 : * @param pszProj4 the PROJ style string.
11529 : *
11530 : * @return OGRERR_NONE on success or OGRERR_CORRUPT_DATA on failure.
11531 : */
11532 :
11533 706 : OGRErr OGRSpatialReference::importFromProj4(const char *pszProj4)
11534 :
11535 : {
11536 1412 : TAKE_OPTIONAL_LOCK();
11537 :
11538 706 : if (strlen(pszProj4) >= 10000)
11539 : {
11540 1 : CPLError(CE_Failure, CPLE_AppDefined, "Too long PROJ string");
11541 1 : return OGRERR_CORRUPT_DATA;
11542 : }
11543 :
11544 : /* -------------------------------------------------------------------- */
11545 : /* Clear any existing definition. */
11546 : /* -------------------------------------------------------------------- */
11547 705 : Clear();
11548 :
11549 705 : CPLString osProj4(pszProj4);
11550 705 : if (osProj4.find("type=crs") == std::string::npos)
11551 : {
11552 696 : osProj4 += " +type=crs";
11553 : }
11554 :
11555 707 : if (osProj4.find("+init=epsg:") != std::string::npos &&
11556 2 : getenv("PROJ_USE_PROJ4_INIT_RULES") == nullptr)
11557 : {
11558 2 : CPLErrorOnce(CE_Warning, CPLE_AppDefined,
11559 : "+init=epsg:XXXX syntax is deprecated. It might return "
11560 : "a CRS with a non-EPSG compliant axis order.");
11561 : }
11562 705 : proj_context_use_proj4_init_rules(d->getPROJContext(), true);
11563 705 : d->setPjCRS(proj_create(d->getPROJContext(), osProj4.c_str()));
11564 705 : proj_context_use_proj4_init_rules(d->getPROJContext(), false);
11565 705 : return d->m_pj_crs ? OGRERR_NONE : OGRERR_CORRUPT_DATA;
11566 : }
11567 :
11568 : /************************************************************************/
11569 : /* OSRExportToProj4() */
11570 : /************************************************************************/
11571 : /**
11572 : * \brief Export coordinate system in PROJ.4 legacy format.
11573 : *
11574 : * \warning Use of this function is discouraged. Its behavior in GDAL >= 3 /
11575 : * PROJ >= 6 is significantly different from earlier versions. In particular
11576 : * +datum will only encode WGS84, NAD27 and NAD83, and +towgs84/+nadgrids terms
11577 : * will be missing most of the time. PROJ strings to encode CRS should be
11578 : * considered as a legacy solution. Using a AUTHORITY:CODE or WKT representation
11579 : * is the recommended way.
11580 : *
11581 : * This function is the same as OGRSpatialReference::exportToProj4().
11582 : */
11583 427 : OGRErr CPL_STDCALL OSRExportToProj4(OGRSpatialReferenceH hSRS,
11584 : char **ppszReturn)
11585 :
11586 : {
11587 427 : VALIDATE_POINTER1(hSRS, "OSRExportToProj4", OGRERR_FAILURE);
11588 :
11589 427 : *ppszReturn = nullptr;
11590 :
11591 427 : return OGRSpatialReference::FromHandle(hSRS)->exportToProj4(ppszReturn);
11592 : }
11593 :
11594 : /************************************************************************/
11595 : /* exportToProj4() */
11596 : /************************************************************************/
11597 :
11598 : /**
11599 : * \brief Export coordinate system in PROJ.4 legacy format.
11600 : *
11601 : * \warning Use of this function is discouraged. Its behavior in GDAL >= 3 /
11602 : * PROJ >= 6 is significantly different from earlier versions. In particular
11603 : * +datum will only encode WGS84, NAD27 and NAD83, and +towgs84/+nadgrids terms
11604 : * will be missing most of the time. PROJ strings to encode CRS should be
11605 : * considered as a a legacy solution. Using a AUTHORITY:CODE or WKT
11606 : * representation is the recommended way.
11607 : *
11608 : * Converts the loaded coordinate reference system into PROJ format
11609 : * to the extent possible. The string returned in ppszProj4 should be
11610 : * deallocated by the caller with CPLFree() when no longer needed.
11611 : *
11612 : * LOCAL_CS coordinate systems are not translatable. An empty string
11613 : * will be returned along with OGRERR_NONE.
11614 : *
11615 : * Special processing for Transverse Mercator:
11616 : * Starting with GDAL 3.0, if the OSR_USE_APPROX_TMERC configuration option is
11617 : * set to YES, the PROJ definition built from the SRS will use the +approx flag
11618 : * for the tmerc and utm projection methods, rather than the more accurate
11619 : * method.
11620 : *
11621 : * Starting with GDAL 3.0.3, this method will try to add a +towgs84 parameter,
11622 : * if there's none attached yet to the SRS and if the SRS has a EPSG code.
11623 : * See the AddGuessedTOWGS84() method for how this +towgs84 parameter may be
11624 : * added. This automatic addition may be disabled by setting the
11625 : * OSR_ADD_TOWGS84_ON_EXPORT_TO_PROJ4 configuration option to NO.
11626 : *
11627 : * This method is the equivalent of the C function OSRExportToProj4().
11628 : *
11629 : * @param ppszProj4 pointer to which dynamically allocated PROJ definition
11630 : * will be assigned.
11631 : *
11632 : * @return OGRERR_NONE on success or an error code on failure.
11633 : */
11634 :
11635 1370 : OGRErr OGRSpatialReference::exportToProj4(char **ppszProj4) const
11636 :
11637 : {
11638 : // In the past calling this method was thread-safe, even if we never
11639 : // guaranteed it. Now proj_as_proj_string() will cache the result
11640 : // internally, so this is no longer thread-safe.
11641 2740 : std::lock_guard oLock(d->m_mutex);
11642 :
11643 1370 : d->refreshProjObj();
11644 1370 : if (d->m_pj_crs == nullptr || d->m_pjType == PJ_TYPE_ENGINEERING_CRS)
11645 : {
11646 4 : *ppszProj4 = CPLStrdup("");
11647 4 : return OGRERR_FAILURE;
11648 : }
11649 :
11650 : // OSR_USE_ETMERC is here just for legacy
11651 1366 : bool bForceApproxTMerc = false;
11652 1366 : const char *pszUseETMERC = CPLGetConfigOption("OSR_USE_ETMERC", nullptr);
11653 1366 : if (pszUseETMERC && pszUseETMERC[0])
11654 : {
11655 0 : CPLErrorOnce(CE_Warning, CPLE_AppDefined,
11656 : "OSR_USE_ETMERC is a legacy configuration option, which "
11657 : "now has only effect when set to NO (YES is the default). "
11658 : "Use OSR_USE_APPROX_TMERC=YES instead");
11659 0 : bForceApproxTMerc = !CPLTestBool(pszUseETMERC);
11660 : }
11661 : else
11662 : {
11663 : const char *pszUseApproxTMERC =
11664 1366 : CPLGetConfigOption("OSR_USE_APPROX_TMERC", nullptr);
11665 1366 : if (pszUseApproxTMERC && pszUseApproxTMERC[0])
11666 : {
11667 2 : bForceApproxTMerc = CPLTestBool(pszUseApproxTMERC);
11668 : }
11669 : }
11670 1366 : const char *options[] = {
11671 1366 : bForceApproxTMerc ? "USE_APPROX_TMERC=YES" : nullptr, nullptr};
11672 :
11673 1366 : const char *projString = proj_as_proj_string(
11674 1366 : d->getPROJContext(), d->m_pj_crs, PJ_PROJ_4, options);
11675 :
11676 1366 : PJ *boundCRS = nullptr;
11677 2728 : if (projString &&
11678 1362 : (strstr(projString, "+datum=") == nullptr ||
11679 2738 : d->m_pjType == PJ_TYPE_COMPOUND_CRS) &&
11680 545 : CPLTestBool(
11681 : CPLGetConfigOption("OSR_ADD_TOWGS84_ON_EXPORT_TO_PROJ4", "YES")))
11682 : {
11683 545 : boundCRS = GDAL_proj_crs_create_bound_crs_to_WGS84(
11684 545 : d->getPROJContext(), d->m_pj_crs, true,
11685 545 : strstr(projString, "+datum=") == nullptr);
11686 545 : if (boundCRS)
11687 : {
11688 225 : projString = proj_as_proj_string(d->getPROJContext(), boundCRS,
11689 : PJ_PROJ_4, options);
11690 : }
11691 : }
11692 :
11693 1366 : if (projString == nullptr)
11694 : {
11695 4 : *ppszProj4 = CPLStrdup("");
11696 4 : proj_destroy(boundCRS);
11697 4 : return OGRERR_FAILURE;
11698 : }
11699 1362 : *ppszProj4 = CPLStrdup(projString);
11700 1362 : proj_destroy(boundCRS);
11701 1362 : char *pszTypeCrs = strstr(*ppszProj4, " +type=crs");
11702 1362 : if (pszTypeCrs)
11703 1362 : *pszTypeCrs = '\0';
11704 1362 : return OGRERR_NONE;
11705 : }
11706 :
11707 : /************************************************************************/
11708 : /* morphToESRI() */
11709 : /************************************************************************/
11710 : /**
11711 : * \brief Convert in place to ESRI WKT format.
11712 : *
11713 : * The value nodes of this coordinate system are modified in various manners
11714 : * more closely map onto the ESRI concept of WKT format. This includes
11715 : * renaming a variety of projections and arguments, and stripping out
11716 : * nodes note recognised by ESRI (like AUTHORITY and AXIS).
11717 : *
11718 : * \note Since GDAL 3.0, this function has only user-visible effects at
11719 : * exportToWkt() time. It is recommended to use instead exportToWkt(char**,
11720 : * const char* const char*) const with options having FORMAT=WKT1_ESRI.
11721 : *
11722 : * This does the same as the C function OSRMorphToESRI().
11723 : *
11724 : * @return OGRERR_NONE unless something goes badly wrong.
11725 : * @deprecated
11726 : */
11727 :
11728 266 : OGRErr OGRSpatialReference::morphToESRI()
11729 :
11730 : {
11731 266 : TAKE_OPTIONAL_LOCK();
11732 :
11733 266 : d->refreshProjObj();
11734 266 : d->setMorphToESRI(true);
11735 :
11736 532 : return OGRERR_NONE;
11737 : }
11738 :
11739 : /************************************************************************/
11740 : /* OSRMorphToESRI() */
11741 : /************************************************************************/
11742 :
11743 : /**
11744 : * \brief Convert in place to ESRI WKT format.
11745 : *
11746 : * This function is the same as the C++ method
11747 : * OGRSpatialReference::morphToESRI().
11748 : */
11749 102 : OGRErr OSRMorphToESRI(OGRSpatialReferenceH hSRS)
11750 :
11751 : {
11752 102 : VALIDATE_POINTER1(hSRS, "OSRMorphToESRI", OGRERR_FAILURE);
11753 :
11754 102 : return OGRSpatialReference::FromHandle(hSRS)->morphToESRI();
11755 : }
11756 :
11757 : /************************************************************************/
11758 : /* morphFromESRI() */
11759 : /************************************************************************/
11760 :
11761 : /**
11762 : * \brief Convert in place from ESRI WKT format.
11763 : *
11764 : * The value notes of this coordinate system are modified in various manners
11765 : * to adhere more closely to the WKT standard. This mostly involves
11766 : * translating a variety of ESRI names for projections, arguments and
11767 : * datums to "standard" names, as defined by Adam Gawne-Cain's reference
11768 : * translation of EPSG to WKT for the CT specification.
11769 : *
11770 : * \note Since GDAL 3.0, this function is essentially a no-operation, since
11771 : * morphing from ESRI is automatically done by importFromWkt(). Its only
11772 : * effect is to undo the effect of a potential prior call to morphToESRI().
11773 : *
11774 : * This does the same as the C function OSRMorphFromESRI().
11775 : *
11776 : * @return OGRERR_NONE unless something goes badly wrong.
11777 : * @deprecated
11778 : */
11779 :
11780 21 : OGRErr OGRSpatialReference::morphFromESRI()
11781 :
11782 : {
11783 21 : TAKE_OPTIONAL_LOCK();
11784 :
11785 21 : d->refreshProjObj();
11786 21 : d->setMorphToESRI(false);
11787 :
11788 42 : return OGRERR_NONE;
11789 : }
11790 :
11791 : /************************************************************************/
11792 : /* OSRMorphFromESRI() */
11793 : /************************************************************************/
11794 :
11795 : /**
11796 : * \brief Convert in place from ESRI WKT format.
11797 : *
11798 : * This function is the same as the C++ method
11799 : * OGRSpatialReference::morphFromESRI().
11800 : */
11801 20 : OGRErr OSRMorphFromESRI(OGRSpatialReferenceH hSRS)
11802 :
11803 : {
11804 20 : VALIDATE_POINTER1(hSRS, "OSRMorphFromESRI", OGRERR_FAILURE);
11805 :
11806 20 : return OGRSpatialReference::FromHandle(hSRS)->morphFromESRI();
11807 : }
11808 :
11809 : /************************************************************************/
11810 : /* FindMatches() */
11811 : /************************************************************************/
11812 :
11813 : /**
11814 : * \brief Try to identify a match between the passed SRS and a related SRS
11815 : * in a catalog.
11816 : *
11817 : * Matching may be partial, or may fail.
11818 : * Returned entries will be sorted by decreasing match confidence (first
11819 : * entry has the highest match confidence).
11820 : *
11821 : * The exact way matching is done may change in future versions. Starting with
11822 : * GDAL 3.0, it relies on PROJ' proj_identify() function.
11823 : *
11824 : * This method is the same as OSRFindMatches().
11825 : *
11826 : * @param papszOptions NULL terminated list of options or NULL
11827 : * @param pnEntries Output parameter. Number of values in the returned array.
11828 : * @param ppanMatchConfidence Output parameter (or NULL). *ppanMatchConfidence
11829 : * will be allocated to an array of *pnEntries whose values between 0 and 100
11830 : * indicate the confidence in the match. 100 is the highest confidence level.
11831 : * The array must be freed with CPLFree().
11832 : *
11833 : * @return an array of SRS that match the passed SRS, or NULL. Must be freed
11834 : * with OSRFreeSRSArray()
11835 : *
11836 : * @since GDAL 2.3
11837 : *
11838 : * @see OGRSpatialReference::FindBestMatch()
11839 : */
11840 : OGRSpatialReferenceH *
11841 1396 : OGRSpatialReference::FindMatches(char **papszOptions, int *pnEntries,
11842 : int **ppanMatchConfidence) const
11843 : {
11844 2792 : TAKE_OPTIONAL_LOCK();
11845 :
11846 1396 : CPL_IGNORE_RET_VAL(papszOptions);
11847 :
11848 1396 : if (pnEntries)
11849 1396 : *pnEntries = 0;
11850 1396 : if (ppanMatchConfidence)
11851 1396 : *ppanMatchConfidence = nullptr;
11852 :
11853 1396 : d->refreshProjObj();
11854 1396 : if (!d->m_pj_crs)
11855 0 : return nullptr;
11856 :
11857 1396 : int *panConfidence = nullptr;
11858 1396 : auto ctxt = d->getPROJContext();
11859 : auto list =
11860 1396 : proj_identify(ctxt, d->m_pj_crs, nullptr, nullptr, &panConfidence);
11861 1396 : if (!list)
11862 0 : return nullptr;
11863 :
11864 1396 : const int nMatches = proj_list_get_count(list);
11865 :
11866 1396 : if (pnEntries)
11867 1396 : *pnEntries = static_cast<int>(nMatches);
11868 : OGRSpatialReferenceH *pahRet = static_cast<OGRSpatialReferenceH *>(
11869 1396 : CPLCalloc(sizeof(OGRSpatialReferenceH), nMatches + 1));
11870 1396 : if (ppanMatchConfidence)
11871 : {
11872 1396 : *ppanMatchConfidence =
11873 1396 : static_cast<int *>(CPLMalloc(sizeof(int) * (nMatches + 1)));
11874 : }
11875 :
11876 1396 : bool bSortAgain = false;
11877 :
11878 4183 : for (int i = 0; i < nMatches; i++)
11879 : {
11880 2787 : PJ *obj = proj_list_get(ctxt, list, i);
11881 2787 : CPLAssert(obj);
11882 2787 : OGRSpatialReference *poSRS = new OGRSpatialReference();
11883 2787 : poSRS->d->setPjCRS(obj);
11884 2787 : pahRet[i] = ToHandle(poSRS);
11885 :
11886 : // Identify matches that only differ by axis order
11887 9 : if (panConfidence[i] == 50 && GetAxesCount() == 2 &&
11888 2805 : poSRS->GetAxesCount() == 2 &&
11889 2796 : GetDataAxisToSRSAxisMapping() == std::vector<int>{1, 2})
11890 : {
11891 9 : OGRAxisOrientation eThisAxis0 = OAO_Other;
11892 9 : OGRAxisOrientation eThisAxis1 = OAO_Other;
11893 9 : OGRAxisOrientation eSRSAxis0 = OAO_Other;
11894 9 : OGRAxisOrientation eSRSAxis1 = OAO_Other;
11895 9 : GetAxis(nullptr, 0, &eThisAxis0);
11896 9 : GetAxis(nullptr, 1, &eThisAxis1);
11897 9 : poSRS->GetAxis(nullptr, 0, &eSRSAxis0);
11898 9 : poSRS->GetAxis(nullptr, 1, &eSRSAxis1);
11899 9 : if (eThisAxis0 == OAO_East && eThisAxis1 == OAO_North &&
11900 9 : eSRSAxis0 == OAO_North && eSRSAxis1 == OAO_East)
11901 : {
11902 : auto pj_crs_normalized =
11903 9 : proj_normalize_for_visualization(ctxt, poSRS->d->m_pj_crs);
11904 9 : if (pj_crs_normalized)
11905 : {
11906 9 : if (proj_is_equivalent_to(d->m_pj_crs, pj_crs_normalized,
11907 9 : PJ_COMP_EQUIVALENT))
11908 : {
11909 3 : bSortAgain = true;
11910 3 : panConfidence[i] = 90;
11911 3 : poSRS->SetDataAxisToSRSAxisMapping({2, 1});
11912 : }
11913 9 : proj_destroy(pj_crs_normalized);
11914 : }
11915 : }
11916 : }
11917 :
11918 2787 : if (ppanMatchConfidence)
11919 2787 : (*ppanMatchConfidence)[i] = panConfidence[i];
11920 : }
11921 :
11922 1396 : if (bSortAgain)
11923 : {
11924 3 : std::vector<int> anIndices;
11925 12 : for (int i = 0; i < nMatches; ++i)
11926 9 : anIndices.push_back(i);
11927 :
11928 3 : std::stable_sort(anIndices.begin(), anIndices.end(),
11929 9 : [&panConfidence](int i, int j)
11930 9 : { return panConfidence[i] > panConfidence[j]; });
11931 :
11932 : OGRSpatialReferenceH *pahRetSorted =
11933 : static_cast<OGRSpatialReferenceH *>(
11934 3 : CPLCalloc(sizeof(OGRSpatialReferenceH), nMatches + 1));
11935 12 : for (int i = 0; i < nMatches; ++i)
11936 : {
11937 9 : pahRetSorted[i] = pahRet[anIndices[i]];
11938 9 : if (ppanMatchConfidence)
11939 9 : (*ppanMatchConfidence)[i] = panConfidence[anIndices[i]];
11940 : }
11941 3 : CPLFree(pahRet);
11942 3 : pahRet = pahRetSorted;
11943 : }
11944 :
11945 1396 : pahRet[nMatches] = nullptr;
11946 1396 : proj_list_destroy(list);
11947 1396 : proj_int_list_destroy(panConfidence);
11948 :
11949 1396 : return pahRet;
11950 : }
11951 :
11952 : /************************************************************************/
11953 : /* importFromEPSGA() */
11954 : /************************************************************************/
11955 :
11956 : /**
11957 : * \brief Initialize SRS based on EPSG geographic, projected or vertical CRS
11958 : * code.
11959 : *
11960 : * This method will initialize the spatial reference based on the
11961 : * passed in EPSG CRS code found in the PROJ database.
11962 : *
11963 : * Since GDAL 3.0, this method is identical to importFromEPSG().
11964 : *
11965 : * Before GDAL 3.0.3, this method would try to attach a 3-parameter or
11966 : * 7-parameter Helmert transformation to WGS84 when there is one and only one
11967 : * such method available for the CRS. This behavior might not always be
11968 : * desirable, so starting with GDAL 3.0.3, this is no longer done unless
11969 : * the OSR_ADD_TOWGS84_ON_IMPORT_FROM_EPSG configuration option is set to YES.
11970 : * The AddGuessedTOWGS84() method can also be used for that purpose.
11971 : *
11972 : * The method will also by default substitute a deprecated EPSG code by its
11973 : * non-deprecated replacement. If this behavior is not desired, the
11974 : * OSR_USE_NON_DEPRECATED configuration option can be set to NO.
11975 : *
11976 : * This method is the same as the C function OSRImportFromEPSGA().
11977 : *
11978 : * @param nCode a CRS code.
11979 : *
11980 : * @return OGRERR_NONE on success, or an error code on failure.
11981 : */
11982 :
11983 40688 : OGRErr OGRSpatialReference::importFromEPSGA(int nCode)
11984 :
11985 : {
11986 81375 : TAKE_OPTIONAL_LOCK();
11987 :
11988 40687 : Clear();
11989 :
11990 : const char *pszUseNonDeprecated =
11991 40689 : CPLGetConfigOption("OSR_USE_NON_DEPRECATED", nullptr);
11992 : const bool bUseNonDeprecated =
11993 40689 : CPLTestBool(pszUseNonDeprecated ? pszUseNonDeprecated : "YES");
11994 40688 : const bool bAddTOWGS84 = CPLTestBool(
11995 : CPLGetConfigOption("OSR_ADD_TOWGS84_ON_IMPORT_FROM_EPSG", "NO"));
11996 40689 : auto tlsCache = OSRGetProjTLSCache();
11997 40689 : if (tlsCache)
11998 : {
11999 : auto cachedObj =
12000 40688 : tlsCache->GetPJForEPSGCode(nCode, bUseNonDeprecated, bAddTOWGS84);
12001 40688 : if (cachedObj)
12002 : {
12003 30900 : d->setPjCRS(cachedObj);
12004 30900 : return OGRERR_NONE;
12005 : }
12006 : }
12007 :
12008 19577 : CPLString osCode;
12009 9787 : osCode.Printf("%d", nCode);
12010 : PJ *obj;
12011 9787 : constexpr int FIRST_NON_DEPRECATED_ESRI_CODE = 53001;
12012 9787 : if (nCode < FIRST_NON_DEPRECATED_ESRI_CODE)
12013 : {
12014 9783 : obj = proj_create_from_database(d->getPROJContext(), "EPSG",
12015 : osCode.c_str(), PJ_CATEGORY_CRS, true,
12016 : nullptr);
12017 9784 : if (!obj)
12018 : {
12019 23 : return OGRERR_FAILURE;
12020 : }
12021 : }
12022 : else
12023 : {
12024 : // Likely to be an ESRI CRS...
12025 4 : CPLErr eLastErrorType = CE_None;
12026 4 : CPLErrorNum eLastErrorNum = CPLE_None;
12027 4 : std::string osLastErrorMsg;
12028 4 : bool bIsESRI = false;
12029 : {
12030 8 : CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
12031 4 : CPLErrorReset();
12032 4 : obj = proj_create_from_database(d->getPROJContext(), "EPSG",
12033 : osCode.c_str(), PJ_CATEGORY_CRS,
12034 : true, nullptr);
12035 4 : if (!obj)
12036 : {
12037 1 : eLastErrorType = CPLGetLastErrorType();
12038 1 : eLastErrorNum = CPLGetLastErrorNo();
12039 1 : osLastErrorMsg = CPLGetLastErrorMsg();
12040 1 : obj = proj_create_from_database(d->getPROJContext(), "ESRI",
12041 : osCode.c_str(), PJ_CATEGORY_CRS,
12042 : true, nullptr);
12043 1 : if (obj)
12044 0 : bIsESRI = true;
12045 : }
12046 : }
12047 4 : if (!obj)
12048 : {
12049 1 : if (eLastErrorType != CE_None)
12050 1 : CPLError(eLastErrorType, eLastErrorNum, "%s",
12051 : osLastErrorMsg.c_str());
12052 1 : return OGRERR_FAILURE;
12053 : }
12054 3 : if (bIsESRI)
12055 : {
12056 0 : CPLError(CE_Warning, CPLE_AppDefined,
12057 : "EPSG:%d is not a valid CRS code, but ESRI:%d is. "
12058 : "Assuming ESRI:%d was meant",
12059 : nCode, nCode, nCode);
12060 : }
12061 : }
12062 :
12063 9764 : if (bUseNonDeprecated && proj_is_deprecated(obj))
12064 : {
12065 409 : auto list = proj_get_non_deprecated(d->getPROJContext(), obj);
12066 409 : if (list)
12067 : {
12068 409 : const auto count = proj_list_get_count(list);
12069 409 : if (count == 1)
12070 : {
12071 : auto nonDeprecated =
12072 359 : proj_list_get(d->getPROJContext(), list, 0);
12073 359 : if (nonDeprecated)
12074 : {
12075 359 : if (pszUseNonDeprecated == nullptr)
12076 : {
12077 : const char *pszNewAuth =
12078 359 : proj_get_id_auth_name(nonDeprecated, 0);
12079 : const char *pszNewCode =
12080 359 : proj_get_id_code(nonDeprecated, 0);
12081 359 : CPLError(CE_Warning, CPLE_AppDefined,
12082 : "CRS EPSG:%d is deprecated. "
12083 : "Its non-deprecated replacement %s:%s "
12084 : "will be used instead. "
12085 : "To use the original CRS, set the "
12086 : "OSR_USE_NON_DEPRECATED "
12087 : "configuration option to NO.",
12088 : nCode, pszNewAuth ? pszNewAuth : "(null)",
12089 : pszNewCode ? pszNewCode : "(null)");
12090 : }
12091 359 : proj_destroy(obj);
12092 359 : obj = nonDeprecated;
12093 : }
12094 : }
12095 : }
12096 409 : proj_list_destroy(list);
12097 : }
12098 :
12099 9764 : if (bAddTOWGS84)
12100 : {
12101 1 : auto boundCRS = proj_crs_create_bound_crs_to_WGS84(d->getPROJContext(),
12102 : obj, nullptr);
12103 1 : if (boundCRS)
12104 : {
12105 1 : proj_destroy(obj);
12106 1 : obj = boundCRS;
12107 : }
12108 : }
12109 :
12110 9764 : d->setPjCRS(obj);
12111 :
12112 9765 : if (tlsCache)
12113 : {
12114 9764 : tlsCache->CachePJForEPSGCode(nCode, bUseNonDeprecated, bAddTOWGS84,
12115 : obj);
12116 : }
12117 :
12118 9764 : return OGRERR_NONE;
12119 : }
12120 :
12121 : /************************************************************************/
12122 : /* AddGuessedTOWGS84() */
12123 : /************************************************************************/
12124 :
12125 : /**
12126 : * \brief Try to add a a 3-parameter or 7-parameter Helmert transformation
12127 : * to WGS84.
12128 : *
12129 : * This method try to attach a 3-parameter or 7-parameter Helmert transformation
12130 : * to WGS84 when there is one and only one such method available for the CRS.
12131 : * Note: this is more restrictive to how GDAL < 3 worked.
12132 : *
12133 : * This method is the same as the C function OSRAddGuessedTOWGS84().
12134 : *
12135 : * @return OGRERR_NONE on success, or an error code on failure (the CRS has
12136 : * already a transformation to WGS84 or none matching could be found).
12137 : *
12138 : * @since GDAL 3.0.3
12139 : */
12140 18 : OGRErr OGRSpatialReference::AddGuessedTOWGS84()
12141 : {
12142 36 : TAKE_OPTIONAL_LOCK();
12143 :
12144 18 : d->refreshProjObj();
12145 18 : if (!d->m_pj_crs)
12146 0 : return OGRERR_FAILURE;
12147 18 : auto boundCRS = GDAL_proj_crs_create_bound_crs_to_WGS84(
12148 18 : d->getPROJContext(), d->m_pj_crs, false, true);
12149 18 : if (!boundCRS)
12150 : {
12151 0 : return OGRERR_FAILURE;
12152 : }
12153 18 : d->setPjCRS(boundCRS);
12154 18 : return OGRERR_NONE;
12155 : }
12156 :
12157 : /************************************************************************/
12158 : /* OSRImportFromEPSGA() */
12159 : /************************************************************************/
12160 :
12161 : /**
12162 : * \brief Try to add a a 3-parameter or 7-parameter Helmert transformation
12163 : * to WGS84.
12164 : *
12165 : * This function is the same as OGRSpatialReference::AddGuessedTOWGS84().
12166 : *
12167 : * @since GDAL 3.0.3
12168 : */
12169 :
12170 2 : OGRErr OSRAddGuessedTOWGS84(OGRSpatialReferenceH hSRS)
12171 :
12172 : {
12173 2 : VALIDATE_POINTER1(hSRS, "OSRAddGuessedTOWGS84", OGRERR_FAILURE);
12174 :
12175 2 : return OGRSpatialReference::FromHandle(hSRS)->AddGuessedTOWGS84();
12176 : }
12177 :
12178 : /************************************************************************/
12179 : /* OSRImportFromEPSGA() */
12180 : /************************************************************************/
12181 :
12182 : /**
12183 : * \brief Initialize SRS based on EPSG geographic, projected or vertical CRS
12184 : * code.
12185 : *
12186 : * This function is the same as OGRSpatialReference::importFromEPSGA().
12187 : */
12188 :
12189 3 : OGRErr CPL_STDCALL OSRImportFromEPSGA(OGRSpatialReferenceH hSRS, int nCode)
12190 :
12191 : {
12192 3 : VALIDATE_POINTER1(hSRS, "OSRImportFromEPSGA", OGRERR_FAILURE);
12193 :
12194 3 : return OGRSpatialReference::FromHandle(hSRS)->importFromEPSGA(nCode);
12195 : }
12196 :
12197 : /************************************************************************/
12198 : /* importFromEPSG() */
12199 : /************************************************************************/
12200 :
12201 : /**
12202 : * \brief Initialize SRS based on EPSG geographic, projected or vertical CRS
12203 : * code.
12204 : *
12205 : * This method will initialize the spatial reference based on the
12206 : * passed in EPSG CRS code found in the PROJ database.
12207 : *
12208 : * This method is the same as the C function OSRImportFromEPSG().
12209 : *
12210 : * Before GDAL 3.0.3, this method would try to attach a 3-parameter or
12211 : * 7-parameter Helmert transformation to WGS84 when there is one and only one
12212 : * such method available for the CRS. This behavior might not always be
12213 : * desirable, so starting with GDAL 3.0.3, this is no longer done unless
12214 : * the OSR_ADD_TOWGS84_ON_IMPORT_FROM_EPSG configuration option is set to YES.
12215 : *
12216 : * @param nCode a GCS or PCS code from the horizontal coordinate system table.
12217 : *
12218 : * @return OGRERR_NONE on success, or an error code on failure.
12219 : */
12220 :
12221 37127 : OGRErr OGRSpatialReference::importFromEPSG(int nCode)
12222 :
12223 : {
12224 37127 : return importFromEPSGA(nCode);
12225 : }
12226 :
12227 : /************************************************************************/
12228 : /* OSRImportFromEPSG() */
12229 : /************************************************************************/
12230 :
12231 : /**
12232 : * \brief Initialize SRS based on EPSG geographic, projected or vertical CRS
12233 : * code.
12234 : *
12235 : * This function is the same as OGRSpatialReference::importFromEPSG().
12236 : */
12237 :
12238 1445 : OGRErr CPL_STDCALL OSRImportFromEPSG(OGRSpatialReferenceH hSRS, int nCode)
12239 :
12240 : {
12241 1445 : VALIDATE_POINTER1(hSRS, "OSRImportFromEPSG", OGRERR_FAILURE);
12242 :
12243 1445 : return OGRSpatialReference::FromHandle(hSRS)->importFromEPSG(nCode);
12244 : }
12245 :
12246 : /************************************************************************/
12247 : /* EPSGTreatsAsLatLong() */
12248 : /************************************************************************/
12249 :
12250 : /**
12251 : * \brief This method returns TRUE if this geographic coordinate
12252 : * system should be treated as having lat/long coordinate ordering.
12253 : *
12254 : * Currently this returns TRUE for all geographic coordinate systems
12255 : * with axes set defining it as lat, long (prior to GDAL 3.10, it
12256 : * also checked that the CRS had belonged to EPSG authority, but this check
12257 : * has now been removed).
12258 : *
12259 : * \note Important change of behavior since GDAL 3.0. In previous versions,
12260 : * geographic CRS imported with importFromEPSG() would cause this method to
12261 : * return FALSE on them, whereas now it returns TRUE, since importFromEPSG()
12262 : * is now equivalent to importFromEPSGA().
12263 : *
12264 : * FALSE will be returned for all coordinate systems that are not geographic,
12265 : * or whose axes ordering is not latitude, longitude.
12266 : *
12267 : * This method is the same as the C function OSREPSGTreatsAsLatLong().
12268 : *
12269 : * @return TRUE or FALSE.
12270 : */
12271 :
12272 786 : int OGRSpatialReference::EPSGTreatsAsLatLong() const
12273 :
12274 : {
12275 1572 : TAKE_OPTIONAL_LOCK();
12276 :
12277 786 : if (!IsGeographic())
12278 618 : return FALSE;
12279 :
12280 168 : d->demoteFromBoundCRS();
12281 :
12282 168 : bool ret = false;
12283 168 : if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
12284 : {
12285 : auto horizCRS =
12286 3 : proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
12287 3 : if (horizCRS)
12288 : {
12289 : auto cs =
12290 3 : proj_crs_get_coordinate_system(d->getPROJContext(), horizCRS);
12291 3 : if (cs)
12292 : {
12293 3 : const char *pszDirection = nullptr;
12294 3 : if (proj_cs_get_axis_info(d->getPROJContext(), cs, 0, nullptr,
12295 : nullptr, &pszDirection, nullptr,
12296 3 : nullptr, nullptr, nullptr))
12297 : {
12298 3 : if (EQUAL(pszDirection, "north"))
12299 : {
12300 3 : ret = true;
12301 : }
12302 : }
12303 :
12304 3 : proj_destroy(cs);
12305 : }
12306 :
12307 3 : proj_destroy(horizCRS);
12308 : }
12309 : }
12310 : else
12311 : {
12312 : auto cs =
12313 165 : proj_crs_get_coordinate_system(d->getPROJContext(), d->m_pj_crs);
12314 165 : if (cs)
12315 : {
12316 165 : const char *pszDirection = nullptr;
12317 165 : if (proj_cs_get_axis_info(d->getPROJContext(), cs, 0, nullptr,
12318 : nullptr, &pszDirection, nullptr, nullptr,
12319 165 : nullptr, nullptr))
12320 : {
12321 165 : if (EQUAL(pszDirection, "north"))
12322 : {
12323 116 : ret = true;
12324 : }
12325 : }
12326 :
12327 165 : proj_destroy(cs);
12328 : }
12329 : }
12330 168 : d->undoDemoteFromBoundCRS();
12331 :
12332 168 : return ret;
12333 : }
12334 :
12335 : /************************************************************************/
12336 : /* OSREPSGTreatsAsLatLong() */
12337 : /************************************************************************/
12338 :
12339 : /**
12340 : * \brief This function returns TRUE if this geographic coordinate
12341 : * system should be treated as having lat/long coordinate ordering.
12342 : *
12343 : * This function is the same as OGRSpatialReference::OSREPSGTreatsAsLatLong().
12344 : */
12345 :
12346 180 : int OSREPSGTreatsAsLatLong(OGRSpatialReferenceH hSRS)
12347 :
12348 : {
12349 180 : VALIDATE_POINTER1(hSRS, "OSREPSGTreatsAsLatLong", OGRERR_FAILURE);
12350 :
12351 180 : return OGRSpatialReference::FromHandle(hSRS)->EPSGTreatsAsLatLong();
12352 : }
12353 :
12354 : /************************************************************************/
12355 : /* EPSGTreatsAsNorthingEasting() */
12356 : /************************************************************************/
12357 :
12358 : /**
12359 : * \brief This method returns TRUE if this projected coordinate
12360 : * system should be treated as having northing/easting coordinate ordering.
12361 : *
12362 : * Currently this returns TRUE for all projected coordinate systems
12363 : * with axes set defining it as northing, easting (prior to GDAL 3.10, it
12364 : * also checked that the CRS had belonged to EPSG authority, but this check
12365 : * has now been removed).
12366 : *
12367 : * \note Important change of behavior since GDAL 3.0. In previous versions,
12368 : * projected CRS with northing, easting axis order imported with
12369 : * importFromEPSG() would cause this method to
12370 : * return FALSE on them, whereas now it returns TRUE, since importFromEPSG()
12371 : * is now equivalent to importFromEPSGA().
12372 : *
12373 : * FALSE will be returned for all coordinate systems that are not projected,
12374 : * or whose axes ordering is not northing, easting.
12375 : *
12376 : * This method is the same as the C function EPSGTreatsAsNorthingEasting().
12377 : *
12378 : * @return TRUE or FALSE.
12379 : *
12380 : * @since OGR 1.10.0
12381 : */
12382 :
12383 677 : int OGRSpatialReference::EPSGTreatsAsNorthingEasting() const
12384 :
12385 : {
12386 1354 : TAKE_OPTIONAL_LOCK();
12387 :
12388 677 : if (!IsProjected())
12389 46 : return FALSE;
12390 :
12391 631 : d->demoteFromBoundCRS();
12392 : PJ *projCRS;
12393 631 : const auto ctxt = d->getPROJContext();
12394 631 : if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
12395 : {
12396 4 : projCRS = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
12397 4 : if (!projCRS || proj_get_type(projCRS) != PJ_TYPE_PROJECTED_CRS)
12398 : {
12399 0 : d->undoDemoteFromBoundCRS();
12400 0 : proj_destroy(projCRS);
12401 0 : return FALSE;
12402 : }
12403 : }
12404 : else
12405 : {
12406 627 : projCRS = proj_clone(ctxt, d->m_pj_crs);
12407 : }
12408 :
12409 631 : bool ret = false;
12410 631 : auto cs = proj_crs_get_coordinate_system(ctxt, projCRS);
12411 631 : proj_destroy(projCRS);
12412 631 : d->undoDemoteFromBoundCRS();
12413 :
12414 631 : if (cs)
12415 : {
12416 631 : ret = isNorthEastAxisOrder(ctxt, cs);
12417 631 : proj_destroy(cs);
12418 : }
12419 :
12420 631 : return ret;
12421 : }
12422 :
12423 : /************************************************************************/
12424 : /* OSREPSGTreatsAsNorthingEasting() */
12425 : /************************************************************************/
12426 :
12427 : /**
12428 : * \brief This function returns TRUE if this projected coordinate
12429 : * system should be treated as having northing/easting coordinate ordering.
12430 : *
12431 : * This function is the same as
12432 : * OGRSpatialReference::EPSGTreatsAsNorthingEasting().
12433 : *
12434 : * @since OGR 1.10.0
12435 : */
12436 :
12437 187 : int OSREPSGTreatsAsNorthingEasting(OGRSpatialReferenceH hSRS)
12438 :
12439 : {
12440 187 : VALIDATE_POINTER1(hSRS, "OSREPSGTreatsAsNorthingEasting", OGRERR_FAILURE);
12441 :
12442 187 : return OGRSpatialReference::FromHandle(hSRS)->EPSGTreatsAsNorthingEasting();
12443 : }
12444 :
12445 : /************************************************************************/
12446 : /* ImportFromESRIWisconsinWKT() */
12447 : /* */
12448 : /* Search a ESRI State Plane WKT and import it. */
12449 : /************************************************************************/
12450 :
12451 : // This is only used by the HFA driver and somewhat dubious we really need that
12452 : // Coming from an old ESRI merge
12453 :
12454 1 : OGRErr OGRSpatialReference::ImportFromESRIWisconsinWKT(const char *prjName,
12455 : double centralMeridian,
12456 : double latOfOrigin,
12457 : const char *unitsName,
12458 : const char *crsName)
12459 : {
12460 2 : TAKE_OPTIONAL_LOCK();
12461 :
12462 1 : if (centralMeridian < -93 || centralMeridian > -87)
12463 0 : return OGRERR_FAILURE;
12464 1 : if (latOfOrigin < 40 || latOfOrigin > 47)
12465 0 : return OGRERR_FAILURE;
12466 :
12467 : // If the CS name is known.
12468 1 : if (!prjName && !unitsName && crsName)
12469 : {
12470 0 : const PJ_TYPE type = PJ_TYPE_PROJECTED_CRS;
12471 0 : PJ_OBJ_LIST *list = proj_create_from_name(
12472 : d->getPROJContext(), "ESRI", crsName, &type, 1, false, 1, nullptr);
12473 0 : if (list)
12474 : {
12475 0 : if (proj_list_get_count(list) == 1)
12476 : {
12477 0 : auto crs = proj_list_get(d->getPROJContext(), list, 0);
12478 0 : if (crs)
12479 : {
12480 0 : Clear();
12481 0 : d->setPjCRS(crs);
12482 0 : proj_list_destroy(list);
12483 0 : return OGRERR_NONE;
12484 : }
12485 : }
12486 0 : proj_list_destroy(list);
12487 : }
12488 0 : return OGRERR_FAILURE;
12489 : }
12490 :
12491 1 : if (prjName == nullptr || unitsName == nullptr)
12492 : {
12493 0 : return OGRERR_FAILURE;
12494 : }
12495 :
12496 1 : const PJ_TYPE type = PJ_TYPE_PROJECTED_CRS;
12497 1 : PJ_OBJ_LIST *list = proj_create_from_name(d->getPROJContext(), "ESRI",
12498 : "NAD_1983_HARN_WISCRS_", &type, 1,
12499 : true, 0, nullptr);
12500 1 : if (list)
12501 : {
12502 1 : const auto listSize = proj_list_get_count(list);
12503 8 : for (int i = 0; i < listSize; i++)
12504 : {
12505 8 : auto crs = proj_list_get(d->getPROJContext(), list, i);
12506 8 : if (!crs)
12507 : {
12508 7 : continue;
12509 : }
12510 :
12511 8 : auto conv = proj_crs_get_coordoperation(d->getPROJContext(), crs);
12512 8 : if (!conv)
12513 : {
12514 0 : proj_destroy(crs);
12515 0 : continue;
12516 : }
12517 8 : const char *pszMethodCode = nullptr;
12518 8 : proj_coordoperation_get_method_info(
12519 : d->getPROJContext(), conv, nullptr, nullptr, &pszMethodCode);
12520 8 : const int nMethodCode = atoi(pszMethodCode ? pszMethodCode : "0");
12521 8 : if (!((EQUAL(prjName, SRS_PT_TRANSVERSE_MERCATOR) &&
12522 : nMethodCode == EPSG_CODE_METHOD_TRANSVERSE_MERCATOR) ||
12523 3 : (EQUAL(prjName, "Lambert_Conformal_Conic") &&
12524 : nMethodCode ==
12525 : EPSG_CODE_METHOD_LAMBERT_CONIC_CONFORMAL_1SP)))
12526 : {
12527 3 : proj_destroy(crs);
12528 3 : proj_destroy(conv);
12529 3 : continue;
12530 : }
12531 :
12532 : auto coordSys =
12533 5 : proj_crs_get_coordinate_system(d->getPROJContext(), crs);
12534 5 : if (!coordSys)
12535 : {
12536 0 : proj_destroy(crs);
12537 0 : proj_destroy(conv);
12538 0 : continue;
12539 : }
12540 :
12541 5 : double dfConvFactor = 0.0;
12542 5 : proj_cs_get_axis_info(d->getPROJContext(), coordSys, 0, nullptr,
12543 : nullptr, nullptr, &dfConvFactor, nullptr,
12544 : nullptr, nullptr);
12545 5 : proj_destroy(coordSys);
12546 :
12547 6 : if ((EQUAL(unitsName, "meters") && dfConvFactor != 1.0) ||
12548 1 : (!EQUAL(unitsName, "meters") &&
12549 0 : std::fabs(dfConvFactor - CPLAtof(SRS_UL_US_FOOT_CONV)) >
12550 : 1e-10))
12551 : {
12552 4 : proj_destroy(crs);
12553 4 : proj_destroy(conv);
12554 4 : continue;
12555 : }
12556 :
12557 1 : int idx_lat = proj_coordoperation_get_param_index(
12558 : d->getPROJContext(), conv,
12559 : EPSG_NAME_PARAMETER_LATITUDE_OF_NATURAL_ORIGIN);
12560 1 : double valueLat = -1000;
12561 1 : proj_coordoperation_get_param(d->getPROJContext(), conv, idx_lat,
12562 : nullptr, nullptr, nullptr, &valueLat,
12563 : nullptr, nullptr, nullptr, nullptr,
12564 : nullptr, nullptr);
12565 1 : int idx_lon = proj_coordoperation_get_param_index(
12566 : d->getPROJContext(), conv,
12567 : EPSG_NAME_PARAMETER_LONGITUDE_OF_NATURAL_ORIGIN);
12568 1 : double valueLong = -1000;
12569 1 : proj_coordoperation_get_param(d->getPROJContext(), conv, idx_lon,
12570 : nullptr, nullptr, nullptr, &valueLong,
12571 : nullptr, nullptr, nullptr, nullptr,
12572 : nullptr, nullptr);
12573 1 : if (std::fabs(centralMeridian - valueLong) <= 1e-10 &&
12574 1 : std::fabs(latOfOrigin - valueLat) <= 1e-10)
12575 : {
12576 1 : Clear();
12577 1 : d->setPjCRS(crs);
12578 1 : proj_list_destroy(list);
12579 1 : proj_destroy(conv);
12580 1 : return OGRERR_NONE;
12581 : }
12582 :
12583 0 : proj_destroy(crs);
12584 0 : proj_destroy(conv);
12585 : }
12586 0 : proj_list_destroy(list);
12587 : }
12588 :
12589 0 : return OGRERR_FAILURE;
12590 : }
12591 :
12592 : /************************************************************************/
12593 : /* GetAxisMappingStrategy() */
12594 : /************************************************************************/
12595 :
12596 : /** \brief Return the data axis to CRS axis mapping strategy.
12597 : *
12598 : * <ul>
12599 : * <li>OAMS_TRADITIONAL_GIS_ORDER means that for geographic CRS with
12600 : * lat/long order, the data will still be long/lat ordered. Similarly for
12601 : * a projected CRS with northing/easting order, the data will still be
12602 : * easting/northing ordered.
12603 : * <li>OAMS_AUTHORITY_COMPLIANT means that the data axis will be identical to
12604 : * the CRS axis.
12605 : * <li>OAMS_CUSTOM means that the data axis are customly defined with
12606 : * SetDataAxisToSRSAxisMapping()
12607 : * </ul>
12608 : * @return the data axis to CRS axis mapping strategy.
12609 : * @since GDAL 3.0
12610 : */
12611 72 : OSRAxisMappingStrategy OGRSpatialReference::GetAxisMappingStrategy() const
12612 : {
12613 72 : TAKE_OPTIONAL_LOCK();
12614 :
12615 144 : return d->m_axisMappingStrategy;
12616 : }
12617 :
12618 : /************************************************************************/
12619 : /* OSRGetAxisMappingStrategy() */
12620 : /************************************************************************/
12621 :
12622 : /** \brief Return the data axis to CRS axis mapping strategy.
12623 : *
12624 : * See OGRSpatialReference::GetAxisMappingStrategy()
12625 : * @since GDAL 3.0
12626 : */
12627 37 : OSRAxisMappingStrategy OSRGetAxisMappingStrategy(OGRSpatialReferenceH hSRS)
12628 : {
12629 37 : VALIDATE_POINTER1(hSRS, "OSRGetAxisMappingStrategy", OAMS_CUSTOM);
12630 :
12631 37 : return OGRSpatialReference::FromHandle(hSRS)->GetAxisMappingStrategy();
12632 : }
12633 :
12634 : /************************************************************************/
12635 : /* SetAxisMappingStrategy() */
12636 : /************************************************************************/
12637 :
12638 : /** \brief Set the data axis to CRS axis mapping strategy.
12639 : *
12640 : * Starting with GDAL 3.5, the OSR_DEFAULT_AXIS_MAPPING_STRATEGY configuration
12641 : * option can be set to "TRADITIONAL_GIS_ORDER" / "AUTHORITY_COMPLIANT" (the
12642 : * later being the default value when the option is not set) to control the
12643 : * value of the data axis to CRS axis mapping strategy when a
12644 : * OSRSpatialReference object is created. Calling SetAxisMappingStrategy() will
12645 : * override this default value.
12646 : *
12647 : * See OGRSpatialReference::GetAxisMappingStrategy()
12648 : * @since GDAL 3.0
12649 : */
12650 84417 : void OGRSpatialReference::SetAxisMappingStrategy(
12651 : OSRAxisMappingStrategy strategy)
12652 : {
12653 168498 : TAKE_OPTIONAL_LOCK();
12654 :
12655 84099 : d->m_axisMappingStrategy = strategy;
12656 84216 : d->refreshAxisMapping();
12657 83968 : }
12658 :
12659 : /************************************************************************/
12660 : /* OSRSetAxisMappingStrategy() */
12661 : /************************************************************************/
12662 :
12663 : /** \brief Set the data axis to CRS axis mapping strategy.
12664 : *
12665 : * See OGRSpatialReference::SetAxisMappingStrategy()
12666 : * @since GDAL 3.0
12667 : */
12668 840 : void OSRSetAxisMappingStrategy(OGRSpatialReferenceH hSRS,
12669 : OSRAxisMappingStrategy strategy)
12670 : {
12671 840 : VALIDATE_POINTER0(hSRS, "OSRSetAxisMappingStrategy");
12672 :
12673 840 : OGRSpatialReference::FromHandle(hSRS)->SetAxisMappingStrategy(strategy);
12674 : }
12675 :
12676 : /************************************************************************/
12677 : /* GetDataAxisToSRSAxisMapping() */
12678 : /************************************************************************/
12679 :
12680 : /** \brief Return the data axis to SRS axis mapping.
12681 : *
12682 : * The number of elements of the vector will be the number of axis of the CRS.
12683 : * Values start at 1.
12684 : *
12685 : * If m = GetDataAxisToSRSAxisMapping(), then m[0] is the data axis number
12686 : * for the first axis of the CRS.
12687 : *
12688 : * @since GDAL 3.0
12689 : */
12690 4003440 : const std::vector<int> &OGRSpatialReference::GetDataAxisToSRSAxisMapping() const
12691 : {
12692 4003440 : TAKE_OPTIONAL_LOCK();
12693 :
12694 8006400 : return d->m_axisMapping;
12695 : }
12696 :
12697 : /************************************************************************/
12698 : /* OSRGetDataAxisToSRSAxisMapping() */
12699 : /************************************************************************/
12700 :
12701 : /** \brief Return the data axis to SRS axis mapping.
12702 : *
12703 : * See OGRSpatialReference::GetDataAxisToSRSAxisMapping()
12704 : *
12705 : * @since GDAL 3.0
12706 : */
12707 209 : const int *OSRGetDataAxisToSRSAxisMapping(OGRSpatialReferenceH hSRS,
12708 : int *pnCount)
12709 : {
12710 209 : VALIDATE_POINTER1(hSRS, "OSRGetDataAxisToSRSAxisMapping", nullptr);
12711 209 : VALIDATE_POINTER1(pnCount, "OSRGetDataAxisToSRSAxisMapping", nullptr);
12712 :
12713 : const auto &v =
12714 209 : OGRSpatialReference::FromHandle(hSRS)->GetDataAxisToSRSAxisMapping();
12715 209 : *pnCount = static_cast<int>(v.size());
12716 209 : return v.data();
12717 : }
12718 :
12719 : /************************************************************************/
12720 : /* SetDataAxisToSRSAxisMapping() */
12721 : /************************************************************************/
12722 :
12723 : /** \brief Set a custom data axis to CRS axis mapping.
12724 : *
12725 : * The number of elements of the mapping vector should be the number of axis
12726 : * of the CRS (as returned by GetAxesCount()) (although this method does not
12727 : * check that, beyond checking there are at least 2 elements, so that this
12728 : * method and setting the CRS can be done in any order).
12729 : * This is taken into account by OGRCoordinateTransformation to transform the
12730 : * order of coordinates to the order expected by the CRS before
12731 : * transformation, and back to the data order after transformation.
12732 : *
12733 : * The mapping[i] value (one based) represents the data axis number for the i(th)
12734 : * axis of the CRS. A negative value can also be used to ask for a sign
12735 : * reversal during coordinate transformation (to deal with northing vs southing,
12736 : * easting vs westing, heights vs depths).
12737 : *
12738 : * When used with OGRCoordinateTransformation,
12739 : * - the only valid values for mapping[0] (data axis number for the first axis
12740 : * of the CRS) are 1, 2, -1, -2.
12741 : * - the only valid values for mapping[1] (data axis number for the second axis
12742 : * of the CRS) are 1, 2, -1, -2.
12743 : * - the only valid values mapping[2] are 3 or -3.
12744 : * Note: this method does not validate the values of mapping[].
12745 : *
12746 : * mapping=[2,1] typically expresses the inversion of axis between the data
12747 : * axis and the CRS axis for a 2D CRS.
12748 : *
12749 : * Automatically implies SetAxisMappingStrategy(OAMS_CUSTOM)
12750 : *
12751 : * This is the same as the C function OSRSetDataAxisToSRSAxisMapping().
12752 : *
12753 : * @param mapping The new data axis to CRS axis mapping.
12754 : *
12755 : * @since GDAL 3.0
12756 : * @see OGRSpatialReference::GetDataAxisToSRSAxisMapping()
12757 : */
12758 7826 : OGRErr OGRSpatialReference::SetDataAxisToSRSAxisMapping(
12759 : const std::vector<int> &mapping)
12760 : {
12761 15652 : TAKE_OPTIONAL_LOCK();
12762 :
12763 7826 : if (mapping.size() < 2)
12764 0 : return OGRERR_FAILURE;
12765 7826 : d->m_axisMappingStrategy = OAMS_CUSTOM;
12766 7826 : d->m_axisMapping = mapping;
12767 7826 : return OGRERR_NONE;
12768 : }
12769 :
12770 : /************************************************************************/
12771 : /* OSRSetDataAxisToSRSAxisMapping() */
12772 : /************************************************************************/
12773 :
12774 : /** \brief Set a custom data axis to CRS axis mapping.
12775 : *
12776 : * Automatically implies SetAxisMappingStrategy(OAMS_CUSTOM)
12777 : *
12778 : * This is the same as the C++ method
12779 : * OGRSpatialReference::SetDataAxisToSRSAxisMapping()
12780 : *
12781 : * @since GDAL 3.1
12782 : */
12783 15 : OGRErr OSRSetDataAxisToSRSAxisMapping(OGRSpatialReferenceH hSRS,
12784 : int nMappingSize, const int *panMapping)
12785 : {
12786 15 : VALIDATE_POINTER1(hSRS, "OSRSetDataAxisToSRSAxisMapping", OGRERR_FAILURE);
12787 15 : VALIDATE_POINTER1(panMapping, "OSRSetDataAxisToSRSAxisMapping",
12788 : OGRERR_FAILURE);
12789 :
12790 15 : if (nMappingSize < 0)
12791 0 : return OGRERR_FAILURE;
12792 :
12793 30 : std::vector<int> mapping(nMappingSize);
12794 15 : if (nMappingSize)
12795 15 : memcpy(&mapping[0], panMapping, nMappingSize * sizeof(int));
12796 15 : return OGRSpatialReference::FromHandle(hSRS)->SetDataAxisToSRSAxisMapping(
12797 15 : mapping);
12798 : }
12799 :
12800 : /************************************************************************/
12801 : /* GetAreaOfUse() */
12802 : /************************************************************************/
12803 :
12804 : /** \brief Return the area of use of the CRS.
12805 : *
12806 : * This method is the same as the OSRGetAreaOfUse() function.
12807 : *
12808 : * @param pdfWestLongitudeDeg Pointer to a double to receive the western-most
12809 : * longitude, expressed in degree. Might be NULL. If the returned value is
12810 : * -1000, the bounding box is unknown.
12811 : * @param pdfSouthLatitudeDeg Pointer to a double to receive the southern-most
12812 : * latitude, expressed in degree. Might be NULL. If the returned value is -1000,
12813 : * the bounding box is unknown.
12814 : * @param pdfEastLongitudeDeg Pointer to a double to receive the eastern-most
12815 : * longitude, expressed in degree. Might be NULL. If the returned value is
12816 : * -1000, the bounding box is unknown.
12817 : * @param pdfNorthLatitudeDeg Pointer to a double to receive the northern-most
12818 : * latitude, expressed in degree. Might be NULL. If the returned value is -1000,
12819 : * the bounding box is unknown.
12820 : * @param ppszAreaName Pointer to a string to receive the name of the area of
12821 : * use. Might be NULL. Note that *ppszAreaName is short-lived and might be
12822 : * invalidated by further calls.
12823 : * @return true in case of success
12824 : * @since GDAL 3.0
12825 : */
12826 32 : bool OGRSpatialReference::GetAreaOfUse(double *pdfWestLongitudeDeg,
12827 : double *pdfSouthLatitudeDeg,
12828 : double *pdfEastLongitudeDeg,
12829 : double *pdfNorthLatitudeDeg,
12830 : const char **ppszAreaName) const
12831 : {
12832 64 : TAKE_OPTIONAL_LOCK();
12833 :
12834 32 : d->refreshProjObj();
12835 32 : if (!d->m_pj_crs)
12836 : {
12837 0 : return false;
12838 : }
12839 32 : d->demoteFromBoundCRS();
12840 32 : const char *pszAreaName = nullptr;
12841 32 : int bSuccess = proj_get_area_of_use(
12842 32 : d->getPROJContext(), d->m_pj_crs, pdfWestLongitudeDeg,
12843 : pdfSouthLatitudeDeg, pdfEastLongitudeDeg, pdfNorthLatitudeDeg,
12844 : &pszAreaName);
12845 32 : d->undoDemoteFromBoundCRS();
12846 32 : d->m_osAreaName = pszAreaName ? pszAreaName : "";
12847 32 : if (ppszAreaName)
12848 1 : *ppszAreaName = d->m_osAreaName.c_str();
12849 32 : return CPL_TO_BOOL(bSuccess);
12850 : }
12851 :
12852 : /************************************************************************/
12853 : /* GetAreaOfUse() */
12854 : /************************************************************************/
12855 :
12856 : /** \brief Return the area of use of the CRS.
12857 : *
12858 : * This function is the same as the OGRSpatialReference::GetAreaOfUse() method.
12859 : *
12860 : * @since GDAL 3.0
12861 : */
12862 1 : int OSRGetAreaOfUse(OGRSpatialReferenceH hSRS, double *pdfWestLongitudeDeg,
12863 : double *pdfSouthLatitudeDeg, double *pdfEastLongitudeDeg,
12864 : double *pdfNorthLatitudeDeg, const char **ppszAreaName)
12865 : {
12866 1 : VALIDATE_POINTER1(hSRS, "OSRGetAreaOfUse", FALSE);
12867 :
12868 1 : return OGRSpatialReference::FromHandle(hSRS)->GetAreaOfUse(
12869 : pdfWestLongitudeDeg, pdfSouthLatitudeDeg, pdfEastLongitudeDeg,
12870 1 : pdfNorthLatitudeDeg, ppszAreaName);
12871 : }
12872 :
12873 : /************************************************************************/
12874 : /* OSRGetCRSInfoListFromDatabase() */
12875 : /************************************************************************/
12876 :
12877 : /** \brief Enumerate CRS objects from the database.
12878 : *
12879 : * The returned object is an array of OSRCRSInfo* pointers, whose last
12880 : * entry is NULL. This array should be freed with OSRDestroyCRSInfoList()
12881 : *
12882 : * @param pszAuthName Authority name, used to restrict the search.
12883 : * Or NULL for all authorities.
12884 : * @param params Additional criteria. Must be set to NULL for now.
12885 : * @param pnOutResultCount Output parameter pointing to an integer to receive
12886 : * the size of the result list. Might be NULL
12887 : * @return an array of OSRCRSInfo* pointers to be freed with
12888 : * OSRDestroyCRSInfoList(), or NULL in case of error.
12889 : *
12890 : * @since GDAL 3.0
12891 : */
12892 : OSRCRSInfo **
12893 14 : OSRGetCRSInfoListFromDatabase(const char *pszAuthName,
12894 : CPL_UNUSED const OSRCRSListParameters *params,
12895 : int *pnOutResultCount)
12896 : {
12897 14 : int nResultCount = 0;
12898 14 : auto projList = proj_get_crs_info_list_from_database(
12899 : OSRGetProjTLSContext(), pszAuthName, nullptr, &nResultCount);
12900 14 : if (pnOutResultCount)
12901 14 : *pnOutResultCount = nResultCount;
12902 14 : if (!projList)
12903 : {
12904 0 : return nullptr;
12905 : }
12906 14 : auto res = new OSRCRSInfo *[nResultCount + 1];
12907 69387 : for (int i = 0; i < nResultCount; i++)
12908 : {
12909 69373 : res[i] = new OSRCRSInfo;
12910 138746 : res[i]->pszAuthName = projList[i]->auth_name
12911 69373 : ? CPLStrdup(projList[i]->auth_name)
12912 : : nullptr;
12913 69373 : res[i]->pszCode =
12914 69373 : projList[i]->code ? CPLStrdup(projList[i]->code) : nullptr;
12915 69373 : res[i]->pszName =
12916 69373 : projList[i]->name ? CPLStrdup(projList[i]->name) : nullptr;
12917 69373 : res[i]->eType = OSR_CRS_TYPE_OTHER;
12918 69373 : switch (projList[i]->type)
12919 : {
12920 6455 : case PJ_TYPE_GEOGRAPHIC_2D_CRS:
12921 6455 : res[i]->eType = OSR_CRS_TYPE_GEOGRAPHIC_2D;
12922 6455 : break;
12923 2263 : case PJ_TYPE_GEOGRAPHIC_3D_CRS:
12924 2263 : res[i]->eType = OSR_CRS_TYPE_GEOGRAPHIC_3D;
12925 2263 : break;
12926 2354 : case PJ_TYPE_GEOCENTRIC_CRS:
12927 2354 : res[i]->eType = OSR_CRS_TYPE_GEOCENTRIC;
12928 2354 : break;
12929 53042 : case PJ_TYPE_PROJECTED_CRS:
12930 53042 : res[i]->eType = OSR_CRS_TYPE_PROJECTED;
12931 53042 : break;
12932 2262 : case PJ_TYPE_VERTICAL_CRS:
12933 2262 : res[i]->eType = OSR_CRS_TYPE_VERTICAL;
12934 2262 : break;
12935 2997 : case PJ_TYPE_COMPOUND_CRS:
12936 2997 : res[i]->eType = OSR_CRS_TYPE_COMPOUND;
12937 2997 : break;
12938 0 : default:
12939 0 : break;
12940 : }
12941 69373 : res[i]->bDeprecated = projList[i]->deprecated;
12942 69373 : res[i]->bBboxValid = projList[i]->bbox_valid;
12943 69373 : res[i]->dfWestLongitudeDeg = projList[i]->west_lon_degree;
12944 69373 : res[i]->dfSouthLatitudeDeg = projList[i]->south_lat_degree;
12945 69373 : res[i]->dfEastLongitudeDeg = projList[i]->east_lon_degree;
12946 69373 : res[i]->dfNorthLatitudeDeg = projList[i]->north_lat_degree;
12947 138746 : res[i]->pszAreaName = projList[i]->area_name
12948 69373 : ? CPLStrdup(projList[i]->area_name)
12949 : : nullptr;
12950 69373 : res[i]->pszProjectionMethod =
12951 69373 : projList[i]->projection_method_name
12952 69373 : ? CPLStrdup(projList[i]->projection_method_name)
12953 : : nullptr;
12954 : #if PROJ_AT_LEAST_VERSION(8, 1, 0)
12955 : res[i]->pszCelestialBodyName =
12956 : projList[i]->celestial_body_name
12957 : ? CPLStrdup(projList[i]->celestial_body_name)
12958 : : nullptr;
12959 : #else
12960 69373 : res[i]->pszCelestialBodyName =
12961 69373 : res[i]->pszAuthName && EQUAL(res[i]->pszAuthName, "EPSG")
12962 138746 : ? CPLStrdup("Earth")
12963 : : nullptr;
12964 : #endif
12965 : }
12966 14 : res[nResultCount] = nullptr;
12967 14 : proj_crs_info_list_destroy(projList);
12968 14 : return res;
12969 : }
12970 :
12971 : /************************************************************************/
12972 : /* OSRDestroyCRSInfoList() */
12973 : /************************************************************************/
12974 :
12975 : /** \brief Destroy the result returned by
12976 : * OSRGetCRSInfoListFromDatabase().
12977 : *
12978 : * @since GDAL 3.0
12979 : */
12980 14 : void OSRDestroyCRSInfoList(OSRCRSInfo **list)
12981 : {
12982 14 : if (list)
12983 : {
12984 69387 : for (int i = 0; list[i] != nullptr; i++)
12985 : {
12986 69373 : CPLFree(list[i]->pszAuthName);
12987 69373 : CPLFree(list[i]->pszCode);
12988 69373 : CPLFree(list[i]->pszName);
12989 69373 : CPLFree(list[i]->pszAreaName);
12990 69373 : CPLFree(list[i]->pszProjectionMethod);
12991 69373 : CPLFree(list[i]->pszCelestialBodyName);
12992 69373 : delete list[i];
12993 : }
12994 14 : delete[] list;
12995 : }
12996 14 : }
12997 :
12998 : /************************************************************************/
12999 : /* OSRGetAuthorityListFromDatabase() */
13000 : /************************************************************************/
13001 :
13002 : /** \brief Return the list of CRS authorities used in the PROJ database.
13003 : *
13004 : * Such as "EPSG", "ESRI", "PROJ", "IGNF", "IAU_2015", etc.
13005 : *
13006 : * This is a direct mapping of https://proj.org/en/latest/development/reference/functions.html#c.proj_get_authorities_from_database
13007 : *
13008 : * @return nullptr in case of error, or a NULL terminated list of strings to
13009 : * free with CSLDestroy()
13010 : * @since GDAL 3.10
13011 : */
13012 2 : char **OSRGetAuthorityListFromDatabase()
13013 : {
13014 : PROJ_STRING_LIST list =
13015 2 : proj_get_authorities_from_database(OSRGetProjTLSContext());
13016 2 : if (!list)
13017 : {
13018 0 : return nullptr;
13019 : }
13020 2 : int count = 0;
13021 12 : while (list[count])
13022 10 : ++count;
13023 2 : char **res = static_cast<char **>(CPLCalloc(count + 1, sizeof(char *)));
13024 12 : for (int i = 0; i < count; ++i)
13025 10 : res[i] = CPLStrdup(list[i]);
13026 2 : proj_string_list_destroy(list);
13027 2 : return res;
13028 : }
13029 :
13030 : /************************************************************************/
13031 : /* UpdateCoordinateSystemFromGeogCRS() */
13032 : /************************************************************************/
13033 :
13034 : /*! @cond Doxygen_Suppress */
13035 : /** \brief Used by gt_wkt_srs.cpp to create projected 3D CRS. Internal use only
13036 : *
13037 : * @since GDAL 3.1
13038 : */
13039 1 : void OGRSpatialReference::UpdateCoordinateSystemFromGeogCRS()
13040 : {
13041 1 : TAKE_OPTIONAL_LOCK();
13042 :
13043 1 : d->refreshProjObj();
13044 1 : if (!d->m_pj_crs)
13045 0 : return;
13046 1 : if (d->m_pjType != PJ_TYPE_PROJECTED_CRS)
13047 0 : return;
13048 1 : if (GetAxesCount() == 3)
13049 0 : return;
13050 1 : auto ctxt = d->getPROJContext();
13051 1 : auto baseCRS = proj_crs_get_geodetic_crs(ctxt, d->m_pj_crs);
13052 1 : if (!baseCRS)
13053 0 : return;
13054 1 : auto baseCRSCS = proj_crs_get_coordinate_system(ctxt, baseCRS);
13055 1 : if (!baseCRSCS)
13056 : {
13057 0 : proj_destroy(baseCRS);
13058 0 : return;
13059 : }
13060 1 : if (proj_cs_get_axis_count(ctxt, baseCRSCS) != 3)
13061 : {
13062 0 : proj_destroy(baseCRSCS);
13063 0 : proj_destroy(baseCRS);
13064 0 : return;
13065 : }
13066 1 : auto projCS = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
13067 1 : if (!projCS || proj_cs_get_axis_count(ctxt, projCS) != 2)
13068 : {
13069 0 : proj_destroy(baseCRSCS);
13070 0 : proj_destroy(baseCRS);
13071 0 : proj_destroy(projCS);
13072 0 : return;
13073 : }
13074 :
13075 : PJ_AXIS_DESCRIPTION axis[3];
13076 4 : for (int i = 0; i < 3; i++)
13077 : {
13078 3 : const char *name = nullptr;
13079 3 : const char *abbreviation = nullptr;
13080 3 : const char *direction = nullptr;
13081 3 : double unit_conv_factor = 0;
13082 3 : const char *unit_name = nullptr;
13083 3 : proj_cs_get_axis_info(ctxt, i < 2 ? projCS : baseCRSCS, i, &name,
13084 : &abbreviation, &direction, &unit_conv_factor,
13085 : &unit_name, nullptr, nullptr);
13086 3 : axis[i].name = CPLStrdup(name);
13087 3 : axis[i].abbreviation = CPLStrdup(abbreviation);
13088 3 : axis[i].direction = CPLStrdup(direction);
13089 3 : axis[i].unit_name = CPLStrdup(unit_name);
13090 3 : axis[i].unit_conv_factor = unit_conv_factor;
13091 3 : axis[i].unit_type = PJ_UT_LINEAR;
13092 : }
13093 1 : proj_destroy(baseCRSCS);
13094 1 : proj_destroy(projCS);
13095 1 : auto cs = proj_create_cs(ctxt, PJ_CS_TYPE_CARTESIAN, 3, axis);
13096 4 : for (int i = 0; i < 3; i++)
13097 : {
13098 3 : CPLFree(axis[i].name);
13099 3 : CPLFree(axis[i].abbreviation);
13100 3 : CPLFree(axis[i].direction);
13101 3 : CPLFree(axis[i].unit_name);
13102 : }
13103 1 : if (!cs)
13104 : {
13105 0 : proj_destroy(baseCRS);
13106 0 : return;
13107 : }
13108 1 : auto conversion = proj_crs_get_coordoperation(ctxt, d->m_pj_crs);
13109 1 : auto crs = proj_create_projected_crs(ctxt, d->getProjCRSName(), baseCRS,
13110 : conversion, cs);
13111 1 : proj_destroy(baseCRS);
13112 1 : proj_destroy(conversion);
13113 1 : proj_destroy(cs);
13114 1 : d->setPjCRS(crs);
13115 : }
13116 :
13117 : /*! @endcond */
13118 :
13119 : /************************************************************************/
13120 : /* PromoteTo3D() */
13121 : /************************************************************************/
13122 :
13123 : /** \brief "Promotes" a 2D CRS to a 3D CRS one.
13124 : *
13125 : * The new axis will be ellipsoidal height, oriented upwards, and with metre
13126 : * units.
13127 : *
13128 : * @param pszName New name for the CRS. If set to NULL, the previous name will
13129 : * be used.
13130 : * @return OGRERR_NONE if no error occurred.
13131 : * @since GDAL 3.1 and PROJ 6.3
13132 : */
13133 42 : OGRErr OGRSpatialReference::PromoteTo3D(const char *pszName)
13134 : {
13135 84 : TAKE_OPTIONAL_LOCK();
13136 :
13137 42 : d->refreshProjObj();
13138 42 : if (!d->m_pj_crs)
13139 0 : return OGRERR_FAILURE;
13140 : auto newPj =
13141 42 : proj_crs_promote_to_3D(d->getPROJContext(), pszName, d->m_pj_crs);
13142 42 : if (!newPj)
13143 0 : return OGRERR_FAILURE;
13144 42 : d->setPjCRS(newPj);
13145 42 : return OGRERR_NONE;
13146 : }
13147 :
13148 : /************************************************************************/
13149 : /* OSRPromoteTo3D() */
13150 : /************************************************************************/
13151 :
13152 : /** \brief "Promotes" a 2D CRS to a 3D CRS one.
13153 : *
13154 : * See OGRSpatialReference::PromoteTo3D()
13155 : *
13156 : * @since GDAL 3.1 and PROJ 6.3
13157 : */
13158 3 : OGRErr OSRPromoteTo3D(OGRSpatialReferenceH hSRS, const char *pszName)
13159 : {
13160 3 : VALIDATE_POINTER1(hSRS, "OSRPromoteTo3D", OGRERR_FAILURE);
13161 :
13162 3 : return OGRSpatialReference::FromHandle(hSRS)->PromoteTo3D(pszName);
13163 : }
13164 :
13165 : /************************************************************************/
13166 : /* DemoteTo2D() */
13167 : /************************************************************************/
13168 :
13169 : /** \brief "Demote" a 3D CRS to a 2D CRS one.
13170 : *
13171 : * @param pszName New name for the CRS. If set to NULL, the previous name will
13172 : * be used.
13173 : * @return OGRERR_NONE if no error occurred.
13174 : * @since GDAL 3.2 and PROJ 6.3
13175 : */
13176 45 : OGRErr OGRSpatialReference::DemoteTo2D(const char *pszName)
13177 : {
13178 90 : TAKE_OPTIONAL_LOCK();
13179 :
13180 45 : d->refreshProjObj();
13181 45 : if (!d->m_pj_crs)
13182 0 : return OGRERR_FAILURE;
13183 : auto newPj =
13184 45 : proj_crs_demote_to_2D(d->getPROJContext(), pszName, d->m_pj_crs);
13185 45 : if (!newPj)
13186 0 : return OGRERR_FAILURE;
13187 45 : d->setPjCRS(newPj);
13188 45 : return OGRERR_NONE;
13189 : }
13190 :
13191 : /************************************************************************/
13192 : /* OSRDemoteTo2D() */
13193 : /************************************************************************/
13194 :
13195 : /** \brief "Demote" a 3D CRS to a 2D CRS one.
13196 : *
13197 : * See OGRSpatialReference::DemoteTo2D()
13198 : *
13199 : * @since GDAL 3.2 and PROJ 6.3
13200 : */
13201 1 : OGRErr OSRDemoteTo2D(OGRSpatialReferenceH hSRS, const char *pszName)
13202 : {
13203 1 : VALIDATE_POINTER1(hSRS, "OSRDemoteTo2D", OGRERR_FAILURE);
13204 :
13205 1 : return OGRSpatialReference::FromHandle(hSRS)->DemoteTo2D(pszName);
13206 : }
13207 :
13208 : /************************************************************************/
13209 : /* GetEPSGGeogCS() */
13210 : /************************************************************************/
13211 :
13212 : /** Try to establish what the EPSG code for this coordinate systems
13213 : * GEOGCS might be. Returns -1 if no reasonable guess can be made.
13214 : *
13215 : * @return EPSG code
13216 : */
13217 :
13218 342 : int OGRSpatialReference::GetEPSGGeogCS() const
13219 :
13220 : {
13221 684 : TAKE_OPTIONAL_LOCK();
13222 :
13223 : /* -------------------------------------------------------------------- */
13224 : /* Check axis order. */
13225 : /* -------------------------------------------------------------------- */
13226 684 : auto poGeogCRS = std::unique_ptr<OGRSpatialReference>(CloneGeogCS());
13227 342 : if (!poGeogCRS)
13228 0 : return -1;
13229 :
13230 342 : bool ret = false;
13231 342 : poGeogCRS->d->demoteFromBoundCRS();
13232 342 : auto cs = proj_crs_get_coordinate_system(d->getPROJContext(),
13233 342 : poGeogCRS->d->m_pj_crs);
13234 342 : poGeogCRS->d->undoDemoteFromBoundCRS();
13235 342 : if (cs)
13236 : {
13237 342 : const char *pszDirection = nullptr;
13238 342 : if (proj_cs_get_axis_info(d->getPROJContext(), cs, 0, nullptr, nullptr,
13239 : &pszDirection, nullptr, nullptr, nullptr,
13240 342 : nullptr))
13241 : {
13242 342 : if (EQUAL(pszDirection, "north"))
13243 : {
13244 143 : ret = true;
13245 : }
13246 : }
13247 :
13248 342 : proj_destroy(cs);
13249 : }
13250 342 : if (!ret)
13251 199 : return -1;
13252 :
13253 : /* -------------------------------------------------------------------- */
13254 : /* Do we already have it? */
13255 : /* -------------------------------------------------------------------- */
13256 143 : const char *pszAuthName = GetAuthorityName("GEOGCS");
13257 143 : if (pszAuthName != nullptr && EQUAL(pszAuthName, "epsg"))
13258 66 : return atoi(GetAuthorityCode("GEOGCS"));
13259 :
13260 : /* -------------------------------------------------------------------- */
13261 : /* Get the datum and geogcs names. */
13262 : /* -------------------------------------------------------------------- */
13263 :
13264 77 : const char *pszGEOGCS = GetAttrValue("GEOGCS");
13265 77 : const char *pszDatum = GetAttrValue("DATUM");
13266 :
13267 : // We can only operate on coordinate systems with a geogcs.
13268 154 : OGRSpatialReference oSRSTmp;
13269 77 : if (pszGEOGCS == nullptr || pszDatum == nullptr)
13270 : {
13271 : // Calling GetAttrValue("GEOGCS") will fail on a CRS that can't be
13272 : // export to WKT1, so try to extract the geographic CRS through PROJ
13273 : // API with CopyGeogCSFrom() and get the nodes' values from it.
13274 1 : oSRSTmp.CopyGeogCSFrom(this);
13275 1 : pszGEOGCS = oSRSTmp.GetAttrValue("GEOGCS");
13276 1 : pszDatum = oSRSTmp.GetAttrValue("DATUM");
13277 1 : if (pszGEOGCS == nullptr || pszDatum == nullptr)
13278 : {
13279 0 : return -1;
13280 : }
13281 : }
13282 :
13283 : // Lookup geographic CRS name
13284 77 : const PJ_TYPE type = PJ_TYPE_GEOGRAPHIC_2D_CRS;
13285 77 : PJ_OBJ_LIST *list = proj_create_from_name(
13286 : d->getPROJContext(), nullptr, pszGEOGCS, &type, 1, false, 1, nullptr);
13287 77 : if (list)
13288 : {
13289 77 : const auto listSize = proj_list_get_count(list);
13290 77 : if (listSize == 1)
13291 : {
13292 49 : auto crs = proj_list_get(d->getPROJContext(), list, 0);
13293 49 : if (crs)
13294 : {
13295 49 : pszAuthName = proj_get_id_auth_name(crs, 0);
13296 49 : const char *pszCode = proj_get_id_code(crs, 0);
13297 49 : if (pszAuthName && pszCode && EQUAL(pszAuthName, "EPSG"))
13298 : {
13299 47 : const int nCode = atoi(pszCode);
13300 47 : proj_destroy(crs);
13301 47 : proj_list_destroy(list);
13302 47 : return nCode;
13303 : }
13304 2 : proj_destroy(crs);
13305 : }
13306 : }
13307 30 : proj_list_destroy(list);
13308 : }
13309 :
13310 : /* -------------------------------------------------------------------- */
13311 : /* Is this a "well known" geographic coordinate system? */
13312 : /* -------------------------------------------------------------------- */
13313 90 : const bool bWGS = strstr(pszGEOGCS, "WGS") != nullptr ||
13314 30 : strstr(pszDatum, "WGS") ||
13315 30 : strstr(pszGEOGCS, "World Geodetic System") ||
13316 30 : strstr(pszGEOGCS, "World_Geodetic_System") ||
13317 90 : strstr(pszDatum, "World Geodetic System") ||
13318 30 : strstr(pszDatum, "World_Geodetic_System");
13319 :
13320 90 : const bool bNAD = strstr(pszGEOGCS, "NAD") != nullptr ||
13321 30 : strstr(pszDatum, "NAD") ||
13322 30 : strstr(pszGEOGCS, "North American") ||
13323 30 : strstr(pszGEOGCS, "North_American") ||
13324 90 : strstr(pszDatum, "North American") ||
13325 30 : strstr(pszDatum, "North_American");
13326 :
13327 30 : if (bWGS && (strstr(pszGEOGCS, "84") || strstr(pszDatum, "84")))
13328 0 : return 4326;
13329 :
13330 30 : if (bWGS && (strstr(pszGEOGCS, "72") || strstr(pszDatum, "72")))
13331 0 : return 4322;
13332 :
13333 : // This is questionable as there are several 'flavors' of NAD83 that
13334 : // are not the same as 4269
13335 30 : if (bNAD && (strstr(pszGEOGCS, "83") || strstr(pszDatum, "83")))
13336 0 : return 4269;
13337 :
13338 30 : if (bNAD && (strstr(pszGEOGCS, "27") || strstr(pszDatum, "27")))
13339 0 : return 4267;
13340 :
13341 : /* -------------------------------------------------------------------- */
13342 : /* If we know the datum, associate the most likely GCS with */
13343 : /* it. */
13344 : /* -------------------------------------------------------------------- */
13345 30 : const OGRSpatialReference &oActiveObj = oSRSTmp.IsEmpty() ? *this : oSRSTmp;
13346 30 : pszAuthName = oActiveObj.GetAuthorityName("GEOGCS|DATUM");
13347 30 : if (pszAuthName != nullptr && EQUAL(pszAuthName, "epsg") &&
13348 0 : GetPrimeMeridian() == 0.0)
13349 : {
13350 0 : const int nDatum = atoi(oActiveObj.GetAuthorityCode("GEOGCS|DATUM"));
13351 :
13352 0 : if (nDatum >= 6000 && nDatum <= 6999)
13353 0 : return nDatum - 2000;
13354 : }
13355 :
13356 30 : return -1;
13357 : }
13358 :
13359 : /************************************************************************/
13360 : /* SetCoordinateEpoch() */
13361 : /************************************************************************/
13362 :
13363 : /** Set the coordinate epoch, as decimal year.
13364 : *
13365 : * In a dynamic CRS, coordinates of a point on the surface of the Earth may
13366 : * change with time. To be unambiguous the coordinates must always be qualified
13367 : * with the epoch at which they are valid. The coordinate epoch is not
13368 : * necessarily the epoch at which the observation was collected.
13369 : *
13370 : * Pedantically the coordinate epoch of an observation belongs to the
13371 : * observation, and not to the CRS, however it is often more practical to
13372 : * bind it to the CRS. The coordinate epoch should be specified for dynamic
13373 : * CRS (see IsDynamic())
13374 : *
13375 : * This method is the same as the OSRSetCoordinateEpoch() function.
13376 : *
13377 : * @param dfCoordinateEpoch Coordinate epoch as decimal year (e.g. 2021.3)
13378 : * @since OGR 3.4
13379 : */
13380 :
13381 825 : void OGRSpatialReference::SetCoordinateEpoch(double dfCoordinateEpoch)
13382 : {
13383 825 : d->m_coordinateEpoch = dfCoordinateEpoch;
13384 825 : }
13385 :
13386 : /************************************************************************/
13387 : /* OSRSetCoordinateEpoch() */
13388 : /************************************************************************/
13389 :
13390 : /** \brief Set the coordinate epoch, as decimal year.
13391 : *
13392 : * See OGRSpatialReference::SetCoordinateEpoch()
13393 : *
13394 : * @since OGR 3.4
13395 : */
13396 31 : void OSRSetCoordinateEpoch(OGRSpatialReferenceH hSRS, double dfCoordinateEpoch)
13397 : {
13398 31 : VALIDATE_POINTER0(hSRS, "OSRSetCoordinateEpoch");
13399 :
13400 31 : return OGRSpatialReference::FromHandle(hSRS)->SetCoordinateEpoch(
13401 31 : dfCoordinateEpoch);
13402 : }
13403 :
13404 : /************************************************************************/
13405 : /* GetCoordinateEpoch() */
13406 : /************************************************************************/
13407 :
13408 : /** Return the coordinate epoch, as decimal year.
13409 : *
13410 : * In a dynamic CRS, coordinates of a point on the surface of the Earth may
13411 : * change with time. To be unambiguous the coordinates must always be qualified
13412 : * with the epoch at which they are valid. The coordinate epoch is not
13413 : * necessarily the epoch at which the observation was collected.
13414 : *
13415 : * Pedantically the coordinate epoch of an observation belongs to the
13416 : * observation, and not to the CRS, however it is often more practical to
13417 : * bind it to the CRS. The coordinate epoch should be specified for dynamic
13418 : * CRS (see IsDynamic())
13419 : *
13420 : * This method is the same as the OSRGetCoordinateEpoch() function.
13421 : *
13422 : * @return coordinateEpoch Coordinate epoch as decimal year (e.g. 2021.3), or 0
13423 : * if not set, or relevant.
13424 : * @since OGR 3.4
13425 : */
13426 :
13427 11758 : double OGRSpatialReference::GetCoordinateEpoch() const
13428 : {
13429 11758 : return d->m_coordinateEpoch;
13430 : }
13431 :
13432 : /************************************************************************/
13433 : /* OSRGetCoordinateEpoch() */
13434 : /************************************************************************/
13435 :
13436 : /** \brief Get the coordinate epoch, as decimal year.
13437 : *
13438 : * See OGRSpatialReference::GetCoordinateEpoch()
13439 : *
13440 : * @since OGR 3.4
13441 : */
13442 651 : double OSRGetCoordinateEpoch(OGRSpatialReferenceH hSRS)
13443 : {
13444 651 : VALIDATE_POINTER1(hSRS, "OSRGetCoordinateEpoch", 0);
13445 :
13446 651 : return OGRSpatialReference::FromHandle(hSRS)->GetCoordinateEpoch();
13447 : }
|