本文整理汇总了C++中QgsVectorDataProvider::addAttributes方法的典型用法代码示例。如果您正苦于以下问题:C++ QgsVectorDataProvider::addAttributes方法的具体用法?C++ QgsVectorDataProvider::addAttributes怎么用?C++ QgsVectorDataProvider::addAttributes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QgsVectorDataProvider
的用法示例。
在下文中一共展示了QgsVectorDataProvider::addAttributes方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: mapLayer
QgsVectorLayer* RgExportDlg::mapLayer() const
{
QgsVectorLayer* myLayer = NULL;
QString layerId = mcbLayers->itemData( mcbLayers->currentIndex() ).toString();
if ( layerId == "-1" )
{
// create a temporary layer
myLayer = new QgsVectorLayer( QString( "LineString?crs=epsg:4326&memoryid=%1" ).arg( QUuid::createUuid().toString() ), "shortest path", "memory" );
QgsVectorDataProvider *prov = myLayer->dataProvider();
if ( prov == NULL )
return NULL;
QList<QgsField> attrList;
attrList.append( QgsField( "length", QVariant::Double, "", 20, 8 ) );
attrList.append( QgsField( "time", QVariant::Double, "", 20, 8 ) );
prov->addAttributes( attrList );
myLayer->updateFields();
QList<QgsMapLayer *> myList;
myList << myLayer;
QgsMapLayerRegistry::instance()->addMapLayers( myList );
}
else
{
// return selected layer
myLayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
}
return myLayer;
} // QgsVectorLayer* RgExportDlg::vectorLayer() const
示例2: mapLayer
QgsVectorLayer* RgExportDlg::mapLayer() const
{
QgsVectorLayer* myLayer = NULL;
QString layerId = mcbLayers->itemData( mcbLayers->currentIndex() ).toString();
if ( layerId == QString( "-1" ) )
{
// create a temporary layer
myLayer = new QgsVectorLayer( "LineString", "shortest path", "memory" );
QgsVectorDataProvider *prov = myLayer->dataProvider();
if ( prov == NULL )
return NULL;
QList<QgsField> attrList;
attrList.append( QgsField( "one", QVariant::Int ) );
prov->addAttributes( attrList );
QList<QgsMapLayer *> myList;
myList << myLayer;
QgsMapLayerRegistry::instance()->addMapLayers( myList );
}
else
{
// retrun selected layer
myLayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
}
return myLayer;
} // QgsVectorLayer* RgExportDlg::vectorLayer() const
示例3: calculateStatistics
//.........这里部分代码省略.........
{
rangeFieldName = getUniqueFieldName( mAttributePrefix + "range", newFieldList );
QgsField rangeField( rangeFieldName, QVariant::Double, QStringLiteral( "double precision" ) );
newFieldList.push_back( rangeField );
}
QString minorityFieldName;
if ( mStatistics & QgsZonalStatistics::Minority )
{
minorityFieldName = getUniqueFieldName( mAttributePrefix + "minority", newFieldList );
QgsField minorityField( minorityFieldName, QVariant::Double, QStringLiteral( "double precision" ) );
newFieldList.push_back( minorityField );
}
QString majorityFieldName;
if ( mStatistics & QgsZonalStatistics::Majority )
{
majorityFieldName = getUniqueFieldName( mAttributePrefix + "majority", newFieldList );
QgsField majField( majorityFieldName, QVariant::Double, QStringLiteral( "double precision" ) );
newFieldList.push_back( majField );
}
QString varietyFieldName;
if ( mStatistics & QgsZonalStatistics::Variety )
{
varietyFieldName = getUniqueFieldName( mAttributePrefix + "variety", newFieldList );
QgsField varietyField( varietyFieldName, QVariant::Int, QStringLiteral( "int" ) );
newFieldList.push_back( varietyField );
}
QString varianceFieldName;
if ( mStatistics & QgsZonalStatistics::Variance )
{
varianceFieldName = getUniqueFieldName( mAttributePrefix + "variance", newFieldList );
QgsField varianceField( varianceFieldName, QVariant::Double, QStringLiteral( "double precision" ) );
newFieldList.push_back( varianceField );
}
vectorProvider->addAttributes( newFieldList );
//index of the new fields
int countIndex = mStatistics & QgsZonalStatistics::Count ? vectorProvider->fieldNameIndex( countFieldName ) : -1;
int sumIndex = mStatistics & QgsZonalStatistics::Sum ? vectorProvider->fieldNameIndex( sumFieldName ) : -1;
int meanIndex = mStatistics & QgsZonalStatistics::Mean ? vectorProvider->fieldNameIndex( meanFieldName ) : -1;
int medianIndex = mStatistics & QgsZonalStatistics::Median ? vectorProvider->fieldNameIndex( medianFieldName ) : -1;
int stdevIndex = mStatistics & QgsZonalStatistics::StDev ? vectorProvider->fieldNameIndex( stdevFieldName ) : -1;
int minIndex = mStatistics & QgsZonalStatistics::Min ? vectorProvider->fieldNameIndex( minFieldName ) : -1;
int maxIndex = mStatistics & QgsZonalStatistics::Max ? vectorProvider->fieldNameIndex( maxFieldName ) : -1;
int rangeIndex = mStatistics & QgsZonalStatistics::Range ? vectorProvider->fieldNameIndex( rangeFieldName ) : -1;
int minorityIndex = mStatistics & QgsZonalStatistics::Minority ? vectorProvider->fieldNameIndex( minorityFieldName ) : -1;
int majorityIndex = mStatistics & QgsZonalStatistics::Majority ? vectorProvider->fieldNameIndex( majorityFieldName ) : -1;
int varietyIndex = mStatistics & QgsZonalStatistics::Variety ? vectorProvider->fieldNameIndex( varietyFieldName ) : -1;
int varianceIndex = mStatistics & QgsZonalStatistics::Variance ? vectorProvider->fieldNameIndex( varianceFieldName ) : -1;
if ( ( mStatistics & QgsZonalStatistics::Count && countIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::Sum && sumIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::Mean && meanIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::Median && medianIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::StDev && stdevIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::Min && minIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::Max && maxIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::Range && rangeIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::Minority && minorityIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::Majority && majorityIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::Variety && varietyIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::Variance && varianceIndex == -1 )
)
{
//failed to create a required field
return 8;
}
示例4: calculateStatistics
int QgsZonalStatistics::calculateStatistics( QProgressDialog* p )
{
if ( !mPolygonLayer || mPolygonLayer->geometryType() != QGis::Polygon )
{
return 1;
}
QgsVectorDataProvider* vectorProvider = mPolygonLayer->dataProvider();
if ( !vectorProvider )
{
return 2;
}
//open the raster layer and the raster band
GDALAllRegister();
GDALDatasetH inputDataset = GDALOpen( mRasterFilePath.toLocal8Bit().data(), GA_ReadOnly );
if ( inputDataset == NULL )
{
return 3;
}
if ( GDALGetRasterCount( inputDataset ) < ( mRasterBand - 1 ) )
{
GDALClose( inputDataset );
return 4;
}
GDALRasterBandH rasterBand = GDALGetRasterBand( inputDataset, mRasterBand );
if ( rasterBand == NULL )
{
GDALClose( inputDataset );
return 5;
}
mInputNodataValue = GDALGetRasterNoDataValue( rasterBand, NULL );
//get geometry info about raster layer
int nCellsX = GDALGetRasterXSize( inputDataset );
int nCellsY = GDALGetRasterYSize( inputDataset );
double geoTransform[6];
if ( GDALGetGeoTransform( inputDataset, geoTransform ) != CE_None )
{
GDALClose( inputDataset );
return 6;
}
double cellsizeX = geoTransform[1];
if ( cellsizeX < 0 )
{
cellsizeX = -cellsizeX;
}
double cellsizeY = geoTransform[5];
if ( cellsizeY < 0 )
{
cellsizeY = -cellsizeY;
}
QgsRectangle rasterBBox( geoTransform[0], geoTransform[3] - ( nCellsY * cellsizeY ), geoTransform[0] + ( nCellsX * cellsizeX ), geoTransform[3] );
//add the new count, sum, mean fields to the provider
QList<QgsField> newFieldList;
QgsField countField( mAttributePrefix + "count", QVariant::Double );
QgsField sumField( mAttributePrefix + "sum", QVariant::Double );
QgsField meanField( mAttributePrefix + "mean", QVariant::Double );
newFieldList.push_back( countField );
newFieldList.push_back( sumField );
newFieldList.push_back( meanField );
if ( !vectorProvider->addAttributes( newFieldList ) )
{
return 7;
}
//index of the new fields
int countIndex = vectorProvider->fieldNameIndex( mAttributePrefix + "count" );
int sumIndex = vectorProvider->fieldNameIndex( mAttributePrefix + "sum" );
int meanIndex = vectorProvider->fieldNameIndex( mAttributePrefix + "mean" );
if ( countIndex == -1 || sumIndex == -1 || meanIndex == -1 )
{
return 8;
}
//progress dialog
long featureCount = vectorProvider->featureCount();
if ( p )
{
p->setMaximum( featureCount );
}
//iterate over each polygon
vectorProvider->select( QgsAttributeList(), QgsRectangle(), true, false );
vectorProvider->rewind();
QgsFeature f;
double count = 0;
double sum = 0;
double mean = 0;
int featureCounter = 0;
while ( vectorProvider->nextFeature( f ) )
{
qWarning( "%d", featureCounter );
if ( p )
//.........这里部分代码省略.........
示例5: calculateStatistics
//.........这里部分代码省略.........
{
maxFieldName = getUniqueFieldName( mAttributePrefix + "max" );
QgsField maxField( maxFieldName, QVariant::Double, "double precision" );
newFieldList.push_back( maxField );
}
QString rangeFieldName;
if ( mStatistics & QgsZonalStatistics::Range )
{
rangeFieldName = getUniqueFieldName( mAttributePrefix + "range" );
QgsField rangeField( rangeFieldName, QVariant::Double, "double precision" );
newFieldList.push_back( rangeField );
}
QString minorityFieldName;
if ( mStatistics & QgsZonalStatistics::Minority )
{
minorityFieldName = getUniqueFieldName( mAttributePrefix + "minority" );
QgsField minorityField( minorityFieldName, QVariant::Double, "double precision" );
newFieldList.push_back( minorityField );
}
QString majorityFieldName;
if ( mStatistics & QgsZonalStatistics::Majority )
{
majorityFieldName = getUniqueFieldName( mAttributePrefix + "majority" );
QgsField majField( majorityFieldName, QVariant::Double, "double precision" );
newFieldList.push_back( majField );
}
QString varietyFieldName;
if ( mStatistics & QgsZonalStatistics::Variety )
{
varietyFieldName = getUniqueFieldName( mAttributePrefix + "variety" );
QgsField varietyField( varietyFieldName, QVariant::Int, "int" );
newFieldList.push_back( varietyField );
}
vectorProvider->addAttributes( newFieldList );
//index of the new fields
int countIndex = mStatistics & QgsZonalStatistics::Count ? vectorProvider->fieldNameIndex( countFieldName ) : -1;
int sumIndex = mStatistics & QgsZonalStatistics::Sum ? vectorProvider->fieldNameIndex( sumFieldName ) : -1;
int meanIndex = mStatistics & QgsZonalStatistics::Mean ? vectorProvider->fieldNameIndex( meanFieldName ) : -1;
int medianIndex = mStatistics & QgsZonalStatistics::Median ? vectorProvider->fieldNameIndex( medianFieldName ) : -1;
int stdevIndex = mStatistics & QgsZonalStatistics::StDev ? vectorProvider->fieldNameIndex( stdevFieldName ) : -1;
int minIndex = mStatistics & QgsZonalStatistics::Min ? vectorProvider->fieldNameIndex( minFieldName ) : -1;
int maxIndex = mStatistics & QgsZonalStatistics::Max ? vectorProvider->fieldNameIndex( maxFieldName ) : -1;
int rangeIndex = mStatistics & QgsZonalStatistics::Range ? vectorProvider->fieldNameIndex( rangeFieldName ) : -1;
int minorityIndex = mStatistics & QgsZonalStatistics::Minority ? vectorProvider->fieldNameIndex( minorityFieldName ) : -1;
int majorityIndex = mStatistics & QgsZonalStatistics::Majority ? vectorProvider->fieldNameIndex( majorityFieldName ) : -1;
int varietyIndex = mStatistics & QgsZonalStatistics::Variety ? vectorProvider->fieldNameIndex( varietyFieldName ) : -1;
if (( mStatistics & QgsZonalStatistics::Count && countIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::Sum && sumIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::Mean && meanIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::Median && medianIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::StDev && stdevIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::Min && minIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::Max && maxIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::Range && rangeIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::Minority && minorityIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::Majority && majorityIndex == -1 )
|| ( mStatistics & QgsZonalStatistics::Variety && varietyIndex == -1 )
)
{
//failed to create a required field
return 8;
}
//progress dialog
示例6: commitChanges
bool QgsVectorLayerEditBuffer::commitChanges( QStringList& commitErrors )
{
QgsVectorDataProvider* provider = L->dataProvider();
commitErrors.clear();
int cap = provider->capabilities();
bool success = true;
QgsFields oldFields = L->pendingFields();
//
// delete attributes
//
bool attributesChanged = false;
if ( !mDeletedAttributeIds.isEmpty() )
{
if (( cap & QgsVectorDataProvider::DeleteAttributes ) && provider->deleteAttributes( mDeletedAttributeIds.toSet() ) )
{
commitErrors << tr( "SUCCESS: %n attribute(s) deleted.", "deleted attributes count", mDeletedAttributeIds.size() );
emit committedAttributesDeleted( L->id(), mDeletedAttributeIds );
mDeletedAttributeIds.clear();
attributesChanged = true;
}
else
{
commitErrors << tr( "ERROR: %n attribute(s) not deleted.", "not deleted attributes count", mDeletedAttributeIds.size() );
success = false;
}
}
//
// add attributes
//
if ( !mAddedAttributes.isEmpty() )
{
if (( cap & QgsVectorDataProvider::AddAttributes ) && provider->addAttributes( mAddedAttributes ) )
{
commitErrors << tr( "SUCCESS: %n attribute(s) added.", "added attributes count", mAddedAttributes.size() );
emit committedAttributesAdded( L->id(), mAddedAttributes );
mAddedAttributes.clear();
attributesChanged = true;
}
else
{
commitErrors << tr( "ERROR: %n new attribute(s) not added", "not added attributes count", mAddedAttributes.size() );
success = false;
}
}
//
// check that addition/removal went as expected
//
bool attributeChangesOk = true;
if ( attributesChanged )
{
L->updateFields();
QgsFields newFields = L->pendingFields();
if ( oldFields.count() != newFields.count() )
{
commitErrors << tr( "ERROR: the count of fields is incorrect after addition/removal of fields!" );
attributeChangesOk = false; // don't try attribute updates - they'll fail.
}
for ( int i = 0; i < oldFields.count(); ++i )
{
const QgsField& oldField = oldFields[i];
const QgsField& newField = newFields[i];
if ( attributeChangesOk && oldField != newField )
{
commitErrors << tr( "ERROR: field with index %1 is not the same!" ).arg( i );
attributeChangesOk = false; // don't try attribute updates - they'll fail.
}
}
}
if ( attributeChangesOk )
{
//
// change attributes
//
if ( !mChangedAttributeValues.isEmpty() )
{
if (( cap & QgsVectorDataProvider::ChangeAttributeValues ) && provider->changeAttributeValues( mChangedAttributeValues ) )
{
commitErrors << tr( "SUCCESS: %n attribute value(s) changed.", "changed attribute values count", mChangedAttributeValues.size() );
emit committedAttributeValuesChanges( L->id(), mChangedAttributeValues );
mChangedAttributeValues.clear();
}
else
{
commitErrors << tr( "ERROR: %n attribute value change(s) not applied.", "not changed attribute values count", mChangedAttributeValues.size() );
success = false;
}
//.........这里部分代码省略.........