Skip to content

Commit

Permalink
Inverse tmerc spherical: fix wrong sign of latitude when lat_0 is used (
Browse files Browse the repository at this point in the history
fixes #2468)

Corrected formula given by @evanmiller
  • Loading branch information
rouault committed Nov 29, 2020
1 parent 87270ae commit a86c745
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/projections/tmerc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,12 @@ static PJ_LP approx_s_inv (PJ_XY xy, PJ *P) {
return proj_coord_error().lp;
}
g = .5 * (h - 1. / h);
h = cos (P->phi0 + xy.y / Q->esp);
const double acos_h = P->phi0 + xy.y / Q->esp;
h = cos (acos_h);
lp.phi = asin(sqrt((1. - h * h) / (1. + g * g)));

/* Make sure that phi is on the correct hemisphere when false northing is used */
if (xy.y < 0. && -lp.phi+P->phi0 < 0.0) lp.phi = -lp.phi;
lp.phi = copysign(lp.phi, acos_h);

lp.lam = (g != 0.0 || h != 0.0) ? atan2 (g, h) : 0.;
return lp;
Expand Down
124 changes: 124 additions & 0 deletions test/gie/builtins.gie
Original file line number Diff line number Diff line change
Expand Up @@ -5781,6 +5781,130 @@ expect -0.001790493 0.000895247
accept -200 -100
expect -0.001790493 -0.000895247

-------------------------------------------------------------------------------
# Approx tmerc on a almost spherical ellipsoid, lat_0 north hemisphere

operation +proj=tmerc +a=6400000 +rf=1e12 +k=0.9 +lat_0=40 +approx
-------------------------------------------------------------------------------
tolerance 0.1 mm

accept 0 -30
expect 0 -7037167.5440
roundtrip 1

accept 1 -30
expect 87064.5795 -7037547.4590
roundtrip 1

accept -1 -30
expect -87064.5795 -7037547.4590
roundtrip 1

accept 0 30
expect 0 -1005309.6491
roundtrip 1

accept 0 40
expect 0 0
roundtrip 1

accept 1 41
expect 75872.2182 100965.3718
roundtrip 1

-------------------------------------------------------------------------------
# Approx tmerc on a sphere, lat_0 north hemisphere

operation +proj=tmerc +R=6400000 +k=0.9 +lat_0=40
-------------------------------------------------------------------------------
tolerance 0.1 mm

accept 0 -30
expect 0 -7037167.5440
roundtrip 1

accept 1 -30
expect 87064.5795 -7037547.4590
roundtrip 1

accept -1 -30
expect -87064.5795 -7037547.4590
roundtrip 1

accept 0 30
expect 0 -1005309.6491
roundtrip 1

accept 0 40
expect 0 0
roundtrip 1

accept 1 41
expect 75872.2182 100965.3718
roundtrip 1

-------------------------------------------------------------------------------
# Approx tmerc on a almost spherical ellipsoid, lat_0 south hemisphere

operation +proj=tmerc +a=6400000 +rf=1e12 +k=0.9 +lat_0=-40 +approx
-------------------------------------------------------------------------------
tolerance 0.1 mm

accept 0 -30
expect 0 1005309.6491
roundtrip 1

accept 1 -30
expect 87064.5795 1004929.7341
roundtrip 1

accept -1 -30
expect -87064.5795 1004929.7341
roundtrip 1

accept 0 30
expect 0 7037167.5440
roundtrip 1

accept 0 -40
expect 0 0
roundtrip 1

accept 1 -41
expect 75872.2182 -100965.3718
roundtrip 1

-------------------------------------------------------------------------------
# Approx tmerc on a sphere, lat_0 south hemisphere

operation +proj=tmerc +R=6400000 +k=0.9 +lat_0=-40
-------------------------------------------------------------------------------
tolerance 0.1 mm

accept 0 -30
expect 0 1005309.6491
roundtrip 1

accept 1 -30
expect 87064.5795 1004929.7341
roundtrip 1

accept -1 -30
expect -87064.5795 1004929.7341
roundtrip 1

accept 0 30
expect 0 7037167.5440
roundtrip 1

accept 0 -40
expect 0 0
roundtrip 1

accept 1 -41
expect 75872.2182 -100965.3718
roundtrip 1

-------------------------------------------------------------------------------
operation +proj=tmerc +R=1
-------------------------------------------------------------------------------
Expand Down

0 comments on commit a86c745

Please sign in to comment.