Skip to content

2. Creating an index

Payas Rajan edited this page Jul 11, 2016 · 2 revisions

An index will be used to store all spatial items. I am using the function GetDefaults() which will give us some default settings for the index but you can customise many of the options. The defaults are in utility.cc http://libspatialindex.github.io/doxygen/Utility_8cc_source.html#l00031

This example uses a 2 dimensional r*-tree spatial index that is stored in memory. Create the following c++ program and save as tutorial2.cpp

#include <iostream>
#include <spatialindex/capi/sidx_api.h>
#include <spatialindex/capi/sidx_impl.h>
#include <spatialindex/capi/sidx_config.h>

using namespace std;
using namespace SpatialIndex;

// function to create a new spatial index
Index* createIndex()
{
	// create a property set with default values.
	// see utility.cc for all defaults  http://libspatialindex.github.io/doxygen/Utility_8cc_source.html#l00031
	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 memory (default is disk)
	var.m_varType = Tools::VT_ULONG;
	var.m_val.ulVal = RT_Memory;
	ps->setProperty("IndexStorageType", var);

	// initalise index
	Index* idx = new Index(*ps);
	delete ps;

	// check index is ok
	if (!idx->index().isIndexValid())
		throw "Failed to create valid index";
	else
		cout << "created index" << endl;

	return idx;
}


int main(int argc, char* argv[])
{
	// initalise Indexpointer
	Index* idx = createIndex();

}

Then compile and run as follows:

g++ -std=c++0x tutorial2.cpp -lspatialindex_c -lspatialindex -o tutorial2
./tutorial2

You should receive the message

created index
Clone this wiki locally