| 1 |
/* |
| 2 |
* (C) Copyright 1996- ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
#include "ProjProjection.h" |
| 13 |
|
| 14 |
#include <proj.h> |
| 15 |
|
| 16 |
#include "eckit/types/FloatCompare.h" |
| 17 |
#include "eckit/utils/Hash.h" |
| 18 |
|
| 19 |
#include "atlas/projection/detail/ProjectionFactory.h" |
| 20 |
#include "atlas/runtime/Exception.h" |
| 21 |
#include "atlas/util/CoordinateEnums.h" |
| 22 |
#include "atlas/util/Point.h" |
| 23 |
|
| 24 |
namespace atlas { |
| 25 |
namespace projection { |
| 26 |
namespace detail { |
| 27 |
|
| 28 |
namespace { |
| 29 |
struct pj_t : std::unique_ptr<PJ, decltype(&proj_destroy)> { |
| 30 |
using t = std::unique_ptr<PJ, decltype(&proj_destroy)>; |
| 31 |
explicit pj_t(PJ* ptr): t(ptr, &proj_destroy) {} |
| 32 |
operator PJ*() const { return t::get(); } |
| 33 |
}; |
| 34 |
|
| 35 |
struct ctx_t : std::unique_ptr<PJ_CONTEXT, decltype(&proj_context_destroy)> { |
| 36 |
using t = std::unique_ptr<PJ_CONTEXT, decltype(&proj_context_destroy)>; |
| 37 |
explicit ctx_t(PJ_CONTEXT* ptr): t(ptr, &proj_context_destroy) {} |
| 38 |
operator PJ_CONTEXT*() const { return t::get(); } |
| 39 |
}; |
| 40 |
|
| 41 |
bool proj_ellipsoid_params(PJ_CONTEXT* ctxt, const std::string& proj_str, double& a, double& b) { |
| 42 |
bool success = false; |
| 43 |
pj_t identity(proj_create_crs_to_crs(ctxt, proj_str.c_str(), proj_str.c_str(), nullptr)); |
| 44 |
pj_t proj(proj_get_target_crs(ctxt, identity)); |
| 45 |
pj_t proj_ellps(proj_get_ellipsoid(ctxt, proj)); |
| 46 |
if (proj_ellps) { |
| 47 |
ATLAS_ASSERT(proj_ellipsoid_get_parameters(ctxt, proj_ellps, &a, &b, nullptr, nullptr)); |
| 48 |
ATLAS_ASSERT(0 < b && b <= a); |
| 49 |
success = true; |
| 50 |
} |
| 51 |
return success; |
| 52 |
}; |
| 53 |
|
| 54 |
std::string source_str(PJ_CONTEXT* ctxt, const std::string& proj_str) { |
| 55 |
double a, b; |
| 56 |
if (proj_ellipsoid_params(ctxt, proj_str, a, b)) { |
| 57 |
auto axes = b < a ? " +a=" + std::to_string(a) + " +b=" + std::to_string(b) : " +R=" + std::to_string(a); |
| 58 |
return "+proj=lonlat" + axes; |
| 59 |
} |
| 60 |
else { |
| 61 |
return "EPSG:4326"; // WGS84 (lat,lon) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
std::string geocentric_str(PJ_CONTEXT* ctxt, const std::string& proj_str) { |
| 67 |
double a, b; |
| 68 |
if (proj_ellipsoid_params(ctxt, proj_str, a, b)) { |
| 69 |
return b < a ? "+proj=geocent +a=" + std::to_string(a) + " +b=" + std::to_string(b) |
| 70 |
: "+proj=geocent +R=" + std::to_string(a); |
| 71 |
} |
| 72 |
else { |
| 73 |
return "EPSG:4978"; // WGS84 (x,y,z) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
} // namespace |
| 78 |
|
| 79 |
|
| 80 |
struct Proj { |
| 81 |
pj_t sourceToTarget_{nullptr}; |
| 82 |
pj_t sourceToGeocentric_{nullptr}; |
| 83 |
ctx_t context_{PJ_DEFAULT_CTX}; |
| 84 |
}; |
| 85 |
|
| 86 |
ProjProjection::ProjProjection(const eckit::Parametrisation& param): normalise_(param), proj_(new Proj()) { |
| 87 |
ATLAS_ASSERT(param.get("proj", proj_string_) && !proj_string_.empty()); |
| 88 |
source_encoded_ = param.get("proj_source", source_ = source_str(proj_->context_, proj_string_)); |
| 89 |
geocentric_encoded_ = param.get("proj_geocentric", geocentric_ = geocentric_str(proj_->context_, proj_string_)); |
| 90 |
|
| 91 |
// set x/y transformations to/from lon/lat and to/from geocentric coordinates |
| 92 |
{ |
| 93 |
pj_t p1(proj_create_crs_to_crs(proj_->context_, source_.c_str(), proj_string_.c_str(), nullptr)); |
| 94 |
ATLAS_ASSERT(p1); |
| 95 |
proj_->sourceToTarget_.reset(proj_normalize_for_visualization(proj_->context_, p1)); |
| 96 |
ATLAS_ASSERT(proj_->sourceToTarget_); |
| 97 |
} |
| 98 |
|
| 99 |
{ |
| 100 |
pj_t p2(proj_create_crs_to_crs(proj_->context_, source_.c_str(), geocentric_.c_str(), nullptr)); |
| 101 |
ATLAS_ASSERT(p2); |
| 102 |
proj_->sourceToGeocentric_.reset(proj_normalize_for_visualization(proj_->context_, p2)); |
| 103 |
ATLAS_ASSERT(proj_->sourceToGeocentric_); |
| 104 |
} |
| 105 |
|
| 106 |
// set semi-major/minor axis |
| 107 |
{ |
| 108 |
double a, b; |
| 109 |
if (proj_ellipsoid_params(proj_->context_, proj_string_, a, b)) { |
| 110 |
eckit::types::is_approximately_equal(a, b) ? extraSpec_.set("radius", a) |
| 111 |
: extraSpec_.set("semi_major_axis", a).set("semi_minor_axis", b); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
// set units |
| 116 |
{ |
| 117 |
pj_t target(proj_get_target_crs(proj_->context_, proj_->sourceToTarget_)); |
| 118 |
ATLAS_ASSERT(target); |
| 119 |
|
| 120 |
pj_t coord(proj_crs_get_coordinate_system(proj_->context_, target)); |
| 121 |
ATLAS_ASSERT(coord); |
| 122 |
ATLAS_ASSERT(proj_cs_get_axis_count(proj_->context_, coord) > 0); |
| 123 |
|
| 124 |
const char* units_c_str; |
| 125 |
if (proj_cs_get_axis_info(nullptr, coord, 0, nullptr, nullptr, nullptr, nullptr, &units_c_str, nullptr, |
| 126 |
nullptr)) { |
| 127 |
std::string units(units_c_str); |
| 128 |
if (!units.empty()) { |
| 129 |
if (units == "metre") { |
| 130 |
units = "meters"; |
| 131 |
} |
| 132 |
extraSpec_.set("units", units); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} // namespace detail |
| 137 |
|
| 138 |
|
| 139 |
void ProjProjection::xy2lonlat(double crd[]) const { |
| 140 |
PJ_COORD P = proj_coord(crd[XX], crd[YY], 0, 0); |
| 141 |
P = proj_trans(proj_->sourceToTarget_, PJ_INV, P); |
| 142 |
|
| 143 |
// std::memcpy(crd, &P, 2 * sizeof(double)); |
| 144 |
crd[LON] = P.enu.e; |
| 145 |
crd[LAT] = P.enu.n; |
| 146 |
|
| 147 |
normalise_(crd); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
void ProjProjection::lonlat2xy(double crd[]) const { |
| 152 |
PJ_COORD P = proj_coord(crd[LON], crd[LAT], 0, 0); |
| 153 |
P = proj_trans(proj_->sourceToTarget_, PJ_FWD, P); |
| 154 |
|
| 155 |
// std::memcpy(crd, &P, 2 * sizeof(double)); |
| 156 |
crd[XX] = P.xy.x; |
| 157 |
crd[YY] = P.xy.y; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
ProjectionImpl::Jacobian ProjProjection::jacobian(const PointLonLat&) const { |
| 162 |
throw_NotImplemented("ProjProjection::jacobian", Here()); |
| 163 |
} |
| 164 |
|
| 165 |
PointXYZ ProjProjection::xyz(const PointLonLat& lonlat) const { |
| 166 |
PJ_COORD P = proj_coord(lonlat.lon(), lonlat.lat(), 0, 0); |
| 167 |
P = proj_trans(proj_->sourceToGeocentric_, PJ_FWD, P); |
| 168 |
return {P.xyz.x, P.xyz.y, P.xyz.z}; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
RectangularLonLatDomain ProjProjection::lonlatBoundingBox(const Domain& domain) const { |
| 173 |
return ProjectionImpl::lonlatBoundingBox(domain); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
ProjProjection::Spec ProjProjection::spec() const { |
| 178 |
Spec spec; |
| 179 |
spec.set("type", type()); |
| 180 |
spec.set("proj", proj_string_); |
| 181 |
if (source_encoded_) { |
| 182 |
spec.set("proj_source", source_); |
| 183 |
} |
| 184 |
if (geocentric_encoded_) { |
| 185 |
spec.set("proj_geocentric", geocentric_); |
| 186 |
} |
| 187 |
normalise_.spec(spec); |
| 188 |
return spec | extraSpec_; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
std::string ProjProjection::units() const { |
| 193 |
return extraSpec_.getString("units", ""); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
void ProjProjection::hash(eckit::Hash& h) const { |
| 198 |
h.add(type()); |
| 199 |
h.add(proj_string_); |
| 200 |
if (source_encoded_) { |
| 201 |
h.add(source_); |
| 202 |
} |
| 203 |
if (geocentric_encoded_) { |
| 204 |
h.add(geocentric_); |
| 205 |
} |
| 206 |
normalise_.hash(h); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
namespace { |
| 211 |
static ProjectionBuilder<ProjProjection> register_1(ProjProjection::static_type()); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
} // namespace detail |
| 216 |
} // namespace projection |
| 217 |
} // namespace atlas |