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中去了。結果如圖:





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章