17#include <geos/geom/Geometry.h>
18#include <geos/index/SpatialIndex.h>
19#include <geos/index/chain/MonotoneChain.h>
20#include <geos/index/ItemVisitor.h>
23#include <geos/index/strtree/TemplateSTRNode.h>
24#include <geos/index/strtree/TemplateSTRNodePair.h>
25#include <geos/index/strtree/TemplateSTRtreeDistance.h>
26#include <geos/index/strtree/Interval.h>
56template<
typename ItemType,
typename BoundsTraits>
59 using Node = TemplateSTRNode<ItemType, BoundsTraits>;
60 using NodeList = std::vector<Node>;
61 using NodeListIterator =
typename NodeList::iterator;
62 using BoundsType =
typename BoundsTraits::BoundsType;
66 using iterator_category = std::forward_iterator_tag;
67 using value_type = ItemType;
68 using difference_type =
typename NodeList::const_iterator::difference_type;
69 using pointer = ItemType*;
70 using reference = ItemType&;
72 Iterator(
typename NodeList::const_iterator&& iter,
73 typename NodeList::const_iterator&& end) : m_iter(iter), m_end(end) {
77 const ItemType& operator*()
const {
78 return m_iter->getItem();
81 Iterator& operator++() {
87 friend bool operator==(
const Iterator& a,
const Iterator& b) {
88 return a.m_iter == b.m_iter;
91 friend bool operator!=(
const Iterator& a,
const Iterator& b) {
92 return a.m_iter != b.m_iter;
97 while(m_iter != m_end && m_iter->isDeleted()) {
102 typename NodeList::const_iterator m_iter;
103 typename NodeList::const_iterator m_end;
111 return Iterator(m_tree.nodes.cbegin(),
112 std::next(m_tree.nodes.cbegin(),
static_cast<long>(m_tree.numItems)));
116 return Iterator(std::next(m_tree.nodes.cbegin(),
static_cast<long>(m_tree.numItems)),
117 std::next(m_tree.nodes.cbegin(),
static_cast<long>(m_tree.numItems)));
132 nodeCapacity(p_nodeCapacity),
143 nodeCapacity(p_nodeCapacity),
145 auto finalSize = treeSize(itemCapacity);
146 nodes.reserve(finalSize);
154 nodeCapacity(other.nodeCapacity),
155 numItems(other.numItems) {
162 nodeCapacity = other.nodeCapacity;
163 numItems = other.numItems;
174 insert(BoundsTraits::fromItem(item), std::forward<ItemType>(item));
179 insert(BoundsTraits::fromItem(item), item);
183 void insert(
const BoundsType& itemEnv, ItemType&& item) {
184 if (!BoundsTraits::isNull(itemEnv)) {
185 createLeafNode(std::forward<ItemType>(item), itemEnv);
190 void insert(
const BoundsType& itemEnv,
const ItemType& item) {
191 if (!BoundsTraits::isNull(itemEnv)) {
192 createLeafNode(item, itemEnv);
201 template<
typename ItemDistance>
207 template<
typename ItemDistance>
214 template<
typename ItemDistance>
218 return {
nullptr,
nullptr };
221 TemplateSTRtreeDistance<ItemType, BoundsTraits, ItemDistance> td(distance);
222 return td.nearestNeighbour(*root, *other.root);
226 template<
typename ItemDistance>
232 template<
typename ItemDistance>
240 TemplateSTRNode<ItemType, BoundsTraits> bnd(item, env);
241 TemplateSTRNodePair<ItemType, BoundsTraits, ItemDistance> pair(*
getRoot(), bnd, itemDist);
243 TemplateSTRtreeDistance<ItemType, BoundsTraits, ItemDistance> td(itemDist);
244 return td.nearestNeighbour(pair).first;
247 template<
typename ItemDistance>
262 template<
typename Visitor>
263 void query(
const BoundsType& queryEnv, Visitor &&visitor) {
268 if (root && root->boundsIntersect(queryEnv)) {
269 if (root->isLeaf()) {
270 visitLeaf(visitor, *root);
272 query(queryEnv, *root, visitor);
278 void query(
const BoundsType& queryEnv, std::vector<ItemType>& results) {
279 query(queryEnv, [&results](
const ItemType& x) {
280 results.push_back(x);
298 auto n =
built() ? numItems : nodes.size();
299 for (
size_t i = 0; i < n; i++) {
300 func(nodes[i].getItem());
308 bool remove(
const BoundsType& itemEnv,
const ItemType& item) {
311 if (root ==
nullptr) {
315 if (root->isLeaf()) {
316 if (!root->isDeleted() && root->getItem() == item) {
323 return remove(itemEnv, *root, item);
332 return root !=
nullptr;
345 std::lock_guard<std::mutex> lock(lock_);
355 numItems = nodes.size();
359 auto finalSize = treeSize(numItems);
360 nodes.reserve(finalSize);
363 auto begin = nodes.begin();
364 auto number =
static_cast<size_t>(std::distance(begin, nodes.end()));
367 createParentNodes(begin, number);
368 std::advance(begin,
static_cast<long>(number));
369 number =
static_cast<size_t>(std::distance(begin, nodes.end()));
372 assert(finalSize == nodes.size());
374 root = &nodes.back();
387 void createLeafNode(ItemType&& item,
const BoundsType& env) {
388 nodes.emplace_back(std::forward<ItemType>(item), env);
391 void createLeafNode(
const ItemType& item,
const BoundsType& env) {
392 nodes.emplace_back(item, env);
395 void createBranchNode(
const Node *begin,
const Node *end) {
396 assert(nodes.size() < nodes.capacity());
397 nodes.emplace_back(begin, end);
402 size_t treeSize(
size_t numLeafNodes) {
403 size_t nodesInTree = numLeafNodes;
405 size_t nodesWithoutParents = numLeafNodes;
406 while (nodesWithoutParents > 1) {
407 auto numSlices = sliceCount(nodesWithoutParents);
408 auto nodesPerSlice = sliceCapacity(nodesWithoutParents, numSlices);
410 size_t parentNodesAdded = 0;
411 for (
size_t j = 0; j < numSlices; j++) {
412 auto nodesInSlice = std::min(nodesWithoutParents, nodesPerSlice);
413 nodesWithoutParents -= nodesInSlice;
415 parentNodesAdded +=
static_cast<size_t>(std::ceil(
416 static_cast<double>(nodesInSlice) /
static_cast<double>(nodeCapacity)));
419 nodesInTree += parentNodesAdded;
420 nodesWithoutParents = parentNodesAdded;
426 void createParentNodes(
const NodeListIterator& begin,
size_t number) {
430 auto numSlices = sliceCount(number);
431 std::size_t nodesPerSlice = sliceCapacity(number, numSlices);
437 auto end = begin +
static_cast<long>(number);
438 sortNodesX(begin, end);
440 auto startOfSlice = begin;
441 for (
decltype(numSlices) j = 0; j < numSlices; j++) {
443 end = begin +
static_cast<long>(number);
444 auto nodesRemaining =
static_cast<size_t>(std::distance(startOfSlice, end));
445 auto nodesInSlice = std::min(nodesRemaining, nodesPerSlice);
446 auto endOfSlice = std::next(startOfSlice,
static_cast<long>(nodesInSlice));
453 addParentNodesFromVerticalSlice(startOfSlice, endOfSlice);
455 startOfSlice = endOfSlice;
459 void addParentNodesFromVerticalSlice(
const NodeListIterator& begin,
const NodeListIterator& end) {
460 if (BoundsTraits::TwoDimensional::value) {
461 sortNodesY(begin, end);
467 auto firstChild = begin;
468 while (firstChild != end) {
469 auto childrenRemaining =
static_cast<size_t>(std::distance(firstChild, end));
470 auto childrenForNode = std::min(nodeCapacity, childrenRemaining);
471 auto lastChild = std::next(firstChild,
static_cast<long>(childrenForNode));
479 const Node *ptr_first = &*firstChild;
480 const Node *ptr_end = ptr_first + childrenForNode;
482 createBranchNode(ptr_first, ptr_end);
483 firstChild = lastChild;
487 void sortNodesX(
const NodeListIterator& begin,
const NodeListIterator& end) {
488 std::sort(begin, end, [](
const Node &a,
const Node &b) {
489 return BoundsTraits::getX(a.getBounds()) < BoundsTraits::getX(b.getBounds());
493 void sortNodesY(
const NodeListIterator& begin,
const NodeListIterator& end) {
494 std::sort(begin, end, [](
const Node &a,
const Node &b) {
495 return BoundsTraits::getY(a.getBounds()) < BoundsTraits::getY(b.getBounds());
502 template<
typename Visitor,
503 typename std::enable_if<std::is_void<decltype(std::declval<Visitor>()(std::declval<ItemType>()))>::value, std::nullptr_t>::type =
nullptr >
504 bool visitLeaf(Visitor&& visitor,
const Node& node)
506 visitor(node.getItem());
512#if !defined(_MSC_VER) || _MSC_VER >= 1910
513 template<
typename Visitor,
514 typename std::enable_if<std::is_void<decltype(std::declval<Visitor>()(std::declval<BoundsType>(), std::declval<ItemType>()))>::value, std::nullptr_t>::type =
nullptr >
515 bool visitLeaf(Visitor&& visitor,
const Node& node)
517 visitor(node.getBounds(), node.getItem());
524 template<
typename Visitor,
525 typename std::enable_if<!std::is_void<decltype(std::declval<Visitor>()(std::declval<ItemType>()))>::value, std::nullptr_t>::type =
nullptr>
526 bool visitLeaf(Visitor&& visitor,
const Node& node)
528 return visitor(node.getItem());
533#if !defined(_MSC_VER) || _MSC_VER >= 1910
534 template<
typename Visitor,
535 typename std::enable_if<!std::is_void<decltype(std::declval<Visitor>()(std::declval<BoundsType>(), std::declval<ItemType>()))>::value, std::nullptr_t>::type =
nullptr>
536 bool visitLeaf(Visitor&& visitor,
const Node& node)
538 return visitor(node.getBounds(), node.getItem());
542 template<
typename Visitor>
543 bool query(
const BoundsType& queryEnv,
547 assert(!node.isLeaf());
549 for (
auto *child = node.beginChildren(); child < node.endChildren(); ++child) {
550 if (child->boundsIntersect(queryEnv)) {
551 if (child->isLeaf()) {
552 if (!child->isDeleted()) {
553 if (!visitLeaf(visitor, *child)) {
558 if (!query(queryEnv, *child, visitor)) {
567 bool remove(
const BoundsType& queryEnv,
569 const ItemType& item) {
571 assert(!node.isLeaf());
573 for (
auto *child = node.beginChildren(); child < node.endChildren(); ++child) {
574 if (child->boundsIntersect(queryEnv)) {
575 if (child->isLeaf()) {
576 if (!child->isDeleted() && child->getItem() == item) {
579 auto mutableChild =
const_cast<Node*
>(child);
580 mutableChild->removeItem();
584 bool removed = remove(queryEnv, *child, item);
595 size_t sliceCount(
size_t numNodes)
const {
596 double minLeafCount = std::ceil(
static_cast<double>(numNodes) /
static_cast<double>(nodeCapacity));
598 return static_cast<size_t>(std::ceil(std::sqrt(minLeafCount)));
601 static size_t sliceCapacity(
size_t numNodes,
size_t numSlices) {
602 return static_cast<size_t>(std::ceil(
static_cast<double>(numNodes) /
static_cast<double>(numSlices)));
606struct EnvelopeTraits {
607 using BoundsType = geom::Envelope;
608 using TwoDimensional = std::true_type;
610 static bool intersects(
const BoundsType& a,
const BoundsType& b) {
611 return a.intersects(b);
614 static double size(
const BoundsType& a) {
618 static double distance(
const BoundsType& a,
const BoundsType& b) {
619 return a.distance(b);
622 static BoundsType empty() {
626 template<
typename ItemType>
627 static const BoundsType& fromItem(
const ItemType& i) {
628 return *(i->getEnvelopeInternal());
631 template<
typename ItemType>
632 static const BoundsType& fromItem(ItemType&& i) {
633 return *(i->getEnvelopeInternal());
636 static double getX(
const BoundsType& a) {
637 return a.getMinX() + a.getMaxX();
640 static double getY(
const BoundsType& a) {
641 return a.getMinY() + a.getMaxY();
644 static void expandToInclude(BoundsType& a,
const BoundsType& b) {
645 a.expandToInclude(b);
648 static bool isNull(
const BoundsType& a) {
653struct IntervalTraits {
654 using BoundsType = Interval;
655 using TwoDimensional = std::false_type;
657 static bool intersects(
const BoundsType& a,
const BoundsType& b) {
658 return a.intersects(&b);
661 static double size(
const BoundsType& a) {
665 static double getX(
const BoundsType& a) {
666 return a.getMin() + a.getMax();
669 static double getY(
const BoundsType& a) {
670 return a.getMin() + a.getMax();
673 static void expandToInclude(BoundsType& a,
const BoundsType& b) {
674 a.expandToInclude(&b);
677 static bool isNull(
const BoundsType& a) {
684template<
typename ItemType,
typename BoundsTraits = EnvelopeTraits>
685class TemplateSTRtree :
public TemplateSTRtreeImpl<ItemType, BoundsTraits> {
693template<
typename ItemType>
694class TemplateSTRtree<ItemType*, EnvelopeTraits> :
public TemplateSTRtreeImpl<ItemType*, EnvelopeTraits>,
public SpatialIndex {
696 using TemplateSTRtreeImpl<ItemType*, EnvelopeTraits>::TemplateSTRtreeImpl;
697 using TemplateSTRtreeImpl<ItemType*, EnvelopeTraits>::insert;
698 using TemplateSTRtreeImpl<ItemType*, EnvelopeTraits>::query;
699 using TemplateSTRtreeImpl<ItemType*, EnvelopeTraits>::remove;
702 void query(
const geom::Envelope* queryEnv, std::vector<void*>& results)
override {
703 query(*queryEnv, [&results](
const ItemType* x) {
704 results.push_back(
const_cast<void*
>(
static_cast<const void*
>(x)));
708 void query(
const geom::Envelope* queryEnv, ItemVisitor& visitor)
override {
709 query(*queryEnv, [&visitor](
const ItemType* x) {
710 visitor.visitItem(
const_cast<void*
>(
static_cast<const void*
>(x)));
714 bool remove(
const geom::Envelope* itemEnv,
void* item)
override {
715 return remove(*itemEnv,
static_cast<ItemType*
>(item));
718 void insert(
const geom::Envelope* itemEnv,
void* item)
override {
719 insert(*itemEnv, std::move(
static_cast<ItemType*
>(item)));
A function method which computes the distance between two ItemBoundables in an STRtree....
Definition: ItemDistance.h:33
A query-only R-tree created using the Sort-Tile-Recursive (STR) algorithm. For one- or two-dimensiona...
Definition: TemplateSTRtree.h:57
void build()
Definition: TemplateSTRtree.h:344
std::pair< ItemType, ItemType > nearestNeighbour(TemplateSTRtreeImpl< ItemType, BoundsTraits > &other)
Definition: TemplateSTRtree.h:227
std::pair< ItemType, ItemType > nearestNeighbour()
Definition: TemplateSTRtree.h:208
std::pair< ItemType, ItemType > nearestNeighbour(TemplateSTRtreeImpl< ItemType, BoundsTraits > &other, ItemDistance &distance)
Definition: TemplateSTRtree.h:215
std::pair< ItemType, ItemType > nearestNeighbour(ItemDistance &distance)
Definition: TemplateSTRtree.h:202
TemplateSTRtreeImpl(size_t p_nodeCapacity=10)
Definition: TemplateSTRtree.h:130
TemplateSTRtreeImpl(size_t p_nodeCapacity, size_t itemCapacity)
Definition: TemplateSTRtree.h:141
TemplateSTRtreeImpl(const TemplateSTRtreeImpl &other)
Definition: TemplateSTRtree.h:152
void insert(const BoundsType &itemEnv, const ItemType &item)
Definition: TemplateSTRtree.h:190
void insert(const ItemType &item)
Definition: TemplateSTRtree.h:178
void insert(ItemType &&item)
Definition: TemplateSTRtree.h:173
void insert(const BoundsType &itemEnv, ItemType &&item)
Definition: TemplateSTRtree.h:183
bool built() const
Definition: TemplateSTRtree.h:331
const Node * getRoot()
Definition: TemplateSTRtree.h:336
void iterate(F &&func)
Definition: TemplateSTRtree.h:297
Items items()
Definition: TemplateSTRtree.h:287
Basic namespace for all GEOS functionalities.
Definition: geos.h:39