Skip to content

Commit

Permalink
Merge pull request #10204 from rouault/hdf5_planet_adjustments
Browse files Browse the repository at this point in the history
HDF5-EOS products related adjustments
  • Loading branch information
rouault authored Jun 13, 2024
2 parents e3933fb + cab1cc9 commit 608d6d1
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 38 deletions.
Binary file modified autotest/gdrivers/data/hdf5/fwhm.h5
Binary file not shown.
19 changes: 4 additions & 15 deletions autotest/gdrivers/fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import gdaltest
import pytest

from osgeo import gdal
from osgeo import gdal, osr

pytestmark = pytest.mark.require_driver("FAST")

Expand Down Expand Up @@ -185,20 +185,9 @@ def test_fast_7():
gt = (676565.09, 5, 0, 5348341.5, 0, -5)

# Expected definition of the projection
proj = """PROJCS["UTM Zone 32, Northern Hemisphere",
GEOGCS["Unknown datum based upon the WGS 84 ellipsoid",
DATUM["Not specified (based on WGS 84 spheroid)",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]]],
PRIMEM["Greenwich",0],
UNIT["degree",0.0174532925199433]],
PROJECTION["Transverse_Mercator"],
PARAMETER["latitude_of_origin",0],
PARAMETER["central_meridian",9],
PARAMETER["scale_factor",0.9996],
PARAMETER["false_easting",500000],
PARAMETER["false_northing",0],
UNIT["Meter",1]]"""
srs = osr.SpatialReference()
srs.ImportFromEPSG(32632)
proj = srs.ExportToWkt()

tst.testOpen(check_gt=gt, check_prj=proj)

Expand Down
6 changes: 6 additions & 0 deletions autotest/gdrivers/hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,8 @@ def test_hdf5_band_specific_attribute():
ds.attrs["fwhm"] = [0.01, 0.02]
ds.attrs["fwhm_units"] = "Micrometers"
ds.attrs["bad_band_list"] = [0, 1]
ds.attrs["center_wavelengths"] = [300, 400]
ds.attrs["my_coefficients"] = [1, 2]
f.close()

ds = gdal.Open("data/hdf5/fwhm.h5")
Expand All @@ -1315,11 +1317,15 @@ def test_hdf5_band_specific_attribute():
"fwhm": "0.01",
"fwhm_units": "Micrometers",
"bad_band": "0",
"center_wavelength": "300",
"my_coefficient": "1",
}
assert ds.GetRasterBand(2).GetMetadata_Dict() == {
"fwhm": "0.02",
"fwhm_units": "Micrometers",
"bad_band": "1",
"center_wavelength": "400",
"my_coefficient": "2",
}
ds = None

Expand Down
18 changes: 18 additions & 0 deletions autotest/osr/osr_usgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,21 @@ def test_osr_usgs_2():
and gdal.PackedDMSToDec(params[4]) == pytest.approx(-117.4745429, abs=0.0000005)
and gdal.PackedDMSToDec(params[5]) == pytest.approx(33.76446203, abs=0.0000005)
), "Can not import Lambert Conformal Conic projection."


###############################################################################
# Test the osr.SpatialReference.ImportFromUSGS() function with WGS 84
#


def test_osr_usgs_wgs84():

srs = osr.SpatialReference()
srs.ImportFromEPSG(32631)
(proj_code, zone, params, datum_code) = srs.ExportToUSGS()

srs2 = osr.SpatialReference()
srs2.ImportFromUSGS(proj_code, zone, params, datum_code)

assert srs2.IsSame(srs)
assert srs2.GetAuthorityCode(None) == "32631"
59 changes: 39 additions & 20 deletions frmts/hdf5/hdf5imagedataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1224,34 +1224,53 @@ GDALDataset *HDF5ImageDataset::Open(GDALOpenInfo *poOpenInfo)
{
HDF5Dataset::CreateMetadata(poDS->m_hHDF5, poDS->poH5Objects,
H5G_DATASET, false, aosMetadata);
if (nBands > 1)
if (nBands > 1 && poDS->nRasterXSize != nBands &&
poDS->nRasterYSize != nBands)
{
// Logic specific of Planet data cubes with per-band metadata items
static const struct
// Heuristics to detect non-scalar attributes, that are intended
// to be attached to a specific band.
const CPLStringList aosMetadataDup(aosMetadata);
for (const auto &[pszKey, pszValue] :
cpl::IterateNameValue(aosMetadataDup))
{
const char *pszSrcName;
const char *pszDstName;
} asItems[] = {
{"calibration_coefficients", "calibration_coefficient"},
{"center_wavelengths", "center_wavelength"},
{"fwhm", "fwhm"},
{"bad_band_list", "bad_band"},
};

for (const auto &sItem : asItems)
{
const char *pszVal =
aosMetadata.FetchNameValue(sItem.pszSrcName);
if (pszVal)
const hid_t hAttrID = H5Aopen_name(poDS->dataset_id, pszKey);
const hid_t hAttrSpace = H5Aget_space(hAttrID);
if (H5Sget_simple_extent_ndims(hAttrSpace) == 1 &&
H5Sget_simple_extent_npoints(hAttrSpace) == nBands)
{
CPLStringList aosTokens(CSLTokenizeString2(pszVal, " ", 0));
CPLStringList aosTokens(
CSLTokenizeString2(pszValue, " ", 0));
if (aosTokens.size() == nBands)
{
oMapBandSpecificMetadata[sItem.pszDstName] =
std::string osAttrName(pszKey);
if (osAttrName.size() > strlen("_coefficients") &&
osAttrName.substr(osAttrName.size() -
strlen("_coefficients")) ==
"_coefficients")
{
osAttrName.pop_back();
}
else if (osAttrName.size() > strlen("_wavelengths") &&
osAttrName.substr(osAttrName.size() -
strlen("_wavelengths")) ==
"_wavelengths")
{
osAttrName.pop_back();
}
else if (osAttrName.size() > strlen("_list") &&
osAttrName.substr(osAttrName.size() -
strlen("_list")) == "_list")
{
osAttrName.resize(osAttrName.size() -
strlen("_list"));
}
oMapBandSpecificMetadata[osAttrName] =
std::move(aosTokens);
aosMetadata.SetNameValue(sItem.pszSrcName, nullptr);
aosMetadata.SetNameValue(pszKey, nullptr);
}
}
H5Sclose(hAttrSpace);
H5Aclose(hAttrID);
}
}
}
Expand Down
22 changes: 19 additions & 3 deletions ogr/ogr_srs_usgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,9 +732,14 @@ OGRErr OGRSpatialReference::importFromUSGS(long iProjSys, long iZone,
}
else if (iDatum < NUMBER_OF_USGS_ELLIPSOIDS && aoEllipsUSGS[iDatum])
{
if (OSRGetEllipsoidInfo(aoEllipsUSGS[iDatum], &pszName,
&dfSemiMajor,
&dfInvFlattening) == OGRERR_NONE)
if (aoEllipsUSGS[iDatum] == 7030) // WGS 84 ellipsoid
{
// Assume a WGS 84 datum
SetWellKnownGeogCS("WGS84");
}
else if (OSRGetEllipsoidInfo(aoEllipsUSGS[iDatum], &pszName,
&dfSemiMajor,
&dfInvFlattening) == OGRERR_NONE)
{
SetGeogCS(
CPLString().Printf(
Expand Down Expand Up @@ -772,6 +777,17 @@ OGRErr OGRSpatialReference::importFromUSGS(long iProjSys, long iZone,
if (IsLocal() || IsProjected())
SetLinearUnits(SRS_UL_METER, 1.0);

if (iDatum < NUMBER_OF_USGS_ELLIPSOIDS && aoEllipsUSGS[iDatum] == 7030)
{
if (AutoIdentifyEPSG() == OGRERR_NONE)
{
const char *pszAuthName = GetAuthorityName(nullptr);
const char *pszAuthCode = GetAuthorityCode(nullptr);
if (pszAuthName && pszAuthCode && EQUAL(pszAuthName, "EPSG"))
CPL_IGNORE_RET_VAL(importFromEPSG(atoi(pszAuthCode)));
}
}

return OGRERR_NONE;
}

Expand Down

0 comments on commit 608d6d1

Please sign in to comment.