0

Through QGIS API, I would like to add some tens of vector layers to QGIS, where each layer would be a single shapefile from a folder. I would then like to print (to Python console) the coordinates of all the point features in those shapefiles.

Can that be achieved with QGIS API, but outside of QGIS (through a Python editor like PyCharm/Atom for example)?

I found an API snippet how that can be achieved, but only from inside the QGIS. Is it possible to do it from PyCharm/Atom too?

I am struggling to adapt the upper code. For example, I can't even import qgis.core module:

# coding=utf-8
import os, sys

# append QGIS Python library to python search path
sys.path.append(R"C:\Program Files\QGIS 3.10\apps\qgis\python")

# append location of DLLs to current system PATH envrionment variable
os.environ['PATH'] += R";C:\Program Files\QGIS 3.10\apps\qgis\bin;"

# examine new PATH environment variable
print (os.environ['PATH'])


import qgis
import PyQt5.QtCore

app = qgis.QtCore.QCoreApplication([]) # is this 'QGIS app'?
import qgis.core  # raises an error: 'from qgis._core import *   ImportError: DLL load failed: The specified procedure could not be found.'
3
  • 1
    That's a lot of questions in one, please try to focus this post on exactly one and rather post additional ones. For pycharm the setup is easy: gis.stackexchange.com/a/428577/51035 Commented Dec 28, 2022 at 23:19
  • Thank you @bugmenot123. So If I pass beyond just importing qgis.core into PyCharm, then would you say there is some difference (in terms of API syntax) between calling the QGIS API from PyCharm, and directly from QGIS Python editor?
    – marco
    Commented Dec 28, 2022 at 23:34
  • 1
    Zero difference, except for imports where the QGIS Python editor might automatically import all QGIS classes at start. Commented Dec 28, 2022 at 23:40

1 Answer 1

1

This snippet works for me in the OSGEO4W shell outside QGIS:

# coding=utf-8
import os, sys

sys.path.append(R"C:\OSGeo4W\apps\qgis\python")

os.environ['PATH'] += R";C:\OSGeo4W\apps\qgis\bin\;"

from qgis.core import *
from PyQt5.QtWidgets  import QApplication

app = QApplication([])
QgsApplication.setPrefixPath("C:\\OSGeo4W\\apps\\qgis\\", True) 
QgsApplication.initQgis()

layer = QgsVectorLayer("C:\\my_folder\\my_points.shp", "testlayer", "ogr")

if not layer.isValid():
  print ("Layer failed to load!")
else:
    for feat in layer.getFeatures():
        geom = feat.geometry()
        print(geom)

app.quit()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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