正在寻找:
背景:
INSERT INTO routes (route_id, route_name, route_geography)
VALUES (
8,
'route-test-8',
geography::STGeomFromText('LINESTRING(50.47959 -80.518649, 50.47923 -80.519601, ......)', 4326)
);
注意:出于测试目的,该随机点是其中一条路线中存在的点。所以理论上我至少应该得到 1 条路线的真实结果
尝试了以下查询:
-- Declare the "random" point
DECLARE @startPoint geography;
SET @startPoint = geography::Point('50.47959','-80.518649' , 4326); -- this is the exact point from one of the routes
-- Create a buffer around the "random" point
DECLARE @buffer geography;
SET @buffer = @startPoint.STBuffer(5000); -- Create a 5km radius buffer
-- Finding the routes that pass through the buffer
SELECT [route_id], [route_name],[route_geography].
FROM [test-DB].[dbo].[routes]
WHERE @buffer.STContains([route_geography]) = 1; -- when assigned 0 returns all the routes
-- Declare the "random" point
DECLARE @startPoint geography;
SET @startPoint = geography::Point('50.47959','-80.518649' , 4326); -- this is the exact point from one of the routes
-- Create a buffer around the "random" point
DECLARE @buffer geography;
SET @buffer = @startPoint.STBuffer(5000); -- Create a 5km radius buffer
-- Finding the routes that pass through the buffer
SELECT [route_id], [route_name]
FROM [test-DB].[dbo].[routes]
WHERE [route_geography].STIntersects(@buffer) = 1; -- when assigned 0 returns all the routes
这可能是因为您向
geography::Point
提供的参数顺序错误。函数 geography::Point
首先采用纬度,然后采用经度。这与 WKT 不同,WKT 的坐标先是经度,然后是纬度。文档中的注释中指出了这一点:https://learn.microsoft.com/en-us/sql/t-sql/spatial-geography/point-geography-data-type?view=sql-server -ver16