2

我有一堆点,我需要对它们进行最近邻搜索,所以我使用的是 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 但是如果我在初始化后立即进行查询或检查索引中的元素数量,它会失败并且只有一个节点树。

我已经看过这些问题:( 重新)使用空间索引库加载 R 树

C++ 空间索引库:从磁盘加载/存储主内存 RTree

但是我没有成功,因为我使用的是索引,当我直接使用 RTree 时,数据插入速度要慢 1000 倍。

4

1 回答 1

0

我找到了解决方案。索引实例化了树的 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);
于 2015-10-28T18:51:09.170 回答