Skip to content
Permalink
Browse files

Implement QgsReferencedGeometry

  • Loading branch information
dmarteau authored and nyalldawson committed Sep 14, 2020
1 parent 4eb931e commit 4eb779798ed0e473a1b79d0ebf89e4936ae9cb39
@@ -51,6 +51,43 @@ no reference system is required.

};

class QgsReferencedGeometry : QgsGeometry, QgsReferencedGeometryBase
{
%Docstring
A QgsGeometry with associated coordinate reference system.

.. versionadded:: 3.16
%End

%TypeHeaderCode
#include "qgsreferencedgeometry.h"
%End
public:

QgsReferencedGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs );
%Docstring
Constructor for QgsReferencedGeometry, with the specified initial ``geometry``
and ``crs``.
%End

QgsReferencedGeometry();
%Docstring
Constructor for QgsReferencedGeometry.
%End

operator QVariant() const;

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsReferencedGeometry: %1 (%2)>" ).arg( sipCpp->asWkt(), sipCpp->crs().authid() );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

};




class QgsReferencedRectangle : QgsRectangle, QgsReferencedGeometryBase
{
%Docstring
@@ -127,6 +164,7 @@ Constructor for QgsReferencedPointXY.
};



/************************************************************************
* This file has been generated automatically from *
* *
@@ -50,3 +50,10 @@ bool QgsReferencedPointXY::operator!=( const QgsReferencedPointXY &other )
{
return !( *this == other );
}

QgsReferencedGeometry::QgsReferencedGeometry( const QgsGeometry &geom, const QgsCoordinateReferenceSystem &crs )
: QgsGeometry( geom )
, QgsReferencedGeometryBase( crs )
{}


@@ -22,6 +22,7 @@
#include "qgis_core.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsrectangle.h"
#include "qgsgeometry.h"

/**
* \class QgsReferencedGeometryBase
@@ -64,6 +65,46 @@ class CORE_EXPORT QgsReferencedGeometryBase

};

/**
* \ingroup core
* A QgsGeometry with associated coordinate reference system.
* \since QGIS 3.16
*/
class CORE_EXPORT QgsReferencedGeometry : public QgsGeometry, public QgsReferencedGeometryBase
{
public:

/**
* Constructor for QgsReferencedGeometry, with the specified initial \a geometry
* and \a crs.
*/
QgsReferencedGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs );

/**
* Constructor for QgsReferencedGeometry.
*/
QgsReferencedGeometry() = default;

//! Allows direct construction of QVariants from geometry.
operator QVariant() const
{
return QVariant::fromValue( *this );
}

#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsReferencedGeometry: %1 (%2)>" ).arg( sipCpp->asWkt(), sipCpp->crs().authid() );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif

};

Q_DECLARE_METATYPE( QgsReferencedGeometry )



/**
* \ingroup core
* A QgsRectangle with associated coordinate reference system.
@@ -146,4 +187,5 @@ class CORE_EXPORT QgsReferencedPointXY : public QgsPointXY, public QgsReferenced

Q_DECLARE_METATYPE( QgsReferencedPointXY )


#endif // QGSREFERENCEDGEOMETRY_H
@@ -53,6 +53,7 @@
#include "qgsproject.h"
#include "qgslinesegment.h"
#include "qgsgeos.h"
#include "qgsreferencedgeometry.h"

//qgs unit test utility class
#include "qgsrenderchecker.h"
@@ -80,6 +81,7 @@ class TestQgsGeometry : public QObject
void copy();
void assignment();
void asVariant(); //test conversion to and from a QVariant
void referenced();
void isEmpty();
void equality();
void vertexIterator();
@@ -409,6 +411,7 @@ void TestQgsGeometry::asVariant()
//convert to and from a QVariant
QVariant var = QVariant::fromValue( original );
QVERIFY( var.isValid() );
QVERIFY( !var.canConvert< QgsReferencedGeometry >() );

QgsGeometry fromVar = qvariant_cast<QgsGeometry>( var );
QCOMPARE( fromVar.constGet()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
@@ -428,6 +431,26 @@ void TestQgsGeometry::asVariant()
QCOMPARE( fromVar3.constGet()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );
}


void TestQgsGeometry::referenced()
{
QgsReferencedGeometry geom1 = QgsReferencedGeometry( QgsGeometry::fromPointXY( QgsPointXY( 1, 2 ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3111" ) ) );
QCOMPARE( geom1.crs().authid(), QStringLiteral( "EPSG:3111" ) );
geom1.setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:28356" ) ) );
QCOMPARE( geom1.crs().authid(), QStringLiteral( "EPSG:28356" ) );

//convert to and from a QVariant
QVariant var = QVariant::fromValue( geom1 );
QVERIFY( var.isValid() );

QVERIFY( var.canConvert< QgsReferencedGeometry >() );

QgsReferencedGeometry geom2 = qvariant_cast<QgsReferencedGeometry>( var );
QCOMPARE( geom2.asWkt(), geom1.asWkt() );
QCOMPARE( geom2.crs().authid(), QStringLiteral( "EPSG:28356" ) );
}


void TestQgsGeometry::isEmpty()
{
QgsGeometry geom;
@@ -19,7 +19,7 @@
QgsMimeDataUtils, QgsVector, QgsVector3D, QgsVectorLayer, QgsReferencedPointXY, QgsReferencedRectangle,\
QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsProject, QgsClassificationRange, QgsBookmark, \
QgsLayoutMeasurement, QgsLayoutPoint, QgsLayoutSize, QgsUnitTypes, QgsConditionalStyle, QgsTableCell, QgsProperty, \
QgsVertexId
QgsVertexId, QgsReferencedGeometry

start_app()

@@ -138,6 +138,10 @@ def testQgsReferencedRectangleRepr(self):
r = QgsReferencedRectangle(QgsRectangle(1, 2, 3, 4), QgsCoordinateReferenceSystem('EPSG:4326'))
self.assertEqual(r.__repr__(), '<QgsReferencedRectangle: 1 2, 3 4 (EPSG:4326)>')

def testQgsReferencedGeometryRepr(self):
g = QgsReferencedGeometry(QgsGeometry.fromPoint(QgsPointXY(1, 2)), QgsCoordinateReferenceSystem('EPSG:4326'))
self.assertEqual(g.__repr__(), '<QgsReferencedGeometry: Point (1 2) (EPSG:4326)>')

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

0 comments on commit 4eb7797

Please sign in to comment.
You can’t perform that action at this time.