简体   繁体   English

SQL地理相交总是返回1

[英]SQL Geography Intersect always returns 1

I'm trying to determine whether a point is within a polygon. 我试图确定一个点是否在多边形内。 The results I'm getting back appear to be always returning 1 regardless of whether the point is within the boundary or not. 无论点是否在边界内,我返回的结果似乎总是返回1。

DECLARE @point GEOGRAPHY = GEOGRAPHY::Point(54.2225,-4.5366, 4326)
DECLARE @polygon GEOGRAPHY = GEOGRAPHY::STGeomFromText('POLYGON((54.2826 -4.4420, 54.2904 -4.6564, 54.0467 -4.7031, 54.2826 -4.4420))', 4326)

SELECT @polygon.STIntersects(@point), @point.STIntersects(@polygon)

I'm using SQL Express 2014 running locally 我正在使用本地运行的SQL Express 2014

You have a classic problem. 你有一个经典的问题。 The order in which you specify points in a geometry polygon is meaningful. 在几何多边形中指定点的顺序是有意义的。 As you've defined it, you've created a polygon that is the whole globe minus a tiny hole. 定义好之后,您创建了一个多边形,即整个地球减去一个小孔。 Luckily, both the test for this problem and the fix are fairly easy. 幸运的是,针对此问题的测试和修复都相当容易。

DECLARE @polygon GEOGRAPHY = GEOGRAPHY::STGeomFromText('POLYGON((54.2826 -4.4420, 54.2904 -4.6564, 54.0467 -4.7031, 54.2826 -4.4420))', 4326)
set @polygon = @polygon.ReorientObject();
select @polygon.EnvelopeAngle()

If you comment out the call to ReorientObject() , you'll see that the envelope angle is 180 degrees. 如果您注释掉对ReorientObject()的调用,则会看到包络角为180度。 That is the heuristic that I use personally to see if there's a ring orientation problem. 这是我个人用来查看是否存在环向问题的启发式方法。 You could also check the area with STArea() . 您也可以使用STArea()检查该区域。 Either way, the re-oriented object is the what you were probably expecting and should give you better results when doing intersection tests! 无论哪种方式,重新定向的对象都是您可能期望的,并且在进行相交测试时应能为您提供更好的结果!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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