将地理转换为几何SQL Server 2008R2 [英] Convert geography to geometry SQL Server 2008R2

查看:130
本文介绍了将地理转换为几何SQL Server 2008R2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


您好,我在SQL Server中有以下代码,为什么如果我想要
计算@geog的sTArea失败并且@geom成功?,我该如何
将这个多边形从geometry转换为geography数据类型以获得
STArea ?,谢谢。




  DECLARE @geom几何; 
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,-99.2157974243164 19.449802398681641,-99.2127456665039 19.450002670288086,-99.213546752929688 19.448402404785156))',4326);
select @ geog.STArea();


解决方案

=https://stackoverflow.com/questions/279983/how-can-i-convert-geometry-data-into-a-geography-data-in-ms-sql-server-2008>我怎样才能转换几何数据转换为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 让我得到一个合理的解释和工作代码。
$ b

关键是:你必须确保你的几何图形可以先转换为有效的地理图形。



代码(当然可以有一些操作组合在一起,但为了清晰起见,它们在这里被分开。)

  DECLARE @geog GEOGRAPHY; 
DECLARE @geom GEOMETRY; ((-99.213546752929688 19.448402404785156,-99.2157974243164 19.449802398681641,-99.2127456665039 19.450002670288086,-99.213546752929688 19.448402404785156))'',4326);(b)
SET @geom = @ geom.MakeValid() - 强制为有效几何
SET @geom = @ geom.STUnion(@ geom.STStartPoint()); - 强制正确的几何环定向
SET @geog = GEOGRAPHY :: STGeomFromText(@ geom.STAsText(),4326)

SELECT @ geog.STArea();

对于那些不会阅读Spatial Ed博客文章的重要提示,请记住,这种方法是天真的,因为它不能适应几个潜在的边缘条件。总之,这种方法在很多情况下都适用。


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, -99.2157974243164 19.449802398681641, -99.2127456665039 19.450002670288086, -99.213546752929688 19.448402404785156))', 4326); 
select @geog.STArea();

解决方案

I poked around a little and this answer How can I convert Geometry data into a Geography data in MS SQL Server 2008? which more or less just points to 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 leads me to a reasonable explanation and working code.

The crux: You have to make sure your geometry can be translated to valid geography first.

The code (which can certainly have some operations combined, but they are broken out here for clarity.)

DECLARE @geog GEOGRAPHY;
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); 
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();

And for those that won't read all the way through Spatial Ed's blog post one important tip, "please bear in mind that this approach is naive in that it does not accommodate several potential edge conditions. Never-the-less, this approach should work in many cases."

这篇关于将地理转换为几何SQL Server 2008R2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆