From 234ca99a714cddf443798640b963d8550024fae1 Mon Sep 17 00:00:00 2001 From: Paul Wessel Date: Tue, 9 Nov 2021 14:43:31 -1000 Subject: [PATCH 1/3] Improve string breaking --- src/gmt_gdalcall.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/gmt_gdalcall.c b/src/gmt_gdalcall.c index 0e0194a2864..93a1416bb6e 100644 --- a/src/gmt_gdalcall.c +++ b/src/gmt_gdalcall.c @@ -108,7 +108,7 @@ GMT_LOCAL GDALDatasetH gdal_vector (struct GMT_CTRL *GMT, char *fname) { /* ------------------------------------------------------------------------------------------------------------ */ GMT_LOCAL char ** breakMe(struct GMT_CTRL *GMT, char *in) { - /* Breake a string "-aa -bb -cc dd" into tokens "-aa" "-bb" "-cc" "dd" */ + /* Break a string "-mo TIFFTAG_XRESOLUTION=300 -a_srs '+proj=stere +lat_0=90'" into tokens */ /* Based on GMT_Create_Options() */ unsigned int pos = 0, k, n_args = 0; bool quoted; @@ -122,11 +122,11 @@ GMT_LOCAL char ** breakMe(struct GMT_CTRL *GMT, char *in) { txt_in = strdup (in); args = gmt_M_memory (GMT, NULL, n_alloc, char *); - /* txt_in can contain options that take multi-word text strings, e.g., -B+t"My title". We avoid the problem of splitting - * these items by temporarily replacing spaces inside quoted strings with ASCII 31 US (Unit Separator), do the strtok on + /* txt_in can contain options that take multi-word text strings, e.g., '+proj=stere +lat_0=90'. We avoid the problem of splitting + * these items by temporarily replacing spaces inside single quoted strings with ASCII 31 US (Unit Separator), do the strtok on * space, and then replace all ASCII 31 with space at the end (we do the same for tab using ASCII 29 GS (group separator) */ for (k = 0, quoted = false; txt_in[k]; k++) { - if (txt_in[k] == '\"') quoted = !quoted; /* Initially false, becomes true at start of quote, then false when exit quote */ + if (txt_in[k] == '\'') quoted = !quoted; /* Initially false, becomes true at start of quote, then false when exit quote */ else if (quoted && txt_in[k] == '\t') txt_in[k] = GMT_ASCII_GS; else if (quoted && txt_in[k] == ' ') txt_in[k] = GMT_ASCII_US; } @@ -139,7 +139,7 @@ GMT_LOCAL char ** breakMe(struct GMT_CTRL *GMT, char *in) { p[k] = ' '; /* Replace spaces and tabs masked above */ } for (i = o = 0; p[i]; i++) - if (p[i] != '\"') p[o++] = p[i]; /* Ignore double quotes */ + if (p[i] != '\"') p[o++] = p[i]; /* Ignore any double quotes */ p[o] = '\0'; args[n_args++] = strdup(p); @@ -148,7 +148,7 @@ GMT_LOCAL char ** breakMe(struct GMT_CTRL *GMT, char *in) { args = gmt_M_memory(GMT, args, n_alloc, char *); } } - for (k = 0; txt_in[k]; k++) /* Restore input string to prestine condition */ + for (k = 0; txt_in[k]; k++) /* Restore input string to pristine condition */ if (txt_in[k] == GMT_ASCII_GS) txt_in[k] = '\t'; else if (txt_in[k] == GMT_ASCII_US) txt_in[k] = ' '; /* Replace spaces and tabs masked above */ From e6472c2d6dfc980618733e362768689140c1dbdb Mon Sep 17 00:00:00 2001 From: Paul Wessel Date: Thu, 11 Nov 2021 20:03:11 -1000 Subject: [PATCH 2/3] Update docs --- doc/rst/source/psconvert.rst | 7 ++----- src/gmt_gdalcall.c | 2 +- src/psconvert.c | 26 ++++++++++---------------- 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/doc/rst/source/psconvert.rst b/doc/rst/source/psconvert.rst index bf3d966d3b4..005300f8a6b 100644 --- a/doc/rst/source/psconvert.rst +++ b/doc/rst/source/psconvert.rst @@ -226,12 +226,9 @@ Optional Arguments by the Ghostscript. In that case you really must use **-B** or use a slightly off-white color. - Together with **-V** it prints on screen the gdal_translate - (gdal_translate is a command line tool from the GDAL package) + Together with **-V** it prints on screen the :doc:`grdgdal` command that reads the raster + world file and creates a true - geotiff file. Append **+g** to do a system call to gdal_translate - and create a geoTIFF image right away. The output file will have a - .tiff extension. + geotiff file. The output file will have a .tiff extension. The world file naming follows the convention of jamming a 'w' in the file extension. So, if output is tif **-Tt** the world file is a diff --git a/src/gmt_gdalcall.c b/src/gmt_gdalcall.c index 93a1416bb6e..5d21a39ccfa 100644 --- a/src/gmt_gdalcall.c +++ b/src/gmt_gdalcall.c @@ -139,7 +139,7 @@ GMT_LOCAL char ** breakMe(struct GMT_CTRL *GMT, char *in) { p[k] = ' '; /* Replace spaces and tabs masked above */ } for (i = o = 0; p[i]; i++) - if (p[i] != '\"') p[o++] = p[i]; /* Ignore any double quotes */ + if (p[i] != '\'') p[o++] = p[i]; /* Ignore any single quotes */ p[o] = '\0'; args[n_args++] = strdup(p); diff --git a/src/psconvert.c b/src/psconvert.c index 1db95c8e270..2591ce58682 100644 --- a/src/psconvert.c +++ b/src/psconvert.c @@ -720,7 +720,7 @@ static int usage (struct GMTAPI_CTRL *API, int level) { GMT_Usage (API, -2, "Note: The EPS format can be combined with any of the other formats. " "For example, -Tef creates both an EPS and PDF file."); GMT_Option (API, "V"); - GMT_Usage (API, -2, "Note: Shows the gdal_translate command, in case you want to use this program " + GMT_Usage (API, -2, "Note: Shows the grdgdal command, in case you want to use this program " "to create a geoTIFF file."); GMT_Usage (API, 1, "\n-W[+a[/][+g][+k][+l/][+n][+o][+t][+u<URL>]"); GMT_Usage (API, -2, "Write an ESRI type world file suitable to make .tif files " @@ -735,8 +735,7 @@ static int usage (struct GMTAPI_CTRL *API, int level) { "computations. The world file naming follows the convention of jamming " "a 'w' in the file extension. So, if the output is tif (-Tt) the world " "file is a .tfw, for jpeg a .jgw, and so on. A few modifiers are available:"); - GMT_Usage (API, 3, "+g Do a system call to gdal_translate and produce a true " - "eoTIFF image right away. The output file will have the extension " + GMT_Usage (API, 3, "+g Produce a true geoTIFF image right away. The output file will have the extension " ".tiff. See the man page for other 'gotchas'. Automatically sets -A -P."); GMT_Usage (API, 3, "+k Create a minimalist KML file that allows loading the " "image in Google Earth. Note that for this option the image must be " @@ -1596,7 +1595,7 @@ EXTERN_MSC int GMT_psconvert (void *V_API, int mode, void *args) { char ps_file[PATH_MAX] = "", no_U_file[PATH_MAX] = "", clean_PS_file[PATH_MAX] = "", tmp_file[PATH_MAX] = "", out_file[PATH_MAX] = "", BB_file[PATH_MAX] = "", resolution[GMT_LEN128] = "", jpeg_device[GMT_LEN16] = {""}; char *line = NULL, c1[20] = {""}, c2[20] = {""}, c3[20] = {""}, c4[20] = {""}, GSstring[GMT_LEN64] = {""}, - cmd[GMT_BUFSIZ] = {""}, proj4_name[20] = {""}, *quiet = NULL; + cmd[GMT_BUFSIZ] = {""}, proj4_name[20] = {""}; char *gs_BB = NULL, *proj4_cmd = NULL; char *device[N_GS_DEVICES] = {"", "pdfwrite", "svg", "jpeg", "png16m", "ppmraw", "tiff24nc", "bmp16m", "pngalpha", "jpeggray", "pnggray", "tiffgray", "bmpgray"}; @@ -2767,19 +2766,14 @@ EXTERN_MSC int GMT_psconvert (void *V_API, int mode, void *args) { world_file[pos_ext] = '\0'; strcat (world_file, ".tiff"); - if (GMT->current.setting.verbose < GMT_MSG_WARNING) /* Shut up the gdal_translate (low level) verbosity */ - quiet = " -quiet"; - else - quiet = ""; - - sprintf (cmd, "gdal_translate -mo TIFFTAG_XRESOLUTION=%g -mo TIFFTAG_YRESOLUTION=%g -a_srs %c%s%c " - "-co COMPRESS=LZW -co TILED=YES %s %c%s%c %c%s%c", - Ctrl->E.dpi, Ctrl->E.dpi, quote, proj4_cmd, quote, quiet, quote, out_file, quote, quote, world_file, quote); + sprintf (cmd, "%c%s%c -Atranslate -M -G%c%s%c -F\"-mo TIFFTAG_XRESOLUTION=%g -mo TIFFTAG_YRESOLUTION=%g -a_srs %c%s%c " + "-co COMPRESS=LZW -co TILED=YES\"", + quote, out_file, quote, quote, world_file, quote, Ctrl->E.dpi, Ctrl->E.dpi, quote, proj4_cmd, quote); gmt_M_str_free (proj4_cmd); - sys_retval = system (cmd); /* Execute the gdal_translate command */ - GMT_Report (API, GMT_MSG_INFORMATION, "The gdal_translate command: \n%s\n", cmd); - if (sys_retval) { - GMT_Report (API, GMT_MSG_ERROR, "System call [%s] returned error %d.\n", cmd, sys_retval); + GMT_Report (API, GMT_MSG_INFORMATION, "The grdgdal command: \n%s\n", cmd); + + if (GMT_Call_Module (API, "grdgdal", GMT_MODULE_CMD, cmd) != GMT_OK) { /* Failed to do the conversion */ + GMT_Report (API, GMT_MSG_ERROR, "Call to grdgdal [%s] returned error %d.\n", cmd, sys_retval); Return (GMT_RUNTIME_ERROR); } if (!Ctrl->T.active) /* Get rid of the intermediate JPG file if -T was not set */ From 3b430fe03546556b74cc823d1efd6e18b61260d9 Mon Sep 17 00:00:00 2001 From: Paul Wessel <pwessel@hawaii.edu> Date: Fri, 12 Nov 2021 07:04:24 -1000 Subject: [PATCH 3/3] Check for both single and double quotes --- src/gmt_gdalcall.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gmt_gdalcall.c b/src/gmt_gdalcall.c index 5d21a39ccfa..9974a29388f 100644 --- a/src/gmt_gdalcall.c +++ b/src/gmt_gdalcall.c @@ -126,7 +126,7 @@ GMT_LOCAL char ** breakMe(struct GMT_CTRL *GMT, char *in) { * these items by temporarily replacing spaces inside single quoted strings with ASCII 31 US (Unit Separator), do the strtok on * space, and then replace all ASCII 31 with space at the end (we do the same for tab using ASCII 29 GS (group separator) */ for (k = 0, quoted = false; txt_in[k]; k++) { - if (txt_in[k] == '\'') quoted = !quoted; /* Initially false, becomes true at start of quote, then false when exit quote */ + if (txt_in[k] == '\'' || txt_in[k] == '\"') quoted = !quoted; /* Initially false, becomes true at start of quote, then false when exit quote */ else if (quoted && txt_in[k] == '\t') txt_in[k] = GMT_ASCII_GS; else if (quoted && txt_in[k] == ' ') txt_in[k] = GMT_ASCII_US; } @@ -139,7 +139,7 @@ GMT_LOCAL char ** breakMe(struct GMT_CTRL *GMT, char *in) { p[k] = ' '; /* Replace spaces and tabs masked above */ } for (i = o = 0; p[i]; i++) - if (p[i] != '\'') p[o++] = p[i]; /* Ignore any single quotes */ + if (!(p[i] == '\'' || p[i] == '\"')) p[o++] = p[i]; /* Ignore any single or double quotes */ p[o] = '\0'; args[n_args++] = strdup(p);