Skip to content

Commit

Permalink
[processing] Fix conversion of ESRI CRS when running GDAL algorithms …
Browse files Browse the repository at this point in the history
…under proj 6

Fixes #35123
  • Loading branch information
nyalldawson committed Mar 17, 2020
1 parent ccbe6b9 commit 8b54d2f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
10 changes: 8 additions & 2 deletions python/plugins/processing/algs/gdal/GdalUtils.py
Expand Up @@ -41,7 +41,9 @@
QgsMessageLog,
QgsSettings,
QgsCredentials,
QgsDataSourceUri)
QgsDataSourceUri,
QgsProjUtils,
QgsCoordinateReferenceSystem)
from processing.core.ProcessingConfig import ProcessingConfig
from processing.tools.system import isWindows, isMac

Expand Down Expand Up @@ -432,8 +434,12 @@ def gdal_crs_string(crs):
:param crs: crs to convert
:return: gdal friendly string
"""
if crs.authid().upper().startswith('EPSG:'):
if crs.authid().upper().startswith('EPSG:') or crs.authid().upper().startswith('IGNF:') or crs.authid().upper().startswith('ESRI:'):
return crs.authid()

if QgsProjUtils.projVersionMajor() >= 6:
# use WKT
return crs.toWkt(QgsCoordinateReferenceSystem.WKT2_2018)

# fallback to proj4 string, stripping out newline characters
return crs.toProj().replace('\n', ' ').replace('\r', ' ')
26 changes: 17 additions & 9 deletions python/plugins/processing/tests/GdalAlgorithmsGeneralTest.py
Expand Up @@ -36,6 +36,7 @@
QgsProject,
QgsVectorLayer,
QgsRectangle,
QgsProjUtils,
QgsProcessingException,
QgsProcessingFeatureSourceDefinition)

Expand Down Expand Up @@ -318,15 +319,22 @@ def testCrsConversion(self):
crs.createFromProj(
'+proj=utm +zone=36 +south +a=600000 +b=70000 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs')
self.assertTrue(crs.isValid())
self.assertEqual(GdalUtils.gdal_crs_string(crs),
'+proj=utm +zone=36 +south +a=600000 +b=70000 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs')
# check that newlines are stripped
crs = QgsCoordinateReferenceSystem()
crs.createFromProj(
'+proj=utm +zone=36 +south\n +a=600000 +b=70000 \r\n +towgs84=-143,-90,-294,0,0,0,0 +units=m\n+no_defs')
self.assertTrue(crs.isValid())
self.assertEqual(GdalUtils.gdal_crs_string(crs),
'+proj=utm +zone=36 +south +a=600000 +b=70000 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs')

if QgsProjUtils.projVersionMajor() >= 6:
# proj 6, WKT should be used
self.assertEqual(GdalUtils.gdal_crs_string(crs)[:40], 'BOUNDCRS[SOURCECRS[PROJCRS["unknown",BAS')

self.assertEqual(GdalUtils.gdal_crs_string(QgsCoordinateReferenceSystem('ESRI:102003')), 'ESRI:102003')
else:
self.assertEqual(GdalUtils.gdal_crs_string(crs),
'+proj=utm +zone=36 +south +a=600000 +b=70000 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs')
# check that newlines are stripped
crs = QgsCoordinateReferenceSystem()
crs.createFromProj(
'+proj=utm +zone=36 +south\n +a=600000 +b=70000 \r\n +towgs84=-143,-90,-294,0,0,0,0 +units=m\n+no_defs')
self.assertTrue(crs.isValid())
self.assertEqual(GdalUtils.gdal_crs_string(crs),
'+proj=utm +zone=36 +south +a=600000 +b=70000 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs')


if __name__ == '__main__':
Expand Down

0 comments on commit 8b54d2f

Please sign in to comment.