【发布时间】:2020-05-14 21:59:41
【问题描述】:
我正在研究空间数据库 SQL Server,并且很难查询地理包含给定纬度/经度的行。
我可以让这个查询工作:
DECLARE @polygon Geography;
select @polygon = (
select
geog4269
from census_tracts
WHERE namelsad10 = 'Census Tract 9801.02'
);
set @polygon = @polygon.ReorientObject();
select @polygon.STContains(
geography::Point(18.4102591, -66.0732014, 4269)
);
但是,我希望能够选择包含给定纬度/经度的行,如下所示:
select
*
from census_tracts
WHERE geog4269.ReorientObject().STContains(
geography::Point(18.4102591, -66.0732014, 4269)
) = 1
当我运行使用 MakeValid 来避免它时,我遇到了 .NET Framework 异常,但添加 .MakeValid() 并不能解决问题。
这是异常消息:
Msg 6522, Level 16, State 1, Line 1
A .NET Framework error occurred during execution of user-defined routine or aggregate "geography":
System.ArgumentException: 24144: This operation cannot be completed because the instance is not valid. Use MakeValid to convert the instance to a valid instance. Note that MakeValid may cause the points of a geometry instance to shift slightly.
System.ArgumentException:
at Microsoft.SqlServer.Types.SqlGeography.ThrowIfInvalid()
at Microsoft.SqlServer.Types.SqlGeography.ReorientObject()
.
当我使用以下查询时:
select
*
from census_tracts
WHERE geog4269.MakeValid().ReorientObject().STContains(
geography::Point(18.4102591, -66.0732014, 4269)
) = 1
地理不会重新定向(每个地理都说它包含所有点)。
以前有没有人遇到过类似的事情,或者可以指出我哪里出错了?感谢您的帮助!
【问题讨论】:
-
您能否在代码中添加 MakeValid 和确切的异常消息?
-
@MichaelEntin 我已经添加了您的建议。谢谢。
标签: sql sql-server gis geospatial spatial-query