简体   繁体   English

C# 中纬度/经度值的双精度或小数

[英]Double or decimal for latitude/longitude values in C#

What is the best data type to use when storing geopositional data in C#?在 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.我读到大多数时候,纬度或经度的精度不需要超过 6 或 7 位。 Does the inexactness of doubles even matter then or can it be ignored?那么双打的不精确性是否重要还是可以忽略不计?

Go for double , there are several reasons.选择double ,有几个原因。

  • 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 double 的精度(范围为 100 纳米)远远超出您对 Lat/Lon 值的任何要求
  • GeoCoordinate Class and third-Party modules (eg DotSpatial ) also use double for coordinates GeoCoordinate Class和第三方模块(例如DotSpatial )也使用 double 作为坐标

A double has up to 15 decimal digits of precision. double 具有最多 15 位十进制数字的精度。 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).因此,让我们假设其中三个数字将位于纬度/经度值(最大 180 度)的小数点左侧。 This leaves 12 digits of precision on the right.这在右边留下了 12 位精度。 Since a degree of lat/long is ~111km, 5 of those 12 digits would give us precision to the meter.由于纬度/经度的度数约为 111 公里,因此这 12 位数字中的 5 位将为我们提供米的精度。 3 more digits would give us precision to the millimeter.多 3 位数字将使我们精确到毫米。 The remaining 4 digits would get us precision to around 100 nanometers.剩下的 4 位数字将使我们的精度达到 100 纳米左右。 Since double will win from the perspective of performance and memory, I see no reason to even consider using decimal.由于从性能和内存的角度来看 double 会获胜,我认为没有理由甚至考虑使用小数。

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) //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然后保存数据在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.否则,如果您使用的不是 sql 的其他东西,只需将点转换为十六进制并存储它。 I know after a long time using spacial that this is the safest.长时间使用空间后,我知道这是最安全的。

Double双倍的

Combining the answers, it is how Microsoft represents it itself in SqlGeography library结合答案,这就是微软在 SqlGeography 库中的表示方式

[get: Microsoft.SqlServer.Server.SqlMethod(IsDeterministic=true, IsPrecise=true)] public System.Data.SqlTypes.SqlDouble Lat { get; [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. } 属性值SqlDouble指定纬度的 SqlDouble 值。

If you are using .net ef core, I would recommend you the NetTopologySuite library.如果您使用的是 .net ef core,我会向您推荐 NetTopologySuite 库。 Read the full documentation at below link: https://docs.microsoft.com/en-us/ef/core/modeling/spatial阅读以下链接的完整文档: https ://docs.microsoft.com/en-us/ef/core/modeling/spatial

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM