Skip to content

Commit

Permalink
[processing] Hide redundant grid creation algorithms
Browse files Browse the repository at this point in the history
Condenses the duplicate grid algorithms into single algorithms
(one for line, one for polygon), taking the best bits from both
and adding tests
  • Loading branch information
nyalldawson committed Nov 24, 2016
1 parent 7e0cd9a commit 772b769
Show file tree
Hide file tree
Showing 14 changed files with 920 additions and 64 deletions.
15 changes: 11 additions & 4 deletions python/plugins/processing/algs/help/qgis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,19 @@ qgis:createattributeindex: >
qgis:createconstantrasterlayer: >
Given an input raster layer an a value, this algorithm generates a new layer with the same extent and cellsize as the input one, and all cells with the specified value.

qgis:creategrid:
This algorithm creates a vector layer with a grid convering a given extent. Features can be lines or polygons, and the shape used in the grid can be rectangles, diamond or hexagons.
qgis:creategridlines:
This algorithm creates a line vector layer with a grid covering a given extent.

The size of each element in the grid is defined using a horizontal and vertical spaciong.
The size of each element in the grid is defined using a horizontal and vertical spacing.

The CRS of the output layer must be defined. The grid extent and the spacing values are supposed to be expressed in the coordinates and units of this CRS.
The CRS of the output layer must be defined. The grid extent and the spacing values must be expressed in the coordinates and units of this CRS.

qgis:creategridpolygon:
This algorithm creates a polygon vector layer with a grid covering a given extent. The grid shape can be rectangles, diamond or hexagons.

The size of each element in the grid is defined using a horizontal and vertical spacing.

The CRS of the output layer must be defined. The grid extent and the spacing values must be expressed in the coordinates and units of this CRS.

qgis:createpointsalonglines: >
This algorithms creates a points layer, with points distributed along the lines of an input vector layer. the distance between points (measured along the line) is defined as a parameter.
Expand Down
119 changes: 76 additions & 43 deletions python/plugins/processing/algs/qgis/GridLine.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* *
***************************************************************************
"""
from builtins import range

__author__ = 'Michael Minn'
__date__ = 'May 2010'
Expand All @@ -29,8 +28,17 @@
import os
import math

from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtCore import QVariant
from qgis.core import QgsRectangle, QgsCoordinateReferenceSystem, Qgis, QgsField, QgsFeature, QgsGeometry, QgsPoint, QgsWkbTypes
from qgis.core import (QgsRectangle,
QgsCoordinateReferenceSystem,
Qgis,
QgsField,
QgsFeature,
QgsGeometry,
QgsPointV2,
QgsLineString,
QgsWkbTypes)
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterExtent
Expand All @@ -49,20 +57,21 @@ class GridLine(GeoAlgorithm):
CRS = 'CRS'
OUTPUT = 'OUTPUT'

#def getIcon(self):
# return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'vector_grid.png'))
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.tags = self.tr('grid,lines,vector,create,fishnet')

self.addParameter(ParameterExtent(self.EXTENT,
self.tr('Grid extent'), optional=False))
self.addParameter(ParameterNumber(self.HSPACING,
self.tr('Horizontal spacing'), default=10.0))
self.tr('Horizontal spacing'), 0.0, 1000000000.0, default=0.0001))
self.addParameter(ParameterNumber(self.VSPACING,
self.tr('Vertical spacing'), default=10.0))
self.addParameter(ParameterCrs(self.CRS, 'Grid CRS'))
self.tr('Vertical spacing'), 0.0, 1000000000.0, default=0.0001))
self.addParameter(ParameterCrs(self.CRS, 'Grid CRS', 'EPSG:4326'))

self.addOutput(OutputVector(self.OUTPUT, self.tr('Grid'), datatype=[dataobjects.TYPE_VECTOR_LINE]))

Expand All @@ -77,8 +86,6 @@ def processAlgorithm(self, progress):

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

if hSpacing <= 0 or vSpacing <= 0:
raise GeoAlgorithmExecutionException(
Expand All @@ -95,42 +102,68 @@ def processAlgorithm(self, progress):
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)
QgsField('bottom', QVariant.Double, '', 24, 16),
QgsField('id', QVariant.Int, '', 10, 0),
QgsField('coord', QVariant.Double, '', 24, 15)
]

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 range(columns + 1):
polyline = []
x = originX + (col * hSpacing)
for row in range(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 range(rows + 1):
polyline = []
y = originY - (row * vSpacing)
for col in range(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)
feat = QgsFeature()
feat.initAttributes(len(fields))

count = 0
id = 1

# latitude lines
count_max = height / vSpacing
count_update = count_max * 0.10
y = bbox.yMaximum()
while y >= bbox.yMinimum():
pt1 = QgsPointV2(bbox.xMinimum(), y)
pt2 = QgsPointV2(bbox.xMaximum(), y)
line = QgsLineString()
line.setPoints([pt1, pt2])
feat.setGeometry(QgsGeometry(line))
feat.setAttributes([bbox.xMinimum(),
y,
bbox.xMaximum(),
y,
id,
y])
writer.addFeature(feat)
y = y - vSpacing
id += 1
count += 1
if int(math.fmod(count, count_update)) == 0:
progress.setPercentage(int(count / count_max * 50))

progress.setPercentage(50)

# longitude lines
# counters for progressbar - update every 5%
count = 0
count_max = width / hSpacing
count_update = count_max * 0.10
x = bbox.xMinimum()
while x <= bbox.xMaximum():
pt1 = QgsPointV2(x, bbox.yMaximum())
pt2 = QgsPointV2(x, bbox.yMinimum())
line = QgsLineString()
line.setPoints([pt1, pt2])
feat.setGeometry(QgsGeometry(line))
feat.setAttributes([x,
bbox.yMaximum(),
x,
bbox.yMinimum(),
id,
x])
writer.addFeature(feat)
x = x + hSpacing
id += 1
count += 1
if int(math.fmod(count, count_update)) == 0:
progress.setPercentage(50 + int(count / count_max * 50))

del writer
63 changes: 47 additions & 16 deletions python/plugins/processing/algs/qgis/GridPolygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* *
***************************************************************************
"""
from builtins import range

__author__ = 'Michael Minn'
__date__ = 'May 2010'
Expand All @@ -29,6 +28,7 @@
import os
import math

from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtCore import QVariant
from qgis.core import QgsRectangle, QgsCoordinateReferenceSystem, Qgis, QgsField, QgsFeature, QgsGeometry, QgsPoint, QgsWkbTypes
from processing.core.GeoAlgorithm import GeoAlgorithm
Expand All @@ -51,12 +51,13 @@ class GridPolygon(GeoAlgorithm):
CRS = 'CRS'
OUTPUT = 'OUTPUT'

#def getIcon(self):
# return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'vector_grid.png'))
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 (polygon)')
self.group, self.i18n_group = self.trAlgorithm('Vector creation tools')
self.tags = self.tr('grid,polygons,vector,create,fishnet')

self.types = [self.tr('Rectangle (polygon)'),
self.tr('Diamond (polygon)'),
Expand All @@ -67,10 +68,10 @@ def defineCharacteristics(self):
self.addParameter(ParameterExtent(self.EXTENT,
self.tr('Grid extent'), optional=False))
self.addParameter(ParameterNumber(self.HSPACING,
self.tr('Horizontal spacing'), default=10.0))
self.tr('Horizontal spacing'), 0.0, 1000000000.0, 0.0001))
self.addParameter(ParameterNumber(self.VSPACING,
self.tr('Vertical spacing'), default=10.0))
self.addParameter(ParameterCrs(self.CRS, 'Grid CRS'))
self.tr('Vertical spacing'), 0.0, 1000000000.0, 0.0001))
self.addParameter(ParameterCrs(self.CRS, 'Grid CRS', 'EPSG:4326'))

self.addOutput(OutputVector(self.OUTPUT, self.tr('Grid'), datatype=[dataobjects.TYPE_VECTOR_POLYGON]))

Expand Down Expand Up @@ -104,30 +105,36 @@ def processAlgorithm(self, progress):
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)
QgsField('bottom', QVariant.Double, '', 24, 16),
QgsField('id', QVariant.Int, '', 10, 0)
]

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

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

del writer

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

columns = int(math.ceil(float(width) / hSpacing))
rows = int(math.ceil(float(height) / vSpacing))
cells = rows * columns
count_update = cells * 0.05

id = 1
count = 0

for col in range(columns):
# (column + 1) and (row + 1) calculation is used to maintain
Expand All @@ -147,11 +154,15 @@ def _rectangleGrid(self, writer, width, height, originX, originY,
polyline.append(QgsPoint(x1, y1))

ft.setGeometry(QgsGeometry.fromPolygon([polyline]))
ft.setAttributes([x1, y1, x2, y2])
ft.setAttributes([x1, y1, x2, y2, id])
writer.addFeature(ft)
id += 1
count += 1
if int(math.fmod(count, count_update)) == 0:
progress.setPercentage(int(count / cells * 100))

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

halfHSpacing = hSpacing / 2
Expand All @@ -160,6 +171,12 @@ def _diamondGrid(self, writer, width, height, originX, originY,
columns = int(math.ceil(float(width) / halfHSpacing))
rows = int(math.ceil(float(height) / vSpacing))

cells = rows * columns
count_update = cells * 0.05

id = 1
count = 0

for col in range(columns):
x1 = originX + ((col + 0) * halfHSpacing)
x2 = originX + ((col + 1) * halfHSpacing)
Expand All @@ -183,11 +200,15 @@ def _diamondGrid(self, writer, width, height, originX, originY,
polyline.append(QgsPoint(x1, y2))

ft.setGeometry(QgsGeometry.fromPolygon([polyline]))
ft.setAttributes([x1, y1, x3, y3])
ft.setAttributes([x1, y1, x3, y3, id])
writer.addFeature(ft)
id += 1
count += 1
if int(math.fmod(count, count_update)) == 0:
progress.setPercentage(int(count / cells * 100))

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

# To preserve symmetry, hspacing is fixed relative to vspacing
Expand All @@ -200,6 +221,12 @@ def _hexagonGrid(self, writer, width, height, originX, originY,
columns = int(math.ceil(float(width) / hSpacing))
rows = int(math.ceil(float(height) / vSpacing))

cells = rows * columns
count_update = cells * 0.05

id = 1
count = 0

for col in range(columns):
# (column + 1) and (row + 1) calculation is used to maintain
# topology between adjacent shapes and avoid overlaps/holes
Expand Down Expand Up @@ -229,5 +256,9 @@ def _hexagonGrid(self, writer, width, height, originX, originY,
polyline.append(QgsPoint(x1, y2))

ft.setGeometry(QgsGeometry.fromPolygon([polyline]))
ft.setAttributes([x1, y1, x4, y3])
ft.setAttributes([x1, y1, x4, y3, id])
writer.addFeature(ft)
id += 1
count += 1
if int(math.fmod(count, count_update)) == 0:
progress.setPercentage(int(count / cells * 100))
7 changes: 6 additions & 1 deletion python/plugins/processing/algs/qgis/VectorGridLines.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class VectorGridLines(GeoAlgorithm):
STEP_Y = 'STEP_Y'
OUTPUT = 'OUTPUT'

def __init__(self):
GeoAlgorithm.__init__(self)
# this algorithm is deprecated - use GridLine instead
self.showInToolbox = False

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

Expand All @@ -58,7 +63,7 @@ def defineCharacteristics(self):
self.group, self.i18n_group = self.trAlgorithm('Vector creation tools')

self.addParameter(ParameterExtent(self.EXTENT,
self.tr('Grid extent')))
self.tr('Grid extent'), optional=False))
self.addParameter(ParameterNumber(self.STEP_X,
self.tr('X spacing'), 0.0, 1000000000.0, 0.0001))
self.addParameter(ParameterNumber(self.STEP_Y,
Expand Down
5 changes: 5 additions & 0 deletions python/plugins/processing/algs/qgis/VectorGridPolygons.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class VectorGridPolygons(GeoAlgorithm):
STEP_Y = 'STEP_Y'
OUTPUT = 'OUTPUT'

def __init__(self):
GeoAlgorithm.__init__(self)
# this algorithm is deprecated - use GridPolygon instead
self.showInToolbox = False

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

Expand Down
Loading

0 comments on commit 772b769

Please sign in to comment.