mirror of
https://github.com/qgis/QGIS.git
synced 2025-12-15 00:07:25 -05:00
add __repr__ for more geometry classes and add test
This commit is contained in:
parent
ac4f508c13
commit
cc3e29bcc2
2
python/core/auto_additions/qgscircle.py
Normal file
2
python/core/auto_additions/qgscircle.py
Normal file
@ -0,0 +1,2 @@
|
||||
# The following has been generated automatically from src/core/geometry/qgscircle.h
|
||||
QgsCircle.__repr__ = lambda self: '<QgsPoint {}>'.format(self.toString())
|
||||
2
python/core/auto_additions/qgscircularstring.py
Normal file
2
python/core/auto_additions/qgscircularstring.py
Normal file
@ -0,0 +1,2 @@
|
||||
# The following has been generated automatically from src/core/geometry/qgscircularstring.h
|
||||
QgsCircularString.__repr__ = lambda self: '<QgsCircularString {}>'.format(self.asWkt())
|
||||
2
python/core/auto_additions/qgscompoundcurve.py
Normal file
2
python/core/auto_additions/qgscompoundcurve.py
Normal file
@ -0,0 +1,2 @@
|
||||
# The following has been generated automatically from src/core/geometry/qgscompoundcurve.h
|
||||
QgsCompoundCurve.__repr__ = lambda self: '<QgsCompoundCurve {}>'.format(self.asWkt())
|
||||
2
python/core/auto_additions/qgscurvepolygon.py
Normal file
2
python/core/auto_additions/qgscurvepolygon.py
Normal file
@ -0,0 +1,2 @@
|
||||
# The following has been generated automatically from src/core/geometry/qgscurvepolygon.h
|
||||
QgsCurvePolygon.__repr__ = lambda self: '<QgsCurvePolygon {}>'.format(self.asWkt())
|
||||
2
python/core/auto_additions/qgsellipse.py
Normal file
2
python/core/auto_additions/qgsellipse.py
Normal file
@ -0,0 +1,2 @@
|
||||
# The following has been generated automatically from src/core/geometry/qgsellipse.h
|
||||
QgsEllipse.__repr__ = lambda self: '<QgsEllipse {}>'.format(self.toString())
|
||||
@ -2,3 +2,4 @@
|
||||
QgsGeometry.BufferSide.baseClass = QgsGeometry
|
||||
QgsGeometry.EndCapStyle.baseClass = QgsGeometry
|
||||
QgsGeometry.JoinStyle.baseClass = QgsGeometry
|
||||
QgsGeometry.__repr__ = lambda self: '<QgsGeometry {}>'.format(self.asWkt())
|
||||
|
||||
2
python/core/auto_additions/qgslinestring.py
Normal file
2
python/core/auto_additions/qgslinestring.py
Normal file
@ -0,0 +1,2 @@
|
||||
# The following has been generated automatically from src/core/geometry/qgslinestring.h
|
||||
QgsLineString.__repr__ = lambda self: '<QgsLineString {}>'.format(self.asWkt())
|
||||
2
python/core/auto_additions/qgsmulticurve.py
Normal file
2
python/core/auto_additions/qgsmulticurve.py
Normal file
@ -0,0 +1,2 @@
|
||||
# The following has been generated automatically from src/core/geometry/qgsmulticurve.h
|
||||
QgsMultiCurve.__repr__ = lambda self: '<QgsMulitCurve {}>'.format(self.asWkt())
|
||||
2
python/core/auto_additions/qgsmultilinestring.py
Normal file
2
python/core/auto_additions/qgsmultilinestring.py
Normal file
@ -0,0 +1,2 @@
|
||||
# The following has been generated automatically from src/core/geometry/qgsmultilinestring.h
|
||||
QgsMultiLineString.__repr__ = lambda self: '<QgsMultiLineString {}>'.format(self.asWkt())
|
||||
2
python/core/auto_additions/qgsmultipoint.py
Normal file
2
python/core/auto_additions/qgsmultipoint.py
Normal file
@ -0,0 +1,2 @@
|
||||
# The following has been generated automatically from src/core/geometry/qgsmultipoint.h
|
||||
QgsMultiPoint.__repr__ = lambda self: '<QgsMultiPoint {}>'.format(self.asWkt())
|
||||
2
python/core/auto_additions/qgsmultipolygon.py
Normal file
2
python/core/auto_additions/qgsmultipolygon.py
Normal file
@ -0,0 +1,2 @@
|
||||
# The following has been generated automatically from src/core/geometry/qgsmultipolygon.h
|
||||
QgsMultiPolygon.__repr__ = lambda self: '<QgsMultiPolygon {}>'.format(self.asWkt())
|
||||
2
python/core/auto_additions/qgspolygon.py
Normal file
2
python/core/auto_additions/qgspolygon.py
Normal file
@ -0,0 +1,2 @@
|
||||
# The following has been generated automatically from src/core/geometry/qgspolygon.h
|
||||
QgsPolygon.__repr__ = lambda self: '<QgsPolygon {}>'.format(self.asWkt())
|
||||
2
python/core/auto_additions/qgsrectangle.py
Normal file
2
python/core/auto_additions/qgsrectangle.py
Normal file
@ -0,0 +1,2 @@
|
||||
# The following has been generated automatically from src/core/geometry/qgsrectangle.h
|
||||
QgsRectangle.__repr__ = lambda self: '<QgsRectangle {}>'.format(self.asWktCoordinates())
|
||||
@ -253,6 +253,7 @@ Returns true if the circle contains the ``point``.
|
||||
virtual QString toString( int pointPrecision = 17, int radiusPrecision = 17, int azimuthPrecision = 2 ) const;
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
/************************************************************************
|
||||
|
||||
@ -162,6 +162,7 @@ Sets the circular string's points
|
||||
virtual QgsCircularString *createEmptyWithSameType() const /Factory/;
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
virtual QgsRectangle calculateBoundingBox() const;
|
||||
|
||||
@ -166,6 +166,7 @@ Appends first point if not already closed.
|
||||
virtual QgsCompoundCurve *createEmptyWithSameType() const /Factory/;
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
virtual QgsRectangle calculateBoundingBox() const;
|
||||
|
||||
@ -216,6 +216,7 @@ Returns approximate rotation angle for a vertex. Usually average angle between a
|
||||
virtual QgsCurvePolygon *createEmptyWithSameType() const /Factory/;
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
virtual int childCount() const;
|
||||
|
||||
@ -248,6 +248,7 @@ returns a string representation of the ellipse.
|
||||
Members will be truncated to the specified precision.
|
||||
%End
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
};
|
||||
|
||||
@ -1290,6 +1290,7 @@ Exports the geometry to WKT
|
||||
precision parameter added in QGIS 2.4
|
||||
%End
|
||||
|
||||
|
||||
QString asJson( int precision = 17 ) const;
|
||||
%Docstring
|
||||
Exports the geometry to a GeoJSON string.
|
||||
|
||||
@ -317,6 +317,7 @@ of the curve.
|
||||
virtual QgsLineString *createEmptyWithSameType() const /Factory/;
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
virtual QgsRectangle calculateBoundingBox() const;
|
||||
|
||||
@ -57,6 +57,7 @@ Returns a copy of the multi curve, where each component curve has had its line d
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -53,6 +53,7 @@ Returns the geometry converted to the more generic curve type :py:class:`QgsMult
|
||||
virtual QgsMultiLineString *createEmptyWithSameType() const /Factory/;
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
virtual bool wktOmitChildType() const;
|
||||
|
||||
@ -55,6 +55,7 @@ Multi point geometry collection.
|
||||
virtual QgsMultiPoint *createEmptyWithSameType() const /Factory/;
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
virtual bool wktOmitChildType() const;
|
||||
|
||||
@ -54,6 +54,7 @@ Returns the geometry converted to the more generic curve type :py:class:`QgsMult
|
||||
virtual QgsMultiPolygon *createEmptyWithSameType() const /Factory/;
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
virtual bool wktOmitChildType() const;
|
||||
|
||||
@ -65,6 +65,7 @@ negative if the point lies outside the polygon.
|
||||
virtual QgsPolygon *createEmptyWithSameType() const /Factory/;
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
|
||||
@ -322,6 +322,7 @@ Converts the rectangle to a 3D box, with the specified
|
||||
|
||||
operator QVariant() const;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -122,7 +122,6 @@ Returns a string representation of the point (x, y) with a preset ``precision``.
|
||||
If ``precision`` is -1, then a default precision will be used.
|
||||
%End
|
||||
|
||||
|
||||
QString asWkt() const;
|
||||
%Docstring
|
||||
Returns the well known text representation for the point (e.g. "POINT(x y)").
|
||||
|
||||
@ -39,6 +39,16 @@ count=0
|
||||
|
||||
modules=(core gui analysis server)
|
||||
for module in "${modules[@]}"; do
|
||||
|
||||
# clean auto_additions and auto_generated folders
|
||||
rm -rf python/${module}/auto_additions/*.py
|
||||
rm -rf python/${module}/auto_generated/*.py
|
||||
# put back __init__.py
|
||||
echo '"""
|
||||
This folder is completed using sipify.pl script
|
||||
It is not aimed to be manually edited
|
||||
"""' > python/${module}/auto_additions/__init__.py
|
||||
|
||||
while read -r sipfile; do
|
||||
echo "$sipfile.in"
|
||||
header=$(${GP}sed -E 's@(.*)\.sip@src/\1.h@; s@auto_generated/@@' <<< $sipfile)
|
||||
|
||||
@ -247,6 +247,8 @@ class CORE_EXPORT QgsCircle : public QgsEllipse
|
||||
|
||||
QString toString( int pointPrecision = 17, int radiusPrecision = 17, int azimuthPrecision = 2 ) const override;
|
||||
|
||||
SIP_PYTHON_SPECIAL_REPR( "'<QgsPoint {}>'.format(self.toString())" )
|
||||
|
||||
};
|
||||
|
||||
#endif // QGSCIRCLE_H
|
||||
|
||||
@ -146,6 +146,8 @@ class CORE_EXPORT QgsCircularString: public QgsCurve
|
||||
|
||||
QgsCircularString *createEmptyWithSameType() const override SIP_FACTORY;
|
||||
|
||||
SIP_PYTHON_SPECIAL_REPR( "'<QgsCircularString {}>'.format(self.asWkt())" )
|
||||
|
||||
protected:
|
||||
|
||||
QgsRectangle calculateBoundingBox() const override;
|
||||
|
||||
@ -146,6 +146,8 @@ class CORE_EXPORT QgsCompoundCurve: public QgsCurve
|
||||
|
||||
QgsCompoundCurve *createEmptyWithSameType() const override SIP_FACTORY;
|
||||
|
||||
SIP_PYTHON_SPECIAL_REPR( "'<QgsCompoundCurve {}>'.format(self.asWkt())" )
|
||||
|
||||
protected:
|
||||
|
||||
QgsRectangle calculateBoundingBox() const override;
|
||||
|
||||
@ -204,6 +204,8 @@ class CORE_EXPORT QgsCurvePolygon: public QgsSurface
|
||||
|
||||
QgsCurvePolygon *createEmptyWithSameType() const override SIP_FACTORY;
|
||||
|
||||
SIP_PYTHON_SPECIAL_REPR( "'<QgsCurvePolygon {}>'.format(self.asWkt())" )
|
||||
|
||||
protected:
|
||||
|
||||
int childCount() const override;
|
||||
|
||||
@ -240,6 +240,8 @@ class CORE_EXPORT QgsEllipse
|
||||
*/
|
||||
virtual QString toString( int pointPrecision = 17, int axisPrecision = 17, int azimuthPrecision = 2 ) const;
|
||||
|
||||
SIP_PYTHON_SPECIAL_REPR( "'<QgsEllipse {}>'.format(self.toString())" )
|
||||
|
||||
protected:
|
||||
QgsPoint mCenter;
|
||||
double mSemiMajorAxis = 0.0;
|
||||
|
||||
@ -1248,6 +1248,8 @@ class CORE_EXPORT QgsGeometry
|
||||
*/
|
||||
QString asWkt( int precision = 17 ) const;
|
||||
|
||||
SIP_PYTHON_SPECIAL_REPR( "'<QgsGeometry {}>'.format(self.asWkt())" )
|
||||
|
||||
/**
|
||||
* Exports the geometry to a GeoJSON string.
|
||||
*/
|
||||
|
||||
@ -346,6 +346,8 @@ class CORE_EXPORT QgsLineString: public QgsCurve
|
||||
|
||||
QgsLineString *createEmptyWithSameType() const override SIP_FACTORY;
|
||||
|
||||
SIP_PYTHON_SPECIAL_REPR( "'<QgsLineString {}>'.format(self.asWkt())" )
|
||||
|
||||
protected:
|
||||
|
||||
QgsRectangle calculateBoundingBox() const override;
|
||||
|
||||
@ -73,6 +73,8 @@ class CORE_EXPORT QgsMultiCurve: public QgsGeometryCollection
|
||||
|
||||
QgsMultiCurve *createEmptyWithSameType() const override SIP_FACTORY;
|
||||
|
||||
SIP_PYTHON_SPECIAL_REPR( "'<QgsMulitCurve {}>'.format(self.asWkt())" )
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
@ -65,6 +65,8 @@ class CORE_EXPORT QgsMultiLineString: public QgsMultiCurve
|
||||
|
||||
QgsMultiLineString *createEmptyWithSameType() const override SIP_FACTORY;
|
||||
|
||||
SIP_PYTHON_SPECIAL_REPR( "'<QgsMultiLineString {}>'.format(self.asWkt())" )
|
||||
|
||||
protected:
|
||||
|
||||
bool wktOmitChildType() const override;
|
||||
|
||||
@ -66,6 +66,8 @@ class CORE_EXPORT QgsMultiPoint: public QgsGeometryCollection
|
||||
|
||||
QgsMultiPoint *createEmptyWithSameType() const override SIP_FACTORY;
|
||||
|
||||
SIP_PYTHON_SPECIAL_REPR( "'<QgsMultiPoint {}>'.format(self.asWkt())" )
|
||||
|
||||
protected:
|
||||
|
||||
bool wktOmitChildType() const override;
|
||||
|
||||
@ -65,6 +65,8 @@ class CORE_EXPORT QgsMultiPolygon: public QgsMultiSurface
|
||||
|
||||
QgsMultiPolygon *createEmptyWithSameType() const override SIP_FACTORY;
|
||||
|
||||
SIP_PYTHON_SPECIAL_REPR( "'<QgsMultiPolygon {}>'.format(self.asWkt())" )
|
||||
|
||||
protected:
|
||||
|
||||
bool wktOmitChildType() const override;
|
||||
|
||||
@ -84,6 +84,8 @@ class CORE_EXPORT QgsPolygon: public QgsCurvePolygon
|
||||
|
||||
QgsPolygon *createEmptyWithSameType() const override SIP_FACTORY;
|
||||
|
||||
SIP_PYTHON_SPECIAL_REPR( "'<QgsPolygon {}>'.format(self.asWkt())" )
|
||||
|
||||
protected:
|
||||
|
||||
friend class QgsCurvePolygon;
|
||||
|
||||
@ -541,6 +541,8 @@ class CORE_EXPORT QgsRectangle
|
||||
return QVariant::fromValue( *this );
|
||||
}
|
||||
|
||||
SIP_PYTHON_SPECIAL_REPR( "'<QgsRectange {}>'.format(self.asWktCoordinates())" )
|
||||
|
||||
private:
|
||||
|
||||
double mXmin = 0.0;
|
||||
|
||||
@ -156,8 +156,6 @@ class CORE_EXPORT QgsPointXY
|
||||
*/
|
||||
QString toString( int precision = -1 ) const;
|
||||
|
||||
SIP_PYTHON_SPECIAL_REPR( "'<QgsPointXY {}>'.format(self.toString())" )
|
||||
|
||||
/**
|
||||
* Returns the well known text representation for the point (e.g. "POINT(x y)").
|
||||
* The wkt is created without an SRID.
|
||||
|
||||
@ -9,6 +9,7 @@ IF (WITH_SERVER)
|
||||
ENDIF (WITH_SERVER)
|
||||
|
||||
ADD_PYTHON_TEST(PyCoreAdittions test_core_additions.py)
|
||||
ADD_PYTHON_TEST(PyPythonRepr test_python_repr.py)
|
||||
ADD_PYTHON_TEST(PyQgsActionManager test_qgsactionmanager.py)
|
||||
ADD_PYTHON_TEST(PyQgsAFSProvider test_provider_afs.py)
|
||||
ADD_PYTHON_TEST(PyQgsPythonProvider test_provider_python.py)
|
||||
|
||||
116
tests/src/python/test_python_repr.py
Normal file
116
tests/src/python/test_python_repr.py
Normal file
@ -0,0 +1,116 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""QGIS Unit tests for core additions
|
||||
|
||||
.. note:: This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
"""
|
||||
__author__ = 'Denis Rouzaud'
|
||||
__date__ = '05.06.2018'
|
||||
__copyright__ = 'Copyright 2015, The QGIS Project'
|
||||
# This will get replaced with a git SHA1 when you do a git archive
|
||||
__revision__ = '$Format:%H$'
|
||||
|
||||
import qgis # NOQA
|
||||
import os
|
||||
|
||||
from qgis.testing import unittest, start_app
|
||||
from qgis.core import QgsGeometry, QgsPoint, QgsPointXY, QgsCircle, QgsCircularString, QgsCompoundCurve, QgsCurve,\
|
||||
QgsCurvePolygon, QgsEllipse, QgsLineString, QgsMultiCurve, QgsMultiLineString, QgsMultiPoint, QgsMultiPolygon,\
|
||||
QgsPolygon, QgsRectangle
|
||||
|
||||
import sip
|
||||
|
||||
start_app()
|
||||
|
||||
|
||||
class TestCoreAdditions(unittest.TestCase):
|
||||
|
||||
def TestQgsPointRepr(self):
|
||||
p = QgsPoint(123.456, 987.654, 100)
|
||||
print(p)
|
||||
|
||||
def TestQgsPointXYRepr(self):
|
||||
p = QgsPointXY(123.456, 987.654)
|
||||
print(p)
|
||||
|
||||
def TestQgsCircleRepr(self):
|
||||
c = QgsCircle(QgsPoint(1, 1), 2.0)
|
||||
print(c)
|
||||
|
||||
def TestQgsCircularstringRepr(self):
|
||||
cs = QgsCircularString(QgsPoint(1, 2), QgsPoint(2, 3), QgsPoint(3, 4))
|
||||
print(cs)
|
||||
|
||||
def TestQgsCompoundcurveRepr(self):
|
||||
cs = QgsCircularString(QgsPoint(1, 2), QgsPoint(2, 3), QgsPoint(3, 4))
|
||||
cc = QgsCompoundCurve()
|
||||
cc.addCurve(cs)
|
||||
print(cc)
|
||||
|
||||
def TestQgsCurvepolygonRepr(self):
|
||||
cp = QgsCurvePolygon()
|
||||
cs = QgsCircularString(QgsPoint(1, 10), QgsPoint(2, 11), QgsPoint(1, 10))
|
||||
cp.setExteriorRing(cs)
|
||||
print(cp)
|
||||
|
||||
def TestQgsEllipseRepr(self):
|
||||
e = QgsEllipse(QgsPoint(1, 2), 2.0, 3.0)
|
||||
print(e)
|
||||
|
||||
def TestQgsLineStringRepr(self):
|
||||
ls = QgsLineString([QgsPoint(10, 2), QgsPoint(10, 1), QgsPoint(5, 1)])
|
||||
print(ls)
|
||||
|
||||
def TestQgsMulticurveRepr(self):
|
||||
mc = QgsMultiCurve()
|
||||
cs = QgsCircularString(QgsPoint(1, 10), QgsPoint(2, 11), QgsPoint(3, 12))
|
||||
mc.addGeometry(cs)
|
||||
cs2 = QgsCircularString(QgsPoint(4, 20), QgsPoint(5, 22), QgsPoint(6, 24))
|
||||
mc.addGeometry(cs2)
|
||||
print(mc)
|
||||
|
||||
def TestQgsMultilineStringRepr(self):
|
||||
ml = QgsGeometry.fromMultiPolylineXY(
|
||||
[
|
||||
[QgsPointXY(0, 0), QgsPointXY(1, 0), QgsPointXY(1, 1), QgsPointXY(2, 1), QgsPointXY(2, 0), ],
|
||||
[QgsPointXY(3, 0), QgsPointXY(3, 1), QgsPointXY(5, 1), QgsPointXY(5, 0), QgsPointXY(6, 0), ]
|
||||
]
|
||||
)
|
||||
print(ml)
|
||||
|
||||
def TestQgsMultiPointRepr(self):
|
||||
wkt = "MultiPoint ((10 30),(40 20),(30 10),(20 10))"
|
||||
mp = QgsGeometry.fromWkt(wkt)
|
||||
print(mp)
|
||||
|
||||
def TestQgsMultipolygonRepr(self):
|
||||
mp = QgsGeometry.fromMultiPolygonXY([
|
||||
[[QgsPointXY(1, 1),
|
||||
QgsPointXY(2, 2),
|
||||
QgsPointXY(1, 2),
|
||||
QgsPointXY(1, 1)]],
|
||||
[[QgsPointXY(2, 2),
|
||||
QgsPointXY(3, 3),
|
||||
QgsPointXY(3, 1),
|
||||
QgsPointXY(2, 2)]]
|
||||
])
|
||||
print(mp)
|
||||
|
||||
def TestQgsPolygonRepr(self):
|
||||
p = QgsGeometry.fromPolygonXY(
|
||||
[[QgsPointXY(0, 0),
|
||||
QgsPointXY(2, 0),
|
||||
QgsPointXY(2, 2),
|
||||
QgsPointXY(0, 2),
|
||||
QgsPointXY(0, 0)]])
|
||||
print(p)
|
||||
|
||||
def TestQgsRectangleRepr(self):
|
||||
r = QgsRectangle(1, 2, 3, 4)
|
||||
print(r)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Loading…
x
Reference in New Issue
Block a user