3

Whenever I run my join csv script via custom plugin I have to run the plugin reloader for my script to take effect.

How can I make my script run without clicking plugin reloader everytime?

Heres my script:

from PyQt4.QtCore import *
import qgis
from qgis.core import QgsMapLayerRegistry, QgsVectorJoinInfo
import csv

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
  # Change to your shapefile name
  if layer.name() == "Naguilian":
    qgis.utils.iface.setActiveLayer(layer)
    shp = qgis.utils.iface.activeLayer()

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
  # Change to your csv name
  if layer.name() == "naguiliandb":
    qgis.utils.iface.setActiveLayer(layer)
    csv = qgis.utils.iface.activeLayer()

# Set up join parameters
shpField='PIN'
csvField='u_pin'
joinObject = QgsVectorJoinInfo()
joinObject.joinLayerId = csv.id()
joinObject.joinFieldName = csvField
joinObject.targetFieldName = shpField
joinObject.memoryCache = True
shp.addJoin(joinObject)
| improve this question | | | | |
2

I would suggest using the QtDesigner and add a QPushButton to your plugin so that whenever the user clicks on this, the script is run.

Example:

We can add a push button to our plugin interface as shown below by dragging the button from the left panel to the DockWidget. We can then change the Text inside the button by double-clicking on it (e.g. Run) and also the objectName which is used in the code as an identifier (e.g. runScript_button).

QtDesigner

Now in your main.py file, we can define a function which runs your script when a user clicks on this button:

def run(self):
    def run_script():
        from PyQt4.QtCore import *
        import qgis
        from qgis.core import QgsMapLayerRegistry, QgsVectorJoinInfo
        import csv

        for layer in QgsMapLayerRegistry.instance().mapLayers().values():
        # Change to your shapefile name
        if layer.name() == "Naguilian":
        qgis.utils.iface.setActiveLayer(layer)
        shp = qgis.utils.iface.activeLayer()

        for layer in QgsMapLayerRegistry.instance().mapLayers().values():
        # Change to your csv name
        if layer.name() == "naguiliandb":
        qgis.utils.iface.setActiveLayer(layer)
        csv = qgis.utils.iface.activeLayer()

        # Set up join parameters
        shpField='PIN'
        csvField='u_pin'
        joinObject = QgsVectorJoinInfo()
        joinObject.joinLayerId = csv.id()
        joinObject.joinFieldName = csvField
        joinObject.targetFieldName = shpField
        joinObject.memoryCache = True
        shp.addJoin(joinObject)


    # Set up push button to connect to above function
    run_button = self.dlg.runScript_button
    run_button.clicked.connect(run_script)

Make sure to reload your plugin when you make changes to the user interface or code. Now when you click the button, it should run the script without you having to reload it everytime.

| improve this answer | | | | |
  • But I used plugin creator to create my plugin.. Sorry if my question is very basic.. Im really new to this.. :( – Conrado Domingo Oct 6 '16 at 14:05
  • 1
    No need to apologise! When you created your plugin, you were given a choice of template between dialog and dockwidget. Which did you choose? Also, you can just put the code inside the def run(self): function. – Joseph Oct 6 '16 at 14:08
  • I picked tool button with dialog sir Joseph. – Conrado Domingo Oct 6 '16 at 14:18
  • 1
    @ConradoDomingo - Glad you got it working :). Hmm from the QgsVectorJoinInfo, you could set a prefix by using something like joinObject.prefix = ''. Haven't used this so cannot confirm if it will work but the string should be empty otherwise it will use the default layer name. – Joseph Oct 7 '16 at 8:39
  • 1
    thanks! @Joseph really appreciate your help. :) trying to learn from scratch. hehehe. – Conrado Domingo Oct 7 '16 at 9:30
1

If you want to reload plugin with python code, simply do:

from qgis.utils import reloadPlugin

reloadPlugin('Plugin Name')

Somwhere in your code.

| improve this answer | | | | |
  • 1
    +1, good tip but I think the OP wants to avoid loading the plugin each time =) – Joseph Oct 6 '16 at 14:02

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.