Skip to content
Permalink
Browse files

[processing] split some native algs to take advantage of explicit out…

…put types
  • Loading branch information
alexbruy committed Sep 2, 2016
1 parent 12d6e15 commit 13cd4c944f72a79fb6e4175295afb69550973217
@@ -0,0 +1,134 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
Grid.py
---------------------
Date : May 2010
Copyright : (C) 2010 by Michael Minn
Email : pyqgis at michaelminn dot com
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""

__author__ = 'Michael Minn'
__date__ = 'May 2010'
__copyright__ = '(C) 2010, Michael Minn'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

import os
import math

from qgis.PyQt.QtCore import QVariant
from qgis.core import QgsRectangle, QgsCoordinateReferenceSystem, Qgis, QgsField, QgsFeature, QgsGeometry, QgsPoint, QgsWkbTypes
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterExtent
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterCrs
from processing.core.outputs import OutputVector

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]


class GridLine(GeoAlgorithm):
EXTENT = 'EXTENT'
HSPACING = 'HSPACING'
VSPACING = 'VSPACING'
CRS = 'CRS'
OUTPUT = 'OUTPUT'

#def getIcon(self):
# return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'vector_grid.png'))

def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Create grid (lines)')
self.group, self.i18n_group = self.trAlgorithm('Vector creation tools')

self.addParameter(ParameterExtent(self.EXTENT,
self.tr('Grid extent')))
self.addParameter(ParameterNumber(self.HSPACING,
self.tr('Horizontal spacing'), default=10.0))
self.addParameter(ParameterNumber(self.VSPACING,
self.tr('Vertical spacing'), default=10.0))
self.addParameter(ParameterCrs(self.CRS, 'Grid CRS'))

self.addOutput(OutputVector(self.OUTPUT, self.tr('Grid')))

def processAlgorithm(self, progress):
extent = self.getParameterValue(self.EXTENT).split(',')
hSpacing = self.getParameterValue(self.HSPACING)
vSpacing = self.getParameterValue(self.VSPACING)
crs = QgsCoordinateReferenceSystem(self.getParameterValue(self.CRS))

bbox = QgsRectangle(float(extent[0]), float(extent[2]),
float(extent[1]), float(extent[3]))

width = bbox.width()
height = bbox.height()
originX = bbox.xMinimum()
originY = bbox.yMaximum()

if hSpacing <= 0 or vSpacing <= 0:
raise GeoAlgorithmExecutionException(
self.tr('Invalid grid spacing: %s/%s' % (hSpacing, vSpacing)))

if width < hSpacing:
raise GeoAlgorithmExecutionException(
self.tr('Horizontal spacing is too small for the covered area'))

if height < vSpacing:
raise GeoAlgorithmExecutionException(
self.tr('Vertical spacing is too small for the covered area'))

fields = [QgsField('left', QVariant.Double, '', 24, 16),
QgsField('top', QVariant.Double, '', 24, 16),
QgsField('right', QVariant.Double, '', 24, 16),
QgsField('bottom', QVariant.Double, '', 24, 16)
]

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields,
QgsWkbTypes.LineString, crs)

self._rectangleGridLine(
writer, width, height, originX, originY, hSpacing, vSpacing)

def _rectangleGridLine(self, writer, width, height, originX, originY,
hSpacing, vSpacing):
ft = QgsFeature()

columns = int(math.ceil(float(width) / hSpacing))
rows = int(math.ceil(float(height) / vSpacing))

# Longitude lines
for col in xrange(columns + 1):
polyline = []
x = originX + (col * hSpacing)
for row in xrange(rows + 1):
y = originY - (row * vSpacing)
polyline.append(QgsPoint(x, y))

ft.setGeometry(QgsGeometry.fromPolyline(polyline))
ft.setAttributes([x, originY, x, originY + (rows * vSpacing)])
writer.addFeature(ft)

# Latitude lines
for row in xrange(rows + 1):
polyline = []
y = originY - (row * vSpacing)
for col in xrange(columns + 1):
x = originX + (col * hSpacing)
polyline.append(QgsPoint(x, y))

ft.setGeometry(QgsGeometry.fromPolyline(polyline))
ft.setAttributes([originX, y, originX + (col * hSpacing), y])
writer.addFeature(ft)
@@ -2,7 +2,7 @@

"""
***************************************************************************
Grid.py
GridPolygon.py
---------------------
Date : May 2010
Copyright : (C) 2010 by Michael Minn
@@ -41,7 +41,7 @@
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]


class Grid(GeoAlgorithm):
class GridPolygon(GeoAlgorithm):
TYPE = 'TYPE'
EXTENT = 'EXTENT'
HSPACING = 'HSPACING'
@@ -53,11 +53,10 @@ class Grid(GeoAlgorithm):
# return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'vector_grid.png'))

def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Create grid')
self.name, self.i18n_name = self.trAlgorithm('Create grid (polygon)')
self.group, self.i18n_group = self.trAlgorithm('Vector creation tools')

self.types = [self.tr('Rectangle (line)'),
self.tr('Rectangle (polygon)'),
self.types = [self.tr('Rectangle (polygon)'),
self.tr('Diamond (polygon)'),
self.tr('Hexagon (polygon)')]

@@ -100,68 +99,28 @@ def processAlgorithm(self, progress):
raise GeoAlgorithmExecutionException(
self.tr('Vertical spacing is too small for the covered area'))

#if self.types[idx].find(self.tr('polygon')) >= 0:
if idx != 0:
geometryType = QgsWkbTypes.Polygon
else:
geometryType = QgsWkbTypes.LineString

fields = [QgsField('left', QVariant.Double, '', 24, 16),
QgsField('top', QVariant.Double, '', 24, 16),
QgsField('right', QVariant.Double, '', 24, 16),
QgsField('bottom', QVariant.Double, '', 24, 16)
]

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields,
geometryType, crs)
QgsWkbTypes.Polygon, crs)

if idx == 0:
self._rectangleGridLine(
self._rectangleGrid(
writer, width, height, originX, originY, hSpacing, vSpacing)
elif idx == 1:
self._rectangleGridPoly(
writer, width, height, originX, originY, hSpacing, vSpacing)
elif idx == 2:
self._diamondGrid(
writer, width, height, originX, originY, hSpacing, vSpacing)
elif idx == 3:
elif idx == 2:
self._hexagonGrid(
writer, width, height, originX, originY, hSpacing, vSpacing)

del writer

def _rectangleGridLine(self, writer, width, height, originX, originY,
hSpacing, vSpacing):
ft = QgsFeature()

columns = int(math.ceil(float(width) / hSpacing))
rows = int(math.ceil(float(height) / vSpacing))

# Longitude lines
for col in xrange(columns + 1):
polyline = []
x = originX + (col * hSpacing)
for row in xrange(rows + 1):
y = originY - (row * vSpacing)
polyline.append(QgsPoint(x, y))

ft.setGeometry(QgsGeometry.fromPolyline(polyline))
ft.setAttributes([x, originY, x, originY + (rows * vSpacing)])
writer.addFeature(ft)

# Latitude lines
for row in xrange(rows + 1):
polyline = []
y = originY - (row * vSpacing)
for col in xrange(columns + 1):
x = originX + (col * hSpacing)
polyline.append(QgsPoint(x, y))

ft.setGeometry(QgsGeometry.fromPolyline(polyline))
ft.setAttributes([originX, y, originX + (col * hSpacing), y])
writer.addFeature(ft)

def _rectangleGridPoly(self, writer, width, height, originX, originY,
def _rectangleGrid(self, writer, width, height, originX, originY,
hSpacing, vSpacing):
ft = QgsFeature()

@@ -2,7 +2,7 @@

"""
***************************************************************************
Gridify.py
HubDistanceLines.py
---------------------
Date : May 2010
Copyright : (C) 2010 by Michael Minn
@@ -39,18 +39,13 @@
from math import sqrt


class HubDistance(GeoAlgorithm):
class HubDistanceLines(GeoAlgorithm):
POINTS = 'POINTS'
HUBS = 'HUBS'
FIELD = 'FIELD'
GEOMETRY = 'GEOMETRY'
UNIT = 'UNIT'
OUTPUT = 'OUTPUT'

GEOMETRIES = ['Point',
'Line to hub'
]

UNITS = ['Meters',
'Feet',
'Miles',
@@ -59,7 +54,7 @@ class HubDistance(GeoAlgorithm):
]

def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Distance to nearest hub')
self.name, self.i18n_name = self.trAlgorithm('Distance to nearest hub (line to hub)')
self.group, self.i18n_group = self.trAlgorithm('Vector analysis tools')

self.units = [self.tr('Meters'),
@@ -74,8 +69,6 @@ def defineCharacteristics(self):
self.tr('Destination hubs layer'), [ParameterVector.VECTOR_TYPE_ANY]))
self.addParameter(ParameterTableField(self.FIELD,
self.tr('Hub layer name attribute'), self.HUBS))
self.addParameter(ParameterSelection(self.GEOMETRY,
self.tr('Output shape type'), self.GEOMETRIES))
self.addParameter(ParameterSelection(self.UNIT,
self.tr('Measurement unit'), self.units))

@@ -95,16 +88,12 @@ def processAlgorithm(self, progress):
raise GeoAlgorithmExecutionException(
self.tr('Same layer given for both hubs and spokes'))

geomType = QgsWkbTypes.Point
if addLines:
geomType = QgsWkbTypes.LineString

fields = layerPoints.fields()
fields.append(QgsField('HubName', QVariant.String))
fields.append(QgsField('HubDist', QVariant.Double))

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
fields, geomType, layerPoints.crs())
fields, QgsWkbTypes.LineString, layerPoints.crs())

index = vector.spatialindex(layerHubs)

@@ -141,10 +130,7 @@ def processAlgorithm(self, progress):
feat = QgsFeature()
feat.setAttributes(attributes)

if geomType == QgsWkbTypes.Point:
feat.setGeometry(QgsGeometry.fromPoint(src))
else:
feat.setGeometry(QgsGeometry.fromPolyline([src, closest]))
feat.setGeometry(QgsGeometry.fromPolyline([src, closest]))

writer.addFeature(feat)
progress.setPercentage(int(current * total))

0 comments on commit 13cd4c9

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