Skip to content

Commit

Permalink
SRTMHGT: add support for creating 7201x7201 files
Browse files Browse the repository at this point in the history
Fixes #10627
  • Loading branch information
rouault committed Aug 25, 2024
1 parent 30eaea3 commit 4a4eac4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
3 changes: 3 additions & 0 deletions autotest/gdrivers/srtmhgt.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,6 @@ def test_srtmhgt_all_supported_sizes(tmp_vsimem, width, height, nb_bytes):
if nb_bytes == 2
else gdal.GDT_Float32
)

out_filename = str(tmp_vsimem / "create" / "n00e000.hgt")
gdal.GetDriverByName("SRTMHGT").CreateCopy(out_filename, ds)
3 changes: 3 additions & 0 deletions doc/source/drivers/raster/srtmhgt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ The driver does support creating new files, but the input data must be
exactly formatted as a SRTM-3 or SRTM-1 cell. That is the size, and
bounds must be appropriate for a cell.

Starting with GDAL 3.10, the driver also supports reading and writing 0.5
degree resolution SRTM HGT files.

See Also:

- `SRTM
Expand Down
38 changes: 25 additions & 13 deletions frmts/srtmhgt/srtmhgtdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class SRTMHGTDataset final : public GDALPamDataset
GByte *pabyBuffer = nullptr;
OGRSpatialReference m_oSRS{};

static GDALPamDataset *OpenPAM(GDALOpenInfo *);

public:
SRTMHGTDataset();
virtual ~SRTMHGTDataset();
Expand Down Expand Up @@ -360,6 +362,15 @@ int SRTMHGTDataset::Identify(GDALOpenInfo *poOpenInfo)
/************************************************************************/

GDALDataset *SRTMHGTDataset::Open(GDALOpenInfo *poOpenInfo)
{
return OpenPAM(poOpenInfo);
}

/************************************************************************/
/* OpenPAM() */
/************************************************************************/

GDALPamDataset *SRTMHGTDataset::OpenPAM(GDALOpenInfo *poOpenInfo)
{
if (!Identify(poOpenInfo))
return nullptr;
Expand All @@ -374,7 +385,7 @@ GDALDataset *SRTMHGTDataset::Open(GDALOpenInfo *poOpenInfo)
osFilename += CPLString(fileName).substr(0, 7);
osFilename += ".hgt";
GDALOpenInfo oOpenInfo(osFilename, poOpenInfo->eAccess);
GDALDataset *poDS = Open(&oOpenInfo);
auto poDS = OpenPAM(&oOpenInfo);
if (poDS != nullptr)
{
// override description with the main one
Expand All @@ -392,7 +403,7 @@ GDALDataset *SRTMHGTDataset::Open(GDALOpenInfo *poOpenInfo)
osFilename += CPLString(fileName).substr(0, 7);
osFilename += ".raw";
GDALOpenInfo oOpenInfo(osFilename, poOpenInfo->eAccess);
GDALDataset *poDS = Open(&oOpenInfo);
auto poDS = OpenPAM(&oOpenInfo);
if (poDS != nullptr)
{
// override description with the main one
Expand Down Expand Up @@ -427,15 +438,14 @@ GDALDataset *SRTMHGTDataset::Open(GDALOpenInfo *poOpenInfo)
/* -------------------------------------------------------------------- */
/* Create a corresponding GDALDataset. */
/* -------------------------------------------------------------------- */
SRTMHGTDataset *poDS = new SRTMHGTDataset();
auto poDS = std::make_unique<SRTMHGTDataset>();

poDS->fpImage = poOpenInfo->fpL;
poOpenInfo->fpL = nullptr;

VSIStatBufL fileStat;
if (VSIStatL(poOpenInfo->pszFilename, &fileStat) != 0)
{
delete poDS;
return nullptr;
}

Expand Down Expand Up @@ -498,7 +508,7 @@ GDALDataset *SRTMHGTDataset::Open(GDALOpenInfo *poOpenInfo)
/* -------------------------------------------------------------------- */
/* Create band information object. */
/* -------------------------------------------------------------------- */
SRTMHGTRasterBand *tmpBand = new SRTMHGTRasterBand(poDS, 1, eDT);
SRTMHGTRasterBand *tmpBand = new SRTMHGTRasterBand(poDS.get(), 1, eDT);
poDS->SetBand(1, tmpBand);

/* -------------------------------------------------------------------- */
Expand All @@ -510,9 +520,9 @@ GDALDataset *SRTMHGTDataset::Open(GDALOpenInfo *poOpenInfo)
/* -------------------------------------------------------------------- */
/* Support overviews. */
/* -------------------------------------------------------------------- */
poDS->oOvManager.Initialize(poDS, poOpenInfo->pszFilename);
poDS->oOvManager.Initialize(poDS.get(), poOpenInfo->pszFilename);

return poDS;
return poDS.release();
}

/************************************************************************/
Expand Down Expand Up @@ -597,12 +607,14 @@ GDALDataset *SRTMHGTDataset::CreateCopy(const char *pszFilename,
const int nYSize = poSrcDS->GetRasterYSize();

if (!((nXSize == 1201 && nYSize == 1201) ||
(nXSize == 1801 && nYSize == 3601) ||
(nXSize == 3601 && nYSize == 3601) ||
(nXSize == 3601 && nYSize == 3601) ||
(nXSize == 1801 && nYSize == 3601)))
(nXSize == 7201 && nYSize == 7201)))
{
CPLError(
CE_Failure, CPLE_AppDefined,
"Image dimensions should be 1201x1201, 3601x3601 or 1801x3601.");
CPLError(CE_Failure, CPLE_AppDefined,
"Image dimensions should be 1201x1201, 1801x3601, 3601x3601 "
"or 7201x7201.");
return nullptr;
}

Expand Down Expand Up @@ -691,8 +703,8 @@ GDALDataset *SRTMHGTDataset::CreateCopy(const char *pszFilename,
/* -------------------------------------------------------------------- */
/* Reopen and copy missing information into a PAM file. */
/* -------------------------------------------------------------------- */
GDALPamDataset *poDS =
reinterpret_cast<GDALPamDataset *>(GDALOpen(pszFilename, GA_ReadOnly));
GDALOpenInfo oOpenInfo(pszFilename, GA_ReadOnly);
auto poDS = OpenPAM(&oOpenInfo);

if (poDS)
poDS->CloneInfo(poSrcDS, GCIF_PAM_DEFAULT);
Expand Down

0 comments on commit 4a4eac4

Please sign in to comment.