TileMapImageIOExample.cxx 6.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*=========================================================================

  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.

=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

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

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

// 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
42 43 44 45 46 47 48 49
#include "itkRGBPixel.h"
#include "otbImage.h"
#include "otbImageFileReader.h"
#include "otbTileMapImageIO.h"
#include "otbInverseSensorModel.h"
#include "otbExtractROI.h"
#include "otbImageFileWriter.h"
#include "ossim/projection/ossimTileMapModel.h"
Emmanuel Christophe's avatar
Emmanuel Christophe committed
50
// Software Guide : EndCodeSnippet
51 52 53 54 55 56 57 58 59 60 61 62 63 64

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

  if(argc!=7)
  {
    std::cout << argv[0] <<" <inputFilename> <outputFilename> "
        << "<cacheDirectory> <lon> <lat> <depth>"
        << std::endl;

    return EXIT_FAILURE;
  }


Emmanuel Christophe's avatar
Emmanuel Christophe committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  // 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
83 84 85 86 87 88
  char * inputFilename = argv[1];
  char * outputFilename = argv[2];
  char * cacheDirectory = argv[3];
  double lon = atof(argv[4]);
  double lat = atof(argv[5]);
  int depth = atoi(argv[6]);
Emmanuel Christophe's avatar
Emmanuel Christophe committed
89 90 91 92
  // Software Guide : EndCodeSnippet

  // Software Guide : BeginLatex
  //
93 94
  // 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
95 96
  //
  // Software Guide : EndLatex
97

Emmanuel Christophe's avatar
Emmanuel Christophe committed
98
  // Software Guide : BeginCodeSnippet
99 100 101 102 103 104 105 106 107 108 109 110
  typedef itk::RGBPixel<unsigned char> RGBPixelType;
  typedef otb::Image<RGBPixelType, 2> ImageType;
  typedef otb::ImageFileReader<ImageType> ReaderType;
  typedef otb::TileMapImageIO ImageIOType;

  ImageIOType::Pointer tileIO = ImageIOType::New();
  ReaderType::Pointer readerTile = ReaderType::New();
  tileIO->SetDepth(depth);
  tileIO->SetCacheDirectory(cacheDirectory);
  readerTile->SetImageIO(tileIO);
  readerTile->SetFileName(inputFilename);
  readerTile->UpdateOutputInformation();
Emmanuel Christophe's avatar
Emmanuel Christophe committed
111 112 113 114
  // Software Guide : EndCodeSnippet

  // Software Guide : BeginLatex
  //
115
  // Now, we potentially have an image of several Peta-Bytes covering the whole world
Emmanuel Christophe's avatar
Emmanuel Christophe committed
116 117 118 119
  // in the reader
  // that's why we don't want to do an update before extracting a specific
  // area.
  //
120
  // The coordinates are refered with an origin at the North Pole and the
Emmanuel Christophe's avatar
Emmanuel Christophe committed
121 122 123 124 125 126
  // 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
127 128 129 130 131 132 133 134 135 136 137
  typedef otb::InverseSensorModel<double>  ModelType;
  ModelType::Pointer   model= ModelType::New();

  model->SetImageGeometry(readerTile->GetOutput()->GetImageKeywordlist());
  dynamic_cast<ossimTileMapModel*>(model->GetOssimModel())->setDepth(depth);
  if(!model)
  {
    std::cerr << "Unable to create a model" << std::endl;
    return 1;
  }

Emmanuel Christophe's avatar
Emmanuel Christophe committed
138

139 140 141 142 143 144 145
  typedef itk::Point <double, 2> PointType;
  PointType lonLatPoint;
  lonLatPoint[0]=lon;
  lonLatPoint[1]=lat;

  PointType tilePoint;
  tilePoint=model->TransformPoint(lonLatPoint);
Emmanuel Christophe's avatar
Emmanuel Christophe committed
146
  // Software Guide : EndCodeSnippet
147

Emmanuel Christophe's avatar
Emmanuel Christophe committed
148 149
  // Software Guide : BeginLatex
  //
150
  // This enables us to use the \doxygen{otb}{ExtractROI} to retrieve only
Emmanuel Christophe's avatar
Emmanuel Christophe committed
151 152 153 154 155
  // the area of interest and to avoid crashing our memory-limited computer.
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  long int startX=static_cast<long int>(tilePoint[0]);
  long int startY=static_cast<long int>(tilePoint[1]);
  long int sizeX = 500;
  long int sizeY = 500;

  std::cerr << startX <<", "<< startY << std::endl;
  std::cerr << sizeX <<", "<< sizeY << std::endl;

  typedef otb::ExtractROI< RGBPixelType,  RGBPixelType >  ExtractROIFilterType;
  ExtractROIFilterType::Pointer extractROIOsmFilter = ExtractROIFilterType::New();
  extractROIOsmFilter->SetStartX(startX-sizeX/2);
  extractROIOsmFilter->SetStartY(startY-sizeY/2);
  extractROIOsmFilter->SetSizeX( sizeX );
  extractROIOsmFilter->SetSizeY( sizeY );

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

Emmanuel Christophe's avatar
Emmanuel Christophe committed
174 175 176 177 178 179
  // Software Guide : BeginLatex
  //
  // Finally, we just plug this to the writer to save our nice map of
  // the area:
  //
  // Software Guide : EndLatex
180

Emmanuel Christophe's avatar
Emmanuel Christophe committed
181
  // Software Guide : BeginCodeSnippet
182 183 184 185 186
  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
187 188 189 190 191 192 193 194 195 196 197
  // Software Guide : EndCodeSnippet


  // Software Guide : BeginLatex
  //
  // Figure~\ref{fig:TILEMAPIMAGEIOEXAMPLE} shows the output images created from
  // open street map data.
  //
  //
  // \begin{figure}
  // \center
198 199 200
  // \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
201 202 203 204 205 206
  // \label{fig:TILEMAPIMAGEIOEXAMPLE}
  // \end{figure}
  //
  // If your street is missing, go and improve the map.
  //
  // Software Guide : EndLatex
207 208 209 210

  return EXIT_SUCCESS;

}