Get Geography From Geometry - CSharp System

CSharp examples for System:Math Geometry

Description

Get Geography From Geometry

Demo Code


using System.Windows;
using Microsoft.SqlServer.Types;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/* w ww  .j  a  va2 s . c  o  m*/

public class Main{
        public static SqlGeography GetGeographyFromGeometry(SqlGeometry geom)
        {
            if (geom == null) return null;

            try
            {
                return SqlGeography.STGeomFromWKB(geom.STAsBinary(), SRID);
            }
            catch (Exception)
            {
                // A common reason for an exception being thrown is invalid ring orientation, 
                // so attempt to fix it. The technique used is described at
                // http://blogs.msdn.com/edkatibah/archive/2008/08/19/working-with-invalid-data-and-the-sql-server-2008-geography-data-type-part-1b.aspx

                return SqlGeography.STGeomFromWKB(
                    geom.MakeValid().Reduce(.000001).STUnion(geom.STStartPoint()).STAsBinary(), SRID);
            }
        }
}

Related Tutorials