一、先说说Sql Server中geometry和geography的区别:git
geometry:planar 平面坐标系【supported by SQL Server conforms to the Open Geospatial Consortium (OGC) Simple Features for SQL Specification version 1.1.0.】sql
geography: terrestrial 地理坐标系【stores ellipsoidal (round-earth) data, such as GPS latitude and longitude coordinates.】spa
若是要计算两个lat/lon点之间的实际距离就须要将geometry类型转成geography类型,否则结果确定不正确。rest
二、geometry转geography的方法:orm
geography::STGeomFromText(boundary.ToString(), 4326)server
boundary是geometry类型的,4326是坐标系的参数,4326表明GCS-WGS-1984坐标系,是系统默认的坐标系。ip
能够经过这个sql得到系统的坐标系(Sql server中):Select * from sys.spatial_reference_systems where authorized_spatial_reference_id=4326ci
三、STDistance的用法:it
https://msdn.microsoft.com/zh-cn/library/bb933952(v=sql.110).aspxio
按照里面的例子可以计算出距离,可是若是输入的是经纬度的值,得出的结果老是以为不对,值比较小,实际上须要按照第二步转化为geography类型再计算就能够了,4326坐标系默认返回距离的单位【unit】是米【meter】。
STDistance也能够计算点到面的最短距离。