Skip to content

Commit

Permalink
Remove caching of WKB from QgsGeometry (was kept just for compatibility)
Browse files Browse the repository at this point in the history
Also improves the API to export/import WKB as QByteArray
  • Loading branch information
wonder-sk committed Nov 14, 2016
1 parent 8b1adc5 commit d729951
Show file tree
Hide file tree
Showing 53 changed files with 305 additions and 434 deletions.
8 changes: 8 additions & 0 deletions doc/api_break.dox
Original file line number Diff line number Diff line change
Expand Up @@ -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}
----------------
Expand Down Expand Up @@ -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
Expand Down
13 changes: 4 additions & 9 deletions python/core/geometry/qgsabstractgeometry.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions python/core/geometry/qgscircularstring.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions python/core/geometry/qgscompoundcurve.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions python/core/geometry/qgscurvepolygon.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 10 additions & 20 deletions python/core/geometry/qgsgeometry.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions python/core/geometry/qgsgeometrycollection.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions python/core/geometry/qgslinestring.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions python/core/geometry/qgspointv2.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions python/core/geometry/qgspolygon.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/scripts/Fill_holes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/analysis/interpolation/qgsinterpolator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion src/analysis/interpolation/qgstininterpolator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions src/analysis/openstreetmap/qgsosmdatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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 );

Expand Down
6 changes: 4 additions & 2 deletions src/analysis/vector/qgsgeometryanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 )
Expand Down
13 changes: 4 additions & 9 deletions src/core/geometry/qgsabstractgeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 8 additions & 12 deletions src/core/geometry/qgscircularstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/core/geometry/qgscircularstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit d729951

Please sign in to comment.