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 final : public OGR_SRSNode::Listener
67 : {
68 : OGRSpatialReference::Private *m_poObj = nullptr;
69 :
70 282531 : explicit Listener(OGRSpatialReference::Private *poObj) : m_poObj(poObj)
71 : {
72 282531 : }
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(bool bForceWKT2);
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 1305410 : PJ_CONTEXT *getPROJContext()
159 : {
160 1305410 : 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 18481800 : explicit OptionalLockGuard(Private *p) : m_private(*p)
180 : {
181 18481800 : if (m_private.m_bIsThreadSafe)
182 3798 : m_private.m_mutex.lock();
183 18481800 : }
184 :
185 18481800 : ~OptionalLockGuard()
186 18481800 : {
187 18481800 : if (m_private.m_bIsThreadSafe)
188 3798 : m_private.m_mutex.unlock();
189 18481800 : }
190 : };
191 :
192 18481800 : inline OptionalLockGuard GetOptionalLockGuard()
193 : {
194 18481800 : return OptionalLockGuard(this);
195 : }
196 : };
197 :
198 2448440 : void OGRSpatialReference::Private::Listener::notifyChange(OGR_SRSNode *)
199 : {
200 2448440 : m_poObj->nodesChanged();
201 2448440 : }
202 :
203 : #define TAKE_OPTIONAL_LOCK() \
204 : auto lock = d->GetOptionalLockGuard(); \
205 : CPL_IGNORE_RET_VAL(lock)
206 :
207 282531 : static OSRAxisMappingStrategy GetDefaultAxisMappingStrategy()
208 : {
209 : const char *pszDefaultAMS =
210 282531 : CPLGetConfigOption("OSR_DEFAULT_AXIS_MAPPING_STRATEGY", nullptr);
211 282531 : 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 282530 : return OAMS_AUTHORITY_COMPLIANT;
225 : }
226 :
227 282531 : OGRSpatialReference::Private::Private(OGRSpatialReference *poSelf)
228 282531 : : m_poSelf(poSelf), m_poListener(std::make_shared<Listener>(this))
229 : {
230 : // Get the default value for m_axisMappingStrategy from the
231 : // OSR_DEFAULT_AXIS_MAPPING_STRATEGY configuration option, if set.
232 282531 : m_axisMappingStrategy = GetDefaultAxisMappingStrategy();
233 282531 : }
234 :
235 1126220 : OGRSpatialReference::Private::~Private()
236 : {
237 : // In case we destroy the object not in the thread that created it,
238 : // we need to reassign the PROJ context. Having the context bundled inside
239 : // PJ* deeply sucks...
240 281555 : PJ_CONTEXT *pj_context_to_destroy = nullptr;
241 : PJ_CONTEXT *ctxt;
242 281555 : if (GDALThreadLocalDatasetCacheIsInDestruction())
243 : {
244 185 : pj_context_to_destroy = proj_context_create();
245 185 : ctxt = pj_context_to_destroy;
246 : }
247 : else
248 : {
249 281370 : ctxt = getPROJContext();
250 : }
251 :
252 281555 : proj_assign_context(m_pj_crs, ctxt);
253 281555 : proj_destroy(m_pj_crs);
254 :
255 281555 : proj_assign_context(m_pj_geod_base_crs_temp, ctxt);
256 281555 : proj_destroy(m_pj_geod_base_crs_temp);
257 :
258 281555 : proj_assign_context(m_pj_proj_crs_cs_temp, ctxt);
259 281555 : proj_destroy(m_pj_proj_crs_cs_temp);
260 :
261 281555 : proj_assign_context(m_pj_bound_crs_target, ctxt);
262 281555 : proj_destroy(m_pj_bound_crs_target);
263 :
264 281555 : proj_assign_context(m_pj_bound_crs_co, ctxt);
265 281555 : proj_destroy(m_pj_bound_crs_co);
266 :
267 281555 : proj_assign_context(m_pj_crs_backup, ctxt);
268 281555 : proj_destroy(m_pj_crs_backup);
269 :
270 281555 : delete m_poRootBackup;
271 281555 : delete m_poRoot;
272 281555 : proj_context_destroy(pj_context_to_destroy);
273 281555 : }
274 :
275 136934 : void OGRSpatialReference::Private::clear()
276 : {
277 136934 : proj_assign_context(m_pj_crs, getPROJContext());
278 136934 : proj_destroy(m_pj_crs);
279 136934 : m_pj_crs = nullptr;
280 :
281 136934 : delete m_poRoot;
282 136934 : m_poRoot = nullptr;
283 136934 : m_bNodesChanged = false;
284 :
285 136934 : m_wktImportWarnings.clear();
286 136934 : m_wktImportErrors.clear();
287 :
288 136934 : m_pj_crs_modified_during_demote = false;
289 136934 : m_pjType = PJ_TYPE_UNKNOWN;
290 136934 : m_osPrimeMeridianName.clear();
291 136934 : m_osAngularUnits.clear();
292 136934 : m_osLinearUnits.clear();
293 :
294 136934 : bNormInfoSet = FALSE;
295 136934 : dfFromGreenwich = 1.0;
296 136934 : dfToMeter = 1.0;
297 136934 : dfToDegrees = 1.0;
298 136934 : m_dfAngularUnitToRadian = 0.0;
299 :
300 136934 : m_bMorphToESRI = false;
301 136934 : m_bHasCenterLong = false;
302 :
303 136934 : m_coordinateEpoch = 0.0;
304 136934 : }
305 :
306 30491 : void OGRSpatialReference::Private::setRoot(OGR_SRSNode *poRoot)
307 : {
308 30491 : m_poRoot = poRoot;
309 30491 : if (m_poRoot)
310 : {
311 30491 : m_poRoot->RegisterListener(m_poListener);
312 : }
313 30491 : nodesChanged();
314 30491 : }
315 :
316 201288 : void OGRSpatialReference::Private::setPjCRS(PJ *pj_crsIn,
317 : bool doRefreshAxisMapping)
318 : {
319 201288 : auto ctxt = getPROJContext();
320 :
321 : #if PROJ_AT_LEAST_VERSION(9, 2, 0)
322 : if (proj_get_type(pj_crsIn) == PJ_TYPE_COORDINATE_METADATA)
323 : {
324 : const double dfEpoch =
325 : proj_coordinate_metadata_get_epoch(ctxt, pj_crsIn);
326 : if (!std::isnan(dfEpoch))
327 : {
328 : m_poSelf->SetCoordinateEpoch(dfEpoch);
329 : }
330 : auto crs = proj_get_source_crs(ctxt, pj_crsIn);
331 : proj_destroy(pj_crsIn);
332 : pj_crsIn = crs;
333 : }
334 : #endif
335 :
336 201288 : proj_assign_context(m_pj_crs, ctxt);
337 201288 : proj_destroy(m_pj_crs);
338 201288 : m_pj_crs = pj_crsIn;
339 201288 : if (m_pj_crs)
340 : {
341 201237 : m_pjType = proj_get_type(m_pj_crs);
342 : }
343 201288 : if (m_pj_crs_backup)
344 : {
345 21 : m_pj_crs_modified_during_demote = true;
346 : }
347 201288 : invalidateNodes();
348 201288 : if (doRefreshAxisMapping)
349 : {
350 201268 : refreshAxisMapping();
351 : }
352 201288 : }
353 :
354 812170 : void OGRSpatialReference::Private::refreshProjObj()
355 : {
356 812170 : if (m_bNodesChanged && m_poRoot)
357 : {
358 9003 : char *pszWKT = nullptr;
359 9003 : m_poRoot->exportToWkt(&pszWKT);
360 9003 : auto poRootBackup = m_poRoot;
361 9003 : m_poRoot = nullptr;
362 9003 : const double dfCoordinateEpochBackup = m_coordinateEpoch;
363 9003 : clear();
364 9003 : m_coordinateEpoch = dfCoordinateEpochBackup;
365 9003 : m_bHasCenterLong = strstr(pszWKT, "CENTER_LONG") != nullptr;
366 :
367 9003 : const char *const options[] = {
368 : "STRICT=NO",
369 : #if PROJ_AT_LEAST_VERSION(9, 1, 0)
370 : "UNSET_IDENTIFIERS_IF_INCOMPATIBLE_DEF=NO",
371 : #endif
372 : nullptr};
373 9003 : PROJ_STRING_LIST warnings = nullptr;
374 9003 : PROJ_STRING_LIST errors = nullptr;
375 9003 : setPjCRS(proj_create_from_wkt(getPROJContext(), pszWKT, options,
376 : &warnings, &errors));
377 17623 : for (auto iter = warnings; iter && *iter; ++iter)
378 : {
379 8620 : m_wktImportWarnings.push_back(*iter);
380 : }
381 9219 : for (auto iter = errors; iter && *iter; ++iter)
382 : {
383 216 : m_wktImportErrors.push_back(*iter);
384 : }
385 9003 : proj_string_list_destroy(warnings);
386 9003 : proj_string_list_destroy(errors);
387 :
388 9003 : CPLFree(pszWKT);
389 :
390 9003 : m_poRoot = poRootBackup;
391 9003 : m_bNodesChanged = false;
392 : }
393 812170 : }
394 :
395 32628 : void OGRSpatialReference::Private::refreshRootFromProjObj(bool bForceWKT2)
396 : {
397 32628 : CPLAssert(m_poRoot == nullptr);
398 :
399 32628 : if (m_pj_crs)
400 : {
401 60892 : CPLStringList aosOptions;
402 30446 : if (!m_bMorphToESRI)
403 : {
404 30442 : aosOptions.SetNameValue("OUTPUT_AXIS", "YES");
405 30442 : aosOptions.SetNameValue("MULTILINE", "NO");
406 : }
407 30446 : aosOptions.SetNameValue("STRICT", "NO");
408 :
409 30446 : const char *pszWKT = nullptr;
410 30446 : if (!bForceWKT2)
411 : {
412 30429 : CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
413 30429 : pszWKT = proj_as_wkt(getPROJContext(), m_pj_crs,
414 30429 : m_bMorphToESRI ? PJ_WKT1_ESRI : PJ_WKT1_GDAL,
415 30429 : aosOptions.List());
416 30429 : m_bNodesWKT2 = false;
417 : }
418 30446 : if (!m_bMorphToESRI && pszWKT == nullptr)
419 : {
420 113 : pszWKT = proj_as_wkt(getPROJContext(), m_pj_crs, PJ_WKT2_2018,
421 113 : aosOptions.List());
422 113 : m_bNodesWKT2 = true;
423 : }
424 30446 : if (pszWKT)
425 : {
426 30446 : auto root = new OGR_SRSNode();
427 30446 : setRoot(root);
428 30446 : root->importFromWkt(&pszWKT);
429 30446 : m_bNodesChanged = false;
430 : }
431 : }
432 32628 : }
433 :
434 235241 : static bool isNorthEastAxisOrder(PJ_CONTEXT *ctx, PJ *cs)
435 : {
436 235241 : const char *pszName1 = nullptr;
437 235241 : const char *pszDirection1 = nullptr;
438 235241 : proj_cs_get_axis_info(ctx, cs, 0, &pszName1, nullptr, &pszDirection1,
439 : nullptr, nullptr, nullptr, nullptr);
440 235241 : const char *pszName2 = nullptr;
441 235241 : const char *pszDirection2 = nullptr;
442 235241 : proj_cs_get_axis_info(ctx, cs, 1, &pszName2, nullptr, &pszDirection2,
443 : nullptr, nullptr, nullptr, nullptr);
444 235241 : if (pszDirection1 && EQUAL(pszDirection1, "north") && pszDirection2 &&
445 104219 : EQUAL(pszDirection2, "east"))
446 : {
447 102567 : return true;
448 : }
449 132674 : if (pszDirection1 && pszDirection2 &&
450 132674 : ((EQUAL(pszDirection1, "north") && EQUAL(pszDirection2, "north")) ||
451 131039 : (EQUAL(pszDirection1, "south") && EQUAL(pszDirection2, "south"))) &&
452 3407 : pszName1 && STARTS_WITH_CI(pszName1, "northing") && pszName2 &&
453 1393 : STARTS_WITH_CI(pszName2, "easting"))
454 : {
455 1393 : return true;
456 : }
457 131281 : return false;
458 : }
459 :
460 296689 : void OGRSpatialReference::Private::refreshAxisMapping()
461 : {
462 296689 : if (!m_pj_crs || m_axisMappingStrategy == OAMS_CUSTOM)
463 62025 : return;
464 :
465 234664 : bool doUndoDemote = false;
466 234664 : if (m_pj_crs_backup == nullptr)
467 : {
468 234643 : doUndoDemote = true;
469 234643 : demoteFromBoundCRS();
470 : }
471 234664 : const auto ctxt = getPROJContext();
472 234664 : PJ *horizCRS = nullptr;
473 234664 : int axisCount = 0;
474 234664 : if (m_pjType == PJ_TYPE_VERTICAL_CRS)
475 : {
476 311 : axisCount = 1;
477 : }
478 234353 : else if (m_pjType == PJ_TYPE_COMPOUND_CRS)
479 : {
480 1268 : horizCRS = proj_crs_get_sub_crs(ctxt, m_pj_crs, 0);
481 1268 : if (horizCRS && proj_get_type(horizCRS) == PJ_TYPE_BOUND_CRS)
482 : {
483 222 : auto baseCRS = proj_get_source_crs(ctxt, horizCRS);
484 222 : if (baseCRS)
485 : {
486 222 : proj_destroy(horizCRS);
487 222 : horizCRS = baseCRS;
488 : }
489 : }
490 :
491 1268 : auto vertCRS = proj_crs_get_sub_crs(ctxt, m_pj_crs, 1);
492 1268 : if (vertCRS)
493 : {
494 1265 : if (proj_get_type(vertCRS) == PJ_TYPE_BOUND_CRS)
495 : {
496 398 : auto baseCRS = proj_get_source_crs(ctxt, vertCRS);
497 398 : if (baseCRS)
498 : {
499 398 : proj_destroy(vertCRS);
500 398 : vertCRS = baseCRS;
501 : }
502 : }
503 :
504 1265 : auto cs = proj_crs_get_coordinate_system(ctxt, vertCRS);
505 1265 : if (cs)
506 : {
507 1265 : axisCount += proj_cs_get_axis_count(ctxt, cs);
508 1265 : proj_destroy(cs);
509 : }
510 1265 : proj_destroy(vertCRS);
511 : }
512 : }
513 : else
514 : {
515 233085 : horizCRS = m_pj_crs;
516 : }
517 :
518 234664 : bool bSwitchForGisFriendlyOrder = false;
519 234664 : if (horizCRS)
520 : {
521 234350 : auto cs = proj_crs_get_coordinate_system(ctxt, horizCRS);
522 234350 : if (cs)
523 : {
524 234350 : int nHorizCSAxisCount = proj_cs_get_axis_count(ctxt, cs);
525 234350 : axisCount += nHorizCSAxisCount;
526 234350 : if (nHorizCSAxisCount >= 2)
527 : {
528 234340 : bSwitchForGisFriendlyOrder = isNorthEastAxisOrder(ctxt, cs);
529 : }
530 234350 : proj_destroy(cs);
531 : }
532 : }
533 234664 : if (horizCRS != m_pj_crs)
534 : {
535 1579 : proj_destroy(horizCRS);
536 : }
537 234664 : if (doUndoDemote)
538 : {
539 234643 : undoDemoteFromBoundCRS();
540 : }
541 :
542 234664 : m_axisMapping.resize(axisCount);
543 234664 : if (m_axisMappingStrategy == OAMS_AUTHORITY_COMPLIANT ||
544 70218 : !bSwitchForGisFriendlyOrder)
545 : {
546 608412 : for (int i = 0; i < axisCount; i++)
547 : {
548 406121 : m_axisMapping[i] = i + 1;
549 202291 : }
550 : }
551 : else
552 : {
553 32373 : m_axisMapping[0] = 2;
554 32373 : m_axisMapping[1] = 1;
555 32373 : if (axisCount == 3)
556 : {
557 511 : m_axisMapping[2] = 3;
558 : }
559 : }
560 : }
561 :
562 2478930 : void OGRSpatialReference::Private::nodesChanged()
563 : {
564 2478930 : m_bNodesChanged = true;
565 2478930 : }
566 :
567 201562 : void OGRSpatialReference::Private::invalidateNodes()
568 : {
569 201562 : delete m_poRoot;
570 201562 : m_poRoot = nullptr;
571 201562 : m_bNodesChanged = false;
572 201562 : }
573 :
574 257 : void OGRSpatialReference::Private::setMorphToESRI(bool b)
575 : {
576 257 : invalidateNodes();
577 257 : m_bMorphToESRI = b;
578 257 : }
579 :
580 687578 : void OGRSpatialReference::Private::demoteFromBoundCRS()
581 : {
582 687578 : CPLAssert(m_pj_bound_crs_target == nullptr);
583 687578 : CPLAssert(m_pj_bound_crs_co == nullptr);
584 687578 : CPLAssert(m_poRootBackup == nullptr);
585 687578 : CPLAssert(m_pj_crs_backup == nullptr);
586 :
587 687578 : m_pj_crs_modified_during_demote = false;
588 :
589 687578 : if (m_pjType == PJ_TYPE_BOUND_CRS)
590 : {
591 2753 : auto baseCRS = proj_get_source_crs(getPROJContext(), m_pj_crs);
592 2753 : m_pj_bound_crs_target = proj_get_target_crs(getPROJContext(), m_pj_crs);
593 2753 : m_pj_bound_crs_co =
594 2753 : proj_crs_get_coordoperation(getPROJContext(), m_pj_crs);
595 :
596 2753 : m_poRootBackup = m_poRoot;
597 2753 : m_poRoot = nullptr;
598 2753 : m_pj_crs_backup = m_pj_crs;
599 2753 : m_pj_crs = baseCRS;
600 2753 : m_pjType = proj_get_type(m_pj_crs);
601 : }
602 687578 : }
603 :
604 687578 : void OGRSpatialReference::Private::undoDemoteFromBoundCRS()
605 : {
606 687578 : if (m_pj_bound_crs_target)
607 : {
608 2753 : CPLAssert(m_poRoot == nullptr);
609 2753 : CPLAssert(m_pj_crs);
610 2753 : if (!m_pj_crs_modified_during_demote)
611 : {
612 2733 : proj_destroy(m_pj_crs);
613 2733 : m_pj_crs = m_pj_crs_backup;
614 2733 : m_pjType = proj_get_type(m_pj_crs);
615 2733 : m_poRoot = m_poRootBackup;
616 : }
617 : else
618 : {
619 20 : delete m_poRootBackup;
620 20 : m_poRootBackup = nullptr;
621 20 : proj_destroy(m_pj_crs_backup);
622 20 : m_pj_crs_backup = nullptr;
623 20 : setPjCRS(proj_crs_create_bound_crs(getPROJContext(), m_pj_crs,
624 20 : m_pj_bound_crs_target,
625 20 : m_pj_bound_crs_co),
626 : false);
627 : }
628 : }
629 :
630 687578 : m_poRootBackup = nullptr;
631 687578 : m_pj_crs_backup = nullptr;
632 687578 : proj_destroy(m_pj_bound_crs_target);
633 687578 : m_pj_bound_crs_target = nullptr;
634 687578 : proj_destroy(m_pj_bound_crs_co);
635 687578 : m_pj_bound_crs_co = nullptr;
636 687578 : m_pj_crs_modified_during_demote = false;
637 687578 : }
638 :
639 174437 : const char *OGRSpatialReference::Private::nullifyTargetKeyIfPossible(
640 : const char *pszTargetKey)
641 : {
642 174437 : if (pszTargetKey)
643 : {
644 62139 : demoteFromBoundCRS();
645 62139 : if ((m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
646 32751 : m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS) &&
647 29466 : EQUAL(pszTargetKey, "GEOGCS"))
648 : {
649 7137 : pszTargetKey = nullptr;
650 : }
651 55002 : else if (m_pjType == PJ_TYPE_GEOCENTRIC_CRS &&
652 20 : EQUAL(pszTargetKey, "GEOCCS"))
653 : {
654 0 : pszTargetKey = nullptr;
655 : }
656 55002 : else if (m_pjType == PJ_TYPE_PROJECTED_CRS &&
657 31361 : EQUAL(pszTargetKey, "PROJCS"))
658 : {
659 4407 : pszTargetKey = nullptr;
660 : }
661 50595 : else if (m_pjType == PJ_TYPE_VERTICAL_CRS &&
662 4 : EQUAL(pszTargetKey, "VERT_CS"))
663 : {
664 2 : pszTargetKey = nullptr;
665 : }
666 62139 : undoDemoteFromBoundCRS();
667 : }
668 174437 : return pszTargetKey;
669 : }
670 :
671 10021 : PJ *OGRSpatialReference::Private::getGeodBaseCRS()
672 : {
673 10021 : if (m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
674 9968 : m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS)
675 : {
676 53 : return m_pj_crs;
677 : }
678 :
679 9968 : auto ctxt = getPROJContext();
680 9968 : if (m_pjType == PJ_TYPE_PROJECTED_CRS)
681 : {
682 4606 : proj_assign_context(m_pj_geod_base_crs_temp, ctxt);
683 4606 : proj_destroy(m_pj_geod_base_crs_temp);
684 4606 : m_pj_geod_base_crs_temp = proj_crs_get_geodetic_crs(ctxt, m_pj_crs);
685 4606 : return m_pj_geod_base_crs_temp;
686 : }
687 :
688 5362 : proj_assign_context(m_pj_geod_base_crs_temp, ctxt);
689 5362 : proj_destroy(m_pj_geod_base_crs_temp);
690 5362 : auto cs = proj_create_ellipsoidal_2D_cs(ctxt, PJ_ELLPS2D_LATITUDE_LONGITUDE,
691 : nullptr, 0);
692 5362 : m_pj_geod_base_crs_temp = proj_create_geographic_crs(
693 : ctxt, "WGS 84", "World Geodetic System 1984", "WGS 84",
694 : SRS_WGS84_SEMIMAJOR, SRS_WGS84_INVFLATTENING, SRS_PM_GREENWICH, 0.0,
695 : SRS_UA_DEGREE, CPLAtof(SRS_UA_DEGREE_CONV), cs);
696 5362 : proj_destroy(cs);
697 :
698 5362 : return m_pj_geod_base_crs_temp;
699 : }
700 :
701 5564 : PJ *OGRSpatialReference::Private::getProjCRSCoordSys()
702 : {
703 5564 : auto ctxt = getPROJContext();
704 5564 : if (m_pjType == PJ_TYPE_PROJECTED_CRS)
705 : {
706 4592 : proj_assign_context(m_pj_proj_crs_cs_temp, ctxt);
707 4592 : proj_destroy(m_pj_proj_crs_cs_temp);
708 4592 : m_pj_proj_crs_cs_temp =
709 4592 : proj_crs_get_coordinate_system(getPROJContext(), m_pj_crs);
710 4592 : return m_pj_proj_crs_cs_temp;
711 : }
712 :
713 972 : proj_assign_context(m_pj_proj_crs_cs_temp, ctxt);
714 972 : proj_destroy(m_pj_proj_crs_cs_temp);
715 972 : m_pj_proj_crs_cs_temp = proj_create_cartesian_2D_cs(
716 : ctxt, PJ_CART2D_EASTING_NORTHING, nullptr, 0);
717 972 : return m_pj_proj_crs_cs_temp;
718 : }
719 :
720 5615 : const char *OGRSpatialReference::Private::getProjCRSName()
721 : {
722 5615 : if (m_pjType == PJ_TYPE_PROJECTED_CRS)
723 : {
724 4607 : return proj_get_name(m_pj_crs);
725 : }
726 :
727 1008 : return "unnamed";
728 : }
729 :
730 1381 : OGRErr OGRSpatialReference::Private::replaceConversionAndUnref(PJ *conv)
731 : {
732 1381 : refreshProjObj();
733 :
734 1381 : demoteFromBoundCRS();
735 :
736 : auto projCRS =
737 1381 : proj_create_projected_crs(getPROJContext(), getProjCRSName(),
738 1381 : getGeodBaseCRS(), conv, getProjCRSCoordSys());
739 1381 : proj_destroy(conv);
740 :
741 1381 : setPjCRS(projCRS);
742 :
743 1381 : undoDemoteFromBoundCRS();
744 1381 : return OGRERR_NONE;
745 : }
746 :
747 : /************************************************************************/
748 : /* ToPointer() */
749 : /************************************************************************/
750 :
751 27641 : static inline OGRSpatialReference *ToPointer(OGRSpatialReferenceH hSRS)
752 : {
753 27641 : return OGRSpatialReference::FromHandle(hSRS);
754 : }
755 :
756 : /************************************************************************/
757 : /* ToHandle() */
758 : /************************************************************************/
759 :
760 4921 : static inline OGRSpatialReferenceH ToHandle(OGRSpatialReference *poSRS)
761 : {
762 4921 : return OGRSpatialReference::ToHandle(poSRS);
763 : }
764 :
765 : /************************************************************************/
766 : /* OGRsnPrintDouble() */
767 : /************************************************************************/
768 :
769 : void OGRsnPrintDouble(char *pszStrBuf, size_t size, double dfValue);
770 :
771 126 : void OGRsnPrintDouble(char *pszStrBuf, size_t size, double dfValue)
772 :
773 : {
774 126 : CPLsnprintf(pszStrBuf, size, "%.16g", dfValue);
775 :
776 126 : const size_t nLen = strlen(pszStrBuf);
777 :
778 : // The following hack is intended to truncate some "precision" in cases
779 : // that appear to be roundoff error.
780 126 : if (nLen > 15 && (strcmp(pszStrBuf + nLen - 6, "999999") == 0 ||
781 8 : strcmp(pszStrBuf + nLen - 6, "000001") == 0))
782 : {
783 0 : CPLsnprintf(pszStrBuf, size, "%.15g", dfValue);
784 : }
785 :
786 : // Force to user periods regardless of locale.
787 126 : if (strchr(pszStrBuf, ',') != nullptr)
788 : {
789 0 : char *const pszDelim = strchr(pszStrBuf, ',');
790 0 : *pszDelim = '.';
791 : }
792 126 : }
793 :
794 : /************************************************************************/
795 : /* OGRSpatialReference() */
796 : /************************************************************************/
797 :
798 : /**
799 : * \brief Constructor.
800 : *
801 : * This constructor takes an optional string argument which if passed
802 : * should be a WKT representation of an SRS. Passing this is equivalent
803 : * to not passing it, and then calling importFromWkt() with the WKT string.
804 : *
805 : * Note that newly created objects are given a reference count of one.
806 : *
807 : * Starting with GDAL 3.0, coordinates associated with a OGRSpatialReference
808 : * object are assumed to be in the order of the axis of the CRS definition
809 : (which
810 : * for example means latitude first, longitude second for geographic CRS
811 : belonging
812 : * to the EPSG authority). It is possible to define a data axis to CRS axis
813 : * mapping strategy with the SetAxisMappingStrategy() method.
814 : *
815 : * Starting with GDAL 3.5, the OSR_DEFAULT_AXIS_MAPPING_STRATEGY configuration
816 : * option can be set to "TRADITIONAL_GIS_ORDER" / "AUTHORITY_COMPLIANT" (the
817 : later
818 : * being the default value when the option is not set) to control the value of
819 : the
820 : * data axis to CRS axis mapping strategy when a OSRSpatialReference object is
821 : * created. Calling SetAxisMappingStrategy() will override this default value.
822 :
823 : * The C function OSRNewSpatialReference() does the same thing as this
824 : * constructor.
825 : *
826 : * @param pszWKT well known text definition to which the object should
827 : * be initialized, or NULL (the default).
828 : */
829 :
830 279785 : OGRSpatialReference::OGRSpatialReference(const char *pszWKT)
831 279785 : : d(new Private(this))
832 : {
833 279785 : if (pszWKT != nullptr)
834 1255 : importFromWkt(pszWKT);
835 279785 : }
836 :
837 : /************************************************************************/
838 : /* OSRNewSpatialReference() */
839 : /************************************************************************/
840 :
841 : /**
842 : * \brief Constructor.
843 : *
844 : * Starting with GDAL 3.0, coordinates associated with a OGRSpatialReference
845 : * object are assumed to be in the order of the axis of the CRS definition
846 : * (which for example means latitude first, longitude second for geographic CRS
847 : * belonging to the EPSG authority). It is possible to define a data axis to CRS
848 : * axis mapping strategy with the SetAxisMappingStrategy() method.
849 : *
850 : * Starting with GDAL 3.5, the OSR_DEFAULT_AXIS_MAPPING_STRATEGY configuration
851 : * option can be set to "TRADITIONAL_GIS_ORDER" / "AUTHORITY_COMPLIANT" (the
852 : * later being the default value when the option is not set) to control the
853 : * value of the data axis to CRS axis mapping strategy when a
854 : * OSRSpatialReference object is created. Calling SetAxisMappingStrategy() will
855 : * override this default value.
856 : *
857 : * This function is the same as OGRSpatialReference::OGRSpatialReference()
858 : */
859 3413 : OGRSpatialReferenceH CPL_STDCALL OSRNewSpatialReference(const char *pszWKT)
860 :
861 : {
862 3413 : OGRSpatialReference *poSRS = new OGRSpatialReference();
863 :
864 3413 : if (pszWKT != nullptr && strlen(pszWKT) > 0)
865 : {
866 65 : if (poSRS->importFromWkt(pszWKT) != OGRERR_NONE)
867 : {
868 0 : delete poSRS;
869 0 : poSRS = nullptr;
870 : }
871 : }
872 :
873 3413 : return ToHandle(poSRS);
874 : }
875 :
876 : /************************************************************************/
877 : /* OGRSpatialReference() */
878 : /************************************************************************/
879 :
880 : /** Copy constructor. See also Clone().
881 : * @param oOther other spatial reference
882 : */
883 2746 : OGRSpatialReference::OGRSpatialReference(const OGRSpatialReference &oOther)
884 2746 : : d(new Private(this))
885 : {
886 2746 : *this = oOther;
887 2746 : }
888 :
889 : /************************************************************************/
890 : /* OGRSpatialReference() */
891 : /************************************************************************/
892 :
893 : /** Move constructor.
894 : * @param oOther other spatial reference
895 : */
896 1 : OGRSpatialReference::OGRSpatialReference(OGRSpatialReference &&oOther)
897 1 : : d(std::move(oOther.d))
898 : {
899 1 : }
900 :
901 : /************************************************************************/
902 : /* ~OGRSpatialReference() */
903 : /************************************************************************/
904 :
905 : /**
906 : * \brief OGRSpatialReference destructor.
907 : *
908 : * The C function OSRDestroySpatialReference() does the same thing as this
909 : * method. Preferred C++ method : OGRSpatialReference::DestroySpatialReference()
910 : *
911 : * @deprecated
912 : */
913 :
914 342437 : OGRSpatialReference::~OGRSpatialReference()
915 :
916 : {
917 342437 : }
918 :
919 : /************************************************************************/
920 : /* DestroySpatialReference() */
921 : /************************************************************************/
922 :
923 : /**
924 : * \brief OGRSpatialReference destructor.
925 : *
926 : * This static method will destroy a OGRSpatialReference. It is
927 : * equivalent to calling delete on the object, but it ensures that the
928 : * deallocation is properly executed within the OGR libraries heap on
929 : * platforms where this can matter (win32).
930 : *
931 : * This function is the same as OSRDestroySpatialReference()
932 : *
933 : * @param poSRS the object to delete
934 : *
935 : */
936 :
937 0 : void OGRSpatialReference::DestroySpatialReference(OGRSpatialReference *poSRS)
938 : {
939 0 : delete poSRS;
940 0 : }
941 :
942 : /************************************************************************/
943 : /* OSRDestroySpatialReference() */
944 : /************************************************************************/
945 :
946 : /**
947 : * \brief OGRSpatialReference destructor.
948 : *
949 : * This function is the same as OGRSpatialReference::~OGRSpatialReference()
950 : * and OGRSpatialReference::DestroySpatialReference()
951 : *
952 : * @param hSRS the object to delete
953 : */
954 9807 : void CPL_STDCALL OSRDestroySpatialReference(OGRSpatialReferenceH hSRS)
955 :
956 : {
957 9807 : delete ToPointer(hSRS);
958 9807 : }
959 :
960 : /************************************************************************/
961 : /* Clear() */
962 : /************************************************************************/
963 :
964 : /**
965 : * \brief Wipe current definition.
966 : *
967 : * Returns OGRSpatialReference to a state with no definition, as it
968 : * exists when first created. It does not affect reference counts.
969 : */
970 :
971 127931 : void OGRSpatialReference::Clear()
972 :
973 : {
974 127931 : d->clear();
975 127931 : }
976 :
977 : /************************************************************************/
978 : /* operator=() */
979 : /************************************************************************/
980 :
981 : /** Assignment operator.
982 : * @param oSource SRS to assign to *this
983 : * @return *this
984 : */
985 : OGRSpatialReference &
986 28559 : OGRSpatialReference::operator=(const OGRSpatialReference &oSource)
987 :
988 : {
989 28559 : if (&oSource != this)
990 : {
991 28558 : Clear();
992 : #ifdef CPPCHECK
993 : // Otherwise cppcheck would protest that nRefCount isn't modified
994 : d->nRefCount = (d->nRefCount + 1) - 1;
995 : #endif
996 :
997 28558 : oSource.d->refreshProjObj();
998 28558 : if (oSource.d->m_pj_crs)
999 28104 : d->setPjCRS(proj_clone(d->getPROJContext(), oSource.d->m_pj_crs));
1000 28558 : if (oSource.d->m_axisMappingStrategy == OAMS_TRADITIONAL_GIS_ORDER)
1001 13488 : SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
1002 15070 : else if (oSource.d->m_axisMappingStrategy == OAMS_CUSTOM)
1003 144 : SetDataAxisToSRSAxisMapping(oSource.d->m_axisMapping);
1004 :
1005 28558 : d->m_coordinateEpoch = oSource.d->m_coordinateEpoch;
1006 : }
1007 :
1008 28559 : return *this;
1009 : }
1010 :
1011 : /************************************************************************/
1012 : /* operator=() */
1013 : /************************************************************************/
1014 :
1015 : /** Move assignment operator.
1016 : * @param oSource SRS to assign to *this
1017 : * @return *this
1018 : */
1019 : OGRSpatialReference &
1020 5456 : OGRSpatialReference::operator=(OGRSpatialReference &&oSource)
1021 :
1022 : {
1023 5456 : if (&oSource != this)
1024 : {
1025 5455 : d = std::move(oSource.d);
1026 : }
1027 :
1028 5456 : return *this;
1029 : }
1030 :
1031 : /************************************************************************/
1032 : /* AssignAndSetThreadSafe() */
1033 : /************************************************************************/
1034 :
1035 : /** Assignment method, with thread-safety.
1036 : *
1037 : * Same as an assignment operator, but asking also that the *this instance
1038 : * becomes thread-safe.
1039 : *
1040 : * @param oSource SRS to assign to *this
1041 : * @return *this
1042 : * @since 3.10
1043 : */
1044 :
1045 : OGRSpatialReference &
1046 2 : OGRSpatialReference::AssignAndSetThreadSafe(const OGRSpatialReference &oSource)
1047 : {
1048 2 : *this = oSource;
1049 2 : d->SetThreadSafe();
1050 2 : return *this;
1051 : }
1052 :
1053 : /************************************************************************/
1054 : /* Reference() */
1055 : /************************************************************************/
1056 :
1057 : /**
1058 : * \brief Increments the reference count by one.
1059 : *
1060 : * The reference count is used keep track of the number of OGRGeometry objects
1061 : * referencing this SRS.
1062 : *
1063 : * The method does the same thing as the C function OSRReference().
1064 : *
1065 : * @return the updated reference count.
1066 : */
1067 :
1068 3733330 : int OGRSpatialReference::Reference()
1069 :
1070 : {
1071 3733330 : return CPLAtomicInc(&d->nRefCount);
1072 : }
1073 :
1074 : /************************************************************************/
1075 : /* OSRReference() */
1076 : /************************************************************************/
1077 :
1078 : /**
1079 : * \brief Increments the reference count by one.
1080 : *
1081 : * This function is the same as OGRSpatialReference::Reference()
1082 : */
1083 1095 : int OSRReference(OGRSpatialReferenceH hSRS)
1084 :
1085 : {
1086 1095 : VALIDATE_POINTER1(hSRS, "OSRReference", 0);
1087 :
1088 1095 : return ToPointer(hSRS)->Reference();
1089 : }
1090 :
1091 : /************************************************************************/
1092 : /* Dereference() */
1093 : /************************************************************************/
1094 :
1095 : /**
1096 : * \brief Decrements the reference count by one.
1097 : *
1098 : * The method does the same thing as the C function OSRDereference().
1099 : *
1100 : * @return the updated reference count.
1101 : */
1102 :
1103 3776850 : int OGRSpatialReference::Dereference()
1104 :
1105 : {
1106 3776850 : if (d->nRefCount <= 0)
1107 0 : CPLDebug("OSR",
1108 : "Dereference() called on an object with refcount %d,"
1109 : "likely already destroyed!",
1110 0 : d->nRefCount);
1111 3776850 : return CPLAtomicDec(&d->nRefCount);
1112 : }
1113 :
1114 : /************************************************************************/
1115 : /* OSRDereference() */
1116 : /************************************************************************/
1117 :
1118 : /**
1119 : * \brief Decrements the reference count by one.
1120 : *
1121 : * This function is the same as OGRSpatialReference::Dereference()
1122 : */
1123 0 : int OSRDereference(OGRSpatialReferenceH hSRS)
1124 :
1125 : {
1126 0 : VALIDATE_POINTER1(hSRS, "OSRDereference", 0);
1127 :
1128 0 : return ToPointer(hSRS)->Dereference();
1129 : }
1130 :
1131 : /************************************************************************/
1132 : /* GetReferenceCount() */
1133 : /************************************************************************/
1134 :
1135 : /**
1136 : * \brief Fetch current reference count.
1137 : *
1138 : * @return the current reference count.
1139 : */
1140 180 : int OGRSpatialReference::GetReferenceCount() const
1141 : {
1142 180 : return d->nRefCount;
1143 : }
1144 :
1145 : /************************************************************************/
1146 : /* Release() */
1147 : /************************************************************************/
1148 :
1149 : /**
1150 : * \brief Decrements the reference count by one, and destroy if zero.
1151 : *
1152 : * The method does the same thing as the C function OSRRelease().
1153 : */
1154 :
1155 3776590 : void OGRSpatialReference::Release()
1156 :
1157 : {
1158 3776590 : if (Dereference() <= 0)
1159 43464 : delete this;
1160 3776590 : }
1161 :
1162 : /************************************************************************/
1163 : /* OSRRelease() */
1164 : /************************************************************************/
1165 :
1166 : /**
1167 : * \brief Decrements the reference count by one, and destroy if zero.
1168 : *
1169 : * This function is the same as OGRSpatialReference::Release()
1170 : */
1171 6908 : void OSRRelease(OGRSpatialReferenceH hSRS)
1172 :
1173 : {
1174 6908 : VALIDATE_POINTER0(hSRS, "OSRRelease");
1175 :
1176 6908 : ToPointer(hSRS)->Release();
1177 : }
1178 :
1179 95673 : OGR_SRSNode *OGRSpatialReference::GetRoot()
1180 : {
1181 95673 : TAKE_OPTIONAL_LOCK();
1182 :
1183 95673 : if (!d->m_poRoot)
1184 : {
1185 29206 : d->refreshRootFromProjObj(false);
1186 : }
1187 191346 : return d->m_poRoot;
1188 : }
1189 :
1190 8804 : const OGR_SRSNode *OGRSpatialReference::GetRoot() const
1191 : {
1192 8804 : TAKE_OPTIONAL_LOCK();
1193 :
1194 8804 : if (!d->m_poRoot)
1195 : {
1196 3405 : d->refreshRootFromProjObj(false);
1197 : }
1198 17608 : return d->m_poRoot;
1199 : }
1200 :
1201 : /************************************************************************/
1202 : /* SetRoot() */
1203 : /************************************************************************/
1204 :
1205 : /**
1206 : * \brief Set the root SRS node.
1207 : *
1208 : * If the object has an existing tree of OGR_SRSNodes, they are destroyed
1209 : * as part of assigning the new root. Ownership of the passed OGR_SRSNode is
1210 : * is assumed by the OGRSpatialReference.
1211 : *
1212 : * @param poNewRoot object to assign as root.
1213 : */
1214 :
1215 45 : void OGRSpatialReference::SetRoot(OGR_SRSNode *poNewRoot)
1216 :
1217 : {
1218 45 : if (d->m_poRoot != poNewRoot)
1219 : {
1220 45 : delete d->m_poRoot;
1221 45 : d->setRoot(poNewRoot);
1222 : }
1223 45 : }
1224 :
1225 : /************************************************************************/
1226 : /* GetAttrNode() */
1227 : /************************************************************************/
1228 :
1229 : /**
1230 : * \brief Find named node in tree.
1231 : *
1232 : * This method does a pre-order traversal of the node tree searching for
1233 : * a node with this exact value (case insensitive), and returns it. Leaf
1234 : * nodes are not considered, under the assumption that they are just
1235 : * attribute value nodes.
1236 : *
1237 : * If a node appears more than once in the tree (such as UNIT for instance),
1238 : * the first encountered will be returned. Use GetNode() on a subtree to be
1239 : * more specific.
1240 : *
1241 : * @param pszNodePath the name of the node to search for. May contain multiple
1242 : * components such as "GEOGCS|UNIT".
1243 : *
1244 : * @return a pointer to the node found, or NULL if none.
1245 : */
1246 :
1247 92532 : OGR_SRSNode *OGRSpatialReference::GetAttrNode(const char *pszNodePath)
1248 :
1249 : {
1250 92532 : if (strstr(pszNodePath, "CONVERSION") && !d->m_bNodesWKT2)
1251 : {
1252 17 : d->invalidateNodes();
1253 17 : d->refreshRootFromProjObj(/* bForceWKT2 = */ true);
1254 : }
1255 :
1256 92532 : if (strchr(pszNodePath, '|') == nullptr)
1257 : {
1258 : // Fast path
1259 51469 : OGR_SRSNode *poNode = GetRoot();
1260 51469 : if (poNode)
1261 50252 : poNode = poNode->GetNode(pszNodePath);
1262 51469 : return poNode;
1263 : }
1264 :
1265 : char **papszPathTokens =
1266 41063 : CSLTokenizeStringComplex(pszNodePath, "|", TRUE, FALSE);
1267 :
1268 41063 : if (CSLCount(papszPathTokens) < 1)
1269 : {
1270 0 : CSLDestroy(papszPathTokens);
1271 0 : return nullptr;
1272 : }
1273 :
1274 41063 : OGR_SRSNode *poNode = GetRoot();
1275 125020 : for (int i = 0; poNode != nullptr && papszPathTokens[i] != nullptr; i++)
1276 : {
1277 83957 : poNode = poNode->GetNode(papszPathTokens[i]);
1278 : }
1279 :
1280 41063 : CSLDestroy(papszPathTokens);
1281 :
1282 41063 : return poNode;
1283 : }
1284 :
1285 : /**
1286 : * \brief Find named node in tree.
1287 : *
1288 : * This method does a pre-order traversal of the node tree searching for
1289 : * a node with this exact value (case insensitive), and returns it. Leaf
1290 : * nodes are not considered, under the assumption that they are just
1291 : * attribute value nodes.
1292 : *
1293 : * If a node appears more than once in the tree (such as UNIT for instance),
1294 : * the first encountered will be returned. Use GetNode() on a subtree to be
1295 : * more specific.
1296 : *
1297 : * @param pszNodePath the name of the node to search for. May contain multiple
1298 : * components such as "GEOGCS|UNIT".
1299 : *
1300 : * @return a pointer to the node found, or NULL if none.
1301 : */
1302 :
1303 : const OGR_SRSNode *
1304 83776 : OGRSpatialReference::GetAttrNode(const char *pszNodePath) const
1305 :
1306 : {
1307 : OGR_SRSNode *poNode =
1308 83776 : const_cast<OGRSpatialReference *>(this)->GetAttrNode(pszNodePath);
1309 :
1310 83776 : return poNode;
1311 : }
1312 :
1313 : /************************************************************************/
1314 : /* GetAttrValue() */
1315 : /************************************************************************/
1316 :
1317 : /**
1318 : * \brief Fetch indicated attribute of named node.
1319 : *
1320 : * This method uses GetAttrNode() to find the named node, and then extracts
1321 : * the value of the indicated child. Thus a call to GetAttrValue("UNIT",1)
1322 : * would return the second child of the UNIT node, which is normally the
1323 : * length of the linear unit in meters.
1324 : *
1325 : * This method does the same thing as the C function OSRGetAttrValue().
1326 : *
1327 : * @param pszNodeName the tree node to look for (case insensitive).
1328 : * @param iAttr the child of the node to fetch (zero based).
1329 : *
1330 : * @return the requested value, or NULL if it fails for any reason.
1331 : */
1332 :
1333 25093 : const char *OGRSpatialReference::GetAttrValue(const char *pszNodeName,
1334 : int iAttr) const
1335 :
1336 : {
1337 25093 : const OGR_SRSNode *poNode = GetAttrNode(pszNodeName);
1338 25093 : if (poNode == nullptr)
1339 : {
1340 10721 : if (d->m_bNodesWKT2 && EQUAL(pszNodeName, "PROJECTION"))
1341 : {
1342 14 : return GetAttrValue("METHOD", iAttr);
1343 : }
1344 10707 : else if (d->m_bNodesWKT2 && EQUAL(pszNodeName, "PROJCS|PROJECTION"))
1345 : {
1346 0 : return GetAttrValue("PROJCRS|METHOD", iAttr);
1347 : }
1348 10707 : else if (d->m_bNodesWKT2 && EQUAL(pszNodeName, "PROJCS"))
1349 : {
1350 1 : return GetAttrValue("PROJCRS", iAttr);
1351 : }
1352 10706 : return nullptr;
1353 : }
1354 :
1355 14372 : if (iAttr < 0 || iAttr >= poNode->GetChildCount())
1356 0 : return nullptr;
1357 :
1358 14372 : return poNode->GetChild(iAttr)->GetValue();
1359 : }
1360 :
1361 : /************************************************************************/
1362 : /* OSRGetAttrValue() */
1363 : /************************************************************************/
1364 :
1365 : /**
1366 : * \brief Fetch indicated attribute of named node.
1367 : *
1368 : * This function is the same as OGRSpatialReference::GetAttrValue()
1369 : */
1370 36 : const char *CPL_STDCALL OSRGetAttrValue(OGRSpatialReferenceH hSRS,
1371 : const char *pszKey, int iChild)
1372 :
1373 : {
1374 36 : VALIDATE_POINTER1(hSRS, "OSRGetAttrValue", nullptr);
1375 :
1376 36 : return ToPointer(hSRS)->GetAttrValue(pszKey, iChild);
1377 : }
1378 :
1379 : /************************************************************************/
1380 : /* GetName() */
1381 : /************************************************************************/
1382 :
1383 : /**
1384 : * \brief Return the CRS name.
1385 : *
1386 : * The returned value is only short lived and should not be used after other
1387 : * calls to methods on this object.
1388 : *
1389 : * @since GDAL 3.0
1390 : */
1391 :
1392 8965 : const char *OGRSpatialReference::GetName() const
1393 : {
1394 17930 : TAKE_OPTIONAL_LOCK();
1395 :
1396 8965 : d->refreshProjObj();
1397 8965 : if (!d->m_pj_crs)
1398 113 : return nullptr;
1399 8852 : const char *pszName = proj_get_name(d->m_pj_crs);
1400 : #if PROJ_VERSION_NUMBER == PROJ_COMPUTE_VERSION(8, 2, 0)
1401 : if (d->m_pjType == PJ_TYPE_BOUND_CRS && EQUAL(pszName, "SOURCECRS"))
1402 : {
1403 : // Work around a bug of PROJ 8.2.0 (fixed in 8.2.1)
1404 : PJ *baseCRS = proj_get_source_crs(d->getPROJContext(), d->m_pj_crs);
1405 : if (baseCRS)
1406 : {
1407 : pszName = proj_get_name(baseCRS);
1408 : // pszName still remains valid after proj_destroy(), since
1409 : // d->m_pj_crs keeps a reference to the base CRS C++ object.
1410 : proj_destroy(baseCRS);
1411 : }
1412 : }
1413 : #endif
1414 8852 : return pszName;
1415 : }
1416 :
1417 : /************************************************************************/
1418 : /* OSRGetName() */
1419 : /************************************************************************/
1420 :
1421 : /**
1422 : * \brief Return the CRS name.
1423 : *
1424 : * The returned value is only short lived and should not be used after other
1425 : * calls to methods on this object.
1426 : *
1427 : * @since GDAL 3.0
1428 : */
1429 47 : const char *OSRGetName(OGRSpatialReferenceH hSRS)
1430 :
1431 : {
1432 47 : VALIDATE_POINTER1(hSRS, "OSRGetName", nullptr);
1433 :
1434 47 : return ToPointer(hSRS)->GetName();
1435 : }
1436 :
1437 : /************************************************************************/
1438 : /* GetCelestialBodyName() */
1439 : /************************************************************************/
1440 :
1441 : /**
1442 : * \brief Return the name of the celestial body of this CRS.
1443 : *
1444 : * e.g. "Earth" for an Earth CRS
1445 : *
1446 : * The returned value is only short lived and should not be used after other
1447 : * calls to methods on this object.
1448 : *
1449 : * @since GDAL 3.12 and PROJ 8.1
1450 : */
1451 :
1452 4 : const char *OGRSpatialReference::GetCelestialBodyName() const
1453 : {
1454 : #if PROJ_AT_LEAST_VERSION(8, 1, 0)
1455 :
1456 : TAKE_OPTIONAL_LOCK();
1457 :
1458 : d->refreshProjObj();
1459 : if (!d->m_pj_crs)
1460 : return nullptr;
1461 : d->demoteFromBoundCRS();
1462 : const char *name =
1463 : proj_get_celestial_body_name(d->getPROJContext(), d->m_pj_crs);
1464 : if (name)
1465 : {
1466 : d->m_celestialBodyName = name;
1467 : }
1468 : d->undoDemoteFromBoundCRS();
1469 : return d->m_celestialBodyName.c_str();
1470 : #else
1471 4 : if (std::fabs(GetSemiMajor(nullptr) - SRS_WGS84_SEMIMAJOR) <=
1472 : 0.05 * SRS_WGS84_SEMIMAJOR)
1473 4 : return "Earth";
1474 0 : const char *pszAuthName = GetAuthorityName();
1475 0 : if (pszAuthName && EQUAL(pszAuthName, "EPSG"))
1476 0 : return "Earth";
1477 0 : return nullptr;
1478 : #endif
1479 : }
1480 :
1481 : /************************************************************************/
1482 : /* OSRGetCelestialBodyName() */
1483 : /************************************************************************/
1484 :
1485 : /**
1486 : * \brief Return the name of the celestial body of this CRS.
1487 : *
1488 : * e.g. "Earth" for an Earth CRS
1489 : *
1490 : * The returned value is only short lived and should not be used after other
1491 : * calls to methods on this object.
1492 : *
1493 : * @since GDAL 3.12 and PROJ 8.1
1494 : */
1495 :
1496 1 : const char *OSRGetCelestialBodyName(OGRSpatialReferenceH hSRS)
1497 :
1498 : {
1499 1 : VALIDATE_POINTER1(hSRS, "GetCelestialBodyName", nullptr);
1500 :
1501 1 : return ToPointer(hSRS)->GetCelestialBodyName();
1502 : }
1503 :
1504 : /************************************************************************/
1505 : /* Clone() */
1506 : /************************************************************************/
1507 :
1508 : /**
1509 : * \brief Make a duplicate of this OGRSpatialReference.
1510 : *
1511 : * This method is the same as the C function OSRClone().
1512 : *
1513 : * @return a new SRS, which becomes the responsibility of the caller.
1514 : */
1515 :
1516 32838 : OGRSpatialReference *OGRSpatialReference::Clone() const
1517 :
1518 : {
1519 32838 : OGRSpatialReference *poNewRef = new OGRSpatialReference();
1520 :
1521 32838 : TAKE_OPTIONAL_LOCK();
1522 :
1523 32838 : d->refreshProjObj();
1524 32838 : if (d->m_pj_crs != nullptr)
1525 32794 : poNewRef->d->setPjCRS(proj_clone(d->getPROJContext(), d->m_pj_crs));
1526 32838 : if (d->m_bHasCenterLong && d->m_poRoot)
1527 : {
1528 0 : poNewRef->d->setRoot(d->m_poRoot->Clone());
1529 : }
1530 32838 : poNewRef->d->m_axisMapping = d->m_axisMapping;
1531 32838 : poNewRef->d->m_axisMappingStrategy = d->m_axisMappingStrategy;
1532 32838 : poNewRef->d->m_coordinateEpoch = d->m_coordinateEpoch;
1533 65676 : return poNewRef;
1534 : }
1535 :
1536 : /************************************************************************/
1537 : /* OSRClone() */
1538 : /************************************************************************/
1539 :
1540 : /**
1541 : * \brief Make a duplicate of this OGRSpatialReference.
1542 : *
1543 : * This function is the same as OGRSpatialReference::Clone()
1544 : */
1545 1354 : OGRSpatialReferenceH CPL_STDCALL OSRClone(OGRSpatialReferenceH hSRS)
1546 :
1547 : {
1548 1354 : VALIDATE_POINTER1(hSRS, "OSRClone", nullptr);
1549 :
1550 1354 : return ToHandle(ToPointer(hSRS)->Clone());
1551 : }
1552 :
1553 : /************************************************************************/
1554 : /* dumpReadable() */
1555 : /************************************************************************/
1556 :
1557 : /** Dump pretty wkt to stdout, mostly for debugging.
1558 : */
1559 0 : void OGRSpatialReference::dumpReadable()
1560 :
1561 : {
1562 0 : char *pszPrettyWkt = nullptr;
1563 :
1564 0 : const char *const apszOptions[] = {"FORMAT=WKT2", "MULTILINE=YES", nullptr};
1565 0 : exportToWkt(&pszPrettyWkt, apszOptions);
1566 0 : printf("%s\n", pszPrettyWkt); /*ok*/
1567 0 : CPLFree(pszPrettyWkt);
1568 0 : }
1569 :
1570 : /************************************************************************/
1571 : /* exportToPrettyWkt() */
1572 : /************************************************************************/
1573 :
1574 : /**
1575 : * Convert this SRS into a nicely formatted WKT 1 string for display to a
1576 : * person.
1577 : *
1578 : * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
1579 : * Issues</a> page for implementation details of WKT 1 in OGR.
1580 : *
1581 : * Note that the returned WKT string should be freed with
1582 : * CPLFree() when no longer needed. It is the responsibility of the caller.
1583 : *
1584 : * The WKT version can be overridden by using the OSR_WKT_FORMAT configuration
1585 : * option. Valid values are the one of the FORMAT option of
1586 : * exportToWkt( char ** ppszResult, const char* const* papszOptions ) const
1587 : *
1588 : * This method is the same as the C function OSRExportToPrettyWkt().
1589 : *
1590 : * @param ppszResult the resulting string is returned in this pointer.
1591 : * @param bSimplify TRUE if the AXIS, AUTHORITY and EXTENSION nodes should be
1592 : * stripped off.
1593 : *
1594 : * @return OGRERR_NONE if successful.
1595 : */
1596 :
1597 58 : OGRErr OGRSpatialReference::exportToPrettyWkt(char **ppszResult,
1598 : int bSimplify) const
1599 :
1600 : {
1601 116 : CPLStringList aosOptions;
1602 58 : aosOptions.SetNameValue("MULTILINE", "YES");
1603 58 : if (bSimplify)
1604 : {
1605 0 : aosOptions.SetNameValue("FORMAT", "WKT1_SIMPLE");
1606 : }
1607 116 : return exportToWkt(ppszResult, aosOptions.List());
1608 : }
1609 :
1610 : /************************************************************************/
1611 : /* OSRExportToPrettyWkt() */
1612 : /************************************************************************/
1613 :
1614 : /**
1615 : * \brief Convert this SRS into a nicely formatted WKT 1 string for display to a
1616 : * person.
1617 : *
1618 : * The WKT version can be overridden by using the OSR_WKT_FORMAT configuration
1619 : * option. Valid values are the one of the FORMAT option of
1620 : * exportToWkt( char ** ppszResult, const char* const* papszOptions ) const
1621 : *
1622 : * This function is the same as OGRSpatialReference::exportToPrettyWkt().
1623 : */
1624 :
1625 56 : OGRErr CPL_STDCALL OSRExportToPrettyWkt(OGRSpatialReferenceH hSRS,
1626 : char **ppszReturn, int bSimplify)
1627 :
1628 : {
1629 56 : VALIDATE_POINTER1(hSRS, "OSRExportToPrettyWkt", OGRERR_FAILURE);
1630 :
1631 56 : *ppszReturn = nullptr;
1632 :
1633 56 : return ToPointer(hSRS)->exportToPrettyWkt(ppszReturn, bSimplify);
1634 : }
1635 :
1636 : /************************************************************************/
1637 : /* exportToWkt() */
1638 : /************************************************************************/
1639 :
1640 : /**
1641 : * \brief Convert this SRS into WKT 1 format.
1642 : *
1643 : * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
1644 : * Issues</a> page for implementation details of WKT 1 in OGR.
1645 : *
1646 : * Note that the returned WKT string should be freed with
1647 : * CPLFree() when no longer needed. It is the responsibility of the caller.
1648 : *
1649 : * The WKT version can be overridden by using the OSR_WKT_FORMAT configuration
1650 : * option. Valid values are the one of the FORMAT option of
1651 : * exportToWkt( char ** ppszResult, const char* const* papszOptions ) const
1652 : *
1653 : * This method is the same as the C function OSRExportToWkt().
1654 : *
1655 : * @param ppszResult the resulting string is returned in this pointer.
1656 : *
1657 : * @return OGRERR_NONE if successful.
1658 : */
1659 :
1660 12392 : OGRErr OGRSpatialReference::exportToWkt(char **ppszResult) const
1661 :
1662 : {
1663 12392 : return exportToWkt(ppszResult, nullptr);
1664 : }
1665 :
1666 : /************************************************************************/
1667 : /* GDAL_proj_crs_create_bound_crs_to_WGS84() */
1668 : /************************************************************************/
1669 :
1670 549 : static PJ *GDAL_proj_crs_create_bound_crs_to_WGS84(PJ_CONTEXT *ctx, PJ *pj,
1671 : bool onlyIfEPSGCode,
1672 : bool canModifyHorizPart)
1673 : {
1674 549 : PJ *ret = nullptr;
1675 549 : if (proj_get_type(pj) == PJ_TYPE_COMPOUND_CRS)
1676 : {
1677 13 : auto horizCRS = proj_crs_get_sub_crs(ctx, pj, 0);
1678 13 : auto vertCRS = proj_crs_get_sub_crs(ctx, pj, 1);
1679 13 : if (horizCRS && proj_get_type(horizCRS) != PJ_TYPE_BOUND_CRS &&
1680 26 : vertCRS &&
1681 10 : (!onlyIfEPSGCode || proj_get_id_auth_name(horizCRS, 0) != nullptr))
1682 : {
1683 : auto boundHoriz =
1684 : canModifyHorizPart
1685 3 : ? proj_crs_create_bound_crs_to_WGS84(ctx, horizCRS, nullptr)
1686 3 : : proj_clone(ctx, horizCRS);
1687 : auto boundVert =
1688 3 : proj_crs_create_bound_crs_to_WGS84(ctx, vertCRS, nullptr);
1689 3 : if (boundHoriz && boundVert)
1690 : {
1691 3 : ret = proj_create_compound_crs(ctx, proj_get_name(pj),
1692 : boundHoriz, boundVert);
1693 : }
1694 3 : proj_destroy(boundHoriz);
1695 3 : proj_destroy(boundVert);
1696 : }
1697 13 : proj_destroy(horizCRS);
1698 13 : proj_destroy(vertCRS);
1699 : }
1700 1032 : else if (proj_get_type(pj) != PJ_TYPE_BOUND_CRS &&
1701 496 : (!onlyIfEPSGCode || proj_get_id_auth_name(pj, 0) != nullptr))
1702 : {
1703 189 : ret = proj_crs_create_bound_crs_to_WGS84(ctx, pj, nullptr);
1704 : }
1705 549 : return ret;
1706 : }
1707 :
1708 : /************************************************************************/
1709 : /* exportToWkt() */
1710 : /************************************************************************/
1711 :
1712 : /**
1713 : * Convert this SRS into a WKT string.
1714 : *
1715 : * Note that the returned WKT string should be freed with
1716 : * CPLFree() when no longer needed. It is the responsibility of the caller.
1717 : *
1718 : * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
1719 : * Issues</a> page for implementation details of WKT 1 in OGR.
1720 : *
1721 : * @param ppszResult the resulting string is returned in this pointer.
1722 : * @param papszOptions NULL terminated list of options, or NULL. Currently
1723 : * supported options are
1724 : * <ul>
1725 : * <li>MULTILINE=YES/NO. Defaults to NO.</li>
1726 : * <li>FORMAT=SFSQL/WKT1_SIMPLE/WKT1/WKT1_GDAL/WKT1_ESRI/WKT2_2015/WKT2_2018/WKT2/DEFAULT.
1727 : * If SFSQL, a WKT1 string without AXIS, TOWGS84, AUTHORITY or EXTENSION
1728 : * node is returned.
1729 : * If WKT1_SIMPLE, a WKT1 string without AXIS, AUTHORITY or EXTENSION
1730 : * node is returned.
1731 : * WKT1 is an alias of WKT1_GDAL.
1732 : * WKT2 will default to the latest revision implemented (currently
1733 : * WKT2_2018) WKT2_2019 can be used as an alias of WKT2_2018 since GDAL 3.2
1734 : * </li>
1735 : * <li>ALLOW_ELLIPSOIDAL_HEIGHT_AS_VERTICAL_CRS=YES/NO. Default is NO. If set
1736 : * to YES and FORMAT=WKT1_GDAL, a Geographic 3D CRS or a Projected 3D CRS will
1737 : * be exported as a compound CRS whose vertical part represents an ellipsoidal
1738 : * height (for example for use with LAS 1.4 WKT1).
1739 : * Requires PROJ 7.2.1 and GDAL 3.2.1.</li>
1740 : * </ul>
1741 : *
1742 : * Starting with GDAL 3.0.3, if the OSR_ADD_TOWGS84_ON_EXPORT_TO_WKT1
1743 : * configuration option is set to YES, when exporting to WKT1_GDAL, this method
1744 : * will try to add a TOWGS84[] node, if there's none attached yet to the SRS and
1745 : * if the SRS has a EPSG code. See the AddGuessedTOWGS84() method for how this
1746 : * TOWGS84[] node may be added.
1747 : *
1748 : * @return OGRERR_NONE if successful.
1749 : * @since GDAL 3.0
1750 : */
1751 :
1752 17521 : OGRErr OGRSpatialReference::exportToWkt(char **ppszResult,
1753 : const char *const *papszOptions) const
1754 : {
1755 : // In the past calling this method was thread-safe, even if we never
1756 : // guaranteed it. Now proj_as_wkt() will cache the result internally,
1757 : // so this is no longer thread-safe.
1758 35042 : std::lock_guard oLock(d->m_mutex);
1759 :
1760 17521 : d->refreshProjObj();
1761 17521 : if (!d->m_pj_crs)
1762 : {
1763 21 : *ppszResult = CPLStrdup("");
1764 21 : return OGRERR_FAILURE;
1765 : }
1766 :
1767 17500 : if (d->m_bHasCenterLong && d->m_poRoot && !d->m_bMorphToESRI)
1768 : {
1769 0 : return d->m_poRoot->exportToWkt(ppszResult);
1770 : }
1771 :
1772 17500 : auto ctxt = d->getPROJContext();
1773 17500 : auto wktFormat = PJ_WKT1_GDAL;
1774 : const char *pszFormat =
1775 17500 : CSLFetchNameValueDef(papszOptions, "FORMAT",
1776 : CPLGetConfigOption("OSR_WKT_FORMAT", "DEFAULT"));
1777 17500 : if (EQUAL(pszFormat, "DEFAULT"))
1778 14637 : pszFormat = "";
1779 :
1780 17500 : if (EQUAL(pszFormat, "WKT1_ESRI") || d->m_bMorphToESRI)
1781 : {
1782 672 : wktFormat = PJ_WKT1_ESRI;
1783 : }
1784 16828 : else if (EQUAL(pszFormat, "WKT1") || EQUAL(pszFormat, "WKT1_GDAL") ||
1785 16109 : EQUAL(pszFormat, "WKT1_SIMPLE") || EQUAL(pszFormat, "SFSQL"))
1786 : {
1787 724 : wktFormat = PJ_WKT1_GDAL;
1788 : }
1789 16104 : else if (EQUAL(pszFormat, "WKT2_2015"))
1790 : {
1791 363 : wktFormat = PJ_WKT2_2015;
1792 : }
1793 15741 : else if (EQUAL(pszFormat, "WKT2") || EQUAL(pszFormat, "WKT2_2018") ||
1794 15342 : EQUAL(pszFormat, "WKT2_2019"))
1795 : {
1796 1334 : wktFormat = PJ_WKT2_2018;
1797 : }
1798 14407 : else if (pszFormat[0] == '\0')
1799 : {
1800 : // cppcheck-suppress knownConditionTrueFalse
1801 14407 : if (IsDerivedGeographic())
1802 : {
1803 2 : wktFormat = PJ_WKT2_2018;
1804 : }
1805 28177 : else if ((IsGeographic() || IsProjected()) && !IsCompound() &&
1806 13772 : GetAxesCount() == 3)
1807 : {
1808 80 : wktFormat = PJ_WKT2_2018;
1809 : }
1810 : }
1811 : else
1812 : {
1813 0 : CPLError(CE_Failure, CPLE_AppDefined, "Unsupported value for FORMAT");
1814 0 : *ppszResult = CPLStrdup("");
1815 0 : return OGRERR_FAILURE;
1816 : }
1817 :
1818 35000 : CPLStringList aosOptions;
1819 17500 : if (wktFormat != PJ_WKT1_ESRI)
1820 : {
1821 16828 : aosOptions.SetNameValue("OUTPUT_AXIS", "YES");
1822 : }
1823 : aosOptions.SetNameValue(
1824 17500 : "MULTILINE", CSLFetchNameValueDef(papszOptions, "MULTILINE", "NO"));
1825 :
1826 17500 : const char *pszAllowEllpsHeightAsVertCS = CSLFetchNameValue(
1827 : papszOptions, "ALLOW_ELLIPSOIDAL_HEIGHT_AS_VERTICAL_CRS");
1828 17500 : if (pszAllowEllpsHeightAsVertCS)
1829 : {
1830 : aosOptions.SetNameValue("ALLOW_ELLIPSOIDAL_HEIGHT_AS_VERTICAL_CRS",
1831 0 : pszAllowEllpsHeightAsVertCS);
1832 : }
1833 :
1834 17500 : PJ *boundCRS = nullptr;
1835 32549 : if (wktFormat == PJ_WKT1_GDAL &&
1836 15049 : CPLTestBool(CSLFetchNameValueDef(
1837 : papszOptions, "ADD_TOWGS84_ON_EXPORT_TO_WKT1",
1838 : CPLGetConfigOption("OSR_ADD_TOWGS84_ON_EXPORT_TO_WKT1", "NO"))))
1839 : {
1840 0 : boundCRS = GDAL_proj_crs_create_bound_crs_to_WGS84(
1841 0 : d->getPROJContext(), d->m_pj_crs, true, true);
1842 : }
1843 :
1844 35000 : CPLErrorAccumulator oErrorAccumulator;
1845 : const char *pszWKT;
1846 : {
1847 17500 : auto oAccumulator = oErrorAccumulator.InstallForCurrentScope();
1848 17500 : CPL_IGNORE_RET_VAL(oAccumulator);
1849 17500 : pszWKT = proj_as_wkt(ctxt, boundCRS ? boundCRS : d->m_pj_crs, wktFormat,
1850 17500 : aosOptions.List());
1851 : }
1852 17502 : for (const auto &oError : oErrorAccumulator.GetErrors())
1853 : {
1854 32 : if (pszFormat[0] == '\0' &&
1855 14 : (oError.msg.find("Unsupported conversion method") !=
1856 2 : std::string::npos ||
1857 2 : oError.msg.find("can only be exported to WKT2") !=
1858 0 : std::string::npos ||
1859 0 : oError.msg.find("can only be exported since WKT2:2019") !=
1860 : std::string::npos))
1861 : {
1862 14 : CPLErrorReset();
1863 : // If we cannot export in the default mode (WKT1), retry with WKT2
1864 14 : pszWKT = proj_as_wkt(ctxt, boundCRS ? boundCRS : d->m_pj_crs,
1865 14 : PJ_WKT2_2018, aosOptions.List());
1866 14 : break;
1867 : }
1868 2 : CPLError(oError.type, oError.no, "%s", oError.msg.c_str());
1869 : }
1870 :
1871 17500 : if (!pszWKT)
1872 : {
1873 2 : *ppszResult = CPLStrdup("");
1874 2 : proj_destroy(boundCRS);
1875 2 : return OGRERR_FAILURE;
1876 : }
1877 :
1878 17498 : if (EQUAL(pszFormat, "SFSQL") || EQUAL(pszFormat, "WKT1_SIMPLE"))
1879 : {
1880 5 : OGR_SRSNode oRoot;
1881 5 : oRoot.importFromWkt(&pszWKT);
1882 5 : oRoot.StripNodes("AXIS");
1883 5 : if (EQUAL(pszFormat, "SFSQL"))
1884 : {
1885 3 : oRoot.StripNodes("TOWGS84");
1886 : }
1887 5 : oRoot.StripNodes("AUTHORITY");
1888 5 : oRoot.StripNodes("EXTENSION");
1889 : OGRErr eErr;
1890 5 : if (CPLTestBool(CSLFetchNameValueDef(papszOptions, "MULTILINE", "NO")))
1891 2 : eErr = oRoot.exportToPrettyWkt(ppszResult, 1);
1892 : else
1893 3 : eErr = oRoot.exportToWkt(ppszResult);
1894 5 : proj_destroy(boundCRS);
1895 5 : return eErr;
1896 : }
1897 :
1898 17493 : *ppszResult = CPLStrdup(pszWKT);
1899 :
1900 : #if !(PROJ_AT_LEAST_VERSION(9, 5, 0))
1901 17493 : if (wktFormat == PJ_WKT2_2018)
1902 : {
1903 : // Works around bug fixed per https://github.com/OSGeo/PROJ/pull/4166
1904 : // related to a wrong EPSG code assigned to UTM South conversions
1905 1416 : char *pszPtr = strstr(*ppszResult, "CONVERSION[\"UTM zone ");
1906 1416 : if (pszPtr)
1907 : {
1908 348 : pszPtr += strlen("CONVERSION[\"UTM zone ");
1909 348 : const int nZone = atoi(pszPtr);
1910 1043 : while (*pszPtr >= '0' && *pszPtr <= '9')
1911 695 : ++pszPtr;
1912 348 : if (nZone >= 1 && nZone <= 60 && *pszPtr == 'S' &&
1913 1 : pszPtr[1] == '"' && pszPtr[2] == ',')
1914 : {
1915 1 : pszPtr += 3;
1916 1 : int nLevel = 0;
1917 1 : bool bInString = false;
1918 : // Find the ID node corresponding to this CONVERSION node
1919 480 : while (*pszPtr)
1920 : {
1921 480 : if (bInString)
1922 : {
1923 197 : if (*pszPtr == '"' && pszPtr[1] == '"')
1924 : {
1925 0 : ++pszPtr;
1926 : }
1927 197 : else if (*pszPtr == '"')
1928 : {
1929 17 : bInString = false;
1930 : }
1931 : }
1932 283 : else if (nLevel == 0 && STARTS_WITH_CI(pszPtr, "ID["))
1933 : {
1934 1 : if (STARTS_WITH_CI(pszPtr, CPLSPrintf("ID[\"EPSG\",%d]",
1935 : 17000 + nZone)))
1936 : {
1937 1 : CPLAssert(pszPtr[11] == '7');
1938 1 : CPLAssert(pszPtr[12] == '0');
1939 1 : pszPtr[11] = '6';
1940 1 : pszPtr[12] = '1';
1941 : }
1942 1 : break;
1943 : }
1944 282 : else if (*pszPtr == '"')
1945 : {
1946 17 : bInString = true;
1947 : }
1948 265 : else if (*pszPtr == '[')
1949 : {
1950 17 : ++nLevel;
1951 : }
1952 248 : else if (*pszPtr == ']')
1953 : {
1954 17 : --nLevel;
1955 : }
1956 :
1957 479 : ++pszPtr;
1958 : }
1959 : }
1960 : }
1961 : }
1962 : #endif
1963 :
1964 17493 : proj_destroy(boundCRS);
1965 17493 : return OGRERR_NONE;
1966 : }
1967 :
1968 : /************************************************************************/
1969 : /* exportToWkt() */
1970 : /************************************************************************/
1971 :
1972 : /**
1973 : * Convert this SRS into a WKT string.
1974 : *
1975 : * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
1976 : * Issues</a> page for implementation details of WKT 1 in OGR.
1977 : *
1978 : * @param papszOptions NULL terminated list of options, or NULL. Currently
1979 : * supported options are
1980 : * <ul>
1981 : * <li>MULTILINE=YES/NO. Defaults to NO.</li>
1982 : * <li>FORMAT=SFSQL/WKT1_SIMPLE/WKT1/WKT1_GDAL/WKT1_ESRI/WKT2_2015/WKT2_2018/WKT2/DEFAULT.
1983 : * If SFSQL, a WKT1 string without AXIS, TOWGS84, AUTHORITY or EXTENSION
1984 : * node is returned.
1985 : * If WKT1_SIMPLE, a WKT1 string without AXIS, AUTHORITY or EXTENSION
1986 : * node is returned.
1987 : * WKT1 is an alias of WKT1_GDAL.
1988 : * WKT2 will default to the latest revision implemented (currently
1989 : * WKT2_2019)
1990 : * </li>
1991 : * <li>ALLOW_ELLIPSOIDAL_HEIGHT_AS_VERTICAL_CRS=YES/NO. Default is NO. If set
1992 : * to YES and FORMAT=WKT1_GDAL, a Geographic 3D CRS or a Projected 3D CRS will
1993 : * be exported as a compound CRS whose vertical part represents an ellipsoidal
1994 : * height (for example for use with LAS 1.4 WKT1).
1995 : * Requires PROJ 7.2.1.</li>
1996 : * </ul>
1997 : *
1998 : * If the OSR_ADD_TOWGS84_ON_EXPORT_TO_WKT1
1999 : * configuration option is set to YES, when exporting to WKT1_GDAL, this method
2000 : * will try to add a TOWGS84[] node, if there's none attached yet to the SRS and
2001 : * if the SRS has a EPSG code. See the AddGuessedTOWGS84() method for how this
2002 : * TOWGS84[] node may be added.
2003 : *
2004 : * @return a non-empty string if successful.
2005 : * @since GDAL 3.9
2006 : */
2007 :
2008 : std::string
2009 804 : OGRSpatialReference::exportToWkt(const char *const *papszOptions) const
2010 : {
2011 804 : std::string osWKT;
2012 804 : char *pszWKT = nullptr;
2013 804 : if (exportToWkt(&pszWKT, papszOptions) == OGRERR_NONE)
2014 791 : osWKT = pszWKT;
2015 804 : CPLFree(pszWKT);
2016 1608 : return osWKT;
2017 : }
2018 :
2019 : /************************************************************************/
2020 : /* OSRExportToWkt() */
2021 : /************************************************************************/
2022 :
2023 : /**
2024 : * \brief Convert this SRS into WKT 1 format.
2025 : *
2026 : * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2027 : * Issues</a> page for implementation details of WKT in OGR.
2028 : *
2029 : * The WKT version can be overridden by using the OSR_WKT_FORMAT configuration
2030 : * option. Valid values are the one of the FORMAT option of
2031 : * exportToWkt( char ** ppszResult, const char* const* papszOptions ) const
2032 : *
2033 : * This function is the same as OGRSpatialReference::exportToWkt().
2034 : */
2035 :
2036 1075 : OGRErr CPL_STDCALL OSRExportToWkt(OGRSpatialReferenceH hSRS, char **ppszReturn)
2037 :
2038 : {
2039 1075 : VALIDATE_POINTER1(hSRS, "OSRExportToWkt", OGRERR_FAILURE);
2040 :
2041 1075 : *ppszReturn = nullptr;
2042 :
2043 1075 : return ToPointer(hSRS)->exportToWkt(ppszReturn);
2044 : }
2045 :
2046 : /************************************************************************/
2047 : /* OSRExportToWktEx() */
2048 : /************************************************************************/
2049 :
2050 : /**
2051 : * \brief Convert this SRS into WKT format.
2052 : *
2053 : * This function is the same as OGRSpatialReference::exportToWkt(char **
2054 : * ppszResult,const char* const* papszOptions ) const
2055 : *
2056 : * @since GDAL 3.0
2057 : */
2058 :
2059 1301 : OGRErr OSRExportToWktEx(OGRSpatialReferenceH hSRS, char **ppszReturn,
2060 : const char *const *papszOptions)
2061 : {
2062 1301 : VALIDATE_POINTER1(hSRS, "OSRExportToWktEx", OGRERR_FAILURE);
2063 :
2064 1301 : *ppszReturn = nullptr;
2065 :
2066 1301 : return ToPointer(hSRS)->exportToWkt(ppszReturn, papszOptions);
2067 : }
2068 :
2069 : /************************************************************************/
2070 : /* exportToPROJJSON() */
2071 : /************************************************************************/
2072 :
2073 : /**
2074 : * Convert this SRS into a PROJJSON string.
2075 : *
2076 : * Note that the returned JSON string should be freed with
2077 : * CPLFree() when no longer needed. It is the responsibility of the caller.
2078 : *
2079 : * @param ppszResult the resulting string is returned in this pointer.
2080 : * @param papszOptions NULL terminated list of options, or NULL. Currently
2081 : * supported options are
2082 : * <ul>
2083 : * <li>MULTILINE=YES/NO. Defaults to YES</li>
2084 : * <li>INDENTATION_WIDTH=number. Defaults to 2 (when multiline output is
2085 : * on).</li>
2086 : * <li>SCHEMA=string. URL to PROJJSON schema. Can be set to empty string to
2087 : * disable it.</li>
2088 : * </ul>
2089 : *
2090 : * @return OGRERR_NONE if successful.
2091 : * @since GDAL 3.1 and PROJ 6.2
2092 : */
2093 :
2094 2967 : OGRErr OGRSpatialReference::exportToPROJJSON(
2095 : char **ppszResult, CPL_UNUSED const char *const *papszOptions) const
2096 : {
2097 5934 : TAKE_OPTIONAL_LOCK();
2098 :
2099 2967 : d->refreshProjObj();
2100 2967 : if (!d->m_pj_crs)
2101 : {
2102 1 : *ppszResult = nullptr;
2103 1 : return OGRERR_FAILURE;
2104 : }
2105 :
2106 : const char *pszPROJJSON =
2107 2966 : proj_as_projjson(d->getPROJContext(), d->m_pj_crs, papszOptions);
2108 :
2109 2966 : if (!pszPROJJSON)
2110 : {
2111 0 : *ppszResult = CPLStrdup("");
2112 0 : return OGRERR_FAILURE;
2113 : }
2114 :
2115 2966 : *ppszResult = CPLStrdup(pszPROJJSON);
2116 :
2117 : #if !(PROJ_AT_LEAST_VERSION(9, 5, 0))
2118 : {
2119 : // Works around bug fixed per https://github.com/OSGeo/PROJ/pull/4166
2120 : // related to a wrong EPSG code assigned to UTM South conversions
2121 2966 : char *pszPtr = strstr(*ppszResult, "\"name\": \"UTM zone ");
2122 2966 : if (pszPtr)
2123 : {
2124 258 : pszPtr += strlen("\"name\": \"UTM zone ");
2125 258 : const int nZone = atoi(pszPtr);
2126 773 : while (*pszPtr >= '0' && *pszPtr <= '9')
2127 515 : ++pszPtr;
2128 258 : if (nZone >= 1 && nZone <= 60 && *pszPtr == 'S' && pszPtr[1] == '"')
2129 : {
2130 4 : pszPtr += 2;
2131 4 : int nLevel = 0;
2132 4 : bool bInString = false;
2133 : // Find the id node corresponding to this conversion node
2134 5299 : while (*pszPtr)
2135 : {
2136 5299 : if (bInString)
2137 : {
2138 1950 : if (*pszPtr == '\\')
2139 : {
2140 0 : ++pszPtr;
2141 : }
2142 1950 : else if (*pszPtr == '"')
2143 : {
2144 244 : bInString = false;
2145 : }
2146 : }
2147 3349 : else if (nLevel == 0 && STARTS_WITH(pszPtr, "\"id\": {"))
2148 : {
2149 4 : const char *pszNextEndCurl = strchr(pszPtr, '}');
2150 : const char *pszAuthEPSG =
2151 4 : strstr(pszPtr, "\"authority\": \"EPSG\"");
2152 4 : char *pszCode = strstr(
2153 : pszPtr, CPLSPrintf("\"code\": %d", 17000 + nZone));
2154 4 : if (pszAuthEPSG && pszCode && pszNextEndCurl &&
2155 4 : pszNextEndCurl - pszAuthEPSG > 0 &&
2156 4 : pszNextEndCurl - pszCode > 0)
2157 : {
2158 4 : CPLAssert(pszCode[9] == '7');
2159 4 : CPLAssert(pszCode[10] == '0');
2160 4 : pszCode[9] = '6';
2161 4 : pszCode[10] = '1';
2162 : }
2163 4 : break;
2164 : }
2165 3345 : else if (*pszPtr == '"')
2166 : {
2167 244 : bInString = true;
2168 : }
2169 3101 : else if (*pszPtr == '{' || *pszPtr == '[')
2170 : {
2171 60 : ++nLevel;
2172 : }
2173 3041 : else if (*pszPtr == '}' || *pszPtr == ']')
2174 : {
2175 60 : --nLevel;
2176 : }
2177 :
2178 5295 : ++pszPtr;
2179 : }
2180 : }
2181 : }
2182 : }
2183 : #endif
2184 :
2185 2966 : return OGRERR_NONE;
2186 : }
2187 :
2188 : /************************************************************************/
2189 : /* OSRExportToPROJJSON() */
2190 : /************************************************************************/
2191 :
2192 : /**
2193 : * \brief Convert this SRS into PROJJSON format.
2194 : *
2195 : * This function is the same as OGRSpatialReference::exportToPROJJSON() const
2196 : *
2197 : * @since GDAL 3.1 and PROJ 6.2
2198 : */
2199 :
2200 2 : OGRErr OSRExportToPROJJSON(OGRSpatialReferenceH hSRS, char **ppszReturn,
2201 : const char *const *papszOptions)
2202 : {
2203 2 : VALIDATE_POINTER1(hSRS, "OSRExportToPROJJSON", OGRERR_FAILURE);
2204 :
2205 2 : *ppszReturn = nullptr;
2206 :
2207 2 : return ToPointer(hSRS)->exportToPROJJSON(ppszReturn, papszOptions);
2208 : }
2209 :
2210 : /************************************************************************/
2211 : /* importFromWkt() */
2212 : /************************************************************************/
2213 :
2214 : /**
2215 : * \brief Import from WKT string.
2216 : *
2217 : * This method will wipe the existing SRS definition, and
2218 : * reassign it based on the contents of the passed WKT string. Only as
2219 : * much of the input string as needed to construct this SRS is consumed from
2220 : * the input string, and the input string pointer
2221 : * is then updated to point to the remaining (unused) input.
2222 : *
2223 : * Starting with PROJ 9.2, if invoked on a COORDINATEMETADATA[] construct,
2224 : * the CRS contained in it will be used to fill the OGRSpatialReference object,
2225 : * and the coordinate epoch potentially present used as the coordinate epoch
2226 : * property of the OGRSpatialReference object.
2227 : *
2228 : * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2229 : * Issues</a> page for implementation details of WKT in OGR.
2230 : *
2231 : * This method is the same as the C function OSRImportFromWkt().
2232 : *
2233 : * @param ppszInput Pointer to pointer to input. The pointer is updated to
2234 : * point to remaining unused input text.
2235 : *
2236 : * @return OGRERR_NONE if import succeeds, or OGRERR_CORRUPT_DATA if it
2237 : * fails for any reason.
2238 : */
2239 :
2240 21048 : OGRErr OGRSpatialReference::importFromWkt(const char **ppszInput)
2241 :
2242 : {
2243 21048 : return importFromWkt(ppszInput, nullptr);
2244 : }
2245 :
2246 : /************************************************************************/
2247 : /* importFromWkt() */
2248 : /************************************************************************/
2249 :
2250 : /*! @cond Doxygen_Suppress */
2251 :
2252 21 : OGRErr OGRSpatialReference::importFromWkt(const char *pszInput,
2253 : CSLConstList papszOptions)
2254 :
2255 : {
2256 21 : return importFromWkt(&pszInput, papszOptions);
2257 : }
2258 :
2259 21069 : OGRErr OGRSpatialReference::importFromWkt(const char **ppszInput,
2260 : CSLConstList papszOptions)
2261 :
2262 : {
2263 42138 : TAKE_OPTIONAL_LOCK();
2264 :
2265 21069 : if (!ppszInput || !*ppszInput)
2266 0 : return OGRERR_FAILURE;
2267 :
2268 21069 : if (strlen(*ppszInput) > 100 * 1000 &&
2269 0 : CPLTestBool(CPLGetConfigOption("OSR_IMPORT_FROM_WKT_LIMIT", "YES")))
2270 : {
2271 0 : CPLError(CE_Failure, CPLE_NotSupported,
2272 : "Suspiciously large input for importFromWkt(). Rejecting it. "
2273 : "You can remove this limitation by definition the "
2274 : "OSR_IMPORT_FROM_WKT_LIMIT configuration option to NO.");
2275 0 : return OGRERR_FAILURE;
2276 : }
2277 :
2278 21069 : Clear();
2279 :
2280 21069 : bool canCache = false;
2281 21069 : auto tlsCache = OSRGetProjTLSCache();
2282 42138 : std::string osWkt;
2283 21069 : if (**ppszInput)
2284 : {
2285 20514 : osWkt = *ppszInput;
2286 20514 : auto cachedObj = tlsCache->GetPJForWKT(osWkt);
2287 20514 : if (cachedObj)
2288 : {
2289 18504 : d->setPjCRS(cachedObj);
2290 : }
2291 : else
2292 : {
2293 4020 : CPLStringList aosOptions(papszOptions);
2294 2010 : if (aosOptions.FetchNameValue("STRICT") == nullptr)
2295 2010 : aosOptions.SetNameValue("STRICT", "NO");
2296 2010 : PROJ_STRING_LIST warnings = nullptr;
2297 2010 : PROJ_STRING_LIST errors = nullptr;
2298 2010 : auto ctxt = d->getPROJContext();
2299 2010 : auto pj = proj_create_from_wkt(ctxt, *ppszInput, aosOptions.List(),
2300 : &warnings, &errors);
2301 2010 : d->setPjCRS(pj);
2302 :
2303 2063 : for (auto iter = warnings; iter && *iter; ++iter)
2304 : {
2305 53 : d->m_wktImportWarnings.push_back(*iter);
2306 : }
2307 2247 : for (auto iter = errors; iter && *iter; ++iter)
2308 : {
2309 237 : d->m_wktImportErrors.push_back(*iter);
2310 237 : if (!d->m_pj_crs)
2311 : {
2312 34 : CPLError(CE_Failure, CPLE_AppDefined, "%s", *iter);
2313 : }
2314 : }
2315 2010 : if (warnings == nullptr && errors == nullptr)
2316 : {
2317 1729 : canCache = true;
2318 : }
2319 2010 : proj_string_list_destroy(warnings);
2320 2010 : proj_string_list_destroy(errors);
2321 : }
2322 : }
2323 21069 : if (!d->m_pj_crs)
2324 589 : return OGRERR_CORRUPT_DATA;
2325 :
2326 : // Only accept CRS objects
2327 20480 : if (!proj_is_crs(d->m_pj_crs))
2328 : {
2329 0 : Clear();
2330 0 : return OGRERR_CORRUPT_DATA;
2331 : }
2332 :
2333 20480 : if (canCache)
2334 : {
2335 1729 : tlsCache->CachePJForWKT(osWkt, d->m_pj_crs);
2336 : }
2337 :
2338 20480 : if (strstr(*ppszInput, "CENTER_LONG"))
2339 : {
2340 0 : auto poRoot = new OGR_SRSNode();
2341 0 : d->setRoot(poRoot);
2342 0 : const char *pszTmp = *ppszInput;
2343 0 : poRoot->importFromWkt(&pszTmp);
2344 0 : d->m_bHasCenterLong = true;
2345 : }
2346 :
2347 : // TODO? we don't really update correctly since we assume that the
2348 : // passed string is only WKT.
2349 20480 : *ppszInput += strlen(*ppszInput);
2350 20480 : return OGRERR_NONE;
2351 :
2352 : #if no_longer_implemented_for_now
2353 : /* -------------------------------------------------------------------- */
2354 : /* The following seems to try and detect and unconsumed */
2355 : /* VERTCS[] coordinate system definition (ESRI style) and to */
2356 : /* import and attach it to the existing root. Likely we will */
2357 : /* need to extend this somewhat to bring it into an acceptable */
2358 : /* OGRSpatialReference organization at some point. */
2359 : /* -------------------------------------------------------------------- */
2360 : if (strlen(*ppszInput) > 0 && strstr(*ppszInput, "VERTCS"))
2361 : {
2362 : if (((*ppszInput)[0]) == ',')
2363 : (*ppszInput)++;
2364 : OGR_SRSNode *poNewChild = new OGR_SRSNode();
2365 : poRoot->AddChild(poNewChild);
2366 : return poNewChild->importFromWkt(ppszInput);
2367 : }
2368 : #endif
2369 : }
2370 :
2371 : /*! @endcond */
2372 :
2373 : /**
2374 : * \brief Import from WKT string.
2375 : *
2376 : * This method will wipe the existing SRS definition, and
2377 : * reassign it based on the contents of the passed WKT string. Only as
2378 : * much of the input string as needed to construct this SRS is consumed from
2379 : * the input string, and the input string pointer
2380 : * is then updated to point to the remaining (unused) input.
2381 : *
2382 : * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2383 : * Issues</a> page for implementation details of WKT in OGR.
2384 : *
2385 : * This method is the same as the C function OSRImportFromWkt().
2386 : *
2387 : * @param ppszInput Pointer to pointer to input. The pointer is updated to
2388 : * point to remaining unused input text.
2389 : *
2390 : * @return OGRERR_NONE if import succeeds, or OGRERR_CORRUPT_DATA if it
2391 : * fails for any reason.
2392 : * @deprecated Use importFromWkt(const char**) or importFromWkt(const
2393 : * char*)
2394 : */
2395 :
2396 0 : OGRErr OGRSpatialReference::importFromWkt(char **ppszInput)
2397 :
2398 : {
2399 0 : return importFromWkt(const_cast<const char **>(ppszInput));
2400 : }
2401 :
2402 : /**
2403 : * \brief Import from WKT string.
2404 : *
2405 : * This method will wipe the existing SRS definition, and
2406 : * reassign it based on the contents of the passed WKT string. Only as
2407 : * much of the input string as needed to construct this SRS is consumed from
2408 : * the input string, and the input string pointer
2409 : * is then updated to point to the remaining (unused) input.
2410 : *
2411 : * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2412 : * Issues</a> page for implementation details of WKT in OGR.
2413 : *
2414 : * @param pszInput Input WKT
2415 : *
2416 : * @return OGRERR_NONE if import succeeds, or OGRERR_CORRUPT_DATA if it
2417 : * fails for any reason.
2418 : */
2419 :
2420 20785 : OGRErr OGRSpatialReference::importFromWkt(const char *pszInput)
2421 : {
2422 20785 : return importFromWkt(&pszInput);
2423 : }
2424 :
2425 : /************************************************************************/
2426 : /* Validate() */
2427 : /************************************************************************/
2428 :
2429 : /**
2430 : * \brief Validate CRS imported with importFromWkt() or with modified with
2431 : * direct node manipulations. Otherwise the CRS should be always valid.
2432 : *
2433 : * This method attempts to verify that the spatial reference system is
2434 : * well formed, and consists of known tokens. The validation is not
2435 : * comprehensive.
2436 : *
2437 : * This method is the same as the C function OSRValidate().
2438 : *
2439 : * @return OGRERR_NONE if all is fine, OGRERR_CORRUPT_DATA if the SRS is
2440 : * not well formed, and OGRERR_UNSUPPORTED_SRS if the SRS is well formed,
2441 : * but contains non-standard PROJECTION[] values.
2442 : */
2443 :
2444 116 : OGRErr OGRSpatialReference::Validate() const
2445 :
2446 : {
2447 232 : TAKE_OPTIONAL_LOCK();
2448 :
2449 154 : for (const auto &str : d->m_wktImportErrors)
2450 : {
2451 38 : CPLDebug("OGRSpatialReference::Validate", "%s", str.c_str());
2452 : }
2453 116 : for (const auto &str : d->m_wktImportWarnings)
2454 : {
2455 0 : CPLDebug("OGRSpatialReference::Validate", "%s", str.c_str());
2456 : }
2457 116 : if (!d->m_pj_crs || !d->m_wktImportErrors.empty())
2458 : {
2459 37 : return OGRERR_CORRUPT_DATA;
2460 : }
2461 79 : if (!d->m_wktImportWarnings.empty())
2462 : {
2463 0 : return OGRERR_UNSUPPORTED_SRS;
2464 : }
2465 79 : return OGRERR_NONE;
2466 : }
2467 :
2468 : /************************************************************************/
2469 : /* OSRValidate() */
2470 : /************************************************************************/
2471 : /**
2472 : * \brief Validate SRS tokens.
2473 : *
2474 : * This function is the same as the C++ method OGRSpatialReference::Validate().
2475 : */
2476 114 : OGRErr OSRValidate(OGRSpatialReferenceH hSRS)
2477 :
2478 : {
2479 114 : VALIDATE_POINTER1(hSRS, "OSRValidate", OGRERR_FAILURE);
2480 :
2481 114 : return OGRSpatialReference::FromHandle(hSRS)->Validate();
2482 : }
2483 :
2484 : /************************************************************************/
2485 : /* OSRImportFromWkt() */
2486 : /************************************************************************/
2487 :
2488 : /**
2489 : * \brief Import from WKT string.
2490 : *
2491 : * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2492 : * Issues</a> page for implementation details of WKT in OGR.
2493 : *
2494 : * This function is the same as OGRSpatialReference::importFromWkt().
2495 : */
2496 :
2497 263 : OGRErr OSRImportFromWkt(OGRSpatialReferenceH hSRS, char **ppszInput)
2498 :
2499 : {
2500 263 : VALIDATE_POINTER1(hSRS, "OSRImportFromWkt", OGRERR_FAILURE);
2501 :
2502 263 : return ToPointer(hSRS)->importFromWkt(const_cast<const char **>(ppszInput));
2503 : }
2504 :
2505 : /************************************************************************/
2506 : /* SetNode() */
2507 : /************************************************************************/
2508 :
2509 : /**
2510 : * \brief Set attribute value in spatial reference.
2511 : *
2512 : * Missing intermediate nodes in the path will be created if not already
2513 : * in existence. If the attribute has no children one will be created and
2514 : * assigned the value otherwise the zeroth child will be assigned the value.
2515 : *
2516 : * This method does the same as the C function OSRSetAttrValue().
2517 : *
2518 : * @param pszNodePath full path to attribute to be set. For instance
2519 : * "PROJCS|GEOGCS|UNIT".
2520 : *
2521 : * @param pszNewNodeValue value to be assigned to node, such as "meter".
2522 : * This may be NULL if you just want to force creation of the intermediate
2523 : * path.
2524 : *
2525 : * @return OGRERR_NONE on success.
2526 : */
2527 :
2528 584 : OGRErr OGRSpatialReference::SetNode(const char *pszNodePath,
2529 : const char *pszNewNodeValue)
2530 :
2531 : {
2532 1168 : TAKE_OPTIONAL_LOCK();
2533 :
2534 : char **papszPathTokens =
2535 584 : CSLTokenizeStringComplex(pszNodePath, "|", TRUE, FALSE);
2536 :
2537 584 : if (CSLCount(papszPathTokens) < 1)
2538 : {
2539 0 : CSLDestroy(papszPathTokens);
2540 0 : return OGRERR_FAILURE;
2541 : }
2542 :
2543 1019 : if (GetRoot() == nullptr ||
2544 435 : !EQUAL(papszPathTokens[0], GetRoot()->GetValue()))
2545 : {
2546 269 : if (EQUAL(papszPathTokens[0], "PROJCS") &&
2547 116 : CSLCount(papszPathTokens) == 1)
2548 : {
2549 116 : CSLDestroy(papszPathTokens);
2550 116 : return SetProjCS(pszNewNodeValue);
2551 : }
2552 : else
2553 : {
2554 37 : SetRoot(new OGR_SRSNode(papszPathTokens[0]));
2555 : }
2556 : }
2557 :
2558 468 : OGR_SRSNode *poNode = GetRoot();
2559 726 : for (int i = 1; papszPathTokens[i] != nullptr; i++)
2560 : {
2561 258 : int j = 0; // Used after for.
2562 :
2563 645 : for (; j < poNode->GetChildCount(); j++)
2564 : {
2565 585 : if (EQUAL(poNode->GetChild(j)->GetValue(), papszPathTokens[i]))
2566 : {
2567 198 : poNode = poNode->GetChild(j);
2568 198 : j = -1;
2569 198 : break;
2570 : }
2571 : }
2572 :
2573 258 : if (j != -1)
2574 : {
2575 60 : OGR_SRSNode *poNewNode = new OGR_SRSNode(papszPathTokens[i]);
2576 60 : poNode->AddChild(poNewNode);
2577 60 : poNode = poNewNode;
2578 : }
2579 : }
2580 :
2581 468 : CSLDestroy(papszPathTokens);
2582 :
2583 468 : if (pszNewNodeValue != nullptr)
2584 : {
2585 468 : if (poNode->GetChildCount() > 0)
2586 371 : poNode->GetChild(0)->SetValue(pszNewNodeValue);
2587 : else
2588 97 : poNode->AddChild(new OGR_SRSNode(pszNewNodeValue));
2589 : };
2590 468 : return OGRERR_NONE;
2591 : }
2592 :
2593 : /************************************************************************/
2594 : /* OSRSetAttrValue() */
2595 : /************************************************************************/
2596 :
2597 : /**
2598 : * \brief Set attribute value in spatial reference.
2599 : *
2600 : * This function is the same as OGRSpatialReference::SetNode()
2601 : */
2602 1 : OGRErr CPL_STDCALL OSRSetAttrValue(OGRSpatialReferenceH hSRS,
2603 : const char *pszPath, const char *pszValue)
2604 :
2605 : {
2606 1 : VALIDATE_POINTER1(hSRS, "OSRSetAttrValue", OGRERR_FAILURE);
2607 :
2608 1 : return ToPointer(hSRS)->SetNode(pszPath, pszValue);
2609 : }
2610 :
2611 : /************************************************************************/
2612 : /* SetNode() */
2613 : /************************************************************************/
2614 :
2615 : /**
2616 : * \brief Set attribute value in spatial reference.
2617 : *
2618 : * Missing intermediate nodes in the path will be created if not already
2619 : * in existence. If the attribute has no children one will be created and
2620 : * assigned the value otherwise the zeroth child will be assigned the value.
2621 : *
2622 : * This method does the same as the C function OSRSetAttrValue().
2623 : *
2624 : * @param pszNodePath full path to attribute to be set. For instance
2625 : * "PROJCS|GEOGCS|UNIT".
2626 : *
2627 : * @param dfValue value to be assigned to node.
2628 : *
2629 : * @return OGRERR_NONE on success.
2630 : */
2631 :
2632 0 : OGRErr OGRSpatialReference::SetNode(const char *pszNodePath, double dfValue)
2633 :
2634 : {
2635 0 : char szValue[64] = {'\0'};
2636 :
2637 0 : if (std::abs(dfValue - static_cast<int>(dfValue)) == 0.0)
2638 0 : snprintf(szValue, sizeof(szValue), "%d", static_cast<int>(dfValue));
2639 : else
2640 0 : OGRsnPrintDouble(szValue, sizeof(szValue), dfValue);
2641 :
2642 0 : return SetNode(pszNodePath, szValue);
2643 : }
2644 :
2645 : /************************************************************************/
2646 : /* SetAngularUnits() */
2647 : /************************************************************************/
2648 :
2649 : /**
2650 : * \brief Set the angular units for the geographic coordinate system.
2651 : *
2652 : * This method creates a UNIT subnode with the specified values as a
2653 : * child of the GEOGCS node.
2654 : *
2655 : * This method does the same as the C function OSRSetAngularUnits().
2656 : *
2657 : * @param pszUnitsName the units name to be used. Some preferred units
2658 : * names can be found in ogr_srs_api.h such as SRS_UA_DEGREE.
2659 : *
2660 : * @param dfInRadians the value to multiple by an angle in the indicated
2661 : * units to transform to radians. Some standard conversion factors can
2662 : * be found in ogr_srs_api.h.
2663 : *
2664 : * @return OGRERR_NONE on success.
2665 : */
2666 :
2667 1598 : OGRErr OGRSpatialReference::SetAngularUnits(const char *pszUnitsName,
2668 : double dfInRadians)
2669 :
2670 : {
2671 3196 : TAKE_OPTIONAL_LOCK();
2672 :
2673 1598 : d->bNormInfoSet = FALSE;
2674 :
2675 1598 : d->refreshProjObj();
2676 1598 : if (!d->m_pj_crs)
2677 0 : return OGRERR_FAILURE;
2678 1598 : auto geodCRS = proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
2679 1598 : if (!geodCRS)
2680 0 : return OGRERR_FAILURE;
2681 1598 : proj_destroy(geodCRS);
2682 1598 : d->demoteFromBoundCRS();
2683 1598 : d->setPjCRS(proj_crs_alter_cs_angular_unit(d->getPROJContext(), d->m_pj_crs,
2684 : pszUnitsName, dfInRadians,
2685 : nullptr, nullptr));
2686 1598 : d->undoDemoteFromBoundCRS();
2687 :
2688 1598 : d->m_osAngularUnits = pszUnitsName;
2689 1598 : d->m_dfAngularUnitToRadian = dfInRadians;
2690 :
2691 1598 : return OGRERR_NONE;
2692 : }
2693 :
2694 : /************************************************************************/
2695 : /* OSRSetAngularUnits() */
2696 : /************************************************************************/
2697 :
2698 : /**
2699 : * \brief Set the angular units for the geographic coordinate system.
2700 : *
2701 : * This function is the same as OGRSpatialReference::SetAngularUnits()
2702 : */
2703 48 : OGRErr OSRSetAngularUnits(OGRSpatialReferenceH hSRS, const char *pszUnits,
2704 : double dfInRadians)
2705 :
2706 : {
2707 48 : VALIDATE_POINTER1(hSRS, "OSRSetAngularUnits", OGRERR_FAILURE);
2708 :
2709 48 : return ToPointer(hSRS)->SetAngularUnits(pszUnits, dfInRadians);
2710 : }
2711 :
2712 : /************************************************************************/
2713 : /* GetAngularUnits() */
2714 : /************************************************************************/
2715 :
2716 : /**
2717 : * \brief Fetch angular geographic coordinate system units.
2718 : *
2719 : * If no units are available, a value of "degree" and SRS_UA_DEGREE_CONV
2720 : * will be assumed. This method only checks directly under the GEOGCS node
2721 : * for units.
2722 : *
2723 : * This method does the same thing as the C function OSRGetAngularUnits().
2724 : *
2725 : * @param ppszName a pointer to be updated with the pointer to the units name.
2726 : * The returned value remains internal to the OGRSpatialReference and should
2727 : * not be freed, or modified. It may be invalidated on the next
2728 : * OGRSpatialReference call.
2729 : *
2730 : * @return the value to multiply by angular distances to transform them to
2731 : * radians.
2732 : */
2733 :
2734 8821 : double OGRSpatialReference::GetAngularUnits(const char **ppszName) const
2735 :
2736 : {
2737 17642 : TAKE_OPTIONAL_LOCK();
2738 :
2739 8821 : d->refreshProjObj();
2740 :
2741 8821 : if (!d->m_osAngularUnits.empty())
2742 : {
2743 2428 : if (ppszName != nullptr)
2744 184 : *ppszName = d->m_osAngularUnits.c_str();
2745 2428 : return d->m_dfAngularUnitToRadian;
2746 : }
2747 :
2748 : do
2749 : {
2750 6393 : if (d->m_pj_crs == nullptr || d->m_pjType == PJ_TYPE_ENGINEERING_CRS)
2751 : {
2752 113 : break;
2753 : }
2754 :
2755 : auto geodCRS =
2756 6282 : proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
2757 6282 : if (!geodCRS)
2758 : {
2759 0 : break;
2760 : }
2761 : auto coordSys =
2762 6282 : proj_crs_get_coordinate_system(d->getPROJContext(), geodCRS);
2763 6282 : proj_destroy(geodCRS);
2764 6282 : if (!coordSys)
2765 : {
2766 0 : break;
2767 : }
2768 6282 : if (proj_cs_get_type(d->getPROJContext(), coordSys) !=
2769 : PJ_CS_TYPE_ELLIPSOIDAL)
2770 : {
2771 2 : proj_destroy(coordSys);
2772 2 : break;
2773 : }
2774 :
2775 6280 : double dfConvFactor = 0.0;
2776 6280 : const char *pszUnitName = nullptr;
2777 6280 : if (!proj_cs_get_axis_info(d->getPROJContext(), coordSys, 0, nullptr,
2778 : nullptr, nullptr, &dfConvFactor,
2779 : &pszUnitName, nullptr, nullptr))
2780 : {
2781 0 : proj_destroy(coordSys);
2782 0 : break;
2783 : }
2784 :
2785 6280 : d->m_osAngularUnits = pszUnitName;
2786 :
2787 6280 : proj_destroy(coordSys);
2788 6280 : d->m_dfAngularUnitToRadian = dfConvFactor;
2789 : } while (false);
2790 :
2791 6393 : if (d->m_osAngularUnits.empty())
2792 : {
2793 113 : d->m_osAngularUnits = "degree";
2794 113 : d->m_dfAngularUnitToRadian = CPLAtof(SRS_UA_DEGREE_CONV);
2795 : }
2796 :
2797 6393 : if (ppszName != nullptr)
2798 3270 : *ppszName = d->m_osAngularUnits.c_str();
2799 6393 : return d->m_dfAngularUnitToRadian;
2800 : }
2801 :
2802 : /**
2803 : * \brief Fetch angular geographic coordinate system units.
2804 : *
2805 : * If no units are available, a value of "degree" and SRS_UA_DEGREE_CONV
2806 : * will be assumed. This method only checks directly under the GEOGCS node
2807 : * for units.
2808 : *
2809 : * This method does the same thing as the C function OSRGetAngularUnits().
2810 : *
2811 : * @param ppszName a pointer to be updated with the pointer to the units name.
2812 : * The returned value remains internal to the OGRSpatialReference and should
2813 : * not be freed, or modified. It may be invalidated on the next
2814 : * OGRSpatialReference call.
2815 : *
2816 : * @return the value to multiply by angular distances to transform them to
2817 : * radians.
2818 : * @deprecated Use GetAngularUnits(const char**) const.
2819 : */
2820 :
2821 0 : double OGRSpatialReference::GetAngularUnits(char **ppszName) const
2822 :
2823 : {
2824 0 : return GetAngularUnits(const_cast<const char **>(ppszName));
2825 : }
2826 :
2827 : /************************************************************************/
2828 : /* OSRGetAngularUnits() */
2829 : /************************************************************************/
2830 :
2831 : /**
2832 : * \brief Fetch angular geographic coordinate system units.
2833 : *
2834 : * This function is the same as OGRSpatialReference::GetAngularUnits()
2835 : */
2836 1 : double OSRGetAngularUnits(OGRSpatialReferenceH hSRS, char **ppszName)
2837 :
2838 : {
2839 1 : VALIDATE_POINTER1(hSRS, "OSRGetAngularUnits", 0);
2840 :
2841 1 : return ToPointer(hSRS)->GetAngularUnits(
2842 1 : const_cast<const char **>(ppszName));
2843 : }
2844 :
2845 : /************************************************************************/
2846 : /* SetLinearUnitsAndUpdateParameters() */
2847 : /************************************************************************/
2848 :
2849 : /**
2850 : * \brief Set the linear units for the projection.
2851 : *
2852 : * This method creates a UNIT subnode with the specified values as a
2853 : * child of the PROJCS or LOCAL_CS node. It works the same as the
2854 : * SetLinearUnits() method, but it also updates all existing linear
2855 : * projection parameter values from the old units to the new units.
2856 : *
2857 : * @param pszName the units name to be used. Some preferred units
2858 : * names can be found in ogr_srs_api.h such as SRS_UL_METER, SRS_UL_FOOT
2859 : * and SRS_UL_US_FOOT.
2860 : *
2861 : * @param dfInMeters the value to multiple by a length in the indicated
2862 : * units to transform to meters. Some standard conversion factors can
2863 : * be found in ogr_srs_api.h.
2864 : *
2865 : * @param pszUnitAuthority Unit authority name. Or nullptr
2866 : *
2867 : * @param pszUnitCode Unit code. Or nullptr
2868 : *
2869 : * @return OGRERR_NONE on success.
2870 : */
2871 :
2872 38 : OGRErr OGRSpatialReference::SetLinearUnitsAndUpdateParameters(
2873 : const char *pszName, double dfInMeters, const char *pszUnitAuthority,
2874 : const char *pszUnitCode)
2875 :
2876 : {
2877 76 : TAKE_OPTIONAL_LOCK();
2878 :
2879 38 : if (dfInMeters <= 0.0)
2880 0 : return OGRERR_FAILURE;
2881 :
2882 38 : d->refreshProjObj();
2883 38 : if (!d->m_pj_crs)
2884 0 : return OGRERR_FAILURE;
2885 :
2886 38 : d->demoteFromBoundCRS();
2887 38 : if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
2888 : {
2889 76 : d->setPjCRS(proj_crs_alter_parameters_linear_unit(
2890 38 : d->getPROJContext(), d->m_pj_crs, pszName, dfInMeters,
2891 : pszUnitAuthority, pszUnitCode, true));
2892 : }
2893 38 : d->setPjCRS(proj_crs_alter_cs_linear_unit(d->getPROJContext(), d->m_pj_crs,
2894 : pszName, dfInMeters,
2895 : pszUnitAuthority, pszUnitCode));
2896 38 : d->undoDemoteFromBoundCRS();
2897 :
2898 38 : d->m_osLinearUnits = pszName;
2899 38 : d->dfToMeter = dfInMeters;
2900 :
2901 38 : return OGRERR_NONE;
2902 : }
2903 :
2904 : /************************************************************************/
2905 : /* OSRSetLinearUnitsAndUpdateParameters() */
2906 : /************************************************************************/
2907 :
2908 : /**
2909 : * \brief Set the linear units for the projection.
2910 : *
2911 : * This function is the same as
2912 : * OGRSpatialReference::SetLinearUnitsAndUpdateParameters()
2913 : */
2914 1 : OGRErr OSRSetLinearUnitsAndUpdateParameters(OGRSpatialReferenceH hSRS,
2915 : const char *pszUnits,
2916 : double dfInMeters)
2917 :
2918 : {
2919 1 : VALIDATE_POINTER1(hSRS, "OSRSetLinearUnitsAndUpdateParameters",
2920 : OGRERR_FAILURE);
2921 :
2922 1 : return ToPointer(hSRS)->SetLinearUnitsAndUpdateParameters(pszUnits,
2923 1 : dfInMeters);
2924 : }
2925 :
2926 : /************************************************************************/
2927 : /* SetLinearUnits() */
2928 : /************************************************************************/
2929 :
2930 : /**
2931 : * \brief Set the linear units for the projection.
2932 : *
2933 : * This method creates a UNIT subnode with the specified values as a
2934 : * child of the PROJCS, GEOCCS, GEOGCS or LOCAL_CS node. When called on a
2935 : * Geographic 3D CRS the vertical axis units will be set.
2936 : *
2937 : * This method does the same as the C function OSRSetLinearUnits().
2938 : *
2939 : * @param pszUnitsName the units name to be used. Some preferred units
2940 : * names can be found in ogr_srs_api.h such as SRS_UL_METER, SRS_UL_FOOT
2941 : * and SRS_UL_US_FOOT.
2942 : *
2943 : * @param dfInMeters the value to multiple by a length in the indicated
2944 : * units to transform to meters. Some standard conversion factors can
2945 : * be found in ogr_srs_api.h.
2946 : *
2947 : * @return OGRERR_NONE on success.
2948 : */
2949 :
2950 7478 : OGRErr OGRSpatialReference::SetLinearUnits(const char *pszUnitsName,
2951 : double dfInMeters)
2952 :
2953 : {
2954 7478 : return SetTargetLinearUnits(nullptr, pszUnitsName, dfInMeters);
2955 : }
2956 :
2957 : /************************************************************************/
2958 : /* OSRSetLinearUnits() */
2959 : /************************************************************************/
2960 :
2961 : /**
2962 : * \brief Set the linear units for the projection.
2963 : *
2964 : * This function is the same as OGRSpatialReference::SetLinearUnits()
2965 : */
2966 7 : OGRErr OSRSetLinearUnits(OGRSpatialReferenceH hSRS, const char *pszUnits,
2967 : double dfInMeters)
2968 :
2969 : {
2970 7 : VALIDATE_POINTER1(hSRS, "OSRSetLinearUnits", OGRERR_FAILURE);
2971 :
2972 7 : return ToPointer(hSRS)->SetLinearUnits(pszUnits, dfInMeters);
2973 : }
2974 :
2975 : /************************************************************************/
2976 : /* SetTargetLinearUnits() */
2977 : /************************************************************************/
2978 :
2979 : /**
2980 : * \brief Set the linear units for the projection.
2981 : *
2982 : * This method creates a UNIT subnode with the specified values as a
2983 : * child of the target node.
2984 : *
2985 : * This method does the same as the C function OSRSetTargetLinearUnits().
2986 : *
2987 : * @param pszTargetKey the keyword to set the linear units for.
2988 : * i.e. "PROJCS" or "VERT_CS"
2989 : *
2990 : * @param pszUnitsName the units name to be used. Some preferred units
2991 : * names can be found in ogr_srs_api.h such as SRS_UL_METER, SRS_UL_FOOT
2992 : * and SRS_UL_US_FOOT.
2993 : *
2994 : * @param dfInMeters the value to multiple by a length in the indicated
2995 : * units to transform to meters. Some standard conversion factors can
2996 : * be found in ogr_srs_api.h.
2997 : *
2998 : * @param pszUnitAuthority Unit authority name. Or nullptr
2999 : *
3000 : * @param pszUnitCode Unit code. Or nullptr
3001 : *
3002 : * @return OGRERR_NONE on success.
3003 : *
3004 : */
3005 :
3006 12004 : OGRErr OGRSpatialReference::SetTargetLinearUnits(const char *pszTargetKey,
3007 : const char *pszUnitsName,
3008 : double dfInMeters,
3009 : const char *pszUnitAuthority,
3010 : const char *pszUnitCode)
3011 :
3012 : {
3013 24008 : TAKE_OPTIONAL_LOCK();
3014 :
3015 12004 : if (dfInMeters <= 0.0)
3016 0 : return OGRERR_FAILURE;
3017 :
3018 12004 : d->refreshProjObj();
3019 12004 : pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
3020 12004 : if (pszTargetKey == nullptr)
3021 : {
3022 12004 : if (!d->m_pj_crs)
3023 0 : return OGRERR_FAILURE;
3024 :
3025 12004 : d->demoteFromBoundCRS();
3026 12004 : if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
3027 : {
3028 18206 : d->setPjCRS(proj_crs_alter_parameters_linear_unit(
3029 9103 : d->getPROJContext(), d->m_pj_crs, pszUnitsName, dfInMeters,
3030 : pszUnitAuthority, pszUnitCode, false));
3031 : }
3032 24008 : d->setPjCRS(proj_crs_alter_cs_linear_unit(
3033 12004 : d->getPROJContext(), d->m_pj_crs, pszUnitsName, dfInMeters,
3034 : pszUnitAuthority, pszUnitCode));
3035 12004 : d->undoDemoteFromBoundCRS();
3036 :
3037 12004 : d->m_osLinearUnits = pszUnitsName;
3038 12004 : d->dfToMeter = dfInMeters;
3039 :
3040 12004 : 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 : */
3089 1 : OGRErr OSRSetTargetLinearUnits(OGRSpatialReferenceH hSRS,
3090 : const char *pszTargetKey, const char *pszUnits,
3091 : double dfInMeters)
3092 :
3093 : {
3094 1 : VALIDATE_POINTER1(hSRS, "OSRSetTargetLinearUnits", OGRERR_FAILURE);
3095 :
3096 1 : return ToPointer(hSRS)->SetTargetLinearUnits(pszTargetKey, pszUnits,
3097 1 : dfInMeters);
3098 : }
3099 :
3100 : /************************************************************************/
3101 : /* GetLinearUnits() */
3102 : /************************************************************************/
3103 :
3104 : /**
3105 : * \brief Fetch linear projection units.
3106 : *
3107 : * If no units are available, a value of "Meters" and 1.0 will be assumed.
3108 : * This method only checks directly under the PROJCS, GEOCCS, GEOGCS or
3109 : * LOCAL_CS node for units. When called on a Geographic 3D CRS the vertical
3110 : * axis units will be returned.
3111 : *
3112 : * This method does the same thing as the C function OSRGetLinearUnits()
3113 : *
3114 : * @param ppszName a pointer to be updated with the pointer to the units name.
3115 : * The returned value remains internal to the OGRSpatialReference and should
3116 : * not be freed, or modified. It may be invalidated on the next
3117 : * OGRSpatialReference call.
3118 : *
3119 : * @return the value to multiply by linear distances to transform them to
3120 : * meters.
3121 : * @deprecated Use GetLinearUnits(const char**) const.
3122 : */
3123 :
3124 0 : double OGRSpatialReference::GetLinearUnits(char **ppszName) const
3125 :
3126 : {
3127 0 : return GetTargetLinearUnits(nullptr, const_cast<const char **>(ppszName));
3128 : }
3129 :
3130 : /**
3131 : * \brief Fetch linear projection units.
3132 : *
3133 : * If no units are available, a value of "Meters" and 1.0 will be assumed.
3134 : * This method only checks directly under the PROJCS, GEOCCS or LOCAL_CS node
3135 : * for units.
3136 : *
3137 : * This method does the same thing as the C function OSRGetLinearUnits()
3138 : *
3139 : * @param ppszName a pointer to be updated with the pointer to the units name.
3140 : * The returned value remains internal to the OGRSpatialReference and should
3141 : * not be freed, or modified. It may be invalidated on the next
3142 : * OGRSpatialReference call.
3143 : *
3144 : * @return the value to multiply by linear distances to transform them to
3145 : * meters.
3146 : */
3147 :
3148 21833 : double OGRSpatialReference::GetLinearUnits(const char **ppszName) const
3149 :
3150 : {
3151 21833 : return GetTargetLinearUnits(nullptr, ppszName);
3152 : }
3153 :
3154 : /************************************************************************/
3155 : /* OSRGetLinearUnits() */
3156 : /************************************************************************/
3157 :
3158 : /**
3159 : * \brief Fetch linear projection units.
3160 : *
3161 : * This function is the same as OGRSpatialReference::GetLinearUnits()
3162 : */
3163 255 : double OSRGetLinearUnits(OGRSpatialReferenceH hSRS, char **ppszName)
3164 :
3165 : {
3166 255 : VALIDATE_POINTER1(hSRS, "OSRGetLinearUnits", 0);
3167 :
3168 255 : return ToPointer(hSRS)->GetLinearUnits(const_cast<const char **>(ppszName));
3169 : }
3170 :
3171 : /************************************************************************/
3172 : /* GetTargetLinearUnits() */
3173 : /************************************************************************/
3174 :
3175 : /**
3176 : * \brief Fetch linear units for target.
3177 : *
3178 : * If no units are available, a value of "Meters" and 1.0 will be assumed.
3179 : *
3180 : * This method does the same thing as the C function OSRGetTargetLinearUnits()
3181 : *
3182 : * @param pszTargetKey the key to look on. i.e. "PROJCS" or "VERT_CS". Might be
3183 : * NULL, in which case PROJCS will be implied (and if not found, LOCAL_CS,
3184 : * GEOCCS, GEOGCS and VERT_CS are looked up)
3185 : * @param ppszName a pointer to be updated with the pointer to the units name.
3186 : * The returned value remains internal to the OGRSpatialReference and should not
3187 : * be freed, or modified. It may be invalidated on the next
3188 : * OGRSpatialReference call. ppszName can be set to NULL.
3189 : *
3190 : * @return the value to multiply by linear distances to transform them to
3191 : * meters.
3192 : *
3193 : * @deprecated Use GetTargetLinearUnits(const char*, const char**)
3194 : * const.
3195 : */
3196 :
3197 21985 : double OGRSpatialReference::GetTargetLinearUnits(const char *pszTargetKey,
3198 : const char **ppszName) const
3199 :
3200 : {
3201 43970 : TAKE_OPTIONAL_LOCK();
3202 :
3203 21985 : d->refreshProjObj();
3204 :
3205 21985 : pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
3206 21985 : if (pszTargetKey == nullptr)
3207 : {
3208 : // Use cached result if available
3209 21893 : if (!d->m_osLinearUnits.empty())
3210 : {
3211 9276 : if (ppszName)
3212 8398 : *ppszName = d->m_osLinearUnits.c_str();
3213 9276 : return d->dfToMeter;
3214 : }
3215 :
3216 : while (true)
3217 : {
3218 12617 : if (d->m_pj_crs == nullptr)
3219 : {
3220 242 : break;
3221 : }
3222 :
3223 12375 : d->demoteFromBoundCRS();
3224 12375 : PJ *coordSys = nullptr;
3225 12375 : if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
3226 : {
3227 37 : for (int iComponent = 0; iComponent < 2; iComponent++)
3228 : {
3229 37 : auto subCRS = proj_crs_get_sub_crs(d->getPROJContext(),
3230 37 : d->m_pj_crs, iComponent);
3231 37 : if (subCRS && proj_get_type(subCRS) == PJ_TYPE_BOUND_CRS)
3232 : {
3233 : auto temp =
3234 0 : proj_get_source_crs(d->getPROJContext(), subCRS);
3235 0 : proj_destroy(subCRS);
3236 0 : subCRS = temp;
3237 : }
3238 74 : if (subCRS &&
3239 37 : (proj_get_type(subCRS) == PJ_TYPE_PROJECTED_CRS ||
3240 16 : proj_get_type(subCRS) == PJ_TYPE_ENGINEERING_CRS ||
3241 12 : proj_get_type(subCRS) == PJ_TYPE_VERTICAL_CRS))
3242 : {
3243 31 : coordSys = proj_crs_get_coordinate_system(
3244 : d->getPROJContext(), subCRS);
3245 31 : proj_destroy(subCRS);
3246 31 : break;
3247 : }
3248 6 : else if (subCRS)
3249 : {
3250 6 : proj_destroy(subCRS);
3251 : }
3252 : }
3253 31 : if (coordSys == nullptr)
3254 : {
3255 0 : d->undoDemoteFromBoundCRS();
3256 0 : break;
3257 : }
3258 : }
3259 : else
3260 : {
3261 12344 : coordSys = proj_crs_get_coordinate_system(d->getPROJContext(),
3262 12344 : d->m_pj_crs);
3263 : }
3264 :
3265 12375 : d->undoDemoteFromBoundCRS();
3266 12375 : if (!coordSys)
3267 : {
3268 0 : break;
3269 : }
3270 12375 : auto csType = proj_cs_get_type(d->getPROJContext(), coordSys);
3271 :
3272 12375 : if (csType != PJ_CS_TYPE_CARTESIAN &&
3273 2277 : csType != PJ_CS_TYPE_VERTICAL &&
3274 0 : csType != PJ_CS_TYPE_ELLIPSOIDAL &&
3275 : csType != PJ_CS_TYPE_SPHERICAL)
3276 : {
3277 0 : proj_destroy(coordSys);
3278 0 : break;
3279 : }
3280 :
3281 12375 : int axis = 0;
3282 :
3283 12375 : if (csType == PJ_CS_TYPE_ELLIPSOIDAL ||
3284 : csType == PJ_CS_TYPE_SPHERICAL)
3285 : {
3286 : const int axisCount =
3287 2277 : proj_cs_get_axis_count(d->getPROJContext(), coordSys);
3288 :
3289 2277 : if (axisCount == 3)
3290 : {
3291 4 : axis = 2;
3292 : }
3293 : else
3294 : {
3295 2273 : proj_destroy(coordSys);
3296 2273 : break;
3297 : }
3298 : }
3299 :
3300 10102 : double dfConvFactor = 0.0;
3301 10102 : const char *pszUnitName = nullptr;
3302 10102 : if (!proj_cs_get_axis_info(d->getPROJContext(), coordSys, axis,
3303 : nullptr, nullptr, nullptr, &dfConvFactor,
3304 : &pszUnitName, nullptr, nullptr))
3305 : {
3306 0 : proj_destroy(coordSys);
3307 0 : break;
3308 : }
3309 :
3310 10102 : d->m_osLinearUnits = pszUnitName;
3311 10102 : d->dfToMeter = dfConvFactor;
3312 10102 : if (ppszName)
3313 1302 : *ppszName = d->m_osLinearUnits.c_str();
3314 :
3315 10102 : proj_destroy(coordSys);
3316 10102 : return dfConvFactor;
3317 : }
3318 :
3319 2515 : d->m_osLinearUnits = "unknown";
3320 2515 : d->dfToMeter = 1.0;
3321 :
3322 2515 : if (ppszName != nullptr)
3323 2329 : *ppszName = d->m_osLinearUnits.c_str();
3324 2515 : return 1.0;
3325 : }
3326 :
3327 92 : const OGR_SRSNode *poCS = GetAttrNode(pszTargetKey);
3328 :
3329 92 : if (ppszName != nullptr)
3330 38 : *ppszName = "unknown";
3331 :
3332 92 : if (poCS == nullptr)
3333 53 : return 1.0;
3334 :
3335 117 : for (int iChild = 0; iChild < poCS->GetChildCount(); iChild++)
3336 : {
3337 117 : const OGR_SRSNode *poChild = poCS->GetChild(iChild);
3338 :
3339 117 : if (EQUAL(poChild->GetValue(), "UNIT") && poChild->GetChildCount() >= 2)
3340 : {
3341 39 : if (ppszName != nullptr)
3342 38 : *ppszName = poChild->GetChild(0)->GetValue();
3343 :
3344 39 : return CPLAtof(poChild->GetChild(1)->GetValue());
3345 : }
3346 : }
3347 :
3348 0 : return 1.0;
3349 : }
3350 :
3351 : /**
3352 : * \brief Fetch linear units for target.
3353 : *
3354 : * If no units are available, a value of "Meters" and 1.0 will be assumed.
3355 : *
3356 : * This method does the same thing as the C function OSRGetTargetLinearUnits()
3357 : *
3358 : * @param pszTargetKey the key to look on. i.e. "PROJCS" or "VERT_CS". Might be
3359 : * NULL, in which case PROJCS will be implied (and if not found, LOCAL_CS,
3360 : * GEOCCS and VERT_CS are looked up)
3361 : * @param ppszName a pointer to be updated with the pointer to the units name.
3362 : * The returned value remains internal to the OGRSpatialReference and should not
3363 : * be freed, or modified. It may be invalidated on the next
3364 : * OGRSpatialReference call. ppszName can be set to NULL.
3365 : *
3366 : * @return the value to multiply by linear distances to transform them to
3367 : * meters.
3368 : *
3369 : */
3370 :
3371 0 : double OGRSpatialReference::GetTargetLinearUnits(const char *pszTargetKey,
3372 : char **ppszName) const
3373 :
3374 : {
3375 0 : return GetTargetLinearUnits(pszTargetKey,
3376 0 : const_cast<const char **>(ppszName));
3377 : }
3378 :
3379 : /************************************************************************/
3380 : /* OSRGetTargetLinearUnits() */
3381 : /************************************************************************/
3382 :
3383 : /**
3384 : * \brief Fetch linear projection units.
3385 : *
3386 : * This function is the same as OGRSpatialReference::GetTargetLinearUnits()
3387 : *
3388 : */
3389 4 : double OSRGetTargetLinearUnits(OGRSpatialReferenceH hSRS,
3390 : const char *pszTargetKey, char **ppszName)
3391 :
3392 : {
3393 4 : VALIDATE_POINTER1(hSRS, "OSRGetTargetLinearUnits", 0);
3394 :
3395 4 : return ToPointer(hSRS)->GetTargetLinearUnits(
3396 4 : pszTargetKey, const_cast<const char **>(ppszName));
3397 : }
3398 :
3399 : /************************************************************************/
3400 : /* GetPrimeMeridian() */
3401 : /************************************************************************/
3402 :
3403 : /**
3404 : * \brief Fetch prime meridian info.
3405 : *
3406 : * Returns the offset of the prime meridian from greenwich in degrees,
3407 : * and the prime meridian name (if requested). If no PRIMEM value exists
3408 : * in the coordinate system definition a value of "Greenwich" and an
3409 : * offset of 0.0 is assumed.
3410 : *
3411 : * If the prime meridian name is returned, the pointer is to an internal
3412 : * copy of the name. It should not be freed, altered or depended on after
3413 : * the next OGR call.
3414 : *
3415 : * This method is the same as the C function OSRGetPrimeMeridian().
3416 : *
3417 : * @param ppszName return location for prime meridian name. If NULL, name
3418 : * is not returned.
3419 : *
3420 : * @return the offset to the GEOGCS prime meridian from greenwich in decimal
3421 : * degrees.
3422 : * @deprecated Use GetPrimeMeridian(const char**) const.
3423 : */
3424 :
3425 1545 : double OGRSpatialReference::GetPrimeMeridian(const char **ppszName) const
3426 :
3427 : {
3428 3090 : TAKE_OPTIONAL_LOCK();
3429 :
3430 1545 : d->refreshProjObj();
3431 :
3432 1545 : if (!d->m_osPrimeMeridianName.empty())
3433 : {
3434 87 : if (ppszName != nullptr)
3435 11 : *ppszName = d->m_osPrimeMeridianName.c_str();
3436 87 : return d->dfFromGreenwich;
3437 : }
3438 :
3439 : while (true)
3440 : {
3441 1458 : if (!d->m_pj_crs)
3442 0 : break;
3443 :
3444 1458 : auto pm = proj_get_prime_meridian(d->getPROJContext(), d->m_pj_crs);
3445 1458 : if (!pm)
3446 0 : break;
3447 :
3448 1458 : d->m_osPrimeMeridianName = proj_get_name(pm);
3449 1458 : if (ppszName)
3450 20 : *ppszName = d->m_osPrimeMeridianName.c_str();
3451 1458 : double dfLongitude = 0.0;
3452 1458 : double dfConvFactor = 0.0;
3453 1458 : proj_prime_meridian_get_parameters(
3454 : d->getPROJContext(), pm, &dfLongitude, &dfConvFactor, nullptr);
3455 1458 : proj_destroy(pm);
3456 2916 : d->dfFromGreenwich =
3457 1458 : dfLongitude * dfConvFactor / CPLAtof(SRS_UA_DEGREE_CONV);
3458 1458 : return d->dfFromGreenwich;
3459 : }
3460 :
3461 0 : d->m_osPrimeMeridianName = SRS_PM_GREENWICH;
3462 0 : d->dfFromGreenwich = 0.0;
3463 0 : if (ppszName != nullptr)
3464 0 : *ppszName = d->m_osPrimeMeridianName.c_str();
3465 0 : return d->dfFromGreenwich;
3466 : }
3467 :
3468 : /**
3469 : * \brief Fetch prime meridian info.
3470 : *
3471 : * Returns the offset of the prime meridian from greenwich in degrees,
3472 : * and the prime meridian name (if requested). If no PRIMEM value exists
3473 : * in the coordinate system definition a value of "Greenwich" and an
3474 : * offset of 0.0 is assumed.
3475 : *
3476 : * If the prime meridian name is returned, the pointer is to an internal
3477 : * copy of the name. It should not be freed, altered or depended on after
3478 : * the next OGR call.
3479 : *
3480 : * This method is the same as the C function OSRGetPrimeMeridian().
3481 : *
3482 : * @param ppszName return location for prime meridian name. If NULL, name
3483 : * is not returned.
3484 : *
3485 : * @return the offset to the GEOGCS prime meridian from greenwich in decimal
3486 : * degrees.
3487 : */
3488 :
3489 0 : double OGRSpatialReference::GetPrimeMeridian(char **ppszName) const
3490 :
3491 : {
3492 0 : return GetPrimeMeridian(const_cast<const char **>(ppszName));
3493 : }
3494 :
3495 : /************************************************************************/
3496 : /* OSRGetPrimeMeridian() */
3497 : /************************************************************************/
3498 :
3499 : /**
3500 : * \brief Fetch prime meridian info.
3501 : *
3502 : * This function is the same as OGRSpatialReference::GetPrimeMeridian()
3503 : */
3504 0 : double OSRGetPrimeMeridian(OGRSpatialReferenceH hSRS, char **ppszName)
3505 :
3506 : {
3507 0 : VALIDATE_POINTER1(hSRS, "OSRGetPrimeMeridian", 0);
3508 :
3509 0 : return ToPointer(hSRS)->GetPrimeMeridian(
3510 0 : const_cast<const char **>(ppszName));
3511 : }
3512 :
3513 : /************************************************************************/
3514 : /* SetGeogCS() */
3515 : /************************************************************************/
3516 :
3517 : /**
3518 : * \brief Set geographic coordinate system.
3519 : *
3520 : * This method is used to set the datum, ellipsoid, prime meridian and
3521 : * angular units for a geographic coordinate system. It can be used on its
3522 : * own to establish a geographic spatial reference, or applied to a
3523 : * projected coordinate system to establish the underlying geographic
3524 : * coordinate system.
3525 : *
3526 : * This method does the same as the C function OSRSetGeogCS().
3527 : *
3528 : * @param pszGeogName user visible name for the geographic coordinate system
3529 : * (not to serve as a key).
3530 : *
3531 : * @param pszDatumName key name for this datum. The OpenGIS specification
3532 : * lists some known values, and otherwise EPSG datum names with a standard
3533 : * transformation are considered legal keys.
3534 : *
3535 : * @param pszSpheroidName user visible spheroid name (not to serve as a key)
3536 : *
3537 : * @param dfSemiMajor the semi major axis of the spheroid.
3538 : *
3539 : * @param dfInvFlattening the inverse flattening for the spheroid.
3540 : * This can be computed from the semi minor axis as
3541 : * 1/f = 1.0 / (1.0 - semiminor/semimajor).
3542 : *
3543 : * @param pszPMName the name of the prime meridian (not to serve as a key)
3544 : * If this is NULL a default value of "Greenwich" will be used.
3545 : *
3546 : * @param dfPMOffset the longitude of Greenwich relative to this prime
3547 : * meridian. Always in Degrees
3548 : *
3549 : * @param pszAngularUnits the angular units name (see ogr_srs_api.h for some
3550 : * standard names). If NULL a value of "degrees" will be assumed.
3551 : *
3552 : * @param dfConvertToRadians value to multiply angular units by to transform
3553 : * them to radians. A value of SRS_UA_DEGREE_CONV will be used if
3554 : * pszAngularUnits is NULL.
3555 : *
3556 : * @return OGRERR_NONE on success.
3557 : */
3558 :
3559 9817 : OGRErr OGRSpatialReference::SetGeogCS(
3560 : const char *pszGeogName, const char *pszDatumName,
3561 : const char *pszSpheroidName, double dfSemiMajor, double dfInvFlattening,
3562 : const char *pszPMName, double dfPMOffset, const char *pszAngularUnits,
3563 : double dfConvertToRadians)
3564 :
3565 : {
3566 19634 : TAKE_OPTIONAL_LOCK();
3567 :
3568 9817 : d->bNormInfoSet = FALSE;
3569 9817 : d->m_osAngularUnits.clear();
3570 9817 : d->m_dfAngularUnitToRadian = 0.0;
3571 9817 : d->m_osPrimeMeridianName.clear();
3572 9817 : d->dfFromGreenwich = 0.0;
3573 :
3574 : /* -------------------------------------------------------------------- */
3575 : /* For a geocentric coordinate system we want to set the datum */
3576 : /* and ellipsoid based on the GEOGCS. Create the GEOGCS in a */
3577 : /* temporary srs and use the copy method which has special */
3578 : /* handling for GEOCCS. */
3579 : /* -------------------------------------------------------------------- */
3580 9817 : if (IsGeocentric())
3581 : {
3582 4 : OGRSpatialReference oGCS;
3583 :
3584 2 : oGCS.SetGeogCS(pszGeogName, pszDatumName, pszSpheroidName, dfSemiMajor,
3585 : dfInvFlattening, pszPMName, dfPMOffset, pszAngularUnits,
3586 : dfConvertToRadians);
3587 2 : return CopyGeogCSFrom(&oGCS);
3588 : }
3589 :
3590 9815 : auto cs = proj_create_ellipsoidal_2D_cs(
3591 : d->getPROJContext(), PJ_ELLPS2D_LATITUDE_LONGITUDE, pszAngularUnits,
3592 : dfConvertToRadians);
3593 : // Prime meridian expressed in Degree
3594 9815 : auto obj = proj_create_geographic_crs(
3595 : d->getPROJContext(), pszGeogName, pszDatumName, pszSpheroidName,
3596 : dfSemiMajor, dfInvFlattening, pszPMName, dfPMOffset, nullptr, 0.0, cs);
3597 9815 : proj_destroy(cs);
3598 :
3599 14882 : if (d->m_pj_crs == nullptr || d->m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
3600 5067 : d->m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS)
3601 : {
3602 4748 : d->setPjCRS(obj);
3603 : }
3604 5067 : else if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
3605 : {
3606 10134 : d->setPjCRS(
3607 5067 : proj_crs_alter_geodetic_crs(d->getPROJContext(), d->m_pj_crs, obj));
3608 5067 : proj_destroy(obj);
3609 : }
3610 : else
3611 : {
3612 0 : proj_destroy(obj);
3613 : }
3614 :
3615 9815 : return OGRERR_NONE;
3616 : }
3617 :
3618 : /************************************************************************/
3619 : /* OSRSetGeogCS() */
3620 : /************************************************************************/
3621 :
3622 : /**
3623 : * \brief Set geographic coordinate system.
3624 : *
3625 : * This function is the same as OGRSpatialReference::SetGeogCS()
3626 : */
3627 18 : OGRErr OSRSetGeogCS(OGRSpatialReferenceH hSRS, const char *pszGeogName,
3628 : const char *pszDatumName, const char *pszSpheroidName,
3629 : double dfSemiMajor, double dfInvFlattening,
3630 : const char *pszPMName, double dfPMOffset,
3631 : const char *pszAngularUnits, double dfConvertToRadians)
3632 :
3633 : {
3634 18 : VALIDATE_POINTER1(hSRS, "OSRSetGeogCS", OGRERR_FAILURE);
3635 :
3636 18 : return ToPointer(hSRS)->SetGeogCS(pszGeogName, pszDatumName,
3637 : pszSpheroidName, dfSemiMajor,
3638 : dfInvFlattening, pszPMName, dfPMOffset,
3639 18 : pszAngularUnits, dfConvertToRadians);
3640 : }
3641 :
3642 : /************************************************************************/
3643 : /* SetWellKnownGeogCS() */
3644 : /************************************************************************/
3645 :
3646 : /**
3647 : * \brief Set a GeogCS based on well known name.
3648 : *
3649 : * This may be called on an empty OGRSpatialReference to make a geographic
3650 : * coordinate system, or on something with an existing PROJCS node to
3651 : * set the underlying geographic coordinate system of a projected coordinate
3652 : * system.
3653 : *
3654 : * The following well known text values are currently supported,
3655 : * Except for "EPSG:n", the others are without dependency on EPSG data files:
3656 : * <ul>
3657 : * <li> "EPSG:n": where n is the code a Geographic coordinate reference system.
3658 : * <li> "WGS84": same as "EPSG:4326" (axis order lat/long).
3659 : * <li> "WGS72": same as "EPSG:4322" (axis order lat/long).
3660 : * <li> "NAD83": same as "EPSG:4269" (axis order lat/long).
3661 : * <li> "NAD27": same as "EPSG:4267" (axis order lat/long).
3662 : * <li> "CRS84", "CRS:84": same as "WGS84" but with axis order long/lat.
3663 : * <li> "CRS72", "CRS:72": same as "WGS72" but with axis order long/lat.
3664 : * <li> "CRS27", "CRS:27": same as "NAD27" but with axis order long/lat.
3665 : * </ul>
3666 : *
3667 : * @param pszName name of well known geographic coordinate system.
3668 : * @return OGRERR_NONE on success, or OGRERR_FAILURE if the name isn't
3669 : * recognised, the target object is already initialized, or an EPSG value
3670 : * can't be successfully looked up.
3671 : */
3672 :
3673 4914 : OGRErr OGRSpatialReference::SetWellKnownGeogCS(const char *pszName)
3674 :
3675 : {
3676 9828 : TAKE_OPTIONAL_LOCK();
3677 :
3678 : /* -------------------------------------------------------------------- */
3679 : /* Check for EPSG authority numbers. */
3680 : /* -------------------------------------------------------------------- */
3681 4914 : if (STARTS_WITH_CI(pszName, "EPSG:") || STARTS_WITH_CI(pszName, "EPSGA:"))
3682 : {
3683 84 : OGRSpatialReference oSRS2;
3684 42 : const OGRErr eErr = oSRS2.importFromEPSG(atoi(pszName + 5));
3685 42 : if (eErr != OGRERR_NONE)
3686 0 : return eErr;
3687 :
3688 42 : if (!oSRS2.IsGeographic())
3689 0 : return OGRERR_FAILURE;
3690 :
3691 42 : return CopyGeogCSFrom(&oSRS2);
3692 : }
3693 :
3694 : /* -------------------------------------------------------------------- */
3695 : /* Check for simple names. */
3696 : /* -------------------------------------------------------------------- */
3697 4872 : const char *pszWKT = nullptr;
3698 :
3699 4872 : if (EQUAL(pszName, "WGS84"))
3700 : {
3701 2745 : pszWKT = SRS_WKT_WGS84_LAT_LONG;
3702 : }
3703 2127 : else if (EQUAL(pszName, "CRS84") || EQUAL(pszName, "CRS:84"))
3704 : {
3705 1174 : pszWKT =
3706 : "GEOGCRS[\"WGS 84 (CRS84)\",DATUM[\"World Geodetic System 1984\","
3707 : "ELLIPSOID[\"WGS "
3708 : "84\",6378137,298.257223563,LENGTHUNIT[\"metre\",1]]],"
3709 : "PRIMEM[\"Greenwich\",0,ANGLEUNIT[\"degree\",0.0174532925199433]],"
3710 : "CS[ellipsoidal,2],AXIS[\"geodetic longitude (Lon)\",east,ORDER[1],"
3711 : "ANGLEUNIT[\"degree\",0.0174532925199433]],"
3712 : "AXIS[\"geodetic latitude (Lat)\",north,ORDER[2],"
3713 : "ANGLEUNIT[\"degree\",0.0174532925199433]],"
3714 : "USAGE[SCOPE[\"unknown\"],AREA[\"World\"],BBOX[-90,-180,90,180]],"
3715 : "ID[\"OGC\",\"CRS84\"]]";
3716 : }
3717 953 : else if (EQUAL(pszName, "WGS72"))
3718 19 : pszWKT =
3719 : "GEOGCS[\"WGS 72\",DATUM[\"WGS_1972\","
3720 : "SPHEROID[\"WGS 72\",6378135,298.26,AUTHORITY[\"EPSG\",\"7043\"]],"
3721 : "AUTHORITY[\"EPSG\",\"6322\"]],"
3722 : "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3723 : "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3724 : "AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],"
3725 : "AUTHORITY[\"EPSG\",\"4322\"]]";
3726 :
3727 934 : else if (EQUAL(pszName, "NAD27"))
3728 136 : pszWKT =
3729 : "GEOGCS[\"NAD27\",DATUM[\"North_American_Datum_1927\","
3730 : "SPHEROID[\"Clarke 1866\",6378206.4,294.9786982138982,"
3731 : "AUTHORITY[\"EPSG\",\"7008\"]],AUTHORITY[\"EPSG\",\"6267\"]],"
3732 : "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3733 : "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3734 : "AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],"
3735 : "AUTHORITY[\"EPSG\",\"4267\"]]";
3736 :
3737 798 : else if (EQUAL(pszName, "CRS27") || EQUAL(pszName, "CRS:27"))
3738 0 : pszWKT =
3739 : "GEOGCS[\"NAD27\",DATUM[\"North_American_Datum_1927\","
3740 : "SPHEROID[\"Clarke 1866\",6378206.4,294.9786982138982,"
3741 : "AUTHORITY[\"EPSG\",\"7008\"]],AUTHORITY[\"EPSG\",\"6267\"]],"
3742 : "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3743 : "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3744 : "AXIS[\"Longitude\",EAST],AXIS[\"Latitude\",NORTH]]";
3745 :
3746 798 : else if (EQUAL(pszName, "NAD83"))
3747 794 : pszWKT =
3748 : "GEOGCS[\"NAD83\",DATUM[\"North_American_Datum_1983\","
3749 : "SPHEROID[\"GRS 1980\",6378137,298.257222101,"
3750 : "AUTHORITY[\"EPSG\",\"7019\"]],"
3751 : "AUTHORITY[\"EPSG\",\"6269\"]],"
3752 : "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3753 : "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3754 : "AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],AUTHORITY["
3755 : "\"EPSG\",\"4269\"]]";
3756 :
3757 4 : else if (EQUAL(pszName, "CRS83") || EQUAL(pszName, "CRS:83"))
3758 0 : pszWKT =
3759 : "GEOGCS[\"NAD83\",DATUM[\"North_American_Datum_1983\","
3760 : "SPHEROID[\"GRS 1980\",6378137,298.257222101,"
3761 : "AUTHORITY[\"EPSG\",\"7019\"]],"
3762 : "AUTHORITY[\"EPSG\",\"6269\"]],"
3763 : "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3764 : "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3765 : "AXIS[\"Longitude\",EAST],AXIS[\"Latitude\",NORTH]]";
3766 :
3767 : else
3768 4 : return OGRERR_FAILURE;
3769 :
3770 : /* -------------------------------------------------------------------- */
3771 : /* Import the WKT */
3772 : /* -------------------------------------------------------------------- */
3773 9736 : OGRSpatialReference oSRS2;
3774 4868 : const OGRErr eErr = oSRS2.importFromWkt(pszWKT);
3775 4868 : if (eErr != OGRERR_NONE)
3776 0 : return eErr;
3777 :
3778 : /* -------------------------------------------------------------------- */
3779 : /* Copy over. */
3780 : /* -------------------------------------------------------------------- */
3781 4868 : return CopyGeogCSFrom(&oSRS2);
3782 : }
3783 :
3784 : /************************************************************************/
3785 : /* OSRSetWellKnownGeogCS() */
3786 : /************************************************************************/
3787 :
3788 : /**
3789 : * \brief Set a GeogCS based on well known name.
3790 : *
3791 : * This function is the same as OGRSpatialReference::SetWellKnownGeogCS()
3792 : */
3793 155 : OGRErr OSRSetWellKnownGeogCS(OGRSpatialReferenceH hSRS, const char *pszName)
3794 :
3795 : {
3796 155 : VALIDATE_POINTER1(hSRS, "OSRSetWellKnownGeogCS", OGRERR_FAILURE);
3797 :
3798 155 : return ToPointer(hSRS)->SetWellKnownGeogCS(pszName);
3799 : }
3800 :
3801 : /************************************************************************/
3802 : /* CopyGeogCSFrom() */
3803 : /************************************************************************/
3804 :
3805 : /**
3806 : * \brief Copy GEOGCS from another OGRSpatialReference.
3807 : *
3808 : * The GEOGCS information is copied into this OGRSpatialReference from another.
3809 : * If this object has a PROJCS root already, the GEOGCS is installed within
3810 : * it, otherwise it is installed as the root.
3811 : *
3812 : * @param poSrcSRS the spatial reference to copy the GEOGCS information from.
3813 : *
3814 : * @return OGRERR_NONE on success or an error code.
3815 : */
3816 5376 : OGRErr OGRSpatialReference::CopyGeogCSFrom(const OGRSpatialReference *poSrcSRS)
3817 :
3818 : {
3819 5376 : return CopyGeogCSFrom(poSrcSRS, false);
3820 : }
3821 :
3822 : /**
3823 : * \brief Copy GEOGCS from another OGRSpatialReference.
3824 : *
3825 : * The GEOGCS information is copied into this OGRSpatialReference from another.
3826 : * If this object has a PROJCS root already, the GEOGCS is installed within
3827 : * it, otherwise it is installed as the root.
3828 : *
3829 : * @param poSrcSRS the spatial reference to copy the GEOGCS information from.
3830 : * @param bInnerMostGeogCRS Whether the inner-most geographic CRS must be used.
3831 : * This setting makes a difference if this CRS is a
3832 : * derived geographic CRS. Setting bInnerMostGeogCRS
3833 : * to true will then extract its base CRS.
3834 : *
3835 : * @return OGRERR_NONE on success or an error code.
3836 : * @since 3.13.2
3837 : */
3838 5531 : OGRErr OGRSpatialReference::CopyGeogCSFrom(const OGRSpatialReference *poSrcSRS,
3839 : bool bInnerMostGeogCRS)
3840 :
3841 : {
3842 11062 : TAKE_OPTIONAL_LOCK();
3843 :
3844 5531 : d->bNormInfoSet = FALSE;
3845 5531 : d->m_osAngularUnits.clear();
3846 5531 : d->m_dfAngularUnitToRadian = 0.0;
3847 5531 : d->m_osPrimeMeridianName.clear();
3848 5531 : d->dfFromGreenwich = 0.0;
3849 :
3850 5531 : d->refreshProjObj();
3851 5531 : poSrcSRS->d->refreshProjObj();
3852 5531 : if (!poSrcSRS->d->m_pj_crs)
3853 : {
3854 1 : return OGRERR_FAILURE;
3855 : }
3856 : auto geodCRS =
3857 5530 : proj_crs_get_geodetic_crs(d->getPROJContext(), poSrcSRS->d->m_pj_crs);
3858 5530 : if (!geodCRS)
3859 : {
3860 0 : return OGRERR_FAILURE;
3861 : }
3862 :
3863 5530 : if (bInnerMostGeogCRS && poSrcSRS->IsDerivedGeographic())
3864 : {
3865 0 : auto baseCRS = proj_get_source_crs(d->getPROJContext(), geodCRS);
3866 0 : if (!baseCRS)
3867 0 : return OGRERR_FAILURE;
3868 0 : proj_destroy(geodCRS);
3869 0 : geodCRS = baseCRS;
3870 : }
3871 :
3872 : /* -------------------------------------------------------------------- */
3873 : /* Handle geocentric coordinate systems specially. We just */
3874 : /* want to copy the DATUM. */
3875 : /* -------------------------------------------------------------------- */
3876 5530 : if (d->m_pjType == PJ_TYPE_GEOCENTRIC_CRS)
3877 : {
3878 3 : auto datum = proj_crs_get_datum(d->getPROJContext(), geodCRS);
3879 : #if PROJ_VERSION_MAJOR > 7 || \
3880 : (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
3881 : if (datum == nullptr)
3882 : {
3883 : datum = proj_crs_get_datum_ensemble(d->getPROJContext(), geodCRS);
3884 : }
3885 : #endif
3886 3 : if (datum == nullptr)
3887 : {
3888 0 : proj_destroy(geodCRS);
3889 0 : return OGRERR_FAILURE;
3890 : }
3891 :
3892 3 : const char *pszUnitName = nullptr;
3893 3 : double unitConvFactor = GetLinearUnits(&pszUnitName);
3894 :
3895 3 : auto pj_crs = proj_create_geocentric_crs_from_datum(
3896 3 : d->getPROJContext(), proj_get_name(d->m_pj_crs), datum, pszUnitName,
3897 : unitConvFactor);
3898 3 : proj_destroy(datum);
3899 :
3900 3 : d->setPjCRS(pj_crs);
3901 : }
3902 :
3903 5527 : else if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
3904 : {
3905 319 : auto pj_crs = proj_crs_alter_geodetic_crs(d->getPROJContext(),
3906 319 : d->m_pj_crs, geodCRS);
3907 319 : d->setPjCRS(pj_crs);
3908 : }
3909 :
3910 : else
3911 : {
3912 5208 : d->setPjCRS(proj_clone(d->getPROJContext(), geodCRS));
3913 : }
3914 :
3915 : // Apply TOWGS84 of source CRS
3916 5530 : if (poSrcSRS->d->m_pjType == PJ_TYPE_BOUND_CRS)
3917 : {
3918 : auto target =
3919 1 : proj_get_target_crs(d->getPROJContext(), poSrcSRS->d->m_pj_crs);
3920 1 : auto co = proj_crs_get_coordoperation(d->getPROJContext(),
3921 1 : poSrcSRS->d->m_pj_crs);
3922 1 : d->setPjCRS(proj_crs_create_bound_crs(d->getPROJContext(), d->m_pj_crs,
3923 : target, co));
3924 1 : proj_destroy(target);
3925 1 : proj_destroy(co);
3926 : }
3927 :
3928 5530 : proj_destroy(geodCRS);
3929 :
3930 5530 : return OGRERR_NONE;
3931 : }
3932 :
3933 : /************************************************************************/
3934 : /* OSRCopyGeogCSFrom() */
3935 : /************************************************************************/
3936 :
3937 : /**
3938 : * \brief Copy GEOGCS from another OGRSpatialReference.
3939 : *
3940 : * This function is the same as OGRSpatialReference::CopyGeogCSFrom()
3941 : */
3942 1 : OGRErr OSRCopyGeogCSFrom(OGRSpatialReferenceH hSRS,
3943 : const OGRSpatialReferenceH hSrcSRS)
3944 :
3945 : {
3946 1 : VALIDATE_POINTER1(hSRS, "OSRCopyGeogCSFrom", OGRERR_FAILURE);
3947 1 : VALIDATE_POINTER1(hSrcSRS, "OSRCopyGeogCSFrom", OGRERR_FAILURE);
3948 :
3949 1 : return ToPointer(hSRS)->CopyGeogCSFrom(ToPointer(hSrcSRS));
3950 : }
3951 :
3952 : /************************************************************************/
3953 : /* SET_FROM_USER_INPUT_LIMITATIONS_get() */
3954 : /************************************************************************/
3955 :
3956 : /** Limitations for OGRSpatialReference::SetFromUserInput().
3957 : *
3958 : * Currently ALLOW_NETWORK_ACCESS=NO and ALLOW_FILE_ACCESS=NO.
3959 : */
3960 : const char *const OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS[] = {
3961 : "ALLOW_NETWORK_ACCESS=NO", "ALLOW_FILE_ACCESS=NO", nullptr};
3962 :
3963 : /**
3964 : * \brief Return OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS
3965 : */
3966 2790 : CSLConstList OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS_get()
3967 : {
3968 2790 : return SET_FROM_USER_INPUT_LIMITATIONS;
3969 : }
3970 :
3971 : /************************************************************************/
3972 : /* RemoveIDFromMemberOfEnsembles() */
3973 : /************************************************************************/
3974 :
3975 : // cppcheck-suppress constParameterReference
3976 243 : static void RemoveIDFromMemberOfEnsembles(CPLJSONObject &obj)
3977 : {
3978 : // Remove "id" from members of datum ensembles for compatibility with
3979 : // older PROJ versions
3980 : // Cf https://github.com/opengeospatial/geoparquet/discussions/110
3981 : // and https://github.com/OSGeo/PROJ/pull/3221
3982 243 : if (obj.GetType() == CPLJSONObject::Type::Object)
3983 : {
3984 300 : for (auto &subObj : obj.GetChildren())
3985 : {
3986 235 : RemoveIDFromMemberOfEnsembles(subObj);
3987 : }
3988 : }
3989 198 : else if (obj.GetType() == CPLJSONObject::Type::Array &&
3990 198 : obj.GetName() == "members")
3991 : {
3992 60 : for (auto &subObj : obj.ToArray())
3993 : {
3994 52 : if (subObj.GetType() == CPLJSONObject::Type::Object)
3995 : {
3996 51 : subObj.Delete("id");
3997 : }
3998 : }
3999 : }
4000 243 : }
4001 :
4002 : /************************************************************************/
4003 : /* SetFromUserInput() */
4004 : /************************************************************************/
4005 :
4006 : /**
4007 : * \brief Set spatial reference from various text formats.
4008 : *
4009 : * This method will examine the provided input, and try to deduce the
4010 : * format, and then use it to initialize the spatial reference system. It
4011 : * may take the following forms:
4012 : *
4013 : * <ol>
4014 : * <li> Well Known Text definition - passed on to importFromWkt().
4015 : * <li> "EPSG:n" - number passed on to importFromEPSG().
4016 : * <li> "EPSGA:n" - number passed on to importFromEPSGA().
4017 : * <li> "AUTO:proj_id,unit_id,lon0,lat0" - WMS auto projections.
4018 : * <li> "urn:ogc:def:crs:EPSG::n" - ogc urns
4019 : * <li> PROJ.4 definitions - passed on to importFromProj4().
4020 : * <li> filename - file read for WKT, XML or PROJ.4 definition.
4021 : * <li> well known name accepted by SetWellKnownGeogCS(), such as NAD27, NAD83,
4022 : * WGS84 or WGS72.
4023 : * <li> "IGNF:xxxx", "ESRI:xxxx", etc. from definitions from the PROJ database;
4024 : * <li> PROJJSON (PROJ >= 6.2)
4025 : * </ol>
4026 : *
4027 : * It is expected that this method will be extended in the future to support
4028 : * XML and perhaps a simplified "minilanguage" for indicating common UTM and
4029 : * State Plane definitions.
4030 : *
4031 : * This method is intended to be flexible, but by its nature it is
4032 : * imprecise as it must guess information about the format intended. When
4033 : * possible applications should call the specific method appropriate if the
4034 : * input is known to be in a particular format.
4035 : *
4036 : * This method does the same thing as the OSRSetFromUserInput() function.
4037 : *
4038 : * @param pszDefinition text definition to try to deduce SRS from.
4039 : *
4040 : * @return OGRERR_NONE on success, or an error code if the name isn't
4041 : * recognised, the definition is corrupt, or an EPSG value can't be
4042 : * successfully looked up.
4043 : */
4044 :
4045 23480 : OGRErr OGRSpatialReference::SetFromUserInput(const char *pszDefinition)
4046 : {
4047 23480 : return SetFromUserInput(pszDefinition, nullptr);
4048 : }
4049 :
4050 : /**
4051 : * \brief Set spatial reference from various text formats.
4052 : *
4053 : * This method will examine the provided input, and try to deduce the
4054 : * format, and then use it to initialize the spatial reference system. It
4055 : * may take the following forms:
4056 : *
4057 : * <ol>
4058 : * <li> Well Known Text definition - passed on to importFromWkt().
4059 : * <li> "EPSG:n" - number passed on to importFromEPSG().
4060 : * <li> "EPSGA:n" - number passed on to importFromEPSGA().
4061 : * <li> "AUTO:proj_id,unit_id,lon0,lat0" - WMS auto projections.
4062 : * <li> "urn:ogc:def:crs:EPSG::n" - ogc urns
4063 : * <li> PROJ.4 definitions - passed on to importFromProj4().
4064 : * <li> filename - file read for WKT, XML or PROJ.4 definition.
4065 : * <li> well known name accepted by SetWellKnownGeogCS(), such as NAD27, NAD83,
4066 : * WGS84 or WGS72.
4067 : * <li> "IGNF:xxxx", "ESRI:xxxx", etc. from definitions from the PROJ database;
4068 : * <li> PROJJSON (PROJ >= 6.2)
4069 : * </ol>
4070 : *
4071 : * It is expected that this method will be extended in the future to support
4072 : * XML and perhaps a simplified "minilanguage" for indicating common UTM and
4073 : * State Plane definitions.
4074 : *
4075 : * This method is intended to be flexible, but by its nature it is
4076 : * imprecise as it must guess information about the format intended. When
4077 : * possible applications should call the specific method appropriate if the
4078 : * input is known to be in a particular format.
4079 : *
4080 : * This method does the same thing as the OSRSetFromUserInput() and
4081 : * OSRSetFromUserInputEx() functions.
4082 : *
4083 : * @param pszDefinition text definition to try to deduce SRS from.
4084 : *
4085 : * @param papszOptions NULL terminated list of options, or NULL.
4086 : * <ol>
4087 : * <li> ALLOW_NETWORK_ACCESS=YES/NO.
4088 : * Whether http:// or https:// access is allowed. Defaults to YES.
4089 : * <li> ALLOW_FILE_ACCESS=YES/NO.
4090 : * Whether reading a file using the Virtual File System layer is allowed
4091 : * (can also involve network access). Defaults to YES.
4092 : * </ol>
4093 : *
4094 : * @return OGRERR_NONE on success, or an error code if the name isn't
4095 : * recognised, the definition is corrupt, or an EPSG value can't be
4096 : * successfully looked up.
4097 : */
4098 :
4099 31206 : OGRErr OGRSpatialReference::SetFromUserInput(const char *pszDefinition,
4100 : CSLConstList papszOptions)
4101 : {
4102 62412 : TAKE_OPTIONAL_LOCK();
4103 :
4104 : // Skip leading white space
4105 31208 : while (isspace(static_cast<unsigned char>(*pszDefinition)))
4106 2 : pszDefinition++;
4107 :
4108 31206 : if (STARTS_WITH_CI(pszDefinition, "ESRI::"))
4109 : {
4110 1 : pszDefinition += 6;
4111 : }
4112 :
4113 : /* -------------------------------------------------------------------- */
4114 : /* Is it a recognised syntax? */
4115 : /* -------------------------------------------------------------------- */
4116 31206 : const char *const wktKeywords[] = {
4117 : // WKT1
4118 : "GEOGCS", "GEOCCS", "PROJCS", "VERT_CS", "COMPD_CS", "LOCAL_CS",
4119 : // WKT2"
4120 : "GEODCRS", "GEOGCRS", "GEODETICCRS", "GEOGRAPHICCRS", "PROJCRS",
4121 : "PROJECTEDCRS", "VERTCRS", "VERTICALCRS", "COMPOUNDCRS", "ENGCRS",
4122 : "ENGINEERINGCRS", "BOUNDCRS", "DERIVEDPROJCRS", "COORDINATEMETADATA"};
4123 483572 : for (const char *keyword : wktKeywords)
4124 : {
4125 461651 : if (STARTS_WITH_CI(pszDefinition, keyword))
4126 : {
4127 9285 : return importFromWkt(pszDefinition);
4128 : }
4129 : }
4130 :
4131 21921 : const bool bStartsWithEPSG = STARTS_WITH_CI(pszDefinition, "EPSG:");
4132 21921 : if (bStartsWithEPSG || STARTS_WITH_CI(pszDefinition, "EPSGA:"))
4133 : {
4134 12316 : OGRErr eStatus = OGRERR_NONE;
4135 :
4136 12316 : if (strchr(pszDefinition, '+') || strchr(pszDefinition, '@'))
4137 : {
4138 : // Use proj_create() as it allows things like EPSG:3157+4617
4139 : // that are not normally supported by the below code that
4140 : // builds manually a compound CRS
4141 68 : PJ *pj = proj_create(d->getPROJContext(), pszDefinition);
4142 68 : if (!pj)
4143 : {
4144 1 : return OGRERR_FAILURE;
4145 : }
4146 67 : Clear();
4147 67 : d->setPjCRS(pj);
4148 67 : return OGRERR_NONE;
4149 : }
4150 : else
4151 : {
4152 : eStatus =
4153 12248 : importFromEPSG(atoi(pszDefinition + (bStartsWithEPSG ? 5 : 6)));
4154 : }
4155 :
4156 12248 : return eStatus;
4157 : }
4158 :
4159 9605 : if (STARTS_WITH_CI(pszDefinition, "urn:ogc:def:crs:") ||
4160 8880 : STARTS_WITH_CI(pszDefinition, "urn:ogc:def:crs,crs:") ||
4161 8879 : STARTS_WITH_CI(pszDefinition, "urn:x-ogc:def:crs:") ||
4162 8821 : STARTS_WITH_CI(pszDefinition, "urn:opengis:crs:") ||
4163 8821 : STARTS_WITH_CI(pszDefinition, "urn:opengis:def:crs:") ||
4164 8821 : STARTS_WITH_CI(pszDefinition, "urn:ogc:def:coordinateMetadata:"))
4165 784 : return importFromURN(pszDefinition);
4166 :
4167 8821 : if (STARTS_WITH_CI(pszDefinition, "http://opengis.net/def/crs") ||
4168 8819 : STARTS_WITH_CI(pszDefinition, "https://opengis.net/def/crs") ||
4169 8818 : STARTS_WITH_CI(pszDefinition, "http://www.opengis.net/def/crs") ||
4170 1674 : STARTS_WITH_CI(pszDefinition, "https://www.opengis.net/def/crs") ||
4171 1673 : STARTS_WITH_CI(pszDefinition, "www.opengis.net/def/crs"))
4172 7148 : return importFromCRSURL(pszDefinition);
4173 :
4174 1673 : if (STARTS_WITH_CI(pszDefinition, "AUTO:"))
4175 1 : return importFromWMSAUTO(pszDefinition);
4176 :
4177 : // WMS/WCS OGC codes like OGC:CRS84.
4178 1672 : if (EQUAL(pszDefinition, "OGC:CRS84"))
4179 79 : return SetWellKnownGeogCS(pszDefinition + 4);
4180 :
4181 1593 : if (STARTS_WITH_CI(pszDefinition, "CRS:"))
4182 1 : return SetWellKnownGeogCS(pszDefinition);
4183 :
4184 1592 : if (STARTS_WITH_CI(pszDefinition, "DICT:") && strstr(pszDefinition, ","))
4185 : {
4186 0 : char *pszFile = CPLStrdup(pszDefinition + 5);
4187 0 : char *pszCode = strstr(pszFile, ",") + 1;
4188 :
4189 0 : pszCode[-1] = '\0';
4190 :
4191 0 : OGRErr err = importFromDict(pszFile, pszCode);
4192 0 : CPLFree(pszFile);
4193 :
4194 0 : return err;
4195 : }
4196 :
4197 1592 : if (EQUAL(pszDefinition, "NAD27") || EQUAL(pszDefinition, "NAD83") ||
4198 1587 : EQUAL(pszDefinition, "WGS84") || EQUAL(pszDefinition, "WGS72"))
4199 : {
4200 546 : Clear();
4201 546 : return SetWellKnownGeogCS(pszDefinition);
4202 : }
4203 :
4204 : // PROJJSON
4205 1046 : if (pszDefinition[0] == '{' && strstr(pszDefinition, "\"type\"") &&
4206 179 : (strstr(pszDefinition, "GeodeticCRS") ||
4207 179 : strstr(pszDefinition, "GeographicCRS") ||
4208 129 : strstr(pszDefinition, "ProjectedCRS") ||
4209 0 : strstr(pszDefinition, "VerticalCRS") ||
4210 0 : strstr(pszDefinition, "BoundCRS") ||
4211 0 : strstr(pszDefinition, "CompoundCRS") ||
4212 0 : strstr(pszDefinition, "DerivedGeodeticCRS") ||
4213 0 : strstr(pszDefinition, "DerivedGeographicCRS") ||
4214 0 : strstr(pszDefinition, "DerivedProjectedCRS") ||
4215 0 : strstr(pszDefinition, "DerivedVerticalCRS") ||
4216 0 : strstr(pszDefinition, "EngineeringCRS") ||
4217 0 : strstr(pszDefinition, "DerivedEngineeringCRS") ||
4218 0 : strstr(pszDefinition, "ParametricCRS") ||
4219 0 : strstr(pszDefinition, "DerivedParametricCRS") ||
4220 0 : strstr(pszDefinition, "TemporalCRS") ||
4221 0 : strstr(pszDefinition, "DerivedTemporalCRS")))
4222 : {
4223 : PJ *pj;
4224 179 : if (strstr(pszDefinition, "datum_ensemble") != nullptr)
4225 : {
4226 : // PROJ < 9.0.1 doesn't like a datum_ensemble whose member have
4227 : // a unknown id.
4228 8 : CPLJSONDocument oCRSDoc;
4229 8 : if (!oCRSDoc.LoadMemory(pszDefinition))
4230 0 : return OGRERR_CORRUPT_DATA;
4231 8 : CPLJSONObject oCRSRoot = oCRSDoc.GetRoot();
4232 8 : RemoveIDFromMemberOfEnsembles(oCRSRoot);
4233 8 : pj = proj_create(d->getPROJContext(), oCRSRoot.ToString().c_str());
4234 : }
4235 : else
4236 : {
4237 171 : pj = proj_create(d->getPROJContext(), pszDefinition);
4238 : }
4239 179 : if (!pj)
4240 : {
4241 2 : return OGRERR_FAILURE;
4242 : }
4243 177 : Clear();
4244 177 : d->setPjCRS(pj);
4245 177 : return OGRERR_NONE;
4246 : }
4247 :
4248 867 : if (strstr(pszDefinition, "+proj") != nullptr ||
4249 408 : strstr(pszDefinition, "+init") != nullptr)
4250 459 : return importFromProj4(pszDefinition);
4251 :
4252 408 : if (STARTS_WITH_CI(pszDefinition, "http://") ||
4253 383 : STARTS_WITH_CI(pszDefinition, "https://"))
4254 : {
4255 26 : if (CPLTestBool(CSLFetchNameValueDef(papszOptions,
4256 : "ALLOW_NETWORK_ACCESS", "YES")))
4257 0 : return importFromUrl(pszDefinition);
4258 :
4259 26 : CPLError(CE_Failure, CPLE_AppDefined,
4260 : "Cannot import %s due to ALLOW_NETWORK_ACCESS=NO",
4261 : pszDefinition);
4262 26 : return OGRERR_FAILURE;
4263 : }
4264 :
4265 382 : if (EQUAL(pszDefinition, "osgb:BNG"))
4266 : {
4267 8 : return importFromEPSG(27700);
4268 : }
4269 :
4270 : // Used by German CityGML files
4271 374 : if (EQUAL(pszDefinition, "urn:adv:crs:ETRS89_UTM32*DE_DHHN92_NH"))
4272 : {
4273 : // "ETRS89 / UTM Zone 32N + DHHN92 height"
4274 0 : return SetFromUserInput("EPSG:25832+5783");
4275 : }
4276 374 : else if (EQUAL(pszDefinition, "urn:adv:crs:ETRS89_UTM32*DE_DHHN2016_NH"))
4277 : {
4278 : // "ETRS89 / UTM Zone 32N + DHHN2016 height"
4279 3 : return SetFromUserInput("EPSG:25832+7837");
4280 : }
4281 :
4282 : // Used by Japan's Fundamental Geospatial Data (FGD) GML
4283 371 : if (EQUAL(pszDefinition, "fguuid:jgd2001.bl"))
4284 0 : return importFromEPSG(4612); // JGD2000 (slight difference in years)
4285 371 : else if (EQUAL(pszDefinition, "fguuid:jgd2011.bl"))
4286 8 : return importFromEPSG(6668); // JGD2011
4287 363 : else if (EQUAL(pszDefinition, "fguuid:jgd2024.bl"))
4288 : {
4289 : // FIXME when EPSG attributes a CRS code
4290 3 : return importFromWkt(
4291 : "GEOGCRS[\"JGD2024\",\n"
4292 : " DATUM[\"Japanese Geodetic Datum 2024\",\n"
4293 : " ELLIPSOID[\"GRS 1980\",6378137,298.257222101,\n"
4294 : " LENGTHUNIT[\"metre\",1]]],\n"
4295 : " PRIMEM[\"Greenwich\",0,\n"
4296 : " ANGLEUNIT[\"degree\",0.0174532925199433]],\n"
4297 : " CS[ellipsoidal,2],\n"
4298 : " AXIS[\"geodetic latitude (Lat)\",north,\n"
4299 : " ORDER[1],\n"
4300 : " ANGLEUNIT[\"degree\",0.0174532925199433]],\n"
4301 : " AXIS[\"geodetic longitude (Lon)\",east,\n"
4302 : " ORDER[2],\n"
4303 : " ANGLEUNIT[\"degree\",0.0174532925199433]],\n"
4304 : " USAGE[\n"
4305 : " SCOPE[\"Horizontal component of 3D system.\"],\n"
4306 : " AREA[\"Japan - onshore and offshore.\"],\n"
4307 3 : " BBOX[17.09,122.38,46.05,157.65]]]");
4308 : }
4309 :
4310 : // Deal with IGNF:xxx, ESRI:xxx, etc from the PROJ database
4311 360 : const char *pszDot = strrchr(pszDefinition, ':');
4312 360 : if (pszDot)
4313 : {
4314 125 : CPLString osPrefix(pszDefinition, pszDot - pszDefinition);
4315 : auto authorities =
4316 125 : proj_get_authorities_from_database(d->getPROJContext());
4317 125 : if (authorities)
4318 : {
4319 125 : std::set<std::string> aosCandidateAuthorities;
4320 290 : for (auto iter = authorities; *iter; ++iter)
4321 : {
4322 288 : if (*iter == osPrefix)
4323 : {
4324 123 : aosCandidateAuthorities.clear();
4325 123 : aosCandidateAuthorities.insert(*iter);
4326 123 : break;
4327 : }
4328 : // Deal with "IAU_2015" as authority in the list and input
4329 : // "IAU:code"
4330 165 : else if (strncmp(*iter, osPrefix.c_str(), osPrefix.size()) ==
4331 165 : 0 &&
4332 0 : (*iter)[osPrefix.size()] == '_')
4333 : {
4334 0 : aosCandidateAuthorities.insert(*iter);
4335 : }
4336 : // Deal with "IAU_2015" as authority in the list and input
4337 : // "IAU:2015:code"
4338 330 : else if (osPrefix.find(':') != std::string::npos &&
4339 165 : osPrefix.size() == strlen(*iter) &&
4340 165 : CPLString(osPrefix).replaceAll(':', '_') == *iter)
4341 : {
4342 0 : aosCandidateAuthorities.clear();
4343 0 : aosCandidateAuthorities.insert(*iter);
4344 0 : break;
4345 : }
4346 : }
4347 :
4348 125 : proj_string_list_destroy(authorities);
4349 :
4350 125 : if (!aosCandidateAuthorities.empty())
4351 : {
4352 123 : auto obj = proj_create_from_database(
4353 : d->getPROJContext(),
4354 123 : aosCandidateAuthorities.rbegin()->c_str(), pszDot + 1,
4355 : PJ_CATEGORY_CRS, false, nullptr);
4356 123 : if (!obj)
4357 : {
4358 16 : return OGRERR_FAILURE;
4359 : }
4360 107 : Clear();
4361 107 : d->setPjCRS(obj);
4362 107 : return OGRERR_NONE;
4363 : }
4364 : }
4365 : }
4366 :
4367 : /* -------------------------------------------------------------------- */
4368 : /* Try to open it as a file. */
4369 : /* -------------------------------------------------------------------- */
4370 237 : if (!CPLTestBool(
4371 : CSLFetchNameValueDef(papszOptions, "ALLOW_FILE_ACCESS", "YES")))
4372 : {
4373 : VSIStatBufL sStat;
4374 40 : if (STARTS_WITH(pszDefinition, "/vsi") ||
4375 20 : VSIStatExL(pszDefinition, &sStat, VSI_STAT_EXISTS_FLAG) == 0)
4376 : {
4377 0 : CPLError(CE_Failure, CPLE_AppDefined,
4378 : "Cannot import %s due to ALLOW_FILE_ACCESS=NO",
4379 : pszDefinition);
4380 0 : return OGRERR_FAILURE;
4381 : }
4382 : // We used to silently return an error without a CE_Failure message
4383 : // Cf https://github.com/Toblerity/Fiona/issues/1063
4384 20 : return OGRERR_CORRUPT_DATA;
4385 : }
4386 :
4387 434 : CPLConfigOptionSetter oSetter("CPL_ALLOW_VSISTDIN", "NO", true);
4388 217 : VSILFILE *const fp = VSIFOpenL(pszDefinition, "rt");
4389 217 : if (fp == nullptr)
4390 214 : return OGRERR_CORRUPT_DATA;
4391 :
4392 3 : const size_t nBufMax = 100000;
4393 3 : char *const pszBuffer = static_cast<char *>(CPLMalloc(nBufMax));
4394 3 : const size_t nBytes = VSIFReadL(pszBuffer, 1, nBufMax - 1, fp);
4395 3 : VSIFCloseL(fp);
4396 :
4397 3 : if (nBytes == nBufMax - 1)
4398 : {
4399 0 : CPLDebug("OGR",
4400 : "OGRSpatialReference::SetFromUserInput(%s), opened file "
4401 : "but it is to large for our generous buffer. Is it really "
4402 : "just a WKT definition?",
4403 : pszDefinition);
4404 0 : CPLFree(pszBuffer);
4405 0 : return OGRERR_FAILURE;
4406 : }
4407 :
4408 3 : pszBuffer[nBytes] = '\0';
4409 :
4410 3 : char *pszBufPtr = pszBuffer;
4411 3 : while (pszBufPtr[0] == ' ' || pszBufPtr[0] == '\n')
4412 0 : pszBufPtr++;
4413 :
4414 3 : OGRErr err = OGRERR_NONE;
4415 3 : if (pszBufPtr[0] == '<')
4416 0 : err = importFromXML(pszBufPtr);
4417 3 : else if ((strstr(pszBuffer, "+proj") != nullptr ||
4418 3 : strstr(pszBuffer, "+init") != nullptr) &&
4419 0 : (strstr(pszBuffer, "EXTENSION") == nullptr &&
4420 0 : strstr(pszBuffer, "extension") == nullptr))
4421 0 : err = importFromProj4(pszBufPtr);
4422 : else
4423 : {
4424 3 : if (STARTS_WITH_CI(pszBufPtr, "ESRI::"))
4425 : {
4426 0 : pszBufPtr += 6;
4427 : }
4428 :
4429 : // coverity[tainted_data]
4430 3 : err = importFromWkt(pszBufPtr);
4431 : }
4432 :
4433 3 : CPLFree(pszBuffer);
4434 :
4435 3 : return err;
4436 : }
4437 :
4438 : /************************************************************************/
4439 : /* OSRSetFromUserInput() */
4440 : /************************************************************************/
4441 :
4442 : /**
4443 : * \brief Set spatial reference from various text formats.
4444 : *
4445 : * This function is the same as OGRSpatialReference::SetFromUserInput()
4446 : *
4447 : * \see OSRSetFromUserInputEx() for a variant allowing to pass options.
4448 : */
4449 383 : OGRErr CPL_STDCALL OSRSetFromUserInput(OGRSpatialReferenceH hSRS,
4450 : const char *pszDef)
4451 :
4452 : {
4453 383 : VALIDATE_POINTER1(hSRS, "OSRSetFromUserInput", OGRERR_FAILURE);
4454 :
4455 383 : return ToPointer(hSRS)->SetFromUserInput(pszDef);
4456 : }
4457 :
4458 : /************************************************************************/
4459 : /* OSRSetFromUserInputEx() */
4460 : /************************************************************************/
4461 :
4462 : /**
4463 : * \brief Set spatial reference from various text formats.
4464 : *
4465 : * This function is the same as OGRSpatialReference::SetFromUserInput().
4466 : *
4467 : * @since GDAL 3.9
4468 : */
4469 1264 : OGRErr OSRSetFromUserInputEx(OGRSpatialReferenceH hSRS, const char *pszDef,
4470 : CSLConstList papszOptions)
4471 :
4472 : {
4473 1264 : VALIDATE_POINTER1(hSRS, "OSRSetFromUserInputEx", OGRERR_FAILURE);
4474 :
4475 1264 : return ToPointer(hSRS)->SetFromUserInput(pszDef, papszOptions);
4476 : }
4477 :
4478 : /************************************************************************/
4479 : /* ImportFromUrl() */
4480 : /************************************************************************/
4481 :
4482 : /**
4483 : * \brief Set spatial reference from a URL.
4484 : *
4485 : * This method will download the spatial reference at a given URL and
4486 : * feed it into SetFromUserInput for you.
4487 : *
4488 : * This method does the same thing as the OSRImportFromUrl() function.
4489 : *
4490 : * @param pszUrl text definition to try to deduce SRS from.
4491 : *
4492 : * @return OGRERR_NONE on success, or an error code with the curl
4493 : * error message if it is unable to download data.
4494 : */
4495 :
4496 5 : OGRErr OGRSpatialReference::importFromUrl(const char *pszUrl)
4497 :
4498 : {
4499 10 : TAKE_OPTIONAL_LOCK();
4500 :
4501 5 : if (!STARTS_WITH_CI(pszUrl, "http://") &&
4502 3 : !STARTS_WITH_CI(pszUrl, "https://"))
4503 : {
4504 2 : CPLError(CE_Failure, CPLE_AppDefined,
4505 : "The given string is not recognized as a URL"
4506 : "starting with 'http://' -- %s",
4507 : pszUrl);
4508 2 : return OGRERR_FAILURE;
4509 : }
4510 :
4511 : /* -------------------------------------------------------------------- */
4512 : /* Fetch the result. */
4513 : /* -------------------------------------------------------------------- */
4514 3 : CPLErrorReset();
4515 :
4516 6 : std::string osUrl(pszUrl);
4517 : // We have historically supported "http://spatialreference.org/ref/AUTHNAME/CODE/"
4518 : // as a valid URL since we used a "Accept: application/x-ogcwkt" header
4519 : // to query WKT. To allow a static server to be used, rather append a
4520 : // "ogcwkt/" suffix.
4521 2 : for (const char *pszPrefix : {"https://spatialreference.org/ref/",
4522 5 : "http://spatialreference.org/ref/"})
4523 : {
4524 5 : if (STARTS_WITH(pszUrl, pszPrefix))
4525 : {
4526 : const CPLStringList aosTokens(
4527 6 : CSLTokenizeString2(pszUrl + strlen(pszPrefix), "/", 0));
4528 3 : if (aosTokens.size() == 2)
4529 : {
4530 2 : osUrl = "https://spatialreference.org/ref/";
4531 2 : osUrl += aosTokens[0]; // authority
4532 2 : osUrl += '/';
4533 2 : osUrl += aosTokens[1]; // code
4534 2 : osUrl += "/ogcwkt/";
4535 : }
4536 3 : break;
4537 : }
4538 : }
4539 :
4540 3 : const char *pszTimeout = "TIMEOUT=10";
4541 3 : char *apszOptions[] = {const_cast<char *>(pszTimeout), nullptr};
4542 :
4543 3 : CPLHTTPResult *psResult = CPLHTTPFetch(osUrl.c_str(), apszOptions);
4544 :
4545 : /* -------------------------------------------------------------------- */
4546 : /* Try to handle errors. */
4547 : /* -------------------------------------------------------------------- */
4548 :
4549 3 : if (psResult == nullptr)
4550 0 : return OGRERR_FAILURE;
4551 6 : if (psResult->nDataLen == 0 || CPLGetLastErrorNo() != 0 ||
4552 3 : psResult->pabyData == nullptr)
4553 : {
4554 0 : if (CPLGetLastErrorNo() == 0)
4555 : {
4556 0 : CPLError(CE_Failure, CPLE_AppDefined,
4557 : "No data was returned from the given URL");
4558 : }
4559 0 : CPLHTTPDestroyResult(psResult);
4560 0 : return OGRERR_FAILURE;
4561 : }
4562 :
4563 3 : if (psResult->nStatus != 0)
4564 : {
4565 0 : CPLError(CE_Failure, CPLE_AppDefined, "Curl reports error: %d: %s",
4566 : psResult->nStatus, psResult->pszErrBuf);
4567 0 : CPLHTTPDestroyResult(psResult);
4568 0 : return OGRERR_FAILURE;
4569 : }
4570 :
4571 3 : const char *pszData = reinterpret_cast<const char *>(psResult->pabyData);
4572 3 : if (STARTS_WITH_CI(pszData, "http://") ||
4573 3 : STARTS_WITH_CI(pszData, "https://"))
4574 : {
4575 0 : CPLError(CE_Failure, CPLE_AppDefined,
4576 : "The data that was downloaded also starts with 'http://' "
4577 : "and cannot be passed into SetFromUserInput. Is this "
4578 : "really a spatial reference definition? ");
4579 0 : CPLHTTPDestroyResult(psResult);
4580 0 : return OGRERR_FAILURE;
4581 : }
4582 3 : if (OGRERR_NONE != SetFromUserInput(pszData))
4583 : {
4584 0 : CPLHTTPDestroyResult(psResult);
4585 0 : return OGRERR_FAILURE;
4586 : }
4587 :
4588 3 : CPLHTTPDestroyResult(psResult);
4589 3 : return OGRERR_NONE;
4590 : }
4591 :
4592 : /************************************************************************/
4593 : /* OSRimportFromUrl() */
4594 : /************************************************************************/
4595 :
4596 : /**
4597 : * \brief Set spatial reference from a URL.
4598 : *
4599 : * This function is the same as OGRSpatialReference::importFromUrl()
4600 : */
4601 3 : OGRErr OSRImportFromUrl(OGRSpatialReferenceH hSRS, const char *pszUrl)
4602 :
4603 : {
4604 3 : VALIDATE_POINTER1(hSRS, "OSRImportFromUrl", OGRERR_FAILURE);
4605 :
4606 3 : return ToPointer(hSRS)->importFromUrl(pszUrl);
4607 : }
4608 :
4609 : /************************************************************************/
4610 : /* importFromURNPart() */
4611 : /************************************************************************/
4612 7359 : OGRErr OGRSpatialReference::importFromURNPart(const char *pszAuthority,
4613 : const char *pszCode,
4614 : const char *pszURN)
4615 : {
4616 : #if PROJ_AT_LEAST_VERSION(8, 1, 0)
4617 : (void)this;
4618 : (void)pszAuthority;
4619 : (void)pszCode;
4620 : (void)pszURN;
4621 : return OGRERR_FAILURE;
4622 : #else
4623 : /* -------------------------------------------------------------------- */
4624 : /* Is this an EPSG code? Note that we import it with EPSG */
4625 : /* preferred axis ordering for geographic coordinate systems. */
4626 : /* -------------------------------------------------------------------- */
4627 7359 : if (STARTS_WITH_CI(pszAuthority, "EPSG"))
4628 6262 : return importFromEPSGA(atoi(pszCode));
4629 :
4630 : /* -------------------------------------------------------------------- */
4631 : /* Is this an IAU code? Lets try for the IAU2000 dictionary. */
4632 : /* -------------------------------------------------------------------- */
4633 1097 : if (STARTS_WITH_CI(pszAuthority, "IAU"))
4634 0 : return importFromDict("IAU2000.wkt", pszCode);
4635 :
4636 : /* -------------------------------------------------------------------- */
4637 : /* Is this an OGC code? */
4638 : /* -------------------------------------------------------------------- */
4639 1097 : if (!STARTS_WITH_CI(pszAuthority, "OGC"))
4640 : {
4641 1 : CPLError(CE_Failure, CPLE_AppDefined,
4642 : "URN %s has unrecognized authority.", pszURN);
4643 1 : return OGRERR_FAILURE;
4644 : }
4645 :
4646 1096 : if (STARTS_WITH_CI(pszCode, "CRS84"))
4647 1084 : return SetWellKnownGeogCS(pszCode);
4648 12 : else if (STARTS_WITH_CI(pszCode, "CRS83"))
4649 0 : return SetWellKnownGeogCS(pszCode);
4650 12 : else if (STARTS_WITH_CI(pszCode, "CRS27"))
4651 0 : return SetWellKnownGeogCS(pszCode);
4652 12 : else if (STARTS_WITH_CI(pszCode, "84")) // urn:ogc:def:crs:OGC:2:84
4653 10 : return SetWellKnownGeogCS("CRS84");
4654 :
4655 : /* -------------------------------------------------------------------- */
4656 : /* Handle auto codes. We need to convert from format */
4657 : /* AUTO42001:99:8888 to format AUTO:42001,99,8888. */
4658 : /* -------------------------------------------------------------------- */
4659 2 : else if (STARTS_WITH_CI(pszCode, "AUTO"))
4660 : {
4661 2 : char szWMSAuto[100] = {'\0'};
4662 :
4663 2 : if (strlen(pszCode) > sizeof(szWMSAuto) - 2)
4664 0 : return OGRERR_FAILURE;
4665 :
4666 2 : snprintf(szWMSAuto, sizeof(szWMSAuto), "AUTO:%s", pszCode + 4);
4667 28 : for (int i = 5; szWMSAuto[i] != '\0'; i++)
4668 : {
4669 26 : if (szWMSAuto[i] == ':')
4670 4 : szWMSAuto[i] = ',';
4671 : }
4672 :
4673 2 : return importFromWMSAUTO(szWMSAuto);
4674 : }
4675 :
4676 : /* -------------------------------------------------------------------- */
4677 : /* Not a recognise OGC item. */
4678 : /* -------------------------------------------------------------------- */
4679 0 : CPLError(CE_Failure, CPLE_AppDefined, "URN %s value not supported.",
4680 : pszURN);
4681 :
4682 0 : return OGRERR_FAILURE;
4683 : #endif
4684 : }
4685 :
4686 : /************************************************************************/
4687 : /* importFromURN() */
4688 : /* */
4689 : /* See OGC recommendation paper 06-023r1 or later for details. */
4690 : /************************************************************************/
4691 :
4692 : /**
4693 : * \brief Initialize from OGC URN.
4694 : *
4695 : * Initializes this spatial reference from a coordinate system defined
4696 : * by an OGC URN prefixed with "urn:ogc:def:crs:" per recommendation
4697 : * paper 06-023r1. Currently EPSG and OGC authority values are supported,
4698 : * including OGC auto codes, but not including CRS1 or CRS88 (NAVD88).
4699 : *
4700 : * This method is also support through SetFromUserInput() which can
4701 : * normally be used for URNs.
4702 : *
4703 : * @param pszURN the urn string.
4704 : *
4705 : * @return OGRERR_NONE on success or an error code.
4706 : */
4707 :
4708 845 : OGRErr OGRSpatialReference::importFromURN(const char *pszURN)
4709 :
4710 : {
4711 845 : constexpr const char *EPSG_URN_CRS_PREFIX = "urn:ogc:def:crs:EPSG::";
4712 1605 : if (STARTS_WITH(pszURN, EPSG_URN_CRS_PREFIX) &&
4713 760 : CPLGetValueType(pszURN + strlen(EPSG_URN_CRS_PREFIX)) ==
4714 : CPL_VALUE_INTEGER)
4715 : {
4716 757 : return importFromEPSG(atoi(pszURN + strlen(EPSG_URN_CRS_PREFIX)));
4717 : }
4718 :
4719 176 : TAKE_OPTIONAL_LOCK();
4720 :
4721 : #if PROJ_AT_LEAST_VERSION(8, 1, 0)
4722 :
4723 : // PROJ 8.2.0 has support for IAU codes now.
4724 : #if !PROJ_AT_LEAST_VERSION(8, 2, 0)
4725 : /* -------------------------------------------------------------------- */
4726 : /* Is this an IAU code? Lets try for the IAU2000 dictionary. */
4727 : /* -------------------------------------------------------------------- */
4728 : const char *pszIAU = strstr(pszURN, "IAU");
4729 : if (pszIAU)
4730 : {
4731 : const char *pszCode = strchr(pszIAU, ':');
4732 : if (pszCode)
4733 : {
4734 : ++pszCode;
4735 : if (*pszCode == ':')
4736 : ++pszCode;
4737 : return importFromDict("IAU2000.wkt", pszCode);
4738 : }
4739 : }
4740 : #endif
4741 :
4742 : if (strlen(pszURN) >= 1000)
4743 : {
4744 : CPLError(CE_Failure, CPLE_AppDefined, "Too long input string");
4745 : return OGRERR_CORRUPT_DATA;
4746 : }
4747 : auto obj = proj_create(d->getPROJContext(), pszURN);
4748 : if (!obj)
4749 : {
4750 : return OGRERR_FAILURE;
4751 : }
4752 : Clear();
4753 : d->setPjCRS(obj);
4754 : return OGRERR_NONE;
4755 : #else
4756 88 : const char *pszCur = nullptr;
4757 :
4758 88 : if (STARTS_WITH_CI(pszURN, "urn:ogc:def:crs:"))
4759 23 : pszCur = pszURN + 16;
4760 65 : else if (STARTS_WITH_CI(pszURN, "urn:ogc:def:crs,crs:"))
4761 1 : pszCur = pszURN + 20;
4762 64 : else if (STARTS_WITH_CI(pszURN, "urn:x-ogc:def:crs:"))
4763 62 : pszCur = pszURN + 18;
4764 2 : else if (STARTS_WITH_CI(pszURN, "urn:opengis:crs:"))
4765 0 : pszCur = pszURN + 16;
4766 2 : else if (STARTS_WITH_CI(pszURN, "urn:opengis:def:crs:"))
4767 0 : pszCur = pszURN + 20;
4768 : else
4769 : {
4770 2 : CPLError(CE_Failure, CPLE_AppDefined, "URN %s not a supported format.",
4771 : pszURN);
4772 2 : return OGRERR_FAILURE;
4773 : }
4774 :
4775 : /* -------------------------------------------------------------------- */
4776 : /* Clear any existing definition. */
4777 : /* -------------------------------------------------------------------- */
4778 86 : Clear();
4779 :
4780 : /* -------------------------------------------------------------------- */
4781 : /* Find code (ignoring version) out of string like: */
4782 : /* */
4783 : /* authority:[version]:code */
4784 : /* -------------------------------------------------------------------- */
4785 86 : const char *pszAuthority = pszCur;
4786 :
4787 : // skip authority
4788 414 : while (*pszCur != ':' && *pszCur)
4789 328 : pszCur++;
4790 86 : if (*pszCur == ':')
4791 86 : pszCur++;
4792 :
4793 : // skip version
4794 86 : const char *pszBeforeVersion = pszCur;
4795 387 : while (*pszCur != ':' && *pszCur)
4796 301 : pszCur++;
4797 86 : if (*pszCur == ':')
4798 58 : pszCur++;
4799 : else
4800 : // We come here in the case, the content to parse is authority:code
4801 : // (instead of authority::code) which is probably illegal according to
4802 : // http://www.opengeospatial.org/ogcUrnPolicy but such content is found
4803 : // for example in what is returned by GeoServer.
4804 28 : pszCur = pszBeforeVersion;
4805 :
4806 86 : const char *pszCode = pszCur;
4807 :
4808 86 : const char *pszComma = strchr(pszCur, ',');
4809 86 : if (pszComma == nullptr)
4810 85 : return importFromURNPart(pszAuthority, pszCode, pszURN);
4811 :
4812 : // There's a second part with the vertical SRS.
4813 1 : pszCur = pszComma + 1;
4814 1 : if (!STARTS_WITH(pszCur, "crs:"))
4815 : {
4816 0 : CPLError(CE_Failure, CPLE_AppDefined, "URN %s not a supported format.",
4817 : pszURN);
4818 0 : return OGRERR_FAILURE;
4819 : }
4820 :
4821 1 : pszCur += 4;
4822 :
4823 1 : char *pszFirstCode = CPLStrdup(pszCode);
4824 1 : pszFirstCode[pszComma - pszCode] = '\0';
4825 1 : OGRErr eStatus = importFromURNPart(pszAuthority, pszFirstCode, pszURN);
4826 1 : CPLFree(pszFirstCode);
4827 :
4828 : // Do we want to turn this into a compound definition
4829 : // with a vertical datum?
4830 1 : if (eStatus != OGRERR_NONE)
4831 0 : return eStatus;
4832 :
4833 : /* -------------------------------------------------------------------- */
4834 : /* Find code (ignoring version) out of string like: */
4835 : /* */
4836 : /* authority:[version]:code */
4837 : /* -------------------------------------------------------------------- */
4838 1 : pszAuthority = pszCur;
4839 :
4840 : // skip authority
4841 5 : while (*pszCur != ':' && *pszCur)
4842 4 : pszCur++;
4843 1 : if (*pszCur == ':')
4844 1 : pszCur++;
4845 :
4846 : // skip version
4847 1 : pszBeforeVersion = pszCur;
4848 1 : while (*pszCur != ':' && *pszCur)
4849 0 : pszCur++;
4850 1 : if (*pszCur == ':')
4851 1 : pszCur++;
4852 : else
4853 0 : pszCur = pszBeforeVersion;
4854 :
4855 1 : pszCode = pszCur;
4856 :
4857 2 : OGRSpatialReference oVertSRS;
4858 1 : eStatus = oVertSRS.importFromURNPart(pszAuthority, pszCode, pszURN);
4859 1 : if (eStatus == OGRERR_NONE)
4860 : {
4861 1 : OGRSpatialReference oHorizSRS(*this);
4862 :
4863 1 : Clear();
4864 :
4865 1 : oHorizSRS.d->refreshProjObj();
4866 1 : oVertSRS.d->refreshProjObj();
4867 1 : if (!oHorizSRS.d->m_pj_crs || !oVertSRS.d->m_pj_crs)
4868 0 : return OGRERR_FAILURE;
4869 :
4870 1 : const char *pszHorizName = proj_get_name(oHorizSRS.d->m_pj_crs);
4871 1 : const char *pszVertName = proj_get_name(oVertSRS.d->m_pj_crs);
4872 :
4873 2 : CPLString osName = pszHorizName ? pszHorizName : "";
4874 1 : osName += " + ";
4875 1 : osName += pszVertName ? pszVertName : "";
4876 :
4877 1 : SetCompoundCS(osName, &oHorizSRS, &oVertSRS);
4878 : }
4879 :
4880 1 : return eStatus;
4881 : #endif
4882 : }
4883 :
4884 : /************************************************************************/
4885 : /* importFromCRSURL() */
4886 : /* */
4887 : /* See OGC Best Practice document 11-135 for details. */
4888 : /************************************************************************/
4889 :
4890 : /**
4891 : * \brief Initialize from OGC URL.
4892 : *
4893 : * Initializes this spatial reference from a coordinate system defined
4894 : * by an OGC URL prefixed with "http://opengis.net/def/crs" per best practice
4895 : * paper 11-135. Currently EPSG and OGC authority values are supported,
4896 : * including OGC auto codes, but not including CRS1 or CRS88 (NAVD88).
4897 : *
4898 : * This method is also supported through SetFromUserInput() which can
4899 : * normally be used for URLs.
4900 : *
4901 : * @param pszURL the URL string.
4902 : *
4903 : * @return OGRERR_NONE on success or an error code.
4904 : */
4905 :
4906 7285 : OGRErr OGRSpatialReference::importFromCRSURL(const char *pszURL)
4907 :
4908 : {
4909 14570 : TAKE_OPTIONAL_LOCK();
4910 :
4911 : #if !PROJ_AT_LEAST_VERSION(9, 1, 0)
4912 7285 : if (strcmp(pszURL, "http://www.opengis.net/def/crs/OGC/0/CRS84h") == 0)
4913 : {
4914 12 : PJ *obj = proj_create(
4915 : d->getPROJContext(),
4916 : "GEOGCRS[\"WGS 84 longitude-latitude-height\",\n"
4917 : " ENSEMBLE[\"World Geodetic System 1984 ensemble\",\n"
4918 : " MEMBER[\"World Geodetic System 1984 (Transit)\"],\n"
4919 : " MEMBER[\"World Geodetic System 1984 (G730)\"],\n"
4920 : " MEMBER[\"World Geodetic System 1984 (G873)\"],\n"
4921 : " MEMBER[\"World Geodetic System 1984 (G1150)\"],\n"
4922 : " MEMBER[\"World Geodetic System 1984 (G1674)\"],\n"
4923 : " MEMBER[\"World Geodetic System 1984 (G1762)\"],\n"
4924 : " MEMBER[\"World Geodetic System 1984 (G2139)\"],\n"
4925 : " MEMBER[\"World Geodetic System 1984 (G2296)\"],\n"
4926 : " ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n"
4927 : " LENGTHUNIT[\"metre\",1]],\n"
4928 : " ENSEMBLEACCURACY[2.0]],\n"
4929 : " PRIMEM[\"Greenwich\",0,\n"
4930 : " ANGLEUNIT[\"degree\",0.0174532925199433]],\n"
4931 : " CS[ellipsoidal,3],\n"
4932 : " AXIS[\"geodetic longitude (Lon)\",east,\n"
4933 : " ORDER[1],\n"
4934 : " ANGLEUNIT[\"degree\",0.0174532925199433]],\n"
4935 : " AXIS[\"geodetic latitude (Lat)\",north,\n"
4936 : " ORDER[2],\n"
4937 : " ANGLEUNIT[\"degree\",0.0174532925199433]],\n"
4938 : " AXIS[\"ellipsoidal height (h)\",up,\n"
4939 : " ORDER[3],\n"
4940 : " LENGTHUNIT[\"metre\",1]],\n"
4941 : " USAGE[\n"
4942 : " SCOPE[\"3D system frequently used in GIS, Web APIs and "
4943 : "Web applications\"],\n"
4944 : " AREA[\"World.\"],\n"
4945 : " BBOX[-90,-180,90,180]],\n"
4946 : " ID[\"OGC\",\"CRS84h\"]]");
4947 12 : if (!obj)
4948 : {
4949 0 : return OGRERR_FAILURE;
4950 : }
4951 12 : Clear();
4952 12 : d->setPjCRS(obj);
4953 12 : return OGRERR_NONE;
4954 : }
4955 : #endif
4956 :
4957 : #if PROJ_AT_LEAST_VERSION(8, 1, 0)
4958 : if (strlen(pszURL) >= 10000)
4959 : {
4960 : CPLError(CE_Failure, CPLE_AppDefined, "Too long input string");
4961 : return OGRERR_CORRUPT_DATA;
4962 : }
4963 :
4964 : PJ *obj;
4965 : #if !PROJ_AT_LEAST_VERSION(9, 2, 0)
4966 : if (STARTS_WITH(pszURL, "http://www.opengis.net/def/crs/IAU/0/"))
4967 : {
4968 : obj = proj_create(
4969 : d->getPROJContext(),
4970 : CPLSPrintf("IAU:%s",
4971 : pszURL +
4972 : strlen("http://www.opengis.net/def/crs/IAU/0/")));
4973 : }
4974 : else
4975 : #endif
4976 : {
4977 : obj = proj_create(d->getPROJContext(), pszURL);
4978 : }
4979 : if (!obj)
4980 : {
4981 : return OGRERR_FAILURE;
4982 : }
4983 : Clear();
4984 : d->setPjCRS(obj);
4985 : return OGRERR_NONE;
4986 : #else
4987 7273 : const char *pszCur = nullptr;
4988 :
4989 7273 : if (STARTS_WITH_CI(pszURL, "http://opengis.net/def/crs"))
4990 2 : pszCur = pszURL + 26;
4991 7271 : else if (STARTS_WITH_CI(pszURL, "https://opengis.net/def/crs"))
4992 1 : pszCur = pszURL + 27;
4993 7270 : else if (STARTS_WITH_CI(pszURL, "http://www.opengis.net/def/crs"))
4994 7269 : pszCur = pszURL + 30;
4995 1 : else if (STARTS_WITH_CI(pszURL, "https://www.opengis.net/def/crs"))
4996 1 : pszCur = pszURL + 31;
4997 0 : else if (STARTS_WITH_CI(pszURL, "www.opengis.net/def/crs"))
4998 0 : pszCur = pszURL + 23;
4999 : else
5000 : {
5001 0 : CPLError(CE_Failure, CPLE_AppDefined, "URL %s not a supported format.",
5002 : pszURL);
5003 0 : return OGRERR_FAILURE;
5004 : }
5005 :
5006 7273 : if (*pszCur == '\0')
5007 : {
5008 0 : CPLError(CE_Failure, CPLE_AppDefined, "URL %s malformed.", pszURL);
5009 0 : return OGRERR_FAILURE;
5010 : }
5011 :
5012 : /* -------------------------------------------------------------------- */
5013 : /* Clear any existing definition. */
5014 : /* -------------------------------------------------------------------- */
5015 7273 : Clear();
5016 :
5017 7273 : if (STARTS_WITH_CI(pszCur, "-compound?1="))
5018 : {
5019 : /* --------------------------------------------------------------------
5020 : */
5021 : /* It's a compound CRS, of the form: */
5022 : /* */
5023 : /* http://opengis.net/def/crs-compound?1=URL1&2=URL2&3=URL3&.. */
5024 : /* --------------------------------------------------------------------
5025 : */
5026 1 : pszCur += 12;
5027 :
5028 : // Extract each component CRS URL.
5029 1 : int iComponentUrl = 2;
5030 :
5031 2 : CPLString osName = "";
5032 1 : Clear();
5033 :
5034 3 : while (iComponentUrl != -1)
5035 : {
5036 2 : char searchStr[15] = {};
5037 2 : snprintf(searchStr, sizeof(searchStr), "&%d=", iComponentUrl);
5038 :
5039 2 : const char *pszUrlEnd = strstr(pszCur, searchStr);
5040 :
5041 : // Figure out the next component URL.
5042 2 : char *pszComponentUrl = nullptr;
5043 :
5044 2 : if (pszUrlEnd)
5045 : {
5046 1 : size_t nLen = pszUrlEnd - pszCur;
5047 1 : pszComponentUrl = static_cast<char *>(CPLMalloc(nLen + 1));
5048 1 : strncpy(pszComponentUrl, pszCur, nLen);
5049 1 : pszComponentUrl[nLen] = '\0';
5050 :
5051 1 : ++iComponentUrl;
5052 1 : pszCur += nLen + strlen(searchStr);
5053 : }
5054 : else
5055 : {
5056 1 : if (iComponentUrl == 2)
5057 : {
5058 0 : CPLError(CE_Failure, CPLE_AppDefined,
5059 : "Compound CRS URLs must have at least two "
5060 : "component CRSs.");
5061 0 : return OGRERR_FAILURE;
5062 : }
5063 : else
5064 : {
5065 1 : pszComponentUrl = CPLStrdup(pszCur);
5066 : // no more components
5067 1 : iComponentUrl = -1;
5068 : }
5069 : }
5070 :
5071 2 : OGRSpatialReference oComponentSRS;
5072 2 : OGRErr eStatus = oComponentSRS.importFromCRSURL(pszComponentUrl);
5073 :
5074 2 : CPLFree(pszComponentUrl);
5075 2 : pszComponentUrl = nullptr;
5076 :
5077 2 : if (eStatus == OGRERR_NONE)
5078 : {
5079 2 : if (osName.length() != 0)
5080 : {
5081 1 : osName += " + ";
5082 : }
5083 2 : osName += oComponentSRS.GetRoot()->GetValue();
5084 2 : SetNode("COMPD_CS", osName);
5085 2 : GetRoot()->AddChild(oComponentSRS.GetRoot()->Clone());
5086 : }
5087 : else
5088 0 : return eStatus;
5089 : }
5090 :
5091 1 : return OGRERR_NONE;
5092 : }
5093 :
5094 : /* -------------------------------------------------------------------- */
5095 : /* It's a normal CRS URL, of the form: */
5096 : /* */
5097 : /* http://opengis.net/def/crs/AUTHORITY/VERSION/CODE */
5098 : /* -------------------------------------------------------------------- */
5099 7272 : ++pszCur;
5100 7272 : const char *pszAuthority = pszCur;
5101 :
5102 : // skip authority
5103 135276 : while (*pszCur != '/' && *pszCur)
5104 128004 : pszCur++;
5105 7272 : if (*pszCur == '/')
5106 7271 : pszCur++;
5107 :
5108 : // skip version
5109 16675 : while (*pszCur != '/' && *pszCur)
5110 9403 : pszCur++;
5111 7272 : if (*pszCur == '/')
5112 7271 : pszCur++;
5113 :
5114 7272 : const char *pszCode = pszCur;
5115 :
5116 7272 : return importFromURNPart(pszAuthority, pszCode, pszURL);
5117 : #endif
5118 : }
5119 :
5120 : /************************************************************************/
5121 : /* importFromWMSAUTO() */
5122 : /************************************************************************/
5123 :
5124 : /**
5125 : * \brief Initialize from WMSAUTO string.
5126 : *
5127 : * Note that the WMS 1.3 specification does not include the
5128 : * units code, while apparently earlier specs do. We try to
5129 : * guess around this.
5130 : *
5131 : * @param pszDefinition the WMSAUTO string
5132 : *
5133 : * @return OGRERR_NONE on success or an error code.
5134 : */
5135 3 : OGRErr OGRSpatialReference::importFromWMSAUTO(const char *pszDefinition)
5136 :
5137 : {
5138 6 : TAKE_OPTIONAL_LOCK();
5139 :
5140 : #if PROJ_AT_LEAST_VERSION(8, 1, 0)
5141 : if (strlen(pszDefinition) >= 10000)
5142 : {
5143 : CPLError(CE_Failure, CPLE_AppDefined, "Too long input string");
5144 : return OGRERR_CORRUPT_DATA;
5145 : }
5146 :
5147 : auto obj = proj_create(d->getPROJContext(), pszDefinition);
5148 : if (!obj)
5149 : {
5150 : return OGRERR_FAILURE;
5151 : }
5152 : Clear();
5153 : d->setPjCRS(obj);
5154 : return OGRERR_NONE;
5155 : #else
5156 : int nProjId, nUnitsId;
5157 3 : double dfRefLong, dfRefLat = 0.0;
5158 :
5159 : /* -------------------------------------------------------------------- */
5160 : /* Tokenize */
5161 : /* -------------------------------------------------------------------- */
5162 3 : if (STARTS_WITH_CI(pszDefinition, "AUTO:"))
5163 3 : pszDefinition += 5;
5164 :
5165 : char **papszTokens =
5166 3 : CSLTokenizeStringComplex(pszDefinition, ",", FALSE, TRUE);
5167 :
5168 3 : if (CSLCount(papszTokens) == 4)
5169 : {
5170 0 : nProjId = atoi(papszTokens[0]);
5171 0 : nUnitsId = atoi(papszTokens[1]);
5172 0 : dfRefLong = CPLAtof(papszTokens[2]);
5173 0 : dfRefLat = CPLAtof(papszTokens[3]);
5174 : }
5175 3 : else if (CSLCount(papszTokens) == 3 && atoi(papszTokens[0]) == 42005)
5176 : {
5177 0 : nProjId = atoi(papszTokens[0]);
5178 0 : nUnitsId = atoi(papszTokens[1]);
5179 0 : dfRefLong = CPLAtof(papszTokens[2]);
5180 0 : dfRefLat = 0.0;
5181 : }
5182 3 : else if (CSLCount(papszTokens) == 3)
5183 : {
5184 2 : nProjId = atoi(papszTokens[0]);
5185 2 : nUnitsId = 9001;
5186 2 : dfRefLong = CPLAtof(papszTokens[1]);
5187 2 : dfRefLat = CPLAtof(papszTokens[2]);
5188 : }
5189 1 : else if (CSLCount(papszTokens) == 2 && atoi(papszTokens[0]) == 42005)
5190 : {
5191 0 : nProjId = atoi(papszTokens[0]);
5192 0 : nUnitsId = 9001;
5193 0 : dfRefLong = CPLAtof(papszTokens[1]);
5194 : }
5195 : else
5196 : {
5197 1 : CSLDestroy(papszTokens);
5198 1 : CPLError(CE_Failure, CPLE_AppDefined,
5199 : "AUTO projection has wrong number of arguments, expected\n"
5200 : "AUTO:proj_id,units_id,ref_long,ref_lat or"
5201 : "AUTO:proj_id,ref_long,ref_lat");
5202 1 : return OGRERR_FAILURE;
5203 : }
5204 :
5205 2 : CSLDestroy(papszTokens);
5206 2 : papszTokens = nullptr;
5207 :
5208 : /* -------------------------------------------------------------------- */
5209 : /* Build coordsys. */
5210 : /* -------------------------------------------------------------------- */
5211 2 : Clear();
5212 :
5213 : /* -------------------------------------------------------------------- */
5214 : /* Set WGS84. */
5215 : /* -------------------------------------------------------------------- */
5216 2 : SetWellKnownGeogCS("WGS84");
5217 :
5218 2 : switch (nProjId)
5219 : {
5220 2 : case 42001: // Auto UTM
5221 2 : SetUTM(static_cast<int>(floor((dfRefLong + 180.0) / 6.0)) + 1,
5222 : dfRefLat >= 0.0);
5223 2 : break;
5224 :
5225 0 : case 42002: // Auto TM (strangely very UTM-like).
5226 0 : SetTM(0, dfRefLong, 0.9996, 500000.0,
5227 : (dfRefLat >= 0.0) ? 0.0 : 10000000.0);
5228 0 : break;
5229 :
5230 0 : case 42003: // Auto Orthographic.
5231 0 : SetOrthographic(dfRefLat, dfRefLong, 0.0, 0.0);
5232 0 : break;
5233 :
5234 0 : case 42004: // Auto Equirectangular
5235 0 : SetEquirectangular(dfRefLat, dfRefLong, 0.0, 0.0);
5236 0 : break;
5237 :
5238 0 : case 42005:
5239 0 : SetMollweide(dfRefLong, 0.0, 0.0);
5240 0 : break;
5241 :
5242 0 : default:
5243 0 : CPLError(CE_Failure, CPLE_AppDefined,
5244 : "Unsupported projection id in importFromWMSAUTO(): %d",
5245 : nProjId);
5246 0 : return OGRERR_FAILURE;
5247 : }
5248 :
5249 : /* -------------------------------------------------------------------- */
5250 : /* Set units. */
5251 : /* -------------------------------------------------------------------- */
5252 :
5253 2 : switch (nUnitsId)
5254 : {
5255 2 : case 9001:
5256 2 : SetTargetLinearUnits(nullptr, SRS_UL_METER, 1.0, "EPSG", "9001");
5257 2 : break;
5258 :
5259 0 : case 9002:
5260 0 : SetTargetLinearUnits(nullptr, "Foot", 0.3048, "EPSG", "9002");
5261 0 : break;
5262 :
5263 0 : case 9003:
5264 0 : SetTargetLinearUnits(nullptr, "US survey foot",
5265 : CPLAtof(SRS_UL_US_FOOT_CONV), "EPSG", "9003");
5266 0 : break;
5267 :
5268 0 : default:
5269 0 : CPLError(CE_Failure, CPLE_AppDefined,
5270 : "Unsupported units code (%d).", nUnitsId);
5271 0 : return OGRERR_FAILURE;
5272 : break;
5273 : }
5274 :
5275 2 : return OGRERR_NONE;
5276 : #endif
5277 : }
5278 :
5279 : /************************************************************************/
5280 : /* GetSemiMajor() */
5281 : /************************************************************************/
5282 :
5283 : /**
5284 : * \brief Get spheroid semi major axis (in metres starting with GDAL 3.0)
5285 : *
5286 : * This method does the same thing as the C function OSRGetSemiMajor().
5287 : *
5288 : * @param pnErr if non-NULL set to OGRERR_FAILURE if semi major axis
5289 : * can be found.
5290 : *
5291 : * @return semi-major axis, or SRS_WGS84_SEMIMAJOR if it can't be found.
5292 : */
5293 :
5294 6980 : double OGRSpatialReference::GetSemiMajor(OGRErr *pnErr) const
5295 :
5296 : {
5297 13960 : TAKE_OPTIONAL_LOCK();
5298 :
5299 6980 : if (pnErr != nullptr)
5300 3612 : *pnErr = OGRERR_FAILURE;
5301 :
5302 6980 : d->refreshProjObj();
5303 6980 : if (!d->m_pj_crs)
5304 111 : return SRS_WGS84_SEMIMAJOR;
5305 :
5306 6869 : auto ellps = proj_get_ellipsoid(d->getPROJContext(), d->m_pj_crs);
5307 6869 : if (!ellps)
5308 5 : return SRS_WGS84_SEMIMAJOR;
5309 :
5310 6864 : double dfSemiMajor = 0.0;
5311 6864 : proj_ellipsoid_get_parameters(d->getPROJContext(), ellps, &dfSemiMajor,
5312 : nullptr, nullptr, nullptr);
5313 6864 : proj_destroy(ellps);
5314 :
5315 6864 : if (dfSemiMajor > 0)
5316 : {
5317 6864 : if (pnErr != nullptr)
5318 3498 : *pnErr = OGRERR_NONE;
5319 6864 : return dfSemiMajor;
5320 : }
5321 :
5322 0 : return SRS_WGS84_SEMIMAJOR;
5323 : }
5324 :
5325 : /************************************************************************/
5326 : /* OSRGetSemiMajor() */
5327 : /************************************************************************/
5328 :
5329 : /**
5330 : * \brief Get spheroid semi major axis.
5331 : *
5332 : * This function is the same as OGRSpatialReference::GetSemiMajor()
5333 : */
5334 87 : double OSRGetSemiMajor(OGRSpatialReferenceH hSRS, OGRErr *pnErr)
5335 :
5336 : {
5337 87 : VALIDATE_POINTER1(hSRS, "OSRGetSemiMajor", 0);
5338 :
5339 87 : return ToPointer(hSRS)->GetSemiMajor(pnErr);
5340 : }
5341 :
5342 : /************************************************************************/
5343 : /* GetInvFlattening() */
5344 : /************************************************************************/
5345 :
5346 : /**
5347 : * \brief Get spheroid inverse flattening.
5348 : *
5349 : * This method does the same thing as the C function OSRGetInvFlattening().
5350 : *
5351 : * @param pnErr if non-NULL set to OGRERR_FAILURE if no inverse flattening
5352 : * can be found.
5353 : *
5354 : * @return inverse flattening, or SRS_WGS84_INVFLATTENING if it can't be found.
5355 : */
5356 :
5357 4588 : double OGRSpatialReference::GetInvFlattening(OGRErr *pnErr) const
5358 :
5359 : {
5360 9176 : TAKE_OPTIONAL_LOCK();
5361 :
5362 4588 : if (pnErr != nullptr)
5363 3502 : *pnErr = OGRERR_FAILURE;
5364 :
5365 4588 : d->refreshProjObj();
5366 4588 : if (!d->m_pj_crs)
5367 111 : return SRS_WGS84_INVFLATTENING;
5368 :
5369 4477 : auto ellps = proj_get_ellipsoid(d->getPROJContext(), d->m_pj_crs);
5370 4477 : if (!ellps)
5371 2 : return SRS_WGS84_INVFLATTENING;
5372 :
5373 4475 : double dfInvFlattening = -1.0;
5374 4475 : proj_ellipsoid_get_parameters(d->getPROJContext(), ellps, nullptr, nullptr,
5375 : nullptr, &dfInvFlattening);
5376 4475 : proj_destroy(ellps);
5377 :
5378 4475 : if (dfInvFlattening >= 0.0)
5379 : {
5380 4475 : if (pnErr != nullptr)
5381 3391 : *pnErr = OGRERR_NONE;
5382 4475 : return dfInvFlattening;
5383 : }
5384 :
5385 0 : return SRS_WGS84_INVFLATTENING;
5386 : }
5387 :
5388 : /************************************************************************/
5389 : /* OSRGetInvFlattening() */
5390 : /************************************************************************/
5391 :
5392 : /**
5393 : * \brief Get spheroid inverse flattening.
5394 : *
5395 : * This function is the same as OGRSpatialReference::GetInvFlattening()
5396 : */
5397 10 : double OSRGetInvFlattening(OGRSpatialReferenceH hSRS, OGRErr *pnErr)
5398 :
5399 : {
5400 10 : VALIDATE_POINTER1(hSRS, "OSRGetInvFlattening", 0);
5401 :
5402 10 : return ToPointer(hSRS)->GetInvFlattening(pnErr);
5403 : }
5404 :
5405 : /************************************************************************/
5406 : /* GetEccentricity() */
5407 : /************************************************************************/
5408 :
5409 : /**
5410 : * \brief Get spheroid eccentricity
5411 : *
5412 : * @return eccentricity (or -1 in case of error)
5413 : */
5414 :
5415 0 : double OGRSpatialReference::GetEccentricity() const
5416 :
5417 : {
5418 0 : OGRErr eErr = OGRERR_NONE;
5419 0 : const double dfInvFlattening = GetInvFlattening(&eErr);
5420 0 : if (eErr != OGRERR_NONE)
5421 : {
5422 0 : return -1.0;
5423 : }
5424 0 : if (dfInvFlattening == 0.0)
5425 0 : return 0.0;
5426 0 : if (dfInvFlattening < 0.5)
5427 0 : return -1.0;
5428 0 : return sqrt(2.0 / dfInvFlattening -
5429 0 : 1.0 / (dfInvFlattening * dfInvFlattening));
5430 : }
5431 :
5432 : /************************************************************************/
5433 : /* GetSquaredEccentricity() */
5434 : /************************************************************************/
5435 :
5436 : /**
5437 : * \brief Get spheroid squared eccentricity
5438 : *
5439 : * @return squared eccentricity (or -1 in case of error)
5440 : */
5441 :
5442 0 : double OGRSpatialReference::GetSquaredEccentricity() const
5443 :
5444 : {
5445 0 : OGRErr eErr = OGRERR_NONE;
5446 0 : const double dfInvFlattening = GetInvFlattening(&eErr);
5447 0 : if (eErr != OGRERR_NONE)
5448 : {
5449 0 : return -1.0;
5450 : }
5451 0 : if (dfInvFlattening == 0.0)
5452 0 : return 0.0;
5453 0 : if (dfInvFlattening < 0.5)
5454 0 : return -1.0;
5455 0 : return 2.0 / dfInvFlattening - 1.0 / (dfInvFlattening * dfInvFlattening);
5456 : }
5457 :
5458 : /************************************************************************/
5459 : /* GetSemiMinor() */
5460 : /************************************************************************/
5461 :
5462 : /**
5463 : * \brief Get spheroid semi minor axis.
5464 : *
5465 : * This method does the same thing as the C function OSRGetSemiMinor().
5466 : *
5467 : * @param pnErr if non-NULL set to OGRERR_FAILURE if semi minor axis
5468 : * can be found.
5469 : *
5470 : * @return semi-minor axis, or WGS84 semi minor if it can't be found.
5471 : */
5472 :
5473 651 : double OGRSpatialReference::GetSemiMinor(OGRErr *pnErr) const
5474 :
5475 : {
5476 651 : const double dfSemiMajor = GetSemiMajor(pnErr);
5477 651 : const double dfInvFlattening = GetInvFlattening(pnErr);
5478 :
5479 651 : return OSRCalcSemiMinorFromInvFlattening(dfSemiMajor, dfInvFlattening);
5480 : }
5481 :
5482 : /************************************************************************/
5483 : /* OSRGetSemiMinor() */
5484 : /************************************************************************/
5485 :
5486 : /**
5487 : * \brief Get spheroid semi minor axis.
5488 : *
5489 : * This function is the same as OGRSpatialReference::GetSemiMinor()
5490 : */
5491 4 : double OSRGetSemiMinor(OGRSpatialReferenceH hSRS, OGRErr *pnErr)
5492 :
5493 : {
5494 4 : VALIDATE_POINTER1(hSRS, "OSRGetSemiMinor", 0);
5495 :
5496 4 : return ToPointer(hSRS)->GetSemiMinor(pnErr);
5497 : }
5498 :
5499 : /************************************************************************/
5500 : /* SetLocalCS() */
5501 : /************************************************************************/
5502 :
5503 : /**
5504 : * \brief Set the user visible LOCAL_CS name.
5505 : *
5506 : * This method is the same as the C function OSRSetLocalCS().
5507 : *
5508 : * This method will ensure a LOCAL_CS node is created as the root,
5509 : * and set the provided name on it. It must be used before SetLinearUnits().
5510 : *
5511 : * @param pszName the user visible name to assign. Not used as a key.
5512 : *
5513 : * @return OGRERR_NONE on success.
5514 : */
5515 :
5516 2900 : OGRErr OGRSpatialReference::SetLocalCS(const char *pszName)
5517 :
5518 : {
5519 5800 : TAKE_OPTIONAL_LOCK();
5520 :
5521 2900 : if (d->m_pjType == PJ_TYPE_UNKNOWN ||
5522 0 : d->m_pjType == PJ_TYPE_ENGINEERING_CRS)
5523 : {
5524 2900 : d->setPjCRS(proj_create_engineering_crs(d->getPROJContext(), pszName));
5525 : }
5526 : else
5527 : {
5528 0 : CPLDebug("OGR",
5529 : "OGRSpatialReference::SetLocalCS(%s) failed. "
5530 : "It appears an incompatible object already exists.",
5531 : pszName);
5532 0 : return OGRERR_FAILURE;
5533 : }
5534 :
5535 2900 : return OGRERR_NONE;
5536 : }
5537 :
5538 : /************************************************************************/
5539 : /* OSRSetLocalCS() */
5540 : /************************************************************************/
5541 :
5542 : /**
5543 : * \brief Set the user visible LOCAL_CS name.
5544 : *
5545 : * This function is the same as OGRSpatialReference::SetLocalCS()
5546 : */
5547 1 : OGRErr OSRSetLocalCS(OGRSpatialReferenceH hSRS, const char *pszName)
5548 :
5549 : {
5550 1 : VALIDATE_POINTER1(hSRS, "OSRSetLocalCS", OGRERR_FAILURE);
5551 :
5552 1 : return ToPointer(hSRS)->SetLocalCS(pszName);
5553 : }
5554 :
5555 : /************************************************************************/
5556 : /* SetGeocCS() */
5557 : /************************************************************************/
5558 :
5559 : /**
5560 : * \brief Set the user visible GEOCCS name.
5561 : *
5562 : * This method is the same as the C function OSRSetGeocCS().
5563 :
5564 : * This method will ensure a GEOCCS node is created as the root,
5565 : * and set the provided name on it. If used on a GEOGCS coordinate system,
5566 : * the DATUM and PRIMEM nodes from the GEOGCS will be transferred over to
5567 : * the GEOGCS.
5568 : *
5569 : * @param pszName the user visible name to assign. Not used as a key.
5570 : *
5571 : * @return OGRERR_NONE on success.
5572 : *
5573 : */
5574 :
5575 6 : OGRErr OGRSpatialReference::SetGeocCS(const char *pszName)
5576 :
5577 : {
5578 12 : TAKE_OPTIONAL_LOCK();
5579 :
5580 6 : OGRErr eErr = OGRERR_NONE;
5581 6 : d->refreshProjObj();
5582 6 : d->demoteFromBoundCRS();
5583 6 : if (d->m_pjType == PJ_TYPE_UNKNOWN)
5584 : {
5585 3 : d->setPjCRS(proj_create_geocentric_crs(
5586 : d->getPROJContext(), pszName, "World Geodetic System 1984",
5587 : "WGS 84", SRS_WGS84_SEMIMAJOR, SRS_WGS84_INVFLATTENING,
5588 : SRS_PM_GREENWICH, 0.0, SRS_UA_DEGREE, CPLAtof(SRS_UA_DEGREE_CONV),
5589 : "Metre", 1.0));
5590 : }
5591 3 : else if (d->m_pjType == PJ_TYPE_GEOCENTRIC_CRS)
5592 : {
5593 1 : d->setPjCRS(proj_alter_name(d->getPROJContext(), d->m_pj_crs, pszName));
5594 : }
5595 3 : else if (d->m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
5596 1 : d->m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS)
5597 : {
5598 1 : auto datum = proj_crs_get_datum(d->getPROJContext(), d->m_pj_crs);
5599 : #if PROJ_VERSION_MAJOR > 7 || \
5600 : (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
5601 : if (datum == nullptr)
5602 : {
5603 : datum =
5604 : proj_crs_get_datum_ensemble(d->getPROJContext(), d->m_pj_crs);
5605 : }
5606 : #endif
5607 1 : if (datum == nullptr)
5608 : {
5609 0 : d->undoDemoteFromBoundCRS();
5610 0 : return OGRERR_FAILURE;
5611 : }
5612 :
5613 1 : auto pj_crs = proj_create_geocentric_crs_from_datum(
5614 1 : d->getPROJContext(), proj_get_name(d->m_pj_crs), datum, nullptr,
5615 : 0.0);
5616 1 : d->setPjCRS(pj_crs);
5617 :
5618 1 : proj_destroy(datum);
5619 : }
5620 : else
5621 : {
5622 1 : CPLDebug("OGR",
5623 : "OGRSpatialReference::SetGeocCS(%s) failed. "
5624 : "It appears an incompatible object already exists.",
5625 : pszName);
5626 1 : eErr = OGRERR_FAILURE;
5627 : }
5628 6 : d->undoDemoteFromBoundCRS();
5629 :
5630 6 : return eErr;
5631 : }
5632 :
5633 : /************************************************************************/
5634 : /* OSRSetGeocCS() */
5635 : /************************************************************************/
5636 :
5637 : /**
5638 : * \brief Set the user visible PROJCS name.
5639 : *
5640 : * This function is the same as OGRSpatialReference::SetGeocCS()
5641 : *
5642 : */
5643 4 : OGRErr OSRSetGeocCS(OGRSpatialReferenceH hSRS, const char *pszName)
5644 :
5645 : {
5646 4 : VALIDATE_POINTER1(hSRS, "OSRSetGeocCS", OGRERR_FAILURE);
5647 :
5648 4 : return ToPointer(hSRS)->SetGeocCS(pszName);
5649 : }
5650 :
5651 : /************************************************************************/
5652 : /* SetVertCS() */
5653 : /************************************************************************/
5654 :
5655 : /**
5656 : * \brief Set the user visible VERT_CS name.
5657 : *
5658 : * This method is the same as the C function OSRSetVertCS().
5659 :
5660 : * This method will ensure a VERT_CS node is created if needed. If the
5661 : * existing coordinate system is GEOGCS or PROJCS rooted, then it will be
5662 : * turned into a COMPD_CS.
5663 : *
5664 : * @param pszVertCSName the user visible name of the vertical coordinate
5665 : * system. Not used as a key.
5666 : *
5667 : * @param pszVertDatumName the user visible name of the vertical datum. It
5668 : * is helpful if this matches the EPSG name.
5669 : *
5670 : * @param nVertDatumType the OGC vertical datum type. Ignored
5671 : *
5672 : * @return OGRERR_NONE on success.
5673 : *
5674 : */
5675 :
5676 1 : OGRErr OGRSpatialReference::SetVertCS(const char *pszVertCSName,
5677 : const char *pszVertDatumName,
5678 : int nVertDatumType)
5679 :
5680 : {
5681 1 : TAKE_OPTIONAL_LOCK();
5682 :
5683 1 : CPL_IGNORE_RET_VAL(nVertDatumType);
5684 :
5685 1 : d->refreshProjObj();
5686 :
5687 1 : auto vertCRS = proj_create_vertical_crs(d->getPROJContext(), pszVertCSName,
5688 : pszVertDatumName, nullptr, 0.0);
5689 :
5690 : /* -------------------------------------------------------------------- */
5691 : /* Handle the case where we want to make a compound coordinate */
5692 : /* system. */
5693 : /* -------------------------------------------------------------------- */
5694 1 : if (IsProjected() || IsGeographic())
5695 : {
5696 1 : auto compoundCRS = proj_create_compound_crs(
5697 1 : d->getPROJContext(), nullptr, d->m_pj_crs, vertCRS);
5698 1 : proj_destroy(vertCRS);
5699 1 : d->setPjCRS(compoundCRS);
5700 : }
5701 : else
5702 : {
5703 0 : d->setPjCRS(vertCRS);
5704 : }
5705 2 : return OGRERR_NONE;
5706 : }
5707 :
5708 : /************************************************************************/
5709 : /* OSRSetVertCS() */
5710 : /************************************************************************/
5711 :
5712 : /**
5713 : * \brief Setup the vertical coordinate system.
5714 : *
5715 : * This function is the same as OGRSpatialReference::SetVertCS()
5716 : *
5717 : */
5718 0 : OGRErr OSRSetVertCS(OGRSpatialReferenceH hSRS, const char *pszVertCSName,
5719 : const char *pszVertDatumName, int nVertDatumType)
5720 :
5721 : {
5722 0 : VALIDATE_POINTER1(hSRS, "OSRSetVertCS", OGRERR_FAILURE);
5723 :
5724 0 : return ToPointer(hSRS)->SetVertCS(pszVertCSName, pszVertDatumName,
5725 0 : nVertDatumType);
5726 : }
5727 :
5728 : /************************************************************************/
5729 : /* SetCompoundCS() */
5730 : /************************************************************************/
5731 :
5732 : /**
5733 : * \brief Setup a compound coordinate system.
5734 : *
5735 : * This method is the same as the C function OSRSetCompoundCS().
5736 :
5737 : * This method is replace the current SRS with a COMPD_CS coordinate system
5738 : * consisting of the passed in horizontal and vertical coordinate systems.
5739 : *
5740 : * @param pszName the name of the compound coordinate system.
5741 : *
5742 : * @param poHorizSRS the horizontal SRS (PROJCS or GEOGCS).
5743 : *
5744 : * @param poVertSRS the vertical SRS (VERT_CS).
5745 : *
5746 : * @return OGRERR_NONE on success.
5747 : */
5748 :
5749 183 : OGRErr OGRSpatialReference::SetCompoundCS(const char *pszName,
5750 : const OGRSpatialReference *poHorizSRS,
5751 : const OGRSpatialReference *poVertSRS)
5752 :
5753 : {
5754 366 : TAKE_OPTIONAL_LOCK();
5755 :
5756 : /* -------------------------------------------------------------------- */
5757 : /* Verify these are legal horizontal and vertical coordinate */
5758 : /* systems. */
5759 : /* -------------------------------------------------------------------- */
5760 183 : if (!poVertSRS->IsVertical())
5761 : {
5762 0 : CPLError(CE_Failure, CPLE_AppDefined,
5763 : "SetCompoundCS() fails, vertical component is not VERT_CS.");
5764 0 : return OGRERR_FAILURE;
5765 : }
5766 183 : if (!poHorizSRS->IsProjected() && !poHorizSRS->IsGeographic())
5767 : {
5768 0 : CPLError(CE_Failure, CPLE_AppDefined,
5769 : "SetCompoundCS() fails, horizontal component is not PROJCS or "
5770 : "GEOGCS.");
5771 0 : return OGRERR_FAILURE;
5772 : }
5773 :
5774 : /* -------------------------------------------------------------------- */
5775 : /* Replace with compound srs. */
5776 : /* -------------------------------------------------------------------- */
5777 183 : Clear();
5778 :
5779 183 : auto compoundCRS = proj_create_compound_crs(d->getPROJContext(), pszName,
5780 183 : poHorizSRS->d->m_pj_crs,
5781 183 : poVertSRS->d->m_pj_crs);
5782 183 : d->setPjCRS(compoundCRS);
5783 :
5784 183 : return OGRERR_NONE;
5785 : }
5786 :
5787 : /************************************************************************/
5788 : /* OSRSetCompoundCS() */
5789 : /************************************************************************/
5790 :
5791 : /**
5792 : * \brief Setup a compound coordinate system.
5793 : *
5794 : * This function is the same as OGRSpatialReference::SetCompoundCS()
5795 : */
5796 8 : OGRErr OSRSetCompoundCS(OGRSpatialReferenceH hSRS, const char *pszName,
5797 : OGRSpatialReferenceH hHorizSRS,
5798 : OGRSpatialReferenceH hVertSRS)
5799 :
5800 : {
5801 8 : VALIDATE_POINTER1(hSRS, "OSRSetCompoundCS", OGRERR_FAILURE);
5802 8 : VALIDATE_POINTER1(hHorizSRS, "OSRSetCompoundCS", OGRERR_FAILURE);
5803 8 : VALIDATE_POINTER1(hVertSRS, "OSRSetCompoundCS", OGRERR_FAILURE);
5804 :
5805 16 : return ToPointer(hSRS)->SetCompoundCS(pszName, ToPointer(hHorizSRS),
5806 16 : ToPointer(hVertSRS));
5807 : }
5808 :
5809 : /************************************************************************/
5810 : /* SetProjCS() */
5811 : /************************************************************************/
5812 :
5813 : /**
5814 : * \brief Set the user visible PROJCS name.
5815 : *
5816 : * This method is the same as the C function OSRSetProjCS().
5817 : *
5818 : * This method will ensure a PROJCS node is created as the root,
5819 : * and set the provided name on it. If used on a GEOGCS coordinate system,
5820 : * the GEOGCS node will be demoted to be a child of the new PROJCS root.
5821 : *
5822 : * @param pszName the user visible name to assign. Not used as a key.
5823 : *
5824 : * @return OGRERR_NONE on success.
5825 : */
5826 :
5827 4896 : OGRErr OGRSpatialReference::SetProjCS(const char *pszName)
5828 :
5829 : {
5830 4896 : TAKE_OPTIONAL_LOCK();
5831 :
5832 4896 : d->refreshProjObj();
5833 4896 : d->demoteFromBoundCRS();
5834 4896 : if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
5835 : {
5836 489 : d->setPjCRS(proj_alter_name(d->getPROJContext(), d->m_pj_crs, pszName));
5837 : }
5838 : else
5839 : {
5840 4407 : auto dummyConv = proj_create_conversion(d->getPROJContext(), nullptr,
5841 : nullptr, nullptr, nullptr,
5842 : nullptr, nullptr, 0, nullptr);
5843 4407 : auto cs = proj_create_cartesian_2D_cs(
5844 : d->getPROJContext(), PJ_CART2D_EASTING_NORTHING, nullptr, 0);
5845 :
5846 4407 : auto projCRS = proj_create_projected_crs(
5847 4407 : d->getPROJContext(), pszName, d->getGeodBaseCRS(), dummyConv, cs);
5848 4407 : proj_destroy(dummyConv);
5849 4407 : proj_destroy(cs);
5850 :
5851 4407 : d->setPjCRS(projCRS);
5852 : }
5853 4896 : d->undoDemoteFromBoundCRS();
5854 9792 : return OGRERR_NONE;
5855 : }
5856 :
5857 : /************************************************************************/
5858 : /* OSRSetProjCS() */
5859 : /************************************************************************/
5860 :
5861 : /**
5862 : * \brief Set the user visible PROJCS name.
5863 : *
5864 : * This function is the same as OGRSpatialReference::SetProjCS()
5865 : */
5866 1 : OGRErr OSRSetProjCS(OGRSpatialReferenceH hSRS, const char *pszName)
5867 :
5868 : {
5869 1 : VALIDATE_POINTER1(hSRS, "OSRSetProjCS", OGRERR_FAILURE);
5870 :
5871 1 : return ToPointer(hSRS)->SetProjCS(pszName);
5872 : }
5873 :
5874 : /************************************************************************/
5875 : /* SetProjection() */
5876 : /************************************************************************/
5877 :
5878 : /**
5879 : * \brief Set a projection name.
5880 : *
5881 : * This method is the same as the C function OSRSetProjection().
5882 : *
5883 : * @param pszProjection the projection name, which should be selected from
5884 : * the macros in ogr_srs_api.h, such as SRS_PT_TRANSVERSE_MERCATOR.
5885 : *
5886 : * @return OGRERR_NONE on success.
5887 : */
5888 :
5889 23 : OGRErr OGRSpatialReference::SetProjection(const char *pszProjection)
5890 :
5891 : {
5892 46 : TAKE_OPTIONAL_LOCK();
5893 :
5894 23 : OGR_SRSNode *poGeogCS = nullptr;
5895 :
5896 23 : if (GetRoot() != nullptr && EQUAL(d->m_poRoot->GetValue(), "GEOGCS"))
5897 : {
5898 4 : poGeogCS = d->m_poRoot;
5899 4 : d->m_poRoot = nullptr;
5900 : }
5901 :
5902 23 : if (!GetAttrNode("PROJCS"))
5903 : {
5904 11 : SetNode("PROJCS", "unnamed");
5905 : }
5906 :
5907 23 : const OGRErr eErr = SetNode("PROJCS|PROJECTION", pszProjection);
5908 23 : if (eErr != OGRERR_NONE)
5909 0 : return eErr;
5910 :
5911 23 : if (poGeogCS != nullptr)
5912 4 : d->m_poRoot->InsertChild(poGeogCS, 1);
5913 :
5914 23 : return OGRERR_NONE;
5915 : }
5916 :
5917 : /************************************************************************/
5918 : /* OSRSetProjection() */
5919 : /************************************************************************/
5920 :
5921 : /**
5922 : * \brief Set a projection name.
5923 : *
5924 : * This function is the same as OGRSpatialReference::SetProjection()
5925 : */
5926 0 : OGRErr OSRSetProjection(OGRSpatialReferenceH hSRS, const char *pszProjection)
5927 :
5928 : {
5929 0 : VALIDATE_POINTER1(hSRS, "OSRSetProjection", OGRERR_FAILURE);
5930 :
5931 0 : return ToPointer(hSRS)->SetProjection(pszProjection);
5932 : }
5933 :
5934 : /************************************************************************/
5935 : /* GetWKT2ProjectionMethod() */
5936 : /************************************************************************/
5937 :
5938 : /**
5939 : * \brief Returns info on the projection method, based on WKT2 naming
5940 : * conventions.
5941 : *
5942 : * The returned strings are short lived and should be considered to be
5943 : * invalidated by any further call to the GDAL API.
5944 : *
5945 : * @param[out] ppszMethodName Pointer to a string that will receive the
5946 : * projection method name.
5947 : * @param[out] ppszMethodAuthName null pointer, or pointer to a string that will
5948 : * receive the name of the authority that defines the projection method.
5949 : * *ppszMethodAuthName may be nullptr if the projection method is not linked to
5950 : * an authority.
5951 : * @param[out] ppszMethodCode null pointer, or pointer to a string that will
5952 : * receive the code that defines the projection method.
5953 : * *ppszMethodCode may be nullptr if the projection method is not linked to
5954 : * an authority.
5955 : *
5956 : * @return OGRERR_NONE on success.
5957 : */
5958 : OGRErr
5959 1 : OGRSpatialReference::GetWKT2ProjectionMethod(const char **ppszMethodName,
5960 : const char **ppszMethodAuthName,
5961 : const char **ppszMethodCode) const
5962 : {
5963 2 : TAKE_OPTIONAL_LOCK();
5964 :
5965 1 : auto conv = proj_crs_get_coordoperation(d->getPROJContext(), d->m_pj_crs);
5966 1 : if (!conv)
5967 0 : return OGRERR_FAILURE;
5968 1 : const char *pszTmpMethodName = "";
5969 1 : const char *pszTmpMethodAuthName = "";
5970 1 : const char *pszTmpMethodCode = "";
5971 1 : int ret = proj_coordoperation_get_method_info(
5972 : d->getPROJContext(), conv, &pszTmpMethodName, &pszTmpMethodAuthName,
5973 : &pszTmpMethodCode);
5974 : // "Internalize" temporary strings returned by PROJ
5975 1 : CPLAssert(pszTmpMethodName);
5976 1 : if (ppszMethodName)
5977 1 : *ppszMethodName = CPLSPrintf("%s", pszTmpMethodName);
5978 1 : if (ppszMethodAuthName)
5979 0 : *ppszMethodAuthName = pszTmpMethodAuthName
5980 0 : ? CPLSPrintf("%s", pszTmpMethodAuthName)
5981 0 : : nullptr;
5982 1 : if (ppszMethodCode)
5983 0 : *ppszMethodCode =
5984 0 : pszTmpMethodCode ? CPLSPrintf("%s", pszTmpMethodCode) : nullptr;
5985 1 : proj_destroy(conv);
5986 1 : return ret ? OGRERR_NONE : OGRERR_FAILURE;
5987 : }
5988 :
5989 : /************************************************************************/
5990 : /* SetProjParm() */
5991 : /************************************************************************/
5992 :
5993 : /**
5994 : * \brief Set a projection parameter value.
5995 : *
5996 : * Adds a new PARAMETER under the PROJCS with the indicated name and value.
5997 : *
5998 : * This method is the same as the C function OSRSetProjParm().
5999 : *
6000 : * Please check https://gdal.org/proj_list pages for
6001 : * legal parameter names for specific projections.
6002 : *
6003 : *
6004 : * @param pszParamName the parameter name, which should be selected from
6005 : * the macros in ogr_srs_api.h, such as SRS_PP_CENTRAL_MERIDIAN.
6006 : *
6007 : * @param dfValue value to assign.
6008 : *
6009 : * @return OGRERR_NONE on success.
6010 : */
6011 :
6012 129 : OGRErr OGRSpatialReference::SetProjParm(const char *pszParamName,
6013 : double dfValue)
6014 :
6015 : {
6016 258 : TAKE_OPTIONAL_LOCK();
6017 :
6018 129 : OGR_SRSNode *poPROJCS = GetAttrNode("PROJCS");
6019 :
6020 129 : if (poPROJCS == nullptr)
6021 3 : return OGRERR_FAILURE;
6022 :
6023 126 : char szValue[64] = {'\0'};
6024 126 : OGRsnPrintDouble(szValue, sizeof(szValue), dfValue);
6025 :
6026 : /* -------------------------------------------------------------------- */
6027 : /* Try to find existing parameter with this name. */
6028 : /* -------------------------------------------------------------------- */
6029 1030 : for (int iChild = 0; iChild < poPROJCS->GetChildCount(); iChild++)
6030 : {
6031 943 : OGR_SRSNode *poParam = poPROJCS->GetChild(iChild);
6032 :
6033 1242 : if (EQUAL(poParam->GetValue(), "PARAMETER") &&
6034 1242 : poParam->GetChildCount() == 2 &&
6035 299 : EQUAL(poParam->GetChild(0)->GetValue(), pszParamName))
6036 : {
6037 39 : poParam->GetChild(1)->SetValue(szValue);
6038 39 : return OGRERR_NONE;
6039 : }
6040 : }
6041 :
6042 : /* -------------------------------------------------------------------- */
6043 : /* Otherwise create a new parameter and append. */
6044 : /* -------------------------------------------------------------------- */
6045 87 : OGR_SRSNode *poParam = new OGR_SRSNode("PARAMETER");
6046 87 : poParam->AddChild(new OGR_SRSNode(pszParamName));
6047 87 : poParam->AddChild(new OGR_SRSNode(szValue));
6048 :
6049 87 : poPROJCS->AddChild(poParam);
6050 :
6051 87 : return OGRERR_NONE;
6052 : }
6053 :
6054 : /************************************************************************/
6055 : /* OSRSetProjParm() */
6056 : /************************************************************************/
6057 :
6058 : /**
6059 : * \brief Set a projection parameter value.
6060 : *
6061 : * This function is the same as OGRSpatialReference::SetProjParm()
6062 : */
6063 0 : OGRErr OSRSetProjParm(OGRSpatialReferenceH hSRS, const char *pszParamName,
6064 : double dfValue)
6065 :
6066 : {
6067 0 : VALIDATE_POINTER1(hSRS, "OSRSetProjParm", OGRERR_FAILURE);
6068 :
6069 0 : return ToPointer(hSRS)->SetProjParm(pszParamName, dfValue);
6070 : }
6071 :
6072 : /************************************************************************/
6073 : /* FindProjParm() */
6074 : /************************************************************************/
6075 :
6076 : /**
6077 : * \brief Return the child index of the named projection parameter on
6078 : * its parent PROJCS node.
6079 : *
6080 : * @param pszParameter projection parameter to look for
6081 : * @param poPROJCS projection CS node to look in. If NULL is passed,
6082 : * the PROJCS node of the SpatialReference object will be searched.
6083 : *
6084 : * @return the child index of the named projection parameter. -1 on failure
6085 : */
6086 5179 : int OGRSpatialReference::FindProjParm(const char *pszParameter,
6087 : const OGR_SRSNode *poPROJCS) const
6088 :
6089 : {
6090 10358 : TAKE_OPTIONAL_LOCK();
6091 :
6092 5179 : if (poPROJCS == nullptr)
6093 0 : poPROJCS = GetAttrNode("PROJCS");
6094 :
6095 5179 : if (poPROJCS == nullptr)
6096 0 : return -1;
6097 :
6098 : /* -------------------------------------------------------------------- */
6099 : /* Search for requested parameter. */
6100 : /* -------------------------------------------------------------------- */
6101 5179 : bool bIsWKT2 = false;
6102 34154 : for (int iChild = 0; iChild < poPROJCS->GetChildCount(); iChild++)
6103 : {
6104 33463 : const OGR_SRSNode *poParameter = poPROJCS->GetChild(iChild);
6105 :
6106 33463 : if (poParameter->GetChildCount() >= 2)
6107 : {
6108 23146 : const char *pszValue = poParameter->GetValue();
6109 38140 : if (EQUAL(pszValue, "PARAMETER") &&
6110 14994 : EQUAL(poPROJCS->GetChild(iChild)->GetChild(0)->GetValue(),
6111 : pszParameter))
6112 : {
6113 4488 : return iChild;
6114 : }
6115 18658 : else if (EQUAL(pszValue, "METHOD"))
6116 : {
6117 41 : bIsWKT2 = true;
6118 : }
6119 : }
6120 : }
6121 :
6122 : /* -------------------------------------------------------------------- */
6123 : /* Try similar names, for selected parameters. */
6124 : /* -------------------------------------------------------------------- */
6125 691 : if (EQUAL(pszParameter, SRS_PP_LATITUDE_OF_ORIGIN))
6126 : {
6127 324 : if (bIsWKT2)
6128 : {
6129 8 : int iChild = FindProjParm(
6130 : EPSG_NAME_PARAMETER_LATITUDE_OF_NATURAL_ORIGIN, poPROJCS);
6131 8 : if (iChild == -1)
6132 3 : iChild = FindProjParm(
6133 : EPSG_NAME_PARAMETER_LATITUDE_PROJECTION_CENTRE, poPROJCS);
6134 8 : return iChild;
6135 : }
6136 316 : return FindProjParm(SRS_PP_LATITUDE_OF_CENTER, poPROJCS);
6137 : }
6138 :
6139 367 : if (EQUAL(pszParameter, SRS_PP_CENTRAL_MERIDIAN))
6140 : {
6141 38 : if (bIsWKT2)
6142 : {
6143 9 : int iChild = FindProjParm(
6144 : EPSG_NAME_PARAMETER_LONGITUDE_OF_NATURAL_ORIGIN, poPROJCS);
6145 9 : if (iChild == -1)
6146 0 : iChild = FindProjParm(
6147 : EPSG_NAME_PARAMETER_LONGITUDE_PROJECTION_CENTRE, poPROJCS);
6148 9 : return iChild;
6149 : }
6150 29 : int iChild = FindProjParm(SRS_PP_LONGITUDE_OF_CENTER, poPROJCS);
6151 29 : if (iChild == -1)
6152 0 : iChild = FindProjParm(SRS_PP_LONGITUDE_OF_ORIGIN, poPROJCS);
6153 29 : return iChild;
6154 : }
6155 :
6156 329 : return -1;
6157 : }
6158 :
6159 : /************************************************************************/
6160 : /* GetProjParm() */
6161 : /************************************************************************/
6162 :
6163 : /**
6164 : * \brief Fetch a projection parameter value.
6165 : *
6166 : * NOTE: This code should be modified to translate non degree angles into
6167 : * degrees based on the GEOGCS unit. This has not yet been done.
6168 : *
6169 : * This method is the same as the C function OSRGetProjParm().
6170 : *
6171 : * @param pszName the name of the parameter to fetch, from the set of
6172 : * SRS_PP codes in ogr_srs_api.h.
6173 : *
6174 : * @param dfDefaultValue the value to return if this parameter doesn't exist.
6175 : *
6176 : * @param pnErr place to put error code on failure. Ignored if NULL.
6177 : *
6178 : * @return value of parameter.
6179 : */
6180 :
6181 5072 : double OGRSpatialReference::GetProjParm(const char *pszName,
6182 : double dfDefaultValue,
6183 : OGRErr *pnErr) const
6184 :
6185 : {
6186 10144 : TAKE_OPTIONAL_LOCK();
6187 :
6188 5072 : d->refreshProjObj();
6189 5072 : GetRoot(); // force update of d->m_bNodesWKT2
6190 :
6191 5072 : if (pnErr != nullptr)
6192 4072 : *pnErr = OGRERR_NONE;
6193 :
6194 : /* -------------------------------------------------------------------- */
6195 : /* Find the desired parameter. */
6196 : /* -------------------------------------------------------------------- */
6197 : const OGR_SRSNode *poPROJCS =
6198 5072 : GetAttrNode(d->m_bNodesWKT2 ? "CONVERSION" : "PROJCS");
6199 5072 : if (poPROJCS == nullptr)
6200 : {
6201 258 : if (pnErr != nullptr)
6202 258 : *pnErr = OGRERR_FAILURE;
6203 258 : return dfDefaultValue;
6204 : }
6205 :
6206 4814 : const int iChild = FindProjParm(pszName, poPROJCS);
6207 4814 : if (iChild == -1)
6208 : {
6209 326 : if (IsProjected() && GetAxesCount() == 3)
6210 : {
6211 3 : OGRSpatialReference *poSRSTmp = Clone();
6212 3 : poSRSTmp->DemoteTo2D(nullptr);
6213 : const double dfRet =
6214 3 : poSRSTmp->GetProjParm(pszName, dfDefaultValue, pnErr);
6215 3 : delete poSRSTmp;
6216 3 : return dfRet;
6217 : }
6218 :
6219 323 : if (pnErr != nullptr)
6220 301 : *pnErr = OGRERR_FAILURE;
6221 323 : return dfDefaultValue;
6222 : }
6223 :
6224 4488 : const OGR_SRSNode *poParameter = poPROJCS->GetChild(iChild);
6225 4488 : return CPLAtof(poParameter->GetChild(1)->GetValue());
6226 : }
6227 :
6228 : /************************************************************************/
6229 : /* OSRGetProjParm() */
6230 : /************************************************************************/
6231 :
6232 : /**
6233 : * \brief Fetch a projection parameter value.
6234 : *
6235 : * This function is the same as OGRSpatialReference::GetProjParm()
6236 : */
6237 90 : double OSRGetProjParm(OGRSpatialReferenceH hSRS, const char *pszName,
6238 : double dfDefaultValue, OGRErr *pnErr)
6239 :
6240 : {
6241 90 : VALIDATE_POINTER1(hSRS, "OSRGetProjParm", 0);
6242 :
6243 90 : return ToPointer(hSRS)->GetProjParm(pszName, dfDefaultValue, pnErr);
6244 : }
6245 :
6246 : /************************************************************************/
6247 : /* GetNormProjParm() */
6248 : /************************************************************************/
6249 :
6250 : /**
6251 : * \brief Fetch a normalized projection parameter value.
6252 : *
6253 : * This method is the same as GetProjParm() except that the value of
6254 : * the parameter is "normalized" into degrees or meters depending on
6255 : * whether it is linear or angular.
6256 : *
6257 : * This method is the same as the C function OSRGetNormProjParm().
6258 : *
6259 : * @param pszName the name of the parameter to fetch, from the set of
6260 : * SRS_PP codes in ogr_srs_api.h.
6261 : *
6262 : * @param dfDefaultValue the value to return if this parameter doesn't exist.
6263 : *
6264 : * @param pnErr place to put error code on failure. Ignored if NULL.
6265 : *
6266 : * @return value of parameter.
6267 : */
6268 :
6269 4047 : double OGRSpatialReference::GetNormProjParm(const char *pszName,
6270 : double dfDefaultValue,
6271 : OGRErr *pnErr) const
6272 :
6273 : {
6274 8094 : TAKE_OPTIONAL_LOCK();
6275 :
6276 4047 : GetNormInfo();
6277 :
6278 4047 : OGRErr nError = OGRERR_NONE;
6279 4047 : double dfRawResult = GetProjParm(pszName, dfDefaultValue, &nError);
6280 4047 : if (pnErr != nullptr)
6281 0 : *pnErr = nError;
6282 :
6283 : // If we got the default just return it unadjusted.
6284 4047 : if (nError != OGRERR_NONE)
6285 559 : return dfRawResult;
6286 :
6287 3488 : if (d->dfToDegrees != 1.0 && IsAngularParameter(pszName))
6288 8 : dfRawResult *= d->dfToDegrees;
6289 :
6290 3488 : if (d->dfToMeter != 1.0 && IsLinearParameter(pszName))
6291 5 : return dfRawResult * d->dfToMeter;
6292 :
6293 3483 : return dfRawResult;
6294 : }
6295 :
6296 : /************************************************************************/
6297 : /* OSRGetNormProjParm() */
6298 : /************************************************************************/
6299 :
6300 : /**
6301 : * \brief This function is the same as OGRSpatialReference::
6302 : *
6303 : * This function is the same as OGRSpatialReference::GetNormProjParm()
6304 : */
6305 1 : double OSRGetNormProjParm(OGRSpatialReferenceH hSRS, const char *pszName,
6306 : double dfDefaultValue, OGRErr *pnErr)
6307 :
6308 : {
6309 1 : VALIDATE_POINTER1(hSRS, "OSRGetNormProjParm", 0);
6310 :
6311 1 : return ToPointer(hSRS)->GetNormProjParm(pszName, dfDefaultValue, pnErr);
6312 : }
6313 :
6314 : /************************************************************************/
6315 : /* SetNormProjParm() */
6316 : /************************************************************************/
6317 :
6318 : /**
6319 : * \brief Set a projection parameter with a normalized value.
6320 : *
6321 : * This method is the same as SetProjParm() except that the value of
6322 : * the parameter passed in is assumed to be in "normalized" form (decimal
6323 : * degrees for angular values, meters for linear values. The values are
6324 : * converted in a form suitable for the GEOGCS and linear units in effect.
6325 : *
6326 : * This method is the same as the C function OSRSetNormProjParm().
6327 : *
6328 : * @param pszName the parameter name, which should be selected from
6329 : * the macros in ogr_srs_api.h, such as SRS_PP_CENTRAL_MERIDIAN.
6330 : *
6331 : * @param dfValue value to assign.
6332 : *
6333 : * @return OGRERR_NONE on success.
6334 : */
6335 :
6336 91 : OGRErr OGRSpatialReference::SetNormProjParm(const char *pszName, double dfValue)
6337 :
6338 : {
6339 182 : TAKE_OPTIONAL_LOCK();
6340 :
6341 91 : GetNormInfo();
6342 :
6343 91 : if (d->dfToDegrees != 0.0 &&
6344 91 : (d->dfToDegrees != 1.0 || d->dfFromGreenwich != 0.0) &&
6345 0 : IsAngularParameter(pszName))
6346 : {
6347 0 : dfValue /= d->dfToDegrees;
6348 : }
6349 95 : else if (d->dfToMeter != 1.0 && d->dfToMeter != 0.0 &&
6350 4 : IsLinearParameter(pszName))
6351 4 : dfValue /= d->dfToMeter;
6352 :
6353 182 : return SetProjParm(pszName, dfValue);
6354 : }
6355 :
6356 : /************************************************************************/
6357 : /* OSRSetNormProjParm() */
6358 : /************************************************************************/
6359 :
6360 : /**
6361 : * \brief Set a projection parameter with a normalized value.
6362 : *
6363 : * This function is the same as OGRSpatialReference::SetNormProjParm()
6364 : */
6365 0 : OGRErr OSRSetNormProjParm(OGRSpatialReferenceH hSRS, const char *pszParamName,
6366 : double dfValue)
6367 :
6368 : {
6369 0 : VALIDATE_POINTER1(hSRS, "OSRSetNormProjParm", OGRERR_FAILURE);
6370 :
6371 0 : return ToPointer(hSRS)->SetNormProjParm(pszParamName, dfValue);
6372 : }
6373 :
6374 : /************************************************************************/
6375 : /* SetTM() */
6376 : /************************************************************************/
6377 :
6378 442 : OGRErr OGRSpatialReference::SetTM(double dfCenterLat, double dfCenterLong,
6379 : double dfScale, double dfFalseEasting,
6380 : double dfFalseNorthing)
6381 :
6382 : {
6383 884 : TAKE_OPTIONAL_LOCK();
6384 :
6385 442 : return d->replaceConversionAndUnref(
6386 : proj_create_conversion_transverse_mercator(
6387 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale,
6388 884 : dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6389 : }
6390 :
6391 : /************************************************************************/
6392 : /* OSRSetTM() */
6393 : /************************************************************************/
6394 :
6395 1 : OGRErr OSRSetTM(OGRSpatialReferenceH hSRS, double dfCenterLat,
6396 : double dfCenterLong, double dfScale, double dfFalseEasting,
6397 : double dfFalseNorthing)
6398 :
6399 : {
6400 1 : VALIDATE_POINTER1(hSRS, "OSRSetTM", OGRERR_FAILURE);
6401 :
6402 1 : return ToPointer(hSRS)->SetTM(dfCenterLat, dfCenterLong, dfScale,
6403 1 : dfFalseEasting, dfFalseNorthing);
6404 : }
6405 :
6406 : /************************************************************************/
6407 : /* SetTMVariant() */
6408 : /************************************************************************/
6409 :
6410 0 : OGRErr OGRSpatialReference::SetTMVariant(const char *pszVariantName,
6411 : double dfCenterLat,
6412 : double dfCenterLong, double dfScale,
6413 : double dfFalseEasting,
6414 : double dfFalseNorthing)
6415 :
6416 : {
6417 0 : TAKE_OPTIONAL_LOCK();
6418 :
6419 0 : SetProjection(pszVariantName);
6420 0 : SetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, dfCenterLat);
6421 0 : SetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, dfCenterLong);
6422 0 : SetNormProjParm(SRS_PP_SCALE_FACTOR, dfScale);
6423 0 : SetNormProjParm(SRS_PP_FALSE_EASTING, dfFalseEasting);
6424 0 : SetNormProjParm(SRS_PP_FALSE_NORTHING, dfFalseNorthing);
6425 :
6426 0 : return OGRERR_NONE;
6427 : }
6428 :
6429 : /************************************************************************/
6430 : /* OSRSetTMVariant() */
6431 : /************************************************************************/
6432 :
6433 0 : OGRErr OSRSetTMVariant(OGRSpatialReferenceH hSRS, const char *pszVariantName,
6434 : double dfCenterLat, double dfCenterLong, double dfScale,
6435 : double dfFalseEasting, double dfFalseNorthing)
6436 :
6437 : {
6438 0 : VALIDATE_POINTER1(hSRS, "OSRSetTMVariant", OGRERR_FAILURE);
6439 :
6440 0 : return ToPointer(hSRS)->SetTMVariant(pszVariantName, dfCenterLat,
6441 : dfCenterLong, dfScale, dfFalseEasting,
6442 0 : dfFalseNorthing);
6443 : }
6444 :
6445 : /************************************************************************/
6446 : /* SetTMSO() */
6447 : /************************************************************************/
6448 :
6449 3 : OGRErr OGRSpatialReference::SetTMSO(double dfCenterLat, double dfCenterLong,
6450 : double dfScale, double dfFalseEasting,
6451 : double dfFalseNorthing)
6452 :
6453 : {
6454 6 : TAKE_OPTIONAL_LOCK();
6455 :
6456 3 : auto conv = proj_create_conversion_transverse_mercator_south_oriented(
6457 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale, dfFalseEasting,
6458 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6459 :
6460 3 : const char *pszName = nullptr;
6461 3 : double dfConvFactor = GetTargetLinearUnits(nullptr, &pszName);
6462 3 : CPLString osName = pszName ? pszName : "";
6463 :
6464 3 : d->refreshProjObj();
6465 :
6466 3 : d->demoteFromBoundCRS();
6467 :
6468 3 : auto cs = proj_create_cartesian_2D_cs(
6469 : d->getPROJContext(), PJ_CART2D_WESTING_SOUTHING,
6470 3 : !osName.empty() ? osName.c_str() : nullptr, dfConvFactor);
6471 : auto projCRS =
6472 3 : proj_create_projected_crs(d->getPROJContext(), d->getProjCRSName(),
6473 3 : d->getGeodBaseCRS(), conv, cs);
6474 3 : proj_destroy(conv);
6475 3 : proj_destroy(cs);
6476 :
6477 3 : d->setPjCRS(projCRS);
6478 :
6479 3 : d->undoDemoteFromBoundCRS();
6480 :
6481 6 : return OGRERR_NONE;
6482 : }
6483 :
6484 : /************************************************************************/
6485 : /* OSRSetTMSO() */
6486 : /************************************************************************/
6487 :
6488 0 : OGRErr OSRSetTMSO(OGRSpatialReferenceH hSRS, double dfCenterLat,
6489 : double dfCenterLong, double dfScale, double dfFalseEasting,
6490 : double dfFalseNorthing)
6491 :
6492 : {
6493 0 : VALIDATE_POINTER1(hSRS, "OSRSetTMSO", OGRERR_FAILURE);
6494 :
6495 0 : return ToPointer(hSRS)->SetTMSO(dfCenterLat, dfCenterLong, dfScale,
6496 0 : dfFalseEasting, dfFalseNorthing);
6497 : }
6498 :
6499 : /************************************************************************/
6500 : /* SetTPED() */
6501 : /************************************************************************/
6502 :
6503 1 : OGRErr OGRSpatialReference::SetTPED(double dfLat1, double dfLong1,
6504 : double dfLat2, double dfLong2,
6505 : double dfFalseEasting,
6506 : double dfFalseNorthing)
6507 :
6508 : {
6509 2 : TAKE_OPTIONAL_LOCK();
6510 :
6511 1 : return d->replaceConversionAndUnref(
6512 : proj_create_conversion_two_point_equidistant(
6513 : d->getPROJContext(), dfLat1, dfLong1, dfLat2, dfLong2,
6514 2 : dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6515 : }
6516 :
6517 : /************************************************************************/
6518 : /* OSRSetTPED() */
6519 : /************************************************************************/
6520 :
6521 0 : OGRErr OSRSetTPED(OGRSpatialReferenceH hSRS, double dfLat1, double dfLong1,
6522 : double dfLat2, double dfLong2, double dfFalseEasting,
6523 : double dfFalseNorthing)
6524 :
6525 : {
6526 0 : VALIDATE_POINTER1(hSRS, "OSRSetTPED", OGRERR_FAILURE);
6527 :
6528 0 : return ToPointer(hSRS)->SetTPED(dfLat1, dfLong1, dfLat2, dfLong2,
6529 0 : dfFalseEasting, dfFalseNorthing);
6530 : }
6531 :
6532 : /************************************************************************/
6533 : /* SetTMG() */
6534 : /************************************************************************/
6535 :
6536 0 : OGRErr OGRSpatialReference::SetTMG(double dfCenterLat, double dfCenterLong,
6537 : double dfFalseEasting,
6538 : double dfFalseNorthing)
6539 :
6540 : {
6541 0 : TAKE_OPTIONAL_LOCK();
6542 :
6543 0 : return d->replaceConversionAndUnref(
6544 : proj_create_conversion_tunisia_mapping_grid(
6545 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
6546 0 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6547 : }
6548 :
6549 : /************************************************************************/
6550 : /* OSRSetTMG() */
6551 : /************************************************************************/
6552 :
6553 0 : OGRErr OSRSetTMG(OGRSpatialReferenceH hSRS, double dfCenterLat,
6554 : double dfCenterLong, double dfFalseEasting,
6555 : double dfFalseNorthing)
6556 :
6557 : {
6558 0 : VALIDATE_POINTER1(hSRS, "OSRSetTMG", OGRERR_FAILURE);
6559 :
6560 0 : return ToPointer(hSRS)->SetTMG(dfCenterLat, dfCenterLong, dfFalseEasting,
6561 0 : dfFalseNorthing);
6562 : }
6563 :
6564 : /************************************************************************/
6565 : /* SetACEA() */
6566 : /************************************************************************/
6567 :
6568 39 : OGRErr OGRSpatialReference::SetACEA(double dfStdP1, double dfStdP2,
6569 : double dfCenterLat, double dfCenterLong,
6570 : double dfFalseEasting,
6571 : double dfFalseNorthing)
6572 :
6573 : {
6574 78 : TAKE_OPTIONAL_LOCK();
6575 :
6576 : // Note different order of parameters. The one in PROJ is conformant with
6577 : // EPSG
6578 39 : return d->replaceConversionAndUnref(
6579 : proj_create_conversion_albers_equal_area(
6580 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfStdP1, dfStdP2,
6581 78 : dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6582 : }
6583 :
6584 : /************************************************************************/
6585 : /* OSRSetACEA() */
6586 : /************************************************************************/
6587 :
6588 0 : OGRErr OSRSetACEA(OGRSpatialReferenceH hSRS, double dfStdP1, double dfStdP2,
6589 : double dfCenterLat, double dfCenterLong,
6590 : double dfFalseEasting, double dfFalseNorthing)
6591 :
6592 : {
6593 0 : VALIDATE_POINTER1(hSRS, "OSRSetACEA", OGRERR_FAILURE);
6594 :
6595 0 : return ToPointer(hSRS)->SetACEA(dfStdP1, dfStdP2, dfCenterLat, dfCenterLong,
6596 0 : dfFalseEasting, dfFalseNorthing);
6597 : }
6598 :
6599 : /************************************************************************/
6600 : /* SetAE() */
6601 : /************************************************************************/
6602 :
6603 21 : OGRErr OGRSpatialReference::SetAE(double dfCenterLat, double dfCenterLong,
6604 : double dfFalseEasting, double dfFalseNorthing)
6605 :
6606 : {
6607 42 : TAKE_OPTIONAL_LOCK();
6608 :
6609 21 : return d->replaceConversionAndUnref(
6610 : proj_create_conversion_azimuthal_equidistant(
6611 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
6612 42 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6613 : }
6614 :
6615 : /************************************************************************/
6616 : /* OSRSetAE() */
6617 : /************************************************************************/
6618 :
6619 0 : OGRErr OSRSetAE(OGRSpatialReferenceH hSRS, double dfCenterLat,
6620 : double dfCenterLong, double dfFalseEasting,
6621 : double dfFalseNorthing)
6622 :
6623 : {
6624 0 : VALIDATE_POINTER1(hSRS, "OSRSetACEA", OGRERR_FAILURE);
6625 :
6626 0 : return ToPointer(hSRS)->SetAE(dfCenterLat, dfCenterLong, dfFalseEasting,
6627 0 : dfFalseNorthing);
6628 : }
6629 :
6630 : /************************************************************************/
6631 : /* SetBonne() */
6632 : /************************************************************************/
6633 :
6634 1 : OGRErr OGRSpatialReference::SetBonne(double dfStdP1, double dfCentralMeridian,
6635 : double dfFalseEasting,
6636 : double dfFalseNorthing)
6637 :
6638 : {
6639 2 : TAKE_OPTIONAL_LOCK();
6640 :
6641 1 : return d->replaceConversionAndUnref(proj_create_conversion_bonne(
6642 : d->getPROJContext(), dfStdP1, dfCentralMeridian, dfFalseEasting,
6643 2 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6644 : }
6645 :
6646 : /************************************************************************/
6647 : /* OSRSetBonne() */
6648 : /************************************************************************/
6649 :
6650 0 : OGRErr OSRSetBonne(OGRSpatialReferenceH hSRS, double dfStdP1,
6651 : double dfCentralMeridian, double dfFalseEasting,
6652 : double dfFalseNorthing)
6653 :
6654 : {
6655 0 : VALIDATE_POINTER1(hSRS, "OSRSetBonne", OGRERR_FAILURE);
6656 :
6657 0 : return ToPointer(hSRS)->SetBonne(dfStdP1, dfCentralMeridian, dfFalseEasting,
6658 0 : dfFalseNorthing);
6659 : }
6660 :
6661 : /************************************************************************/
6662 : /* SetCEA() */
6663 : /************************************************************************/
6664 :
6665 4 : OGRErr OGRSpatialReference::SetCEA(double dfStdP1, double dfCentralMeridian,
6666 : double dfFalseEasting,
6667 : double dfFalseNorthing)
6668 :
6669 : {
6670 8 : TAKE_OPTIONAL_LOCK();
6671 :
6672 4 : return d->replaceConversionAndUnref(
6673 : proj_create_conversion_lambert_cylindrical_equal_area(
6674 : d->getPROJContext(), dfStdP1, dfCentralMeridian, dfFalseEasting,
6675 8 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6676 : }
6677 :
6678 : /************************************************************************/
6679 : /* OSRSetCEA() */
6680 : /************************************************************************/
6681 :
6682 0 : OGRErr OSRSetCEA(OGRSpatialReferenceH hSRS, double dfStdP1,
6683 : double dfCentralMeridian, double dfFalseEasting,
6684 : double dfFalseNorthing)
6685 :
6686 : {
6687 0 : VALIDATE_POINTER1(hSRS, "OSRSetCEA", OGRERR_FAILURE);
6688 :
6689 0 : return ToPointer(hSRS)->SetCEA(dfStdP1, dfCentralMeridian, dfFalseEasting,
6690 0 : dfFalseNorthing);
6691 : }
6692 :
6693 : /************************************************************************/
6694 : /* SetCS() */
6695 : /************************************************************************/
6696 :
6697 5 : OGRErr OGRSpatialReference::SetCS(double dfCenterLat, double dfCenterLong,
6698 : double dfFalseEasting, double dfFalseNorthing)
6699 :
6700 : {
6701 10 : TAKE_OPTIONAL_LOCK();
6702 :
6703 5 : return d->replaceConversionAndUnref(proj_create_conversion_cassini_soldner(
6704 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
6705 10 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6706 : }
6707 :
6708 : /************************************************************************/
6709 : /* OSRSetCS() */
6710 : /************************************************************************/
6711 :
6712 0 : OGRErr OSRSetCS(OGRSpatialReferenceH hSRS, double dfCenterLat,
6713 : double dfCenterLong, double dfFalseEasting,
6714 : double dfFalseNorthing)
6715 :
6716 : {
6717 0 : VALIDATE_POINTER1(hSRS, "OSRSetCS", OGRERR_FAILURE);
6718 :
6719 0 : return ToPointer(hSRS)->SetCS(dfCenterLat, dfCenterLong, dfFalseEasting,
6720 0 : dfFalseNorthing);
6721 : }
6722 :
6723 : /************************************************************************/
6724 : /* SetEC() */
6725 : /************************************************************************/
6726 :
6727 7 : OGRErr OGRSpatialReference::SetEC(double dfStdP1, double dfStdP2,
6728 : double dfCenterLat, double dfCenterLong,
6729 : double dfFalseEasting, double dfFalseNorthing)
6730 :
6731 : {
6732 14 : TAKE_OPTIONAL_LOCK();
6733 :
6734 : // Note: different order of arguments
6735 7 : return d->replaceConversionAndUnref(
6736 : proj_create_conversion_equidistant_conic(
6737 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfStdP1, dfStdP2,
6738 14 : dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6739 : }
6740 :
6741 : /************************************************************************/
6742 : /* OSRSetEC() */
6743 : /************************************************************************/
6744 :
6745 0 : OGRErr OSRSetEC(OGRSpatialReferenceH hSRS, double dfStdP1, double dfStdP2,
6746 : double dfCenterLat, double dfCenterLong, double dfFalseEasting,
6747 : double dfFalseNorthing)
6748 :
6749 : {
6750 0 : VALIDATE_POINTER1(hSRS, "OSRSetEC", OGRERR_FAILURE);
6751 :
6752 0 : return ToPointer(hSRS)->SetEC(dfStdP1, dfStdP2, dfCenterLat, dfCenterLong,
6753 0 : dfFalseEasting, dfFalseNorthing);
6754 : }
6755 :
6756 : /************************************************************************/
6757 : /* SetEckert() */
6758 : /************************************************************************/
6759 :
6760 10 : OGRErr OGRSpatialReference::SetEckert(int nVariation, // 1-6.
6761 : double dfCentralMeridian,
6762 : double dfFalseEasting,
6763 : double dfFalseNorthing)
6764 :
6765 : {
6766 20 : TAKE_OPTIONAL_LOCK();
6767 :
6768 : PJ *conv;
6769 10 : if (nVariation == 1)
6770 : {
6771 1 : conv = proj_create_conversion_eckert_i(
6772 : d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6773 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6774 : }
6775 9 : else if (nVariation == 2)
6776 : {
6777 1 : conv = proj_create_conversion_eckert_ii(
6778 : d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6779 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6780 : }
6781 8 : else if (nVariation == 3)
6782 : {
6783 1 : conv = proj_create_conversion_eckert_iii(
6784 : d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6785 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6786 : }
6787 7 : else if (nVariation == 4)
6788 : {
6789 3 : conv = proj_create_conversion_eckert_iv(
6790 : d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6791 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6792 : }
6793 4 : else if (nVariation == 5)
6794 : {
6795 1 : conv = proj_create_conversion_eckert_v(
6796 : d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6797 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6798 : }
6799 3 : else if (nVariation == 6)
6800 : {
6801 3 : conv = proj_create_conversion_eckert_vi(
6802 : d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6803 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6804 : }
6805 : else
6806 : {
6807 0 : CPLError(CE_Failure, CPLE_AppDefined,
6808 : "Unsupported Eckert variation (%d).", nVariation);
6809 0 : return OGRERR_UNSUPPORTED_SRS;
6810 : }
6811 :
6812 10 : return d->replaceConversionAndUnref(conv);
6813 : }
6814 :
6815 : /************************************************************************/
6816 : /* OSRSetEckert() */
6817 : /************************************************************************/
6818 :
6819 0 : OGRErr OSRSetEckert(OGRSpatialReferenceH hSRS, int nVariation,
6820 : double dfCentralMeridian, double dfFalseEasting,
6821 : double dfFalseNorthing)
6822 :
6823 : {
6824 0 : VALIDATE_POINTER1(hSRS, "OSRSetEckert", OGRERR_FAILURE);
6825 :
6826 0 : return ToPointer(hSRS)->SetEckert(nVariation, dfCentralMeridian,
6827 0 : dfFalseEasting, dfFalseNorthing);
6828 : }
6829 :
6830 : /************************************************************************/
6831 : /* SetEckertIV() */
6832 : /* */
6833 : /* Deprecated */
6834 : /************************************************************************/
6835 :
6836 2 : OGRErr OGRSpatialReference::SetEckertIV(double dfCentralMeridian,
6837 : double dfFalseEasting,
6838 : double dfFalseNorthing)
6839 :
6840 : {
6841 2 : return SetEckert(4, dfCentralMeridian, dfFalseEasting, dfFalseNorthing);
6842 : }
6843 :
6844 : /************************************************************************/
6845 : /* OSRSetEckertIV() */
6846 : /************************************************************************/
6847 :
6848 0 : OGRErr OSRSetEckertIV(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
6849 : double dfFalseEasting, double dfFalseNorthing)
6850 :
6851 : {
6852 0 : VALIDATE_POINTER1(hSRS, "OSRSetEckertIV", OGRERR_FAILURE);
6853 :
6854 0 : return ToPointer(hSRS)->SetEckertIV(dfCentralMeridian, dfFalseEasting,
6855 0 : dfFalseNorthing);
6856 : }
6857 :
6858 : /************************************************************************/
6859 : /* SetEckertVI() */
6860 : /* */
6861 : /* Deprecated */
6862 : /************************************************************************/
6863 :
6864 2 : OGRErr OGRSpatialReference::SetEckertVI(double dfCentralMeridian,
6865 : double dfFalseEasting,
6866 : double dfFalseNorthing)
6867 :
6868 : {
6869 2 : return SetEckert(6, dfCentralMeridian, dfFalseEasting, dfFalseNorthing);
6870 : }
6871 :
6872 : /************************************************************************/
6873 : /* OSRSetEckertVI() */
6874 : /************************************************************************/
6875 :
6876 0 : OGRErr OSRSetEckertVI(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
6877 : double dfFalseEasting, double dfFalseNorthing)
6878 :
6879 : {
6880 0 : VALIDATE_POINTER1(hSRS, "OSRSetEckertVI", OGRERR_FAILURE);
6881 :
6882 0 : return ToPointer(hSRS)->SetEckertVI(dfCentralMeridian, dfFalseEasting,
6883 0 : dfFalseNorthing);
6884 : }
6885 :
6886 : /************************************************************************/
6887 : /* SetEquirectangular() */
6888 : /************************************************************************/
6889 :
6890 2 : OGRErr OGRSpatialReference::SetEquirectangular(double dfCenterLat,
6891 : double dfCenterLong,
6892 : double dfFalseEasting,
6893 : double dfFalseNorthing)
6894 :
6895 : {
6896 4 : TAKE_OPTIONAL_LOCK();
6897 :
6898 2 : if (dfCenterLat == 0.0)
6899 : {
6900 0 : return d->replaceConversionAndUnref(
6901 : proj_create_conversion_equidistant_cylindrical(
6902 : d->getPROJContext(), 0.0, dfCenterLong, dfFalseEasting,
6903 0 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6904 : }
6905 :
6906 : // Non-standard extension with non-zero latitude of origin
6907 2 : SetProjection(SRS_PT_EQUIRECTANGULAR);
6908 2 : SetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, dfCenterLat);
6909 2 : SetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, dfCenterLong);
6910 2 : SetNormProjParm(SRS_PP_FALSE_EASTING, dfFalseEasting);
6911 2 : SetNormProjParm(SRS_PP_FALSE_NORTHING, dfFalseNorthing);
6912 :
6913 2 : return OGRERR_NONE;
6914 : }
6915 :
6916 : /************************************************************************/
6917 : /* OSRSetEquirectangular() */
6918 : /************************************************************************/
6919 :
6920 0 : OGRErr OSRSetEquirectangular(OGRSpatialReferenceH hSRS, double dfCenterLat,
6921 : double dfCenterLong, double dfFalseEasting,
6922 : double dfFalseNorthing)
6923 :
6924 : {
6925 0 : VALIDATE_POINTER1(hSRS, "OSRSetEquirectangular", OGRERR_FAILURE);
6926 :
6927 0 : return ToPointer(hSRS)->SetEquirectangular(dfCenterLat, dfCenterLong,
6928 0 : dfFalseEasting, dfFalseNorthing);
6929 : }
6930 :
6931 : /************************************************************************/
6932 : /* SetEquirectangular2() */
6933 : /* Generalized form */
6934 : /************************************************************************/
6935 :
6936 179 : OGRErr OGRSpatialReference::SetEquirectangular2(double dfCenterLat,
6937 : double dfCenterLong,
6938 : double dfStdParallel1,
6939 : double dfFalseEasting,
6940 : double dfFalseNorthing)
6941 :
6942 : {
6943 358 : TAKE_OPTIONAL_LOCK();
6944 :
6945 179 : if (dfCenterLat == 0.0)
6946 : {
6947 174 : return d->replaceConversionAndUnref(
6948 : proj_create_conversion_equidistant_cylindrical(
6949 : d->getPROJContext(), dfStdParallel1, dfCenterLong,
6950 174 : dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6951 : }
6952 :
6953 : // Non-standard extension with non-zero latitude of origin
6954 5 : SetProjection(SRS_PT_EQUIRECTANGULAR);
6955 5 : SetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, dfCenterLat);
6956 5 : SetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, dfCenterLong);
6957 5 : SetNormProjParm(SRS_PP_STANDARD_PARALLEL_1, dfStdParallel1);
6958 5 : SetNormProjParm(SRS_PP_FALSE_EASTING, dfFalseEasting);
6959 5 : SetNormProjParm(SRS_PP_FALSE_NORTHING, dfFalseNorthing);
6960 :
6961 5 : return OGRERR_NONE;
6962 : }
6963 :
6964 : /************************************************************************/
6965 : /* OSRSetEquirectangular2() */
6966 : /************************************************************************/
6967 :
6968 3 : OGRErr OSRSetEquirectangular2(OGRSpatialReferenceH hSRS, double dfCenterLat,
6969 : double dfCenterLong, double dfStdParallel1,
6970 : double dfFalseEasting, double dfFalseNorthing)
6971 :
6972 : {
6973 3 : VALIDATE_POINTER1(hSRS, "OSRSetEquirectangular2", OGRERR_FAILURE);
6974 :
6975 3 : return ToPointer(hSRS)->SetEquirectangular2(dfCenterLat, dfCenterLong,
6976 : dfStdParallel1, dfFalseEasting,
6977 3 : dfFalseNorthing);
6978 : }
6979 :
6980 : /************************************************************************/
6981 : /* SetGS() */
6982 : /************************************************************************/
6983 :
6984 5 : OGRErr OGRSpatialReference::SetGS(double dfCentralMeridian,
6985 : double dfFalseEasting, double dfFalseNorthing)
6986 :
6987 : {
6988 5 : return d->replaceConversionAndUnref(proj_create_conversion_gall(
6989 : d->getPROJContext(), dfCentralMeridian, dfFalseEasting, dfFalseNorthing,
6990 5 : nullptr, 0.0, nullptr, 0.0));
6991 : }
6992 :
6993 : /************************************************************************/
6994 : /* OSRSetGS() */
6995 : /************************************************************************/
6996 :
6997 2 : OGRErr OSRSetGS(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
6998 : double dfFalseEasting, double dfFalseNorthing)
6999 :
7000 : {
7001 2 : VALIDATE_POINTER1(hSRS, "OSRSetGS", OGRERR_FAILURE);
7002 :
7003 2 : return ToPointer(hSRS)->SetGS(dfCentralMeridian, dfFalseEasting,
7004 2 : dfFalseNorthing);
7005 : }
7006 :
7007 : /************************************************************************/
7008 : /* SetGH() */
7009 : /************************************************************************/
7010 :
7011 0 : OGRErr OGRSpatialReference::SetGH(double dfCentralMeridian,
7012 : double dfFalseEasting, double dfFalseNorthing)
7013 :
7014 : {
7015 0 : TAKE_OPTIONAL_LOCK();
7016 :
7017 0 : return d->replaceConversionAndUnref(proj_create_conversion_goode_homolosine(
7018 : d->getPROJContext(), dfCentralMeridian, dfFalseEasting, dfFalseNorthing,
7019 0 : nullptr, 0.0, nullptr, 0.0));
7020 : }
7021 :
7022 : /************************************************************************/
7023 : /* OSRSetGH() */
7024 : /************************************************************************/
7025 :
7026 0 : OGRErr OSRSetGH(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
7027 : double dfFalseEasting, double dfFalseNorthing)
7028 :
7029 : {
7030 0 : VALIDATE_POINTER1(hSRS, "OSRSetGH", OGRERR_FAILURE);
7031 :
7032 0 : return ToPointer(hSRS)->SetGH(dfCentralMeridian, dfFalseEasting,
7033 0 : dfFalseNorthing);
7034 : }
7035 :
7036 : /************************************************************************/
7037 : /* SetIGH() */
7038 : /************************************************************************/
7039 :
7040 0 : OGRErr OGRSpatialReference::SetIGH()
7041 :
7042 : {
7043 0 : TAKE_OPTIONAL_LOCK();
7044 :
7045 0 : return d->replaceConversionAndUnref(
7046 : proj_create_conversion_interrupted_goode_homolosine(
7047 0 : d->getPROJContext(), 0.0, 0.0, 0.0, nullptr, 0.0, nullptr, 0.0));
7048 : }
7049 :
7050 : /************************************************************************/
7051 : /* OSRSetIGH() */
7052 : /************************************************************************/
7053 :
7054 0 : OGRErr OSRSetIGH(OGRSpatialReferenceH hSRS)
7055 :
7056 : {
7057 0 : VALIDATE_POINTER1(hSRS, "OSRSetIGH", OGRERR_FAILURE);
7058 :
7059 0 : return ToPointer(hSRS)->SetIGH();
7060 : }
7061 :
7062 : /************************************************************************/
7063 : /* SetGEOS() */
7064 : /************************************************************************/
7065 :
7066 3 : OGRErr OGRSpatialReference::SetGEOS(double dfCentralMeridian,
7067 : double dfSatelliteHeight,
7068 : double dfFalseEasting,
7069 : double dfFalseNorthing)
7070 :
7071 : {
7072 6 : TAKE_OPTIONAL_LOCK();
7073 :
7074 3 : return d->replaceConversionAndUnref(
7075 : proj_create_conversion_geostationary_satellite_sweep_y(
7076 : d->getPROJContext(), dfCentralMeridian, dfSatelliteHeight,
7077 6 : dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
7078 : }
7079 :
7080 : /************************************************************************/
7081 : /* OSRSetGEOS() */
7082 : /************************************************************************/
7083 :
7084 0 : OGRErr OSRSetGEOS(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
7085 : double dfSatelliteHeight, double dfFalseEasting,
7086 : double dfFalseNorthing)
7087 :
7088 : {
7089 0 : VALIDATE_POINTER1(hSRS, "OSRSetGEOS", OGRERR_FAILURE);
7090 :
7091 0 : return ToPointer(hSRS)->SetGEOS(dfCentralMeridian, dfSatelliteHeight,
7092 0 : dfFalseEasting, dfFalseNorthing);
7093 : }
7094 :
7095 : /************************************************************************/
7096 : /* SetGaussSchreiberTMercator() */
7097 : /************************************************************************/
7098 :
7099 0 : OGRErr OGRSpatialReference::SetGaussSchreiberTMercator(double dfCenterLat,
7100 : double dfCenterLong,
7101 : double dfScale,
7102 : double dfFalseEasting,
7103 : double dfFalseNorthing)
7104 :
7105 : {
7106 0 : TAKE_OPTIONAL_LOCK();
7107 :
7108 0 : return d->replaceConversionAndUnref(
7109 : proj_create_conversion_gauss_schreiber_transverse_mercator(
7110 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale,
7111 0 : dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
7112 : }
7113 :
7114 : /************************************************************************/
7115 : /* OSRSetGaussSchreiberTMercator() */
7116 : /************************************************************************/
7117 :
7118 0 : OGRErr OSRSetGaussSchreiberTMercator(OGRSpatialReferenceH hSRS,
7119 : double dfCenterLat, double dfCenterLong,
7120 : double dfScale, double dfFalseEasting,
7121 : double dfFalseNorthing)
7122 :
7123 : {
7124 0 : VALIDATE_POINTER1(hSRS, "OSRSetGaussSchreiberTMercator", OGRERR_FAILURE);
7125 :
7126 0 : return ToPointer(hSRS)->SetGaussSchreiberTMercator(
7127 0 : dfCenterLat, dfCenterLong, dfScale, dfFalseEasting, dfFalseNorthing);
7128 : }
7129 :
7130 : /************************************************************************/
7131 : /* SetGnomonic() */
7132 : /************************************************************************/
7133 :
7134 2 : OGRErr OGRSpatialReference::SetGnomonic(double dfCenterLat, double dfCenterLong,
7135 : double dfFalseEasting,
7136 : double dfFalseNorthing)
7137 :
7138 : {
7139 4 : TAKE_OPTIONAL_LOCK();
7140 :
7141 2 : return d->replaceConversionAndUnref(proj_create_conversion_gnomonic(
7142 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7143 4 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
7144 : }
7145 :
7146 : /************************************************************************/
7147 : /* OSRSetGnomonic() */
7148 : /************************************************************************/
7149 :
7150 0 : OGRErr OSRSetGnomonic(OGRSpatialReferenceH hSRS, double dfCenterLat,
7151 : double dfCenterLong, double dfFalseEasting,
7152 : double dfFalseNorthing)
7153 :
7154 : {
7155 0 : VALIDATE_POINTER1(hSRS, "OSRSetGnomonic", OGRERR_FAILURE);
7156 :
7157 0 : return ToPointer(hSRS)->SetGnomonic(dfCenterLat, dfCenterLong,
7158 0 : dfFalseEasting, dfFalseNorthing);
7159 : }
7160 :
7161 : /************************************************************************/
7162 : /* SetHOMAC() */
7163 : /************************************************************************/
7164 :
7165 : /**
7166 : * \brief Set an Hotine Oblique Mercator Azimuth Center projection using
7167 : * azimuth angle.
7168 : *
7169 : * This projection corresponds to EPSG projection method 9815, also
7170 : * sometimes known as hotine oblique mercator (variant B).
7171 : *
7172 : * This method does the same thing as the C function OSRSetHOMAC().
7173 : *
7174 : * @param dfCenterLat Latitude of the projection origin.
7175 : * @param dfCenterLong Longitude of the projection origin.
7176 : * @param dfAzimuth Azimuth, measured clockwise from North, of the projection
7177 : * centerline.
7178 : * @param dfRectToSkew Angle from Rectified to Skew Grid
7179 : * @param dfScale Scale factor applies to the projection origin.
7180 : * @param dfFalseEasting False easting.
7181 : * @param dfFalseNorthing False northing.
7182 : *
7183 : * @return OGRERR_NONE on success.
7184 : */
7185 :
7186 4 : OGRErr OGRSpatialReference::SetHOMAC(double dfCenterLat, double dfCenterLong,
7187 : double dfAzimuth, double dfRectToSkew,
7188 : double dfScale, double dfFalseEasting,
7189 : double dfFalseNorthing)
7190 :
7191 : {
7192 8 : TAKE_OPTIONAL_LOCK();
7193 :
7194 4 : return d->replaceConversionAndUnref(
7195 : proj_create_conversion_hotine_oblique_mercator_variant_b(
7196 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfAzimuth,
7197 : dfRectToSkew, dfScale, dfFalseEasting, dfFalseNorthing, nullptr,
7198 8 : 0.0, nullptr, 0.0));
7199 : }
7200 :
7201 : /************************************************************************/
7202 : /* OSRSetHOMAC() */
7203 : /************************************************************************/
7204 :
7205 : /**
7206 : * \brief Set an Oblique Mercator projection using azimuth angle.
7207 : *
7208 : * This is the same as the C++ method OGRSpatialReference::SetHOMAC()
7209 : */
7210 0 : OGRErr OSRSetHOMAC(OGRSpatialReferenceH hSRS, double dfCenterLat,
7211 : double dfCenterLong, double dfAzimuth, double dfRectToSkew,
7212 : double dfScale, double dfFalseEasting,
7213 : double dfFalseNorthing)
7214 :
7215 : {
7216 0 : VALIDATE_POINTER1(hSRS, "OSRSetHOMAC", OGRERR_FAILURE);
7217 :
7218 0 : return ToPointer(hSRS)->SetHOMAC(dfCenterLat, dfCenterLong, dfAzimuth,
7219 : dfRectToSkew, dfScale, dfFalseEasting,
7220 0 : dfFalseNorthing);
7221 : }
7222 :
7223 : /************************************************************************/
7224 : /* SetHOM() */
7225 : /************************************************************************/
7226 :
7227 : /**
7228 : * \brief Set a Hotine Oblique Mercator projection using azimuth angle.
7229 : *
7230 : * This projection corresponds to EPSG projection method 9812, also
7231 : * sometimes known as hotine oblique mercator (variant A)..
7232 : *
7233 : * This method does the same thing as the C function OSRSetHOM().
7234 : *
7235 : * @param dfCenterLat Latitude of the projection origin.
7236 : * @param dfCenterLong Longitude of the projection origin.
7237 : * @param dfAzimuth Azimuth, measured clockwise from North, of the projection
7238 : * centerline.
7239 : * @param dfRectToSkew Angle from Rectified to Skew Grid
7240 : * @param dfScale Scale factor applies to the projection origin.
7241 : * @param dfFalseEasting False easting.
7242 : * @param dfFalseNorthing False northing.
7243 : *
7244 : * @return OGRERR_NONE on success.
7245 : */
7246 :
7247 13 : OGRErr OGRSpatialReference::SetHOM(double dfCenterLat, double dfCenterLong,
7248 : double dfAzimuth, double dfRectToSkew,
7249 : double dfScale, double dfFalseEasting,
7250 : double dfFalseNorthing)
7251 :
7252 : {
7253 26 : TAKE_OPTIONAL_LOCK();
7254 :
7255 13 : return d->replaceConversionAndUnref(
7256 : proj_create_conversion_hotine_oblique_mercator_variant_a(
7257 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfAzimuth,
7258 : dfRectToSkew, dfScale, dfFalseEasting, dfFalseNorthing, nullptr,
7259 26 : 0.0, nullptr, 0.0));
7260 : }
7261 :
7262 : /************************************************************************/
7263 : /* OSRSetHOM() */
7264 : /************************************************************************/
7265 : /**
7266 : * \brief Set a Hotine Oblique Mercator projection using azimuth angle.
7267 : *
7268 : * This is the same as the C++ method OGRSpatialReference::SetHOM()
7269 : */
7270 0 : OGRErr OSRSetHOM(OGRSpatialReferenceH hSRS, double dfCenterLat,
7271 : double dfCenterLong, double dfAzimuth, double dfRectToSkew,
7272 : double dfScale, double dfFalseEasting, double dfFalseNorthing)
7273 :
7274 : {
7275 0 : VALIDATE_POINTER1(hSRS, "OSRSetHOM", OGRERR_FAILURE);
7276 :
7277 0 : return ToPointer(hSRS)->SetHOM(dfCenterLat, dfCenterLong, dfAzimuth,
7278 : dfRectToSkew, dfScale, dfFalseEasting,
7279 0 : dfFalseNorthing);
7280 : }
7281 :
7282 : /************************************************************************/
7283 : /* SetHOM2PNO() */
7284 : /************************************************************************/
7285 :
7286 : /**
7287 : * \brief Set a Hotine Oblique Mercator projection using two points on
7288 : * projection centerline.
7289 : *
7290 : * This method does the same thing as the C function OSRSetHOM2PNO().
7291 : *
7292 : * @param dfCenterLat Latitude of the projection origin.
7293 : * @param dfLat1 Latitude of the first point on center line.
7294 : * @param dfLong1 Longitude of the first point on center line.
7295 : * @param dfLat2 Latitude of the second point on center line.
7296 : * @param dfLong2 Longitude of the second point on center line.
7297 : * @param dfScale Scale factor applies to the projection origin.
7298 : * @param dfFalseEasting False easting.
7299 : * @param dfFalseNorthing False northing.
7300 : *
7301 : * @return OGRERR_NONE on success.
7302 : */
7303 :
7304 3 : OGRErr OGRSpatialReference::SetHOM2PNO(double dfCenterLat, double dfLat1,
7305 : double dfLong1, double dfLat2,
7306 : double dfLong2, double dfScale,
7307 : double dfFalseEasting,
7308 : double dfFalseNorthing)
7309 :
7310 : {
7311 6 : TAKE_OPTIONAL_LOCK();
7312 :
7313 3 : return d->replaceConversionAndUnref(
7314 : proj_create_conversion_hotine_oblique_mercator_two_point_natural_origin(
7315 : d->getPROJContext(), dfCenterLat, dfLat1, dfLong1, dfLat2, dfLong2,
7316 : dfScale, dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr,
7317 6 : 0.0));
7318 : }
7319 :
7320 : /************************************************************************/
7321 : /* OSRSetHOM2PNO() */
7322 : /************************************************************************/
7323 : /**
7324 : * \brief Set a Hotine Oblique Mercator projection using two points on
7325 : * projection centerline.
7326 : *
7327 : * This is the same as the C++ method OGRSpatialReference::SetHOM2PNO()
7328 : */
7329 0 : OGRErr OSRSetHOM2PNO(OGRSpatialReferenceH hSRS, double dfCenterLat,
7330 : double dfLat1, double dfLong1, double dfLat2,
7331 : double dfLong2, double dfScale, double dfFalseEasting,
7332 : double dfFalseNorthing)
7333 :
7334 : {
7335 0 : VALIDATE_POINTER1(hSRS, "OSRSetHOM2PNO", OGRERR_FAILURE);
7336 :
7337 0 : return ToPointer(hSRS)->SetHOM2PNO(dfCenterLat, dfLat1, dfLong1, dfLat2,
7338 : dfLong2, dfScale, dfFalseEasting,
7339 0 : dfFalseNorthing);
7340 : }
7341 :
7342 : /************************************************************************/
7343 : /* SetLOM() */
7344 : /************************************************************************/
7345 :
7346 : /**
7347 : * \brief Set a Laborde Oblique Mercator projection.
7348 : *
7349 : * @param dfCenterLat Latitude of the projection origin.
7350 : * @param dfCenterLong Longitude of the projection origin.
7351 : * @param dfAzimuth Azimuth, measured clockwise from North, of the projection
7352 : * centerline.
7353 : * @param dfScale Scale factor on the initial line
7354 : * @param dfFalseEasting False easting.
7355 : * @param dfFalseNorthing False northing.
7356 : *
7357 : * @return OGRERR_NONE on success.
7358 : */
7359 :
7360 0 : OGRErr OGRSpatialReference::SetLOM(double dfCenterLat, double dfCenterLong,
7361 : double dfAzimuth, double dfScale,
7362 : double dfFalseEasting,
7363 : double dfFalseNorthing)
7364 :
7365 : {
7366 0 : TAKE_OPTIONAL_LOCK();
7367 :
7368 0 : return d->replaceConversionAndUnref(
7369 : proj_create_conversion_laborde_oblique_mercator(
7370 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfAzimuth, dfScale,
7371 0 : dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
7372 : }
7373 :
7374 : /************************************************************************/
7375 : /* SetIWMPolyconic() */
7376 : /************************************************************************/
7377 :
7378 0 : OGRErr OGRSpatialReference::SetIWMPolyconic(double dfLat1, double dfLat2,
7379 : double dfCenterLong,
7380 : double dfFalseEasting,
7381 : double dfFalseNorthing)
7382 :
7383 : {
7384 0 : TAKE_OPTIONAL_LOCK();
7385 :
7386 0 : return d->replaceConversionAndUnref(
7387 : proj_create_conversion_international_map_world_polyconic(
7388 : d->getPROJContext(), dfCenterLong, dfLat1, dfLat2, dfFalseEasting,
7389 0 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
7390 : }
7391 :
7392 : /************************************************************************/
7393 : /* OSRSetIWMPolyconic() */
7394 : /************************************************************************/
7395 :
7396 0 : OGRErr OSRSetIWMPolyconic(OGRSpatialReferenceH hSRS, double dfLat1,
7397 : double dfLat2, double dfCenterLong,
7398 : double dfFalseEasting, double dfFalseNorthing)
7399 :
7400 : {
7401 0 : VALIDATE_POINTER1(hSRS, "OSRSetIWMPolyconic", OGRERR_FAILURE);
7402 :
7403 0 : return ToPointer(hSRS)->SetIWMPolyconic(dfLat1, dfLat2, dfCenterLong,
7404 0 : dfFalseEasting, dfFalseNorthing);
7405 : }
7406 :
7407 : /************************************************************************/
7408 : /* SetKrovak() */
7409 : /************************************************************************/
7410 :
7411 : /** Krovak east-north projection.
7412 : *
7413 : * Note that dfAzimuth and dfPseudoStdParallel1 are ignored when exporting
7414 : * to PROJ and should be respectively set to 30.28813972222222 and 78.5
7415 : */
7416 3 : OGRErr OGRSpatialReference::SetKrovak(double dfCenterLat, double dfCenterLong,
7417 : double dfAzimuth,
7418 : double dfPseudoStdParallel1,
7419 : double dfScale, double dfFalseEasting,
7420 : double dfFalseNorthing)
7421 :
7422 : {
7423 6 : TAKE_OPTIONAL_LOCK();
7424 :
7425 3 : return d->replaceConversionAndUnref(
7426 : proj_create_conversion_krovak_north_oriented(
7427 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfAzimuth,
7428 : dfPseudoStdParallel1, dfScale, dfFalseEasting, dfFalseNorthing,
7429 6 : nullptr, 0.0, nullptr, 0.0));
7430 : }
7431 :
7432 : /************************************************************************/
7433 : /* OSRSetKrovak() */
7434 : /************************************************************************/
7435 :
7436 0 : OGRErr OSRSetKrovak(OGRSpatialReferenceH hSRS, double dfCenterLat,
7437 : double dfCenterLong, double dfAzimuth,
7438 : double dfPseudoStdParallel1, double dfScale,
7439 : double dfFalseEasting, double dfFalseNorthing)
7440 :
7441 : {
7442 0 : VALIDATE_POINTER1(hSRS, "OSRSetKrovak", OGRERR_FAILURE);
7443 :
7444 0 : return ToPointer(hSRS)->SetKrovak(dfCenterLat, dfCenterLong, dfAzimuth,
7445 : dfPseudoStdParallel1, dfScale,
7446 0 : dfFalseEasting, dfFalseNorthing);
7447 : }
7448 :
7449 : /************************************************************************/
7450 : /* SetLAEA() */
7451 : /************************************************************************/
7452 :
7453 17 : OGRErr OGRSpatialReference::SetLAEA(double dfCenterLat, double dfCenterLong,
7454 : double dfFalseEasting,
7455 : double dfFalseNorthing)
7456 :
7457 : {
7458 34 : TAKE_OPTIONAL_LOCK();
7459 :
7460 17 : auto conv = proj_create_conversion_lambert_azimuthal_equal_area(
7461 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7462 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
7463 :
7464 17 : const char *pszName = nullptr;
7465 17 : double dfConvFactor = GetTargetLinearUnits(nullptr, &pszName);
7466 17 : CPLString osName = pszName ? pszName : "";
7467 :
7468 17 : d->refreshProjObj();
7469 :
7470 17 : d->demoteFromBoundCRS();
7471 :
7472 17 : auto cs = proj_create_cartesian_2D_cs(
7473 : d->getPROJContext(),
7474 17 : std::fabs(dfCenterLat - 90) < 1e-10 && dfCenterLong == 0
7475 : ? PJ_CART2D_NORTH_POLE_EASTING_SOUTH_NORTHING_SOUTH
7476 0 : : std::fabs(dfCenterLat - -90) < 1e-10 && dfCenterLong == 0
7477 14 : ? PJ_CART2D_SOUTH_POLE_EASTING_NORTH_NORTHING_NORTH
7478 : : PJ_CART2D_EASTING_NORTHING,
7479 17 : !osName.empty() ? osName.c_str() : nullptr, dfConvFactor);
7480 : auto projCRS =
7481 17 : proj_create_projected_crs(d->getPROJContext(), d->getProjCRSName(),
7482 17 : d->getGeodBaseCRS(), conv, cs);
7483 17 : proj_destroy(conv);
7484 17 : proj_destroy(cs);
7485 :
7486 17 : d->setPjCRS(projCRS);
7487 :
7488 17 : d->undoDemoteFromBoundCRS();
7489 :
7490 34 : return OGRERR_NONE;
7491 : }
7492 :
7493 : /************************************************************************/
7494 : /* OSRSetLAEA() */
7495 : /************************************************************************/
7496 :
7497 0 : OGRErr OSRSetLAEA(OGRSpatialReferenceH hSRS, double dfCenterLat,
7498 : double dfCenterLong, double dfFalseEasting,
7499 : double dfFalseNorthing)
7500 :
7501 : {
7502 0 : VALIDATE_POINTER1(hSRS, "OSRSetLAEA", OGRERR_FAILURE);
7503 :
7504 0 : return ToPointer(hSRS)->SetLAEA(dfCenterLat, dfCenterLong, dfFalseEasting,
7505 0 : dfFalseNorthing);
7506 : }
7507 :
7508 : /************************************************************************/
7509 : /* SetLCC() */
7510 : /************************************************************************/
7511 :
7512 148 : OGRErr OGRSpatialReference::SetLCC(double dfStdP1, double dfStdP2,
7513 : double dfCenterLat, double dfCenterLong,
7514 : double dfFalseEasting,
7515 : double dfFalseNorthing)
7516 :
7517 : {
7518 296 : TAKE_OPTIONAL_LOCK();
7519 :
7520 148 : return d->replaceConversionAndUnref(
7521 : proj_create_conversion_lambert_conic_conformal_2sp(
7522 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfStdP1, dfStdP2,
7523 296 : dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0));
7524 : }
7525 :
7526 : /************************************************************************/
7527 : /* OSRSetLCC() */
7528 : /************************************************************************/
7529 :
7530 1 : OGRErr OSRSetLCC(OGRSpatialReferenceH hSRS, double dfStdP1, double dfStdP2,
7531 : double dfCenterLat, double dfCenterLong, double dfFalseEasting,
7532 : double dfFalseNorthing)
7533 :
7534 : {
7535 1 : VALIDATE_POINTER1(hSRS, "OSRSetLCC", OGRERR_FAILURE);
7536 :
7537 1 : return ToPointer(hSRS)->SetLCC(dfStdP1, dfStdP2, dfCenterLat, dfCenterLong,
7538 1 : dfFalseEasting, dfFalseNorthing);
7539 : }
7540 :
7541 : /************************************************************************/
7542 : /* SetLCC1SP() */
7543 : /************************************************************************/
7544 :
7545 10 : OGRErr OGRSpatialReference::SetLCC1SP(double dfCenterLat, double dfCenterLong,
7546 : double dfScale, double dfFalseEasting,
7547 : double dfFalseNorthing)
7548 :
7549 : {
7550 20 : TAKE_OPTIONAL_LOCK();
7551 :
7552 10 : return d->replaceConversionAndUnref(
7553 : proj_create_conversion_lambert_conic_conformal_1sp(
7554 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale,
7555 20 : dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0));
7556 : }
7557 :
7558 : /************************************************************************/
7559 : /* OSRSetLCC1SP() */
7560 : /************************************************************************/
7561 :
7562 0 : OGRErr OSRSetLCC1SP(OGRSpatialReferenceH hSRS, double dfCenterLat,
7563 : double dfCenterLong, double dfScale, double dfFalseEasting,
7564 : double dfFalseNorthing)
7565 :
7566 : {
7567 0 : VALIDATE_POINTER1(hSRS, "OSRSetLCC1SP", OGRERR_FAILURE);
7568 :
7569 0 : return ToPointer(hSRS)->SetLCC1SP(dfCenterLat, dfCenterLong, dfScale,
7570 0 : dfFalseEasting, dfFalseNorthing);
7571 : }
7572 :
7573 : /************************************************************************/
7574 : /* SetLCCB() */
7575 : /************************************************************************/
7576 :
7577 2 : OGRErr OGRSpatialReference::SetLCCB(double dfStdP1, double dfStdP2,
7578 : double dfCenterLat, double dfCenterLong,
7579 : double dfFalseEasting,
7580 : double dfFalseNorthing)
7581 :
7582 : {
7583 4 : TAKE_OPTIONAL_LOCK();
7584 :
7585 2 : return d->replaceConversionAndUnref(
7586 : proj_create_conversion_lambert_conic_conformal_2sp_belgium(
7587 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfStdP1, dfStdP2,
7588 4 : dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0));
7589 : }
7590 :
7591 : /************************************************************************/
7592 : /* OSRSetLCCB() */
7593 : /************************************************************************/
7594 :
7595 0 : OGRErr OSRSetLCCB(OGRSpatialReferenceH hSRS, double dfStdP1, double dfStdP2,
7596 : double dfCenterLat, double dfCenterLong,
7597 : double dfFalseEasting, double dfFalseNorthing)
7598 :
7599 : {
7600 0 : VALIDATE_POINTER1(hSRS, "OSRSetLCCB", OGRERR_FAILURE);
7601 :
7602 0 : return ToPointer(hSRS)->SetLCCB(dfStdP1, dfStdP2, dfCenterLat, dfCenterLong,
7603 0 : dfFalseEasting, dfFalseNorthing);
7604 : }
7605 :
7606 : /************************************************************************/
7607 : /* SetMC() */
7608 : /************************************************************************/
7609 :
7610 4 : OGRErr OGRSpatialReference::SetMC(double dfCenterLat, double dfCenterLong,
7611 : double dfFalseEasting, double dfFalseNorthing)
7612 :
7613 : {
7614 8 : TAKE_OPTIONAL_LOCK();
7615 :
7616 : (void)dfCenterLat; // ignored
7617 :
7618 4 : return d->replaceConversionAndUnref(
7619 : proj_create_conversion_miller_cylindrical(
7620 : d->getPROJContext(), dfCenterLong, dfFalseEasting, dfFalseNorthing,
7621 8 : nullptr, 0, nullptr, 0));
7622 : }
7623 :
7624 : /************************************************************************/
7625 : /* OSRSetMC() */
7626 : /************************************************************************/
7627 :
7628 0 : OGRErr OSRSetMC(OGRSpatialReferenceH hSRS, double dfCenterLat,
7629 : double dfCenterLong, double dfFalseEasting,
7630 : double dfFalseNorthing)
7631 :
7632 : {
7633 0 : VALIDATE_POINTER1(hSRS, "OSRSetMC", OGRERR_FAILURE);
7634 :
7635 0 : return ToPointer(hSRS)->SetMC(dfCenterLat, dfCenterLong, dfFalseEasting,
7636 0 : dfFalseNorthing);
7637 : }
7638 :
7639 : /************************************************************************/
7640 : /* SetMercator() */
7641 : /************************************************************************/
7642 :
7643 59 : OGRErr OGRSpatialReference::SetMercator(double dfCenterLat, double dfCenterLong,
7644 : double dfScale, double dfFalseEasting,
7645 : double dfFalseNorthing)
7646 :
7647 : {
7648 118 : TAKE_OPTIONAL_LOCK();
7649 :
7650 59 : if (dfCenterLat != 0.0 && dfScale == 1.0)
7651 : {
7652 : // Not sure this is correct, but this is how it has been used
7653 : // historically
7654 0 : return SetMercator2SP(dfCenterLat, 0.0, dfCenterLong, dfFalseEasting,
7655 0 : dfFalseNorthing);
7656 : }
7657 59 : return d->replaceConversionAndUnref(
7658 : proj_create_conversion_mercator_variant_a(
7659 : d->getPROJContext(),
7660 : dfCenterLat, // should be zero
7661 : dfCenterLong, dfScale, dfFalseEasting, dfFalseNorthing, nullptr, 0,
7662 59 : nullptr, 0));
7663 : }
7664 :
7665 : /************************************************************************/
7666 : /* OSRSetMercator() */
7667 : /************************************************************************/
7668 :
7669 2 : OGRErr OSRSetMercator(OGRSpatialReferenceH hSRS, double dfCenterLat,
7670 : double dfCenterLong, double dfScale,
7671 : double dfFalseEasting, double dfFalseNorthing)
7672 :
7673 : {
7674 2 : VALIDATE_POINTER1(hSRS, "OSRSetMercator", OGRERR_FAILURE);
7675 :
7676 2 : return ToPointer(hSRS)->SetMercator(dfCenterLat, dfCenterLong, dfScale,
7677 2 : dfFalseEasting, dfFalseNorthing);
7678 : }
7679 :
7680 : /************************************************************************/
7681 : /* SetMercator2SP() */
7682 : /************************************************************************/
7683 :
7684 30 : OGRErr OGRSpatialReference::SetMercator2SP(double dfStdP1, double dfCenterLat,
7685 : double dfCenterLong,
7686 : double dfFalseEasting,
7687 : double dfFalseNorthing)
7688 :
7689 : {
7690 30 : if (dfCenterLat == 0.0)
7691 : {
7692 29 : return d->replaceConversionAndUnref(
7693 : proj_create_conversion_mercator_variant_b(
7694 : d->getPROJContext(), dfStdP1, dfCenterLong, dfFalseEasting,
7695 29 : dfFalseNorthing, nullptr, 0, nullptr, 0));
7696 : }
7697 :
7698 1 : TAKE_OPTIONAL_LOCK();
7699 :
7700 1 : SetProjection(SRS_PT_MERCATOR_2SP);
7701 :
7702 1 : SetNormProjParm(SRS_PP_STANDARD_PARALLEL_1, dfStdP1);
7703 1 : SetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, dfCenterLat);
7704 1 : SetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, dfCenterLong);
7705 1 : SetNormProjParm(SRS_PP_FALSE_EASTING, dfFalseEasting);
7706 1 : SetNormProjParm(SRS_PP_FALSE_NORTHING, dfFalseNorthing);
7707 :
7708 1 : return OGRERR_NONE;
7709 : }
7710 :
7711 : /************************************************************************/
7712 : /* OSRSetMercator2SP() */
7713 : /************************************************************************/
7714 :
7715 1 : OGRErr OSRSetMercator2SP(OGRSpatialReferenceH hSRS, double dfStdP1,
7716 : double dfCenterLat, double dfCenterLong,
7717 : double dfFalseEasting, double dfFalseNorthing)
7718 :
7719 : {
7720 1 : VALIDATE_POINTER1(hSRS, "OSRSetMercator2SP", OGRERR_FAILURE);
7721 :
7722 1 : return ToPointer(hSRS)->SetMercator2SP(dfStdP1, dfCenterLat, dfCenterLong,
7723 1 : dfFalseEasting, dfFalseNorthing);
7724 : }
7725 :
7726 : /************************************************************************/
7727 : /* SetMollweide() */
7728 : /************************************************************************/
7729 :
7730 3 : OGRErr OGRSpatialReference::SetMollweide(double dfCentralMeridian,
7731 : double dfFalseEasting,
7732 : double dfFalseNorthing)
7733 :
7734 : {
7735 6 : TAKE_OPTIONAL_LOCK();
7736 :
7737 3 : return d->replaceConversionAndUnref(proj_create_conversion_mollweide(
7738 : d->getPROJContext(), dfCentralMeridian, dfFalseEasting, dfFalseNorthing,
7739 6 : nullptr, 0, nullptr, 0));
7740 : }
7741 :
7742 : /************************************************************************/
7743 : /* OSRSetMollweide() */
7744 : /************************************************************************/
7745 :
7746 0 : OGRErr OSRSetMollweide(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
7747 : double dfFalseEasting, double dfFalseNorthing)
7748 :
7749 : {
7750 0 : VALIDATE_POINTER1(hSRS, "OSRSetMollweide", OGRERR_FAILURE);
7751 :
7752 0 : return ToPointer(hSRS)->SetMollweide(dfCentralMeridian, dfFalseEasting,
7753 0 : dfFalseNorthing);
7754 : }
7755 :
7756 : /************************************************************************/
7757 : /* SetNZMG() */
7758 : /************************************************************************/
7759 :
7760 7 : OGRErr OGRSpatialReference::SetNZMG(double dfCenterLat, double dfCenterLong,
7761 : double dfFalseEasting,
7762 : double dfFalseNorthing)
7763 :
7764 : {
7765 14 : TAKE_OPTIONAL_LOCK();
7766 :
7767 7 : return d->replaceConversionAndUnref(
7768 : proj_create_conversion_new_zealand_mapping_grid(
7769 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7770 14 : dfFalseNorthing, nullptr, 0, nullptr, 0));
7771 : }
7772 :
7773 : /************************************************************************/
7774 : /* OSRSetNZMG() */
7775 : /************************************************************************/
7776 :
7777 0 : OGRErr OSRSetNZMG(OGRSpatialReferenceH hSRS, double dfCenterLat,
7778 : double dfCenterLong, double dfFalseEasting,
7779 : double dfFalseNorthing)
7780 :
7781 : {
7782 0 : VALIDATE_POINTER1(hSRS, "OSRSetNZMG", OGRERR_FAILURE);
7783 :
7784 0 : return ToPointer(hSRS)->SetNZMG(dfCenterLat, dfCenterLong, dfFalseEasting,
7785 0 : dfFalseNorthing);
7786 : }
7787 :
7788 : /************************************************************************/
7789 : /* SetOS() */
7790 : /************************************************************************/
7791 :
7792 6 : OGRErr OGRSpatialReference::SetOS(double dfOriginLat, double dfCMeridian,
7793 : double dfScale, double dfFalseEasting,
7794 : double dfFalseNorthing)
7795 :
7796 : {
7797 12 : TAKE_OPTIONAL_LOCK();
7798 :
7799 6 : return d->replaceConversionAndUnref(
7800 : proj_create_conversion_oblique_stereographic(
7801 : d->getPROJContext(), dfOriginLat, dfCMeridian, dfScale,
7802 12 : dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0));
7803 : }
7804 :
7805 : /************************************************************************/
7806 : /* OSRSetOS() */
7807 : /************************************************************************/
7808 :
7809 0 : OGRErr OSRSetOS(OGRSpatialReferenceH hSRS, double dfOriginLat,
7810 : double dfCMeridian, double dfScale, double dfFalseEasting,
7811 : double dfFalseNorthing)
7812 :
7813 : {
7814 0 : VALIDATE_POINTER1(hSRS, "OSRSetOS", OGRERR_FAILURE);
7815 :
7816 0 : return ToPointer(hSRS)->SetOS(dfOriginLat, dfCMeridian, dfScale,
7817 0 : dfFalseEasting, dfFalseNorthing);
7818 : }
7819 :
7820 : /************************************************************************/
7821 : /* SetOrthographic() */
7822 : /************************************************************************/
7823 :
7824 7 : OGRErr OGRSpatialReference::SetOrthographic(double dfCenterLat,
7825 : double dfCenterLong,
7826 : double dfFalseEasting,
7827 : double dfFalseNorthing)
7828 :
7829 : {
7830 14 : TAKE_OPTIONAL_LOCK();
7831 :
7832 7 : return d->replaceConversionAndUnref(proj_create_conversion_orthographic(
7833 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7834 14 : dfFalseNorthing, nullptr, 0, nullptr, 0));
7835 : }
7836 :
7837 : /************************************************************************/
7838 : /* OSRSetOrthographic() */
7839 : /************************************************************************/
7840 :
7841 1 : OGRErr OSRSetOrthographic(OGRSpatialReferenceH hSRS, double dfCenterLat,
7842 : double dfCenterLong, double dfFalseEasting,
7843 : double dfFalseNorthing)
7844 :
7845 : {
7846 1 : VALIDATE_POINTER1(hSRS, "OSRSetOrthographic", OGRERR_FAILURE);
7847 :
7848 1 : return ToPointer(hSRS)->SetOrthographic(dfCenterLat, dfCenterLong,
7849 1 : dfFalseEasting, dfFalseNorthing);
7850 : }
7851 :
7852 : /************************************************************************/
7853 : /* SetPolyconic() */
7854 : /************************************************************************/
7855 :
7856 7 : OGRErr OGRSpatialReference::SetPolyconic(double dfCenterLat,
7857 : double dfCenterLong,
7858 : double dfFalseEasting,
7859 : double dfFalseNorthing)
7860 :
7861 : {
7862 14 : TAKE_OPTIONAL_LOCK();
7863 :
7864 : // note: it seems that by some definitions this should include a
7865 : // scale_factor parameter.
7866 7 : return d->replaceConversionAndUnref(
7867 : proj_create_conversion_american_polyconic(
7868 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7869 14 : dfFalseNorthing, nullptr, 0, nullptr, 0));
7870 : }
7871 :
7872 : /************************************************************************/
7873 : /* OSRSetPolyconic() */
7874 : /************************************************************************/
7875 :
7876 0 : OGRErr OSRSetPolyconic(OGRSpatialReferenceH hSRS, double dfCenterLat,
7877 : double dfCenterLong, double dfFalseEasting,
7878 : double dfFalseNorthing)
7879 :
7880 : {
7881 0 : VALIDATE_POINTER1(hSRS, "OSRSetPolyconic", OGRERR_FAILURE);
7882 :
7883 0 : return ToPointer(hSRS)->SetPolyconic(dfCenterLat, dfCenterLong,
7884 0 : dfFalseEasting, dfFalseNorthing);
7885 : }
7886 :
7887 : /************************************************************************/
7888 : /* SetPS() */
7889 : /************************************************************************/
7890 :
7891 : /** Sets a Polar Stereographic projection.
7892 : *
7893 : * Two variants are possible:
7894 : * - Polar Stereographic Variant A: dfCenterLat must be +/- 90° and is
7895 : * interpreted as the latitude of origin, combined with the scale factor
7896 : * - Polar Stereographic Variant B: dfCenterLat is different from +/- 90° and
7897 : * is interpreted as the latitude of true scale. In that situation, dfScale
7898 : * must be set to 1 (it is ignored in the projection parameters)
7899 : */
7900 30 : OGRErr OGRSpatialReference::SetPS(double dfCenterLat, double dfCenterLong,
7901 : double dfScale, double dfFalseEasting,
7902 : double dfFalseNorthing)
7903 :
7904 : {
7905 60 : TAKE_OPTIONAL_LOCK();
7906 :
7907 : PJ *conv;
7908 30 : if (dfScale == 1.0 && std::abs(std::abs(dfCenterLat) - 90) > 1e-8)
7909 : {
7910 20 : conv = proj_create_conversion_polar_stereographic_variant_b(
7911 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7912 : dfFalseNorthing, nullptr, 0, nullptr, 0);
7913 : }
7914 : else
7915 : {
7916 10 : conv = proj_create_conversion_polar_stereographic_variant_a(
7917 : d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale,
7918 : dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0);
7919 : }
7920 :
7921 30 : const char *pszName = nullptr;
7922 30 : double dfConvFactor = GetTargetLinearUnits(nullptr, &pszName);
7923 30 : CPLString osName = pszName ? pszName : "";
7924 :
7925 30 : d->refreshProjObj();
7926 :
7927 30 : d->demoteFromBoundCRS();
7928 :
7929 30 : auto cs = proj_create_cartesian_2D_cs(
7930 : d->getPROJContext(),
7931 : dfCenterLat > 0 ? PJ_CART2D_NORTH_POLE_EASTING_SOUTH_NORTHING_SOUTH
7932 : : PJ_CART2D_SOUTH_POLE_EASTING_NORTH_NORTHING_NORTH,
7933 30 : !osName.empty() ? osName.c_str() : nullptr, dfConvFactor);
7934 : auto projCRS =
7935 30 : proj_create_projected_crs(d->getPROJContext(), d->getProjCRSName(),
7936 30 : d->getGeodBaseCRS(), conv, cs);
7937 30 : proj_destroy(conv);
7938 30 : proj_destroy(cs);
7939 :
7940 30 : d->setPjCRS(projCRS);
7941 :
7942 30 : d->undoDemoteFromBoundCRS();
7943 :
7944 60 : return OGRERR_NONE;
7945 : }
7946 :
7947 : /************************************************************************/
7948 : /* OSRSetPS() */
7949 : /************************************************************************/
7950 :
7951 1 : OGRErr OSRSetPS(OGRSpatialReferenceH hSRS, double dfCenterLat,
7952 : double dfCenterLong, double dfScale, double dfFalseEasting,
7953 : double dfFalseNorthing)
7954 :
7955 : {
7956 1 : VALIDATE_POINTER1(hSRS, "OSRSetPS", OGRERR_FAILURE);
7957 :
7958 1 : return ToPointer(hSRS)->SetPS(dfCenterLat, dfCenterLong, dfScale,
7959 1 : dfFalseEasting, dfFalseNorthing);
7960 : }
7961 :
7962 : /************************************************************************/
7963 : /* SetRobinson() */
7964 : /************************************************************************/
7965 :
7966 4 : OGRErr OGRSpatialReference::SetRobinson(double dfCenterLong,
7967 : double dfFalseEasting,
7968 : double dfFalseNorthing)
7969 :
7970 : {
7971 8 : TAKE_OPTIONAL_LOCK();
7972 :
7973 4 : return d->replaceConversionAndUnref(proj_create_conversion_robinson(
7974 : d->getPROJContext(), dfCenterLong, dfFalseEasting, dfFalseNorthing,
7975 8 : nullptr, 0, nullptr, 0));
7976 : }
7977 :
7978 : /************************************************************************/
7979 : /* OSRSetRobinson() */
7980 : /************************************************************************/
7981 :
7982 0 : OGRErr OSRSetRobinson(OGRSpatialReferenceH hSRS, double dfCenterLong,
7983 : double dfFalseEasting, double dfFalseNorthing)
7984 :
7985 : {
7986 0 : VALIDATE_POINTER1(hSRS, "OSRSetRobinson", OGRERR_FAILURE);
7987 :
7988 0 : return ToPointer(hSRS)->SetRobinson(dfCenterLong, dfFalseEasting,
7989 0 : dfFalseNorthing);
7990 : }
7991 :
7992 : /************************************************************************/
7993 : /* SetSinusoidal() */
7994 : /************************************************************************/
7995 :
7996 36 : OGRErr OGRSpatialReference::SetSinusoidal(double dfCenterLong,
7997 : double dfFalseEasting,
7998 : double dfFalseNorthing)
7999 :
8000 : {
8001 72 : TAKE_OPTIONAL_LOCK();
8002 :
8003 36 : return d->replaceConversionAndUnref(proj_create_conversion_sinusoidal(
8004 : d->getPROJContext(), dfCenterLong, dfFalseEasting, dfFalseNorthing,
8005 72 : nullptr, 0, nullptr, 0));
8006 : }
8007 :
8008 : /************************************************************************/
8009 : /* OSRSetSinusoidal() */
8010 : /************************************************************************/
8011 :
8012 1 : OGRErr OSRSetSinusoidal(OGRSpatialReferenceH hSRS, double dfCenterLong,
8013 : double dfFalseEasting, double dfFalseNorthing)
8014 :
8015 : {
8016 1 : VALIDATE_POINTER1(hSRS, "OSRSetSinusoidal", OGRERR_FAILURE);
8017 :
8018 1 : return ToPointer(hSRS)->SetSinusoidal(dfCenterLong, dfFalseEasting,
8019 1 : dfFalseNorthing);
8020 : }
8021 :
8022 : /************************************************************************/
8023 : /* SetStereographic() */
8024 : /************************************************************************/
8025 :
8026 2 : OGRErr OGRSpatialReference::SetStereographic(double dfOriginLat,
8027 : double dfCMeridian, double dfScale,
8028 : double dfFalseEasting,
8029 : double dfFalseNorthing)
8030 :
8031 : {
8032 4 : TAKE_OPTIONAL_LOCK();
8033 :
8034 2 : return d->replaceConversionAndUnref(proj_create_conversion_stereographic(
8035 : d->getPROJContext(), dfOriginLat, dfCMeridian, dfScale, dfFalseEasting,
8036 4 : dfFalseNorthing, nullptr, 0, nullptr, 0));
8037 : }
8038 :
8039 : /************************************************************************/
8040 : /* OSRSetStereographic() */
8041 : /************************************************************************/
8042 :
8043 0 : OGRErr OSRSetStereographic(OGRSpatialReferenceH hSRS, double dfOriginLat,
8044 : double dfCMeridian, double dfScale,
8045 : double dfFalseEasting, double dfFalseNorthing)
8046 :
8047 : {
8048 0 : VALIDATE_POINTER1(hSRS, "OSRSetStereographic", OGRERR_FAILURE);
8049 :
8050 0 : return ToPointer(hSRS)->SetStereographic(dfOriginLat, dfCMeridian, dfScale,
8051 0 : dfFalseEasting, dfFalseNorthing);
8052 : }
8053 :
8054 : /************************************************************************/
8055 : /* SetSOC() */
8056 : /* */
8057 : /* NOTE: This definition isn't really used in practice any more */
8058 : /* and should be considered deprecated. It seems that swiss */
8059 : /* oblique mercator is now define as Hotine_Oblique_Mercator */
8060 : /* with an azimuth of 90 and a rectified_grid_angle of 90. See */
8061 : /* EPSG:2056 and Bug 423. */
8062 : /************************************************************************/
8063 :
8064 2 : OGRErr OGRSpatialReference::SetSOC(double dfLatitudeOfOrigin,
8065 : double dfCentralMeridian,
8066 : double dfFalseEasting,
8067 : double dfFalseNorthing)
8068 :
8069 : {
8070 4 : TAKE_OPTIONAL_LOCK();
8071 :
8072 2 : return d->replaceConversionAndUnref(
8073 : proj_create_conversion_hotine_oblique_mercator_variant_b(
8074 : d->getPROJContext(), dfLatitudeOfOrigin, dfCentralMeridian, 90.0,
8075 : 90.0, 1.0, dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr,
8076 4 : 0.0));
8077 : #if 0
8078 : SetProjection( SRS_PT_SWISS_OBLIQUE_CYLINDRICAL );
8079 : SetNormProjParm( SRS_PP_LATITUDE_OF_CENTER, dfLatitudeOfOrigin );
8080 : SetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, dfCentralMeridian );
8081 : SetNormProjParm( SRS_PP_FALSE_EASTING, dfFalseEasting );
8082 : SetNormProjParm( SRS_PP_FALSE_NORTHING, dfFalseNorthing );
8083 :
8084 : return OGRERR_NONE;
8085 : #endif
8086 : }
8087 :
8088 : /************************************************************************/
8089 : /* OSRSetSOC() */
8090 : /************************************************************************/
8091 :
8092 0 : OGRErr OSRSetSOC(OGRSpatialReferenceH hSRS, double dfLatitudeOfOrigin,
8093 : double dfCentralMeridian, double dfFalseEasting,
8094 : double dfFalseNorthing)
8095 :
8096 : {
8097 0 : VALIDATE_POINTER1(hSRS, "OSRSetSOC", OGRERR_FAILURE);
8098 :
8099 0 : return ToPointer(hSRS)->SetSOC(dfLatitudeOfOrigin, dfCentralMeridian,
8100 0 : dfFalseEasting, dfFalseNorthing);
8101 : }
8102 :
8103 : /************************************************************************/
8104 : /* SetVDG() */
8105 : /************************************************************************/
8106 :
8107 2 : OGRErr OGRSpatialReference::SetVDG(double dfCMeridian, double dfFalseEasting,
8108 : double dfFalseNorthing)
8109 :
8110 : {
8111 4 : TAKE_OPTIONAL_LOCK();
8112 :
8113 2 : return d->replaceConversionAndUnref(proj_create_conversion_van_der_grinten(
8114 : d->getPROJContext(), dfCMeridian, dfFalseEasting, dfFalseNorthing,
8115 4 : nullptr, 0, nullptr, 0));
8116 : }
8117 :
8118 : /************************************************************************/
8119 : /* OSRSetVDG() */
8120 : /************************************************************************/
8121 :
8122 0 : OGRErr OSRSetVDG(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
8123 : double dfFalseEasting, double dfFalseNorthing)
8124 :
8125 : {
8126 0 : VALIDATE_POINTER1(hSRS, "OSRSetVDG", OGRERR_FAILURE);
8127 :
8128 0 : return ToPointer(hSRS)->SetVDG(dfCentralMeridian, dfFalseEasting,
8129 0 : dfFalseNorthing);
8130 : }
8131 :
8132 : /************************************************************************/
8133 : /* SetUTM() */
8134 : /************************************************************************/
8135 :
8136 : /**
8137 : * \brief Set UTM projection definition.
8138 : *
8139 : * This will generate a projection definition with the full set of
8140 : * transverse mercator projection parameters for the given UTM zone.
8141 : * If no PROJCS[] description is set yet, one will be set to look
8142 : * like "UTM Zone %d, {Northern, Southern} Hemisphere".
8143 : *
8144 : * This method is the same as the C function OSRSetUTM().
8145 : *
8146 : * @param nZone UTM zone.
8147 : *
8148 : * @param bNorth TRUE for northern hemisphere, or FALSE for southern
8149 : * hemisphere.
8150 : *
8151 : * @return OGRERR_NONE on success.
8152 : */
8153 :
8154 313 : OGRErr OGRSpatialReference::SetUTM(int nZone, int bNorth)
8155 :
8156 : {
8157 626 : TAKE_OPTIONAL_LOCK();
8158 :
8159 313 : if (nZone < 0 || nZone > 60)
8160 : {
8161 0 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid zone: %d", nZone);
8162 0 : return OGRERR_FAILURE;
8163 : }
8164 :
8165 313 : return d->replaceConversionAndUnref(
8166 313 : proj_create_conversion_utm(d->getPROJContext(), nZone, bNorth));
8167 : }
8168 :
8169 : /************************************************************************/
8170 : /* OSRSetUTM() */
8171 : /************************************************************************/
8172 :
8173 : /**
8174 : * \brief Set UTM projection definition.
8175 : *
8176 : * This is the same as the C++ method OGRSpatialReference::SetUTM()
8177 : */
8178 19 : OGRErr OSRSetUTM(OGRSpatialReferenceH hSRS, int nZone, int bNorth)
8179 :
8180 : {
8181 19 : VALIDATE_POINTER1(hSRS, "OSRSetUTM", OGRERR_FAILURE);
8182 :
8183 19 : return ToPointer(hSRS)->SetUTM(nZone, bNorth);
8184 : }
8185 :
8186 : /************************************************************************/
8187 : /* GetUTMZone() */
8188 : /* */
8189 : /* Returns zero if it isn't UTM. */
8190 : /************************************************************************/
8191 :
8192 : /**
8193 : * \brief Get utm zone information.
8194 : *
8195 : * This is the same as the C function OSRGetUTMZone().
8196 : *
8197 : * In SWIG bindings (Python, Java, etc) the GetUTMZone() method returns a
8198 : * zone which is negative in the southern hemisphere instead of having the
8199 : * pbNorth flag used in the C and C++ interface.
8200 : *
8201 : * @param pbNorth pointer to in to set to TRUE if northern hemisphere, or
8202 : * FALSE if southern.
8203 : *
8204 : * @return UTM zone number or zero if this isn't a UTM definition.
8205 : */
8206 :
8207 611 : int OGRSpatialReference::GetUTMZone(int *pbNorth) const
8208 :
8209 : {
8210 1222 : TAKE_OPTIONAL_LOCK();
8211 :
8212 611 : if (IsProjected() && GetAxesCount() == 3)
8213 : {
8214 1 : OGRSpatialReference *poSRSTmp = Clone();
8215 1 : poSRSTmp->DemoteTo2D(nullptr);
8216 1 : const int nZone = poSRSTmp->GetUTMZone(pbNorth);
8217 1 : delete poSRSTmp;
8218 1 : return nZone;
8219 : }
8220 :
8221 610 : const char *pszProjection = GetAttrValue("PROJECTION");
8222 :
8223 610 : if (pszProjection == nullptr ||
8224 530 : !EQUAL(pszProjection, SRS_PT_TRANSVERSE_MERCATOR))
8225 278 : return 0;
8226 :
8227 332 : if (GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0) != 0.0)
8228 5 : return 0;
8229 :
8230 327 : if (GetProjParm(SRS_PP_SCALE_FACTOR, 1.0) != 0.9996)
8231 15 : return 0;
8232 :
8233 312 : if (fabs(GetNormProjParm(SRS_PP_FALSE_EASTING, 0.0) - 500000.0) > 0.001)
8234 6 : return 0;
8235 :
8236 306 : const double dfFalseNorthing = GetNormProjParm(SRS_PP_FALSE_NORTHING, 0.0);
8237 :
8238 306 : if (dfFalseNorthing != 0.0 && fabs(dfFalseNorthing - 10000000.0) > 0.001)
8239 0 : return 0;
8240 :
8241 306 : if (pbNorth != nullptr)
8242 240 : *pbNorth = (dfFalseNorthing == 0);
8243 :
8244 : const double dfCentralMeridian =
8245 306 : GetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, 0.0);
8246 306 : const double dfZone = (dfCentralMeridian + 186.0) / 6.0;
8247 :
8248 612 : if (dfCentralMeridian < -177.00001 || dfCentralMeridian > 177.000001 ||
8249 918 : std::isnan(dfZone) ||
8250 306 : std::abs(dfZone - static_cast<int>(dfZone) - 0.5) > 0.00001)
8251 0 : return 0;
8252 :
8253 306 : return static_cast<int>(dfZone);
8254 : }
8255 :
8256 : /************************************************************************/
8257 : /* OSRGetUTMZone() */
8258 : /************************************************************************/
8259 :
8260 : /**
8261 : * \brief Get utm zone information.
8262 : *
8263 : * This is the same as the C++ method OGRSpatialReference::GetUTMZone()
8264 : */
8265 6 : int OSRGetUTMZone(OGRSpatialReferenceH hSRS, int *pbNorth)
8266 :
8267 : {
8268 6 : VALIDATE_POINTER1(hSRS, "OSRGetUTMZone", 0);
8269 :
8270 6 : return ToPointer(hSRS)->GetUTMZone(pbNorth);
8271 : }
8272 :
8273 : /************************************************************************/
8274 : /* SetWagner() */
8275 : /************************************************************************/
8276 :
8277 0 : OGRErr OGRSpatialReference::SetWagner(int nVariation, // 1--7.
8278 : double dfCenterLat, double dfFalseEasting,
8279 : double dfFalseNorthing)
8280 :
8281 : {
8282 0 : TAKE_OPTIONAL_LOCK();
8283 :
8284 : PJ *conv;
8285 0 : if (nVariation == 1)
8286 : {
8287 0 : conv = proj_create_conversion_wagner_i(d->getPROJContext(), 0.0,
8288 : dfFalseEasting, dfFalseNorthing,
8289 : nullptr, 0.0, nullptr, 0.0);
8290 : }
8291 0 : else if (nVariation == 2)
8292 : {
8293 0 : conv = proj_create_conversion_wagner_ii(d->getPROJContext(), 0.0,
8294 : dfFalseEasting, dfFalseNorthing,
8295 : nullptr, 0.0, nullptr, 0.0);
8296 : }
8297 0 : else if (nVariation == 3)
8298 : {
8299 0 : conv = proj_create_conversion_wagner_iii(
8300 : d->getPROJContext(), dfCenterLat, 0.0, dfFalseEasting,
8301 : dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
8302 : }
8303 0 : else if (nVariation == 4)
8304 : {
8305 0 : conv = proj_create_conversion_wagner_iv(d->getPROJContext(), 0.0,
8306 : dfFalseEasting, dfFalseNorthing,
8307 : nullptr, 0.0, nullptr, 0.0);
8308 : }
8309 0 : else if (nVariation == 5)
8310 : {
8311 0 : conv = proj_create_conversion_wagner_v(d->getPROJContext(), 0.0,
8312 : dfFalseEasting, dfFalseNorthing,
8313 : nullptr, 0.0, nullptr, 0.0);
8314 : }
8315 0 : else if (nVariation == 6)
8316 : {
8317 0 : conv = proj_create_conversion_wagner_vi(d->getPROJContext(), 0.0,
8318 : dfFalseEasting, dfFalseNorthing,
8319 : nullptr, 0.0, nullptr, 0.0);
8320 : }
8321 0 : else if (nVariation == 7)
8322 : {
8323 0 : conv = proj_create_conversion_wagner_vii(
8324 : d->getPROJContext(), 0.0, dfFalseEasting, dfFalseNorthing, nullptr,
8325 : 0.0, nullptr, 0.0);
8326 : }
8327 : else
8328 : {
8329 0 : CPLError(CE_Failure, CPLE_AppDefined,
8330 : "Unsupported Wagner variation (%d).", nVariation);
8331 0 : return OGRERR_UNSUPPORTED_SRS;
8332 : }
8333 :
8334 0 : return d->replaceConversionAndUnref(conv);
8335 : }
8336 :
8337 : /************************************************************************/
8338 : /* OSRSetWagner() */
8339 : /************************************************************************/
8340 :
8341 0 : OGRErr OSRSetWagner(OGRSpatialReferenceH hSRS, int nVariation,
8342 : double dfCenterLat, double dfFalseEasting,
8343 : double dfFalseNorthing)
8344 :
8345 : {
8346 0 : VALIDATE_POINTER1(hSRS, "OSRSetWagner", OGRERR_FAILURE);
8347 :
8348 0 : return ToPointer(hSRS)->SetWagner(nVariation, dfCenterLat, dfFalseEasting,
8349 0 : dfFalseNorthing);
8350 : }
8351 :
8352 : /************************************************************************/
8353 : /* SetQSC() */
8354 : /************************************************************************/
8355 :
8356 0 : OGRErr OGRSpatialReference::SetQSC(double dfCenterLat, double dfCenterLong)
8357 : {
8358 0 : TAKE_OPTIONAL_LOCK();
8359 :
8360 0 : return d->replaceConversionAndUnref(
8361 : proj_create_conversion_quadrilateralized_spherical_cube(
8362 : d->getPROJContext(), dfCenterLat, dfCenterLong, 0.0, 0.0, nullptr,
8363 0 : 0, nullptr, 0));
8364 : }
8365 :
8366 : /************************************************************************/
8367 : /* OSRSetQSC() */
8368 : /************************************************************************/
8369 :
8370 0 : OGRErr OSRSetQSC(OGRSpatialReferenceH hSRS, double dfCenterLat,
8371 : double dfCenterLong)
8372 :
8373 : {
8374 0 : VALIDATE_POINTER1(hSRS, "OSRSetQSC", OGRERR_FAILURE);
8375 :
8376 0 : return ToPointer(hSRS)->SetQSC(dfCenterLat, dfCenterLong);
8377 : }
8378 :
8379 : /************************************************************************/
8380 : /* SetSCH() */
8381 : /************************************************************************/
8382 :
8383 0 : OGRErr OGRSpatialReference::SetSCH(double dfPegLat, double dfPegLong,
8384 : double dfPegHeading, double dfPegHgt)
8385 :
8386 : {
8387 0 : TAKE_OPTIONAL_LOCK();
8388 :
8389 0 : return d->replaceConversionAndUnref(
8390 : proj_create_conversion_spherical_cross_track_height(
8391 : d->getPROJContext(), dfPegLat, dfPegLong, dfPegHeading, dfPegHgt,
8392 0 : nullptr, 0, nullptr, 0));
8393 : }
8394 :
8395 : /************************************************************************/
8396 : /* OSRSetSCH() */
8397 : /************************************************************************/
8398 :
8399 0 : OGRErr OSRSetSCH(OGRSpatialReferenceH hSRS, double dfPegLat, double dfPegLong,
8400 : double dfPegHeading, double dfPegHgt)
8401 :
8402 : {
8403 0 : VALIDATE_POINTER1(hSRS, "OSRSetSCH", OGRERR_FAILURE);
8404 :
8405 0 : return ToPointer(hSRS)->SetSCH(dfPegLat, dfPegLong, dfPegHeading, dfPegHgt);
8406 : }
8407 :
8408 : /************************************************************************/
8409 : /* SetVerticalPerspective() */
8410 : /************************************************************************/
8411 :
8412 3 : OGRErr OGRSpatialReference::SetVerticalPerspective(
8413 : double dfTopoOriginLat, double dfTopoOriginLon, double dfTopoOriginHeight,
8414 : double dfViewPointHeight, double dfFalseEasting, double dfFalseNorthing)
8415 : {
8416 6 : TAKE_OPTIONAL_LOCK();
8417 :
8418 3 : return d->replaceConversionAndUnref(
8419 : proj_create_conversion_vertical_perspective(
8420 : d->getPROJContext(), dfTopoOriginLat, dfTopoOriginLon,
8421 : dfTopoOriginHeight, dfViewPointHeight, dfFalseEasting,
8422 6 : dfFalseNorthing, nullptr, 0, nullptr, 0));
8423 : }
8424 :
8425 : /************************************************************************/
8426 : /* OSRSetVerticalPerspective() */
8427 : /************************************************************************/
8428 :
8429 1 : OGRErr OSRSetVerticalPerspective(OGRSpatialReferenceH hSRS,
8430 : double dfTopoOriginLat, double dfTopoOriginLon,
8431 : double dfTopoOriginHeight,
8432 : double dfViewPointHeight,
8433 : double dfFalseEasting, double dfFalseNorthing)
8434 :
8435 : {
8436 1 : VALIDATE_POINTER1(hSRS, "OSRSetVerticalPerspective", OGRERR_FAILURE);
8437 :
8438 1 : return ToPointer(hSRS)->SetVerticalPerspective(
8439 : dfTopoOriginLat, dfTopoOriginLon, dfTopoOriginHeight, dfViewPointHeight,
8440 1 : dfFalseEasting, dfFalseNorthing);
8441 : }
8442 :
8443 : /************************************************************************/
8444 : /* SetDerivedGeogCRSWithPoleRotationGRIBConvention() */
8445 : /************************************************************************/
8446 :
8447 2 : OGRErr OGRSpatialReference::SetDerivedGeogCRSWithPoleRotationGRIBConvention(
8448 : const char *pszCRSName, double dfSouthPoleLat, double dfSouthPoleLon,
8449 : double dfAxisRotation)
8450 : {
8451 4 : TAKE_OPTIONAL_LOCK();
8452 :
8453 2 : d->refreshProjObj();
8454 2 : if (!d->m_pj_crs)
8455 0 : return OGRERR_FAILURE;
8456 2 : if (d->m_pjType != PJ_TYPE_GEOGRAPHIC_2D_CRS)
8457 0 : return OGRERR_FAILURE;
8458 2 : auto ctxt = d->getPROJContext();
8459 2 : auto conv = proj_create_conversion_pole_rotation_grib_convention(
8460 : ctxt, dfSouthPoleLat, dfSouthPoleLon, dfAxisRotation, nullptr, 0);
8461 2 : auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
8462 4 : d->setPjCRS(proj_create_derived_geographic_crs(ctxt, pszCRSName,
8463 2 : d->m_pj_crs, conv, cs));
8464 2 : proj_destroy(conv);
8465 2 : proj_destroy(cs);
8466 2 : return OGRERR_NONE;
8467 : }
8468 :
8469 : /************************************************************************/
8470 : /* SetDerivedGeogCRSWithPoleRotationNetCDFCFConvention() */
8471 : /************************************************************************/
8472 :
8473 3 : OGRErr OGRSpatialReference::SetDerivedGeogCRSWithPoleRotationNetCDFCFConvention(
8474 : const char *pszCRSName, double dfGridNorthPoleLat,
8475 : double dfGridNorthPoleLon, double dfNorthPoleGridLon)
8476 : {
8477 3 : TAKE_OPTIONAL_LOCK();
8478 :
8479 : #if PROJ_VERSION_MAJOR > 8 || \
8480 : (PROJ_VERSION_MAJOR == 8 && PROJ_VERSION_MINOR >= 2)
8481 : d->refreshProjObj();
8482 : if (!d->m_pj_crs)
8483 : return OGRERR_FAILURE;
8484 : if (d->m_pjType != PJ_TYPE_GEOGRAPHIC_2D_CRS)
8485 : return OGRERR_FAILURE;
8486 : auto ctxt = d->getPROJContext();
8487 : auto conv = proj_create_conversion_pole_rotation_netcdf_cf_convention(
8488 : ctxt, dfGridNorthPoleLat, dfGridNorthPoleLon, dfNorthPoleGridLon,
8489 : nullptr, 0);
8490 : auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
8491 : d->setPjCRS(proj_create_derived_geographic_crs(ctxt, pszCRSName,
8492 : d->m_pj_crs, conv, cs));
8493 : proj_destroy(conv);
8494 : proj_destroy(cs);
8495 : return OGRERR_NONE;
8496 : #else
8497 : (void)pszCRSName;
8498 3 : SetProjection("Rotated_pole");
8499 3 : SetExtension(
8500 : "PROJCS", "PROJ4",
8501 : CPLSPrintf("+proj=ob_tran +o_proj=longlat +lon_0=%.17g +o_lon_p=%.17g "
8502 : "+o_lat_p=%.17g +a=%.17g +b=%.17g "
8503 : "+to_meter=0.0174532925199433 "
8504 : "+wktext",
8505 : 180.0 + dfGridNorthPoleLon, dfNorthPoleGridLon,
8506 : dfGridNorthPoleLat, GetSemiMajor(nullptr),
8507 : GetSemiMinor(nullptr)));
8508 6 : return OGRERR_NONE;
8509 : #endif
8510 : }
8511 :
8512 : /************************************************************************/
8513 : /* SetAuthority() */
8514 : /************************************************************************/
8515 :
8516 : /**
8517 : * \brief Set the authority for a node.
8518 : *
8519 : * This method is the same as the C function OSRSetAuthority().
8520 : *
8521 : * @param pszTargetKey the partial or complete path to the node to
8522 : * set an authority on. i.e. "PROJCS", "GEOGCS" or "GEOGCS|UNIT".
8523 : *
8524 : * @param pszAuthority authority name, such as "EPSG".
8525 : *
8526 : * @param nCode code for value with this authority.
8527 : *
8528 : * @return OGRERR_NONE on success.
8529 : */
8530 :
8531 12914 : OGRErr OGRSpatialReference::SetAuthority(const char *pszTargetKey,
8532 : const char *pszAuthority, int nCode)
8533 :
8534 : {
8535 25828 : TAKE_OPTIONAL_LOCK();
8536 :
8537 12914 : d->refreshProjObj();
8538 12914 : pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
8539 :
8540 12914 : if (pszTargetKey == nullptr)
8541 : {
8542 265 : if (!d->m_pj_crs)
8543 0 : return OGRERR_FAILURE;
8544 265 : CPLString osCode;
8545 265 : osCode.Printf("%d", nCode);
8546 265 : d->demoteFromBoundCRS();
8547 265 : d->setPjCRS(proj_alter_id(d->getPROJContext(), d->m_pj_crs,
8548 : pszAuthority, osCode.c_str()));
8549 265 : d->undoDemoteFromBoundCRS();
8550 265 : return OGRERR_NONE;
8551 : }
8552 :
8553 12649 : d->demoteFromBoundCRS();
8554 12649 : if (d->m_pjType == PJ_TYPE_PROJECTED_CRS && EQUAL(pszTargetKey, "GEOGCS"))
8555 : {
8556 4183 : CPLString osCode;
8557 4183 : osCode.Printf("%d", nCode);
8558 : auto newGeogCRS =
8559 4183 : proj_alter_id(d->getPROJContext(), d->getGeodBaseCRS(),
8560 : pszAuthority, osCode.c_str());
8561 :
8562 : auto conv =
8563 4183 : proj_crs_get_coordoperation(d->getPROJContext(), d->m_pj_crs);
8564 :
8565 4183 : auto projCRS = proj_create_projected_crs(
8566 : d->getPROJContext(), d->getProjCRSName(), newGeogCRS, conv,
8567 4183 : d->getProjCRSCoordSys());
8568 :
8569 : // Preserve existing id on the PROJCRS
8570 4183 : const char *pszProjCRSAuthName = proj_get_id_auth_name(d->m_pj_crs, 0);
8571 4183 : const char *pszProjCRSCode = proj_get_id_code(d->m_pj_crs, 0);
8572 4183 : if (pszProjCRSAuthName && pszProjCRSCode)
8573 : {
8574 : auto projCRSWithId =
8575 0 : proj_alter_id(d->getPROJContext(), projCRS, pszProjCRSAuthName,
8576 : pszProjCRSCode);
8577 0 : proj_destroy(projCRS);
8578 0 : projCRS = projCRSWithId;
8579 : }
8580 :
8581 4183 : proj_destroy(newGeogCRS);
8582 4183 : proj_destroy(conv);
8583 :
8584 4183 : d->setPjCRS(projCRS);
8585 4183 : d->undoDemoteFromBoundCRS();
8586 4183 : return OGRERR_NONE;
8587 : }
8588 8466 : d->undoDemoteFromBoundCRS();
8589 :
8590 : /* -------------------------------------------------------------------- */
8591 : /* Find the node below which the authority should be put. */
8592 : /* -------------------------------------------------------------------- */
8593 8466 : OGR_SRSNode *poNode = GetAttrNode(pszTargetKey);
8594 :
8595 8466 : if (poNode == nullptr)
8596 0 : return OGRERR_FAILURE;
8597 :
8598 : /* -------------------------------------------------------------------- */
8599 : /* If there is an existing AUTHORITY child blow it away before */
8600 : /* trying to set a new one. */
8601 : /* -------------------------------------------------------------------- */
8602 8466 : int iOldChild = poNode->FindChild("AUTHORITY");
8603 8466 : if (iOldChild != -1)
8604 5 : poNode->DestroyChild(iOldChild);
8605 :
8606 : /* -------------------------------------------------------------------- */
8607 : /* Create a new authority node. */
8608 : /* -------------------------------------------------------------------- */
8609 8466 : char szCode[32] = {};
8610 :
8611 8466 : snprintf(szCode, sizeof(szCode), "%d", nCode);
8612 :
8613 8466 : OGR_SRSNode *poAuthNode = new OGR_SRSNode("AUTHORITY");
8614 8466 : poAuthNode->AddChild(new OGR_SRSNode(pszAuthority));
8615 8466 : poAuthNode->AddChild(new OGR_SRSNode(szCode));
8616 :
8617 8466 : poNode->AddChild(poAuthNode);
8618 :
8619 8466 : return OGRERR_NONE;
8620 : }
8621 :
8622 : /************************************************************************/
8623 : /* OSRSetAuthority() */
8624 : /************************************************************************/
8625 :
8626 : /**
8627 : * \brief Set the authority for a node.
8628 : *
8629 : * This function is the same as OGRSpatialReference::SetAuthority().
8630 : */
8631 0 : OGRErr OSRSetAuthority(OGRSpatialReferenceH hSRS, const char *pszTargetKey,
8632 : const char *pszAuthority, int nCode)
8633 :
8634 : {
8635 0 : VALIDATE_POINTER1(hSRS, "OSRSetAuthority", OGRERR_FAILURE);
8636 :
8637 0 : return ToPointer(hSRS)->SetAuthority(pszTargetKey, pszAuthority, nCode);
8638 : }
8639 :
8640 : /************************************************************************/
8641 : /* GetAuthorityCode() */
8642 : /************************************************************************/
8643 :
8644 : /**
8645 : * \brief Get the authority code for a node.
8646 : *
8647 : * This method is used to query an AUTHORITY[] node from within the
8648 : * WKT tree, and fetch the code value.
8649 : *
8650 : * While in theory values may be non-numeric, for the EPSG authority all
8651 : * code values should be integral.
8652 : *
8653 : * This method is the same as the C function OSRGetAuthorityCode().
8654 : *
8655 : * @param pszTargetKey the partial or complete path to the node to
8656 : * get an authority from. i.e. "PROJCS", "GEOGCS", "GEOGCS|UNIT" or NULL to
8657 : * search for an authority node on the root element.
8658 : *
8659 : * @return value code from authority node, or NULL on failure. The value
8660 : * returned is internal and should not be freed or modified.
8661 : */
8662 :
8663 : const char *
8664 52300 : OGRSpatialReference::GetAuthorityCode(const char *pszTargetKey) const
8665 :
8666 : {
8667 104600 : TAKE_OPTIONAL_LOCK();
8668 :
8669 52300 : d->refreshProjObj();
8670 52300 : const char *pszInputTargetKey = pszTargetKey;
8671 52300 : pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
8672 52300 : if (pszTargetKey == nullptr)
8673 : {
8674 43815 : if (!d->m_pj_crs)
8675 : {
8676 17 : return nullptr;
8677 : }
8678 43798 : d->demoteFromBoundCRS();
8679 43798 : auto ret = proj_get_id_code(d->m_pj_crs, 0);
8680 43798 : if (ret == nullptr && d->m_pjType == PJ_TYPE_PROJECTED_CRS)
8681 : {
8682 1387 : auto ctxt = d->getPROJContext();
8683 1387 : auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
8684 1387 : if (cs)
8685 : {
8686 1387 : const int axisCount = proj_cs_get_axis_count(ctxt, cs);
8687 1387 : proj_destroy(cs);
8688 1387 : if (axisCount == 3)
8689 : {
8690 : // This might come from a COMPD_CS with a VERT_DATUM type =
8691 : // 2002 in which case, using the WKT1 representation will
8692 : // enable us to recover the EPSG code.
8693 14 : pszTargetKey = pszInputTargetKey;
8694 : }
8695 : }
8696 : }
8697 43798 : d->undoDemoteFromBoundCRS();
8698 43798 : if (ret != nullptr || pszTargetKey == nullptr)
8699 : {
8700 43798 : return ret;
8701 : }
8702 : }
8703 :
8704 : // Special key for that context
8705 8489 : else if (EQUAL(pszTargetKey, "HORIZCRS") &&
8706 4 : d->m_pjType == PJ_TYPE_COMPOUND_CRS)
8707 : {
8708 4 : auto ctxt = d->getPROJContext();
8709 4 : auto crs = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
8710 4 : if (crs)
8711 : {
8712 4 : const char *ret = proj_get_id_code(crs, 0);
8713 4 : if (ret)
8714 4 : ret = CPLSPrintf("%s", ret);
8715 4 : proj_destroy(crs);
8716 4 : return ret;
8717 : }
8718 : }
8719 8485 : else if (EQUAL(pszTargetKey, "VERTCRS") &&
8720 4 : d->m_pjType == PJ_TYPE_COMPOUND_CRS)
8721 : {
8722 4 : auto ctxt = d->getPROJContext();
8723 4 : auto crs = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 1);
8724 4 : if (crs)
8725 : {
8726 4 : const char *ret = proj_get_id_code(crs, 0);
8727 4 : if (ret)
8728 4 : ret = CPLSPrintf("%s", ret);
8729 4 : proj_destroy(crs);
8730 4 : return ret;
8731 : }
8732 : }
8733 :
8734 : /* -------------------------------------------------------------------- */
8735 : /* Find the node below which the authority should be put. */
8736 : /* -------------------------------------------------------------------- */
8737 8477 : const OGR_SRSNode *poNode = GetAttrNode(pszTargetKey);
8738 :
8739 8477 : if (poNode == nullptr)
8740 106 : return nullptr;
8741 :
8742 : /* -------------------------------------------------------------------- */
8743 : /* Fetch AUTHORITY child if there is one. */
8744 : /* -------------------------------------------------------------------- */
8745 8371 : if (poNode->FindChild("AUTHORITY") == -1)
8746 197 : return nullptr;
8747 :
8748 8174 : poNode = poNode->GetChild(poNode->FindChild("AUTHORITY"));
8749 :
8750 : /* -------------------------------------------------------------------- */
8751 : /* Create a new authority node. */
8752 : /* -------------------------------------------------------------------- */
8753 8174 : if (poNode->GetChildCount() < 2)
8754 0 : return nullptr;
8755 :
8756 8174 : return poNode->GetChild(1)->GetValue();
8757 : }
8758 :
8759 : /************************************************************************/
8760 : /* OSRGetAuthorityCode() */
8761 : /************************************************************************/
8762 :
8763 : /**
8764 : * \brief Get the authority code for a node.
8765 : *
8766 : * This function is the same as OGRSpatialReference::GetAuthorityCode().
8767 : */
8768 830 : const char *OSRGetAuthorityCode(OGRSpatialReferenceH hSRS,
8769 : const char *pszTargetKey)
8770 :
8771 : {
8772 830 : VALIDATE_POINTER1(hSRS, "OSRGetAuthorityCode", nullptr);
8773 :
8774 830 : return ToPointer(hSRS)->GetAuthorityCode(pszTargetKey);
8775 : }
8776 :
8777 : /************************************************************************/
8778 : /* GetAuthorityName() */
8779 : /************************************************************************/
8780 :
8781 : /**
8782 : * \brief Get the authority name for a node.
8783 : *
8784 : * This method is used to query an AUTHORITY[] node from within the
8785 : * WKT tree, and fetch the authority name value.
8786 : *
8787 : * The most common authority is "EPSG".
8788 : *
8789 : * This method is the same as the C function OSRGetAuthorityName().
8790 : *
8791 : * @param pszTargetKey the partial or complete path to the node to
8792 : * get an authority from. i.e. "PROJCS", "GEOGCS", "GEOGCS|UNIT" or NULL to
8793 : * search for an authority node on the root element.
8794 : *
8795 : * @return value code from authority node, or NULL on failure. The value
8796 : * returned is internal and should not be freed or modified.
8797 : */
8798 :
8799 : const char *
8800 66191 : OGRSpatialReference::GetAuthorityName(const char *pszTargetKey) const
8801 :
8802 : {
8803 132382 : TAKE_OPTIONAL_LOCK();
8804 :
8805 66191 : d->refreshProjObj();
8806 66191 : const char *pszInputTargetKey = pszTargetKey;
8807 66191 : pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
8808 66191 : if (pszTargetKey == nullptr)
8809 : {
8810 36824 : if (!d->m_pj_crs)
8811 : {
8812 18 : return nullptr;
8813 : }
8814 36806 : d->demoteFromBoundCRS();
8815 36806 : auto ret = proj_get_id_auth_name(d->m_pj_crs, 0);
8816 36806 : if (ret == nullptr && d->m_pjType == PJ_TYPE_PROJECTED_CRS)
8817 : {
8818 1009 : auto ctxt = d->getPROJContext();
8819 1009 : auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
8820 1009 : if (cs)
8821 : {
8822 1009 : const int axisCount = proj_cs_get_axis_count(ctxt, cs);
8823 1009 : proj_destroy(cs);
8824 1009 : if (axisCount == 3)
8825 : {
8826 : // This might come from a COMPD_CS with a VERT_DATUM type =
8827 : // 2002 in which case, using the WKT1 representation will
8828 : // enable us to recover the EPSG code.
8829 14 : pszTargetKey = pszInputTargetKey;
8830 : }
8831 : }
8832 : }
8833 36806 : d->undoDemoteFromBoundCRS();
8834 36806 : if (ret != nullptr || pszTargetKey == nullptr)
8835 : {
8836 36806 : return ret;
8837 : }
8838 : }
8839 :
8840 : // Special key for that context
8841 29371 : else if (EQUAL(pszTargetKey, "HORIZCRS") &&
8842 4 : d->m_pjType == PJ_TYPE_COMPOUND_CRS)
8843 : {
8844 4 : auto ctxt = d->getPROJContext();
8845 4 : auto crs = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
8846 4 : if (crs)
8847 : {
8848 4 : const char *ret = proj_get_id_auth_name(crs, 0);
8849 4 : if (ret)
8850 4 : ret = CPLSPrintf("%s", ret);
8851 4 : proj_destroy(crs);
8852 4 : return ret;
8853 : }
8854 : }
8855 29367 : else if (EQUAL(pszTargetKey, "VERTCRS") &&
8856 4 : d->m_pjType == PJ_TYPE_COMPOUND_CRS)
8857 : {
8858 4 : auto ctxt = d->getPROJContext();
8859 4 : auto crs = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 1);
8860 4 : if (crs)
8861 : {
8862 4 : const char *ret = proj_get_id_auth_name(crs, 0);
8863 4 : if (ret)
8864 4 : ret = CPLSPrintf("%s", ret);
8865 4 : proj_destroy(crs);
8866 4 : return ret;
8867 : }
8868 : }
8869 :
8870 : /* -------------------------------------------------------------------- */
8871 : /* Find the node below which the authority should be put. */
8872 : /* -------------------------------------------------------------------- */
8873 29359 : const OGR_SRSNode *poNode = GetAttrNode(pszTargetKey);
8874 :
8875 29359 : if (poNode == nullptr)
8876 12045 : return nullptr;
8877 :
8878 : /* -------------------------------------------------------------------- */
8879 : /* Fetch AUTHORITY child if there is one. */
8880 : /* -------------------------------------------------------------------- */
8881 17314 : if (poNode->FindChild("AUTHORITY") == -1)
8882 1597 : return nullptr;
8883 :
8884 15717 : poNode = poNode->GetChild(poNode->FindChild("AUTHORITY"));
8885 :
8886 : /* -------------------------------------------------------------------- */
8887 : /* Create a new authority node. */
8888 : /* -------------------------------------------------------------------- */
8889 15717 : if (poNode->GetChildCount() < 2)
8890 0 : return nullptr;
8891 :
8892 15717 : return poNode->GetChild(0)->GetValue();
8893 : }
8894 :
8895 : /************************************************************************/
8896 : /* OSRGetAuthorityName() */
8897 : /************************************************************************/
8898 :
8899 : /**
8900 : * \brief Get the authority name for a node.
8901 : *
8902 : * This function is the same as OGRSpatialReference::GetAuthorityName().
8903 : */
8904 94 : const char *OSRGetAuthorityName(OGRSpatialReferenceH hSRS,
8905 : const char *pszTargetKey)
8906 :
8907 : {
8908 94 : VALIDATE_POINTER1(hSRS, "OSRGetAuthorityName", nullptr);
8909 :
8910 94 : return ToPointer(hSRS)->GetAuthorityName(pszTargetKey);
8911 : }
8912 :
8913 : /************************************************************************/
8914 : /* GetOGCURN() */
8915 : /************************************************************************/
8916 :
8917 : /**
8918 : * \brief Get a OGC URN string describing the CRS, when possible
8919 : *
8920 : * This method assumes that the CRS has a top-level identifier, or is
8921 : * a compound CRS whose horizontal and vertical parts have a top-level
8922 : * identifier.
8923 : *
8924 : * @return a string to free with CPLFree(), or nullptr when no result can be
8925 : * generated
8926 : *
8927 : * @since GDAL 3.5
8928 : */
8929 :
8930 68 : char *OGRSpatialReference::GetOGCURN() const
8931 :
8932 : {
8933 136 : TAKE_OPTIONAL_LOCK();
8934 :
8935 68 : const char *pszAuthName = GetAuthorityName();
8936 68 : const char *pszAuthCode = GetAuthorityCode();
8937 68 : if (pszAuthName && pszAuthCode)
8938 65 : return CPLStrdup(
8939 65 : CPLSPrintf("urn:ogc:def:crs:%s::%s", pszAuthName, pszAuthCode));
8940 3 : if (d->m_pjType != PJ_TYPE_COMPOUND_CRS)
8941 2 : return nullptr;
8942 1 : auto horizCRS = proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
8943 1 : auto vertCRS = proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 1);
8944 1 : char *pszRet = nullptr;
8945 1 : if (horizCRS && vertCRS)
8946 : {
8947 1 : auto horizAuthName = proj_get_id_auth_name(horizCRS, 0);
8948 1 : auto horizAuthCode = proj_get_id_code(horizCRS, 0);
8949 1 : auto vertAuthName = proj_get_id_auth_name(vertCRS, 0);
8950 1 : auto vertAuthCode = proj_get_id_code(vertCRS, 0);
8951 1 : if (horizAuthName && horizAuthCode && vertAuthName && vertAuthCode)
8952 : {
8953 1 : pszRet = CPLStrdup(CPLSPrintf(
8954 : "urn:ogc:def:crs,crs:%s::%s,crs:%s::%s", horizAuthName,
8955 : horizAuthCode, vertAuthName, vertAuthCode));
8956 : }
8957 : }
8958 1 : proj_destroy(horizCRS);
8959 1 : proj_destroy(vertCRS);
8960 1 : return pszRet;
8961 : }
8962 :
8963 : /************************************************************************/
8964 : /* StripVertical() */
8965 : /************************************************************************/
8966 :
8967 : /**
8968 : * \brief Convert a compound cs into a horizontal CS.
8969 : *
8970 : * If this SRS is of type COMPD_CS[] then the vertical CS and the root COMPD_CS
8971 : * nodes are stripped resulting and only the horizontal coordinate system
8972 : * portion remains (normally PROJCS, GEOGCS or LOCAL_CS).
8973 : *
8974 : * If this is not a compound coordinate system then nothing is changed.
8975 : *
8976 : * This method is the same as the C function OSRStripVertical().
8977 : *
8978 : */
8979 :
8980 47 : OGRErr OGRSpatialReference::StripVertical()
8981 :
8982 : {
8983 94 : TAKE_OPTIONAL_LOCK();
8984 :
8985 47 : d->refreshProjObj();
8986 47 : d->demoteFromBoundCRS();
8987 47 : if (!d->m_pj_crs || d->m_pjType != PJ_TYPE_COMPOUND_CRS)
8988 : {
8989 0 : d->undoDemoteFromBoundCRS();
8990 0 : return OGRERR_NONE;
8991 : }
8992 47 : auto horizCRS = proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
8993 47 : if (!horizCRS)
8994 : {
8995 0 : d->undoDemoteFromBoundCRS();
8996 0 : return OGRERR_FAILURE;
8997 : }
8998 :
8999 47 : bool reuseExistingBoundCRS = false;
9000 47 : if (d->m_pj_bound_crs_target)
9001 : {
9002 4 : auto type = proj_get_type(d->m_pj_bound_crs_target);
9003 8 : reuseExistingBoundCRS = type == PJ_TYPE_GEOCENTRIC_CRS ||
9004 8 : type == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
9005 : type == PJ_TYPE_GEOGRAPHIC_3D_CRS;
9006 : }
9007 :
9008 47 : if (reuseExistingBoundCRS)
9009 : {
9010 4 : auto newBoundCRS = proj_crs_create_bound_crs(
9011 4 : d->getPROJContext(), horizCRS, d->m_pj_bound_crs_target,
9012 4 : d->m_pj_bound_crs_co);
9013 4 : proj_destroy(horizCRS);
9014 4 : d->undoDemoteFromBoundCRS();
9015 4 : d->setPjCRS(newBoundCRS);
9016 : }
9017 : else
9018 : {
9019 43 : d->undoDemoteFromBoundCRS();
9020 43 : d->setPjCRS(horizCRS);
9021 : }
9022 :
9023 47 : return OGRERR_NONE;
9024 : }
9025 :
9026 : /************************************************************************/
9027 : /* OSRStripVertical() */
9028 : /************************************************************************/
9029 : /**
9030 : * \brief Convert a compound cs into a horizontal CS.
9031 : *
9032 : * This function is the same as the C++ method
9033 : * OGRSpatialReference::StripVertical().
9034 : */
9035 1 : OGRErr OSRStripVertical(OGRSpatialReferenceH hSRS)
9036 :
9037 : {
9038 1 : VALIDATE_POINTER1(hSRS, "OSRStripVertical", OGRERR_FAILURE);
9039 :
9040 1 : return OGRSpatialReference::FromHandle(hSRS)->StripVertical();
9041 : }
9042 :
9043 : /************************************************************************/
9044 : /* StripTOWGS84IfKnownDatumAndAllowed() */
9045 : /************************************************************************/
9046 :
9047 : /**
9048 : * \brief Remove TOWGS84 information if the CRS has a known horizontal datum
9049 : * and this is allowed by the user.
9050 : *
9051 : * The default behavior is to remove TOWGS84 information if the CRS has a
9052 : * known horizontal datum. This can be disabled by setting the
9053 : * OSR_STRIP_TOWGS84 configuration option to NO.
9054 : *
9055 : * @return true if TOWGS84 has been removed.
9056 : * @since OGR 3.1.0
9057 : */
9058 :
9059 9641 : bool OGRSpatialReference::StripTOWGS84IfKnownDatumAndAllowed()
9060 : {
9061 9641 : if (CPLTestBool(CPLGetConfigOption("OSR_STRIP_TOWGS84", "YES")))
9062 : {
9063 9638 : if (StripTOWGS84IfKnownDatum())
9064 : {
9065 72 : CPLDebug("OSR", "TOWGS84 information has been removed. "
9066 : "It can be kept by setting the OSR_STRIP_TOWGS84 "
9067 : "configuration option to NO");
9068 72 : return true;
9069 : }
9070 : }
9071 9569 : return false;
9072 : }
9073 :
9074 : /************************************************************************/
9075 : /* StripTOWGS84IfKnownDatum() */
9076 : /************************************************************************/
9077 :
9078 : /**
9079 : * \brief Remove TOWGS84 information if the CRS has a known horizontal datum
9080 : *
9081 : * @return true if TOWGS84 has been removed.
9082 : * @since OGR 3.1.0
9083 : */
9084 :
9085 9644 : bool OGRSpatialReference::StripTOWGS84IfKnownDatum()
9086 :
9087 : {
9088 19288 : TAKE_OPTIONAL_LOCK();
9089 :
9090 9644 : d->refreshProjObj();
9091 9644 : if (!d->m_pj_crs || d->m_pjType != PJ_TYPE_BOUND_CRS)
9092 : {
9093 9552 : return false;
9094 : }
9095 92 : auto ctxt = d->getPROJContext();
9096 92 : auto baseCRS = proj_get_source_crs(ctxt, d->m_pj_crs);
9097 92 : if (proj_get_type(baseCRS) == PJ_TYPE_COMPOUND_CRS)
9098 : {
9099 3 : proj_destroy(baseCRS);
9100 3 : return false;
9101 : }
9102 :
9103 : // Known base CRS code ? Return base CRS
9104 89 : const char *pszCode = proj_get_id_code(baseCRS, 0);
9105 89 : if (pszCode)
9106 : {
9107 2 : d->setPjCRS(baseCRS);
9108 2 : return true;
9109 : }
9110 :
9111 87 : auto datum = proj_crs_get_datum(ctxt, baseCRS);
9112 : #if PROJ_VERSION_MAJOR > 7 || \
9113 : (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
9114 : if (datum == nullptr)
9115 : {
9116 : datum = proj_crs_get_datum_ensemble(ctxt, baseCRS);
9117 : }
9118 : #endif
9119 87 : if (!datum)
9120 : {
9121 0 : proj_destroy(baseCRS);
9122 0 : return false;
9123 : }
9124 :
9125 : // Known datum code ? Return base CRS
9126 87 : pszCode = proj_get_id_code(datum, 0);
9127 87 : if (pszCode)
9128 : {
9129 3 : proj_destroy(datum);
9130 3 : d->setPjCRS(baseCRS);
9131 3 : return true;
9132 : }
9133 :
9134 84 : const char *name = proj_get_name(datum);
9135 84 : if (EQUAL(name, "unknown"))
9136 : {
9137 1 : proj_destroy(datum);
9138 1 : proj_destroy(baseCRS);
9139 1 : return false;
9140 : }
9141 83 : const PJ_TYPE type = PJ_TYPE_GEODETIC_REFERENCE_FRAME;
9142 : PJ_OBJ_LIST *list =
9143 83 : proj_create_from_name(ctxt, nullptr, name, &type, 1, false, 1, nullptr);
9144 :
9145 83 : bool knownDatumName = false;
9146 83 : if (list)
9147 : {
9148 83 : if (proj_list_get_count(list) == 1)
9149 : {
9150 70 : knownDatumName = true;
9151 : }
9152 83 : proj_list_destroy(list);
9153 : }
9154 :
9155 83 : proj_destroy(datum);
9156 83 : if (knownDatumName)
9157 : {
9158 70 : d->setPjCRS(baseCRS);
9159 70 : return true;
9160 : }
9161 13 : proj_destroy(baseCRS);
9162 13 : return false;
9163 : }
9164 :
9165 : /************************************************************************/
9166 : /* IsCompound() */
9167 : /************************************************************************/
9168 :
9169 : /**
9170 : * \brief Check if coordinate system is compound.
9171 : *
9172 : * This method is the same as the C function OSRIsCompound().
9173 : *
9174 : * @return TRUE if this is rooted with a COMPD_CS node.
9175 : */
9176 :
9177 43178 : int OGRSpatialReference::IsCompound() const
9178 :
9179 : {
9180 43178 : TAKE_OPTIONAL_LOCK();
9181 :
9182 43178 : d->refreshProjObj();
9183 43178 : d->demoteFromBoundCRS();
9184 43178 : bool isCompound = d->m_pjType == PJ_TYPE_COMPOUND_CRS;
9185 43178 : d->undoDemoteFromBoundCRS();
9186 86356 : return isCompound;
9187 : }
9188 :
9189 : /************************************************************************/
9190 : /* OSRIsCompound() */
9191 : /************************************************************************/
9192 :
9193 : /**
9194 : * \brief Check if the coordinate system is compound.
9195 : *
9196 : * This function is the same as OGRSpatialReference::IsCompound().
9197 : */
9198 5 : int OSRIsCompound(OGRSpatialReferenceH hSRS)
9199 :
9200 : {
9201 5 : VALIDATE_POINTER1(hSRS, "OSRIsCompound", 0);
9202 :
9203 5 : return ToPointer(hSRS)->IsCompound();
9204 : }
9205 :
9206 : /************************************************************************/
9207 : /* IsProjected() */
9208 : /************************************************************************/
9209 :
9210 : /**
9211 : * \brief Check if projected coordinate system.
9212 : *
9213 : * This method is the same as the C function OSRIsProjected().
9214 : *
9215 : * @return TRUE if this contains a PROJCS node indicating a it is a
9216 : * projected coordinate system. Also if it is a CompoundCRS made of a
9217 : * ProjectedCRS
9218 : */
9219 :
9220 46235 : int OGRSpatialReference::IsProjected() const
9221 :
9222 : {
9223 46235 : TAKE_OPTIONAL_LOCK();
9224 :
9225 46235 : d->refreshProjObj();
9226 46235 : d->demoteFromBoundCRS();
9227 46235 : bool isProjected = d->m_pjType == PJ_TYPE_PROJECTED_CRS;
9228 46235 : if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
9229 : {
9230 : auto horizCRS =
9231 151 : proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
9232 151 : if (horizCRS)
9233 : {
9234 151 : auto horizCRSType = proj_get_type(horizCRS);
9235 151 : isProjected = horizCRSType == PJ_TYPE_PROJECTED_CRS;
9236 151 : if (horizCRSType == PJ_TYPE_BOUND_CRS)
9237 : {
9238 6 : auto base = proj_get_source_crs(d->getPROJContext(), horizCRS);
9239 6 : if (base)
9240 : {
9241 6 : isProjected = proj_get_type(base) == PJ_TYPE_PROJECTED_CRS;
9242 6 : proj_destroy(base);
9243 : }
9244 : }
9245 151 : proj_destroy(horizCRS);
9246 : }
9247 : }
9248 46235 : d->undoDemoteFromBoundCRS();
9249 92470 : return isProjected;
9250 : }
9251 :
9252 : /************************************************************************/
9253 : /* OSRIsProjected() */
9254 : /************************************************************************/
9255 : /**
9256 : * \brief Check if projected coordinate system.
9257 : *
9258 : * This function is the same as OGRSpatialReference::IsProjected().
9259 : */
9260 439 : int OSRIsProjected(OGRSpatialReferenceH hSRS)
9261 :
9262 : {
9263 439 : VALIDATE_POINTER1(hSRS, "OSRIsProjected", 0);
9264 :
9265 439 : return ToPointer(hSRS)->IsProjected();
9266 : }
9267 :
9268 : /************************************************************************/
9269 : /* IsGeocentric() */
9270 : /************************************************************************/
9271 :
9272 : /**
9273 : * \brief Check if geocentric coordinate system.
9274 : *
9275 : * This method is the same as the C function OSRIsGeocentric().
9276 : *
9277 : * @return TRUE if this contains a GEOCCS node indicating a it is a
9278 : * geocentric coordinate system.
9279 : *
9280 : */
9281 :
9282 17962 : int OGRSpatialReference::IsGeocentric() const
9283 :
9284 : {
9285 17962 : TAKE_OPTIONAL_LOCK();
9286 :
9287 17962 : d->refreshProjObj();
9288 17962 : d->demoteFromBoundCRS();
9289 17962 : bool isGeocentric = d->m_pjType == PJ_TYPE_GEOCENTRIC_CRS;
9290 17962 : d->undoDemoteFromBoundCRS();
9291 35924 : return isGeocentric;
9292 : }
9293 :
9294 : /************************************************************************/
9295 : /* OSRIsGeocentric() */
9296 : /************************************************************************/
9297 : /**
9298 : * \brief Check if geocentric coordinate system.
9299 : *
9300 : * This function is the same as OGRSpatialReference::IsGeocentric().
9301 : *
9302 : */
9303 2 : int OSRIsGeocentric(OGRSpatialReferenceH hSRS)
9304 :
9305 : {
9306 2 : VALIDATE_POINTER1(hSRS, "OSRIsGeocentric", 0);
9307 :
9308 2 : return ToPointer(hSRS)->IsGeocentric();
9309 : }
9310 :
9311 : /************************************************************************/
9312 : /* IsEmpty() */
9313 : /************************************************************************/
9314 :
9315 : /**
9316 : * \brief Return if the SRS is not set.
9317 : */
9318 :
9319 120382 : bool OGRSpatialReference::IsEmpty() const
9320 : {
9321 120382 : TAKE_OPTIONAL_LOCK();
9322 :
9323 120382 : d->refreshProjObj();
9324 240764 : return d->m_pj_crs == nullptr;
9325 : }
9326 :
9327 : /************************************************************************/
9328 : /* IsGeographic() */
9329 : /************************************************************************/
9330 :
9331 : /**
9332 : * \brief Check if geographic coordinate system.
9333 : *
9334 : * This method is the same as the C function OSRIsGeographic().
9335 : *
9336 : * @return TRUE if this spatial reference is geographic ... that is the
9337 : * root is a GEOGCS node. Also if it is a CompoundCRS made of a
9338 : * GeographicCRS
9339 : */
9340 :
9341 64062 : int OGRSpatialReference::IsGeographic() const
9342 :
9343 : {
9344 64062 : TAKE_OPTIONAL_LOCK();
9345 :
9346 64062 : d->refreshProjObj();
9347 64062 : d->demoteFromBoundCRS();
9348 89824 : bool isGeog = d->m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
9349 25762 : d->m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS;
9350 64062 : if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
9351 : {
9352 : auto horizCRS =
9353 328 : proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
9354 328 : if (horizCRS)
9355 : {
9356 328 : auto horizCRSType = proj_get_type(horizCRS);
9357 328 : isGeog = horizCRSType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
9358 : horizCRSType == PJ_TYPE_GEOGRAPHIC_3D_CRS;
9359 328 : if (horizCRSType == PJ_TYPE_BOUND_CRS)
9360 : {
9361 13 : auto base = proj_get_source_crs(d->getPROJContext(), horizCRS);
9362 13 : if (base)
9363 : {
9364 13 : horizCRSType = proj_get_type(base);
9365 13 : isGeog = horizCRSType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
9366 : horizCRSType == PJ_TYPE_GEOGRAPHIC_3D_CRS;
9367 13 : proj_destroy(base);
9368 : }
9369 : }
9370 328 : proj_destroy(horizCRS);
9371 : }
9372 : }
9373 64062 : d->undoDemoteFromBoundCRS();
9374 128124 : return isGeog;
9375 : }
9376 :
9377 : /************************************************************************/
9378 : /* OSRIsGeographic() */
9379 : /************************************************************************/
9380 : /**
9381 : * \brief Check if geographic coordinate system.
9382 : *
9383 : * This function is the same as OGRSpatialReference::IsGeographic().
9384 : */
9385 284 : int OSRIsGeographic(OGRSpatialReferenceH hSRS)
9386 :
9387 : {
9388 284 : VALIDATE_POINTER1(hSRS, "OSRIsGeographic", 0);
9389 :
9390 284 : return ToPointer(hSRS)->IsGeographic();
9391 : }
9392 :
9393 : /************************************************************************/
9394 : /* IsDerivedGeographic() */
9395 : /************************************************************************/
9396 :
9397 : /**
9398 : * \brief Check if the CRS is a derived geographic coordinate system.
9399 : * (for example a rotated long/lat grid)
9400 : *
9401 : * This method is the same as the C function OSRIsDerivedGeographic().
9402 : *
9403 : * @since GDAL 3.1.0 and PROJ 6.3.0
9404 : */
9405 :
9406 15727 : int OGRSpatialReference::IsDerivedGeographic() const
9407 :
9408 : {
9409 15727 : TAKE_OPTIONAL_LOCK();
9410 :
9411 15727 : d->refreshProjObj();
9412 15727 : d->demoteFromBoundCRS();
9413 25753 : const bool isGeog = d->m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
9414 10026 : d->m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS;
9415 : const bool isDerivedGeographic =
9416 15727 : isGeog && proj_is_derived_crs(d->getPROJContext(), d->m_pj_crs);
9417 15727 : d->undoDemoteFromBoundCRS();
9418 31454 : return isDerivedGeographic ? TRUE : FALSE;
9419 : }
9420 :
9421 : /************************************************************************/
9422 : /* OSRIsDerivedGeographic() */
9423 : /************************************************************************/
9424 : /**
9425 : * \brief Check if the CRS is a derived geographic coordinate system.
9426 : * (for example a rotated long/lat grid)
9427 : *
9428 : * This function is the same as OGRSpatialReference::IsDerivedGeographic().
9429 : */
9430 1 : int OSRIsDerivedGeographic(OGRSpatialReferenceH hSRS)
9431 :
9432 : {
9433 1 : VALIDATE_POINTER1(hSRS, "OSRIsDerivedGeographic", 0);
9434 :
9435 1 : return ToPointer(hSRS)->IsDerivedGeographic();
9436 : }
9437 :
9438 : /************************************************************************/
9439 : /* IsDerivedProjected() */
9440 : /************************************************************************/
9441 :
9442 : /**
9443 : * \brief Check if the CRS is a derived projected coordinate system.
9444 : *
9445 : * This method is the same as the C function OSRIsDerivedGeographic().
9446 : *
9447 : * @since GDAL 3.9.0 (and may only return non-zero starting with PROJ 9.2.0)
9448 : */
9449 :
9450 0 : int OGRSpatialReference::IsDerivedProjected() const
9451 :
9452 : {
9453 : #if PROJ_AT_LEAST_VERSION(9, 2, 0)
9454 : TAKE_OPTIONAL_LOCK();
9455 : d->refreshProjObj();
9456 : d->demoteFromBoundCRS();
9457 : const bool isDerivedProjected =
9458 : d->m_pjType == PJ_TYPE_DERIVED_PROJECTED_CRS;
9459 : d->undoDemoteFromBoundCRS();
9460 : return isDerivedProjected ? TRUE : FALSE;
9461 : #else
9462 0 : return FALSE;
9463 : #endif
9464 : }
9465 :
9466 : /************************************************************************/
9467 : /* OSRIsDerivedProjected() */
9468 : /************************************************************************/
9469 : /**
9470 : * \brief Check if the CRS is a derived projected coordinate system.
9471 : *
9472 : * This function is the same as OGRSpatialReference::IsDerivedProjected().
9473 : *
9474 : * @since GDAL 3.9.0 (and may only return non-zero starting with PROJ 9.2.0)
9475 : */
9476 0 : int OSRIsDerivedProjected(OGRSpatialReferenceH hSRS)
9477 :
9478 : {
9479 0 : VALIDATE_POINTER1(hSRS, "OSRIsDerivedProjected", 0);
9480 :
9481 0 : return ToPointer(hSRS)->IsDerivedProjected();
9482 : }
9483 :
9484 : /************************************************************************/
9485 : /* IsLocal() */
9486 : /************************************************************************/
9487 :
9488 : /**
9489 : * \brief Check if local coordinate system.
9490 : *
9491 : * This method is the same as the C function OSRIsLocal().
9492 : *
9493 : * @return TRUE if this spatial reference is local ... that is the
9494 : * root is a LOCAL_CS node.
9495 : */
9496 :
9497 8276 : int OGRSpatialReference::IsLocal() const
9498 :
9499 : {
9500 8276 : TAKE_OPTIONAL_LOCK();
9501 8276 : d->refreshProjObj();
9502 16552 : return d->m_pjType == PJ_TYPE_ENGINEERING_CRS;
9503 : }
9504 :
9505 : /************************************************************************/
9506 : /* OSRIsLocal() */
9507 : /************************************************************************/
9508 : /**
9509 : * \brief Check if local coordinate system.
9510 : *
9511 : * This function is the same as OGRSpatialReference::IsLocal().
9512 : */
9513 8 : int OSRIsLocal(OGRSpatialReferenceH hSRS)
9514 :
9515 : {
9516 8 : VALIDATE_POINTER1(hSRS, "OSRIsLocal", 0);
9517 :
9518 8 : return ToPointer(hSRS)->IsLocal();
9519 : }
9520 :
9521 : /************************************************************************/
9522 : /* IsVertical() */
9523 : /************************************************************************/
9524 :
9525 : /**
9526 : * \brief Check if vertical coordinate system.
9527 : *
9528 : * This method is the same as the C function OSRIsVertical().
9529 : *
9530 : * @return TRUE if this contains a VERT_CS node indicating a it is a
9531 : * vertical coordinate system. Also if it is a CompoundCRS made of a
9532 : * VerticalCRS
9533 : *
9534 : */
9535 :
9536 9269 : int OGRSpatialReference::IsVertical() const
9537 :
9538 : {
9539 9269 : TAKE_OPTIONAL_LOCK();
9540 9269 : d->refreshProjObj();
9541 9269 : d->demoteFromBoundCRS();
9542 9269 : bool isVertical = d->m_pjType == PJ_TYPE_VERTICAL_CRS;
9543 9269 : if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
9544 : {
9545 : auto vertCRS =
9546 34 : proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 1);
9547 34 : if (vertCRS)
9548 : {
9549 34 : const auto vertCRSType = proj_get_type(vertCRS);
9550 34 : isVertical = vertCRSType == PJ_TYPE_VERTICAL_CRS;
9551 34 : if (vertCRSType == PJ_TYPE_BOUND_CRS)
9552 : {
9553 0 : auto base = proj_get_source_crs(d->getPROJContext(), vertCRS);
9554 0 : if (base)
9555 : {
9556 0 : isVertical = proj_get_type(base) == PJ_TYPE_VERTICAL_CRS;
9557 0 : proj_destroy(base);
9558 : }
9559 : }
9560 34 : proj_destroy(vertCRS);
9561 : }
9562 : }
9563 9269 : d->undoDemoteFromBoundCRS();
9564 18538 : return isVertical;
9565 : }
9566 :
9567 : /************************************************************************/
9568 : /* OSRIsVertical() */
9569 : /************************************************************************/
9570 : /**
9571 : * \brief Check if vertical coordinate system.
9572 : *
9573 : * This function is the same as OGRSpatialReference::IsVertical().
9574 : *
9575 : */
9576 0 : int OSRIsVertical(OGRSpatialReferenceH hSRS)
9577 :
9578 : {
9579 0 : VALIDATE_POINTER1(hSRS, "OSRIsVertical", 0);
9580 :
9581 0 : return ToPointer(hSRS)->IsVertical();
9582 : }
9583 :
9584 : /************************************************************************/
9585 : /* IsDynamic() */
9586 : /************************************************************************/
9587 :
9588 : /**
9589 : * \brief Check if a CRS is a dynamic CRS.
9590 : *
9591 : * A dynamic CRS relies on a dynamic datum, that is a datum that is not
9592 : * plate-fixed.
9593 : *
9594 : * This method is the same as the C function OSRIsDynamic().
9595 : *
9596 : * @return true if the CRS is dynamic
9597 : *
9598 : * @since OGR 3.4.0
9599 : *
9600 : * @see HasPointMotionOperation()
9601 : */
9602 :
9603 17225 : bool OGRSpatialReference::IsDynamic() const
9604 :
9605 : {
9606 17225 : TAKE_OPTIONAL_LOCK();
9607 17225 : bool isDynamic = false;
9608 17225 : d->refreshProjObj();
9609 17225 : d->demoteFromBoundCRS();
9610 17225 : auto ctxt = d->getPROJContext();
9611 17225 : PJ *horiz = nullptr;
9612 17225 : if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
9613 : {
9614 99 : horiz = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
9615 : }
9616 17126 : else if (d->m_pj_crs)
9617 : {
9618 16932 : horiz = proj_clone(ctxt, d->m_pj_crs);
9619 : }
9620 17225 : if (horiz && proj_get_type(horiz) == PJ_TYPE_BOUND_CRS)
9621 : {
9622 6 : auto baseCRS = proj_get_source_crs(ctxt, horiz);
9623 6 : if (baseCRS)
9624 : {
9625 6 : proj_destroy(horiz);
9626 6 : horiz = baseCRS;
9627 : }
9628 : }
9629 17225 : auto datum = horiz ? proj_crs_get_datum(ctxt, horiz) : nullptr;
9630 17225 : if (datum)
9631 : {
9632 16996 : const auto type = proj_get_type(datum);
9633 16996 : isDynamic = type == PJ_TYPE_DYNAMIC_GEODETIC_REFERENCE_FRAME ||
9634 : type == PJ_TYPE_DYNAMIC_VERTICAL_REFERENCE_FRAME;
9635 16996 : if (!isDynamic)
9636 : {
9637 16996 : const char *auth_name = proj_get_id_auth_name(datum, 0);
9638 16996 : const char *code = proj_get_id_code(datum, 0);
9639 16996 : if (auth_name && code && EQUAL(auth_name, "EPSG") &&
9640 16460 : EQUAL(code, "6326"))
9641 : {
9642 10733 : isDynamic = true;
9643 : }
9644 : }
9645 16996 : proj_destroy(datum);
9646 : }
9647 : #if PROJ_VERSION_MAJOR > 7 || \
9648 : (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
9649 : else
9650 : {
9651 : auto ensemble =
9652 : horiz ? proj_crs_get_datum_ensemble(ctxt, horiz) : nullptr;
9653 : if (ensemble)
9654 : {
9655 : auto member = proj_datum_ensemble_get_member(ctxt, ensemble, 0);
9656 : if (member)
9657 : {
9658 : const auto type = proj_get_type(member);
9659 : isDynamic = type == PJ_TYPE_DYNAMIC_GEODETIC_REFERENCE_FRAME ||
9660 : type == PJ_TYPE_DYNAMIC_VERTICAL_REFERENCE_FRAME;
9661 : proj_destroy(member);
9662 : }
9663 : proj_destroy(ensemble);
9664 : }
9665 : }
9666 : #endif
9667 17225 : proj_destroy(horiz);
9668 17225 : d->undoDemoteFromBoundCRS();
9669 34450 : return isDynamic;
9670 : }
9671 :
9672 : /************************************************************************/
9673 : /* OSRIsDynamic() */
9674 : /************************************************************************/
9675 : /**
9676 : * \brief Check if a CRS is a dynamic CRS.
9677 : *
9678 : * A dynamic CRS relies on a dynamic datum, that is a datum that is not
9679 : * plate-fixed.
9680 : *
9681 : * This function is the same as OGRSpatialReference::IsDynamic().
9682 : *
9683 : * @since OGR 3.4.0
9684 : */
9685 0 : int OSRIsDynamic(OGRSpatialReferenceH hSRS)
9686 :
9687 : {
9688 0 : VALIDATE_POINTER1(hSRS, "OSRIsDynamic", 0);
9689 :
9690 0 : return ToPointer(hSRS)->IsDynamic();
9691 : }
9692 :
9693 : /************************************************************************/
9694 : /* HasPointMotionOperation() */
9695 : /************************************************************************/
9696 :
9697 : /**
9698 : * \brief Check if a CRS has at least an associated point motion operation.
9699 : *
9700 : * Some CRS are not formally declared as dynamic, but may behave as such
9701 : * in practice due to the presence of point motion operation, to perform
9702 : * coordinate epoch changes within the CRS. Typically NAD83(CSRS)v7
9703 : *
9704 : * @return true if the CRS has at least an associated point motion operation.
9705 : *
9706 : * @since OGR 3.8.0 and PROJ 9.4.0
9707 : *
9708 : * @see IsDynamic()
9709 : */
9710 :
9711 5 : bool OGRSpatialReference::HasPointMotionOperation() const
9712 :
9713 : {
9714 : #if PROJ_VERSION_MAJOR > 9 || \
9715 : (PROJ_VERSION_MAJOR == 9 && PROJ_VERSION_MINOR >= 4)
9716 : TAKE_OPTIONAL_LOCK();
9717 : d->refreshProjObj();
9718 : d->demoteFromBoundCRS();
9719 : auto ctxt = d->getPROJContext();
9720 : auto res =
9721 : CPL_TO_BOOL(proj_crs_has_point_motion_operation(ctxt, d->m_pj_crs));
9722 : d->undoDemoteFromBoundCRS();
9723 : return res;
9724 : #else
9725 5 : return false;
9726 : #endif
9727 : }
9728 :
9729 : /************************************************************************/
9730 : /* OSRHasPointMotionOperation() */
9731 : /************************************************************************/
9732 :
9733 : /**
9734 : * \brief Check if a CRS has at least an associated point motion operation.
9735 : *
9736 : * Some CRS are not formally declared as dynamic, but may behave as such
9737 : * in practice due to the presence of point motion operation, to perform
9738 : * coordinate epoch changes within the CRS. Typically NAD83(CSRS)v7
9739 : *
9740 : * This function is the same as OGRSpatialReference::HasPointMotionOperation().
9741 : *
9742 : * @since OGR 3.8.0 and PROJ 9.4.0
9743 : */
9744 0 : int OSRHasPointMotionOperation(OGRSpatialReferenceH hSRS)
9745 :
9746 : {
9747 0 : VALIDATE_POINTER1(hSRS, "OSRHasPointMotionOperation", 0);
9748 :
9749 0 : return ToPointer(hSRS)->HasPointMotionOperation();
9750 : }
9751 :
9752 : /************************************************************************/
9753 : /* CloneGeogCS() */
9754 : /************************************************************************/
9755 :
9756 : /**
9757 : * \brief Make a duplicate of the GEOGCS node of this OGRSpatialReference
9758 : * object.
9759 : *
9760 : * @return a new SRS, which becomes the responsibility of the caller.
9761 : */
9762 4641 : OGRSpatialReference *OGRSpatialReference::CloneGeogCS() const
9763 :
9764 : {
9765 9282 : TAKE_OPTIONAL_LOCK();
9766 4641 : d->refreshProjObj();
9767 4641 : if (d->m_pj_crs)
9768 : {
9769 4641 : if (d->m_pjType == PJ_TYPE_ENGINEERING_CRS)
9770 0 : return nullptr;
9771 :
9772 : auto geodCRS =
9773 4641 : proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
9774 4641 : if (geodCRS)
9775 : {
9776 4641 : OGRSpatialReference *poNewSRS = new OGRSpatialReference();
9777 4641 : if (d->m_pjType == PJ_TYPE_BOUND_CRS)
9778 : {
9779 : PJ *hub_crs =
9780 13 : proj_get_target_crs(d->getPROJContext(), d->m_pj_crs);
9781 13 : PJ *co = proj_crs_get_coordoperation(d->getPROJContext(),
9782 13 : d->m_pj_crs);
9783 13 : auto temp = proj_crs_create_bound_crs(d->getPROJContext(),
9784 : geodCRS, hub_crs, co);
9785 13 : proj_destroy(geodCRS);
9786 13 : geodCRS = temp;
9787 13 : proj_destroy(hub_crs);
9788 13 : proj_destroy(co);
9789 : }
9790 :
9791 : /* --------------------------------------------------------------------
9792 : */
9793 : /* We have to reconstruct the GEOGCS node for geocentric */
9794 : /* coordinate systems. */
9795 : /* --------------------------------------------------------------------
9796 : */
9797 4641 : if (proj_get_type(geodCRS) == PJ_TYPE_GEOCENTRIC_CRS)
9798 : {
9799 0 : auto datum = proj_crs_get_datum(d->getPROJContext(), geodCRS);
9800 : #if PROJ_VERSION_MAJOR > 7 || \
9801 : (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
9802 : if (datum == nullptr)
9803 : {
9804 : datum = proj_crs_get_datum_ensemble(d->getPROJContext(),
9805 : geodCRS);
9806 : }
9807 : #endif
9808 0 : if (datum)
9809 : {
9810 0 : auto cs = proj_create_ellipsoidal_2D_cs(
9811 : d->getPROJContext(), PJ_ELLPS2D_LATITUDE_LONGITUDE,
9812 : nullptr, 0);
9813 0 : auto temp = proj_create_geographic_crs_from_datum(
9814 : d->getPROJContext(), "unnamed", datum, cs);
9815 0 : proj_destroy(datum);
9816 0 : proj_destroy(cs);
9817 0 : proj_destroy(geodCRS);
9818 0 : geodCRS = temp;
9819 : }
9820 : }
9821 :
9822 4641 : poNewSRS->d->setPjCRS(geodCRS);
9823 4641 : if (d->m_axisMappingStrategy == OAMS_TRADITIONAL_GIS_ORDER)
9824 3041 : poNewSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
9825 4641 : return poNewSRS;
9826 : }
9827 : }
9828 0 : return nullptr;
9829 : }
9830 :
9831 : /************************************************************************/
9832 : /* OSRCloneGeogCS() */
9833 : /************************************************************************/
9834 : /**
9835 : * \brief Make a duplicate of the GEOGCS node of this OGRSpatialReference
9836 : * object.
9837 : *
9838 : * This function is the same as OGRSpatialReference::CloneGeogCS().
9839 : */
9840 126 : OGRSpatialReferenceH CPL_STDCALL OSRCloneGeogCS(OGRSpatialReferenceH hSource)
9841 :
9842 : {
9843 126 : VALIDATE_POINTER1(hSource, "OSRCloneGeogCS", nullptr);
9844 :
9845 126 : return ToHandle(ToPointer(hSource)->CloneGeogCS());
9846 : }
9847 :
9848 : /************************************************************************/
9849 : /* IsSameGeogCS() */
9850 : /************************************************************************/
9851 :
9852 : /**
9853 : * \brief Do the GeogCS'es match?
9854 : *
9855 : * This method is the same as the C function OSRIsSameGeogCS().
9856 : *
9857 : * @param poOther the SRS being compared against.
9858 : *
9859 : * @return TRUE if they are the same or FALSE otherwise.
9860 : */
9861 :
9862 8845 : int OGRSpatialReference::IsSameGeogCS(const OGRSpatialReference *poOther) const
9863 :
9864 : {
9865 8845 : return IsSameGeogCS(poOther, nullptr);
9866 : }
9867 :
9868 : /**
9869 : * \brief Do the GeogCS'es match?
9870 : *
9871 : * This method is the same as the C function OSRIsSameGeogCS().
9872 : *
9873 : * @param poOther the SRS being compared against.
9874 : * @param papszOptions options. ignored
9875 : *
9876 : * @return TRUE if they are the same or FALSE otherwise.
9877 : */
9878 :
9879 8845 : int OGRSpatialReference::IsSameGeogCS(const OGRSpatialReference *poOther,
9880 : const char *const *papszOptions) const
9881 :
9882 : {
9883 17690 : TAKE_OPTIONAL_LOCK();
9884 :
9885 8845 : CPL_IGNORE_RET_VAL(papszOptions);
9886 :
9887 8845 : d->refreshProjObj();
9888 8845 : poOther->d->refreshProjObj();
9889 8845 : if (!d->m_pj_crs || !poOther->d->m_pj_crs)
9890 0 : return FALSE;
9891 8845 : if (d->m_pjType == PJ_TYPE_ENGINEERING_CRS ||
9892 8845 : d->m_pjType == PJ_TYPE_VERTICAL_CRS ||
9893 26535 : poOther->d->m_pjType == PJ_TYPE_ENGINEERING_CRS ||
9894 8845 : poOther->d->m_pjType == PJ_TYPE_VERTICAL_CRS)
9895 : {
9896 0 : return FALSE;
9897 : }
9898 :
9899 8845 : auto geodCRS = proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
9900 : auto otherGeodCRS =
9901 8845 : proj_crs_get_geodetic_crs(d->getPROJContext(), poOther->d->m_pj_crs);
9902 8845 : if (!geodCRS || !otherGeodCRS)
9903 : {
9904 0 : proj_destroy(geodCRS);
9905 0 : proj_destroy(otherGeodCRS);
9906 0 : return FALSE;
9907 : }
9908 :
9909 8845 : int ret = proj_is_equivalent_to(
9910 : geodCRS, otherGeodCRS, PJ_COMP_EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS);
9911 :
9912 8845 : proj_destroy(geodCRS);
9913 8845 : proj_destroy(otherGeodCRS);
9914 8845 : return ret;
9915 : }
9916 :
9917 : /************************************************************************/
9918 : /* OSRIsSameGeogCS() */
9919 : /************************************************************************/
9920 :
9921 : /**
9922 : * \brief Do the GeogCS'es match?
9923 : *
9924 : * This function is the same as OGRSpatialReference::IsSameGeogCS().
9925 : */
9926 0 : int OSRIsSameGeogCS(OGRSpatialReferenceH hSRS1, OGRSpatialReferenceH hSRS2)
9927 :
9928 : {
9929 0 : VALIDATE_POINTER1(hSRS1, "OSRIsSameGeogCS", 0);
9930 0 : VALIDATE_POINTER1(hSRS2, "OSRIsSameGeogCS", 0);
9931 :
9932 0 : return ToPointer(hSRS1)->IsSameGeogCS(ToPointer(hSRS2));
9933 : }
9934 :
9935 : /************************************************************************/
9936 : /* IsSameVertCS() */
9937 : /************************************************************************/
9938 :
9939 : /**
9940 : * \brief Do the VertCS'es match?
9941 : *
9942 : * This method is the same as the C function OSRIsSameVertCS().
9943 : *
9944 : * @param poOther the SRS being compared against.
9945 : *
9946 : * @return TRUE if they are the same or FALSE otherwise.
9947 : */
9948 :
9949 0 : int OGRSpatialReference::IsSameVertCS(const OGRSpatialReference *poOther) const
9950 :
9951 : {
9952 0 : TAKE_OPTIONAL_LOCK();
9953 :
9954 : /* -------------------------------------------------------------------- */
9955 : /* Does the datum name match? */
9956 : /* -------------------------------------------------------------------- */
9957 0 : const char *pszThisValue = this->GetAttrValue("VERT_DATUM");
9958 0 : const char *pszOtherValue = poOther->GetAttrValue("VERT_DATUM");
9959 :
9960 0 : if (pszThisValue == nullptr || pszOtherValue == nullptr ||
9961 0 : !EQUAL(pszThisValue, pszOtherValue))
9962 0 : return FALSE;
9963 :
9964 : /* -------------------------------------------------------------------- */
9965 : /* Do the units match? */
9966 : /* -------------------------------------------------------------------- */
9967 0 : pszThisValue = this->GetAttrValue("VERT_CS|UNIT", 1);
9968 0 : if (pszThisValue == nullptr)
9969 0 : pszThisValue = "1.0";
9970 :
9971 0 : pszOtherValue = poOther->GetAttrValue("VERT_CS|UNIT", 1);
9972 0 : if (pszOtherValue == nullptr)
9973 0 : pszOtherValue = "1.0";
9974 :
9975 0 : if (std::abs(CPLAtof(pszOtherValue) - CPLAtof(pszThisValue)) > 0.00000001)
9976 0 : return FALSE;
9977 :
9978 0 : return TRUE;
9979 : }
9980 :
9981 : /************************************************************************/
9982 : /* OSRIsSameVertCS() */
9983 : /************************************************************************/
9984 :
9985 : /**
9986 : * \brief Do the VertCS'es match?
9987 : *
9988 : * This function is the same as OGRSpatialReference::IsSameVertCS().
9989 : */
9990 0 : int OSRIsSameVertCS(OGRSpatialReferenceH hSRS1, OGRSpatialReferenceH hSRS2)
9991 :
9992 : {
9993 0 : VALIDATE_POINTER1(hSRS1, "OSRIsSameVertCS", 0);
9994 0 : VALIDATE_POINTER1(hSRS2, "OSRIsSameVertCS", 0);
9995 :
9996 0 : return ToPointer(hSRS1)->IsSameVertCS(ToPointer(hSRS2));
9997 : }
9998 :
9999 : /************************************************************************/
10000 : /* IsSame() */
10001 : /************************************************************************/
10002 :
10003 : /**
10004 : * \brief Do these two spatial references describe the same system ?
10005 : *
10006 : * @param poOtherSRS the SRS being compared to.
10007 : *
10008 : * @return TRUE if equivalent or FALSE otherwise.
10009 : */
10010 :
10011 24858 : int OGRSpatialReference::IsSame(const OGRSpatialReference *poOtherSRS) const
10012 :
10013 : {
10014 24858 : return IsSame(poOtherSRS, nullptr);
10015 : }
10016 :
10017 : /**
10018 : * \brief Do these two spatial references describe the same system ?
10019 : *
10020 : * This also takes into account the data axis to CRS axis mapping by default
10021 : *
10022 : * @param poOtherSRS the SRS being compared to.
10023 : * @param papszOptions options. NULL or NULL terminated list of options.
10024 : * Currently supported options are:
10025 : * <ul>
10026 : * <li>IGNORE_DATA_AXIS_TO_SRS_AXIS_MAPPING=YES/NO. Defaults to NO</li>
10027 : * <li>CRITERION=STRICT/EQUIVALENT/EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS.
10028 : * Defaults to EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS.</li>
10029 : * <li>IGNORE_COORDINATE_EPOCH=YES/NO. Defaults to NO</li>
10030 : * </ul>
10031 : *
10032 : * @return TRUE if equivalent or FALSE otherwise.
10033 : */
10034 :
10035 38766 : int OGRSpatialReference::IsSame(const OGRSpatialReference *poOtherSRS,
10036 : const char *const *papszOptions) const
10037 :
10038 : {
10039 77532 : TAKE_OPTIONAL_LOCK();
10040 :
10041 38766 : d->refreshProjObj();
10042 38766 : poOtherSRS->d->refreshProjObj();
10043 38766 : if (!d->m_pj_crs || !poOtherSRS->d->m_pj_crs)
10044 51 : return d->m_pj_crs == poOtherSRS->d->m_pj_crs;
10045 38715 : if (!CPLTestBool(CSLFetchNameValueDef(
10046 : papszOptions, "IGNORE_DATA_AXIS_TO_SRS_AXIS_MAPPING", "NO")))
10047 : {
10048 37958 : if (d->m_axisMapping != poOtherSRS->d->m_axisMapping)
10049 3722 : return false;
10050 : }
10051 :
10052 34993 : if (!CPLTestBool(CSLFetchNameValueDef(papszOptions,
10053 : "IGNORE_COORDINATE_EPOCH", "NO")))
10054 : {
10055 34502 : if (d->m_coordinateEpoch != poOtherSRS->d->m_coordinateEpoch)
10056 27 : return false;
10057 : }
10058 :
10059 34966 : bool reboundSelf = false;
10060 34966 : bool reboundOther = false;
10061 35018 : if (d->m_pjType == PJ_TYPE_BOUND_CRS &&
10062 52 : poOtherSRS->d->m_pjType != PJ_TYPE_BOUND_CRS)
10063 : {
10064 14 : d->demoteFromBoundCRS();
10065 14 : reboundSelf = true;
10066 : }
10067 69866 : else if (d->m_pjType != PJ_TYPE_BOUND_CRS &&
10068 34914 : poOtherSRS->d->m_pjType == PJ_TYPE_BOUND_CRS)
10069 : {
10070 28 : poOtherSRS->d->demoteFromBoundCRS();
10071 28 : reboundOther = true;
10072 : }
10073 :
10074 34966 : PJ_COMPARISON_CRITERION criterion =
10075 : PJ_COMP_EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS;
10076 34966 : const char *pszCriterion = CSLFetchNameValueDef(
10077 : papszOptions, "CRITERION", "EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS");
10078 34966 : if (EQUAL(pszCriterion, "STRICT"))
10079 0 : criterion = PJ_COMP_STRICT;
10080 34966 : else if (EQUAL(pszCriterion, "EQUIVALENT"))
10081 10356 : criterion = PJ_COMP_EQUIVALENT;
10082 24610 : else if (!EQUAL(pszCriterion, "EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS"))
10083 : {
10084 0 : CPLError(CE_Warning, CPLE_NotSupported,
10085 : "Unsupported value for CRITERION: %s", pszCriterion);
10086 : }
10087 : int ret =
10088 34966 : proj_is_equivalent_to(d->m_pj_crs, poOtherSRS->d->m_pj_crs, criterion);
10089 34966 : if (reboundSelf)
10090 14 : d->undoDemoteFromBoundCRS();
10091 34966 : if (reboundOther)
10092 28 : poOtherSRS->d->undoDemoteFromBoundCRS();
10093 :
10094 34966 : return ret;
10095 : }
10096 :
10097 : /************************************************************************/
10098 : /* OSRIsSame() */
10099 : /************************************************************************/
10100 :
10101 : /**
10102 : * \brief Do these two spatial references describe the same system ?
10103 : *
10104 : * This function is the same as OGRSpatialReference::IsSame().
10105 : */
10106 41 : int OSRIsSame(OGRSpatialReferenceH hSRS1, OGRSpatialReferenceH hSRS2)
10107 :
10108 : {
10109 41 : VALIDATE_POINTER1(hSRS1, "OSRIsSame", 0);
10110 41 : VALIDATE_POINTER1(hSRS2, "OSRIsSame", 0);
10111 :
10112 41 : return ToPointer(hSRS1)->IsSame(ToPointer(hSRS2));
10113 : }
10114 :
10115 : /************************************************************************/
10116 : /* OSRIsSameEx() */
10117 : /************************************************************************/
10118 :
10119 : /**
10120 : * \brief Do these two spatial references describe the same system ?
10121 : *
10122 : * This function is the same as OGRSpatialReference::IsSame().
10123 : */
10124 672 : int OSRIsSameEx(OGRSpatialReferenceH hSRS1, OGRSpatialReferenceH hSRS2,
10125 : const char *const *papszOptions)
10126 : {
10127 672 : VALIDATE_POINTER1(hSRS1, "OSRIsSame", 0);
10128 672 : VALIDATE_POINTER1(hSRS2, "OSRIsSame", 0);
10129 :
10130 672 : return ToPointer(hSRS1)->IsSame(ToPointer(hSRS2), papszOptions);
10131 : }
10132 :
10133 : /************************************************************************/
10134 : /* convertToOtherProjection() */
10135 : /************************************************************************/
10136 :
10137 : /**
10138 : * \brief Convert to another equivalent projection
10139 : *
10140 : * Currently implemented:
10141 : * <ul>
10142 : * <li>SRS_PT_MERCATOR_1SP to SRS_PT_MERCATOR_2SP</li>
10143 : * <li>SRS_PT_MERCATOR_2SP to SRS_PT_MERCATOR_1SP</li>
10144 : * <li>SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP to
10145 : * SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP</li>
10146 : * <li>SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP to
10147 : * SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP</li>
10148 : * </ul>
10149 : *
10150 : * @param pszTargetProjection target projection.
10151 : * @param papszOptions lists of options. None supported currently.
10152 : * @return a new SRS, or NULL in case of error.
10153 : *
10154 : */
10155 91 : OGRSpatialReference *OGRSpatialReference::convertToOtherProjection(
10156 : const char *pszTargetProjection,
10157 : CPL_UNUSED const char *const *papszOptions) const
10158 : {
10159 182 : TAKE_OPTIONAL_LOCK();
10160 :
10161 91 : if (pszTargetProjection == nullptr)
10162 1 : return nullptr;
10163 : int new_code;
10164 90 : if (EQUAL(pszTargetProjection, SRS_PT_MERCATOR_1SP))
10165 : {
10166 6 : new_code = EPSG_CODE_METHOD_MERCATOR_VARIANT_A;
10167 : }
10168 84 : else if (EQUAL(pszTargetProjection, SRS_PT_MERCATOR_2SP))
10169 : {
10170 7 : new_code = EPSG_CODE_METHOD_MERCATOR_VARIANT_B;
10171 : }
10172 77 : else if (EQUAL(pszTargetProjection, SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP))
10173 : {
10174 65 : new_code = EPSG_CODE_METHOD_LAMBERT_CONIC_CONFORMAL_1SP;
10175 : }
10176 12 : else if (EQUAL(pszTargetProjection, SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP))
10177 : {
10178 11 : new_code = EPSG_CODE_METHOD_LAMBERT_CONIC_CONFORMAL_2SP;
10179 : }
10180 : else
10181 : {
10182 1 : return nullptr;
10183 : }
10184 :
10185 89 : d->refreshProjObj();
10186 89 : d->demoteFromBoundCRS();
10187 89 : OGRSpatialReference *poNewSRS = nullptr;
10188 89 : if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
10189 : {
10190 : auto conv =
10191 88 : proj_crs_get_coordoperation(d->getPROJContext(), d->m_pj_crs);
10192 88 : auto new_conv = proj_convert_conversion_to_other_method(
10193 : d->getPROJContext(), conv, new_code, nullptr);
10194 88 : proj_destroy(conv);
10195 88 : if (new_conv)
10196 : {
10197 : auto geodCRS =
10198 74 : proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
10199 74 : auto cs = proj_crs_get_coordinate_system(d->getPROJContext(),
10200 74 : d->m_pj_crs);
10201 74 : if (geodCRS && cs)
10202 : {
10203 74 : auto new_proj_crs = proj_create_projected_crs(
10204 74 : d->getPROJContext(), proj_get_name(d->m_pj_crs), geodCRS,
10205 : new_conv, cs);
10206 74 : proj_destroy(new_conv);
10207 74 : if (new_proj_crs)
10208 : {
10209 74 : poNewSRS = new OGRSpatialReference();
10210 :
10211 74 : if (d->m_pj_bound_crs_target && d->m_pj_bound_crs_co)
10212 : {
10213 9 : auto boundCRS = proj_crs_create_bound_crs(
10214 : d->getPROJContext(), new_proj_crs,
10215 9 : d->m_pj_bound_crs_target, d->m_pj_bound_crs_co);
10216 9 : if (boundCRS)
10217 : {
10218 9 : proj_destroy(new_proj_crs);
10219 9 : new_proj_crs = boundCRS;
10220 : }
10221 : }
10222 :
10223 74 : poNewSRS->d->setPjCRS(new_proj_crs);
10224 : }
10225 : }
10226 74 : proj_destroy(geodCRS);
10227 74 : proj_destroy(cs);
10228 : }
10229 : }
10230 89 : d->undoDemoteFromBoundCRS();
10231 89 : return poNewSRS;
10232 : }
10233 :
10234 : /************************************************************************/
10235 : /* OSRConvertToOtherProjection() */
10236 : /************************************************************************/
10237 :
10238 : /**
10239 : * \brief Convert to another equivalent projection
10240 : *
10241 : * Currently implemented:
10242 : * <ul>
10243 : * <li>SRS_PT_MERCATOR_1SP to SRS_PT_MERCATOR_2SP</li>
10244 : * <li>SRS_PT_MERCATOR_2SP to SRS_PT_MERCATOR_1SP</li>
10245 : * <li>SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP to
10246 : * SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP</li>
10247 : * <li>SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP to
10248 : * SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP</li>
10249 : * </ul>
10250 : *
10251 : * @param hSRS source SRS
10252 : * @param pszTargetProjection target projection.
10253 : * @param papszOptions lists of options. None supported currently.
10254 : * @return a new SRS, or NULL in case of error.
10255 : *
10256 : */
10257 : OGRSpatialReferenceH
10258 28 : OSRConvertToOtherProjection(OGRSpatialReferenceH hSRS,
10259 : const char *pszTargetProjection,
10260 : const char *const *papszOptions)
10261 : {
10262 28 : VALIDATE_POINTER1(hSRS, "OSRConvertToOtherProjection", nullptr);
10263 28 : return ToHandle(ToPointer(hSRS)->convertToOtherProjection(
10264 28 : pszTargetProjection, papszOptions));
10265 : }
10266 :
10267 : /************************************************************************/
10268 : /* OSRFindMatches() */
10269 : /************************************************************************/
10270 :
10271 : /**
10272 : * \brief Try to identify a match between the passed SRS and a related SRS
10273 : * in a catalog.
10274 : *
10275 : * Matching may be partial, or may fail.
10276 : * Returned entries will be sorted by decreasing match confidence (first
10277 : * entry has the highest match confidence).
10278 : *
10279 : * The exact way matching is done may change in future versions. Starting with
10280 : * GDAL 3.0, it relies on PROJ' proj_identify() function.
10281 : *
10282 : * This function is the same as OGRSpatialReference::FindMatches().
10283 : *
10284 : * @param hSRS SRS to match
10285 : * @param papszOptions NULL terminated list of options or NULL
10286 : * @param pnEntries Output parameter. Number of values in the returned array.
10287 : * @param ppanMatchConfidence Output parameter (or NULL). *ppanMatchConfidence
10288 : * will be allocated to an array of *pnEntries whose values between 0 and 100
10289 : * indicate the confidence in the match. 100 is the highest confidence level.
10290 : * The array must be freed with CPLFree().
10291 : *
10292 : * @return an array of SRS that match the passed SRS, or NULL. Must be freed
10293 : * with OSRFreeSRSArray()
10294 : *
10295 : */
10296 8 : OGRSpatialReferenceH *OSRFindMatches(OGRSpatialReferenceH hSRS,
10297 : CSLConstList papszOptions, int *pnEntries,
10298 : int **ppanMatchConfidence)
10299 : {
10300 8 : if (pnEntries)
10301 8 : *pnEntries = 0;
10302 8 : if (ppanMatchConfidence)
10303 8 : *ppanMatchConfidence = nullptr;
10304 8 : VALIDATE_POINTER1(hSRS, "OSRFindMatches", nullptr);
10305 :
10306 8 : OGRSpatialReference *poSRS = ToPointer(hSRS);
10307 8 : return poSRS->FindMatches(papszOptions, pnEntries, ppanMatchConfidence);
10308 : }
10309 :
10310 : /************************************************************************/
10311 : /* OSRFreeSRSArray() */
10312 : /************************************************************************/
10313 :
10314 : /**
10315 : * \brief Free return of OSRIdentifyMatches()
10316 : *
10317 : * @param pahSRS array of SRS (must be NULL terminated)
10318 : */
10319 197 : void OSRFreeSRSArray(OGRSpatialReferenceH *pahSRS)
10320 : {
10321 197 : if (pahSRS != nullptr)
10322 : {
10323 1743 : for (int i = 0; pahSRS[i] != nullptr; ++i)
10324 : {
10325 1564 : OSRRelease(pahSRS[i]);
10326 : }
10327 179 : CPLFree(pahSRS);
10328 : }
10329 197 : }
10330 :
10331 : /************************************************************************/
10332 : /* FindBestMatch() */
10333 : /************************************************************************/
10334 :
10335 : /**
10336 : * \brief Try to identify the best match between the passed SRS and a related
10337 : * SRS in a catalog.
10338 : *
10339 : * This is a wrapper over OGRSpatialReference::FindMatches() that takes care
10340 : * of filtering its output.
10341 : * Only matches whose confidence is greater or equal to nMinimumMatchConfidence
10342 : * will be considered. If there is a single match, it is returned.
10343 : * If there are several matches, only return the one under the
10344 : * pszPreferredAuthority, if there is a single one under that authority.
10345 : *
10346 : * @param nMinimumMatchConfidence Minimum match confidence (value between 0 and
10347 : * 100). If set to 0, 90 is used.
10348 : * @param pszPreferredAuthority Preferred CRS authority. If set to nullptr,
10349 : * "EPSG" is used.
10350 : * @param papszOptions NULL terminated list of options or NULL. No option is
10351 : * defined at time of writing.
10352 : *
10353 : * @return a new OGRSpatialReference* object to free with Release(), or nullptr
10354 : *
10355 : * @since GDAL 3.6
10356 : * @see OGRSpatialReference::FindMatches()
10357 : */
10358 : OGRSpatialReference *
10359 1551 : OGRSpatialReference::FindBestMatch(int nMinimumMatchConfidence,
10360 : const char *pszPreferredAuthority,
10361 : CSLConstList papszOptions) const
10362 : {
10363 3102 : TAKE_OPTIONAL_LOCK();
10364 :
10365 1551 : CPL_IGNORE_RET_VAL(papszOptions); // ignored for now.
10366 :
10367 1551 : if (nMinimumMatchConfidence == 0)
10368 0 : nMinimumMatchConfidence = 90;
10369 1551 : if (pszPreferredAuthority == nullptr)
10370 202 : pszPreferredAuthority = "EPSG";
10371 :
10372 : // Try to identify the CRS with the database
10373 1551 : int nEntries = 0;
10374 1551 : int *panConfidence = nullptr;
10375 : OGRSpatialReferenceH *pahSRS =
10376 1551 : FindMatches(nullptr, &nEntries, &panConfidence);
10377 1551 : if (nEntries == 1 && panConfidence[0] >= nMinimumMatchConfidence)
10378 : {
10379 2774 : std::vector<double> adfTOWGS84(7);
10380 1387 : if (GetTOWGS84(&adfTOWGS84[0], 7) != OGRERR_NONE)
10381 : {
10382 1386 : adfTOWGS84.clear();
10383 : }
10384 :
10385 1387 : auto poSRS = OGRSpatialReference::FromHandle(pahSRS[0]);
10386 :
10387 : auto poBaseGeogCRS =
10388 1387 : std::unique_ptr<OGRSpatialReference>(poSRS->CloneGeogCS());
10389 1387 : if (poBaseGeogCRS)
10390 : {
10391 : // If the base geographic SRS of the SRS is EPSG:4326
10392 : // with TOWGS84[0,0,0,0,0,0], then just use the official
10393 : // SRS code
10394 : // Same with EPSG:4258 (ETRS89), since it's the only known
10395 : // TOWGS84[] style transformation to WGS 84, and given the
10396 : // "fuzzy" nature of both ETRS89 and WGS 84, there's little
10397 : // chance that a non-NULL TOWGS84[] will emerge.
10398 1387 : const char *pszAuthorityName = nullptr;
10399 1387 : const char *pszAuthorityCode = nullptr;
10400 1387 : const char *pszBaseAuthorityName = nullptr;
10401 1387 : const char *pszBaseAuthorityCode = nullptr;
10402 1387 : const char *pszBaseName = poBaseGeogCRS->GetName();
10403 2774 : if (adfTOWGS84 == std::vector<double>(7) &&
10404 1 : (pszAuthorityName = poSRS->GetAuthorityName()) != nullptr &&
10405 1 : EQUAL(pszAuthorityName, "EPSG") &&
10406 1 : (pszAuthorityCode = poSRS->GetAuthorityCode()) != nullptr &&
10407 1 : (pszBaseAuthorityName = poBaseGeogCRS->GetAuthorityName()) !=
10408 1 : nullptr &&
10409 1 : EQUAL(pszBaseAuthorityName, "EPSG") &&
10410 1 : (pszBaseAuthorityCode = poBaseGeogCRS->GetAuthorityCode()) !=
10411 2775 : nullptr &&
10412 1 : (EQUAL(pszBaseAuthorityCode, "4326") ||
10413 1 : EQUAL(pszBaseAuthorityCode, "4258") ||
10414 : // For ETRS89-XXX [...] new CRS added in EPSG 12.033+
10415 0 : (pszBaseName && STARTS_WITH(pszBaseName, "ETRS89"))))
10416 : {
10417 1 : poSRS->importFromEPSG(atoi(pszAuthorityCode));
10418 : }
10419 : }
10420 :
10421 1387 : CPLFree(pahSRS);
10422 1387 : CPLFree(panConfidence);
10423 :
10424 1387 : return poSRS;
10425 : }
10426 : else
10427 : {
10428 : // If there are several matches >= nMinimumMatchConfidence, take the
10429 : // only one that is under pszPreferredAuthority
10430 164 : int iBestEntry = -1;
10431 1678 : for (int i = 0; i < nEntries; i++)
10432 : {
10433 1514 : if (panConfidence[i] >= nMinimumMatchConfidence)
10434 : {
10435 : const char *pszAuthName =
10436 3 : OGRSpatialReference::FromHandle(pahSRS[i])
10437 3 : ->GetAuthorityName();
10438 3 : if (pszAuthName != nullptr &&
10439 3 : EQUAL(pszAuthName, pszPreferredAuthority))
10440 : {
10441 3 : if (iBestEntry < 0)
10442 3 : iBestEntry = i;
10443 : else
10444 : {
10445 0 : iBestEntry = -1;
10446 0 : break;
10447 : }
10448 : }
10449 : }
10450 : }
10451 164 : if (iBestEntry >= 0)
10452 : {
10453 3 : auto poRet = OGRSpatialReference::FromHandle(pahSRS[0])->Clone();
10454 3 : OSRFreeSRSArray(pahSRS);
10455 3 : CPLFree(panConfidence);
10456 3 : return poRet;
10457 : }
10458 : }
10459 161 : OSRFreeSRSArray(pahSRS);
10460 161 : CPLFree(panConfidence);
10461 161 : return nullptr;
10462 : }
10463 :
10464 : /************************************************************************/
10465 : /* SetTOWGS84() */
10466 : /************************************************************************/
10467 :
10468 : /**
10469 : * \brief Set the Bursa-Wolf conversion to WGS84.
10470 : *
10471 : * This will create the TOWGS84 node as a child of the DATUM. It will fail
10472 : * if there is no existing DATUM node. It will replace
10473 : * an existing TOWGS84 node if there is one.
10474 : *
10475 : * The parameters have the same meaning as EPSG transformation 9606
10476 : * (Position Vector 7-param. transformation).
10477 : *
10478 : * This method is the same as the C function OSRSetTOWGS84().
10479 : *
10480 : * @param dfDX X child in meters.
10481 : * @param dfDY Y child in meters.
10482 : * @param dfDZ Z child in meters.
10483 : * @param dfEX X rotation in arc seconds (optional, defaults to zero).
10484 : * @param dfEY Y rotation in arc seconds (optional, defaults to zero).
10485 : * @param dfEZ Z rotation in arc seconds (optional, defaults to zero).
10486 : * @param dfPPM scaling factor (parts per million).
10487 : *
10488 : * @return OGRERR_NONE on success.
10489 : */
10490 :
10491 103 : OGRErr OGRSpatialReference::SetTOWGS84(double dfDX, double dfDY, double dfDZ,
10492 : double dfEX, double dfEY, double dfEZ,
10493 : double dfPPM)
10494 :
10495 : {
10496 206 : TAKE_OPTIONAL_LOCK();
10497 :
10498 103 : d->refreshProjObj();
10499 103 : if (d->m_pj_crs == nullptr)
10500 : {
10501 0 : return OGRERR_FAILURE;
10502 : }
10503 :
10504 : // Remove existing BoundCRS
10505 103 : if (d->m_pjType == PJ_TYPE_BOUND_CRS)
10506 : {
10507 0 : auto baseCRS = proj_get_source_crs(d->getPROJContext(), d->m_pj_crs);
10508 0 : if (!baseCRS)
10509 0 : return OGRERR_FAILURE;
10510 0 : d->setPjCRS(baseCRS);
10511 : }
10512 :
10513 : PJ_PARAM_DESCRIPTION params[7];
10514 :
10515 103 : params[0].name = EPSG_NAME_PARAMETER_X_AXIS_TRANSLATION;
10516 103 : params[0].auth_name = "EPSG";
10517 103 : params[0].code = XSTRINGIFY(EPSG_CODE_PARAMETER_X_AXIS_TRANSLATION);
10518 103 : params[0].value = dfDX;
10519 103 : params[0].unit_name = "metre";
10520 103 : params[0].unit_conv_factor = 1.0;
10521 103 : params[0].unit_type = PJ_UT_LINEAR;
10522 :
10523 103 : params[1].name = EPSG_NAME_PARAMETER_Y_AXIS_TRANSLATION;
10524 103 : params[1].auth_name = "EPSG";
10525 103 : params[1].code = XSTRINGIFY(EPSG_CODE_PARAMETER_Y_AXIS_TRANSLATION);
10526 103 : params[1].value = dfDY;
10527 103 : params[1].unit_name = "metre";
10528 103 : params[1].unit_conv_factor = 1.0;
10529 103 : params[1].unit_type = PJ_UT_LINEAR;
10530 :
10531 103 : params[2].name = EPSG_NAME_PARAMETER_Z_AXIS_TRANSLATION;
10532 103 : params[2].auth_name = "EPSG";
10533 103 : params[2].code = XSTRINGIFY(EPSG_CODE_PARAMETER_Z_AXIS_TRANSLATION);
10534 103 : params[2].value = dfDZ;
10535 103 : params[2].unit_name = "metre";
10536 103 : params[2].unit_conv_factor = 1.0;
10537 103 : params[2].unit_type = PJ_UT_LINEAR;
10538 :
10539 103 : params[3].name = EPSG_NAME_PARAMETER_X_AXIS_ROTATION;
10540 103 : params[3].auth_name = "EPSG";
10541 103 : params[3].code = XSTRINGIFY(EPSG_CODE_PARAMETER_X_AXIS_ROTATION);
10542 103 : params[3].value = dfEX;
10543 103 : params[3].unit_name = "arc-second";
10544 103 : params[3].unit_conv_factor = 1. / 3600 * M_PI / 180;
10545 103 : params[3].unit_type = PJ_UT_ANGULAR;
10546 :
10547 103 : params[4].name = EPSG_NAME_PARAMETER_Y_AXIS_ROTATION;
10548 103 : params[4].auth_name = "EPSG";
10549 103 : params[4].code = XSTRINGIFY(EPSG_CODE_PARAMETER_Y_AXIS_ROTATION);
10550 103 : params[4].value = dfEY;
10551 103 : params[4].unit_name = "arc-second";
10552 103 : params[4].unit_conv_factor = 1. / 3600 * M_PI / 180;
10553 103 : params[4].unit_type = PJ_UT_ANGULAR;
10554 :
10555 103 : params[5].name = EPSG_NAME_PARAMETER_Z_AXIS_ROTATION;
10556 103 : params[5].auth_name = "EPSG";
10557 103 : params[5].code = XSTRINGIFY(EPSG_CODE_PARAMETER_Z_AXIS_ROTATION);
10558 103 : params[5].value = dfEZ;
10559 103 : params[5].unit_name = "arc-second";
10560 103 : params[5].unit_conv_factor = 1. / 3600 * M_PI / 180;
10561 103 : params[5].unit_type = PJ_UT_ANGULAR;
10562 :
10563 103 : params[6].name = EPSG_NAME_PARAMETER_SCALE_DIFFERENCE;
10564 103 : params[6].auth_name = "EPSG";
10565 103 : params[6].code = XSTRINGIFY(EPSG_CODE_PARAMETER_SCALE_DIFFERENCE);
10566 103 : params[6].value = dfPPM;
10567 103 : params[6].unit_name = "parts per million";
10568 103 : params[6].unit_conv_factor = 1e-6;
10569 103 : params[6].unit_type = PJ_UT_SCALE;
10570 :
10571 : auto sourceCRS =
10572 103 : proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
10573 103 : if (!sourceCRS)
10574 : {
10575 0 : return OGRERR_FAILURE;
10576 : }
10577 :
10578 103 : const auto sourceType = proj_get_type(sourceCRS);
10579 :
10580 103 : auto targetCRS = proj_create_from_database(
10581 : d->getPROJContext(), "EPSG",
10582 : sourceType == PJ_TYPE_GEOGRAPHIC_2D_CRS ? "4326"
10583 : : sourceType == PJ_TYPE_GEOGRAPHIC_3D_CRS ? "4979"
10584 : : "4978",
10585 : PJ_CATEGORY_CRS, false, nullptr);
10586 103 : if (!targetCRS)
10587 : {
10588 0 : proj_destroy(sourceCRS);
10589 0 : return OGRERR_FAILURE;
10590 : }
10591 :
10592 206 : CPLString osMethodCode;
10593 : osMethodCode.Printf("%d",
10594 : sourceType == PJ_TYPE_GEOGRAPHIC_2D_CRS
10595 : ? EPSG_CODE_METHOD_POSITION_VECTOR_GEOGRAPHIC_2D
10596 : : sourceType == PJ_TYPE_GEOGRAPHIC_3D_CRS
10597 0 : ? EPSG_CODE_METHOD_POSITION_VECTOR_GEOGRAPHIC_3D
10598 103 : : EPSG_CODE_METHOD_POSITION_VECTOR_GEOCENTRIC);
10599 :
10600 103 : auto transf = proj_create_transformation(
10601 : d->getPROJContext(), "Transformation to WGS84", nullptr, nullptr,
10602 : sourceCRS, targetCRS, nullptr,
10603 : sourceType == PJ_TYPE_GEOGRAPHIC_2D_CRS
10604 : ? EPSG_NAME_METHOD_POSITION_VECTOR_GEOGRAPHIC_2D
10605 : : sourceType == PJ_TYPE_GEOGRAPHIC_3D_CRS
10606 0 : ? EPSG_NAME_METHOD_POSITION_VECTOR_GEOGRAPHIC_3D
10607 : : EPSG_NAME_METHOD_POSITION_VECTOR_GEOCENTRIC,
10608 : "EPSG", osMethodCode.c_str(), 7, params, -1);
10609 103 : proj_destroy(sourceCRS);
10610 103 : if (!transf)
10611 : {
10612 0 : proj_destroy(targetCRS);
10613 0 : return OGRERR_FAILURE;
10614 : }
10615 :
10616 103 : auto newBoundCRS = proj_crs_create_bound_crs(
10617 103 : d->getPROJContext(), d->m_pj_crs, targetCRS, transf);
10618 103 : proj_destroy(transf);
10619 103 : proj_destroy(targetCRS);
10620 103 : if (!newBoundCRS)
10621 : {
10622 0 : return OGRERR_FAILURE;
10623 : }
10624 :
10625 103 : d->setPjCRS(newBoundCRS);
10626 103 : return OGRERR_NONE;
10627 : }
10628 :
10629 : /************************************************************************/
10630 : /* OSRSetTOWGS84() */
10631 : /************************************************************************/
10632 :
10633 : /**
10634 : * \brief Set the Bursa-Wolf conversion to WGS84.
10635 : *
10636 : * This function is the same as OGRSpatialReference::SetTOWGS84().
10637 : */
10638 4 : OGRErr OSRSetTOWGS84(OGRSpatialReferenceH hSRS, double dfDX, double dfDY,
10639 : double dfDZ, double dfEX, double dfEY, double dfEZ,
10640 : double dfPPM)
10641 :
10642 : {
10643 4 : VALIDATE_POINTER1(hSRS, "OSRSetTOWGS84", OGRERR_FAILURE);
10644 :
10645 4 : return ToPointer(hSRS)->SetTOWGS84(dfDX, dfDY, dfDZ, dfEX, dfEY, dfEZ,
10646 4 : dfPPM);
10647 : }
10648 :
10649 : /************************************************************************/
10650 : /* GetTOWGS84() */
10651 : /************************************************************************/
10652 :
10653 : /**
10654 : * \brief Fetch TOWGS84 parameters, if available.
10655 : *
10656 : * The parameters have the same meaning as EPSG transformation 9606
10657 : * (Position Vector 7-param. transformation).
10658 : *
10659 : * @param padfCoeff array into which up to 7 coefficients are placed.
10660 : * @param nCoeffCount size of padfCoeff - defaults to 7.
10661 : *
10662 : * @return OGRERR_NONE on success, or OGRERR_FAILURE if there is no
10663 : * TOWGS84 node available.
10664 : */
10665 :
10666 5018 : OGRErr OGRSpatialReference::GetTOWGS84(double *padfCoeff, int nCoeffCount) const
10667 :
10668 : {
10669 10036 : TAKE_OPTIONAL_LOCK();
10670 :
10671 5018 : d->refreshProjObj();
10672 5018 : if (d->m_pjType != PJ_TYPE_BOUND_CRS)
10673 4970 : return OGRERR_FAILURE;
10674 :
10675 48 : memset(padfCoeff, 0, sizeof(double) * nCoeffCount);
10676 :
10677 48 : auto transf = proj_crs_get_coordoperation(d->getPROJContext(), d->m_pj_crs);
10678 48 : int success = proj_coordoperation_get_towgs84_values(
10679 : d->getPROJContext(), transf, padfCoeff, nCoeffCount, false);
10680 48 : proj_destroy(transf);
10681 :
10682 48 : return success ? OGRERR_NONE : OGRERR_FAILURE;
10683 : }
10684 :
10685 : /************************************************************************/
10686 : /* OSRGetTOWGS84() */
10687 : /************************************************************************/
10688 :
10689 : /**
10690 : * \brief Fetch TOWGS84 parameters, if available.
10691 : *
10692 : * This function is the same as OGRSpatialReference::GetTOWGS84().
10693 : */
10694 10 : OGRErr OSRGetTOWGS84(OGRSpatialReferenceH hSRS, double *padfCoeff,
10695 : int nCoeffCount)
10696 :
10697 : {
10698 10 : VALIDATE_POINTER1(hSRS, "OSRGetTOWGS84", OGRERR_FAILURE);
10699 :
10700 10 : return ToPointer(hSRS)->GetTOWGS84(padfCoeff, nCoeffCount);
10701 : }
10702 :
10703 : /************************************************************************/
10704 : /* IsAngularParameter() */
10705 : /************************************************************************/
10706 :
10707 : /** Is the passed projection parameter an angular one?
10708 : *
10709 : * @return TRUE or FALSE
10710 : */
10711 :
10712 : /* static */
10713 10 : int OGRSpatialReference::IsAngularParameter(const char *pszParameterName)
10714 :
10715 : {
10716 10 : if (STARTS_WITH_CI(pszParameterName, "long") ||
10717 10 : STARTS_WITH_CI(pszParameterName, "lati") ||
10718 7 : EQUAL(pszParameterName, SRS_PP_CENTRAL_MERIDIAN) ||
10719 4 : STARTS_WITH_CI(pszParameterName, "standard_parallel") ||
10720 2 : EQUAL(pszParameterName, SRS_PP_AZIMUTH) ||
10721 2 : EQUAL(pszParameterName, SRS_PP_RECTIFIED_GRID_ANGLE))
10722 8 : return TRUE;
10723 :
10724 2 : return FALSE;
10725 : }
10726 :
10727 : /************************************************************************/
10728 : /* IsLongitudeParameter() */
10729 : /************************************************************************/
10730 :
10731 : /** Is the passed projection parameter an angular longitude
10732 : * (relative to a prime meridian)?
10733 : *
10734 : * @return TRUE or FALSE
10735 : */
10736 :
10737 : /* static */
10738 0 : int OGRSpatialReference::IsLongitudeParameter(const char *pszParameterName)
10739 :
10740 : {
10741 0 : if (STARTS_WITH_CI(pszParameterName, "long") ||
10742 0 : EQUAL(pszParameterName, SRS_PP_CENTRAL_MERIDIAN))
10743 0 : return TRUE;
10744 :
10745 0 : return FALSE;
10746 : }
10747 :
10748 : /************************************************************************/
10749 : /* IsLinearParameter() */
10750 : /************************************************************************/
10751 :
10752 : /** Is the passed projection parameter an linear one measured in meters or
10753 : * some similar linear measure.
10754 : *
10755 : * @return TRUE or FALSE
10756 : */
10757 :
10758 : /* static */
10759 43 : int OGRSpatialReference::IsLinearParameter(const char *pszParameterName)
10760 :
10761 : {
10762 43 : if (STARTS_WITH_CI(pszParameterName, "false_") ||
10763 34 : EQUAL(pszParameterName, SRS_PP_SATELLITE_HEIGHT))
10764 9 : return TRUE;
10765 :
10766 34 : return FALSE;
10767 : }
10768 :
10769 : /************************************************************************/
10770 : /* GetNormInfo() */
10771 : /************************************************************************/
10772 :
10773 : /**
10774 : * \brief Set the internal information for normalizing linear, and angular
10775 : * values.
10776 : */
10777 4138 : void OGRSpatialReference::GetNormInfo() const
10778 :
10779 : {
10780 4138 : TAKE_OPTIONAL_LOCK();
10781 :
10782 4138 : if (d->bNormInfoSet)
10783 2958 : return;
10784 :
10785 : /* -------------------------------------------------------------------- */
10786 : /* Initialize values. */
10787 : /* -------------------------------------------------------------------- */
10788 1180 : d->bNormInfoSet = TRUE;
10789 :
10790 1180 : d->dfFromGreenwich = GetPrimeMeridian(nullptr);
10791 1180 : d->dfToMeter = GetLinearUnits(nullptr);
10792 1180 : d->dfToDegrees = GetAngularUnits(nullptr) / CPLAtof(SRS_UA_DEGREE_CONV);
10793 1180 : if (fabs(d->dfToDegrees - 1.0) < 0.000000001)
10794 1177 : d->dfToDegrees = 1.0;
10795 : }
10796 :
10797 : /************************************************************************/
10798 : /* GetExtension() */
10799 : /************************************************************************/
10800 :
10801 : /**
10802 : * \brief Fetch extension value.
10803 : *
10804 : * Fetch the value of the named EXTENSION item for the identified
10805 : * target node.
10806 : *
10807 : * @param pszTargetKey the name or path to the parent node of the EXTENSION.
10808 : * @param pszName the name of the extension being fetched.
10809 : * @param pszDefault the value to return if the extension is not found.
10810 : *
10811 : * @return node value if successful or pszDefault on failure.
10812 : */
10813 :
10814 13590 : const char *OGRSpatialReference::GetExtension(const char *pszTargetKey,
10815 : const char *pszName,
10816 : const char *pszDefault) const
10817 :
10818 : {
10819 27180 : TAKE_OPTIONAL_LOCK();
10820 :
10821 : /* -------------------------------------------------------------------- */
10822 : /* Find the target node. */
10823 : /* -------------------------------------------------------------------- */
10824 : const OGR_SRSNode *poNode =
10825 13590 : pszTargetKey == nullptr ? GetRoot() : GetAttrNode(pszTargetKey);
10826 :
10827 13590 : if (poNode == nullptr)
10828 2391 : return nullptr;
10829 :
10830 : /* -------------------------------------------------------------------- */
10831 : /* Fetch matching EXTENSION if there is one. */
10832 : /* -------------------------------------------------------------------- */
10833 82468 : for (int i = poNode->GetChildCount() - 1; i >= 0; i--)
10834 : {
10835 71293 : const OGR_SRSNode *poChild = poNode->GetChild(i);
10836 :
10837 71319 : if (EQUAL(poChild->GetValue(), "EXTENSION") &&
10838 26 : poChild->GetChildCount() >= 2)
10839 : {
10840 26 : if (EQUAL(poChild->GetChild(0)->GetValue(), pszName))
10841 24 : return poChild->GetChild(1)->GetValue();
10842 : }
10843 : }
10844 :
10845 11175 : return pszDefault;
10846 : }
10847 :
10848 : /************************************************************************/
10849 : /* SetExtension() */
10850 : /************************************************************************/
10851 : /**
10852 : * \brief Set extension value.
10853 : *
10854 : * Set the value of the named EXTENSION item for the identified
10855 : * target node.
10856 : *
10857 : * @param pszTargetKey the name or path to the parent node of the EXTENSION.
10858 : * @param pszName the name of the extension being fetched.
10859 : * @param pszValue the value to set
10860 : *
10861 : * @return OGRERR_NONE on success
10862 : */
10863 :
10864 20 : OGRErr OGRSpatialReference::SetExtension(const char *pszTargetKey,
10865 : const char *pszName,
10866 : const char *pszValue)
10867 :
10868 : {
10869 40 : TAKE_OPTIONAL_LOCK();
10870 :
10871 : /* -------------------------------------------------------------------- */
10872 : /* Find the target node. */
10873 : /* -------------------------------------------------------------------- */
10874 20 : OGR_SRSNode *poNode = nullptr;
10875 :
10876 20 : if (pszTargetKey == nullptr)
10877 0 : poNode = GetRoot();
10878 : else
10879 20 : poNode = GetAttrNode(pszTargetKey);
10880 :
10881 20 : if (poNode == nullptr)
10882 0 : return OGRERR_FAILURE;
10883 :
10884 : /* -------------------------------------------------------------------- */
10885 : /* Fetch matching EXTENSION if there is one. */
10886 : /* -------------------------------------------------------------------- */
10887 151 : for (int i = poNode->GetChildCount() - 1; i >= 0; i--)
10888 : {
10889 137 : OGR_SRSNode *poChild = poNode->GetChild(i);
10890 :
10891 143 : if (EQUAL(poChild->GetValue(), "EXTENSION") &&
10892 6 : poChild->GetChildCount() >= 2)
10893 : {
10894 6 : if (EQUAL(poChild->GetChild(0)->GetValue(), pszName))
10895 : {
10896 6 : poChild->GetChild(1)->SetValue(pszValue);
10897 6 : return OGRERR_NONE;
10898 : }
10899 : }
10900 : }
10901 :
10902 : /* -------------------------------------------------------------------- */
10903 : /* Create a new EXTENSION node. */
10904 : /* -------------------------------------------------------------------- */
10905 14 : OGR_SRSNode *poAuthNode = new OGR_SRSNode("EXTENSION");
10906 14 : poAuthNode->AddChild(new OGR_SRSNode(pszName));
10907 14 : poAuthNode->AddChild(new OGR_SRSNode(pszValue));
10908 :
10909 14 : poNode->AddChild(poAuthNode);
10910 :
10911 14 : return OGRERR_NONE;
10912 : }
10913 :
10914 : /************************************************************************/
10915 : /* OSRCleanup() */
10916 : /************************************************************************/
10917 :
10918 : static void CleanupSRSWGS84Mutex();
10919 :
10920 : /**
10921 : * \brief Cleanup cached SRS related memory.
10922 : *
10923 : * This function will attempt to cleanup any cache spatial reference
10924 : * related information, such as cached tables of coordinate systems.
10925 : *
10926 : * This function should not be called concurrently with any other GDAL/OGR
10927 : * function. It is meant at being called once before process termination
10928 : * (typically from the main thread). CPLCleanupTLS() might be used to clean
10929 : * thread-specific resources before thread termination.
10930 : */
10931 1306 : void OSRCleanup(void)
10932 :
10933 : {
10934 1306 : OGRCTDumpStatistics();
10935 1306 : CSVDeaccess(nullptr);
10936 1306 : CleanupSRSWGS84Mutex();
10937 1306 : OSRCTCleanCache();
10938 1306 : OSRCleanupTLSContext();
10939 1306 : }
10940 :
10941 : /************************************************************************/
10942 : /* GetAxesCount() */
10943 : /************************************************************************/
10944 :
10945 : /**
10946 : * \brief Return the number of axis of the coordinate system of the CRS.
10947 : *
10948 : * @since GDAL 3.0
10949 : */
10950 40291 : int OGRSpatialReference::GetAxesCount() const
10951 : {
10952 80582 : TAKE_OPTIONAL_LOCK();
10953 :
10954 40291 : int axisCount = 0;
10955 40291 : d->refreshProjObj();
10956 40291 : if (d->m_pj_crs == nullptr)
10957 : {
10958 0 : return 0;
10959 : }
10960 40291 : d->demoteFromBoundCRS();
10961 40291 : auto ctxt = d->getPROJContext();
10962 40291 : if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
10963 : {
10964 96 : for (int i = 0;; i++)
10965 : {
10966 288 : auto subCRS = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, i);
10967 288 : if (!subCRS)
10968 96 : break;
10969 192 : if (proj_get_type(subCRS) == PJ_TYPE_BOUND_CRS)
10970 : {
10971 18 : auto baseCRS = proj_get_source_crs(ctxt, subCRS);
10972 18 : if (baseCRS)
10973 : {
10974 18 : proj_destroy(subCRS);
10975 18 : subCRS = baseCRS;
10976 : }
10977 : }
10978 192 : auto cs = proj_crs_get_coordinate_system(ctxt, subCRS);
10979 192 : if (cs)
10980 : {
10981 192 : axisCount += proj_cs_get_axis_count(ctxt, cs);
10982 192 : proj_destroy(cs);
10983 : }
10984 192 : proj_destroy(subCRS);
10985 192 : }
10986 : }
10987 : else
10988 : {
10989 40195 : auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
10990 40195 : if (cs)
10991 : {
10992 40195 : axisCount = proj_cs_get_axis_count(ctxt, cs);
10993 40195 : proj_destroy(cs);
10994 : }
10995 : }
10996 40291 : d->undoDemoteFromBoundCRS();
10997 40291 : return axisCount;
10998 : }
10999 :
11000 : /************************************************************************/
11001 : /* OSRGetAxesCount() */
11002 : /************************************************************************/
11003 :
11004 : /**
11005 : * \brief Return the number of axis of the coordinate system of the CRS.
11006 : *
11007 : * This method is the equivalent of the C++ method
11008 : * OGRSpatialReference::GetAxesCount()
11009 : *
11010 : * @since GDAL 3.1
11011 : */
11012 6 : int OSRGetAxesCount(OGRSpatialReferenceH hSRS)
11013 :
11014 : {
11015 6 : VALIDATE_POINTER1(hSRS, "OSRGetAxesCount", 0);
11016 :
11017 6 : return ToPointer(hSRS)->GetAxesCount();
11018 : }
11019 :
11020 : /************************************************************************/
11021 : /* GetAxis() */
11022 : /************************************************************************/
11023 :
11024 : /**
11025 : * \brief Fetch the orientation of one axis.
11026 : *
11027 : * Fetches the request axis (iAxis - zero based) from the
11028 : * indicated portion of the coordinate system (pszTargetKey) which
11029 : * should be either "GEOGCS" or "PROJCS".
11030 : *
11031 : * No CPLError is issued on routine failures (such as not finding the AXIS).
11032 : *
11033 : * This method is equivalent to the C function OSRGetAxis().
11034 : *
11035 : * @param pszTargetKey the coordinate system part to query ("PROJCS" or
11036 : * "GEOGCS").
11037 : * @param iAxis the axis to query (0 for first, 1 for second, 2 for third).
11038 : * @param peOrientation location into which to place the fetch orientation, may
11039 : * be NULL.
11040 : * @param pdfConvUnit (GDAL >= 3.4) Location into which to place axis conversion
11041 : * factor. May be NULL. Only set if pszTargetKey == NULL
11042 : *
11043 : * @return the name of the axis or NULL on failure.
11044 : */
11045 :
11046 9046 : const char *OGRSpatialReference::GetAxis(const char *pszTargetKey, int iAxis,
11047 : OGRAxisOrientation *peOrientation,
11048 : double *pdfConvUnit) const
11049 :
11050 : {
11051 18092 : TAKE_OPTIONAL_LOCK();
11052 :
11053 9046 : if (peOrientation != nullptr)
11054 8935 : *peOrientation = OAO_Other;
11055 9046 : if (pdfConvUnit != nullptr)
11056 103 : *pdfConvUnit = 0;
11057 :
11058 9046 : d->refreshProjObj();
11059 9046 : if (d->m_pj_crs == nullptr)
11060 : {
11061 3 : return nullptr;
11062 : }
11063 :
11064 9043 : pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
11065 9043 : if (pszTargetKey == nullptr && iAxis <= 2)
11066 : {
11067 9043 : auto ctxt = d->getPROJContext();
11068 :
11069 9043 : int iAxisModified = iAxis;
11070 :
11071 9043 : d->demoteFromBoundCRS();
11072 :
11073 9043 : PJ *cs = nullptr;
11074 9043 : if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
11075 : {
11076 138 : auto horizCRS = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
11077 138 : if (horizCRS)
11078 : {
11079 138 : if (proj_get_type(horizCRS) == PJ_TYPE_BOUND_CRS)
11080 : {
11081 6 : auto baseCRS = proj_get_source_crs(ctxt, horizCRS);
11082 6 : if (baseCRS)
11083 : {
11084 6 : proj_destroy(horizCRS);
11085 6 : horizCRS = baseCRS;
11086 : }
11087 : }
11088 138 : cs = proj_crs_get_coordinate_system(ctxt, horizCRS);
11089 138 : proj_destroy(horizCRS);
11090 138 : if (cs)
11091 : {
11092 138 : if (iAxisModified >= proj_cs_get_axis_count(ctxt, cs))
11093 : {
11094 45 : iAxisModified -= proj_cs_get_axis_count(ctxt, cs);
11095 45 : proj_destroy(cs);
11096 45 : cs = nullptr;
11097 : }
11098 : }
11099 : }
11100 :
11101 138 : if (cs == nullptr)
11102 : {
11103 45 : auto vertCRS = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 1);
11104 45 : if (vertCRS)
11105 : {
11106 45 : if (proj_get_type(vertCRS) == PJ_TYPE_BOUND_CRS)
11107 : {
11108 30 : auto baseCRS = proj_get_source_crs(ctxt, vertCRS);
11109 30 : if (baseCRS)
11110 : {
11111 30 : proj_destroy(vertCRS);
11112 30 : vertCRS = baseCRS;
11113 : }
11114 : }
11115 :
11116 45 : cs = proj_crs_get_coordinate_system(ctxt, vertCRS);
11117 45 : proj_destroy(vertCRS);
11118 : }
11119 : }
11120 : }
11121 : else
11122 : {
11123 8905 : cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
11124 : }
11125 :
11126 9043 : if (cs)
11127 : {
11128 9043 : const char *pszName = nullptr;
11129 9043 : const char *pszOrientation = nullptr;
11130 9043 : double dfConvFactor = 0.0;
11131 9043 : proj_cs_get_axis_info(ctxt, cs, iAxisModified, &pszName, nullptr,
11132 : &pszOrientation, &dfConvFactor, nullptr,
11133 : nullptr, nullptr);
11134 :
11135 9043 : if (pdfConvUnit != nullptr)
11136 : {
11137 103 : *pdfConvUnit = dfConvFactor;
11138 : }
11139 :
11140 9043 : if (pszName && pszOrientation)
11141 : {
11142 9043 : d->m_osAxisName[iAxis] = pszName;
11143 9043 : if (peOrientation)
11144 : {
11145 8932 : if (EQUAL(pszOrientation, "NORTH"))
11146 5626 : *peOrientation = OAO_North;
11147 3306 : else if (EQUAL(pszOrientation, "EAST"))
11148 3228 : *peOrientation = OAO_East;
11149 78 : else if (EQUAL(pszOrientation, "SOUTH"))
11150 67 : *peOrientation = OAO_South;
11151 11 : else if (EQUAL(pszOrientation, "WEST"))
11152 0 : *peOrientation = OAO_West;
11153 11 : else if (EQUAL(pszOrientation, "UP"))
11154 1 : *peOrientation = OAO_Up;
11155 10 : else if (EQUAL(pszOrientation, "DOWN"))
11156 0 : *peOrientation = OAO_Down;
11157 : }
11158 9043 : proj_destroy(cs);
11159 9043 : d->undoDemoteFromBoundCRS();
11160 9043 : return d->m_osAxisName[iAxis].c_str();
11161 : }
11162 0 : proj_destroy(cs);
11163 : }
11164 0 : d->undoDemoteFromBoundCRS();
11165 : }
11166 :
11167 : /* -------------------------------------------------------------------- */
11168 : /* Find the target node. */
11169 : /* -------------------------------------------------------------------- */
11170 0 : const OGR_SRSNode *poNode = nullptr;
11171 :
11172 0 : if (pszTargetKey == nullptr)
11173 0 : poNode = GetRoot();
11174 : else
11175 0 : poNode = GetAttrNode(pszTargetKey);
11176 :
11177 0 : if (poNode == nullptr)
11178 0 : return nullptr;
11179 :
11180 : /* -------------------------------------------------------------------- */
11181 : /* Find desired child AXIS. */
11182 : /* -------------------------------------------------------------------- */
11183 0 : const OGR_SRSNode *poAxis = nullptr;
11184 0 : const int nChildCount = poNode->GetChildCount();
11185 :
11186 0 : for (int iChild = 0; iChild < nChildCount; iChild++)
11187 : {
11188 0 : const OGR_SRSNode *poChild = poNode->GetChild(iChild);
11189 :
11190 0 : if (!EQUAL(poChild->GetValue(), "AXIS"))
11191 0 : continue;
11192 :
11193 0 : if (iAxis == 0)
11194 : {
11195 0 : poAxis = poChild;
11196 0 : break;
11197 : }
11198 0 : iAxis--;
11199 : }
11200 :
11201 0 : if (poAxis == nullptr)
11202 0 : return nullptr;
11203 :
11204 0 : if (poAxis->GetChildCount() < 2)
11205 0 : return nullptr;
11206 :
11207 : /* -------------------------------------------------------------------- */
11208 : /* Extract name and orientation if possible. */
11209 : /* -------------------------------------------------------------------- */
11210 0 : if (peOrientation != nullptr)
11211 : {
11212 0 : const char *pszOrientation = poAxis->GetChild(1)->GetValue();
11213 :
11214 0 : if (EQUAL(pszOrientation, "NORTH"))
11215 0 : *peOrientation = OAO_North;
11216 0 : else if (EQUAL(pszOrientation, "EAST"))
11217 0 : *peOrientation = OAO_East;
11218 0 : else if (EQUAL(pszOrientation, "SOUTH"))
11219 0 : *peOrientation = OAO_South;
11220 0 : else if (EQUAL(pszOrientation, "WEST"))
11221 0 : *peOrientation = OAO_West;
11222 0 : else if (EQUAL(pszOrientation, "UP"))
11223 0 : *peOrientation = OAO_Up;
11224 0 : else if (EQUAL(pszOrientation, "DOWN"))
11225 0 : *peOrientation = OAO_Down;
11226 0 : else if (EQUAL(pszOrientation, "OTHER"))
11227 0 : *peOrientation = OAO_Other;
11228 : else
11229 : {
11230 0 : CPLDebug("OSR", "Unrecognized orientation value '%s'.",
11231 : pszOrientation);
11232 : }
11233 : }
11234 :
11235 0 : return poAxis->GetChild(0)->GetValue();
11236 : }
11237 :
11238 : /************************************************************************/
11239 : /* OSRGetAxis() */
11240 : /************************************************************************/
11241 :
11242 : /**
11243 : * \brief Fetch the orientation of one axis.
11244 : *
11245 : * This method is the equivalent of the C++ method OGRSpatialReference::GetAxis
11246 : */
11247 13 : const char *OSRGetAxis(OGRSpatialReferenceH hSRS, const char *pszTargetKey,
11248 : int iAxis, OGRAxisOrientation *peOrientation)
11249 :
11250 : {
11251 13 : VALIDATE_POINTER1(hSRS, "OSRGetAxis", nullptr);
11252 :
11253 13 : return ToPointer(hSRS)->GetAxis(pszTargetKey, iAxis, peOrientation);
11254 : }
11255 :
11256 : /************************************************************************/
11257 : /* OSRAxisEnumToName() */
11258 : /************************************************************************/
11259 :
11260 : /**
11261 : * \brief Return the string representation for the OGRAxisOrientation
11262 : * enumeration.
11263 : *
11264 : * For example "NORTH" for OAO_North.
11265 : *
11266 : * @return an internal string
11267 : */
11268 400 : const char *OSRAxisEnumToName(OGRAxisOrientation eOrientation)
11269 :
11270 : {
11271 400 : if (eOrientation == OAO_North)
11272 200 : return "NORTH";
11273 200 : if (eOrientation == OAO_East)
11274 200 : return "EAST";
11275 0 : if (eOrientation == OAO_South)
11276 0 : return "SOUTH";
11277 0 : if (eOrientation == OAO_West)
11278 0 : return "WEST";
11279 0 : if (eOrientation == OAO_Up)
11280 0 : return "UP";
11281 0 : if (eOrientation == OAO_Down)
11282 0 : return "DOWN";
11283 0 : if (eOrientation == OAO_Other)
11284 0 : return "OTHER";
11285 :
11286 0 : return "UNKNOWN";
11287 : }
11288 :
11289 : /************************************************************************/
11290 : /* SetAxes() */
11291 : /************************************************************************/
11292 :
11293 : /**
11294 : * \brief Set the axes for a coordinate system.
11295 : *
11296 : * Set the names, and orientations of the axes for either a projected
11297 : * (PROJCS) or geographic (GEOGCS) coordinate system.
11298 : *
11299 : * This method is equivalent to the C function OSRSetAxes().
11300 : *
11301 : * @param pszTargetKey either "PROJCS" or "GEOGCS", must already exist in SRS.
11302 : * @param pszXAxisName name of first axis, normally "Long" or "Easting".
11303 : * @param eXAxisOrientation normally OAO_East.
11304 : * @param pszYAxisName name of second axis, normally "Lat" or "Northing".
11305 : * @param eYAxisOrientation normally OAO_North.
11306 : *
11307 : * @return OGRERR_NONE on success or an error code.
11308 : */
11309 :
11310 200 : OGRErr OGRSpatialReference::SetAxes(const char *pszTargetKey,
11311 : const char *pszXAxisName,
11312 : OGRAxisOrientation eXAxisOrientation,
11313 : const char *pszYAxisName,
11314 : OGRAxisOrientation eYAxisOrientation)
11315 :
11316 : {
11317 400 : TAKE_OPTIONAL_LOCK();
11318 :
11319 : /* -------------------------------------------------------------------- */
11320 : /* Find the target node. */
11321 : /* -------------------------------------------------------------------- */
11322 200 : OGR_SRSNode *poNode = nullptr;
11323 :
11324 200 : if (pszTargetKey == nullptr)
11325 200 : poNode = GetRoot();
11326 : else
11327 0 : poNode = GetAttrNode(pszTargetKey);
11328 :
11329 200 : if (poNode == nullptr)
11330 0 : return OGRERR_FAILURE;
11331 :
11332 : /* -------------------------------------------------------------------- */
11333 : /* Strip any existing AXIS children. */
11334 : /* -------------------------------------------------------------------- */
11335 600 : while (poNode->FindChild("AXIS") >= 0)
11336 400 : poNode->DestroyChild(poNode->FindChild("AXIS"));
11337 :
11338 : /* -------------------------------------------------------------------- */
11339 : /* Insert desired axes */
11340 : /* -------------------------------------------------------------------- */
11341 200 : OGR_SRSNode *poAxis = new OGR_SRSNode("AXIS");
11342 :
11343 200 : poAxis->AddChild(new OGR_SRSNode(pszXAxisName));
11344 200 : poAxis->AddChild(new OGR_SRSNode(OSRAxisEnumToName(eXAxisOrientation)));
11345 :
11346 200 : poNode->AddChild(poAxis);
11347 :
11348 200 : poAxis = new OGR_SRSNode("AXIS");
11349 :
11350 200 : poAxis->AddChild(new OGR_SRSNode(pszYAxisName));
11351 200 : poAxis->AddChild(new OGR_SRSNode(OSRAxisEnumToName(eYAxisOrientation)));
11352 :
11353 200 : poNode->AddChild(poAxis);
11354 :
11355 200 : return OGRERR_NONE;
11356 : }
11357 :
11358 : /************************************************************************/
11359 : /* OSRSetAxes() */
11360 : /************************************************************************/
11361 : /**
11362 : * \brief Set the axes for a coordinate system.
11363 : *
11364 : * This method is the equivalent of the C++ method OGRSpatialReference::SetAxes
11365 : */
11366 0 : OGRErr OSRSetAxes(OGRSpatialReferenceH hSRS, const char *pszTargetKey,
11367 : const char *pszXAxisName,
11368 : OGRAxisOrientation eXAxisOrientation,
11369 : const char *pszYAxisName,
11370 : OGRAxisOrientation eYAxisOrientation)
11371 : {
11372 0 : VALIDATE_POINTER1(hSRS, "OSRSetAxes", OGRERR_FAILURE);
11373 :
11374 0 : return ToPointer(hSRS)->SetAxes(pszTargetKey, pszXAxisName,
11375 : eXAxisOrientation, pszYAxisName,
11376 0 : eYAxisOrientation);
11377 : }
11378 :
11379 : /************************************************************************/
11380 : /* OSRExportToMICoordSys() */
11381 : /************************************************************************/
11382 : /**
11383 : * \brief Export coordinate system in Mapinfo style CoordSys format.
11384 : *
11385 : * This method is the equivalent of the C++ method
11386 : * OGRSpatialReference::exportToMICoordSys
11387 : */
11388 5 : OGRErr OSRExportToMICoordSys(OGRSpatialReferenceH hSRS, char **ppszReturn)
11389 :
11390 : {
11391 5 : VALIDATE_POINTER1(hSRS, "OSRExportToMICoordSys", OGRERR_FAILURE);
11392 :
11393 5 : *ppszReturn = nullptr;
11394 :
11395 5 : return ToPointer(hSRS)->exportToMICoordSys(ppszReturn);
11396 : }
11397 :
11398 : /************************************************************************/
11399 : /* exportToMICoordSys() */
11400 : /************************************************************************/
11401 :
11402 : /**
11403 : * \brief Export coordinate system in Mapinfo style CoordSys format.
11404 : *
11405 : * Note that the returned WKT string should be freed with
11406 : * CPLFree() when no longer needed. It is the responsibility of the caller.
11407 : *
11408 : * This method is the same as the C function OSRExportToMICoordSys().
11409 : *
11410 : * @param ppszResult pointer to which dynamically allocated Mapinfo CoordSys
11411 : * definition will be assigned.
11412 : *
11413 : * @return OGRERR_NONE on success, OGRERR_FAILURE on failure,
11414 : * OGRERR_UNSUPPORTED_OPERATION if MITAB library was not linked in.
11415 : */
11416 :
11417 7 : OGRErr OGRSpatialReference::exportToMICoordSys(char **ppszResult) const
11418 :
11419 : {
11420 7 : *ppszResult = MITABSpatialRef2CoordSys(this);
11421 7 : if (*ppszResult != nullptr && strlen(*ppszResult) > 0)
11422 7 : return OGRERR_NONE;
11423 :
11424 0 : return OGRERR_FAILURE;
11425 : }
11426 :
11427 : /************************************************************************/
11428 : /* OSRImportFromMICoordSys() */
11429 : /************************************************************************/
11430 : /**
11431 : * \brief Import Mapinfo style CoordSys definition.
11432 : *
11433 : * This method is the equivalent of the C++ method
11434 : * OGRSpatialReference::importFromMICoordSys
11435 : */
11436 :
11437 3 : OGRErr OSRImportFromMICoordSys(OGRSpatialReferenceH hSRS,
11438 : const char *pszCoordSys)
11439 :
11440 : {
11441 3 : VALIDATE_POINTER1(hSRS, "OSRImportFromMICoordSys", OGRERR_FAILURE);
11442 :
11443 3 : return ToPointer(hSRS)->importFromMICoordSys(pszCoordSys);
11444 : }
11445 :
11446 : /************************************************************************/
11447 : /* importFromMICoordSys() */
11448 : /************************************************************************/
11449 :
11450 : /**
11451 : * \brief Import Mapinfo style CoordSys definition.
11452 : *
11453 : * The OGRSpatialReference is initialized from the passed Mapinfo style CoordSys
11454 : * definition string.
11455 : *
11456 : * This method is the equivalent of the C function OSRImportFromMICoordSys().
11457 : *
11458 : * @param pszCoordSys Mapinfo style CoordSys definition string.
11459 : *
11460 : * @return OGRERR_NONE on success, OGRERR_FAILURE on failure,
11461 : * OGRERR_UNSUPPORTED_OPERATION if MITAB library was not linked in.
11462 : */
11463 :
11464 17 : OGRErr OGRSpatialReference::importFromMICoordSys(const char *pszCoordSys)
11465 :
11466 : {
11467 17 : OGRSpatialReference *poResult = MITABCoordSys2SpatialRef(pszCoordSys);
11468 :
11469 17 : if (poResult == nullptr)
11470 0 : return OGRERR_FAILURE;
11471 :
11472 17 : *this = *poResult;
11473 17 : delete poResult;
11474 :
11475 17 : return OGRERR_NONE;
11476 : }
11477 :
11478 : /************************************************************************/
11479 : /* OSRCalcInvFlattening() */
11480 : /************************************************************************/
11481 :
11482 : /**
11483 : * \brief Compute inverse flattening from semi-major and semi-minor axis
11484 : *
11485 : * @param dfSemiMajor Semi-major axis length.
11486 : * @param dfSemiMinor Semi-minor axis length.
11487 : *
11488 : * @return inverse flattening, or 0 if both axis are equal.
11489 : */
11490 :
11491 8916 : double OSRCalcInvFlattening(double dfSemiMajor, double dfSemiMinor)
11492 : {
11493 8916 : if (fabs(dfSemiMajor - dfSemiMinor) < 1e-1)
11494 27 : return 0;
11495 8889 : if (dfSemiMajor <= 0 || dfSemiMinor <= 0 || dfSemiMinor > dfSemiMajor)
11496 : {
11497 0 : CPLError(CE_Failure, CPLE_IllegalArg,
11498 : "OSRCalcInvFlattening(): Wrong input values");
11499 0 : return 0;
11500 : }
11501 :
11502 8889 : return dfSemiMajor / (dfSemiMajor - dfSemiMinor);
11503 : }
11504 :
11505 : /************************************************************************/
11506 : /* OSRCalcInvFlattening() */
11507 : /************************************************************************/
11508 :
11509 : /**
11510 : * \brief Compute semi-minor axis from semi-major axis and inverse flattening.
11511 : *
11512 : * @param dfSemiMajor Semi-major axis length.
11513 : * @param dfInvFlattening Inverse flattening or 0 for sphere.
11514 : *
11515 : * @return semi-minor axis
11516 : */
11517 :
11518 655 : double OSRCalcSemiMinorFromInvFlattening(double dfSemiMajor,
11519 : double dfInvFlattening)
11520 : {
11521 655 : if (fabs(dfInvFlattening) < 0.000000000001)
11522 103 : return dfSemiMajor;
11523 552 : if (dfSemiMajor <= 0.0 || dfInvFlattening <= 1.0)
11524 : {
11525 0 : CPLError(CE_Failure, CPLE_IllegalArg,
11526 : "OSRCalcSemiMinorFromInvFlattening(): Wrong input values");
11527 0 : return dfSemiMajor;
11528 : }
11529 :
11530 552 : return dfSemiMajor * (1.0 - 1.0 / dfInvFlattening);
11531 : }
11532 :
11533 : /************************************************************************/
11534 : /* GetWGS84SRS() */
11535 : /************************************************************************/
11536 :
11537 : static OGRSpatialReference *poSRSWGS84 = nullptr;
11538 : static CPLMutex *hMutex = nullptr;
11539 :
11540 : /**
11541 : * \brief Returns an instance of a SRS object with WGS84 WKT.
11542 : *
11543 : * Note: the instance will have
11544 : * SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER)
11545 : *
11546 : * The reference counter of the returned object is not increased by this
11547 : * operation.
11548 : *
11549 : * @return instance.
11550 : */
11551 :
11552 1004 : OGRSpatialReference *OGRSpatialReference::GetWGS84SRS()
11553 : {
11554 1004 : CPLMutexHolderD(&hMutex);
11555 1004 : if (poSRSWGS84 == nullptr)
11556 : {
11557 5 : poSRSWGS84 = new OGRSpatialReference(SRS_WKT_WGS84_LAT_LONG);
11558 5 : poSRSWGS84->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
11559 : }
11560 2008 : return poSRSWGS84;
11561 : }
11562 :
11563 : /************************************************************************/
11564 : /* CleanupSRSWGS84Mutex() */
11565 : /************************************************************************/
11566 :
11567 1306 : static void CleanupSRSWGS84Mutex()
11568 : {
11569 1306 : if (hMutex != nullptr)
11570 : {
11571 3 : poSRSWGS84->Release();
11572 3 : poSRSWGS84 = nullptr;
11573 3 : CPLDestroyMutex(hMutex);
11574 3 : hMutex = nullptr;
11575 : }
11576 1306 : }
11577 :
11578 : /************************************************************************/
11579 : /* OSRImportFromProj4() */
11580 : /************************************************************************/
11581 : /**
11582 : * \brief Import PROJ coordinate string.
11583 : *
11584 : * This function is the same as OGRSpatialReference::importFromProj4().
11585 : */
11586 218 : OGRErr OSRImportFromProj4(OGRSpatialReferenceH hSRS, const char *pszProj4)
11587 :
11588 : {
11589 218 : VALIDATE_POINTER1(hSRS, "OSRImportFromProj4", OGRERR_FAILURE);
11590 :
11591 218 : return OGRSpatialReference::FromHandle(hSRS)->importFromProj4(pszProj4);
11592 : }
11593 :
11594 : /************************************************************************/
11595 : /* importFromProj4() */
11596 : /************************************************************************/
11597 :
11598 : /**
11599 : * \brief Import PROJ coordinate string.
11600 : *
11601 : * The OGRSpatialReference is initialized from the passed PROJs style
11602 : * coordinate system string.
11603 : *
11604 : * Example:
11605 : * pszProj4 = "+proj=utm +zone=11 +datum=WGS84"
11606 : *
11607 : * It is also possible to import "+init=epsg:n" style definitions. Those are
11608 : * a legacy syntax that should be avoided in the future. In particular they will
11609 : * result in CRS objects whose axis order might not correspond to the official
11610 : * EPSG axis order.
11611 : *
11612 : * This method is the equivalent of the C function OSRImportFromProj4().
11613 : *
11614 : * @param pszProj4 the PROJ style string.
11615 : *
11616 : * @return OGRERR_NONE on success or OGRERR_CORRUPT_DATA on failure.
11617 : */
11618 :
11619 771 : OGRErr OGRSpatialReference::importFromProj4(const char *pszProj4)
11620 :
11621 : {
11622 1542 : TAKE_OPTIONAL_LOCK();
11623 :
11624 771 : if (strlen(pszProj4) >= 10000)
11625 : {
11626 1 : CPLError(CE_Failure, CPLE_AppDefined, "Too long PROJ string");
11627 1 : return OGRERR_CORRUPT_DATA;
11628 : }
11629 :
11630 : /* -------------------------------------------------------------------- */
11631 : /* Clear any existing definition. */
11632 : /* -------------------------------------------------------------------- */
11633 770 : Clear();
11634 :
11635 770 : CPLString osProj4(pszProj4);
11636 770 : if (osProj4.find("type=crs") == std::string::npos)
11637 : {
11638 761 : osProj4 += " +type=crs";
11639 : }
11640 :
11641 772 : if (osProj4.find("+init=epsg:") != std::string::npos &&
11642 2 : getenv("PROJ_USE_PROJ4_INIT_RULES") == nullptr)
11643 : {
11644 2 : CPLErrorOnce(CE_Warning, CPLE_AppDefined,
11645 : "+init=epsg:XXXX syntax is deprecated. It might return "
11646 : "a CRS with a non-EPSG compliant axis order.");
11647 : }
11648 770 : proj_context_use_proj4_init_rules(d->getPROJContext(), true);
11649 770 : d->setPjCRS(proj_create(d->getPROJContext(), osProj4.c_str()));
11650 770 : proj_context_use_proj4_init_rules(d->getPROJContext(), false);
11651 770 : return d->m_pj_crs ? OGRERR_NONE : OGRERR_CORRUPT_DATA;
11652 : }
11653 :
11654 : /************************************************************************/
11655 : /* OSRExportToProj4() */
11656 : /************************************************************************/
11657 : /**
11658 : * \brief Export coordinate system in PROJ.4 legacy format.
11659 : *
11660 : * \warning Use of this function is discouraged. Its behavior in GDAL >= 3 /
11661 : * PROJ >= 6 is significantly different from earlier versions. In particular
11662 : * +datum will only encode WGS84, NAD27 and NAD83, and +towgs84/+nadgrids terms
11663 : * will be missing most of the time. PROJ strings to encode CRS should be
11664 : * considered as a legacy solution. Using a AUTHORITY:CODE or WKT representation
11665 : * is the recommended way.
11666 : *
11667 : * This function is the same as OGRSpatialReference::exportToProj4().
11668 : */
11669 311 : OGRErr CPL_STDCALL OSRExportToProj4(OGRSpatialReferenceH hSRS,
11670 : char **ppszReturn)
11671 :
11672 : {
11673 311 : VALIDATE_POINTER1(hSRS, "OSRExportToProj4", OGRERR_FAILURE);
11674 :
11675 311 : *ppszReturn = nullptr;
11676 :
11677 311 : return OGRSpatialReference::FromHandle(hSRS)->exportToProj4(ppszReturn);
11678 : }
11679 :
11680 : /************************************************************************/
11681 : /* exportToProj4() */
11682 : /************************************************************************/
11683 :
11684 : /**
11685 : * \brief Export coordinate system in PROJ.4 legacy format.
11686 : *
11687 : * \warning Use of this function is discouraged. Its behavior in GDAL >= 3 /
11688 : * PROJ >= 6 is significantly different from earlier versions. In particular
11689 : * +datum will only encode WGS84, NAD27 and NAD83, and +towgs84/+nadgrids terms
11690 : * will be missing most of the time. PROJ strings to encode CRS should be
11691 : * considered as a a legacy solution. Using a AUTHORITY:CODE or WKT
11692 : * representation is the recommended way.
11693 : *
11694 : * Converts the loaded coordinate reference system into PROJ format
11695 : * to the extent possible. The string returned in ppszProj4 should be
11696 : * deallocated by the caller with CPLFree() when no longer needed.
11697 : *
11698 : * LOCAL_CS coordinate systems are not translatable. An empty string
11699 : * will be returned along with OGRERR_NONE.
11700 : *
11701 : * Special processing for Transverse Mercator:
11702 : * Starting with GDAL 3.0, if the OSR_USE_APPROX_TMERC configuration option is
11703 : * set to YES, the PROJ definition built from the SRS will use the +approx flag
11704 : * for the tmerc and utm projection methods, rather than the more accurate
11705 : * method.
11706 : *
11707 : * Starting with GDAL 3.0.3, this method will try to add a +towgs84 parameter,
11708 : * if there's none attached yet to the SRS and if the SRS has a EPSG code.
11709 : * See the AddGuessedTOWGS84() method for how this +towgs84 parameter may be
11710 : * added. This automatic addition may be disabled by setting the
11711 : * OSR_ADD_TOWGS84_ON_EXPORT_TO_PROJ4 configuration option to NO.
11712 : *
11713 : * This method is the equivalent of the C function OSRExportToProj4().
11714 : *
11715 : * @param ppszProj4 pointer to which dynamically allocated PROJ definition
11716 : * will be assigned.
11717 : *
11718 : * @return OGRERR_NONE on success or an error code on failure.
11719 : */
11720 :
11721 1454 : OGRErr OGRSpatialReference::exportToProj4(char **ppszProj4) const
11722 :
11723 : {
11724 : // In the past calling this method was thread-safe, even if we never
11725 : // guaranteed it. Now proj_as_proj_string() will cache the result
11726 : // internally, so this is no longer thread-safe.
11727 2908 : std::lock_guard oLock(d->m_mutex);
11728 :
11729 1454 : d->refreshProjObj();
11730 1454 : if (d->m_pj_crs == nullptr || d->m_pjType == PJ_TYPE_ENGINEERING_CRS)
11731 : {
11732 4 : *ppszProj4 = CPLStrdup("");
11733 4 : return OGRERR_FAILURE;
11734 : }
11735 :
11736 : // OSR_USE_ETMERC is here just for legacy
11737 1450 : bool bForceApproxTMerc = false;
11738 1450 : const char *pszUseETMERC = CPLGetConfigOption("OSR_USE_ETMERC", nullptr);
11739 1450 : if (pszUseETMERC && pszUseETMERC[0])
11740 : {
11741 0 : CPLErrorOnce(CE_Warning, CPLE_AppDefined,
11742 : "OSR_USE_ETMERC is a legacy configuration option, which "
11743 : "now has only effect when set to NO (YES is the default). "
11744 : "Use OSR_USE_APPROX_TMERC=YES instead");
11745 0 : bForceApproxTMerc = !CPLTestBool(pszUseETMERC);
11746 : }
11747 : else
11748 : {
11749 : const char *pszUseApproxTMERC =
11750 1450 : CPLGetConfigOption("OSR_USE_APPROX_TMERC", nullptr);
11751 1450 : if (pszUseApproxTMERC && pszUseApproxTMERC[0])
11752 : {
11753 2 : bForceApproxTMerc = CPLTestBool(pszUseApproxTMERC);
11754 : }
11755 : }
11756 1450 : const char *options[] = {
11757 1450 : bForceApproxTMerc ? "USE_APPROX_TMERC=YES" : nullptr, nullptr};
11758 :
11759 1450 : const char *projString = proj_as_proj_string(
11760 1450 : d->getPROJContext(), d->m_pj_crs, PJ_PROJ_4, options);
11761 :
11762 1450 : PJ *boundCRS = nullptr;
11763 2896 : if (projString &&
11764 1446 : (strstr(projString, "+datum=") == nullptr ||
11765 2906 : d->m_pjType == PJ_TYPE_COMPOUND_CRS) &&
11766 531 : CPLTestBool(
11767 : CPLGetConfigOption("OSR_ADD_TOWGS84_ON_EXPORT_TO_PROJ4", "YES")))
11768 : {
11769 531 : boundCRS = GDAL_proj_crs_create_bound_crs_to_WGS84(
11770 531 : d->getPROJContext(), d->m_pj_crs, true,
11771 531 : strstr(projString, "+datum=") == nullptr);
11772 531 : if (boundCRS)
11773 : {
11774 174 : projString = proj_as_proj_string(d->getPROJContext(), boundCRS,
11775 : PJ_PROJ_4, options);
11776 : }
11777 : }
11778 :
11779 1450 : if (projString == nullptr)
11780 : {
11781 4 : *ppszProj4 = CPLStrdup("");
11782 4 : proj_destroy(boundCRS);
11783 4 : return OGRERR_FAILURE;
11784 : }
11785 1446 : *ppszProj4 = CPLStrdup(projString);
11786 1446 : proj_destroy(boundCRS);
11787 1446 : char *pszTypeCrs = strstr(*ppszProj4, " +type=crs");
11788 1446 : if (pszTypeCrs)
11789 1446 : *pszTypeCrs = '\0';
11790 1446 : return OGRERR_NONE;
11791 : }
11792 :
11793 : /************************************************************************/
11794 : /* morphToESRI() */
11795 : /************************************************************************/
11796 : /**
11797 : * \brief Convert in place to ESRI WKT format.
11798 : *
11799 : * The value nodes of this coordinate system are modified in various manners
11800 : * more closely map onto the ESRI concept of WKT format. This includes
11801 : * renaming a variety of projections and arguments, and stripping out
11802 : * nodes note recognised by ESRI (like AUTHORITY and AXIS).
11803 : *
11804 : * \note Since GDAL 3.0, this function has only user-visible effects at
11805 : * exportToWkt() time. It is recommended to use instead exportToWkt(char**,
11806 : * const char* const char*) const with options having FORMAT=WKT1_ESRI.
11807 : *
11808 : * This does the same as the C function OSRMorphToESRI().
11809 : *
11810 : * @return OGRERR_NONE unless something goes badly wrong.
11811 : * @deprecated
11812 : */
11813 :
11814 236 : OGRErr OGRSpatialReference::morphToESRI()
11815 :
11816 : {
11817 236 : TAKE_OPTIONAL_LOCK();
11818 :
11819 236 : d->refreshProjObj();
11820 236 : d->setMorphToESRI(true);
11821 :
11822 472 : return OGRERR_NONE;
11823 : }
11824 :
11825 : /************************************************************************/
11826 : /* OSRMorphToESRI() */
11827 : /************************************************************************/
11828 :
11829 : /**
11830 : * \brief Convert in place to ESRI WKT format.
11831 : *
11832 : * This function is the same as the C++ method
11833 : * OGRSpatialReference::morphToESRI().
11834 : */
11835 71 : OGRErr OSRMorphToESRI(OGRSpatialReferenceH hSRS)
11836 :
11837 : {
11838 71 : VALIDATE_POINTER1(hSRS, "OSRMorphToESRI", OGRERR_FAILURE);
11839 :
11840 71 : return OGRSpatialReference::FromHandle(hSRS)->morphToESRI();
11841 : }
11842 :
11843 : /************************************************************************/
11844 : /* morphFromESRI() */
11845 : /************************************************************************/
11846 :
11847 : /**
11848 : * \brief Convert in place from ESRI WKT format.
11849 : *
11850 : * The value notes of this coordinate system are modified in various manners
11851 : * to adhere more closely to the WKT standard. This mostly involves
11852 : * translating a variety of ESRI names for projections, arguments and
11853 : * datums to "standard" names, as defined by Adam Gawne-Cain's reference
11854 : * translation of EPSG to WKT for the CT specification.
11855 : *
11856 : * \note Since GDAL 3.0, this function is essentially a no-operation, since
11857 : * morphing from ESRI is automatically done by importFromWkt(). Its only
11858 : * effect is to undo the effect of a potential prior call to morphToESRI().
11859 : *
11860 : * This does the same as the C function OSRMorphFromESRI().
11861 : *
11862 : * @return OGRERR_NONE unless something goes badly wrong.
11863 : * @deprecated
11864 : */
11865 :
11866 21 : OGRErr OGRSpatialReference::morphFromESRI()
11867 :
11868 : {
11869 21 : TAKE_OPTIONAL_LOCK();
11870 :
11871 21 : d->refreshProjObj();
11872 21 : d->setMorphToESRI(false);
11873 :
11874 42 : return OGRERR_NONE;
11875 : }
11876 :
11877 : /************************************************************************/
11878 : /* OSRMorphFromESRI() */
11879 : /************************************************************************/
11880 :
11881 : /**
11882 : * \brief Convert in place from ESRI WKT format.
11883 : *
11884 : * This function is the same as the C++ method
11885 : * OGRSpatialReference::morphFromESRI().
11886 : */
11887 20 : OGRErr OSRMorphFromESRI(OGRSpatialReferenceH hSRS)
11888 :
11889 : {
11890 20 : VALIDATE_POINTER1(hSRS, "OSRMorphFromESRI", OGRERR_FAILURE);
11891 :
11892 20 : return OGRSpatialReference::FromHandle(hSRS)->morphFromESRI();
11893 : }
11894 :
11895 : /************************************************************************/
11896 : /* FindMatches() */
11897 : /************************************************************************/
11898 :
11899 : /**
11900 : * \brief Try to identify a match between the passed SRS and a related SRS
11901 : * in a catalog.
11902 : *
11903 : * Matching may be partial, or may fail.
11904 : * Returned entries will be sorted by decreasing match confidence (first
11905 : * entry has the highest match confidence).
11906 : *
11907 : * The exact way matching is done may change in future versions. Starting with
11908 : * GDAL 3.0, it relies on PROJ' proj_identify() function.
11909 : *
11910 : * This method is the same as OSRFindMatches().
11911 : *
11912 : * @param papszOptions NULL terminated list of options or NULL
11913 : * @param pnEntries Output parameter. Number of values in the returned array.
11914 : * @param ppanMatchConfidence Output parameter (or NULL). *ppanMatchConfidence
11915 : * will be allocated to an array of *pnEntries whose values between 0 and 100
11916 : * indicate the confidence in the match. 100 is the highest confidence level.
11917 : * The array must be freed with CPLFree().
11918 : *
11919 : * @return an array of SRS that match the passed SRS, or NULL. Must be freed
11920 : * with OSRFreeSRSArray()
11921 : *
11922 : *
11923 : * @see OGRSpatialReference::FindBestMatch()
11924 : */
11925 : OGRSpatialReferenceH *
11926 1564 : OGRSpatialReference::FindMatches(CSLConstList papszOptions, int *pnEntries,
11927 : int **ppanMatchConfidence) const
11928 : {
11929 3128 : TAKE_OPTIONAL_LOCK();
11930 :
11931 1564 : CPL_IGNORE_RET_VAL(papszOptions);
11932 :
11933 1564 : if (pnEntries)
11934 1564 : *pnEntries = 0;
11935 1564 : if (ppanMatchConfidence)
11936 1564 : *ppanMatchConfidence = nullptr;
11937 :
11938 1564 : d->refreshProjObj();
11939 1564 : if (!d->m_pj_crs)
11940 0 : return nullptr;
11941 :
11942 1564 : int *panConfidence = nullptr;
11943 1564 : auto ctxt = d->getPROJContext();
11944 : auto list =
11945 1564 : proj_identify(ctxt, d->m_pj_crs, nullptr, nullptr, &panConfidence);
11946 1564 : if (!list)
11947 0 : return nullptr;
11948 :
11949 1564 : const int nMatches = proj_list_get_count(list);
11950 :
11951 1564 : if (pnEntries)
11952 1564 : *pnEntries = static_cast<int>(nMatches);
11953 : OGRSpatialReferenceH *pahRet = static_cast<OGRSpatialReferenceH *>(
11954 1564 : CPLCalloc(sizeof(OGRSpatialReferenceH), nMatches + 1));
11955 1564 : if (ppanMatchConfidence)
11956 : {
11957 1564 : *ppanMatchConfidence =
11958 1564 : static_cast<int *>(CPLMalloc(sizeof(int) * (nMatches + 1)));
11959 : }
11960 :
11961 1564 : bool bSortAgain = false;
11962 :
11963 4510 : for (int i = 0; i < nMatches; i++)
11964 : {
11965 2946 : PJ *obj = proj_list_get(ctxt, list, i);
11966 2946 : CPLAssert(obj);
11967 2946 : OGRSpatialReference *poSRS = new OGRSpatialReference();
11968 2946 : poSRS->d->setPjCRS(obj);
11969 2946 : pahRet[i] = ToHandle(poSRS);
11970 :
11971 : // Identify matches that only differ by axis order
11972 9 : if (panConfidence[i] == 50 && GetAxesCount() == 2 &&
11973 2964 : poSRS->GetAxesCount() == 2 &&
11974 2955 : GetDataAxisToSRSAxisMapping() == std::vector<int>{1, 2})
11975 : {
11976 9 : OGRAxisOrientation eThisAxis0 = OAO_Other;
11977 9 : OGRAxisOrientation eThisAxis1 = OAO_Other;
11978 9 : OGRAxisOrientation eSRSAxis0 = OAO_Other;
11979 9 : OGRAxisOrientation eSRSAxis1 = OAO_Other;
11980 9 : GetAxis(nullptr, 0, &eThisAxis0);
11981 9 : GetAxis(nullptr, 1, &eThisAxis1);
11982 9 : poSRS->GetAxis(nullptr, 0, &eSRSAxis0);
11983 9 : poSRS->GetAxis(nullptr, 1, &eSRSAxis1);
11984 9 : if (eThisAxis0 == OAO_East && eThisAxis1 == OAO_North &&
11985 9 : eSRSAxis0 == OAO_North && eSRSAxis1 == OAO_East)
11986 : {
11987 : auto pj_crs_normalized =
11988 9 : proj_normalize_for_visualization(ctxt, poSRS->d->m_pj_crs);
11989 9 : if (pj_crs_normalized)
11990 : {
11991 9 : if (proj_is_equivalent_to(d->m_pj_crs, pj_crs_normalized,
11992 9 : PJ_COMP_EQUIVALENT))
11993 : {
11994 3 : bSortAgain = true;
11995 3 : panConfidence[i] = 90;
11996 3 : poSRS->SetDataAxisToSRSAxisMapping({2, 1});
11997 : }
11998 9 : proj_destroy(pj_crs_normalized);
11999 : }
12000 : }
12001 : }
12002 :
12003 2946 : if (ppanMatchConfidence)
12004 2946 : (*ppanMatchConfidence)[i] = panConfidence[i];
12005 : }
12006 :
12007 1564 : if (bSortAgain)
12008 : {
12009 3 : std::vector<int> anIndices;
12010 12 : for (int i = 0; i < nMatches; ++i)
12011 9 : anIndices.push_back(i);
12012 :
12013 3 : std::stable_sort(anIndices.begin(), anIndices.end(),
12014 9 : [&panConfidence](int i, int j)
12015 9 : { return panConfidence[i] > panConfidence[j]; });
12016 :
12017 : OGRSpatialReferenceH *pahRetSorted =
12018 : static_cast<OGRSpatialReferenceH *>(
12019 3 : CPLCalloc(sizeof(OGRSpatialReferenceH), nMatches + 1));
12020 12 : for (int i = 0; i < nMatches; ++i)
12021 : {
12022 9 : pahRetSorted[i] = pahRet[anIndices[i]];
12023 9 : if (ppanMatchConfidence)
12024 9 : (*ppanMatchConfidence)[i] = panConfidence[anIndices[i]];
12025 : }
12026 3 : CPLFree(pahRet);
12027 3 : pahRet = pahRetSorted;
12028 : }
12029 :
12030 1564 : pahRet[nMatches] = nullptr;
12031 1564 : proj_list_destroy(list);
12032 1564 : proj_int_list_destroy(panConfidence);
12033 :
12034 1564 : return pahRet;
12035 : }
12036 :
12037 : /************************************************************************/
12038 : /* importFromEPSGA() */
12039 : /************************************************************************/
12040 :
12041 : /**
12042 : * \brief Initialize SRS based on EPSG geographic, projected or vertical CRS
12043 : * code.
12044 : *
12045 : * This method will initialize the spatial reference based on the
12046 : * passed in EPSG CRS code found in the PROJ database.
12047 : *
12048 : * Since GDAL 3.0, this method is identical to importFromEPSG().
12049 : *
12050 : * Before GDAL 3.0.3, this method would try to attach a 3-parameter or
12051 : * 7-parameter Helmert transformation to WGS84 when there is one and only one
12052 : * such method available for the CRS. This behavior might not always be
12053 : * desirable, so starting with GDAL 3.0.3, this is no longer done unless
12054 : * the OSR_ADD_TOWGS84_ON_IMPORT_FROM_EPSG configuration option is set to YES.
12055 : * The AddGuessedTOWGS84() method can also be used for that purpose.
12056 : *
12057 : * The method will also by default substitute a deprecated EPSG code by its
12058 : * non-deprecated replacement. If this behavior is not desired, the
12059 : * OSR_USE_NON_DEPRECATED configuration option can be set to NO.
12060 : *
12061 : * This method is the same as the C function OSRImportFromEPSGA().
12062 : *
12063 : * @param nCode a CRS code.
12064 : *
12065 : * @return OGRERR_NONE on success, or an error code on failure.
12066 : */
12067 :
12068 49754 : OGRErr OGRSpatialReference::importFromEPSGA(int nCode)
12069 :
12070 : {
12071 99508 : TAKE_OPTIONAL_LOCK();
12072 :
12073 49754 : Clear();
12074 :
12075 : const char *pszUseNonDeprecated =
12076 49754 : CPLGetConfigOption("OSR_USE_NON_DEPRECATED", nullptr);
12077 : const bool bUseNonDeprecated =
12078 49754 : CPLTestBool(pszUseNonDeprecated ? pszUseNonDeprecated : "YES");
12079 49754 : const bool bAddTOWGS84 = CPLTestBool(
12080 : CPLGetConfigOption("OSR_ADD_TOWGS84_ON_IMPORT_FROM_EPSG", "NO"));
12081 49754 : auto tlsCache = OSRGetProjTLSCache();
12082 49754 : if (tlsCache)
12083 : {
12084 : auto cachedObj =
12085 49754 : tlsCache->GetPJForEPSGCode(nCode, bUseNonDeprecated, bAddTOWGS84);
12086 49754 : if (cachedObj)
12087 : {
12088 39352 : d->setPjCRS(cachedObj);
12089 39352 : return OGRERR_NONE;
12090 : }
12091 : }
12092 :
12093 20804 : CPLString osCode;
12094 10402 : osCode.Printf("%d", nCode);
12095 : PJ *obj;
12096 10402 : constexpr int FIRST_NON_DEPRECATED_ESRI_CODE = 53001;
12097 10402 : if (nCode < FIRST_NON_DEPRECATED_ESRI_CODE)
12098 : {
12099 10397 : obj = proj_create_from_database(d->getPROJContext(), "EPSG",
12100 : osCode.c_str(), PJ_CATEGORY_CRS, true,
12101 : nullptr);
12102 10397 : if (!obj)
12103 : {
12104 25 : return OGRERR_FAILURE;
12105 : }
12106 : }
12107 : else
12108 : {
12109 : // Likely to be an ESRI CRS...
12110 5 : CPLErr eLastErrorType = CE_None;
12111 5 : CPLErrorNum eLastErrorNum = CPLE_None;
12112 5 : std::string osLastErrorMsg;
12113 5 : bool bIsESRI = false;
12114 : {
12115 10 : CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
12116 5 : CPLErrorReset();
12117 5 : obj = proj_create_from_database(d->getPROJContext(), "EPSG",
12118 : osCode.c_str(), PJ_CATEGORY_CRS,
12119 : true, nullptr);
12120 5 : if (!obj)
12121 : {
12122 2 : eLastErrorType = CPLGetLastErrorType();
12123 2 : eLastErrorNum = CPLGetLastErrorNo();
12124 2 : osLastErrorMsg = CPLGetLastErrorMsg();
12125 2 : obj = proj_create_from_database(d->getPROJContext(), "ESRI",
12126 : osCode.c_str(), PJ_CATEGORY_CRS,
12127 : true, nullptr);
12128 2 : if (obj)
12129 1 : bIsESRI = true;
12130 : }
12131 : }
12132 5 : if (!obj)
12133 : {
12134 1 : if (eLastErrorType != CE_None)
12135 1 : CPLError(eLastErrorType, eLastErrorNum, "%s",
12136 : osLastErrorMsg.c_str());
12137 1 : return OGRERR_FAILURE;
12138 : }
12139 4 : if (bIsESRI)
12140 : {
12141 1 : CPLError(CE_Warning, CPLE_AppDefined,
12142 : "EPSG:%d is not a valid CRS code, but ESRI:%d is. "
12143 : "Assuming ESRI:%d was meant",
12144 : nCode, nCode, nCode);
12145 : }
12146 : }
12147 :
12148 10376 : if (bUseNonDeprecated && proj_is_deprecated(obj))
12149 : {
12150 410 : auto list = proj_get_non_deprecated(d->getPROJContext(), obj);
12151 410 : if (list)
12152 : {
12153 410 : const auto count = proj_list_get_count(list);
12154 410 : if (count == 1)
12155 : {
12156 : auto nonDeprecated =
12157 359 : proj_list_get(d->getPROJContext(), list, 0);
12158 359 : if (nonDeprecated)
12159 : {
12160 359 : if (pszUseNonDeprecated == nullptr)
12161 : {
12162 : const char *pszNewAuth =
12163 359 : proj_get_id_auth_name(nonDeprecated, 0);
12164 : const char *pszNewCode =
12165 359 : proj_get_id_code(nonDeprecated, 0);
12166 359 : CPLError(CE_Warning, CPLE_AppDefined,
12167 : "CRS EPSG:%d is deprecated. "
12168 : "Its non-deprecated replacement %s:%s "
12169 : "will be used instead. "
12170 : "To use the original CRS, set the "
12171 : "OSR_USE_NON_DEPRECATED "
12172 : "configuration option to NO.",
12173 : nCode, pszNewAuth ? pszNewAuth : "(null)",
12174 : pszNewCode ? pszNewCode : "(null)");
12175 : }
12176 359 : proj_destroy(obj);
12177 359 : obj = nonDeprecated;
12178 : }
12179 : }
12180 : }
12181 410 : proj_list_destroy(list);
12182 : }
12183 :
12184 10376 : if (bAddTOWGS84)
12185 : {
12186 1 : auto boundCRS = proj_crs_create_bound_crs_to_WGS84(d->getPROJContext(),
12187 : obj, nullptr);
12188 1 : if (boundCRS)
12189 : {
12190 1 : proj_destroy(obj);
12191 1 : obj = boundCRS;
12192 : }
12193 : }
12194 :
12195 10376 : d->setPjCRS(obj);
12196 :
12197 10376 : if (tlsCache)
12198 : {
12199 10376 : tlsCache->CachePJForEPSGCode(nCode, bUseNonDeprecated, bAddTOWGS84,
12200 : obj);
12201 : }
12202 :
12203 10376 : return OGRERR_NONE;
12204 : }
12205 :
12206 : /************************************************************************/
12207 : /* AddGuessedTOWGS84() */
12208 : /************************************************************************/
12209 :
12210 : /**
12211 : * \brief Try to add a a 3-parameter or 7-parameter Helmert transformation
12212 : * to WGS84.
12213 : *
12214 : * This method try to attach a 3-parameter or 7-parameter Helmert transformation
12215 : * to WGS84 when there is one and only one such method available for the CRS.
12216 : * Note: this is more restrictive to how GDAL < 3 worked.
12217 : *
12218 : * This method is the same as the C function OSRAddGuessedTOWGS84().
12219 : *
12220 : * @return OGRERR_NONE on success, or an error code on failure (the CRS has
12221 : * already a transformation to WGS84 or none matching could be found).
12222 : *
12223 : * @since GDAL 3.0.3
12224 : */
12225 18 : OGRErr OGRSpatialReference::AddGuessedTOWGS84()
12226 : {
12227 36 : TAKE_OPTIONAL_LOCK();
12228 :
12229 18 : d->refreshProjObj();
12230 18 : if (!d->m_pj_crs)
12231 0 : return OGRERR_FAILURE;
12232 18 : auto boundCRS = GDAL_proj_crs_create_bound_crs_to_WGS84(
12233 18 : d->getPROJContext(), d->m_pj_crs, false, true);
12234 18 : if (!boundCRS)
12235 : {
12236 0 : return OGRERR_FAILURE;
12237 : }
12238 18 : d->setPjCRS(boundCRS);
12239 18 : return OGRERR_NONE;
12240 : }
12241 :
12242 : /************************************************************************/
12243 : /* OSRImportFromEPSGA() */
12244 : /************************************************************************/
12245 :
12246 : /**
12247 : * \brief Try to add a a 3-parameter or 7-parameter Helmert transformation
12248 : * to WGS84.
12249 : *
12250 : * This function is the same as OGRSpatialReference::AddGuessedTOWGS84().
12251 : *
12252 : * @since GDAL 3.0.3
12253 : */
12254 :
12255 2 : OGRErr OSRAddGuessedTOWGS84(OGRSpatialReferenceH hSRS)
12256 :
12257 : {
12258 2 : VALIDATE_POINTER1(hSRS, "OSRAddGuessedTOWGS84", OGRERR_FAILURE);
12259 :
12260 2 : return OGRSpatialReference::FromHandle(hSRS)->AddGuessedTOWGS84();
12261 : }
12262 :
12263 : /************************************************************************/
12264 : /* OSRImportFromEPSGA() */
12265 : /************************************************************************/
12266 :
12267 : /**
12268 : * \brief Initialize SRS based on EPSG geographic, projected or vertical CRS
12269 : * code.
12270 : *
12271 : * This function is the same as OGRSpatialReference::importFromEPSGA().
12272 : */
12273 :
12274 3 : OGRErr CPL_STDCALL OSRImportFromEPSGA(OGRSpatialReferenceH hSRS, int nCode)
12275 :
12276 : {
12277 3 : VALIDATE_POINTER1(hSRS, "OSRImportFromEPSGA", OGRERR_FAILURE);
12278 :
12279 3 : return OGRSpatialReference::FromHandle(hSRS)->importFromEPSGA(nCode);
12280 : }
12281 :
12282 : /************************************************************************/
12283 : /* importFromEPSG() */
12284 : /************************************************************************/
12285 :
12286 : /**
12287 : * \brief Initialize SRS based on EPSG geographic, projected or vertical CRS
12288 : * code.
12289 : *
12290 : * This method will initialize the spatial reference based on the
12291 : * passed in EPSG CRS code found in the PROJ database.
12292 : *
12293 : * This method is the same as the C function OSRImportFromEPSG().
12294 : *
12295 : * Before GDAL 3.0.3, this method would try to attach a 3-parameter or
12296 : * 7-parameter Helmert transformation to WGS84 when there is one and only one
12297 : * such method available for the CRS. This behavior might not always be
12298 : * desirable, so starting with GDAL 3.0.3, this is no longer done unless
12299 : * the OSR_ADD_TOWGS84_ON_IMPORT_FROM_EPSG configuration option is set to YES.
12300 : *
12301 : * @param nCode a GCS or PCS code from the horizontal coordinate system table.
12302 : *
12303 : * @return OGRERR_NONE on success, or an error code on failure.
12304 : */
12305 :
12306 43457 : OGRErr OGRSpatialReference::importFromEPSG(int nCode)
12307 :
12308 : {
12309 43457 : return importFromEPSGA(nCode);
12310 : }
12311 :
12312 : /************************************************************************/
12313 : /* OSRImportFromEPSG() */
12314 : /************************************************************************/
12315 :
12316 : /**
12317 : * \brief Initialize SRS based on EPSG geographic, projected or vertical CRS
12318 : * code.
12319 : *
12320 : * This function is the same as OGRSpatialReference::importFromEPSG().
12321 : */
12322 :
12323 1514 : OGRErr CPL_STDCALL OSRImportFromEPSG(OGRSpatialReferenceH hSRS, int nCode)
12324 :
12325 : {
12326 1514 : VALIDATE_POINTER1(hSRS, "OSRImportFromEPSG", OGRERR_FAILURE);
12327 :
12328 1514 : return OGRSpatialReference::FromHandle(hSRS)->importFromEPSG(nCode);
12329 : }
12330 :
12331 : /************************************************************************/
12332 : /* EPSGTreatsAsLatLong() */
12333 : /************************************************************************/
12334 :
12335 : /**
12336 : * \brief This method returns TRUE if this geographic coordinate
12337 : * system should be treated as having lat/long coordinate ordering.
12338 : *
12339 : * Currently this returns TRUE for all geographic coordinate systems
12340 : * with axes set defining it as lat, long (prior to GDAL 3.10, it
12341 : * also checked that the CRS had belonged to EPSG authority, but this check
12342 : * has now been removed).
12343 : *
12344 : * \note Important change of behavior since GDAL 3.0. In previous versions,
12345 : * geographic CRS imported with importFromEPSG() would cause this method to
12346 : * return FALSE on them, whereas now it returns TRUE, since importFromEPSG()
12347 : * is now equivalent to importFromEPSGA().
12348 : *
12349 : * FALSE will be returned for all coordinate systems that are not geographic,
12350 : * or whose axes ordering is not latitude, longitude.
12351 : *
12352 : * This method is the same as the C function OSREPSGTreatsAsLatLong().
12353 : *
12354 : * @return TRUE or FALSE.
12355 : */
12356 :
12357 1295 : int OGRSpatialReference::EPSGTreatsAsLatLong() const
12358 :
12359 : {
12360 2590 : TAKE_OPTIONAL_LOCK();
12361 :
12362 1295 : if (!IsGeographic())
12363 863 : return FALSE;
12364 :
12365 432 : d->demoteFromBoundCRS();
12366 :
12367 432 : bool ret = false;
12368 432 : if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
12369 : {
12370 : auto horizCRS =
12371 17 : proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
12372 17 : if (horizCRS)
12373 : {
12374 : auto cs =
12375 17 : proj_crs_get_coordinate_system(d->getPROJContext(), horizCRS);
12376 17 : if (cs)
12377 : {
12378 17 : const char *pszDirection = nullptr;
12379 17 : if (proj_cs_get_axis_info(d->getPROJContext(), cs, 0, nullptr,
12380 : nullptr, &pszDirection, nullptr,
12381 17 : nullptr, nullptr, nullptr))
12382 : {
12383 17 : if (EQUAL(pszDirection, "north"))
12384 : {
12385 17 : ret = true;
12386 : }
12387 : }
12388 :
12389 17 : proj_destroy(cs);
12390 : }
12391 :
12392 17 : proj_destroy(horizCRS);
12393 : }
12394 : }
12395 : else
12396 : {
12397 : auto cs =
12398 415 : proj_crs_get_coordinate_system(d->getPROJContext(), d->m_pj_crs);
12399 415 : if (cs)
12400 : {
12401 415 : const char *pszDirection = nullptr;
12402 415 : if (proj_cs_get_axis_info(d->getPROJContext(), cs, 0, nullptr,
12403 : nullptr, &pszDirection, nullptr, nullptr,
12404 415 : nullptr, nullptr))
12405 : {
12406 415 : if (EQUAL(pszDirection, "north"))
12407 : {
12408 368 : ret = true;
12409 : }
12410 : }
12411 :
12412 415 : proj_destroy(cs);
12413 : }
12414 : }
12415 432 : d->undoDemoteFromBoundCRS();
12416 :
12417 432 : return ret;
12418 : }
12419 :
12420 : /************************************************************************/
12421 : /* OSREPSGTreatsAsLatLong() */
12422 : /************************************************************************/
12423 :
12424 : /**
12425 : * \brief This function returns TRUE if this geographic coordinate
12426 : * system should be treated as having lat/long coordinate ordering.
12427 : *
12428 : * This function is the same as OGRSpatialReference::OSREPSGTreatsAsLatLong().
12429 : */
12430 :
12431 208 : int OSREPSGTreatsAsLatLong(OGRSpatialReferenceH hSRS)
12432 :
12433 : {
12434 208 : VALIDATE_POINTER1(hSRS, "OSREPSGTreatsAsLatLong", OGRERR_FAILURE);
12435 :
12436 208 : return OGRSpatialReference::FromHandle(hSRS)->EPSGTreatsAsLatLong();
12437 : }
12438 :
12439 : /************************************************************************/
12440 : /* EPSGTreatsAsNorthingEasting() */
12441 : /************************************************************************/
12442 :
12443 : /**
12444 : * \brief This method returns TRUE if this projected coordinate
12445 : * system should be treated as having northing/easting coordinate ordering.
12446 : *
12447 : * Currently this returns TRUE for all projected coordinate systems
12448 : * with axes set defining it as northing, easting (prior to GDAL 3.10, it
12449 : * also checked that the CRS had belonged to EPSG authority, but this check
12450 : * has now been removed).
12451 : *
12452 : * \note Important change of behavior since GDAL 3.0. In previous versions,
12453 : * projected CRS with northing, easting axis order imported with
12454 : * importFromEPSG() would cause this method to
12455 : * return FALSE on them, whereas now it returns TRUE, since importFromEPSG()
12456 : * is now equivalent to importFromEPSGA().
12457 : *
12458 : * FALSE will be returned for all coordinate systems that are not projected,
12459 : * or whose axes ordering is not northing, easting.
12460 : *
12461 : * This method is the same as the C function EPSGTreatsAsNorthingEasting().
12462 : *
12463 : * @return TRUE or FALSE.
12464 : *
12465 : */
12466 :
12467 950 : int OGRSpatialReference::EPSGTreatsAsNorthingEasting() const
12468 :
12469 : {
12470 1900 : TAKE_OPTIONAL_LOCK();
12471 :
12472 950 : if (!IsProjected())
12473 49 : return FALSE;
12474 :
12475 901 : d->demoteFromBoundCRS();
12476 : PJ *projCRS;
12477 901 : const auto ctxt = d->getPROJContext();
12478 901 : if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
12479 : {
12480 4 : projCRS = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
12481 4 : if (!projCRS || proj_get_type(projCRS) != PJ_TYPE_PROJECTED_CRS)
12482 : {
12483 0 : d->undoDemoteFromBoundCRS();
12484 0 : proj_destroy(projCRS);
12485 0 : return FALSE;
12486 : }
12487 : }
12488 : else
12489 : {
12490 897 : projCRS = proj_clone(ctxt, d->m_pj_crs);
12491 : }
12492 :
12493 901 : bool ret = false;
12494 901 : auto cs = proj_crs_get_coordinate_system(ctxt, projCRS);
12495 901 : proj_destroy(projCRS);
12496 901 : d->undoDemoteFromBoundCRS();
12497 :
12498 901 : if (cs)
12499 : {
12500 901 : ret = isNorthEastAxisOrder(ctxt, cs);
12501 901 : proj_destroy(cs);
12502 : }
12503 :
12504 901 : return ret;
12505 : }
12506 :
12507 : /************************************************************************/
12508 : /* OSREPSGTreatsAsNorthingEasting() */
12509 : /************************************************************************/
12510 :
12511 : /**
12512 : * \brief This function returns TRUE if this projected coordinate
12513 : * system should be treated as having northing/easting coordinate ordering.
12514 : *
12515 : * This function is the same as
12516 : * OGRSpatialReference::EPSGTreatsAsNorthingEasting().
12517 : *
12518 : */
12519 :
12520 215 : int OSREPSGTreatsAsNorthingEasting(OGRSpatialReferenceH hSRS)
12521 :
12522 : {
12523 215 : VALIDATE_POINTER1(hSRS, "OSREPSGTreatsAsNorthingEasting", OGRERR_FAILURE);
12524 :
12525 215 : return OGRSpatialReference::FromHandle(hSRS)->EPSGTreatsAsNorthingEasting();
12526 : }
12527 :
12528 : /************************************************************************/
12529 : /* ImportFromESRIWisconsinWKT() */
12530 : /* */
12531 : /* Search a ESRI State Plane WKT and import it. */
12532 : /************************************************************************/
12533 :
12534 : // This is only used by the HFA driver and somewhat dubious we really need that
12535 : // Coming from an old ESRI merge
12536 :
12537 1 : OGRErr OGRSpatialReference::ImportFromESRIWisconsinWKT(const char *prjName,
12538 : double centralMeridian,
12539 : double latOfOrigin,
12540 : const char *unitsName,
12541 : const char *crsName)
12542 : {
12543 2 : TAKE_OPTIONAL_LOCK();
12544 :
12545 1 : if (centralMeridian < -93 || centralMeridian > -87)
12546 0 : return OGRERR_FAILURE;
12547 1 : if (latOfOrigin < 40 || latOfOrigin > 47)
12548 0 : return OGRERR_FAILURE;
12549 :
12550 : // If the CS name is known.
12551 1 : if (!prjName && !unitsName && crsName)
12552 : {
12553 0 : const PJ_TYPE type = PJ_TYPE_PROJECTED_CRS;
12554 0 : PJ_OBJ_LIST *list = proj_create_from_name(
12555 : d->getPROJContext(), "ESRI", crsName, &type, 1, false, 1, nullptr);
12556 0 : if (list)
12557 : {
12558 0 : if (proj_list_get_count(list) == 1)
12559 : {
12560 0 : auto crs = proj_list_get(d->getPROJContext(), list, 0);
12561 0 : if (crs)
12562 : {
12563 0 : Clear();
12564 0 : d->setPjCRS(crs);
12565 0 : proj_list_destroy(list);
12566 0 : return OGRERR_NONE;
12567 : }
12568 : }
12569 0 : proj_list_destroy(list);
12570 : }
12571 0 : return OGRERR_FAILURE;
12572 : }
12573 :
12574 1 : if (prjName == nullptr || unitsName == nullptr)
12575 : {
12576 0 : return OGRERR_FAILURE;
12577 : }
12578 :
12579 1 : const PJ_TYPE type = PJ_TYPE_PROJECTED_CRS;
12580 1 : PJ_OBJ_LIST *list = proj_create_from_name(d->getPROJContext(), "ESRI",
12581 : "NAD_1983_HARN_WISCRS_", &type, 1,
12582 : true, 0, nullptr);
12583 1 : if (list)
12584 : {
12585 1 : const auto listSize = proj_list_get_count(list);
12586 8 : for (int i = 0; i < listSize; i++)
12587 : {
12588 8 : auto crs = proj_list_get(d->getPROJContext(), list, i);
12589 8 : if (!crs)
12590 : {
12591 7 : continue;
12592 : }
12593 :
12594 8 : auto conv = proj_crs_get_coordoperation(d->getPROJContext(), crs);
12595 8 : if (!conv)
12596 : {
12597 0 : proj_destroy(crs);
12598 0 : continue;
12599 : }
12600 8 : const char *pszMethodCode = nullptr;
12601 8 : proj_coordoperation_get_method_info(
12602 : d->getPROJContext(), conv, nullptr, nullptr, &pszMethodCode);
12603 8 : const int nMethodCode = atoi(pszMethodCode ? pszMethodCode : "0");
12604 8 : if (!((EQUAL(prjName, SRS_PT_TRANSVERSE_MERCATOR) &&
12605 : nMethodCode == EPSG_CODE_METHOD_TRANSVERSE_MERCATOR) ||
12606 3 : (EQUAL(prjName, "Lambert_Conformal_Conic") &&
12607 : nMethodCode ==
12608 : EPSG_CODE_METHOD_LAMBERT_CONIC_CONFORMAL_1SP)))
12609 : {
12610 3 : proj_destroy(crs);
12611 3 : proj_destroy(conv);
12612 3 : continue;
12613 : }
12614 :
12615 : auto coordSys =
12616 5 : proj_crs_get_coordinate_system(d->getPROJContext(), crs);
12617 5 : if (!coordSys)
12618 : {
12619 0 : proj_destroy(crs);
12620 0 : proj_destroy(conv);
12621 0 : continue;
12622 : }
12623 :
12624 5 : double dfConvFactor = 0.0;
12625 5 : proj_cs_get_axis_info(d->getPROJContext(), coordSys, 0, nullptr,
12626 : nullptr, nullptr, &dfConvFactor, nullptr,
12627 : nullptr, nullptr);
12628 5 : proj_destroy(coordSys);
12629 :
12630 6 : if ((EQUAL(unitsName, "meters") && dfConvFactor != 1.0) ||
12631 1 : (!EQUAL(unitsName, "meters") &&
12632 0 : std::fabs(dfConvFactor - CPLAtof(SRS_UL_US_FOOT_CONV)) >
12633 : 1e-10))
12634 : {
12635 4 : proj_destroy(crs);
12636 4 : proj_destroy(conv);
12637 4 : continue;
12638 : }
12639 :
12640 1 : int idx_lat = proj_coordoperation_get_param_index(
12641 : d->getPROJContext(), conv,
12642 : EPSG_NAME_PARAMETER_LATITUDE_OF_NATURAL_ORIGIN);
12643 1 : double valueLat = -1000;
12644 1 : proj_coordoperation_get_param(d->getPROJContext(), conv, idx_lat,
12645 : nullptr, nullptr, nullptr, &valueLat,
12646 : nullptr, nullptr, nullptr, nullptr,
12647 : nullptr, nullptr);
12648 1 : int idx_lon = proj_coordoperation_get_param_index(
12649 : d->getPROJContext(), conv,
12650 : EPSG_NAME_PARAMETER_LONGITUDE_OF_NATURAL_ORIGIN);
12651 1 : double valueLong = -1000;
12652 1 : proj_coordoperation_get_param(d->getPROJContext(), conv, idx_lon,
12653 : nullptr, nullptr, nullptr, &valueLong,
12654 : nullptr, nullptr, nullptr, nullptr,
12655 : nullptr, nullptr);
12656 1 : if (std::fabs(centralMeridian - valueLong) <= 1e-10 &&
12657 1 : std::fabs(latOfOrigin - valueLat) <= 1e-10)
12658 : {
12659 1 : Clear();
12660 1 : d->setPjCRS(crs);
12661 1 : proj_list_destroy(list);
12662 1 : proj_destroy(conv);
12663 1 : return OGRERR_NONE;
12664 : }
12665 :
12666 0 : proj_destroy(crs);
12667 0 : proj_destroy(conv);
12668 : }
12669 0 : proj_list_destroy(list);
12670 : }
12671 :
12672 0 : return OGRERR_FAILURE;
12673 : }
12674 :
12675 : /************************************************************************/
12676 : /* GetAxisMappingStrategy() */
12677 : /************************************************************************/
12678 :
12679 : /** \brief Return the data axis to CRS axis mapping strategy.
12680 : *
12681 : * <ul>
12682 : * <li>OAMS_TRADITIONAL_GIS_ORDER means that for geographic CRS with
12683 : * lat/long order, the data will still be long/lat ordered. Similarly for
12684 : * a projected CRS with northing/easting order, the data will still be
12685 : * easting/northing ordered.
12686 : * <li>OAMS_AUTHORITY_COMPLIANT means that the data axis will be identical to
12687 : * the CRS axis.
12688 : * <li>OAMS_CUSTOM means that the data axis are customly defined with
12689 : * SetDataAxisToSRSAxisMapping()
12690 : * </ul>
12691 : * @return the data axis to CRS axis mapping strategy.
12692 : * @since GDAL 3.0
12693 : */
12694 95 : OSRAxisMappingStrategy OGRSpatialReference::GetAxisMappingStrategy() const
12695 : {
12696 95 : TAKE_OPTIONAL_LOCK();
12697 :
12698 190 : return d->m_axisMappingStrategy;
12699 : }
12700 :
12701 : /************************************************************************/
12702 : /* OSRGetAxisMappingStrategy() */
12703 : /************************************************************************/
12704 :
12705 : /** \brief Return the data axis to CRS axis mapping strategy.
12706 : *
12707 : * See OGRSpatialReference::GetAxisMappingStrategy()
12708 : * @since GDAL 3.0
12709 : */
12710 37 : OSRAxisMappingStrategy OSRGetAxisMappingStrategy(OGRSpatialReferenceH hSRS)
12711 : {
12712 37 : VALIDATE_POINTER1(hSRS, "OSRGetAxisMappingStrategy", OAMS_CUSTOM);
12713 :
12714 37 : return OGRSpatialReference::FromHandle(hSRS)->GetAxisMappingStrategy();
12715 : }
12716 :
12717 : /************************************************************************/
12718 : /* SetAxisMappingStrategy() */
12719 : /************************************************************************/
12720 :
12721 : /** \brief Set the data axis to CRS axis mapping strategy.
12722 : *
12723 : * Starting with GDAL 3.5, the OSR_DEFAULT_AXIS_MAPPING_STRATEGY configuration
12724 : * option can be set to "TRADITIONAL_GIS_ORDER" / "AUTHORITY_COMPLIANT" (the
12725 : * later being the default value when the option is not set) to control the
12726 : * value of the data axis to CRS axis mapping strategy when a
12727 : * OSRSpatialReference object is created. Calling SetAxisMappingStrategy() will
12728 : * override this default value.
12729 : *
12730 : * See OGRSpatialReference::GetAxisMappingStrategy()
12731 : * @since GDAL 3.0
12732 : */
12733 95421 : void OGRSpatialReference::SetAxisMappingStrategy(
12734 : OSRAxisMappingStrategy strategy)
12735 : {
12736 190842 : TAKE_OPTIONAL_LOCK();
12737 :
12738 95421 : d->m_axisMappingStrategy = strategy;
12739 95421 : d->refreshAxisMapping();
12740 95421 : }
12741 :
12742 : /************************************************************************/
12743 : /* OSRSetAxisMappingStrategy() */
12744 : /************************************************************************/
12745 :
12746 : /** \brief Set the data axis to CRS axis mapping strategy.
12747 : *
12748 : * See OGRSpatialReference::SetAxisMappingStrategy()
12749 : * @since GDAL 3.0
12750 : */
12751 814 : void OSRSetAxisMappingStrategy(OGRSpatialReferenceH hSRS,
12752 : OSRAxisMappingStrategy strategy)
12753 : {
12754 814 : VALIDATE_POINTER0(hSRS, "OSRSetAxisMappingStrategy");
12755 :
12756 814 : OGRSpatialReference::FromHandle(hSRS)->SetAxisMappingStrategy(strategy);
12757 : }
12758 :
12759 : /************************************************************************/
12760 : /* GetDataAxisToSRSAxisMapping() */
12761 : /************************************************************************/
12762 :
12763 : /** \brief Return the data axis to SRS axis mapping.
12764 : *
12765 : * The number of elements of the vector will be the number of axis of the CRS.
12766 : * Values start at 1.
12767 : *
12768 : * If m = GetDataAxisToSRSAxisMapping(), then m[0] is the data axis number
12769 : * for the first axis of the CRS.
12770 : *
12771 : * @since GDAL 3.0
12772 : */
12773 17398900 : const std::vector<int> &OGRSpatialReference::GetDataAxisToSRSAxisMapping() const
12774 : {
12775 17398900 : TAKE_OPTIONAL_LOCK();
12776 :
12777 34797900 : return d->m_axisMapping;
12778 : }
12779 :
12780 : /************************************************************************/
12781 : /* OSRGetDataAxisToSRSAxisMapping() */
12782 : /************************************************************************/
12783 :
12784 : /** \brief Return the data axis to SRS axis mapping.
12785 : *
12786 : * See OGRSpatialReference::GetDataAxisToSRSAxisMapping()
12787 : *
12788 : * @since GDAL 3.0
12789 : */
12790 105 : const int *OSRGetDataAxisToSRSAxisMapping(OGRSpatialReferenceH hSRS,
12791 : int *pnCount)
12792 : {
12793 105 : VALIDATE_POINTER1(hSRS, "OSRGetDataAxisToSRSAxisMapping", nullptr);
12794 105 : VALIDATE_POINTER1(pnCount, "OSRGetDataAxisToSRSAxisMapping", nullptr);
12795 :
12796 : const auto &v =
12797 105 : OGRSpatialReference::FromHandle(hSRS)->GetDataAxisToSRSAxisMapping();
12798 105 : *pnCount = static_cast<int>(v.size());
12799 105 : return v.data();
12800 : }
12801 :
12802 : /************************************************************************/
12803 : /* SetDataAxisToSRSAxisMapping() */
12804 : /************************************************************************/
12805 :
12806 : /** \brief Set a custom data axis to CRS axis mapping.
12807 : *
12808 : * The number of elements of the mapping vector should be the number of axis
12809 : * of the CRS (as returned by GetAxesCount()) (although this method does not
12810 : * check that, beyond checking there are at least 2 elements, so that this
12811 : * method and setting the CRS can be done in any order).
12812 : * This is taken into account by OGRCoordinateTransformation to transform the
12813 : * order of coordinates to the order expected by the CRS before
12814 : * transformation, and back to the data order after transformation.
12815 : *
12816 : * The mapping[i] value (one based) represents the data axis number for the i(th)
12817 : * axis of the CRS. A negative value can also be used to ask for a sign
12818 : * reversal during coordinate transformation (to deal with northing vs southing,
12819 : * easting vs westing, heights vs depths).
12820 : *
12821 : * When used with OGRCoordinateTransformation,
12822 : * - the only valid values for mapping[0] (data axis number for the first axis
12823 : * of the CRS) are 1, 2, -1, -2.
12824 : * - the only valid values for mapping[1] (data axis number for the second axis
12825 : * of the CRS) are 1, 2, -1, -2.
12826 : * - the only valid values mapping[2] are 3 or -3.
12827 : * Note: this method does not validate the values of mapping[].
12828 : *
12829 : * mapping=[2,1] typically expresses the inversion of axis between the data
12830 : * axis and the CRS axis for a 2D CRS.
12831 : *
12832 : * Automatically implies SetAxisMappingStrategy(OAMS_CUSTOM)
12833 : *
12834 : * This is the same as the C function OSRSetDataAxisToSRSAxisMapping().
12835 : *
12836 : * @param mapping The new data axis to CRS axis mapping.
12837 : *
12838 : * @since GDAL 3.0
12839 : * @see OGRSpatialReference::GetDataAxisToSRSAxisMapping()
12840 : */
12841 10477 : OGRErr OGRSpatialReference::SetDataAxisToSRSAxisMapping(
12842 : const std::vector<int> &mapping)
12843 : {
12844 20954 : TAKE_OPTIONAL_LOCK();
12845 :
12846 10477 : if (mapping.size() < 2)
12847 0 : return OGRERR_FAILURE;
12848 10477 : d->m_axisMappingStrategy = OAMS_CUSTOM;
12849 10477 : d->m_axisMapping = mapping;
12850 10477 : return OGRERR_NONE;
12851 : }
12852 :
12853 : /************************************************************************/
12854 : /* OSRSetDataAxisToSRSAxisMapping() */
12855 : /************************************************************************/
12856 :
12857 : /** \brief Set a custom data axis to CRS axis mapping.
12858 : *
12859 : * Automatically implies SetAxisMappingStrategy(OAMS_CUSTOM)
12860 : *
12861 : * This is the same as the C++ method
12862 : * OGRSpatialReference::SetDataAxisToSRSAxisMapping()
12863 : *
12864 : * @since GDAL 3.1
12865 : */
12866 15 : OGRErr OSRSetDataAxisToSRSAxisMapping(OGRSpatialReferenceH hSRS,
12867 : int nMappingSize, const int *panMapping)
12868 : {
12869 15 : VALIDATE_POINTER1(hSRS, "OSRSetDataAxisToSRSAxisMapping", OGRERR_FAILURE);
12870 15 : VALIDATE_POINTER1(panMapping, "OSRSetDataAxisToSRSAxisMapping",
12871 : OGRERR_FAILURE);
12872 :
12873 15 : if (nMappingSize < 0)
12874 0 : return OGRERR_FAILURE;
12875 :
12876 30 : std::vector<int> mapping(nMappingSize);
12877 15 : if (nMappingSize)
12878 15 : memcpy(&mapping[0], panMapping, nMappingSize * sizeof(int));
12879 15 : return OGRSpatialReference::FromHandle(hSRS)->SetDataAxisToSRSAxisMapping(
12880 15 : mapping);
12881 : }
12882 :
12883 : /************************************************************************/
12884 : /* GetAreaOfUse() */
12885 : /************************************************************************/
12886 :
12887 : /** \brief Return the area of use of the CRS.
12888 : *
12889 : * This method is the same as the OSRGetAreaOfUse() function.
12890 : *
12891 : * @param pdfWestLongitudeDeg Pointer to a double to receive the western-most
12892 : * longitude, expressed in degree. Might be NULL. If the returned value is
12893 : * -1000, the bounding box is unknown.
12894 : * @param pdfSouthLatitudeDeg Pointer to a double to receive the southern-most
12895 : * latitude, expressed in degree. Might be NULL. If the returned value is -1000,
12896 : * the bounding box is unknown.
12897 : * @param pdfEastLongitudeDeg Pointer to a double to receive the eastern-most
12898 : * longitude, expressed in degree. Might be NULL. If the returned value is
12899 : * -1000, the bounding box is unknown.
12900 : * @param pdfNorthLatitudeDeg Pointer to a double to receive the northern-most
12901 : * latitude, expressed in degree. Might be NULL. If the returned value is -1000,
12902 : * the bounding box is unknown.
12903 : * @param ppszAreaName Pointer to a string to receive the name of the area of
12904 : * use. Might be NULL. Note that *ppszAreaName is short-lived and might be
12905 : * invalidated by further calls.
12906 : * @return true in case of success
12907 : * @since GDAL 3.0
12908 : */
12909 81 : bool OGRSpatialReference::GetAreaOfUse(double *pdfWestLongitudeDeg,
12910 : double *pdfSouthLatitudeDeg,
12911 : double *pdfEastLongitudeDeg,
12912 : double *pdfNorthLatitudeDeg,
12913 : const char **ppszAreaName) const
12914 : {
12915 162 : TAKE_OPTIONAL_LOCK();
12916 :
12917 81 : d->refreshProjObj();
12918 81 : if (!d->m_pj_crs)
12919 : {
12920 0 : return false;
12921 : }
12922 81 : d->demoteFromBoundCRS();
12923 81 : const char *pszAreaName = nullptr;
12924 81 : int bSuccess = proj_get_area_of_use(
12925 81 : d->getPROJContext(), d->m_pj_crs, pdfWestLongitudeDeg,
12926 : pdfSouthLatitudeDeg, pdfEastLongitudeDeg, pdfNorthLatitudeDeg,
12927 : &pszAreaName);
12928 81 : d->undoDemoteFromBoundCRS();
12929 81 : d->m_osAreaName = pszAreaName ? pszAreaName : "";
12930 81 : if (ppszAreaName)
12931 27 : *ppszAreaName = d->m_osAreaName.c_str();
12932 81 : return CPL_TO_BOOL(bSuccess);
12933 : }
12934 :
12935 : /************************************************************************/
12936 : /* GetAreaOfUse() */
12937 : /************************************************************************/
12938 :
12939 : /** \brief Return the area of use of the CRS.
12940 : *
12941 : * This function is the same as the OGRSpatialReference::GetAreaOfUse() method.
12942 : *
12943 : * @since GDAL 3.0
12944 : */
12945 1 : int OSRGetAreaOfUse(OGRSpatialReferenceH hSRS, double *pdfWestLongitudeDeg,
12946 : double *pdfSouthLatitudeDeg, double *pdfEastLongitudeDeg,
12947 : double *pdfNorthLatitudeDeg, const char **ppszAreaName)
12948 : {
12949 1 : VALIDATE_POINTER1(hSRS, "OSRGetAreaOfUse", FALSE);
12950 :
12951 1 : return OGRSpatialReference::FromHandle(hSRS)->GetAreaOfUse(
12952 : pdfWestLongitudeDeg, pdfSouthLatitudeDeg, pdfEastLongitudeDeg,
12953 1 : pdfNorthLatitudeDeg, ppszAreaName);
12954 : }
12955 :
12956 : /************************************************************************/
12957 : /* OSRGetCRSInfoListFromDatabase() */
12958 : /************************************************************************/
12959 :
12960 : /** \brief Enumerate CRS objects from the database.
12961 : *
12962 : * The returned object is an array of OSRCRSInfo* pointers, whose last
12963 : * entry is NULL. This array should be freed with OSRDestroyCRSInfoList()
12964 : *
12965 : * @param pszAuthName Authority name, used to restrict the search.
12966 : * Or NULL for all authorities.
12967 : * @param params Additional criteria. Must be set to NULL for now.
12968 : * @param pnOutResultCount Output parameter pointing to an integer to receive
12969 : * the size of the result list. Might be NULL
12970 : * @return an array of OSRCRSInfo* pointers to be freed with
12971 : * OSRDestroyCRSInfoList(), or NULL in case of error.
12972 : *
12973 : * @since GDAL 3.0
12974 : */
12975 : OSRCRSInfo **
12976 28 : OSRGetCRSInfoListFromDatabase(const char *pszAuthName,
12977 : CPL_UNUSED const OSRCRSListParameters *params,
12978 : int *pnOutResultCount)
12979 : {
12980 28 : int nResultCount = 0;
12981 28 : auto projList = proj_get_crs_info_list_from_database(
12982 : OSRGetProjTLSContext(), pszAuthName, nullptr, &nResultCount);
12983 28 : if (pnOutResultCount)
12984 28 : *pnOutResultCount = nResultCount;
12985 28 : if (!projList)
12986 : {
12987 0 : return nullptr;
12988 : }
12989 28 : auto res = new OSRCRSInfo *[nResultCount + 1];
12990 92468 : for (int i = 0; i < nResultCount; i++)
12991 : {
12992 92440 : res[i] = new OSRCRSInfo;
12993 184880 : res[i]->pszAuthName = projList[i]->auth_name
12994 92440 : ? CPLStrdup(projList[i]->auth_name)
12995 : : nullptr;
12996 92440 : res[i]->pszCode =
12997 92440 : projList[i]->code ? CPLStrdup(projList[i]->code) : nullptr;
12998 92440 : res[i]->pszName =
12999 92440 : projList[i]->name ? CPLStrdup(projList[i]->name) : nullptr;
13000 92440 : res[i]->eType = OSR_CRS_TYPE_OTHER;
13001 92440 : switch (projList[i]->type)
13002 : {
13003 9272 : case PJ_TYPE_GEOGRAPHIC_2D_CRS:
13004 9272 : res[i]->eType = OSR_CRS_TYPE_GEOGRAPHIC_2D;
13005 9272 : break;
13006 2864 : case PJ_TYPE_GEOGRAPHIC_3D_CRS:
13007 2864 : res[i]->eType = OSR_CRS_TYPE_GEOGRAPHIC_3D;
13008 2864 : break;
13009 3200 : case PJ_TYPE_GEOCENTRIC_CRS:
13010 3200 : res[i]->eType = OSR_CRS_TYPE_GEOCENTRIC;
13011 3200 : break;
13012 70012 : case PJ_TYPE_PROJECTED_CRS:
13013 70012 : res[i]->eType = OSR_CRS_TYPE_PROJECTED;
13014 70012 : break;
13015 2860 : case PJ_TYPE_VERTICAL_CRS:
13016 2860 : res[i]->eType = OSR_CRS_TYPE_VERTICAL;
13017 2860 : break;
13018 4232 : case PJ_TYPE_COMPOUND_CRS:
13019 4232 : res[i]->eType = OSR_CRS_TYPE_COMPOUND;
13020 4232 : break;
13021 0 : default:
13022 0 : break;
13023 : }
13024 92440 : res[i]->bDeprecated = projList[i]->deprecated;
13025 92440 : res[i]->bBboxValid = projList[i]->bbox_valid;
13026 92440 : res[i]->dfWestLongitudeDeg = projList[i]->west_lon_degree;
13027 92440 : res[i]->dfSouthLatitudeDeg = projList[i]->south_lat_degree;
13028 92440 : res[i]->dfEastLongitudeDeg = projList[i]->east_lon_degree;
13029 92440 : res[i]->dfNorthLatitudeDeg = projList[i]->north_lat_degree;
13030 184880 : res[i]->pszAreaName = projList[i]->area_name
13031 92440 : ? CPLStrdup(projList[i]->area_name)
13032 : : nullptr;
13033 92440 : res[i]->pszProjectionMethod =
13034 92440 : projList[i]->projection_method_name
13035 92440 : ? CPLStrdup(projList[i]->projection_method_name)
13036 : : nullptr;
13037 : #if PROJ_AT_LEAST_VERSION(8, 1, 0)
13038 : res[i]->pszCelestialBodyName =
13039 : projList[i]->celestial_body_name
13040 : ? CPLStrdup(projList[i]->celestial_body_name)
13041 : : nullptr;
13042 : #else
13043 92440 : res[i]->pszCelestialBodyName =
13044 92440 : res[i]->pszAuthName && EQUAL(res[i]->pszAuthName, "EPSG")
13045 184880 : ? CPLStrdup("Earth")
13046 : : nullptr;
13047 : #endif
13048 : }
13049 28 : res[nResultCount] = nullptr;
13050 28 : proj_crs_info_list_destroy(projList);
13051 28 : return res;
13052 : }
13053 :
13054 : /************************************************************************/
13055 : /* OSRDestroyCRSInfoList() */
13056 : /************************************************************************/
13057 :
13058 : /** \brief Destroy the result returned by
13059 : * OSRGetCRSInfoListFromDatabase().
13060 : *
13061 : * @since GDAL 3.0
13062 : */
13063 28 : void OSRDestroyCRSInfoList(OSRCRSInfo **list)
13064 : {
13065 28 : if (list)
13066 : {
13067 92468 : for (int i = 0; list[i] != nullptr; i++)
13068 : {
13069 92440 : CPLFree(list[i]->pszAuthName);
13070 92440 : CPLFree(list[i]->pszCode);
13071 92440 : CPLFree(list[i]->pszName);
13072 92440 : CPLFree(list[i]->pszAreaName);
13073 92440 : CPLFree(list[i]->pszProjectionMethod);
13074 92440 : CPLFree(list[i]->pszCelestialBodyName);
13075 92440 : delete list[i];
13076 : }
13077 28 : delete[] list;
13078 : }
13079 28 : }
13080 :
13081 : /************************************************************************/
13082 : /* OSRGetAuthorityListFromDatabase() */
13083 : /************************************************************************/
13084 :
13085 : /** \brief Return the list of CRS authorities used in the PROJ database.
13086 : *
13087 : * Such as "EPSG", "ESRI", "PROJ", "IGNF", "IAU_2015", etc.
13088 : *
13089 : * This is a direct mapping of https://proj.org/en/latest/development/reference/functions.html#c.proj_get_authorities_from_database
13090 : *
13091 : * @return nullptr in case of error, or a NULL terminated list of strings to
13092 : * free with CSLDestroy()
13093 : * @since GDAL 3.10
13094 : */
13095 5 : char **OSRGetAuthorityListFromDatabase()
13096 : {
13097 : PROJ_STRING_LIST list =
13098 5 : proj_get_authorities_from_database(OSRGetProjTLSContext());
13099 5 : if (!list)
13100 : {
13101 0 : return nullptr;
13102 : }
13103 5 : int count = 0;
13104 30 : while (list[count])
13105 25 : ++count;
13106 5 : char **res = static_cast<char **>(CPLCalloc(count + 1, sizeof(char *)));
13107 30 : for (int i = 0; i < count; ++i)
13108 25 : res[i] = CPLStrdup(list[i]);
13109 5 : proj_string_list_destroy(list);
13110 5 : return res;
13111 : }
13112 :
13113 : /************************************************************************/
13114 : /* UpdateCoordinateSystemFromGeogCRS() */
13115 : /************************************************************************/
13116 :
13117 : /*! @cond Doxygen_Suppress */
13118 : /** \brief Used by gt_wkt_srs.cpp to create projected 3D CRS. Internal use only
13119 : *
13120 : * @since GDAL 3.1
13121 : */
13122 1 : void OGRSpatialReference::UpdateCoordinateSystemFromGeogCRS()
13123 : {
13124 1 : TAKE_OPTIONAL_LOCK();
13125 :
13126 1 : d->refreshProjObj();
13127 1 : if (!d->m_pj_crs)
13128 0 : return;
13129 1 : if (d->m_pjType != PJ_TYPE_PROJECTED_CRS)
13130 0 : return;
13131 1 : if (GetAxesCount() == 3)
13132 0 : return;
13133 1 : auto ctxt = d->getPROJContext();
13134 1 : auto baseCRS = proj_crs_get_geodetic_crs(ctxt, d->m_pj_crs);
13135 1 : if (!baseCRS)
13136 0 : return;
13137 1 : auto baseCRSCS = proj_crs_get_coordinate_system(ctxt, baseCRS);
13138 1 : if (!baseCRSCS)
13139 : {
13140 0 : proj_destroy(baseCRS);
13141 0 : return;
13142 : }
13143 1 : if (proj_cs_get_axis_count(ctxt, baseCRSCS) != 3)
13144 : {
13145 0 : proj_destroy(baseCRSCS);
13146 0 : proj_destroy(baseCRS);
13147 0 : return;
13148 : }
13149 1 : auto projCS = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
13150 1 : if (!projCS || proj_cs_get_axis_count(ctxt, projCS) != 2)
13151 : {
13152 0 : proj_destroy(baseCRSCS);
13153 0 : proj_destroy(baseCRS);
13154 0 : proj_destroy(projCS);
13155 0 : return;
13156 : }
13157 :
13158 : PJ_AXIS_DESCRIPTION axis[3];
13159 4 : for (int i = 0; i < 3; i++)
13160 : {
13161 3 : const char *name = nullptr;
13162 3 : const char *abbreviation = nullptr;
13163 3 : const char *direction = nullptr;
13164 3 : double unit_conv_factor = 0;
13165 3 : const char *unit_name = nullptr;
13166 3 : proj_cs_get_axis_info(ctxt, i < 2 ? projCS : baseCRSCS, i, &name,
13167 : &abbreviation, &direction, &unit_conv_factor,
13168 : &unit_name, nullptr, nullptr);
13169 3 : axis[i].name = CPLStrdup(name);
13170 3 : axis[i].abbreviation = CPLStrdup(abbreviation);
13171 3 : axis[i].direction = CPLStrdup(direction);
13172 3 : axis[i].unit_name = CPLStrdup(unit_name);
13173 3 : axis[i].unit_conv_factor = unit_conv_factor;
13174 3 : axis[i].unit_type = PJ_UT_LINEAR;
13175 : }
13176 1 : proj_destroy(baseCRSCS);
13177 1 : proj_destroy(projCS);
13178 1 : auto cs = proj_create_cs(ctxt, PJ_CS_TYPE_CARTESIAN, 3, axis);
13179 4 : for (int i = 0; i < 3; i++)
13180 : {
13181 3 : CPLFree(axis[i].name);
13182 3 : CPLFree(axis[i].abbreviation);
13183 3 : CPLFree(axis[i].direction);
13184 3 : CPLFree(axis[i].unit_name);
13185 : }
13186 1 : if (!cs)
13187 : {
13188 0 : proj_destroy(baseCRS);
13189 0 : return;
13190 : }
13191 1 : auto conversion = proj_crs_get_coordoperation(ctxt, d->m_pj_crs);
13192 1 : auto crs = proj_create_projected_crs(ctxt, d->getProjCRSName(), baseCRS,
13193 : conversion, cs);
13194 1 : proj_destroy(baseCRS);
13195 1 : proj_destroy(conversion);
13196 1 : proj_destroy(cs);
13197 1 : d->setPjCRS(crs);
13198 : }
13199 :
13200 : /*! @endcond */
13201 :
13202 : /************************************************************************/
13203 : /* PromoteTo3D() */
13204 : /************************************************************************/
13205 :
13206 : /** \brief "Promotes" a 2D CRS to a 3D CRS one.
13207 : *
13208 : * The new axis will be ellipsoidal height, oriented upwards, and with metre
13209 : * units.
13210 : *
13211 : * @param pszName New name for the CRS. If set to NULL, the previous name will
13212 : * be used.
13213 : * @return OGRERR_NONE if no error occurred.
13214 : * @since GDAL 3.1 and PROJ 6.3
13215 : */
13216 44 : OGRErr OGRSpatialReference::PromoteTo3D(const char *pszName)
13217 : {
13218 88 : TAKE_OPTIONAL_LOCK();
13219 :
13220 44 : d->refreshProjObj();
13221 44 : if (!d->m_pj_crs)
13222 0 : return OGRERR_FAILURE;
13223 : auto newPj =
13224 44 : proj_crs_promote_to_3D(d->getPROJContext(), pszName, d->m_pj_crs);
13225 44 : if (!newPj)
13226 0 : return OGRERR_FAILURE;
13227 44 : d->setPjCRS(newPj);
13228 44 : return OGRERR_NONE;
13229 : }
13230 :
13231 : /************************************************************************/
13232 : /* OSRPromoteTo3D() */
13233 : /************************************************************************/
13234 :
13235 : /** \brief "Promotes" a 2D CRS to a 3D CRS one.
13236 : *
13237 : * See OGRSpatialReference::PromoteTo3D()
13238 : *
13239 : * @since GDAL 3.1 and PROJ 6.3
13240 : */
13241 3 : OGRErr OSRPromoteTo3D(OGRSpatialReferenceH hSRS, const char *pszName)
13242 : {
13243 3 : VALIDATE_POINTER1(hSRS, "OSRPromoteTo3D", OGRERR_FAILURE);
13244 :
13245 3 : return OGRSpatialReference::FromHandle(hSRS)->PromoteTo3D(pszName);
13246 : }
13247 :
13248 : /************************************************************************/
13249 : /* DemoteTo2D() */
13250 : /************************************************************************/
13251 :
13252 : /** \brief "Demote" a 3D CRS to a 2D CRS one.
13253 : *
13254 : * @param pszName New name for the CRS. If set to NULL, the previous name will
13255 : * be used.
13256 : * @return OGRERR_NONE if no error occurred.
13257 : * @since GDAL 3.2 and PROJ 6.3
13258 : */
13259 49 : OGRErr OGRSpatialReference::DemoteTo2D(const char *pszName)
13260 : {
13261 98 : TAKE_OPTIONAL_LOCK();
13262 :
13263 49 : d->refreshProjObj();
13264 49 : if (!d->m_pj_crs)
13265 0 : return OGRERR_FAILURE;
13266 : auto newPj =
13267 49 : proj_crs_demote_to_2D(d->getPROJContext(), pszName, d->m_pj_crs);
13268 49 : if (!newPj)
13269 0 : return OGRERR_FAILURE;
13270 49 : d->setPjCRS(newPj);
13271 49 : return OGRERR_NONE;
13272 : }
13273 :
13274 : /************************************************************************/
13275 : /* OSRDemoteTo2D() */
13276 : /************************************************************************/
13277 :
13278 : /** \brief "Demote" a 3D CRS to a 2D CRS one.
13279 : *
13280 : * See OGRSpatialReference::DemoteTo2D()
13281 : *
13282 : * @since GDAL 3.2 and PROJ 6.3
13283 : */
13284 1 : OGRErr OSRDemoteTo2D(OGRSpatialReferenceH hSRS, const char *pszName)
13285 : {
13286 1 : VALIDATE_POINTER1(hSRS, "OSRDemoteTo2D", OGRERR_FAILURE);
13287 :
13288 1 : return OGRSpatialReference::FromHandle(hSRS)->DemoteTo2D(pszName);
13289 : }
13290 :
13291 : /************************************************************************/
13292 : /* GetEPSGGeogCS() */
13293 : /************************************************************************/
13294 :
13295 : /** Try to establish what the EPSG code for this coordinate systems
13296 : * GEOGCS might be. Returns -1 if no reasonable guess can be made.
13297 : *
13298 : * @return EPSG code
13299 : */
13300 :
13301 346 : int OGRSpatialReference::GetEPSGGeogCS() const
13302 :
13303 : {
13304 692 : TAKE_OPTIONAL_LOCK();
13305 :
13306 : /* -------------------------------------------------------------------- */
13307 : /* Check axis order. */
13308 : /* -------------------------------------------------------------------- */
13309 692 : auto poGeogCRS = std::unique_ptr<OGRSpatialReference>(CloneGeogCS());
13310 346 : if (!poGeogCRS)
13311 0 : return -1;
13312 :
13313 346 : bool ret = false;
13314 346 : poGeogCRS->d->demoteFromBoundCRS();
13315 346 : auto cs = proj_crs_get_coordinate_system(d->getPROJContext(),
13316 346 : poGeogCRS->d->m_pj_crs);
13317 346 : poGeogCRS->d->undoDemoteFromBoundCRS();
13318 346 : if (cs)
13319 : {
13320 346 : const char *pszDirection = nullptr;
13321 346 : if (proj_cs_get_axis_info(d->getPROJContext(), cs, 0, nullptr, nullptr,
13322 : &pszDirection, nullptr, nullptr, nullptr,
13323 346 : nullptr))
13324 : {
13325 346 : if (EQUAL(pszDirection, "north"))
13326 : {
13327 144 : ret = true;
13328 : }
13329 : }
13330 :
13331 346 : proj_destroy(cs);
13332 : }
13333 346 : if (!ret)
13334 202 : return -1;
13335 :
13336 : /* -------------------------------------------------------------------- */
13337 : /* Do we already have it? */
13338 : /* -------------------------------------------------------------------- */
13339 144 : const char *pszAuthName = GetAuthorityName("GEOGCS");
13340 144 : if (pszAuthName != nullptr && EQUAL(pszAuthName, "epsg"))
13341 67 : return atoi(GetAuthorityCode("GEOGCS"));
13342 :
13343 : /* -------------------------------------------------------------------- */
13344 : /* Get the datum and geogcs names. */
13345 : /* -------------------------------------------------------------------- */
13346 :
13347 77 : const char *pszGEOGCS = GetAttrValue("GEOGCS");
13348 77 : const char *pszDatum = GetAttrValue("DATUM");
13349 :
13350 : // We can only operate on coordinate systems with a geogcs.
13351 154 : OGRSpatialReference oSRSTmp;
13352 77 : if (pszGEOGCS == nullptr || pszDatum == nullptr)
13353 : {
13354 : // Calling GetAttrValue("GEOGCS") will fail on a CRS that can't be
13355 : // export to WKT1, so try to extract the geographic CRS through PROJ
13356 : // API with CopyGeogCSFrom() and get the nodes' values from it.
13357 1 : oSRSTmp.CopyGeogCSFrom(this);
13358 1 : pszGEOGCS = oSRSTmp.GetAttrValue("GEOGCS");
13359 1 : pszDatum = oSRSTmp.GetAttrValue("DATUM");
13360 1 : if (pszGEOGCS == nullptr || pszDatum == nullptr)
13361 : {
13362 0 : return -1;
13363 : }
13364 : }
13365 :
13366 : // Lookup geographic CRS name
13367 77 : const PJ_TYPE type = PJ_TYPE_GEOGRAPHIC_2D_CRS;
13368 77 : PJ_OBJ_LIST *list = proj_create_from_name(
13369 : d->getPROJContext(), nullptr, pszGEOGCS, &type, 1, false, 1, nullptr);
13370 77 : if (list)
13371 : {
13372 77 : const auto listSize = proj_list_get_count(list);
13373 77 : if (listSize == 1)
13374 : {
13375 49 : auto crs = proj_list_get(d->getPROJContext(), list, 0);
13376 49 : if (crs)
13377 : {
13378 49 : pszAuthName = proj_get_id_auth_name(crs, 0);
13379 49 : const char *pszCode = proj_get_id_code(crs, 0);
13380 49 : if (pszAuthName && pszCode && EQUAL(pszAuthName, "EPSG"))
13381 : {
13382 47 : const int nCode = atoi(pszCode);
13383 47 : proj_destroy(crs);
13384 47 : proj_list_destroy(list);
13385 47 : return nCode;
13386 : }
13387 2 : proj_destroy(crs);
13388 : }
13389 : }
13390 30 : proj_list_destroy(list);
13391 : }
13392 :
13393 : /* -------------------------------------------------------------------- */
13394 : /* Is this a "well known" geographic coordinate system? */
13395 : /* -------------------------------------------------------------------- */
13396 90 : const bool bWGS = strstr(pszGEOGCS, "WGS") != nullptr ||
13397 30 : strstr(pszDatum, "WGS") ||
13398 30 : strstr(pszGEOGCS, "World Geodetic System") ||
13399 30 : strstr(pszGEOGCS, "World_Geodetic_System") ||
13400 90 : strstr(pszDatum, "World Geodetic System") ||
13401 30 : strstr(pszDatum, "World_Geodetic_System");
13402 :
13403 90 : const bool bNAD = strstr(pszGEOGCS, "NAD") != nullptr ||
13404 30 : strstr(pszDatum, "NAD") ||
13405 30 : strstr(pszGEOGCS, "North American") ||
13406 30 : strstr(pszGEOGCS, "North_American") ||
13407 90 : strstr(pszDatum, "North American") ||
13408 30 : strstr(pszDatum, "North_American");
13409 :
13410 30 : if (bWGS && (strstr(pszGEOGCS, "84") || strstr(pszDatum, "84")))
13411 0 : return 4326;
13412 :
13413 30 : if (bWGS && (strstr(pszGEOGCS, "72") || strstr(pszDatum, "72")))
13414 0 : return 4322;
13415 :
13416 : // This is questionable as there are several 'flavors' of NAD83 that
13417 : // are not the same as 4269
13418 30 : if (bNAD && (strstr(pszGEOGCS, "83") || strstr(pszDatum, "83")))
13419 0 : return 4269;
13420 :
13421 30 : if (bNAD && (strstr(pszGEOGCS, "27") || strstr(pszDatum, "27")))
13422 0 : return 4267;
13423 :
13424 : /* -------------------------------------------------------------------- */
13425 : /* If we know the datum, associate the most likely GCS with */
13426 : /* it. */
13427 : /* -------------------------------------------------------------------- */
13428 30 : const OGRSpatialReference &oActiveObj = oSRSTmp.IsEmpty() ? *this : oSRSTmp;
13429 30 : pszAuthName = oActiveObj.GetAuthorityName("GEOGCS|DATUM");
13430 30 : if (pszAuthName != nullptr && EQUAL(pszAuthName, "epsg") &&
13431 0 : GetPrimeMeridian() == 0.0)
13432 : {
13433 0 : const int nDatum = atoi(oActiveObj.GetAuthorityCode("GEOGCS|DATUM"));
13434 :
13435 0 : if (nDatum >= 6000 && nDatum <= 6999)
13436 0 : return nDatum - 2000;
13437 : }
13438 :
13439 30 : return -1;
13440 : }
13441 :
13442 : /************************************************************************/
13443 : /* SetCoordinateEpoch() */
13444 : /************************************************************************/
13445 :
13446 : /** Set the coordinate epoch, as decimal year.
13447 : *
13448 : * In a dynamic CRS, coordinates of a point on the surface of the Earth may
13449 : * change with time. To be unambiguous the coordinates must always be qualified
13450 : * with the epoch at which they are valid. The coordinate epoch is not
13451 : * necessarily the epoch at which the observation was collected.
13452 : *
13453 : * Pedantically the coordinate epoch of an observation belongs to the
13454 : * observation, and not to the CRS, however it is often more practical to
13455 : * bind it to the CRS. The coordinate epoch should be specified for dynamic
13456 : * CRS (see IsDynamic())
13457 : *
13458 : * This method is the same as the OSRSetCoordinateEpoch() function.
13459 : *
13460 : * @param dfCoordinateEpoch Coordinate epoch as decimal year (e.g. 2021.3)
13461 : * @since OGR 3.4
13462 : */
13463 :
13464 958 : void OGRSpatialReference::SetCoordinateEpoch(double dfCoordinateEpoch)
13465 : {
13466 958 : d->m_coordinateEpoch = dfCoordinateEpoch;
13467 958 : }
13468 :
13469 : /************************************************************************/
13470 : /* OSRSetCoordinateEpoch() */
13471 : /************************************************************************/
13472 :
13473 : /** \brief Set the coordinate epoch, as decimal year.
13474 : *
13475 : * See OGRSpatialReference::SetCoordinateEpoch()
13476 : *
13477 : * @since OGR 3.4
13478 : */
13479 31 : void OSRSetCoordinateEpoch(OGRSpatialReferenceH hSRS, double dfCoordinateEpoch)
13480 : {
13481 31 : VALIDATE_POINTER0(hSRS, "OSRSetCoordinateEpoch");
13482 :
13483 31 : return OGRSpatialReference::FromHandle(hSRS)->SetCoordinateEpoch(
13484 31 : dfCoordinateEpoch);
13485 : }
13486 :
13487 : /************************************************************************/
13488 : /* GetCoordinateEpoch() */
13489 : /************************************************************************/
13490 :
13491 : /** Return the coordinate epoch, as decimal year.
13492 : *
13493 : * In a dynamic CRS, coordinates of a point on the surface of the Earth may
13494 : * change with time. To be unambiguous the coordinates must always be qualified
13495 : * with the epoch at which they are valid. The coordinate epoch is not
13496 : * necessarily the epoch at which the observation was collected.
13497 : *
13498 : * Pedantically the coordinate epoch of an observation belongs to the
13499 : * observation, and not to the CRS, however it is often more practical to
13500 : * bind it to the CRS. The coordinate epoch should be specified for dynamic
13501 : * CRS (see IsDynamic())
13502 : *
13503 : * This method is the same as the OSRGetCoordinateEpoch() function.
13504 : *
13505 : * @return coordinateEpoch Coordinate epoch as decimal year (e.g. 2021.3), or 0
13506 : * if not set, or relevant.
13507 : * @since OGR 3.4
13508 : */
13509 :
13510 15125 : double OGRSpatialReference::GetCoordinateEpoch() const
13511 : {
13512 15125 : return d->m_coordinateEpoch;
13513 : }
13514 :
13515 : /************************************************************************/
13516 : /* OSRGetCoordinateEpoch() */
13517 : /************************************************************************/
13518 :
13519 : /** \brief Get the coordinate epoch, as decimal year.
13520 : *
13521 : * See OGRSpatialReference::GetCoordinateEpoch()
13522 : *
13523 : * @since OGR 3.4
13524 : */
13525 609 : double OSRGetCoordinateEpoch(OGRSpatialReferenceH hSRS)
13526 : {
13527 609 : VALIDATE_POINTER1(hSRS, "OSRGetCoordinateEpoch", 0);
13528 :
13529 609 : return OGRSpatialReference::FromHandle(hSRS)->GetCoordinateEpoch();
13530 : }
|