0

I am trying to use DbGeography without any SQL database. I have 1 polygon DbGeography and 1 point DbGeography.

I try to use polygon.Intersects(point) to check if the point is inside polygon but Intersects always return true.

How can I check a point is inside a polygon ?

Thank you

EDIT:

here is polygon

POLYGON((103.000833333333 14.2038888888889,103.000833333333 17.9880555555556,103.0675 18.1216666666667,103.300833333333 18.4216666666667,103.4175 18.4383333333333,103.9675 18.3216666666667,104.334444444444 17.8213888888889,104.784444444444 17.4047222222222,104.767777777778 16.6877777777778,105.001388888889 16.2544444444444,105.418055555556 16.0044444444444,105.634722222222 15.6708333333333,105.584722222222 15.0041666666667,105.534722222222 14.5538888888889,105.068055555556 14.2205555555556,104.467777777778 14.3538888888889,103.9175 14.3372222222222,103.3675 14.3538888888889,103.000833333333 14.2038888888889))

and here is point

POINT (98.9505555555556 18.7505555555556)
dokibi
  • 43
  • 8
  • You should go to : https://stackoverflow.com/questions/13960878/check-if-dbgeometry-dbgeometry-dbgeography-point-is-within-a-polygon – Benoit Glaizette Jun 22 '17 at 10:50
  • You should go to : https://stackoverflow.com/questions/13960878/check-if-dbgeometry-dbgeometry-dbgeography-point-is-within-a-polygon – Benoit Glaizette Jun 22 '17 at 10:52

1 Answers1

0
public void CheckInPolygon()
{
    bool inPolygon;

    PointF[] pts = new PointF[] { new PointF { X = 14.2038888888889f, Y = 103.000833333333f }, 
                                    new PointF { X = 17.9880555555556f, Y = 103.000833333333f }, 
                                    new PointF { X = 18.1216666666667f, Y = 103.0675f }, 
                                    new PointF { X = 18.4216666666667f, Y = 103.300833333333f } , 
                                    new PointF { X = 18.4383333333333f, Y = 103.4175f } , 
                                    new PointF { X = 18.3216666666667f, Y = 103.9675f } , 
                                    new PointF { X = 17.8213888888889f, Y = 104.334444444444f } , 
                                    new PointF { X = 17.4047222222222f, Y = 104.784444444444f } , 
                                    new PointF { X = 16.6877777777778f, Y = 104.767777777778f } , 
                                    new PointF { X = 16.2544444444444f, Y = 105.001388888889f } , 
                                    new PointF { X = 16.0044444444444f, Y = 105.418055555556f } , 
                                    new PointF { X = 15.6708333333333f, Y = 105.634722222222f } , 
                                    new PointF { X = 15.0041666666667f, Y = 105.584722222222f } , 
                                    new PointF { X = 14.5538888888889f, Y = 105.534722222222f }, 
                                    new PointF { X = 14.2205555555556f, Y = 105.068055555556f }, 
                                    new PointF { X = 14.3538888888889f, Y = 104.467777777778f }, 
                                    new PointF { X = 14.3372222222222f, Y = 103.9175f }, 
                                    new PointF { X = 14.3538888888889f, Y = 103.3675f }, 
                                    new PointF { X = 14.2038888888889f, Y = 103.000833333333f } };      //POLYGON as provided

    inPolygon = IsInPolygon(pts, new PointF { X = 18.7505555555556f, Y = 98.9505555555556f });          //POINT as provided

    MessageBox.Show(inPolygon.ToString());      //return : false
}

public static bool IsInPolygon(PointF[] poly, PointF point)
{
    var coef = poly.Skip(1).Select((p, i) =>
                                    (point.Y - poly[i].Y) * (p.X - poly[i].X)
                                  - (point.X - poly[i].X) * (p.Y - poly[i].Y))
                                .ToList();
    if (coef.Any(p => p == 0))
        return true;

    for (int i = 1; i < coef.Count(); i++)
    {
        if (coef[i] * coef[i - 1] < 0)
            return false;
    }
    return true;
}
Harlo
  • 507
  • 2
  • 12