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"));
相关推荐
苏三有春1 小时前
PyQt5实战——UTF-8编码器功能的实现(六)
开发语言·qt
Vanranrr1 小时前
C++ QT
java·c++·qt
兆。2 小时前
掌握 PyQt5:从零开始的桌面应用开发
开发语言·爬虫·python·qt
徒步僧12 小时前
ThingsBoard规则链节点:RPC Call Reply节点详解
qt·microsoft·rpc
可峰科技14 小时前
斗破QT编程入门系列之一:认识Qt:初步使用(四星斗师)
开发语言·qt
我喜欢就喜欢14 小时前
基于qt vs下的视频播放
开发语言·qt·音视频
CP-DD15 小时前
Qt的架构设计
qt
阿_旭15 小时前
基于YOLO11/v10/v8/v5深度学习的维修工具检测识别系统设计与实现【python源码+Pyqt5界面+数据集+训练代码】
人工智能·python·深度学习·qt·ai
Bruce小鬼19 小时前
QT创建按钮篇
开发语言·qt
martian66521 小时前
QT开发:掌握现代UI动画技术:深入解析QML和Qt Quick中的动画效果
开发语言·c++·qt·ui