[a34150]: contrib / brl / bbas / brad / brad_image_metadata.cxx  Maximize  Restore  History

Download this file

538 lines (495 with data), 19.3 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
#include "brad_image_metadata.h"
//:
// \file
#include <vcl_iostream.h>
#include <vcl_fstream.h>
#include <vul/vul_awk.h>
#include <vul/vul_file.h>
#include <vul/vul_file_iterator.h>
#include <vcl_sstream.h>
#include <vil/vil_image_resource.h>
#include <vil/vil_load.h>
#include <vil/file_formats/vil_nitf2_image.h>
#include <vpgl/file_formats/vpgl_nitf_rational_camera.h>
#include <vpgl/vpgl_local_rational_camera.h>
#include <vpgl/vpgl_lvcs.h>
#include <vpgl/algo/vpgl_backproject.h>
#include <vgl/vgl_point_3d.h>
#include <vgl/vgl_vector_3d.h>
#include <vgl/vgl_box_2d.h>
#include <vgl/vgl_intersection.h>
#include <brad/brad_sun_pos.h>
// Construct using nitf and metadata file
brad_image_metadata::brad_image_metadata(vcl_string const& nitf_filename, vcl_string const& meta_folder)
{
if (!parse(nitf_filename, meta_folder)) {
vcl_cerr << "ERROR parsing image metadata\n";
}
vcl_cout << "!!!! lower left lon: " << lower_left_.x() << " lat: " << lower_left_.y() << '\n';
vcl_cout << "!!!! upper right lon: " << upper_right_.x() << " lat: " << upper_right_.y() << '\n';
}
// Write brad_image_metadata to stream
vcl_ostream& operator<<(vcl_ostream& s, brad_image_metadata const& md)
{
s << "sun_elevation = " << md.sun_elevation_ << '\n'
<< "sun_azimuth = " << md.sun_azimuth_ << '\n'
<< "view_elevation = " << md.view_elevation_ << '\n'
<< "view_azimuth = " << md.view_azimuth_ << '\n'
<< "gain = " << md.gain_ << '\n'
<< "offset = " << md.offset_ << '\n'
<< "sun_irradiance = " << md.sun_irradiance_ << '\n'
<< "number_of_bits = " << md.number_of_bits_ << vcl_endl;
return s;
}
// Read brad_image_metadata from stream
vcl_istream& operator>>(vcl_istream& s, brad_image_metadata& md)
{
vcl_string input;
while (!s.eof()) {
s >> input;
if (input=="sun_elevation") {
s >> input;
s >> md.sun_elevation_;
}
if (input=="sun_azimuth") {
s >> input;
s >> md.sun_azimuth_;
}
if (input=="view_elevation") {
s >> input;
s >> md.view_elevation_;
}
if (input=="view_azimuth") {
s >> input;
s >> md.view_azimuth_;
}
if (input == "gain") {
s >> input;
s >> md.gain_;
}
if (input == "offset") {
s >> input;
s >> md.offset_;
}
if (input == "sun_irradiance") {
s >> input;
s >> md.sun_irradiance_;
}
if (input == "number_of_bits") {
s >> input;
s >> md.number_of_bits_;
}
}
return s;
}
bool brad_image_metadata::parse_from_imd(vcl_string const& filename)
{
vcl_ifstream ifs( filename.c_str() );
if (!ifs.good()){
vcl_cerr << "Error opening file " << filename << vcl_endl;
return false;
}
n_bands_ = 0;
// now parse the IMD file
vul_awk awk(ifs);
double absCalfact = 1.0;
double effectiveBand = 1.0;
lower_left_.set(181, 91, 10000);
upper_right_.set(-181,-91, -10000);
for (; awk; ++awk)
{
vcl_stringstream linestr(awk.line());
vcl_string tag;
linestr >> tag;
if (tag.compare("absCalFactor") == 0) {
linestr >> tag; // read =
linestr >> absCalfact;
continue;
}
if (tag.compare("effectiveBandwidth") == 0) {
linestr >> tag; // read =
linestr >> effectiveBand;
continue;
}
if (tag.compare("cloudCover") == 0) {
linestr >> tag;
linestr >> cloud_coverage_percentage_;
continue;
}
if (tag.compare("satId") == 0) {
linestr >> tag;
linestr >> satellite_name_;
satellite_name_ = satellite_name_.substr(satellite_name_.find_first_of("\"")+1, satellite_name_.find_last_of("\"")-1);
continue;
}
if (tag.compare("LLLon") == 0 || tag.compare("URLon") == 0 || tag.compare("ULLon") == 0 || tag.compare("LRLon") == 0) {
linestr >> tag;
double x;
linestr >> x;
if (lower_left_.x() > x) lower_left_.set(x, lower_left_.y(), lower_left_.z());
if (upper_right_.x() < x) upper_right_.set(x, upper_right_.y(), upper_right_.z());
continue;
}
if (tag.compare("LLLat") == 0 || tag.compare("URLat") == 0 || tag.compare("ULLat") == 0 || tag.compare("LRLat") == 0) {
linestr >> tag;
double y;
linestr >> y;
if (lower_left_.y() > y) lower_left_.set(lower_left_.x(), y, lower_left_.z());
if (upper_right_.y() < y) upper_right_.set(upper_right_.x(), y, upper_right_.z());
continue;
}
if (tag.compare("LLHAE") == 0 || tag.compare("URHAE") == 0 || tag.compare("ULHAE") == 0 || tag.compare("LRHAE") == 0) { //CAUTION: height above ELLIPSOID (not mean sea level/ geoid)
linestr >> tag;
double z;
linestr >> z;
if (lower_left_.z() > z) lower_left_.set(lower_left_.x(), lower_left_.y(), z);
if (upper_right_.z() < z) upper_right_.set(upper_right_.x(), upper_right_.y(), z);
continue;
}
if (tag.compare("bandId") == 0) {
linestr >> tag;
vcl_string band_str;
linestr >> band_str;
if (band_str.find("P") != vcl_string::npos) {
band_ = "PAN";
} else
band_ = "MULTI";
continue;
}
if (tag.compare("BEGIN_GROUP") == 0) {
n_bands_++;
continue;
}
}
n_bands_--; // there is an extra BEGIN_GROUP for some other image info not related to individual bands
vcl_cout << "abs: " << absCalfact << " eff: " << effectiveBand << " cloud coverage percentage : " << cloud_coverage_percentage_ << " band: " << band_ << " number of bands: " << n_bands_ << vcl_endl;
gain_ = absCalfact/effectiveBand;
offset_ = 0.0;
return true;
}
// only parse the cloud coverage for now
bool brad_image_metadata::parse_from_pvl(vcl_string const& filename)
{
//vcl_cout << "Parse from PVL file is not implemented yet!\n";
vcl_ifstream ifs( filename.c_str() );
if (!ifs.good()){
vcl_cerr << "Error opening file " << filename << vcl_endl;
return false;
}
n_bands_ = 0;
lower_left_.set(181, 91, 10000);
upper_right_.set(-181,-91, -10000);
// now parse the IMD file
vul_awk awk(ifs);
for (; awk; ++awk)
{
vcl_stringstream linestr(awk.line());
vcl_string tag;
linestr >> tag;
if (tag.compare("productCloudCoverPercentage") == 0) {
linestr >> tag;
linestr >> cloud_coverage_percentage_;
continue;
}
if ((linestr.str().find("BEGIN_GROUP") != vcl_string::npos && linestr.str().find("upperRightCorner") != vcl_string::npos) ||
(linestr.str().find("BEGIN_GROUP") != vcl_string::npos && linestr.str().find("upperLeftCorner") != vcl_string::npos) ||
(linestr.str().find("BEGIN_GROUP") != vcl_string::npos && linestr.str().find("lowerRightCorner") != vcl_string::npos) ||
(linestr.str().find("BEGIN_GROUP") != vcl_string::npos && linestr.str().find("lowerLeftCorner") != vcl_string::npos) )
{
vcl_stringstream linestr(awk.line());
while (linestr.str().find("latitude") == vcl_string::npos) {
++awk;
//linestr = vcl_stringstream(awk.line());
linestr.clear();
linestr.str(awk.line());
}
vcl_string dummy; linestr >> dummy; linestr >> dummy;
double y; linestr >> y;
while (linestr.str().find("longitude") == vcl_string::npos) {
++awk;
//linestr = vcl_stringstream(awk.line());
linestr.clear();
linestr.str(awk.line());
}
linestr >> dummy; linestr >> dummy;
double x; linestr >> x;
while (linestr.str().find("height") == vcl_string::npos) {
++awk;
//linestr = vcl_stringstream(awk.line());
linestr.clear();
linestr.str(awk.line());
}
linestr >> dummy; linestr >> dummy;
double z; linestr >> z;
if (lower_left_.x() > x) lower_left_.set(x, lower_left_.y(), lower_left_.z());
if (lower_left_.y() > y) lower_left_.set(lower_left_.x(), y, lower_left_.z());
if (lower_left_.z() > z) lower_left_.set(lower_left_.x(), lower_left_.y(), z);
if (upper_right_.x() < x) upper_right_.set(x, upper_right_.y(), upper_right_.z());
if (upper_right_.y() < y) upper_right_.set(upper_right_.x(), y, upper_right_.z());
if (upper_right_.z() < z) upper_right_.set(upper_right_.x(), upper_right_.y(), z);
continue;
}
if (tag.compare("productSpectralType") == 0) {
linestr >> tag;
vcl_string band_str;
linestr >> band_str;
if (band_str.find("PAN") != vcl_string::npos)
band_ = "PAN";
else
band_ = "MULTI";
continue;
}
if (tag.compare("numberOfSpectralBands") == 0) {
linestr >> tag;
linestr >> n_bands_;
continue;
}
}
vcl_cout << "cloud coverage percentage : " << cloud_coverage_percentage_ << " band: " << band_ << " number of bands: " << n_bands_ << vcl_endl;
return true;
}
//: parse header in nitf image, assumes that metadata files are in the same folder with the image
// If meta_folder is not empty, they are searched in that folder as well
bool brad_image_metadata::parse(vcl_string const& nitf_filename, vcl_string const& meta_folder)
{
vil_image_resource_sptr image = vil_load_image_resource(nitf_filename.c_str());
if (!image)
{
vcl_cout << "NITF image load failed!\n";
return false;
}
vcl_string format = image->file_format();
vcl_string prefix = format.substr(0,4);
if (prefix != "nitf")
{
vcl_cout << "source image is not NITF\n";
return false;
}
//cast to an nitf2_image
vil_nitf2_image *nitf_image = static_cast<vil_nitf2_image*>(image.ptr());
vpgl_nitf_rational_camera nitf_cam(nitf_image, false);
//upper_left_ = nitf_cam.upper_left(); // caution, lat is x and lon is y when read from nitf camera with this method
upper_right_.set(nitf_cam.upper_right()[1], nitf_cam.upper_right()[0], 0);
lower_left_.set(nitf_cam.lower_left()[1], nitf_cam.lower_left()[0], 0);
//lower_right_ = nitf_cam.lower_right();
vcl_cout << "!!!! lower left lon: " << lower_left_.x() << " lat: " << lower_left_.y() << '\n';
vcl_cout << "!!!! upper right lon: " << upper_right_.x() << " lat: " << upper_right_.y() << '\n';
//get NITF information
vcl_vector< vil_nitf2_image_subheader* > headers = nitf_image->get_image_headers();
vil_nitf2_image_subheader* hdr = headers[0];
double sun_el;
double sun_az;
if (!hdr->get_sun_params(sun_el, sun_az)) {
vcl_cerr << "failed to obtain sun parameters info\n";
return false;
}
sun_elevation_ = sun_el;
sun_azimuth_ = sun_az;
int year, month, day, hour, min;
if (!hdr->get_date_time(year, month, day, hour, min)) {
vcl_cerr << "failed to obtain date time info\n";
return false;
}
t_.year = year; t_.month = month; t_.day = day; t_.hour = hour; t_.min = min;
number_of_bits_ = hdr->get_number_of_bits_per_pixel();
// set solar irradiance to a reasonable default in case we don't have the information
// "reasonable" is defined here as roughly in the range of the examples we know.
double solar_irrad = 1500.0;
// solar irradiance is dependent on sensor because each has a different range of wavelengths they are sensitive to.
vcl_string img_info = hdr->get_image_source();
if (img_info.find("IKONOS") != vcl_string::npos || nitf_filename.find("IK") != vcl_string::npos) {
solar_irrad = 1375.8;
satellite_name_ = "IKONOS";
} else if (img_info.find("GeoEye-1") != vcl_string::npos || img_info.find("GEOEYE1") != vcl_string::npos) { // OZGE TODO: check this one
solar_irrad = 1617;
satellite_name_ = "GeoEye-1";
} else if (img_info.find("QuickBird") != vcl_string::npos || nitf_filename.find("QB") != vcl_string::npos) {
solar_irrad = 1381.7;
satellite_name_ = "QuickBird";
} else if (img_info.find("WorldView") != vcl_string::npos || nitf_filename.find("WV") != vcl_string::npos) {
solar_irrad = 1580.814;
satellite_name_ = "WorldView";
} else if (img_info.find("WorldView2") != vcl_string::npos || img_info.find("WV02") != vcl_string::npos) {
solar_irrad = 1580.814;
satellite_name_ = "WorldView2";
} else if (img_info.find("DigitalGlobe") != vcl_string::npos) {
solar_irrad = 1580.814;
satellite_name_ = "DigitalGlobe"; // which satellite when the name is DigitalGlobe??
} else
vcl_cerr << "Cannot find satellite name for: " << img_info << " in NITF: Guessing band-averaged solar irradiance value = " << solar_irrad << "." << nitf_filename;
vcl_cout << "solar_irrad: " << solar_irrad << vcl_endl;
// scale sun irradiance using Earth-Sun distance
double d = brad_sun_distance(year, month, day, hour, min);
sun_irradiance_ = solar_irrad/(d*d);
// compute satellite az,el values for center of image
double off_u, off_v;
nitf_cam.image_offset(off_u, off_v);
// get lat,lon offsets for local euclidean coord. system origin
double lon0 = nitf_cam.offset(vpgl_rational_camera<double>::X_INDX);
double lat0 = nitf_cam.offset(vpgl_rational_camera<double>::Y_INDX);
// get elevation offset value
double el0 = nitf_cam.offset(vpgl_rational_camera<double>::Z_INDX);
vpgl_lvcs lvcs(lat0, lon0, el0, vpgl_lvcs::wgs84, 0, 0, vpgl_lvcs::DEG, vpgl_lvcs::METERS);
vpgl_local_rational_camera<double> local_cam(lvcs, nitf_cam);
// determine direction to camera
vgl_vector_3d<double> to_camera;
vpgl_backproject::direction_to_camera(local_cam, vgl_point_3d<double>(0,0,0), to_camera);
// convert vector to az,el
const double rad_to_deg = 180.0 / vnl_math::pi;
// degrees above horizon
view_elevation_ = vcl_asin(to_camera.z()) * rad_to_deg;
// degrees east of north
view_azimuth_ = vcl_atan2(to_camera.x(), to_camera.y()) * rad_to_deg;
if (view_azimuth_ < 0)
view_azimuth_ += 360;
vcl_string dirname = vul_file::dirname(nitf_filename);
// set gain offset defaults, some satellites' images do not require any adjustment
gain_ = 1.0f;
offset_ = 0.0f;
// look for metadata files with known formats recursively in the directory of the image
// If we find one, check file name to see if it is for the same image, if so parse it
vcl_string imagename = vul_file::strip_directory(nitf_filename);
imagename = vul_file::strip_extension(imagename);
vcl_cout << "imagename: " << imagename << vcl_endl;
vcl_string in_dir = dirname + "/*.*";
vcl_string meta_filename = "";
for (vul_file_iterator fn = in_dir.c_str(); fn; ++fn) {
vcl_string filename = fn();
vcl_string name = vul_file::strip_directory(filename);
name = vul_file::strip_extension(name);
vcl_string ext = vul_file::extension(filename);
if (imagename.find(name) != vcl_string::npos &&
(ext.compare(".IMD") == 0 || ext.compare(".imd") == 0 ||
ext.compare(".PVL") == 0 || ext.compare(".pvl") == 0)
) {
meta_filename = filename;
break;
}
}
if (meta_filename.size() == 0 && meta_folder.size() != 0) {
vcl_cout << " searching " << meta_folder << vcl_endl;
vcl_string in_dir = meta_folder + "/*.*";
for (vul_file_iterator fn = in_dir.c_str(); fn; ++fn) {
vcl_string filename = fn();
vcl_string name = vul_file::strip_directory(filename);
name = vul_file::strip_extension(name);
vcl_string ext = vul_file::extension(filename);
if (imagename.find(name) != vcl_string::npos &&
(ext.compare(".IMD") == 0 || ext.compare(".imd") == 0 ||
ext.compare(".PVL") == 0 || ext.compare(".pvl") == 0)
) {
meta_filename = filename;
break;
}
}
}
if (meta_filename.size() == 0) {
// check if this is IKONOS
vcl_string type = hdr->get_image_type(); // type mono is band PAN
unsigned bpp = number_of_bits_;
vcl_cout << "Ikonos: bpp " << bpp << " type: " << type << vcl_endl;
if (img_info.compare("IKONOS") == 0 && type.compare("MONO") == 0 && bpp == (unsigned)11) {
vcl_cout << "An 11-bit Panchromatic IKONOS image, setting gain & offset values according to tech document\n";
gain_ = (10.0/161.0)/0.403;
offset_ = 0.0;
}
else {
vcl_cout << "could not set gain and offset for " << imagename << vcl_endl;
}
vcl_cout << *this;
return true;
}
vcl_string ext = vul_file::extension(meta_filename);
if (ext.compare(".IMD") == 0 || ext.compare(".imd") == 0)
parse_from_imd(meta_filename);
else if (ext.compare(".PVL") == 0 || ext.compare(".pvl") == 0)
parse_from_pvl(meta_filename);
else
vcl_cout << "unknown meta file format: " << ext << " in name: " << meta_filename << "!\n";
vcl_cout << " !!!!!!!!!! satellite name: " << satellite_name_ << vcl_endl;
vcl_cout << *this;
return true;
}
bool brad_image_metadata::same_time(brad_image_metadata& other)
{
if (this->t_.min == other.t_.min && this->t_.hour == other.t_.hour &&
this->t_.day == other.t_.day && this->t_.month == other.t_.month)
return true;
else
return false;
}
//: compare the lat, lon bounding boxes. treat as Euclidean coordinate system, good for small boxes
bool brad_image_metadata::same_extent(brad_image_metadata& other)
{
vgl_box_2d<double> b1(lower_left_.x(), lower_left_.y(), upper_right_.x(), upper_right_.y());
vgl_box_2d<double> b2(other.lower_left_.x(), other.lower_left_.y(), other.upper_right_.x(), other.upper_right_.y());
if (vcl_abs(vgl_intersection(b1, b2).area() - b1.area()) < 0.000000001)
return true;
}
//: binary save self to stream
void brad_image_metadata::b_write(vsl_b_ostream& os) const
{
vsl_b_write(os, version());
vsl_b_write(os, sun_elevation_);
vsl_b_write(os, sun_azimuth_);
vsl_b_write(os, view_elevation_);
vsl_b_write(os, view_azimuth_);
vsl_b_write(os, gain_);
vsl_b_write(os, offset_);
vsl_b_write(os, sun_irradiance_);
vsl_b_write(os, t_.day);
vsl_b_write(os, t_.hour);
vsl_b_write(os, t_.min);
vsl_b_write(os, t_.month);
vsl_b_write(os, t_.year);
vsl_b_write(os, number_of_bits_);
vsl_b_write(os, satellite_name_);
vsl_b_write(os, cloud_coverage_percentage_);
vsl_b_write(os, upper_right_.x());
vsl_b_write(os, upper_right_.y());
vsl_b_write(os, upper_right_.z());
vsl_b_write(os, lower_left_.x());
vsl_b_write(os, lower_left_.y());
vsl_b_write(os, lower_left_.z());
vsl_b_write(os, band_);
vsl_b_write(os, n_bands_);
}
//: binary load self from stream
void brad_image_metadata::b_read(vsl_b_istream& is)
{
if (!is) return;
short ver;
vsl_b_read(is, ver);
if (ver == 0) {
vsl_b_read(is, sun_elevation_);
vsl_b_read(is, sun_azimuth_);
vsl_b_read(is, view_elevation_);
vsl_b_read(is, view_azimuth_);
vsl_b_read(is, gain_);
vsl_b_read(is, offset_);
vsl_b_read(is, sun_irradiance_);
vsl_b_read(is, t_.day);
vsl_b_read(is, t_.hour);
vsl_b_read(is, t_.min);
vsl_b_read(is, t_.month);
vsl_b_read(is, t_.year);
vsl_b_read(is, number_of_bits_);
vsl_b_read(is, satellite_name_);
vsl_b_read(is, cloud_coverage_percentage_);
double x,y,z;
vsl_b_read(is, x);
vsl_b_read(is, y);
vsl_b_read(is, z);
upper_right_.set(x,y,z);
vsl_b_read(is, x);
vsl_b_read(is, y);
vsl_b_read(is, z);
lower_left_.set(x,y,z);
vsl_b_read(is, band_);
vsl_b_read(is, n_bands_);
}
else {
vcl_cout << "brad_image_metadata -- unknown binary io version " << ver << '\n';
return;
}
}