using System; using System.Collections; namespace GisSharpBlog.NetTopologySuite.Index.Strtree { /// /// One-dimensional version of an STR-packed R-tree. SIR stands for /// "Sort-Interval-Recursive". STR-packed R-trees are described in: /// P. Rigaux, Michel Scholl and Agnes Voisard. Spatial Databases With /// Application To GIS. Morgan Kaufmann, San Francisco, 2002. /// public class SIRtree : AbstractSTRtree { private readonly IComparer comparator = new AnnonymousComparerImpl(); private readonly IIntersectsOp intersectsOp = new AnonymousIntersectsOpImpl(); /// /// Constructs an SIRtree with the default (10) node capacity. /// public SIRtree() : this(10) {} /// /// Constructs an SIRtree with the given maximum number of child nodes that /// a node may have. /// public SIRtree(int nodeCapacity) : base(nodeCapacity) {} /// /// Inserts an item having the given bounds into the tree. /// /// /// /// public void Insert(double x1, double x2, object item) { base.Insert(new Interval(Math.Min(x1, x2), Math.Max(x1, x2)), item); } /// /// Returns items whose bounds intersect the given value. /// /// public IList Query(double x) { return Query(x, x); } /// /// Returns items whose bounds intersect the given bounds. /// /// Possibly equal to x2. /// Possibly equal to x1. public IList Query(double x1, double x2) { return base.Query(new Interval(Math.Min(x1, x2), Math.Max(x1, x2))); } /// /// /// protected override IIntersectsOp IntersectsOp { get { return intersectsOp; } } /// /// /// /// /// protected override AbstractNode CreateNode(int level) { return new AnonymousAbstractNodeImpl(level); } /// /// /// /// protected override IComparer GetComparer() { return comparator; } /// /// /// private class AnnonymousComparerImpl : IComparer { /// /// /// /// /// /// public int Compare(object o1, object o2) { return new SIRtree().CompareDoubles(((Interval) ((IBoundable) o1).Bounds).Centre, ((Interval) ((IBoundable) o2).Bounds).Centre); } } /// /// /// private class AnonymousAbstractNodeImpl : AbstractNode { /// /// /// /// public AnonymousAbstractNodeImpl(int nodeCapacity) : base(nodeCapacity) {} /// /// /// /// protected override object ComputeBounds() { Interval bounds = null; for (IEnumerator i = ChildBoundables.GetEnumerator(); i.MoveNext();) { IBoundable childBoundable = (IBoundable) i.Current; if (bounds == null) { bounds = new Interval((Interval) childBoundable.Bounds); } else { bounds.ExpandToInclude((Interval) childBoundable.Bounds); } } return bounds; } } /// /// /// private class AnonymousIntersectsOpImpl : IIntersectsOp { /// /// /// /// /// /// public bool Intersects(object aBounds, object bBounds) { return ((Interval) aBounds).Intersects((Interval) bBounds); } } } }