Geometry常用的function 1 of 3
繼續介紹其他 Geometry 的 function
- .STCrosses() - 檢查線是否有相交
DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 2, 2 0)', 0);
SET @h = geometry::STGeomFromText('LINESTRING(0 0, 2 2)', 0);
SELECT @g.STCrosses(@h);
--> 有,回傳1,若無回傳0
PS:但這好像不適合用在面,雖然可以執行,不會出錯,但明明有相交卻還是回傳0
- .STDisjoint() - 檢查幾何是否有分離(就是交集的相反
DECLARE @a geometry= (select geom from 太平區);
DECLARE @b geometry= (select geom from 太平區霧峰區);
DECLARE @c geometry= (select geom from 沙鹿區);
SELECT @a.STDisjoint(@b); -->回傳0,沒有分離
SELECT @a.STDisjoint(@c); -->回傳1,有分離
- .STDimension() - 計算幾何的類型
DECLARE @temp table ([name] varchar(10), [geom] geometry);
INSERT INTO @temp values ('Point', geometry::STGeomFromText('POINT(3 3)', 0));
INSERT INTO @temp values ('LineString', geometry::STGeomFromText('LINESTRING(0 0, 3 3)', 0));
INSERT INTO @temp values ('Polygon', geometry::STGeomFromText('POLYGON((0 0, 3 0, 0 3, 0 0))', 0));
SELECT [name], [geom].STDimension() as [dim]
FROM @temp;
結果
- .STStartPoint() - 回傳第一個點位
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 1 0)', 0);
SELECT @g.STStartPoint().ToString();
--回傳 POINT (0 0)
- .STEndPoint() - 回傳最後一個點位
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 1 0)', 0);
SELECT @g.STEndPoint().ToString();
--回傳 POINT (1 0)
- .STGeometryN() - 取得該幾何圖形的第幾個幾何
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('MULTIPOINT(0 0, 13.5 2, 7 19)', 0);
SELECT @g.STGeometryN(2).ToString();
--> POINT (13.5 2)
- .STPointN() - 取得該幾何圖形的第幾個點
DECLARE @a geometry= (select geom from 太平區);
SELECT @a.STPointN(10).ToString();
--> POINT (120.80155499156966 24.169213615983971)
- .STIsClosed() - 檢查線的圖形是否有封閉(就是檢查頭尾的點有沒有一樣)
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 1 0)', 0);
SELECT @g.STIsClosed();
--> 若有回傳1,無回傳0
- .STIsEmpty() - 是否為空的
DECLARE @a geometry= (select geom from 太平區);
DECLARE @c geometry= (select geom from 沙鹿區);
SELECT @a.STIntersection(@c).STIsEmpty();
--> 回傳1,因為太平區跟沙鹿區的確沒有交集
- .STIsSimple() - 檢查這個幾何圖形中有沒有自己的點或線交錯
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 0 2, 2 0)', 0);
SELECT @g.STIsSimple();
--> 回傳0,因為有交錯
- .STLength() - 計算長度
若是點則為0,若為線則就是線的長度
若是面就是面的周長
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 1 0)', 0);
SELECT @g.STLength();
--> 5.06449510224598
- .STNumPoints() - 計算幾何圖形有幾個點
DECLARE @a geometry= (select geom from 太平區);
SELECT @a.STNumPoints();
--> 3032
- .STOverlaps() - 檢查幾何圖形是否有重疊
DECLARE @a geometry= (select geom from 太平區);
DECLARE @b geometry= (select geom from 太平區霧峰區);
DECLARE @c geometry= (select geom from 大里區太平區);
SELECT @a.STOverlaps(@b);
--> 回傳0,因為太平區算是被包含在太平區霧峰區裡面,不算有重疊
SELECT @b.STOverlaps(@c);
--> 回傳1,因為太平區霧峰區 跟 大里區太平區 有交集到太平區
以上常用的 function
希望對大家有幫助
沒有留言:
張貼留言