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
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);
}
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With