Navigation Menu

Skip to content

Commit

Permalink
QgsFeature cleanup, part 1
Browse files Browse the repository at this point in the history
Deprecate some QgsFeature methods which take or return pointers,
update other classes as required
  • Loading branch information
nyalldawson committed Jul 31, 2016
1 parent 429fe2d commit aceddae
Show file tree
Hide file tree
Showing 136 changed files with 869 additions and 840 deletions.
31 changes: 31 additions & 0 deletions doc/api_break.dox
Expand Up @@ -212,6 +212,15 @@ attributeIndexes(), pkAttributeIndexes(), isSaveAndLoadStyleToDBSupported()</li>
</li>
</ul>

\subsection qgis_api_break_3_0_QgsFeature QgsFeature

<ul>
<li>geometryAndOwnership() has been removed. Use geometry() instead.</li>
<li>setGeometryAndOwnership() has been removed. Use setGeometry() instead.</li>
<li>The setGeometry( QgsGeometry* ) method has been removed, use setGeometry( const QgsGeometry& ) instead.</li>
<li>setFields( const QgsFields*, bool ) has been removed, use setFields( const QgsFields&, bool ) instead.</li>
</ul>

\subsection qgis_api_break_3_0_QgsGroupWMSDataDialog QgsGroupWMSDataDialog

<ul>
Expand Down Expand Up @@ -384,6 +393,12 @@ be returned instead of a null pointer if no transformation is required.</li>
<li>setCoordinateTransform() now takes a QgsCoordinateTransform reference, not a pointer. An invalid QgsCoordinateTransform should be used instead of a null pointer if no transformation is required.</li>
</ul>

\subsection qgis_api_break_3_0_QgsRubberBand QgsRubberBand

<ul>
<li>setToGeometry() and addGeometry() now take geometry references, not pointers.</li>
</ul>

\subsection qgis_api_break_3_0_QgsPalLayerSettings QgsPalLayerSettings

<ul>
Expand Down Expand Up @@ -418,6 +433,15 @@ plugins calling this method will need to be updated.</li>
setExcludeAttributesWms()</li>
<li>excludeAttributesWFS() and setExcludeAttributesWFS() have been renamed to excludeAttributesWfs() and
setExcludeAttributesWfs()</li>
<li>changeGeometry() now accepts a geometry reference, not a pointer.</li>
<li>The geometryChanged() signal now uses a const QgsGeometry reference.</li>
</ul>

\subsection qgis_api_break_3_0_QgsVectorLayerEditBuffer QgsVectorLayerEditBuffer

<ul>
<li>changeGeometry() now accepts a geometry reference, not a pointer.</li>
<li>The geometryChanged() signal now uses a const QgsGeometry reference.</li>
</ul>

\subsection qgis_api_break_3_0_QgsVectorLayerImport QgsVectorLayerImport
Expand All @@ -429,6 +453,13 @@ pointers makes for more robust, safer code. Use an invalid (default constructed)
in code which previously passed a null pointer to QgsVectorLayerImport.</li>
</ul>

\subsection qgis_api_break_3_0_QgsVectorLayerUndoCommand QgsVectorLayerUndoCommand

<ul>
<li>QgsVectorLayerUndoCommandChangeGeometry constructor now accepts a geometry reference, not a pointer.</li>
</ul>


\subsection qgis_api_break_3_0_QgsPointLocator QgsPointLocator

<ul>
Expand Down
45 changes: 0 additions & 45 deletions python/core/qgsfeature.sip
Expand Up @@ -333,7 +333,6 @@ class QgsFeature
*
* @returns pointer to feature's geometry
* @see constGeometry
* @see geometryAndOwnership
* @see setGeometry
*/
QgsGeometry* geometry();
Expand All @@ -351,58 +350,14 @@ class QgsFeature
*/
const QgsGeometry* constGeometry() const;

/** Get the geometry object associated with this feature, and transfer ownership of the
* geometry to the caller. The caller assumes responsibility for the QgsGeometry*'s destruction.
* @returns pointer to feature's geometry
* @see geometry
* @see setGeometry
* @deprecated use constGeometry() instead
*/
QgsGeometry *geometryAndOwnership() /Factory,Deprecated/;

/** Set this feature's geometry from another QgsGeometry object. This method performs a deep copy
* of the geometry.
* @param geom new feature geometry
* @see geometry
* @see constGeometry
* @see geometryAndOwnership
* @see setGeometryAndOwnership
*/
void setGeometry( const QgsGeometry& geom );

/** Set this feature's geometry from a QgsGeometry pointer. Ownership of the geometry is transferred
* to the feature.
* @param geom new feature geometry
* @note not available in python bindings
* @see geometry
* @see constGeometry
* @see geometryAndOwnership
* @see setGeometryAndOwnership
*/
// void setGeometry( QgsGeometry* geom /Transfer/ );

/** Set this feature's geometry from WKB. This feature assumes responsibility for destroying the
* created geometry.
* @param geom geometry as WKB
* @param length size of WKB
* @see setGeometry
* @see geometry
* @see constGeometry
* @see geometryAndOwnership
* @deprecated will be removed in QGIS 3.0
*/
void setGeometryAndOwnership( unsigned char * geom /Transfer/, int length ) /Deprecated/;

/** Assign a field map with the feature to allow attribute access by attribute name.
* @param fields The attribute fields which this feature holds
* @param initAttributes If true, attributes are initialized. Clears any data previously assigned.
* C++: Defaults to false
* Python: Defaults to true
* @deprecated use setFields( const QgsFields& fields, bool initAttributes = false ) instead
* @note not available in Python bindings
*/
//void setFields( const QgsFields* fields, bool initAttributes = true );

/** Assign a field map with the feature to allow attribute access by attribute name.
* @param fields The attribute fields which this feature holds
* @param initAttributes If true, attributes are initialized. Clears any data previously assigned.
Expand Down
4 changes: 2 additions & 2 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -911,7 +911,7 @@ class QgsVectorLayer : QgsMapLayer
bool setReadOnly( bool readonly = true );

/** Change feature's geometry */
bool changeGeometry( QgsFeatureId fid, QgsGeometry* geom );
bool changeGeometry( QgsFeatureId fid, const QgsGeometry& geom );

/**
* Changes an attribute value (but does not commit it)
Expand Down Expand Up @@ -1615,7 +1615,7 @@ class QgsVectorLayer : QgsMapLayer
* @param fid The id of the changed feature
* @param geometry The new geometry
*/
void geometryChanged( QgsFeatureId fid, QgsGeometry& geometry );
void geometryChanged( QgsFeatureId fid, const QgsGeometry& geometry );

/** This signal is emitted, when attributes are deleted from the provider */
void committedAttributesDeleted( const QString& layerId, const QgsAttributeList& deletedAttributes );
Expand Down
4 changes: 2 additions & 2 deletions python/core/qgsvectorlayereditbuffer.sip
Expand Up @@ -31,7 +31,7 @@ class QgsVectorLayerEditBuffer : QObject
virtual bool deleteFeatures( const QgsFeatureIds& fid );

/** Change feature's geometry */
virtual bool changeGeometry( QgsFeatureId fid, QgsGeometry* geom );
virtual bool changeGeometry( QgsFeatureId fid, QgsGeometry geom );

/** Changed an attribute value (but does not commit it) */
virtual bool changeAttributeValue( QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue = QVariant() );
Expand Down Expand Up @@ -145,7 +145,7 @@ class QgsVectorLayerEditBuffer : QObject

void featureAdded( QgsFeatureId fid );
void featureDeleted( QgsFeatureId fid );
void geometryChanged( QgsFeatureId fid, QgsGeometry &geom );
void geometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
void attributeValueChanged( QgsFeatureId fid, int idx, const QVariant & );
void attributeAdded( int idx );
void attributeDeleted( int idx );
Expand Down
2 changes: 1 addition & 1 deletion python/core/qgsvectorlayereditpassthrough.sip
Expand Up @@ -10,7 +10,7 @@ class QgsVectorLayerEditPassthrough : QgsVectorLayerEditBuffer
bool addFeatures( QgsFeatureList& features );
bool deleteFeature( QgsFeatureId fid );
bool deleteFeatures( const QgsFeatureIds& fids );
bool changeGeometry( QgsFeatureId fid, QgsGeometry* geom );
bool changeGeometry( QgsFeatureId fid, QgsGeometry geom );
bool changeAttributeValue( QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue = QVariant() );
bool addAttribute( const QgsField &field );
bool deleteAttribute( int attr );
Expand Down
2 changes: 1 addition & 1 deletion python/core/qgsvectorlayerundocommand.sip
Expand Up @@ -87,7 +87,7 @@ class QgsVectorLayerUndoCommandChangeGeometry : QgsVectorLayerUndoCommand
* @param fid feature ID of feature to modify geometry of
* @param newGeom new geometry for feature
*/
QgsVectorLayerUndoCommandChangeGeometry( QgsVectorLayerEditBuffer* buffer /Transfer/, QgsFeatureId fid, QgsGeometry* newGeom /Transfer/ );
QgsVectorLayerUndoCommandChangeGeometry( QgsVectorLayerEditBuffer* buffer /Transfer/, QgsFeatureId fid, QgsGeometry newGeom );
~QgsVectorLayerUndoCommandChangeGeometry();

virtual void undo();
Expand Down
4 changes: 2 additions & 2 deletions python/gui/qgsrubberband.sip
Expand Up @@ -170,7 +170,7 @@ class QgsRubberBand: QgsMapCanvasItem
* @param layer the layer containing the feature, used for coord transformation to map
* crs. In case of 0 pointer, the coordinates are not going to be transformed.
*/
void setToGeometry( const QgsGeometry *geom, QgsVectorLayer* layer );
void setToGeometry( const QgsGeometry& geom, QgsVectorLayer* layer );

/**
* Sets this rubber band to a map canvas rectangle
Expand All @@ -189,7 +189,7 @@ class QgsRubberBand: QgsMapCanvasItem
* @param layer the layer containing the feature, used for coord transformation to map
* crs. In case of 0 pointer, the coordinates are not going to be transformed.
*/
void addGeometry( const QgsGeometry *geom, QgsVectorLayer* layer );
void addGeometry( const QgsGeometry& geom, QgsVectorLayer* layer );

/**
* Adds translation to original coordinates (all in map coordinates)
Expand Down
3 changes: 2 additions & 1 deletion src/analysis/interpolation/DualEdgeTriangulation.cc
Expand Up @@ -3139,7 +3139,8 @@ bool DualEdgeTriangulation::saveAsShapefile( const QString& fileName ) const
lineGeom.push_back( QgsPoint( p1->getX(), p1->getY() ) );
lineGeom.push_back( QgsPoint( p2->getX(), p2->getY() ) );
QgsGeometry* geom = QgsGeometry::fromPolyline( lineGeom );
edgeLineFeature.setGeometry( geom );
edgeLineFeature.setGeometry( *geom );
delete geom;
edgeLineFeature.initAttributes( 1 );

//attributes
Expand Down
44 changes: 27 additions & 17 deletions src/analysis/vector/qgsgeometryanalyzer.cpp
Expand Up @@ -138,7 +138,8 @@ void QgsGeometryAnalyzer::simplifyFeature( QgsFeature& f, QgsVectorFileWriter* v
tmpGeometry = featureGeometry->simplify( tolerance );

QgsFeature newFeature;
newFeature.setGeometry( tmpGeometry );
newFeature.setGeometry( *tmpGeometry );
delete tmpGeometry;
newFeature.setAttributes( f.attributes() );

//add it to vector file writer
Expand Down Expand Up @@ -254,7 +255,8 @@ void QgsGeometryAnalyzer::centroidFeature( QgsFeature& f, QgsVectorFileWriter* v
tmpGeometry = featureGeometry->centroid();

QgsFeature newFeature;
newFeature.setGeometry( tmpGeometry );
newFeature.setGeometry( *tmpGeometry );
delete tmpGeometry;
newFeature.setAttributes( f.attributes() );

//add it to vector file writer
Expand Down Expand Up @@ -331,7 +333,9 @@ bool QgsGeometryAnalyzer::extent( QgsVectorLayer* layer,
attrs[8] = QVariant( height );
attrs[9] = QVariant( width );
feat.setAttributes( attrs );
feat.setGeometry( QgsGeometry::fromRect( rect ) );
QgsGeometry* g = QgsGeometry::fromRect( rect );
feat.setGeometry( *g );
delete g;
vWriter.addFeature( feat );
return true;
}
Expand Down Expand Up @@ -493,7 +497,8 @@ bool QgsGeometryAnalyzer::convexHull( QgsVectorLayer* layer, const QString& shap
attributes[2] = values.at( 1 );
QgsFeature dissolveFeature;
dissolveFeature.setAttributes( attributes );
dissolveFeature.setGeometry( dissolveGeometry );
dissolveFeature.setGeometry( *dissolveGeometry );
delete dissolveGeometry;
vWriter.addFeature( dissolveFeature );
}
//take all features
Expand Down Expand Up @@ -540,7 +545,8 @@ bool QgsGeometryAnalyzer::convexHull( QgsVectorLayer* layer, const QString& shap
attributes[2] = QVariant( values[ 1 ] );
QgsFeature dissolveFeature;
dissolveFeature.setAttributes( attributes );
dissolveFeature.setGeometry( dissolveGeometry );
dissolveFeature.setGeometry( *dissolveGeometry );
delete dissolveGeometry;
vWriter.addFeature( dissolveFeature );
}
}
Expand Down Expand Up @@ -702,7 +708,8 @@ bool QgsGeometryAnalyzer::dissolve( QgsVectorLayer* layer, const QString& shapef
++jt;
}
}
outputFeature.setGeometry( dissolveGeometry );
outputFeature.setGeometry( *dissolveGeometry );
delete dissolveGeometry;
vWriter.addFeature( outputFeature );
}
return true;
Expand Down Expand Up @@ -831,7 +838,8 @@ bool QgsGeometryAnalyzer::buffer( QgsVectorLayer* layer, const QString& shapefil
QgsDebugMsg( "no dissolved geometry - should not happen" );
return false;
}
dissolveFeature.setGeometry( dissolveGeometry );
dissolveFeature.setGeometry( *dissolveGeometry );
delete dissolveGeometry;
vWriter.addFeature( dissolveFeature );
}
return true;
Expand Down Expand Up @@ -878,7 +886,8 @@ void QgsGeometryAnalyzer::bufferFeature( QgsFeature& f, int nProcessedFeatures,
else //dissolve
{
QgsFeature newFeature;
newFeature.setGeometry( bufferGeometry );
newFeature.setGeometry( *bufferGeometry );
delete bufferGeometry;
newFeature.setAttributes( f.attributes() );

//add it to vector file writer
Expand Down Expand Up @@ -990,7 +999,8 @@ bool QgsGeometryAnalyzer::eventLayer( QgsVectorLayer* lineLayer, QgsVectorLayer*
if ( lrsGeom )
{
++nOutputFeatures;
addEventLayerFeature( fet, lrsGeom, featureIdIt->geometry(), fileWriter, memoryProviderFeatures, offsetField, offsetScale, forceSingleGeometry );
addEventLayerFeature( fet, lrsGeom, featureIdIt->constGeometry() ? *featureIdIt->constGeometry() :
QgsGeometry(), fileWriter, memoryProviderFeatures, offsetField, offsetScale, forceSingleGeometry );
}
}
if ( nOutputFeatures < 1 )
Expand All @@ -1012,7 +1022,7 @@ bool QgsGeometryAnalyzer::eventLayer( QgsVectorLayer* lineLayer, QgsVectorLayer*
return true;
}

void QgsGeometryAnalyzer::addEventLayerFeature( QgsFeature& feature, QgsGeometry* geom, QgsGeometry* lineGeom, QgsVectorFileWriter* fileWriter, QgsFeatureList& memoryFeatures,
void QgsGeometryAnalyzer::addEventLayerFeature( QgsFeature& feature, QgsGeometry* geom, const QgsGeometry& lineGeom, QgsVectorFileWriter* fileWriter, QgsFeatureList& memoryFeatures,
int offsetField, double offsetScale, bool forceSingleType )
{
if ( !geom )
Expand Down Expand Up @@ -1045,7 +1055,7 @@ void QgsGeometryAnalyzer::addEventLayerFeature( QgsFeature& feature, QgsGeometry
}
}

feature.setGeometry( *geomIt );
feature.setGeometry( **geomIt );
if ( fileWriter )
{
fileWriter->addFeature( feature );
Expand All @@ -1062,9 +1072,9 @@ void QgsGeometryAnalyzer::addEventLayerFeature( QgsFeature& feature, QgsGeometry
}
}

bool QgsGeometryAnalyzer::createOffsetGeometry( QgsGeometry* geom, QgsGeometry* lineGeom, double offset )
bool QgsGeometryAnalyzer::createOffsetGeometry( QgsGeometry* geom, const QgsGeometry& lineGeom, double offset )
{
if ( !geom || !lineGeom )
if ( !geom || lineGeom.isEmpty() )
{
return false;
}
Expand Down Expand Up @@ -1147,16 +1157,16 @@ bool QgsGeometryAnalyzer::createOffsetGeometry( QgsGeometry* geom, QgsGeometry*
return true;
}

QgsPoint QgsGeometryAnalyzer::createPointOffset( double x, double y, double dist, QgsGeometry* lineGeom ) const
QgsPoint QgsGeometryAnalyzer::createPointOffset( double x, double y, double dist, const QgsGeometry& lineGeom ) const
{
QgsPoint p( x, y );
QgsPoint minDistPoint;
int afterVertexNr;
lineGeom->closestSegmentWithContext( p, minDistPoint, afterVertexNr );
lineGeom.closestSegmentWithContext( p, minDistPoint, afterVertexNr );

int beforeVertexNr = afterVertexNr - 1;
QgsPoint beforeVertex = lineGeom->vertexAt( beforeVertexNr );
QgsPoint afterVertex = lineGeom->vertexAt( afterVertexNr );
QgsPoint beforeVertex = lineGeom.vertexAt( beforeVertexNr );
QgsPoint afterVertex = lineGeom.vertexAt( afterVertexNr );

//get normal vector
double dx = afterVertex.x() - beforeVertex.x();
Expand Down
6 changes: 3 additions & 3 deletions src/analysis/vector/qgsgeometryanalyzer.h
Expand Up @@ -142,14 +142,14 @@ class ANALYSIS_EXPORT QgsGeometryAnalyzer
void dissolveFeature( QgsFeature& f, int nProcessedFeatures, QgsGeometry** dissolveGeometry );

//helper functions for event layer
void addEventLayerFeature( QgsFeature& feature, QgsGeometry* geom, QgsGeometry* lineGeom, QgsVectorFileWriter* fileWriter, QgsFeatureList& memoryFeatures, int offsetField = -1, double offsetScale = 1.0,
void addEventLayerFeature( QgsFeature& feature, QgsGeometry* geom, const QgsGeometry& lineGeom, QgsVectorFileWriter* fileWriter, QgsFeatureList& memoryFeatures, int offsetField = -1, double offsetScale = 1.0,
bool forceSingleType = false );
/** Create geometry offset relative to line geometry.
@param geom the geometry to modify
@param lineGeom the line geometry to which the feature is referenced
@param offset the offset value in layer unit. Negative values mean offset towards left, positive values offset to the right side*/
bool createOffsetGeometry( QgsGeometry* geom, QgsGeometry* lineGeom, double offset );
QgsPoint createPointOffset( double x, double y, double dist, QgsGeometry* lineGeom ) const;
bool createOffsetGeometry( QgsGeometry* geom, const QgsGeometry& lineGeom, double offset );
QgsPoint createPointOffset( double x, double y, double dist, const QgsGeometry& lineGeom ) const;
QgsConstWkbPtr locateBetweenWkbString( QgsConstWkbPtr ptr, QgsMultiPolyline& result, double fromMeasure, double toMeasure );
QgsConstWkbPtr locateAlongWkbString( QgsConstWkbPtr ptr, QgsMultiPoint& result, double measure );
static bool clipSegmentByRange( double x1, double y1, double m1, double x2, double y2, double m2, double range1, double range2, QgsPoint& pt1, QgsPoint& pt2, bool& secondPointClipped );
Expand Down
3 changes: 2 additions & 1 deletion src/analysis/vector/qgsoverlayanalyzer.cpp
Expand Up @@ -169,7 +169,8 @@ void QgsOverlayAnalyzer::intersectFeature( QgsFeature& f, QgsVectorFileWriter* v
{
intersectGeometry = featureGeometry->intersection( overlayFeature.constGeometry() );

outFeature.setGeometry( intersectGeometry );
outFeature.setGeometry( *intersectGeometry );
delete intersectGeometry;
QgsAttributes attributesA = f.attributes();
QgsAttributes attributesB = overlayFeature.attributes();
combineAttributeMaps( attributesA, attributesB );
Expand Down
3 changes: 2 additions & 1 deletion src/analysis/vector/qgspointsample.cpp
Expand Up @@ -127,7 +127,8 @@ void QgsPointSample::addSamplePoints( QgsFeature& inputFeature, QgsVectorFileWri
f.setAttribute( "id", mNCreatedPoints + 1 );
f.setAttribute( "station_id", points + 1 );
f.setAttribute( "stratum_id", inputFeature.id() );
f.setGeometry( ptGeom );
f.setGeometry( *ptGeom );
delete ptGeom;
writer.addFeature( f );
sIndex.insertFeature( f );
pointMapForFeature.insert( mNCreatedPoints, randPoint );
Expand Down

0 comments on commit aceddae

Please sign in to comment.