Skip to content
Permalink
Browse files

Use constGeometry to avoid some QgsFeature detachments

  • Loading branch information
nyalldawson committed Jan 13, 2016
1 parent 31d981c commit 884c3ba8dd024519e561989bce9c91617eb62b77
@@ -78,14 +78,14 @@ void QgsMapToolNodeTool::createTopologyRubberBands()
}
// Get VertexId of snapped vertex
QgsVertexId vid;
if ( !feature.geometry()->vertexIdFromVertexNr( snapResult.snappedVertexNr, vid ) )
if ( !feature.constGeometry()->vertexIdFromVertexNr( snapResult.snappedVertexNr, vid ) )
{
continue;
}
// Add rubberband if not already added
if ( !mMoveRubberBands.contains( snapFeatureId ) )
{
QgsGeometryRubberBand* rb = new QgsGeometryRubberBand( mCanvas, feature.geometry()->type() );
QgsGeometryRubberBand* rb = new QgsGeometryRubberBand( mCanvas, feature.constGeometry()->type() );
QSettings settings;
QColor color(
settings.value( "/qgis/digitizing/line_color_red", 255 ).toInt(),
@@ -96,14 +96,14 @@ void QgsMapToolNodeTool::createTopologyRubberBands()
rb->setOutlineColor( color );
rb->setBrushStyle( Qt::NoBrush );
rb->setOutlineWidth( settings.value( "/qgis/digitizing/line_width", 1 ).toInt() );
QgsAbstractGeometryV2* rbGeom = feature.geometry()->geometry()->clone();
QgsAbstractGeometryV2* rbGeom = feature.constGeometry()->geometry()->clone();
if ( mCanvas->mapSettings().layerTransform( vlayer ) )
rbGeom->transform( *mCanvas->mapSettings().layerTransform( vlayer ) );
rb->setGeometry( rbGeom );
mMoveRubberBands.insert( snapFeatureId, rb );
}
// Add to list of vertices to be moved
mMoveVertices[snapFeatureId].append( qMakePair( vid, toMapCoordinates( vlayer, feature.geometry()->geometry()->vertexAt( vid ) ) ) );
mMoveVertices[snapFeatureId].append( qMakePair( vid, toMapCoordinates( vlayer, feature.constGeometry()->geometry()->vertexAt( vid ) ) ) );
}
}
}
@@ -226,7 +226,7 @@ void QgsMapToolNodeTool::canvasPressEvent( QgsMapMouseEvent* e )
if ( snapResults.size() < 1 )
{
QgsFeature feature = getFeatureAtPoint( e );
if ( !feature.geometry() )
if ( !feature.constGeometry() )
{
emit messageEmitted( tr( "could not snap to a segment on the current layer." ) );
return;
@@ -366,7 +366,7 @@ void QgsMapToolNodeTool::canvasPressEvent( QgsMapMouseEvent* e )
mSelectedFeature->deselectAllVertexes();

QgsFeature feature = getFeatureAtPoint( e );
if ( !feature.geometry() )
if ( !feature.constGeometry() )
return;

mAnother = feature.id();
@@ -269,10 +269,10 @@ QgsAbstractGeometryV2* QgsGeometryEditUtils::avoidIntersections( const QgsAbstra
if ( ignoreIds.contains( f.id() ) )
continue;

if ( !f.geometry() )
if ( !f.constGeometry() )
continue;

nearGeometries << f.geometry()->geometry()->clone();
nearGeometries << f.constGeometry()->geometry()->clone();
}
}
}
@@ -403,7 +403,7 @@ void resetGraph( QgsTracerGraph& g )
}


void extractLinework( QgsGeometry* g, QgsMultiPolyline& mpl )
void extractLinework( const QgsGeometry* g, QgsMultiPolyline& mpl )
{
switch ( QgsWKBTypes::flatType( g->geometry()->wkbType() ) )
{
@@ -470,7 +470,7 @@ bool QgsTracer::initGraph()
QgsFeatureIterator fi = vl->getFeatures( request );
while ( fi.nextFeature( f ) )
{
if ( !f.geometry() )
if ( !f.constGeometry() )
continue;

if ( !ct.isShortCircuited() )
@@ -485,7 +485,7 @@ bool QgsTracer::initGraph()
}
}

extractLinework( f.geometry(), mpl );
extractLinework( f.constGeometry(), mpl );

++featuresCounted;
if ( mMaxFeatureCount != 0 && featuresCounted >= mMaxFeatureCount )
@@ -1796,7 +1796,7 @@ OGRFeatureH QgsVectorFileWriter::createFeature( QgsFeature& feature )

if ( mWkbType != QgsWKBTypes::NoGeometry )
{
if ( feature.geometry() && feature.geometry()->geometry() )
if ( feature.constGeometry() && !feature.constGeometry()->isEmpty() )
{
// build geometry from WKB
QgsGeometry* geom = feature.geometry();
@@ -361,7 +361,7 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
{
try
{
if ( fet.geometry() )
if ( fet.constGeometry() )
{
fet.geometry()->transform( *ct );
}
@@ -183,7 +183,7 @@ void QgsVectorLayerUndoCommandChangeGeometry::undo()
mBuffer->mChangedGeometries.remove( mFid );

QgsFeature f;
if ( layer()->getFeatures( QgsFeatureRequest().setFilterFid( mFid ).setSubsetOfAttributes( QgsAttributeList() ) ).nextFeature( f ) && f.geometry() )
if ( layer()->getFeatures( QgsFeatureRequest().setFilterFid( mFid ).setSubsetOfAttributes( QgsAttributeList() ) ).nextFeature( f ) && f.constGeometry() )
{
cache()->cacheGeometry( mFid, *f.constGeometry() );
emit mBuffer->geometryChanged( mFid, *f.geometry() );
@@ -1086,9 +1086,9 @@ void QgsMapCanvas::zoomToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds&
int featureCount = 0;
while ( it.nextFeature( fet ) )
{
QgsGeometry* geom = fet.geometry();
const QgsGeometry* geom = fet.constGeometry();
QString errorMessage;
if ( !geom || !geom->geometry() )
if ( !geom || geom->isEmpty() )
{
errorMessage = tr( "Feature does not have a geometry" );
}
@@ -245,7 +245,7 @@ int main( int argc, char **argv )
break;
}

QgsGeometry* geometry = feature.geometry();
const QgsGeometry* geometry = feature.constGeometry();
if ( geometry )
{
// geometry type may be probably different from provider type (e.g. multi x single)
@@ -423,16 +423,16 @@ int main( int argc, char **argv )
{
break;
}
if ( !feature.geometry() )
if ( !feature.constGeometry() )
{
continue;
}

QList<QgsFeatureId> idList = spatialIndex.intersects( feature.geometry()->boundingBox() );
QList<QgsFeatureId> idList = spatialIndex.intersects( feature.constGeometry()->boundingBox() );
Q_FOREACH ( QgsFeatureId id, idList )
{
QgsFeature& centroid = centroids[id];
if ( feature.geometry()->contains( centroid.geometry() ) )
if ( feature.constGeometry()->contains( centroid.constGeometry() ) )
{
QgsAttributes attr = centroid.attributes();
attr.append(( int )feature.id() + fidToCatPlus );
@@ -450,9 +450,9 @@ int main( int argc, char **argv )

int centroidsCount = centroids.size();
count = 0;
Q_FOREACH ( QgsFeature centroid, centroids.values() )
Q_FOREACH ( const QgsFeature& centroid, centroids.values() )
{
QgsPoint point = centroid.geometry()->asPoint();
QgsPoint point = centroid.constGeometry()->asPoint();

if ( centroid.attributes().size() > 0 )
{
@@ -1245,9 +1245,9 @@ void QgsGrassProvider::onFeatureAdded( QgsFeatureId fid )
return;
}
QgsFeature feature = mEditBuffer->addedFeatures().value( fid );
if ( feature.geometry() )
if ( feature.constGeometry() )
{
geometry = feature.geometry()->geometry();
geometry = feature.constGeometry()->geometry();
}
else
{
@@ -1941,7 +1941,7 @@ void QgsGrassProvider::setAddedFeaturesSymbol()
Q_FOREACH ( QgsFeatureId fid, features.keys() )
{
QgsFeature feature = features[fid];
if ( !feature.geometry() || !feature.geometry()->geometry() )
if ( !feature.constGeometry() || !feature.constGeometry()->geometry() )
{
continue;
}
@@ -1864,7 +1864,7 @@ QString QgsWFSServer::createFeatureGeoJSON( QgsFeature* feat, int prec, QgsCoord
fStr += "\"" + mTypeName + "." + QString::number( feat->id() ) + "\"";
fStr += ",\n";

QgsGeometry* geom = feat->geometry();
const QgsGeometry* geom = feat->constGeometry();
if ( geom && mWithGeom && mGeometryName != "NONE" )
{
QgsRectangle box = geom->boundingBox();
@@ -1948,7 +1948,7 @@ QDomElement QgsWFSServer::createFeatureGML2( QgsFeature* feat, QDomDocument& doc
if ( mWithGeom && mGeometryName != "NONE" )
{
//add geometry column (as gml)
QgsGeometry* geom = feat->geometry();
const QgsGeometry* geom = feat->constGeometry();

QDomElement geomElem = doc.createElement( "qgs:geometry" );
QDomElement gmlElem;
@@ -2021,7 +2021,7 @@ QDomElement QgsWFSServer::createFeatureGML3( QgsFeature* feat, QDomDocument& doc
if ( mWithGeom && mGeometryName != "NONE" )
{
//add geometry column (as gml)
QgsGeometry* geom = feat->geometry();
const QgsGeometry* geom = feat->constGeometry();

QDomElement geomElem = doc.createElement( "qgs:geometry" );
QDomElement gmlElem;
@@ -2232,7 +2232,7 @@ int QgsWMSServer::featureInfoFromVectorLayer( QgsVectorLayer* layer,
QgsRectangle box;
if ( hasGeometry )
{
box = mapRender->layerExtentToOutputExtent( layer, feature.geometry()->boundingBox() );
box = mapRender->layerExtentToOutputExtent( layer, feature.constGeometry()->boundingBox() );
if ( featureBBox ) //extend feature info bounding box if requested
{
if ( !featureBBoxInitialized && featureBBox->isEmpty() )
@@ -3192,7 +3192,7 @@ QDomElement QgsWMSServer::createFeatureGML(
// always add bounding box info if feature contains geometry
if ( geom && geom->type() != QGis::UnknownGeometry && geom->type() != QGis::NoGeometry )
{
QgsRectangle box = feat->geometry()->boundingBox();
QgsRectangle box = feat->constGeometry()->boundingBox();
if ( transform )
{
try
@@ -1479,7 +1479,7 @@ QList<QgsFeature> TestQgsGrassProvider::getFeatures( QgsVectorLayer *layer )

bool TestQgsGrassProvider::equal( QgsFeature feature, QgsFeature expectedFeature )
{
if ( !feature.geometry()->equals( expectedFeature.geometry() ) )
if ( !feature.constGeometry()->equals( expectedFeature.constGeometry() ) )
{
return false;
}

0 comments on commit 884c3ba

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