Skip to content

Commit

Permalink
Handle unfound optional grids more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
kbevers committed Feb 16, 2017
1 parent d720f20 commit bd951b9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 28 deletions.
44 changes: 28 additions & 16 deletions src/PJ_hgridshift.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ static XYZ forward_3d(LPZ lpz, PJ *P) {
PJ_TRIPLET point;
point.lpz = lpz;

pj_apply_gridshift_3( pj_get_ctx( P ), P->gridlist,
P->gridlist_count, 0, 1, 0,
&point.xyz.x, &point.xyz.y, &point.xyz.z );
if (P->gridlist != NULL) {
/* Only try the gridshift if at least one grid is loaded,
* otherwise just pass the coordinate through unchanged. */
pj_apply_gridshift_3( P->ctx, P->gridlist,
P->gridlist_count, 0, 1, 0,
&point.xyz.x, &point.xyz.y, &point.xyz.z );
}

return point.xyz;
}
Expand All @@ -37,9 +41,13 @@ static LPZ reverse_3d(XYZ xyz, PJ *P) {
PJ_TRIPLET point;
point.xyz = xyz;

pj_apply_gridshift_3( pj_get_ctx( P ), P->gridlist,
P->gridlist_count, 1, 1, 0,
&point.xyz.x, &point.xyz.y, &point.xyz.z );
if (P->gridlist != NULL) {
/* Only try the gridshift if at least one grid is loaded,
* otherwise just pass the coordinate through unchanged. */
pj_apply_gridshift_3( P->ctx, P->gridlist,
P->gridlist_count, 1, 1, 0,
&point.xyz.x, &point.xyz.y, &point.xyz.z );
}

return point.lpz;
}
Expand All @@ -51,6 +59,7 @@ static PJ_OBS forward_obs(PJ_OBS obs, PJ *P) {
return point;
}


static PJ_OBS reverse_obs(PJ_OBS obs, PJ *P) {
PJ_OBS point;
point.coo.lpz = reverse_3d (obs.coo.xyz, P);
Expand All @@ -60,17 +69,20 @@ static PJ_OBS reverse_obs(PJ_OBS obs, PJ *P) {

PJ *PROJECTION(hgridshift) {

if( P->gridlist == NULL ) {
P->gridlist = pj_gridlist_from_nadgrids( pj_get_ctx( P ),
pj_param(P->ctx, P->params, "sgrids").s,
&(P->gridlist_count) );

if( P->gridlist == NULL || P->gridlist_count == 0 ) {
pj_log_error(P, "hgridshift: grids parameter missing.");
return freeup_msg(P, -1);
}
if (!pj_param(P->ctx, P->params, "tgrids").i) {
pj_log_error(P, "hgridshift: +grids parameter missing.");
return freeup_msg(P, -1);
}

/* Build gridlist. P->gridlist can be empty if +grids only ask for optional grids. */
P->gridlist = pj_gridlist_from_nadgrids( P->ctx, pj_param(P->ctx, P->params, "sgrids").s,
&(P->gridlist_count) );

/* Was gridlist compiled properly? */
if ( pj_ctx_get_errno(P->ctx) ) {
pj_log_error(P, "hgridshift: could not find required grid(s).");
return freeup_msg(P, -38);
}

P->fwdobs = forward_obs;
P->invobs = reverse_obs;
Expand Down Expand Up @@ -100,7 +112,7 @@ int pj_hgridshift_selftest (void) {
* optional grid in the test. The test is fairly useless
* if the grid isn't found, but at least it won't fail
* because of a missing file */
P = pj_create ("+proj=hgridshift +grids=@nzgd2kgrid0005.gsb");
P = pj_create ("+proj=hgridshift +grids=@nzgd2kgrid0005.gsb +ellps=GRS80");
if (0==P)
return 10;

Expand Down
44 changes: 32 additions & 12 deletions src/PJ_vgridshift.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ static void freeup (PJ *P) {
static XYZ forward_3d(LPZ lpz, PJ *P) {
PJ_TRIPLET point;
point.lpz = lpz;
pj_apply_vgridshift( P, "sgrids",
&(P->vgridlist_geoid),
&(P->vgridlist_geoid_count),
0, 1, 0,
&point.xyz.x, &point.xyz.y, &point.xyz.z );

if (P->gridlist != NULL) {
/* Only try the gridshift if at least one grid is loaded,
* otherwise just pass the coordinate through unchanged. */
pj_apply_vgridshift( P, "sgrids",
&(P->vgridlist_geoid),
&(P->vgridlist_geoid_count),
0, 1, 0,
&point.xyz.x, &point.xyz.y, &point.xyz.z );
}

return point.xyz;
}
Expand All @@ -37,11 +42,16 @@ static XYZ forward_3d(LPZ lpz, PJ *P) {
static LPZ reverse_3d(XYZ xyz, PJ *P) {
PJ_TRIPLET point;
point.xyz = xyz;
pj_apply_vgridshift( P, "sgrids",
&(P->vgridlist_geoid),
&(P->vgridlist_geoid_count),
1, 1, 0,
&point.xyz.x, &point.xyz.y, &point.xyz.z );

if (P->gridlist != NULL) {
/* Only try the gridshift if at least one grid is loaded,
* otherwise just pass the coordinate through unchanged. */
pj_apply_vgridshift( P, "sgrids",
&(P->vgridlist_geoid),
&(P->vgridlist_geoid_count),
1, 1, 0,
&point.xyz.x, &point.xyz.y, &point.xyz.z );
}

return point.lpz;
}
Expand All @@ -63,10 +73,20 @@ static PJ_OBS reverse_obs(PJ_OBS obs, PJ *P) {
PJ *PROJECTION(vgridshift) {

if (!pj_param(P->ctx, P->params, "tgrids").i) {
pj_log_error(P, "vgridshift: grids parameter missing.");
pj_log_error(P, "vgridshift: +grids parameter missing.");
return freeup_msg(P, -1);
}

/* Build gridlist. P->gridlist can be empty if +grids only ask for optional grids. */
P->gridlist = pj_gridlist_from_nadgrids( P->ctx, pj_param(P->ctx, P->params, "sgrids").s,
&(P->gridlist_count) );

/* Was gridlist compiled properly? */
if ( pj_ctx_get_errno(P->ctx) ) {
pj_log_error(P, "vgridshift: could not find required grid(s).");
return freeup_msg(P, -38);
}

P->fwdobs = forward_obs;
P->invobs = reverse_obs;
P->fwd3d = forward_3d;
Expand Down Expand Up @@ -94,7 +114,7 @@ int pj_vgridshift_selftest (void) {
* only include it as a optional grid in the test.
* The test is fairly useless if the grid isn't found, but at least it
* won't fail because of a missing file */
P = pj_create ("+proj=vgridshift +grids=@egm96_15.gtx");
P = pj_create ("+proj=vgridshift +grids=@egm96_15.gtx +ellps=GRS80");
if (0==P)
return 10;

Expand Down

0 comments on commit bd951b9

Please sign in to comment.