osgEarth 2.1.1
Public Types | Public Member Functions | Protected Attributes

osgEarth::Util::LatLongFormatter Class Reference

List of all members.

Public Types

enum  AngularFormat { FORMAT_DEFAULT, FORMAT_DECIMAL_DEGREES, FORMAT_DEGREES_DECIMAL_MINUTES, FORMAT_DEGREES_MINUTES_SECONDS }
enum  Options { USE_SYMBOLS = 1 << 0, USE_COLONS = 1 << 1, USE_SPACES = 1 << 2 }

Public Member Functions

 LatLongFormatter (const AngularFormat &defaultFormat=FORMAT_DEGREES_MINUTES_SECONDS, unsigned optionsMask=(USE_SYMBOLS|USE_SPACES))
void setPrecision (int value)
void setOptions (const Options &options)
std::string format (const Angular &angle, int precision=-1, const AngularFormat &format=FORMAT_DEFAULT)
bool parseAngle (const std::string &input, Angular &out_value)

Protected Attributes

unsigned _options
AngularFormat _defaultFormat
int _prec

Detailed Description

Definition at line 30 of file Formatters.


Member Enumeration Documentation

Enumerator:
FORMAT_DEFAULT 
FORMAT_DECIMAL_DEGREES 
FORMAT_DEGREES_DECIMAL_MINUTES 
FORMAT_DEGREES_MINUTES_SECONDS 

Definition at line 33 of file Formatters.

Enumerator:
USE_SYMBOLS 
USE_COLONS 
USE_SPACES 

Definition at line 40 of file Formatters.

                     {
            USE_SYMBOLS     = 1 << 0,   // whether to use symbols
            USE_COLONS      = 1 << 1,   // whether to separate components with colons
            USE_SPACES      = 1 << 2    // whether to separate components with spaces
        };

Constructor & Destructor Documentation

LatLongFormatter::LatLongFormatter ( const AngularFormat defaultFormat = FORMAT_DEGREES_MINUTES_SECONDS,
unsigned  optionsMask = (USE_SYMBOLS | USE_SPACES) 
)

Definition at line 30 of file Formatters.cpp.

                                                                  :
_defaultFormat( defaultFormat ),
_options      ( options ),
_prec         ( 5 )
{
    if ( _defaultFormat == FORMAT_DEFAULT )
    {
        _defaultFormat = FORMAT_DEGREES_MINUTES_SECONDS;
    }
}

Member Function Documentation

std::string LatLongFormatter::format ( const Angular angle,
int  precision = -1,
const AngularFormat format = FORMAT_DEFAULT 
)

Formats an angle into one of the supported angular formats

Definition at line 43 of file Formatters.cpp.

{
    std::stringstream buf;
    std::string result;
    std::string space = _options & USE_SPACES ? " " : "";

    AngularFormat f =
        format == FORMAT_DEFAULT ? _defaultFormat :
        format;

    if ( precision < 0 )
        precision = _prec;

    if ( precision > 0 )
        buf << std::setprecision(precision);

    switch( f )
    {
    case FORMAT_DECIMAL_DEGREES:
        {
            if ( _options & USE_SYMBOLS )
                buf << angle.as(Units::DEGREES) << "\xb0";
            else
                buf << angle.as(Units::DEGREES);
        }
        break;

    case FORMAT_DEGREES_DECIMAL_MINUTES:
        {
            double df = angle.as(Units::DEGREES);
            int    d  = (int)floor(df);
            double mf = 60.0*(df-(double)d);
            if ( mf == 60.0 ) {
                d += 1;
                mf = 0.0;
            }
            if ( _options & USE_SYMBOLS )
                buf << d << "\xb0" << space << mf << "'";
            else if ( _options & USE_COLONS )
                buf << d << ":" << mf;
            else
                buf << d << " " << mf;
        }
        break;

    case FORMAT_DEGREES_MINUTES_SECONDS:
        {
            double df = angle.as(Units::DEGREES);
            int    d  = (int)floor(df);
            double mf = 60.0*(df-(double)d);
            int    m  = (int)floor(mf);
            double sf = 60.0*(mf-(double)m);
            if ( sf == 60.0 ) {
                m += 1;
                sf = 0.0;
                if ( m == 60 ) {
                    d += 1;
                    m = 0;
                }
            }
            if ( _options & USE_SYMBOLS )
                buf << d << "\xb0" << space << m << "'" << space << sf << "\"";
            else if ( _options & USE_COLONS )
                buf << d << ":" << m << ":" << sf;
            else
                buf << d << " " << m << " " << sf;
        }
        break;
    }

    result = buf.str();
    return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool LatLongFormatter::parseAngle ( const std::string &  input,
Angular out_value 
)

Parses a string into an angle (returning false if parsing fails).

Definition at line 118 of file Formatters.cpp.

{
    const char* c = input.c_str();

    double d=0.0, m=0.0, s=0.0;

    if (sscanf(c, "%lf:%lf:%lf",     &d, &m, &s) == 3 ||
        sscanf(c, "%lf\xb0%lf'%lf\"",   &d, &m, &s) == 3 ||
        sscanf(c, "%lf\xb0 %lf' %lf\"", &d, &m ,&s) == 3 ||
        sscanf(c, "%lfd %lf' %lf\"", &d, &m, &s) == 3 ||
        sscanf(c, "%lfd %lfm %lfs",  &d, &m, &s) == 3 ||
        sscanf(c, "%lf %lf' %lf\"",  &d, &m, &s) == 3 )
    {
        out_value.set( d + m/60.0 + s/3600.0, Units::DEGREES );
        return true;
    }
    else if (
        sscanf(c, "%lf:%lf",   &d, &m) == 2 ||
        sscanf(c, "%lf\xb0 %lf'", &d, &m) == 2 ||
        sscanf(c, "%lf\xb0%lf'",  &d, &m) == 2 ||
        sscanf(c, "%lfd %lf'", &d, &m) == 2 ||
        sscanf(c, "%lfd %lfm", &d, &m) == 2 ||
        sscanf(c, "%lfd%lf'",  &d, &m) == 2 ||
        sscanf(c, "%lf %lf'",  &d, &m) == 2 )
    {
        out_value.set( d + m/60.0, Units::DEGREES );
        return true;
    }
    else if (
        sscanf(c, "%lf\xb0", &d) == 1 ||
        sscanf(c, "%lfd", &d) == 1 ||
        sscanf(c, "%lf",  &d) == 1 )
    {
        out_value.set( d, Units::DEGREES );
        return true;
    }

    return false;
}

Here is the call graph for this function:

void osgEarth::Util::LatLongFormatter::setOptions ( const Options options) [inline]

Sets the formatting options

Definition at line 59 of file Formatters.

{ _options = options; }
void osgEarth::Util::LatLongFormatter::setPrecision ( int  value) [inline]

Sets the output precision for decimal numbers (default is 5)

Definition at line 54 of file Formatters.

{ _prec = value; }

Member Data Documentation

Definition at line 76 of file Formatters.

Definition at line 75 of file Formatters.

Definition at line 77 of file Formatters.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines