本文整理汇总了C++中QgsVectorLayer::fieldNameIndex方法的典型用法代码示例。如果您正苦于以下问题:C++ QgsVectorLayer::fieldNameIndex方法的具体用法?C++ QgsVectorLayer::fieldNameIndex怎么用?C++ QgsVectorLayer::fieldNameIndex使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QgsVectorLayer
的用法示例。
在下文中一共展示了QgsVectorLayer::fieldNameIndex方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: exportErrorsDo
bool QgsGeometryCheckerResultTab::exportErrorsDo( const QString& file )
{
QList< QPair<QString, QString> > attributes;
attributes.append( qMakePair( QString( "FeatureID" ), QString( "String;10;" ) ) );
attributes.append( qMakePair( QString( "ErrorDesc" ), QString( "String;80;" ) ) );
QLibrary ogrLib( QgsProviderRegistry::instance()->library( "ogr" ) );
if ( !ogrLib.load() )
{
return false;
}
typedef bool ( *createEmptyDataSourceProc )( const QString&, const QString&, const QString&, QGis::WkbType, const QList< QPair<QString, QString> >&, const QgsCoordinateReferenceSystem * );
createEmptyDataSourceProc createEmptyDataSource = ( createEmptyDataSourceProc ) cast_to_fptr( ogrLib.resolve( "createEmptyDataSource" ) );
if ( !createEmptyDataSource )
{
return false;
}
if ( !createEmptyDataSource( file, "ESRI Shapefile", mFeaturePool->getLayer()->dataProvider()->encoding(), QGis::WKBPoint, attributes, &mFeaturePool->getLayer()->crs() ) )
{
return false;
}
QgsVectorLayer* layer = new QgsVectorLayer( file, QFileInfo( file ).baseName(), "ogr" );
if ( !layer->isValid() )
{
delete layer;
return false;
}
int fieldFeatureId = layer->fieldNameIndex( "FeatureID" );
int fieldErrDesc = layer->fieldNameIndex( "ErrorDesc" );
for ( int row = 0, nRows = ui.tableWidgetErrors->rowCount(); row < nRows; ++row )
{
QgsGeometryCheckError* error = ui.tableWidgetErrors->item( row, 0 )->data( Qt::UserRole ).value<QgsGeometryCheckError*>();
QgsFeature f( layer->pendingFields() );
f.setAttribute( fieldFeatureId, error->featureId() );
f.setAttribute( fieldErrDesc, error->description() );
f.setGeometry( new QgsGeometry( error->location().clone() ) );
layer->dataProvider()->addFeatures( QgsFeatureList() << f );
}
// Remove existing layer with same uri
QStringList toRemove;
foreach ( QgsMapLayer* maplayer, QgsMapLayerRegistry::instance()->mapLayers() )
{
if ( dynamic_cast<QgsVectorLayer*>( maplayer ) &&
static_cast<QgsVectorLayer*>( maplayer )->dataProvider()->dataSourceUri() == layer->dataProvider()->dataSourceUri() )
{
toRemove.append( maplayer->id() );
}
}
if ( !toRemove.isEmpty() )
{
QgsMapLayerRegistry::instance()->removeMapLayers( toRemove );
}
QgsMapLayerRegistry::instance()->addMapLayers( QList<QgsMapLayer*>() << layer );
return true;
}
示例2: 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();
}
示例3: readXML
int QgsUniqueValueRenderer::readXML( const QDomNode& rnode, QgsVectorLayer& vl )
{
mGeometryType = vl.geometryType();
QDomNode classnode = rnode.namedItem( "classificationfield" );
QString classificationField = classnode.toElement().text();
QgsVectorDataProvider* theProvider = vl.dataProvider();
if ( !theProvider )
{
return 1;
}
int classificationId = vl.fieldNameIndex( classificationField );
if ( classificationId == -1 )
{
//go on. Because with joins, it might be the joined layer is not loaded yet
}
setClassificationField( classificationId );
QDomNode symbolnode = rnode.namedItem( "symbol" );
while ( !symbolnode.isNull() )
{
QgsSymbol* msy = new QgsSymbol( mGeometryType );
msy->readXML( symbolnode, &vl );
insertValue( msy->lowerValue(), msy );
symbolnode = symbolnode.nextSibling();
}
updateSymbolAttributes();
vl.setRenderer( this );
return 0;
}
示例4: addLayerProjectSettings
void QgsServerProjectParser::addLayerProjectSettings( QDomElement& layerElem, QDomDocument& doc, QgsMapLayer* currentLayer ) const
{
if ( !currentLayer )
{
return;
}
// Layer tree name
QDomElement treeNameElem = doc.createElement( "TreeName" );
QDomText treeNameText = doc.createTextNode( currentLayer->name() );
treeNameElem.appendChild( treeNameText );
layerElem.appendChild( treeNameElem );
if ( currentLayer->type() == QgsMapLayer::VectorLayer )
{
QgsVectorLayer* vLayer = static_cast<QgsVectorLayer*>( currentLayer );
const QSet<QString>& excludedAttributes = vLayer->excludeAttributesWMS();
int displayFieldIdx = vLayer->fieldNameIndex( vLayer->displayField() );
QString displayField = displayFieldIdx < 0 ? "maptip" : vLayer->displayField();
//attributes
QDomElement attributesElem = doc.createElement( "Attributes" );
const QgsFields& layerFields = vLayer->pendingFields();
for ( int idx = 0; idx < layerFields.count(); ++idx )
{
const QgsField& field = layerFields[idx];
if ( excludedAttributes.contains( field.name() ) )
{
continue;
}
// field alias in case of displayField
if ( idx == displayFieldIdx )
{
displayField = vLayer->attributeDisplayName( idx );
}
QDomElement attributeElem = doc.createElement( "Attribute" );
attributeElem.setAttribute( "name", field.name() );
attributeElem.setAttribute( "type", QVariant::typeToName( field.type() ) );
attributeElem.setAttribute( "typeName", field.typeName() );
QString alias = vLayer->attributeAlias( idx );
if ( !alias.isEmpty() )
{
attributeElem.setAttribute( "alias", alias );
}
//edit type to text
attributeElem.setAttribute( "editType", vLayer->editFormConfig()->widgetType( idx ) );
attributeElem.setAttribute( "comment", field.comment() );
attributeElem.setAttribute( "length", field.length() );
attributeElem.setAttribute( "precision", field.precision() );
attributesElem.appendChild( attributeElem );
}
//displayfield
layerElem.setAttribute( "displayField", displayField );
//geometry type
layerElem.setAttribute( "geometryType", QGis::featureType( vLayer->wkbType() ) );
layerElem.appendChild( attributesElem );
}
}
示例5: test_willRenderFeature_symbolsForFeature
void test_willRenderFeature_symbolsForFeature()
{
// prepare features
QgsVectorLayer* layer = new QgsVectorLayer( "point?field=fld:int", "x", "memory" );
int idx = layer->fieldNameIndex( "fld" );
QVERIFY( idx != -1 );
QgsFeature f1;
f1.initAttributes( 1 );
f1.setAttribute( idx, QVariant( 2 ) );
QgsFeature f2;
f2.initAttributes( 1 );
f2.setAttribute( idx, QVariant( 8 ) );
QgsFeature f3;
f3.initAttributes( 1 );
f3.setAttribute( idx, QVariant( 100 ) );
// prepare renderer
QgsSymbol* s1 = QgsSymbol::defaultSymbol( QgsWkbTypes::PointGeometry );
QgsSymbol* s2 = QgsSymbol::defaultSymbol( QgsWkbTypes::PointGeometry );
RRule* rootRule = new RRule( nullptr );
rootRule->appendChild( new RRule( s1, 0, 0, "fld >= 5 and fld <= 20" ) );
rootRule->appendChild( new RRule( s2, 0, 0, "fld <= 10" ) );
QgsRuleBasedRenderer r( rootRule );
QVERIFY( r.capabilities() & QgsFeatureRenderer::MoreSymbolsPerFeature );
QgsRenderContext ctx; // dummy render context
ctx.expressionContext().setFields( layer->fields() );
r.startRender( ctx, layer->fields() );
// test willRenderFeature
ctx.expressionContext().setFeature( f1 );
QVERIFY( r.willRenderFeature( f1, ctx ) );
ctx.expressionContext().setFeature( f2 );
QVERIFY( r.willRenderFeature( f2, ctx ) );
ctx.expressionContext().setFeature( f3 );
QVERIFY( !r.willRenderFeature( f3, ctx ) );
// test symbolsForFeature
ctx.expressionContext().setFeature( f1 );
QgsSymbolList lst1 = r.symbolsForFeature( f1, ctx );
QVERIFY( lst1.count() == 1 );
ctx.expressionContext().setFeature( f2 );
QgsSymbolList lst2 = r.symbolsForFeature( f2, ctx );
QVERIFY( lst2.count() == 2 );
ctx.expressionContext().setFeature( f3 );
QgsSymbolList lst3 = r.symbolsForFeature( f3, ctx );
QVERIFY( lst3.isEmpty() );
r.stopRender( ctx );
delete layer;
}
示例6: readXML
int QgsContinuousColorRenderer::readXML( const QDomNode& rnode, QgsVectorLayer& vl )
{
mGeometryType = vl.geometryType();
QDomNode classnode = rnode.namedItem( "classificationfield" );
QString classificationField = classnode.toElement().text();
QgsVectorDataProvider* theProvider = vl.dataProvider();
if ( !theProvider )
{
return 1;
}
int classificationId = vl.fieldNameIndex( classificationField );
if ( classificationId == -1 )
{
//go on. Because with joins, it might be the joined layer is not loaded yet
}
setClassificationField( classificationId );
//polygon outline
QDomNode polyoutlinenode = rnode.namedItem( "polygonoutline" );
QString polyoutline = polyoutlinenode.toElement().text();
if ( polyoutline == "0" )
{
mDrawPolygonOutline = false;
}
else if ( polyoutline == "1" )
{
mDrawPolygonOutline = true;
}
//read the settings for the renderitem of the minimum value
QDomNode lowernode = rnode.namedItem( "lowestsymbol" );
QDomNode lsymbolnode = lowernode.namedItem( "symbol" );
if ( ! lsymbolnode.isNull() )
{
QgsSymbol* lsy = new QgsSymbol( mGeometryType );
lsy->readXML( lsymbolnode, &vl );
setMinimumSymbol( lsy );
}
QDomNode uppernode = rnode.namedItem( "highestsymbol" );
QDomNode usymbolnode = uppernode.namedItem( "symbol" );
if ( ! usymbolnode.isNull() )
{
QgsSymbol* usy = new QgsSymbol( mGeometryType );
usy->readXML( usymbolnode, &vl );
setMaximumSymbol( usy );
}
vl.setRenderer( this );
return 0;
}
示例7: readMapLayer
void QgsEditorWidgetRegistry::readMapLayer( QgsMapLayer* mapLayer, const QDomElement& layerElem )
{
if ( mapLayer->type() != QgsMapLayer::VectorLayer )
{
return;
}
QgsVectorLayer* vectorLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
Q_ASSERT( vectorLayer );
QDomNodeList editTypeNodes = layerElem.namedItem( "edittypes" ).childNodes();
for ( int i = 0; i < editTypeNodes.size(); i++ )
{
QDomNode editTypeNode = editTypeNodes.at( i );
QDomElement editTypeElement = editTypeNode.toElement();
QString name = editTypeElement.attribute( "name" );
int idx = vectorLayer->fieldNameIndex( name );
if ( idx == -1 )
continue;
QString ewv2Type = editTypeElement.attribute( "widgetv2type" );
QgsEditorWidgetConfig cfg;
if ( mWidgetFactories.contains( ewv2Type ) )
{
vectorLayer->editFormConfig()->setWidgetType( idx, ewv2Type );
QDomElement ewv2CfgElem = editTypeElement.namedItem( "widgetv2config" ).toElement();
if ( !ewv2CfgElem.isNull() )
{
cfg = mWidgetFactories[ewv2Type]->readEditorConfig( ewv2CfgElem, vectorLayer, idx );
}
vectorLayer->editFormConfig()->setReadOnly( idx, ewv2CfgElem.attribute( "fieldEditable", "1" ) != "1" );
vectorLayer->editFormConfig()->setLabelOnTop( idx, ewv2CfgElem.attribute( "labelOnTop", "0" ) == "1" );
vectorLayer->editFormConfig()->setNotNull( idx, ewv2CfgElem.attribute( "notNull", "0" ) == "1" );
vectorLayer->editFormConfig()->setExpression( idx, ewv2CfgElem.attribute( "constraint", QString() ) );
vectorLayer->editFormConfig()->setExpressionDescription( idx, ewv2CfgElem.attribute( "constraintDescription", QString() ) );
vectorLayer->editFormConfig()->setWidgetConfig( idx, cfg );
}
else
{
QgsMessageLog::logMessage( tr( "Unknown attribute editor widget '%1'" ).arg( ewv2Type ) );
}
}
}
示例8: setModelData
void FieldSelectorDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
{
QgsVectorLayerAndAttributeModel *m = dynamic_cast< QgsVectorLayerAndAttributeModel *>( model );
if ( !m )
return;
QgsVectorLayer *vl = m->vectorLayer( index );
if ( !vl )
return;
QgsFieldComboBox *fcb = qobject_cast<QgsFieldComboBox *>( editor );
if ( !fcb )
return;
model->setData( index, vl->fieldNameIndex( fcb->currentField() ) );
}
示例9: 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 "";
}
示例10: readXML
int QgsGraduatedSymbolRenderer::readXML( const QDomNode& rnode, QgsVectorLayer& vl )
{
mGeometryType = vl.geometryType();
QDomNode modeNode = rnode.namedItem( "mode" );
QString modeValue = modeNode.toElement().text();
QDomNode classnode = rnode.namedItem( "classificationfield" );
QString classificationField = classnode.toElement().text();
QgsVectorDataProvider* theProvider = vl.dataProvider();
if ( !theProvider )
{
return 1;
}
if ( modeValue == "Empty" )
{
mMode = QgsGraduatedSymbolRenderer::Empty;
}
else if ( modeValue == "Quantile" )
{
mMode = QgsGraduatedSymbolRenderer::Quantile;
}
else //default
{
mMode = QgsGraduatedSymbolRenderer::EqualInterval;
}
int classificationId = vl.fieldNameIndex( classificationField );
if ( classificationId == -1 )
{
//go on. Because with joins, it might be the joined layer is not loaded yet
}
setClassificationField( classificationId );
QDomNode symbolnode = rnode.namedItem( "symbol" );
while ( !symbolnode.isNull() )
{
QgsSymbol* sy = new QgsSymbol( mGeometryType );
sy->readXML( symbolnode, &vl );
addSymbol( sy );
symbolnode = symbolnode.nextSibling();
}
updateSymbolAttributes();
vl.setRenderer( this );
return 0;
}
示例11: currentLabelText
QString QgsMapToolLabel::currentLabelText()
{
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->featureAtId( mCurrentLabelPos.featureId, f, false, true ) )
{
return f.attributeMap()[labelFieldId].toString();
}
}
return "";
}
示例12: test_willRenderFeature_symbolsForFeature
void test_willRenderFeature_symbolsForFeature()
{
// prepare features
QgsVectorLayer* layer = new QgsVectorLayer( "point?field=fld:int", "x", "memory" );
int idx = layer->fieldNameIndex( "fld" );
QVERIFY( idx != -1 );
QgsFeature f1; f1.initAttributes( 1 ); f1.setAttribute( idx, QVariant( 2 ) );
QgsFeature f2; f2.initAttributes( 1 ); f2.setAttribute( idx, QVariant( 8 ) );
QgsFeature f3; f3.initAttributes( 1 ); f3.setAttribute( idx, QVariant( 100 ) );
// prepare renderer
QgsSymbolV2* s1 = QgsSymbolV2::defaultSymbol( QGis::Point );
QgsSymbolV2* s2 = QgsSymbolV2::defaultSymbol( QGis::Point );
RRule* rootRule = new RRule( NULL );
rootRule->appendChild( new RRule( s1, 0, 0, "fld >= 5 and fld <= 20" ) );
rootRule->appendChild( new RRule( s2, 0, 0, "fld <= 10" ) );
QgsRuleBasedRendererV2 r( rootRule );
QVERIFY( r.capabilities() & QgsFeatureRendererV2::MoreSymbolsPerFeature );
QgsRenderContext ctx; // dummy render context
r.startRender( ctx, layer->pendingFields() );
// test willRenderFeature
QVERIFY( r.willRenderFeature( f1 ) );
QVERIFY( r.willRenderFeature( f2 ) );
QVERIFY( !r.willRenderFeature( f3 ) );
// test symbolsForFeature
QgsSymbolV2List lst1 = r.symbolsForFeature( f1 );
QVERIFY( lst1.count() == 1 );
QgsSymbolV2List lst2 = r.symbolsForFeature( f2 );
QVERIFY( lst2.count() == 2 );
QgsSymbolV2List lst3 = r.symbolsForFeature( f3 );
QVERIFY( lst3.count() == 0 );
r.stopRender( ctx );
delete layer;
}
示例13: sourceLayer
void QgsComposerAttributeTableV2::restoreFieldAliasMap( const QMap<int, QString>& map )
{
QgsVectorLayer* source = sourceLayer();
if ( !source )
{
return;
}
QList<QgsComposerTableColumn*>::const_iterator columnIt = mColumns.constBegin();
for ( ; columnIt != mColumns.constEnd(); ++columnIt )
{
int attrIdx = source->fieldNameIndex(( *columnIt )->attribute() );
if ( map.contains( attrIdx ) )
{
( *columnIt )->setHeading( map.value( attrIdx ) );
}
else
{
( *columnIt )->setHeading( source->attributeDisplayName( attrIdx ) );
}
}
}
示例14: readMapLayer
void QgsEditorWidgetRegistry::readMapLayer( QgsMapLayer* mapLayer, const QDomElement& layerElem )
{
if ( mapLayer->type() != QgsMapLayer::VectorLayer )
{
return;
}
QgsVectorLayer* vectorLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
Q_ASSERT( vectorLayer );
QDomNodeList editTypeNodes = layerElem.namedItem( "edittypes" ).childNodes();
for ( int i = 0; i < editTypeNodes.size(); i++ )
{
QDomNode editTypeNode = editTypeNodes.at( i );
QDomElement editTypeElement = editTypeNode.toElement();
QString name = editTypeElement.attribute( "name" );
int idx = vectorLayer->fieldNameIndex( name );
if ( idx == -1 )
continue;
bool hasLegacyType;
QgsVectorLayer::EditType editType =
( QgsVectorLayer::EditType ) editTypeElement.attribute( "type" ).toInt( &hasLegacyType );
QString ewv2Type;
QgsEditorWidgetConfig cfg;
if ( hasLegacyType && editType != QgsVectorLayer::EditorWidgetV2 )
{
Q_NOWARN_DEPRECATED_PUSH
ewv2Type = readLegacyConfig( vectorLayer, editTypeElement, cfg );
Q_NOWARN_DEPRECATED_POP
}
else
示例15: loadAttributes
void QgsAttributeTableModel::loadAttributes()
{
if ( !layer() )
{
return;
}
bool ins = false, rm = false;
QgsAttributeList attributes;
const QgsFields& fields = layer()->pendingFields();
for ( int idx = 0; idx < fields.count(); ++idx )
{
switch ( layer()->editType( idx ) )
{
case QgsVectorLayer::Hidden:
continue;
case QgsVectorLayer::ValueMap:
mValueMaps.insert( idx, new QMap< QString, QVariant >( layer()->valueMap( idx ) ) );
break;
case QgsVectorLayer::ValueRelation:
{
const QgsVectorLayer::ValueRelationData &data = layer()->valueRelation( idx );
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( data.mLayer ) );
if ( !layer )
continue;
int ki = layer->fieldNameIndex( data.mKey );
int vi = layer->fieldNameIndex( data.mValue );
QgsExpression *e = 0;
if ( !data.mFilterExpression.isEmpty() )
{
e = new QgsExpression( data.mFilterExpression );
if ( e->hasParserError() || !e->prepare( layer->pendingFields() ) )
continue;
}
if ( ki >= 0 && vi >= 0 )
{
QSet<int> attributes;
attributes << ki << vi;
QgsFeatureRequest::Flag flags = QgsFeatureRequest::NoGeometry;
if ( e )
{
if ( e->needsGeometry() )
flags = QgsFeatureRequest::NoFlags;
foreach ( const QString &field, e->referencedColumns() )
{
int idx = layer->fieldNameIndex( field );
if ( idx < 0 )
continue;
attributes << idx;
}
}
QMap< QString, QVariant > *map = new QMap< QString, QVariant >();
QgsFeatureIterator fit = layer->getFeatures( QgsFeatureRequest().setFlags( flags ).setSubsetOfAttributes( attributes.toList() ) );
QgsFeature f;
while ( fit.nextFeature( f ) )
{
if ( e && !e->evaluate( &f ).toBool() )
continue;
map->insert( f.attribute( vi ).toString(), f.attribute( ki ) );
}
mValueMaps.insert( idx, map );
}
}
break;
default:
break;
}
attributes << idx;
}