Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2008 GEOGRAPHY STDistance() value

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) 
like image 517
Chris Avatar asked Jul 26 '10 14:07

Chris


People also ask

What is STDistance in SQL?

Applies to: SQL Server (all supported versions) Azure SQL Database Azure SQL Managed Instance. Returns the shortest distance between a point in a geography instance and a point in another geography instance. STDistance() returns the shortest LineString between two geography types.

What is geography data type in SQL?

The geography spatial data type, geography, is implemented as a . NET common language runtime (CLR) data type in SQL Server. This type represents data in a round-earth coordinate system. The SQL Server geography data type stores ellipsoidal (round-earth) data, such as GPS latitude and longitude coordinates.

What is spatial results in SQL Server?

The Spatial results window provides visual mapping tools for viewing spatial data. To view spatial results, your query results must include a spatial column with either geometry or geography data. The Spatial results window is only available if your results are returned to a grid in the Results window.

What is difference between geography and geometry in SQL Server?

Geometry : Stores data based on a flat (Euclidean) coordinate system. The data type is often used to store the X and Y coordinates that represent lines, points, and polygons in two-dimensional spaces. Geography : Stores data based on a round-earth coordinate system.


2 Answers

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

like image 66
SPE109 Avatar answered Sep 21 '22 21:09

SPE109


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).

like image 44
Nij Avatar answered Sep 19 '22 21:09

Nij