I have a bunch of points and I need to do a nearest neighbor search on them so I'm using the libSpatialIndex. The code is pretty straight forward and the library gives me the option to store the data on disk but I can not load it.
the code:
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);
}
The program finishes right after that. Two files are indeed created in memory, "indexTest.dat" 8.8MB and "indexTest.idx" 0kB but if I do the query or check the number of elements in the index right after initialization, it fails and theres only one node on the tree.
I already looked at the questions: (Re)loading the R Tree with spatialindex library
C++ spatialindex library: loading/storing main memory RTree from/to disk
But I got no success since I'm using Index and when I use RTree directly, data insertion is like 1000x slower.
I found the solution. The index instantiates an ID for the Tree and it must be used on Index creation in order to load the file correctly.
code:
//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);