Skip to content

Commit

Permalink
Improve python repr string for QgsCoordinateReferenceSystem
Browse files Browse the repository at this point in the history
- Indicate invalid crses
- If crs doesn't have an authid, use wkt representation
  • Loading branch information
nyalldawson committed Jan 11, 2021
1 parent e0ad337 commit f8e5e0b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
Expand Up @@ -860,7 +860,8 @@ Returns auth id of related geographic CRS

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsCoordinateReferenceSystem: %1>" ).arg( sipCpp->authid() );
const QString str = sipCpp->isValid() ? QStringLiteral( "<QgsCoordinateReferenceSystem: %1>" ).arg( !sipCpp->authid().isEmpty() ? sipCpp->authid() : sipCpp->toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) )
: QStringLiteral( "<QgsCoordinateReferenceSystem: invalid>" );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

Expand Down
3 changes: 2 additions & 1 deletion src/core/qgscoordinatereferencesystem.h
Expand Up @@ -792,7 +792,8 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsCoordinateReferenceSystem: %1>" ).arg( sipCpp->authid() );
const QString str = sipCpp->isValid() ? QStringLiteral( "<QgsCoordinateReferenceSystem: %1>" ).arg( !sipCpp->authid().isEmpty() ? sipCpp->authid() : sipCpp->toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) )
: QStringLiteral( "<QgsCoordinateReferenceSystem: invalid>" );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif
Expand Down
2 changes: 2 additions & 0 deletions tests/src/python/test_python_repr.py
Expand Up @@ -182,6 +182,8 @@ def testQgsReferencedGeometryRepr(self):
self.assertEqual(g.__repr__(), '<QgsReferencedGeometry: Point (1 2) (EPSG:4326)>')

def testQgsCoordinateReferenceSystem(self):
crs = QgsCoordinateReferenceSystem()
self.assertEqual(crs.__repr__(), '<QgsCoordinateReferenceSystem: invalid>')
crs = QgsCoordinateReferenceSystem('EPSG:4326')
self.assertEqual(crs.__repr__(), '<QgsCoordinateReferenceSystem: EPSG:4326>')
crs = QgsCoordinateReferenceSystem('EPSG:3111')
Expand Down

0 comments on commit f8e5e0b

Please sign in to comment.