0

I can't seem to figure out why my layer won't charge in QGIS.

After reading many topics, it seems to be linked to the prefix path. However changing this, does not seem to work (I tried around 10 different paths in my quest).

import os
    os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = r'C:/OSGeo4W64/apps/Qt5/plugins'
    os.environ['PATH'] += r';C:/OSGeo4W64/apps/qgis/bin;~qgis directory/apps/Qt5/bin'

import sys
sys.path.extend(['C:/OSGeo4W64/apps/qgis-ltr/python', 
'C:/OSGeo4W64/apps/Python37/Lib/site-packages', 'C:/OSGeo4W64/apps/qgis/python','C:/OSGeo4W64/apps/Python37/Lib/site-packages'
])

from qgis.core import QgsApplication, QgsProject, QgsVectorLayer
from PyQt5.QtCore import QFileInfo

QgsApplication.setPrefixPath('C:\\OSGeo4W\\apps\\qgis\\', True)
qgs = QgsApplication([], False)
qgs.initQgis()

pathQGS = r'C:\Users\Me\Jordan\jordan.qgs'
layerPath = r'C:\Users\Me\Jordan\Data\gis_osm_waterways_free_1.shp'
fileInfo = QFileInfo(layerPath)
path = fileInfo.filePath()
baseName = fileInfo.baseName()

project = QgsProject.instance()
if os.path.isfile(pathQGS):
    os.remove(pathQGS)
project.setFileName(pathQGS)
layer = QgsVectorLayer(path, baseName, 'ogr')
if not layer.isValid():
    print("Layer is not valid")
project.addMapLayer(layer)
project.write()

qgs.exitQgis()

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.