在 SQL Server 中存储坐标(经度/纬度,来自 Google 地图)的最佳方式是什么?
我正在 SQL Server 2008 中设计一个表,它将存储用户列表和 Google 地图坐标(经度和纬度)。
我需要两个字段,还是可以用 1 个字段来完成?
用于存储此类数据的最佳(或最常见)数据类型是什么?
I'm designing a table in SQL Server 2008 that will store a list of users and a Google Maps co-ordinate (longitude & latitude).
Will I need two fields, or can it be done with 1?
What's the best (or most common) data-type to use for storing this kind of data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
公平警告! 在接受使用 GEOGRAPHY 类型的建议之前,请确保您不打算使用 Linq 或实体框架来访问数据,因为它不受支持(截至 2010 年 11 月),并且您将伤心!
Fair Warning! Before taking the advice to use the GEOGRAPHY type, make sure you are not planning on using Linq or Entity Framework to access the data because it's not supported (as of November 2010) and you will be sad!
看一下 SQL Server 2008 中引入的新空间数据类型。它们是为此类任务而设计的,使索引和查询变得更加容易和高效。
更多信息:
MS TechNet:SQL Server 2008 空间数据类型,
MSDN:使用空间数据(数据库引擎)。
Take a look at the new Spatial data-types that were introduced in SQL Server 2008. They are designed for this kind of task and make indexing and querying much easier and more efficient.
More information:
MS TechNet: SQL Server 2008 Spatial Data Types,
MSDN: Working with Spatial Data (Database Engine).
我不知道 SQL Server 的答案,但是...
在 MySQL 中将其保存为
FLOAT( 10, 6 )
< /strong>这是 Google 开发者文档<的官方建议< /a>.
I don't know the answer for SQL Server but...
In MySQL save it as
FLOAT( 10, 6 )
This is the official recommendation from the Google developer documentation.
我这样做的方式:我存储纬度和经度,然后我有第三列,它是第一两列的自动派生地理类型。 该表如下所示:
这使您可以灵活地在 geoPoint 列上进行空间查询,并且您还可以根据需要检索纬度和经度值以进行显示或提取以用于 csv 目的。
The way I do it: I store the latitude and longitude and then I have a third column which is a automatic derived geography type of the 1st two columns. The table looks like this:
This gives you the flexibility of spatial queries on the geoPoint column and you can also retrieve the latitude and longitude values as you need them for display or extracting for csv purposes.
我讨厌成为那些说“这是一种新类型,让我们使用它”的人的逆向者。 新的 SQL Server 2008 空间类型有一些优点 - 即效率,但是您不能盲目地说始终使用该类型。 这实际上取决于一些更大的问题。
举个例子,整合。 这种类型在 .Net 中有一个等效的类型 - 但是互操作呢? 支持或扩展旧版本的 .Net 怎么样? 将这种类型跨服务层暴露给其他平台怎么样? 数据标准化怎么样——也许您对纬度或经度作为独立的信息感兴趣。 也许您已经编写了复杂的业务逻辑来处理经/纬度。
我并不是说您不应该使用空间类型 - 在很多情况下您应该使用。 我只是说在走这条路之前你应该问一些更关键的问题。 为了最准确地回答你的问题,我需要更多地了解你的具体情况。
单独存储长/纬度或以空间类型存储都是可行的解决方案,并且根据您自己的情况,其中一种可能优于另一种。
I hate to be a contrarian to those who said "here is a new type, let's use it". The new SQL Server 2008 spatial types have some pros to it - namely efficiency, however you can't blindly say always use that type. It really depends on some bigger picture issues.
As an example, integration. This type has an equivilent type in .Net - but what about interop? What about supporting or extending older versions of .Net? What about exposing this type across the service layer to other platforms? What about normalization of data - maybe you are interested in lat or long as standalone pieces of information. Perhaps you've already written complex business logic to handle long/lat.
I'm not saying that you shouldn't use the spatial type - in many cases you should. I'm just saying you should ask some more critical questions before going down that path. For me to answer your question most accurately I would need to know more about your specific situation.
Storing long/lat separately or in a spatial type are both viable solutions, and one may be preferable to the other depending on your own circumstances.
您想要做的是将纬度和经度存储为新的 SQL2008 空间类型 -> 地理。
这是我拥有的表格的屏幕截图。
替代文本 http://img20.imageshack.us/img20/6839/zipcodetable.png
在此表中,我们有两个存储地理数据的字段。
您想要将其作为 GEOGRAPHY 类型保存到数据库的主要原因是这样您就可以利用它的所有 SPATIAL 方法 -> 例如。 多边形中的点、两点之间的距离等。
顺便说一句,我们还使用 Google 的 Maps API 来检索纬度/经度数据并将其存储在我们的 Sql 2008 数据库中 - 所以这个方法确实有效。
What you want to do is store the Latitude and Longitude as the new SQL2008 Spatial type -> GEOGRAPHY.
Here's a screen shot of a table, which I have.
alt text http://img20.imageshack.us/img20/6839/zipcodetable.png
In this table, we have two fields that store geography data.
The main reason why you want to save it to the database as a GEOGRAPHY type is so you can then leverage all the SPATIAL methods off it -> eg. Point in Poly, Distance between two points, etc.
BTW, we also use Google's Maps API to retrieve lat/long data and store that in our Sql 2008 DB -- so this method does work.
SQL Server 支持空间相关信息。 您可以在 http://www.microsoft.com/ 查看更多信息sqlserver/2008/en/us/spatial-data.aspx。
或者,您可以将信息存储为两个基本字段,通常浮点数是大多数设备报告的标准数据类型,并且在一两英寸内足够精确 - 对于 Google 地图来说绰绰有余。
SQL Server has support for spatial related information. You can see more at http://www.microsoft.com/sqlserver/2008/en/us/spatial-data.aspx.
Alternativly you can store the information as two basic fields, usually a float is the standard data type reported by most devices and is accurate enough for within an inch or two - more than adequate for Google Maps.
来自 google Maps 的纬度和经度应在 SQL Server 中以地理数据类型下的点(注意大写 P)数据存储。
假设您当前的数据以 varchar 形式存储在表
Sample
中的lat
和lon
列下,下面的查询将帮助您转换为地理PS:下次当您使用地理数据对此表进行选择时,除了结果和消息选项卡之外,您还将获得如下所示的空间结果选项卡以进行可视化
latitute and longitude from google Maps should be stored as Point(note capital P) data in SQL server under geography data type.
Assuming your current data is stored in a table
Sample
as varchar under columnslat
andlon
, below query will help you convert to geographyPS: Next time when you do a select on this table with geography data, apart from Results and Messages tab, you will also get Spatial results tab like below for visualization
如果您使用的是 Entity Framework 5 < 您可以使用
DbGeography
。 MSDN 中的示例:https://msdn.microsoft .com/en-us/library/hh859721(v=vs.113).aspx
当我开始使用
DbGeography
时,我遇到的一个问题是coordinateSystemId
。 请参阅下面的答案,了解下面代码的精彩解释和来源。https://stackoverflow.com/a/25563269/3850405
If you are using Entity Framework 5 < you can use
DbGeography
. Example from MSDN:https://msdn.microsoft.com/en-us/library/hh859721(v=vs.113).aspx
Something I struggled with then I started using
DbGeography
was thecoordinateSystemId
. See the answer below for an excellent explanation and source for the code below.https://stackoverflow.com/a/25563269/3850405
如果你只是想把它替换成一个 URL,我想一个字段就可以了 - 所以你可以形成一个 URL,
但因为它是两条数据,所以我会将它们存储在单独的字段中
If you are just going to substitute it into a URL I suppose one field would do - so you can form a URL like
but as it is two pieces of data I would store them in separate fields
将两者存储为浮点数,并在它们上使用唯一的关键字。i.em
Store both as float, and use unique key words on them.i.em