Skip to content

Commit d4dbb92

Browse files
committedOct 3, 2014
refactor Gridify alg. fix license headers
1 parent 98226c5 commit d4dbb92

File tree

7 files changed

+188
-6
lines changed

7 files changed

+188
-6
lines changed
 

‎python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
from mmqgisx.ExtractByAttribute import ExtractByAttribute
8080
from mmqgisx.SelectByAttribute import SelectByAttribute
8181
from mmqgisx.Grid import Grid
82+
from mmqgisx.Gridify import Gridify
8283

8384
from ConcaveHull import ConcaveHull
8485
from Polygonize import Polygonize
@@ -145,7 +146,7 @@ def __init__(self):
145146
# ------ mmqgisx ------
146147
DeleteColumn(), DeleteDuplicateGeometries(),
147148
TextToFloat(), ExtractByAttribute(),
148-
SelectByAttribute(), Grid(),
149+
SelectByAttribute(), Grid(), Gridify(),
149150
#mmqgisx_delete_duplicate_geometries_algorithm(),
150151
#mmqgisx_geometry_convert_algorithm(),
151152
#mmqgisx_grid_algorithm(),

‎python/plugins/processing/algs/qgis/mmqgisx/DeleteDuplicateGeometries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
"""
44
***************************************************************************
5-
DeleteColumn.py
5+
DeleteDuplicateGeometries.py
66
---------------------
77
Date : May 2010
88
Copyright : (C) 2010 by Michael Minn

‎python/plugins/processing/algs/qgis/mmqgisx/ExtractByAttribute.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
"""
44
***************************************************************************
5-
DeleteColumn.py
5+
ExtractByAttribute.py
66
---------------------
77
Date : May 2010
88
Copyright : (C) 2010 by Michael Minn

‎python/plugins/processing/algs/qgis/mmqgisx/Grid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
"""
44
***************************************************************************
5-
DeleteColumn.py
5+
Grid.py
66
---------------------
77
Date : May 2010
88
Copyright : (C) 2010 by Michael Minn
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
Gridify.py
6+
---------------------
7+
Date : May 2010
8+
Copyright : (C) 2010 by Michael Minn
9+
Email : pyqgis at michaelminn dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Michael Minn'
21+
__date__ = 'May 2010'
22+
__copyright__ = '(C) 2010, Michael Minn'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from PyQt4.QtCore import *
29+
from qgis.core import *
30+
from processing.core.GeoAlgorithm import GeoAlgorithm
31+
from processing.core.GeoAlgorithmExecutionException import \
32+
GeoAlgorithmExecutionException
33+
from processing.core.ProcessingLog import ProcessingLog
34+
from processing.core.parameters import ParameterVector
35+
from processing.core.parameters import ParameterNumber
36+
from processing.core.outputs import OutputVector
37+
38+
from processing.tools import dataobjects, vector
39+
40+
class Gridify(GeoAlgorithm):
41+
INPUT = 'INPUT'
42+
HSPACING = 'HSPACING'
43+
VSPACING = 'VSPACING'
44+
OUTPUT = 'OUTPUT'
45+
46+
def defineCharacteristics(self):
47+
self.name = 'Snap points to grid'
48+
self.group = 'Vector general tools'
49+
50+
self.addParameter(ParameterVector(
51+
self.INPUT, 'Input Layer', [ParameterVector.VECTOR_TYPE_ANY]))
52+
self.addParameter(ParameterNumber(
53+
self.HSPACING, 'Horizontal spacing', default=0.1))
54+
self.addParameter(ParameterNumber(
55+
self.VSPACING, 'Vertical spacing', default=0.1))
56+
57+
self.addOutput(OutputVector(self.OUTPUT, 'Output'))
58+
59+
def processAlgorithm(self, progress):
60+
layer = dataobjects.getObjectFromUri(
61+
self.getParameterValue(self.INPUT))
62+
hSpacing = self.getParameterValue(self.HSPACING)
63+
vSpacing = self.getParameterValue(self.VSPACING)
64+
65+
if hSpacing <= 0 or vSpacing <= 0:
66+
raise GeoAlgorithmExecutionException(
67+
'Invalid grid spacing: %s/%s' % (hSpacing, vSpacing))
68+
69+
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
70+
layer.pendingFields(), layer.wkbType(), layer.crs())
71+
72+
features = vector.features(layer)
73+
74+
count = len(features)
75+
total = 100.0 / float(count)
76+
77+
for count, f in enumerate(features):
78+
geom = f.geometry()
79+
geomType = geom.wkbType()
80+
81+
if geomType == QGis.WKBPoint:
82+
points = self._gridify([geom.asPoint()], hSpacing, vSpacing)
83+
newGeom = QgsGeometry.fromPoint(points[0])
84+
elif geomType == QGis.WKBMultiPoint:
85+
points = self._gridify(geom.aMultiPoint(), hSpacing, vSpacing)
86+
newGeom = QgsGeometry.fromMultiPoint(points)
87+
elif geomType == QGis.WKBLineString:
88+
points = self._gridify(geom.asPolyline(), hSpacing, vSpacing)
89+
if len(points) < 2:
90+
ProcessingLog.addToLog(Processing.LOG_INFO,
91+
'Failed to gridify feature with FID %s' % f.id())
92+
newGeom = None
93+
else:
94+
newGeom = QgsGeometry.fromPolyline(points)
95+
elif geomType == QGis.WKBMultiLineString:
96+
polyline = []
97+
for line in geom.asMultiPolyline():
98+
points = self._gridify(line, hSpacing, vSpacing)
99+
if len(points) > 1:
100+
polyline.append(points)
101+
if len(polyline) <= 0:
102+
ProcessingLog.addToLog(Processing.LOG_INFO,
103+
'Failed to gridify feature with FID %s' % f.id())
104+
newGeom = None
105+
else:
106+
newGeom = QgsGeometry.fromMultiPolyline(polyline)
107+
108+
elif geomType == QGis.WKBPolygon:
109+
polygon = []
110+
for line in geom.asPolygon():
111+
points = self._gridify(line, hSpacing, vSpacing)
112+
if len(points) > 1:
113+
polygon.append(points)
114+
if len(polygon) <= 0:
115+
ProcessingLog.addToLog(Processing.LOG_INFO,
116+
'Failed to gridify feature with FID %s' % f.id())
117+
newGeom = None
118+
else:
119+
newGeom = QgsGeometry.fromPolygon(polygon)
120+
elif geomType == QGis.WKBMultiPolygon:
121+
multipolygon = []
122+
for polygon in geometry.asMultiPolygon():
123+
newPolygon = []
124+
for line in polygon:
125+
points = self._gridify(line, hSpacing, vSpacing)
126+
if len(points) > 2:
127+
newPolygon.append(points)
128+
129+
if len(newPolygon) > 0:
130+
multipolygon.append(newPolygon)
131+
132+
if len(multipolygon) <= 0:
133+
ProcessingLog.addToLog(Processing.LOG_INFO,
134+
'Failed to gridify feature with FID %s' % f.id())
135+
newGeom = None
136+
else:
137+
newGeom = QgsGeometry.fromMultiPolygon(multipolygon)
138+
139+
if newGeom is not None:
140+
feat = QgsFeature()
141+
feat.setGeometry(newGeom)
142+
feat.setAttributes(f.attributes())
143+
writer.addFeature(feat)
144+
145+
progress.setPercentage(int(count * total))
146+
147+
del writer
148+
149+
def _gridify(self, points, hSpacing, vSpacing):
150+
nPoints = []
151+
for p in points:
152+
nPoints.append(QgsPoint(round(p.x() / hSpacing, 0) * hSpacing,
153+
round(p.y() / vSpacing, 0) * vSpacing))
154+
155+
i = 0
156+
# Delete overlapping points
157+
while i < len(nPoints) - 2:
158+
if nPoints[i] == nPoints[i + 1]:
159+
nPoints.pop(i + 1)
160+
else:
161+
i += 1
162+
163+
i = 0
164+
# Delete line points that go out and return to the same place
165+
while i < len(nPoints) - 3:
166+
if nPoints[i] == nPoints[i + 2]:
167+
nPoints.pop(i + 1)
168+
nPoints.pop(i + 1)
169+
170+
# Step back to catch arcs
171+
if i > 0:
172+
i -= 1
173+
else:
174+
i += 1
175+
176+
i = 0
177+
# Delete overlapping start/end points
178+
while len(nPoints) > 1 and nPoints[0] == nPoints[len(nPoints) - 1]:
179+
nPoints.pop(len(nPoints) - 1)
180+
181+
return nPoints

‎python/plugins/processing/algs/qgis/mmqgisx/SelectByAttribute.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
"""
44
***************************************************************************
5-
DeleteColumn.py
5+
SelectByAttribute.py
66
---------------------
77
Date : May 2010
88
Copyright : (C) 2010 by Michael Minn

‎python/plugins/processing/algs/qgis/mmqgisx/TextToFloat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
"""
44
***************************************************************************
5-
DeleteColumn.py
5+
TextToFloat.py
66
---------------------
77
Date : May 2010
88
Copyright : (C) 2010 by Michael Minn

0 commit comments

Comments
 (0)
Please sign in to comment.