QGIS secondary development three: display Shapefile

Shapefile is one of the most important data formats supported by OGR and can be loaded by QGIS naturally. So how to display Shapefile?

1. First upload the code

#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgsmapcanvas.h>
#include <qgsvectorlayer.h>

int main(int argc, char **argv)
{
	// 创建 QgsApplication 实例
	QgsApplication app(argc, argv, true);
	// 设置并检查数据插件目录
	QgsProviderRegistry::instance("D:/OSGeo4W/apps/qgis-ltr/plugins");

	// 控制台打印已载入的插件目录
	qDebug() << "QGIS data providers loaded:" << QgsProviderRegistry::instance()->providerList();

	// 设置 GDAL 数据目录环境变量
	qputenv("GDAL_DATA", "D:\\OSGeo4W\\apps\\gdal\\share\\gdal");

	// 创建 QgsCanvas 画布实例
	QgsMapCanvas c;

	// 从磁盘 .shp 文件创建矢量图层
	QgsVectorLayer* pVectorLayer = new QgsVectorLayer(u8"E:\\TestImage\\全国省界\\全国省界.shp", u8"省界");

	// 确认图层是否创建成功
	qDebug() << "Is layer valid:" << pVectorLayer->isValid();

	// 将图层添加到画布上
	c.setLayers(QList<QgsMapLayer*>() << pVectorLayer);

	// 设置画布窗体标题并显示画布
	// 画布本身是 QWidget 的子类,因此可以承担 QWidget 的所有操作
	c.setWindowTitle(u8"QGIS 二次开发:画布");
	c.show();

	// 缩放到图层的空间范围
	c.zoomToFullExtent();

	// 启动 QgsApplication 实例
	return app.exec();
}

2. Rendering

 3. Code explanation

(one)

// 创建 QgsApplication 实例
QgsApplication app(argc, argv, true);

Requires header file < qgsapplication.h >

参见QGIS API Documentation: QgsApplication Class Reference

This class inherits from QApplication and provides access to QGIS-specific resources (such as theme paths, database paths, etc.).

The constructor accepts 5 parameters. 3 of them are mandatory parameters. The first two parameters  main can be passed to the two parameters of the function, and the third parameter indicates whether to activate the graphical interface GUI, which is generally set to  true, unless you want to make a command-line program, such as a program that is purely used for data processing and does not require an interface . Here we set as  true

(two)

// 设置并检查数据插件目录
QgsProviderRegistry::instance("D:/OSGeo4W/apps/qgis-ltr/plugins");

Requires header file < qgsproviderregistry.h >

Official documentation QGIS API Documentation: QgsProviderRegistry Class Reference

The parameters are as follows, pluginPath is the path of the data plugin


QgsProviderRegistry * QgsProviderRegistry::instance(const QString & pluginPath = QString())	

Data-driven plug-ins are a series of dynamic link library files, stored in the plugins directory of the QGIS development kit

// 控制台打印已载入的插件目录
qDebug()<<"QGIS data providers loaded:"<<QgsProviderRegistry::instance()->providerList();

QgsProviderRegistry The member function   called  can return a  string list providerList() storing all supported data-driven plug-ins  , which can be used for debugging.QStringList

参见QGIS API Documentation: QgsProviderRegistry Class Reference

(three) 

// 设置 GDAL 数据目录环境变量
qputenv("GDAL_DATA", "D:\\OSGeo4W\\apps\\gdal\\share\\gdal");

Then the program will check  GDAL_DATA if the environment variable of GDAL is set. The environment variable is a directory that stores resources and data files required by GDAL. The global functions provided by Qt  qputenv() can be used to conveniently set environment variables without modifying the operating system settings, and the environment variables only take effect in the current program.

See the official documentation  https://doc.qt.io/qt-5/qtglobal.html#qputenv

(Four)

// 创建 QgsCanvas 画布实例
QgsMapCanvas c;

Header file < qgsmapcanvas.h >

It is a subclass of Qt used to describe graphical scenes  QGraphicsView and to display GIS-type data.

Official documentation  QGIS API Documentation: QgsMapCanvas Class Reference 

(five)

// 从磁盘 .shp 文件创建矢量图层
QgsVectorLayer* pVectorLayer = new QgsVectorLayer(u8"E:\\TestImage\\全国省界\\全国省界.shp", u8"省界");

 Header file < qgsvectorlayer.h >

QgsVectorLayer instance created  . This class draws a vector layer in QGIS and is  QgsMapLayer a subclass of QGIS.

The inheritance relationship is as follows

Inheritance graph

The constructor prototype is

QgsVectorLayer::QgsVectorLayer	(
    const QString & path = QString(),
    const QString & baseName = QString(),
    const QString & providerLib = "ogr",
    const QgsVectorLayer::LayerOptions & options = QgsVectorLayer::LayerOptions() 
)	

 path is the path or url of the parameter, baseName is the layer name displayed in the legend,

参见QGIS API Documentation: QgsVectorLayer Class Reference

After the layer is created, you can call  isValid() the method to check whether it is created successfully.

// 确认图层是否创建成功
qDebug() << "Is layer valid:" << pVectorLayer->isValid();

The method is located atQgsVectorLayer的父类QgsMapLayer中,返回图层的状态,创建成功则返回True,否则返回False。如果返回False,则可以通过QgsMapLayer的error()方法进步查看错误详情。

(six)

// 将图层添加到画布上
c.setLayers(QList<QgsMapLayer*>() << pVectorLayer);

After the layer is successfully created, add it to the canvas. QgsMapCanvas Just call the method of  the canvas class  setLayers() , which will set a list of layers to be displayed on the canvas.

The function prototype is as follows, see  QGIS API Documentation: QgsMapCanvas Class Reference

void QgsMapCanvas::setLayers(const QList< QgsMapLayer * > & layers)	

This method accepts a  QgsMapLayer list of layer base class pointers. In general, the higher the layer is in the list (that is, the smaller the subscript), the lower the drawing order (that is, the closer to the viewer).

(seven)

// 设置画布窗体标题并显示画布
// 画布本身是 QWidget 的子类,因此可以承担 QWidget 的所有操作
c.setWindowTitle(u8"QGIS 二次开发:画布");
c.show();

In Qt, as long as it  QWidget and its subclasses can be treated as a separate program form. QgsMapCanvas As  QGraphicsView a subclass of QWidget, of course, it is also a subclass of QWidget. We can use QWidget's public slot function setWindowTitle() to set the window title for it, and then use show() to display it like a normal window.

(eight)

// 缩放到图层的空间范围
c.zoomToFullExtent();

Call zoomToFullExtent() of QgsMapCanvas, this step will make QgsMapCanvas zoom to the full range of all internal visible layers, that is, to make all visible layers fully displayed.

See  QGIS API Documentation: QgsMapCanvas Class Reference

(Nine)

// 启动 QgsApplication 实例
return app.exec();

 Finally, start QgsApplication (that is, QApplication). exec() The method is located in the parent class QApplication of QgsApplication, and the purpose is to let the program enter the main loop. After the main loop is interrupted (eg all windows are closed), the program ends.

See official documentation  QApplication Class | Qt Widgets 5.15.14

Reference article  article page | mriiiron's blog 

Guess you like

Origin blog.csdn.net/KK_2018/article/details/132168944