I'm using the .NET NTS library and have the code together to load a KML file. This gives me an instance of IGeometry.

Along with this data being loaded, I also have coordinates that I'd like to look up against it to see what shapes are at that point (usually will only be one). After getting the shape(s), the KML I have also includes some additional feature information which I'd then like to be able to get the values from.

My understanding of these APIs is somewhat limited and I haven't been able to find any comprehensive examples of what I need to do once I have the data loaded.

var kml = await stream.ReadToEndAsync();
var reader = new GMLReader();

geometry = reader.Read(kml);

// ...now what?

In your case, you can test the 'intersection' between the shape and the point like this:

IGeometry shape = ...;
IPoint point = ...;
if (shape.Intersects(point))
    // Do something      

Or maybe you could use 'within' instead or 'intersects' to test if the point is whithin the shape but not in the boundary.

Read about the DE-9IM topological model, that is used by most of GIS libraries, to know about the spatial predicate (or custom matrix) you need in each situation.


For the case when you have hundreds or thousands of polygons to test, you should use an spatial index.

List<IGeometry> shapes = ...;
IPoint point = ...;

// Builds the index
STRtree<IGeometry> index = new STRtree<IGeometry>();
foreach (IGeometry shape in shapes)
    index.Insert(shape.EnvelopeInternal, shape);

// Makes the query. The 'nearShapes' are the shapes whose envelopes contains the point.
IList<IGeometry> nearShapes = index.Query(point.EnvelopeInternal);
// Now the app only have to test the intersection against a few shapes.
foreach (IGeometry shape in nearShapes)
    if (shape.Intersects(point))
        // Do whatever
  • My data is made up of multiple sub shapes. Is there any way to get the correct one based on the point? This situation would have me enumerating through all of them, which seems unoptimized. – Omega May 2 '17 at 2:42
  • Added an example in case you have lots of polygons to test. – César Argul García May 2 '17 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.