example.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <stdio.h>
  2. #include <cstdlib>
  3. #include <spatialindex/capi/sidx_api.h>
  4. #include <spatialindex/capi/sidx_impl.h>
  5. #include <spatialindex/capi/sidx_config.h>
  6. using namespace std;
  7. using namespace SpatialIndex;
  8. void load(IndexH idx);
  9. void query(IndexH idx);
  10. void bounds(IndexH idx);
  11. int main()
  12. {
  13. char* pszVersion = SIDX_Version();
  14. fprintf(stdout, "libspatialindex version: %s\n", pszVersion);
  15. fflush(stdout);
  16. free(pszVersion);
  17. IndexPropertyH props = IndexProperty_Create();
  18. // create an in-memory r*-tree index
  19. IndexProperty_SetIndexType(props, RT_RTree);
  20. IndexProperty_SetIndexStorage(props, RT_Memory);
  21. IndexH idx = Index_Create(props);
  22. IndexProperty_Destroy(props);
  23. if (Index_IsValid(idx))
  24. {
  25. load(idx);
  26. bounds(idx);
  27. query(idx);
  28. Index_Destroy(idx);
  29. }
  30. else
  31. {
  32. printf("Failed to create valid index\n");
  33. }
  34. return 0;
  35. }
  36. void load(IndexH idx)
  37. {
  38. double min[] = {0.5, 0.5};
  39. double max[] = {0.5, 0.5};
  40. Index_InsertData(idx, 1, min, max, 2, 0, 0);
  41. }
  42. void query(IndexH idx)
  43. {
  44. double min[] = {0.0, 0.0};
  45. double max[] = {1.0, 1.0};
  46. uint64_t nResults;
  47. Index_Intersects_count(idx, min, max, 2, &nResults);
  48. if (nResults == 1)
  49. printf("Successful Query\n");
  50. else
  51. printf("Failed to execute query\n");
  52. }
  53. void bounds(IndexH idx)
  54. {
  55. uint32_t dims;
  56. double* pMins;
  57. double* pMaxs;
  58. Index_GetBounds(idx, &pMins, &pMaxs, &dims);
  59. free(pMins);
  60. free(pMaxs);
  61. if (dims == 2)
  62. printf("Successful bounds query\n");
  63. else
  64. printf("Failed to execute bounds query\n");
  65. }