File: MapProjectionExample.cxx

package info (click to toggle)
otb 6.6.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,068 kB
  • sloc: cpp: 316,755; ansic: 4,474; sh: 1,610; python: 497; perl: 92; makefile: 82; java: 72
file content (219 lines) | stat: -rw-r--r-- 7,466 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * 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.
 */



//  Software Guide : BeginCommandLineArgs
//    OUTPUTS: {mapProjectionExample-output.txt}
//  Software Guide : EndCommandLineArgs

// Software Guide : BeginLatex
//
// 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.
//
// 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.
//
// 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.
//
// The otbGenericMapProjection enables you to instantiate a map
// projection from a WKT (Well Known Text) string, which is popular
// with OGR for example.
//
// Software Guide : EndLatex

#include <fstream>
#include <iomanip>

// Software Guide : BeginCodeSnippet
#include "otbMapProjections.h"
#include "otbGenericMapProjection.h"
// Software Guide : EndCodeSnippet

int main(int argc, char* argv[])
{
  if (argc < 2)
    {
    std::cout << argv[0] << " <outputfile> "  << std::endl;

    return EXIT_FAILURE;
    }

  // Software Guide : BeginLatex
  //
  // We retrieve the command line parameters and put them in the
  // correct variables. The transforms are going to work with an
  // \doxygen{itk}{Point}.
  //
  // Software Guide : EndLatex

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

  itk::Point<double, 2> point;
  point[0] = 1.4835345;
  point[1] = 43.55968261;
  // Software Guide : EndCodeSnippet

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

  // Software Guide : BeginCodeSnippet
  std::ofstream file;
  file.open(outFileName);
  file << std::setprecision(15);
  // Software Guide : EndCodeSnippet

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

  // Software Guide : BeginCodeSnippet
  otb::UtmForwardProjection::Pointer utmProjection
    = otb::UtmForwardProjection::New();
  utmProjection->SetZone(31);
  utmProjection->SetHemisphere('N');
  // Software Guide : EndCodeSnippet

  // Software Guide : BeginLatex
  //
  // The TransformPoint() method returns the coordinates of the point in the
  // 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
  otb::Lambert93ForwardProjection::Pointer lambertProjection
    = otb::Lambert93ForwardProjection::New();

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

  // Software Guide : BeginLatex
  //
  // 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).
  //
  // 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.
  //
  // For example:
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
  std::string projectionRefWkt = "PROJCS[\"UTM Zone 31, Northern Hemisphere\","
                                 "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]]";
  // Software Guide : EndCodeSnippet

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

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

  file << "Forward generic projection: " << std::endl;
  file << point << " -> ";
  file << genericMapProjection->TransformPoint(point);
  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:
  //
  //  \begin{verbatim}
  //   Forward UTM projection:
  //       [1.4835345, 43.55968261] -> [377522.448427013, 4824086.71129131]
  //
  //   Forward Lambert93 projection:
  //      [1.4835345, 43.55968261] -> [577437.889798954, 6274578.791561]
  //
  //   Forward generic projection:
  //      [1.4835345, 43.55968261] -> [377522.448427013, 4824086.71129131]
  //   \end{verbatim}
  //
  // Software Guide : EndLatex

  return EXIT_SUCCESS;
}