加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

将地理转换为几何SQL Server 2008R2

发布时间:2020-12-12 16:27:03 所属栏目:MsSql教程 来源:网络整理
导读:Hello,i have the following code in SQL Server,why if i want to calculate the sTArea of @geog fails and with @geom succeed?,how can i convert this polygon from geometry to geography datatype in order to get the STArea?,thank you. DECLARE @g

Hello,i have the following code in SQL Server,why if i want to
calculate the sTArea of @geog fails and with @geom succeed?,how can i
convert this polygon from geometry to geography datatype in order to get the
STArea?,thank you.

DECLARE @geom geometry;
SET @geom = geometry::STGeomFromText('POLYGON ((-99.213546752929688 19.448402404785156,-99.2157974243164 19.449802398681641,-99.2127456665039 19.450002670288086,-99.213546752929688 19.448402404785156))',4326); 
select @geom.STArea();

DECLARE @geog geography;
SET @geog = geography::STGeomFromText('POLYGON ((-99.213546752929688 19.448402404785156,4326); 
select @geog.STArea();

解决方法

我捅了一下,这个答案 How can I convert Geometry data into a Geography data in MS SQL Server 2008?或多或少只是指向 http://blogs.msdn.com/b/edkatibah/archive/2008/08/19/working-with-invalid-data-and-the-sql-server-2008-geography-data-type-part-1b.aspx,这让我得到一个合理的解释和工作代码.

关键所在:您必须首先确保您的几何图形可以转换为有效的地理位置.

代码(当然可以将一些操作组合在一起,但为了清楚起见,它们在这里被分解.)

DECLARE @geog GEOGRAPHY;
DECLARE @geom GEOMETRY;

SET @geom = GEOMETRY::STGeomFromText('POLYGON ((-99.213546752929688 19.448402404785156,4326); 
SET @geom = @geom.MakeValid() --Force to valid geometry
SET @geom = @geom.STUnion(@geom.STStartPoint()); --Forces the correct the geometry ring orientation
SET @geog = GEOGRAPHY::STGeomFromText(@geom.STAsText(),4326) 

SELECT @geog.STArea();

对于那些不会一直阅读Spatial Ed的博客的人发表的一篇重要提示,“请记住,这种方法很天真,因为它不能适应几种潜在的边缘条件.不过,这种方法应该是在许多情况下工作.“

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读