0

I'm new with QGIS. After lots of trying I finally executed a successful compile on QGIS libs and used them in my program. In my simple program I just read some layers (vector and raster) and showed them in a QGIS canvas. This is my code and I use qgis-dev 3.13 libraries.

 // QGIS Includes
#include "QApplication"
#include "qgsapplication.h"
#include "qgsproviderregistry.h"
#include "qgssinglesymbolrenderer.h"
#include "qgsvectorlayer.h"
#include "qgsrasterlayer.h"
#include "qgsmapcanvas.h"
#include "qgsproject.h"
#include "qgsrasterrenderer.h"
#include "qgssinglebandpseudocolorrenderer.h"
#include "qgscolorrampshader.h"
#include "qgsrastershader.h"
#include "qgshillshaderenderer.h"
#include "qgsmaplayerstyle.h"
#include "qgsrastertransparency.h"
#include "iostream"
using namespace std;

int main(int argc, char ** argv)
{

    // Start the Application
    QApplication a(argc,argv);
    QgsApplication app(argc, argv, true);

   QgsApplication::setPluginPath("C:/OSGeo4W64/apps/qgis-dev/lib");
    QgsApplication::setPrefixPath("C:/OSGeo4W64/apps/qgis-dev");
    QApplication::addLibraryPath("C:/OSGeo4W64/apps/qgis-dev/lib");
    QgsProviderRegistry::instance("C:/OSGeo4W64/apps/gdal-dev/bin/gdalplugins");

    QgsApplication::initQgis();


   // QgsVectorLayer *vectLayer = new QgsVectorLayer("E:/QGISProject/Shape_Files/iran-administrative/iran_administrative.shp","vectLayer","ogr");
    QgsRasterLayer *rastLayer = new QgsRasterLayer("C:/Users/Test/Desktop/build-testQGISLib-Desktop_Qt_5_12_1_MSVC2015_64bit-Release/release/j37g41.tif","rastLayer");
   // QgsRasterLayer *rast3dLayer = new QgsRasterLayer("C:/Users/Test/Desktop/build-testQGISLib-Desktop_Qt_5_12_1_MSVC2015_64bit-Release/release/j37g41.tif");
    QgsRasterLayer *ecwLayer = new QgsRasterLayer("C:/Users/Test/Downloads/Compressed/I33a1_Ecw/I33a1.ecw");

    QgsMapCanvas *mapcanvas = new QgsMapCanvas();
    QList<QgsMapLayer*> layers;


    //rast3dLayer = rastLayer;

    /*if(vectLayer->isValid())
        cout<<"it's ok"<<endl;
    else {
        cout<<"it is not ok"<<endl;   }


    vectLayer->updateExtents();*/



      QgsRasterShader *rasterShader = new QgsRasterShader();
      QgsColorRampShader *colorRampShader = new QgsColorRampShader();
      colorRampShader->setColorRampType( QgsColorRampShader::Interpolated );

      //items to imitate old pseudo color renderer
      QList<QgsColorRampShader::ColorRampItem> colorRampItems;
      QgsColorRampShader::ColorRampItem firstItem;
      firstItem.value = 0.0;
      firstItem.color = QColor( 0, 0, 255 );
      colorRampItems.append( firstItem );
      QgsColorRampShader::ColorRampItem secondItem;
      secondItem.value = 1500.0;
      secondItem.color = QColor( 0, 255, 255 );
      colorRampItems.append( secondItem );
      QgsColorRampShader::ColorRampItem thirdItem;
      thirdItem.value = 3000.0;
      thirdItem.color = QColor( 255, 255, 0 );
      colorRampItems.append( thirdItem );
      QgsColorRampShader::ColorRampItem fourthItem;
      fourthItem.value = 4500;
      fourthItem.color = QColor( 255, 0, 0 );
      colorRampItems.append( fourthItem );
      colorRampShader->setColorRampItemList( colorRampItems );
      rasterShader->setRasterShaderFunction( colorRampShader );


      QgsSingleBandPseudoColorRenderer *rasterRenderer = new QgsSingleBandPseudoColorRenderer( rastLayer->dataProvider(), 1, rasterShader );
      //QgsHillshadeRenderer *renderer3d = new QgsHillshadeRenderer(rast3dLayer->dataProvider(), 1, 274.0, 45.0);

     rastLayer->dataProvider()->setNoDataValue( 1, 126 );


    QgsProject::instance()->addMapLayer(rastLayer);
    //QgsProject::instance()->addMapLayer(rast3dLayer);
    //renderer3d->setOpacity(.5);
    rasterRenderer->setOpacity(1.0);
    rastLayer->setRenderer( rasterRenderer );

    //layers.append(rast3dLayer);
    layers.append(ecwLayer);
    //layers.append(vectLayer);
    layers.append(rastLayer);




    mapcanvas->setExtent(rastLayer->extent());
    mapcanvas->setLayers(layers);
    mapcanvas->zoomToFullExtent();
    //mapcanvas->setCanvasColor(QColor::Rgb);
    mapcanvas->setCanvasColor("white");
    cout<<"rend is "<< mapcanvas->layer(0)->Rendering<<endl;

    QStringList test = QgsApplication::deferredSkippedGdalDrivers();
    QString settings =  QgsApplication::showSettings();
    qDebug()<<"this is just settings : "<<settings;


    cout<<"count of test items"<<test.count()<<endl;
    for(int i=0;i<test.count();i++){
        cout<<"entered to loop"<<endl;
        cout<<"this is the list of skipped"<<test[i].toStdString()<<endl;
    }

    //mapcanvas->layer(0)->setAutoRefreshEnabled(true);
    //mapcanvas->layer(1)->setAutoRefreshEnabled(true);
    //mapcanvas->setMapUpdateInterval(500);
    //rast3dLayer->setRenderer(renderer3d);


    mapcanvas->enableAntiAliasing(true);
    mapcanvas->refresh();
//mapcanvas->setParallelRenderingEnabled(true);
    //rastLayer->renderer()->set
    mapcanvas->setCachingEnabled(true);
    mapcanvas->show();
    //mapcanvas->setCurrentLayer(rastLayer);
//mapcanvas->redrawAllLayers();
    //rastLayer->triggerRepaint(true);
    //rast3dLayer->triggerRepaint(true);

//mapcanvas->layer(0)->setSubLayerVisibility("rastLayer",false);
//mapcanvas->layer(2)->
//mapcanvas->refresh();



    cout<<"the count of layers is : " << mapcanvas->layerCount()<<endl;


    int retval = app.exec();

    app.exitQgis();


    return retval;
}

Everything is ok except the loading of ECW formats in QGIS. I read some sources to how add ECW format to QGIS but all of them are just about the QGIS software not about QGIS C++ API. I can see ECW files in my qgis-dev software but in my program I couldn't do that. When I run my program in Qt see this logs in application output :

12:55:15: The program has unexpectedly finished.
12:55:15: The process was ended forcefully.
12:55:15: C:/Users/Test/Desktop/build-testQGISLib-Desktop_Qt_5_12_1_MSVC2015_64bit-Debug/debug/testQGISLib.exe crashed.

12:55:15: Starting C:\Users\Test\Desktop\build-testQGISLib-Desktop_Qt_5_12_1_MSVC2015_64bit-Debug\debug\testQGISLib.exe...
..\..\..\src\core\qgsapplication.cpp(160) : (QgsApplication::QgsApplication) [0ms] loading of qgis translation failed C:/Users/Test/Desktop/build-testQGISLib-Desktop_Qt_5_12_1_MSVC2015_64bit-Debug/debug/./i18n//qgis_
..\..\..\src\core\qgsapplication.cpp(175) : (QgsApplication::QgsApplication) [0ms] loading of qt translation failed C:/Qt/Qt5.12.1/5.12.1/msvc2015_64/translations/qt_
..\..\..\src\core\qgsproviderregistry.cpp(133) : (QgsProviderRegistry::init) [36ms] Checking C:/OSGeo4W64/apps/gdal-dev/bin/gdalplugins for provider plugins
..\..\..\src\core\qgsproviderregistry.cpp(193) : (QgsProviderRegistry::init) [8ms] Checking C:/OSGeo4W64/apps/gdal-dev/bin/gdalplugins/gdal_ECW_JP2ECW.dll: ...invalid (no providerMetadataFactory method)
..\..\..\src\core\qgsproviderregistry.cpp(215) : (QgsProviderRegistry::init) [0ms] Loaded 4 providers (gdal;memory;mesh_memory;ogr) 
..\..\..\src\core\qgsapplication.cpp(1686) : (QgsApplication::applyGdalSkippedDrivers) [0ms] Gdal Skipped driver list set to:
..\..\..\src\core\qgsapplication.cpp(1687) : (QgsApplication::applyGdalSkippedDrivers) [0ms] 
..\..\..\src\core\providers\ogr\qgsogrprovider.cpp(3287) : (createFilters) [6ms] Unknown driver VICAR for file filters.
..\..\..\src\core\providers\ogr\qgsogrprovider.cpp(3287) : (createFilters) [0ms] Unknown driver EEDA for file filters.

rend is 1024
count of test items0
the count of layers is : 2

Here is my pro file :

   #-------------------------------------------------
#
# Project created by QtCreator 2020-02-13T15:54:14
#
#-------------------------------------------------

QT += core xml gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = testQGISLib
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

INCLUDEPATH +=  "C:/OSGeo4W64/include"
DEPENDPATH += "C:/OSGeo4W64/include"
INCLUDEPATH +=  "C:/OSGeo4W64/apps/qgis-dev/include"
DEPENDPATH += "C:/OSGeo4W64/apps/qgis-dev/include"
LIBS += -L"C:/OSGeo4W64/apps/qgis-dev/lib" -lqgis_core -lqgis_gui

#LIBS += -L"C:/Users/Test/AppData/Local/Hexagon/ERDAS ECW JPEG 2000 SDK 5.5.0/Desktop Read-Only/lib/vc140/x64" -lNCSEcw
#INCLUDEPATH += "C:/Users/Test/AppData/Local/Hexagon/ERDAS ECW JPEG 2000 SDK 5.5.0/Desktop Read-Only/include"


#LIBS += -L"C:/OSGeo4W64/apps/gdal-dev/lib" -lgdal_i
#INCLUDEPATH +=  "C:/OSGeo4W64/apps/gdal-dev/include"



INCLUDEPATH += "C:/Qt/Qt5.12.1/5.12.1/msvc2015_64/include"
DEPENDPATH += "C:/Qt/Qt5.12.1/5.12.1/msvc2015_64/include"
#INCLUDEPATH += "C:/Qt/Qt5.12.1/5.12.1/msvc2015_64/bin"
#INCLUDEPATH += "C:/OSGeo4W64/bin"
#INCLUDEPATH += "C:/OSGeo4W64/apps/qgis-dev"
#INCLUDEPATH += "C:/OSGeo4W64/apps/qgis-dev/bin"
#DEPENDPATH += "C:/OSGeo4W64/apps/qgis-dev"
#DEPENDPATH += "C:/OSGeo4W64/apps/qgis-dev/bin"
LIBS += -L"C:/Qt/Qt5.12.1/5.12.1/msvc2015_64/lib" -lQt5Core -lQt5Gui -lQt5Xml
LIBS += -L"C:/Qt/Qt5.12.1/5.12.1/msvc2015_64/lib" -lQt5Cored -lQt5Guid -lQt5Xmld



DEFINES+=CORE_EXPORT=
DEFINES+=GUI_EXPORT=
  • QGIS-DEV 3.13
  • QT Creator 4.8.1
  • OS : windows 10
  • Compiler : msvc2015

What should I do?

1

it seems that part of my problem is solved. the problem was that i forgot to specify GDAL_PATH_DRIVER for the program as in QGIS Desktop. i just have a look to *.env files in OSGEO_ROOT (for me is in c drive) and use exactly addresses that is used there and start qtCreator by this environmental variables and bomb... ecw files executed rightly. the addresses for me like this :

set OSGEO4W_ROOT=C:\OSGeo4W64

SET PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37
SET PYTHONPATH=%PYTHONHOME%;%PYTHONHOME%\Scripts
PATH %PYTHONPATH%;%PATH%

SET PROJ_LIB=%OSGEO4W_ROOT%\apps\proj-dev\share\proj
PATH %OSGEO4W_ROOT%\apps\proj-dev\bin;%PATH%

SET GDAL_DATA=%OSGEO4W_ROOT%\apps\gdal-dev\share\gdal
SET GDAL_DRIVER_PATH=%OSGEO4W_ROOT%\apps\gdal-dev\bin\gdalplugins
PATH %OSGEO4W_ROOT%\apps\gdal-dev\bin;%PATH%

path %OSGEO4W_ROOT%\apps\qgis-dev\bin;%PATH%
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis-dev
set GDAL_FILENAME_IS_UTF8=YES
rem Set VSI cache to be used as buffer, see #6448
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000

start "qtcreator" /B C:\Qt\Qt5.12.1\Tools\QtCreator\bin\qtcreator.exe %*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.