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 
37 namespace 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.
57  Converter();
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
68  ~Converter();
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  bool convert(const double xIn, const double yIn, double &xOut, double &yOut) const;
141 
142  /*!
143  \brief Converts a coordinate from source SRS to target SRS.
144  \param x X value in source SRS as input and modified to target SRS for output.
145  \param y Y value in source SRS as input and modified to target SRS for output.
146  \return true if succeed and false otherwise.
147  */
148  bool convert(double &x, double &y) const;
149 
150  /*!
151  \brief Inverts a vector of coordinates from target SRS to dource SRS.
152 
153  The X and Y dimensions of the coordinates are given in separate vectors.
154  Output vectors must be previously allocaded by the caller of this method, and caller is responsible for deallocating them.
155 
156  \param xIn pointer to array of X values in target SRS (input).
157  \param yIn pointer to array of Y valueS in target SRS (input).
158  \param xOut pointer to array of X values in source SRS (output).
159  \param yOut pointer to array of Y values in source SRS (output).
160  \param numCoord number of coordinates in the array (input).
161  \param coordOffset the step size from value to value (in double size) within the x/y arrays (input).
162  \return true if succeed and false otherwise.
163  */
164  bool invert(double *xIn, double *yIn, double *xOut, double* yOut, long numCoord, int coordOffset=1) const;
165 
166  /*!
167  \brief Inverts a vector of coordinates from target SRS to source SRS
168  \param x pointer to array of X values in target SRS as input and modified to source SRS for output.
169  \param y pointer to array of Y values in target SRS as input and modified to source SRS for output.
170  \param numCoord number of coordinates in the array (input).
171  \param coordOffset the step size from value to value in double size) within the x/y arrays (input).
172  \return true if succeed and false otherwise.
173  */
174  bool invert(double *x, double* y, long numCoord, int coordOffset=1) const;
175 
176  /*!
177  \brief Inverts a coordinate from source SRS to target SRS.
178  \param xIn pointer to array of X values in target SRS (input).
179  \param yIn pointer to array of Y valueS in target SRS (input).
180  \param xOut pointer to array of X values in source SRS (output).
181  \param yOut pointer to array of Y values in source SRS (output).
182  \return true if succeed and false otherwise.
183  */
184  bool invert(const double xIn, const double yIn, double &xOut, double &yOut) const;
185 
186  /*!
187  \brief Inverts a coordinate from target SRS to source SRS
188  \param x pointer to array of X values in target SRS as inputand modified to source SRS for output.
189  \param y pointer to array of Y values in target SRS as inputand modified to source SRS for output.
190  \return true if succeed and false otherwise.
191  */
192  bool invert(double &x, double &y) const;
193 
194  /*!
195  \brief Converts a coordinate from a projected SRS to its underlying geographic SRS (same Datum).
196  \param x projected X-coordinate. Will return the geographic longitude coordinate.
197  \param y projected Y-coordinate. Will return the geographic latitude coordinate.
198  \param SRID projected SRS identifier that x and y refers to.
199  \return true if succeed and false otherwise.
200  */
201  bool convertToGeographic(double& x, double& y, int SRID) const;
202 
203  /*!
204  \brief Converts a coordinate from a geographic SRS to a projected SRS based on the same Datum.
205  \param lon geographic longitude. Will return the projected x-coordinate.
206  \param lat geogrpahic latitude. Will return the projected y-coordinate.
207  \param SRID target projected SRS identifier.
208  \return true if succeed and false otherwise.
209  */
210  bool convertToProjected(double &lon, double &lat, int SRID) const;
211 
212  private:
213 
216 
217  void* m_sourcePj4Handler; // Proj4 handler to source SRS
218  void* m_targetPj4Handler; // Proj4 handler to target SRS
219 
220  };
221 
222  typedef std::auto_ptr<Converter> ConverterPtr; //!< \typedef ConverterPtr an auto pointer to a Converter.
223  }
224 } // end TerraLib
225 
226 #endif // __TERRALIB_SRS_INTERNAL_CONVERTER_H
void * m_sourcePj4Handler
Definition: Converter.h:217
URI C++ Library.
A Converter is responsible for the conversion of coordinates between different Coordinate Systems (CS...
Definition: Converter.h:53
void * m_targetPj4Handler
Definition: Converter.h:218
std::string TECOMMONEXPORT convert(const path &v)
URI path to string.
#define TESRSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:364