本文整理汇总了C++中QgsVectorLayer::addFeature方法的典型用法代码示例。如果您正苦于以下问题:C++ QgsVectorLayer::addFeature方法的具体用法?C++ QgsVectorLayer::addFeature怎么用?C++ QgsVectorLayer::addFeature使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QgsVectorLayer
的用法示例。
在下文中一共展示了QgsVectorLayer::addFeature方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: canvasReleaseEvent
void QgsMapToolOffsetCurve::canvasReleaseEvent( QMouseEvent * e )
{
Q_UNUSED( e );
QgsVectorLayer* vlayer = currentVectorLayer();
if ( !vlayer )
{
deleteRubberBandAndGeometry();
return;
}
if ( !mGeometryModified )
{
deleteRubberBandAndGeometry();
vlayer->destroyEditCommand();
return;
}
if ( mMultiPartGeometry )
{
mModifiedGeometry.convertToMultiType();
}
vlayer->beginEditCommand( tr( "Offset curve" ) );
bool editOk;
if ( mSourceLayerId == vlayer->id() && !mForceCopy )
{
editOk = vlayer->changeGeometry( mModifiedFeature, &mModifiedGeometry );
}
else
{
QgsFeature f;
f.setGeometry( mModifiedGeometry );
//add empty values for all fields (allows inserting attribute values via the feature form in the same session)
QgsAttributes attrs( vlayer->pendingFields().count() );
const QgsFields& fields = vlayer->pendingFields();
for ( int idx = 0; idx < fields.count(); ++idx )
{
attrs[idx] = QVariant();
}
f.setAttributes( attrs );
editOk = vlayer->addFeature( f );
}
if ( editOk )
{
vlayer->endEditCommand();
}
else
{
vlayer->destroyEditCommand();
}
deleteRubberBandAndGeometry();
deleteDistanceItem();
delete mSnapVertexMarker; mSnapVertexMarker = 0;
mForceCopy = false;
mCanvas->refresh();
}
示例2: newFeature
QgsVectorLayer *QgsAuxiliaryLayer::toSpatialLayer() const
{
QgsVectorLayer *layer = QgsMemoryProviderUtils::createMemoryLayer( QStringLiteral( "auxiliary_layer" ), fields(), mLayer->wkbType(), mLayer->crs() );
QString pkField = mJoinInfo.targetFieldName();
QgsFeature joinFeature;
QgsFeature targetFeature;
QgsFeatureIterator it = getFeatures();
layer->startEditing();
while ( it.nextFeature( joinFeature ) )
{
QString filter = QgsExpression::createFieldEqualityExpression( pkField, joinFeature.attribute( AS_JOINFIELD ) );
QgsFeatureRequest request;
request.setFilterExpression( filter );
mLayer->getFeatures( request ).nextFeature( targetFeature );
if ( targetFeature.isValid() )
{
QgsFeature newFeature( joinFeature );
newFeature.setGeometry( targetFeature.geometry() );
layer->addFeature( newFeature );
}
}
layer->commitChanges();
return layer;
}
示例3: make_layer
static QgsVectorLayer* make_layer( const QStringList& wkts )
{
QgsVectorLayer* vl = new QgsVectorLayer( "LineString", "x", "memory" );
Q_ASSERT( vl->isValid() );
vl->startEditing();
Q_FOREACH ( const QString& wkt, wkts )
{
QgsFeature f( make_feature( wkt ) );
vl->addFeature( f, false );
}
示例4: exportPath
void RgShortestPathWidget::exportPath()
{
RgExportDlg dlg( this );
if ( !dlg.exec() )
return;
QgsVectorLayer *vl = dlg.mapLayer();
if ( vl == NULL )
return;
QgsPoint p1, p2;
QgsGraph *path = getPath( p1, p2 );
if ( path == NULL )
return;
QgsCoordinateTransform ct( mPlugin->iface()->mapCanvas()->mapRenderer()->destinationCrs(),
vl->crs() );
int startVertexIdx = path->findVertex( p1 );
int stopVertexIdx = path->findVertex( p2 );
QgsPolyline p;
while ( startVertexIdx != stopVertexIdx )
{
QgsGraphArcIdList l = path->vertex( stopVertexIdx ).inArc();
if ( l.empty() )
break;
const QgsGraphArc& e = path->arc( l.front() );
p.push_front( ct.transform( path->vertex( e.inVertex() ).point() ) );
stopVertexIdx = e.outVertex();
}
p.push_front( ct.transform( p1 ) );
vl->startEditing();
QgsFeature f;
f.setGeometry( QgsGeometry::fromPolyline( p ) );
vl->addFeature( f );
vl->updateExtents();
mPlugin->iface()->mapCanvas()->update();
delete path;
}
示例5: cadCanvasReleaseEvent
//.........这里部分代码省略.........
closePolygon();
vlayer->beginEditCommand( tr( "Ring added and filled" ) );
QList< QgsPoint > pointList = points();
QgsFeatureId modifiedFid;
int addRingReturnCode = vlayer->addRing( pointList, &modifiedFid );
if ( addRingReturnCode != 0 )
{
QString errorMessage;
//todo: open message box to communicate errors
if ( addRingReturnCode == 1 )
{
errorMessage = tr( "a problem with geometry type occured" );
}
else if ( addRingReturnCode == 2 )
{
errorMessage = tr( "the inserted Ring is not closed" );
}
else if ( addRingReturnCode == 3 )
{
errorMessage = tr( "the inserted Ring is not a valid geometry" );
}
else if ( addRingReturnCode == 4 )
{
errorMessage = tr( "the inserted Ring crosses existing rings" );
}
else if ( addRingReturnCode == 5 )
{
errorMessage = tr( "the inserted Ring is not contained in a feature" );
}
else
{
errorMessage = tr( "an unknown error occured" );
}
emit messageEmitted( tr( "could not add ring since %1." ).arg( errorMessage ), QgsMessageBar::CRITICAL );
vlayer->destroyEditCommand();
}
else
{
// find parent feature and get it attributes
double xMin, xMax, yMin, yMax;
QgsRectangle bBox;
xMin = std::numeric_limits<double>::max();
xMax = -std::numeric_limits<double>::max();
yMin = std::numeric_limits<double>::max();
yMax = -std::numeric_limits<double>::max();
Q_FOREACH ( const QgsPoint& point, pointList )
{
xMin = qMin( xMin, point.x() );
xMax = qMax( xMax, point.x() );
yMin = qMin( yMin, point.y() );
yMax = qMax( yMax, point.y() );
}
bBox.setXMinimum( xMin );
bBox.setYMinimum( yMin );
bBox.setXMaximum( xMax );
bBox.setYMaximum( yMax );
QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest().setFilterFid( modifiedFid ) );
QgsFeature f;
bool res = false;
if ( fit.nextFeature( f ) )
{
//create QgsFeature with wkb representation
QgsFeature* ft = new QgsFeature( vlayer->fields(), 0 );
ft->setGeometry( QgsGeometry::fromPolygon( QgsPolygon() << pointList.toVector() ) );
ft->setAttributes( f.attributes() );
if ( QApplication::keyboardModifiers() == Qt::ControlModifier )
{
res = vlayer->addFeature( *ft );
}
else
{
QgsAttributeDialog *dialog = new QgsAttributeDialog( vlayer, ft, false, NULL, true );
dialog->setIsAddDialog( true );
res = dialog->exec(); // will also add the feature
}
if ( res )
{
vlayer->endEditCommand();
}
else
{
delete ft;
vlayer->destroyEditCommand();
}
res = false;
}
}
stopCapturing();
}
示例6: canvasReleaseEvent
//.........这里部分代码省略.........
errorMessage = tr( "A problem with geometry type occured" );
}
else if ( addRingReturnCode == 2 )
{
errorMessage = tr( "The inserted Ring is not closed" );
}
else if ( addRingReturnCode == 3 )
{
errorMessage = tr( "The inserted Ring is not a valid geometry" );
}
else if ( addRingReturnCode == 4 )
{
errorMessage = tr( "The inserted Ring crosses existing rings" );
}
else if ( addRingReturnCode == 5 )
{
errorMessage = tr( "The inserted Ring is not contained in a feature" );
}
else
{
errorMessage = tr( "An unknown error occured" );
}
QMessageBox::critical( 0, tr( "Error, could not add ring" ), errorMessage );
vlayer->destroyEditCommand();
}
else
{
// find parent feature and get it attributes
double xMin, xMax, yMin, yMax;
QgsRectangle bBox;
xMin = std::numeric_limits<double>::max();
xMax = -std::numeric_limits<double>::max();
yMin = std::numeric_limits<double>::max();
yMax = -std::numeric_limits<double>::max();
for ( QList<QgsPoint>::const_iterator it = points().constBegin(); it != points().constEnd(); ++it )
{
if ( it->x() < xMin )
{
xMin = it->x();
}
if ( it->x() > xMax )
{
xMax = it->x();
}
if ( it->y() < yMin )
{
yMin = it->y();
}
if ( it->y() > yMax )
{
yMax = it->y();
}
}
bBox.setXMinimum( xMin );
bBox.setYMinimum( yMin );
bBox.setXMaximum( xMax );
bBox.setYMaximum( yMax );
QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest().setFilterRect( bBox ).setFlags( QgsFeatureRequest::ExactIntersect ) );
QgsFeature f;
bool res = false;
while ( fit.nextFeature( f ) )
{
//create QgsFeature with wkb representation
QgsFeature* ft = new QgsFeature( vlayer->pendingFields(), 0 );
QgsGeometry *g;
g = QgsGeometry::fromPolygon( QgsPolygon() << points().toVector() );
ft->setGeometry( g );
ft->setAttributes( f.attributes() );
if ( QgsApplication::keyboardModifiers() == Qt::ControlModifier )
{
res = vlayer->addFeature( *ft );
}
else
{
QgsAttributeDialog *dialog = new QgsAttributeDialog( vlayer, ft, false, NULL, true );
dialog->setIsAddDialog( true );
res = dialog->exec(); // will also add the feature
}
if ( res )
{
vlayer->endEditCommand();
}
else
{
delete ft;
vlayer->destroyEditCommand();
}
res = false;
}
}
stopCapturing();
}
}
示例7: copyVectorLayer
//.........这里部分代码省略.........
// add new layer
QgsVectorLayer* newLayer = new QgsVectorLayer( QString( "dbname='%1' table='%2'(Geometry) sql=" )
.arg( offlineDbPath ).arg( tableName ), tableName + " (offline)", "spatialite" );
if ( newLayer->isValid() )
{
// mark as offline layer
newLayer->setCustomProperty( CUSTOM_PROPERTY_IS_OFFLINE_EDITABLE, true );
// store original layer source
newLayer->setCustomProperty( CUSTOM_PROPERTY_REMOTE_SOURCE, layer->source() );
newLayer->setCustomProperty( CUSTOM_PROPERTY_REMOTE_PROVIDER, layer->providerType() );
// copy style
bool hasLabels = layer->hasLabelsEnabled();
if ( !hasLabels )
{
// NOTE: copy symbology before adding the layer so it is displayed correctly
copySymbology( layer, newLayer );
}
// register this layer with the central layers registry
QgsMapLayerRegistry::instance()->addMapLayers(
QList<QgsMapLayer *>() << newLayer );
if ( hasLabels )
{
// NOTE: copy symbology of layers with labels enabled after adding to project, as it will crash otherwise (WORKAROUND)
copySymbology( layer, newLayer );
}
// TODO: layer order
// copy features
newLayer->startEditing();
QgsFeature f;
// NOTE: force feature recount for PostGIS layer, else only visible features are counted, before iterating over all features (WORKAROUND)
layer->setSubsetString( "" );
QgsFeatureIterator fit = layer->getFeatures();
emit progressModeSet( QgsOfflineEditing::CopyFeatures, layer->featureCount() );
int featureCount = 1;
QList<QgsFeatureId> remoteFeatureIds;
while ( fit.nextFeature( f ) )
{
remoteFeatureIds << f.id();
// NOTE: Spatialite provider ignores position of geometry column
// fill gap in QgsAttributeMap if geometry column is not last (WORKAROUND)
int column = 0;
QgsAttributes attrs = f.attributes();
QgsAttributes newAttrs( attrs.count() );
for ( int it = 0; it < attrs.count(); ++it )
{
newAttrs[column++] = attrs[it];
}
f.setAttributes( newAttrs );
newLayer->addFeature( f, false );
emit progressUpdated( featureCount++ );
}
if ( newLayer->commitChanges() )
{
emit progressModeSet( QgsOfflineEditing::ProcessFeatures, layer->featureCount() );
featureCount = 1;
// update feature id lookup
int layerId = getOrCreateLayerId( db, newLayer->id() );
QList<QgsFeatureId> offlineFeatureIds;
QgsFeatureIterator fit = newLayer->getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ).setSubsetOfAttributes( QgsAttributeList() ) );
while ( fit.nextFeature( f ) )
{
offlineFeatureIds << f.id();
}
// NOTE: insert fids in this loop, as the db is locked during newLayer->nextFeature()
sqlExec( db, "BEGIN" );
int remoteCount = remoteFeatureIds.size();
for ( int i = 0; i < remoteCount; i++ )
{
addFidLookup( db, layerId, offlineFeatureIds.at( i ), remoteFeatureIds.at( remoteCount - ( i + 1 ) ) );
emit progressUpdated( featureCount++ );
}
sqlExec( db, "COMMIT" );
}
else
{
showWarning( newLayer->commitErrors().join( "\n" ) );
}
// remove remote layer
QgsMapLayerRegistry::instance()->removeMapLayers(
QStringList() << layer->id() );
}
}
}
示例8: applyOffset
void QgsMapToolOffsetCurve::applyOffset()
{
QgsVectorLayer* layer = currentVectorLayer();
if ( !layer )
{
deleteRubberBandAndGeometry();
notifyNotVectorLayer();
return;
}
// no modification
if ( !mGeometryModified )
{
deleteRubberBandAndGeometry();
layer->destroyEditCommand();
deleteDistanceWidget();
return;
}
if ( mMultiPartGeometry )
{
mModifiedGeometry.convertToMultiType();
}
layer->beginEditCommand( tr( "Offset curve" ) );
bool editOk;
if ( mSourceLayerId == layer->id() && !mForceCopy )
{
editOk = layer->changeGeometry( mModifiedFeature, &mModifiedGeometry );
}
else
{
QgsFeature f;
f.setGeometry( mModifiedGeometry );
//add empty values for all fields (allows inserting attribute values via the feature form in the same session)
QgsAttributes attrs( layer->fields().count() );
const QgsFields& fields = layer->fields();
for ( int idx = 0; idx < fields.count(); ++idx )
{
attrs[idx] = QVariant();
}
f.setAttributes( attrs );
editOk = layer->addFeature( f );
}
if ( editOk )
{
layer->endEditCommand();
}
else
{
layer->destroyEditCommand();
}
deleteRubberBandAndGeometry();
deleteDistanceWidget();
delete mSnapVertexMarker;
mSnapVertexMarker = nullptr;
mForceCopy = false;
mCanvas->refresh();
}