Creating the same table within another Dataset

751
0
06-04-2019 07:03 AM
ZacharyPeel
New Contributor

So currently I am trying to work on having the same table within several different datasets as a structure similar to the following.

GdbManagement.gdb

--CA_CANADA (dataset)

----Polygon (table)

----Polyline (table)

----Multipoint (table)

--GB_UNITED_KINGDOM (dataset)

----Polygon (table)

----Polyline (table)

----Multipoint (table)

... and so on.

The three tables are true duplicates at creation, but eventually will contain different data, but I am having a problem with creating the same table in a different dataset. I have tried to close and open on each iteration, define the table once, and shorting the name, which all have failed. So I am looking for some help to get past this issue. Any help is appreciated.

Current code I am using for testing (the single table is just a test that it doesn't currently create the table a second time).

using namespace std;
using namespace FileGDBAPI;

int main() {
	fgdbError   hr;
	wstring     errorText;
	Geodatabase geodatabase;
	if ((hr = CreateGeodatabase(L"../GeodatabaseManagement/GdbManagement.gdb", geodatabase)) != S_OK) {
		wcout << "An error occurred while creating the geodatabase." << endl;
		ErrorInfo::GetErrorDescription(hr, errorText);
		wcout << errorText << "(" << hr << ")." << endl;
		return -1;
	}
	CloseGeodatabase(geodatabase);

	SpatialReference spatialReference;
	spatialReference.SetSpatialReferenceID(4326);
	spatialReference.SetSpatialReferenceText(L"GEOGCS[\"WGS84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]");

	string inCountries[5] = { "CA_CANADA", "GB_UNITED_KINGDOM", "US_UNITED_STATES", "JA_JAPAN", "CN_CHINA" };

	// Create the geometryDef
	GeometryDef geometryDef;
	geometryDef.SetGeometryType(geometryPolyline);
	geometryDef.SetSpatialReference(spatialReference);
	geometryDef.SetHasZ(false); //Set to true if the feature class is to be Z enabled. Defaults to FALSE.
	geometryDef.SetHasM(false); //Set to true if the feature class is to be M enabled. Defaults to FALSE.

	// Create the field def for the table.
	std::vector<FieldDef> fieldDefs;

	FieldDef fieldDef1;
	fieldDef1.SetName(L"OBJECTID");
	fieldDef1.SetType(fieldTypeOID);
	fieldDef1.SetIsNullable(false);
	fieldDefs.push_back(fieldDef1);

	FieldDef fieldDef2;
	fieldDef2.SetName(L"Shape");
	fieldDef2.SetType(fieldTypeGeometry);
	fieldDef2.SetIsNullable(true);
	fieldDef2.SetGeometryDef(geometryDef);
	fieldDefs.push_back(fieldDef2);

	FieldDef fieldDef3;
	fieldDef3.SetName(L"StopID");
	fieldDef3.SetType(fieldTypeInteger);
	fieldDef3.SetIsNullable(true);
	fieldDef3.SetAlias(L"Stop Identifier");
	fieldDefs.push_back(fieldDef3);

	FieldDef fieldDef4;
	fieldDef4.SetName(L"StopType");
	fieldDef4.SetType(fieldTypeSmallInteger);
	fieldDef4.SetIsNullable(true);
	fieldDefs.push_back(fieldDef4);

	FieldDef fieldDef5;
	fieldDef5.SetName(L"Test_Value");
	fieldDef5.SetType(fieldTypeString);
	fieldDef5.SetIsNullable(true);
	fieldDef5.SetAlias(L"This is a test");
	fieldDef5.SetLength(4000);
	fieldDefs.push_back(fieldDef5);

	for (int i = 0; i < 5; i++) {

		if ((hr = OpenGeodatabase(L"../GeodatabaseManagement/GdbManagement.gdb", geodatabase)) != S_OK) {
			wcout << "An error occurred while opening the geodatabase." << endl;
			ErrorInfo::GetErrorDescription(hr, errorText);
			wcout << errorText << "(" << hr << ")." << endl;
			return -1;
		}

		string country = "\\" + inCountries[i];
		wstring wide_string = wstring(country.begin(), country.end());
		const wchar_t* result = wide_string.c_str();
		if ((hr = geodatabase.CreateFeatureDataset(result, spatialReference)) != S_OK) {
			  wcout << "An error occurred while creating the geodatabase!" << endl;
			  ErrorInfo::GetErrorDescription(hr, errorText);
			  wcout << errorText << "(" << hr << ")." << endl;
			  return -1;
		}


		  // Create the table.
		  Table table;
		  country = "\\" + inCountries[i] + "\\Test";
		  wide_string = wstring(country.begin(), country.end());
		  result = wide_string.c_str();
		  if ((hr = geodatabase.CreateTable(result, fieldDefs, L"DEFAULTS", table)) != S_OK) {
			  wcout << "An error occurred while creating the table." << endl;
			  ErrorInfo::GetErrorDescription(hr, errorText);
			  wcout << errorText << "(" << hr << ")." << endl;
			  //return -1;
		  }
		  wcout << "The table has been created." << endl;

		  //Compact the geodatabase.Recomended after bulk updates.
		  if ((hr = geodatabase.CompactDatabase()) != S_OK) {
			  wcout << "An error occurred while compacting the geodatabase." << endl;
			  ErrorInfo::GetErrorDescription(hr, errorText);
			  wcout << errorText << "(" << hr << ")." << endl;
			  return -1;
		  }

		  // Close the geodatabase before the delete.
		  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;
		  }
	}
return 0;
}
0 Kudos
0 Replies