-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathIndexShapeFiles.cpp
More file actions
282 lines (252 loc) · 8.78 KB
/
IndexShapeFiles.cpp
File metadata and controls
282 lines (252 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// IndexShapeFiles.cpp : Main functions for creating and querying indexes
//
// Index Searching Library
//
// Copyright (C) 2008 Versaterm Inc.
//
// The core of this library is the Spatial Index Library by Marios Hadjieleftheriou.
//
// http://www.research.att.com/~marioh/spatialindex/index.html
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//
#include <StdAfx.h>
#include "IndexShapeFiles.h"
#include <gsl/pointers>
#include <gsl/util>
#include "ShapeFileStream.h"
// ReSharper disable once CppInconsistentNaming
namespace IndexSearching
{
/* ************************************************************************* **
**
** Function to create inde file on a shape file.
**
** Creates baseName.dat and baseName.idx
**
** ************************************************************************* */
bool CreateIndexFile(const double utilization, const int capacity, string baseName)
{
bool ret;
//IStorageManager* diskfile = nullptr;
//StorageManager::IBuffer* file = nullptr;
//ISpatialIndex* tree = nullptr;
try
{
// Create a new storage manager with the provided base name and a 4K page size.
//diskfile = StorageManager::createNewDiskStorageManager(baseName, 4096);
//file = StorageManager::createNewRandomEvictionsBuffer(*diskfile, capacity, false);
// Create a new storage manager with the provided base name and a 4K page size.
const auto diskfile = unique_ptr<IStorageManager>(StorageManager::createNewDiskStorageManager(baseName, 4096));
// this will try to locate and open an already existing storage manager.
const auto file = unique_ptr<StorageManager::IBuffer>(StorageManager::createNewRandomEvictionsBuffer(*diskfile, capacity, false));
ShapeFileStream inputStream(baseName);
// Create and bulk load a new RTree with dimensionality 2, using "file" as
// the StorageManager and the RSTAR splitting policy.
id_type indexIdentifier = 0;
// tree = RTree::createAndBulkLoadNewRTree(RTree::BLM_STR, inputStream, *file, utilization, capacity, capacity, 2, SpatialIndex::RTree::RV_RSTAR, indexIdentifier);
const auto tree = unique_ptr<ISpatialIndex>(createAndBulkLoadNewRTree(
RTree::BLM_STR, inputStream, *file, utilization, capacity, capacity, 2, SpatialIndex::RTree::RV_RSTAR,
indexIdentifier));
ret = tree->isIndexValid();
}
catch (const char* str)
{
ret = false;
cerr << "Got an error " << str << endl;
}
catch (const exception& ex)
{
ret = false;
cerr << "Got an error " << ex.what() << endl;
}
catch (Tools::IllegalStateException& ex)
{
ret = false;
cerr << "Got an IllegalStateException " << ex.what() << endl;
}
catch (Tools::IllegalArgumentException& ex)
{
ret = false;
cerr << "Got an IllegalArgumentException " << ex.what() << endl;
}
//delete tree;
//delete file;
//delete diskfile;
return ret;
}
/* ************************************************************************* **
**
** Function to query shape file.
**
**
**
** ************************************************************************* */
bool IsValidIndexFile(string baseName, const int bufferSize)
{
bool ret = false;
//IStorageManager* diskfile = nullptr;
//StorageManager::IBuffer* file = nullptr;
//ISpatialIndex* tree = nullptr;
try
{
//diskfile = StorageManager::loadDiskStorageManager(baseName);
// this will try to locate and open an already existing storage manager.
//file = StorageManager::createNewRandomEvictionsBuffer(*diskfile, bufferSize, false);
//tree = RTree::loadRTree(*file, 1);
const auto diskfile = unique_ptr<IStorageManager>(StorageManager::loadDiskStorageManager(baseName));
// this will try to locate and open an already existing storage manager.
const auto file = unique_ptr<StorageManager::IBuffer>(StorageManager::createNewRandomEvictionsBuffer(*diskfile, bufferSize, false));
const auto tree = unique_ptr<ISpatialIndex>(RTree::loadRTree(*file, 1));
ret = tree->isIndexValid();
}
catch (const char* const str)
{
cerr << "Got an error " << str << endl;
}
//delete tree;
//delete file;
//delete diskfile;
return ret;
}
/* ************************************************************************* **
**
** Function to query shape file.
**
**
**
** ************************************************************************* */
void QueryIndexFile(const gsl::not_null<ISpatialIndex*> spatialIndex, const SpatialIndex::Region& queryRegion, const QueryTypeFlags queryType, ShapeIdxVisitor* vis)
{
try
{
if (queryType == QueryTypeFlags::intersection)
spatialIndex->intersectsWithQuery(queryRegion, *vis);
else if (queryType == QueryTypeFlags::contained)
spatialIndex->containsWhatQuery(queryRegion, *vis);
}
catch (...)
{
cerr << "Error running query on spatial index." << endl;
}
}
// TODO: Lots of compile warnings:
int SelectShapesFromIndex(ISpatialIndex* spatialIndex, double* lowVals, double* hiVals, const QueryTypeFlags queryType, CIndexSearching* resulSet)
{
int rCode = 0;
try
{
vector <long> res;
const double xmin = lowVals[0] < hiVals[0] ? lowVals[0] : hiVals[0];
const double ymin = lowVals[1] < hiVals[1] ? lowVals[1] : hiVals[1];
const double xmax = lowVals[0] > hiVals[0] ? lowVals[0] : hiVals[0];
const double ymax = lowVals[1] > hiVals[1] ? lowVals[1] : hiVals[1];
lowVals[0] = xmin;
lowVals[1] = ymin;
hiVals[0] = xmax;
hiVals[1] = ymax;
const Region* const queryRegion = new Region(lowVals, hiVals, 2);
const auto vis = new ShapeIdxVisitor();
QueryIndexFile(spatialIndex, *queryRegion, queryType, vis);
resulSet->SetCapacity(gsl::narrow_cast<int>(vis->ids.size()));
// int arrSize = vis->ids.size();
for (constexpr unsigned int i = 0; i < vis->ids.size(); vis->ids.pop())
{
const long val = static_cast<long>(vis->ids.front());
//int len = resulSet->GetLength();
resulSet->AddValue(val);
}
delete queryRegion;
delete vis;
}
catch (exception&)
{
rCode = -1;
}
catch (Tools::IllegalStateException&)
{
rCode = -1;
}
catch (Tools::IllegalArgumentException&)
{
rCode = -1;
}
catch (...)
{
rCode = -1;
}
return rCode;
}
/* ************************************************************************* **
**
** Function to query shape file.
**
**
**
** ************************************************************************* */
void ShapeIdxVisitor::visitData(const IData& d)
{
// data should be an array of characters representing a Region as a string.
uint8_t* pData = nullptr;
uint32_t cLen = 0;
d.getData(cLen, &pData);
delete[] pData; // TODO: Fix compile warning
const auto val = d.getIdentifier();
ids.push(val);
}
void ShapeIdxVisitor::visitNode(const INode& n)
{
if (n.isLeaf())
m_leafIO++;
else
m_indexIO++;
}
//08-24-2009 (sm) caching for performance
CSpatialIndexCache& CSpatialIndexCache::Instance()
{
static CSpatialIndexCache spatialIndexCache;
return spatialIndexCache;
}
ISpatialIndex* CSpatialIndexCache::GetSpatialIndexById(const CSpatialIndexID spatialIndexId)
{
const auto mapIterator = m_cache.find(spatialIndexId);
if (mapIterator != m_cache.end())
return (*mapIterator).second.m_tree;
return nullptr;
}
CSpatialIndexID CSpatialIndexCache::CacheSpatialIndex(ISpatialIndex* tree, IStorageManager* diskfile, StorageManager::IBuffer* file)
{
const CSpatialIndexID newSpatialIndexId = m_nextSpatialIndexID++;
m_cache[newSpatialIndexId] = CacheItem(diskfile, file, tree);
return newSpatialIndexId;
}
void CSpatialIndexCache::UncacheSpatialIndex(const CSpatialIndexID spatialIndexId, const bool releaseAll)
{
const auto mapIterator = m_cache.find(spatialIndexId);
if (mapIterator != m_cache.end())
{
if (releaseAll)
(*mapIterator).second.ReleaseAll();
m_cache.erase(mapIterator);
}
}
void CSpatialIndexCache::CacheItem::ReleaseAll()
{
delete m_tree;
delete m_file;
delete m_diskfile;
}
}