Skip to content

Commit

Permalink
JP2OpenJPEG GMLJP2 output: avoid creating temporary 'inner' /vsimem/ …
Browse files Browse the repository at this point in the history
…directories
  • Loading branch information
rouault authored and dbaston committed Oct 2, 2023
1 parent 9a69968 commit 438a28a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 25 deletions.
12 changes: 8 additions & 4 deletions autotest/gdrivers/jp2openjpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2575,8 +2575,10 @@ def test_jp2openjpeg_45():
del out_ds

# Now do the checks
dircontent = gdal.ReadDir("/vsimem/gmljp2")
assert dircontent is None
dircontent = gdal.ReadDir("/vsimem/")
if dircontent:
for filename in dircontent:
assert not filename.startswith("gmljp2")

ds = gdal.Open("/vsimem/jp2openjpeg_45.jp2")
gmljp2 = ds.GetMetadata_List("xml:gml.root-instance")[0]
Expand Down Expand Up @@ -2741,8 +2743,10 @@ def test_jp2openjpeg_45():
)
del out_ds

dircontent = gdal.ReadDir("/vsimem/gmljp2")
assert dircontent is None
dircontent = gdal.ReadDir("/vsimem/")
if dircontent:
for filename in dircontent:
assert not filename.startswith("gmljp2")

ds = ogr.Open("/vsimem/jp2openjpeg_45.jp2")
assert ds.GetLayerCount() == 1
Expand Down
14 changes: 4 additions & 10 deletions gcore/gdaljp2abstractdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ void GDALJP2AbstractDataset::LoadVectorLayers(int bOpenRemoteResources)

if (psFC != nullptr)
{
osGMLTmpFile = CPLSPrintf("/vsimem/gmljp2/%p/my.gml", this);
osGMLTmpFile = CPLSPrintf("/vsimem/gmljp2_%p/my.gml", this);
// Create temporary .gml file.
CPLSerializeXMLTreeToFile(psFC, osGMLTmpFile);
}
Expand Down Expand Up @@ -490,7 +490,7 @@ void GDALJP2AbstractDataset::LoadVectorLayers(int bOpenRemoteResources)
if (papszBoxData != nullptr)
{
osXSDTmpFile = CPLSPrintf(
"/vsimem/gmljp2/%p/my.xsd", this);
"/vsimem/gmljp2_%p/my.xsd", this);
CPL_IGNORE_RET_VAL(
VSIFCloseL(VSIFileFromMemBuffer(
osXSDTmpFile,
Expand Down Expand Up @@ -546,9 +546,6 @@ void GDALJP2AbstractDataset::LoadVectorLayers(int bOpenRemoteResources)
poSrcLyr->GetName());
poMemDS->CopyLayer(poSrcLyr, pszLayerName, nullptr);
}

// If there was no schema, a .gfs might have been generated.
VSIUnlink(CPLSPrintf("/vsimem/gmljp2/%p/my.gfs", this));
}
}
else
Expand All @@ -557,10 +554,7 @@ void GDALJP2AbstractDataset::LoadVectorLayers(int bOpenRemoteResources)
"No GML driver found to read feature collection");
}

if (!STARTS_WITH(osGMLTmpFile, "/vsicurl/"))
VSIUnlink(osGMLTmpFile);
if (!osXSDTmpFile.empty())
VSIUnlink(osXSDTmpFile);
VSIRmdirRecursive(CPLSPrintf("/vsimem/gmljp2_%p", this));
}
}

Expand Down Expand Up @@ -599,7 +593,7 @@ void GDALJP2AbstractDataset::LoadVectorLayers(int bOpenRemoteResources)
// Create temporary .kml file.
CPLXMLNode *const psKML = psGCorGMLJP2FeaturesChildIter->psChild;
CPLString osKMLTmpFile(
CPLSPrintf("/vsimem/gmljp2/%p/my.kml", this));
CPLSPrintf("/vsimem/gmljp2_%p_my.kml", this));
CPLSerializeXMLTreeToFile(psKML, osKMLTmpFile);

GDALDatasetUniquePtr poTmpDS(GDALDataset::Open(
Expand Down
15 changes: 4 additions & 11 deletions gcore/gdaljp2metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2587,7 +2587,6 @@ GDALJP2Box *GDALJP2Metadata::CreateGMLJP2V2(int nXSize, int nYSize,
/* -------------------------------------------------------------------- */
/* Process metadata, annotations and features collections. */
/* -------------------------------------------------------------------- */
std::vector<CPLString> aosTmpFiles;
if (!aoMetadata.empty() || !aoAnnotations.empty() || !aoGMLFiles.empty() ||
!aoStyles.empty() || !aoExtensions.empty())
{
Expand Down Expand Up @@ -2748,7 +2747,7 @@ GDALJP2Box *GDALJP2Metadata::CreateGMLJP2V2(int nXSize, int nYSize,
if (hSrcDS)
{
CPLString osTmpFile =
CPLSPrintf("/vsimem/gmljp2/%p/%d/%s.gml", this, i,
CPLSPrintf("/vsimem/gmljp2_%p/%d/%s.gml", this, i,
CPLGetBasename(aoGMLFiles[i].osFile));
char **papszOptions = nullptr;
papszOptions =
Expand Down Expand Up @@ -2783,8 +2782,6 @@ GDALJP2Box *GDALJP2Metadata::CreateGMLJP2V2(int nXSize, int nYSize,
CPLXMLTreeCloser(CPLParseXMLFile(osTmpFile));
aoGMLFiles[i].osFile = osTmpFile;
VSIUnlink(osTmpFile);
aosTmpFiles.emplace_back(
CPLResetExtension(osTmpFile, "xsd"));
}
else
{
Expand Down Expand Up @@ -2858,9 +2855,8 @@ GDALJP2Box *GDALJP2Metadata::CreateGMLJP2V2(int nXSize, int nYSize,
!aoGMLFiles[i].osRemoteResource.empty())
{
osTmpFile =
CPLSPrintf("/vsimem/gmljp2/%p/%d/%s.gml", this, i,
CPLSPrintf("/vsimem/gmljp2_%p/%d/%s.gml", this, i,
CPLGetBasename(aoGMLFiles[i].osFile));
aosTmpFiles.push_back(osTmpFile);

GMLJP2V2BoxDesc oDesc;
oDesc.osFile = osTmpFile;
Expand Down Expand Up @@ -3039,7 +3035,7 @@ GDALJP2Box *GDALJP2Metadata::CreateGMLJP2V2(int nXSize, int nYSize,
if (hSrcDS)
{
CPLString osTmpFile =
CPLSPrintf("/vsimem/gmljp2/%p/%d/%s.kml", this, i,
CPLSPrintf("/vsimem/gmljp2_%p/%d/%s.kml", this, i,
CPLGetBasename(aoAnnotations[i].osFile));
char **papszOptions = nullptr;
if (aoAnnotations.size() > 1)
Expand Down Expand Up @@ -3255,10 +3251,7 @@ GDALJP2Box *GDALJP2Metadata::CreateGMLJP2V2(int nXSize, int nYSize,
for (auto &poGMLBox : apoGMLBoxes)
delete poGMLBox;

for (const auto &osTmpFile : aosTmpFiles)
{
VSIUnlink(osTmpFile);
}
VSIRmdirRecursive(CPLSPrintf("/vsimem/gmljp2_%p", this));

return poGMLData;
}
Expand Down

0 comments on commit 438a28a

Please sign in to comment.