Skip to content
Permalink
Browse files

Remove caching of WKB from QgsGeometry (was kept just for compatibility)

Also improves the API to export/import WKB as QByteArray
  • Loading branch information
wonder-sk committed Nov 14, 2016
1 parent 8b1adc5 commit d729951dcd13ed38d71ab2b850d1673bd794a569
Showing with 305 additions and 434 deletions.
  1. +8 −0 doc/api_break.dox
  2. +4 −9 python/core/geometry/qgsabstractgeometry.sip
  3. +2 −3 python/core/geometry/qgscircularstring.sip
  4. +2 −3 python/core/geometry/qgscompoundcurve.sip
  5. +2 −3 python/core/geometry/qgscurvepolygon.sip
  6. +10 −20 python/core/geometry/qgsgeometry.sip
  7. +2 −3 python/core/geometry/qgsgeometrycollection.sip
  8. +2 −3 python/core/geometry/qgslinestring.sip
  9. +2 −3 python/core/geometry/qgspointv2.sip
  10. +2 −3 python/core/geometry/qgspolygon.sip
  11. +1 −1 python/plugins/processing/algs/qgis/scripts/Fill_holes.py
  12. +2 −1 src/analysis/interpolation/qgsinterpolator.cpp
  13. +2 −1 src/analysis/interpolation/qgstininterpolator.cpp
  14. +6 −2 src/analysis/openstreetmap/qgsosmdatabase.cpp
  15. +4 −2 src/analysis/vector/qgsgeometryanalyzer.cpp
  16. +4 −9 src/core/geometry/qgsabstractgeometry.h
  17. +8 −12 src/core/geometry/qgscircularstring.cpp
  18. +2 −3 src/core/geometry/qgscircularstring.h
  19. +14 −22 src/core/geometry/qgscompoundcurve.cpp
  20. +2 −3 src/core/geometry/qgscompoundcurve.h
  21. +18 −30 src/core/geometry/qgscurvepolygon.cpp
  22. +2 −3 src/core/geometry/qgscurvepolygon.h
  23. +18 −83 src/core/geometry/qgsgeometry.cpp
  24. +9 −11 src/core/geometry/qgsgeometry.h
  25. +15 −24 src/core/geometry/qgsgeometrycollection.cpp
  26. +2 −3 src/core/geometry/qgsgeometrycollection.h
  27. +2 −2 src/core/geometry/qgsgeometryfactory.cpp
  28. +2 −1 src/core/geometry/qgsgeometryfactory.h
  29. +8 −12 src/core/geometry/qgslinestring.cpp
  30. +2 −3 src/core/geometry/qgslinestring.h
  31. +9 −13 src/core/geometry/qgspointv2.cpp
  32. +2 −3 src/core/geometry/qgspointv2.h
  33. +11 −15 src/core/geometry/qgspolygon.cpp
  34. +2 −3 src/core/geometry/qgspolygon.h
  35. +15 −0 src/core/geometry/qgswkbptr.cpp
  36. +10 −0 src/core/geometry/qgswkbptr.h
  37. +3 −2 src/core/qgsogcutils.cpp
  38. +3 −3 src/core/qgspointlocator.cpp
  39. +4 −2 src/core/qgsvectorfilewriter.cpp
  40. +2 −2 src/providers/db2/qgsdb2provider.cpp
  41. +2 −1 src/providers/gpx/qgsgpxprovider.cpp
  42. +2 −2 src/providers/mssql/qgsmssqlprovider.cpp
  43. +8 −7 src/providers/ogr/qgsogrprovider.cpp
  44. +4 −3 src/providers/postgres/qgspostgresprovider.cpp
  45. +5 −3 src/providers/spatialite/qgsspatialiteprovider.cpp
  46. +4 −4 src/providers/virtual/qgsvirtuallayerblob.cpp
  47. +1 −6 src/providers/wfs/qgswfsfeatureiterator.cpp
  48. +4 −8 src/providers/wfs/qgswfsshareddata.cpp
  49. +1 −3 src/providers/wfs/qgswfsutils.cpp
  50. +10 −10 tests/src/core/testqgsfeature.cpp
  51. +39 −61 tests/src/core/testqgsgeometry.cpp
  52. +2 −2 tests/src/python/test_provider_ogr_gpkg.py
  53. +3 −3 tests/src/python/test_qgsgeometry.py
@@ -318,6 +318,13 @@ QgsAnnotation {#qgis_api_break_3_0_QgsAnnotation}
- mapPositionFixed() has been renamed to hasFixedMapPosition()


QgsAbstractGeometry {#qgis_api_break_3_0_QgsAbstractGeometry}
-------------------

- asWkb() returns QByteArray instead of new raw pointer
- wkbSize() has been removed, use asWkb() to get length of returned QByteArray
- fromWkb() gets the WKB pointer passed by reference instead of value, so that caller may to find out where the parsing ended


QgsActionManager {#qgis_api_break_3_0_QgsActionManager}
----------------
@@ -836,6 +843,7 @@ QgsGeometry {#qgis_api_break_3_0_QgsGeometry}
value instead of a pointer. The biggest impact with this change is that PyQGIS code should not compare a geometry
result to None, but instead either use a boolean test (`if g.buffer(10):`) or explicitly use the isEmpty()
method to determine if a geometry is valid.
- wkbSize() and asWkb() has been replaced by exportToWkb(). WKB representation is no longer cached within QgsGeometry
- int addPart( const QList<QgsPoint> &points, QgsWkbTypes::GeometryType geomType ) has been renamed to addPoints
- int addPart( const QList<QgsPointV2> &points, QgsWkbTypes::GeometryType geomType ) has been renamed to addPointsV2
- static bool compare( const QgsPolyline& p1, const QgsPolyline& p2, double epsilon ) has been renamed to comparePolylines
@@ -140,9 +140,10 @@ class QgsAbstractGeometry
//import

/** Sets the geometry from a WKB string.
* After successful read the wkb argument will be at the position where the reading has stopped.
* @see fromWkt
*/
virtual bool fromWkb( QgsConstWkbPtr wkb ) = 0;
virtual bool fromWkb( QgsConstWkbPtr& wkb ) = 0;

/** Sets the geometry from a WKT string.
* @see fromWkb
@@ -151,20 +152,14 @@ class QgsAbstractGeometry

//export

/** Returns the size of the WKB representation of the geometry.
* @see asWkb
*/
virtual int wkbSize() const = 0;

/** Returns a WKB representation of the geometry.
* @param binarySize will be set to the size of the returned WKB string
* @see wkbSize
* @see asWkt
* @see asGML2
* @see asGML3
* @see asJSON
* @note added in 3.0
*/
virtual unsigned char* asWkb( int& binarySize ) const = 0;
virtual QByteArray asWkb() const = 0;

/** Returns a WKT representation of the geometry.
* @param precision number of decimal places for coordinates
@@ -16,11 +16,10 @@ class QgsCircularString: public QgsCurve
virtual QgsCircularString* clone() const /Factory/;
virtual void clear();

virtual bool fromWkb( QgsConstWkbPtr wkb );
virtual bool fromWkb( QgsConstWkbPtr& wkb );
virtual bool fromWkt( const QString& wkt );

int wkbSize() const;
unsigned char* asWkb( int& binarySize ) const;
QByteArray asWkb() const;
QString asWkt( int precision = 17 ) const;
QDomElement asGML2( QDomDocument& doc, int precision = 17, const QString& ns = "gml" ) const;
QDomElement asGML3( QDomDocument& doc, int precision = 17, const QString& ns = "gml" ) const;
@@ -18,11 +18,10 @@ class QgsCompoundCurve: public QgsCurve
virtual QgsCompoundCurve* clone() const /Factory/;
virtual void clear();

virtual bool fromWkb( QgsConstWkbPtr wkb );
virtual bool fromWkb( QgsConstWkbPtr& wkb );
virtual bool fromWkt( const QString& wkt );

int wkbSize() const;
unsigned char* asWkb( int& binarySize ) const;
QByteArray asWkb() const;
QString asWkt( int precision = 17 ) const;
QDomElement asGML2( QDomDocument& doc, int precision = 17, const QString& ns = "gml" ) const;
QDomElement asGML3( QDomDocument& doc, int precision = 17, const QString& ns = "gml" ) const;
@@ -15,11 +15,10 @@ class QgsCurvePolygon: public QgsSurface
virtual QgsCurvePolygon* clone() const /Factory/;
void clear();

virtual bool fromWkb( QgsConstWkbPtr wkb );
virtual bool fromWkb( QgsConstWkbPtr& wkb );
virtual bool fromWkt( const QString& wkt );

int wkbSize() const;
unsigned char* asWkb( int& binarySize ) const;
QByteArray asWkb() const;
QString asWkt( int precision = 17 ) const;
QDomElement asGML2( QDomDocument& doc, int precision = 17, const QString& ns = "gml" ) const;
QDomElement asGML3( QDomDocument& doc, int precision = 17, const QString& ns = "gml" ) const;
@@ -93,30 +93,15 @@ class QgsGeometry
/**
Set the geometry, feeding in the buffer containing OGC Well-Known Binary and the buffer's length.
This class will take ownership of the buffer.
@note not available in python bindings
*/
void fromWkb( unsigned char * wkb /Array/, int length /ArraySize/ );
%MethodCode
// create copy of Python's string and pass it to fromWkb()
unsigned char * copy = new unsigned char[a1];
memcpy(copy, a0, a1);
sipCpp->fromWkb(copy, a1);
%End
// void fromWkb( unsigned char *wkb, int length );

/**
Returns the buffer containing this geometry in WKB format.
You may wish to use in conjunction with wkbSize().
@see wkbSize
* Set the geometry, feeding in the buffer containing OGC Well-Known Binary
* @note added in 3.0
*/
SIP_PYOBJECT asWkb();
%MethodCode
sipRes = PyBytes_FromStringAndSize((const char *)sipCpp->asWkb(), sipCpp->wkbSize());
%End

/**
* Returns the size of the WKB in asWkb().
* @see asWkb
*/
int wkbSize() const;
void fromWkb( const QByteArray& wkb );

/** Returns a geos geometry. QgsGeometry retains ownership of the geometry, so the returned object should not be deleted.
* @param precision The precision of the grid to which to snap the geometry vertices. If 0, no snapping is performed.
@@ -593,6 +578,11 @@ class QgsGeometry
/** Returns an extruded version of this geometry. */
QgsGeometry extrude( double x, double y );

/** Export the geometry to WKB
* @note added in 3.0
*/
QByteArray exportToWkb() const;

/** Exports the geometry to WKT
* @note precision parameter added in 2.4
* @return true in case of success and false else
@@ -55,10 +55,9 @@ class QgsGeometryCollection: public QgsAbstractGeometry

virtual void draw( QPainter& p ) const;

bool fromWkb( QgsConstWkbPtr wkb );
bool fromWkb( QgsConstWkbPtr& wkb );
virtual bool fromWkt( const QString& wkt );
int wkbSize() const;
unsigned char* asWkb( int& binarySize ) const;
QByteArray asWkb() const;
QString asWkt( int precision = 17 ) const;
QDomElement asGML2( QDomDocument& doc, int precision = 17, const QString& ns = "gml" ) const;
QDomElement asGML3( QDomDocument& doc, int precision = 17, const QString& ns = "gml" ) const;
@@ -107,11 +107,10 @@ class QgsLineString: public QgsCurve
virtual QgsLineString* clone() const /Factory/;
virtual void clear();

virtual bool fromWkb( QgsConstWkbPtr wkb );
virtual bool fromWkb( QgsConstWkbPtr& wkb );
virtual bool fromWkt( const QString& wkt );

int wkbSize() const;
unsigned char* asWkb( int& binarySize ) const;
QByteArray asWkb() const;
QString asWkt( int precision = 17 ) const;
QDomElement asGML2( QDomDocument& doc, int precision = 17, const QString& ns = "gml" ) const;
QDomElement asGML3( QDomDocument& doc, int precision = 17, const QString& ns = "gml" ) const;
@@ -185,10 +185,9 @@ class QgsPointV2: public QgsAbstractGeometry
virtual int dimension() const;
virtual QgsPointV2* clone() const /Factory/;
void clear();
virtual bool fromWkb( QgsConstWkbPtr wkb );
virtual bool fromWkb( QgsConstWkbPtr& wkb );
virtual bool fromWkt( const QString& wkt );
int wkbSize() const;
unsigned char* asWkb( int& binarySize ) const;
QByteArray asWkb() const;
QString asWkt( int precision = 17 ) const;
QDomElement asGML2( QDomDocument& doc, int precision = 17, const QString& ns = "gml" ) const;
QDomElement asGML3( QDomDocument& doc, int precision = 17, const QString& ns = "gml" ) const;
@@ -14,12 +14,11 @@ class QgsPolygonV2: public QgsCurvePolygon
virtual QgsPolygonV2* clone() const /Factory/;
void clear();

virtual bool fromWkb( QgsConstWkbPtr wkb );
virtual bool fromWkb( QgsConstWkbPtr& wkb );

// inherited: bool fromWkt( const QString &wkt );

int wkbSize() const;
unsigned char* asWkb( int& binarySize ) const;
QByteArray asWkb() const;
// inherited: QString asWkt( int precision = 17 ) const;
// inherited: QDomElement asGML2( QDomDocument& doc, int precision = 17, const QString& ns = "gml" ) const;
// inherited: QDomElement asGML3( QDomDocument& doc, int precision = 17, const QString& ns = "gml" ) const;
@@ -25,7 +25,7 @@
progress.setPercentage(int(100 * l / n))
l += 1

g = loads(feat.geometry().asWkb())
g = loads(feat.geometry().exportToWkb())

if g.geom_type == 'MultiPolygon':
resg = [Polygon(p.exterior,
@@ -109,7 +109,8 @@ int QgsInterpolator::addVerticesToCache( const QgsGeometry& geom, bool zCoord, d
return 1;

bool hasZValue = false;
QgsConstWkbPtr currentWkbPtr( geom.asWkb(), geom.wkbSize() );
QByteArray wkb( geom.exportToWkb() );
QgsConstWkbPtr currentWkbPtr( wkb );
currentWkbPtr.readHeader();
vertexData theVertex; //the current vertex

@@ -201,7 +201,8 @@ int QgsTINInterpolator::insertData( QgsFeature* f, bool zCoord, int attr, InputT
//parse WKB. It is ugly, but we cannot use the methods with QgsPoint because they don't contain z-values for 25D types
bool hasZValue = false;
double x, y, z;
QgsConstWkbPtr currentWkbPtr( g.asWkb(), g.wkbSize() );
QByteArray wkb( g.exportToWkb() );
QgsConstWkbPtr currentWkbPtr( wkb );
currentWkbPtr.readHeader();
//maybe a structure or break line
Line3D* line = nullptr;
@@ -423,7 +423,8 @@ void QgsOSMDatabase::exportSpatiaLiteNodes( const QString& tableName, const QStr
sqlite3_bind_null( stmtInsert, ++col );
}

sqlite3_bind_blob( stmtInsert, ++col, geom.asWkb(), ( int ) geom.wkbSize(), SQLITE_STATIC );
QByteArray wkb( geom.exportToWkb() );
sqlite3_bind_blob( stmtInsert, ++col, wkb.constData(), wkb.length(), SQLITE_STATIC );

int insertRes = sqlite3_step( stmtInsert );
if ( insertRes != SQLITE_DONE )
@@ -504,7 +505,10 @@ void QgsOSMDatabase::exportSpatiaLiteWays( bool closed, const QString& tableName
}

if ( !geom.isEmpty() )
sqlite3_bind_blob( stmtInsert, ++col, geom.asWkb(), ( int ) geom.wkbSize(), SQLITE_STATIC );
{
QByteArray wkb( geom.exportToWkb() );
sqlite3_bind_blob( stmtInsert, ++col, wkb.constData(), wkb.length(), SQLITE_STATIC );
}
else
sqlite3_bind_null( stmtInsert, ++col );

@@ -1153,7 +1153,8 @@ QgsGeometry QgsGeometryAnalyzer::locateBetweenMeasures( double fromMeasure, doub
QgsMultiPolyline resultGeom;

//need to go with WKB and z coordinate until QgsGeometry supports M values
QgsConstWkbPtr wkbPtr( lineGeom.asWkb(), lineGeom.wkbSize() );
QByteArray wkb( lineGeom.exportToWkb() );
QgsConstWkbPtr wkbPtr( wkb );
wkbPtr.readHeader();

QgsWkbTypes::Type wkbType = lineGeom.wkbType();
@@ -1194,7 +1195,8 @@ QgsGeometry QgsGeometryAnalyzer::locateAlongMeasure( double measure, const QgsGe
QgsMultiPoint resultGeom;

//need to go with WKB and z coordinate until QgsGeometry supports M values
QgsConstWkbPtr wkbPtr( lineGeom.asWkb(), lineGeom.wkbSize() );
QByteArray wkb( lineGeom.exportToWkb() );
QgsConstWkbPtr wkbPtr( wkb );
QgsWkbTypes::Type wkbType = lineGeom.wkbType();

if ( wkbType != QgsWkbTypes::LineString25D && wkbType != QgsWkbTypes::MultiLineString25D )
@@ -125,9 +125,10 @@ class CORE_EXPORT QgsAbstractGeometry
//import

/** Sets the geometry from a WKB string.
* After successful read the wkb argument will be at the position where the reading has stopped.
* @see fromWkt
*/
virtual bool fromWkb( QgsConstWkbPtr wkb ) = 0;
virtual bool fromWkb( QgsConstWkbPtr& wkb ) = 0;

/** Sets the geometry from a WKT string.
* @see fromWkb
@@ -136,20 +137,14 @@ class CORE_EXPORT QgsAbstractGeometry

//export

/** Returns the size of the WKB representation of the geometry.
* @see asWkb
*/
virtual int wkbSize() const = 0;

/** Returns a WKB representation of the geometry.
* @param binarySize will be set to the size of the returned WKB string
* @see wkbSize
* @see asWkt
* @see asGML2
* @see asGML3
* @see asJSON
* @note added in 3.0
*/
virtual unsigned char* asWkb( int& binarySize ) const = 0;
virtual QByteArray asWkb() const = 0;

/** Returns a WKT representation of the geometry.
* @param precision number of decimal places for coordinates
@@ -207,7 +207,7 @@ QgsPointSequence QgsCircularString::compassPointsOnSegment( double p1Angle, doub
return pointList;
}

bool QgsCircularString::fromWkb( QgsConstWkbPtr wkbPtr )
bool QgsCircularString::fromWkb( QgsConstWkbPtr& wkbPtr )
{
if ( !wkbPtr )
return false;
@@ -260,24 +260,20 @@ bool QgsCircularString::fromWkt( const QString& wkt )
return true;
}

int QgsCircularString::wkbSize() const
QByteArray QgsCircularString::asWkb() const
{
int size = sizeof( char ) + sizeof( quint32 ) + sizeof( quint32 );
size += numPoints() * ( 2 + is3D() + isMeasure() ) * sizeof( double );
return size;
}
int binarySize = sizeof( char ) + sizeof( quint32 ) + sizeof( quint32 );
binarySize += numPoints() * ( 2 + is3D() + isMeasure() ) * sizeof( double );

unsigned char* QgsCircularString::asWkb( int& binarySize ) const
{
binarySize = wkbSize();
unsigned char* geomPtr = new unsigned char[binarySize];
QgsWkbPtr wkb( geomPtr, binarySize );
QByteArray wkbArray;
wkbArray.resize( binarySize );
QgsWkbPtr wkb( wkbArray );
wkb << static_cast<char>( QgsApplication::endian() );
wkb << static_cast<quint32>( wkbType() );
QgsPointSequence pts;
points( pts );
QgsGeometryUtils::pointsToWKB( wkb, pts, is3D(), isMeasure() );
return geomPtr;
return wkbArray;
}

QString QgsCircularString::asWkt( int precision ) const
@@ -41,11 +41,10 @@ class CORE_EXPORT QgsCircularString: public QgsCurve
virtual QgsCircularString* clone() const override;
virtual void clear() override;

virtual bool fromWkb( QgsConstWkbPtr wkb ) override;
virtual bool fromWkb( QgsConstWkbPtr& wkb ) override;
virtual bool fromWkt( const QString& wkt ) override;

int wkbSize() const override;
unsigned char* asWkb( int& binarySize ) const override;
QByteArray asWkb() const override;
QString asWkt( int precision = 17 ) const override;
QDomElement asGML2( QDomDocument& doc, int precision = 17, const QString& ns = "gml" ) const override;
QDomElement asGML3( QDomDocument& doc, int precision = 17, const QString& ns = "gml" ) const override;

0 comments on commit d729951

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