vil_nitf2_compound_field_value.cxx
Go to the documentation of this file.
1 // vil_nitf2: Written by Harry Voorhees (hlv@) and Rob Radtke (rob@) of
2 // Stellar Science Ltd. Co. (stellarscience.com) for
3 // Air Force Research Laboratory, 2005.
4 
5 #include <iomanip>
6 #include <string>
8 
9 // not used? #include <sstream>
10 #ifdef _MSC_VER
11 # include <vcl_msvc_warnings.h>
12 #endif
13 
15 
16 //==============================================================================
17 // Class vil_nitf2_date_time
18 
19 std::ostream& vil_nitf2_date_time::output(std::ostream& os) const
20 {
21  os << year << '/'
22  << std::setw(2) << std::setfill('0') << month << '/'
23  << std::setw(2) << std::setfill('0') << day << ' '
24  << std::setw(2) << std::setfill('0') << hour << ':'
25  << std::setw(2) << std::setfill('0') << minute << ':';
26  if (second < 10) os << '0';
27  if (sec_precision==0) {
28  os << int(second);
29  } else if (sec_precision>0) {
30  os << std::fixed << std::setprecision(sec_precision) << second;
31  }
32  return os;
33 }
34 
36 {
37  return year > 1900 && year <= 9999 &&
38  month > 0 && month <= 12 &&
39  day > 0 && day <= 31 &&
40  hour >= 0 && hour < 24 &&
41  minute >=0 && minute < 60 &&
42  second >= 0.0 && second < 60.0;
43 }
44 
45 bool vil_nitf2_date_time::write(std::ostream& output, int field_width) const
46 {
47  output << std::setw(4) << std::noshowpos << std::internal << year
48  << std::setw(2) << std::noshowpos << std::internal << std::setfill('0') << month
49  << std::setw(2) << std::noshowpos << std::internal << std::setfill('0') << day;
50  if (field_width >= 10 && !output.fail()) {
51  output << std::setw(2) << std::noshowpos << std::internal << std::setfill('0') << hour;
52  }
53  else
54  output << " ";
55  if (field_width >= 12 && !output.fail()) {
56  output << std::setw(2) << std::noshowpos << std::internal << std::setfill('0') << minute;
57  }
58  else
59  output << " ";
60  if (field_width < 14 && !output.fail()) {
61  // seconds not displayed
62  output << " ";
63  } else if (field_width == 14 && !output.fail()) {
64  // display integer seconds
65  output << std::setw(2) << std::noshowpos << std::internal << std::setfill('0') << (int)second;
66  } else if (!output.fail()) {
67  // display decimal seconds
68  output << std::setw(field_width - 12) << std::fixed << std::noshowpos << std::internal
69  << std::setfill(' ') << std::setprecision(field_width - 15) << second;
70  }
71  // Return whether all output operations were successful
72  return !output.fail();
73 }
74 
75 bool vil_nitf2_date_time::read(std::istream& input, int field_width, bool& out_blank)
76 {
77  bool blank;
78  std::string fieldStr;
79  bool ok;
80  ok = vil_nitf2_integer_formatter(4).read_vcl_stream(input, year, blank); out_blank = blank;
81  ok &= vil_nitf2_integer_formatter(2).read_vcl_stream(input, month, blank); out_blank &= blank;
82  ok &= vil_nitf2_integer_formatter(2).read_vcl_stream(input, day, blank); out_blank &= blank;
83  if (field_width >= 10) {
84  ok &= vil_nitf2_integer_formatter(2).read_vcl_stream(input, hour, blank); out_blank &= blank;
85  } else {
86  hour = 0;
87  }
88  if (field_width >= 12) {
89  ok &= vil_nitf2_integer_formatter(2).read_vcl_stream(input, minute, blank); out_blank &= blank;
90  } else {
91  minute = 0;
92  }
93  if (field_width == 14) {
94  // integer seconds, no decimal point
95  int intSecond;
96  ok &= vil_nitf2_integer_formatter(2).read_vcl_stream(input, intSecond, blank); out_blank &= blank;
97  second = intSecond;
98  } else if (field_width > 14) {
99  // decimal seconds
100  ok &= vil_nitf2_double_formatter(field_width-12, field_width-15, false)
101  .read_vcl_stream(input, second, blank); out_blank &= blank;
102  } else {
103  // no seconds
104  second = 0.0;
105  }
106  return ok && is_valid();
107 }
108 
109 std::ostream& operator << (std::ostream& os, const vil_nitf2_date_time& dateTime)
110 {
111  return dateTime.output(os);
112 }
113 
114 //==============================================================================
115 // Class vil_nitf2_location_degrees
116 
117 std::ostream& vil_nitf2_location_degrees::output(std::ostream& os) const
118 {
119  os << '('
120  << std::fixed << lat_degrees << ", "
121  << std::fixed << lon_degrees << ')';
122  return os;
123 }
124 
125 bool vil_nitf2_location_degrees::read(std::istream& input, int field_width, bool& out_blank)
126 {
127  int lat_width = (field_width-1)/2;
128  int lon_width = (field_width+1)/2;
129  bool ok, blank;
130  ok = vil_nitf2_double_formatter(lat_width, precision, true).read_vcl_stream(input, lat_degrees, blank);
131  out_blank = blank;
132  ok &= vil_nitf2_double_formatter(lon_width, precision, true).read_vcl_stream(input, lon_degrees, out_blank);
133  out_blank &= blank;
134  return ok && is_valid();
135 }
136 
137 bool vil_nitf2_location_degrees::write(std::ostream& output, int field_width)
138 {
139  // Could someone remind me again why I didn't just use printf and scanf
140  // instead?
141  output << std::setw((field_width-1)/2) << std::fixed << std::showpos << std::internal
142  << std::setfill('0') << std::setprecision(precision) << lat_degrees
143  << std::setw((field_width+1)/2) << std::fixed << std::showpos << std::internal
144  << std::setfill('0') << std::setprecision(precision) << lon_degrees;
145  return !output.fail();
146 }
147 
149 {
150  return lat_degrees >= -90.0 && lat_degrees <= 90.0 &&
151  lon_degrees >= -180.0 && lon_degrees <= 180.0;
152 }
153 
154 
155 //==============================================================================
156 // Class vil_nitf2_location
157 
158 std::ostream& operator << (std::ostream& os, const vil_nitf2_location& loc)
159 {
160  return loc.output(os);
161 }
162 
163 //==============================================================================
164 // Class vil_nitf2_location_dmsh
165 
166 std::ostream& vil_nitf2_location_dmsh::output(std::ostream& os) const
167 {
168  os << '('
169  << lat_degrees << ':' << lat_minutes << ':'
170  << lat_seconds << ':' << lat_hemisphere << ", "
171  << lon_degrees << ':' << lon_minutes << ':'
172  << lon_seconds << ':' << lon_hemisphere << ')';
173  return os;
174 }
175 
176 bool vil_nitf2_location_dmsh::read(std::istream& input, int /* field_width */, bool& out_blank)
177 {
178  bool blank;
179  // Read latitude fields
180  bool ok = vil_nitf2_integer_formatter(2).read_vcl_stream(input, lat_degrees, blank);
181  if (out_blank) out_blank = blank;
182  if (ok) ok = vil_nitf2_integer_formatter(2).read_vcl_stream(input, lat_minutes, out_blank);
183  if (out_blank) out_blank = blank;
185  .read_vcl_stream(input, lat_seconds, out_blank);
186  if (out_blank) out_blank = blank;
187  if (ok) ok = vil_nitf2_char_formatter().read_vcl_stream(input, lat_hemisphere, out_blank);
188  if (out_blank) out_blank = blank;
189  // Read longitude fields (degrees is one digit longer than latitude)
190  if (ok) ok = vil_nitf2_integer_formatter(3).read_vcl_stream(input, lon_degrees, out_blank);
191  if (out_blank) out_blank = blank;
192  if (ok) ok = vil_nitf2_integer_formatter(2).read_vcl_stream(input, lon_minutes, out_blank);
193  if (out_blank) out_blank = blank;
195  .read_vcl_stream(input, lon_seconds, out_blank);
196  if (out_blank) out_blank = blank;
197  if (ok) ok = vil_nitf2_char_formatter().read_vcl_stream(input,lon_hemisphere, out_blank);
198  if (out_blank) out_blank = blank;
199  return ok && is_valid();
200 }
201 
202 bool vil_nitf2_location_dmsh::write(std::ostream& output, int /* field_width */)
203 {
204  bool ok;
205  // Write latitude fields
210  // Write longitude fields (degrees is one digit longer than latitude)
215  return ok;
216 }
217 
219 {
220  return lat_degrees >= -90 && lat_degrees <= 90 &&
221  lon_degrees >= -180 && lon_degrees <= 180 &&
222  lat_minutes >= 0 && lat_minutes < 60 &&
223  lon_minutes >= 0 && lon_minutes < 60 &&
224  lat_seconds >= 0.0 && lat_seconds < 60.0 &&
225  lon_seconds >= 0.0 && lon_seconds < 60.0 &&
226  std::string("NnSs").find(lat_hemisphere) != std::string::npos &&
227  std::string("EeWw").find(lon_hemisphere) != std::string::npos;
228 }
bool write_vcl_stream(std::ostream &output, const double &value) override
bool read(std::istream &input, int field_width, bool &out_blank) override
bool write(std::ostream &output, int field_width) const
bool write_vcl_stream(std::ostream &output, const int &value) override
bool read(std::istream &input, int field_width, bool &out_blank)
bool read_vcl_stream(std::istream &input, int &out_value, bool &out_blank) override
virtual std::ostream & output(std::ostream &) const =0
bool read(std::istream &input, int field_width, bool &out_blank) override
bool write_vcl_stream(std::ostream &output, const char &value) override
bool write(std::ostream &output, int field_width) override
bool read_vcl_stream(std::istream &input, char &out_value, bool &out_blank) override
bool write(std::ostream &output, int field_width) override
std::ostream & operator<<(std::ostream &os, const vil_nitf2_date_time &dateTime)
std::ostream & output(std::ostream &) const override
bool read_vcl_stream(std::istream &input, double &out_value, bool &out_blank) override
std::ostream & output(std::ostream &os) const override
std::ostream & output(std::ostream &) const override