Coverage Report

Created: 2025-06-22 06:59

/src/gdal/ogr/ogrspatialreference.cpp
Line
Count
Source (jump to first uncovered line)
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
0
#define STRINGIFY(s) #s
62
0
#define XSTRINGIFY(s) STRINGIFY(s)
63
64
struct OGRSpatialReference::Private
65
{
66
    struct Listener : public OGR_SRSNode::Listener
67
    {
68
        OGRSpatialReference::Private *m_poObj = nullptr;
69
70
0
        explicit Listener(OGRSpatialReference::Private *poObj) : m_poObj(poObj)
71
0
        {
72
0
        }
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
    void SetThreadSafe()
135
0
    {
136
0
        m_bIsThreadSafe = true;
137
0
    }
138
139
    void clear();
140
    void setPjCRS(PJ *pj_crsIn, bool doRefreshAxisMapping = true);
141
    void setRoot(OGR_SRSNode *poRoot);
142
    void refreshProjObj();
143
    void nodesChanged();
144
    void refreshRootFromProjObj();
145
    void invalidateNodes();
146
147
    void setMorphToESRI(bool b);
148
149
    PJ *getGeodBaseCRS();
150
    PJ *getProjCRSCoordSys();
151
152
    const char *getProjCRSName();
153
    OGRErr replaceConversionAndUnref(PJ *conv);
154
155
    void demoteFromBoundCRS();
156
    void undoDemoteFromBoundCRS();
157
158
    PJ_CONTEXT *getPROJContext()
159
0
    {
160
0
        return OSRGetProjTLSContext();
161
0
    }
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
0
        explicit OptionalLockGuard(Private *p) : m_private(*p)
180
0
        {
181
0
            if (m_private.m_bIsThreadSafe)
182
0
                m_private.m_mutex.lock();
183
0
        }
184
185
        ~OptionalLockGuard()
186
0
        {
187
0
            if (m_private.m_bIsThreadSafe)
188
0
                m_private.m_mutex.unlock();
189
0
        }
190
    };
191
192
    inline OptionalLockGuard GetOptionalLockGuard()
193
0
    {
194
0
        return OptionalLockGuard(this);
195
0
    }
196
};
197
198
void OGRSpatialReference::Private::Listener::notifyChange(OGR_SRSNode *)
199
0
{
200
0
    m_poObj->nodesChanged();
201
0
}
202
203
#define TAKE_OPTIONAL_LOCK()                                                   \
204
0
    auto lock = d->GetOptionalLockGuard();                                     \
205
0
    CPL_IGNORE_RET_VAL(lock)
206
207
static OSRAxisMappingStrategy GetDefaultAxisMappingStrategy()
208
0
{
209
0
    const char *pszDefaultAMS =
210
0
        CPLGetConfigOption("OSR_DEFAULT_AXIS_MAPPING_STRATEGY", nullptr);
211
0
    if (pszDefaultAMS)
212
0
    {
213
0
        if (EQUAL(pszDefaultAMS, "AUTHORITY_COMPLIANT"))
214
0
            return OAMS_AUTHORITY_COMPLIANT;
215
0
        else if (EQUAL(pszDefaultAMS, "TRADITIONAL_GIS_ORDER"))
216
0
            return OAMS_TRADITIONAL_GIS_ORDER;
217
0
        else
218
0
        {
219
0
            CPLError(CE_Failure, CPLE_AppDefined,
220
0
                     "Illegal value for OSR_DEFAULT_AXIS_MAPPING_STRATEGY = %s",
221
0
                     pszDefaultAMS);
222
0
        }
223
0
    }
224
0
    return OAMS_AUTHORITY_COMPLIANT;
225
0
}
226
227
OGRSpatialReference::Private::Private(OGRSpatialReference *poSelf)
228
0
    : m_poSelf(poSelf),
229
0
      m_poListener(std::shared_ptr<Listener>(new Listener(this)))
230
0
{
231
    // Get the default value for m_axisMappingStrategy from the
232
    // OSR_DEFAULT_AXIS_MAPPING_STRATEGY configuration option, if set.
233
0
    m_axisMappingStrategy = GetDefaultAxisMappingStrategy();
234
0
}
235
236
OGRSpatialReference::Private::~Private()
237
0
{
238
    // In case we destroy the object not in the thread that created it,
239
    // we need to reassign the PROJ context. Having the context bundled inside
240
    // PJ* deeply sucks...
241
0
    PJ_CONTEXT *pj_context_to_destroy = nullptr;
242
0
    PJ_CONTEXT *ctxt;
243
0
    if (GDALThreadLocalDatasetCacheIsInDestruction())
244
0
    {
245
0
        pj_context_to_destroy = proj_context_create();
246
0
        ctxt = pj_context_to_destroy;
247
0
    }
248
0
    else
249
0
    {
250
0
        ctxt = getPROJContext();
251
0
    }
252
253
0
    proj_assign_context(m_pj_crs, ctxt);
254
0
    proj_destroy(m_pj_crs);
255
256
0
    proj_assign_context(m_pj_geod_base_crs_temp, ctxt);
257
0
    proj_destroy(m_pj_geod_base_crs_temp);
258
259
0
    proj_assign_context(m_pj_proj_crs_cs_temp, ctxt);
260
0
    proj_destroy(m_pj_proj_crs_cs_temp);
261
262
0
    proj_assign_context(m_pj_bound_crs_target, ctxt);
263
0
    proj_destroy(m_pj_bound_crs_target);
264
265
0
    proj_assign_context(m_pj_bound_crs_co, ctxt);
266
0
    proj_destroy(m_pj_bound_crs_co);
267
268
0
    proj_assign_context(m_pj_crs_backup, ctxt);
269
0
    proj_destroy(m_pj_crs_backup);
270
271
0
    delete m_poRootBackup;
272
0
    delete m_poRoot;
273
0
    proj_context_destroy(pj_context_to_destroy);
274
0
}
275
276
void OGRSpatialReference::Private::clear()
277
0
{
278
0
    proj_assign_context(m_pj_crs, getPROJContext());
279
0
    proj_destroy(m_pj_crs);
280
0
    m_pj_crs = nullptr;
281
282
0
    delete m_poRoot;
283
0
    m_poRoot = nullptr;
284
0
    m_bNodesChanged = false;
285
286
0
    m_wktImportWarnings.clear();
287
0
    m_wktImportErrors.clear();
288
289
0
    m_pj_crs_modified_during_demote = false;
290
0
    m_pjType = PJ_TYPE_UNKNOWN;
291
0
    m_osPrimeMeridianName.clear();
292
0
    m_osAngularUnits.clear();
293
0
    m_osLinearUnits.clear();
294
295
0
    bNormInfoSet = FALSE;
296
0
    dfFromGreenwich = 1.0;
297
0
    dfToMeter = 1.0;
298
0
    dfToDegrees = 1.0;
299
0
    m_dfAngularUnitToRadian = 0.0;
300
301
0
    m_bMorphToESRI = false;
302
0
    m_bHasCenterLong = false;
303
304
0
    m_coordinateEpoch = 0.0;
305
0
}
306
307
void OGRSpatialReference::Private::setRoot(OGR_SRSNode *poRoot)
308
0
{
309
0
    m_poRoot = poRoot;
310
0
    if (m_poRoot)
311
0
    {
312
0
        m_poRoot->RegisterListener(m_poListener);
313
0
    }
314
0
    nodesChanged();
315
0
}
316
317
void OGRSpatialReference::Private::setPjCRS(PJ *pj_crsIn,
318
                                            bool doRefreshAxisMapping)
319
0
{
320
0
    auto ctxt = getPROJContext();
321
322
0
#if PROJ_AT_LEAST_VERSION(9, 2, 0)
323
0
    if (proj_get_type(pj_crsIn) == PJ_TYPE_COORDINATE_METADATA)
324
0
    {
325
0
        const double dfEpoch =
326
0
            proj_coordinate_metadata_get_epoch(ctxt, pj_crsIn);
327
0
        if (!std::isnan(dfEpoch))
328
0
        {
329
0
            m_poSelf->SetCoordinateEpoch(dfEpoch);
330
0
        }
331
0
        auto crs = proj_get_source_crs(ctxt, pj_crsIn);
332
0
        proj_destroy(pj_crsIn);
333
0
        pj_crsIn = crs;
334
0
    }
335
0
#endif
336
337
0
    proj_assign_context(m_pj_crs, ctxt);
338
0
    proj_destroy(m_pj_crs);
339
0
    m_pj_crs = pj_crsIn;
340
0
    if (m_pj_crs)
341
0
    {
342
0
        m_pjType = proj_get_type(m_pj_crs);
343
0
    }
344
0
    if (m_pj_crs_backup)
345
0
    {
346
0
        m_pj_crs_modified_during_demote = true;
347
0
    }
348
0
    invalidateNodes();
349
0
    if (doRefreshAxisMapping)
350
0
    {
351
0
        refreshAxisMapping();
352
0
    }
353
0
}
354
355
void OGRSpatialReference::Private::refreshProjObj()
356
0
{
357
0
    if (m_bNodesChanged && m_poRoot)
358
0
    {
359
0
        char *pszWKT = nullptr;
360
0
        m_poRoot->exportToWkt(&pszWKT);
361
0
        auto poRootBackup = m_poRoot;
362
0
        m_poRoot = nullptr;
363
0
        const double dfCoordinateEpochBackup = m_coordinateEpoch;
364
0
        clear();
365
0
        m_coordinateEpoch = dfCoordinateEpochBackup;
366
0
        m_bHasCenterLong = strstr(pszWKT, "CENTER_LONG") != nullptr;
367
368
0
        const char *const options[] = {
369
0
            "STRICT=NO",
370
0
#if PROJ_AT_LEAST_VERSION(9, 1, 0)
371
0
            "UNSET_IDENTIFIERS_IF_INCOMPATIBLE_DEF=NO",
372
0
#endif
373
0
            nullptr
374
0
        };
375
0
        PROJ_STRING_LIST warnings = nullptr;
376
0
        PROJ_STRING_LIST errors = nullptr;
377
0
        setPjCRS(proj_create_from_wkt(getPROJContext(), pszWKT, options,
378
0
                                      &warnings, &errors));
379
0
        for (auto iter = warnings; iter && *iter; ++iter)
380
0
        {
381
0
            m_wktImportWarnings.push_back(*iter);
382
0
        }
383
0
        for (auto iter = errors; iter && *iter; ++iter)
384
0
        {
385
0
            m_wktImportErrors.push_back(*iter);
386
0
        }
387
0
        proj_string_list_destroy(warnings);
388
0
        proj_string_list_destroy(errors);
389
390
0
        CPLFree(pszWKT);
391
392
0
        m_poRoot = poRootBackup;
393
0
        m_bNodesChanged = false;
394
0
    }
395
0
}
396
397
void OGRSpatialReference::Private::refreshRootFromProjObj()
398
0
{
399
0
    CPLAssert(m_poRoot == nullptr);
400
401
0
    if (m_pj_crs)
402
0
    {
403
0
        CPLStringList aosOptions;
404
0
        if (!m_bMorphToESRI)
405
0
        {
406
0
            aosOptions.SetNameValue("OUTPUT_AXIS", "YES");
407
0
            aosOptions.SetNameValue("MULTILINE", "NO");
408
0
        }
409
0
        aosOptions.SetNameValue("STRICT", "NO");
410
411
0
        const char *pszWKT;
412
0
        {
413
0
            CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
414
0
            pszWKT = proj_as_wkt(getPROJContext(), m_pj_crs,
415
0
                                 m_bMorphToESRI ? PJ_WKT1_ESRI : PJ_WKT1_GDAL,
416
0
                                 aosOptions.List());
417
0
            m_bNodesWKT2 = false;
418
0
        }
419
0
        if (!m_bMorphToESRI && pszWKT == nullptr)
420
0
        {
421
0
            pszWKT = proj_as_wkt(getPROJContext(), m_pj_crs, PJ_WKT2_2018,
422
0
                                 aosOptions.List());
423
0
            m_bNodesWKT2 = true;
424
0
        }
425
0
        if (pszWKT)
426
0
        {
427
0
            auto root = new OGR_SRSNode();
428
0
            setRoot(root);
429
0
            root->importFromWkt(&pszWKT);
430
0
            m_bNodesChanged = false;
431
0
        }
432
0
    }
433
0
}
434
435
static bool isNorthEastAxisOrder(PJ_CONTEXT *ctx, PJ *cs)
436
0
{
437
0
    const char *pszName1 = nullptr;
438
0
    const char *pszDirection1 = nullptr;
439
0
    proj_cs_get_axis_info(ctx, cs, 0, &pszName1, nullptr, &pszDirection1,
440
0
                          nullptr, nullptr, nullptr, nullptr);
441
0
    const char *pszName2 = nullptr;
442
0
    const char *pszDirection2 = nullptr;
443
0
    proj_cs_get_axis_info(ctx, cs, 1, &pszName2, nullptr, &pszDirection2,
444
0
                          nullptr, nullptr, nullptr, nullptr);
445
0
    if (pszDirection1 && EQUAL(pszDirection1, "north") && pszDirection2 &&
446
0
        EQUAL(pszDirection2, "east"))
447
0
    {
448
0
        return true;
449
0
    }
450
0
    if (pszDirection1 && pszDirection2 &&
451
0
        ((EQUAL(pszDirection1, "north") && EQUAL(pszDirection2, "north")) ||
452
0
         (EQUAL(pszDirection1, "south") && EQUAL(pszDirection2, "south"))) &&
453
0
        pszName1 && STARTS_WITH_CI(pszName1, "northing") && pszName2 &&
454
0
        STARTS_WITH_CI(pszName2, "easting"))
455
0
    {
456
0
        return true;
457
0
    }
458
0
    return false;
459
0
}
460
461
void OGRSpatialReference::Private::refreshAxisMapping()
462
0
{
463
0
    if (!m_pj_crs || m_axisMappingStrategy == OAMS_CUSTOM)
464
0
        return;
465
466
0
    bool doUndoDemote = false;
467
0
    if (m_pj_crs_backup == nullptr)
468
0
    {
469
0
        doUndoDemote = true;
470
0
        demoteFromBoundCRS();
471
0
    }
472
0
    const auto ctxt = getPROJContext();
473
0
    PJ *horizCRS = nullptr;
474
0
    int axisCount = 0;
475
0
    if (m_pjType == PJ_TYPE_VERTICAL_CRS)
476
0
    {
477
0
        axisCount = 1;
478
0
    }
479
0
    else if (m_pjType == PJ_TYPE_COMPOUND_CRS)
480
0
    {
481
0
        horizCRS = proj_crs_get_sub_crs(ctxt, m_pj_crs, 0);
482
0
        if (horizCRS && proj_get_type(horizCRS) == PJ_TYPE_BOUND_CRS)
483
0
        {
484
0
            auto baseCRS = proj_get_source_crs(ctxt, horizCRS);
485
0
            if (baseCRS)
486
0
            {
487
0
                proj_destroy(horizCRS);
488
0
                horizCRS = baseCRS;
489
0
            }
490
0
        }
491
492
0
        auto vertCRS = proj_crs_get_sub_crs(ctxt, m_pj_crs, 1);
493
0
        if (vertCRS)
494
0
        {
495
0
            if (proj_get_type(vertCRS) == PJ_TYPE_BOUND_CRS)
496
0
            {
497
0
                auto baseCRS = proj_get_source_crs(ctxt, vertCRS);
498
0
                if (baseCRS)
499
0
                {
500
0
                    proj_destroy(vertCRS);
501
0
                    vertCRS = baseCRS;
502
0
                }
503
0
            }
504
505
0
            auto cs = proj_crs_get_coordinate_system(ctxt, vertCRS);
506
0
            if (cs)
507
0
            {
508
0
                axisCount += proj_cs_get_axis_count(ctxt, cs);
509
0
                proj_destroy(cs);
510
0
            }
511
0
            proj_destroy(vertCRS);
512
0
        }
513
0
    }
514
0
    else
515
0
    {
516
0
        horizCRS = m_pj_crs;
517
0
    }
518
519
0
    bool bSwitchForGisFriendlyOrder = false;
520
0
    if (horizCRS)
521
0
    {
522
0
        auto cs = proj_crs_get_coordinate_system(ctxt, horizCRS);
523
0
        if (cs)
524
0
        {
525
0
            int nHorizCSAxisCount = proj_cs_get_axis_count(ctxt, cs);
526
0
            axisCount += nHorizCSAxisCount;
527
0
            if (nHorizCSAxisCount >= 2)
528
0
            {
529
0
                bSwitchForGisFriendlyOrder = isNorthEastAxisOrder(ctxt, cs);
530
0
            }
531
0
            proj_destroy(cs);
532
0
        }
533
0
    }
534
0
    if (horizCRS != m_pj_crs)
535
0
    {
536
0
        proj_destroy(horizCRS);
537
0
    }
538
0
    if (doUndoDemote)
539
0
    {
540
0
        undoDemoteFromBoundCRS();
541
0
    }
542
543
0
    m_axisMapping.resize(axisCount);
544
0
    if (m_axisMappingStrategy == OAMS_AUTHORITY_COMPLIANT ||
545
0
        !bSwitchForGisFriendlyOrder)
546
0
    {
547
0
        for (int i = 0; i < axisCount; i++)
548
0
        {
549
0
            m_axisMapping[i] = i + 1;
550
0
        }
551
0
    }
552
0
    else
553
0
    {
554
0
        m_axisMapping[0] = 2;
555
0
        m_axisMapping[1] = 1;
556
0
        if (axisCount == 3)
557
0
        {
558
0
            m_axisMapping[2] = 3;
559
0
        }
560
0
    }
561
0
}
562
563
void OGRSpatialReference::Private::nodesChanged()
564
0
{
565
0
    m_bNodesChanged = true;
566
0
}
567
568
void OGRSpatialReference::Private::invalidateNodes()
569
0
{
570
0
    delete m_poRoot;
571
0
    m_poRoot = nullptr;
572
0
    m_bNodesChanged = false;
573
0
}
574
575
void OGRSpatialReference::Private::setMorphToESRI(bool b)
576
0
{
577
0
    invalidateNodes();
578
0
    m_bMorphToESRI = b;
579
0
}
580
581
void OGRSpatialReference::Private::demoteFromBoundCRS()
582
0
{
583
0
    CPLAssert(m_pj_bound_crs_target == nullptr);
584
0
    CPLAssert(m_pj_bound_crs_co == nullptr);
585
0
    CPLAssert(m_poRootBackup == nullptr);
586
0
    CPLAssert(m_pj_crs_backup == nullptr);
587
588
0
    m_pj_crs_modified_during_demote = false;
589
590
0
    if (m_pjType == PJ_TYPE_BOUND_CRS)
591
0
    {
592
0
        auto baseCRS = proj_get_source_crs(getPROJContext(), m_pj_crs);
593
0
        m_pj_bound_crs_target = proj_get_target_crs(getPROJContext(), m_pj_crs);
594
0
        m_pj_bound_crs_co =
595
0
            proj_crs_get_coordoperation(getPROJContext(), m_pj_crs);
596
597
0
        m_poRootBackup = m_poRoot;
598
0
        m_poRoot = nullptr;
599
0
        m_pj_crs_backup = m_pj_crs;
600
0
        m_pj_crs = baseCRS;
601
0
        m_pjType = proj_get_type(m_pj_crs);
602
0
    }
603
0
}
604
605
void OGRSpatialReference::Private::undoDemoteFromBoundCRS()
606
0
{
607
0
    if (m_pj_bound_crs_target)
608
0
    {
609
0
        CPLAssert(m_poRoot == nullptr);
610
0
        CPLAssert(m_pj_crs);
611
0
        if (!m_pj_crs_modified_during_demote)
612
0
        {
613
0
            proj_destroy(m_pj_crs);
614
0
            m_pj_crs = m_pj_crs_backup;
615
0
            m_pjType = proj_get_type(m_pj_crs);
616
0
            m_poRoot = m_poRootBackup;
617
0
        }
618
0
        else
619
0
        {
620
0
            delete m_poRootBackup;
621
0
            m_poRootBackup = nullptr;
622
0
            proj_destroy(m_pj_crs_backup);
623
0
            m_pj_crs_backup = nullptr;
624
0
            setPjCRS(proj_crs_create_bound_crs(getPROJContext(), m_pj_crs,
625
0
                                               m_pj_bound_crs_target,
626
0
                                               m_pj_bound_crs_co),
627
0
                     false);
628
0
        }
629
0
    }
630
631
0
    m_poRootBackup = nullptr;
632
0
    m_pj_crs_backup = nullptr;
633
0
    proj_destroy(m_pj_bound_crs_target);
634
0
    m_pj_bound_crs_target = nullptr;
635
0
    proj_destroy(m_pj_bound_crs_co);
636
0
    m_pj_bound_crs_co = nullptr;
637
0
    m_pj_crs_modified_during_demote = false;
638
0
}
639
640
const char *OGRSpatialReference::Private::nullifyTargetKeyIfPossible(
641
    const char *pszTargetKey)
642
0
{
643
0
    if (pszTargetKey)
644
0
    {
645
0
        demoteFromBoundCRS();
646
0
        if ((m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
647
0
             m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS) &&
648
0
            EQUAL(pszTargetKey, "GEOGCS"))
649
0
        {
650
0
            pszTargetKey = nullptr;
651
0
        }
652
0
        else if (m_pjType == PJ_TYPE_GEOCENTRIC_CRS &&
653
0
                 EQUAL(pszTargetKey, "GEOCCS"))
654
0
        {
655
0
            pszTargetKey = nullptr;
656
0
        }
657
0
        else if (m_pjType == PJ_TYPE_PROJECTED_CRS &&
658
0
                 EQUAL(pszTargetKey, "PROJCS"))
659
0
        {
660
0
            pszTargetKey = nullptr;
661
0
        }
662
0
        else if (m_pjType == PJ_TYPE_VERTICAL_CRS &&
663
0
                 EQUAL(pszTargetKey, "VERT_CS"))
664
0
        {
665
0
            pszTargetKey = nullptr;
666
0
        }
667
0
        undoDemoteFromBoundCRS();
668
0
    }
669
0
    return pszTargetKey;
670
0
}
671
672
PJ *OGRSpatialReference::Private::getGeodBaseCRS()
673
0
{
674
0
    if (m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
675
0
        m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS)
676
0
    {
677
0
        return m_pj_crs;
678
0
    }
679
680
0
    auto ctxt = getPROJContext();
681
0
    if (m_pjType == PJ_TYPE_PROJECTED_CRS)
682
0
    {
683
0
        proj_assign_context(m_pj_geod_base_crs_temp, ctxt);
684
0
        proj_destroy(m_pj_geod_base_crs_temp);
685
0
        m_pj_geod_base_crs_temp = proj_crs_get_geodetic_crs(ctxt, m_pj_crs);
686
0
        return m_pj_geod_base_crs_temp;
687
0
    }
688
689
0
    proj_assign_context(m_pj_geod_base_crs_temp, ctxt);
690
0
    proj_destroy(m_pj_geod_base_crs_temp);
691
0
    auto cs = proj_create_ellipsoidal_2D_cs(ctxt, PJ_ELLPS2D_LATITUDE_LONGITUDE,
692
0
                                            nullptr, 0);
693
0
    m_pj_geod_base_crs_temp = proj_create_geographic_crs(
694
0
        ctxt, "WGS 84", "World Geodetic System 1984", "WGS 84",
695
0
        SRS_WGS84_SEMIMAJOR, SRS_WGS84_INVFLATTENING, SRS_PM_GREENWICH, 0.0,
696
0
        SRS_UA_DEGREE, CPLAtof(SRS_UA_DEGREE_CONV), cs);
697
0
    proj_destroy(cs);
698
699
0
    return m_pj_geod_base_crs_temp;
700
0
}
701
702
PJ *OGRSpatialReference::Private::getProjCRSCoordSys()
703
0
{
704
0
    auto ctxt = getPROJContext();
705
0
    if (m_pjType == PJ_TYPE_PROJECTED_CRS)
706
0
    {
707
0
        proj_assign_context(m_pj_proj_crs_cs_temp, ctxt);
708
0
        proj_destroy(m_pj_proj_crs_cs_temp);
709
0
        m_pj_proj_crs_cs_temp =
710
0
            proj_crs_get_coordinate_system(getPROJContext(), m_pj_crs);
711
0
        return m_pj_proj_crs_cs_temp;
712
0
    }
713
714
0
    proj_assign_context(m_pj_proj_crs_cs_temp, ctxt);
715
0
    proj_destroy(m_pj_proj_crs_cs_temp);
716
0
    m_pj_proj_crs_cs_temp = proj_create_cartesian_2D_cs(
717
0
        ctxt, PJ_CART2D_EASTING_NORTHING, nullptr, 0);
718
0
    return m_pj_proj_crs_cs_temp;
719
0
}
720
721
const char *OGRSpatialReference::Private::getProjCRSName()
722
0
{
723
0
    if (m_pjType == PJ_TYPE_PROJECTED_CRS)
724
0
    {
725
0
        return proj_get_name(m_pj_crs);
726
0
    }
727
728
0
    return "unnamed";
729
0
}
730
731
OGRErr OGRSpatialReference::Private::replaceConversionAndUnref(PJ *conv)
732
0
{
733
0
    refreshProjObj();
734
735
0
    demoteFromBoundCRS();
736
737
0
    auto projCRS =
738
0
        proj_create_projected_crs(getPROJContext(), getProjCRSName(),
739
0
                                  getGeodBaseCRS(), conv, getProjCRSCoordSys());
740
0
    proj_destroy(conv);
741
742
0
    setPjCRS(projCRS);
743
744
0
    undoDemoteFromBoundCRS();
745
0
    return OGRERR_NONE;
746
0
}
747
748
/************************************************************************/
749
/*                           ToPointer()                                */
750
/************************************************************************/
751
752
static inline OGRSpatialReference *ToPointer(OGRSpatialReferenceH hSRS)
753
0
{
754
0
    return OGRSpatialReference::FromHandle(hSRS);
755
0
}
756
757
/************************************************************************/
758
/*                           ToHandle()                                 */
759
/************************************************************************/
760
761
static inline OGRSpatialReferenceH ToHandle(OGRSpatialReference *poSRS)
762
0
{
763
0
    return OGRSpatialReference::ToHandle(poSRS);
764
0
}
765
766
/************************************************************************/
767
/*                           OGRsnPrintDouble()                         */
768
/************************************************************************/
769
770
void OGRsnPrintDouble(char *pszStrBuf, size_t size, double dfValue);
771
772
void OGRsnPrintDouble(char *pszStrBuf, size_t size, double dfValue)
773
774
0
{
775
0
    CPLsnprintf(pszStrBuf, size, "%.16g", dfValue);
776
777
0
    const size_t nLen = strlen(pszStrBuf);
778
779
    // The following hack is intended to truncate some "precision" in cases
780
    // that appear to be roundoff error.
781
0
    if (nLen > 15 && (strcmp(pszStrBuf + nLen - 6, "999999") == 0 ||
782
0
                      strcmp(pszStrBuf + nLen - 6, "000001") == 0))
783
0
    {
784
0
        CPLsnprintf(pszStrBuf, size, "%.15g", dfValue);
785
0
    }
786
787
    // Force to user periods regardless of locale.
788
0
    if (strchr(pszStrBuf, ',') != nullptr)
789
0
    {
790
0
        char *const pszDelim = strchr(pszStrBuf, ',');
791
0
        *pszDelim = '.';
792
0
    }
793
0
}
794
795
/************************************************************************/
796
/*                        OGRSpatialReference()                         */
797
/************************************************************************/
798
799
/**
800
 * \brief Constructor.
801
 *
802
 * This constructor takes an optional string argument which if passed
803
 * should be a WKT representation of an SRS.  Passing this is equivalent
804
 * to not passing it, and then calling importFromWkt() with the WKT string.
805
 *
806
 * Note that newly created objects are given a reference count of one.
807
 *
808
 * Starting with GDAL 3.0, coordinates associated with a OGRSpatialReference
809
 * object are assumed to be in the order of the axis of the CRS definition
810
 (which
811
 * for example means latitude first, longitude second for geographic CRS
812
 belonging
813
 * to the EPSG authority). It is possible to define a data axis to CRS axis
814
 * mapping strategy with the SetAxisMappingStrategy() method.
815
 *
816
 * Starting with GDAL 3.5, the OSR_DEFAULT_AXIS_MAPPING_STRATEGY configuration
817
 * option can be set to "TRADITIONAL_GIS_ORDER" / "AUTHORITY_COMPLIANT" (the
818
 later
819
 * being the default value when the option is not set) to control the value of
820
 the
821
 * data axis to CRS axis mapping strategy when a OSRSpatialReference object is
822
 * created. Calling SetAxisMappingStrategy() will override this default value.
823
824
 * The C function OSRNewSpatialReference() does the same thing as this
825
 * constructor.
826
 *
827
 * @param pszWKT well known text definition to which the object should
828
 * be initialized, or NULL (the default).
829
 */
830
831
OGRSpatialReference::OGRSpatialReference(const char *pszWKT)
832
0
    : d(new Private(this))
833
0
{
834
0
    if (pszWKT != nullptr)
835
0
        importFromWkt(pszWKT);
836
0
}
837
838
/************************************************************************/
839
/*                       OSRNewSpatialReference()                       */
840
/************************************************************************/
841
842
/**
843
 * \brief Constructor.
844
 *
845
 * Starting with GDAL 3.0, coordinates associated with a OGRSpatialReference
846
 * object are assumed to be in the order of the axis of the CRS definition
847
 * (which for example means latitude first, longitude second for geographic CRS
848
 * belonging to the EPSG authority). It is possible to define a data axis to CRS
849
 * axis mapping strategy with the SetAxisMappingStrategy() method.
850
 *
851
 * Starting with GDAL 3.5, the OSR_DEFAULT_AXIS_MAPPING_STRATEGY configuration
852
 * option can be set to "TRADITIONAL_GIS_ORDER" / "AUTHORITY_COMPLIANT" (the
853
 * later being the default value when the option is not set) to control the
854
 * value of the data axis to CRS axis mapping strategy when a
855
 * OSRSpatialReference object is created. Calling SetAxisMappingStrategy() will
856
 * override this default value.
857
 *
858
 * This function is the same as OGRSpatialReference::OGRSpatialReference()
859
 */
860
OGRSpatialReferenceH CPL_STDCALL OSRNewSpatialReference(const char *pszWKT)
861
862
0
{
863
0
    OGRSpatialReference *poSRS = new OGRSpatialReference();
864
865
0
    if (pszWKT != nullptr && strlen(pszWKT) > 0)
866
0
    {
867
0
        if (poSRS->importFromWkt(pszWKT) != OGRERR_NONE)
868
0
        {
869
0
            delete poSRS;
870
0
            poSRS = nullptr;
871
0
        }
872
0
    }
873
874
0
    return ToHandle(poSRS);
875
0
}
876
877
/************************************************************************/
878
/*                        OGRSpatialReference()                         */
879
/************************************************************************/
880
881
/** Copy constructor. See also Clone().
882
 * @param oOther other spatial reference
883
 */
884
OGRSpatialReference::OGRSpatialReference(const OGRSpatialReference &oOther)
885
0
    : d(new Private(this))
886
0
{
887
0
    *this = oOther;
888
0
}
889
890
/************************************************************************/
891
/*                        OGRSpatialReference()                         */
892
/************************************************************************/
893
894
/** Move constructor.
895
 * @param oOther other spatial reference
896
 */
897
OGRSpatialReference::OGRSpatialReference(OGRSpatialReference &&oOther)
898
0
    : d(std::move(oOther.d))
899
0
{
900
0
}
901
902
/************************************************************************/
903
/*                        ~OGRSpatialReference()                        */
904
/************************************************************************/
905
906
/**
907
 * \brief OGRSpatialReference destructor.
908
 *
909
 * The C function OSRDestroySpatialReference() does the same thing as this
910
 * method. Preferred C++ method : OGRSpatialReference::DestroySpatialReference()
911
 *
912
 * @deprecated
913
 */
914
915
OGRSpatialReference::~OGRSpatialReference()
916
917
0
{
918
0
}
919
920
/************************************************************************/
921
/*                      DestroySpatialReference()                       */
922
/************************************************************************/
923
924
/**
925
 * \brief OGRSpatialReference destructor.
926
 *
927
 * This static method will destroy a OGRSpatialReference.  It is
928
 * equivalent to calling delete on the object, but it ensures that the
929
 * deallocation is properly executed within the OGR libraries heap on
930
 * platforms where this can matter (win32).
931
 *
932
 * This function is the same as OSRDestroySpatialReference()
933
 *
934
 * @param poSRS the object to delete
935
 *
936
 * @since GDAL 1.7.0
937
 */
938
939
void OGRSpatialReference::DestroySpatialReference(OGRSpatialReference *poSRS)
940
0
{
941
0
    delete poSRS;
942
0
}
943
944
/************************************************************************/
945
/*                     OSRDestroySpatialReference()                     */
946
/************************************************************************/
947
948
/**
949
 * \brief OGRSpatialReference destructor.
950
 *
951
 * This function is the same as OGRSpatialReference::~OGRSpatialReference()
952
 * and OGRSpatialReference::DestroySpatialReference()
953
 *
954
 * @param hSRS the object to delete
955
 */
956
void CPL_STDCALL OSRDestroySpatialReference(OGRSpatialReferenceH hSRS)
957
958
0
{
959
0
    delete ToPointer(hSRS);
960
0
}
961
962
/************************************************************************/
963
/*                               Clear()                                */
964
/************************************************************************/
965
966
/**
967
 * \brief Wipe current definition.
968
 *
969
 * Returns OGRSpatialReference to a state with no definition, as it
970
 * exists when first created.  It does not affect reference counts.
971
 */
972
973
void OGRSpatialReference::Clear()
974
975
0
{
976
0
    d->clear();
977
0
}
978
979
/************************************************************************/
980
/*                             operator=()                              */
981
/************************************************************************/
982
983
/** Assignment operator.
984
 * @param oSource SRS to assign to *this
985
 * @return *this
986
 */
987
OGRSpatialReference &
988
OGRSpatialReference::operator=(const OGRSpatialReference &oSource)
989
990
0
{
991
0
    if (&oSource != this)
992
0
    {
993
0
        Clear();
994
#ifdef CPPCHECK
995
        // Otherwise cppcheck would protest that nRefCount isn't modified
996
        d->nRefCount = (d->nRefCount + 1) - 1;
997
#endif
998
999
0
        oSource.d->refreshProjObj();
1000
0
        if (oSource.d->m_pj_crs)
1001
0
            d->setPjCRS(proj_clone(d->getPROJContext(), oSource.d->m_pj_crs));
1002
0
        if (oSource.d->m_axisMappingStrategy == OAMS_TRADITIONAL_GIS_ORDER)
1003
0
            SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
1004
0
        else if (oSource.d->m_axisMappingStrategy == OAMS_CUSTOM)
1005
0
            SetDataAxisToSRSAxisMapping(oSource.d->m_axisMapping);
1006
1007
0
        d->m_coordinateEpoch = oSource.d->m_coordinateEpoch;
1008
0
    }
1009
1010
0
    return *this;
1011
0
}
1012
1013
/************************************************************************/
1014
/*                             operator=()                              */
1015
/************************************************************************/
1016
1017
/** Move assignment operator.
1018
 * @param oSource SRS to assign to *this
1019
 * @return *this
1020
 */
1021
OGRSpatialReference &
1022
OGRSpatialReference::operator=(OGRSpatialReference &&oSource)
1023
1024
0
{
1025
0
    if (&oSource != this)
1026
0
    {
1027
0
        d = std::move(oSource.d);
1028
0
    }
1029
1030
0
    return *this;
1031
0
}
1032
1033
/************************************************************************/
1034
/*                      AssignAndSetThreadSafe()                        */
1035
/************************************************************************/
1036
1037
/** Assignment method, with thread-safety.
1038
 *
1039
 * Same as an assignment operator, but asking also that the *this instance
1040
 * becomes thread-safe.
1041
 *
1042
 * @param oSource SRS to assign to *this
1043
 * @return *this
1044
 * @since 3.10
1045
 */
1046
1047
OGRSpatialReference &
1048
OGRSpatialReference::AssignAndSetThreadSafe(const OGRSpatialReference &oSource)
1049
0
{
1050
0
    *this = oSource;
1051
0
    d->SetThreadSafe();
1052
0
    return *this;
1053
0
}
1054
1055
/************************************************************************/
1056
/*                             Reference()                              */
1057
/************************************************************************/
1058
1059
/**
1060
 * \brief Increments the reference count by one.
1061
 *
1062
 * The reference count is used keep track of the number of OGRGeometry objects
1063
 * referencing this SRS.
1064
 *
1065
 * The method does the same thing as the C function OSRReference().
1066
 *
1067
 * @return the updated reference count.
1068
 */
1069
1070
int OGRSpatialReference::Reference()
1071
1072
0
{
1073
0
    return CPLAtomicInc(&d->nRefCount);
1074
0
}
1075
1076
/************************************************************************/
1077
/*                            OSRReference()                            */
1078
/************************************************************************/
1079
1080
/**
1081
 * \brief Increments the reference count by one.
1082
 *
1083
 * This function is the same as OGRSpatialReference::Reference()
1084
 */
1085
int OSRReference(OGRSpatialReferenceH hSRS)
1086
1087
0
{
1088
0
    VALIDATE_POINTER1(hSRS, "OSRReference", 0);
1089
1090
0
    return ToPointer(hSRS)->Reference();
1091
0
}
1092
1093
/************************************************************************/
1094
/*                            Dereference()                             */
1095
/************************************************************************/
1096
1097
/**
1098
 * \brief Decrements the reference count by one.
1099
 *
1100
 * The method does the same thing as the C function OSRDereference().
1101
 *
1102
 * @return the updated reference count.
1103
 */
1104
1105
int OGRSpatialReference::Dereference()
1106
1107
0
{
1108
0
    if (d->nRefCount <= 0)
1109
0
        CPLDebug("OSR",
1110
0
                 "Dereference() called on an object with refcount %d,"
1111
0
                 "likely already destroyed!",
1112
0
                 d->nRefCount);
1113
0
    return CPLAtomicDec(&d->nRefCount);
1114
0
}
1115
1116
/************************************************************************/
1117
/*                           OSRDereference()                           */
1118
/************************************************************************/
1119
1120
/**
1121
 * \brief Decrements the reference count by one.
1122
 *
1123
 * This function is the same as OGRSpatialReference::Dereference()
1124
 */
1125
int OSRDereference(OGRSpatialReferenceH hSRS)
1126
1127
0
{
1128
0
    VALIDATE_POINTER1(hSRS, "OSRDereference", 0);
1129
1130
0
    return ToPointer(hSRS)->Dereference();
1131
0
}
1132
1133
/************************************************************************/
1134
/*                         GetReferenceCount()                          */
1135
/************************************************************************/
1136
1137
/**
1138
 * \brief Fetch current reference count.
1139
 *
1140
 * @return the current reference count.
1141
 */
1142
int OGRSpatialReference::GetReferenceCount() const
1143
0
{
1144
0
    return d->nRefCount;
1145
0
}
1146
1147
/************************************************************************/
1148
/*                              Release()                               */
1149
/************************************************************************/
1150
1151
/**
1152
 * \brief Decrements the reference count by one, and destroy if zero.
1153
 *
1154
 * The method does the same thing as the C function OSRRelease().
1155
 */
1156
1157
void OGRSpatialReference::Release()
1158
1159
0
{
1160
0
    if (Dereference() <= 0)
1161
0
        delete this;
1162
0
}
1163
1164
/************************************************************************/
1165
/*                             OSRRelease()                             */
1166
/************************************************************************/
1167
1168
/**
1169
 * \brief Decrements the reference count by one, and destroy if zero.
1170
 *
1171
 * This function is the same as OGRSpatialReference::Release()
1172
 */
1173
void OSRRelease(OGRSpatialReferenceH hSRS)
1174
1175
0
{
1176
0
    VALIDATE_POINTER0(hSRS, "OSRRelease");
1177
1178
0
    ToPointer(hSRS)->Release();
1179
0
}
1180
1181
OGR_SRSNode *OGRSpatialReference::GetRoot()
1182
0
{
1183
0
    TAKE_OPTIONAL_LOCK();
1184
1185
0
    if (!d->m_poRoot)
1186
0
    {
1187
0
        d->refreshRootFromProjObj();
1188
0
    }
1189
0
    return d->m_poRoot;
1190
0
}
1191
1192
const OGR_SRSNode *OGRSpatialReference::GetRoot() const
1193
0
{
1194
0
    TAKE_OPTIONAL_LOCK();
1195
1196
0
    if (!d->m_poRoot)
1197
0
    {
1198
0
        d->refreshRootFromProjObj();
1199
0
    }
1200
0
    return d->m_poRoot;
1201
0
}
1202
1203
/************************************************************************/
1204
/*                              SetRoot()                               */
1205
/************************************************************************/
1206
1207
/**
1208
 * \brief Set the root SRS node.
1209
 *
1210
 * If the object has an existing tree of OGR_SRSNodes, they are destroyed
1211
 * as part of assigning the new root.  Ownership of the passed OGR_SRSNode is
1212
 * is assumed by the OGRSpatialReference.
1213
 *
1214
 * @param poNewRoot object to assign as root.
1215
 */
1216
1217
void OGRSpatialReference::SetRoot(OGR_SRSNode *poNewRoot)
1218
1219
0
{
1220
0
    if (d->m_poRoot != poNewRoot)
1221
0
    {
1222
0
        delete d->m_poRoot;
1223
0
        d->setRoot(poNewRoot);
1224
0
    }
1225
0
}
1226
1227
/************************************************************************/
1228
/*                            GetAttrNode()                             */
1229
/************************************************************************/
1230
1231
/**
1232
 * \brief Find named node in tree.
1233
 *
1234
 * This method does a pre-order traversal of the node tree searching for
1235
 * a node with this exact value (case insensitive), and returns it.  Leaf
1236
 * nodes are not considered, under the assumption that they are just
1237
 * attribute value nodes.
1238
 *
1239
 * If a node appears more than once in the tree (such as UNIT for instance),
1240
 * the first encountered will be returned.  Use GetNode() on a subtree to be
1241
 * more specific.
1242
 *
1243
 * @param pszNodePath the name of the node to search for.  May contain multiple
1244
 * components such as "GEOGCS|UNIT".
1245
 *
1246
 * @return a pointer to the node found, or NULL if none.
1247
 */
1248
1249
OGR_SRSNode *OGRSpatialReference::GetAttrNode(const char *pszNodePath)
1250
1251
0
{
1252
0
    if (strchr(pszNodePath, '|') == nullptr)
1253
0
    {
1254
        // Fast path
1255
0
        OGR_SRSNode *poNode = GetRoot();
1256
0
        if (poNode)
1257
0
            poNode = poNode->GetNode(pszNodePath);
1258
0
        return poNode;
1259
0
    }
1260
1261
0
    char **papszPathTokens =
1262
0
        CSLTokenizeStringComplex(pszNodePath, "|", TRUE, FALSE);
1263
1264
0
    if (CSLCount(papszPathTokens) < 1)
1265
0
    {
1266
0
        CSLDestroy(papszPathTokens);
1267
0
        return nullptr;
1268
0
    }
1269
1270
0
    OGR_SRSNode *poNode = GetRoot();
1271
0
    for (int i = 0; poNode != nullptr && papszPathTokens[i] != nullptr; i++)
1272
0
    {
1273
0
        poNode = poNode->GetNode(papszPathTokens[i]);
1274
0
    }
1275
1276
0
    CSLDestroy(papszPathTokens);
1277
1278
0
    return poNode;
1279
0
}
1280
1281
/**
1282
 * \brief Find named node in tree.
1283
 *
1284
 * This method does a pre-order traversal of the node tree searching for
1285
 * a node with this exact value (case insensitive), and returns it.  Leaf
1286
 * nodes are not considered, under the assumption that they are just
1287
 * attribute value nodes.
1288
 *
1289
 * If a node appears more than once in the tree (such as UNIT for instance),
1290
 * the first encountered will be returned.  Use GetNode() on a subtree to be
1291
 * more specific.
1292
 *
1293
 * @param pszNodePath the name of the node to search for.  May contain multiple
1294
 * components such as "GEOGCS|UNIT".
1295
 *
1296
 * @return a pointer to the node found, or NULL if none.
1297
 */
1298
1299
const OGR_SRSNode *
1300
OGRSpatialReference::GetAttrNode(const char *pszNodePath) const
1301
1302
0
{
1303
0
    OGR_SRSNode *poNode =
1304
0
        const_cast<OGRSpatialReference *>(this)->GetAttrNode(pszNodePath);
1305
1306
0
    return poNode;
1307
0
}
1308
1309
/************************************************************************/
1310
/*                            GetAttrValue()                            */
1311
/************************************************************************/
1312
1313
/**
1314
 * \brief Fetch indicated attribute of named node.
1315
 *
1316
 * This method uses GetAttrNode() to find the named node, and then extracts
1317
 * the value of the indicated child.  Thus a call to GetAttrValue("UNIT",1)
1318
 * would return the second child of the UNIT node, which is normally the
1319
 * length of the linear unit in meters.
1320
 *
1321
 * This method does the same thing as the C function OSRGetAttrValue().
1322
 *
1323
 * @param pszNodeName the tree node to look for (case insensitive).
1324
 * @param iAttr the child of the node to fetch (zero based).
1325
 *
1326
 * @return the requested value, or NULL if it fails for any reason.
1327
 */
1328
1329
const char *OGRSpatialReference::GetAttrValue(const char *pszNodeName,
1330
                                              int iAttr) const
1331
1332
0
{
1333
0
    const OGR_SRSNode *poNode = GetAttrNode(pszNodeName);
1334
0
    if (poNode == nullptr)
1335
0
    {
1336
0
        if (d->m_bNodesWKT2 && EQUAL(pszNodeName, "PROJECTION"))
1337
0
        {
1338
0
            return GetAttrValue("METHOD", iAttr);
1339
0
        }
1340
0
        else if (d->m_bNodesWKT2 && EQUAL(pszNodeName, "PROJCS|PROJECTION"))
1341
0
        {
1342
0
            return GetAttrValue("PROJCRS|METHOD", iAttr);
1343
0
        }
1344
0
        else if (d->m_bNodesWKT2 && EQUAL(pszNodeName, "PROJCS"))
1345
0
        {
1346
0
            return GetAttrValue("PROJCRS", iAttr);
1347
0
        }
1348
0
        return nullptr;
1349
0
    }
1350
1351
0
    if (iAttr < 0 || iAttr >= poNode->GetChildCount())
1352
0
        return nullptr;
1353
1354
0
    return poNode->GetChild(iAttr)->GetValue();
1355
0
}
1356
1357
/************************************************************************/
1358
/*                          OSRGetAttrValue()                           */
1359
/************************************************************************/
1360
1361
/**
1362
 * \brief Fetch indicated attribute of named node.
1363
 *
1364
 * This function is the same as OGRSpatialReference::GetAttrValue()
1365
 */
1366
const char *CPL_STDCALL OSRGetAttrValue(OGRSpatialReferenceH hSRS,
1367
                                        const char *pszKey, int iChild)
1368
1369
0
{
1370
0
    VALIDATE_POINTER1(hSRS, "OSRGetAttrValue", nullptr);
1371
1372
0
    return ToPointer(hSRS)->GetAttrValue(pszKey, iChild);
1373
0
}
1374
1375
/************************************************************************/
1376
/*                             GetName()                                */
1377
/************************************************************************/
1378
1379
/**
1380
 * \brief Return the CRS name.
1381
 *
1382
 * The returned value is only short lived and should not be used after other
1383
 * calls to methods on this object.
1384
 *
1385
 * @since GDAL 3.0
1386
 */
1387
1388
const char *OGRSpatialReference::GetName() const
1389
0
{
1390
0
    TAKE_OPTIONAL_LOCK();
1391
1392
0
    d->refreshProjObj();
1393
0
    if (!d->m_pj_crs)
1394
0
        return nullptr;
1395
0
    const char *pszName = proj_get_name(d->m_pj_crs);
1396
#if PROJ_VERSION_NUMBER == PROJ_COMPUTE_VERSION(8, 2, 0)
1397
    if (d->m_pjType == PJ_TYPE_BOUND_CRS && EQUAL(pszName, "SOURCECRS"))
1398
    {
1399
        // Work around a bug of PROJ 8.2.0 (fixed in 8.2.1)
1400
        PJ *baseCRS = proj_get_source_crs(d->getPROJContext(), d->m_pj_crs);
1401
        if (baseCRS)
1402
        {
1403
            pszName = proj_get_name(baseCRS);
1404
            // pszName still remains valid after proj_destroy(), since
1405
            // d->m_pj_crs keeps a reference to the base CRS C++ object.
1406
            proj_destroy(baseCRS);
1407
        }
1408
    }
1409
#endif
1410
0
    return pszName;
1411
0
}
1412
1413
/************************************************************************/
1414
/*                           OSRGetName()                               */
1415
/************************************************************************/
1416
1417
/**
1418
 * \brief Return the CRS name.
1419
 *
1420
 * The returned value is only short lived and should not be used after other
1421
 * calls to methods on this object.
1422
 *
1423
 * @since GDAL 3.0
1424
 */
1425
const char *OSRGetName(OGRSpatialReferenceH hSRS)
1426
1427
0
{
1428
0
    VALIDATE_POINTER1(hSRS, "OSRGetName", nullptr);
1429
1430
0
    return ToPointer(hSRS)->GetName();
1431
0
}
1432
1433
/************************************************************************/
1434
/*                       GetCelestialBodyName()                         */
1435
/************************************************************************/
1436
1437
/**
1438
 * \brief Return the name of the celestial body of this CRS.
1439
 *
1440
 * e.g. "Earth" for an Earth CRS
1441
 *
1442
 * The returned value is only short lived and should not be used after other
1443
 * calls to methods on this object.
1444
 *
1445
 * @since GDAL 3.12 and PROJ 8.1
1446
 */
1447
1448
const char *OGRSpatialReference::GetCelestialBodyName() const
1449
0
{
1450
0
#if PROJ_AT_LEAST_VERSION(8, 1, 0)
1451
1452
0
    TAKE_OPTIONAL_LOCK();
1453
1454
0
    d->refreshProjObj();
1455
0
    if (!d->m_pj_crs)
1456
0
        return nullptr;
1457
0
    d->demoteFromBoundCRS();
1458
0
    const char *name =
1459
0
        proj_get_celestial_body_name(d->getPROJContext(), d->m_pj_crs);
1460
0
    if (name)
1461
0
    {
1462
0
        d->m_celestialBodyName = name;
1463
0
    }
1464
0
    d->undoDemoteFromBoundCRS();
1465
0
    return d->m_celestialBodyName.c_str();
1466
#else
1467
    if (std::fabs(GetSemiMajor(nullptr) - SRS_WGS84_SEMIMAJOR) <=
1468
        0.05 * SRS_WGS84_SEMIMAJOR)
1469
        return "Earth";
1470
    const char *pszAuthName = GetAuthorityName(nullptr);
1471
    if (pszAuthName && EQUAL(pszAuthName, "EPSG"))
1472
        return "Earth";
1473
    return nullptr;
1474
#endif
1475
0
}
1476
1477
/************************************************************************/
1478
/*                       OSRGetCelestialBodyName()                      */
1479
/************************************************************************/
1480
1481
/**
1482
 * \brief Return the name of the celestial body of this CRS.
1483
 *
1484
 * e.g. "Earth" for an Earth CRS
1485
 *
1486
 * The returned value is only short lived and should not be used after other
1487
 * calls to methods on this object.
1488
 *
1489
 * @since GDAL 3.12 and PROJ 8.1
1490
 */
1491
1492
const char *OSRGetCelestialBodyName(OGRSpatialReferenceH hSRS)
1493
1494
0
{
1495
0
    VALIDATE_POINTER1(hSRS, "GetCelestialBodyName", nullptr);
1496
1497
0
    return ToPointer(hSRS)->GetCelestialBodyName();
1498
0
}
1499
1500
/************************************************************************/
1501
/*                               Clone()                                */
1502
/************************************************************************/
1503
1504
/**
1505
 * \brief Make a duplicate of this OGRSpatialReference.
1506
 *
1507
 * This method is the same as the C function OSRClone().
1508
 *
1509
 * @return a new SRS, which becomes the responsibility of the caller.
1510
 */
1511
1512
OGRSpatialReference *OGRSpatialReference::Clone() const
1513
1514
0
{
1515
0
    OGRSpatialReference *poNewRef = new OGRSpatialReference();
1516
1517
0
    TAKE_OPTIONAL_LOCK();
1518
1519
0
    d->refreshProjObj();
1520
0
    if (d->m_pj_crs != nullptr)
1521
0
        poNewRef->d->setPjCRS(proj_clone(d->getPROJContext(), d->m_pj_crs));
1522
0
    if (d->m_bHasCenterLong && d->m_poRoot)
1523
0
    {
1524
0
        poNewRef->d->setRoot(d->m_poRoot->Clone());
1525
0
    }
1526
0
    poNewRef->d->m_axisMapping = d->m_axisMapping;
1527
0
    poNewRef->d->m_axisMappingStrategy = d->m_axisMappingStrategy;
1528
0
    poNewRef->d->m_coordinateEpoch = d->m_coordinateEpoch;
1529
0
    return poNewRef;
1530
0
}
1531
1532
/************************************************************************/
1533
/*                              OSRClone()                              */
1534
/************************************************************************/
1535
1536
/**
1537
 * \brief Make a duplicate of this OGRSpatialReference.
1538
 *
1539
 * This function is the same as OGRSpatialReference::Clone()
1540
 */
1541
OGRSpatialReferenceH CPL_STDCALL OSRClone(OGRSpatialReferenceH hSRS)
1542
1543
0
{
1544
0
    VALIDATE_POINTER1(hSRS, "OSRClone", nullptr);
1545
1546
0
    return ToHandle(ToPointer(hSRS)->Clone());
1547
0
}
1548
1549
/************************************************************************/
1550
/*                            dumpReadable()                            */
1551
/************************************************************************/
1552
1553
/** Dump pretty wkt to stdout, mostly for debugging.
1554
 */
1555
void OGRSpatialReference::dumpReadable()
1556
1557
0
{
1558
0
    char *pszPrettyWkt = nullptr;
1559
1560
0
    const char *const apszOptions[] = {"FORMAT=WKT2", "MULTILINE=YES", nullptr};
1561
0
    exportToWkt(&pszPrettyWkt, apszOptions);
1562
0
    printf("%s\n", pszPrettyWkt); /*ok*/
1563
0
    CPLFree(pszPrettyWkt);
1564
0
}
1565
1566
/************************************************************************/
1567
/*                         exportToPrettyWkt()                          */
1568
/************************************************************************/
1569
1570
/**
1571
 * Convert this SRS into a nicely formatted WKT 1 string for display to a
1572
 * person.
1573
 *
1574
 * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
1575
 * Issues</a> page for implementation details of WKT 1 in OGR.
1576
 *
1577
 * Note that the returned WKT string should be freed with
1578
 * CPLFree() when no longer needed.  It is the responsibility of the caller.
1579
 *
1580
 * The WKT version can be overridden by using the OSR_WKT_FORMAT configuration
1581
 * option. Valid values are the one of the FORMAT option of
1582
 * exportToWkt( char ** ppszResult, const char* const* papszOptions ) const
1583
 *
1584
 * This method is the same as the C function OSRExportToPrettyWkt().
1585
 *
1586
 * @param ppszResult the resulting string is returned in this pointer.
1587
 * @param bSimplify TRUE if the AXIS, AUTHORITY and EXTENSION nodes should be
1588
 *   stripped off.
1589
 *
1590
 * @return OGRERR_NONE if successful.
1591
 */
1592
1593
OGRErr OGRSpatialReference::exportToPrettyWkt(char **ppszResult,
1594
                                              int bSimplify) const
1595
1596
0
{
1597
0
    CPLStringList aosOptions;
1598
0
    aosOptions.SetNameValue("MULTILINE", "YES");
1599
0
    if (bSimplify)
1600
0
    {
1601
0
        aosOptions.SetNameValue("FORMAT", "WKT1_SIMPLE");
1602
0
    }
1603
0
    return exportToWkt(ppszResult, aosOptions.List());
1604
0
}
1605
1606
/************************************************************************/
1607
/*                        OSRExportToPrettyWkt()                        */
1608
/************************************************************************/
1609
1610
/**
1611
 * \brief Convert this SRS into a nicely formatted WKT 1 string for display to a
1612
 * person.
1613
 *
1614
 * The WKT version can be overridden by using the OSR_WKT_FORMAT configuration
1615
 * option. Valid values are the one of the FORMAT option of
1616
 * exportToWkt( char ** ppszResult, const char* const* papszOptions ) const
1617
 *
1618
 * This function is the same as OGRSpatialReference::exportToPrettyWkt().
1619
 */
1620
1621
OGRErr CPL_STDCALL OSRExportToPrettyWkt(OGRSpatialReferenceH hSRS,
1622
                                        char **ppszReturn, int bSimplify)
1623
1624
0
{
1625
0
    VALIDATE_POINTER1(hSRS, "OSRExportToPrettyWkt", OGRERR_FAILURE);
1626
1627
0
    *ppszReturn = nullptr;
1628
1629
0
    return ToPointer(hSRS)->exportToPrettyWkt(ppszReturn, bSimplify);
1630
0
}
1631
1632
/************************************************************************/
1633
/*                            exportToWkt()                             */
1634
/************************************************************************/
1635
1636
/**
1637
 * \brief Convert this SRS into WKT 1 format.
1638
 *
1639
 * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
1640
 * Issues</a> page for implementation details of WKT 1 in OGR.
1641
 *
1642
 * Note that the returned WKT string should be freed with
1643
 * CPLFree() when no longer needed.  It is the responsibility of the caller.
1644
 *
1645
 * The WKT version can be overridden by using the OSR_WKT_FORMAT configuration
1646
 * option. Valid values are the one of the FORMAT option of
1647
 * exportToWkt( char ** ppszResult, const char* const* papszOptions ) const
1648
 *
1649
 * This method is the same as the C function OSRExportToWkt().
1650
 *
1651
 * @param ppszResult the resulting string is returned in this pointer.
1652
 *
1653
 * @return OGRERR_NONE if successful.
1654
 */
1655
1656
OGRErr OGRSpatialReference::exportToWkt(char **ppszResult) const
1657
1658
0
{
1659
0
    return exportToWkt(ppszResult, nullptr);
1660
0
}
1661
1662
/************************************************************************/
1663
/*                GDAL_proj_crs_create_bound_crs_to_WGS84()             */
1664
/************************************************************************/
1665
1666
static PJ *GDAL_proj_crs_create_bound_crs_to_WGS84(PJ_CONTEXT *ctx, PJ *pj,
1667
                                                   bool onlyIfEPSGCode,
1668
                                                   bool canModifyHorizPart)
1669
0
{
1670
0
    PJ *ret = nullptr;
1671
0
    if (proj_get_type(pj) == PJ_TYPE_COMPOUND_CRS)
1672
0
    {
1673
0
        auto horizCRS = proj_crs_get_sub_crs(ctx, pj, 0);
1674
0
        auto vertCRS = proj_crs_get_sub_crs(ctx, pj, 1);
1675
0
        if (horizCRS && proj_get_type(horizCRS) != PJ_TYPE_BOUND_CRS &&
1676
0
            vertCRS &&
1677
0
            (!onlyIfEPSGCode || proj_get_id_auth_name(horizCRS, 0) != nullptr))
1678
0
        {
1679
0
            auto boundHoriz =
1680
0
                canModifyHorizPart
1681
0
                    ? proj_crs_create_bound_crs_to_WGS84(ctx, horizCRS, nullptr)
1682
0
                    : proj_clone(ctx, horizCRS);
1683
0
            auto boundVert =
1684
0
                proj_crs_create_bound_crs_to_WGS84(ctx, vertCRS, nullptr);
1685
0
            if (boundHoriz && boundVert)
1686
0
            {
1687
0
                ret = proj_create_compound_crs(ctx, proj_get_name(pj),
1688
0
                                               boundHoriz, boundVert);
1689
0
            }
1690
0
            proj_destroy(boundHoriz);
1691
0
            proj_destroy(boundVert);
1692
0
        }
1693
0
        proj_destroy(horizCRS);
1694
0
        proj_destroy(vertCRS);
1695
0
    }
1696
0
    else if (proj_get_type(pj) != PJ_TYPE_BOUND_CRS &&
1697
0
             (!onlyIfEPSGCode || proj_get_id_auth_name(pj, 0) != nullptr))
1698
0
    {
1699
0
        ret = proj_crs_create_bound_crs_to_WGS84(ctx, pj, nullptr);
1700
0
    }
1701
0
    return ret;
1702
0
}
1703
1704
/************************************************************************/
1705
/*                            exportToWkt()                             */
1706
/************************************************************************/
1707
1708
/**
1709
 * Convert this SRS into a WKT string.
1710
 *
1711
 * Note that the returned WKT string should be freed with
1712
 * CPLFree() when no longer needed.  It is the responsibility of the caller.
1713
 *
1714
 * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
1715
 * Issues</a> page for implementation details of WKT 1 in OGR.
1716
 *
1717
 * @param ppszResult the resulting string is returned in this pointer.
1718
 * @param papszOptions NULL terminated list of options, or NULL. Currently
1719
 * supported options are
1720
 * <ul>
1721
 * <li>MULTILINE=YES/NO. Defaults to NO.</li>
1722
 * <li>FORMAT=SFSQL/WKT1_SIMPLE/WKT1/WKT1_GDAL/WKT1_ESRI/WKT2_2015/WKT2_2018/WKT2/DEFAULT.
1723
 *     If SFSQL, a WKT1 string without AXIS, TOWGS84, AUTHORITY or EXTENSION
1724
 *     node is returned.
1725
 *     If WKT1_SIMPLE, a WKT1 string without AXIS, AUTHORITY or EXTENSION
1726
 *     node is returned.
1727
 *     WKT1 is an alias of WKT1_GDAL.
1728
 *     WKT2 will default to the latest revision implemented (currently
1729
 *     WKT2_2018) WKT2_2019 can be used as an alias of WKT2_2018 since GDAL 3.2
1730
 * </li>
1731
 * <li>ALLOW_ELLIPSOIDAL_HEIGHT_AS_VERTICAL_CRS=YES/NO. Default is NO. If set
1732
 * to YES and FORMAT=WKT1_GDAL, a Geographic 3D CRS or a Projected 3D CRS will
1733
 * be exported as a compound CRS whose vertical part represents an ellipsoidal
1734
 * height (for example for use with LAS 1.4 WKT1).
1735
 * Requires PROJ 7.2.1 and GDAL 3.2.1.</li>
1736
 * </ul>
1737
 *
1738
 * Starting with GDAL 3.0.3, if the OSR_ADD_TOWGS84_ON_EXPORT_TO_WKT1
1739
 * configuration option is set to YES, when exporting to WKT1_GDAL, this method
1740
 * will try to add a TOWGS84[] node, if there's none attached yet to the SRS and
1741
 * if the SRS has a EPSG code. See the AddGuessedTOWGS84() method for how this
1742
 * TOWGS84[] node may be added.
1743
 *
1744
 * @return OGRERR_NONE if successful.
1745
 * @since GDAL 3.0
1746
 */
1747
1748
OGRErr OGRSpatialReference::exportToWkt(char **ppszResult,
1749
                                        const char *const *papszOptions) const
1750
0
{
1751
    // In the past calling this method was thread-safe, even if we never
1752
    // guaranteed it. Now proj_as_wkt() will cache the result internally,
1753
    // so this is no longer thread-safe.
1754
0
    std::lock_guard oLock(d->m_mutex);
1755
1756
0
    d->refreshProjObj();
1757
0
    if (!d->m_pj_crs)
1758
0
    {
1759
0
        *ppszResult = CPLStrdup("");
1760
0
        return OGRERR_FAILURE;
1761
0
    }
1762
1763
0
    if (d->m_bHasCenterLong && d->m_poRoot && !d->m_bMorphToESRI)
1764
0
    {
1765
0
        return d->m_poRoot->exportToWkt(ppszResult);
1766
0
    }
1767
1768
0
    auto ctxt = d->getPROJContext();
1769
0
    auto wktFormat = PJ_WKT1_GDAL;
1770
0
    const char *pszFormat =
1771
0
        CSLFetchNameValueDef(papszOptions, "FORMAT",
1772
0
                             CPLGetConfigOption("OSR_WKT_FORMAT", "DEFAULT"));
1773
0
    if (EQUAL(pszFormat, "DEFAULT"))
1774
0
        pszFormat = "";
1775
1776
0
    if (EQUAL(pszFormat, "WKT1_ESRI") || d->m_bMorphToESRI)
1777
0
    {
1778
0
        wktFormat = PJ_WKT1_ESRI;
1779
0
    }
1780
0
    else if (EQUAL(pszFormat, "WKT1") || EQUAL(pszFormat, "WKT1_GDAL") ||
1781
0
             EQUAL(pszFormat, "WKT1_SIMPLE") || EQUAL(pszFormat, "SFSQL"))
1782
0
    {
1783
0
        wktFormat = PJ_WKT1_GDAL;
1784
0
    }
1785
0
    else if (EQUAL(pszFormat, "WKT2_2015"))
1786
0
    {
1787
0
        wktFormat = PJ_WKT2_2015;
1788
0
    }
1789
0
    else if (EQUAL(pszFormat, "WKT2") || EQUAL(pszFormat, "WKT2_2018") ||
1790
0
             EQUAL(pszFormat, "WKT2_2019"))
1791
0
    {
1792
0
        wktFormat = PJ_WKT2_2018;
1793
0
    }
1794
0
    else if (pszFormat[0] == '\0')
1795
0
    {
1796
        // cppcheck-suppress knownConditionTrueFalse
1797
0
        if (IsDerivedGeographic())
1798
0
        {
1799
0
            wktFormat = PJ_WKT2_2018;
1800
0
        }
1801
0
        else if ((IsGeographic() || IsProjected()) && !IsCompound() &&
1802
0
                 GetAxesCount() == 3)
1803
0
        {
1804
0
            wktFormat = PJ_WKT2_2018;
1805
0
        }
1806
0
    }
1807
0
    else
1808
0
    {
1809
0
        CPLError(CE_Failure, CPLE_AppDefined, "Unsupported value for FORMAT");
1810
0
        *ppszResult = CPLStrdup("");
1811
0
        return OGRERR_FAILURE;
1812
0
    }
1813
1814
0
    CPLStringList aosOptions;
1815
0
    if (wktFormat != PJ_WKT1_ESRI)
1816
0
    {
1817
0
        aosOptions.SetNameValue("OUTPUT_AXIS", "YES");
1818
0
    }
1819
0
    aosOptions.SetNameValue(
1820
0
        "MULTILINE", CSLFetchNameValueDef(papszOptions, "MULTILINE", "NO"));
1821
1822
0
    const char *pszAllowEllpsHeightAsVertCS = CSLFetchNameValue(
1823
0
        papszOptions, "ALLOW_ELLIPSOIDAL_HEIGHT_AS_VERTICAL_CRS");
1824
0
    if (pszAllowEllpsHeightAsVertCS)
1825
0
    {
1826
0
        aosOptions.SetNameValue("ALLOW_ELLIPSOIDAL_HEIGHT_AS_VERTICAL_CRS",
1827
0
                                pszAllowEllpsHeightAsVertCS);
1828
0
    }
1829
1830
0
    PJ *boundCRS = nullptr;
1831
0
    if (wktFormat == PJ_WKT1_GDAL &&
1832
0
        CPLTestBool(CSLFetchNameValueDef(
1833
0
            papszOptions, "ADD_TOWGS84_ON_EXPORT_TO_WKT1",
1834
0
            CPLGetConfigOption("OSR_ADD_TOWGS84_ON_EXPORT_TO_WKT1", "NO"))))
1835
0
    {
1836
0
        boundCRS = GDAL_proj_crs_create_bound_crs_to_WGS84(
1837
0
            d->getPROJContext(), d->m_pj_crs, true, true);
1838
0
    }
1839
1840
0
    CPLErrorAccumulator oErrorAccumulator;
1841
0
    const char *pszWKT;
1842
0
    {
1843
0
        auto oAccumulator = oErrorAccumulator.InstallForCurrentScope();
1844
0
        CPL_IGNORE_RET_VAL(oAccumulator);
1845
0
        pszWKT = proj_as_wkt(ctxt, boundCRS ? boundCRS : d->m_pj_crs, wktFormat,
1846
0
                             aosOptions.List());
1847
0
    }
1848
0
    for (const auto &oError : oErrorAccumulator.GetErrors())
1849
0
    {
1850
0
        if (pszFormat[0] == '\0' &&
1851
0
            (oError.msg.find("Unsupported conversion method") !=
1852
0
                 std::string::npos ||
1853
0
             oError.msg.find("can only be exported to WKT2") !=
1854
0
                 std::string::npos ||
1855
0
             oError.msg.find("can only be exported since WKT2:2019") !=
1856
0
                 std::string::npos))
1857
0
        {
1858
0
            CPLErrorReset();
1859
            // If we cannot export in the default mode (WKT1), retry with WKT2
1860
0
            pszWKT = proj_as_wkt(ctxt, boundCRS ? boundCRS : d->m_pj_crs,
1861
0
                                 PJ_WKT2_2018, aosOptions.List());
1862
0
            break;
1863
0
        }
1864
0
        CPLError(oError.type, oError.no, "%s", oError.msg.c_str());
1865
0
    }
1866
1867
0
    if (!pszWKT)
1868
0
    {
1869
0
        *ppszResult = CPLStrdup("");
1870
0
        proj_destroy(boundCRS);
1871
0
        return OGRERR_FAILURE;
1872
0
    }
1873
1874
0
    if (EQUAL(pszFormat, "SFSQL") || EQUAL(pszFormat, "WKT1_SIMPLE"))
1875
0
    {
1876
0
        OGR_SRSNode oRoot;
1877
0
        oRoot.importFromWkt(&pszWKT);
1878
0
        oRoot.StripNodes("AXIS");
1879
0
        if (EQUAL(pszFormat, "SFSQL"))
1880
0
        {
1881
0
            oRoot.StripNodes("TOWGS84");
1882
0
        }
1883
0
        oRoot.StripNodes("AUTHORITY");
1884
0
        oRoot.StripNodes("EXTENSION");
1885
0
        OGRErr eErr;
1886
0
        if (CPLTestBool(CSLFetchNameValueDef(papszOptions, "MULTILINE", "NO")))
1887
0
            eErr = oRoot.exportToPrettyWkt(ppszResult, 1);
1888
0
        else
1889
0
            eErr = oRoot.exportToWkt(ppszResult);
1890
0
        proj_destroy(boundCRS);
1891
0
        return eErr;
1892
0
    }
1893
1894
0
    *ppszResult = CPLStrdup(pszWKT);
1895
1896
#if !(PROJ_AT_LEAST_VERSION(9, 5, 0))
1897
    if (wktFormat == PJ_WKT2_2018)
1898
    {
1899
        // Works around bug fixed per https://github.com/OSGeo/PROJ/pull/4166
1900
        // related to a wrong EPSG code assigned to UTM South conversions
1901
        char *pszPtr = strstr(*ppszResult, "CONVERSION[\"UTM zone ");
1902
        if (pszPtr)
1903
        {
1904
            pszPtr += strlen("CONVERSION[\"UTM zone ");
1905
            const int nZone = atoi(pszPtr);
1906
            while (*pszPtr >= '0' && *pszPtr <= '9')
1907
                ++pszPtr;
1908
            if (nZone >= 1 && nZone <= 60 && *pszPtr == 'S' &&
1909
                pszPtr[1] == '"' && pszPtr[2] == ',')
1910
            {
1911
                pszPtr += 3;
1912
                int nLevel = 0;
1913
                bool bInString = false;
1914
                // Find the ID node corresponding to this CONVERSION node
1915
                while (*pszPtr)
1916
                {
1917
                    if (bInString)
1918
                    {
1919
                        if (*pszPtr == '"' && pszPtr[1] == '"')
1920
                        {
1921
                            ++pszPtr;
1922
                        }
1923
                        else if (*pszPtr == '"')
1924
                        {
1925
                            bInString = false;
1926
                        }
1927
                    }
1928
                    else if (nLevel == 0 && STARTS_WITH_CI(pszPtr, "ID["))
1929
                    {
1930
                        if (STARTS_WITH_CI(pszPtr, CPLSPrintf("ID[\"EPSG\",%d]",
1931
                                                              17000 + nZone)))
1932
                        {
1933
                            CPLAssert(pszPtr[11] == '7');
1934
                            CPLAssert(pszPtr[12] == '0');
1935
                            pszPtr[11] = '6';
1936
                            pszPtr[12] = '1';
1937
                        }
1938
                        break;
1939
                    }
1940
                    else if (*pszPtr == '"')
1941
                    {
1942
                        bInString = true;
1943
                    }
1944
                    else if (*pszPtr == '[')
1945
                    {
1946
                        ++nLevel;
1947
                    }
1948
                    else if (*pszPtr == ']')
1949
                    {
1950
                        --nLevel;
1951
                    }
1952
1953
                    ++pszPtr;
1954
                }
1955
            }
1956
        }
1957
    }
1958
#endif
1959
1960
0
    proj_destroy(boundCRS);
1961
0
    return OGRERR_NONE;
1962
0
}
1963
1964
/************************************************************************/
1965
/*                            exportToWkt()                             */
1966
/************************************************************************/
1967
1968
/**
1969
 * Convert this SRS into a WKT string.
1970
 *
1971
 * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
1972
 * Issues</a> page for implementation details of WKT 1 in OGR.
1973
 *
1974
 * @param papszOptions NULL terminated list of options, or NULL. Currently
1975
 * supported options are
1976
 * <ul>
1977
 * <li>MULTILINE=YES/NO. Defaults to NO.</li>
1978
 * <li>FORMAT=SFSQL/WKT1_SIMPLE/WKT1/WKT1_GDAL/WKT1_ESRI/WKT2_2015/WKT2_2018/WKT2/DEFAULT.
1979
 *     If SFSQL, a WKT1 string without AXIS, TOWGS84, AUTHORITY or EXTENSION
1980
 *     node is returned.
1981
 *     If WKT1_SIMPLE, a WKT1 string without AXIS, AUTHORITY or EXTENSION
1982
 *     node is returned.
1983
 *     WKT1 is an alias of WKT1_GDAL.
1984
 *     WKT2 will default to the latest revision implemented (currently
1985
 *     WKT2_2019)
1986
 * </li>
1987
 * <li>ALLOW_ELLIPSOIDAL_HEIGHT_AS_VERTICAL_CRS=YES/NO. Default is NO. If set
1988
 * to YES and FORMAT=WKT1_GDAL, a Geographic 3D CRS or a Projected 3D CRS will
1989
 * be exported as a compound CRS whose vertical part represents an ellipsoidal
1990
 * height (for example for use with LAS 1.4 WKT1).
1991
 * Requires PROJ 7.2.1.</li>
1992
 * </ul>
1993
 *
1994
 * If the OSR_ADD_TOWGS84_ON_EXPORT_TO_WKT1
1995
 * configuration option is set to YES, when exporting to WKT1_GDAL, this method
1996
 * will try to add a TOWGS84[] node, if there's none attached yet to the SRS and
1997
 * if the SRS has a EPSG code. See the AddGuessedTOWGS84() method for how this
1998
 * TOWGS84[] node may be added.
1999
 *
2000
 * @return a non-empty string if successful.
2001
 * @since GDAL 3.9
2002
 */
2003
2004
std::string
2005
OGRSpatialReference::exportToWkt(const char *const *papszOptions) const
2006
0
{
2007
0
    std::string osWKT;
2008
0
    char *pszWKT = nullptr;
2009
0
    if (exportToWkt(&pszWKT, papszOptions) == OGRERR_NONE)
2010
0
        osWKT = pszWKT;
2011
0
    CPLFree(pszWKT);
2012
0
    return osWKT;
2013
0
}
2014
2015
/************************************************************************/
2016
/*                           OSRExportToWkt()                           */
2017
/************************************************************************/
2018
2019
/**
2020
 * \brief Convert this SRS into WKT 1 format.
2021
 *
2022
 * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2023
 * Issues</a> page for implementation details of WKT in OGR.
2024
 *
2025
 * The WKT version can be overridden by using the OSR_WKT_FORMAT configuration
2026
 * option. Valid values are the one of the FORMAT option of
2027
 * exportToWkt( char ** ppszResult, const char* const* papszOptions ) const
2028
 *
2029
 * This function is the same as OGRSpatialReference::exportToWkt().
2030
 */
2031
2032
OGRErr CPL_STDCALL OSRExportToWkt(OGRSpatialReferenceH hSRS, char **ppszReturn)
2033
2034
0
{
2035
0
    VALIDATE_POINTER1(hSRS, "OSRExportToWkt", OGRERR_FAILURE);
2036
2037
0
    *ppszReturn = nullptr;
2038
2039
0
    return ToPointer(hSRS)->exportToWkt(ppszReturn);
2040
0
}
2041
2042
/************************************************************************/
2043
/*                          OSRExportToWktEx()                          */
2044
/************************************************************************/
2045
2046
/**
2047
 * \brief Convert this SRS into WKT format.
2048
 *
2049
 * This function is the same as OGRSpatialReference::exportToWkt(char **
2050
 * ppszResult,const char* const* papszOptions ) const
2051
 *
2052
 * @since GDAL 3.0
2053
 */
2054
2055
OGRErr OSRExportToWktEx(OGRSpatialReferenceH hSRS, char **ppszReturn,
2056
                        const char *const *papszOptions)
2057
0
{
2058
0
    VALIDATE_POINTER1(hSRS, "OSRExportToWktEx", OGRERR_FAILURE);
2059
2060
0
    *ppszReturn = nullptr;
2061
2062
0
    return ToPointer(hSRS)->exportToWkt(ppszReturn, papszOptions);
2063
0
}
2064
2065
/************************************************************************/
2066
/*                       exportToPROJJSON()                             */
2067
/************************************************************************/
2068
2069
/**
2070
 * Convert this SRS into a PROJJSON string.
2071
 *
2072
 * Note that the returned JSON string should be freed with
2073
 * CPLFree() when no longer needed.  It is the responsibility of the caller.
2074
 *
2075
 * @param ppszResult the resulting string is returned in this pointer.
2076
 * @param papszOptions NULL terminated list of options, or NULL. Currently
2077
 * supported options are
2078
 * <ul>
2079
 * <li>MULTILINE=YES/NO. Defaults to YES</li>
2080
 * <li>INDENTATION_WIDTH=number. Defaults to 2 (when multiline output is
2081
 * on).</li>
2082
 * <li>SCHEMA=string. URL to PROJJSON schema. Can be set to empty string to
2083
 * disable it.</li>
2084
 * </ul>
2085
 *
2086
 * @return OGRERR_NONE if successful.
2087
 * @since GDAL 3.1 and PROJ 6.2
2088
 */
2089
2090
OGRErr OGRSpatialReference::exportToPROJJSON(
2091
    char **ppszResult, CPL_UNUSED const char *const *papszOptions) const
2092
0
{
2093
0
    TAKE_OPTIONAL_LOCK();
2094
2095
0
    d->refreshProjObj();
2096
0
    if (!d->m_pj_crs)
2097
0
    {
2098
0
        *ppszResult = nullptr;
2099
0
        return OGRERR_FAILURE;
2100
0
    }
2101
2102
0
    const char *pszPROJJSON =
2103
0
        proj_as_projjson(d->getPROJContext(), d->m_pj_crs, papszOptions);
2104
2105
0
    if (!pszPROJJSON)
2106
0
    {
2107
0
        *ppszResult = CPLStrdup("");
2108
0
        return OGRERR_FAILURE;
2109
0
    }
2110
2111
0
    *ppszResult = CPLStrdup(pszPROJJSON);
2112
2113
#if !(PROJ_AT_LEAST_VERSION(9, 5, 0))
2114
    {
2115
        // Works around bug fixed per https://github.com/OSGeo/PROJ/pull/4166
2116
        // related to a wrong EPSG code assigned to UTM South conversions
2117
        char *pszPtr = strstr(*ppszResult, "\"name\": \"UTM zone ");
2118
        if (pszPtr)
2119
        {
2120
            pszPtr += strlen("\"name\": \"UTM zone ");
2121
            const int nZone = atoi(pszPtr);
2122
            while (*pszPtr >= '0' && *pszPtr <= '9')
2123
                ++pszPtr;
2124
            if (nZone >= 1 && nZone <= 60 && *pszPtr == 'S' && pszPtr[1] == '"')
2125
            {
2126
                pszPtr += 2;
2127
                int nLevel = 0;
2128
                bool bInString = false;
2129
                // Find the id node corresponding to this conversion node
2130
                while (*pszPtr)
2131
                {
2132
                    if (bInString)
2133
                    {
2134
                        if (*pszPtr == '\\')
2135
                        {
2136
                            ++pszPtr;
2137
                        }
2138
                        else if (*pszPtr == '"')
2139
                        {
2140
                            bInString = false;
2141
                        }
2142
                    }
2143
                    else if (nLevel == 0 && STARTS_WITH(pszPtr, "\"id\": {"))
2144
                    {
2145
                        const char *pszNextEndCurl = strchr(pszPtr, '}');
2146
                        const char *pszAuthEPSG =
2147
                            strstr(pszPtr, "\"authority\": \"EPSG\"");
2148
                        char *pszCode = strstr(
2149
                            pszPtr, CPLSPrintf("\"code\": %d", 17000 + nZone));
2150
                        if (pszAuthEPSG && pszCode && pszNextEndCurl &&
2151
                            pszNextEndCurl - pszAuthEPSG > 0 &&
2152
                            pszNextEndCurl - pszCode > 0)
2153
                        {
2154
                            CPLAssert(pszCode[9] == '7');
2155
                            CPLAssert(pszCode[10] == '0');
2156
                            pszCode[9] = '6';
2157
                            pszCode[10] = '1';
2158
                        }
2159
                        break;
2160
                    }
2161
                    else if (*pszPtr == '"')
2162
                    {
2163
                        bInString = true;
2164
                    }
2165
                    else if (*pszPtr == '{' || *pszPtr == '[')
2166
                    {
2167
                        ++nLevel;
2168
                    }
2169
                    else if (*pszPtr == '}' || *pszPtr == ']')
2170
                    {
2171
                        --nLevel;
2172
                    }
2173
2174
                    ++pszPtr;
2175
                }
2176
            }
2177
        }
2178
    }
2179
#endif
2180
2181
0
    return OGRERR_NONE;
2182
0
}
2183
2184
/************************************************************************/
2185
/*                          OSRExportToPROJJSON()                       */
2186
/************************************************************************/
2187
2188
/**
2189
 * \brief Convert this SRS into PROJJSON format.
2190
 *
2191
 * This function is the same as OGRSpatialReference::exportToPROJJSON() const
2192
 *
2193
 * @since GDAL 3.1 and PROJ 6.2
2194
 */
2195
2196
OGRErr OSRExportToPROJJSON(OGRSpatialReferenceH hSRS, char **ppszReturn,
2197
                           const char *const *papszOptions)
2198
0
{
2199
0
    VALIDATE_POINTER1(hSRS, "OSRExportToPROJJSON", OGRERR_FAILURE);
2200
2201
0
    *ppszReturn = nullptr;
2202
2203
0
    return ToPointer(hSRS)->exportToPROJJSON(ppszReturn, papszOptions);
2204
0
}
2205
2206
/************************************************************************/
2207
/*                           importFromWkt()                            */
2208
/************************************************************************/
2209
2210
/**
2211
 * \brief Import from WKT string.
2212
 *
2213
 * This method will wipe the existing SRS definition, and
2214
 * reassign it based on the contents of the passed WKT string.  Only as
2215
 * much of the input string as needed to construct this SRS is consumed from
2216
 * the input string, and the input string pointer
2217
 * is then updated to point to the remaining (unused) input.
2218
 *
2219
 * Starting with PROJ 9.2, if invoked on a COORDINATEMETADATA[] construct,
2220
 * the CRS contained in it will be used to fill the OGRSpatialReference object,
2221
 * and the coordinate epoch potentially present used as the coordinate epoch
2222
 * property of the OGRSpatialReference object.
2223
 *
2224
 * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2225
 * Issues</a> page for implementation details of WKT in OGR.
2226
 *
2227
 * This method is the same as the C function OSRImportFromWkt().
2228
 *
2229
 * @param ppszInput Pointer to pointer to input.  The pointer is updated to
2230
 * point to remaining unused input text.
2231
 *
2232
 * @return OGRERR_NONE if import succeeds, or OGRERR_CORRUPT_DATA if it
2233
 * fails for any reason.
2234
 * @since GDAL 2.3
2235
 */
2236
2237
OGRErr OGRSpatialReference::importFromWkt(const char **ppszInput)
2238
2239
0
{
2240
0
    return importFromWkt(ppszInput, nullptr);
2241
0
}
2242
2243
/************************************************************************/
2244
/*                           importFromWkt()                            */
2245
/************************************************************************/
2246
2247
/*! @cond Doxygen_Suppress */
2248
2249
OGRErr OGRSpatialReference::importFromWkt(const char *pszInput,
2250
                                          CSLConstList papszOptions)
2251
2252
0
{
2253
0
    return importFromWkt(&pszInput, papszOptions);
2254
0
}
2255
2256
OGRErr OGRSpatialReference::importFromWkt(const char **ppszInput,
2257
                                          CSLConstList papszOptions)
2258
2259
0
{
2260
0
    TAKE_OPTIONAL_LOCK();
2261
2262
0
    if (!ppszInput || !*ppszInput)
2263
0
        return OGRERR_FAILURE;
2264
2265
0
    if (strlen(*ppszInput) > 100 * 1000 &&
2266
0
        CPLTestBool(CPLGetConfigOption("OSR_IMPORT_FROM_WKT_LIMIT", "YES")))
2267
0
    {
2268
0
        CPLError(CE_Failure, CPLE_NotSupported,
2269
0
                 "Suspiciously large input for importFromWkt(). Rejecting it. "
2270
0
                 "You can remove this limitation by definition the "
2271
0
                 "OSR_IMPORT_FROM_WKT_LIMIT configuration option to NO.");
2272
0
        return OGRERR_FAILURE;
2273
0
    }
2274
2275
0
    Clear();
2276
2277
0
    bool canCache = false;
2278
0
    auto tlsCache = OSRGetProjTLSCache();
2279
0
    std::string osWkt;
2280
0
    if (**ppszInput)
2281
0
    {
2282
0
        osWkt = *ppszInput;
2283
0
        auto cachedObj = tlsCache->GetPJForWKT(osWkt);
2284
0
        if (cachedObj)
2285
0
        {
2286
0
            d->setPjCRS(cachedObj);
2287
0
        }
2288
0
        else
2289
0
        {
2290
0
            CPLStringList aosOptions(papszOptions);
2291
0
            if (aosOptions.FetchNameValue("STRICT") == nullptr)
2292
0
                aosOptions.SetNameValue("STRICT", "NO");
2293
0
            PROJ_STRING_LIST warnings = nullptr;
2294
0
            PROJ_STRING_LIST errors = nullptr;
2295
0
            auto ctxt = d->getPROJContext();
2296
0
            auto pj = proj_create_from_wkt(ctxt, *ppszInput, aosOptions.List(),
2297
0
                                           &warnings, &errors);
2298
0
            d->setPjCRS(pj);
2299
2300
0
            for (auto iter = warnings; iter && *iter; ++iter)
2301
0
            {
2302
0
                d->m_wktImportWarnings.push_back(*iter);
2303
0
            }
2304
0
            for (auto iter = errors; iter && *iter; ++iter)
2305
0
            {
2306
0
                d->m_wktImportErrors.push_back(*iter);
2307
0
                if (!d->m_pj_crs)
2308
0
                {
2309
0
                    CPLError(CE_Failure, CPLE_AppDefined, "%s", *iter);
2310
0
                }
2311
0
            }
2312
0
            if (warnings == nullptr && errors == nullptr)
2313
0
            {
2314
0
                canCache = true;
2315
0
            }
2316
0
            proj_string_list_destroy(warnings);
2317
0
            proj_string_list_destroy(errors);
2318
0
        }
2319
0
    }
2320
0
    if (!d->m_pj_crs)
2321
0
        return OGRERR_CORRUPT_DATA;
2322
2323
    // Only accept CRS objects
2324
0
    if (!proj_is_crs(d->m_pj_crs))
2325
0
    {
2326
0
        Clear();
2327
0
        return OGRERR_CORRUPT_DATA;
2328
0
    }
2329
2330
0
    if (canCache)
2331
0
    {
2332
0
        tlsCache->CachePJForWKT(osWkt, d->m_pj_crs);
2333
0
    }
2334
2335
0
    if (strstr(*ppszInput, "CENTER_LONG"))
2336
0
    {
2337
0
        auto poRoot = new OGR_SRSNode();
2338
0
        d->setRoot(poRoot);
2339
0
        const char *pszTmp = *ppszInput;
2340
0
        poRoot->importFromWkt(&pszTmp);
2341
0
        d->m_bHasCenterLong = true;
2342
0
    }
2343
2344
    // TODO? we don't really update correctly since we assume that the
2345
    // passed string is only WKT.
2346
0
    *ppszInput += strlen(*ppszInput);
2347
0
    return OGRERR_NONE;
2348
2349
#if no_longer_implemented_for_now
2350
    /* -------------------------------------------------------------------- */
2351
    /*      The following seems to try and detect and unconsumed            */
2352
    /*      VERTCS[] coordinate system definition (ESRI style) and to       */
2353
    /*      import and attach it to the existing root.  Likely we will      */
2354
    /*      need to extend this somewhat to bring it into an acceptable     */
2355
    /*      OGRSpatialReference organization at some point.                 */
2356
    /* -------------------------------------------------------------------- */
2357
    if (strlen(*ppszInput) > 0 && strstr(*ppszInput, "VERTCS"))
2358
    {
2359
        if (((*ppszInput)[0]) == ',')
2360
            (*ppszInput)++;
2361
        OGR_SRSNode *poNewChild = new OGR_SRSNode();
2362
        poRoot->AddChild(poNewChild);
2363
        return poNewChild->importFromWkt(ppszInput);
2364
    }
2365
#endif
2366
0
}
2367
2368
/*! @endcond */
2369
2370
/**
2371
 * \brief Import from WKT string.
2372
 *
2373
 * This method will wipe the existing SRS definition, and
2374
 * reassign it based on the contents of the passed WKT string.  Only as
2375
 * much of the input string as needed to construct this SRS is consumed from
2376
 * the input string, and the input string pointer
2377
 * is then updated to point to the remaining (unused) input.
2378
 *
2379
 * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2380
 * Issues</a> page for implementation details of WKT in OGR.
2381
 *
2382
 * This method is the same as the C function OSRImportFromWkt().
2383
 *
2384
 * @param ppszInput Pointer to pointer to input.  The pointer is updated to
2385
 * point to remaining unused input text.
2386
 *
2387
 * @return OGRERR_NONE if import succeeds, or OGRERR_CORRUPT_DATA if it
2388
 * fails for any reason.
2389
 * @deprecated GDAL 2.3. Use importFromWkt(const char**) or importFromWkt(const
2390
 * char*)
2391
 */
2392
2393
OGRErr OGRSpatialReference::importFromWkt(char **ppszInput)
2394
2395
0
{
2396
0
    return importFromWkt(const_cast<const char **>(ppszInput));
2397
0
}
2398
2399
/**
2400
 * \brief Import from WKT string.
2401
 *
2402
 * This method will wipe the existing SRS definition, and
2403
 * reassign it based on the contents of the passed WKT string.  Only as
2404
 * much of the input string as needed to construct this SRS is consumed from
2405
 * the input string, and the input string pointer
2406
 * is then updated to point to the remaining (unused) input.
2407
 *
2408
 * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2409
 * Issues</a> page for implementation details of WKT in OGR.
2410
 *
2411
 * @param pszInput Input WKT
2412
 *
2413
 * @return OGRERR_NONE if import succeeds, or OGRERR_CORRUPT_DATA if it
2414
 * fails for any reason.
2415
 * @since GDAL 2.3
2416
 */
2417
2418
OGRErr OGRSpatialReference::importFromWkt(const char *pszInput)
2419
0
{
2420
0
    return importFromWkt(&pszInput);
2421
0
}
2422
2423
/************************************************************************/
2424
/*                              Validate()                              */
2425
/************************************************************************/
2426
2427
/**
2428
 * \brief Validate CRS imported with importFromWkt() or with modified with
2429
 * direct node manipulations. Otherwise the CRS should be always valid.
2430
 *
2431
 * This method attempts to verify that the spatial reference system is
2432
 * well formed, and consists of known tokens.  The validation is not
2433
 * comprehensive.
2434
 *
2435
 * This method is the same as the C function OSRValidate().
2436
 *
2437
 * @return OGRERR_NONE if all is fine, OGRERR_CORRUPT_DATA if the SRS is
2438
 * not well formed, and OGRERR_UNSUPPORTED_SRS if the SRS is well formed,
2439
 * but contains non-standard PROJECTION[] values.
2440
 */
2441
2442
OGRErr OGRSpatialReference::Validate() const
2443
2444
0
{
2445
0
    TAKE_OPTIONAL_LOCK();
2446
2447
0
    for (const auto &str : d->m_wktImportErrors)
2448
0
    {
2449
0
        CPLDebug("OGRSpatialReference::Validate", "%s", str.c_str());
2450
0
    }
2451
0
    for (const auto &str : d->m_wktImportWarnings)
2452
0
    {
2453
0
        CPLDebug("OGRSpatialReference::Validate", "%s", str.c_str());
2454
0
    }
2455
0
    if (!d->m_pj_crs || !d->m_wktImportErrors.empty())
2456
0
    {
2457
0
        return OGRERR_CORRUPT_DATA;
2458
0
    }
2459
0
    if (!d->m_wktImportWarnings.empty())
2460
0
    {
2461
0
        return OGRERR_UNSUPPORTED_SRS;
2462
0
    }
2463
0
    return OGRERR_NONE;
2464
0
}
2465
2466
/************************************************************************/
2467
/*                            OSRValidate()                             */
2468
/************************************************************************/
2469
/**
2470
 * \brief Validate SRS tokens.
2471
 *
2472
 * This function is the same as the C++ method OGRSpatialReference::Validate().
2473
 */
2474
OGRErr OSRValidate(OGRSpatialReferenceH hSRS)
2475
2476
0
{
2477
0
    VALIDATE_POINTER1(hSRS, "OSRValidate", OGRERR_FAILURE);
2478
2479
0
    return OGRSpatialReference::FromHandle(hSRS)->Validate();
2480
0
}
2481
2482
/************************************************************************/
2483
/*                          OSRImportFromWkt()                          */
2484
/************************************************************************/
2485
2486
/**
2487
 * \brief Import from WKT string.
2488
 *
2489
 * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2490
 * Issues</a> page for implementation details of WKT in OGR.
2491
 *
2492
 * This function is the same as OGRSpatialReference::importFromWkt().
2493
 */
2494
2495
OGRErr OSRImportFromWkt(OGRSpatialReferenceH hSRS, char **ppszInput)
2496
2497
0
{
2498
0
    VALIDATE_POINTER1(hSRS, "OSRImportFromWkt", OGRERR_FAILURE);
2499
2500
0
    return ToPointer(hSRS)->importFromWkt(const_cast<const char **>(ppszInput));
2501
0
}
2502
2503
/************************************************************************/
2504
/*                              SetNode()                               */
2505
/************************************************************************/
2506
2507
/**
2508
 * \brief Set attribute value in spatial reference.
2509
 *
2510
 * Missing intermediate nodes in the path will be created if not already
2511
 * in existence.  If the attribute has no children one will be created and
2512
 * assigned the value otherwise the zeroth child will be assigned the value.
2513
 *
2514
 * This method does the same as the C function OSRSetAttrValue().
2515
 *
2516
 * @param pszNodePath full path to attribute to be set.  For instance
2517
 * "PROJCS|GEOGCS|UNIT".
2518
 *
2519
 * @param pszNewNodeValue value to be assigned to node, such as "meter".
2520
 * This may be NULL if you just want to force creation of the intermediate
2521
 * path.
2522
 *
2523
 * @return OGRERR_NONE on success.
2524
 */
2525
2526
OGRErr OGRSpatialReference::SetNode(const char *pszNodePath,
2527
                                    const char *pszNewNodeValue)
2528
2529
0
{
2530
0
    TAKE_OPTIONAL_LOCK();
2531
2532
0
    char **papszPathTokens =
2533
0
        CSLTokenizeStringComplex(pszNodePath, "|", TRUE, FALSE);
2534
2535
0
    if (CSLCount(papszPathTokens) < 1)
2536
0
    {
2537
0
        CSLDestroy(papszPathTokens);
2538
0
        return OGRERR_FAILURE;
2539
0
    }
2540
2541
0
    if (GetRoot() == nullptr ||
2542
0
        !EQUAL(papszPathTokens[0], GetRoot()->GetValue()))
2543
0
    {
2544
0
        if (EQUAL(papszPathTokens[0], "PROJCS") &&
2545
0
            CSLCount(papszPathTokens) == 1)
2546
0
        {
2547
0
            CSLDestroy(papszPathTokens);
2548
0
            return SetProjCS(pszNewNodeValue);
2549
0
        }
2550
0
        else
2551
0
        {
2552
0
            SetRoot(new OGR_SRSNode(papszPathTokens[0]));
2553
0
        }
2554
0
    }
2555
2556
0
    OGR_SRSNode *poNode = GetRoot();
2557
0
    for (int i = 1; papszPathTokens[i] != nullptr; i++)
2558
0
    {
2559
0
        int j = 0;  // Used after for.
2560
2561
0
        for (; j < poNode->GetChildCount(); j++)
2562
0
        {
2563
0
            if (EQUAL(poNode->GetChild(j)->GetValue(), papszPathTokens[i]))
2564
0
            {
2565
0
                poNode = poNode->GetChild(j);
2566
0
                j = -1;
2567
0
                break;
2568
0
            }
2569
0
        }
2570
2571
0
        if (j != -1)
2572
0
        {
2573
0
            OGR_SRSNode *poNewNode = new OGR_SRSNode(papszPathTokens[i]);
2574
0
            poNode->AddChild(poNewNode);
2575
0
            poNode = poNewNode;
2576
0
        }
2577
0
    }
2578
2579
0
    CSLDestroy(papszPathTokens);
2580
2581
0
    if (pszNewNodeValue != nullptr)
2582
0
    {
2583
0
        if (poNode->GetChildCount() > 0)
2584
0
            poNode->GetChild(0)->SetValue(pszNewNodeValue);
2585
0
        else
2586
0
            poNode->AddChild(new OGR_SRSNode(pszNewNodeValue));
2587
0
    };
2588
0
    return OGRERR_NONE;
2589
0
}
2590
2591
/************************************************************************/
2592
/*                          OSRSetAttrValue()                           */
2593
/************************************************************************/
2594
2595
/**
2596
 * \brief Set attribute value in spatial reference.
2597
 *
2598
 * This function is the same as OGRSpatialReference::SetNode()
2599
 */
2600
OGRErr CPL_STDCALL OSRSetAttrValue(OGRSpatialReferenceH hSRS,
2601
                                   const char *pszPath, const char *pszValue)
2602
2603
0
{
2604
0
    VALIDATE_POINTER1(hSRS, "OSRSetAttrValue", OGRERR_FAILURE);
2605
2606
0
    return ToPointer(hSRS)->SetNode(pszPath, pszValue);
2607
0
}
2608
2609
/************************************************************************/
2610
/*                              SetNode()                               */
2611
/************************************************************************/
2612
2613
/**
2614
 * \brief Set attribute value in spatial reference.
2615
 *
2616
 * Missing intermediate nodes in the path will be created if not already
2617
 * in existence.  If the attribute has no children one will be created and
2618
 * assigned the value otherwise the zeroth child will be assigned the value.
2619
 *
2620
 * This method does the same as the C function OSRSetAttrValue().
2621
 *
2622
 * @param pszNodePath full path to attribute to be set.  For instance
2623
 * "PROJCS|GEOGCS|UNIT".
2624
 *
2625
 * @param dfValue value to be assigned to node.
2626
 *
2627
 * @return OGRERR_NONE on success.
2628
 */
2629
2630
OGRErr OGRSpatialReference::SetNode(const char *pszNodePath, double dfValue)
2631
2632
0
{
2633
0
    char szValue[64] = {'\0'};
2634
2635
0
    if (std::abs(dfValue - static_cast<int>(dfValue)) == 0.0)
2636
0
        snprintf(szValue, sizeof(szValue), "%d", static_cast<int>(dfValue));
2637
0
    else
2638
0
        OGRsnPrintDouble(szValue, sizeof(szValue), dfValue);
2639
2640
0
    return SetNode(pszNodePath, szValue);
2641
0
}
2642
2643
/************************************************************************/
2644
/*                          SetAngularUnits()                           */
2645
/************************************************************************/
2646
2647
/**
2648
 * \brief Set the angular units for the geographic coordinate system.
2649
 *
2650
 * This method creates a UNIT subnode with the specified values as a
2651
 * child of the GEOGCS node.
2652
 *
2653
 * This method does the same as the C function OSRSetAngularUnits().
2654
 *
2655
 * @param pszUnitsName the units name to be used.  Some preferred units
2656
 * names can be found in ogr_srs_api.h such as SRS_UA_DEGREE.
2657
 *
2658
 * @param dfInRadians the value to multiple by an angle in the indicated
2659
 * units to transform to radians.  Some standard conversion factors can
2660
 * be found in ogr_srs_api.h.
2661
 *
2662
 * @return OGRERR_NONE on success.
2663
 */
2664
2665
OGRErr OGRSpatialReference::SetAngularUnits(const char *pszUnitsName,
2666
                                            double dfInRadians)
2667
2668
0
{
2669
0
    TAKE_OPTIONAL_LOCK();
2670
2671
0
    d->bNormInfoSet = FALSE;
2672
2673
0
    d->refreshProjObj();
2674
0
    if (!d->m_pj_crs)
2675
0
        return OGRERR_FAILURE;
2676
0
    auto geodCRS = proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
2677
0
    if (!geodCRS)
2678
0
        return OGRERR_FAILURE;
2679
0
    proj_destroy(geodCRS);
2680
0
    d->demoteFromBoundCRS();
2681
0
    d->setPjCRS(proj_crs_alter_cs_angular_unit(d->getPROJContext(), d->m_pj_crs,
2682
0
                                               pszUnitsName, dfInRadians,
2683
0
                                               nullptr, nullptr));
2684
0
    d->undoDemoteFromBoundCRS();
2685
2686
0
    d->m_osAngularUnits = pszUnitsName;
2687
0
    d->m_dfAngularUnitToRadian = dfInRadians;
2688
2689
0
    return OGRERR_NONE;
2690
0
}
2691
2692
/************************************************************************/
2693
/*                         OSRSetAngularUnits()                         */
2694
/************************************************************************/
2695
2696
/**
2697
 * \brief Set the angular units for the geographic coordinate system.
2698
 *
2699
 * This function is the same as OGRSpatialReference::SetAngularUnits()
2700
 */
2701
OGRErr OSRSetAngularUnits(OGRSpatialReferenceH hSRS, const char *pszUnits,
2702
                          double dfInRadians)
2703
2704
0
{
2705
0
    VALIDATE_POINTER1(hSRS, "OSRSetAngularUnits", OGRERR_FAILURE);
2706
2707
0
    return ToPointer(hSRS)->SetAngularUnits(pszUnits, dfInRadians);
2708
0
}
2709
2710
/************************************************************************/
2711
/*                          GetAngularUnits()                           */
2712
/************************************************************************/
2713
2714
/**
2715
 * \brief Fetch angular geographic coordinate system units.
2716
 *
2717
 * If no units are available, a value of "degree" and SRS_UA_DEGREE_CONV
2718
 * will be assumed.  This method only checks directly under the GEOGCS node
2719
 * for units.
2720
 *
2721
 * This method does the same thing as the C function OSRGetAngularUnits().
2722
 *
2723
 * @param ppszName a pointer to be updated with the pointer to the units name.
2724
 * The returned value remains internal to the OGRSpatialReference and should
2725
 * not be freed, or modified.  It may be invalidated on the next
2726
 * OGRSpatialReference call.
2727
 *
2728
 * @return the value to multiply by angular distances to transform them to
2729
 * radians.
2730
 * @since GDAL 2.3.0
2731
 */
2732
2733
double OGRSpatialReference::GetAngularUnits(const char **ppszName) const
2734
2735
0
{
2736
0
    TAKE_OPTIONAL_LOCK();
2737
2738
0
    d->refreshProjObj();
2739
2740
0
    if (!d->m_osAngularUnits.empty())
2741
0
    {
2742
0
        if (ppszName != nullptr)
2743
0
            *ppszName = d->m_osAngularUnits.c_str();
2744
0
        return d->m_dfAngularUnitToRadian;
2745
0
    }
2746
2747
0
    do
2748
0
    {
2749
0
        if (d->m_pj_crs == nullptr || d->m_pjType == PJ_TYPE_ENGINEERING_CRS)
2750
0
        {
2751
0
            break;
2752
0
        }
2753
2754
0
        auto geodCRS =
2755
0
            proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
2756
0
        if (!geodCRS)
2757
0
        {
2758
0
            break;
2759
0
        }
2760
0
        auto coordSys =
2761
0
            proj_crs_get_coordinate_system(d->getPROJContext(), geodCRS);
2762
0
        proj_destroy(geodCRS);
2763
0
        if (!coordSys)
2764
0
        {
2765
0
            break;
2766
0
        }
2767
0
        if (proj_cs_get_type(d->getPROJContext(), coordSys) !=
2768
0
            PJ_CS_TYPE_ELLIPSOIDAL)
2769
0
        {
2770
0
            proj_destroy(coordSys);
2771
0
            break;
2772
0
        }
2773
2774
0
        double dfConvFactor = 0.0;
2775
0
        const char *pszUnitName = nullptr;
2776
0
        if (!proj_cs_get_axis_info(d->getPROJContext(), coordSys, 0, nullptr,
2777
0
                                   nullptr, nullptr, &dfConvFactor,
2778
0
                                   &pszUnitName, nullptr, nullptr))
2779
0
        {
2780
0
            proj_destroy(coordSys);
2781
0
            break;
2782
0
        }
2783
2784
0
        d->m_osAngularUnits = pszUnitName;
2785
2786
0
        proj_destroy(coordSys);
2787
0
        d->m_dfAngularUnitToRadian = dfConvFactor;
2788
0
    } while (false);
2789
2790
0
    if (d->m_osAngularUnits.empty())
2791
0
    {
2792
0
        d->m_osAngularUnits = "degree";
2793
0
        d->m_dfAngularUnitToRadian = CPLAtof(SRS_UA_DEGREE_CONV);
2794
0
    }
2795
2796
0
    if (ppszName != nullptr)
2797
0
        *ppszName = d->m_osAngularUnits.c_str();
2798
0
    return d->m_dfAngularUnitToRadian;
2799
0
}
2800
2801
/**
2802
 * \brief Fetch angular geographic coordinate system units.
2803
 *
2804
 * If no units are available, a value of "degree" and SRS_UA_DEGREE_CONV
2805
 * will be assumed.  This method only checks directly under the GEOGCS node
2806
 * for units.
2807
 *
2808
 * This method does the same thing as the C function OSRGetAngularUnits().
2809
 *
2810
 * @param ppszName a pointer to be updated with the pointer to the units name.
2811
 * The returned value remains internal to the OGRSpatialReference and should
2812
 * not be freed, or modified.  It may be invalidated on the next
2813
 * OGRSpatialReference call.
2814
 *
2815
 * @return the value to multiply by angular distances to transform them to
2816
 * radians.
2817
 * @deprecated GDAL 2.3.0. Use GetAngularUnits(const char**) const.
2818
 */
2819
2820
double OGRSpatialReference::GetAngularUnits(char **ppszName) const
2821
2822
0
{
2823
0
    return GetAngularUnits(const_cast<const char **>(ppszName));
2824
0
}
2825
2826
/************************************************************************/
2827
/*                         OSRGetAngularUnits()                         */
2828
/************************************************************************/
2829
2830
/**
2831
 * \brief Fetch angular geographic coordinate system units.
2832
 *
2833
 * This function is the same as OGRSpatialReference::GetAngularUnits()
2834
 */
2835
double OSRGetAngularUnits(OGRSpatialReferenceH hSRS, char **ppszName)
2836
2837
0
{
2838
0
    VALIDATE_POINTER1(hSRS, "OSRGetAngularUnits", 0);
2839
2840
0
    return ToPointer(hSRS)->GetAngularUnits(
2841
0
        const_cast<const char **>(ppszName));
2842
0
}
2843
2844
/************************************************************************/
2845
/*                 SetLinearUnitsAndUpdateParameters()                  */
2846
/************************************************************************/
2847
2848
/**
2849
 * \brief Set the linear units for the projection.
2850
 *
2851
 * This method creates a UNIT subnode with the specified values as a
2852
 * child of the PROJCS or LOCAL_CS node.   It works the same as the
2853
 * SetLinearUnits() method, but it also updates all existing linear
2854
 * projection parameter values from the old units to the new units.
2855
 *
2856
 * @param pszName the units name to be used.  Some preferred units
2857
 * names can be found in ogr_srs_api.h such as SRS_UL_METER, SRS_UL_FOOT
2858
 * and SRS_UL_US_FOOT.
2859
 *
2860
 * @param dfInMeters the value to multiple by a length in the indicated
2861
 * units to transform to meters.  Some standard conversion factors can
2862
 * be found in ogr_srs_api.h.
2863
 *
2864
 * @param pszUnitAuthority Unit authority name. Or nullptr
2865
 *
2866
 * @param pszUnitCode Unit code. Or nullptr
2867
 *
2868
 * @return OGRERR_NONE on success.
2869
 */
2870
2871
OGRErr OGRSpatialReference::SetLinearUnitsAndUpdateParameters(
2872
    const char *pszName, double dfInMeters, const char *pszUnitAuthority,
2873
    const char *pszUnitCode)
2874
2875
0
{
2876
0
    TAKE_OPTIONAL_LOCK();
2877
2878
0
    if (dfInMeters <= 0.0)
2879
0
        return OGRERR_FAILURE;
2880
2881
0
    d->refreshProjObj();
2882
0
    if (!d->m_pj_crs)
2883
0
        return OGRERR_FAILURE;
2884
2885
0
    d->demoteFromBoundCRS();
2886
0
    if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
2887
0
    {
2888
0
        d->setPjCRS(proj_crs_alter_parameters_linear_unit(
2889
0
            d->getPROJContext(), d->m_pj_crs, pszName, dfInMeters,
2890
0
            pszUnitAuthority, pszUnitCode, true));
2891
0
    }
2892
0
    d->setPjCRS(proj_crs_alter_cs_linear_unit(d->getPROJContext(), d->m_pj_crs,
2893
0
                                              pszName, dfInMeters,
2894
0
                                              pszUnitAuthority, pszUnitCode));
2895
0
    d->undoDemoteFromBoundCRS();
2896
2897
0
    d->m_osLinearUnits = pszName;
2898
0
    d->dfToMeter = dfInMeters;
2899
2900
0
    return OGRERR_NONE;
2901
0
}
2902
2903
/************************************************************************/
2904
/*                OSRSetLinearUnitsAndUpdateParameters()                */
2905
/************************************************************************/
2906
2907
/**
2908
 * \brief Set the linear units for the projection.
2909
 *
2910
 * This function is the same as
2911
 *   OGRSpatialReference::SetLinearUnitsAndUpdateParameters()
2912
 */
2913
OGRErr OSRSetLinearUnitsAndUpdateParameters(OGRSpatialReferenceH hSRS,
2914
                                            const char *pszUnits,
2915
                                            double dfInMeters)
2916
2917
0
{
2918
0
    VALIDATE_POINTER1(hSRS, "OSRSetLinearUnitsAndUpdateParameters",
2919
0
                      OGRERR_FAILURE);
2920
2921
0
    return ToPointer(hSRS)->SetLinearUnitsAndUpdateParameters(pszUnits,
2922
0
                                                              dfInMeters);
2923
0
}
2924
2925
/************************************************************************/
2926
/*                           SetLinearUnits()                           */
2927
/************************************************************************/
2928
2929
/**
2930
 * \brief Set the linear units for the projection.
2931
 *
2932
 * This method creates a UNIT subnode with the specified values as a
2933
 * child of the PROJCS, GEOCCS, GEOGCS or LOCAL_CS node. When called on a
2934
 * Geographic 3D CRS the vertical axis units will be set.
2935
 *
2936
 * This method does the same as the C function OSRSetLinearUnits().
2937
 *
2938
 * @param pszUnitsName the units name to be used.  Some preferred units
2939
 * names can be found in ogr_srs_api.h such as SRS_UL_METER, SRS_UL_FOOT
2940
 * and SRS_UL_US_FOOT.
2941
 *
2942
 * @param dfInMeters the value to multiple by a length in the indicated
2943
 * units to transform to meters.  Some standard conversion factors can
2944
 * be found in ogr_srs_api.h.
2945
 *
2946
 * @return OGRERR_NONE on success.
2947
 */
2948
2949
OGRErr OGRSpatialReference::SetLinearUnits(const char *pszUnitsName,
2950
                                           double dfInMeters)
2951
2952
0
{
2953
0
    return SetTargetLinearUnits(nullptr, pszUnitsName, dfInMeters);
2954
0
}
2955
2956
/************************************************************************/
2957
/*                         OSRSetLinearUnits()                          */
2958
/************************************************************************/
2959
2960
/**
2961
 * \brief Set the linear units for the projection.
2962
 *
2963
 * This function is the same as OGRSpatialReference::SetLinearUnits()
2964
 */
2965
OGRErr OSRSetLinearUnits(OGRSpatialReferenceH hSRS, const char *pszUnits,
2966
                         double dfInMeters)
2967
2968
0
{
2969
0
    VALIDATE_POINTER1(hSRS, "OSRSetLinearUnits", OGRERR_FAILURE);
2970
2971
0
    return ToPointer(hSRS)->SetLinearUnits(pszUnits, dfInMeters);
2972
0
}
2973
2974
/************************************************************************/
2975
/*                        SetTargetLinearUnits()                        */
2976
/************************************************************************/
2977
2978
/**
2979
 * \brief Set the linear units for the projection.
2980
 *
2981
 * This method creates a UNIT subnode with the specified values as a
2982
 * child of the target node.
2983
 *
2984
 * This method does the same as the C function OSRSetTargetLinearUnits().
2985
 *
2986
 * @param pszTargetKey the keyword to set the linear units for.
2987
 * i.e. "PROJCS" or "VERT_CS"
2988
 *
2989
 * @param pszUnitsName the units name to be used.  Some preferred units
2990
 * names can be found in ogr_srs_api.h such as SRS_UL_METER, SRS_UL_FOOT
2991
 * and SRS_UL_US_FOOT.
2992
 *
2993
 * @param dfInMeters the value to multiple by a length in the indicated
2994
 * units to transform to meters.  Some standard conversion factors can
2995
 * be found in ogr_srs_api.h.
2996
 *
2997
 * @param pszUnitAuthority Unit authority name. Or nullptr
2998
 *
2999
 * @param pszUnitCode Unit code. Or nullptr
3000
 *
3001
 * @return OGRERR_NONE on success.
3002
 *
3003
 * @since OGR 1.9.0
3004
 */
3005
3006
OGRErr OGRSpatialReference::SetTargetLinearUnits(const char *pszTargetKey,
3007
                                                 const char *pszUnitsName,
3008
                                                 double dfInMeters,
3009
                                                 const char *pszUnitAuthority,
3010
                                                 const char *pszUnitCode)
3011
3012
0
{
3013
0
    TAKE_OPTIONAL_LOCK();
3014
3015
0
    if (dfInMeters <= 0.0)
3016
0
        return OGRERR_FAILURE;
3017
3018
0
    d->refreshProjObj();
3019
0
    pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
3020
0
    if (pszTargetKey == nullptr)
3021
0
    {
3022
0
        if (!d->m_pj_crs)
3023
0
            return OGRERR_FAILURE;
3024
3025
0
        d->demoteFromBoundCRS();
3026
0
        if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
3027
0
        {
3028
0
            d->setPjCRS(proj_crs_alter_parameters_linear_unit(
3029
0
                d->getPROJContext(), d->m_pj_crs, pszUnitsName, dfInMeters,
3030
0
                pszUnitAuthority, pszUnitCode, false));
3031
0
        }
3032
0
        d->setPjCRS(proj_crs_alter_cs_linear_unit(
3033
0
            d->getPROJContext(), d->m_pj_crs, pszUnitsName, dfInMeters,
3034
0
            pszUnitAuthority, pszUnitCode));
3035
0
        d->undoDemoteFromBoundCRS();
3036
3037
0
        d->m_osLinearUnits = pszUnitsName;
3038
0
        d->dfToMeter = dfInMeters;
3039
3040
0
        return OGRERR_NONE;
3041
0
    }
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
0
    else
3054
0
        OGRsnPrintDouble(szValue, sizeof(szValue), dfInMeters);
3055
3056
0
    OGR_SRSNode *poUnits = nullptr;
3057
0
    if (poCS->FindChild("UNIT") >= 0)
3058
0
    {
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
0
    }
3067
0
    else
3068
0
    {
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
0
    }
3075
3076
0
    return OGRERR_NONE;
3077
0
}
3078
3079
/************************************************************************/
3080
/*                         OSRSetLinearUnits()                          */
3081
/************************************************************************/
3082
3083
/**
3084
 * \brief Set the linear units for the target node.
3085
 *
3086
 * This function is the same as OGRSpatialReference::SetTargetLinearUnits()
3087
 *
3088
 * @since OGR 1.9.0
3089
 */
3090
OGRErr OSRSetTargetLinearUnits(OGRSpatialReferenceH hSRS,
3091
                               const char *pszTargetKey, const char *pszUnits,
3092
                               double dfInMeters)
3093
3094
0
{
3095
0
    VALIDATE_POINTER1(hSRS, "OSRSetTargetLinearUnits", OGRERR_FAILURE);
3096
3097
0
    return ToPointer(hSRS)->SetTargetLinearUnits(pszTargetKey, pszUnits,
3098
0
                                                 dfInMeters);
3099
0
}
3100
3101
/************************************************************************/
3102
/*                           GetLinearUnits()                           */
3103
/************************************************************************/
3104
3105
/**
3106
 * \brief Fetch linear projection units.
3107
 *
3108
 * If no units are available, a value of "Meters" and 1.0 will be assumed.
3109
 * This method only checks directly under the PROJCS, GEOCCS, GEOGCS or
3110
 * LOCAL_CS node for units. When called on a Geographic 3D CRS the vertical
3111
 * axis units will be returned.
3112
 *
3113
 * This method does the same thing as the C function OSRGetLinearUnits()
3114
 *
3115
 * @param ppszName a pointer to be updated with the pointer to the units name.
3116
 * The returned value remains internal to the OGRSpatialReference and should
3117
 * not be freed, or modified.  It may be invalidated on the next
3118
 * OGRSpatialReference call.
3119
 *
3120
 * @return the value to multiply by linear distances to transform them to
3121
 * meters.
3122
 * @deprecated GDAL 2.3.0. Use GetLinearUnits(const char**) const.
3123
 */
3124
3125
double OGRSpatialReference::GetLinearUnits(char **ppszName) const
3126
3127
0
{
3128
0
    return GetTargetLinearUnits(nullptr, const_cast<const char **>(ppszName));
3129
0
}
3130
3131
/**
3132
 * \brief Fetch linear projection units.
3133
 *
3134
 * If no units are available, a value of "Meters" and 1.0 will be assumed.
3135
 * This method only checks directly under the PROJCS, GEOCCS or LOCAL_CS node
3136
 * for units.
3137
 *
3138
 * This method does the same thing as the C function OSRGetLinearUnits()
3139
 *
3140
 * @param ppszName a pointer to be updated with the pointer to the units name.
3141
 * The returned value remains internal to the OGRSpatialReference and should
3142
 * not be freed, or modified.  It may be invalidated on the next
3143
 * OGRSpatialReference call.
3144
 *
3145
 * @return the value to multiply by linear distances to transform them to
3146
 * meters.
3147
 * @since GDAL 2.3.0
3148
 */
3149
3150
double OGRSpatialReference::GetLinearUnits(const char **ppszName) const
3151
3152
0
{
3153
0
    return GetTargetLinearUnits(nullptr, ppszName);
3154
0
}
3155
3156
/************************************************************************/
3157
/*                         OSRGetLinearUnits()                          */
3158
/************************************************************************/
3159
3160
/**
3161
 * \brief Fetch linear projection units.
3162
 *
3163
 * This function is the same as OGRSpatialReference::GetLinearUnits()
3164
 */
3165
double OSRGetLinearUnits(OGRSpatialReferenceH hSRS, char **ppszName)
3166
3167
0
{
3168
0
    VALIDATE_POINTER1(hSRS, "OSRGetLinearUnits", 0);
3169
3170
0
    return ToPointer(hSRS)->GetLinearUnits(const_cast<const char **>(ppszName));
3171
0
}
3172
3173
/************************************************************************/
3174
/*                        GetTargetLinearUnits()                        */
3175
/************************************************************************/
3176
3177
/**
3178
 * \brief Fetch linear units for target.
3179
 *
3180
 * If no units are available, a value of "Meters" and 1.0 will be assumed.
3181
 *
3182
 * This method does the same thing as the C function OSRGetTargetLinearUnits()
3183
 *
3184
 * @param pszTargetKey the key to look on. i.e. "PROJCS" or "VERT_CS". Might be
3185
 * NULL, in which case PROJCS will be implied (and if not found, LOCAL_CS,
3186
 * GEOCCS, GEOGCS and VERT_CS are looked up)
3187
 * @param ppszName a pointer to be updated with the pointer to the units name.
3188
 * The returned value remains internal to the OGRSpatialReference and should not
3189
 * be freed, or modified.  It may be invalidated on the next
3190
 * OGRSpatialReference call. ppszName can be set to NULL.
3191
 *
3192
 * @return the value to multiply by linear distances to transform them to
3193
 * meters.
3194
 *
3195
 * @since OGR 1.9.0
3196
 * @deprecated GDAL 2.3.0. Use GetTargetLinearUnits(const char*, const char**)
3197
 * const.
3198
 */
3199
3200
double OGRSpatialReference::GetTargetLinearUnits(const char *pszTargetKey,
3201
                                                 const char **ppszName) const
3202
3203
0
{
3204
0
    TAKE_OPTIONAL_LOCK();
3205
3206
0
    d->refreshProjObj();
3207
3208
0
    pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
3209
0
    if (pszTargetKey == nullptr)
3210
0
    {
3211
        // Use cached result if available
3212
0
        if (!d->m_osLinearUnits.empty())
3213
0
        {
3214
0
            if (ppszName)
3215
0
                *ppszName = d->m_osLinearUnits.c_str();
3216
0
            return d->dfToMeter;
3217
0
        }
3218
3219
0
        while (true)
3220
0
        {
3221
0
            if (d->m_pj_crs == nullptr)
3222
0
            {
3223
0
                break;
3224
0
            }
3225
3226
0
            d->demoteFromBoundCRS();
3227
0
            PJ *coordSys = nullptr;
3228
0
            if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
3229
0
            {
3230
0
                for (int iComponent = 0; iComponent < 2; iComponent++)
3231
0
                {
3232
0
                    auto subCRS = proj_crs_get_sub_crs(d->getPROJContext(),
3233
0
                                                       d->m_pj_crs, iComponent);
3234
0
                    if (subCRS && proj_get_type(subCRS) == PJ_TYPE_BOUND_CRS)
3235
0
                    {
3236
0
                        auto temp =
3237
0
                            proj_get_source_crs(d->getPROJContext(), subCRS);
3238
0
                        proj_destroy(subCRS);
3239
0
                        subCRS = temp;
3240
0
                    }
3241
0
                    if (subCRS &&
3242
0
                        (proj_get_type(subCRS) == PJ_TYPE_PROJECTED_CRS ||
3243
0
                         proj_get_type(subCRS) == PJ_TYPE_ENGINEERING_CRS ||
3244
0
                         proj_get_type(subCRS) == PJ_TYPE_VERTICAL_CRS))
3245
0
                    {
3246
0
                        coordSys = proj_crs_get_coordinate_system(
3247
0
                            d->getPROJContext(), subCRS);
3248
0
                        proj_destroy(subCRS);
3249
0
                        break;
3250
0
                    }
3251
0
                    else if (subCRS)
3252
0
                    {
3253
0
                        proj_destroy(subCRS);
3254
0
                    }
3255
0
                }
3256
0
                if (coordSys == nullptr)
3257
0
                {
3258
0
                    d->undoDemoteFromBoundCRS();
3259
0
                    break;
3260
0
                }
3261
0
            }
3262
0
            else
3263
0
            {
3264
0
                coordSys = proj_crs_get_coordinate_system(d->getPROJContext(),
3265
0
                                                          d->m_pj_crs);
3266
0
            }
3267
3268
0
            d->undoDemoteFromBoundCRS();
3269
0
            if (!coordSys)
3270
0
            {
3271
0
                break;
3272
0
            }
3273
0
            auto csType = proj_cs_get_type(d->getPROJContext(), coordSys);
3274
3275
0
            if (csType != PJ_CS_TYPE_CARTESIAN &&
3276
0
                csType != PJ_CS_TYPE_VERTICAL &&
3277
0
                csType != PJ_CS_TYPE_ELLIPSOIDAL &&
3278
0
                csType != PJ_CS_TYPE_SPHERICAL)
3279
0
            {
3280
0
                proj_destroy(coordSys);
3281
0
                break;
3282
0
            }
3283
3284
0
            int axis = 0;
3285
3286
0
            if (csType == PJ_CS_TYPE_ELLIPSOIDAL ||
3287
0
                csType == PJ_CS_TYPE_SPHERICAL)
3288
0
            {
3289
0
                const int axisCount =
3290
0
                    proj_cs_get_axis_count(d->getPROJContext(), coordSys);
3291
3292
0
                if (axisCount == 3)
3293
0
                {
3294
0
                    axis = 2;
3295
0
                }
3296
0
                else
3297
0
                {
3298
0
                    proj_destroy(coordSys);
3299
0
                    break;
3300
0
                }
3301
0
            }
3302
3303
0
            double dfConvFactor = 0.0;
3304
0
            const char *pszUnitName = nullptr;
3305
0
            if (!proj_cs_get_axis_info(d->getPROJContext(), coordSys, axis,
3306
0
                                       nullptr, nullptr, nullptr, &dfConvFactor,
3307
0
                                       &pszUnitName, nullptr, nullptr))
3308
0
            {
3309
0
                proj_destroy(coordSys);
3310
0
                break;
3311
0
            }
3312
3313
0
            d->m_osLinearUnits = pszUnitName;
3314
0
            d->dfToMeter = dfConvFactor;
3315
0
            if (ppszName)
3316
0
                *ppszName = d->m_osLinearUnits.c_str();
3317
3318
0
            proj_destroy(coordSys);
3319
0
            return dfConvFactor;
3320
0
        }
3321
3322
0
        d->m_osLinearUnits = "unknown";
3323
0
        d->dfToMeter = 1.0;
3324
3325
0
        if (ppszName != nullptr)
3326
0
            *ppszName = d->m_osLinearUnits.c_str();
3327
0
        return 1.0;
3328
0
    }
3329
3330
0
    const OGR_SRSNode *poCS = GetAttrNode(pszTargetKey);
3331
3332
0
    if (ppszName != nullptr)
3333
0
        *ppszName = "unknown";
3334
3335
0
    if (poCS == nullptr)
3336
0
        return 1.0;
3337
3338
0
    for (int iChild = 0; iChild < poCS->GetChildCount(); iChild++)
3339
0
    {
3340
0
        const OGR_SRSNode *poChild = poCS->GetChild(iChild);
3341
3342
0
        if (EQUAL(poChild->GetValue(), "UNIT") && poChild->GetChildCount() >= 2)
3343
0
        {
3344
0
            if (ppszName != nullptr)
3345
0
                *ppszName = poChild->GetChild(0)->GetValue();
3346
3347
0
            return CPLAtof(poChild->GetChild(1)->GetValue());
3348
0
        }
3349
0
    }
3350
3351
0
    return 1.0;
3352
0
}
3353
3354
/**
3355
 * \brief Fetch linear units for target.
3356
 *
3357
 * If no units are available, a value of "Meters" and 1.0 will be assumed.
3358
 *
3359
 * This method does the same thing as the C function OSRGetTargetLinearUnits()
3360
 *
3361
 * @param pszTargetKey the key to look on. i.e. "PROJCS" or "VERT_CS". Might be
3362
 * NULL, in which case PROJCS will be implied (and if not found, LOCAL_CS,
3363
 * GEOCCS and VERT_CS are looked up)
3364
 * @param ppszName a pointer to be updated with the pointer to the units name.
3365
 * The returned value remains internal to the OGRSpatialReference and should not
3366
 * be freed, or modified.  It may be invalidated on the next
3367
 * OGRSpatialReference call. ppszName can be set to NULL.
3368
 *
3369
 * @return the value to multiply by linear distances to transform them to
3370
 * meters.
3371
 *
3372
 * @since GDAL 2.3.0
3373
 */
3374
3375
double OGRSpatialReference::GetTargetLinearUnits(const char *pszTargetKey,
3376
                                                 char **ppszName) const
3377
3378
0
{
3379
0
    return GetTargetLinearUnits(pszTargetKey,
3380
0
                                const_cast<const char **>(ppszName));
3381
0
}
3382
3383
/************************************************************************/
3384
/*                      OSRGetTargetLinearUnits()                       */
3385
/************************************************************************/
3386
3387
/**
3388
 * \brief Fetch linear projection units.
3389
 *
3390
 * This function is the same as OGRSpatialReference::GetTargetLinearUnits()
3391
 *
3392
 * @since OGR 1.9.0
3393
 */
3394
double OSRGetTargetLinearUnits(OGRSpatialReferenceH hSRS,
3395
                               const char *pszTargetKey, char **ppszName)
3396
3397
0
{
3398
0
    VALIDATE_POINTER1(hSRS, "OSRGetTargetLinearUnits", 0);
3399
3400
0
    return ToPointer(hSRS)->GetTargetLinearUnits(
3401
0
        pszTargetKey, const_cast<const char **>(ppszName));
3402
0
}
3403
3404
/************************************************************************/
3405
/*                          GetPrimeMeridian()                          */
3406
/************************************************************************/
3407
3408
/**
3409
 * \brief Fetch prime meridian info.
3410
 *
3411
 * Returns the offset of the prime meridian from greenwich in degrees,
3412
 * and the prime meridian name (if requested).   If no PRIMEM value exists
3413
 * in the coordinate system definition a value of "Greenwich" and an
3414
 * offset of 0.0 is assumed.
3415
 *
3416
 * If the prime meridian name is returned, the pointer is to an internal
3417
 * copy of the name. It should not be freed, altered or depended on after
3418
 * the next OGR call.
3419
 *
3420
 * This method is the same as the C function OSRGetPrimeMeridian().
3421
 *
3422
 * @param ppszName return location for prime meridian name.  If NULL, name
3423
 * is not returned.
3424
 *
3425
 * @return the offset to the GEOGCS prime meridian from greenwich in decimal
3426
 * degrees.
3427
 * @deprecated GDAL 2.3.0. Use GetPrimeMeridian(const char**) const.
3428
 */
3429
3430
double OGRSpatialReference::GetPrimeMeridian(const char **ppszName) const
3431
3432
0
{
3433
0
    TAKE_OPTIONAL_LOCK();
3434
3435
0
    d->refreshProjObj();
3436
3437
0
    if (!d->m_osPrimeMeridianName.empty())
3438
0
    {
3439
0
        if (ppszName != nullptr)
3440
0
            *ppszName = d->m_osPrimeMeridianName.c_str();
3441
0
        return d->dfFromGreenwich;
3442
0
    }
3443
3444
0
    while (true)
3445
0
    {
3446
0
        if (!d->m_pj_crs)
3447
0
            break;
3448
3449
0
        auto pm = proj_get_prime_meridian(d->getPROJContext(), d->m_pj_crs);
3450
0
        if (!pm)
3451
0
            break;
3452
3453
0
        d->m_osPrimeMeridianName = proj_get_name(pm);
3454
0
        if (ppszName)
3455
0
            *ppszName = d->m_osPrimeMeridianName.c_str();
3456
0
        double dfLongitude = 0.0;
3457
0
        double dfConvFactor = 0.0;
3458
0
        proj_prime_meridian_get_parameters(
3459
0
            d->getPROJContext(), pm, &dfLongitude, &dfConvFactor, nullptr);
3460
0
        proj_destroy(pm);
3461
0
        d->dfFromGreenwich =
3462
0
            dfLongitude * dfConvFactor / CPLAtof(SRS_UA_DEGREE_CONV);
3463
0
        return d->dfFromGreenwich;
3464
0
    }
3465
3466
0
    d->m_osPrimeMeridianName = SRS_PM_GREENWICH;
3467
0
    d->dfFromGreenwich = 0.0;
3468
0
    if (ppszName != nullptr)
3469
0
        *ppszName = d->m_osPrimeMeridianName.c_str();
3470
0
    return d->dfFromGreenwich;
3471
0
}
3472
3473
/**
3474
 * \brief Fetch prime meridian info.
3475
 *
3476
 * Returns the offset of the prime meridian from greenwich in degrees,
3477
 * and the prime meridian name (if requested).   If no PRIMEM value exists
3478
 * in the coordinate system definition a value of "Greenwich" and an
3479
 * offset of 0.0 is assumed.
3480
 *
3481
 * If the prime meridian name is returned, the pointer is to an internal
3482
 * copy of the name. It should not be freed, altered or depended on after
3483
 * the next OGR call.
3484
 *
3485
 * This method is the same as the C function OSRGetPrimeMeridian().
3486
 *
3487
 * @param ppszName return location for prime meridian name.  If NULL, name
3488
 * is not returned.
3489
 *
3490
 * @return the offset to the GEOGCS prime meridian from greenwich in decimal
3491
 * degrees.
3492
 * @since GDAL 2.3.0
3493
 */
3494
3495
double OGRSpatialReference::GetPrimeMeridian(char **ppszName) const
3496
3497
0
{
3498
0
    return GetPrimeMeridian(const_cast<const char **>(ppszName));
3499
0
}
3500
3501
/************************************************************************/
3502
/*                        OSRGetPrimeMeridian()                         */
3503
/************************************************************************/
3504
3505
/**
3506
 * \brief Fetch prime meridian info.
3507
 *
3508
 * This function is the same as OGRSpatialReference::GetPrimeMeridian()
3509
 */
3510
double OSRGetPrimeMeridian(OGRSpatialReferenceH hSRS, char **ppszName)
3511
3512
0
{
3513
0
    VALIDATE_POINTER1(hSRS, "OSRGetPrimeMeridian", 0);
3514
3515
0
    return ToPointer(hSRS)->GetPrimeMeridian(
3516
0
        const_cast<const char **>(ppszName));
3517
0
}
3518
3519
/************************************************************************/
3520
/*                             SetGeogCS()                              */
3521
/************************************************************************/
3522
3523
/**
3524
 * \brief Set geographic coordinate system.
3525
 *
3526
 * This method is used to set the datum, ellipsoid, prime meridian and
3527
 * angular units for a geographic coordinate system.  It can be used on its
3528
 * own to establish a geographic spatial reference, or applied to a
3529
 * projected coordinate system to establish the underlying geographic
3530
 * coordinate system.
3531
 *
3532
 * This method does the same as the C function OSRSetGeogCS().
3533
 *
3534
 * @param pszGeogName user visible name for the geographic coordinate system
3535
 * (not to serve as a key).
3536
 *
3537
 * @param pszDatumName key name for this datum.  The OpenGIS specification
3538
 * lists some known values, and otherwise EPSG datum names with a standard
3539
 * transformation are considered legal keys.
3540
 *
3541
 * @param pszSpheroidName user visible spheroid name (not to serve as a key)
3542
 *
3543
 * @param dfSemiMajor the semi major axis of the spheroid.
3544
 *
3545
 * @param dfInvFlattening the inverse flattening for the spheroid.
3546
 * This can be computed from the semi minor axis as
3547
 * 1/f = 1.0 / (1.0 - semiminor/semimajor).
3548
 *
3549
 * @param pszPMName the name of the prime meridian (not to serve as a key)
3550
 * If this is NULL a default value of "Greenwich" will be used.
3551
 *
3552
 * @param dfPMOffset the longitude of Greenwich relative to this prime
3553
 * meridian. Always in Degrees
3554
 *
3555
 * @param pszAngularUnits the angular units name (see ogr_srs_api.h for some
3556
 * standard names).  If NULL a value of "degrees" will be assumed.
3557
 *
3558
 * @param dfConvertToRadians value to multiply angular units by to transform
3559
 * them to radians.  A value of SRS_UA_DEGREE_CONV will be used if
3560
 * pszAngularUnits is NULL.
3561
 *
3562
 * @return OGRERR_NONE on success.
3563
 */
3564
3565
OGRErr OGRSpatialReference::SetGeogCS(
3566
    const char *pszGeogName, const char *pszDatumName,
3567
    const char *pszSpheroidName, double dfSemiMajor, double dfInvFlattening,
3568
    const char *pszPMName, double dfPMOffset, const char *pszAngularUnits,
3569
    double dfConvertToRadians)
3570
3571
0
{
3572
0
    TAKE_OPTIONAL_LOCK();
3573
3574
0
    d->bNormInfoSet = FALSE;
3575
0
    d->m_osAngularUnits.clear();
3576
0
    d->m_dfAngularUnitToRadian = 0.0;
3577
0
    d->m_osPrimeMeridianName.clear();
3578
0
    d->dfFromGreenwich = 0.0;
3579
3580
    /* -------------------------------------------------------------------- */
3581
    /*      For a geocentric coordinate system we want to set the datum     */
3582
    /*      and ellipsoid based on the GEOGCS.  Create the GEOGCS in a      */
3583
    /*      temporary srs and use the copy method which has special         */
3584
    /*      handling for GEOCCS.                                            */
3585
    /* -------------------------------------------------------------------- */
3586
0
    if (IsGeocentric())
3587
0
    {
3588
0
        OGRSpatialReference oGCS;
3589
3590
0
        oGCS.SetGeogCS(pszGeogName, pszDatumName, pszSpheroidName, dfSemiMajor,
3591
0
                       dfInvFlattening, pszPMName, dfPMOffset, pszAngularUnits,
3592
0
                       dfConvertToRadians);
3593
0
        return CopyGeogCSFrom(&oGCS);
3594
0
    }
3595
3596
0
    auto cs = proj_create_ellipsoidal_2D_cs(
3597
0
        d->getPROJContext(), PJ_ELLPS2D_LATITUDE_LONGITUDE, pszAngularUnits,
3598
0
        dfConvertToRadians);
3599
    // Prime meridian expressed in Degree
3600
0
    auto obj = proj_create_geographic_crs(
3601
0
        d->getPROJContext(), pszGeogName, pszDatumName, pszSpheroidName,
3602
0
        dfSemiMajor, dfInvFlattening, pszPMName, dfPMOffset, nullptr, 0.0, cs);
3603
0
    proj_destroy(cs);
3604
3605
0
    if (d->m_pj_crs == nullptr || d->m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
3606
0
        d->m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS)
3607
0
    {
3608
0
        d->setPjCRS(obj);
3609
0
    }
3610
0
    else if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
3611
0
    {
3612
0
        d->setPjCRS(
3613
0
            proj_crs_alter_geodetic_crs(d->getPROJContext(), d->m_pj_crs, obj));
3614
0
        proj_destroy(obj);
3615
0
    }
3616
0
    else
3617
0
    {
3618
0
        proj_destroy(obj);
3619
0
    }
3620
3621
0
    return OGRERR_NONE;
3622
0
}
3623
3624
/************************************************************************/
3625
/*                            OSRSetGeogCS()                            */
3626
/************************************************************************/
3627
3628
/**
3629
 * \brief Set geographic coordinate system.
3630
 *
3631
 * This function is the same as OGRSpatialReference::SetGeogCS()
3632
 */
3633
OGRErr OSRSetGeogCS(OGRSpatialReferenceH hSRS, const char *pszGeogName,
3634
                    const char *pszDatumName, const char *pszSpheroidName,
3635
                    double dfSemiMajor, double dfInvFlattening,
3636
                    const char *pszPMName, double dfPMOffset,
3637
                    const char *pszAngularUnits, double dfConvertToRadians)
3638
3639
0
{
3640
0
    VALIDATE_POINTER1(hSRS, "OSRSetGeogCS", OGRERR_FAILURE);
3641
3642
0
    return ToPointer(hSRS)->SetGeogCS(pszGeogName, pszDatumName,
3643
0
                                      pszSpheroidName, dfSemiMajor,
3644
0
                                      dfInvFlattening, pszPMName, dfPMOffset,
3645
0
                                      pszAngularUnits, dfConvertToRadians);
3646
0
}
3647
3648
/************************************************************************/
3649
/*                         SetWellKnownGeogCS()                         */
3650
/************************************************************************/
3651
3652
/**
3653
 * \brief Set a GeogCS based on well known name.
3654
 *
3655
 * This may be called on an empty OGRSpatialReference to make a geographic
3656
 * coordinate system, or on something with an existing PROJCS node to
3657
 * set the underlying geographic coordinate system of a projected coordinate
3658
 * system.
3659
 *
3660
 * The following well known text values are currently supported,
3661
 * Except for "EPSG:n", the others are without dependency on EPSG data files:
3662
 * <ul>
3663
 * <li> "EPSG:n": where n is the code a Geographic coordinate reference system.
3664
 * <li> "WGS84": same as "EPSG:4326" (axis order lat/long).
3665
 * <li> "WGS72": same as "EPSG:4322" (axis order lat/long).
3666
 * <li> "NAD83": same as "EPSG:4269" (axis order lat/long).
3667
 * <li> "NAD27": same as "EPSG:4267" (axis order lat/long).
3668
 * <li> "CRS84", "CRS:84": same as "WGS84" but with axis order long/lat.
3669
 * <li> "CRS72", "CRS:72": same as "WGS72" but with axis order long/lat.
3670
 * <li> "CRS27", "CRS:27": same as "NAD27" but with axis order long/lat.
3671
 * </ul>
3672
 *
3673
 * @param pszName name of well known geographic coordinate system.
3674
 * @return OGRERR_NONE on success, or OGRERR_FAILURE if the name isn't
3675
 * recognised, the target object is already initialized, or an EPSG value
3676
 * can't be successfully looked up.
3677
 */
3678
3679
OGRErr OGRSpatialReference::SetWellKnownGeogCS(const char *pszName)
3680
3681
0
{
3682
0
    TAKE_OPTIONAL_LOCK();
3683
3684
    /* -------------------------------------------------------------------- */
3685
    /*      Check for EPSG authority numbers.                               */
3686
    /* -------------------------------------------------------------------- */
3687
0
    if (STARTS_WITH_CI(pszName, "EPSG:") || STARTS_WITH_CI(pszName, "EPSGA:"))
3688
0
    {
3689
0
        OGRSpatialReference oSRS2;
3690
0
        const OGRErr eErr = oSRS2.importFromEPSG(atoi(pszName + 5));
3691
0
        if (eErr != OGRERR_NONE)
3692
0
            return eErr;
3693
3694
0
        if (!oSRS2.IsGeographic())
3695
0
            return OGRERR_FAILURE;
3696
3697
0
        return CopyGeogCSFrom(&oSRS2);
3698
0
    }
3699
3700
    /* -------------------------------------------------------------------- */
3701
    /*      Check for simple names.                                         */
3702
    /* -------------------------------------------------------------------- */
3703
0
    const char *pszWKT = nullptr;
3704
3705
0
    if (EQUAL(pszName, "WGS84"))
3706
0
    {
3707
0
        pszWKT = SRS_WKT_WGS84_LAT_LONG;
3708
0
    }
3709
0
    else if (EQUAL(pszName, "CRS84") || EQUAL(pszName, "CRS:84"))
3710
0
    {
3711
0
        pszWKT =
3712
0
            "GEOGCRS[\"WGS 84 (CRS84)\",DATUM[\"World Geodetic System 1984\","
3713
0
            "ELLIPSOID[\"WGS "
3714
0
            "84\",6378137,298.257223563,LENGTHUNIT[\"metre\",1]]],"
3715
0
            "PRIMEM[\"Greenwich\",0,ANGLEUNIT[\"degree\",0.0174532925199433]],"
3716
0
            "CS[ellipsoidal,2],AXIS[\"geodetic longitude (Lon)\",east,ORDER[1],"
3717
0
            "ANGLEUNIT[\"degree\",0.0174532925199433]],"
3718
0
            "AXIS[\"geodetic latitude (Lat)\",north,ORDER[2],"
3719
0
            "ANGLEUNIT[\"degree\",0.0174532925199433]],"
3720
0
            "USAGE[SCOPE[\"unknown\"],AREA[\"World\"],BBOX[-90,-180,90,180]],"
3721
0
            "ID[\"OGC\",\"CRS84\"]]";
3722
0
    }
3723
0
    else if (EQUAL(pszName, "WGS72"))
3724
0
        pszWKT =
3725
0
            "GEOGCS[\"WGS 72\",DATUM[\"WGS_1972\","
3726
0
            "SPHEROID[\"WGS 72\",6378135,298.26,AUTHORITY[\"EPSG\",\"7043\"]],"
3727
0
            "AUTHORITY[\"EPSG\",\"6322\"]],"
3728
0
            "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3729
0
            "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3730
0
            "AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],"
3731
0
            "AUTHORITY[\"EPSG\",\"4322\"]]";
3732
3733
0
    else if (EQUAL(pszName, "NAD27"))
3734
0
        pszWKT =
3735
0
            "GEOGCS[\"NAD27\",DATUM[\"North_American_Datum_1927\","
3736
0
            "SPHEROID[\"Clarke 1866\",6378206.4,294.9786982138982,"
3737
0
            "AUTHORITY[\"EPSG\",\"7008\"]],AUTHORITY[\"EPSG\",\"6267\"]],"
3738
0
            "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3739
0
            "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3740
0
            "AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],"
3741
0
            "AUTHORITY[\"EPSG\",\"4267\"]]";
3742
3743
0
    else if (EQUAL(pszName, "CRS27") || EQUAL(pszName, "CRS:27"))
3744
0
        pszWKT =
3745
0
            "GEOGCS[\"NAD27\",DATUM[\"North_American_Datum_1927\","
3746
0
            "SPHEROID[\"Clarke 1866\",6378206.4,294.9786982138982,"
3747
0
            "AUTHORITY[\"EPSG\",\"7008\"]],AUTHORITY[\"EPSG\",\"6267\"]],"
3748
0
            "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3749
0
            "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3750
0
            "AXIS[\"Longitude\",EAST],AXIS[\"Latitude\",NORTH]]";
3751
3752
0
    else if (EQUAL(pszName, "NAD83"))
3753
0
        pszWKT =
3754
0
            "GEOGCS[\"NAD83\",DATUM[\"North_American_Datum_1983\","
3755
0
            "SPHEROID[\"GRS 1980\",6378137,298.257222101,"
3756
0
            "AUTHORITY[\"EPSG\",\"7019\"]],"
3757
0
            "AUTHORITY[\"EPSG\",\"6269\"]],"
3758
0
            "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3759
0
            "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3760
0
            "AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],AUTHORITY["
3761
0
            "\"EPSG\",\"4269\"]]";
3762
3763
0
    else if (EQUAL(pszName, "CRS83") || EQUAL(pszName, "CRS:83"))
3764
0
        pszWKT =
3765
0
            "GEOGCS[\"NAD83\",DATUM[\"North_American_Datum_1983\","
3766
0
            "SPHEROID[\"GRS 1980\",6378137,298.257222101,"
3767
0
            "AUTHORITY[\"EPSG\",\"7019\"]],"
3768
0
            "AUTHORITY[\"EPSG\",\"6269\"]],"
3769
0
            "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3770
0
            "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3771
0
            "AXIS[\"Longitude\",EAST],AXIS[\"Latitude\",NORTH]]";
3772
3773
0
    else
3774
0
        return OGRERR_FAILURE;
3775
3776
    /* -------------------------------------------------------------------- */
3777
    /*      Import the WKT                                                  */
3778
    /* -------------------------------------------------------------------- */
3779
0
    OGRSpatialReference oSRS2;
3780
0
    const OGRErr eErr = oSRS2.importFromWkt(pszWKT);
3781
0
    if (eErr != OGRERR_NONE)
3782
0
        return eErr;
3783
3784
    /* -------------------------------------------------------------------- */
3785
    /*      Copy over.                                                      */
3786
    /* -------------------------------------------------------------------- */
3787
0
    return CopyGeogCSFrom(&oSRS2);
3788
0
}
3789
3790
/************************************************************************/
3791
/*                       OSRSetWellKnownGeogCS()                        */
3792
/************************************************************************/
3793
3794
/**
3795
 * \brief Set a GeogCS based on well known name.
3796
 *
3797
 * This function is the same as OGRSpatialReference::SetWellKnownGeogCS()
3798
 */
3799
OGRErr OSRSetWellKnownGeogCS(OGRSpatialReferenceH hSRS, const char *pszName)
3800
3801
0
{
3802
0
    VALIDATE_POINTER1(hSRS, "OSRSetWellKnownGeogCS", OGRERR_FAILURE);
3803
3804
0
    return ToPointer(hSRS)->SetWellKnownGeogCS(pszName);
3805
0
}
3806
3807
/************************************************************************/
3808
/*                           CopyGeogCSFrom()                           */
3809
/************************************************************************/
3810
3811
/**
3812
 * \brief Copy GEOGCS from another OGRSpatialReference.
3813
 *
3814
 * The GEOGCS information is copied into this OGRSpatialReference from another.
3815
 * If this object has a PROJCS root already, the GEOGCS is installed within
3816
 * it, otherwise it is installed as the root.
3817
 *
3818
 * @param poSrcSRS the spatial reference to copy the GEOGCS information from.
3819
 *
3820
 * @return OGRERR_NONE on success or an error code.
3821
 */
3822
3823
OGRErr OGRSpatialReference::CopyGeogCSFrom(const OGRSpatialReference *poSrcSRS)
3824
3825
0
{
3826
0
    TAKE_OPTIONAL_LOCK();
3827
3828
0
    d->bNormInfoSet = FALSE;
3829
0
    d->m_osAngularUnits.clear();
3830
0
    d->m_dfAngularUnitToRadian = 0.0;
3831
0
    d->m_osPrimeMeridianName.clear();
3832
0
    d->dfFromGreenwich = 0.0;
3833
3834
0
    d->refreshProjObj();
3835
0
    poSrcSRS->d->refreshProjObj();
3836
0
    if (!poSrcSRS->d->m_pj_crs)
3837
0
    {
3838
0
        return OGRERR_FAILURE;
3839
0
    }
3840
0
    auto geodCRS =
3841
0
        proj_crs_get_geodetic_crs(d->getPROJContext(), poSrcSRS->d->m_pj_crs);
3842
0
    if (!geodCRS)
3843
0
    {
3844
0
        return OGRERR_FAILURE;
3845
0
    }
3846
3847
    /* -------------------------------------------------------------------- */
3848
    /*      Handle geocentric coordinate systems specially.  We just        */
3849
    /*      want to copy the DATUM.                                         */
3850
    /* -------------------------------------------------------------------- */
3851
0
    if (d->m_pjType == PJ_TYPE_GEOCENTRIC_CRS)
3852
0
    {
3853
0
        auto datum = proj_crs_get_datum(d->getPROJContext(), geodCRS);
3854
0
#if PROJ_VERSION_MAJOR > 7 ||                                                  \
3855
0
    (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
3856
0
        if (datum == nullptr)
3857
0
        {
3858
0
            datum = proj_crs_get_datum_ensemble(d->getPROJContext(), geodCRS);
3859
0
        }
3860
0
#endif
3861
0
        if (datum == nullptr)
3862
0
        {
3863
0
            proj_destroy(geodCRS);
3864
0
            return OGRERR_FAILURE;
3865
0
        }
3866
3867
0
        const char *pszUnitName = nullptr;
3868
0
        double unitConvFactor = GetLinearUnits(&pszUnitName);
3869
3870
0
        auto pj_crs = proj_create_geocentric_crs_from_datum(
3871
0
            d->getPROJContext(), proj_get_name(d->m_pj_crs), datum, pszUnitName,
3872
0
            unitConvFactor);
3873
0
        proj_destroy(datum);
3874
3875
0
        d->setPjCRS(pj_crs);
3876
0
    }
3877
3878
0
    else if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
3879
0
    {
3880
0
        auto pj_crs = proj_crs_alter_geodetic_crs(d->getPROJContext(),
3881
0
                                                  d->m_pj_crs, geodCRS);
3882
0
        d->setPjCRS(pj_crs);
3883
0
    }
3884
3885
0
    else
3886
0
    {
3887
0
        d->setPjCRS(proj_clone(d->getPROJContext(), geodCRS));
3888
0
    }
3889
3890
    // Apply TOWGS84 of source CRS
3891
0
    if (poSrcSRS->d->m_pjType == PJ_TYPE_BOUND_CRS)
3892
0
    {
3893
0
        auto target =
3894
0
            proj_get_target_crs(d->getPROJContext(), poSrcSRS->d->m_pj_crs);
3895
0
        auto co = proj_crs_get_coordoperation(d->getPROJContext(),
3896
0
                                              poSrcSRS->d->m_pj_crs);
3897
0
        d->setPjCRS(proj_crs_create_bound_crs(d->getPROJContext(), d->m_pj_crs,
3898
0
                                              target, co));
3899
0
        proj_destroy(target);
3900
0
        proj_destroy(co);
3901
0
    }
3902
3903
0
    proj_destroy(geodCRS);
3904
3905
0
    return OGRERR_NONE;
3906
0
}
3907
3908
/************************************************************************/
3909
/*                         OSRCopyGeogCSFrom()                          */
3910
/************************************************************************/
3911
3912
/**
3913
 * \brief Copy GEOGCS from another OGRSpatialReference.
3914
 *
3915
 * This function is the same as OGRSpatialReference::CopyGeogCSFrom()
3916
 */
3917
OGRErr OSRCopyGeogCSFrom(OGRSpatialReferenceH hSRS,
3918
                         const OGRSpatialReferenceH hSrcSRS)
3919
3920
0
{
3921
0
    VALIDATE_POINTER1(hSRS, "OSRCopyGeogCSFrom", OGRERR_FAILURE);
3922
0
    VALIDATE_POINTER1(hSrcSRS, "OSRCopyGeogCSFrom", OGRERR_FAILURE);
3923
3924
0
    return ToPointer(hSRS)->CopyGeogCSFrom(ToPointer(hSrcSRS));
3925
0
}
3926
3927
/************************************************************************/
3928
/*                   SET_FROM_USER_INPUT_LIMITATIONS_get()              */
3929
/************************************************************************/
3930
3931
/** Limitations for OGRSpatialReference::SetFromUserInput().
3932
 *
3933
 * Currently ALLOW_NETWORK_ACCESS=NO and ALLOW_FILE_ACCESS=NO.
3934
 */
3935
const char *const OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS[] = {
3936
    "ALLOW_NETWORK_ACCESS=NO", "ALLOW_FILE_ACCESS=NO", nullptr};
3937
3938
/**
3939
 * \brief Return OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS
3940
 */
3941
CSLConstList OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS_get()
3942
0
{
3943
0
    return SET_FROM_USER_INPUT_LIMITATIONS;
3944
0
}
3945
3946
/************************************************************************/
3947
/*                      RemoveIDFromMemberOfEnsembles()                 */
3948
/************************************************************************/
3949
3950
// cppcheck-suppress constParameterReference
3951
static void RemoveIDFromMemberOfEnsembles(CPLJSONObject &obj)
3952
0
{
3953
    // Remove "id" from members of datum ensembles for compatibility with
3954
    // older PROJ versions
3955
    // Cf https://github.com/opengeospatial/geoparquet/discussions/110
3956
    // and https://github.com/OSGeo/PROJ/pull/3221
3957
0
    if (obj.GetType() == CPLJSONObject::Type::Object)
3958
0
    {
3959
0
        for (auto &subObj : obj.GetChildren())
3960
0
        {
3961
0
            RemoveIDFromMemberOfEnsembles(subObj);
3962
0
        }
3963
0
    }
3964
0
    else if (obj.GetType() == CPLJSONObject::Type::Array &&
3965
0
             obj.GetName() == "members")
3966
0
    {
3967
0
        for (auto &subObj : obj.ToArray())
3968
0
        {
3969
0
            if (subObj.GetType() == CPLJSONObject::Type::Object)
3970
0
            {
3971
0
                subObj.Delete("id");
3972
0
            }
3973
0
        }
3974
0
    }
3975
0
}
3976
3977
/************************************************************************/
3978
/*                          SetFromUserInput()                          */
3979
/************************************************************************/
3980
3981
/**
3982
 * \brief Set spatial reference from various text formats.
3983
 *
3984
 * This method will examine the provided input, and try to deduce the
3985
 * format, and then use it to initialize the spatial reference system.  It
3986
 * may take the following forms:
3987
 *
3988
 * <ol>
3989
 * <li> Well Known Text definition - passed on to importFromWkt().
3990
 * <li> "EPSG:n" - number passed on to importFromEPSG().
3991
 * <li> "EPSGA:n" - number passed on to importFromEPSGA().
3992
 * <li> "AUTO:proj_id,unit_id,lon0,lat0" - WMS auto projections.
3993
 * <li> "urn:ogc:def:crs:EPSG::n" - ogc urns
3994
 * <li> PROJ.4 definitions - passed on to importFromProj4().
3995
 * <li> filename - file read for WKT, XML or PROJ.4 definition.
3996
 * <li> well known name accepted by SetWellKnownGeogCS(), such as NAD27, NAD83,
3997
 * WGS84 or WGS72.
3998
 * <li> "IGNF:xxxx", "ESRI:xxxx", etc. from definitions from the PROJ database;
3999
 * <li> PROJJSON (PROJ &gt;= 6.2)
4000
 * </ol>
4001
 *
4002
 * It is expected that this method will be extended in the future to support
4003
 * XML and perhaps a simplified "minilanguage" for indicating common UTM and
4004
 * State Plane definitions.
4005
 *
4006
 * This method is intended to be flexible, but by its nature it is
4007
 * imprecise as it must guess information about the format intended.  When
4008
 * possible applications should call the specific method appropriate if the
4009
 * input is known to be in a particular format.
4010
 *
4011
 * This method does the same thing as the OSRSetFromUserInput() function.
4012
 *
4013
 * @param pszDefinition text definition to try to deduce SRS from.
4014
 *
4015
 * @return OGRERR_NONE on success, or an error code if the name isn't
4016
 * recognised, the definition is corrupt, or an EPSG value can't be
4017
 * successfully looked up.
4018
 */
4019
4020
OGRErr OGRSpatialReference::SetFromUserInput(const char *pszDefinition)
4021
0
{
4022
0
    return SetFromUserInput(pszDefinition, nullptr);
4023
0
}
4024
4025
/**
4026
 * \brief Set spatial reference from various text formats.
4027
 *
4028
 * This method will examine the provided input, and try to deduce the
4029
 * format, and then use it to initialize the spatial reference system.  It
4030
 * may take the following forms:
4031
 *
4032
 * <ol>
4033
 * <li> Well Known Text definition - passed on to importFromWkt().
4034
 * <li> "EPSG:n" - number passed on to importFromEPSG().
4035
 * <li> "EPSGA:n" - number passed on to importFromEPSGA().
4036
 * <li> "AUTO:proj_id,unit_id,lon0,lat0" - WMS auto projections.
4037
 * <li> "urn:ogc:def:crs:EPSG::n" - ogc urns
4038
 * <li> PROJ.4 definitions - passed on to importFromProj4().
4039
 * <li> filename - file read for WKT, XML or PROJ.4 definition.
4040
 * <li> well known name accepted by SetWellKnownGeogCS(), such as NAD27, NAD83,
4041
 * WGS84 or WGS72.
4042
 * <li> "IGNF:xxxx", "ESRI:xxxx", etc. from definitions from the PROJ database;
4043
 * <li> PROJJSON (PROJ &gt;= 6.2)
4044
 * </ol>
4045
 *
4046
 * It is expected that this method will be extended in the future to support
4047
 * XML and perhaps a simplified "minilanguage" for indicating common UTM and
4048
 * State Plane definitions.
4049
 *
4050
 * This method is intended to be flexible, but by its nature it is
4051
 * imprecise as it must guess information about the format intended.  When
4052
 * possible applications should call the specific method appropriate if the
4053
 * input is known to be in a particular format.
4054
 *
4055
 * This method does the same thing as the OSRSetFromUserInput() and
4056
 * OSRSetFromUserInputEx() functions.
4057
 *
4058
 * @param pszDefinition text definition to try to deduce SRS from.
4059
 *
4060
 * @param papszOptions NULL terminated list of options, or NULL.
4061
 * <ol>
4062
 * <li> ALLOW_NETWORK_ACCESS=YES/NO.
4063
 *      Whether http:// or https:// access is allowed. Defaults to YES.
4064
 * <li> ALLOW_FILE_ACCESS=YES/NO.
4065
 *      Whether reading a file using the Virtual File System layer is allowed
4066
 *      (can also involve network access). Defaults to YES.
4067
 * </ol>
4068
 *
4069
 * @return OGRERR_NONE on success, or an error code if the name isn't
4070
 * recognised, the definition is corrupt, or an EPSG value can't be
4071
 * successfully looked up.
4072
 */
4073
4074
OGRErr OGRSpatialReference::SetFromUserInput(const char *pszDefinition,
4075
                                             CSLConstList papszOptions)
4076
0
{
4077
0
    TAKE_OPTIONAL_LOCK();
4078
4079
    // Skip leading white space
4080
0
    while (isspace(static_cast<unsigned char>(*pszDefinition)))
4081
0
        pszDefinition++;
4082
4083
0
    if (STARTS_WITH_CI(pszDefinition, "ESRI::"))
4084
0
    {
4085
0
        pszDefinition += 6;
4086
0
    }
4087
4088
    /* -------------------------------------------------------------------- */
4089
    /*      Is it a recognised syntax?                                      */
4090
    /* -------------------------------------------------------------------- */
4091
0
    const char *const wktKeywords[] = {
4092
        // WKT1
4093
0
        "GEOGCS", "GEOCCS", "PROJCS", "VERT_CS", "COMPD_CS", "LOCAL_CS",
4094
        // WKT2"
4095
0
        "GEODCRS", "GEOGCRS", "GEODETICCRS", "GEOGRAPHICCRS", "PROJCRS",
4096
0
        "PROJECTEDCRS", "VERTCRS", "VERTICALCRS", "COMPOUNDCRS", "ENGCRS",
4097
0
        "ENGINEERINGCRS", "BOUNDCRS", "DERIVEDPROJCRS", "COORDINATEMETADATA"};
4098
0
    for (const char *keyword : wktKeywords)
4099
0
    {
4100
0
        if (STARTS_WITH_CI(pszDefinition, keyword))
4101
0
        {
4102
0
            return importFromWkt(pszDefinition);
4103
0
        }
4104
0
    }
4105
4106
0
    const bool bStartsWithEPSG = STARTS_WITH_CI(pszDefinition, "EPSG:");
4107
0
    if (bStartsWithEPSG || STARTS_WITH_CI(pszDefinition, "EPSGA:"))
4108
0
    {
4109
0
        OGRErr eStatus = OGRERR_NONE;
4110
4111
0
        if (strchr(pszDefinition, '+') || strchr(pszDefinition, '@'))
4112
0
        {
4113
            // Use proj_create() as it allows things like EPSG:3157+4617
4114
            // that are not normally supported by the below code that
4115
            // builds manually a compound CRS
4116
0
            PJ *pj = proj_create(d->getPROJContext(), pszDefinition);
4117
0
            if (!pj)
4118
0
            {
4119
0
                return OGRERR_FAILURE;
4120
0
            }
4121
0
            Clear();
4122
0
            d->setPjCRS(pj);
4123
0
            return OGRERR_NONE;
4124
0
        }
4125
0
        else
4126
0
        {
4127
0
            eStatus =
4128
0
                importFromEPSG(atoi(pszDefinition + (bStartsWithEPSG ? 5 : 6)));
4129
0
        }
4130
4131
0
        return eStatus;
4132
0
    }
4133
4134
0
    if (STARTS_WITH_CI(pszDefinition, "urn:ogc:def:crs:") ||
4135
0
        STARTS_WITH_CI(pszDefinition, "urn:ogc:def:crs,crs:") ||
4136
0
        STARTS_WITH_CI(pszDefinition, "urn:x-ogc:def:crs:") ||
4137
0
        STARTS_WITH_CI(pszDefinition, "urn:opengis:crs:") ||
4138
0
        STARTS_WITH_CI(pszDefinition, "urn:opengis:def:crs:") ||
4139
0
        STARTS_WITH_CI(pszDefinition, "urn:ogc:def:coordinateMetadata:"))
4140
0
        return importFromURN(pszDefinition);
4141
4142
0
    if (STARTS_WITH_CI(pszDefinition, "http://opengis.net/def/crs") ||
4143
0
        STARTS_WITH_CI(pszDefinition, "https://opengis.net/def/crs") ||
4144
0
        STARTS_WITH_CI(pszDefinition, "http://www.opengis.net/def/crs") ||
4145
0
        STARTS_WITH_CI(pszDefinition, "https://www.opengis.net/def/crs") ||
4146
0
        STARTS_WITH_CI(pszDefinition, "www.opengis.net/def/crs"))
4147
0
        return importFromCRSURL(pszDefinition);
4148
4149
0
    if (STARTS_WITH_CI(pszDefinition, "AUTO:"))
4150
0
        return importFromWMSAUTO(pszDefinition);
4151
4152
    // WMS/WCS OGC codes like OGC:CRS84.
4153
0
    if (EQUAL(pszDefinition, "OGC:CRS84"))
4154
0
        return SetWellKnownGeogCS(pszDefinition + 4);
4155
4156
0
    if (STARTS_WITH_CI(pszDefinition, "CRS:"))
4157
0
        return SetWellKnownGeogCS(pszDefinition);
4158
4159
0
    if (STARTS_WITH_CI(pszDefinition, "DICT:") && strstr(pszDefinition, ","))
4160
0
    {
4161
0
        char *pszFile = CPLStrdup(pszDefinition + 5);
4162
0
        char *pszCode = strstr(pszFile, ",") + 1;
4163
4164
0
        pszCode[-1] = '\0';
4165
4166
0
        OGRErr err = importFromDict(pszFile, pszCode);
4167
0
        CPLFree(pszFile);
4168
4169
0
        return err;
4170
0
    }
4171
4172
0
    if (EQUAL(pszDefinition, "NAD27") || EQUAL(pszDefinition, "NAD83") ||
4173
0
        EQUAL(pszDefinition, "WGS84") || EQUAL(pszDefinition, "WGS72"))
4174
0
    {
4175
0
        Clear();
4176
0
        return SetWellKnownGeogCS(pszDefinition);
4177
0
    }
4178
4179
    // PROJJSON
4180
0
    if (pszDefinition[0] == '{' && strstr(pszDefinition, "\"type\"") &&
4181
0
        (strstr(pszDefinition, "GeodeticCRS") ||
4182
0
         strstr(pszDefinition, "GeographicCRS") ||
4183
0
         strstr(pszDefinition, "ProjectedCRS") ||
4184
0
         strstr(pszDefinition, "VerticalCRS") ||
4185
0
         strstr(pszDefinition, "BoundCRS") ||
4186
0
         strstr(pszDefinition, "CompoundCRS") ||
4187
0
         strstr(pszDefinition, "DerivedGeodeticCRS") ||
4188
0
         strstr(pszDefinition, "DerivedGeographicCRS") ||
4189
0
         strstr(pszDefinition, "DerivedProjectedCRS") ||
4190
0
         strstr(pszDefinition, "DerivedVerticalCRS") ||
4191
0
         strstr(pszDefinition, "EngineeringCRS") ||
4192
0
         strstr(pszDefinition, "DerivedEngineeringCRS") ||
4193
0
         strstr(pszDefinition, "ParametricCRS") ||
4194
0
         strstr(pszDefinition, "DerivedParametricCRS") ||
4195
0
         strstr(pszDefinition, "TemporalCRS") ||
4196
0
         strstr(pszDefinition, "DerivedTemporalCRS")))
4197
0
    {
4198
0
        PJ *pj;
4199
0
        if (strstr(pszDefinition, "datum_ensemble") != nullptr)
4200
0
        {
4201
            // PROJ < 9.0.1 doesn't like a datum_ensemble whose member have
4202
            // a unknown id.
4203
0
            CPLJSONDocument oCRSDoc;
4204
0
            if (!oCRSDoc.LoadMemory(pszDefinition))
4205
0
                return OGRERR_CORRUPT_DATA;
4206
0
            CPLJSONObject oCRSRoot = oCRSDoc.GetRoot();
4207
0
            RemoveIDFromMemberOfEnsembles(oCRSRoot);
4208
0
            pj = proj_create(d->getPROJContext(), oCRSRoot.ToString().c_str());
4209
0
        }
4210
0
        else
4211
0
        {
4212
0
            pj = proj_create(d->getPROJContext(), pszDefinition);
4213
0
        }
4214
0
        if (!pj)
4215
0
        {
4216
0
            return OGRERR_FAILURE;
4217
0
        }
4218
0
        Clear();
4219
0
        d->setPjCRS(pj);
4220
0
        return OGRERR_NONE;
4221
0
    }
4222
4223
0
    if (strstr(pszDefinition, "+proj") != nullptr ||
4224
0
        strstr(pszDefinition, "+init") != nullptr)
4225
0
        return importFromProj4(pszDefinition);
4226
4227
0
    if (STARTS_WITH_CI(pszDefinition, "http://") ||
4228
0
        STARTS_WITH_CI(pszDefinition, "https://"))
4229
0
    {
4230
0
        if (CPLTestBool(CSLFetchNameValueDef(papszOptions,
4231
0
                                             "ALLOW_NETWORK_ACCESS", "YES")))
4232
0
            return importFromUrl(pszDefinition);
4233
4234
0
        CPLError(CE_Failure, CPLE_AppDefined,
4235
0
                 "Cannot import %s due to ALLOW_NETWORK_ACCESS=NO",
4236
0
                 pszDefinition);
4237
0
        return OGRERR_FAILURE;
4238
0
    }
4239
4240
0
    if (EQUAL(pszDefinition, "osgb:BNG"))
4241
0
    {
4242
0
        return importFromEPSG(27700);
4243
0
    }
4244
4245
    // Used by German CityGML files
4246
0
    if (EQUAL(pszDefinition, "urn:adv:crs:ETRS89_UTM32*DE_DHHN92_NH"))
4247
0
    {
4248
        // "ETRS89 / UTM Zone 32N + DHHN92 height"
4249
0
        return SetFromUserInput("EPSG:25832+5783");
4250
0
    }
4251
0
    else if (EQUAL(pszDefinition, "urn:adv:crs:ETRS89_UTM32*DE_DHHN2016_NH"))
4252
0
    {
4253
        // "ETRS89 / UTM Zone 32N + DHHN2016 height"
4254
0
        return SetFromUserInput("EPSG:25832+7837");
4255
0
    }
4256
4257
    // Deal with IGNF:xxx, ESRI:xxx, etc from the PROJ database
4258
0
    const char *pszDot = strrchr(pszDefinition, ':');
4259
0
    if (pszDot)
4260
0
    {
4261
0
        CPLString osPrefix(pszDefinition, pszDot - pszDefinition);
4262
0
        auto authorities =
4263
0
            proj_get_authorities_from_database(d->getPROJContext());
4264
0
        if (authorities)
4265
0
        {
4266
0
            std::set<std::string> aosCandidateAuthorities;
4267
0
            for (auto iter = authorities; *iter; ++iter)
4268
0
            {
4269
0
                if (*iter == osPrefix)
4270
0
                {
4271
0
                    aosCandidateAuthorities.clear();
4272
0
                    aosCandidateAuthorities.insert(*iter);
4273
0
                    break;
4274
0
                }
4275
                // Deal with "IAU_2015" as authority in the list and input
4276
                // "IAU:code"
4277
0
                else if (strncmp(*iter, osPrefix.c_str(), osPrefix.size()) ==
4278
0
                             0 &&
4279
0
                         (*iter)[osPrefix.size()] == '_')
4280
0
                {
4281
0
                    aosCandidateAuthorities.insert(*iter);
4282
0
                }
4283
                // Deal with "IAU_2015" as authority in the list and input
4284
                // "IAU:2015:code"
4285
0
                else if (osPrefix.find(':') != std::string::npos &&
4286
0
                         osPrefix.size() == strlen(*iter) &&
4287
0
                         CPLString(osPrefix).replaceAll(':', '_') == *iter)
4288
0
                {
4289
0
                    aosCandidateAuthorities.clear();
4290
0
                    aosCandidateAuthorities.insert(*iter);
4291
0
                    break;
4292
0
                }
4293
0
            }
4294
4295
0
            proj_string_list_destroy(authorities);
4296
4297
0
            if (!aosCandidateAuthorities.empty())
4298
0
            {
4299
0
                auto obj = proj_create_from_database(
4300
0
                    d->getPROJContext(),
4301
0
                    aosCandidateAuthorities.rbegin()->c_str(), pszDot + 1,
4302
0
                    PJ_CATEGORY_CRS, false, nullptr);
4303
0
                if (!obj)
4304
0
                {
4305
0
                    return OGRERR_FAILURE;
4306
0
                }
4307
0
                Clear();
4308
0
                d->setPjCRS(obj);
4309
0
                return OGRERR_NONE;
4310
0
            }
4311
0
        }
4312
0
    }
4313
4314
    /* -------------------------------------------------------------------- */
4315
    /*      Try to open it as a file.                                       */
4316
    /* -------------------------------------------------------------------- */
4317
0
    if (!CPLTestBool(
4318
0
            CSLFetchNameValueDef(papszOptions, "ALLOW_FILE_ACCESS", "YES")))
4319
0
    {
4320
0
        VSIStatBufL sStat;
4321
0
        if (STARTS_WITH(pszDefinition, "/vsi") ||
4322
0
            VSIStatExL(pszDefinition, &sStat, VSI_STAT_EXISTS_FLAG) == 0)
4323
0
        {
4324
0
            CPLError(CE_Failure, CPLE_AppDefined,
4325
0
                     "Cannot import %s due to ALLOW_FILE_ACCESS=NO",
4326
0
                     pszDefinition);
4327
0
            return OGRERR_FAILURE;
4328
0
        }
4329
        // We used to silently return an error without a CE_Failure message
4330
        // Cf https://github.com/Toblerity/Fiona/issues/1063
4331
0
        return OGRERR_CORRUPT_DATA;
4332
0
    }
4333
4334
0
    CPLConfigOptionSetter oSetter("CPL_ALLOW_VSISTDIN", "NO", true);
4335
0
    VSILFILE *const fp = VSIFOpenL(pszDefinition, "rt");
4336
0
    if (fp == nullptr)
4337
0
        return OGRERR_CORRUPT_DATA;
4338
4339
0
    const size_t nBufMax = 100000;
4340
0
    char *const pszBuffer = static_cast<char *>(CPLMalloc(nBufMax));
4341
0
    const size_t nBytes = VSIFReadL(pszBuffer, 1, nBufMax - 1, fp);
4342
0
    VSIFCloseL(fp);
4343
4344
0
    if (nBytes == nBufMax - 1)
4345
0
    {
4346
0
        CPLDebug("OGR",
4347
0
                 "OGRSpatialReference::SetFromUserInput(%s), opened file "
4348
0
                 "but it is to large for our generous buffer.  Is it really "
4349
0
                 "just a WKT definition?",
4350
0
                 pszDefinition);
4351
0
        CPLFree(pszBuffer);
4352
0
        return OGRERR_FAILURE;
4353
0
    }
4354
4355
0
    pszBuffer[nBytes] = '\0';
4356
4357
0
    char *pszBufPtr = pszBuffer;
4358
0
    while (pszBufPtr[0] == ' ' || pszBufPtr[0] == '\n')
4359
0
        pszBufPtr++;
4360
4361
0
    OGRErr err = OGRERR_NONE;
4362
0
    if (pszBufPtr[0] == '<')
4363
0
        err = importFromXML(pszBufPtr);
4364
0
    else if ((strstr(pszBuffer, "+proj") != nullptr ||
4365
0
              strstr(pszBuffer, "+init") != nullptr) &&
4366
0
             (strstr(pszBuffer, "EXTENSION") == nullptr &&
4367
0
              strstr(pszBuffer, "extension") == nullptr))
4368
0
        err = importFromProj4(pszBufPtr);
4369
0
    else
4370
0
    {
4371
0
        if (STARTS_WITH_CI(pszBufPtr, "ESRI::"))
4372
0
        {
4373
0
            pszBufPtr += 6;
4374
0
        }
4375
4376
        // coverity[tainted_data]
4377
0
        err = importFromWkt(pszBufPtr);
4378
0
    }
4379
4380
0
    CPLFree(pszBuffer);
4381
4382
0
    return err;
4383
0
}
4384
4385
/************************************************************************/
4386
/*                        OSRSetFromUserInput()                         */
4387
/************************************************************************/
4388
4389
/**
4390
 * \brief Set spatial reference from various text formats.
4391
 *
4392
 * This function is the same as OGRSpatialReference::SetFromUserInput()
4393
 *
4394
 * \see OSRSetFromUserInputEx() for a variant allowing to pass options.
4395
 */
4396
OGRErr CPL_STDCALL OSRSetFromUserInput(OGRSpatialReferenceH hSRS,
4397
                                       const char *pszDef)
4398
4399
0
{
4400
0
    VALIDATE_POINTER1(hSRS, "OSRSetFromUserInput", OGRERR_FAILURE);
4401
4402
0
    return ToPointer(hSRS)->SetFromUserInput(pszDef);
4403
0
}
4404
4405
/************************************************************************/
4406
/*                       OSRSetFromUserInputEx()                        */
4407
/************************************************************************/
4408
4409
/**
4410
 * \brief Set spatial reference from various text formats.
4411
 *
4412
 * This function is the same as OGRSpatialReference::SetFromUserInput().
4413
 *
4414
 * @since GDAL 3.9
4415
 */
4416
OGRErr OSRSetFromUserInputEx(OGRSpatialReferenceH hSRS, const char *pszDef,
4417
                             CSLConstList papszOptions)
4418
4419
0
{
4420
0
    VALIDATE_POINTER1(hSRS, "OSRSetFromUserInputEx", OGRERR_FAILURE);
4421
4422
0
    return ToPointer(hSRS)->SetFromUserInput(pszDef, papszOptions);
4423
0
}
4424
4425
/************************************************************************/
4426
/*                          ImportFromUrl()                             */
4427
/************************************************************************/
4428
4429
/**
4430
 * \brief Set spatial reference from a URL.
4431
 *
4432
 * This method will download the spatial reference at a given URL and
4433
 * feed it into SetFromUserInput for you.
4434
 *
4435
 * This method does the same thing as the OSRImportFromUrl() function.
4436
 *
4437
 * @param pszUrl text definition to try to deduce SRS from.
4438
 *
4439
 * @return OGRERR_NONE on success, or an error code with the curl
4440
 * error message if it is unable to download data.
4441
 */
4442
4443
OGRErr OGRSpatialReference::importFromUrl(const char *pszUrl)
4444
4445
0
{
4446
0
    TAKE_OPTIONAL_LOCK();
4447
4448
0
    if (!STARTS_WITH_CI(pszUrl, "http://") &&
4449
0
        !STARTS_WITH_CI(pszUrl, "https://"))
4450
0
    {
4451
0
        CPLError(CE_Failure, CPLE_AppDefined,
4452
0
                 "The given string is not recognized as a URL"
4453
0
                 "starting with 'http://' -- %s",
4454
0
                 pszUrl);
4455
0
        return OGRERR_FAILURE;
4456
0
    }
4457
4458
    /* -------------------------------------------------------------------- */
4459
    /*      Fetch the result.                                               */
4460
    /* -------------------------------------------------------------------- */
4461
0
    CPLErrorReset();
4462
4463
0
    std::string osUrl(pszUrl);
4464
    // We have historically supported "http://spatialreference.org/ref/AUTHNAME/CODE/"
4465
    // as a valid URL since we used a "Accept: application/x-ogcwkt" header
4466
    // to query WKT. To allow a static server to be used, rather append a
4467
    // "ogcwkt/" suffix.
4468
0
    for (const char *pszPrefix : {"https://spatialreference.org/ref/",
4469
0
                                  "http://spatialreference.org/ref/"})
4470
0
    {
4471
0
        if (STARTS_WITH(pszUrl, pszPrefix))
4472
0
        {
4473
0
            const CPLStringList aosTokens(
4474
0
                CSLTokenizeString2(pszUrl + strlen(pszPrefix), "/", 0));
4475
0
            if (aosTokens.size() == 2)
4476
0
            {
4477
0
                osUrl = "https://spatialreference.org/ref/";
4478
0
                osUrl += aosTokens[0];  // authority
4479
0
                osUrl += '/';
4480
0
                osUrl += aosTokens[1];  // code
4481
0
                osUrl += "/ogcwkt/";
4482
0
            }
4483
0
            break;
4484
0
        }
4485
0
    }
4486
4487
0
    const char *pszTimeout = "TIMEOUT=10";
4488
0
    char *apszOptions[] = {const_cast<char *>(pszTimeout), nullptr};
4489
4490
0
    CPLHTTPResult *psResult = CPLHTTPFetch(osUrl.c_str(), apszOptions);
4491
4492
    /* -------------------------------------------------------------------- */
4493
    /*      Try to handle errors.                                           */
4494
    /* -------------------------------------------------------------------- */
4495
4496
0
    if (psResult == nullptr)
4497
0
        return OGRERR_FAILURE;
4498
0
    if (psResult->nDataLen == 0 || CPLGetLastErrorNo() != 0 ||
4499
0
        psResult->pabyData == nullptr)
4500
0
    {
4501
0
        if (CPLGetLastErrorNo() == 0)
4502
0
        {
4503
0
            CPLError(CE_Failure, CPLE_AppDefined,
4504
0
                     "No data was returned from the given URL");
4505
0
        }
4506
0
        CPLHTTPDestroyResult(psResult);
4507
0
        return OGRERR_FAILURE;
4508
0
    }
4509
4510
0
    if (psResult->nStatus != 0)
4511
0
    {
4512
0
        CPLError(CE_Failure, CPLE_AppDefined, "Curl reports error: %d: %s",
4513
0
                 psResult->nStatus, psResult->pszErrBuf);
4514
0
        CPLHTTPDestroyResult(psResult);
4515
0
        return OGRERR_FAILURE;
4516
0
    }
4517
4518
0
    const char *pszData = reinterpret_cast<const char *>(psResult->pabyData);
4519
0
    if (STARTS_WITH_CI(pszData, "http://") ||
4520
0
        STARTS_WITH_CI(pszData, "https://"))
4521
0
    {
4522
0
        CPLError(CE_Failure, CPLE_AppDefined,
4523
0
                 "The data that was downloaded also starts with 'http://' "
4524
0
                 "and cannot be passed into SetFromUserInput.  Is this "
4525
0
                 "really a spatial reference definition? ");
4526
0
        CPLHTTPDestroyResult(psResult);
4527
0
        return OGRERR_FAILURE;
4528
0
    }
4529
0
    if (OGRERR_NONE != SetFromUserInput(pszData))
4530
0
    {
4531
0
        CPLHTTPDestroyResult(psResult);
4532
0
        return OGRERR_FAILURE;
4533
0
    }
4534
4535
0
    CPLHTTPDestroyResult(psResult);
4536
0
    return OGRERR_NONE;
4537
0
}
4538
4539
/************************************************************************/
4540
/*                        OSRimportFromUrl()                            */
4541
/************************************************************************/
4542
4543
/**
4544
 * \brief Set spatial reference from a URL.
4545
 *
4546
 * This function is the same as OGRSpatialReference::importFromUrl()
4547
 */
4548
OGRErr OSRImportFromUrl(OGRSpatialReferenceH hSRS, const char *pszUrl)
4549
4550
0
{
4551
0
    VALIDATE_POINTER1(hSRS, "OSRImportFromUrl", OGRERR_FAILURE);
4552
4553
0
    return ToPointer(hSRS)->importFromUrl(pszUrl);
4554
0
}
4555
4556
/************************************************************************/
4557
/*                         importFromURNPart()                          */
4558
/************************************************************************/
4559
OGRErr OGRSpatialReference::importFromURNPart(const char *pszAuthority,
4560
                                              const char *pszCode,
4561
                                              const char *pszURN)
4562
0
{
4563
0
#if PROJ_AT_LEAST_VERSION(8, 1, 0)
4564
0
    (void)this;
4565
0
    (void)pszAuthority;
4566
0
    (void)pszCode;
4567
0
    (void)pszURN;
4568
0
    return OGRERR_FAILURE;
4569
#else
4570
    /* -------------------------------------------------------------------- */
4571
    /*      Is this an EPSG code? Note that we import it with EPSG          */
4572
    /*      preferred axis ordering for geographic coordinate systems.      */
4573
    /* -------------------------------------------------------------------- */
4574
    if (STARTS_WITH_CI(pszAuthority, "EPSG"))
4575
        return importFromEPSGA(atoi(pszCode));
4576
4577
    /* -------------------------------------------------------------------- */
4578
    /*      Is this an IAU code?  Lets try for the IAU2000 dictionary.      */
4579
    /* -------------------------------------------------------------------- */
4580
    if (STARTS_WITH_CI(pszAuthority, "IAU"))
4581
        return importFromDict("IAU2000.wkt", pszCode);
4582
4583
    /* -------------------------------------------------------------------- */
4584
    /*      Is this an OGC code?                                            */
4585
    /* -------------------------------------------------------------------- */
4586
    if (!STARTS_WITH_CI(pszAuthority, "OGC"))
4587
    {
4588
        CPLError(CE_Failure, CPLE_AppDefined,
4589
                 "URN %s has unrecognized authority.", pszURN);
4590
        return OGRERR_FAILURE;
4591
    }
4592
4593
    if (STARTS_WITH_CI(pszCode, "CRS84"))
4594
        return SetWellKnownGeogCS(pszCode);
4595
    else if (STARTS_WITH_CI(pszCode, "CRS83"))
4596
        return SetWellKnownGeogCS(pszCode);
4597
    else if (STARTS_WITH_CI(pszCode, "CRS27"))
4598
        return SetWellKnownGeogCS(pszCode);
4599
    else if (STARTS_WITH_CI(pszCode, "84"))  // urn:ogc:def:crs:OGC:2:84
4600
        return SetWellKnownGeogCS("CRS84");
4601
4602
    /* -------------------------------------------------------------------- */
4603
    /*      Handle auto codes.  We need to convert from format              */
4604
    /*      AUTO42001:99:8888 to format AUTO:42001,99,8888.                 */
4605
    /* -------------------------------------------------------------------- */
4606
    else if (STARTS_WITH_CI(pszCode, "AUTO"))
4607
    {
4608
        char szWMSAuto[100] = {'\0'};
4609
4610
        if (strlen(pszCode) > sizeof(szWMSAuto) - 2)
4611
            return OGRERR_FAILURE;
4612
4613
        snprintf(szWMSAuto, sizeof(szWMSAuto), "AUTO:%s", pszCode + 4);
4614
        for (int i = 5; szWMSAuto[i] != '\0'; i++)
4615
        {
4616
            if (szWMSAuto[i] == ':')
4617
                szWMSAuto[i] = ',';
4618
        }
4619
4620
        return importFromWMSAUTO(szWMSAuto);
4621
    }
4622
4623
    /* -------------------------------------------------------------------- */
4624
    /*      Not a recognise OGC item.                                       */
4625
    /* -------------------------------------------------------------------- */
4626
    CPLError(CE_Failure, CPLE_AppDefined, "URN %s value not supported.",
4627
             pszURN);
4628
4629
    return OGRERR_FAILURE;
4630
#endif
4631
0
}
4632
4633
/************************************************************************/
4634
/*                           importFromURN()                            */
4635
/*                                                                      */
4636
/*      See OGC recommendation paper 06-023r1 or later for details.     */
4637
/************************************************************************/
4638
4639
/**
4640
 * \brief Initialize from OGC URN.
4641
 *
4642
 * Initializes this spatial reference from a coordinate system defined
4643
 * by an OGC URN prefixed with "urn:ogc:def:crs:" per recommendation
4644
 * paper 06-023r1.  Currently EPSG and OGC authority values are supported,
4645
 * including OGC auto codes, but not including CRS1 or CRS88 (NAVD88).
4646
 *
4647
 * This method is also support through SetFromUserInput() which can
4648
 * normally be used for URNs.
4649
 *
4650
 * @param pszURN the urn string.
4651
 *
4652
 * @return OGRERR_NONE on success or an error code.
4653
 */
4654
4655
OGRErr OGRSpatialReference::importFromURN(const char *pszURN)
4656
4657
0
{
4658
0
    constexpr const char *EPSG_URN_CRS_PREFIX = "urn:ogc:def:crs:EPSG::";
4659
0
    if (STARTS_WITH(pszURN, EPSG_URN_CRS_PREFIX) &&
4660
0
        CPLGetValueType(pszURN + strlen(EPSG_URN_CRS_PREFIX)) ==
4661
0
            CPL_VALUE_INTEGER)
4662
0
    {
4663
0
        return importFromEPSG(atoi(pszURN + strlen(EPSG_URN_CRS_PREFIX)));
4664
0
    }
4665
4666
0
    TAKE_OPTIONAL_LOCK();
4667
4668
0
#if PROJ_AT_LEAST_VERSION(8, 1, 0)
4669
4670
    // PROJ 8.2.0 has support for IAU codes now.
4671
#if !PROJ_AT_LEAST_VERSION(8, 2, 0)
4672
    /* -------------------------------------------------------------------- */
4673
    /*      Is this an IAU code?  Lets try for the IAU2000 dictionary.      */
4674
    /* -------------------------------------------------------------------- */
4675
    const char *pszIAU = strstr(pszURN, "IAU");
4676
    if (pszIAU)
4677
    {
4678
        const char *pszCode = strchr(pszIAU, ':');
4679
        if (pszCode)
4680
        {
4681
            ++pszCode;
4682
            if (*pszCode == ':')
4683
                ++pszCode;
4684
            return importFromDict("IAU2000.wkt", pszCode);
4685
        }
4686
    }
4687
#endif
4688
4689
0
    if (strlen(pszURN) >= 1000)
4690
0
    {
4691
0
        CPLError(CE_Failure, CPLE_AppDefined, "Too long input string");
4692
0
        return OGRERR_CORRUPT_DATA;
4693
0
    }
4694
0
    auto obj = proj_create(d->getPROJContext(), pszURN);
4695
0
    if (!obj)
4696
0
    {
4697
0
        return OGRERR_FAILURE;
4698
0
    }
4699
0
    Clear();
4700
0
    d->setPjCRS(obj);
4701
0
    return OGRERR_NONE;
4702
#else
4703
    const char *pszCur = nullptr;
4704
4705
    if (STARTS_WITH_CI(pszURN, "urn:ogc:def:crs:"))
4706
        pszCur = pszURN + 16;
4707
    else if (STARTS_WITH_CI(pszURN, "urn:ogc:def:crs,crs:"))
4708
        pszCur = pszURN + 20;
4709
    else if (STARTS_WITH_CI(pszURN, "urn:x-ogc:def:crs:"))
4710
        pszCur = pszURN + 18;
4711
    else if (STARTS_WITH_CI(pszURN, "urn:opengis:crs:"))
4712
        pszCur = pszURN + 16;
4713
    else if (STARTS_WITH_CI(pszURN, "urn:opengis:def:crs:"))
4714
        pszCur = pszURN + 20;
4715
    else
4716
    {
4717
        CPLError(CE_Failure, CPLE_AppDefined, "URN %s not a supported format.",
4718
                 pszURN);
4719
        return OGRERR_FAILURE;
4720
    }
4721
4722
    /* -------------------------------------------------------------------- */
4723
    /*      Clear any existing definition.                                  */
4724
    /* -------------------------------------------------------------------- */
4725
    Clear();
4726
4727
    /* -------------------------------------------------------------------- */
4728
    /*      Find code (ignoring version) out of string like:                */
4729
    /*                                                                      */
4730
    /*      authority:[version]:code                                        */
4731
    /* -------------------------------------------------------------------- */
4732
    const char *pszAuthority = pszCur;
4733
4734
    // skip authority
4735
    while (*pszCur != ':' && *pszCur)
4736
        pszCur++;
4737
    if (*pszCur == ':')
4738
        pszCur++;
4739
4740
    // skip version
4741
    const char *pszBeforeVersion = pszCur;
4742
    while (*pszCur != ':' && *pszCur)
4743
        pszCur++;
4744
    if (*pszCur == ':')
4745
        pszCur++;
4746
    else
4747
        // We come here in the case, the content to parse is authority:code
4748
        // (instead of authority::code) which is probably illegal according to
4749
        // http://www.opengeospatial.org/ogcUrnPolicy but such content is found
4750
        // for example in what is returned by GeoServer.
4751
        pszCur = pszBeforeVersion;
4752
4753
    const char *pszCode = pszCur;
4754
4755
    const char *pszComma = strchr(pszCur, ',');
4756
    if (pszComma == nullptr)
4757
        return importFromURNPart(pszAuthority, pszCode, pszURN);
4758
4759
    // There's a second part with the vertical SRS.
4760
    pszCur = pszComma + 1;
4761
    if (!STARTS_WITH(pszCur, "crs:"))
4762
    {
4763
        CPLError(CE_Failure, CPLE_AppDefined, "URN %s not a supported format.",
4764
                 pszURN);
4765
        return OGRERR_FAILURE;
4766
    }
4767
4768
    pszCur += 4;
4769
4770
    char *pszFirstCode = CPLStrdup(pszCode);
4771
    pszFirstCode[pszComma - pszCode] = '\0';
4772
    OGRErr eStatus = importFromURNPart(pszAuthority, pszFirstCode, pszURN);
4773
    CPLFree(pszFirstCode);
4774
4775
    // Do we want to turn this into a compound definition
4776
    // with a vertical datum?
4777
    if (eStatus != OGRERR_NONE)
4778
        return eStatus;
4779
4780
    /* -------------------------------------------------------------------- */
4781
    /*      Find code (ignoring version) out of string like:                */
4782
    /*                                                                      */
4783
    /*      authority:[version]:code                                        */
4784
    /* -------------------------------------------------------------------- */
4785
    pszAuthority = pszCur;
4786
4787
    // skip authority
4788
    while (*pszCur != ':' && *pszCur)
4789
        pszCur++;
4790
    if (*pszCur == ':')
4791
        pszCur++;
4792
4793
    // skip version
4794
    pszBeforeVersion = pszCur;
4795
    while (*pszCur != ':' && *pszCur)
4796
        pszCur++;
4797
    if (*pszCur == ':')
4798
        pszCur++;
4799
    else
4800
        pszCur = pszBeforeVersion;
4801
4802
    pszCode = pszCur;
4803
4804
    OGRSpatialReference oVertSRS;
4805
    eStatus = oVertSRS.importFromURNPart(pszAuthority, pszCode, pszURN);
4806
    if (eStatus == OGRERR_NONE)
4807
    {
4808
        OGRSpatialReference oHorizSRS(*this);
4809
4810
        Clear();
4811
4812
        oHorizSRS.d->refreshProjObj();
4813
        oVertSRS.d->refreshProjObj();
4814
        if (!oHorizSRS.d->m_pj_crs || !oVertSRS.d->m_pj_crs)
4815
            return OGRERR_FAILURE;
4816
4817
        const char *pszHorizName = proj_get_name(oHorizSRS.d->m_pj_crs);
4818
        const char *pszVertName = proj_get_name(oVertSRS.d->m_pj_crs);
4819
4820
        CPLString osName = pszHorizName ? pszHorizName : "";
4821
        osName += " + ";
4822
        osName += pszVertName ? pszVertName : "";
4823
4824
        SetCompoundCS(osName, &oHorizSRS, &oVertSRS);
4825
    }
4826
4827
    return eStatus;
4828
#endif
4829
0
}
4830
4831
/************************************************************************/
4832
/*                           importFromCRSURL()                         */
4833
/*                                                                      */
4834
/*      See OGC Best Practice document 11-135 for details.              */
4835
/************************************************************************/
4836
4837
/**
4838
 * \brief Initialize from OGC URL.
4839
 *
4840
 * Initializes this spatial reference from a coordinate system defined
4841
 * by an OGC URL prefixed with "http://opengis.net/def/crs" per best practice
4842
 * paper 11-135.  Currently EPSG and OGC authority values are supported,
4843
 * including OGC auto codes, but not including CRS1 or CRS88 (NAVD88).
4844
 *
4845
 * This method is also supported through SetFromUserInput() which can
4846
 * normally be used for URLs.
4847
 *
4848
 * @param pszURL the URL string.
4849
 *
4850
 * @return OGRERR_NONE on success or an error code.
4851
 */
4852
4853
OGRErr OGRSpatialReference::importFromCRSURL(const char *pszURL)
4854
4855
0
{
4856
0
    TAKE_OPTIONAL_LOCK();
4857
4858
0
#if PROJ_AT_LEAST_VERSION(8, 1, 0)
4859
0
    if (strlen(pszURL) >= 10000)
4860
0
    {
4861
0
        CPLError(CE_Failure, CPLE_AppDefined, "Too long input string");
4862
0
        return OGRERR_CORRUPT_DATA;
4863
0
    }
4864
4865
0
    PJ *obj;
4866
#if !PROJ_AT_LEAST_VERSION(9, 2, 0)
4867
    if (STARTS_WITH(pszURL, "http://www.opengis.net/def/crs/IAU/0/"))
4868
    {
4869
        obj = proj_create(
4870
            d->getPROJContext(),
4871
            CPLSPrintf("IAU:%s",
4872
                       pszURL +
4873
                           strlen("http://www.opengis.net/def/crs/IAU/0/")));
4874
    }
4875
    else
4876
#endif
4877
0
    {
4878
0
        obj = proj_create(d->getPROJContext(), pszURL);
4879
0
    }
4880
0
    if (!obj)
4881
0
    {
4882
0
        return OGRERR_FAILURE;
4883
0
    }
4884
0
    Clear();
4885
0
    d->setPjCRS(obj);
4886
0
    return OGRERR_NONE;
4887
#else
4888
    const char *pszCur = nullptr;
4889
4890
    if (STARTS_WITH_CI(pszURL, "http://opengis.net/def/crs"))
4891
        pszCur = pszURL + 26;
4892
    else if (STARTS_WITH_CI(pszURL, "https://opengis.net/def/crs"))
4893
        pszCur = pszURL + 27;
4894
    else if (STARTS_WITH_CI(pszURL, "http://www.opengis.net/def/crs"))
4895
        pszCur = pszURL + 30;
4896
    else if (STARTS_WITH_CI(pszURL, "https://www.opengis.net/def/crs"))
4897
        pszCur = pszURL + 31;
4898
    else if (STARTS_WITH_CI(pszURL, "www.opengis.net/def/crs"))
4899
        pszCur = pszURL + 23;
4900
    else
4901
    {
4902
        CPLError(CE_Failure, CPLE_AppDefined, "URL %s not a supported format.",
4903
                 pszURL);
4904
        return OGRERR_FAILURE;
4905
    }
4906
4907
    if (*pszCur == '\0')
4908
    {
4909
        CPLError(CE_Failure, CPLE_AppDefined, "URL %s malformed.", pszURL);
4910
        return OGRERR_FAILURE;
4911
    }
4912
4913
    /* -------------------------------------------------------------------- */
4914
    /*      Clear any existing definition.                                  */
4915
    /* -------------------------------------------------------------------- */
4916
    Clear();
4917
4918
    if (STARTS_WITH_CI(pszCur, "-compound?1="))
4919
    {
4920
        /* --------------------------------------------------------------------
4921
         */
4922
        /*      It's a compound CRS, of the form: */
4923
        /*                                                                      */
4924
        /*      http://opengis.net/def/crs-compound?1=URL1&2=URL2&3=URL3&.. */
4925
        /* --------------------------------------------------------------------
4926
         */
4927
        pszCur += 12;
4928
4929
        // Extract each component CRS URL.
4930
        int iComponentUrl = 2;
4931
4932
        CPLString osName = "";
4933
        Clear();
4934
4935
        while (iComponentUrl != -1)
4936
        {
4937
            char searchStr[15] = {};
4938
            snprintf(searchStr, sizeof(searchStr), "&%d=", iComponentUrl);
4939
4940
            const char *pszUrlEnd = strstr(pszCur, searchStr);
4941
4942
            // Figure out the next component URL.
4943
            char *pszComponentUrl = nullptr;
4944
4945
            if (pszUrlEnd)
4946
            {
4947
                size_t nLen = pszUrlEnd - pszCur;
4948
                pszComponentUrl = static_cast<char *>(CPLMalloc(nLen + 1));
4949
                strncpy(pszComponentUrl, pszCur, nLen);
4950
                pszComponentUrl[nLen] = '\0';
4951
4952
                ++iComponentUrl;
4953
                pszCur += nLen + strlen(searchStr);
4954
            }
4955
            else
4956
            {
4957
                if (iComponentUrl == 2)
4958
                {
4959
                    CPLError(CE_Failure, CPLE_AppDefined,
4960
                             "Compound CRS URLs must have at least two "
4961
                             "component CRSs.");
4962
                    return OGRERR_FAILURE;
4963
                }
4964
                else
4965
                {
4966
                    pszComponentUrl = CPLStrdup(pszCur);
4967
                    // no more components
4968
                    iComponentUrl = -1;
4969
                }
4970
            }
4971
4972
            OGRSpatialReference oComponentSRS;
4973
            OGRErr eStatus = oComponentSRS.importFromCRSURL(pszComponentUrl);
4974
4975
            CPLFree(pszComponentUrl);
4976
            pszComponentUrl = nullptr;
4977
4978
            if (eStatus == OGRERR_NONE)
4979
            {
4980
                if (osName.length() != 0)
4981
                {
4982
                    osName += " + ";
4983
                }
4984
                osName += oComponentSRS.GetRoot()->GetValue();
4985
                SetNode("COMPD_CS", osName);
4986
                GetRoot()->AddChild(oComponentSRS.GetRoot()->Clone());
4987
            }
4988
            else
4989
                return eStatus;
4990
        }
4991
4992
        return OGRERR_NONE;
4993
    }
4994
4995
    /* -------------------------------------------------------------------- */
4996
    /*      It's a normal CRS URL, of the form:                             */
4997
    /*                                                                      */
4998
    /*      http://opengis.net/def/crs/AUTHORITY/VERSION/CODE               */
4999
    /* -------------------------------------------------------------------- */
5000
    ++pszCur;
5001
    const char *pszAuthority = pszCur;
5002
5003
    // skip authority
5004
    while (*pszCur != '/' && *pszCur)
5005
        pszCur++;
5006
    if (*pszCur == '/')
5007
        pszCur++;
5008
5009
    // skip version
5010
    while (*pszCur != '/' && *pszCur)
5011
        pszCur++;
5012
    if (*pszCur == '/')
5013
        pszCur++;
5014
5015
    const char *pszCode = pszCur;
5016
5017
    return importFromURNPart(pszAuthority, pszCode, pszURL);
5018
#endif
5019
0
}
5020
5021
/************************************************************************/
5022
/*                         importFromWMSAUTO()                          */
5023
/************************************************************************/
5024
5025
/**
5026
 * \brief Initialize from WMSAUTO string.
5027
 *
5028
 * Note that the WMS 1.3 specification does not include the
5029
 * units code, while apparently earlier specs do.  We try to
5030
 * guess around this.
5031
 *
5032
 * @param pszDefinition the WMSAUTO string
5033
 *
5034
 * @return OGRERR_NONE on success or an error code.
5035
 */
5036
OGRErr OGRSpatialReference::importFromWMSAUTO(const char *pszDefinition)
5037
5038
0
{
5039
0
    TAKE_OPTIONAL_LOCK();
5040
5041
0
#if PROJ_AT_LEAST_VERSION(8, 1, 0)
5042
0
    if (strlen(pszDefinition) >= 10000)
5043
0
    {
5044
0
        CPLError(CE_Failure, CPLE_AppDefined, "Too long input string");
5045
0
        return OGRERR_CORRUPT_DATA;
5046
0
    }
5047
5048
0
    auto obj = proj_create(d->getPROJContext(), pszDefinition);
5049
0
    if (!obj)
5050
0
    {
5051
0
        return OGRERR_FAILURE;
5052
0
    }
5053
0
    Clear();
5054
0
    d->setPjCRS(obj);
5055
0
    return OGRERR_NONE;
5056
#else
5057
    int nProjId, nUnitsId;
5058
    double dfRefLong, dfRefLat = 0.0;
5059
5060
    /* -------------------------------------------------------------------- */
5061
    /*      Tokenize                                                        */
5062
    /* -------------------------------------------------------------------- */
5063
    if (STARTS_WITH_CI(pszDefinition, "AUTO:"))
5064
        pszDefinition += 5;
5065
5066
    char **papszTokens =
5067
        CSLTokenizeStringComplex(pszDefinition, ",", FALSE, TRUE);
5068
5069
    if (CSLCount(papszTokens) == 4)
5070
    {
5071
        nProjId = atoi(papszTokens[0]);
5072
        nUnitsId = atoi(papszTokens[1]);
5073
        dfRefLong = CPLAtof(papszTokens[2]);
5074
        dfRefLat = CPLAtof(papszTokens[3]);
5075
    }
5076
    else if (CSLCount(papszTokens) == 3 && atoi(papszTokens[0]) == 42005)
5077
    {
5078
        nProjId = atoi(papszTokens[0]);
5079
        nUnitsId = atoi(papszTokens[1]);
5080
        dfRefLong = CPLAtof(papszTokens[2]);
5081
        dfRefLat = 0.0;
5082
    }
5083
    else if (CSLCount(papszTokens) == 3)
5084
    {
5085
        nProjId = atoi(papszTokens[0]);
5086
        nUnitsId = 9001;
5087
        dfRefLong = CPLAtof(papszTokens[1]);
5088
        dfRefLat = CPLAtof(papszTokens[2]);
5089
    }
5090
    else if (CSLCount(papszTokens) == 2 && atoi(papszTokens[0]) == 42005)
5091
    {
5092
        nProjId = atoi(papszTokens[0]);
5093
        nUnitsId = 9001;
5094
        dfRefLong = CPLAtof(papszTokens[1]);
5095
    }
5096
    else
5097
    {
5098
        CSLDestroy(papszTokens);
5099
        CPLError(CE_Failure, CPLE_AppDefined,
5100
                 "AUTO projection has wrong number of arguments, expected\n"
5101
                 "AUTO:proj_id,units_id,ref_long,ref_lat or"
5102
                 "AUTO:proj_id,ref_long,ref_lat");
5103
        return OGRERR_FAILURE;
5104
    }
5105
5106
    CSLDestroy(papszTokens);
5107
    papszTokens = nullptr;
5108
5109
    /* -------------------------------------------------------------------- */
5110
    /*      Build coordsys.                                                 */
5111
    /* -------------------------------------------------------------------- */
5112
    Clear();
5113
5114
    /* -------------------------------------------------------------------- */
5115
    /*      Set WGS84.                                                      */
5116
    /* -------------------------------------------------------------------- */
5117
    SetWellKnownGeogCS("WGS84");
5118
5119
    switch (nProjId)
5120
    {
5121
        case 42001:  // Auto UTM
5122
            SetUTM(static_cast<int>(floor((dfRefLong + 180.0) / 6.0)) + 1,
5123
                   dfRefLat >= 0.0);
5124
            break;
5125
5126
        case 42002:  // Auto TM (strangely very UTM-like).
5127
            SetTM(0, dfRefLong, 0.9996, 500000.0,
5128
                  (dfRefLat >= 0.0) ? 0.0 : 10000000.0);
5129
            break;
5130
5131
        case 42003:  // Auto Orthographic.
5132
            SetOrthographic(dfRefLat, dfRefLong, 0.0, 0.0);
5133
            break;
5134
5135
        case 42004:  // Auto Equirectangular
5136
            SetEquirectangular(dfRefLat, dfRefLong, 0.0, 0.0);
5137
            break;
5138
5139
        case 42005:
5140
            SetMollweide(dfRefLong, 0.0, 0.0);
5141
            break;
5142
5143
        default:
5144
            CPLError(CE_Failure, CPLE_AppDefined,
5145
                     "Unsupported projection id in importFromWMSAUTO(): %d",
5146
                     nProjId);
5147
            return OGRERR_FAILURE;
5148
    }
5149
5150
    /* -------------------------------------------------------------------- */
5151
    /*      Set units.                                                      */
5152
    /* -------------------------------------------------------------------- */
5153
5154
    switch (nUnitsId)
5155
    {
5156
        case 9001:
5157
            SetTargetLinearUnits(nullptr, SRS_UL_METER, 1.0, "EPSG", "9001");
5158
            break;
5159
5160
        case 9002:
5161
            SetTargetLinearUnits(nullptr, "Foot", 0.3048, "EPSG", "9002");
5162
            break;
5163
5164
        case 9003:
5165
            SetTargetLinearUnits(nullptr, "US survey foot",
5166
                                 CPLAtof(SRS_UL_US_FOOT_CONV), "EPSG", "9003");
5167
            break;
5168
5169
        default:
5170
            CPLError(CE_Failure, CPLE_AppDefined,
5171
                     "Unsupported units code (%d).", nUnitsId);
5172
            return OGRERR_FAILURE;
5173
            break;
5174
    }
5175
5176
    return OGRERR_NONE;
5177
#endif
5178
0
}
5179
5180
/************************************************************************/
5181
/*                            GetSemiMajor()                            */
5182
/************************************************************************/
5183
5184
/**
5185
 * \brief Get spheroid semi major axis (in metres starting with GDAL 3.0)
5186
 *
5187
 * This method does the same thing as the C function OSRGetSemiMajor().
5188
 *
5189
 * @param pnErr if non-NULL set to OGRERR_FAILURE if semi major axis
5190
 * can be found.
5191
 *
5192
 * @return semi-major axis, or SRS_WGS84_SEMIMAJOR if it can't be found.
5193
 */
5194
5195
double OGRSpatialReference::GetSemiMajor(OGRErr *pnErr) const
5196
5197
0
{
5198
0
    TAKE_OPTIONAL_LOCK();
5199
5200
0
    if (pnErr != nullptr)
5201
0
        *pnErr = OGRERR_FAILURE;
5202
5203
0
    d->refreshProjObj();
5204
0
    if (!d->m_pj_crs)
5205
0
        return SRS_WGS84_SEMIMAJOR;
5206
5207
0
    auto ellps = proj_get_ellipsoid(d->getPROJContext(), d->m_pj_crs);
5208
0
    if (!ellps)
5209
0
        return SRS_WGS84_SEMIMAJOR;
5210
5211
0
    double dfSemiMajor = 0.0;
5212
0
    proj_ellipsoid_get_parameters(d->getPROJContext(), ellps, &dfSemiMajor,
5213
0
                                  nullptr, nullptr, nullptr);
5214
0
    proj_destroy(ellps);
5215
5216
0
    if (dfSemiMajor > 0)
5217
0
    {
5218
0
        if (pnErr != nullptr)
5219
0
            *pnErr = OGRERR_NONE;
5220
0
        return dfSemiMajor;
5221
0
    }
5222
5223
0
    return SRS_WGS84_SEMIMAJOR;
5224
0
}
5225
5226
/************************************************************************/
5227
/*                          OSRGetSemiMajor()                           */
5228
/************************************************************************/
5229
5230
/**
5231
 * \brief Get spheroid semi major axis.
5232
 *
5233
 * This function is the same as OGRSpatialReference::GetSemiMajor()
5234
 */
5235
double OSRGetSemiMajor(OGRSpatialReferenceH hSRS, OGRErr *pnErr)
5236
5237
0
{
5238
0
    VALIDATE_POINTER1(hSRS, "OSRGetSemiMajor", 0);
5239
5240
0
    return ToPointer(hSRS)->GetSemiMajor(pnErr);
5241
0
}
5242
5243
/************************************************************************/
5244
/*                          GetInvFlattening()                          */
5245
/************************************************************************/
5246
5247
/**
5248
 * \brief Get spheroid inverse flattening.
5249
 *
5250
 * This method does the same thing as the C function OSRGetInvFlattening().
5251
 *
5252
 * @param pnErr if non-NULL set to OGRERR_FAILURE if no inverse flattening
5253
 * can be found.
5254
 *
5255
 * @return inverse flattening, or SRS_WGS84_INVFLATTENING if it can't be found.
5256
 */
5257
5258
double OGRSpatialReference::GetInvFlattening(OGRErr *pnErr) const
5259
5260
0
{
5261
0
    TAKE_OPTIONAL_LOCK();
5262
5263
0
    if (pnErr != nullptr)
5264
0
        *pnErr = OGRERR_FAILURE;
5265
5266
0
    d->refreshProjObj();
5267
0
    if (!d->m_pj_crs)
5268
0
        return SRS_WGS84_INVFLATTENING;
5269
5270
0
    auto ellps = proj_get_ellipsoid(d->getPROJContext(), d->m_pj_crs);
5271
0
    if (!ellps)
5272
0
        return SRS_WGS84_INVFLATTENING;
5273
5274
0
    double dfInvFlattening = -1.0;
5275
0
    proj_ellipsoid_get_parameters(d->getPROJContext(), ellps, nullptr, nullptr,
5276
0
                                  nullptr, &dfInvFlattening);
5277
0
    proj_destroy(ellps);
5278
5279
0
    if (dfInvFlattening >= 0.0)
5280
0
    {
5281
0
        if (pnErr != nullptr)
5282
0
            *pnErr = OGRERR_NONE;
5283
0
        return dfInvFlattening;
5284
0
    }
5285
5286
0
    return SRS_WGS84_INVFLATTENING;
5287
0
}
5288
5289
/************************************************************************/
5290
/*                        OSRGetInvFlattening()                         */
5291
/************************************************************************/
5292
5293
/**
5294
 * \brief Get spheroid inverse flattening.
5295
 *
5296
 * This function is the same as OGRSpatialReference::GetInvFlattening()
5297
 */
5298
double OSRGetInvFlattening(OGRSpatialReferenceH hSRS, OGRErr *pnErr)
5299
5300
0
{
5301
0
    VALIDATE_POINTER1(hSRS, "OSRGetInvFlattening", 0);
5302
5303
0
    return ToPointer(hSRS)->GetInvFlattening(pnErr);
5304
0
}
5305
5306
/************************************************************************/
5307
/*                           GetEccentricity()                          */
5308
/************************************************************************/
5309
5310
/**
5311
 * \brief Get spheroid eccentricity
5312
 *
5313
 * @return eccentricity (or -1 in case of error)
5314
 * @since GDAL 2.3
5315
 */
5316
5317
double OGRSpatialReference::GetEccentricity() const
5318
5319
0
{
5320
0
    OGRErr eErr = OGRERR_NONE;
5321
0
    const double dfInvFlattening = GetInvFlattening(&eErr);
5322
0
    if (eErr != OGRERR_NONE)
5323
0
    {
5324
0
        return -1.0;
5325
0
    }
5326
0
    if (dfInvFlattening == 0.0)
5327
0
        return 0.0;
5328
0
    if (dfInvFlattening < 0.5)
5329
0
        return -1.0;
5330
0
    return sqrt(2.0 / dfInvFlattening -
5331
0
                1.0 / (dfInvFlattening * dfInvFlattening));
5332
0
}
5333
5334
/************************************************************************/
5335
/*                      GetSquaredEccentricity()                        */
5336
/************************************************************************/
5337
5338
/**
5339
 * \brief Get spheroid squared eccentricity
5340
 *
5341
 * @return squared eccentricity (or -1 in case of error)
5342
 * @since GDAL 2.3
5343
 */
5344
5345
double OGRSpatialReference::GetSquaredEccentricity() const
5346
5347
0
{
5348
0
    OGRErr eErr = OGRERR_NONE;
5349
0
    const double dfInvFlattening = GetInvFlattening(&eErr);
5350
0
    if (eErr != OGRERR_NONE)
5351
0
    {
5352
0
        return -1.0;
5353
0
    }
5354
0
    if (dfInvFlattening == 0.0)
5355
0
        return 0.0;
5356
0
    if (dfInvFlattening < 0.5)
5357
0
        return -1.0;
5358
0
    return 2.0 / dfInvFlattening - 1.0 / (dfInvFlattening * dfInvFlattening);
5359
0
}
5360
5361
/************************************************************************/
5362
/*                            GetSemiMinor()                            */
5363
/************************************************************************/
5364
5365
/**
5366
 * \brief Get spheroid semi minor axis.
5367
 *
5368
 * This method does the same thing as the C function OSRGetSemiMinor().
5369
 *
5370
 * @param pnErr if non-NULL set to OGRERR_FAILURE if semi minor axis
5371
 * can be found.
5372
 *
5373
 * @return semi-minor axis, or WGS84 semi minor if it can't be found.
5374
 */
5375
5376
double OGRSpatialReference::GetSemiMinor(OGRErr *pnErr) const
5377
5378
0
{
5379
0
    const double dfSemiMajor = GetSemiMajor(pnErr);
5380
0
    const double dfInvFlattening = GetInvFlattening(pnErr);
5381
5382
0
    return OSRCalcSemiMinorFromInvFlattening(dfSemiMajor, dfInvFlattening);
5383
0
}
5384
5385
/************************************************************************/
5386
/*                          OSRGetSemiMinor()                           */
5387
/************************************************************************/
5388
5389
/**
5390
 * \brief Get spheroid semi minor axis.
5391
 *
5392
 * This function is the same as OGRSpatialReference::GetSemiMinor()
5393
 */
5394
double OSRGetSemiMinor(OGRSpatialReferenceH hSRS, OGRErr *pnErr)
5395
5396
0
{
5397
0
    VALIDATE_POINTER1(hSRS, "OSRGetSemiMinor", 0);
5398
5399
0
    return ToPointer(hSRS)->GetSemiMinor(pnErr);
5400
0
}
5401
5402
/************************************************************************/
5403
/*                             SetLocalCS()                             */
5404
/************************************************************************/
5405
5406
/**
5407
 * \brief Set the user visible LOCAL_CS name.
5408
 *
5409
 * This method is the same as the C function OSRSetLocalCS().
5410
 *
5411
 * This method will ensure a LOCAL_CS node is created as the root,
5412
 * and set the provided name on it.  It must be used before SetLinearUnits().
5413
 *
5414
 * @param pszName the user visible name to assign.  Not used as a key.
5415
 *
5416
 * @return OGRERR_NONE on success.
5417
 */
5418
5419
OGRErr OGRSpatialReference::SetLocalCS(const char *pszName)
5420
5421
0
{
5422
0
    TAKE_OPTIONAL_LOCK();
5423
5424
0
    if (d->m_pjType == PJ_TYPE_UNKNOWN ||
5425
0
        d->m_pjType == PJ_TYPE_ENGINEERING_CRS)
5426
0
    {
5427
0
        d->setPjCRS(proj_create_engineering_crs(d->getPROJContext(), pszName));
5428
0
    }
5429
0
    else
5430
0
    {
5431
0
        CPLDebug("OGR",
5432
0
                 "OGRSpatialReference::SetLocalCS(%s) failed.  "
5433
0
                 "It appears an incompatible object already exists.",
5434
0
                 pszName);
5435
0
        return OGRERR_FAILURE;
5436
0
    }
5437
5438
0
    return OGRERR_NONE;
5439
0
}
5440
5441
/************************************************************************/
5442
/*                           OSRSetLocalCS()                            */
5443
/************************************************************************/
5444
5445
/**
5446
 * \brief Set the user visible LOCAL_CS name.
5447
 *
5448
 * This function is the same as OGRSpatialReference::SetLocalCS()
5449
 */
5450
OGRErr OSRSetLocalCS(OGRSpatialReferenceH hSRS, const char *pszName)
5451
5452
0
{
5453
0
    VALIDATE_POINTER1(hSRS, "OSRSetLocalCS", OGRERR_FAILURE);
5454
5455
0
    return ToPointer(hSRS)->SetLocalCS(pszName);
5456
0
}
5457
5458
/************************************************************************/
5459
/*                             SetGeocCS()                              */
5460
/************************************************************************/
5461
5462
/**
5463
 * \brief Set the user visible GEOCCS name.
5464
 *
5465
 * This method is the same as the C function OSRSetGeocCS().
5466
5467
 * This method will ensure a GEOCCS node is created as the root,
5468
 * and set the provided name on it.  If used on a GEOGCS coordinate system,
5469
 * the DATUM and PRIMEM nodes from the GEOGCS will be transferred over to
5470
 * the GEOGCS.
5471
 *
5472
 * @param pszName the user visible name to assign.  Not used as a key.
5473
 *
5474
 * @return OGRERR_NONE on success.
5475
 *
5476
 * @since OGR 1.9.0
5477
 */
5478
5479
OGRErr OGRSpatialReference::SetGeocCS(const char *pszName)
5480
5481
0
{
5482
0
    TAKE_OPTIONAL_LOCK();
5483
5484
0
    OGRErr eErr = OGRERR_NONE;
5485
0
    d->refreshProjObj();
5486
0
    d->demoteFromBoundCRS();
5487
0
    if (d->m_pjType == PJ_TYPE_UNKNOWN)
5488
0
    {
5489
0
        d->setPjCRS(proj_create_geocentric_crs(
5490
0
            d->getPROJContext(), pszName, "World Geodetic System 1984",
5491
0
            "WGS 84", SRS_WGS84_SEMIMAJOR, SRS_WGS84_INVFLATTENING,
5492
0
            SRS_PM_GREENWICH, 0.0, SRS_UA_DEGREE, CPLAtof(SRS_UA_DEGREE_CONV),
5493
0
            "Metre", 1.0));
5494
0
    }
5495
0
    else if (d->m_pjType == PJ_TYPE_GEOCENTRIC_CRS)
5496
0
    {
5497
0
        d->setPjCRS(proj_alter_name(d->getPROJContext(), d->m_pj_crs, pszName));
5498
0
    }
5499
0
    else if (d->m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
5500
0
             d->m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS)
5501
0
    {
5502
0
        auto datum = proj_crs_get_datum(d->getPROJContext(), d->m_pj_crs);
5503
0
#if PROJ_VERSION_MAJOR > 7 ||                                                  \
5504
0
    (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
5505
0
        if (datum == nullptr)
5506
0
        {
5507
0
            datum =
5508
0
                proj_crs_get_datum_ensemble(d->getPROJContext(), d->m_pj_crs);
5509
0
        }
5510
0
#endif
5511
0
        if (datum == nullptr)
5512
0
        {
5513
0
            d->undoDemoteFromBoundCRS();
5514
0
            return OGRERR_FAILURE;
5515
0
        }
5516
5517
0
        auto pj_crs = proj_create_geocentric_crs_from_datum(
5518
0
            d->getPROJContext(), proj_get_name(d->m_pj_crs), datum, nullptr,
5519
0
            0.0);
5520
0
        d->setPjCRS(pj_crs);
5521
5522
0
        proj_destroy(datum);
5523
0
    }
5524
0
    else
5525
0
    {
5526
0
        CPLDebug("OGR",
5527
0
                 "OGRSpatialReference::SetGeocCS(%s) failed.  "
5528
0
                 "It appears an incompatible object already exists.",
5529
0
                 pszName);
5530
0
        eErr = OGRERR_FAILURE;
5531
0
    }
5532
0
    d->undoDemoteFromBoundCRS();
5533
5534
0
    return eErr;
5535
0
}
5536
5537
/************************************************************************/
5538
/*                            OSRSetGeocCS()                            */
5539
/************************************************************************/
5540
5541
/**
5542
 * \brief Set the user visible PROJCS name.
5543
 *
5544
 * This function is the same as OGRSpatialReference::SetGeocCS()
5545
 *
5546
 * @since OGR 1.9.0
5547
 */
5548
OGRErr OSRSetGeocCS(OGRSpatialReferenceH hSRS, const char *pszName)
5549
5550
0
{
5551
0
    VALIDATE_POINTER1(hSRS, "OSRSetGeocCS", OGRERR_FAILURE);
5552
5553
0
    return ToPointer(hSRS)->SetGeocCS(pszName);
5554
0
}
5555
5556
/************************************************************************/
5557
/*                             SetVertCS()                              */
5558
/************************************************************************/
5559
5560
/**
5561
 * \brief Set the user visible VERT_CS name.
5562
 *
5563
 * This method is the same as the C function OSRSetVertCS().
5564
5565
 * This method will ensure a VERT_CS node is created if needed.  If the
5566
 * existing coordinate system is GEOGCS or PROJCS rooted, then it will be
5567
 * turned into a COMPD_CS.
5568
 *
5569
 * @param pszVertCSName the user visible name of the vertical coordinate
5570
 * system. Not used as a key.
5571
 *
5572
 * @param pszVertDatumName the user visible name of the vertical datum.  It
5573
 * is helpful if this matches the EPSG name.
5574
 *
5575
 * @param nVertDatumType the OGC vertical datum type. Ignored
5576
 *
5577
 * @return OGRERR_NONE on success.
5578
 *
5579
 * @since OGR 1.9.0
5580
 */
5581
5582
OGRErr OGRSpatialReference::SetVertCS(const char *pszVertCSName,
5583
                                      const char *pszVertDatumName,
5584
                                      int nVertDatumType)
5585
5586
0
{
5587
0
    TAKE_OPTIONAL_LOCK();
5588
5589
0
    CPL_IGNORE_RET_VAL(nVertDatumType);
5590
5591
0
    d->refreshProjObj();
5592
5593
0
    auto vertCRS = proj_create_vertical_crs(d->getPROJContext(), pszVertCSName,
5594
0
                                            pszVertDatumName, nullptr, 0.0);
5595
5596
    /* -------------------------------------------------------------------- */
5597
    /*      Handle the case where we want to make a compound coordinate     */
5598
    /*      system.                                                         */
5599
    /* -------------------------------------------------------------------- */
5600
0
    if (IsProjected() || IsGeographic())
5601
0
    {
5602
0
        auto compoundCRS = proj_create_compound_crs(
5603
0
            d->getPROJContext(), nullptr, d->m_pj_crs, vertCRS);
5604
0
        proj_destroy(vertCRS);
5605
0
        d->setPjCRS(compoundCRS);
5606
0
    }
5607
0
    else
5608
0
    {
5609
0
        d->setPjCRS(vertCRS);
5610
0
    }
5611
0
    return OGRERR_NONE;
5612
0
}
5613
5614
/************************************************************************/
5615
/*                            OSRSetVertCS()                            */
5616
/************************************************************************/
5617
5618
/**
5619
 * \brief Setup the vertical coordinate system.
5620
 *
5621
 * This function is the same as OGRSpatialReference::SetVertCS()
5622
 *
5623
 * @since OGR 1.9.0
5624
 */
5625
OGRErr OSRSetVertCS(OGRSpatialReferenceH hSRS, const char *pszVertCSName,
5626
                    const char *pszVertDatumName, int nVertDatumType)
5627
5628
0
{
5629
0
    VALIDATE_POINTER1(hSRS, "OSRSetVertCS", OGRERR_FAILURE);
5630
5631
0
    return ToPointer(hSRS)->SetVertCS(pszVertCSName, pszVertDatumName,
5632
0
                                      nVertDatumType);
5633
0
}
5634
5635
/************************************************************************/
5636
/*                           SetCompoundCS()                            */
5637
/************************************************************************/
5638
5639
/**
5640
 * \brief Setup a compound coordinate system.
5641
 *
5642
 * This method is the same as the C function OSRSetCompoundCS().
5643
5644
 * This method is replace the current SRS with a COMPD_CS coordinate system
5645
 * consisting of the passed in horizontal and vertical coordinate systems.
5646
 *
5647
 * @param pszName the name of the compound coordinate system.
5648
 *
5649
 * @param poHorizSRS the horizontal SRS (PROJCS or GEOGCS).
5650
 *
5651
 * @param poVertSRS the vertical SRS (VERT_CS).
5652
 *
5653
 * @return OGRERR_NONE on success.
5654
 */
5655
5656
OGRErr OGRSpatialReference::SetCompoundCS(const char *pszName,
5657
                                          const OGRSpatialReference *poHorizSRS,
5658
                                          const OGRSpatialReference *poVertSRS)
5659
5660
0
{
5661
0
    TAKE_OPTIONAL_LOCK();
5662
5663
    /* -------------------------------------------------------------------- */
5664
    /*      Verify these are legal horizontal and vertical coordinate       */
5665
    /*      systems.                                                        */
5666
    /* -------------------------------------------------------------------- */
5667
0
    if (!poVertSRS->IsVertical())
5668
0
    {
5669
0
        CPLError(CE_Failure, CPLE_AppDefined,
5670
0
                 "SetCompoundCS() fails, vertical component is not VERT_CS.");
5671
0
        return OGRERR_FAILURE;
5672
0
    }
5673
0
    if (!poHorizSRS->IsProjected() && !poHorizSRS->IsGeographic())
5674
0
    {
5675
0
        CPLError(CE_Failure, CPLE_AppDefined,
5676
0
                 "SetCompoundCS() fails, horizontal component is not PROJCS or "
5677
0
                 "GEOGCS.");
5678
0
        return OGRERR_FAILURE;
5679
0
    }
5680
5681
    /* -------------------------------------------------------------------- */
5682
    /*      Replace with compound srs.                                      */
5683
    /* -------------------------------------------------------------------- */
5684
0
    Clear();
5685
5686
0
    auto compoundCRS = proj_create_compound_crs(d->getPROJContext(), pszName,
5687
0
                                                poHorizSRS->d->m_pj_crs,
5688
0
                                                poVertSRS->d->m_pj_crs);
5689
0
    d->setPjCRS(compoundCRS);
5690
5691
0
    return OGRERR_NONE;
5692
0
}
5693
5694
/************************************************************************/
5695
/*                          OSRSetCompoundCS()                          */
5696
/************************************************************************/
5697
5698
/**
5699
 * \brief Setup a compound coordinate system.
5700
 *
5701
 * This function is the same as OGRSpatialReference::SetCompoundCS()
5702
 */
5703
OGRErr OSRSetCompoundCS(OGRSpatialReferenceH hSRS, const char *pszName,
5704
                        OGRSpatialReferenceH hHorizSRS,
5705
                        OGRSpatialReferenceH hVertSRS)
5706
5707
0
{
5708
0
    VALIDATE_POINTER1(hSRS, "OSRSetCompoundCS", OGRERR_FAILURE);
5709
0
    VALIDATE_POINTER1(hHorizSRS, "OSRSetCompoundCS", OGRERR_FAILURE);
5710
0
    VALIDATE_POINTER1(hVertSRS, "OSRSetCompoundCS", OGRERR_FAILURE);
5711
5712
0
    return ToPointer(hSRS)->SetCompoundCS(pszName, ToPointer(hHorizSRS),
5713
0
                                          ToPointer(hVertSRS));
5714
0
}
5715
5716
/************************************************************************/
5717
/*                             SetProjCS()                              */
5718
/************************************************************************/
5719
5720
/**
5721
 * \brief Set the user visible PROJCS name.
5722
 *
5723
 * This method is the same as the C function OSRSetProjCS().
5724
 *
5725
 * This method will ensure a PROJCS node is created as the root,
5726
 * and set the provided name on it.  If used on a GEOGCS coordinate system,
5727
 * the GEOGCS node will be demoted to be a child of the new PROJCS root.
5728
 *
5729
 * @param pszName the user visible name to assign.  Not used as a key.
5730
 *
5731
 * @return OGRERR_NONE on success.
5732
 */
5733
5734
OGRErr OGRSpatialReference::SetProjCS(const char *pszName)
5735
5736
0
{
5737
0
    TAKE_OPTIONAL_LOCK();
5738
5739
0
    d->refreshProjObj();
5740
0
    d->demoteFromBoundCRS();
5741
0
    if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
5742
0
    {
5743
0
        d->setPjCRS(proj_alter_name(d->getPROJContext(), d->m_pj_crs, pszName));
5744
0
    }
5745
0
    else
5746
0
    {
5747
0
        auto dummyConv = proj_create_conversion(d->getPROJContext(), nullptr,
5748
0
                                                nullptr, nullptr, nullptr,
5749
0
                                                nullptr, nullptr, 0, nullptr);
5750
0
        auto cs = proj_create_cartesian_2D_cs(
5751
0
            d->getPROJContext(), PJ_CART2D_EASTING_NORTHING, nullptr, 0);
5752
5753
0
        auto projCRS = proj_create_projected_crs(
5754
0
            d->getPROJContext(), pszName, d->getGeodBaseCRS(), dummyConv, cs);
5755
0
        proj_destroy(dummyConv);
5756
0
        proj_destroy(cs);
5757
5758
0
        d->setPjCRS(projCRS);
5759
0
    }
5760
0
    d->undoDemoteFromBoundCRS();
5761
0
    return OGRERR_NONE;
5762
0
}
5763
5764
/************************************************************************/
5765
/*                            OSRSetProjCS()                            */
5766
/************************************************************************/
5767
5768
/**
5769
 * \brief Set the user visible PROJCS name.
5770
 *
5771
 * This function is the same as OGRSpatialReference::SetProjCS()
5772
 */
5773
OGRErr OSRSetProjCS(OGRSpatialReferenceH hSRS, const char *pszName)
5774
5775
0
{
5776
0
    VALIDATE_POINTER1(hSRS, "OSRSetProjCS", OGRERR_FAILURE);
5777
5778
0
    return ToPointer(hSRS)->SetProjCS(pszName);
5779
0
}
5780
5781
/************************************************************************/
5782
/*                           SetProjection()                            */
5783
/************************************************************************/
5784
5785
/**
5786
 * \brief Set a projection name.
5787
 *
5788
 * This method is the same as the C function OSRSetProjection().
5789
 *
5790
 * @param pszProjection the projection name, which should be selected from
5791
 * the macros in ogr_srs_api.h, such as SRS_PT_TRANSVERSE_MERCATOR.
5792
 *
5793
 * @return OGRERR_NONE on success.
5794
 */
5795
5796
OGRErr OGRSpatialReference::SetProjection(const char *pszProjection)
5797
5798
0
{
5799
0
    TAKE_OPTIONAL_LOCK();
5800
5801
0
    OGR_SRSNode *poGeogCS = nullptr;
5802
5803
0
    if (GetRoot() != nullptr && EQUAL(d->m_poRoot->GetValue(), "GEOGCS"))
5804
0
    {
5805
0
        poGeogCS = d->m_poRoot;
5806
0
        d->m_poRoot = nullptr;
5807
0
    }
5808
5809
0
    if (!GetAttrNode("PROJCS"))
5810
0
    {
5811
0
        SetNode("PROJCS", "unnamed");
5812
0
    }
5813
5814
0
    const OGRErr eErr = SetNode("PROJCS|PROJECTION", pszProjection);
5815
0
    if (eErr != OGRERR_NONE)
5816
0
        return eErr;
5817
5818
0
    if (poGeogCS != nullptr)
5819
0
        d->m_poRoot->InsertChild(poGeogCS, 1);
5820
5821
0
    return OGRERR_NONE;
5822
0
}
5823
5824
/************************************************************************/
5825
/*                            OSRSetProjection()                        */
5826
/************************************************************************/
5827
5828
/**
5829
 * \brief Set a projection name.
5830
 *
5831
 * This function is the same as OGRSpatialReference::SetProjection()
5832
 */
5833
OGRErr OSRSetProjection(OGRSpatialReferenceH hSRS, const char *pszProjection)
5834
5835
0
{
5836
0
    VALIDATE_POINTER1(hSRS, "OSRSetProjection", OGRERR_FAILURE);
5837
5838
0
    return ToPointer(hSRS)->SetProjection(pszProjection);
5839
0
}
5840
5841
/************************************************************************/
5842
/*                      GetWKT2ProjectionMethod()                       */
5843
/************************************************************************/
5844
5845
/**
5846
 * \brief Returns info on the projection method, based on WKT2 naming
5847
 * conventions.
5848
 *
5849
 * The returned strings are short lived and should be considered to be
5850
 * invalidated by any further call to the GDAL API.
5851
 *
5852
 * @param[out] ppszMethodName Pointer to a string that will receive the
5853
 * projection method name.
5854
 * @param[out] ppszMethodAuthName null pointer, or pointer to a string that will
5855
 * receive the name of the authority that defines the projection method.
5856
 * *ppszMethodAuthName may be nullptr if the projection method is not linked to
5857
 * an authority.
5858
 * @param[out] ppszMethodCode null pointer, or pointer to a string that will
5859
 * receive the code that defines the projection method.
5860
 * *ppszMethodCode may be nullptr if the projection method is not linked to
5861
 * an authority.
5862
 *
5863
 * @return OGRERR_NONE on success.
5864
 */
5865
OGRErr
5866
OGRSpatialReference::GetWKT2ProjectionMethod(const char **ppszMethodName,
5867
                                             const char **ppszMethodAuthName,
5868
                                             const char **ppszMethodCode) const
5869
0
{
5870
0
    TAKE_OPTIONAL_LOCK();
5871
5872
0
    auto conv = proj_crs_get_coordoperation(d->getPROJContext(), d->m_pj_crs);
5873
0
    if (!conv)
5874
0
        return OGRERR_FAILURE;
5875
0
    const char *pszTmpMethodName = "";
5876
0
    const char *pszTmpMethodAuthName = "";
5877
0
    const char *pszTmpMethodCode = "";
5878
0
    int ret = proj_coordoperation_get_method_info(
5879
0
        d->getPROJContext(), conv, &pszTmpMethodName, &pszTmpMethodAuthName,
5880
0
        &pszTmpMethodCode);
5881
    // "Internalize" temporary strings returned by PROJ
5882
0
    CPLAssert(pszTmpMethodName);
5883
0
    if (ppszMethodName)
5884
0
        *ppszMethodName = CPLSPrintf("%s", pszTmpMethodName);
5885
0
    if (ppszMethodAuthName)
5886
0
        *ppszMethodAuthName = pszTmpMethodAuthName
5887
0
                                  ? CPLSPrintf("%s", pszTmpMethodAuthName)
5888
0
                                  : nullptr;
5889
0
    if (ppszMethodCode)
5890
0
        *ppszMethodCode =
5891
0
            pszTmpMethodCode ? CPLSPrintf("%s", pszTmpMethodCode) : nullptr;
5892
0
    proj_destroy(conv);
5893
0
    return ret ? OGRERR_NONE : OGRERR_FAILURE;
5894
0
}
5895
5896
/************************************************************************/
5897
/*                            SetProjParm()                             */
5898
/************************************************************************/
5899
5900
/**
5901
 * \brief Set a projection parameter value.
5902
 *
5903
 * Adds a new PARAMETER under the PROJCS with the indicated name and value.
5904
 *
5905
 * This method is the same as the C function OSRSetProjParm().
5906
 *
5907
 * Please check https://gdal.org/proj_list pages for
5908
 * legal parameter names for specific projections.
5909
 *
5910
 *
5911
 * @param pszParamName the parameter name, which should be selected from
5912
 * the macros in ogr_srs_api.h, such as SRS_PP_CENTRAL_MERIDIAN.
5913
 *
5914
 * @param dfValue value to assign.
5915
 *
5916
 * @return OGRERR_NONE on success.
5917
 */
5918
5919
OGRErr OGRSpatialReference::SetProjParm(const char *pszParamName,
5920
                                        double dfValue)
5921
5922
0
{
5923
0
    TAKE_OPTIONAL_LOCK();
5924
5925
0
    OGR_SRSNode *poPROJCS = GetAttrNode("PROJCS");
5926
5927
0
    if (poPROJCS == nullptr)
5928
0
        return OGRERR_FAILURE;
5929
5930
0
    char szValue[64] = {'\0'};
5931
0
    OGRsnPrintDouble(szValue, sizeof(szValue), dfValue);
5932
5933
    /* -------------------------------------------------------------------- */
5934
    /*      Try to find existing parameter with this name.                  */
5935
    /* -------------------------------------------------------------------- */
5936
0
    for (int iChild = 0; iChild < poPROJCS->GetChildCount(); iChild++)
5937
0
    {
5938
0
        OGR_SRSNode *poParam = poPROJCS->GetChild(iChild);
5939
5940
0
        if (EQUAL(poParam->GetValue(), "PARAMETER") &&
5941
0
            poParam->GetChildCount() == 2 &&
5942
0
            EQUAL(poParam->GetChild(0)->GetValue(), pszParamName))
5943
0
        {
5944
0
            poParam->GetChild(1)->SetValue(szValue);
5945
0
            return OGRERR_NONE;
5946
0
        }
5947
0
    }
5948
5949
    /* -------------------------------------------------------------------- */
5950
    /*      Otherwise create a new parameter and append.                    */
5951
    /* -------------------------------------------------------------------- */
5952
0
    OGR_SRSNode *poParam = new OGR_SRSNode("PARAMETER");
5953
0
    poParam->AddChild(new OGR_SRSNode(pszParamName));
5954
0
    poParam->AddChild(new OGR_SRSNode(szValue));
5955
5956
0
    poPROJCS->AddChild(poParam);
5957
5958
0
    return OGRERR_NONE;
5959
0
}
5960
5961
/************************************************************************/
5962
/*                           OSRSetProjParm()                           */
5963
/************************************************************************/
5964
5965
/**
5966
 * \brief Set a projection parameter value.
5967
 *
5968
 * This function is the same as OGRSpatialReference::SetProjParm()
5969
 */
5970
OGRErr OSRSetProjParm(OGRSpatialReferenceH hSRS, const char *pszParamName,
5971
                      double dfValue)
5972
5973
0
{
5974
0
    VALIDATE_POINTER1(hSRS, "OSRSetProjParm", OGRERR_FAILURE);
5975
5976
0
    return ToPointer(hSRS)->SetProjParm(pszParamName, dfValue);
5977
0
}
5978
5979
/************************************************************************/
5980
/*                            FindProjParm()                            */
5981
/************************************************************************/
5982
5983
/**
5984
 * \brief Return the child index of the named projection parameter on
5985
 * its parent PROJCS node.
5986
 *
5987
 * @param pszParameter projection parameter to look for
5988
 * @param poPROJCS projection CS node to look in. If NULL is passed,
5989
 *        the PROJCS node of the SpatialReference object will be searched.
5990
 *
5991
 * @return the child index of the named projection parameter. -1 on failure
5992
 */
5993
int OGRSpatialReference::FindProjParm(const char *pszParameter,
5994
                                      const OGR_SRSNode *poPROJCS) const
5995
5996
0
{
5997
0
    TAKE_OPTIONAL_LOCK();
5998
5999
0
    if (poPROJCS == nullptr)
6000
0
        poPROJCS = GetAttrNode("PROJCS");
6001
6002
0
    if (poPROJCS == nullptr)
6003
0
        return -1;
6004
6005
    /* -------------------------------------------------------------------- */
6006
    /*      Search for requested parameter.                                 */
6007
    /* -------------------------------------------------------------------- */
6008
0
    bool bIsWKT2 = false;
6009
0
    for (int iChild = 0; iChild < poPROJCS->GetChildCount(); iChild++)
6010
0
    {
6011
0
        const OGR_SRSNode *poParameter = poPROJCS->GetChild(iChild);
6012
6013
0
        if (poParameter->GetChildCount() >= 2)
6014
0
        {
6015
0
            const char *pszValue = poParameter->GetValue();
6016
0
            if (EQUAL(pszValue, "PARAMETER") &&
6017
0
                EQUAL(poPROJCS->GetChild(iChild)->GetChild(0)->GetValue(),
6018
0
                      pszParameter))
6019
0
            {
6020
0
                return iChild;
6021
0
            }
6022
0
            else if (EQUAL(pszValue, "METHOD"))
6023
0
            {
6024
0
                bIsWKT2 = true;
6025
0
            }
6026
0
        }
6027
0
    }
6028
6029
    /* -------------------------------------------------------------------- */
6030
    /*      Try similar names, for selected parameters.                     */
6031
    /* -------------------------------------------------------------------- */
6032
0
    if (EQUAL(pszParameter, SRS_PP_LATITUDE_OF_ORIGIN))
6033
0
    {
6034
0
        if (bIsWKT2)
6035
0
        {
6036
0
            int iChild = FindProjParm(
6037
0
                EPSG_NAME_PARAMETER_LATITUDE_OF_NATURAL_ORIGIN, poPROJCS);
6038
0
            if (iChild == -1)
6039
0
                iChild = FindProjParm(
6040
0
                    EPSG_NAME_PARAMETER_LATITUDE_PROJECTION_CENTRE, poPROJCS);
6041
0
            return iChild;
6042
0
        }
6043
0
        return FindProjParm(SRS_PP_LATITUDE_OF_CENTER, poPROJCS);
6044
0
    }
6045
6046
0
    if (EQUAL(pszParameter, SRS_PP_CENTRAL_MERIDIAN))
6047
0
    {
6048
0
        if (bIsWKT2)
6049
0
        {
6050
0
            int iChild = FindProjParm(
6051
0
                EPSG_NAME_PARAMETER_LONGITUDE_OF_NATURAL_ORIGIN, poPROJCS);
6052
0
            if (iChild == -1)
6053
0
                iChild = FindProjParm(
6054
0
                    EPSG_NAME_PARAMETER_LONGITUDE_PROJECTION_CENTRE, poPROJCS);
6055
0
            return iChild;
6056
0
        }
6057
0
        int iChild = FindProjParm(SRS_PP_LONGITUDE_OF_CENTER, poPROJCS);
6058
0
        if (iChild == -1)
6059
0
            iChild = FindProjParm(SRS_PP_LONGITUDE_OF_ORIGIN, poPROJCS);
6060
0
        return iChild;
6061
0
    }
6062
6063
0
    return -1;
6064
0
}
6065
6066
/************************************************************************/
6067
/*                            GetProjParm()                             */
6068
/************************************************************************/
6069
6070
/**
6071
 * \brief Fetch a projection parameter value.
6072
 *
6073
 * NOTE: This code should be modified to translate non degree angles into
6074
 * degrees based on the GEOGCS unit.  This has not yet been done.
6075
 *
6076
 * This method is the same as the C function OSRGetProjParm().
6077
 *
6078
 * @param pszName the name of the parameter to fetch, from the set of
6079
 * SRS_PP codes in ogr_srs_api.h.
6080
 *
6081
 * @param dfDefaultValue the value to return if this parameter doesn't exist.
6082
 *
6083
 * @param pnErr place to put error code on failure.  Ignored if NULL.
6084
 *
6085
 * @return value of parameter.
6086
 */
6087
6088
double OGRSpatialReference::GetProjParm(const char *pszName,
6089
                                        double dfDefaultValue,
6090
                                        OGRErr *pnErr) const
6091
6092
0
{
6093
0
    TAKE_OPTIONAL_LOCK();
6094
6095
0
    d->refreshProjObj();
6096
0
    GetRoot();  // force update of d->m_bNodesWKT2
6097
6098
0
    if (pnErr != nullptr)
6099
0
        *pnErr = OGRERR_NONE;
6100
6101
    /* -------------------------------------------------------------------- */
6102
    /*      Find the desired parameter.                                     */
6103
    /* -------------------------------------------------------------------- */
6104
0
    const OGR_SRSNode *poPROJCS =
6105
0
        GetAttrNode(d->m_bNodesWKT2 ? "CONVERSION" : "PROJCS");
6106
0
    if (poPROJCS == nullptr)
6107
0
    {
6108
0
        if (pnErr != nullptr)
6109
0
            *pnErr = OGRERR_FAILURE;
6110
0
        return dfDefaultValue;
6111
0
    }
6112
6113
0
    const int iChild = FindProjParm(pszName, poPROJCS);
6114
0
    if (iChild == -1)
6115
0
    {
6116
0
        if (IsProjected() && GetAxesCount() == 3)
6117
0
        {
6118
0
            OGRSpatialReference *poSRSTmp = Clone();
6119
0
            poSRSTmp->DemoteTo2D(nullptr);
6120
0
            const double dfRet =
6121
0
                poSRSTmp->GetProjParm(pszName, dfDefaultValue, pnErr);
6122
0
            delete poSRSTmp;
6123
0
            return dfRet;
6124
0
        }
6125
6126
0
        if (pnErr != nullptr)
6127
0
            *pnErr = OGRERR_FAILURE;
6128
0
        return dfDefaultValue;
6129
0
    }
6130
6131
0
    const OGR_SRSNode *poParameter = poPROJCS->GetChild(iChild);
6132
0
    return CPLAtof(poParameter->GetChild(1)->GetValue());
6133
0
}
6134
6135
/************************************************************************/
6136
/*                           OSRGetProjParm()                           */
6137
/************************************************************************/
6138
6139
/**
6140
 * \brief Fetch a projection parameter value.
6141
 *
6142
 * This function is the same as OGRSpatialReference::GetProjParm()
6143
 */
6144
double OSRGetProjParm(OGRSpatialReferenceH hSRS, const char *pszName,
6145
                      double dfDefaultValue, OGRErr *pnErr)
6146
6147
0
{
6148
0
    VALIDATE_POINTER1(hSRS, "OSRGetProjParm", 0);
6149
6150
0
    return ToPointer(hSRS)->GetProjParm(pszName, dfDefaultValue, pnErr);
6151
0
}
6152
6153
/************************************************************************/
6154
/*                          GetNormProjParm()                           */
6155
/************************************************************************/
6156
6157
/**
6158
 * \brief Fetch a normalized projection parameter value.
6159
 *
6160
 * This method is the same as GetProjParm() except that the value of
6161
 * the parameter is "normalized" into degrees or meters depending on
6162
 * whether it is linear or angular.
6163
 *
6164
 * This method is the same as the C function OSRGetNormProjParm().
6165
 *
6166
 * @param pszName the name of the parameter to fetch, from the set of
6167
 * SRS_PP codes in ogr_srs_api.h.
6168
 *
6169
 * @param dfDefaultValue the value to return if this parameter doesn't exist.
6170
 *
6171
 * @param pnErr place to put error code on failure.  Ignored if NULL.
6172
 *
6173
 * @return value of parameter.
6174
 */
6175
6176
double OGRSpatialReference::GetNormProjParm(const char *pszName,
6177
                                            double dfDefaultValue,
6178
                                            OGRErr *pnErr) const
6179
6180
0
{
6181
0
    TAKE_OPTIONAL_LOCK();
6182
6183
0
    GetNormInfo();
6184
6185
0
    OGRErr nError = OGRERR_NONE;
6186
0
    double dfRawResult = GetProjParm(pszName, dfDefaultValue, &nError);
6187
0
    if (pnErr != nullptr)
6188
0
        *pnErr = nError;
6189
6190
    // If we got the default just return it unadjusted.
6191
0
    if (nError != OGRERR_NONE)
6192
0
        return dfRawResult;
6193
6194
0
    if (d->dfToDegrees != 1.0 && IsAngularParameter(pszName))
6195
0
        dfRawResult *= d->dfToDegrees;
6196
6197
0
    if (d->dfToMeter != 1.0 && IsLinearParameter(pszName))
6198
0
        return dfRawResult * d->dfToMeter;
6199
6200
0
    return dfRawResult;
6201
0
}
6202
6203
/************************************************************************/
6204
/*                         OSRGetNormProjParm()                         */
6205
/************************************************************************/
6206
6207
/**
6208
 * \brief This function is the same as OGRSpatialReference::
6209
 *
6210
 * This function is the same as OGRSpatialReference::GetNormProjParm()
6211
 */
6212
double OSRGetNormProjParm(OGRSpatialReferenceH hSRS, const char *pszName,
6213
                          double dfDefaultValue, OGRErr *pnErr)
6214
6215
0
{
6216
0
    VALIDATE_POINTER1(hSRS, "OSRGetNormProjParm", 0);
6217
6218
0
    return ToPointer(hSRS)->GetNormProjParm(pszName, dfDefaultValue, pnErr);
6219
0
}
6220
6221
/************************************************************************/
6222
/*                          SetNormProjParm()                           */
6223
/************************************************************************/
6224
6225
/**
6226
 * \brief Set a projection parameter with a normalized value.
6227
 *
6228
 * This method is the same as SetProjParm() except that the value of
6229
 * the parameter passed in is assumed to be in "normalized" form (decimal
6230
 * degrees for angular values, meters for linear values.  The values are
6231
 * converted in a form suitable for the GEOGCS and linear units in effect.
6232
 *
6233
 * This method is the same as the C function OSRSetNormProjParm().
6234
 *
6235
 * @param pszName the parameter name, which should be selected from
6236
 * the macros in ogr_srs_api.h, such as SRS_PP_CENTRAL_MERIDIAN.
6237
 *
6238
 * @param dfValue value to assign.
6239
 *
6240
 * @return OGRERR_NONE on success.
6241
 */
6242
6243
OGRErr OGRSpatialReference::SetNormProjParm(const char *pszName, double dfValue)
6244
6245
0
{
6246
0
    TAKE_OPTIONAL_LOCK();
6247
6248
0
    GetNormInfo();
6249
6250
0
    if (d->dfToDegrees != 0.0 &&
6251
0
        (d->dfToDegrees != 1.0 || d->dfFromGreenwich != 0.0) &&
6252
0
        IsAngularParameter(pszName))
6253
0
    {
6254
0
        dfValue /= d->dfToDegrees;
6255
0
    }
6256
0
    else if (d->dfToMeter != 1.0 && d->dfToMeter != 0.0 &&
6257
0
             IsLinearParameter(pszName))
6258
0
        dfValue /= d->dfToMeter;
6259
6260
0
    return SetProjParm(pszName, dfValue);
6261
0
}
6262
6263
/************************************************************************/
6264
/*                         OSRSetNormProjParm()                         */
6265
/************************************************************************/
6266
6267
/**
6268
 * \brief Set a projection parameter with a normalized value.
6269
 *
6270
 * This function is the same as OGRSpatialReference::SetNormProjParm()
6271
 */
6272
OGRErr OSRSetNormProjParm(OGRSpatialReferenceH hSRS, const char *pszParamName,
6273
                          double dfValue)
6274
6275
0
{
6276
0
    VALIDATE_POINTER1(hSRS, "OSRSetNormProjParm", OGRERR_FAILURE);
6277
6278
0
    return ToPointer(hSRS)->SetNormProjParm(pszParamName, dfValue);
6279
0
}
6280
6281
/************************************************************************/
6282
/*                               SetTM()                                */
6283
/************************************************************************/
6284
6285
OGRErr OGRSpatialReference::SetTM(double dfCenterLat, double dfCenterLong,
6286
                                  double dfScale, double dfFalseEasting,
6287
                                  double dfFalseNorthing)
6288
6289
0
{
6290
0
    TAKE_OPTIONAL_LOCK();
6291
6292
0
    return d->replaceConversionAndUnref(
6293
0
        proj_create_conversion_transverse_mercator(
6294
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale,
6295
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6296
0
}
6297
6298
/************************************************************************/
6299
/*                              OSRSetTM()                              */
6300
/************************************************************************/
6301
6302
OGRErr OSRSetTM(OGRSpatialReferenceH hSRS, double dfCenterLat,
6303
                double dfCenterLong, double dfScale, double dfFalseEasting,
6304
                double dfFalseNorthing)
6305
6306
0
{
6307
0
    VALIDATE_POINTER1(hSRS, "OSRSetTM", OGRERR_FAILURE);
6308
6309
0
    return ToPointer(hSRS)->SetTM(dfCenterLat, dfCenterLong, dfScale,
6310
0
                                  dfFalseEasting, dfFalseNorthing);
6311
0
}
6312
6313
/************************************************************************/
6314
/*                            SetTMVariant()                            */
6315
/************************************************************************/
6316
6317
OGRErr OGRSpatialReference::SetTMVariant(const char *pszVariantName,
6318
                                         double dfCenterLat,
6319
                                         double dfCenterLong, double dfScale,
6320
                                         double dfFalseEasting,
6321
                                         double dfFalseNorthing)
6322
6323
0
{
6324
0
    TAKE_OPTIONAL_LOCK();
6325
6326
0
    SetProjection(pszVariantName);
6327
0
    SetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, dfCenterLat);
6328
0
    SetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, dfCenterLong);
6329
0
    SetNormProjParm(SRS_PP_SCALE_FACTOR, dfScale);
6330
0
    SetNormProjParm(SRS_PP_FALSE_EASTING, dfFalseEasting);
6331
0
    SetNormProjParm(SRS_PP_FALSE_NORTHING, dfFalseNorthing);
6332
6333
0
    return OGRERR_NONE;
6334
0
}
6335
6336
/************************************************************************/
6337
/*                          OSRSetTMVariant()                           */
6338
/************************************************************************/
6339
6340
OGRErr OSRSetTMVariant(OGRSpatialReferenceH hSRS, const char *pszVariantName,
6341
                       double dfCenterLat, double dfCenterLong, double dfScale,
6342
                       double dfFalseEasting, double dfFalseNorthing)
6343
6344
0
{
6345
0
    VALIDATE_POINTER1(hSRS, "OSRSetTMVariant", OGRERR_FAILURE);
6346
6347
0
    return ToPointer(hSRS)->SetTMVariant(pszVariantName, dfCenterLat,
6348
0
                                         dfCenterLong, dfScale, dfFalseEasting,
6349
0
                                         dfFalseNorthing);
6350
0
}
6351
6352
/************************************************************************/
6353
/*                              SetTMSO()                               */
6354
/************************************************************************/
6355
6356
OGRErr OGRSpatialReference::SetTMSO(double dfCenterLat, double dfCenterLong,
6357
                                    double dfScale, double dfFalseEasting,
6358
                                    double dfFalseNorthing)
6359
6360
0
{
6361
0
    TAKE_OPTIONAL_LOCK();
6362
6363
0
    auto conv = proj_create_conversion_transverse_mercator_south_oriented(
6364
0
        d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale, dfFalseEasting,
6365
0
        dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6366
6367
0
    const char *pszName = nullptr;
6368
0
    double dfConvFactor = GetTargetLinearUnits(nullptr, &pszName);
6369
0
    CPLString osName = pszName ? pszName : "";
6370
6371
0
    d->refreshProjObj();
6372
6373
0
    d->demoteFromBoundCRS();
6374
6375
0
    auto cs = proj_create_cartesian_2D_cs(
6376
0
        d->getPROJContext(), PJ_CART2D_WESTING_SOUTHING,
6377
0
        !osName.empty() ? osName.c_str() : nullptr, dfConvFactor);
6378
0
    auto projCRS =
6379
0
        proj_create_projected_crs(d->getPROJContext(), d->getProjCRSName(),
6380
0
                                  d->getGeodBaseCRS(), conv, cs);
6381
0
    proj_destroy(conv);
6382
0
    proj_destroy(cs);
6383
6384
0
    d->setPjCRS(projCRS);
6385
6386
0
    d->undoDemoteFromBoundCRS();
6387
6388
0
    return OGRERR_NONE;
6389
0
}
6390
6391
/************************************************************************/
6392
/*                             OSRSetTMSO()                             */
6393
/************************************************************************/
6394
6395
OGRErr OSRSetTMSO(OGRSpatialReferenceH hSRS, double dfCenterLat,
6396
                  double dfCenterLong, double dfScale, double dfFalseEasting,
6397
                  double dfFalseNorthing)
6398
6399
0
{
6400
0
    VALIDATE_POINTER1(hSRS, "OSRSetTMSO", OGRERR_FAILURE);
6401
6402
0
    return ToPointer(hSRS)->SetTMSO(dfCenterLat, dfCenterLong, dfScale,
6403
0
                                    dfFalseEasting, dfFalseNorthing);
6404
0
}
6405
6406
/************************************************************************/
6407
/*                              SetTPED()                               */
6408
/************************************************************************/
6409
6410
OGRErr OGRSpatialReference::SetTPED(double dfLat1, double dfLong1,
6411
                                    double dfLat2, double dfLong2,
6412
                                    double dfFalseEasting,
6413
                                    double dfFalseNorthing)
6414
6415
0
{
6416
0
    TAKE_OPTIONAL_LOCK();
6417
6418
0
    return d->replaceConversionAndUnref(
6419
0
        proj_create_conversion_two_point_equidistant(
6420
0
            d->getPROJContext(), dfLat1, dfLong1, dfLat2, dfLong2,
6421
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6422
0
}
6423
6424
/************************************************************************/
6425
/*                             OSRSetTPED()                             */
6426
/************************************************************************/
6427
6428
OGRErr OSRSetTPED(OGRSpatialReferenceH hSRS, double dfLat1, double dfLong1,
6429
                  double dfLat2, double dfLong2, double dfFalseEasting,
6430
                  double dfFalseNorthing)
6431
6432
0
{
6433
0
    VALIDATE_POINTER1(hSRS, "OSRSetTPED", OGRERR_FAILURE);
6434
6435
0
    return ToPointer(hSRS)->SetTPED(dfLat1, dfLong1, dfLat2, dfLong2,
6436
0
                                    dfFalseEasting, dfFalseNorthing);
6437
0
}
6438
6439
/************************************************************************/
6440
/*                               SetTMG()                               */
6441
/************************************************************************/
6442
6443
OGRErr OGRSpatialReference::SetTMG(double dfCenterLat, double dfCenterLong,
6444
                                   double dfFalseEasting,
6445
                                   double dfFalseNorthing)
6446
6447
0
{
6448
0
    TAKE_OPTIONAL_LOCK();
6449
6450
0
    return d->replaceConversionAndUnref(
6451
0
        proj_create_conversion_tunisia_mapping_grid(
6452
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
6453
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6454
0
}
6455
6456
/************************************************************************/
6457
/*                             OSRSetTMG()                              */
6458
/************************************************************************/
6459
6460
OGRErr OSRSetTMG(OGRSpatialReferenceH hSRS, double dfCenterLat,
6461
                 double dfCenterLong, double dfFalseEasting,
6462
                 double dfFalseNorthing)
6463
6464
0
{
6465
0
    VALIDATE_POINTER1(hSRS, "OSRSetTMG", OGRERR_FAILURE);
6466
6467
0
    return ToPointer(hSRS)->SetTMG(dfCenterLat, dfCenterLong, dfFalseEasting,
6468
0
                                   dfFalseNorthing);
6469
0
}
6470
6471
/************************************************************************/
6472
/*                              SetACEA()                               */
6473
/************************************************************************/
6474
6475
OGRErr OGRSpatialReference::SetACEA(double dfStdP1, double dfStdP2,
6476
                                    double dfCenterLat, double dfCenterLong,
6477
                                    double dfFalseEasting,
6478
                                    double dfFalseNorthing)
6479
6480
0
{
6481
0
    TAKE_OPTIONAL_LOCK();
6482
6483
    // Note different order of parameters. The one in PROJ is conformant with
6484
    // EPSG
6485
0
    return d->replaceConversionAndUnref(
6486
0
        proj_create_conversion_albers_equal_area(
6487
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfStdP1, dfStdP2,
6488
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6489
0
}
6490
6491
/************************************************************************/
6492
/*                             OSRSetACEA()                             */
6493
/************************************************************************/
6494
6495
OGRErr OSRSetACEA(OGRSpatialReferenceH hSRS, double dfStdP1, double dfStdP2,
6496
                  double dfCenterLat, double dfCenterLong,
6497
                  double dfFalseEasting, double dfFalseNorthing)
6498
6499
0
{
6500
0
    VALIDATE_POINTER1(hSRS, "OSRSetACEA", OGRERR_FAILURE);
6501
6502
0
    return ToPointer(hSRS)->SetACEA(dfStdP1, dfStdP2, dfCenterLat, dfCenterLong,
6503
0
                                    dfFalseEasting, dfFalseNorthing);
6504
0
}
6505
6506
/************************************************************************/
6507
/*                               SetAE()                                */
6508
/************************************************************************/
6509
6510
OGRErr OGRSpatialReference::SetAE(double dfCenterLat, double dfCenterLong,
6511
                                  double dfFalseEasting, double dfFalseNorthing)
6512
6513
0
{
6514
0
    TAKE_OPTIONAL_LOCK();
6515
6516
0
    return d->replaceConversionAndUnref(
6517
0
        proj_create_conversion_azimuthal_equidistant(
6518
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
6519
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6520
0
}
6521
6522
/************************************************************************/
6523
/*                              OSRSetAE()                              */
6524
/************************************************************************/
6525
6526
OGRErr OSRSetAE(OGRSpatialReferenceH hSRS, double dfCenterLat,
6527
                double dfCenterLong, double dfFalseEasting,
6528
                double dfFalseNorthing)
6529
6530
0
{
6531
0
    VALIDATE_POINTER1(hSRS, "OSRSetACEA", OGRERR_FAILURE);
6532
6533
0
    return ToPointer(hSRS)->SetAE(dfCenterLat, dfCenterLong, dfFalseEasting,
6534
0
                                  dfFalseNorthing);
6535
0
}
6536
6537
/************************************************************************/
6538
/*                              SetBonne()                              */
6539
/************************************************************************/
6540
6541
OGRErr OGRSpatialReference::SetBonne(double dfStdP1, double dfCentralMeridian,
6542
                                     double dfFalseEasting,
6543
                                     double dfFalseNorthing)
6544
6545
0
{
6546
0
    TAKE_OPTIONAL_LOCK();
6547
6548
0
    return d->replaceConversionAndUnref(proj_create_conversion_bonne(
6549
0
        d->getPROJContext(), dfStdP1, dfCentralMeridian, dfFalseEasting,
6550
0
        dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6551
0
}
6552
6553
/************************************************************************/
6554
/*                            OSRSetBonne()                             */
6555
/************************************************************************/
6556
6557
OGRErr OSRSetBonne(OGRSpatialReferenceH hSRS, double dfStdP1,
6558
                   double dfCentralMeridian, double dfFalseEasting,
6559
                   double dfFalseNorthing)
6560
6561
0
{
6562
0
    VALIDATE_POINTER1(hSRS, "OSRSetBonne", OGRERR_FAILURE);
6563
6564
0
    return ToPointer(hSRS)->SetBonne(dfStdP1, dfCentralMeridian, dfFalseEasting,
6565
0
                                     dfFalseNorthing);
6566
0
}
6567
6568
/************************************************************************/
6569
/*                               SetCEA()                               */
6570
/************************************************************************/
6571
6572
OGRErr OGRSpatialReference::SetCEA(double dfStdP1, double dfCentralMeridian,
6573
                                   double dfFalseEasting,
6574
                                   double dfFalseNorthing)
6575
6576
0
{
6577
0
    TAKE_OPTIONAL_LOCK();
6578
6579
0
    return d->replaceConversionAndUnref(
6580
0
        proj_create_conversion_lambert_cylindrical_equal_area(
6581
0
            d->getPROJContext(), dfStdP1, dfCentralMeridian, dfFalseEasting,
6582
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6583
0
}
6584
6585
/************************************************************************/
6586
/*                             OSRSetCEA()                              */
6587
/************************************************************************/
6588
6589
OGRErr OSRSetCEA(OGRSpatialReferenceH hSRS, double dfStdP1,
6590
                 double dfCentralMeridian, double dfFalseEasting,
6591
                 double dfFalseNorthing)
6592
6593
0
{
6594
0
    VALIDATE_POINTER1(hSRS, "OSRSetCEA", OGRERR_FAILURE);
6595
6596
0
    return ToPointer(hSRS)->SetCEA(dfStdP1, dfCentralMeridian, dfFalseEasting,
6597
0
                                   dfFalseNorthing);
6598
0
}
6599
6600
/************************************************************************/
6601
/*                               SetCS()                                */
6602
/************************************************************************/
6603
6604
OGRErr OGRSpatialReference::SetCS(double dfCenterLat, double dfCenterLong,
6605
                                  double dfFalseEasting, double dfFalseNorthing)
6606
6607
0
{
6608
0
    TAKE_OPTIONAL_LOCK();
6609
6610
0
    return d->replaceConversionAndUnref(proj_create_conversion_cassini_soldner(
6611
0
        d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
6612
0
        dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6613
0
}
6614
6615
/************************************************************************/
6616
/*                              OSRSetCS()                              */
6617
/************************************************************************/
6618
6619
OGRErr OSRSetCS(OGRSpatialReferenceH hSRS, double dfCenterLat,
6620
                double dfCenterLong, double dfFalseEasting,
6621
                double dfFalseNorthing)
6622
6623
0
{
6624
0
    VALIDATE_POINTER1(hSRS, "OSRSetCS", OGRERR_FAILURE);
6625
6626
0
    return ToPointer(hSRS)->SetCS(dfCenterLat, dfCenterLong, dfFalseEasting,
6627
0
                                  dfFalseNorthing);
6628
0
}
6629
6630
/************************************************************************/
6631
/*                               SetEC()                                */
6632
/************************************************************************/
6633
6634
OGRErr OGRSpatialReference::SetEC(double dfStdP1, double dfStdP2,
6635
                                  double dfCenterLat, double dfCenterLong,
6636
                                  double dfFalseEasting, double dfFalseNorthing)
6637
6638
0
{
6639
0
    TAKE_OPTIONAL_LOCK();
6640
6641
    // Note: different order of arguments
6642
0
    return d->replaceConversionAndUnref(
6643
0
        proj_create_conversion_equidistant_conic(
6644
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfStdP1, dfStdP2,
6645
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6646
0
}
6647
6648
/************************************************************************/
6649
/*                              OSRSetEC()                              */
6650
/************************************************************************/
6651
6652
OGRErr OSRSetEC(OGRSpatialReferenceH hSRS, double dfStdP1, double dfStdP2,
6653
                double dfCenterLat, double dfCenterLong, double dfFalseEasting,
6654
                double dfFalseNorthing)
6655
6656
0
{
6657
0
    VALIDATE_POINTER1(hSRS, "OSRSetEC", OGRERR_FAILURE);
6658
6659
0
    return ToPointer(hSRS)->SetEC(dfStdP1, dfStdP2, dfCenterLat, dfCenterLong,
6660
0
                                  dfFalseEasting, dfFalseNorthing);
6661
0
}
6662
6663
/************************************************************************/
6664
/*                             SetEckert()                              */
6665
/************************************************************************/
6666
6667
OGRErr OGRSpatialReference::SetEckert(int nVariation,  // 1-6.
6668
                                      double dfCentralMeridian,
6669
                                      double dfFalseEasting,
6670
                                      double dfFalseNorthing)
6671
6672
0
{
6673
0
    TAKE_OPTIONAL_LOCK();
6674
6675
0
    PJ *conv;
6676
0
    if (nVariation == 1)
6677
0
    {
6678
0
        conv = proj_create_conversion_eckert_i(
6679
0
            d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6680
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6681
0
    }
6682
0
    else if (nVariation == 2)
6683
0
    {
6684
0
        conv = proj_create_conversion_eckert_ii(
6685
0
            d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6686
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6687
0
    }
6688
0
    else if (nVariation == 3)
6689
0
    {
6690
0
        conv = proj_create_conversion_eckert_iii(
6691
0
            d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6692
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6693
0
    }
6694
0
    else if (nVariation == 4)
6695
0
    {
6696
0
        conv = proj_create_conversion_eckert_iv(
6697
0
            d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6698
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6699
0
    }
6700
0
    else if (nVariation == 5)
6701
0
    {
6702
0
        conv = proj_create_conversion_eckert_v(
6703
0
            d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6704
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6705
0
    }
6706
0
    else if (nVariation == 6)
6707
0
    {
6708
0
        conv = proj_create_conversion_eckert_vi(
6709
0
            d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6710
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6711
0
    }
6712
0
    else
6713
0
    {
6714
0
        CPLError(CE_Failure, CPLE_AppDefined,
6715
0
                 "Unsupported Eckert variation (%d).", nVariation);
6716
0
        return OGRERR_UNSUPPORTED_SRS;
6717
0
    }
6718
6719
0
    return d->replaceConversionAndUnref(conv);
6720
0
}
6721
6722
/************************************************************************/
6723
/*                            OSRSetEckert()                            */
6724
/************************************************************************/
6725
6726
OGRErr OSRSetEckert(OGRSpatialReferenceH hSRS, int nVariation,
6727
                    double dfCentralMeridian, double dfFalseEasting,
6728
                    double dfFalseNorthing)
6729
6730
0
{
6731
0
    VALIDATE_POINTER1(hSRS, "OSRSetEckert", OGRERR_FAILURE);
6732
6733
0
    return ToPointer(hSRS)->SetEckert(nVariation, dfCentralMeridian,
6734
0
                                      dfFalseEasting, dfFalseNorthing);
6735
0
}
6736
6737
/************************************************************************/
6738
/*                            SetEckertIV()                             */
6739
/*                                                                      */
6740
/*      Deprecated                                                      */
6741
/************************************************************************/
6742
6743
OGRErr OGRSpatialReference::SetEckertIV(double dfCentralMeridian,
6744
                                        double dfFalseEasting,
6745
                                        double dfFalseNorthing)
6746
6747
0
{
6748
0
    return SetEckert(4, dfCentralMeridian, dfFalseEasting, dfFalseNorthing);
6749
0
}
6750
6751
/************************************************************************/
6752
/*                           OSRSetEckertIV()                           */
6753
/************************************************************************/
6754
6755
OGRErr OSRSetEckertIV(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
6756
                      double dfFalseEasting, double dfFalseNorthing)
6757
6758
0
{
6759
0
    VALIDATE_POINTER1(hSRS, "OSRSetEckertIV", OGRERR_FAILURE);
6760
6761
0
    return ToPointer(hSRS)->SetEckertIV(dfCentralMeridian, dfFalseEasting,
6762
0
                                        dfFalseNorthing);
6763
0
}
6764
6765
/************************************************************************/
6766
/*                            SetEckertVI()                             */
6767
/*                                                                      */
6768
/*      Deprecated                                                      */
6769
/************************************************************************/
6770
6771
OGRErr OGRSpatialReference::SetEckertVI(double dfCentralMeridian,
6772
                                        double dfFalseEasting,
6773
                                        double dfFalseNorthing)
6774
6775
0
{
6776
0
    return SetEckert(6, dfCentralMeridian, dfFalseEasting, dfFalseNorthing);
6777
0
}
6778
6779
/************************************************************************/
6780
/*                           OSRSetEckertVI()                           */
6781
/************************************************************************/
6782
6783
OGRErr OSRSetEckertVI(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
6784
                      double dfFalseEasting, double dfFalseNorthing)
6785
6786
0
{
6787
0
    VALIDATE_POINTER1(hSRS, "OSRSetEckertVI", OGRERR_FAILURE);
6788
6789
0
    return ToPointer(hSRS)->SetEckertVI(dfCentralMeridian, dfFalseEasting,
6790
0
                                        dfFalseNorthing);
6791
0
}
6792
6793
/************************************************************************/
6794
/*                         SetEquirectangular()                         */
6795
/************************************************************************/
6796
6797
OGRErr OGRSpatialReference::SetEquirectangular(double dfCenterLat,
6798
                                               double dfCenterLong,
6799
                                               double dfFalseEasting,
6800
                                               double dfFalseNorthing)
6801
6802
0
{
6803
0
    TAKE_OPTIONAL_LOCK();
6804
6805
0
    if (dfCenterLat == 0.0)
6806
0
    {
6807
0
        return d->replaceConversionAndUnref(
6808
0
            proj_create_conversion_equidistant_cylindrical(
6809
0
                d->getPROJContext(), 0.0, dfCenterLong, dfFalseEasting,
6810
0
                dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6811
0
    }
6812
6813
    // Non-standard extension with non-zero latitude of origin
6814
0
    SetProjection(SRS_PT_EQUIRECTANGULAR);
6815
0
    SetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, dfCenterLat);
6816
0
    SetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, dfCenterLong);
6817
0
    SetNormProjParm(SRS_PP_FALSE_EASTING, dfFalseEasting);
6818
0
    SetNormProjParm(SRS_PP_FALSE_NORTHING, dfFalseNorthing);
6819
6820
0
    return OGRERR_NONE;
6821
0
}
6822
6823
/************************************************************************/
6824
/*                       OSRSetEquirectangular()                        */
6825
/************************************************************************/
6826
6827
OGRErr OSRSetEquirectangular(OGRSpatialReferenceH hSRS, double dfCenterLat,
6828
                             double dfCenterLong, double dfFalseEasting,
6829
                             double dfFalseNorthing)
6830
6831
0
{
6832
0
    VALIDATE_POINTER1(hSRS, "OSRSetEquirectangular", OGRERR_FAILURE);
6833
6834
0
    return ToPointer(hSRS)->SetEquirectangular(dfCenterLat, dfCenterLong,
6835
0
                                               dfFalseEasting, dfFalseNorthing);
6836
0
}
6837
6838
/************************************************************************/
6839
/*                         SetEquirectangular2()                        */
6840
/* Generalized form                                                     */
6841
/************************************************************************/
6842
6843
OGRErr OGRSpatialReference::SetEquirectangular2(double dfCenterLat,
6844
                                                double dfCenterLong,
6845
                                                double dfStdParallel1,
6846
                                                double dfFalseEasting,
6847
                                                double dfFalseNorthing)
6848
6849
0
{
6850
0
    TAKE_OPTIONAL_LOCK();
6851
6852
0
    if (dfCenterLat == 0.0)
6853
0
    {
6854
0
        return d->replaceConversionAndUnref(
6855
0
            proj_create_conversion_equidistant_cylindrical(
6856
0
                d->getPROJContext(), dfStdParallel1, dfCenterLong,
6857
0
                dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6858
0
    }
6859
6860
    // Non-standard extension with non-zero latitude of origin
6861
0
    SetProjection(SRS_PT_EQUIRECTANGULAR);
6862
0
    SetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, dfCenterLat);
6863
0
    SetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, dfCenterLong);
6864
0
    SetNormProjParm(SRS_PP_STANDARD_PARALLEL_1, dfStdParallel1);
6865
0
    SetNormProjParm(SRS_PP_FALSE_EASTING, dfFalseEasting);
6866
0
    SetNormProjParm(SRS_PP_FALSE_NORTHING, dfFalseNorthing);
6867
6868
0
    return OGRERR_NONE;
6869
0
}
6870
6871
/************************************************************************/
6872
/*                       OSRSetEquirectangular2()                       */
6873
/************************************************************************/
6874
6875
OGRErr OSRSetEquirectangular2(OGRSpatialReferenceH hSRS, double dfCenterLat,
6876
                              double dfCenterLong, double dfStdParallel1,
6877
                              double dfFalseEasting, double dfFalseNorthing)
6878
6879
0
{
6880
0
    VALIDATE_POINTER1(hSRS, "OSRSetEquirectangular2", OGRERR_FAILURE);
6881
6882
0
    return ToPointer(hSRS)->SetEquirectangular2(dfCenterLat, dfCenterLong,
6883
0
                                                dfStdParallel1, dfFalseEasting,
6884
0
                                                dfFalseNorthing);
6885
0
}
6886
6887
/************************************************************************/
6888
/*                               SetGS()                                */
6889
/************************************************************************/
6890
6891
OGRErr OGRSpatialReference::SetGS(double dfCentralMeridian,
6892
                                  double dfFalseEasting, double dfFalseNorthing)
6893
6894
0
{
6895
0
    return d->replaceConversionAndUnref(proj_create_conversion_gall(
6896
0
        d->getPROJContext(), dfCentralMeridian, dfFalseEasting, dfFalseNorthing,
6897
0
        nullptr, 0.0, nullptr, 0.0));
6898
0
}
6899
6900
/************************************************************************/
6901
/*                              OSRSetGS()                              */
6902
/************************************************************************/
6903
6904
OGRErr OSRSetGS(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
6905
                double dfFalseEasting, double dfFalseNorthing)
6906
6907
0
{
6908
0
    VALIDATE_POINTER1(hSRS, "OSRSetGS", OGRERR_FAILURE);
6909
6910
0
    return ToPointer(hSRS)->SetGS(dfCentralMeridian, dfFalseEasting,
6911
0
                                  dfFalseNorthing);
6912
0
}
6913
6914
/************************************************************************/
6915
/*                               SetGH()                                */
6916
/************************************************************************/
6917
6918
OGRErr OGRSpatialReference::SetGH(double dfCentralMeridian,
6919
                                  double dfFalseEasting, double dfFalseNorthing)
6920
6921
0
{
6922
0
    TAKE_OPTIONAL_LOCK();
6923
6924
0
    return d->replaceConversionAndUnref(proj_create_conversion_goode_homolosine(
6925
0
        d->getPROJContext(), dfCentralMeridian, dfFalseEasting, dfFalseNorthing,
6926
0
        nullptr, 0.0, nullptr, 0.0));
6927
0
}
6928
6929
/************************************************************************/
6930
/*                              OSRSetGH()                              */
6931
/************************************************************************/
6932
6933
OGRErr OSRSetGH(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
6934
                double dfFalseEasting, double dfFalseNorthing)
6935
6936
0
{
6937
0
    VALIDATE_POINTER1(hSRS, "OSRSetGH", OGRERR_FAILURE);
6938
6939
0
    return ToPointer(hSRS)->SetGH(dfCentralMeridian, dfFalseEasting,
6940
0
                                  dfFalseNorthing);
6941
0
}
6942
6943
/************************************************************************/
6944
/*                              SetIGH()                                */
6945
/************************************************************************/
6946
6947
OGRErr OGRSpatialReference::SetIGH()
6948
6949
0
{
6950
0
    TAKE_OPTIONAL_LOCK();
6951
6952
0
    return d->replaceConversionAndUnref(
6953
0
        proj_create_conversion_interrupted_goode_homolosine(
6954
0
            d->getPROJContext(), 0.0, 0.0, 0.0, nullptr, 0.0, nullptr, 0.0));
6955
0
}
6956
6957
/************************************************************************/
6958
/*                              OSRSetIGH()                             */
6959
/************************************************************************/
6960
6961
OGRErr OSRSetIGH(OGRSpatialReferenceH hSRS)
6962
6963
0
{
6964
0
    VALIDATE_POINTER1(hSRS, "OSRSetIGH", OGRERR_FAILURE);
6965
6966
0
    return ToPointer(hSRS)->SetIGH();
6967
0
}
6968
6969
/************************************************************************/
6970
/*                              SetGEOS()                               */
6971
/************************************************************************/
6972
6973
OGRErr OGRSpatialReference::SetGEOS(double dfCentralMeridian,
6974
                                    double dfSatelliteHeight,
6975
                                    double dfFalseEasting,
6976
                                    double dfFalseNorthing)
6977
6978
0
{
6979
0
    TAKE_OPTIONAL_LOCK();
6980
6981
0
    return d->replaceConversionAndUnref(
6982
0
        proj_create_conversion_geostationary_satellite_sweep_y(
6983
0
            d->getPROJContext(), dfCentralMeridian, dfSatelliteHeight,
6984
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6985
0
}
6986
6987
/************************************************************************/
6988
/*                              OSRSetGEOS()                             */
6989
/************************************************************************/
6990
6991
OGRErr OSRSetGEOS(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
6992
                  double dfSatelliteHeight, double dfFalseEasting,
6993
                  double dfFalseNorthing)
6994
6995
0
{
6996
0
    VALIDATE_POINTER1(hSRS, "OSRSetGEOS", OGRERR_FAILURE);
6997
6998
0
    return ToPointer(hSRS)->SetGEOS(dfCentralMeridian, dfSatelliteHeight,
6999
0
                                    dfFalseEasting, dfFalseNorthing);
7000
0
}
7001
7002
/************************************************************************/
7003
/*                       SetGaussSchreiberTMercator()                   */
7004
/************************************************************************/
7005
7006
OGRErr OGRSpatialReference::SetGaussSchreiberTMercator(double dfCenterLat,
7007
                                                       double dfCenterLong,
7008
                                                       double dfScale,
7009
                                                       double dfFalseEasting,
7010
                                                       double dfFalseNorthing)
7011
7012
0
{
7013
0
    TAKE_OPTIONAL_LOCK();
7014
7015
0
    return d->replaceConversionAndUnref(
7016
0
        proj_create_conversion_gauss_schreiber_transverse_mercator(
7017
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale,
7018
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
7019
0
}
7020
7021
/************************************************************************/
7022
/*                     OSRSetGaussSchreiberTMercator()                  */
7023
/************************************************************************/
7024
7025
OGRErr OSRSetGaussSchreiberTMercator(OGRSpatialReferenceH hSRS,
7026
                                     double dfCenterLat, double dfCenterLong,
7027
                                     double dfScale, double dfFalseEasting,
7028
                                     double dfFalseNorthing)
7029
7030
0
{
7031
0
    VALIDATE_POINTER1(hSRS, "OSRSetGaussSchreiberTMercator", OGRERR_FAILURE);
7032
7033
0
    return ToPointer(hSRS)->SetGaussSchreiberTMercator(
7034
0
        dfCenterLat, dfCenterLong, dfScale, dfFalseEasting, dfFalseNorthing);
7035
0
}
7036
7037
/************************************************************************/
7038
/*                            SetGnomonic()                             */
7039
/************************************************************************/
7040
7041
OGRErr OGRSpatialReference::SetGnomonic(double dfCenterLat, double dfCenterLong,
7042
                                        double dfFalseEasting,
7043
                                        double dfFalseNorthing)
7044
7045
0
{
7046
0
    TAKE_OPTIONAL_LOCK();
7047
7048
0
    return d->replaceConversionAndUnref(proj_create_conversion_gnomonic(
7049
0
        d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7050
0
        dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
7051
0
}
7052
7053
/************************************************************************/
7054
/*                           OSRSetGnomonic()                           */
7055
/************************************************************************/
7056
7057
OGRErr OSRSetGnomonic(OGRSpatialReferenceH hSRS, double dfCenterLat,
7058
                      double dfCenterLong, double dfFalseEasting,
7059
                      double dfFalseNorthing)
7060
7061
0
{
7062
0
    VALIDATE_POINTER1(hSRS, "OSRSetGnomonic", OGRERR_FAILURE);
7063
7064
0
    return ToPointer(hSRS)->SetGnomonic(dfCenterLat, dfCenterLong,
7065
0
                                        dfFalseEasting, dfFalseNorthing);
7066
0
}
7067
7068
/************************************************************************/
7069
/*                              SetHOMAC()                              */
7070
/************************************************************************/
7071
7072
/**
7073
 * \brief Set an Hotine Oblique Mercator Azimuth Center projection using
7074
 * azimuth angle.
7075
 *
7076
 * This projection corresponds to EPSG projection method 9815, also
7077
 * sometimes known as hotine oblique mercator (variant B).
7078
 *
7079
 * This method does the same thing as the C function OSRSetHOMAC().
7080
 *
7081
 * @param dfCenterLat Latitude of the projection origin.
7082
 * @param dfCenterLong Longitude of the projection origin.
7083
 * @param dfAzimuth Azimuth, measured clockwise from North, of the projection
7084
 * centerline.
7085
 * @param dfRectToSkew Angle from Rectified to Skew Grid
7086
 * @param dfScale Scale factor applies to the projection origin.
7087
 * @param dfFalseEasting False easting.
7088
 * @param dfFalseNorthing False northing.
7089
 *
7090
 * @return OGRERR_NONE on success.
7091
 */
7092
7093
OGRErr OGRSpatialReference::SetHOMAC(double dfCenterLat, double dfCenterLong,
7094
                                     double dfAzimuth, double dfRectToSkew,
7095
                                     double dfScale, double dfFalseEasting,
7096
                                     double dfFalseNorthing)
7097
7098
0
{
7099
0
    TAKE_OPTIONAL_LOCK();
7100
7101
0
    return d->replaceConversionAndUnref(
7102
0
        proj_create_conversion_hotine_oblique_mercator_variant_b(
7103
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfAzimuth,
7104
0
            dfRectToSkew, dfScale, dfFalseEasting, dfFalseNorthing, nullptr,
7105
0
            0.0, nullptr, 0.0));
7106
0
}
7107
7108
/************************************************************************/
7109
/*                            OSRSetHOMAC()                             */
7110
/************************************************************************/
7111
7112
/**
7113
 * \brief Set an Oblique Mercator projection using azimuth angle.
7114
 *
7115
 * This is the same as the C++ method OGRSpatialReference::SetHOMAC()
7116
 */
7117
OGRErr OSRSetHOMAC(OGRSpatialReferenceH hSRS, double dfCenterLat,
7118
                   double dfCenterLong, double dfAzimuth, double dfRectToSkew,
7119
                   double dfScale, double dfFalseEasting,
7120
                   double dfFalseNorthing)
7121
7122
0
{
7123
0
    VALIDATE_POINTER1(hSRS, "OSRSetHOMAC", OGRERR_FAILURE);
7124
7125
0
    return ToPointer(hSRS)->SetHOMAC(dfCenterLat, dfCenterLong, dfAzimuth,
7126
0
                                     dfRectToSkew, dfScale, dfFalseEasting,
7127
0
                                     dfFalseNorthing);
7128
0
}
7129
7130
/************************************************************************/
7131
/*                               SetHOM()                               */
7132
/************************************************************************/
7133
7134
/**
7135
 * \brief Set a Hotine Oblique Mercator projection using azimuth angle.
7136
 *
7137
 * This projection corresponds to EPSG projection method 9812, also
7138
 * sometimes known as hotine oblique mercator (variant A)..
7139
 *
7140
 * This method does the same thing as the C function OSRSetHOM().
7141
 *
7142
 * @param dfCenterLat Latitude of the projection origin.
7143
 * @param dfCenterLong Longitude of the projection origin.
7144
 * @param dfAzimuth Azimuth, measured clockwise from North, of the projection
7145
 * centerline.
7146
 * @param dfRectToSkew Angle from Rectified to Skew Grid
7147
 * @param dfScale Scale factor applies to the projection origin.
7148
 * @param dfFalseEasting False easting.
7149
 * @param dfFalseNorthing False northing.
7150
 *
7151
 * @return OGRERR_NONE on success.
7152
 */
7153
7154
OGRErr OGRSpatialReference::SetHOM(double dfCenterLat, double dfCenterLong,
7155
                                   double dfAzimuth, double dfRectToSkew,
7156
                                   double dfScale, double dfFalseEasting,
7157
                                   double dfFalseNorthing)
7158
7159
0
{
7160
0
    TAKE_OPTIONAL_LOCK();
7161
7162
0
    return d->replaceConversionAndUnref(
7163
0
        proj_create_conversion_hotine_oblique_mercator_variant_a(
7164
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfAzimuth,
7165
0
            dfRectToSkew, dfScale, dfFalseEasting, dfFalseNorthing, nullptr,
7166
0
            0.0, nullptr, 0.0));
7167
0
}
7168
7169
/************************************************************************/
7170
/*                             OSRSetHOM()                              */
7171
/************************************************************************/
7172
/**
7173
 * \brief Set a Hotine Oblique Mercator projection using azimuth angle.
7174
 *
7175
 * This is the same as the C++ method OGRSpatialReference::SetHOM()
7176
 */
7177
OGRErr OSRSetHOM(OGRSpatialReferenceH hSRS, double dfCenterLat,
7178
                 double dfCenterLong, double dfAzimuth, double dfRectToSkew,
7179
                 double dfScale, double dfFalseEasting, double dfFalseNorthing)
7180
7181
0
{
7182
0
    VALIDATE_POINTER1(hSRS, "OSRSetHOM", OGRERR_FAILURE);
7183
7184
0
    return ToPointer(hSRS)->SetHOM(dfCenterLat, dfCenterLong, dfAzimuth,
7185
0
                                   dfRectToSkew, dfScale, dfFalseEasting,
7186
0
                                   dfFalseNorthing);
7187
0
}
7188
7189
/************************************************************************/
7190
/*                             SetHOM2PNO()                             */
7191
/************************************************************************/
7192
7193
/**
7194
 * \brief Set a Hotine Oblique Mercator projection using two points on
7195
 * projection centerline.
7196
 *
7197
 * This method does the same thing as the C function OSRSetHOM2PNO().
7198
 *
7199
 * @param dfCenterLat Latitude of the projection origin.
7200
 * @param dfLat1 Latitude of the first point on center line.
7201
 * @param dfLong1 Longitude of the first point on center line.
7202
 * @param dfLat2 Latitude of the second point on center line.
7203
 * @param dfLong2 Longitude of the second point on center line.
7204
 * @param dfScale Scale factor applies to the projection origin.
7205
 * @param dfFalseEasting False easting.
7206
 * @param dfFalseNorthing False northing.
7207
 *
7208
 * @return OGRERR_NONE on success.
7209
 */
7210
7211
OGRErr OGRSpatialReference::SetHOM2PNO(double dfCenterLat, double dfLat1,
7212
                                       double dfLong1, double dfLat2,
7213
                                       double dfLong2, double dfScale,
7214
                                       double dfFalseEasting,
7215
                                       double dfFalseNorthing)
7216
7217
0
{
7218
0
    TAKE_OPTIONAL_LOCK();
7219
7220
0
    return d->replaceConversionAndUnref(
7221
0
        proj_create_conversion_hotine_oblique_mercator_two_point_natural_origin(
7222
0
            d->getPROJContext(), dfCenterLat, dfLat1, dfLong1, dfLat2, dfLong2,
7223
0
            dfScale, dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr,
7224
0
            0.0));
7225
0
}
7226
7227
/************************************************************************/
7228
/*                           OSRSetHOM2PNO()                            */
7229
/************************************************************************/
7230
/**
7231
 * \brief Set a Hotine Oblique Mercator projection using two points on
7232
 *  projection centerline.
7233
 *
7234
 * This is the same as the C++ method OGRSpatialReference::SetHOM2PNO()
7235
 */
7236
OGRErr OSRSetHOM2PNO(OGRSpatialReferenceH hSRS, double dfCenterLat,
7237
                     double dfLat1, double dfLong1, double dfLat2,
7238
                     double dfLong2, double dfScale, double dfFalseEasting,
7239
                     double dfFalseNorthing)
7240
7241
0
{
7242
0
    VALIDATE_POINTER1(hSRS, "OSRSetHOM2PNO", OGRERR_FAILURE);
7243
7244
0
    return ToPointer(hSRS)->SetHOM2PNO(dfCenterLat, dfLat1, dfLong1, dfLat2,
7245
0
                                       dfLong2, dfScale, dfFalseEasting,
7246
0
                                       dfFalseNorthing);
7247
0
}
7248
7249
/************************************************************************/
7250
/*                               SetLOM()                               */
7251
/************************************************************************/
7252
7253
/**
7254
 * \brief Set a Laborde Oblique Mercator projection.
7255
 *
7256
 * @param dfCenterLat Latitude of the projection origin.
7257
 * @param dfCenterLong Longitude of the projection origin.
7258
 * @param dfAzimuth Azimuth, measured clockwise from North, of the projection
7259
 * centerline.
7260
 * @param dfScale Scale factor on the initiali line
7261
 * @param dfFalseEasting False easting.
7262
 * @param dfFalseNorthing False northing.
7263
 *
7264
 * @return OGRERR_NONE on success.
7265
 */
7266
7267
OGRErr OGRSpatialReference::SetLOM(double dfCenterLat, double dfCenterLong,
7268
                                   double dfAzimuth, double dfScale,
7269
                                   double dfFalseEasting,
7270
                                   double dfFalseNorthing)
7271
7272
0
{
7273
0
    TAKE_OPTIONAL_LOCK();
7274
7275
0
    return d->replaceConversionAndUnref(
7276
0
        proj_create_conversion_laborde_oblique_mercator(
7277
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfAzimuth, dfScale,
7278
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
7279
0
}
7280
7281
/************************************************************************/
7282
/*                            SetIWMPolyconic()                         */
7283
/************************************************************************/
7284
7285
OGRErr OGRSpatialReference::SetIWMPolyconic(double dfLat1, double dfLat2,
7286
                                            double dfCenterLong,
7287
                                            double dfFalseEasting,
7288
                                            double dfFalseNorthing)
7289
7290
0
{
7291
0
    TAKE_OPTIONAL_LOCK();
7292
7293
0
    return d->replaceConversionAndUnref(
7294
0
        proj_create_conversion_international_map_world_polyconic(
7295
0
            d->getPROJContext(), dfCenterLong, dfLat1, dfLat2, dfFalseEasting,
7296
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
7297
0
}
7298
7299
/************************************************************************/
7300
/*                          OSRSetIWMPolyconic()                        */
7301
/************************************************************************/
7302
7303
OGRErr OSRSetIWMPolyconic(OGRSpatialReferenceH hSRS, double dfLat1,
7304
                          double dfLat2, double dfCenterLong,
7305
                          double dfFalseEasting, double dfFalseNorthing)
7306
7307
0
{
7308
0
    VALIDATE_POINTER1(hSRS, "OSRSetIWMPolyconic", OGRERR_FAILURE);
7309
7310
0
    return ToPointer(hSRS)->SetIWMPolyconic(dfLat1, dfLat2, dfCenterLong,
7311
0
                                            dfFalseEasting, dfFalseNorthing);
7312
0
}
7313
7314
/************************************************************************/
7315
/*                             SetKrovak()                              */
7316
/************************************************************************/
7317
7318
/** Krovak east-north projection.
7319
 *
7320
 * Note that dfAzimuth and dfPseudoStdParallel1 are ignored when exporting
7321
 * to PROJ and should be respectively set to 30.28813972222222 and 78.5
7322
 */
7323
OGRErr OGRSpatialReference::SetKrovak(double dfCenterLat, double dfCenterLong,
7324
                                      double dfAzimuth,
7325
                                      double dfPseudoStdParallel1,
7326
                                      double dfScale, double dfFalseEasting,
7327
                                      double dfFalseNorthing)
7328
7329
0
{
7330
0
    TAKE_OPTIONAL_LOCK();
7331
7332
0
    return d->replaceConversionAndUnref(
7333
0
        proj_create_conversion_krovak_north_oriented(
7334
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfAzimuth,
7335
0
            dfPseudoStdParallel1, dfScale, dfFalseEasting, dfFalseNorthing,
7336
0
            nullptr, 0.0, nullptr, 0.0));
7337
0
}
7338
7339
/************************************************************************/
7340
/*                            OSRSetKrovak()                            */
7341
/************************************************************************/
7342
7343
OGRErr OSRSetKrovak(OGRSpatialReferenceH hSRS, double dfCenterLat,
7344
                    double dfCenterLong, double dfAzimuth,
7345
                    double dfPseudoStdParallel1, double dfScale,
7346
                    double dfFalseEasting, double dfFalseNorthing)
7347
7348
0
{
7349
0
    VALIDATE_POINTER1(hSRS, "OSRSetKrovak", OGRERR_FAILURE);
7350
7351
0
    return ToPointer(hSRS)->SetKrovak(dfCenterLat, dfCenterLong, dfAzimuth,
7352
0
                                      dfPseudoStdParallel1, dfScale,
7353
0
                                      dfFalseEasting, dfFalseNorthing);
7354
0
}
7355
7356
/************************************************************************/
7357
/*                              SetLAEA()                               */
7358
/************************************************************************/
7359
7360
OGRErr OGRSpatialReference::SetLAEA(double dfCenterLat, double dfCenterLong,
7361
                                    double dfFalseEasting,
7362
                                    double dfFalseNorthing)
7363
7364
0
{
7365
0
    TAKE_OPTIONAL_LOCK();
7366
7367
0
    auto conv = proj_create_conversion_lambert_azimuthal_equal_area(
7368
0
        d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7369
0
        dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
7370
7371
0
    const char *pszName = nullptr;
7372
0
    double dfConvFactor = GetTargetLinearUnits(nullptr, &pszName);
7373
0
    CPLString osName = pszName ? pszName : "";
7374
7375
0
    d->refreshProjObj();
7376
7377
0
    d->demoteFromBoundCRS();
7378
7379
0
    auto cs = proj_create_cartesian_2D_cs(
7380
0
        d->getPROJContext(),
7381
0
        std::fabs(dfCenterLat - 90) < 1e-10 && dfCenterLong == 0
7382
0
            ? PJ_CART2D_NORTH_POLE_EASTING_SOUTH_NORTHING_SOUTH
7383
0
        : std::fabs(dfCenterLat - -90) < 1e-10 && dfCenterLong == 0
7384
0
            ? PJ_CART2D_SOUTH_POLE_EASTING_NORTH_NORTHING_NORTH
7385
0
            : PJ_CART2D_EASTING_NORTHING,
7386
0
        !osName.empty() ? osName.c_str() : nullptr, dfConvFactor);
7387
0
    auto projCRS =
7388
0
        proj_create_projected_crs(d->getPROJContext(), d->getProjCRSName(),
7389
0
                                  d->getGeodBaseCRS(), conv, cs);
7390
0
    proj_destroy(conv);
7391
0
    proj_destroy(cs);
7392
7393
0
    d->setPjCRS(projCRS);
7394
7395
0
    d->undoDemoteFromBoundCRS();
7396
7397
0
    return OGRERR_NONE;
7398
0
}
7399
7400
/************************************************************************/
7401
/*                             OSRSetLAEA()                             */
7402
/************************************************************************/
7403
7404
OGRErr OSRSetLAEA(OGRSpatialReferenceH hSRS, double dfCenterLat,
7405
                  double dfCenterLong, double dfFalseEasting,
7406
                  double dfFalseNorthing)
7407
7408
0
{
7409
0
    VALIDATE_POINTER1(hSRS, "OSRSetLAEA", OGRERR_FAILURE);
7410
7411
0
    return ToPointer(hSRS)->SetLAEA(dfCenterLat, dfCenterLong, dfFalseEasting,
7412
0
                                    dfFalseNorthing);
7413
0
}
7414
7415
/************************************************************************/
7416
/*                               SetLCC()                               */
7417
/************************************************************************/
7418
7419
OGRErr OGRSpatialReference::SetLCC(double dfStdP1, double dfStdP2,
7420
                                   double dfCenterLat, double dfCenterLong,
7421
                                   double dfFalseEasting,
7422
                                   double dfFalseNorthing)
7423
7424
0
{
7425
0
    TAKE_OPTIONAL_LOCK();
7426
7427
0
    return d->replaceConversionAndUnref(
7428
0
        proj_create_conversion_lambert_conic_conformal_2sp(
7429
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfStdP1, dfStdP2,
7430
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0));
7431
0
}
7432
7433
/************************************************************************/
7434
/*                             OSRSetLCC()                              */
7435
/************************************************************************/
7436
7437
OGRErr OSRSetLCC(OGRSpatialReferenceH hSRS, double dfStdP1, double dfStdP2,
7438
                 double dfCenterLat, double dfCenterLong, double dfFalseEasting,
7439
                 double dfFalseNorthing)
7440
7441
0
{
7442
0
    VALIDATE_POINTER1(hSRS, "OSRSetLCC", OGRERR_FAILURE);
7443
7444
0
    return ToPointer(hSRS)->SetLCC(dfStdP1, dfStdP2, dfCenterLat, dfCenterLong,
7445
0
                                   dfFalseEasting, dfFalseNorthing);
7446
0
}
7447
7448
/************************************************************************/
7449
/*                             SetLCC1SP()                              */
7450
/************************************************************************/
7451
7452
OGRErr OGRSpatialReference::SetLCC1SP(double dfCenterLat, double dfCenterLong,
7453
                                      double dfScale, double dfFalseEasting,
7454
                                      double dfFalseNorthing)
7455
7456
0
{
7457
0
    TAKE_OPTIONAL_LOCK();
7458
7459
0
    return d->replaceConversionAndUnref(
7460
0
        proj_create_conversion_lambert_conic_conformal_1sp(
7461
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale,
7462
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0));
7463
0
}
7464
7465
/************************************************************************/
7466
/*                            OSRSetLCC1SP()                            */
7467
/************************************************************************/
7468
7469
OGRErr OSRSetLCC1SP(OGRSpatialReferenceH hSRS, double dfCenterLat,
7470
                    double dfCenterLong, double dfScale, double dfFalseEasting,
7471
                    double dfFalseNorthing)
7472
7473
0
{
7474
0
    VALIDATE_POINTER1(hSRS, "OSRSetLCC1SP", OGRERR_FAILURE);
7475
7476
0
    return ToPointer(hSRS)->SetLCC1SP(dfCenterLat, dfCenterLong, dfScale,
7477
0
                                      dfFalseEasting, dfFalseNorthing);
7478
0
}
7479
7480
/************************************************************************/
7481
/*                              SetLCCB()                               */
7482
/************************************************************************/
7483
7484
OGRErr OGRSpatialReference::SetLCCB(double dfStdP1, double dfStdP2,
7485
                                    double dfCenterLat, double dfCenterLong,
7486
                                    double dfFalseEasting,
7487
                                    double dfFalseNorthing)
7488
7489
0
{
7490
0
    TAKE_OPTIONAL_LOCK();
7491
7492
0
    return d->replaceConversionAndUnref(
7493
0
        proj_create_conversion_lambert_conic_conformal_2sp_belgium(
7494
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfStdP1, dfStdP2,
7495
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0));
7496
0
}
7497
7498
/************************************************************************/
7499
/*                             OSRSetLCCB()                             */
7500
/************************************************************************/
7501
7502
OGRErr OSRSetLCCB(OGRSpatialReferenceH hSRS, double dfStdP1, double dfStdP2,
7503
                  double dfCenterLat, double dfCenterLong,
7504
                  double dfFalseEasting, double dfFalseNorthing)
7505
7506
0
{
7507
0
    VALIDATE_POINTER1(hSRS, "OSRSetLCCB", OGRERR_FAILURE);
7508
7509
0
    return ToPointer(hSRS)->SetLCCB(dfStdP1, dfStdP2, dfCenterLat, dfCenterLong,
7510
0
                                    dfFalseEasting, dfFalseNorthing);
7511
0
}
7512
7513
/************************************************************************/
7514
/*                               SetMC()                                */
7515
/************************************************************************/
7516
7517
OGRErr OGRSpatialReference::SetMC(double dfCenterLat, double dfCenterLong,
7518
                                  double dfFalseEasting, double dfFalseNorthing)
7519
7520
0
{
7521
0
    TAKE_OPTIONAL_LOCK();
7522
7523
0
    (void)dfCenterLat;  // ignored
7524
7525
0
    return d->replaceConversionAndUnref(
7526
0
        proj_create_conversion_miller_cylindrical(
7527
0
            d->getPROJContext(), dfCenterLong, dfFalseEasting, dfFalseNorthing,
7528
0
            nullptr, 0, nullptr, 0));
7529
0
}
7530
7531
/************************************************************************/
7532
/*                              OSRSetMC()                              */
7533
/************************************************************************/
7534
7535
OGRErr OSRSetMC(OGRSpatialReferenceH hSRS, double dfCenterLat,
7536
                double dfCenterLong, double dfFalseEasting,
7537
                double dfFalseNorthing)
7538
7539
0
{
7540
0
    VALIDATE_POINTER1(hSRS, "OSRSetMC", OGRERR_FAILURE);
7541
7542
0
    return ToPointer(hSRS)->SetMC(dfCenterLat, dfCenterLong, dfFalseEasting,
7543
0
                                  dfFalseNorthing);
7544
0
}
7545
7546
/************************************************************************/
7547
/*                            SetMercator()                             */
7548
/************************************************************************/
7549
7550
OGRErr OGRSpatialReference::SetMercator(double dfCenterLat, double dfCenterLong,
7551
                                        double dfScale, double dfFalseEasting,
7552
                                        double dfFalseNorthing)
7553
7554
0
{
7555
0
    TAKE_OPTIONAL_LOCK();
7556
7557
0
    if (dfCenterLat != 0.0 && dfScale == 1.0)
7558
0
    {
7559
        // Not sure this is correct, but this is how it has been used
7560
        // historically
7561
0
        return SetMercator2SP(dfCenterLat, 0.0, dfCenterLong, dfFalseEasting,
7562
0
                              dfFalseNorthing);
7563
0
    }
7564
0
    return d->replaceConversionAndUnref(
7565
0
        proj_create_conversion_mercator_variant_a(
7566
0
            d->getPROJContext(),
7567
0
            dfCenterLat,  // should be zero
7568
0
            dfCenterLong, dfScale, dfFalseEasting, dfFalseNorthing, nullptr, 0,
7569
0
            nullptr, 0));
7570
0
}
7571
7572
/************************************************************************/
7573
/*                           OSRSetMercator()                           */
7574
/************************************************************************/
7575
7576
OGRErr OSRSetMercator(OGRSpatialReferenceH hSRS, double dfCenterLat,
7577
                      double dfCenterLong, double dfScale,
7578
                      double dfFalseEasting, double dfFalseNorthing)
7579
7580
0
{
7581
0
    VALIDATE_POINTER1(hSRS, "OSRSetMercator", OGRERR_FAILURE);
7582
7583
0
    return ToPointer(hSRS)->SetMercator(dfCenterLat, dfCenterLong, dfScale,
7584
0
                                        dfFalseEasting, dfFalseNorthing);
7585
0
}
7586
7587
/************************************************************************/
7588
/*                           SetMercator2SP()                           */
7589
/************************************************************************/
7590
7591
OGRErr OGRSpatialReference::SetMercator2SP(double dfStdP1, double dfCenterLat,
7592
                                           double dfCenterLong,
7593
                                           double dfFalseEasting,
7594
                                           double dfFalseNorthing)
7595
7596
0
{
7597
0
    if (dfCenterLat == 0.0)
7598
0
    {
7599
0
        return d->replaceConversionAndUnref(
7600
0
            proj_create_conversion_mercator_variant_b(
7601
0
                d->getPROJContext(), dfStdP1, dfCenterLong, dfFalseEasting,
7602
0
                dfFalseNorthing, nullptr, 0, nullptr, 0));
7603
0
    }
7604
7605
0
    TAKE_OPTIONAL_LOCK();
7606
7607
0
    SetProjection(SRS_PT_MERCATOR_2SP);
7608
7609
0
    SetNormProjParm(SRS_PP_STANDARD_PARALLEL_1, dfStdP1);
7610
0
    SetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, dfCenterLat);
7611
0
    SetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, dfCenterLong);
7612
0
    SetNormProjParm(SRS_PP_FALSE_EASTING, dfFalseEasting);
7613
0
    SetNormProjParm(SRS_PP_FALSE_NORTHING, dfFalseNorthing);
7614
7615
0
    return OGRERR_NONE;
7616
0
}
7617
7618
/************************************************************************/
7619
/*                         OSRSetMercator2SP()                          */
7620
/************************************************************************/
7621
7622
OGRErr OSRSetMercator2SP(OGRSpatialReferenceH hSRS, double dfStdP1,
7623
                         double dfCenterLat, double dfCenterLong,
7624
                         double dfFalseEasting, double dfFalseNorthing)
7625
7626
0
{
7627
0
    VALIDATE_POINTER1(hSRS, "OSRSetMercator2SP", OGRERR_FAILURE);
7628
7629
0
    return ToPointer(hSRS)->SetMercator2SP(dfStdP1, dfCenterLat, dfCenterLong,
7630
0
                                           dfFalseEasting, dfFalseNorthing);
7631
0
}
7632
7633
/************************************************************************/
7634
/*                            SetMollweide()                            */
7635
/************************************************************************/
7636
7637
OGRErr OGRSpatialReference::SetMollweide(double dfCentralMeridian,
7638
                                         double dfFalseEasting,
7639
                                         double dfFalseNorthing)
7640
7641
0
{
7642
0
    TAKE_OPTIONAL_LOCK();
7643
7644
0
    return d->replaceConversionAndUnref(proj_create_conversion_mollweide(
7645
0
        d->getPROJContext(), dfCentralMeridian, dfFalseEasting, dfFalseNorthing,
7646
0
        nullptr, 0, nullptr, 0));
7647
0
}
7648
7649
/************************************************************************/
7650
/*                          OSRSetMollweide()                           */
7651
/************************************************************************/
7652
7653
OGRErr OSRSetMollweide(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
7654
                       double dfFalseEasting, double dfFalseNorthing)
7655
7656
0
{
7657
0
    VALIDATE_POINTER1(hSRS, "OSRSetMollweide", OGRERR_FAILURE);
7658
7659
0
    return ToPointer(hSRS)->SetMollweide(dfCentralMeridian, dfFalseEasting,
7660
0
                                         dfFalseNorthing);
7661
0
}
7662
7663
/************************************************************************/
7664
/*                              SetNZMG()                               */
7665
/************************************************************************/
7666
7667
OGRErr OGRSpatialReference::SetNZMG(double dfCenterLat, double dfCenterLong,
7668
                                    double dfFalseEasting,
7669
                                    double dfFalseNorthing)
7670
7671
0
{
7672
0
    TAKE_OPTIONAL_LOCK();
7673
7674
0
    return d->replaceConversionAndUnref(
7675
0
        proj_create_conversion_new_zealand_mapping_grid(
7676
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7677
0
            dfFalseNorthing, nullptr, 0, nullptr, 0));
7678
0
}
7679
7680
/************************************************************************/
7681
/*                             OSRSetNZMG()                             */
7682
/************************************************************************/
7683
7684
OGRErr OSRSetNZMG(OGRSpatialReferenceH hSRS, double dfCenterLat,
7685
                  double dfCenterLong, double dfFalseEasting,
7686
                  double dfFalseNorthing)
7687
7688
0
{
7689
0
    VALIDATE_POINTER1(hSRS, "OSRSetNZMG", OGRERR_FAILURE);
7690
7691
0
    return ToPointer(hSRS)->SetNZMG(dfCenterLat, dfCenterLong, dfFalseEasting,
7692
0
                                    dfFalseNorthing);
7693
0
}
7694
7695
/************************************************************************/
7696
/*                               SetOS()                                */
7697
/************************************************************************/
7698
7699
OGRErr OGRSpatialReference::SetOS(double dfOriginLat, double dfCMeridian,
7700
                                  double dfScale, double dfFalseEasting,
7701
                                  double dfFalseNorthing)
7702
7703
0
{
7704
0
    TAKE_OPTIONAL_LOCK();
7705
7706
0
    return d->replaceConversionAndUnref(
7707
0
        proj_create_conversion_oblique_stereographic(
7708
0
            d->getPROJContext(), dfOriginLat, dfCMeridian, dfScale,
7709
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0));
7710
0
}
7711
7712
/************************************************************************/
7713
/*                              OSRSetOS()                              */
7714
/************************************************************************/
7715
7716
OGRErr OSRSetOS(OGRSpatialReferenceH hSRS, double dfOriginLat,
7717
                double dfCMeridian, double dfScale, double dfFalseEasting,
7718
                double dfFalseNorthing)
7719
7720
0
{
7721
0
    VALIDATE_POINTER1(hSRS, "OSRSetOS", OGRERR_FAILURE);
7722
7723
0
    return ToPointer(hSRS)->SetOS(dfOriginLat, dfCMeridian, dfScale,
7724
0
                                  dfFalseEasting, dfFalseNorthing);
7725
0
}
7726
7727
/************************************************************************/
7728
/*                          SetOrthographic()                           */
7729
/************************************************************************/
7730
7731
OGRErr OGRSpatialReference::SetOrthographic(double dfCenterLat,
7732
                                            double dfCenterLong,
7733
                                            double dfFalseEasting,
7734
                                            double dfFalseNorthing)
7735
7736
0
{
7737
0
    TAKE_OPTIONAL_LOCK();
7738
7739
0
    return d->replaceConversionAndUnref(proj_create_conversion_orthographic(
7740
0
        d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7741
0
        dfFalseNorthing, nullptr, 0, nullptr, 0));
7742
0
}
7743
7744
/************************************************************************/
7745
/*                         OSRSetOrthographic()                         */
7746
/************************************************************************/
7747
7748
OGRErr OSRSetOrthographic(OGRSpatialReferenceH hSRS, double dfCenterLat,
7749
                          double dfCenterLong, double dfFalseEasting,
7750
                          double dfFalseNorthing)
7751
7752
0
{
7753
0
    VALIDATE_POINTER1(hSRS, "OSRSetOrthographic", OGRERR_FAILURE);
7754
7755
0
    return ToPointer(hSRS)->SetOrthographic(dfCenterLat, dfCenterLong,
7756
0
                                            dfFalseEasting, dfFalseNorthing);
7757
0
}
7758
7759
/************************************************************************/
7760
/*                            SetPolyconic()                            */
7761
/************************************************************************/
7762
7763
OGRErr OGRSpatialReference::SetPolyconic(double dfCenterLat,
7764
                                         double dfCenterLong,
7765
                                         double dfFalseEasting,
7766
                                         double dfFalseNorthing)
7767
7768
0
{
7769
0
    TAKE_OPTIONAL_LOCK();
7770
7771
    // note: it seems that by some definitions this should include a
7772
    //       scale_factor parameter.
7773
0
    return d->replaceConversionAndUnref(
7774
0
        proj_create_conversion_american_polyconic(
7775
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7776
0
            dfFalseNorthing, nullptr, 0, nullptr, 0));
7777
0
}
7778
7779
/************************************************************************/
7780
/*                          OSRSetPolyconic()                           */
7781
/************************************************************************/
7782
7783
OGRErr OSRSetPolyconic(OGRSpatialReferenceH hSRS, double dfCenterLat,
7784
                       double dfCenterLong, double dfFalseEasting,
7785
                       double dfFalseNorthing)
7786
7787
0
{
7788
0
    VALIDATE_POINTER1(hSRS, "OSRSetPolyconic", OGRERR_FAILURE);
7789
7790
0
    return ToPointer(hSRS)->SetPolyconic(dfCenterLat, dfCenterLong,
7791
0
                                         dfFalseEasting, dfFalseNorthing);
7792
0
}
7793
7794
/************************************************************************/
7795
/*                               SetPS()                                */
7796
/************************************************************************/
7797
7798
/** Sets a Polar Stereographic projection.
7799
 *
7800
 * Two variants are possible:
7801
 * - Polar Stereographic Variant A: dfCenterLat must be +/- 90° and is
7802
 *   interpreted as the latitude of origin, combined with the scale factor
7803
 * - Polar Stereographic Variant B: dfCenterLat is different from +/- 90° and
7804
 *   is interpreted as the latitude of true scale. In that situation, dfScale
7805
 *   must be set to 1 (it is ignored in the projection parameters)
7806
 */
7807
OGRErr OGRSpatialReference::SetPS(double dfCenterLat, double dfCenterLong,
7808
                                  double dfScale, double dfFalseEasting,
7809
                                  double dfFalseNorthing)
7810
7811
0
{
7812
0
    TAKE_OPTIONAL_LOCK();
7813
7814
0
    PJ *conv;
7815
0
    if (dfScale == 1.0 && std::abs(std::abs(dfCenterLat) - 90) > 1e-8)
7816
0
    {
7817
0
        conv = proj_create_conversion_polar_stereographic_variant_b(
7818
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7819
0
            dfFalseNorthing, nullptr, 0, nullptr, 0);
7820
0
    }
7821
0
    else
7822
0
    {
7823
0
        conv = proj_create_conversion_polar_stereographic_variant_a(
7824
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale,
7825
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0);
7826
0
    }
7827
7828
0
    const char *pszName = nullptr;
7829
0
    double dfConvFactor = GetTargetLinearUnits(nullptr, &pszName);
7830
0
    CPLString osName = pszName ? pszName : "";
7831
7832
0
    d->refreshProjObj();
7833
7834
0
    d->demoteFromBoundCRS();
7835
7836
0
    auto cs = proj_create_cartesian_2D_cs(
7837
0
        d->getPROJContext(),
7838
0
        dfCenterLat > 0 ? PJ_CART2D_NORTH_POLE_EASTING_SOUTH_NORTHING_SOUTH
7839
0
                        : PJ_CART2D_SOUTH_POLE_EASTING_NORTH_NORTHING_NORTH,
7840
0
        !osName.empty() ? osName.c_str() : nullptr, dfConvFactor);
7841
0
    auto projCRS =
7842
0
        proj_create_projected_crs(d->getPROJContext(), d->getProjCRSName(),
7843
0
                                  d->getGeodBaseCRS(), conv, cs);
7844
0
    proj_destroy(conv);
7845
0
    proj_destroy(cs);
7846
7847
0
    d->setPjCRS(projCRS);
7848
7849
0
    d->undoDemoteFromBoundCRS();
7850
7851
0
    return OGRERR_NONE;
7852
0
}
7853
7854
/************************************************************************/
7855
/*                              OSRSetPS()                              */
7856
/************************************************************************/
7857
7858
OGRErr OSRSetPS(OGRSpatialReferenceH hSRS, double dfCenterLat,
7859
                double dfCenterLong, double dfScale, double dfFalseEasting,
7860
                double dfFalseNorthing)
7861
7862
0
{
7863
0
    VALIDATE_POINTER1(hSRS, "OSRSetPS", OGRERR_FAILURE);
7864
7865
0
    return ToPointer(hSRS)->SetPS(dfCenterLat, dfCenterLong, dfScale,
7866
0
                                  dfFalseEasting, dfFalseNorthing);
7867
0
}
7868
7869
/************************************************************************/
7870
/*                            SetRobinson()                             */
7871
/************************************************************************/
7872
7873
OGRErr OGRSpatialReference::SetRobinson(double dfCenterLong,
7874
                                        double dfFalseEasting,
7875
                                        double dfFalseNorthing)
7876
7877
0
{
7878
0
    TAKE_OPTIONAL_LOCK();
7879
7880
0
    return d->replaceConversionAndUnref(proj_create_conversion_robinson(
7881
0
        d->getPROJContext(), dfCenterLong, dfFalseEasting, dfFalseNorthing,
7882
0
        nullptr, 0, nullptr, 0));
7883
0
}
7884
7885
/************************************************************************/
7886
/*                           OSRSetRobinson()                           */
7887
/************************************************************************/
7888
7889
OGRErr OSRSetRobinson(OGRSpatialReferenceH hSRS, double dfCenterLong,
7890
                      double dfFalseEasting, double dfFalseNorthing)
7891
7892
0
{
7893
0
    VALIDATE_POINTER1(hSRS, "OSRSetRobinson", OGRERR_FAILURE);
7894
7895
0
    return ToPointer(hSRS)->SetRobinson(dfCenterLong, dfFalseEasting,
7896
0
                                        dfFalseNorthing);
7897
0
}
7898
7899
/************************************************************************/
7900
/*                           SetSinusoidal()                            */
7901
/************************************************************************/
7902
7903
OGRErr OGRSpatialReference::SetSinusoidal(double dfCenterLong,
7904
                                          double dfFalseEasting,
7905
                                          double dfFalseNorthing)
7906
7907
0
{
7908
0
    TAKE_OPTIONAL_LOCK();
7909
7910
0
    return d->replaceConversionAndUnref(proj_create_conversion_sinusoidal(
7911
0
        d->getPROJContext(), dfCenterLong, dfFalseEasting, dfFalseNorthing,
7912
0
        nullptr, 0, nullptr, 0));
7913
0
}
7914
7915
/************************************************************************/
7916
/*                          OSRSetSinusoidal()                          */
7917
/************************************************************************/
7918
7919
OGRErr OSRSetSinusoidal(OGRSpatialReferenceH hSRS, double dfCenterLong,
7920
                        double dfFalseEasting, double dfFalseNorthing)
7921
7922
0
{
7923
0
    VALIDATE_POINTER1(hSRS, "OSRSetSinusoidal", OGRERR_FAILURE);
7924
7925
0
    return ToPointer(hSRS)->SetSinusoidal(dfCenterLong, dfFalseEasting,
7926
0
                                          dfFalseNorthing);
7927
0
}
7928
7929
/************************************************************************/
7930
/*                          SetStereographic()                          */
7931
/************************************************************************/
7932
7933
OGRErr OGRSpatialReference::SetStereographic(double dfOriginLat,
7934
                                             double dfCMeridian, double dfScale,
7935
                                             double dfFalseEasting,
7936
                                             double dfFalseNorthing)
7937
7938
0
{
7939
0
    TAKE_OPTIONAL_LOCK();
7940
7941
0
    return d->replaceConversionAndUnref(proj_create_conversion_stereographic(
7942
0
        d->getPROJContext(), dfOriginLat, dfCMeridian, dfScale, dfFalseEasting,
7943
0
        dfFalseNorthing, nullptr, 0, nullptr, 0));
7944
0
}
7945
7946
/************************************************************************/
7947
/*                        OSRSetStereographic()                         */
7948
/************************************************************************/
7949
7950
OGRErr OSRSetStereographic(OGRSpatialReferenceH hSRS, double dfOriginLat,
7951
                           double dfCMeridian, double dfScale,
7952
                           double dfFalseEasting, double dfFalseNorthing)
7953
7954
0
{
7955
0
    VALIDATE_POINTER1(hSRS, "OSRSetStereographic", OGRERR_FAILURE);
7956
7957
0
    return ToPointer(hSRS)->SetStereographic(dfOriginLat, dfCMeridian, dfScale,
7958
0
                                             dfFalseEasting, dfFalseNorthing);
7959
0
}
7960
7961
/************************************************************************/
7962
/*                               SetSOC()                               */
7963
/*                                                                      */
7964
/*      NOTE: This definition isn't really used in practice any more    */
7965
/*      and should be considered deprecated.  It seems that swiss       */
7966
/*      oblique mercator is now define as Hotine_Oblique_Mercator       */
7967
/*      with an azimuth of 90 and a rectified_grid_angle of 90.  See    */
7968
/*      EPSG:2056 and Bug 423.                                          */
7969
/************************************************************************/
7970
7971
OGRErr OGRSpatialReference::SetSOC(double dfLatitudeOfOrigin,
7972
                                   double dfCentralMeridian,
7973
                                   double dfFalseEasting,
7974
                                   double dfFalseNorthing)
7975
7976
0
{
7977
0
    TAKE_OPTIONAL_LOCK();
7978
7979
0
    return d->replaceConversionAndUnref(
7980
0
        proj_create_conversion_hotine_oblique_mercator_variant_b(
7981
0
            d->getPROJContext(), dfLatitudeOfOrigin, dfCentralMeridian, 90.0,
7982
0
            90.0, 1.0, dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr,
7983
0
            0.0));
7984
#if 0
7985
    SetProjection( SRS_PT_SWISS_OBLIQUE_CYLINDRICAL );
7986
    SetNormProjParm( SRS_PP_LATITUDE_OF_CENTER, dfLatitudeOfOrigin );
7987
    SetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, dfCentralMeridian );
7988
    SetNormProjParm( SRS_PP_FALSE_EASTING, dfFalseEasting );
7989
    SetNormProjParm( SRS_PP_FALSE_NORTHING, dfFalseNorthing );
7990
7991
    return OGRERR_NONE;
7992
#endif
7993
0
}
7994
7995
/************************************************************************/
7996
/*                             OSRSetSOC()                              */
7997
/************************************************************************/
7998
7999
OGRErr OSRSetSOC(OGRSpatialReferenceH hSRS, double dfLatitudeOfOrigin,
8000
                 double dfCentralMeridian, double dfFalseEasting,
8001
                 double dfFalseNorthing)
8002
8003
0
{
8004
0
    VALIDATE_POINTER1(hSRS, "OSRSetSOC", OGRERR_FAILURE);
8005
8006
0
    return ToPointer(hSRS)->SetSOC(dfLatitudeOfOrigin, dfCentralMeridian,
8007
0
                                   dfFalseEasting, dfFalseNorthing);
8008
0
}
8009
8010
/************************************************************************/
8011
/*                               SetVDG()                               */
8012
/************************************************************************/
8013
8014
OGRErr OGRSpatialReference::SetVDG(double dfCMeridian, double dfFalseEasting,
8015
                                   double dfFalseNorthing)
8016
8017
0
{
8018
0
    TAKE_OPTIONAL_LOCK();
8019
8020
0
    return d->replaceConversionAndUnref(proj_create_conversion_van_der_grinten(
8021
0
        d->getPROJContext(), dfCMeridian, dfFalseEasting, dfFalseNorthing,
8022
0
        nullptr, 0, nullptr, 0));
8023
0
}
8024
8025
/************************************************************************/
8026
/*                             OSRSetVDG()                              */
8027
/************************************************************************/
8028
8029
OGRErr OSRSetVDG(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
8030
                 double dfFalseEasting, double dfFalseNorthing)
8031
8032
0
{
8033
0
    VALIDATE_POINTER1(hSRS, "OSRSetVDG", OGRERR_FAILURE);
8034
8035
0
    return ToPointer(hSRS)->SetVDG(dfCentralMeridian, dfFalseEasting,
8036
0
                                   dfFalseNorthing);
8037
0
}
8038
8039
/************************************************************************/
8040
/*                               SetUTM()                               */
8041
/************************************************************************/
8042
8043
/**
8044
 * \brief Set UTM projection definition.
8045
 *
8046
 * This will generate a projection definition with the full set of
8047
 * transverse mercator projection parameters for the given UTM zone.
8048
 * If no PROJCS[] description is set yet, one will be set to look
8049
 * like "UTM Zone %d, {Northern, Southern} Hemisphere".
8050
 *
8051
 * This method is the same as the C function OSRSetUTM().
8052
 *
8053
 * @param nZone UTM zone.
8054
 *
8055
 * @param bNorth TRUE for northern hemisphere, or FALSE for southern
8056
 * hemisphere.
8057
 *
8058
 * @return OGRERR_NONE on success.
8059
 */
8060
8061
OGRErr OGRSpatialReference::SetUTM(int nZone, int bNorth)
8062
8063
0
{
8064
0
    TAKE_OPTIONAL_LOCK();
8065
8066
0
    if (nZone < 0 || nZone > 60)
8067
0
    {
8068
0
        CPLError(CE_Failure, CPLE_AppDefined, "Invalid zone: %d", nZone);
8069
0
        return OGRERR_FAILURE;
8070
0
    }
8071
8072
0
    return d->replaceConversionAndUnref(
8073
0
        proj_create_conversion_utm(d->getPROJContext(), nZone, bNorth));
8074
0
}
8075
8076
/************************************************************************/
8077
/*                             OSRSetUTM()                              */
8078
/************************************************************************/
8079
8080
/**
8081
 * \brief Set UTM projection definition.
8082
 *
8083
 * This is the same as the C++ method OGRSpatialReference::SetUTM()
8084
 */
8085
OGRErr OSRSetUTM(OGRSpatialReferenceH hSRS, int nZone, int bNorth)
8086
8087
0
{
8088
0
    VALIDATE_POINTER1(hSRS, "OSRSetUTM", OGRERR_FAILURE);
8089
8090
0
    return ToPointer(hSRS)->SetUTM(nZone, bNorth);
8091
0
}
8092
8093
/************************************************************************/
8094
/*                             GetUTMZone()                             */
8095
/*                                                                      */
8096
/*      Returns zero if it isn't UTM.                                   */
8097
/************************************************************************/
8098
8099
/**
8100
 * \brief Get utm zone information.
8101
 *
8102
 * This is the same as the C function OSRGetUTMZone().
8103
 *
8104
 * In SWIG bindings (Python, Java, etc) the GetUTMZone() method returns a
8105
 * zone which is negative in the southern hemisphere instead of having the
8106
 * pbNorth flag used in the C and C++ interface.
8107
 *
8108
 * @param pbNorth pointer to in to set to TRUE if northern hemisphere, or
8109
 * FALSE if southern.
8110
 *
8111
 * @return UTM zone number or zero if this isn't a UTM definition.
8112
 */
8113
8114
int OGRSpatialReference::GetUTMZone(int *pbNorth) const
8115
8116
0
{
8117
0
    TAKE_OPTIONAL_LOCK();
8118
8119
0
    if (IsProjected() && GetAxesCount() == 3)
8120
0
    {
8121
0
        OGRSpatialReference *poSRSTmp = Clone();
8122
0
        poSRSTmp->DemoteTo2D(nullptr);
8123
0
        const int nZone = poSRSTmp->GetUTMZone(pbNorth);
8124
0
        delete poSRSTmp;
8125
0
        return nZone;
8126
0
    }
8127
8128
0
    const char *pszProjection = GetAttrValue("PROJECTION");
8129
8130
0
    if (pszProjection == nullptr ||
8131
0
        !EQUAL(pszProjection, SRS_PT_TRANSVERSE_MERCATOR))
8132
0
        return 0;
8133
8134
0
    if (GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0) != 0.0)
8135
0
        return 0;
8136
8137
0
    if (GetProjParm(SRS_PP_SCALE_FACTOR, 1.0) != 0.9996)
8138
0
        return 0;
8139
8140
0
    if (fabs(GetNormProjParm(SRS_PP_FALSE_EASTING, 0.0) - 500000.0) > 0.001)
8141
0
        return 0;
8142
8143
0
    const double dfFalseNorthing = GetNormProjParm(SRS_PP_FALSE_NORTHING, 0.0);
8144
8145
0
    if (dfFalseNorthing != 0.0 && fabs(dfFalseNorthing - 10000000.0) > 0.001)
8146
0
        return 0;
8147
8148
0
    if (pbNorth != nullptr)
8149
0
        *pbNorth = (dfFalseNorthing == 0);
8150
8151
0
    const double dfCentralMeridian =
8152
0
        GetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, 0.0);
8153
0
    const double dfZone = (dfCentralMeridian + 186.0) / 6.0;
8154
8155
0
    if (dfCentralMeridian < -177.00001 || dfCentralMeridian > 177.000001 ||
8156
0
        std::isnan(dfZone) ||
8157
0
        std::abs(dfZone - static_cast<int>(dfZone) - 0.5) > 0.00001)
8158
0
        return 0;
8159
8160
0
    return static_cast<int>(dfZone);
8161
0
}
8162
8163
/************************************************************************/
8164
/*                           OSRGetUTMZone()                            */
8165
/************************************************************************/
8166
8167
/**
8168
 * \brief Get utm zone information.
8169
 *
8170
 * This is the same as the C++ method OGRSpatialReference::GetUTMZone()
8171
 */
8172
int OSRGetUTMZone(OGRSpatialReferenceH hSRS, int *pbNorth)
8173
8174
0
{
8175
0
    VALIDATE_POINTER1(hSRS, "OSRGetUTMZone", 0);
8176
8177
0
    return ToPointer(hSRS)->GetUTMZone(pbNorth);
8178
0
}
8179
8180
/************************************************************************/
8181
/*                             SetWagner()                              */
8182
/************************************************************************/
8183
8184
OGRErr OGRSpatialReference::SetWagner(int nVariation,  // 1--7.
8185
                                      double dfCenterLat, double dfFalseEasting,
8186
                                      double dfFalseNorthing)
8187
8188
0
{
8189
0
    TAKE_OPTIONAL_LOCK();
8190
8191
0
    PJ *conv;
8192
0
    if (nVariation == 1)
8193
0
    {
8194
0
        conv = proj_create_conversion_wagner_i(d->getPROJContext(), 0.0,
8195
0
                                               dfFalseEasting, dfFalseNorthing,
8196
0
                                               nullptr, 0.0, nullptr, 0.0);
8197
0
    }
8198
0
    else if (nVariation == 2)
8199
0
    {
8200
0
        conv = proj_create_conversion_wagner_ii(d->getPROJContext(), 0.0,
8201
0
                                                dfFalseEasting, dfFalseNorthing,
8202
0
                                                nullptr, 0.0, nullptr, 0.0);
8203
0
    }
8204
0
    else if (nVariation == 3)
8205
0
    {
8206
0
        conv = proj_create_conversion_wagner_iii(
8207
0
            d->getPROJContext(), dfCenterLat, 0.0, dfFalseEasting,
8208
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
8209
0
    }
8210
0
    else if (nVariation == 4)
8211
0
    {
8212
0
        conv = proj_create_conversion_wagner_iv(d->getPROJContext(), 0.0,
8213
0
                                                dfFalseEasting, dfFalseNorthing,
8214
0
                                                nullptr, 0.0, nullptr, 0.0);
8215
0
    }
8216
0
    else if (nVariation == 5)
8217
0
    {
8218
0
        conv = proj_create_conversion_wagner_v(d->getPROJContext(), 0.0,
8219
0
                                               dfFalseEasting, dfFalseNorthing,
8220
0
                                               nullptr, 0.0, nullptr, 0.0);
8221
0
    }
8222
0
    else if (nVariation == 6)
8223
0
    {
8224
0
        conv = proj_create_conversion_wagner_vi(d->getPROJContext(), 0.0,
8225
0
                                                dfFalseEasting, dfFalseNorthing,
8226
0
                                                nullptr, 0.0, nullptr, 0.0);
8227
0
    }
8228
0
    else if (nVariation == 7)
8229
0
    {
8230
0
        conv = proj_create_conversion_wagner_vii(
8231
0
            d->getPROJContext(), 0.0, dfFalseEasting, dfFalseNorthing, nullptr,
8232
0
            0.0, nullptr, 0.0);
8233
0
    }
8234
0
    else
8235
0
    {
8236
0
        CPLError(CE_Failure, CPLE_AppDefined,
8237
0
                 "Unsupported Wagner variation (%d).", nVariation);
8238
0
        return OGRERR_UNSUPPORTED_SRS;
8239
0
    }
8240
8241
0
    return d->replaceConversionAndUnref(conv);
8242
0
}
8243
8244
/************************************************************************/
8245
/*                            OSRSetWagner()                            */
8246
/************************************************************************/
8247
8248
OGRErr OSRSetWagner(OGRSpatialReferenceH hSRS, int nVariation,
8249
                    double dfCenterLat, double dfFalseEasting,
8250
                    double dfFalseNorthing)
8251
8252
0
{
8253
0
    VALIDATE_POINTER1(hSRS, "OSRSetWagner", OGRERR_FAILURE);
8254
8255
0
    return ToPointer(hSRS)->SetWagner(nVariation, dfCenterLat, dfFalseEasting,
8256
0
                                      dfFalseNorthing);
8257
0
}
8258
8259
/************************************************************************/
8260
/*                            SetQSC()                     */
8261
/************************************************************************/
8262
8263
OGRErr OGRSpatialReference::SetQSC(double dfCenterLat, double dfCenterLong)
8264
0
{
8265
0
    TAKE_OPTIONAL_LOCK();
8266
8267
0
    return d->replaceConversionAndUnref(
8268
0
        proj_create_conversion_quadrilateralized_spherical_cube(
8269
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, 0.0, 0.0, nullptr,
8270
0
            0, nullptr, 0));
8271
0
}
8272
8273
/************************************************************************/
8274
/*                           OSRSetQSC()                   */
8275
/************************************************************************/
8276
8277
OGRErr OSRSetQSC(OGRSpatialReferenceH hSRS, double dfCenterLat,
8278
                 double dfCenterLong)
8279
8280
0
{
8281
0
    VALIDATE_POINTER1(hSRS, "OSRSetQSC", OGRERR_FAILURE);
8282
8283
0
    return ToPointer(hSRS)->SetQSC(dfCenterLat, dfCenterLong);
8284
0
}
8285
8286
/************************************************************************/
8287
/*                            SetSCH()                     */
8288
/************************************************************************/
8289
8290
OGRErr OGRSpatialReference::SetSCH(double dfPegLat, double dfPegLong,
8291
                                   double dfPegHeading, double dfPegHgt)
8292
8293
0
{
8294
0
    TAKE_OPTIONAL_LOCK();
8295
8296
0
    return d->replaceConversionAndUnref(
8297
0
        proj_create_conversion_spherical_cross_track_height(
8298
0
            d->getPROJContext(), dfPegLat, dfPegLong, dfPegHeading, dfPegHgt,
8299
0
            nullptr, 0, nullptr, 0));
8300
0
}
8301
8302
/************************************************************************/
8303
/*                           OSRSetSCH()                   */
8304
/************************************************************************/
8305
8306
OGRErr OSRSetSCH(OGRSpatialReferenceH hSRS, double dfPegLat, double dfPegLong,
8307
                 double dfPegHeading, double dfPegHgt)
8308
8309
0
{
8310
0
    VALIDATE_POINTER1(hSRS, "OSRSetSCH", OGRERR_FAILURE);
8311
8312
0
    return ToPointer(hSRS)->SetSCH(dfPegLat, dfPegLong, dfPegHeading, dfPegHgt);
8313
0
}
8314
8315
/************************************************************************/
8316
/*                         SetVerticalPerspective()                     */
8317
/************************************************************************/
8318
8319
OGRErr OGRSpatialReference::SetVerticalPerspective(
8320
    double dfTopoOriginLat, double dfTopoOriginLon, double dfTopoOriginHeight,
8321
    double dfViewPointHeight, double dfFalseEasting, double dfFalseNorthing)
8322
0
{
8323
0
    TAKE_OPTIONAL_LOCK();
8324
8325
0
    return d->replaceConversionAndUnref(
8326
0
        proj_create_conversion_vertical_perspective(
8327
0
            d->getPROJContext(), dfTopoOriginLat, dfTopoOriginLon,
8328
0
            dfTopoOriginHeight, dfViewPointHeight, dfFalseEasting,
8329
0
            dfFalseNorthing, nullptr, 0, nullptr, 0));
8330
0
}
8331
8332
/************************************************************************/
8333
/*                       OSRSetVerticalPerspective()                    */
8334
/************************************************************************/
8335
8336
OGRErr OSRSetVerticalPerspective(OGRSpatialReferenceH hSRS,
8337
                                 double dfTopoOriginLat, double dfTopoOriginLon,
8338
                                 double dfTopoOriginHeight,
8339
                                 double dfViewPointHeight,
8340
                                 double dfFalseEasting, double dfFalseNorthing)
8341
8342
0
{
8343
0
    VALIDATE_POINTER1(hSRS, "OSRSetVerticalPerspective", OGRERR_FAILURE);
8344
8345
0
    return ToPointer(hSRS)->SetVerticalPerspective(
8346
0
        dfTopoOriginLat, dfTopoOriginLon, dfTopoOriginHeight, dfViewPointHeight,
8347
0
        dfFalseEasting, dfFalseNorthing);
8348
0
}
8349
8350
/************************************************************************/
8351
/*             SetDerivedGeogCRSWithPoleRotationGRIBConvention()        */
8352
/************************************************************************/
8353
8354
OGRErr OGRSpatialReference::SetDerivedGeogCRSWithPoleRotationGRIBConvention(
8355
    const char *pszCRSName, double dfSouthPoleLat, double dfSouthPoleLon,
8356
    double dfAxisRotation)
8357
0
{
8358
0
    TAKE_OPTIONAL_LOCK();
8359
8360
0
    d->refreshProjObj();
8361
0
    if (!d->m_pj_crs)
8362
0
        return OGRERR_FAILURE;
8363
0
    if (d->m_pjType != PJ_TYPE_GEOGRAPHIC_2D_CRS)
8364
0
        return OGRERR_FAILURE;
8365
0
    auto ctxt = d->getPROJContext();
8366
0
    auto conv = proj_create_conversion_pole_rotation_grib_convention(
8367
0
        ctxt, dfSouthPoleLat, dfSouthPoleLon, dfAxisRotation, nullptr, 0);
8368
0
    auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
8369
0
    d->setPjCRS(proj_create_derived_geographic_crs(ctxt, pszCRSName,
8370
0
                                                   d->m_pj_crs, conv, cs));
8371
0
    proj_destroy(conv);
8372
0
    proj_destroy(cs);
8373
0
    return OGRERR_NONE;
8374
0
}
8375
8376
/************************************************************************/
8377
/*         SetDerivedGeogCRSWithPoleRotationNetCDFCFConvention()        */
8378
/************************************************************************/
8379
8380
OGRErr OGRSpatialReference::SetDerivedGeogCRSWithPoleRotationNetCDFCFConvention(
8381
    const char *pszCRSName, double dfGridNorthPoleLat,
8382
    double dfGridNorthPoleLon, double dfNorthPoleGridLon)
8383
0
{
8384
0
    TAKE_OPTIONAL_LOCK();
8385
8386
0
#if PROJ_VERSION_MAJOR > 8 ||                                                  \
8387
0
    (PROJ_VERSION_MAJOR == 8 && PROJ_VERSION_MINOR >= 2)
8388
0
    d->refreshProjObj();
8389
0
    if (!d->m_pj_crs)
8390
0
        return OGRERR_FAILURE;
8391
0
    if (d->m_pjType != PJ_TYPE_GEOGRAPHIC_2D_CRS)
8392
0
        return OGRERR_FAILURE;
8393
0
    auto ctxt = d->getPROJContext();
8394
0
    auto conv = proj_create_conversion_pole_rotation_netcdf_cf_convention(
8395
0
        ctxt, dfGridNorthPoleLat, dfGridNorthPoleLon, dfNorthPoleGridLon,
8396
0
        nullptr, 0);
8397
0
    auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
8398
0
    d->setPjCRS(proj_create_derived_geographic_crs(ctxt, pszCRSName,
8399
0
                                                   d->m_pj_crs, conv, cs));
8400
0
    proj_destroy(conv);
8401
0
    proj_destroy(cs);
8402
0
    return OGRERR_NONE;
8403
#else
8404
    (void)pszCRSName;
8405
    SetProjection("Rotated_pole");
8406
    SetExtension(
8407
        "PROJCS", "PROJ4",
8408
        CPLSPrintf("+proj=ob_tran +o_proj=longlat +lon_0=%.17g +o_lon_p=%.17g "
8409
                   "+o_lat_p=%.17g +a=%.17g +b=%.17g "
8410
                   "+to_meter=0.0174532925199433 "
8411
                   "+wktext",
8412
                   180.0 + dfGridNorthPoleLon, dfNorthPoleGridLon,
8413
                   dfGridNorthPoleLat, GetSemiMajor(nullptr),
8414
                   GetSemiMinor(nullptr)));
8415
    return OGRERR_NONE;
8416
#endif
8417
0
}
8418
8419
/************************************************************************/
8420
/*                            SetAuthority()                            */
8421
/************************************************************************/
8422
8423
/**
8424
 * \brief Set the authority for a node.
8425
 *
8426
 * This method is the same as the C function OSRSetAuthority().
8427
 *
8428
 * @param pszTargetKey the partial or complete path to the node to
8429
 * set an authority on.  i.e. "PROJCS", "GEOGCS" or "GEOGCS|UNIT".
8430
 *
8431
 * @param pszAuthority authority name, such as "EPSG".
8432
 *
8433
 * @param nCode code for value with this authority.
8434
 *
8435
 * @return OGRERR_NONE on success.
8436
 */
8437
8438
OGRErr OGRSpatialReference::SetAuthority(const char *pszTargetKey,
8439
                                         const char *pszAuthority, int nCode)
8440
8441
0
{
8442
0
    TAKE_OPTIONAL_LOCK();
8443
8444
0
    d->refreshProjObj();
8445
0
    pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
8446
8447
0
    if (pszTargetKey == nullptr)
8448
0
    {
8449
0
        if (!d->m_pj_crs)
8450
0
            return OGRERR_FAILURE;
8451
0
        CPLString osCode;
8452
0
        osCode.Printf("%d", nCode);
8453
0
        d->demoteFromBoundCRS();
8454
0
        d->setPjCRS(proj_alter_id(d->getPROJContext(), d->m_pj_crs,
8455
0
                                  pszAuthority, osCode.c_str()));
8456
0
        d->undoDemoteFromBoundCRS();
8457
0
        return OGRERR_NONE;
8458
0
    }
8459
8460
0
    d->demoteFromBoundCRS();
8461
0
    if (d->m_pjType == PJ_TYPE_PROJECTED_CRS && EQUAL(pszTargetKey, "GEOGCS"))
8462
0
    {
8463
0
        CPLString osCode;
8464
0
        osCode.Printf("%d", nCode);
8465
0
        auto newGeogCRS =
8466
0
            proj_alter_id(d->getPROJContext(), d->getGeodBaseCRS(),
8467
0
                          pszAuthority, osCode.c_str());
8468
8469
0
        auto conv =
8470
0
            proj_crs_get_coordoperation(d->getPROJContext(), d->m_pj_crs);
8471
8472
0
        auto projCRS = proj_create_projected_crs(
8473
0
            d->getPROJContext(), d->getProjCRSName(), newGeogCRS, conv,
8474
0
            d->getProjCRSCoordSys());
8475
8476
        // Preserve existing id on the PROJCRS
8477
0
        const char *pszProjCRSAuthName = proj_get_id_auth_name(d->m_pj_crs, 0);
8478
0
        const char *pszProjCRSCode = proj_get_id_code(d->m_pj_crs, 0);
8479
0
        if (pszProjCRSAuthName && pszProjCRSCode)
8480
0
        {
8481
0
            auto projCRSWithId =
8482
0
                proj_alter_id(d->getPROJContext(), projCRS, pszProjCRSAuthName,
8483
0
                              pszProjCRSCode);
8484
0
            proj_destroy(projCRS);
8485
0
            projCRS = projCRSWithId;
8486
0
        }
8487
8488
0
        proj_destroy(newGeogCRS);
8489
0
        proj_destroy(conv);
8490
8491
0
        d->setPjCRS(projCRS);
8492
0
        d->undoDemoteFromBoundCRS();
8493
0
        return OGRERR_NONE;
8494
0
    }
8495
0
    d->undoDemoteFromBoundCRS();
8496
8497
    /* -------------------------------------------------------------------- */
8498
    /*      Find the node below which the authority should be put.          */
8499
    /* -------------------------------------------------------------------- */
8500
0
    OGR_SRSNode *poNode = GetAttrNode(pszTargetKey);
8501
8502
0
    if (poNode == nullptr)
8503
0
        return OGRERR_FAILURE;
8504
8505
    /* -------------------------------------------------------------------- */
8506
    /*      If there is an existing AUTHORITY child blow it away before     */
8507
    /*      trying to set a new one.                                        */
8508
    /* -------------------------------------------------------------------- */
8509
0
    int iOldChild = poNode->FindChild("AUTHORITY");
8510
0
    if (iOldChild != -1)
8511
0
        poNode->DestroyChild(iOldChild);
8512
8513
    /* -------------------------------------------------------------------- */
8514
    /*      Create a new authority node.                                    */
8515
    /* -------------------------------------------------------------------- */
8516
0
    char szCode[32] = {};
8517
8518
0
    snprintf(szCode, sizeof(szCode), "%d", nCode);
8519
8520
0
    OGR_SRSNode *poAuthNode = new OGR_SRSNode("AUTHORITY");
8521
0
    poAuthNode->AddChild(new OGR_SRSNode(pszAuthority));
8522
0
    poAuthNode->AddChild(new OGR_SRSNode(szCode));
8523
8524
0
    poNode->AddChild(poAuthNode);
8525
8526
0
    return OGRERR_NONE;
8527
0
}
8528
8529
/************************************************************************/
8530
/*                          OSRSetAuthority()                           */
8531
/************************************************************************/
8532
8533
/**
8534
 * \brief Set the authority for a node.
8535
 *
8536
 * This function is the same as OGRSpatialReference::SetAuthority().
8537
 */
8538
OGRErr OSRSetAuthority(OGRSpatialReferenceH hSRS, const char *pszTargetKey,
8539
                       const char *pszAuthority, int nCode)
8540
8541
0
{
8542
0
    VALIDATE_POINTER1(hSRS, "OSRSetAuthority", OGRERR_FAILURE);
8543
8544
0
    return ToPointer(hSRS)->SetAuthority(pszTargetKey, pszAuthority, nCode);
8545
0
}
8546
8547
/************************************************************************/
8548
/*                          GetAuthorityCode()                          */
8549
/************************************************************************/
8550
8551
/**
8552
 * \brief Get the authority code for a node.
8553
 *
8554
 * This method is used to query an AUTHORITY[] node from within the
8555
 * WKT tree, and fetch the code value.
8556
 *
8557
 * While in theory values may be non-numeric, for the EPSG authority all
8558
 * code values should be integral.
8559
 *
8560
 * This method is the same as the C function OSRGetAuthorityCode().
8561
 *
8562
 * @param pszTargetKey the partial or complete path to the node to
8563
 * get an authority from.  i.e. "PROJCS", "GEOGCS", "GEOGCS|UNIT" or NULL to
8564
 * search for an authority node on the root element.
8565
 *
8566
 * @return value code from authority node, or NULL on failure.  The value
8567
 * returned is internal and should not be freed or modified.
8568
 */
8569
8570
const char *
8571
OGRSpatialReference::GetAuthorityCode(const char *pszTargetKey) const
8572
8573
0
{
8574
0
    TAKE_OPTIONAL_LOCK();
8575
8576
0
    d->refreshProjObj();
8577
0
    const char *pszInputTargetKey = pszTargetKey;
8578
0
    pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
8579
0
    if (pszTargetKey == nullptr)
8580
0
    {
8581
0
        if (!d->m_pj_crs)
8582
0
        {
8583
0
            return nullptr;
8584
0
        }
8585
0
        d->demoteFromBoundCRS();
8586
0
        auto ret = proj_get_id_code(d->m_pj_crs, 0);
8587
0
        if (ret == nullptr && d->m_pjType == PJ_TYPE_PROJECTED_CRS)
8588
0
        {
8589
0
            auto ctxt = d->getPROJContext();
8590
0
            auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
8591
0
            if (cs)
8592
0
            {
8593
0
                const int axisCount = proj_cs_get_axis_count(ctxt, cs);
8594
0
                proj_destroy(cs);
8595
0
                if (axisCount == 3)
8596
0
                {
8597
                    // This might come from a COMPD_CS with a VERT_DATUM type =
8598
                    // 2002 in which case, using the WKT1 representation will
8599
                    // enable us to recover the EPSG code.
8600
0
                    pszTargetKey = pszInputTargetKey;
8601
0
                }
8602
0
            }
8603
0
        }
8604
0
        d->undoDemoteFromBoundCRS();
8605
0
        if (ret != nullptr || pszTargetKey == nullptr)
8606
0
        {
8607
0
            return ret;
8608
0
        }
8609
0
    }
8610
8611
    // Special key for that context
8612
0
    else if (EQUAL(pszTargetKey, "HORIZCRS") &&
8613
0
             d->m_pjType == PJ_TYPE_COMPOUND_CRS)
8614
0
    {
8615
0
        auto ctxt = d->getPROJContext();
8616
0
        auto crs = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
8617
0
        if (crs)
8618
0
        {
8619
0
            const char *ret = proj_get_id_code(crs, 0);
8620
0
            if (ret)
8621
0
                ret = CPLSPrintf("%s", ret);
8622
0
            proj_destroy(crs);
8623
0
            return ret;
8624
0
        }
8625
0
    }
8626
0
    else if (EQUAL(pszTargetKey, "VERTCRS") &&
8627
0
             d->m_pjType == PJ_TYPE_COMPOUND_CRS)
8628
0
    {
8629
0
        auto ctxt = d->getPROJContext();
8630
0
        auto crs = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 1);
8631
0
        if (crs)
8632
0
        {
8633
0
            const char *ret = proj_get_id_code(crs, 0);
8634
0
            if (ret)
8635
0
                ret = CPLSPrintf("%s", ret);
8636
0
            proj_destroy(crs);
8637
0
            return ret;
8638
0
        }
8639
0
    }
8640
8641
    /* -------------------------------------------------------------------- */
8642
    /*      Find the node below which the authority should be put.          */
8643
    /* -------------------------------------------------------------------- */
8644
0
    const OGR_SRSNode *poNode = GetAttrNode(pszTargetKey);
8645
8646
0
    if (poNode == nullptr)
8647
0
        return nullptr;
8648
8649
    /* -------------------------------------------------------------------- */
8650
    /*      Fetch AUTHORITY child if there is one.                          */
8651
    /* -------------------------------------------------------------------- */
8652
0
    if (poNode->FindChild("AUTHORITY") == -1)
8653
0
        return nullptr;
8654
8655
0
    poNode = poNode->GetChild(poNode->FindChild("AUTHORITY"));
8656
8657
    /* -------------------------------------------------------------------- */
8658
    /*      Create a new authority node.                                    */
8659
    /* -------------------------------------------------------------------- */
8660
0
    if (poNode->GetChildCount() < 2)
8661
0
        return nullptr;
8662
8663
0
    return poNode->GetChild(1)->GetValue();
8664
0
}
8665
8666
/************************************************************************/
8667
/*                          OSRGetAuthorityCode()                       */
8668
/************************************************************************/
8669
8670
/**
8671
 * \brief Get the authority code for a node.
8672
 *
8673
 * This function is the same as OGRSpatialReference::GetAuthorityCode().
8674
 */
8675
const char *OSRGetAuthorityCode(OGRSpatialReferenceH hSRS,
8676
                                const char *pszTargetKey)
8677
8678
0
{
8679
0
    VALIDATE_POINTER1(hSRS, "OSRGetAuthorityCode", nullptr);
8680
8681
0
    return ToPointer(hSRS)->GetAuthorityCode(pszTargetKey);
8682
0
}
8683
8684
/************************************************************************/
8685
/*                          GetAuthorityName()                          */
8686
/************************************************************************/
8687
8688
/**
8689
 * \brief Get the authority name for a node.
8690
 *
8691
 * This method is used to query an AUTHORITY[] node from within the
8692
 * WKT tree, and fetch the authority name value.
8693
 *
8694
 * The most common authority is "EPSG".
8695
 *
8696
 * This method is the same as the C function OSRGetAuthorityName().
8697
 *
8698
 * @param pszTargetKey the partial or complete path to the node to
8699
 * get an authority from.  i.e. "PROJCS", "GEOGCS", "GEOGCS|UNIT" or NULL to
8700
 * search for an authority node on the root element.
8701
 *
8702
 * @return value code from authority node, or NULL on failure. The value
8703
 * returned is internal and should not be freed or modified.
8704
 */
8705
8706
const char *
8707
OGRSpatialReference::GetAuthorityName(const char *pszTargetKey) const
8708
8709
0
{
8710
0
    TAKE_OPTIONAL_LOCK();
8711
8712
0
    d->refreshProjObj();
8713
0
    const char *pszInputTargetKey = pszTargetKey;
8714
0
    pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
8715
0
    if (pszTargetKey == nullptr)
8716
0
    {
8717
0
        if (!d->m_pj_crs)
8718
0
        {
8719
0
            return nullptr;
8720
0
        }
8721
0
        d->demoteFromBoundCRS();
8722
0
        auto ret = proj_get_id_auth_name(d->m_pj_crs, 0);
8723
0
        if (ret == nullptr && d->m_pjType == PJ_TYPE_PROJECTED_CRS)
8724
0
        {
8725
0
            auto ctxt = d->getPROJContext();
8726
0
            auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
8727
0
            if (cs)
8728
0
            {
8729
0
                const int axisCount = proj_cs_get_axis_count(ctxt, cs);
8730
0
                proj_destroy(cs);
8731
0
                if (axisCount == 3)
8732
0
                {
8733
                    // This might come from a COMPD_CS with a VERT_DATUM type =
8734
                    // 2002 in which case, using the WKT1 representation will
8735
                    // enable us to recover the EPSG code.
8736
0
                    pszTargetKey = pszInputTargetKey;
8737
0
                }
8738
0
            }
8739
0
        }
8740
0
        d->undoDemoteFromBoundCRS();
8741
0
        if (ret != nullptr || pszTargetKey == nullptr)
8742
0
        {
8743
0
            return ret;
8744
0
        }
8745
0
    }
8746
8747
    // Special key for that context
8748
0
    else if (EQUAL(pszTargetKey, "HORIZCRS") &&
8749
0
             d->m_pjType == PJ_TYPE_COMPOUND_CRS)
8750
0
    {
8751
0
        auto ctxt = d->getPROJContext();
8752
0
        auto crs = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
8753
0
        if (crs)
8754
0
        {
8755
0
            const char *ret = proj_get_id_auth_name(crs, 0);
8756
0
            if (ret)
8757
0
                ret = CPLSPrintf("%s", ret);
8758
0
            proj_destroy(crs);
8759
0
            return ret;
8760
0
        }
8761
0
    }
8762
0
    else if (EQUAL(pszTargetKey, "VERTCRS") &&
8763
0
             d->m_pjType == PJ_TYPE_COMPOUND_CRS)
8764
0
    {
8765
0
        auto ctxt = d->getPROJContext();
8766
0
        auto crs = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 1);
8767
0
        if (crs)
8768
0
        {
8769
0
            const char *ret = proj_get_id_auth_name(crs, 0);
8770
0
            if (ret)
8771
0
                ret = CPLSPrintf("%s", ret);
8772
0
            proj_destroy(crs);
8773
0
            return ret;
8774
0
        }
8775
0
    }
8776
8777
    /* -------------------------------------------------------------------- */
8778
    /*      Find the node below which the authority should be put.          */
8779
    /* -------------------------------------------------------------------- */
8780
0
    const OGR_SRSNode *poNode = GetAttrNode(pszTargetKey);
8781
8782
0
    if (poNode == nullptr)
8783
0
        return nullptr;
8784
8785
    /* -------------------------------------------------------------------- */
8786
    /*      Fetch AUTHORITY child if there is one.                          */
8787
    /* -------------------------------------------------------------------- */
8788
0
    if (poNode->FindChild("AUTHORITY") == -1)
8789
0
        return nullptr;
8790
8791
0
    poNode = poNode->GetChild(poNode->FindChild("AUTHORITY"));
8792
8793
    /* -------------------------------------------------------------------- */
8794
    /*      Create a new authority node.                                    */
8795
    /* -------------------------------------------------------------------- */
8796
0
    if (poNode->GetChildCount() < 2)
8797
0
        return nullptr;
8798
8799
0
    return poNode->GetChild(0)->GetValue();
8800
0
}
8801
8802
/************************************************************************/
8803
/*                        OSRGetAuthorityName()                         */
8804
/************************************************************************/
8805
8806
/**
8807
 * \brief Get the authority name for a node.
8808
 *
8809
 * This function is the same as OGRSpatialReference::GetAuthorityName().
8810
 */
8811
const char *OSRGetAuthorityName(OGRSpatialReferenceH hSRS,
8812
                                const char *pszTargetKey)
8813
8814
0
{
8815
0
    VALIDATE_POINTER1(hSRS, "OSRGetAuthorityName", nullptr);
8816
8817
0
    return ToPointer(hSRS)->GetAuthorityName(pszTargetKey);
8818
0
}
8819
8820
/************************************************************************/
8821
/*                          GetOGCURN()                                 */
8822
/************************************************************************/
8823
8824
/**
8825
 * \brief Get a OGC URN string describing the CRS, when possible
8826
 *
8827
 * This method assumes that the CRS has a top-level identifier, or is
8828
 * a compound CRS whose horizontal and vertical parts have a top-level
8829
 * identifier.
8830
 *
8831
 * @return a string to free with CPLFree(), or nullptr when no result can be
8832
 * generated
8833
 *
8834
 * @since GDAL 3.5
8835
 */
8836
8837
char *OGRSpatialReference::GetOGCURN() const
8838
8839
0
{
8840
0
    TAKE_OPTIONAL_LOCK();
8841
8842
0
    const char *pszAuthName = GetAuthorityName(nullptr);
8843
0
    const char *pszAuthCode = GetAuthorityCode(nullptr);
8844
0
    if (pszAuthName && pszAuthCode)
8845
0
        return CPLStrdup(
8846
0
            CPLSPrintf("urn:ogc:def:crs:%s::%s", pszAuthName, pszAuthCode));
8847
0
    if (d->m_pjType != PJ_TYPE_COMPOUND_CRS)
8848
0
        return nullptr;
8849
0
    auto horizCRS = proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
8850
0
    auto vertCRS = proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 1);
8851
0
    char *pszRet = nullptr;
8852
0
    if (horizCRS && vertCRS)
8853
0
    {
8854
0
        auto horizAuthName = proj_get_id_auth_name(horizCRS, 0);
8855
0
        auto horizAuthCode = proj_get_id_code(horizCRS, 0);
8856
0
        auto vertAuthName = proj_get_id_auth_name(vertCRS, 0);
8857
0
        auto vertAuthCode = proj_get_id_code(vertCRS, 0);
8858
0
        if (horizAuthName && horizAuthCode && vertAuthName && vertAuthCode)
8859
0
        {
8860
0
            pszRet = CPLStrdup(CPLSPrintf(
8861
0
                "urn:ogc:def:crs,crs:%s::%s,crs:%s::%s", horizAuthName,
8862
0
                horizAuthCode, vertAuthName, vertAuthCode));
8863
0
        }
8864
0
    }
8865
0
    proj_destroy(horizCRS);
8866
0
    proj_destroy(vertCRS);
8867
0
    return pszRet;
8868
0
}
8869
8870
/************************************************************************/
8871
/*                           StripVertical()                            */
8872
/************************************************************************/
8873
8874
/**
8875
 * \brief Convert a compound cs into a horizontal CS.
8876
 *
8877
 * If this SRS is of type COMPD_CS[] then the vertical CS and the root COMPD_CS
8878
 * nodes are stripped resulting and only the horizontal coordinate system
8879
 * portion remains (normally PROJCS, GEOGCS or LOCAL_CS).
8880
 *
8881
 * If this is not a compound coordinate system then nothing is changed.
8882
 *
8883
 * This method is the same as the C function OSRStripVertical().
8884
 *
8885
 * @since OGR 1.8.0
8886
 */
8887
8888
OGRErr OGRSpatialReference::StripVertical()
8889
8890
0
{
8891
0
    TAKE_OPTIONAL_LOCK();
8892
8893
0
    d->refreshProjObj();
8894
0
    d->demoteFromBoundCRS();
8895
0
    if (!d->m_pj_crs || d->m_pjType != PJ_TYPE_COMPOUND_CRS)
8896
0
    {
8897
0
        d->undoDemoteFromBoundCRS();
8898
0
        return OGRERR_NONE;
8899
0
    }
8900
0
    auto horizCRS = proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
8901
0
    if (!horizCRS)
8902
0
    {
8903
0
        d->undoDemoteFromBoundCRS();
8904
0
        return OGRERR_FAILURE;
8905
0
    }
8906
8907
0
    bool reuseExistingBoundCRS = false;
8908
0
    if (d->m_pj_bound_crs_target)
8909
0
    {
8910
0
        auto type = proj_get_type(d->m_pj_bound_crs_target);
8911
0
        reuseExistingBoundCRS = type == PJ_TYPE_GEOCENTRIC_CRS ||
8912
0
                                type == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
8913
0
                                type == PJ_TYPE_GEOGRAPHIC_3D_CRS;
8914
0
    }
8915
8916
0
    if (reuseExistingBoundCRS)
8917
0
    {
8918
0
        auto newBoundCRS = proj_crs_create_bound_crs(
8919
0
            d->getPROJContext(), horizCRS, d->m_pj_bound_crs_target,
8920
0
            d->m_pj_bound_crs_co);
8921
0
        proj_destroy(horizCRS);
8922
0
        d->undoDemoteFromBoundCRS();
8923
0
        d->setPjCRS(newBoundCRS);
8924
0
    }
8925
0
    else
8926
0
    {
8927
0
        d->undoDemoteFromBoundCRS();
8928
0
        d->setPjCRS(horizCRS);
8929
0
    }
8930
8931
0
    return OGRERR_NONE;
8932
0
}
8933
8934
/************************************************************************/
8935
/*                            OSRStripVertical()                             */
8936
/************************************************************************/
8937
/**
8938
 * \brief Convert a compound cs into a horizontal CS.
8939
 *
8940
 * This function is the same as the C++ method
8941
 * OGRSpatialReference::StripVertical().
8942
 */
8943
OGRErr OSRStripVertical(OGRSpatialReferenceH hSRS)
8944
8945
0
{
8946
0
    VALIDATE_POINTER1(hSRS, "OSRStripVertical", OGRERR_FAILURE);
8947
8948
0
    return OGRSpatialReference::FromHandle(hSRS)->StripVertical();
8949
0
}
8950
8951
/************************************************************************/
8952
/*                   StripTOWGS84IfKnownDatumAndAllowed()               */
8953
/************************************************************************/
8954
8955
/**
8956
 * \brief Remove TOWGS84 information if the CRS has a known horizontal datum
8957
 *        and this is allowed by the user.
8958
 *
8959
 * The default behavior is to remove TOWGS84 information if the CRS has a
8960
 * known horizontal datum. This can be disabled by setting the
8961
 * OSR_STRIP_TOWGS84 configuration option to NO.
8962
 *
8963
 * @return true if TOWGS84 has been removed.
8964
 * @since OGR 3.1.0
8965
 */
8966
8967
bool OGRSpatialReference::StripTOWGS84IfKnownDatumAndAllowed()
8968
0
{
8969
0
    if (CPLTestBool(CPLGetConfigOption("OSR_STRIP_TOWGS84", "YES")))
8970
0
    {
8971
0
        if (StripTOWGS84IfKnownDatum())
8972
0
        {
8973
0
            CPLDebug("OSR", "TOWGS84 information has been removed. "
8974
0
                            "It can be kept by setting the OSR_STRIP_TOWGS84 "
8975
0
                            "configuration option to NO");
8976
0
            return true;
8977
0
        }
8978
0
    }
8979
0
    return false;
8980
0
}
8981
8982
/************************************************************************/
8983
/*                      StripTOWGS84IfKnownDatum()                      */
8984
/************************************************************************/
8985
8986
/**
8987
 * \brief Remove TOWGS84 information if the CRS has a known horizontal datum
8988
 *
8989
 * @return true if TOWGS84 has been removed.
8990
 * @since OGR 3.1.0
8991
 */
8992
8993
bool OGRSpatialReference::StripTOWGS84IfKnownDatum()
8994
8995
0
{
8996
0
    TAKE_OPTIONAL_LOCK();
8997
8998
0
    d->refreshProjObj();
8999
0
    if (!d->m_pj_crs || d->m_pjType != PJ_TYPE_BOUND_CRS)
9000
0
    {
9001
0
        return false;
9002
0
    }
9003
0
    auto ctxt = d->getPROJContext();
9004
0
    auto baseCRS = proj_get_source_crs(ctxt, d->m_pj_crs);
9005
0
    if (proj_get_type(baseCRS) == PJ_TYPE_COMPOUND_CRS)
9006
0
    {
9007
0
        proj_destroy(baseCRS);
9008
0
        return false;
9009
0
    }
9010
9011
    // Known base CRS code ? Return base CRS
9012
0
    const char *pszCode = proj_get_id_code(baseCRS, 0);
9013
0
    if (pszCode)
9014
0
    {
9015
0
        d->setPjCRS(baseCRS);
9016
0
        return true;
9017
0
    }
9018
9019
0
    auto datum = proj_crs_get_datum(ctxt, baseCRS);
9020
0
#if PROJ_VERSION_MAJOR > 7 ||                                                  \
9021
0
    (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
9022
0
    if (datum == nullptr)
9023
0
    {
9024
0
        datum = proj_crs_get_datum_ensemble(ctxt, baseCRS);
9025
0
    }
9026
0
#endif
9027
0
    if (!datum)
9028
0
    {
9029
0
        proj_destroy(baseCRS);
9030
0
        return false;
9031
0
    }
9032
9033
    // Known datum code ? Return base CRS
9034
0
    pszCode = proj_get_id_code(datum, 0);
9035
0
    if (pszCode)
9036
0
    {
9037
0
        proj_destroy(datum);
9038
0
        d->setPjCRS(baseCRS);
9039
0
        return true;
9040
0
    }
9041
9042
0
    const char *name = proj_get_name(datum);
9043
0
    if (EQUAL(name, "unknown"))
9044
0
    {
9045
0
        proj_destroy(datum);
9046
0
        proj_destroy(baseCRS);
9047
0
        return false;
9048
0
    }
9049
0
    const PJ_TYPE type = PJ_TYPE_GEODETIC_REFERENCE_FRAME;
9050
0
    PJ_OBJ_LIST *list =
9051
0
        proj_create_from_name(ctxt, nullptr, name, &type, 1, false, 1, nullptr);
9052
9053
0
    bool knownDatumName = false;
9054
0
    if (list)
9055
0
    {
9056
0
        if (proj_list_get_count(list) == 1)
9057
0
        {
9058
0
            knownDatumName = true;
9059
0
        }
9060
0
        proj_list_destroy(list);
9061
0
    }
9062
9063
0
    proj_destroy(datum);
9064
0
    if (knownDatumName)
9065
0
    {
9066
0
        d->setPjCRS(baseCRS);
9067
0
        return true;
9068
0
    }
9069
0
    proj_destroy(baseCRS);
9070
0
    return false;
9071
0
}
9072
9073
/************************************************************************/
9074
/*                             IsCompound()                             */
9075
/************************************************************************/
9076
9077
/**
9078
 * \brief Check if coordinate system is compound.
9079
 *
9080
 * This method is the same as the C function OSRIsCompound().
9081
 *
9082
 * @return TRUE if this is rooted with a COMPD_CS node.
9083
 */
9084
9085
int OGRSpatialReference::IsCompound() const
9086
9087
0
{
9088
0
    TAKE_OPTIONAL_LOCK();
9089
9090
0
    d->refreshProjObj();
9091
0
    d->demoteFromBoundCRS();
9092
0
    bool isCompound = d->m_pjType == PJ_TYPE_COMPOUND_CRS;
9093
0
    d->undoDemoteFromBoundCRS();
9094
0
    return isCompound;
9095
0
}
9096
9097
/************************************************************************/
9098
/*                           OSRIsCompound()                            */
9099
/************************************************************************/
9100
9101
/**
9102
 * \brief Check if the coordinate system is compound.
9103
 *
9104
 * This function is the same as OGRSpatialReference::IsCompound().
9105
 */
9106
int OSRIsCompound(OGRSpatialReferenceH hSRS)
9107
9108
0
{
9109
0
    VALIDATE_POINTER1(hSRS, "OSRIsCompound", 0);
9110
9111
0
    return ToPointer(hSRS)->IsCompound();
9112
0
}
9113
9114
/************************************************************************/
9115
/*                            IsProjected()                             */
9116
/************************************************************************/
9117
9118
/**
9119
 * \brief Check if projected coordinate system.
9120
 *
9121
 * This method is the same as the C function OSRIsProjected().
9122
 *
9123
 * @return TRUE if this contains a PROJCS node indicating a it is a
9124
 * projected coordinate system. Also if it is a CompoundCRS made of a
9125
 * ProjectedCRS
9126
 */
9127
9128
int OGRSpatialReference::IsProjected() const
9129
9130
0
{
9131
0
    TAKE_OPTIONAL_LOCK();
9132
9133
0
    d->refreshProjObj();
9134
0
    d->demoteFromBoundCRS();
9135
0
    bool isProjected = d->m_pjType == PJ_TYPE_PROJECTED_CRS;
9136
0
    if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
9137
0
    {
9138
0
        auto horizCRS =
9139
0
            proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
9140
0
        if (horizCRS)
9141
0
        {
9142
0
            auto horizCRSType = proj_get_type(horizCRS);
9143
0
            isProjected = horizCRSType == PJ_TYPE_PROJECTED_CRS;
9144
0
            if (horizCRSType == PJ_TYPE_BOUND_CRS)
9145
0
            {
9146
0
                auto base = proj_get_source_crs(d->getPROJContext(), horizCRS);
9147
0
                if (base)
9148
0
                {
9149
0
                    isProjected = proj_get_type(base) == PJ_TYPE_PROJECTED_CRS;
9150
0
                    proj_destroy(base);
9151
0
                }
9152
0
            }
9153
0
            proj_destroy(horizCRS);
9154
0
        }
9155
0
    }
9156
0
    d->undoDemoteFromBoundCRS();
9157
0
    return isProjected;
9158
0
}
9159
9160
/************************************************************************/
9161
/*                           OSRIsProjected()                           */
9162
/************************************************************************/
9163
/**
9164
 * \brief Check if projected coordinate system.
9165
 *
9166
 * This function is the same as OGRSpatialReference::IsProjected().
9167
 */
9168
int OSRIsProjected(OGRSpatialReferenceH hSRS)
9169
9170
0
{
9171
0
    VALIDATE_POINTER1(hSRS, "OSRIsProjected", 0);
9172
9173
0
    return ToPointer(hSRS)->IsProjected();
9174
0
}
9175
9176
/************************************************************************/
9177
/*                            IsGeocentric()                            */
9178
/************************************************************************/
9179
9180
/**
9181
 * \brief Check if geocentric coordinate system.
9182
 *
9183
 * This method is the same as the C function OSRIsGeocentric().
9184
 *
9185
 * @return TRUE if this contains a GEOCCS node indicating a it is a
9186
 * geocentric coordinate system.
9187
 *
9188
 * @since OGR 1.9.0
9189
 */
9190
9191
int OGRSpatialReference::IsGeocentric() const
9192
9193
0
{
9194
0
    TAKE_OPTIONAL_LOCK();
9195
9196
0
    d->refreshProjObj();
9197
0
    d->demoteFromBoundCRS();
9198
0
    bool isGeocentric = d->m_pjType == PJ_TYPE_GEOCENTRIC_CRS;
9199
0
    d->undoDemoteFromBoundCRS();
9200
0
    return isGeocentric;
9201
0
}
9202
9203
/************************************************************************/
9204
/*                           OSRIsGeocentric()                          */
9205
/************************************************************************/
9206
/**
9207
 * \brief Check if geocentric coordinate system.
9208
 *
9209
 * This function is the same as OGRSpatialReference::IsGeocentric().
9210
 *
9211
 * @since OGR 1.9.0
9212
 */
9213
int OSRIsGeocentric(OGRSpatialReferenceH hSRS)
9214
9215
0
{
9216
0
    VALIDATE_POINTER1(hSRS, "OSRIsGeocentric", 0);
9217
9218
0
    return ToPointer(hSRS)->IsGeocentric();
9219
0
}
9220
9221
/************************************************************************/
9222
/*                            IsEmpty()                                 */
9223
/************************************************************************/
9224
9225
/**
9226
 * \brief Return if the SRS is not set.
9227
 */
9228
9229
bool OGRSpatialReference::IsEmpty() const
9230
0
{
9231
0
    TAKE_OPTIONAL_LOCK();
9232
9233
0
    d->refreshProjObj();
9234
0
    return d->m_pj_crs == nullptr;
9235
0
}
9236
9237
/************************************************************************/
9238
/*                            IsGeographic()                            */
9239
/************************************************************************/
9240
9241
/**
9242
 * \brief Check if geographic coordinate system.
9243
 *
9244
 * This method is the same as the C function OSRIsGeographic().
9245
 *
9246
 * @return TRUE if this spatial reference is geographic ... that is the
9247
 * root is a GEOGCS node. Also if it is a CompoundCRS made of a
9248
 * GeographicCRS
9249
 */
9250
9251
int OGRSpatialReference::IsGeographic() const
9252
9253
0
{
9254
0
    TAKE_OPTIONAL_LOCK();
9255
9256
0
    d->refreshProjObj();
9257
0
    d->demoteFromBoundCRS();
9258
0
    bool isGeog = d->m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
9259
0
                  d->m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS;
9260
0
    if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
9261
0
    {
9262
0
        auto horizCRS =
9263
0
            proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
9264
0
        if (horizCRS)
9265
0
        {
9266
0
            auto horizCRSType = proj_get_type(horizCRS);
9267
0
            isGeog = horizCRSType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
9268
0
                     horizCRSType == PJ_TYPE_GEOGRAPHIC_3D_CRS;
9269
0
            if (horizCRSType == PJ_TYPE_BOUND_CRS)
9270
0
            {
9271
0
                auto base = proj_get_source_crs(d->getPROJContext(), horizCRS);
9272
0
                if (base)
9273
0
                {
9274
0
                    horizCRSType = proj_get_type(base);
9275
0
                    isGeog = horizCRSType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
9276
0
                             horizCRSType == PJ_TYPE_GEOGRAPHIC_3D_CRS;
9277
0
                    proj_destroy(base);
9278
0
                }
9279
0
            }
9280
0
            proj_destroy(horizCRS);
9281
0
        }
9282
0
    }
9283
0
    d->undoDemoteFromBoundCRS();
9284
0
    return isGeog;
9285
0
}
9286
9287
/************************************************************************/
9288
/*                          OSRIsGeographic()                           */
9289
/************************************************************************/
9290
/**
9291
 * \brief Check if geographic coordinate system.
9292
 *
9293
 * This function is the same as OGRSpatialReference::IsGeographic().
9294
 */
9295
int OSRIsGeographic(OGRSpatialReferenceH hSRS)
9296
9297
0
{
9298
0
    VALIDATE_POINTER1(hSRS, "OSRIsGeographic", 0);
9299
9300
0
    return ToPointer(hSRS)->IsGeographic();
9301
0
}
9302
9303
/************************************************************************/
9304
/*                      IsDerivedGeographic()                           */
9305
/************************************************************************/
9306
9307
/**
9308
 * \brief Check if the CRS is a derived geographic coordinate system.
9309
 * (for example a rotated long/lat grid)
9310
 *
9311
 * This method is the same as the C function OSRIsDerivedGeographic().
9312
 *
9313
 * @since GDAL 3.1.0 and PROJ 6.3.0
9314
 */
9315
9316
int OGRSpatialReference::IsDerivedGeographic() const
9317
9318
0
{
9319
0
    TAKE_OPTIONAL_LOCK();
9320
9321
0
    d->refreshProjObj();
9322
0
    d->demoteFromBoundCRS();
9323
0
    const bool isGeog = d->m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
9324
0
                        d->m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS;
9325
0
    const bool isDerivedGeographic =
9326
0
        isGeog && proj_is_derived_crs(d->getPROJContext(), d->m_pj_crs);
9327
0
    d->undoDemoteFromBoundCRS();
9328
0
    return isDerivedGeographic ? TRUE : FALSE;
9329
0
}
9330
9331
/************************************************************************/
9332
/*                      OSRIsDerivedGeographic()                        */
9333
/************************************************************************/
9334
/**
9335
 * \brief Check if the CRS is a derived geographic coordinate system.
9336
 * (for example a rotated long/lat grid)
9337
 *
9338
 * This function is the same as OGRSpatialReference::IsDerivedGeographic().
9339
 */
9340
int OSRIsDerivedGeographic(OGRSpatialReferenceH hSRS)
9341
9342
0
{
9343
0
    VALIDATE_POINTER1(hSRS, "OSRIsDerivedGeographic", 0);
9344
9345
0
    return ToPointer(hSRS)->IsDerivedGeographic();
9346
0
}
9347
9348
/************************************************************************/
9349
/*                      IsDerivedProjected()                            */
9350
/************************************************************************/
9351
9352
/**
9353
 * \brief Check if the CRS is a derived projected coordinate system.
9354
 *
9355
 * This method is the same as the C function OSRIsDerivedGeographic().
9356
 *
9357
 * @since GDAL 3.9.0 (and may only return non-zero starting with PROJ 9.2.0)
9358
 */
9359
9360
int OGRSpatialReference::IsDerivedProjected() const
9361
9362
0
{
9363
0
#if PROJ_AT_LEAST_VERSION(9, 2, 0)
9364
0
    TAKE_OPTIONAL_LOCK();
9365
0
    d->refreshProjObj();
9366
0
    d->demoteFromBoundCRS();
9367
0
    const bool isDerivedProjected =
9368
0
        d->m_pjType == PJ_TYPE_DERIVED_PROJECTED_CRS;
9369
0
    d->undoDemoteFromBoundCRS();
9370
0
    return isDerivedProjected ? TRUE : FALSE;
9371
#else
9372
    return FALSE;
9373
#endif
9374
0
}
9375
9376
/************************************************************************/
9377
/*                      OSRIsDerivedProjected()                         */
9378
/************************************************************************/
9379
/**
9380
 * \brief Check if the CRS is a derived projected coordinate system.
9381
 *
9382
 * This function is the same as OGRSpatialReference::IsDerivedProjected().
9383
 *
9384
 * @since GDAL 3.9.0 (and may only return non-zero starting with PROJ 9.2.0)
9385
 */
9386
int OSRIsDerivedProjected(OGRSpatialReferenceH hSRS)
9387
9388
0
{
9389
0
    VALIDATE_POINTER1(hSRS, "OSRIsDerivedProjected", 0);
9390
9391
0
    return ToPointer(hSRS)->IsDerivedProjected();
9392
0
}
9393
9394
/************************************************************************/
9395
/*                              IsLocal()                               */
9396
/************************************************************************/
9397
9398
/**
9399
 * \brief Check if local coordinate system.
9400
 *
9401
 * This method is the same as the C function OSRIsLocal().
9402
 *
9403
 * @return TRUE if this spatial reference is local ... that is the
9404
 * root is a LOCAL_CS node.
9405
 */
9406
9407
int OGRSpatialReference::IsLocal() const
9408
9409
0
{
9410
0
    TAKE_OPTIONAL_LOCK();
9411
0
    d->refreshProjObj();
9412
0
    return d->m_pjType == PJ_TYPE_ENGINEERING_CRS;
9413
0
}
9414
9415
/************************************************************************/
9416
/*                          OSRIsLocal()                                */
9417
/************************************************************************/
9418
/**
9419
 * \brief Check if local coordinate system.
9420
 *
9421
 * This function is the same as OGRSpatialReference::IsLocal().
9422
 */
9423
int OSRIsLocal(OGRSpatialReferenceH hSRS)
9424
9425
0
{
9426
0
    VALIDATE_POINTER1(hSRS, "OSRIsLocal", 0);
9427
9428
0
    return ToPointer(hSRS)->IsLocal();
9429
0
}
9430
9431
/************************************************************************/
9432
/*                            IsVertical()                              */
9433
/************************************************************************/
9434
9435
/**
9436
 * \brief Check if vertical coordinate system.
9437
 *
9438
 * This method is the same as the C function OSRIsVertical().
9439
 *
9440
 * @return TRUE if this contains a VERT_CS node indicating a it is a
9441
 * vertical coordinate system. Also if it is a CompoundCRS made of a
9442
 * VerticalCRS
9443
 *
9444
 * @since OGR 1.8.0
9445
 */
9446
9447
int OGRSpatialReference::IsVertical() const
9448
9449
0
{
9450
0
    TAKE_OPTIONAL_LOCK();
9451
0
    d->refreshProjObj();
9452
0
    d->demoteFromBoundCRS();
9453
0
    bool isVertical = d->m_pjType == PJ_TYPE_VERTICAL_CRS;
9454
0
    if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
9455
0
    {
9456
0
        auto vertCRS =
9457
0
            proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 1);
9458
0
        if (vertCRS)
9459
0
        {
9460
0
            const auto vertCRSType = proj_get_type(vertCRS);
9461
0
            isVertical = vertCRSType == PJ_TYPE_VERTICAL_CRS;
9462
0
            if (vertCRSType == PJ_TYPE_BOUND_CRS)
9463
0
            {
9464
0
                auto base = proj_get_source_crs(d->getPROJContext(), vertCRS);
9465
0
                if (base)
9466
0
                {
9467
0
                    isVertical = proj_get_type(base) == PJ_TYPE_VERTICAL_CRS;
9468
0
                    proj_destroy(base);
9469
0
                }
9470
0
            }
9471
0
            proj_destroy(vertCRS);
9472
0
        }
9473
0
    }
9474
0
    d->undoDemoteFromBoundCRS();
9475
0
    return isVertical;
9476
0
}
9477
9478
/************************************************************************/
9479
/*                           OSRIsVertical()                            */
9480
/************************************************************************/
9481
/**
9482
 * \brief Check if vertical coordinate system.
9483
 *
9484
 * This function is the same as OGRSpatialReference::IsVertical().
9485
 *
9486
 * @since OGR 1.8.0
9487
 */
9488
int OSRIsVertical(OGRSpatialReferenceH hSRS)
9489
9490
0
{
9491
0
    VALIDATE_POINTER1(hSRS, "OSRIsVertical", 0);
9492
9493
0
    return ToPointer(hSRS)->IsVertical();
9494
0
}
9495
9496
/************************************************************************/
9497
/*                            IsDynamic()                               */
9498
/************************************************************************/
9499
9500
/**
9501
 * \brief Check if a CRS is a dynamic CRS.
9502
 *
9503
 * A dynamic CRS relies on a dynamic datum, that is a datum that is not
9504
 * plate-fixed.
9505
 *
9506
 * This method is the same as the C function OSRIsDynamic().
9507
 *
9508
 * @return true if the CRS is dynamic
9509
 *
9510
 * @since OGR 3.4.0
9511
 *
9512
 * @see HasPointMotionOperation()
9513
 */
9514
9515
bool OGRSpatialReference::IsDynamic() const
9516
9517
0
{
9518
0
    TAKE_OPTIONAL_LOCK();
9519
0
    bool isDynamic = false;
9520
0
    d->refreshProjObj();
9521
0
    d->demoteFromBoundCRS();
9522
0
    auto ctxt = d->getPROJContext();
9523
0
    PJ *horiz = nullptr;
9524
0
    if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
9525
0
    {
9526
0
        horiz = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
9527
0
    }
9528
0
    else if (d->m_pj_crs)
9529
0
    {
9530
0
        horiz = proj_clone(ctxt, d->m_pj_crs);
9531
0
    }
9532
0
    if (horiz && proj_get_type(horiz) == PJ_TYPE_BOUND_CRS)
9533
0
    {
9534
0
        auto baseCRS = proj_get_source_crs(ctxt, horiz);
9535
0
        if (baseCRS)
9536
0
        {
9537
0
            proj_destroy(horiz);
9538
0
            horiz = baseCRS;
9539
0
        }
9540
0
    }
9541
0
    auto datum = horiz ? proj_crs_get_datum(ctxt, horiz) : nullptr;
9542
0
    if (datum)
9543
0
    {
9544
0
        const auto type = proj_get_type(datum);
9545
0
        isDynamic = type == PJ_TYPE_DYNAMIC_GEODETIC_REFERENCE_FRAME ||
9546
0
                    type == PJ_TYPE_DYNAMIC_VERTICAL_REFERENCE_FRAME;
9547
0
        if (!isDynamic)
9548
0
        {
9549
0
            const char *auth_name = proj_get_id_auth_name(datum, 0);
9550
0
            const char *code = proj_get_id_code(datum, 0);
9551
0
            if (auth_name && code && EQUAL(auth_name, "EPSG") &&
9552
0
                EQUAL(code, "6326"))
9553
0
            {
9554
0
                isDynamic = true;
9555
0
            }
9556
0
        }
9557
0
        proj_destroy(datum);
9558
0
    }
9559
0
#if PROJ_VERSION_MAJOR > 7 ||                                                  \
9560
0
    (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
9561
0
    else
9562
0
    {
9563
0
        auto ensemble =
9564
0
            horiz ? proj_crs_get_datum_ensemble(ctxt, horiz) : nullptr;
9565
0
        if (ensemble)
9566
0
        {
9567
0
            auto member = proj_datum_ensemble_get_member(ctxt, ensemble, 0);
9568
0
            if (member)
9569
0
            {
9570
0
                const auto type = proj_get_type(member);
9571
0
                isDynamic = type == PJ_TYPE_DYNAMIC_GEODETIC_REFERENCE_FRAME ||
9572
0
                            type == PJ_TYPE_DYNAMIC_VERTICAL_REFERENCE_FRAME;
9573
0
                proj_destroy(member);
9574
0
            }
9575
0
            proj_destroy(ensemble);
9576
0
        }
9577
0
    }
9578
0
#endif
9579
0
    proj_destroy(horiz);
9580
0
    d->undoDemoteFromBoundCRS();
9581
0
    return isDynamic;
9582
0
}
9583
9584
/************************************************************************/
9585
/*                           OSRIsDynamic()                             */
9586
/************************************************************************/
9587
/**
9588
 * \brief Check if a CRS is a dynamic CRS.
9589
 *
9590
 * A dynamic CRS relies on a dynamic datum, that is a datum that is not
9591
 * plate-fixed.
9592
 *
9593
 * This function is the same as OGRSpatialReference::IsDynamic().
9594
 *
9595
 * @since OGR 3.4.0
9596
 */
9597
int OSRIsDynamic(OGRSpatialReferenceH hSRS)
9598
9599
0
{
9600
0
    VALIDATE_POINTER1(hSRS, "OSRIsDynamic", 0);
9601
9602
0
    return ToPointer(hSRS)->IsDynamic();
9603
0
}
9604
9605
/************************************************************************/
9606
/*                         HasPointMotionOperation()                    */
9607
/************************************************************************/
9608
9609
/**
9610
 * \brief Check if a CRS has at least an associated point motion operation.
9611
 *
9612
 * Some CRS are not formally declared as dynamic, but may behave as such
9613
 * in practice due to the presence of point motion operation, to perform
9614
 * coordinate epoch changes within the CRS. Typically NAD83(CSRS)v7
9615
 *
9616
 * @return true if the CRS has at least an associated point motion operation.
9617
 *
9618
 * @since OGR 3.8.0 and PROJ 9.4.0
9619
 *
9620
 * @see IsDynamic()
9621
 */
9622
9623
bool OGRSpatialReference::HasPointMotionOperation() const
9624
9625
0
{
9626
0
#if PROJ_VERSION_MAJOR > 9 ||                                                  \
9627
0
    (PROJ_VERSION_MAJOR == 9 && PROJ_VERSION_MINOR >= 4)
9628
0
    TAKE_OPTIONAL_LOCK();
9629
0
    d->refreshProjObj();
9630
0
    d->demoteFromBoundCRS();
9631
0
    auto ctxt = d->getPROJContext();
9632
0
    auto res =
9633
0
        CPL_TO_BOOL(proj_crs_has_point_motion_operation(ctxt, d->m_pj_crs));
9634
0
    d->undoDemoteFromBoundCRS();
9635
0
    return res;
9636
#else
9637
    return false;
9638
#endif
9639
0
}
9640
9641
/************************************************************************/
9642
/*                      OSRHasPointMotionOperation()                    */
9643
/************************************************************************/
9644
9645
/**
9646
 * \brief Check if a CRS has at least an associated point motion operation.
9647
 *
9648
 * Some CRS are not formally declared as dynamic, but may behave as such
9649
 * in practice due to the presence of point motion operation, to perform
9650
 * coordinate epoch changes within the CRS. Typically NAD83(CSRS)v7
9651
 *
9652
 * This function is the same as OGRSpatialReference::HasPointMotionOperation().
9653
 *
9654
 * @since OGR 3.8.0 and PROJ 9.4.0
9655
 */
9656
int OSRHasPointMotionOperation(OGRSpatialReferenceH hSRS)
9657
9658
0
{
9659
0
    VALIDATE_POINTER1(hSRS, "OSRHasPointMotionOperation", 0);
9660
9661
0
    return ToPointer(hSRS)->HasPointMotionOperation();
9662
0
}
9663
9664
/************************************************************************/
9665
/*                            CloneGeogCS()                             */
9666
/************************************************************************/
9667
9668
/**
9669
 * \brief Make a duplicate of the GEOGCS node of this OGRSpatialReference
9670
 * object.
9671
 *
9672
 * @return a new SRS, which becomes the responsibility of the caller.
9673
 */
9674
OGRSpatialReference *OGRSpatialReference::CloneGeogCS() const
9675
9676
0
{
9677
0
    TAKE_OPTIONAL_LOCK();
9678
0
    d->refreshProjObj();
9679
0
    if (d->m_pj_crs)
9680
0
    {
9681
0
        if (d->m_pjType == PJ_TYPE_ENGINEERING_CRS)
9682
0
            return nullptr;
9683
9684
0
        auto geodCRS =
9685
0
            proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
9686
0
        if (geodCRS)
9687
0
        {
9688
0
            OGRSpatialReference *poNewSRS = new OGRSpatialReference();
9689
0
            if (d->m_pjType == PJ_TYPE_BOUND_CRS)
9690
0
            {
9691
0
                PJ *hub_crs =
9692
0
                    proj_get_target_crs(d->getPROJContext(), d->m_pj_crs);
9693
0
                PJ *co = proj_crs_get_coordoperation(d->getPROJContext(),
9694
0
                                                     d->m_pj_crs);
9695
0
                auto temp = proj_crs_create_bound_crs(d->getPROJContext(),
9696
0
                                                      geodCRS, hub_crs, co);
9697
0
                proj_destroy(geodCRS);
9698
0
                geodCRS = temp;
9699
0
                proj_destroy(hub_crs);
9700
0
                proj_destroy(co);
9701
0
            }
9702
9703
            /* --------------------------------------------------------------------
9704
             */
9705
            /*      We have to reconstruct the GEOGCS node for geocentric */
9706
            /*      coordinate systems. */
9707
            /* --------------------------------------------------------------------
9708
             */
9709
0
            if (proj_get_type(geodCRS) == PJ_TYPE_GEOCENTRIC_CRS)
9710
0
            {
9711
0
                auto datum = proj_crs_get_datum(d->getPROJContext(), geodCRS);
9712
0
#if PROJ_VERSION_MAJOR > 7 ||                                                  \
9713
0
    (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
9714
0
                if (datum == nullptr)
9715
0
                {
9716
0
                    datum = proj_crs_get_datum_ensemble(d->getPROJContext(),
9717
0
                                                        geodCRS);
9718
0
                }
9719
0
#endif
9720
0
                if (datum)
9721
0
                {
9722
0
                    auto cs = proj_create_ellipsoidal_2D_cs(
9723
0
                        d->getPROJContext(), PJ_ELLPS2D_LATITUDE_LONGITUDE,
9724
0
                        nullptr, 0);
9725
0
                    auto temp = proj_create_geographic_crs_from_datum(
9726
0
                        d->getPROJContext(), "unnamed", datum, cs);
9727
0
                    proj_destroy(datum);
9728
0
                    proj_destroy(cs);
9729
0
                    proj_destroy(geodCRS);
9730
0
                    geodCRS = temp;
9731
0
                }
9732
0
            }
9733
9734
0
            poNewSRS->d->setPjCRS(geodCRS);
9735
0
            if (d->m_axisMappingStrategy == OAMS_TRADITIONAL_GIS_ORDER)
9736
0
                poNewSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
9737
0
            return poNewSRS;
9738
0
        }
9739
0
    }
9740
0
    return nullptr;
9741
0
}
9742
9743
/************************************************************************/
9744
/*                           OSRCloneGeogCS()                           */
9745
/************************************************************************/
9746
/**
9747
 * \brief Make a duplicate of the GEOGCS node of this OGRSpatialReference
9748
 * object.
9749
 *
9750
 * This function is the same as OGRSpatialReference::CloneGeogCS().
9751
 */
9752
OGRSpatialReferenceH CPL_STDCALL OSRCloneGeogCS(OGRSpatialReferenceH hSource)
9753
9754
0
{
9755
0
    VALIDATE_POINTER1(hSource, "OSRCloneGeogCS", nullptr);
9756
9757
0
    return ToHandle(ToPointer(hSource)->CloneGeogCS());
9758
0
}
9759
9760
/************************************************************************/
9761
/*                            IsSameGeogCS()                            */
9762
/************************************************************************/
9763
9764
/**
9765
 * \brief Do the GeogCS'es match?
9766
 *
9767
 * This method is the same as the C function OSRIsSameGeogCS().
9768
 *
9769
 * @param poOther the SRS being compared against.
9770
 *
9771
 * @return TRUE if they are the same or FALSE otherwise.
9772
 */
9773
9774
int OGRSpatialReference::IsSameGeogCS(const OGRSpatialReference *poOther) const
9775
9776
0
{
9777
0
    return IsSameGeogCS(poOther, nullptr);
9778
0
}
9779
9780
/**
9781
 * \brief Do the GeogCS'es match?
9782
 *
9783
 * This method is the same as the C function OSRIsSameGeogCS().
9784
 *
9785
 * @param poOther the SRS being compared against.
9786
 * @param papszOptions options. ignored
9787
 *
9788
 * @return TRUE if they are the same or FALSE otherwise.
9789
 */
9790
9791
int OGRSpatialReference::IsSameGeogCS(const OGRSpatialReference *poOther,
9792
                                      const char *const *papszOptions) const
9793
9794
0
{
9795
0
    TAKE_OPTIONAL_LOCK();
9796
9797
0
    CPL_IGNORE_RET_VAL(papszOptions);
9798
9799
0
    d->refreshProjObj();
9800
0
    poOther->d->refreshProjObj();
9801
0
    if (!d->m_pj_crs || !poOther->d->m_pj_crs)
9802
0
        return FALSE;
9803
0
    if (d->m_pjType == PJ_TYPE_ENGINEERING_CRS ||
9804
0
        d->m_pjType == PJ_TYPE_VERTICAL_CRS ||
9805
0
        poOther->d->m_pjType == PJ_TYPE_ENGINEERING_CRS ||
9806
0
        poOther->d->m_pjType == PJ_TYPE_VERTICAL_CRS)
9807
0
    {
9808
0
        return FALSE;
9809
0
    }
9810
9811
0
    auto geodCRS = proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
9812
0
    auto otherGeodCRS =
9813
0
        proj_crs_get_geodetic_crs(d->getPROJContext(), poOther->d->m_pj_crs);
9814
0
    if (!geodCRS || !otherGeodCRS)
9815
0
    {
9816
0
        proj_destroy(geodCRS);
9817
0
        proj_destroy(otherGeodCRS);
9818
0
        return FALSE;
9819
0
    }
9820
9821
0
    int ret = proj_is_equivalent_to(
9822
0
        geodCRS, otherGeodCRS, PJ_COMP_EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS);
9823
9824
0
    proj_destroy(geodCRS);
9825
0
    proj_destroy(otherGeodCRS);
9826
0
    return ret;
9827
0
}
9828
9829
/************************************************************************/
9830
/*                          OSRIsSameGeogCS()                           */
9831
/************************************************************************/
9832
9833
/**
9834
 * \brief Do the GeogCS'es match?
9835
 *
9836
 * This function is the same as OGRSpatialReference::IsSameGeogCS().
9837
 */
9838
int OSRIsSameGeogCS(OGRSpatialReferenceH hSRS1, OGRSpatialReferenceH hSRS2)
9839
9840
0
{
9841
0
    VALIDATE_POINTER1(hSRS1, "OSRIsSameGeogCS", 0);
9842
0
    VALIDATE_POINTER1(hSRS2, "OSRIsSameGeogCS", 0);
9843
9844
0
    return ToPointer(hSRS1)->IsSameGeogCS(ToPointer(hSRS2));
9845
0
}
9846
9847
/************************************************************************/
9848
/*                            IsSameVertCS()                            */
9849
/************************************************************************/
9850
9851
/**
9852
 * \brief Do the VertCS'es match?
9853
 *
9854
 * This method is the same as the C function OSRIsSameVertCS().
9855
 *
9856
 * @param poOther the SRS being compared against.
9857
 *
9858
 * @return TRUE if they are the same or FALSE otherwise.
9859
 */
9860
9861
int OGRSpatialReference::IsSameVertCS(const OGRSpatialReference *poOther) const
9862
9863
0
{
9864
0
    TAKE_OPTIONAL_LOCK();
9865
9866
    /* -------------------------------------------------------------------- */
9867
    /*      Does the datum name match?                                      */
9868
    /* -------------------------------------------------------------------- */
9869
0
    const char *pszThisValue = this->GetAttrValue("VERT_DATUM");
9870
0
    const char *pszOtherValue = poOther->GetAttrValue("VERT_DATUM");
9871
9872
0
    if (pszThisValue == nullptr || pszOtherValue == nullptr ||
9873
0
        !EQUAL(pszThisValue, pszOtherValue))
9874
0
        return FALSE;
9875
9876
    /* -------------------------------------------------------------------- */
9877
    /*      Do the units match?                                             */
9878
    /* -------------------------------------------------------------------- */
9879
0
    pszThisValue = this->GetAttrValue("VERT_CS|UNIT", 1);
9880
0
    if (pszThisValue == nullptr)
9881
0
        pszThisValue = "1.0";
9882
9883
0
    pszOtherValue = poOther->GetAttrValue("VERT_CS|UNIT", 1);
9884
0
    if (pszOtherValue == nullptr)
9885
0
        pszOtherValue = "1.0";
9886
9887
0
    if (std::abs(CPLAtof(pszOtherValue) - CPLAtof(pszThisValue)) > 0.00000001)
9888
0
        return FALSE;
9889
9890
0
    return TRUE;
9891
0
}
9892
9893
/************************************************************************/
9894
/*                          OSRIsSameVertCS()                           */
9895
/************************************************************************/
9896
9897
/**
9898
 * \brief Do the VertCS'es match?
9899
 *
9900
 * This function is the same as OGRSpatialReference::IsSameVertCS().
9901
 */
9902
int OSRIsSameVertCS(OGRSpatialReferenceH hSRS1, OGRSpatialReferenceH hSRS2)
9903
9904
0
{
9905
0
    VALIDATE_POINTER1(hSRS1, "OSRIsSameVertCS", 0);
9906
0
    VALIDATE_POINTER1(hSRS2, "OSRIsSameVertCS", 0);
9907
9908
0
    return ToPointer(hSRS1)->IsSameVertCS(ToPointer(hSRS2));
9909
0
}
9910
9911
/************************************************************************/
9912
/*                               IsSame()                               */
9913
/************************************************************************/
9914
9915
/**
9916
 * \brief Do these two spatial references describe the same system ?
9917
 *
9918
 * @param poOtherSRS the SRS being compared to.
9919
 *
9920
 * @return TRUE if equivalent or FALSE otherwise.
9921
 */
9922
9923
int OGRSpatialReference::IsSame(const OGRSpatialReference *poOtherSRS) const
9924
9925
0
{
9926
0
    return IsSame(poOtherSRS, nullptr);
9927
0
}
9928
9929
/**
9930
 * \brief Do these two spatial references describe the same system ?
9931
 *
9932
 * This also takes into account the data axis to CRS axis mapping by default
9933
 *
9934
 * @param poOtherSRS the SRS being compared to.
9935
 * @param papszOptions options. NULL or NULL terminated list of options.
9936
 * Currently supported options are:
9937
 * <ul>
9938
 * <li>IGNORE_DATA_AXIS_TO_SRS_AXIS_MAPPING=YES/NO. Defaults to NO</li>
9939
 * <li>CRITERION=STRICT/EQUIVALENT/EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS.
9940
 *     Defaults to EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS.</li>
9941
 * <li>IGNORE_COORDINATE_EPOCH=YES/NO. Defaults to NO</li>
9942
 * </ul>
9943
 *
9944
 * @return TRUE if equivalent or FALSE otherwise.
9945
 */
9946
9947
int OGRSpatialReference::IsSame(const OGRSpatialReference *poOtherSRS,
9948
                                const char *const *papszOptions) const
9949
9950
0
{
9951
0
    TAKE_OPTIONAL_LOCK();
9952
9953
0
    d->refreshProjObj();
9954
0
    poOtherSRS->d->refreshProjObj();
9955
0
    if (!d->m_pj_crs || !poOtherSRS->d->m_pj_crs)
9956
0
        return d->m_pj_crs == poOtherSRS->d->m_pj_crs;
9957
0
    if (!CPLTestBool(CSLFetchNameValueDef(
9958
0
            papszOptions, "IGNORE_DATA_AXIS_TO_SRS_AXIS_MAPPING", "NO")))
9959
0
    {
9960
0
        if (d->m_axisMapping != poOtherSRS->d->m_axisMapping)
9961
0
            return false;
9962
0
    }
9963
9964
0
    if (!CPLTestBool(CSLFetchNameValueDef(papszOptions,
9965
0
                                          "IGNORE_COORDINATE_EPOCH", "NO")))
9966
0
    {
9967
0
        if (d->m_coordinateEpoch != poOtherSRS->d->m_coordinateEpoch)
9968
0
            return false;
9969
0
    }
9970
9971
0
    bool reboundSelf = false;
9972
0
    bool reboundOther = false;
9973
0
    if (d->m_pjType == PJ_TYPE_BOUND_CRS &&
9974
0
        poOtherSRS->d->m_pjType != PJ_TYPE_BOUND_CRS)
9975
0
    {
9976
0
        d->demoteFromBoundCRS();
9977
0
        reboundSelf = true;
9978
0
    }
9979
0
    else if (d->m_pjType != PJ_TYPE_BOUND_CRS &&
9980
0
             poOtherSRS->d->m_pjType == PJ_TYPE_BOUND_CRS)
9981
0
    {
9982
0
        poOtherSRS->d->demoteFromBoundCRS();
9983
0
        reboundOther = true;
9984
0
    }
9985
9986
0
    PJ_COMPARISON_CRITERION criterion =
9987
0
        PJ_COMP_EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS;
9988
0
    const char *pszCriterion = CSLFetchNameValueDef(
9989
0
        papszOptions, "CRITERION", "EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS");
9990
0
    if (EQUAL(pszCriterion, "STRICT"))
9991
0
        criterion = PJ_COMP_STRICT;
9992
0
    else if (EQUAL(pszCriterion, "EQUIVALENT"))
9993
0
        criterion = PJ_COMP_EQUIVALENT;
9994
0
    else if (!EQUAL(pszCriterion, "EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS"))
9995
0
    {
9996
0
        CPLError(CE_Warning, CPLE_NotSupported,
9997
0
                 "Unsupported value for CRITERION: %s", pszCriterion);
9998
0
    }
9999
0
    int ret =
10000
0
        proj_is_equivalent_to(d->m_pj_crs, poOtherSRS->d->m_pj_crs, criterion);
10001
0
    if (reboundSelf)
10002
0
        d->undoDemoteFromBoundCRS();
10003
0
    if (reboundOther)
10004
0
        poOtherSRS->d->undoDemoteFromBoundCRS();
10005
10006
0
    return ret;
10007
0
}
10008
10009
/************************************************************************/
10010
/*                             OSRIsSame()                              */
10011
/************************************************************************/
10012
10013
/**
10014
 * \brief Do these two spatial references describe the same system ?
10015
 *
10016
 * This function is the same as OGRSpatialReference::IsSame().
10017
 */
10018
int OSRIsSame(OGRSpatialReferenceH hSRS1, OGRSpatialReferenceH hSRS2)
10019
10020
0
{
10021
0
    VALIDATE_POINTER1(hSRS1, "OSRIsSame", 0);
10022
0
    VALIDATE_POINTER1(hSRS2, "OSRIsSame", 0);
10023
10024
0
    return ToPointer(hSRS1)->IsSame(ToPointer(hSRS2));
10025
0
}
10026
10027
/************************************************************************/
10028
/*                             OSRIsSameEx()                            */
10029
/************************************************************************/
10030
10031
/**
10032
 * \brief Do these two spatial references describe the same system ?
10033
 *
10034
 * This function is the same as OGRSpatialReference::IsSame().
10035
 */
10036
int OSRIsSameEx(OGRSpatialReferenceH hSRS1, OGRSpatialReferenceH hSRS2,
10037
                const char *const *papszOptions)
10038
0
{
10039
0
    VALIDATE_POINTER1(hSRS1, "OSRIsSame", 0);
10040
0
    VALIDATE_POINTER1(hSRS2, "OSRIsSame", 0);
10041
10042
0
    return ToPointer(hSRS1)->IsSame(ToPointer(hSRS2), papszOptions);
10043
0
}
10044
10045
/************************************************************************/
10046
/*                    convertToOtherProjection()                        */
10047
/************************************************************************/
10048
10049
/**
10050
 * \brief Convert to another equivalent projection
10051
 *
10052
 * Currently implemented:
10053
 * <ul>
10054
 * <li>SRS_PT_MERCATOR_1SP to SRS_PT_MERCATOR_2SP</li>
10055
 * <li>SRS_PT_MERCATOR_2SP to SRS_PT_MERCATOR_1SP</li>
10056
 * <li>SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP to
10057
 * SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP</li>
10058
 * <li>SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP to
10059
 * SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP</li>
10060
 * </ul>
10061
 *
10062
 * @param pszTargetProjection target projection.
10063
 * @param papszOptions lists of options. None supported currently.
10064
 * @return a new SRS, or NULL in case of error.
10065
 *
10066
 * @since GDAL 2.3
10067
 */
10068
OGRSpatialReference *OGRSpatialReference::convertToOtherProjection(
10069
    const char *pszTargetProjection,
10070
    CPL_UNUSED const char *const *papszOptions) const
10071
0
{
10072
0
    TAKE_OPTIONAL_LOCK();
10073
10074
0
    if (pszTargetProjection == nullptr)
10075
0
        return nullptr;
10076
0
    int new_code;
10077
0
    if (EQUAL(pszTargetProjection, SRS_PT_MERCATOR_1SP))
10078
0
    {
10079
0
        new_code = EPSG_CODE_METHOD_MERCATOR_VARIANT_A;
10080
0
    }
10081
0
    else if (EQUAL(pszTargetProjection, SRS_PT_MERCATOR_2SP))
10082
0
    {
10083
0
        new_code = EPSG_CODE_METHOD_MERCATOR_VARIANT_B;
10084
0
    }
10085
0
    else if (EQUAL(pszTargetProjection, SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP))
10086
0
    {
10087
0
        new_code = EPSG_CODE_METHOD_LAMBERT_CONIC_CONFORMAL_1SP;
10088
0
    }
10089
0
    else if (EQUAL(pszTargetProjection, SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP))
10090
0
    {
10091
0
        new_code = EPSG_CODE_METHOD_LAMBERT_CONIC_CONFORMAL_2SP;
10092
0
    }
10093
0
    else
10094
0
    {
10095
0
        return nullptr;
10096
0
    }
10097
10098
0
    d->refreshProjObj();
10099
0
    d->demoteFromBoundCRS();
10100
0
    OGRSpatialReference *poNewSRS = nullptr;
10101
0
    if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
10102
0
    {
10103
0
        auto conv =
10104
0
            proj_crs_get_coordoperation(d->getPROJContext(), d->m_pj_crs);
10105
0
        auto new_conv = proj_convert_conversion_to_other_method(
10106
0
            d->getPROJContext(), conv, new_code, nullptr);
10107
0
        proj_destroy(conv);
10108
0
        if (new_conv)
10109
0
        {
10110
0
            auto geodCRS =
10111
0
                proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
10112
0
            auto cs = proj_crs_get_coordinate_system(d->getPROJContext(),
10113
0
                                                     d->m_pj_crs);
10114
0
            if (geodCRS && cs)
10115
0
            {
10116
0
                auto new_proj_crs = proj_create_projected_crs(
10117
0
                    d->getPROJContext(), proj_get_name(d->m_pj_crs), geodCRS,
10118
0
                    new_conv, cs);
10119
0
                proj_destroy(new_conv);
10120
0
                if (new_proj_crs)
10121
0
                {
10122
0
                    poNewSRS = new OGRSpatialReference();
10123
10124
0
                    if (d->m_pj_bound_crs_target && d->m_pj_bound_crs_co)
10125
0
                    {
10126
0
                        auto boundCRS = proj_crs_create_bound_crs(
10127
0
                            d->getPROJContext(), new_proj_crs,
10128
0
                            d->m_pj_bound_crs_target, d->m_pj_bound_crs_co);
10129
0
                        if (boundCRS)
10130
0
                        {
10131
0
                            proj_destroy(new_proj_crs);
10132
0
                            new_proj_crs = boundCRS;
10133
0
                        }
10134
0
                    }
10135
10136
0
                    poNewSRS->d->setPjCRS(new_proj_crs);
10137
0
                }
10138
0
            }
10139
0
            proj_destroy(geodCRS);
10140
0
            proj_destroy(cs);
10141
0
        }
10142
0
    }
10143
0
    d->undoDemoteFromBoundCRS();
10144
0
    return poNewSRS;
10145
0
}
10146
10147
/************************************************************************/
10148
/*                    OSRConvertToOtherProjection()                     */
10149
/************************************************************************/
10150
10151
/**
10152
 * \brief Convert to another equivalent projection
10153
 *
10154
 * Currently implemented:
10155
 * <ul>
10156
 * <li>SRS_PT_MERCATOR_1SP to SRS_PT_MERCATOR_2SP</li>
10157
 * <li>SRS_PT_MERCATOR_2SP to SRS_PT_MERCATOR_1SP</li>
10158
 * <li>SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP to
10159
 * SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP</li>
10160
 * <li>SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP to
10161
 * SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP</li>
10162
 * </ul>
10163
 *
10164
 * @param hSRS source SRS
10165
 * @param pszTargetProjection target projection.
10166
 * @param papszOptions lists of options. None supported currently.
10167
 * @return a new SRS, or NULL in case of error.
10168
 *
10169
 * @since GDAL 2.3
10170
 */
10171
OGRSpatialReferenceH
10172
OSRConvertToOtherProjection(OGRSpatialReferenceH hSRS,
10173
                            const char *pszTargetProjection,
10174
                            const char *const *papszOptions)
10175
0
{
10176
0
    VALIDATE_POINTER1(hSRS, "OSRConvertToOtherProjection", nullptr);
10177
0
    return ToHandle(ToPointer(hSRS)->convertToOtherProjection(
10178
0
        pszTargetProjection, papszOptions));
10179
0
}
10180
10181
/************************************************************************/
10182
/*                           OSRFindMatches()                           */
10183
/************************************************************************/
10184
10185
/**
10186
 * \brief Try to identify a match between the passed SRS and a related SRS
10187
 * in a catalog.
10188
 *
10189
 * Matching may be partial, or may fail.
10190
 * Returned entries will be sorted by decreasing match confidence (first
10191
 * entry has the highest match confidence).
10192
 *
10193
 * The exact way matching is done may change in future versions. Starting with
10194
 * GDAL 3.0, it relies on PROJ' proj_identify() function.
10195
 *
10196
 * This function is the same as OGRSpatialReference::FindMatches().
10197
 *
10198
 * @param hSRS SRS to match
10199
 * @param papszOptions NULL terminated list of options or NULL
10200
 * @param pnEntries Output parameter. Number of values in the returned array.
10201
 * @param ppanMatchConfidence Output parameter (or NULL). *ppanMatchConfidence
10202
 * will be allocated to an array of *pnEntries whose values between 0 and 100
10203
 * indicate the confidence in the match. 100 is the highest confidence level.
10204
 * The array must be freed with CPLFree().
10205
 *
10206
 * @return an array of SRS that match the passed SRS, or NULL. Must be freed
10207
 * with OSRFreeSRSArray()
10208
 *
10209
 * @since GDAL 2.3
10210
 */
10211
OGRSpatialReferenceH *OSRFindMatches(OGRSpatialReferenceH hSRS,
10212
                                     char **papszOptions, int *pnEntries,
10213
                                     int **ppanMatchConfidence)
10214
0
{
10215
0
    if (pnEntries)
10216
0
        *pnEntries = 0;
10217
0
    if (ppanMatchConfidence)
10218
0
        *ppanMatchConfidence = nullptr;
10219
0
    VALIDATE_POINTER1(hSRS, "OSRFindMatches", nullptr);
10220
10221
0
    OGRSpatialReference *poSRS = ToPointer(hSRS);
10222
0
    return poSRS->FindMatches(papszOptions, pnEntries, ppanMatchConfidence);
10223
0
}
10224
10225
/************************************************************************/
10226
/*                           OSRFreeSRSArray()                          */
10227
/************************************************************************/
10228
10229
/**
10230
 * \brief Free return of OSRIdentifyMatches()
10231
 *
10232
 * @param pahSRS array of SRS (must be NULL terminated)
10233
 * @since GDAL 2.3
10234
 */
10235
void OSRFreeSRSArray(OGRSpatialReferenceH *pahSRS)
10236
0
{
10237
0
    if (pahSRS != nullptr)
10238
0
    {
10239
0
        for (int i = 0; pahSRS[i] != nullptr; ++i)
10240
0
        {
10241
0
            OSRRelease(pahSRS[i]);
10242
0
        }
10243
0
        CPLFree(pahSRS);
10244
0
    }
10245
0
}
10246
10247
/************************************************************************/
10248
/*                         FindBestMatch()                              */
10249
/************************************************************************/
10250
10251
/**
10252
 * \brief Try to identify the best match between the passed SRS and a related
10253
 * SRS in a catalog.
10254
 *
10255
 * This is a wrapper over OGRSpatialReference::FindMatches() that takes care
10256
 * of filtering its output.
10257
 * Only matches whose confidence is greater or equal to nMinimumMatchConfidence
10258
 * will be considered. If there is a single match, it is returned.
10259
 * If there are several matches, only return the one under the
10260
 * pszPreferredAuthority, if there is a single one under that authority.
10261
 *
10262
 * @param nMinimumMatchConfidence Minimum match confidence (value between 0 and
10263
 * 100). If set to 0, 90 is used.
10264
 * @param pszPreferredAuthority Preferred CRS authority. If set to nullptr,
10265
 * "EPSG" is used.
10266
 * @param papszOptions NULL terminated list of options or NULL. No option is
10267
 * defined at time of writing.
10268
 *
10269
 * @return a new OGRSpatialReference* object to free with Release(), or nullptr
10270
 *
10271
 * @since GDAL 3.6
10272
 * @see OGRSpatialReference::FindMatches()
10273
 */
10274
OGRSpatialReference *
10275
OGRSpatialReference::FindBestMatch(int nMinimumMatchConfidence,
10276
                                   const char *pszPreferredAuthority,
10277
                                   CSLConstList papszOptions) const
10278
0
{
10279
0
    TAKE_OPTIONAL_LOCK();
10280
10281
0
    CPL_IGNORE_RET_VAL(papszOptions);  // ignored for now.
10282
10283
0
    if (nMinimumMatchConfidence == 0)
10284
0
        nMinimumMatchConfidence = 90;
10285
0
    if (pszPreferredAuthority == nullptr)
10286
0
        pszPreferredAuthority = "EPSG";
10287
10288
    // Try to identify the CRS with the database
10289
0
    int nEntries = 0;
10290
0
    int *panConfidence = nullptr;
10291
0
    OGRSpatialReferenceH *pahSRS =
10292
0
        FindMatches(nullptr, &nEntries, &panConfidence);
10293
0
    if (nEntries == 1 && panConfidence[0] >= nMinimumMatchConfidence)
10294
0
    {
10295
0
        std::vector<double> adfTOWGS84(7);
10296
0
        if (GetTOWGS84(&adfTOWGS84[0], 7) != OGRERR_NONE)
10297
0
        {
10298
0
            adfTOWGS84.clear();
10299
0
        }
10300
10301
0
        auto poSRS = OGRSpatialReference::FromHandle(pahSRS[0]);
10302
10303
0
        auto poBaseGeogCRS =
10304
0
            std::unique_ptr<OGRSpatialReference>(poSRS->CloneGeogCS());
10305
10306
        // If the base geographic SRS of the SRS is EPSG:4326
10307
        // with TOWGS84[0,0,0,0,0,0], then just use the official
10308
        // SRS code
10309
        // Same with EPSG:4258 (ETRS89), since it's the only known
10310
        // TOWGS84[] style transformation to WGS 84, and given the
10311
        // "fuzzy" nature of both ETRS89 and WGS 84, there's little
10312
        // chance that a non-NULL TOWGS84[] will emerge.
10313
0
        const char *pszAuthorityName = nullptr;
10314
0
        const char *pszAuthorityCode = nullptr;
10315
0
        const char *pszBaseAuthorityName = nullptr;
10316
0
        const char *pszBaseAuthorityCode = nullptr;
10317
0
        if (adfTOWGS84 == std::vector<double>(7) &&
10318
0
            (pszAuthorityName = poSRS->GetAuthorityName(nullptr)) != nullptr &&
10319
0
            EQUAL(pszAuthorityName, "EPSG") &&
10320
0
            (pszAuthorityCode = poSRS->GetAuthorityCode(nullptr)) != nullptr &&
10321
0
            (pszBaseAuthorityName = poBaseGeogCRS->GetAuthorityName(nullptr)) !=
10322
0
                nullptr &&
10323
0
            EQUAL(pszBaseAuthorityName, "EPSG") &&
10324
0
            (pszBaseAuthorityCode = poBaseGeogCRS->GetAuthorityCode(nullptr)) !=
10325
0
                nullptr &&
10326
0
            (EQUAL(pszBaseAuthorityCode, "4326") ||
10327
0
             EQUAL(pszBaseAuthorityCode, "4258")))
10328
0
        {
10329
0
            poSRS->importFromEPSG(atoi(pszAuthorityCode));
10330
0
        }
10331
10332
0
        CPLFree(pahSRS);
10333
0
        CPLFree(panConfidence);
10334
10335
0
        return poSRS;
10336
0
    }
10337
0
    else
10338
0
    {
10339
        // If there are several matches >= nMinimumMatchConfidence, take the
10340
        // only one that is under pszPreferredAuthority
10341
0
        int iBestEntry = -1;
10342
0
        for (int i = 0; i < nEntries; i++)
10343
0
        {
10344
0
            if (panConfidence[i] >= nMinimumMatchConfidence)
10345
0
            {
10346
0
                const char *pszAuthName =
10347
0
                    OGRSpatialReference::FromHandle(pahSRS[i])
10348
0
                        ->GetAuthorityName(nullptr);
10349
0
                if (pszAuthName != nullptr &&
10350
0
                    EQUAL(pszAuthName, pszPreferredAuthority))
10351
0
                {
10352
0
                    if (iBestEntry < 0)
10353
0
                        iBestEntry = i;
10354
0
                    else
10355
0
                    {
10356
0
                        iBestEntry = -1;
10357
0
                        break;
10358
0
                    }
10359
0
                }
10360
0
            }
10361
0
        }
10362
0
        if (iBestEntry >= 0)
10363
0
        {
10364
0
            auto poRet = OGRSpatialReference::FromHandle(pahSRS[0])->Clone();
10365
0
            OSRFreeSRSArray(pahSRS);
10366
0
            CPLFree(panConfidence);
10367
0
            return poRet;
10368
0
        }
10369
0
    }
10370
0
    OSRFreeSRSArray(pahSRS);
10371
0
    CPLFree(panConfidence);
10372
0
    return nullptr;
10373
0
}
10374
10375
/************************************************************************/
10376
/*                             SetTOWGS84()                             */
10377
/************************************************************************/
10378
10379
/**
10380
 * \brief Set the Bursa-Wolf conversion to WGS84.
10381
 *
10382
 * This will create the TOWGS84 node as a child of the DATUM.  It will fail
10383
 * if there is no existing DATUM node. It will replace
10384
 * an existing TOWGS84 node if there is one.
10385
 *
10386
 * The parameters have the same meaning as EPSG transformation 9606
10387
 * (Position Vector 7-param. transformation).
10388
 *
10389
 * This method is the same as the C function OSRSetTOWGS84().
10390
 *
10391
 * @param dfDX X child in meters.
10392
 * @param dfDY Y child in meters.
10393
 * @param dfDZ Z child in meters.
10394
 * @param dfEX X rotation in arc seconds (optional, defaults to zero).
10395
 * @param dfEY Y rotation in arc seconds (optional, defaults to zero).
10396
 * @param dfEZ Z rotation in arc seconds (optional, defaults to zero).
10397
 * @param dfPPM scaling factor (parts per million).
10398
 *
10399
 * @return OGRERR_NONE on success.
10400
 */
10401
10402
OGRErr OGRSpatialReference::SetTOWGS84(double dfDX, double dfDY, double dfDZ,
10403
                                       double dfEX, double dfEY, double dfEZ,
10404
                                       double dfPPM)
10405
10406
0
{
10407
0
    TAKE_OPTIONAL_LOCK();
10408
10409
0
    d->refreshProjObj();
10410
0
    if (d->m_pj_crs == nullptr)
10411
0
    {
10412
0
        return OGRERR_FAILURE;
10413
0
    }
10414
10415
    // Remove existing BoundCRS
10416
0
    if (d->m_pjType == PJ_TYPE_BOUND_CRS)
10417
0
    {
10418
0
        auto baseCRS = proj_get_source_crs(d->getPROJContext(), d->m_pj_crs);
10419
0
        if (!baseCRS)
10420
0
            return OGRERR_FAILURE;
10421
0
        d->setPjCRS(baseCRS);
10422
0
    }
10423
10424
0
    PJ_PARAM_DESCRIPTION params[7];
10425
10426
0
    params[0].name = EPSG_NAME_PARAMETER_X_AXIS_TRANSLATION;
10427
0
    params[0].auth_name = "EPSG";
10428
0
    params[0].code = XSTRINGIFY(EPSG_CODE_PARAMETER_X_AXIS_TRANSLATION);
10429
0
    params[0].value = dfDX;
10430
0
    params[0].unit_name = "metre";
10431
0
    params[0].unit_conv_factor = 1.0;
10432
0
    params[0].unit_type = PJ_UT_LINEAR;
10433
10434
0
    params[1].name = EPSG_NAME_PARAMETER_Y_AXIS_TRANSLATION;
10435
0
    params[1].auth_name = "EPSG";
10436
0
    params[1].code = XSTRINGIFY(EPSG_CODE_PARAMETER_Y_AXIS_TRANSLATION);
10437
0
    params[1].value = dfDY;
10438
0
    params[1].unit_name = "metre";
10439
0
    params[1].unit_conv_factor = 1.0;
10440
0
    params[1].unit_type = PJ_UT_LINEAR;
10441
10442
0
    params[2].name = EPSG_NAME_PARAMETER_Z_AXIS_TRANSLATION;
10443
0
    params[2].auth_name = "EPSG";
10444
0
    params[2].code = XSTRINGIFY(EPSG_CODE_PARAMETER_Z_AXIS_TRANSLATION);
10445
0
    params[2].value = dfDZ;
10446
0
    params[2].unit_name = "metre";
10447
0
    params[2].unit_conv_factor = 1.0;
10448
0
    params[2].unit_type = PJ_UT_LINEAR;
10449
10450
0
    params[3].name = EPSG_NAME_PARAMETER_X_AXIS_ROTATION;
10451
0
    params[3].auth_name = "EPSG";
10452
0
    params[3].code = XSTRINGIFY(EPSG_CODE_PARAMETER_X_AXIS_ROTATION);
10453
0
    params[3].value = dfEX;
10454
0
    params[3].unit_name = "arc-second";
10455
0
    params[3].unit_conv_factor = 1. / 3600 * M_PI / 180;
10456
0
    params[3].unit_type = PJ_UT_ANGULAR;
10457
10458
0
    params[4].name = EPSG_NAME_PARAMETER_Y_AXIS_ROTATION;
10459
0
    params[4].auth_name = "EPSG";
10460
0
    params[4].code = XSTRINGIFY(EPSG_CODE_PARAMETER_Y_AXIS_ROTATION);
10461
0
    params[4].value = dfEY;
10462
0
    params[4].unit_name = "arc-second";
10463
0
    params[4].unit_conv_factor = 1. / 3600 * M_PI / 180;
10464
0
    params[4].unit_type = PJ_UT_ANGULAR;
10465
10466
0
    params[5].name = EPSG_NAME_PARAMETER_Z_AXIS_ROTATION;
10467
0
    params[5].auth_name = "EPSG";
10468
0
    params[5].code = XSTRINGIFY(EPSG_CODE_PARAMETER_Z_AXIS_ROTATION);
10469
0
    params[5].value = dfEZ;
10470
0
    params[5].unit_name = "arc-second";
10471
0
    params[5].unit_conv_factor = 1. / 3600 * M_PI / 180;
10472
0
    params[5].unit_type = PJ_UT_ANGULAR;
10473
10474
0
    params[6].name = EPSG_NAME_PARAMETER_SCALE_DIFFERENCE;
10475
0
    params[6].auth_name = "EPSG";
10476
0
    params[6].code = XSTRINGIFY(EPSG_CODE_PARAMETER_SCALE_DIFFERENCE);
10477
0
    params[6].value = dfPPM;
10478
0
    params[6].unit_name = "parts per million";
10479
0
    params[6].unit_conv_factor = 1e-6;
10480
0
    params[6].unit_type = PJ_UT_SCALE;
10481
10482
0
    auto sourceCRS =
10483
0
        proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
10484
0
    if (!sourceCRS)
10485
0
    {
10486
0
        return OGRERR_FAILURE;
10487
0
    }
10488
10489
0
    const auto sourceType = proj_get_type(sourceCRS);
10490
10491
0
    auto targetCRS = proj_create_from_database(
10492
0
        d->getPROJContext(), "EPSG",
10493
0
        sourceType == PJ_TYPE_GEOGRAPHIC_2D_CRS   ? "4326"
10494
0
        : sourceType == PJ_TYPE_GEOGRAPHIC_3D_CRS ? "4979"
10495
0
                                                  : "4978",
10496
0
        PJ_CATEGORY_CRS, false, nullptr);
10497
0
    if (!targetCRS)
10498
0
    {
10499
0
        proj_destroy(sourceCRS);
10500
0
        return OGRERR_FAILURE;
10501
0
    }
10502
10503
0
    CPLString osMethodCode;
10504
0
    osMethodCode.Printf("%d",
10505
0
                        sourceType == PJ_TYPE_GEOGRAPHIC_2D_CRS
10506
0
                            ? EPSG_CODE_METHOD_POSITION_VECTOR_GEOGRAPHIC_2D
10507
0
                        : sourceType == PJ_TYPE_GEOGRAPHIC_3D_CRS
10508
0
                            ? EPSG_CODE_METHOD_POSITION_VECTOR_GEOGRAPHIC_3D
10509
0
                            : EPSG_CODE_METHOD_POSITION_VECTOR_GEOCENTRIC);
10510
10511
0
    auto transf = proj_create_transformation(
10512
0
        d->getPROJContext(), "Transformation to WGS84", nullptr, nullptr,
10513
0
        sourceCRS, targetCRS, nullptr,
10514
0
        sourceType == PJ_TYPE_GEOGRAPHIC_2D_CRS
10515
0
            ? EPSG_NAME_METHOD_POSITION_VECTOR_GEOGRAPHIC_2D
10516
0
        : sourceType == PJ_TYPE_GEOGRAPHIC_3D_CRS
10517
0
            ? EPSG_NAME_METHOD_POSITION_VECTOR_GEOGRAPHIC_3D
10518
0
            : EPSG_NAME_METHOD_POSITION_VECTOR_GEOCENTRIC,
10519
0
        "EPSG", osMethodCode.c_str(), 7, params, -1);
10520
0
    proj_destroy(sourceCRS);
10521
0
    if (!transf)
10522
0
    {
10523
0
        proj_destroy(targetCRS);
10524
0
        return OGRERR_FAILURE;
10525
0
    }
10526
10527
0
    auto newBoundCRS = proj_crs_create_bound_crs(
10528
0
        d->getPROJContext(), d->m_pj_crs, targetCRS, transf);
10529
0
    proj_destroy(transf);
10530
0
    proj_destroy(targetCRS);
10531
0
    if (!newBoundCRS)
10532
0
    {
10533
0
        return OGRERR_FAILURE;
10534
0
    }
10535
10536
0
    d->setPjCRS(newBoundCRS);
10537
0
    return OGRERR_NONE;
10538
0
}
10539
10540
/************************************************************************/
10541
/*                           OSRSetTOWGS84()                            */
10542
/************************************************************************/
10543
10544
/**
10545
 * \brief Set the Bursa-Wolf conversion to WGS84.
10546
 *
10547
 * This function is the same as OGRSpatialReference::SetTOWGS84().
10548
 */
10549
OGRErr OSRSetTOWGS84(OGRSpatialReferenceH hSRS, double dfDX, double dfDY,
10550
                     double dfDZ, double dfEX, double dfEY, double dfEZ,
10551
                     double dfPPM)
10552
10553
0
{
10554
0
    VALIDATE_POINTER1(hSRS, "OSRSetTOWGS84", OGRERR_FAILURE);
10555
10556
0
    return ToPointer(hSRS)->SetTOWGS84(dfDX, dfDY, dfDZ, dfEX, dfEY, dfEZ,
10557
0
                                       dfPPM);
10558
0
}
10559
10560
/************************************************************************/
10561
/*                             GetTOWGS84()                             */
10562
/************************************************************************/
10563
10564
/**
10565
 * \brief Fetch TOWGS84 parameters, if available.
10566
 *
10567
 * The parameters have the same meaning as EPSG transformation 9606
10568
 * (Position Vector 7-param. transformation).
10569
 *
10570
 * @param padfCoeff array into which up to 7 coefficients are placed.
10571
 * @param nCoeffCount size of padfCoeff - defaults to 7.
10572
 *
10573
 * @return OGRERR_NONE on success, or OGRERR_FAILURE if there is no
10574
 * TOWGS84 node available.
10575
 */
10576
10577
OGRErr OGRSpatialReference::GetTOWGS84(double *padfCoeff, int nCoeffCount) const
10578
10579
0
{
10580
0
    TAKE_OPTIONAL_LOCK();
10581
10582
0
    d->refreshProjObj();
10583
0
    if (d->m_pjType != PJ_TYPE_BOUND_CRS)
10584
0
        return OGRERR_FAILURE;
10585
10586
0
    memset(padfCoeff, 0, sizeof(double) * nCoeffCount);
10587
10588
0
    auto transf = proj_crs_get_coordoperation(d->getPROJContext(), d->m_pj_crs);
10589
0
    int success = proj_coordoperation_get_towgs84_values(
10590
0
        d->getPROJContext(), transf, padfCoeff, nCoeffCount, false);
10591
0
    proj_destroy(transf);
10592
10593
0
    return success ? OGRERR_NONE : OGRERR_FAILURE;
10594
0
}
10595
10596
/************************************************************************/
10597
/*                           OSRGetTOWGS84()                            */
10598
/************************************************************************/
10599
10600
/**
10601
 * \brief Fetch TOWGS84 parameters, if available.
10602
 *
10603
 * This function is the same as OGRSpatialReference::GetTOWGS84().
10604
 */
10605
OGRErr OSRGetTOWGS84(OGRSpatialReferenceH hSRS, double *padfCoeff,
10606
                     int nCoeffCount)
10607
10608
0
{
10609
0
    VALIDATE_POINTER1(hSRS, "OSRGetTOWGS84", OGRERR_FAILURE);
10610
10611
0
    return ToPointer(hSRS)->GetTOWGS84(padfCoeff, nCoeffCount);
10612
0
}
10613
10614
/************************************************************************/
10615
/*                         IsAngularParameter()                         */
10616
/************************************************************************/
10617
10618
/** Is the passed projection parameter an angular one?
10619
 *
10620
 * @return TRUE or FALSE
10621
 */
10622
10623
/* static */
10624
int OGRSpatialReference::IsAngularParameter(const char *pszParameterName)
10625
10626
0
{
10627
0
    if (STARTS_WITH_CI(pszParameterName, "long") ||
10628
0
        STARTS_WITH_CI(pszParameterName, "lati") ||
10629
0
        EQUAL(pszParameterName, SRS_PP_CENTRAL_MERIDIAN) ||
10630
0
        STARTS_WITH_CI(pszParameterName, "standard_parallel") ||
10631
0
        EQUAL(pszParameterName, SRS_PP_AZIMUTH) ||
10632
0
        EQUAL(pszParameterName, SRS_PP_RECTIFIED_GRID_ANGLE))
10633
0
        return TRUE;
10634
10635
0
    return FALSE;
10636
0
}
10637
10638
/************************************************************************/
10639
/*                        IsLongitudeParameter()                        */
10640
/************************************************************************/
10641
10642
/** Is the passed projection parameter an angular longitude
10643
 * (relative to a prime meridian)?
10644
 *
10645
 * @return TRUE or FALSE
10646
 */
10647
10648
/* static */
10649
int OGRSpatialReference::IsLongitudeParameter(const char *pszParameterName)
10650
10651
0
{
10652
0
    if (STARTS_WITH_CI(pszParameterName, "long") ||
10653
0
        EQUAL(pszParameterName, SRS_PP_CENTRAL_MERIDIAN))
10654
0
        return TRUE;
10655
10656
0
    return FALSE;
10657
0
}
10658
10659
/************************************************************************/
10660
/*                         IsLinearParameter()                          */
10661
/************************************************************************/
10662
10663
/** Is the passed projection parameter an linear one measured in meters or
10664
 * some similar linear measure.
10665
 *
10666
 * @return TRUE or FALSE
10667
 */
10668
10669
/* static */
10670
int OGRSpatialReference::IsLinearParameter(const char *pszParameterName)
10671
10672
0
{
10673
0
    if (STARTS_WITH_CI(pszParameterName, "false_") ||
10674
0
        EQUAL(pszParameterName, SRS_PP_SATELLITE_HEIGHT))
10675
0
        return TRUE;
10676
10677
0
    return FALSE;
10678
0
}
10679
10680
/************************************************************************/
10681
/*                            GetNormInfo()                             */
10682
/************************************************************************/
10683
10684
/**
10685
 * \brief Set the internal information for normalizing linear, and angular
10686
 * values.
10687
 */
10688
void OGRSpatialReference::GetNormInfo() const
10689
10690
0
{
10691
0
    TAKE_OPTIONAL_LOCK();
10692
10693
0
    if (d->bNormInfoSet)
10694
0
        return;
10695
10696
    /* -------------------------------------------------------------------- */
10697
    /*      Initialize values.                                              */
10698
    /* -------------------------------------------------------------------- */
10699
0
    d->bNormInfoSet = TRUE;
10700
10701
0
    d->dfFromGreenwich = GetPrimeMeridian(nullptr);
10702
0
    d->dfToMeter = GetLinearUnits(nullptr);
10703
0
    d->dfToDegrees = GetAngularUnits(nullptr) / CPLAtof(SRS_UA_DEGREE_CONV);
10704
0
    if (fabs(d->dfToDegrees - 1.0) < 0.000000001)
10705
0
        d->dfToDegrees = 1.0;
10706
0
}
10707
10708
/************************************************************************/
10709
/*                            GetExtension()                            */
10710
/************************************************************************/
10711
10712
/**
10713
 * \brief Fetch extension value.
10714
 *
10715
 * Fetch the value of the named EXTENSION item for the identified
10716
 * target node.
10717
 *
10718
 * @param pszTargetKey the name or path to the parent node of the EXTENSION.
10719
 * @param pszName the name of the extension being fetched.
10720
 * @param pszDefault the value to return if the extension is not found.
10721
 *
10722
 * @return node value if successful or pszDefault on failure.
10723
 */
10724
10725
const char *OGRSpatialReference::GetExtension(const char *pszTargetKey,
10726
                                              const char *pszName,
10727
                                              const char *pszDefault) const
10728
10729
0
{
10730
0
    TAKE_OPTIONAL_LOCK();
10731
10732
    /* -------------------------------------------------------------------- */
10733
    /*      Find the target node.                                           */
10734
    /* -------------------------------------------------------------------- */
10735
0
    const OGR_SRSNode *poNode =
10736
0
        pszTargetKey == nullptr ? GetRoot() : GetAttrNode(pszTargetKey);
10737
10738
0
    if (poNode == nullptr)
10739
0
        return nullptr;
10740
10741
    /* -------------------------------------------------------------------- */
10742
    /*      Fetch matching EXTENSION if there is one.                       */
10743
    /* -------------------------------------------------------------------- */
10744
0
    for (int i = poNode->GetChildCount() - 1; i >= 0; i--)
10745
0
    {
10746
0
        const OGR_SRSNode *poChild = poNode->GetChild(i);
10747
10748
0
        if (EQUAL(poChild->GetValue(), "EXTENSION") &&
10749
0
            poChild->GetChildCount() >= 2)
10750
0
        {
10751
0
            if (EQUAL(poChild->GetChild(0)->GetValue(), pszName))
10752
0
                return poChild->GetChild(1)->GetValue();
10753
0
        }
10754
0
    }
10755
10756
0
    return pszDefault;
10757
0
}
10758
10759
/************************************************************************/
10760
/*                            SetExtension()                            */
10761
/************************************************************************/
10762
/**
10763
 * \brief Set extension value.
10764
 *
10765
 * Set the value of the named EXTENSION item for the identified
10766
 * target node.
10767
 *
10768
 * @param pszTargetKey the name or path to the parent node of the EXTENSION.
10769
 * @param pszName the name of the extension being fetched.
10770
 * @param pszValue the value to set
10771
 *
10772
 * @return OGRERR_NONE on success
10773
 */
10774
10775
OGRErr OGRSpatialReference::SetExtension(const char *pszTargetKey,
10776
                                         const char *pszName,
10777
                                         const char *pszValue)
10778
10779
0
{
10780
0
    TAKE_OPTIONAL_LOCK();
10781
10782
    /* -------------------------------------------------------------------- */
10783
    /*      Find the target node.                                           */
10784
    /* -------------------------------------------------------------------- */
10785
0
    OGR_SRSNode *poNode = nullptr;
10786
10787
0
    if (pszTargetKey == nullptr)
10788
0
        poNode = GetRoot();
10789
0
    else
10790
0
        poNode = GetAttrNode(pszTargetKey);
10791
10792
0
    if (poNode == nullptr)
10793
0
        return OGRERR_FAILURE;
10794
10795
    /* -------------------------------------------------------------------- */
10796
    /*      Fetch matching EXTENSION if there is one.                       */
10797
    /* -------------------------------------------------------------------- */
10798
0
    for (int i = poNode->GetChildCount() - 1; i >= 0; i--)
10799
0
    {
10800
0
        OGR_SRSNode *poChild = poNode->GetChild(i);
10801
10802
0
        if (EQUAL(poChild->GetValue(), "EXTENSION") &&
10803
0
            poChild->GetChildCount() >= 2)
10804
0
        {
10805
0
            if (EQUAL(poChild->GetChild(0)->GetValue(), pszName))
10806
0
            {
10807
0
                poChild->GetChild(1)->SetValue(pszValue);
10808
0
                return OGRERR_NONE;
10809
0
            }
10810
0
        }
10811
0
    }
10812
10813
    /* -------------------------------------------------------------------- */
10814
    /*      Create a new EXTENSION node.                                    */
10815
    /* -------------------------------------------------------------------- */
10816
0
    OGR_SRSNode *poAuthNode = new OGR_SRSNode("EXTENSION");
10817
0
    poAuthNode->AddChild(new OGR_SRSNode(pszName));
10818
0
    poAuthNode->AddChild(new OGR_SRSNode(pszValue));
10819
10820
0
    poNode->AddChild(poAuthNode);
10821
10822
0
    return OGRERR_NONE;
10823
0
}
10824
10825
/************************************************************************/
10826
/*                             OSRCleanup()                             */
10827
/************************************************************************/
10828
10829
static void CleanupSRSWGS84Mutex();
10830
10831
/**
10832
 * \brief Cleanup cached SRS related memory.
10833
 *
10834
 * This function will attempt to cleanup any cache spatial reference
10835
 * related information, such as cached tables of coordinate systems.
10836
 *
10837
 * This function should not be called concurrently with any other GDAL/OGR
10838
 * function. It is meant at being called once before process termination
10839
 * (typically from the main thread). CPLCleanupTLS() might be used to clean
10840
 * thread-specific resources before thread termination.
10841
 */
10842
void OSRCleanup(void)
10843
10844
0
{
10845
0
    OGRCTDumpStatistics();
10846
0
    CSVDeaccess(nullptr);
10847
0
    CleanupSRSWGS84Mutex();
10848
0
    OSRCTCleanCache();
10849
0
    OSRCleanupTLSContext();
10850
0
}
10851
10852
/************************************************************************/
10853
/*                              GetAxesCount()                          */
10854
/************************************************************************/
10855
10856
/**
10857
 * \brief Return the number of axis of the coordinate system of the CRS.
10858
 *
10859
 * @since GDAL 3.0
10860
 */
10861
int OGRSpatialReference::GetAxesCount() const
10862
0
{
10863
0
    TAKE_OPTIONAL_LOCK();
10864
10865
0
    int axisCount = 0;
10866
0
    d->refreshProjObj();
10867
0
    if (d->m_pj_crs == nullptr)
10868
0
    {
10869
0
        return 0;
10870
0
    }
10871
0
    d->demoteFromBoundCRS();
10872
0
    auto ctxt = d->getPROJContext();
10873
0
    if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
10874
0
    {
10875
0
        for (int i = 0;; i++)
10876
0
        {
10877
0
            auto subCRS = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, i);
10878
0
            if (!subCRS)
10879
0
                break;
10880
0
            if (proj_get_type(subCRS) == PJ_TYPE_BOUND_CRS)
10881
0
            {
10882
0
                auto baseCRS = proj_get_source_crs(ctxt, subCRS);
10883
0
                if (baseCRS)
10884
0
                {
10885
0
                    proj_destroy(subCRS);
10886
0
                    subCRS = baseCRS;
10887
0
                }
10888
0
            }
10889
0
            auto cs = proj_crs_get_coordinate_system(ctxt, subCRS);
10890
0
            if (cs)
10891
0
            {
10892
0
                axisCount += proj_cs_get_axis_count(ctxt, cs);
10893
0
                proj_destroy(cs);
10894
0
            }
10895
0
            proj_destroy(subCRS);
10896
0
        }
10897
0
    }
10898
0
    else
10899
0
    {
10900
0
        auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
10901
0
        if (cs)
10902
0
        {
10903
0
            axisCount = proj_cs_get_axis_count(ctxt, cs);
10904
0
            proj_destroy(cs);
10905
0
        }
10906
0
    }
10907
0
    d->undoDemoteFromBoundCRS();
10908
0
    return axisCount;
10909
0
}
10910
10911
/************************************************************************/
10912
/*                           OSRGetAxesCount()                          */
10913
/************************************************************************/
10914
10915
/**
10916
 * \brief Return the number of axis of the coordinate system of the CRS.
10917
 *
10918
 * This method is the equivalent of the C++ method
10919
 * OGRSpatialReference::GetAxesCount()
10920
 *
10921
 * @since GDAL 3.1
10922
 */
10923
int OSRGetAxesCount(OGRSpatialReferenceH hSRS)
10924
10925
0
{
10926
0
    VALIDATE_POINTER1(hSRS, "OSRGetAxesCount", 0);
10927
10928
0
    return ToPointer(hSRS)->GetAxesCount();
10929
0
}
10930
10931
/************************************************************************/
10932
/*                              GetAxis()                               */
10933
/************************************************************************/
10934
10935
/**
10936
 * \brief Fetch the orientation of one axis.
10937
 *
10938
 * Fetches the request axis (iAxis - zero based) from the
10939
 * indicated portion of the coordinate system (pszTargetKey) which
10940
 * should be either "GEOGCS" or "PROJCS".
10941
 *
10942
 * No CPLError is issued on routine failures (such as not finding the AXIS).
10943
 *
10944
 * This method is equivalent to the C function OSRGetAxis().
10945
 *
10946
 * @param pszTargetKey the coordinate system part to query ("PROJCS" or
10947
 * "GEOGCS").
10948
 * @param iAxis the axis to query (0 for first, 1 for second, 2 for third).
10949
 * @param peOrientation location into which to place the fetch orientation, may
10950
 * be NULL.
10951
 * @param pdfConvUnit (GDAL >= 3.4) Location into which to place axis conversion
10952
 * factor. May be NULL. Only set if pszTargetKey == NULL
10953
 *
10954
 * @return the name of the axis or NULL on failure.
10955
 */
10956
10957
const char *OGRSpatialReference::GetAxis(const char *pszTargetKey, int iAxis,
10958
                                         OGRAxisOrientation *peOrientation,
10959
                                         double *pdfConvUnit) const
10960
10961
0
{
10962
0
    TAKE_OPTIONAL_LOCK();
10963
10964
0
    if (peOrientation != nullptr)
10965
0
        *peOrientation = OAO_Other;
10966
0
    if (pdfConvUnit != nullptr)
10967
0
        *pdfConvUnit = 0;
10968
10969
0
    d->refreshProjObj();
10970
0
    if (d->m_pj_crs == nullptr)
10971
0
    {
10972
0
        return nullptr;
10973
0
    }
10974
10975
0
    pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
10976
0
    if (pszTargetKey == nullptr && iAxis <= 2)
10977
0
    {
10978
0
        auto ctxt = d->getPROJContext();
10979
10980
0
        int iAxisModified = iAxis;
10981
10982
0
        d->demoteFromBoundCRS();
10983
10984
0
        PJ *cs = nullptr;
10985
0
        if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
10986
0
        {
10987
0
            auto horizCRS = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
10988
0
            if (horizCRS)
10989
0
            {
10990
0
                if (proj_get_type(horizCRS) == PJ_TYPE_BOUND_CRS)
10991
0
                {
10992
0
                    auto baseCRS = proj_get_source_crs(ctxt, horizCRS);
10993
0
                    if (baseCRS)
10994
0
                    {
10995
0
                        proj_destroy(horizCRS);
10996
0
                        horizCRS = baseCRS;
10997
0
                    }
10998
0
                }
10999
0
                cs = proj_crs_get_coordinate_system(ctxt, horizCRS);
11000
0
                proj_destroy(horizCRS);
11001
0
                if (cs)
11002
0
                {
11003
0
                    if (iAxisModified >= proj_cs_get_axis_count(ctxt, cs))
11004
0
                    {
11005
0
                        iAxisModified -= proj_cs_get_axis_count(ctxt, cs);
11006
0
                        proj_destroy(cs);
11007
0
                        cs = nullptr;
11008
0
                    }
11009
0
                }
11010
0
            }
11011
11012
0
            if (cs == nullptr)
11013
0
            {
11014
0
                auto vertCRS = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 1);
11015
0
                if (vertCRS)
11016
0
                {
11017
0
                    if (proj_get_type(vertCRS) == PJ_TYPE_BOUND_CRS)
11018
0
                    {
11019
0
                        auto baseCRS = proj_get_source_crs(ctxt, vertCRS);
11020
0
                        if (baseCRS)
11021
0
                        {
11022
0
                            proj_destroy(vertCRS);
11023
0
                            vertCRS = baseCRS;
11024
0
                        }
11025
0
                    }
11026
11027
0
                    cs = proj_crs_get_coordinate_system(ctxt, vertCRS);
11028
0
                    proj_destroy(vertCRS);
11029
0
                }
11030
0
            }
11031
0
        }
11032
0
        else
11033
0
        {
11034
0
            cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
11035
0
        }
11036
11037
0
        if (cs)
11038
0
        {
11039
0
            const char *pszName = nullptr;
11040
0
            const char *pszOrientation = nullptr;
11041
0
            double dfConvFactor = 0.0;
11042
0
            proj_cs_get_axis_info(ctxt, cs, iAxisModified, &pszName, nullptr,
11043
0
                                  &pszOrientation, &dfConvFactor, nullptr,
11044
0
                                  nullptr, nullptr);
11045
11046
0
            if (pdfConvUnit != nullptr)
11047
0
            {
11048
0
                *pdfConvUnit = dfConvFactor;
11049
0
            }
11050
11051
0
            if (pszName && pszOrientation)
11052
0
            {
11053
0
                d->m_osAxisName[iAxis] = pszName;
11054
0
                if (peOrientation)
11055
0
                {
11056
0
                    if (EQUAL(pszOrientation, "NORTH"))
11057
0
                        *peOrientation = OAO_North;
11058
0
                    else if (EQUAL(pszOrientation, "EAST"))
11059
0
                        *peOrientation = OAO_East;
11060
0
                    else if (EQUAL(pszOrientation, "SOUTH"))
11061
0
                        *peOrientation = OAO_South;
11062
0
                    else if (EQUAL(pszOrientation, "WEST"))
11063
0
                        *peOrientation = OAO_West;
11064
0
                    else if (EQUAL(pszOrientation, "UP"))
11065
0
                        *peOrientation = OAO_Up;
11066
0
                    else if (EQUAL(pszOrientation, "DOWN"))
11067
0
                        *peOrientation = OAO_Down;
11068
0
                }
11069
0
                proj_destroy(cs);
11070
0
                d->undoDemoteFromBoundCRS();
11071
0
                return d->m_osAxisName[iAxis].c_str();
11072
0
            }
11073
0
            proj_destroy(cs);
11074
0
        }
11075
0
        d->undoDemoteFromBoundCRS();
11076
0
    }
11077
11078
    /* -------------------------------------------------------------------- */
11079
    /*      Find the target node.                                           */
11080
    /* -------------------------------------------------------------------- */
11081
0
    const OGR_SRSNode *poNode = nullptr;
11082
11083
0
    if (pszTargetKey == nullptr)
11084
0
        poNode = GetRoot();
11085
0
    else
11086
0
        poNode = GetAttrNode(pszTargetKey);
11087
11088
0
    if (poNode == nullptr)
11089
0
        return nullptr;
11090
11091
    /* -------------------------------------------------------------------- */
11092
    /*      Find desired child AXIS.                                        */
11093
    /* -------------------------------------------------------------------- */
11094
0
    const OGR_SRSNode *poAxis = nullptr;
11095
0
    const int nChildCount = poNode->GetChildCount();
11096
11097
0
    for (int iChild = 0; iChild < nChildCount; iChild++)
11098
0
    {
11099
0
        const OGR_SRSNode *poChild = poNode->GetChild(iChild);
11100
11101
0
        if (!EQUAL(poChild->GetValue(), "AXIS"))
11102
0
            continue;
11103
11104
0
        if (iAxis == 0)
11105
0
        {
11106
0
            poAxis = poChild;
11107
0
            break;
11108
0
        }
11109
0
        iAxis--;
11110
0
    }
11111
11112
0
    if (poAxis == nullptr)
11113
0
        return nullptr;
11114
11115
0
    if (poAxis->GetChildCount() < 2)
11116
0
        return nullptr;
11117
11118
    /* -------------------------------------------------------------------- */
11119
    /*      Extract name and orientation if possible.                       */
11120
    /* -------------------------------------------------------------------- */
11121
0
    if (peOrientation != nullptr)
11122
0
    {
11123
0
        const char *pszOrientation = poAxis->GetChild(1)->GetValue();
11124
11125
0
        if (EQUAL(pszOrientation, "NORTH"))
11126
0
            *peOrientation = OAO_North;
11127
0
        else if (EQUAL(pszOrientation, "EAST"))
11128
0
            *peOrientation = OAO_East;
11129
0
        else if (EQUAL(pszOrientation, "SOUTH"))
11130
0
            *peOrientation = OAO_South;
11131
0
        else if (EQUAL(pszOrientation, "WEST"))
11132
0
            *peOrientation = OAO_West;
11133
0
        else if (EQUAL(pszOrientation, "UP"))
11134
0
            *peOrientation = OAO_Up;
11135
0
        else if (EQUAL(pszOrientation, "DOWN"))
11136
0
            *peOrientation = OAO_Down;
11137
0
        else if (EQUAL(pszOrientation, "OTHER"))
11138
0
            *peOrientation = OAO_Other;
11139
0
        else
11140
0
        {
11141
0
            CPLDebug("OSR", "Unrecognized orientation value '%s'.",
11142
0
                     pszOrientation);
11143
0
        }
11144
0
    }
11145
11146
0
    return poAxis->GetChild(0)->GetValue();
11147
0
}
11148
11149
/************************************************************************/
11150
/*                             OSRGetAxis()                             */
11151
/************************************************************************/
11152
11153
/**
11154
 * \brief Fetch the orientation of one axis.
11155
 *
11156
 * This method is the equivalent of the C++ method OGRSpatialReference::GetAxis
11157
 */
11158
const char *OSRGetAxis(OGRSpatialReferenceH hSRS, const char *pszTargetKey,
11159
                       int iAxis, OGRAxisOrientation *peOrientation)
11160
11161
0
{
11162
0
    VALIDATE_POINTER1(hSRS, "OSRGetAxis", nullptr);
11163
11164
0
    return ToPointer(hSRS)->GetAxis(pszTargetKey, iAxis, peOrientation);
11165
0
}
11166
11167
/************************************************************************/
11168
/*                         OSRAxisEnumToName()                          */
11169
/************************************************************************/
11170
11171
/**
11172
 * \brief Return the string representation for the OGRAxisOrientation
11173
 * enumeration.
11174
 *
11175
 * For example "NORTH" for OAO_North.
11176
 *
11177
 * @return an internal string
11178
 */
11179
const char *OSRAxisEnumToName(OGRAxisOrientation eOrientation)
11180
11181
0
{
11182
0
    if (eOrientation == OAO_North)
11183
0
        return "NORTH";
11184
0
    if (eOrientation == OAO_East)
11185
0
        return "EAST";
11186
0
    if (eOrientation == OAO_South)
11187
0
        return "SOUTH";
11188
0
    if (eOrientation == OAO_West)
11189
0
        return "WEST";
11190
0
    if (eOrientation == OAO_Up)
11191
0
        return "UP";
11192
0
    if (eOrientation == OAO_Down)
11193
0
        return "DOWN";
11194
0
    if (eOrientation == OAO_Other)
11195
0
        return "OTHER";
11196
11197
0
    return "UNKNOWN";
11198
0
}
11199
11200
/************************************************************************/
11201
/*                              SetAxes()                               */
11202
/************************************************************************/
11203
11204
/**
11205
 * \brief Set the axes for a coordinate system.
11206
 *
11207
 * Set the names, and orientations of the axes for either a projected
11208
 * (PROJCS) or geographic (GEOGCS) coordinate system.
11209
 *
11210
 * This method is equivalent to the C function OSRSetAxes().
11211
 *
11212
 * @param pszTargetKey either "PROJCS" or "GEOGCS", must already exist in SRS.
11213
 * @param pszXAxisName name of first axis, normally "Long" or "Easting".
11214
 * @param eXAxisOrientation normally OAO_East.
11215
 * @param pszYAxisName name of second axis, normally "Lat" or "Northing".
11216
 * @param eYAxisOrientation normally OAO_North.
11217
 *
11218
 * @return OGRERR_NONE on success or an error code.
11219
 */
11220
11221
OGRErr OGRSpatialReference::SetAxes(const char *pszTargetKey,
11222
                                    const char *pszXAxisName,
11223
                                    OGRAxisOrientation eXAxisOrientation,
11224
                                    const char *pszYAxisName,
11225
                                    OGRAxisOrientation eYAxisOrientation)
11226
11227
0
{
11228
0
    TAKE_OPTIONAL_LOCK();
11229
11230
    /* -------------------------------------------------------------------- */
11231
    /*      Find the target node.                                           */
11232
    /* -------------------------------------------------------------------- */
11233
0
    OGR_SRSNode *poNode = nullptr;
11234
11235
0
    if (pszTargetKey == nullptr)
11236
0
        poNode = GetRoot();
11237
0
    else
11238
0
        poNode = GetAttrNode(pszTargetKey);
11239
11240
0
    if (poNode == nullptr)
11241
0
        return OGRERR_FAILURE;
11242
11243
    /* -------------------------------------------------------------------- */
11244
    /*      Strip any existing AXIS children.                               */
11245
    /* -------------------------------------------------------------------- */
11246
0
    while (poNode->FindChild("AXIS") >= 0)
11247
0
        poNode->DestroyChild(poNode->FindChild("AXIS"));
11248
11249
    /* -------------------------------------------------------------------- */
11250
    /*      Insert desired axes                                             */
11251
    /* -------------------------------------------------------------------- */
11252
0
    OGR_SRSNode *poAxis = new OGR_SRSNode("AXIS");
11253
11254
0
    poAxis->AddChild(new OGR_SRSNode(pszXAxisName));
11255
0
    poAxis->AddChild(new OGR_SRSNode(OSRAxisEnumToName(eXAxisOrientation)));
11256
11257
0
    poNode->AddChild(poAxis);
11258
11259
0
    poAxis = new OGR_SRSNode("AXIS");
11260
11261
0
    poAxis->AddChild(new OGR_SRSNode(pszYAxisName));
11262
0
    poAxis->AddChild(new OGR_SRSNode(OSRAxisEnumToName(eYAxisOrientation)));
11263
11264
0
    poNode->AddChild(poAxis);
11265
11266
0
    return OGRERR_NONE;
11267
0
}
11268
11269
/************************************************************************/
11270
/*                             OSRSetAxes()                             */
11271
/************************************************************************/
11272
/**
11273
 * \brief Set the axes for a coordinate system.
11274
 *
11275
 * This method is the equivalent of the C++ method OGRSpatialReference::SetAxes
11276
 */
11277
OGRErr OSRSetAxes(OGRSpatialReferenceH hSRS, const char *pszTargetKey,
11278
                  const char *pszXAxisName,
11279
                  OGRAxisOrientation eXAxisOrientation,
11280
                  const char *pszYAxisName,
11281
                  OGRAxisOrientation eYAxisOrientation)
11282
0
{
11283
0
    VALIDATE_POINTER1(hSRS, "OSRSetAxes", OGRERR_FAILURE);
11284
11285
0
    return ToPointer(hSRS)->SetAxes(pszTargetKey, pszXAxisName,
11286
0
                                    eXAxisOrientation, pszYAxisName,
11287
0
                                    eYAxisOrientation);
11288
0
}
11289
11290
/************************************************************************/
11291
/*                       OSRExportToMICoordSys()                        */
11292
/************************************************************************/
11293
/**
11294
 * \brief Export coordinate system in Mapinfo style CoordSys format.
11295
 *
11296
 * This method is the equivalent of the C++ method
11297
 * OGRSpatialReference::exportToMICoordSys
11298
 */
11299
OGRErr OSRExportToMICoordSys(OGRSpatialReferenceH hSRS, char **ppszReturn)
11300
11301
0
{
11302
0
    VALIDATE_POINTER1(hSRS, "OSRExportToMICoordSys", OGRERR_FAILURE);
11303
11304
0
    *ppszReturn = nullptr;
11305
11306
0
    return ToPointer(hSRS)->exportToMICoordSys(ppszReturn);
11307
0
}
11308
11309
/************************************************************************/
11310
/*                         exportToMICoordSys()                         */
11311
/************************************************************************/
11312
11313
/**
11314
 * \brief Export coordinate system in Mapinfo style CoordSys format.
11315
 *
11316
 * Note that the returned WKT string should be freed with
11317
 * CPLFree() when no longer needed.  It is the responsibility of the caller.
11318
 *
11319
 * This method is the same as the C function OSRExportToMICoordSys().
11320
 *
11321
 * @param ppszResult pointer to which dynamically allocated Mapinfo CoordSys
11322
 * definition will be assigned.
11323
 *
11324
 * @return OGRERR_NONE on success, OGRERR_FAILURE on failure,
11325
 * OGRERR_UNSUPPORTED_OPERATION if MITAB library was not linked in.
11326
 */
11327
11328
OGRErr OGRSpatialReference::exportToMICoordSys(char **ppszResult) const
11329
11330
0
{
11331
0
    *ppszResult = MITABSpatialRef2CoordSys(this);
11332
0
    if (*ppszResult != nullptr && strlen(*ppszResult) > 0)
11333
0
        return OGRERR_NONE;
11334
11335
0
    return OGRERR_FAILURE;
11336
0
}
11337
11338
/************************************************************************/
11339
/*                       OSRImportFromMICoordSys()                      */
11340
/************************************************************************/
11341
/**
11342
 * \brief Import Mapinfo style CoordSys definition.
11343
 *
11344
 * This method is the equivalent of the C++ method
11345
 * OGRSpatialReference::importFromMICoordSys
11346
 */
11347
11348
OGRErr OSRImportFromMICoordSys(OGRSpatialReferenceH hSRS,
11349
                               const char *pszCoordSys)
11350
11351
0
{
11352
0
    VALIDATE_POINTER1(hSRS, "OSRImportFromMICoordSys", OGRERR_FAILURE);
11353
11354
0
    return ToPointer(hSRS)->importFromMICoordSys(pszCoordSys);
11355
0
}
11356
11357
/************************************************************************/
11358
/*                        importFromMICoordSys()                        */
11359
/************************************************************************/
11360
11361
/**
11362
 * \brief Import Mapinfo style CoordSys definition.
11363
 *
11364
 * The OGRSpatialReference is initialized from the passed Mapinfo style CoordSys
11365
 * definition string.
11366
 *
11367
 * This method is the equivalent of the C function OSRImportFromMICoordSys().
11368
 *
11369
 * @param pszCoordSys Mapinfo style CoordSys definition string.
11370
 *
11371
 * @return OGRERR_NONE on success, OGRERR_FAILURE on failure,
11372
 * OGRERR_UNSUPPORTED_OPERATION if MITAB library was not linked in.
11373
 */
11374
11375
OGRErr OGRSpatialReference::importFromMICoordSys(const char *pszCoordSys)
11376
11377
0
{
11378
0
    OGRSpatialReference *poResult = MITABCoordSys2SpatialRef(pszCoordSys);
11379
11380
0
    if (poResult == nullptr)
11381
0
        return OGRERR_FAILURE;
11382
11383
0
    *this = *poResult;
11384
0
    delete poResult;
11385
11386
0
    return OGRERR_NONE;
11387
0
}
11388
11389
/************************************************************************/
11390
/*                        OSRCalcInvFlattening()                        */
11391
/************************************************************************/
11392
11393
/**
11394
 * \brief Compute inverse flattening from semi-major and semi-minor axis
11395
 *
11396
 * @param dfSemiMajor Semi-major axis length.
11397
 * @param dfSemiMinor Semi-minor axis length.
11398
 *
11399
 * @return inverse flattening, or 0 if both axis are equal.
11400
 * @since GDAL 2.0
11401
 */
11402
11403
double OSRCalcInvFlattening(double dfSemiMajor, double dfSemiMinor)
11404
0
{
11405
0
    if (fabs(dfSemiMajor - dfSemiMinor) < 1e-1)
11406
0
        return 0;
11407
0
    if (dfSemiMajor <= 0 || dfSemiMinor <= 0 || dfSemiMinor > dfSemiMajor)
11408
0
    {
11409
0
        CPLError(CE_Failure, CPLE_IllegalArg,
11410
0
                 "OSRCalcInvFlattening(): Wrong input values");
11411
0
        return 0;
11412
0
    }
11413
11414
0
    return dfSemiMajor / (dfSemiMajor - dfSemiMinor);
11415
0
}
11416
11417
/************************************************************************/
11418
/*                        OSRCalcInvFlattening()                        */
11419
/************************************************************************/
11420
11421
/**
11422
 * \brief Compute semi-minor axis from semi-major axis and inverse flattening.
11423
 *
11424
 * @param dfSemiMajor Semi-major axis length.
11425
 * @param dfInvFlattening Inverse flattening or 0 for sphere.
11426
 *
11427
 * @return semi-minor axis
11428
 * @since GDAL 2.0
11429
 */
11430
11431
double OSRCalcSemiMinorFromInvFlattening(double dfSemiMajor,
11432
                                         double dfInvFlattening)
11433
0
{
11434
0
    if (fabs(dfInvFlattening) < 0.000000000001)
11435
0
        return dfSemiMajor;
11436
0
    if (dfSemiMajor <= 0.0 || dfInvFlattening <= 1.0)
11437
0
    {
11438
0
        CPLError(CE_Failure, CPLE_IllegalArg,
11439
0
                 "OSRCalcSemiMinorFromInvFlattening(): Wrong input values");
11440
0
        return dfSemiMajor;
11441
0
    }
11442
11443
0
    return dfSemiMajor * (1.0 - 1.0 / dfInvFlattening);
11444
0
}
11445
11446
/************************************************************************/
11447
/*                        GetWGS84SRS()                                 */
11448
/************************************************************************/
11449
11450
static OGRSpatialReference *poSRSWGS84 = nullptr;
11451
static CPLMutex *hMutex = nullptr;
11452
11453
/**
11454
 * \brief Returns an instance of a SRS object with WGS84 WKT.
11455
 *
11456
 * Note: the instance will have
11457
 * SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER)
11458
 *
11459
 * The reference counter of the returned object is not increased by this
11460
 * operation.
11461
 *
11462
 * @return instance.
11463
 * @since GDAL 2.0
11464
 */
11465
11466
OGRSpatialReference *OGRSpatialReference::GetWGS84SRS()
11467
0
{
11468
0
    CPLMutexHolderD(&hMutex);
11469
0
    if (poSRSWGS84 == nullptr)
11470
0
    {
11471
0
        poSRSWGS84 = new OGRSpatialReference(SRS_WKT_WGS84_LAT_LONG);
11472
0
        poSRSWGS84->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
11473
0
    }
11474
0
    return poSRSWGS84;
11475
0
}
11476
11477
/************************************************************************/
11478
/*                        CleanupSRSWGS84Mutex()                       */
11479
/************************************************************************/
11480
11481
static void CleanupSRSWGS84Mutex()
11482
0
{
11483
0
    if (hMutex != nullptr)
11484
0
    {
11485
0
        poSRSWGS84->Release();
11486
0
        poSRSWGS84 = nullptr;
11487
0
        CPLDestroyMutex(hMutex);
11488
0
        hMutex = nullptr;
11489
0
    }
11490
0
}
11491
11492
/************************************************************************/
11493
/*                         OSRImportFromProj4()                         */
11494
/************************************************************************/
11495
/**
11496
 * \brief Import PROJ coordinate string.
11497
 *
11498
 * This function is the same as OGRSpatialReference::importFromProj4().
11499
 */
11500
OGRErr OSRImportFromProj4(OGRSpatialReferenceH hSRS, const char *pszProj4)
11501
11502
0
{
11503
0
    VALIDATE_POINTER1(hSRS, "OSRImportFromProj4", OGRERR_FAILURE);
11504
11505
0
    return OGRSpatialReference::FromHandle(hSRS)->importFromProj4(pszProj4);
11506
0
}
11507
11508
/************************************************************************/
11509
/*                          importFromProj4()                           */
11510
/************************************************************************/
11511
11512
/**
11513
 * \brief Import PROJ coordinate string.
11514
 *
11515
 * The OGRSpatialReference is initialized from the passed PROJs style
11516
 * coordinate system string.
11517
 *
11518
 * Example:
11519
 *   pszProj4 = "+proj=utm +zone=11 +datum=WGS84"
11520
 *
11521
 * It is also possible to import "+init=epsg:n" style definitions. Those are
11522
 * a legacy syntax that should be avoided in the future. In particular they will
11523
 * result in CRS objects whose axis order might not correspond to the official
11524
 * EPSG axis order.
11525
 *
11526
 * This method is the equivalent of the C function OSRImportFromProj4().
11527
 *
11528
 * @param pszProj4 the PROJ style string.
11529
 *
11530
 * @return OGRERR_NONE on success or OGRERR_CORRUPT_DATA on failure.
11531
 */
11532
11533
OGRErr OGRSpatialReference::importFromProj4(const char *pszProj4)
11534
11535
0
{
11536
0
    TAKE_OPTIONAL_LOCK();
11537
11538
0
    if (strlen(pszProj4) >= 10000)
11539
0
    {
11540
0
        CPLError(CE_Failure, CPLE_AppDefined, "Too long PROJ string");
11541
0
        return OGRERR_CORRUPT_DATA;
11542
0
    }
11543
11544
    /* -------------------------------------------------------------------- */
11545
    /*      Clear any existing definition.                                  */
11546
    /* -------------------------------------------------------------------- */
11547
0
    Clear();
11548
11549
0
    CPLString osProj4(pszProj4);
11550
0
    if (osProj4.find("type=crs") == std::string::npos)
11551
0
    {
11552
0
        osProj4 += " +type=crs";
11553
0
    }
11554
11555
0
    if (osProj4.find("+init=epsg:") != std::string::npos &&
11556
0
        getenv("PROJ_USE_PROJ4_INIT_RULES") == nullptr)
11557
0
    {
11558
0
        CPLErrorOnce(CE_Warning, CPLE_AppDefined,
11559
0
                     "+init=epsg:XXXX syntax is deprecated. It might return "
11560
0
                     "a CRS with a non-EPSG compliant axis order.");
11561
0
    }
11562
0
    proj_context_use_proj4_init_rules(d->getPROJContext(), true);
11563
0
    d->setPjCRS(proj_create(d->getPROJContext(), osProj4.c_str()));
11564
0
    proj_context_use_proj4_init_rules(d->getPROJContext(), false);
11565
0
    return d->m_pj_crs ? OGRERR_NONE : OGRERR_CORRUPT_DATA;
11566
0
}
11567
11568
/************************************************************************/
11569
/*                          OSRExportToProj4()                          */
11570
/************************************************************************/
11571
/**
11572
 * \brief Export coordinate system in PROJ.4 legacy format.
11573
 *
11574
 * \warning Use of this function is discouraged. Its behavior in GDAL &gt;= 3 /
11575
 * PROJ &gt;= 6 is significantly different from earlier versions. In particular
11576
 * +datum will only encode WGS84, NAD27 and NAD83, and +towgs84/+nadgrids terms
11577
 * will be missing most of the time. PROJ strings to encode CRS should be
11578
 * considered as a legacy solution. Using a AUTHORITY:CODE or WKT representation
11579
 * is the recommended way.
11580
 *
11581
 * This function is the same as OGRSpatialReference::exportToProj4().
11582
 */
11583
OGRErr CPL_STDCALL OSRExportToProj4(OGRSpatialReferenceH hSRS,
11584
                                    char **ppszReturn)
11585
11586
0
{
11587
0
    VALIDATE_POINTER1(hSRS, "OSRExportToProj4", OGRERR_FAILURE);
11588
11589
0
    *ppszReturn = nullptr;
11590
11591
0
    return OGRSpatialReference::FromHandle(hSRS)->exportToProj4(ppszReturn);
11592
0
}
11593
11594
/************************************************************************/
11595
/*                           exportToProj4()                            */
11596
/************************************************************************/
11597
11598
/**
11599
 * \brief Export coordinate system in PROJ.4 legacy format.
11600
 *
11601
 * \warning Use of this function is discouraged. Its behavior in GDAL &gt;= 3 /
11602
 * PROJ &gt;= 6 is significantly different from earlier versions. In particular
11603
 * +datum will only encode WGS84, NAD27 and NAD83, and +towgs84/+nadgrids terms
11604
 * will be missing most of the time. PROJ strings to encode CRS should be
11605
 * considered as a a legacy solution. Using a AUTHORITY:CODE or WKT
11606
 * representation is the recommended way.
11607
 *
11608
 * Converts the loaded coordinate reference system into PROJ format
11609
 * to the extent possible.  The string returned in ppszProj4 should be
11610
 * deallocated by the caller with CPLFree() when no longer needed.
11611
 *
11612
 * LOCAL_CS coordinate systems are not translatable.  An empty string
11613
 * will be returned along with OGRERR_NONE.
11614
 *
11615
 * Special processing for Transverse Mercator:
11616
 * Starting with GDAL 3.0, if the OSR_USE_APPROX_TMERC configuration option is
11617
 * set to YES, the PROJ definition built from the SRS will use the +approx flag
11618
 * for the tmerc and utm projection methods, rather than the more accurate
11619
 * method.
11620
 *
11621
 * Starting with GDAL 3.0.3, this method will try to add a +towgs84 parameter,
11622
 * if there's none attached yet to the SRS and if the SRS has a EPSG code.
11623
 * See the AddGuessedTOWGS84() method for how this +towgs84 parameter may be
11624
 * added. This automatic addition may be disabled by setting the
11625
 * OSR_ADD_TOWGS84_ON_EXPORT_TO_PROJ4 configuration option to NO.
11626
 *
11627
 * This method is the equivalent of the C function OSRExportToProj4().
11628
 *
11629
 * @param ppszProj4 pointer to which dynamically allocated PROJ definition
11630
 * will be assigned.
11631
 *
11632
 * @return OGRERR_NONE on success or an error code on failure.
11633
 */
11634
11635
OGRErr OGRSpatialReference::exportToProj4(char **ppszProj4) const
11636
11637
0
{
11638
    // In the past calling this method was thread-safe, even if we never
11639
    // guaranteed it. Now proj_as_proj_string() will cache the result
11640
    // internally, so this is no longer thread-safe.
11641
0
    std::lock_guard oLock(d->m_mutex);
11642
11643
0
    d->refreshProjObj();
11644
0
    if (d->m_pj_crs == nullptr || d->m_pjType == PJ_TYPE_ENGINEERING_CRS)
11645
0
    {
11646
0
        *ppszProj4 = CPLStrdup("");
11647
0
        return OGRERR_FAILURE;
11648
0
    }
11649
11650
    // OSR_USE_ETMERC is here just for legacy
11651
0
    bool bForceApproxTMerc = false;
11652
0
    const char *pszUseETMERC = CPLGetConfigOption("OSR_USE_ETMERC", nullptr);
11653
0
    if (pszUseETMERC && pszUseETMERC[0])
11654
0
    {
11655
0
        CPLErrorOnce(CE_Warning, CPLE_AppDefined,
11656
0
                     "OSR_USE_ETMERC is a legacy configuration option, which "
11657
0
                     "now has only effect when set to NO (YES is the default). "
11658
0
                     "Use OSR_USE_APPROX_TMERC=YES instead");
11659
0
        bForceApproxTMerc = !CPLTestBool(pszUseETMERC);
11660
0
    }
11661
0
    else
11662
0
    {
11663
0
        const char *pszUseApproxTMERC =
11664
0
            CPLGetConfigOption("OSR_USE_APPROX_TMERC", nullptr);
11665
0
        if (pszUseApproxTMERC && pszUseApproxTMERC[0])
11666
0
        {
11667
0
            bForceApproxTMerc = CPLTestBool(pszUseApproxTMERC);
11668
0
        }
11669
0
    }
11670
0
    const char *options[] = {
11671
0
        bForceApproxTMerc ? "USE_APPROX_TMERC=YES" : nullptr, nullptr};
11672
11673
0
    const char *projString = proj_as_proj_string(
11674
0
        d->getPROJContext(), d->m_pj_crs, PJ_PROJ_4, options);
11675
11676
0
    PJ *boundCRS = nullptr;
11677
0
    if (projString &&
11678
0
        (strstr(projString, "+datum=") == nullptr ||
11679
0
         d->m_pjType == PJ_TYPE_COMPOUND_CRS) &&
11680
0
        CPLTestBool(
11681
0
            CPLGetConfigOption("OSR_ADD_TOWGS84_ON_EXPORT_TO_PROJ4", "YES")))
11682
0
    {
11683
0
        boundCRS = GDAL_proj_crs_create_bound_crs_to_WGS84(
11684
0
            d->getPROJContext(), d->m_pj_crs, true,
11685
0
            strstr(projString, "+datum=") == nullptr);
11686
0
        if (boundCRS)
11687
0
        {
11688
0
            projString = proj_as_proj_string(d->getPROJContext(), boundCRS,
11689
0
                                             PJ_PROJ_4, options);
11690
0
        }
11691
0
    }
11692
11693
0
    if (projString == nullptr)
11694
0
    {
11695
0
        *ppszProj4 = CPLStrdup("");
11696
0
        proj_destroy(boundCRS);
11697
0
        return OGRERR_FAILURE;
11698
0
    }
11699
0
    *ppszProj4 = CPLStrdup(projString);
11700
0
    proj_destroy(boundCRS);
11701
0
    char *pszTypeCrs = strstr(*ppszProj4, " +type=crs");
11702
0
    if (pszTypeCrs)
11703
0
        *pszTypeCrs = '\0';
11704
0
    return OGRERR_NONE;
11705
0
}
11706
11707
/************************************************************************/
11708
/*                            morphToESRI()                             */
11709
/************************************************************************/
11710
/**
11711
 * \brief Convert in place to ESRI WKT format.
11712
 *
11713
 * The value nodes of this coordinate system are modified in various manners
11714
 * more closely map onto the ESRI concept of WKT format.  This includes
11715
 * renaming a variety of projections and arguments, and stripping out
11716
 * nodes note recognised by ESRI (like AUTHORITY and AXIS).
11717
 *
11718
 * \note Since GDAL 3.0, this function has only user-visible effects at
11719
 * exportToWkt() time. It is recommended to use instead exportToWkt(char**,
11720
 * const char* const char*) const with options having FORMAT=WKT1_ESRI.
11721
 *
11722
 * This does the same as the C function OSRMorphToESRI().
11723
 *
11724
 * @return OGRERR_NONE unless something goes badly wrong.
11725
 * @deprecated
11726
 */
11727
11728
OGRErr OGRSpatialReference::morphToESRI()
11729
11730
0
{
11731
0
    TAKE_OPTIONAL_LOCK();
11732
11733
0
    d->refreshProjObj();
11734
0
    d->setMorphToESRI(true);
11735
11736
0
    return OGRERR_NONE;
11737
0
}
11738
11739
/************************************************************************/
11740
/*                           OSRMorphToESRI()                           */
11741
/************************************************************************/
11742
11743
/**
11744
 * \brief Convert in place to ESRI WKT format.
11745
 *
11746
 * This function is the same as the C++ method
11747
 * OGRSpatialReference::morphToESRI().
11748
 */
11749
OGRErr OSRMorphToESRI(OGRSpatialReferenceH hSRS)
11750
11751
0
{
11752
0
    VALIDATE_POINTER1(hSRS, "OSRMorphToESRI", OGRERR_FAILURE);
11753
11754
0
    return OGRSpatialReference::FromHandle(hSRS)->morphToESRI();
11755
0
}
11756
11757
/************************************************************************/
11758
/*                           morphFromESRI()                            */
11759
/************************************************************************/
11760
11761
/**
11762
 * \brief Convert in place from ESRI WKT format.
11763
 *
11764
 * The value notes of this coordinate system are modified in various manners
11765
 * to adhere more closely to the WKT standard.  This mostly involves
11766
 * translating a variety of ESRI names for projections, arguments and
11767
 * datums to "standard" names, as defined by Adam Gawne-Cain's reference
11768
 * translation of EPSG to WKT for the CT specification.
11769
 *
11770
 * \note Since GDAL 3.0, this function is essentially a no-operation, since
11771
 * morphing from ESRI is automatically done by importFromWkt(). Its only
11772
 * effect is to undo the effect of a potential prior call to morphToESRI().
11773
 *
11774
 * This does the same as the C function OSRMorphFromESRI().
11775
 *
11776
 * @return OGRERR_NONE unless something goes badly wrong.
11777
 * @deprecated
11778
 */
11779
11780
OGRErr OGRSpatialReference::morphFromESRI()
11781
11782
0
{
11783
0
    TAKE_OPTIONAL_LOCK();
11784
11785
0
    d->refreshProjObj();
11786
0
    d->setMorphToESRI(false);
11787
11788
0
    return OGRERR_NONE;
11789
0
}
11790
11791
/************************************************************************/
11792
/*                          OSRMorphFromESRI()                          */
11793
/************************************************************************/
11794
11795
/**
11796
 * \brief Convert in place from ESRI WKT format.
11797
 *
11798
 * This function is the same as the C++ method
11799
 * OGRSpatialReference::morphFromESRI().
11800
 */
11801
OGRErr OSRMorphFromESRI(OGRSpatialReferenceH hSRS)
11802
11803
0
{
11804
0
    VALIDATE_POINTER1(hSRS, "OSRMorphFromESRI", OGRERR_FAILURE);
11805
11806
0
    return OGRSpatialReference::FromHandle(hSRS)->morphFromESRI();
11807
0
}
11808
11809
/************************************************************************/
11810
/*                            FindMatches()                             */
11811
/************************************************************************/
11812
11813
/**
11814
 * \brief Try to identify a match between the passed SRS and a related SRS
11815
 * in a catalog.
11816
 *
11817
 * Matching may be partial, or may fail.
11818
 * Returned entries will be sorted by decreasing match confidence (first
11819
 * entry has the highest match confidence).
11820
 *
11821
 * The exact way matching is done may change in future versions. Starting with
11822
 * GDAL 3.0, it relies on PROJ' proj_identify() function.
11823
 *
11824
 * This method is the same as OSRFindMatches().
11825
 *
11826
 * @param papszOptions NULL terminated list of options or NULL
11827
 * @param pnEntries Output parameter. Number of values in the returned array.
11828
 * @param ppanMatchConfidence Output parameter (or NULL). *ppanMatchConfidence
11829
 * will be allocated to an array of *pnEntries whose values between 0 and 100
11830
 * indicate the confidence in the match. 100 is the highest confidence level.
11831
 * The array must be freed with CPLFree().
11832
 *
11833
 * @return an array of SRS that match the passed SRS, or NULL. Must be freed
11834
 * with OSRFreeSRSArray()
11835
 *
11836
 * @since GDAL 2.3
11837
 *
11838
 * @see OGRSpatialReference::FindBestMatch()
11839
 */
11840
OGRSpatialReferenceH *
11841
OGRSpatialReference::FindMatches(char **papszOptions, int *pnEntries,
11842
                                 int **ppanMatchConfidence) const
11843
0
{
11844
0
    TAKE_OPTIONAL_LOCK();
11845
11846
0
    CPL_IGNORE_RET_VAL(papszOptions);
11847
11848
0
    if (pnEntries)
11849
0
        *pnEntries = 0;
11850
0
    if (ppanMatchConfidence)
11851
0
        *ppanMatchConfidence = nullptr;
11852
11853
0
    d->refreshProjObj();
11854
0
    if (!d->m_pj_crs)
11855
0
        return nullptr;
11856
11857
0
    int *panConfidence = nullptr;
11858
0
    auto ctxt = d->getPROJContext();
11859
0
    auto list =
11860
0
        proj_identify(ctxt, d->m_pj_crs, nullptr, nullptr, &panConfidence);
11861
0
    if (!list)
11862
0
        return nullptr;
11863
11864
0
    const int nMatches = proj_list_get_count(list);
11865
11866
0
    if (pnEntries)
11867
0
        *pnEntries = static_cast<int>(nMatches);
11868
0
    OGRSpatialReferenceH *pahRet = static_cast<OGRSpatialReferenceH *>(
11869
0
        CPLCalloc(sizeof(OGRSpatialReferenceH), nMatches + 1));
11870
0
    if (ppanMatchConfidence)
11871
0
    {
11872
0
        *ppanMatchConfidence =
11873
0
            static_cast<int *>(CPLMalloc(sizeof(int) * (nMatches + 1)));
11874
0
    }
11875
11876
0
    bool bSortAgain = false;
11877
11878
0
    for (int i = 0; i < nMatches; i++)
11879
0
    {
11880
0
        PJ *obj = proj_list_get(ctxt, list, i);
11881
0
        CPLAssert(obj);
11882
0
        OGRSpatialReference *poSRS = new OGRSpatialReference();
11883
0
        poSRS->d->setPjCRS(obj);
11884
0
        pahRet[i] = ToHandle(poSRS);
11885
11886
        // Identify matches that only differ by axis order
11887
0
        if (panConfidence[i] == 50 && GetAxesCount() == 2 &&
11888
0
            poSRS->GetAxesCount() == 2 &&
11889
0
            GetDataAxisToSRSAxisMapping() == std::vector<int>{1, 2})
11890
0
        {
11891
0
            OGRAxisOrientation eThisAxis0 = OAO_Other;
11892
0
            OGRAxisOrientation eThisAxis1 = OAO_Other;
11893
0
            OGRAxisOrientation eSRSAxis0 = OAO_Other;
11894
0
            OGRAxisOrientation eSRSAxis1 = OAO_Other;
11895
0
            GetAxis(nullptr, 0, &eThisAxis0);
11896
0
            GetAxis(nullptr, 1, &eThisAxis1);
11897
0
            poSRS->GetAxis(nullptr, 0, &eSRSAxis0);
11898
0
            poSRS->GetAxis(nullptr, 1, &eSRSAxis1);
11899
0
            if (eThisAxis0 == OAO_East && eThisAxis1 == OAO_North &&
11900
0
                eSRSAxis0 == OAO_North && eSRSAxis1 == OAO_East)
11901
0
            {
11902
0
                auto pj_crs_normalized =
11903
0
                    proj_normalize_for_visualization(ctxt, poSRS->d->m_pj_crs);
11904
0
                if (pj_crs_normalized)
11905
0
                {
11906
0
                    if (proj_is_equivalent_to(d->m_pj_crs, pj_crs_normalized,
11907
0
                                              PJ_COMP_EQUIVALENT))
11908
0
                    {
11909
0
                        bSortAgain = true;
11910
0
                        panConfidence[i] = 90;
11911
0
                        poSRS->SetDataAxisToSRSAxisMapping({2, 1});
11912
0
                    }
11913
0
                    proj_destroy(pj_crs_normalized);
11914
0
                }
11915
0
            }
11916
0
        }
11917
11918
0
        if (ppanMatchConfidence)
11919
0
            (*ppanMatchConfidence)[i] = panConfidence[i];
11920
0
    }
11921
11922
0
    if (bSortAgain)
11923
0
    {
11924
0
        std::vector<int> anIndices;
11925
0
        for (int i = 0; i < nMatches; ++i)
11926
0
            anIndices.push_back(i);
11927
11928
0
        std::stable_sort(anIndices.begin(), anIndices.end(),
11929
0
                         [&panConfidence](int i, int j)
11930
0
                         { return panConfidence[i] > panConfidence[j]; });
11931
11932
0
        OGRSpatialReferenceH *pahRetSorted =
11933
0
            static_cast<OGRSpatialReferenceH *>(
11934
0
                CPLCalloc(sizeof(OGRSpatialReferenceH), nMatches + 1));
11935
0
        for (int i = 0; i < nMatches; ++i)
11936
0
        {
11937
0
            pahRetSorted[i] = pahRet[anIndices[i]];
11938
0
            if (ppanMatchConfidence)
11939
0
                (*ppanMatchConfidence)[i] = panConfidence[anIndices[i]];
11940
0
        }
11941
0
        CPLFree(pahRet);
11942
0
        pahRet = pahRetSorted;
11943
0
    }
11944
11945
0
    pahRet[nMatches] = nullptr;
11946
0
    proj_list_destroy(list);
11947
0
    proj_int_list_destroy(panConfidence);
11948
11949
0
    return pahRet;
11950
0
}
11951
11952
/************************************************************************/
11953
/*                          importFromEPSGA()                           */
11954
/************************************************************************/
11955
11956
/**
11957
 * \brief  Initialize SRS based on EPSG geographic, projected or vertical CRS
11958
 * code.
11959
 *
11960
 * This method will initialize the spatial reference based on the
11961
 * passed in EPSG CRS code found in the PROJ database.
11962
 *
11963
 * Since GDAL 3.0, this method is identical to importFromEPSG().
11964
 *
11965
 * Before GDAL 3.0.3, this method would try to attach a 3-parameter or
11966
 * 7-parameter Helmert transformation to WGS84 when there is one and only one
11967
 * such method available for the CRS. This behavior might not always be
11968
 * desirable, so starting with GDAL 3.0.3, this is no longer done unless
11969
 * the OSR_ADD_TOWGS84_ON_IMPORT_FROM_EPSG configuration option is set to YES.
11970
 * The AddGuessedTOWGS84() method can also be used for that purpose.
11971
 *
11972
 * The method will also by default substitute a deprecated EPSG code by its
11973
 * non-deprecated replacement. If this behavior is not desired, the
11974
 * OSR_USE_NON_DEPRECATED configuration option can be set to NO.
11975
 *
11976
 * This method is the same as the C function OSRImportFromEPSGA().
11977
 *
11978
 * @param nCode a CRS code.
11979
 *
11980
 * @return OGRERR_NONE on success, or an error code on failure.
11981
 */
11982
11983
OGRErr OGRSpatialReference::importFromEPSGA(int nCode)
11984
11985
0
{
11986
0
    TAKE_OPTIONAL_LOCK();
11987
11988
0
    Clear();
11989
11990
0
    const char *pszUseNonDeprecated =
11991
0
        CPLGetConfigOption("OSR_USE_NON_DEPRECATED", nullptr);
11992
0
    const bool bUseNonDeprecated =
11993
0
        CPLTestBool(pszUseNonDeprecated ? pszUseNonDeprecated : "YES");
11994
0
    const bool bAddTOWGS84 = CPLTestBool(
11995
0
        CPLGetConfigOption("OSR_ADD_TOWGS84_ON_IMPORT_FROM_EPSG", "NO"));
11996
0
    auto tlsCache = OSRGetProjTLSCache();
11997
0
    if (tlsCache)
11998
0
    {
11999
0
        auto cachedObj =
12000
0
            tlsCache->GetPJForEPSGCode(nCode, bUseNonDeprecated, bAddTOWGS84);
12001
0
        if (cachedObj)
12002
0
        {
12003
0
            d->setPjCRS(cachedObj);
12004
0
            return OGRERR_NONE;
12005
0
        }
12006
0
    }
12007
12008
0
    CPLString osCode;
12009
0
    osCode.Printf("%d", nCode);
12010
0
    PJ *obj;
12011
0
    constexpr int FIRST_NON_DEPRECATED_ESRI_CODE = 53001;
12012
0
    if (nCode < FIRST_NON_DEPRECATED_ESRI_CODE)
12013
0
    {
12014
0
        obj = proj_create_from_database(d->getPROJContext(), "EPSG",
12015
0
                                        osCode.c_str(), PJ_CATEGORY_CRS, true,
12016
0
                                        nullptr);
12017
0
        if (!obj)
12018
0
        {
12019
0
            return OGRERR_FAILURE;
12020
0
        }
12021
0
    }
12022
0
    else
12023
0
    {
12024
        // Likely to be an ESRI CRS...
12025
0
        CPLErr eLastErrorType = CE_None;
12026
0
        CPLErrorNum eLastErrorNum = CPLE_None;
12027
0
        std::string osLastErrorMsg;
12028
0
        bool bIsESRI = false;
12029
0
        {
12030
0
            CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
12031
0
            CPLErrorReset();
12032
0
            obj = proj_create_from_database(d->getPROJContext(), "EPSG",
12033
0
                                            osCode.c_str(), PJ_CATEGORY_CRS,
12034
0
                                            true, nullptr);
12035
0
            if (!obj)
12036
0
            {
12037
0
                eLastErrorType = CPLGetLastErrorType();
12038
0
                eLastErrorNum = CPLGetLastErrorNo();
12039
0
                osLastErrorMsg = CPLGetLastErrorMsg();
12040
0
                obj = proj_create_from_database(d->getPROJContext(), "ESRI",
12041
0
                                                osCode.c_str(), PJ_CATEGORY_CRS,
12042
0
                                                true, nullptr);
12043
0
                if (obj)
12044
0
                    bIsESRI = true;
12045
0
            }
12046
0
        }
12047
0
        if (!obj)
12048
0
        {
12049
0
            if (eLastErrorType != CE_None)
12050
0
                CPLError(eLastErrorType, eLastErrorNum, "%s",
12051
0
                         osLastErrorMsg.c_str());
12052
0
            return OGRERR_FAILURE;
12053
0
        }
12054
0
        if (bIsESRI)
12055
0
        {
12056
0
            CPLError(CE_Warning, CPLE_AppDefined,
12057
0
                     "EPSG:%d is not a valid CRS code, but ESRI:%d is. "
12058
0
                     "Assuming ESRI:%d was meant",
12059
0
                     nCode, nCode, nCode);
12060
0
        }
12061
0
    }
12062
12063
0
    if (bUseNonDeprecated && proj_is_deprecated(obj))
12064
0
    {
12065
0
        auto list = proj_get_non_deprecated(d->getPROJContext(), obj);
12066
0
        if (list)
12067
0
        {
12068
0
            const auto count = proj_list_get_count(list);
12069
0
            if (count == 1)
12070
0
            {
12071
0
                auto nonDeprecated =
12072
0
                    proj_list_get(d->getPROJContext(), list, 0);
12073
0
                if (nonDeprecated)
12074
0
                {
12075
0
                    if (pszUseNonDeprecated == nullptr)
12076
0
                    {
12077
0
                        const char *pszNewAuth =
12078
0
                            proj_get_id_auth_name(nonDeprecated, 0);
12079
0
                        const char *pszNewCode =
12080
0
                            proj_get_id_code(nonDeprecated, 0);
12081
0
                        CPLError(CE_Warning, CPLE_AppDefined,
12082
0
                                 "CRS EPSG:%d is deprecated. "
12083
0
                                 "Its non-deprecated replacement %s:%s "
12084
0
                                 "will be used instead. "
12085
0
                                 "To use the original CRS, set the "
12086
0
                                 "OSR_USE_NON_DEPRECATED "
12087
0
                                 "configuration option to NO.",
12088
0
                                 nCode, pszNewAuth ? pszNewAuth : "(null)",
12089
0
                                 pszNewCode ? pszNewCode : "(null)");
12090
0
                    }
12091
0
                    proj_destroy(obj);
12092
0
                    obj = nonDeprecated;
12093
0
                }
12094
0
            }
12095
0
        }
12096
0
        proj_list_destroy(list);
12097
0
    }
12098
12099
0
    if (bAddTOWGS84)
12100
0
    {
12101
0
        auto boundCRS = proj_crs_create_bound_crs_to_WGS84(d->getPROJContext(),
12102
0
                                                           obj, nullptr);
12103
0
        if (boundCRS)
12104
0
        {
12105
0
            proj_destroy(obj);
12106
0
            obj = boundCRS;
12107
0
        }
12108
0
    }
12109
12110
0
    d->setPjCRS(obj);
12111
12112
0
    if (tlsCache)
12113
0
    {
12114
0
        tlsCache->CachePJForEPSGCode(nCode, bUseNonDeprecated, bAddTOWGS84,
12115
0
                                     obj);
12116
0
    }
12117
12118
0
    return OGRERR_NONE;
12119
0
}
12120
12121
/************************************************************************/
12122
/*                          AddGuessedTOWGS84()                         */
12123
/************************************************************************/
12124
12125
/**
12126
 * \brief  Try to add a a 3-parameter or 7-parameter Helmert transformation
12127
 * to WGS84.
12128
 *
12129
 * This method try to attach a 3-parameter or 7-parameter Helmert transformation
12130
 * to WGS84 when there is one and only one such method available for the CRS.
12131
 * Note: this is more restrictive to how GDAL < 3 worked.
12132
 *
12133
 * This method is the same as the C function OSRAddGuessedTOWGS84().
12134
 *
12135
 * @return OGRERR_NONE on success, or an error code on failure (the CRS has
12136
 * already a transformation to WGS84 or none matching could be found).
12137
 *
12138
 * @since GDAL 3.0.3
12139
 */
12140
OGRErr OGRSpatialReference::AddGuessedTOWGS84()
12141
0
{
12142
0
    TAKE_OPTIONAL_LOCK();
12143
12144
0
    d->refreshProjObj();
12145
0
    if (!d->m_pj_crs)
12146
0
        return OGRERR_FAILURE;
12147
0
    auto boundCRS = GDAL_proj_crs_create_bound_crs_to_WGS84(
12148
0
        d->getPROJContext(), d->m_pj_crs, false, true);
12149
0
    if (!boundCRS)
12150
0
    {
12151
0
        return OGRERR_FAILURE;
12152
0
    }
12153
0
    d->setPjCRS(boundCRS);
12154
0
    return OGRERR_NONE;
12155
0
}
12156
12157
/************************************************************************/
12158
/*                         OSRImportFromEPSGA()                         */
12159
/************************************************************************/
12160
12161
/**
12162
 * \brief  Try to add a a 3-parameter or 7-parameter Helmert transformation
12163
 * to WGS84.
12164
 *
12165
 * This function is the same as OGRSpatialReference::AddGuessedTOWGS84().
12166
 *
12167
 * @since GDAL 3.0.3
12168
 */
12169
12170
OGRErr OSRAddGuessedTOWGS84(OGRSpatialReferenceH hSRS)
12171
12172
0
{
12173
0
    VALIDATE_POINTER1(hSRS, "OSRAddGuessedTOWGS84", OGRERR_FAILURE);
12174
12175
0
    return OGRSpatialReference::FromHandle(hSRS)->AddGuessedTOWGS84();
12176
0
}
12177
12178
/************************************************************************/
12179
/*                         OSRImportFromEPSGA()                         */
12180
/************************************************************************/
12181
12182
/**
12183
 * \brief  Initialize SRS based on EPSG geographic, projected or vertical CRS
12184
 * code.
12185
 *
12186
 * This function is the same as OGRSpatialReference::importFromEPSGA().
12187
 */
12188
12189
OGRErr CPL_STDCALL OSRImportFromEPSGA(OGRSpatialReferenceH hSRS, int nCode)
12190
12191
0
{
12192
0
    VALIDATE_POINTER1(hSRS, "OSRImportFromEPSGA", OGRERR_FAILURE);
12193
12194
0
    return OGRSpatialReference::FromHandle(hSRS)->importFromEPSGA(nCode);
12195
0
}
12196
12197
/************************************************************************/
12198
/*                           importFromEPSG()                           */
12199
/************************************************************************/
12200
12201
/**
12202
 * \brief  Initialize SRS based on EPSG geographic, projected or vertical CRS
12203
 * code.
12204
 *
12205
 * This method will initialize the spatial reference based on the
12206
 * passed in EPSG CRS code found in the PROJ database.
12207
 *
12208
 * This method is the same as the C function OSRImportFromEPSG().
12209
 *
12210
 * Before GDAL 3.0.3, this method would try to attach a 3-parameter or
12211
 * 7-parameter Helmert transformation to WGS84 when there is one and only one
12212
 * such method available for the CRS. This behavior might not always be
12213
 * desirable, so starting with GDAL 3.0.3, this is no longer done unless
12214
 * the OSR_ADD_TOWGS84_ON_IMPORT_FROM_EPSG configuration option is set to YES.
12215
 *
12216
 * @param nCode a GCS or PCS code from the horizontal coordinate system table.
12217
 *
12218
 * @return OGRERR_NONE on success, or an error code on failure.
12219
 */
12220
12221
OGRErr OGRSpatialReference::importFromEPSG(int nCode)
12222
12223
0
{
12224
0
    return importFromEPSGA(nCode);
12225
0
}
12226
12227
/************************************************************************/
12228
/*                         OSRImportFromEPSG()                          */
12229
/************************************************************************/
12230
12231
/**
12232
 * \brief  Initialize SRS based on EPSG geographic, projected or vertical CRS
12233
 * code.
12234
 *
12235
 * This function is the same as OGRSpatialReference::importFromEPSG().
12236
 */
12237
12238
OGRErr CPL_STDCALL OSRImportFromEPSG(OGRSpatialReferenceH hSRS, int nCode)
12239
12240
0
{
12241
0
    VALIDATE_POINTER1(hSRS, "OSRImportFromEPSG", OGRERR_FAILURE);
12242
12243
0
    return OGRSpatialReference::FromHandle(hSRS)->importFromEPSG(nCode);
12244
0
}
12245
12246
/************************************************************************/
12247
/*                        EPSGTreatsAsLatLong()                         */
12248
/************************************************************************/
12249
12250
/**
12251
 * \brief This method returns TRUE if this geographic coordinate
12252
 * system should be treated as having lat/long coordinate ordering.
12253
 *
12254
 * Currently this returns TRUE for all geographic coordinate systems
12255
 * with axes set defining it as lat, long (prior to GDAL 3.10, it
12256
 * also checked that the CRS had belonged to EPSG authority, but this check
12257
 * has now been removed).
12258
 *
12259
 * \note Important change of behavior since GDAL 3.0. In previous versions,
12260
 * geographic CRS imported with importFromEPSG() would cause this method to
12261
 * return FALSE on them, whereas now it returns TRUE, since importFromEPSG()
12262
 * is now equivalent to importFromEPSGA().
12263
 *
12264
 * FALSE will be returned for all coordinate systems that are not geographic,
12265
 * or whose axes ordering is not latitude, longitude.
12266
 *
12267
 * This method is the same as the C function OSREPSGTreatsAsLatLong().
12268
 *
12269
 * @return TRUE or FALSE.
12270
 */
12271
12272
int OGRSpatialReference::EPSGTreatsAsLatLong() const
12273
12274
0
{
12275
0
    TAKE_OPTIONAL_LOCK();
12276
12277
0
    if (!IsGeographic())
12278
0
        return FALSE;
12279
12280
0
    d->demoteFromBoundCRS();
12281
12282
0
    bool ret = false;
12283
0
    if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
12284
0
    {
12285
0
        auto horizCRS =
12286
0
            proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
12287
0
        if (horizCRS)
12288
0
        {
12289
0
            auto cs =
12290
0
                proj_crs_get_coordinate_system(d->getPROJContext(), horizCRS);
12291
0
            if (cs)
12292
0
            {
12293
0
                const char *pszDirection = nullptr;
12294
0
                if (proj_cs_get_axis_info(d->getPROJContext(), cs, 0, nullptr,
12295
0
                                          nullptr, &pszDirection, nullptr,
12296
0
                                          nullptr, nullptr, nullptr))
12297
0
                {
12298
0
                    if (EQUAL(pszDirection, "north"))
12299
0
                    {
12300
0
                        ret = true;
12301
0
                    }
12302
0
                }
12303
12304
0
                proj_destroy(cs);
12305
0
            }
12306
12307
0
            proj_destroy(horizCRS);
12308
0
        }
12309
0
    }
12310
0
    else
12311
0
    {
12312
0
        auto cs =
12313
0
            proj_crs_get_coordinate_system(d->getPROJContext(), d->m_pj_crs);
12314
0
        if (cs)
12315
0
        {
12316
0
            const char *pszDirection = nullptr;
12317
0
            if (proj_cs_get_axis_info(d->getPROJContext(), cs, 0, nullptr,
12318
0
                                      nullptr, &pszDirection, nullptr, nullptr,
12319
0
                                      nullptr, nullptr))
12320
0
            {
12321
0
                if (EQUAL(pszDirection, "north"))
12322
0
                {
12323
0
                    ret = true;
12324
0
                }
12325
0
            }
12326
12327
0
            proj_destroy(cs);
12328
0
        }
12329
0
    }
12330
0
    d->undoDemoteFromBoundCRS();
12331
12332
0
    return ret;
12333
0
}
12334
12335
/************************************************************************/
12336
/*                       OSREPSGTreatsAsLatLong()                       */
12337
/************************************************************************/
12338
12339
/**
12340
 * \brief This function returns TRUE if this geographic coordinate
12341
 * system should be treated as having lat/long coordinate ordering.
12342
 *
12343
 * This function is the same as OGRSpatialReference::OSREPSGTreatsAsLatLong().
12344
 */
12345
12346
int OSREPSGTreatsAsLatLong(OGRSpatialReferenceH hSRS)
12347
12348
0
{
12349
0
    VALIDATE_POINTER1(hSRS, "OSREPSGTreatsAsLatLong", OGRERR_FAILURE);
12350
12351
0
    return OGRSpatialReference::FromHandle(hSRS)->EPSGTreatsAsLatLong();
12352
0
}
12353
12354
/************************************************************************/
12355
/*                     EPSGTreatsAsNorthingEasting()                    */
12356
/************************************************************************/
12357
12358
/**
12359
 * \brief This method returns TRUE if this projected coordinate
12360
 * system should be treated as having northing/easting coordinate ordering.
12361
 *
12362
 * Currently this returns TRUE for all projected coordinate systems
12363
 * with axes set defining it as northing, easting (prior to GDAL 3.10, it
12364
 * also checked that the CRS had belonged to EPSG authority, but this check
12365
 * has now been removed).
12366
 *
12367
 * \note Important change of behavior since GDAL 3.0. In previous versions,
12368
 * projected CRS with northing, easting axis order imported with
12369
 * importFromEPSG() would cause this method to
12370
 * return FALSE on them, whereas now it returns TRUE, since importFromEPSG()
12371
 * is now equivalent to importFromEPSGA().
12372
 *
12373
 * FALSE will be returned for all coordinate systems that are not projected,
12374
 * or whose axes ordering is not northing, easting.
12375
 *
12376
 * This method is the same as the C function EPSGTreatsAsNorthingEasting().
12377
 *
12378
 * @return TRUE or FALSE.
12379
 *
12380
 * @since OGR 1.10.0
12381
 */
12382
12383
int OGRSpatialReference::EPSGTreatsAsNorthingEasting() const
12384
12385
0
{
12386
0
    TAKE_OPTIONAL_LOCK();
12387
12388
0
    if (!IsProjected())
12389
0
        return FALSE;
12390
12391
0
    d->demoteFromBoundCRS();
12392
0
    PJ *projCRS;
12393
0
    const auto ctxt = d->getPROJContext();
12394
0
    if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
12395
0
    {
12396
0
        projCRS = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
12397
0
        if (!projCRS || proj_get_type(projCRS) != PJ_TYPE_PROJECTED_CRS)
12398
0
        {
12399
0
            d->undoDemoteFromBoundCRS();
12400
0
            proj_destroy(projCRS);
12401
0
            return FALSE;
12402
0
        }
12403
0
    }
12404
0
    else
12405
0
    {
12406
0
        projCRS = proj_clone(ctxt, d->m_pj_crs);
12407
0
    }
12408
12409
0
    bool ret = false;
12410
0
    auto cs = proj_crs_get_coordinate_system(ctxt, projCRS);
12411
0
    proj_destroy(projCRS);
12412
0
    d->undoDemoteFromBoundCRS();
12413
12414
0
    if (cs)
12415
0
    {
12416
0
        ret = isNorthEastAxisOrder(ctxt, cs);
12417
0
        proj_destroy(cs);
12418
0
    }
12419
12420
0
    return ret;
12421
0
}
12422
12423
/************************************************************************/
12424
/*                     OSREPSGTreatsAsNorthingEasting()                 */
12425
/************************************************************************/
12426
12427
/**
12428
 * \brief This function returns TRUE if this projected coordinate
12429
 * system should be treated as having northing/easting coordinate ordering.
12430
 *
12431
 * This function is the same as
12432
 * OGRSpatialReference::EPSGTreatsAsNorthingEasting().
12433
 *
12434
 * @since OGR 1.10.0
12435
 */
12436
12437
int OSREPSGTreatsAsNorthingEasting(OGRSpatialReferenceH hSRS)
12438
12439
0
{
12440
0
    VALIDATE_POINTER1(hSRS, "OSREPSGTreatsAsNorthingEasting", OGRERR_FAILURE);
12441
12442
0
    return OGRSpatialReference::FromHandle(hSRS)->EPSGTreatsAsNorthingEasting();
12443
0
}
12444
12445
/************************************************************************/
12446
/*                     ImportFromESRIWisconsinWKT()                     */
12447
/*                                                                      */
12448
/*      Search a ESRI State Plane WKT and import it.                    */
12449
/************************************************************************/
12450
12451
// This is only used by the HFA driver and somewhat dubious we really need that
12452
// Coming from an old ESRI merge
12453
12454
OGRErr OGRSpatialReference::ImportFromESRIWisconsinWKT(const char *prjName,
12455
                                                       double centralMeridian,
12456
                                                       double latOfOrigin,
12457
                                                       const char *unitsName,
12458
                                                       const char *crsName)
12459
0
{
12460
0
    TAKE_OPTIONAL_LOCK();
12461
12462
0
    if (centralMeridian < -93 || centralMeridian > -87)
12463
0
        return OGRERR_FAILURE;
12464
0
    if (latOfOrigin < 40 || latOfOrigin > 47)
12465
0
        return OGRERR_FAILURE;
12466
12467
    // If the CS name is known.
12468
0
    if (!prjName && !unitsName && crsName)
12469
0
    {
12470
0
        const PJ_TYPE type = PJ_TYPE_PROJECTED_CRS;
12471
0
        PJ_OBJ_LIST *list = proj_create_from_name(
12472
0
            d->getPROJContext(), "ESRI", crsName, &type, 1, false, 1, nullptr);
12473
0
        if (list)
12474
0
        {
12475
0
            if (proj_list_get_count(list) == 1)
12476
0
            {
12477
0
                auto crs = proj_list_get(d->getPROJContext(), list, 0);
12478
0
                if (crs)
12479
0
                {
12480
0
                    Clear();
12481
0
                    d->setPjCRS(crs);
12482
0
                    proj_list_destroy(list);
12483
0
                    return OGRERR_NONE;
12484
0
                }
12485
0
            }
12486
0
            proj_list_destroy(list);
12487
0
        }
12488
0
        return OGRERR_FAILURE;
12489
0
    }
12490
12491
0
    if (prjName == nullptr || unitsName == nullptr)
12492
0
    {
12493
0
        return OGRERR_FAILURE;
12494
0
    }
12495
12496
0
    const PJ_TYPE type = PJ_TYPE_PROJECTED_CRS;
12497
0
    PJ_OBJ_LIST *list = proj_create_from_name(d->getPROJContext(), "ESRI",
12498
0
                                              "NAD_1983_HARN_WISCRS_", &type, 1,
12499
0
                                              true, 0, nullptr);
12500
0
    if (list)
12501
0
    {
12502
0
        const auto listSize = proj_list_get_count(list);
12503
0
        for (int i = 0; i < listSize; i++)
12504
0
        {
12505
0
            auto crs = proj_list_get(d->getPROJContext(), list, i);
12506
0
            if (!crs)
12507
0
            {
12508
0
                continue;
12509
0
            }
12510
12511
0
            auto conv = proj_crs_get_coordoperation(d->getPROJContext(), crs);
12512
0
            if (!conv)
12513
0
            {
12514
0
                proj_destroy(crs);
12515
0
                continue;
12516
0
            }
12517
0
            const char *pszMethodCode = nullptr;
12518
0
            proj_coordoperation_get_method_info(
12519
0
                d->getPROJContext(), conv, nullptr, nullptr, &pszMethodCode);
12520
0
            const int nMethodCode = atoi(pszMethodCode ? pszMethodCode : "0");
12521
0
            if (!((EQUAL(prjName, SRS_PT_TRANSVERSE_MERCATOR) &&
12522
0
                   nMethodCode == EPSG_CODE_METHOD_TRANSVERSE_MERCATOR) ||
12523
0
                  (EQUAL(prjName, "Lambert_Conformal_Conic") &&
12524
0
                   nMethodCode ==
12525
0
                       EPSG_CODE_METHOD_LAMBERT_CONIC_CONFORMAL_1SP)))
12526
0
            {
12527
0
                proj_destroy(crs);
12528
0
                proj_destroy(conv);
12529
0
                continue;
12530
0
            }
12531
12532
0
            auto coordSys =
12533
0
                proj_crs_get_coordinate_system(d->getPROJContext(), crs);
12534
0
            if (!coordSys)
12535
0
            {
12536
0
                proj_destroy(crs);
12537
0
                proj_destroy(conv);
12538
0
                continue;
12539
0
            }
12540
12541
0
            double dfConvFactor = 0.0;
12542
0
            proj_cs_get_axis_info(d->getPROJContext(), coordSys, 0, nullptr,
12543
0
                                  nullptr, nullptr, &dfConvFactor, nullptr,
12544
0
                                  nullptr, nullptr);
12545
0
            proj_destroy(coordSys);
12546
12547
0
            if ((EQUAL(unitsName, "meters") && dfConvFactor != 1.0) ||
12548
0
                (!EQUAL(unitsName, "meters") &&
12549
0
                 std::fabs(dfConvFactor - CPLAtof(SRS_UL_US_FOOT_CONV)) >
12550
0
                     1e-10))
12551
0
            {
12552
0
                proj_destroy(crs);
12553
0
                proj_destroy(conv);
12554
0
                continue;
12555
0
            }
12556
12557
0
            int idx_lat = proj_coordoperation_get_param_index(
12558
0
                d->getPROJContext(), conv,
12559
0
                EPSG_NAME_PARAMETER_LATITUDE_OF_NATURAL_ORIGIN);
12560
0
            double valueLat = -1000;
12561
0
            proj_coordoperation_get_param(d->getPROJContext(), conv, idx_lat,
12562
0
                                          nullptr, nullptr, nullptr, &valueLat,
12563
0
                                          nullptr, nullptr, nullptr, nullptr,
12564
0
                                          nullptr, nullptr);
12565
0
            int idx_lon = proj_coordoperation_get_param_index(
12566
0
                d->getPROJContext(), conv,
12567
0
                EPSG_NAME_PARAMETER_LONGITUDE_OF_NATURAL_ORIGIN);
12568
0
            double valueLong = -1000;
12569
0
            proj_coordoperation_get_param(d->getPROJContext(), conv, idx_lon,
12570
0
                                          nullptr, nullptr, nullptr, &valueLong,
12571
0
                                          nullptr, nullptr, nullptr, nullptr,
12572
0
                                          nullptr, nullptr);
12573
0
            if (std::fabs(centralMeridian - valueLong) <= 1e-10 &&
12574
0
                std::fabs(latOfOrigin - valueLat) <= 1e-10)
12575
0
            {
12576
0
                Clear();
12577
0
                d->setPjCRS(crs);
12578
0
                proj_list_destroy(list);
12579
0
                proj_destroy(conv);
12580
0
                return OGRERR_NONE;
12581
0
            }
12582
12583
0
            proj_destroy(crs);
12584
0
            proj_destroy(conv);
12585
0
        }
12586
0
        proj_list_destroy(list);
12587
0
    }
12588
12589
0
    return OGRERR_FAILURE;
12590
0
}
12591
12592
/************************************************************************/
12593
/*                      GetAxisMappingStrategy()                        */
12594
/************************************************************************/
12595
12596
/** \brief Return the data axis to CRS axis mapping strategy.
12597
 *
12598
 * <ul>
12599
 * <li>OAMS_TRADITIONAL_GIS_ORDER means that for geographic CRS with
12600
 *     lat/long order, the data will still be long/lat ordered. Similarly for
12601
 *     a projected CRS with northing/easting order, the data will still be
12602
 *     easting/northing ordered.
12603
 * <li>OAMS_AUTHORITY_COMPLIANT means that the data axis will be identical to
12604
 *     the CRS axis.
12605
 * <li>OAMS_CUSTOM means that the data axis are customly defined with
12606
 *     SetDataAxisToSRSAxisMapping()
12607
 * </ul>
12608
 * @return the data axis to CRS axis mapping strategy.
12609
 * @since GDAL 3.0
12610
 */
12611
OSRAxisMappingStrategy OGRSpatialReference::GetAxisMappingStrategy() const
12612
0
{
12613
0
    TAKE_OPTIONAL_LOCK();
12614
12615
0
    return d->m_axisMappingStrategy;
12616
0
}
12617
12618
/************************************************************************/
12619
/*                      OSRGetAxisMappingStrategy()                     */
12620
/************************************************************************/
12621
12622
/** \brief Return the data axis to CRS axis mapping strategy.
12623
 *
12624
 * See OGRSpatialReference::GetAxisMappingStrategy()
12625
 * @since GDAL 3.0
12626
 */
12627
OSRAxisMappingStrategy OSRGetAxisMappingStrategy(OGRSpatialReferenceH hSRS)
12628
0
{
12629
0
    VALIDATE_POINTER1(hSRS, "OSRGetAxisMappingStrategy", OAMS_CUSTOM);
12630
12631
0
    return OGRSpatialReference::FromHandle(hSRS)->GetAxisMappingStrategy();
12632
0
}
12633
12634
/************************************************************************/
12635
/*                      SetAxisMappingStrategy()                        */
12636
/************************************************************************/
12637
12638
/** \brief Set the data axis to CRS axis mapping strategy.
12639
 *
12640
 * Starting with GDAL 3.5, the OSR_DEFAULT_AXIS_MAPPING_STRATEGY configuration
12641
 * option can be set to "TRADITIONAL_GIS_ORDER" / "AUTHORITY_COMPLIANT" (the
12642
 * later being the default value when the option is not set) to control the
12643
 * value of the data axis to CRS axis mapping strategy when a
12644
 * OSRSpatialReference object is created. Calling SetAxisMappingStrategy() will
12645
 * override this default value.
12646
 *
12647
 * See OGRSpatialReference::GetAxisMappingStrategy()
12648
 * @since GDAL 3.0
12649
 */
12650
void OGRSpatialReference::SetAxisMappingStrategy(
12651
    OSRAxisMappingStrategy strategy)
12652
0
{
12653
0
    TAKE_OPTIONAL_LOCK();
12654
12655
0
    d->m_axisMappingStrategy = strategy;
12656
0
    d->refreshAxisMapping();
12657
0
}
12658
12659
/************************************************************************/
12660
/*                      OSRSetAxisMappingStrategy()                     */
12661
/************************************************************************/
12662
12663
/** \brief Set the data axis to CRS axis mapping strategy.
12664
 *
12665
 * See OGRSpatialReference::SetAxisMappingStrategy()
12666
 * @since GDAL 3.0
12667
 */
12668
void OSRSetAxisMappingStrategy(OGRSpatialReferenceH hSRS,
12669
                               OSRAxisMappingStrategy strategy)
12670
0
{
12671
0
    VALIDATE_POINTER0(hSRS, "OSRSetAxisMappingStrategy");
12672
12673
0
    OGRSpatialReference::FromHandle(hSRS)->SetAxisMappingStrategy(strategy);
12674
0
}
12675
12676
/************************************************************************/
12677
/*                      GetDataAxisToSRSAxisMapping()                   */
12678
/************************************************************************/
12679
12680
/** \brief Return the data axis to SRS axis mapping.
12681
 *
12682
 * The number of elements of the vector will be the number of axis of the CRS.
12683
 * Values start at 1.
12684
 *
12685
 * If m = GetDataAxisToSRSAxisMapping(), then m[0] is the data axis number
12686
 * for the first axis of the CRS.
12687
 *
12688
 * @since GDAL 3.0
12689
 */
12690
const std::vector<int> &OGRSpatialReference::GetDataAxisToSRSAxisMapping() const
12691
0
{
12692
0
    TAKE_OPTIONAL_LOCK();
12693
12694
0
    return d->m_axisMapping;
12695
0
}
12696
12697
/************************************************************************/
12698
/*                     OSRGetDataAxisToSRSAxisMapping()                 */
12699
/************************************************************************/
12700
12701
/** \brief Return the data axis to SRS axis mapping.
12702
 *
12703
 * See OGRSpatialReference::GetDataAxisToSRSAxisMapping()
12704
 *
12705
 * @since GDAL 3.0
12706
 */
12707
const int *OSRGetDataAxisToSRSAxisMapping(OGRSpatialReferenceH hSRS,
12708
                                          int *pnCount)
12709
0
{
12710
0
    VALIDATE_POINTER1(hSRS, "OSRGetDataAxisToSRSAxisMapping", nullptr);
12711
0
    VALIDATE_POINTER1(pnCount, "OSRGetDataAxisToSRSAxisMapping", nullptr);
12712
12713
0
    const auto &v =
12714
0
        OGRSpatialReference::FromHandle(hSRS)->GetDataAxisToSRSAxisMapping();
12715
0
    *pnCount = static_cast<int>(v.size());
12716
0
    return v.data();
12717
0
}
12718
12719
/************************************************************************/
12720
/*                      SetDataAxisToSRSAxisMapping()                   */
12721
/************************************************************************/
12722
12723
/** \brief Set a custom data axis to CRS axis mapping.
12724
 *
12725
 * The number of elements of the mapping vector should be the number of axis
12726
 * of the CRS (as returned by GetAxesCount()) (although this method does not
12727
 * check that, beyond checking there are at least 2 elements, so that this
12728
 * method and setting the CRS can be done in any order).
12729
 * This is taken into account by OGRCoordinateTransformation to transform the
12730
 * order of coordinates to the order expected by the CRS before
12731
 * transformation, and back to the data order after transformation.
12732
 *
12733
 * The mapping[i] value (one based) represents the data axis number for the i(th)
12734
 * axis of the CRS. A negative value can also be used to ask for a sign
12735
 * reversal during coordinate transformation (to deal with northing vs southing,
12736
 * easting vs westing, heights vs depths).
12737
 *
12738
 * When used with OGRCoordinateTransformation,
12739
 * - the only valid values for mapping[0] (data axis number for the first axis
12740
 *   of the CRS) are 1, 2, -1, -2.
12741
 * - the only valid values for mapping[1] (data axis number for the second axis
12742
 *   of the CRS) are 1, 2, -1, -2.
12743
 *  - the only valid values mapping[2] are 3 or -3.
12744
 * Note: this method does not validate the values of mapping[].
12745
 *
12746
 * mapping=[2,1] typically expresses the inversion of axis between the data
12747
 * axis and the CRS axis for a 2D CRS.
12748
 *
12749
 * Automatically implies SetAxisMappingStrategy(OAMS_CUSTOM)
12750
 *
12751
 * This is the same as the C function OSRSetDataAxisToSRSAxisMapping().
12752
 *
12753
 * @param mapping The new data axis to CRS axis mapping.
12754
 *
12755
 * @since GDAL 3.0
12756
 * @see OGRSpatialReference::GetDataAxisToSRSAxisMapping()
12757
 */
12758
OGRErr OGRSpatialReference::SetDataAxisToSRSAxisMapping(
12759
    const std::vector<int> &mapping)
12760
0
{
12761
0
    TAKE_OPTIONAL_LOCK();
12762
12763
0
    if (mapping.size() < 2)
12764
0
        return OGRERR_FAILURE;
12765
0
    d->m_axisMappingStrategy = OAMS_CUSTOM;
12766
0
    d->m_axisMapping = mapping;
12767
0
    return OGRERR_NONE;
12768
0
}
12769
12770
/************************************************************************/
12771
/*                     OSRSetDataAxisToSRSAxisMapping()                 */
12772
/************************************************************************/
12773
12774
/** \brief Set a custom data axis to CRS axis mapping.
12775
 *
12776
 * Automatically implies SetAxisMappingStrategy(OAMS_CUSTOM)
12777
 *
12778
 * This is the same as the C++ method
12779
 * OGRSpatialReference::SetDataAxisToSRSAxisMapping()
12780
 *
12781
 * @since GDAL 3.1
12782
 */
12783
OGRErr OSRSetDataAxisToSRSAxisMapping(OGRSpatialReferenceH hSRS,
12784
                                      int nMappingSize, const int *panMapping)
12785
0
{
12786
0
    VALIDATE_POINTER1(hSRS, "OSRSetDataAxisToSRSAxisMapping", OGRERR_FAILURE);
12787
0
    VALIDATE_POINTER1(panMapping, "OSRSetDataAxisToSRSAxisMapping",
12788
0
                      OGRERR_FAILURE);
12789
12790
0
    if (nMappingSize < 0)
12791
0
        return OGRERR_FAILURE;
12792
12793
0
    std::vector<int> mapping(nMappingSize);
12794
0
    if (nMappingSize)
12795
0
        memcpy(&mapping[0], panMapping, nMappingSize * sizeof(int));
12796
0
    return OGRSpatialReference::FromHandle(hSRS)->SetDataAxisToSRSAxisMapping(
12797
0
        mapping);
12798
0
}
12799
12800
/************************************************************************/
12801
/*                               GetAreaOfUse()                         */
12802
/************************************************************************/
12803
12804
/** \brief Return the area of use of the CRS.
12805
 *
12806
 * This method is the same as the OSRGetAreaOfUse() function.
12807
 *
12808
 * @param pdfWestLongitudeDeg Pointer to a double to receive the western-most
12809
 * longitude, expressed in degree. Might be NULL. If the returned value is
12810
 * -1000, the bounding box is unknown.
12811
 * @param pdfSouthLatitudeDeg Pointer to a double to receive the southern-most
12812
 * latitude, expressed in degree. Might be NULL. If the returned value is -1000,
12813
 * the bounding box is unknown.
12814
 * @param pdfEastLongitudeDeg Pointer to a double to receive the eastern-most
12815
 * longitude, expressed in degree. Might be NULL. If the returned value is
12816
 * -1000, the bounding box is unknown.
12817
 * @param pdfNorthLatitudeDeg Pointer to a double to receive the northern-most
12818
 * latitude, expressed in degree. Might be NULL. If the returned value is -1000,
12819
 * the bounding box is unknown.
12820
 * @param ppszAreaName Pointer to a string to receive the name of the area of
12821
 * use. Might be NULL. Note that *ppszAreaName is short-lived and might be
12822
 * invalidated by further calls.
12823
 * @return true in case of success
12824
 * @since GDAL 3.0
12825
 */
12826
bool OGRSpatialReference::GetAreaOfUse(double *pdfWestLongitudeDeg,
12827
                                       double *pdfSouthLatitudeDeg,
12828
                                       double *pdfEastLongitudeDeg,
12829
                                       double *pdfNorthLatitudeDeg,
12830
                                       const char **ppszAreaName) const
12831
0
{
12832
0
    TAKE_OPTIONAL_LOCK();
12833
12834
0
    d->refreshProjObj();
12835
0
    if (!d->m_pj_crs)
12836
0
    {
12837
0
        return false;
12838
0
    }
12839
0
    d->demoteFromBoundCRS();
12840
0
    const char *pszAreaName = nullptr;
12841
0
    int bSuccess = proj_get_area_of_use(
12842
0
        d->getPROJContext(), d->m_pj_crs, pdfWestLongitudeDeg,
12843
0
        pdfSouthLatitudeDeg, pdfEastLongitudeDeg, pdfNorthLatitudeDeg,
12844
0
        &pszAreaName);
12845
0
    d->undoDemoteFromBoundCRS();
12846
0
    d->m_osAreaName = pszAreaName ? pszAreaName : "";
12847
0
    if (ppszAreaName)
12848
0
        *ppszAreaName = d->m_osAreaName.c_str();
12849
0
    return CPL_TO_BOOL(bSuccess);
12850
0
}
12851
12852
/************************************************************************/
12853
/*                               GetAreaOfUse()                         */
12854
/************************************************************************/
12855
12856
/** \brief Return the area of use of the CRS.
12857
 *
12858
 * This function is the same as the OGRSpatialReference::GetAreaOfUse() method.
12859
 *
12860
 * @since GDAL 3.0
12861
 */
12862
int OSRGetAreaOfUse(OGRSpatialReferenceH hSRS, double *pdfWestLongitudeDeg,
12863
                    double *pdfSouthLatitudeDeg, double *pdfEastLongitudeDeg,
12864
                    double *pdfNorthLatitudeDeg, const char **ppszAreaName)
12865
0
{
12866
0
    VALIDATE_POINTER1(hSRS, "OSRGetAreaOfUse", FALSE);
12867
12868
0
    return OGRSpatialReference::FromHandle(hSRS)->GetAreaOfUse(
12869
0
        pdfWestLongitudeDeg, pdfSouthLatitudeDeg, pdfEastLongitudeDeg,
12870
0
        pdfNorthLatitudeDeg, ppszAreaName);
12871
0
}
12872
12873
/************************************************************************/
12874
/*                     OSRGetCRSInfoListFromDatabase()                  */
12875
/************************************************************************/
12876
12877
/** \brief Enumerate CRS objects from the database.
12878
 *
12879
 * The returned object is an array of OSRCRSInfo* pointers, whose last
12880
 * entry is NULL. This array should be freed with OSRDestroyCRSInfoList()
12881
 *
12882
 * @param pszAuthName Authority name, used to restrict the search.
12883
 * Or NULL for all authorities.
12884
 * @param params Additional criteria. Must be set to NULL for now.
12885
 * @param pnOutResultCount Output parameter pointing to an integer to receive
12886
 * the size of the result list. Might be NULL
12887
 * @return an array of OSRCRSInfo* pointers to be freed with
12888
 * OSRDestroyCRSInfoList(), or NULL in case of error.
12889
 *
12890
 * @since GDAL 3.0
12891
 */
12892
OSRCRSInfo **
12893
OSRGetCRSInfoListFromDatabase(const char *pszAuthName,
12894
                              CPL_UNUSED const OSRCRSListParameters *params,
12895
                              int *pnOutResultCount)
12896
0
{
12897
0
    int nResultCount = 0;
12898
0
    auto projList = proj_get_crs_info_list_from_database(
12899
0
        OSRGetProjTLSContext(), pszAuthName, nullptr, &nResultCount);
12900
0
    if (pnOutResultCount)
12901
0
        *pnOutResultCount = nResultCount;
12902
0
    if (!projList)
12903
0
    {
12904
0
        return nullptr;
12905
0
    }
12906
0
    auto res = new OSRCRSInfo *[nResultCount + 1];
12907
0
    for (int i = 0; i < nResultCount; i++)
12908
0
    {
12909
0
        res[i] = new OSRCRSInfo;
12910
0
        res[i]->pszAuthName = projList[i]->auth_name
12911
0
                                  ? CPLStrdup(projList[i]->auth_name)
12912
0
                                  : nullptr;
12913
0
        res[i]->pszCode =
12914
0
            projList[i]->code ? CPLStrdup(projList[i]->code) : nullptr;
12915
0
        res[i]->pszName =
12916
0
            projList[i]->name ? CPLStrdup(projList[i]->name) : nullptr;
12917
0
        res[i]->eType = OSR_CRS_TYPE_OTHER;
12918
0
        switch (projList[i]->type)
12919
0
        {
12920
0
            case PJ_TYPE_GEOGRAPHIC_2D_CRS:
12921
0
                res[i]->eType = OSR_CRS_TYPE_GEOGRAPHIC_2D;
12922
0
                break;
12923
0
            case PJ_TYPE_GEOGRAPHIC_3D_CRS:
12924
0
                res[i]->eType = OSR_CRS_TYPE_GEOGRAPHIC_3D;
12925
0
                break;
12926
0
            case PJ_TYPE_GEOCENTRIC_CRS:
12927
0
                res[i]->eType = OSR_CRS_TYPE_GEOCENTRIC;
12928
0
                break;
12929
0
            case PJ_TYPE_PROJECTED_CRS:
12930
0
                res[i]->eType = OSR_CRS_TYPE_PROJECTED;
12931
0
                break;
12932
0
            case PJ_TYPE_VERTICAL_CRS:
12933
0
                res[i]->eType = OSR_CRS_TYPE_VERTICAL;
12934
0
                break;
12935
0
            case PJ_TYPE_COMPOUND_CRS:
12936
0
                res[i]->eType = OSR_CRS_TYPE_COMPOUND;
12937
0
                break;
12938
0
            default:
12939
0
                break;
12940
0
        }
12941
0
        res[i]->bDeprecated = projList[i]->deprecated;
12942
0
        res[i]->bBboxValid = projList[i]->bbox_valid;
12943
0
        res[i]->dfWestLongitudeDeg = projList[i]->west_lon_degree;
12944
0
        res[i]->dfSouthLatitudeDeg = projList[i]->south_lat_degree;
12945
0
        res[i]->dfEastLongitudeDeg = projList[i]->east_lon_degree;
12946
0
        res[i]->dfNorthLatitudeDeg = projList[i]->north_lat_degree;
12947
0
        res[i]->pszAreaName = projList[i]->area_name
12948
0
                                  ? CPLStrdup(projList[i]->area_name)
12949
0
                                  : nullptr;
12950
0
        res[i]->pszProjectionMethod =
12951
0
            projList[i]->projection_method_name
12952
0
                ? CPLStrdup(projList[i]->projection_method_name)
12953
0
                : nullptr;
12954
0
#if PROJ_AT_LEAST_VERSION(8, 1, 0)
12955
0
        res[i]->pszCelestialBodyName =
12956
0
            projList[i]->celestial_body_name
12957
0
                ? CPLStrdup(projList[i]->celestial_body_name)
12958
0
                : nullptr;
12959
#else
12960
        res[i]->pszCelestialBodyName =
12961
            res[i]->pszAuthName && EQUAL(res[i]->pszAuthName, "EPSG")
12962
                ? CPLStrdup("Earth")
12963
                : nullptr;
12964
#endif
12965
0
    }
12966
0
    res[nResultCount] = nullptr;
12967
0
    proj_crs_info_list_destroy(projList);
12968
0
    return res;
12969
0
}
12970
12971
/************************************************************************/
12972
/*                        OSRDestroyCRSInfoList()                       */
12973
/************************************************************************/
12974
12975
/** \brief Destroy the result returned by
12976
 * OSRGetCRSInfoListFromDatabase().
12977
 *
12978
 * @since GDAL 3.0
12979
 */
12980
void OSRDestroyCRSInfoList(OSRCRSInfo **list)
12981
0
{
12982
0
    if (list)
12983
0
    {
12984
0
        for (int i = 0; list[i] != nullptr; i++)
12985
0
        {
12986
0
            CPLFree(list[i]->pszAuthName);
12987
0
            CPLFree(list[i]->pszCode);
12988
0
            CPLFree(list[i]->pszName);
12989
0
            CPLFree(list[i]->pszAreaName);
12990
0
            CPLFree(list[i]->pszProjectionMethod);
12991
0
            CPLFree(list[i]->pszCelestialBodyName);
12992
0
            delete list[i];
12993
0
        }
12994
0
        delete[] list;
12995
0
    }
12996
0
}
12997
12998
/************************************************************************/
12999
/*                   OSRGetAuthorityListFromDatabase()                  */
13000
/************************************************************************/
13001
13002
/** \brief Return the list of CRS authorities used in the PROJ database.
13003
 *
13004
 * Such as "EPSG", "ESRI", "PROJ", "IGNF", "IAU_2015", etc.
13005
 *
13006
 * This is a direct mapping of https://proj.org/en/latest/development/reference/functions.html#c.proj_get_authorities_from_database
13007
 *
13008
 * @return nullptr in case of error, or a NULL terminated list of strings to
13009
 * free with CSLDestroy()
13010
 * @since GDAL 3.10
13011
 */
13012
char **OSRGetAuthorityListFromDatabase()
13013
0
{
13014
0
    PROJ_STRING_LIST list =
13015
0
        proj_get_authorities_from_database(OSRGetProjTLSContext());
13016
0
    if (!list)
13017
0
    {
13018
0
        return nullptr;
13019
0
    }
13020
0
    int count = 0;
13021
0
    while (list[count])
13022
0
        ++count;
13023
0
    char **res = static_cast<char **>(CPLCalloc(count + 1, sizeof(char *)));
13024
0
    for (int i = 0; i < count; ++i)
13025
0
        res[i] = CPLStrdup(list[i]);
13026
0
    proj_string_list_destroy(list);
13027
0
    return res;
13028
0
}
13029
13030
/************************************************************************/
13031
/*                    UpdateCoordinateSystemFromGeogCRS()               */
13032
/************************************************************************/
13033
13034
/*! @cond Doxygen_Suppress */
13035
/** \brief Used by gt_wkt_srs.cpp to create projected 3D CRS. Internal use only
13036
 *
13037
 * @since GDAL 3.1
13038
 */
13039
void OGRSpatialReference::UpdateCoordinateSystemFromGeogCRS()
13040
0
{
13041
0
    TAKE_OPTIONAL_LOCK();
13042
13043
0
    d->refreshProjObj();
13044
0
    if (!d->m_pj_crs)
13045
0
        return;
13046
0
    if (d->m_pjType != PJ_TYPE_PROJECTED_CRS)
13047
0
        return;
13048
0
    if (GetAxesCount() == 3)
13049
0
        return;
13050
0
    auto ctxt = d->getPROJContext();
13051
0
    auto baseCRS = proj_crs_get_geodetic_crs(ctxt, d->m_pj_crs);
13052
0
    if (!baseCRS)
13053
0
        return;
13054
0
    auto baseCRSCS = proj_crs_get_coordinate_system(ctxt, baseCRS);
13055
0
    if (!baseCRSCS)
13056
0
    {
13057
0
        proj_destroy(baseCRS);
13058
0
        return;
13059
0
    }
13060
0
    if (proj_cs_get_axis_count(ctxt, baseCRSCS) != 3)
13061
0
    {
13062
0
        proj_destroy(baseCRSCS);
13063
0
        proj_destroy(baseCRS);
13064
0
        return;
13065
0
    }
13066
0
    auto projCS = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
13067
0
    if (!projCS || proj_cs_get_axis_count(ctxt, projCS) != 2)
13068
0
    {
13069
0
        proj_destroy(baseCRSCS);
13070
0
        proj_destroy(baseCRS);
13071
0
        proj_destroy(projCS);
13072
0
        return;
13073
0
    }
13074
13075
0
    PJ_AXIS_DESCRIPTION axis[3];
13076
0
    for (int i = 0; i < 3; i++)
13077
0
    {
13078
0
        const char *name = nullptr;
13079
0
        const char *abbreviation = nullptr;
13080
0
        const char *direction = nullptr;
13081
0
        double unit_conv_factor = 0;
13082
0
        const char *unit_name = nullptr;
13083
0
        proj_cs_get_axis_info(ctxt, i < 2 ? projCS : baseCRSCS, i, &name,
13084
0
                              &abbreviation, &direction, &unit_conv_factor,
13085
0
                              &unit_name, nullptr, nullptr);
13086
0
        axis[i].name = CPLStrdup(name);
13087
0
        axis[i].abbreviation = CPLStrdup(abbreviation);
13088
0
        axis[i].direction = CPLStrdup(direction);
13089
0
        axis[i].unit_name = CPLStrdup(unit_name);
13090
0
        axis[i].unit_conv_factor = unit_conv_factor;
13091
0
        axis[i].unit_type = PJ_UT_LINEAR;
13092
0
    }
13093
0
    proj_destroy(baseCRSCS);
13094
0
    proj_destroy(projCS);
13095
0
    auto cs = proj_create_cs(ctxt, PJ_CS_TYPE_CARTESIAN, 3, axis);
13096
0
    for (int i = 0; i < 3; i++)
13097
0
    {
13098
0
        CPLFree(axis[i].name);
13099
0
        CPLFree(axis[i].abbreviation);
13100
0
        CPLFree(axis[i].direction);
13101
0
        CPLFree(axis[i].unit_name);
13102
0
    }
13103
0
    if (!cs)
13104
0
    {
13105
0
        proj_destroy(baseCRS);
13106
0
        return;
13107
0
    }
13108
0
    auto conversion = proj_crs_get_coordoperation(ctxt, d->m_pj_crs);
13109
0
    auto crs = proj_create_projected_crs(ctxt, d->getProjCRSName(), baseCRS,
13110
0
                                         conversion, cs);
13111
0
    proj_destroy(baseCRS);
13112
0
    proj_destroy(conversion);
13113
0
    proj_destroy(cs);
13114
0
    d->setPjCRS(crs);
13115
0
}
13116
13117
/*! @endcond */
13118
13119
/************************************************************************/
13120
/*                             PromoteTo3D()                            */
13121
/************************************************************************/
13122
13123
/** \brief "Promotes" a 2D CRS to a 3D CRS one.
13124
 *
13125
 * The new axis will be ellipsoidal height, oriented upwards, and with metre
13126
 * units.
13127
 *
13128
 * @param pszName New name for the CRS. If set to NULL, the previous name will
13129
 * be used.
13130
 * @return OGRERR_NONE if no error occurred.
13131
 * @since GDAL 3.1 and PROJ 6.3
13132
 */
13133
OGRErr OGRSpatialReference::PromoteTo3D(const char *pszName)
13134
0
{
13135
0
    TAKE_OPTIONAL_LOCK();
13136
13137
0
    d->refreshProjObj();
13138
0
    if (!d->m_pj_crs)
13139
0
        return OGRERR_FAILURE;
13140
0
    auto newPj =
13141
0
        proj_crs_promote_to_3D(d->getPROJContext(), pszName, d->m_pj_crs);
13142
0
    if (!newPj)
13143
0
        return OGRERR_FAILURE;
13144
0
    d->setPjCRS(newPj);
13145
0
    return OGRERR_NONE;
13146
0
}
13147
13148
/************************************************************************/
13149
/*                             OSRPromoteTo3D()                         */
13150
/************************************************************************/
13151
13152
/** \brief "Promotes" a 2D CRS to a 3D CRS one.
13153
 *
13154
 * See OGRSpatialReference::PromoteTo3D()
13155
 *
13156
 * @since GDAL 3.1 and PROJ 6.3
13157
 */
13158
OGRErr OSRPromoteTo3D(OGRSpatialReferenceH hSRS, const char *pszName)
13159
0
{
13160
0
    VALIDATE_POINTER1(hSRS, "OSRPromoteTo3D", OGRERR_FAILURE);
13161
13162
0
    return OGRSpatialReference::FromHandle(hSRS)->PromoteTo3D(pszName);
13163
0
}
13164
13165
/************************************************************************/
13166
/*                             DemoteTo2D()                             */
13167
/************************************************************************/
13168
13169
/** \brief "Demote" a 3D CRS to a 2D CRS one.
13170
 *
13171
 * @param pszName New name for the CRS. If set to NULL, the previous name will
13172
 * be used.
13173
 * @return OGRERR_NONE if no error occurred.
13174
 * @since GDAL 3.2 and PROJ 6.3
13175
 */
13176
OGRErr OGRSpatialReference::DemoteTo2D(const char *pszName)
13177
0
{
13178
0
    TAKE_OPTIONAL_LOCK();
13179
13180
0
    d->refreshProjObj();
13181
0
    if (!d->m_pj_crs)
13182
0
        return OGRERR_FAILURE;
13183
0
    auto newPj =
13184
0
        proj_crs_demote_to_2D(d->getPROJContext(), pszName, d->m_pj_crs);
13185
0
    if (!newPj)
13186
0
        return OGRERR_FAILURE;
13187
0
    d->setPjCRS(newPj);
13188
0
    return OGRERR_NONE;
13189
0
}
13190
13191
/************************************************************************/
13192
/*                             OSRDemoteTo2D()                          */
13193
/************************************************************************/
13194
13195
/** \brief "Demote" a 3D CRS to a 2D CRS one.
13196
 *
13197
 * See OGRSpatialReference::DemoteTo2D()
13198
 *
13199
 * @since GDAL 3.2 and PROJ 6.3
13200
 */
13201
OGRErr OSRDemoteTo2D(OGRSpatialReferenceH hSRS, const char *pszName)
13202
0
{
13203
0
    VALIDATE_POINTER1(hSRS, "OSRDemoteTo2D", OGRERR_FAILURE);
13204
13205
0
    return OGRSpatialReference::FromHandle(hSRS)->DemoteTo2D(pszName);
13206
0
}
13207
13208
/************************************************************************/
13209
/*                           GetEPSGGeogCS()                            */
13210
/************************************************************************/
13211
13212
/** Try to establish what the EPSG code for this coordinate systems
13213
 * GEOGCS might be.  Returns -1 if no reasonable guess can be made.
13214
 *
13215
 * @return EPSG code
13216
 */
13217
13218
int OGRSpatialReference::GetEPSGGeogCS() const
13219
13220
0
{
13221
0
    TAKE_OPTIONAL_LOCK();
13222
13223
    /* -------------------------------------------------------------------- */
13224
    /*      Check axis order.                                               */
13225
    /* -------------------------------------------------------------------- */
13226
0
    auto poGeogCRS = std::unique_ptr<OGRSpatialReference>(CloneGeogCS());
13227
0
    if (!poGeogCRS)
13228
0
        return -1;
13229
13230
0
    bool ret = false;
13231
0
    poGeogCRS->d->demoteFromBoundCRS();
13232
0
    auto cs = proj_crs_get_coordinate_system(d->getPROJContext(),
13233
0
                                             poGeogCRS->d->m_pj_crs);
13234
0
    poGeogCRS->d->undoDemoteFromBoundCRS();
13235
0
    if (cs)
13236
0
    {
13237
0
        const char *pszDirection = nullptr;
13238
0
        if (proj_cs_get_axis_info(d->getPROJContext(), cs, 0, nullptr, nullptr,
13239
0
                                  &pszDirection, nullptr, nullptr, nullptr,
13240
0
                                  nullptr))
13241
0
        {
13242
0
            if (EQUAL(pszDirection, "north"))
13243
0
            {
13244
0
                ret = true;
13245
0
            }
13246
0
        }
13247
13248
0
        proj_destroy(cs);
13249
0
    }
13250
0
    if (!ret)
13251
0
        return -1;
13252
13253
    /* -------------------------------------------------------------------- */
13254
    /*      Do we already have it?                                          */
13255
    /* -------------------------------------------------------------------- */
13256
0
    const char *pszAuthName = GetAuthorityName("GEOGCS");
13257
0
    if (pszAuthName != nullptr && EQUAL(pszAuthName, "epsg"))
13258
0
        return atoi(GetAuthorityCode("GEOGCS"));
13259
13260
    /* -------------------------------------------------------------------- */
13261
    /*      Get the datum and geogcs names.                                 */
13262
    /* -------------------------------------------------------------------- */
13263
13264
0
    const char *pszGEOGCS = GetAttrValue("GEOGCS");
13265
0
    const char *pszDatum = GetAttrValue("DATUM");
13266
13267
    // We can only operate on coordinate systems with a geogcs.
13268
0
    OGRSpatialReference oSRSTmp;
13269
0
    if (pszGEOGCS == nullptr || pszDatum == nullptr)
13270
0
    {
13271
        // Calling GetAttrValue("GEOGCS") will fail on a CRS that can't be
13272
        // export to WKT1, so try to extract the geographic CRS through PROJ
13273
        // API with CopyGeogCSFrom() and get the nodes' values from it.
13274
0
        oSRSTmp.CopyGeogCSFrom(this);
13275
0
        pszGEOGCS = oSRSTmp.GetAttrValue("GEOGCS");
13276
0
        pszDatum = oSRSTmp.GetAttrValue("DATUM");
13277
0
        if (pszGEOGCS == nullptr || pszDatum == nullptr)
13278
0
        {
13279
0
            return -1;
13280
0
        }
13281
0
    }
13282
13283
    // Lookup geographic CRS name
13284
0
    const PJ_TYPE type = PJ_TYPE_GEOGRAPHIC_2D_CRS;
13285
0
    PJ_OBJ_LIST *list = proj_create_from_name(
13286
0
        d->getPROJContext(), nullptr, pszGEOGCS, &type, 1, false, 1, nullptr);
13287
0
    if (list)
13288
0
    {
13289
0
        const auto listSize = proj_list_get_count(list);
13290
0
        if (listSize == 1)
13291
0
        {
13292
0
            auto crs = proj_list_get(d->getPROJContext(), list, 0);
13293
0
            if (crs)
13294
0
            {
13295
0
                pszAuthName = proj_get_id_auth_name(crs, 0);
13296
0
                const char *pszCode = proj_get_id_code(crs, 0);
13297
0
                if (pszAuthName && pszCode && EQUAL(pszAuthName, "EPSG"))
13298
0
                {
13299
0
                    const int nCode = atoi(pszCode);
13300
0
                    proj_destroy(crs);
13301
0
                    proj_list_destroy(list);
13302
0
                    return nCode;
13303
0
                }
13304
0
                proj_destroy(crs);
13305
0
            }
13306
0
        }
13307
0
        proj_list_destroy(list);
13308
0
    }
13309
13310
    /* -------------------------------------------------------------------- */
13311
    /*      Is this a "well known" geographic coordinate system?            */
13312
    /* -------------------------------------------------------------------- */
13313
0
    const bool bWGS = strstr(pszGEOGCS, "WGS") != nullptr ||
13314
0
                      strstr(pszDatum, "WGS") ||
13315
0
                      strstr(pszGEOGCS, "World Geodetic System") ||
13316
0
                      strstr(pszGEOGCS, "World_Geodetic_System") ||
13317
0
                      strstr(pszDatum, "World Geodetic System") ||
13318
0
                      strstr(pszDatum, "World_Geodetic_System");
13319
13320
0
    const bool bNAD = strstr(pszGEOGCS, "NAD") != nullptr ||
13321
0
                      strstr(pszDatum, "NAD") ||
13322
0
                      strstr(pszGEOGCS, "North American") ||
13323
0
                      strstr(pszGEOGCS, "North_American") ||
13324
0
                      strstr(pszDatum, "North American") ||
13325
0
                      strstr(pszDatum, "North_American");
13326
13327
0
    if (bWGS && (strstr(pszGEOGCS, "84") || strstr(pszDatum, "84")))
13328
0
        return 4326;
13329
13330
0
    if (bWGS && (strstr(pszGEOGCS, "72") || strstr(pszDatum, "72")))
13331
0
        return 4322;
13332
13333
    // This is questionable as there are several 'flavors' of NAD83 that
13334
    // are not the same as 4269
13335
0
    if (bNAD && (strstr(pszGEOGCS, "83") || strstr(pszDatum, "83")))
13336
0
        return 4269;
13337
13338
0
    if (bNAD && (strstr(pszGEOGCS, "27") || strstr(pszDatum, "27")))
13339
0
        return 4267;
13340
13341
    /* -------------------------------------------------------------------- */
13342
    /*      If we know the datum, associate the most likely GCS with        */
13343
    /*      it.                                                             */
13344
    /* -------------------------------------------------------------------- */
13345
0
    const OGRSpatialReference &oActiveObj = oSRSTmp.IsEmpty() ? *this : oSRSTmp;
13346
0
    pszAuthName = oActiveObj.GetAuthorityName("GEOGCS|DATUM");
13347
0
    if (pszAuthName != nullptr && EQUAL(pszAuthName, "epsg") &&
13348
0
        GetPrimeMeridian() == 0.0)
13349
0
    {
13350
0
        const int nDatum = atoi(oActiveObj.GetAuthorityCode("GEOGCS|DATUM"));
13351
13352
0
        if (nDatum >= 6000 && nDatum <= 6999)
13353
0
            return nDatum - 2000;
13354
0
    }
13355
13356
0
    return -1;
13357
0
}
13358
13359
/************************************************************************/
13360
/*                          SetCoordinateEpoch()                        */
13361
/************************************************************************/
13362
13363
/** Set the coordinate epoch, as decimal year.
13364
 *
13365
 * In a dynamic CRS, coordinates of a point on the surface of the Earth may
13366
 * change with time. To be unambiguous the coordinates must always be qualified
13367
 * with the epoch at which they are valid. The coordinate epoch is not
13368
 * necessarily the epoch at which the observation was collected.
13369
 *
13370
 * Pedantically the coordinate epoch of an observation belongs to the
13371
 * observation, and not to the CRS, however it is often more practical to
13372
 * bind it to the CRS. The coordinate epoch should be specified for dynamic
13373
 * CRS (see IsDynamic())
13374
 *
13375
 * This method is the same as the OSRSetCoordinateEpoch() function.
13376
 *
13377
 * @param dfCoordinateEpoch Coordinate epoch as decimal year (e.g. 2021.3)
13378
 * @since OGR 3.4
13379
 */
13380
13381
void OGRSpatialReference::SetCoordinateEpoch(double dfCoordinateEpoch)
13382
0
{
13383
0
    d->m_coordinateEpoch = dfCoordinateEpoch;
13384
0
}
13385
13386
/************************************************************************/
13387
/*                      OSRSetCoordinateEpoch()                         */
13388
/************************************************************************/
13389
13390
/** \brief Set the coordinate epoch, as decimal year.
13391
 *
13392
 * See OGRSpatialReference::SetCoordinateEpoch()
13393
 *
13394
 * @since OGR 3.4
13395
 */
13396
void OSRSetCoordinateEpoch(OGRSpatialReferenceH hSRS, double dfCoordinateEpoch)
13397
0
{
13398
0
    VALIDATE_POINTER0(hSRS, "OSRSetCoordinateEpoch");
13399
13400
0
    return OGRSpatialReference::FromHandle(hSRS)->SetCoordinateEpoch(
13401
0
        dfCoordinateEpoch);
13402
0
}
13403
13404
/************************************************************************/
13405
/*                          GetCoordinateEpoch()                        */
13406
/************************************************************************/
13407
13408
/** Return the coordinate epoch, as decimal year.
13409
 *
13410
 * In a dynamic CRS, coordinates of a point on the surface of the Earth may
13411
 * change with time. To be unambiguous the coordinates must always be qualified
13412
 * with the epoch at which they are valid. The coordinate epoch is not
13413
 * necessarily the epoch at which the observation was collected.
13414
 *
13415
 * Pedantically the coordinate epoch of an observation belongs to the
13416
 * observation, and not to the CRS, however it is often more practical to
13417
 * bind it to the CRS. The coordinate epoch should be specified for dynamic
13418
 * CRS (see IsDynamic())
13419
 *
13420
 * This method is the same as the OSRGetCoordinateEpoch() function.
13421
 *
13422
 * @return coordinateEpoch Coordinate epoch as decimal year (e.g. 2021.3), or 0
13423
 *                         if not set, or relevant.
13424
 * @since OGR 3.4
13425
 */
13426
13427
double OGRSpatialReference::GetCoordinateEpoch() const
13428
0
{
13429
0
    return d->m_coordinateEpoch;
13430
0
}
13431
13432
/************************************************************************/
13433
/*                      OSRGetCoordinateEpoch()                        */
13434
/************************************************************************/
13435
13436
/** \brief Get the coordinate epoch, as decimal year.
13437
 *
13438
 * See OGRSpatialReference::GetCoordinateEpoch()
13439
 *
13440
 * @since OGR 3.4
13441
 */
13442
double OSRGetCoordinateEpoch(OGRSpatialReferenceH hSRS)
13443
0
{
13444
0
    VALIDATE_POINTER1(hSRS, "OSRGetCoordinateEpoch", 0);
13445
13446
0
    return OGRSpatialReference::FromHandle(hSRS)->GetCoordinateEpoch();
13447
0
}