MetadataExample.cxxΒΆ

Example source code (MetadataExample.cxx):

//  This example illustrates the access to metadata image information
//  with OTB. By metadata, we mean data which is typically stored with
//  remote sensing images, like geographical coordinates of pixels,
//  pixel spacing or resolution, etc. Of course, the availability of
//  these data depends on the image format used and on the fact that
//  the image producer must fill the available metadata fields. The
//  image formats which typically support metadata are for example
//  CEOS and GeoTiff.
//
//  The metadata support is embedded in OTB's IO functionnalities and
//  is accessible through the \doxygen{otb}{Image} and
//  \doxygen{otb}{VectorImage} classes. You should avoid using the
//  \doxygen{itk}{Image} class if you want to have metadata support.

#include "itkMacro.h"
#include <iostream>
#include <fstream>
#include <string>

#include "otbImage.h"
#include "otbImageFileReader.h"

int main(int itkNotUsed(argc), char* argv[])
{

  // Verify the number of parameters in the command line
  const char* inputFilename       = argv[1];
  const char* outputAsciiFilename = argv[2];

  //  This simple example will consist on reading an image from a file
  //  and writing the metadata to an output ASCII file. As usual we
  //  start by defining the types needed for the image to be read.

  using InputPixelType         = unsigned char;
  const unsigned int Dimension = 2;

  using InputImageType = otb::Image<InputPixelType, Dimension>;

  using ReaderType = otb::ImageFileReader<InputImageType>;

  //  We can now instantiate the reader and get a pointer to the input image.

  ReaderType::Pointer     reader = ReaderType::New();
  InputImageType::Pointer image  = InputImageType::New();

  reader->SetFileName(inputFilename);
  reader->Update();

  image = reader->GetOutput();

  //  Once the image has been read, we can access the metadata
  //  information. We will copy this information to an ASCII file, so we
  //  create an output file stream for this purpose.

  std::ofstream file;

  file.open(outputAsciiFilename);

  //  We can now call the different available methods for accessing the
  //  metadata. Useful methods are :
  //  \begin{itemize}
  //      \item \code{GetSpacing}: the sampling step;
  //      \item \code{GetOrigin}: the coordinates of the origin of the image;
  //      \item \code{GetProjectionRef}: the image projection reference;
  //      \item \code{GetGCPProjection}: the projection for the eventual
  //  ground control points;
  //      \item \code{GetGCPCount}: the number of GCPs available;
  //  \end{itemize}

  file << "Spacing " << image->GetSignedSpacing() << std::endl;
  file << "Origin " << image->GetOrigin() << std::endl;

  file << "Projection REF " << image->GetProjectionRef() << std::endl;

  file << "GCP Projection " << image->GetGCPProjection() << std::endl;

  unsigned int GCPCount = image->GetGCPCount();
  file << "GCP Count " << image->GetGCPCount() << std::endl;

  //  One can also get the GCPs by number, as well as their coordinates
  //  in image and geographical space.

  for (unsigned int GCPnum = 0; GCPnum < GCPCount; GCPnum++)
  {
    file << "GCP[" << GCPnum << "] Id " << image->GetGCPId(GCPnum) << std::endl;
    file << "GCP[" << GCPnum << "] Info " << image->GetGCPInfo(GCPnum) << std::endl;
    file << "GCP[" << GCPnum << "] Row " << image->GetGCPRow(GCPnum) << std::endl;
    file << "GCP[" << GCPnum << "] Col " << image->GetGCPCol(GCPnum) << std::endl;
    file << "GCP[" << GCPnum << "] X " << image->GetGCPX(GCPnum) << std::endl;
    file << "GCP[" << GCPnum << "] Y " << image->GetGCPY(GCPnum) << std::endl;
    file << "GCP[" << GCPnum << "] Z " << image->GetGCPZ(GCPnum) << std::endl;
    file << "----------------" << std::endl;
  }

  //  If a geographical transformation is available, it can be recovered
  //  as follows.

  InputImageType::VectorType tab = image->GetGeoTransform();

  file << "Geo Transform " << std::endl;
  for (unsigned int i = 0; i < tab.size(); ++i)
  {
    file << " " << i << " -> " << tab[i] << std::endl;
  }
  tab.clear();

  tab = image->GetUpperLeftCorner();
  file << "Corners " << std::endl;
  for (unsigned int i = 0; i < tab.size(); ++i)
  {
    file << " UL[" << i << "] -> " << tab[i] << std::endl;
  }
  tab.clear();

  tab = image->GetUpperRightCorner();
  for (unsigned int i = 0; i < tab.size(); ++i)
  {
    file << " UR[" << i << "] -> " << tab[i] << std::endl;
  }
  tab.clear();

  tab = image->GetLowerLeftCorner();
  for (unsigned int i = 0; i < tab.size(); ++i)
  {
    file << " LL[" << i << "] -> " << tab[i] << std::endl;
  }
  tab.clear();

  tab = image->GetLowerRightCorner();
  for (unsigned int i = 0; i < tab.size(); ++i)
  {
    file << " LR[" << i << "] -> " << tab[i] << std::endl;
  }
  tab.clear();

  file.close();

  return EXIT_SUCCESS;
}