File: MetadataExample.cxx

package info (click to toggle)
otb 5.8.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 38,496 kB
  • ctags: 40,282
  • sloc: cpp: 306,573; ansic: 3,575; python: 450; sh: 214; perl: 74; java: 72; makefile: 70
file content (201 lines) | stat: -rw-r--r-- 6,233 bytes parent folder | download
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
/*=========================================================================

  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.


     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/


//  SoftwareGuide: BeginLatex
//
//  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.
//
//  SoftwareGuide: EndLatex

#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];

//  SoftwareGuide: BeginLatex
//
//  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.
//
//  SoftwareGuide: EndLatex

// SoftwareGuide : BeginCodeSnippet
  typedef unsigned char InputPixelType;
  const unsigned int Dimension = 2;

  typedef otb::Image<InputPixelType,  Dimension> InputImageType;

  typedef otb::ImageFileReader<InputImageType> ReaderType;
// SoftwareGuide : EndCodeSnippet

//  SoftwareGuide: BeginLatex
//
//  We can now instantiate the reader and get a pointer to the input image.
//
//  SoftwareGuide: EndLatex

// SoftwareGuide : BeginCodeSnippet
  ReaderType::Pointer     reader = ReaderType::New();
  InputImageType::Pointer image = InputImageType::New();

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

  image = reader->GetOutput();
// SoftwareGuide : EndCodeSnippet

//  SoftwareGuide: BeginLatex
//
//  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.
//
//  SoftwareGuide: EndLatex

// SoftwareGuide : BeginCodeSnippet
  std::ofstream file;

  file.open(outputAsciiFilename);
// SoftwareGuide : EndCodeSnippet

//  SoftwareGuide: BeginLatex
//
//  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}
//
//  SoftwareGuide: EndLatex

// SoftwareGuide : BeginCodeSnippet
  file << "Spacing " << image->GetSpacing() << 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;
// SoftwareGuide : EndCodeSnippet

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

// SoftwareGuide : BeginCodeSnippet
  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;
    }
// SoftwareGuide : EndCodeSnippet

//  SoftwareGuide: BeginLatex
//
//  If a geographical transformation is available, it can be recovered
//  as follows.
//
//  SoftwareGuide: EndLatex

// SoftwareGuide : BeginCodeSnippet
  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();
  // SoftwareGuide : EndCodeSnippet

  return EXIT_SUCCESS;
}