I'm using PyCharm with QGIS3 and Python3. I tried this code to import a shape file and the other to connect to PostGIS vector layer. The codes goes right on the python console of QGIS, but in the PcCharm environment it says me "the layer is not valid". I don't understand why. It seems I can't read the address.

First code:

from qgis.core import *

local_path = '/home/nicola/Scrivania/layers/ate_argillla.shp'
#vlayer = QgsVectorLayer(os.path.abspath(local_path), 'insediativo_50000', 'ogr')
vlayer = QgsVectorLayer('/home/nicola/Scrivania/layers/ate_argillla.shp', 'argille', 'ogr')

if not vlayer.isValid():
    print("Il layer non è valido")
    print("Il layer è valido")

Second code:

from qgis.core import *

uri = QgsDataSourceUri()
uri.setConnection("localhost", "5432", "ptcp", "postgres", "aaa")
uri.setDataSource("public", "franelineari", "geom")
vlayer1 = QgsVectorLayer(uri.uri(False), "alvei fluviali", "postgres")
if vlayer1.isValid():
    print("il layer è valido")
    print("layer is not valid")

Sorry, i have made a mistake, i don't have initialized the ogr provider. Everything goes right adding the following code: qgs = QgsApplication.setPrefixPath([b'sys.argv'], True) qgs.initQgis() ...

  • I have solved all the settings problems thank you – nicola frosi Sep 26 '19 at 13:40

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.