source: git/src/survexport.cc @ 5605a4b

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

Use unsigned type to test export option bitmask

This makes the code more robust should we ever reach 32 export flags
(currently we have 20).

  • Property mode set to 100644
File size: 13.7 KB
Line 
1/* survexport.cc
2 * Convert a processed survey data file to another format.
3 */
4
5/* Copyright (C) 1994-2004,2008,2010,2011,2013,2014,2018,2020 Olly Betts
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
42#include <iostream>
43#include <string>
44
45using namespace std;
46
47int
48main(int argc, char **argv)
49{
50   double pan = 0;
51   double tilt = -90.0;
52   export_format format = FMT_MAX_PLUS_ONE_;
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;
59   SurveyFilter* filter = NULL;
60
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
70   const int OPT_FMT_BASE = 20000;
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,
75       OPT_CENTRED, OPT_FULL_COORDS, OPT_CLAMP_TO_GROUND, OPT_DEFAULTS
76   };
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'},
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},
85        {"legs", no_argument, 0, OPT_LEGS},
86        {"surface-legs", no_argument, 0, OPT_SURF},
87        {"splays", no_argument, 0, OPT_SPLAYS},
88        {"crosses", no_argument, 0, OPT_CROSSES},
89        {"station-names", no_argument, 0, OPT_LABELS},
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},
98        {"clamp-to-ground", no_argument, 0, OPT_CLAMP_TO_GROUND},
99        {"defaults", no_argument, 0, OPT_DEFAULTS},
100        {"grid", optional_argument, 0, 'g'},
101        {"text-height", required_argument, 0, 't'},
102        {"marker-size", required_argument, 0, 'm'},
103        {"csv", no_argument, 0, OPT_FMT_BASE + FMT_CSV},
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},
114        {"help", no_argument, 0, HLP_HELP},
115        {"version", no_argument, 0, HLP_VERSION},
116        // US spelling:
117        {"origin-in-center", no_argument, 0, OPT_CENTRED},
118        // Abbreviation:
119        {"full-coords", no_argument, 0, OPT_FULL_COORDS},
120        {0,0,0,0}
121   };
122
123#define short_opts "s:g::t:m:"
124
125   static struct help_msg help[] = {
126        /*                      <-- */
127        {HLP_ENCODELONG(0),   /*only load the sub-survey with this prefix*/199, 0},
128        /* TRANSLATORS: These example input values should not be translated. */
129        {HLP_ENCODELONG(1),   /*scale (50, 0.02, 1:50 and 2:100 all mean 1:50)*/217, 0},
130        /* TRANSLATORS: These example input values should not be translated. */
131        {HLP_ENCODELONG(2),   /*bearing (90, 90d, 100g all mean 90°)*/460, 0},
132        /* TRANSLATORS: These example input values should not be translated. */
133        {HLP_ENCODELONG(3),   /*tilt (45, 45d, 50g, 100% all mean 45°)*/461, 0},
134        /* TRANSLATORS: Don't translate example command line option --tilt=-90 */
135        {HLP_ENCODELONG(4),   /*plan view (equivalent to --tilt=-90)*/462, 0},
136        /* TRANSLATORS: Don't translate example command line option --tilt=0 */
137        {HLP_ENCODELONG(5),   /*elevation view (equivalent to --tilt=0)*/463, 0},
138        {HLP_ENCODELONG(6),   /*underground survey legs*/476, 0},
139        {HLP_ENCODELONG(7),   /*surface survey legs*/464, 0},
140        {HLP_ENCODELONG(8),   /*splay legs*/465, 0},
141        {HLP_ENCODELONG(9),   /*station markers*/474, 0},
142        {HLP_ENCODELONG(10),  /*station labels*/475, 0},
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},
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)},
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},
161        {HLP_ENCODELONG(29),  /*produce JSON output*/457, 0},
162        {HLP_ENCODELONG(30),  /*produce KML output*/458, 0},
163        /* TRANSLATORS: "Compass" and "Carto" are the names of software packages,
164         * so should not be translated. */
165        {HLP_ENCODELONG(31),  /*produce Compass PLT output for Carto*/159, 0},
166        /* TRANSLATORS: "Skencil" is the name of a software package, so should not be
167         * translated. */
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},
171        {0, 0, 0}
172   };
173
174   msg_init(argv);
175
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);
181   while (1) {
182      long_index = -1;
183      int opt = cmdline_getopt();
184      if (opt == EOF) break;
185      int bit = 0;
186      switch (opt) {
187       case OPT_LEGS:
188         bit = LEGS;
189         break;
190       case OPT_SURF:
191         bit = SURF;
192         break;
193       case OPT_SPLAYS:
194         bit = SPLAYS;
195         break;
196       case OPT_CROSSES:
197         bit = STNS;
198         break;
199       case OPT_LABELS:
200         bit = LABELS;
201         break;
202       case OPT_ENTS:
203         bit = ENTS;
204         break;
205       case OPT_FIXES:
206         bit = FIXES;
207         break;
208       case OPT_EXPORTS:
209         bit = EXPORTS;
210         break;
211       case OPT_XSECT:
212         bit = XSECT;
213         break;
214       case OPT_WALLS:
215         bit = WALLS;
216         break;
217       case OPT_PASG:
218         bit = PASG;
219         break;
220       case OPT_CENTRED:
221         bit = CENTRED;
222         break;
223       case OPT_FULL_COORDS:
224         bit = FULL_COORDS;
225         break;
226       case OPT_CLAMP_TO_GROUND:
227         bit = CLAMP_TO_GROUND;
228         break;
229       case OPT_DEFAULTS:
230         always_include_defaults = true;
231         break;
232       case 'g': /* Grid */
233         if (optarg) {
234            grid = cmdline_double_arg();
235         } else {
236            grid = (double)DEFAULT_GRID_SPACING;
237         }
238         bit = GRID;
239         break;
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();
253             optarg -= 2;
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;
260             *colon = ':';
261         }
262         bit = SCALE;
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             }
277             pan = cmdline_double_arg();
278             optarg[len - 1] = ch;
279         } else {
280             pan = cmdline_double_arg();
281         }
282         if (units == 'g') {
283             pan *= 0.9;
284         }
285         bit = ORIENTABLE;
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             }
301             tilt = cmdline_double_arg();
302             optarg[len - 1] = ch;
303         } else {
304             tilt = cmdline_double_arg();
305         }
306         if (units == 'g') {
307             tilt *= 0.9;
308         } else if (units == '%') {
309             tilt = deg(atan(tilt * 0.01));
310         }
311         bit = ORIENTABLE;
312         break;
313       }
314       case OPT_PLAN:
315         tilt = -90.0;
316         bit = ORIENTABLE;
317         break;
318       case OPT_ELEV:
319         tilt = 0.0;
320         bit = ORIENTABLE;
321         break;
322       case 't': /* Text height */
323         text_height = cmdline_double_arg();
324         bit = TEXT_HEIGHT;
325         break;
326       case 'm': /* Marker size */
327         marker_size = cmdline_double_arg();
328         bit = MARKER_SIZE;
329         break;
330       case 's':
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         }
340         break;
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         }
345      }
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      }
376   }
377
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
382   const char* fnm_in = argv[optind++];
383   const char* fnm_out = argv[optind];
384   if (fnm_out) {
385      if (format == FMT_MAX_PLUS_ONE_) {
386         // Select format based on extension.
387         size_t len = strlen(fnm_out);
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);
391            if (len > l + 1 &&
392                strcasecmp(fnm_out + len - l, info.extension) == 0) {
393               format = export_format(i);
394               break;
395            }
396         }
397         if (format == FMT_MAX_PLUS_ONE_) {
398            fatalerror(/*Export format not specified and not known from output file extension*/252);
399         }
400      }
401   } else {
402      if (format == FMT_MAX_PLUS_ONE_) {
403         fatalerror(/*Export format not specified*/253);
404      }
405      char *baseleaf = baseleaf_from_fnm(fnm_in);
406      /* note : memory allocated by fnm_out gets leaked in this case... */
407      fnm_out = add_ext(baseleaf, export_format_info[format].extension);
408      osfree(baseleaf);
409   }
410
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       unsigned 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
435   if (!(format_info_mask & ORIENTABLE)) {
436       pan = 0.0;
437       tilt = -90.0;
438   }
439
440   Model model;
441   int err = model.Load(fnm_in, survey);
442   if (err) fatalerror(err, fnm_in);
443   if (filter) filter->SetSeparator(model.GetSeparator());
444
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) {
455       wxString r = msg_appname();
456       r += ": ";
457       r += wmsg(/*error*/93);
458       r += ": ";
459       r += m;
460       wcerr << r.c_str() << '\n';
461   }
462
463   return 0;
464}
Note: See TracBrowser for help on using the repository browser.