如何计算一组结果中2行点之间的距离?

时间:2013-10-30 18:53:42

标签: sql tsql sql-server-2008-r2 common-table-expression spatial

我有一个查询,它接受LINESTRING并将其转换为POINTS的结果集。

我无法弄清楚如何找到此结果集中2个特定行点之间的距离。

这是我到目前为止所做的:

DECLARE @GeographyToConvert geography
SET     @GeographyToConvert = geography::STGeomFromText('LINESTRING (26.6434033 -81.7097817, 26.6435367 -81.709785, 26.6435783 -81.7098033, 26.6436067 -81.709825, 26.6435883 -81.709875, 26.64356 -81.7100417, 26.6434417 -81.710125, 26.6433167 -81.7101467, 26.643195 -81.7101033, 26.6431533 -81.7099517, 26.643175 -81.7097867, 26.643165 -81.7097917, 26.6431633 -81.7097367, 26.6431583 -81.7097083)',4326);
WITH GeographyPoints(N, Point) AS  
( 
   SELECT 1,  @GeographyToConvert.STPointN(1)
   UNION ALL
   SELECT N + 1, @GeographyToConvert.STPointN(N + 1)
   FROM GeographyPoints GP
   WHERE N < @GeographyToConvert.STNumPoints() 
)

SELECT N,Point.STBuffer(0.25) as point, Point.STAsText() FROM GeographyPoints

enter image description here

例如,如何比较N = 10&amp;的距离? N = 11吗

这就是我的尝试,但它不起作用:

Declare @Point1 geography;
Declare @Point2 geography;

DECLARE @GeographyToConvert geography
--SET     @GeometryToConvert = (select top 1 geotrack from dbo.SYNCTESTING2 where geotrack is not null);
SET     @GeographyToConvert = geography::STGeomFromText('LINESTRING (26.6434033 -81.7097817, 26.6435367 -81.709785, 26.6435783 -81.7098033, 26.6436067 -81.709825, 26.6435883 -81.709875, 26.64356 -81.7100417, 26.6434417 -81.710125, 26.6433167 -81.7101467, 26.643195 -81.7101033, 26.6431533 -81.7099517, 26.643175 -81.7097867, 26.643165 -81.7097917, 26.6431633 -81.7097367, 26.6431583 -81.7097083)',4326);
WITH GeographyPoints(N, Point) AS  
( 
   SELECT 1,  @GeographyToConvert.STPointN(1)
   UNION ALL
   SELECT N + 1, @GeographyToConvert.STPointN(N + 1)
   FROM GeographyPoints GP
   WHERE N < @GeographyToConvert.STNumPoints() 
)


SELECT N,Point.STBuffer(0.25) as point, Point.STAsText() FROM GeographyPoints

select @Point1 = Point FROM GeometryPoints where N = 10;

select @Point2 = Point FROM GeometryPoints where N = 11

select @Point1.STDistance(@Point2) as [Distance in Meters]

2 个答案:

答案 0 :(得分:2)

这是你要找的吗?到上一点的距离?

DECLARE @GeographyToConvert geography
SET     @GeographyToConvert = geography::STGeomFromText('LINESTRING (26.6434033 -81.7097817, 26.6435367 -81.709785, 26.6435783 -81.7098033, 26.6436067 -81.709825, 26.6435883 -81.709875, 26.64356 -81.7100417, 26.6434417 -81.710125, 26.6433167 -81.7101467, 26.643195 -81.7101033, 26.6431533 -81.7099517, 26.643175 -81.7097867, 26.643165 -81.7097917, 26.6431633 -81.7097367, 26.6431583 -81.7097083)',4326);
WITH GeographyPoints(N, Point, PreviousPoint, DistanceFromPrevious) AS  
( 
   SELECT 1,  @GeographyToConvert.STPointN(1), CAST(NULL AS GEOGRAPHY), CAST(0 AS Float)
   UNION ALL
   SELECT N + 1, @GeographyToConvert.STPointN(N + 1)
            , @GeographyToConvert.STPointN(N)
            , @GeographyToConvert.STPointN(N).STDistance(@GeographyToConvert.STPointN(N + 1))
   FROM GeographyPoints GP
   WHERE N < @GeographyToConvert.STNumPoints() 
)

SELECT N,Point.STBuffer(0.25) as point, Point.STAsText(), PreviousPoint, DistanceFromPrevious FROM GeographyPoints

enter image description here

答案 1 :(得分:2)

替换

SELECT N,Point.STBuffer(0.25) as point, Point.STAsText() FROM GeographyPoints

SELECT * INTO #GeographyPoints FROM GeographyPoints

DECLARE @N1 INT = 10
DECLARE @N2 INT = 11

SELECT (SELECT Point FROM #GeographyPoints WHERE N=@N1).STDistance(
            (SELECT Point FROM #GeographyPoints WHERE N=@N2))

DROP TABLE #GeographyPoints

只需更改@ N1&amp;的值@ N2作为必要