Search code examples
c#wkt

DbGeometry Contains() works only for Polygon WKT


I tried the following code that behaves differently from what I expected. The DbGeometry.FromText is supposed to create an object from WKT. However, the contains function only seems to work on Polygon WKT and not for circles or linestrings (I've drawn a diamond in the example). In these examples, all the geometries "contain" 0,0 but only polygon actually results the value I expected. Any idea of what's going on? Am I missing something in code or am I missing some theory about how DbGeometry works?

  DbGeometry point = DbGeometry.FromText("POINT (0 0)");

  DbGeometry circle = DbGeometry.FromText("CIRCULARSTRING(0 -1, 1 0, 0 1, -1 0, 0 -1)");
  Console.WriteLine(circle.Contains(point)); // returns false

  var diamond = DbGeometry.FromText("LINESTRING(0 -1, 1 0, 0 1, -1 0, 0 -1)");
  Console.WriteLine(diamond.Contains(point)); //returns false

  var polygon = DbGeometry.FromText("POLYGON((-1 -1, -1 1, 1 1, 1 -1, -1 -1))");
  Console.WriteLine(polygon.Contains(point)); //returns true

Solution

  • The example circle doesn't contain point since it is a string and not a polygon. (However, something like POINT(0 -1) is on the example circlestring). Just because the circularstring is closed, doesn't make it a polygon.

    I think what you want is CURVEPOLYGON(CIRCULARSTRING(0 -1, 1 0, 0 1, -1 0, 0 -1)), which does contain POINT(0 0).

    Lastly, not all implementations support curves, so you may need to use STCurveToLine to get a polygonal approximation of a geometry instance that contains curved segments.