Skip to content

Commit

Permalink
Support proj versions >= 8 (#167)
Browse files Browse the repository at this point in the history
Co-authored-by: Jim Minter <[email protected]>
  • Loading branch information
pietern and jim-minter authored Feb 10, 2024
1 parent 865e5c7 commit eb4c764
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/goesproc/map_drawer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ void MapDrawer::generatePoints(
double lat, lon;
double x, y;
for (const auto& coord : coords) {
lon = coord.at(0).get<double>() * DEG_TO_RAD;
lat = coord.at(1).get<double>() * DEG_TO_RAD;
lon = proj_torad(coord.at(0).get<double>());
lat = proj_torad(coord.at(1).get<double>());
std::tie(x, y) = proj_.fwd(lon, lat);

// If out of range, ignore
Expand Down
69 changes: 67 additions & 2 deletions src/goesproc/proj.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

namespace {

std::vector<std::string> toVector(
const std::map<std::string, std::string>& args) {
std::vector<std::string> toVector(const std::map<std::string, std::string>& args) {
std::vector<std::string> vargs;
vargs.reserve(args.size());
for (const auto& arg : args) {
Expand All @@ -17,6 +16,12 @@ std::vector<std::string> toVector(
return vargs;
}

}

#if PROJ_VERSION_MAJOR == 4

namespace {

std::string pj_error(std::string prefix = "proj: ") {
std::stringstream ss;
ss << prefix << pj_strerrno(pj_errno);
Expand All @@ -25,6 +30,11 @@ std::string pj_error(std::string prefix = "proj: ") {

} // namespace

// Forward compatibility.
double proj_torad (double angle_in_degrees) {
return angle_in_degrees * DEG_TO_RAD;
}

Proj::Proj(const std::vector<std::string>& args) {
std::vector<char*> argv;
for (const auto& arg : args) {
Expand Down Expand Up @@ -58,3 +68,58 @@ std::tuple<double, double> Proj::inv(double x, double y) {
projUV out = pj_inv(in, proj_);
return std::make_tuple<double, double>(std::move(out.u), std::move(out.v));
}

#elif PROJ_VERSION_MAJOR >= 5

namespace {

std::string toString(const std::vector<std::string>& vargs) {
std::stringstream ss;
for (auto it = vargs.begin(); it != vargs.end(); it++) {
ss << "+" << *it;
if (std::next(it) != std::end(vargs)) {
ss << " ";
}
}
return ss.str();
}

std::string pj_error(std::string prefix = "proj: ") {
std::stringstream ss;
ss << prefix << proj_errno_string(proj_errno(NULL));
return ss.str();
}

} // namespace

Proj::Proj(const std::vector<std::string>& vargs) {
const auto args = toString(vargs);
proj_ = proj_create(NULL, args.c_str());
if (!proj_) {
throw std::runtime_error(pj_error("proj initialization error: "));
}
}

Proj::Proj(const std::map<std::string, std::string>& args)
: Proj(toVector(args)) {
}

Proj::~Proj() {
proj_destroy(proj_);
}

std::tuple<double, double> Proj::fwd(double lon, double lat) {
PJ_COORD in;
in.uv = { lon, lat };
PJ_COORD out = proj_trans(proj_, PJ_FWD, in);
return std::make_tuple<double, double>(std::move(out.xy.x), std::move(out.xy.y));
}

std::tuple<double, double> Proj::inv(double x, double y) {
PJ_COORD in;
in.xy = { x, y };
PJ_COORD out = proj_trans(proj_, PJ_INV, in);
return std::make_tuple<double, double>(std::move(out.uv.u), std::move(out.uv.v));
}

#endif
21 changes: 14 additions & 7 deletions src/goesproc/proj.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
#pragma once

#if PROJ_VERSION_MAJOR < 4
#error "proj version >= 4 required"
#if PROJ_VERSION_MAJOR == 4
#include <proj_api.h>
#elif PROJ_VERSION_MAJOR >= 5
#include <proj.h>
#else
// Assume proj continues to ship with a backwards compatibility layer.
// See for a migration guide https://proj.org/development/migration.html.
#define ACCEPT_USE_OF_DEPRECATED_PROJ_API_H 1
#error "proj version >= 4 required"
#endif

#if PROJ_VERSION_MAJOR == 4
// Forward compatibility.
double proj_torad (double angle_in_degrees);
#endif

#include <map>
#include <string>
#include <tuple>
#include <vector>

#include <proj_api.h>

class Proj {
public:
explicit Proj(const std::vector<std::string>& args);
Expand All @@ -28,5 +31,9 @@ class Proj {
std::tuple<double, double> inv(double x, double y);

protected:
#if PROJ_VERSION_MAJOR == 4
projPJ proj_;
#elif PROJ_VERSION_MAJOR >= 5
PJ *proj_;
#endif
};

0 comments on commit eb4c764

Please sign in to comment.