2015-07-22 24 views
0

我想在沒有任何SQL數據庫的情況下使用DbGeography。 我有1多邊形DbGeography和1點DbGeography。DbGeography如何檢查點在多邊形內?

我嘗試使用polygon.Intersects(點)來檢查點是否在多邊形內,但相交總是返回true。

如何檢查點是否在多邊形內?

謝謝

編輯:

這裏是多邊形

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

,這裏是點

POINT (98.9505555555556 18.7505555555556) 
+0

你應該去: https://開頭計算器.COM /問題/ 1396 0878/check-if-dbgeometry-dbgeometry -dbgeography-point-is-within-a-polygon –

+0

你應該去:https://stackoverflow.com/questions/13960878/check-if-dbgeometry-dbgeometry-dbgeographypoint -is中之多邊形 –

回答

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