Trouble with Geometry -> Geography RRS feed

  • Question

  • Hello, I have this shape which I can not seem to convert to a Geography. There are a few 1000 points, so here is a link to the code:




    What kind of operations can I perform to make the geography valid? Here is the error I'm getting:


    Msg 6522, Level 16, State 1, Line 7

    A .NET Framework error occurred during execution of user-defined routine or aggregate "geography": 

    System.ArgumentException: 24200: The specified input does not represent a valid geography instance.


       at Microsoft.SqlServer.Types.SqlGeography.ConstructGeographyFromUserInput(GeoData g, Int32 srid)

       at Microsoft.SqlServer.Types.SqlGeography.GeographyFromBinary(OpenGisType type, SqlBytes binary, Int32 srid)

       at Microsoft.SqlServer.Types.SqlGeography.STGeomFromWKB(SqlBytes wkbGeometry, Int32 srid)


    Thursday, October 28, 2010 1:47 PM


  • I would guess it's self-intersecting. You can fix that by Reduce()-ing the geometry a fractional amount before "converting" to geography:

    DECLARE @Geometry as Geometry
    SET @Geometry = Geometry::STGeomFromText('MULTIPOLYGON (((-80.821928 40.904599, -80.821933 40.906807, -80.821936 40.908276... etc. etc.', 1)
    SET @Geometry = @Geometry.Reduce(0.0000000000000000001).MakeValid()
    DECLARE @Geography as Geography
    SET @Geography = Geography::STGeomFromWKB(@Geometry.STAsBinary(), 4326)
    SELECT @Geography

    Beginning Spatial with SQL Server http://www.apress.com/book/view/1430218290
    • Marked as answer by Kbalz Thursday, October 28, 2010 3:58 PM
    Thursday, October 28, 2010 2:37 PM