Coverage Report

Created: 2025-12-31 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/ogr/ogrspatialreference.cpp
Line
Count
Source
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 final : 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), m_poListener(std::make_shared<Listener>(this))
229
0
{
230
    // Get the default value for m_axisMappingStrategy from the
231
    // OSR_DEFAULT_AXIS_MAPPING_STRATEGY configuration option, if set.
232
0
    m_axisMappingStrategy = GetDefaultAxisMappingStrategy();
233
0
}
234
235
OGRSpatialReference::Private::~Private()
236
0
{
237
    // In case we destroy the object not in the thread that created it,
238
    // we need to reassign the PROJ context. Having the context bundled inside
239
    // PJ* deeply sucks...
240
0
    PJ_CONTEXT *pj_context_to_destroy = nullptr;
241
0
    PJ_CONTEXT *ctxt;
242
0
    if (GDALThreadLocalDatasetCacheIsInDestruction())
243
0
    {
244
0
        pj_context_to_destroy = proj_context_create();
245
0
        ctxt = pj_context_to_destroy;
246
0
    }
247
0
    else
248
0
    {
249
0
        ctxt = getPROJContext();
250
0
    }
251
252
0
    proj_assign_context(m_pj_crs, ctxt);
253
0
    proj_destroy(m_pj_crs);
254
255
0
    proj_assign_context(m_pj_geod_base_crs_temp, ctxt);
256
0
    proj_destroy(m_pj_geod_base_crs_temp);
257
258
0
    proj_assign_context(m_pj_proj_crs_cs_temp, ctxt);
259
0
    proj_destroy(m_pj_proj_crs_cs_temp);
260
261
0
    proj_assign_context(m_pj_bound_crs_target, ctxt);
262
0
    proj_destroy(m_pj_bound_crs_target);
263
264
0
    proj_assign_context(m_pj_bound_crs_co, ctxt);
265
0
    proj_destroy(m_pj_bound_crs_co);
266
267
0
    proj_assign_context(m_pj_crs_backup, ctxt);
268
0
    proj_destroy(m_pj_crs_backup);
269
270
0
    delete m_poRootBackup;
271
0
    delete m_poRoot;
272
0
    proj_context_destroy(pj_context_to_destroy);
273
0
}
274
275
void OGRSpatialReference::Private::clear()
276
0
{
277
0
    proj_assign_context(m_pj_crs, getPROJContext());
278
0
    proj_destroy(m_pj_crs);
279
0
    m_pj_crs = nullptr;
280
281
0
    delete m_poRoot;
282
0
    m_poRoot = nullptr;
283
0
    m_bNodesChanged = false;
284
285
0
    m_wktImportWarnings.clear();
286
0
    m_wktImportErrors.clear();
287
288
0
    m_pj_crs_modified_during_demote = false;
289
0
    m_pjType = PJ_TYPE_UNKNOWN;
290
0
    m_osPrimeMeridianName.clear();
291
0
    m_osAngularUnits.clear();
292
0
    m_osLinearUnits.clear();
293
294
0
    bNormInfoSet = FALSE;
295
0
    dfFromGreenwich = 1.0;
296
0
    dfToMeter = 1.0;
297
0
    dfToDegrees = 1.0;
298
0
    m_dfAngularUnitToRadian = 0.0;
299
300
0
    m_bMorphToESRI = false;
301
0
    m_bHasCenterLong = false;
302
303
0
    m_coordinateEpoch = 0.0;
304
0
}
305
306
void OGRSpatialReference::Private::setRoot(OGR_SRSNode *poRoot)
307
0
{
308
0
    m_poRoot = poRoot;
309
0
    if (m_poRoot)
310
0
    {
311
0
        m_poRoot->RegisterListener(m_poListener);
312
0
    }
313
0
    nodesChanged();
314
0
}
315
316
void OGRSpatialReference::Private::setPjCRS(PJ *pj_crsIn,
317
                                            bool doRefreshAxisMapping)
318
0
{
319
0
    auto ctxt = getPROJContext();
320
321
0
#if PROJ_AT_LEAST_VERSION(9, 2, 0)
322
0
    if (proj_get_type(pj_crsIn) == PJ_TYPE_COORDINATE_METADATA)
323
0
    {
324
0
        const double dfEpoch =
325
0
            proj_coordinate_metadata_get_epoch(ctxt, pj_crsIn);
326
0
        if (!std::isnan(dfEpoch))
327
0
        {
328
0
            m_poSelf->SetCoordinateEpoch(dfEpoch);
329
0
        }
330
0
        auto crs = proj_get_source_crs(ctxt, pj_crsIn);
331
0
        proj_destroy(pj_crsIn);
332
0
        pj_crsIn = crs;
333
0
    }
334
0
#endif
335
336
0
    proj_assign_context(m_pj_crs, ctxt);
337
0
    proj_destroy(m_pj_crs);
338
0
    m_pj_crs = pj_crsIn;
339
0
    if (m_pj_crs)
340
0
    {
341
0
        m_pjType = proj_get_type(m_pj_crs);
342
0
    }
343
0
    if (m_pj_crs_backup)
344
0
    {
345
0
        m_pj_crs_modified_during_demote = true;
346
0
    }
347
0
    invalidateNodes();
348
0
    if (doRefreshAxisMapping)
349
0
    {
350
0
        refreshAxisMapping();
351
0
    }
352
0
}
353
354
void OGRSpatialReference::Private::refreshProjObj()
355
0
{
356
0
    if (m_bNodesChanged && m_poRoot)
357
0
    {
358
0
        char *pszWKT = nullptr;
359
0
        m_poRoot->exportToWkt(&pszWKT);
360
0
        auto poRootBackup = m_poRoot;
361
0
        m_poRoot = nullptr;
362
0
        const double dfCoordinateEpochBackup = m_coordinateEpoch;
363
0
        clear();
364
0
        m_coordinateEpoch = dfCoordinateEpochBackup;
365
0
        m_bHasCenterLong = strstr(pszWKT, "CENTER_LONG") != nullptr;
366
367
0
        const char *const options[] = {
368
0
            "STRICT=NO",
369
0
#if PROJ_AT_LEAST_VERSION(9, 1, 0)
370
0
            "UNSET_IDENTIFIERS_IF_INCOMPATIBLE_DEF=NO",
371
0
#endif
372
0
            nullptr
373
0
        };
374
0
        PROJ_STRING_LIST warnings = nullptr;
375
0
        PROJ_STRING_LIST errors = nullptr;
376
0
        setPjCRS(proj_create_from_wkt(getPROJContext(), pszWKT, options,
377
0
                                      &warnings, &errors));
378
0
        for (auto iter = warnings; iter && *iter; ++iter)
379
0
        {
380
0
            m_wktImportWarnings.push_back(*iter);
381
0
        }
382
0
        for (auto iter = errors; iter && *iter; ++iter)
383
0
        {
384
0
            m_wktImportErrors.push_back(*iter);
385
0
        }
386
0
        proj_string_list_destroy(warnings);
387
0
        proj_string_list_destroy(errors);
388
389
0
        CPLFree(pszWKT);
390
391
0
        m_poRoot = poRootBackup;
392
0
        m_bNodesChanged = false;
393
0
    }
394
0
}
395
396
void OGRSpatialReference::Private::refreshRootFromProjObj()
397
0
{
398
0
    CPLAssert(m_poRoot == nullptr);
399
400
0
    if (m_pj_crs)
401
0
    {
402
0
        CPLStringList aosOptions;
403
0
        if (!m_bMorphToESRI)
404
0
        {
405
0
            aosOptions.SetNameValue("OUTPUT_AXIS", "YES");
406
0
            aosOptions.SetNameValue("MULTILINE", "NO");
407
0
        }
408
0
        aosOptions.SetNameValue("STRICT", "NO");
409
410
0
        const char *pszWKT;
411
0
        {
412
0
            CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
413
0
            pszWKT = proj_as_wkt(getPROJContext(), m_pj_crs,
414
0
                                 m_bMorphToESRI ? PJ_WKT1_ESRI : PJ_WKT1_GDAL,
415
0
                                 aosOptions.List());
416
0
            m_bNodesWKT2 = false;
417
0
        }
418
0
        if (!m_bMorphToESRI && pszWKT == nullptr)
419
0
        {
420
0
            pszWKT = proj_as_wkt(getPROJContext(), m_pj_crs, PJ_WKT2_2018,
421
0
                                 aosOptions.List());
422
0
            m_bNodesWKT2 = true;
423
0
        }
424
0
        if (pszWKT)
425
0
        {
426
0
            auto root = new OGR_SRSNode();
427
0
            setRoot(root);
428
0
            root->importFromWkt(&pszWKT);
429
0
            m_bNodesChanged = false;
430
0
        }
431
0
    }
432
0
}
433
434
static bool isNorthEastAxisOrder(PJ_CONTEXT *ctx, PJ *cs)
435
0
{
436
0
    const char *pszName1 = nullptr;
437
0
    const char *pszDirection1 = nullptr;
438
0
    proj_cs_get_axis_info(ctx, cs, 0, &pszName1, nullptr, &pszDirection1,
439
0
                          nullptr, nullptr, nullptr, nullptr);
440
0
    const char *pszName2 = nullptr;
441
0
    const char *pszDirection2 = nullptr;
442
0
    proj_cs_get_axis_info(ctx, cs, 1, &pszName2, nullptr, &pszDirection2,
443
0
                          nullptr, nullptr, nullptr, nullptr);
444
0
    if (pszDirection1 && EQUAL(pszDirection1, "north") && pszDirection2 &&
445
0
        EQUAL(pszDirection2, "east"))
446
0
    {
447
0
        return true;
448
0
    }
449
0
    if (pszDirection1 && pszDirection2 &&
450
0
        ((EQUAL(pszDirection1, "north") && EQUAL(pszDirection2, "north")) ||
451
0
         (EQUAL(pszDirection1, "south") && EQUAL(pszDirection2, "south"))) &&
452
0
        pszName1 && STARTS_WITH_CI(pszName1, "northing") && pszName2 &&
453
0
        STARTS_WITH_CI(pszName2, "easting"))
454
0
    {
455
0
        return true;
456
0
    }
457
0
    return false;
458
0
}
459
460
void OGRSpatialReference::Private::refreshAxisMapping()
461
0
{
462
0
    if (!m_pj_crs || m_axisMappingStrategy == OAMS_CUSTOM)
463
0
        return;
464
465
0
    bool doUndoDemote = false;
466
0
    if (m_pj_crs_backup == nullptr)
467
0
    {
468
0
        doUndoDemote = true;
469
0
        demoteFromBoundCRS();
470
0
    }
471
0
    const auto ctxt = getPROJContext();
472
0
    PJ *horizCRS = nullptr;
473
0
    int axisCount = 0;
474
0
    if (m_pjType == PJ_TYPE_VERTICAL_CRS)
475
0
    {
476
0
        axisCount = 1;
477
0
    }
478
0
    else if (m_pjType == PJ_TYPE_COMPOUND_CRS)
479
0
    {
480
0
        horizCRS = proj_crs_get_sub_crs(ctxt, m_pj_crs, 0);
481
0
        if (horizCRS && proj_get_type(horizCRS) == PJ_TYPE_BOUND_CRS)
482
0
        {
483
0
            auto baseCRS = proj_get_source_crs(ctxt, horizCRS);
484
0
            if (baseCRS)
485
0
            {
486
0
                proj_destroy(horizCRS);
487
0
                horizCRS = baseCRS;
488
0
            }
489
0
        }
490
491
0
        auto vertCRS = proj_crs_get_sub_crs(ctxt, m_pj_crs, 1);
492
0
        if (vertCRS)
493
0
        {
494
0
            if (proj_get_type(vertCRS) == PJ_TYPE_BOUND_CRS)
495
0
            {
496
0
                auto baseCRS = proj_get_source_crs(ctxt, vertCRS);
497
0
                if (baseCRS)
498
0
                {
499
0
                    proj_destroy(vertCRS);
500
0
                    vertCRS = baseCRS;
501
0
                }
502
0
            }
503
504
0
            auto cs = proj_crs_get_coordinate_system(ctxt, vertCRS);
505
0
            if (cs)
506
0
            {
507
0
                axisCount += proj_cs_get_axis_count(ctxt, cs);
508
0
                proj_destroy(cs);
509
0
            }
510
0
            proj_destroy(vertCRS);
511
0
        }
512
0
    }
513
0
    else
514
0
    {
515
0
        horizCRS = m_pj_crs;
516
0
    }
517
518
0
    bool bSwitchForGisFriendlyOrder = false;
519
0
    if (horizCRS)
520
0
    {
521
0
        auto cs = proj_crs_get_coordinate_system(ctxt, horizCRS);
522
0
        if (cs)
523
0
        {
524
0
            int nHorizCSAxisCount = proj_cs_get_axis_count(ctxt, cs);
525
0
            axisCount += nHorizCSAxisCount;
526
0
            if (nHorizCSAxisCount >= 2)
527
0
            {
528
0
                bSwitchForGisFriendlyOrder = isNorthEastAxisOrder(ctxt, cs);
529
0
            }
530
0
            proj_destroy(cs);
531
0
        }
532
0
    }
533
0
    if (horizCRS != m_pj_crs)
534
0
    {
535
0
        proj_destroy(horizCRS);
536
0
    }
537
0
    if (doUndoDemote)
538
0
    {
539
0
        undoDemoteFromBoundCRS();
540
0
    }
541
542
0
    m_axisMapping.resize(axisCount);
543
0
    if (m_axisMappingStrategy == OAMS_AUTHORITY_COMPLIANT ||
544
0
        !bSwitchForGisFriendlyOrder)
545
0
    {
546
0
        for (int i = 0; i < axisCount; i++)
547
0
        {
548
0
            m_axisMapping[i] = i + 1;
549
0
        }
550
0
    }
551
0
    else
552
0
    {
553
0
        m_axisMapping[0] = 2;
554
0
        m_axisMapping[1] = 1;
555
0
        if (axisCount == 3)
556
0
        {
557
0
            m_axisMapping[2] = 3;
558
0
        }
559
0
    }
560
0
}
561
562
void OGRSpatialReference::Private::nodesChanged()
563
0
{
564
0
    m_bNodesChanged = true;
565
0
}
566
567
void OGRSpatialReference::Private::invalidateNodes()
568
0
{
569
0
    delete m_poRoot;
570
0
    m_poRoot = nullptr;
571
0
    m_bNodesChanged = false;
572
0
}
573
574
void OGRSpatialReference::Private::setMorphToESRI(bool b)
575
0
{
576
0
    invalidateNodes();
577
0
    m_bMorphToESRI = b;
578
0
}
579
580
void OGRSpatialReference::Private::demoteFromBoundCRS()
581
0
{
582
0
    CPLAssert(m_pj_bound_crs_target == nullptr);
583
0
    CPLAssert(m_pj_bound_crs_co == nullptr);
584
0
    CPLAssert(m_poRootBackup == nullptr);
585
0
    CPLAssert(m_pj_crs_backup == nullptr);
586
587
0
    m_pj_crs_modified_during_demote = false;
588
589
0
    if (m_pjType == PJ_TYPE_BOUND_CRS)
590
0
    {
591
0
        auto baseCRS = proj_get_source_crs(getPROJContext(), m_pj_crs);
592
0
        m_pj_bound_crs_target = proj_get_target_crs(getPROJContext(), m_pj_crs);
593
0
        m_pj_bound_crs_co =
594
0
            proj_crs_get_coordoperation(getPROJContext(), m_pj_crs);
595
596
0
        m_poRootBackup = m_poRoot;
597
0
        m_poRoot = nullptr;
598
0
        m_pj_crs_backup = m_pj_crs;
599
0
        m_pj_crs = baseCRS;
600
0
        m_pjType = proj_get_type(m_pj_crs);
601
0
    }
602
0
}
603
604
void OGRSpatialReference::Private::undoDemoteFromBoundCRS()
605
0
{
606
0
    if (m_pj_bound_crs_target)
607
0
    {
608
0
        CPLAssert(m_poRoot == nullptr);
609
0
        CPLAssert(m_pj_crs);
610
0
        if (!m_pj_crs_modified_during_demote)
611
0
        {
612
0
            proj_destroy(m_pj_crs);
613
0
            m_pj_crs = m_pj_crs_backup;
614
0
            m_pjType = proj_get_type(m_pj_crs);
615
0
            m_poRoot = m_poRootBackup;
616
0
        }
617
0
        else
618
0
        {
619
0
            delete m_poRootBackup;
620
0
            m_poRootBackup = nullptr;
621
0
            proj_destroy(m_pj_crs_backup);
622
0
            m_pj_crs_backup = nullptr;
623
0
            setPjCRS(proj_crs_create_bound_crs(getPROJContext(), m_pj_crs,
624
0
                                               m_pj_bound_crs_target,
625
0
                                               m_pj_bound_crs_co),
626
0
                     false);
627
0
        }
628
0
    }
629
630
0
    m_poRootBackup = nullptr;
631
0
    m_pj_crs_backup = nullptr;
632
0
    proj_destroy(m_pj_bound_crs_target);
633
0
    m_pj_bound_crs_target = nullptr;
634
0
    proj_destroy(m_pj_bound_crs_co);
635
0
    m_pj_bound_crs_co = nullptr;
636
0
    m_pj_crs_modified_during_demote = false;
637
0
}
638
639
const char *OGRSpatialReference::Private::nullifyTargetKeyIfPossible(
640
    const char *pszTargetKey)
641
0
{
642
0
    if (pszTargetKey)
643
0
    {
644
0
        demoteFromBoundCRS();
645
0
        if ((m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
646
0
             m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS) &&
647
0
            EQUAL(pszTargetKey, "GEOGCS"))
648
0
        {
649
0
            pszTargetKey = nullptr;
650
0
        }
651
0
        else if (m_pjType == PJ_TYPE_GEOCENTRIC_CRS &&
652
0
                 EQUAL(pszTargetKey, "GEOCCS"))
653
0
        {
654
0
            pszTargetKey = nullptr;
655
0
        }
656
0
        else if (m_pjType == PJ_TYPE_PROJECTED_CRS &&
657
0
                 EQUAL(pszTargetKey, "PROJCS"))
658
0
        {
659
0
            pszTargetKey = nullptr;
660
0
        }
661
0
        else if (m_pjType == PJ_TYPE_VERTICAL_CRS &&
662
0
                 EQUAL(pszTargetKey, "VERT_CS"))
663
0
        {
664
0
            pszTargetKey = nullptr;
665
0
        }
666
0
        undoDemoteFromBoundCRS();
667
0
    }
668
0
    return pszTargetKey;
669
0
}
670
671
PJ *OGRSpatialReference::Private::getGeodBaseCRS()
672
0
{
673
0
    if (m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
674
0
        m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS)
675
0
    {
676
0
        return m_pj_crs;
677
0
    }
678
679
0
    auto ctxt = getPROJContext();
680
0
    if (m_pjType == PJ_TYPE_PROJECTED_CRS)
681
0
    {
682
0
        proj_assign_context(m_pj_geod_base_crs_temp, ctxt);
683
0
        proj_destroy(m_pj_geod_base_crs_temp);
684
0
        m_pj_geod_base_crs_temp = proj_crs_get_geodetic_crs(ctxt, m_pj_crs);
685
0
        return m_pj_geod_base_crs_temp;
686
0
    }
687
688
0
    proj_assign_context(m_pj_geod_base_crs_temp, ctxt);
689
0
    proj_destroy(m_pj_geod_base_crs_temp);
690
0
    auto cs = proj_create_ellipsoidal_2D_cs(ctxt, PJ_ELLPS2D_LATITUDE_LONGITUDE,
691
0
                                            nullptr, 0);
692
0
    m_pj_geod_base_crs_temp = proj_create_geographic_crs(
693
0
        ctxt, "WGS 84", "World Geodetic System 1984", "WGS 84",
694
0
        SRS_WGS84_SEMIMAJOR, SRS_WGS84_INVFLATTENING, SRS_PM_GREENWICH, 0.0,
695
0
        SRS_UA_DEGREE, CPLAtof(SRS_UA_DEGREE_CONV), cs);
696
0
    proj_destroy(cs);
697
698
0
    return m_pj_geod_base_crs_temp;
699
0
}
700
701
PJ *OGRSpatialReference::Private::getProjCRSCoordSys()
702
0
{
703
0
    auto ctxt = getPROJContext();
704
0
    if (m_pjType == PJ_TYPE_PROJECTED_CRS)
705
0
    {
706
0
        proj_assign_context(m_pj_proj_crs_cs_temp, ctxt);
707
0
        proj_destroy(m_pj_proj_crs_cs_temp);
708
0
        m_pj_proj_crs_cs_temp =
709
0
            proj_crs_get_coordinate_system(getPROJContext(), m_pj_crs);
710
0
        return m_pj_proj_crs_cs_temp;
711
0
    }
712
713
0
    proj_assign_context(m_pj_proj_crs_cs_temp, ctxt);
714
0
    proj_destroy(m_pj_proj_crs_cs_temp);
715
0
    m_pj_proj_crs_cs_temp = proj_create_cartesian_2D_cs(
716
0
        ctxt, PJ_CART2D_EASTING_NORTHING, nullptr, 0);
717
0
    return m_pj_proj_crs_cs_temp;
718
0
}
719
720
const char *OGRSpatialReference::Private::getProjCRSName()
721
0
{
722
0
    if (m_pjType == PJ_TYPE_PROJECTED_CRS)
723
0
    {
724
0
        return proj_get_name(m_pj_crs);
725
0
    }
726
727
0
    return "unnamed";
728
0
}
729
730
OGRErr OGRSpatialReference::Private::replaceConversionAndUnref(PJ *conv)
731
0
{
732
0
    refreshProjObj();
733
734
0
    demoteFromBoundCRS();
735
736
0
    auto projCRS =
737
0
        proj_create_projected_crs(getPROJContext(), getProjCRSName(),
738
0
                                  getGeodBaseCRS(), conv, getProjCRSCoordSys());
739
0
    proj_destroy(conv);
740
741
0
    setPjCRS(projCRS);
742
743
0
    undoDemoteFromBoundCRS();
744
0
    return OGRERR_NONE;
745
0
}
746
747
/************************************************************************/
748
/*                           ToPointer()                                */
749
/************************************************************************/
750
751
static inline OGRSpatialReference *ToPointer(OGRSpatialReferenceH hSRS)
752
0
{
753
0
    return OGRSpatialReference::FromHandle(hSRS);
754
0
}
755
756
/************************************************************************/
757
/*                           ToHandle()                                 */
758
/************************************************************************/
759
760
static inline OGRSpatialReferenceH ToHandle(OGRSpatialReference *poSRS)
761
0
{
762
0
    return OGRSpatialReference::ToHandle(poSRS);
763
0
}
764
765
/************************************************************************/
766
/*                           OGRsnPrintDouble()                         */
767
/************************************************************************/
768
769
void OGRsnPrintDouble(char *pszStrBuf, size_t size, double dfValue);
770
771
void OGRsnPrintDouble(char *pszStrBuf, size_t size, double dfValue)
772
773
0
{
774
0
    CPLsnprintf(pszStrBuf, size, "%.16g", dfValue);
775
776
0
    const size_t nLen = strlen(pszStrBuf);
777
778
    // The following hack is intended to truncate some "precision" in cases
779
    // that appear to be roundoff error.
780
0
    if (nLen > 15 && (strcmp(pszStrBuf + nLen - 6, "999999") == 0 ||
781
0
                      strcmp(pszStrBuf + nLen - 6, "000001") == 0))
782
0
    {
783
0
        CPLsnprintf(pszStrBuf, size, "%.15g", dfValue);
784
0
    }
785
786
    // Force to user periods regardless of locale.
787
0
    if (strchr(pszStrBuf, ',') != nullptr)
788
0
    {
789
0
        char *const pszDelim = strchr(pszStrBuf, ',');
790
0
        *pszDelim = '.';
791
0
    }
792
0
}
793
794
/************************************************************************/
795
/*                        OGRSpatialReference()                         */
796
/************************************************************************/
797
798
/**
799
 * \brief Constructor.
800
 *
801
 * This constructor takes an optional string argument which if passed
802
 * should be a WKT representation of an SRS.  Passing this is equivalent
803
 * to not passing it, and then calling importFromWkt() with the WKT string.
804
 *
805
 * Note that newly created objects are given a reference count of one.
806
 *
807
 * Starting with GDAL 3.0, coordinates associated with a OGRSpatialReference
808
 * object are assumed to be in the order of the axis of the CRS definition
809
 (which
810
 * for example means latitude first, longitude second for geographic CRS
811
 belonging
812
 * to the EPSG authority). It is possible to define a data axis to CRS axis
813
 * mapping strategy with the SetAxisMappingStrategy() method.
814
 *
815
 * Starting with GDAL 3.5, the OSR_DEFAULT_AXIS_MAPPING_STRATEGY configuration
816
 * option can be set to "TRADITIONAL_GIS_ORDER" / "AUTHORITY_COMPLIANT" (the
817
 later
818
 * being the default value when the option is not set) to control the value of
819
 the
820
 * data axis to CRS axis mapping strategy when a OSRSpatialReference object is
821
 * created. Calling SetAxisMappingStrategy() will override this default value.
822
823
 * The C function OSRNewSpatialReference() does the same thing as this
824
 * constructor.
825
 *
826
 * @param pszWKT well known text definition to which the object should
827
 * be initialized, or NULL (the default).
828
 */
829
830
OGRSpatialReference::OGRSpatialReference(const char *pszWKT)
831
0
    : d(new Private(this))
832
0
{
833
0
    if (pszWKT != nullptr)
834
0
        importFromWkt(pszWKT);
835
0
}
836
837
/************************************************************************/
838
/*                       OSRNewSpatialReference()                       */
839
/************************************************************************/
840
841
/**
842
 * \brief Constructor.
843
 *
844
 * Starting with GDAL 3.0, coordinates associated with a OGRSpatialReference
845
 * object are assumed to be in the order of the axis of the CRS definition
846
 * (which for example means latitude first, longitude second for geographic CRS
847
 * belonging to the EPSG authority). It is possible to define a data axis to CRS
848
 * axis mapping strategy with the SetAxisMappingStrategy() method.
849
 *
850
 * Starting with GDAL 3.5, the OSR_DEFAULT_AXIS_MAPPING_STRATEGY configuration
851
 * option can be set to "TRADITIONAL_GIS_ORDER" / "AUTHORITY_COMPLIANT" (the
852
 * later being the default value when the option is not set) to control the
853
 * value of the data axis to CRS axis mapping strategy when a
854
 * OSRSpatialReference object is created. Calling SetAxisMappingStrategy() will
855
 * override this default value.
856
 *
857
 * This function is the same as OGRSpatialReference::OGRSpatialReference()
858
 */
859
OGRSpatialReferenceH CPL_STDCALL OSRNewSpatialReference(const char *pszWKT)
860
861
0
{
862
0
    OGRSpatialReference *poSRS = new OGRSpatialReference();
863
864
0
    if (pszWKT != nullptr && strlen(pszWKT) > 0)
865
0
    {
866
0
        if (poSRS->importFromWkt(pszWKT) != OGRERR_NONE)
867
0
        {
868
0
            delete poSRS;
869
0
            poSRS = nullptr;
870
0
        }
871
0
    }
872
873
0
    return ToHandle(poSRS);
874
0
}
875
876
/************************************************************************/
877
/*                        OGRSpatialReference()                         */
878
/************************************************************************/
879
880
/** Copy constructor. See also Clone().
881
 * @param oOther other spatial reference
882
 */
883
OGRSpatialReference::OGRSpatialReference(const OGRSpatialReference &oOther)
884
0
    : d(new Private(this))
885
0
{
886
0
    *this = oOther;
887
0
}
888
889
/************************************************************************/
890
/*                        OGRSpatialReference()                         */
891
/************************************************************************/
892
893
/** Move constructor.
894
 * @param oOther other spatial reference
895
 */
896
OGRSpatialReference::OGRSpatialReference(OGRSpatialReference &&oOther)
897
0
    : d(std::move(oOther.d))
898
0
{
899
0
}
900
901
/************************************************************************/
902
/*                        ~OGRSpatialReference()                        */
903
/************************************************************************/
904
905
/**
906
 * \brief OGRSpatialReference destructor.
907
 *
908
 * The C function OSRDestroySpatialReference() does the same thing as this
909
 * method. Preferred C++ method : OGRSpatialReference::DestroySpatialReference()
910
 *
911
 * @deprecated
912
 */
913
914
OGRSpatialReference::~OGRSpatialReference()
915
916
0
{
917
0
}
918
919
/************************************************************************/
920
/*                      DestroySpatialReference()                       */
921
/************************************************************************/
922
923
/**
924
 * \brief OGRSpatialReference destructor.
925
 *
926
 * This static method will destroy a OGRSpatialReference.  It is
927
 * equivalent to calling delete on the object, but it ensures that the
928
 * deallocation is properly executed within the OGR libraries heap on
929
 * platforms where this can matter (win32).
930
 *
931
 * This function is the same as OSRDestroySpatialReference()
932
 *
933
 * @param poSRS the object to delete
934
 *
935
 */
936
937
void OGRSpatialReference::DestroySpatialReference(OGRSpatialReference *poSRS)
938
0
{
939
0
    delete poSRS;
940
0
}
941
942
/************************************************************************/
943
/*                     OSRDestroySpatialReference()                     */
944
/************************************************************************/
945
946
/**
947
 * \brief OGRSpatialReference destructor.
948
 *
949
 * This function is the same as OGRSpatialReference::~OGRSpatialReference()
950
 * and OGRSpatialReference::DestroySpatialReference()
951
 *
952
 * @param hSRS the object to delete
953
 */
954
void CPL_STDCALL OSRDestroySpatialReference(OGRSpatialReferenceH hSRS)
955
956
0
{
957
0
    delete ToPointer(hSRS);
958
0
}
959
960
/************************************************************************/
961
/*                               Clear()                                */
962
/************************************************************************/
963
964
/**
965
 * \brief Wipe current definition.
966
 *
967
 * Returns OGRSpatialReference to a state with no definition, as it
968
 * exists when first created.  It does not affect reference counts.
969
 */
970
971
void OGRSpatialReference::Clear()
972
973
0
{
974
0
    d->clear();
975
0
}
976
977
/************************************************************************/
978
/*                             operator=()                              */
979
/************************************************************************/
980
981
/** Assignment operator.
982
 * @param oSource SRS to assign to *this
983
 * @return *this
984
 */
985
OGRSpatialReference &
986
OGRSpatialReference::operator=(const OGRSpatialReference &oSource)
987
988
0
{
989
0
    if (&oSource != this)
990
0
    {
991
0
        Clear();
992
#ifdef CPPCHECK
993
        // Otherwise cppcheck would protest that nRefCount isn't modified
994
        d->nRefCount = (d->nRefCount + 1) - 1;
995
#endif
996
997
0
        oSource.d->refreshProjObj();
998
0
        if (oSource.d->m_pj_crs)
999
0
            d->setPjCRS(proj_clone(d->getPROJContext(), oSource.d->m_pj_crs));
1000
0
        if (oSource.d->m_axisMappingStrategy == OAMS_TRADITIONAL_GIS_ORDER)
1001
0
            SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
1002
0
        else if (oSource.d->m_axisMappingStrategy == OAMS_CUSTOM)
1003
0
            SetDataAxisToSRSAxisMapping(oSource.d->m_axisMapping);
1004
1005
0
        d->m_coordinateEpoch = oSource.d->m_coordinateEpoch;
1006
0
    }
1007
1008
0
    return *this;
1009
0
}
1010
1011
/************************************************************************/
1012
/*                             operator=()                              */
1013
/************************************************************************/
1014
1015
/** Move assignment operator.
1016
 * @param oSource SRS to assign to *this
1017
 * @return *this
1018
 */
1019
OGRSpatialReference &
1020
OGRSpatialReference::operator=(OGRSpatialReference &&oSource)
1021
1022
0
{
1023
0
    if (&oSource != this)
1024
0
    {
1025
0
        d = std::move(oSource.d);
1026
0
    }
1027
1028
0
    return *this;
1029
0
}
1030
1031
/************************************************************************/
1032
/*                      AssignAndSetThreadSafe()                        */
1033
/************************************************************************/
1034
1035
/** Assignment method, with thread-safety.
1036
 *
1037
 * Same as an assignment operator, but asking also that the *this instance
1038
 * becomes thread-safe.
1039
 *
1040
 * @param oSource SRS to assign to *this
1041
 * @return *this
1042
 * @since 3.10
1043
 */
1044
1045
OGRSpatialReference &
1046
OGRSpatialReference::AssignAndSetThreadSafe(const OGRSpatialReference &oSource)
1047
0
{
1048
0
    *this = oSource;
1049
0
    d->SetThreadSafe();
1050
0
    return *this;
1051
0
}
1052
1053
/************************************************************************/
1054
/*                             Reference()                              */
1055
/************************************************************************/
1056
1057
/**
1058
 * \brief Increments the reference count by one.
1059
 *
1060
 * The reference count is used keep track of the number of OGRGeometry objects
1061
 * referencing this SRS.
1062
 *
1063
 * The method does the same thing as the C function OSRReference().
1064
 *
1065
 * @return the updated reference count.
1066
 */
1067
1068
int OGRSpatialReference::Reference()
1069
1070
0
{
1071
0
    return CPLAtomicInc(&d->nRefCount);
1072
0
}
1073
1074
/************************************************************************/
1075
/*                            OSRReference()                            */
1076
/************************************************************************/
1077
1078
/**
1079
 * \brief Increments the reference count by one.
1080
 *
1081
 * This function is the same as OGRSpatialReference::Reference()
1082
 */
1083
int OSRReference(OGRSpatialReferenceH hSRS)
1084
1085
0
{
1086
0
    VALIDATE_POINTER1(hSRS, "OSRReference", 0);
1087
1088
0
    return ToPointer(hSRS)->Reference();
1089
0
}
1090
1091
/************************************************************************/
1092
/*                            Dereference()                             */
1093
/************************************************************************/
1094
1095
/**
1096
 * \brief Decrements the reference count by one.
1097
 *
1098
 * The method does the same thing as the C function OSRDereference().
1099
 *
1100
 * @return the updated reference count.
1101
 */
1102
1103
int OGRSpatialReference::Dereference()
1104
1105
0
{
1106
0
    if (d->nRefCount <= 0)
1107
0
        CPLDebug("OSR",
1108
0
                 "Dereference() called on an object with refcount %d,"
1109
0
                 "likely already destroyed!",
1110
0
                 d->nRefCount);
1111
0
    return CPLAtomicDec(&d->nRefCount);
1112
0
}
1113
1114
/************************************************************************/
1115
/*                           OSRDereference()                           */
1116
/************************************************************************/
1117
1118
/**
1119
 * \brief Decrements the reference count by one.
1120
 *
1121
 * This function is the same as OGRSpatialReference::Dereference()
1122
 */
1123
int OSRDereference(OGRSpatialReferenceH hSRS)
1124
1125
0
{
1126
0
    VALIDATE_POINTER1(hSRS, "OSRDereference", 0);
1127
1128
0
    return ToPointer(hSRS)->Dereference();
1129
0
}
1130
1131
/************************************************************************/
1132
/*                         GetReferenceCount()                          */
1133
/************************************************************************/
1134
1135
/**
1136
 * \brief Fetch current reference count.
1137
 *
1138
 * @return the current reference count.
1139
 */
1140
int OGRSpatialReference::GetReferenceCount() const
1141
0
{
1142
0
    return d->nRefCount;
1143
0
}
1144
1145
/************************************************************************/
1146
/*                              Release()                               */
1147
/************************************************************************/
1148
1149
/**
1150
 * \brief Decrements the reference count by one, and destroy if zero.
1151
 *
1152
 * The method does the same thing as the C function OSRRelease().
1153
 */
1154
1155
void OGRSpatialReference::Release()
1156
1157
0
{
1158
0
    if (Dereference() <= 0)
1159
0
        delete this;
1160
0
}
1161
1162
/************************************************************************/
1163
/*                             OSRRelease()                             */
1164
/************************************************************************/
1165
1166
/**
1167
 * \brief Decrements the reference count by one, and destroy if zero.
1168
 *
1169
 * This function is the same as OGRSpatialReference::Release()
1170
 */
1171
void OSRRelease(OGRSpatialReferenceH hSRS)
1172
1173
0
{
1174
0
    VALIDATE_POINTER0(hSRS, "OSRRelease");
1175
1176
0
    ToPointer(hSRS)->Release();
1177
0
}
1178
1179
OGR_SRSNode *OGRSpatialReference::GetRoot()
1180
0
{
1181
0
    TAKE_OPTIONAL_LOCK();
1182
1183
0
    if (!d->m_poRoot)
1184
0
    {
1185
0
        d->refreshRootFromProjObj();
1186
0
    }
1187
0
    return d->m_poRoot;
1188
0
}
1189
1190
const OGR_SRSNode *OGRSpatialReference::GetRoot() const
1191
0
{
1192
0
    TAKE_OPTIONAL_LOCK();
1193
1194
0
    if (!d->m_poRoot)
1195
0
    {
1196
0
        d->refreshRootFromProjObj();
1197
0
    }
1198
0
    return d->m_poRoot;
1199
0
}
1200
1201
/************************************************************************/
1202
/*                              SetRoot()                               */
1203
/************************************************************************/
1204
1205
/**
1206
 * \brief Set the root SRS node.
1207
 *
1208
 * If the object has an existing tree of OGR_SRSNodes, they are destroyed
1209
 * as part of assigning the new root.  Ownership of the passed OGR_SRSNode is
1210
 * is assumed by the OGRSpatialReference.
1211
 *
1212
 * @param poNewRoot object to assign as root.
1213
 */
1214
1215
void OGRSpatialReference::SetRoot(OGR_SRSNode *poNewRoot)
1216
1217
0
{
1218
0
    if (d->m_poRoot != poNewRoot)
1219
0
    {
1220
0
        delete d->m_poRoot;
1221
0
        d->setRoot(poNewRoot);
1222
0
    }
1223
0
}
1224
1225
/************************************************************************/
1226
/*                            GetAttrNode()                             */
1227
/************************************************************************/
1228
1229
/**
1230
 * \brief Find named node in tree.
1231
 *
1232
 * This method does a pre-order traversal of the node tree searching for
1233
 * a node with this exact value (case insensitive), and returns it.  Leaf
1234
 * nodes are not considered, under the assumption that they are just
1235
 * attribute value nodes.
1236
 *
1237
 * If a node appears more than once in the tree (such as UNIT for instance),
1238
 * the first encountered will be returned.  Use GetNode() on a subtree to be
1239
 * more specific.
1240
 *
1241
 * @param pszNodePath the name of the node to search for.  May contain multiple
1242
 * components such as "GEOGCS|UNIT".
1243
 *
1244
 * @return a pointer to the node found, or NULL if none.
1245
 */
1246
1247
OGR_SRSNode *OGRSpatialReference::GetAttrNode(const char *pszNodePath)
1248
1249
0
{
1250
0
    if (strchr(pszNodePath, '|') == nullptr)
1251
0
    {
1252
        // Fast path
1253
0
        OGR_SRSNode *poNode = GetRoot();
1254
0
        if (poNode)
1255
0
            poNode = poNode->GetNode(pszNodePath);
1256
0
        return poNode;
1257
0
    }
1258
1259
0
    char **papszPathTokens =
1260
0
        CSLTokenizeStringComplex(pszNodePath, "|", TRUE, FALSE);
1261
1262
0
    if (CSLCount(papszPathTokens) < 1)
1263
0
    {
1264
0
        CSLDestroy(papszPathTokens);
1265
0
        return nullptr;
1266
0
    }
1267
1268
0
    OGR_SRSNode *poNode = GetRoot();
1269
0
    for (int i = 0; poNode != nullptr && papszPathTokens[i] != nullptr; i++)
1270
0
    {
1271
0
        poNode = poNode->GetNode(papszPathTokens[i]);
1272
0
    }
1273
1274
0
    CSLDestroy(papszPathTokens);
1275
1276
0
    return poNode;
1277
0
}
1278
1279
/**
1280
 * \brief Find named node in tree.
1281
 *
1282
 * This method does a pre-order traversal of the node tree searching for
1283
 * a node with this exact value (case insensitive), and returns it.  Leaf
1284
 * nodes are not considered, under the assumption that they are just
1285
 * attribute value nodes.
1286
 *
1287
 * If a node appears more than once in the tree (such as UNIT for instance),
1288
 * the first encountered will be returned.  Use GetNode() on a subtree to be
1289
 * more specific.
1290
 *
1291
 * @param pszNodePath the name of the node to search for.  May contain multiple
1292
 * components such as "GEOGCS|UNIT".
1293
 *
1294
 * @return a pointer to the node found, or NULL if none.
1295
 */
1296
1297
const OGR_SRSNode *
1298
OGRSpatialReference::GetAttrNode(const char *pszNodePath) const
1299
1300
0
{
1301
0
    OGR_SRSNode *poNode =
1302
0
        const_cast<OGRSpatialReference *>(this)->GetAttrNode(pszNodePath);
1303
1304
0
    return poNode;
1305
0
}
1306
1307
/************************************************************************/
1308
/*                            GetAttrValue()                            */
1309
/************************************************************************/
1310
1311
/**
1312
 * \brief Fetch indicated attribute of named node.
1313
 *
1314
 * This method uses GetAttrNode() to find the named node, and then extracts
1315
 * the value of the indicated child.  Thus a call to GetAttrValue("UNIT",1)
1316
 * would return the second child of the UNIT node, which is normally the
1317
 * length of the linear unit in meters.
1318
 *
1319
 * This method does the same thing as the C function OSRGetAttrValue().
1320
 *
1321
 * @param pszNodeName the tree node to look for (case insensitive).
1322
 * @param iAttr the child of the node to fetch (zero based).
1323
 *
1324
 * @return the requested value, or NULL if it fails for any reason.
1325
 */
1326
1327
const char *OGRSpatialReference::GetAttrValue(const char *pszNodeName,
1328
                                              int iAttr) const
1329
1330
0
{
1331
0
    const OGR_SRSNode *poNode = GetAttrNode(pszNodeName);
1332
0
    if (poNode == nullptr)
1333
0
    {
1334
0
        if (d->m_bNodesWKT2 && EQUAL(pszNodeName, "PROJECTION"))
1335
0
        {
1336
0
            return GetAttrValue("METHOD", iAttr);
1337
0
        }
1338
0
        else if (d->m_bNodesWKT2 && EQUAL(pszNodeName, "PROJCS|PROJECTION"))
1339
0
        {
1340
0
            return GetAttrValue("PROJCRS|METHOD", iAttr);
1341
0
        }
1342
0
        else if (d->m_bNodesWKT2 && EQUAL(pszNodeName, "PROJCS"))
1343
0
        {
1344
0
            return GetAttrValue("PROJCRS", iAttr);
1345
0
        }
1346
0
        return nullptr;
1347
0
    }
1348
1349
0
    if (iAttr < 0 || iAttr >= poNode->GetChildCount())
1350
0
        return nullptr;
1351
1352
0
    return poNode->GetChild(iAttr)->GetValue();
1353
0
}
1354
1355
/************************************************************************/
1356
/*                          OSRGetAttrValue()                           */
1357
/************************************************************************/
1358
1359
/**
1360
 * \brief Fetch indicated attribute of named node.
1361
 *
1362
 * This function is the same as OGRSpatialReference::GetAttrValue()
1363
 */
1364
const char *CPL_STDCALL OSRGetAttrValue(OGRSpatialReferenceH hSRS,
1365
                                        const char *pszKey, int iChild)
1366
1367
0
{
1368
0
    VALIDATE_POINTER1(hSRS, "OSRGetAttrValue", nullptr);
1369
1370
0
    return ToPointer(hSRS)->GetAttrValue(pszKey, iChild);
1371
0
}
1372
1373
/************************************************************************/
1374
/*                             GetName()                                */
1375
/************************************************************************/
1376
1377
/**
1378
 * \brief Return the CRS name.
1379
 *
1380
 * The returned value is only short lived and should not be used after other
1381
 * calls to methods on this object.
1382
 *
1383
 * @since GDAL 3.0
1384
 */
1385
1386
const char *OGRSpatialReference::GetName() const
1387
0
{
1388
0
    TAKE_OPTIONAL_LOCK();
1389
1390
0
    d->refreshProjObj();
1391
0
    if (!d->m_pj_crs)
1392
0
        return nullptr;
1393
0
    const char *pszName = proj_get_name(d->m_pj_crs);
1394
#if PROJ_VERSION_NUMBER == PROJ_COMPUTE_VERSION(8, 2, 0)
1395
    if (d->m_pjType == PJ_TYPE_BOUND_CRS && EQUAL(pszName, "SOURCECRS"))
1396
    {
1397
        // Work around a bug of PROJ 8.2.0 (fixed in 8.2.1)
1398
        PJ *baseCRS = proj_get_source_crs(d->getPROJContext(), d->m_pj_crs);
1399
        if (baseCRS)
1400
        {
1401
            pszName = proj_get_name(baseCRS);
1402
            // pszName still remains valid after proj_destroy(), since
1403
            // d->m_pj_crs keeps a reference to the base CRS C++ object.
1404
            proj_destroy(baseCRS);
1405
        }
1406
    }
1407
#endif
1408
0
    return pszName;
1409
0
}
1410
1411
/************************************************************************/
1412
/*                           OSRGetName()                               */
1413
/************************************************************************/
1414
1415
/**
1416
 * \brief Return the CRS name.
1417
 *
1418
 * The returned value is only short lived and should not be used after other
1419
 * calls to methods on this object.
1420
 *
1421
 * @since GDAL 3.0
1422
 */
1423
const char *OSRGetName(OGRSpatialReferenceH hSRS)
1424
1425
0
{
1426
0
    VALIDATE_POINTER1(hSRS, "OSRGetName", nullptr);
1427
1428
0
    return ToPointer(hSRS)->GetName();
1429
0
}
1430
1431
/************************************************************************/
1432
/*                       GetCelestialBodyName()                         */
1433
/************************************************************************/
1434
1435
/**
1436
 * \brief Return the name of the celestial body of this CRS.
1437
 *
1438
 * e.g. "Earth" for an Earth CRS
1439
 *
1440
 * The returned value is only short lived and should not be used after other
1441
 * calls to methods on this object.
1442
 *
1443
 * @since GDAL 3.12 and PROJ 8.1
1444
 */
1445
1446
const char *OGRSpatialReference::GetCelestialBodyName() const
1447
0
{
1448
0
#if PROJ_AT_LEAST_VERSION(8, 1, 0)
1449
1450
0
    TAKE_OPTIONAL_LOCK();
1451
1452
0
    d->refreshProjObj();
1453
0
    if (!d->m_pj_crs)
1454
0
        return nullptr;
1455
0
    d->demoteFromBoundCRS();
1456
0
    const char *name =
1457
0
        proj_get_celestial_body_name(d->getPROJContext(), d->m_pj_crs);
1458
0
    if (name)
1459
0
    {
1460
0
        d->m_celestialBodyName = name;
1461
0
    }
1462
0
    d->undoDemoteFromBoundCRS();
1463
0
    return d->m_celestialBodyName.c_str();
1464
#else
1465
    if (std::fabs(GetSemiMajor(nullptr) - SRS_WGS84_SEMIMAJOR) <=
1466
        0.05 * SRS_WGS84_SEMIMAJOR)
1467
        return "Earth";
1468
    const char *pszAuthName = GetAuthorityName(nullptr);
1469
    if (pszAuthName && EQUAL(pszAuthName, "EPSG"))
1470
        return "Earth";
1471
    return nullptr;
1472
#endif
1473
0
}
1474
1475
/************************************************************************/
1476
/*                       OSRGetCelestialBodyName()                      */
1477
/************************************************************************/
1478
1479
/**
1480
 * \brief Return the name of the celestial body of this CRS.
1481
 *
1482
 * e.g. "Earth" for an Earth CRS
1483
 *
1484
 * The returned value is only short lived and should not be used after other
1485
 * calls to methods on this object.
1486
 *
1487
 * @since GDAL 3.12 and PROJ 8.1
1488
 */
1489
1490
const char *OSRGetCelestialBodyName(OGRSpatialReferenceH hSRS)
1491
1492
0
{
1493
0
    VALIDATE_POINTER1(hSRS, "GetCelestialBodyName", nullptr);
1494
1495
0
    return ToPointer(hSRS)->GetCelestialBodyName();
1496
0
}
1497
1498
/************************************************************************/
1499
/*                               Clone()                                */
1500
/************************************************************************/
1501
1502
/**
1503
 * \brief Make a duplicate of this OGRSpatialReference.
1504
 *
1505
 * This method is the same as the C function OSRClone().
1506
 *
1507
 * @return a new SRS, which becomes the responsibility of the caller.
1508
 */
1509
1510
OGRSpatialReference *OGRSpatialReference::Clone() const
1511
1512
0
{
1513
0
    OGRSpatialReference *poNewRef = new OGRSpatialReference();
1514
1515
0
    TAKE_OPTIONAL_LOCK();
1516
1517
0
    d->refreshProjObj();
1518
0
    if (d->m_pj_crs != nullptr)
1519
0
        poNewRef->d->setPjCRS(proj_clone(d->getPROJContext(), d->m_pj_crs));
1520
0
    if (d->m_bHasCenterLong && d->m_poRoot)
1521
0
    {
1522
0
        poNewRef->d->setRoot(d->m_poRoot->Clone());
1523
0
    }
1524
0
    poNewRef->d->m_axisMapping = d->m_axisMapping;
1525
0
    poNewRef->d->m_axisMappingStrategy = d->m_axisMappingStrategy;
1526
0
    poNewRef->d->m_coordinateEpoch = d->m_coordinateEpoch;
1527
0
    return poNewRef;
1528
0
}
1529
1530
/************************************************************************/
1531
/*                              OSRClone()                              */
1532
/************************************************************************/
1533
1534
/**
1535
 * \brief Make a duplicate of this OGRSpatialReference.
1536
 *
1537
 * This function is the same as OGRSpatialReference::Clone()
1538
 */
1539
OGRSpatialReferenceH CPL_STDCALL OSRClone(OGRSpatialReferenceH hSRS)
1540
1541
0
{
1542
0
    VALIDATE_POINTER1(hSRS, "OSRClone", nullptr);
1543
1544
0
    return ToHandle(ToPointer(hSRS)->Clone());
1545
0
}
1546
1547
/************************************************************************/
1548
/*                            dumpReadable()                            */
1549
/************************************************************************/
1550
1551
/** Dump pretty wkt to stdout, mostly for debugging.
1552
 */
1553
void OGRSpatialReference::dumpReadable()
1554
1555
0
{
1556
0
    char *pszPrettyWkt = nullptr;
1557
1558
0
    const char *const apszOptions[] = {"FORMAT=WKT2", "MULTILINE=YES", nullptr};
1559
0
    exportToWkt(&pszPrettyWkt, apszOptions);
1560
0
    printf("%s\n", pszPrettyWkt); /*ok*/
1561
0
    CPLFree(pszPrettyWkt);
1562
0
}
1563
1564
/************************************************************************/
1565
/*                         exportToPrettyWkt()                          */
1566
/************************************************************************/
1567
1568
/**
1569
 * Convert this SRS into a nicely formatted WKT 1 string for display to a
1570
 * person.
1571
 *
1572
 * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
1573
 * Issues</a> page for implementation details of WKT 1 in OGR.
1574
 *
1575
 * Note that the returned WKT string should be freed with
1576
 * CPLFree() when no longer needed.  It is the responsibility of the caller.
1577
 *
1578
 * The WKT version can be overridden by using the OSR_WKT_FORMAT configuration
1579
 * option. Valid values are the one of the FORMAT option of
1580
 * exportToWkt( char ** ppszResult, const char* const* papszOptions ) const
1581
 *
1582
 * This method is the same as the C function OSRExportToPrettyWkt().
1583
 *
1584
 * @param ppszResult the resulting string is returned in this pointer.
1585
 * @param bSimplify TRUE if the AXIS, AUTHORITY and EXTENSION nodes should be
1586
 *   stripped off.
1587
 *
1588
 * @return OGRERR_NONE if successful.
1589
 */
1590
1591
OGRErr OGRSpatialReference::exportToPrettyWkt(char **ppszResult,
1592
                                              int bSimplify) const
1593
1594
0
{
1595
0
    CPLStringList aosOptions;
1596
0
    aosOptions.SetNameValue("MULTILINE", "YES");
1597
0
    if (bSimplify)
1598
0
    {
1599
0
        aosOptions.SetNameValue("FORMAT", "WKT1_SIMPLE");
1600
0
    }
1601
0
    return exportToWkt(ppszResult, aosOptions.List());
1602
0
}
1603
1604
/************************************************************************/
1605
/*                        OSRExportToPrettyWkt()                        */
1606
/************************************************************************/
1607
1608
/**
1609
 * \brief Convert this SRS into a nicely formatted WKT 1 string for display to a
1610
 * person.
1611
 *
1612
 * The WKT version can be overridden by using the OSR_WKT_FORMAT configuration
1613
 * option. Valid values are the one of the FORMAT option of
1614
 * exportToWkt( char ** ppszResult, const char* const* papszOptions ) const
1615
 *
1616
 * This function is the same as OGRSpatialReference::exportToPrettyWkt().
1617
 */
1618
1619
OGRErr CPL_STDCALL OSRExportToPrettyWkt(OGRSpatialReferenceH hSRS,
1620
                                        char **ppszReturn, int bSimplify)
1621
1622
0
{
1623
0
    VALIDATE_POINTER1(hSRS, "OSRExportToPrettyWkt", OGRERR_FAILURE);
1624
1625
0
    *ppszReturn = nullptr;
1626
1627
0
    return ToPointer(hSRS)->exportToPrettyWkt(ppszReturn, bSimplify);
1628
0
}
1629
1630
/************************************************************************/
1631
/*                            exportToWkt()                             */
1632
/************************************************************************/
1633
1634
/**
1635
 * \brief Convert this SRS into WKT 1 format.
1636
 *
1637
 * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
1638
 * Issues</a> page for implementation details of WKT 1 in OGR.
1639
 *
1640
 * Note that the returned WKT string should be freed with
1641
 * CPLFree() when no longer needed.  It is the responsibility of the caller.
1642
 *
1643
 * The WKT version can be overridden by using the OSR_WKT_FORMAT configuration
1644
 * option. Valid values are the one of the FORMAT option of
1645
 * exportToWkt( char ** ppszResult, const char* const* papszOptions ) const
1646
 *
1647
 * This method is the same as the C function OSRExportToWkt().
1648
 *
1649
 * @param ppszResult the resulting string is returned in this pointer.
1650
 *
1651
 * @return OGRERR_NONE if successful.
1652
 */
1653
1654
OGRErr OGRSpatialReference::exportToWkt(char **ppszResult) const
1655
1656
0
{
1657
0
    return exportToWkt(ppszResult, nullptr);
1658
0
}
1659
1660
/************************************************************************/
1661
/*                GDAL_proj_crs_create_bound_crs_to_WGS84()             */
1662
/************************************************************************/
1663
1664
static PJ *GDAL_proj_crs_create_bound_crs_to_WGS84(PJ_CONTEXT *ctx, PJ *pj,
1665
                                                   bool onlyIfEPSGCode,
1666
                                                   bool canModifyHorizPart)
1667
0
{
1668
0
    PJ *ret = nullptr;
1669
0
    if (proj_get_type(pj) == PJ_TYPE_COMPOUND_CRS)
1670
0
    {
1671
0
        auto horizCRS = proj_crs_get_sub_crs(ctx, pj, 0);
1672
0
        auto vertCRS = proj_crs_get_sub_crs(ctx, pj, 1);
1673
0
        if (horizCRS && proj_get_type(horizCRS) != PJ_TYPE_BOUND_CRS &&
1674
0
            vertCRS &&
1675
0
            (!onlyIfEPSGCode || proj_get_id_auth_name(horizCRS, 0) != nullptr))
1676
0
        {
1677
0
            auto boundHoriz =
1678
0
                canModifyHorizPart
1679
0
                    ? proj_crs_create_bound_crs_to_WGS84(ctx, horizCRS, nullptr)
1680
0
                    : proj_clone(ctx, horizCRS);
1681
0
            auto boundVert =
1682
0
                proj_crs_create_bound_crs_to_WGS84(ctx, vertCRS, nullptr);
1683
0
            if (boundHoriz && boundVert)
1684
0
            {
1685
0
                ret = proj_create_compound_crs(ctx, proj_get_name(pj),
1686
0
                                               boundHoriz, boundVert);
1687
0
            }
1688
0
            proj_destroy(boundHoriz);
1689
0
            proj_destroy(boundVert);
1690
0
        }
1691
0
        proj_destroy(horizCRS);
1692
0
        proj_destroy(vertCRS);
1693
0
    }
1694
0
    else if (proj_get_type(pj) != PJ_TYPE_BOUND_CRS &&
1695
0
             (!onlyIfEPSGCode || proj_get_id_auth_name(pj, 0) != nullptr))
1696
0
    {
1697
0
        ret = proj_crs_create_bound_crs_to_WGS84(ctx, pj, nullptr);
1698
0
    }
1699
0
    return ret;
1700
0
}
1701
1702
/************************************************************************/
1703
/*                            exportToWkt()                             */
1704
/************************************************************************/
1705
1706
/**
1707
 * Convert this SRS into a WKT string.
1708
 *
1709
 * Note that the returned WKT string should be freed with
1710
 * CPLFree() when no longer needed.  It is the responsibility of the caller.
1711
 *
1712
 * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
1713
 * Issues</a> page for implementation details of WKT 1 in OGR.
1714
 *
1715
 * @param ppszResult the resulting string is returned in this pointer.
1716
 * @param papszOptions NULL terminated list of options, or NULL. Currently
1717
 * supported options are
1718
 * <ul>
1719
 * <li>MULTILINE=YES/NO. Defaults to NO.</li>
1720
 * <li>FORMAT=SFSQL/WKT1_SIMPLE/WKT1/WKT1_GDAL/WKT1_ESRI/WKT2_2015/WKT2_2018/WKT2/DEFAULT.
1721
 *     If SFSQL, a WKT1 string without AXIS, TOWGS84, AUTHORITY or EXTENSION
1722
 *     node is returned.
1723
 *     If WKT1_SIMPLE, a WKT1 string without AXIS, AUTHORITY or EXTENSION
1724
 *     node is returned.
1725
 *     WKT1 is an alias of WKT1_GDAL.
1726
 *     WKT2 will default to the latest revision implemented (currently
1727
 *     WKT2_2018) WKT2_2019 can be used as an alias of WKT2_2018 since GDAL 3.2
1728
 * </li>
1729
 * <li>ALLOW_ELLIPSOIDAL_HEIGHT_AS_VERTICAL_CRS=YES/NO. Default is NO. If set
1730
 * to YES and FORMAT=WKT1_GDAL, a Geographic 3D CRS or a Projected 3D CRS will
1731
 * be exported as a compound CRS whose vertical part represents an ellipsoidal
1732
 * height (for example for use with LAS 1.4 WKT1).
1733
 * Requires PROJ 7.2.1 and GDAL 3.2.1.</li>
1734
 * </ul>
1735
 *
1736
 * Starting with GDAL 3.0.3, if the OSR_ADD_TOWGS84_ON_EXPORT_TO_WKT1
1737
 * configuration option is set to YES, when exporting to WKT1_GDAL, this method
1738
 * will try to add a TOWGS84[] node, if there's none attached yet to the SRS and
1739
 * if the SRS has a EPSG code. See the AddGuessedTOWGS84() method for how this
1740
 * TOWGS84[] node may be added.
1741
 *
1742
 * @return OGRERR_NONE if successful.
1743
 * @since GDAL 3.0
1744
 */
1745
1746
OGRErr OGRSpatialReference::exportToWkt(char **ppszResult,
1747
                                        const char *const *papszOptions) const
1748
0
{
1749
    // In the past calling this method was thread-safe, even if we never
1750
    // guaranteed it. Now proj_as_wkt() will cache the result internally,
1751
    // so this is no longer thread-safe.
1752
0
    std::lock_guard oLock(d->m_mutex);
1753
1754
0
    d->refreshProjObj();
1755
0
    if (!d->m_pj_crs)
1756
0
    {
1757
0
        *ppszResult = CPLStrdup("");
1758
0
        return OGRERR_FAILURE;
1759
0
    }
1760
1761
0
    if (d->m_bHasCenterLong && d->m_poRoot && !d->m_bMorphToESRI)
1762
0
    {
1763
0
        return d->m_poRoot->exportToWkt(ppszResult);
1764
0
    }
1765
1766
0
    auto ctxt = d->getPROJContext();
1767
0
    auto wktFormat = PJ_WKT1_GDAL;
1768
0
    const char *pszFormat =
1769
0
        CSLFetchNameValueDef(papszOptions, "FORMAT",
1770
0
                             CPLGetConfigOption("OSR_WKT_FORMAT", "DEFAULT"));
1771
0
    if (EQUAL(pszFormat, "DEFAULT"))
1772
0
        pszFormat = "";
1773
1774
0
    if (EQUAL(pszFormat, "WKT1_ESRI") || d->m_bMorphToESRI)
1775
0
    {
1776
0
        wktFormat = PJ_WKT1_ESRI;
1777
0
    }
1778
0
    else if (EQUAL(pszFormat, "WKT1") || EQUAL(pszFormat, "WKT1_GDAL") ||
1779
0
             EQUAL(pszFormat, "WKT1_SIMPLE") || EQUAL(pszFormat, "SFSQL"))
1780
0
    {
1781
0
        wktFormat = PJ_WKT1_GDAL;
1782
0
    }
1783
0
    else if (EQUAL(pszFormat, "WKT2_2015"))
1784
0
    {
1785
0
        wktFormat = PJ_WKT2_2015;
1786
0
    }
1787
0
    else if (EQUAL(pszFormat, "WKT2") || EQUAL(pszFormat, "WKT2_2018") ||
1788
0
             EQUAL(pszFormat, "WKT2_2019"))
1789
0
    {
1790
0
        wktFormat = PJ_WKT2_2018;
1791
0
    }
1792
0
    else if (pszFormat[0] == '\0')
1793
0
    {
1794
        // cppcheck-suppress knownConditionTrueFalse
1795
0
        if (IsDerivedGeographic())
1796
0
        {
1797
0
            wktFormat = PJ_WKT2_2018;
1798
0
        }
1799
0
        else if ((IsGeographic() || IsProjected()) && !IsCompound() &&
1800
0
                 GetAxesCount() == 3)
1801
0
        {
1802
0
            wktFormat = PJ_WKT2_2018;
1803
0
        }
1804
0
    }
1805
0
    else
1806
0
    {
1807
0
        CPLError(CE_Failure, CPLE_AppDefined, "Unsupported value for FORMAT");
1808
0
        *ppszResult = CPLStrdup("");
1809
0
        return OGRERR_FAILURE;
1810
0
    }
1811
1812
0
    CPLStringList aosOptions;
1813
0
    if (wktFormat != PJ_WKT1_ESRI)
1814
0
    {
1815
0
        aosOptions.SetNameValue("OUTPUT_AXIS", "YES");
1816
0
    }
1817
0
    aosOptions.SetNameValue(
1818
0
        "MULTILINE", CSLFetchNameValueDef(papszOptions, "MULTILINE", "NO"));
1819
1820
0
    const char *pszAllowEllpsHeightAsVertCS = CSLFetchNameValue(
1821
0
        papszOptions, "ALLOW_ELLIPSOIDAL_HEIGHT_AS_VERTICAL_CRS");
1822
0
    if (pszAllowEllpsHeightAsVertCS)
1823
0
    {
1824
0
        aosOptions.SetNameValue("ALLOW_ELLIPSOIDAL_HEIGHT_AS_VERTICAL_CRS",
1825
0
                                pszAllowEllpsHeightAsVertCS);
1826
0
    }
1827
1828
0
    PJ *boundCRS = nullptr;
1829
0
    if (wktFormat == PJ_WKT1_GDAL &&
1830
0
        CPLTestBool(CSLFetchNameValueDef(
1831
0
            papszOptions, "ADD_TOWGS84_ON_EXPORT_TO_WKT1",
1832
0
            CPLGetConfigOption("OSR_ADD_TOWGS84_ON_EXPORT_TO_WKT1", "NO"))))
1833
0
    {
1834
0
        boundCRS = GDAL_proj_crs_create_bound_crs_to_WGS84(
1835
0
            d->getPROJContext(), d->m_pj_crs, true, true);
1836
0
    }
1837
1838
0
    CPLErrorAccumulator oErrorAccumulator;
1839
0
    const char *pszWKT;
1840
0
    {
1841
0
        auto oAccumulator = oErrorAccumulator.InstallForCurrentScope();
1842
0
        CPL_IGNORE_RET_VAL(oAccumulator);
1843
0
        pszWKT = proj_as_wkt(ctxt, boundCRS ? boundCRS : d->m_pj_crs, wktFormat,
1844
0
                             aosOptions.List());
1845
0
    }
1846
0
    for (const auto &oError : oErrorAccumulator.GetErrors())
1847
0
    {
1848
0
        if (pszFormat[0] == '\0' &&
1849
0
            (oError.msg.find("Unsupported conversion method") !=
1850
0
                 std::string::npos ||
1851
0
             oError.msg.find("can only be exported to WKT2") !=
1852
0
                 std::string::npos ||
1853
0
             oError.msg.find("can only be exported since WKT2:2019") !=
1854
0
                 std::string::npos))
1855
0
        {
1856
0
            CPLErrorReset();
1857
            // If we cannot export in the default mode (WKT1), retry with WKT2
1858
0
            pszWKT = proj_as_wkt(ctxt, boundCRS ? boundCRS : d->m_pj_crs,
1859
0
                                 PJ_WKT2_2018, aosOptions.List());
1860
0
            break;
1861
0
        }
1862
0
        CPLError(oError.type, oError.no, "%s", oError.msg.c_str());
1863
0
    }
1864
1865
0
    if (!pszWKT)
1866
0
    {
1867
0
        *ppszResult = CPLStrdup("");
1868
0
        proj_destroy(boundCRS);
1869
0
        return OGRERR_FAILURE;
1870
0
    }
1871
1872
0
    if (EQUAL(pszFormat, "SFSQL") || EQUAL(pszFormat, "WKT1_SIMPLE"))
1873
0
    {
1874
0
        OGR_SRSNode oRoot;
1875
0
        oRoot.importFromWkt(&pszWKT);
1876
0
        oRoot.StripNodes("AXIS");
1877
0
        if (EQUAL(pszFormat, "SFSQL"))
1878
0
        {
1879
0
            oRoot.StripNodes("TOWGS84");
1880
0
        }
1881
0
        oRoot.StripNodes("AUTHORITY");
1882
0
        oRoot.StripNodes("EXTENSION");
1883
0
        OGRErr eErr;
1884
0
        if (CPLTestBool(CSLFetchNameValueDef(papszOptions, "MULTILINE", "NO")))
1885
0
            eErr = oRoot.exportToPrettyWkt(ppszResult, 1);
1886
0
        else
1887
0
            eErr = oRoot.exportToWkt(ppszResult);
1888
0
        proj_destroy(boundCRS);
1889
0
        return eErr;
1890
0
    }
1891
1892
0
    *ppszResult = CPLStrdup(pszWKT);
1893
1894
#if !(PROJ_AT_LEAST_VERSION(9, 5, 0))
1895
    if (wktFormat == PJ_WKT2_2018)
1896
    {
1897
        // Works around bug fixed per https://github.com/OSGeo/PROJ/pull/4166
1898
        // related to a wrong EPSG code assigned to UTM South conversions
1899
        char *pszPtr = strstr(*ppszResult, "CONVERSION[\"UTM zone ");
1900
        if (pszPtr)
1901
        {
1902
            pszPtr += strlen("CONVERSION[\"UTM zone ");
1903
            const int nZone = atoi(pszPtr);
1904
            while (*pszPtr >= '0' && *pszPtr <= '9')
1905
                ++pszPtr;
1906
            if (nZone >= 1 && nZone <= 60 && *pszPtr == 'S' &&
1907
                pszPtr[1] == '"' && pszPtr[2] == ',')
1908
            {
1909
                pszPtr += 3;
1910
                int nLevel = 0;
1911
                bool bInString = false;
1912
                // Find the ID node corresponding to this CONVERSION node
1913
                while (*pszPtr)
1914
                {
1915
                    if (bInString)
1916
                    {
1917
                        if (*pszPtr == '"' && pszPtr[1] == '"')
1918
                        {
1919
                            ++pszPtr;
1920
                        }
1921
                        else if (*pszPtr == '"')
1922
                        {
1923
                            bInString = false;
1924
                        }
1925
                    }
1926
                    else if (nLevel == 0 && STARTS_WITH_CI(pszPtr, "ID["))
1927
                    {
1928
                        if (STARTS_WITH_CI(pszPtr, CPLSPrintf("ID[\"EPSG\",%d]",
1929
                                                              17000 + nZone)))
1930
                        {
1931
                            CPLAssert(pszPtr[11] == '7');
1932
                            CPLAssert(pszPtr[12] == '0');
1933
                            pszPtr[11] = '6';
1934
                            pszPtr[12] = '1';
1935
                        }
1936
                        break;
1937
                    }
1938
                    else if (*pszPtr == '"')
1939
                    {
1940
                        bInString = true;
1941
                    }
1942
                    else if (*pszPtr == '[')
1943
                    {
1944
                        ++nLevel;
1945
                    }
1946
                    else if (*pszPtr == ']')
1947
                    {
1948
                        --nLevel;
1949
                    }
1950
1951
                    ++pszPtr;
1952
                }
1953
            }
1954
        }
1955
    }
1956
#endif
1957
1958
0
    proj_destroy(boundCRS);
1959
0
    return OGRERR_NONE;
1960
0
}
1961
1962
/************************************************************************/
1963
/*                            exportToWkt()                             */
1964
/************************************************************************/
1965
1966
/**
1967
 * Convert this SRS into a WKT string.
1968
 *
1969
 * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
1970
 * Issues</a> page for implementation details of WKT 1 in OGR.
1971
 *
1972
 * @param papszOptions NULL terminated list of options, or NULL. Currently
1973
 * supported options are
1974
 * <ul>
1975
 * <li>MULTILINE=YES/NO. Defaults to NO.</li>
1976
 * <li>FORMAT=SFSQL/WKT1_SIMPLE/WKT1/WKT1_GDAL/WKT1_ESRI/WKT2_2015/WKT2_2018/WKT2/DEFAULT.
1977
 *     If SFSQL, a WKT1 string without AXIS, TOWGS84, AUTHORITY or EXTENSION
1978
 *     node is returned.
1979
 *     If WKT1_SIMPLE, a WKT1 string without AXIS, AUTHORITY or EXTENSION
1980
 *     node is returned.
1981
 *     WKT1 is an alias of WKT1_GDAL.
1982
 *     WKT2 will default to the latest revision implemented (currently
1983
 *     WKT2_2019)
1984
 * </li>
1985
 * <li>ALLOW_ELLIPSOIDAL_HEIGHT_AS_VERTICAL_CRS=YES/NO. Default is NO. If set
1986
 * to YES and FORMAT=WKT1_GDAL, a Geographic 3D CRS or a Projected 3D CRS will
1987
 * be exported as a compound CRS whose vertical part represents an ellipsoidal
1988
 * height (for example for use with LAS 1.4 WKT1).
1989
 * Requires PROJ 7.2.1.</li>
1990
 * </ul>
1991
 *
1992
 * If the OSR_ADD_TOWGS84_ON_EXPORT_TO_WKT1
1993
 * configuration option is set to YES, when exporting to WKT1_GDAL, this method
1994
 * will try to add a TOWGS84[] node, if there's none attached yet to the SRS and
1995
 * if the SRS has a EPSG code. See the AddGuessedTOWGS84() method for how this
1996
 * TOWGS84[] node may be added.
1997
 *
1998
 * @return a non-empty string if successful.
1999
 * @since GDAL 3.9
2000
 */
2001
2002
std::string
2003
OGRSpatialReference::exportToWkt(const char *const *papszOptions) const
2004
0
{
2005
0
    std::string osWKT;
2006
0
    char *pszWKT = nullptr;
2007
0
    if (exportToWkt(&pszWKT, papszOptions) == OGRERR_NONE)
2008
0
        osWKT = pszWKT;
2009
0
    CPLFree(pszWKT);
2010
0
    return osWKT;
2011
0
}
2012
2013
/************************************************************************/
2014
/*                           OSRExportToWkt()                           */
2015
/************************************************************************/
2016
2017
/**
2018
 * \brief Convert this SRS into WKT 1 format.
2019
 *
2020
 * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2021
 * Issues</a> page for implementation details of WKT in OGR.
2022
 *
2023
 * The WKT version can be overridden by using the OSR_WKT_FORMAT configuration
2024
 * option. Valid values are the one of the FORMAT option of
2025
 * exportToWkt( char ** ppszResult, const char* const* papszOptions ) const
2026
 *
2027
 * This function is the same as OGRSpatialReference::exportToWkt().
2028
 */
2029
2030
OGRErr CPL_STDCALL OSRExportToWkt(OGRSpatialReferenceH hSRS, char **ppszReturn)
2031
2032
0
{
2033
0
    VALIDATE_POINTER1(hSRS, "OSRExportToWkt", OGRERR_FAILURE);
2034
2035
0
    *ppszReturn = nullptr;
2036
2037
0
    return ToPointer(hSRS)->exportToWkt(ppszReturn);
2038
0
}
2039
2040
/************************************************************************/
2041
/*                          OSRExportToWktEx()                          */
2042
/************************************************************************/
2043
2044
/**
2045
 * \brief Convert this SRS into WKT format.
2046
 *
2047
 * This function is the same as OGRSpatialReference::exportToWkt(char **
2048
 * ppszResult,const char* const* papszOptions ) const
2049
 *
2050
 * @since GDAL 3.0
2051
 */
2052
2053
OGRErr OSRExportToWktEx(OGRSpatialReferenceH hSRS, char **ppszReturn,
2054
                        const char *const *papszOptions)
2055
0
{
2056
0
    VALIDATE_POINTER1(hSRS, "OSRExportToWktEx", OGRERR_FAILURE);
2057
2058
0
    *ppszReturn = nullptr;
2059
2060
0
    return ToPointer(hSRS)->exportToWkt(ppszReturn, papszOptions);
2061
0
}
2062
2063
/************************************************************************/
2064
/*                       exportToPROJJSON()                             */
2065
/************************************************************************/
2066
2067
/**
2068
 * Convert this SRS into a PROJJSON string.
2069
 *
2070
 * Note that the returned JSON string should be freed with
2071
 * CPLFree() when no longer needed.  It is the responsibility of the caller.
2072
 *
2073
 * @param ppszResult the resulting string is returned in this pointer.
2074
 * @param papszOptions NULL terminated list of options, or NULL. Currently
2075
 * supported options are
2076
 * <ul>
2077
 * <li>MULTILINE=YES/NO. Defaults to YES</li>
2078
 * <li>INDENTATION_WIDTH=number. Defaults to 2 (when multiline output is
2079
 * on).</li>
2080
 * <li>SCHEMA=string. URL to PROJJSON schema. Can be set to empty string to
2081
 * disable it.</li>
2082
 * </ul>
2083
 *
2084
 * @return OGRERR_NONE if successful.
2085
 * @since GDAL 3.1 and PROJ 6.2
2086
 */
2087
2088
OGRErr OGRSpatialReference::exportToPROJJSON(
2089
    char **ppszResult, CPL_UNUSED const char *const *papszOptions) const
2090
0
{
2091
0
    TAKE_OPTIONAL_LOCK();
2092
2093
0
    d->refreshProjObj();
2094
0
    if (!d->m_pj_crs)
2095
0
    {
2096
0
        *ppszResult = nullptr;
2097
0
        return OGRERR_FAILURE;
2098
0
    }
2099
2100
0
    const char *pszPROJJSON =
2101
0
        proj_as_projjson(d->getPROJContext(), d->m_pj_crs, papszOptions);
2102
2103
0
    if (!pszPROJJSON)
2104
0
    {
2105
0
        *ppszResult = CPLStrdup("");
2106
0
        return OGRERR_FAILURE;
2107
0
    }
2108
2109
0
    *ppszResult = CPLStrdup(pszPROJJSON);
2110
2111
#if !(PROJ_AT_LEAST_VERSION(9, 5, 0))
2112
    {
2113
        // Works around bug fixed per https://github.com/OSGeo/PROJ/pull/4166
2114
        // related to a wrong EPSG code assigned to UTM South conversions
2115
        char *pszPtr = strstr(*ppszResult, "\"name\": \"UTM zone ");
2116
        if (pszPtr)
2117
        {
2118
            pszPtr += strlen("\"name\": \"UTM zone ");
2119
            const int nZone = atoi(pszPtr);
2120
            while (*pszPtr >= '0' && *pszPtr <= '9')
2121
                ++pszPtr;
2122
            if (nZone >= 1 && nZone <= 60 && *pszPtr == 'S' && pszPtr[1] == '"')
2123
            {
2124
                pszPtr += 2;
2125
                int nLevel = 0;
2126
                bool bInString = false;
2127
                // Find the id node corresponding to this conversion node
2128
                while (*pszPtr)
2129
                {
2130
                    if (bInString)
2131
                    {
2132
                        if (*pszPtr == '\\')
2133
                        {
2134
                            ++pszPtr;
2135
                        }
2136
                        else if (*pszPtr == '"')
2137
                        {
2138
                            bInString = false;
2139
                        }
2140
                    }
2141
                    else if (nLevel == 0 && STARTS_WITH(pszPtr, "\"id\": {"))
2142
                    {
2143
                        const char *pszNextEndCurl = strchr(pszPtr, '}');
2144
                        const char *pszAuthEPSG =
2145
                            strstr(pszPtr, "\"authority\": \"EPSG\"");
2146
                        char *pszCode = strstr(
2147
                            pszPtr, CPLSPrintf("\"code\": %d", 17000 + nZone));
2148
                        if (pszAuthEPSG && pszCode && pszNextEndCurl &&
2149
                            pszNextEndCurl - pszAuthEPSG > 0 &&
2150
                            pszNextEndCurl - pszCode > 0)
2151
                        {
2152
                            CPLAssert(pszCode[9] == '7');
2153
                            CPLAssert(pszCode[10] == '0');
2154
                            pszCode[9] = '6';
2155
                            pszCode[10] = '1';
2156
                        }
2157
                        break;
2158
                    }
2159
                    else if (*pszPtr == '"')
2160
                    {
2161
                        bInString = true;
2162
                    }
2163
                    else if (*pszPtr == '{' || *pszPtr == '[')
2164
                    {
2165
                        ++nLevel;
2166
                    }
2167
                    else if (*pszPtr == '}' || *pszPtr == ']')
2168
                    {
2169
                        --nLevel;
2170
                    }
2171
2172
                    ++pszPtr;
2173
                }
2174
            }
2175
        }
2176
    }
2177
#endif
2178
2179
0
    return OGRERR_NONE;
2180
0
}
2181
2182
/************************************************************************/
2183
/*                          OSRExportToPROJJSON()                       */
2184
/************************************************************************/
2185
2186
/**
2187
 * \brief Convert this SRS into PROJJSON format.
2188
 *
2189
 * This function is the same as OGRSpatialReference::exportToPROJJSON() const
2190
 *
2191
 * @since GDAL 3.1 and PROJ 6.2
2192
 */
2193
2194
OGRErr OSRExportToPROJJSON(OGRSpatialReferenceH hSRS, char **ppszReturn,
2195
                           const char *const *papszOptions)
2196
0
{
2197
0
    VALIDATE_POINTER1(hSRS, "OSRExportToPROJJSON", OGRERR_FAILURE);
2198
2199
0
    *ppszReturn = nullptr;
2200
2201
0
    return ToPointer(hSRS)->exportToPROJJSON(ppszReturn, papszOptions);
2202
0
}
2203
2204
/************************************************************************/
2205
/*                           importFromWkt()                            */
2206
/************************************************************************/
2207
2208
/**
2209
 * \brief Import from WKT string.
2210
 *
2211
 * This method will wipe the existing SRS definition, and
2212
 * reassign it based on the contents of the passed WKT string.  Only as
2213
 * much of the input string as needed to construct this SRS is consumed from
2214
 * the input string, and the input string pointer
2215
 * is then updated to point to the remaining (unused) input.
2216
 *
2217
 * Starting with PROJ 9.2, if invoked on a COORDINATEMETADATA[] construct,
2218
 * the CRS contained in it will be used to fill the OGRSpatialReference object,
2219
 * and the coordinate epoch potentially present used as the coordinate epoch
2220
 * property of the OGRSpatialReference object.
2221
 *
2222
 * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2223
 * Issues</a> page for implementation details of WKT in OGR.
2224
 *
2225
 * This method is the same as the C function OSRImportFromWkt().
2226
 *
2227
 * @param ppszInput Pointer to pointer to input.  The pointer is updated to
2228
 * point to remaining unused input text.
2229
 *
2230
 * @return OGRERR_NONE if import succeeds, or OGRERR_CORRUPT_DATA if it
2231
 * fails for any reason.
2232
 */
2233
2234
OGRErr OGRSpatialReference::importFromWkt(const char **ppszInput)
2235
2236
0
{
2237
0
    return importFromWkt(ppszInput, nullptr);
2238
0
}
2239
2240
/************************************************************************/
2241
/*                           importFromWkt()                            */
2242
/************************************************************************/
2243
2244
/*! @cond Doxygen_Suppress */
2245
2246
OGRErr OGRSpatialReference::importFromWkt(const char *pszInput,
2247
                                          CSLConstList papszOptions)
2248
2249
0
{
2250
0
    return importFromWkt(&pszInput, papszOptions);
2251
0
}
2252
2253
OGRErr OGRSpatialReference::importFromWkt(const char **ppszInput,
2254
                                          CSLConstList papszOptions)
2255
2256
0
{
2257
0
    TAKE_OPTIONAL_LOCK();
2258
2259
0
    if (!ppszInput || !*ppszInput)
2260
0
        return OGRERR_FAILURE;
2261
2262
0
    if (strlen(*ppszInput) > 100 * 1000 &&
2263
0
        CPLTestBool(CPLGetConfigOption("OSR_IMPORT_FROM_WKT_LIMIT", "YES")))
2264
0
    {
2265
0
        CPLError(CE_Failure, CPLE_NotSupported,
2266
0
                 "Suspiciously large input for importFromWkt(). Rejecting it. "
2267
0
                 "You can remove this limitation by definition the "
2268
0
                 "OSR_IMPORT_FROM_WKT_LIMIT configuration option to NO.");
2269
0
        return OGRERR_FAILURE;
2270
0
    }
2271
2272
0
    Clear();
2273
2274
0
    bool canCache = false;
2275
0
    auto tlsCache = OSRGetProjTLSCache();
2276
0
    std::string osWkt;
2277
0
    if (**ppszInput)
2278
0
    {
2279
0
        osWkt = *ppszInput;
2280
0
        auto cachedObj = tlsCache->GetPJForWKT(osWkt);
2281
0
        if (cachedObj)
2282
0
        {
2283
0
            d->setPjCRS(cachedObj);
2284
0
        }
2285
0
        else
2286
0
        {
2287
0
            CPLStringList aosOptions(papszOptions);
2288
0
            if (aosOptions.FetchNameValue("STRICT") == nullptr)
2289
0
                aosOptions.SetNameValue("STRICT", "NO");
2290
0
            PROJ_STRING_LIST warnings = nullptr;
2291
0
            PROJ_STRING_LIST errors = nullptr;
2292
0
            auto ctxt = d->getPROJContext();
2293
0
            auto pj = proj_create_from_wkt(ctxt, *ppszInput, aosOptions.List(),
2294
0
                                           &warnings, &errors);
2295
0
            d->setPjCRS(pj);
2296
2297
0
            for (auto iter = warnings; iter && *iter; ++iter)
2298
0
            {
2299
0
                d->m_wktImportWarnings.push_back(*iter);
2300
0
            }
2301
0
            for (auto iter = errors; iter && *iter; ++iter)
2302
0
            {
2303
0
                d->m_wktImportErrors.push_back(*iter);
2304
0
                if (!d->m_pj_crs)
2305
0
                {
2306
0
                    CPLError(CE_Failure, CPLE_AppDefined, "%s", *iter);
2307
0
                }
2308
0
            }
2309
0
            if (warnings == nullptr && errors == nullptr)
2310
0
            {
2311
0
                canCache = true;
2312
0
            }
2313
0
            proj_string_list_destroy(warnings);
2314
0
            proj_string_list_destroy(errors);
2315
0
        }
2316
0
    }
2317
0
    if (!d->m_pj_crs)
2318
0
        return OGRERR_CORRUPT_DATA;
2319
2320
    // Only accept CRS objects
2321
0
    if (!proj_is_crs(d->m_pj_crs))
2322
0
    {
2323
0
        Clear();
2324
0
        return OGRERR_CORRUPT_DATA;
2325
0
    }
2326
2327
0
    if (canCache)
2328
0
    {
2329
0
        tlsCache->CachePJForWKT(osWkt, d->m_pj_crs);
2330
0
    }
2331
2332
0
    if (strstr(*ppszInput, "CENTER_LONG"))
2333
0
    {
2334
0
        auto poRoot = new OGR_SRSNode();
2335
0
        d->setRoot(poRoot);
2336
0
        const char *pszTmp = *ppszInput;
2337
0
        poRoot->importFromWkt(&pszTmp);
2338
0
        d->m_bHasCenterLong = true;
2339
0
    }
2340
2341
    // TODO? we don't really update correctly since we assume that the
2342
    // passed string is only WKT.
2343
0
    *ppszInput += strlen(*ppszInput);
2344
0
    return OGRERR_NONE;
2345
2346
#if no_longer_implemented_for_now
2347
    /* -------------------------------------------------------------------- */
2348
    /*      The following seems to try and detect and unconsumed            */
2349
    /*      VERTCS[] coordinate system definition (ESRI style) and to       */
2350
    /*      import and attach it to the existing root.  Likely we will      */
2351
    /*      need to extend this somewhat to bring it into an acceptable     */
2352
    /*      OGRSpatialReference organization at some point.                 */
2353
    /* -------------------------------------------------------------------- */
2354
    if (strlen(*ppszInput) > 0 && strstr(*ppszInput, "VERTCS"))
2355
    {
2356
        if (((*ppszInput)[0]) == ',')
2357
            (*ppszInput)++;
2358
        OGR_SRSNode *poNewChild = new OGR_SRSNode();
2359
        poRoot->AddChild(poNewChild);
2360
        return poNewChild->importFromWkt(ppszInput);
2361
    }
2362
#endif
2363
0
}
2364
2365
/*! @endcond */
2366
2367
/**
2368
 * \brief Import from WKT string.
2369
 *
2370
 * This method will wipe the existing SRS definition, and
2371
 * reassign it based on the contents of the passed WKT string.  Only as
2372
 * much of the input string as needed to construct this SRS is consumed from
2373
 * the input string, and the input string pointer
2374
 * is then updated to point to the remaining (unused) input.
2375
 *
2376
 * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2377
 * Issues</a> page for implementation details of WKT in OGR.
2378
 *
2379
 * This method is the same as the C function OSRImportFromWkt().
2380
 *
2381
 * @param ppszInput Pointer to pointer to input.  The pointer is updated to
2382
 * point to remaining unused input text.
2383
 *
2384
 * @return OGRERR_NONE if import succeeds, or OGRERR_CORRUPT_DATA if it
2385
 * fails for any reason.
2386
 * @deprecated Use importFromWkt(const char**) or importFromWkt(const
2387
 * char*)
2388
 */
2389
2390
OGRErr OGRSpatialReference::importFromWkt(char **ppszInput)
2391
2392
0
{
2393
0
    return importFromWkt(const_cast<const char **>(ppszInput));
2394
0
}
2395
2396
/**
2397
 * \brief Import from WKT string.
2398
 *
2399
 * This method will wipe the existing SRS definition, and
2400
 * reassign it based on the contents of the passed WKT string.  Only as
2401
 * much of the input string as needed to construct this SRS is consumed from
2402
 * the input string, and the input string pointer
2403
 * is then updated to point to the remaining (unused) input.
2404
 *
2405
 * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2406
 * Issues</a> page for implementation details of WKT in OGR.
2407
 *
2408
 * @param pszInput Input WKT
2409
 *
2410
 * @return OGRERR_NONE if import succeeds, or OGRERR_CORRUPT_DATA if it
2411
 * fails for any reason.
2412
 */
2413
2414
OGRErr OGRSpatialReference::importFromWkt(const char *pszInput)
2415
0
{
2416
0
    return importFromWkt(&pszInput);
2417
0
}
2418
2419
/************************************************************************/
2420
/*                              Validate()                              */
2421
/************************************************************************/
2422
2423
/**
2424
 * \brief Validate CRS imported with importFromWkt() or with modified with
2425
 * direct node manipulations. Otherwise the CRS should be always valid.
2426
 *
2427
 * This method attempts to verify that the spatial reference system is
2428
 * well formed, and consists of known tokens.  The validation is not
2429
 * comprehensive.
2430
 *
2431
 * This method is the same as the C function OSRValidate().
2432
 *
2433
 * @return OGRERR_NONE if all is fine, OGRERR_CORRUPT_DATA if the SRS is
2434
 * not well formed, and OGRERR_UNSUPPORTED_SRS if the SRS is well formed,
2435
 * but contains non-standard PROJECTION[] values.
2436
 */
2437
2438
OGRErr OGRSpatialReference::Validate() const
2439
2440
0
{
2441
0
    TAKE_OPTIONAL_LOCK();
2442
2443
0
    for (const auto &str : d->m_wktImportErrors)
2444
0
    {
2445
0
        CPLDebug("OGRSpatialReference::Validate", "%s", str.c_str());
2446
0
    }
2447
0
    for (const auto &str : d->m_wktImportWarnings)
2448
0
    {
2449
0
        CPLDebug("OGRSpatialReference::Validate", "%s", str.c_str());
2450
0
    }
2451
0
    if (!d->m_pj_crs || !d->m_wktImportErrors.empty())
2452
0
    {
2453
0
        return OGRERR_CORRUPT_DATA;
2454
0
    }
2455
0
    if (!d->m_wktImportWarnings.empty())
2456
0
    {
2457
0
        return OGRERR_UNSUPPORTED_SRS;
2458
0
    }
2459
0
    return OGRERR_NONE;
2460
0
}
2461
2462
/************************************************************************/
2463
/*                            OSRValidate()                             */
2464
/************************************************************************/
2465
/**
2466
 * \brief Validate SRS tokens.
2467
 *
2468
 * This function is the same as the C++ method OGRSpatialReference::Validate().
2469
 */
2470
OGRErr OSRValidate(OGRSpatialReferenceH hSRS)
2471
2472
0
{
2473
0
    VALIDATE_POINTER1(hSRS, "OSRValidate", OGRERR_FAILURE);
2474
2475
0
    return OGRSpatialReference::FromHandle(hSRS)->Validate();
2476
0
}
2477
2478
/************************************************************************/
2479
/*                          OSRImportFromWkt()                          */
2480
/************************************************************************/
2481
2482
/**
2483
 * \brief Import from WKT string.
2484
 *
2485
 * Consult also the <a href="wktproblems.html">OGC WKT Coordinate System
2486
 * Issues</a> page for implementation details of WKT in OGR.
2487
 *
2488
 * This function is the same as OGRSpatialReference::importFromWkt().
2489
 */
2490
2491
OGRErr OSRImportFromWkt(OGRSpatialReferenceH hSRS, char **ppszInput)
2492
2493
0
{
2494
0
    VALIDATE_POINTER1(hSRS, "OSRImportFromWkt", OGRERR_FAILURE);
2495
2496
0
    return ToPointer(hSRS)->importFromWkt(const_cast<const char **>(ppszInput));
2497
0
}
2498
2499
/************************************************************************/
2500
/*                              SetNode()                               */
2501
/************************************************************************/
2502
2503
/**
2504
 * \brief Set attribute value in spatial reference.
2505
 *
2506
 * Missing intermediate nodes in the path will be created if not already
2507
 * in existence.  If the attribute has no children one will be created and
2508
 * assigned the value otherwise the zeroth child will be assigned the value.
2509
 *
2510
 * This method does the same as the C function OSRSetAttrValue().
2511
 *
2512
 * @param pszNodePath full path to attribute to be set.  For instance
2513
 * "PROJCS|GEOGCS|UNIT".
2514
 *
2515
 * @param pszNewNodeValue value to be assigned to node, such as "meter".
2516
 * This may be NULL if you just want to force creation of the intermediate
2517
 * path.
2518
 *
2519
 * @return OGRERR_NONE on success.
2520
 */
2521
2522
OGRErr OGRSpatialReference::SetNode(const char *pszNodePath,
2523
                                    const char *pszNewNodeValue)
2524
2525
0
{
2526
0
    TAKE_OPTIONAL_LOCK();
2527
2528
0
    char **papszPathTokens =
2529
0
        CSLTokenizeStringComplex(pszNodePath, "|", TRUE, FALSE);
2530
2531
0
    if (CSLCount(papszPathTokens) < 1)
2532
0
    {
2533
0
        CSLDestroy(papszPathTokens);
2534
0
        return OGRERR_FAILURE;
2535
0
    }
2536
2537
0
    if (GetRoot() == nullptr ||
2538
0
        !EQUAL(papszPathTokens[0], GetRoot()->GetValue()))
2539
0
    {
2540
0
        if (EQUAL(papszPathTokens[0], "PROJCS") &&
2541
0
            CSLCount(papszPathTokens) == 1)
2542
0
        {
2543
0
            CSLDestroy(papszPathTokens);
2544
0
            return SetProjCS(pszNewNodeValue);
2545
0
        }
2546
0
        else
2547
0
        {
2548
0
            SetRoot(new OGR_SRSNode(papszPathTokens[0]));
2549
0
        }
2550
0
    }
2551
2552
0
    OGR_SRSNode *poNode = GetRoot();
2553
0
    for (int i = 1; papszPathTokens[i] != nullptr; i++)
2554
0
    {
2555
0
        int j = 0;  // Used after for.
2556
2557
0
        for (; j < poNode->GetChildCount(); j++)
2558
0
        {
2559
0
            if (EQUAL(poNode->GetChild(j)->GetValue(), papszPathTokens[i]))
2560
0
            {
2561
0
                poNode = poNode->GetChild(j);
2562
0
                j = -1;
2563
0
                break;
2564
0
            }
2565
0
        }
2566
2567
0
        if (j != -1)
2568
0
        {
2569
0
            OGR_SRSNode *poNewNode = new OGR_SRSNode(papszPathTokens[i]);
2570
0
            poNode->AddChild(poNewNode);
2571
0
            poNode = poNewNode;
2572
0
        }
2573
0
    }
2574
2575
0
    CSLDestroy(papszPathTokens);
2576
2577
0
    if (pszNewNodeValue != nullptr)
2578
0
    {
2579
0
        if (poNode->GetChildCount() > 0)
2580
0
            poNode->GetChild(0)->SetValue(pszNewNodeValue);
2581
0
        else
2582
0
            poNode->AddChild(new OGR_SRSNode(pszNewNodeValue));
2583
0
    };
2584
0
    return OGRERR_NONE;
2585
0
}
2586
2587
/************************************************************************/
2588
/*                          OSRSetAttrValue()                           */
2589
/************************************************************************/
2590
2591
/**
2592
 * \brief Set attribute value in spatial reference.
2593
 *
2594
 * This function is the same as OGRSpatialReference::SetNode()
2595
 */
2596
OGRErr CPL_STDCALL OSRSetAttrValue(OGRSpatialReferenceH hSRS,
2597
                                   const char *pszPath, const char *pszValue)
2598
2599
0
{
2600
0
    VALIDATE_POINTER1(hSRS, "OSRSetAttrValue", OGRERR_FAILURE);
2601
2602
0
    return ToPointer(hSRS)->SetNode(pszPath, pszValue);
2603
0
}
2604
2605
/************************************************************************/
2606
/*                              SetNode()                               */
2607
/************************************************************************/
2608
2609
/**
2610
 * \brief Set attribute value in spatial reference.
2611
 *
2612
 * Missing intermediate nodes in the path will be created if not already
2613
 * in existence.  If the attribute has no children one will be created and
2614
 * assigned the value otherwise the zeroth child will be assigned the value.
2615
 *
2616
 * This method does the same as the C function OSRSetAttrValue().
2617
 *
2618
 * @param pszNodePath full path to attribute to be set.  For instance
2619
 * "PROJCS|GEOGCS|UNIT".
2620
 *
2621
 * @param dfValue value to be assigned to node.
2622
 *
2623
 * @return OGRERR_NONE on success.
2624
 */
2625
2626
OGRErr OGRSpatialReference::SetNode(const char *pszNodePath, double dfValue)
2627
2628
0
{
2629
0
    char szValue[64] = {'\0'};
2630
2631
0
    if (std::abs(dfValue - static_cast<int>(dfValue)) == 0.0)
2632
0
        snprintf(szValue, sizeof(szValue), "%d", static_cast<int>(dfValue));
2633
0
    else
2634
0
        OGRsnPrintDouble(szValue, sizeof(szValue), dfValue);
2635
2636
0
    return SetNode(pszNodePath, szValue);
2637
0
}
2638
2639
/************************************************************************/
2640
/*                          SetAngularUnits()                           */
2641
/************************************************************************/
2642
2643
/**
2644
 * \brief Set the angular units for the geographic coordinate system.
2645
 *
2646
 * This method creates a UNIT subnode with the specified values as a
2647
 * child of the GEOGCS node.
2648
 *
2649
 * This method does the same as the C function OSRSetAngularUnits().
2650
 *
2651
 * @param pszUnitsName the units name to be used.  Some preferred units
2652
 * names can be found in ogr_srs_api.h such as SRS_UA_DEGREE.
2653
 *
2654
 * @param dfInRadians the value to multiple by an angle in the indicated
2655
 * units to transform to radians.  Some standard conversion factors can
2656
 * be found in ogr_srs_api.h.
2657
 *
2658
 * @return OGRERR_NONE on success.
2659
 */
2660
2661
OGRErr OGRSpatialReference::SetAngularUnits(const char *pszUnitsName,
2662
                                            double dfInRadians)
2663
2664
0
{
2665
0
    TAKE_OPTIONAL_LOCK();
2666
2667
0
    d->bNormInfoSet = FALSE;
2668
2669
0
    d->refreshProjObj();
2670
0
    if (!d->m_pj_crs)
2671
0
        return OGRERR_FAILURE;
2672
0
    auto geodCRS = proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
2673
0
    if (!geodCRS)
2674
0
        return OGRERR_FAILURE;
2675
0
    proj_destroy(geodCRS);
2676
0
    d->demoteFromBoundCRS();
2677
0
    d->setPjCRS(proj_crs_alter_cs_angular_unit(d->getPROJContext(), d->m_pj_crs,
2678
0
                                               pszUnitsName, dfInRadians,
2679
0
                                               nullptr, nullptr));
2680
0
    d->undoDemoteFromBoundCRS();
2681
2682
0
    d->m_osAngularUnits = pszUnitsName;
2683
0
    d->m_dfAngularUnitToRadian = dfInRadians;
2684
2685
0
    return OGRERR_NONE;
2686
0
}
2687
2688
/************************************************************************/
2689
/*                         OSRSetAngularUnits()                         */
2690
/************************************************************************/
2691
2692
/**
2693
 * \brief Set the angular units for the geographic coordinate system.
2694
 *
2695
 * This function is the same as OGRSpatialReference::SetAngularUnits()
2696
 */
2697
OGRErr OSRSetAngularUnits(OGRSpatialReferenceH hSRS, const char *pszUnits,
2698
                          double dfInRadians)
2699
2700
0
{
2701
0
    VALIDATE_POINTER1(hSRS, "OSRSetAngularUnits", OGRERR_FAILURE);
2702
2703
0
    return ToPointer(hSRS)->SetAngularUnits(pszUnits, dfInRadians);
2704
0
}
2705
2706
/************************************************************************/
2707
/*                          GetAngularUnits()                           */
2708
/************************************************************************/
2709
2710
/**
2711
 * \brief Fetch angular geographic coordinate system units.
2712
 *
2713
 * If no units are available, a value of "degree" and SRS_UA_DEGREE_CONV
2714
 * will be assumed.  This method only checks directly under the GEOGCS node
2715
 * for units.
2716
 *
2717
 * This method does the same thing as the C function OSRGetAngularUnits().
2718
 *
2719
 * @param ppszName a pointer to be updated with the pointer to the units name.
2720
 * The returned value remains internal to the OGRSpatialReference and should
2721
 * not be freed, or modified.  It may be invalidated on the next
2722
 * OGRSpatialReference call.
2723
 *
2724
 * @return the value to multiply by angular distances to transform them to
2725
 * radians.
2726
 */
2727
2728
double OGRSpatialReference::GetAngularUnits(const char **ppszName) const
2729
2730
0
{
2731
0
    TAKE_OPTIONAL_LOCK();
2732
2733
0
    d->refreshProjObj();
2734
2735
0
    if (!d->m_osAngularUnits.empty())
2736
0
    {
2737
0
        if (ppszName != nullptr)
2738
0
            *ppszName = d->m_osAngularUnits.c_str();
2739
0
        return d->m_dfAngularUnitToRadian;
2740
0
    }
2741
2742
0
    do
2743
0
    {
2744
0
        if (d->m_pj_crs == nullptr || d->m_pjType == PJ_TYPE_ENGINEERING_CRS)
2745
0
        {
2746
0
            break;
2747
0
        }
2748
2749
0
        auto geodCRS =
2750
0
            proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
2751
0
        if (!geodCRS)
2752
0
        {
2753
0
            break;
2754
0
        }
2755
0
        auto coordSys =
2756
0
            proj_crs_get_coordinate_system(d->getPROJContext(), geodCRS);
2757
0
        proj_destroy(geodCRS);
2758
0
        if (!coordSys)
2759
0
        {
2760
0
            break;
2761
0
        }
2762
0
        if (proj_cs_get_type(d->getPROJContext(), coordSys) !=
2763
0
            PJ_CS_TYPE_ELLIPSOIDAL)
2764
0
        {
2765
0
            proj_destroy(coordSys);
2766
0
            break;
2767
0
        }
2768
2769
0
        double dfConvFactor = 0.0;
2770
0
        const char *pszUnitName = nullptr;
2771
0
        if (!proj_cs_get_axis_info(d->getPROJContext(), coordSys, 0, nullptr,
2772
0
                                   nullptr, nullptr, &dfConvFactor,
2773
0
                                   &pszUnitName, nullptr, nullptr))
2774
0
        {
2775
0
            proj_destroy(coordSys);
2776
0
            break;
2777
0
        }
2778
2779
0
        d->m_osAngularUnits = pszUnitName;
2780
2781
0
        proj_destroy(coordSys);
2782
0
        d->m_dfAngularUnitToRadian = dfConvFactor;
2783
0
    } while (false);
2784
2785
0
    if (d->m_osAngularUnits.empty())
2786
0
    {
2787
0
        d->m_osAngularUnits = "degree";
2788
0
        d->m_dfAngularUnitToRadian = CPLAtof(SRS_UA_DEGREE_CONV);
2789
0
    }
2790
2791
0
    if (ppszName != nullptr)
2792
0
        *ppszName = d->m_osAngularUnits.c_str();
2793
0
    return d->m_dfAngularUnitToRadian;
2794
0
}
2795
2796
/**
2797
 * \brief Fetch angular geographic coordinate system units.
2798
 *
2799
 * If no units are available, a value of "degree" and SRS_UA_DEGREE_CONV
2800
 * will be assumed.  This method only checks directly under the GEOGCS node
2801
 * for units.
2802
 *
2803
 * This method does the same thing as the C function OSRGetAngularUnits().
2804
 *
2805
 * @param ppszName a pointer to be updated with the pointer to the units name.
2806
 * The returned value remains internal to the OGRSpatialReference and should
2807
 * not be freed, or modified.  It may be invalidated on the next
2808
 * OGRSpatialReference call.
2809
 *
2810
 * @return the value to multiply by angular distances to transform them to
2811
 * radians.
2812
 * @deprecated Use GetAngularUnits(const char**) const.
2813
 */
2814
2815
double OGRSpatialReference::GetAngularUnits(char **ppszName) const
2816
2817
0
{
2818
0
    return GetAngularUnits(const_cast<const char **>(ppszName));
2819
0
}
2820
2821
/************************************************************************/
2822
/*                         OSRGetAngularUnits()                         */
2823
/************************************************************************/
2824
2825
/**
2826
 * \brief Fetch angular geographic coordinate system units.
2827
 *
2828
 * This function is the same as OGRSpatialReference::GetAngularUnits()
2829
 */
2830
double OSRGetAngularUnits(OGRSpatialReferenceH hSRS, char **ppszName)
2831
2832
0
{
2833
0
    VALIDATE_POINTER1(hSRS, "OSRGetAngularUnits", 0);
2834
2835
0
    return ToPointer(hSRS)->GetAngularUnits(
2836
0
        const_cast<const char **>(ppszName));
2837
0
}
2838
2839
/************************************************************************/
2840
/*                 SetLinearUnitsAndUpdateParameters()                  */
2841
/************************************************************************/
2842
2843
/**
2844
 * \brief Set the linear units for the projection.
2845
 *
2846
 * This method creates a UNIT subnode with the specified values as a
2847
 * child of the PROJCS or LOCAL_CS node.   It works the same as the
2848
 * SetLinearUnits() method, but it also updates all existing linear
2849
 * projection parameter values from the old units to the new units.
2850
 *
2851
 * @param pszName the units name to be used.  Some preferred units
2852
 * names can be found in ogr_srs_api.h such as SRS_UL_METER, SRS_UL_FOOT
2853
 * and SRS_UL_US_FOOT.
2854
 *
2855
 * @param dfInMeters the value to multiple by a length in the indicated
2856
 * units to transform to meters.  Some standard conversion factors can
2857
 * be found in ogr_srs_api.h.
2858
 *
2859
 * @param pszUnitAuthority Unit authority name. Or nullptr
2860
 *
2861
 * @param pszUnitCode Unit code. Or nullptr
2862
 *
2863
 * @return OGRERR_NONE on success.
2864
 */
2865
2866
OGRErr OGRSpatialReference::SetLinearUnitsAndUpdateParameters(
2867
    const char *pszName, double dfInMeters, const char *pszUnitAuthority,
2868
    const char *pszUnitCode)
2869
2870
0
{
2871
0
    TAKE_OPTIONAL_LOCK();
2872
2873
0
    if (dfInMeters <= 0.0)
2874
0
        return OGRERR_FAILURE;
2875
2876
0
    d->refreshProjObj();
2877
0
    if (!d->m_pj_crs)
2878
0
        return OGRERR_FAILURE;
2879
2880
0
    d->demoteFromBoundCRS();
2881
0
    if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
2882
0
    {
2883
0
        d->setPjCRS(proj_crs_alter_parameters_linear_unit(
2884
0
            d->getPROJContext(), d->m_pj_crs, pszName, dfInMeters,
2885
0
            pszUnitAuthority, pszUnitCode, true));
2886
0
    }
2887
0
    d->setPjCRS(proj_crs_alter_cs_linear_unit(d->getPROJContext(), d->m_pj_crs,
2888
0
                                              pszName, dfInMeters,
2889
0
                                              pszUnitAuthority, pszUnitCode));
2890
0
    d->undoDemoteFromBoundCRS();
2891
2892
0
    d->m_osLinearUnits = pszName;
2893
0
    d->dfToMeter = dfInMeters;
2894
2895
0
    return OGRERR_NONE;
2896
0
}
2897
2898
/************************************************************************/
2899
/*                OSRSetLinearUnitsAndUpdateParameters()                */
2900
/************************************************************************/
2901
2902
/**
2903
 * \brief Set the linear units for the projection.
2904
 *
2905
 * This function is the same as
2906
 *   OGRSpatialReference::SetLinearUnitsAndUpdateParameters()
2907
 */
2908
OGRErr OSRSetLinearUnitsAndUpdateParameters(OGRSpatialReferenceH hSRS,
2909
                                            const char *pszUnits,
2910
                                            double dfInMeters)
2911
2912
0
{
2913
0
    VALIDATE_POINTER1(hSRS, "OSRSetLinearUnitsAndUpdateParameters",
2914
0
                      OGRERR_FAILURE);
2915
2916
0
    return ToPointer(hSRS)->SetLinearUnitsAndUpdateParameters(pszUnits,
2917
0
                                                              dfInMeters);
2918
0
}
2919
2920
/************************************************************************/
2921
/*                           SetLinearUnits()                           */
2922
/************************************************************************/
2923
2924
/**
2925
 * \brief Set the linear units for the projection.
2926
 *
2927
 * This method creates a UNIT subnode with the specified values as a
2928
 * child of the PROJCS, GEOCCS, GEOGCS or LOCAL_CS node. When called on a
2929
 * Geographic 3D CRS the vertical axis units will be set.
2930
 *
2931
 * This method does the same as the C function OSRSetLinearUnits().
2932
 *
2933
 * @param pszUnitsName the units name to be used.  Some preferred units
2934
 * names can be found in ogr_srs_api.h such as SRS_UL_METER, SRS_UL_FOOT
2935
 * and SRS_UL_US_FOOT.
2936
 *
2937
 * @param dfInMeters the value to multiple by a length in the indicated
2938
 * units to transform to meters.  Some standard conversion factors can
2939
 * be found in ogr_srs_api.h.
2940
 *
2941
 * @return OGRERR_NONE on success.
2942
 */
2943
2944
OGRErr OGRSpatialReference::SetLinearUnits(const char *pszUnitsName,
2945
                                           double dfInMeters)
2946
2947
0
{
2948
0
    return SetTargetLinearUnits(nullptr, pszUnitsName, dfInMeters);
2949
0
}
2950
2951
/************************************************************************/
2952
/*                         OSRSetLinearUnits()                          */
2953
/************************************************************************/
2954
2955
/**
2956
 * \brief Set the linear units for the projection.
2957
 *
2958
 * This function is the same as OGRSpatialReference::SetLinearUnits()
2959
 */
2960
OGRErr OSRSetLinearUnits(OGRSpatialReferenceH hSRS, const char *pszUnits,
2961
                         double dfInMeters)
2962
2963
0
{
2964
0
    VALIDATE_POINTER1(hSRS, "OSRSetLinearUnits", OGRERR_FAILURE);
2965
2966
0
    return ToPointer(hSRS)->SetLinearUnits(pszUnits, dfInMeters);
2967
0
}
2968
2969
/************************************************************************/
2970
/*                        SetTargetLinearUnits()                        */
2971
/************************************************************************/
2972
2973
/**
2974
 * \brief Set the linear units for the projection.
2975
 *
2976
 * This method creates a UNIT subnode with the specified values as a
2977
 * child of the target node.
2978
 *
2979
 * This method does the same as the C function OSRSetTargetLinearUnits().
2980
 *
2981
 * @param pszTargetKey the keyword to set the linear units for.
2982
 * i.e. "PROJCS" or "VERT_CS"
2983
 *
2984
 * @param pszUnitsName the units name to be used.  Some preferred units
2985
 * names can be found in ogr_srs_api.h such as SRS_UL_METER, SRS_UL_FOOT
2986
 * and SRS_UL_US_FOOT.
2987
 *
2988
 * @param dfInMeters the value to multiple by a length in the indicated
2989
 * units to transform to meters.  Some standard conversion factors can
2990
 * be found in ogr_srs_api.h.
2991
 *
2992
 * @param pszUnitAuthority Unit authority name. Or nullptr
2993
 *
2994
 * @param pszUnitCode Unit code. Or nullptr
2995
 *
2996
 * @return OGRERR_NONE on success.
2997
 *
2998
 */
2999
3000
OGRErr OGRSpatialReference::SetTargetLinearUnits(const char *pszTargetKey,
3001
                                                 const char *pszUnitsName,
3002
                                                 double dfInMeters,
3003
                                                 const char *pszUnitAuthority,
3004
                                                 const char *pszUnitCode)
3005
3006
0
{
3007
0
    TAKE_OPTIONAL_LOCK();
3008
3009
0
    if (dfInMeters <= 0.0)
3010
0
        return OGRERR_FAILURE;
3011
3012
0
    d->refreshProjObj();
3013
0
    pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
3014
0
    if (pszTargetKey == nullptr)
3015
0
    {
3016
0
        if (!d->m_pj_crs)
3017
0
            return OGRERR_FAILURE;
3018
3019
0
        d->demoteFromBoundCRS();
3020
0
        if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
3021
0
        {
3022
0
            d->setPjCRS(proj_crs_alter_parameters_linear_unit(
3023
0
                d->getPROJContext(), d->m_pj_crs, pszUnitsName, dfInMeters,
3024
0
                pszUnitAuthority, pszUnitCode, false));
3025
0
        }
3026
0
        d->setPjCRS(proj_crs_alter_cs_linear_unit(
3027
0
            d->getPROJContext(), d->m_pj_crs, pszUnitsName, dfInMeters,
3028
0
            pszUnitAuthority, pszUnitCode));
3029
0
        d->undoDemoteFromBoundCRS();
3030
3031
0
        d->m_osLinearUnits = pszUnitsName;
3032
0
        d->dfToMeter = dfInMeters;
3033
3034
0
        return OGRERR_NONE;
3035
0
    }
3036
3037
0
    OGR_SRSNode *poCS = GetAttrNode(pszTargetKey);
3038
3039
0
    if (poCS == nullptr)
3040
0
        return OGRERR_FAILURE;
3041
3042
0
    char szValue[128] = {'\0'};
3043
0
    if (dfInMeters < std::numeric_limits<int>::max() &&
3044
0
        dfInMeters > std::numeric_limits<int>::min() &&
3045
0
        dfInMeters == static_cast<int>(dfInMeters))
3046
0
        snprintf(szValue, sizeof(szValue), "%d", static_cast<int>(dfInMeters));
3047
0
    else
3048
0
        OGRsnPrintDouble(szValue, sizeof(szValue), dfInMeters);
3049
3050
0
    OGR_SRSNode *poUnits = nullptr;
3051
0
    if (poCS->FindChild("UNIT") >= 0)
3052
0
    {
3053
0
        poUnits = poCS->GetChild(poCS->FindChild("UNIT"));
3054
0
        if (poUnits->GetChildCount() < 2)
3055
0
            return OGRERR_FAILURE;
3056
0
        poUnits->GetChild(0)->SetValue(pszUnitsName);
3057
0
        poUnits->GetChild(1)->SetValue(szValue);
3058
0
        if (poUnits->FindChild("AUTHORITY") != -1)
3059
0
            poUnits->DestroyChild(poUnits->FindChild("AUTHORITY"));
3060
0
    }
3061
0
    else
3062
0
    {
3063
0
        poUnits = new OGR_SRSNode("UNIT");
3064
0
        poUnits->AddChild(new OGR_SRSNode(pszUnitsName));
3065
0
        poUnits->AddChild(new OGR_SRSNode(szValue));
3066
3067
0
        poCS->AddChild(poUnits);
3068
0
    }
3069
3070
0
    return OGRERR_NONE;
3071
0
}
3072
3073
/************************************************************************/
3074
/*                         OSRSetLinearUnits()                          */
3075
/************************************************************************/
3076
3077
/**
3078
 * \brief Set the linear units for the target node.
3079
 *
3080
 * This function is the same as OGRSpatialReference::SetTargetLinearUnits()
3081
 *
3082
 */
3083
OGRErr OSRSetTargetLinearUnits(OGRSpatialReferenceH hSRS,
3084
                               const char *pszTargetKey, const char *pszUnits,
3085
                               double dfInMeters)
3086
3087
0
{
3088
0
    VALIDATE_POINTER1(hSRS, "OSRSetTargetLinearUnits", OGRERR_FAILURE);
3089
3090
0
    return ToPointer(hSRS)->SetTargetLinearUnits(pszTargetKey, pszUnits,
3091
0
                                                 dfInMeters);
3092
0
}
3093
3094
/************************************************************************/
3095
/*                           GetLinearUnits()                           */
3096
/************************************************************************/
3097
3098
/**
3099
 * \brief Fetch linear projection units.
3100
 *
3101
 * If no units are available, a value of "Meters" and 1.0 will be assumed.
3102
 * This method only checks directly under the PROJCS, GEOCCS, GEOGCS or
3103
 * LOCAL_CS node for units. When called on a Geographic 3D CRS the vertical
3104
 * axis units will be returned.
3105
 *
3106
 * This method does the same thing as the C function OSRGetLinearUnits()
3107
 *
3108
 * @param ppszName a pointer to be updated with the pointer to the units name.
3109
 * The returned value remains internal to the OGRSpatialReference and should
3110
 * not be freed, or modified.  It may be invalidated on the next
3111
 * OGRSpatialReference call.
3112
 *
3113
 * @return the value to multiply by linear distances to transform them to
3114
 * meters.
3115
 * @deprecated Use GetLinearUnits(const char**) const.
3116
 */
3117
3118
double OGRSpatialReference::GetLinearUnits(char **ppszName) const
3119
3120
0
{
3121
0
    return GetTargetLinearUnits(nullptr, const_cast<const char **>(ppszName));
3122
0
}
3123
3124
/**
3125
 * \brief Fetch linear projection units.
3126
 *
3127
 * If no units are available, a value of "Meters" and 1.0 will be assumed.
3128
 * This method only checks directly under the PROJCS, GEOCCS or LOCAL_CS node
3129
 * for units.
3130
 *
3131
 * This method does the same thing as the C function OSRGetLinearUnits()
3132
 *
3133
 * @param ppszName a pointer to be updated with the pointer to the units name.
3134
 * The returned value remains internal to the OGRSpatialReference and should
3135
 * not be freed, or modified.  It may be invalidated on the next
3136
 * OGRSpatialReference call.
3137
 *
3138
 * @return the value to multiply by linear distances to transform them to
3139
 * meters.
3140
 */
3141
3142
double OGRSpatialReference::GetLinearUnits(const char **ppszName) const
3143
3144
0
{
3145
0
    return GetTargetLinearUnits(nullptr, ppszName);
3146
0
}
3147
3148
/************************************************************************/
3149
/*                         OSRGetLinearUnits()                          */
3150
/************************************************************************/
3151
3152
/**
3153
 * \brief Fetch linear projection units.
3154
 *
3155
 * This function is the same as OGRSpatialReference::GetLinearUnits()
3156
 */
3157
double OSRGetLinearUnits(OGRSpatialReferenceH hSRS, char **ppszName)
3158
3159
0
{
3160
0
    VALIDATE_POINTER1(hSRS, "OSRGetLinearUnits", 0);
3161
3162
0
    return ToPointer(hSRS)->GetLinearUnits(const_cast<const char **>(ppszName));
3163
0
}
3164
3165
/************************************************************************/
3166
/*                        GetTargetLinearUnits()                        */
3167
/************************************************************************/
3168
3169
/**
3170
 * \brief Fetch linear units for target.
3171
 *
3172
 * If no units are available, a value of "Meters" and 1.0 will be assumed.
3173
 *
3174
 * This method does the same thing as the C function OSRGetTargetLinearUnits()
3175
 *
3176
 * @param pszTargetKey the key to look on. i.e. "PROJCS" or "VERT_CS". Might be
3177
 * NULL, in which case PROJCS will be implied (and if not found, LOCAL_CS,
3178
 * GEOCCS, GEOGCS and VERT_CS are looked up)
3179
 * @param ppszName a pointer to be updated with the pointer to the units name.
3180
 * The returned value remains internal to the OGRSpatialReference and should not
3181
 * be freed, or modified.  It may be invalidated on the next
3182
 * OGRSpatialReference call. ppszName can be set to NULL.
3183
 *
3184
 * @return the value to multiply by linear distances to transform them to
3185
 * meters.
3186
 *
3187
 * @deprecated Use GetTargetLinearUnits(const char*, const char**)
3188
 * const.
3189
 */
3190
3191
double OGRSpatialReference::GetTargetLinearUnits(const char *pszTargetKey,
3192
                                                 const char **ppszName) const
3193
3194
0
{
3195
0
    TAKE_OPTIONAL_LOCK();
3196
3197
0
    d->refreshProjObj();
3198
3199
0
    pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
3200
0
    if (pszTargetKey == nullptr)
3201
0
    {
3202
        // Use cached result if available
3203
0
        if (!d->m_osLinearUnits.empty())
3204
0
        {
3205
0
            if (ppszName)
3206
0
                *ppszName = d->m_osLinearUnits.c_str();
3207
0
            return d->dfToMeter;
3208
0
        }
3209
3210
0
        while (true)
3211
0
        {
3212
0
            if (d->m_pj_crs == nullptr)
3213
0
            {
3214
0
                break;
3215
0
            }
3216
3217
0
            d->demoteFromBoundCRS();
3218
0
            PJ *coordSys = nullptr;
3219
0
            if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
3220
0
            {
3221
0
                for (int iComponent = 0; iComponent < 2; iComponent++)
3222
0
                {
3223
0
                    auto subCRS = proj_crs_get_sub_crs(d->getPROJContext(),
3224
0
                                                       d->m_pj_crs, iComponent);
3225
0
                    if (subCRS && proj_get_type(subCRS) == PJ_TYPE_BOUND_CRS)
3226
0
                    {
3227
0
                        auto temp =
3228
0
                            proj_get_source_crs(d->getPROJContext(), subCRS);
3229
0
                        proj_destroy(subCRS);
3230
0
                        subCRS = temp;
3231
0
                    }
3232
0
                    if (subCRS &&
3233
0
                        (proj_get_type(subCRS) == PJ_TYPE_PROJECTED_CRS ||
3234
0
                         proj_get_type(subCRS) == PJ_TYPE_ENGINEERING_CRS ||
3235
0
                         proj_get_type(subCRS) == PJ_TYPE_VERTICAL_CRS))
3236
0
                    {
3237
0
                        coordSys = proj_crs_get_coordinate_system(
3238
0
                            d->getPROJContext(), subCRS);
3239
0
                        proj_destroy(subCRS);
3240
0
                        break;
3241
0
                    }
3242
0
                    else if (subCRS)
3243
0
                    {
3244
0
                        proj_destroy(subCRS);
3245
0
                    }
3246
0
                }
3247
0
                if (coordSys == nullptr)
3248
0
                {
3249
0
                    d->undoDemoteFromBoundCRS();
3250
0
                    break;
3251
0
                }
3252
0
            }
3253
0
            else
3254
0
            {
3255
0
                coordSys = proj_crs_get_coordinate_system(d->getPROJContext(),
3256
0
                                                          d->m_pj_crs);
3257
0
            }
3258
3259
0
            d->undoDemoteFromBoundCRS();
3260
0
            if (!coordSys)
3261
0
            {
3262
0
                break;
3263
0
            }
3264
0
            auto csType = proj_cs_get_type(d->getPROJContext(), coordSys);
3265
3266
0
            if (csType != PJ_CS_TYPE_CARTESIAN &&
3267
0
                csType != PJ_CS_TYPE_VERTICAL &&
3268
0
                csType != PJ_CS_TYPE_ELLIPSOIDAL &&
3269
0
                csType != PJ_CS_TYPE_SPHERICAL)
3270
0
            {
3271
0
                proj_destroy(coordSys);
3272
0
                break;
3273
0
            }
3274
3275
0
            int axis = 0;
3276
3277
0
            if (csType == PJ_CS_TYPE_ELLIPSOIDAL ||
3278
0
                csType == PJ_CS_TYPE_SPHERICAL)
3279
0
            {
3280
0
                const int axisCount =
3281
0
                    proj_cs_get_axis_count(d->getPROJContext(), coordSys);
3282
3283
0
                if (axisCount == 3)
3284
0
                {
3285
0
                    axis = 2;
3286
0
                }
3287
0
                else
3288
0
                {
3289
0
                    proj_destroy(coordSys);
3290
0
                    break;
3291
0
                }
3292
0
            }
3293
3294
0
            double dfConvFactor = 0.0;
3295
0
            const char *pszUnitName = nullptr;
3296
0
            if (!proj_cs_get_axis_info(d->getPROJContext(), coordSys, axis,
3297
0
                                       nullptr, nullptr, nullptr, &dfConvFactor,
3298
0
                                       &pszUnitName, nullptr, nullptr))
3299
0
            {
3300
0
                proj_destroy(coordSys);
3301
0
                break;
3302
0
            }
3303
3304
0
            d->m_osLinearUnits = pszUnitName;
3305
0
            d->dfToMeter = dfConvFactor;
3306
0
            if (ppszName)
3307
0
                *ppszName = d->m_osLinearUnits.c_str();
3308
3309
0
            proj_destroy(coordSys);
3310
0
            return dfConvFactor;
3311
0
        }
3312
3313
0
        d->m_osLinearUnits = "unknown";
3314
0
        d->dfToMeter = 1.0;
3315
3316
0
        if (ppszName != nullptr)
3317
0
            *ppszName = d->m_osLinearUnits.c_str();
3318
0
        return 1.0;
3319
0
    }
3320
3321
0
    const OGR_SRSNode *poCS = GetAttrNode(pszTargetKey);
3322
3323
0
    if (ppszName != nullptr)
3324
0
        *ppszName = "unknown";
3325
3326
0
    if (poCS == nullptr)
3327
0
        return 1.0;
3328
3329
0
    for (int iChild = 0; iChild < poCS->GetChildCount(); iChild++)
3330
0
    {
3331
0
        const OGR_SRSNode *poChild = poCS->GetChild(iChild);
3332
3333
0
        if (EQUAL(poChild->GetValue(), "UNIT") && poChild->GetChildCount() >= 2)
3334
0
        {
3335
0
            if (ppszName != nullptr)
3336
0
                *ppszName = poChild->GetChild(0)->GetValue();
3337
3338
0
            return CPLAtof(poChild->GetChild(1)->GetValue());
3339
0
        }
3340
0
    }
3341
3342
0
    return 1.0;
3343
0
}
3344
3345
/**
3346
 * \brief Fetch linear units for target.
3347
 *
3348
 * If no units are available, a value of "Meters" and 1.0 will be assumed.
3349
 *
3350
 * This method does the same thing as the C function OSRGetTargetLinearUnits()
3351
 *
3352
 * @param pszTargetKey the key to look on. i.e. "PROJCS" or "VERT_CS". Might be
3353
 * NULL, in which case PROJCS will be implied (and if not found, LOCAL_CS,
3354
 * GEOCCS and VERT_CS are looked up)
3355
 * @param ppszName a pointer to be updated with the pointer to the units name.
3356
 * The returned value remains internal to the OGRSpatialReference and should not
3357
 * be freed, or modified.  It may be invalidated on the next
3358
 * OGRSpatialReference call. ppszName can be set to NULL.
3359
 *
3360
 * @return the value to multiply by linear distances to transform them to
3361
 * meters.
3362
 *
3363
 */
3364
3365
double OGRSpatialReference::GetTargetLinearUnits(const char *pszTargetKey,
3366
                                                 char **ppszName) const
3367
3368
0
{
3369
0
    return GetTargetLinearUnits(pszTargetKey,
3370
0
                                const_cast<const char **>(ppszName));
3371
0
}
3372
3373
/************************************************************************/
3374
/*                      OSRGetTargetLinearUnits()                       */
3375
/************************************************************************/
3376
3377
/**
3378
 * \brief Fetch linear projection units.
3379
 *
3380
 * This function is the same as OGRSpatialReference::GetTargetLinearUnits()
3381
 *
3382
 */
3383
double OSRGetTargetLinearUnits(OGRSpatialReferenceH hSRS,
3384
                               const char *pszTargetKey, char **ppszName)
3385
3386
0
{
3387
0
    VALIDATE_POINTER1(hSRS, "OSRGetTargetLinearUnits", 0);
3388
3389
0
    return ToPointer(hSRS)->GetTargetLinearUnits(
3390
0
        pszTargetKey, const_cast<const char **>(ppszName));
3391
0
}
3392
3393
/************************************************************************/
3394
/*                          GetPrimeMeridian()                          */
3395
/************************************************************************/
3396
3397
/**
3398
 * \brief Fetch prime meridian info.
3399
 *
3400
 * Returns the offset of the prime meridian from greenwich in degrees,
3401
 * and the prime meridian name (if requested).   If no PRIMEM value exists
3402
 * in the coordinate system definition a value of "Greenwich" and an
3403
 * offset of 0.0 is assumed.
3404
 *
3405
 * If the prime meridian name is returned, the pointer is to an internal
3406
 * copy of the name. It should not be freed, altered or depended on after
3407
 * the next OGR call.
3408
 *
3409
 * This method is the same as the C function OSRGetPrimeMeridian().
3410
 *
3411
 * @param ppszName return location for prime meridian name.  If NULL, name
3412
 * is not returned.
3413
 *
3414
 * @return the offset to the GEOGCS prime meridian from greenwich in decimal
3415
 * degrees.
3416
 * @deprecated Use GetPrimeMeridian(const char**) const.
3417
 */
3418
3419
double OGRSpatialReference::GetPrimeMeridian(const char **ppszName) const
3420
3421
0
{
3422
0
    TAKE_OPTIONAL_LOCK();
3423
3424
0
    d->refreshProjObj();
3425
3426
0
    if (!d->m_osPrimeMeridianName.empty())
3427
0
    {
3428
0
        if (ppszName != nullptr)
3429
0
            *ppszName = d->m_osPrimeMeridianName.c_str();
3430
0
        return d->dfFromGreenwich;
3431
0
    }
3432
3433
0
    while (true)
3434
0
    {
3435
0
        if (!d->m_pj_crs)
3436
0
            break;
3437
3438
0
        auto pm = proj_get_prime_meridian(d->getPROJContext(), d->m_pj_crs);
3439
0
        if (!pm)
3440
0
            break;
3441
3442
0
        d->m_osPrimeMeridianName = proj_get_name(pm);
3443
0
        if (ppszName)
3444
0
            *ppszName = d->m_osPrimeMeridianName.c_str();
3445
0
        double dfLongitude = 0.0;
3446
0
        double dfConvFactor = 0.0;
3447
0
        proj_prime_meridian_get_parameters(
3448
0
            d->getPROJContext(), pm, &dfLongitude, &dfConvFactor, nullptr);
3449
0
        proj_destroy(pm);
3450
0
        d->dfFromGreenwich =
3451
0
            dfLongitude * dfConvFactor / CPLAtof(SRS_UA_DEGREE_CONV);
3452
0
        return d->dfFromGreenwich;
3453
0
    }
3454
3455
0
    d->m_osPrimeMeridianName = SRS_PM_GREENWICH;
3456
0
    d->dfFromGreenwich = 0.0;
3457
0
    if (ppszName != nullptr)
3458
0
        *ppszName = d->m_osPrimeMeridianName.c_str();
3459
0
    return d->dfFromGreenwich;
3460
0
}
3461
3462
/**
3463
 * \brief Fetch prime meridian info.
3464
 *
3465
 * Returns the offset of the prime meridian from greenwich in degrees,
3466
 * and the prime meridian name (if requested).   If no PRIMEM value exists
3467
 * in the coordinate system definition a value of "Greenwich" and an
3468
 * offset of 0.0 is assumed.
3469
 *
3470
 * If the prime meridian name is returned, the pointer is to an internal
3471
 * copy of the name. It should not be freed, altered or depended on after
3472
 * the next OGR call.
3473
 *
3474
 * This method is the same as the C function OSRGetPrimeMeridian().
3475
 *
3476
 * @param ppszName return location for prime meridian name.  If NULL, name
3477
 * is not returned.
3478
 *
3479
 * @return the offset to the GEOGCS prime meridian from greenwich in decimal
3480
 * degrees.
3481
 */
3482
3483
double OGRSpatialReference::GetPrimeMeridian(char **ppszName) const
3484
3485
0
{
3486
0
    return GetPrimeMeridian(const_cast<const char **>(ppszName));
3487
0
}
3488
3489
/************************************************************************/
3490
/*                        OSRGetPrimeMeridian()                         */
3491
/************************************************************************/
3492
3493
/**
3494
 * \brief Fetch prime meridian info.
3495
 *
3496
 * This function is the same as OGRSpatialReference::GetPrimeMeridian()
3497
 */
3498
double OSRGetPrimeMeridian(OGRSpatialReferenceH hSRS, char **ppszName)
3499
3500
0
{
3501
0
    VALIDATE_POINTER1(hSRS, "OSRGetPrimeMeridian", 0);
3502
3503
0
    return ToPointer(hSRS)->GetPrimeMeridian(
3504
0
        const_cast<const char **>(ppszName));
3505
0
}
3506
3507
/************************************************************************/
3508
/*                             SetGeogCS()                              */
3509
/************************************************************************/
3510
3511
/**
3512
 * \brief Set geographic coordinate system.
3513
 *
3514
 * This method is used to set the datum, ellipsoid, prime meridian and
3515
 * angular units for a geographic coordinate system.  It can be used on its
3516
 * own to establish a geographic spatial reference, or applied to a
3517
 * projected coordinate system to establish the underlying geographic
3518
 * coordinate system.
3519
 *
3520
 * This method does the same as the C function OSRSetGeogCS().
3521
 *
3522
 * @param pszGeogName user visible name for the geographic coordinate system
3523
 * (not to serve as a key).
3524
 *
3525
 * @param pszDatumName key name for this datum.  The OpenGIS specification
3526
 * lists some known values, and otherwise EPSG datum names with a standard
3527
 * transformation are considered legal keys.
3528
 *
3529
 * @param pszSpheroidName user visible spheroid name (not to serve as a key)
3530
 *
3531
 * @param dfSemiMajor the semi major axis of the spheroid.
3532
 *
3533
 * @param dfInvFlattening the inverse flattening for the spheroid.
3534
 * This can be computed from the semi minor axis as
3535
 * 1/f = 1.0 / (1.0 - semiminor/semimajor).
3536
 *
3537
 * @param pszPMName the name of the prime meridian (not to serve as a key)
3538
 * If this is NULL a default value of "Greenwich" will be used.
3539
 *
3540
 * @param dfPMOffset the longitude of Greenwich relative to this prime
3541
 * meridian. Always in Degrees
3542
 *
3543
 * @param pszAngularUnits the angular units name (see ogr_srs_api.h for some
3544
 * standard names).  If NULL a value of "degrees" will be assumed.
3545
 *
3546
 * @param dfConvertToRadians value to multiply angular units by to transform
3547
 * them to radians.  A value of SRS_UA_DEGREE_CONV will be used if
3548
 * pszAngularUnits is NULL.
3549
 *
3550
 * @return OGRERR_NONE on success.
3551
 */
3552
3553
OGRErr OGRSpatialReference::SetGeogCS(
3554
    const char *pszGeogName, const char *pszDatumName,
3555
    const char *pszSpheroidName, double dfSemiMajor, double dfInvFlattening,
3556
    const char *pszPMName, double dfPMOffset, const char *pszAngularUnits,
3557
    double dfConvertToRadians)
3558
3559
0
{
3560
0
    TAKE_OPTIONAL_LOCK();
3561
3562
0
    d->bNormInfoSet = FALSE;
3563
0
    d->m_osAngularUnits.clear();
3564
0
    d->m_dfAngularUnitToRadian = 0.0;
3565
0
    d->m_osPrimeMeridianName.clear();
3566
0
    d->dfFromGreenwich = 0.0;
3567
3568
    /* -------------------------------------------------------------------- */
3569
    /*      For a geocentric coordinate system we want to set the datum     */
3570
    /*      and ellipsoid based on the GEOGCS.  Create the GEOGCS in a      */
3571
    /*      temporary srs and use the copy method which has special         */
3572
    /*      handling for GEOCCS.                                            */
3573
    /* -------------------------------------------------------------------- */
3574
0
    if (IsGeocentric())
3575
0
    {
3576
0
        OGRSpatialReference oGCS;
3577
3578
0
        oGCS.SetGeogCS(pszGeogName, pszDatumName, pszSpheroidName, dfSemiMajor,
3579
0
                       dfInvFlattening, pszPMName, dfPMOffset, pszAngularUnits,
3580
0
                       dfConvertToRadians);
3581
0
        return CopyGeogCSFrom(&oGCS);
3582
0
    }
3583
3584
0
    auto cs = proj_create_ellipsoidal_2D_cs(
3585
0
        d->getPROJContext(), PJ_ELLPS2D_LATITUDE_LONGITUDE, pszAngularUnits,
3586
0
        dfConvertToRadians);
3587
    // Prime meridian expressed in Degree
3588
0
    auto obj = proj_create_geographic_crs(
3589
0
        d->getPROJContext(), pszGeogName, pszDatumName, pszSpheroidName,
3590
0
        dfSemiMajor, dfInvFlattening, pszPMName, dfPMOffset, nullptr, 0.0, cs);
3591
0
    proj_destroy(cs);
3592
3593
0
    if (d->m_pj_crs == nullptr || d->m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
3594
0
        d->m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS)
3595
0
    {
3596
0
        d->setPjCRS(obj);
3597
0
    }
3598
0
    else if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
3599
0
    {
3600
0
        d->setPjCRS(
3601
0
            proj_crs_alter_geodetic_crs(d->getPROJContext(), d->m_pj_crs, obj));
3602
0
        proj_destroy(obj);
3603
0
    }
3604
0
    else
3605
0
    {
3606
0
        proj_destroy(obj);
3607
0
    }
3608
3609
0
    return OGRERR_NONE;
3610
0
}
3611
3612
/************************************************************************/
3613
/*                            OSRSetGeogCS()                            */
3614
/************************************************************************/
3615
3616
/**
3617
 * \brief Set geographic coordinate system.
3618
 *
3619
 * This function is the same as OGRSpatialReference::SetGeogCS()
3620
 */
3621
OGRErr OSRSetGeogCS(OGRSpatialReferenceH hSRS, const char *pszGeogName,
3622
                    const char *pszDatumName, const char *pszSpheroidName,
3623
                    double dfSemiMajor, double dfInvFlattening,
3624
                    const char *pszPMName, double dfPMOffset,
3625
                    const char *pszAngularUnits, double dfConvertToRadians)
3626
3627
0
{
3628
0
    VALIDATE_POINTER1(hSRS, "OSRSetGeogCS", OGRERR_FAILURE);
3629
3630
0
    return ToPointer(hSRS)->SetGeogCS(pszGeogName, pszDatumName,
3631
0
                                      pszSpheroidName, dfSemiMajor,
3632
0
                                      dfInvFlattening, pszPMName, dfPMOffset,
3633
0
                                      pszAngularUnits, dfConvertToRadians);
3634
0
}
3635
3636
/************************************************************************/
3637
/*                         SetWellKnownGeogCS()                         */
3638
/************************************************************************/
3639
3640
/**
3641
 * \brief Set a GeogCS based on well known name.
3642
 *
3643
 * This may be called on an empty OGRSpatialReference to make a geographic
3644
 * coordinate system, or on something with an existing PROJCS node to
3645
 * set the underlying geographic coordinate system of a projected coordinate
3646
 * system.
3647
 *
3648
 * The following well known text values are currently supported,
3649
 * Except for "EPSG:n", the others are without dependency on EPSG data files:
3650
 * <ul>
3651
 * <li> "EPSG:n": where n is the code a Geographic coordinate reference system.
3652
 * <li> "WGS84": same as "EPSG:4326" (axis order lat/long).
3653
 * <li> "WGS72": same as "EPSG:4322" (axis order lat/long).
3654
 * <li> "NAD83": same as "EPSG:4269" (axis order lat/long).
3655
 * <li> "NAD27": same as "EPSG:4267" (axis order lat/long).
3656
 * <li> "CRS84", "CRS:84": same as "WGS84" but with axis order long/lat.
3657
 * <li> "CRS72", "CRS:72": same as "WGS72" but with axis order long/lat.
3658
 * <li> "CRS27", "CRS:27": same as "NAD27" but with axis order long/lat.
3659
 * </ul>
3660
 *
3661
 * @param pszName name of well known geographic coordinate system.
3662
 * @return OGRERR_NONE on success, or OGRERR_FAILURE if the name isn't
3663
 * recognised, the target object is already initialized, or an EPSG value
3664
 * can't be successfully looked up.
3665
 */
3666
3667
OGRErr OGRSpatialReference::SetWellKnownGeogCS(const char *pszName)
3668
3669
0
{
3670
0
    TAKE_OPTIONAL_LOCK();
3671
3672
    /* -------------------------------------------------------------------- */
3673
    /*      Check for EPSG authority numbers.                               */
3674
    /* -------------------------------------------------------------------- */
3675
0
    if (STARTS_WITH_CI(pszName, "EPSG:") || STARTS_WITH_CI(pszName, "EPSGA:"))
3676
0
    {
3677
0
        OGRSpatialReference oSRS2;
3678
0
        const OGRErr eErr = oSRS2.importFromEPSG(atoi(pszName + 5));
3679
0
        if (eErr != OGRERR_NONE)
3680
0
            return eErr;
3681
3682
0
        if (!oSRS2.IsGeographic())
3683
0
            return OGRERR_FAILURE;
3684
3685
0
        return CopyGeogCSFrom(&oSRS2);
3686
0
    }
3687
3688
    /* -------------------------------------------------------------------- */
3689
    /*      Check for simple names.                                         */
3690
    /* -------------------------------------------------------------------- */
3691
0
    const char *pszWKT = nullptr;
3692
3693
0
    if (EQUAL(pszName, "WGS84"))
3694
0
    {
3695
0
        pszWKT = SRS_WKT_WGS84_LAT_LONG;
3696
0
    }
3697
0
    else if (EQUAL(pszName, "CRS84") || EQUAL(pszName, "CRS:84"))
3698
0
    {
3699
0
        pszWKT =
3700
0
            "GEOGCRS[\"WGS 84 (CRS84)\",DATUM[\"World Geodetic System 1984\","
3701
0
            "ELLIPSOID[\"WGS "
3702
0
            "84\",6378137,298.257223563,LENGTHUNIT[\"metre\",1]]],"
3703
0
            "PRIMEM[\"Greenwich\",0,ANGLEUNIT[\"degree\",0.0174532925199433]],"
3704
0
            "CS[ellipsoidal,2],AXIS[\"geodetic longitude (Lon)\",east,ORDER[1],"
3705
0
            "ANGLEUNIT[\"degree\",0.0174532925199433]],"
3706
0
            "AXIS[\"geodetic latitude (Lat)\",north,ORDER[2],"
3707
0
            "ANGLEUNIT[\"degree\",0.0174532925199433]],"
3708
0
            "USAGE[SCOPE[\"unknown\"],AREA[\"World\"],BBOX[-90,-180,90,180]],"
3709
0
            "ID[\"OGC\",\"CRS84\"]]";
3710
0
    }
3711
0
    else if (EQUAL(pszName, "WGS72"))
3712
0
        pszWKT =
3713
0
            "GEOGCS[\"WGS 72\",DATUM[\"WGS_1972\","
3714
0
            "SPHEROID[\"WGS 72\",6378135,298.26,AUTHORITY[\"EPSG\",\"7043\"]],"
3715
0
            "AUTHORITY[\"EPSG\",\"6322\"]],"
3716
0
            "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3717
0
            "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3718
0
            "AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],"
3719
0
            "AUTHORITY[\"EPSG\",\"4322\"]]";
3720
3721
0
    else if (EQUAL(pszName, "NAD27"))
3722
0
        pszWKT =
3723
0
            "GEOGCS[\"NAD27\",DATUM[\"North_American_Datum_1927\","
3724
0
            "SPHEROID[\"Clarke 1866\",6378206.4,294.9786982138982,"
3725
0
            "AUTHORITY[\"EPSG\",\"7008\"]],AUTHORITY[\"EPSG\",\"6267\"]],"
3726
0
            "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3727
0
            "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3728
0
            "AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],"
3729
0
            "AUTHORITY[\"EPSG\",\"4267\"]]";
3730
3731
0
    else if (EQUAL(pszName, "CRS27") || EQUAL(pszName, "CRS:27"))
3732
0
        pszWKT =
3733
0
            "GEOGCS[\"NAD27\",DATUM[\"North_American_Datum_1927\","
3734
0
            "SPHEROID[\"Clarke 1866\",6378206.4,294.9786982138982,"
3735
0
            "AUTHORITY[\"EPSG\",\"7008\"]],AUTHORITY[\"EPSG\",\"6267\"]],"
3736
0
            "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3737
0
            "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3738
0
            "AXIS[\"Longitude\",EAST],AXIS[\"Latitude\",NORTH]]";
3739
3740
0
    else if (EQUAL(pszName, "NAD83"))
3741
0
        pszWKT =
3742
0
            "GEOGCS[\"NAD83\",DATUM[\"North_American_Datum_1983\","
3743
0
            "SPHEROID[\"GRS 1980\",6378137,298.257222101,"
3744
0
            "AUTHORITY[\"EPSG\",\"7019\"]],"
3745
0
            "AUTHORITY[\"EPSG\",\"6269\"]],"
3746
0
            "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3747
0
            "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3748
0
            "AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],AUTHORITY["
3749
0
            "\"EPSG\",\"4269\"]]";
3750
3751
0
    else if (EQUAL(pszName, "CRS83") || EQUAL(pszName, "CRS:83"))
3752
0
        pszWKT =
3753
0
            "GEOGCS[\"NAD83\",DATUM[\"North_American_Datum_1983\","
3754
0
            "SPHEROID[\"GRS 1980\",6378137,298.257222101,"
3755
0
            "AUTHORITY[\"EPSG\",\"7019\"]],"
3756
0
            "AUTHORITY[\"EPSG\",\"6269\"]],"
3757
0
            "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
3758
0
            "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
3759
0
            "AXIS[\"Longitude\",EAST],AXIS[\"Latitude\",NORTH]]";
3760
3761
0
    else
3762
0
        return OGRERR_FAILURE;
3763
3764
    /* -------------------------------------------------------------------- */
3765
    /*      Import the WKT                                                  */
3766
    /* -------------------------------------------------------------------- */
3767
0
    OGRSpatialReference oSRS2;
3768
0
    const OGRErr eErr = oSRS2.importFromWkt(pszWKT);
3769
0
    if (eErr != OGRERR_NONE)
3770
0
        return eErr;
3771
3772
    /* -------------------------------------------------------------------- */
3773
    /*      Copy over.                                                      */
3774
    /* -------------------------------------------------------------------- */
3775
0
    return CopyGeogCSFrom(&oSRS2);
3776
0
}
3777
3778
/************************************************************************/
3779
/*                       OSRSetWellKnownGeogCS()                        */
3780
/************************************************************************/
3781
3782
/**
3783
 * \brief Set a GeogCS based on well known name.
3784
 *
3785
 * This function is the same as OGRSpatialReference::SetWellKnownGeogCS()
3786
 */
3787
OGRErr OSRSetWellKnownGeogCS(OGRSpatialReferenceH hSRS, const char *pszName)
3788
3789
0
{
3790
0
    VALIDATE_POINTER1(hSRS, "OSRSetWellKnownGeogCS", OGRERR_FAILURE);
3791
3792
0
    return ToPointer(hSRS)->SetWellKnownGeogCS(pszName);
3793
0
}
3794
3795
/************************************************************************/
3796
/*                           CopyGeogCSFrom()                           */
3797
/************************************************************************/
3798
3799
/**
3800
 * \brief Copy GEOGCS from another OGRSpatialReference.
3801
 *
3802
 * The GEOGCS information is copied into this OGRSpatialReference from another.
3803
 * If this object has a PROJCS root already, the GEOGCS is installed within
3804
 * it, otherwise it is installed as the root.
3805
 *
3806
 * @param poSrcSRS the spatial reference to copy the GEOGCS information from.
3807
 *
3808
 * @return OGRERR_NONE on success or an error code.
3809
 */
3810
3811
OGRErr OGRSpatialReference::CopyGeogCSFrom(const OGRSpatialReference *poSrcSRS)
3812
3813
0
{
3814
0
    TAKE_OPTIONAL_LOCK();
3815
3816
0
    d->bNormInfoSet = FALSE;
3817
0
    d->m_osAngularUnits.clear();
3818
0
    d->m_dfAngularUnitToRadian = 0.0;
3819
0
    d->m_osPrimeMeridianName.clear();
3820
0
    d->dfFromGreenwich = 0.0;
3821
3822
0
    d->refreshProjObj();
3823
0
    poSrcSRS->d->refreshProjObj();
3824
0
    if (!poSrcSRS->d->m_pj_crs)
3825
0
    {
3826
0
        return OGRERR_FAILURE;
3827
0
    }
3828
0
    auto geodCRS =
3829
0
        proj_crs_get_geodetic_crs(d->getPROJContext(), poSrcSRS->d->m_pj_crs);
3830
0
    if (!geodCRS)
3831
0
    {
3832
0
        return OGRERR_FAILURE;
3833
0
    }
3834
3835
    /* -------------------------------------------------------------------- */
3836
    /*      Handle geocentric coordinate systems specially.  We just        */
3837
    /*      want to copy the DATUM.                                         */
3838
    /* -------------------------------------------------------------------- */
3839
0
    if (d->m_pjType == PJ_TYPE_GEOCENTRIC_CRS)
3840
0
    {
3841
0
        auto datum = proj_crs_get_datum(d->getPROJContext(), geodCRS);
3842
0
#if PROJ_VERSION_MAJOR > 7 ||                                                  \
3843
0
    (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
3844
0
        if (datum == nullptr)
3845
0
        {
3846
0
            datum = proj_crs_get_datum_ensemble(d->getPROJContext(), geodCRS);
3847
0
        }
3848
0
#endif
3849
0
        if (datum == nullptr)
3850
0
        {
3851
0
            proj_destroy(geodCRS);
3852
0
            return OGRERR_FAILURE;
3853
0
        }
3854
3855
0
        const char *pszUnitName = nullptr;
3856
0
        double unitConvFactor = GetLinearUnits(&pszUnitName);
3857
3858
0
        auto pj_crs = proj_create_geocentric_crs_from_datum(
3859
0
            d->getPROJContext(), proj_get_name(d->m_pj_crs), datum, pszUnitName,
3860
0
            unitConvFactor);
3861
0
        proj_destroy(datum);
3862
3863
0
        d->setPjCRS(pj_crs);
3864
0
    }
3865
3866
0
    else if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
3867
0
    {
3868
0
        auto pj_crs = proj_crs_alter_geodetic_crs(d->getPROJContext(),
3869
0
                                                  d->m_pj_crs, geodCRS);
3870
0
        d->setPjCRS(pj_crs);
3871
0
    }
3872
3873
0
    else
3874
0
    {
3875
0
        d->setPjCRS(proj_clone(d->getPROJContext(), geodCRS));
3876
0
    }
3877
3878
    // Apply TOWGS84 of source CRS
3879
0
    if (poSrcSRS->d->m_pjType == PJ_TYPE_BOUND_CRS)
3880
0
    {
3881
0
        auto target =
3882
0
            proj_get_target_crs(d->getPROJContext(), poSrcSRS->d->m_pj_crs);
3883
0
        auto co = proj_crs_get_coordoperation(d->getPROJContext(),
3884
0
                                              poSrcSRS->d->m_pj_crs);
3885
0
        d->setPjCRS(proj_crs_create_bound_crs(d->getPROJContext(), d->m_pj_crs,
3886
0
                                              target, co));
3887
0
        proj_destroy(target);
3888
0
        proj_destroy(co);
3889
0
    }
3890
3891
0
    proj_destroy(geodCRS);
3892
3893
0
    return OGRERR_NONE;
3894
0
}
3895
3896
/************************************************************************/
3897
/*                         OSRCopyGeogCSFrom()                          */
3898
/************************************************************************/
3899
3900
/**
3901
 * \brief Copy GEOGCS from another OGRSpatialReference.
3902
 *
3903
 * This function is the same as OGRSpatialReference::CopyGeogCSFrom()
3904
 */
3905
OGRErr OSRCopyGeogCSFrom(OGRSpatialReferenceH hSRS,
3906
                         const OGRSpatialReferenceH hSrcSRS)
3907
3908
0
{
3909
0
    VALIDATE_POINTER1(hSRS, "OSRCopyGeogCSFrom", OGRERR_FAILURE);
3910
0
    VALIDATE_POINTER1(hSrcSRS, "OSRCopyGeogCSFrom", OGRERR_FAILURE);
3911
3912
0
    return ToPointer(hSRS)->CopyGeogCSFrom(ToPointer(hSrcSRS));
3913
0
}
3914
3915
/************************************************************************/
3916
/*                   SET_FROM_USER_INPUT_LIMITATIONS_get()              */
3917
/************************************************************************/
3918
3919
/** Limitations for OGRSpatialReference::SetFromUserInput().
3920
 *
3921
 * Currently ALLOW_NETWORK_ACCESS=NO and ALLOW_FILE_ACCESS=NO.
3922
 */
3923
const char *const OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS[] = {
3924
    "ALLOW_NETWORK_ACCESS=NO", "ALLOW_FILE_ACCESS=NO", nullptr};
3925
3926
/**
3927
 * \brief Return OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS
3928
 */
3929
CSLConstList OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS_get()
3930
0
{
3931
0
    return SET_FROM_USER_INPUT_LIMITATIONS;
3932
0
}
3933
3934
/************************************************************************/
3935
/*                      RemoveIDFromMemberOfEnsembles()                 */
3936
/************************************************************************/
3937
3938
// cppcheck-suppress constParameterReference
3939
static void RemoveIDFromMemberOfEnsembles(CPLJSONObject &obj)
3940
0
{
3941
    // Remove "id" from members of datum ensembles for compatibility with
3942
    // older PROJ versions
3943
    // Cf https://github.com/opengeospatial/geoparquet/discussions/110
3944
    // and https://github.com/OSGeo/PROJ/pull/3221
3945
0
    if (obj.GetType() == CPLJSONObject::Type::Object)
3946
0
    {
3947
0
        for (auto &subObj : obj.GetChildren())
3948
0
        {
3949
0
            RemoveIDFromMemberOfEnsembles(subObj);
3950
0
        }
3951
0
    }
3952
0
    else if (obj.GetType() == CPLJSONObject::Type::Array &&
3953
0
             obj.GetName() == "members")
3954
0
    {
3955
0
        for (auto &subObj : obj.ToArray())
3956
0
        {
3957
0
            if (subObj.GetType() == CPLJSONObject::Type::Object)
3958
0
            {
3959
0
                subObj.Delete("id");
3960
0
            }
3961
0
        }
3962
0
    }
3963
0
}
3964
3965
/************************************************************************/
3966
/*                          SetFromUserInput()                          */
3967
/************************************************************************/
3968
3969
/**
3970
 * \brief Set spatial reference from various text formats.
3971
 *
3972
 * This method will examine the provided input, and try to deduce the
3973
 * format, and then use it to initialize the spatial reference system.  It
3974
 * may take the following forms:
3975
 *
3976
 * <ol>
3977
 * <li> Well Known Text definition - passed on to importFromWkt().
3978
 * <li> "EPSG:n" - number passed on to importFromEPSG().
3979
 * <li> "EPSGA:n" - number passed on to importFromEPSGA().
3980
 * <li> "AUTO:proj_id,unit_id,lon0,lat0" - WMS auto projections.
3981
 * <li> "urn:ogc:def:crs:EPSG::n" - ogc urns
3982
 * <li> PROJ.4 definitions - passed on to importFromProj4().
3983
 * <li> filename - file read for WKT, XML or PROJ.4 definition.
3984
 * <li> well known name accepted by SetWellKnownGeogCS(), such as NAD27, NAD83,
3985
 * WGS84 or WGS72.
3986
 * <li> "IGNF:xxxx", "ESRI:xxxx", etc. from definitions from the PROJ database;
3987
 * <li> PROJJSON (PROJ &gt;= 6.2)
3988
 * </ol>
3989
 *
3990
 * It is expected that this method will be extended in the future to support
3991
 * XML and perhaps a simplified "minilanguage" for indicating common UTM and
3992
 * State Plane definitions.
3993
 *
3994
 * This method is intended to be flexible, but by its nature it is
3995
 * imprecise as it must guess information about the format intended.  When
3996
 * possible applications should call the specific method appropriate if the
3997
 * input is known to be in a particular format.
3998
 *
3999
 * This method does the same thing as the OSRSetFromUserInput() function.
4000
 *
4001
 * @param pszDefinition text definition to try to deduce SRS from.
4002
 *
4003
 * @return OGRERR_NONE on success, or an error code if the name isn't
4004
 * recognised, the definition is corrupt, or an EPSG value can't be
4005
 * successfully looked up.
4006
 */
4007
4008
OGRErr OGRSpatialReference::SetFromUserInput(const char *pszDefinition)
4009
0
{
4010
0
    return SetFromUserInput(pszDefinition, nullptr);
4011
0
}
4012
4013
/**
4014
 * \brief Set spatial reference from various text formats.
4015
 *
4016
 * This method will examine the provided input, and try to deduce the
4017
 * format, and then use it to initialize the spatial reference system.  It
4018
 * may take the following forms:
4019
 *
4020
 * <ol>
4021
 * <li> Well Known Text definition - passed on to importFromWkt().
4022
 * <li> "EPSG:n" - number passed on to importFromEPSG().
4023
 * <li> "EPSGA:n" - number passed on to importFromEPSGA().
4024
 * <li> "AUTO:proj_id,unit_id,lon0,lat0" - WMS auto projections.
4025
 * <li> "urn:ogc:def:crs:EPSG::n" - ogc urns
4026
 * <li> PROJ.4 definitions - passed on to importFromProj4().
4027
 * <li> filename - file read for WKT, XML or PROJ.4 definition.
4028
 * <li> well known name accepted by SetWellKnownGeogCS(), such as NAD27, NAD83,
4029
 * WGS84 or WGS72.
4030
 * <li> "IGNF:xxxx", "ESRI:xxxx", etc. from definitions from the PROJ database;
4031
 * <li> PROJJSON (PROJ &gt;= 6.2)
4032
 * </ol>
4033
 *
4034
 * It is expected that this method will be extended in the future to support
4035
 * XML and perhaps a simplified "minilanguage" for indicating common UTM and
4036
 * State Plane definitions.
4037
 *
4038
 * This method is intended to be flexible, but by its nature it is
4039
 * imprecise as it must guess information about the format intended.  When
4040
 * possible applications should call the specific method appropriate if the
4041
 * input is known to be in a particular format.
4042
 *
4043
 * This method does the same thing as the OSRSetFromUserInput() and
4044
 * OSRSetFromUserInputEx() functions.
4045
 *
4046
 * @param pszDefinition text definition to try to deduce SRS from.
4047
 *
4048
 * @param papszOptions NULL terminated list of options, or NULL.
4049
 * <ol>
4050
 * <li> ALLOW_NETWORK_ACCESS=YES/NO.
4051
 *      Whether http:// or https:// access is allowed. Defaults to YES.
4052
 * <li> ALLOW_FILE_ACCESS=YES/NO.
4053
 *      Whether reading a file using the Virtual File System layer is allowed
4054
 *      (can also involve network access). Defaults to YES.
4055
 * </ol>
4056
 *
4057
 * @return OGRERR_NONE on success, or an error code if the name isn't
4058
 * recognised, the definition is corrupt, or an EPSG value can't be
4059
 * successfully looked up.
4060
 */
4061
4062
OGRErr OGRSpatialReference::SetFromUserInput(const char *pszDefinition,
4063
                                             CSLConstList papszOptions)
4064
0
{
4065
0
    TAKE_OPTIONAL_LOCK();
4066
4067
    // Skip leading white space
4068
0
    while (isspace(static_cast<unsigned char>(*pszDefinition)))
4069
0
        pszDefinition++;
4070
4071
0
    if (STARTS_WITH_CI(pszDefinition, "ESRI::"))
4072
0
    {
4073
0
        pszDefinition += 6;
4074
0
    }
4075
4076
    /* -------------------------------------------------------------------- */
4077
    /*      Is it a recognised syntax?                                      */
4078
    /* -------------------------------------------------------------------- */
4079
0
    const char *const wktKeywords[] = {
4080
        // WKT1
4081
0
        "GEOGCS", "GEOCCS", "PROJCS", "VERT_CS", "COMPD_CS", "LOCAL_CS",
4082
        // WKT2"
4083
0
        "GEODCRS", "GEOGCRS", "GEODETICCRS", "GEOGRAPHICCRS", "PROJCRS",
4084
0
        "PROJECTEDCRS", "VERTCRS", "VERTICALCRS", "COMPOUNDCRS", "ENGCRS",
4085
0
        "ENGINEERINGCRS", "BOUNDCRS", "DERIVEDPROJCRS", "COORDINATEMETADATA"};
4086
0
    for (const char *keyword : wktKeywords)
4087
0
    {
4088
0
        if (STARTS_WITH_CI(pszDefinition, keyword))
4089
0
        {
4090
0
            return importFromWkt(pszDefinition);
4091
0
        }
4092
0
    }
4093
4094
0
    const bool bStartsWithEPSG = STARTS_WITH_CI(pszDefinition, "EPSG:");
4095
0
    if (bStartsWithEPSG || STARTS_WITH_CI(pszDefinition, "EPSGA:"))
4096
0
    {
4097
0
        OGRErr eStatus = OGRERR_NONE;
4098
4099
0
        if (strchr(pszDefinition, '+') || strchr(pszDefinition, '@'))
4100
0
        {
4101
            // Use proj_create() as it allows things like EPSG:3157+4617
4102
            // that are not normally supported by the below code that
4103
            // builds manually a compound CRS
4104
0
            PJ *pj = proj_create(d->getPROJContext(), pszDefinition);
4105
0
            if (!pj)
4106
0
            {
4107
0
                return OGRERR_FAILURE;
4108
0
            }
4109
0
            Clear();
4110
0
            d->setPjCRS(pj);
4111
0
            return OGRERR_NONE;
4112
0
        }
4113
0
        else
4114
0
        {
4115
0
            eStatus =
4116
0
                importFromEPSG(atoi(pszDefinition + (bStartsWithEPSG ? 5 : 6)));
4117
0
        }
4118
4119
0
        return eStatus;
4120
0
    }
4121
4122
0
    if (STARTS_WITH_CI(pszDefinition, "urn:ogc:def:crs:") ||
4123
0
        STARTS_WITH_CI(pszDefinition, "urn:ogc:def:crs,crs:") ||
4124
0
        STARTS_WITH_CI(pszDefinition, "urn:x-ogc:def:crs:") ||
4125
0
        STARTS_WITH_CI(pszDefinition, "urn:opengis:crs:") ||
4126
0
        STARTS_WITH_CI(pszDefinition, "urn:opengis:def:crs:") ||
4127
0
        STARTS_WITH_CI(pszDefinition, "urn:ogc:def:coordinateMetadata:"))
4128
0
        return importFromURN(pszDefinition);
4129
4130
0
    if (STARTS_WITH_CI(pszDefinition, "http://opengis.net/def/crs") ||
4131
0
        STARTS_WITH_CI(pszDefinition, "https://opengis.net/def/crs") ||
4132
0
        STARTS_WITH_CI(pszDefinition, "http://www.opengis.net/def/crs") ||
4133
0
        STARTS_WITH_CI(pszDefinition, "https://www.opengis.net/def/crs") ||
4134
0
        STARTS_WITH_CI(pszDefinition, "www.opengis.net/def/crs"))
4135
0
        return importFromCRSURL(pszDefinition);
4136
4137
0
    if (STARTS_WITH_CI(pszDefinition, "AUTO:"))
4138
0
        return importFromWMSAUTO(pszDefinition);
4139
4140
    // WMS/WCS OGC codes like OGC:CRS84.
4141
0
    if (EQUAL(pszDefinition, "OGC:CRS84"))
4142
0
        return SetWellKnownGeogCS(pszDefinition + 4);
4143
4144
0
    if (STARTS_WITH_CI(pszDefinition, "CRS:"))
4145
0
        return SetWellKnownGeogCS(pszDefinition);
4146
4147
0
    if (STARTS_WITH_CI(pszDefinition, "DICT:") && strstr(pszDefinition, ","))
4148
0
    {
4149
0
        char *pszFile = CPLStrdup(pszDefinition + 5);
4150
0
        char *pszCode = strstr(pszFile, ",") + 1;
4151
4152
0
        pszCode[-1] = '\0';
4153
4154
0
        OGRErr err = importFromDict(pszFile, pszCode);
4155
0
        CPLFree(pszFile);
4156
4157
0
        return err;
4158
0
    }
4159
4160
0
    if (EQUAL(pszDefinition, "NAD27") || EQUAL(pszDefinition, "NAD83") ||
4161
0
        EQUAL(pszDefinition, "WGS84") || EQUAL(pszDefinition, "WGS72"))
4162
0
    {
4163
0
        Clear();
4164
0
        return SetWellKnownGeogCS(pszDefinition);
4165
0
    }
4166
4167
    // PROJJSON
4168
0
    if (pszDefinition[0] == '{' && strstr(pszDefinition, "\"type\"") &&
4169
0
        (strstr(pszDefinition, "GeodeticCRS") ||
4170
0
         strstr(pszDefinition, "GeographicCRS") ||
4171
0
         strstr(pszDefinition, "ProjectedCRS") ||
4172
0
         strstr(pszDefinition, "VerticalCRS") ||
4173
0
         strstr(pszDefinition, "BoundCRS") ||
4174
0
         strstr(pszDefinition, "CompoundCRS") ||
4175
0
         strstr(pszDefinition, "DerivedGeodeticCRS") ||
4176
0
         strstr(pszDefinition, "DerivedGeographicCRS") ||
4177
0
         strstr(pszDefinition, "DerivedProjectedCRS") ||
4178
0
         strstr(pszDefinition, "DerivedVerticalCRS") ||
4179
0
         strstr(pszDefinition, "EngineeringCRS") ||
4180
0
         strstr(pszDefinition, "DerivedEngineeringCRS") ||
4181
0
         strstr(pszDefinition, "ParametricCRS") ||
4182
0
         strstr(pszDefinition, "DerivedParametricCRS") ||
4183
0
         strstr(pszDefinition, "TemporalCRS") ||
4184
0
         strstr(pszDefinition, "DerivedTemporalCRS")))
4185
0
    {
4186
0
        PJ *pj;
4187
0
        if (strstr(pszDefinition, "datum_ensemble") != nullptr)
4188
0
        {
4189
            // PROJ < 9.0.1 doesn't like a datum_ensemble whose member have
4190
            // a unknown id.
4191
0
            CPLJSONDocument oCRSDoc;
4192
0
            if (!oCRSDoc.LoadMemory(pszDefinition))
4193
0
                return OGRERR_CORRUPT_DATA;
4194
0
            CPLJSONObject oCRSRoot = oCRSDoc.GetRoot();
4195
0
            RemoveIDFromMemberOfEnsembles(oCRSRoot);
4196
0
            pj = proj_create(d->getPROJContext(), oCRSRoot.ToString().c_str());
4197
0
        }
4198
0
        else
4199
0
        {
4200
0
            pj = proj_create(d->getPROJContext(), pszDefinition);
4201
0
        }
4202
0
        if (!pj)
4203
0
        {
4204
0
            return OGRERR_FAILURE;
4205
0
        }
4206
0
        Clear();
4207
0
        d->setPjCRS(pj);
4208
0
        return OGRERR_NONE;
4209
0
    }
4210
4211
0
    if (strstr(pszDefinition, "+proj") != nullptr ||
4212
0
        strstr(pszDefinition, "+init") != nullptr)
4213
0
        return importFromProj4(pszDefinition);
4214
4215
0
    if (STARTS_WITH_CI(pszDefinition, "http://") ||
4216
0
        STARTS_WITH_CI(pszDefinition, "https://"))
4217
0
    {
4218
0
        if (CPLTestBool(CSLFetchNameValueDef(papszOptions,
4219
0
                                             "ALLOW_NETWORK_ACCESS", "YES")))
4220
0
            return importFromUrl(pszDefinition);
4221
4222
0
        CPLError(CE_Failure, CPLE_AppDefined,
4223
0
                 "Cannot import %s due to ALLOW_NETWORK_ACCESS=NO",
4224
0
                 pszDefinition);
4225
0
        return OGRERR_FAILURE;
4226
0
    }
4227
4228
0
    if (EQUAL(pszDefinition, "osgb:BNG"))
4229
0
    {
4230
0
        return importFromEPSG(27700);
4231
0
    }
4232
4233
    // Used by German CityGML files
4234
0
    if (EQUAL(pszDefinition, "urn:adv:crs:ETRS89_UTM32*DE_DHHN92_NH"))
4235
0
    {
4236
        // "ETRS89 / UTM Zone 32N + DHHN92 height"
4237
0
        return SetFromUserInput("EPSG:25832+5783");
4238
0
    }
4239
0
    else if (EQUAL(pszDefinition, "urn:adv:crs:ETRS89_UTM32*DE_DHHN2016_NH"))
4240
0
    {
4241
        // "ETRS89 / UTM Zone 32N + DHHN2016 height"
4242
0
        return SetFromUserInput("EPSG:25832+7837");
4243
0
    }
4244
4245
    // Used by  Japan's Fundamental Geospatial Data (FGD) GML
4246
0
    if (EQUAL(pszDefinition, "fguuid:jgd2001.bl"))
4247
0
        return importFromEPSG(4612);  // JGD2000 (slight difference in years)
4248
0
    else if (EQUAL(pszDefinition, "fguuid:jgd2011.bl"))
4249
0
        return importFromEPSG(6668);  // JGD2011
4250
0
    else if (EQUAL(pszDefinition, "fguuid:jgd2024.bl"))
4251
0
    {
4252
        // FIXME when EPSG attributes a CRS code
4253
0
        return importFromWkt(
4254
0
            "GEOGCRS[\"JGD2024\",\n"
4255
0
            "    DATUM[\"Japanese Geodetic Datum 2024\",\n"
4256
0
            "        ELLIPSOID[\"GRS 1980\",6378137,298.257222101,\n"
4257
0
            "            LENGTHUNIT[\"metre\",1]]],\n"
4258
0
            "    PRIMEM[\"Greenwich\",0,\n"
4259
0
            "        ANGLEUNIT[\"degree\",0.0174532925199433]],\n"
4260
0
            "    CS[ellipsoidal,2],\n"
4261
0
            "        AXIS[\"geodetic latitude (Lat)\",north,\n"
4262
0
            "            ORDER[1],\n"
4263
0
            "            ANGLEUNIT[\"degree\",0.0174532925199433]],\n"
4264
0
            "        AXIS[\"geodetic longitude (Lon)\",east,\n"
4265
0
            "            ORDER[2],\n"
4266
0
            "            ANGLEUNIT[\"degree\",0.0174532925199433]],\n"
4267
0
            "    USAGE[\n"
4268
0
            "        SCOPE[\"Horizontal component of 3D system.\"],\n"
4269
0
            "        AREA[\"Japan - onshore and offshore.\"],\n"
4270
0
            "        BBOX[17.09,122.38,46.05,157.65]]]");
4271
0
    }
4272
4273
    // Deal with IGNF:xxx, ESRI:xxx, etc from the PROJ database
4274
0
    const char *pszDot = strrchr(pszDefinition, ':');
4275
0
    if (pszDot)
4276
0
    {
4277
0
        CPLString osPrefix(pszDefinition, pszDot - pszDefinition);
4278
0
        auto authorities =
4279
0
            proj_get_authorities_from_database(d->getPROJContext());
4280
0
        if (authorities)
4281
0
        {
4282
0
            std::set<std::string> aosCandidateAuthorities;
4283
0
            for (auto iter = authorities; *iter; ++iter)
4284
0
            {
4285
0
                if (*iter == osPrefix)
4286
0
                {
4287
0
                    aosCandidateAuthorities.clear();
4288
0
                    aosCandidateAuthorities.insert(*iter);
4289
0
                    break;
4290
0
                }
4291
                // Deal with "IAU_2015" as authority in the list and input
4292
                // "IAU:code"
4293
0
                else if (strncmp(*iter, osPrefix.c_str(), osPrefix.size()) ==
4294
0
                             0 &&
4295
0
                         (*iter)[osPrefix.size()] == '_')
4296
0
                {
4297
0
                    aosCandidateAuthorities.insert(*iter);
4298
0
                }
4299
                // Deal with "IAU_2015" as authority in the list and input
4300
                // "IAU:2015:code"
4301
0
                else if (osPrefix.find(':') != std::string::npos &&
4302
0
                         osPrefix.size() == strlen(*iter) &&
4303
0
                         CPLString(osPrefix).replaceAll(':', '_') == *iter)
4304
0
                {
4305
0
                    aosCandidateAuthorities.clear();
4306
0
                    aosCandidateAuthorities.insert(*iter);
4307
0
                    break;
4308
0
                }
4309
0
            }
4310
4311
0
            proj_string_list_destroy(authorities);
4312
4313
0
            if (!aosCandidateAuthorities.empty())
4314
0
            {
4315
0
                auto obj = proj_create_from_database(
4316
0
                    d->getPROJContext(),
4317
0
                    aosCandidateAuthorities.rbegin()->c_str(), pszDot + 1,
4318
0
                    PJ_CATEGORY_CRS, false, nullptr);
4319
0
                if (!obj)
4320
0
                {
4321
0
                    return OGRERR_FAILURE;
4322
0
                }
4323
0
                Clear();
4324
0
                d->setPjCRS(obj);
4325
0
                return OGRERR_NONE;
4326
0
            }
4327
0
        }
4328
0
    }
4329
4330
    /* -------------------------------------------------------------------- */
4331
    /*      Try to open it as a file.                                       */
4332
    /* -------------------------------------------------------------------- */
4333
0
    if (!CPLTestBool(
4334
0
            CSLFetchNameValueDef(papszOptions, "ALLOW_FILE_ACCESS", "YES")))
4335
0
    {
4336
0
        VSIStatBufL sStat;
4337
0
        if (STARTS_WITH(pszDefinition, "/vsi") ||
4338
0
            VSIStatExL(pszDefinition, &sStat, VSI_STAT_EXISTS_FLAG) == 0)
4339
0
        {
4340
0
            CPLError(CE_Failure, CPLE_AppDefined,
4341
0
                     "Cannot import %s due to ALLOW_FILE_ACCESS=NO",
4342
0
                     pszDefinition);
4343
0
            return OGRERR_FAILURE;
4344
0
        }
4345
        // We used to silently return an error without a CE_Failure message
4346
        // Cf https://github.com/Toblerity/Fiona/issues/1063
4347
0
        return OGRERR_CORRUPT_DATA;
4348
0
    }
4349
4350
0
    CPLConfigOptionSetter oSetter("CPL_ALLOW_VSISTDIN", "NO", true);
4351
0
    VSILFILE *const fp = VSIFOpenL(pszDefinition, "rt");
4352
0
    if (fp == nullptr)
4353
0
        return OGRERR_CORRUPT_DATA;
4354
4355
0
    const size_t nBufMax = 100000;
4356
0
    char *const pszBuffer = static_cast<char *>(CPLMalloc(nBufMax));
4357
0
    const size_t nBytes = VSIFReadL(pszBuffer, 1, nBufMax - 1, fp);
4358
0
    VSIFCloseL(fp);
4359
4360
0
    if (nBytes == nBufMax - 1)
4361
0
    {
4362
0
        CPLDebug("OGR",
4363
0
                 "OGRSpatialReference::SetFromUserInput(%s), opened file "
4364
0
                 "but it is to large for our generous buffer.  Is it really "
4365
0
                 "just a WKT definition?",
4366
0
                 pszDefinition);
4367
0
        CPLFree(pszBuffer);
4368
0
        return OGRERR_FAILURE;
4369
0
    }
4370
4371
0
    pszBuffer[nBytes] = '\0';
4372
4373
0
    char *pszBufPtr = pszBuffer;
4374
0
    while (pszBufPtr[0] == ' ' || pszBufPtr[0] == '\n')
4375
0
        pszBufPtr++;
4376
4377
0
    OGRErr err = OGRERR_NONE;
4378
0
    if (pszBufPtr[0] == '<')
4379
0
        err = importFromXML(pszBufPtr);
4380
0
    else if ((strstr(pszBuffer, "+proj") != nullptr ||
4381
0
              strstr(pszBuffer, "+init") != nullptr) &&
4382
0
             (strstr(pszBuffer, "EXTENSION") == nullptr &&
4383
0
              strstr(pszBuffer, "extension") == nullptr))
4384
0
        err = importFromProj4(pszBufPtr);
4385
0
    else
4386
0
    {
4387
0
        if (STARTS_WITH_CI(pszBufPtr, "ESRI::"))
4388
0
        {
4389
0
            pszBufPtr += 6;
4390
0
        }
4391
4392
        // coverity[tainted_data]
4393
0
        err = importFromWkt(pszBufPtr);
4394
0
    }
4395
4396
0
    CPLFree(pszBuffer);
4397
4398
0
    return err;
4399
0
}
4400
4401
/************************************************************************/
4402
/*                        OSRSetFromUserInput()                         */
4403
/************************************************************************/
4404
4405
/**
4406
 * \brief Set spatial reference from various text formats.
4407
 *
4408
 * This function is the same as OGRSpatialReference::SetFromUserInput()
4409
 *
4410
 * \see OSRSetFromUserInputEx() for a variant allowing to pass options.
4411
 */
4412
OGRErr CPL_STDCALL OSRSetFromUserInput(OGRSpatialReferenceH hSRS,
4413
                                       const char *pszDef)
4414
4415
0
{
4416
0
    VALIDATE_POINTER1(hSRS, "OSRSetFromUserInput", OGRERR_FAILURE);
4417
4418
0
    return ToPointer(hSRS)->SetFromUserInput(pszDef);
4419
0
}
4420
4421
/************************************************************************/
4422
/*                       OSRSetFromUserInputEx()                        */
4423
/************************************************************************/
4424
4425
/**
4426
 * \brief Set spatial reference from various text formats.
4427
 *
4428
 * This function is the same as OGRSpatialReference::SetFromUserInput().
4429
 *
4430
 * @since GDAL 3.9
4431
 */
4432
OGRErr OSRSetFromUserInputEx(OGRSpatialReferenceH hSRS, const char *pszDef,
4433
                             CSLConstList papszOptions)
4434
4435
0
{
4436
0
    VALIDATE_POINTER1(hSRS, "OSRSetFromUserInputEx", OGRERR_FAILURE);
4437
4438
0
    return ToPointer(hSRS)->SetFromUserInput(pszDef, papszOptions);
4439
0
}
4440
4441
/************************************************************************/
4442
/*                          ImportFromUrl()                             */
4443
/************************************************************************/
4444
4445
/**
4446
 * \brief Set spatial reference from a URL.
4447
 *
4448
 * This method will download the spatial reference at a given URL and
4449
 * feed it into SetFromUserInput for you.
4450
 *
4451
 * This method does the same thing as the OSRImportFromUrl() function.
4452
 *
4453
 * @param pszUrl text definition to try to deduce SRS from.
4454
 *
4455
 * @return OGRERR_NONE on success, or an error code with the curl
4456
 * error message if it is unable to download data.
4457
 */
4458
4459
OGRErr OGRSpatialReference::importFromUrl(const char *pszUrl)
4460
4461
0
{
4462
0
    TAKE_OPTIONAL_LOCK();
4463
4464
0
    if (!STARTS_WITH_CI(pszUrl, "http://") &&
4465
0
        !STARTS_WITH_CI(pszUrl, "https://"))
4466
0
    {
4467
0
        CPLError(CE_Failure, CPLE_AppDefined,
4468
0
                 "The given string is not recognized as a URL"
4469
0
                 "starting with 'http://' -- %s",
4470
0
                 pszUrl);
4471
0
        return OGRERR_FAILURE;
4472
0
    }
4473
4474
    /* -------------------------------------------------------------------- */
4475
    /*      Fetch the result.                                               */
4476
    /* -------------------------------------------------------------------- */
4477
0
    CPLErrorReset();
4478
4479
0
    std::string osUrl(pszUrl);
4480
    // We have historically supported "http://spatialreference.org/ref/AUTHNAME/CODE/"
4481
    // as a valid URL since we used a "Accept: application/x-ogcwkt" header
4482
    // to query WKT. To allow a static server to be used, rather append a
4483
    // "ogcwkt/" suffix.
4484
0
    for (const char *pszPrefix : {"https://spatialreference.org/ref/",
4485
0
                                  "http://spatialreference.org/ref/"})
4486
0
    {
4487
0
        if (STARTS_WITH(pszUrl, pszPrefix))
4488
0
        {
4489
0
            const CPLStringList aosTokens(
4490
0
                CSLTokenizeString2(pszUrl + strlen(pszPrefix), "/", 0));
4491
0
            if (aosTokens.size() == 2)
4492
0
            {
4493
0
                osUrl = "https://spatialreference.org/ref/";
4494
0
                osUrl += aosTokens[0];  // authority
4495
0
                osUrl += '/';
4496
0
                osUrl += aosTokens[1];  // code
4497
0
                osUrl += "/ogcwkt/";
4498
0
            }
4499
0
            break;
4500
0
        }
4501
0
    }
4502
4503
0
    const char *pszTimeout = "TIMEOUT=10";
4504
0
    char *apszOptions[] = {const_cast<char *>(pszTimeout), nullptr};
4505
4506
0
    CPLHTTPResult *psResult = CPLHTTPFetch(osUrl.c_str(), apszOptions);
4507
4508
    /* -------------------------------------------------------------------- */
4509
    /*      Try to handle errors.                                           */
4510
    /* -------------------------------------------------------------------- */
4511
4512
0
    if (psResult == nullptr)
4513
0
        return OGRERR_FAILURE;
4514
0
    if (psResult->nDataLen == 0 || CPLGetLastErrorNo() != 0 ||
4515
0
        psResult->pabyData == nullptr)
4516
0
    {
4517
0
        if (CPLGetLastErrorNo() == 0)
4518
0
        {
4519
0
            CPLError(CE_Failure, CPLE_AppDefined,
4520
0
                     "No data was returned from the given URL");
4521
0
        }
4522
0
        CPLHTTPDestroyResult(psResult);
4523
0
        return OGRERR_FAILURE;
4524
0
    }
4525
4526
0
    if (psResult->nStatus != 0)
4527
0
    {
4528
0
        CPLError(CE_Failure, CPLE_AppDefined, "Curl reports error: %d: %s",
4529
0
                 psResult->nStatus, psResult->pszErrBuf);
4530
0
        CPLHTTPDestroyResult(psResult);
4531
0
        return OGRERR_FAILURE;
4532
0
    }
4533
4534
0
    const char *pszData = reinterpret_cast<const char *>(psResult->pabyData);
4535
0
    if (STARTS_WITH_CI(pszData, "http://") ||
4536
0
        STARTS_WITH_CI(pszData, "https://"))
4537
0
    {
4538
0
        CPLError(CE_Failure, CPLE_AppDefined,
4539
0
                 "The data that was downloaded also starts with 'http://' "
4540
0
                 "and cannot be passed into SetFromUserInput.  Is this "
4541
0
                 "really a spatial reference definition? ");
4542
0
        CPLHTTPDestroyResult(psResult);
4543
0
        return OGRERR_FAILURE;
4544
0
    }
4545
0
    if (OGRERR_NONE != SetFromUserInput(pszData))
4546
0
    {
4547
0
        CPLHTTPDestroyResult(psResult);
4548
0
        return OGRERR_FAILURE;
4549
0
    }
4550
4551
0
    CPLHTTPDestroyResult(psResult);
4552
0
    return OGRERR_NONE;
4553
0
}
4554
4555
/************************************************************************/
4556
/*                        OSRimportFromUrl()                            */
4557
/************************************************************************/
4558
4559
/**
4560
 * \brief Set spatial reference from a URL.
4561
 *
4562
 * This function is the same as OGRSpatialReference::importFromUrl()
4563
 */
4564
OGRErr OSRImportFromUrl(OGRSpatialReferenceH hSRS, const char *pszUrl)
4565
4566
0
{
4567
0
    VALIDATE_POINTER1(hSRS, "OSRImportFromUrl", OGRERR_FAILURE);
4568
4569
0
    return ToPointer(hSRS)->importFromUrl(pszUrl);
4570
0
}
4571
4572
/************************************************************************/
4573
/*                         importFromURNPart()                          */
4574
/************************************************************************/
4575
OGRErr OGRSpatialReference::importFromURNPart(const char *pszAuthority,
4576
                                              const char *pszCode,
4577
                                              const char *pszURN)
4578
0
{
4579
0
#if PROJ_AT_LEAST_VERSION(8, 1, 0)
4580
0
    (void)this;
4581
0
    (void)pszAuthority;
4582
0
    (void)pszCode;
4583
0
    (void)pszURN;
4584
0
    return OGRERR_FAILURE;
4585
#else
4586
    /* -------------------------------------------------------------------- */
4587
    /*      Is this an EPSG code? Note that we import it with EPSG          */
4588
    /*      preferred axis ordering for geographic coordinate systems.      */
4589
    /* -------------------------------------------------------------------- */
4590
    if (STARTS_WITH_CI(pszAuthority, "EPSG"))
4591
        return importFromEPSGA(atoi(pszCode));
4592
4593
    /* -------------------------------------------------------------------- */
4594
    /*      Is this an IAU code?  Lets try for the IAU2000 dictionary.      */
4595
    /* -------------------------------------------------------------------- */
4596
    if (STARTS_WITH_CI(pszAuthority, "IAU"))
4597
        return importFromDict("IAU2000.wkt", pszCode);
4598
4599
    /* -------------------------------------------------------------------- */
4600
    /*      Is this an OGC code?                                            */
4601
    /* -------------------------------------------------------------------- */
4602
    if (!STARTS_WITH_CI(pszAuthority, "OGC"))
4603
    {
4604
        CPLError(CE_Failure, CPLE_AppDefined,
4605
                 "URN %s has unrecognized authority.", pszURN);
4606
        return OGRERR_FAILURE;
4607
    }
4608
4609
    if (STARTS_WITH_CI(pszCode, "CRS84"))
4610
        return SetWellKnownGeogCS(pszCode);
4611
    else if (STARTS_WITH_CI(pszCode, "CRS83"))
4612
        return SetWellKnownGeogCS(pszCode);
4613
    else if (STARTS_WITH_CI(pszCode, "CRS27"))
4614
        return SetWellKnownGeogCS(pszCode);
4615
    else if (STARTS_WITH_CI(pszCode, "84"))  // urn:ogc:def:crs:OGC:2:84
4616
        return SetWellKnownGeogCS("CRS84");
4617
4618
    /* -------------------------------------------------------------------- */
4619
    /*      Handle auto codes.  We need to convert from format              */
4620
    /*      AUTO42001:99:8888 to format AUTO:42001,99,8888.                 */
4621
    /* -------------------------------------------------------------------- */
4622
    else if (STARTS_WITH_CI(pszCode, "AUTO"))
4623
    {
4624
        char szWMSAuto[100] = {'\0'};
4625
4626
        if (strlen(pszCode) > sizeof(szWMSAuto) - 2)
4627
            return OGRERR_FAILURE;
4628
4629
        snprintf(szWMSAuto, sizeof(szWMSAuto), "AUTO:%s", pszCode + 4);
4630
        for (int i = 5; szWMSAuto[i] != '\0'; i++)
4631
        {
4632
            if (szWMSAuto[i] == ':')
4633
                szWMSAuto[i] = ',';
4634
        }
4635
4636
        return importFromWMSAUTO(szWMSAuto);
4637
    }
4638
4639
    /* -------------------------------------------------------------------- */
4640
    /*      Not a recognise OGC item.                                       */
4641
    /* -------------------------------------------------------------------- */
4642
    CPLError(CE_Failure, CPLE_AppDefined, "URN %s value not supported.",
4643
             pszURN);
4644
4645
    return OGRERR_FAILURE;
4646
#endif
4647
0
}
4648
4649
/************************************************************************/
4650
/*                           importFromURN()                            */
4651
/*                                                                      */
4652
/*      See OGC recommendation paper 06-023r1 or later for details.     */
4653
/************************************************************************/
4654
4655
/**
4656
 * \brief Initialize from OGC URN.
4657
 *
4658
 * Initializes this spatial reference from a coordinate system defined
4659
 * by an OGC URN prefixed with "urn:ogc:def:crs:" per recommendation
4660
 * paper 06-023r1.  Currently EPSG and OGC authority values are supported,
4661
 * including OGC auto codes, but not including CRS1 or CRS88 (NAVD88).
4662
 *
4663
 * This method is also support through SetFromUserInput() which can
4664
 * normally be used for URNs.
4665
 *
4666
 * @param pszURN the urn string.
4667
 *
4668
 * @return OGRERR_NONE on success or an error code.
4669
 */
4670
4671
OGRErr OGRSpatialReference::importFromURN(const char *pszURN)
4672
4673
0
{
4674
0
    constexpr const char *EPSG_URN_CRS_PREFIX = "urn:ogc:def:crs:EPSG::";
4675
0
    if (STARTS_WITH(pszURN, EPSG_URN_CRS_PREFIX) &&
4676
0
        CPLGetValueType(pszURN + strlen(EPSG_URN_CRS_PREFIX)) ==
4677
0
            CPL_VALUE_INTEGER)
4678
0
    {
4679
0
        return importFromEPSG(atoi(pszURN + strlen(EPSG_URN_CRS_PREFIX)));
4680
0
    }
4681
4682
0
    TAKE_OPTIONAL_LOCK();
4683
4684
0
#if PROJ_AT_LEAST_VERSION(8, 1, 0)
4685
4686
    // PROJ 8.2.0 has support for IAU codes now.
4687
#if !PROJ_AT_LEAST_VERSION(8, 2, 0)
4688
    /* -------------------------------------------------------------------- */
4689
    /*      Is this an IAU code?  Lets try for the IAU2000 dictionary.      */
4690
    /* -------------------------------------------------------------------- */
4691
    const char *pszIAU = strstr(pszURN, "IAU");
4692
    if (pszIAU)
4693
    {
4694
        const char *pszCode = strchr(pszIAU, ':');
4695
        if (pszCode)
4696
        {
4697
            ++pszCode;
4698
            if (*pszCode == ':')
4699
                ++pszCode;
4700
            return importFromDict("IAU2000.wkt", pszCode);
4701
        }
4702
    }
4703
#endif
4704
4705
0
    if (strlen(pszURN) >= 1000)
4706
0
    {
4707
0
        CPLError(CE_Failure, CPLE_AppDefined, "Too long input string");
4708
0
        return OGRERR_CORRUPT_DATA;
4709
0
    }
4710
0
    auto obj = proj_create(d->getPROJContext(), pszURN);
4711
0
    if (!obj)
4712
0
    {
4713
0
        return OGRERR_FAILURE;
4714
0
    }
4715
0
    Clear();
4716
0
    d->setPjCRS(obj);
4717
0
    return OGRERR_NONE;
4718
#else
4719
    const char *pszCur = nullptr;
4720
4721
    if (STARTS_WITH_CI(pszURN, "urn:ogc:def:crs:"))
4722
        pszCur = pszURN + 16;
4723
    else if (STARTS_WITH_CI(pszURN, "urn:ogc:def:crs,crs:"))
4724
        pszCur = pszURN + 20;
4725
    else if (STARTS_WITH_CI(pszURN, "urn:x-ogc:def:crs:"))
4726
        pszCur = pszURN + 18;
4727
    else if (STARTS_WITH_CI(pszURN, "urn:opengis:crs:"))
4728
        pszCur = pszURN + 16;
4729
    else if (STARTS_WITH_CI(pszURN, "urn:opengis:def:crs:"))
4730
        pszCur = pszURN + 20;
4731
    else
4732
    {
4733
        CPLError(CE_Failure, CPLE_AppDefined, "URN %s not a supported format.",
4734
                 pszURN);
4735
        return OGRERR_FAILURE;
4736
    }
4737
4738
    /* -------------------------------------------------------------------- */
4739
    /*      Clear any existing definition.                                  */
4740
    /* -------------------------------------------------------------------- */
4741
    Clear();
4742
4743
    /* -------------------------------------------------------------------- */
4744
    /*      Find code (ignoring version) out of string like:                */
4745
    /*                                                                      */
4746
    /*      authority:[version]:code                                        */
4747
    /* -------------------------------------------------------------------- */
4748
    const char *pszAuthority = pszCur;
4749
4750
    // skip authority
4751
    while (*pszCur != ':' && *pszCur)
4752
        pszCur++;
4753
    if (*pszCur == ':')
4754
        pszCur++;
4755
4756
    // skip version
4757
    const char *pszBeforeVersion = pszCur;
4758
    while (*pszCur != ':' && *pszCur)
4759
        pszCur++;
4760
    if (*pszCur == ':')
4761
        pszCur++;
4762
    else
4763
        // We come here in the case, the content to parse is authority:code
4764
        // (instead of authority::code) which is probably illegal according to
4765
        // http://www.opengeospatial.org/ogcUrnPolicy but such content is found
4766
        // for example in what is returned by GeoServer.
4767
        pszCur = pszBeforeVersion;
4768
4769
    const char *pszCode = pszCur;
4770
4771
    const char *pszComma = strchr(pszCur, ',');
4772
    if (pszComma == nullptr)
4773
        return importFromURNPart(pszAuthority, pszCode, pszURN);
4774
4775
    // There's a second part with the vertical SRS.
4776
    pszCur = pszComma + 1;
4777
    if (!STARTS_WITH(pszCur, "crs:"))
4778
    {
4779
        CPLError(CE_Failure, CPLE_AppDefined, "URN %s not a supported format.",
4780
                 pszURN);
4781
        return OGRERR_FAILURE;
4782
    }
4783
4784
    pszCur += 4;
4785
4786
    char *pszFirstCode = CPLStrdup(pszCode);
4787
    pszFirstCode[pszComma - pszCode] = '\0';
4788
    OGRErr eStatus = importFromURNPart(pszAuthority, pszFirstCode, pszURN);
4789
    CPLFree(pszFirstCode);
4790
4791
    // Do we want to turn this into a compound definition
4792
    // with a vertical datum?
4793
    if (eStatus != OGRERR_NONE)
4794
        return eStatus;
4795
4796
    /* -------------------------------------------------------------------- */
4797
    /*      Find code (ignoring version) out of string like:                */
4798
    /*                                                                      */
4799
    /*      authority:[version]:code                                        */
4800
    /* -------------------------------------------------------------------- */
4801
    pszAuthority = pszCur;
4802
4803
    // skip authority
4804
    while (*pszCur != ':' && *pszCur)
4805
        pszCur++;
4806
    if (*pszCur == ':')
4807
        pszCur++;
4808
4809
    // skip version
4810
    pszBeforeVersion = pszCur;
4811
    while (*pszCur != ':' && *pszCur)
4812
        pszCur++;
4813
    if (*pszCur == ':')
4814
        pszCur++;
4815
    else
4816
        pszCur = pszBeforeVersion;
4817
4818
    pszCode = pszCur;
4819
4820
    OGRSpatialReference oVertSRS;
4821
    eStatus = oVertSRS.importFromURNPart(pszAuthority, pszCode, pszURN);
4822
    if (eStatus == OGRERR_NONE)
4823
    {
4824
        OGRSpatialReference oHorizSRS(*this);
4825
4826
        Clear();
4827
4828
        oHorizSRS.d->refreshProjObj();
4829
        oVertSRS.d->refreshProjObj();
4830
        if (!oHorizSRS.d->m_pj_crs || !oVertSRS.d->m_pj_crs)
4831
            return OGRERR_FAILURE;
4832
4833
        const char *pszHorizName = proj_get_name(oHorizSRS.d->m_pj_crs);
4834
        const char *pszVertName = proj_get_name(oVertSRS.d->m_pj_crs);
4835
4836
        CPLString osName = pszHorizName ? pszHorizName : "";
4837
        osName += " + ";
4838
        osName += pszVertName ? pszVertName : "";
4839
4840
        SetCompoundCS(osName, &oHorizSRS, &oVertSRS);
4841
    }
4842
4843
    return eStatus;
4844
#endif
4845
0
}
4846
4847
/************************************************************************/
4848
/*                           importFromCRSURL()                         */
4849
/*                                                                      */
4850
/*      See OGC Best Practice document 11-135 for details.              */
4851
/************************************************************************/
4852
4853
/**
4854
 * \brief Initialize from OGC URL.
4855
 *
4856
 * Initializes this spatial reference from a coordinate system defined
4857
 * by an OGC URL prefixed with "http://opengis.net/def/crs" per best practice
4858
 * paper 11-135.  Currently EPSG and OGC authority values are supported,
4859
 * including OGC auto codes, but not including CRS1 or CRS88 (NAVD88).
4860
 *
4861
 * This method is also supported through SetFromUserInput() which can
4862
 * normally be used for URLs.
4863
 *
4864
 * @param pszURL the URL string.
4865
 *
4866
 * @return OGRERR_NONE on success or an error code.
4867
 */
4868
4869
OGRErr OGRSpatialReference::importFromCRSURL(const char *pszURL)
4870
4871
0
{
4872
0
    TAKE_OPTIONAL_LOCK();
4873
4874
#if !PROJ_AT_LEAST_VERSION(9, 1, 0)
4875
    if (strcmp(pszURL, "http://www.opengis.net/def/crs/OGC/0/CRS84h") == 0)
4876
    {
4877
        PJ *obj = proj_create(
4878
            d->getPROJContext(),
4879
            "GEOGCRS[\"WGS 84 longitude-latitude-height\",\n"
4880
            "    ENSEMBLE[\"World Geodetic System 1984 ensemble\",\n"
4881
            "        MEMBER[\"World Geodetic System 1984 (Transit)\"],\n"
4882
            "        MEMBER[\"World Geodetic System 1984 (G730)\"],\n"
4883
            "        MEMBER[\"World Geodetic System 1984 (G873)\"],\n"
4884
            "        MEMBER[\"World Geodetic System 1984 (G1150)\"],\n"
4885
            "        MEMBER[\"World Geodetic System 1984 (G1674)\"],\n"
4886
            "        MEMBER[\"World Geodetic System 1984 (G1762)\"],\n"
4887
            "        MEMBER[\"World Geodetic System 1984 (G2139)\"],\n"
4888
            "        MEMBER[\"World Geodetic System 1984 (G2296)\"],\n"
4889
            "        ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n"
4890
            "            LENGTHUNIT[\"metre\",1]],\n"
4891
            "        ENSEMBLEACCURACY[2.0]],\n"
4892
            "    PRIMEM[\"Greenwich\",0,\n"
4893
            "        ANGLEUNIT[\"degree\",0.0174532925199433]],\n"
4894
            "    CS[ellipsoidal,3],\n"
4895
            "        AXIS[\"geodetic longitude (Lon)\",east,\n"
4896
            "            ORDER[1],\n"
4897
            "            ANGLEUNIT[\"degree\",0.0174532925199433]],\n"
4898
            "        AXIS[\"geodetic latitude (Lat)\",north,\n"
4899
            "            ORDER[2],\n"
4900
            "            ANGLEUNIT[\"degree\",0.0174532925199433]],\n"
4901
            "        AXIS[\"ellipsoidal height (h)\",up,\n"
4902
            "            ORDER[3],\n"
4903
            "            LENGTHUNIT[\"metre\",1]],\n"
4904
            "    USAGE[\n"
4905
            "        SCOPE[\"3D system frequently used in GIS, Web APIs and "
4906
            "Web applications\"],\n"
4907
            "        AREA[\"World.\"],\n"
4908
            "        BBOX[-90,-180,90,180]],\n"
4909
            "    ID[\"OGC\",\"CRS84h\"]]");
4910
        if (!obj)
4911
        {
4912
            return OGRERR_FAILURE;
4913
        }
4914
        Clear();
4915
        d->setPjCRS(obj);
4916
        return OGRERR_NONE;
4917
    }
4918
#endif
4919
4920
0
#if PROJ_AT_LEAST_VERSION(8, 1, 0)
4921
0
    if (strlen(pszURL) >= 10000)
4922
0
    {
4923
0
        CPLError(CE_Failure, CPLE_AppDefined, "Too long input string");
4924
0
        return OGRERR_CORRUPT_DATA;
4925
0
    }
4926
4927
0
    PJ *obj;
4928
#if !PROJ_AT_LEAST_VERSION(9, 2, 0)
4929
    if (STARTS_WITH(pszURL, "http://www.opengis.net/def/crs/IAU/0/"))
4930
    {
4931
        obj = proj_create(
4932
            d->getPROJContext(),
4933
            CPLSPrintf("IAU:%s",
4934
                       pszURL +
4935
                           strlen("http://www.opengis.net/def/crs/IAU/0/")));
4936
    }
4937
    else
4938
#endif
4939
0
    {
4940
0
        obj = proj_create(d->getPROJContext(), pszURL);
4941
0
    }
4942
0
    if (!obj)
4943
0
    {
4944
0
        return OGRERR_FAILURE;
4945
0
    }
4946
0
    Clear();
4947
0
    d->setPjCRS(obj);
4948
0
    return OGRERR_NONE;
4949
#else
4950
    const char *pszCur = nullptr;
4951
4952
    if (STARTS_WITH_CI(pszURL, "http://opengis.net/def/crs"))
4953
        pszCur = pszURL + 26;
4954
    else if (STARTS_WITH_CI(pszURL, "https://opengis.net/def/crs"))
4955
        pszCur = pszURL + 27;
4956
    else if (STARTS_WITH_CI(pszURL, "http://www.opengis.net/def/crs"))
4957
        pszCur = pszURL + 30;
4958
    else if (STARTS_WITH_CI(pszURL, "https://www.opengis.net/def/crs"))
4959
        pszCur = pszURL + 31;
4960
    else if (STARTS_WITH_CI(pszURL, "www.opengis.net/def/crs"))
4961
        pszCur = pszURL + 23;
4962
    else
4963
    {
4964
        CPLError(CE_Failure, CPLE_AppDefined, "URL %s not a supported format.",
4965
                 pszURL);
4966
        return OGRERR_FAILURE;
4967
    }
4968
4969
    if (*pszCur == '\0')
4970
    {
4971
        CPLError(CE_Failure, CPLE_AppDefined, "URL %s malformed.", pszURL);
4972
        return OGRERR_FAILURE;
4973
    }
4974
4975
    /* -------------------------------------------------------------------- */
4976
    /*      Clear any existing definition.                                  */
4977
    /* -------------------------------------------------------------------- */
4978
    Clear();
4979
4980
    if (STARTS_WITH_CI(pszCur, "-compound?1="))
4981
    {
4982
        /* --------------------------------------------------------------------
4983
         */
4984
        /*      It's a compound CRS, of the form: */
4985
        /*                                                                      */
4986
        /*      http://opengis.net/def/crs-compound?1=URL1&2=URL2&3=URL3&.. */
4987
        /* --------------------------------------------------------------------
4988
         */
4989
        pszCur += 12;
4990
4991
        // Extract each component CRS URL.
4992
        int iComponentUrl = 2;
4993
4994
        CPLString osName = "";
4995
        Clear();
4996
4997
        while (iComponentUrl != -1)
4998
        {
4999
            char searchStr[15] = {};
5000
            snprintf(searchStr, sizeof(searchStr), "&%d=", iComponentUrl);
5001
5002
            const char *pszUrlEnd = strstr(pszCur, searchStr);
5003
5004
            // Figure out the next component URL.
5005
            char *pszComponentUrl = nullptr;
5006
5007
            if (pszUrlEnd)
5008
            {
5009
                size_t nLen = pszUrlEnd - pszCur;
5010
                pszComponentUrl = static_cast<char *>(CPLMalloc(nLen + 1));
5011
                strncpy(pszComponentUrl, pszCur, nLen);
5012
                pszComponentUrl[nLen] = '\0';
5013
5014
                ++iComponentUrl;
5015
                pszCur += nLen + strlen(searchStr);
5016
            }
5017
            else
5018
            {
5019
                if (iComponentUrl == 2)
5020
                {
5021
                    CPLError(CE_Failure, CPLE_AppDefined,
5022
                             "Compound CRS URLs must have at least two "
5023
                             "component CRSs.");
5024
                    return OGRERR_FAILURE;
5025
                }
5026
                else
5027
                {
5028
                    pszComponentUrl = CPLStrdup(pszCur);
5029
                    // no more components
5030
                    iComponentUrl = -1;
5031
                }
5032
            }
5033
5034
            OGRSpatialReference oComponentSRS;
5035
            OGRErr eStatus = oComponentSRS.importFromCRSURL(pszComponentUrl);
5036
5037
            CPLFree(pszComponentUrl);
5038
            pszComponentUrl = nullptr;
5039
5040
            if (eStatus == OGRERR_NONE)
5041
            {
5042
                if (osName.length() != 0)
5043
                {
5044
                    osName += " + ";
5045
                }
5046
                osName += oComponentSRS.GetRoot()->GetValue();
5047
                SetNode("COMPD_CS", osName);
5048
                GetRoot()->AddChild(oComponentSRS.GetRoot()->Clone());
5049
            }
5050
            else
5051
                return eStatus;
5052
        }
5053
5054
        return OGRERR_NONE;
5055
    }
5056
5057
    /* -------------------------------------------------------------------- */
5058
    /*      It's a normal CRS URL, of the form:                             */
5059
    /*                                                                      */
5060
    /*      http://opengis.net/def/crs/AUTHORITY/VERSION/CODE               */
5061
    /* -------------------------------------------------------------------- */
5062
    ++pszCur;
5063
    const char *pszAuthority = pszCur;
5064
5065
    // skip authority
5066
    while (*pszCur != '/' && *pszCur)
5067
        pszCur++;
5068
    if (*pszCur == '/')
5069
        pszCur++;
5070
5071
    // skip version
5072
    while (*pszCur != '/' && *pszCur)
5073
        pszCur++;
5074
    if (*pszCur == '/')
5075
        pszCur++;
5076
5077
    const char *pszCode = pszCur;
5078
5079
    return importFromURNPart(pszAuthority, pszCode, pszURL);
5080
#endif
5081
0
}
5082
5083
/************************************************************************/
5084
/*                         importFromWMSAUTO()                          */
5085
/************************************************************************/
5086
5087
/**
5088
 * \brief Initialize from WMSAUTO string.
5089
 *
5090
 * Note that the WMS 1.3 specification does not include the
5091
 * units code, while apparently earlier specs do.  We try to
5092
 * guess around this.
5093
 *
5094
 * @param pszDefinition the WMSAUTO string
5095
 *
5096
 * @return OGRERR_NONE on success or an error code.
5097
 */
5098
OGRErr OGRSpatialReference::importFromWMSAUTO(const char *pszDefinition)
5099
5100
0
{
5101
0
    TAKE_OPTIONAL_LOCK();
5102
5103
0
#if PROJ_AT_LEAST_VERSION(8, 1, 0)
5104
0
    if (strlen(pszDefinition) >= 10000)
5105
0
    {
5106
0
        CPLError(CE_Failure, CPLE_AppDefined, "Too long input string");
5107
0
        return OGRERR_CORRUPT_DATA;
5108
0
    }
5109
5110
0
    auto obj = proj_create(d->getPROJContext(), pszDefinition);
5111
0
    if (!obj)
5112
0
    {
5113
0
        return OGRERR_FAILURE;
5114
0
    }
5115
0
    Clear();
5116
0
    d->setPjCRS(obj);
5117
0
    return OGRERR_NONE;
5118
#else
5119
    int nProjId, nUnitsId;
5120
    double dfRefLong, dfRefLat = 0.0;
5121
5122
    /* -------------------------------------------------------------------- */
5123
    /*      Tokenize                                                        */
5124
    /* -------------------------------------------------------------------- */
5125
    if (STARTS_WITH_CI(pszDefinition, "AUTO:"))
5126
        pszDefinition += 5;
5127
5128
    char **papszTokens =
5129
        CSLTokenizeStringComplex(pszDefinition, ",", FALSE, TRUE);
5130
5131
    if (CSLCount(papszTokens) == 4)
5132
    {
5133
        nProjId = atoi(papszTokens[0]);
5134
        nUnitsId = atoi(papszTokens[1]);
5135
        dfRefLong = CPLAtof(papszTokens[2]);
5136
        dfRefLat = CPLAtof(papszTokens[3]);
5137
    }
5138
    else if (CSLCount(papszTokens) == 3 && atoi(papszTokens[0]) == 42005)
5139
    {
5140
        nProjId = atoi(papszTokens[0]);
5141
        nUnitsId = atoi(papszTokens[1]);
5142
        dfRefLong = CPLAtof(papszTokens[2]);
5143
        dfRefLat = 0.0;
5144
    }
5145
    else if (CSLCount(papszTokens) == 3)
5146
    {
5147
        nProjId = atoi(papszTokens[0]);
5148
        nUnitsId = 9001;
5149
        dfRefLong = CPLAtof(papszTokens[1]);
5150
        dfRefLat = CPLAtof(papszTokens[2]);
5151
    }
5152
    else if (CSLCount(papszTokens) == 2 && atoi(papszTokens[0]) == 42005)
5153
    {
5154
        nProjId = atoi(papszTokens[0]);
5155
        nUnitsId = 9001;
5156
        dfRefLong = CPLAtof(papszTokens[1]);
5157
    }
5158
    else
5159
    {
5160
        CSLDestroy(papszTokens);
5161
        CPLError(CE_Failure, CPLE_AppDefined,
5162
                 "AUTO projection has wrong number of arguments, expected\n"
5163
                 "AUTO:proj_id,units_id,ref_long,ref_lat or"
5164
                 "AUTO:proj_id,ref_long,ref_lat");
5165
        return OGRERR_FAILURE;
5166
    }
5167
5168
    CSLDestroy(papszTokens);
5169
    papszTokens = nullptr;
5170
5171
    /* -------------------------------------------------------------------- */
5172
    /*      Build coordsys.                                                 */
5173
    /* -------------------------------------------------------------------- */
5174
    Clear();
5175
5176
    /* -------------------------------------------------------------------- */
5177
    /*      Set WGS84.                                                      */
5178
    /* -------------------------------------------------------------------- */
5179
    SetWellKnownGeogCS("WGS84");
5180
5181
    switch (nProjId)
5182
    {
5183
        case 42001:  // Auto UTM
5184
            SetUTM(static_cast<int>(floor((dfRefLong + 180.0) / 6.0)) + 1,
5185
                   dfRefLat >= 0.0);
5186
            break;
5187
5188
        case 42002:  // Auto TM (strangely very UTM-like).
5189
            SetTM(0, dfRefLong, 0.9996, 500000.0,
5190
                  (dfRefLat >= 0.0) ? 0.0 : 10000000.0);
5191
            break;
5192
5193
        case 42003:  // Auto Orthographic.
5194
            SetOrthographic(dfRefLat, dfRefLong, 0.0, 0.0);
5195
            break;
5196
5197
        case 42004:  // Auto Equirectangular
5198
            SetEquirectangular(dfRefLat, dfRefLong, 0.0, 0.0);
5199
            break;
5200
5201
        case 42005:
5202
            SetMollweide(dfRefLong, 0.0, 0.0);
5203
            break;
5204
5205
        default:
5206
            CPLError(CE_Failure, CPLE_AppDefined,
5207
                     "Unsupported projection id in importFromWMSAUTO(): %d",
5208
                     nProjId);
5209
            return OGRERR_FAILURE;
5210
    }
5211
5212
    /* -------------------------------------------------------------------- */
5213
    /*      Set units.                                                      */
5214
    /* -------------------------------------------------------------------- */
5215
5216
    switch (nUnitsId)
5217
    {
5218
        case 9001:
5219
            SetTargetLinearUnits(nullptr, SRS_UL_METER, 1.0, "EPSG", "9001");
5220
            break;
5221
5222
        case 9002:
5223
            SetTargetLinearUnits(nullptr, "Foot", 0.3048, "EPSG", "9002");
5224
            break;
5225
5226
        case 9003:
5227
            SetTargetLinearUnits(nullptr, "US survey foot",
5228
                                 CPLAtof(SRS_UL_US_FOOT_CONV), "EPSG", "9003");
5229
            break;
5230
5231
        default:
5232
            CPLError(CE_Failure, CPLE_AppDefined,
5233
                     "Unsupported units code (%d).", nUnitsId);
5234
            return OGRERR_FAILURE;
5235
            break;
5236
    }
5237
5238
    return OGRERR_NONE;
5239
#endif
5240
0
}
5241
5242
/************************************************************************/
5243
/*                            GetSemiMajor()                            */
5244
/************************************************************************/
5245
5246
/**
5247
 * \brief Get spheroid semi major axis (in metres starting with GDAL 3.0)
5248
 *
5249
 * This method does the same thing as the C function OSRGetSemiMajor().
5250
 *
5251
 * @param pnErr if non-NULL set to OGRERR_FAILURE if semi major axis
5252
 * can be found.
5253
 *
5254
 * @return semi-major axis, or SRS_WGS84_SEMIMAJOR if it can't be found.
5255
 */
5256
5257
double OGRSpatialReference::GetSemiMajor(OGRErr *pnErr) const
5258
5259
0
{
5260
0
    TAKE_OPTIONAL_LOCK();
5261
5262
0
    if (pnErr != nullptr)
5263
0
        *pnErr = OGRERR_FAILURE;
5264
5265
0
    d->refreshProjObj();
5266
0
    if (!d->m_pj_crs)
5267
0
        return SRS_WGS84_SEMIMAJOR;
5268
5269
0
    auto ellps = proj_get_ellipsoid(d->getPROJContext(), d->m_pj_crs);
5270
0
    if (!ellps)
5271
0
        return SRS_WGS84_SEMIMAJOR;
5272
5273
0
    double dfSemiMajor = 0.0;
5274
0
    proj_ellipsoid_get_parameters(d->getPROJContext(), ellps, &dfSemiMajor,
5275
0
                                  nullptr, nullptr, nullptr);
5276
0
    proj_destroy(ellps);
5277
5278
0
    if (dfSemiMajor > 0)
5279
0
    {
5280
0
        if (pnErr != nullptr)
5281
0
            *pnErr = OGRERR_NONE;
5282
0
        return dfSemiMajor;
5283
0
    }
5284
5285
0
    return SRS_WGS84_SEMIMAJOR;
5286
0
}
5287
5288
/************************************************************************/
5289
/*                          OSRGetSemiMajor()                           */
5290
/************************************************************************/
5291
5292
/**
5293
 * \brief Get spheroid semi major axis.
5294
 *
5295
 * This function is the same as OGRSpatialReference::GetSemiMajor()
5296
 */
5297
double OSRGetSemiMajor(OGRSpatialReferenceH hSRS, OGRErr *pnErr)
5298
5299
0
{
5300
0
    VALIDATE_POINTER1(hSRS, "OSRGetSemiMajor", 0);
5301
5302
0
    return ToPointer(hSRS)->GetSemiMajor(pnErr);
5303
0
}
5304
5305
/************************************************************************/
5306
/*                          GetInvFlattening()                          */
5307
/************************************************************************/
5308
5309
/**
5310
 * \brief Get spheroid inverse flattening.
5311
 *
5312
 * This method does the same thing as the C function OSRGetInvFlattening().
5313
 *
5314
 * @param pnErr if non-NULL set to OGRERR_FAILURE if no inverse flattening
5315
 * can be found.
5316
 *
5317
 * @return inverse flattening, or SRS_WGS84_INVFLATTENING if it can't be found.
5318
 */
5319
5320
double OGRSpatialReference::GetInvFlattening(OGRErr *pnErr) const
5321
5322
0
{
5323
0
    TAKE_OPTIONAL_LOCK();
5324
5325
0
    if (pnErr != nullptr)
5326
0
        *pnErr = OGRERR_FAILURE;
5327
5328
0
    d->refreshProjObj();
5329
0
    if (!d->m_pj_crs)
5330
0
        return SRS_WGS84_INVFLATTENING;
5331
5332
0
    auto ellps = proj_get_ellipsoid(d->getPROJContext(), d->m_pj_crs);
5333
0
    if (!ellps)
5334
0
        return SRS_WGS84_INVFLATTENING;
5335
5336
0
    double dfInvFlattening = -1.0;
5337
0
    proj_ellipsoid_get_parameters(d->getPROJContext(), ellps, nullptr, nullptr,
5338
0
                                  nullptr, &dfInvFlattening);
5339
0
    proj_destroy(ellps);
5340
5341
0
    if (dfInvFlattening >= 0.0)
5342
0
    {
5343
0
        if (pnErr != nullptr)
5344
0
            *pnErr = OGRERR_NONE;
5345
0
        return dfInvFlattening;
5346
0
    }
5347
5348
0
    return SRS_WGS84_INVFLATTENING;
5349
0
}
5350
5351
/************************************************************************/
5352
/*                        OSRGetInvFlattening()                         */
5353
/************************************************************************/
5354
5355
/**
5356
 * \brief Get spheroid inverse flattening.
5357
 *
5358
 * This function is the same as OGRSpatialReference::GetInvFlattening()
5359
 */
5360
double OSRGetInvFlattening(OGRSpatialReferenceH hSRS, OGRErr *pnErr)
5361
5362
0
{
5363
0
    VALIDATE_POINTER1(hSRS, "OSRGetInvFlattening", 0);
5364
5365
0
    return ToPointer(hSRS)->GetInvFlattening(pnErr);
5366
0
}
5367
5368
/************************************************************************/
5369
/*                           GetEccentricity()                          */
5370
/************************************************************************/
5371
5372
/**
5373
 * \brief Get spheroid eccentricity
5374
 *
5375
 * @return eccentricity (or -1 in case of error)
5376
 */
5377
5378
double OGRSpatialReference::GetEccentricity() const
5379
5380
0
{
5381
0
    OGRErr eErr = OGRERR_NONE;
5382
0
    const double dfInvFlattening = GetInvFlattening(&eErr);
5383
0
    if (eErr != OGRERR_NONE)
5384
0
    {
5385
0
        return -1.0;
5386
0
    }
5387
0
    if (dfInvFlattening == 0.0)
5388
0
        return 0.0;
5389
0
    if (dfInvFlattening < 0.5)
5390
0
        return -1.0;
5391
0
    return sqrt(2.0 / dfInvFlattening -
5392
0
                1.0 / (dfInvFlattening * dfInvFlattening));
5393
0
}
5394
5395
/************************************************************************/
5396
/*                      GetSquaredEccentricity()                        */
5397
/************************************************************************/
5398
5399
/**
5400
 * \brief Get spheroid squared eccentricity
5401
 *
5402
 * @return squared eccentricity (or -1 in case of error)
5403
 */
5404
5405
double OGRSpatialReference::GetSquaredEccentricity() const
5406
5407
0
{
5408
0
    OGRErr eErr = OGRERR_NONE;
5409
0
    const double dfInvFlattening = GetInvFlattening(&eErr);
5410
0
    if (eErr != OGRERR_NONE)
5411
0
    {
5412
0
        return -1.0;
5413
0
    }
5414
0
    if (dfInvFlattening == 0.0)
5415
0
        return 0.0;
5416
0
    if (dfInvFlattening < 0.5)
5417
0
        return -1.0;
5418
0
    return 2.0 / dfInvFlattening - 1.0 / (dfInvFlattening * dfInvFlattening);
5419
0
}
5420
5421
/************************************************************************/
5422
/*                            GetSemiMinor()                            */
5423
/************************************************************************/
5424
5425
/**
5426
 * \brief Get spheroid semi minor axis.
5427
 *
5428
 * This method does the same thing as the C function OSRGetSemiMinor().
5429
 *
5430
 * @param pnErr if non-NULL set to OGRERR_FAILURE if semi minor axis
5431
 * can be found.
5432
 *
5433
 * @return semi-minor axis, or WGS84 semi minor if it can't be found.
5434
 */
5435
5436
double OGRSpatialReference::GetSemiMinor(OGRErr *pnErr) const
5437
5438
0
{
5439
0
    const double dfSemiMajor = GetSemiMajor(pnErr);
5440
0
    const double dfInvFlattening = GetInvFlattening(pnErr);
5441
5442
0
    return OSRCalcSemiMinorFromInvFlattening(dfSemiMajor, dfInvFlattening);
5443
0
}
5444
5445
/************************************************************************/
5446
/*                          OSRGetSemiMinor()                           */
5447
/************************************************************************/
5448
5449
/**
5450
 * \brief Get spheroid semi minor axis.
5451
 *
5452
 * This function is the same as OGRSpatialReference::GetSemiMinor()
5453
 */
5454
double OSRGetSemiMinor(OGRSpatialReferenceH hSRS, OGRErr *pnErr)
5455
5456
0
{
5457
0
    VALIDATE_POINTER1(hSRS, "OSRGetSemiMinor", 0);
5458
5459
0
    return ToPointer(hSRS)->GetSemiMinor(pnErr);
5460
0
}
5461
5462
/************************************************************************/
5463
/*                             SetLocalCS()                             */
5464
/************************************************************************/
5465
5466
/**
5467
 * \brief Set the user visible LOCAL_CS name.
5468
 *
5469
 * This method is the same as the C function OSRSetLocalCS().
5470
 *
5471
 * This method will ensure a LOCAL_CS node is created as the root,
5472
 * and set the provided name on it.  It must be used before SetLinearUnits().
5473
 *
5474
 * @param pszName the user visible name to assign.  Not used as a key.
5475
 *
5476
 * @return OGRERR_NONE on success.
5477
 */
5478
5479
OGRErr OGRSpatialReference::SetLocalCS(const char *pszName)
5480
5481
0
{
5482
0
    TAKE_OPTIONAL_LOCK();
5483
5484
0
    if (d->m_pjType == PJ_TYPE_UNKNOWN ||
5485
0
        d->m_pjType == PJ_TYPE_ENGINEERING_CRS)
5486
0
    {
5487
0
        d->setPjCRS(proj_create_engineering_crs(d->getPROJContext(), pszName));
5488
0
    }
5489
0
    else
5490
0
    {
5491
0
        CPLDebug("OGR",
5492
0
                 "OGRSpatialReference::SetLocalCS(%s) failed.  "
5493
0
                 "It appears an incompatible object already exists.",
5494
0
                 pszName);
5495
0
        return OGRERR_FAILURE;
5496
0
    }
5497
5498
0
    return OGRERR_NONE;
5499
0
}
5500
5501
/************************************************************************/
5502
/*                           OSRSetLocalCS()                            */
5503
/************************************************************************/
5504
5505
/**
5506
 * \brief Set the user visible LOCAL_CS name.
5507
 *
5508
 * This function is the same as OGRSpatialReference::SetLocalCS()
5509
 */
5510
OGRErr OSRSetLocalCS(OGRSpatialReferenceH hSRS, const char *pszName)
5511
5512
0
{
5513
0
    VALIDATE_POINTER1(hSRS, "OSRSetLocalCS", OGRERR_FAILURE);
5514
5515
0
    return ToPointer(hSRS)->SetLocalCS(pszName);
5516
0
}
5517
5518
/************************************************************************/
5519
/*                             SetGeocCS()                              */
5520
/************************************************************************/
5521
5522
/**
5523
 * \brief Set the user visible GEOCCS name.
5524
 *
5525
 * This method is the same as the C function OSRSetGeocCS().
5526
5527
 * This method will ensure a GEOCCS node is created as the root,
5528
 * and set the provided name on it.  If used on a GEOGCS coordinate system,
5529
 * the DATUM and PRIMEM nodes from the GEOGCS will be transferred over to
5530
 * the GEOGCS.
5531
 *
5532
 * @param pszName the user visible name to assign.  Not used as a key.
5533
 *
5534
 * @return OGRERR_NONE on success.
5535
 *
5536
 */
5537
5538
OGRErr OGRSpatialReference::SetGeocCS(const char *pszName)
5539
5540
0
{
5541
0
    TAKE_OPTIONAL_LOCK();
5542
5543
0
    OGRErr eErr = OGRERR_NONE;
5544
0
    d->refreshProjObj();
5545
0
    d->demoteFromBoundCRS();
5546
0
    if (d->m_pjType == PJ_TYPE_UNKNOWN)
5547
0
    {
5548
0
        d->setPjCRS(proj_create_geocentric_crs(
5549
0
            d->getPROJContext(), pszName, "World Geodetic System 1984",
5550
0
            "WGS 84", SRS_WGS84_SEMIMAJOR, SRS_WGS84_INVFLATTENING,
5551
0
            SRS_PM_GREENWICH, 0.0, SRS_UA_DEGREE, CPLAtof(SRS_UA_DEGREE_CONV),
5552
0
            "Metre", 1.0));
5553
0
    }
5554
0
    else if (d->m_pjType == PJ_TYPE_GEOCENTRIC_CRS)
5555
0
    {
5556
0
        d->setPjCRS(proj_alter_name(d->getPROJContext(), d->m_pj_crs, pszName));
5557
0
    }
5558
0
    else if (d->m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
5559
0
             d->m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS)
5560
0
    {
5561
0
        auto datum = proj_crs_get_datum(d->getPROJContext(), d->m_pj_crs);
5562
0
#if PROJ_VERSION_MAJOR > 7 ||                                                  \
5563
0
    (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
5564
0
        if (datum == nullptr)
5565
0
        {
5566
0
            datum =
5567
0
                proj_crs_get_datum_ensemble(d->getPROJContext(), d->m_pj_crs);
5568
0
        }
5569
0
#endif
5570
0
        if (datum == nullptr)
5571
0
        {
5572
0
            d->undoDemoteFromBoundCRS();
5573
0
            return OGRERR_FAILURE;
5574
0
        }
5575
5576
0
        auto pj_crs = proj_create_geocentric_crs_from_datum(
5577
0
            d->getPROJContext(), proj_get_name(d->m_pj_crs), datum, nullptr,
5578
0
            0.0);
5579
0
        d->setPjCRS(pj_crs);
5580
5581
0
        proj_destroy(datum);
5582
0
    }
5583
0
    else
5584
0
    {
5585
0
        CPLDebug("OGR",
5586
0
                 "OGRSpatialReference::SetGeocCS(%s) failed.  "
5587
0
                 "It appears an incompatible object already exists.",
5588
0
                 pszName);
5589
0
        eErr = OGRERR_FAILURE;
5590
0
    }
5591
0
    d->undoDemoteFromBoundCRS();
5592
5593
0
    return eErr;
5594
0
}
5595
5596
/************************************************************************/
5597
/*                            OSRSetGeocCS()                            */
5598
/************************************************************************/
5599
5600
/**
5601
 * \brief Set the user visible PROJCS name.
5602
 *
5603
 * This function is the same as OGRSpatialReference::SetGeocCS()
5604
 *
5605
 */
5606
OGRErr OSRSetGeocCS(OGRSpatialReferenceH hSRS, const char *pszName)
5607
5608
0
{
5609
0
    VALIDATE_POINTER1(hSRS, "OSRSetGeocCS", OGRERR_FAILURE);
5610
5611
0
    return ToPointer(hSRS)->SetGeocCS(pszName);
5612
0
}
5613
5614
/************************************************************************/
5615
/*                             SetVertCS()                              */
5616
/************************************************************************/
5617
5618
/**
5619
 * \brief Set the user visible VERT_CS name.
5620
 *
5621
 * This method is the same as the C function OSRSetVertCS().
5622
5623
 * This method will ensure a VERT_CS node is created if needed.  If the
5624
 * existing coordinate system is GEOGCS or PROJCS rooted, then it will be
5625
 * turned into a COMPD_CS.
5626
 *
5627
 * @param pszVertCSName the user visible name of the vertical coordinate
5628
 * system. Not used as a key.
5629
 *
5630
 * @param pszVertDatumName the user visible name of the vertical datum.  It
5631
 * is helpful if this matches the EPSG name.
5632
 *
5633
 * @param nVertDatumType the OGC vertical datum type. Ignored
5634
 *
5635
 * @return OGRERR_NONE on success.
5636
 *
5637
 */
5638
5639
OGRErr OGRSpatialReference::SetVertCS(const char *pszVertCSName,
5640
                                      const char *pszVertDatumName,
5641
                                      int nVertDatumType)
5642
5643
0
{
5644
0
    TAKE_OPTIONAL_LOCK();
5645
5646
0
    CPL_IGNORE_RET_VAL(nVertDatumType);
5647
5648
0
    d->refreshProjObj();
5649
5650
0
    auto vertCRS = proj_create_vertical_crs(d->getPROJContext(), pszVertCSName,
5651
0
                                            pszVertDatumName, nullptr, 0.0);
5652
5653
    /* -------------------------------------------------------------------- */
5654
    /*      Handle the case where we want to make a compound coordinate     */
5655
    /*      system.                                                         */
5656
    /* -------------------------------------------------------------------- */
5657
0
    if (IsProjected() || IsGeographic())
5658
0
    {
5659
0
        auto compoundCRS = proj_create_compound_crs(
5660
0
            d->getPROJContext(), nullptr, d->m_pj_crs, vertCRS);
5661
0
        proj_destroy(vertCRS);
5662
0
        d->setPjCRS(compoundCRS);
5663
0
    }
5664
0
    else
5665
0
    {
5666
0
        d->setPjCRS(vertCRS);
5667
0
    }
5668
0
    return OGRERR_NONE;
5669
0
}
5670
5671
/************************************************************************/
5672
/*                            OSRSetVertCS()                            */
5673
/************************************************************************/
5674
5675
/**
5676
 * \brief Setup the vertical coordinate system.
5677
 *
5678
 * This function is the same as OGRSpatialReference::SetVertCS()
5679
 *
5680
 */
5681
OGRErr OSRSetVertCS(OGRSpatialReferenceH hSRS, const char *pszVertCSName,
5682
                    const char *pszVertDatumName, int nVertDatumType)
5683
5684
0
{
5685
0
    VALIDATE_POINTER1(hSRS, "OSRSetVertCS", OGRERR_FAILURE);
5686
5687
0
    return ToPointer(hSRS)->SetVertCS(pszVertCSName, pszVertDatumName,
5688
0
                                      nVertDatumType);
5689
0
}
5690
5691
/************************************************************************/
5692
/*                           SetCompoundCS()                            */
5693
/************************************************************************/
5694
5695
/**
5696
 * \brief Setup a compound coordinate system.
5697
 *
5698
 * This method is the same as the C function OSRSetCompoundCS().
5699
5700
 * This method is replace the current SRS with a COMPD_CS coordinate system
5701
 * consisting of the passed in horizontal and vertical coordinate systems.
5702
 *
5703
 * @param pszName the name of the compound coordinate system.
5704
 *
5705
 * @param poHorizSRS the horizontal SRS (PROJCS or GEOGCS).
5706
 *
5707
 * @param poVertSRS the vertical SRS (VERT_CS).
5708
 *
5709
 * @return OGRERR_NONE on success.
5710
 */
5711
5712
OGRErr OGRSpatialReference::SetCompoundCS(const char *pszName,
5713
                                          const OGRSpatialReference *poHorizSRS,
5714
                                          const OGRSpatialReference *poVertSRS)
5715
5716
0
{
5717
0
    TAKE_OPTIONAL_LOCK();
5718
5719
    /* -------------------------------------------------------------------- */
5720
    /*      Verify these are legal horizontal and vertical coordinate       */
5721
    /*      systems.                                                        */
5722
    /* -------------------------------------------------------------------- */
5723
0
    if (!poVertSRS->IsVertical())
5724
0
    {
5725
0
        CPLError(CE_Failure, CPLE_AppDefined,
5726
0
                 "SetCompoundCS() fails, vertical component is not VERT_CS.");
5727
0
        return OGRERR_FAILURE;
5728
0
    }
5729
0
    if (!poHorizSRS->IsProjected() && !poHorizSRS->IsGeographic())
5730
0
    {
5731
0
        CPLError(CE_Failure, CPLE_AppDefined,
5732
0
                 "SetCompoundCS() fails, horizontal component is not PROJCS or "
5733
0
                 "GEOGCS.");
5734
0
        return OGRERR_FAILURE;
5735
0
    }
5736
5737
    /* -------------------------------------------------------------------- */
5738
    /*      Replace with compound srs.                                      */
5739
    /* -------------------------------------------------------------------- */
5740
0
    Clear();
5741
5742
0
    auto compoundCRS = proj_create_compound_crs(d->getPROJContext(), pszName,
5743
0
                                                poHorizSRS->d->m_pj_crs,
5744
0
                                                poVertSRS->d->m_pj_crs);
5745
0
    d->setPjCRS(compoundCRS);
5746
5747
0
    return OGRERR_NONE;
5748
0
}
5749
5750
/************************************************************************/
5751
/*                          OSRSetCompoundCS()                          */
5752
/************************************************************************/
5753
5754
/**
5755
 * \brief Setup a compound coordinate system.
5756
 *
5757
 * This function is the same as OGRSpatialReference::SetCompoundCS()
5758
 */
5759
OGRErr OSRSetCompoundCS(OGRSpatialReferenceH hSRS, const char *pszName,
5760
                        OGRSpatialReferenceH hHorizSRS,
5761
                        OGRSpatialReferenceH hVertSRS)
5762
5763
0
{
5764
0
    VALIDATE_POINTER1(hSRS, "OSRSetCompoundCS", OGRERR_FAILURE);
5765
0
    VALIDATE_POINTER1(hHorizSRS, "OSRSetCompoundCS", OGRERR_FAILURE);
5766
0
    VALIDATE_POINTER1(hVertSRS, "OSRSetCompoundCS", OGRERR_FAILURE);
5767
5768
0
    return ToPointer(hSRS)->SetCompoundCS(pszName, ToPointer(hHorizSRS),
5769
0
                                          ToPointer(hVertSRS));
5770
0
}
5771
5772
/************************************************************************/
5773
/*                             SetProjCS()                              */
5774
/************************************************************************/
5775
5776
/**
5777
 * \brief Set the user visible PROJCS name.
5778
 *
5779
 * This method is the same as the C function OSRSetProjCS().
5780
 *
5781
 * This method will ensure a PROJCS node is created as the root,
5782
 * and set the provided name on it.  If used on a GEOGCS coordinate system,
5783
 * the GEOGCS node will be demoted to be a child of the new PROJCS root.
5784
 *
5785
 * @param pszName the user visible name to assign.  Not used as a key.
5786
 *
5787
 * @return OGRERR_NONE on success.
5788
 */
5789
5790
OGRErr OGRSpatialReference::SetProjCS(const char *pszName)
5791
5792
0
{
5793
0
    TAKE_OPTIONAL_LOCK();
5794
5795
0
    d->refreshProjObj();
5796
0
    d->demoteFromBoundCRS();
5797
0
    if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
5798
0
    {
5799
0
        d->setPjCRS(proj_alter_name(d->getPROJContext(), d->m_pj_crs, pszName));
5800
0
    }
5801
0
    else
5802
0
    {
5803
0
        auto dummyConv = proj_create_conversion(d->getPROJContext(), nullptr,
5804
0
                                                nullptr, nullptr, nullptr,
5805
0
                                                nullptr, nullptr, 0, nullptr);
5806
0
        auto cs = proj_create_cartesian_2D_cs(
5807
0
            d->getPROJContext(), PJ_CART2D_EASTING_NORTHING, nullptr, 0);
5808
5809
0
        auto projCRS = proj_create_projected_crs(
5810
0
            d->getPROJContext(), pszName, d->getGeodBaseCRS(), dummyConv, cs);
5811
0
        proj_destroy(dummyConv);
5812
0
        proj_destroy(cs);
5813
5814
0
        d->setPjCRS(projCRS);
5815
0
    }
5816
0
    d->undoDemoteFromBoundCRS();
5817
0
    return OGRERR_NONE;
5818
0
}
5819
5820
/************************************************************************/
5821
/*                            OSRSetProjCS()                            */
5822
/************************************************************************/
5823
5824
/**
5825
 * \brief Set the user visible PROJCS name.
5826
 *
5827
 * This function is the same as OGRSpatialReference::SetProjCS()
5828
 */
5829
OGRErr OSRSetProjCS(OGRSpatialReferenceH hSRS, const char *pszName)
5830
5831
0
{
5832
0
    VALIDATE_POINTER1(hSRS, "OSRSetProjCS", OGRERR_FAILURE);
5833
5834
0
    return ToPointer(hSRS)->SetProjCS(pszName);
5835
0
}
5836
5837
/************************************************************************/
5838
/*                           SetProjection()                            */
5839
/************************************************************************/
5840
5841
/**
5842
 * \brief Set a projection name.
5843
 *
5844
 * This method is the same as the C function OSRSetProjection().
5845
 *
5846
 * @param pszProjection the projection name, which should be selected from
5847
 * the macros in ogr_srs_api.h, such as SRS_PT_TRANSVERSE_MERCATOR.
5848
 *
5849
 * @return OGRERR_NONE on success.
5850
 */
5851
5852
OGRErr OGRSpatialReference::SetProjection(const char *pszProjection)
5853
5854
0
{
5855
0
    TAKE_OPTIONAL_LOCK();
5856
5857
0
    OGR_SRSNode *poGeogCS = nullptr;
5858
5859
0
    if (GetRoot() != nullptr && EQUAL(d->m_poRoot->GetValue(), "GEOGCS"))
5860
0
    {
5861
0
        poGeogCS = d->m_poRoot;
5862
0
        d->m_poRoot = nullptr;
5863
0
    }
5864
5865
0
    if (!GetAttrNode("PROJCS"))
5866
0
    {
5867
0
        SetNode("PROJCS", "unnamed");
5868
0
    }
5869
5870
0
    const OGRErr eErr = SetNode("PROJCS|PROJECTION", pszProjection);
5871
0
    if (eErr != OGRERR_NONE)
5872
0
        return eErr;
5873
5874
0
    if (poGeogCS != nullptr)
5875
0
        d->m_poRoot->InsertChild(poGeogCS, 1);
5876
5877
0
    return OGRERR_NONE;
5878
0
}
5879
5880
/************************************************************************/
5881
/*                            OSRSetProjection()                        */
5882
/************************************************************************/
5883
5884
/**
5885
 * \brief Set a projection name.
5886
 *
5887
 * This function is the same as OGRSpatialReference::SetProjection()
5888
 */
5889
OGRErr OSRSetProjection(OGRSpatialReferenceH hSRS, const char *pszProjection)
5890
5891
0
{
5892
0
    VALIDATE_POINTER1(hSRS, "OSRSetProjection", OGRERR_FAILURE);
5893
5894
0
    return ToPointer(hSRS)->SetProjection(pszProjection);
5895
0
}
5896
5897
/************************************************************************/
5898
/*                      GetWKT2ProjectionMethod()                       */
5899
/************************************************************************/
5900
5901
/**
5902
 * \brief Returns info on the projection method, based on WKT2 naming
5903
 * conventions.
5904
 *
5905
 * The returned strings are short lived and should be considered to be
5906
 * invalidated by any further call to the GDAL API.
5907
 *
5908
 * @param[out] ppszMethodName Pointer to a string that will receive the
5909
 * projection method name.
5910
 * @param[out] ppszMethodAuthName null pointer, or pointer to a string that will
5911
 * receive the name of the authority that defines the projection method.
5912
 * *ppszMethodAuthName may be nullptr if the projection method is not linked to
5913
 * an authority.
5914
 * @param[out] ppszMethodCode null pointer, or pointer to a string that will
5915
 * receive the code that defines the projection method.
5916
 * *ppszMethodCode may be nullptr if the projection method is not linked to
5917
 * an authority.
5918
 *
5919
 * @return OGRERR_NONE on success.
5920
 */
5921
OGRErr
5922
OGRSpatialReference::GetWKT2ProjectionMethod(const char **ppszMethodName,
5923
                                             const char **ppszMethodAuthName,
5924
                                             const char **ppszMethodCode) const
5925
0
{
5926
0
    TAKE_OPTIONAL_LOCK();
5927
5928
0
    auto conv = proj_crs_get_coordoperation(d->getPROJContext(), d->m_pj_crs);
5929
0
    if (!conv)
5930
0
        return OGRERR_FAILURE;
5931
0
    const char *pszTmpMethodName = "";
5932
0
    const char *pszTmpMethodAuthName = "";
5933
0
    const char *pszTmpMethodCode = "";
5934
0
    int ret = proj_coordoperation_get_method_info(
5935
0
        d->getPROJContext(), conv, &pszTmpMethodName, &pszTmpMethodAuthName,
5936
0
        &pszTmpMethodCode);
5937
    // "Internalize" temporary strings returned by PROJ
5938
0
    CPLAssert(pszTmpMethodName);
5939
0
    if (ppszMethodName)
5940
0
        *ppszMethodName = CPLSPrintf("%s", pszTmpMethodName);
5941
0
    if (ppszMethodAuthName)
5942
0
        *ppszMethodAuthName = pszTmpMethodAuthName
5943
0
                                  ? CPLSPrintf("%s", pszTmpMethodAuthName)
5944
0
                                  : nullptr;
5945
0
    if (ppszMethodCode)
5946
0
        *ppszMethodCode =
5947
0
            pszTmpMethodCode ? CPLSPrintf("%s", pszTmpMethodCode) : nullptr;
5948
0
    proj_destroy(conv);
5949
0
    return ret ? OGRERR_NONE : OGRERR_FAILURE;
5950
0
}
5951
5952
/************************************************************************/
5953
/*                            SetProjParm()                             */
5954
/************************************************************************/
5955
5956
/**
5957
 * \brief Set a projection parameter value.
5958
 *
5959
 * Adds a new PARAMETER under the PROJCS with the indicated name and value.
5960
 *
5961
 * This method is the same as the C function OSRSetProjParm().
5962
 *
5963
 * Please check https://gdal.org/proj_list pages for
5964
 * legal parameter names for specific projections.
5965
 *
5966
 *
5967
 * @param pszParamName the parameter name, which should be selected from
5968
 * the macros in ogr_srs_api.h, such as SRS_PP_CENTRAL_MERIDIAN.
5969
 *
5970
 * @param dfValue value to assign.
5971
 *
5972
 * @return OGRERR_NONE on success.
5973
 */
5974
5975
OGRErr OGRSpatialReference::SetProjParm(const char *pszParamName,
5976
                                        double dfValue)
5977
5978
0
{
5979
0
    TAKE_OPTIONAL_LOCK();
5980
5981
0
    OGR_SRSNode *poPROJCS = GetAttrNode("PROJCS");
5982
5983
0
    if (poPROJCS == nullptr)
5984
0
        return OGRERR_FAILURE;
5985
5986
0
    char szValue[64] = {'\0'};
5987
0
    OGRsnPrintDouble(szValue, sizeof(szValue), dfValue);
5988
5989
    /* -------------------------------------------------------------------- */
5990
    /*      Try to find existing parameter with this name.                  */
5991
    /* -------------------------------------------------------------------- */
5992
0
    for (int iChild = 0; iChild < poPROJCS->GetChildCount(); iChild++)
5993
0
    {
5994
0
        OGR_SRSNode *poParam = poPROJCS->GetChild(iChild);
5995
5996
0
        if (EQUAL(poParam->GetValue(), "PARAMETER") &&
5997
0
            poParam->GetChildCount() == 2 &&
5998
0
            EQUAL(poParam->GetChild(0)->GetValue(), pszParamName))
5999
0
        {
6000
0
            poParam->GetChild(1)->SetValue(szValue);
6001
0
            return OGRERR_NONE;
6002
0
        }
6003
0
    }
6004
6005
    /* -------------------------------------------------------------------- */
6006
    /*      Otherwise create a new parameter and append.                    */
6007
    /* -------------------------------------------------------------------- */
6008
0
    OGR_SRSNode *poParam = new OGR_SRSNode("PARAMETER");
6009
0
    poParam->AddChild(new OGR_SRSNode(pszParamName));
6010
0
    poParam->AddChild(new OGR_SRSNode(szValue));
6011
6012
0
    poPROJCS->AddChild(poParam);
6013
6014
0
    return OGRERR_NONE;
6015
0
}
6016
6017
/************************************************************************/
6018
/*                           OSRSetProjParm()                           */
6019
/************************************************************************/
6020
6021
/**
6022
 * \brief Set a projection parameter value.
6023
 *
6024
 * This function is the same as OGRSpatialReference::SetProjParm()
6025
 */
6026
OGRErr OSRSetProjParm(OGRSpatialReferenceH hSRS, const char *pszParamName,
6027
                      double dfValue)
6028
6029
0
{
6030
0
    VALIDATE_POINTER1(hSRS, "OSRSetProjParm", OGRERR_FAILURE);
6031
6032
0
    return ToPointer(hSRS)->SetProjParm(pszParamName, dfValue);
6033
0
}
6034
6035
/************************************************************************/
6036
/*                            FindProjParm()                            */
6037
/************************************************************************/
6038
6039
/**
6040
 * \brief Return the child index of the named projection parameter on
6041
 * its parent PROJCS node.
6042
 *
6043
 * @param pszParameter projection parameter to look for
6044
 * @param poPROJCS projection CS node to look in. If NULL is passed,
6045
 *        the PROJCS node of the SpatialReference object will be searched.
6046
 *
6047
 * @return the child index of the named projection parameter. -1 on failure
6048
 */
6049
int OGRSpatialReference::FindProjParm(const char *pszParameter,
6050
                                      const OGR_SRSNode *poPROJCS) const
6051
6052
0
{
6053
0
    TAKE_OPTIONAL_LOCK();
6054
6055
0
    if (poPROJCS == nullptr)
6056
0
        poPROJCS = GetAttrNode("PROJCS");
6057
6058
0
    if (poPROJCS == nullptr)
6059
0
        return -1;
6060
6061
    /* -------------------------------------------------------------------- */
6062
    /*      Search for requested parameter.                                 */
6063
    /* -------------------------------------------------------------------- */
6064
0
    bool bIsWKT2 = false;
6065
0
    for (int iChild = 0; iChild < poPROJCS->GetChildCount(); iChild++)
6066
0
    {
6067
0
        const OGR_SRSNode *poParameter = poPROJCS->GetChild(iChild);
6068
6069
0
        if (poParameter->GetChildCount() >= 2)
6070
0
        {
6071
0
            const char *pszValue = poParameter->GetValue();
6072
0
            if (EQUAL(pszValue, "PARAMETER") &&
6073
0
                EQUAL(poPROJCS->GetChild(iChild)->GetChild(0)->GetValue(),
6074
0
                      pszParameter))
6075
0
            {
6076
0
                return iChild;
6077
0
            }
6078
0
            else if (EQUAL(pszValue, "METHOD"))
6079
0
            {
6080
0
                bIsWKT2 = true;
6081
0
            }
6082
0
        }
6083
0
    }
6084
6085
    /* -------------------------------------------------------------------- */
6086
    /*      Try similar names, for selected parameters.                     */
6087
    /* -------------------------------------------------------------------- */
6088
0
    if (EQUAL(pszParameter, SRS_PP_LATITUDE_OF_ORIGIN))
6089
0
    {
6090
0
        if (bIsWKT2)
6091
0
        {
6092
0
            int iChild = FindProjParm(
6093
0
                EPSG_NAME_PARAMETER_LATITUDE_OF_NATURAL_ORIGIN, poPROJCS);
6094
0
            if (iChild == -1)
6095
0
                iChild = FindProjParm(
6096
0
                    EPSG_NAME_PARAMETER_LATITUDE_PROJECTION_CENTRE, poPROJCS);
6097
0
            return iChild;
6098
0
        }
6099
0
        return FindProjParm(SRS_PP_LATITUDE_OF_CENTER, poPROJCS);
6100
0
    }
6101
6102
0
    if (EQUAL(pszParameter, SRS_PP_CENTRAL_MERIDIAN))
6103
0
    {
6104
0
        if (bIsWKT2)
6105
0
        {
6106
0
            int iChild = FindProjParm(
6107
0
                EPSG_NAME_PARAMETER_LONGITUDE_OF_NATURAL_ORIGIN, poPROJCS);
6108
0
            if (iChild == -1)
6109
0
                iChild = FindProjParm(
6110
0
                    EPSG_NAME_PARAMETER_LONGITUDE_PROJECTION_CENTRE, poPROJCS);
6111
0
            return iChild;
6112
0
        }
6113
0
        int iChild = FindProjParm(SRS_PP_LONGITUDE_OF_CENTER, poPROJCS);
6114
0
        if (iChild == -1)
6115
0
            iChild = FindProjParm(SRS_PP_LONGITUDE_OF_ORIGIN, poPROJCS);
6116
0
        return iChild;
6117
0
    }
6118
6119
0
    return -1;
6120
0
}
6121
6122
/************************************************************************/
6123
/*                            GetProjParm()                             */
6124
/************************************************************************/
6125
6126
/**
6127
 * \brief Fetch a projection parameter value.
6128
 *
6129
 * NOTE: This code should be modified to translate non degree angles into
6130
 * degrees based on the GEOGCS unit.  This has not yet been done.
6131
 *
6132
 * This method is the same as the C function OSRGetProjParm().
6133
 *
6134
 * @param pszName the name of the parameter to fetch, from the set of
6135
 * SRS_PP codes in ogr_srs_api.h.
6136
 *
6137
 * @param dfDefaultValue the value to return if this parameter doesn't exist.
6138
 *
6139
 * @param pnErr place to put error code on failure.  Ignored if NULL.
6140
 *
6141
 * @return value of parameter.
6142
 */
6143
6144
double OGRSpatialReference::GetProjParm(const char *pszName,
6145
                                        double dfDefaultValue,
6146
                                        OGRErr *pnErr) const
6147
6148
0
{
6149
0
    TAKE_OPTIONAL_LOCK();
6150
6151
0
    d->refreshProjObj();
6152
0
    GetRoot();  // force update of d->m_bNodesWKT2
6153
6154
0
    if (pnErr != nullptr)
6155
0
        *pnErr = OGRERR_NONE;
6156
6157
    /* -------------------------------------------------------------------- */
6158
    /*      Find the desired parameter.                                     */
6159
    /* -------------------------------------------------------------------- */
6160
0
    const OGR_SRSNode *poPROJCS =
6161
0
        GetAttrNode(d->m_bNodesWKT2 ? "CONVERSION" : "PROJCS");
6162
0
    if (poPROJCS == nullptr)
6163
0
    {
6164
0
        if (pnErr != nullptr)
6165
0
            *pnErr = OGRERR_FAILURE;
6166
0
        return dfDefaultValue;
6167
0
    }
6168
6169
0
    const int iChild = FindProjParm(pszName, poPROJCS);
6170
0
    if (iChild == -1)
6171
0
    {
6172
0
        if (IsProjected() && GetAxesCount() == 3)
6173
0
        {
6174
0
            OGRSpatialReference *poSRSTmp = Clone();
6175
0
            poSRSTmp->DemoteTo2D(nullptr);
6176
0
            const double dfRet =
6177
0
                poSRSTmp->GetProjParm(pszName, dfDefaultValue, pnErr);
6178
0
            delete poSRSTmp;
6179
0
            return dfRet;
6180
0
        }
6181
6182
0
        if (pnErr != nullptr)
6183
0
            *pnErr = OGRERR_FAILURE;
6184
0
        return dfDefaultValue;
6185
0
    }
6186
6187
0
    const OGR_SRSNode *poParameter = poPROJCS->GetChild(iChild);
6188
0
    return CPLAtof(poParameter->GetChild(1)->GetValue());
6189
0
}
6190
6191
/************************************************************************/
6192
/*                           OSRGetProjParm()                           */
6193
/************************************************************************/
6194
6195
/**
6196
 * \brief Fetch a projection parameter value.
6197
 *
6198
 * This function is the same as OGRSpatialReference::GetProjParm()
6199
 */
6200
double OSRGetProjParm(OGRSpatialReferenceH hSRS, const char *pszName,
6201
                      double dfDefaultValue, OGRErr *pnErr)
6202
6203
0
{
6204
0
    VALIDATE_POINTER1(hSRS, "OSRGetProjParm", 0);
6205
6206
0
    return ToPointer(hSRS)->GetProjParm(pszName, dfDefaultValue, pnErr);
6207
0
}
6208
6209
/************************************************************************/
6210
/*                          GetNormProjParm()                           */
6211
/************************************************************************/
6212
6213
/**
6214
 * \brief Fetch a normalized projection parameter value.
6215
 *
6216
 * This method is the same as GetProjParm() except that the value of
6217
 * the parameter is "normalized" into degrees or meters depending on
6218
 * whether it is linear or angular.
6219
 *
6220
 * This method is the same as the C function OSRGetNormProjParm().
6221
 *
6222
 * @param pszName the name of the parameter to fetch, from the set of
6223
 * SRS_PP codes in ogr_srs_api.h.
6224
 *
6225
 * @param dfDefaultValue the value to return if this parameter doesn't exist.
6226
 *
6227
 * @param pnErr place to put error code on failure.  Ignored if NULL.
6228
 *
6229
 * @return value of parameter.
6230
 */
6231
6232
double OGRSpatialReference::GetNormProjParm(const char *pszName,
6233
                                            double dfDefaultValue,
6234
                                            OGRErr *pnErr) const
6235
6236
0
{
6237
0
    TAKE_OPTIONAL_LOCK();
6238
6239
0
    GetNormInfo();
6240
6241
0
    OGRErr nError = OGRERR_NONE;
6242
0
    double dfRawResult = GetProjParm(pszName, dfDefaultValue, &nError);
6243
0
    if (pnErr != nullptr)
6244
0
        *pnErr = nError;
6245
6246
    // If we got the default just return it unadjusted.
6247
0
    if (nError != OGRERR_NONE)
6248
0
        return dfRawResult;
6249
6250
0
    if (d->dfToDegrees != 1.0 && IsAngularParameter(pszName))
6251
0
        dfRawResult *= d->dfToDegrees;
6252
6253
0
    if (d->dfToMeter != 1.0 && IsLinearParameter(pszName))
6254
0
        return dfRawResult * d->dfToMeter;
6255
6256
0
    return dfRawResult;
6257
0
}
6258
6259
/************************************************************************/
6260
/*                         OSRGetNormProjParm()                         */
6261
/************************************************************************/
6262
6263
/**
6264
 * \brief This function is the same as OGRSpatialReference::
6265
 *
6266
 * This function is the same as OGRSpatialReference::GetNormProjParm()
6267
 */
6268
double OSRGetNormProjParm(OGRSpatialReferenceH hSRS, const char *pszName,
6269
                          double dfDefaultValue, OGRErr *pnErr)
6270
6271
0
{
6272
0
    VALIDATE_POINTER1(hSRS, "OSRGetNormProjParm", 0);
6273
6274
0
    return ToPointer(hSRS)->GetNormProjParm(pszName, dfDefaultValue, pnErr);
6275
0
}
6276
6277
/************************************************************************/
6278
/*                          SetNormProjParm()                           */
6279
/************************************************************************/
6280
6281
/**
6282
 * \brief Set a projection parameter with a normalized value.
6283
 *
6284
 * This method is the same as SetProjParm() except that the value of
6285
 * the parameter passed in is assumed to be in "normalized" form (decimal
6286
 * degrees for angular values, meters for linear values.  The values are
6287
 * converted in a form suitable for the GEOGCS and linear units in effect.
6288
 *
6289
 * This method is the same as the C function OSRSetNormProjParm().
6290
 *
6291
 * @param pszName the parameter name, which should be selected from
6292
 * the macros in ogr_srs_api.h, such as SRS_PP_CENTRAL_MERIDIAN.
6293
 *
6294
 * @param dfValue value to assign.
6295
 *
6296
 * @return OGRERR_NONE on success.
6297
 */
6298
6299
OGRErr OGRSpatialReference::SetNormProjParm(const char *pszName, double dfValue)
6300
6301
0
{
6302
0
    TAKE_OPTIONAL_LOCK();
6303
6304
0
    GetNormInfo();
6305
6306
0
    if (d->dfToDegrees != 0.0 &&
6307
0
        (d->dfToDegrees != 1.0 || d->dfFromGreenwich != 0.0) &&
6308
0
        IsAngularParameter(pszName))
6309
0
    {
6310
0
        dfValue /= d->dfToDegrees;
6311
0
    }
6312
0
    else if (d->dfToMeter != 1.0 && d->dfToMeter != 0.0 &&
6313
0
             IsLinearParameter(pszName))
6314
0
        dfValue /= d->dfToMeter;
6315
6316
0
    return SetProjParm(pszName, dfValue);
6317
0
}
6318
6319
/************************************************************************/
6320
/*                         OSRSetNormProjParm()                         */
6321
/************************************************************************/
6322
6323
/**
6324
 * \brief Set a projection parameter with a normalized value.
6325
 *
6326
 * This function is the same as OGRSpatialReference::SetNormProjParm()
6327
 */
6328
OGRErr OSRSetNormProjParm(OGRSpatialReferenceH hSRS, const char *pszParamName,
6329
                          double dfValue)
6330
6331
0
{
6332
0
    VALIDATE_POINTER1(hSRS, "OSRSetNormProjParm", OGRERR_FAILURE);
6333
6334
0
    return ToPointer(hSRS)->SetNormProjParm(pszParamName, dfValue);
6335
0
}
6336
6337
/************************************************************************/
6338
/*                               SetTM()                                */
6339
/************************************************************************/
6340
6341
OGRErr OGRSpatialReference::SetTM(double dfCenterLat, double dfCenterLong,
6342
                                  double dfScale, double dfFalseEasting,
6343
                                  double dfFalseNorthing)
6344
6345
0
{
6346
0
    TAKE_OPTIONAL_LOCK();
6347
6348
0
    return d->replaceConversionAndUnref(
6349
0
        proj_create_conversion_transverse_mercator(
6350
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale,
6351
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6352
0
}
6353
6354
/************************************************************************/
6355
/*                              OSRSetTM()                              */
6356
/************************************************************************/
6357
6358
OGRErr OSRSetTM(OGRSpatialReferenceH hSRS, double dfCenterLat,
6359
                double dfCenterLong, double dfScale, double dfFalseEasting,
6360
                double dfFalseNorthing)
6361
6362
0
{
6363
0
    VALIDATE_POINTER1(hSRS, "OSRSetTM", OGRERR_FAILURE);
6364
6365
0
    return ToPointer(hSRS)->SetTM(dfCenterLat, dfCenterLong, dfScale,
6366
0
                                  dfFalseEasting, dfFalseNorthing);
6367
0
}
6368
6369
/************************************************************************/
6370
/*                            SetTMVariant()                            */
6371
/************************************************************************/
6372
6373
OGRErr OGRSpatialReference::SetTMVariant(const char *pszVariantName,
6374
                                         double dfCenterLat,
6375
                                         double dfCenterLong, double dfScale,
6376
                                         double dfFalseEasting,
6377
                                         double dfFalseNorthing)
6378
6379
0
{
6380
0
    TAKE_OPTIONAL_LOCK();
6381
6382
0
    SetProjection(pszVariantName);
6383
0
    SetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, dfCenterLat);
6384
0
    SetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, dfCenterLong);
6385
0
    SetNormProjParm(SRS_PP_SCALE_FACTOR, dfScale);
6386
0
    SetNormProjParm(SRS_PP_FALSE_EASTING, dfFalseEasting);
6387
0
    SetNormProjParm(SRS_PP_FALSE_NORTHING, dfFalseNorthing);
6388
6389
0
    return OGRERR_NONE;
6390
0
}
6391
6392
/************************************************************************/
6393
/*                          OSRSetTMVariant()                           */
6394
/************************************************************************/
6395
6396
OGRErr OSRSetTMVariant(OGRSpatialReferenceH hSRS, const char *pszVariantName,
6397
                       double dfCenterLat, double dfCenterLong, double dfScale,
6398
                       double dfFalseEasting, double dfFalseNorthing)
6399
6400
0
{
6401
0
    VALIDATE_POINTER1(hSRS, "OSRSetTMVariant", OGRERR_FAILURE);
6402
6403
0
    return ToPointer(hSRS)->SetTMVariant(pszVariantName, dfCenterLat,
6404
0
                                         dfCenterLong, dfScale, dfFalseEasting,
6405
0
                                         dfFalseNorthing);
6406
0
}
6407
6408
/************************************************************************/
6409
/*                              SetTMSO()                               */
6410
/************************************************************************/
6411
6412
OGRErr OGRSpatialReference::SetTMSO(double dfCenterLat, double dfCenterLong,
6413
                                    double dfScale, double dfFalseEasting,
6414
                                    double dfFalseNorthing)
6415
6416
0
{
6417
0
    TAKE_OPTIONAL_LOCK();
6418
6419
0
    auto conv = proj_create_conversion_transverse_mercator_south_oriented(
6420
0
        d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale, dfFalseEasting,
6421
0
        dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6422
6423
0
    const char *pszName = nullptr;
6424
0
    double dfConvFactor = GetTargetLinearUnits(nullptr, &pszName);
6425
0
    CPLString osName = pszName ? pszName : "";
6426
6427
0
    d->refreshProjObj();
6428
6429
0
    d->demoteFromBoundCRS();
6430
6431
0
    auto cs = proj_create_cartesian_2D_cs(
6432
0
        d->getPROJContext(), PJ_CART2D_WESTING_SOUTHING,
6433
0
        !osName.empty() ? osName.c_str() : nullptr, dfConvFactor);
6434
0
    auto projCRS =
6435
0
        proj_create_projected_crs(d->getPROJContext(), d->getProjCRSName(),
6436
0
                                  d->getGeodBaseCRS(), conv, cs);
6437
0
    proj_destroy(conv);
6438
0
    proj_destroy(cs);
6439
6440
0
    d->setPjCRS(projCRS);
6441
6442
0
    d->undoDemoteFromBoundCRS();
6443
6444
0
    return OGRERR_NONE;
6445
0
}
6446
6447
/************************************************************************/
6448
/*                             OSRSetTMSO()                             */
6449
/************************************************************************/
6450
6451
OGRErr OSRSetTMSO(OGRSpatialReferenceH hSRS, double dfCenterLat,
6452
                  double dfCenterLong, double dfScale, double dfFalseEasting,
6453
                  double dfFalseNorthing)
6454
6455
0
{
6456
0
    VALIDATE_POINTER1(hSRS, "OSRSetTMSO", OGRERR_FAILURE);
6457
6458
0
    return ToPointer(hSRS)->SetTMSO(dfCenterLat, dfCenterLong, dfScale,
6459
0
                                    dfFalseEasting, dfFalseNorthing);
6460
0
}
6461
6462
/************************************************************************/
6463
/*                              SetTPED()                               */
6464
/************************************************************************/
6465
6466
OGRErr OGRSpatialReference::SetTPED(double dfLat1, double dfLong1,
6467
                                    double dfLat2, double dfLong2,
6468
                                    double dfFalseEasting,
6469
                                    double dfFalseNorthing)
6470
6471
0
{
6472
0
    TAKE_OPTIONAL_LOCK();
6473
6474
0
    return d->replaceConversionAndUnref(
6475
0
        proj_create_conversion_two_point_equidistant(
6476
0
            d->getPROJContext(), dfLat1, dfLong1, dfLat2, dfLong2,
6477
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6478
0
}
6479
6480
/************************************************************************/
6481
/*                             OSRSetTPED()                             */
6482
/************************************************************************/
6483
6484
OGRErr OSRSetTPED(OGRSpatialReferenceH hSRS, double dfLat1, double dfLong1,
6485
                  double dfLat2, double dfLong2, double dfFalseEasting,
6486
                  double dfFalseNorthing)
6487
6488
0
{
6489
0
    VALIDATE_POINTER1(hSRS, "OSRSetTPED", OGRERR_FAILURE);
6490
6491
0
    return ToPointer(hSRS)->SetTPED(dfLat1, dfLong1, dfLat2, dfLong2,
6492
0
                                    dfFalseEasting, dfFalseNorthing);
6493
0
}
6494
6495
/************************************************************************/
6496
/*                               SetTMG()                               */
6497
/************************************************************************/
6498
6499
OGRErr OGRSpatialReference::SetTMG(double dfCenterLat, double dfCenterLong,
6500
                                   double dfFalseEasting,
6501
                                   double dfFalseNorthing)
6502
6503
0
{
6504
0
    TAKE_OPTIONAL_LOCK();
6505
6506
0
    return d->replaceConversionAndUnref(
6507
0
        proj_create_conversion_tunisia_mapping_grid(
6508
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
6509
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6510
0
}
6511
6512
/************************************************************************/
6513
/*                             OSRSetTMG()                              */
6514
/************************************************************************/
6515
6516
OGRErr OSRSetTMG(OGRSpatialReferenceH hSRS, double dfCenterLat,
6517
                 double dfCenterLong, double dfFalseEasting,
6518
                 double dfFalseNorthing)
6519
6520
0
{
6521
0
    VALIDATE_POINTER1(hSRS, "OSRSetTMG", OGRERR_FAILURE);
6522
6523
0
    return ToPointer(hSRS)->SetTMG(dfCenterLat, dfCenterLong, dfFalseEasting,
6524
0
                                   dfFalseNorthing);
6525
0
}
6526
6527
/************************************************************************/
6528
/*                              SetACEA()                               */
6529
/************************************************************************/
6530
6531
OGRErr OGRSpatialReference::SetACEA(double dfStdP1, double dfStdP2,
6532
                                    double dfCenterLat, double dfCenterLong,
6533
                                    double dfFalseEasting,
6534
                                    double dfFalseNorthing)
6535
6536
0
{
6537
0
    TAKE_OPTIONAL_LOCK();
6538
6539
    // Note different order of parameters. The one in PROJ is conformant with
6540
    // EPSG
6541
0
    return d->replaceConversionAndUnref(
6542
0
        proj_create_conversion_albers_equal_area(
6543
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfStdP1, dfStdP2,
6544
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6545
0
}
6546
6547
/************************************************************************/
6548
/*                             OSRSetACEA()                             */
6549
/************************************************************************/
6550
6551
OGRErr OSRSetACEA(OGRSpatialReferenceH hSRS, double dfStdP1, double dfStdP2,
6552
                  double dfCenterLat, double dfCenterLong,
6553
                  double dfFalseEasting, double dfFalseNorthing)
6554
6555
0
{
6556
0
    VALIDATE_POINTER1(hSRS, "OSRSetACEA", OGRERR_FAILURE);
6557
6558
0
    return ToPointer(hSRS)->SetACEA(dfStdP1, dfStdP2, dfCenterLat, dfCenterLong,
6559
0
                                    dfFalseEasting, dfFalseNorthing);
6560
0
}
6561
6562
/************************************************************************/
6563
/*                               SetAE()                                */
6564
/************************************************************************/
6565
6566
OGRErr OGRSpatialReference::SetAE(double dfCenterLat, double dfCenterLong,
6567
                                  double dfFalseEasting, double dfFalseNorthing)
6568
6569
0
{
6570
0
    TAKE_OPTIONAL_LOCK();
6571
6572
0
    return d->replaceConversionAndUnref(
6573
0
        proj_create_conversion_azimuthal_equidistant(
6574
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
6575
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6576
0
}
6577
6578
/************************************************************************/
6579
/*                              OSRSetAE()                              */
6580
/************************************************************************/
6581
6582
OGRErr OSRSetAE(OGRSpatialReferenceH hSRS, double dfCenterLat,
6583
                double dfCenterLong, double dfFalseEasting,
6584
                double dfFalseNorthing)
6585
6586
0
{
6587
0
    VALIDATE_POINTER1(hSRS, "OSRSetACEA", OGRERR_FAILURE);
6588
6589
0
    return ToPointer(hSRS)->SetAE(dfCenterLat, dfCenterLong, dfFalseEasting,
6590
0
                                  dfFalseNorthing);
6591
0
}
6592
6593
/************************************************************************/
6594
/*                              SetBonne()                              */
6595
/************************************************************************/
6596
6597
OGRErr OGRSpatialReference::SetBonne(double dfStdP1, double dfCentralMeridian,
6598
                                     double dfFalseEasting,
6599
                                     double dfFalseNorthing)
6600
6601
0
{
6602
0
    TAKE_OPTIONAL_LOCK();
6603
6604
0
    return d->replaceConversionAndUnref(proj_create_conversion_bonne(
6605
0
        d->getPROJContext(), dfStdP1, dfCentralMeridian, dfFalseEasting,
6606
0
        dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6607
0
}
6608
6609
/************************************************************************/
6610
/*                            OSRSetBonne()                             */
6611
/************************************************************************/
6612
6613
OGRErr OSRSetBonne(OGRSpatialReferenceH hSRS, double dfStdP1,
6614
                   double dfCentralMeridian, double dfFalseEasting,
6615
                   double dfFalseNorthing)
6616
6617
0
{
6618
0
    VALIDATE_POINTER1(hSRS, "OSRSetBonne", OGRERR_FAILURE);
6619
6620
0
    return ToPointer(hSRS)->SetBonne(dfStdP1, dfCentralMeridian, dfFalseEasting,
6621
0
                                     dfFalseNorthing);
6622
0
}
6623
6624
/************************************************************************/
6625
/*                               SetCEA()                               */
6626
/************************************************************************/
6627
6628
OGRErr OGRSpatialReference::SetCEA(double dfStdP1, double dfCentralMeridian,
6629
                                   double dfFalseEasting,
6630
                                   double dfFalseNorthing)
6631
6632
0
{
6633
0
    TAKE_OPTIONAL_LOCK();
6634
6635
0
    return d->replaceConversionAndUnref(
6636
0
        proj_create_conversion_lambert_cylindrical_equal_area(
6637
0
            d->getPROJContext(), dfStdP1, dfCentralMeridian, dfFalseEasting,
6638
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6639
0
}
6640
6641
/************************************************************************/
6642
/*                             OSRSetCEA()                              */
6643
/************************************************************************/
6644
6645
OGRErr OSRSetCEA(OGRSpatialReferenceH hSRS, double dfStdP1,
6646
                 double dfCentralMeridian, double dfFalseEasting,
6647
                 double dfFalseNorthing)
6648
6649
0
{
6650
0
    VALIDATE_POINTER1(hSRS, "OSRSetCEA", OGRERR_FAILURE);
6651
6652
0
    return ToPointer(hSRS)->SetCEA(dfStdP1, dfCentralMeridian, dfFalseEasting,
6653
0
                                   dfFalseNorthing);
6654
0
}
6655
6656
/************************************************************************/
6657
/*                               SetCS()                                */
6658
/************************************************************************/
6659
6660
OGRErr OGRSpatialReference::SetCS(double dfCenterLat, double dfCenterLong,
6661
                                  double dfFalseEasting, double dfFalseNorthing)
6662
6663
0
{
6664
0
    TAKE_OPTIONAL_LOCK();
6665
6666
0
    return d->replaceConversionAndUnref(proj_create_conversion_cassini_soldner(
6667
0
        d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
6668
0
        dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6669
0
}
6670
6671
/************************************************************************/
6672
/*                              OSRSetCS()                              */
6673
/************************************************************************/
6674
6675
OGRErr OSRSetCS(OGRSpatialReferenceH hSRS, double dfCenterLat,
6676
                double dfCenterLong, double dfFalseEasting,
6677
                double dfFalseNorthing)
6678
6679
0
{
6680
0
    VALIDATE_POINTER1(hSRS, "OSRSetCS", OGRERR_FAILURE);
6681
6682
0
    return ToPointer(hSRS)->SetCS(dfCenterLat, dfCenterLong, dfFalseEasting,
6683
0
                                  dfFalseNorthing);
6684
0
}
6685
6686
/************************************************************************/
6687
/*                               SetEC()                                */
6688
/************************************************************************/
6689
6690
OGRErr OGRSpatialReference::SetEC(double dfStdP1, double dfStdP2,
6691
                                  double dfCenterLat, double dfCenterLong,
6692
                                  double dfFalseEasting, double dfFalseNorthing)
6693
6694
0
{
6695
0
    TAKE_OPTIONAL_LOCK();
6696
6697
    // Note: different order of arguments
6698
0
    return d->replaceConversionAndUnref(
6699
0
        proj_create_conversion_equidistant_conic(
6700
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfStdP1, dfStdP2,
6701
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6702
0
}
6703
6704
/************************************************************************/
6705
/*                              OSRSetEC()                              */
6706
/************************************************************************/
6707
6708
OGRErr OSRSetEC(OGRSpatialReferenceH hSRS, double dfStdP1, double dfStdP2,
6709
                double dfCenterLat, double dfCenterLong, double dfFalseEasting,
6710
                double dfFalseNorthing)
6711
6712
0
{
6713
0
    VALIDATE_POINTER1(hSRS, "OSRSetEC", OGRERR_FAILURE);
6714
6715
0
    return ToPointer(hSRS)->SetEC(dfStdP1, dfStdP2, dfCenterLat, dfCenterLong,
6716
0
                                  dfFalseEasting, dfFalseNorthing);
6717
0
}
6718
6719
/************************************************************************/
6720
/*                             SetEckert()                              */
6721
/************************************************************************/
6722
6723
OGRErr OGRSpatialReference::SetEckert(int nVariation,  // 1-6.
6724
                                      double dfCentralMeridian,
6725
                                      double dfFalseEasting,
6726
                                      double dfFalseNorthing)
6727
6728
0
{
6729
0
    TAKE_OPTIONAL_LOCK();
6730
6731
0
    PJ *conv;
6732
0
    if (nVariation == 1)
6733
0
    {
6734
0
        conv = proj_create_conversion_eckert_i(
6735
0
            d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6736
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6737
0
    }
6738
0
    else if (nVariation == 2)
6739
0
    {
6740
0
        conv = proj_create_conversion_eckert_ii(
6741
0
            d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6742
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6743
0
    }
6744
0
    else if (nVariation == 3)
6745
0
    {
6746
0
        conv = proj_create_conversion_eckert_iii(
6747
0
            d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6748
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6749
0
    }
6750
0
    else if (nVariation == 4)
6751
0
    {
6752
0
        conv = proj_create_conversion_eckert_iv(
6753
0
            d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6754
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6755
0
    }
6756
0
    else if (nVariation == 5)
6757
0
    {
6758
0
        conv = proj_create_conversion_eckert_v(
6759
0
            d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6760
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6761
0
    }
6762
0
    else if (nVariation == 6)
6763
0
    {
6764
0
        conv = proj_create_conversion_eckert_vi(
6765
0
            d->getPROJContext(), dfCentralMeridian, dfFalseEasting,
6766
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
6767
0
    }
6768
0
    else
6769
0
    {
6770
0
        CPLError(CE_Failure, CPLE_AppDefined,
6771
0
                 "Unsupported Eckert variation (%d).", nVariation);
6772
0
        return OGRERR_UNSUPPORTED_SRS;
6773
0
    }
6774
6775
0
    return d->replaceConversionAndUnref(conv);
6776
0
}
6777
6778
/************************************************************************/
6779
/*                            OSRSetEckert()                            */
6780
/************************************************************************/
6781
6782
OGRErr OSRSetEckert(OGRSpatialReferenceH hSRS, int nVariation,
6783
                    double dfCentralMeridian, double dfFalseEasting,
6784
                    double dfFalseNorthing)
6785
6786
0
{
6787
0
    VALIDATE_POINTER1(hSRS, "OSRSetEckert", OGRERR_FAILURE);
6788
6789
0
    return ToPointer(hSRS)->SetEckert(nVariation, dfCentralMeridian,
6790
0
                                      dfFalseEasting, dfFalseNorthing);
6791
0
}
6792
6793
/************************************************************************/
6794
/*                            SetEckertIV()                             */
6795
/*                                                                      */
6796
/*      Deprecated                                                      */
6797
/************************************************************************/
6798
6799
OGRErr OGRSpatialReference::SetEckertIV(double dfCentralMeridian,
6800
                                        double dfFalseEasting,
6801
                                        double dfFalseNorthing)
6802
6803
0
{
6804
0
    return SetEckert(4, dfCentralMeridian, dfFalseEasting, dfFalseNorthing);
6805
0
}
6806
6807
/************************************************************************/
6808
/*                           OSRSetEckertIV()                           */
6809
/************************************************************************/
6810
6811
OGRErr OSRSetEckertIV(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
6812
                      double dfFalseEasting, double dfFalseNorthing)
6813
6814
0
{
6815
0
    VALIDATE_POINTER1(hSRS, "OSRSetEckertIV", OGRERR_FAILURE);
6816
6817
0
    return ToPointer(hSRS)->SetEckertIV(dfCentralMeridian, dfFalseEasting,
6818
0
                                        dfFalseNorthing);
6819
0
}
6820
6821
/************************************************************************/
6822
/*                            SetEckertVI()                             */
6823
/*                                                                      */
6824
/*      Deprecated                                                      */
6825
/************************************************************************/
6826
6827
OGRErr OGRSpatialReference::SetEckertVI(double dfCentralMeridian,
6828
                                        double dfFalseEasting,
6829
                                        double dfFalseNorthing)
6830
6831
0
{
6832
0
    return SetEckert(6, dfCentralMeridian, dfFalseEasting, dfFalseNorthing);
6833
0
}
6834
6835
/************************************************************************/
6836
/*                           OSRSetEckertVI()                           */
6837
/************************************************************************/
6838
6839
OGRErr OSRSetEckertVI(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
6840
                      double dfFalseEasting, double dfFalseNorthing)
6841
6842
0
{
6843
0
    VALIDATE_POINTER1(hSRS, "OSRSetEckertVI", OGRERR_FAILURE);
6844
6845
0
    return ToPointer(hSRS)->SetEckertVI(dfCentralMeridian, dfFalseEasting,
6846
0
                                        dfFalseNorthing);
6847
0
}
6848
6849
/************************************************************************/
6850
/*                         SetEquirectangular()                         */
6851
/************************************************************************/
6852
6853
OGRErr OGRSpatialReference::SetEquirectangular(double dfCenterLat,
6854
                                               double dfCenterLong,
6855
                                               double dfFalseEasting,
6856
                                               double dfFalseNorthing)
6857
6858
0
{
6859
0
    TAKE_OPTIONAL_LOCK();
6860
6861
0
    if (dfCenterLat == 0.0)
6862
0
    {
6863
0
        return d->replaceConversionAndUnref(
6864
0
            proj_create_conversion_equidistant_cylindrical(
6865
0
                d->getPROJContext(), 0.0, dfCenterLong, dfFalseEasting,
6866
0
                dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6867
0
    }
6868
6869
    // Non-standard extension with non-zero latitude of origin
6870
0
    SetProjection(SRS_PT_EQUIRECTANGULAR);
6871
0
    SetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, dfCenterLat);
6872
0
    SetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, dfCenterLong);
6873
0
    SetNormProjParm(SRS_PP_FALSE_EASTING, dfFalseEasting);
6874
0
    SetNormProjParm(SRS_PP_FALSE_NORTHING, dfFalseNorthing);
6875
6876
0
    return OGRERR_NONE;
6877
0
}
6878
6879
/************************************************************************/
6880
/*                       OSRSetEquirectangular()                        */
6881
/************************************************************************/
6882
6883
OGRErr OSRSetEquirectangular(OGRSpatialReferenceH hSRS, double dfCenterLat,
6884
                             double dfCenterLong, double dfFalseEasting,
6885
                             double dfFalseNorthing)
6886
6887
0
{
6888
0
    VALIDATE_POINTER1(hSRS, "OSRSetEquirectangular", OGRERR_FAILURE);
6889
6890
0
    return ToPointer(hSRS)->SetEquirectangular(dfCenterLat, dfCenterLong,
6891
0
                                               dfFalseEasting, dfFalseNorthing);
6892
0
}
6893
6894
/************************************************************************/
6895
/*                         SetEquirectangular2()                        */
6896
/* Generalized form                                                     */
6897
/************************************************************************/
6898
6899
OGRErr OGRSpatialReference::SetEquirectangular2(double dfCenterLat,
6900
                                                double dfCenterLong,
6901
                                                double dfStdParallel1,
6902
                                                double dfFalseEasting,
6903
                                                double dfFalseNorthing)
6904
6905
0
{
6906
0
    TAKE_OPTIONAL_LOCK();
6907
6908
0
    if (dfCenterLat == 0.0)
6909
0
    {
6910
0
        return d->replaceConversionAndUnref(
6911
0
            proj_create_conversion_equidistant_cylindrical(
6912
0
                d->getPROJContext(), dfStdParallel1, dfCenterLong,
6913
0
                dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
6914
0
    }
6915
6916
    // Non-standard extension with non-zero latitude of origin
6917
0
    SetProjection(SRS_PT_EQUIRECTANGULAR);
6918
0
    SetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, dfCenterLat);
6919
0
    SetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, dfCenterLong);
6920
0
    SetNormProjParm(SRS_PP_STANDARD_PARALLEL_1, dfStdParallel1);
6921
0
    SetNormProjParm(SRS_PP_FALSE_EASTING, dfFalseEasting);
6922
0
    SetNormProjParm(SRS_PP_FALSE_NORTHING, dfFalseNorthing);
6923
6924
0
    return OGRERR_NONE;
6925
0
}
6926
6927
/************************************************************************/
6928
/*                       OSRSetEquirectangular2()                       */
6929
/************************************************************************/
6930
6931
OGRErr OSRSetEquirectangular2(OGRSpatialReferenceH hSRS, double dfCenterLat,
6932
                              double dfCenterLong, double dfStdParallel1,
6933
                              double dfFalseEasting, double dfFalseNorthing)
6934
6935
0
{
6936
0
    VALIDATE_POINTER1(hSRS, "OSRSetEquirectangular2", OGRERR_FAILURE);
6937
6938
0
    return ToPointer(hSRS)->SetEquirectangular2(dfCenterLat, dfCenterLong,
6939
0
                                                dfStdParallel1, dfFalseEasting,
6940
0
                                                dfFalseNorthing);
6941
0
}
6942
6943
/************************************************************************/
6944
/*                               SetGS()                                */
6945
/************************************************************************/
6946
6947
OGRErr OGRSpatialReference::SetGS(double dfCentralMeridian,
6948
                                  double dfFalseEasting, double dfFalseNorthing)
6949
6950
0
{
6951
0
    return d->replaceConversionAndUnref(proj_create_conversion_gall(
6952
0
        d->getPROJContext(), dfCentralMeridian, dfFalseEasting, dfFalseNorthing,
6953
0
        nullptr, 0.0, nullptr, 0.0));
6954
0
}
6955
6956
/************************************************************************/
6957
/*                              OSRSetGS()                              */
6958
/************************************************************************/
6959
6960
OGRErr OSRSetGS(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
6961
                double dfFalseEasting, double dfFalseNorthing)
6962
6963
0
{
6964
0
    VALIDATE_POINTER1(hSRS, "OSRSetGS", OGRERR_FAILURE);
6965
6966
0
    return ToPointer(hSRS)->SetGS(dfCentralMeridian, dfFalseEasting,
6967
0
                                  dfFalseNorthing);
6968
0
}
6969
6970
/************************************************************************/
6971
/*                               SetGH()                                */
6972
/************************************************************************/
6973
6974
OGRErr OGRSpatialReference::SetGH(double dfCentralMeridian,
6975
                                  double dfFalseEasting, double dfFalseNorthing)
6976
6977
0
{
6978
0
    TAKE_OPTIONAL_LOCK();
6979
6980
0
    return d->replaceConversionAndUnref(proj_create_conversion_goode_homolosine(
6981
0
        d->getPROJContext(), dfCentralMeridian, dfFalseEasting, dfFalseNorthing,
6982
0
        nullptr, 0.0, nullptr, 0.0));
6983
0
}
6984
6985
/************************************************************************/
6986
/*                              OSRSetGH()                              */
6987
/************************************************************************/
6988
6989
OGRErr OSRSetGH(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
6990
                double dfFalseEasting, double dfFalseNorthing)
6991
6992
0
{
6993
0
    VALIDATE_POINTER1(hSRS, "OSRSetGH", OGRERR_FAILURE);
6994
6995
0
    return ToPointer(hSRS)->SetGH(dfCentralMeridian, dfFalseEasting,
6996
0
                                  dfFalseNorthing);
6997
0
}
6998
6999
/************************************************************************/
7000
/*                              SetIGH()                                */
7001
/************************************************************************/
7002
7003
OGRErr OGRSpatialReference::SetIGH()
7004
7005
0
{
7006
0
    TAKE_OPTIONAL_LOCK();
7007
7008
0
    return d->replaceConversionAndUnref(
7009
0
        proj_create_conversion_interrupted_goode_homolosine(
7010
0
            d->getPROJContext(), 0.0, 0.0, 0.0, nullptr, 0.0, nullptr, 0.0));
7011
0
}
7012
7013
/************************************************************************/
7014
/*                              OSRSetIGH()                             */
7015
/************************************************************************/
7016
7017
OGRErr OSRSetIGH(OGRSpatialReferenceH hSRS)
7018
7019
0
{
7020
0
    VALIDATE_POINTER1(hSRS, "OSRSetIGH", OGRERR_FAILURE);
7021
7022
0
    return ToPointer(hSRS)->SetIGH();
7023
0
}
7024
7025
/************************************************************************/
7026
/*                              SetGEOS()                               */
7027
/************************************************************************/
7028
7029
OGRErr OGRSpatialReference::SetGEOS(double dfCentralMeridian,
7030
                                    double dfSatelliteHeight,
7031
                                    double dfFalseEasting,
7032
                                    double dfFalseNorthing)
7033
7034
0
{
7035
0
    TAKE_OPTIONAL_LOCK();
7036
7037
0
    return d->replaceConversionAndUnref(
7038
0
        proj_create_conversion_geostationary_satellite_sweep_y(
7039
0
            d->getPROJContext(), dfCentralMeridian, dfSatelliteHeight,
7040
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
7041
0
}
7042
7043
/************************************************************************/
7044
/*                              OSRSetGEOS()                             */
7045
/************************************************************************/
7046
7047
OGRErr OSRSetGEOS(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
7048
                  double dfSatelliteHeight, double dfFalseEasting,
7049
                  double dfFalseNorthing)
7050
7051
0
{
7052
0
    VALIDATE_POINTER1(hSRS, "OSRSetGEOS", OGRERR_FAILURE);
7053
7054
0
    return ToPointer(hSRS)->SetGEOS(dfCentralMeridian, dfSatelliteHeight,
7055
0
                                    dfFalseEasting, dfFalseNorthing);
7056
0
}
7057
7058
/************************************************************************/
7059
/*                       SetGaussSchreiberTMercator()                   */
7060
/************************************************************************/
7061
7062
OGRErr OGRSpatialReference::SetGaussSchreiberTMercator(double dfCenterLat,
7063
                                                       double dfCenterLong,
7064
                                                       double dfScale,
7065
                                                       double dfFalseEasting,
7066
                                                       double dfFalseNorthing)
7067
7068
0
{
7069
0
    TAKE_OPTIONAL_LOCK();
7070
7071
0
    return d->replaceConversionAndUnref(
7072
0
        proj_create_conversion_gauss_schreiber_transverse_mercator(
7073
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale,
7074
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
7075
0
}
7076
7077
/************************************************************************/
7078
/*                     OSRSetGaussSchreiberTMercator()                  */
7079
/************************************************************************/
7080
7081
OGRErr OSRSetGaussSchreiberTMercator(OGRSpatialReferenceH hSRS,
7082
                                     double dfCenterLat, double dfCenterLong,
7083
                                     double dfScale, double dfFalseEasting,
7084
                                     double dfFalseNorthing)
7085
7086
0
{
7087
0
    VALIDATE_POINTER1(hSRS, "OSRSetGaussSchreiberTMercator", OGRERR_FAILURE);
7088
7089
0
    return ToPointer(hSRS)->SetGaussSchreiberTMercator(
7090
0
        dfCenterLat, dfCenterLong, dfScale, dfFalseEasting, dfFalseNorthing);
7091
0
}
7092
7093
/************************************************************************/
7094
/*                            SetGnomonic()                             */
7095
/************************************************************************/
7096
7097
OGRErr OGRSpatialReference::SetGnomonic(double dfCenterLat, double dfCenterLong,
7098
                                        double dfFalseEasting,
7099
                                        double dfFalseNorthing)
7100
7101
0
{
7102
0
    TAKE_OPTIONAL_LOCK();
7103
7104
0
    return d->replaceConversionAndUnref(proj_create_conversion_gnomonic(
7105
0
        d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7106
0
        dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
7107
0
}
7108
7109
/************************************************************************/
7110
/*                           OSRSetGnomonic()                           */
7111
/************************************************************************/
7112
7113
OGRErr OSRSetGnomonic(OGRSpatialReferenceH hSRS, double dfCenterLat,
7114
                      double dfCenterLong, double dfFalseEasting,
7115
                      double dfFalseNorthing)
7116
7117
0
{
7118
0
    VALIDATE_POINTER1(hSRS, "OSRSetGnomonic", OGRERR_FAILURE);
7119
7120
0
    return ToPointer(hSRS)->SetGnomonic(dfCenterLat, dfCenterLong,
7121
0
                                        dfFalseEasting, dfFalseNorthing);
7122
0
}
7123
7124
/************************************************************************/
7125
/*                              SetHOMAC()                              */
7126
/************************************************************************/
7127
7128
/**
7129
 * \brief Set an Hotine Oblique Mercator Azimuth Center projection using
7130
 * azimuth angle.
7131
 *
7132
 * This projection corresponds to EPSG projection method 9815, also
7133
 * sometimes known as hotine oblique mercator (variant B).
7134
 *
7135
 * This method does the same thing as the C function OSRSetHOMAC().
7136
 *
7137
 * @param dfCenterLat Latitude of the projection origin.
7138
 * @param dfCenterLong Longitude of the projection origin.
7139
 * @param dfAzimuth Azimuth, measured clockwise from North, of the projection
7140
 * centerline.
7141
 * @param dfRectToSkew Angle from Rectified to Skew Grid
7142
 * @param dfScale Scale factor applies to the projection origin.
7143
 * @param dfFalseEasting False easting.
7144
 * @param dfFalseNorthing False northing.
7145
 *
7146
 * @return OGRERR_NONE on success.
7147
 */
7148
7149
OGRErr OGRSpatialReference::SetHOMAC(double dfCenterLat, double dfCenterLong,
7150
                                     double dfAzimuth, double dfRectToSkew,
7151
                                     double dfScale, double dfFalseEasting,
7152
                                     double dfFalseNorthing)
7153
7154
0
{
7155
0
    TAKE_OPTIONAL_LOCK();
7156
7157
0
    return d->replaceConversionAndUnref(
7158
0
        proj_create_conversion_hotine_oblique_mercator_variant_b(
7159
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfAzimuth,
7160
0
            dfRectToSkew, dfScale, dfFalseEasting, dfFalseNorthing, nullptr,
7161
0
            0.0, nullptr, 0.0));
7162
0
}
7163
7164
/************************************************************************/
7165
/*                            OSRSetHOMAC()                             */
7166
/************************************************************************/
7167
7168
/**
7169
 * \brief Set an Oblique Mercator projection using azimuth angle.
7170
 *
7171
 * This is the same as the C++ method OGRSpatialReference::SetHOMAC()
7172
 */
7173
OGRErr OSRSetHOMAC(OGRSpatialReferenceH hSRS, double dfCenterLat,
7174
                   double dfCenterLong, double dfAzimuth, double dfRectToSkew,
7175
                   double dfScale, double dfFalseEasting,
7176
                   double dfFalseNorthing)
7177
7178
0
{
7179
0
    VALIDATE_POINTER1(hSRS, "OSRSetHOMAC", OGRERR_FAILURE);
7180
7181
0
    return ToPointer(hSRS)->SetHOMAC(dfCenterLat, dfCenterLong, dfAzimuth,
7182
0
                                     dfRectToSkew, dfScale, dfFalseEasting,
7183
0
                                     dfFalseNorthing);
7184
0
}
7185
7186
/************************************************************************/
7187
/*                               SetHOM()                               */
7188
/************************************************************************/
7189
7190
/**
7191
 * \brief Set a Hotine Oblique Mercator projection using azimuth angle.
7192
 *
7193
 * This projection corresponds to EPSG projection method 9812, also
7194
 * sometimes known as hotine oblique mercator (variant A)..
7195
 *
7196
 * This method does the same thing as the C function OSRSetHOM().
7197
 *
7198
 * @param dfCenterLat Latitude of the projection origin.
7199
 * @param dfCenterLong Longitude of the projection origin.
7200
 * @param dfAzimuth Azimuth, measured clockwise from North, of the projection
7201
 * centerline.
7202
 * @param dfRectToSkew Angle from Rectified to Skew Grid
7203
 * @param dfScale Scale factor applies to the projection origin.
7204
 * @param dfFalseEasting False easting.
7205
 * @param dfFalseNorthing False northing.
7206
 *
7207
 * @return OGRERR_NONE on success.
7208
 */
7209
7210
OGRErr OGRSpatialReference::SetHOM(double dfCenterLat, double dfCenterLong,
7211
                                   double dfAzimuth, double dfRectToSkew,
7212
                                   double dfScale, double dfFalseEasting,
7213
                                   double dfFalseNorthing)
7214
7215
0
{
7216
0
    TAKE_OPTIONAL_LOCK();
7217
7218
0
    return d->replaceConversionAndUnref(
7219
0
        proj_create_conversion_hotine_oblique_mercator_variant_a(
7220
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfAzimuth,
7221
0
            dfRectToSkew, dfScale, dfFalseEasting, dfFalseNorthing, nullptr,
7222
0
            0.0, nullptr, 0.0));
7223
0
}
7224
7225
/************************************************************************/
7226
/*                             OSRSetHOM()                              */
7227
/************************************************************************/
7228
/**
7229
 * \brief Set a Hotine Oblique Mercator projection using azimuth angle.
7230
 *
7231
 * This is the same as the C++ method OGRSpatialReference::SetHOM()
7232
 */
7233
OGRErr OSRSetHOM(OGRSpatialReferenceH hSRS, double dfCenterLat,
7234
                 double dfCenterLong, double dfAzimuth, double dfRectToSkew,
7235
                 double dfScale, double dfFalseEasting, double dfFalseNorthing)
7236
7237
0
{
7238
0
    VALIDATE_POINTER1(hSRS, "OSRSetHOM", OGRERR_FAILURE);
7239
7240
0
    return ToPointer(hSRS)->SetHOM(dfCenterLat, dfCenterLong, dfAzimuth,
7241
0
                                   dfRectToSkew, dfScale, dfFalseEasting,
7242
0
                                   dfFalseNorthing);
7243
0
}
7244
7245
/************************************************************************/
7246
/*                             SetHOM2PNO()                             */
7247
/************************************************************************/
7248
7249
/**
7250
 * \brief Set a Hotine Oblique Mercator projection using two points on
7251
 * projection centerline.
7252
 *
7253
 * This method does the same thing as the C function OSRSetHOM2PNO().
7254
 *
7255
 * @param dfCenterLat Latitude of the projection origin.
7256
 * @param dfLat1 Latitude of the first point on center line.
7257
 * @param dfLong1 Longitude of the first point on center line.
7258
 * @param dfLat2 Latitude of the second point on center line.
7259
 * @param dfLong2 Longitude of the second point on center line.
7260
 * @param dfScale Scale factor applies to the projection origin.
7261
 * @param dfFalseEasting False easting.
7262
 * @param dfFalseNorthing False northing.
7263
 *
7264
 * @return OGRERR_NONE on success.
7265
 */
7266
7267
OGRErr OGRSpatialReference::SetHOM2PNO(double dfCenterLat, double dfLat1,
7268
                                       double dfLong1, double dfLat2,
7269
                                       double dfLong2, double dfScale,
7270
                                       double dfFalseEasting,
7271
                                       double dfFalseNorthing)
7272
7273
0
{
7274
0
    TAKE_OPTIONAL_LOCK();
7275
7276
0
    return d->replaceConversionAndUnref(
7277
0
        proj_create_conversion_hotine_oblique_mercator_two_point_natural_origin(
7278
0
            d->getPROJContext(), dfCenterLat, dfLat1, dfLong1, dfLat2, dfLong2,
7279
0
            dfScale, dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr,
7280
0
            0.0));
7281
0
}
7282
7283
/************************************************************************/
7284
/*                           OSRSetHOM2PNO()                            */
7285
/************************************************************************/
7286
/**
7287
 * \brief Set a Hotine Oblique Mercator projection using two points on
7288
 *  projection centerline.
7289
 *
7290
 * This is the same as the C++ method OGRSpatialReference::SetHOM2PNO()
7291
 */
7292
OGRErr OSRSetHOM2PNO(OGRSpatialReferenceH hSRS, double dfCenterLat,
7293
                     double dfLat1, double dfLong1, double dfLat2,
7294
                     double dfLong2, double dfScale, double dfFalseEasting,
7295
                     double dfFalseNorthing)
7296
7297
0
{
7298
0
    VALIDATE_POINTER1(hSRS, "OSRSetHOM2PNO", OGRERR_FAILURE);
7299
7300
0
    return ToPointer(hSRS)->SetHOM2PNO(dfCenterLat, dfLat1, dfLong1, dfLat2,
7301
0
                                       dfLong2, dfScale, dfFalseEasting,
7302
0
                                       dfFalseNorthing);
7303
0
}
7304
7305
/************************************************************************/
7306
/*                               SetLOM()                               */
7307
/************************************************************************/
7308
7309
/**
7310
 * \brief Set a Laborde Oblique Mercator projection.
7311
 *
7312
 * @param dfCenterLat Latitude of the projection origin.
7313
 * @param dfCenterLong Longitude of the projection origin.
7314
 * @param dfAzimuth Azimuth, measured clockwise from North, of the projection
7315
 * centerline.
7316
 * @param dfScale Scale factor on the initial line
7317
 * @param dfFalseEasting False easting.
7318
 * @param dfFalseNorthing False northing.
7319
 *
7320
 * @return OGRERR_NONE on success.
7321
 */
7322
7323
OGRErr OGRSpatialReference::SetLOM(double dfCenterLat, double dfCenterLong,
7324
                                   double dfAzimuth, double dfScale,
7325
                                   double dfFalseEasting,
7326
                                   double dfFalseNorthing)
7327
7328
0
{
7329
0
    TAKE_OPTIONAL_LOCK();
7330
7331
0
    return d->replaceConversionAndUnref(
7332
0
        proj_create_conversion_laborde_oblique_mercator(
7333
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfAzimuth, dfScale,
7334
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
7335
0
}
7336
7337
/************************************************************************/
7338
/*                            SetIWMPolyconic()                         */
7339
/************************************************************************/
7340
7341
OGRErr OGRSpatialReference::SetIWMPolyconic(double dfLat1, double dfLat2,
7342
                                            double dfCenterLong,
7343
                                            double dfFalseEasting,
7344
                                            double dfFalseNorthing)
7345
7346
0
{
7347
0
    TAKE_OPTIONAL_LOCK();
7348
7349
0
    return d->replaceConversionAndUnref(
7350
0
        proj_create_conversion_international_map_world_polyconic(
7351
0
            d->getPROJContext(), dfCenterLong, dfLat1, dfLat2, dfFalseEasting,
7352
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0));
7353
0
}
7354
7355
/************************************************************************/
7356
/*                          OSRSetIWMPolyconic()                        */
7357
/************************************************************************/
7358
7359
OGRErr OSRSetIWMPolyconic(OGRSpatialReferenceH hSRS, double dfLat1,
7360
                          double dfLat2, double dfCenterLong,
7361
                          double dfFalseEasting, double dfFalseNorthing)
7362
7363
0
{
7364
0
    VALIDATE_POINTER1(hSRS, "OSRSetIWMPolyconic", OGRERR_FAILURE);
7365
7366
0
    return ToPointer(hSRS)->SetIWMPolyconic(dfLat1, dfLat2, dfCenterLong,
7367
0
                                            dfFalseEasting, dfFalseNorthing);
7368
0
}
7369
7370
/************************************************************************/
7371
/*                             SetKrovak()                              */
7372
/************************************************************************/
7373
7374
/** Krovak east-north projection.
7375
 *
7376
 * Note that dfAzimuth and dfPseudoStdParallel1 are ignored when exporting
7377
 * to PROJ and should be respectively set to 30.28813972222222 and 78.5
7378
 */
7379
OGRErr OGRSpatialReference::SetKrovak(double dfCenterLat, double dfCenterLong,
7380
                                      double dfAzimuth,
7381
                                      double dfPseudoStdParallel1,
7382
                                      double dfScale, double dfFalseEasting,
7383
                                      double dfFalseNorthing)
7384
7385
0
{
7386
0
    TAKE_OPTIONAL_LOCK();
7387
7388
0
    return d->replaceConversionAndUnref(
7389
0
        proj_create_conversion_krovak_north_oriented(
7390
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfAzimuth,
7391
0
            dfPseudoStdParallel1, dfScale, dfFalseEasting, dfFalseNorthing,
7392
0
            nullptr, 0.0, nullptr, 0.0));
7393
0
}
7394
7395
/************************************************************************/
7396
/*                            OSRSetKrovak()                            */
7397
/************************************************************************/
7398
7399
OGRErr OSRSetKrovak(OGRSpatialReferenceH hSRS, double dfCenterLat,
7400
                    double dfCenterLong, double dfAzimuth,
7401
                    double dfPseudoStdParallel1, double dfScale,
7402
                    double dfFalseEasting, double dfFalseNorthing)
7403
7404
0
{
7405
0
    VALIDATE_POINTER1(hSRS, "OSRSetKrovak", OGRERR_FAILURE);
7406
7407
0
    return ToPointer(hSRS)->SetKrovak(dfCenterLat, dfCenterLong, dfAzimuth,
7408
0
                                      dfPseudoStdParallel1, dfScale,
7409
0
                                      dfFalseEasting, dfFalseNorthing);
7410
0
}
7411
7412
/************************************************************************/
7413
/*                              SetLAEA()                               */
7414
/************************************************************************/
7415
7416
OGRErr OGRSpatialReference::SetLAEA(double dfCenterLat, double dfCenterLong,
7417
                                    double dfFalseEasting,
7418
                                    double dfFalseNorthing)
7419
7420
0
{
7421
0
    TAKE_OPTIONAL_LOCK();
7422
7423
0
    auto conv = proj_create_conversion_lambert_azimuthal_equal_area(
7424
0
        d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7425
0
        dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
7426
7427
0
    const char *pszName = nullptr;
7428
0
    double dfConvFactor = GetTargetLinearUnits(nullptr, &pszName);
7429
0
    CPLString osName = pszName ? pszName : "";
7430
7431
0
    d->refreshProjObj();
7432
7433
0
    d->demoteFromBoundCRS();
7434
7435
0
    auto cs = proj_create_cartesian_2D_cs(
7436
0
        d->getPROJContext(),
7437
0
        std::fabs(dfCenterLat - 90) < 1e-10 && dfCenterLong == 0
7438
0
            ? PJ_CART2D_NORTH_POLE_EASTING_SOUTH_NORTHING_SOUTH
7439
0
        : std::fabs(dfCenterLat - -90) < 1e-10 && dfCenterLong == 0
7440
0
            ? PJ_CART2D_SOUTH_POLE_EASTING_NORTH_NORTHING_NORTH
7441
0
            : PJ_CART2D_EASTING_NORTHING,
7442
0
        !osName.empty() ? osName.c_str() : nullptr, dfConvFactor);
7443
0
    auto projCRS =
7444
0
        proj_create_projected_crs(d->getPROJContext(), d->getProjCRSName(),
7445
0
                                  d->getGeodBaseCRS(), conv, cs);
7446
0
    proj_destroy(conv);
7447
0
    proj_destroy(cs);
7448
7449
0
    d->setPjCRS(projCRS);
7450
7451
0
    d->undoDemoteFromBoundCRS();
7452
7453
0
    return OGRERR_NONE;
7454
0
}
7455
7456
/************************************************************************/
7457
/*                             OSRSetLAEA()                             */
7458
/************************************************************************/
7459
7460
OGRErr OSRSetLAEA(OGRSpatialReferenceH hSRS, double dfCenterLat,
7461
                  double dfCenterLong, double dfFalseEasting,
7462
                  double dfFalseNorthing)
7463
7464
0
{
7465
0
    VALIDATE_POINTER1(hSRS, "OSRSetLAEA", OGRERR_FAILURE);
7466
7467
0
    return ToPointer(hSRS)->SetLAEA(dfCenterLat, dfCenterLong, dfFalseEasting,
7468
0
                                    dfFalseNorthing);
7469
0
}
7470
7471
/************************************************************************/
7472
/*                               SetLCC()                               */
7473
/************************************************************************/
7474
7475
OGRErr OGRSpatialReference::SetLCC(double dfStdP1, double dfStdP2,
7476
                                   double dfCenterLat, double dfCenterLong,
7477
                                   double dfFalseEasting,
7478
                                   double dfFalseNorthing)
7479
7480
0
{
7481
0
    TAKE_OPTIONAL_LOCK();
7482
7483
0
    return d->replaceConversionAndUnref(
7484
0
        proj_create_conversion_lambert_conic_conformal_2sp(
7485
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfStdP1, dfStdP2,
7486
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0));
7487
0
}
7488
7489
/************************************************************************/
7490
/*                             OSRSetLCC()                              */
7491
/************************************************************************/
7492
7493
OGRErr OSRSetLCC(OGRSpatialReferenceH hSRS, double dfStdP1, double dfStdP2,
7494
                 double dfCenterLat, double dfCenterLong, double dfFalseEasting,
7495
                 double dfFalseNorthing)
7496
7497
0
{
7498
0
    VALIDATE_POINTER1(hSRS, "OSRSetLCC", OGRERR_FAILURE);
7499
7500
0
    return ToPointer(hSRS)->SetLCC(dfStdP1, dfStdP2, dfCenterLat, dfCenterLong,
7501
0
                                   dfFalseEasting, dfFalseNorthing);
7502
0
}
7503
7504
/************************************************************************/
7505
/*                             SetLCC1SP()                              */
7506
/************************************************************************/
7507
7508
OGRErr OGRSpatialReference::SetLCC1SP(double dfCenterLat, double dfCenterLong,
7509
                                      double dfScale, double dfFalseEasting,
7510
                                      double dfFalseNorthing)
7511
7512
0
{
7513
0
    TAKE_OPTIONAL_LOCK();
7514
7515
0
    return d->replaceConversionAndUnref(
7516
0
        proj_create_conversion_lambert_conic_conformal_1sp(
7517
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale,
7518
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0));
7519
0
}
7520
7521
/************************************************************************/
7522
/*                            OSRSetLCC1SP()                            */
7523
/************************************************************************/
7524
7525
OGRErr OSRSetLCC1SP(OGRSpatialReferenceH hSRS, double dfCenterLat,
7526
                    double dfCenterLong, double dfScale, double dfFalseEasting,
7527
                    double dfFalseNorthing)
7528
7529
0
{
7530
0
    VALIDATE_POINTER1(hSRS, "OSRSetLCC1SP", OGRERR_FAILURE);
7531
7532
0
    return ToPointer(hSRS)->SetLCC1SP(dfCenterLat, dfCenterLong, dfScale,
7533
0
                                      dfFalseEasting, dfFalseNorthing);
7534
0
}
7535
7536
/************************************************************************/
7537
/*                              SetLCCB()                               */
7538
/************************************************************************/
7539
7540
OGRErr OGRSpatialReference::SetLCCB(double dfStdP1, double dfStdP2,
7541
                                    double dfCenterLat, double dfCenterLong,
7542
                                    double dfFalseEasting,
7543
                                    double dfFalseNorthing)
7544
7545
0
{
7546
0
    TAKE_OPTIONAL_LOCK();
7547
7548
0
    return d->replaceConversionAndUnref(
7549
0
        proj_create_conversion_lambert_conic_conformal_2sp_belgium(
7550
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfStdP1, dfStdP2,
7551
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0));
7552
0
}
7553
7554
/************************************************************************/
7555
/*                             OSRSetLCCB()                             */
7556
/************************************************************************/
7557
7558
OGRErr OSRSetLCCB(OGRSpatialReferenceH hSRS, double dfStdP1, double dfStdP2,
7559
                  double dfCenterLat, double dfCenterLong,
7560
                  double dfFalseEasting, double dfFalseNorthing)
7561
7562
0
{
7563
0
    VALIDATE_POINTER1(hSRS, "OSRSetLCCB", OGRERR_FAILURE);
7564
7565
0
    return ToPointer(hSRS)->SetLCCB(dfStdP1, dfStdP2, dfCenterLat, dfCenterLong,
7566
0
                                    dfFalseEasting, dfFalseNorthing);
7567
0
}
7568
7569
/************************************************************************/
7570
/*                               SetMC()                                */
7571
/************************************************************************/
7572
7573
OGRErr OGRSpatialReference::SetMC(double dfCenterLat, double dfCenterLong,
7574
                                  double dfFalseEasting, double dfFalseNorthing)
7575
7576
0
{
7577
0
    TAKE_OPTIONAL_LOCK();
7578
7579
0
    (void)dfCenterLat;  // ignored
7580
7581
0
    return d->replaceConversionAndUnref(
7582
0
        proj_create_conversion_miller_cylindrical(
7583
0
            d->getPROJContext(), dfCenterLong, dfFalseEasting, dfFalseNorthing,
7584
0
            nullptr, 0, nullptr, 0));
7585
0
}
7586
7587
/************************************************************************/
7588
/*                              OSRSetMC()                              */
7589
/************************************************************************/
7590
7591
OGRErr OSRSetMC(OGRSpatialReferenceH hSRS, double dfCenterLat,
7592
                double dfCenterLong, double dfFalseEasting,
7593
                double dfFalseNorthing)
7594
7595
0
{
7596
0
    VALIDATE_POINTER1(hSRS, "OSRSetMC", OGRERR_FAILURE);
7597
7598
0
    return ToPointer(hSRS)->SetMC(dfCenterLat, dfCenterLong, dfFalseEasting,
7599
0
                                  dfFalseNorthing);
7600
0
}
7601
7602
/************************************************************************/
7603
/*                            SetMercator()                             */
7604
/************************************************************************/
7605
7606
OGRErr OGRSpatialReference::SetMercator(double dfCenterLat, double dfCenterLong,
7607
                                        double dfScale, double dfFalseEasting,
7608
                                        double dfFalseNorthing)
7609
7610
0
{
7611
0
    TAKE_OPTIONAL_LOCK();
7612
7613
0
    if (dfCenterLat != 0.0 && dfScale == 1.0)
7614
0
    {
7615
        // Not sure this is correct, but this is how it has been used
7616
        // historically
7617
0
        return SetMercator2SP(dfCenterLat, 0.0, dfCenterLong, dfFalseEasting,
7618
0
                              dfFalseNorthing);
7619
0
    }
7620
0
    return d->replaceConversionAndUnref(
7621
0
        proj_create_conversion_mercator_variant_a(
7622
0
            d->getPROJContext(),
7623
0
            dfCenterLat,  // should be zero
7624
0
            dfCenterLong, dfScale, dfFalseEasting, dfFalseNorthing, nullptr, 0,
7625
0
            nullptr, 0));
7626
0
}
7627
7628
/************************************************************************/
7629
/*                           OSRSetMercator()                           */
7630
/************************************************************************/
7631
7632
OGRErr OSRSetMercator(OGRSpatialReferenceH hSRS, double dfCenterLat,
7633
                      double dfCenterLong, double dfScale,
7634
                      double dfFalseEasting, double dfFalseNorthing)
7635
7636
0
{
7637
0
    VALIDATE_POINTER1(hSRS, "OSRSetMercator", OGRERR_FAILURE);
7638
7639
0
    return ToPointer(hSRS)->SetMercator(dfCenterLat, dfCenterLong, dfScale,
7640
0
                                        dfFalseEasting, dfFalseNorthing);
7641
0
}
7642
7643
/************************************************************************/
7644
/*                           SetMercator2SP()                           */
7645
/************************************************************************/
7646
7647
OGRErr OGRSpatialReference::SetMercator2SP(double dfStdP1, double dfCenterLat,
7648
                                           double dfCenterLong,
7649
                                           double dfFalseEasting,
7650
                                           double dfFalseNorthing)
7651
7652
0
{
7653
0
    if (dfCenterLat == 0.0)
7654
0
    {
7655
0
        return d->replaceConversionAndUnref(
7656
0
            proj_create_conversion_mercator_variant_b(
7657
0
                d->getPROJContext(), dfStdP1, dfCenterLong, dfFalseEasting,
7658
0
                dfFalseNorthing, nullptr, 0, nullptr, 0));
7659
0
    }
7660
7661
0
    TAKE_OPTIONAL_LOCK();
7662
7663
0
    SetProjection(SRS_PT_MERCATOR_2SP);
7664
7665
0
    SetNormProjParm(SRS_PP_STANDARD_PARALLEL_1, dfStdP1);
7666
0
    SetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, dfCenterLat);
7667
0
    SetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, dfCenterLong);
7668
0
    SetNormProjParm(SRS_PP_FALSE_EASTING, dfFalseEasting);
7669
0
    SetNormProjParm(SRS_PP_FALSE_NORTHING, dfFalseNorthing);
7670
7671
0
    return OGRERR_NONE;
7672
0
}
7673
7674
/************************************************************************/
7675
/*                         OSRSetMercator2SP()                          */
7676
/************************************************************************/
7677
7678
OGRErr OSRSetMercator2SP(OGRSpatialReferenceH hSRS, double dfStdP1,
7679
                         double dfCenterLat, double dfCenterLong,
7680
                         double dfFalseEasting, double dfFalseNorthing)
7681
7682
0
{
7683
0
    VALIDATE_POINTER1(hSRS, "OSRSetMercator2SP", OGRERR_FAILURE);
7684
7685
0
    return ToPointer(hSRS)->SetMercator2SP(dfStdP1, dfCenterLat, dfCenterLong,
7686
0
                                           dfFalseEasting, dfFalseNorthing);
7687
0
}
7688
7689
/************************************************************************/
7690
/*                            SetMollweide()                            */
7691
/************************************************************************/
7692
7693
OGRErr OGRSpatialReference::SetMollweide(double dfCentralMeridian,
7694
                                         double dfFalseEasting,
7695
                                         double dfFalseNorthing)
7696
7697
0
{
7698
0
    TAKE_OPTIONAL_LOCK();
7699
7700
0
    return d->replaceConversionAndUnref(proj_create_conversion_mollweide(
7701
0
        d->getPROJContext(), dfCentralMeridian, dfFalseEasting, dfFalseNorthing,
7702
0
        nullptr, 0, nullptr, 0));
7703
0
}
7704
7705
/************************************************************************/
7706
/*                          OSRSetMollweide()                           */
7707
/************************************************************************/
7708
7709
OGRErr OSRSetMollweide(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
7710
                       double dfFalseEasting, double dfFalseNorthing)
7711
7712
0
{
7713
0
    VALIDATE_POINTER1(hSRS, "OSRSetMollweide", OGRERR_FAILURE);
7714
7715
0
    return ToPointer(hSRS)->SetMollweide(dfCentralMeridian, dfFalseEasting,
7716
0
                                         dfFalseNorthing);
7717
0
}
7718
7719
/************************************************************************/
7720
/*                              SetNZMG()                               */
7721
/************************************************************************/
7722
7723
OGRErr OGRSpatialReference::SetNZMG(double dfCenterLat, double dfCenterLong,
7724
                                    double dfFalseEasting,
7725
                                    double dfFalseNorthing)
7726
7727
0
{
7728
0
    TAKE_OPTIONAL_LOCK();
7729
7730
0
    return d->replaceConversionAndUnref(
7731
0
        proj_create_conversion_new_zealand_mapping_grid(
7732
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7733
0
            dfFalseNorthing, nullptr, 0, nullptr, 0));
7734
0
}
7735
7736
/************************************************************************/
7737
/*                             OSRSetNZMG()                             */
7738
/************************************************************************/
7739
7740
OGRErr OSRSetNZMG(OGRSpatialReferenceH hSRS, double dfCenterLat,
7741
                  double dfCenterLong, double dfFalseEasting,
7742
                  double dfFalseNorthing)
7743
7744
0
{
7745
0
    VALIDATE_POINTER1(hSRS, "OSRSetNZMG", OGRERR_FAILURE);
7746
7747
0
    return ToPointer(hSRS)->SetNZMG(dfCenterLat, dfCenterLong, dfFalseEasting,
7748
0
                                    dfFalseNorthing);
7749
0
}
7750
7751
/************************************************************************/
7752
/*                               SetOS()                                */
7753
/************************************************************************/
7754
7755
OGRErr OGRSpatialReference::SetOS(double dfOriginLat, double dfCMeridian,
7756
                                  double dfScale, double dfFalseEasting,
7757
                                  double dfFalseNorthing)
7758
7759
0
{
7760
0
    TAKE_OPTIONAL_LOCK();
7761
7762
0
    return d->replaceConversionAndUnref(
7763
0
        proj_create_conversion_oblique_stereographic(
7764
0
            d->getPROJContext(), dfOriginLat, dfCMeridian, dfScale,
7765
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0));
7766
0
}
7767
7768
/************************************************************************/
7769
/*                              OSRSetOS()                              */
7770
/************************************************************************/
7771
7772
OGRErr OSRSetOS(OGRSpatialReferenceH hSRS, double dfOriginLat,
7773
                double dfCMeridian, double dfScale, double dfFalseEasting,
7774
                double dfFalseNorthing)
7775
7776
0
{
7777
0
    VALIDATE_POINTER1(hSRS, "OSRSetOS", OGRERR_FAILURE);
7778
7779
0
    return ToPointer(hSRS)->SetOS(dfOriginLat, dfCMeridian, dfScale,
7780
0
                                  dfFalseEasting, dfFalseNorthing);
7781
0
}
7782
7783
/************************************************************************/
7784
/*                          SetOrthographic()                           */
7785
/************************************************************************/
7786
7787
OGRErr OGRSpatialReference::SetOrthographic(double dfCenterLat,
7788
                                            double dfCenterLong,
7789
                                            double dfFalseEasting,
7790
                                            double dfFalseNorthing)
7791
7792
0
{
7793
0
    TAKE_OPTIONAL_LOCK();
7794
7795
0
    return d->replaceConversionAndUnref(proj_create_conversion_orthographic(
7796
0
        d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7797
0
        dfFalseNorthing, nullptr, 0, nullptr, 0));
7798
0
}
7799
7800
/************************************************************************/
7801
/*                         OSRSetOrthographic()                         */
7802
/************************************************************************/
7803
7804
OGRErr OSRSetOrthographic(OGRSpatialReferenceH hSRS, double dfCenterLat,
7805
                          double dfCenterLong, double dfFalseEasting,
7806
                          double dfFalseNorthing)
7807
7808
0
{
7809
0
    VALIDATE_POINTER1(hSRS, "OSRSetOrthographic", OGRERR_FAILURE);
7810
7811
0
    return ToPointer(hSRS)->SetOrthographic(dfCenterLat, dfCenterLong,
7812
0
                                            dfFalseEasting, dfFalseNorthing);
7813
0
}
7814
7815
/************************************************************************/
7816
/*                            SetPolyconic()                            */
7817
/************************************************************************/
7818
7819
OGRErr OGRSpatialReference::SetPolyconic(double dfCenterLat,
7820
                                         double dfCenterLong,
7821
                                         double dfFalseEasting,
7822
                                         double dfFalseNorthing)
7823
7824
0
{
7825
0
    TAKE_OPTIONAL_LOCK();
7826
7827
    // note: it seems that by some definitions this should include a
7828
    //       scale_factor parameter.
7829
0
    return d->replaceConversionAndUnref(
7830
0
        proj_create_conversion_american_polyconic(
7831
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7832
0
            dfFalseNorthing, nullptr, 0, nullptr, 0));
7833
0
}
7834
7835
/************************************************************************/
7836
/*                          OSRSetPolyconic()                           */
7837
/************************************************************************/
7838
7839
OGRErr OSRSetPolyconic(OGRSpatialReferenceH hSRS, double dfCenterLat,
7840
                       double dfCenterLong, double dfFalseEasting,
7841
                       double dfFalseNorthing)
7842
7843
0
{
7844
0
    VALIDATE_POINTER1(hSRS, "OSRSetPolyconic", OGRERR_FAILURE);
7845
7846
0
    return ToPointer(hSRS)->SetPolyconic(dfCenterLat, dfCenterLong,
7847
0
                                         dfFalseEasting, dfFalseNorthing);
7848
0
}
7849
7850
/************************************************************************/
7851
/*                               SetPS()                                */
7852
/************************************************************************/
7853
7854
/** Sets a Polar Stereographic projection.
7855
 *
7856
 * Two variants are possible:
7857
 * - Polar Stereographic Variant A: dfCenterLat must be +/- 90° and is
7858
 *   interpreted as the latitude of origin, combined with the scale factor
7859
 * - Polar Stereographic Variant B: dfCenterLat is different from +/- 90° and
7860
 *   is interpreted as the latitude of true scale. In that situation, dfScale
7861
 *   must be set to 1 (it is ignored in the projection parameters)
7862
 */
7863
OGRErr OGRSpatialReference::SetPS(double dfCenterLat, double dfCenterLong,
7864
                                  double dfScale, double dfFalseEasting,
7865
                                  double dfFalseNorthing)
7866
7867
0
{
7868
0
    TAKE_OPTIONAL_LOCK();
7869
7870
0
    PJ *conv;
7871
0
    if (dfScale == 1.0 && std::abs(std::abs(dfCenterLat) - 90) > 1e-8)
7872
0
    {
7873
0
        conv = proj_create_conversion_polar_stereographic_variant_b(
7874
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfFalseEasting,
7875
0
            dfFalseNorthing, nullptr, 0, nullptr, 0);
7876
0
    }
7877
0
    else
7878
0
    {
7879
0
        conv = proj_create_conversion_polar_stereographic_variant_a(
7880
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, dfScale,
7881
0
            dfFalseEasting, dfFalseNorthing, nullptr, 0, nullptr, 0);
7882
0
    }
7883
7884
0
    const char *pszName = nullptr;
7885
0
    double dfConvFactor = GetTargetLinearUnits(nullptr, &pszName);
7886
0
    CPLString osName = pszName ? pszName : "";
7887
7888
0
    d->refreshProjObj();
7889
7890
0
    d->demoteFromBoundCRS();
7891
7892
0
    auto cs = proj_create_cartesian_2D_cs(
7893
0
        d->getPROJContext(),
7894
0
        dfCenterLat > 0 ? PJ_CART2D_NORTH_POLE_EASTING_SOUTH_NORTHING_SOUTH
7895
0
                        : PJ_CART2D_SOUTH_POLE_EASTING_NORTH_NORTHING_NORTH,
7896
0
        !osName.empty() ? osName.c_str() : nullptr, dfConvFactor);
7897
0
    auto projCRS =
7898
0
        proj_create_projected_crs(d->getPROJContext(), d->getProjCRSName(),
7899
0
                                  d->getGeodBaseCRS(), conv, cs);
7900
0
    proj_destroy(conv);
7901
0
    proj_destroy(cs);
7902
7903
0
    d->setPjCRS(projCRS);
7904
7905
0
    d->undoDemoteFromBoundCRS();
7906
7907
0
    return OGRERR_NONE;
7908
0
}
7909
7910
/************************************************************************/
7911
/*                              OSRSetPS()                              */
7912
/************************************************************************/
7913
7914
OGRErr OSRSetPS(OGRSpatialReferenceH hSRS, double dfCenterLat,
7915
                double dfCenterLong, double dfScale, double dfFalseEasting,
7916
                double dfFalseNorthing)
7917
7918
0
{
7919
0
    VALIDATE_POINTER1(hSRS, "OSRSetPS", OGRERR_FAILURE);
7920
7921
0
    return ToPointer(hSRS)->SetPS(dfCenterLat, dfCenterLong, dfScale,
7922
0
                                  dfFalseEasting, dfFalseNorthing);
7923
0
}
7924
7925
/************************************************************************/
7926
/*                            SetRobinson()                             */
7927
/************************************************************************/
7928
7929
OGRErr OGRSpatialReference::SetRobinson(double dfCenterLong,
7930
                                        double dfFalseEasting,
7931
                                        double dfFalseNorthing)
7932
7933
0
{
7934
0
    TAKE_OPTIONAL_LOCK();
7935
7936
0
    return d->replaceConversionAndUnref(proj_create_conversion_robinson(
7937
0
        d->getPROJContext(), dfCenterLong, dfFalseEasting, dfFalseNorthing,
7938
0
        nullptr, 0, nullptr, 0));
7939
0
}
7940
7941
/************************************************************************/
7942
/*                           OSRSetRobinson()                           */
7943
/************************************************************************/
7944
7945
OGRErr OSRSetRobinson(OGRSpatialReferenceH hSRS, double dfCenterLong,
7946
                      double dfFalseEasting, double dfFalseNorthing)
7947
7948
0
{
7949
0
    VALIDATE_POINTER1(hSRS, "OSRSetRobinson", OGRERR_FAILURE);
7950
7951
0
    return ToPointer(hSRS)->SetRobinson(dfCenterLong, dfFalseEasting,
7952
0
                                        dfFalseNorthing);
7953
0
}
7954
7955
/************************************************************************/
7956
/*                           SetSinusoidal()                            */
7957
/************************************************************************/
7958
7959
OGRErr OGRSpatialReference::SetSinusoidal(double dfCenterLong,
7960
                                          double dfFalseEasting,
7961
                                          double dfFalseNorthing)
7962
7963
0
{
7964
0
    TAKE_OPTIONAL_LOCK();
7965
7966
0
    return d->replaceConversionAndUnref(proj_create_conversion_sinusoidal(
7967
0
        d->getPROJContext(), dfCenterLong, dfFalseEasting, dfFalseNorthing,
7968
0
        nullptr, 0, nullptr, 0));
7969
0
}
7970
7971
/************************************************************************/
7972
/*                          OSRSetSinusoidal()                          */
7973
/************************************************************************/
7974
7975
OGRErr OSRSetSinusoidal(OGRSpatialReferenceH hSRS, double dfCenterLong,
7976
                        double dfFalseEasting, double dfFalseNorthing)
7977
7978
0
{
7979
0
    VALIDATE_POINTER1(hSRS, "OSRSetSinusoidal", OGRERR_FAILURE);
7980
7981
0
    return ToPointer(hSRS)->SetSinusoidal(dfCenterLong, dfFalseEasting,
7982
0
                                          dfFalseNorthing);
7983
0
}
7984
7985
/************************************************************************/
7986
/*                          SetStereographic()                          */
7987
/************************************************************************/
7988
7989
OGRErr OGRSpatialReference::SetStereographic(double dfOriginLat,
7990
                                             double dfCMeridian, double dfScale,
7991
                                             double dfFalseEasting,
7992
                                             double dfFalseNorthing)
7993
7994
0
{
7995
0
    TAKE_OPTIONAL_LOCK();
7996
7997
0
    return d->replaceConversionAndUnref(proj_create_conversion_stereographic(
7998
0
        d->getPROJContext(), dfOriginLat, dfCMeridian, dfScale, dfFalseEasting,
7999
0
        dfFalseNorthing, nullptr, 0, nullptr, 0));
8000
0
}
8001
8002
/************************************************************************/
8003
/*                        OSRSetStereographic()                         */
8004
/************************************************************************/
8005
8006
OGRErr OSRSetStereographic(OGRSpatialReferenceH hSRS, double dfOriginLat,
8007
                           double dfCMeridian, double dfScale,
8008
                           double dfFalseEasting, double dfFalseNorthing)
8009
8010
0
{
8011
0
    VALIDATE_POINTER1(hSRS, "OSRSetStereographic", OGRERR_FAILURE);
8012
8013
0
    return ToPointer(hSRS)->SetStereographic(dfOriginLat, dfCMeridian, dfScale,
8014
0
                                             dfFalseEasting, dfFalseNorthing);
8015
0
}
8016
8017
/************************************************************************/
8018
/*                               SetSOC()                               */
8019
/*                                                                      */
8020
/*      NOTE: This definition isn't really used in practice any more    */
8021
/*      and should be considered deprecated.  It seems that swiss       */
8022
/*      oblique mercator is now define as Hotine_Oblique_Mercator       */
8023
/*      with an azimuth of 90 and a rectified_grid_angle of 90.  See    */
8024
/*      EPSG:2056 and Bug 423.                                          */
8025
/************************************************************************/
8026
8027
OGRErr OGRSpatialReference::SetSOC(double dfLatitudeOfOrigin,
8028
                                   double dfCentralMeridian,
8029
                                   double dfFalseEasting,
8030
                                   double dfFalseNorthing)
8031
8032
0
{
8033
0
    TAKE_OPTIONAL_LOCK();
8034
8035
0
    return d->replaceConversionAndUnref(
8036
0
        proj_create_conversion_hotine_oblique_mercator_variant_b(
8037
0
            d->getPROJContext(), dfLatitudeOfOrigin, dfCentralMeridian, 90.0,
8038
0
            90.0, 1.0, dfFalseEasting, dfFalseNorthing, nullptr, 0.0, nullptr,
8039
0
            0.0));
8040
#if 0
8041
    SetProjection( SRS_PT_SWISS_OBLIQUE_CYLINDRICAL );
8042
    SetNormProjParm( SRS_PP_LATITUDE_OF_CENTER, dfLatitudeOfOrigin );
8043
    SetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, dfCentralMeridian );
8044
    SetNormProjParm( SRS_PP_FALSE_EASTING, dfFalseEasting );
8045
    SetNormProjParm( SRS_PP_FALSE_NORTHING, dfFalseNorthing );
8046
8047
    return OGRERR_NONE;
8048
#endif
8049
0
}
8050
8051
/************************************************************************/
8052
/*                             OSRSetSOC()                              */
8053
/************************************************************************/
8054
8055
OGRErr OSRSetSOC(OGRSpatialReferenceH hSRS, double dfLatitudeOfOrigin,
8056
                 double dfCentralMeridian, double dfFalseEasting,
8057
                 double dfFalseNorthing)
8058
8059
0
{
8060
0
    VALIDATE_POINTER1(hSRS, "OSRSetSOC", OGRERR_FAILURE);
8061
8062
0
    return ToPointer(hSRS)->SetSOC(dfLatitudeOfOrigin, dfCentralMeridian,
8063
0
                                   dfFalseEasting, dfFalseNorthing);
8064
0
}
8065
8066
/************************************************************************/
8067
/*                               SetVDG()                               */
8068
/************************************************************************/
8069
8070
OGRErr OGRSpatialReference::SetVDG(double dfCMeridian, double dfFalseEasting,
8071
                                   double dfFalseNorthing)
8072
8073
0
{
8074
0
    TAKE_OPTIONAL_LOCK();
8075
8076
0
    return d->replaceConversionAndUnref(proj_create_conversion_van_der_grinten(
8077
0
        d->getPROJContext(), dfCMeridian, dfFalseEasting, dfFalseNorthing,
8078
0
        nullptr, 0, nullptr, 0));
8079
0
}
8080
8081
/************************************************************************/
8082
/*                             OSRSetVDG()                              */
8083
/************************************************************************/
8084
8085
OGRErr OSRSetVDG(OGRSpatialReferenceH hSRS, double dfCentralMeridian,
8086
                 double dfFalseEasting, double dfFalseNorthing)
8087
8088
0
{
8089
0
    VALIDATE_POINTER1(hSRS, "OSRSetVDG", OGRERR_FAILURE);
8090
8091
0
    return ToPointer(hSRS)->SetVDG(dfCentralMeridian, dfFalseEasting,
8092
0
                                   dfFalseNorthing);
8093
0
}
8094
8095
/************************************************************************/
8096
/*                               SetUTM()                               */
8097
/************************************************************************/
8098
8099
/**
8100
 * \brief Set UTM projection definition.
8101
 *
8102
 * This will generate a projection definition with the full set of
8103
 * transverse mercator projection parameters for the given UTM zone.
8104
 * If no PROJCS[] description is set yet, one will be set to look
8105
 * like "UTM Zone %d, {Northern, Southern} Hemisphere".
8106
 *
8107
 * This method is the same as the C function OSRSetUTM().
8108
 *
8109
 * @param nZone UTM zone.
8110
 *
8111
 * @param bNorth TRUE for northern hemisphere, or FALSE for southern
8112
 * hemisphere.
8113
 *
8114
 * @return OGRERR_NONE on success.
8115
 */
8116
8117
OGRErr OGRSpatialReference::SetUTM(int nZone, int bNorth)
8118
8119
0
{
8120
0
    TAKE_OPTIONAL_LOCK();
8121
8122
0
    if (nZone < 0 || nZone > 60)
8123
0
    {
8124
0
        CPLError(CE_Failure, CPLE_AppDefined, "Invalid zone: %d", nZone);
8125
0
        return OGRERR_FAILURE;
8126
0
    }
8127
8128
0
    return d->replaceConversionAndUnref(
8129
0
        proj_create_conversion_utm(d->getPROJContext(), nZone, bNorth));
8130
0
}
8131
8132
/************************************************************************/
8133
/*                             OSRSetUTM()                              */
8134
/************************************************************************/
8135
8136
/**
8137
 * \brief Set UTM projection definition.
8138
 *
8139
 * This is the same as the C++ method OGRSpatialReference::SetUTM()
8140
 */
8141
OGRErr OSRSetUTM(OGRSpatialReferenceH hSRS, int nZone, int bNorth)
8142
8143
0
{
8144
0
    VALIDATE_POINTER1(hSRS, "OSRSetUTM", OGRERR_FAILURE);
8145
8146
0
    return ToPointer(hSRS)->SetUTM(nZone, bNorth);
8147
0
}
8148
8149
/************************************************************************/
8150
/*                             GetUTMZone()                             */
8151
/*                                                                      */
8152
/*      Returns zero if it isn't UTM.                                   */
8153
/************************************************************************/
8154
8155
/**
8156
 * \brief Get utm zone information.
8157
 *
8158
 * This is the same as the C function OSRGetUTMZone().
8159
 *
8160
 * In SWIG bindings (Python, Java, etc) the GetUTMZone() method returns a
8161
 * zone which is negative in the southern hemisphere instead of having the
8162
 * pbNorth flag used in the C and C++ interface.
8163
 *
8164
 * @param pbNorth pointer to in to set to TRUE if northern hemisphere, or
8165
 * FALSE if southern.
8166
 *
8167
 * @return UTM zone number or zero if this isn't a UTM definition.
8168
 */
8169
8170
int OGRSpatialReference::GetUTMZone(int *pbNorth) const
8171
8172
0
{
8173
0
    TAKE_OPTIONAL_LOCK();
8174
8175
0
    if (IsProjected() && GetAxesCount() == 3)
8176
0
    {
8177
0
        OGRSpatialReference *poSRSTmp = Clone();
8178
0
        poSRSTmp->DemoteTo2D(nullptr);
8179
0
        const int nZone = poSRSTmp->GetUTMZone(pbNorth);
8180
0
        delete poSRSTmp;
8181
0
        return nZone;
8182
0
    }
8183
8184
0
    const char *pszProjection = GetAttrValue("PROJECTION");
8185
8186
0
    if (pszProjection == nullptr ||
8187
0
        !EQUAL(pszProjection, SRS_PT_TRANSVERSE_MERCATOR))
8188
0
        return 0;
8189
8190
0
    if (GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0) != 0.0)
8191
0
        return 0;
8192
8193
0
    if (GetProjParm(SRS_PP_SCALE_FACTOR, 1.0) != 0.9996)
8194
0
        return 0;
8195
8196
0
    if (fabs(GetNormProjParm(SRS_PP_FALSE_EASTING, 0.0) - 500000.0) > 0.001)
8197
0
        return 0;
8198
8199
0
    const double dfFalseNorthing = GetNormProjParm(SRS_PP_FALSE_NORTHING, 0.0);
8200
8201
0
    if (dfFalseNorthing != 0.0 && fabs(dfFalseNorthing - 10000000.0) > 0.001)
8202
0
        return 0;
8203
8204
0
    if (pbNorth != nullptr)
8205
0
        *pbNorth = (dfFalseNorthing == 0);
8206
8207
0
    const double dfCentralMeridian =
8208
0
        GetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, 0.0);
8209
0
    const double dfZone = (dfCentralMeridian + 186.0) / 6.0;
8210
8211
0
    if (dfCentralMeridian < -177.00001 || dfCentralMeridian > 177.000001 ||
8212
0
        std::isnan(dfZone) ||
8213
0
        std::abs(dfZone - static_cast<int>(dfZone) - 0.5) > 0.00001)
8214
0
        return 0;
8215
8216
0
    return static_cast<int>(dfZone);
8217
0
}
8218
8219
/************************************************************************/
8220
/*                           OSRGetUTMZone()                            */
8221
/************************************************************************/
8222
8223
/**
8224
 * \brief Get utm zone information.
8225
 *
8226
 * This is the same as the C++ method OGRSpatialReference::GetUTMZone()
8227
 */
8228
int OSRGetUTMZone(OGRSpatialReferenceH hSRS, int *pbNorth)
8229
8230
0
{
8231
0
    VALIDATE_POINTER1(hSRS, "OSRGetUTMZone", 0);
8232
8233
0
    return ToPointer(hSRS)->GetUTMZone(pbNorth);
8234
0
}
8235
8236
/************************************************************************/
8237
/*                             SetWagner()                              */
8238
/************************************************************************/
8239
8240
OGRErr OGRSpatialReference::SetWagner(int nVariation,  // 1--7.
8241
                                      double dfCenterLat, double dfFalseEasting,
8242
                                      double dfFalseNorthing)
8243
8244
0
{
8245
0
    TAKE_OPTIONAL_LOCK();
8246
8247
0
    PJ *conv;
8248
0
    if (nVariation == 1)
8249
0
    {
8250
0
        conv = proj_create_conversion_wagner_i(d->getPROJContext(), 0.0,
8251
0
                                               dfFalseEasting, dfFalseNorthing,
8252
0
                                               nullptr, 0.0, nullptr, 0.0);
8253
0
    }
8254
0
    else if (nVariation == 2)
8255
0
    {
8256
0
        conv = proj_create_conversion_wagner_ii(d->getPROJContext(), 0.0,
8257
0
                                                dfFalseEasting, dfFalseNorthing,
8258
0
                                                nullptr, 0.0, nullptr, 0.0);
8259
0
    }
8260
0
    else if (nVariation == 3)
8261
0
    {
8262
0
        conv = proj_create_conversion_wagner_iii(
8263
0
            d->getPROJContext(), dfCenterLat, 0.0, dfFalseEasting,
8264
0
            dfFalseNorthing, nullptr, 0.0, nullptr, 0.0);
8265
0
    }
8266
0
    else if (nVariation == 4)
8267
0
    {
8268
0
        conv = proj_create_conversion_wagner_iv(d->getPROJContext(), 0.0,
8269
0
                                                dfFalseEasting, dfFalseNorthing,
8270
0
                                                nullptr, 0.0, nullptr, 0.0);
8271
0
    }
8272
0
    else if (nVariation == 5)
8273
0
    {
8274
0
        conv = proj_create_conversion_wagner_v(d->getPROJContext(), 0.0,
8275
0
                                               dfFalseEasting, dfFalseNorthing,
8276
0
                                               nullptr, 0.0, nullptr, 0.0);
8277
0
    }
8278
0
    else if (nVariation == 6)
8279
0
    {
8280
0
        conv = proj_create_conversion_wagner_vi(d->getPROJContext(), 0.0,
8281
0
                                                dfFalseEasting, dfFalseNorthing,
8282
0
                                                nullptr, 0.0, nullptr, 0.0);
8283
0
    }
8284
0
    else if (nVariation == 7)
8285
0
    {
8286
0
        conv = proj_create_conversion_wagner_vii(
8287
0
            d->getPROJContext(), 0.0, dfFalseEasting, dfFalseNorthing, nullptr,
8288
0
            0.0, nullptr, 0.0);
8289
0
    }
8290
0
    else
8291
0
    {
8292
0
        CPLError(CE_Failure, CPLE_AppDefined,
8293
0
                 "Unsupported Wagner variation (%d).", nVariation);
8294
0
        return OGRERR_UNSUPPORTED_SRS;
8295
0
    }
8296
8297
0
    return d->replaceConversionAndUnref(conv);
8298
0
}
8299
8300
/************************************************************************/
8301
/*                            OSRSetWagner()                            */
8302
/************************************************************************/
8303
8304
OGRErr OSRSetWagner(OGRSpatialReferenceH hSRS, int nVariation,
8305
                    double dfCenterLat, double dfFalseEasting,
8306
                    double dfFalseNorthing)
8307
8308
0
{
8309
0
    VALIDATE_POINTER1(hSRS, "OSRSetWagner", OGRERR_FAILURE);
8310
8311
0
    return ToPointer(hSRS)->SetWagner(nVariation, dfCenterLat, dfFalseEasting,
8312
0
                                      dfFalseNorthing);
8313
0
}
8314
8315
/************************************************************************/
8316
/*                            SetQSC()                     */
8317
/************************************************************************/
8318
8319
OGRErr OGRSpatialReference::SetQSC(double dfCenterLat, double dfCenterLong)
8320
0
{
8321
0
    TAKE_OPTIONAL_LOCK();
8322
8323
0
    return d->replaceConversionAndUnref(
8324
0
        proj_create_conversion_quadrilateralized_spherical_cube(
8325
0
            d->getPROJContext(), dfCenterLat, dfCenterLong, 0.0, 0.0, nullptr,
8326
0
            0, nullptr, 0));
8327
0
}
8328
8329
/************************************************************************/
8330
/*                           OSRSetQSC()                   */
8331
/************************************************************************/
8332
8333
OGRErr OSRSetQSC(OGRSpatialReferenceH hSRS, double dfCenterLat,
8334
                 double dfCenterLong)
8335
8336
0
{
8337
0
    VALIDATE_POINTER1(hSRS, "OSRSetQSC", OGRERR_FAILURE);
8338
8339
0
    return ToPointer(hSRS)->SetQSC(dfCenterLat, dfCenterLong);
8340
0
}
8341
8342
/************************************************************************/
8343
/*                            SetSCH()                     */
8344
/************************************************************************/
8345
8346
OGRErr OGRSpatialReference::SetSCH(double dfPegLat, double dfPegLong,
8347
                                   double dfPegHeading, double dfPegHgt)
8348
8349
0
{
8350
0
    TAKE_OPTIONAL_LOCK();
8351
8352
0
    return d->replaceConversionAndUnref(
8353
0
        proj_create_conversion_spherical_cross_track_height(
8354
0
            d->getPROJContext(), dfPegLat, dfPegLong, dfPegHeading, dfPegHgt,
8355
0
            nullptr, 0, nullptr, 0));
8356
0
}
8357
8358
/************************************************************************/
8359
/*                           OSRSetSCH()                   */
8360
/************************************************************************/
8361
8362
OGRErr OSRSetSCH(OGRSpatialReferenceH hSRS, double dfPegLat, double dfPegLong,
8363
                 double dfPegHeading, double dfPegHgt)
8364
8365
0
{
8366
0
    VALIDATE_POINTER1(hSRS, "OSRSetSCH", OGRERR_FAILURE);
8367
8368
0
    return ToPointer(hSRS)->SetSCH(dfPegLat, dfPegLong, dfPegHeading, dfPegHgt);
8369
0
}
8370
8371
/************************************************************************/
8372
/*                         SetVerticalPerspective()                     */
8373
/************************************************************************/
8374
8375
OGRErr OGRSpatialReference::SetVerticalPerspective(
8376
    double dfTopoOriginLat, double dfTopoOriginLon, double dfTopoOriginHeight,
8377
    double dfViewPointHeight, double dfFalseEasting, double dfFalseNorthing)
8378
0
{
8379
0
    TAKE_OPTIONAL_LOCK();
8380
8381
0
    return d->replaceConversionAndUnref(
8382
0
        proj_create_conversion_vertical_perspective(
8383
0
            d->getPROJContext(), dfTopoOriginLat, dfTopoOriginLon,
8384
0
            dfTopoOriginHeight, dfViewPointHeight, dfFalseEasting,
8385
0
            dfFalseNorthing, nullptr, 0, nullptr, 0));
8386
0
}
8387
8388
/************************************************************************/
8389
/*                       OSRSetVerticalPerspective()                    */
8390
/************************************************************************/
8391
8392
OGRErr OSRSetVerticalPerspective(OGRSpatialReferenceH hSRS,
8393
                                 double dfTopoOriginLat, double dfTopoOriginLon,
8394
                                 double dfTopoOriginHeight,
8395
                                 double dfViewPointHeight,
8396
                                 double dfFalseEasting, double dfFalseNorthing)
8397
8398
0
{
8399
0
    VALIDATE_POINTER1(hSRS, "OSRSetVerticalPerspective", OGRERR_FAILURE);
8400
8401
0
    return ToPointer(hSRS)->SetVerticalPerspective(
8402
0
        dfTopoOriginLat, dfTopoOriginLon, dfTopoOriginHeight, dfViewPointHeight,
8403
0
        dfFalseEasting, dfFalseNorthing);
8404
0
}
8405
8406
/************************************************************************/
8407
/*             SetDerivedGeogCRSWithPoleRotationGRIBConvention()        */
8408
/************************************************************************/
8409
8410
OGRErr OGRSpatialReference::SetDerivedGeogCRSWithPoleRotationGRIBConvention(
8411
    const char *pszCRSName, double dfSouthPoleLat, double dfSouthPoleLon,
8412
    double dfAxisRotation)
8413
0
{
8414
0
    TAKE_OPTIONAL_LOCK();
8415
8416
0
    d->refreshProjObj();
8417
0
    if (!d->m_pj_crs)
8418
0
        return OGRERR_FAILURE;
8419
0
    if (d->m_pjType != PJ_TYPE_GEOGRAPHIC_2D_CRS)
8420
0
        return OGRERR_FAILURE;
8421
0
    auto ctxt = d->getPROJContext();
8422
0
    auto conv = proj_create_conversion_pole_rotation_grib_convention(
8423
0
        ctxt, dfSouthPoleLat, dfSouthPoleLon, dfAxisRotation, nullptr, 0);
8424
0
    auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
8425
0
    d->setPjCRS(proj_create_derived_geographic_crs(ctxt, pszCRSName,
8426
0
                                                   d->m_pj_crs, conv, cs));
8427
0
    proj_destroy(conv);
8428
0
    proj_destroy(cs);
8429
0
    return OGRERR_NONE;
8430
0
}
8431
8432
/************************************************************************/
8433
/*         SetDerivedGeogCRSWithPoleRotationNetCDFCFConvention()        */
8434
/************************************************************************/
8435
8436
OGRErr OGRSpatialReference::SetDerivedGeogCRSWithPoleRotationNetCDFCFConvention(
8437
    const char *pszCRSName, double dfGridNorthPoleLat,
8438
    double dfGridNorthPoleLon, double dfNorthPoleGridLon)
8439
0
{
8440
0
    TAKE_OPTIONAL_LOCK();
8441
8442
0
#if PROJ_VERSION_MAJOR > 8 ||                                                  \
8443
0
    (PROJ_VERSION_MAJOR == 8 && PROJ_VERSION_MINOR >= 2)
8444
0
    d->refreshProjObj();
8445
0
    if (!d->m_pj_crs)
8446
0
        return OGRERR_FAILURE;
8447
0
    if (d->m_pjType != PJ_TYPE_GEOGRAPHIC_2D_CRS)
8448
0
        return OGRERR_FAILURE;
8449
0
    auto ctxt = d->getPROJContext();
8450
0
    auto conv = proj_create_conversion_pole_rotation_netcdf_cf_convention(
8451
0
        ctxt, dfGridNorthPoleLat, dfGridNorthPoleLon, dfNorthPoleGridLon,
8452
0
        nullptr, 0);
8453
0
    auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
8454
0
    d->setPjCRS(proj_create_derived_geographic_crs(ctxt, pszCRSName,
8455
0
                                                   d->m_pj_crs, conv, cs));
8456
0
    proj_destroy(conv);
8457
0
    proj_destroy(cs);
8458
0
    return OGRERR_NONE;
8459
#else
8460
    (void)pszCRSName;
8461
    SetProjection("Rotated_pole");
8462
    SetExtension(
8463
        "PROJCS", "PROJ4",
8464
        CPLSPrintf("+proj=ob_tran +o_proj=longlat +lon_0=%.17g +o_lon_p=%.17g "
8465
                   "+o_lat_p=%.17g +a=%.17g +b=%.17g "
8466
                   "+to_meter=0.0174532925199433 "
8467
                   "+wktext",
8468
                   180.0 + dfGridNorthPoleLon, dfNorthPoleGridLon,
8469
                   dfGridNorthPoleLat, GetSemiMajor(nullptr),
8470
                   GetSemiMinor(nullptr)));
8471
    return OGRERR_NONE;
8472
#endif
8473
0
}
8474
8475
/************************************************************************/
8476
/*                            SetAuthority()                            */
8477
/************************************************************************/
8478
8479
/**
8480
 * \brief Set the authority for a node.
8481
 *
8482
 * This method is the same as the C function OSRSetAuthority().
8483
 *
8484
 * @param pszTargetKey the partial or complete path to the node to
8485
 * set an authority on.  i.e. "PROJCS", "GEOGCS" or "GEOGCS|UNIT".
8486
 *
8487
 * @param pszAuthority authority name, such as "EPSG".
8488
 *
8489
 * @param nCode code for value with this authority.
8490
 *
8491
 * @return OGRERR_NONE on success.
8492
 */
8493
8494
OGRErr OGRSpatialReference::SetAuthority(const char *pszTargetKey,
8495
                                         const char *pszAuthority, int nCode)
8496
8497
0
{
8498
0
    TAKE_OPTIONAL_LOCK();
8499
8500
0
    d->refreshProjObj();
8501
0
    pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
8502
8503
0
    if (pszTargetKey == nullptr)
8504
0
    {
8505
0
        if (!d->m_pj_crs)
8506
0
            return OGRERR_FAILURE;
8507
0
        CPLString osCode;
8508
0
        osCode.Printf("%d", nCode);
8509
0
        d->demoteFromBoundCRS();
8510
0
        d->setPjCRS(proj_alter_id(d->getPROJContext(), d->m_pj_crs,
8511
0
                                  pszAuthority, osCode.c_str()));
8512
0
        d->undoDemoteFromBoundCRS();
8513
0
        return OGRERR_NONE;
8514
0
    }
8515
8516
0
    d->demoteFromBoundCRS();
8517
0
    if (d->m_pjType == PJ_TYPE_PROJECTED_CRS && EQUAL(pszTargetKey, "GEOGCS"))
8518
0
    {
8519
0
        CPLString osCode;
8520
0
        osCode.Printf("%d", nCode);
8521
0
        auto newGeogCRS =
8522
0
            proj_alter_id(d->getPROJContext(), d->getGeodBaseCRS(),
8523
0
                          pszAuthority, osCode.c_str());
8524
8525
0
        auto conv =
8526
0
            proj_crs_get_coordoperation(d->getPROJContext(), d->m_pj_crs);
8527
8528
0
        auto projCRS = proj_create_projected_crs(
8529
0
            d->getPROJContext(), d->getProjCRSName(), newGeogCRS, conv,
8530
0
            d->getProjCRSCoordSys());
8531
8532
        // Preserve existing id on the PROJCRS
8533
0
        const char *pszProjCRSAuthName = proj_get_id_auth_name(d->m_pj_crs, 0);
8534
0
        const char *pszProjCRSCode = proj_get_id_code(d->m_pj_crs, 0);
8535
0
        if (pszProjCRSAuthName && pszProjCRSCode)
8536
0
        {
8537
0
            auto projCRSWithId =
8538
0
                proj_alter_id(d->getPROJContext(), projCRS, pszProjCRSAuthName,
8539
0
                              pszProjCRSCode);
8540
0
            proj_destroy(projCRS);
8541
0
            projCRS = projCRSWithId;
8542
0
        }
8543
8544
0
        proj_destroy(newGeogCRS);
8545
0
        proj_destroy(conv);
8546
8547
0
        d->setPjCRS(projCRS);
8548
0
        d->undoDemoteFromBoundCRS();
8549
0
        return OGRERR_NONE;
8550
0
    }
8551
0
    d->undoDemoteFromBoundCRS();
8552
8553
    /* -------------------------------------------------------------------- */
8554
    /*      Find the node below which the authority should be put.          */
8555
    /* -------------------------------------------------------------------- */
8556
0
    OGR_SRSNode *poNode = GetAttrNode(pszTargetKey);
8557
8558
0
    if (poNode == nullptr)
8559
0
        return OGRERR_FAILURE;
8560
8561
    /* -------------------------------------------------------------------- */
8562
    /*      If there is an existing AUTHORITY child blow it away before     */
8563
    /*      trying to set a new one.                                        */
8564
    /* -------------------------------------------------------------------- */
8565
0
    int iOldChild = poNode->FindChild("AUTHORITY");
8566
0
    if (iOldChild != -1)
8567
0
        poNode->DestroyChild(iOldChild);
8568
8569
    /* -------------------------------------------------------------------- */
8570
    /*      Create a new authority node.                                    */
8571
    /* -------------------------------------------------------------------- */
8572
0
    char szCode[32] = {};
8573
8574
0
    snprintf(szCode, sizeof(szCode), "%d", nCode);
8575
8576
0
    OGR_SRSNode *poAuthNode = new OGR_SRSNode("AUTHORITY");
8577
0
    poAuthNode->AddChild(new OGR_SRSNode(pszAuthority));
8578
0
    poAuthNode->AddChild(new OGR_SRSNode(szCode));
8579
8580
0
    poNode->AddChild(poAuthNode);
8581
8582
0
    return OGRERR_NONE;
8583
0
}
8584
8585
/************************************************************************/
8586
/*                          OSRSetAuthority()                           */
8587
/************************************************************************/
8588
8589
/**
8590
 * \brief Set the authority for a node.
8591
 *
8592
 * This function is the same as OGRSpatialReference::SetAuthority().
8593
 */
8594
OGRErr OSRSetAuthority(OGRSpatialReferenceH hSRS, const char *pszTargetKey,
8595
                       const char *pszAuthority, int nCode)
8596
8597
0
{
8598
0
    VALIDATE_POINTER1(hSRS, "OSRSetAuthority", OGRERR_FAILURE);
8599
8600
0
    return ToPointer(hSRS)->SetAuthority(pszTargetKey, pszAuthority, nCode);
8601
0
}
8602
8603
/************************************************************************/
8604
/*                          GetAuthorityCode()                          */
8605
/************************************************************************/
8606
8607
/**
8608
 * \brief Get the authority code for a node.
8609
 *
8610
 * This method is used to query an AUTHORITY[] node from within the
8611
 * WKT tree, and fetch the code value.
8612
 *
8613
 * While in theory values may be non-numeric, for the EPSG authority all
8614
 * code values should be integral.
8615
 *
8616
 * This method is the same as the C function OSRGetAuthorityCode().
8617
 *
8618
 * @param pszTargetKey the partial or complete path to the node to
8619
 * get an authority from.  i.e. "PROJCS", "GEOGCS", "GEOGCS|UNIT" or NULL to
8620
 * search for an authority node on the root element.
8621
 *
8622
 * @return value code from authority node, or NULL on failure.  The value
8623
 * returned is internal and should not be freed or modified.
8624
 */
8625
8626
const char *
8627
OGRSpatialReference::GetAuthorityCode(const char *pszTargetKey) const
8628
8629
0
{
8630
0
    TAKE_OPTIONAL_LOCK();
8631
8632
0
    d->refreshProjObj();
8633
0
    const char *pszInputTargetKey = pszTargetKey;
8634
0
    pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
8635
0
    if (pszTargetKey == nullptr)
8636
0
    {
8637
0
        if (!d->m_pj_crs)
8638
0
        {
8639
0
            return nullptr;
8640
0
        }
8641
0
        d->demoteFromBoundCRS();
8642
0
        auto ret = proj_get_id_code(d->m_pj_crs, 0);
8643
0
        if (ret == nullptr && d->m_pjType == PJ_TYPE_PROJECTED_CRS)
8644
0
        {
8645
0
            auto ctxt = d->getPROJContext();
8646
0
            auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
8647
0
            if (cs)
8648
0
            {
8649
0
                const int axisCount = proj_cs_get_axis_count(ctxt, cs);
8650
0
                proj_destroy(cs);
8651
0
                if (axisCount == 3)
8652
0
                {
8653
                    // This might come from a COMPD_CS with a VERT_DATUM type =
8654
                    // 2002 in which case, using the WKT1 representation will
8655
                    // enable us to recover the EPSG code.
8656
0
                    pszTargetKey = pszInputTargetKey;
8657
0
                }
8658
0
            }
8659
0
        }
8660
0
        d->undoDemoteFromBoundCRS();
8661
0
        if (ret != nullptr || pszTargetKey == nullptr)
8662
0
        {
8663
0
            return ret;
8664
0
        }
8665
0
    }
8666
8667
    // Special key for that context
8668
0
    else if (EQUAL(pszTargetKey, "HORIZCRS") &&
8669
0
             d->m_pjType == PJ_TYPE_COMPOUND_CRS)
8670
0
    {
8671
0
        auto ctxt = d->getPROJContext();
8672
0
        auto crs = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
8673
0
        if (crs)
8674
0
        {
8675
0
            const char *ret = proj_get_id_code(crs, 0);
8676
0
            if (ret)
8677
0
                ret = CPLSPrintf("%s", ret);
8678
0
            proj_destroy(crs);
8679
0
            return ret;
8680
0
        }
8681
0
    }
8682
0
    else if (EQUAL(pszTargetKey, "VERTCRS") &&
8683
0
             d->m_pjType == PJ_TYPE_COMPOUND_CRS)
8684
0
    {
8685
0
        auto ctxt = d->getPROJContext();
8686
0
        auto crs = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 1);
8687
0
        if (crs)
8688
0
        {
8689
0
            const char *ret = proj_get_id_code(crs, 0);
8690
0
            if (ret)
8691
0
                ret = CPLSPrintf("%s", ret);
8692
0
            proj_destroy(crs);
8693
0
            return ret;
8694
0
        }
8695
0
    }
8696
8697
    /* -------------------------------------------------------------------- */
8698
    /*      Find the node below which the authority should be put.          */
8699
    /* -------------------------------------------------------------------- */
8700
0
    const OGR_SRSNode *poNode = GetAttrNode(pszTargetKey);
8701
8702
0
    if (poNode == nullptr)
8703
0
        return nullptr;
8704
8705
    /* -------------------------------------------------------------------- */
8706
    /*      Fetch AUTHORITY child if there is one.                          */
8707
    /* -------------------------------------------------------------------- */
8708
0
    if (poNode->FindChild("AUTHORITY") == -1)
8709
0
        return nullptr;
8710
8711
0
    poNode = poNode->GetChild(poNode->FindChild("AUTHORITY"));
8712
8713
    /* -------------------------------------------------------------------- */
8714
    /*      Create a new authority node.                                    */
8715
    /* -------------------------------------------------------------------- */
8716
0
    if (poNode->GetChildCount() < 2)
8717
0
        return nullptr;
8718
8719
0
    return poNode->GetChild(1)->GetValue();
8720
0
}
8721
8722
/************************************************************************/
8723
/*                          OSRGetAuthorityCode()                       */
8724
/************************************************************************/
8725
8726
/**
8727
 * \brief Get the authority code for a node.
8728
 *
8729
 * This function is the same as OGRSpatialReference::GetAuthorityCode().
8730
 */
8731
const char *OSRGetAuthorityCode(OGRSpatialReferenceH hSRS,
8732
                                const char *pszTargetKey)
8733
8734
0
{
8735
0
    VALIDATE_POINTER1(hSRS, "OSRGetAuthorityCode", nullptr);
8736
8737
0
    return ToPointer(hSRS)->GetAuthorityCode(pszTargetKey);
8738
0
}
8739
8740
/************************************************************************/
8741
/*                          GetAuthorityName()                          */
8742
/************************************************************************/
8743
8744
/**
8745
 * \brief Get the authority name for a node.
8746
 *
8747
 * This method is used to query an AUTHORITY[] node from within the
8748
 * WKT tree, and fetch the authority name value.
8749
 *
8750
 * The most common authority is "EPSG".
8751
 *
8752
 * This method is the same as the C function OSRGetAuthorityName().
8753
 *
8754
 * @param pszTargetKey the partial or complete path to the node to
8755
 * get an authority from.  i.e. "PROJCS", "GEOGCS", "GEOGCS|UNIT" or NULL to
8756
 * search for an authority node on the root element.
8757
 *
8758
 * @return value code from authority node, or NULL on failure. The value
8759
 * returned is internal and should not be freed or modified.
8760
 */
8761
8762
const char *
8763
OGRSpatialReference::GetAuthorityName(const char *pszTargetKey) const
8764
8765
0
{
8766
0
    TAKE_OPTIONAL_LOCK();
8767
8768
0
    d->refreshProjObj();
8769
0
    const char *pszInputTargetKey = pszTargetKey;
8770
0
    pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
8771
0
    if (pszTargetKey == nullptr)
8772
0
    {
8773
0
        if (!d->m_pj_crs)
8774
0
        {
8775
0
            return nullptr;
8776
0
        }
8777
0
        d->demoteFromBoundCRS();
8778
0
        auto ret = proj_get_id_auth_name(d->m_pj_crs, 0);
8779
0
        if (ret == nullptr && d->m_pjType == PJ_TYPE_PROJECTED_CRS)
8780
0
        {
8781
0
            auto ctxt = d->getPROJContext();
8782
0
            auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
8783
0
            if (cs)
8784
0
            {
8785
0
                const int axisCount = proj_cs_get_axis_count(ctxt, cs);
8786
0
                proj_destroy(cs);
8787
0
                if (axisCount == 3)
8788
0
                {
8789
                    // This might come from a COMPD_CS with a VERT_DATUM type =
8790
                    // 2002 in which case, using the WKT1 representation will
8791
                    // enable us to recover the EPSG code.
8792
0
                    pszTargetKey = pszInputTargetKey;
8793
0
                }
8794
0
            }
8795
0
        }
8796
0
        d->undoDemoteFromBoundCRS();
8797
0
        if (ret != nullptr || pszTargetKey == nullptr)
8798
0
        {
8799
0
            return ret;
8800
0
        }
8801
0
    }
8802
8803
    // Special key for that context
8804
0
    else if (EQUAL(pszTargetKey, "HORIZCRS") &&
8805
0
             d->m_pjType == PJ_TYPE_COMPOUND_CRS)
8806
0
    {
8807
0
        auto ctxt = d->getPROJContext();
8808
0
        auto crs = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
8809
0
        if (crs)
8810
0
        {
8811
0
            const char *ret = proj_get_id_auth_name(crs, 0);
8812
0
            if (ret)
8813
0
                ret = CPLSPrintf("%s", ret);
8814
0
            proj_destroy(crs);
8815
0
            return ret;
8816
0
        }
8817
0
    }
8818
0
    else if (EQUAL(pszTargetKey, "VERTCRS") &&
8819
0
             d->m_pjType == PJ_TYPE_COMPOUND_CRS)
8820
0
    {
8821
0
        auto ctxt = d->getPROJContext();
8822
0
        auto crs = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 1);
8823
0
        if (crs)
8824
0
        {
8825
0
            const char *ret = proj_get_id_auth_name(crs, 0);
8826
0
            if (ret)
8827
0
                ret = CPLSPrintf("%s", ret);
8828
0
            proj_destroy(crs);
8829
0
            return ret;
8830
0
        }
8831
0
    }
8832
8833
    /* -------------------------------------------------------------------- */
8834
    /*      Find the node below which the authority should be put.          */
8835
    /* -------------------------------------------------------------------- */
8836
0
    const OGR_SRSNode *poNode = GetAttrNode(pszTargetKey);
8837
8838
0
    if (poNode == nullptr)
8839
0
        return nullptr;
8840
8841
    /* -------------------------------------------------------------------- */
8842
    /*      Fetch AUTHORITY child if there is one.                          */
8843
    /* -------------------------------------------------------------------- */
8844
0
    if (poNode->FindChild("AUTHORITY") == -1)
8845
0
        return nullptr;
8846
8847
0
    poNode = poNode->GetChild(poNode->FindChild("AUTHORITY"));
8848
8849
    /* -------------------------------------------------------------------- */
8850
    /*      Create a new authority node.                                    */
8851
    /* -------------------------------------------------------------------- */
8852
0
    if (poNode->GetChildCount() < 2)
8853
0
        return nullptr;
8854
8855
0
    return poNode->GetChild(0)->GetValue();
8856
0
}
8857
8858
/************************************************************************/
8859
/*                        OSRGetAuthorityName()                         */
8860
/************************************************************************/
8861
8862
/**
8863
 * \brief Get the authority name for a node.
8864
 *
8865
 * This function is the same as OGRSpatialReference::GetAuthorityName().
8866
 */
8867
const char *OSRGetAuthorityName(OGRSpatialReferenceH hSRS,
8868
                                const char *pszTargetKey)
8869
8870
0
{
8871
0
    VALIDATE_POINTER1(hSRS, "OSRGetAuthorityName", nullptr);
8872
8873
0
    return ToPointer(hSRS)->GetAuthorityName(pszTargetKey);
8874
0
}
8875
8876
/************************************************************************/
8877
/*                          GetOGCURN()                                 */
8878
/************************************************************************/
8879
8880
/**
8881
 * \brief Get a OGC URN string describing the CRS, when possible
8882
 *
8883
 * This method assumes that the CRS has a top-level identifier, or is
8884
 * a compound CRS whose horizontal and vertical parts have a top-level
8885
 * identifier.
8886
 *
8887
 * @return a string to free with CPLFree(), or nullptr when no result can be
8888
 * generated
8889
 *
8890
 * @since GDAL 3.5
8891
 */
8892
8893
char *OGRSpatialReference::GetOGCURN() const
8894
8895
0
{
8896
0
    TAKE_OPTIONAL_LOCK();
8897
8898
0
    const char *pszAuthName = GetAuthorityName(nullptr);
8899
0
    const char *pszAuthCode = GetAuthorityCode(nullptr);
8900
0
    if (pszAuthName && pszAuthCode)
8901
0
        return CPLStrdup(
8902
0
            CPLSPrintf("urn:ogc:def:crs:%s::%s", pszAuthName, pszAuthCode));
8903
0
    if (d->m_pjType != PJ_TYPE_COMPOUND_CRS)
8904
0
        return nullptr;
8905
0
    auto horizCRS = proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
8906
0
    auto vertCRS = proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 1);
8907
0
    char *pszRet = nullptr;
8908
0
    if (horizCRS && vertCRS)
8909
0
    {
8910
0
        auto horizAuthName = proj_get_id_auth_name(horizCRS, 0);
8911
0
        auto horizAuthCode = proj_get_id_code(horizCRS, 0);
8912
0
        auto vertAuthName = proj_get_id_auth_name(vertCRS, 0);
8913
0
        auto vertAuthCode = proj_get_id_code(vertCRS, 0);
8914
0
        if (horizAuthName && horizAuthCode && vertAuthName && vertAuthCode)
8915
0
        {
8916
0
            pszRet = CPLStrdup(CPLSPrintf(
8917
0
                "urn:ogc:def:crs,crs:%s::%s,crs:%s::%s", horizAuthName,
8918
0
                horizAuthCode, vertAuthName, vertAuthCode));
8919
0
        }
8920
0
    }
8921
0
    proj_destroy(horizCRS);
8922
0
    proj_destroy(vertCRS);
8923
0
    return pszRet;
8924
0
}
8925
8926
/************************************************************************/
8927
/*                           StripVertical()                            */
8928
/************************************************************************/
8929
8930
/**
8931
 * \brief Convert a compound cs into a horizontal CS.
8932
 *
8933
 * If this SRS is of type COMPD_CS[] then the vertical CS and the root COMPD_CS
8934
 * nodes are stripped resulting and only the horizontal coordinate system
8935
 * portion remains (normally PROJCS, GEOGCS or LOCAL_CS).
8936
 *
8937
 * If this is not a compound coordinate system then nothing is changed.
8938
 *
8939
 * This method is the same as the C function OSRStripVertical().
8940
 *
8941
 */
8942
8943
OGRErr OGRSpatialReference::StripVertical()
8944
8945
0
{
8946
0
    TAKE_OPTIONAL_LOCK();
8947
8948
0
    d->refreshProjObj();
8949
0
    d->demoteFromBoundCRS();
8950
0
    if (!d->m_pj_crs || d->m_pjType != PJ_TYPE_COMPOUND_CRS)
8951
0
    {
8952
0
        d->undoDemoteFromBoundCRS();
8953
0
        return OGRERR_NONE;
8954
0
    }
8955
0
    auto horizCRS = proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
8956
0
    if (!horizCRS)
8957
0
    {
8958
0
        d->undoDemoteFromBoundCRS();
8959
0
        return OGRERR_FAILURE;
8960
0
    }
8961
8962
0
    bool reuseExistingBoundCRS = false;
8963
0
    if (d->m_pj_bound_crs_target)
8964
0
    {
8965
0
        auto type = proj_get_type(d->m_pj_bound_crs_target);
8966
0
        reuseExistingBoundCRS = type == PJ_TYPE_GEOCENTRIC_CRS ||
8967
0
                                type == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
8968
0
                                type == PJ_TYPE_GEOGRAPHIC_3D_CRS;
8969
0
    }
8970
8971
0
    if (reuseExistingBoundCRS)
8972
0
    {
8973
0
        auto newBoundCRS = proj_crs_create_bound_crs(
8974
0
            d->getPROJContext(), horizCRS, d->m_pj_bound_crs_target,
8975
0
            d->m_pj_bound_crs_co);
8976
0
        proj_destroy(horizCRS);
8977
0
        d->undoDemoteFromBoundCRS();
8978
0
        d->setPjCRS(newBoundCRS);
8979
0
    }
8980
0
    else
8981
0
    {
8982
0
        d->undoDemoteFromBoundCRS();
8983
0
        d->setPjCRS(horizCRS);
8984
0
    }
8985
8986
0
    return OGRERR_NONE;
8987
0
}
8988
8989
/************************************************************************/
8990
/*                            OSRStripVertical()                             */
8991
/************************************************************************/
8992
/**
8993
 * \brief Convert a compound cs into a horizontal CS.
8994
 *
8995
 * This function is the same as the C++ method
8996
 * OGRSpatialReference::StripVertical().
8997
 */
8998
OGRErr OSRStripVertical(OGRSpatialReferenceH hSRS)
8999
9000
0
{
9001
0
    VALIDATE_POINTER1(hSRS, "OSRStripVertical", OGRERR_FAILURE);
9002
9003
0
    return OGRSpatialReference::FromHandle(hSRS)->StripVertical();
9004
0
}
9005
9006
/************************************************************************/
9007
/*                   StripTOWGS84IfKnownDatumAndAllowed()               */
9008
/************************************************************************/
9009
9010
/**
9011
 * \brief Remove TOWGS84 information if the CRS has a known horizontal datum
9012
 *        and this is allowed by the user.
9013
 *
9014
 * The default behavior is to remove TOWGS84 information if the CRS has a
9015
 * known horizontal datum. This can be disabled by setting the
9016
 * OSR_STRIP_TOWGS84 configuration option to NO.
9017
 *
9018
 * @return true if TOWGS84 has been removed.
9019
 * @since OGR 3.1.0
9020
 */
9021
9022
bool OGRSpatialReference::StripTOWGS84IfKnownDatumAndAllowed()
9023
0
{
9024
0
    if (CPLTestBool(CPLGetConfigOption("OSR_STRIP_TOWGS84", "YES")))
9025
0
    {
9026
0
        if (StripTOWGS84IfKnownDatum())
9027
0
        {
9028
0
            CPLDebug("OSR", "TOWGS84 information has been removed. "
9029
0
                            "It can be kept by setting the OSR_STRIP_TOWGS84 "
9030
0
                            "configuration option to NO");
9031
0
            return true;
9032
0
        }
9033
0
    }
9034
0
    return false;
9035
0
}
9036
9037
/************************************************************************/
9038
/*                      StripTOWGS84IfKnownDatum()                      */
9039
/************************************************************************/
9040
9041
/**
9042
 * \brief Remove TOWGS84 information if the CRS has a known horizontal datum
9043
 *
9044
 * @return true if TOWGS84 has been removed.
9045
 * @since OGR 3.1.0
9046
 */
9047
9048
bool OGRSpatialReference::StripTOWGS84IfKnownDatum()
9049
9050
0
{
9051
0
    TAKE_OPTIONAL_LOCK();
9052
9053
0
    d->refreshProjObj();
9054
0
    if (!d->m_pj_crs || d->m_pjType != PJ_TYPE_BOUND_CRS)
9055
0
    {
9056
0
        return false;
9057
0
    }
9058
0
    auto ctxt = d->getPROJContext();
9059
0
    auto baseCRS = proj_get_source_crs(ctxt, d->m_pj_crs);
9060
0
    if (proj_get_type(baseCRS) == PJ_TYPE_COMPOUND_CRS)
9061
0
    {
9062
0
        proj_destroy(baseCRS);
9063
0
        return false;
9064
0
    }
9065
9066
    // Known base CRS code ? Return base CRS
9067
0
    const char *pszCode = proj_get_id_code(baseCRS, 0);
9068
0
    if (pszCode)
9069
0
    {
9070
0
        d->setPjCRS(baseCRS);
9071
0
        return true;
9072
0
    }
9073
9074
0
    auto datum = proj_crs_get_datum(ctxt, baseCRS);
9075
0
#if PROJ_VERSION_MAJOR > 7 ||                                                  \
9076
0
    (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
9077
0
    if (datum == nullptr)
9078
0
    {
9079
0
        datum = proj_crs_get_datum_ensemble(ctxt, baseCRS);
9080
0
    }
9081
0
#endif
9082
0
    if (!datum)
9083
0
    {
9084
0
        proj_destroy(baseCRS);
9085
0
        return false;
9086
0
    }
9087
9088
    // Known datum code ? Return base CRS
9089
0
    pszCode = proj_get_id_code(datum, 0);
9090
0
    if (pszCode)
9091
0
    {
9092
0
        proj_destroy(datum);
9093
0
        d->setPjCRS(baseCRS);
9094
0
        return true;
9095
0
    }
9096
9097
0
    const char *name = proj_get_name(datum);
9098
0
    if (EQUAL(name, "unknown"))
9099
0
    {
9100
0
        proj_destroy(datum);
9101
0
        proj_destroy(baseCRS);
9102
0
        return false;
9103
0
    }
9104
0
    const PJ_TYPE type = PJ_TYPE_GEODETIC_REFERENCE_FRAME;
9105
0
    PJ_OBJ_LIST *list =
9106
0
        proj_create_from_name(ctxt, nullptr, name, &type, 1, false, 1, nullptr);
9107
9108
0
    bool knownDatumName = false;
9109
0
    if (list)
9110
0
    {
9111
0
        if (proj_list_get_count(list) == 1)
9112
0
        {
9113
0
            knownDatumName = true;
9114
0
        }
9115
0
        proj_list_destroy(list);
9116
0
    }
9117
9118
0
    proj_destroy(datum);
9119
0
    if (knownDatumName)
9120
0
    {
9121
0
        d->setPjCRS(baseCRS);
9122
0
        return true;
9123
0
    }
9124
0
    proj_destroy(baseCRS);
9125
0
    return false;
9126
0
}
9127
9128
/************************************************************************/
9129
/*                             IsCompound()                             */
9130
/************************************************************************/
9131
9132
/**
9133
 * \brief Check if coordinate system is compound.
9134
 *
9135
 * This method is the same as the C function OSRIsCompound().
9136
 *
9137
 * @return TRUE if this is rooted with a COMPD_CS node.
9138
 */
9139
9140
int OGRSpatialReference::IsCompound() const
9141
9142
0
{
9143
0
    TAKE_OPTIONAL_LOCK();
9144
9145
0
    d->refreshProjObj();
9146
0
    d->demoteFromBoundCRS();
9147
0
    bool isCompound = d->m_pjType == PJ_TYPE_COMPOUND_CRS;
9148
0
    d->undoDemoteFromBoundCRS();
9149
0
    return isCompound;
9150
0
}
9151
9152
/************************************************************************/
9153
/*                           OSRIsCompound()                            */
9154
/************************************************************************/
9155
9156
/**
9157
 * \brief Check if the coordinate system is compound.
9158
 *
9159
 * This function is the same as OGRSpatialReference::IsCompound().
9160
 */
9161
int OSRIsCompound(OGRSpatialReferenceH hSRS)
9162
9163
0
{
9164
0
    VALIDATE_POINTER1(hSRS, "OSRIsCompound", 0);
9165
9166
0
    return ToPointer(hSRS)->IsCompound();
9167
0
}
9168
9169
/************************************************************************/
9170
/*                            IsProjected()                             */
9171
/************************************************************************/
9172
9173
/**
9174
 * \brief Check if projected coordinate system.
9175
 *
9176
 * This method is the same as the C function OSRIsProjected().
9177
 *
9178
 * @return TRUE if this contains a PROJCS node indicating a it is a
9179
 * projected coordinate system. Also if it is a CompoundCRS made of a
9180
 * ProjectedCRS
9181
 */
9182
9183
int OGRSpatialReference::IsProjected() const
9184
9185
0
{
9186
0
    TAKE_OPTIONAL_LOCK();
9187
9188
0
    d->refreshProjObj();
9189
0
    d->demoteFromBoundCRS();
9190
0
    bool isProjected = d->m_pjType == PJ_TYPE_PROJECTED_CRS;
9191
0
    if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
9192
0
    {
9193
0
        auto horizCRS =
9194
0
            proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
9195
0
        if (horizCRS)
9196
0
        {
9197
0
            auto horizCRSType = proj_get_type(horizCRS);
9198
0
            isProjected = horizCRSType == PJ_TYPE_PROJECTED_CRS;
9199
0
            if (horizCRSType == PJ_TYPE_BOUND_CRS)
9200
0
            {
9201
0
                auto base = proj_get_source_crs(d->getPROJContext(), horizCRS);
9202
0
                if (base)
9203
0
                {
9204
0
                    isProjected = proj_get_type(base) == PJ_TYPE_PROJECTED_CRS;
9205
0
                    proj_destroy(base);
9206
0
                }
9207
0
            }
9208
0
            proj_destroy(horizCRS);
9209
0
        }
9210
0
    }
9211
0
    d->undoDemoteFromBoundCRS();
9212
0
    return isProjected;
9213
0
}
9214
9215
/************************************************************************/
9216
/*                           OSRIsProjected()                           */
9217
/************************************************************************/
9218
/**
9219
 * \brief Check if projected coordinate system.
9220
 *
9221
 * This function is the same as OGRSpatialReference::IsProjected().
9222
 */
9223
int OSRIsProjected(OGRSpatialReferenceH hSRS)
9224
9225
0
{
9226
0
    VALIDATE_POINTER1(hSRS, "OSRIsProjected", 0);
9227
9228
0
    return ToPointer(hSRS)->IsProjected();
9229
0
}
9230
9231
/************************************************************************/
9232
/*                            IsGeocentric()                            */
9233
/************************************************************************/
9234
9235
/**
9236
 * \brief Check if geocentric coordinate system.
9237
 *
9238
 * This method is the same as the C function OSRIsGeocentric().
9239
 *
9240
 * @return TRUE if this contains a GEOCCS node indicating a it is a
9241
 * geocentric coordinate system.
9242
 *
9243
 */
9244
9245
int OGRSpatialReference::IsGeocentric() const
9246
9247
0
{
9248
0
    TAKE_OPTIONAL_LOCK();
9249
9250
0
    d->refreshProjObj();
9251
0
    d->demoteFromBoundCRS();
9252
0
    bool isGeocentric = d->m_pjType == PJ_TYPE_GEOCENTRIC_CRS;
9253
0
    d->undoDemoteFromBoundCRS();
9254
0
    return isGeocentric;
9255
0
}
9256
9257
/************************************************************************/
9258
/*                           OSRIsGeocentric()                          */
9259
/************************************************************************/
9260
/**
9261
 * \brief Check if geocentric coordinate system.
9262
 *
9263
 * This function is the same as OGRSpatialReference::IsGeocentric().
9264
 *
9265
 */
9266
int OSRIsGeocentric(OGRSpatialReferenceH hSRS)
9267
9268
0
{
9269
0
    VALIDATE_POINTER1(hSRS, "OSRIsGeocentric", 0);
9270
9271
0
    return ToPointer(hSRS)->IsGeocentric();
9272
0
}
9273
9274
/************************************************************************/
9275
/*                            IsEmpty()                                 */
9276
/************************************************************************/
9277
9278
/**
9279
 * \brief Return if the SRS is not set.
9280
 */
9281
9282
bool OGRSpatialReference::IsEmpty() const
9283
0
{
9284
0
    TAKE_OPTIONAL_LOCK();
9285
9286
0
    d->refreshProjObj();
9287
0
    return d->m_pj_crs == nullptr;
9288
0
}
9289
9290
/************************************************************************/
9291
/*                            IsGeographic()                            */
9292
/************************************************************************/
9293
9294
/**
9295
 * \brief Check if geographic coordinate system.
9296
 *
9297
 * This method is the same as the C function OSRIsGeographic().
9298
 *
9299
 * @return TRUE if this spatial reference is geographic ... that is the
9300
 * root is a GEOGCS node. Also if it is a CompoundCRS made of a
9301
 * GeographicCRS
9302
 */
9303
9304
int OGRSpatialReference::IsGeographic() const
9305
9306
0
{
9307
0
    TAKE_OPTIONAL_LOCK();
9308
9309
0
    d->refreshProjObj();
9310
0
    d->demoteFromBoundCRS();
9311
0
    bool isGeog = d->m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
9312
0
                  d->m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS;
9313
0
    if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
9314
0
    {
9315
0
        auto horizCRS =
9316
0
            proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
9317
0
        if (horizCRS)
9318
0
        {
9319
0
            auto horizCRSType = proj_get_type(horizCRS);
9320
0
            isGeog = horizCRSType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
9321
0
                     horizCRSType == PJ_TYPE_GEOGRAPHIC_3D_CRS;
9322
0
            if (horizCRSType == PJ_TYPE_BOUND_CRS)
9323
0
            {
9324
0
                auto base = proj_get_source_crs(d->getPROJContext(), horizCRS);
9325
0
                if (base)
9326
0
                {
9327
0
                    horizCRSType = proj_get_type(base);
9328
0
                    isGeog = horizCRSType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
9329
0
                             horizCRSType == PJ_TYPE_GEOGRAPHIC_3D_CRS;
9330
0
                    proj_destroy(base);
9331
0
                }
9332
0
            }
9333
0
            proj_destroy(horizCRS);
9334
0
        }
9335
0
    }
9336
0
    d->undoDemoteFromBoundCRS();
9337
0
    return isGeog;
9338
0
}
9339
9340
/************************************************************************/
9341
/*                          OSRIsGeographic()                           */
9342
/************************************************************************/
9343
/**
9344
 * \brief Check if geographic coordinate system.
9345
 *
9346
 * This function is the same as OGRSpatialReference::IsGeographic().
9347
 */
9348
int OSRIsGeographic(OGRSpatialReferenceH hSRS)
9349
9350
0
{
9351
0
    VALIDATE_POINTER1(hSRS, "OSRIsGeographic", 0);
9352
9353
0
    return ToPointer(hSRS)->IsGeographic();
9354
0
}
9355
9356
/************************************************************************/
9357
/*                      IsDerivedGeographic()                           */
9358
/************************************************************************/
9359
9360
/**
9361
 * \brief Check if the CRS is a derived geographic coordinate system.
9362
 * (for example a rotated long/lat grid)
9363
 *
9364
 * This method is the same as the C function OSRIsDerivedGeographic().
9365
 *
9366
 * @since GDAL 3.1.0 and PROJ 6.3.0
9367
 */
9368
9369
int OGRSpatialReference::IsDerivedGeographic() const
9370
9371
0
{
9372
0
    TAKE_OPTIONAL_LOCK();
9373
9374
0
    d->refreshProjObj();
9375
0
    d->demoteFromBoundCRS();
9376
0
    const bool isGeog = d->m_pjType == PJ_TYPE_GEOGRAPHIC_2D_CRS ||
9377
0
                        d->m_pjType == PJ_TYPE_GEOGRAPHIC_3D_CRS;
9378
0
    const bool isDerivedGeographic =
9379
0
        isGeog && proj_is_derived_crs(d->getPROJContext(), d->m_pj_crs);
9380
0
    d->undoDemoteFromBoundCRS();
9381
0
    return isDerivedGeographic ? TRUE : FALSE;
9382
0
}
9383
9384
/************************************************************************/
9385
/*                      OSRIsDerivedGeographic()                        */
9386
/************************************************************************/
9387
/**
9388
 * \brief Check if the CRS is a derived geographic coordinate system.
9389
 * (for example a rotated long/lat grid)
9390
 *
9391
 * This function is the same as OGRSpatialReference::IsDerivedGeographic().
9392
 */
9393
int OSRIsDerivedGeographic(OGRSpatialReferenceH hSRS)
9394
9395
0
{
9396
0
    VALIDATE_POINTER1(hSRS, "OSRIsDerivedGeographic", 0);
9397
9398
0
    return ToPointer(hSRS)->IsDerivedGeographic();
9399
0
}
9400
9401
/************************************************************************/
9402
/*                      IsDerivedProjected()                            */
9403
/************************************************************************/
9404
9405
/**
9406
 * \brief Check if the CRS is a derived projected coordinate system.
9407
 *
9408
 * This method is the same as the C function OSRIsDerivedGeographic().
9409
 *
9410
 * @since GDAL 3.9.0 (and may only return non-zero starting with PROJ 9.2.0)
9411
 */
9412
9413
int OGRSpatialReference::IsDerivedProjected() const
9414
9415
0
{
9416
0
#if PROJ_AT_LEAST_VERSION(9, 2, 0)
9417
0
    TAKE_OPTIONAL_LOCK();
9418
0
    d->refreshProjObj();
9419
0
    d->demoteFromBoundCRS();
9420
0
    const bool isDerivedProjected =
9421
0
        d->m_pjType == PJ_TYPE_DERIVED_PROJECTED_CRS;
9422
0
    d->undoDemoteFromBoundCRS();
9423
0
    return isDerivedProjected ? TRUE : FALSE;
9424
#else
9425
    return FALSE;
9426
#endif
9427
0
}
9428
9429
/************************************************************************/
9430
/*                      OSRIsDerivedProjected()                         */
9431
/************************************************************************/
9432
/**
9433
 * \brief Check if the CRS is a derived projected coordinate system.
9434
 *
9435
 * This function is the same as OGRSpatialReference::IsDerivedProjected().
9436
 *
9437
 * @since GDAL 3.9.0 (and may only return non-zero starting with PROJ 9.2.0)
9438
 */
9439
int OSRIsDerivedProjected(OGRSpatialReferenceH hSRS)
9440
9441
0
{
9442
0
    VALIDATE_POINTER1(hSRS, "OSRIsDerivedProjected", 0);
9443
9444
0
    return ToPointer(hSRS)->IsDerivedProjected();
9445
0
}
9446
9447
/************************************************************************/
9448
/*                              IsLocal()                               */
9449
/************************************************************************/
9450
9451
/**
9452
 * \brief Check if local coordinate system.
9453
 *
9454
 * This method is the same as the C function OSRIsLocal().
9455
 *
9456
 * @return TRUE if this spatial reference is local ... that is the
9457
 * root is a LOCAL_CS node.
9458
 */
9459
9460
int OGRSpatialReference::IsLocal() const
9461
9462
0
{
9463
0
    TAKE_OPTIONAL_LOCK();
9464
0
    d->refreshProjObj();
9465
0
    return d->m_pjType == PJ_TYPE_ENGINEERING_CRS;
9466
0
}
9467
9468
/************************************************************************/
9469
/*                          OSRIsLocal()                                */
9470
/************************************************************************/
9471
/**
9472
 * \brief Check if local coordinate system.
9473
 *
9474
 * This function is the same as OGRSpatialReference::IsLocal().
9475
 */
9476
int OSRIsLocal(OGRSpatialReferenceH hSRS)
9477
9478
0
{
9479
0
    VALIDATE_POINTER1(hSRS, "OSRIsLocal", 0);
9480
9481
0
    return ToPointer(hSRS)->IsLocal();
9482
0
}
9483
9484
/************************************************************************/
9485
/*                            IsVertical()                              */
9486
/************************************************************************/
9487
9488
/**
9489
 * \brief Check if vertical coordinate system.
9490
 *
9491
 * This method is the same as the C function OSRIsVertical().
9492
 *
9493
 * @return TRUE if this contains a VERT_CS node indicating a it is a
9494
 * vertical coordinate system. Also if it is a CompoundCRS made of a
9495
 * VerticalCRS
9496
 *
9497
 */
9498
9499
int OGRSpatialReference::IsVertical() const
9500
9501
0
{
9502
0
    TAKE_OPTIONAL_LOCK();
9503
0
    d->refreshProjObj();
9504
0
    d->demoteFromBoundCRS();
9505
0
    bool isVertical = d->m_pjType == PJ_TYPE_VERTICAL_CRS;
9506
0
    if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
9507
0
    {
9508
0
        auto vertCRS =
9509
0
            proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 1);
9510
0
        if (vertCRS)
9511
0
        {
9512
0
            const auto vertCRSType = proj_get_type(vertCRS);
9513
0
            isVertical = vertCRSType == PJ_TYPE_VERTICAL_CRS;
9514
0
            if (vertCRSType == PJ_TYPE_BOUND_CRS)
9515
0
            {
9516
0
                auto base = proj_get_source_crs(d->getPROJContext(), vertCRS);
9517
0
                if (base)
9518
0
                {
9519
0
                    isVertical = proj_get_type(base) == PJ_TYPE_VERTICAL_CRS;
9520
0
                    proj_destroy(base);
9521
0
                }
9522
0
            }
9523
0
            proj_destroy(vertCRS);
9524
0
        }
9525
0
    }
9526
0
    d->undoDemoteFromBoundCRS();
9527
0
    return isVertical;
9528
0
}
9529
9530
/************************************************************************/
9531
/*                           OSRIsVertical()                            */
9532
/************************************************************************/
9533
/**
9534
 * \brief Check if vertical coordinate system.
9535
 *
9536
 * This function is the same as OGRSpatialReference::IsVertical().
9537
 *
9538
 */
9539
int OSRIsVertical(OGRSpatialReferenceH hSRS)
9540
9541
0
{
9542
0
    VALIDATE_POINTER1(hSRS, "OSRIsVertical", 0);
9543
9544
0
    return ToPointer(hSRS)->IsVertical();
9545
0
}
9546
9547
/************************************************************************/
9548
/*                            IsDynamic()                               */
9549
/************************************************************************/
9550
9551
/**
9552
 * \brief Check if a CRS is a dynamic CRS.
9553
 *
9554
 * A dynamic CRS relies on a dynamic datum, that is a datum that is not
9555
 * plate-fixed.
9556
 *
9557
 * This method is the same as the C function OSRIsDynamic().
9558
 *
9559
 * @return true if the CRS is dynamic
9560
 *
9561
 * @since OGR 3.4.0
9562
 *
9563
 * @see HasPointMotionOperation()
9564
 */
9565
9566
bool OGRSpatialReference::IsDynamic() const
9567
9568
0
{
9569
0
    TAKE_OPTIONAL_LOCK();
9570
0
    bool isDynamic = false;
9571
0
    d->refreshProjObj();
9572
0
    d->demoteFromBoundCRS();
9573
0
    auto ctxt = d->getPROJContext();
9574
0
    PJ *horiz = nullptr;
9575
0
    if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
9576
0
    {
9577
0
        horiz = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
9578
0
    }
9579
0
    else if (d->m_pj_crs)
9580
0
    {
9581
0
        horiz = proj_clone(ctxt, d->m_pj_crs);
9582
0
    }
9583
0
    if (horiz && proj_get_type(horiz) == PJ_TYPE_BOUND_CRS)
9584
0
    {
9585
0
        auto baseCRS = proj_get_source_crs(ctxt, horiz);
9586
0
        if (baseCRS)
9587
0
        {
9588
0
            proj_destroy(horiz);
9589
0
            horiz = baseCRS;
9590
0
        }
9591
0
    }
9592
0
    auto datum = horiz ? proj_crs_get_datum(ctxt, horiz) : nullptr;
9593
0
    if (datum)
9594
0
    {
9595
0
        const auto type = proj_get_type(datum);
9596
0
        isDynamic = type == PJ_TYPE_DYNAMIC_GEODETIC_REFERENCE_FRAME ||
9597
0
                    type == PJ_TYPE_DYNAMIC_VERTICAL_REFERENCE_FRAME;
9598
0
        if (!isDynamic)
9599
0
        {
9600
0
            const char *auth_name = proj_get_id_auth_name(datum, 0);
9601
0
            const char *code = proj_get_id_code(datum, 0);
9602
0
            if (auth_name && code && EQUAL(auth_name, "EPSG") &&
9603
0
                EQUAL(code, "6326"))
9604
0
            {
9605
0
                isDynamic = true;
9606
0
            }
9607
0
        }
9608
0
        proj_destroy(datum);
9609
0
    }
9610
0
#if PROJ_VERSION_MAJOR > 7 ||                                                  \
9611
0
    (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
9612
0
    else
9613
0
    {
9614
0
        auto ensemble =
9615
0
            horiz ? proj_crs_get_datum_ensemble(ctxt, horiz) : nullptr;
9616
0
        if (ensemble)
9617
0
        {
9618
0
            auto member = proj_datum_ensemble_get_member(ctxt, ensemble, 0);
9619
0
            if (member)
9620
0
            {
9621
0
                const auto type = proj_get_type(member);
9622
0
                isDynamic = type == PJ_TYPE_DYNAMIC_GEODETIC_REFERENCE_FRAME ||
9623
0
                            type == PJ_TYPE_DYNAMIC_VERTICAL_REFERENCE_FRAME;
9624
0
                proj_destroy(member);
9625
0
            }
9626
0
            proj_destroy(ensemble);
9627
0
        }
9628
0
    }
9629
0
#endif
9630
0
    proj_destroy(horiz);
9631
0
    d->undoDemoteFromBoundCRS();
9632
0
    return isDynamic;
9633
0
}
9634
9635
/************************************************************************/
9636
/*                           OSRIsDynamic()                             */
9637
/************************************************************************/
9638
/**
9639
 * \brief Check if a CRS is a dynamic CRS.
9640
 *
9641
 * A dynamic CRS relies on a dynamic datum, that is a datum that is not
9642
 * plate-fixed.
9643
 *
9644
 * This function is the same as OGRSpatialReference::IsDynamic().
9645
 *
9646
 * @since OGR 3.4.0
9647
 */
9648
int OSRIsDynamic(OGRSpatialReferenceH hSRS)
9649
9650
0
{
9651
0
    VALIDATE_POINTER1(hSRS, "OSRIsDynamic", 0);
9652
9653
0
    return ToPointer(hSRS)->IsDynamic();
9654
0
}
9655
9656
/************************************************************************/
9657
/*                         HasPointMotionOperation()                    */
9658
/************************************************************************/
9659
9660
/**
9661
 * \brief Check if a CRS has at least an associated point motion operation.
9662
 *
9663
 * Some CRS are not formally declared as dynamic, but may behave as such
9664
 * in practice due to the presence of point motion operation, to perform
9665
 * coordinate epoch changes within the CRS. Typically NAD83(CSRS)v7
9666
 *
9667
 * @return true if the CRS has at least an associated point motion operation.
9668
 *
9669
 * @since OGR 3.8.0 and PROJ 9.4.0
9670
 *
9671
 * @see IsDynamic()
9672
 */
9673
9674
bool OGRSpatialReference::HasPointMotionOperation() const
9675
9676
0
{
9677
0
#if PROJ_VERSION_MAJOR > 9 ||                                                  \
9678
0
    (PROJ_VERSION_MAJOR == 9 && PROJ_VERSION_MINOR >= 4)
9679
0
    TAKE_OPTIONAL_LOCK();
9680
0
    d->refreshProjObj();
9681
0
    d->demoteFromBoundCRS();
9682
0
    auto ctxt = d->getPROJContext();
9683
0
    auto res =
9684
0
        CPL_TO_BOOL(proj_crs_has_point_motion_operation(ctxt, d->m_pj_crs));
9685
0
    d->undoDemoteFromBoundCRS();
9686
0
    return res;
9687
#else
9688
    return false;
9689
#endif
9690
0
}
9691
9692
/************************************************************************/
9693
/*                      OSRHasPointMotionOperation()                    */
9694
/************************************************************************/
9695
9696
/**
9697
 * \brief Check if a CRS has at least an associated point motion operation.
9698
 *
9699
 * Some CRS are not formally declared as dynamic, but may behave as such
9700
 * in practice due to the presence of point motion operation, to perform
9701
 * coordinate epoch changes within the CRS. Typically NAD83(CSRS)v7
9702
 *
9703
 * This function is the same as OGRSpatialReference::HasPointMotionOperation().
9704
 *
9705
 * @since OGR 3.8.0 and PROJ 9.4.0
9706
 */
9707
int OSRHasPointMotionOperation(OGRSpatialReferenceH hSRS)
9708
9709
0
{
9710
0
    VALIDATE_POINTER1(hSRS, "OSRHasPointMotionOperation", 0);
9711
9712
0
    return ToPointer(hSRS)->HasPointMotionOperation();
9713
0
}
9714
9715
/************************************************************************/
9716
/*                            CloneGeogCS()                             */
9717
/************************************************************************/
9718
9719
/**
9720
 * \brief Make a duplicate of the GEOGCS node of this OGRSpatialReference
9721
 * object.
9722
 *
9723
 * @return a new SRS, which becomes the responsibility of the caller.
9724
 */
9725
OGRSpatialReference *OGRSpatialReference::CloneGeogCS() const
9726
9727
0
{
9728
0
    TAKE_OPTIONAL_LOCK();
9729
0
    d->refreshProjObj();
9730
0
    if (d->m_pj_crs)
9731
0
    {
9732
0
        if (d->m_pjType == PJ_TYPE_ENGINEERING_CRS)
9733
0
            return nullptr;
9734
9735
0
        auto geodCRS =
9736
0
            proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
9737
0
        if (geodCRS)
9738
0
        {
9739
0
            OGRSpatialReference *poNewSRS = new OGRSpatialReference();
9740
0
            if (d->m_pjType == PJ_TYPE_BOUND_CRS)
9741
0
            {
9742
0
                PJ *hub_crs =
9743
0
                    proj_get_target_crs(d->getPROJContext(), d->m_pj_crs);
9744
0
                PJ *co = proj_crs_get_coordoperation(d->getPROJContext(),
9745
0
                                                     d->m_pj_crs);
9746
0
                auto temp = proj_crs_create_bound_crs(d->getPROJContext(),
9747
0
                                                      geodCRS, hub_crs, co);
9748
0
                proj_destroy(geodCRS);
9749
0
                geodCRS = temp;
9750
0
                proj_destroy(hub_crs);
9751
0
                proj_destroy(co);
9752
0
            }
9753
9754
            /* --------------------------------------------------------------------
9755
             */
9756
            /*      We have to reconstruct the GEOGCS node for geocentric */
9757
            /*      coordinate systems. */
9758
            /* --------------------------------------------------------------------
9759
             */
9760
0
            if (proj_get_type(geodCRS) == PJ_TYPE_GEOCENTRIC_CRS)
9761
0
            {
9762
0
                auto datum = proj_crs_get_datum(d->getPROJContext(), geodCRS);
9763
0
#if PROJ_VERSION_MAJOR > 7 ||                                                  \
9764
0
    (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2)
9765
0
                if (datum == nullptr)
9766
0
                {
9767
0
                    datum = proj_crs_get_datum_ensemble(d->getPROJContext(),
9768
0
                                                        geodCRS);
9769
0
                }
9770
0
#endif
9771
0
                if (datum)
9772
0
                {
9773
0
                    auto cs = proj_create_ellipsoidal_2D_cs(
9774
0
                        d->getPROJContext(), PJ_ELLPS2D_LATITUDE_LONGITUDE,
9775
0
                        nullptr, 0);
9776
0
                    auto temp = proj_create_geographic_crs_from_datum(
9777
0
                        d->getPROJContext(), "unnamed", datum, cs);
9778
0
                    proj_destroy(datum);
9779
0
                    proj_destroy(cs);
9780
0
                    proj_destroy(geodCRS);
9781
0
                    geodCRS = temp;
9782
0
                }
9783
0
            }
9784
9785
0
            poNewSRS->d->setPjCRS(geodCRS);
9786
0
            if (d->m_axisMappingStrategy == OAMS_TRADITIONAL_GIS_ORDER)
9787
0
                poNewSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
9788
0
            return poNewSRS;
9789
0
        }
9790
0
    }
9791
0
    return nullptr;
9792
0
}
9793
9794
/************************************************************************/
9795
/*                           OSRCloneGeogCS()                           */
9796
/************************************************************************/
9797
/**
9798
 * \brief Make a duplicate of the GEOGCS node of this OGRSpatialReference
9799
 * object.
9800
 *
9801
 * This function is the same as OGRSpatialReference::CloneGeogCS().
9802
 */
9803
OGRSpatialReferenceH CPL_STDCALL OSRCloneGeogCS(OGRSpatialReferenceH hSource)
9804
9805
0
{
9806
0
    VALIDATE_POINTER1(hSource, "OSRCloneGeogCS", nullptr);
9807
9808
0
    return ToHandle(ToPointer(hSource)->CloneGeogCS());
9809
0
}
9810
9811
/************************************************************************/
9812
/*                            IsSameGeogCS()                            */
9813
/************************************************************************/
9814
9815
/**
9816
 * \brief Do the GeogCS'es match?
9817
 *
9818
 * This method is the same as the C function OSRIsSameGeogCS().
9819
 *
9820
 * @param poOther the SRS being compared against.
9821
 *
9822
 * @return TRUE if they are the same or FALSE otherwise.
9823
 */
9824
9825
int OGRSpatialReference::IsSameGeogCS(const OGRSpatialReference *poOther) const
9826
9827
0
{
9828
0
    return IsSameGeogCS(poOther, nullptr);
9829
0
}
9830
9831
/**
9832
 * \brief Do the GeogCS'es match?
9833
 *
9834
 * This method is the same as the C function OSRIsSameGeogCS().
9835
 *
9836
 * @param poOther the SRS being compared against.
9837
 * @param papszOptions options. ignored
9838
 *
9839
 * @return TRUE if they are the same or FALSE otherwise.
9840
 */
9841
9842
int OGRSpatialReference::IsSameGeogCS(const OGRSpatialReference *poOther,
9843
                                      const char *const *papszOptions) const
9844
9845
0
{
9846
0
    TAKE_OPTIONAL_LOCK();
9847
9848
0
    CPL_IGNORE_RET_VAL(papszOptions);
9849
9850
0
    d->refreshProjObj();
9851
0
    poOther->d->refreshProjObj();
9852
0
    if (!d->m_pj_crs || !poOther->d->m_pj_crs)
9853
0
        return FALSE;
9854
0
    if (d->m_pjType == PJ_TYPE_ENGINEERING_CRS ||
9855
0
        d->m_pjType == PJ_TYPE_VERTICAL_CRS ||
9856
0
        poOther->d->m_pjType == PJ_TYPE_ENGINEERING_CRS ||
9857
0
        poOther->d->m_pjType == PJ_TYPE_VERTICAL_CRS)
9858
0
    {
9859
0
        return FALSE;
9860
0
    }
9861
9862
0
    auto geodCRS = proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
9863
0
    auto otherGeodCRS =
9864
0
        proj_crs_get_geodetic_crs(d->getPROJContext(), poOther->d->m_pj_crs);
9865
0
    if (!geodCRS || !otherGeodCRS)
9866
0
    {
9867
0
        proj_destroy(geodCRS);
9868
0
        proj_destroy(otherGeodCRS);
9869
0
        return FALSE;
9870
0
    }
9871
9872
0
    int ret = proj_is_equivalent_to(
9873
0
        geodCRS, otherGeodCRS, PJ_COMP_EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS);
9874
9875
0
    proj_destroy(geodCRS);
9876
0
    proj_destroy(otherGeodCRS);
9877
0
    return ret;
9878
0
}
9879
9880
/************************************************************************/
9881
/*                          OSRIsSameGeogCS()                           */
9882
/************************************************************************/
9883
9884
/**
9885
 * \brief Do the GeogCS'es match?
9886
 *
9887
 * This function is the same as OGRSpatialReference::IsSameGeogCS().
9888
 */
9889
int OSRIsSameGeogCS(OGRSpatialReferenceH hSRS1, OGRSpatialReferenceH hSRS2)
9890
9891
0
{
9892
0
    VALIDATE_POINTER1(hSRS1, "OSRIsSameGeogCS", 0);
9893
0
    VALIDATE_POINTER1(hSRS2, "OSRIsSameGeogCS", 0);
9894
9895
0
    return ToPointer(hSRS1)->IsSameGeogCS(ToPointer(hSRS2));
9896
0
}
9897
9898
/************************************************************************/
9899
/*                            IsSameVertCS()                            */
9900
/************************************************************************/
9901
9902
/**
9903
 * \brief Do the VertCS'es match?
9904
 *
9905
 * This method is the same as the C function OSRIsSameVertCS().
9906
 *
9907
 * @param poOther the SRS being compared against.
9908
 *
9909
 * @return TRUE if they are the same or FALSE otherwise.
9910
 */
9911
9912
int OGRSpatialReference::IsSameVertCS(const OGRSpatialReference *poOther) const
9913
9914
0
{
9915
0
    TAKE_OPTIONAL_LOCK();
9916
9917
    /* -------------------------------------------------------------------- */
9918
    /*      Does the datum name match?                                      */
9919
    /* -------------------------------------------------------------------- */
9920
0
    const char *pszThisValue = this->GetAttrValue("VERT_DATUM");
9921
0
    const char *pszOtherValue = poOther->GetAttrValue("VERT_DATUM");
9922
9923
0
    if (pszThisValue == nullptr || pszOtherValue == nullptr ||
9924
0
        !EQUAL(pszThisValue, pszOtherValue))
9925
0
        return FALSE;
9926
9927
    /* -------------------------------------------------------------------- */
9928
    /*      Do the units match?                                             */
9929
    /* -------------------------------------------------------------------- */
9930
0
    pszThisValue = this->GetAttrValue("VERT_CS|UNIT", 1);
9931
0
    if (pszThisValue == nullptr)
9932
0
        pszThisValue = "1.0";
9933
9934
0
    pszOtherValue = poOther->GetAttrValue("VERT_CS|UNIT", 1);
9935
0
    if (pszOtherValue == nullptr)
9936
0
        pszOtherValue = "1.0";
9937
9938
0
    if (std::abs(CPLAtof(pszOtherValue) - CPLAtof(pszThisValue)) > 0.00000001)
9939
0
        return FALSE;
9940
9941
0
    return TRUE;
9942
0
}
9943
9944
/************************************************************************/
9945
/*                          OSRIsSameVertCS()                           */
9946
/************************************************************************/
9947
9948
/**
9949
 * \brief Do the VertCS'es match?
9950
 *
9951
 * This function is the same as OGRSpatialReference::IsSameVertCS().
9952
 */
9953
int OSRIsSameVertCS(OGRSpatialReferenceH hSRS1, OGRSpatialReferenceH hSRS2)
9954
9955
0
{
9956
0
    VALIDATE_POINTER1(hSRS1, "OSRIsSameVertCS", 0);
9957
0
    VALIDATE_POINTER1(hSRS2, "OSRIsSameVertCS", 0);
9958
9959
0
    return ToPointer(hSRS1)->IsSameVertCS(ToPointer(hSRS2));
9960
0
}
9961
9962
/************************************************************************/
9963
/*                               IsSame()                               */
9964
/************************************************************************/
9965
9966
/**
9967
 * \brief Do these two spatial references describe the same system ?
9968
 *
9969
 * @param poOtherSRS the SRS being compared to.
9970
 *
9971
 * @return TRUE if equivalent or FALSE otherwise.
9972
 */
9973
9974
int OGRSpatialReference::IsSame(const OGRSpatialReference *poOtherSRS) const
9975
9976
0
{
9977
0
    return IsSame(poOtherSRS, nullptr);
9978
0
}
9979
9980
/**
9981
 * \brief Do these two spatial references describe the same system ?
9982
 *
9983
 * This also takes into account the data axis to CRS axis mapping by default
9984
 *
9985
 * @param poOtherSRS the SRS being compared to.
9986
 * @param papszOptions options. NULL or NULL terminated list of options.
9987
 * Currently supported options are:
9988
 * <ul>
9989
 * <li>IGNORE_DATA_AXIS_TO_SRS_AXIS_MAPPING=YES/NO. Defaults to NO</li>
9990
 * <li>CRITERION=STRICT/EQUIVALENT/EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS.
9991
 *     Defaults to EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS.</li>
9992
 * <li>IGNORE_COORDINATE_EPOCH=YES/NO. Defaults to NO</li>
9993
 * </ul>
9994
 *
9995
 * @return TRUE if equivalent or FALSE otherwise.
9996
 */
9997
9998
int OGRSpatialReference::IsSame(const OGRSpatialReference *poOtherSRS,
9999
                                const char *const *papszOptions) const
10000
10001
0
{
10002
0
    TAKE_OPTIONAL_LOCK();
10003
10004
0
    d->refreshProjObj();
10005
0
    poOtherSRS->d->refreshProjObj();
10006
0
    if (!d->m_pj_crs || !poOtherSRS->d->m_pj_crs)
10007
0
        return d->m_pj_crs == poOtherSRS->d->m_pj_crs;
10008
0
    if (!CPLTestBool(CSLFetchNameValueDef(
10009
0
            papszOptions, "IGNORE_DATA_AXIS_TO_SRS_AXIS_MAPPING", "NO")))
10010
0
    {
10011
0
        if (d->m_axisMapping != poOtherSRS->d->m_axisMapping)
10012
0
            return false;
10013
0
    }
10014
10015
0
    if (!CPLTestBool(CSLFetchNameValueDef(papszOptions,
10016
0
                                          "IGNORE_COORDINATE_EPOCH", "NO")))
10017
0
    {
10018
0
        if (d->m_coordinateEpoch != poOtherSRS->d->m_coordinateEpoch)
10019
0
            return false;
10020
0
    }
10021
10022
0
    bool reboundSelf = false;
10023
0
    bool reboundOther = false;
10024
0
    if (d->m_pjType == PJ_TYPE_BOUND_CRS &&
10025
0
        poOtherSRS->d->m_pjType != PJ_TYPE_BOUND_CRS)
10026
0
    {
10027
0
        d->demoteFromBoundCRS();
10028
0
        reboundSelf = true;
10029
0
    }
10030
0
    else if (d->m_pjType != PJ_TYPE_BOUND_CRS &&
10031
0
             poOtherSRS->d->m_pjType == PJ_TYPE_BOUND_CRS)
10032
0
    {
10033
0
        poOtherSRS->d->demoteFromBoundCRS();
10034
0
        reboundOther = true;
10035
0
    }
10036
10037
0
    PJ_COMPARISON_CRITERION criterion =
10038
0
        PJ_COMP_EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS;
10039
0
    const char *pszCriterion = CSLFetchNameValueDef(
10040
0
        papszOptions, "CRITERION", "EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS");
10041
0
    if (EQUAL(pszCriterion, "STRICT"))
10042
0
        criterion = PJ_COMP_STRICT;
10043
0
    else if (EQUAL(pszCriterion, "EQUIVALENT"))
10044
0
        criterion = PJ_COMP_EQUIVALENT;
10045
0
    else if (!EQUAL(pszCriterion, "EQUIVALENT_EXCEPT_AXIS_ORDER_GEOGCRS"))
10046
0
    {
10047
0
        CPLError(CE_Warning, CPLE_NotSupported,
10048
0
                 "Unsupported value for CRITERION: %s", pszCriterion);
10049
0
    }
10050
0
    int ret =
10051
0
        proj_is_equivalent_to(d->m_pj_crs, poOtherSRS->d->m_pj_crs, criterion);
10052
0
    if (reboundSelf)
10053
0
        d->undoDemoteFromBoundCRS();
10054
0
    if (reboundOther)
10055
0
        poOtherSRS->d->undoDemoteFromBoundCRS();
10056
10057
0
    return ret;
10058
0
}
10059
10060
/************************************************************************/
10061
/*                             OSRIsSame()                              */
10062
/************************************************************************/
10063
10064
/**
10065
 * \brief Do these two spatial references describe the same system ?
10066
 *
10067
 * This function is the same as OGRSpatialReference::IsSame().
10068
 */
10069
int OSRIsSame(OGRSpatialReferenceH hSRS1, OGRSpatialReferenceH hSRS2)
10070
10071
0
{
10072
0
    VALIDATE_POINTER1(hSRS1, "OSRIsSame", 0);
10073
0
    VALIDATE_POINTER1(hSRS2, "OSRIsSame", 0);
10074
10075
0
    return ToPointer(hSRS1)->IsSame(ToPointer(hSRS2));
10076
0
}
10077
10078
/************************************************************************/
10079
/*                             OSRIsSameEx()                            */
10080
/************************************************************************/
10081
10082
/**
10083
 * \brief Do these two spatial references describe the same system ?
10084
 *
10085
 * This function is the same as OGRSpatialReference::IsSame().
10086
 */
10087
int OSRIsSameEx(OGRSpatialReferenceH hSRS1, OGRSpatialReferenceH hSRS2,
10088
                const char *const *papszOptions)
10089
0
{
10090
0
    VALIDATE_POINTER1(hSRS1, "OSRIsSame", 0);
10091
0
    VALIDATE_POINTER1(hSRS2, "OSRIsSame", 0);
10092
10093
0
    return ToPointer(hSRS1)->IsSame(ToPointer(hSRS2), papszOptions);
10094
0
}
10095
10096
/************************************************************************/
10097
/*                    convertToOtherProjection()                        */
10098
/************************************************************************/
10099
10100
/**
10101
 * \brief Convert to another equivalent projection
10102
 *
10103
 * Currently implemented:
10104
 * <ul>
10105
 * <li>SRS_PT_MERCATOR_1SP to SRS_PT_MERCATOR_2SP</li>
10106
 * <li>SRS_PT_MERCATOR_2SP to SRS_PT_MERCATOR_1SP</li>
10107
 * <li>SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP to
10108
 * SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP</li>
10109
 * <li>SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP to
10110
 * SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP</li>
10111
 * </ul>
10112
 *
10113
 * @param pszTargetProjection target projection.
10114
 * @param papszOptions lists of options. None supported currently.
10115
 * @return a new SRS, or NULL in case of error.
10116
 *
10117
 */
10118
OGRSpatialReference *OGRSpatialReference::convertToOtherProjection(
10119
    const char *pszTargetProjection,
10120
    CPL_UNUSED const char *const *papszOptions) const
10121
0
{
10122
0
    TAKE_OPTIONAL_LOCK();
10123
10124
0
    if (pszTargetProjection == nullptr)
10125
0
        return nullptr;
10126
0
    int new_code;
10127
0
    if (EQUAL(pszTargetProjection, SRS_PT_MERCATOR_1SP))
10128
0
    {
10129
0
        new_code = EPSG_CODE_METHOD_MERCATOR_VARIANT_A;
10130
0
    }
10131
0
    else if (EQUAL(pszTargetProjection, SRS_PT_MERCATOR_2SP))
10132
0
    {
10133
0
        new_code = EPSG_CODE_METHOD_MERCATOR_VARIANT_B;
10134
0
    }
10135
0
    else if (EQUAL(pszTargetProjection, SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP))
10136
0
    {
10137
0
        new_code = EPSG_CODE_METHOD_LAMBERT_CONIC_CONFORMAL_1SP;
10138
0
    }
10139
0
    else if (EQUAL(pszTargetProjection, SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP))
10140
0
    {
10141
0
        new_code = EPSG_CODE_METHOD_LAMBERT_CONIC_CONFORMAL_2SP;
10142
0
    }
10143
0
    else
10144
0
    {
10145
0
        return nullptr;
10146
0
    }
10147
10148
0
    d->refreshProjObj();
10149
0
    d->demoteFromBoundCRS();
10150
0
    OGRSpatialReference *poNewSRS = nullptr;
10151
0
    if (d->m_pjType == PJ_TYPE_PROJECTED_CRS)
10152
0
    {
10153
0
        auto conv =
10154
0
            proj_crs_get_coordoperation(d->getPROJContext(), d->m_pj_crs);
10155
0
        auto new_conv = proj_convert_conversion_to_other_method(
10156
0
            d->getPROJContext(), conv, new_code, nullptr);
10157
0
        proj_destroy(conv);
10158
0
        if (new_conv)
10159
0
        {
10160
0
            auto geodCRS =
10161
0
                proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
10162
0
            auto cs = proj_crs_get_coordinate_system(d->getPROJContext(),
10163
0
                                                     d->m_pj_crs);
10164
0
            if (geodCRS && cs)
10165
0
            {
10166
0
                auto new_proj_crs = proj_create_projected_crs(
10167
0
                    d->getPROJContext(), proj_get_name(d->m_pj_crs), geodCRS,
10168
0
                    new_conv, cs);
10169
0
                proj_destroy(new_conv);
10170
0
                if (new_proj_crs)
10171
0
                {
10172
0
                    poNewSRS = new OGRSpatialReference();
10173
10174
0
                    if (d->m_pj_bound_crs_target && d->m_pj_bound_crs_co)
10175
0
                    {
10176
0
                        auto boundCRS = proj_crs_create_bound_crs(
10177
0
                            d->getPROJContext(), new_proj_crs,
10178
0
                            d->m_pj_bound_crs_target, d->m_pj_bound_crs_co);
10179
0
                        if (boundCRS)
10180
0
                        {
10181
0
                            proj_destroy(new_proj_crs);
10182
0
                            new_proj_crs = boundCRS;
10183
0
                        }
10184
0
                    }
10185
10186
0
                    poNewSRS->d->setPjCRS(new_proj_crs);
10187
0
                }
10188
0
            }
10189
0
            proj_destroy(geodCRS);
10190
0
            proj_destroy(cs);
10191
0
        }
10192
0
    }
10193
0
    d->undoDemoteFromBoundCRS();
10194
0
    return poNewSRS;
10195
0
}
10196
10197
/************************************************************************/
10198
/*                    OSRConvertToOtherProjection()                     */
10199
/************************************************************************/
10200
10201
/**
10202
 * \brief Convert to another equivalent projection
10203
 *
10204
 * Currently implemented:
10205
 * <ul>
10206
 * <li>SRS_PT_MERCATOR_1SP to SRS_PT_MERCATOR_2SP</li>
10207
 * <li>SRS_PT_MERCATOR_2SP to SRS_PT_MERCATOR_1SP</li>
10208
 * <li>SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP to
10209
 * SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP</li>
10210
 * <li>SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP to
10211
 * SRS_PT_LAMBERT_CONFORMAL_CONIC_1SP</li>
10212
 * </ul>
10213
 *
10214
 * @param hSRS source SRS
10215
 * @param pszTargetProjection target projection.
10216
 * @param papszOptions lists of options. None supported currently.
10217
 * @return a new SRS, or NULL in case of error.
10218
 *
10219
 */
10220
OGRSpatialReferenceH
10221
OSRConvertToOtherProjection(OGRSpatialReferenceH hSRS,
10222
                            const char *pszTargetProjection,
10223
                            const char *const *papszOptions)
10224
0
{
10225
0
    VALIDATE_POINTER1(hSRS, "OSRConvertToOtherProjection", nullptr);
10226
0
    return ToHandle(ToPointer(hSRS)->convertToOtherProjection(
10227
0
        pszTargetProjection, papszOptions));
10228
0
}
10229
10230
/************************************************************************/
10231
/*                           OSRFindMatches()                           */
10232
/************************************************************************/
10233
10234
/**
10235
 * \brief Try to identify a match between the passed SRS and a related SRS
10236
 * in a catalog.
10237
 *
10238
 * Matching may be partial, or may fail.
10239
 * Returned entries will be sorted by decreasing match confidence (first
10240
 * entry has the highest match confidence).
10241
 *
10242
 * The exact way matching is done may change in future versions. Starting with
10243
 * GDAL 3.0, it relies on PROJ' proj_identify() function.
10244
 *
10245
 * This function is the same as OGRSpatialReference::FindMatches().
10246
 *
10247
 * @param hSRS SRS to match
10248
 * @param papszOptions NULL terminated list of options or NULL
10249
 * @param pnEntries Output parameter. Number of values in the returned array.
10250
 * @param ppanMatchConfidence Output parameter (or NULL). *ppanMatchConfidence
10251
 * will be allocated to an array of *pnEntries whose values between 0 and 100
10252
 * indicate the confidence in the match. 100 is the highest confidence level.
10253
 * The array must be freed with CPLFree().
10254
 *
10255
 * @return an array of SRS that match the passed SRS, or NULL. Must be freed
10256
 * with OSRFreeSRSArray()
10257
 *
10258
 */
10259
OGRSpatialReferenceH *OSRFindMatches(OGRSpatialReferenceH hSRS,
10260
                                     char **papszOptions, int *pnEntries,
10261
                                     int **ppanMatchConfidence)
10262
0
{
10263
0
    if (pnEntries)
10264
0
        *pnEntries = 0;
10265
0
    if (ppanMatchConfidence)
10266
0
        *ppanMatchConfidence = nullptr;
10267
0
    VALIDATE_POINTER1(hSRS, "OSRFindMatches", nullptr);
10268
10269
0
    OGRSpatialReference *poSRS = ToPointer(hSRS);
10270
0
    return poSRS->FindMatches(papszOptions, pnEntries, ppanMatchConfidence);
10271
0
}
10272
10273
/************************************************************************/
10274
/*                           OSRFreeSRSArray()                          */
10275
/************************************************************************/
10276
10277
/**
10278
 * \brief Free return of OSRIdentifyMatches()
10279
 *
10280
 * @param pahSRS array of SRS (must be NULL terminated)
10281
 */
10282
void OSRFreeSRSArray(OGRSpatialReferenceH *pahSRS)
10283
0
{
10284
0
    if (pahSRS != nullptr)
10285
0
    {
10286
0
        for (int i = 0; pahSRS[i] != nullptr; ++i)
10287
0
        {
10288
0
            OSRRelease(pahSRS[i]);
10289
0
        }
10290
0
        CPLFree(pahSRS);
10291
0
    }
10292
0
}
10293
10294
/************************************************************************/
10295
/*                         FindBestMatch()                              */
10296
/************************************************************************/
10297
10298
/**
10299
 * \brief Try to identify the best match between the passed SRS and a related
10300
 * SRS in a catalog.
10301
 *
10302
 * This is a wrapper over OGRSpatialReference::FindMatches() that takes care
10303
 * of filtering its output.
10304
 * Only matches whose confidence is greater or equal to nMinimumMatchConfidence
10305
 * will be considered. If there is a single match, it is returned.
10306
 * If there are several matches, only return the one under the
10307
 * pszPreferredAuthority, if there is a single one under that authority.
10308
 *
10309
 * @param nMinimumMatchConfidence Minimum match confidence (value between 0 and
10310
 * 100). If set to 0, 90 is used.
10311
 * @param pszPreferredAuthority Preferred CRS authority. If set to nullptr,
10312
 * "EPSG" is used.
10313
 * @param papszOptions NULL terminated list of options or NULL. No option is
10314
 * defined at time of writing.
10315
 *
10316
 * @return a new OGRSpatialReference* object to free with Release(), or nullptr
10317
 *
10318
 * @since GDAL 3.6
10319
 * @see OGRSpatialReference::FindMatches()
10320
 */
10321
OGRSpatialReference *
10322
OGRSpatialReference::FindBestMatch(int nMinimumMatchConfidence,
10323
                                   const char *pszPreferredAuthority,
10324
                                   CSLConstList papszOptions) const
10325
0
{
10326
0
    TAKE_OPTIONAL_LOCK();
10327
10328
0
    CPL_IGNORE_RET_VAL(papszOptions);  // ignored for now.
10329
10330
0
    if (nMinimumMatchConfidence == 0)
10331
0
        nMinimumMatchConfidence = 90;
10332
0
    if (pszPreferredAuthority == nullptr)
10333
0
        pszPreferredAuthority = "EPSG";
10334
10335
    // Try to identify the CRS with the database
10336
0
    int nEntries = 0;
10337
0
    int *panConfidence = nullptr;
10338
0
    OGRSpatialReferenceH *pahSRS =
10339
0
        FindMatches(nullptr, &nEntries, &panConfidence);
10340
0
    if (nEntries == 1 && panConfidence[0] >= nMinimumMatchConfidence)
10341
0
    {
10342
0
        std::vector<double> adfTOWGS84(7);
10343
0
        if (GetTOWGS84(&adfTOWGS84[0], 7) != OGRERR_NONE)
10344
0
        {
10345
0
            adfTOWGS84.clear();
10346
0
        }
10347
10348
0
        auto poSRS = OGRSpatialReference::FromHandle(pahSRS[0]);
10349
10350
0
        auto poBaseGeogCRS =
10351
0
            std::unique_ptr<OGRSpatialReference>(poSRS->CloneGeogCS());
10352
0
        if (poBaseGeogCRS)
10353
0
        {
10354
            // If the base geographic SRS of the SRS is EPSG:4326
10355
            // with TOWGS84[0,0,0,0,0,0], then just use the official
10356
            // SRS code
10357
            // Same with EPSG:4258 (ETRS89), since it's the only known
10358
            // TOWGS84[] style transformation to WGS 84, and given the
10359
            // "fuzzy" nature of both ETRS89 and WGS 84, there's little
10360
            // chance that a non-NULL TOWGS84[] will emerge.
10361
0
            const char *pszAuthorityName = nullptr;
10362
0
            const char *pszAuthorityCode = nullptr;
10363
0
            const char *pszBaseAuthorityName = nullptr;
10364
0
            const char *pszBaseAuthorityCode = nullptr;
10365
0
            const char *pszBaseName = poBaseGeogCRS->GetName();
10366
0
            if (adfTOWGS84 == std::vector<double>(7) &&
10367
0
                (pszAuthorityName = poSRS->GetAuthorityName(nullptr)) !=
10368
0
                    nullptr &&
10369
0
                EQUAL(pszAuthorityName, "EPSG") &&
10370
0
                (pszAuthorityCode = poSRS->GetAuthorityCode(nullptr)) !=
10371
0
                    nullptr &&
10372
0
                (pszBaseAuthorityName =
10373
0
                     poBaseGeogCRS->GetAuthorityName(nullptr)) != nullptr &&
10374
0
                EQUAL(pszBaseAuthorityName, "EPSG") &&
10375
0
                (pszBaseAuthorityCode =
10376
0
                     poBaseGeogCRS->GetAuthorityCode(nullptr)) != nullptr &&
10377
0
                (EQUAL(pszBaseAuthorityCode, "4326") ||
10378
0
                 EQUAL(pszBaseAuthorityCode, "4258") ||
10379
                 // For ETRS89-XXX [...] new CRS added in EPSG 12.033+
10380
0
                 (pszBaseName && STARTS_WITH(pszBaseName, "ETRS89"))))
10381
0
            {
10382
0
                poSRS->importFromEPSG(atoi(pszAuthorityCode));
10383
0
            }
10384
0
        }
10385
10386
0
        CPLFree(pahSRS);
10387
0
        CPLFree(panConfidence);
10388
10389
0
        return poSRS;
10390
0
    }
10391
0
    else
10392
0
    {
10393
        // If there are several matches >= nMinimumMatchConfidence, take the
10394
        // only one that is under pszPreferredAuthority
10395
0
        int iBestEntry = -1;
10396
0
        for (int i = 0; i < nEntries; i++)
10397
0
        {
10398
0
            if (panConfidence[i] >= nMinimumMatchConfidence)
10399
0
            {
10400
0
                const char *pszAuthName =
10401
0
                    OGRSpatialReference::FromHandle(pahSRS[i])
10402
0
                        ->GetAuthorityName(nullptr);
10403
0
                if (pszAuthName != nullptr &&
10404
0
                    EQUAL(pszAuthName, pszPreferredAuthority))
10405
0
                {
10406
0
                    if (iBestEntry < 0)
10407
0
                        iBestEntry = i;
10408
0
                    else
10409
0
                    {
10410
0
                        iBestEntry = -1;
10411
0
                        break;
10412
0
                    }
10413
0
                }
10414
0
            }
10415
0
        }
10416
0
        if (iBestEntry >= 0)
10417
0
        {
10418
0
            auto poRet = OGRSpatialReference::FromHandle(pahSRS[0])->Clone();
10419
0
            OSRFreeSRSArray(pahSRS);
10420
0
            CPLFree(panConfidence);
10421
0
            return poRet;
10422
0
        }
10423
0
    }
10424
0
    OSRFreeSRSArray(pahSRS);
10425
0
    CPLFree(panConfidence);
10426
0
    return nullptr;
10427
0
}
10428
10429
/************************************************************************/
10430
/*                             SetTOWGS84()                             */
10431
/************************************************************************/
10432
10433
/**
10434
 * \brief Set the Bursa-Wolf conversion to WGS84.
10435
 *
10436
 * This will create the TOWGS84 node as a child of the DATUM.  It will fail
10437
 * if there is no existing DATUM node. It will replace
10438
 * an existing TOWGS84 node if there is one.
10439
 *
10440
 * The parameters have the same meaning as EPSG transformation 9606
10441
 * (Position Vector 7-param. transformation).
10442
 *
10443
 * This method is the same as the C function OSRSetTOWGS84().
10444
 *
10445
 * @param dfDX X child in meters.
10446
 * @param dfDY Y child in meters.
10447
 * @param dfDZ Z child in meters.
10448
 * @param dfEX X rotation in arc seconds (optional, defaults to zero).
10449
 * @param dfEY Y rotation in arc seconds (optional, defaults to zero).
10450
 * @param dfEZ Z rotation in arc seconds (optional, defaults to zero).
10451
 * @param dfPPM scaling factor (parts per million).
10452
 *
10453
 * @return OGRERR_NONE on success.
10454
 */
10455
10456
OGRErr OGRSpatialReference::SetTOWGS84(double dfDX, double dfDY, double dfDZ,
10457
                                       double dfEX, double dfEY, double dfEZ,
10458
                                       double dfPPM)
10459
10460
0
{
10461
0
    TAKE_OPTIONAL_LOCK();
10462
10463
0
    d->refreshProjObj();
10464
0
    if (d->m_pj_crs == nullptr)
10465
0
    {
10466
0
        return OGRERR_FAILURE;
10467
0
    }
10468
10469
    // Remove existing BoundCRS
10470
0
    if (d->m_pjType == PJ_TYPE_BOUND_CRS)
10471
0
    {
10472
0
        auto baseCRS = proj_get_source_crs(d->getPROJContext(), d->m_pj_crs);
10473
0
        if (!baseCRS)
10474
0
            return OGRERR_FAILURE;
10475
0
        d->setPjCRS(baseCRS);
10476
0
    }
10477
10478
0
    PJ_PARAM_DESCRIPTION params[7];
10479
10480
0
    params[0].name = EPSG_NAME_PARAMETER_X_AXIS_TRANSLATION;
10481
0
    params[0].auth_name = "EPSG";
10482
0
    params[0].code = XSTRINGIFY(EPSG_CODE_PARAMETER_X_AXIS_TRANSLATION);
10483
0
    params[0].value = dfDX;
10484
0
    params[0].unit_name = "metre";
10485
0
    params[0].unit_conv_factor = 1.0;
10486
0
    params[0].unit_type = PJ_UT_LINEAR;
10487
10488
0
    params[1].name = EPSG_NAME_PARAMETER_Y_AXIS_TRANSLATION;
10489
0
    params[1].auth_name = "EPSG";
10490
0
    params[1].code = XSTRINGIFY(EPSG_CODE_PARAMETER_Y_AXIS_TRANSLATION);
10491
0
    params[1].value = dfDY;
10492
0
    params[1].unit_name = "metre";
10493
0
    params[1].unit_conv_factor = 1.0;
10494
0
    params[1].unit_type = PJ_UT_LINEAR;
10495
10496
0
    params[2].name = EPSG_NAME_PARAMETER_Z_AXIS_TRANSLATION;
10497
0
    params[2].auth_name = "EPSG";
10498
0
    params[2].code = XSTRINGIFY(EPSG_CODE_PARAMETER_Z_AXIS_TRANSLATION);
10499
0
    params[2].value = dfDZ;
10500
0
    params[2].unit_name = "metre";
10501
0
    params[2].unit_conv_factor = 1.0;
10502
0
    params[2].unit_type = PJ_UT_LINEAR;
10503
10504
0
    params[3].name = EPSG_NAME_PARAMETER_X_AXIS_ROTATION;
10505
0
    params[3].auth_name = "EPSG";
10506
0
    params[3].code = XSTRINGIFY(EPSG_CODE_PARAMETER_X_AXIS_ROTATION);
10507
0
    params[3].value = dfEX;
10508
0
    params[3].unit_name = "arc-second";
10509
0
    params[3].unit_conv_factor = 1. / 3600 * M_PI / 180;
10510
0
    params[3].unit_type = PJ_UT_ANGULAR;
10511
10512
0
    params[4].name = EPSG_NAME_PARAMETER_Y_AXIS_ROTATION;
10513
0
    params[4].auth_name = "EPSG";
10514
0
    params[4].code = XSTRINGIFY(EPSG_CODE_PARAMETER_Y_AXIS_ROTATION);
10515
0
    params[4].value = dfEY;
10516
0
    params[4].unit_name = "arc-second";
10517
0
    params[4].unit_conv_factor = 1. / 3600 * M_PI / 180;
10518
0
    params[4].unit_type = PJ_UT_ANGULAR;
10519
10520
0
    params[5].name = EPSG_NAME_PARAMETER_Z_AXIS_ROTATION;
10521
0
    params[5].auth_name = "EPSG";
10522
0
    params[5].code = XSTRINGIFY(EPSG_CODE_PARAMETER_Z_AXIS_ROTATION);
10523
0
    params[5].value = dfEZ;
10524
0
    params[5].unit_name = "arc-second";
10525
0
    params[5].unit_conv_factor = 1. / 3600 * M_PI / 180;
10526
0
    params[5].unit_type = PJ_UT_ANGULAR;
10527
10528
0
    params[6].name = EPSG_NAME_PARAMETER_SCALE_DIFFERENCE;
10529
0
    params[6].auth_name = "EPSG";
10530
0
    params[6].code = XSTRINGIFY(EPSG_CODE_PARAMETER_SCALE_DIFFERENCE);
10531
0
    params[6].value = dfPPM;
10532
0
    params[6].unit_name = "parts per million";
10533
0
    params[6].unit_conv_factor = 1e-6;
10534
0
    params[6].unit_type = PJ_UT_SCALE;
10535
10536
0
    auto sourceCRS =
10537
0
        proj_crs_get_geodetic_crs(d->getPROJContext(), d->m_pj_crs);
10538
0
    if (!sourceCRS)
10539
0
    {
10540
0
        return OGRERR_FAILURE;
10541
0
    }
10542
10543
0
    const auto sourceType = proj_get_type(sourceCRS);
10544
10545
0
    auto targetCRS = proj_create_from_database(
10546
0
        d->getPROJContext(), "EPSG",
10547
0
        sourceType == PJ_TYPE_GEOGRAPHIC_2D_CRS   ? "4326"
10548
0
        : sourceType == PJ_TYPE_GEOGRAPHIC_3D_CRS ? "4979"
10549
0
                                                  : "4978",
10550
0
        PJ_CATEGORY_CRS, false, nullptr);
10551
0
    if (!targetCRS)
10552
0
    {
10553
0
        proj_destroy(sourceCRS);
10554
0
        return OGRERR_FAILURE;
10555
0
    }
10556
10557
0
    CPLString osMethodCode;
10558
0
    osMethodCode.Printf("%d",
10559
0
                        sourceType == PJ_TYPE_GEOGRAPHIC_2D_CRS
10560
0
                            ? EPSG_CODE_METHOD_POSITION_VECTOR_GEOGRAPHIC_2D
10561
0
                        : sourceType == PJ_TYPE_GEOGRAPHIC_3D_CRS
10562
0
                            ? EPSG_CODE_METHOD_POSITION_VECTOR_GEOGRAPHIC_3D
10563
0
                            : EPSG_CODE_METHOD_POSITION_VECTOR_GEOCENTRIC);
10564
10565
0
    auto transf = proj_create_transformation(
10566
0
        d->getPROJContext(), "Transformation to WGS84", nullptr, nullptr,
10567
0
        sourceCRS, targetCRS, nullptr,
10568
0
        sourceType == PJ_TYPE_GEOGRAPHIC_2D_CRS
10569
0
            ? EPSG_NAME_METHOD_POSITION_VECTOR_GEOGRAPHIC_2D
10570
0
        : sourceType == PJ_TYPE_GEOGRAPHIC_3D_CRS
10571
0
            ? EPSG_NAME_METHOD_POSITION_VECTOR_GEOGRAPHIC_3D
10572
0
            : EPSG_NAME_METHOD_POSITION_VECTOR_GEOCENTRIC,
10573
0
        "EPSG", osMethodCode.c_str(), 7, params, -1);
10574
0
    proj_destroy(sourceCRS);
10575
0
    if (!transf)
10576
0
    {
10577
0
        proj_destroy(targetCRS);
10578
0
        return OGRERR_FAILURE;
10579
0
    }
10580
10581
0
    auto newBoundCRS = proj_crs_create_bound_crs(
10582
0
        d->getPROJContext(), d->m_pj_crs, targetCRS, transf);
10583
0
    proj_destroy(transf);
10584
0
    proj_destroy(targetCRS);
10585
0
    if (!newBoundCRS)
10586
0
    {
10587
0
        return OGRERR_FAILURE;
10588
0
    }
10589
10590
0
    d->setPjCRS(newBoundCRS);
10591
0
    return OGRERR_NONE;
10592
0
}
10593
10594
/************************************************************************/
10595
/*                           OSRSetTOWGS84()                            */
10596
/************************************************************************/
10597
10598
/**
10599
 * \brief Set the Bursa-Wolf conversion to WGS84.
10600
 *
10601
 * This function is the same as OGRSpatialReference::SetTOWGS84().
10602
 */
10603
OGRErr OSRSetTOWGS84(OGRSpatialReferenceH hSRS, double dfDX, double dfDY,
10604
                     double dfDZ, double dfEX, double dfEY, double dfEZ,
10605
                     double dfPPM)
10606
10607
0
{
10608
0
    VALIDATE_POINTER1(hSRS, "OSRSetTOWGS84", OGRERR_FAILURE);
10609
10610
0
    return ToPointer(hSRS)->SetTOWGS84(dfDX, dfDY, dfDZ, dfEX, dfEY, dfEZ,
10611
0
                                       dfPPM);
10612
0
}
10613
10614
/************************************************************************/
10615
/*                             GetTOWGS84()                             */
10616
/************************************************************************/
10617
10618
/**
10619
 * \brief Fetch TOWGS84 parameters, if available.
10620
 *
10621
 * The parameters have the same meaning as EPSG transformation 9606
10622
 * (Position Vector 7-param. transformation).
10623
 *
10624
 * @param padfCoeff array into which up to 7 coefficients are placed.
10625
 * @param nCoeffCount size of padfCoeff - defaults to 7.
10626
 *
10627
 * @return OGRERR_NONE on success, or OGRERR_FAILURE if there is no
10628
 * TOWGS84 node available.
10629
 */
10630
10631
OGRErr OGRSpatialReference::GetTOWGS84(double *padfCoeff, int nCoeffCount) const
10632
10633
0
{
10634
0
    TAKE_OPTIONAL_LOCK();
10635
10636
0
    d->refreshProjObj();
10637
0
    if (d->m_pjType != PJ_TYPE_BOUND_CRS)
10638
0
        return OGRERR_FAILURE;
10639
10640
0
    memset(padfCoeff, 0, sizeof(double) * nCoeffCount);
10641
10642
0
    auto transf = proj_crs_get_coordoperation(d->getPROJContext(), d->m_pj_crs);
10643
0
    int success = proj_coordoperation_get_towgs84_values(
10644
0
        d->getPROJContext(), transf, padfCoeff, nCoeffCount, false);
10645
0
    proj_destroy(transf);
10646
10647
0
    return success ? OGRERR_NONE : OGRERR_FAILURE;
10648
0
}
10649
10650
/************************************************************************/
10651
/*                           OSRGetTOWGS84()                            */
10652
/************************************************************************/
10653
10654
/**
10655
 * \brief Fetch TOWGS84 parameters, if available.
10656
 *
10657
 * This function is the same as OGRSpatialReference::GetTOWGS84().
10658
 */
10659
OGRErr OSRGetTOWGS84(OGRSpatialReferenceH hSRS, double *padfCoeff,
10660
                     int nCoeffCount)
10661
10662
0
{
10663
0
    VALIDATE_POINTER1(hSRS, "OSRGetTOWGS84", OGRERR_FAILURE);
10664
10665
0
    return ToPointer(hSRS)->GetTOWGS84(padfCoeff, nCoeffCount);
10666
0
}
10667
10668
/************************************************************************/
10669
/*                         IsAngularParameter()                         */
10670
/************************************************************************/
10671
10672
/** Is the passed projection parameter an angular one?
10673
 *
10674
 * @return TRUE or FALSE
10675
 */
10676
10677
/* static */
10678
int OGRSpatialReference::IsAngularParameter(const char *pszParameterName)
10679
10680
0
{
10681
0
    if (STARTS_WITH_CI(pszParameterName, "long") ||
10682
0
        STARTS_WITH_CI(pszParameterName, "lati") ||
10683
0
        EQUAL(pszParameterName, SRS_PP_CENTRAL_MERIDIAN) ||
10684
0
        STARTS_WITH_CI(pszParameterName, "standard_parallel") ||
10685
0
        EQUAL(pszParameterName, SRS_PP_AZIMUTH) ||
10686
0
        EQUAL(pszParameterName, SRS_PP_RECTIFIED_GRID_ANGLE))
10687
0
        return TRUE;
10688
10689
0
    return FALSE;
10690
0
}
10691
10692
/************************************************************************/
10693
/*                        IsLongitudeParameter()                        */
10694
/************************************************************************/
10695
10696
/** Is the passed projection parameter an angular longitude
10697
 * (relative to a prime meridian)?
10698
 *
10699
 * @return TRUE or FALSE
10700
 */
10701
10702
/* static */
10703
int OGRSpatialReference::IsLongitudeParameter(const char *pszParameterName)
10704
10705
0
{
10706
0
    if (STARTS_WITH_CI(pszParameterName, "long") ||
10707
0
        EQUAL(pszParameterName, SRS_PP_CENTRAL_MERIDIAN))
10708
0
        return TRUE;
10709
10710
0
    return FALSE;
10711
0
}
10712
10713
/************************************************************************/
10714
/*                         IsLinearParameter()                          */
10715
/************************************************************************/
10716
10717
/** Is the passed projection parameter an linear one measured in meters or
10718
 * some similar linear measure.
10719
 *
10720
 * @return TRUE or FALSE
10721
 */
10722
10723
/* static */
10724
int OGRSpatialReference::IsLinearParameter(const char *pszParameterName)
10725
10726
0
{
10727
0
    if (STARTS_WITH_CI(pszParameterName, "false_") ||
10728
0
        EQUAL(pszParameterName, SRS_PP_SATELLITE_HEIGHT))
10729
0
        return TRUE;
10730
10731
0
    return FALSE;
10732
0
}
10733
10734
/************************************************************************/
10735
/*                            GetNormInfo()                             */
10736
/************************************************************************/
10737
10738
/**
10739
 * \brief Set the internal information for normalizing linear, and angular
10740
 * values.
10741
 */
10742
void OGRSpatialReference::GetNormInfo() const
10743
10744
0
{
10745
0
    TAKE_OPTIONAL_LOCK();
10746
10747
0
    if (d->bNormInfoSet)
10748
0
        return;
10749
10750
    /* -------------------------------------------------------------------- */
10751
    /*      Initialize values.                                              */
10752
    /* -------------------------------------------------------------------- */
10753
0
    d->bNormInfoSet = TRUE;
10754
10755
0
    d->dfFromGreenwich = GetPrimeMeridian(nullptr);
10756
0
    d->dfToMeter = GetLinearUnits(nullptr);
10757
0
    d->dfToDegrees = GetAngularUnits(nullptr) / CPLAtof(SRS_UA_DEGREE_CONV);
10758
0
    if (fabs(d->dfToDegrees - 1.0) < 0.000000001)
10759
0
        d->dfToDegrees = 1.0;
10760
0
}
10761
10762
/************************************************************************/
10763
/*                            GetExtension()                            */
10764
/************************************************************************/
10765
10766
/**
10767
 * \brief Fetch extension value.
10768
 *
10769
 * Fetch the value of the named EXTENSION item for the identified
10770
 * target node.
10771
 *
10772
 * @param pszTargetKey the name or path to the parent node of the EXTENSION.
10773
 * @param pszName the name of the extension being fetched.
10774
 * @param pszDefault the value to return if the extension is not found.
10775
 *
10776
 * @return node value if successful or pszDefault on failure.
10777
 */
10778
10779
const char *OGRSpatialReference::GetExtension(const char *pszTargetKey,
10780
                                              const char *pszName,
10781
                                              const char *pszDefault) const
10782
10783
0
{
10784
0
    TAKE_OPTIONAL_LOCK();
10785
10786
    /* -------------------------------------------------------------------- */
10787
    /*      Find the target node.                                           */
10788
    /* -------------------------------------------------------------------- */
10789
0
    const OGR_SRSNode *poNode =
10790
0
        pszTargetKey == nullptr ? GetRoot() : GetAttrNode(pszTargetKey);
10791
10792
0
    if (poNode == nullptr)
10793
0
        return nullptr;
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
        const 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
                return poChild->GetChild(1)->GetValue();
10807
0
        }
10808
0
    }
10809
10810
0
    return pszDefault;
10811
0
}
10812
10813
/************************************************************************/
10814
/*                            SetExtension()                            */
10815
/************************************************************************/
10816
/**
10817
 * \brief Set extension value.
10818
 *
10819
 * Set the value of the named EXTENSION item for the identified
10820
 * target node.
10821
 *
10822
 * @param pszTargetKey the name or path to the parent node of the EXTENSION.
10823
 * @param pszName the name of the extension being fetched.
10824
 * @param pszValue the value to set
10825
 *
10826
 * @return OGRERR_NONE on success
10827
 */
10828
10829
OGRErr OGRSpatialReference::SetExtension(const char *pszTargetKey,
10830
                                         const char *pszName,
10831
                                         const char *pszValue)
10832
10833
0
{
10834
0
    TAKE_OPTIONAL_LOCK();
10835
10836
    /* -------------------------------------------------------------------- */
10837
    /*      Find the target node.                                           */
10838
    /* -------------------------------------------------------------------- */
10839
0
    OGR_SRSNode *poNode = nullptr;
10840
10841
0
    if (pszTargetKey == nullptr)
10842
0
        poNode = GetRoot();
10843
0
    else
10844
0
        poNode = GetAttrNode(pszTargetKey);
10845
10846
0
    if (poNode == nullptr)
10847
0
        return OGRERR_FAILURE;
10848
10849
    /* -------------------------------------------------------------------- */
10850
    /*      Fetch matching EXTENSION if there is one.                       */
10851
    /* -------------------------------------------------------------------- */
10852
0
    for (int i = poNode->GetChildCount() - 1; i >= 0; i--)
10853
0
    {
10854
0
        OGR_SRSNode *poChild = poNode->GetChild(i);
10855
10856
0
        if (EQUAL(poChild->GetValue(), "EXTENSION") &&
10857
0
            poChild->GetChildCount() >= 2)
10858
0
        {
10859
0
            if (EQUAL(poChild->GetChild(0)->GetValue(), pszName))
10860
0
            {
10861
0
                poChild->GetChild(1)->SetValue(pszValue);
10862
0
                return OGRERR_NONE;
10863
0
            }
10864
0
        }
10865
0
    }
10866
10867
    /* -------------------------------------------------------------------- */
10868
    /*      Create a new EXTENSION node.                                    */
10869
    /* -------------------------------------------------------------------- */
10870
0
    OGR_SRSNode *poAuthNode = new OGR_SRSNode("EXTENSION");
10871
0
    poAuthNode->AddChild(new OGR_SRSNode(pszName));
10872
0
    poAuthNode->AddChild(new OGR_SRSNode(pszValue));
10873
10874
0
    poNode->AddChild(poAuthNode);
10875
10876
0
    return OGRERR_NONE;
10877
0
}
10878
10879
/************************************************************************/
10880
/*                             OSRCleanup()                             */
10881
/************************************************************************/
10882
10883
static void CleanupSRSWGS84Mutex();
10884
10885
/**
10886
 * \brief Cleanup cached SRS related memory.
10887
 *
10888
 * This function will attempt to cleanup any cache spatial reference
10889
 * related information, such as cached tables of coordinate systems.
10890
 *
10891
 * This function should not be called concurrently with any other GDAL/OGR
10892
 * function. It is meant at being called once before process termination
10893
 * (typically from the main thread). CPLCleanupTLS() might be used to clean
10894
 * thread-specific resources before thread termination.
10895
 */
10896
void OSRCleanup(void)
10897
10898
0
{
10899
0
    OGRCTDumpStatistics();
10900
0
    CSVDeaccess(nullptr);
10901
0
    CleanupSRSWGS84Mutex();
10902
0
    OSRCTCleanCache();
10903
0
    OSRCleanupTLSContext();
10904
0
}
10905
10906
/************************************************************************/
10907
/*                              GetAxesCount()                          */
10908
/************************************************************************/
10909
10910
/**
10911
 * \brief Return the number of axis of the coordinate system of the CRS.
10912
 *
10913
 * @since GDAL 3.0
10914
 */
10915
int OGRSpatialReference::GetAxesCount() const
10916
0
{
10917
0
    TAKE_OPTIONAL_LOCK();
10918
10919
0
    int axisCount = 0;
10920
0
    d->refreshProjObj();
10921
0
    if (d->m_pj_crs == nullptr)
10922
0
    {
10923
0
        return 0;
10924
0
    }
10925
0
    d->demoteFromBoundCRS();
10926
0
    auto ctxt = d->getPROJContext();
10927
0
    if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
10928
0
    {
10929
0
        for (int i = 0;; i++)
10930
0
        {
10931
0
            auto subCRS = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, i);
10932
0
            if (!subCRS)
10933
0
                break;
10934
0
            if (proj_get_type(subCRS) == PJ_TYPE_BOUND_CRS)
10935
0
            {
10936
0
                auto baseCRS = proj_get_source_crs(ctxt, subCRS);
10937
0
                if (baseCRS)
10938
0
                {
10939
0
                    proj_destroy(subCRS);
10940
0
                    subCRS = baseCRS;
10941
0
                }
10942
0
            }
10943
0
            auto cs = proj_crs_get_coordinate_system(ctxt, subCRS);
10944
0
            if (cs)
10945
0
            {
10946
0
                axisCount += proj_cs_get_axis_count(ctxt, cs);
10947
0
                proj_destroy(cs);
10948
0
            }
10949
0
            proj_destroy(subCRS);
10950
0
        }
10951
0
    }
10952
0
    else
10953
0
    {
10954
0
        auto cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
10955
0
        if (cs)
10956
0
        {
10957
0
            axisCount = proj_cs_get_axis_count(ctxt, cs);
10958
0
            proj_destroy(cs);
10959
0
        }
10960
0
    }
10961
0
    d->undoDemoteFromBoundCRS();
10962
0
    return axisCount;
10963
0
}
10964
10965
/************************************************************************/
10966
/*                           OSRGetAxesCount()                          */
10967
/************************************************************************/
10968
10969
/**
10970
 * \brief Return the number of axis of the coordinate system of the CRS.
10971
 *
10972
 * This method is the equivalent of the C++ method
10973
 * OGRSpatialReference::GetAxesCount()
10974
 *
10975
 * @since GDAL 3.1
10976
 */
10977
int OSRGetAxesCount(OGRSpatialReferenceH hSRS)
10978
10979
0
{
10980
0
    VALIDATE_POINTER1(hSRS, "OSRGetAxesCount", 0);
10981
10982
0
    return ToPointer(hSRS)->GetAxesCount();
10983
0
}
10984
10985
/************************************************************************/
10986
/*                              GetAxis()                               */
10987
/************************************************************************/
10988
10989
/**
10990
 * \brief Fetch the orientation of one axis.
10991
 *
10992
 * Fetches the request axis (iAxis - zero based) from the
10993
 * indicated portion of the coordinate system (pszTargetKey) which
10994
 * should be either "GEOGCS" or "PROJCS".
10995
 *
10996
 * No CPLError is issued on routine failures (such as not finding the AXIS).
10997
 *
10998
 * This method is equivalent to the C function OSRGetAxis().
10999
 *
11000
 * @param pszTargetKey the coordinate system part to query ("PROJCS" or
11001
 * "GEOGCS").
11002
 * @param iAxis the axis to query (0 for first, 1 for second, 2 for third).
11003
 * @param peOrientation location into which to place the fetch orientation, may
11004
 * be NULL.
11005
 * @param pdfConvUnit (GDAL >= 3.4) Location into which to place axis conversion
11006
 * factor. May be NULL. Only set if pszTargetKey == NULL
11007
 *
11008
 * @return the name of the axis or NULL on failure.
11009
 */
11010
11011
const char *OGRSpatialReference::GetAxis(const char *pszTargetKey, int iAxis,
11012
                                         OGRAxisOrientation *peOrientation,
11013
                                         double *pdfConvUnit) const
11014
11015
0
{
11016
0
    TAKE_OPTIONAL_LOCK();
11017
11018
0
    if (peOrientation != nullptr)
11019
0
        *peOrientation = OAO_Other;
11020
0
    if (pdfConvUnit != nullptr)
11021
0
        *pdfConvUnit = 0;
11022
11023
0
    d->refreshProjObj();
11024
0
    if (d->m_pj_crs == nullptr)
11025
0
    {
11026
0
        return nullptr;
11027
0
    }
11028
11029
0
    pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey);
11030
0
    if (pszTargetKey == nullptr && iAxis <= 2)
11031
0
    {
11032
0
        auto ctxt = d->getPROJContext();
11033
11034
0
        int iAxisModified = iAxis;
11035
11036
0
        d->demoteFromBoundCRS();
11037
11038
0
        PJ *cs = nullptr;
11039
0
        if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
11040
0
        {
11041
0
            auto horizCRS = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
11042
0
            if (horizCRS)
11043
0
            {
11044
0
                if (proj_get_type(horizCRS) == PJ_TYPE_BOUND_CRS)
11045
0
                {
11046
0
                    auto baseCRS = proj_get_source_crs(ctxt, horizCRS);
11047
0
                    if (baseCRS)
11048
0
                    {
11049
0
                        proj_destroy(horizCRS);
11050
0
                        horizCRS = baseCRS;
11051
0
                    }
11052
0
                }
11053
0
                cs = proj_crs_get_coordinate_system(ctxt, horizCRS);
11054
0
                proj_destroy(horizCRS);
11055
0
                if (cs)
11056
0
                {
11057
0
                    if (iAxisModified >= proj_cs_get_axis_count(ctxt, cs))
11058
0
                    {
11059
0
                        iAxisModified -= proj_cs_get_axis_count(ctxt, cs);
11060
0
                        proj_destroy(cs);
11061
0
                        cs = nullptr;
11062
0
                    }
11063
0
                }
11064
0
            }
11065
11066
0
            if (cs == nullptr)
11067
0
            {
11068
0
                auto vertCRS = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 1);
11069
0
                if (vertCRS)
11070
0
                {
11071
0
                    if (proj_get_type(vertCRS) == PJ_TYPE_BOUND_CRS)
11072
0
                    {
11073
0
                        auto baseCRS = proj_get_source_crs(ctxt, vertCRS);
11074
0
                        if (baseCRS)
11075
0
                        {
11076
0
                            proj_destroy(vertCRS);
11077
0
                            vertCRS = baseCRS;
11078
0
                        }
11079
0
                    }
11080
11081
0
                    cs = proj_crs_get_coordinate_system(ctxt, vertCRS);
11082
0
                    proj_destroy(vertCRS);
11083
0
                }
11084
0
            }
11085
0
        }
11086
0
        else
11087
0
        {
11088
0
            cs = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
11089
0
        }
11090
11091
0
        if (cs)
11092
0
        {
11093
0
            const char *pszName = nullptr;
11094
0
            const char *pszOrientation = nullptr;
11095
0
            double dfConvFactor = 0.0;
11096
0
            proj_cs_get_axis_info(ctxt, cs, iAxisModified, &pszName, nullptr,
11097
0
                                  &pszOrientation, &dfConvFactor, nullptr,
11098
0
                                  nullptr, nullptr);
11099
11100
0
            if (pdfConvUnit != nullptr)
11101
0
            {
11102
0
                *pdfConvUnit = dfConvFactor;
11103
0
            }
11104
11105
0
            if (pszName && pszOrientation)
11106
0
            {
11107
0
                d->m_osAxisName[iAxis] = pszName;
11108
0
                if (peOrientation)
11109
0
                {
11110
0
                    if (EQUAL(pszOrientation, "NORTH"))
11111
0
                        *peOrientation = OAO_North;
11112
0
                    else if (EQUAL(pszOrientation, "EAST"))
11113
0
                        *peOrientation = OAO_East;
11114
0
                    else if (EQUAL(pszOrientation, "SOUTH"))
11115
0
                        *peOrientation = OAO_South;
11116
0
                    else if (EQUAL(pszOrientation, "WEST"))
11117
0
                        *peOrientation = OAO_West;
11118
0
                    else if (EQUAL(pszOrientation, "UP"))
11119
0
                        *peOrientation = OAO_Up;
11120
0
                    else if (EQUAL(pszOrientation, "DOWN"))
11121
0
                        *peOrientation = OAO_Down;
11122
0
                }
11123
0
                proj_destroy(cs);
11124
0
                d->undoDemoteFromBoundCRS();
11125
0
                return d->m_osAxisName[iAxis].c_str();
11126
0
            }
11127
0
            proj_destroy(cs);
11128
0
        }
11129
0
        d->undoDemoteFromBoundCRS();
11130
0
    }
11131
11132
    /* -------------------------------------------------------------------- */
11133
    /*      Find the target node.                                           */
11134
    /* -------------------------------------------------------------------- */
11135
0
    const OGR_SRSNode *poNode = nullptr;
11136
11137
0
    if (pszTargetKey == nullptr)
11138
0
        poNode = GetRoot();
11139
0
    else
11140
0
        poNode = GetAttrNode(pszTargetKey);
11141
11142
0
    if (poNode == nullptr)
11143
0
        return nullptr;
11144
11145
    /* -------------------------------------------------------------------- */
11146
    /*      Find desired child AXIS.                                        */
11147
    /* -------------------------------------------------------------------- */
11148
0
    const OGR_SRSNode *poAxis = nullptr;
11149
0
    const int nChildCount = poNode->GetChildCount();
11150
11151
0
    for (int iChild = 0; iChild < nChildCount; iChild++)
11152
0
    {
11153
0
        const OGR_SRSNode *poChild = poNode->GetChild(iChild);
11154
11155
0
        if (!EQUAL(poChild->GetValue(), "AXIS"))
11156
0
            continue;
11157
11158
0
        if (iAxis == 0)
11159
0
        {
11160
0
            poAxis = poChild;
11161
0
            break;
11162
0
        }
11163
0
        iAxis--;
11164
0
    }
11165
11166
0
    if (poAxis == nullptr)
11167
0
        return nullptr;
11168
11169
0
    if (poAxis->GetChildCount() < 2)
11170
0
        return nullptr;
11171
11172
    /* -------------------------------------------------------------------- */
11173
    /*      Extract name and orientation if possible.                       */
11174
    /* -------------------------------------------------------------------- */
11175
0
    if (peOrientation != nullptr)
11176
0
    {
11177
0
        const char *pszOrientation = poAxis->GetChild(1)->GetValue();
11178
11179
0
        if (EQUAL(pszOrientation, "NORTH"))
11180
0
            *peOrientation = OAO_North;
11181
0
        else if (EQUAL(pszOrientation, "EAST"))
11182
0
            *peOrientation = OAO_East;
11183
0
        else if (EQUAL(pszOrientation, "SOUTH"))
11184
0
            *peOrientation = OAO_South;
11185
0
        else if (EQUAL(pszOrientation, "WEST"))
11186
0
            *peOrientation = OAO_West;
11187
0
        else if (EQUAL(pszOrientation, "UP"))
11188
0
            *peOrientation = OAO_Up;
11189
0
        else if (EQUAL(pszOrientation, "DOWN"))
11190
0
            *peOrientation = OAO_Down;
11191
0
        else if (EQUAL(pszOrientation, "OTHER"))
11192
0
            *peOrientation = OAO_Other;
11193
0
        else
11194
0
        {
11195
0
            CPLDebug("OSR", "Unrecognized orientation value '%s'.",
11196
0
                     pszOrientation);
11197
0
        }
11198
0
    }
11199
11200
0
    return poAxis->GetChild(0)->GetValue();
11201
0
}
11202
11203
/************************************************************************/
11204
/*                             OSRGetAxis()                             */
11205
/************************************************************************/
11206
11207
/**
11208
 * \brief Fetch the orientation of one axis.
11209
 *
11210
 * This method is the equivalent of the C++ method OGRSpatialReference::GetAxis
11211
 */
11212
const char *OSRGetAxis(OGRSpatialReferenceH hSRS, const char *pszTargetKey,
11213
                       int iAxis, OGRAxisOrientation *peOrientation)
11214
11215
0
{
11216
0
    VALIDATE_POINTER1(hSRS, "OSRGetAxis", nullptr);
11217
11218
0
    return ToPointer(hSRS)->GetAxis(pszTargetKey, iAxis, peOrientation);
11219
0
}
11220
11221
/************************************************************************/
11222
/*                         OSRAxisEnumToName()                          */
11223
/************************************************************************/
11224
11225
/**
11226
 * \brief Return the string representation for the OGRAxisOrientation
11227
 * enumeration.
11228
 *
11229
 * For example "NORTH" for OAO_North.
11230
 *
11231
 * @return an internal string
11232
 */
11233
const char *OSRAxisEnumToName(OGRAxisOrientation eOrientation)
11234
11235
0
{
11236
0
    if (eOrientation == OAO_North)
11237
0
        return "NORTH";
11238
0
    if (eOrientation == OAO_East)
11239
0
        return "EAST";
11240
0
    if (eOrientation == OAO_South)
11241
0
        return "SOUTH";
11242
0
    if (eOrientation == OAO_West)
11243
0
        return "WEST";
11244
0
    if (eOrientation == OAO_Up)
11245
0
        return "UP";
11246
0
    if (eOrientation == OAO_Down)
11247
0
        return "DOWN";
11248
0
    if (eOrientation == OAO_Other)
11249
0
        return "OTHER";
11250
11251
0
    return "UNKNOWN";
11252
0
}
11253
11254
/************************************************************************/
11255
/*                              SetAxes()                               */
11256
/************************************************************************/
11257
11258
/**
11259
 * \brief Set the axes for a coordinate system.
11260
 *
11261
 * Set the names, and orientations of the axes for either a projected
11262
 * (PROJCS) or geographic (GEOGCS) coordinate system.
11263
 *
11264
 * This method is equivalent to the C function OSRSetAxes().
11265
 *
11266
 * @param pszTargetKey either "PROJCS" or "GEOGCS", must already exist in SRS.
11267
 * @param pszXAxisName name of first axis, normally "Long" or "Easting".
11268
 * @param eXAxisOrientation normally OAO_East.
11269
 * @param pszYAxisName name of second axis, normally "Lat" or "Northing".
11270
 * @param eYAxisOrientation normally OAO_North.
11271
 *
11272
 * @return OGRERR_NONE on success or an error code.
11273
 */
11274
11275
OGRErr OGRSpatialReference::SetAxes(const char *pszTargetKey,
11276
                                    const char *pszXAxisName,
11277
                                    OGRAxisOrientation eXAxisOrientation,
11278
                                    const char *pszYAxisName,
11279
                                    OGRAxisOrientation eYAxisOrientation)
11280
11281
0
{
11282
0
    TAKE_OPTIONAL_LOCK();
11283
11284
    /* -------------------------------------------------------------------- */
11285
    /*      Find the target node.                                           */
11286
    /* -------------------------------------------------------------------- */
11287
0
    OGR_SRSNode *poNode = nullptr;
11288
11289
0
    if (pszTargetKey == nullptr)
11290
0
        poNode = GetRoot();
11291
0
    else
11292
0
        poNode = GetAttrNode(pszTargetKey);
11293
11294
0
    if (poNode == nullptr)
11295
0
        return OGRERR_FAILURE;
11296
11297
    /* -------------------------------------------------------------------- */
11298
    /*      Strip any existing AXIS children.                               */
11299
    /* -------------------------------------------------------------------- */
11300
0
    while (poNode->FindChild("AXIS") >= 0)
11301
0
        poNode->DestroyChild(poNode->FindChild("AXIS"));
11302
11303
    /* -------------------------------------------------------------------- */
11304
    /*      Insert desired axes                                             */
11305
    /* -------------------------------------------------------------------- */
11306
0
    OGR_SRSNode *poAxis = new OGR_SRSNode("AXIS");
11307
11308
0
    poAxis->AddChild(new OGR_SRSNode(pszXAxisName));
11309
0
    poAxis->AddChild(new OGR_SRSNode(OSRAxisEnumToName(eXAxisOrientation)));
11310
11311
0
    poNode->AddChild(poAxis);
11312
11313
0
    poAxis = new OGR_SRSNode("AXIS");
11314
11315
0
    poAxis->AddChild(new OGR_SRSNode(pszYAxisName));
11316
0
    poAxis->AddChild(new OGR_SRSNode(OSRAxisEnumToName(eYAxisOrientation)));
11317
11318
0
    poNode->AddChild(poAxis);
11319
11320
0
    return OGRERR_NONE;
11321
0
}
11322
11323
/************************************************************************/
11324
/*                             OSRSetAxes()                             */
11325
/************************************************************************/
11326
/**
11327
 * \brief Set the axes for a coordinate system.
11328
 *
11329
 * This method is the equivalent of the C++ method OGRSpatialReference::SetAxes
11330
 */
11331
OGRErr OSRSetAxes(OGRSpatialReferenceH hSRS, const char *pszTargetKey,
11332
                  const char *pszXAxisName,
11333
                  OGRAxisOrientation eXAxisOrientation,
11334
                  const char *pszYAxisName,
11335
                  OGRAxisOrientation eYAxisOrientation)
11336
0
{
11337
0
    VALIDATE_POINTER1(hSRS, "OSRSetAxes", OGRERR_FAILURE);
11338
11339
0
    return ToPointer(hSRS)->SetAxes(pszTargetKey, pszXAxisName,
11340
0
                                    eXAxisOrientation, pszYAxisName,
11341
0
                                    eYAxisOrientation);
11342
0
}
11343
11344
/************************************************************************/
11345
/*                       OSRExportToMICoordSys()                        */
11346
/************************************************************************/
11347
/**
11348
 * \brief Export coordinate system in Mapinfo style CoordSys format.
11349
 *
11350
 * This method is the equivalent of the C++ method
11351
 * OGRSpatialReference::exportToMICoordSys
11352
 */
11353
OGRErr OSRExportToMICoordSys(OGRSpatialReferenceH hSRS, char **ppszReturn)
11354
11355
0
{
11356
0
    VALIDATE_POINTER1(hSRS, "OSRExportToMICoordSys", OGRERR_FAILURE);
11357
11358
0
    *ppszReturn = nullptr;
11359
11360
0
    return ToPointer(hSRS)->exportToMICoordSys(ppszReturn);
11361
0
}
11362
11363
/************************************************************************/
11364
/*                         exportToMICoordSys()                         */
11365
/************************************************************************/
11366
11367
/**
11368
 * \brief Export coordinate system in Mapinfo style CoordSys format.
11369
 *
11370
 * Note that the returned WKT string should be freed with
11371
 * CPLFree() when no longer needed.  It is the responsibility of the caller.
11372
 *
11373
 * This method is the same as the C function OSRExportToMICoordSys().
11374
 *
11375
 * @param ppszResult pointer to which dynamically allocated Mapinfo CoordSys
11376
 * definition will be assigned.
11377
 *
11378
 * @return OGRERR_NONE on success, OGRERR_FAILURE on failure,
11379
 * OGRERR_UNSUPPORTED_OPERATION if MITAB library was not linked in.
11380
 */
11381
11382
OGRErr OGRSpatialReference::exportToMICoordSys(char **ppszResult) const
11383
11384
0
{
11385
0
    *ppszResult = MITABSpatialRef2CoordSys(this);
11386
0
    if (*ppszResult != nullptr && strlen(*ppszResult) > 0)
11387
0
        return OGRERR_NONE;
11388
11389
0
    return OGRERR_FAILURE;
11390
0
}
11391
11392
/************************************************************************/
11393
/*                       OSRImportFromMICoordSys()                      */
11394
/************************************************************************/
11395
/**
11396
 * \brief Import Mapinfo style CoordSys definition.
11397
 *
11398
 * This method is the equivalent of the C++ method
11399
 * OGRSpatialReference::importFromMICoordSys
11400
 */
11401
11402
OGRErr OSRImportFromMICoordSys(OGRSpatialReferenceH hSRS,
11403
                               const char *pszCoordSys)
11404
11405
0
{
11406
0
    VALIDATE_POINTER1(hSRS, "OSRImportFromMICoordSys", OGRERR_FAILURE);
11407
11408
0
    return ToPointer(hSRS)->importFromMICoordSys(pszCoordSys);
11409
0
}
11410
11411
/************************************************************************/
11412
/*                        importFromMICoordSys()                        */
11413
/************************************************************************/
11414
11415
/**
11416
 * \brief Import Mapinfo style CoordSys definition.
11417
 *
11418
 * The OGRSpatialReference is initialized from the passed Mapinfo style CoordSys
11419
 * definition string.
11420
 *
11421
 * This method is the equivalent of the C function OSRImportFromMICoordSys().
11422
 *
11423
 * @param pszCoordSys Mapinfo style CoordSys definition string.
11424
 *
11425
 * @return OGRERR_NONE on success, OGRERR_FAILURE on failure,
11426
 * OGRERR_UNSUPPORTED_OPERATION if MITAB library was not linked in.
11427
 */
11428
11429
OGRErr OGRSpatialReference::importFromMICoordSys(const char *pszCoordSys)
11430
11431
0
{
11432
0
    OGRSpatialReference *poResult = MITABCoordSys2SpatialRef(pszCoordSys);
11433
11434
0
    if (poResult == nullptr)
11435
0
        return OGRERR_FAILURE;
11436
11437
0
    *this = *poResult;
11438
0
    delete poResult;
11439
11440
0
    return OGRERR_NONE;
11441
0
}
11442
11443
/************************************************************************/
11444
/*                        OSRCalcInvFlattening()                        */
11445
/************************************************************************/
11446
11447
/**
11448
 * \brief Compute inverse flattening from semi-major and semi-minor axis
11449
 *
11450
 * @param dfSemiMajor Semi-major axis length.
11451
 * @param dfSemiMinor Semi-minor axis length.
11452
 *
11453
 * @return inverse flattening, or 0 if both axis are equal.
11454
 */
11455
11456
double OSRCalcInvFlattening(double dfSemiMajor, double dfSemiMinor)
11457
0
{
11458
0
    if (fabs(dfSemiMajor - dfSemiMinor) < 1e-1)
11459
0
        return 0;
11460
0
    if (dfSemiMajor <= 0 || dfSemiMinor <= 0 || dfSemiMinor > dfSemiMajor)
11461
0
    {
11462
0
        CPLError(CE_Failure, CPLE_IllegalArg,
11463
0
                 "OSRCalcInvFlattening(): Wrong input values");
11464
0
        return 0;
11465
0
    }
11466
11467
0
    return dfSemiMajor / (dfSemiMajor - dfSemiMinor);
11468
0
}
11469
11470
/************************************************************************/
11471
/*                        OSRCalcInvFlattening()                        */
11472
/************************************************************************/
11473
11474
/**
11475
 * \brief Compute semi-minor axis from semi-major axis and inverse flattening.
11476
 *
11477
 * @param dfSemiMajor Semi-major axis length.
11478
 * @param dfInvFlattening Inverse flattening or 0 for sphere.
11479
 *
11480
 * @return semi-minor axis
11481
 */
11482
11483
double OSRCalcSemiMinorFromInvFlattening(double dfSemiMajor,
11484
                                         double dfInvFlattening)
11485
0
{
11486
0
    if (fabs(dfInvFlattening) < 0.000000000001)
11487
0
        return dfSemiMajor;
11488
0
    if (dfSemiMajor <= 0.0 || dfInvFlattening <= 1.0)
11489
0
    {
11490
0
        CPLError(CE_Failure, CPLE_IllegalArg,
11491
0
                 "OSRCalcSemiMinorFromInvFlattening(): Wrong input values");
11492
0
        return dfSemiMajor;
11493
0
    }
11494
11495
0
    return dfSemiMajor * (1.0 - 1.0 / dfInvFlattening);
11496
0
}
11497
11498
/************************************************************************/
11499
/*                        GetWGS84SRS()                                 */
11500
/************************************************************************/
11501
11502
static OGRSpatialReference *poSRSWGS84 = nullptr;
11503
static CPLMutex *hMutex = nullptr;
11504
11505
/**
11506
 * \brief Returns an instance of a SRS object with WGS84 WKT.
11507
 *
11508
 * Note: the instance will have
11509
 * SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER)
11510
 *
11511
 * The reference counter of the returned object is not increased by this
11512
 * operation.
11513
 *
11514
 * @return instance.
11515
 */
11516
11517
OGRSpatialReference *OGRSpatialReference::GetWGS84SRS()
11518
0
{
11519
0
    CPLMutexHolderD(&hMutex);
11520
0
    if (poSRSWGS84 == nullptr)
11521
0
    {
11522
0
        poSRSWGS84 = new OGRSpatialReference(SRS_WKT_WGS84_LAT_LONG);
11523
0
        poSRSWGS84->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
11524
0
    }
11525
0
    return poSRSWGS84;
11526
0
}
11527
11528
/************************************************************************/
11529
/*                        CleanupSRSWGS84Mutex()                       */
11530
/************************************************************************/
11531
11532
static void CleanupSRSWGS84Mutex()
11533
0
{
11534
0
    if (hMutex != nullptr)
11535
0
    {
11536
0
        poSRSWGS84->Release();
11537
0
        poSRSWGS84 = nullptr;
11538
0
        CPLDestroyMutex(hMutex);
11539
0
        hMutex = nullptr;
11540
0
    }
11541
0
}
11542
11543
/************************************************************************/
11544
/*                         OSRImportFromProj4()                         */
11545
/************************************************************************/
11546
/**
11547
 * \brief Import PROJ coordinate string.
11548
 *
11549
 * This function is the same as OGRSpatialReference::importFromProj4().
11550
 */
11551
OGRErr OSRImportFromProj4(OGRSpatialReferenceH hSRS, const char *pszProj4)
11552
11553
0
{
11554
0
    VALIDATE_POINTER1(hSRS, "OSRImportFromProj4", OGRERR_FAILURE);
11555
11556
0
    return OGRSpatialReference::FromHandle(hSRS)->importFromProj4(pszProj4);
11557
0
}
11558
11559
/************************************************************************/
11560
/*                          importFromProj4()                           */
11561
/************************************************************************/
11562
11563
/**
11564
 * \brief Import PROJ coordinate string.
11565
 *
11566
 * The OGRSpatialReference is initialized from the passed PROJs style
11567
 * coordinate system string.
11568
 *
11569
 * Example:
11570
 *   pszProj4 = "+proj=utm +zone=11 +datum=WGS84"
11571
 *
11572
 * It is also possible to import "+init=epsg:n" style definitions. Those are
11573
 * a legacy syntax that should be avoided in the future. In particular they will
11574
 * result in CRS objects whose axis order might not correspond to the official
11575
 * EPSG axis order.
11576
 *
11577
 * This method is the equivalent of the C function OSRImportFromProj4().
11578
 *
11579
 * @param pszProj4 the PROJ style string.
11580
 *
11581
 * @return OGRERR_NONE on success or OGRERR_CORRUPT_DATA on failure.
11582
 */
11583
11584
OGRErr OGRSpatialReference::importFromProj4(const char *pszProj4)
11585
11586
0
{
11587
0
    TAKE_OPTIONAL_LOCK();
11588
11589
0
    if (strlen(pszProj4) >= 10000)
11590
0
    {
11591
0
        CPLError(CE_Failure, CPLE_AppDefined, "Too long PROJ string");
11592
0
        return OGRERR_CORRUPT_DATA;
11593
0
    }
11594
11595
    /* -------------------------------------------------------------------- */
11596
    /*      Clear any existing definition.                                  */
11597
    /* -------------------------------------------------------------------- */
11598
0
    Clear();
11599
11600
0
    CPLString osProj4(pszProj4);
11601
0
    if (osProj4.find("type=crs") == std::string::npos)
11602
0
    {
11603
0
        osProj4 += " +type=crs";
11604
0
    }
11605
11606
0
    if (osProj4.find("+init=epsg:") != std::string::npos &&
11607
0
        getenv("PROJ_USE_PROJ4_INIT_RULES") == nullptr)
11608
0
    {
11609
0
        CPLErrorOnce(CE_Warning, CPLE_AppDefined,
11610
0
                     "+init=epsg:XXXX syntax is deprecated. It might return "
11611
0
                     "a CRS with a non-EPSG compliant axis order.");
11612
0
    }
11613
0
    proj_context_use_proj4_init_rules(d->getPROJContext(), true);
11614
0
    d->setPjCRS(proj_create(d->getPROJContext(), osProj4.c_str()));
11615
0
    proj_context_use_proj4_init_rules(d->getPROJContext(), false);
11616
0
    return d->m_pj_crs ? OGRERR_NONE : OGRERR_CORRUPT_DATA;
11617
0
}
11618
11619
/************************************************************************/
11620
/*                          OSRExportToProj4()                          */
11621
/************************************************************************/
11622
/**
11623
 * \brief Export coordinate system in PROJ.4 legacy format.
11624
 *
11625
 * \warning Use of this function is discouraged. Its behavior in GDAL &gt;= 3 /
11626
 * PROJ &gt;= 6 is significantly different from earlier versions. In particular
11627
 * +datum will only encode WGS84, NAD27 and NAD83, and +towgs84/+nadgrids terms
11628
 * will be missing most of the time. PROJ strings to encode CRS should be
11629
 * considered as a legacy solution. Using a AUTHORITY:CODE or WKT representation
11630
 * is the recommended way.
11631
 *
11632
 * This function is the same as OGRSpatialReference::exportToProj4().
11633
 */
11634
OGRErr CPL_STDCALL OSRExportToProj4(OGRSpatialReferenceH hSRS,
11635
                                    char **ppszReturn)
11636
11637
0
{
11638
0
    VALIDATE_POINTER1(hSRS, "OSRExportToProj4", OGRERR_FAILURE);
11639
11640
0
    *ppszReturn = nullptr;
11641
11642
0
    return OGRSpatialReference::FromHandle(hSRS)->exportToProj4(ppszReturn);
11643
0
}
11644
11645
/************************************************************************/
11646
/*                           exportToProj4()                            */
11647
/************************************************************************/
11648
11649
/**
11650
 * \brief Export coordinate system in PROJ.4 legacy format.
11651
 *
11652
 * \warning Use of this function is discouraged. Its behavior in GDAL &gt;= 3 /
11653
 * PROJ &gt;= 6 is significantly different from earlier versions. In particular
11654
 * +datum will only encode WGS84, NAD27 and NAD83, and +towgs84/+nadgrids terms
11655
 * will be missing most of the time. PROJ strings to encode CRS should be
11656
 * considered as a a legacy solution. Using a AUTHORITY:CODE or WKT
11657
 * representation is the recommended way.
11658
 *
11659
 * Converts the loaded coordinate reference system into PROJ format
11660
 * to the extent possible.  The string returned in ppszProj4 should be
11661
 * deallocated by the caller with CPLFree() when no longer needed.
11662
 *
11663
 * LOCAL_CS coordinate systems are not translatable.  An empty string
11664
 * will be returned along with OGRERR_NONE.
11665
 *
11666
 * Special processing for Transverse Mercator:
11667
 * Starting with GDAL 3.0, if the OSR_USE_APPROX_TMERC configuration option is
11668
 * set to YES, the PROJ definition built from the SRS will use the +approx flag
11669
 * for the tmerc and utm projection methods, rather than the more accurate
11670
 * method.
11671
 *
11672
 * Starting with GDAL 3.0.3, this method will try to add a +towgs84 parameter,
11673
 * if there's none attached yet to the SRS and if the SRS has a EPSG code.
11674
 * See the AddGuessedTOWGS84() method for how this +towgs84 parameter may be
11675
 * added. This automatic addition may be disabled by setting the
11676
 * OSR_ADD_TOWGS84_ON_EXPORT_TO_PROJ4 configuration option to NO.
11677
 *
11678
 * This method is the equivalent of the C function OSRExportToProj4().
11679
 *
11680
 * @param ppszProj4 pointer to which dynamically allocated PROJ definition
11681
 * will be assigned.
11682
 *
11683
 * @return OGRERR_NONE on success or an error code on failure.
11684
 */
11685
11686
OGRErr OGRSpatialReference::exportToProj4(char **ppszProj4) const
11687
11688
0
{
11689
    // In the past calling this method was thread-safe, even if we never
11690
    // guaranteed it. Now proj_as_proj_string() will cache the result
11691
    // internally, so this is no longer thread-safe.
11692
0
    std::lock_guard oLock(d->m_mutex);
11693
11694
0
    d->refreshProjObj();
11695
0
    if (d->m_pj_crs == nullptr || d->m_pjType == PJ_TYPE_ENGINEERING_CRS)
11696
0
    {
11697
0
        *ppszProj4 = CPLStrdup("");
11698
0
        return OGRERR_FAILURE;
11699
0
    }
11700
11701
    // OSR_USE_ETMERC is here just for legacy
11702
0
    bool bForceApproxTMerc = false;
11703
0
    const char *pszUseETMERC = CPLGetConfigOption("OSR_USE_ETMERC", nullptr);
11704
0
    if (pszUseETMERC && pszUseETMERC[0])
11705
0
    {
11706
0
        CPLErrorOnce(CE_Warning, CPLE_AppDefined,
11707
0
                     "OSR_USE_ETMERC is a legacy configuration option, which "
11708
0
                     "now has only effect when set to NO (YES is the default). "
11709
0
                     "Use OSR_USE_APPROX_TMERC=YES instead");
11710
0
        bForceApproxTMerc = !CPLTestBool(pszUseETMERC);
11711
0
    }
11712
0
    else
11713
0
    {
11714
0
        const char *pszUseApproxTMERC =
11715
0
            CPLGetConfigOption("OSR_USE_APPROX_TMERC", nullptr);
11716
0
        if (pszUseApproxTMERC && pszUseApproxTMERC[0])
11717
0
        {
11718
0
            bForceApproxTMerc = CPLTestBool(pszUseApproxTMERC);
11719
0
        }
11720
0
    }
11721
0
    const char *options[] = {
11722
0
        bForceApproxTMerc ? "USE_APPROX_TMERC=YES" : nullptr, nullptr};
11723
11724
0
    const char *projString = proj_as_proj_string(
11725
0
        d->getPROJContext(), d->m_pj_crs, PJ_PROJ_4, options);
11726
11727
0
    PJ *boundCRS = nullptr;
11728
0
    if (projString &&
11729
0
        (strstr(projString, "+datum=") == nullptr ||
11730
0
         d->m_pjType == PJ_TYPE_COMPOUND_CRS) &&
11731
0
        CPLTestBool(
11732
0
            CPLGetConfigOption("OSR_ADD_TOWGS84_ON_EXPORT_TO_PROJ4", "YES")))
11733
0
    {
11734
0
        boundCRS = GDAL_proj_crs_create_bound_crs_to_WGS84(
11735
0
            d->getPROJContext(), d->m_pj_crs, true,
11736
0
            strstr(projString, "+datum=") == nullptr);
11737
0
        if (boundCRS)
11738
0
        {
11739
0
            projString = proj_as_proj_string(d->getPROJContext(), boundCRS,
11740
0
                                             PJ_PROJ_4, options);
11741
0
        }
11742
0
    }
11743
11744
0
    if (projString == nullptr)
11745
0
    {
11746
0
        *ppszProj4 = CPLStrdup("");
11747
0
        proj_destroy(boundCRS);
11748
0
        return OGRERR_FAILURE;
11749
0
    }
11750
0
    *ppszProj4 = CPLStrdup(projString);
11751
0
    proj_destroy(boundCRS);
11752
0
    char *pszTypeCrs = strstr(*ppszProj4, " +type=crs");
11753
0
    if (pszTypeCrs)
11754
0
        *pszTypeCrs = '\0';
11755
0
    return OGRERR_NONE;
11756
0
}
11757
11758
/************************************************************************/
11759
/*                            morphToESRI()                             */
11760
/************************************************************************/
11761
/**
11762
 * \brief Convert in place to ESRI WKT format.
11763
 *
11764
 * The value nodes of this coordinate system are modified in various manners
11765
 * more closely map onto the ESRI concept of WKT format.  This includes
11766
 * renaming a variety of projections and arguments, and stripping out
11767
 * nodes note recognised by ESRI (like AUTHORITY and AXIS).
11768
 *
11769
 * \note Since GDAL 3.0, this function has only user-visible effects at
11770
 * exportToWkt() time. It is recommended to use instead exportToWkt(char**,
11771
 * const char* const char*) const with options having FORMAT=WKT1_ESRI.
11772
 *
11773
 * This does the same as the C function OSRMorphToESRI().
11774
 *
11775
 * @return OGRERR_NONE unless something goes badly wrong.
11776
 * @deprecated
11777
 */
11778
11779
OGRErr OGRSpatialReference::morphToESRI()
11780
11781
0
{
11782
0
    TAKE_OPTIONAL_LOCK();
11783
11784
0
    d->refreshProjObj();
11785
0
    d->setMorphToESRI(true);
11786
11787
0
    return OGRERR_NONE;
11788
0
}
11789
11790
/************************************************************************/
11791
/*                           OSRMorphToESRI()                           */
11792
/************************************************************************/
11793
11794
/**
11795
 * \brief Convert in place to ESRI WKT format.
11796
 *
11797
 * This function is the same as the C++ method
11798
 * OGRSpatialReference::morphToESRI().
11799
 */
11800
OGRErr OSRMorphToESRI(OGRSpatialReferenceH hSRS)
11801
11802
0
{
11803
0
    VALIDATE_POINTER1(hSRS, "OSRMorphToESRI", OGRERR_FAILURE);
11804
11805
0
    return OGRSpatialReference::FromHandle(hSRS)->morphToESRI();
11806
0
}
11807
11808
/************************************************************************/
11809
/*                           morphFromESRI()                            */
11810
/************************************************************************/
11811
11812
/**
11813
 * \brief Convert in place from ESRI WKT format.
11814
 *
11815
 * The value notes of this coordinate system are modified in various manners
11816
 * to adhere more closely to the WKT standard.  This mostly involves
11817
 * translating a variety of ESRI names for projections, arguments and
11818
 * datums to "standard" names, as defined by Adam Gawne-Cain's reference
11819
 * translation of EPSG to WKT for the CT specification.
11820
 *
11821
 * \note Since GDAL 3.0, this function is essentially a no-operation, since
11822
 * morphing from ESRI is automatically done by importFromWkt(). Its only
11823
 * effect is to undo the effect of a potential prior call to morphToESRI().
11824
 *
11825
 * This does the same as the C function OSRMorphFromESRI().
11826
 *
11827
 * @return OGRERR_NONE unless something goes badly wrong.
11828
 * @deprecated
11829
 */
11830
11831
OGRErr OGRSpatialReference::morphFromESRI()
11832
11833
0
{
11834
0
    TAKE_OPTIONAL_LOCK();
11835
11836
0
    d->refreshProjObj();
11837
0
    d->setMorphToESRI(false);
11838
11839
0
    return OGRERR_NONE;
11840
0
}
11841
11842
/************************************************************************/
11843
/*                          OSRMorphFromESRI()                          */
11844
/************************************************************************/
11845
11846
/**
11847
 * \brief Convert in place from ESRI WKT format.
11848
 *
11849
 * This function is the same as the C++ method
11850
 * OGRSpatialReference::morphFromESRI().
11851
 */
11852
OGRErr OSRMorphFromESRI(OGRSpatialReferenceH hSRS)
11853
11854
0
{
11855
0
    VALIDATE_POINTER1(hSRS, "OSRMorphFromESRI", OGRERR_FAILURE);
11856
11857
0
    return OGRSpatialReference::FromHandle(hSRS)->morphFromESRI();
11858
0
}
11859
11860
/************************************************************************/
11861
/*                            FindMatches()                             */
11862
/************************************************************************/
11863
11864
/**
11865
 * \brief Try to identify a match between the passed SRS and a related SRS
11866
 * in a catalog.
11867
 *
11868
 * Matching may be partial, or may fail.
11869
 * Returned entries will be sorted by decreasing match confidence (first
11870
 * entry has the highest match confidence).
11871
 *
11872
 * The exact way matching is done may change in future versions. Starting with
11873
 * GDAL 3.0, it relies on PROJ' proj_identify() function.
11874
 *
11875
 * This method is the same as OSRFindMatches().
11876
 *
11877
 * @param papszOptions NULL terminated list of options or NULL
11878
 * @param pnEntries Output parameter. Number of values in the returned array.
11879
 * @param ppanMatchConfidence Output parameter (or NULL). *ppanMatchConfidence
11880
 * will be allocated to an array of *pnEntries whose values between 0 and 100
11881
 * indicate the confidence in the match. 100 is the highest confidence level.
11882
 * The array must be freed with CPLFree().
11883
 *
11884
 * @return an array of SRS that match the passed SRS, or NULL. Must be freed
11885
 * with OSRFreeSRSArray()
11886
 *
11887
 *
11888
 * @see OGRSpatialReference::FindBestMatch()
11889
 */
11890
OGRSpatialReferenceH *
11891
OGRSpatialReference::FindMatches(char **papszOptions, int *pnEntries,
11892
                                 int **ppanMatchConfidence) const
11893
0
{
11894
0
    TAKE_OPTIONAL_LOCK();
11895
11896
0
    CPL_IGNORE_RET_VAL(papszOptions);
11897
11898
0
    if (pnEntries)
11899
0
        *pnEntries = 0;
11900
0
    if (ppanMatchConfidence)
11901
0
        *ppanMatchConfidence = nullptr;
11902
11903
0
    d->refreshProjObj();
11904
0
    if (!d->m_pj_crs)
11905
0
        return nullptr;
11906
11907
0
    int *panConfidence = nullptr;
11908
0
    auto ctxt = d->getPROJContext();
11909
0
    auto list =
11910
0
        proj_identify(ctxt, d->m_pj_crs, nullptr, nullptr, &panConfidence);
11911
0
    if (!list)
11912
0
        return nullptr;
11913
11914
0
    const int nMatches = proj_list_get_count(list);
11915
11916
0
    if (pnEntries)
11917
0
        *pnEntries = static_cast<int>(nMatches);
11918
0
    OGRSpatialReferenceH *pahRet = static_cast<OGRSpatialReferenceH *>(
11919
0
        CPLCalloc(sizeof(OGRSpatialReferenceH), nMatches + 1));
11920
0
    if (ppanMatchConfidence)
11921
0
    {
11922
0
        *ppanMatchConfidence =
11923
0
            static_cast<int *>(CPLMalloc(sizeof(int) * (nMatches + 1)));
11924
0
    }
11925
11926
0
    bool bSortAgain = false;
11927
11928
0
    for (int i = 0; i < nMatches; i++)
11929
0
    {
11930
0
        PJ *obj = proj_list_get(ctxt, list, i);
11931
0
        CPLAssert(obj);
11932
0
        OGRSpatialReference *poSRS = new OGRSpatialReference();
11933
0
        poSRS->d->setPjCRS(obj);
11934
0
        pahRet[i] = ToHandle(poSRS);
11935
11936
        // Identify matches that only differ by axis order
11937
0
        if (panConfidence[i] == 50 && GetAxesCount() == 2 &&
11938
0
            poSRS->GetAxesCount() == 2 &&
11939
0
            GetDataAxisToSRSAxisMapping() == std::vector<int>{1, 2})
11940
0
        {
11941
0
            OGRAxisOrientation eThisAxis0 = OAO_Other;
11942
0
            OGRAxisOrientation eThisAxis1 = OAO_Other;
11943
0
            OGRAxisOrientation eSRSAxis0 = OAO_Other;
11944
0
            OGRAxisOrientation eSRSAxis1 = OAO_Other;
11945
0
            GetAxis(nullptr, 0, &eThisAxis0);
11946
0
            GetAxis(nullptr, 1, &eThisAxis1);
11947
0
            poSRS->GetAxis(nullptr, 0, &eSRSAxis0);
11948
0
            poSRS->GetAxis(nullptr, 1, &eSRSAxis1);
11949
0
            if (eThisAxis0 == OAO_East && eThisAxis1 == OAO_North &&
11950
0
                eSRSAxis0 == OAO_North && eSRSAxis1 == OAO_East)
11951
0
            {
11952
0
                auto pj_crs_normalized =
11953
0
                    proj_normalize_for_visualization(ctxt, poSRS->d->m_pj_crs);
11954
0
                if (pj_crs_normalized)
11955
0
                {
11956
0
                    if (proj_is_equivalent_to(d->m_pj_crs, pj_crs_normalized,
11957
0
                                              PJ_COMP_EQUIVALENT))
11958
0
                    {
11959
0
                        bSortAgain = true;
11960
0
                        panConfidence[i] = 90;
11961
0
                        poSRS->SetDataAxisToSRSAxisMapping({2, 1});
11962
0
                    }
11963
0
                    proj_destroy(pj_crs_normalized);
11964
0
                }
11965
0
            }
11966
0
        }
11967
11968
0
        if (ppanMatchConfidence)
11969
0
            (*ppanMatchConfidence)[i] = panConfidence[i];
11970
0
    }
11971
11972
0
    if (bSortAgain)
11973
0
    {
11974
0
        std::vector<int> anIndices;
11975
0
        for (int i = 0; i < nMatches; ++i)
11976
0
            anIndices.push_back(i);
11977
11978
0
        std::stable_sort(anIndices.begin(), anIndices.end(),
11979
0
                         [&panConfidence](int i, int j)
11980
0
                         { return panConfidence[i] > panConfidence[j]; });
11981
11982
0
        OGRSpatialReferenceH *pahRetSorted =
11983
0
            static_cast<OGRSpatialReferenceH *>(
11984
0
                CPLCalloc(sizeof(OGRSpatialReferenceH), nMatches + 1));
11985
0
        for (int i = 0; i < nMatches; ++i)
11986
0
        {
11987
0
            pahRetSorted[i] = pahRet[anIndices[i]];
11988
0
            if (ppanMatchConfidence)
11989
0
                (*ppanMatchConfidence)[i] = panConfidence[anIndices[i]];
11990
0
        }
11991
0
        CPLFree(pahRet);
11992
0
        pahRet = pahRetSorted;
11993
0
    }
11994
11995
0
    pahRet[nMatches] = nullptr;
11996
0
    proj_list_destroy(list);
11997
0
    proj_int_list_destroy(panConfidence);
11998
11999
0
    return pahRet;
12000
0
}
12001
12002
/************************************************************************/
12003
/*                          importFromEPSGA()                           */
12004
/************************************************************************/
12005
12006
/**
12007
 * \brief  Initialize SRS based on EPSG geographic, projected or vertical CRS
12008
 * code.
12009
 *
12010
 * This method will initialize the spatial reference based on the
12011
 * passed in EPSG CRS code found in the PROJ database.
12012
 *
12013
 * Since GDAL 3.0, this method is identical to importFromEPSG().
12014
 *
12015
 * Before GDAL 3.0.3, this method would try to attach a 3-parameter or
12016
 * 7-parameter Helmert transformation to WGS84 when there is one and only one
12017
 * such method available for the CRS. This behavior might not always be
12018
 * desirable, so starting with GDAL 3.0.3, this is no longer done unless
12019
 * the OSR_ADD_TOWGS84_ON_IMPORT_FROM_EPSG configuration option is set to YES.
12020
 * The AddGuessedTOWGS84() method can also be used for that purpose.
12021
 *
12022
 * The method will also by default substitute a deprecated EPSG code by its
12023
 * non-deprecated replacement. If this behavior is not desired, the
12024
 * OSR_USE_NON_DEPRECATED configuration option can be set to NO.
12025
 *
12026
 * This method is the same as the C function OSRImportFromEPSGA().
12027
 *
12028
 * @param nCode a CRS code.
12029
 *
12030
 * @return OGRERR_NONE on success, or an error code on failure.
12031
 */
12032
12033
OGRErr OGRSpatialReference::importFromEPSGA(int nCode)
12034
12035
0
{
12036
0
    TAKE_OPTIONAL_LOCK();
12037
12038
0
    Clear();
12039
12040
0
    const char *pszUseNonDeprecated =
12041
0
        CPLGetConfigOption("OSR_USE_NON_DEPRECATED", nullptr);
12042
0
    const bool bUseNonDeprecated =
12043
0
        CPLTestBool(pszUseNonDeprecated ? pszUseNonDeprecated : "YES");
12044
0
    const bool bAddTOWGS84 = CPLTestBool(
12045
0
        CPLGetConfigOption("OSR_ADD_TOWGS84_ON_IMPORT_FROM_EPSG", "NO"));
12046
0
    auto tlsCache = OSRGetProjTLSCache();
12047
0
    if (tlsCache)
12048
0
    {
12049
0
        auto cachedObj =
12050
0
            tlsCache->GetPJForEPSGCode(nCode, bUseNonDeprecated, bAddTOWGS84);
12051
0
        if (cachedObj)
12052
0
        {
12053
0
            d->setPjCRS(cachedObj);
12054
0
            return OGRERR_NONE;
12055
0
        }
12056
0
    }
12057
12058
0
    CPLString osCode;
12059
0
    osCode.Printf("%d", nCode);
12060
0
    PJ *obj;
12061
0
    constexpr int FIRST_NON_DEPRECATED_ESRI_CODE = 53001;
12062
0
    if (nCode < FIRST_NON_DEPRECATED_ESRI_CODE)
12063
0
    {
12064
0
        obj = proj_create_from_database(d->getPROJContext(), "EPSG",
12065
0
                                        osCode.c_str(), PJ_CATEGORY_CRS, true,
12066
0
                                        nullptr);
12067
0
        if (!obj)
12068
0
        {
12069
0
            return OGRERR_FAILURE;
12070
0
        }
12071
0
    }
12072
0
    else
12073
0
    {
12074
        // Likely to be an ESRI CRS...
12075
0
        CPLErr eLastErrorType = CE_None;
12076
0
        CPLErrorNum eLastErrorNum = CPLE_None;
12077
0
        std::string osLastErrorMsg;
12078
0
        bool bIsESRI = false;
12079
0
        {
12080
0
            CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
12081
0
            CPLErrorReset();
12082
0
            obj = proj_create_from_database(d->getPROJContext(), "EPSG",
12083
0
                                            osCode.c_str(), PJ_CATEGORY_CRS,
12084
0
                                            true, nullptr);
12085
0
            if (!obj)
12086
0
            {
12087
0
                eLastErrorType = CPLGetLastErrorType();
12088
0
                eLastErrorNum = CPLGetLastErrorNo();
12089
0
                osLastErrorMsg = CPLGetLastErrorMsg();
12090
0
                obj = proj_create_from_database(d->getPROJContext(), "ESRI",
12091
0
                                                osCode.c_str(), PJ_CATEGORY_CRS,
12092
0
                                                true, nullptr);
12093
0
                if (obj)
12094
0
                    bIsESRI = true;
12095
0
            }
12096
0
        }
12097
0
        if (!obj)
12098
0
        {
12099
0
            if (eLastErrorType != CE_None)
12100
0
                CPLError(eLastErrorType, eLastErrorNum, "%s",
12101
0
                         osLastErrorMsg.c_str());
12102
0
            return OGRERR_FAILURE;
12103
0
        }
12104
0
        if (bIsESRI)
12105
0
        {
12106
0
            CPLError(CE_Warning, CPLE_AppDefined,
12107
0
                     "EPSG:%d is not a valid CRS code, but ESRI:%d is. "
12108
0
                     "Assuming ESRI:%d was meant",
12109
0
                     nCode, nCode, nCode);
12110
0
        }
12111
0
    }
12112
12113
0
    if (bUseNonDeprecated && proj_is_deprecated(obj))
12114
0
    {
12115
0
        auto list = proj_get_non_deprecated(d->getPROJContext(), obj);
12116
0
        if (list)
12117
0
        {
12118
0
            const auto count = proj_list_get_count(list);
12119
0
            if (count == 1)
12120
0
            {
12121
0
                auto nonDeprecated =
12122
0
                    proj_list_get(d->getPROJContext(), list, 0);
12123
0
                if (nonDeprecated)
12124
0
                {
12125
0
                    if (pszUseNonDeprecated == nullptr)
12126
0
                    {
12127
0
                        const char *pszNewAuth =
12128
0
                            proj_get_id_auth_name(nonDeprecated, 0);
12129
0
                        const char *pszNewCode =
12130
0
                            proj_get_id_code(nonDeprecated, 0);
12131
0
                        CPLError(CE_Warning, CPLE_AppDefined,
12132
0
                                 "CRS EPSG:%d is deprecated. "
12133
0
                                 "Its non-deprecated replacement %s:%s "
12134
0
                                 "will be used instead. "
12135
0
                                 "To use the original CRS, set the "
12136
0
                                 "OSR_USE_NON_DEPRECATED "
12137
0
                                 "configuration option to NO.",
12138
0
                                 nCode, pszNewAuth ? pszNewAuth : "(null)",
12139
0
                                 pszNewCode ? pszNewCode : "(null)");
12140
0
                    }
12141
0
                    proj_destroy(obj);
12142
0
                    obj = nonDeprecated;
12143
0
                }
12144
0
            }
12145
0
        }
12146
0
        proj_list_destroy(list);
12147
0
    }
12148
12149
0
    if (bAddTOWGS84)
12150
0
    {
12151
0
        auto boundCRS = proj_crs_create_bound_crs_to_WGS84(d->getPROJContext(),
12152
0
                                                           obj, nullptr);
12153
0
        if (boundCRS)
12154
0
        {
12155
0
            proj_destroy(obj);
12156
0
            obj = boundCRS;
12157
0
        }
12158
0
    }
12159
12160
0
    d->setPjCRS(obj);
12161
12162
0
    if (tlsCache)
12163
0
    {
12164
0
        tlsCache->CachePJForEPSGCode(nCode, bUseNonDeprecated, bAddTOWGS84,
12165
0
                                     obj);
12166
0
    }
12167
12168
0
    return OGRERR_NONE;
12169
0
}
12170
12171
/************************************************************************/
12172
/*                          AddGuessedTOWGS84()                         */
12173
/************************************************************************/
12174
12175
/**
12176
 * \brief  Try to add a a 3-parameter or 7-parameter Helmert transformation
12177
 * to WGS84.
12178
 *
12179
 * This method try to attach a 3-parameter or 7-parameter Helmert transformation
12180
 * to WGS84 when there is one and only one such method available for the CRS.
12181
 * Note: this is more restrictive to how GDAL < 3 worked.
12182
 *
12183
 * This method is the same as the C function OSRAddGuessedTOWGS84().
12184
 *
12185
 * @return OGRERR_NONE on success, or an error code on failure (the CRS has
12186
 * already a transformation to WGS84 or none matching could be found).
12187
 *
12188
 * @since GDAL 3.0.3
12189
 */
12190
OGRErr OGRSpatialReference::AddGuessedTOWGS84()
12191
0
{
12192
0
    TAKE_OPTIONAL_LOCK();
12193
12194
0
    d->refreshProjObj();
12195
0
    if (!d->m_pj_crs)
12196
0
        return OGRERR_FAILURE;
12197
0
    auto boundCRS = GDAL_proj_crs_create_bound_crs_to_WGS84(
12198
0
        d->getPROJContext(), d->m_pj_crs, false, true);
12199
0
    if (!boundCRS)
12200
0
    {
12201
0
        return OGRERR_FAILURE;
12202
0
    }
12203
0
    d->setPjCRS(boundCRS);
12204
0
    return OGRERR_NONE;
12205
0
}
12206
12207
/************************************************************************/
12208
/*                         OSRImportFromEPSGA()                         */
12209
/************************************************************************/
12210
12211
/**
12212
 * \brief  Try to add a a 3-parameter or 7-parameter Helmert transformation
12213
 * to WGS84.
12214
 *
12215
 * This function is the same as OGRSpatialReference::AddGuessedTOWGS84().
12216
 *
12217
 * @since GDAL 3.0.3
12218
 */
12219
12220
OGRErr OSRAddGuessedTOWGS84(OGRSpatialReferenceH hSRS)
12221
12222
0
{
12223
0
    VALIDATE_POINTER1(hSRS, "OSRAddGuessedTOWGS84", OGRERR_FAILURE);
12224
12225
0
    return OGRSpatialReference::FromHandle(hSRS)->AddGuessedTOWGS84();
12226
0
}
12227
12228
/************************************************************************/
12229
/*                         OSRImportFromEPSGA()                         */
12230
/************************************************************************/
12231
12232
/**
12233
 * \brief  Initialize SRS based on EPSG geographic, projected or vertical CRS
12234
 * code.
12235
 *
12236
 * This function is the same as OGRSpatialReference::importFromEPSGA().
12237
 */
12238
12239
OGRErr CPL_STDCALL OSRImportFromEPSGA(OGRSpatialReferenceH hSRS, int nCode)
12240
12241
0
{
12242
0
    VALIDATE_POINTER1(hSRS, "OSRImportFromEPSGA", OGRERR_FAILURE);
12243
12244
0
    return OGRSpatialReference::FromHandle(hSRS)->importFromEPSGA(nCode);
12245
0
}
12246
12247
/************************************************************************/
12248
/*                           importFromEPSG()                           */
12249
/************************************************************************/
12250
12251
/**
12252
 * \brief  Initialize SRS based on EPSG geographic, projected or vertical CRS
12253
 * code.
12254
 *
12255
 * This method will initialize the spatial reference based on the
12256
 * passed in EPSG CRS code found in the PROJ database.
12257
 *
12258
 * This method is the same as the C function OSRImportFromEPSG().
12259
 *
12260
 * Before GDAL 3.0.3, this method would try to attach a 3-parameter or
12261
 * 7-parameter Helmert transformation to WGS84 when there is one and only one
12262
 * such method available for the CRS. This behavior might not always be
12263
 * desirable, so starting with GDAL 3.0.3, this is no longer done unless
12264
 * the OSR_ADD_TOWGS84_ON_IMPORT_FROM_EPSG configuration option is set to YES.
12265
 *
12266
 * @param nCode a GCS or PCS code from the horizontal coordinate system table.
12267
 *
12268
 * @return OGRERR_NONE on success, or an error code on failure.
12269
 */
12270
12271
OGRErr OGRSpatialReference::importFromEPSG(int nCode)
12272
12273
0
{
12274
0
    return importFromEPSGA(nCode);
12275
0
}
12276
12277
/************************************************************************/
12278
/*                         OSRImportFromEPSG()                          */
12279
/************************************************************************/
12280
12281
/**
12282
 * \brief  Initialize SRS based on EPSG geographic, projected or vertical CRS
12283
 * code.
12284
 *
12285
 * This function is the same as OGRSpatialReference::importFromEPSG().
12286
 */
12287
12288
OGRErr CPL_STDCALL OSRImportFromEPSG(OGRSpatialReferenceH hSRS, int nCode)
12289
12290
0
{
12291
0
    VALIDATE_POINTER1(hSRS, "OSRImportFromEPSG", OGRERR_FAILURE);
12292
12293
0
    return OGRSpatialReference::FromHandle(hSRS)->importFromEPSG(nCode);
12294
0
}
12295
12296
/************************************************************************/
12297
/*                        EPSGTreatsAsLatLong()                         */
12298
/************************************************************************/
12299
12300
/**
12301
 * \brief This method returns TRUE if this geographic coordinate
12302
 * system should be treated as having lat/long coordinate ordering.
12303
 *
12304
 * Currently this returns TRUE for all geographic coordinate systems
12305
 * with axes set defining it as lat, long (prior to GDAL 3.10, it
12306
 * also checked that the CRS had belonged to EPSG authority, but this check
12307
 * has now been removed).
12308
 *
12309
 * \note Important change of behavior since GDAL 3.0. In previous versions,
12310
 * geographic CRS imported with importFromEPSG() would cause this method to
12311
 * return FALSE on them, whereas now it returns TRUE, since importFromEPSG()
12312
 * is now equivalent to importFromEPSGA().
12313
 *
12314
 * FALSE will be returned for all coordinate systems that are not geographic,
12315
 * or whose axes ordering is not latitude, longitude.
12316
 *
12317
 * This method is the same as the C function OSREPSGTreatsAsLatLong().
12318
 *
12319
 * @return TRUE or FALSE.
12320
 */
12321
12322
int OGRSpatialReference::EPSGTreatsAsLatLong() const
12323
12324
0
{
12325
0
    TAKE_OPTIONAL_LOCK();
12326
12327
0
    if (!IsGeographic())
12328
0
        return FALSE;
12329
12330
0
    d->demoteFromBoundCRS();
12331
12332
0
    bool ret = false;
12333
0
    if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
12334
0
    {
12335
0
        auto horizCRS =
12336
0
            proj_crs_get_sub_crs(d->getPROJContext(), d->m_pj_crs, 0);
12337
0
        if (horizCRS)
12338
0
        {
12339
0
            auto cs =
12340
0
                proj_crs_get_coordinate_system(d->getPROJContext(), horizCRS);
12341
0
            if (cs)
12342
0
            {
12343
0
                const char *pszDirection = nullptr;
12344
0
                if (proj_cs_get_axis_info(d->getPROJContext(), cs, 0, nullptr,
12345
0
                                          nullptr, &pszDirection, nullptr,
12346
0
                                          nullptr, nullptr, nullptr))
12347
0
                {
12348
0
                    if (EQUAL(pszDirection, "north"))
12349
0
                    {
12350
0
                        ret = true;
12351
0
                    }
12352
0
                }
12353
12354
0
                proj_destroy(cs);
12355
0
            }
12356
12357
0
            proj_destroy(horizCRS);
12358
0
        }
12359
0
    }
12360
0
    else
12361
0
    {
12362
0
        auto cs =
12363
0
            proj_crs_get_coordinate_system(d->getPROJContext(), d->m_pj_crs);
12364
0
        if (cs)
12365
0
        {
12366
0
            const char *pszDirection = nullptr;
12367
0
            if (proj_cs_get_axis_info(d->getPROJContext(), cs, 0, nullptr,
12368
0
                                      nullptr, &pszDirection, nullptr, nullptr,
12369
0
                                      nullptr, nullptr))
12370
0
            {
12371
0
                if (EQUAL(pszDirection, "north"))
12372
0
                {
12373
0
                    ret = true;
12374
0
                }
12375
0
            }
12376
12377
0
            proj_destroy(cs);
12378
0
        }
12379
0
    }
12380
0
    d->undoDemoteFromBoundCRS();
12381
12382
0
    return ret;
12383
0
}
12384
12385
/************************************************************************/
12386
/*                       OSREPSGTreatsAsLatLong()                       */
12387
/************************************************************************/
12388
12389
/**
12390
 * \brief This function returns TRUE if this geographic coordinate
12391
 * system should be treated as having lat/long coordinate ordering.
12392
 *
12393
 * This function is the same as OGRSpatialReference::OSREPSGTreatsAsLatLong().
12394
 */
12395
12396
int OSREPSGTreatsAsLatLong(OGRSpatialReferenceH hSRS)
12397
12398
0
{
12399
0
    VALIDATE_POINTER1(hSRS, "OSREPSGTreatsAsLatLong", OGRERR_FAILURE);
12400
12401
0
    return OGRSpatialReference::FromHandle(hSRS)->EPSGTreatsAsLatLong();
12402
0
}
12403
12404
/************************************************************************/
12405
/*                     EPSGTreatsAsNorthingEasting()                    */
12406
/************************************************************************/
12407
12408
/**
12409
 * \brief This method returns TRUE if this projected coordinate
12410
 * system should be treated as having northing/easting coordinate ordering.
12411
 *
12412
 * Currently this returns TRUE for all projected coordinate systems
12413
 * with axes set defining it as northing, easting (prior to GDAL 3.10, it
12414
 * also checked that the CRS had belonged to EPSG authority, but this check
12415
 * has now been removed).
12416
 *
12417
 * \note Important change of behavior since GDAL 3.0. In previous versions,
12418
 * projected CRS with northing, easting axis order imported with
12419
 * importFromEPSG() would cause this method to
12420
 * return FALSE on them, whereas now it returns TRUE, since importFromEPSG()
12421
 * is now equivalent to importFromEPSGA().
12422
 *
12423
 * FALSE will be returned for all coordinate systems that are not projected,
12424
 * or whose axes ordering is not northing, easting.
12425
 *
12426
 * This method is the same as the C function EPSGTreatsAsNorthingEasting().
12427
 *
12428
 * @return TRUE or FALSE.
12429
 *
12430
 */
12431
12432
int OGRSpatialReference::EPSGTreatsAsNorthingEasting() const
12433
12434
0
{
12435
0
    TAKE_OPTIONAL_LOCK();
12436
12437
0
    if (!IsProjected())
12438
0
        return FALSE;
12439
12440
0
    d->demoteFromBoundCRS();
12441
0
    PJ *projCRS;
12442
0
    const auto ctxt = d->getPROJContext();
12443
0
    if (d->m_pjType == PJ_TYPE_COMPOUND_CRS)
12444
0
    {
12445
0
        projCRS = proj_crs_get_sub_crs(ctxt, d->m_pj_crs, 0);
12446
0
        if (!projCRS || proj_get_type(projCRS) != PJ_TYPE_PROJECTED_CRS)
12447
0
        {
12448
0
            d->undoDemoteFromBoundCRS();
12449
0
            proj_destroy(projCRS);
12450
0
            return FALSE;
12451
0
        }
12452
0
    }
12453
0
    else
12454
0
    {
12455
0
        projCRS = proj_clone(ctxt, d->m_pj_crs);
12456
0
    }
12457
12458
0
    bool ret = false;
12459
0
    auto cs = proj_crs_get_coordinate_system(ctxt, projCRS);
12460
0
    proj_destroy(projCRS);
12461
0
    d->undoDemoteFromBoundCRS();
12462
12463
0
    if (cs)
12464
0
    {
12465
0
        ret = isNorthEastAxisOrder(ctxt, cs);
12466
0
        proj_destroy(cs);
12467
0
    }
12468
12469
0
    return ret;
12470
0
}
12471
12472
/************************************************************************/
12473
/*                     OSREPSGTreatsAsNorthingEasting()                 */
12474
/************************************************************************/
12475
12476
/**
12477
 * \brief This function returns TRUE if this projected coordinate
12478
 * system should be treated as having northing/easting coordinate ordering.
12479
 *
12480
 * This function is the same as
12481
 * OGRSpatialReference::EPSGTreatsAsNorthingEasting().
12482
 *
12483
 */
12484
12485
int OSREPSGTreatsAsNorthingEasting(OGRSpatialReferenceH hSRS)
12486
12487
0
{
12488
0
    VALIDATE_POINTER1(hSRS, "OSREPSGTreatsAsNorthingEasting", OGRERR_FAILURE);
12489
12490
0
    return OGRSpatialReference::FromHandle(hSRS)->EPSGTreatsAsNorthingEasting();
12491
0
}
12492
12493
/************************************************************************/
12494
/*                     ImportFromESRIWisconsinWKT()                     */
12495
/*                                                                      */
12496
/*      Search a ESRI State Plane WKT and import it.                    */
12497
/************************************************************************/
12498
12499
// This is only used by the HFA driver and somewhat dubious we really need that
12500
// Coming from an old ESRI merge
12501
12502
OGRErr OGRSpatialReference::ImportFromESRIWisconsinWKT(const char *prjName,
12503
                                                       double centralMeridian,
12504
                                                       double latOfOrigin,
12505
                                                       const char *unitsName,
12506
                                                       const char *crsName)
12507
0
{
12508
0
    TAKE_OPTIONAL_LOCK();
12509
12510
0
    if (centralMeridian < -93 || centralMeridian > -87)
12511
0
        return OGRERR_FAILURE;
12512
0
    if (latOfOrigin < 40 || latOfOrigin > 47)
12513
0
        return OGRERR_FAILURE;
12514
12515
    // If the CS name is known.
12516
0
    if (!prjName && !unitsName && crsName)
12517
0
    {
12518
0
        const PJ_TYPE type = PJ_TYPE_PROJECTED_CRS;
12519
0
        PJ_OBJ_LIST *list = proj_create_from_name(
12520
0
            d->getPROJContext(), "ESRI", crsName, &type, 1, false, 1, nullptr);
12521
0
        if (list)
12522
0
        {
12523
0
            if (proj_list_get_count(list) == 1)
12524
0
            {
12525
0
                auto crs = proj_list_get(d->getPROJContext(), list, 0);
12526
0
                if (crs)
12527
0
                {
12528
0
                    Clear();
12529
0
                    d->setPjCRS(crs);
12530
0
                    proj_list_destroy(list);
12531
0
                    return OGRERR_NONE;
12532
0
                }
12533
0
            }
12534
0
            proj_list_destroy(list);
12535
0
        }
12536
0
        return OGRERR_FAILURE;
12537
0
    }
12538
12539
0
    if (prjName == nullptr || unitsName == nullptr)
12540
0
    {
12541
0
        return OGRERR_FAILURE;
12542
0
    }
12543
12544
0
    const PJ_TYPE type = PJ_TYPE_PROJECTED_CRS;
12545
0
    PJ_OBJ_LIST *list = proj_create_from_name(d->getPROJContext(), "ESRI",
12546
0
                                              "NAD_1983_HARN_WISCRS_", &type, 1,
12547
0
                                              true, 0, nullptr);
12548
0
    if (list)
12549
0
    {
12550
0
        const auto listSize = proj_list_get_count(list);
12551
0
        for (int i = 0; i < listSize; i++)
12552
0
        {
12553
0
            auto crs = proj_list_get(d->getPROJContext(), list, i);
12554
0
            if (!crs)
12555
0
            {
12556
0
                continue;
12557
0
            }
12558
12559
0
            auto conv = proj_crs_get_coordoperation(d->getPROJContext(), crs);
12560
0
            if (!conv)
12561
0
            {
12562
0
                proj_destroy(crs);
12563
0
                continue;
12564
0
            }
12565
0
            const char *pszMethodCode = nullptr;
12566
0
            proj_coordoperation_get_method_info(
12567
0
                d->getPROJContext(), conv, nullptr, nullptr, &pszMethodCode);
12568
0
            const int nMethodCode = atoi(pszMethodCode ? pszMethodCode : "0");
12569
0
            if (!((EQUAL(prjName, SRS_PT_TRANSVERSE_MERCATOR) &&
12570
0
                   nMethodCode == EPSG_CODE_METHOD_TRANSVERSE_MERCATOR) ||
12571
0
                  (EQUAL(prjName, "Lambert_Conformal_Conic") &&
12572
0
                   nMethodCode ==
12573
0
                       EPSG_CODE_METHOD_LAMBERT_CONIC_CONFORMAL_1SP)))
12574
0
            {
12575
0
                proj_destroy(crs);
12576
0
                proj_destroy(conv);
12577
0
                continue;
12578
0
            }
12579
12580
0
            auto coordSys =
12581
0
                proj_crs_get_coordinate_system(d->getPROJContext(), crs);
12582
0
            if (!coordSys)
12583
0
            {
12584
0
                proj_destroy(crs);
12585
0
                proj_destroy(conv);
12586
0
                continue;
12587
0
            }
12588
12589
0
            double dfConvFactor = 0.0;
12590
0
            proj_cs_get_axis_info(d->getPROJContext(), coordSys, 0, nullptr,
12591
0
                                  nullptr, nullptr, &dfConvFactor, nullptr,
12592
0
                                  nullptr, nullptr);
12593
0
            proj_destroy(coordSys);
12594
12595
0
            if ((EQUAL(unitsName, "meters") && dfConvFactor != 1.0) ||
12596
0
                (!EQUAL(unitsName, "meters") &&
12597
0
                 std::fabs(dfConvFactor - CPLAtof(SRS_UL_US_FOOT_CONV)) >
12598
0
                     1e-10))
12599
0
            {
12600
0
                proj_destroy(crs);
12601
0
                proj_destroy(conv);
12602
0
                continue;
12603
0
            }
12604
12605
0
            int idx_lat = proj_coordoperation_get_param_index(
12606
0
                d->getPROJContext(), conv,
12607
0
                EPSG_NAME_PARAMETER_LATITUDE_OF_NATURAL_ORIGIN);
12608
0
            double valueLat = -1000;
12609
0
            proj_coordoperation_get_param(d->getPROJContext(), conv, idx_lat,
12610
0
                                          nullptr, nullptr, nullptr, &valueLat,
12611
0
                                          nullptr, nullptr, nullptr, nullptr,
12612
0
                                          nullptr, nullptr);
12613
0
            int idx_lon = proj_coordoperation_get_param_index(
12614
0
                d->getPROJContext(), conv,
12615
0
                EPSG_NAME_PARAMETER_LONGITUDE_OF_NATURAL_ORIGIN);
12616
0
            double valueLong = -1000;
12617
0
            proj_coordoperation_get_param(d->getPROJContext(), conv, idx_lon,
12618
0
                                          nullptr, nullptr, nullptr, &valueLong,
12619
0
                                          nullptr, nullptr, nullptr, nullptr,
12620
0
                                          nullptr, nullptr);
12621
0
            if (std::fabs(centralMeridian - valueLong) <= 1e-10 &&
12622
0
                std::fabs(latOfOrigin - valueLat) <= 1e-10)
12623
0
            {
12624
0
                Clear();
12625
0
                d->setPjCRS(crs);
12626
0
                proj_list_destroy(list);
12627
0
                proj_destroy(conv);
12628
0
                return OGRERR_NONE;
12629
0
            }
12630
12631
0
            proj_destroy(crs);
12632
0
            proj_destroy(conv);
12633
0
        }
12634
0
        proj_list_destroy(list);
12635
0
    }
12636
12637
0
    return OGRERR_FAILURE;
12638
0
}
12639
12640
/************************************************************************/
12641
/*                      GetAxisMappingStrategy()                        */
12642
/************************************************************************/
12643
12644
/** \brief Return the data axis to CRS axis mapping strategy.
12645
 *
12646
 * <ul>
12647
 * <li>OAMS_TRADITIONAL_GIS_ORDER means that for geographic CRS with
12648
 *     lat/long order, the data will still be long/lat ordered. Similarly for
12649
 *     a projected CRS with northing/easting order, the data will still be
12650
 *     easting/northing ordered.
12651
 * <li>OAMS_AUTHORITY_COMPLIANT means that the data axis will be identical to
12652
 *     the CRS axis.
12653
 * <li>OAMS_CUSTOM means that the data axis are customly defined with
12654
 *     SetDataAxisToSRSAxisMapping()
12655
 * </ul>
12656
 * @return the data axis to CRS axis mapping strategy.
12657
 * @since GDAL 3.0
12658
 */
12659
OSRAxisMappingStrategy OGRSpatialReference::GetAxisMappingStrategy() const
12660
0
{
12661
0
    TAKE_OPTIONAL_LOCK();
12662
12663
0
    return d->m_axisMappingStrategy;
12664
0
}
12665
12666
/************************************************************************/
12667
/*                      OSRGetAxisMappingStrategy()                     */
12668
/************************************************************************/
12669
12670
/** \brief Return the data axis to CRS axis mapping strategy.
12671
 *
12672
 * See OGRSpatialReference::GetAxisMappingStrategy()
12673
 * @since GDAL 3.0
12674
 */
12675
OSRAxisMappingStrategy OSRGetAxisMappingStrategy(OGRSpatialReferenceH hSRS)
12676
0
{
12677
0
    VALIDATE_POINTER1(hSRS, "OSRGetAxisMappingStrategy", OAMS_CUSTOM);
12678
12679
0
    return OGRSpatialReference::FromHandle(hSRS)->GetAxisMappingStrategy();
12680
0
}
12681
12682
/************************************************************************/
12683
/*                      SetAxisMappingStrategy()                        */
12684
/************************************************************************/
12685
12686
/** \brief Set the data axis to CRS axis mapping strategy.
12687
 *
12688
 * Starting with GDAL 3.5, the OSR_DEFAULT_AXIS_MAPPING_STRATEGY configuration
12689
 * option can be set to "TRADITIONAL_GIS_ORDER" / "AUTHORITY_COMPLIANT" (the
12690
 * later being the default value when the option is not set) to control the
12691
 * value of the data axis to CRS axis mapping strategy when a
12692
 * OSRSpatialReference object is created. Calling SetAxisMappingStrategy() will
12693
 * override this default value.
12694
 *
12695
 * See OGRSpatialReference::GetAxisMappingStrategy()
12696
 * @since GDAL 3.0
12697
 */
12698
void OGRSpatialReference::SetAxisMappingStrategy(
12699
    OSRAxisMappingStrategy strategy)
12700
0
{
12701
0
    TAKE_OPTIONAL_LOCK();
12702
12703
0
    d->m_axisMappingStrategy = strategy;
12704
0
    d->refreshAxisMapping();
12705
0
}
12706
12707
/************************************************************************/
12708
/*                      OSRSetAxisMappingStrategy()                     */
12709
/************************************************************************/
12710
12711
/** \brief Set the data axis to CRS axis mapping strategy.
12712
 *
12713
 * See OGRSpatialReference::SetAxisMappingStrategy()
12714
 * @since GDAL 3.0
12715
 */
12716
void OSRSetAxisMappingStrategy(OGRSpatialReferenceH hSRS,
12717
                               OSRAxisMappingStrategy strategy)
12718
0
{
12719
0
    VALIDATE_POINTER0(hSRS, "OSRSetAxisMappingStrategy");
12720
12721
0
    OGRSpatialReference::FromHandle(hSRS)->SetAxisMappingStrategy(strategy);
12722
0
}
12723
12724
/************************************************************************/
12725
/*                      GetDataAxisToSRSAxisMapping()                   */
12726
/************************************************************************/
12727
12728
/** \brief Return the data axis to SRS axis mapping.
12729
 *
12730
 * The number of elements of the vector will be the number of axis of the CRS.
12731
 * Values start at 1.
12732
 *
12733
 * If m = GetDataAxisToSRSAxisMapping(), then m[0] is the data axis number
12734
 * for the first axis of the CRS.
12735
 *
12736
 * @since GDAL 3.0
12737
 */
12738
const std::vector<int> &OGRSpatialReference::GetDataAxisToSRSAxisMapping() const
12739
0
{
12740
0
    TAKE_OPTIONAL_LOCK();
12741
12742
0
    return d->m_axisMapping;
12743
0
}
12744
12745
/************************************************************************/
12746
/*                     OSRGetDataAxisToSRSAxisMapping()                 */
12747
/************************************************************************/
12748
12749
/** \brief Return the data axis to SRS axis mapping.
12750
 *
12751
 * See OGRSpatialReference::GetDataAxisToSRSAxisMapping()
12752
 *
12753
 * @since GDAL 3.0
12754
 */
12755
const int *OSRGetDataAxisToSRSAxisMapping(OGRSpatialReferenceH hSRS,
12756
                                          int *pnCount)
12757
0
{
12758
0
    VALIDATE_POINTER1(hSRS, "OSRGetDataAxisToSRSAxisMapping", nullptr);
12759
0
    VALIDATE_POINTER1(pnCount, "OSRGetDataAxisToSRSAxisMapping", nullptr);
12760
12761
0
    const auto &v =
12762
0
        OGRSpatialReference::FromHandle(hSRS)->GetDataAxisToSRSAxisMapping();
12763
0
    *pnCount = static_cast<int>(v.size());
12764
0
    return v.data();
12765
0
}
12766
12767
/************************************************************************/
12768
/*                      SetDataAxisToSRSAxisMapping()                   */
12769
/************************************************************************/
12770
12771
/** \brief Set a custom data axis to CRS axis mapping.
12772
 *
12773
 * The number of elements of the mapping vector should be the number of axis
12774
 * of the CRS (as returned by GetAxesCount()) (although this method does not
12775
 * check that, beyond checking there are at least 2 elements, so that this
12776
 * method and setting the CRS can be done in any order).
12777
 * This is taken into account by OGRCoordinateTransformation to transform the
12778
 * order of coordinates to the order expected by the CRS before
12779
 * transformation, and back to the data order after transformation.
12780
 *
12781
 * The mapping[i] value (one based) represents the data axis number for the i(th)
12782
 * axis of the CRS. A negative value can also be used to ask for a sign
12783
 * reversal during coordinate transformation (to deal with northing vs southing,
12784
 * easting vs westing, heights vs depths).
12785
 *
12786
 * When used with OGRCoordinateTransformation,
12787
 * - the only valid values for mapping[0] (data axis number for the first axis
12788
 *   of the CRS) are 1, 2, -1, -2.
12789
 * - the only valid values for mapping[1] (data axis number for the second axis
12790
 *   of the CRS) are 1, 2, -1, -2.
12791
 *  - the only valid values mapping[2] are 3 or -3.
12792
 * Note: this method does not validate the values of mapping[].
12793
 *
12794
 * mapping=[2,1] typically expresses the inversion of axis between the data
12795
 * axis and the CRS axis for a 2D CRS.
12796
 *
12797
 * Automatically implies SetAxisMappingStrategy(OAMS_CUSTOM)
12798
 *
12799
 * This is the same as the C function OSRSetDataAxisToSRSAxisMapping().
12800
 *
12801
 * @param mapping The new data axis to CRS axis mapping.
12802
 *
12803
 * @since GDAL 3.0
12804
 * @see OGRSpatialReference::GetDataAxisToSRSAxisMapping()
12805
 */
12806
OGRErr OGRSpatialReference::SetDataAxisToSRSAxisMapping(
12807
    const std::vector<int> &mapping)
12808
0
{
12809
0
    TAKE_OPTIONAL_LOCK();
12810
12811
0
    if (mapping.size() < 2)
12812
0
        return OGRERR_FAILURE;
12813
0
    d->m_axisMappingStrategy = OAMS_CUSTOM;
12814
0
    d->m_axisMapping = mapping;
12815
0
    return OGRERR_NONE;
12816
0
}
12817
12818
/************************************************************************/
12819
/*                     OSRSetDataAxisToSRSAxisMapping()                 */
12820
/************************************************************************/
12821
12822
/** \brief Set a custom data axis to CRS axis mapping.
12823
 *
12824
 * Automatically implies SetAxisMappingStrategy(OAMS_CUSTOM)
12825
 *
12826
 * This is the same as the C++ method
12827
 * OGRSpatialReference::SetDataAxisToSRSAxisMapping()
12828
 *
12829
 * @since GDAL 3.1
12830
 */
12831
OGRErr OSRSetDataAxisToSRSAxisMapping(OGRSpatialReferenceH hSRS,
12832
                                      int nMappingSize, const int *panMapping)
12833
0
{
12834
0
    VALIDATE_POINTER1(hSRS, "OSRSetDataAxisToSRSAxisMapping", OGRERR_FAILURE);
12835
0
    VALIDATE_POINTER1(panMapping, "OSRSetDataAxisToSRSAxisMapping",
12836
0
                      OGRERR_FAILURE);
12837
12838
0
    if (nMappingSize < 0)
12839
0
        return OGRERR_FAILURE;
12840
12841
0
    std::vector<int> mapping(nMappingSize);
12842
0
    if (nMappingSize)
12843
0
        memcpy(&mapping[0], panMapping, nMappingSize * sizeof(int));
12844
0
    return OGRSpatialReference::FromHandle(hSRS)->SetDataAxisToSRSAxisMapping(
12845
0
        mapping);
12846
0
}
12847
12848
/************************************************************************/
12849
/*                               GetAreaOfUse()                         */
12850
/************************************************************************/
12851
12852
/** \brief Return the area of use of the CRS.
12853
 *
12854
 * This method is the same as the OSRGetAreaOfUse() function.
12855
 *
12856
 * @param pdfWestLongitudeDeg Pointer to a double to receive the western-most
12857
 * longitude, expressed in degree. Might be NULL. If the returned value is
12858
 * -1000, the bounding box is unknown.
12859
 * @param pdfSouthLatitudeDeg Pointer to a double to receive the southern-most
12860
 * latitude, expressed in degree. Might be NULL. If the returned value is -1000,
12861
 * the bounding box is unknown.
12862
 * @param pdfEastLongitudeDeg Pointer to a double to receive the eastern-most
12863
 * longitude, expressed in degree. Might be NULL. If the returned value is
12864
 * -1000, the bounding box is unknown.
12865
 * @param pdfNorthLatitudeDeg Pointer to a double to receive the northern-most
12866
 * latitude, expressed in degree. Might be NULL. If the returned value is -1000,
12867
 * the bounding box is unknown.
12868
 * @param ppszAreaName Pointer to a string to receive the name of the area of
12869
 * use. Might be NULL. Note that *ppszAreaName is short-lived and might be
12870
 * invalidated by further calls.
12871
 * @return true in case of success
12872
 * @since GDAL 3.0
12873
 */
12874
bool OGRSpatialReference::GetAreaOfUse(double *pdfWestLongitudeDeg,
12875
                                       double *pdfSouthLatitudeDeg,
12876
                                       double *pdfEastLongitudeDeg,
12877
                                       double *pdfNorthLatitudeDeg,
12878
                                       const char **ppszAreaName) const
12879
0
{
12880
0
    TAKE_OPTIONAL_LOCK();
12881
12882
0
    d->refreshProjObj();
12883
0
    if (!d->m_pj_crs)
12884
0
    {
12885
0
        return false;
12886
0
    }
12887
0
    d->demoteFromBoundCRS();
12888
0
    const char *pszAreaName = nullptr;
12889
0
    int bSuccess = proj_get_area_of_use(
12890
0
        d->getPROJContext(), d->m_pj_crs, pdfWestLongitudeDeg,
12891
0
        pdfSouthLatitudeDeg, pdfEastLongitudeDeg, pdfNorthLatitudeDeg,
12892
0
        &pszAreaName);
12893
0
    d->undoDemoteFromBoundCRS();
12894
0
    d->m_osAreaName = pszAreaName ? pszAreaName : "";
12895
0
    if (ppszAreaName)
12896
0
        *ppszAreaName = d->m_osAreaName.c_str();
12897
0
    return CPL_TO_BOOL(bSuccess);
12898
0
}
12899
12900
/************************************************************************/
12901
/*                               GetAreaOfUse()                         */
12902
/************************************************************************/
12903
12904
/** \brief Return the area of use of the CRS.
12905
 *
12906
 * This function is the same as the OGRSpatialReference::GetAreaOfUse() method.
12907
 *
12908
 * @since GDAL 3.0
12909
 */
12910
int OSRGetAreaOfUse(OGRSpatialReferenceH hSRS, double *pdfWestLongitudeDeg,
12911
                    double *pdfSouthLatitudeDeg, double *pdfEastLongitudeDeg,
12912
                    double *pdfNorthLatitudeDeg, const char **ppszAreaName)
12913
0
{
12914
0
    VALIDATE_POINTER1(hSRS, "OSRGetAreaOfUse", FALSE);
12915
12916
0
    return OGRSpatialReference::FromHandle(hSRS)->GetAreaOfUse(
12917
0
        pdfWestLongitudeDeg, pdfSouthLatitudeDeg, pdfEastLongitudeDeg,
12918
0
        pdfNorthLatitudeDeg, ppszAreaName);
12919
0
}
12920
12921
/************************************************************************/
12922
/*                     OSRGetCRSInfoListFromDatabase()                  */
12923
/************************************************************************/
12924
12925
/** \brief Enumerate CRS objects from the database.
12926
 *
12927
 * The returned object is an array of OSRCRSInfo* pointers, whose last
12928
 * entry is NULL. This array should be freed with OSRDestroyCRSInfoList()
12929
 *
12930
 * @param pszAuthName Authority name, used to restrict the search.
12931
 * Or NULL for all authorities.
12932
 * @param params Additional criteria. Must be set to NULL for now.
12933
 * @param pnOutResultCount Output parameter pointing to an integer to receive
12934
 * the size of the result list. Might be NULL
12935
 * @return an array of OSRCRSInfo* pointers to be freed with
12936
 * OSRDestroyCRSInfoList(), or NULL in case of error.
12937
 *
12938
 * @since GDAL 3.0
12939
 */
12940
OSRCRSInfo **
12941
OSRGetCRSInfoListFromDatabase(const char *pszAuthName,
12942
                              CPL_UNUSED const OSRCRSListParameters *params,
12943
                              int *pnOutResultCount)
12944
0
{
12945
0
    int nResultCount = 0;
12946
0
    auto projList = proj_get_crs_info_list_from_database(
12947
0
        OSRGetProjTLSContext(), pszAuthName, nullptr, &nResultCount);
12948
0
    if (pnOutResultCount)
12949
0
        *pnOutResultCount = nResultCount;
12950
0
    if (!projList)
12951
0
    {
12952
0
        return nullptr;
12953
0
    }
12954
0
    auto res = new OSRCRSInfo *[nResultCount + 1];
12955
0
    for (int i = 0; i < nResultCount; i++)
12956
0
    {
12957
0
        res[i] = new OSRCRSInfo;
12958
0
        res[i]->pszAuthName = projList[i]->auth_name
12959
0
                                  ? CPLStrdup(projList[i]->auth_name)
12960
0
                                  : nullptr;
12961
0
        res[i]->pszCode =
12962
0
            projList[i]->code ? CPLStrdup(projList[i]->code) : nullptr;
12963
0
        res[i]->pszName =
12964
0
            projList[i]->name ? CPLStrdup(projList[i]->name) : nullptr;
12965
0
        res[i]->eType = OSR_CRS_TYPE_OTHER;
12966
0
        switch (projList[i]->type)
12967
0
        {
12968
0
            case PJ_TYPE_GEOGRAPHIC_2D_CRS:
12969
0
                res[i]->eType = OSR_CRS_TYPE_GEOGRAPHIC_2D;
12970
0
                break;
12971
0
            case PJ_TYPE_GEOGRAPHIC_3D_CRS:
12972
0
                res[i]->eType = OSR_CRS_TYPE_GEOGRAPHIC_3D;
12973
0
                break;
12974
0
            case PJ_TYPE_GEOCENTRIC_CRS:
12975
0
                res[i]->eType = OSR_CRS_TYPE_GEOCENTRIC;
12976
0
                break;
12977
0
            case PJ_TYPE_PROJECTED_CRS:
12978
0
                res[i]->eType = OSR_CRS_TYPE_PROJECTED;
12979
0
                break;
12980
0
            case PJ_TYPE_VERTICAL_CRS:
12981
0
                res[i]->eType = OSR_CRS_TYPE_VERTICAL;
12982
0
                break;
12983
0
            case PJ_TYPE_COMPOUND_CRS:
12984
0
                res[i]->eType = OSR_CRS_TYPE_COMPOUND;
12985
0
                break;
12986
0
            default:
12987
0
                break;
12988
0
        }
12989
0
        res[i]->bDeprecated = projList[i]->deprecated;
12990
0
        res[i]->bBboxValid = projList[i]->bbox_valid;
12991
0
        res[i]->dfWestLongitudeDeg = projList[i]->west_lon_degree;
12992
0
        res[i]->dfSouthLatitudeDeg = projList[i]->south_lat_degree;
12993
0
        res[i]->dfEastLongitudeDeg = projList[i]->east_lon_degree;
12994
0
        res[i]->dfNorthLatitudeDeg = projList[i]->north_lat_degree;
12995
0
        res[i]->pszAreaName = projList[i]->area_name
12996
0
                                  ? CPLStrdup(projList[i]->area_name)
12997
0
                                  : nullptr;
12998
0
        res[i]->pszProjectionMethod =
12999
0
            projList[i]->projection_method_name
13000
0
                ? CPLStrdup(projList[i]->projection_method_name)
13001
0
                : nullptr;
13002
0
#if PROJ_AT_LEAST_VERSION(8, 1, 0)
13003
0
        res[i]->pszCelestialBodyName =
13004
0
            projList[i]->celestial_body_name
13005
0
                ? CPLStrdup(projList[i]->celestial_body_name)
13006
0
                : nullptr;
13007
#else
13008
        res[i]->pszCelestialBodyName =
13009
            res[i]->pszAuthName && EQUAL(res[i]->pszAuthName, "EPSG")
13010
                ? CPLStrdup("Earth")
13011
                : nullptr;
13012
#endif
13013
0
    }
13014
0
    res[nResultCount] = nullptr;
13015
0
    proj_crs_info_list_destroy(projList);
13016
0
    return res;
13017
0
}
13018
13019
/************************************************************************/
13020
/*                        OSRDestroyCRSInfoList()                       */
13021
/************************************************************************/
13022
13023
/** \brief Destroy the result returned by
13024
 * OSRGetCRSInfoListFromDatabase().
13025
 *
13026
 * @since GDAL 3.0
13027
 */
13028
void OSRDestroyCRSInfoList(OSRCRSInfo **list)
13029
0
{
13030
0
    if (list)
13031
0
    {
13032
0
        for (int i = 0; list[i] != nullptr; i++)
13033
0
        {
13034
0
            CPLFree(list[i]->pszAuthName);
13035
0
            CPLFree(list[i]->pszCode);
13036
0
            CPLFree(list[i]->pszName);
13037
0
            CPLFree(list[i]->pszAreaName);
13038
0
            CPLFree(list[i]->pszProjectionMethod);
13039
0
            CPLFree(list[i]->pszCelestialBodyName);
13040
0
            delete list[i];
13041
0
        }
13042
0
        delete[] list;
13043
0
    }
13044
0
}
13045
13046
/************************************************************************/
13047
/*                   OSRGetAuthorityListFromDatabase()                  */
13048
/************************************************************************/
13049
13050
/** \brief Return the list of CRS authorities used in the PROJ database.
13051
 *
13052
 * Such as "EPSG", "ESRI", "PROJ", "IGNF", "IAU_2015", etc.
13053
 *
13054
 * This is a direct mapping of https://proj.org/en/latest/development/reference/functions.html#c.proj_get_authorities_from_database
13055
 *
13056
 * @return nullptr in case of error, or a NULL terminated list of strings to
13057
 * free with CSLDestroy()
13058
 * @since GDAL 3.10
13059
 */
13060
char **OSRGetAuthorityListFromDatabase()
13061
0
{
13062
0
    PROJ_STRING_LIST list =
13063
0
        proj_get_authorities_from_database(OSRGetProjTLSContext());
13064
0
    if (!list)
13065
0
    {
13066
0
        return nullptr;
13067
0
    }
13068
0
    int count = 0;
13069
0
    while (list[count])
13070
0
        ++count;
13071
0
    char **res = static_cast<char **>(CPLCalloc(count + 1, sizeof(char *)));
13072
0
    for (int i = 0; i < count; ++i)
13073
0
        res[i] = CPLStrdup(list[i]);
13074
0
    proj_string_list_destroy(list);
13075
0
    return res;
13076
0
}
13077
13078
/************************************************************************/
13079
/*                    UpdateCoordinateSystemFromGeogCRS()               */
13080
/************************************************************************/
13081
13082
/*! @cond Doxygen_Suppress */
13083
/** \brief Used by gt_wkt_srs.cpp to create projected 3D CRS. Internal use only
13084
 *
13085
 * @since GDAL 3.1
13086
 */
13087
void OGRSpatialReference::UpdateCoordinateSystemFromGeogCRS()
13088
0
{
13089
0
    TAKE_OPTIONAL_LOCK();
13090
13091
0
    d->refreshProjObj();
13092
0
    if (!d->m_pj_crs)
13093
0
        return;
13094
0
    if (d->m_pjType != PJ_TYPE_PROJECTED_CRS)
13095
0
        return;
13096
0
    if (GetAxesCount() == 3)
13097
0
        return;
13098
0
    auto ctxt = d->getPROJContext();
13099
0
    auto baseCRS = proj_crs_get_geodetic_crs(ctxt, d->m_pj_crs);
13100
0
    if (!baseCRS)
13101
0
        return;
13102
0
    auto baseCRSCS = proj_crs_get_coordinate_system(ctxt, baseCRS);
13103
0
    if (!baseCRSCS)
13104
0
    {
13105
0
        proj_destroy(baseCRS);
13106
0
        return;
13107
0
    }
13108
0
    if (proj_cs_get_axis_count(ctxt, baseCRSCS) != 3)
13109
0
    {
13110
0
        proj_destroy(baseCRSCS);
13111
0
        proj_destroy(baseCRS);
13112
0
        return;
13113
0
    }
13114
0
    auto projCS = proj_crs_get_coordinate_system(ctxt, d->m_pj_crs);
13115
0
    if (!projCS || proj_cs_get_axis_count(ctxt, projCS) != 2)
13116
0
    {
13117
0
        proj_destroy(baseCRSCS);
13118
0
        proj_destroy(baseCRS);
13119
0
        proj_destroy(projCS);
13120
0
        return;
13121
0
    }
13122
13123
0
    PJ_AXIS_DESCRIPTION axis[3];
13124
0
    for (int i = 0; i < 3; i++)
13125
0
    {
13126
0
        const char *name = nullptr;
13127
0
        const char *abbreviation = nullptr;
13128
0
        const char *direction = nullptr;
13129
0
        double unit_conv_factor = 0;
13130
0
        const char *unit_name = nullptr;
13131
0
        proj_cs_get_axis_info(ctxt, i < 2 ? projCS : baseCRSCS, i, &name,
13132
0
                              &abbreviation, &direction, &unit_conv_factor,
13133
0
                              &unit_name, nullptr, nullptr);
13134
0
        axis[i].name = CPLStrdup(name);
13135
0
        axis[i].abbreviation = CPLStrdup(abbreviation);
13136
0
        axis[i].direction = CPLStrdup(direction);
13137
0
        axis[i].unit_name = CPLStrdup(unit_name);
13138
0
        axis[i].unit_conv_factor = unit_conv_factor;
13139
0
        axis[i].unit_type = PJ_UT_LINEAR;
13140
0
    }
13141
0
    proj_destroy(baseCRSCS);
13142
0
    proj_destroy(projCS);
13143
0
    auto cs = proj_create_cs(ctxt, PJ_CS_TYPE_CARTESIAN, 3, axis);
13144
0
    for (int i = 0; i < 3; i++)
13145
0
    {
13146
0
        CPLFree(axis[i].name);
13147
0
        CPLFree(axis[i].abbreviation);
13148
0
        CPLFree(axis[i].direction);
13149
0
        CPLFree(axis[i].unit_name);
13150
0
    }
13151
0
    if (!cs)
13152
0
    {
13153
0
        proj_destroy(baseCRS);
13154
0
        return;
13155
0
    }
13156
0
    auto conversion = proj_crs_get_coordoperation(ctxt, d->m_pj_crs);
13157
0
    auto crs = proj_create_projected_crs(ctxt, d->getProjCRSName(), baseCRS,
13158
0
                                         conversion, cs);
13159
0
    proj_destroy(baseCRS);
13160
0
    proj_destroy(conversion);
13161
0
    proj_destroy(cs);
13162
0
    d->setPjCRS(crs);
13163
0
}
13164
13165
/*! @endcond */
13166
13167
/************************************************************************/
13168
/*                             PromoteTo3D()                            */
13169
/************************************************************************/
13170
13171
/** \brief "Promotes" a 2D CRS to a 3D CRS one.
13172
 *
13173
 * The new axis will be ellipsoidal height, oriented upwards, and with metre
13174
 * units.
13175
 *
13176
 * @param pszName New name for the CRS. If set to NULL, the previous name will
13177
 * be used.
13178
 * @return OGRERR_NONE if no error occurred.
13179
 * @since GDAL 3.1 and PROJ 6.3
13180
 */
13181
OGRErr OGRSpatialReference::PromoteTo3D(const char *pszName)
13182
0
{
13183
0
    TAKE_OPTIONAL_LOCK();
13184
13185
0
    d->refreshProjObj();
13186
0
    if (!d->m_pj_crs)
13187
0
        return OGRERR_FAILURE;
13188
0
    auto newPj =
13189
0
        proj_crs_promote_to_3D(d->getPROJContext(), pszName, d->m_pj_crs);
13190
0
    if (!newPj)
13191
0
        return OGRERR_FAILURE;
13192
0
    d->setPjCRS(newPj);
13193
0
    return OGRERR_NONE;
13194
0
}
13195
13196
/************************************************************************/
13197
/*                             OSRPromoteTo3D()                         */
13198
/************************************************************************/
13199
13200
/** \brief "Promotes" a 2D CRS to a 3D CRS one.
13201
 *
13202
 * See OGRSpatialReference::PromoteTo3D()
13203
 *
13204
 * @since GDAL 3.1 and PROJ 6.3
13205
 */
13206
OGRErr OSRPromoteTo3D(OGRSpatialReferenceH hSRS, const char *pszName)
13207
0
{
13208
0
    VALIDATE_POINTER1(hSRS, "OSRPromoteTo3D", OGRERR_FAILURE);
13209
13210
0
    return OGRSpatialReference::FromHandle(hSRS)->PromoteTo3D(pszName);
13211
0
}
13212
13213
/************************************************************************/
13214
/*                             DemoteTo2D()                             */
13215
/************************************************************************/
13216
13217
/** \brief "Demote" a 3D CRS to a 2D CRS one.
13218
 *
13219
 * @param pszName New name for the CRS. If set to NULL, the previous name will
13220
 * be used.
13221
 * @return OGRERR_NONE if no error occurred.
13222
 * @since GDAL 3.2 and PROJ 6.3
13223
 */
13224
OGRErr OGRSpatialReference::DemoteTo2D(const char *pszName)
13225
0
{
13226
0
    TAKE_OPTIONAL_LOCK();
13227
13228
0
    d->refreshProjObj();
13229
0
    if (!d->m_pj_crs)
13230
0
        return OGRERR_FAILURE;
13231
0
    auto newPj =
13232
0
        proj_crs_demote_to_2D(d->getPROJContext(), pszName, d->m_pj_crs);
13233
0
    if (!newPj)
13234
0
        return OGRERR_FAILURE;
13235
0
    d->setPjCRS(newPj);
13236
0
    return OGRERR_NONE;
13237
0
}
13238
13239
/************************************************************************/
13240
/*                             OSRDemoteTo2D()                          */
13241
/************************************************************************/
13242
13243
/** \brief "Demote" a 3D CRS to a 2D CRS one.
13244
 *
13245
 * See OGRSpatialReference::DemoteTo2D()
13246
 *
13247
 * @since GDAL 3.2 and PROJ 6.3
13248
 */
13249
OGRErr OSRDemoteTo2D(OGRSpatialReferenceH hSRS, const char *pszName)
13250
0
{
13251
0
    VALIDATE_POINTER1(hSRS, "OSRDemoteTo2D", OGRERR_FAILURE);
13252
13253
0
    return OGRSpatialReference::FromHandle(hSRS)->DemoteTo2D(pszName);
13254
0
}
13255
13256
/************************************************************************/
13257
/*                           GetEPSGGeogCS()                            */
13258
/************************************************************************/
13259
13260
/** Try to establish what the EPSG code for this coordinate systems
13261
 * GEOGCS might be.  Returns -1 if no reasonable guess can be made.
13262
 *
13263
 * @return EPSG code
13264
 */
13265
13266
int OGRSpatialReference::GetEPSGGeogCS() const
13267
13268
0
{
13269
0
    TAKE_OPTIONAL_LOCK();
13270
13271
    /* -------------------------------------------------------------------- */
13272
    /*      Check axis order.                                               */
13273
    /* -------------------------------------------------------------------- */
13274
0
    auto poGeogCRS = std::unique_ptr<OGRSpatialReference>(CloneGeogCS());
13275
0
    if (!poGeogCRS)
13276
0
        return -1;
13277
13278
0
    bool ret = false;
13279
0
    poGeogCRS->d->demoteFromBoundCRS();
13280
0
    auto cs = proj_crs_get_coordinate_system(d->getPROJContext(),
13281
0
                                             poGeogCRS->d->m_pj_crs);
13282
0
    poGeogCRS->d->undoDemoteFromBoundCRS();
13283
0
    if (cs)
13284
0
    {
13285
0
        const char *pszDirection = nullptr;
13286
0
        if (proj_cs_get_axis_info(d->getPROJContext(), cs, 0, nullptr, nullptr,
13287
0
                                  &pszDirection, nullptr, nullptr, nullptr,
13288
0
                                  nullptr))
13289
0
        {
13290
0
            if (EQUAL(pszDirection, "north"))
13291
0
            {
13292
0
                ret = true;
13293
0
            }
13294
0
        }
13295
13296
0
        proj_destroy(cs);
13297
0
    }
13298
0
    if (!ret)
13299
0
        return -1;
13300
13301
    /* -------------------------------------------------------------------- */
13302
    /*      Do we already have it?                                          */
13303
    /* -------------------------------------------------------------------- */
13304
0
    const char *pszAuthName = GetAuthorityName("GEOGCS");
13305
0
    if (pszAuthName != nullptr && EQUAL(pszAuthName, "epsg"))
13306
0
        return atoi(GetAuthorityCode("GEOGCS"));
13307
13308
    /* -------------------------------------------------------------------- */
13309
    /*      Get the datum and geogcs names.                                 */
13310
    /* -------------------------------------------------------------------- */
13311
13312
0
    const char *pszGEOGCS = GetAttrValue("GEOGCS");
13313
0
    const char *pszDatum = GetAttrValue("DATUM");
13314
13315
    // We can only operate on coordinate systems with a geogcs.
13316
0
    OGRSpatialReference oSRSTmp;
13317
0
    if (pszGEOGCS == nullptr || pszDatum == nullptr)
13318
0
    {
13319
        // Calling GetAttrValue("GEOGCS") will fail on a CRS that can't be
13320
        // export to WKT1, so try to extract the geographic CRS through PROJ
13321
        // API with CopyGeogCSFrom() and get the nodes' values from it.
13322
0
        oSRSTmp.CopyGeogCSFrom(this);
13323
0
        pszGEOGCS = oSRSTmp.GetAttrValue("GEOGCS");
13324
0
        pszDatum = oSRSTmp.GetAttrValue("DATUM");
13325
0
        if (pszGEOGCS == nullptr || pszDatum == nullptr)
13326
0
        {
13327
0
            return -1;
13328
0
        }
13329
0
    }
13330
13331
    // Lookup geographic CRS name
13332
0
    const PJ_TYPE type = PJ_TYPE_GEOGRAPHIC_2D_CRS;
13333
0
    PJ_OBJ_LIST *list = proj_create_from_name(
13334
0
        d->getPROJContext(), nullptr, pszGEOGCS, &type, 1, false, 1, nullptr);
13335
0
    if (list)
13336
0
    {
13337
0
        const auto listSize = proj_list_get_count(list);
13338
0
        if (listSize == 1)
13339
0
        {
13340
0
            auto crs = proj_list_get(d->getPROJContext(), list, 0);
13341
0
            if (crs)
13342
0
            {
13343
0
                pszAuthName = proj_get_id_auth_name(crs, 0);
13344
0
                const char *pszCode = proj_get_id_code(crs, 0);
13345
0
                if (pszAuthName && pszCode && EQUAL(pszAuthName, "EPSG"))
13346
0
                {
13347
0
                    const int nCode = atoi(pszCode);
13348
0
                    proj_destroy(crs);
13349
0
                    proj_list_destroy(list);
13350
0
                    return nCode;
13351
0
                }
13352
0
                proj_destroy(crs);
13353
0
            }
13354
0
        }
13355
0
        proj_list_destroy(list);
13356
0
    }
13357
13358
    /* -------------------------------------------------------------------- */
13359
    /*      Is this a "well known" geographic coordinate system?            */
13360
    /* -------------------------------------------------------------------- */
13361
0
    const bool bWGS = strstr(pszGEOGCS, "WGS") != nullptr ||
13362
0
                      strstr(pszDatum, "WGS") ||
13363
0
                      strstr(pszGEOGCS, "World Geodetic System") ||
13364
0
                      strstr(pszGEOGCS, "World_Geodetic_System") ||
13365
0
                      strstr(pszDatum, "World Geodetic System") ||
13366
0
                      strstr(pszDatum, "World_Geodetic_System");
13367
13368
0
    const bool bNAD = strstr(pszGEOGCS, "NAD") != nullptr ||
13369
0
                      strstr(pszDatum, "NAD") ||
13370
0
                      strstr(pszGEOGCS, "North American") ||
13371
0
                      strstr(pszGEOGCS, "North_American") ||
13372
0
                      strstr(pszDatum, "North American") ||
13373
0
                      strstr(pszDatum, "North_American");
13374
13375
0
    if (bWGS && (strstr(pszGEOGCS, "84") || strstr(pszDatum, "84")))
13376
0
        return 4326;
13377
13378
0
    if (bWGS && (strstr(pszGEOGCS, "72") || strstr(pszDatum, "72")))
13379
0
        return 4322;
13380
13381
    // This is questionable as there are several 'flavors' of NAD83 that
13382
    // are not the same as 4269
13383
0
    if (bNAD && (strstr(pszGEOGCS, "83") || strstr(pszDatum, "83")))
13384
0
        return 4269;
13385
13386
0
    if (bNAD && (strstr(pszGEOGCS, "27") || strstr(pszDatum, "27")))
13387
0
        return 4267;
13388
13389
    /* -------------------------------------------------------------------- */
13390
    /*      If we know the datum, associate the most likely GCS with        */
13391
    /*      it.                                                             */
13392
    /* -------------------------------------------------------------------- */
13393
0
    const OGRSpatialReference &oActiveObj = oSRSTmp.IsEmpty() ? *this : oSRSTmp;
13394
0
    pszAuthName = oActiveObj.GetAuthorityName("GEOGCS|DATUM");
13395
0
    if (pszAuthName != nullptr && EQUAL(pszAuthName, "epsg") &&
13396
0
        GetPrimeMeridian() == 0.0)
13397
0
    {
13398
0
        const int nDatum = atoi(oActiveObj.GetAuthorityCode("GEOGCS|DATUM"));
13399
13400
0
        if (nDatum >= 6000 && nDatum <= 6999)
13401
0
            return nDatum - 2000;
13402
0
    }
13403
13404
0
    return -1;
13405
0
}
13406
13407
/************************************************************************/
13408
/*                          SetCoordinateEpoch()                        */
13409
/************************************************************************/
13410
13411
/** Set the coordinate epoch, as decimal year.
13412
 *
13413
 * In a dynamic CRS, coordinates of a point on the surface of the Earth may
13414
 * change with time. To be unambiguous the coordinates must always be qualified
13415
 * with the epoch at which they are valid. The coordinate epoch is not
13416
 * necessarily the epoch at which the observation was collected.
13417
 *
13418
 * Pedantically the coordinate epoch of an observation belongs to the
13419
 * observation, and not to the CRS, however it is often more practical to
13420
 * bind it to the CRS. The coordinate epoch should be specified for dynamic
13421
 * CRS (see IsDynamic())
13422
 *
13423
 * This method is the same as the OSRSetCoordinateEpoch() function.
13424
 *
13425
 * @param dfCoordinateEpoch Coordinate epoch as decimal year (e.g. 2021.3)
13426
 * @since OGR 3.4
13427
 */
13428
13429
void OGRSpatialReference::SetCoordinateEpoch(double dfCoordinateEpoch)
13430
0
{
13431
0
    d->m_coordinateEpoch = dfCoordinateEpoch;
13432
0
}
13433
13434
/************************************************************************/
13435
/*                      OSRSetCoordinateEpoch()                         */
13436
/************************************************************************/
13437
13438
/** \brief Set the coordinate epoch, as decimal year.
13439
 *
13440
 * See OGRSpatialReference::SetCoordinateEpoch()
13441
 *
13442
 * @since OGR 3.4
13443
 */
13444
void OSRSetCoordinateEpoch(OGRSpatialReferenceH hSRS, double dfCoordinateEpoch)
13445
0
{
13446
0
    VALIDATE_POINTER0(hSRS, "OSRSetCoordinateEpoch");
13447
13448
0
    return OGRSpatialReference::FromHandle(hSRS)->SetCoordinateEpoch(
13449
0
        dfCoordinateEpoch);
13450
0
}
13451
13452
/************************************************************************/
13453
/*                          GetCoordinateEpoch()                        */
13454
/************************************************************************/
13455
13456
/** Return the coordinate epoch, as decimal year.
13457
 *
13458
 * In a dynamic CRS, coordinates of a point on the surface of the Earth may
13459
 * change with time. To be unambiguous the coordinates must always be qualified
13460
 * with the epoch at which they are valid. The coordinate epoch is not
13461
 * necessarily the epoch at which the observation was collected.
13462
 *
13463
 * Pedantically the coordinate epoch of an observation belongs to the
13464
 * observation, and not to the CRS, however it is often more practical to
13465
 * bind it to the CRS. The coordinate epoch should be specified for dynamic
13466
 * CRS (see IsDynamic())
13467
 *
13468
 * This method is the same as the OSRGetCoordinateEpoch() function.
13469
 *
13470
 * @return coordinateEpoch Coordinate epoch as decimal year (e.g. 2021.3), or 0
13471
 *                         if not set, or relevant.
13472
 * @since OGR 3.4
13473
 */
13474
13475
double OGRSpatialReference::GetCoordinateEpoch() const
13476
0
{
13477
0
    return d->m_coordinateEpoch;
13478
0
}
13479
13480
/************************************************************************/
13481
/*                      OSRGetCoordinateEpoch()                        */
13482
/************************************************************************/
13483
13484
/** \brief Get the coordinate epoch, as decimal year.
13485
 *
13486
 * See OGRSpatialReference::GetCoordinateEpoch()
13487
 *
13488
 * @since OGR 3.4
13489
 */
13490
double OSRGetCoordinateEpoch(OGRSpatialReferenceH hSRS)
13491
0
{
13492
0
    VALIDATE_POINTER1(hSRS, "OSRGetCoordinateEpoch", 0);
13493
13494
0
    return OGRSpatialReference::FromHandle(hSRS)->GetCoordinateEpoch();
13495
0
}