diff options
| author | Alan D. Snow <alansnow21@gmail.com> | 2021-04-18 05:34:29 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-18 12:34:29 +0200 |
| commit | e4e2991b174ea48b67e9b41c8f356a9cf1bba081 (patch) | |
| tree | d523f7534caf2662f1b97d5e8e40b5d16886c998 /src/iso19111/c_api.cpp | |
| parent | 3d1c573dc9d9f56ced597554ad9fa1643f0a0ae9 (diff) | |
| download | PROJ-e4e2991b174ea48b67e9b41c8f356a9cf1bba081.tar.gz PROJ-e4e2991b174ea48b67e9b41c8f356a9cf1bba081.zip | |
Added proj_get_celestial_body_list_from_database (#2674)
Closes #2667
Diffstat (limited to 'src/iso19111/c_api.cpp')
| -rw-r--r-- | src/iso19111/c_api.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/iso19111/c_api.cpp b/src/iso19111/c_api.cpp index c9eaf46a..cb44151a 100644 --- a/src/iso19111/c_api.cpp +++ b/src/iso19111/c_api.cpp @@ -2674,6 +2674,76 @@ PROJ_STRING_LIST proj_get_codes_from_database(PJ_CONTEXT *ctx, // --------------------------------------------------------------------------- +/** \brief Enumerate celestial bodies from the database. + * + * The returned object is an array of PROJ_CELESTIAL_BODY_INFO* pointers, whose last + * entry is NULL. This array should be freed with proj_celestial_body_list_destroy() + * + * @param ctx PROJ context, or NULL for default context + * @param auth_name Authority name, used to restrict the search. + * Or NULL for all authorities. + * @param out_result_count Output parameter pointing to an integer to receive + * the size of the result list. Might be NULL + * @return an array of PROJ_CELESTIAL_BODY_INFO* pointers to be freed with + * proj_celestial_body_list_destroy(), or NULL in case of error. + * @since 8.1 + */ +PROJ_CELESTIAL_BODY_INFO **proj_get_celestial_body_list_from_database(PJ_CONTEXT *ctx, + const char *auth_name, + int *out_result_count) { + SANITIZE_CTX(ctx); + PROJ_CELESTIAL_BODY_INFO **ret = nullptr; + int i = 0; + try { + auto factory = AuthorityFactory::create(getDBcontext(ctx), + auth_name ? auth_name : ""); + auto list = factory->getCelestialBodyList(); + ret = new PROJ_CELESTIAL_BODY_INFO *[list.size() + 1]; + for (const auto &info : list) { + ret[i] = new PROJ_CELESTIAL_BODY_INFO; + ret[i]->auth_name = pj_strdup(info.authName.c_str()); + ret[i]->name = pj_strdup(info.name.c_str()); + i++; + } + ret[i] = nullptr; + if (out_result_count) + *out_result_count = i; + ctx->safeAutoCloseDbIfNeeded(); + return ret; + } catch (const std::exception &e) { + proj_log_error(ctx, __FUNCTION__, e.what()); + if (ret) { + ret[i + 1] = nullptr; + proj_celestial_body_list_destroy(ret); + } + if (out_result_count) + *out_result_count = 0; + } + ctx->safeAutoCloseDbIfNeeded(); + return nullptr; +} + +// --------------------------------------------------------------------------- + +/** \brief Destroy the result returned by + * proj_get_celestial_body_list_from_database(). + * + * @since 8.1 + */ +void proj_celestial_body_list_destroy(PROJ_CELESTIAL_BODY_INFO **list) { + if (list) { + for (int i = 0; list[i] != nullptr; i++) { + free(list[i]->auth_name); + free(list[i]->name); + delete list[i]; + } + delete[] list; + } +} + + +// --------------------------------------------------------------------------- + /** Free a list of NULL terminated strings. */ void proj_string_list_destroy(PROJ_STRING_LIST list) { if (list) { |
