Search code examples
c#asp.net-core-2.2nettopologysuite

Fast find if points belong to polygon (NetTopologySuite.Geometries, C#. .net core 2.2)


Is there an efficient way to find points of type NetTopologySuite.Geometries.Point that are inside given polygon of type NetTopologySuite.Geometries.PointPolygon using .net core 2.2. I tried following the documentation below, but no luck: https://nettopologysuite.github.io/html/class_net_topology_suite_1_1_algorithm_1_1_locate_1_1_indexed_point_in_area_locator.html#ad28b305b77c52327b7787ca8016c0fd7.


Solution

  • For one against many predicate tests use the GeoAPI.Geometries.Prepared.IPreparedGeometry. You can create one using the NetTopologySuite.Geometries.Prepared.PreparedGeometryFactory.

    private IList<IPoint> Contains(IGeometry geom, IEnumerable<IPoint> points) {
        var prepGeom = new NetTopologySuite.Geometries.Prepared.PreparedGeometryFactory().Prepare(geom);
        var res = new List<IPoint>();
        foreach(var point in points) {
            if (prepGeom.Contains(point)) res.Add(point);
        }
        return res;
    }
    

    See issue on GitHub