首页 > 解决方案 > 哪个 NetTopologySuite 索引最适合包含和排除矩形搜索

问题描述

我有以下问题:大量的点和大量的查询需要尽可能快地提供包含矩形(红色)和外部排除矩形(绿色)的任何点。由于点的数量很大并且矩形可以有各种大小,因此最好使用一些空间索引,但据我所知, NetTopologySuite.Index中的所有查询函数都返回包含所有结果的列表,我需要任何单个结果等等当带有许多点的大矩形出现时,查询太昂贵了。

我是否错过了一些可以解决我的问题的空间索引?或者一些快速解决问题的好方法?我可以使用访问者,但他们会针对每个节点进行评估,我可以在找到元素时使用异常来突破,但这看起来很难看。

可视化

标签: spatial-indexnettopologysuite

解决方案


IItemVisitor{T}您可以使用排除与绿色矩形相交的项目的自定义查询索引。沿着这些思路:

/// <summary>
/// Item visitor that specifically excludes a predefined area.
/// </summary>
/// <typeparam name="T">The type of the items to visit</typeparam>
public class ExcludingItemVisitor<T> : IItemVisitor<T> where T:Geometry
{
    private readonly Envelope _exclude;
    private readonly List<T> _items = new List<T>();

    /// <summary>
    /// Initialize with <paramref name="exclude"/>
    /// </summary>
    /// <param name="exclude"></param>
    public ExcludingItemVisitor(Envelope exclude)
    {
        _exclude = exclude;
    }

    /// <inheritdoc cref="IItemVisitor{T}.VisitItem"/>>
    public void VisitItem(T item)
    {
        // If we have no intersection with _exclude, add it.
        if (!_exclude.Intersects(item.EnvelopeInternal))
            _items.Add(item);
    }

    /// <summary>
    /// Get a value indicating the gathered items
    /// </summary>
    public IList<T> Items => _items;
}

推荐阅读