| 53 | // --------------------------------------------------------------------------- |
| 54 | |
| 55 | static bool get_grid_values(PJ *P, xyzgridshiftData *Q, const PJ_LP &lp, |
| 56 | double &dx, double &dy, double &dz) { |
| 57 | if (Q->defer_grid_opening) { |
| 58 | Q->defer_grid_opening = false; |
| 59 | Q->grids = pj_generic_grid_init(P, "grids"); |
| 60 | Q->error_code_in_defer_grid_opening = proj_errno(P); |
| 61 | } |
| 62 | if (Q->error_code_in_defer_grid_opening) { |
| 63 | proj_errno_set(P, Q->error_code_in_defer_grid_opening); |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | GenericShiftGridSet *gridset = nullptr; |
| 68 | auto grid = pj_find_generic_grid(Q->grids, lp, gridset); |
| 69 | if (!grid) { |
| 70 | return false; |
| 71 | } |
| 72 | if (grid->isNullGrid()) { |
| 73 | dx = 0; |
| 74 | dy = 0; |
| 75 | dz = 0; |
| 76 | return true; |
| 77 | } |
| 78 | const auto samplesPerPixel = grid->samplesPerPixel(); |
| 79 | if (samplesPerPixel < 3) { |
| 80 | proj_log_error(P, "xyzgridshift: grid has not enough samples"); |
| 81 | return false; |
| 82 | } |
| 83 | int sampleX = 0; |
| 84 | int sampleY = 1; |
| 85 | int sampleZ = 2; |
| 86 | for (int i = 0; i < samplesPerPixel; i++) { |
| 87 | const auto desc = grid->description(i); |
| 88 | if (desc == "x_translation") { |
| 89 | sampleX = i; |
| 90 | } else if (desc == "y_translation") { |
| 91 | sampleY = i; |
| 92 | } else if (desc == "z_translation") { |
| 93 | sampleZ = i; |
| 94 | } |
| 95 | } |
| 96 | const auto unit = grid->unit(sampleX); |
| 97 | if (!unit.empty() && unit != "metre") { |
| 98 | proj_log_error(P, "xyzgridshift: Only unit=metre currently handled"); |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | bool must_retry = false; |
| 103 | if (!pj_bilinear_interpolation_three_samples(P->ctx, grid, lp, sampleX, |
| 104 | sampleY, sampleZ, dx, dy, dz, |
| 105 | must_retry)) { |
| 106 | if (must_retry) |
| 107 | return get_grid_values(P, Q, lp, dx, dy, dz); |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | dx *= Q->multiplier; |
| 112 | dy *= Q->multiplier; |
no test coverage detected