1

I'm trying to make a standalone python app and I want to use in some parts of app functions of PyQGIS.

I'm testing things to understand how it works.

Doing tests I have found a situation that I do not understand.

I can load a QgsVectorLayer without QgsApplication.initQgis(), but with QgsRasterLayer this not works.

Can someone explain why this happends? (I know it is recommended to start always with QgsApplication.initQgis().

Here is my test code.

Works QgsVectorLayer but not QgsRasterLayer.

from qgis.core import QgsVectorLayer,QgsRasterLayer

file='MyTrack.gpx'+"|layername=track_points"
vlayer = QgsVectorLayer(file, "Termes", "ogr")
if vlayer.isValid():
    print('Load Correct QgsVectorLayer')
else:
    print('Not Load QgsVectorLayer')

for feature in vlayer.getFeatures():
    print(feature.geometry().asWkt())
    break

uri="dpiMode=7&identifier=mdt:Elevacion4258_25&url=http://www.ign.es/wcs/mdt"

DEM  = QgsRasterLayer(uri, 'my wcs layer', 'wcs')              
if not DEM.isValid():
    print("QgsRasterLayer failed to load! ")
else:
    print("QgsRasterLayer Load Correct!") 

Works QgsVectorLayer and QgsRasterLayer (take about 1 minute to load)

from qgis.core import QgsApplication
qgs = QgsApplication([], False)
qgs.initQgis()

from qgis.core import QgsVectorLayer,QgsRasterLayer

file='MyTrack.gpx'+"|layername=track_points"
vlayer = QgsVectorLayer(file, "Termes", "ogr")
if vlayer.isValid():
    print('Load Correct QgsVectorLayer')
else:
    print('Not Load QgsVectorLayer')

for feature in vlayer.getFeatures():
    print(feature.geometry().asWkt())
    break

uri="dpiMode=7&identifier=mdt:Elevacion4258_25&url=http://www.ign.es/wcs/mdt"

DEM  = QgsRasterLayer(uri, 'my wcs layer', 'wcs')              
if not DEM.isValid():
    print("QgsRasterLayer failed to load! ")
else:
    print("QgsRasterLayer Load Correct!") 

Your Answer

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

Browse other questions tagged or ask your own question.