2010-09-29 48 views
1

我遇到了SQL Server 2008和幾何函數的問題。我正在嘗試使用STDifference函數創建不重疊的形狀......但它似乎並沒有在100%的時間內工作。SQL Server 2008幾何不工作?

以下是演示此問題的所有示例代碼。任何人有任何想法讓這些不重疊的多邊形? (除了「不要讓你的多邊形,使複雜」)

-- create our Geometry polygons. 
    DECLARE @Geo1 Geometry, @Geo2 Geometry; 
    SET @Geo1 = geometry::STGeomFromText('POLYGON ((-79725887.5234375 42951274.765625, -79699548.921875 42991827.84375, -79695546.375697568 42997990.498925969, -79695559.967852488 42998690.733179785, -79696152.0625 43029192.375, -79715247.75 43051708.75, -79802181.8984375 43020797.71875, -79806253.6640625 43018621.6953125, -79810436.03125 43014767.046875, -79825837.5 43000572.375, -79830640.625 42973672.03125, -79832936.7265625 42960812.4296875, -79725887.5234375 42951274.765625))',0); 
    SET @Geo2 = geometry::STGeomFromText('POLYGON ((-79863430.6875 42937209.4375, -79850399.0625 42940068.75, -79850394.9375 42940069.625, -79845320.6875 42946082.625, -79843216.25 42948576.375, -79832949.125 42960743, -79814909.939453125 43061773.556640625, -79817218.30078125 43060262.947265625, -79823496.6875 43056154.3125, -79867800.5 43027161.5625, -79871834.50390625 43024521.70703125, -79875660.228515625 43022018.123046875, -79875666.8125 43022013.8125, -79875674.536313191 43022008.760254942, -79875676.408203125 43022007.53515625, -79875677.236328125 43022006.994140625, -79875718.458984375 43021980.017578125, -79875728.506891936 43021973.442301653, -79881723.017578125 43018050.58984375, -79882437.0625 43017583.3125, -79882585.375 43017486.25, -79884466.9375 43016254.9375, -79884550.875 43016200, -79886514.3125 43014915.125, -79887785.5 43014083.25, -79887790.4375 43014080, -79887793.125 43014078.26953125, -79887807.171875 43014069.076171875, -79887825.568359375 43014057.03515625, -79887831.322335 43014053.269705132, -79888029.5 43013923.5625, -79890094.5625 43012572.1875, -79934236.875 42983685.125, -79863430.6875 42937209.4375))',0); 

    -- A quick test so you can see them 
    SELECT @Geo1, 'Geo1' as MyName UNION ALL SELECT @Geo2, 'Geo2' as MyName; 

    -- how do they compare initially? 
    SELECT 
    @Geo1.STTouches(@Geo2) as 'Touches' 
    ,@Geo1.STOverlaps(@Geo2) as 'Overlaps' 
    ,@Geo1.STIntersects(@Geo2) as 'Intersects' 

    -- try to make them NOT overlap or intersect 
    SET @Geo1 = @Geo1.STDifference(@Geo2); 
    SET @Geo2 = @Geo2.STDifference(@Geo1); 

    -- Now, how do they compare? 
    SELECT 
    @Geo1.STTouches(@Geo2) as 'Touches' 
    ,@Geo1.STOverlaps(@Geo2) as 'Overlaps' 
    ,@Geo1.STIntersects(@Geo2) as 'Intersects' 

    -- what does the intersection look like? 
    SELECT @Geo1.STIntersection(@Geo2).STAsText(); 

這是最後的選擇結果:

MULTIPOLYGON (((-79831832.015625 42966999.5078125, -79830502.34765625 42974446.45703125, -79830181.430394545 42976243.79133676, -79830220.96484375 42976022.375, -79831832.015625 42966999.5078125)), ((-79832481.0538819 42963364.484146826, -79832328.75390625 42964217.45703125, -79831832.015625 42966999.5078125, -79832481.0538819 42963364.484146826))) 

回答

0

不是一個答案,只是一個觀察...我得到不同的結果,如果我爲兩個差異引入兩個新變量。

-- create our Geometry polygons. 
    DECLARE @Geo1 Geometry, @Geo2 Geometry, @Geo3 Geometry, @Geo4 Geometry; 
    SET @Geo1 = geometry::STGeomFromText('POLYGON ((-79725887.5234375 42951274.765625, -79699548.921875 42991827.84375, -79695546.375697568 42997990.498925969, -79695559.967852488 42998690.733179785, -79696152.0625 43029192.375, -79715247.75 43051708.75, -79802181.8984375 43020797.71875, -79806253.6640625 43018621.6953125, -79810436.03125 43014767.046875, -79825837.5 43000572.375, -79830640.625 42973672.03125, -79832936.7265625 42960812.4296875, -79725887.5234375 42951274.765625))',0); 
    SET @Geo2 = geometry::STGeomFromText('POLYGON ((-79863430.6875 42937209.4375, -79850399.0625 42940068.75, -79850394.9375 42940069.625, -79845320.6875 42946082.625, -79843216.25 42948576.375, -79832949.125 42960743, -79814909.939453125 43061773.556640625, -79817218.30078125 43060262.947265625, -79823496.6875 43056154.3125, -79867800.5 43027161.5625, -79871834.50390625 43024521.70703125, -79875660.228515625 43022018.123046875, -79875666.8125 43022013.8125, -79875674.536313191 43022008.760254942, -79875676.408203125 43022007.53515625, -79875677.236328125 43022006.994140625, -79875718.458984375 43021980.017578125, -79875728.506891936 43021973.442301653, -79881723.017578125 43018050.58984375, -79882437.0625 43017583.3125, -79882585.375 43017486.25, -79884466.9375 43016254.9375, -79884550.875 43016200, -79886514.3125 43014915.125, -79887785.5 43014083.25, -79887790.4375 43014080, -79887793.125 43014078.26953125, -79887807.171875 43014069.076171875, -79887825.568359375 43014057.03515625, -79887831.322335 43014053.269705132, -79888029.5 43013923.5625, -79890094.5625 43012572.1875, -79934236.875 42983685.125, -79863430.6875 42937209.4375))',0); 

    -- A quick test so you can see them 
    SELECT @Geo1, 'Geo1' as MyName UNION ALL SELECT @Geo2, 'Geo2' as MyName; 

    -- how do they compare initially? 
    SELECT 
    @Geo1.STTouches(@Geo2) as 'Touches' 
    ,@Geo1.STOverlaps(@Geo2) as 'Overlaps' 
    ,@Geo1.STIntersects(@Geo2) as 'Intersects' 

    -- try to make them NOT overlap or intersect 
    SET @Geo3 = @Geo1.STDifference(@Geo2); 
    SET @Geo4 = @Geo2.STDifference(@Geo1); 

    -- Now, how do they compare? 
    SELECT 
    @Geo3.STTouches(@Geo4) as 'Touches' 
    ,@Geo3.STOverlaps(@Geo4) as 'Overlaps' 
    ,@Geo3.STIntersects(@Geo4) as 'Intersects' 

    -- what does the intersection look like? 
    SELECT @Geo3.STIntersection(@Geo4).STAsText(); 

比較:

Touches  Overlaps  Intersects 
1   0    1 

最終選擇:

MULTIPOINT ((-79830220.96484375 42976022.375), (-79831832.015625 42966999.5078125)) 
+0

我能理解這個結果。在我的初始SQL中,第二個SET ... STDifference行正在使用在第一個SET中修改過的@ Geo1。但是,實際上,我不應該做第二個。第一個應該使Geo1與Geo2不重疊。我在那裏設置Geo2的事實是因爲我認爲它會有所幫助。感謝您的觀察。我會看看它是否會幫助我。 – 2010-09-30 12:49:54

0

我懷疑這是由於方法/精確度與SQL Server執行空間計算。對於像STIntersection()等操作,SQL Server將提供的浮點座標值四捨五入爲27位整數網格。這可能對作爲空間操作結果返回的座標稍稍造成扭曲。

在您的情況下,您的座標值很大,這意味着固定大小的整數網格必須非常粗糙以適應全部數據範圍。但是,您嘗試刪除的相交座標之間的相對差異非常小。捕捉到這個粗糙網格時,SQL Server無法正確識別相交區域。

在這裏看到一個解釋,也建議,這種情況已經在SQL Server Denali車型進行了改進: https://connect.microsoft.com/SQLServer/feedback/details/580254/spatial-operations-are-done-with-a-low-precision-causing-troubles-in-the-returned-data

0

正如喬·斯特凡內利指出,你的多邊形沒有技術上重疊,但他們做的相交。原因是,如果使用較小的多邊形從一個多邊形中切出一個孔,則較小的多邊形將不會與新多邊形重疊,但會沿着切割的邊緣與其交叉。它不像臺鋸那樣沿切割邊緣去除一些材料。我無法包含代碼,因爲SO在某些SQL語句中遇到了問題並阻止了該帖子。

看到圖像

參考如果你想它不重疊OR相交,你可以創建自己的鋸片效果。在STDifference()函數中使用時,在較小的幾何圖形周圍應用一個非常小的緩衝區。

查閱參考B圖像

我知道這個職位是舊的,但它似乎仍然相關,對於我們這些誰仍然會在2008年的一段時間。

enter image description here