Skip to content

Commit

Permalink
GPKG: make CreateCopy() work on vector datasets
Browse files Browse the repository at this point in the history
Fixes #11282, which is due to a9745e5 that mapped CopyDataSource() onto
CreateCopy(), but the GPKG driver implementation of CreateCopy() wasn't ready
to deal with vector dataset.
  • Loading branch information
rouault committed Nov 15, 2024
1 parent eebbd64 commit 759ddc8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
15 changes: 15 additions & 0 deletions autotest/ogr/ogr_gpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10617,6 +10617,21 @@ def test_ogr_gpkg_ST_Length_on_ellipsoid(tmp_vsimem):
assert f[0] is None


###############################################################################
# Test that CreateCopy() works on a vector dataset
# Test fix for https://github.com/OSGeo/gdal/issues/11282


@gdaltest.enable_exceptions()
@pytest.mark.usefixtures("tpoly")
def test_ogr_gpkg_CreateCopy(gpkg_ds, tmp_vsimem):

tmpfilename = tmp_vsimem / "test_ogr_gpkg_CreateCopy.gpkg"

out_ds = gdal.GetDriverByName("GPKG").CreateCopy(tmpfilename, gpkg_ds)
assert out_ds.GetLayerCount() == 1


###############################################################################
# Test LAUNDER=YES layer creation option

Expand Down
18 changes: 16 additions & 2 deletions ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6005,6 +6005,21 @@ GDALDataset *GDALGeoPackageDataset::CreateCopy(const char *pszFilename,
GDALProgressFunc pfnProgress,
void *pProgressData)
{
const int nBands = poSrcDS->GetRasterCount();
if (nBands == 0)
{
GDALDataset *poDS = nullptr;
GDALDriver *poThisDriver =
GDALDriver::FromHandle(GDALGetDriverByName("GPKG"));
if (poThisDriver != nullptr)
{
poDS = poThisDriver->DefaultCreateCopy(pszFilename, poSrcDS,
bStrict, papszOptions,
pfnProgress, pProgressData);
}
return poDS;
}

const char *pszTilingScheme =
CSLFetchNameValueDef(papszOptions, "TILING_SCHEME", "CUSTOM");

Expand All @@ -6018,7 +6033,6 @@ GDALDataset *GDALGeoPackageDataset::CreateCopy(const char *pszFilename,
apszUpdatedOptions.SetNameValue("RASTER_TABLE", osBasename);
}

const int nBands = poSrcDS->GetRasterCount();
if (nBands != 1 && nBands != 2 && nBands != 3 && nBands != 4)
{
CPLError(CE_Failure, CPLE_NotSupported,
Expand All @@ -6045,7 +6059,7 @@ GDALDataset *GDALGeoPackageDataset::CreateCopy(const char *pszFilename,

GDALGeoPackageDataset *poDS = nullptr;
GDALDriver *poThisDriver =
reinterpret_cast<GDALDriver *>(GDALGetDriverByName("GPKG"));
GDALDriver::FromHandle(GDALGetDriverByName("GPKG"));
if (poThisDriver != nullptr)
{
apszUpdatedOptions.SetNameValue("SKIP_HOLES", "YES");
Expand Down

0 comments on commit 759ddc8

Please sign in to comment.