35 lines
1.1 KiB
C#
35 lines
1.1 KiB
C#
using GeoAPI.Geometries;
|
|
using NetTopologySuite.Geometries;
|
|
using WorkerService1.Domains;
|
|
using Polygon = WorkerService1.Domains.Polygon;
|
|
|
|
namespace WorkerService1.Utils;
|
|
|
|
public static class Utilities
|
|
{
|
|
public static bool IsPointInsidePolygon(this Points point, Polygon polygon)
|
|
{
|
|
if (polygon.Points == null) return false;
|
|
var testPoint = new Coordinate(point.X, point.Y);
|
|
foreach (var points in polygon.Points)
|
|
{
|
|
var polygonPoints = points.Select(x => new Coordinate(x.X, x.Y)).ToArray();
|
|
var isInside = IsPointInsidePolygon(testPoint, polygonPoints);
|
|
if (isInside) return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
public static bool IsPointInsidePolygon(Coordinate testPoint, Coordinate[] polygonPoints)
|
|
{
|
|
var geometryFactory = new GeometryFactory();
|
|
var testPointGeom = geometryFactory.CreatePoint(testPoint);
|
|
|
|
var linearRing = new LinearRing(polygonPoints.Append(polygonPoints.First()).ToArray());
|
|
var polygon = geometryFactory.CreatePolygon(linearRing);
|
|
|
|
return polygon.Intersects(testPointGeom);
|
|
}
|
|
} |