Skip to content

Commit

Permalink
OgrFileImport: Add global SRS override
Browse files Browse the repository at this point in the history
Adds API which e.g. allows to use "WGS 84 (G1762)"
when GDAL would only offer "WGS 84".
Task: GH-1264 (NAD83 vs. WGS84)
  • Loading branch information
dg0yt committed Jan 2, 2021
1 parent 43b409f commit 1f1c0c4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/gdal/ogr_file_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,39 @@ void OgrFileImport::setGeoreferencingImportEnabled(bool enabled)



QString OgrFileImport::overrideCrs() const
{
return override_srs_spec;
}

bool OgrFileImport::setOverrideCrs(const QString& spec)
{
if (spec.isEmpty())
{
override_srs_spec.clear();
override_srs.reset();
return true;
}

auto srs = ogr::unique_srs(OSRNewSpatialReference(nullptr));
auto spec_latin1 = spec.toLatin1();
#ifdef PROJ_ISSUE_1573
// Cf. https://github.com/OSGeo/PROJ/pull/1573
spec_latin1.replace("+datum=potsdam", "+ellps=bessel +nadgrids=@BETA2007.gsb");
#endif
if (auto error = OSRImportFromProj4(srs.get(), spec_latin1))
{
addWarning(tr("Unable to setup \"%1\" SRS for GDAL: %2")
.arg(spec, QString::number(error)));
return false;
}
override_srs_spec = spec;
override_srs = std::move(srs);
return true;
}



ogr::unique_srs OgrFileImport::srsFromMap()
{
auto srs = ogr::unique_srs(OSRNewSpatialReference(nullptr));
Expand Down Expand Up @@ -1315,6 +1348,11 @@ bool OgrFileImport::setSRS(OGRSpatialReferenceH srs)
return true;
}

if (override_srs)
{
srs = override_srs.get();
}

if (!srs)
{
data_srs = {};
Expand Down
16 changes: 16 additions & 0 deletions src/gdal/ogr_file_format_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,19 @@ class OgrFileImport : public Importer
void setGeoreferencingImportEnabled(bool enabled);


/**
* Returns the CRS specification which overrides the SRS information from the data or driver.
*/
QString overrideCrs() const;

/**
* Set a CRS which overrides the SRS information from the data or driver.
*
* Returns true on success.
*/
bool setOverrideCrs(const QString& spec);


/**
* Tests if the file's spatial references can be used with the given georeferencing.
*
Expand Down Expand Up @@ -353,6 +366,9 @@ class OgrFileImport : public Importer

OGRSpatialReferenceH data_srs = {};

QString override_srs_spec;
ogr::unique_srs override_srs;

ogr::unique_transformation data_transform;

ogr::unique_stylemanager manager;
Expand Down

0 comments on commit 1f1c0c4

Please sign in to comment.