0

I have a point (latitude,longitude) ex : 25,-80 and I'm looking for a way in c# to check if this point is in specific polygon.

I did some research and I found that containsLocation function contained within the Google Maps Geometry Library does exactly what I need but it is not available for c#. Here is an examples in which this method is utlized in JS:

// This example requires the Geometry library. Include the libraries=geometry
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry">

function initMap() {

        var triangleCoords = [
          {lat: 25.774, lng: -80.19},
          {lat: 18.466, lng: -66.118},
          {lat: 32.321, lng: -64.757}
        ];

        var coords = new google.maps.LatLng(25.774, -80.19);
        var bermudaTriangle = new google.maps.Polygon({paths: triangleCoords});

        var result = google.maps.geometry.poly.containsLocation(coords, bermudaTriangle);
        alert('The location exist in polygon: '+result);
}
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=geometry&callback=initMap"
         async defer></script>

I found a c# library for gmaps google maps API for C# but it does not support the function containsLocation Is there a way to do the required above in c#?

devedv
  • 562
  • 2
  • 15
  • 45

1 Answers1

4

Ray casting algorithm is commonly used to determine whether the given point is located within polygon. This answer contains the implementation in C#.

Also NetTopologySuite library could be utilized for that matter, in particular NetTopologySuite.Algorithm.Locate.IndexedPointInAreaLocator class:

var triangleCoords = new[] {
      new Coordinate(25.774, -80.19),
      new Coordinate(18.466, -66.118),
      new Coordinate(32.321, -64.757),
      new Coordinate(25.774, -80.19)
};
IGeometryFactory geometryFactory = new GeometryFactory();
var poly = geometryFactory.CreatePolygon(triangleCoords);


var locator = new NetTopologySuite.Algorithm.Locate.IndexedPointInAreaLocator(poly);
var location = locator.Locate(new Coordinate(24.886, -70.269));
if (location == GeoAPI.Geometries.Location.Interior)
{
     Console.WriteLine("Polygon contains the location");
}
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • It is not as accurate as containslocation method provided by google map API. On certain locations it is giving exception at CreatePolygon method "points must form a closed linestring" while GMAP API works fine for those coordinates. @VadimGremyachev – devedv Apr 26 '19 at 14:56
  • Forexample at var triangleCoords = new[] { new Coordinate(25.774, -80.19), new Coordinate(18.466, -66.118), new Coordinate(32.321, -64.757), new Coordinate(32.321, -62.757) }; checkLatLong = {20.8, -69.5} – devedv Apr 26 '19 at 15:00
  • @devedv, this exception has nothing to do with "accuracy", its because _startpoint and endpoint of a polygon must be equals_ – Vadim Gremyachev Apr 26 '19 at 15:00
  • Yes, it is working for the provided example but in my case, I always don't have accurate polygon. Can you suggest something for that? Thank you. – devedv Apr 26 '19 at 15:03
  • one option would be to ensure first and last item from array of coordinates are identical: say you have: `var triangleCoords = new[] { new Coordinate(25.774, -80.19),...}` then consider to append the last item like this: `triangleCoords = new List(triangleCoords).Add(triangleCoords[0]).ToArray();` – Vadim Gremyachev Apr 26 '19 at 15:11