diff options
| author | Even Rouault <even.rouault@spatialys.com> | 2019-12-06 14:07:27 +0100 |
|---|---|---|
| committer | Even Rouault <even.rouault@spatialys.com> | 2019-12-06 21:35:15 +0100 |
| commit | 916a92062ffa2f2b59007047fae2176bbb463ca3 (patch) | |
| tree | 0c9af22bd14db3cc0c56d1f5450a6f544c5d4f91 /src | |
| parent | 6875ef7116b9dab4021afeb06e2b79cd5679743b (diff) | |
| download | PROJ-916a92062ffa2f2b59007047fae2176bbb463ca3.tar.gz PROJ-916a92062ffa2f2b59007047fae2176bbb463ca3.zip | |
Remove hgrids and vgrids member from PJ structure, and store them in hgridshift/vgridshift/deformation structures
Diffstat (limited to 'src')
| -rw-r--r-- | src/4D_api.cpp | 1 | ||||
| -rw-r--r-- | src/apply_gridshift.cpp | 36 | ||||
| -rw-r--r-- | src/apply_vgridshift.cpp | 35 | ||||
| -rw-r--r-- | src/grids.hpp | 16 | ||||
| -rw-r--r-- | src/malloc.cpp | 6 | ||||
| -rw-r--r-- | src/proj_internal.h | 15 | ||||
| -rw-r--r-- | src/transform.cpp | 33 | ||||
| -rw-r--r-- | src/transformations/deformation.cpp | 51 | ||||
| -rw-r--r-- | src/transformations/hgridshift.cpp | 44 | ||||
| -rw-r--r-- | src/transformations/vgridshift.cpp | 48 |
10 files changed, 175 insertions, 110 deletions
diff --git a/src/4D_api.cpp b/src/4D_api.cpp index c994df63..3300a6bb 100644 --- a/src/4D_api.cpp +++ b/src/4D_api.cpp @@ -47,6 +47,7 @@ #include "proj_internal.h" #include <math.h> #include "geodesic.h" +#include "grids.hpp" #include "proj/common.hpp" #include "proj/coordinateoperation.hpp" diff --git a/src/apply_gridshift.cpp b/src/apply_gridshift.cpp index 5a5404da..a0ffa394 100644 --- a/src/apply_gridshift.cpp +++ b/src/apply_gridshift.cpp @@ -39,14 +39,14 @@ #include "proj.h" #include "proj_internal.h" #include "proj/internal/internal.hpp" +#include "grids.hpp" -using namespace NS_PROJ; +NS_PROJ_START // --------------------------------------------------------------------------- -static const HorizontalShiftGrid* findGrid( - const std::vector<std::unique_ptr<HorizontalShiftGridSet>>& grids, - PJ_LP input) +static const HorizontalShiftGrid* findGrid(const ListOfHGrids& grids, + PJ_LP input) { for( const auto& gridset: grids ) { @@ -59,11 +59,9 @@ static const HorizontalShiftGrid* findGrid( // --------------------------------------------------------------------------- -static std::vector<std::unique_ptr<NS_PROJ::HorizontalShiftGridSet>> - getListOfGridSets(PJ_CONTEXT* ctx, - const char* grids) +static ListOfHGrids getListOfGridSets(PJ_CONTEXT* ctx, const char* grids) { - std::vector<std::unique_ptr<NS_PROJ::HorizontalShiftGridSet>> list; + ListOfHGrids list; auto listOfGrids = internal::split(std::string(grids), ','); for( const auto& grid: listOfGrids ) { @@ -93,7 +91,7 @@ static std::vector<std::unique_ptr<NS_PROJ::HorizontalShiftGridSet>> /**********************************************/ -int proj_hgrid_init(PJ* P, const char *gridkey) { +ListOfHGrids proj_hgrid_init(PJ* P, const char *gridkey) { /********************************************** Initizalize and populate list of horizontal @@ -112,10 +110,9 @@ int proj_hgrid_init(PJ* P, const char *gridkey) { key += gridkey; const char* grids = pj_param(P->ctx, P->params, key.c_str()).s; if( grids == nullptr ) - return 0; + return {}; - P->hgrids = getListOfGridSets(P->ctx, grids); - return static_cast<int>(P->hgrids.size()); + return getListOfGridSets(P->ctx, grids); } typedef struct { pj_int32 lam, phi; } ILP; @@ -267,10 +264,10 @@ PJ_LP nad_cvt(PJ_LP in, int inverse, const HorizontalShiftGrid* grid) { /* */ /* Return coordinate offset in grid */ /********************************************/ -PJ_LP proj_hgrid_value(PJ *P, PJ_LP lp) { +PJ_LP proj_hgrid_value(PJ *P, const ListOfHGrids& grids, PJ_LP lp) { PJ_LP out = proj_coord_error().lp; - const auto grid = findGrid(P->hgrids, lp); + const auto grid = findGrid(grids, lp); if( !grid ) { pj_ctx_set_errno( P->ctx, PJD_ERR_GRID_AREA ); return out; @@ -296,7 +293,7 @@ static PJ_LP proj_hgrid_apply_internal(PJ_CONTEXT* ctx, PJ_LP lp, PJ_DIRECTION direction, - const std::vector<std::unique_ptr<HorizontalShiftGridSet>>& grids) + const ListOfHGrids& grids) { PJ_LP out; @@ -322,10 +319,13 @@ PJ_LP proj_hgrid_apply_internal(PJ_CONTEXT* ctx, return out; } -PJ_LP proj_hgrid_apply(PJ *P, PJ_LP lp, PJ_DIRECTION direction) { - return proj_hgrid_apply_internal(P->ctx, lp, direction, P->hgrids); +PJ_LP proj_hgrid_apply(PJ *P, const ListOfHGrids& grids, PJ_LP lp, PJ_DIRECTION direction) { + return proj_hgrid_apply_internal(P->ctx, lp, direction, grids); } + +NS_PROJ_END + /************************************************************************/ /* pj_apply_gridshift() */ /* */ @@ -340,7 +340,7 @@ int pj_apply_gridshift( projCtx ctx, const char *nadgrids, int inverse, double *x, double *y, double * /*z */ ) { - auto hgrids = getListOfGridSets(ctx, nadgrids); + auto hgrids = NS_PROJ::getListOfGridSets(ctx, nadgrids); if( hgrids.empty() ) { pj_ctx_set_errno( ctx, PJD_ERR_FAILED_TO_LOAD_GRID ); diff --git a/src/apply_vgridshift.cpp b/src/apply_vgridshift.cpp index 0d18b5dc..b0136e5c 100644 --- a/src/apply_vgridshift.cpp +++ b/src/apply_vgridshift.cpp @@ -37,10 +37,11 @@ #include <math.h> #include "proj_internal.h" #include "proj/internal/internal.hpp" +#include "grids.hpp" -using namespace NS_PROJ; +NS_PROJ_START -static double read_vgrid_value( PJ *defn, PJ_LP input, double vmultiplier) { +static double read_vgrid_value(const ListOfVGrids& grids, PJ_LP input, double vmultiplier) { /* do not deal with NaN coordinates */ /* cppcheck-suppress duplicateExpression */ @@ -50,7 +51,7 @@ static double read_vgrid_value( PJ *defn, PJ_LP input, double vmultiplier) { } const VerticalShiftGrid* grid = nullptr; - for( const auto& gridset: defn->vgrids ) + for( const auto& gridset: grids ) { grid = gridset->gridAt(input.lam, input.phi); if( grid ) @@ -145,7 +146,7 @@ static double read_vgrid_value( PJ *defn, PJ_LP input, double vmultiplier) { } /**********************************************/ -int proj_vgrid_init(PJ* P, const char *gridkey) { +ListOfVGrids proj_vgrid_init(PJ* P, const char *gridkey) { /********************************************** Initizalize and populate gridlist. @@ -161,14 +162,15 @@ int proj_vgrid_init(PJ* P, const char *gridkey) { std::string key("s"); key += gridkey; - const char* grids = pj_param(P->ctx, P->params, key.c_str()).s; - if( grids == nullptr ) - return 0; + const char* gridnames = pj_param(P->ctx, P->params, key.c_str()).s; + if( gridnames == nullptr ) + return {}; - auto listOfGrids = internal::split(std::string(grids), ','); - for( const auto& grid: listOfGrids ) + auto listOfGridNames = internal::split(std::string(gridnames), ','); + ListOfVGrids grids; + for( const auto& gridnameStr: listOfGridNames ) { - const char* gridname = grid.c_str(); + const char* gridname = gridnameStr.c_str(); bool canFail = false; if( gridname[0] == '@' ) { @@ -181,21 +183,20 @@ int proj_vgrid_init(PJ* P, const char *gridkey) { if( !canFail ) { pj_ctx_set_errno( P->ctx, PJD_ERR_FAILED_TO_LOAD_GRID ); - P->vgrids.clear(); - return 0; + return {}; } } else { - P->vgrids.emplace_back(std::move(gridSet)); + grids.emplace_back(std::move(gridSet)); } } - return static_cast<int>(P->vgrids.size()); + return grids; } /***********************************************/ -double proj_vgrid_value(PJ *P, PJ_LP lp, double vmultiplier){ +double proj_vgrid_value(PJ *P, const ListOfVGrids& grids, PJ_LP lp, double vmultiplier){ /*********************************************** Read grid value at position lp in grids loaded @@ -207,8 +208,10 @@ double proj_vgrid_value(PJ *P, PJ_LP lp, double vmultiplier){ double value; - value = read_vgrid_value(P, lp, vmultiplier); + value = read_vgrid_value(grids, lp, vmultiplier); proj_log_trace(P, "proj_vgrid_value: (%f, %f) = %f", lp.lam*RAD_TO_DEG, lp.phi*RAD_TO_DEG, value); return value; } + +NS_PROJ_END diff --git a/src/grids.hpp b/src/grids.hpp index 0c4af09f..975f7716 100644 --- a/src/grids.hpp +++ b/src/grids.hpp @@ -31,10 +31,9 @@ #include <memory> #include <vector> +#include "proj.h" #include "proj/util.hpp" -typedef struct projCtx_t PJ_CONTEXT; - NS_PROJ_START struct ExtentAndRes { @@ -149,6 +148,19 @@ class HorizontalShiftGridSet { const HorizontalShiftGrid *gridAt(double lon, double lat) const; }; +// --------------------------------------------------------------------------- + +typedef std::vector<std::unique_ptr<HorizontalShiftGridSet>> ListOfHGrids; +typedef std::vector<std::unique_ptr<VerticalShiftGridSet>> ListOfVGrids; + +ListOfVGrids proj_vgrid_init(PJ *P, const char *grids); +ListOfHGrids proj_hgrid_init(PJ *P, const char *grids); +double proj_vgrid_value(PJ *P, const ListOfVGrids &, PJ_LP lp, + double vmultiplier); +PJ_LP proj_hgrid_value(PJ *P, const ListOfHGrids &, PJ_LP lp); +PJ_LP proj_hgrid_apply(PJ *P, const ListOfHGrids &, PJ_LP lp, + PJ_DIRECTION direction); + NS_PROJ_END #endif // GRIDS_HPP_INCLUDED diff --git a/src/malloc.cpp b/src/malloc.cpp index b5fe76fc..3fd3699f 100644 --- a/src/malloc.cpp +++ b/src/malloc.cpp @@ -48,6 +48,9 @@ #include "proj.h" #include "proj_internal.h" +#include "grids.hpp" + +using namespace NS_PROJ; /**********************************************************************/ void *pj_malloc(size_t size) { @@ -225,6 +228,9 @@ PJ *pj_default_destructor (PJ *P, int errlev) { /* Destructor */ pj_dealloc(P->def_spherification); pj_dealloc(P->def_ellps); + delete static_cast<ListOfHGrids*>(P->hgrids_legacy); + delete static_cast<ListOfVGrids*>(P->vgrids_legacy); + /* We used to call pj_dalloc( P->catalog ), but this will leak */ /* memory. The safe way to clear catalog and grid is to call */ /* pj_gc_unloadall(pj_get_default_ctx()); and pj_deallocate_grids(); */ diff --git a/src/proj_internal.h b/src/proj_internal.h index b7a84f87..7d826414 100644 --- a/src/proj_internal.h +++ b/src/proj_internal.h @@ -65,7 +65,6 @@ #include <vector> #include "proj.h" -#include "grids.hpp" #ifdef PROJ_API_H #error proj_internal.h must be included before proj_api.h @@ -196,14 +195,6 @@ PJ_COORD pj_inv4d (PJ_COORD coo, PJ *P); PJ_COORD PROJ_DLL pj_approx_2D_trans (PJ *P, PJ_DIRECTION direction, PJ_COORD coo); PJ_COORD PROJ_DLL pj_approx_3D_trans (PJ *P, PJ_DIRECTION direction, PJ_COORD coo); - -/* Grid functionality */ -int proj_vgrid_init(PJ *P, const char *grids); -int proj_hgrid_init(PJ *P, const char *grids); -double proj_vgrid_value(PJ *P, PJ_LP lp, double vmultiplier); -PJ_LP proj_hgrid_value(PJ *P, PJ_LP lp); -PJ_LP proj_hgrid_apply(PJ *P, PJ_LP lp, PJ_DIRECTION direction); - void PROJ_DLL proj_log_error (PJ *P, const char *fmt, ...); void proj_log_debug (PJ *P, const char *fmt, ...); void proj_log_trace (PJ *P, const char *fmt, ...); @@ -479,10 +470,10 @@ struct PJconsts { int datum_type = PJD_UNKNOWN; /* PJD_UNKNOWN/3PARAM/7PARAM/GRIDSHIFT/WGS84 */ double datum_params[7] = {0,0,0,0,0,0,0}; /* Parameters for 3PARAM and 7PARAM */ - std::vector<std::unique_ptr<NS_PROJ::HorizontalShiftGridSet>> hgrids{}; - int has_geoid_vgrids = 0; /* TODO: Description needed */ - std::vector<std::unique_ptr<NS_PROJ::VerticalShiftGridSet>> vgrids{}; + int has_geoid_vgrids = 0; /* used by legacy transform.cpp */ + void* hgrids_legacy = nullptr; /* used by legacy transform.cpp. Is a pointer to a ListOfHGrids* */ + void* vgrids_legacy = nullptr; /* used by legacy transform.cpp. Is a pointer to a ListOfVGrids* */ double from_greenwich = 0.0; /* prime meridian offset (in radians) */ double long_wrap_center = 0.0; /* 0.0 for -180 to 180, actually in radians*/ diff --git a/src/transform.cpp b/src/transform.cpp index f6002b70..020d62ea 100644 --- a/src/transform.cpp +++ b/src/transform.cpp @@ -34,6 +34,9 @@ #include "proj.h" #include "proj_internal.h" #include "geocent.h" +#include "grids.hpp" + +using namespace NS_PROJ; static int adjust_axis( projCtx ctx, const char *axis, int denormalize_flag, long point_count, int point_offset, @@ -445,9 +448,17 @@ static int pj_apply_vgridshift( PJ *defn, double *x, double *y, double *z ) { - if( defn->vgrids.empty() ) + if( defn->vgrids_legacy == nullptr ) + { + defn->vgrids_legacy = new ListOfVGrids; + auto vgrids = proj_vgrid_init(defn, "geoidgrids"); + if( vgrids.empty() ) + return 0; + *static_cast<ListOfVGrids*>(defn->vgrids_legacy) = std::move(vgrids); + } + if( static_cast<ListOfVGrids*>(defn->vgrids_legacy)->empty() ) { - proj_vgrid_init(defn, "geoidgrids"); + return 0; } for( int i = 0; i < point_count; i++ ) @@ -459,7 +470,7 @@ static int pj_apply_vgridshift( PJ *defn, input.phi = y[io]; input.lam = x[io]; - value = proj_vgrid_value(defn, input, 1.0); + value = proj_vgrid_value(defn, *static_cast<ListOfVGrids*>(defn->vgrids_legacy), input, 1.0); if( inverse ) z[io] -= value; @@ -476,7 +487,7 @@ static int pj_apply_vgridshift( PJ *defn, x[io] * RAD_TO_DEG, y[io] * RAD_TO_DEG ); - for( const auto& gridset: defn->vgrids ) + for( const auto& gridset: *static_cast<ListOfVGrids*>(defn->vgrids_legacy) ) { if( gridlist.empty() ) gridlist += " tried: "; @@ -882,9 +893,17 @@ int pj_apply_gridshift_2( PJ *defn, int inverse, double *x, double *y, double * /*z*/ ) { - if( defn->hgrids.empty() ) + if( defn->hgrids_legacy == nullptr ) { - proj_hgrid_init(defn, "nadgrids"); + defn->hgrids_legacy = new ListOfHGrids; + auto hgrids = proj_hgrid_init(defn, "nadgrids"); + if( hgrids.empty() ) + return 0; + *static_cast<ListOfHGrids*>(defn->hgrids_legacy) = std::move(hgrids); + } + if( static_cast<ListOfHGrids*>(defn->hgrids_legacy)->empty() ) + { + return 0; } for( long i = 0; i < point_count; i++ ) @@ -895,7 +914,7 @@ int pj_apply_gridshift_2( PJ *defn, int inverse, input.phi = y[io]; input.lam = x[io]; - auto output = proj_hgrid_apply(defn, input, inverse ? PJ_INV : PJ_FWD); + auto output = proj_hgrid_apply(defn, *static_cast<ListOfHGrids*>(defn->hgrids_legacy), input, inverse ? PJ_INV : PJ_FWD); if ( output.lam != HUGE_VAL ) { diff --git a/src/transformations/deformation.cpp b/src/transformations/deformation.cpp index f1311a54..f5468775 100644 --- a/src/transformations/deformation.cpp +++ b/src/transformations/deformation.cpp @@ -56,17 +56,22 @@ grid-values in units of mm/year in ENU-space. #include "proj.h" #include "proj_internal.h" #include <math.h> +#include "grids.hpp" PROJ_HEAD(deformation, "Kinematic grid shift"); #define TOL 1e-8 #define MAX_ITERATIONS 10 +using namespace NS_PROJ; + namespace { // anonymous namespace -struct pj_opaque { - double dt; - double t_epoch; - PJ *cart; +struct deformationData { + double dt = 0; + double t_epoch = 0; + PJ *cart = nullptr; + ListOfHGrids hgrids{}; + ListOfVGrids vgrids{}; }; } // anonymous namespace @@ -85,13 +90,14 @@ static PJ_XYZ get_grid_shift(PJ* P, const PJ_XYZ& cartesian) { PJ_COORD geodetic, shift, temp; double sp, cp, sl, cl; int previous_errno = proj_errno_reset(P); + auto Q = static_cast<deformationData*>(P->opaque); /* cartesian to geodetic */ - geodetic.lpz = pj_inv3d(cartesian, static_cast<struct pj_opaque*>(P->opaque)->cart); + geodetic.lpz = pj_inv3d(cartesian, Q->cart); /* look up correction values in grids */ - shift.lp = proj_hgrid_value(P, geodetic.lp); - shift.enu.u = proj_vgrid_value(P, geodetic.lp, 1.0); + shift.lp = proj_hgrid_value(P, Q->hgrids, geodetic.lp); + shift.enu.u = proj_vgrid_value(P, Q->vgrids, geodetic.lp, 1.0); if (proj_errno(P) == PJD_ERR_GRID_AREA) proj_log_debug(P, "deformation: coordinate (%.3f, %.3f) outside deformation model", @@ -163,7 +169,7 @@ static PJ_XYZ reverse_shift(PJ *P, PJ_XYZ input, double dt) { } static PJ_XYZ forward_3d(PJ_LPZ lpz, PJ *P) { - struct pj_opaque *Q = (struct pj_opaque *) P->opaque; + struct deformationData *Q = (struct deformationData *) P->opaque; PJ_COORD out, in; PJ_XYZ shift; in.lpz = lpz; @@ -186,7 +192,7 @@ static PJ_XYZ forward_3d(PJ_LPZ lpz, PJ *P) { static PJ_COORD forward_4d(PJ_COORD in, PJ *P) { - struct pj_opaque *Q = (struct pj_opaque *) P->opaque; + struct deformationData *Q = (struct deformationData *) P->opaque; double dt; PJ_XYZ shift; PJ_COORD out = in; @@ -209,7 +215,7 @@ static PJ_COORD forward_4d(PJ_COORD in, PJ *P) { static PJ_LPZ reverse_3d(PJ_XYZ in, PJ *P) { - struct pj_opaque *Q = (struct pj_opaque *) P->opaque; + struct deformationData *Q = (struct deformationData *) P->opaque; PJ_COORD out; out.xyz = in; @@ -225,7 +231,7 @@ static PJ_LPZ reverse_3d(PJ_XYZ in, PJ *P) { } static PJ_COORD reverse_4d(PJ_COORD in, PJ *P) { - struct pj_opaque *Q = (struct pj_opaque *) P->opaque; + struct deformationData *Q = (struct deformationData *) P->opaque; PJ_COORD out = in; double dt; @@ -244,11 +250,14 @@ static PJ *destructor(PJ *P, int errlev) { if (nullptr==P) return nullptr; - if (nullptr==P->opaque) - return pj_default_destructor (P, errlev); - - if (static_cast<struct pj_opaque*>(P->opaque)->cart) - static_cast<struct pj_opaque*>(P->opaque)->cart->destructor (static_cast<struct pj_opaque*>(P->opaque)->cart, errlev); + auto Q = static_cast<struct deformationData*>(P->opaque); + if( Q ) + { + if (Q->cart) + Q->cart->destructor (Q->cart, errlev); + delete Q; + } + P->opaque = nullptr; return pj_default_destructor(P, errlev); } @@ -257,10 +266,9 @@ static PJ *destructor(PJ *P, int errlev) { PJ *TRANSFORMATION(deformation,1) { int has_xy_grids = 0; int has_z_grids = 0; - struct pj_opaque *Q = static_cast<struct pj_opaque*>(pj_calloc (1, sizeof (struct pj_opaque))); - if (nullptr==Q) - return destructor(P, ENOMEM); + auto Q = new deformationData; P->opaque = (void *) Q; + P->destructor = destructor; // Pass a dummy ellipsoid definition that will be overridden just afterwards Q->cart = proj_create(P->ctx, "+proj=cart +a=1"); @@ -279,13 +287,13 @@ PJ *TRANSFORMATION(deformation,1) { return destructor(P, PJD_ERR_NO_ARGS ); } - proj_hgrid_init(P, "xy_grids"); + Q->hgrids = proj_hgrid_init(P, "xy_grids"); if (proj_errno(P)) { proj_log_error(P, "deformation: could not find requested xy_grid(s)."); return destructor(P, PJD_ERR_FAILED_TO_LOAD_GRID); } - proj_vgrid_init(P, "z_grids"); + Q->vgrids = proj_vgrid_init(P, "z_grids"); if (proj_errno(P)) { proj_log_error(P, "deformation: could not find requested z_grid(s)."); return destructor(P, PJD_ERR_FAILED_TO_LOAD_GRID); @@ -325,7 +333,6 @@ PJ *TRANSFORMATION(deformation,1) { P->left = PJ_IO_UNITS_CARTESIAN; P->right = PJ_IO_UNITS_CARTESIAN; - P->destructor = destructor; return P; } diff --git a/src/transformations/hgridshift.cpp b/src/transformations/hgridshift.cpp index 465bfe98..e9983df6 100644 --- a/src/transformations/hgridshift.cpp +++ b/src/transformations/hgridshift.cpp @@ -6,24 +6,29 @@ #include <time.h> #include "proj_internal.h" +#include "grids.hpp" PROJ_HEAD(hgridshift, "Horizontal grid shift"); +using namespace NS_PROJ; + namespace { // anonymous namespace -struct pj_opaque_hgridshift { - double t_final; - double t_epoch; +struct hgridshiftData { + double t_final = 0; + double t_epoch = 0; + ListOfHGrids grids{}; }; } // anonymous namespace static PJ_XYZ forward_3d(PJ_LPZ lpz, PJ *P) { + auto Q = static_cast<hgridshiftData*>(P->opaque); PJ_COORD point = {{0,0,0,0}}; point.lpz = lpz; - if (!P->hgrids.empty()) { + if (!Q->grids.empty()) { /* Only try the gridshift if at least one grid is loaded, * otherwise just pass the coordinate through unchanged. */ - point.lp = proj_hgrid_apply(P, point.lp, PJ_FWD); + point.lp = proj_hgrid_apply(P, Q->grids, point.lp, PJ_FWD); } return point.xyz; @@ -31,20 +36,21 @@ static PJ_XYZ forward_3d(PJ_LPZ lpz, PJ *P) { static PJ_LPZ reverse_3d(PJ_XYZ xyz, PJ *P) { + auto Q = static_cast<hgridshiftData*>(P->opaque); PJ_COORD point = {{0,0,0,0}}; point.xyz = xyz; - if (!P->hgrids.empty()) { + if (!Q->grids.empty()) { /* Only try the gridshift if at least one grid is loaded, * otherwise just pass the coordinate through unchanged. */ - point.lp = proj_hgrid_apply(P, point.lp, PJ_INV); + point.lp = proj_hgrid_apply(P, Q->grids, point.lp, PJ_INV); } return point.lpz; } static PJ_COORD forward_4d(PJ_COORD obs, PJ *P) { - struct pj_opaque_hgridshift *Q = (struct pj_opaque_hgridshift *) P->opaque; + struct hgridshiftData *Q = (struct hgridshiftData *) P->opaque; PJ_COORD point = obs; /* If transformation is not time restricted, we always call it */ @@ -62,7 +68,7 @@ static PJ_COORD forward_4d(PJ_COORD obs, PJ *P) { } static PJ_COORD reverse_4d(PJ_COORD obs, PJ *P) { - struct pj_opaque_hgridshift *Q = (struct pj_opaque_hgridshift *) P->opaque; + struct hgridshiftData *Q = (struct hgridshiftData *) P->opaque; PJ_COORD point = obs; /* If transformation is not time restricted, we always call it */ @@ -78,12 +84,20 @@ static PJ_COORD reverse_4d(PJ_COORD obs, PJ *P) { return point; } +static PJ *destructor (PJ *P, int errlev) { + if (nullptr==P) + return nullptr; + + delete static_cast<struct hgridshiftData*>(P->opaque); + P->opaque = nullptr; + + return pj_default_destructor(P, errlev); +} PJ *TRANSFORMATION(hgridshift,0) { - struct pj_opaque_hgridshift *Q = static_cast<struct pj_opaque_hgridshift*>(pj_calloc (1, sizeof (struct pj_opaque_hgridshift))); - if (nullptr==Q) - return pj_default_destructor (P, ENOMEM); + auto Q = new hgridshiftData; P->opaque = (void *) Q; + P->destructor = destructor; P->fwd4d = forward_4d; P->inv4d = reverse_4d; @@ -97,7 +111,7 @@ PJ *TRANSFORMATION(hgridshift,0) { if (0==pj_param(P->ctx, P->params, "tgrids").i) { proj_log_error(P, "hgridshift: +grids parameter missing."); - return pj_default_destructor (P, PJD_ERR_NO_ARGS); + return destructor (P, PJD_ERR_NO_ARGS); } /* TODO: Refactor into shared function that can be used */ @@ -121,11 +135,11 @@ PJ *TRANSFORMATION(hgridshift,0) { Q->t_epoch = pj_param (P->ctx, P->params, "dt_epoch").f; - proj_hgrid_init(P, "grids"); + Q->grids = proj_hgrid_init(P, "grids"); /* Was gridlist compiled properly? */ if ( proj_errno(P) ) { proj_log_error(P, "hgridshift: could not find required grid(s)."); - return pj_default_destructor(P, PJD_ERR_FAILED_TO_LOAD_GRID); + return destructor(P, PJD_ERR_FAILED_TO_LOAD_GRID); } return P; diff --git a/src/transformations/vgridshift.cpp b/src/transformations/vgridshift.cpp index 51f129ab..b964f45b 100644 --- a/src/transformations/vgridshift.cpp +++ b/src/transformations/vgridshift.cpp @@ -6,26 +6,30 @@ #include <time.h> #include "proj_internal.h" +#include "grids.hpp" PROJ_HEAD(vgridshift, "Vertical grid shift"); +using namespace NS_PROJ; + namespace { // anonymous namespace -struct pj_opaque_vgridshift { - double t_final; - double t_epoch; - double forward_multiplier; +struct vgridshiftData { + double t_final = 0; + double t_epoch = 0; + double forward_multiplier = 0; + ListOfVGrids grids{}; }; } // anonymous namespace static PJ_XYZ forward_3d(PJ_LPZ lpz, PJ *P) { - struct pj_opaque_vgridshift *Q = (struct pj_opaque_vgridshift *) P->opaque; + struct vgridshiftData *Q = (struct vgridshiftData *) P->opaque; PJ_COORD point = {{0,0,0,0}}; point.lpz = lpz; - if (!P->vgrids.empty()) { + if (!Q->grids.empty()) { /* Only try the gridshift if at least one grid is loaded, * otherwise just pass the coordinate through unchanged. */ - point.xyz.z += proj_vgrid_value(P, point.lp, Q->forward_multiplier); + point.xyz.z += proj_vgrid_value(P, Q->grids, point.lp, Q->forward_multiplier); } return point.xyz; @@ -33,14 +37,14 @@ static PJ_XYZ forward_3d(PJ_LPZ lpz, PJ *P) { static PJ_LPZ reverse_3d(PJ_XYZ xyz, PJ *P) { - struct pj_opaque_vgridshift *Q = (struct pj_opaque_vgridshift *) P->opaque; + struct vgridshiftData *Q = (struct vgridshiftData *) P->opaque; PJ_COORD point = {{0,0,0,0}}; point.xyz = xyz; - if (!P->vgrids.empty()) { + if (!Q->grids.empty()) { /* Only try the gridshift if at least one grid is loaded, * otherwise just pass the coordinate through unchanged. */ - point.xyz.z -= proj_vgrid_value(P, point.lp, Q->forward_multiplier); + point.xyz.z -= proj_vgrid_value(P, Q->grids, point.lp, Q->forward_multiplier); } return point.lpz; @@ -48,7 +52,7 @@ static PJ_LPZ reverse_3d(PJ_XYZ xyz, PJ *P) { static PJ_COORD forward_4d(PJ_COORD obs, PJ *P) { - struct pj_opaque_vgridshift *Q = (struct pj_opaque_vgridshift *) P->opaque; + struct vgridshiftData *Q = (struct vgridshiftData *) P->opaque; PJ_COORD point = obs; /* If transformation is not time restricted, we always call it */ @@ -66,7 +70,7 @@ static PJ_COORD forward_4d(PJ_COORD obs, PJ *P) { } static PJ_COORD reverse_4d(PJ_COORD obs, PJ *P) { - struct pj_opaque_vgridshift *Q = (struct pj_opaque_vgridshift *) P->opaque; + struct vgridshiftData *Q = (struct vgridshiftData *) P->opaque; PJ_COORD point = obs; /* If transformation is not time restricted, we always call it */ @@ -82,16 +86,24 @@ static PJ_COORD reverse_4d(PJ_COORD obs, PJ *P) { return point; } +static PJ *destructor (PJ *P, int errlev) { + if (nullptr==P) + return nullptr; + + delete static_cast<struct vgridshiftData*>(P->opaque); + P->opaque = nullptr; + + return pj_default_destructor(P, errlev); +} PJ *TRANSFORMATION(vgridshift,0) { - struct pj_opaque_vgridshift *Q = static_cast<struct pj_opaque_vgridshift*>(pj_calloc (1, sizeof (struct pj_opaque_vgridshift))); - if (nullptr==Q) - return pj_default_destructor (P, ENOMEM); + auto Q = new vgridshiftData; P->opaque = (void *) Q; + P->destructor = destructor; if (!pj_param(P->ctx, P->params, "tgrids").i) { proj_log_error(P, "vgridshift: +grids parameter missing."); - return pj_default_destructor(P, PJD_ERR_NO_ARGS); + return destructor(P, PJD_ERR_NO_ARGS); } /* TODO: Refactor into shared function that can be used */ @@ -121,12 +133,12 @@ PJ *TRANSFORMATION(vgridshift,0) { } /* Build gridlist. P->vgridlist_geoid can be empty if +grids only ask for optional grids. */ - proj_vgrid_init(P, "grids"); + Q->grids = proj_vgrid_init(P, "grids"); /* Was gridlist compiled properly? */ if ( proj_errno(P) ) { proj_log_error(P, "vgridshift: could not find required grid(s)."); - return pj_default_destructor(P, PJD_ERR_FAILED_TO_LOAD_GRID); + return destructor(P, PJD_ERR_FAILED_TO_LOAD_GRID); } P->fwd4d = forward_4d; |
