-
Notifications
You must be signed in to change notification settings - Fork 794
/
c_api.cpp
7059 lines (6596 loc) · 270 KB
/
c_api.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
*
* Project: PROJ
* Purpose: C API wraper of C++ API
* Author: Even Rouault <even dot rouault at spatialys dot com>
*
******************************************************************************
* Copyright (c) 2018, Even Rouault <even dot rouault at spatialys dot com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#ifndef FROM_PROJ_CPP
#define FROM_PROJ_CPP
#endif
#include <algorithm>
#include <cassert>
#include <cstdarg>
#include <cstring>
#include <map>
#include <memory>
#include <new>
#include <utility>
#include <vector>
#include "proj/common.hpp"
#include "proj/coordinateoperation.hpp"
#include "proj/coordinatesystem.hpp"
#include "proj/crs.hpp"
#include "proj/datum.hpp"
#include "proj/io.hpp"
#include "proj/metadata.hpp"
#include "proj/util.hpp"
#include "proj/internal/internal.hpp"
#include "proj/internal/io_internal.hpp"
// PROJ include order is sensitive
// clang-format off
#include "proj.h"
#include "proj_internal.h"
#include "proj_experimental.h"
// clang-format on
#include "proj_constants.h"
using namespace NS_PROJ::common;
using namespace NS_PROJ::crs;
using namespace NS_PROJ::cs;
using namespace NS_PROJ::datum;
using namespace NS_PROJ::io;
using namespace NS_PROJ::internal;
using namespace NS_PROJ::metadata;
using namespace NS_PROJ::operation;
using namespace NS_PROJ::util;
using namespace NS_PROJ;
// ---------------------------------------------------------------------------
static void PROJ_NO_INLINE proj_log_error(PJ_CONTEXT *ctx, const char *function,
const char *text) {
std::string msg(function);
msg += ": ";
msg += text;
ctx->logger(ctx->logger_app_data, PJ_LOG_ERROR, msg.c_str());
auto previous_errno = pj_ctx_get_errno(ctx);
if (previous_errno == 0) {
// only set errno if it wasn't set deeper down the call stack
pj_ctx_set_errno(ctx, PJD_ERR_GENERIC_ERROR);
}
}
// ---------------------------------------------------------------------------
static void PROJ_NO_INLINE proj_log_debug(PJ_CONTEXT *ctx, const char *function,
const char *text) {
std::string msg(function);
msg += ": ";
msg += text;
ctx->logger(ctx->logger_app_data, PJ_LOG_DEBUG, msg.c_str());
}
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
// ---------------------------------------------------------------------------
void proj_context_delete_cpp_context(struct projCppContext *cppContext) {
delete cppContext;
}
//! @endcond
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
static PROJ_NO_INLINE const DatabaseContextNNPtr &
getDBcontext(PJ_CONTEXT *ctx) {
if (ctx->cpp_context == nullptr) {
ctx->cpp_context = new projCppContext(ctx);
}
return ctx->cpp_context->databaseContext;
}
// ---------------------------------------------------------------------------
static PROJ_NO_INLINE DatabaseContextPtr
getDBcontextNoException(PJ_CONTEXT *ctx, const char *function) {
try {
return getDBcontext(ctx).as_nullable();
} catch (const std::exception &e) {
proj_log_debug(ctx, function, e.what());
return nullptr;
}
}
// ---------------------------------------------------------------------------
static PJ *pj_obj_create(PJ_CONTEXT *ctx, const IdentifiedObjectNNPtr &objIn) {
auto coordop = dynamic_cast<const CoordinateOperation *>(objIn.get());
if (coordop) {
auto dbContext = getDBcontextNoException(ctx, __FUNCTION__);
try {
auto formatter = PROJStringFormatter::create(
PROJStringFormatter::Convention::PROJ_5, dbContext);
auto projString = coordop->exportToPROJString(formatter.get());
auto pj = pj_create_internal(ctx, projString.c_str());
if (pj) {
pj->iso_obj = objIn;
return pj;
}
} catch (const std::exception &) {
// Silence, since we may not always be able to export as a
// PROJ string.
}
}
auto pj = pj_new();
if (pj) {
pj->ctx = ctx;
pj->descr = "ISO-19111 object";
pj->iso_obj = objIn;
}
return pj;
}
//! @endcond
// ---------------------------------------------------------------------------
/** \brief Opaque object representing a set of operation results. */
struct PJ_OBJ_LIST {
//! @cond Doxygen_Suppress
std::vector<IdentifiedObjectNNPtr> objects;
explicit PJ_OBJ_LIST(std::vector<IdentifiedObjectNNPtr> &&objectsIn)
: objects(std::move(objectsIn)) {}
PJ_OBJ_LIST(const PJ_OBJ_LIST &) = delete;
PJ_OBJ_LIST &operator=(const PJ_OBJ_LIST &) = delete;
//! @endcond
};
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
#define SANITIZE_CTX(ctx) \
do { \
if (ctx == nullptr) { \
ctx = pj_get_default_ctx(); \
} \
} while (0)
//! @endcond
// ---------------------------------------------------------------------------
/** \brief Explicitly point to the main PROJ CRS and coordinate operation
* definition database ("proj.db"), and potentially auxiliary databases with
* same structure.
*
* @param ctx PROJ context, or NULL for default context
* @param dbPath Path to main database, or NULL for default.
* @param auxDbPaths NULL-terminated list of auxiliary database filenames, or
* NULL.
* @param options should be set to NULL for now
* @return TRUE in case of success
*/
int proj_context_set_database_path(PJ_CONTEXT *ctx, const char *dbPath,
const char *const *auxDbPaths,
const char *const *options) {
SANITIZE_CTX(ctx);
(void)options;
delete ctx->cpp_context;
ctx->cpp_context = nullptr;
try {
ctx->cpp_context = new projCppContext(ctx, dbPath, auxDbPaths);
return true;
} catch (const std::exception &e) {
proj_log_error(ctx, __FUNCTION__, e.what());
return false;
}
}
// ---------------------------------------------------------------------------
/** \brief Returns the path to the database.
*
* The returned pointer remains valid while ctx is valid, and until
* proj_context_set_database_path() is called.
*
* @param ctx PROJ context, or NULL for default context
* @return path, or nullptr
*/
const char *proj_context_get_database_path(PJ_CONTEXT *ctx) {
SANITIZE_CTX(ctx);
try {
return getDBcontext(ctx)->getPath().c_str();
} catch (const std::exception &e) {
proj_log_error(ctx, __FUNCTION__, e.what());
return nullptr;
}
}
// ---------------------------------------------------------------------------
/** \brief Return a metadata from the database.
*
* The returned pointer remains valid while ctx is valid, and until
* proj_context_get_database_metadata() is called.
*
* @param ctx PROJ context, or NULL for default context
* @param key Metadata key. Must not be NULL
* @return value, or nullptr
*/
const char *proj_context_get_database_metadata(PJ_CONTEXT *ctx,
const char *key) {
SANITIZE_CTX(ctx);
try {
return getDBcontext(ctx)->getMetadata(key);
} catch (const std::exception &e) {
proj_log_error(ctx, __FUNCTION__, e.what());
return nullptr;
}
}
// ---------------------------------------------------------------------------
/** \brief Guess the "dialect" of the WKT string.
*
* @param ctx PROJ context, or NULL for default context
* @param wkt String (must not be NULL)
*/
PJ_GUESSED_WKT_DIALECT proj_context_guess_wkt_dialect(PJ_CONTEXT *ctx,
const char *wkt) {
(void)ctx;
assert(wkt);
switch (WKTParser().guessDialect(wkt)) {
case WKTParser::WKTGuessedDialect::WKT2_2018:
return PJ_GUESSED_WKT2_2018;
case WKTParser::WKTGuessedDialect::WKT2_2015:
return PJ_GUESSED_WKT2_2015;
case WKTParser::WKTGuessedDialect::WKT1_GDAL:
return PJ_GUESSED_WKT1_GDAL;
case WKTParser::WKTGuessedDialect::WKT1_ESRI:
return PJ_GUESSED_WKT1_ESRI;
case WKTParser::WKTGuessedDialect::NOT_WKT:
break;
}
return PJ_GUESSED_NOT_WKT;
}
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
static const char *getOptionValue(const char *option,
const char *keyWithEqual) noexcept {
if (ci_starts_with(option, keyWithEqual)) {
return option + strlen(keyWithEqual);
}
return nullptr;
}
//! @endcond
// ---------------------------------------------------------------------------
/** \brief "Clone" an object.
*
* Technically this just increases the reference counter on the object, since
* PJ objects are immutable.
*
* The returned object must be unreferenced with proj_destroy() after use.
* It should be used by at most one thread at a time.
*
* @param ctx PROJ context, or NULL for default context
* @param obj Object to clone. Must not be NULL.
* @return Object that must be unreferenced with proj_destroy(), or NULL in
* case of error.
*/
PJ *proj_clone(PJ_CONTEXT *ctx, const PJ *obj) {
SANITIZE_CTX(ctx);
if (!obj->iso_obj) {
return nullptr;
}
try {
return pj_obj_create(ctx, NN_NO_CHECK(obj->iso_obj));
} catch (const std::exception &e) {
proj_log_error(ctx, __FUNCTION__, e.what());
}
return nullptr;
}
// ---------------------------------------------------------------------------
/** \brief Instantiate an object from a WKT string, PROJ string or object code
* (like "EPSG:4326", "urn:ogc:def:crs:EPSG::4326",
* "urn:ogc:def:coordinateOperation:EPSG::1671").
*
* This function calls osgeo::proj::io::createFromUserInput()
*
* The returned object must be unreferenced with proj_destroy() after use.
* It should be used by at most one thread at a time.
*
* @param ctx PROJ context, or NULL for default context
* @param text String (must not be NULL)
* @return Object that must be unreferenced with proj_destroy(), or NULL in
* case of error.
*/
PJ *proj_create(PJ_CONTEXT *ctx, const char *text) {
SANITIZE_CTX(ctx);
assert(text);
// Only connect to proj.db if needed
if (strstr(text, "proj=") == nullptr || strstr(text, "init=") != nullptr) {
getDBcontextNoException(ctx, __FUNCTION__);
}
try {
auto identifiedObject = nn_dynamic_pointer_cast<IdentifiedObject>(
createFromUserInput(text, ctx));
if (identifiedObject) {
return pj_obj_create(ctx, NN_NO_CHECK(identifiedObject));
}
} catch (const std::exception &e) {
proj_log_error(ctx, __FUNCTION__, e.what());
}
return nullptr;
}
// ---------------------------------------------------------------------------
template <class T> static PROJ_STRING_LIST to_string_list(T &&set) {
auto ret = new char *[set.size() + 1];
size_t i = 0;
for (const auto &str : set) {
try {
ret[i] = new char[str.size() + 1];
} catch (const std::exception &) {
while (--i > 0) {
delete[] ret[i];
}
delete[] ret;
throw;
}
std::memcpy(ret[i], str.c_str(), str.size() + 1);
i++;
}
ret[i] = nullptr;
return ret;
}
// ---------------------------------------------------------------------------
/** \brief Instantiate an object from a WKT string.
*
* This function calls osgeo::proj::io::WKTParser::createFromWKT()
*
* The returned object must be unreferenced with proj_destroy() after use.
* It should be used by at most one thread at a time.
*
* @param ctx PROJ context, or NULL for default context
* @param wkt WKT string (must not be NULL)
* @param options null-terminated list of options, or NULL. Currently
* supported options are:
* <ul>
* <li>STRICT=YES/NO. Defaults to NO. When set to YES, strict validation will
* be enabled.</li>
* </ul>
* @param out_warnings Pointer to a PROJ_STRING_LIST object, or NULL.
* If provided, *out_warnings will contain a list of warnings, typically for
* non recognized projection method or parameters. It must be freed with
* proj_string_list_destroy().
* @param out_grammar_errors Pointer to a PROJ_STRING_LIST object, or NULL.
* If provided, *out_grammar_errors will contain a list of errors regarding the
* WKT grammaer. It must be freed with proj_string_list_destroy().
* @return Object that must be unreferenced with proj_destroy(), or NULL in
* case of error.
*/
PJ *proj_create_from_wkt(PJ_CONTEXT *ctx, const char *wkt,
const char *const *options,
PROJ_STRING_LIST *out_warnings,
PROJ_STRING_LIST *out_grammar_errors) {
SANITIZE_CTX(ctx);
assert(wkt);
if (out_warnings) {
*out_warnings = nullptr;
}
if (out_grammar_errors) {
*out_grammar_errors = nullptr;
}
try {
WKTParser parser;
auto dbContext = getDBcontextNoException(ctx, __FUNCTION__);
if (dbContext) {
parser.attachDatabaseContext(NN_NO_CHECK(dbContext));
}
for (auto iter = options; iter && iter[0]; ++iter) {
const char *value;
if ((value = getOptionValue(*iter, "STRICT="))) {
parser.setStrict(ci_equal(value, "YES"));
} else {
std::string msg("Unknown option :");
msg += *iter;
proj_log_error(ctx, __FUNCTION__, msg.c_str());
return nullptr;
}
}
auto obj = nn_dynamic_pointer_cast<IdentifiedObject>(
parser.createFromWKT(wkt));
if (out_grammar_errors) {
auto warnings = parser.warningList();
if (!warnings.empty()) {
*out_grammar_errors = to_string_list(warnings);
}
}
if (obj && out_warnings) {
auto derivedCRS = dynamic_cast<const crs::DerivedCRS *>(obj.get());
if (derivedCRS) {
auto warnings =
derivedCRS->derivingConversionRef()->validateParameters();
if (!warnings.empty()) {
*out_warnings = to_string_list(warnings);
}
} else {
auto singleOp =
dynamic_cast<const operation::SingleOperation *>(obj.get());
if (singleOp) {
auto warnings = singleOp->validateParameters();
if (!warnings.empty()) {
*out_warnings = to_string_list(warnings);
}
}
}
}
if (obj) {
return pj_obj_create(ctx, NN_NO_CHECK(obj));
}
} catch (const std::exception &e) {
if (out_grammar_errors) {
std::list<std::string> exc{e.what()};
try {
*out_grammar_errors = to_string_list(exc);
} catch (const std::exception &) {
proj_log_error(ctx, __FUNCTION__, e.what());
}
} else {
proj_log_error(ctx, __FUNCTION__, e.what());
}
}
return nullptr;
}
// ---------------------------------------------------------------------------
/** \brief Instantiate an object from a database lookup.
*
* The returned object must be unreferenced with proj_destroy() after use.
* It should be used by at most one thread at a time.
*
* @param ctx Context, or NULL for default context.
* @param auth_name Authority name (must not be NULL)
* @param code Object code (must not be NULL)
* @param category Object category
* @param usePROJAlternativeGridNames Whether PROJ alternative grid names
* should be substituted to the official grid names. Only used on
* transformations
* @param options should be set to NULL for now
* @return Object that must be unreferenced with proj_destroy(), or NULL in
* case of error.
*/
PJ *proj_create_from_database(PJ_CONTEXT *ctx, const char *auth_name,
const char *code, PJ_CATEGORY category,
int usePROJAlternativeGridNames,
const char *const *options) {
assert(auth_name);
assert(code);
(void)options;
SANITIZE_CTX(ctx);
try {
const std::string codeStr(code);
auto factory = AuthorityFactory::create(getDBcontext(ctx), auth_name);
IdentifiedObjectPtr obj;
switch (category) {
case PJ_CATEGORY_ELLIPSOID:
obj = factory->createEllipsoid(codeStr).as_nullable();
break;
case PJ_CATEGORY_PRIME_MERIDIAN:
obj = factory->createPrimeMeridian(codeStr).as_nullable();
break;
case PJ_CATEGORY_DATUM:
obj = factory->createDatum(codeStr).as_nullable();
break;
case PJ_CATEGORY_CRS:
obj =
factory->createCoordinateReferenceSystem(codeStr).as_nullable();
break;
case PJ_CATEGORY_COORDINATE_OPERATION:
obj = factory
->createCoordinateOperation(
codeStr, usePROJAlternativeGridNames != 0)
.as_nullable();
break;
}
return pj_obj_create(ctx, NN_NO_CHECK(obj));
} catch (const std::exception &e) {
proj_log_error(ctx, __FUNCTION__, e.what());
}
return nullptr;
}
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
static const char *get_unit_category(UnitOfMeasure::Type type) {
const char *ret = nullptr;
switch (type) {
case UnitOfMeasure::Type::UNKNOWN:
ret = "unknown";
break;
case UnitOfMeasure::Type::NONE:
ret = "none";
break;
case UnitOfMeasure::Type::ANGULAR:
ret = "angular";
break;
case UnitOfMeasure::Type::LINEAR:
ret = "linear";
break;
case UnitOfMeasure::Type::SCALE:
ret = "scale";
break;
case UnitOfMeasure::Type::TIME:
ret = "time";
break;
case UnitOfMeasure::Type::PARAMETRIC:
ret = "parametric";
break;
}
return ret;
}
//! @endcond
// ---------------------------------------------------------------------------
/** \brief Get information for a unit of measure from a database lookup.
*
* @param ctx Context, or NULL for default context.
* @param auth_name Authority name (must not be NULL)
* @param code Unit of measure code (must not be NULL)
* @param out_name Pointer to a string value to store the parameter name. or
* NULL. This value remains valid until the next call to
* proj_uom_get_info_from_database() or the context destruction.
* @param out_conv_factor Pointer to a value to store the conversion
* factor of the prime meridian longitude unit to radian. or NULL
* @param out_category Pointer to a string value to store the parameter name. or
* NULL. This value might be "unknown", "none", "linear", "angular", "scale",
* "time" or "parametric";
* @return TRUE in case of success
*/
int proj_uom_get_info_from_database(PJ_CONTEXT *ctx, const char *auth_name,
const char *code, const char **out_name,
double *out_conv_factor,
const char **out_category) {
assert(auth_name);
assert(code);
SANITIZE_CTX(ctx);
try {
auto factory = AuthorityFactory::create(getDBcontext(ctx), auth_name);
auto obj = factory->createUnitOfMeasure(code);
if (out_name) {
ctx->cpp_context->lastUOMName_ = obj->name();
*out_name = ctx->cpp_context->lastUOMName_.c_str();
}
if (out_conv_factor) {
*out_conv_factor = obj->conversionToSI();
}
if (out_category) {
*out_category = get_unit_category(obj->type());
}
return true;
} catch (const std::exception &e) {
proj_log_error(ctx, __FUNCTION__, e.what());
}
return false;
}
// ---------------------------------------------------------------------------
/** \brief Get information for a grid from a database lookup.
*
* @param ctx Context, or NULL for default context.
* @param grid_name Grid name (must not be NULL)
* @param out_full_name Pointer to a string value to store the grid full
* filename. or NULL
* @param out_package_name Pointer to a string value to store the package name
* where
* the grid might be found. or NULL
* @param out_url Pointer to a string value to store the grid URL or the
* package URL where the grid might be found. or NULL
* @param out_direct_download Pointer to a int (boolean) value to store whether
* *out_url can be downloaded directly. or NULL
* @param out_open_license Pointer to a int (boolean) value to store whether
* the grid is released with an open license. or NULL
* @param out_available Pointer to a int (boolean) value to store whether the
* grid is available at runtime. or NULL
* @return TRUE in case of success.
*/
int PROJ_DLL proj_grid_get_info_from_database(
PJ_CONTEXT *ctx, const char *grid_name, const char **out_full_name,
const char **out_package_name, const char **out_url,
int *out_direct_download, int *out_open_license, int *out_available) {
assert(grid_name);
SANITIZE_CTX(ctx);
try {
auto db_context = getDBcontext(ctx);
bool direct_download;
bool open_license;
bool available;
if (!db_context->lookForGridInfo(
grid_name, ctx->cpp_context->lastGridFullName_,
ctx->cpp_context->lastGridPackageName_,
ctx->cpp_context->lastGridUrl_, direct_download, open_license,
available))
return false;
if (out_full_name)
*out_full_name = ctx->cpp_context->lastGridFullName_.c_str();
if (out_package_name)
*out_package_name = ctx->cpp_context->lastGridPackageName_.c_str();
if (out_url)
*out_url = ctx->cpp_context->lastGridUrl_.c_str();
if (out_direct_download)
*out_direct_download = direct_download ? 1 : 0;
if (out_open_license)
*out_open_license = open_license ? 1 : 0;
if (out_available)
*out_available = available ? 1 : 0;
return true;
} catch (const std::exception &e) {
proj_log_error(ctx, __FUNCTION__, e.what());
}
return false;
}
// ---------------------------------------------------------------------------
/** \brief Return GeodeticCRS that use the specified datum.
*
* @param ctx Context, or NULL for default context.
* @param crs_auth_name CRS authority name, or NULL.
* @param datum_auth_name Datum authority name (must not be NULL)
* @param datum_code Datum code (must not be NULL)
* @param crs_type "geographic 2D", "geographic 3D", "geocentric" or NULL
* @return a result set that must be unreferenced with
* proj_list_destroy(), or NULL in case of error.
*/
PJ_OBJ_LIST *proj_query_geodetic_crs_from_datum(PJ_CONTEXT *ctx,
const char *crs_auth_name,
const char *datum_auth_name,
const char *datum_code,
const char *crs_type) {
assert(datum_auth_name);
assert(datum_code);
SANITIZE_CTX(ctx);
try {
auto factory = AuthorityFactory::create(
getDBcontext(ctx), crs_auth_name ? crs_auth_name : "");
auto res = factory->createGeodeticCRSFromDatum(
datum_auth_name, datum_code, crs_type ? crs_type : "");
std::vector<IdentifiedObjectNNPtr> objects;
for (const auto &obj : res) {
objects.push_back(obj);
}
return new PJ_OBJ_LIST(std::move(objects));
} catch (const std::exception &e) {
proj_log_error(ctx, __FUNCTION__, e.what());
}
return nullptr;
}
// ---------------------------------------------------------------------------
//! @cond Doxygen_Suppress
static AuthorityFactory::ObjectType
convertPJObjectTypeToObjectType(PJ_TYPE type, bool &valid) {
valid = true;
AuthorityFactory::ObjectType cppType = AuthorityFactory::ObjectType::CRS;
switch (type) {
case PJ_TYPE_ELLIPSOID:
cppType = AuthorityFactory::ObjectType::ELLIPSOID;
break;
case PJ_TYPE_PRIME_MERIDIAN:
cppType = AuthorityFactory::ObjectType::PRIME_MERIDIAN;
break;
case PJ_TYPE_GEODETIC_REFERENCE_FRAME:
case PJ_TYPE_DYNAMIC_GEODETIC_REFERENCE_FRAME:
cppType = AuthorityFactory::ObjectType::GEODETIC_REFERENCE_FRAME;
break;
case PJ_TYPE_VERTICAL_REFERENCE_FRAME:
case PJ_TYPE_DYNAMIC_VERTICAL_REFERENCE_FRAME:
cppType = AuthorityFactory::ObjectType::VERTICAL_REFERENCE_FRAME;
break;
case PJ_TYPE_DATUM_ENSEMBLE:
cppType = AuthorityFactory::ObjectType::DATUM;
break;
case PJ_TYPE_CRS:
cppType = AuthorityFactory::ObjectType::CRS;
break;
case PJ_TYPE_GEODETIC_CRS:
cppType = AuthorityFactory::ObjectType::GEODETIC_CRS;
break;
case PJ_TYPE_GEOCENTRIC_CRS:
cppType = AuthorityFactory::ObjectType::GEOCENTRIC_CRS;
break;
case PJ_TYPE_GEOGRAPHIC_CRS:
cppType = AuthorityFactory::ObjectType::GEOGRAPHIC_CRS;
break;
case PJ_TYPE_GEOGRAPHIC_2D_CRS:
cppType = AuthorityFactory::ObjectType::GEOGRAPHIC_2D_CRS;
break;
case PJ_TYPE_GEOGRAPHIC_3D_CRS:
cppType = AuthorityFactory::ObjectType::GEOGRAPHIC_3D_CRS;
break;
case PJ_TYPE_VERTICAL_CRS:
cppType = AuthorityFactory::ObjectType::VERTICAL_CRS;
break;
case PJ_TYPE_PROJECTED_CRS:
cppType = AuthorityFactory::ObjectType::PROJECTED_CRS;
break;
case PJ_TYPE_COMPOUND_CRS:
cppType = AuthorityFactory::ObjectType::COMPOUND_CRS;
break;
case PJ_TYPE_ENGINEERING_CRS:
valid = false;
break;
case PJ_TYPE_TEMPORAL_CRS:
valid = false;
break;
case PJ_TYPE_BOUND_CRS:
valid = false;
break;
case PJ_TYPE_OTHER_CRS:
cppType = AuthorityFactory::ObjectType::CRS;
break;
case PJ_TYPE_CONVERSION:
cppType = AuthorityFactory::ObjectType::CONVERSION;
break;
case PJ_TYPE_TRANSFORMATION:
cppType = AuthorityFactory::ObjectType::TRANSFORMATION;
break;
case PJ_TYPE_CONCATENATED_OPERATION:
cppType = AuthorityFactory::ObjectType::CONCATENATED_OPERATION;
break;
case PJ_TYPE_OTHER_COORDINATE_OPERATION:
cppType = AuthorityFactory::ObjectType::COORDINATE_OPERATION;
break;
case PJ_TYPE_UNKNOWN:
valid = false;
break;
}
return cppType;
}
//! @endcond
// ---------------------------------------------------------------------------
/** \brief Return a list of objects by their name.
*
* @param ctx Context, or NULL for default context.
* @param auth_name Authority name, used to restrict the search.
* Or NULL for all authorities.
* @param searchedName Searched name. Must be at least 2 character long.
* @param types List of object types into which to search. If
* NULL, all object types will be searched.
* @param typesCount Number of elements in types, or 0 if types is NULL
* @param approximateMatch Whether approximate name identification is allowed.
* @param limitResultCount Maximum number of results to return.
* Or 0 for unlimited.
* @param options should be set to NULL for now
* @return a result set that must be unreferenced with
* proj_list_destroy(), or NULL in case of error.
*/
PJ_OBJ_LIST *proj_create_from_name(PJ_CONTEXT *ctx, const char *auth_name,
const char *searchedName,
const PJ_TYPE *types, size_t typesCount,
int approximateMatch,
size_t limitResultCount,
const char *const *options) {
assert(searchedName);
assert((types != nullptr && typesCount > 0) ||
(types == nullptr && typesCount == 0));
(void)options;
SANITIZE_CTX(ctx);
try {
auto factory = AuthorityFactory::create(getDBcontext(ctx),
auth_name ? auth_name : "");
std::vector<AuthorityFactory::ObjectType> allowedTypes;
for (size_t i = 0; i < typesCount; ++i) {
bool valid = false;
auto type = convertPJObjectTypeToObjectType(types[i], valid);
if (valid) {
allowedTypes.push_back(type);
}
}
auto res = factory->createObjectsFromName(searchedName, allowedTypes,
approximateMatch != 0,
limitResultCount);
std::vector<IdentifiedObjectNNPtr> objects;
for (const auto &obj : res) {
objects.push_back(obj);
}
return new PJ_OBJ_LIST(std::move(objects));
} catch (const std::exception &e) {
proj_log_error(ctx, __FUNCTION__, e.what());
}
return nullptr;
}
// ---------------------------------------------------------------------------
/** \brief Return the type of an object.
*
* @param obj Object (must not be NULL)
* @return its type.
*/
PJ_TYPE proj_get_type(const PJ *obj) {
assert(obj);
if (!obj->iso_obj) {
return PJ_TYPE_UNKNOWN;
}
auto ptr = obj->iso_obj.get();
if (dynamic_cast<Ellipsoid *>(ptr)) {
return PJ_TYPE_ELLIPSOID;
}
if (dynamic_cast<PrimeMeridian *>(ptr)) {
return PJ_TYPE_PRIME_MERIDIAN;
}
if (dynamic_cast<DynamicGeodeticReferenceFrame *>(ptr)) {
return PJ_TYPE_DYNAMIC_GEODETIC_REFERENCE_FRAME;
}
if (dynamic_cast<GeodeticReferenceFrame *>(ptr)) {
return PJ_TYPE_GEODETIC_REFERENCE_FRAME;
}
if (dynamic_cast<DynamicVerticalReferenceFrame *>(ptr)) {
return PJ_TYPE_DYNAMIC_VERTICAL_REFERENCE_FRAME;
}
if (dynamic_cast<VerticalReferenceFrame *>(ptr)) {
return PJ_TYPE_VERTICAL_REFERENCE_FRAME;
}
if (dynamic_cast<DatumEnsemble *>(ptr)) {
return PJ_TYPE_DATUM_ENSEMBLE;
}
{
auto crs = dynamic_cast<GeographicCRS *>(ptr);
if (crs) {
if (crs->coordinateSystem()->axisList().size() == 2) {
return PJ_TYPE_GEOGRAPHIC_2D_CRS;
} else {
return PJ_TYPE_GEOGRAPHIC_3D_CRS;
}
}
}
{
auto crs = dynamic_cast<GeodeticCRS *>(ptr);
if (crs) {
if (crs->isGeocentric()) {
return PJ_TYPE_GEOCENTRIC_CRS;
} else {
return PJ_TYPE_GEODETIC_CRS;
}
}
}
if (dynamic_cast<VerticalCRS *>(ptr)) {
return PJ_TYPE_VERTICAL_CRS;
}
if (dynamic_cast<ProjectedCRS *>(ptr)) {
return PJ_TYPE_PROJECTED_CRS;
}
if (dynamic_cast<CompoundCRS *>(ptr)) {
return PJ_TYPE_COMPOUND_CRS;
}
if (dynamic_cast<TemporalCRS *>(ptr)) {
return PJ_TYPE_TEMPORAL_CRS;
}
if (dynamic_cast<EngineeringCRS *>(ptr)) {
return PJ_TYPE_ENGINEERING_CRS;
}
if (dynamic_cast<BoundCRS *>(ptr)) {
return PJ_TYPE_BOUND_CRS;
}
if (dynamic_cast<CRS *>(ptr)) {
return PJ_TYPE_OTHER_CRS;
}
if (dynamic_cast<Conversion *>(ptr)) {
return PJ_TYPE_CONVERSION;
}
if (dynamic_cast<Transformation *>(ptr)) {
return PJ_TYPE_TRANSFORMATION;
}
if (dynamic_cast<ConcatenatedOperation *>(ptr)) {
return PJ_TYPE_CONCATENATED_OPERATION;
}
if (dynamic_cast<CoordinateOperation *>(ptr)) {
return PJ_TYPE_OTHER_COORDINATE_OPERATION;
}
return PJ_TYPE_UNKNOWN;
}
// ---------------------------------------------------------------------------
/** \brief Return whether an object is deprecated.
*
* @param obj Object (must not be NULL)
* @return TRUE if it is deprecated, FALSE otherwise
*/
int proj_is_deprecated(const PJ *obj) {
assert(obj);
if (!obj->iso_obj) {
return false;
}
return obj->iso_obj->isDeprecated();
}
// ---------------------------------------------------------------------------
/** \brief Return a list of non-deprecated objects related to the passed one
*
* @param ctx Context, or NULL for default context.
* @param obj Object (of type CRS for now) for which non-deprecated objects
* must be searched. Must not be NULL