Skip to content

Commit

Permalink
Add a GDALJP2Metadata::IsSRSCompatible() method (refs OSGeo#9223)
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Feb 26, 2024
1 parent b308b64 commit 4e2f3a8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
68 changes: 40 additions & 28 deletions gcore/gdaljp2metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,32 @@ GDALJP2Box *GDALJP2Metadata::CreateJP2GeoTIFF()
return poBox;
}

/************************************************************************/
/* IsSRSCompatible() */
/************************************************************************/

/* Returns true if the SRS can be references through a EPSG code, or encoded
* as a GML SRS
*/
bool GDALJP2Metadata::IsSRSCompatible(const OGRSpatialReference *poSRS)
{
const char *pszAuthName = poSRS->GetAuthorityName(nullptr);
const char *pszAuthCode = poSRS->GetAuthorityCode(nullptr);

if (pszAuthName && pszAuthCode && EQUAL(pszAuthName, "epsg"))
{
if (atoi(pszAuthCode))
return true;
}

CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler);
CPLErrorStateBackuper oErrorStateBackuper;
char *pszGMLDef = nullptr;
const bool bRet = (poSRS->exportToXML(&pszGMLDef, nullptr) == OGRERR_NONE);
CPLFree(pszGMLDef);
return bRet;
}

/************************************************************************/
/* GetGMLJP2GeoreferencingInfo() */
/************************************************************************/
Expand All @@ -1269,43 +1295,28 @@ void GDALJP2Metadata::GetGMLJP2GeoreferencingInfo(
bNeedAxisFlip = false;
OGRSpatialReference oSRS(m_oSRS);

if (oSRS.IsProjected())
{
const char *pszAuthName = oSRS.GetAuthorityName("PROJCS");
const char *pszAuthName = oSRS.GetAuthorityName(nullptr);
const char *pszAuthCode = oSRS.GetAuthorityCode(nullptr);

if (pszAuthName != nullptr && EQUAL(pszAuthName, "epsg"))
{
nEPSGCode = atoi(oSRS.GetAuthorityCode("PROJCS"));
}
}
else if (oSRS.IsGeographic())
if (pszAuthName && pszAuthCode && EQUAL(pszAuthName, "epsg"))
{
const char *pszAuthName = oSRS.GetAuthorityName("GEOGCS");

if (pszAuthName != nullptr && EQUAL(pszAuthName, "epsg"))
{
nEPSGCode = atoi(oSRS.GetAuthorityCode("GEOGCS"));
}
nEPSGCode = atoi(pszAuthCode);
}

// Save error state as importFromEPSGA() will call CPLReset()
CPLErrorNum errNo = CPLGetLastErrorNo();
CPLErr eErr = CPLGetLastErrorType();
CPLString osLastErrorMsg = CPLGetLastErrorMsg();

// Determine if we need to flip axis. Reimport from EPSG and make
// sure not to strip axis definitions to determine the axis order.
if (nEPSGCode != 0 && oSRS.importFromEPSGA(nEPSGCode) == OGRERR_NONE)
{
if (oSRS.EPSGTreatsAsLatLong() || oSRS.EPSGTreatsAsNorthingEasting())
CPLErrorStateBackuper oErrorStateBackuper;
// Determine if we need to flip axis. Reimport from EPSG and make
// sure not to strip axis definitions to determine the axis order.
if (nEPSGCode != 0 && oSRS.importFromEPSG(nEPSGCode) == OGRERR_NONE)
{
bNeedAxisFlip = true;
if (oSRS.EPSGTreatsAsLatLong() ||
oSRS.EPSGTreatsAsNorthingEasting())
{
bNeedAxisFlip = true;
}
}
}

// Restore error state
CPLErrorSetState(eErr, errNo, osLastErrorMsg);

/* -------------------------------------------------------------------- */
/* Prepare coverage origin and offset vectors. Take axis */
/* order into account if needed. */
Expand Down Expand Up @@ -1369,6 +1380,7 @@ void GDALJP2Metadata::GetGMLJP2GeoreferencingInfo(
{
char *pszGMLDef = nullptr;

CPLErrorStateBackuper oErrorStateBackuper;
if (oSRS.exportToXML(&pszGMLDef, nullptr) == OGRERR_NONE)
{
char *pszWKT = nullptr;
Expand Down
2 changes: 2 additions & 0 deletions gcore/gdaljp2metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ class CPL_DLL GDALJP2Metadata
static GDALJP2Box *CreateIPRBox(GDALDataset *poSrcDS);
static int IsUUID_MSI(const GByte *abyUUID);
static int IsUUID_XMP(const GByte *abyUUID);

static bool IsSRSCompatible(const OGRSpatialReference *poSRS);
};

CPLXMLNode *GDALGetJPEG2000Structure(const char *pszFilename, VSILFILE *fp,
Expand Down

0 comments on commit 4e2f3a8

Please sign in to comment.