Orfeo Toolbox  4.0
otbImageMetadataInterfaceBase.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 
20 
21 #include "otbMacro.h"
22 #include "otbImageKeywordlist.h"
23 #include "itkMetaDataObject.h"
24 #include "itksys/SystemTools.hxx"
25 
26 namespace otb
27 {
28 
31 {
32 }
33 
34 std::string
36 {
37  std::string metadata;
38  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
39 
41  {
42  itk::ExposeMetaData<std::string>(dict, static_cast<std::string>(MetaDataKey::ProjectionRefKey), metadata);
43  return (metadata);
44  }
45  else return ("");
46 }
47 
48 std::string
50 {
51  std::string metadata;
52  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
53 
55  {
56  itk::ExposeMetaData<std::string>(dict, static_cast<std::string>(MetaDataKey::GCPProjectionKey), metadata);
57  return (metadata);
58  }
59  else return ("");
60 }
61 
62 unsigned int
64 {
65  unsigned int GCPCount = 0;
66  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
67 
69  {
70  itk::ExposeMetaData<unsigned int>(dict, MetaDataKey::GCPCountKey, GCPCount);
71  }
72 
73  return (GCPCount);
74 }
75 
76 OTB_GCP&
78 {
79  std::string key;
80  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
81 
82  std::ostringstream lStream;
83  lStream << MetaDataKey::GCPParametersKey << GCPnum;
84  key = lStream.str();
85 
86  if (dict.HasKey(key))
87  {
88 
89  itk::ExposeMetaData<OTB_GCP>(dict, key, m_GCP);
90  }
91  return (m_GCP);
92 }
93 
94 std::string
95 ImageMetadataInterfaceBase::GetGCPId(unsigned int GCPnum) const
96 {
97  std::string key;
98  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
99 
100  std::ostringstream lStream;
101  lStream << MetaDataKey::GCPParametersKey << GCPnum;
102  key = lStream.str();
103 
104  if (dict.HasKey(key))
105  {
106  OTB_GCP gcp;
107  itk::ExposeMetaData<OTB_GCP>(dict, key, gcp);
108  return (gcp.m_Id);
109  }
110  else return ("");
111 }
112 
113 std::string
114 ImageMetadataInterfaceBase::GetGCPInfo(unsigned int GCPnum) const
115 {
116  std::string key;
117  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
118 
119  std::ostringstream lStream;
120  lStream << MetaDataKey::GCPParametersKey << GCPnum;
121  key = lStream.str();
122 
123  if (dict.HasKey(key))
124  {
125  OTB_GCP gcp;
126  itk::ExposeMetaData<OTB_GCP>(dict, key, gcp);
127  return (gcp.m_Info);
128  }
129  else return ("");
130 }
131 
132 double
133 ImageMetadataInterfaceBase::GetGCPRow(unsigned int GCPnum) const
134 {
135  std::string key;
136  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
137 
138  std::ostringstream lStream;
139  lStream << MetaDataKey::GCPParametersKey << GCPnum;
140  key = lStream.str();
141 
142  if (dict.HasKey(key))
143  {
144  OTB_GCP gcp;
145  itk::ExposeMetaData<OTB_GCP>(dict, key, gcp);
146  return (gcp.m_GCPRow);
147  }
148  else return (0);
149 }
150 
151 double
152 ImageMetadataInterfaceBase::GetGCPCol(unsigned int GCPnum) const
153 {
154  std::string key;
155  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
156 
157  std::ostringstream lStream;
158  lStream << MetaDataKey::GCPParametersKey << GCPnum;
159  key = lStream.str();
160 
161  if (dict.HasKey(key))
162  {
163  OTB_GCP gcp;
164  itk::ExposeMetaData<OTB_GCP>(dict, key, gcp);
165  return (gcp.m_GCPCol);
166  }
167  else return (0);
168 }
169 
170 double
171 ImageMetadataInterfaceBase::GetGCPX(unsigned int GCPnum) const
172 {
173  std::string key;
174  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
175 
176  std::ostringstream lStream;
177  lStream << MetaDataKey::GCPParametersKey << GCPnum;
178  key = lStream.str();
179 
180  if (dict.HasKey(key))
181  {
182  OTB_GCP gcp;
183  itk::ExposeMetaData<OTB_GCP>(dict, key, gcp);
184  return (gcp.m_GCPX);
185  }
186  else return (0);
187 }
188 
189 double
190 ImageMetadataInterfaceBase::GetGCPY(unsigned int GCPnum) const
191 {
192  std::string key;
193  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
194 
195  std::ostringstream lStream;
196  lStream << MetaDataKey::GCPParametersKey << GCPnum;
197  key = lStream.str();
198 
199  if (dict.HasKey(key))
200  {
201  OTB_GCP gcp;
202  itk::ExposeMetaData<OTB_GCP>(dict, key, gcp);
203  return (gcp.m_GCPY);
204  }
205  else return (0);
206 }
207 
208 double
209 ImageMetadataInterfaceBase::GetGCPZ(unsigned int GCPnum) const
210 {
211  std::string key;
212  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
213 
214  std::ostringstream lStream;
215  lStream << MetaDataKey::GCPParametersKey << GCPnum;
216  key = lStream.str();
217 
218  if (dict.HasKey(key))
219  {
220  OTB_GCP gcp;
221  itk::ExposeMetaData<OTB_GCP>(dict, key, gcp);
222  return (gcp.m_GCPZ);
223  }
224  else return (0);
225 }
226 
229 {
230  VectorType adfGeoTransform;
231  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
232 
234  {
235  itk::ExposeMetaData<VectorType>(dict, MetaDataKey::GeoTransformKey, adfGeoTransform);
236  }
237  return (adfGeoTransform);
238 }
239 
242 {
243  VectorType UpperLeftCorner;
244  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
245 
247  {
248  itk::ExposeMetaData<VectorType>(dict, MetaDataKey::UpperLeftCornerKey, UpperLeftCorner);
249  }
250  return (UpperLeftCorner);
251 }
252 
255 {
256  VectorType UpperRightCorner;
257  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
258 
260  {
261  itk::ExposeMetaData<VectorType>(dict, MetaDataKey::UpperRightCornerKey, UpperRightCorner);
262  }
263  return (UpperRightCorner);
264 }
265 
268 {
269  VectorType LowerLeftCorner;
270  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
271 
273  {
274  itk::ExposeMetaData<VectorType>(dict, MetaDataKey::LowerLeftCornerKey, LowerLeftCorner);
275  }
276  return (LowerLeftCorner);
277 }
278 
281 {
282  VectorType LowerRightCorner;
283  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
284 
286  {
287  itk::ExposeMetaData<VectorType>(dict, MetaDataKey::LowerRightCornerKey, LowerRightCorner);
288  }
289  return (LowerRightCorner);
290 }
291 
294 {
295  ImageKeywordlistType imageKeywordlist;
296  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
297 
299  {
300  itk::ExposeMetaData<ImageKeywordlistType>(dict, MetaDataKey::OSSIMKeywordlistKey, imageKeywordlist);
301  }
302  return (imageKeywordlist);
303 }
304 
307 {
308  ImageKeywordlistType imageKeywordlist;
309  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
310 
312  {
313  itk::ExposeMetaData<ImageKeywordlistType>(dict, MetaDataKey::OSSIMKeywordlistKey, imageKeywordlist);
314  }
315  return (imageKeywordlist);
316 }
317 
318 
319 std::string
321 {
322  ImageKeywordlistType imageKeywordlist;
323  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
324 
326  {
327  itk::ExposeMetaData<ImageKeywordlistType>(dict, MetaDataKey::OSSIMKeywordlistKey, imageKeywordlist);
328  }
329  if (!imageKeywordlist.HasKey("sensor"))
330  {
331  return "";
332  }
333 
334  return imageKeywordlist.GetMetadataByKey("sensor");
335 }
336 
337 unsigned int
339 {
340  ImageKeywordlistType imageKeywordlist;
341  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
342 
344  {
345  itk::ExposeMetaData<ImageKeywordlistType>(dict, MetaDataKey::OSSIMKeywordlistKey, imageKeywordlist);
346  }
347 
348  if (!imageKeywordlist.HasKey("support_data.number_bands"))
349  {
350  return 0;
351  }
352 
353  std::string valueString = imageKeywordlist.GetMetadataByKey("support_data.number_bands");
354  unsigned int value = atoi(valueString.c_str());
355  return value;
356 }
357 
358 std::vector<std::string>
360 {
361  ImageKeywordlistType imageKeywordlist;
362  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
363 
365  {
366  itk::ExposeMetaData<ImageKeywordlistType>(dict, MetaDataKey::OSSIMKeywordlistKey, imageKeywordlist);
367  }
368 
369  /* band_name attribut is only used by OSSIM metadata reader otherwise it is band_name_list */
370  std::vector<std::string> outputValues;
371  if (!imageKeywordlist.HasKey("support_data.band_name"))
372  {
373  if (imageKeywordlist.HasKey("support_data.band_name_list"))
374  {
375  std::string valueString = imageKeywordlist.GetMetadataByKey("support_data.band_name_list");
376  itksys::SystemTools::Split(valueString.c_str(), outputValues, ' ');
377  }
378  else
379  return outputValues;
380  }
381  else
382  {
383  std::string valueString = imageKeywordlist.GetMetadataByKey("support_data.band_name");
384  itksys::SystemTools::Split(valueString.c_str(), outputValues, '/');
385  }
386 
387  return outputValues;
388 }
389 
390 double
392 {
393  ImageKeywordlistType imageKeywordlist;
394  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
395 
397  {
398  itk::ExposeMetaData<ImageKeywordlistType>(dict, MetaDataKey::OSSIMKeywordlistKey, imageKeywordlist);
399  }
400 
401  if (imageKeywordlist.HasKey("meters_per_pixel_x"))
402  {
403  std::string valueString = imageKeywordlist.GetMetadataByKey("meters_per_pixel_x");
404  double value = atof(valueString.c_str());
405  return value;
406  }
407 
408  if (imageKeywordlist.HasKey("pixel_spacing"))
409  {
410  std::string valueString = imageKeywordlist.GetMetadataByKey("pixel_spacing");
411  double value = atof(valueString.c_str());
412  return value;
413  }
414 
415  return 0;
416 }
417 
418 double
420 {
421  ImageKeywordlistType imageKeywordlist;
422  const MetaDataDictionaryType& dict = this->GetMetaDataDictionary();
423 
425  {
426  itk::ExposeMetaData<ImageKeywordlistType>(dict, MetaDataKey::OSSIMKeywordlistKey, imageKeywordlist);
427  }
428 
429  if (imageKeywordlist.HasKey("meters_per_pixel_y"))
430  {
431  std::string valueString = imageKeywordlist.GetMetadataByKey("meters_per_pixel_y");
432  double value = atof(valueString.c_str());
433  return value;
434  }
435 
436  if (imageKeywordlist.HasKey("pixel_spacing"))
437  {
438  std::string valueString = imageKeywordlist.GetMetadataByKey("pixel_spacing");
439  double value = atof(valueString.c_str());
440  return value;
441  }
442 
443  return 0;
444 
445 }
446 
447 
448 void
450 {
451 
452  std::vector<std::string> keys = dict.GetKeys();
453  std::string svalue;
454  unsigned int ivalue(0);
455  VectorType vvalue;
456  double dvalue = 0.;
457  OTB_GCP gcpvalue;
458  ImageKeywordlist kwl;
459 // an extra dependency just for printing is a bad idea.
460 // VectorDataKeywordlist vectorDataKeywordlistValue;
461  unsigned int i(0);
462 
463  // Copy of the const metadata dictionary in a metadata dictionary to be used
464  // by the ExposeMetaData method
465 
466  MetaDataDictionaryType dict2 = dict;
467 
468  for (unsigned int itkey = 0; itkey < keys.size(); ++itkey)
469  {
470 
471  switch (MetaDataKey::GetKeyType(keys[itkey]))
472  {
474  itk::ExposeMetaData<std::string>(dict2, keys[itkey], svalue);
475  os << indent << "---> " << keys[itkey] << " = " << svalue << std::endl;
476  break;
477 
479  itk::ExposeMetaData<unsigned int>(dict2, keys[itkey], ivalue);
480  os << indent << "---> " << keys[itkey] << " = " << ivalue << std::endl;
481  break;
482 
484  itk::ExposeMetaData<VectorType>(dict2, keys[itkey], vvalue);
485 
486  for (i = 0; i < vvalue.size(); ++i)
487  {
488  os << indent << "---> " << keys[itkey] << "[" << i << "] = " << vvalue[i] << std::endl;
489  }
490  vvalue.clear();
491 
492  break;
493 
495  itk::ExposeMetaData<double>(dict2, keys[itkey], dvalue);
496  os << indent << "---> " << keys[itkey] << " = " << dvalue << std::endl;
497  break;
498 
500  itk::ExposeMetaData<OTB_GCP>(dict2, keys[itkey], gcpvalue);
501 
502  os << indent << "---> " << keys[itkey] << std::endl;
503  gcpvalue.Print(os);
504  break;
506  itk::ExposeMetaData<ImageKeywordlist>(dict2, keys[itkey], kwl);
507 
508  os << indent << "---> " << keys[itkey] << std::endl;
509  kwl.Print(os);
510  break;
511 // case MetaDataKey::TVECTORDATAKEYWORDLIST:
512 // itk::ExposeMetaData<VectorDataKeywordlist>(dict2, keys[itkey], vectorDataKeywordlistValue);
513 //
514 // os << indent << "---> " << keys[itkey] << std::endl;
515 // vectorDataKeywordlistValue.Print(os);
516 // break;
517  default:
518  break;
519  }
520 
521  }
522 }
523 
524 
525 void
527 ::PrintSelf(std::ostream& os, itk::Indent indent) const
528 {
529  this->Superclass::PrintSelf(os, indent);
530 
531  bool canRead = this->CanRead();
532 
533  os << indent << "Initialized: " << (canRead ? true : false) << std::endl;
534  if (canRead)
535  {
536  std::vector<unsigned int> defaultDisplay = this->GetDefaultDisplay();
537  os << indent << "Default RGB Display: ["
538  << defaultDisplay[0] << ", "
539  << defaultDisplay[1] << ", "
540  << defaultDisplay[2] << "]" << std::endl;
541  os << indent << "ProjectionRef: " << this->GetProjectionRef() << std::endl;
542  os << indent << "GCPProjection: " << this->GetGCPProjection( ) << std::endl;
543  os << indent << "GCPCount: " << this->GetGCPCount( ) << std::endl;
544  for(unsigned int gcpIdx = 0; gcpIdx < this->GetGCPCount(); ++ gcpIdx)
545  {
546  //os << indent << "GCPs: " << this->GetGCPs(gcpIdx) << std::endl;
547  os << indent << "GCPId: " << this->GetGCPId(gcpIdx) << std::endl;
548  os << indent << "GCPInfo: " << this->GetGCPInfo(gcpIdx) << std::endl;
549  }
550  //os << indent << "GeoTransform: " << this->GetGeoTransform( ) << std::endl;
551  //os << indent << "UpperLeftCorner: " << this->GetUpperLeftCorner( ) << std::endl;
552  //os << indent << "UpperRightCorner:" << this->GetUpperRightCorner( ) << std::endl;
553  //os << indent << "LowerLeftCorner: " << this->GetLowerLeftCorner( ) << std::endl;
554  //os << indent << "LowerRightCorner:" << this->GetLowerRightCorner( ) << std::endl;
555  //os << indent << "ImageKeywordlist:" << this->GetImageKeywordlist( ) << std::endl;
556  os << indent << "SensorID: " << this->GetSensorID( ) << std::endl;
557  os << indent << "NumberOfBands: " << this->GetNumberOfBands( ) << std::endl;
558 
559  std::vector<std::string> bandNameList = this->GetBandName();
560  if (bandNameList.size() == 1)
561  {
562  os << indent << "BandName: " << bandNameList[0] << std::endl;
563  }
564  else
565  if (bandNameList.size() > 1)
566  {
567  os << indent << "BandNameList: ";
568  for (std::vector<std::string>::iterator it = bandNameList.begin(); it != bandNameList.end(); ++it)
569  {
570  os << *it << ", ";
571  }
572  os << std::endl;
573  }
574  os << indent << "XPixelSpacing: " << this->GetXPixelSpacing( ) << std::endl;
575  os << indent << "YPixelSpacing: " << this->GetYPixelSpacing( ) << std::endl;
576  os << indent << "Day: " << this->GetDay( ) << std::endl;
577  os << indent << "Month: " << this->GetMonth( ) << std::endl;
578  os << indent << "Year: " << this->GetYear( ) << std::endl;
579  os << indent << "Hour: " << this->GetHour( ) << std::endl;
580  os << indent << "Minute: " << this->GetMinute( ) << std::endl;
581  os << indent << "ProductionDay: " << this->GetProductionDay( ) << std::endl;
582  os << indent << "ProductionMonth: " << this->GetProductionMonth( ) << std::endl;
583  os << indent << "ProductionYear: " << this->GetProductionYear( ) << std::endl;
584  }
585 }
586 
587 
588 } // end namespace otb

Generated at Sat Mar 8 2014 15:59:08 for Orfeo Toolbox with doxygen 1.8.3.1