aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/apply_gridshift.cpp51
-rw-r--r--test/cli/ntv2_out.dist3
-rwxr-xr-xtest/cli/testntv28
3 files changed, 45 insertions, 17 deletions
diff --git a/src/apply_gridshift.cpp b/src/apply_gridshift.cpp
index c786a50a..b994474b 100644
--- a/src/apply_gridshift.cpp
+++ b/src/apply_gridshift.cpp
@@ -36,6 +36,8 @@
#include <stdio.h>
#include <string.h>
+#include <limits>
+
#include "proj.h"
#include "proj_internal.h"
#include "proj/internal/internal.hpp"
@@ -191,7 +193,8 @@ static PJ_LP nad_intr(PJ_LP t, const HorizontalShiftGrid* grid) {
#define TOL 1e-12
static
-PJ_LP nad_cvt(PJ_LP in, int inverse, const HorizontalShiftGrid* grid) {
+PJ_LP nad_cvt(projCtx ctx, PJ_LP in, int inverse, const HorizontalShiftGrid* grid,
+ const ListOfHGrids& grids) {
PJ_LP t, tb,del, dif;
int i = MAX_ITERATIONS;
const double toltol = TOL*TOL;
@@ -201,9 +204,9 @@ PJ_LP nad_cvt(PJ_LP in, int inverse, const HorizontalShiftGrid* grid) {
/* normalize input to ll origin */
tb = in;
- const auto& extent = grid->extentAndRes();
- tb.lam -= extent.westLon;
- tb.phi -= extent.southLat;
+ const auto* extent = &(grid->extentAndRes());
+ tb.lam -= extent->westLon;
+ tb.phi -= extent->southLat;
tb.lam = adjlon (tb.lam - M_PI) + M_PI;
@@ -223,17 +226,31 @@ PJ_LP nad_cvt(PJ_LP in, int inverse, const HorizontalShiftGrid* grid) {
do {
del = nad_intr(t, grid);
- /* This case used to return failure, but I have
- changed it to return the first order approximation
- of the inverse shift. This avoids cases where the
- grid shift *into* this grid came from another grid.
- While we aren't returning optimally correct results
- I feel a close result in this case is better than
- no result. NFW
- To demonstrate use -112.5839956 49.4914451 against
- the NTv2 grid shift file from Canada. */
+ /* We can possibly go outside of the initial guessed grid, so try */
+ /* to fetch a new grid into which iterate... */
if (del.lam == HUGE_VAL)
- break;
+ {
+ PJ_LP lp;
+ lp.lam = t.lam + extent->westLon;
+ lp.phi = t.phi + extent->southLat;
+ auto newGrid = findGrid(grids, lp);
+ if( newGrid == nullptr || newGrid == grid || newGrid->isNullGrid() )
+ break;
+ pj_log(ctx, PJ_LOG_DEBUG_MINOR, "Switching from grid %s to grid %s",
+ grid->name().c_str(),
+ newGrid->name().c_str());
+ grid = newGrid;
+ extent = &(grid->extentAndRes());
+ t.lam = lp.lam - extent->westLon;
+ t.phi = lp.phi - extent->southLat;
+ tb = in;
+ tb.lam -= extent->westLon;
+ tb.phi -= extent->southLat;
+ tb.lam = adjlon (tb.lam - M_PI) + M_PI;
+ dif.lam = std::numeric_limits<double>::max();
+ dif.phi = std::numeric_limits<double>::max();
+ continue;
+ }
dif.lam = t.lam + del.lam - tb.lam;
dif.phi = t.phi + del.phi - tb.phi;
@@ -254,8 +271,8 @@ PJ_LP nad_cvt(PJ_LP in, int inverse, const HorizontalShiftGrid* grid) {
if (del.lam==HUGE_VAL && getenv ("PROJ_DEBUG"))
fprintf (stderr, "Inverse grid shift iteration failed, presumably at grid edge.\nUsing first approximation.\n");
- in.lam = adjlon (t.lam + extent.westLon);
- in.phi = t.phi + extent.southLat;
+ in.lam = adjlon (t.lam + extent->westLon);
+ in.phi = t.phi + extent->southLat;
return in;
}
@@ -311,7 +328,7 @@ PJ_LP proj_hgrid_apply_internal(PJ_CONTEXT* ctx,
}
int inverse = direction == PJ_FWD ? 0 : 1;
- out = nad_cvt(lp, inverse, grid);
+ out = nad_cvt(ctx, lp, inverse, grid, grids);
if (out.lam == HUGE_VAL || out.phi == HUGE_VAL)
pj_ctx_set_errno(ctx, PJD_ERR_GRID_AREA);
diff --git a/test/cli/ntv2_out.dist b/test/cli/ntv2_out.dist
index 9a97f9cf..650a69d8 100644
--- a/test/cli/ntv2_out.dist
+++ b/test/cli/ntv2_out.dist
@@ -9,3 +9,6 @@ Try with NTv2 and NTv1 together ... falls back to NTv1
99d00'00.000"W 65d00'00.000"N 0.0 99d0'1.5885"W 65d0'1.3482"N 0.000
111d00'00.000"W 46d00'00.000"N 0.0 111d0'3.1897"W 45d59'59.7489"N 0.000
111d00'00.000"W 47d30'00.000"N 0.0 111d0'2.7989"W 47d29'59.9896"N 0.000
+##############################################################
+Switching between NTv2 subgrids
+-112.5839956 49.4914451 0 -112.58307487 49.49145197 0.00000000
diff --git a/test/cli/testntv2 b/test/cli/testntv2
index 73371dbe..2a31304e 100755
--- a/test/cli/testntv2
+++ b/test/cli/testntv2
@@ -52,6 +52,14 @@ $EXE +proj=latlong +ellps=clrk66 +nadgrids=ntv2_0.gsb,ntv1_can.dat,conus \
111d00'00.000"W 46d00'00.000"N 0.0
111d00'00.000"W 47d30'00.000"N 0.0
EOF
+
+echo "##############################################################" >> ${OUT}
+echo Switching between NTv2 subgrids >> ${OUT}
+# Initial guess is in ALraymnd, going to parent CAwest afterwards
+$EXE +proj=latlong +datum=NAD83 +to +proj=latlong +ellps=clrk66 +nadgrids=ntv2_0.gsb -E -d 8 >>${OUT} <<EOF
+-112.5839956 49.4914451 0
+EOF
+
#
##############################################################################
# Done!