Skip to content
Kanishka Azimi edited this page Feb 12, 2015 · 6 revisions

Generally, one accesses all the index implementations through a common interface, a set of global wrapper/dispatching functions prefixed with Index_. Property Sets are used for providing a varying set of parameters to the common interface, such as when creating specific implementation instances.

Throughout, the [Type]H alias is used instead of [Type]* for handles.

#include <stdio.h>
#include <spatialindex/capi/sidx_api.h>  

Include the C API, Spatial Index API header.

void load(IndexH idx);
void query(IndexH idx);
void bounds(IndexH idx);

int main()
{
   char* pszVersion = SIDX_Version();
   fprintf(stdout, "libspatialindex version: %s\n", pszVersion);
   fflush(stdout);
   free(pszVersion);

   IndexPropertyH props = IndexProperty_Create();
 
   // create an in-memory r*-tree index
   IndexProperty_SetIndexType(props, RT_RTree);
   IndexProperty_SetIndexStorage(props, RT_Memory);
   IndexH idx = Index_Create(props);
   IndexProperty_Destroy(props); 

Create an empty property set. Set the Index Type property to RTree and set the Index Storage property to Memory. Create an index with the property set. Release the property set.

   if (Index_IsValid(idx))

Ensure the index was created successfully.

   {
       load(idx);
       bounds(idx);
       query(idx);
       Index_Destroy(idx);

Release the created index.

   }
   else
   {
       printf("Failed to create valid index\n");
   }
   return 0; 
}

void load(IndexH idx)
{
    double min[] = {0.5, 0.5};
    double max[] = {0.5, 0.5};
    Index_InsertData(idx, 1, min, max, 2, 0, 0);

Insert a 2D point with id of 1 and no additional property data. This creates a point, instead of a region, because it follows the convention of passing in equivalent min and max coordinates.

}
void query(IndexH idx)
{
    double min[] = {0.0, 0.0};
    double max[] = {1.0, 1.0};
    uint64_t nResults;
    Index_Intersects_count(idx, min, max, 2, &nResults);

Query the index for the number of intersections with 2D region bounded by min and max.

    if (nResults == 1)
         printf("Successful Query\n");
    else
         printf("Failed to execute query\n");
}

void bounds(IndexH idx)
{
    uint32_t dims;
    double* pMins;
    double* pMaxs;
    Index_GetBounds(idx, &pMins, &pMaxs, &dims);

Get the lower and upper bound points of the index, where each boundary point is provided as an array of dimension values.

    free(pMins);
    free(pMaxs);

    if (dims == 2)
        printf("Successful bounds query\n");
    else
        printf("Failed to execute bounds query\n");
}
Clone this wiki locally