Skip to content
Patrick Woodcock edited this page May 6, 2015 · 1 revision

The index we created is now ready to have shapes added to it. There are a few different types of shapes you can add such as Point, Region, LineSegment, Node. Each shape must have a unique ID and can also store a pointer to an object. We will add some points in this example. First copy tutorial2.cpp to tutorial3.cpp and add the following function before the main() function:

// add a Point to index.
void addPoint(Index* idx,double lat,double lon, int64_t id)
{
	// create array with lat/lon points
	double coords[] = {lat, lon};

	// shapes can also have anobject associated with them but we'll leave that for the moment.
	uint8_t* pData = 0;
	uint32_t nDataLength = 0;

	// create shape
	SpatialIndex::IShape* shape = 0;
	shape = new SpatialIndex::Point(coords, 2);

	// insert into index along with the an object and an ID
	idx->index().insertData(nDataLength,pData,*shape,id);

	cout << "Point " << id << " inserted into index." << endl;

	delete shape;

}

This function will take an index,lat,lon and ID for our point to insert. Although I'm using lat/lon here it doesn't have to use lat/lon points. Now modify the main() function so it reads:

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

	// add some points
	addPoint(idx,51.501364,-0.14189,1); // buckingham palace
	addPoint(idx,51.505456,-0.075356,2); // tower bridge
	addPoint(idx,51.5072734,-0.1657391,3); // hyde park

}

This will now add 3 points to the index. Now compile and run as follows:

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

This should output:

created index
Point 1 inserted into index.
Point 2 inserted into index.
Point 3 inserted into index.
Clone this wiki locally