1 /******************************************************************************
2 * Project: libsidx - A C API wrapper around libspatialindex
3 * Purpose: C++ objects to implement a query of the index's leaves.
4 * Author: Howard Butler, hobu.inc@gmail.com
5 ******************************************************************************
6 * Copyright (c) 2009, Howard Butler
7 *
8 * All rights reserved.
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 ******************************************************************************/
28
29 #include <spatialindex/capi/sidx_impl.h>
30
LeafQuery()31 LeafQuery::LeafQuery()
32 {
33
34 }
35
get_results(const SpatialIndex::INode * n)36 LeafQueryResult get_results(const SpatialIndex::INode* n)
37 {
38 LeafQueryResult result (n->getIdentifier());
39
40 SpatialIndex::IShape* ps;
41 n->getShape(&ps);
42 SpatialIndex::Region* pr = dynamic_cast<SpatialIndex::Region*>(ps);
43 std::vector<SpatialIndex::id_type> ids;
44 for (uint32_t cChild = 0; cChild < n->getChildrenCount(); cChild++)
45 {
46 ids.push_back(n->getChildIdentifier(cChild));
47 }
48
49 result.SetIDs(ids);
50 result.SetBounds(pr);
51 delete ps;
52
53 return result;
54 }
getNextEntry(const SpatialIndex::IEntry & entry,SpatialIndex::id_type & nextEntry,bool & hasNext)55 void LeafQuery::getNextEntry( const SpatialIndex::IEntry& entry,
56 SpatialIndex::id_type& nextEntry,
57 bool& hasNext)
58 {
59
60 const SpatialIndex::INode* n = dynamic_cast<const SpatialIndex::INode*>(&entry);
61
62 // traverse only index nodes at levels 2 and higher.
63 if (n != 0 && n->getLevel() > 0)
64 {
65 for (uint32_t cChild = 0; cChild < n->getChildrenCount(); cChild++)
66 {
67 m_ids.push(n->getChildIdentifier(cChild));
68 }
69 }
70
71 if (n->isLeaf()) {
72 m_results.push_back(get_results(n));
73 }
74
75 if (! m_ids.empty())
76 {
77 nextEntry = m_ids.front(); m_ids.pop();
78 hasNext = true;
79 }
80 else
81 {
82 hasNext = false;
83 }
84 }
85
86
GetIDs() const87 std::vector<SpatialIndex::id_type> const& LeafQueryResult::GetIDs() const
88 {
89 return ids;
90 }
91
SetIDs(std::vector<SpatialIndex::id_type> & v)92 void LeafQueryResult::SetIDs(std::vector<SpatialIndex::id_type>& v)
93 {
94 ids.resize(v.size());
95 std::copy(v.begin(), v.end(), ids.begin());
96 }
GetBounds() const97 const SpatialIndex::Region* LeafQueryResult::GetBounds() const
98 {
99 return bounds;
100 }
101
SetBounds(const SpatialIndex::Region * b)102 void LeafQueryResult::SetBounds(const SpatialIndex::Region* b)
103 {
104 bounds = new SpatialIndex::Region(*b);
105 }
106
LeafQueryResult(LeafQueryResult const & other)107 LeafQueryResult::LeafQueryResult(LeafQueryResult const& other)
108 {
109 ids.resize(other.ids.size());
110 std::copy(other.ids.begin(), other.ids.end(), ids.begin());
111 m_id = other.m_id;
112
113 bounds = other.bounds->clone();
114 }
115
operator =(LeafQueryResult const & rhs)116 LeafQueryResult& LeafQueryResult::operator=(LeafQueryResult const& rhs)
117 {
118 if (&rhs != this)
119 {
120 ids.resize(rhs.ids.size());
121 std::copy(rhs.ids.begin(), rhs.ids.end(), ids.begin());
122 m_id = rhs.m_id;
123 bounds = rhs.bounds->clone();
124 }
125 return *this;
126 }
127
128