Qt+QGis二次开发:打开S-57格式(*.000)电子海图数据,并设置多边形要素的显示风格

不过多的废话了,直接上源码:

addChartlayers()方法时“打开海图”按钮的triggered()信号所绑定的槽函数。
//添加海图数据小按钮槽函数
void MainWindow::addChartlayers()
{
  m_mapCanvas->freeze(true);//冻结或解冻地图画布对象,frozen (true) or thawed (false). Default is true.
    //步骤1:打开文件选择对话框
    QString filename=QFileDialog::getOpenFileName(this,tr("打开海图数据"),"","*.000");
    if(filename.isNull())//如果未选择文件则返回
    {
        return;
    }
    QFileInfo fi(filename);
    QString basename=fi.baseName();//获取文件基名称。基名称:即不包含路径又不包含扩展名的文件名
    //步骤2:创建QgsVectorLayer类
    QgsVectorLayer* layer=new QgsVectorLayer(filename,basename,"ogr",false);
    if(!layer->isValid())//如果图层不合法
    {
        QMessageBox::critical(this,"error","图层无效!");
        return;
    }
    else
    {
     //QStringList sEncodings = QgsVectorDataProvider::availableEncodings();
        layer->setProviderEncoding( "System"  );//设置图层的编码格式
        QStringList sublayers = layer->dataProvider()->subLayers();//获取用户选择的电子海图所包含的全部子图层的字符串名称
        int subLayersCount=sublayers.count();// If the newly created layer has more than 1 layer of data available, we show the sublayers selection dialog so the user can select the sublayers to actually load.
        if ( subLayersCount >= 1 )
        {
            for(int i=0;i<subLayersCount;i++)
            {
                //注册添加矢量数据,并个并添加到画布中
                QStringList sLayerDefs = sublayers[i].split( ':' );//.000文件中的子图层的字符串名称结构:如0:DSID:Unknown:None和1:BUAARE:15:Point等
                QString composedURI = filename + "|layerid=" + sLayerDefs[0] ;
                QString layerName =basename+"@"+sLayerDefs[1];

                QString layerGeometryType = sLayerDefs[3];//图层类型

                QgsVectorLayer* layerTemp;
                if( !layerGeometryType.isEmpty() && layerGeometryType=="Polygon" && sLayerDefs[1]=="LNDARE" )//LNDARE是我的*.000数据中,我打算显示的那个子图层的图层名称
                {
                    composedURI += "|geometrytype=" + layerGeometryType;//设置海图子图层的完全字符串名称
                    layerTemp=new QgsVectorLayer(composedURI,layerName,"ogr",false);

                    if(!layerTemp->isValid())//如果图层不合法
                    {
                        QMessageBox::critical(this,"error","海图子图层无效!");
                        return;
                    }
                    QgsMapLayerRegistry::instance()->addMapLayer(layerTemp,true,true);
            //设置该图层的单一渲染风格 QgsSymbolV2
* symbol = QgsSymbolV2::defaultSymbol(layerTemp->geometryType());//QGis::GeometryType::Polygon symbol->setColor(QColor(100,100,160,255)); layerTemp->setRendererV2( new QgsSingleSymbolRendererV2(symbol) ); m_mapCanvasLayerSet.append(layerTemp); m_mapCanvas->setExtent(layerTemp->extent());//设置显示范围 } else { continue; } } delete layer;// The first layer loaded is not useful in that case. } else { QString msg = tr( "%1 doesn't have any layers" ).arg( basename ); QMessageBox::critical(this,"Invalid Data Source",msg); delete layer; } } //步骤3:将图层集合添加到画布中 m_mapCanvas->setLayerSet(m_mapCanvasLayerSet);//设置图层集合 m_mapCanvas->setVisible(true);//设置是否可见 m_mapCanvas->freeze(false);//解冻图层 m_mapCanvas->refresh();//刷新 //在状态条设置地图的比例尺 double dCurrScale= m_mapCanvas->scale(); QString sCurrScale=this->m_scaleEdit->toString(dCurrScale); this->m_scaleEdit->setScaleString(sCurrScale); }

参考链接:

1、QGis C++ 开发之图层分类显示

2、QGis二次开发基础 -- 矢量图层的显示样式

posted @ 2019-02-13 22:17  rainbow70626  阅读(5450)  评论(0编辑  收藏  举报