From e438e1e0c54f82c8c3489f4d8058941a1672bd3c Mon Sep 17 00:00:00 2001 From: Oliver Jowett Date: Mon, 17 Aug 2015 18:07:40 +0100 Subject: [PATCH] Fix decoding of even lat=0 + odd lat=0 which can have 3 results (-90, 0, +90) --- cpr.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/cpr.c b/cpr.c index d641670..3f8187b 100644 --- a/cpr.c +++ b/cpr.c @@ -250,8 +250,26 @@ int decodeCPRsurface(double reflat, double reflon, // reflat=-40, rlat=4, use rlat=4 // reflat=-40, rlat=6, use rlat=6-90 = -84 - if ( (rlat0 - reflat) > 45 ) rlat0 -= 90; - if ( (rlat1 - reflat) > 45 ) rlat1 -= 90; + // As a special case, -90, 0 and +90 all encode to zero, so + // there's a little extra work to do there. + + if (rlat0 == 0) { + if (reflat < -45) + rlat0 = -90; + else if (reflat > 45) + rlat0 = 90; + } else if ((rlat0 - reflat) > 45) { + rlat0 -= 90; + } + + if (rlat1 == 0) { + if (reflat < -45) + rlat1 = -90; + else if (reflat > 45) + rlat1 = 90; + } else if ((rlat1 - reflat) > 45) { + rlat1 -= 90; + } // Check to see that the latitude is in range: -90 .. +90 if (rlat0 < -90 || rlat0 > 90 || rlat1 < -90 || rlat1 > 90)