Skip to content
Permalink
Browse files

[processing] adapted algorithm to function refactoring

Also set threaded execution to false by default, since it was behaving strangely
  • Loading branch information
volaya committed Sep 11, 2013
1 parent ff5e1ee commit 236c239844a00f3b6ba203ca52034dae8e920b10
@@ -16,6 +16,7 @@
* *
***************************************************************************
"""
from processing.tools import dataobjects

__author__ = 'Victor Olaya'
__date__ = 'October 2012'
@@ -24,7 +25,6 @@
__revision__ = '$Format:%H$'

import os
from processing.core.QGisLayers import QGisLayers
from processing.parameters.ParameterBoolean import ParameterBoolean
from processing.parameters.ParameterVector import ParameterVector
from processing.core.GeoAlgorithm import GeoAlgorithm
@@ -81,7 +81,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)
@@ -24,14 +24,10 @@
__revision__ = '$Format:%H$'

import math

from PyQt4.QtCore import *

from qgis.core import *

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

from processing.parameters.ParameterVector import ParameterVector
from processing.parameters.ParameterNumber import ParameterNumber
from processing.parameters.ParameterBoolean import ParameterBoolean
@@ -58,12 +54,12 @@ def processAlgorithm(self, progress):
horizontal = self.getParameterValue(self.HORIZONTAL)
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()
writer = output.getVectorWriter(provider.fields(), provider.geometryType(), provider.crs())

features = QGisLayers.features(layer)
features = vector.features(layer)

current = 0
total = 100.0 / len(features)
@@ -24,21 +24,14 @@
__revision__ = '$Format:%H$'

from PyQt4.QtCore import *

from osgeo import gdal

from qgis.core import *

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

from processing.parameters.ParameterRaster import ParameterRaster
from processing.parameters.ParameterVector import ParameterVector

from processing.outputs.OutputVector import OutputVector

from processing.algs import QGISUtils as utils

class PointsFromLines(GeoAlgorithm):

INPUT_RASTER = "INPUT_RASTER"
@@ -47,15 +40,15 @@ class PointsFromLines(GeoAlgorithm):
OUTPUT_LAYER = "OUTPUT_LAYER"

def defineCharacteristics(self):
self.name = "Points from lines"
self.name = "Get raster values at layer point"
self.group = "Vector geometry tools"

self.addParameter(ParameterRaster(self.INPUT_RASTER, "Raster layer"))
self.addParameter(ParameterVector(self.INPUT_VECTOR, "Vector layer", [ParameterVector.VECTOR_TYPE_LINE]))
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer"))

def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_VECTOR))
layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT_VECTOR))

rasterPath = unicode(self.getParameterValue(self.INPUT_RASTER))

@@ -78,7 +71,7 @@ def processAlgorithm(self, progress):
self.pointId = 0

current = 0
features = QGisLayers.features(layer)
features = vector.features(layer)
total = 100.0 / len(features)
for f in features:
geom = f.geometry()
@@ -89,8 +82,8 @@ def processAlgorithm(self, progress):
p1 = line[i]
p2 = line[i + 1]

x1, y1 = utils.mapToPixel(p1.x(), p1.y(), geoTransform)
x2, y2 = utils.mapToPixel(p2.x(), p2.y(), geoTransform)
x1, y1 = raster.mapToPixel(p1.x(), p1.y(), geoTransform)
x2, y2 = raster.mapToPixel(p2.x(), p2.y(), geoTransform)

self.buildLine(x1, y1, x2, y2, geoTransform, writer, outFeature)
else:
@@ -99,8 +92,8 @@ def processAlgorithm(self, progress):
p1 = points[i]
p2 = points[i + 1]

x1, y1 = utils.mapToPixel(p1.x(), p1.y(), geoTransform)
x2, y2 = utils.mapToPixel(p2.x(), p2.y(), geoTransform)
x1, y1 = raster.mapToPixel(p1.x(), p1.y(), geoTransform)
x2, y2 = raster.mapToPixel(p2.x(), p2.y(), geoTransform)

self.buildLine(x1, y1, x2, y2, geoTransform, writer, outFeature)

@@ -114,7 +107,6 @@ def processAlgorithm(self, progress):

def buildLine(self, startX, startY, endX, endY, geoTransform, writer, feature):
point = QgsPoint()

if startX == endX:
if startY > endY:
startY, endY = endY, startY
@@ -168,7 +160,7 @@ def buildLine(self, startX, startY, endX, endY, geoTransform, writer, feature):
startY += dy2

def createPoint(self, pX, pY, geoTransform, writer, feature):
x, y = utils.pixelToMap(pX, pY, geoTransform)
x, y = raster.pixelToMap(pX, pY, geoTransform)

feature.setGeometry(QgsGeometry.fromPoint(QgsPoint(x, y)))
feature["id"] = self.fid
@@ -23,21 +23,15 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from PyQt4.QtCore import *

from osgeo import gdal

from qgis.core import *

from PyQt4.QtCore import *
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.QGisLayers import QGisLayers

from processing.parameters.ParameterRaster import ParameterRaster
from processing.parameters.ParameterVector import ParameterVector

from processing.outputs.OutputVector import OutputVector

from processing.algs import QGISUtils as utils
from processing.tools import dataobjects, vector
from processing.tools.general import *

class PointsFromPolygons(GeoAlgorithm):

@@ -55,7 +49,7 @@ def defineCharacteristics(self):
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer"))

def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_VECTOR))
layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT_VECTOR))

rasterPath = unicode(self.getParameterValue(self.INPUT_RASTER))

@@ -79,7 +73,7 @@ def processAlgorithm(self, progress):
pointId = 0

current = 0
features = QGisLayers.features(layer)
features = vector.features(layer)
total = 100.0 / len(features)
for f in features:
geom = f.geometry()
@@ -90,12 +84,12 @@ def processAlgorithm(self, progress):
yMin = bbox.yMinimum()
yMax = bbox.yMaximum()

startRow, startColumn = utils.mapToPixel(xMin, yMax, geoTransform)
endRow, endColumn = utils.mapToPixel(xMax, yMin, geoTransform)
startRow, startColumn = mapToPixel(xMin, yMax, geoTransform)
endRow, endColumn = mapToPixel(xMax, yMin, geoTransform)

for row in xrange(startRow, endRow + 1):
for col in xrange(startColumn, endColumn + 1):
x, y = utils.pixelToMap(row, col, geoTransform)
x, y = pixelToMap(row, col, geoTransform)
point.setX(x)
point.setY(y)

This file was deleted.

@@ -24,26 +24,19 @@
__revision__ = '$Format:%H$'

from PyQt4.QtCore import *

import numpy
from osgeo import gdal, ogr, osr

from qgis.core import *

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

from processing.parameters.ParameterVector import ParameterVector
from processing.parameters.ParameterRaster import ParameterRaster
from processing.parameters.ParameterString import ParameterString
from processing.parameters.ParameterNumber import ParameterNumber
from processing.parameters.ParameterBoolean import ParameterBoolean

from processing.outputs.OutputVector import OutputVector

from processing.algs.ftools import FToolsUtils as ftools_utils
from processing.algs import QGISUtils as utils

class ZonalStatistics(GeoAlgorithm):

INPUT_RASTER = "INPUT_RASTER"
@@ -65,7 +58,7 @@ def defineCharacteristics(self):
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer"))

def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_VECTOR))
layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT_VECTOR))

rasterPath = unicode(self.getParameterValue(self.INPUT_RASTER))
bandNumber = self.getParameterValue(self.RASTER_BAND)
@@ -99,8 +92,8 @@ def processAlgorithm(self, progress):
yMin = rasterBBox.yMinimum()
yMax = rasterBBox.yMaximum()

startColumn, startRow = utils.mapToPixel(xMin, yMax, geoTransform)
endColumn, endRow = utils.mapToPixel(xMax, yMin, geoTransform)
startColumn, startRow = mapToPixel(xMin, yMax, geoTransform)
endColumn, endRow = mapToPixel(xMax, yMin, geoTransform)

width = endColumn - startColumn
height = endRow - startRow
@@ -120,15 +113,15 @@ def processAlgorithm(self, progress):
memRasterDriver = gdal.GetDriverByName("MEM")

fields = layer.pendingFields()
idxMin, fields = ftools_utils.findOrCreateField(layer, fields, columnPrefix + "min", 21, 6)
idxMax, fields = ftools_utils.findOrCreateField(layer, fields, columnPrefix + "max", 21, 6)
idxSum, fields = ftools_utils.findOrCreateField(layer, fields, columnPrefix + "sum", 21, 6)
idxCount, fields = ftools_utils.findOrCreateField(layer, fields, columnPrefix + "count", 21, 6)
idxMean, fields = ftools_utils.findOrCreateField(layer, fields, columnPrefix + "mean", 21, 6)
idxStd, fields = ftools_utils.findOrCreateField(layer, fields, columnPrefix + "std", 21, 6)
idxUnique, fields = ftools_utils.findOrCreateField(layer, fields, columnPrefix + "unique", 21, 6)
idxRange, fields = ftools_utils.findOrCreateField(layer, fields, columnPrefix + "range", 21, 6)
idxCV, fields = ftools_utils.findOrCreateField(layer, fields, columnPrefix + "cv", 21, 6)
idxMin, fields = vector.findOrCreateField(layer, fields, columnPrefix + "min", 21, 6)
idxMax, fields = vector.findOrCreateField(layer, fields, columnPrefix + "max", 21, 6)
idxSum, fields = vector.findOrCreateField(layer, fields, columnPrefix + "sum", 21, 6)
idxCount, fields = vector.findOrCreateField(layer, fields, columnPrefix + "count", 21, 6)
idxMean, fields = vector.findOrCreateField(layer, fields, columnPrefix + "mean", 21, 6)
idxStd, fields = vector.findOrCreateField(layer, fields, columnPrefix + "std", 21, 6)
idxUnique, fields = vector.findOrCreateField(layer, fields, columnPrefix + "unique", 21, 6)
idxRange, fields = vector.findOrCreateField(layer, fields, columnPrefix + "range", 21, 6)
idxCV, fields = vector.findOrCreateField(layer, fields, columnPrefix + "cv", 21, 6)
#idxMedian, fields = ftools_utils.findOrCreateField(layer, fields, columnPrefix + "median", 21, 6)

writer = self.getOutputFromName(self.OUTPUT_LAYER).getVectorWriter(fields.toList(),
@@ -140,7 +133,7 @@ def processAlgorithm(self, progress):
outFeat.setFields(fields)

current = 0
features = QGisLayers.features(layer)
features = vector.features(layer)
total = 100.0 / len(features)
for f in features:
geom = f.geometry()
@@ -156,8 +149,8 @@ def processAlgorithm(self, progress):
yMin = bbox.yMinimum()
yMax = bbox.yMaximum()

startColumn, startRow = utils.mapToPixel(xMin, yMax, geoTransform)
endColumn, endRow = utils.mapToPixel(xMax, yMin, geoTransform)
startColumn, startRow = mapToPixel(xMin, yMax, geoTransform)
endColumn, endRow = mapToPixel(xMax, yMin, geoTransform)

width = endColumn - startColumn
height = endRow - startRow

0 comments on commit 236c239

Please sign in to comment.
You can’t perform that action at this time.