James Hill
James Hill

Reputation: 61862

Convert Lat/Lon to Integer

A service that I'm consuming requires that I pass it the lat/lon for an address as integers. Currently, my lat/lon's are stored as doubles:

double lat = 38.898748;
double lon = -77.037684;

I've exhausted my Google-fu and can't find a method for converting lat/lon to an integer representation. Any help would be appreciated.

Upvotes: 11

Views: 17876

Answers (7)

Tim Makins
Tim Makins

Reputation: 492

double_lat = 38.898748;

double_lon = -77.037684;

A simple way to store decimal positions as integers:

lat = (double_lat + 90) * 1000000;

lon = (double_lon + 180) * 1000000;

lat = 128898748;
lon = 102962316;

Upvotes: 1

Derrick H
Derrick H

Reputation: 520

I am going to take a wild stab at this and guess that you are wanting to convert pure degrees to Degrees:Minutes:Seconds in the form of a single integer.

Here is one way you could convert this into an integer, but obviously you are going to want to confirm this is the format they are using.

double originalLat = 38.898748,
    TEMPdecimal;

int degrees = (int)originalLat;

TEMPdecimal = (originalLat - degrees) * 60;

int minutes = (int)TEMPdecimal;

TEMPdecimal = (TEMPdecimal - minutes) * 60;

int seconds = (int)TEMPdecimal;

int lat = (degrees * 10000) + (minutes * 100) + seconds;

In this case the return is 385355 or 38 degrees 53 minutes 55 seconds. Sorry if this looks a little sloppy and the type casting might not be the most efficient (definitely would need to make sure it rounds properly, i think type casting to int is rounding down all the time) but it will give you longitude/latitude.

Upvotes: 2

Jodrell
Jodrell

Reputation: 35726

The answer could be either,

int FactorConversion(double degrees)
{
    return (int) degrees * MagicConversionFactor 
    //You supply this number, a likely candidate is 360 since
    //this converts degrees to seconds 
}

or

 int BinaryConversion32(float degrees)
 {
     return BitConvetor.ToInt32(BitConvertor.GetBytes(degrees))
 }

or

long BinaryConversion64(double degrees)
{
    return BitConvetor.ToInt64(BitConvertor.GetBytes(degrees))
}

note that double is an 8 byte type. As previously stated by me and others, it all rather depends on what the API expects.

The second two options would be lossless but bizarre.

Upvotes: 1

Paul Sasik
Paul Sasik

Reputation: 81537

You need to know what Geodetic System you're using and what the API expects. E.g. WGS84, NAD83, OSGB36, ED50... (Example: Google Earth uses WGS84)

If they need an integer then they're probably using something different from Google and other providers. Chances are that rounding a a double or some other integer conversion will not work. You need the Geodetic System information and then make the conversion between the two values.

Upvotes: 10

Guffa
Guffa

Reputation: 700730

If you need the coordinates in seconds, just multiply by 60 * 60:

int latitude = (int)Math.Round(lat * 60.0 * 60.0);
int longitude = (int)Math.Round(lon * 60.0 * 60.0);

If you need the coodinates in higher resolution, for example 100th of a second, just multiply by 100 more.

Upvotes: 2

ChaosPandion
ChaosPandion

Reputation: 78282

The only way I can see this happening is if you shift the decimal point.

int lat = (int)(38.898748 * 1000000.0);
int lon = (int)(-77.037684 * 1000000.0);

Upvotes: 5

keithwarren7
keithwarren7

Reputation: 14278

Sometimes it is simple, just multiply by 1 million.

Multiply by .000001 to convert back.

Granted this assumes you only want precision to the 6th decimal.

Upvotes: 11

Related Questions