3

I am trying to use the Net Topology Suite (v 1.13.2) in a simple console app to find points in a shape file that contains road information.

I have loaded the shape file and stored the data in a typed list, and I have checked that the data looks as expected..

When I iterate around the list I want to find if a given point is within any of the shapes in the shape file.

Sounds straightforward!

my code looks like;

    private static Feature FindPoint(double lat, double lon)
    {
        Coordinate c = new Coordinate(lat, lon);

        IGeometry g = factory.CreateGeometry(Geometry.DefaultFactory.CreatePoint(c));

        foreach(Feature f in Features)
        {
            if (f.Geometry.Overlaps(g))
                return f;
            if (f.Geometry.EnvelopeInternal.Contains(c))
                return f;
            if (f.Geometry.Boundary.Contains(g))
                return f;
            if (f.Geometry.Contains(g))
                return f;
        }
        return null;
    }

None of these statements, or any of the others I have tried return any indication that the point is within any of the shapes in.

I am trying with a Lat and Long I picked from one of the shapes in the file! SO it should be there.

Any ideas as to where I am going wrong?

@Habib, I have checked and the geometry type is LineString, but the Envelope is defined as a polygon.

I have looked at the link you sent, new code below;

DistanceOp dop = new DistanceOp(f.Geometry,g);
var np = dop.NearestPoints();
var d = dop.Distance();

Tried to do as indicated there, but the answer I get for the distance between the point I have chosen and the line is 71.236662957979718.

71.236662957979718 what? cm, metres, degrees???

Strange anyway, as I have picked a point on one of the lines, so I would expect an answer of 0.

AndrewS
  • 369
  • 5
  • 19
  • What is the type of geometries in your Shapefiles ? Your code should work if the shapes are polygons, Otherwise you will need to find the "nearest geometry". See: https://code.google.com/p/nettopologysuite/source/browse/trunk/NetTopologySuite.Samples.Console/Operation/Distance/ClosestPointExample.cs?r=763 – Habib Sep 17 '14 at 16:07

1 Answers1

1

Putting the longitude before the latitude while creating the coordinate got it working for me.

Corrected code:

private static Feature FindPoint(double lat, double lon)
{
    Coordinate c = new Coordinate(lon, lat);
    Geometry g = Geometry.DefaultFactory.CreatePoint(c);

    foreach(Feature f in Features)
    {
        if (f.Geometry.Contains(g))
            return f;
    }
    return null;
}

I'm using NetTopologySuite.IO.Esri found here: https://github.com/NetTopologySuite/NetTopologySuite.IO.Esri.

RyanB
  • 37
  • 7