Coverage Report

Created: 2022-08-24 06:40

/src/geos/include/geos/index/strtree/STRtree.h
Line
Count
Source (jump to first uncovered line)
1
/**********************************************************************
2
 *
3
 * GEOS - Geometry Engine Open Source
4
 * http://geos.osgeo.org
5
 *
6
 * Copyright (C) 2006 Refractions Research Inc.
7
 *
8
 * This is free software; you can redistribute and/or modify it under
9
 * the terms of the GNU Lesser General Public Licence as published
10
 * by the Free Software Foundation.
11
 * See the COPYING file for more information.
12
 *
13
 **********************************************************************
14
 *
15
 * Last port: index/strtree/STRtree.java rev. 1.11
16
 *
17
 **********************************************************************/
18
19
#pragma once
20
21
#include <geos/export.h>
22
#include <geos/index/strtree/ItemDistance.h>
23
#include <geos/index/strtree/BoundablePair.h>
24
#include <geos/index/strtree/AbstractSTRtree.h> // for inheritance
25
#include <geos/index/SpatialIndex.h> // for inheritance
26
#include <geos/geom/Envelope.h> // for inlines
27
28
#include <vector>
29
30
#ifdef _MSC_VER
31
#pragma warning(push)
32
#pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
33
#endif
34
35
// Forward declarations
36
namespace geos {
37
namespace index {
38
namespace strtree {
39
class Boundable;
40
}
41
}
42
}
43
44
namespace geos {
45
namespace index { // geos::index
46
namespace strtree { // geos::index::strtree
47
48
/**
49
 * \brief
50
 * A query-only R-tree created using the Sort-Tile-Recursive (STR) algorithm.
51
 * For two-dimensional spatial data.
52
 *
53
 * The STR packed R-tree is simple to implement and maximizes space
54
 * utilization; that is, as many leaves as possible are filled to capacity.
55
 * Overlap between nodes is far less than in a basic R-tree. However, once the
56
 * tree has been built (explicitly or on the first call to #query), items may
57
 * not be added or removed.
58
 *
59
 * Described in: P. Rigaux, Michel Scholl and Agnes Voisard. Spatial
60
 * Databases With Application To GIS. Morgan Kaufmann, San Francisco, 2002.
61
 *
62
 */
63
class GEOS_DLL STRtree: public AbstractSTRtree, public SpatialIndex {
64
    using AbstractSTRtree::insert;
65
    using AbstractSTRtree::query;
66
67
private:
68
    class GEOS_DLL STRIntersectsOp: public AbstractSTRtree::IntersectsOp {
69
    public:
70
        bool intersects(const void* aBounds, const void* bBounds) override;
71
    };
72
73
    /**
74
     * Creates the parent level for the given child level. First, orders the items
75
     * by the x-values of the midpoints, and groups them into vertical slices.
76
     * For each slice, orders the items by the y-values of the midpoints, and
77
     * group them into runs of size M (the node capacity). For each run, creates
78
     * a new (parent) node.
79
     */
80
    std::unique_ptr<BoundableList> createParentBoundables(BoundableList* childBoundables, int newLevel) override;
81
82
    std::unique_ptr<BoundableList> createParentBoundablesFromVerticalSlices(std::vector<BoundableList*>* verticalSlices,
83
            int newLevel);
84
85
    STRIntersectsOp intersectsOp;
86
87
    std::unique_ptr<BoundableList> sortBoundablesY(const BoundableList* input);
88
    std::unique_ptr<BoundableList> sortBoundablesX(const BoundableList* input);
89
90
    std::unique_ptr<BoundableList> createParentBoundablesFromVerticalSlice(
91
        BoundableList* childBoundables,
92
        int newLevel);
93
94
    /**
95
     * @param childBoundables Must be sorted by the x-value of
96
     *        the envelope midpoints
97
     * @return
98
     */
99
    std::vector<BoundableList*>* verticalSlices(
100
        BoundableList* childBoundables,
101
        std::size_t sliceCount);
102
103
    bool isWithinDistance(BoundablePair* initBndPair, double maxDistance);
104
105
protected:
106
107
    AbstractNode* createNode(int level) override;
108
109
    IntersectsOp*
110
    getIntersectsOp() override
111
0
    {
112
0
        return &intersectsOp;
113
0
    }
114
115
public:
116
117
    ~STRtree() override = default;
118
119
    /**
120
     * Constructs an STRtree with the given maximum number of child nodes that
121
     * a node may have
122
     */
123
    STRtree(std::size_t nodeCapacity = 10);
124
125
    void insert(const geom::Envelope* itemEnv, void* item) override;
126
127
    //static double centreX(const geom::Envelope *e);
128
129
    static double
130
    avg(double a, double b)
131
0
    {
132
0
        return (a + b) / 2.0;
133
0
    }
134
135
    static double
136
    centreY(const geom::Envelope* e)
137
0
    {
138
0
        return STRtree::avg(e->getMinY(), e->getMaxY());
139
0
    }
140
141
    void
142
    query(const geom::Envelope* searchEnv, std::vector<void*>& matches) override
143
0
    {
144
0
        AbstractSTRtree::query(searchEnv, matches);
145
0
    }
146
147
    void
148
    query(const geom::Envelope* searchEnv, ItemVisitor& visitor) override
149
0
    {
150
0
        return AbstractSTRtree::query(searchEnv, visitor);
151
0
    }
152
153
    std::pair<const void*, const void*> nearestNeighbour(ItemDistance* itemDist);
154
    const void* nearestNeighbour(const geom::Envelope* env, const void* item, ItemDistance* itemDist);
155
    std::pair<const void*, const void*> nearestNeighbour(STRtree* tree, ItemDistance* itemDist);
156
    std::pair<const void*, const void*> nearestNeighbour(BoundablePair* initBndPair);
157
    std::pair<const void*, const void*> nearestNeighbour(BoundablePair* initBndPair, double maxDistance);
158
159
    bool
160
    remove(const geom::Envelope* itemEnv, void* item) override
161
0
    {
162
0
        return AbstractSTRtree::remove(itemEnv, item);
163
0
    }
164
165
    bool isWithinDistance(STRtree* tree, ItemDistance* itemDist, double maxDistance);
166
167
};
168
169
} // namespace geos::index::strtree
170
} // namespace geos::index
171
} // namespace geos
172
173
174
#ifdef _MSC_VER
175
#pragma warning(pop)
176
#endif
177