SQL Server 2008 GEOGRAPHY STDistance() 值

发布于 2024-09-11 19:24:43 字数 361 浏览 9 评论 0原文

我正在使用 geography.STDistance() 返回两个单点位置之间的距离。我很好奇返回值使用哪种测量?单位是公里、英里还是其他单位?

我得到的结果超过 250k,但我不知道我的 TSQL 是否做错了什么,因为这些是历史位置(即它们不再存在),所以我不能只进行快速查找。

declare @p1 geography

declare @p2 geography

SELECT @p1 = Location from tblLocations where Id = 1
SELECT @p2 = Location from tblLocations where Id = 2

select @p1.STDistance(@p2)

I am using geography.STDistance() to return the distance between two single point locations. I'm curious as to which measurement is used for the return value? Is it in KM's, miles or perhaps some other?

I'm getting results back upwards of 250k but i've no idea if im doing something wrong with my TSQL as these are historical locations(i.e. they no longer exist) so I can't just do a quick lookup.

declare @p1 geography

declare @p2 geography

SELECT @p1 = Location from tblLocations where Id = 1
SELECT @p2 = Location from tblLocations where Id = 2

select @p1.STDistance(@p2)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人如玉 2024-09-18 19:24:43

我认为返回测量取决于空间参考标识符 (SRID)
您的地理数据类型。默认值为 4326,单位为米。数据库中有一个表,您可以检查Select * from sys.spatial_reference_systems

I think the return measurement depends upon the Spatial Reference Identifiers (SRIDs)
of your geography data type. The default is 4326 which is in meters. There' a table in the DB you can check Select * from sys.spatial_reference_systems

抚笙 2024-09-18 19:24:43

只是为了覆盖那些在使用带有 GEOMETRY 类型的 STDistance 时寻找答案的人,结果“以与坐标值本身相同的测量单位表示”(来自“Beginning Spatial with SQL Server 2008”),对于 WGS84 / SRID 4326 数据以度为单位。

以下 SQL 应在 SQL Server 2008 R2 及更高版本上运行(爱丁堡韦弗利和伦敦查林十字车站 bing 地图的位置数据来源):

DECLARE @edinGeom GEOMETRY = GEOMETRY::STGeomFromText('POINT(-3.1917 55.9517)', 4326)
DECLARE @cxGeom GEOMETRY = GEOMETRY::STGeomFromText('POINT(-0.1252 51.5083)', 4326)
SELECT @edinGeom.STDistance(@cxGeom), sqrt(square(3.1917-0.1252) + square(55.9517-51.5083)) AS 'Distance from Pythagoras';

DECLARE @MetersPerMile FLOAT = 1609.344;
DECLARE @edinGeog GEOGRAPHY = GEOGRAPHY::STGeomFromText('POINT(-3.1917 55.9517)', 4326)
DECLARE @cxGeog GEOGRAPHY = GEOGRAPHY::STGeomFromText('POINT(-0.1252 51.5083)', 4326)
SELECT @edinGeog.STDistance(@cxGeog), @edinGeog.STDistance(@cxGeog)/@MetersPerMile;

使用 GEOMETRY 类型的前 3 行结果为:

STDistance Geom: 5.39881707506376,距毕达哥拉斯的距离: 5.39881707506376

GEOGRAPHY 类型的结果为:

STDistance Geog: 534226.761544321,转换为英里:< /strong> 331.953119745885

GEOGRAPHY 计算中的“331 英里”左右的换算结果与 Bing 地图上显示的两点之间的距离非常吻合(显然这不是任何证明,但它表明了类似的基础计算)。

GEOMETRY 计算输出的数字有望证明结果非常清楚地以度为单位,该值显然是使用毕达哥拉斯计算的(如果我们获取点和多边形之间的距离,底层计算会更复杂)。

Just to cover people arriving here looking for the answer when using STDistance with GEOMETRY types, the result is "expressed in the same unit of measurement as the coordinate values themselves' (from 'Beginning Spatial with SQL Server 2008') which for WGS84 / SRID 4326 data is in Degrees.

The following SQL should run on SQL Server 2008 R2 and above. (Source of location data for Edinburgh Waverley and London Charing Cross stations bing maps):

DECLARE @edinGeom GEOMETRY = GEOMETRY::STGeomFromText('POINT(-3.1917 55.9517)', 4326)
DECLARE @cxGeom GEOMETRY = GEOMETRY::STGeomFromText('POINT(-0.1252 51.5083)', 4326)
SELECT @edinGeom.STDistance(@cxGeom), sqrt(square(3.1917-0.1252) + square(55.9517-51.5083)) AS 'Distance from Pythagoras';

DECLARE @MetersPerMile FLOAT = 1609.344;
DECLARE @edinGeog GEOGRAPHY = GEOGRAPHY::STGeomFromText('POINT(-3.1917 55.9517)', 4326)
DECLARE @cxGeog GEOGRAPHY = GEOGRAPHY::STGeomFromText('POINT(-0.1252 51.5083)', 4326)
SELECT @edinGeog.STDistance(@cxGeog), @edinGeog.STDistance(@cxGeog)/@MetersPerMile;

The results for the first 3 lines using GEOMETRY types are:

STDistance Geom: 5.39881707506376, Distance From Pythagoras: 5.39881707506376

The results for the GEOGRAPHY types are:

STDistance Geog: 534226.761544321, Converted to Miles: 331.953119745885

The '331 miles' or so from the GEOGRAPHY calculation with conversion ties in nicely with that shown on Bing maps as the distance between two points (clearly this is not a proof of anything, but it suggests similar underlying calculations).

The numbers output by the GEOMETRY calculation hopefully demonstrate that the result is very clearly in degrees, with the value apparently being calculated using pythagoras (the underlying calculations would be more complex if we were getting the distance between points and polygons).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文