TileMapImageIOExample.cxx 8.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*=========================================================================

  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.

=========================================================================*/
18

19 20 21

//  Software Guide : BeginCommandLineArgs
//    INPUTS: {osmfile.otb}
22
//    OUTPUTS: {openStreetMap-Toulouse.png}
Emmanuel Christophe's avatar
Emmanuel Christophe committed
23
//    ${GeneratedFolder} 1.4835345 43.55968261 13
24 25
//  Software Guide : EndCommandLineArgs

26 27 28
//  Software Guide : BeginCommandLineArgs
//    INPUTS: {osmfile.otb}
//    OUTPUTS: {openStreetMap-Singapore.png}
Emmanuel Christophe's avatar
Emmanuel Christophe committed
29
//    ${GeneratedFolder} 103.780646 1.297643 13
30
//  Software Guide : EndCommandLineArgs
Emmanuel Christophe's avatar
Emmanuel Christophe committed
31 32 33 34 35 36 37 38 39

// Software Guide : BeginLatex
//
// First, we need to include several headers. There will be a bit of
// manual work going on here.
//
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
40 41 42 43
#include "itkRGBPixel.h"
#include "otbImageFileReader.h"
#include "otbTileMapImageIO.h"
#include "otbInverseSensorModel.h"
44
#include "otbForwardSensorModel.h"
45 46
#include "otbExtractROI.h"
#include "otbImageFileWriter.h"
47
#include "otbTileMapTransform.h"
48
#include "otbWorldFile.h"
Emmanuel Christophe's avatar
Emmanuel Christophe committed
49
// Software Guide : EndCodeSnippet
50

OTB Bot's avatar
STYLE  
OTB Bot committed
51
int main(int argc, char* argv[])
52 53
{

OTB Bot's avatar
STYLE  
OTB Bot committed
54 55 56
  if (argc != 7)
    {
    std::cout << argv[0] << " <inputFilename> <outputFilename> "
57 58
              << "<cacheDirectory> <lon> <lat> <depth>"
              << std::endl;
59 60

    return EXIT_FAILURE;
OTB Bot's avatar
STYLE  
OTB Bot committed
61
    }
62

Emmanuel Christophe's avatar
Emmanuel Christophe committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  // Software Guide : BeginLatex
  //
  // We retrieve the input parameters:
  // \begin{itemize}
  // \item the input filename is a simple text file specifying the access
  // modality to open street map data;
  // \item the output file is the image where you want to save the result;
  // \item the cache directory is necessary to keep the data retrieved from
  // the internet. It can also be reused to minimize network access;
  // \item longitude of the center of the scene;
  // \item latitude of the center of the scene;
  // \item depth which is inversely related to the resolution: when you increase
  // the depth by one, you divide the resolution by two.
  // \end{itemize}
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
81 82 83
  std::string inputFilename = argv[1];
  std::string outputFilename = argv[2];
  std::string cacheDirectory = argv[3];
OTB Bot's avatar
STYLE  
OTB Bot committed
84 85 86
  double      lon = atof(argv[4]);
  double      lat = atof(argv[5]);
  int         depth = atoi(argv[6]);
Emmanuel Christophe's avatar
Emmanuel Christophe committed
87 88 89 90
  // Software Guide : EndCodeSnippet

  // Software Guide : BeginLatex
  //
91 92
  // We now instantiate the reader. As some parameters need to be given to the
  // IO which is an \doxygen{otb}{TileMapImageIO}, we need to manually create it:
Emmanuel Christophe's avatar
Emmanuel Christophe committed
93 94
  //
  // Software Guide : EndLatex
95

Emmanuel Christophe's avatar
Emmanuel Christophe committed
96
  // Software Guide : BeginCodeSnippet
Emmanuel Christophe's avatar
Emmanuel Christophe committed
97 98
  typedef itk::RGBPixel<unsigned char>    RGBPixelType;
  typedef otb::Image<RGBPixelType, 2>     ImageType;
99
  typedef otb::ImageFileReader<ImageType> ReaderType;
Emmanuel Christophe's avatar
Emmanuel Christophe committed
100
  typedef otb::TileMapImageIO             ImageIOType;
101 102

  ImageIOType::Pointer tileIO = ImageIOType::New();
OTB Bot's avatar
STYLE  
OTB Bot committed
103
  ReaderType::Pointer  readerTile = ReaderType::New();
104 105 106 107 108
  tileIO->SetDepth(depth);
  tileIO->SetCacheDirectory(cacheDirectory);
  readerTile->SetImageIO(tileIO);
  readerTile->SetFileName(inputFilename);
  readerTile->UpdateOutputInformation();
Emmanuel Christophe's avatar
Emmanuel Christophe committed
109 110 111 112
  // Software Guide : EndCodeSnippet

  // Software Guide : BeginLatex
  //
113
  // Now, we potentially have an image of several Peta-Bytes covering the whole world
Emmanuel Christophe's avatar
Emmanuel Christophe committed
114 115 116 117
  // in the reader
  // that's why we don't want to do an update before extracting a specific
  // area.
  //
Emmanuel Christophe's avatar
Emmanuel Christophe committed
118
  // The coordinates are referred with an origin at the North Pole and the
Emmanuel Christophe's avatar
Emmanuel Christophe committed
119 120 121 122 123 124
  // change date meridian in Mercator projection. So we need to translate the latitude
  // and the longitude in this funny coordinate system:
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
125 126 127
  typedef otb::TileMapTransform<otb::TransformDirection::FORWARD> TransformType;
  TransformType::Pointer transform = TransformType::New();
  transform->SetDepth(depth);
Emmanuel Christophe's avatar
Emmanuel Christophe committed
128

129 130
  typedef itk::Point <double, 2> PointType;
  PointType lonLatPoint;
OTB Bot's avatar
STYLE  
OTB Bot committed
131 132
  lonLatPoint[0] = lon;
  lonLatPoint[1] = lat;
133 134

  PointType tilePoint;
135
  tilePoint = transform->TransformPoint(lonLatPoint);
Emmanuel Christophe's avatar
Emmanuel Christophe committed
136
  // Software Guide : EndCodeSnippet
137

Emmanuel Christophe's avatar
Emmanuel Christophe committed
138 139
  // Software Guide : BeginLatex
  //
140
  // This enables us to use the \doxygen{otb}{ExtractROI} to retrieve only
Emmanuel Christophe's avatar
Emmanuel Christophe committed
141 142 143 144 145
  // the area of interest and to avoid crashing our memory-limited computer.
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
146 147
  long int startX = static_cast<long int>(tilePoint[0]);
  long int startY = static_cast<long int>(tilePoint[1]);
148 149 150
  long int sizeX = 500;
  long int sizeY = 500;

OTB Bot's avatar
STYLE  
OTB Bot committed
151 152
  std::cerr << startX << ", " << startY << std::endl;
  std::cerr << sizeX << ", " << sizeY << std::endl;
153

OTB Bot's avatar
STYLE  
OTB Bot committed
154
  typedef otb::ExtractROI<RGBPixelType,  RGBPixelType> ExtractROIFilterType;
155
  ExtractROIFilterType::Pointer extractROIOsmFilter = ExtractROIFilterType::New();
OTB Bot's avatar
STYLE  
OTB Bot committed
156 157 158 159
  extractROIOsmFilter->SetStartX(startX - sizeX / 2);
  extractROIOsmFilter->SetStartY(startY - sizeY / 2);
  extractROIOsmFilter->SetSizeX(sizeX);
  extractROIOsmFilter->SetSizeY(sizeY);
160 161

  extractROIOsmFilter->SetInput(readerTile->GetOutput());
Emmanuel Christophe's avatar
Emmanuel Christophe committed
162
  // Software Guide : EndCodeSnippet
163

Emmanuel Christophe's avatar
Emmanuel Christophe committed
164 165 166 167 168 169
  // Software Guide : BeginLatex
  //
  // Finally, we just plug this to the writer to save our nice map of
  // the area:
  //
  // Software Guide : EndLatex
170

Emmanuel Christophe's avatar
Emmanuel Christophe committed
171
  // Software Guide : BeginCodeSnippet
172 173 174 175 176
  typedef otb::ImageFileWriter<ImageType> WriterType;
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(outputFilename);
  writer->SetInput(extractROIOsmFilter->GetOutput());
  writer->Update();
Emmanuel Christophe's avatar
Emmanuel Christophe committed
177 178
  // Software Guide : EndCodeSnippet

179 180 181 182 183 184
  // Software Guide : BeginLatex
  //
  // We also want to create the associated world file to be able to use this
  // new image in a GIS system. For this, we need to compute the coordinates
  // of the top left corner and the spacing in latitude and longitude.
  //
185
  // For that, we use the inverse transform to convert the corner coordinates into
186 187 188 189 190
  // latitude and longitude.
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
191 192 193 194 195
  typedef otb::TileMapTransform<otb::TransformDirection::INVERSE> InverseTransformType;
  InverseTransformType::Pointer transformInverse = InverseTransformType::New();
  transformInverse->SetDepth(depth);


196 197
  double lonUL, latUL, lonSpacing, latSpacing;

OTB Bot's avatar
STYLE  
OTB Bot committed
198 199
  tilePoint[0] = startX - sizeX / 2;
  tilePoint[1] = startY - sizeY / 2;
200
  lonLatPoint = transformInverse->TransformPoint(tilePoint);
201 202
  lonUL = lonLatPoint[0];
  latUL = lonLatPoint[1];
OTB Bot's avatar
STYLE  
OTB Bot committed
203 204
  tilePoint[0] = startX + sizeX / 2;
  tilePoint[1] = startY + sizeY / 2;
205
  lonLatPoint = transformInverse->TransformPoint(tilePoint);
OTB Bot's avatar
STYLE  
OTB Bot committed
206 207
  lonSpacing = (lonLatPoint[0] - lonUL) / (sizeX - 1);
  latSpacing = (lonLatPoint[1] - latUL) / (sizeY - 1);
208 209 210 211 212 213 214 215 216 217 218 219
  // Software Guide : EndCodeSnippet

  // Software Guide : BeginLatex
  //
  // Now that we have all the information, we can write the world file
  // which has the wld extension. This is a simple text file containing
  // the coordinates of the center of the top left pixel and the x and y
  // spacing.
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
220 221 222 223 224 225 226
  otb::WorldFile::Pointer worldFile = otb::WorldFile::New();
  worldFile->SetImageFilename(outputFilename);
  worldFile->SetLonOrigin(lonUL);
  worldFile->SetLatOrigin(latUL);
  worldFile->SetLonSpacing(lonSpacing);
  worldFile->SetLatSpacing(latSpacing);
  worldFile->Update();
227 228
  // Software Guide : EndCodeSnippet

Emmanuel Christophe's avatar
Emmanuel Christophe committed
229 230 231
  // Software Guide : BeginLatex
  //
  // Figure~\ref{fig:TILEMAPIMAGEIOEXAMPLE} shows the output images created from
232
  // open street map data.
Emmanuel Christophe's avatar
Emmanuel Christophe committed
233 234 235
  //
  // \begin{figure}
  // \center
236 237 238
  // \includegraphics[width=0.45\textwidth]{openStreetMap-Toulouse.eps}
  // \includegraphics[width=0.45\textwidth]{openStreetMap-Singapore.eps}
  // \itkcaption[Open street map]{Map created from open street map showing the OTB headquaters}
Emmanuel Christophe's avatar
Emmanuel Christophe committed
239 240 241
  // \label{fig:TILEMAPIMAGEIOEXAMPLE}
  // \end{figure}
  //
242
  // If your street is missing, go and improve the map by adding it yourself.
Emmanuel Christophe's avatar
Emmanuel Christophe committed
243 244
  //
  // Software Guide : EndLatex
245

246

247 248 249
  return EXIT_SUCCESS;

}