Loading...
Searching...
No Matches
Converter.h
Go to the documentation of this file.
1/* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2
3 This file is part of the TerraLib - a Framework for building GIS enabled applications.
4
5 TerraLib is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 TerraLib is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with TerraLib. See COPYING. If not, write to
17 TerraLib Team at <terralib-team@terralib.org>.
18 */
19
20/*!
21 \file Converter.h
22
23 \brief This file contains the support to convert coordinates from a SRS to another.
24 */
25
26#ifndef __TERRALIB_SRS_INTERNAL_CONVERTER_H
27#define __TERRALIB_SRS_INTERNAL_CONVERTER_H
28
29// TerraLib
30#include "Config.h"
31
32// STL
33#include <map>
34#include <string>
35#include <memory>
36
37namespace te
38{
39 namespace srs
40 {
41 /*!
42 \class Converter
43
44 \brief A Converter is responsible for the conversion of coordinates between different Coordinate Systems (CS).
45
46 A Converter is responsible for the conversion of coordinates between two different Coordinate Systems (CS) or a Spatial Reference System (SRS).
47 A CS can be uniquely identified by a numeric code (SRID). This implementation is based on the PROJ4 cartographic library and only works if it has been enabled.
48
49 \ingroup srs
50
51 \todo Methods to convert 3D coordinates.
52 */
54 {
55 public:
56 //! Default empty constructor.
58
59 /*!
60 \brief Constructor with parameters.
61 \param sourceSRID source SRS identifier (input).
62 \param targetSRID target SRS identifier (input).
63 \exception te::srs::Exception identifier not recognized.
64 */
65 Converter(int sourceSRID, int targetSRID);
66
67 //! Destructor
69
70 /*!
71 \brief Sets the source SRS identifier.
72 \param sourceSRID the source SRS identifier (input).
73 \exception te::srs::Exception identifier not recognized.
74 */
75 void setSourceSRID(int sourceSRID);
76
77 /*!
78 \brief Sets the source SRS PROJ4 description.
79 \param pj4txt PROJ4 description (input). Do not pass an empty string.
80 \exception te::srs::Exception PROJ4 description is not valid.
81 */
82 void setSourcePJ4txt(const std::string& pj4txt);
83
84 //! Gets source SRS identifier.
85 int getSourceSRID() const;
86
87 /*!
88 \brief Sets the target SRS identifier.
89 \param targetSRID the target SRS identifier (input).
90 \exception te::srs::Exception identifier not recognized.
91 */
92 void setTargetSRID(int targetSRID);
93
94 /*!
95 \brief Sets the target SRS PROJ4 description.
96 \param pj4txt PROJ4 description (input). Do not pass an empty string.
97 \exception te::srs::Exception PROJ4 description is not valid.
98 */
99 void setTargetPJ4txt(const std::string& pj4txt);
100
101 //! Gets target SRS identifier.
102 int getTargetSRID() const;
103
104 /*!
105 \brief Converts a vector of coordinates from source SRS to target SRS.
106
107 The X and Y dimensions of the coordinates are given in separate vectors.
108 Output vectors must be previously allocaded by the caller of this method and caller is responsible for deallocating them.
109
110 \param xIn pointer to array of X values in source SRS (input).
111 \param yIn pointer to array of Y valueS in source SRS (input).
112
113 \param xOut pointer to array of X values in target SRS (output).
114 \param yOut pointer to array of X values in target SRS (output).
115
116 \param numCoord number of coordinates in the input arrays (input).
117 \param coordOffset the step size from value to value (measured in doubles) within the xIn/yIn arrays (input).
118 \return true if succeed and false otherwise.
119 */
120 bool convert(double *xIn, double *yIn, double *xOut, double* yOut, long numCoord, int coordOffset=1) const;
121
122 /*!
123 \brief Converts a vector of coordinates from source SRS to target SRS.
124 \param x pointer to array of X values in source SRS as input and modified to target SRS for output.
125 \param y pointer to array of Y valueS in source SRS as input and modified to target SRS for output.
126 \param numCoord number of coordinates in the array (input).
127 \param coordOffset the step size from value to value (measured in doubles) within the x/y arrays (input).
128 \return true if succeed and false otherwise.
129 */
130 bool convert(double *x, double* y, long numCoord, int coordOffset=1) const;
131
132 /*!
133 \brief Converts a single coordinate from source SRS to target SRS.
134 \param xIn coordinate X value in source SRS (input).
135 \param yIn coordinate Y value in source SRS (input).
136 \param xOut coordinate X value in target SRS (output).
137 \param yOut coordinate Y values in target SRS (output).
138 \return true if succeed and false otherwise.
139 */
140 inline bool convert(const double& xIn, const double& yIn, double &xOut, double &yOut) const
141 {
142 xOut = xIn;
143 yOut = yIn;
144 return convert( xOut, yOut );
145 };
146
147 /*!
148 \brief Converts a coordinate from source SRS to target SRS.
149 \param x X value in source SRS as input and modified to target SRS for output.
150 \param y Y value in source SRS as input and modified to target SRS for output.
151 \return true if succeed and false otherwise.
152 */
153 bool convert(double &x, double &y) const;
154
155 /*!
156 \brief Inverts a vector of coordinates from target SRS to dource SRS.
157
158 The X and Y dimensions of the coordinates are given in separate vectors.
159 Output vectors must be previously allocaded by the caller of this method, and caller is responsible for deallocating them.
160
161 \param xIn pointer to array of X values in target SRS (input).
162 \param yIn pointer to array of Y valueS in target SRS (input).
163 \param xOut pointer to array of X values in source SRS (output).
164 \param yOut pointer to array of Y values in source SRS (output).
165 \param numCoord number of coordinates in the array (input).
166 \param coordOffset the step size from value to value (in double size) within the x/y arrays (input).
167 \return true if succeed and false otherwise.
168 */
169 bool invert(double *xIn, double *yIn, double *xOut, double* yOut, long numCoord, int coordOffset=1) const;
170
171 /*!
172 \brief Inverts a vector of coordinates from target SRS to source SRS
173 \param x pointer to array of X values in target SRS as input and modified to source SRS for output.
174 \param y pointer to array of Y values in target SRS as input and modified to source SRS for output.
175 \param numCoord number of coordinates in the array (input).
176 \param coordOffset the step size from value to value in double size) within the x/y arrays (input).
177 \return true if succeed and false otherwise.
178 */
179 bool invert(double *x, double* y, long numCoord, int coordOffset=1) const;
180
181 /*!
182 \brief Inverts a coordinate from source SRS to target SRS.
183 \param xIn pointer to array of X values in target SRS (input).
184 \param yIn pointer to array of Y valueS in target SRS (input).
185 \param xOut pointer to array of X values in source SRS (output).
186 \param yOut pointer to array of Y values in source SRS (output).
187 \return true if succeed and false otherwise.
188 */
189 inline bool invert(const double& xIn, const double& yIn, double &xOut, double &yOut) const
190 {
191 xOut = xIn;
192 yOut = yIn;
193 return invert( xOut, yOut );
194 };
195
196 /*!
197 \brief Inverts a coordinate from target SRS to source SRS
198 \param x pointer to array of X values in target SRS as inputand modified to source SRS for output.
199 \param y pointer to array of Y values in target SRS as inputand modified to source SRS for output.
200 \return true if succeed and false otherwise.
201 */
202 bool invert(double &x, double &y) const;
203
204 /*!
205 \brief Converts a coordinate from a projected SRS to its underlying geographic SRS (same Datum).
206 \param x projected X-coordinate. Will return the geographic longitude coordinate.
207 \param y projected Y-coordinate. Will return the geographic latitude coordinate.
208 \param SRID projected SRS identifier that x and y refers to.
209 \return true if succeed and false otherwise.
210 \deprecated This method is deprecated and will be removed in a near future.
211 */
212 static bool convertToGeographic(double& x, double& y, int SRID);
213
214 /*!
215 \brief Converts a coordinate from a geographic SRS to a projected SRS based on the same Datum.
216 \param lon geographic longitude. Will return the projected x-coordinate.
217 \param lat geogrpahic latitude. Will return the projected y-coordinate.
218 \param SRID target projected SRS identifier.
219 \return true if succeed and false otherwise.
220 \deprecated This method is deprecated and will be removed in a near future.
221 */
222 static bool convertToProjected(double &lon, double &lat, int SRID);
223
224 /*!
225 \brief Creates a clone of this instance.
226 \return A clone of this instance.
227 \note The caller of this method must take the ownership of the returned object.
228 */
229 Converter* clone() const;
230
231 private:
232
235
236 // members updated by the method update()
237 void* m_sourcePj4Handler; // Proj4 handler to source SRS
238 void* m_targetPj4Handler; // Proj4 handler to target SRS
239 void* m_transfPjHandler; // Proj transformation handler.
240 void* m_transfThreadContextHandler; // Proj transformation thread context handler.
241 void* m_nonNormalizedTransfPjHandler; // Proj CRS to SRS handler
242 bool m_sourceIsLatLong; // Source unit is in radians
243 bool m_targetIsLatLong; // Targe unit is in radians
244
245 std::string m_sourceCRS; // Proj source CRS string.
246 std::string m_targetCRS; // Proj source CRS string.
247
248 // variables used by the methods convert/invert
249 mutable int m_convert_res;
252
253 /*!
254 \brief Update the internal state following the current source/target CRS strings.
255 \param errorString Error string.
256 \return true if OK, false on errros.
257 \note This is NOT A THREAD SAFE method.
258 */
259 bool update( std::string& errorString );
260
261 };
262
263 typedef std::unique_ptr<Converter> ConverterPtr; //!< \typedef ConverterPtr an auto pointer to a Converter.
264 }
265} // end TerraLib
266
267#endif // __TERRALIB_SRS_INTERNAL_CONVERTER_H
A Converter is responsible for the conversion of coordinates between different Coordinate Systems (CS...
Definition: Converter.h:54
bool convert(double *x, double *y, long numCoord, int coordOffset=1) const
Converts a vector of coordinates from source SRS to target SRS.
void setSourceSRID(int sourceSRID)
Sets the source SRS identifier.
bool invert(double *xIn, double *yIn, double *xOut, double *yOut, long numCoord, int coordOffset=1) const
Inverts a vector of coordinates from target SRS to dource SRS.
void * m_transfPjHandler
Definition: Converter.h:239
double m_convert_xOut
Definition: Converter.h:250
bool invert(double &x, double &y) const
Inverts a coordinate from target SRS to source SRS.
void * m_nonNormalizedTransfPjHandler
Definition: Converter.h:241
Converter * clone() const
Creates a clone of this instance.
bool convert(double &x, double &y) const
Converts a coordinate from source SRS to target SRS.
bool convert(const double &xIn, const double &yIn, double &xOut, double &yOut) const
Converts a single coordinate from source SRS to target SRS.
Definition: Converter.h:140
void setTargetPJ4txt(const std::string &pj4txt)
Sets the target SRS PROJ4 description.
void setSourcePJ4txt(const std::string &pj4txt)
Sets the source SRS PROJ4 description.
bool update(std::string &errorString)
Update the internal state following the current source/target CRS strings.
~Converter()
Destructor.
static bool convertToProjected(double &lon, double &lat, int SRID)
Converts a coordinate from a geographic SRS to a projected SRS based on the same Datum.
bool invert(const double &xIn, const double &yIn, double &xOut, double &yOut) const
Inverts a coordinate from source SRS to target SRS.
Definition: Converter.h:189
bool convert(double *xIn, double *yIn, double *xOut, double *yOut, long numCoord, int coordOffset=1) const
Converts a vector of coordinates from source SRS to target SRS.
int getTargetSRID() const
Gets target SRS identifier.
Converter()
Default empty constructor.
double m_convert_yOut
Definition: Converter.h:251
Converter(int sourceSRID, int targetSRID)
Constructor with parameters.
std::string m_targetCRS
Definition: Converter.h:246
void setTargetSRID(int targetSRID)
Sets the target SRS identifier.
void * m_transfThreadContextHandler
Definition: Converter.h:240
void * m_targetPj4Handler
Definition: Converter.h:238
std::string m_sourceCRS
Definition: Converter.h:245
bool invert(double *x, double *y, long numCoord, int coordOffset=1) const
Inverts a vector of coordinates from target SRS to source SRS.
int getSourceSRID() const
Gets source SRS identifier.
static bool convertToGeographic(double &x, double &y, int SRID)
Converts a coordinate from a projected SRS to its underlying geographic SRS (same Datum).
void * m_sourcePj4Handler
Definition: Converter.h:237
TerraLib.
#define TESRSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:377
Proxy configuration file for TerraView (see terraview_config.h).