source: git/src/survexport.cc @ 43e8c72

RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since 43e8c72 was 40623c5, checked in by Olly Betts <olly@…>, 5 years ago

survexport: Fix check for --elevation etc

The check for whether a format supported --elevation, --plan, --bearing
and --tilt was inverted, because the internal EXPORT_3D flag in aven's
export code has the opposite sense to what we need here.

Fix by replacing with a ORIENTABLE flag, which the opposite meaning,
which seems clearer in the aven code too.

Reported by echarlie.

  • Property mode set to 100644
File size: 13.7 KB
RevLine 
[79b32a95]1/* survexport.cc
2 * Convert a processed survey data file to another format.
[bd30612]3 */
4
[a8d2349]5/* Copyright (C) 1994-2004,2008,2010,2011,2013,2014,2018,2020 Olly Betts
[bd30612]6 * Copyright (C) 2004 John Pybus (SVG Output code)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 */
22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
27#include <math.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31
32#include "export.h"
33#include "mainfrm.h"
34
35#include "cmdline.h"
36#include "filename.h"
37#include "img_hosted.h"
38#include "message.h"
39#include "str.h"
40#include "useful.h"
41
[a8d2349]42#include <iostream>
[f7fb2ec]43#include <string>
44
45using namespace std;
46
[bd30612]47int
48main(int argc, char **argv)
49{
50   double pan = 0;
51   double tilt = -90.0;
[4d3c8915]52   export_format format = FMT_MAX_PLUS_ONE_;
[bd30612]53   int show_mask = 0;
54   const char *survey = NULL;
55   double grid = 0.0; /* grid spacing (or 0 for no grid) */
56   double text_height = DEFAULT_TEXT_HEIGHT; /* for station labels */
57   double marker_size = DEFAULT_MARKER_SIZE; /* for station markers */
58   double scale = 500.0;
[1a46879]59   SurveyFilter* filter = NULL;
[bd30612]60
[b349e7e2]61   {
62       /* Default to .pos output if installed as 3dtopos. */
63       char* progname = baseleaf_from_fnm(argv[0]);
64       if (strcasecmp(progname, "3dtopos") == 0) {
65           format = FMT_POS;
66       }
67       osfree(progname);
68   }
69
[4d3c8915]70   const int OPT_FMT_BASE = 20000;
[02ba4c9]71   enum {
72       OPT_SCALE = 0x100, OPT_BEARING, OPT_TILT, OPT_PLAN, OPT_ELEV,
73       OPT_LEGS, OPT_SURF, OPT_SPLAYS, OPT_CROSSES, OPT_LABELS, OPT_ENTS,
74       OPT_FIXES, OPT_EXPORTS, OPT_XSECT, OPT_WALLS, OPT_PASG,
[32a040e]75       OPT_CENTRED, OPT_FULL_COORDS, OPT_CLAMP_TO_GROUND, OPT_DEFAULTS
[02ba4c9]76   };
[bd30612]77   static const struct option long_opts[] = {
78        /* const char *name; int has_arg (0 no_argument, 1 required, 2 options_*); int *flag; int val */
79        {"survey", required_argument, 0, 's'},
[02ba4c9]80        {"scale", required_argument, 0, OPT_SCALE},
81        {"bearing", required_argument, 0, OPT_BEARING},
82        {"tilt", required_argument, 0, OPT_TILT},
83        {"plan", no_argument, 0, OPT_PLAN},
84        {"elevation", no_argument, 0, OPT_ELEV},
[f7fb2ec]85        {"legs", no_argument, 0, OPT_LEGS},
[6e89e5d]86        {"surface-legs", no_argument, 0, OPT_SURF},
[02ba4c9]87        {"splays", no_argument, 0, OPT_SPLAYS},
[f7fb2ec]88        {"crosses", no_argument, 0, OPT_CROSSES},
89        {"station-names", no_argument, 0, OPT_LABELS},
[02ba4c9]90        {"entrances", no_argument, 0, OPT_ENTS},
91        {"fixes", no_argument, 0, OPT_FIXES},
92        {"exports", no_argument, 0, OPT_EXPORTS},
93        {"cross-sections", no_argument, 0, OPT_XSECT},
94        {"walls", no_argument, 0, OPT_WALLS},
95        {"passages", no_argument, 0, OPT_PASG},
96        {"origin-in-centre", no_argument, 0, OPT_CENTRED},
97        {"full-coordinates", no_argument, 0, OPT_FULL_COORDS},
[32a040e]98        {"clamp-to-ground", no_argument, 0, OPT_CLAMP_TO_GROUND},
[f7fb2ec]99        {"defaults", no_argument, 0, OPT_DEFAULTS},
[bd30612]100        {"grid", optional_argument, 0, 'g'},
101        {"text-height", required_argument, 0, 't'},
102        {"marker-size", required_argument, 0, 'm'},
[df121c2d]103        {"csv", no_argument, 0, OPT_FMT_BASE + FMT_CSV},
[4d3c8915]104        {"dxf", no_argument, 0, OPT_FMT_BASE + FMT_DXF},
105        {"eps", no_argument, 0, OPT_FMT_BASE + FMT_EPS},
106        {"gpx", no_argument, 0, OPT_FMT_BASE + FMT_GPX},
107        {"hpgl", no_argument, 0, OPT_FMT_BASE + FMT_HPGL},
108        {"json", no_argument, 0, OPT_FMT_BASE + FMT_JSON},
109        {"kml", no_argument, 0, OPT_FMT_BASE + FMT_KML},
110        {"plt", no_argument, 0, OPT_FMT_BASE + FMT_PLT},
111        {"skencil", no_argument, 0, OPT_FMT_BASE + FMT_SK},
112        {"pos", no_argument, 0, OPT_FMT_BASE + FMT_POS},
113        {"svg", no_argument, 0, OPT_FMT_BASE + FMT_SVG},
[bd30612]114        {"help", no_argument, 0, HLP_HELP},
115        {"version", no_argument, 0, HLP_VERSION},
[02ba4c9]116        // US spelling:
117        {"origin-in-center", no_argument, 0, OPT_CENTRED},
118        // Abbreviation:
119        {"full-coords", no_argument, 0, OPT_FULL_COORDS},
[bd30612]120        {0,0,0,0}
121   };
122
[02ba4c9]123#define short_opts "s:g::t:m:"
[bd30612]124
125   static struct help_msg help[] = {
126        /*                      <-- */
127        {HLP_ENCODELONG(0),   /*only load the sub-survey with this prefix*/199, 0},
[c0e4f18]128        /* TRANSLATORS: These example input values should not be translated. */
[02ba4c9]129        {HLP_ENCODELONG(1),   /*scale (50, 0.02, 1:50 and 2:100 all mean 1:50)*/217, 0},
[c0e4f18]130        /* TRANSLATORS: These example input values should not be translated. */
[02ba4c9]131        {HLP_ENCODELONG(2),   /*bearing (90, 90d, 100g all mean 90°)*/460, 0},
[c0e4f18]132        /* TRANSLATORS: These example input values should not be translated. */
[02ba4c9]133        {HLP_ENCODELONG(3),   /*tilt (45, 45d, 50g, 100% all mean 45°)*/461, 0},
[c0e4f18]134        /* TRANSLATORS: Don't translate example command line option --tilt=-90 */
[02ba4c9]135        {HLP_ENCODELONG(4),   /*plan view (equivalent to --tilt=-90)*/462, 0},
[c0e4f18]136        /* TRANSLATORS: Don't translate example command line option --tilt=0 */
[02ba4c9]137        {HLP_ENCODELONG(5),   /*elevation view (equivalent to --tilt=0)*/463, 0},
[f7fb2ec]138        {HLP_ENCODELONG(6),   /*underground survey legs*/476, 0},
[02ba4c9]139        {HLP_ENCODELONG(7),   /*surface survey legs*/464, 0},
140        {HLP_ENCODELONG(8),   /*splay legs*/465, 0},
[f7fb2ec]141        {HLP_ENCODELONG(9),   /*station markers*/474, 0},
142        {HLP_ENCODELONG(10),  /*station labels*/475, 0},
[02ba4c9]143        {HLP_ENCODELONG(11),  /*entrances*/466, 0},
144        {HLP_ENCODELONG(12),  /*fixed points*/467, 0},
145        {HLP_ENCODELONG(13),  /*exported stations*/468, 0},
146        {HLP_ENCODELONG(14),  /*cross-sections*/469, 0},
147        {HLP_ENCODELONG(15),  /*walls*/470, 0},
148        {HLP_ENCODELONG(16),  /*passages*/471, 0},
149        {HLP_ENCODELONG(17),  /*origin in centre*/472, 0},
150        {HLP_ENCODELONG(18),  /*full coordinates*/473, 0},
[32a040e]151        {HLP_ENCODELONG(19),  /*clamp to ground*/478, 0},
152        {HLP_ENCODELONG(20),  /*include items exported by default*/155, 0},
153        {HLP_ENCODELONG(21),  /*generate grid (default %sm)*/148, STRING(DEFAULT_GRID_SPACING)},
154        {HLP_ENCODELONG(22),  /*station labels text height (default %s)*/149, STRING(DEFAULT_TEXT_HEIGHT)},
155        {HLP_ENCODELONG(23),  /*station marker size (default %s)*/152, STRING(DEFAULT_MARKER_SIZE)},
[df121c2d]156        {HLP_ENCODELONG(24),  /*produce CSV output*/102, 0},
157        {HLP_ENCODELONG(25),  /*produce DXF output*/156, 0},
158        {HLP_ENCODELONG(26),  /*produce EPS output*/454, 0},
159        {HLP_ENCODELONG(27),  /*produce GPX output*/455, 0},
160        {HLP_ENCODELONG(28),  /*produce HPGL output*/456, 0},
[32a040e]161        {HLP_ENCODELONG(29),  /*produce JSON output*/457, 0},
[df121c2d]162        {HLP_ENCODELONG(30),  /*produce KML output*/458, 0},
[bd30612]163        /* TRANSLATORS: "Compass" and "Carto" are the names of software packages,
164         * so should not be translated. */
[df121c2d]165        {HLP_ENCODELONG(31),  /*produce Compass PLT output for Carto*/159, 0},
[4d3c8915]166        /* TRANSLATORS: "Skencil" is the name of a software package, so should not be
167         * translated. */
[df121c2d]168        {HLP_ENCODELONG(32),  /*produce Skencil output*/158, 0},
169        {HLP_ENCODELONG(33),  /*produce Survex POS output*/459, 0},
170        {HLP_ENCODELONG(34),  /*produce SVG output*/160, 0},
[bd30612]171        {0, 0, 0}
172   };
173
174   msg_init(argv);
175
[f7fb2ec]176   string optmap[sizeof(show_mask) * CHAR_BIT];
177
178   int long_index;
179   bool always_include_defaults = false;
180   cmdline_init(argc, argv, short_opts, long_opts, &long_index, help, 1, 2);
[bd30612]181   while (1) {
[f7fb2ec]182      long_index = -1;
[bd30612]183      int opt = cmdline_getopt();
184      if (opt == EOF) break;
[f7fb2ec]185      int bit = 0;
[bd30612]186      switch (opt) {
[02ba4c9]187       case OPT_LEGS:
[f7fb2ec]188         bit = LEGS;
[bd30612]189         break;
[02ba4c9]190       case OPT_SURF:
[f7fb2ec]191         bit = SURF;
[02ba4c9]192         break;
193       case OPT_SPLAYS:
[f7fb2ec]194         bit = SPLAYS;
[02ba4c9]195         break;
196       case OPT_CROSSES:
[f7fb2ec]197         bit = STNS;
[bd30612]198         break;
[02ba4c9]199       case OPT_LABELS:
[f7fb2ec]200         bit = LABELS;
[bd30612]201         break;
[02ba4c9]202       case OPT_ENTS:
[f7fb2ec]203         bit = ENTS;
[02ba4c9]204         break;
205       case OPT_FIXES:
[f7fb2ec]206         bit = FIXES;
[02ba4c9]207         break;
208       case OPT_EXPORTS:
[f7fb2ec]209         bit = EXPORTS;
[02ba4c9]210         break;
211       case OPT_XSECT:
[f7fb2ec]212         bit = XSECT;
[02ba4c9]213         break;
214       case OPT_WALLS:
[f7fb2ec]215         bit = WALLS;
[02ba4c9]216         break;
217       case OPT_PASG:
[f7fb2ec]218         bit = PASG;
[02ba4c9]219         break;
220       case OPT_CENTRED:
[f7fb2ec]221         bit = CENTRED;
[02ba4c9]222         break;
223       case OPT_FULL_COORDS:
[f7fb2ec]224         bit = FULL_COORDS;
225         break;
[32a040e]226       case OPT_CLAMP_TO_GROUND:
227         bit = CLAMP_TO_GROUND;
228         break;
[f7fb2ec]229       case OPT_DEFAULTS:
230         always_include_defaults = true;
[bd30612]231         break;
232       case 'g': /* Grid */
233         if (optarg) {
234            grid = cmdline_double_arg();
235         } else {
236            grid = (double)DEFAULT_GRID_SPACING;
237         }
[f7fb2ec]238         bit = GRID;
[bd30612]239         break;
[02ba4c9]240       case OPT_SCALE: {
241         char* colon = strchr(optarg, ':');
242         if (!colon) {
243             /* --scale=1000 => 1:1000 => scale = 1000 */
244             scale = cmdline_double_arg();
245             if (scale < 1.0) {
246                 /* --scale=0.001 => 1:1000 => scale = 1000 */
247                 scale = 1.0 / scale;
248             }
249         } else if (colon - optarg == 1 && optarg[0] == '1') {
250             /* --scale=1:1000 => 1:1000 => scale = 1000 */
251             optarg += 2;
252             scale = cmdline_double_arg();
[f7fb2ec]253             optarg -= 2;
[02ba4c9]254         } else {
255             /* --scale=2:1000 => 1:500 => scale = 500 */
256             *colon = '\0';
257             scale = cmdline_double_arg();
258             optarg = colon + 1;
259             scale = cmdline_double_arg() / scale;
[f7fb2ec]260             *colon = ':';
[02ba4c9]261         }
[f7fb2ec]262         bit = SCALE;
[02ba4c9]263         break;
264       }
265       case OPT_BEARING: {
266         int units = 0;
267         size_t len = strlen(optarg);
268         if (len > 0) {
269             char ch = optarg[len - 1];
270             switch (ch) {
271                 case 'd':
272                 case 'g':
273                     units = ch;
274                     optarg[len - 1] = '\0';
275                     break;
276             }
[f7fb2ec]277             pan = cmdline_double_arg();
278             optarg[len - 1] = ch;
279         } else {
280             pan = cmdline_double_arg();
[02ba4c9]281         }
282         if (units == 'g') {
283             pan *= 0.9;
284         }
[40623c5]285         bit = ORIENTABLE;
[02ba4c9]286         break;
287       }
288       case OPT_TILT: {
289         int units = 0;
290         size_t len = strlen(optarg);
291         if (len > 0) {
292             char ch = optarg[len - 1];
293             switch (ch) {
294                 case '%':
295                 case 'd':
296                 case 'g':
297                     units = ch;
298                     optarg[len - 1] = '\0';
299                     break;
300             }
[f7fb2ec]301             tilt = cmdline_double_arg();
302             optarg[len - 1] = ch;
303         } else {
304             tilt = cmdline_double_arg();
[02ba4c9]305         }
306         if (units == 'g') {
307             tilt *= 0.9;
308         } else if (units == '%') {
309             tilt = deg(atan(tilt * 0.01));
310         }
[40623c5]311         bit = ORIENTABLE;
[02ba4c9]312         break;
313       }
314       case OPT_PLAN:
315         tilt = -90.0;
[40623c5]316         bit = ORIENTABLE;
[02ba4c9]317         break;
318       case OPT_ELEV:
319         tilt = 0.0;
[40623c5]320         bit = ORIENTABLE;
[bd30612]321         break;
322       case 't': /* Text height */
323         text_height = cmdline_double_arg();
[f7fb2ec]324         bit = TEXT_HEIGHT;
[bd30612]325         break;
326       case 'm': /* Marker size */
327         marker_size = cmdline_double_arg();
[f7fb2ec]328         bit = MARKER_SIZE;
[bd30612]329         break;
330       case 's':
[1a46879]331         if (survey) {
332             if (!filter) {
333                 filter = new SurveyFilter();
334                 filter->add(survey);
335             }
336             filter->add(optarg);
337         } else {
338             survey = optarg;
339         }
[bd30612]340         break;
[4d3c8915]341       default:
342         if (opt >= OPT_FMT_BASE && opt < OPT_FMT_BASE + FMT_MAX_PLUS_ONE_) {
343             format = export_format(opt - OPT_FMT_BASE);
344         }
[bd30612]345      }
[f7fb2ec]346      if (bit) {
347          show_mask |= bit;
348          int i = 0;
349          while (((bit >> i) & 1) == 0) ++i;
350
351          if (!optmap[i].empty()) optmap[i] += ' ';
352
353          // Reconstruct what the command line option was.
354          if (long_index < 0) {
355              optmap[i] += '-';
356              optmap[i] += char(opt);
357              if (optarg) {
358                  if (optarg == argv[optind - 1]) {
359                      optmap[i] += ' ';
360                  }
361                  optmap[i] += optarg;
362              }
363          } else {
364              optmap[i] += "--";
365              optmap[i] += long_opts[long_index].name;
366              if (optarg) {
367                  if (optarg == argv[optind - 1]) {
368                      optmap[i] += ' ';
369                  } else {
370                      optmap[i] += '=';
371                  }
372                  optmap[i] += optarg;
373              }
374          }
375      }
[bd30612]376   }
377
[1a46879]378   // A single --survey is handled by img at load-time.  Multiple --survey are
379   // handled via a SurveyFilter at export time.
380   if (filter) survey = NULL;
381
[bd30612]382   const char* fnm_in = argv[optind++];
383   const char* fnm_out = argv[optind];
384   if (fnm_out) {
[4d3c8915]385      if (format == FMT_MAX_PLUS_ONE_) {
386         // Select format based on extension.
[bd30612]387         size_t len = strlen(fnm_out);
[4d3c8915]388         for (size_t i = 0; i < FMT_MAX_PLUS_ONE_; ++i) {
389            const auto& info = export_format_info[i];
390            size_t l = strlen(info.extension);
[bd30612]391            if (len > l + 1 &&
[4d3c8915]392                strcasecmp(fnm_out + len - l, info.extension) == 0) {
[bd30612]393               format = export_format(i);
394               break;
395            }
396         }
[4d3c8915]397         if (format == FMT_MAX_PLUS_ONE_) {
398            fatalerror(/*Export format not specified and not known from output file extension*/252);
399         }
[bd30612]400      }
401   } else {
[4d3c8915]402      if (format == FMT_MAX_PLUS_ONE_) {
403         fatalerror(/*Export format not specified*/253);
404      }
[bd30612]405      char *baseleaf = baseleaf_from_fnm(fnm_in);
406      /* note : memory allocated by fnm_out gets leaked in this case... */
[4d3c8915]407      fnm_out = add_ext(baseleaf, export_format_info[format].extension);
[bd30612]408      osfree(baseleaf);
409   }
410
[f7fb2ec]411   const auto& format_info_mask = export_format_info[format].mask;
412   unsigned not_allowed = show_mask &~ format_info_mask;
413   if (not_allowed) {
414       printf("warning: The following options are not supported for this export format and will be ignored:\n");
415       int i = 0;
416       int bit = 1;
417       while (not_allowed) {
418           if (not_allowed & bit) {
419               // E.g. --walls maps to two bits in show_mask, but the options
420               // are only put on the least significant in such cases.
421               if (!optmap[i].empty())
422                   printf("%s\n", optmap[i].c_str());
423               not_allowed &= ~bit;
424           }
425           ++i;
426           bit <<= 1;
427       }
428       show_mask &= format_info_mask;
429   }
430
431   if (always_include_defaults || show_mask == 0) {
432       show_mask |= export_format_info[format].defaults;
433   }
434
[40623c5]435   if (!(format_info_mask & ORIENTABLE)) {
[f7fb2ec]436       pan = 0.0;
437       tilt = -90.0;
438   }
439
[bd30612]440   Model model;
441   int err = model.Load(fnm_in, survey);
442   if (err) fatalerror(err, fnm_in);
[1a46879]443   if (filter) filter->SetSeparator(model.GetSeparator());
[bd30612]444
[2907665]445   try {
446       if (!Export(fnm_out, model.GetSurveyTitle(),
447                   model.GetDateString(),
448                   model, filter,
449                   pan, tilt, show_mask, format,
450                   grid, text_height, marker_size,
451                   scale)) {
452          fatalerror(/*Couldn’t write file “%s”*/402, fnm_out);
453       }
454   } catch (const wxString & m) {
[a8d2349]455       wxString r = msg_appname();
456       r += ": ";
457       r += wmsg(/*error*/93);
458       r += ": ";
459       r += m;
460       wcerr << r.c_str() << '\n';
[bd30612]461   }
[2907665]462
[bd30612]463   return 0;
464}
Note: See TracBrowser for help on using the repository browser.