Browse files

cartography: Stop using Proj4's global pj_errno

It is not being reset correctly anymore. Instead we should use the
local pj context to determine if we have an error.

Cool note: Proj4 has made some progress towards becoming thread
safe. We should probably stop doing or ProjContext in a shared pointer
since Proj4 now lets use allocate it's global variables in a special
struct.
  • Loading branch information...
1 parent 67f777e commit 109847c762045786c97d647692cdfe7180ce298f Zack Moratto committed Jul 20, 2012
Showing with 34 additions and 7 deletions.
  1. +17 −1 src/vw/Cartography/GeoReference.cc
  2. +0 −4 src/vw/Cartography/GeoReference.h
  3. +17 −2 src/vw/Cartography/GeoTransform.cc
View
18 src/vw/Cartography/GeoReference.cc
@@ -35,6 +35,19 @@
// Proj.4
#include <proj_api.h>
+// Macro for checking Proj.4 output, something we do a lot of.
+#if PJ_VERSION < 480
+
+#define CHECK_PROJ_ERROR if(pj_errno) vw_throw(ProjectionErr() << "Proj.4 error: " << pj_strerrno(pj_errno))
+#define CHECK_PROJ_INIT_ERROR(str) if(pj_errno) vw_throw(InputErr() << "Proj.4 failed to initialize on string: " << str << "\n\tError was: " << pj_strerrno(pj_errno))
+
+#else
+
+#define CHECK_PROJ_ERROR if(pj_ctx_get_errno(pj_get_default_ctx())) vw_throw(ProjectionErr() << "Proj.4 error: " << pj_strerrno(pj_ctx_get_errno(pj_get_default_ctx())))
+#define CHECK_PROJ_INIT_ERROR(str) if(pj_ctx_get_errno(pj_get_default_ctx())) vw_throw(InputErr() << "Proj.4 failed to initialize on string: " << str << "\n\tError was: " << pj_strerrno(pj_ctx_get_errno(pj_get_default_ctx())))
+
+#endif
+
namespace vw {
namespace cartography {
@@ -438,7 +451,7 @@ namespace cartography {
Vector2 GeoReference::lonlat_to_point(Vector2 lon_lat) const {
if ( ! m_is_projected ) return lon_lat;
// This value is proj's internal limit
- static const double BOUND = M_PI/2.0-(1e-10)-std::numeric_limits<double>::epsilon();
+ static const double BOUND = 1.5707963267948966 - (1e-10) - std::numeric_limits<double>::epsilon();
projXY projected;
projLP unprojected;
@@ -539,3 +552,6 @@ namespace cartography {
}
}} // vw::cartography
+
+#undef CHECK_PROJ_ERROR
+#undef CHECK_PROJ_INIT_ERROR
View
4 src/vw/Cartography/GeoReference.h
@@ -46,10 +46,6 @@ typedef PJ* projPJ;
namespace vw {
namespace cartography {
- // Macro for checking Proj.4 output, something we do a lot of.
-#define CHECK_PROJ_ERROR if(pj_errno) vw_throw(ProjectionErr() << "Proj.4 error: " << pj_strerrno(pj_errno))
-#define CHECK_PROJ_INIT_ERROR(str) if(pj_errno) vw_throw(InputErr() << "Proj.4 failed to initialize on string: " << str << "\n\tError was: " << pj_strerrno(pj_errno))
-
// Here is some machinery to keep track of an initialized proj.4
// projection context using a smart pointer. Using a smart pointer
// here simplies the rest of the GeoReference class considerably, and
View
19 src/vw/Cartography/GeoTransform.cc
@@ -25,6 +25,19 @@
// Proj.4
#include <proj_api.h>
+// Macro for checking Proj.4 output, something we do a lot of.
+#if PJ_VERSION < 480
+
+#define CHECK_PROJ_ERROR if(pj_errno) vw_throw(ProjectionErr() << "Proj.4 error: " << pj_strerrno(pj_errno))
+#define CHECK_PROJ_INIT_ERROR(str) if(pj_errno) vw_throw(InputErr() << "Proj.4 failed to initialize on string: " << str << "\n\tError was: " << pj_strerrno(pj_errno))
+
+#else
+
+#define CHECK_PROJ_ERROR if(pj_ctx_get_errno(pj_get_default_ctx())) vw_throw(ProjectionErr() << "Proj.4 error: " << pj_strerrno(pj_ctx_get_errno(pj_get_default_ctx())))
+#define CHECK_PROJ_INIT_ERROR(str) if(pj_ctx_get_errno(pj_get_default_ctx())) vw_throw(InputErr() << "Proj.4 failed to initialize on string: " << str << "\n\tError was: " << pj_strerrno(pj_ctx_get_errno(pj_get_default_ctx())))
+
+#endif
+
namespace vw {
namespace cartography {
@@ -59,13 +72,13 @@ namespace cartography {
// source or destination georef uses.
ss_src << "+proj=latlong " << src_datum;
m_src_datum = boost::shared_ptr<ProjContext>(new ProjContext(ss_src.str()));
- CHECK_PROJ_INIT_ERROR(ss_src.str().c_str());
+ CHECK_PROJ_INIT_ERROR( ss_src.str() );
// The destination proj4 context.
std::stringstream ss_dst;
ss_dst << "+proj=latlong " << dst_datum;
m_dst_datum = boost::shared_ptr<ProjContext>(new ProjContext(ss_dst.str()));
- CHECK_PROJ_INIT_ERROR(ss_dst.str().c_str());
+ CHECK_PROJ_INIT_ERROR( ss_dst.str() );
}
// Because GeoTransform is typically very slow, we default to a tolerance
// of 0.1 pixels to allow ourselves to be approximated.
@@ -151,3 +164,5 @@ namespace cartography {
}
}} // namespace vw::cartography
+#undef CHECK_PROJ_ERROR
+#undef CHECK_PROJ_INIT_ERROR

0 comments on commit 109847c

Please sign in to comment.