【发布时间】:2014-10-27 19:27:17
【问题描述】:
在我正在处理的项目(C/C++/Qt 应用程序)中,我们正在尝试集成 QGis(可取的最新版本,目前为 2.4)。但是网上关于如何使用 QGis C++ API 的信息很少。
首先,我想编写一个简单的代码示例(读取 shapefile 并在窗口中可视化它)。我找到了 QGis 1.8 的代码示例,但它不适用于 QGis 2.4,因为此后 API 发生了变化。然后我尝试对其进行编辑以使其与 QGis 2.4 一起使用,但没有成功。这是原始代码:
#include <QtCore/QString>
#include <QtGui/QApplication>
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgssinglesymbolrenderer.h>
#include <qgsmaplayerregistry.h>
#include <qgsvectorlayer.h>
#include <qgsmapcanvas.h>
#include <iostream>
int main(int argc, char** argv)
{
// Creation of the Qt GIS application
QgsApplication app(argc, argv, true);
// Hard coded paths
QString myPluginsDir = "/usr/lib64/qgis";
QString myLayerPath = "./HelloWorld/GUI/data/helloQGIS.shp";
QString myLayerBaseName = "helloQGIS";
QString myProviderName = "ogr";
// Instantiate Provider Registry
QgsProviderRegistry::instance(myPluginsDir);
// Create a maplayer instance
QgsVectorLayer* mypLayer = new QgsVectorLayer(myLayerPath, myLayerBaseName, myProviderName);
QgsSingleSymbolRenderer* mypRenderer = new QgsSingleSymbolRenderer(mypLayer->geometryType());
QList <QgsMapCanvasLayer> myLayerSet;
mypLayer->setRenderer(mypRenderer);
if (mypLayer->isValid())
{
qDebug("Layer is valid");
}
else
{
qDebug("Layer is NOT valid");
}
// Add the Vector Layer to the Layer Registry
QList<QgsMapLayer*> theMapLayers;
theMapLayers.append(mypLayer);
QgsMapLayerRegistry::instance()->addMapLayers(theMapLayers, TRUE);
// Add the Layer to the Layer Set
myLayerSet.append(QgsMapCanvasLayer(mypLayer, TRUE));
// Create the Map Canvas
QgsMapCanvas * mypMapCanvas = new QgsMapCanvas(0, 0);
mypMapCanvas->setExtent(mypLayer->extent());
mypMapCanvas->enableAntiAliasing(true);
mypMapCanvas->setCanvasColor(QColor(255, 255, 255));
mypMapCanvas->freeze(false);
// Set the Map Canvas Layer Set
mypMapCanvas->setLayerSet(myLayerSet);
mypMapCanvas->setVisible(true);
mypMapCanvas->refresh();
mypMapCanvas->show();
// Start the Application Event Loop
return app.exec();
}
我尝试了许多不同的方法来修改此代码以使其与 QGis 2.4 一起使用,但没有成功。我使用的唯一信息来源是official doc API。
我自己说过,也许有人已经这样做了和/或有任何其他使用 QGis 2.4 的代码示例。由于 GIS 对我来说是一个新领域,我很难理解 API 应该如何工作。感谢您的帮助,谢谢。
【问题讨论】: