空间 SQL(在 SSMS 中 - 使用 T-SQL)- STIntersect 和 STContains 不返回正确的结果

问题描述 投票:0回答:1

正在寻找

  1. 经过随机点周围 5 公里缓冲区的所有路线。

背景

  1. 我使用以下查询将多条路线存储为地理类型的线串。线串有效。
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. 我在路线附近有一个随机点。

注意:出于测试目的,该随机点是其中一条路线中存在的点。所以理论上我至少应该得到 1 条路线的真实结果

  1. 存储路由的表结构:

Columns and it's types: route_geography of type geography

尝试了以下查询

  1. 使用 STContains:不会将任何路线返回为 true,而实际上它应该返回,因为我选择的(纬度,经度)点位于路线本身中
-- 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
  1. 使用 STIntersect:与 STContains 相同的结果
-- 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
sql tsql geospatial spatial spatial-query
1个回答
0
投票

这可能是因为您向

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

© www.soinside.com 2019 - 2024. All rights reserved.