Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DbGeometry makevalid?

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

like image 886
Thomas Bolander Avatar asked Nov 05 '12 12:11

Thomas Bolander


2 Answers

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);
}
like image 166
Behr Avatar answered Oct 19 '22 17:10

Behr


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);
}

like image 42
Shabgard Avatar answered Oct 19 '22 19:10

Shabgard