25 #include "../common/Translator.h"
44 m_sourcePj4Handler(0),
50 m_targetSRID(targetSRID),
51 m_sourceSRID(sourceSRID),
52 m_sourcePj4Handler(0),
57 if (description.empty())
58 throw te::srs::Exception(
TR_SRS(
"Source SRS ID not recognized."));
63 std::string exceptionTxt =
TR_SRS(
"Source SRS description is not valid: ");
64 char* pjError = pj_strerrno(*(pj_get_errno_ref()));
65 exceptionTxt += std::string(pjError);
66 throw te::srs::Exception(exceptionTxt);
70 if ( description.empty())
71 throw te::srs::Exception(
TR_SRS(
"Target SRS ID not recognized."));
76 std::string exceptionTxt =
TR_SRS(
"Target SRS description is not valid: ");
77 char* pjError = pj_strerrno(*(pj_get_errno_ref()));
78 exceptionTxt += std::string(pjError);
79 throw te::srs::Exception(exceptionTxt);
90 if (m_sourcePj4Handler)
91 pj_free(m_sourcePj4Handler);
93 if (m_targetPj4Handler)
94 pj_free(m_targetPj4Handler);
96 m_sourcePj4Handler = 0;
97 m_targetPj4Handler = 0;
104 if (m_sourcePj4Handler)
106 pj_free(m_sourcePj4Handler);
107 m_sourcePj4Handler = 0;
111 if ( description.empty())
112 throw te::srs::Exception(
TR_SRS(
"Source SRS ID not recognized."));
114 m_sourcePj4Handler = pj_init_plus(description.c_str());
115 if (!m_sourcePj4Handler)
117 std::string exceptionTxt =
TR_SRS(
"Source SRS description is not valid: ");
118 char* pjError = pj_strerrno(*(pj_get_errno_ref()));
119 exceptionTxt += std::string(pjError);
120 throw te::srs::Exception(exceptionTxt);
123 m_sourceSRID = sourceSRID;
128 assert(!pj4txt.empty());
131 if (m_sourcePj4Handler)
133 pj_free(m_sourcePj4Handler);
134 m_sourcePj4Handler = 0;
137 m_sourcePj4Handler = pj_init_plus(pj4txt.c_str());
138 if (!m_sourcePj4Handler)
140 std::string exceptionTxt =
TR_SRS(
"Source SRS PROJ4 description is not valid: ");
141 char* pjError = pj_strerrno(*(pj_get_errno_ref()));
142 exceptionTxt += std::string(pjError);
143 throw te::srs::Exception(exceptionTxt);
159 if (m_targetPj4Handler)
161 pj_free(m_targetPj4Handler);
162 m_targetPj4Handler = 0;
166 if ( description.empty())
167 throw te::srs::Exception(
TR_SRS(
"Target SRS ID not recognized."));
169 m_targetPj4Handler = pj_init_plus(description.c_str());
170 if (!m_targetPj4Handler)
172 std::string exceptionTxt =
TR_SRS(
"Target SRS description is not valid: ");
173 char* pjError = pj_strerrno(*(pj_get_errno_ref()));
174 exceptionTxt += std::string(pjError);
175 throw te::srs::Exception(exceptionTxt);
178 m_targetSRID = targetSRID;
183 assert(!pj4txt.empty());
186 if (m_targetPj4Handler)
188 pj_free(m_targetPj4Handler);
189 m_targetPj4Handler = 0;
192 m_targetPj4Handler = pj_init_plus(pj4txt.c_str());
193 if (!m_targetPj4Handler)
195 std::string exceptionTxt =
TR_SRS(
"Target SRS PROJ4 description is not valid: ");
196 char* pjError = pj_strerrno(*(pj_get_errno_ref()));
197 exceptionTxt += std::string(pjError);
198 throw te::srs::Exception(exceptionTxt);
214 assert(m_sourcePj4Handler);
215 assert(m_targetPj4Handler);
217 memcpy(xOut, xIn, numCoord*
sizeof(
double));
218 memcpy(yOut, yIn, numCoord*
sizeof(
double));
220 if (pj_is_latlong(m_sourcePj4Handler))
221 for (
long i=0; i<numCoord; xOut[i*coordOffset]*=DEG_TO_RAD, yOut[i*coordOffset]*=DEG_TO_RAD, ++i);
223 int res = pj_transform(m_sourcePj4Handler, m_targetPj4Handler, numCoord, coordOffset, xOut, yOut, 0);
225 if (res==0 && pj_is_latlong(m_targetPj4Handler))
226 for (
long i=0; i<numCoord; xOut[i*coordOffset]*=RAD_TO_DEG,yOut[i*coordOffset]*=RAD_TO_DEG, ++i);
230 throw te::srs::Exception(
TR_SRS(
"PROJ4 library has to be enabled in order to support coordinate conversion."));
238 assert(m_sourcePj4Handler);
239 assert(m_targetPj4Handler);
241 if (pj_is_latlong(m_sourcePj4Handler))
242 for (
long i=0; i<numCoord; x[i*coordOffset]*=DEG_TO_RAD, y[i*coordOffset]*=DEG_TO_RAD, ++i);
244 int res = pj_transform(m_sourcePj4Handler, m_targetPj4Handler, numCoord, coordOffset, x, y, 0);
246 if (res==0 && pj_is_latlong(m_targetPj4Handler))
247 for (
long i=0; i<numCoord; x[i*coordOffset]*=RAD_TO_DEG,y[i*coordOffset]*=RAD_TO_DEG, ++i);
251 throw te::srs::Exception(
TR_SRS(
"PROJ4 library has to be enabled in order to support coordinate conversion."));
259 assert(m_sourcePj4Handler);
260 assert(m_targetPj4Handler);
265 if (pj_is_latlong(m_sourcePj4Handler))
271 int res = pj_transform(m_sourcePj4Handler, m_targetPj4Handler, 1, 1, &xOut, &yOut, 0);
273 if (pj_is_latlong(m_targetPj4Handler))
280 throw te::srs::Exception(
TR_SRS(
"PROJ4 library has to be enabled in order to support coordinate conversion."));
288 assert(m_sourcePj4Handler);
289 assert(m_targetPj4Handler);
291 if (pj_is_latlong(m_sourcePj4Handler))
297 int res = pj_transform(m_sourcePj4Handler, m_targetPj4Handler, 1, 1, &x, &y, 0);
299 if (pj_is_latlong(m_targetPj4Handler))
306 throw te::srs::Exception(
TR_SRS(
"PROJ4 library has to be enabled in order to support coordinate conversion."));
314 assert(m_sourcePj4Handler);
315 assert(m_targetPj4Handler);
317 memcpy(xOut, xIn, numCoord*
sizeof(
double));
318 memcpy(yOut, yIn, numCoord*
sizeof(
double));
320 if (pj_is_latlong(m_targetPj4Handler))
321 for (
long i=0; i<numCoord; xOut[i*coordOffset]*=DEG_TO_RAD, yOut[i*coordOffset]*=DEG_TO_RAD, ++i);
323 int res = pj_transform(m_targetPj4Handler, m_sourcePj4Handler, numCoord, coordOffset, xOut, yOut, 0);
325 if (res==0 && pj_is_latlong(m_sourcePj4Handler))
326 for (
long i=0; i<numCoord; xOut[i*coordOffset]*=RAD_TO_DEG, yOut[i*coordOffset]*=RAD_TO_DEG, ++i);
330 throw te::srs::Exception(
TR_SRS(
"PROJ4 library has to be enabled in order to support coordinate conversion."));
338 assert(m_sourcePj4Handler);
339 assert(m_targetPj4Handler);
341 if (pj_is_latlong(m_targetPj4Handler))
342 for (
long i=0; i<numCoord; x[i*coordOffset]*=DEG_TO_RAD, y[i]*=DEG_TO_RAD, ++i);
344 int res = pj_transform(m_targetPj4Handler, m_sourcePj4Handler, numCoord, coordOffset, x, y, 0);
346 if (res==0 && pj_is_latlong(m_sourcePj4Handler))
347 for (
long i=0; i<numCoord; x[i*coordOffset]*=RAD_TO_DEG, y[i]*=RAD_TO_DEG, ++i);
351 throw te::srs::Exception(
TR_SRS(
"PROJ4 library has to be enabled in order to support coordinate conversion."));
359 assert(m_sourcePj4Handler);
360 assert(m_targetPj4Handler);
365 if (pj_is_latlong(m_targetPj4Handler))
371 int res = pj_transform(m_targetPj4Handler, m_sourcePj4Handler, 1, 1, &xOut, &yOut, 0);
373 if (res==0 && pj_is_latlong(m_sourcePj4Handler))
381 throw te::srs::Exception(
TR_SRS(
"PROJ4 library has to be enabled in order to support coordinate conversion."));
389 assert(m_sourcePj4Handler);
390 assert(m_targetPj4Handler);
392 if (pj_is_latlong(m_targetPj4Handler))
398 int res = pj_transform(m_targetPj4Handler, m_sourcePj4Handler, 1, 1, &x, &y, 0);
400 if (res==0 && pj_is_latlong(m_sourcePj4Handler))
408 throw te::srs::Exception(
TR_SRS(
"PROJ4 library has to be enabled in order to support coordinates conversion."));
419 if (description.empty())
422 projPJ pjhProj = pj_init_plus(description.c_str());
425 std::string exceptionTxt =
TR_SRS(
"srs Source SRS description is not valid: ");
426 char* pjError = pj_strerrno(*(pj_get_errno_ref()));
427 exceptionTxt += std::string(pjError);
431 if (pj_is_latlong(pjhProj))
437 projPJ pjhGeog = pj_latlong_from_proj(pjhProj);
438 int res = pj_transform(pjhProj, pjhGeog, 1, 1, &x, &y, 0);
450 throw te::srs::Exception(
TR_SRS(
"PROJ4 library has to be enabled in order to support coordinate conversion."));
458 if (description.empty())
459 throw te::srs::Exception(
TR_SRS(
"Source SRS ID not recognized."));
461 projPJ pjhProj = pj_init_plus(description.c_str());
464 std::string exceptionTxt =
TR_SRS(
"Source SRS description is not valid: ");
465 char* pjError = pj_strerrno(*(pj_get_errno_ref()));
466 exceptionTxt += std::string(pjError);
469 projPJ pjhGeog = pj_latlong_from_proj(pjhProj);
473 int res = pj_transform(pjhGeog, pjhProj, 1, 1, &lon, &lat, 0);
480 throw te::srs::Exception(
TR_SRS(
"PROJ4 library has to be enabled in order to support coordinate conversion."));
Converter()
Default empty constructor.
An exception class for the SRS module.
void setTargetPJ4txt(const std::string &pj4txt)
Sets the target SRS PROJ4 description.
bool convertToGeographic(double &x, double &y, int SRID) const
Converts a coordinate from a projected SRS to its underlying geographic SRS (same Datum)...
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
void * m_targetPj4Handler
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.
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
A class to manage Coordinate Systems representations.
This file contains the support to convert coordinates from a SRS to another.
#define TR_SRS(message)
It marks a string in order to get translated. This is a special mark used in the SRS module of TerraL...
void setTargetSRID(int targetSRID)
Sets the target SRS identifier.
int getTargetSRID() const
Gets target SRS identifier.
void setSourceSRID(int sourceSRID)
Sets the source SRS identifier.
bool convertToProjected(double &lon, double &lat, int SRID) const
Converts a coordinate from a geographic SRS to a projected SRS based on the same Datum.
std::string getP4Txt(unsigned int id, const std::string &authName="EPSG") const
Returns a coordinate system PROJ4 description given an identification.
void setSourcePJ4txt(const std::string &pj4txt)
Sets the source SRS PROJ4 description.
static SpatialReferenceSystemManager & getInstance()
It returns a reference to the singleton instance.
int getSourceSRID() const
Gets source SRS identifier.
void * m_sourcePj4Handler
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.