Skip to content

Commit

Permalink
Allow transform methods in QgsAbstractGeometry/QgsGeometry to also
Browse files Browse the repository at this point in the history
transform z/m values via scale/translate arguments
  • Loading branch information
nyalldawson committed Nov 24, 2017
1 parent 2b5aca5 commit dd223d9
Show file tree
Hide file tree
Showing 24 changed files with 135 additions and 78 deletions.
9 changes: 6 additions & 3 deletions python/core/geometry/qgsabstractgeometry.sip
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,13 @@ class QgsAbstractGeometry
transform.
%End

virtual void transform( const QTransform &t ) = 0;
virtual void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0,
double mTranslate = 0.0, double mScale = 1.0 ) = 0;
%Docstring
Transforms the geometry using a QTransform object
\param t QTransform transformation
Transforms the x and y components of the geometry using a QTransform object ``t``.

Optionally, the geometry's z values can be scaled via ``zScale`` and translated via ``zTranslate``.
Similarly, m-values can be scaled via ``mScale`` and translated via ``mTranslate``.
%End

virtual void draw( QPainter &p ) const = 0;
Expand Down
2 changes: 1 addition & 1 deletion python/core/geometry/qgscircularstring.sip
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class QgsCircularString: QgsCurve

virtual void transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform,
bool transformZ = false );
virtual void transform( const QTransform &t );
virtual void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 );

virtual void addToPainterPath( QPainterPath &path ) const;

Expand Down
2 changes: 1 addition & 1 deletion python/core/geometry/qgscompoundcurve.sip
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class QgsCompoundCurve: QgsCurve

virtual void transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform,
bool transformZ = false );
virtual void transform( const QTransform &t );
virtual void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 );

virtual void addToPainterPath( QPainterPath &path ) const;

Expand Down
2 changes: 1 addition & 1 deletion python/core/geometry/qgscurvepolygon.sip
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Adds an interior ring to the geometry (takes ownership)

virtual void transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform,
bool transformZ = false );
virtual void transform( const QTransform &t );
virtual void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 );


virtual bool insertVertex( QgsVertexId position, const QgsPoint &vertex );
Expand Down
12 changes: 8 additions & 4 deletions python/core/geometry/qgsgeometry.sip
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,9 @@ Returns true if WKB of the geometry is of WKBMulti* type
:rtype: QgsGeometry
%End

OperationResult translate( double dx, double dy );
OperationResult translate( double dx, double dy, double dz = 0.0, double dm = 0.0 );
%Docstring
Translates this geometry by dx, dy
Translates this geometry by dx, dy, dz and dm.
:return: OperationResult a result code: success or reason of failure
:rtype: OperationResult
%End
Expand All @@ -580,9 +580,13 @@ Returns true if WKB of the geometry is of WKBMulti* type
:rtype: OperationResult
%End

OperationResult transform( const QTransform &ct );
OperationResult transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 );
%Docstring
Transforms this geometry as described by QTransform ct
Transforms the x and y components of the geometry using a QTransform object ``t``.

Optionally, the geometry's z values can be scaled via ``zScale`` and translated via ``zTranslate``.
Similarly, m-values can be scaled via ``mScale`` and translated via ``mTranslate``.

:return: OperationResult a result code: success or reason of failure
:rtype: OperationResult
%End
Expand Down
2 changes: 1 addition & 1 deletion python/core/geometry/qgsgeometrycollection.sip
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Adds a geometry and takes ownership. Returns true in case of success.

virtual void transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform,
bool transformZ = false );
virtual void transform( const QTransform &t );
virtual void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 );


virtual void draw( QPainter &p ) const;
Expand Down
2 changes: 1 addition & 1 deletion python/core/geometry/qgslinestring.sip
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ Closes the line string by appending the first point to the end of the line, if i

virtual void transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform,
bool transformZ = false );
virtual void transform( const QTransform &t );
virtual void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 );


virtual void addToPainterPath( QPainterPath &path ) const;
Expand Down
2 changes: 1 addition & 1 deletion python/core/geometry/qgspoint.sip
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ class QgsPoint: QgsAbstractGeometry

virtual void transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform,
bool transformZ = false );
virtual void transform( const QTransform &t );
virtual void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 );

virtual QgsCoordinateSequence coordinateSequence() const;

Expand Down
9 changes: 6 additions & 3 deletions src/core/geometry/qgsabstractgeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,13 @@ class CORE_EXPORT QgsAbstractGeometry
bool transformZ = false ) = 0;

/**
* Transforms the geometry using a QTransform object
* \param t QTransform transformation
* Transforms the x and y components of the geometry using a QTransform object \a t.
*
* Optionally, the geometry's z values can be scaled via \a zScale and translated via \a zTranslate.
* Similarly, m-values can be scaled via \a mScale and translated via \a mTranslate.
*/
virtual void transform( const QTransform &t ) = 0;
virtual void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0,
double mTranslate = 0.0, double mScale = 1.0 ) = 0;

/**
* Draws the geometry using the specified QPainter.
Expand Down
12 changes: 11 additions & 1 deletion src/core/geometry/qgscircularstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,17 +559,27 @@ void QgsCircularString::transform( const QgsCoordinateTransform &ct, QgsCoordina
}
}

void QgsCircularString::transform( const QTransform &t )
void QgsCircularString::transform( const QTransform &t, double zTranslate, double zScale, double mTranslate, double mScale )
{
clearCache();

int nPoints = numPoints();
bool hasZ = is3D();
bool hasM = isMeasure();
for ( int i = 0; i < nPoints; ++i )
{
qreal x, y;
t.map( mX.at( i ), mY.at( i ), &x, &y );
mX[i] = x;
mY[i] = y;
if ( hasZ )
{
mZ[i] = mZ.at( i ) * zScale + zTranslate;
}
if ( hasM )
{
mM[i] = mM.at( i ) * mScale + mTranslate;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgscircularstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class CORE_EXPORT QgsCircularString: public QgsCurve
void draw( QPainter &p ) const override;
void transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform,
bool transformZ = false ) override;
void transform( const QTransform &t ) override;
void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 ) override;
void addToPainterPath( QPainterPath &path ) const override;
void drawAsPolygon( QPainter &p ) const override;
bool insertVertex( QgsVertexId position, const QgsPoint &vertex ) override;
Expand Down
4 changes: 2 additions & 2 deletions src/core/geometry/qgscompoundcurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,11 +509,11 @@ void QgsCompoundCurve::transform( const QgsCoordinateTransform &ct, QgsCoordinat
clearCache();
}

void QgsCompoundCurve::transform( const QTransform &t )
void QgsCompoundCurve::transform( const QTransform &t, double zTranslate, double zScale, double mTranslate, double mScale )
{
for ( QgsCurve *curve : qgis::as_const( mCurves ) )
{
curve->transform( t );
curve->transform( t, zTranslate, zScale, mTranslate, mScale );
}
clearCache();
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgscompoundcurve.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class CORE_EXPORT QgsCompoundCurve: public QgsCurve
void draw( QPainter &p ) const override;
void transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform,
bool transformZ = false ) override;
void transform( const QTransform &t ) override;
void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 ) override;
void addToPainterPath( QPainterPath &path ) const override;
void drawAsPolygon( QPainter &p ) const override;
bool insertVertex( QgsVertexId position, const QgsPoint &vertex ) override;
Expand Down
6 changes: 3 additions & 3 deletions src/core/geometry/qgscurvepolygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,16 +704,16 @@ void QgsCurvePolygon::transform( const QgsCoordinateTransform &ct, QgsCoordinate
clearCache();
}

void QgsCurvePolygon::transform( const QTransform &t )
void QgsCurvePolygon::transform( const QTransform &t, double zTranslate, double zScale, double mTranslate, double mScale )
{
if ( mExteriorRing )
{
mExteriorRing->transform( t );
mExteriorRing->transform( t, zTranslate, zScale, mTranslate, mScale );
}

for ( QgsCurve *curve : qgis::as_const( mInteriorRings ) )
{
curve->transform( t );
curve->transform( t, zTranslate, zScale, mTranslate, mScale );
}
clearCache();
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgscurvepolygon.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class CORE_EXPORT QgsCurvePolygon: public QgsSurface
void draw( QPainter &p ) const override;
void transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform,
bool transformZ = false ) override;
void transform( const QTransform &t ) override;
void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 ) override;

bool insertVertex( QgsVertexId position, const QgsPoint &vertex ) override;
bool moveVertex( QgsVertexId position, const QgsPoint &newPos ) override;
Expand Down
8 changes: 4 additions & 4 deletions src/core/geometry/qgsgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ QgsGeometry::OperationResult QgsGeometry::addPart( GEOSGeometry *newPart )
return QgsGeometryEditUtils::addPart( d->geometry.get(), std::move( geom ) );
}

QgsGeometry::OperationResult QgsGeometry::translate( double dx, double dy )
QgsGeometry::OperationResult QgsGeometry::translate( double dx, double dy, double dz, double dm )
{
if ( !d->geometry )
{
Expand All @@ -761,7 +761,7 @@ QgsGeometry::OperationResult QgsGeometry::translate( double dx, double dy )

detach();

d->geometry->transform( QTransform::fromTranslate( dx, dy ) );
d->geometry->transform( QTransform::fromTranslate( dx, dy ), dz, 1.0, dm );
return QgsGeometry::Success;
}

Expand Down Expand Up @@ -2308,15 +2308,15 @@ QgsGeometry::OperationResult QgsGeometry::transform( const QgsCoordinateTransfor
return QgsGeometry::Success;
}

QgsGeometry::OperationResult QgsGeometry::transform( const QTransform &ct )
QgsGeometry::OperationResult QgsGeometry::transform( const QTransform &ct, double zTranslate, double zScale, double mTranslate, double mScale )
{
if ( !d->geometry )
{
return QgsGeometry::InvalidBaseGeometry;
}

detach();
d->geometry->transform( ct );
d->geometry->transform( ct, zTranslate, zScale, mTranslate, mScale );
return QgsGeometry::Success;
}

Expand Down
12 changes: 8 additions & 4 deletions src/core/geometry/qgsgeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -624,10 +624,10 @@ class CORE_EXPORT QgsGeometry
QgsGeometry removeInteriorRings( double minimumAllowedArea = -1 ) const;

/**
* Translates this geometry by dx, dy
* Translates this geometry by dx, dy, dz and dm.
* \returns OperationResult a result code: success or reason of failure
*/
OperationResult translate( double dx, double dy );
OperationResult translate( double dx, double dy, double dz = 0.0, double dm = 0.0 );

/**
* Transforms this geometry as described by CoordinateTransform ct
Expand All @@ -636,10 +636,14 @@ class CORE_EXPORT QgsGeometry
OperationResult transform( const QgsCoordinateTransform &ct );

/**
* Transforms this geometry as described by QTransform ct
* Transforms the x and y components of the geometry using a QTransform object \a t.
*
* Optionally, the geometry's z values can be scaled via \a zScale and translated via \a zTranslate.
* Similarly, m-values can be scaled via \a mScale and translated via \a mTranslate.
*
* \returns OperationResult a result code: success or reason of failure
*/
OperationResult transform( const QTransform &ct );
OperationResult transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 );

/**
* Rotate this geometry around the Z axis
Expand Down
4 changes: 2 additions & 2 deletions src/core/geometry/qgsgeometrycollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,11 @@ void QgsGeometryCollection::transform( const QgsCoordinateTransform &ct, QgsCoor
clearCache(); //set bounding box invalid
}

void QgsGeometryCollection::transform( const QTransform &t )
void QgsGeometryCollection::transform( const QTransform &t, double zTranslate, double zScale, double mTranslate, double mScale )
{
for ( QgsAbstractGeometry *g : qgis::as_const( mGeometries ) )
{
g->transform( t );
g->transform( t, zTranslate, zScale, mTranslate, mScale );
}
clearCache(); //set bounding box invalid
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgsgeometrycollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class CORE_EXPORT QgsGeometryCollection: public QgsAbstractGeometry

void transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform,
bool transformZ = false ) override;
void transform( const QTransform &t ) override;
void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 ) override;

void draw( QPainter &p ) const override;

Expand Down
12 changes: 11 additions & 1 deletion src/core/geometry/qgslinestring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,15 +772,25 @@ void QgsLineString::transform( const QgsCoordinateTransform &ct, QgsCoordinateTr
clearCache();
}

void QgsLineString::transform( const QTransform &t )
void QgsLineString::transform( const QTransform &t, double zTranslate, double zScale, double mTranslate, double mScale )
{
int nPoints = numPoints();
bool hasZ = is3D();
bool hasM = isMeasure();
for ( int i = 0; i < nPoints; ++i )
{
qreal x, y;
t.map( mX.at( i ), mY.at( i ), &x, &y );
mX[i] = x;
mY[i] = y;
if ( hasZ )
{
mZ[i] = mZ.at( i ) * zScale + zTranslate;
}
if ( hasM )
{
mM[i] = mM.at( i ) * mScale + mTranslate;
}
}
clearCache();
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgslinestring.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class CORE_EXPORT QgsLineString: public QgsCurve

void transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform,
bool transformZ = false ) override;
void transform( const QTransform &t ) override;
void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 ) override;

void addToPainterPath( QPainterPath &path ) const override;
void drawAsPolygon( QPainter &p ) const override;
Expand Down
11 changes: 10 additions & 1 deletion src/core/geometry/qgspoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,13 +494,22 @@ bool QgsPoint::addMValue( double mValue )
return true;
}

void QgsPoint::transform( const QTransform &t )
void QgsPoint::transform( const QTransform &t, double zTranslate, double zScale, double mTranslate, double mScale )
{
clearCache();
qreal x, y;
t.map( mX, mY, &x, &y );
mX = x;
mY = y;

if ( is3D() )
{
mZ = mZ * zScale + zTranslate;
}
if ( isMeasure() )
{
mM = mM * mScale + mTranslate;
}
}


Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgspoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ class CORE_EXPORT QgsPoint: public QgsAbstractGeometry
void draw( QPainter &p ) const override;
void transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform,
bool transformZ = false ) override;
void transform( const QTransform &t ) override;
void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 ) override;
QgsCoordinateSequence coordinateSequence() const override;
int nCoordinates() const override;
int vertexNumberFromVertexId( QgsVertexId id ) const override;
Expand Down
Loading

0 comments on commit dd223d9

Please sign in to comment.