ccrook / QGIS-Contour-Plugin

Contour plugin for QGIS - uses python matplotlib to generate contours of data on vector point data.
43 stars 12 forks source link

Contour Plugin Processing Algorithm not detected in Standalone Script #30

Closed ftharyanto closed 6 years ago

ftharyanto commented 6 years ago

I followed this script. I was successfully imported the processing algorithms but when I list the algorithms using print(QgsApplication.processingRegistry().algorithms()), contour plugin is not listed there (but it listed I tried it in QGIS Python Console) . Is it because it an experimental plugin?

This is what my code looks:

sys.path.append('/usr/share/qgis/python/plugins')
import processing
from processing.core.Processing import Processing
Processing.initialize()

print(QgsApplication.processingRegistry().algorithms())

I use QGIS 3 on Ubuntu 18

ccrook commented 6 years ago

I think the reason is that you haven't registered the plugin processing algorithm. I think the code would be something like

sys.path.append( ... )
from ContourGeneratorProvider import ContourGeneratorProvider
provider=ContourGeneratorProvider()
QgsApplication.processingRegistry().addProvider(self.provider)
ftharyanto commented 6 years ago

So the code like this?

sys.path.append('/usr/share/qgis/python/plugins')
from ContourGeneratorProvider import ContourGeneratorProvider
provider=ContourGeneratorProvider()
QgsApplication.processingRegistry().addProvider(self.provider)

Still get error: ModuleNotFoundError: No module named 'ContourGeneratorProvider'

Is it possible I appended a wrong path? because when I searched the contour plugin, in /usr/share/qgis/python/plugins I couldn't find it. Instead the contour plugin is found in /home/fajar/.local/share/QGIS/QGIS3/profiles/default/python/plugins/contour. I've tried to append it and get this error: ImportError: attempted relative import with no known parent package

ccrook commented 6 years ago

@gaaraxpo You could try

sys.path.append('/home/fajar/.local/share/QGIS/QGIS3/profiles/default/python/plugins')
from contour.ContourGeneratorProvider import ContourGeneratorProvider

Also I see I have an error in the addProvider line, for self.provider read just provider.

However I suggest it may be more useful to firstly make sure you post the entire code being run and the full error output, as that usually identifies the exact line where this exception is being raised, and secondly to post it to the QGIS user or developer mailing lists where you will get more eyes looking at it.

The reason for the second suggestion is that I think that this is not specific to this plugin, but more a general question about how to use a plugin processing script from a standalone python script.

ftharyanto commented 6 years ago

Thank you for your suggestion, I just posted a question on gis stackexchange. This is the entire code:

from qgis import core, gui
from qgis.PyQt import QtWidgets, QtCore, QtGui, uic
import sys, os

# # Append the path where processing plugin can be found
sys.path.append('/home/fajar/.local/share/QGIS/QGIS3/profiles/default/python/plugins/contour')
from ContourGeneratorAlgorithm.ContourGeneratorAlgorithm import ContourGeneratorProvider
provider=ContourGeneratorProvider()
QgsApplication.processingRegistry().addProvider(provider)

qgis_prefix = '/usr'

qtCreatorFile = "gui.ui" # UI file.
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

class mainWindow(QtWidgets.QMainWindow, Ui_MainWindow):

    def __init__(self):

        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)

        # Required by Qt5 to initialize the UI
        self.setupUi(self)

        # Set the title for the app
        self.setWindowTitle("Mapper")

        # Create the map canvas
        self.canvas = gui.QgsMapCanvas()
        self.canvas.show()

        # Lay our widgets out in the main window using a 
        # vertical box layout
        self.layout = QtWidgets.QVBoxLayout(self.frame)
        self.layout.addWidget(self.canvas)

def main(argv):
    # create Qt application
    app = QtWidgets.QApplication(argv)

    # Initialize qgis libraries
    core.QgsApplication.setPrefixPath(qgis_prefix, True)
    core.QgsApplication.initQgis()

    # create main window
    wnd = mainWindow()

    # Move the app window to upper left
    wnd.move(100,100)
    wnd.show()

    # run!
    retval = app.exec_()

    # exit
    core.QgsApplication.exitQgis()
    sys.exit(retval)

if __name__ == "__main__":
    main(sys.argv)

and this is the complete error output:

Traceback (most recent call last):
  File "/media/fajar/DATA/[GEOFISIKA]/[Skripsi]/Skripsi Fajar Tri Haryanto/DAPUR/GUI/NewDesign/try.py", line 7, in <module>
    from ContourGeneratorAlgorithm.ContourGeneratorAlgorithm import ContourGeneratorProvider
  File "/home/fajar/.local/share/QGIS/QGIS3/profiles/default/python/plugins/contour/ContourGeneratorAlgorithm.py", line 47, in <module>
    from .ContourGenerator import ContourGenerator, ContourType, ContourExtendOption
ImportError: attempted relative import with no known parent package
ccrook commented 6 years ago

@gaaraxpo ... try this :-)

import sys, os
from qgis import core, gui
from qgis.core import QgsApplication
from qgis.PyQt import QtWidgets, QtCore, QtGui, uic
import sys, os

# # Append the path where processing plugin can be found
sys.path.append('/home/fajar/.local/share/QGIS/QGIS3/profiles/default/python/plugins')
from contour.ContourGeneratorProvider import ContourGeneratorProvider
provider=ContourGeneratorProvider()
QgsApplication.processingRegistry().addProvider(provider)
ftharyanto commented 6 years ago

Alright this is working, thank you! :D I was unable to import it because there was a folder named "contour" in the same folder as my script which contained the old contour plugin in QGIS 2.x version. Maybe Python prioritize importing in the same directory over recently appended path.

ftharyanto commented 6 years ago

I know this is already closed, but I want to ask one more: processing.run() doesn't seem to work. How to make it work?

ccrook commented 6 years ago

@gaaraxpo I can't see processing.run() in your code! Also again a complete error message would be useful. I suggest writing a script without the GUI to do what you want, and try and get that going. Then add the GUI stuff once you have the basic code going.

ftharyanto commented 6 years ago

thank you :D