forked from ossimlabs/ossim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathossimNitfCommon.cpp
More file actions
545 lines (482 loc) · 14.4 KB
/
Copy pathossimNitfCommon.cpp
File metadata and controls
545 lines (482 loc) · 14.4 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
//----------------------------------------------------------------------------
//
// License: LGPL
//
// See LICENSE.txt file in the top level directory for more details.
//
// Author: David Burken
//
// Description: Utility class for global nitf methods.
//
//----------------------------------------------------------------------------
// $Id: ossimNitfCommon.cpp 17978 2010-08-24 16:17:00Z dburken $
#include <cstring> /* for memcpy */
#include <sstream>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#include <iostream>
#include <ossim/support_data/ossimNitfCommon.h>
#include <ossim/base/ossimDms.h>
#include <ossim/base/ossimDpt.h>
#include <ossim/base/ossimIrect.h>
#include <ossim/base/ossimNotify.h>
#include <ossim/base/ossimTrace.h>
static const ossimTrace traceDebug(ossimString("ossimNitfCommon:debug"));
ossimNitfCommon::ossimNitfCommon(){}
ossimNitfCommon::~ossimNitfCommon(){}
ossimString ossimNitfCommon::convertToScientificString(
const ossim_float64& aValue,
ossim_uint32 size)
{
// Precision cannot hit 0 for this to work...
if ( ((aValue < 0.0) && (size < 8)) ||
((aValue >= 0.0) && (size < 7)) )
{
if (traceDebug())
{
ossimNotify(ossimNotifyLevel_DEBUG)
<< "ossimNitfCommon::convertToScientificString DEBUG:"
<< "\nsize range error!"
<< std::endl;
}
return ossimString();
}
//--
// Set the precision to account for size with 1.xxxxxE+01
//---
ossim_uint32 precision = size - 6;
if (aValue < 0.0)
{
--precision;
}
std::ostringstream s1;
s1 << std::setiosflags(std::ios_base::scientific|std::ios_base::internal)
<< std::setfill('0')
// << setw(size)
<< std::setprecision(precision)
<< aValue;
ossimString result = s1.str();
// Upcase the "e".
result.upcase();
if (traceDebug())
{
if (result.size() != size)
{
ossimNotify(ossimNotifyLevel_DEBUG)
<< "ossimNitfCommon::convertToScientificString DEBUG:"
<< "\nIncorrect output size!"
<< "\nValue: " << aValue
<< "\nString: " << result
<< std::endl;
}
}
return result;
}
ossimString ossimNitfCommon::convertToDoubleString(
const ossim_float64& aValue,
ossim_uint32 precision,
ossim_uint32 size)
{
ossim_uint32 width = size;
std::ostringstream s1;
s1 << std::setiosflags(std::ios_base::fixed|
std::ios_base::internal)
<< std::setprecision(precision)
<< std::setfill('0')
<< std::setw(width)
<< aValue;
ossimString result = s1.str();
if (traceDebug())
{
ossimNotify(ossimNotifyLevel_DEBUG)
<< "ossimNitfCommon::convertToDoubleString DEBUG:"
<< "\nresult: " << result << std::endl;
if (result.size() != size)
{
ossimNotify(ossimNotifyLevel_DEBUG)
<< "ossimNitfCommon::convertToDoubleString DEBUG:"
<< "\nIncorrect output size!"
<< std::endl;
}
}
return result;
}
ossimString ossimNitfCommon::convertToUIntString(ossim_uint32 aValue,
ossim_uint32 size)
{
ossim_uint32 width = size;
std::ostringstream s1;
s1 << std::setiosflags(std::ios_base::fixed|
std::ios_base::internal)
<< std::setfill('0')
<< std::setw(width)
<< aValue;
ossimString result = s1.str();
if (traceDebug())
{
if (result.size() != size)
{
ossimNotify(ossimNotifyLevel_DEBUG)
<< "ossimNitfCommon::convertToUIntString DEBUG:"
<< "\nIncorrect output size!"
<< "\nValue: " << aValue
<< "\nString: " << result
<< std::endl;
}
}
return result;
}
ossimString ossimNitfCommon::convertToIntString(ossim_int32 aValue,
ossim_uint32 size)
{
ossim_uint32 width = size;
std::ostringstream s1;
s1 << std::setiosflags(std::ios_base::fixed|
std::ios_base::internal)
<< std::setfill('0')
<< std::setw(width)
<< aValue;
ossimString result = s1.str();
if (traceDebug())
{
if (result.size() != size)
{
ossimNotify(ossimNotifyLevel_DEBUG)
<< "ossimNitfCommon::convertToIntString DEBUG:"
<< "\nIncorrect output size!"
<< "\nValue: " << aValue
<< "\nString: " << result
<< std::endl;
}
}
return result;
}
ossimString ossimNitfCommon::convertToDdLatLonLocString(const ossimDpt& pt,
ossim_uint32 precision)
{
ossimString lat;
ossimString lon;
ossim_uint32 latSize = precision+4; // size = precision + "-33."
ossim_uint32 lonSize = precision+5; // size = precision + "-122."
if (pt.y >= 0.0)
{
lat = "+";
--latSize;
}
if (pt.x >= 0.0)
{
lon = "+";
--lonSize;
}
lat += convertToDoubleString(pt.y,
precision,
latSize);
lon += convertToDoubleString(pt.x,
precision,
lonSize);
ossimString result = lat+lon;
if (traceDebug())
{
ossimNotify(ossimNotifyLevel_DEBUG)
<< "ossimNitfCommon::convertToDdLatLonLocString DEBUG:"
<< "\nresult: " << result
<< std::endl;
}
return result;
}
void ossimNitfCommon::setField(void* fieldDestination,
const ossimString& src,
std::streamsize width,
std::ios_base::fmtflags ioflags,
char fill)
{
std::ostringstream out;
out << std::setw(width)
<< std::setfill(fill)
<< std::setiosflags(ioflags)
<< src.trim().c_str();
memcpy(fieldDestination, out.str().c_str(), width);
}
ossimString ossimNitfCommon::encodeUtm(
ossim_uint32 zone,
const ossimDpt& ul,
const ossimDpt& ur,
const ossimDpt& lr,
const ossimDpt& ll)
{
std::ostringstream out;
if(zone > 60)
{
std::string s = "ossimNitfImageHeaderV2_1::encodeUtm: ERROR\nUTM zone greate than 60!";
if (traceDebug())
{
ossimNotify(ossimNotifyLevel_WARN) << s << std::endl;
}
throw std::out_of_range(s);
}
ossim_float64 east = ul.x;
ossim_float64 north = ul.y;
if((ossim_uint32)(east+.5) > 999999)
{
std::string s = "ossimNitfImageHeaderV2_1::encodeUtm: ERROR\nUpper left easting too large for NITF field!";
if (traceDebug())
{
ossimNotify(ossimNotifyLevel_WARN) << s << std::endl;
}
throw std::out_of_range(s);
}
if((ossim_uint32)(north+.5) > 9999999)
{
std::string s = "ossimNitfImageHeaderV2_1::encodeUtm: ERROR\nUpper left northing too large for NITF field!";
if (traceDebug())
{
ossimNotify(ossimNotifyLevel_WARN) << s << std::endl;
}
throw std::out_of_range(s);
}
out << std::setw(2)
<< std::setfill('0')
<< zone
<< std::setw(6)
<< std::setfill('0')
<<(ossim_uint32)(east+.5)
<< std::setw(7)
<< std::setfill('0')
<<(ossim_uint32)(north+.5);
east = ur.x;
north = ur.y;
if((ossim_uint32)(east+.5) > 999999)
{
std::string s = "ossimNitfImageHeaderV2_1::encodeUtm: ERROR\nUpper right easting too large for NITF field!";
if (traceDebug())
{
ossimNotify(ossimNotifyLevel_WARN) << s << std::endl;
}
throw std::out_of_range(s);
}
if((ossim_uint32)(north+.5) > 9999999)
{
std::string s = "ossimNitfImageHeaderV2_1::encodeUtm: ERROR\nUpper right northing too large for NITF field!";
if (traceDebug())
{
ossimNotify(ossimNotifyLevel_WARN) << s << std::endl;
}
throw std::out_of_range(s);
}
out << std::setw(2)
<< std::setfill('0')
<< zone
<< std::setw(6)
<< std::setfill('0')
<<(ossim_uint32)(east+.5)
<< std::setw(7)
<< std::setfill('0')
<<(ossim_uint32)(north+.5);
east = lr.x;
north = lr.y;
if((ossim_uint32)(east+.5) > 999999)
{
std::string s = "ossimNitfImageHeaderV2_1::encodeUtm: ERROR\nLower right easting too large for NITF field!";
if (traceDebug())
{
ossimNotify(ossimNotifyLevel_WARN) << s << std::endl;
}
throw std::out_of_range(s);
}
if((ossim_uint32)(north+.5) > 9999999)
{
std::string s = "ossimNitfImageHeaderV2_1::encodeUtm: ERROR\nLower right northing too large for NITF field!";
if (traceDebug())
{
ossimNotify(ossimNotifyLevel_WARN) << s << std::endl;
}
throw std::out_of_range(s);
}
out << std::setw(2)
<< std::setfill('0')
<< zone
<< std::setw(6)
<< std::setfill('0')
<<(ossim_uint32)(east+.5)
<< std::setw(7)
<< std::setfill('0')
<<(ossim_uint32)(north+.5);
east = ll.x;
north = ll.y;
if((ossim_uint32)(east+.5) > 999999)
{
std::string s = "ossimNitfImageHeaderV2_1::encodeUtm: ERROR\nLower left easting too large for NITF field!";
if (traceDebug())
{
ossimNotify(ossimNotifyLevel_WARN) << s << std::endl;
}
throw std::out_of_range(s);
}
if((ossim_uint32)(north+.5) > 9999999)
{
std::string s = "ossimNitfImageHeaderV2_1::encodeUtm: ERROR\nLower left northing too large for NITF field!";
if (traceDebug())
{
ossimNotify(ossimNotifyLevel_WARN) << s << std::endl;
}
throw std::out_of_range(s);
}
out << std::setw(2)
<< std::setfill('0')
<< zone
<< std::setw(6)
<< std::setfill('0')
<<(ossim_uint32)(east+.5)
<< std::setw(7)
<< std::setfill('0')
<<(ossim_uint32)(north+.5);
return out.str().c_str();
}
ossimString ossimNitfCommon::encodeGeographicDms(const ossimDpt& ul,
const ossimDpt& ur,
const ossimDpt& lr,
const ossimDpt& ll)
{
std::ostringstream out;
out << ossimDms(ul.y, true).toString("ddmmssC").c_str();
out << ossimDms(ul.x, false).toString("dddmmssC").c_str();
out << ossimDms(ur.y, true).toString("ddmmssC").c_str();
out << ossimDms(ur.x, false).toString("dddmmssC").c_str();
out << ossimDms(lr.y, true).toString("ddmmssC").c_str();
out << ossimDms(lr.x, false).toString("dddmmssC").c_str();
out << ossimDms(ll.y, true).toString("ddmmssC").c_str();
out << ossimDms(ll.x, false).toString("dddmmssC").c_str();
return ossimString(out.str());
}
ossimString ossimNitfCommon::encodeGeographicDecimalDegrees(const ossimDpt& ul,
const ossimDpt& ur,
const ossimDpt& lr,
const ossimDpt& ll)
{
std::ostringstream out;
out << (ul.lat >= 0.0?"+":"")
<< std::setw(6)
<< std::setfill('0')
<< std::setprecision(3)
<< std::setiosflags(std::ios::fixed)
<< ul.lat
<< (ul.lon >= 0.0?"+":"")
<< std::setw(7)
<< std::setfill('0')
<< std::setprecision(3)
<< std::setiosflags(std::ios::fixed)
<< ul.lon;
out << (ur.lat >= 0.0?"+":"")
<< std::setw(6)
<< std::setfill('0')
<< std::setprecision(3)
<< std::setiosflags(std::ios::fixed)
<< ur.lat
<< (ur.lon >= 0.0?"+":"")
<< std::setw(7)
<< std::setfill('0')
<< std::setprecision(3)
<< std::setiosflags(std::ios::fixed)
<< ur.lon;
out << (lr.lat >= 0.0?"+":"")
<< std::setw(6)
<< std::setfill('0')
<< std::setprecision(3)
<< std::setiosflags(std::ios::fixed)
<< lr.lat
<< (lr.lon >= 0.0?"+":"")
<< std::setw(7)
<< std::setfill('0')
<< std::setprecision(3)
<< std::setiosflags(std::ios::fixed)
<< lr.lon;
out << (ll.lat >= 0.0?"+":"")
<< std::setw(6)
<< std::setfill('0')
<< std::setprecision(3)
<< std::setiosflags(std::ios::fixed)
<< ll.lat
<< (ll.lon >= 0.0?"+":"")
<< std::setw(7)
<< std::setfill('0')
<< std::setprecision(3)
<< std::setiosflags(std::ios::fixed)
<< ll.lon;
return ossimString(out.str());
}
ossimString ossimNitfCommon::getNitfPixelType(ossimScalarType scalarType)
{
ossimString pixelType;
switch(scalarType)
{
case OSSIM_UINT8:
case OSSIM_USHORT11:
case OSSIM_USHORT12:
case OSSIM_USHORT13:
case OSSIM_USHORT14:
case OSSIM_USHORT15:
case OSSIM_UINT16:
case OSSIM_UINT32:
{
pixelType = "INT";
break;
}
case OSSIM_SINT16:
case OSSIM_SINT32:
{
pixelType = "SI";
break;
}
case OSSIM_FLOAT:
case OSSIM_NORMALIZED_FLOAT:
case OSSIM_DOUBLE:
case OSSIM_NORMALIZED_DOUBLE:
{
pixelType = "R";
break;
}
default:
{
ossimNotify(ossimNotifyLevel_DEBUG)
<< __FILE__ << ":" << __LINE__
<< "\nUnhandled scalar type: " << scalarType << std::endl;
break;
}
}
return pixelType;
}
ossimString ossimNitfCommon::getCompressionRate(const ossimIrect& rect,
ossim_uint32 bands,
ossimScalarType scalar,
ossim_uint64 lengthInBytes)
{
ossimString result("");
ossim_float64 uncompressedSize =
ossim::scalarSizeInBytes(scalar) * rect.width() * rect.height() * bands;
ossim_float64 bitsPerPix = ossim::getBitsPerPixel(scalar);
ossim_float64 rate = ( bitsPerPix *
(static_cast<ossim_float64>(lengthInBytes) /
uncompressedSize) );
// Multiply by 100 as there is an implied decimal point.
rate *= 100.0;
// Convert to string with zero precision.
ossimString s = ossimString::toString(rate, 0, 4);
if (s.size())
{
if (s.size() <= 3)
{
result = "N";
if (s.size() == 3)
{
result = "0";
}
else if (s.size() == 2)
{
result = "00";
}
result += s;
}
}
return result;
}