
将class字段添加到shp中去:
(1)从Excel中读取数据(为了读取方便,存为.csv或者txt文件)
[cpp] view plain copy
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
首先要创建新字段名,然后再插入值
[cpp] view plain copy
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 = 0i <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 = 0j <readlist.count()++j)
{
QString filed
QgsChangedAttributesMap changeMap
QgsAttributeMap changeAttributeMap
QStringList field = readlist.at( j ).split(",", QString::SkipEmptyParts)
for ( int k = 0k <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是不一致的。
[cpp] view plain copy
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 = 0i <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中去了
另有参考网址如下,似乎不行https://t3.tianditu.gov.cn/img_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILECOL= {x}&TILEROW={y}&TILEMATRIX={z}&tk=
以下可用:
http://t5.tianditu.gov.cn/DataServer?T=img_w&x=%7Bx%7D&y=%7By%7D&l=%7Bz%7D&tk= 密钥
其中的T=img_w,可替换为不同的数据类型,说明如下:
前三位为类型:vec-矢量,cva-矢量注记,img-影像,cia-影像注记,ter-地形,cta-地形注记,ibo-全球境界,eva-矢量英文注记,eia-影像英文注记。
最后一位为投影类型,c-经纬度投影,w-球面墨卡托投影。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)