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进行类型转换方可进行获取
随机推荐
- OpenJudge计算概论-点和正方形的关系【判断点是否在正方形内部】
/*======================================================== 点和正方形的关系 总时间限制: 1000ms 内存限制: 65536kB 描述 有 ...
- MySQL索引背后的数据结构及算法原理 --转
写在前面的话 在编程领域有一句人尽皆知的法则“程序 = 数据结构 + 算法”,我个人是不太赞同这句话(因为我觉得程序不仅仅是数据结构加算法),但是在日常的学习和工作中我确认深深感受到数据结构和算法的重 ...
- [java] 40个Java多线程问题总结
40个问题汇总 1.多线程有什么用? 一个可能在很多人看来很扯淡的一个问题:我会用多线程就好了,还管它有什么用?在我看来,这个回答更扯淡.所谓"知其然知其所以然","会用 ...
- EF LEFT JON 关联查找
var query = (from a in context.OQC_INSPECTION_SAMPLE.Where(expression).Where(a => context.OQC_INS ...
- C#与JAVA平台RSA算法交互示例
很久以前的文章中,演示了如何对于.net和win32下面的delphi的RSA互操作性的实现,对于C#和JAVA之前的RSA加密解密也是很简单的,一般都采用了标准的规范,所以在互操作性方面是很方便的. ...
- Neutron分析(6)—— neutron-openvswitch-agent
neutron-openvswitch-agent代码分析 neutron.plugins.openvswitch.agent.ovs_neutron_agent:main # init ovs fi ...
- HackerRank "Self Balancing Tree"
Something to learn: Rotation ops in AVL tree does not require recursion. https://github.com/andreima ...
- Configure Puppet Master with Passenger and Apache on Centos
What is Passenger? Passenger (AKA mod_rails or mod_rack) is an Apache 2.x module which lets you run ...
- Add LUN to ASM in Linux
# Create new LUN for Linux in the AMS2100 # echo "- - -" >/sys/class/scsi_host/host3/sc ...
- android学习笔记16——对话框
android支持丰富的对话框,常用4中对话框: 1.AlertDialog: 2.ProgressDialog:进度对话框,这个对话框只是对进度条的封装 3.DatePickerDialog:日期选 ...