Skip to main content
deleted 22 characters in body
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

I hope this helps.

I hope this helps.

Bounty Ended with 100 reputation awarded by cincin21
added 443 characters in body
Source Link
fastest
  • 1.3k
  • 7
  • 15

I can start PyCharm from a batch file (e.g., pycharm_qgis.bat) that looks like the following (note that the QT_PLUGIN_PATH is slightly different than in your case):

ForFrom the code above, the o4w_env.bat, qt5_env.bat, py3_env.bat are the same batch files available in the QGIS binary folder (i.e., in the case of this example %OSGEO4W_ROOT%\apps\qgis-ltr\bin) without any modification. By executing pycharm_qgis.bat PyCharm starts and the Python interpreter can be set to %OSGEO4W_ROOT%\apps\Python37\python.exe in order to work with PyQGIS library.

Then, for PyCharm to recognize and use the processing module, I needone needs to manually add to the interpreter path the folder %OSGEO4W_ROOT%\apps\qgis-ltr\python\plugins, as shown in the next picture:

Finally, I prepared a smallthe following code excerpt that replicates your example and uses the native:extractvertices algorithm:

QgsApplication.setPrefixPath(r"c:\Program Files\QGIS 3.10\apps\qgis-ltr", True)
qgs = QgsApplication([], False)
qgs.initQgis()
sys.path.append(os.path.join(QgsApplication.prefixPath(), "python", "plugins"))

import processing
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

s_lyr_gpk = os.path.join("Shp", "BOUNDARIES3D.shp")
s_lyr_name = "Boundaries"
lyr = QgsVectorLayer(s_lyr_gpk, s_lyr_name, 'ogr')
if not lyr.isValid():
    print("Layer {} is not valid".format(s_lyr_name))

s_alg = 'native:extractvertices'
out = os.path.join("d:\Data\Output", "extracted_vertices_script.gpkg")

di_param = {
     "INPUT": lyr,
     "OUTPUT": out
}
feedback = QgsProcessingFeedback()
try:
    di_out = processing.run(s_alg, di_param, feedback=feedback)
except QgsProcessingException as e:
     print(e)
     qgs.exitQgis()
     sys.exit(1)

print("Vertices extracted to file {}.".format(di_out["OUTPUT"]))
qgs.exitQgis()
import os
import sys

from qgis.analysis import QgsNativeAlgorithms
from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingException, QgsProcessingFeedback

if __name__ == "__main__":
    QgsApplication.setPrefixPath(r"c:\Program Files\QGIS 3.10\apps\qgis-ltr", True)
    qgs = QgsApplication([], False)
    qgs.initQgis()
    sys.path.append(os.path.join(QgsApplication.prefixPath(), "python", "plugins"))

    import processing
    from processing.core.Processing import Processing
    Processing.initialize()
    QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

    s_lyr_gpk = os.path.join("Shp", "BOUNDARIES3D.shp")
    s_lyr_name = "Boundaries"
    lyr = QgsVectorLayer(s_lyr_gpk, s_lyr_name, 'ogr')
    if not lyr.isValid():
        print("Layer {} is not valid".format(s_lyr_name))

    s_alg = 'native:extractvertices'
    out = os.path.join("d:\Data\Output", "extracted_vertices_script.gpkg")

    di_param = {
         "INPUT": lyr,
         "OUTPUT": out
    }
    feedback = QgsProcessingFeedback()
    try:
        di_out = processing.run(s_alg, di_param, feedback=feedback)
    except QgsProcessingException as e:
        print(e)
        qgs.exitQgis()
        sys.exit(1)

    print("Vertices extracted to file {}.".format(di_out["OUTPUT"]))
    qgs.exitQgis()

I can run thisThe above script can be executed directly from PyCharm andto get the output geopackage file correctly written. In my case, noNo error related to missing QT platform plugin is raised (see image below):

I can start PyCharm from a batch file that looks like the following (note that the QT_PLUGIN_PATH is slightly different than in your case):

For PyCharm to recognize and use the processing module, I need to manually add to the interpreter path the folder %OSGEO4W_ROOT%\apps\qgis-ltr\python\plugins, as shown in the next picture:

Finally, I prepared a small code excerpt that replicates your example and uses the native:extractvertices algorithm:

QgsApplication.setPrefixPath(r"c:\Program Files\QGIS 3.10\apps\qgis-ltr", True)
qgs = QgsApplication([], False)
qgs.initQgis()
sys.path.append(os.path.join(QgsApplication.prefixPath(), "python", "plugins"))

import processing
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

s_lyr_gpk = os.path.join("Shp", "BOUNDARIES3D.shp")
s_lyr_name = "Boundaries"
lyr = QgsVectorLayer(s_lyr_gpk, s_lyr_name, 'ogr')
if not lyr.isValid():
    print("Layer {} is not valid".format(s_lyr_name))

s_alg = 'native:extractvertices'
out = os.path.join("d:\Data\Output", "extracted_vertices_script.gpkg")

di_param = {
     "INPUT": lyr,
     "OUTPUT": out
}
feedback = QgsProcessingFeedback()
try:
    di_out = processing.run(s_alg, di_param, feedback=feedback)
except QgsProcessingException as e:
     print(e)
     qgs.exitQgis()
     sys.exit(1)

print("Vertices extracted to file {}.".format(di_out["OUTPUT"]))
qgs.exitQgis()

I can run this script directly from PyCharm and get the output geopackage file correctly written. In my case, no error related to missing QT platform plugin is raised (see image below):

I can start PyCharm from a batch file (e.g., pycharm_qgis.bat) that looks like the following (note that the QT_PLUGIN_PATH is slightly different than in your case):

From the code above, the o4w_env.bat, qt5_env.bat, py3_env.bat are the same batch files available in the QGIS binary folder (i.e., in the case of this example %OSGEO4W_ROOT%\apps\qgis-ltr\bin) without any modification. By executing pycharm_qgis.bat PyCharm starts and the Python interpreter can be set to %OSGEO4W_ROOT%\apps\Python37\python.exe in order to work with PyQGIS library.

Then, for PyCharm to recognize and use the processing module, one needs to manually add to the interpreter path the folder %OSGEO4W_ROOT%\apps\qgis-ltr\python\plugins, as shown in the next picture:

Finally, the following code excerpt replicates your example and uses the native:extractvertices algorithm:

import os
import sys

from qgis.analysis import QgsNativeAlgorithms
from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingException, QgsProcessingFeedback

if __name__ == "__main__":
    QgsApplication.setPrefixPath(r"c:\Program Files\QGIS 3.10\apps\qgis-ltr", True)
    qgs = QgsApplication([], False)
    qgs.initQgis()
    sys.path.append(os.path.join(QgsApplication.prefixPath(), "python", "plugins"))

    import processing
    from processing.core.Processing import Processing
    Processing.initialize()
    QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

    s_lyr_gpk = os.path.join("Shp", "BOUNDARIES3D.shp")
    s_lyr_name = "Boundaries"
    lyr = QgsVectorLayer(s_lyr_gpk, s_lyr_name, 'ogr')
    if not lyr.isValid():
        print("Layer {} is not valid".format(s_lyr_name))

    s_alg = 'native:extractvertices'
    out = os.path.join("d:\Data\Output", "extracted_vertices_script.gpkg")

    di_param = {
         "INPUT": lyr,
         "OUTPUT": out
    }
    feedback = QgsProcessingFeedback()
    try:
        di_out = processing.run(s_alg, di_param, feedback=feedback)
    except QgsProcessingException as e:
        print(e)
        qgs.exitQgis()
        sys.exit(1)

    print("Vertices extracted to file {}.".format(di_out["OUTPUT"]))
    qgs.exitQgis()

The above script can be executed directly from PyCharm to get the output geopackage written. No error related to missing QT platform plugin is raised (see image below):

Source Link
fastest
  • 1.3k
  • 7
  • 15

I would like to share the solution I found to integrate PyCharm and PyQGIS in an effective way to run standalone scripts:

Setup:

  • Windows 10
  • QGIS 3.10.4 installed in c:\Program Files\QGIS 3.10
  • PyCharm 2020.1.1 installed in c:\Program Files\JetBrains\PyCharm Community Edition 2020.1.1\bin\pycharm64.exe

I can start PyCharm from a batch file that looks like the following (note that the QT_PLUGIN_PATH is slightly different than in your case):

@echo off 
set OSGEO4W_ROOT=c:\PROGRA~1\QGIS3~1.10
set path=%OSGEO4W_ROOT%\bin;%WINDIR%\system32;%WINDIR%;%WINDIR%\system32\WBem

call o4w_env.bat 
call qt5_env.bat
call py3_env.bat

@echo off
path %OSGEO4W_ROOT%\apps\qgis-ltr\bin;%PATH%
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis-ltr
set GDAL_FILENAME_IS_UTF8=YES
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis-ltr\qtplugins;%QT_PLUGIN_PATH%
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis-ltr\python;%PYTHONPATH%

set PYCHARM="c:\Program Files\JetBrains\PyCharm Community Edition 2020.1.1\bin\pycharm64.exe"
@echo on
start "PyCharm with QGIS knowledge!" /B %PYCHARM% %*

For PyCharm to recognize and use the processing module, I need to manually add to the interpreter path the folder %OSGEO4W_ROOT%\apps\qgis-ltr\python\plugins, as shown in the next picture:

Manually add the processing module folder to Python interpreter path

After that, PyCharm can interact with the processing module:

PyCharm recognies the processing module

Finally, I prepared a small code excerpt that replicates your example and uses the native:extractvertices algorithm:

QgsApplication.setPrefixPath(r"c:\Program Files\QGIS 3.10\apps\qgis-ltr", True)
qgs = QgsApplication([], False)
qgs.initQgis()
sys.path.append(os.path.join(QgsApplication.prefixPath(), "python", "plugins"))

import processing
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

s_lyr_gpk = os.path.join("Shp", "BOUNDARIES3D.shp")
s_lyr_name = "Boundaries"
lyr = QgsVectorLayer(s_lyr_gpk, s_lyr_name, 'ogr')
if not lyr.isValid():
    print("Layer {} is not valid".format(s_lyr_name))

s_alg = 'native:extractvertices'
out = os.path.join("d:\Data\Output", "extracted_vertices_script.gpkg")

di_param = {
     "INPUT": lyr,
     "OUTPUT": out
}
feedback = QgsProcessingFeedback()
try:
    di_out = processing.run(s_alg, di_param, feedback=feedback)
except QgsProcessingException as e:
     print(e)
     qgs.exitQgis()
     sys.exit(1)

print("Vertices extracted to file {}.".format(di_out["OUTPUT"]))
qgs.exitQgis()

I can run this script directly from PyCharm and get the output geopackage file correctly written. In my case, no error related to missing QT platform plugin is raised (see image below):

Successful script execution from PyCharm

I hope this helps.