OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimMollweidProjection.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // Copyright (C) 2000 ImageLinks Inc.
3 //
4 // License: See top LICENSE.txt file.
5 //
6 // Author: Garrett Potts
7 //
8 // Description:
9 //
10 // Calls Geotrans Mollweid projection code.
11 //*******************************************************************
12 // $Id: ossimMollweidProjection.cpp 17815 2010-08-03 13:23:14Z dburken $
13 #include <math.h>
16 
17 RTTI_DEF1(ossimMollweidProjection, "ossimMollweidProjection", ossimMapProjection)
18 
19 /***************************************************************************/
20 /*
21  * DEFINES
22  */
23 #ifndef PI_OVER_2
24 # define PI_OVER_2 ( M_PI / 2.0)
25 #endif
26 #ifndef TWO_PI
27 # define TWO_PI (2.0 * M_PI)
28 #endif
29 #define MAX_LAT ( (M_PI * 90.0) / 180.0 ) /* 90 degrees in radians */
30 
31 #define MOLL_NO_ERROR 0x0000
32 #define MOLL_LAT_ERROR 0x0001
33 #define MOLL_LON_ERROR 0x0002
34 #define MOLL_EASTING_ERROR 0x0004
35 #define MOLL_NORTHING_ERROR 0x0008
36 #define MOLL_CENT_MER_ERROR 0x0020
37 #define MOLL_A_ERROR 0x0040
38 #define MOLL_B_ERROR 0x0080
39 #define MOLL_A_LESS_B_ERROR 0x0100
40 
42  const ossimGpt& origin)
43  :ossimMapProjection(ellipsoid, origin)
44 {
45  setDefaults();
46  update();
47 }
48 
50  const ossimGpt& origin,
51  double falseEasting,
52  double falseNorthing)
53  :ossimMapProjection(ellipsoid, origin)
54 {
55 
56  Moll_False_Easting = falseEasting;
57  Moll_False_Northing = falseNorthing;
58  Moll_Delta_Northing = 9009965.0;
59  Moll_Max_Easting = 18019930.0;
60  Moll_Min_Easting = -18019930.0;
61 
62  update();
63 }
64 
66 {
67 
70  theOrigin.lonr(),
73 
76 
78 }
79 
81 {
82  Moll_False_Easting = falseEasting;
83  update();
84 }
85 
87 {
88  Moll_False_Northing = falseNorthing;
89  update();
90 }
91 
93 {
94  Moll_False_Easting = 0.0;
95  Moll_False_Northing = 0.0;
96  Moll_Delta_Northing = 9009965.0;
97  Moll_Max_Easting = 18019930.0;
98  Moll_Min_Easting = -18019930.0;
99 }
100 
102  double falseNorthing)
103 {
104  Moll_False_Easting = falseEasting;
105  Moll_False_Northing = falseNorthing;
106 
107  update();
108 }
109 
111 {
112  Moll_Origin_Long = centralMeridian;
113  update();
114 }
115 
117 {
118  double lat = 0.0;
119  double lon = 0.0;
120 
121  Convert_Mollweide_To_Geodetic(eastingNorthing.x,
122  eastingNorthing.y,
123  &lat,
124  &lon);
125 
126  return ossimGpt(lat*DEG_PER_RAD, lon*DEG_PER_RAD, 0.0, theDatum);
127 }
128 
130 {
131  double easting = 0.0;
132  double northing = 0.0;
133  ossimGpt gpt = latLon;
134 
135  if (theDatum)
136  {
137  if (theDatum->code() != latLon.datum()->code())
138  {
139  gpt.changeDatum(theDatum); // Shift to our datum.
140  }
141  }
142 
144  gpt.lonr(),
145  &easting,
146  &northing);
147 
148  return ossimDpt(easting, northing);
149 }
150 
151 bool ossimMollweidProjection::saveState(ossimKeywordlist& kwl, const char* prefix) const
152 {
153  return ossimMapProjection::saveState(kwl, prefix);
154 }
155 
157  const char* prefix)
158 {
159  bool flag = ossimMapProjection::loadState(kwl, prefix);
160 
161  const char* type = kwl.find(prefix, ossimKeywordNames::TYPE_KW);
162 
163  setDefaults();
164 
166  {
169  }
170  update();
171 
172  return flag;
173 }
174 
175 /***************************************************************************/
176 /*
177  * FUNCTIONS
178  */
179 
180 
182  double f,
183  double Central_Meridian,
184  double False_Easting,
185  double False_Northing)
186 { /* Begin Set_Mollweide_Parameters */
187 /*
188  * The function Set_Mollweide_Parameters receives the ellipsoid parameters and
189  * Mollweide projcetion parameters as inputs, and sets the corresponding state
190  * variables. If any errors occur, the error code(s) are returned by the
191  * function, otherwise MOLL_NO_ERROR is returned.
192  *
193  * a : Semi-major axis of ellipsoid, in meters (input)
194  * f : Flattening of ellipsoid (input)
195  * Central_Meridian : Longitude in radians at the center of (input)
196  * the projection
197  * False_Easting : A coordinate value in meters assigned to the
198  * central meridian of the projection. (input)
199  * False_Northing : A coordinate value in meters assigned to the
200  * origin latitude of the projection (input)
201  */
202 
203  double Ra; /* Spherical Radius */
204 // double inv_f = 1 / f;
205  long Error_Code = MOLL_NO_ERROR;
206 
207 // if (a <= 0.0)
208 // { /* Semi-major axis must be greater than zero */
209 // Error_Code |= MOLL_A_ERROR;
210 // }
211 // if ((inv_f < 250) || (inv_f > 350))
212 // { /* Inverse flattening must be between 250 and 350 */
213 // Error_Code |= MOLL_INV_F_ERROR;
214 // }
215 // if ((Central_Meridian < -PI) || (Central_Meridian > TWO_PI))
216 // { /* origin longitude out of range */
217 // Error_Code |= MOLL_CENT_MER_ERROR;
218 // }
219  if (!Error_Code)
220  { /* no errors */
221  Moll_a = a;
222  Moll_f = f;
223  es2 = 2 * Moll_f - Moll_f * Moll_f;
224  es4 = es2 * es2;
225  es6 = es4 * es2;
226  /* spherical radius */
227  Ra = Moll_a * (1.0 - es2 / 6.0 - 17.0 * es4 / 360.0 - 67.0 * es6 / 3024.0);
228  Sqrt2_Ra = sqrt(2.0) * Ra;
229  Sqrt8_Ra = sqrt(8.0) * Ra;
230 // if (Central_Meridian > PI)
231 // Central_Meridian -= TWO_PI;
232  Moll_Origin_Long = Central_Meridian;
233  Moll_False_Easting = False_Easting;
234  Moll_False_Northing = False_Northing;
235 
236  if (Moll_Origin_Long > 0)
237  {
238  Moll_Max_Easting = 17919819.0;
239  Moll_Min_Easting = -18019930.0;
240  }
241  else if (Moll_Origin_Long < 0)
242  {
243  Moll_Max_Easting = 18019930.0;
244  Moll_Min_Easting = -17919819.0;
245  }
246  else
247  {
248  Moll_Max_Easting = 18019930.0;
249  Moll_Min_Easting = -18019930.0;
250  }
251 
252  } /* End if(!Error_Code) */
253  return (Error_Code);
254 } /* End Set_Mollweide_Parameters */
255 
256 
258  double *f,
259  double *Central_Meridian,
260  double *False_Easting,
261  double *False_Northing)const
262 { /* Begin Get_Mollweide_Parameters */
263 /*
264  * The function Get_Mollweide_Parameters returns the current ellipsoid
265  * parameters and Mollweide projection parameters.
266  *
267  * a : Semi-major axis of ellipsoid, in meters (output)
268  * f : Flattening of ellipsoid (output)
269  * Central_Meridian : Longitude in radians at the center of (output)
270  * the projection
271  * False_Easting : A coordinate value in meters assigned to the
272  * central meridian of the projection. (output)
273  * False_Northing : A coordinate value in meters assigned to the
274  * origin latitude of the projection (output)
275  */
276 
277  *a = Moll_a;
278  *f = Moll_f;
279  *Central_Meridian = Moll_Origin_Long;
280  *False_Easting = Moll_False_Easting;
281  *False_Northing = Moll_False_Northing;
282 
283  return;
284 } /* End Get_Mollweide_Parameters */
285 
286 
288  double Longitude,
289  double *Easting,
290  double *Northing)const
291 
292 { /* Begin Convert_Geodetic_To_Mollweide */
293 /*
294  * The function Convert_Geodetic_To_Mollweide converts geodetic (latitude and
295  * longitude) coordinates to Mollweide projection (easting and northing)
296  * coordinates, according to the current ellipsoid and Mollweide projection
297  * parameters. If any errors occur, the error code(s) are returned by the
298  * function, otherwise MOLL_NO_ERROR is returned.
299  *
300  * Latitude : Latitude (phi) in radians (input)
301  * Longitude : Longitude (lambda) in radians (input)
302  * Easting : Easting (X) in meters (output)
303  * Northing : Northing (Y) in meters (output)
304  */
305 
306  double PI_Sin_Latitude = M_PI * sin(Latitude);
307  double dlam; /* Longitude - Central Meridan */
308  double theta;
309  double theta_primed = Latitude;
310  double delta_theta_primed = 0.1745329; /* arbitrarily initialized to 10 deg */
311  double dtp_tolerance = 4.85e-10; /* approximately 1/1000th of
312  an arc second or 1/10th meter */
313  long Error_Code = MOLL_NO_ERROR;
314 
315 // if ((Latitude < -PI_OVER_2) || (Latitude > PI_OVER_2))
316 // { /* Latitude out of range */
317 // Error_Code |= MOLL_LAT_ERROR;
318 // }
319 // if ((Longitude < -PI) || (Longitude > TWO_PI))
320 // { /* Longitude out of range */
321 // Error_Code|= MOLL_LON_ERROR;
322 // }
323 
324  if (!Error_Code)
325  { /* no errors */
326  dlam = Longitude - Moll_Origin_Long;
327 // if (dlam > PI)
328 // {
329 // dlam -= TWO_PI;
330 // }
331 // if (dlam < -PI)
332 // {
333 // dlam += TWO_PI;
334 // }
335  while (fabs(delta_theta_primed) > dtp_tolerance)
336  {
337  delta_theta_primed = -(theta_primed + sin(theta_primed) -
338  PI_Sin_Latitude) / (1.0 + cos(theta_primed));
339  theta_primed += delta_theta_primed;
340  }
341  theta = theta_primed / 2.0;
342  *Easting = (Sqrt8_Ra / M_PI ) * dlam * cos(theta) +
344  *Northing = Sqrt2_Ra * sin(theta) + Moll_False_Northing;
345 
346  }
347  return (Error_Code);
348 
349 } /* End Convert_Geodetic_To_Mollweide */
350 
351 
353  double Northing,
354  double *Latitude,
355  double *Longitude)const
356 { /* Begin Convert_Mollweide_To_Geodetic */
357 /*
358  * The function Convert_Mollweide_To_Geodetic converts Mollweide projection
359  * (easting and northing) coordinates to geodetic (latitude and longitude)
360  * coordinates, according to the current ellipsoid and Mollweide projection
361  * coordinates. If any errors occur, the error code(s) are returned by the
362  * function, otherwise MOLL_NO_ERROR is returned.
363  *
364  * Easting : Easting (X) in meters (input)
365  * Northing : Northing (Y) in meters (input)
366  * Latitude : Latitude (phi) in radians (output)
367  * Longitude : Longitude (lambda) in radians (output)
368  */
369 
370  double dx, dy;
371  double theta = 0.0;
372  double two_theta;
373  double i;
374 
375  long Error_Code = MOLL_NO_ERROR;
376 
377 // if ((Easting < (Moll_False_Easting + Moll_Min_Easting))
378 // || (Easting > (Moll_False_Easting + Moll_Max_Easting)))
379 // { /* Easting out of range */
380 // Error_Code |= MOLL_EASTING_ERROR;
381 // }
382 // if ((Northing < (Moll_False_Northing - Moll_Delta_Northing)) ||
383 // (Northing >(Moll_False_Northing + Moll_Delta_Northing) ))
384 // { /* Northing out of range */
385 // Error_Code |= MOLL_NORTHING_ERROR;
386 // }
387 
388  if (!Error_Code)
389  {
390  dy = Northing - Moll_False_Northing;
391  dx = Easting - Moll_False_Easting;
392  i = dy / Sqrt2_Ra;
393  if (fabs(i) > 1.0)
394  {
395  *Latitude = MAX_LAT;
396  if (Northing < 0.0)
397  *Latitude *= -1.0;
398  }
399 
400  else
401  {
402  theta = asin(i);
403  two_theta = 2.0 * theta;
404  *Latitude = asin((two_theta + sin(two_theta)) / M_PI);
405 
406  if (*Latitude > PI_OVER_2) /* force distorted values to 90, -90 degrees */
407  *Latitude = PI_OVER_2;
408  else if (*Latitude < -PI_OVER_2)
409  *Latitude = -PI_OVER_2;
410 
411  }
412  if (fabs(fabs(*Latitude) - MAX_LAT) < 1.0e-10)
413  *Longitude = Moll_Origin_Long;
414  else
415  *Longitude = Moll_Origin_Long + M_PI * dx /
416  (Sqrt8_Ra * cos(theta));
417 
418 // if (*Longitude > PI)
419 // *Longitude -= TWO_PI;
420 // if (*Longitude < -PI)
421 // *Longitude += TWO_PI;
422 
423 // if (*Longitude > PI) /* force distorted values to 180, -180 degrees */
424 // *Longitude = PI;
425 // else if (*Longitude < -PI)
426 // *Longitude = -PI;
427 
428 
429  }
430  return (Error_Code);
431 
432 } /* End Convert_Mollweide_To_Geodetic */
433 
434 //*************************************************************************************************
436 //*************************************************************************************************
438 {
439  if (!ossimMapProjection::operator==(proj))
440  return false;
441 
442  const ossimMollweidProjection* p = dynamic_cast<const ossimMollweidProjection*>(&proj);
443  if (!p) return false;
444 
446 
447  return true;
448 }
#define PI_OVER_2
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
Method to the load (recreate) the state of an object from a keyword list.
void setCentralMeridian(double centralMeridian)
long Set_Mollweide_Parameters(double a, double f, double Central_Meridian, double False_Easting, double False_Northing)
#define MOLL_NO_ERROR
#define DEG_PER_RAD
Represents serializable keyword/value map.
const char * find(const char *key) const
bool almostEqual(T x, T y, T tolerance=FLT_EPSILON)
Definition: ossimCommon.h:53
void setFalseEastingNorthing(double falseEasting, double falseNorthing)
double y
Definition: ossimDpt.h:165
virtual const ossimString & code() const
Definition: ossimDatum.h:57
virtual bool operator==(const ossimProjection &projection) const
Returns TRUE if principal parameters are within epsilon tolerance.
void setFalseEasting(double falseEasting)
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
static const char * TYPE_KW
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
void changeDatum(const ossimDatum *datum)
This will actually perform a shift.
Definition: ossimGpt.cpp:316
long Convert_Geodetic_To_Mollweide(double Latitude, double Longitude, double *Easting, double *Northing) const
#define STATIC_TYPE_NAME(T)
Definition: ossimRtti.h:325
const ossimDatum * datum() const
datum().
Definition: ossimGpt.h:196
ossimMollweidProjection(const ossimEllipsoid &ellipsoid=ossimEllipsoid(), const ossimGpt &origin=ossimGpt())
long Convert_Mollweide_To_Geodetic(double Easting, double Northing, double *Latitude, double *Longitude) const
virtual ossimDpt forward(const ossimGpt &latLon) const
All map projections will convert the world coordinate to an easting northing (Meters).
#define M_PI
void setFalseNorthing(double falseNorthing)
const double & getA() const
double lonr() const
Returns the longitude in radian measure.
Definition: ossimGpt.h:76
#define MAX_LAT
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Method to save the state of an object to a keyword list.
void Get_Mollweide_Parameters(double *a, double *f, double *Central_Meridian, double *False_Easting, double *False_Northing) const
virtual ossimGpt inverse(const ossimDpt &eastingNorthing) const
Will take a point in meters and convert it to ground.
double x
Definition: ossimDpt.h:164
double latr() const
latr().
Definition: ossimGpt.h:66
const double & getFlattening() const
ossimEllipsoid theEllipsoid
This method verifies that the projection parameters match the current pcs code.
#define RTTI_DEF1(cls, name, b1)
Definition: ossimRtti.h:485
ossimDpt theFalseEastingNorthing
Hold the false easting northing.
const ossimDatum * theDatum
This is only set if we want to have built in datum shifting.