简体   繁体   中英

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.

What I can't figure out is how to find the distance between 2 specific row points in this result set.

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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