在我正在研究的项目(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应该如何运作。感谢您的帮助,谢谢。