【问题标题】:SQL Server Spatial Select Where STContainsSQL Server 空间选择 ST 包含的位置
【发布时间】: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


【解决方案1】:

我的猜测是对于某些地区,MakeValid 还修复了多边形的方向。如果多边形无效,MakeValid 必须猜测预期的形状,并使用一些启发式方法对其进行修复。结果可能会有所不同,具体取决于数据的确切错误 - 有时垃圾输入,垃圾输出适用。

我会避免在查询中同时使用MakeValid()ReorientObject()。这既容易出错,又很慢(因为它会阻止空间索引的使用)。

相反,通过将地理位置更新为预期的地理位置来修复实际数据。

快速而肮脏的方法是仅反转需要反转的那些,例如

update census_tracts 
set geog4269 = 
    IF (geog4269.MakeValid().STArea() < 1e14, -- is it small?
        geog4269.MakeValid(),
        geog4269.MakeValid().ReorientObject());

【讨论】:

    猜你喜欢
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 2011-05-27
    • 2014-07-15
    相关资源
    最近更新 更多