2014-10-03 10:29:54 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
Gridify.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$'
|
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
from qgis.core import (QgsGeometry,
|
|
|
|
QgsFeature,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-06-01 12:18:43 +02:00
|
|
|
QgsPointXY,
|
2017-03-29 10:42:42 +10:00
|
|
|
QgsWkbTypes,
|
2017-04-25 17:53:32 +10:00
|
|
|
QgsApplication,
|
2017-04-24 14:35:50 +10:00
|
|
|
QgsMessageLog,
|
2017-04-25 17:53:32 +10:00
|
|
|
QgsProcessingUtils)
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
2014-10-03 10:29:54 +03:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterNumber
|
|
|
|
from processing.core.outputs import OutputVector
|
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class Gridify(QgisAlgorithm):
|
2014-10-03 10:29:54 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
HSPACING = 'HSPACING'
|
|
|
|
VSPACING = 'VSPACING'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector general tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input Layer')))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterNumber(self.HSPACING,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Horizontal spacing'), default=0.1))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterNumber(self.VSPACING,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Vertical spacing'), default=0.1))
|
2014-10-03 10:29:54 +03:00
|
|
|
|
2015-05-22 15:56:45 +02:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Snapped')))
|
2014-10-03 10:29:54 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'snappointstogrid'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Snap points to grid')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-05-02 13:40:49 +10:00
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
|
2014-10-03 10:29:54 +03:00
|
|
|
hSpacing = self.getParameterValue(self.HSPACING)
|
|
|
|
vSpacing = self.getParameterValue(self.VSPACING)
|
|
|
|
|
|
|
|
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 10:29:54 +03:00
|
|
|
|
2017-04-26 14:33:53 +10:00
|
|
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.fields(), layer.wkbType(), layer.crs(),
|
|
|
|
context)
|
2014-10-03 10:29:54 +03:00
|
|
|
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
|
2014-10-03 10:29:54 +03:00
|
|
|
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, f in enumerate(features):
|
2014-10-03 10:29:54 +03:00
|
|
|
geom = f.geometry()
|
|
|
|
geomType = geom.wkbType()
|
|
|
|
|
2016-08-04 09:10:08 +02:00
|
|
|
if geomType == QgsWkbTypes.Point:
|
2014-10-03 10:29:54 +03:00
|
|
|
points = self._gridify([geom.asPoint()], hSpacing, vSpacing)
|
|
|
|
newGeom = QgsGeometry.fromPoint(points[0])
|
2016-08-04 09:10:08 +02:00
|
|
|
elif geomType == QgsWkbTypes.MultiPoint:
|
2014-10-03 10:29:54 +03:00
|
|
|
points = self._gridify(geom.aMultiPoint(), hSpacing, vSpacing)
|
|
|
|
newGeom = QgsGeometry.fromMultiPoint(points)
|
2016-08-04 09:10:08 +02:00
|
|
|
elif geomType == QgsWkbTypes.LineString:
|
2014-10-03 10:29:54 +03:00
|
|
|
points = self._gridify(geom.asPolyline(), hSpacing, vSpacing)
|
|
|
|
if len(points) < 2:
|
2017-04-26 12:52:23 +10:00
|
|
|
QgsMessageLog.logMessage(self.tr('Failed to gridify feature with FID {0}').format(f.id()), self.tr('Processing'), QgsMessageLog.INFO)
|
2014-10-03 10:29:54 +03:00
|
|
|
newGeom = None
|
|
|
|
else:
|
|
|
|
newGeom = QgsGeometry.fromPolyline(points)
|
2016-08-04 09:10:08 +02:00
|
|
|
elif geomType == QgsWkbTypes.MultiLineString:
|
2014-10-03 10:29:54 +03:00
|
|
|
polyline = []
|
|
|
|
for line in geom.asMultiPolyline():
|
|
|
|
points = self._gridify(line, hSpacing, vSpacing)
|
|
|
|
if len(points) > 1:
|
|
|
|
polyline.append(points)
|
|
|
|
if len(polyline) <= 0:
|
2017-04-26 12:52:23 +10:00
|
|
|
QgsMessageLog.logMessage(self.tr('Failed to gridify feature with FID {0}').format(f.id()), self.tr('Processing'), QgsMessageLog.INFO)
|
2014-10-03 10:29:54 +03:00
|
|
|
newGeom = None
|
|
|
|
else:
|
|
|
|
newGeom = QgsGeometry.fromMultiPolyline(polyline)
|
|
|
|
|
2016-08-04 09:10:08 +02:00
|
|
|
elif geomType == QgsWkbTypes.Polygon:
|
2014-10-03 10:29:54 +03:00
|
|
|
polygon = []
|
|
|
|
for line in geom.asPolygon():
|
|
|
|
points = self._gridify(line, hSpacing, vSpacing)
|
|
|
|
if len(points) > 1:
|
|
|
|
polygon.append(points)
|
|
|
|
if len(polygon) <= 0:
|
2017-04-26 12:52:23 +10:00
|
|
|
QgsMessageLog.logMessage(self.tr('Failed to gridify feature with FID {0}').format(f.id()), self.tr('Processing'), QgsMessageLog.INFO)
|
2014-10-03 10:29:54 +03:00
|
|
|
newGeom = None
|
|
|
|
else:
|
|
|
|
newGeom = QgsGeometry.fromPolygon(polygon)
|
2016-08-04 09:10:08 +02:00
|
|
|
elif geomType == QgsWkbTypes.MultiPolygon:
|
2014-10-03 10:29:54 +03:00
|
|
|
multipolygon = []
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
for polygon in geom.asMultiPolygon():
|
2014-10-03 10:29:54 +03:00
|
|
|
newPolygon = []
|
|
|
|
for line in polygon:
|
|
|
|
points = self._gridify(line, hSpacing, vSpacing)
|
|
|
|
if len(points) > 2:
|
|
|
|
newPolygon.append(points)
|
|
|
|
|
|
|
|
if len(newPolygon) > 0:
|
|
|
|
multipolygon.append(newPolygon)
|
|
|
|
|
|
|
|
if len(multipolygon) <= 0:
|
2017-04-26 12:52:23 +10:00
|
|
|
QgsMessageLog.logMessage(self.tr('Failed to gridify feature with FID {0}').format(f.id()), self.tr('Processing'), QgsMessageLog.INFO)
|
2014-10-03 10:29:54 +03:00
|
|
|
newGeom = None
|
|
|
|
else:
|
|
|
|
newGeom = QgsGeometry.fromMultiPolygon(multipolygon)
|
|
|
|
|
|
|
|
if newGeom is not None:
|
|
|
|
feat = QgsFeature()
|
|
|
|
feat.setGeometry(newGeom)
|
|
|
|
feat.setAttributes(f.attributes())
|
2017-06-23 14:34:38 +10:00
|
|
|
writer.addFeature(feat, QgsFeatureSink.FastInsert)
|
2014-10-03 10:29:54 +03:00
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2014-10-03 10:29:54 +03:00
|
|
|
|
|
|
|
del writer
|
|
|
|
|
|
|
|
def _gridify(self, points, hSpacing, vSpacing):
|
|
|
|
nPoints = []
|
|
|
|
for p in points:
|
2017-06-01 12:18:43 +02:00
|
|
|
nPoints.append(QgsPointXY(round(p.x() / hSpacing, 0) * hSpacing,
|
|
|
|
round(p.y() / vSpacing, 0) * vSpacing))
|
2014-10-03 10:29:54 +03:00
|
|
|
|
|
|
|
i = 0
|
|
|
|
# Delete overlapping points
|
|
|
|
while i < len(nPoints) - 2:
|
|
|
|
if nPoints[i] == nPoints[i + 1]:
|
|
|
|
nPoints.pop(i + 1)
|
|
|
|
else:
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
# Delete line points that go out and return to the same place
|
|
|
|
while i < len(nPoints) - 3:
|
|
|
|
if nPoints[i] == nPoints[i + 2]:
|
|
|
|
nPoints.pop(i + 1)
|
|
|
|
nPoints.pop(i + 1)
|
|
|
|
|
|
|
|
# Step back to catch arcs
|
|
|
|
if i > 0:
|
|
|
|
i -= 1
|
|
|
|
else:
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
# Delete overlapping start/end points
|
|
|
|
while len(nPoints) > 1 and nPoints[0] == nPoints[len(nPoints) - 1]:
|
|
|
|
nPoints.pop(len(nPoints) - 1)
|
|
|
|
|
|
|
|
return nPoints
|