Skip to content

Commit

Permalink
Merge pull request OSGeo#10326 from OSGeo/backport-10318-to-release/3.9
Browse files Browse the repository at this point in the history
[Backport release/3.9] LIBKML: fix writing a .kmz to cloud storage
  • Loading branch information
rouault authored Jun 30, 2024
2 parents 7140b34 + a6bd2c0 commit dce51a7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
11 changes: 11 additions & 0 deletions autotest/ogr/ogr_libkml.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,17 @@ def test_ogr_libkml_write_kmz_use_doc_off(tmp_vsimem):
ogr_libkml_check_write(tmp_vsimem / "libkml_use_doc_off.kmz")


def test_ogr_libkml_write_kmz_simulate_cloud(tmp_vsimem):
with gdal.config_option("CPL_VSIL_USE_TEMP_FILE_FOR_RANDOM_WRITE", "FORCED"):
ogr_libkml_write(tmp_vsimem / "test_ogr_libkml_write_kmz_simulate_cloud.kmz")

ogr_libkml_check_write(tmp_vsimem / "test_ogr_libkml_write_kmz_simulate_cloud.kmz")

with gdal.config_option("CPL_VSIL_USE_TEMP_FILE_FOR_RANDOM_WRITE", "FORCED"):
with pytest.raises(Exception):
ogr_libkml_write("/i_do/not/exist.kmz")


###############################################################################
# Test reading attributes with XML content in them
#
Expand Down
24 changes: 23 additions & 1 deletion ogr/ogrsf_frmts/libkml/ogrlibkmldatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,16 @@ static KmlPtr OGRLIBKMLCreateOGCKml22(KmlFactory *poFactory,

bool OGRLIBKMLDataSource::WriteKmz()
{
void *hZIP = CPLCreateZip(m_pszName, nullptr);
std::string osTmpFilename;
if (!VSISupportsRandomWrite(m_pszName, false) ||
EQUAL(CPLGetConfigOption("CPL_VSIL_USE_TEMP_FILE_FOR_RANDOM_WRITE", ""),
"FORCED"))
{
osTmpFilename = CPLGenerateTempFilename(CPLGetBasename(m_pszName));
}

void *hZIP = CPLCreateZip(
osTmpFilename.empty() ? m_pszName : osTmpFilename.c_str(), nullptr);

if (!hZIP)
{
Expand Down Expand Up @@ -536,6 +545,19 @@ bool OGRLIBKMLDataSource::WriteKmz()
}

CPLCloseZip(hZIP);

if (!osTmpFilename.empty())
{
if (bRet)
{
bRet = CPLCopyFile(m_pszName, osTmpFilename.c_str()) == 0;
if (!bRet)
CPLError(CE_Failure, CPLE_FileIO,
"Cannot copy temporary file to %s", m_pszName);
}
VSIUnlink(osTmpFilename.c_str());
}

return bRet;
}

Expand Down

0 comments on commit dce51a7

Please sign in to comment.