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

前端之家收集整理的这篇文章主要介绍了将地理转换为几何SQL Server 2008R2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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的博客的人发表的一篇重要提示,“请记住,这种方法很天真,因为它不能适应几种潜在的边缘条件.不过,这种方法应该是在许多情况下工作.“

猜你在找的MsSQL相关文章