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

  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.

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

// Software Guide : BeginLatex
//
// This example demonstrates the use of the
22
// \doxygen{otb}{KmzProductWriter} and the
OTB Bot's avatar
OTB Bot committed
23
// \doxygen{otb}{MapFileProductWriter}.
24 25 26 27 28
// The first filter is intended to produce Kmz file (Google Earth
// Readable), and the second one is intended to produce map files
// callable through a WMS (Web Map Service) service. In this example
// we will  use a file with no meta-data, use the
// \doxygen{otb}{GGCPToRPCSensorModelImageFilter} in order to
OTB Bot's avatar
OTB Bot committed
29
// approximate a rpc model, and then use our filters to produce a kmz
30 31 32
// and MapFile products.
// Note that the \doxygen{otb}{KmzProductWriter} and the
// \doxygen{otb}{MapFileProductWriter} can only process inputs with a
33
// non empty geographical information.
34 35 36
//
// The first step toward the use of these filters is to include the
// proper header files: the one for the rpc sensor estimation filter and
37 38
// the one defining the procedure for creating kmz files and finally
// the header concerning the MapProduct writer.
39 40 41 42 43 44 45 46 47
//
// Software Guide : EndLatex

#include "otbMacro.h"
#include "otbVectorImage.h"
#include "otbImageFileReader.h"

// Software Guide : BeginCodeSnippet
#include "otbKmzProductWriter.h"
48
#include "otbMapFileProductWriter.h"
49 50 51 52 53 54 55
#include "otbGCPsToRPCSensorModelImageFilter.h"
// Software Guide : EndCodeSnippet
//

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

56
  if (argc < 6)
57
    {
OTB Bot's avatar
OTB Bot committed
58
    std::cerr << "Usage: " << argv[0]
Otmane Lahlou's avatar
Otmane Lahlou committed
59 60
              << " infname outfname kmzFileName "
              <<"a1x a1y b1x b1y b1z ... aNx aNy aNz bNx bNy bNz" << std::endl;
61 62
    return EXIT_FAILURE;
    }
63
  else if ((argc - 7) % 5 != 0)
64 65 66 67
    {
    std::cerr << "Inconsistent GCPs description!" << std::endl;
    return EXIT_FAILURE;
    }
68

69 70 71
// Software Guide : BeginLatex
//
// We will start by defining the types for the image and the image file
OTB Bot's avatar
OTB Bot committed
72
// reader.
73 74 75 76 77 78 79 80
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
  typedef otb::VectorImage<float, 2>                      ImageType;
  typedef otb::ImageFileReader<ImageType>                 ReaderType;

  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName(argv[1]);
81

82
  otb::DEMHandler::Instance()->OpenDEMDirectory(argv[6]);
83 84 85 86 87 88
// Software Guide : EndCodeSnippet

// Software Guide : BeginLatex
//
// We can now proceed to declare the type for the rpc sensor model
// estimation filter. The class \doxygen{otb}{GGCPToRPCSensorModelImageFilter} is
OTB Bot's avatar
OTB Bot committed
89
// templated over the input image type.
90 91 92 93 94 95 96 97 98 99 100 101
//
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
  typedef otb::GCPsToRPCSensorModelImageFilter<ImageType> GCPsToSensorModelFilterType;
  typedef GCPsToSensorModelFilterType::Point2DType        Point2DType;
  typedef GCPsToSensorModelFilterType::Point3DType        Point3DType;

  GCPsToSensorModelFilterType::Pointer rpcEstimator = GCPsToSensorModelFilterType::New();
  rpcEstimator->SetInput(reader->GetOutput());
// Software Guide : EndCodeSnippet

102
  unsigned int nbGCPs = (argc - 6) / 5;
103 104 105 106 107 108 109 110 111 112 113 114 115

  std::cout << "Receiving " << nbGCPs << " from command line." << std::endl;

  for (unsigned int gcpId = 0; gcpId < nbGCPs; ++gcpId)
    {
// Software Guide : BeginLatex
//
// Here, we set all the Ground Control Points associated to the image
// indexes. This is the entry of the rpc sensor model
// estimator. Everytime a GCP is added, the output image information
// or its keywordlist is updated. In general, a dozen of GCPs are
// needed to estimate an accurate sensor model. The points are added
// via the method AddGCP(PointType2D, PointType3D). The outpput image
116
// obtained have the needed meta-data information for the rest of the
117 118 119 120 121 122
// process.
//
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
    Point2DType sensorPoint;
123 124
    sensorPoint[0] = atof(argv[7 + gcpId * 5]);
    sensorPoint[1] = atof(argv[8 + gcpId * 5]);
125 126

    Point3DType geoPoint;
127 128 129
    geoPoint[0] = atof(argv[9 + 5 * gcpId]);
    geoPoint[1] = atof(argv[10 + 5 * gcpId]);
    geoPoint[2] = atof(argv[11+ 5 * gcpId]);
130 131 132 133 134 135 136 137 138

    rpcEstimator->AddGCP(sensorPoint, geoPoint);
// Software Guide : EndCodeSnippet

    std::cout << "Adding GCP sensor: " << sensorPoint << " <-> geo: " << geoPoint << std::endl;
    }

  rpcEstimator->GetOutput()->UpdateOutputInformation();
  std::cout << "Residual ground error: " << rpcEstimator->GetRMSGroundError() << std::endl;
139

140 141
// Software Guide : BeginLatex
//
OTB Bot's avatar
OTB Bot committed
142 143
// The last step of the chain, is to export the image to a Google
// Earth understandable format using the KmzProductWriter. Note that
144
// the writer can add legends via the method
OTB Bot's avatar
OTB Bot committed
145
// AddLegend(std::string description, ImageType * legend) and a logo in
OTB Bot's avatar
OTB Bot committed
146
// the kmz using SetLogo(ImageType*).
147 148 149 150 151 152 153 154 155 156 157
//
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
  typedef otb::KmzProductWriter<ImageType>     KmzProductWriterType;
  KmzProductWriterType::Pointer    kmzWriter  = KmzProductWriterType::New();
// Software Guide : EndCodeSnippet

// Software Guide : BeginLatex
//
// Finally, we trigger the kmz writting by calling the \code{Update()}
OTB Bot's avatar
OTB Bot committed
158
// method on the writer.
159 160 161 162 163 164 165 166 167
//
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
  kmzWriter->SetInput(rpcEstimator->GetOutput());
  kmzWriter->SetPath(argv[2]);
  kmzWriter->Update();
// Software Guide : EndCodeSnippet

168 169 170 171 172 173
// Software Guide : BeginLatex
//
// The Mapfile is the heart of MapServer. It defines the relationships
// between objects, points MapServer (http://mapserver.org/) to where
// data are located and defines how things are to be drawn. The class
// \doxygen{otb}{MapFileProductWriter} allow producing the mapserver
174
// configuration file, the tiles to draw, and shapefiles describing
175 176
// the tiles and where to find them.
// The Mapfile writer allow setting the complete path to the mapfile
OTB Bot's avatar
OTB Bot committed
177
// via SetFileName(std::string), the path where to store the tiles via
178 179
// the method SetShapeIndexPath() and finally the path to the cgi-bin
// to use via the method SetGCIPath().
OTB Bot's avatar
OTB Bot committed
180
//
181 182 183 184 185 186 187 188 189 190 191 192 193
//
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
  typedef otb::MapFileProductWriter<ImageType>   MapFileProductWriterType;
  MapFileProductWriterType::Pointer  mapWriter  = MapFileProductWriterType::New();
// Software Guide : EndCodeSnippet

// Software Guide : BeginLatex
//
// The user can also specify a Spatial Reference System Identifier
// (SRID) to choose his projection. In this example we choose WGS84 to
// project our datas in. The SRID relative to WGS84 is 4326.
194
// Finally, we trigger the MapFile writing by calling the
OTB Bot's avatar
OTB Bot committed
195
// \code{Update()}  method on the writer.
196 197 198 199 200 201 202 203 204
//
// Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
  mapWriter->SetInput(rpcEstimator->GetOutput());
  mapWriter->SetFileName(argv[3]);
  mapWriter->SetShapeIndexPath(argv[4]);
  mapWriter->SetCGIPath(argv[5]);
  mapWriter->SetSRID(4326);
205

206 207 208
  mapWriter->Update();
// Software Guide : EndCodeSnippet

209 210
  return EXIT_SUCCESS;
}