Skip to content
Permalink
Browse files

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 9, 2020
1 parent 61e7a5f commit d3a54eed6f207085ee4b9202c365a43dbca2eb84
@@ -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`
@@ -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 *
* *
@@ -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;

@@ -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;

@@ -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;

@@ -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

@@ -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;

@@ -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;

@@ -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;

@@ -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/;

@@ -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.
@@ -1234,4 +1247,6 @@ class CORE_EXPORT QgsGeometryConstPartIterator

};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsAbstractGeometry::WkbFlags )

#endif //QGSABSTRACTGEOMETRYV2
@@ -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 );
@@ -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;
@@ -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;
}
@@ -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;
@@ -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;
}
@@ -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;
@@ -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
@@ -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
@@ -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;
}
@@ -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;
@@ -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 );
@@ -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;
@@ -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 );
@@ -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;
@@ -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 );

@@ -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 )
{
@@ -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;

/**

0 comments on commit d3a54ee

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