Skip to content

Commit

Permalink
Use same arguments to printf format string for both radians and degre…
Browse files Browse the repository at this point in the history
…es in output by cct (#2453)

Currently the output of the cct utility is different between radians
and degrees (as expected by cct), because of a bug in cct:

$ printf "1 2\n" | cct -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=rad
  1.0000000000    2.0000000000        0.0000        0.0000

$ printf "1 2\n" | cct -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=deg
       1.0000         2.0000        0.0000        0.0000

The arguments to the printf format string are as follows:

 * radians: width 14, precision 10
 * degrees: width 13, precision  4 (this is by mistake. bug!)

After the suggested fix has been applied, output will be the same for
both radians and degrees:

$ printf "1 2\n" | cct -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=rad
  1.0000000000    2.0000000000        0.0000        0.0000

$ printf "1 2\n" | cct -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=deg
  1.0000000000    2.0000000000        0.0000        0.0000

The cause of the bug is that cct does test if it "has radians to output",
but "neglects" to test if it "has degrees to output", resulting in using
different arguments to the printf format string in the latter case.

The fix makes cct test if it "has either radians or degrees to output".
  • Loading branch information
Houder authored and rouault committed Nov 28, 2020
1 parent f0ef1b9 commit 54616db
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/apps/cct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,14 @@ int main(int argc, char **argv) {
comment_delimiter = (comment && *comment) ? whitespace : blank_comment;

/* Time to print the result */
if (proj_angular_output (P, direction)) {
point.lpzt.lam = proj_todeg (point.lpzt.lam);
point.lpzt.phi = proj_todeg (point.lpzt.phi);
/* use same arguments to printf format string for both radians and
degrees; convert radians to degrees before printing */
if (proj_angular_output (P, direction) ||
proj_degree_output (P, direction)) {
if (proj_angular_output (P, direction)) {
point.lpzt.lam = proj_todeg (point.lpzt.lam);
point.lpzt.phi = proj_todeg (point.lpzt.phi);
}
print (PJ_LOG_NONE, "%14.*f %14.*f %12.*f %12.4f%s%s",
decimals_angles, point.xyzt.x,
decimals_angles, point.xyzt.y,
Expand Down
22 changes: 22 additions & 0 deletions test/cli/testcct
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ echo "Testing cct -d 8 +proj=merc +R=1" >> ${OUT}
echo "90 45" 0 | $EXE -d 8 +proj=merc +R=1 >>${OUT}
echo "" >>${OUT}

# tests without specifying the number of decimals (by default: 10 for radians and degrees, 4 for meters)
echo "Testing echo 0.5 2 | cct -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=rad" >> ${OUT}
echo 0.5 2 | $EXE -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=rad >> ${OUT}

echo "Testing echo 0.5 2 | cct -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=deg" >> ${OUT}
echo 0.5 2 | $EXE -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=deg >> ${OUT}

echo "Testing echo 0.5 2 | cct -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=m +xy_out=km" >> ${OUT}
echo 0.5 2 | $EXE -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=m +xy_out=km >> ${OUT}
echo "" >> ${OUT}

# tests for which the number of decimals has been specified (-d 6)
echo "Testing echo 0.5 2 | cct -d 6 -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=rad" >> ${OUT}
echo 0.5 2 | $EXE -d 6 -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=rad >> ${OUT}

echo "Testing echo 0.5 2 | cct -d 6 -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=deg" >> ${OUT}
echo 0.5 2 | $EXE -d 6 -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=deg >> ${OUT}

echo "Testing echo 0.5 2 | cct -d 6 -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=m +xy_out=km" >> ${OUT}
echo 0.5 2 | $EXE -d 6 -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=m +xy_out=km >> ${OUT}
echo "" >> ${OUT}

# do 'diff' with distribution results
echo "diff ${OUT} with testcct_out.dist"
diff -u ${OUT} ${TEST_CLI_DIR}/testcct_out.dist
Expand Down
14 changes: 14 additions & 0 deletions test/cli/testcct_out.dist
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
Testing cct -d 8 +proj=merc +R=1
1.57079633 0.88137359 0.00000000 inf

Testing echo 0.5 2 | cct -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=rad
0.5000000000 2.0000000000 0.0000 0.0000
Testing echo 0.5 2 | cct -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=deg
0.5000000000 2.0000000000 0.0000 0.0000
Testing echo 0.5 2 | cct -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=m +xy_out=km
0.0005 0.0020 0.0000 0.0000

Testing echo 0.5 2 | cct -d 6 -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=rad
0.500000 2.000000 0.000000 0.0000
Testing echo 0.5 2 | cct -d 6 -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=deg
0.500000 2.000000 0.000000 0.0000
Testing echo 0.5 2 | cct -d 6 -z 0 -t 0 +proj=pipeline +step +proj=unitconvert +xy_in=m +xy_out=km
0.000500 0.002000 0.000000 0.0000

0 comments on commit 54616db

Please sign in to comment.