Repository navigation
Expand file tree
/
Copy pathDEM.c
More file actions
3315 lines (2883 loc) · 93.2 KB
/
Copy pathDEM.c
File metadata and controls
3315 lines (2883 loc) · 93.2 KB
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
/* DEM.c (ne gisdem.c 14 Jan 1994 CXH)
** Digital elevation model manipulations for WCS.
** Original code written by Gary R. Huber, July 1992. Incorporated into
** GIS July 30, 1993.
*/
#define CATCOMP_NUMBERS 1
#include "WCS_locale.h"
#include "WCS.h"
#include "GUIDefines.h"
#include "Useful.h"
LONG KPrintF(STRPTR format, ...); // ALEXANDER
#include <errno.h> //ALEXANDER
#include "BigEndianReadWrite.h"
STATIC_FCN short computerelel(short boxsize, short *arrayptr, struct elmapheaderV101 *map); // used locally only -> static, AF 19.7.2021
STATIC_FCN short Read_USGSProfHeader(FILE *DEM, struct USGS_DEMProfileHeader *ProfHdr); // used locally only -> static, AF 19.7.2021
STATIC_FCN FILE *DEMFile_Init(struct DEMExtractData *DEMExtract, short k, char *MsgHdr); // used locally only -> static, AF 23.7.2021
STATIC_FCN short enterbox(void); // used locally only -> static, AF 23.7.2021
STATIC_FCN short Read_USGSDEMProfile(FILE *DEM, short *ProfPtr, short ProfItems); // used locally only -> static, AF 23.7.2021
STATIC_FCN short Read_USGSHeader(FILE *DEM, struct USGS_DEMHeader *Hdr); // used locally only -> static, AF 23.7.2021
STATIC_FCN short saveDEM(char *filename, struct elmapheaderV101 *Map, short *Array); // used locally only -> static, AF 23.7.2021
STATIC_FCN short SplineMap(short *map, short Xrows, short Ycols, double elvar, double flatmax); // used locally only -> static, AF 23.7.2021
STATIC_FCN void padarray(short *arrayptr, struct elmapheaderV101 *map); // used locally only -> static, AF 23.7.2021
short readDEM(char *filename, struct elmapheaderV101 *map)
{
short rowlength, error = 0;
short *mapptr;
LONG fhelev, Lr;
float Version;
if ((fhelev = open(filename, O_RDONLY, 0)) == -1)
{
return (1);
} /* if open file failed */
read(fhelev, &Version, 4);
ENDIAN_CHANGE_IF_NEEDED(SimpleEndianFlip32F(Version,&Version);) // AF: 14.Dec.2022, Endian correction for i386-aros
if (fabs(Version - 1.00) < .0001)
{
if ((read (fhelev, map, ELEVHDRLENV100)) != ELEVHDRLENV100)
{
Log(WNG_READ_FAIL, (CONST_STRPTR)filename);
close (fhelev);
return (1);
} /* if */
ENDIAN_CHANGE_IF_NEEDED( /* AF: 14.Dec.2022, Endian correction for i386-aros */
SimpleEndianFlip32S(map->rows,&map->rows);
SimpleEndianFlip32S(map->columns,&map->columns);
SimpleEndianFlip64(map->lolat,&map->lolat);
SimpleEndianFlip64(map->lolong,&map->lolong);
SimpleEndianFlip64(map->steplat,&map->steplat);
SimpleEndianFlip64(map->steplong,&map->steplong);
SimpleEndianFlip64(map->elscale,&map->elscale); // offset 40, 8 bytes
// we read only ELEVHDRLENV100 = 48 bytes, so endian-correct only up to here.
)
map->MaxEl = map->MinEl = 0;
map->Samples = 0;
map->SumElDif = map->SumElDifSq = 0.0;
}
else
{
if (fabs(Version - 1.01) < .0001 || fabs(Version - 1.02) < .0001)
{
if ((read (fhelev, map, ELEVHDRLENV101)) != ELEVHDRLENV101)
{
Log(WNG_READ_FAIL, (CONST_STRPTR)filename);
close (fhelev);
return (1);
} /* if */
ENDIAN_CHANGE_IF_NEEDED( /* AF: 14.Dec.2022, Endian correction for i386-aros */
/* same code as above, it is the same target struct. It seems the ELEVHDRLENV101 was just shorter? */
SimpleEndianFlip32S(map->rows,&map->rows);
SimpleEndianFlip32S(map->columns,&map->columns);
SimpleEndianFlip64(map->lolat,&map->lolat);
SimpleEndianFlip64(map->lolong,&map->lolong);
SimpleEndianFlip64(map->steplat,&map->steplat);
SimpleEndianFlip64(map->steplong,&map->steplong);
SimpleEndianFlip64(map->elscale,&map->elscale);
SimpleEndianFlip16S(map->MaxEl,&map->MaxEl);
SimpleEndianFlip16S(map->MinEl,&map->MinEl);
SimpleEndianFlip32S(map->Samples,&map->Samples);
SimpleEndianFlip32F(map->SumElDif,&map->SumElDif);
SimpleEndianFlip32F(map->SumElDifSq,&map->SumElDifSq); // offset 60, 4 bytes
// we read only ELEVHDRLENV101 = 64 bytes, so endian-correct only up to here.
)
}
else
{
Version = 0.0;
lseek(fhelev, 0L, 0);
if ((read (fhelev, map, ELEVHDRLENV100)) != ELEVHDRLENV100)
{
Log(WNG_READ_FAIL, (CONST_STRPTR)filename);
close (fhelev);
return (1);
} /* if */
ENDIAN_CHANGE_IF_NEEDED( /* AF: 14.Dec.2022, Endian correction for i386-aros */
/* same code as above, it is the same target struct. It seems the ELEVHDRLENV101 was just shorter? */
SimpleEndianFlip32S(map->rows,&map->rows);
SimpleEndianFlip32S(map->columns,&map->columns);
SimpleEndianFlip64(map->lolat,&map->lolat);
SimpleEndianFlip64(map->lolong,&map->lolong);
SimpleEndianFlip64(map->steplat,&map->steplat);
SimpleEndianFlip64(map->steplong,&map->steplong);
SimpleEndianFlip64(map->elscale,&map->elscale); // offset 40, 8 bytes
// we read only ELEVHDRLENV100 = 48 bytes, so endian-correct only up to here.
)
map->MaxEl = map->MinEl = 0;
map->Samples = 0;
map->SumElDif = map->SumElDifSq = 0.0;
} /* if oldest file version */
}
map->size = (map->rows + 1) * (map->columns) * 2;
map->scrnptrsize = map->size * 2;
map->map = (short *)get_Memory (map->size, MEMF_ANY);
if (! map->map)
{
close (fhelev);
goto Cleanup;
} /* if */
if (Version == 0.0)
{
mapptr = (short *)map->map;
rowlength = 2 * map->columns;
for (Lr=0; Lr<=map->rows; Lr++)
{
lseek(fhelev, 2, 1);
if ((read (fhelev, mapptr, rowlength)) != rowlength)
{
error = 1;
Log(WNG_READ_FAIL, (CONST_STRPTR)filename);
break;
} /* if */
ENDIAN_CHANGE_IF_NEEDED( /* AF: 14.Dec.2022, Endian correction for i386-aros */
for(unsigned int i=0;i<rowlength;i++)
{
SimpleEndianFlip16S(*(mapptr+i),mapptr+i);
}
)
mapptr += map->columns;
lseek(fhelev, 2, 1);
} /* for */
close(fhelev);
} /* if oldest file version */
else
{
if ((read (fhelev, map->map, map->size)) != map->size)
{
Log(WNG_READ_FAIL, (CONST_STRPTR)filename);
error = 1;
} /* if */
ENDIAN_CHANGE_IF_NEEDED( /* AF: 14.Dec.2022, Endian correction for i386-aros */
for(unsigned int i=0;i<map->size/2;i++)
{
SimpleEndianFlip16S(*(map->map+i),map->map+i);
}
)
close (fhelev);
} /* else version 1.0+ file */
if (! error)
{
if (fabs(Version - DEM_CURRENT_VERSION) > .0001)
{
map->elscale = ELSCALE_METERS;
if (map->MaxEl == map->MinEl && map->MaxEl == 0)
FindElMaxMin(map, map->map);
saveDEM(filename, map, map->map);
} /* if old file version, re-save it */
return (0);
} /* if no error */
Cleanup:
if (map->map) free_Memory(map->map, map->size);
map->map = (short *)NULL;
return (1);
} /* readDEM() */
/*********************************************************************/
STATIC_FCN short saveDEM(char *filename, struct elmapheaderV101 *Map, short *Array) // used locally only -> static, AF 23.7.2021
{
long fhelev,
protflags = FIBB_OTR_READ | FIBB_OTR_WRITE | FIBB_OTR_DELETE;
float Version = DEM_CURRENT_VERSION;
if ((fhelev = open (filename, O_WRONLY | O_TRUNC | O_CREAT, protflags)) == -1)
{
return (0);
} /* if error opening output file */
write (fhelev, (char *)&Version, 4);
write (fhelev, (char *)Map, ELEVHDRLENV101);
if (write (fhelev, (char *)Array, Map->size) != Map->size)
{
close (fhelev);
return (0);
} /* if error writing file */
close(fhelev);
return (1);
} /* saveDEM() */
/*********************************************************************/
short makerelelfile(char *elevpath, char *elevfile)
{
char extension[32], filename[255];
short error = 0, boxsize, Lr;
short *arrayptr, *mapptr, *rowptr;
long fhelev, arraysize, rowsize, maprowsize, maprowbytes;
float Version = DEM_CURRENT_VERSION;
struct elmapheaderV101 map;
boxsize = enterbox();
strsfn(elevfile, NULL, NULL, str, extension);
if (strcmp(extension, "elev"))
{
Log(ERR_WRONG_TYPE, (CONST_STRPTR)elevfile);
return (0);
} /* if wrong file type */
strmfp(filename, elevpath, elevfile);
if ((error = readDEM(filename, &map)) != 0)
{
Log(ERR_READ_FAIL, (CONST_STRPTR)elevfile);
return (0);
} /* if error reading DEM file */
arraysize = (map.rows + 11) * (map.columns + 10) * sizeof (short);
if ((arrayptr = (short *)get_Memory (arraysize, MEMF_ANY)) == NULL)
{
Log(ERR_MEM_FAIL, GetString( MSG_DEM_CREATINGRELATIVEELEVATIONMODEL ) ); // "Creating Relative Elevation Model"
error = 1;
goto EndMap;
} /* if */
rowsize = map.columns + 10;
rowptr = arrayptr + 5 * rowsize + 5;
mapptr = map.map;
maprowsize = map.columns;
maprowbytes = maprowsize * sizeof (short);
for (Lr=0; Lr<=map.rows; Lr++)
{
memcpy (rowptr, mapptr, maprowbytes);
rowptr += rowsize;
mapptr += maprowsize;
} /* for Lr=0... */
padarray(arrayptr, &map);
if (! computerelel(boxsize, arrayptr, &map))
{
error = 1;
goto EndMap;
} /* if abort */
FindElMaxMin(&map, map.map);
strsfn(elevfile, NULL, NULL, str, extension);
strmfp(filename, elevpath, str);
strcat(filename, ".relel");
if ((fhelev = open (filename, O_WRONLY | O_TRUNC | O_CREAT, 0)) == -1)
{
Log(ERR_OPEN_FAIL, GetString( MSG_DEM_RELATIVEELEVATION ) ); // "Relative elevation"
error = 1;
goto EndMap;
} /* if error opening output file */
write (fhelev, (char *)&Version, 4);
write (fhelev, (char *)&map, ELEVHDRLENV101);
if (write (fhelev, (char *)map.map, map.size) != map.size)
{
Log(ERR_WRITE_FAIL, GetString( MSG_DEM_RELATIVEELEVATION )); // "Relative elevation"
close (fhelev);
error = 1;
goto EndMap;
} /* if error writing file */
else
{
close(fhelev);
elevfile[strlen(elevfile) - 4] = 0;
strcat(elevfile, "relel");
Log(MSG_RELEL_SAVE, (CONST_STRPTR)elevfile);
} /* else saved OK */
EndMap:
if (arrayptr) free_Memory (arrayptr, arraysize);
if (map.map) free_Memory (map.map, map.size);
map.map = NULL;
return ((short)(! error));
} /* makerelelfile() */
/*********************************************************************/
STATIC_FCN void padarray(short *arrayptr, struct elmapheaderV101 *map) // used locally only -> static, AF 23.7.2021
{
short *rowptr, *mapptr, Lr, Lc;
long rowsize, maprowsize, maprowbytes;
rowsize = map->columns + 10;
maprowsize = map->columns;
maprowbytes = maprowsize * sizeof (short);
rowptr = arrayptr + 5 * rowsize + 5;
mapptr = arrayptr + 5;
for (Lr=0; Lr<5; Lr++)
{
memcpy(mapptr, rowptr, maprowbytes);
mapptr += rowsize;
} /* for Lr=0... */
rowptr = arrayptr + (map->rows + 5) * rowsize + 5;
mapptr = rowptr + rowsize;
for (Lr=0; Lr<5; Lr++)
{
memcpy(mapptr, rowptr, maprowbytes);
mapptr += rowsize;
} /* for Lr=0... */
rowptr = arrayptr + 5;
for (Lr=0; Lr<=map->rows + 10; Lr++)
{
mapptr = arrayptr + Lr * rowsize;
for (Lc=0; Lc<5; Lc++)
{
*mapptr = *rowptr;
mapptr ++;
} /* for Lc=0... */
rowptr += rowsize;
} /* for Lr=0... */
rowptr = arrayptr + 4 + maprowsize;
for (Lr=0; Lr<=map->rows + 10; Lr++)
{
mapptr = arrayptr + Lr * rowsize + 5 + maprowsize;
for (Lc=0; Lc<5; Lc++)
{
*mapptr = *rowptr;
mapptr ++;
} /* for Lc=0... */
rowptr += rowsize;
} /* for Lr=0... */
} /* padarray() */
/*********************************************************************/
STATIC_FCN short enterbox(void) // used locally only -> static, AF 23.7.2021
{
static const short stdweight11[11][11] = { // static -> init the array only once and reduce stack usage AF, 23.July 2021
{0,0,1,1,1, 2,1,1,1,0,0},
{0,1,1,2,3, 3,3,2,1,1,0},
{1,1,2,3,4, 4,4,3,2,1,1},
{1,2,3,4,5, 6,5,4,3,2,1},
{1,3,4,5,7, 8,7,5,4,3,1},
{2,3,4,6,8,10,8,6,4,3,2},
{1,3,4,5,7, 8,7,5,4,3,1},
{1,2,3,4,5, 6,5,4,3,2,1},
{1,1,2,3,4, 4,4,3,2,1,1},
{0,1,1,2,3, 3,3,2,1,1,0},
{0,0,1,1,1, 2,1,1,1,0,0}
};
short boxsize = 11, i, j;
double sum = 0.0;
#ifdef USER_SPECIFIED_PARAMETERS
/* Current implementation assumes a box size of 11 with standard weighting */
printf("Do you wish to weight the array filter? (2= Std, 1= Input, 0= No): ");
scanf("%hd", &weightarray);
if (weightarray == 2) boxsize = 11;
else
{
printf("Enter an odd number for box filter size: ");
scanf("%hd", &boxsize);
if (boxsize < 3) boxsize = 1;
if (boxsize > 11) boxsize = 11;
}
if (weightarray)
{
if (weightarray == 2)
{
#endif
for (i=0; i<boxsize; i++)
{
for (j=0; j<boxsize; j++)
{
weight[i][j] = stdweight11[i][j];
sum += weight[i][j];
} /* for j=0... */
} /* for i=0... */
#ifdef USER_SPECIFIED_PARAMETERS
} /* if use standard weight array */
else
{
printf("Enter rows of weight values.\n");
for (i=0; i<boxsize; i++)
{
for (j=0; j<boxsize; j++)
{
scanf("%le", &weight[i][j]);
sum += weight[i][j];
} /* for j=0... */
} /* for i=0... */
} /* else not standard array */
printf("Thank you. Input finished. Return to WCS Screen\n");
#endif
if (sum) sum = 1.0 / sum;
for (i=0; i<boxsize; i++)
{
for (j=0; j<boxsize; j++)
{
weight[i][j] *=sum;
} /* for j=0... */
} /* for i=0... */
#ifdef USER_SPECIFIED_PARAMETERS
} /* if weighted array */
else
{
printf("Thank you.\n");
sum=1.0 / (boxsize * boxsize);
for (i=0; i<boxsize; i++)
{
for (j=0; j<boxsize; j++)
{
weight[i][j] = sum;
} /* for j=0... */
} /* for i=0... */
} /* else not weighted array */
#endif
return (boxsize);
} /* enterbox() */
/*********************************************************************/
STATIC_FCN short computerelel(short boxsize, short *arrayptr, struct elmapheaderV101 *map) // used locally only -> static, AF 19.7.2021
{
short a, b, Lr, Lc, boxoffset, error = 0;
short *mapptr, *dataptr, *firstpt, *firstptr;
long datarowsize;
double sum, ScaleFactor;
struct BusyWindow *BWRE;
BWRE = BusyWin_New((char*)GetString( MSG_DEM_COMPUTING ), map->rows + 1, 0, MakeID('B','W','R','E')); // "Computing"
boxoffset = (boxsize - 1) / 2;
datarowsize = map->columns + 10;
mapptr = map->map;
ScaleFactor =
(((1.0 / 1200.0) / map->steplat) + ((1.0 / 1200.0) / map->steplong))
/ 2.0;
for (Lr=0; Lr<=map->rows; Lr++)
{
firstpt = arrayptr + (Lr + 5 - boxoffset) * datarowsize + 5 - boxoffset;
for (Lc=0; Lc<map->columns; Lc++)
{
sum = 0.0;
firstptr = firstpt;
for (a=0; a<boxsize; a++)
{
dataptr = firstptr;
for (b=0; b<boxsize; b++)
{
sum += (*dataptr * weight[a][b]);
dataptr ++;
} /* for b=0... */
firstptr += datarowsize;
} /* for a=0... */
*mapptr -= sum;
*mapptr *= ScaleFactor;
mapptr ++;
firstpt ++;
} /* for Lc=0... */
if (CheckInput_ID() == ID_BW_CLOSE)
{
error = 1;
break;
}
BusyWin_Update(BWRE, Lr + 1);
} /* for Lr=0... */
BusyWin_Del(BWRE);
return ((short)(! error));
} /* computerelel() */
/*********************************************************************/
short InterpDEM(struct DEMInterpolateData *DEMInterp)
{
char append, rootfile[32], newrootfile[32], extension[32],
filename[256], elevfile[32];
short filelen, i, j, k, error = 0, newrows, newcols, Lr, Lc, OBNexists, Elev[6];
short *arrayptr=NULL, *mapptr, *dataptr;
long fhelev, arraysize, maprowbytes,
protflags = FIBB_OTR_READ | FIBB_OTR_WRITE | FIBB_OTR_DELETE;
float Version = DEM_CURRENT_VERSION;
double lolat, lolong, Lon[6], Lat[6];
struct elmapheaderV101 map={0};
struct BusyWindow *BWFI;
BWFI = BusyWin_New((char*)GetString( MSG_DEM_FILES ), DEMInterp->FrFile->rf_NumArgs, 0, MakeID('B','W','F','I')); // "Files"
for (k=0; k<DEMInterp->FrFile->rf_NumArgs; k++)
{
strcpy((char*)DEMInterp->elevfile, (char*)DEMInterp->FrFile->rf_ArgList[k].wa_Name);
if (DEMInterp->elevfile[0] == 0)
{
User_Message(GetString( MSG_DEM_DATAOPSDEMINTERPOLATE ) , // "Data Ops: DEM Interpolate"
GetString( MSG_DEM_NOFILESSELECTED ) , // "No file(s) selected!"
(CONST_STRPTR) GetString( MSG_GLOBAL_OK ) , // "OK",
(CONST_STRPTR)"o");
break;
} /* if no file */
RelelRepeat:
strcpy (elevfile, DEMInterp->elevfile);
strsfn(DEMInterp->elevfile, NULL, NULL, rootfile, extension);
if (stricmp(extension, "elev") && stricmp(extension, "relel")) // AF: make it case independent
{
Log(ERR_WRONG_TYPE, (CONST_STRPTR)DEMInterp->elevfile);
if (! User_Message((CONST_STRPTR)DEMInterp->elevfile,
GetString( MSG_DEM_ERROROPENINGFILEFORINTERPOLATIONILENOTDEMORREMONTINUE ) , // "Error opening file for interpolation!\nFile not DEM or REM\nContinue?"
GetString( MSG_GLOBAL_OKCANCEL ), // "OK|CANCEL"
(CONST_STRPTR)"oc"))
{
break;
}
continue;
}
/* warn if not enough room for interpolated name */
if (! strcmp(extension, "elev"))
{
if (rootfile[length[0] - 1] != ' ')
{
if (User_Message_Def((CONST_STRPTR)rootfile,
GetString( MSG_DEM_DEMNAMEISTOOLONGTOADDANEXTRACHARACTERTODOYOUWISHTOENTER ) , // "DEM name is too long to add an extra character to. Do you wish to enter a new base name for the DEM or abort the interpolation?"
GetString( MSG_DEM_NEWNAMEABORT ), // "New Name|Abort"
(CONST_STRPTR)"na", 1))
{
strcpy(str, rootfile);
if (GetInputString((char*)GetString( MSG_DB_ENTERNEWOBJECTNAME ), ":;*/?`#%", str)) // "Enter new object name."
{
while (strlen(str) < length[0])
strcat(str, " ");
str[length[0]] = 0;
strcpy(newrootfile, str);
} /* if new name entered */
else
continue;
} /* if chooses to enter new name */
else
continue;
} /* if name too long to append letters */
else
strcpy(newrootfile, rootfile);
} /* if elev file */
strmfp(filename, DEMInterp->elevpath, DEMInterp->elevfile);
if ((error = readDEM(filename, &map)) != 0)
{
if (! User_Message(GetString( MSG_DEM_DATAOPSINTERPOLATEDEM ) , // "Data Ops: Interpolate DEM"
GetString( MSG_DEM_ERRORREADINGELEVATIONFILEONTINUE ) , // "Error reading elevation file!\nContinue?",
GetString( MSG_GLOBAL_OKCANCEL ), // "OK|CANCEL"
(CONST_STRPTR)"oc"))
{
break;
} /* if not continue to next file */
continue;
} /* if error reading DEM file */
map.steplat *= .5;
map.steplong *= .5;
newrows = map.rows * 2;
newcols = map.columns * 2 - 1;
arraysize = (newrows + 1) * newcols * 2;
arrayptr = (short *)get_Memory (arraysize, MEMF_CLEAR);
if (arrayptr == NULL)
{
free_Memory (map.map, map.size);
User_Message(GetString( MSG_DEM_DATAOPSINTERPOLATEDEM ) , // "Data Ops: Interpolate DEM"
GetString( MSG_GLOBAL_OUTOFMEMORYOPERATIONTERMINATED ), // "Out of memory!\nOperation terminated."
GetString( MSG_GLOBAL_OK ), // "OK"
(CONST_STRPTR)"o");
} /* if */
mapptr = map.map;
dataptr = arrayptr;
for (Lr=0; Lr<=map.rows; Lr++)
{
for (Lc=0; Lc<map.columns; Lc++)
{
*dataptr = *mapptr;
mapptr ++;
dataptr += 2;
} /* for Lc=columns... */
dataptr += (newcols - 1);
} /* for Lr=0... */
if (! SplineMap(arrayptr, newrows + 1, newcols,
DEMInterp->elvar, DEMInterp->flatmax))
{
error = 1;
break;
} /* if abort */
/* turn off old map in database */
if (! strcmp(extension, "elev"))
{
for (i=0; i<NoOfObjects; i++)
{
if (! strcmp(DBase[i].Name, rootfile))
{
DBase[i].Mark[0] = 'N';
DBase[i].Enabled = ' ';
DBase[i].Flags &= (255 ^ 2);
break;
} /* if matching database item found */
} /* for i=0... */
} /* if elevation file */
append = 'A';
lolat = map.lolat;
lolong = map.lolong;
maprowbytes = map.columns * sizeof (short);
filelen = strlen(newrootfile) - 1;
while (newrootfile[filelen] == ' ' && filelen) newrootfile[filelen--] = 0;
for (i=0; i<2; i++)
{
for (j=0; j<2; j++)
{
map.lolat = lolat + j * (map.columns - 1) * map.steplat;
map.lolong = lolong - i * map.rows * map.steplong;
mapptr = map.map;
dataptr = arrayptr + i * map.rows * newcols + j * (map.columns - 1);
for (Lr=0; Lr<=map.rows; Lr++)
{
memcpy(mapptr, dataptr, maprowbytes);
mapptr += map.columns;
dataptr += newcols;
} /* for Lr=0... */
strcpy(DEMInterp->elevfile, newrootfile);
DEMInterp->elevfile[filelen + 1] = append;
DEMInterp->elevfile[filelen + 2] = 0;
while (strlen(DEMInterp->elevfile) < 10) strcat(DEMInterp->elevfile, " ");
strcat(DEMInterp->elevfile, ".");
strcat(DEMInterp->elevfile, extension);
strmfp(filename, DEMInterp->elevpath, DEMInterp->elevfile);
if ((fhelev = open (filename, O_WRONLY | O_TRUNC | O_CREAT, protflags))
== -1)
{
User_Message(GetString( MSG_DEM_DATAOPSINTERPOLATEDEM ) , // "Data Ops: Interpolate DEM"
GetString( MSG_DEM_ERROROPENINGDEMFILEFOROUTPUTPERATIONTERMINATED ) , // "Error opening DEM file for output!\nOperation terminated."
GetString( MSG_GLOBAL_OK ) , // "OK",
(CONST_STRPTR)"o");
Log(ERR_OPEN_FAIL, (CONST_STRPTR)DEMInterp->elevfile);
error = 1;
break;
} /* if open file failed */
FindElMaxMin(&map, map.map);
write (fhelev, (char *)&Version, 4);
write (fhelev, (char *)&map, ELEVHDRLENV101);
if (write (fhelev, (char *)map.map, map.size) != map.size)
{
User_Message(GetString( MSG_DEM_DATAOPSINTERPOLATEDEM ) , // "Data Ops: Interpolate DEM"
GetString( MSG_DEM_ERRORWRITINGDEMFILEPERATIONTERMINATED ) , // "Error writing DEM file!\nOperation terminated."
(CONST_STRPTR) GetString( MSG_GLOBAL_OK ), // "OK"
(CONST_STRPTR)"o");
Log(ERR_WRITE_FAIL, (CONST_STRPTR)DEMInterp->elevfile);
close (fhelev);
error = 1;
break;
}
else
{
close (fhelev);
Log(MSG_DEM_SAVE, (CONST_STRPTR)DEMInterp->elevfile);
if (! strcmp(extension, "elev"))
{
/* find out if object is already in the database */
DEMInterp->elevfile[length[0]] = 0;
OBNexists = 0;
for (OBN=0; OBN<NoOfObjects; OBN++)
{
if (! strcmp(DEMInterp->elevfile, DBase[OBN].Name))
{
OBNexists = 1;
break;
}
} /* for OBN=0... */
if (! OBNexists)
{
if (NoOfObjects + 1 > DBaseRecords)
{
struct database *NewBase;
if ((NewBase = DataBase_Expand(DBase, DBaseRecords, NoOfObjects,
DBaseRecords + 20)) == NULL)
{
User_Message(GetString( MSG_AGUI_DATABASEMODULE ) , // "Database Module",
GetString( MSG_DB_OUTOFMEMORYEXPANDINGDATABASEOPERATIONTERMINATED ), // "Out of memory expanding database!\nOperation terminated."
GetString( MSG_GLOBAL_OK ), // "OK",
(CONST_STRPTR)"o");
error = 1;
break;
} /* if new database allocation fails */
else
{
DBase = NewBase;
DBaseRecords += 20;
} /* else new database allocated and copied */
} /* if need more database space */
strncpy(DBase[OBN].Name, DEMInterp->elevfile, length[0]);
strcpy(DBase[OBN].Layer1, " ");
strcpy(DBase[OBN].Layer2, "TOP");
DBase[OBN].Color = 2;
DBase[OBN].LineWidth = 1;
strcpy(DBase[OBN].Label, " ");
DBase[OBN].MaxFract = 9;
DBase[OBN].Pattern[0] = 'L';
DBase[OBN].Red = 255;
DBase[OBN].Grn = 255;
DBase[OBN].Blu = 255;
NoOfObjects ++;
if (DE_Win)
{
if (! Add_DE_NewItem())
{
User_Message(GetString( MSG_AGUI_DATABASEMODULE ) , // "Database Module"
GetString( MSG_DLG_OUTOFMEMXPDBEDITORLISTOPERATIONTERMINATE ), // "Out of memory expanding Database Editor List!\nOperation terminated."
GetString( MSG_GLOBAL_OK ), // "OK",
(CONST_STRPTR)"o");
error = 1;
} /* if new list fails */
} /* if database editor open */
} /* if object file not already exists */
DBase[OBN].Points = 5;
DBase[OBN].Mark[0] = 'Y';
DBase[OBN].Enabled = '*';
DBase[OBN].Flags = 6;
strcpy(DBase[OBN].Special, "TOP");
DB_Mod = 1;
Lat[0] = Lat[1] = Lat[2] = Lat[5] = map.lolat;
Lat[3] = Lat[4] = map.lolat + (map.columns - 1) * map.steplat;
Lon[0] = Lon[1] = Lon[4] = Lon[5] = map.lolong - map.rows * map.steplong;
Lon[2] = Lon[3] = map.lolong;
memset(&Elev[0], 0, 6 * sizeof (short));
DEMInterp->elevfile[length[0]] = 0;
strcat(DEMInterp->elevfile, ".Obj");
strmfp(filename, DEMInterp->elevpath, DEMInterp->elevfile);
if (saveobject(OBN, filename, &Lon[0], &Lat[0], &Elev[0]))
{
User_Message(GetString( MSG_DEM_DATAOPSINTERPOLATEDEM ) , // "Data Ops: Interpolate DEM"
GetString( MSG_DEM_ERROROPENINGOBJECTFILEFOROUTPUTPERATIONTERMINATED ) , // "Error opening Object file for output!\nOperation terminated."
GetString( MSG_GLOBAL_OK ) , // "OK"
(CONST_STRPTR)"o");
error = 1;
break;
} /* error saving .Obj file */
} /* if elevation file */
} /* else file saved OK */
append ++;
if (error) break;
} /* for j=0... */
if (error) break;
} /* for i=0... */
//EndMap:
if (map.map) free_Memory(map.map, map.size);
map.map = NULL;
if (arrayptr) free_Memory(arrayptr, arraysize);
arrayptr = NULL;
if (error) break;
if (CheckInput_ID() == ID_BW_CLOSE)
{
error = 1;
break;
}
BusyWin_Update(BWFI, k + 1);
/* now repeat operation for relel file or create it if it doesn't exist */
if (! strcmp(extension, "elev"))
{
FILE *felev;
strcpy(DEMInterp->elevfile, elevfile);
elevfile[strlen(elevfile) - 4] = 0;
strcat(elevfile, "relel");
strmfp(filename, DEMInterp->elevpath, elevfile);
if ((felev = fopen(filename, "rb")) == NULL)
{
if (makerelelfile(DEMInterp->elevpath, DEMInterp->elevfile))
{
goto RelelRepeat;
} /* if make relel successful */
} /* if relel file not exists */
else
{
fclose(felev);
strcpy(DEMInterp->elevfile, elevfile);
goto RelelRepeat;
} /* else file exists */
} /* if elev file */
} /* for k=0... */
BusyWin_Del(BWFI);
if (map.map) free_Memory(map.map, map.size);
if (arrayptr) free_Memory(arrayptr, arraysize);
return ((short)(! error));
} /* InterpDEM() */
/*********************************************************************/
STATIC_FCN short SplineMap(short *map, short Xrows, short Ycols,
double elvar, double flatmax) // used locally only -> static, AF 23.7.2021
{
long i, j, CurPt, NxtPt, Steps, LastStep, rowsize;
double Delta, P1, P2, D1, D2, S1, S2, S3, h1, h2, h3, h4, TempVal, RandVar;
short *rowptr, error = 0;
struct BusyWindow *BWRE;
BWRE = BusyWin_New((char*)GetString( MSG_DEM_COMPUTING ), Xrows, 0, MakeID('B','W','R','E')); // "Computing"
Steps = Ycols - 2;
LastStep = Steps - 2;
rowptr = map;
rowsize = 2 * Ycols;
for (i=0; i<Xrows; i+=2)
{
for (j=0; j<Steps; j+=2)
{
CurPt = j;
NxtPt = CurPt + 2;
Delta = .5;
P1 = rowptr[CurPt];
P2 = rowptr[NxtPt];
D1 = j > 0 ? (.5 * (P2 - rowptr[CurPt - 2])): (P2 - P1);
D2 = j < LastStep ? (.5 * (rowptr[NxtPt + 2] - P1)): (P2 - P1);
S1 = Delta;
S2 = S1 * S1;
S3 = S1 * S2;
h1 = 2.0 * S3 - 3.0 * S2 + 1.0;
h2 = -2.0 * S3 + 3.0 * S2;
h3 = S3 - 2.0 * S2 + S1;
h4 = S3 - S2;
TempVal = P1 * h1 + P2 * h2 + D1 * h3 + D2 * h4;
if (TempVal > P1 && TempVal > P2) TempVal = max(P1, P2);
else if (TempVal < P1 && TempVal < P2) TempVal = min(P1, P2);
RandVar = elvar * fabs(P2 - P1);
if (RandVar < flatmax) RandVar = flatmax;
RandVar *= (2.0 * (.5 - af_drand48()));
rowptr[CurPt + 1] = (short)(TempVal + RandVar);
} /* for j=0... */
rowptr += rowsize;
} /* for i=0... */
Steps = Xrows - 2;
LastStep = Steps - 2;
rowptr = map;
rowsize = 2 * Ycols;
for (i=0; i<Ycols; i++)
{
for (j=0; j<Steps; j+=2)
{
CurPt = j * Ycols;
NxtPt = CurPt + rowsize;
Delta = .5;
P1 = rowptr[CurPt];
P2 = rowptr[NxtPt];
D1 = j > 0 ? (.5 * (P2 - rowptr[CurPt - rowsize])): (P2 - P1);
D2 = j < LastStep ? (.5 * (rowptr[NxtPt + rowsize] - P1)): (P2 - P1);
S1 = Delta;
S2 = S1 * S1;
S3 = S1 * S2;
h1 = 2.0 * S3 - 3.0 * S2 + 1.0;
h2 = -2.0 * S3 + 3.0 * S2;
h3 = S3 - 2.0 * S2 + S1;
h4 = S3 - S2;
TempVal = P1 * h1 + P2 * h2 + D1 * h3 + D2 * h4;
if (TempVal > P1 && TempVal > P2) TempVal = max(P1, P2);
else if (TempVal < P1 && TempVal < P2) TempVal = min(P1, P2);
RandVar = elvar * fabs(P2 - P1);
if (RandVar < flatmax) RandVar = flatmax;
RandVar *= (2.0 * (.5 - af_drand48()));
rowptr[CurPt + Ycols] = (short)(TempVal + RandVar);
} /* for j=0... */
rowptr ++;
if (CheckInput_ID() == ID_BW_CLOSE)
{
error = 1;
break;
}
BusyWin_Update(BWRE, i + 1);
} /* for i=0... */
BusyWin_Del(BWRE);
return ((short)(! error));
}/* SplineMap() */
/*********************************************************************/
short ExtractDEM(struct DEMExtractData *DEMExtract)
{
char elevbase[32], ExtChar[2], *MsgHdr = (char*)GetString( MSG_DEM_DATAOPSDEMEXTRACT ) ; // "Data Ops: DEM Extract"
short *TmpPtr, *MapPtr, error = 0, k, Elev, RowSign, ColSign, UTMZone;
long i, j, Lr, Rows, Columns, lowrow, lowcol, MapRowSize,
Row, Col, MapCtrX, MapCtrY, CornerPtX, CornerPtY, RowQuit, ColQuit;
double Slope;
FILE *DEMFile;
struct elmapheaderV101 MapHdr;
struct BusyWindow *BWFI, *BWRE;
MapHdr.elscale = ELSCALE_METERS;
DEMExtract->UTMData = NULL;
DEMExtract->LLData = NULL;
/* Allocate some basic structures */
if ((DEMExtract->Info = (struct DEMInfo *)get_Memory
(DEMExtract->FrFile->rf_NumArgs * sizeof (struct DEMInfo), MEMF_CLEAR))
== NULL)
{
User_Message((CONST_STRPTR)MsgHdr,
GetString( MSG_DEM_OUTOFMEMALLOCDEMINFOHEADERPERATIONTERMINATED ) , // "Out of memory allocating DEM Info Header!\nOperation terminated."
GetString( MSG_GLOBAL_OK ) , // "OK"
(CONST_STRPTR)"o");
return (0);
} /* if */
if ((DEMExtract->Convert = (struct UTMLatLonCoords *)get_Memory
(sizeof (struct UTMLatLonCoords), MEMF_CLEAR))
== NULL)
{
User_Message((CONST_STRPTR)MsgHdr,
GetString( MSG_DEM_OUTOFMEMALLOCDEMINFOHEADERPERATIONTERMINATED ) , // "Out of memory allocating DEM Info Header!\nOperation terminated."
(CONST_STRPTR) GetString( MSG_GLOBAL_OK ) , // "OK"
(CONST_STRPTR)"o");
goto EndExtract;
} /* if */
/* Get information about each DEM */
for (k=0; k<DEMExtract->FrFile->rf_NumArgs; k++)
{
DEMFile = DEMFile_Init(DEMExtract, k, MsgHdr);
fclose(DEMFile);
DEMExtract->Info[k].Code = atoi(DEMExtract->USGSHdr.RefSysCode);
DEMExtract->Info[k].Zone = atoi(DEMExtract->USGSHdr.Zone);
DEMExtract->Info[k].HUnits = atoi(DEMExtract->USGSHdr.GrUnits);
DEMExtract->Info[k].VUnits = atoi(DEMExtract->USGSHdr.ElUnits);
for (i=0; i<3; i++)
DEMExtract->Info[k].Res[i] = FCvt(DEMExtract->USGSHdr.Resolution[i]);
UTMZone = DEMExtract->Info[k].Zone;
UTMLatLonCoords_Init(DEMExtract->Convert, UTMZone);
for (i=0; i<4; i++)