MapProjectionExample.cxx 7.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
20

21

22

23
//  Software Guide : BeginCommandLineArgs
24
//    OUTPUTS: {mapProjectionExample-output.txt}
25 26 27 28
//  Software Guide : EndCommandLineArgs

// Software Guide : BeginLatex
//
29 30 31 32 33
// Map projection is an important issue when working with satellite
// images. In the orthorectification process, converting between
// geographic and cartographic coordinates is a key step. In this
// process, everything is integrated and you don't need to know the
// details.
34
//
35 36 37 38
// However, sometimes, you need to go hands-on and find out the
// nitty-gritty details.  This example shows you how to play with map
// projections in OTB and how to convert coordinates. In most cases,
// the underlying work is done by OSSIM.
39
//
40 41 42
// First, we start by including the otbMapProjections header. In this
// file, over 30 projections are defined and ready to use. It is easy
// to add new one.
43
//
44
// The otbGenericMapProjection enables you to instantiate a map
45 46
// projection from a WKT (Well Known Text) string, which is popular
// with OGR for example.
47 48 49
//
// Software Guide : EndLatex

50 51 52
#include <fstream>
#include <iomanip>

53
// Software Guide : BeginCodeSnippet
54
#include "otbMapProjections.h"
55 56
#include "otbGenericMapProjection.h"
// Software Guide : EndCodeSnippet
57

OTB Bot's avatar
STYLE  
OTB Bot committed
58
int main(int argc, char* argv[])
59
{
OTB Bot's avatar
STYLE  
OTB Bot committed
60 61 62
  if (argc < 2)
    {
    std::cout << argv[0] << " <outputfile> "  << std::endl;
63 64

    return EXIT_FAILURE;
OTB Bot's avatar
STYLE  
OTB Bot committed
65
    }
66

67 68
  // Software Guide : BeginLatex
  //
69 70 71
  // We retrieve the command line parameters and put them in the
  // correct variables. The transforms are going to work with an
  // \doxygen{itk}{Point}.
72 73 74 75 76 77
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
  const char * outFileName = argv[1];

OTB Bot's avatar
STYLE  
OTB Bot committed
78 79 80
  itk::Point<double, 2> point;
  point[0] = 1.4835345;
  point[1] = 43.55968261;
81 82 83 84
  // Software Guide : EndCodeSnippet

  // Software Guide : BeginLatex
  //
85 86
  // The output of this program will be saved in a text file. We also want
  // to make sure that the precision of the digits will be enough.
87 88 89 90
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
91 92 93
  std::ofstream file;
  file.open(outFileName);
  file << std::setprecision(15);
94 95 96 97
  // Software Guide : EndCodeSnippet

  // Software Guide : BeginLatex
  //
98 99 100 101
  // We can now instantiate our first map projection. Here, it is a
  // UTM projection. We also need to provide the information about
  // the zone and the hemisphere for the projection. These are
  // specific to the UTM projection.
102 103 104 105
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
106
  otb::UtmForwardProjection::Pointer utmProjection
OTB Bot's avatar
STYLE  
OTB Bot committed
107
    = otb::UtmForwardProjection::New();
108 109 110 111 112 113
  utmProjection->SetZone(31);
  utmProjection->SetHemisphere('N');
  // Software Guide : EndCodeSnippet

  // Software Guide : BeginLatex
  //
114
  // The TransformPoint() method returns the coordinates of the point in the
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  // new projection.
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
  file << "Forward UTM projection: " << std::endl;
  file << point << " -> ";
  file << utmProjection->TransformPoint(point);
  file << std::endl << std::endl;
  // Software Guide : EndCodeSnippet

  // Software Guide : BeginLatex
  //
  // We follow the same path for the Lambert93 projection:
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
133
  otb::Lambert93ForwardProjection::Pointer lambertProjection
OTB Bot's avatar
STYLE  
OTB Bot committed
134
    = otb::Lambert93ForwardProjection::New();
135 136 137 138 139 140 141 142 143

  file << "Forward Lambert93 projection: " << std::endl;
  file << point << " -> ";
  file << lambertProjection->TransformPoint(point);
  file << std::endl << std::endl;
  // Software Guide : EndCodeSnippet

  // Software Guide : BeginLatex
  //
144 145 146 147 148 149
  // If you followed carefully the previous examples, you've noticed
  // that the target projections have been directly coded, which means
  // that they can't be changed at run-time. What happens if you don't
  // know the target projection when you're writing the program? It
  // can depend on some input provided by the user (image,
  // shapefile).
150
  //
151 152 153
  // In this situation, you can use the
  // \doxygen{otb}{GenericMapProjection}. It will accept a string to
  // set the projection. This string should be in the WKT format.
154 155 156 157 158 159
  //
  // For example:
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
160
  std::string projectionRefWkt = "PROJCS[\"UTM Zone 31, Northern Hemisphere\","
OTB Bot's avatar
STYLE  
OTB Bot committed
161 162 163 164 165 166 167 168 169
                                 "GEOGCS[\"WGS 84\", DATUM[\"WGS_1984\", SPHEROID[\"WGS 84\", 6378137, 298.257223563,"
                                 "AUTHORITY[\"EPSG\",\"7030\"]], TOWGS84[0, 0, 0, 0, 0, 0, 0],"
                                 "AUTHORITY[\"EPSG\",\"6326\"]], PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\",\"8901\"]],"
                                 "UNIT[\"degree\", 0.0174532925199433, AUTHORITY[\"EPSG\",\"9108\"]],"
                                 "AXIS[\"Lat\", NORTH], AXIS[\"Long\", EAST],"
                                 "AUTHORITY[\"EPSG\",\"4326\"]], PROJECTION[\"Transverse_Mercator\"],"
                                 "PARAMETER[\"latitude_of_origin\", 0], PARAMETER[\"central_meridian\", 3],"
                                 "PARAMETER[\"scale_factor\", 0.9996], PARAMETER[\"false_easting\", 500000],"
                                 "PARAMETER[\"false_northing\", 0], UNIT[\"Meter\", 1]]";
170 171 172 173
  // Software Guide : EndCodeSnippet

  // Software Guide : BeginLatex
  //
174 175
  // This string is then passed to the projection using the
  // \code{SetWkt()} method.
176 177 178 179
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
180
  typedef otb::GenericMapProjection<otb::TransformDirection::FORWARD> GenericMapProjection;
181
  GenericMapProjection::Pointer genericMapProjection =
182
    GenericMapProjection::New();
183 184
  genericMapProjection->SetWkt(projectionRefWkt);

185
  file << "Forward generic projection: " << std::endl;
186
  file << point << " -> ";
OTB Bot's avatar
STYLE  
OTB Bot committed
187
  file << genericMapProjection->TransformPoint(point);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
  file << std::endl << std::endl;
  // Software Guide : EndCodeSnippet

  // Software Guide : BeginLatex
  //
  // And of course, we don't forget to close the file:
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
  file.close();
  // Software Guide : EndCodeSnippet

  // Software Guide : BeginLatex
  //
  // The final output of the program should be:
  //
205 206 207 208 209 210
  //  \begin{verbatim}
  //   Forward UTM projection:
  //       [1.4835345, 43.55968261] -> [377522.448427013, 4824086.71129131]
  //
  //   Forward Lambert93 projection:
  //      [1.4835345, 43.55968261] -> [577437.889798954, 6274578.791561]
211
  //
Emmanuel Christophe's avatar
Emmanuel Christophe committed
212
  //   Forward generic projection:
213 214
  //      [1.4835345, 43.55968261] -> [377522.448427013, 4824086.71129131]
  //   \end{verbatim}
215 216
  //
  // Software Guide : EndLatex
217 218 219

  return EXIT_SUCCESS;
}