Skip to content

Commit

Permalink
cct: forward comments to output
Browse files Browse the repository at this point in the history
Any text written after the coordinate input will automatically be
forwarded to the output stream. Text in columns before the coordinate
input is discarded in the output. This works for any combination of -c, -t
and -z parameters:

$ echo 12 56 100 2018.0 comment comment | cct +proj=merc
 1335833.8895   7522963.2411      100.0000     2018.0000 comment commen

$ echo text 12 56 100 2018.0 comment | cct -c 2,3,4,5 +proj=merc
 1335833.8895   7522963.2411      100.0000     2018.0000 comment

$ echo text 12 56 comment | cct -c 2,3 -t0 -z0 +proj=merc
 1335833.8895   7522963.2411        0.0000        0.0000 comment

$ echo 12 56 comment | cct -t0 -z0 +proj=merc
 1335833.8895   7522963.2411        0.0000        0.0000 comment

Closes OSGeo#918
  • Loading branch information
kbevers committed Sep 6, 2018
1 parent c50a573 commit d85129e
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/cct.c
Expand Up @@ -206,7 +206,7 @@ int main(int argc, char **argv) {
PJ_COORD point;
PJ_PROJ_INFO info;
OPTARGS *o;
char *buf;
char *buf, *comment;
int nfields = 4, direction = 1, skip_lines = 0, verbose;
double fixed_z = HUGE_VAL, fixed_time = HUGE_VAL;
int decimals_angles = 10;
Expand Down Expand Up @@ -268,14 +268,16 @@ int main(int argc, char **argv) {
int dec = atoi (opt_arg (o, "d"));
decimals_angles = dec;
decimals_distances = dec;
nfields--;
}

if (opt_given (o, "s")) {
skip_lines = atoi (opt_arg(o, "s"));
}

if (opt_given (o, "c")) {
/* reset colum numbers to ease comment output later on */
for (int i=0; i<4; i++) columns_xyzt[i] = 0;

/* cppcheck-suppress invalidscanf */
int ncols = sscanf (opt_arg (o, "c"), "%d,%d,%d,%d", columns_xyzt, columns_xyzt+1, columns_xyzt+2, columns_xyzt+3);
if (ncols != nfields) {
Expand Down Expand Up @@ -371,14 +373,35 @@ int main(int argc, char **argv) {
}
proj_errno_restore (P, err);

/* handle comment string */
if (opt_given(o, "c")) {
/* what number is the last coordinate column in the input data? */
int colmax = 0;
for (int i=0; i<4; i++)
colmax = MAX(colmax, columns_xyzt[i]);
comment = column(buf, colmax+1);
} else {
comment = column(buf, nfields+1);
}

/* 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);
print (PJ_LOG_NONE, "%14.*f %14.*f %12.*f %12.4f\n", decimals_angles, point.xyzt.x, decimals_angles, point.xyzt.y, decimals_distances, point.xyzt.z, point.xyzt.t);
print (PJ_LOG_NONE, "%14.*f %14.*f %12.*f %12.4f %s\n",
decimals_angles, point.xyzt.x,
decimals_angles, point.xyzt.y,
decimals_distances, point.xyzt.z,
point.xyzt.t, comment
);
}
else
print (PJ_LOG_NONE, "%13.*f %13.*f %12.*f %12.4f\n", decimals_distances, point.xyzt.x, decimals_distances, point.xyzt.y, decimals_distances, point.xyzt.z, point.xyzt.t);
print (PJ_LOG_NONE, "%13.*f %13.*f %12.*f %12.4f %s\n",
decimals_distances, point.xyzt.x,
decimals_distances, point.xyzt.y,
decimals_distances, point.xyzt.z,
point.xyzt.t, comment
);
}

if (stdout != fout)
Expand Down

0 comments on commit d85129e

Please sign in to comment.