PISM, A Parallel Ice Sheet Model 2.3.0-79cae578d committed by Constantine Khrulev on 2026-03-22
Loading...
Searching...
No Matches
Proj.hh
Go to the documentation of this file.
1/* Copyright (C) 2016, 2017, 2019, 2020, 2024, 2025 PISM Authors
2 *
3 * This file is part of PISM.
4 *
5 * PISM is free software; you can redistribute it and/or modify it under the
6 * terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * PISM is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with PISM; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifndef PROJ_WRAPPER_H
21#define PROJ_WRAPPER_H
22
23#include <string>
24
25#include <proj.h>
26
27#include "pism/util/error_handling.hh"
28
29namespace pism {
30
31//! A wrapper for PJ that makes sure `pj_destroy` is called.
32class Proj {
33public:
34 Proj(const std::string &input, const std::string &output) {
35 // add +type=crs if it is not there yet
36 std::string input_with_crs(input);
37 if ((input.find("+proj") != std::string::npos or input.find("+init") != std::string::npos) and
38 input.find("+type=crs") == std::string::npos) {
39 input_with_crs += " +type=crs";
40 }
41
42 m_pj = nullptr;
43 {
44 PJ *pj = proj_create_crs_to_crs(PJ_DEFAULT_CTX, input_with_crs.c_str(), output.c_str(), 0);
45
46 if (pj != nullptr) {
47 // ensure that this transformation returns coordinates in the predictable order
48 // (x,y or lon,lat)
49 m_pj = proj_normalize_for_visualization(PJ_DEFAULT_CTX, pj);
50
51 proj_destroy(pj);
52 }
53 }
54
55 if (m_pj == nullptr) {
58 "Failed to initialize projection transformation '%s' to '%s' (errno: %d, %s).",
59 input_with_crs.c_str(), output.c_str(), proj_errno(0), proj_errno_string(proj_errno(0)));
60 }
61 }
62
64 if (m_pj != nullptr) {
65 proj_destroy(m_pj);
66 m_pj = nullptr;
67 }
68 }
69 operator PJ*() {
70 return m_pj;
71 }
72private:
73 PJ *m_pj;
74};
75
76} // end of namespace pism
77
78
79#endif /* PROJ_WRAPPER_H */
~Proj()
Definition Proj.hh:63
PJ * m_pj
Definition Proj.hh:73
Proj(const std::string &input, const std::string &output)
Definition Proj.hh:34
A wrapper for PJ that makes sure pj_destroy is called.
Definition Proj.hh:32
static RuntimeError formatted(const ErrorLocation &location, const char format[],...) __attribute__((format(printf
build a RuntimeError with a formatted message
#define PISM_ERROR_LOCATION