sql server Geometry 类型操作 笔记
sqlGeometry 类型为sql server 2008之后的版本 新加的一种CLR扩展数据类型,为广大sql server开发人员存储几何类型及空间运算提供极大的便利,下面说明geometry类型的具体操作
示例SQL语句代码
IF OBJECT_ID ( 'dbo.SpatialTable', 'U' ) IS NOT NULL
DROP TABLE dbo.SpatialTable;
GO CREATE TABLE SpatialTable
( id int IDENTITY (1,1),
GeomCol1 geometry,
GeomCol2 AS GeomCol1.STAsText() );
GO INSERT INTO SpatialTable (GeomCol1)
VALUES (geometry::STGeomFromText('LINESTRING (100 100, 20 180, 180 180)', 0)); INSERT INTO SpatialTable (GeomCol1)
VALUES (geometry::STGeomFromText('POLYGON ((0 0, 150 0, 150 150, 0 150, 0 0))', 0));
GO INSERT INTO SpatialTable (GeomCol1)
VALUES(geometry::STGeomFromText('LINESTRING (116.387112 39.920977,116.385243 39.913063,116.394226 39.917988,116.401772 39.921364,116.41248 39.927893,116.387112 39.920977)', 4326))
几何类型操作
Database db = DatabaseFactory.CreateDatabase();
DbCommand dbCmd = db.GetSqlStringCommand("SELECT GeomCol1 FROM SpatialTable WHERE id=4");
using (IDataReader reader = db.ExecuteReader(dbCmd))
{
if (reader.Read())
{
SqlGeometry o = reader[] as SqlGeometry; }
} SqlGeometry geo = SqlGeometry.Parse("POLYGON ((0 0, 150 0, 150 150, 0 150, 0 0))");
//DataSet ds = db.ExecuteDataSet(dbCmd); //SqlGeometry geo = ds.Tables[0].Rows[0][1] as SqlGeometry; //SqlDouble area = geo.STArea(); dbCmd = db.GetSqlStringCommand(@"INSERT INTO SpatialTable (GeomCol1) values(@p)"); //构建多边形
SqlGeometryBuilder sb = new SqlGeometryBuilder();
sb.SetSrid();
sb.BeginGeometry(OpenGisGeometryType.Polygon);
sb.BeginFigure(, );
sb.AddLine(, );
sb.AddLine(, );
sb.AddLine(, );
sb.AddLine(, );
sb.EndFigure();
sb.EndGeometry(); dbCmd = db.GetSqlStringCommand(string.Format("INSERT INTO SpatialTable (GeomCol1) values(geometry::STGeomFromText('{0}', {1}))"
, sb.ConstructedGeometry.ToString(), ));
//db.AddInParameter(dbCmd, "@p", DbType.Binary, sb.ConstructedGeometry.STAsBinary().Buffer); int cnt = db.ExecuteNonQuery(dbCmd);
数据库对数据进行过滤操作
SELECT c_geomCol.MakeValid().STCentroid().STAsText() FROM t_green_point
WHERE c_geomCol IS NOT NULL
--AND c_geomCol2<>'POLYGON EMPTY'
AND c_geomCol.STIsValid()=1
通过这个sql获得系统的坐标系(Sql server中):Select * from sys.spatial_reference_systems
//MultiPolygon 多个多边形结合处理
SqlGeometryBuilder sb = new SqlGeometryBuilder(); //构造多个多边形实例
sb.SetSrid();
sb.BeginGeometry(OpenGisGeometryType.MultiPolygon); sb.BeginGeometry(OpenGisGeometryType.Polygon); //构造第一个多边形
sb.BeginFigure(, );
sb.AddLine(, -);
sb.AddLine(-,-);
sb.AddLine(-,);
sb.AddLine(,);
sb.EndFigure();
sb.EndGeometry(); sb.BeginGeometry(OpenGisGeometryType.Polygon); //构造第二个多边形
sb.BeginFigure(,);
sb.AddLine(,);
sb.AddLine(,);
sb.AddLine(,);
sb.AddLine(,);
sb.EndFigure();
sb.EndGeometry(); sb.BeginGeometry(OpenGisGeometryType.Polygon); //构造第N个多边形
sb.BeginFigure(,);
sb.AddLine(,);
sb.AddLine(,);
sb.AddLine(,);
sb.AddLine(,);
sb.EndFigure();
sb.EndGeometry(); sb.EndGeometry(); geo = sb.ConstructedGeometry; //下面是解析代码
int numGeometries = geo.STNumGeometries().Value; //有多少个多边形 SqlGeometry geoN = null; for (int i = ; i <= numGeometries; i++)
{
geoN = geo.STGeometryN(i);//第几个多边形
for (int j = ; j <= geoN.STNumPoints(); j++) //转到百度地图多边形最后一个点可以去掉
{
point = geoN.STPointN(i);
Console.WriteLine("第{0}个多边形,第{1}点,X={2},Y={3}", i, j, point.STX.Value, point.STY);
} }
输出结果:

参数化的SQL语句传值
DbCommand dbCmd = db.GetSqlStringCommand(@"INSERT INTO SpatialTable (GeomCol1) values(@p)"); db.AddInParameter(dbCmd, "@p", DbType.Binary, sb.ConstructedGeometry.Serialize()); int cnt = db.ExecuteNonQuery(dbCmd);
判断多边形是否相交
DECLARE @bigGeo geometry= 'POLYGON((0 0, 3 0, 3 3, 0 3,0 0))';
DECLARE @smallGeo geometry='POLYGON((1 1 ,2 1,2 2,1 2,1 1))';
DECLARE @midGeo geometry='POLYGON((0 0, 1.5 0, 1.5 1.5, 0 1.5,0 0))';
DECLARE @Geo3 geometry='POLYGON((2 2, 3 2,3 3,2 3,2 2))'; SELECT @bigGeo.STIntersection(@smallGeo).STAsText() SELECT @midGeo.STIntersection(@smallGeo).STIsEmpty() SELECT @midGeo.STIntersection(@Geo3).STIsEmpty() 如果相交则结果不为空
参考资料:
空间数据类型相关Transact-SQL
sql server Geometry 类型操作 笔记的更多相关文章
- SQL Server锁类型
SQL Server锁类型(SQL)收藏 1. HOLDLOCK: 在该表上保持共享锁,直到整个事务结束,而不是在语句执行完立即释放所添加的锁. 2. NOLOCK:不添加共享锁和排它锁,当这个选项生 ...
- Sql Server 函数的操作实例!(执行多条语句,返回Select查询后的临时表)
Sql Server 函数的操作实例!(执行多条语句,返回Select查询后的临时表) SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ==== ...
- Sql Server 函数的操作实例!(返回一条Select语句查询后的临时表)
Sql Server 函数的操作实例!(返回一条Select语句查询后的临时表) SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE FUN ...
- SQL Server 字段类型 decimal(18,6)小数点前是几位?记一次数据库SP的BUG处理
原文:SQL Server 字段类型 decimal(18,6)小数点前是几位?记一次数据库SP的BUG处理 SQL Server 字段类型 decimal(18,6)小数点前是几位? 不可否认,这是 ...
- SQL SERVER: 合并相关操作(Union,Except,Intersect)
SQL SERVER: 合并相关操作(Union,Except,Intersect) use tempdb create table tempTable1 (id int primary key id ...
- MS SQL server对象类型type
执行下面代码,将获取ms sql server对象类型以及其说明 IF OBJECT_ID('tempdb.dbo.#type') IS NOT NULL DROP TABLE #type CREAT ...
- SQL Server时间类型datetime
SQL Server时间类型datetime 兼容ADO的COleDateTime. SQL datetime 日期和时间数据,可表示1753.1.1 至 9999.12.31的时间,精度为1/300 ...
- SQL Server 多库操作 库名.dbo.表名 出错的问题!
SQL Server 多库操作 库名.dbo.表名 出错的问题! 数据库名不要用数字开头. 例如:343934.dbo.user 这就会出错.md a343934.dbo.user 就没问题!! 记住 ...
- SQL Server 方言类型映射问题
关于SQL Server的类型映射问题,例如,nvarchar无法进行hibernate类型映射,需要通过convert进行类型转换方可进行获取
随机推荐
- docker 使用redis
1. 安装 centos 7 yum install docker 2. 启动 修改配置: nano /etc/sysconfig/docker 添加一下信息: OPTIONS='--selinu ...
- 【转】.NET多种WebKit内核/Blink内核浏览器初步测评报告
第1篇:.NET多种WebKit内核/Blink内核浏览器初步测评报告 本文转自“吾乐吧软件站”,原文链接:http://www.wuleba.com/?p=23590 报告研究时间:2013-10- ...
- 【转】PHP 之 CURL 模拟登陆并获取数据
1.CURL模拟登陆的流程和步骤2.tempnam 创建一个临时文件3.使用CURL模拟登陆到PHP100论坛 <?php$cookie_file = tempnam('./temp','coo ...
- weblogic性能调优参考
1.weblogic内存的调优 2.weblogic性能调优 各个方面比较全内容较多 3.记录一次weblogic 11g压力测试性能调优过程 经调优后,具体java启动参数如下: /app/webl ...
- 用HTML5构建一个流程图绘制工具
在我们的开发工程中经常会使用到各种图,所谓的图就是由节点和节点之间的连接所形成的系统,数学上专门有一个分支叫图论(Graph Theroy).利用图我们可以做很多工具,比如思维导图,流程图,状态机,组 ...
- 【linux】grub详解
参数解释 1. default=0 # default后加一个数字n,表示n+1个“title”操作系统,0表示第一个“title” 的操作系统,以此类推. 2. timeout=0 # timeou ...
- cmd命令运行php,php通过cmd运行文件
一.cmd命令运行php 1.通过配置环境变量 >php "php文件" 如果要待参数 php -q "php文件" 参数 php获取参数 $a = $a ...
- Python之Rpyc模块
简介 rpyc (Remote Python Call)为分布式计算环境提供了优良的基础平台.使用rpyc编写c/s结构程序,完全不用考虑老式的socket编程,现在只用编写简单的3.5行代码即可完成 ...
- SVN设置实例
D:\scmserver\SVNROOT\safeControl,该SVN项目下,有erSystem和hcSystem两个项目.现在人员有两种类型的人,一个内部人员,一个是佰钧成人员. 设置要求: 1 ...
- [tty与uart]2.tty和uart的函数调用流程
以下是在include/uapi/linux/tty.h中定义了现有的线规号,如果需要定义新的,则需要在后面添加新的 /* line disciplines */ #define N_TTY 0 #d ...