forked from MapWindow/MapWinGIS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexShapeFiles.cpp
More file actions
266 lines (241 loc) · 7.43 KB
/
IndexShapeFiles.cpp
File metadata and controls
266 lines (241 loc) · 7.43 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
// 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 "ShapeFileStream.h"
namespace IndexSearching
{
/* ************************************************************************* **
**
** Function to create inde file on a shape file.
**
** Creates baseName.dat and baseName.idx
**
** ************************************************************************* */
bool createIndexFile(double utilization, int capacity, string baseName)
{
bool ret = false;
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);
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;
#ifndef __DEBUG
tree = RTree::createAndBulkLoadNewRTree(RTree::BLM_STR, inputStream, *file, utilization,
capacity, capacity, 2, SpatialIndex::RTree::RV_RSTAR, indexIdentifier);
#endif
ret = tree->isIndexValid();
}
catch (char *str)
{
ret = false;
cerr << "Got an error " << str << endl;
}
catch (exception ex)
{
ret = false;
cerr << "Got an error " << ex.what() << endl;
}
catch (Tools::IllegalStateException ex)
{
ret = false;
cerr << "Got an error " << ex.what() << endl;
}
catch (Tools::IllegalArgumentException ex)
{
ret = false;
cerr << "Got an error " << ex.what() << endl;
}
delete tree;
delete file;
delete diskfile;
return ret;
}
/* ************************************************************************* **
**
** Function to query shape file.
**
**
**
** ************************************************************************* */
bool isValidIndexFile(string baseName, int bufferSize)
{
bool ret = false;
IStorageManager *diskfile;
StorageManager::IBuffer *file;
ISpatialIndex *tree;
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);
ret = tree->isIndexValid();
}
catch (char *str)
{
cerr << "Got an error " << str << endl;
}
delete tree;
delete file;
delete diskfile;
return ret;
}
/* ************************************************************************* **
**
** Function to query shape file.
**
**
**
** ************************************************************************* */
void queryIndexFile(ISpatialIndex *spatialIndex, SpatialIndex::Region queryRegion, QueryTypeFlags queryType, ShapeIdxVisitor *vis)
{
try
{
if (queryType == 1)
spatialIndex->intersectsWithQuery(queryRegion, *vis);
else if (queryType == 2)
spatialIndex->containsWhatQuery(queryRegion, *vis);
}
catch (exception ex)
{
cerr << "Error running query on spatial index." << endl;
}
}
int selectShapesFromIndex(ISpatialIndex *spatialIndex, double *lowVals, double *hiVals, QueryTypeFlags queryType, CIndexSearching *resulSet)
{
int rCode = 0;
try
{
long val;
double xmin, ymin, xmax, ymax;
vector <long> res;
xmin = (lowVals[0] < hiVals[0]) ? lowVals[0] : hiVals[0];
ymin = (lowVals[1] < hiVals[1]) ? lowVals[1] : hiVals[1];
xmax = (lowVals[0] > hiVals[0]) ? lowVals[0] : hiVals[0];
ymax = (lowVals[1] > hiVals[1]) ? lowVals[1] : hiVals[1];
lowVals[0] = xmin;
lowVals[1] = ymin;
hiVals[0] = xmax;
hiVals[1] = ymax;
Region *queryRegion = new Region(lowVals, hiVals, 2);
ShapeIdxVisitor *vis = new ShapeIdxVisitor();
queryIndexFile(spatialIndex, *queryRegion, queryType, vis);
resulSet->setCapacity(vis->ids.size());
int arrSize = vis->ids.size();
for (unsigned i = 0; i < vis->ids.size(); vis->ids.pop())
{
val = (long)vis->ids.front();
int len = resulSet->getLength();
resulSet->addValue(val);
}
delete queryRegion;
delete vis;
}
catch (exception ex)
{
rCode = -1;
}
catch (Tools::IllegalStateException ex1)
{
rCode = -1;
}
catch (Tools::IllegalArgumentException ex2)
{
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.
byte* pData = 0;
uint32_t cLen = 0;
d.getData(cLen, &pData);
delete[] pData;
__int64 val;
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(CSpatialIndexID spatialIndexID)
{
map<CSpatialIndexID, CacheItem>::iterator mapIterator = m_cache.find(spatialIndexID);
if (mapIterator != m_cache.end())
return (*mapIterator).second.m_tree;
return NULL;
}
CSpatialIndexID CSpatialIndexCache::cacheSpatialIndex(ISpatialIndex *tree, IStorageManager *diskfile, StorageManager::IBuffer *file)
{
CSpatialIndexID newSpatialIndexID = m_nextSpatialIndexID++;
m_cache[newSpatialIndexID] = CacheItem(diskfile, file, tree);
return newSpatialIndexID;
}
void CSpatialIndexCache::uncacheSpatialIndex(CSpatialIndexID spatialIndexID, bool releaseAll)
{
map<CSpatialIndexID, CacheItem>::iterator 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;
}
}