QGis(四)shp矢量图层添加新字段

添加一个新的字段到shp文件中,并且从Excel里导入数据到该字段。原shp文件里的字段ID应该与Excel里的字段ID一一对应才能正确的导入。下图分别是shp的字段和Excel的字段

将class字段添加到shp中去:

(1)从Excel中读取数据(为了读取方便,存为.csv或者txt文件)

QStringList readFromCSV(QString mfilename)
{
	QStringList readlist;
 	if (mfilename !="")
	{
		QFileInfo csvFI(mfilename);
		QString ext = csvFI.suffix();
		if ( ext == "csv" || ext == "txt")
		{
			QFile *importFile = new QFile(mfilename);
			if ( !importFile->open(QIODevice::ReadOnly | QIODevice::Text))
			{
				QMessageBox::information(NULL, "error", "Cannot open import file !", QMessageBox::Yes | QMessageBox::No);
				return readlist;
			}
			readlist.clear();
			QTextStream readIn(importFile);//读入文件
			while ( !readIn.atEnd()) //读取每一行
			{
				readlist.push_back(readIn.readLine());
			}

			importFile->close();
		}
	}
	return readlist;
}
返回的readlist是所有行的数据,下面要根据Id来将每一行后面的class字段插入shp文件

(2)插入class字段及值到shp

首先要创建新字段名,然后再插入值

bool ImportLandInfo::insertInfo(QString mShpfile)
{

	QgsVectorLayer * newLayer;

	newLayer = new QgsVectorLayer(mShpfile, fileinfo.baseName(), "ogr");
	if ( newLayer != NULL)
	{
		qDebug("newLayer is valid");
	}
	else
	{
		return false;
	}
	QStringList readlist = readFromCSV(“F:\\data.csv”);//Excel文件

	//创建新字段
	QList<QgsField> newFieldList;
	QStringList fields = readlist.at(0).split(",", QString::SkipEmptyParts);  //得到Excel的字段名
	for (int i = 0; i < fields.count(); ++i)
	{
		QString fieldname;
		if ( fields.at(i) == "Id" )
		{
			continue;
		}
		else
		{
			fieldname = fields.at(i);
		}
		QgsField shpField( fieldname, QVariant::String);
		newFieldList.push_back( shpField );
		
	}
	QgsVectorDataProvider* vectorProvider = newLayer->dataProvider();
	vectorProvider->addAttributes( newFieldList );
	
	//新字段中插入值
	QMap<int, int> idmap = generateIdIndex(); //由原shp图层得到QMap<ID, featureId>
	int fieldIndex = -1;  //每个待插入字段的索引号
	int IdIndex = -1;    // ID字段的索引号
	for (int j = 0; j < readlist.count(); ++j)
	{
		QString filed; 
		QgsChangedAttributesMap changeMap;
		QgsAttributeMap changeAttributeMap;

		QStringList field = readlist.at( j ).split(",", QString::SkipEmptyParts);
		for ( int k = 0; k < field.count(); ++k)
		{
			if (  field.at(k) == "Id" )
			{	
				IdIndex = k;
				continue;
			}			
			if ( j == 0)  //第一行时是计算字段在属性表中的index
			{	
				fieldIndex = vectorProvider->fieldNameIndex( field.at(k) );
				break;
			}
			else  //不是第一行则插入
			{
				changeAttributeMap.insert( fieldIndex + k - 1, QVariant( field.at(k) ) );
			}
		}
		if ( j == 0)
		{
			continue;
		}
 		int ID = field.at(IdIndex).toInt();
		
		QMap<int, int>::iterator i = idmap.find( ID); //找到指定ID对应的要素id(featureId)
		int featureId = i.value();
		changeMap.insert( featureId, changeAttributeMap );
		vectorProvider->changeAttributeValues( changeMap );
	}
	delete vectorProvider;
	return true;
}
generateIdIndex()是为了得到Id对应的FeatureID,因为属性字段Id和要素的FeatureID是不一致的。

QMap<int, int> ImportLandInfo::generateIdIndex()
{
	QMap<int, int> idMap;
	QgsVectorLayer * orignalLayer;
	QFileInfo fileinfo(moriginalShpfile);
	orignalLayer = new QgsVectorLayer(moriginalShpfile, fileinfo.baseName(), "ogr");
	if ( orignalLayer != NULL)
	{
		qDebug("newLayer is valid");
	}
	QgsVectorDataProvider* vectorProvider = orignalLayer->dataProvider();
 	QgsFeature feature;

	int idIndex = vectorProvider->fieldNameIndex( "Id" );
	int count = orignalLayer->featureCount();
	for ( int i = 0; i < count; ++i)
	{
		orignalLayer->featureAtId( i, feature);
		const QgsAttributeMap &attributes = feature.attributeMap();
		int id = -1;
		id = attributes[ idIndex].toInt();
		idMap.insert( id, feature.id());
	}	
	return idMap;
}

这样字段class的值就添加到shp中去了。结果如图:





  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值