4

I would like to use Gridsplitter plugin in console. I have the following code for doing it:

from qgis.core import *
import qgis.utils
import imp

gridSplitterPath = "C:/Users/Mossy/.qgis2/python/plugins/gridSplitter/gridSplitter.py"
outputpath = "C:/Users/Mossy/Desktop/NewFolder/Output"
cutlayerpath = "C:/Users/Mossy/Desktop/NewFolder/InputData/cutlayer.shp"
layertocutpath = "C:/Users/Mossy/Desktop/NewFolder/InputData/layertocut.tif"

iface = qgis.utils.iface
module = imp.load_source("gridSplitter",gridSplitterPath)

mySplitter = module.gridSplitter(iface)
mySplitter.outputfolder = outputpath
mySplitter.layertocut = QgsRasterLayer(layertocutpath,"laytocut")
mySplitter.cutlayeris = True
mySplitter.cutlayer = QgsVectorLayer(cutlayerpath,"cutlay", "ogr")
mySplitter.pref = "cut_"
mySplitter.subfolderis = False
mySplitter.tileindexis = True

mySplitter.operate()

However, I get the following error:

Traceback (most recent call last):
File "<input>", line 1, in <module>
File "c:/users/Mossy/appdata/local/temp/tmpqc7_fe.py", line 23, in <module>
mySplitter.operate()
File "C:/Users/Mossy/.qgis2/python/plugins/gridSplitter/gridSplitter.py", line 206, in operate
self.layertocutcrs= layertocut.crs()
AttributeError: 'NoneType' object has no attribute 'crs'

Does anybody know how to solve this problem?

MossyMann
  • 41
  • 3

2 Answers2

4

In the console, the plugins folder is in the PYTHONPATH (no need of import imp)

Simply use (Is there a way to access QGIS plugins in Python?)

import gridSplitter
gridSplitter.__file__
'/Users/my/.qgis2/python/plugins/gridSplitter/__init__.pyc'

The content of the module with see (dir for humans)

see(gridSplitter)
help()                  .classFactory()         .gridSplitter
.gridSplitter_dialog    .resources_rc

The help of the module

help(gridSplitter)
Help on package gridSplitter:
NAME
gridSplitter
FILE
/Users/my/.qgis2/python/plugins/gridSplitter/__init__.py
DESCRIPTION
/***************************************************************************
 gridSplitter
                                 A QGIS plugin
 A plugin that cuts a layer into pieces(tiles)
                             -------------------
        begin                : 2015-03-26
        copyright            : (C) 2015 by Maximilian Krambach
        email                : maximilian.krambach@gmx.de
        git sha              : $Format:%H$
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
 This script initializes the plugin, making it known to QGIS.

PACKAGE CONTENTS
gridSplitter
gridSplitter_dialog
resources_rc
FUNCTIONS
classFactory(iface)
    Load gridSplitter class from file gridSplitter. 
    :param iface: A QGIS interface instance.
    :type iface: QgsInterface

Therefore you can try

gridSplitter.classFactory(iface)
mySplitter = gridSplitter.gridSplitter
gene
  • 54,868
  • 3
  • 110
  • 187
4

Despite the author's intention, I think the plugin won't be easily usable out of QGIS (or even from the QGIS Python Console) as is.

Inside the operate() function, the code is relying on plugin dialogs and on dialog methods like:

self.dlg.OuptDir.text()

In particular, the error you're getting is due to these lines in operate(), which give layertocut a None value if you have not used the plugin's main dialog before:

index = self.dlg.inputRasterBox.currentIndex()
layertocut = self.dlg.inputRasterBox.itemData(index)

Then, accessing crs() is impossible in:

self.layertocutcrs= layertocut.crs()

Making the plugin "Non-GUI friendly" will need a refactor. You can add a feature request in the GitHub repository or, if it's critical to you, program it by yourself or hire the author to do it!


Note that there is a GridSplitter v0.3.6 in GitHub that is still not available in QGIS plugins repository. I've checked that version as well, but it has similar problems, this time turning outputfolder into None.

Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178