通过WKT构造Point对象
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POINT (1 2)', 0); --定义点对象
SET @g = geometry::STPointFromText('POINT (1 2)',0);
SET @g = geometry::Parse('PoINT (1 2)');
返回实例的WKT和WKB
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POINT (1 2)', 0);
SELECT @g.ToString() --返回WKT
SELECT @g.STAsBinary() --返回对象的WKB
返回Point实例的X、Y、Z和M
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POINT (1 2 3 4)', 0);
SELECT @g.STX; --查询对象下的各个属性
SELECT @g.STY;
SELECT @g.Z;
SELECT @g.M;
--
--15.2.2节示例
--
DECLARE @g geometry;
SET @g = geometry::STMPointFromText('MultiPoint((0 0),(1 1 2 3),(3 4))', 0);
GO
DECLARE @g geometry;
SET @g = geometry::STMPointFromText('MultiPoint(0 0,1 1 2 3,3 4)', 0);
GO
--使用STNumGeometries()方法
DECLARE @g geometry;
SET @g = geometry::STMPointFromText('MultiPoint(0 0,1 1 2 3,3 4)', 0);
SELECT @g.STNumGeometries()
--
--15.2.3节示例
--
构造LineString对象
DECLARE @g geometry;
SET @g = geometry::STLineFromText('LINESTRING(0 0,1 1 2 3,3 4)',0);
求LineString实例的总长度
DECLARE @g geometry;
SET @g = geometry::STLineFromText('LINESTRING(0 0,3 4)',0);
SELECT @g.STLength() --获得线的长度
获得LineString实例的起始点
DECLARE @g geometry;
SET @g = geometry::STLineFromText('LINESTRING(0 0,3 4)',0);
SELECT @g.STStartPoint().ToString() --获得线的起点
--系统返回结果:
POINT (0 0)
获得LineString实例中指定的点
DECLARE @g geometry;
SET @g = geometry::STLineFromText('LINESTRING(0 0,3 4,2 2,4 3)',0);
SELECT @g.STPointN(2).ToString()
--系统返回结果:
POINT (3 4)
获得LineString实例中的点数
DECLARE @g geometry;
SET @g = geometry::STLineFromText('LINESTRING(0 0,3 4,2 2,4 3,0 0)',0);
SELECT @g.STNumPoints()
--其中点(0,0)重复,但仍将计数,系统返回结果5。
判断LineString实例是否简单闭合的
DECLARE @g geometry;
SET @g = geometry::STLineFromText('LINESTRING(0 0,3 4,2 2,4 3,0 0)',0);
SELECT @g.STIsRing() --返回1
SET @g = geometry::STLineFromText('LINESTRING(0 0,3 4,2 2,4 3)',0);
SELECT @g.STIsRing() --返回0
--
--15.2.4节示例
--
构造MultiLineString实例
DECLARE @g geometry;
SET @g = geometry::STMLineFromText(
'MULTILINESTRING((100 100, 200 200), (3 4, 7 8, 10 10))', 0);
--
--15.2.5节示例
--
构造正方形的Polygon实例
DECLARE @g geometry;
SET @g = geometry::Parse('POLYGON((1 0, 0 1, 1 2, 2 1, 1 0))');
构造有内部环的Polygon实例
DECLARE @g geometry;
SET @g = geometry::Parse('POLYGON((1 0, 0 1, 1 2, 2 1, 1 0),(0.5 0.6,0.7 0.7,0.5 0.7,0.5 0.6))');
获得Polygon实例的面积
DECLARE @g geometry;
SET @g = geometry::Parse('POLYGON((1 0, 0 1, 1 2, 2 1, 1 0))');
SELECT @g.STArea()
--系统返回该多边形的面积:2
获得Polygon实例的外环
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 3 0, 3 3, 0 3, 0 0),(2 2, 2 1, 1 1, 1 2, 2 2))', 0);
SELECT @g.STExteriorRing().ToString();
--系统返回结果:
LINESTRING (0 0, 3 0, 3 3, 0 3, 0 0)
获得Polygon实例的内环数
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 3 0, 3 3, 0 3, 0 0),(2 2, 2 1, 1 1, 1 2, 2 2))', 0);
SELECT @g.STNumInteriorRing()
--系统返回结果:1
获得Polygon实例的一个内环
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 3 0, 3 3, 0 3, 0 0),(2 2, 2 1, 1 1, 1 2, 2 2))', 0);
SELECT @g.STInteriorRingN(1).ToString();
--系统返回结果:
LINESTRING (2 2, 2 1, 1 1, 1 2, 2 2)
获得Polygon实例的几何中心
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 3 0, 3 3, 0 3, 0 0),(2 2, 2 1, 1 1, 1 2, 2 2))', 0);
SELECT @g.STCentroid().ToString(); --获得几何中心
--系统返回结果:
POINT (1.5 1.5)
获得Polygon实例中的一个点
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 3 0, 3 3, 0 3, 0 0),(2 2, 2 1, 1 1, 1 2, 2 2))', 0);
SELECT @g.STPointOnSurface().ToString();
--系统返回该多边形中的一个点:
POINT (1.6666666666666667 2.6666666666666665)
--
--15.2.6节示例
--
构造MuliPolygon实例
DECLARE @g geometry;
SET @g = geometry::STMPolyFromText('MULTIPOLYGON(((47.653 -122.358, 47.649 -122.348, 47.658 -122.358, 47.653 -122.358)), ((47.656 -122.341, 47.661 -122.341, 47.661 -122.351, 47.656 -122.341)))', 0);
--
--15.2.7节示例
--
GeometryCollection中包含GeometryCollection对象
DECLARE @g geometry;
SET @g = geometry::STGeomCollFromText('GEOMETRYCOLLECTION(LINESTRING(1 2, 3 4),GEOMETRYCOLLECTION(POINT(3 3 1), POLYGON((0 0 2, 1 10 3, 1 0 4, 0 0 2))))', 1);
获得GeometryCollection中包含的Geometry对象数
DECLARE @g geometry;
SET @g = geometry::STGeomCollFromText('GEOMETRYCOLLECTION(LINESTRING(1 2, 3 4),GEOMETRYCOLLECTION(POINT(3 3 1), POLYGON((0 0 2, 1 10 3, 1 0 4, 0 0 2))))', 1);
SELECT @g.STNumGeometries () --系统返回:2
SELECT @g.STGeometryN(1).ToString() --系统返回:LINESTRING (1 2, 3 4)
--
--15.2.8节示例
--
求点的缓冲区
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('Point(0 0)', 0);
SELECT @g.STBuffer(1).ToString(); --距离为1的缓冲区的WKT
求折线的缓冲区
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LineString(0 2,0 0,2 0)', 0);
SELECT @g.STBuffer(1).ToString(); --折线的缓冲区图形的WKT
求点的有公差的缓冲区
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POINT(3 3)', 0);
SELECT @g.BufferWithTolerance(1, .5, 0).ToString(); --公差缓冲区图形的WKT
返回LineString实例的近似值
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 0 1, 1 0, 2 1, 3 0, 4 1)', 0);
SELECT @g.Reduce(.75).ToString(); --线的近似值
--系统返回结果:
LINESTRING (0 0, 0 1, 3 0, 4 1)
返回Polygon对象的凸包
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 0 2, 1 1, 2 2, 2 0, 0 0))', 0);
SELECT @g.STConvexHull().ToString(); --凸包图形的WKT
--系统返回结果:
POLYGON ((2 0, 2 2, 0 2, 0 0, 2 0))
获得2个Polygon实例的公共部分
DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))', 0);
SET @h = geometry::STGeomFromText('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))', 0);
SELECT @g.STIntersection(@h).ToString(); --相交部分的WKT
--系统返回结果:
POLYGON ((1 1, 2 1, 2 2, 1 2, 1 1))
获得2个Polygon实例的并集
DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))', 0);
SET @h = geometry::STGeomFromText('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))', 0);
SELECT @g.STUnion(@h).ToString(); --并集的WKT
--系统返回结果:
POLYGON ((0 0, 2 0, 2 1, 3 1, 3 3, 1 3, 1 2, 0 2, 0 0))
获得2个LineString实例的并集
DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('LineString(0 0, 0 2, 2 2, 2 0)', 0);
SET @h = geometry::STGeomFromText('LineString(0 0, 3 1, 3 3, 1 3, 3 0)', 0);
SELECT @g.STUnion(@h).ToString(); --两条线的并集
获得2个Polygon实例的1个中不重叠的部分
DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))', 0);
SET @h = geometry::STGeomFromText('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))', 0);
SELECT @g.STDifference(@h).ToString(); --不重叠部分的WKT
--系统返回结果:
POLYGON ((0 0, 2 0, 2 1, 1 1, 1 2, 0 2, 0 0))
获得2个Polygon实例不相交部分
DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))', 0);
SET @h = geometry::STGeomFromText('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))', 0);
SELECT @g.STSymDifference(@h).ToString(); --不相交部分的WKT
--系统返回结果:
MULTIPOLYGON (((2 1, 3 1, 3 3, 1 3, 1 2, 2 2, 2 1)), ((0 0, 2 0, 2 1, 1 1, 1 2, 0 2, 0 0)))
获得1个LineString和1个Polygon不相交部分
DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))', 0);
SET @h = geometry::STGeomFromText('LineString(1 1, 1 1.5)', 0);
SELECT @g.STSymDifference(@h).ToString(); --不相交部分的WKT
--系统将返回结果:
POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))
获得实例的名称
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 3 0, 3 3, 0 3, 0 0))', 0);
SELECT @g.STGeometryType(); --获得实例名称
--系统将返回结果:Polygon
判断实例格式是否正确
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 1 1)', 0);
SELECT @g.STIsValid(); --验证WKT描述是否正确
--系统返回结果:0
获得实例的有效格式
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 1 1)', 0);
SELECT @g.MakeValid().ToString(); --获得有效WKT
--系统返回结果:
LINESTRING (2 2, 1 1, 0 0)
--
--15.2.9节示例
--
获得维度
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('GeometryCollection(POLYGON((0 0, 0 2, 2 2, 2 0, 0 0)),LineString(1 1, 1 1.5),Point(0 1))', 0);
SELECT @g.STDimension(); --返回维度
--系统返回结果:2
判断实例是否为空
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON EMPTY', 0);
SELECT @g.STIsEmpty(); --是否为空
--系统返回结果:1
获得Polygon对象的边界
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0),(1 1, 1 1.5,1.5 1.5 1.5 1,1 1))', 0);
SELECT @g.STBoundary().ToString(); --对象边界WKT
--系统返回结果:
MULTILINESTRING ((1 1, 1 1.5, 1.5 1.5, 1 1), (0 0, 2 0, 2 2, 0 2, 0 0))
获得LineString实例的包络线
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 3)', 0);
SELECT @g.STEnvelope().ToString(); --对象包络线WKT
--系统返回结果:
POLYGON ((0 0, 2 0, 2 3, 0 3, 0 0))
获得和修改SRID
DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 3 0, 3 3, 0 3, 0 0))', 1);
SELECT @g.STSrid; --返回
SET @g.STSrid = 3;
SELECT @g.STSrid; --返回
--
--15.2.10节示例
--
判断2个实例的点集是否相同
DECLARE @g geometry
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 2, 2 0, 4 2)', 0);
SET @h = geometry::STGeomFromText('MULTILINESTRING((4 2, 2 0), (0 2, 2 0))', 0);
SELECT @g.STEquals(@h); --是否相同
--系统返回结果:1
判断2个实例是否不相接
DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 2, 2 0, 4 2)', 0);
SET @h = geometry::STGeomFromText('POINT(1 1)', 0);
SELECT @g.STDisjoint(@h); --是否不相接
--由于点在线上,所以返回结果:0
判断2实例是否接触
DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 2, 2 0)', 0);
SET @h = geometry::STGeomFromText('LINESTRING(2 2,1 1)', 0);
SELECT @g.STTouches(@h); --两实例相接触,返回1
SET @h = geometry::STGeomFromText('LINESTRING(2 2,0 0)', 0);
SELECT @g.STTouches(@h); --两实例未接触,返回0
判断2实例是否重叠
DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))', 0);
SET @h = geometry::STGeomFromText('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))', 0);
SELECT @g.STOverlaps(@h); --是否重叠
--系统返回结果:1
判断2实例是否交叉
DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 2, 2 0)', 0);
SET @h = geometry::STGeomFromText('LINESTRING(2 2,1 1)', 0);
SELECT @g.STCrosses(@h); --两实例相交点是其中一个实例的边界,返回0
SET @h = geometry::STGeomFromText('LINESTRING(2 2,0 0)', 0);
SELECT @g.STCrosses(@h); --两实例相交叉,返回1
判断一个实例是否在另一个实例内部
DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))', 0);
SET @h = geometry::STGeomFromText('POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))', 0);
SELECT @g.STWithin(@h); --是否在实例内部
--由于有共同的部分边界,所以系统返回0
判断一个实例是否包含另一个实例
DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))', 0);
SET @h = geometry::STGeomFromText('POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))', 0);
SELECT @g.STContains(@h); --是否包含了另一个实例
--系统返回:1
使用DE-9IM判断两个实例关系
DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 2, 2 0, 4 2)', 0);
SET @h = geometry::STGeomFromText('POINT(5 5)', 0);
SELECT @g.STRelate(@h, 'FF*FF****'); --实例关系判断
--系统返回结果:1
获得实例之间最短距离
DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('LineString(0 0, 2 0, 2 2, 0 2)', 0);
SET @h = geometry::STGeomFromText('POINT(10 10)', 0);
SELECT @g.STDistance(@h); --实例之间最短距离
--系统返回结果:11.3137084989848