Isis 3 Programmer Reference
Mollweide.cpp
Go to the documentation of this file.
1 
23 #include "Mollweide.h"
24 
25 
26 #include <cfloat>
27 #include <cmath>
28 #include <iomanip>
29 
30 #include "Constants.h"
31 #include "IException.h"
32 #include "TProjection.h"
33 #include "Pvl.h"
34 #include "PvlGroup.h"
35 #include "PvlKeyword.h"
36 
37 using namespace std;
38 
39 
40 namespace Isis {
41 
42 
60  Mollweide::Mollweide(Pvl &label, bool allowDefaults) :
61  TProjection::TProjection(label) {
62  try {
63  // Try to read the mapping group
64  PvlGroup &mapGroup = label.findGroup("Mapping", Pvl::Traverse);
65 
66  // Compute and write the default center longitude if allowed and
67  // necessary
68  if ((allowDefaults) && (!mapGroup.hasKeyword("CenterLongitude"))) {
69  double lon = (m_minimumLongitude + m_maximumLongitude) / 2.0;
70  mapGroup += PvlKeyword("CenterLongitude", toString(lon));
71  }
72 
73  // Get the center longitude
74  m_centerLongitude = mapGroup["CenterLongitude"];
75 
76  // convert to radians, adjust for longitude direction
77  m_centerLongitude *= PI / 180.0;
79  }
80  catch(IException &e) {
81  QString message = "Invalid label group [Mapping]";
82  throw IException(e, IException::Io, message, _FILEINFO_);
83  }
84  }
85 
88  }
89 
90 
99  bool Mollweide::operator== (const Projection &proj) {
100  if (!TProjection::operator==(proj)) return false;
101  // dont do the below it is a recursive plunge
102  // if (TProjection::operator!=(proj)) return false;
103  Mollweide *moll = (Mollweide *) &proj;
104  if (moll->m_centerLongitude != m_centerLongitude) return false;
105  return true;
106  }
107 
108 
114  QString Mollweide::Name() const {
115  return "Mollweide";
116  }
117 
118 
123  QString Mollweide::Version() const {
124  return "1.0";
125  }
126 
127 
147  bool Mollweide::newton_rapheson(double phi, double &result) {
148 
149  double dtheta = 1.0;
150  int niter = 0;
151  double theta[2];
152 
153  theta[0] = asin(2*phi/PI);
154  theta[1]=0.0;
155 
156  //If this condition is too strict, a larger epsilon value than DBL_EPSILON
157  //can be used to decrease the number of iterations.
158  while (dtheta > DBL_EPSILON) {
159 
160  theta[1] = theta[0] - (2*theta[0]+sin(2*theta[0]) -(Isis::PI)*sin(phi))/(2+2*cos(theta[0]));
161  dtheta = fabs(theta[1]-theta[0]);
162  theta[0] = theta[1];
163  niter++;
164 
165  if (niter > 15000000) {
166  //cout << setprecision(10) << phi*(180/PI) << "," << niter << endl;
167  return false;
168  }
169  }
170  result = theta[1];
171 
172 
173  //cout << setprecision(10) << phi*(180/PI) << "," << niter << endl;
174  return true;
175  }
176 
177 
192  bool Mollweide::SetGround(const double lat, const double lon) {
193 
194  // Convert to radians
195  m_latitude = lat;
196  m_longitude = lon;
197  double theta;
198  double latRadians = lat * PI / 180.0;
199  double lonRadians = lon * PI / 180.0;
200  if (m_longitudeDirection == PositiveWest) lonRadians *= -1.0;
201 
202  // Compute the coordinate
203  double deltaLon = lonRadians - m_centerLongitude;
204 
205  if (newton_rapheson(latRadians,theta) ) {
206 
207  double x = (2*sqrt(2)/PI )*m_equatorialRadius*(deltaLon)*cos(theta);
208  double y = sqrt(2)*m_equatorialRadius*sin(theta);
209 
210  SetComputedXY(x, y);
211  m_good = true;
212  return m_good;
213  }
214  else {
215 
216  m_good = false;
217  return m_good;
218  }
219  }
220 
221 
235  bool Mollweide::SetCoordinate(const double x, const double y) {
236  // Save the coordinate
237 
238  SetXY(x, y);
239 
240  double theta = asin(y/(m_equatorialRadius*sqrt(2)));
241 
242  // Compute latitude and make sure it is not above 90
243  m_latitude = asin((2*theta+sin(2*theta))/(Isis::PI));
244 
245  if (fabs(m_latitude) > HALFPI) {
246  if (fabs(HALFPI - fabs(m_latitude)) > DBL_EPSILON) {
247  m_good = false;
248  return m_good;
249  }
250  else if (m_latitude < 0.0) {
251  m_latitude = -HALFPI;
252  }
253  else {
254  m_latitude = HALFPI;
255  }
256  }
257 
258  // Compute longitude
259 
260  double cosLat = cos(m_latitude);
261 
262  if (cosLat <= DBL_EPSILON) {
264  }
265 
266  else {
267  m_longitude = m_centerLongitude+(Isis::PI)*GetX()/(2*m_equatorialRadius*sqrt(2)*cos(theta));
268  }
269 
270 
271  // Convert to degrees
272  m_latitude *= 180.0 / PI;
273  m_longitude *= 180.0 / PI;
274 
275  // Cleanup the longitude
277 
278 
279  // Our double precision is not good once we pass a certain magnitude of
280  // longitude. Prevent failures down the road by failing now.
281  m_good = (fabs(m_longitude) < 1E10);
282 
283 
284  return m_good;
285  }
286 
287 
313  bool Mollweide::XYRange(double &minX, double &maxX,
314  double &minY, double &maxY) {
315  // Check the corners of the lat/lon range
320 
321  // If the latitude crosses the equator check there
322  if ((m_minimumLatitude < 0.0) && (m_maximumLatitude > 0.0)) {
325  }
326 
327  // Make sure everything is ordered
328  if (m_minimumX >= m_maximumX) return false;
329  if (m_minimumY >= m_maximumY) return false;
330 
331  // Return X/Y min/maxs
332  minX = m_minimumX;
333  maxX = m_maximumX;
334  minY = m_minimumY;
335  maxY = m_maximumY;
336  return true;
337  }
338 
339 
346  PvlGroup mapping = TProjection::Mapping();
347 
348  mapping += m_mappingGrp["CenterLongitude"];
349 
350  return mapping;
351  }
352 
353 
361 
362  return mapping;
363  }
364 
365 
373 
374  mapping += m_mappingGrp["CenterLongitude"];
375 
376  return mapping;
377  }
378 
379 } // end namespace isis
380 
381 
395  bool allowDefaults) {
396  return new Isis::Mollweide(lab, allowDefaults);
397 }
bool hasKeyword(const QString &name) const
Check to see if a keyword exists.
bool operator==(const Projection &proj)
Compares two Projection objects to see if they are equal.
Definition: Mollweide.cpp:99
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition: PvlObject.h:141
bool newton_rapheson(double gamma, double &result)
The Newton-Rapheson method is used to find an iterative solution for:
Definition: Mollweide.cpp:147
const double PI
The mathematical constant PI.
Definition: Constants.h:56
Longitude values increase in the westerly direction.
Definition: TProjection.h:241
Base class for Map TProjections.
Definition: TProjection.h:182
const double HALFPI
The mathematical constant PI/2.
Definition: Constants.h:57
Namespace for the standard library.
bool XYRange(double &minX, double &maxX, double &minY, double &maxY)
Find x/y range from lat/lon range.
Definition: Mollweide.cpp:313
Search child objects.
Definition: PvlObject.h:170
double GetX() const
Calculates the unrotated form of current x value.
Definition: Projection.cpp:833
double m_minimumX
The data elements m_minimumX, m_minimumY, m_maximumX, and m_maximumY are convience data elements when...
Definition: Projection.h:333
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:226
A type of error that occurred when performing an actual I/O operation.
Definition: IException.h:171
PvlGroup Mapping()
This function returns the keywords that this projection uses.
Definition: Mollweide.cpp:345
PvlGroup MappingLongitudes()
This function returns the longitude keywords that this projection uses.
Definition: Mollweide.cpp:371
void SetComputedXY(double x, double y)
This protected method is a helper for derived classes.
Definition: Projection.cpp:795
double m_latitude
This contains the currently set latitude value.
Definition: TProjection.h:332
virtual PvlGroup MappingLatitudes()
This function returns the latitude keywords that this projection uses.
double m_maximumY
See minimumX description.
Definition: Projection.h:344
double m_longitude
This contains the currently set longitude value.
Definition: TProjection.h:334
bool SetGround(const double lat, const double lon)
Set lat/lon and attempt to calculate x/y values.
Definition: Mollweide.cpp:192
QString Name() const
Returns the name of the map projection, "Mollweide".
Definition: Mollweide.cpp:114
double m_maximumLongitude
Contains the maximum longitude for the entire ground range.
Definition: TProjection.h:376
double m_minimumLatitude
Contains the minimum latitude for the entire ground range.
Definition: TProjection.h:370
double m_maximumLatitude
Contains the maximum latitude for the entire ground range.
Definition: TProjection.h:372
QString Version() const
Returns the version of the map projection.
Definition: Mollweide.cpp:123
Base class for Map Projections.
Definition: Projection.h:171
double m_minimumY
See minimumX description.
Definition: Projection.h:343
double m_equatorialRadius
Polar radius of the target.
Definition: TProjection.h:351
Contains multiple PvlContainers.
Definition: PvlGroup.h:57
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
~Mollweide()
Destroys the Mollweide object.
Definition: Mollweide.cpp:87
A single keyword-value pair.
Definition: PvlKeyword.h:98
Mollweide Map Projection.
Definition: Mollweide.h:66
PvlGroup MappingLatitudes()
This function returns the latitude keywords that this projection uses.
Definition: Mollweide.cpp:359
Isis::TProjection * MollweidePlugin(Isis::Pvl &lab, bool allowDefaults)
This is the function that is called in order to instantiate a Mollweide object.
Definition: Mollweide.cpp:394
Container for cube-like labels.
Definition: Pvl.h:135
virtual PvlGroup Mapping()
This function returns the keywords that this projection uses.
bool m_good
Indicates if the contents of m_x, m_y, m_latitude, and m_longitude are valid.
Definition: Projection.h:316
LongitudeDirection m_longitudeDirection
An enumerated type indicating the LongitudeDirection read from the labels.
Definition: TProjection.h:340
bool SetCoordinate(const double x, const double y)
This method is used to set the projection x/y.
Definition: Mollweide.cpp:235
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
virtual PvlGroup MappingLongitudes()
This function returns the longitude keywords that this projection uses.
void SetXY(double x, double y)
This protected method is a helper for derived classes.
Definition: Projection.cpp:819
void XYRangeCheck(const double latitude, const double longitude)
This convience function is established to assist in the development of the XYRange virtual method...
double m_minimumLongitude
Contains the minimum longitude for the entire ground range.
Definition: TProjection.h:374
double m_maximumX
See minimumX description.
Definition: Projection.h:342
PvlGroup m_mappingGrp
Mapping group that created this projection.
Definition: Projection.h:345
double m_centerLongitude
The center longitude for the map projection.
Definition: Mollweide.h:85