1

I am storing some location data in Azure CosmosDB as following:

    {
       ...
       "Polygon": {
           "type": "Polygon",
           "coordinates": [
           [
            -72.2811341,
            42.9275741
           ],
           [
            -72.2816062,
            42.9256955
           ],
           [
            -72.2780764,
            42.9261986
           ],
           [
            -72.2811341,
            42.9275741
           ]
         ]
       },
       ...
    }

I want to check if a particular Point is inside this polygon, but the polygon can have multiple rings inside it. I am using the following code to check if a point is inside a single ring polygon:

     private bool IsPointInPolygon(Point p, Position[] polygon)
      {
        double minX = polygon[0].Latitude;
        double maxX = polygon[0].Latitude;
        double minY = polygon[0].Longitude;
        double maxY = polygon[0].Longitude;
        for (int i = 1; i < polygon.Length; i++)
        {
          Position q = polygon[i];
          minX = Math.Min(q.Latitude, minX);
          maxX = Math.Max(q.Latitude, maxX);
          minY = Math.Min(q.Longitude, minY);
          maxY = Math.Max(q.Longitude, maxY);
        }
        if (p.Position.Latitude < minX || p.Position.Latitude > maxX || p.Position.Longitude < minY || p.Position.Longitude > maxY)
        {
          return false;
        }
        bool inside = false;
        for (int i = 0, j = polygon.Length - 1; i < polygon.Length; j = i++)
        {
          if ((polygon[i].Longitude > p.Position.Longitude) != (polygon[j].Longitude > p.Position.Longitude) &&
               p.Position.Latitude < (polygon[j].Latitude - polygon[i].Latitude) * (p.Position.Longitude - polygon[i].Longitude) / (polygon[j].Longitude - polygon[i].Longitude) + polygon[i].Latitude)
          {
            inside = !inside;
          }
        }
        return inside;
      }

Is there a way that I can modify this method to check for multiple rings in polygon which means the first ring is the outer ring and the others basically exclude areas inside this outer ring?

David Makogon
  • 69,407
  • 21
  • 141
  • 189
Muhamed Krasniqi
  • 1,134
  • 1
  • 13
  • 20

0 Answers0