libSpatialIndex:在磁盘上加载/存储索引
libSpatialIndex: loading/storing index on disk
我有一堆点,我需要对它们进行最近邻居搜索,所以我使用libSpatialIndex。代码非常直接,库让我可以选择将数据存储在磁盘上,但我无法加载它
代码:
int main(){
Tools::PropertySet* ps = GetDefaults();
Tools::Variant var;
// set index type to R*-Tree
var.m_varType = Tools::VT_ULONG;
var.m_val.ulVal = RT_RTree;
ps->setProperty("IndexType", var);
// Set index to store in disk
var.m_varType = Tools::VT_ULONG;
var.m_val.ulVal = RT_Disk;
ps->setProperty("IndexStorageType", var);
char filename[] = "indexTeste";
var.m_varType = Tools::VT_PCHAR;
var.m_val.pcVal = filename;
ps->setProperty("FileName", var);
var.m_varType = Tools::VT_BOOL;
var.m_val.blVal = false;
ps->setProperty("Overwrite", var);
cout << (*ps) << endl;
// initalise index
idx = new Index(*ps);
delete ps;
// Now there's specific code for point loading so I've shortened it - this part is working
for (...) { // all points
double pt[] = {point.getX(), point.getY()};
SpatialIndex::IShape* shape = 0;
shape = new SpatialIndex::Point(pt, 2);
// insert into index along with the an object and an ID
idx->index().insertData(nDataLength,(unsigned char*)&lineID,*shape,id);
}
// Now the search - working as well
ObjVisitor* visitor = new ObjVisitor;
SpatialIndex::Point* r = new SpatialIndex::Point(inter, 2);
idx->index().nearestNeighborQuery(1,*r,*visitor);
int64_t nResultCount;
nResultCount = visitor->GetResultCount();
// get actual results
vector<SpatialIndex::IData*>& results = visitor->GetResults();
SpatialIndex::IShape* shape;
results[0]->getShape(&shape);
unsigned char * dataAddr;
unsigned int length = sizeof(int);
results[0]->getData(length,&dataAddr);
int lineId = ((int*)dataAddr)[0];
SpatialIndex::Point center;
shape->getCenter(center);
}
然后程序就结束了。内存中确实创建了两个文件,"indexTest.dat"8.8MB和"indexTest.idx"0kB,但如果我在初始化后立即进行查询或检查索引中的元素数量,则会失败,并且树上只有一个节点。
我已经看了这些问题:(重新)加载带有spatialindex库的R树
C++空间索引库:从磁盘加载/存储主内存RTree
但我没有成功,因为我使用了Index,当我直接使用RTree时,数据插入速度慢了1000倍。
我找到了解决方案。索引实例化了树的ID,必须在创建索引时使用该ID才能正确加载文件。
代码:
//Example
// When storing
Tools::PropertySet* ps = GetDefaults();
Index* idx;
idx = new Index(*ps);
Tools::PropertySet properties = idx->GetProperties();
Tools::Variant vari = properties.getProperty("IndexIdentifier");
cout << "ID: " << vari.m_val.llVal << endl;
// when loading
Tools::PropertySet* ps = GetDefaults();
Tools::Variant var;
// Important
var.m_varType = Tools::VT_BOOL;
var.m_val.blVal = false;
ps->setProperty("Overwrite", var);
var.m_varType = Tools::VT_LONGLONG;
var.m_val.llVal = ID; // The number "couted" before
ps->setProperty("IndexIdentifier", var);
Index* idx;
idx = new Index(*ps);
相关文章:
- 如何对存储为"Compressed Sparse Row"的矩阵进行稀疏矩阵索引?
- 一个 2D 数组,并按行存储值.第一个循环用于行索引,第二个循环用于列索引
- 如何用索引命名一个变量来存储输入 mxArray?
- C++ - 浮点数组在某个非常大的索引之后不存储值
- 存储将单个查询图像与多个图像的列表匹配的关键点索引
- 用于存储网格(将具有负索引)的数据结构
- 使用索引将 BST 值存储到数组中
- C ++将文本存储在数组的不同索引中,并带有一两个
- 如何在C++中找到存储在多集特定索引中的值?
- 可以在同一 VBO 中存储不同的顶点属性和索引
- 如何知道我想在数组中存储多少索引
- 如何将元素的索引从2D数组存储到1D数组中,然后交换这些值
- 将索引存储在向量<集合<int>>与向量<向量中<int>>
- 存储Boost Multi_index索引迭代器
- 几对性能问题(一个较大的向量与较小的块向量),值得存储迭代索引以进行矢量访问
- 对整个垫子图像进行排序并在 OpenCV 中存储索引
- 如何将整数存储在字符数组的单个索引中
- 如何将数组索引存储在映射中
- libSpatialIndex:在磁盘上加载/存储索引
- Omp最大减少与存储索引