Geo Fencing - point inside/outside polygon

后端 未结 16 1642
我在风中等你
我在风中等你 2020-11-28 02:15

I would like to determine a polygon and implement an algorithm which would check if a point is inside or outside the polygon.

Does anyone know if there is any exampl

相关标签:
16条回答
  • 2020-11-28 02:59

    You can try this simple class https://github.com/xopbatgh/sb-polygon-pointer

    It is easy to deal with it

    1. You just insert polygon coordinates into array
    2. Ask library is desired point with lat/lng inside the polygon
    $polygonBox = [
        [55.761515, 37.600375],
        [55.759428, 37.651156],
        [55.737112, 37.649566],
        [55.737649, 37.597301],
    ];
    
    $sbPolygonEngine = new sbPolygonEngine($polygonBox);
    
    $isCrosses = $sbPolygonEngine->isCrossesWith(55.746768, 37.625605);
    
    // $isCrosses is boolean
    
    

    (answer was returned from deleted by myself because initially it was formatted wrong)

    0 讨论(0)
  • 2020-11-28 03:01

    I think there is a simpler and more efficient solution.

    Here is the code in C++. I should be simple to convert it to C#.

    int pnpoly(int npol, float *xp, float *yp, float x, float y)
    {
      int i, j, c = 0;
      for (i = 0, j = npol-1; i < npol; j = i++) {
        if ((((yp[i] <= y) && (y < yp[j])) ||
             ((yp[j] <= y) && (y < yp[i]))) &&
            (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))
          c = !c;
      }
      return c;
    }
    
    0 讨论(0)
  • 2020-11-28 03:02

    The complete solution in asp.Net C#, you can see the complete detail here, you can see how to find point(lat,lon) whether its inside or Outside the Polygon using the latitude and longitudes ? Article Reference Link

    private static bool checkPointExistsInGeofencePolygon(string latlnglist, string lat, string lng) {

        List<Loc> objList = new List<Loc>();
        // sample string should be like this strlatlng = "39.11495,-76.873259|39.114588,-76.872808|39.112921,-76.870373|";
        string[] arr = latlnglist.Split('|');
        for (int i = 0; i <= arr.Length - 1; i++)
        {
            string latlng = arr[i];
            string[] arrlatlng = latlng.Split(',');
    
            Loc er = new Loc(Convert.ToDouble(arrlatlng[0]), Convert.ToDouble(arrlatlng[1]));
            objList.Add(er);
        }
        Loc pt = new Loc(Convert.ToDouble(lat), Convert.ToDouble(lng));
    
        if (IsPointInPolygon(objList, pt) == true)
        {
              return true;
        }
        else
        {
               return false;
        }
    }
    private static bool IsPointInPolygon(List<Loc> poly, Loc point)
    {
        int i, j;
        bool c = false;
        for (i = 0, j = poly.Count - 1; i < poly.Count; j = i++)
        {
            if ((((poly[i].Lt <= point.Lt) && (point.Lt < poly[j].Lt)) |
                ((poly[j].Lt <= point.Lt) && (point.Lt < poly[i].Lt))) &&
                (point.Lg < (poly[j].Lg - poly[i].Lg) * (point.Lt - poly[i].Lt) / (poly[j].Lt - poly[i].Lt) + poly[i].Lg))
                c = !c;
        }
        return c;
    }
    
    0 讨论(0)
  • 2020-11-28 03:09

    Just a heads up (using answer as I can't comment), if you want to use point-in-polygon for geo fencing, then you need to change your algorithm to work with spherical coordinates. -180 longitude is the same as 180 longitude and point-in-polygon will break in such situation.

    0 讨论(0)
提交回复
热议问题