0

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".

1 Answer 1

1

There is an answer in the related GDAL bug report https://github.com/OSGeo/gdal/issues/6472.

The FileGDB SDK is quite restrictive on the CRS it accepts and it can be tricky to convince it. The best is to use the definition of an official EPSG code. DST_SRSWKT is not a valid options of https://gdal.org/drivers/vector/filegdb.html To override the CRS, you might rather use the GDALVectorTranslate() API (C API of ogr2ogr) with the -a_srs option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.