本文整理汇总了C++中QgsVectorDataProvider::changeAttributeValues方法的典型用法代码示例。如果您正苦于以下问题:C++ QgsVectorDataProvider::changeAttributeValues方法的具体用法?C++ QgsVectorDataProvider::changeAttributeValues怎么用?C++ QgsVectorDataProvider::changeAttributeValues使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QgsVectorDataProvider
的用法示例。
在下文中一共展示了QgsVectorDataProvider::changeAttributeValues方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: calculateStatistics
//.........这里部分代码省略.........
statisticsFromMiddlePointTest( featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY,
rasterBBox, featureStats );
if ( featureStats.count <= 1 )
{
//the cell resolution is probably larger than the polygon area. We switch to precise pixel - polygon intersection in this case
statisticsFromPreciseIntersection( featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY,
rasterBBox, featureStats );
}
//write the statistics value to the vector data provider
QgsAttributeMap changeAttributeMap;
if ( mStatistics & QgsZonalStatistics::Count )
changeAttributeMap.insert( countIndex, QVariant( featureStats.count ) );
if ( mStatistics & QgsZonalStatistics::Sum )
changeAttributeMap.insert( sumIndex, QVariant( featureStats.sum ) );
if ( featureStats.count > 0 )
{
double mean = featureStats.sum / featureStats.count;
if ( mStatistics & QgsZonalStatistics::Mean )
changeAttributeMap.insert( meanIndex, QVariant( mean ) );
if ( mStatistics & QgsZonalStatistics::Median )
{
std::sort( featureStats.values.begin(), featureStats.values.end() );
int size = featureStats.values.count();
bool even = ( size % 2 ) < 1;
double medianValue;
if ( even )
{
medianValue = ( featureStats.values.at( size / 2 - 1 ) + featureStats.values.at( size / 2 ) ) / 2;
}
else //odd
{
medianValue = featureStats.values.at( ( size + 1 ) / 2 - 1 );
}
changeAttributeMap.insert( medianIndex, QVariant( medianValue ) );
}
if ( mStatistics & QgsZonalStatistics::StDev || mStatistics & QgsZonalStatistics::Variance )
{
double sumSquared = 0;
for ( int i = 0; i < featureStats.values.count(); ++i )
{
double diff = featureStats.values.at( i ) - mean;
sumSquared += diff * diff;
}
double variance = sumSquared / featureStats.values.count();
if ( mStatistics & QgsZonalStatistics::StDev )
{
double stdev = std::pow( variance, 0.5 );
changeAttributeMap.insert( stdevIndex, QVariant( stdev ) );
}
if ( mStatistics & QgsZonalStatistics::Variance )
changeAttributeMap.insert( varianceIndex, QVariant( variance ) );
}
if ( mStatistics & QgsZonalStatistics::Min )
changeAttributeMap.insert( minIndex, QVariant( featureStats.min ) );
if ( mStatistics & QgsZonalStatistics::Max )
changeAttributeMap.insert( maxIndex, QVariant( featureStats.max ) );
if ( mStatistics & QgsZonalStatistics::Range )
changeAttributeMap.insert( rangeIndex, QVariant( featureStats.max - featureStats.min ) );
if ( mStatistics & QgsZonalStatistics::Minority || mStatistics & QgsZonalStatistics::Majority )
{
QList<int> vals = featureStats.valueCount.values();
std::sort( vals.begin(), vals.end() );
if ( mStatistics & QgsZonalStatistics::Minority )
{
float minorityKey = featureStats.valueCount.key( vals.first() );
changeAttributeMap.insert( minorityIndex, QVariant( minorityKey ) );
}
if ( mStatistics & QgsZonalStatistics::Majority )
{
float majKey = featureStats.valueCount.key( vals.last() );
changeAttributeMap.insert( majorityIndex, QVariant( majKey ) );
}
}
if ( mStatistics & QgsZonalStatistics::Variety )
changeAttributeMap.insert( varietyIndex, QVariant( featureStats.valueCount.count() ) );
}
changeMap.insert( f.id(), changeAttributeMap );
++featureCounter;
}
vectorProvider->changeAttributeValues( changeMap );
if ( feedback )
{
feedback->setProgress( 100 );
}
mPolygonLayer->updateFields();
if ( feedback && feedback->isCanceled() )
{
return 9;
}
return 0;
}
示例2: calculateStatistics
//.........这里部分代码省略.........
}
if (( offsetY + nCellsY ) > nCellsYGDAL )
{
nCellsY = nCellsYGDAL - offsetY;
}
statisticsFromMiddlePointTest( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY,
rasterBBox, featureStats );
if ( featureStats.count <= 1 )
{
//the cell resolution is probably larger than the polygon area. We switch to precise pixel - polygon intersection in this case
statisticsFromPreciseIntersection( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY,
rasterBBox, featureStats );
}
//write the statistics value to the vector data provider
QgsAttributeMap changeAttributeMap;
if ( mStatistics & QgsZonalStatistics::Count )
changeAttributeMap.insert( countIndex, QVariant( featureStats.count ) );
if ( mStatistics & QgsZonalStatistics::Sum )
changeAttributeMap.insert( sumIndex, QVariant( featureStats.sum ) );
if ( featureStats.count > 0 )
{
double mean = featureStats.sum / featureStats.count;
if ( mStatistics & QgsZonalStatistics::Mean )
changeAttributeMap.insert( meanIndex, QVariant( mean ) );
if ( mStatistics & QgsZonalStatistics::Median )
{
qSort( featureStats.values.begin(), featureStats.values.end() );
int size = featureStats.values.count();
bool even = ( size % 2 ) < 1;
double medianValue;
if ( even )
{
medianValue = ( featureStats.values.at( size / 2 - 1 ) + featureStats.values.at( size / 2 ) ) / 2;
}
else //odd
{
medianValue = featureStats.values.at(( size + 1 ) / 2 - 1 );
}
changeAttributeMap.insert( medianIndex, QVariant( medianValue ) );
}
if ( mStatistics & QgsZonalStatistics::StDev )
{
double sumSquared = 0;
for ( int i = 0; i < featureStats.values.count(); ++i )
{
double diff = featureStats.values.at( i ) - mean;
sumSquared += diff * diff;
}
double stdev = qPow( sumSquared / featureStats.values.count(), 0.5 );
changeAttributeMap.insert( stdevIndex, QVariant( stdev ) );
}
if ( mStatistics & QgsZonalStatistics::Min )
changeAttributeMap.insert( minIndex, QVariant( featureStats.min ) );
if ( mStatistics & QgsZonalStatistics::Max )
changeAttributeMap.insert( maxIndex, QVariant( featureStats.max ) );
if ( mStatistics & QgsZonalStatistics::Range )
changeAttributeMap.insert( rangeIndex, QVariant( featureStats.max - featureStats.min ) );
if ( mStatistics & QgsZonalStatistics::Minority || mStatistics & QgsZonalStatistics::Majority )
{
QList<int> vals = featureStats.valueCount.values();
qSort( vals.begin(), vals.end() );
if ( mStatistics & QgsZonalStatistics::Minority )
{
float minorityKey = featureStats.valueCount.key( vals.first() );
changeAttributeMap.insert( minorityIndex, QVariant( minorityKey ) );
}
if ( mStatistics & QgsZonalStatistics::Majority )
{
float majKey = featureStats.valueCount.key( vals.last() );
changeAttributeMap.insert( majorityIndex, QVariant( majKey ) );
}
}
if ( mStatistics & QgsZonalStatistics::Variety )
changeAttributeMap.insert( varietyIndex, QVariant( featureStats.valueCount.count() ) );
}
changeMap.insert( f.id(), changeAttributeMap );
++featureCounter;
}
vectorProvider->changeAttributeValues( changeMap );
if ( p )
{
p->setValue( featureCount );
}
GDALClose( inputDataset );
mPolygonLayer->updateFields();
if ( p && p->wasCanceled() )
{
return 9;
}
return 0;
}
示例3: calculateStatistics
//.........这里部分代码省略.........
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 )
{
p->setValue( featureCounter );
}
if ( p && p->wasCanceled() )
{
break;
}
QgsGeometry* featureGeometry = f.geometry();
if ( !featureGeometry )
{
++featureCounter;
continue;
}
int offsetX, offsetY, nCellsX, nCellsY;
if ( cellInfoForBBox( rasterBBox, featureGeometry->boundingBox(), cellsizeX, cellsizeY, offsetX, offsetY, nCellsX, nCellsY ) != 0 )
{
++featureCounter;
continue;
}
statisticsFromMiddlePointTest_improved( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY,
rasterBBox, sum, count );
if ( count <= 1 )
{
//the cell resolution is probably larger than the polygon area. We switch to precise pixel - polygon intersection in this case
statisticsFromPreciseIntersection( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY,
rasterBBox, sum, count );
}
if ( count == 0 )
{
mean = 0;
}
else
{
mean = sum / count;
}
//write the statistics value to the vector data provider
QgsChangedAttributesMap changeMap;
QgsAttributeMap changeAttributeMap;
changeAttributeMap.insert( countIndex, QVariant( count ) );
changeAttributeMap.insert( sumIndex, QVariant( sum ) );
changeAttributeMap.insert( meanIndex, QVariant( mean ) );
changeMap.insert( f.id(), changeAttributeMap );
vectorProvider->changeAttributeValues( changeMap );
++featureCounter;
}
if ( p )
{
p->setValue( featureCount );
}
GDALClose( inputDataset );
return 0;
}
示例4: 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;
}
//.........这里部分代码省略.........