Double or decimal for latitude/longitude values in C#

C#Latitude LongitudeGeopoints

C# Problem Overview


What is the best data type to use when storing geopositional data in C#? I would use decimal for its exactness, but operations on decimal floating point numbers are slower then binary floating point numbers (double).

I read that most of the time you won't need any more than 6 or 7 digits of precision for latitude or longitude. Does the inexactness of doubles even matter then or can it be ignored?

C# Solutions


Solution 1 - C#

Go for double, there are several reasons.

  • Trigonometric functions are available only for double
  • Precision of double (range of 100 nanometers) is far beyond anything you'll ever require for Lat/Lon values
  • GeoCoordinate Class and third-Party modules (e.g. DotSpatial) also use double for coordinates

Solution 2 - C#

A double has up to 15 decimal digits of precision. So, lets assume three of those digits are going to be on the left of the decimal point for lat/long values (max of 180deg). This leaves 12 digits of precision on the right. Since a degree of lat/long is ~111km, 5 of those 12 digits would give us precision to the meter. 3 more digits would give us precision to the millimeter. The remaining 4 digits would get us precision to around 100 nanometers. Since double will win from the perspective of performance and memory, I see no reason to even consider using decimal.

Solution 3 - C#

I faced this question quite a while ago when i started with spacial programming. I read a book a while ago that led me to this.

//sql server has a really cool dll that deals with spacial data such like
//geography points and so on. 
//add this namespace
Using Microsoft.SqlServer.Types;

//SqlGeography.Point(dblLat, dblLon, srid)

var lat_lon_point = Microsoft.SqlServer.Types.SqlGeography.Point(lat, lon, 4326);

This is the best way when working in your application with spacial data. then to save the data use this in sql

CREATE TABLE myGeoTable
{
LatLonPoint GEOMETRY 
}

else, if you are using something else that isnt sql just convert the point to hexadecimal and store it. I know after a long time using spacial that this is the safest.

Solution 4 - C#

Double

Combining the answers, it is how Microsoft represents it itself in SqlGeography library

[get: Microsoft.SqlServer.Server.SqlMethod(IsDeterministic=true, IsPrecise=true)] public System.Data.SqlTypes.SqlDouble Lat { get; } Property Value SqlDouble A SqlDouble value that specifies the latitude.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKRTacView Question on Stackoverflow
Solution 1 - C#Wernfried DomscheitView Answer on Stackoverflow
Solution 2 - C#MarkPflugView Answer on Stackoverflow
Solution 3 - C#JonnyView Answer on Stackoverflow
Solution 4 - C#alex.peterView Answer on Stackoverflow