326 lines
13 KiB
Python
Raw Normal View History

2014-10-03 09:49:29 +03:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
GridPolygon.py
2014-10-03 09:49:29 +03:00
---------------------
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
2014-10-04 14:02:29 +03:00
import math
from qgis.PyQt.QtGui import QIcon
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsRectangle,
QgsCoordinateReferenceSystem,
QgsField,
QgsFeatureSink,
QgsFeature,
QgsGeometry,
QgsPointXY,
QgsWkbTypes,
QgsProcessing,
2017-06-09 15:31:25 +10:00
QgsProcessingParameterEnum,
QgsProcessingParameterExtent,
QgsProcessingParameterNumber,
QgsProcessingParameterCrs,
QgsProcessingParameterFeatureSink,
QgsProcessingOutputVectorLayer,
QgsProcessingParameterDefinition,
QgsFields)
2017-03-04 19:41:23 +01:00
2017-06-06 13:41:42 +10:00
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.tools import dataobjects
2014-10-03 09:49:29 +03:00
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
2014-10-03 09:49:29 +03:00
class GridPolygon(QgisAlgorithm):
2014-10-03 09:49:29 +03:00
TYPE = 'TYPE'
EXTENT = 'EXTENT'
2014-10-03 09:49:29 +03:00
HSPACING = 'HSPACING'
VSPACING = 'VSPACING'
HOVERLAY = 'HOVERLAY'
VOVERLAY = 'VOVERLAY'
2014-10-03 09:49:29 +03:00
CRS = 'CRS'
OUTPUT = 'OUTPUT'
def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'vector_grid.png'))
def tags(self):
return self.tr('grid,lines,vector,create,fishnet').split(',')
def group(self):
return self.tr('Vector creation tools')
def __init__(self):
super().__init__()
self.types = [self.tr('Rectangle (polygon)'),
self.tr('Diamond (polygon)'),
self.tr('Hexagon (polygon)')]
2017-06-09 15:31:25 +10:00
self.addParameter(QgsProcessingParameterEnum(self.TYPE,
self.tr('Grid type'), self.types))
self.addParameter(QgsProcessingParameterExtent(self.EXTENT, self.tr('Grid extent')))
self.addParameter(QgsProcessingParameterNumber(self.HSPACING,
self.tr('Horizontal spacing'), QgsProcessingParameterNumber.Double,
0.0001, False, 0, 1000000000.0))
self.addParameter(QgsProcessingParameterNumber(self.VSPACING,
self.tr('Vertical spacing'), QgsProcessingParameterNumber.Double,
0.0001, False, 0, 1000000000.0))
self.addParameter(QgsProcessingParameterNumber(self.HOVERLAY,
self.tr('Horizontal overlay'), QgsProcessingParameterNumber.Double,
0.0, False, 0, 1000000000.0))
self.addParameter(QgsProcessingParameterNumber(self.VOVERLAY,
self.tr('Vertical overlay'), QgsProcessingParameterNumber.Double,
0.0, False, 0, 1000000000.0))
self.addParameter(QgsProcessingParameterCrs(self.CRS, 'Grid CRS', 'ProjectCrs'))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Grid')))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Grid'), QgsProcessing.TypeVectorPolygon))
2014-10-03 09:49:29 +03:00
def name(self):
return 'creategridpolygon'
def displayName(self):
return self.tr('Create grid (polygon)')
def processAlgorithm(self, parameters, context, feedback):
2017-06-09 15:31:25 +10:00
idx = self.parameterAsEnum(parameters, self.TYPE, context)
2017-06-09 15:31:25 +10:00
hSpacing = self.parameterAsDouble(parameters, self.HSPACING, context)
vSpacing = self.parameterAsDouble(parameters, self.VSPACING, context)
hOverlay = self.parameterAsDouble(parameters, self.HOVERLAY, context)
vOverlay = self.parameterAsDouble(parameters, self.VOVERLAY, context)
bbox = self.parameterAsExtent(parameters, self.EXTENT, context)
crs = self.parameterAsCrs(parameters, self.CRS, context)
width = bbox.width()
height = bbox.height()
originX = bbox.xMinimum()
originY = bbox.yMaximum()
2014-10-03 09:49:29 +03:00
if hSpacing <= 0 or vSpacing <= 0:
raise GeoAlgorithmExecutionException(
2017-03-04 16:23:36 +01:00
self.tr('Invalid grid spacing: {0}/{1}').format(hSpacing, vSpacing))
2014-10-03 09:49:29 +03:00
if width < hSpacing:
raise GeoAlgorithmExecutionException(
2015-01-15 20:41:15 +02:00
self.tr('Horizontal spacing is too small for the covered area'))
2014-10-03 09:49:29 +03:00
if hSpacing <= hOverlay or vSpacing <= vOverlay:
raise GeoAlgorithmExecutionException(
2017-03-04 16:23:36 +01:00
self.tr('Invalid overlay: {0}/{1}').format(hOverlay, vOverlay))
2014-10-03 09:49:29 +03:00
if height < vSpacing:
raise GeoAlgorithmExecutionException(
2015-01-15 20:41:15 +02:00
self.tr('Vertical spacing is too small for the covered area'))
2014-10-03 09:49:29 +03:00
fields = QgsFields()
fields.append(QgsField('left', QVariant.Double, '', 24, 16))
fields.append(QgsField('top', QVariant.Double, '', 24, 16))
fields.append(QgsField('right', QVariant.Double, '', 24, 16))
fields.append(QgsField('bottom', QVariant.Double, '', 24, 16))
fields.append(QgsField('id', QVariant.Int, '', 10, 0))
2014-10-03 09:49:29 +03:00
2017-06-09 15:31:25 +10:00
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, QgsWkbTypes.Polygon, crs)
2014-10-03 09:49:29 +03:00
if idx == 0:
self._rectangleGrid(
2017-06-09 15:31:25 +10:00
sink, width, height, originX, originY, hSpacing, vSpacing, hOverlay, vOverlay, feedback)
2014-10-03 09:49:29 +03:00
elif idx == 1:
self._diamondGrid(
2017-06-09 15:31:25 +10:00
sink, width, height, originX, originY, hSpacing, vSpacing, hOverlay, vOverlay, feedback)
elif idx == 2:
2014-10-03 09:49:29 +03:00
self._hexagonGrid(
2017-06-09 15:31:25 +10:00
sink, width, height, originX, originY, hSpacing, vSpacing, hOverlay, vOverlay, feedback)
2014-10-03 09:49:29 +03:00
2017-06-09 15:31:25 +10:00
return {self.OUTPUT: dest_id}
2014-10-03 09:49:29 +03:00
2017-06-09 15:31:25 +10:00
def _rectangleGrid(self, sink, width, height, originX, originY,
hSpacing, vSpacing, hOverlay, vOverlay, feedback):
2014-10-03 09:49:29 +03:00
ft = QgsFeature()
2017-01-23 09:24:31 +10:00
columns = int(math.ceil(float(width) / (hSpacing - hOverlay)))
rows = int(math.ceil(float(height) / (vSpacing - vOverlay)))
cells = rows * columns
count_update = cells * 0.05
id = 1
count = 0
2016-09-21 18:24:26 +02:00
for col in range(columns):
2017-06-09 15:31:25 +10:00
if feedback.isCanceled():
break
x1 = originX + (col * hSpacing - col * hOverlay)
x2 = x1 + hSpacing
2016-09-21 18:24:26 +02:00
for row in range(rows):
y1 = originY - (row * vSpacing - row * vOverlay)
y2 = y1 - vSpacing
2014-10-03 09:49:29 +03:00
polyline = []
polyline.append(QgsPointXY(x1, y1))
polyline.append(QgsPointXY(x2, y1))
polyline.append(QgsPointXY(x2, y2))
polyline.append(QgsPointXY(x1, y2))
polyline.append(QgsPointXY(x1, y1))
2014-10-03 09:49:29 +03:00
ft.setGeometry(QgsGeometry.fromPolygon([polyline]))
ft.setAttributes([x1, y1, x2, y2, id])
sink.addFeature(ft, QgsFeatureSink.FastInsert)
id += 1
count += 1
if int(math.fmod(count, count_update)) == 0:
feedback.setProgress(int(count / cells * 100))
2014-10-03 09:49:29 +03:00
2017-06-09 15:31:25 +10:00
def _diamondGrid(self, sink, width, height, originX, originY,
hSpacing, vSpacing, hOverlay, vOverlay, feedback):
2014-10-03 09:49:29 +03:00
ft = QgsFeature()
halfHSpacing = hSpacing / 2
halfVSpacing = vSpacing / 2
halfHOverlay = hOverlay / 2
halfVOverlay = vOverlay / 2
columns = int(math.ceil(float(width) / (halfHSpacing - halfHOverlay)))
rows = int(math.ceil(float(height) / (vSpacing - halfVOverlay)))
cells = rows * columns
count_update = cells * 0.05
id = 1
count = 0
2016-09-21 18:24:26 +02:00
for col in range(columns):
2017-06-09 15:31:25 +10:00
if feedback.isCanceled():
break
2017-01-23 09:24:31 +10:00
x = originX - (col * halfHOverlay)
x1 = x + ((col + 0) * halfHSpacing)
x2 = x + ((col + 1) * halfHSpacing)
x3 = x + ((col + 2) * halfHSpacing)
2016-09-21 18:24:26 +02:00
for row in range(rows):
2017-01-23 09:24:31 +10:00
y = originY + (row * halfVOverlay)
if (col % 2) == 0:
y1 = y - (((row * 2) + 0) * halfVSpacing)
y2 = y - (((row * 2) + 1) * halfVSpacing)
y3 = y - (((row * 2) + 2) * halfVSpacing)
else:
y1 = y - (((row * 2) + 1) * halfVSpacing)
y2 = y - (((row * 2) + 2) * halfVSpacing)
y3 = y - (((row * 2) + 3) * halfVSpacing)
2014-10-03 09:49:29 +03:00
polyline = []
polyline.append(QgsPointXY(x1, y2))
polyline.append(QgsPointXY(x2, y1))
polyline.append(QgsPointXY(x3, y2))
polyline.append(QgsPointXY(x2, y3))
polyline.append(QgsPointXY(x1, y2))
2014-10-03 09:49:29 +03:00
ft.setGeometry(QgsGeometry.fromPolygon([polyline]))
ft.setAttributes([x1, y1, x3, y3, id])
sink.addFeature(ft, QgsFeatureSink.FastInsert)
id += 1
count += 1
if int(math.fmod(count, count_update)) == 0:
feedback.setProgress(int(count / cells * 100))
2014-10-03 09:49:29 +03:00
2017-06-09 15:31:25 +10:00
def _hexagonGrid(self, sink, width, height, originX, originY,
hSpacing, vSpacing, hOverlay, vOverlay, feedback):
2014-10-03 09:49:29 +03:00
ft = QgsFeature()
# To preserve symmetry, hspacing is fixed relative to vspacing
xVertexLo = 0.288675134594813 * vSpacing
xVertexHi = 0.577350269189626 * vSpacing
2014-10-03 09:49:29 +03:00
hSpacing = xVertexLo + xVertexHi
hOverlay = hSpacing - hOverlay
if hOverlay < 0:
raise GeoAlgorithmExecutionException(
self.tr('To preserve symmetry, hspacing is fixed relative to vspacing\n \
2017-03-04 16:23:36 +01:00
hspacing is fixed at: {0} and hoverlay is fixed at: {1}\n \
hoverlay cannot be negative. Increase hoverlay.').format(hSpacing, hOverlay)
)
halfVSpacing = vSpacing / 2.0
columns = int(math.ceil(float(width) / hOverlay))
rows = int(math.ceil(float(height) / (vSpacing - vOverlay)))
cells = rows * columns
count_update = cells * 0.05
id = 1
count = 0
2016-09-21 18:24:26 +02:00
for col in range(columns):
2017-06-09 15:31:25 +10:00
if feedback.isCanceled():
break
# (column + 1) and (row + 1) calculation is used to maintain
# topology between adjacent shapes and avoid overlaps/holes
# due to rounding errors
x1 = originX + (col * hOverlay) # far left
x2 = x1 + (xVertexHi - xVertexLo) # left
x3 = originX + (col * hOverlay) + hSpacing # right
x4 = x3 + (xVertexHi - xVertexLo) # far right
2016-09-21 18:24:26 +02:00
for row in range(rows):
if (col % 2) == 0:
y1 = originY + (row * vOverlay) - (((row * 2) + 0) * halfVSpacing) # hi
y2 = originY + (row * vOverlay) - (((row * 2) + 1) * halfVSpacing) # mid
y3 = originY + (row * vOverlay) - (((row * 2) + 2) * halfVSpacing) # lo
else:
y1 = originY + (row * vOverlay) - (((row * 2) + 1) * halfVSpacing) # hi
y2 = originY + (row * vOverlay) - (((row * 2) + 2) * halfVSpacing) # mid
y3 = originY + (row * vOverlay) - (((row * 2) + 3) * halfVSpacing) # lo
2014-10-03 09:49:29 +03:00
polyline = []
polyline.append(QgsPointXY(x1, y2))
polyline.append(QgsPointXY(x2, y1))
polyline.append(QgsPointXY(x3, y1))
polyline.append(QgsPointXY(x4, y2))
polyline.append(QgsPointXY(x3, y3))
polyline.append(QgsPointXY(x2, y3))
polyline.append(QgsPointXY(x1, y2))
2014-10-03 09:49:29 +03:00
ft.setGeometry(QgsGeometry.fromPolygon([polyline]))
ft.setAttributes([x1, y1, x4, y3, id])
sink.addFeature(ft, QgsFeatureSink.FastInsert)
id += 1
count += 1
if int(math.fmod(count, count_update)) == 0:
feedback.setProgress(int(count / cells * 100))