Skip to content

Commit

Permalink
STACIT: STAC 1.1 support
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed Sep 11, 2024
1 parent 5999d8b commit c128125
Showing 1 changed file with 48 additions and 32 deletions.
80 changes: 48 additions & 32 deletions frmts/stacit/stacitdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct AssetSetByProjection
struct Asset
{
std::string osName{};
CPLJSONArray eoBands{};
CPLJSONArray bands{};
std::map<std::string, AssetSetByProjection> assets{};
};

Expand Down Expand Up @@ -139,8 +139,7 @@ int STACITDataset::Identify(GDALOpenInfo *poOpenInfo)
return pszHeader[0] == '{';
}

if (strstr(pszHeader, "\"stac_version\"") != nullptr &&
strstr(pszHeader, "\"proj:transform\"") != nullptr)
if (strstr(pszHeader, "\"stac_version\"") != nullptr)
{
return true;
}
Expand Down Expand Up @@ -234,34 +233,42 @@ static void ParseAsset(const CPLJSONObject &jAsset,
return oProperties[pszName];
};

auto oProjEPSG = GetAssetOrFeatureProperty("proj:epsg");
std::string osProjUserString;
if (oProjEPSG.IsValid() && oProjEPSG.GetType() != CPLJSONObject::Type::Null)
auto oProjCode = GetAssetOrFeatureProperty("proj:code");
if (oProjCode.IsValid() && oProjCode.GetType() != CPLJSONObject::Type::Null)
{
osProjUserString = "EPSG:" + oProjEPSG.ToString();
osProjUserString = oProjCode.ToString();
}
else
{
auto oProjWKT2 = GetAssetOrFeatureProperty("proj:wkt2");
if (oProjWKT2.IsValid() &&
oProjWKT2.GetType() == CPLJSONObject::Type::String)
else {
auto oProjEPSG = GetAssetOrFeatureProperty("proj:epsg");
if (oProjEPSG.IsValid() &&
oProjEPSG.GetType() != CPLJSONObject::Type::Null)
{
osProjUserString = oProjWKT2.ToString();
osProjUserString = "EPSG:" + oProjEPSG.ToString();
}
else
{
auto oProjPROJJSON = GetAssetOrFeatureProperty("proj:projjson");
if (oProjPROJJSON.IsValid() &&
oProjPROJJSON.GetType() == CPLJSONObject::Type::Object)
auto oProjWKT2 = GetAssetOrFeatureProperty("proj:wkt2");
if (oProjWKT2.IsValid() &&
oProjWKT2.GetType() == CPLJSONObject::Type::String)
{
osProjUserString = oProjPROJJSON.ToString();
osProjUserString = oProjWKT2.ToString();
}
else
{
CPLDebug("STACIT",
"Skipping asset %s that lacks a valid CRS member",
osAssetName.c_str());
return;
auto oProjPROJJSON = GetAssetOrFeatureProperty("proj:projjson");
if (oProjPROJJSON.IsValid() &&
oProjPROJJSON.GetType() == CPLJSONObject::Type::Object)
{
osProjUserString = oProjPROJJSON.ToString();
}
else
{
CPLDebug("STACIT",
"Skipping asset %s that lacks a valid CRS member",
osAssetName.c_str());
return;
}
}
}
}
Expand Down Expand Up @@ -396,7 +403,13 @@ static void ParseAsset(const CPLJSONObject &jAsset,
{
Asset asset;
asset.osName = osAssetName;
asset.eoBands = jAsset.GetArray("eo:bands");
if (jAsset.Has("bands"))
{
asset.bands = jAsset.GetArray("bands");
}
else {
asset.bands = jAsset.GetArray("eo:bands");
}

collection.assets[osAssetName] = std::move(asset);
}
Expand Down Expand Up @@ -594,34 +607,37 @@ bool STACITDataset::SetupDataset(
poVRTBand->SetColorInterpretation(eInterp);

// Set band properties
if (asset.eoBands.IsValid() &&
asset.eoBands.Size() == poItemDS->GetRasterCount())
if (asset.bands.IsValid() &&
asset.bands.Size() == poItemDS->GetRasterCount())
{
const auto &eoBand = asset.eoBands[i];
const auto osBandName = eoBand["name"].ToString();
const auto &band = asset.bands[i];
const auto osBandName = band["name"].ToString();
if (!osBandName.empty())
poVRTBand->SetDescription(osBandName.c_str());

if (eInterp != GCI_Undefined)
{
const auto osCommonName = eoBand["common_name"].ToString();
std::string osCommonName;
if (band.Has("eo:common_name"))
osCommonName = band["eo:common_name"].ToString();
else
osCommonName = band["common_name"].ToString();
if (osCommonName == "red")
poVRTBand->SetColorInterpretation(GCI_RedBand);
else if (osCommonName == "green")
poVRTBand->SetColorInterpretation(GCI_GreenBand);
else if (osCommonName == "blue")
poVRTBand->SetColorInterpretation(GCI_BlueBand);
else if (osCommonName == "alpha")
poVRTBand->SetColorInterpretation(GCI_AlphaBand);
}

for (const auto &eoBandChild : eoBand.GetChildren())
for (const auto &bandChild : band.GetChildren())
{
const auto osChildName = eoBandChild.GetName();
if (osChildName != "name" && osChildName != "common_name")
const auto osChildName = bandChild.GetName();
if (osChildName != "name" && osChildName != "common_name"
&& osChildName != "eo:common_name")
{
poVRTBand->SetMetadataItem(osChildName.c_str(),
eoBandChild.ToString().c_str());
bandChild.ToString().c_str());
}
}
}
Expand Down

0 comments on commit c128125

Please sign in to comment.