Qgis二次开发-加载高德在线地图失败报错Raster layer Cannot instantiate the ‘wms‘ data provider

1.加载在线的高德地图

以下是在线高德地图地址。

cpp 复制代码
http://webrd01.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scale=1&style=8

以下是代码示例:

cpp 复制代码
#include <QApplication>
#include <QMainWindow>
#include "qgisinterface.h"
#include "qgsproviderregistry.h"
#include "qgsrasterlayer.h"
#include "qgsproject.h"
#include "qgsmapcanvas.h"
int main(int argc, char *argv[])
{
    // 初始化QGIS
    QgsApplication app(argc, argv, true);
    app.initQgis();
    // 创建主窗口
    QMainWindow mainWindow;
    mainWindow.setWindowTitle("QGIS with Amap");
    // 创建地图Canvas
    QgsMapCanvas* mapCanvas = new QgsMapCanvas(&mainWindow);
    mapCanvas->setCanvasColor(Qt::white);
    mapCanvas->setVisible(true);
    mapCanvas->setFrameStyle(0);
    mainWindow.setCentralWidget(mapCanvas);
    // 创建栅格图层
    QgsRasterLayer* amapLayer = new QgsRasterLayer("http://webrd01.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scale=1&style=8", "Amap", "wms");
    if (amapLayer->isValid())
    {
        QgsProject::instance()->addMapLayer(amapLayer);
        QgsRectangle extent = amapLayer->extent();
        mapCanvas->setExtent(extent);
    }
    else
    {
        QMessageBox::critical(&mainWindow, "Error", "Could not load Amap layer.");
        return -1;
    }
    // 设置地图范围和投影
    QgsCoordinateReferenceSystem crs("EPSG:3857");
    mapCanvas->setDestinationCrs(crs);
    mapCanvas->enableAntiAliasing(true);
    // 显示主窗口
    mainWindow.show();
    // 执行QGIS事件循环
    return app.exec();
}

结果运行结果无效并报错:

Raster layer Cannot instantiate the 'wms' data provider

大概的意思就是说不能初始化wms的东西,就是wms的相关驱动没有加载好。

解决办法:

1.设置路径

cpp 复制代码
    //设置前缀路径
    QgsApplication app(argc, argv, true);
    app.setPrefixPath("E:/OSGeo4W/apps/qgis-ltr-dev", true);
    app.initQgis();

2.将驱动文件拷贝到运行目录下

不需要设置setPrefixPath()。

将E:/OSGeo4W/apps/qgis-ltr-dev/plugins目录拷贝到生成程序的运行目录下,因为程序启动起来加载的是本地的plugins下的插件。

注意不要拷贝错了,我下载的目录下有两个qgis目录,你使用的哪个路径下的库来编译的程序,就拷贝哪个路径下的plugins目录,否则有可能导致程序起不来。

2.如果url填入不能进行正常加载

提供一个编码url的类

cpp 复制代码
#ifndef QGSXYZCONNECTION_H
#define QGSXYZCONNECTION_H

#include <QStringList>

struct QgsXyzConnection
{
  QString name;
  QString url;
  int zMin = -1;
  int zMax = -1;
  // Authentication configuration id
  QString authCfg;
  // HTTP Basic username
  QString username;
  // HTTP Basic password
  QString password;
  // Referer
  QString referer;
  // tile pixel ratio (0 = unknown (not scaled), 1.0 = 256x256, 2.0 = 512x512)
  double tilePixelRatio = 0;
  bool hidden = false;

  QString encodedUri() const;
};

//! Utility class for handling list of connections to XYZ tile layers
class QgsXyzConnectionUtils
{
  public:
    //! Returns list of existing connections, unless the hidden ones
    static QStringList connectionList();

    //! Returns last used connection
    static QString selectedConnection();

    //! Saves name of the last used connection
    static void setSelectedConnection( const QString &connName );

    //! Returns connection details
    static QgsXyzConnection connection( const QString &name );

    //! Removes a connection from the list
    static void deleteConnection( const QString &name );

    //! Adds a new connection to the list
    static void addConnection( const QgsXyzConnection &conn );
};


#endif // QGSXYZCONNECTION_H



#include <qgslogger.h>
#include "qgsxyzconnection.h"

#include <qgsdatasourceuri.h>
#include <qgssettings.h>

QString QgsXyzConnection::encodedUri() const
{
  QgsDataSourceUri uri;
  uri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
  uri.setParam( QStringLiteral( "url" ), url );
  if ( zMin != -1 )
    uri.setParam( QStringLiteral( "zmin" ), QString::number( zMin ) );
  if ( zMax != -1 )
    uri.setParam( QStringLiteral( "zmax" ), QString::number( zMax ) );
  if ( ! authCfg.isEmpty() )
    uri.setAuthConfigId( authCfg );
  if ( ! username.isEmpty() )
    uri.setUsername( username );
  if ( ! password.isEmpty() )
    uri.setPassword( password );
  if ( ! referer.isEmpty() )
    uri.setParam( QStringLiteral( "referer" ), referer );
  if ( tilePixelRatio != 0 )
    uri.setParam( QStringLiteral( "tilePixelRatio" ), QString::number( tilePixelRatio ) );
  return uri.encodedUri();
}

QStringList QgsXyzConnectionUtils::connectionList()
{
  QgsSettings settings;
  QStringList connList;

  settings.beginGroup( QStringLiteral( "qgis/connections-xyz" ) );
  connList = settings.childGroups();

  const QStringList global = settings.globalChildGroups();
  settings.endGroup();

  for ( const auto &s : global )
  {
    settings.beginGroup( "qgis/connections-xyz/" + s );
    bool isHidden = settings.value( QStringLiteral( "hidden" ), false ).toBool();
    settings.endGroup();
    if ( isHidden )
    {
      connList.removeOne( s );
    }
  }

  return connList;
}

QString QgsXyzConnectionUtils::selectedConnection()
{
  QgsSettings settings;
  return settings.value( QStringLiteral( "qgis/connections-xyz/selected" ) ).toString();
}

void QgsXyzConnectionUtils::setSelectedConnection( const QString &name )
{
  QgsSettings settings;
  return settings.setValue( QStringLiteral( "qgis/connections-xyz/selected" ), name );
}

QgsXyzConnection QgsXyzConnectionUtils::connection( const QString &name )
{
  QgsSettings settings;
  settings.beginGroup( "qgis/connections-xyz/" + name );

  QgsXyzConnection conn;
  conn.name = name;
  conn.url = settings.value( QStringLiteral( "url" ) ).toString();
  conn.zMin = settings.value( QStringLiteral( "zmin" ), -1 ).toInt();
  conn.zMax = settings.value( QStringLiteral( "zmax" ), -1 ).toInt();
  conn.authCfg = settings.value( QStringLiteral( "authcfg" ) ).toString();
  conn.username = settings.value( QStringLiteral( "username" ) ).toString();
  conn.password = settings.value( QStringLiteral( "password" ) ).toString();
  conn.referer = settings.value( QStringLiteral( "referer" ) ).toString();
  conn.tilePixelRatio = settings.value( QStringLiteral( "tilePixelRatio" ), 0 ).toDouble();
  conn.hidden = settings.value( QStringLiteral( "hidden" ) ).toBool();
  return conn;
}

void QgsXyzConnectionUtils::deleteConnection( const QString &name )
{
  QgsSettings settings;
  settings.remove( "qgis/connections-xyz/" + name );

  settings.beginGroup( QStringLiteral( "qgis/connections-xyz" ) );
  QStringList global = settings.globalChildGroups();

  if ( global.contains( name ) )
  {
    QgsSettings settings;
    settings.beginGroup( "qgis/connections-xyz/" + name );
    settings.setValue( QStringLiteral( "hidden" ), true );
  }

}

void QgsXyzConnectionUtils::addConnection( const QgsXyzConnection &conn )
{
  QgsSettings settings;
  bool addHiddenProperty = false;

  settings.beginGroup( QStringLiteral( "qgis/connections-xyz" ) );
  QStringList global = settings.globalChildGroups();
  if ( global.contains( conn.name ) )
  {
    addHiddenProperty = true;
  }
  settings.endGroup();

  settings.beginGroup( "qgis/connections-xyz/" + conn.name );
  settings.setValue( QStringLiteral( "url" ), conn.url );
  settings.setValue( QStringLiteral( "zmin" ), conn.zMin );
  settings.setValue( QStringLiteral( "zmax" ), conn.zMax );
  settings.setValue( QStringLiteral( "authcfg" ), conn.authCfg );
  settings.setValue( QStringLiteral( "username" ), conn.username );
  settings.setValue( QStringLiteral( "password" ), conn.password );
  settings.setValue( QStringLiteral( "referer" ), conn.referer );
  settings.setValue( QStringLiteral( "tilePixelRatio" ), conn.tilePixelRatio );
  if ( addHiddenProperty )
  {
    settings.setValue( QStringLiteral( "hidden" ), false );
  }
}

使用:

cpp 复制代码
	QgsXyzConnection xyz;
	xyz.url = QStringLiteral("http://webrd01.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scale=1&style=8");
	xyz.zMin = 0;
	xyz.zMax = 18;
	xyz.tilePixelRatio = 1.0;

	QgsXyzConnectionUtils::addConnection(xyz);

	QString url2 = xyz.encodedUri();
	qDebug() << "url2 = " << url2;

	QgsRasterLayer *layer = addRasterLayerPrivate(url2, QStringLiteral("ocr0"), QStringLiteral("wms"));
相关推荐
徐霞客3205 小时前
Qt入门1——认识Qt的几个常用头文件和常用函数
开发语言·c++·笔记·qt
姆路6 小时前
QT Designer内存飙升
qt
Bruce小鬼8 小时前
QT文件基本操作
开发语言·qt
懷淰メ8 小时前
PyQt飞机大战游戏(附下载地址)
开发语言·python·qt·游戏·pyqt·游戏开发·pyqt5
Mr.Q13 小时前
OpenCV和Qt坐标系不一致问题
qt·opencv
重生之我是数学王子16 小时前
QT基础 编码问题 定时器 事件 绘图事件 keyPressEvent QT5.12.3环境 C++实现
开发语言·c++·qt
----云烟----1 天前
QT中QString类的各种使用
开发语言·qt
「QT(C++)开发工程师」1 天前
【qt版本概述】
开发语言·qt
一路冰雨1 天前
Qt打开文件对话框选择文件之后弹出两次
开发语言·qt
老赵的博客1 天前
QT 自定义界面布局要诀
开发语言·qt