Click here to Skip to main content
13,006,221 members (65,613 online)
Click here to Skip to main content
Add your own
alternative version


41 bookmarked
Posted 21 Sep 2006

Longitude-Latitude String Parser and Formatter

, 21 Sep 2006
Rate this:
Please Sign up or sign in to vote.
A class for longitude and latitude values so as to parse the string to render a double, or format a double into a string.

Sample Image - llstr.gif


CLongLatString is a class to be used to parse any one of the many forms of representation of longitude or latitude to a double, or format a double that represents a geographic co-ordinate to a nice-looking geographic co-ordinate string.


The Earth is divided up into a geographic grid lines of longitude and latitude. Longitude measures the angular distance east and west from Greenwich, England. Latitude measures the angular distance north and south from the Equator. Lines of equal longitude converge at the poles, while lines of equal latitude are parallel lines that circle the Earth. This is only one geographic coordinate system among many, but it is by far the most universally used and understood.

These co-ordinates are often expressed in terms of a hemisphere (N, S, E, and W), degrees, minutes, and seconds of arc. Some use decimal degrees, degree-minutes, or degree-minute-second combinations. Some replace the hemisphere with positive/negative signs (East is positive). On top of that, a wide number of delimiters and placements complicate the format, some leave out delimiters altogether, usually the last term is decimal while the previous are integers, and some run all the data together to save space, and the hemisphere term can be located anywhere in the co-ordinate.

Some examples of various geographic co-ordinate formats:

45:26:46N,     65:56:55W
45:26:46.302N, 65:56:55.903W
45°26’21"N,    65°58’36"W
45N26 21,      65W58 36
45.446195N,    65.948862W
45.446195,     -65.948862
N45° 26.7717’, W65° 56.93172’

Using the code

This class has a fairly powerful (I think) ability to automatically parse that data and extract the numeric value. This allows you, for example, to offer up one edit box for each geographic co-ordinate instead of a combo box for hemisphere and separate inputs for each of degree, minutes, and seconds.

CLongLatString is a class that deals with these various formats for longitude and latitude so that it can be easily handled and converted to double values for use with other geographic methods. The object maintains a double and a string for each value. The string is kept because sometimes there is a need to retain the original data, so as to maintain precision. You can output the string value from a double in the same way you can format CTime from a time_t using printf-like values, %C, %c, %H %h %D, %d, %M, %m, %S, and %s as the parameters.

CLongLatString does not deal with co-ordinates in pairs - it is more effective to have one object each with a value to indicate whether it is a longitude or latitude. So if you are initializing the data with a plain double, you must also indicate whether the value is longitude or latitude if you want to get any meaningful result out of it.

There are large number of constructors and a copy constructor. You can initialize it with a string or a double as needed. There are also conversion operators and assignment operators so you can easily convert to double or standard strings.

CLongLatString llstrLat1("45N54.2356");
CLongLatString llstrLat2("-45.98739874", LL_LATITUDE);
CLongLatString llstrLat5(-45.98739874, LL_LATITUDE);
CLongLatString llstrLat7("N", "45", "34", "33");
CLongLatString llstrLat8("N455698735", "HDDMMmmmmm");

If you are going to use the assignment operator with a double or a string that contains a text double, you must initialize that object to tell it whether it is a long or lat.

CLongLatString llstrLat(LL_LATITUDE);
llstrLat = -5.877789;
std::string str = llstrLat.Format("%Dd%M\’%S\"%H");
cout << str.c_str() << endl;
// Output -    5d52’40"S
llstrLat = -5.997789;   // Can be reused - still a latitude
str = llstrLat.Format("%Dd%M\’%S\"%H");
cout << str.c_str() << endl;

or a more complicated example:

std::string str = llstrLat.Format("%H%02D %.5m");
// Output -    S05 52.66734

Output Format

%H - hemisphere - single character of N,S,E,W
%C - integer co-ordinate, may be negative or positive
%c - decimal co-ordinate, the entire co-ordinate,
         may be negative or positive
%D - integer degrees, always positive
%M - integer degrees, always positive
%S - integer seconds, always positive, rounded
%d - decimal degrees, always positive
%m - decimal minutes, always positive
%s - decimal seconds, always positive
%% - for %

Standard printf formats may be used e.g. %03D to create three spaces with leading zeroes for integer degrees or %.5m to write decimal minutes out five places. The upper case identifiers use the same printf identifiers as for decimal (integer) formats, while the lower case identifiers use the floating point formats. All other flags will work as well as the entire substring is passed to the snprintf C function.

There are integer Get...() methods for each of the terms in integer and decimal form. If you need to extract string forms individually for each of the terms you can reuse the Format() method.

For example:

CLongLatString llstrLat(-5.9879875, LL_LATITUDE);
std::string strHemi = llstrLat.Format("%H");
std::string strDeg = llstrLat.Format("%02D");
std::string strMin = llstrLat.Format("%02M");
std::string strSec = llstrLat.Format("%05.2s");

Input Format

The input format is different and can only be used for cases of fixed length (e.g. "HDD MM.mmmmm" for "N45 04.25764") and is only used when the format is quite confusing and runs together. For most case in which the degrees, minute, seconds are separated by any whitespace or most delimiters, unspecified default input format should work. H indicates the location of the hemisphere, upper case character indicate the portion that is a digit to the left of the decimal place and lower case indicate digits that are to the right.

For example:

std::string strInputFormat = "HDDMMmmmmm";
CLongLatString llstrLat1("N650490807", strInputFormat);
CLongLatString llstrLat2;
llstrLat2.Set("N450490807", strInputFormat);
    // No input format needed if there are enough delimiters

One caveat - don't input exponential notation in a string - it will misinterpret the "E" as East and will mangle the result. Be sure to use atof() on the argument before passing it on to the constructor or Set method.

That covers it. You can take a look at the methods provided in the header file and it repeats some of the format specs there, the methods are provided with a one line comment explanation. Let me know if there are any scientific accuracy problems or serious bugs.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Jeff Bogan
Web Developer
Canada Canada
I shuffle tiny static charges on intricate wafers of silicon all day.

You may also be interested in...

Comments and Discussions

GeneralGreat Pin
sissi4630-Sep-10 6:33
membersissi4630-Sep-10 6:33 
GeneralLocale Pin
Jan Hrubý23-Sep-08 21:24
memberJan Hrubý23-Sep-08 21:24 
GeneralThanks a lot Pin
zhglzh24-Aug-08 21:59
memberzhglzh24-Aug-08 21:59 
GeneralHangs on pathological expressions Pin
Steffen Neumann4-Feb-07 4:50
memberSteffen Neumann4-Feb-07 4:50 
GeneralMemory leak Pin
Steffen Neumann3-Feb-07 22:38
memberSteffen Neumann3-Feb-07 22:38 
GeneralUnicode support Pin
Alain Rist22-Sep-06 23:51
memberAlain Rist22-Sep-06 23:51 
GeneralRe: Unicode support Pin
Jeff Bogan23-Sep-06 3:54
memberJeff Bogan23-Sep-06 3:54 
JokeRe: Unicode support Pin
Alain Rist23-Sep-06 4:40
memberAlain Rist23-Sep-06 4:40 
GeneralWierd Pin
Graham Shanks22-Sep-06 11:35
memberGraham Shanks22-Sep-06 11:35 
This is so wierd - I'd just finished writing a function to format latitude/longitude, set the project to compile and decided to check out CP and the first article I found was this one.

Now to rip out my lame code and put this in instead


My signature is not black, just a very, very dark blue

GeneralRe: Wierd Pin
Jeff Bogan23-Sep-06 3:59
memberJeff Bogan23-Sep-06 3:59 
GeneralRe: Wierd Pin
Graham Shanks23-Sep-06 10:13
memberGraham Shanks23-Sep-06 10:13 
GeneralCool... Pin
Paul Selormey21-Sep-06 21:18
memberPaul Selormey21-Sep-06 21:18 
GeneralRe: Cool... Pin
Jeff Bogan22-Sep-06 3:08
memberJeff Bogan22-Sep-06 3:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170628.1 | Last Updated 21 Sep 2006
Article Copyright 2006 by Jeff Bogan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid