I used GDAL meathod “GDALDataset::CopyLayer” to copy a layer outside the dataset to the dataset in FileGDB. But, the error is "ERROR 1: Failed at creating table for \K2\KCOUPLtemp (Invalid spatial reference.)" worried me.I have searched the GDALAPI and found one CopyLayer parameter that papszOptions is a StringList of name=value options and Options are driver specific. There is a common option to set output layer spatial reference: DST_SRSWKT. The option should be in WKT format.My code is following:
const char* gdbpath = "G:\\GeoMarkData\\K3_1_1.gdb";
GDALAllRegister();
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
CPLSetConfigOption("SHAPE_ENCODING", "");
GDALDriver* driver = GetGDALDriverManager()->GetDriverByName("FileGDB");
if (driver != NULL)
{
GDALDataset* ds = (GDALDataset*)GDALOpenEx(gdbpath, GDAL_OF_VECTOR | GDAL_OF_UPDATE, NULL, NULL, NULL);
if (ds->GetLayerCount() > 0)
{
vector<string> CopiedName;
for (int i = 0; i < ds->GetLayerCount(); i++)
{
CopiedName.push_back(ds->GetLayer(i)->GetName());
}
for (int i = 0; i < CopiedName.size(); i++)
{
OGRLayer* old_layer = ds->GetLayerByName(CopiedName.at(i).c_str());
OGRSpatialReference* oldsp = old_layer->GetSpatialRef();
char* sp = NULL;
oldsp->exportToWkt(&sp);
cout << sp << endl;
string nametemp = CopiedName.at(i);
string copyname = nametemp + "temp";
char** papszOptions = NULL;
papszOptions = CSLSetNameValue(papszOptions, "DST_SRSWKT", sp);
papszOptions = CSLSetNameValue(papszOptions, "FEATURE_DATASET", "K2");
OGRLayer* templayer = ds->CopyLayer(old_layer, copyname.c_str(), papszOptions);
CSLDestroy(papszOptions);
}
}
GDALClose(ds);
}
}
The data is a FileGDB and hava one dataset named “K2”.In "K2",there is a shapefile named "KCOUPL".