8

I'm new to this. I don't know how to use the SqlSpatialFunction MakeValid. I have a DbGeometry which is a polygon. This polygon is not valid and I want to make it valid.

Can anyone explain how to use the MakeValid method?

MSDN

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Thomas Bolander
  • 3,892
  • 3
  • 22
  • 30
  • 2
    In the query you would use `SqlSpatialFunctions.MakeValid(dbGeometryValue)`. If this does not work for you be more specific what you want to achieve and show what you have tried. – Pawel Nov 08 '12 at 03:56
  • 2
    See this question the last answer may help you http://stackoverflow.com/questions/16640565/is-there-something-like-dbgeometry-makevalid-in-net-4-5/16757876#16757876 – Khalid Omar May 26 '13 at 09:28

2 Answers2

7

Going off of what Pawel commented, All I do is check to see if it is valid then make it valid if it isn't.

DbGeometry myGeometry = DbGeometry.FromText("POLYGON ((10 10, 15 15, 5 15, 10 15, 10 10))");
if(!myGeometry.IsValid)
{
    myGeometry = SqlSpatialFunctions.MakeValid(myGeometry);
}
Behr
  • 1,220
  • 18
  • 24
2

You can't call SqlSpatialFunctions.MakeValid on DbGeometry, and if you do, it will throw the following exception:

System.NotSupportedException : This function can only be invoked from LINQ to Entities.

Therefore, what you can do is to make it valid using its WKT string and then convert that valid string toa DBGeometry like this:


public static DbGeometry MakeValid(DbGeometry geom)
{
     if (geom.IsValid)
          return geom;

     var wkt = SqlGeometry.STGeomFromText(new SqlChars(geom.AsText()), 0).MakeValid().STAsText().ToSqlString().ToString();
     return DbGeometry.FromText(wkt, 0);
}

Shabgard
  • 103
  • 9