00001 /** 00002 * \file DMS.hpp 00003 * \brief Header for GeographicLib::DMS class 00004 * 00005 * Copyright (c) Charles Karney (2008-2011) <charles@karney.com> and licensed 00006 * under the MIT/X11 License. For more information, see 00007 * http://geographiclib.sourceforge.net/ 00008 **********************************************************************/ 00009 00010 #if !defined(GEOGRAPHICLIB_DMS_HPP) 00011 #define GEOGRAPHICLIB_DMS_HPP 1 00012 00013 #include <GeographicLib/Constants.hpp> 00014 #include <GeographicLib/Utility.hpp> 00015 00016 #if defined(_MSC_VER) 00017 // Squelch warnings about dll vs vector and constant conditional expressions 00018 # pragma warning (push) 00019 # pragma warning (disable: 4251 4127) 00020 #endif 00021 00022 namespace GeographicLib { 00023 00024 /** 00025 * \brief Convert between degrees and the %DMS representation 00026 * 00027 * Parse a string representing degree, minutes, and seconds and return the 00028 * angle in degrees and format an angle in degrees as degree, minutes, and 00029 * seconds. In addition, handle NANs and infinities on input and output. 00030 * 00031 * Example of use: 00032 * \include example-DMS.cpp 00033 **********************************************************************/ 00034 class GEOGRAPHICLIB_EXPORT DMS { 00035 private: 00036 typedef Math::real real; 00037 // Replace all occurrences of pat by c 00038 static void replace(std::string& s, const std::string& pat, char c) { 00039 std::string::size_type p = 0; 00040 while (true) { 00041 p = s.find(pat, p); 00042 if (p == std::string::npos) 00043 break; 00044 s.replace(p, pat.length(), 1, c); 00045 } 00046 } 00047 static const std::string hemispheres_; 00048 static const std::string signs_; 00049 static const std::string digits_; 00050 static const std::string dmsindicators_; 00051 static const std::string components_[3]; 00052 static Math::real NumMatch(const std::string& s); 00053 DMS(); // Disable constructor 00054 00055 public: 00056 00057 /** 00058 * Indicator for presence of hemisphere indicator (N/S/E/W) on latitudes 00059 * and longitudes. 00060 **********************************************************************/ 00061 enum flag { 00062 /** 00063 * No indicator present. 00064 * @hideinitializer 00065 **********************************************************************/ 00066 NONE = 0, 00067 /** 00068 * Latitude indicator (N/S) present. 00069 * @hideinitializer 00070 **********************************************************************/ 00071 LATITUDE = 1, 00072 /** 00073 * Longitude indicator (E/W) present. 00074 * @hideinitializer 00075 **********************************************************************/ 00076 LONGITUDE = 2, 00077 /** 00078 * Used in Encode to indicate output of an azimuth in [000, 360) with no 00079 * letter indicator. 00080 * @hideinitializer 00081 **********************************************************************/ 00082 AZIMUTH = 3, 00083 /** 00084 * Used in Encode to indicate output of a plain number. 00085 * @hideinitializer 00086 **********************************************************************/ 00087 NUMBER = 4, 00088 }; 00089 00090 /** 00091 * Indicator for trailing units on an angle. 00092 **********************************************************************/ 00093 enum component { 00094 /** 00095 * Trailing unit is degrees. 00096 * @hideinitializer 00097 **********************************************************************/ 00098 DEGREE = 0, 00099 /** 00100 * Trailing unit is arc minutes. 00101 * @hideinitializer 00102 **********************************************************************/ 00103 MINUTE = 1, 00104 /** 00105 * Trailing unit is arc seconds. 00106 * @hideinitializer 00107 **********************************************************************/ 00108 SECOND = 2, 00109 }; 00110 00111 /** 00112 * Convert a string in DMS to an angle. 00113 * 00114 * @param[in] dms string input. 00115 * @param[out] ind a DMS::flag value signaling the presence of a 00116 * hemisphere indicator. 00117 * @exception GeographicErr if \e dms is malformed (see below). 00118 * @return angle (degrees). 00119 * 00120 * Degrees, minutes, and seconds are indicated by the characters d, ' 00121 * (single quote), " (double quote), and these components may only be 00122 * given in this order. Any (but not all) components may be omitted and 00123 * other symbols (e.g., the ° symbol for degrees and the unicode 00124 * prime and double prime symbols for minutes and seconds) may be 00125 * substituted. The last component indicator may be omitted and is assumed 00126 * to be the next smallest unit (thus 33d10 is interpreted as 33d10'). The 00127 * final component may be a decimal fraction but the non-final components 00128 * must be integers. Instead of using d, ', and " to indicate 00129 * degrees, minutes, and seconds, : (colon) may be used to <i>separate</i> 00130 * these components (numbers must appear before and after each colon); thus 00131 * 50d30'10.3" may be written as 50:30:10.3, 5.5' may be written 00132 * 0:5.5, and so on. The integer parts of the minutes and seconds 00133 * components must be less than 60. A single leading sign is permitted. A 00134 * hemisphere designator (N, E, W, S) may be added to the beginning or end 00135 * of the string. The result is multiplied by the implied sign of the 00136 * hemisphere designator (negative for S and W). In addition \e ind is set 00137 * to DMS::LATITUDE if N or S is present, to DMS::LONGITUDE if E or W is 00138 * present, and to DMS::NONE otherwise. Throws an error on a malformed 00139 * string. No check is performed on the range of the result. Examples of 00140 * legal and illegal strings are 00141 * - <i>LEGAL</i> (all the entries on each line are equivalent) 00142 * - -20.51125, 20d30'40.5"S, -20°30'40.5, -20d30.675, 00143 * N-20d30'40.5", -20:30:40.5 00144 * - 4d0'9, 4d9", 4d9'', 4:0:9, 004:00:09, 4.0025, 4.0025d, 4d0.15, 00145 * 04:.15 00146 * - <i>ILLEGAL</i> (the exception thrown explains the problem) 00147 * - 4d5"4', 4::5, 4:5:, :4:5, 4d4.5'4", -N20.5, 1.8e2d, 4:60, 00148 * 4d-5' 00149 * 00150 * <b>NOTE:</b> At present, all the string handling in the C++ 00151 * implementation %GeographicLib is with 8-bit characters. The support for 00152 * unicode symbols for degrees, minutes, and seconds is therefore via the 00153 * <a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a> encoding. (The 00154 * JavaScript implementation of this class uses unicode natively, of 00155 * course.) 00156 * 00157 * Here is the list of Unicode symbols supported for degrees, minutes, 00158 * seconds: 00159 * - degrees: 00160 * - d, D lower and upper case letters 00161 * - U+00b0 degree symbol (°) 00162 * - U+00ba masculine ordinal indicator 00163 * - U+2070 superscript zero 00164 * - U+02da ring above 00165 * - minutes: 00166 * - ' apostrophe 00167 * - U+2032 prime (′) 00168 * - U+00b4 acute accent 00169 * - U+2019 right single quote (’) 00170 * - seconds: 00171 * - " quotation mark 00172 * - U+2033 double prime (″) 00173 * - U+201d right double quote (”) 00174 * - ' ' any two consecutive symbols for minutes 00175 * . 00176 * The codes with a leading zero byte, e.g., U+00b0, are accepted in their 00177 * UTF-8 coded form 0xc2 0xb0 and as a single byte 0xb0. 00178 **********************************************************************/ 00179 static Math::real Decode(const std::string& dms, flag& ind); 00180 00181 /** 00182 * Convert DMS to an angle. 00183 * 00184 * @param[in] d degrees. 00185 * @param[in] m arc minutes. 00186 * @param[in] s arc seconds. 00187 * @return angle (degrees) 00188 * 00189 * This does not propagate the sign on \e d to the other components, 00190 * so -3d20' would need to be represented as - DMS::Decode(3.0, 20.0) or 00191 * DMS::Decode(-3.0, -20.0). 00192 **********************************************************************/ 00193 static Math::real Decode(real d, real m = 0, real s = 0) 00194 { return d + (m + s / 60) / 60; } 00195 00196 /// \cond SKIP 00197 /** 00198 * <b>DEPRECATED</b> (use Utility::num, instead). 00199 * Convert a string to a real number. 00200 * 00201 * @param[in] str string input. 00202 * @exception GeographicErr if \e str is malformed. 00203 * @return decoded number. 00204 **********************************************************************/ 00205 static Math::real Decode(const std::string& str) 00206 { return Utility::num<real>(str); } 00207 00208 /** 00209 * <b>DEPRECATED</b> (use Utility::fract, instead). 00210 * Convert a string to a real number treating the case where the string is 00211 * a simple fraction. 00212 * 00213 * @param[in] str string input. 00214 * @exception GeographicErr if \e str is malformed. 00215 * @return decoded number. 00216 **********************************************************************/ 00217 static Math::real DecodeFraction(const std::string& str) 00218 { return Utility::fract<real>(str); } 00219 /// \endcond 00220 00221 /** 00222 * Convert a pair of strings to latitude and longitude. 00223 * 00224 * @param[in] dmsa first string. 00225 * @param[in] dmsb second string. 00226 * @param[out] lat latitude. 00227 * @param[out] lon longitude reduced to the range [−180°, 00228 * 180°). 00229 * @param[in] swaplatlong if true assume longitude is given before latitude 00230 * in the absence of hemisphere designators (default false). 00231 * @exception GeographicErr if \e dmsa or \e dmsb is malformed. 00232 * @exception GeographicErr if \e dmsa and \e dmsb are both interpreted as 00233 * latitudes. 00234 * @exception GeographicErr if \e dmsa and \e dmsb are both interpreted as 00235 * longitudes. 00236 * @exception GeographicErr if decoded latitude is not in [−90°, 00237 * 90°]. 00238 * @exception GeographicErr if decoded longitude is not in 00239 * [−540°, 540°). 00240 * 00241 * By default, the \e lat (resp., \e lon) is assigned to the results of 00242 * decoding \e dmsa (resp., \e dmsb). However this is overridden if either 00243 * \e dmsa or \e dmsb contain a latitude or longitude hemisphere designator 00244 * (N, S, E, W). If an exception is thrown, \e lat and \e lon are 00245 * unchanged. 00246 **********************************************************************/ 00247 static void DecodeLatLon(const std::string& dmsa, const std::string& dmsb, 00248 real& lat, real& lon, bool swaplatlong = false); 00249 00250 /** 00251 * Convert a string to an angle in degrees. 00252 * 00253 * @param[in] angstr input string. 00254 * @exception GeographicErr if \e angstr is malformed. 00255 * @exception GeographicErr if \e angstr includes a hemisphere designator. 00256 * @return angle (degrees) 00257 * 00258 * No hemisphere designator is allowed and no check is done on the range of 00259 * the result. 00260 **********************************************************************/ 00261 static Math::real DecodeAngle(const std::string& angstr); 00262 00263 /** 00264 * Convert a string to an azimuth in degrees. 00265 * 00266 * @param[in] azistr input string. 00267 * @exception GeographicErr if \e azistr is malformed. 00268 * @exception GeographicErr if \e azistr includes a N/S designator. 00269 * @exception GeographicErr if decoded azimuth is not in 00270 * [−540°, 540°). 00271 * @return azimuth (degrees) reduced to the range [−180°, 00272 * 180°). 00273 * 00274 * A hemisphere designator E/W can be used; the result is multiplied by 00275 * −1 if W is present. 00276 **********************************************************************/ 00277 static Math::real DecodeAzimuth(const std::string& azistr); 00278 00279 /** 00280 * Convert angle (in degrees) into a DMS string (using d, ', and "). 00281 * 00282 * @param[in] angle input angle (degrees) 00283 * @param[in] trailing DMS::component value indicating the trailing units 00284 * on the string and this is given as a decimal number if necessary. 00285 * @param[in] prec the number of digits after the decimal point for the 00286 * trailing component. 00287 * @param[in] ind DMS::flag value indicated additional formatting. 00288 * @param[in] dmssep if non-null, use as the DMS separator character 00289 * (instead of d, ', " delimiters). 00290 * @exception std::bad_alloc if memory for the string can't be allocated. 00291 * @return formatted string 00292 * 00293 * The interpretation of \e ind is as follows: 00294 * - ind == DMS::NONE, signed result no leading zeros on degrees except in 00295 * the units place, e.g., -8d03'. 00296 * - ind == DMS::LATITUDE, trailing N or S hemisphere designator, no sign, 00297 * pad degrees to 2 digits, e.g., 08d03'S. 00298 * - ind == DMS::LONGITUDE, trailing E or W hemisphere designator, no 00299 * sign, pad degrees to 3 digits, e.g., 008d03'W. 00300 * - ind == DMS::AZIMUTH, convert to the range [0, 360°), no 00301 * sign, pad degrees to 3 digits, , e.g., 351d57'. 00302 * . 00303 * The integer parts of the minutes and seconds components are always given 00304 * with 2 digits. 00305 **********************************************************************/ 00306 static std::string Encode(real angle, component trailing, unsigned prec, 00307 flag ind = NONE, char dmssep = char(0)); 00308 00309 /** 00310 * Convert angle into a DMS string (using d, ', and ") selecting the 00311 * trailing component based on the precision. 00312 * 00313 * @param[in] angle input angle (degrees) 00314 * @param[in] prec the precision relative to 1 degree. 00315 * @param[in] ind DMS::flag value indicated additional formatting. 00316 * @param[in] dmssep if non-null, use as the DMS separator character 00317 * (instead of d, ', " delimiters). 00318 * @exception std::bad_alloc if memory for the string can't be allocated. 00319 * @return formatted string 00320 * 00321 * \e prec indicates the precision relative to 1 degree, e.g., \e prec = 3 00322 * gives a result accurate to 0.1' and \e prec = 4 gives a result accurate 00323 * to 1". \e ind is interpreted as in DMS::Encode with the additional 00324 * facility that DMS::NUMBER represents \e angle as a number in fixed 00325 * format with precision \e prec. 00326 **********************************************************************/ 00327 static std::string Encode(real angle, unsigned prec, flag ind = NONE, 00328 char dmssep = char(0)) { 00329 return ind == NUMBER ? Utility::str(angle, int(prec)) : 00330 Encode(angle, 00331 prec < 2 ? DEGREE : (prec < 4 ? MINUTE : SECOND), 00332 prec < 2 ? prec : (prec < 4 ? prec - 2 : prec - 4), 00333 ind, dmssep); 00334 } 00335 00336 /** 00337 * Split angle into degrees and minutes 00338 * 00339 * @param[in] ang angle (degrees) 00340 * @param[out] d degrees (an integer returned as a real) 00341 * @param[out] m arc minutes. 00342 **********************************************************************/ 00343 static void Encode(real ang, real& d, real& m) { 00344 d = int(ang); m = 60 * (ang - d); 00345 } 00346 00347 /** 00348 * Split angle into degrees and minutes and seconds. 00349 * 00350 * @param[in] ang angle (degrees) 00351 * @param[out] d degrees (an integer returned as a real) 00352 * @param[out] m arc minutes (an integer returned as a real) 00353 * @param[out] s arc seconds. 00354 **********************************************************************/ 00355 static void Encode(real ang, real& d, real& m, real& s) { 00356 d = int(ang); ang = 60 * (ang - d); 00357 m = int(ang); s = 60 * (ang - m); 00358 } 00359 00360 }; 00361 00362 } // namespace GeographicLib 00363 00364 #if defined(_MSC_VER) 00365 # pragma warning (pop) 00366 #endif 00367 00368 #endif // GEOGRAPHICLIB_DMS_HPP