forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIdrisiDataset.cpp
3375 lines (2897 loc) · 118 KB
/
IdrisiDataset.cpp
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
/*****************************************************************************
*
* Project: Idrisi Raster Image File Driver
* Purpose: Read/write Idrisi Raster Image Format RST
* Author: Ivan Lucena, [lucena_ivan at hotmail.com]
*
* Revised by Hongmei Zhu, February, 2013
* Clark Labs/Clark University
*
******************************************************************************
* Copyright( c ) 2006, Ivan Lucena
* Copyright (c) 2007-2012, Even Rouault <even dot rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files( the "Software" ),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_conv.h"
#include "cpl_string.h"
#include "cpl_csv.h"
#include "gdal_frmts.h"
#include "gdal_pam.h"
#include "gdal_alg.h"
#include "gdal_priv.h"
#include "gdal_rat.h"
#include "ogr_spatialref.h"
#include "idrisi.h"
#include "proj_experimental.h"
#include "ogr_proj_p.h"
#include <cmath>
#ifdef WIN32
#define PATHDELIM '\\'
#else
#define PATHDELIM '/'
#endif
//----- Safe numeric conversion, NULL as zero
#define atoi_nz(s) (s == nullptr ? (int)0 : atoi(s))
#define CPLAtof_nz(s) (s == nullptr ? (double)0.0 : CPLAtof(s))
//----- file extensions:
static const char *const extRST = "rst";
static const char *const extRDC = "rdc";
static const char *const extSMP = "smp";
static const char *const extREF = "ref";
// static const char * const extRSTu = "RST";
static const char *const extRDCu = "RDC";
static const char *const extSMPu = "SMP";
static const char *const extREFu = "REF";
//----- field names on rdc file:
static const char *const rdcFILE_FORMAT = "file format ";
static const char *const rdcFILE_TITLE = "file title ";
static const char *const rdcDATA_TYPE = "data type ";
static const char *const rdcFILE_TYPE = "file type ";
static const char *const rdcCOLUMNS = "columns ";
static const char *const rdcROWS = "rows ";
static const char *const rdcREF_SYSTEM = "ref. system ";
static const char *const rdcREF_UNITS = "ref. units ";
static const char *const rdcUNIT_DIST = "unit dist. ";
static const char *const rdcMIN_X = "min. X ";
static const char *const rdcMAX_X = "max. X ";
static const char *const rdcMIN_Y = "min. Y ";
static const char *const rdcMAX_Y = "max. Y ";
static const char *const rdcPOSN_ERROR = "pos'n error ";
static const char *const rdcRESOLUTION = "resolution ";
static const char *const rdcMIN_VALUE = "min. value ";
static const char *const rdcMAX_VALUE = "max. value ";
static const char *const rdcDISPLAY_MIN = "display min ";
static const char *const rdcDISPLAY_MAX = "display max ";
static const char *const rdcVALUE_UNITS = "value units ";
static const char *const rdcVALUE_ERROR = "value error ";
static const char *const rdcFLAG_VALUE = "flag value ";
static const char *const rdcFLAG_DEFN = "flag def'n ";
static const char *const rdcFLAG_DEFN2 = "flag def`n ";
static const char *const rdcLEGEND_CATS = "legend cats ";
static const char *const rdcLINEAGES = "lineage ";
static const char *const rdcCOMMENTS = "comment ";
static const char *const rdcCODE_N = "code %6d ";
//----- ".ref" file field names:
static const char *const refREF_SYSTEM = "ref. system ";
static const char *const refREF_SYSTEM2 = "ref.system ";
static const char *const refPROJECTION = "projection ";
static const char *const refDATUM = "datum ";
static const char *const refDELTA_WGS84 = "delta WGS84 ";
static const char *const refELLIPSOID = "ellipsoid ";
static const char *const refMAJOR_SAX = "major s-ax ";
static const char *const refMINOR_SAX = "minor s-ax ";
static const char *const refORIGIN_LONG = "origin long ";
static const char *const refORIGIN_LAT = "origin lat ";
static const char *const refORIGIN_X = "origin X ";
static const char *const refORIGIN_Y = "origin Y ";
static const char *const refSCALE_FAC = "scale fac ";
static const char *const refUNITS = "units ";
static const char *const refPARAMETERS = "parameters ";
static const char *const refSTANDL_1 = "stand ln 1 ";
static const char *const refSTANDL_2 = "stand ln 2 ";
//----- standard values:
static const char *const rstVERSION = "Idrisi Raster A.1";
static const char *const rstBYTE = "byte";
static const char *const rstINTEGER = "integer";
static const char *const rstREAL = "real";
static const char *const rstRGB24 = "rgb24";
static const char *const rstDEGREE = "deg";
static const char *const rstMETER = "m";
static const char *const rstLATLONG = "latlong";
static const char *const rstLATLONG2 = "lat/long";
static const char *const rstPLANE = "plane";
static const char *const rstUTM = "utm-%d%c";
static const char *const rstSPC = "spc%2d%2s%d";
//----- palette file( .smp ) header size:
constexpr int smpHEADERSIZE = 18;
//----- check if file exists:
bool FileExists(const char *pszPath);
//----- Reference Table
struct ReferenceTab
{
int nCode;
const char *pszName;
};
//----- USA State's reference table to USGS PCS Code
constexpr ReferenceTab aoUSStateTable[] = {
{101, "al"}, {201, "az"}, {301, "ar"}, {401, "ca"}, {501, "co"},
{600, "ct"}, {700, "de"}, {901, "fl"}, {1001, "ga"}, {1101, "id"},
{1201, "il"}, {1301, "in"}, {1401, "ia"}, {1501, "ks"}, {1601, "ky"},
{1701, "la"}, {1801, "me"}, {1900, "md"}, {2001, "ma"}, {2111, "mi"},
{2201, "mn"}, {2301, "ms"}, {2401, "mo"}, {2500, "mt"}, {2600, "ne"},
{2701, "nv"}, {2800, "nh"}, {2900, "nj"}, {3001, "nm"}, {3101, "ny"},
{3200, "nc"}, {3301, "nd"}, {3401, "oh"}, {3501, "ok"}, {3601, "or"},
{3701, "pa"}, {3800, "ri"}, {3900, "sc"}, {4001, "sd"}, {4100, "tn"},
{4201, "tx"}, {4301, "ut"}, {4400, "vt"}, {4501, "va"}, {4601, "wa"},
{4701, "wv"}, {4801, "wv"}, {4901, "wy"}, {5001, "ak"}, {5101, "hi"},
{5200, "pr"}};
//---- The origin table for USA State Plane Systems
struct OriginTab83
{
double longitude;
double latitude;
const char *spcs;
};
constexpr int ORIGIN_COUNT = 148;
//---- USA State plane coordinate system in IDRISI
static const OriginTab83 SPCS83Origin[] = {
{85.83, 30.50, "SPC83AL1"}, {87.50, 30.00, "SPC83AL2"},
{176.00, 51.00, "SPC83AK0"}, {142.00, 54.00, "SPC83AK2"},
{146.00, 54.00, "SPC83AK3"}, {150.00, 54.00, "SPC83AK4"},
{154.00, 54.00, "SPC83AK5"}, {158.00, 54.00, "SPC83AK6"},
{162.00, 54.00, "SPC83AK7"}, {166.00, 54.00, "SPC83AK8"},
{170.00, 54.00, "SPC83AK9"}, {110.17, 31.00, "SPC83AZ1"},
{111.92, 31.00, "SPC83AZ2"}, {113.75, 31.00, "SPC83AZ3"},
{92.00, 34.33, "SPC83AR1"}, {92.00, 32.67, "SPC83AR2"},
{122.00, 39.33, "SPC83CA1"}, {122.00, 37.67, "SPC83CA2"},
{120.50, 36.50, "SPC83CA3"}, {119.00, 35.33, "SPC83CA4"},
{118.00, 33.50, "SPC83CA5"}, {116.25, 32.17, "SPC83CA6"},
{105.50, 39.33, "SPC83CO1"}, {105.50, 37.83, "SPC83CO2"},
{105.50, 36.67, "SPC83CO3"}, {72.75, 40.83, "SPC83CT1"},
{75.42, 38.00, "SPC83DE1"}, {81.00, 24.33, "SPC83FL1"},
{82.00, 24.33, "SPC83FL2"}, {84.50, 29.00, "SPC83FL3"},
{82.17, 30.00, "SPC83GA1"}, {84.17, 30.00, "SPC83GA2"},
{155.50, 18.83, "SPC83HI1"}, {156.67, 20.33, "SPC83HI2"},
{158.00, 21.17, "SPC83HI3"}, {159.50, 21.83, "SPC83HI4"},
{160.17, 21.67, "SPC83HI5"}, {112.17, 41.67, "SPC83ID1"},
{114.00, 41.67, "SPC83ID2"}, {115.75, 41.67, "SPC83ID3"},
{88.33, 36.67, "SPC83IL1"}, {90.17, 36.67, "SPC83IL1"},
{85.67, 37.50, "SPC83IN1"}, {87.08, 37.50, "SPC83IN2"},
{93.50, 41.50, "SPC83IA1"}, {93.50, 40.00, "SPC83IA1"},
{98.00, 38.33, "SPC83KS1"}, {98.50, 36.67, "SPC83KS2"},
{84.25, 37.50, "SPC83KY1"}, {85.75, 36.33, "SPC83KY2"},
{92.50, 30.50, "SPC83LA1"}, {91.33, 28.50, "SPC83LA2"},
{91.33, 25.50, "SPC83LA3"}, {92.50, 30.67, "SPC27LA1"}, // NAD27 system
{91.33, 28.67, "SPC27LA2"}, {91.33, 25.67, "SPC27LA3"}, //
{68.50, 43.67, "SPC83ME1"}, {68.50, 43.83, "SPC27ME1"}, // NAD27
{70.17, 42.83, "SPC83ME2"}, {77.00, 37.67, "SPC83MD1"}, //
{77.00, 37.83, "SPC27MD1"}, // NAD27
{71.50, 41.00, "SPC83MA1"}, {70.50, 41.00, "SPC83MA2"},
{87.00, 44.78, "SPC83MI1"}, {84.37, 43.32, "SPC83MI2"},
{84.37, 41.50, "SPC83MI3"}, {84.33, 43.32, "SPC27MI2"}, // NAD27 L
{84.33, 41.50, "SPC27MI3"}, // NAD27 L
{83.67, 41.50, "SPC27MI4"}, // NAD27 TM
{85.75, 41.50, "SPC27MI5"}, // NAD27 TM
{88.75, 41.50, "SPC27MI6"}, // NAD27 TM
{93.10, 46.50, "SPC83MN1"}, {94.25, 45.00, "SPC83MN2"},
{94.00, 43.00, "SPC83MN3"}, {88.83, 29.50, "SPC83MS1"},
{90.33, 29.50, "SPC83MS2"}, {88.83, 29.67, "SPC83MS1"}, // NAD27
{90.33, 30.50, "SPC83MS2"}, //
{90.50, 35.83, "SPC83MO1"}, {92.50, 35.83, "SPC83MO2"},
{94.50, 36.17, "SPC83MO3"}, {109.50, 44.25, "SPC83MT1"},
{109.50, 47.00, "SPC27MT1"}, // NAD27
{109.50, 45.83, "SPC27MT2"}, {109.50, 44.00, "SPC27MT3"}, //
{100.00, 39.83, "SPC83NE1"}, {115.58, 34.75, "SPC83NV1"},
{116.67, 34.75, "SPC83NV2"}, {118.58, 34.75, "SPC83NV3"},
{71.67, 42.50, "SPC83NH1"}, {74.50, 38.83, "SPC83NJ1"},
{74.67, 38.83, "SPC27NJ1"}, // NAD27
{104.33, 31.00, "SPC83NM1"}, {106.25, 31.00, "SPC83NM2"},
{107.83, 31.00, "SPC83NM3"}, {74.50, 38.83, "SPC83NY1"},
{76.58, 40.00, "SPC83NY2"}, {78.58, 40.00, "SPC83NY3"},
{74.00, 40.17, "SPC83NY4"}, {74.33, 40.00, "SPC27NY1"}, // NAD27
{74.00, 40.50, "SPC27NY4"}, //
{79.00, 33.75, "SPC83NC1"}, {100.50, 47.00, "SPC83ND1"},
{100.50, 45.67, "SPC83ND2"}, {82.50, 39.67, "SPC83OH1"},
{82.50, 38.00, "SPC83OH2"}, {98.00, 35.00, "SPC83OK1"},
{98.00, 33.33, "SPC83OK2"}, {120.50, 43.67, "SPC83OR1"},
{120.50, 41.67, "SPC83OR2"}, {77.75, 40.17, "SPC83PA1"},
{77.75, 39.33, "SPC83PA2"}, {71.50, 41.08, "SPC83RI1"},
{81.00, 31.83, "SPC83SC1"}, {81.00, 33.00, "SPC27SC1"}, // NAD27
{81.00, 31.83, "SPC27SC2"}, // NAD27
{100.00, 43.83, "SPC83SD1"}, {100.33, 42.33, "SPC83SD2"},
{86.00, 34.33, "SPC83TN1"}, {86.00, 34.67, "SPC27TN1"}, // NAD27
{101.50, 34.00, "SPC83TX1"}, //
{98.50, 31.67, "SPC83TX2"}, {100.33, 29.67, "SPC83TX3"},
{99.00, 27.83, "SPC83TX4"}, {98.50, 25.67, "SPC83TX5"},
{97.50, 31.67, "SPC27TX2"}, // NAD27
{111.50, 40.33, "SPC83UT1"}, {111.50, 38.33, "SPC83UT2"},
{111.50, 36.67, "SPC83UT3"}, {72.50, 42.50, "SPC83VT1"},
{78.50, 37.67, "SPC83VA1"}, {78.50, 36.33, "SPC83VA2"},
{120.83, 47.00, "SPC83WA1"}, {120.50, 45.33, "SPC83WA2"},
{79.50, 38.50, "SPC83WV1"}, {81.00, 37.00, "SPC83WV2"},
{90.00, 45.17, "SPC83WI1"}, {90.00, 43.83, "SPC83WI2"},
{90.00, 42.00, "SPC83WI3"}, {105.17, 40.50, "SPC83WY1"},
{107.33, 40.50, "SPC83WY2"}, {108.75, 40.50, "SPC83WY3"},
{110.08, 40.50, "SPC83WY4"}, {105.17, 40.67, "SPC27WY1"}, // NAD27
{105.33, 40.67, "SPC27WY2"}, {108.75, 40.67, "SPC27WY3"},
{110.08, 40.67, "SPC27WY4"}, //
{66.43, 17.83, "SPC83PR1"}};
// Get IDRISI State Plane name by origin
char *GetSpcs(double dfLon, double dfLat);
// change NAD from 83 to 27
void NAD83to27(char *pszOutRef, char *pszInRef);
#define US_STATE_COUNT (sizeof(aoUSStateTable) / sizeof(ReferenceTab))
//----- Get the Code of a US State
int GetStateCode(const char *pszState);
//----- Get the state name of a Code
const char *GetStateName(int nCode);
//----- Conversion Table definition
struct ConversionTab
{
const char *pszName;
int nDefaultI;
int nDefaultG;
double dfConv;
};
//----- Linear Unit Conversion Table
static const ConversionTab aoLinearUnitsConv[] = {
{"m", /* 0 */ 0, 1, 1.0},
{SRS_UL_METER, /* 1 */ 0, 1, 1.0},
{"meters", /* 2 */ 0, 1, 1.0},
{"metre", /* 3 */ 0, 1, 1.0},
{"ft", /* 4 */ 4, 5, 0.3048},
{SRS_UL_FOOT, /* 5 */ 4, 5, 0.3048},
{"feet", /* 6 */ 4, 5, 0.3048},
{"foot_us", /* 7 */ 4, 5, 0.3048006},
{"u.s. foot", /* 8 */ 4, 5, 0.3048006},
{"mi", /* 9 */ 9, 10, 1612.9},
{"mile", /* 10 */ 9, 10, 1612.9},
{"miles", /* 11 */ 9, 10, 1612.9},
{"km", /* 12 */ 12, 13, 1000.0},
{"kilometers", /* 13 */ 12, 13, 1000.0},
{"kilometer", /* 14 */ 12, 13, 1000.0},
{"kilometre", /* 15 */ 12, 13, 1000.0},
{"deg", /* 16 */ 16, 17, 0.0},
{SRS_UA_DEGREE, /* 17 */ 16, 17, 0.0},
{"degrees", /* 18 */ 16, 17, 0.0},
{"rad", /* 19 */ 19, 20, 0.0},
{SRS_UA_RADIAN, /* 20 */ 19, 20, 0.0},
{"radians", /* 21 */ 19, 20, 0.0}};
#define LINEAR_UNITS_COUNT (sizeof(aoLinearUnitsConv) / sizeof(ConversionTab))
//----- Get the index of a given linear unit
static int GetUnitIndex(const char *pszUnitName);
//----- Get the default name
static char *GetUnitDefault(const char *pszUnitName,
const char *pszToMeter = nullptr);
//----- Get the "to meter"
static int GetToMeterIndex(const char *pszToMeter);
//----- CSLSaveCRLF
static int SaveAsCRLF(char **papszStrList, const char *pszFname);
/************************************************************************/
/* myCSLFetchNameValue() */
/************************************************************************/
static const char *myCSLFetchNameValue(char **papszStrList, const char *pszName)
{
if (papszStrList == nullptr || pszName == nullptr)
return nullptr;
size_t nLen = strlen(pszName);
while (nLen > 0 && pszName[nLen - 1] == ' ')
nLen--;
while (*papszStrList != nullptr)
{
if (EQUALN(*papszStrList, pszName, nLen))
{
size_t i;
for (i = nLen; (*papszStrList)[i] == ' '; ++i)
{
}
if ((*papszStrList)[i] == '=' || (*papszStrList)[i] == ':')
{
return (*papszStrList) + i + 1;
}
}
++papszStrList;
}
return nullptr;
}
/************************************************************************/
/* myCSLSetNameValueSeparator() */
/************************************************************************/
static void myCSLSetNameValueSeparator(char **papszList,
const char *pszSeparator)
{
const int nLines = CSLCount(papszList);
for (int iLine = 0; iLine < nLines; ++iLine)
{
char *pszSep = strchr(papszList[iLine], '=');
if (pszSep == nullptr)
pszSep = strchr(papszList[iLine], ':');
if (pszSep == nullptr)
continue;
*pszSep = '\0';
const char *pszKey = papszList[iLine];
const char *pszValue = pszSep + 1;
while (*pszValue == ' ')
pszValue++;
char *pszNewLine = static_cast<char *>(CPLMalloc(
strlen(pszValue) + strlen(pszKey) + strlen(pszSeparator) + 1));
strcpy(pszNewLine, pszKey);
strcat(pszNewLine, pszSeparator);
strcat(pszNewLine, pszValue);
CPLFree(papszList[iLine]);
papszList[iLine] = pszNewLine;
}
}
//----- Classes pre-definition:
class IdrisiRasterBand;
// ----------------------------------------------------------------------------
// Idrisi GDALDataset
// ----------------------------------------------------------------------------
class IdrisiDataset final : public GDALPamDataset
{
friend class IdrisiRasterBand;
private:
VSILFILE *fp;
char *pszFilename;
char *pszDocFilename;
char **papszRDC;
double adfGeoTransform[6];
mutable OGRSpatialReference m_oSRS{};
char **papszCategories;
char *pszUnitType;
// Move GeoReference2Wkt() into header file.
CPLErr Wkt2GeoReference(const OGRSpatialReference &oSRS,
char **pszRefSystem, char **pszRefUnit);
protected:
GDALColorTable *poColorTable;
public:
IdrisiDataset();
virtual ~IdrisiDataset();
static GDALDataset *Open(GDALOpenInfo *poOpenInfo);
static GDALDataset *Create(const char *pszFilename, int nXSize, int nYSize,
int nBands, GDALDataType eType,
char **papszOptions);
static GDALDataset *CreateCopy(const char *pszFilename,
GDALDataset *poSrcDS, int bStrict,
char **papszOptions,
GDALProgressFunc pfnProgress,
void *pProgressData);
virtual char **GetFileList(void) override;
virtual CPLErr GetGeoTransform(double *padfTransform) override;
virtual CPLErr SetGeoTransform(double *padfTransform) override;
const OGRSpatialReference *GetSpatialRef() const override;
CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override;
};
// ----------------------------------------------------------------------------
// Idrisi GDALPamRasterBand
// ----------------------------------------------------------------------------
class IdrisiRasterBand final : public GDALPamRasterBand
{
friend class IdrisiDataset;
GDALRasterAttributeTable *poDefaultRAT;
private:
int nRecordSize;
GByte *pabyScanLine;
public:
IdrisiRasterBand(IdrisiDataset *poDS, int nBand, GDALDataType eDataType);
virtual ~IdrisiRasterBand();
virtual double GetNoDataValue(int *pbSuccess = nullptr) override;
virtual double GetMinimum(int *pbSuccess = nullptr) override;
virtual double GetMaximum(int *pbSuccess = nullptr) override;
virtual CPLErr IReadBlock(int nBlockXOff, int nBlockYOff,
void *pImage) override;
virtual CPLErr IWriteBlock(int nBlockXOff, int nBlockYOff,
void *pImage) override;
virtual GDALColorTable *GetColorTable() override;
virtual GDALColorInterp GetColorInterpretation() override;
virtual char **GetCategoryNames() override;
virtual const char *GetUnitType() override;
virtual CPLErr SetCategoryNames(char **papszCategoryNames) override;
virtual CPLErr SetNoDataValue(double dfNoDataValue) override;
virtual CPLErr SetColorTable(GDALColorTable *poColorTable) override;
virtual CPLErr SetUnitType(const char *pszUnitType) override;
virtual CPLErr SetStatistics(double dfMin, double dfMax, double dfMean,
double dfStdDev) override;
CPLErr SetMinMax(double dfMin, double dfMax);
virtual GDALRasterAttributeTable *GetDefaultRAT() override;
virtual CPLErr SetDefaultRAT(const GDALRasterAttributeTable *) override;
float fMaximum;
float fMinimum;
bool bFirstVal;
};
// ------------------------------------------------------------------------ //
// Implementation of IdrisiDataset //
// ------------------------------------------------------------------------ //
/************************************************************************/
/* IdrisiDataset() */
/************************************************************************/
IdrisiDataset::IdrisiDataset()
: fp(nullptr), pszFilename(nullptr), pszDocFilename(nullptr),
papszRDC(nullptr), papszCategories(nullptr), pszUnitType(nullptr),
poColorTable(new GDALColorTable())
{
m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
adfGeoTransform[0] = 0.0;
adfGeoTransform[1] = 1.0;
adfGeoTransform[2] = 0.0;
adfGeoTransform[3] = 0.0;
adfGeoTransform[4] = 0.0;
adfGeoTransform[5] = 1.0;
}
/************************************************************************/
/* ~IdrisiDataset() */
/************************************************************************/
IdrisiDataset::~IdrisiDataset()
{
FlushCache(true);
if (papszRDC != nullptr && eAccess == GA_Update)
{
// int bSuccessMin = FALSE;
// int bSuccessMax = FALSE;
double dfMin = 0.0;
double dfMax = 0.0;
double dfMean = 0.0;
double dfStdDev = 0.0;
for (int i = 0; i < nBands; i++)
{
IdrisiRasterBand *poBand = (IdrisiRasterBand *)GetRasterBand(i + 1);
poBand->ComputeStatistics(false, &dfMin, &dfMax, &dfMean, &dfStdDev,
nullptr, nullptr);
/*
dfMin = poBand->GetMinimum( &bSuccessMin );
dfMax = poBand->GetMaximum( &bSuccessMax );
if( ! ( bSuccessMin && bSuccessMax ) )
{
poBand->GetStatistics( false, true, &dfMin, &dfMax, NULL, NULL );
}
*/
poBand->SetMinMax(dfMin, dfMax);
}
myCSLSetNameValueSeparator(papszRDC, ": ");
SaveAsCRLF(papszRDC, pszDocFilename);
}
CSLDestroy(papszRDC);
if (poColorTable)
{
delete poColorTable;
}
CPLFree(pszFilename);
CPLFree(pszDocFilename);
CSLDestroy(papszCategories);
CPLFree(pszUnitType);
if (fp != nullptr)
VSIFCloseL(fp);
}
/************************************************************************/
/* Open() */
/************************************************************************/
GDALDataset *IdrisiDataset::Open(GDALOpenInfo *poOpenInfo)
{
if ((poOpenInfo->fpL == nullptr) ||
(EQUAL(CPLGetExtension(poOpenInfo->pszFilename), extRST) ==
FALSE)) // modified
return nullptr;
// --------------------------------------------------------------------
// Check the documentation file .rdc
// --------------------------------------------------------------------
const char *pszLDocFilename =
CPLResetExtension(poOpenInfo->pszFilename, extRDC);
if (!FileExists(pszLDocFilename))
{
pszLDocFilename = CPLResetExtension(poOpenInfo->pszFilename, extRDCu);
if (!FileExists(pszLDocFilename))
{
return nullptr;
}
}
char **papszLRDC = CSLLoad(pszLDocFilename);
myCSLSetNameValueSeparator(papszLRDC, ":");
const char *pszVersion = myCSLFetchNameValue(papszLRDC, rdcFILE_FORMAT);
if (pszVersion == nullptr || !EQUAL(pszVersion, rstVERSION))
{
CSLDestroy(papszLRDC);
return nullptr;
}
// --------------------------------------------------------------------
// Create a corresponding GDALDataset
// --------------------------------------------------------------------
IdrisiDataset *poDS = new IdrisiDataset();
poDS->eAccess = poOpenInfo->eAccess;
poDS->pszFilename = CPLStrdup(poOpenInfo->pszFilename);
if (poOpenInfo->eAccess == GA_ReadOnly)
{
poDS->fp = VSIFOpenL(poDS->pszFilename, "rb");
}
else
{
poDS->fp = VSIFOpenL(poDS->pszFilename, "r+b");
}
if (poDS->fp == nullptr)
{
CSLDestroy(papszLRDC);
delete poDS;
return nullptr;
}
poDS->pszDocFilename = CPLStrdup(pszLDocFilename);
poDS->papszRDC = CSLDuplicate(papszLRDC);
CSLDestroy(papszLRDC);
// --------------------------------------------------------------------
// Load information from rdc
// --------------------------------------------------------------------
poDS->nRasterXSize =
atoi_nz(myCSLFetchNameValue(poDS->papszRDC, rdcCOLUMNS));
poDS->nRasterYSize = atoi_nz(myCSLFetchNameValue(poDS->papszRDC, rdcROWS));
if (!GDALCheckDatasetDimensions(poDS->nRasterXSize, poDS->nRasterYSize))
{
delete poDS;
return nullptr;
}
// --------------------------------------------------------------------
// Create band information
// --------------------------------------------------------------------
const char *pszDataType = myCSLFetchNameValue(poDS->papszRDC, rdcDATA_TYPE);
if (pszDataType == nullptr)
{
delete poDS;
return nullptr;
}
if (EQUAL(pszDataType, rstBYTE))
{
poDS->nBands = 1;
poDS->SetBand(1, new IdrisiRasterBand(poDS, 1, GDT_Byte));
}
else if (EQUAL(pszDataType, rstINTEGER))
{
poDS->nBands = 1;
poDS->SetBand(1, new IdrisiRasterBand(poDS, 1, GDT_Int16));
}
else if (EQUAL(pszDataType, rstREAL))
{
poDS->nBands = 1;
poDS->SetBand(1, new IdrisiRasterBand(poDS, 1, GDT_Float32));
}
else if (EQUAL(pszDataType, rstRGB24))
{
poDS->nBands = 3;
poDS->SetBand(1, new IdrisiRasterBand(poDS, 1, GDT_Byte));
poDS->SetBand(2, new IdrisiRasterBand(poDS, 2, GDT_Byte));
poDS->SetBand(3, new IdrisiRasterBand(poDS, 3, GDT_Byte));
}
else
{
CPLError(CE_Failure, CPLE_AppDefined, "Unknown data type : %s",
pszDataType);
delete poDS;
return nullptr;
}
for (int i = 0; i < poDS->nBands; i++)
{
IdrisiRasterBand *band = (IdrisiRasterBand *)poDS->GetRasterBand(i + 1);
if (band->pabyScanLine == nullptr)
{
delete poDS;
return nullptr;
}
}
// --------------------------------------------------------------------
// Load the transformation matrix
// --------------------------------------------------------------------
const char *pszMinX = myCSLFetchNameValue(poDS->papszRDC, rdcMIN_X);
const char *pszMaxX = myCSLFetchNameValue(poDS->papszRDC, rdcMAX_X);
const char *pszMinY = myCSLFetchNameValue(poDS->papszRDC, rdcMIN_Y);
const char *pszMaxY = myCSLFetchNameValue(poDS->papszRDC, rdcMAX_Y);
const char *pszUnit = myCSLFetchNameValue(poDS->papszRDC, rdcUNIT_DIST);
if (pszMinX != nullptr && strlen(pszMinX) > 0 && pszMaxX != nullptr &&
strlen(pszMaxX) > 0 && pszMinY != nullptr && strlen(pszMinY) > 0 &&
pszMaxY != nullptr && strlen(pszMaxY) > 0 && pszUnit != nullptr &&
strlen(pszUnit) > 0)
{
double dfMinX, dfMaxX, dfMinY, dfMaxY, dfUnit, dfXPixSz, dfYPixSz;
dfMinX = CPLAtof_nz(pszMinX);
dfMaxX = CPLAtof_nz(pszMaxX);
dfMinY = CPLAtof_nz(pszMinY);
dfMaxY = CPLAtof_nz(pszMaxY);
dfUnit = CPLAtof_nz(pszUnit);
dfMinX = dfMinX * dfUnit;
dfMaxX = dfMaxX * dfUnit;
dfMinY = dfMinY * dfUnit;
dfMaxY = dfMaxY * dfUnit;
dfYPixSz = (dfMinY - dfMaxY) / poDS->nRasterYSize;
dfXPixSz = (dfMaxX - dfMinX) / poDS->nRasterXSize;
poDS->adfGeoTransform[0] = dfMinX;
poDS->adfGeoTransform[1] = dfXPixSz;
poDS->adfGeoTransform[2] = 0.0;
poDS->adfGeoTransform[3] = dfMaxY;
poDS->adfGeoTransform[4] = 0.0;
poDS->adfGeoTransform[5] = dfYPixSz;
}
// --------------------------------------------------------------------
// Set Color Table in the presence of a smp file
// --------------------------------------------------------------------
if (poDS->nBands != 3)
{
const char *pszSMPFilename =
CPLResetExtension(poDS->pszFilename, extSMP);
VSILFILE *fpSMP = VSIFOpenL(pszSMPFilename, "rb");
if (fpSMP != nullptr)
{
int dfMaxValue =
atoi_nz(myCSLFetchNameValue(poDS->papszRDC, rdcMAX_VALUE));
int nCatCount =
atoi_nz(myCSLFetchNameValue(poDS->papszRDC, rdcLEGEND_CATS));
if (nCatCount == 0)
dfMaxValue = 255;
VSIFSeekL(fpSMP, smpHEADERSIZE, SEEK_SET);
GDALColorEntry oEntry;
unsigned char aucRGB[3];
int i = 0;
while ((VSIFReadL(&aucRGB, sizeof(aucRGB), 1, fpSMP)) &&
(i <= dfMaxValue))
{
oEntry.c1 = (short)aucRGB[0];
oEntry.c2 = (short)aucRGB[1];
oEntry.c3 = (short)aucRGB[2];
oEntry.c4 = (short)255;
poDS->poColorTable->SetColorEntry(i, &oEntry);
i++;
}
VSIFCloseL(fpSMP);
}
}
// --------------------------------------------------------------------
// Check for Unit Type
// --------------------------------------------------------------------
const char *pszValueUnit =
myCSLFetchNameValue(poDS->papszRDC, rdcVALUE_UNITS);
if (pszValueUnit == nullptr)
poDS->pszUnitType = CPLStrdup("unspecified");
else
{
if (STARTS_WITH_CI(pszValueUnit, "meter"))
{
poDS->pszUnitType = CPLStrdup("m");
}
else if (STARTS_WITH_CI(pszValueUnit, "feet"))
{
poDS->pszUnitType = CPLStrdup("ft");
}
else
poDS->pszUnitType = CPLStrdup(pszValueUnit);
}
// --------------------------------------------------------------------
// Check for category names.
// --------------------------------------------------------------------
int nCatCount =
atoi_nz(myCSLFetchNameValue(poDS->papszRDC, rdcLEGEND_CATS));
if (nCatCount > 0)
{
// ----------------------------------------------------------------
// Sequentialize categories names, from 0 to the last "code n"
// ----------------------------------------------------------------
int nLine = -1;
for (int i = 0; (i < CSLCount(poDS->papszRDC)) && (nLine == -1); i++)
if (EQUALN(poDS->papszRDC[i], rdcLEGEND_CATS, 11))
nLine = i; // get the line where legend cats is
if (nLine > 0)
{
int nCode = 0;
int nCount = 0;
sscanf(poDS->papszRDC[++nLine], rdcCODE_N,
&nCode); // assign legend cats to nCode
for (int i = 0; (i < 255) && (nCount < nCatCount); i++)
{
if (i == nCode)
{
poDS->papszCategories = CSLAddString(
poDS->papszCategories,
CPLParseNameValue(poDS->papszRDC[nLine], nullptr));
nCount++;
if (nCount < nCatCount)
sscanf(poDS->papszRDC[++nLine], rdcCODE_N, &nCode);
}
else
poDS->papszCategories =
CSLAddString(poDS->papszCategories, "");
}
}
}
/* -------------------------------------------------------------------- */
/* Automatic Generated Color Table */
/* -------------------------------------------------------------------- */
if (poDS->papszCategories != nullptr &&
(poDS->poColorTable->GetColorEntryCount() == 0))
{
int nEntryCount = CSLCount(poDS->papszCategories);
GDALColorEntry sFromColor;
sFromColor.c1 = (short)(255);
sFromColor.c2 = (short)(0);
sFromColor.c3 = (short)(0);
sFromColor.c4 = (short)(255);
GDALColorEntry sToColor;
sToColor.c1 = (short)(0);
sToColor.c2 = (short)(0);
sToColor.c3 = (short)(255);
sToColor.c4 = (short)(255);
poDS->poColorTable->CreateColorRamp(0, &sFromColor, (nEntryCount - 1),
&sToColor);
}
/* -------------------------------------------------------------------- */
/* Initialize any PAM information. */
/* -------------------------------------------------------------------- */
poDS->SetDescription(poOpenInfo->pszFilename);
poDS->TryLoadXML();
/* -------------------------------------------------------------------- */
/* Check for external overviews. */
/* -------------------------------------------------------------------- */
poDS->oOvManager.Initialize(poDS, poOpenInfo->pszFilename);
return poDS;
}
/************************************************************************/
/* Create() */
/************************************************************************/
GDALDataset *IdrisiDataset::Create(const char *pszFilename, int nXSize,
int nYSize, int nBandsIn, GDALDataType eType,
char ** /* papszOptions */)
{
// --------------------------------------------------------------------
// Check input options
// --------------------------------------------------------------------
if (nBandsIn != 1 && nBandsIn != 3)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Attempt to create IDRISI dataset with an illegal number of "
"bands(%d)."
" Try again by selecting a specific band if possible. \n",
nBandsIn);
return nullptr;
}
if (nBandsIn == 3 && eType != GDT_Byte)
{
CPLError(
CE_Failure, CPLE_AppDefined,
"Attempt to create IDRISI dataset with an unsupported combination "
"of the number of bands(%d) and data type(%s). \n",
nBandsIn, GDALGetDataTypeName(eType));
return nullptr;
}
// ----------------------------------------------------------------
// Create the header file with minimum information
// ----------------------------------------------------------------
const char *pszLDataType = nullptr;
switch (eType)
{
case GDT_Byte:
if (nBandsIn == 1)
pszLDataType = rstBYTE;
else
pszLDataType = rstRGB24;
break;
case GDT_Int16:
pszLDataType = rstINTEGER;
break;
case GDT_Float32:
pszLDataType = rstREAL;
break;
//--- process compatible data types
case (GDT_UInt16):
pszLDataType = rstINTEGER;
CPLError(CE_Warning, CPLE_AppDefined,
"This process requires a conversion from %s to signed "
"16-bit %s, "
"which may cause data loss.\n",
GDALGetDataTypeName(eType), rstINTEGER);
break;
case GDT_UInt32:
pszLDataType = rstINTEGER;
CPLError(CE_Warning, CPLE_AppDefined,
"This process requires a conversion from %s to signed "
"16-bit %s, "
"which may cause data loss.\n",
GDALGetDataTypeName(eType), rstINTEGER);
break;
case GDT_Int32:
pszLDataType = rstINTEGER;
CPLError(CE_Warning, CPLE_AppDefined,
"This process requires a conversion from %s to signed "
"16-bit %s, "
"which may cause data loss.\n",
GDALGetDataTypeName(eType), rstINTEGER);
break;
case GDT_Float64:
pszLDataType = rstREAL;
CPLError(CE_Warning, CPLE_AppDefined,
"This process requires a conversion from %s to float "
"32-bit %s, "
"which may cause data loss.\n",
GDALGetDataTypeName(eType), rstREAL);
break;
default:
CPLError(CE_Failure, CPLE_AppDefined,
"Attempt to create IDRISI dataset with an illegal "
"data type(%s).\n",
GDALGetDataTypeName(eType));
return nullptr;
};
char **papszLRDC = nullptr;
papszLRDC = CSLAddNameValue(papszLRDC, rdcFILE_FORMAT, rstVERSION);
papszLRDC = CSLAddNameValue(papszLRDC, rdcFILE_TITLE, "");
papszLRDC = CSLAddNameValue(papszLRDC, rdcDATA_TYPE, pszLDataType);
papszLRDC = CSLAddNameValue(papszLRDC, rdcFILE_TYPE, "binary");
papszLRDC =
CSLAddNameValue(papszLRDC, rdcCOLUMNS, CPLSPrintf("%d", nXSize));
papszLRDC = CSLAddNameValue(papszLRDC, rdcROWS, CPLSPrintf("%d", nYSize));
papszLRDC = CSLAddNameValue(papszLRDC, rdcREF_SYSTEM, "plane");
papszLRDC = CSLAddNameValue(papszLRDC, rdcREF_UNITS, "m");
papszLRDC = CSLAddNameValue(papszLRDC, rdcUNIT_DIST, "1");
papszLRDC = CSLAddNameValue(papszLRDC, rdcMIN_X, "0");
papszLRDC = CSLAddNameValue(papszLRDC, rdcMAX_X, CPLSPrintf("%d", nXSize));
papszLRDC = CSLAddNameValue(papszLRDC, rdcMIN_Y, "0");
papszLRDC = CSLAddNameValue(papszLRDC, rdcMAX_Y, CPLSPrintf("%d", nYSize));
papszLRDC = CSLAddNameValue(papszLRDC, rdcPOSN_ERROR, "unspecified");
papszLRDC = CSLAddNameValue(papszLRDC, rdcRESOLUTION, "1.0");
papszLRDC = CSLAddNameValue(papszLRDC, rdcMIN_VALUE, "0");
papszLRDC = CSLAddNameValue(papszLRDC, rdcMAX_VALUE, "0");
papszLRDC = CSLAddNameValue(papszLRDC, rdcDISPLAY_MIN, "0");
papszLRDC = CSLAddNameValue(papszLRDC, rdcDISPLAY_MAX, "0");
papszLRDC = CSLAddNameValue(papszLRDC, rdcVALUE_UNITS, "unspecified");
papszLRDC = CSLAddNameValue(papszLRDC, rdcVALUE_ERROR, "unspecified");
papszLRDC = CSLAddNameValue(papszLRDC, rdcFLAG_VALUE, "none");
papszLRDC = CSLAddNameValue(papszLRDC, rdcFLAG_DEFN, "none");
papszLRDC = CSLAddNameValue(papszLRDC, rdcLEGEND_CATS, "0");
papszLRDC = CSLAddNameValue(papszLRDC, rdcLINEAGES, "");
papszLRDC = CSLAddNameValue(papszLRDC, rdcCOMMENTS, "");
const char *pszLDocFilename = CPLResetExtension(pszFilename, extRDC);
myCSLSetNameValueSeparator(papszLRDC, ": ");
SaveAsCRLF(papszLRDC, pszLDocFilename);
CSLDestroy(papszLRDC);
// ----------------------------------------------------------------
// Create an empty data file
// ----------------------------------------------------------------
VSILFILE *fp = VSIFOpenL(pszFilename, "wb+");
if (fp == nullptr)
{