简体   繁体   English

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

[英]How can I calculate the distance between 2 rows' points in a set of results?

I have a query that takes a LINESTRING and converts it to a result set of POINTS. 我有一个查询,它采用LINESTRING并将其转换为POINTS的结果集。

What I can't figure out is how to find the distance between 2 specific row points in this result set. 我无法弄清楚的是如何找到此结果集中2个特定行点之间的距离。

This is what I have so far: 这是我到目前为止:

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

在此输入图像描述

For example, how can I compare the distance between N=10 & N=11? 例如,如何比较N = 10和N = 11之间的距离?

This is what I was trying, but it does not work: 这是我正在尝试的,但它不起作用:

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]

Is this what you're looking for? 这是你在找什么? Distance to the previous point? 距前一点的距离?

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

在此输入图像描述

Replace 更换

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

With

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

And just change the values for @N1 & @N2 as neccessary 只需更改@ N1和@ N2的值即可

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

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