From db98e7553029bf95501fac0f97c3da2759bad235 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Tue, 28 Jan 2025 18:41:49 +0100 Subject: [PATCH] BYN: remove write support --- .../expected_gdalinfo_formats.txt | 2 +- ...indows_conda_expected_gdalinfo_formats.txt | 2 +- autotest/gdrivers/byn.py | 10 - doc/source/drivers/raster/byn.rst | 6 +- frmts/raw/byndataset.cpp | 330 +----------------- frmts/raw/byndataset.h | 9 - 6 files changed, 5 insertions(+), 354 deletions(-) diff --git a/.github/workflows/ubuntu_24.04/expected_gdalinfo_formats.txt b/.github/workflows/ubuntu_24.04/expected_gdalinfo_formats.txt index cab289a0f1b7..7439aefe7942 100644 --- a/.github/workflows/ubuntu_24.04/expected_gdalinfo_formats.txt +++ b/.github/workflows/ubuntu_24.04/expected_gdalinfo_formats.txt @@ -102,7 +102,7 @@ Supported Formats: (ro:read-only, rw:read-write, +:update, v:virtual-I/O s:subda KRO -raster- (rw+v): KOLOR Raw (*.kro) ROI_PAC -raster- (rw+v): ROI_PAC raster RRASTER -raster- (rw+v): R Raster (*.grd) - BYN -raster- (rw+v): Natural Resources Canada's Geoid (*.byn, *.err) + BYN -raster- (rov): Natural Resources Canada's Geoid (*.byn, *.err) NOAA_B -raster- (rov): NOAA GEOCON/NADCON5 .b format (*.b) NSIDCbin -raster- (rov): NSIDC Sea Ice Concentrations binary (.bin) (*.bin) RIK -raster- (rov): Swedish Grid RIK (.rik) (*.rik) diff --git a/.github/workflows/windows_conda_expected_gdalinfo_formats.txt b/.github/workflows/windows_conda_expected_gdalinfo_formats.txt index b1c830610ce6..e2341cf63acd 100644 --- a/.github/workflows/windows_conda_expected_gdalinfo_formats.txt +++ b/.github/workflows/windows_conda_expected_gdalinfo_formats.txt @@ -104,7 +104,7 @@ Supported Formats: (ro:read-only, rw:read-write, +:update, v:virtual-I/O s:subda KRO -raster- (rw+v): KOLOR Raw (*.kro) ROI_PAC -raster- (rw+v): ROI_PAC raster RRASTER -raster- (rw+v): R Raster (*.grd) - BYN -raster- (rw+v): Natural Resources Canada's Geoid (*.byn, *.err) + BYN -raster- (rov): Natural Resources Canada's Geoid (*.byn, *.err) NOAA_B -raster- (rov): NOAA GEOCON/NADCON5 .b format (*.b) RIK -raster- (rov): Swedish Grid RIK (.rik) (*.rik) USGSDEM -raster- (rwv): USGS Optional ASCII DEM (and CDED) (*.dem) diff --git a/autotest/gdrivers/byn.py b/autotest/gdrivers/byn.py index 3027fb12a724..68417f7bd9f2 100755 --- a/autotest/gdrivers/byn.py +++ b/autotest/gdrivers/byn.py @@ -34,16 +34,6 @@ def test_byn_1(): # -def test_byn_2(): - - tst = gdaltest.GDALTest("BYN", "byn/cgg2013ai08_reduced.byn", 1, 64764) - tst.testCreateCopy(new_filename="tmp/byn_test_2.byn") - - -############################################################################### -# - - def test_byn_invalid_header_bytes(): tst = gdaltest.GDALTest("BYN", "byn/test_invalid_header_bytes.byn", 1, 64764) diff --git a/doc/source/drivers/raster/byn.rst b/doc/source/drivers/raster/byn.rst index efe70ae25e8c..82ae0f99b20b 100644 --- a/doc/source/drivers/raster/byn.rst +++ b/doc/source/drivers/raster/byn.rst @@ -43,10 +43,6 @@ will have variable Data equal to 1 or 3. Driver capabilities ------------------- -.. supports_createcopy:: - -.. supports_create:: - .. supports_georeferencing:: .. supports_virtualio:: @@ -54,7 +50,7 @@ Driver capabilities Factor ------ -When translating from or into BYN file to or from another formats the +When translating from BYN file to or from another formats the scale will affect the result profoundly. Translating to a format that supports Scale (GTIFF for example) will diff --git a/frmts/raw/byndataset.cpp b/frmts/raw/byndataset.cpp index 406e2e7eb98c..4a9c12f951b0 100644 --- a/frmts/raw/byndataset.cpp +++ b/frmts/raw/byndataset.cpp @@ -91,17 +91,6 @@ double BYNRasterBand::GetScale(int *pbSuccess) return (dfFactor != 0.0) ? 1.0 / dfFactor : 0.0; } -/************************************************************************/ -/* SetScale() */ -/************************************************************************/ - -CPLErr BYNRasterBand::SetScale(double dfNewValue) -{ - BYNDataset *poIDS = reinterpret_cast(poDS); - poIDS->hHeader.dfFactor = 1.0 / dfNewValue; - return CE_None; -} - /************************************************************************/ /* ==================================================================== */ /* BYNDataset */ @@ -143,9 +132,6 @@ CPLErr BYNDataset::Close() if (BYNDataset::FlushCache(true) != CE_None) eErr = CE_Failure; - if (GetAccess() == GA_Update) - UpdateHeader(); - if (fpImage != nullptr) { if (VSIFCloseL(fpImage) != 0) @@ -253,7 +239,8 @@ int BYNDataset::Identify(GDALOpenInfo *poOpenInfo) GDALDataset *BYNDataset::Open(GDALOpenInfo *poOpenInfo) { - if (!Identify(poOpenInfo) || poOpenInfo->fpL == nullptr) + if (!Identify(poOpenInfo) || poOpenInfo->fpL == nullptr || + poOpenInfo->eAccess == GA_Update) return nullptr; /* -------------------------------------------------------------------- */ @@ -388,23 +375,6 @@ CPLErr BYNDataset::GetGeoTransform(double *padfTransform) return CE_None; } -/************************************************************************/ -/* SetGeoTransform() */ -/************************************************************************/ - -CPLErr BYNDataset::SetGeoTransform(double *padfTransform) - -{ - if (padfTransform[2] != 0.0 || padfTransform[4] != 0.0) - { - CPLError(CE_Failure, CPLE_AppDefined, - "Attempt to write skewed or rotated geotransform to byn."); - return CE_Failure; - } - memcpy(adfGeoTransform, padfTransform, sizeof(double) * 6); - return CE_None; -} - /************************************************************************/ /* GetSpatialRef() */ /************************************************************************/ @@ -488,76 +458,6 @@ const OGRSpatialReference *BYNDataset::GetSpatialRef() const return nullptr; } -/************************************************************************/ -/* SetSpatialRef() */ -/************************************************************************/ - -CPLErr BYNDataset::SetSpatialRef(const OGRSpatialReference *poSRS) - -{ - if (poSRS == nullptr) - return CE_Failure; - - m_oSRS = *poSRS; - - /* Try to recognize prefefined EPSG compound CS */ - - if (poSRS->IsCompound()) - { - const char *pszAuthName = poSRS->GetAuthorityName("COMPD_CS"); - const char *pszAuthCode = poSRS->GetAuthorityCode("COMPD_CS"); - - if (pszAuthName != nullptr && pszAuthCode != nullptr && - EQUAL(pszAuthName, "EPSG") && - atoi(pszAuthCode) == BYN_DATUM_1_VDATUM_2) - { - hHeader.nVDatum = 2; - hHeader.nDatum = 1; - return CE_None; - } - } - - OGRSpatialReference oSRSTemp; - - /* Try to match GEOGCS */ - - if (poSRS->IsGeographic()) - { - oSRSTemp.importFromEPSG(BYN_DATUM_0); - if (poSRS->IsSameGeogCS(&oSRSTemp)) - hHeader.nDatum = 0; - else - { - oSRSTemp.importFromEPSG(BYN_DATUM_1); - if (poSRS->IsSameGeogCS(&oSRSTemp)) - hHeader.nDatum = 1; - } - } - - /* Try to match VERT_CS */ - - if (poSRS->IsVertical()) - { - oSRSTemp.importFromEPSG(BYN_VDATUM_1); - if (poSRS->IsSameVertCS(&oSRSTemp)) - hHeader.nVDatum = 1; - else - { - oSRSTemp.importFromEPSG(BYN_VDATUM_2); - if (poSRS->IsSameVertCS(&oSRSTemp)) - hHeader.nVDatum = 2; - else - { - oSRSTemp.importFromEPSG(BYN_VDATUM_3); - if (poSRS->IsSameVertCS(&oSRSTemp)) - hHeader.nVDatum = 3; - } - } - } - - return CE_None; -} - /*----------------------------------------------------------------------*/ /* buffer2header() */ /*----------------------------------------------------------------------*/ @@ -642,230 +542,6 @@ void BYNDataset::buffer2header(const GByte *pabyBuf, BYNHeader *pohHeader) #endif } -/*----------------------------------------------------------------------*/ -/* header2buffer() */ -/*----------------------------------------------------------------------*/ - -void BYNDataset::header2buffer(const BYNHeader *pohHeader, GByte *pabyBuf) - -{ - memcpy(pabyBuf, &pohHeader->nSouth, 4); - memcpy(pabyBuf + 4, &pohHeader->nNorth, 4); - memcpy(pabyBuf + 8, &pohHeader->nWest, 4); - memcpy(pabyBuf + 12, &pohHeader->nEast, 4); - memcpy(pabyBuf + 16, &pohHeader->nDLat, 2); - memcpy(pabyBuf + 18, &pohHeader->nDLon, 2); - memcpy(pabyBuf + 20, &pohHeader->nGlobal, 2); - memcpy(pabyBuf + 22, &pohHeader->nType, 2); - memcpy(pabyBuf + 24, &pohHeader->dfFactor, 8); - memcpy(pabyBuf + 32, &pohHeader->nSizeOf, 2); - memcpy(pabyBuf + 34, &pohHeader->nVDatum, 2); - memcpy(pabyBuf + 40, &pohHeader->nDescrip, 2); - memcpy(pabyBuf + 42, &pohHeader->nSubType, 2); - memcpy(pabyBuf + 44, &pohHeader->nDatum, 2); - memcpy(pabyBuf + 46, &pohHeader->nEllipsoid, 2); - memcpy(pabyBuf + 48, &pohHeader->nByteOrder, 2); - memcpy(pabyBuf + 50, &pohHeader->nScale, 2); - memcpy(pabyBuf + 52, &pohHeader->dfWo, 8); - memcpy(pabyBuf + 60, &pohHeader->dfGM, 8); - memcpy(pabyBuf + 68, &pohHeader->nTideSys, 2); - memcpy(pabyBuf + 70, &pohHeader->nRealiz, 2); - memcpy(pabyBuf + 72, &pohHeader->dEpoch, 4); - memcpy(pabyBuf + 76, &pohHeader->nPtType, 2); - -#if defined(CPL_MSB) - CPL_LSBPTR32(pabyBuf); - CPL_LSBPTR32(pabyBuf + 4); - CPL_LSBPTR32(pabyBuf + 8); - CPL_LSBPTR32(pabyBuf + 12); - CPL_LSBPTR16(pabyBuf + 16); - CPL_LSBPTR16(pabyBuf + 18); - CPL_LSBPTR16(pabyBuf + 20); - CPL_LSBPTR16(pabyBuf + 22); - CPL_LSBPTR64(pabyBuf + 24); - CPL_LSBPTR16(pabyBuf + 32); - CPL_LSBPTR16(pabyBuf + 34); - CPL_LSBPTR16(pabyBuf + 40); - CPL_LSBPTR16(pabyBuf + 42); - CPL_LSBPTR16(pabyBuf + 44); - CPL_LSBPTR16(pabyBuf + 46); - CPL_LSBPTR16(pabyBuf + 48); - CPL_LSBPTR16(pabyBuf + 50); - CPL_LSBPTR64(pabyBuf + 52); - CPL_LSBPTR64(pabyBuf + 60); - CPL_LSBPTR16(pabyBuf + 68); - CPL_LSBPTR16(pabyBuf + 70); - CPL_LSBPTR32(pabyBuf + 72); - CPL_LSBPTR16(pabyBuf + 76); -#endif -} - -/************************************************************************/ -/* Create() */ -/************************************************************************/ - -GDALDataset *BYNDataset::Create(const char *pszFilename, int nXSize, int nYSize, - int /* nBands */, GDALDataType eType, - char ** /* papszOptions */) -{ - if (eType != GDT_Int16 && eType != GDT_Int32) - { - CPLError(CE_Failure, CPLE_AppDefined, - "Attempt to create byn file with unsupported data type '%s'.", - GDALGetDataTypeName(eType)); - return nullptr; - } - - /* -------------------------------------------------------------------- */ - /* Check file extension (.byn/.err) */ - /* -------------------------------------------------------------------- */ - - const std::string osExt = CPLGetExtensionSafe(pszFilename); - - if (!EQUAL(osExt.c_str(), "byn") && !EQUAL(osExt.c_str(), "err")) - { - CPLError( - CE_Failure, CPLE_AppDefined, - "Attempt to create byn file with extension other than byn/err."); - return nullptr; - } - - /* -------------------------------------------------------------------- */ - /* Try to create the file. */ - /* -------------------------------------------------------------------- */ - - VSILFILE *fp = VSIFOpenL(pszFilename, "wb+"); - if (fp == nullptr) - { - CPLError(CE_Failure, CPLE_OpenFailed, - "Attempt to create file `%s' failed.\n", pszFilename); - return nullptr; - } - - /* -------------------------------------------------------------------- */ - /* Write an empty header. */ - /* -------------------------------------------------------------------- */ - - GByte abyBuf[BYN_HDR_SZ] = {'\0'}; - - /* Load header with some commum values */ - - BYNHeader hHeader = {0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, - 0, 0, 0, 0, 0, 0.0, 0.0, 0, 0, 0.0, 0}; - - /* Set temporary values on header */ - - hHeader.nSouth = 0; - hHeader.nNorth = nYSize - 2; - hHeader.nWest = 0; - hHeader.nEast = nXSize - 2; - hHeader.nDLat = 1; - hHeader.nDLon = 1; - hHeader.nSizeOf = static_cast(GDALGetDataTypeSizeBytes(eType)); - - /* Prepare buffer for writing */ - - header2buffer(&hHeader, abyBuf); - - /* Write initial header */ - - VSIFWriteL(abyBuf, BYN_HDR_SZ, 1, fp); - VSIFCloseL(fp); - - return GDALDataset::FromHandle(GDALOpen(pszFilename, GA_Update)); -} - -/************************************************************************/ -/* UpdateHeader() */ -/************************************************************************/ - -void BYNDataset::UpdateHeader() -{ - double dfDLon = (adfGeoTransform[1] * 3600.0); - double dfDLat = (adfGeoTransform[5] * 3600.0 * -1); - double dfWest = (adfGeoTransform[0] * 3600.0) + (dfDLon / 2); - double dfNorth = (adfGeoTransform[3] * 3600.0) - (dfDLat / 2); - double dfSouth = dfNorth - ((nRasterYSize - 1) * dfDLat); - double dfEast = dfWest + ((nRasterXSize - 1) * dfDLon); - - if (hHeader.nScale == 1) - { - dfSouth /= BYN_SCALE; - dfNorth /= BYN_SCALE; - dfWest /= BYN_SCALE; - dfEast /= BYN_SCALE; - dfDLat /= BYN_SCALE; - dfDLon /= BYN_SCALE; - } - - hHeader.nSouth = static_cast(dfSouth); - hHeader.nNorth = static_cast(dfNorth); - hHeader.nWest = static_cast(dfWest); - hHeader.nEast = static_cast(dfEast); - hHeader.nDLat = static_cast(dfDLat); - hHeader.nDLon = static_cast(dfDLon); - - GByte abyBuf[BYN_HDR_SZ]; - - header2buffer(&hHeader, abyBuf); - - const char *pszValue = GetMetadataItem("GLOBAL"); - if (pszValue != nullptr) - hHeader.nGlobal = static_cast(atoi(pszValue)); - - pszValue = GetMetadataItem("TYPE"); - if (pszValue != nullptr) - hHeader.nType = static_cast(atoi(pszValue)); - - pszValue = GetMetadataItem("DESCRIPTION"); - if (pszValue != nullptr) - hHeader.nDescrip = static_cast(atoi(pszValue)); - - pszValue = GetMetadataItem("SUBTYPE"); - if (pszValue != nullptr) - hHeader.nSubType = static_cast(atoi(pszValue)); - - pszValue = GetMetadataItem("WO"); - if (pszValue != nullptr) - hHeader.dfWo = CPLAtof(pszValue); - - pszValue = GetMetadataItem("GM"); - if (pszValue != nullptr) - hHeader.dfGM = CPLAtof(pszValue); - - pszValue = GetMetadataItem("TIDESYSTEM"); - if (pszValue != nullptr) - hHeader.nTideSys = static_cast(atoi(pszValue)); - - pszValue = GetMetadataItem("REALIZATION"); - if (pszValue != nullptr) - hHeader.nRealiz = static_cast(atoi(pszValue)); - - pszValue = GetMetadataItem("EPOCH"); - if (pszValue != nullptr) - hHeader.dEpoch = static_cast(CPLAtof(pszValue)); - - pszValue = GetMetadataItem("PTTYPE"); - if (pszValue != nullptr) - hHeader.nPtType = static_cast(atoi(pszValue)); - - CPL_IGNORE_RET_VAL(VSIFSeekL(fpImage, 0, SEEK_SET)); - CPL_IGNORE_RET_VAL(VSIFWriteL(abyBuf, BYN_HDR_SZ, 1, fpImage)); - - /* GDALPam metadata update */ - - SetMetadataItem("GLOBAL", CPLSPrintf("%d", hHeader.nGlobal), "BYN"); - SetMetadataItem("TYPE", CPLSPrintf("%d", hHeader.nType), "BYN"); - SetMetadataItem("DESCRIPTION", CPLSPrintf("%d", hHeader.nDescrip), "BYN"); - SetMetadataItem("SUBTYPE", CPLSPrintf("%d", hHeader.nSubType), "BYN"); - SetMetadataItem("WO", CPLSPrintf("%g", hHeader.dfWo), "BYN"); - SetMetadataItem("GM", CPLSPrintf("%g", hHeader.dfGM), "BYN"); - SetMetadataItem("TIDESYSTEM", CPLSPrintf("%d", hHeader.nTideSys), "BYN"); - SetMetadataItem("REALIZATION", CPLSPrintf("%d", hHeader.nRealiz), "BYN"); - SetMetadataItem("EPOCH", CPLSPrintf("%g", hHeader.dEpoch), "BYN"); - SetMetadataItem("PTTYPE", CPLSPrintf("%d", hHeader.nPtType), "BYN"); -} - /************************************************************************/ /* GDALRegister_BYN() */ /************************************************************************/ @@ -885,11 +561,9 @@ void GDALRegister_BYN() poDriver->SetMetadataItem(GDAL_DMD_EXTENSIONS, "byn err"); poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES"); poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/raster/byn.html"); - poDriver->SetMetadataItem(GDAL_DMD_CREATIONDATATYPES, "Int16 Int32"); poDriver->pfnOpen = BYNDataset::Open; poDriver->pfnIdentify = BYNDataset::Identify; - poDriver->pfnCreate = BYNDataset::Create; GetGDALDriverManager()->RegisterDriver(poDriver); } diff --git a/frmts/raw/byndataset.h b/frmts/raw/byndataset.h index ef2ecf8eaf71..42c49f513423 100644 --- a/frmts/raw/byndataset.h +++ b/frmts/raw/byndataset.h @@ -194,11 +194,8 @@ class BYNDataset final : public RawDataset mutable OGRSpatialReference m_oSRS{}; BYNHeader hHeader; - void UpdateHeader(); - CPL_DISALLOW_COPY_ASSIGN(BYNDataset) - static void header2buffer(const BYNHeader *pohHeader, GByte *pabyBuf); static void buffer2header(const GByte *pabyBuf, BYNHeader *pohHeader); CPLErr Close() override; @@ -208,16 +205,11 @@ class BYNDataset final : public RawDataset ~BYNDataset(); CPLErr GetGeoTransform(double *padfTransform) override; - CPLErr SetGeoTransform(double *padfTransform) override; const OGRSpatialReference *GetSpatialRef() const override; - CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override; static GDALDataset *Open(GDALOpenInfo *); static int Identify(GDALOpenInfo *); - static GDALDataset *Create(const char *pszFilename, int nXSize, int nYSize, - int nBands, GDALDataType eType, - char **papszOptions); }; /************************************************************************/ @@ -240,7 +232,6 @@ class BYNRasterBand final : public RawRasterBand double GetNoDataValue(int *pbSuccess = nullptr) override; double GetScale(int *pbSuccess = nullptr) override; - CPLErr SetScale(double dfNewValue) override; }; #endif // GDAL_FRMTS_RAW_BYNDATASET_H_INCLUDED