Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I poked around a little and this answer <a href="https://stackoverflow.com/questions/279983/how-can-i-convert-geometry-data-into-a-geography-data-in-ms-sql-server-2008">How can I convert Geometry data into a Geography data in MS SQL Server 2008?</a> which more or less just points to <a href="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" rel="nofollow noreferrer">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</a> leads me to a reasonable explanation and working code.</p> <p>The crux: You have to make sure your geometry can be translated to valid geography first.</p> <p>The code (which can certainly have some operations combined, but they are broken out here for clarity.)</p> <pre><code>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(); </code></pre> <p>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."</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload