Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add flags to control geometry to WKB export behavior
With initial flag to force conversion of triangle types to polygons
  • Loading branch information
nyalldawson committed Jun 10, 2020
1 parent 61e7a5f commit d3a54ee
Show file tree
Hide file tree
Showing 26 changed files with 87 additions and 32 deletions.
14 changes: 13 additions & 1 deletion python/core/auto_generated/geometry/qgsabstractgeometry.sip.in
Expand Up @@ -189,10 +189,19 @@ Sets the geometry from a WKT string.
%End


virtual QByteArray asWkb() const = 0;
enum WkbFlag
{
FlagExportTrianglesAsPolygons,
};
typedef QFlags<QgsAbstractGeometry::WkbFlag> WkbFlags;


virtual QByteArray asWkb( WkbFlags flags = 0 ) const = 0;
%Docstring
Returns a WKB representation of the geometry.

The optional ``flags`` argument specifies flags controlling WKB export behavior (since QGIS 3.14).

.. seealso:: :py:func:`asWkt`

.. seealso:: :py:func:`asGml2`
Expand Down Expand Up @@ -991,6 +1000,9 @@ Returns next part of the geometry (undefined behavior if hasNext() returns ``Fal

};

QFlags<QgsAbstractGeometry::WkbFlag> operator|(QgsAbstractGeometry::WkbFlag f1, QFlags<QgsAbstractGeometry::WkbFlag> f2);


/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
Expand Up @@ -71,7 +71,7 @@ to ``p2`` will be used (i.e. winding the other way around the circle).
virtual bool fromWkt( const QString &wkt );


virtual QByteArray asWkb() const;
virtual QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = 0 ) const;

virtual QString asWkt( int precision = 17 ) const;

Expand Down
Expand Up @@ -42,7 +42,7 @@ Compound curve geometry type
virtual bool fromWkt( const QString &wkt );


virtual QByteArray asWkb() const;
virtual QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = 0 ) const;

virtual QString asWkt( int precision = 17 ) const;

Expand Down
2 changes: 1 addition & 1 deletion python/core/auto_generated/geometry/qgscurvepolygon.sip.in
Expand Up @@ -46,7 +46,7 @@ Curve polygon geometry type
virtual bool fromWkt( const QString &wkt );


virtual QByteArray asWkb() const;
virtual QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = 0 ) const;

virtual QString asWkt( int precision = 17 ) const;

Expand Down
4 changes: 3 additions & 1 deletion python/core/auto_generated/geometry/qgsgeometry.sip.in
Expand Up @@ -1522,10 +1522,12 @@ is null, a ValueError will be raised.



QByteArray asWkb() const;
QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = 0 ) const;
%Docstring
Export the geometry to WKB

The optional ``flags`` argument specifies flags controlling WKB export behavior (since QGIS 3.14).

.. versionadded:: 3.0
%End

Expand Down
Expand Up @@ -154,7 +154,7 @@ An IndexError will be raised if no geometry with the specified index exists.

virtual bool fromWkt( const QString &wkt );

virtual QByteArray asWkb() const;
virtual QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = 0 ) const;

virtual QString asWkt( int precision = 17 ) const;

Expand Down
2 changes: 1 addition & 1 deletion python/core/auto_generated/geometry/qgslinestring.sip.in
Expand Up @@ -424,7 +424,7 @@ segment in the line.
virtual bool fromWkt( const QString &wkt );


virtual QByteArray asWkb() const;
virtual QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = 0 ) const;

virtual QString asWkt( int precision = 17 ) const;

Expand Down
2 changes: 1 addition & 1 deletion python/core/auto_generated/geometry/qgspoint.sip.in
Expand Up @@ -367,7 +367,7 @@ M value is preserved.

virtual bool fromWkt( const QString &wkt );

virtual QByteArray asWkb() const;
virtual QByteArray asWkb( QgsAbstractGeometry::WkbFlags = 0 ) const;

virtual QString asWkt( int precision = 17 ) const;

Expand Down
2 changes: 1 addition & 1 deletion python/core/auto_generated/geometry/qgspolygon.sip.in
Expand Up @@ -41,7 +41,7 @@ Ownership of ``exterior`` and ``rings`` is transferred to the polygon.

virtual bool fromWkb( QgsConstWkbPtr &wkb );

virtual QByteArray asWkb() const;
virtual QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = 0 ) const;

virtual QgsPolygon *surfaceToPolygon() const /Factory/;

Expand Down
17 changes: 16 additions & 1 deletion src/core/geometry/qgsabstractgeometry.h
Expand Up @@ -238,15 +238,28 @@ class CORE_EXPORT QgsAbstractGeometry

//export

/**
* WKB export flags.
* \since QGIS 3.14
*/
enum WkbFlag
{
FlagExportTrianglesAsPolygons = 1 << 0, //!< Triangles should be exported as polygon geometries
};
Q_DECLARE_FLAGS( WkbFlags, WkbFlag )

/**
* Returns a WKB representation of the geometry.
*
* The optional \a flags argument specifies flags controlling WKB export behavior (since QGIS 3.14).
*
* \see asWkt
* \see asGml2
* \see asGml3
* \see asJson()
* \since QGIS 3.0
*/
virtual QByteArray asWkb() const = 0;
virtual QByteArray asWkb( WkbFlags flags = nullptr ) const = 0;

/**
* Returns a WKT representation of the geometry.
Expand Down Expand Up @@ -1234,4 +1247,6 @@ class CORE_EXPORT QgsGeometryConstPartIterator

};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsAbstractGeometry::WkbFlags )

#endif //QGSABSTRACTGEOMETRYV2
2 changes: 1 addition & 1 deletion src/core/geometry/qgscircularstring.cpp
Expand Up @@ -321,7 +321,7 @@ bool QgsCircularString::fromWkt( const QString &wkt )
return true;
}

QByteArray QgsCircularString::asWkb() const
QByteArray QgsCircularString::asWkb( WkbFlags ) const
{
int binarySize = sizeof( char ) + sizeof( quint32 ) + sizeof( quint32 );
binarySize += numPoints() * ( 2 + is3D() + isMeasure() ) * sizeof( double );
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgscircularstring.h
Expand Up @@ -75,7 +75,7 @@ class CORE_EXPORT QgsCircularString: public QgsCurve
bool fromWkb( QgsConstWkbPtr &wkb ) override;
bool fromWkt( const QString &wkt ) override;

QByteArray asWkb() const override;
QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = nullptr ) const override;
QString asWkt( int precision = 17 ) const override;
QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
Expand Down
4 changes: 2 additions & 2 deletions src/core/geometry/qgscompoundcurve.cpp
Expand Up @@ -224,14 +224,14 @@ bool QgsCompoundCurve::fromWkt( const QString &wkt )
return true;
}

QByteArray QgsCompoundCurve::asWkb() const
QByteArray QgsCompoundCurve::asWkb( WkbFlags flags ) const
{
int binarySize = sizeof( char ) + sizeof( quint32 ) + sizeof( quint32 );
QVector<QByteArray> wkbForCurves;
wkbForCurves.reserve( mCurves.size() );
for ( const QgsCurve *curve : mCurves )
{
QByteArray wkbForCurve = curve->asWkb();
QByteArray wkbForCurve = curve->asWkb( flags );
binarySize += wkbForCurve.length();
wkbForCurves << wkbForCurve;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgscompoundcurve.h
Expand Up @@ -46,7 +46,7 @@ class CORE_EXPORT QgsCompoundCurve: public QgsCurve
bool fromWkb( QgsConstWkbPtr &wkb ) override;
bool fromWkt( const QString &wkt ) override;

QByteArray asWkb() const override;
QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = nullptr ) const override;
QString asWkt( int precision = 17 ) const override;
QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
Expand Down
6 changes: 3 additions & 3 deletions src/core/geometry/qgscurvepolygon.cpp
Expand Up @@ -285,20 +285,20 @@ QgsRectangle QgsCurvePolygon::calculateBoundingBox() const
return QgsRectangle();
}

QByteArray QgsCurvePolygon::asWkb() const
QByteArray QgsCurvePolygon::asWkb( WkbFlags flags ) const
{
int binarySize = sizeof( char ) + sizeof( quint32 ) + sizeof( quint32 );
QVector<QByteArray> wkbForRings;
wkbForRings.reserve( 1 + mInteriorRings.size() );
if ( mExteriorRing )
{
QByteArray wkb( mExteriorRing->asWkb() );
QByteArray wkb( mExteriorRing->asWkb( flags ) );
binarySize += wkb.length();
wkbForRings << wkb;
}
for ( const QgsCurve *curve : mInteriorRings )
{
QByteArray wkb( curve->asWkb() );
QByteArray wkb( curve->asWkb( flags ) );
binarySize += wkb.length();
wkbForRings << wkb;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgscurvepolygon.h
Expand Up @@ -51,7 +51,7 @@ class CORE_EXPORT QgsCurvePolygon: public QgsSurface
bool fromWkb( QgsConstWkbPtr &wkb ) override;
bool fromWkt( const QString &wkt ) override;

QByteArray asWkb() const override;
QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = nullptr ) const override;
QString asWkt( int precision = 17 ) const override;
QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
Expand Down
4 changes: 2 additions & 2 deletions src/core/geometry/qgsgeometry.cpp
Expand Up @@ -2507,9 +2507,9 @@ QVector<QgsPointXY> QgsGeometry::randomPointsInPolygon( int count, unsigned long
}
///@endcond

QByteArray QgsGeometry::asWkb() const
QByteArray QgsGeometry::asWkb( QgsAbstractGeometry::WkbFlags flags ) const
{
return d->geometry ? d->geometry->asWkb() : QByteArray();
return d->geometry ? d->geometry->asWkb( flags ) : QByteArray();
}

QVector<QgsGeometry> QgsGeometry::asGeometryCollection() const
Expand Down
5 changes: 4 additions & 1 deletion src/core/geometry/qgsgeometry.h
Expand Up @@ -1544,9 +1544,12 @@ class CORE_EXPORT QgsGeometry

/**
* Export the geometry to WKB
*
* The optional \a flags argument specifies flags controlling WKB export behavior (since QGIS 3.14).
*
* \since QGIS 3.0
*/
QByteArray asWkb() const;
QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = nullptr ) const;

/**
* Exports the geometry to WKT
Expand Down
4 changes: 2 additions & 2 deletions src/core/geometry/qgsgeometrycollection.cpp
Expand Up @@ -357,15 +357,15 @@ bool QgsGeometryCollection::fromWkt( const QString &wkt )
<< new QgsMultiCurve << new QgsMultiSurface, QStringLiteral( "GeometryCollection" ) );
}

QByteArray QgsGeometryCollection::asWkb() const
QByteArray QgsGeometryCollection::asWkb( WkbFlags flags ) const
{
int binarySize = sizeof( char ) + sizeof( quint32 ) + sizeof( quint32 );
QVector<QByteArray> wkbForGeometries;
for ( const QgsAbstractGeometry *geom : mGeometries )
{
if ( geom )
{
QByteArray wkb( geom->asWkb() );
QByteArray wkb( geom->asWkb( flags ) );
binarySize += wkb.length();
wkbForGeometries << wkb;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgsgeometrycollection.h
Expand Up @@ -179,7 +179,7 @@ class CORE_EXPORT QgsGeometryCollection: public QgsAbstractGeometry

bool fromWkb( QgsConstWkbPtr &wkb ) override;
bool fromWkt( const QString &wkt ) override;
QByteArray asWkb() const override;
QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = nullptr ) const override;
QString asWkt( int precision = 17 ) const override;
QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgslinestring.cpp
Expand Up @@ -441,7 +441,7 @@ bool QgsLineString::fromWkt( const QString &wkt )
return true;
}

QByteArray QgsLineString::asWkb() const
QByteArray QgsLineString::asWkb( WkbFlags ) const
{
int binarySize = sizeof( char ) + sizeof( quint32 ) + sizeof( quint32 );
binarySize += numPoints() * ( 2 + is3D() + isMeasure() ) * sizeof( double );
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgslinestring.h
Expand Up @@ -588,7 +588,7 @@ class CORE_EXPORT QgsLineString: public QgsCurve
bool fromWkb( QgsConstWkbPtr &wkb ) override;
bool fromWkt( const QString &wkt ) override;

QByteArray asWkb() const override;
QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = nullptr ) const override;
QString asWkt( int precision = 17 ) const override;
QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgspoint.cpp
Expand Up @@ -209,7 +209,7 @@ bool QgsPoint::fromWkt( const QString &wkt )
* See details in QEP #17
****************************************************************************/

QByteArray QgsPoint::asWkb() const
QByteArray QgsPoint::asWkb( WkbFlags ) const
{
int binarySize = sizeof( char ) + sizeof( quint32 );
binarySize += ( 2 + is3D() + isMeasure() ) * sizeof( double );
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgspoint.h
Expand Up @@ -489,7 +489,7 @@ class CORE_EXPORT QgsPoint: public QgsAbstractGeometry
void clear() override;
bool fromWkb( QgsConstWkbPtr &wkb ) override;
bool fromWkt( const QString &wkt ) override;
QByteArray asWkb() const override;
QByteArray asWkb( QgsAbstractGeometry::WkbFlags = nullptr ) const override;
QString asWkt( int precision = 17 ) const override;
QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
Expand Down
27 changes: 25 additions & 2 deletions src/core/geometry/qgspolygon.cpp
Expand Up @@ -121,7 +121,7 @@ bool QgsPolygon::fromWkb( QgsConstWkbPtr &wkbPtr )
return true;
}

QByteArray QgsPolygon::asWkb() const
QByteArray QgsPolygon::asWkb( QgsAbstractGeometry::WkbFlags flags ) const
{
int binarySize = sizeof( char ) + sizeof( quint32 ) + sizeof( quint32 );

Expand All @@ -139,7 +139,30 @@ QByteArray QgsPolygon::asWkb() const
wkbArray.resize( binarySize );
QgsWkbPtr wkb( wkbArray );
wkb << static_cast<char>( QgsApplication::endian() );
wkb << static_cast<quint32>( wkbType() );

QgsWkbTypes::Type type = wkbType();
if ( flags & FlagExportTrianglesAsPolygons )
{
switch ( type )
{
case QgsWkbTypes::Triangle:
type = QgsWkbTypes::Polygon;
break;
case QgsWkbTypes::TriangleZ:
type = QgsWkbTypes::PolygonZ;
break;
case QgsWkbTypes::TriangleM:
type = QgsWkbTypes::PolygonM;
break;
case QgsWkbTypes::TriangleZM:
type = QgsWkbTypes::PolygonZM;
break;
default:
break;
}
}
wkb << static_cast<quint32>( type );

wkb << static_cast<quint32>( ( nullptr != mExteriorRing ) + mInteriorRings.size() );
if ( mExteriorRing )
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgspolygon.h
Expand Up @@ -48,7 +48,7 @@ class CORE_EXPORT QgsPolygon: public QgsCurvePolygon
QgsPolygon *clone() const override SIP_FACTORY;
void clear() override;
bool fromWkb( QgsConstWkbPtr &wkb ) override;
QByteArray asWkb() const override;
QByteArray asWkb( QgsAbstractGeometry::WkbFlags flags = nullptr ) const override;
QgsPolygon *surfaceToPolygon() const override SIP_FACTORY;

/**
Expand Down

0 comments on commit d3a54ee

Please sign in to comment.