Rebuild Spatial Index on File Geo-Database

3533
1
09-10-2014 02:44 PM
PaulAustin
Occasional Contributor

Is it possible using the API to re-build the spatial index for a layer.

We have an application that runs on Citrix servers. Sometimes they get disconnected and the application is forced to close on the server.

This results in records not being updated in the spatial index. As a result any bounding box queries fail to return the records.

In ArcCatalog I can delete the index then re-add it and then everything is back to normal. I'd like to be able to do that using the API.

Tags (1)
0 Kudos
1 Reply
LanceShipman
Esri Regular Contributor

Switching into load only mode and back will rebuild the index.

Try the following:

  // Open the geodatabase.

  fgdbError hr;

  wstring   errorText;

  Geodatabase geodatabase;

  if ((hr = OpenGeodatabase(L"C:/FileGDB_API_Test/OpenTable - Copy/TemplateData.gdb", geodatabase)) != S_OK)

  {

    wcout << "An error occurred while opening the geodatabase." << endl;

    ErrorInfo::GetErrorDescription(hr, errorText);

    wcout << errorText << "(" << hr << ")." << endl;

    return -1;

  }

  wcout << '\n' << "Open TemplateData.gdb" << '\n' << endl;

  // Open the counties table.

  Table table;

  if ((hr = geodatabase.OpenTable(L"\\counties", table)) != S_OK)

  {

    wcout << "An error occurred while opening the table." << endl;

    ErrorInfo::GetErrorDescription(hr, errorText);

    wcout << errorText << "(" << hr << ")." << endl;

    return -1;

  }

  // Switch in and out of LoadOnlyMode. This rebuilds the spatial index

  table.LoadOnlyMode(true);

  table.LoadOnlyMode(false);

  // Close the table

  if ((hr = geodatabase.CloseTable(table)) != S_OK)

  {

    wcout << "An error occurred while closing ManyFieldTypes." << endl;

    ErrorInfo::GetErrorDescription(hr, errorText);

    wcout << errorText << "(" << hr << ")." << endl;

    return -1;

  }

  // Close the geodatabase

  if ((hr = CloseGeodatabase(geodatabase)) != S_OK)

  {

    wcout << "An error occurred while closing the geodatabase." << endl;

    ErrorInfo::GetErrorDescription(hr, errorText);

    wcout << errorText << "(" << hr << ")." << endl;

    return -1;

  }

Lance Shipman

Esri

Geodatabase Product Engineer, File Geodatabase, SQLite

0 Kudos