本文整理汇总了C++中QgsVectorLayer::commitChanges方法的典型用法代码示例。如果您正苦于以下问题:C++ QgsVectorLayer::commitChanges方法的具体用法?C++ QgsVectorLayer::commitChanges怎么用?C++ QgsVectorLayer::commitChanges使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QgsVectorLayer
的用法示例。
在下文中一共展示了QgsVectorLayer::commitChanges方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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;
}
示例2: createTransactionDocument
//.........这里部分代码省略.........
QgsField field = fields.at( fieldMapIt.value() );
if ( field.type() == 2 )
layer->changeAttributeValue( *fidIt, fieldMapIt.value(), it.value().toInt( &conversionSuccess ) );
else if ( field.type() == 6 )
layer->changeAttributeValue( *fidIt, fieldMapIt.value(), it.value().toDouble( &conversionSuccess ) );
else
layer->changeAttributeValue( *fidIt, fieldMapIt.value(), it.value() );
}
if ( !geometryElem.isNull() )
{
QgsGeometry g = QgsOgcUtils::geometryFromGML( geometryElem );
if ( !layer->changeGeometry( *fidIt, g ) )
{
throw QgsRequestNotWellFormedException( QStringLiteral( "Error in change geometry" ) );
}
}
#ifdef HAVE_SERVER_PYTHON_PLUGINS
fit = layer->getFeatures( QgsFeatureRequest( *fidIt ) );
while ( fit.nextFeature( feature ) )
{
if ( !accessControl->allowToEdit( layer, feature ) )
{
layer->rollBack();
throw QgsSecurityAccessException( QStringLiteral( "Feature modify permission denied" ) );
}
}
#endif
}
}
}
// Commit the changes of the update elements
if ( !layer->commitChanges() )
{
addTransactionResult( resp, respElem, QStringLiteral( "PARTIAL" ), QStringLiteral( "Update" ), layer->commitErrors().join( QStringLiteral( "\n " ) ) );
return resp;
}
// Start the delete transaction
layer->startEditing();
if (( cap & QgsVectorDataProvider::DeleteFeatures ) )
{
// Loop through the delete elements
QDomNodeList delNodeList = typeNameElem.elementsByTagNameNS( WFS_NAMESPACE, QStringLiteral( "Delete" ) );
for ( int j = 0; j < delNodeList.count(); ++j )
{
if ( !configParser->wfstDeleteLayers().contains( layer->id() ) )
{
//no wfs permissions to do updates
QString errorMsg = "No permissions to do WFS deletes on layer '" + layer->name() + "'";
QgsMessageLog::logMessage( errorMsg, QStringLiteral( "Server" ), QgsMessageLog::CRITICAL );
addTransactionResult( resp, respElem, QStringLiteral( "FAILED" ), QStringLiteral( "Delete" ), errorMsg );
return resp;
}
actionElem = delNodeList.at( j ).toElement();
QDomElement filterElem = actionElem.firstChild().toElement();
// Get Feature Ids for the Filter element
QgsFeatureIds fids = getFeatureIdsFromFilter( filterElem, layer );
#ifdef HAVE_SERVER_PYTHON_PLUGINS
QgsFeatureIds::const_iterator fidIt = fids.constBegin();
for ( ; fidIt != fids.constEnd(); ++fidIt )
{
QgsFeatureIterator fit = layer->getFeatures( QgsFeatureRequest( *fidIt ) );
QgsFeature feature;
示例3: 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() );
}
}
}
示例4: synchronize
void QgsOfflineEditing::synchronize()
{
// open logging db
sqlite3* db = openLoggingDb();
if ( db == NULL )
{
return;
}
emit progressStarted();
// restore and sync remote layers
QList<QgsMapLayer*> offlineLayers;
QMap<QString, QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
for ( QMap<QString, QgsMapLayer*>::iterator layer_it = mapLayers.begin() ; layer_it != mapLayers.end(); ++layer_it )
{
QgsMapLayer* layer = layer_it.value();
if ( layer->customProperty( CUSTOM_PROPERTY_IS_OFFLINE_EDITABLE, false ).toBool() )
{
offlineLayers << layer;
}
}
for ( int l = 0; l < offlineLayers.count(); l++ )
{
QgsMapLayer* layer = offlineLayers[l];
emit layerProgressUpdated( l + 1, offlineLayers.count() );
QString remoteSource = layer->customProperty( CUSTOM_PROPERTY_REMOTE_SOURCE, "" ).toString();
QString remoteProvider = layer->customProperty( CUSTOM_PROPERTY_REMOTE_PROVIDER, "" ).toString();
QString remoteName = layer->name();
remoteName.remove( QRegExp( " \\(offline\\)$" ) );
QgsVectorLayer* remoteLayer = new QgsVectorLayer( remoteSource, remoteName, remoteProvider );
if ( remoteLayer->isValid() )
{
// TODO: only add remote layer if there are log entries?
QgsVectorLayer* offlineLayer = qobject_cast<QgsVectorLayer*>( layer );
// copy style
copySymbology( offlineLayer, remoteLayer );
// register this layer with the central layers registry
QgsMapLayerRegistry::instance()->addMapLayers(
QList<QgsMapLayer *>() << remoteLayer, true );
// apply layer edit log
QString qgisLayerId = layer->id();
QString sql = QString( "SELECT \"id\" FROM 'log_layer_ids' WHERE \"qgis_id\" = '%1'" ).arg( qgisLayerId );
int layerId = sqlQueryInt( db, sql, -1 );
if ( layerId != -1 )
{
remoteLayer->startEditing();
// TODO: only get commitNos of this layer?
int commitNo = getCommitNo( db );
for ( int i = 0; i < commitNo; i++ )
{
// apply commits chronologically
applyAttributesAdded( remoteLayer, db, layerId, i );
applyAttributeValueChanges( offlineLayer, remoteLayer, db, layerId, i );
applyGeometryChanges( remoteLayer, db, layerId, i );
}
applyFeaturesAdded( offlineLayer, remoteLayer, db, layerId );
applyFeaturesRemoved( remoteLayer, db, layerId );
if ( remoteLayer->commitChanges() )
{
// update fid lookup
updateFidLookup( remoteLayer, db, layerId );
// clear edit log for this layer
sql = QString( "DELETE FROM 'log_added_attrs' WHERE \"layer_id\" = %1" ).arg( layerId );
sqlExec( db, sql );
sql = QString( "DELETE FROM 'log_added_features' WHERE \"layer_id\" = %1" ).arg( layerId );
sqlExec( db, sql );
sql = QString( "DELETE FROM 'log_removed_features' WHERE \"layer_id\" = %1" ).arg( layerId );
sqlExec( db, sql );
sql = QString( "DELETE FROM 'log_feature_updates' WHERE \"layer_id\" = %1" ).arg( layerId );
sqlExec( db, sql );
sql = QString( "DELETE FROM 'log_geometry_updates' WHERE \"layer_id\" = %1" ).arg( layerId );
sqlExec( db, sql );
// reset commitNo
QString sql = QString( "UPDATE 'log_indices' SET 'last_index' = 0 WHERE \"name\" = 'commit_no'" );
sqlExec( db, sql );
}
else
{
showWarning( remoteLayer->commitErrors().join( "\n" ) );
}
}
// remove offline layer
QgsMapLayerRegistry::instance()->removeMapLayers(
( QStringList() << qgisLayerId ) );
//.........这里部分代码省略.........