Improve Python __repr__ handling for null geometries

Also avoid massive long __repr__ strings for complex geometries,
as these can flood the Python console (and first aid plugin),
and aren't useful for debugging anyway.

Refs #14640
This commit is contained in:
Nyall Dawson 2018-12-18 09:42:34 +10:00
parent d072a13234
commit 7d648e5b51
21 changed files with 102 additions and 20 deletions

View File

@ -168,7 +168,10 @@ Sets the circular string's points
SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsCircularString: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsCircularString: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

View File

@ -172,7 +172,10 @@ Appends first point if not already closed.
SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsCompoundCurve: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsCompoundCurve: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

View File

@ -274,7 +274,10 @@ Returns approximate rotation angle for a vertex. Usually average angle between a
SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsCurvePolygon: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsCurvePolygon: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

View File

@ -1391,7 +1391,16 @@ Exports the geometry to WKT
SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsGeometry: %1>" ).arg( sipCpp->asWkt() );
QString str;
if ( sipCpp->isNull() )
str = QStringLiteral( "<QgsGeometry: null>" );
else
{
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
str = QStringLiteral( "<QgsGeometry: %1>" ).arg( wkt );
}
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

View File

@ -501,7 +501,10 @@ of the curve.
SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsLineString: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsLineString: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

View File

@ -58,7 +58,10 @@ Returns a copy of the multi curve, where each component curve has had its line d
SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsMultiCurve: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsMultiCurve: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

View File

@ -55,7 +55,10 @@ Returns the geometry converted to the more generic curve type :py:class:`QgsMult
SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsMultiLineString: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsMultiLineString: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

View File

@ -57,7 +57,10 @@ Multi point geometry collection.
SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsMultiPoint: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsMultiPoint: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

View File

@ -56,7 +56,10 @@ Returns the geometry converted to the more generic curve type :py:class:`QgsMult
SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsMultiPolygon: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsMultiPolygon: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

View File

@ -67,7 +67,10 @@ negative if the point lies outside the polygon.
SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsPolygon: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsPolygon: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

View File

@ -152,7 +152,10 @@ class CORE_EXPORT QgsCircularString: public QgsCurve
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsCircularString: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsCircularString: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif

View File

@ -152,7 +152,10 @@ class CORE_EXPORT QgsCompoundCurve: public QgsCurve
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsCompoundCurve: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsCompoundCurve: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif

View File

@ -286,7 +286,10 @@ class CORE_EXPORT QgsCurvePolygon: public QgsSurface
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsCurvePolygon: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsCurvePolygon: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif

View File

@ -1392,7 +1392,16 @@ class CORE_EXPORT QgsGeometry
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsGeometry: %1>" ).arg( sipCpp->asWkt() );
QString str;
if ( sipCpp->isNull() )
str = QStringLiteral( "<QgsGeometry: null>" );
else
{
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
str = QStringLiteral( "<QgsGeometry: %1>" ).arg( wkt );
}
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif

View File

@ -635,7 +635,10 @@ class CORE_EXPORT QgsLineString: public QgsCurve
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsLineString: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsLineString: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End

View File

@ -76,7 +76,10 @@ class CORE_EXPORT QgsMultiCurve: public QgsGeometryCollection
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsMultiCurve: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsMultiCurve: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif

View File

@ -68,7 +68,10 @@ class CORE_EXPORT QgsMultiLineString: public QgsMultiCurve
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsMultiLineString: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsMultiLineString: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif

View File

@ -69,7 +69,10 @@ class CORE_EXPORT QgsMultiPoint: public QgsGeometryCollection
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsMultiPoint: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsMultiPoint: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif

View File

@ -68,7 +68,10 @@ class CORE_EXPORT QgsMultiPolygon: public QgsMultiSurface
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsMultiPolygon: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsMultiPolygon: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif

View File

@ -87,7 +87,10 @@ class CORE_EXPORT QgsPolygon: public QgsCurvePolygon
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsPolygon: %1>" ).arg( sipCpp->asWkt() );
QString wkt = sipCpp->asWkt();
if ( wkt.length() > 1000 )
wkt = wkt.left( 1000 ) + QStringLiteral( "..." );
QString str = QStringLiteral( "<QgsPolygon: %1>" ).arg( wkt );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif

View File

@ -26,9 +26,19 @@ start_app()
class TestPython__repr__(unittest.TestCase):
def testQgsGeometryRepr(self):
g = QgsGeometry()
self.assertEqual(g.__repr__(), '<QgsGeometry: null>')
p = QgsPointXY(123.456, 987.654)
g = QgsGeometry.fromPointXY(p)
self.assertTrue(g.__repr__().startswith('<QgsGeometry: Point (123.456'))
g = QgsGeometry(QgsLineString([QgsPoint(0, 2), QgsPoint(1010, 2)]))
g = g.densifyByCount(1000)
# long strings must be truncated for performance -- otherwise they flood the console/first aid output
self.assertTrue(g.__repr__().startswith('<QgsGeometry: LineString (0 2,'))
self.assertTrue(
g.__repr__().endswith('...>'))
self.assertEqual(len(g.__repr__()), 1018)
def testQgsPointRepr(self):
p = QgsPoint(123.456, 987.654, 100)