From af342bf74cd154b653a0f9d931d4ca17001650b9 Mon Sep 17 00:00:00 2001 From: Kristian Evers Date: Tue, 10 Nov 2020 14:08:30 +0100 Subject: Allow cct to instantiate operations via object codes or names (#2419) Running cct like cct EPSG:8366 or cct "ITRF2014 to ETRF2014 (1)" is now possible. --- src/apps/cct.cpp | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'src/apps/cct.cpp') diff --git a/src/apps/cct.cpp b/src/apps/cct.cpp index f7413872..4de3cf8e 100644 --- a/src/apps/cct.cpp +++ b/src/apps/cct.cpp @@ -77,6 +77,7 @@ Thomas Knudsen, thokn@sdfe.dk, 2016-05-25/2017-10-26 #include #include #include +#include #include "proj.h" #include "proj_internal.h" @@ -193,9 +194,8 @@ static void print(PJ_LOG_LEVEL log_level, const char *fmt, ...) { free( msg_buf ); } - int main(int argc, char **argv) { - PJ *P; + PJ *P = nullptr; PJ_COORD point; PJ_PROJ_INFO info; OPTARGS *o; @@ -292,8 +292,36 @@ int main(int argc, char **argv) { } /* Setup transformation */ - P = proj_create_argv (nullptr, o->pargc, o->pargv); - if ((nullptr==P) || (0==o->pargc)) { + if (o-> pargc == 0 && o->fargc > 0) { + /* Assume we got a auth:code combination */ + std::string input(o->fargv[0]); + auto n = input.find(":"); + if (n > 0) { + std::string auth = input.substr(0,n); + std::string code = input.substr(n+1, input.length()); + P = proj_create_from_database( + nullptr, auth.c_str(), code.c_str(), PJ_CATEGORY_COORDINATE_OPERATION, 0, nullptr + ); + } + if( P == nullptr ) { + /* if we didn't get a auth:code combo we try to see if the input matches */ + /* anything else */ + P = proj_create(nullptr, o->fargv[0]); + } + + /* If instantiating operation without +-options optargpm thinks the input is */ + /* a file, hence we move all o->fargv entries one place closer to the start */ + /* of the array. This effectively overwrites the input and only leaves a list */ + /* of files in o->fargv. */ + o->fargc = o->fargc-1; + for (int j=0; j < o->fargc; j++) { + o->fargv[j] = o->fargv[j+1]; + } + } else { + P = proj_create_argv (nullptr, o->pargc, o->pargv); + } + + if (nullptr==P) { print (PJ_LOG_ERROR, "%s: Bad transformation arguments - (%s)\n '%s -h' for help", o->progname, pj_strerrno (proj_errno(P)), o->progname); free (o); -- cgit v1.2.3 From 6be4e25c66e805218c851d67157e4d1ddc0a761e Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Tue, 10 Nov 2020 14:40:17 +0100 Subject: cct: allow @filename syntax Similarly as for projinfo, allow "cct @filename" to mean read filename and use its content as if it was provided inline. Useful for WKT or PROJJSON And a tiny improvements, when the object definition contains ':', only try proj_create_from_database() if the left part (authority name) matches a known authority, to avoid a warning. --- src/apps/cct.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) (limited to 'src/apps/cct.cpp') diff --git a/src/apps/cct.cpp b/src/apps/cct.cpp index 4de3cf8e..c482efc2 100644 --- a/src/apps/cct.cpp +++ b/src/apps/cct.cpp @@ -77,6 +77,8 @@ Thomas Knudsen, thokn@sdfe.dk, 2016-05-25/2017-10-26 #include #include #include + +#include // std::ifstream #include #include "proj.h" @@ -293,20 +295,55 @@ int main(int argc, char **argv) { /* Setup transformation */ if (o-> pargc == 0 && o->fargc > 0) { - /* Assume we got a auth:code combination */ std::string input(o->fargv[0]); + + if (!input.empty() && input[0] == '@') { + std::ifstream fs; + auto filename = input.substr(1); + fs.open(filename, std::fstream::in | std::fstream::binary); + if (!fs.is_open()) { + std::cerr << "cannot open " << filename << std::endl; + std::exit(1); + } + input.clear(); + while (!fs.eof()) { + char buffer[256]; + fs.read(buffer, sizeof(buffer)); + input.append(buffer, static_cast(fs.gcount())); + if (input.size() > 100 * 1000) { + fs.close(); + std::cerr << "too big file " << filename << std::endl; + std::exit(1); + } + } + fs.close(); + } + + /* Assume we got a auth:code combination */ auto n = input.find(":"); if (n > 0) { std::string auth = input.substr(0,n); std::string code = input.substr(n+1, input.length()); - P = proj_create_from_database( - nullptr, auth.c_str(), code.c_str(), PJ_CATEGORY_COORDINATE_OPERATION, 0, nullptr - ); + // Check that the authority matches one of the known ones + auto authorityList = proj_get_authorities_from_database(nullptr); + if( authorityList ) + { + for( auto iter = authorityList; *iter; iter++ ) + { + if( *iter == auth ) { + P = proj_create_from_database( + nullptr, auth.c_str(), code.c_str(), + PJ_CATEGORY_COORDINATE_OPERATION, 0, nullptr); + break; + } + } + proj_string_list_destroy(authorityList); + } } if( P == nullptr ) { /* if we didn't get a auth:code combo we try to see if the input matches */ /* anything else */ - P = proj_create(nullptr, o->fargv[0]); + P = proj_create(nullptr, input.c_str()); } /* If instantiating operation without +-options optargpm thinks the input is */ -- cgit v1.2.3 From c560b7957664c32e2465e8425abaccc5a6b2607d Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Tue, 17 Nov 2020 12:39:48 +0100 Subject: cs2cs, cct, proj and geod: fflush(stdout) after each line to emit each result as soon as it is produced This is needed when working with pipes, when stdout is not an interactive terminal, and thus the behaviour is to have it buffered as a regular file, whereas with an interactive terminal, each newline character causes an implicit flush. --- src/apps/cct.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/apps/cct.cpp') diff --git a/src/apps/cct.cpp b/src/apps/cct.cpp index c482efc2..fa75267a 100644 --- a/src/apps/cct.cpp +++ b/src/apps/cct.cpp @@ -470,6 +470,8 @@ int main(int argc, char **argv) { decimals_distances, point.xyzt.z, point.xyzt.t, comment_delimiter, comment ); + if( fout == stdout ) + fflush(stdout); } proj_destroy(P); -- cgit v1.2.3 From 2cbf659bfcdffe12a4e7e397553c886566b2e949 Mon Sep 17 00:00:00 2001 From: "J.H. van de Water" Date: Thu, 19 Nov 2020 10:52:29 +0100 Subject: Replace line feed in input line by null character As result of a modification in logging (adding a line feed), many changes had to be made in different places of cct.cpp. However, one missed the line feed in input to cct. As result of missing this, the output from cct showed a superfluous empty line after each output line, but only if the corresponding input line ended with comment. Replacing the LF in the "comment" string (present if the input line ended with comment) by a null character ('\0') solves this issue. Modification in logging? https://github.com/OSGeo/PROJ/commit/37da5e243191c04607597f6b8a77acfa017a5c99 ( cct: revise end-of-line handling in logging, and always output debug ) See also: https://github.com/OSGeo/PROJ/issues/1677 ( cct outputs excessive whitespace comments are included in input data ) --- src/apps/cct.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/apps/cct.cpp') diff --git a/src/apps/cct.cpp b/src/apps/cct.cpp index fa75267a..b0e15e62 100644 --- a/src/apps/cct.cpp +++ b/src/apps/cct.cpp @@ -450,6 +450,11 @@ int main(int argc, char **argv) { colmax = MAX(colmax, columns_xyzt[i]); comment = column(buf, colmax+1); } + /* remove the line feed from comment, as logger() above, invoked + by print() below (output), will add one */ + size_t len = strlen(comment); + if (len >= 1) + comment[len - 1] = '\0'; comment_delimiter = (comment && *comment) ? whitespace : blank_comment; /* Time to print the result */ -- cgit v1.2.3 From 43efca4ab87fb37a0931edcb6be11c0bd3784098 Mon Sep 17 00:00:00 2001 From: Kristian Evers Date: Wed, 18 Nov 2020 09:57:42 +0100 Subject: Remove pj_free() and move it's functional parts to proj_destroy() --- src/apps/cct.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/apps/cct.cpp') diff --git a/src/apps/cct.cpp b/src/apps/cct.cpp index fa75267a..e63a5fc2 100644 --- a/src/apps/cct.cpp +++ b/src/apps/cct.cpp @@ -387,7 +387,7 @@ int main(int argc, char **argv) { buf = static_cast(calloc (1, 10000)); if (nullptr==buf) { print (PJ_LOG_ERROR, "%s: Out of memory", o->progname); - pj_free (P); + proj_destroy (P); free (o); if (stdout != fout) fclose (fout); -- cgit v1.2.3 From 56f0ad70054eea15e9671cd67aafd14bf7c11c74 Mon Sep 17 00:00:00 2001 From: Kristian Evers Date: Wed, 18 Nov 2020 10:43:16 +0100 Subject: Remove pj_errno and related functions --- src/apps/cct.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/apps/cct.cpp') diff --git a/src/apps/cct.cpp b/src/apps/cct.cpp index e63a5fc2..92867b3a 100644 --- a/src/apps/cct.cpp +++ b/src/apps/cct.cpp @@ -360,7 +360,7 @@ int main(int argc, char **argv) { if (nullptr==P) { print (PJ_LOG_ERROR, "%s: Bad transformation arguments - (%s)\n '%s -h' for help", - o->progname, pj_strerrno (proj_errno(P)), o->progname); + o->progname, proj_errno_string (proj_errno(P)), o->progname); free (o); if (stdout != fout) fclose (fout); @@ -435,7 +435,7 @@ int main(int argc, char **argv) { if (HUGE_VAL==point.xyzt.x) { /* transformation error */ print (PJ_LOG_NONE, "# Record %d TRANSFORMATION ERROR: %s (%s)", - (int) o->record_index, buf, pj_strerrno (proj_errno(P))); + (int) o->record_index, buf, proj_errno_string (proj_errno(P))); proj_errno_restore (P, err); continue; } -- cgit v1.2.3 From 24c74a4d1cead8df41a7f22c8f28b1bf9e884de9 Mon Sep 17 00:00:00 2001 From: Houder <74594217+Houder@users.noreply.github.com> Date: Sat, 28 Nov 2020 13:45:06 +0100 Subject: Use same arguments to printf format string for both radians and degrees 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". --- src/apps/cct.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src/apps/cct.cpp') diff --git a/src/apps/cct.cpp b/src/apps/cct.cpp index ebfa6a23..4f21f10a 100644 --- a/src/apps/cct.cpp +++ b/src/apps/cct.cpp @@ -458,9 +458,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, -- cgit v1.2.3