Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] refactoring to move common functions to tools package
  • Loading branch information
volaya committed Sep 14, 2013
1 parent 4d9832c commit 807ffaa
Show file tree
Hide file tree
Showing 154 changed files with 1,177 additions and 1,345 deletions.
43 changes: 20 additions & 23 deletions python/plugins/processing/ProcessingPlugin.py
Expand Up @@ -16,6 +16,7 @@
* *
***************************************************************************
"""
from processing import interface

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
Expand All @@ -24,44 +25,40 @@
__revision__ = '$Format:%H$'

import shutil
import os, sys
import inspect
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from PyQt4 import QtGui
from processing.commander.CommanderWindow import CommanderWindow
from processing.core.Processing import Processing
from processing.core.QGisLayers import QGisLayers
from processing.core.ProcessingUtils import ProcessingUtils
from processing.tools import dataobjects
from processing.tools.system import *
from processing.gui.ProcessingToolbox import ProcessingToolbox
from processing.gui.HistoryDialog import HistoryDialog
from processing.gui.ConfigDialog import ConfigDialog
from processing.gui.ResultsDialog import ResultsDialog
from processing.modeler.ModelerDialog import ModelerDialog
import processing.resources_rc


cmd_folder = os.path.split(inspect.getfile( inspect.currentframe() ))[0]
if cmd_folder not in sys.path:
sys.path.insert(0, cmd_folder)

class ProcessingPlugin:

def __init__(self, iface):
self.iface = iface
QGisLayers.setInterface(iface)
Processing.initialize()
Processing.setInterface(iface)
Processing.setPlugin(self)
interface.iface = iface
Processing.initialize()

def initGui(self):
self.commander = None
self.toolbox = ProcessingToolbox(self.iface)
self.iface.addDockWidget(Qt.RightDockWidgetArea, self.toolbox)
self.toolbox = ProcessingToolbox()
interface.iface.addDockWidget(Qt.RightDockWidgetArea, self.toolbox)
self.toolbox.hide()
Processing.addAlgListListener(self.toolbox)

self.menu = QMenu(self.iface.mainWindow())
self.menu = QMenu(interface.iface.mainWindow())
self.menu.setTitle(QCoreApplication.translate("Processing", "Processing"))

self.toolboxAction = self.toolbox.toggleViewAction()
Expand All @@ -71,51 +68,51 @@ def initGui(self):

self.modelerAction = QAction(QIcon(":/processing/images/model.png"),
QCoreApplication.translate("Processing", "Graphical modeler"),
self.iface.mainWindow())
interface.iface.mainWindow())
self.modelerAction.triggered.connect(self.openModeler)
self.menu.addAction(self.modelerAction)

self.historyAction = QAction(QIcon(":/processing/images/history.gif"),
QCoreApplication.translate("Processing", "History and log"),
self.iface.mainWindow())
interface.iface.mainWindow())
self.historyAction.triggered.connect(self.openHistory)
self.menu.addAction(self.historyAction)

self.configAction = QAction(QIcon(":/processing/images/config.png"),
QCoreApplication.translate("Processing", "Options and configuration"),
self.iface.mainWindow())
interface.iface.mainWindow())
self.configAction.triggered.connect(self.openConfig)
self.menu.addAction(self.configAction)

self.resultsAction = QAction(QIcon(":/processing/images/results.png"),
QCoreApplication.translate("Processing", "&Results viewer"),
self.iface.mainWindow())
interface.iface.mainWindow())
self.resultsAction.triggered.connect(self.openResults)
self.menu.addAction(self.resultsAction)

menuBar = self.iface.mainWindow().menuBar()
menuBar.insertMenu(self.iface.firstRightStandardMenu().menuAction(), self.menu)
menuBar = interface.iface.mainWindow().menuBar()
menuBar.insertMenu(interface.iface.firstRightStandardMenu().menuAction(), self.menu)

self.commanderAction = QAction(QIcon(":/processing/images/commander.png"),
QCoreApplication.translate("Processing", "&Commander"),
self.iface.mainWindow())
interface.iface.mainWindow())
self.commanderAction.triggered.connect(self.openCommander)
self.menu.addAction(self.commanderAction)
self.iface.registerMainWindowAction(self.commanderAction, "Ctrl+Alt+M")
interface.iface.registerMainWindowAction(self.commanderAction, "Ctrl+Alt+M")

def unload(self):
self.toolbox.setVisible(False)
self.menu.deleteLater()
#delete temporary output files
folder = ProcessingUtils.tempFolder()
folder = tempFolder()
if QDir(folder).exists():
shutil.rmtree(folder, True)

self.iface.unregisterMainWindowAction(self.commanderAction)
interface.iface.unregisterMainWindowAction(self.commanderAction)

def openCommander(self):
if self.commander is None:
self.commander = CommanderWindow(self.iface.mainWindow(), self.iface.mapCanvas())
self.commander = CommanderWindow(interface.iface.mainWindow(), interface.iface.mapCanvas())
Processing.addAlgListListener(self.commander)
self.commander.prepareGui()
self.commander.show()
Expand Down
12 changes: 7 additions & 5 deletions python/plugins/processing/__init__.py
Expand Up @@ -17,17 +17,19 @@
***************************************************************************
"""

from processing.tools.general import runalg, runandload, alghelp, alglist, algoptions, load, extent, getobject
from processing.tools.vector import getfeatures, spatialindex, values, uniquevalues
from processing.tools.raster import scanraster
from processing.tests.TestData import loadTestData

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from processing.tools.dataobjects import *
from processing.tools.general import *
from processing.tools.vector import *
from processing.tools.raster import *
from processing.tools.system import *
from processing.tests.TestData import loadTestData

def classFactory(iface):
from processing.ProcessingPlugin import ProcessingPlugin
return ProcessingPlugin(iface)
4 changes: 2 additions & 2 deletions python/plugins/processing/admintools/CreateMosaicDatastore.py
Expand Up @@ -17,7 +17,7 @@
# * *
# ***************************************************************************
# """
# from processing.core.LayerExporter import LayerExporter
# from processing.tools import dataobjects
# from processing.parameters.ParameterString import ParameterString
# from processing.servertools.GeoServerToolsAlgorithm import GeoServerToolsAlgorithm
#
Expand All @@ -29,7 +29,7 @@
#
# from qgis.core import *
# from processing.parameters.ParameterVector import ParameterVector
# from processing.core.QGisLayers import QGisLayers
# from processing.tools import dataobjects
# import os
#
# class CreateMosaicDatastore(GeoServerToolsAlgorithm):
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/admintools/ImportIntoPostGIS.py
Expand Up @@ -16,7 +16,7 @@
* *
***************************************************************************
"""
from processing.core.QGisLayers import QGisLayers
from processing.tools import dataobjects
from processing.parameters.ParameterBoolean import ParameterBoolean


Expand Down Expand Up @@ -80,7 +80,7 @@ def processAlgorithm(self, progress):
options['overwrite'] = True

layerUri = self.getParameterValue(self.INPUT);
layer = QGisLayers.getObjectFromUri(layerUri)
layer = dataobjects.getObjectFromUri(layerUri)
ret, errMsg = QgsVectorLayerImport.importLayer(layer, uri.uri(), providerName, self.crs, False, False, options)
if ret != 0:
raise GeoAlgorithmExecutionException(u"Error importing to PostGIS\n%s" % errMsg)
Expand Down
Expand Up @@ -26,8 +26,8 @@
import os
from qgis.core import *
from processing.parameters.ParameterVector import ParameterVector
from processing.core.QGisLayers import QGisLayers
from processing.core.LayerExporter import LayerExporter
from processing.tools import dataobjects
from processing.tools import dataobjects
from processing.parameters.ParameterString import ParameterString
from processing.admintools.GeoServerToolsAlgorithm import GeoServerToolsAlgorithm

Expand All @@ -39,9 +39,9 @@ class ImportVectorIntoGeoServer(GeoServerToolsAlgorithm):
def processAlgorithm(self, progress):
self.createCatalog()
inputFilename = self.getParameterValue(self.INPUT)
layer = QGisLayers.getObjectFromUri(inputFilename)
layer = dataobjects.getObjectFromUri(inputFilename)
workspaceName = self.getParameterValue(self.WORKSPACE)
filename = LayerExporter.exportVectorLayer(layer)
filename = dataobjects.exportVectorLayer(layer)
basefilename = os.path.basename(filename)
basepathname = os.path.dirname(filename) + os.sep + basefilename[:basefilename.find('.')]
connection = {
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/admintools/geoserver/support.py
Expand Up @@ -28,7 +28,7 @@
import urllib
import urlparse
from zipfile import ZipFile
from processing.core.ProcessingUtils import ProcessingUtils
from processing.tools.system import *


logger = logging.getLogger("gsconfig.support")
Expand Down Expand Up @@ -195,7 +195,7 @@ def prepare_upload_bundle(name, data):
file-like objects. The client code is responsible for deleting the zip
archive when it's done."""
#we ut the zip file in the processing temp dir, so it is deleted at the end.
f = ProcessingUtils.getTempFilename('zip')
f = getTempFilename('zip')
zip_file = ZipFile(f, 'w')
for ext, stream in data.iteritems():
fname = "%s.%s" % (name, ext)
Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/AddTableField.py
Expand Up @@ -29,7 +29,7 @@
from qgis.core import *

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.QGisLayers import QGisLayers
from processing.tools import dataobjects, vector

from processing.parameters.ParameterVector import ParameterVector
from processing.parameters.ParameterString import ParameterString
Expand Down Expand Up @@ -72,7 +72,7 @@ def processAlgorithm(self, progress):
fieldPrecision = self.getParameterValue(self.FIELD_PRECISION)
output = self.getOutputFromName(self.OUTPUT_LAYER)

layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))

provider = layer.dataProvider()
fields = provider.fields()
Expand All @@ -81,7 +81,7 @@ def processAlgorithm(self, progress):
outFeat = QgsFeature()
inGeom = QgsGeometry()
nElement = 0
features = QGisLayers.features(layer)
features = vector.features(layer)
nFeat = len(features)
for inFeat in features:
progress.setPercentage(int((100 * nElement)/nFeat))
Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/AutoincrementalField.py
Expand Up @@ -27,7 +27,7 @@
from PyQt4.QtCore import *
from qgis.core import *
from processing.parameters.ParameterVector import ParameterVector
from processing.core.QGisLayers import QGisLayers
from processing.tools import dataobjects, vector
from processing.outputs.OutputVector import OutputVector

class AutoincrementalField(GeoAlgorithm):
Expand All @@ -42,7 +42,7 @@ class AutoincrementalField(GeoAlgorithm):

def processAlgorithm(self, progress):
output = self.getOutputFromName(self.OUTPUT)
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
vlayer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT))
vprovider = vlayer.dataProvider()
fields = vprovider.fields()
fields.append(QgsField("AUTO", QVariant.Int))
Expand All @@ -51,7 +51,7 @@ def processAlgorithm(self, progress):
outFeat = QgsFeature()
inGeom = QgsGeometry()
nElement = 0
features = QGisLayers.features(vlayer)
features = vector.features(vlayer)
nFeat = len(features)
for inFeat in features:
progress.setPercentage(int((100 * nElement)/nFeat))
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/BarPlot.py
Expand Up @@ -33,7 +33,7 @@
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.outputs.OutputHTML import OutputHTML
from processing.tools import *
from processing.core.QGisLayers import QGisLayers
from processing.tools import dataobjects

class BarPlot(GeoAlgorithm):

Expand All @@ -44,7 +44,7 @@ class BarPlot(GeoAlgorithm):

def processAlgorithm(self, progress):
uri = self.getParameterValue(self.INPUT)
layer = QGisLayers.getObjectFromUri(uri)
layer = dataobjects.getObjectFromUri(uri)
namefieldname = self.getParameterValue(self.NAME_FIELD)
valuefieldname = self.getParameterValue(self.VALUE_FIELD)
output = self.getOutputValue(self.OUTPUT)
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/CreateConstantRaster.py
Expand Up @@ -33,7 +33,7 @@
from qgis.core import *
from processing.parameters.ParameterNumber import ParameterNumber
from processing.outputs.OutputRaster import OutputRaster
from processing.core.QGisLayers import QGisLayers
from processing.tools import dataobjects

class CreateConstantRaster(GeoAlgorithm):

Expand All @@ -49,7 +49,7 @@ class CreateConstantRaster(GeoAlgorithm):
def processAlgorithm(self, progress):
output = self.getOutputFromName(self.OUTPUT)
value = self.getOutputValue(self.NUMBER)
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT))
cellsize = (layer.extent().xMaximum() - layer.extent().xMinimum())/layer.width()
w = RasterWriter(output.getCompatibleFileName(self), layer.extent().xMinimum(), layer.extent().yMinimum(), layer.extent().xMaximum(),
layer.extent().yMaximum(), cellsize, 1, self.crs)
Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/EquivalentNumField.py
Expand Up @@ -27,7 +27,7 @@
from PyQt4.QtCore import *
from qgis.core import *
from processing.parameters.ParameterVector import ParameterVector
from processing.core.QGisLayers import QGisLayers
from processing.tools import dataobjects, vector
from processing.outputs.OutputVector import OutputVector
from processing.parameters.ParameterTableField import ParameterTableField

Expand All @@ -45,7 +45,7 @@ class EquivalentNumField(GeoAlgorithm):
def processAlgorithm(self, progress):
fieldname = self.getParameterValue(self.FIELD)
output = self.getOutputFromName(self.OUTPUT)
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
vlayer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT))
vprovider = vlayer.dataProvider()
fieldindex = vlayer.fieldNameIndex(fieldname)
fields = vprovider.fields()
Expand All @@ -55,7 +55,7 @@ def processAlgorithm(self, progress):
inGeom = QgsGeometry()
nElement = 0
classes = {}
features = QGisLayers.features(vlayer)
features = vector.features(vlayer)
nFeat = len(features)
for feature in features:
progress.setPercentage(int((100 * nElement)/nFeat))
Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/Explode.py
Expand Up @@ -28,7 +28,7 @@
from PyQt4.QtGui import *
from qgis.core import *
from processing.parameters.ParameterVector import ParameterVector
from processing.core.QGisLayers import QGisLayers
from processing.tools import dataobjects, vector
from processing.outputs.OutputVector import OutputVector

class Explode(GeoAlgorithm):
Expand All @@ -42,15 +42,15 @@ class Explode(GeoAlgorithm):
#===========================================================================

def processAlgorithm(self, progress):
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
vlayer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT))
output = self.getOutputFromName(self.OUTPUT)
vprovider = vlayer.dataProvider()
fields = vprovider.fields()
writer = output.getVectorWriter(fields, QGis.WKBLineString, vlayer.crs() )
outFeat = QgsFeature()
inGeom = QgsGeometry()
nElement = 0
features = QGisLayers.features(vlayer)
features = vector.features(vlayer)
nFeat = len(features)
for feature in features:
nElement += 1
Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/FieldPyculator.py
Expand Up @@ -30,7 +30,7 @@
from qgis.core import *

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.QGisLayers import QGisLayers
from processing.tools import dataobjects, vector
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException

from processing.parameters.ParameterVector import ParameterVector
Expand Down Expand Up @@ -82,7 +82,7 @@ def processAlgorithm(self, progress):
globalExpression = self.getParameterValue(self.GLOBAL)
output = self.getOutputFromName(self.OUTPUT_LAYER)

layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
provider = layer.dataProvider()
fields = provider.fields()
fields.append(QgsField(fieldName, self.TYPES[fieldType], "", fieldLength, fieldPrecision))
Expand Down Expand Up @@ -125,7 +125,7 @@ def processAlgorithm(self, progress):
unicode(sys.exc_info()[0].__name__), unicode(sys.exc_info()[1]))

#run
features = QGisLayers.features(layer)
features = vector.features(layer)
nFeatures = len(features)
nElement = 1
for feat in features:
Expand Down

0 comments on commit 807ffaa

Please sign in to comment.