-
Notifications
You must be signed in to change notification settings - Fork 9
/
xgk-slo.cxx
1864 lines (1607 loc) · 61.1 KB
/
xgk-slo.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// GK - Converter between Gauss-Krueger/TM and WGS84 coordinates for Slovenia
// Copyright (c) 2014-2019 Matjaz Rihtar <[email protected]>
// All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, see http://www.gnu.org/licenses/
//
// xgk-slo.cxx: Main GUI program for converting coordinates from XYZ files
//
#include "common.h"
#include "geo.h"
#include "deelx.h" // regular expression engine in C++ from www.regexlab.com
#include <pthread.h>
#ifdef _MSC_VER // Microsoft C
#define OLD_PTHREADS
#endif
#include <FL/Fl.H>
#include <FL/x.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Sys_Menu_Bar.H>
#include <FL/Fl_Radio_Round_Button.H>
#include <FL/Fl_Check_Button.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Tabs.H>
#include <FL/Fl_Choice.H>
#include <FL/Fl_Browser.H>
#include <FL/Fl_Output.H>
#include <FL/Fl_Text_Display.H>
#include <FL/Fl_Help_Dialog.H>
#include <FL/Fl_Native_File_Chooser.H>
#include <FL/fl_ask.H>
#include <FL/fl_draw.H>
#ifdef _WIN32
#include <Shellapi.h>
#else
#include <X11/xpm.h>
#include "globe.xpm"
#endif
#define SW_VERSION "1.39"
#define SW_BUILD "Jan 23, 2019"
#define HELP "xgk-help.html"
// See https://en.wikipedia.org/wiki/Wikipedia:WikiProject_Geographical_coordinates#Parameters
#define SCALE 5000
#ifdef __cplusplus
extern "C" {
#endif
// global variables
char *prog; // program name
int debug;
int tr; // transformation
int rev; // reverse xy/fila
int wdms; // write DMS
extern int gid_wgs; // selected geoid on WGS 84 (in geo.c)
extern int hsel; // output height calculation (in geo.c)
int ft; // file type (XYZ/SHP)
int ddms; // display DMS
char argv0[MAXS+1]; // original argv[0]
char path0[MAXS+1]; // path of argv[0], len > PATH_MAX
#ifdef __cplusplus
}
#endif
// thread variables
typedef struct ptid {
// WIN32, OLD_PTHREADS { void *p; unsigned int x; }, UNIX: unsigned long
pthread_t tid;
int done;
int sts;
struct ptid *next;
} PTID;
PTID *threads, *threads0;
pthread_attr_t pattr;
pthread_mutex_t xlog_mutex; // xlog mutex
#define MAXTN 25 // maximum number of allowed active threads
pthread_mutex_t tn_mutex; // threads number mutex
int tn; // number of active treads
// thread arguments
typedef struct targ {
char text[MAXL+1];
Fl_Widget *w;
} TARG;
const char *browsers[] = { // Possible UNIX browsers
"chrome", "firefox", "opera", "epiphany", "mozilla",
"netscape", "konqueror", "nautilus", "safari"
};
#ifdef __cplusplus
extern "C" {
#endif
// External function prototypes
int convert_xyz_file(char *url, int outf, FILE *out, char *msg); // in conv.c
int convert_shp_file(TCHAR *inpurl, TCHAR *outurl, TCHAR *msg); // in conv.c
#ifdef __cplusplus
}
#endif
// FLTK callback function prototypes
void open_cb(Fl_Widget *w, void *p);
void quit_cb(Fl_Widget *w, void *p);
void help_cb(Fl_Widget *w, void *p);
void clear_cb(Fl_Widget *w, void *p);
void about_cb(Fl_Widget *w, void *p);
void ftchoice_cb(Fl_Widget *w, void *p);
// FLTK global variables
Fl_Menu_Item menubar_entries[] = {
{"&File", 0, 0, 0, FL_SUBMENU},
{"&Open", FL_ALT+'o', open_cb, 0, FL_MENU_DIVIDER},
{"&Quit", FL_ALT+'q', quit_cb},
{0},
{"&Edit", 0, 0, 0, FL_SUBMENU},
{"Undo", FL_CTRL+'z', 0},
{"Redo", FL_CTRL+'r', 0, 0, FL_MENU_DIVIDER},
{"Cut", FL_CTRL+'x', 0},
{"Copy", FL_CTRL+'c', 0},
{"Paste", FL_CTRL+'v', 0, 0, FL_MENU_DIVIDER},
{"Clear", 0, clear_cb},
{0},
{"&Help", 0, 0, 0, FL_SUBMENU},
{"Help", FL_ALT+'h', help_cb, 0, FL_MENU_DIVIDER},
{"About", 0, about_cb},
{0},
{0}
}; /* menubar_entries */
Fl_Radio_Round_Button *rb_trans[MAXC];
Fl_Radio_Round_Button *rb_geoid[MAXC];
Fl_Radio_Round_Button *rb_height[MAXC];
Fl_Menu_Item ft_choices[] = {
{"XYZ files", 0, ftchoice_cb, (void *)1},
{"SHP files", 0, ftchoice_cb, (void *)2},
{0}
}; /* ft_choices */
Fl_Group *tab1, *tab2;
Fl_Browser *brow;
Fl_Input *input[MAXC];
Fl_Output *output[MAXC];
Fl_Radio_Round_Button *rb_dms[MAXC];
Fl_Group *gtr1, *gtr2, *gtr3;
enum Fld { gtr1_XY, gtr1_X, gtr1_Y, gtr1_H, gtr1_FILA, gtr1_FI, gtr1_LA, gtr1_h,
gtr2_FILA, gtr2_FI, gtr2_LA, gtr2_h, gtr2_XY, gtr2_X, gtr2_Y, gtr2_H,
gtr3_XYi, gtr3_Xi, gtr3_Yi, gtr3_Hi, gtr3_XYo, gtr3_Xo, gtr3_Yo, gtr3_Ho };
// ----------------------------------------------------------------------------
// Fl_DND_Box
// ----------------------------------------------------------------------------
class Fl_DND_Box : public Fl_Box
{
public:
Fl_DND_Box(int X, int Y, int W, int H, const char *L = 0) :
Fl_Box(X,Y,W,H,L), evt(FL_NO_EVENT), evt_txt(0), evt_len(0)
{
labeltype(FL_NO_LABEL);
box(FL_NO_BOX);
clear_visible_focus();
} /* Fl_DND_Box ctor */
virtual ~Fl_DND_Box() {
delete [] evt_txt;
} /* Fl_DND_Box dtor */
int event() {
return evt;
} /* event */
const char *event_text() {
return evt_txt;
} /* event_text */
int event_length() {
return evt_len;
} /* event_length */
static void callback_deferred(void *w) {
Fl_DND_Box *dnd = (Fl_DND_Box *)w;
dnd->do_callback();
} /* callback_deferred */
int handle(int e) {
switch (e) {
// return 1 for these events to 'accept' DND
case FL_DND_ENTER:
case FL_DND_RELEASE:
case FL_DND_LEAVE:
case FL_DND_DRAG:
evt = e;
return 1;
// handle actual drop (paste) operation
case FL_PASTE:
evt = e;
// make a copy of the DND payload
evt_len = Fl::event_length();
delete [] evt_txt;
evt_txt = new char[evt_len+1];
xstrncpy(evt_txt, Fl::event_text(), evt_len);
// If there is a callback registered, call it, but not directly.
// The callback will be executed by the FLTK main-loop once we have
// finished handling the DND event. This allows caller to popup
// a window or change widget focus.
if (callback() && ((when() & FL_WHEN_RELEASE) || (when() & FL_WHEN_CHANGED)))
Fl::add_timeout(0.0, Fl_DND_Box::callback_deferred, (void *)this);
return 1;
} // switch
// pass other events to default event handling of Fl_Box
return Fl_Box::handle(e);
} /* handle */
protected:
int evt; // The event which caused Fl_DND_Box to execute its callback
char *evt_txt;
int evt_len;
}; /* Fl_DND_Box */
// ----------------------------------------------------------------------------
// Fl_Arrow_Box
// ----------------------------------------------------------------------------
class Fl_Arrow_Box : public Fl_Box
{
public:
Fl_Arrow_Box(int X, int Y, int W, int H, const char *L = 0) :
Fl_Box(X,Y,W,H,L)
{
labeltype(FL_NO_LABEL);
box(FL_NO_BOX);
clear_visible_focus();
} /* Fl_Arrow_Box ctor */
protected:
void draw() {
Fl_Color savec = fl_color();
Fl_Box::draw();
fl_color(FL_BLUE);
fl_line_style(FL_SOLID | FL_CAP_ROUND | FL_JOIN_ROUND, 3);
fl_line(x()+10, y()+h()/2-1, x()+w()-10, y()+h()/2-1);
fl_line(x()+w()-20, y()+h()/2-7, x()+w()-10, y()+h()/2-1);
fl_line(x()+w()-20, y()+h()/2+5, x()+w()-10, y()+h()/2-1);
fl_line_style(0);
fl_color(savec);
} /* draw */
}; /* Fl_Arrow_Box */
// ----------------------------------------------------------------------------
// xlog
// ----------------------------------------------------------------------------
int xlog(const char *fmt, ...)
{
va_list ap;
time_t now;
struct tm *tmnow;
char stime[MAXS+1];
char *msg;
char logfile[MAXS+1];
FILE *log;
pthread_mutex_lock(&xlog_mutex);
now = time(NULL);
tmnow = localtime(&now);
//strftime(stime, MAXS, "%y-%m-%d %H:%M:%S", tmnow);
strftime(stime, MAXS, "%H:%M:%S", tmnow);
msg = new char[MAXL+1];
va_start(ap, fmt);
vsnprintf(msg, MAXL, fmt, ap);
va_end(ap);
snprintf(logfile, MAXS, "%s.log", prog);
log = utf8_fopen(logfile, "a");
if (log != NULL) {
fprintf(log, "%s %s", stime, msg);
fclose(log);
}
delete msg;
pthread_mutex_unlock(&xlog_mutex);
return 0;
} /* xlog */
// ----------------------------------------------------------------------------
// xpthread_create
// ----------------------------------------------------------------------------
int xpthread_create(void *(*worker)(void *), void *arg)
{
PTID *pt;
int rc;
pt = new PTID;
if (threads == NULL) { threads = pt; threads0 = threads; }
else { threads->next = pt; threads = threads->next; }
threads->done = 0; threads->sts = -1;
threads->next = NULL;
pthread_mutex_lock(&tn_mutex);
rc = pthread_create(&threads->tid, &pattr, worker, arg);
tn++;
pthread_mutex_unlock(&tn_mutex);
if (rc)
if (rc == EAGAIN) // PTHREAD_THREADS_MAX = 2019
xlog("Thread creation failed, max. number of threads exceeded\n");
else xlog("Thread creation failed, rc = %d\n", rc);
else
#ifdef OLD_PTHREADS
xlog("Created thread %08x\n", threads->tid.p);
#else
xlog("Created thread %08x\n", threads->tid);
#endif
return rc;
} /* xpthread_create */
// ----------------------------------------------------------------------------
// THREAD: worker
// ----------------------------------------------------------------------------
void *worker(void *arg)
{
Fl_Menu_Item *mi;
time_t now;
mi = (Fl_Menu_Item *)arg;
#ifdef OLD_PTHREADS
xlog("THREAD %08x: worker: menu %s\n", pthread_self().p, mi->label());
#else
xlog("THREAD %08x: worker: menu %s\n", pthread_self(), mi->label());
#endif
now = time(NULL);
pthread_mutex_lock(&tn_mutex);
tn--;
pthread_mutex_unlock(&tn_mutex);
pthread_exit((void *)(intptr_t)now);
return NULL;
} /* worker */
// ----------------------------------------------------------------------------
// THREAD: convert
// ----------------------------------------------------------------------------
void *convert(void *arg)
{
TARG *targ;
char *url, orig_url[MAXS+1];
char line[MAXL+14], *linep;
char *msg, *msgp, *s;
int sts;
targ = (TARG *)arg;
url = targ->text;
#ifdef OLD_PTHREADS
xlog("THREAD %08x: convert: url %s\n", pthread_self().p, url);
#else
xlog("THREAD %08x: convert: url %s\n", pthread_self(), url);
#endif
linep = line;
snprintf(linep, MAXL+13, "Converting %s\n", url);
Fl::lock();
brow->add(linep); brow->bottomline(brow->size());
Fl::unlock();
Fl::awake((void *)NULL);
msg = new char[MAXL+1];
xstrncpy(orig_url, url, MAXS);
if (ft == 1) // XYZ files
sts = convert_xyz_file(url, 2, NULL, msg); // convert to separate files
else { // SHP files
if ((s = strrchr(url, '.')) != NULL) *s = '\0'; // clear current extension
xstrncat(url, ".shp", MAXL); // look for <name>.shp
sts = convert_shp_file(url, NULL, msg);
}
if (strlen(msg) > 0) {
xlog("%s", msg); // must use %s here!
Fl::lock();
s = xstrtok_r(msg, "\r\n", &msgp);
while (s != NULL) {
snprintf(linep, MAXS, "@C1%s", s); // write errors in red
brow->add(linep); brow->bottomline(brow->size());
s = xstrtok_r(NULL, "\r\n", &msgp);
}
Fl::unlock();
Fl::awake((void *)NULL);
}
if (sts == 0) {
if (strlen(msg) > 0) // warning: dark magenta
snprintf(linep, MAXS, "@C13Finished converting %s\n", orig_url);
else // all OK: dark green
snprintf(linep, MAXS, "@C10Finished converting %s\n", orig_url);
}
else // error: dark red
snprintf(linep, MAXS, "@C9Finished converting %s\n", orig_url);
Fl::lock();
brow->add(linep); brow->bottomline(brow->size());
Fl::unlock();
Fl::awake((void *)NULL);
delete msg;
delete targ;
pthread_mutex_lock(&tn_mutex);
tn--;
pthread_mutex_unlock(&tn_mutex);
pthread_exit((void *)(intptr_t)sts);
return NULL;
} /* convert */
// ----------------------------------------------------------------------------
// THREAD: convert_all
// ----------------------------------------------------------------------------
void *convert_all(void *arg)
{
char *urls, *url, *sp, *s;
int ii, len, rc;
TARG *targs, *targ;
struct timespec req, rem;
int sts;
unsigned int tid;
targs = (TARG *)arg;
urls = targs->text;
len = strlen(urls);
#ifdef OLD_PTHREADS
tid = PtrToInt(pthread_self().p);
#else
tid = pthread_self();
#endif
if (len > 256)
xlog("THREAD %08x: convert_all: urls %.256s...\n", tid, urls);
else
xlog("THREAD %08x: convert_all: urls %s\n", tid, urls);
brow->clear();
ii = 0; sts = 0;
url = xstrtok_r(urls, "\r\n", &sp);
while (url != NULL) {
while (tn > MAXTN) { // wait until some of the threads finish
#ifdef _WIN32
Sleep(100); // msec
#else
req.tv_sec = 0; req.tv_nsec = 100000000L; // 100 msec
nanosleep(&req, &rem);
#endif
}
url = uri2path(url); // check drag&drop file syntax on Unix (URI)
ii++;
xlog("URL %d: %s\n", ii, url);
// brow->add(url); brow->bottomline(brow->size());
// Fl::flush();
targ = new TARG;
xstrncpy(targ->text, url, MAXL);
if (url != NULL) free(url);
sts += xpthread_create(convert, (void *)targ);
url = xstrtok_r(NULL, "\r\n", &sp);
}
delete targs;
pthread_mutex_lock(&tn_mutex);
tn--;
pthread_mutex_unlock(&tn_mutex);
pthread_exit((void *)(intptr_t)sts);
return NULL;
} /* convert_all */
// ----------------------------------------------------------------------------
// GetGroup
// ----------------------------------------------------------------------------
double GetGroup(const char *str, MatchResult mr, int group)
{
char grp[MAXS+1];
int gs, ge, maxs, n;
double rv;
gs = mr.GetGroupStart(group); ge = mr.GetGroupEnd(group);
maxs = ge - gs;
if (maxs > MAXS) maxs = MAXS;
xstrncpy(grp, str + gs, maxs);
n = sscanf(grp, "%lf", &rv);
if (n != 1) {
// error
rv = 0.0;
}
return rv;
} /* GetGroup */
// ----------------------------------------------------------------------------
// parse_double
// ----------------------------------------------------------------------------
int parse_double(const char *str, double *val)
{
int n;
n = sscanf(str, "%lf", val);
if (n != 1) {
*val = 0.0;
}
return 0;
} /* parse_double */
// ----------------------------------------------------------------------------
// parse_double_xy
// ----------------------------------------------------------------------------
int parse_double_xy(const char *str, double *val1, double *val2)
{
// Decimal numbers (DN): 523125.803, 155370.642
// Regex: ^\s*?([+-]??\d+?(\.\d*?)??)\s*?(,|\s)\s*?([+-]??\d+?(\.\d*?)??)\s*$
// 5 groups, needed: 1, 4
CRegexpT <char> regexp_dn("^\\s*?([+-]??\\d+?(\\.\\d*?)?""?)\\s*?(,|\\s)\\s*?([+-]??\\d+?(\\.\\d*?)?""?)\\s*$");
MatchResult mr;
int rv = 0;
*val1 = 0.0; *val2 = 0.0;
//xlog("parse_double_xy: |%s|\n", str);
mr = regexp_dn.MatchExact(str);
if (mr.IsMatched()) {
xlog("parse_double_xy: regexp_dn matched\n");
*val1 = GetGroup(str, mr, 1);
*val2 = GetGroup(str, mr, 4);
rv = 1;
}
return rv;
} /* parse_double_xy */
// ----------------------------------------------------------------------------
// parse_dms
// ----------------------------------------------------------------------------
int parse_dms(const char *str, double *val)
{
DMS dms;
int n;
dms.deg = 0.0; dms.min = 0.0; dms.sec = 0.0; // to keep compiler happy
switch (ddms) {
case 1: // Dec. Degrees
n = sscanf(str, "%lf", &dms.deg);
if (n != 1) {
// error
dms.deg = 0.0;
}
break;
case 2: // Deg. Min.
n = sscanf(str, "%lf%*[^0-9.-]%lf", &dms.deg, &dms.min);
if (n != 2) {
// error
dms.min = 0.0;
if (n != 1) dms.deg = 0.0;
}
dms.min = fabs(dms.min);
break;
case 3: // Deg. Min. Sec.
n = sscanf(str, "%lf%*[^0-9.-]%lf%*[^0-9.-]%lf", &dms.deg, &dms.min, &dms.sec);
if (n != 3) {
// error
dms.sec = 0.0;
if (n != 2) dms.min = 0.0;
if (n != 1) dms.deg = 0.0;
}
dms.min = fabs(dms.min);
dms.sec = fabs(dms.sec);
break;
} // switch (ddms)
dms2deg(dms, val);
return 0;
} /* parse_dms */
// ----------------------------------------------------------------------------
// parse_dms_fila
// ----------------------------------------------------------------------------
int parse_dms_fila(const char *str, double *val1, double *val2)
{
// Use same format as Google Maps, see https://support.google.com/maps/answer/18539
// Decimal degrees (DD): N 46.5375852874, E 15.3015019823
// Regex: ^\s*?[NS]??\s*?([+-]??\d+?(\.\d*?)??)\s*?\D{0,2}?\s*?,??\s*?[EW]??\s*?([+-]??\d+?(\.\d*?)??)\s*?\D{0,2}?\s*$
// 4 groups, needed: 1, 3
CRegexpT <char> regexp_dd("^\\s*?[NS]??\\s*?([+-]??\\d+?(\\.\\d*?)?""?)\\s*?\\D{0,2}?\\s*?,??\\s*?[EW]??\\s*?([+-]??\\d+?(\\.\\d*?)?""?)\\s*?\\D{0,2}?\\s*$", IGNORECASE);
// Degrees and decimal minutes (DMM): N 46 32.25511725, E 15 18.09011893
// Regex: ^\s*?[NS]??\s*?([+-]??\d+?)\s*?\D{0,2}?\s*?(\d+?(\.\d*?)??)\s*?\D{0,2}?\s*?,??\s*?[EW]??\s*?([+-]??\d+?)\s*?\D{0,2}?\s*?(\d+?(\.\d*?)??)\s*?\D{0,2}?\s*$
// 6 groups, needed: 1 2, 4 5
CRegexpT <char> regexp_dmm("^\\s*?[NS]??\\s*?([+-]??\\d+?)\\s*?\\D{0,2}?\\s*?(\\d+?(\\.\\d*?)?""?)\\s*?\\D{0,2}?\\s*?,??\\s*?[EW]??\\s*?([+-]??\\d+?)\\s*?\\D{0,2}?\\s*?(\\d+?(\\.\\d*?)?""?)\\s*?\\D{0,2}?\\s*$", IGNORECASE);
// Degrees, minutes, and seconds (DMS): 46°32'15.307035"N 15°18'5.407136"E
// Regex: ^\s*?([+-]??\d+?)\s*?\D{0,2}?\s*?(\d+?)\s*?\D{0,2}?\s*?(\d+?(\.\d*?)??)\s*?\D{0,2}?\s*?[NS]??\s*?,??\s*?([+-]??\d+?)\s*?\D{0,2}?\s*?(\d+?)\s*?\D{0,2}?\s*?(\d+?(\.\d*?)??)\s*?\D{0,2}?\s*?[EW]??\s*$
// 8 groups, needed: 1 2 3, 5 6 7
CRegexpT <char> regexp_dms("^\\s*?([+-]??\\d+?)\\s*?\\D{0,2}?\\s*?(\\d+?)\\s*?\\D{0,2}?\\s*?(\\d+?(\\.\\d*?)?""?)\\s*?\\D{0,2}?\\s*?[NS]??\\s*?,??\\s*?([+-]??\\d+?)\\s*?\\D{0,2}?\\s*?(\\d+?)\\s*?\\D{0,2}?\\s*?(\\d+?(\\.\\d*?)?""?)\\s*?\\D{0,2}?\\s*?[EW]??\\s*$", IGNORECASE);
MatchResult mr;
DMS dms1, dms2;
int rv = 0;
dms1.deg = 0.0; dms1.min = 0.0; dms1.sec = 0.0; // to keep compiler happy
dms2.deg = 0.0; dms2.min = 0.0; dms2.sec = 0.0;
//xlog("parse_dms_fila: |%s|\n", str);
switch (ddms) {
case 1: // Dec. Degrees
mr = regexp_dd.MatchExact(str);
if (mr.IsMatched()) {
xlog("parse_dms_fila: regexp_dd matched\n");
dms1.deg = GetGroup(str, mr, 1);
dms2.deg = GetGroup(str, mr, 3);
rv = 1;
}
break;
case 2: // Deg. Min.
mr = regexp_dmm.MatchExact(str);
if (mr.IsMatched()) {
xlog("parse_dms_fila: regexp_dmm matched\n");
dms1.deg = GetGroup(str, mr, 1);
dms1.min = GetGroup(str, mr, 2);
dms2.deg = GetGroup(str, mr, 4);
dms2.min = GetGroup(str, mr, 5);
rv = 1;
}
break;
case 3: // Deg. Min. Sec.
mr = regexp_dms.MatchExact(str);
if (mr.IsMatched()) {
xlog("parse_dms_fila: regexp_dms matched\n");
dms1.deg = GetGroup(str, mr, 1);
dms1.min = GetGroup(str, mr, 2);
dms1.sec = GetGroup(str, mr, 3);
dms2.deg = GetGroup(str, mr, 5);
dms2.min = GetGroup(str, mr, 6);
dms2.sec = GetGroup(str, mr, 7);
rv = 1;
}
break;
} // switch (ddms)
dms2deg(dms1, val1);
dms2deg(dms2, val2);
return rv;
} /* parse_dms_fila */
// ----------------------------------------------------------------------------
// convert_cb
// Widget w and parameter p can be NULL!
// ----------------------------------------------------------------------------
void convert_cb(Fl_Widget *w, void *p)
{
//Fl_Group *gtr;
int n;
char value1[MAXS+1], value2[MAXS+1], value3[MAXS+1];
char value12[MAXS+1], ns, ew;
double fi, la, h, x, y, H;
GEOGRA fl; GEOUTM xy, gkxy, tmxy;
DMS lat, lon;
int last_tri = -1;
//gtr = (Fl_Group *)p;
xy.x = 0.0; xy.y = 0.0; xy.H = 0.0; // to keep compiler happy
fl.fi = 0.0; fl.la = 0.0; fl.h = 0.0;
switch (tr) {
case 1: // gtr1, xy (D96/TM) ==> fila (ETRS89)
case 3: // gtr1, xy (D48/GK) ==> fila (ETRS89)
case 9: // gtr1, xy (D48/GK) ==> fila (ETRS89), AFT
if (parse_double_xy(input[gtr1_XY]->value(), &y, &x)) {
input[gtr1_X]->value("");
input[gtr1_Y]->value("");
}
else {
parse_double(input[gtr1_X]->value(), &y);
parse_double(input[gtr1_Y]->value(), &x);
}
parse_double(input[gtr1_H]->value(), &H);
xlog("convert_cb: input(tr = %d): x = %g, y = %g, H = %g\n", tr, x, y, H);
if (y < 200000.0) {
// warn: possibly reversed x/y
}
xy.x = x; xy.y = y; xy.H = H;
break;
case 2: // gtr2, fila (ETRS89) ==> xy (D96/TM)
case 4: // gtr2, fila (ETRS89) ==> xy (D48/GK)
case 10: // gtr2, fila (ETRS89) ==> xy (D48/GK), AFT
if (parse_dms_fila(input[gtr2_FILA]->value(), &fi, &la)) {
input[gtr2_FI]->value("");
input[gtr2_LA]->value("");
}
else {
parse_dms(input[gtr2_FI]->value(), &fi);
parse_dms(input[gtr2_LA]->value(), &la);
}
parse_double(input[gtr2_h]->value(), &h);
if (fi > 90.0 || fi < -90.0) fi = 0.0;
if (la > 180.0 || la < -180.0) la = 0.0;
xlog("convert_cb: input(tr = %d): fi = %g, la = %g, h = %g\n", tr, fi, la, h);
if (la > 17.0) {
// warn: possibly reversed fi/la
}
fl.fi = fi; fl.la = la; fl.h = h;
break;
case 5: // gtr3, xy (D48/GK) ==> xy (D96/TM)
case 6: // gtr3, xy (D96/TM) ==> xy (D48/GK)
case 7: // gtr3, xy (D48/GK) ==> xy (D96/TM), AFT
case 8: // gtr3, xy (D96/TM) ==> xy (D48/GK), AFT
if (parse_double_xy(input[gtr3_XYi]->value(), &y, &x)) {
input[gtr3_Xi]->value("");
input[gtr3_Yi]->value("");
}
else {
parse_double(input[gtr3_Xi]->value(), &y);
parse_double(input[gtr3_Yi]->value(), &x);
}
parse_double(input[gtr3_Hi]->value(), &H);
xlog("convert_cb: input(tr = %d): x = %g, y = %g, H = %g\n", tr, x, y, H);
if (y < 200000.0) {
// warn: possibly reversed x/y
}
xy.x = x; xy.y = y; xy.H = H;
break;
} // switch (tr)
switch (tr) {
case 1: tmxy2fila_wgs(xy, &fl); break; // gtr1, xy (D96/TM) ==> fila (ETRS89)
case 2: fila_wgs2tmxy(fl, &xy); break; // gtr2, fila (ETRS89) ==> xy (D96/TM)
case 3: gkxy2fila_wgs(xy, &fl); break; // gtr1, xy (D48/GK) ==> fila (ETRS89)
case 4: fila_wgs2gkxy(fl, &xy); break; // gtr2, fila (ETRS89) ==> xy (D48/GK)
case 5: gkxy2tmxy(xy, &tmxy); xy = tmxy; break; // gtr3, xy (D48/GK) ==> xy (D96/TM)
case 6: tmxy2gkxy(xy, &gkxy); xy = gkxy; break; // gtr3, xy (D96/TM) ==> xy (D48/GK)
case 7: gkxy2tmxy_aft(xy, &tmxy, &last_tri); xy = tmxy; break; // gtr3, xy (D48/GK) ==> xy (D96/TM), AFT
case 8: tmxy2gkxy_aft(xy, &gkxy, &last_tri); xy = gkxy; break; // gtr3, xy (D96/TM) ==> xy (D48/GK), AFT
case 9: gkxy2fila_wgs_aft(xy, &fl, &last_tri); break; // gtr1, xy (D48/GK) ==> fila (ETRS89), AFT
case 10: fila_wgs2gkxy_aft(fl, &xy, &last_tri); break; // gtr2, fila (ETRS89) ==> xy (D48/GK), AFT
} // switch (tr)
switch (tr) {
case 1: // gtr1, xy (D96/TM) ==> fila (ETRS89)
case 3: // gtr1, xy (D48/GK) ==> fila (ETRS89)
case 9: // gtr1, xy (D48/GK) ==> fila (ETRS89), AFT
xlog("convert_cb: output(tr = %d): fi = %.9f, la = %.9f, h = %.3f\n", tr, fl.fi, fl.la, fl.h);
switch (ddms) {
case 1: // Dec. Degrees
snprintf(value12, MAXS, "%.9f, %.9f", fl.fi, fl.la);
snprintf(value1, MAXS, "%.9f", fl.fi);
snprintf(value2, MAXS, "%.9f", fl.la);
snprintf(value3, MAXS, "%.3f", fl.h);
break;
case 2: // Deg. Min.
deg2dm(fl.fi, &lat); deg2dm(fl.la, &lon);
snprintf(value12, MAXS, "%.0f\xB0 %.7f', %.0f\xB0 %.7f'", lat.deg, lat.min, lon.deg, lon.min);
snprintf(value1, MAXS, "%.0f\xB0 %.7f'", lat.deg, lat.min);
snprintf(value2, MAXS, "%.0f\xB0 %.7f'", lon.deg, lon.min);
snprintf(value3, MAXS, "%.3f", fl.h);
break;
case 3: // Deg. Min. Sec.
deg2dms(fl.fi, &lat); deg2dms(fl.la, &lon);
if (fl.fi >= 0) ns = 'N'; else ns = 'S';
if (fl.la >= 0) ew = 'E'; else ew = 'W';
snprintf(value12, MAXS, "%.0f\xB0 %.0f' %.5f\"%c %.0f\xB0 %.0f' %.5f\"%c", lat.deg, lat.min, lat.sec, ns, lon.deg, lon.min, lon.sec, ew);
snprintf(value1, MAXS, "%.0f\xB0 %.0f' %.5f\"", lat.deg, lat.min, lat.sec);
snprintf(value2, MAXS, "%.0f\xB0 %.0f' %.5f\"", lon.deg, lon.min, lon.sec);
snprintf(value3, MAXS, "%.3f", fl.h);
break;
} // switch (ddms)
output[gtr1_FILA]->value(value12);
output[gtr1_FI]->value(value1);
output[gtr1_LA]->value(value2);
output[gtr1_h]->value(value3);
break;
case 2: // gtr2, fila (ETRS89) ==> xy (D96/TM)
case 4: // gtr2, fila (ETRS89) ==> xy (D48/GK)
case 10: // gtr2, fila (ETRS89) ==> xy (D48/GK), AFT
xlog("convert_cb: output(tr = %d): x = %.9f, y = %.9f, H = %.3f\n", tr, xy.x, xy.y, xy.H);
snprintf(value12, MAXS, "%.3f, %.3f", xy.y, xy.x);
snprintf(value1, MAXS, "%.3f", xy.y);
snprintf(value2, MAXS, "%.3f", xy.x);
snprintf(value3, MAXS, "%.3f", xy.H);
output[gtr2_XY]->value(value12);
output[gtr2_X]->value(value1);
output[gtr2_Y]->value(value2);
output[gtr2_H]->value(value3);
break;
case 5: // gtr3, xy (D48/GK) ==> xy (D96/TM)
case 6: // gtr3, xy (D96/TM) ==> xy (D48/GK)
case 7: // gtr3, xy (D48/GK) ==> xy (D96/TM), AFT
case 8: // gtr3, xy (D96/TM) ==> xy (D48/GK), AFT
xlog("convert_cb: output(tr = %d): x = %.9f, y = %.9f, H = %.3f\n", tr, xy.x, xy.y, xy.H);
snprintf(value12, MAXS, "%.3f, %.3f", xy.y, xy.x);
snprintf(value1, MAXS, "%.3f", xy.y);
snprintf(value2, MAXS, "%.3f", xy.x);
snprintf(value3, MAXS, "%.3f", xy.H);
output[gtr3_XYo]->value(value12);
output[gtr3_Xo]->value(value1);
output[gtr3_Yo]->value(value2);
output[gtr3_Ho]->value(value3);
break;
} // switch (tr)
} /* convert_cb */
// ----------------------------------------------------------------------------
// open_url
// ----------------------------------------------------------------------------
int open_url(const char *url)
{
int rc, ii, blen;
char buf[MAXS+1], cmd[MAXS+1], *path;
#ifdef _WIN32
HINSTANCE hi;
#endif
#ifdef _WIN32
hi = ShellExecute(HWND_DESKTOP, "open", url, NULL, NULL, SW_SHOWDEFAULT);
rc = PtrToInt(hi);
if (rc > 32) rc = 0;
else if (rc == 0) rc = ENOMEM;
#else
rc = 1;
blen = sizeof(browsers)/sizeof(*browsers);
for (ii = 0; ii < blen; ii++) {
if ((path = locate_exe(browsers[ii], buf, MAXS)) == NULL)
continue;
snprintf(cmd, MAXS, "\"%s\" '%s' >/dev/null 2>&1 &", path, url);
rc = system(cmd);
break;
}
#endif
return rc;
} /* open_url */
// ----------------------------------------------------------------------------
// show_cb
// ----------------------------------------------------------------------------
void show_cb(Fl_Widget *w, void *p)
{
double fi, la, h, x, y, H;
GEOGRA fl; GEOUTM xy;
char fip, lap, url[MAXS+1];
int last_tri = -1;
fi = 0.0; la = 0.0;
fl.fi = 0.0; fl.la = 0.0;
switch (tr) {
case 1: // gtr1, xy (D96/TM) ==> fila (ETRS89)
case 3: // gtr1, xy (D48/GK) ==> fila (ETRS89)
case 9: // gtr1, xy (D48/GK) ==> fila (ETRS89), AFT
parse_dms(output[gtr1_FI]->value(), &fi);
parse_dms(output[gtr1_LA]->value(), &la);
if (fi > 90.0 || fi < -90.0) fi = 0.0;
if (la > 180.0 || la < -180.0) la = 0.0;
if (fi == 0.0 || la == 0.0) {
parse_double(input[gtr1_X]->value(), &y);
parse_double(input[gtr1_Y]->value(), &x);
xy.x = x; xy.y = y; xy.H = 0;
switch (tr) {
case 1: tmxy2fila_wgs(xy, &fl); break; // gtr1, xy (D96/TM) ==> fila (ETRS89)
case 3: gkxy2fila_wgs(xy, &fl); break; // gtr1, xy (D48/GK) ==> fila (ETRS89)
case 9: gkxy2fila_wgs_aft(xy, &fl, &last_tri); break; // gtr1, xy (D48/GK) ==> fila (ETRS89), AFT
} // switch (tr)
fi = fl.fi; la = fl.la;
}
break;
case 2: // gtr2, fila (ETRS89) ==> xy (D96/TM)
case 4: // gtr2, fila (ETRS89) ==> xy (D48/GK)
case 10: // gtr2, fila (ETRS89) ==> xy (D48/GK), AFT
parse_dms(input[gtr2_FI]->value(), &fi);
parse_dms(input[gtr2_LA]->value(), &la);
if (fi > 90.0 || fi < -90.0) fi = 0.0;
if (la > 180.0 || la < -180.0) la = 0.0;
break;
case 5: // gtr3, xy (D48/GK) ==> xy (D96/TM)
case 6: // gtr3, xy (D96/TM) ==> xy (D48/GK)
case 7: // gtr3, xy (D48/GK) ==> xy (D96/TM), AFT
case 8: // gtr3, xy (D96/TM) ==> xy (D48/GK), AFT
parse_double(input[gtr3_Xi]->value(), &y);
parse_double(input[gtr3_Yi]->value(), &x);
xy.x = x; xy.y = y; xy.H = 0;
switch (tr) {
case 5: gkxy2fila_wgs_aft(xy, &fl, &last_tri); break; // gtr3, xy (D48/GK) ==> fila (ETRS89), AFT
case 6: tmxy2fila_wgs(xy, &fl); break; // gtr3, xy (D96/TM) ==> fila (ETRS89)
case 7: gkxy2fila_wgs_aft(xy, &fl, &last_tri); break; // gtr3, xy (D48/GK) ==> fila (ETRS89), AFT
case 8: tmxy2fila_wgs(xy, &fl); break; // gtr3, xy (D96/TM) ==> fila (ETRS89)
} // switch (tr)
fi = fl.fi; la = fl.la;
break;
} // switch (tr)
if (fi >= 0) fip = 'N'; else fip = 'S';
if (la >= 0) lap = 'E'; else lap = 'W';
snprintf(url, MAXS, "https://tools.wmflabs.org/geohack/geohack.php?params=%.9f_%c_%.9f_%c_scale:%d\n",
fabs(fi), fip, fabs(la), lap, SCALE);
open_url(url);
} /* show_cb */
// ----------------------------------------------------------------------------
// mainwin_cb
// ----------------------------------------------------------------------------
void mainwin_cb(Fl_Widget *w, void *p)
{
Fl_Double_Window *mw;
mw = (Fl_Double_Window *)w;
xlog("mainwin_cb\n");
mw->hide(); // not really needed (already default action)
} /* window_cb */
// ----------------------------------------------------------------------------
// menu_cb
// ----------------------------------------------------------------------------
void menu_cb(Fl_Widget *w, void *p)
{
Fl_Menu_ *m;
const Fl_Menu_Item *mi;
int rc;
m = (Fl_Menu_ *)w;
mi = m->mvalue();
if (!mi)
xlog("menu_cb: mi = NULL\n");
else if (mi->shortcut()) {
xlog("menu_cb: %s (%s)\n", mi->label(), fl_shortcut_label(mi->shortcut()));
rc = xpthread_create(worker, (void *)mi);
}
else {
xlog("menu_cb: %s\n", mi->label());
rc = xpthread_create(worker, (void *)mi);
}
} /* menu_cb */
// ----------------------------------------------------------------------------
// open_cb
// ----------------------------------------------------------------------------
void open_cb(Fl_Widget *w, void *p)
{
Fl_Native_File_Chooser *nfc;
char *text;
int ii, rc;
TARG *targ;
nfc = new Fl_Native_File_Chooser();
nfc->title("Select files");
nfc->type(Fl_Native_File_Chooser::BROWSE_MULTI_FILE);
if (ft == 1) // XYZ files