本文整理汇总了C++中QgsVectorLayer::getFeatures方法的典型用法代码示例。如果您正苦于以下问题:C++ QgsVectorLayer::getFeatures方法的具体用法?C++ QgsVectorLayer::getFeatures怎么用?C++ QgsVectorLayer::getFeatures使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QgsVectorLayer
的用法示例。
在下文中一共展示了QgsVectorLayer::getFeatures方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: cacheJoinLayer
void QgsVectorLayerJoinBuffer::cacheJoinLayer( QgsVectorJoinInfo& joinInfo )
{
//memory cache not required or already done
if ( !joinInfo.memoryCache || joinInfo.cachedAttributes.size() > 0 )
{
return;
}
QgsVectorLayer* cacheLayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( joinInfo.joinLayerId ) );
if ( cacheLayer )
{
int joinFieldIndex;
if ( joinInfo.joinFieldName.isEmpty() )
joinFieldIndex = joinInfo.joinFieldIndex; //for compatibility with 1.x
else
joinFieldIndex = cacheLayer->pendingFields().indexFromName( joinInfo.joinFieldName );
if ( joinFieldIndex < 0 || joinFieldIndex >= cacheLayer->pendingFields().count() )
return;
joinInfo.cachedAttributes.clear();
QgsFeatureIterator fit = cacheLayer->getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ) );
QgsFeature f;
while ( fit.nextFeature( f ) )
{
const QgsAttributes& attrs = f.attributes();
joinInfo.cachedAttributes.insert( attrs[joinFieldIndex].toString(), attrs );
}
}
}
示例2: setFeatureForMapPosition
void QgsMapCanvasAnnotationItem::setFeatureForMapPosition()
{
if ( !mAnnotation || !mAnnotation->hasFixedMapPosition() )
return;
QgsVectorLayer *vectorLayer = qobject_cast< QgsVectorLayer * >( mAnnotation->mapLayer() );
if ( !vectorLayer )
return;
double halfIdentifyWidth = QgsMapTool::searchRadiusMU( mMapCanvas );
QgsPointXY mapPosition = mAnnotation->mapPosition();
try
{
QgsCoordinateTransform ct( mAnnotation->mapPositionCrs(), mMapCanvas->mapSettings().destinationCrs(), QgsProject::instance() );
if ( ct.isValid() )
mapPosition = ct.transform( mapPosition );
}
catch ( QgsCsException & )
{
}
QgsRectangle searchRect( mapPosition.x() - halfIdentifyWidth, mapPosition.y() - halfIdentifyWidth,
mapPosition.x() + halfIdentifyWidth, mapPosition.y() + halfIdentifyWidth );
searchRect = mMapCanvas->mapSettings().mapToLayerCoordinates( vectorLayer, searchRect );
QgsFeatureIterator fit = vectorLayer->getFeatures( QgsFeatureRequest().setFilterRect( searchRect ).setFlags( QgsFeatureRequest::ExactIntersect ).setLimit( 1 ) );
QgsFeature currentFeature;
( void )fit.nextFeature( currentFeature );
mAnnotation->setAssociatedFeature( currentFeature );
}
示例3: fetchFeature
QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPoint &mapPosition, QgsMapCanvas *mpMapCanvas )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
if ( !vlayer )
return "";
// Get the setting for the search radius from user preferences, if it exists
QSettings settings;
double identifyValue = settings.value( "/Map/identifyRadius", QGis::DEFAULT_IDENTIFY_RADIUS ).toDouble();
// create the search rectangle
double searchRadius = mpMapCanvas->extent().width() * ( identifyValue / 100.0 );
QgsRectangle r;
r.setXMinimum( mapPosition.x() - searchRadius );
r.setYMinimum( mapPosition.y() - searchRadius );
r.setXMaximum( mapPosition.x() + searchRadius );
r.setYMaximum( mapPosition.y() + searchRadius );
r = mpMapCanvas->mapSettings().mapToLayerCoordinates( layer, r );
QgsFeature feature;
if ( !vlayer->getFeatures( QgsFeatureRequest().setFilterRect( r ).setFlags( QgsFeatureRequest::ExactIntersect ) ).nextFeature( feature ) )
return "";
int idx = vlayer->fieldNameIndex( vlayer->displayField() );
if ( idx < 0 )
return QgsExpression::replaceExpressionText( vlayer->displayField(), &feature, vlayer );
else
return feature.attribute( idx ).toString();
}
示例4: setModelData
void QgsAttributeTableDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
{
QgsVectorLayer *vl = layer( model );
if ( !vl )
return;
int fieldIdx = model->data( index, QgsAttributeTableModel::FieldIndexRole ).toInt();
QgsFeatureId fid = model->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong();
QVariant oldValue = model->data( index, Qt::EditRole );
QVariant newValue;
QgsEditorWidgetWrapper *eww = QgsEditorWidgetWrapper::fromWidget( editor );
if ( !eww )
return;
newValue = eww->value();
if ( ( oldValue != newValue && newValue.isValid() ) || oldValue.isNull() != newValue.isNull() )
{
// This fixes https://issues.qgis.org/issues/16492
QgsFeatureRequest request( fid );
request.setFlags( QgsFeatureRequest::NoGeometry );
request.setSubsetOfAttributes( QgsAttributeList() );
QgsFeature feature;
vl->getFeatures( request ).nextFeature( feature );
if ( feature.isValid() )
{
vl->beginEditCommand( tr( "Attribute changed" ) );
vl->changeAttributeValue( fid, fieldIdx, newValue, oldValue );
vl->endEditCommand();
}
}
}
示例5: applyRotation
void QgsMapToolRotateFeature::applyRotation( double rotation )
{
mRotation = rotation;
mRotationActive = false;
QgsVectorLayer *vlayer = currentVectorLayer();
if ( !vlayer )
{
deleteRubberband();
notifyNotVectorLayer();
return;
}
//calculations for affine transformation
double angle = -1 * mRotation * ( M_PI / 180 );
QgsPointXY anchorPoint = toLayerCoordinates( vlayer, mStartPointMapCoords );
double a = std::cos( angle );
double b = -1 * std::sin( angle );
double c = anchorPoint.x() - std::cos( angle ) * anchorPoint.x() + std::sin( angle ) * anchorPoint.y();
double d = std::sin( angle );
double ee = std::cos( angle );
double f = anchorPoint.y() - std::sin( angle ) * anchorPoint.x() - std::cos( angle ) * anchorPoint.y();
vlayer->beginEditCommand( tr( "Features Rotated" ) );
int start;
if ( vlayer->geometryType() == 2 )
{
start = 1;
}
else
{
start = 0;
}
int i = 0;
Q_FOREACH ( QgsFeatureId id, mRotatedFeatures )
{
QgsFeature feat;
vlayer->getFeatures( QgsFeatureRequest().setFilterFid( id ) ).nextFeature( feat );
QgsGeometry geom = feat.geometry();
i = start;
QgsPointXY vertex = geom.vertexAt( i );
while ( vertex != QgsPointXY( 0, 0 ) )
{
double newX = a * vertex.x() + b * vertex.y() + c;
double newY = d * vertex.x() + ee * vertex.y() + f;
vlayer->moveVertex( newX, newY, id, i );
i = i + 1;
vertex = geom.vertexAt( i );
}
}
示例6: createTopologyRubberBands
void QgsMapToolNodeTool::createTopologyRubberBands()
{
QgsVectorLayer* vlayer = mSelectedFeature->vlayer();
Q_FOREACH ( const QgsVertexEntry* vertexEntry, mSelectedFeature->vertexMap() )
{
if ( !vertexEntry->isSelected() )
{
continue;
}
// Snap vertex
QMultiMap<double, QgsSnappingResult> snapResults;
vlayer->snapWithContext( vertexEntry->pointV1(), ZERO_TOLERANCE, snapResults, QgsSnapper::SnapToVertex );
Q_FOREACH ( const QgsSnappingResult& snapResult, snapResults )
{
// Get geometry of snapped feature
QgsFeatureId snapFeatureId = snapResult.snappedAtGeometry;
QgsFeature feature;
if ( !vlayer->getFeatures( QgsFeatureRequest( snapFeatureId ).setSubsetOfAttributes( QgsAttributeList() ) ).nextFeature( feature ) )
{
continue;
}
// Get VertexId of snapped vertex
QgsVertexId vid;
if ( !feature.geometry().vertexIdFromVertexNr( snapResult.snappedVertexNr, vid ) )
{
continue;
}
// Add rubberband if not already added
if ( !mMoveRubberBands.contains( snapFeatureId ) )
{
QgsGeometryRubberBand* rb = new QgsGeometryRubberBand( mCanvas, feature.geometry().type() );
QSettings settings;
QColor color(
settings.value( QStringLiteral( "/qgis/digitizing/line_color_red" ), 255 ).toInt(),
settings.value( QStringLiteral( "/qgis/digitizing/line_color_green" ), 0 ).toInt(),
settings.value( QStringLiteral( "/qgis/digitizing/line_color_blue" ), 0 ).toInt() );
double myAlpha = settings.value( QStringLiteral( "/qgis/digitizing/line_color_alpha" ), 30 ).toInt() / 255.0 ;
color.setAlphaF( myAlpha );
rb->setOutlineColor( color );
rb->setBrushStyle( Qt::NoBrush );
rb->setOutlineWidth( settings.value( QStringLiteral( "/qgis/digitizing/line_width" ), 1 ).toInt() );
QgsAbstractGeometry* rbGeom = feature.geometry().geometry()->clone();
if ( mCanvas->mapSettings().layerTransform( vlayer ).isValid() )
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 ) ) ) );
}
}
示例7: currentFeature
bool QgsMapToolLabel::currentFeature( QgsFeature& f, bool fetchGeom )
{
QgsVectorLayer* vlayer = currentLayer();
if ( !vlayer )
{
return false;
}
return vlayer->getFeatures( QgsFeatureRequest()
.setFilterFid( mCurrentLabelPos.featureId )
.setFlags( fetchGeom ? QgsFeatureRequest::NoFlags : QgsFeatureRequest::NoGeometry )
).nextFeature( f );
}
示例8: canvasPressEvent
void QgsMapToolOffsetCurve::canvasPressEvent( QMouseEvent* e )
{
deleteRubberBandAndGeometry();
mGeometryModified = false;
mForceCopy = false;
if ( !mCanvas )
{
return;
}
//get selected features or snap to nearest feature if no selection
QgsVectorLayer* layer = currentVectorLayer();
if ( !layer )
{
notifyNotVectorLayer();
return;
}
QgsMapRenderer* renderer = mCanvas->mapRenderer();
QgsSnapper snapper( renderer );
configureSnapper( snapper );
QList<QgsSnappingResult> snapResults;
snapper.snapPoint( e->pos(), snapResults );
if ( snapResults.size() > 0 )
{
QgsFeature fet;
const QgsSnappingResult& snapResult = snapResults.at( 0 );
if ( snapResult.layer )
{
mSourceLayerId = snapResult.layer->id();
QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( mSourceLayerId ) );
if ( vl && vl->getFeatures( QgsFeatureRequest().setFilterFid( snapResult.snappedAtGeometry ) ).nextFeature( fet ) )
{
mForceCopy = ( e->modifiers() & Qt::ControlModifier ); //no geometry modification if ctrl is pressed
mOriginalGeometry = createOriginGeometry( vl, snapResult, fet );
mRubberBand = createRubberBand();
if ( mRubberBand )
{
mRubberBand->setToGeometry( mOriginalGeometry, layer );
}
mModifiedFeature = fet.id();
createDistanceItem();
}
}
}
}
示例9: createTopologyRubberBands
void QgsMapToolNodeTool::createTopologyRubberBands()
{
QgsVectorLayer* vlayer = mSelectedFeature->vlayer();
foreach ( const QgsVertexEntry* vertexEntry, mSelectedFeature->vertexMap() )
{
if ( !vertexEntry->isSelected() )
{
continue;
}
// Snap vertex
QMultiMap<double, QgsSnappingResult> snapResults;
vlayer->snapWithContext( vertexEntry->pointV1(), ZERO_TOLERANCE, snapResults, QgsSnapper::SnapToVertex );
foreach ( const QgsSnappingResult& snapResult, snapResults.values() )
{
// Get geometry of snapped feature
QgsFeatureId snapFeatureId = snapResult.snappedAtGeometry;
QgsFeature feature;
if ( !vlayer->getFeatures( QgsFeatureRequest( snapFeatureId ).setSubsetOfAttributes( QgsAttributeList() ) ).nextFeature( feature ) )
{
continue;
}
// Get VertexId of snapped vertex
QgsVertexId vid;
if ( !feature.geometry()->vertexIdFromVertexNr( snapResult.snappedVertexNr, vid ) )
{
continue;
}
// Add rubberband if not already added
if ( !mMoveRubberBands.contains( snapFeatureId ) )
{
QgsGeometryRubberBand* rb = new QgsGeometryRubberBand( mCanvas, feature.geometry()->type() );
rb->setOutlineColor( Qt::blue );
rb->setBrushStyle( Qt::NoBrush );
rb->setOutlineWidth( 2 );
QgsAbstractGeometryV2* rbGeom = feature.geometry()->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 ) ) ) );
}
}
}
示例10: currentLabelText
QString QgsMapToolLabel::currentLabelText( int trunc )
{
bool settingsOk;
QgsPalLayerSettings& labelSettings = currentLabelSettings( &settingsOk );
if ( !settingsOk )
{
return "";
}
if ( labelSettings.isExpression )
{
QString labelText = mCurrentLabelPos.labelText;
if ( trunc > 0 && labelText.length() > trunc )
{
labelText.truncate( trunc );
labelText += "...";
}
return labelText;
}
else
{
QgsVectorLayer* vlayer = currentLayer();
if ( !vlayer )
{
return "";
}
QString labelField = vlayer->customProperty( "labeling/fieldName" ).toString();
if ( !labelField.isEmpty() )
{
int labelFieldId = vlayer->fieldNameIndex( labelField );
QgsFeature f;
if ( vlayer->getFeatures( QgsFeatureRequest().setFilterFid( mCurrentLabelPos.featureId ).setFlags( QgsFeatureRequest::NoGeometry ) ).nextFeature( f ) )
{
QString labelText = f.attribute( labelFieldId ).toString();
if ( trunc > 0 && labelText.length() > trunc )
{
labelText.truncate( trunc );
labelText += "...";
}
return labelText;
}
}
}
return "";
}
示例11: currentLabelText
QString QgsMapToolLabel::currentLabelText( int trunc )
{
if ( !mCurrentLabel.valid )
{
return QString();
}
QgsPalLayerSettings &labelSettings = mCurrentLabel.settings;
if ( labelSettings.isExpression )
{
QString labelText = mCurrentLabel.pos.labelText;
if ( trunc > 0 && labelText.length() > trunc )
{
labelText.truncate( trunc );
labelText += QChar( 0x2026 );
}
return labelText;
}
else
{
QgsVectorLayer *vlayer = mCurrentLabel.layer;
if ( !vlayer )
{
return QString();
}
QString labelField = labelSettings.fieldName;
if ( !labelField.isEmpty() )
{
int labelFieldId = vlayer->fields().lookupField( labelField );
QgsFeature f;
if ( vlayer->getFeatures( QgsFeatureRequest().setFilterFid( mCurrentLabel.pos.featureId ).setFlags( QgsFeatureRequest::NoGeometry ) ).nextFeature( f ) )
{
QString labelText = f.attribute( labelFieldId ).toString();
if ( trunc > 0 && labelText.length() > trunc )
{
labelText.truncate( trunc );
labelText += QChar( 0x2026 );
}
return labelText;
}
}
}
return QString();
}
示例12: context
Qt3DCore::QEntity *QgsRuleBased3DRenderer::createEntity( const Qgs3DMapSettings &map ) const
{
QgsVectorLayer *vl = layer();
if ( !vl )
return nullptr;
Qgs3DRenderContext context( map );
QgsExpressionContext exprContext( Qgs3DUtils::globalProjectLayerExpressionContext( vl ) );
exprContext.setFields( vl->fields() );
context.setExpressionContext( exprContext );
RuleToHandlerMap handlers;
mRootRule->createHandlers( vl, handlers );
QSet<QString> attributeNames;
mRootRule->prepare( context, attributeNames, handlers );
QgsFeatureRequest req;
req.setDestinationCrs( map.crs(), map.transformContext() );
req.setSubsetOfAttributes( attributeNames, vl->fields() );
QgsFeature f;
QgsFeatureIterator fi = vl->getFeatures( req );
while ( fi.nextFeature( f ) )
{
context.expressionContext().setFeature( f );
mRootRule->registerFeature( f, context, handlers );
}
Qt3DCore::QEntity *entity = new Qt3DCore::QEntity;
for ( QgsFeature3DHandler *handler : handlers.values() )
handler->finalize( entity, context );
qDeleteAll( handlers.values() );
return entity;
}
示例13: currentLabelDataDefinedPosition
bool QgsMapToolLabel::currentLabelDataDefinedPosition( double &x, bool &xSuccess, double &y, bool &ySuccess, int &xCol, int &yCol ) const
{
QgsVectorLayer *vlayer = mCurrentLabel.layer;
QgsFeatureId featureId = mCurrentLabel.pos.featureId;
xSuccess = false;
ySuccess = false;
if ( !vlayer )
{
return false;
}
if ( mCurrentLabel.pos.isDiagram )
{
if ( !diagramMoveable( vlayer, xCol, yCol ) )
{
return false;
}
}
else if ( !labelMoveable( vlayer, mCurrentLabel.settings, xCol, yCol ) )
{
return false;
}
QgsFeature f;
if ( !vlayer->getFeatures( QgsFeatureRequest().setFilterFid( featureId ).setFlags( QgsFeatureRequest::NoGeometry ) ).nextFeature( f ) )
{
return false;
}
QgsAttributes attributes = f.attributes();
if ( !attributes.at( xCol ).isNull() )
x = attributes.at( xCol ).toDouble( &xSuccess );
if ( !attributes.at( yCol ).isNull() )
y = attributes.at( yCol ).toDouble( &ySuccess );
return true;
}
示例14: select
/**
* Selection routine called by the mouse release event
* @param thePoint = QgsPoint representing the x, y coordinates of the mouse release event
*/
void eVisEventIdTool::select( QgsPoint thePoint )
{
if ( 0 == mCanvas )
return;
QgsVectorLayer* myLayer = ( QgsVectorLayer* )mCanvas->currentLayer( );
// create the search rectangle. this was modeled after the QgsMapIdentifyTool in core QGIS application
double searchWidth = mCanvas->extent( ).width( ) * (( double )QGis::DEFAULT_IDENTIFY_RADIUS / 100.0 );
QgsRectangle myRectangle;
myRectangle.setXMinimum( thePoint.x( ) - searchWidth );
myRectangle.setXMaximum( thePoint.x( ) + searchWidth );
myRectangle.setYMinimum( thePoint.y( ) - searchWidth );
myRectangle.setYMaximum( thePoint.y( ) + searchWidth );
//Transform rectange to map coordinates
myRectangle = toLayerCoordinates( myLayer, myRectangle );
//Rather than add to the current selection, clear all selected features
myLayer->removeSelection( false );
//select features
QgsFeatureIterator fit = myLayer->getFeatures( QgsFeatureRequest().setFilterRect( myRectangle ).setFlags( QgsFeatureRequest::ExactIntersect ).setSubsetOfAttributes( QgsAttributeList() ) );
QgsFeature f;
QgsFeatureIds newSelectedFeatures;
while ( fit.nextFeature( f ) )
{
newSelectedFeatures.insert( f.id() );
}
myLayer->setSelectedFeatures( newSelectedFeatures );
//Launch a new event browser to view selected features
mBrowser = new eVisGenericEventBrowserGui( mCanvas, mCanvas, NULL );
mBrowser->setAttribute( Qt::WA_DeleteOnClose );
}
示例15: fetchFeature
QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPoint &mapPosition, QgsMapCanvas *mapCanvas )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
if ( !vlayer )
return QString();
double searchRadius = QgsMapTool::searchRadiusMU( mapCanvas );
QgsRectangle r;
r.setXMinimum( mapPosition.x() - searchRadius );
r.setYMinimum( mapPosition.y() - searchRadius );
r.setXMaximum( mapPosition.x() + searchRadius );
r.setYMaximum( mapPosition.y() + searchRadius );
r = mapCanvas->mapSettings().mapToLayerCoordinates( layer, r );
QgsFeature feature;
if ( !vlayer->getFeatures( QgsFeatureRequest().setFilterRect( r ).setFlags( QgsFeatureRequest::ExactIntersect ) ).nextFeature( feature ) )
return QString();
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( vlayer ) );
if ( mapCanvas )
context.appendScope( QgsExpressionContextUtils::mapSettingsScope( mapCanvas->mapSettings() ) );
context.setFeature( feature );
QString mapTip = vlayer->mapTipTemplate();
if ( !mapTip.isEmpty() )
{
return QgsExpression::replaceExpressionText( mapTip, &context );
}
else
{
QgsExpression exp( vlayer->displayExpression() );
return exp.evaluate( &context ).toString();
}
}