时间:2021-07-01 10:21:17 帮助过:173人阅读
sqlserver目前已经可以支持地理空间字段了,也就是 geometry API地址: http://msdn.microsoft.com/zh-cn/LIBRARY/cc280487.aspx 创建表和geometry字段以及插入点,线,多边形的sql如下: IF OBJECT_ID ( dbo.SpatialTable, U ) IS NOT NULL DROP TABLE dbo.Sp
sqlserver目前已经可以支持地理空间字段了,也就是 geometry
API地址:
http://msdn.microsoft.com/zh-cn/LIBRARY/cc280487.aspx
创建表和geometry字段以及插入点,线,多边形的sql如下:
IF OBJECT_ID ( 'dbo.SpatialTable', 'U' ) IS NOT NULL DROP TABLE dbo.SpatialTable; GO CREATE TABLE SpatialTable ( id int IDENTITY (1,1), geom geometry, adress varchar ); GO INSERT INTO SpatialTable (geom) VALUES (geometry::STGeomFromText('POINT (20 180)', 4326)); INSERT INTO SpatialTable (geom) VALUES (geometry::STGeomFromText('LINESTRING (100 100, 20 180, 180 180)', 4326)); INSERT INTO SpatialTable (geom) VALUES (geometry::STGeomFromText('POLYGON ((0 0, 150 0, 150 150, 0 150, 0 0))', 4326)); GO
ps: 4326是空间引用标识符 (SRID) 一般写0或者4326
存入表中的geom字段如下:
查询语句
假如我们在数据库中存了很多坐标点的地址
选取圆形区域范围的 地址---也就是 圆心到半径范围内的所有点
DECLARE @g geometry; set @g = geometry::STGeomFromText('POINT(104.12765 30.60445)', 4326) SELECT address,geom.STY,geom.STX from SpatialTable where geom.STDistance(@g)<=0.005
PS:这里diatance的单位是 英里
查询出的结果如图选取多边形
DECLARE @g geometry; set @g = geometry::STGeomFromText('POLYGON ((104.12189573049204 30.608145728994504,104.12223905324595 30.60282680842528,104.13262456655161 30.603122311674902,104.13176625966685 30.610066378528995,104.12189573049204 30.608145728994504,104.12189573049204 30.608145728994504))',4326) SELECT address from SpatialTable where geom.STIntersects(@g)=1