Skip to content

Instantly share code, notes, and snippets.

@JamesWP
Created April 27, 2016 16:01
Show Gist options
  • Select an option

  • Save JamesWP/292493e95dbe503581254012994063f5 to your computer and use it in GitHub Desktop.

Select an option

Save JamesWP/292493e95dbe503581254012994063f5 to your computer and use it in GitHub Desktop.
GetNearestPoints Enumerate through nearest points in STRTree
namespace Ex{
class Example{
public static IEnumerable<Point> GetNearestPoints(STRtree<Point> tree, Point p)
{
var returnedSet = new HashSet<Point>();
returnedSet.Add(p);
var dist = new FilteredItemDistance(returnedSet);
while (true)
{
var nearest = tree.NearestNeighbour(p.Boundary.EnvelopeInternal, p, dist);
if (returnedSet.Contains(nearest)) break;
yield return nearest;
returnedSet.Add(nearest);
}
}
}
public class FilteredItemDistance : IItemDistance<Envelope, Point>
{
private ISet<Point> ignorePoints;
private IItemDistance<Envelope, IGeometry> D = new GeometryItemDistance();
public FilteredItemDistance(ISet<Point> ignorePoints)
{
this.ignorePoints = ignorePoints;
}
public double Distance(IBoundable<Envelope, Point> item1, IBoundable<Envelope, Point> item2)
{
if (ignorePoints.Contains(item1.Item)) return double.MaxValue;
return D.Distance(item1, item2);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment