Skip to content

Commit

Permalink
Fix gdalwarp / exporting to WKT when a projection that isn't supporte…
Browse files Browse the repository at this point in the history
…d in WKT1 is involved (fixes OSGeo#4133)
  • Loading branch information
rouault committed Jul 28, 2021
1 parent e96e70e commit 0ea3123
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
5 changes: 3 additions & 2 deletions autotest/gcore/tiff_srs.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,9 @@ def test_tiff_srs_read_epsg32631_4979_geotiff1_1():

def test_tiff_srs_write_vertical_perspective():

if osr.GetPROJVersionMajor() * 100 + osr.GetPROJVersionMinor() < 700:
pytest.skip('requires PROJ 7 or later')

ds = gdal.GetDriverByName('GTiff').Create('/vsimem/src.tif', 1, 1)
sr = osr.SpatialReference()
sr.SetGeogCS("GEOG_NAME", "D_DATUM_NAME", "", 3000000, 0)
Expand All @@ -657,15 +660,13 @@ def test_tiff_srs_write_vertical_perspective():
ds.SetSpatialRef(sr)
assert gdal.GetLastErrorMsg() == ''
ds = None
assert gdal.VSIStatL('/vsimem/src.tif.aux.xml')

src_ds = gdal.Open('/vsimem/src.tif')
# First is PROJ 7
assert src_ds.GetSpatialRef().ExportToProj4() in ('+proj=nsper +lat_0=1 +lon_0=2 +h=1000 +x_0=0 +y_0=0 +R=3000000 +units=m +no_defs', '+proj=nsper +R=3000000 +lat_0=1 +lon_0=2 +h=1000 +x_0=0 +y_0=0 +wktext +no_defs')
gdal.ErrorReset()
gdal.GetDriverByName('GTiff').CreateCopy('/vsimem/dst.tif', src_ds)
assert gdal.GetLastErrorMsg() == ''
assert gdal.VSIStatL('/vsimem/dst.tif.aux.xml')

ds = gdal.Open('/vsimem/dst.tif')
assert ds.GetSpatialRef().ExportToProj4() == src_ds.GetSpatialRef().ExportToProj4()
Expand Down
17 changes: 16 additions & 1 deletion autotest/osr/osr_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import sys

import gdaltest
from osgeo import osr
from osgeo import gdal, osr
import pytest
from threading import Thread

Expand Down Expand Up @@ -1772,3 +1772,18 @@ def test_osr_basic_set_get_coordinate_epoch():
clone.SetCoordinateEpoch(0)
assert not srs.IsSame(clone)
assert srs.IsSame(clone, ['IGNORE_COORDINATE_EPOCH=YES'])


###############################################################################
# Test exporting a projection method that is WKT2-only (#4133)


def test_osr_basic_export_equal_earth_to_wkt():

srs = osr.SpatialReference()
srs.ImportFromEPSG(8859)
wkt = srs.ExportToWkt()
assert wkt
assert wkt == srs.ExportToWkt(['FORMAT=WKT2'])
assert 'METHOD["Equal Earth",' in wkt
assert gdal.GetLastErrorMsg() == ''
23 changes: 22 additions & 1 deletion gdal/ogr/ogrspatialreference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "cpl_conv.h"
#include "cpl_csv.h"
#include "cpl_error.h"
#include "cpl_error_internal.h"
#include "cpl_http.h"
#include "cpl_multiproc.h"
#include "cpl_string.h"
Expand Down Expand Up @@ -1494,7 +1495,10 @@ OGRErr OGRSpatialReference::exportToWkt( char ** ppszResult,
auto ctxt = d->getPROJContext();
auto wktFormat = PJ_WKT1_GDAL;
const char* pszFormat = CSLFetchNameValueDef(papszOptions, "FORMAT",
CPLGetConfigOption("OSR_WKT_FORMAT", ""));
CPLGetConfigOption("OSR_WKT_FORMAT", "DEFAULT"));
if( EQUAL(pszFormat, "DEFAULT") )
pszFormat = "";

if( EQUAL(pszFormat, "WKT1_ESRI" ) || d->m_bMorphToESRI )
{
wktFormat = PJ_WKT1_ESRI;
Expand Down Expand Up @@ -1561,9 +1565,26 @@ OGRErr OGRSpatialReference::exportToWkt( char ** ppszResult,
d->getPROJContext(), d->m_pj_crs, true, true);
}

std::vector<CPLErrorHandlerAccumulatorStruct> aoErrors;
CPLInstallErrorHandlerAccumulator(aoErrors);
const char* pszWKT = proj_as_wkt(
ctxt, boundCRS ? boundCRS : d->m_pj_crs,
wktFormat, aosOptions.List());
CPLUninstallErrorHandlerAccumulator();
for( const auto& oError: aoErrors )
{
if( pszFormat[0] == '\0' &&
oError.msg.find("Unsupported conversion method") != std::string::npos )
{
CPLErrorReset();
// If we cannot export in the default mode (WKT1), retry with WKT2
pszWKT = proj_as_wkt(
ctxt, boundCRS ? boundCRS : d->m_pj_crs,
PJ_WKT2_2018, aosOptions.List());
break;
}
CPLError( oError.type, oError.no, "%s", oError.msg.c_str() );
}

if( !pszWKT )
{
Expand Down

0 comments on commit 0ea3123

Please sign in to comment.