Skip to content

Commit 08577ac

Browse files
committed
[processing] fixes for Create Grid algorithm (fix #14303)
1 parent ead88a3 commit 08577ac

File tree

1 file changed

+28
-26
lines changed
  • python/plugins/processing/algs/qgis

1 file changed

+28
-26
lines changed

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

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ def processAlgorithm(self, progress):
8181
height = bbox.height()
8282
centerX = bbox.center().x()
8383
centerY = bbox.center().y()
84-
originX = centerX - width / 2.0
85-
originY = centerY - height / 2.0
84+
#~ originX = centerX - width / 2.0
85+
#~ originY = centerY - height / 2.0
86+
originX = bbox.xMinimum()
87+
originY = bbox.yMaximum()
8688

8789
if hSpacing <= 0 or vSpacing <= 0:
8890
raise GeoAlgorithmExecutionException(
@@ -130,15 +132,15 @@ def _rectangleGridLine(self, writer, width, height, originX, originY,
130132
hSpacing, vSpacing):
131133
ft = QgsFeature()
132134

133-
columns = int(math.floor(float(width) / hSpacing))
134-
rows = int(math.floor(float(height) / vSpacing))
135+
columns = int(math.ceil(float(width) / hSpacing))
136+
rows = int(math.ceil(float(height) / vSpacing))
135137

136138
# Longitude lines
137139
for col in xrange(0, columns + 1):
138140
polyline = []
139141
x = originX + (col * hSpacing)
140142
for row in xrange(0, rows + 1):
141-
y = originY + (row * vSpacing)
143+
y = originY - (row * vSpacing)
142144
polyline.append(QgsPoint(x, y))
143145

144146
ft.setGeometry(QgsGeometry.fromPolyline(polyline))
@@ -148,7 +150,7 @@ def _rectangleGridLine(self, writer, width, height, originX, originY,
148150
# Latitude lines
149151
for row in xrange(0, rows + 1):
150152
polyline = []
151-
y = originY + (row * vSpacing)
153+
y = originY - (row * vSpacing)
152154
for col in xrange(0, columns + 1):
153155
x = originX + (col * hSpacing)
154156
polyline.append(QgsPoint(x, y))
@@ -161,8 +163,8 @@ def _rectangleGridPoly(self, writer, width, height, originX, originY,
161163
hSpacing, vSpacing):
162164
ft = QgsFeature()
163165

164-
columns = int(math.floor(float(width) / hSpacing))
165-
rows = int(math.floor(float(height) / vSpacing))
166+
columns = int(math.ceil(float(width) / hSpacing))
167+
rows = int(math.ceil(float(height) / vSpacing))
166168

167169
for col in xrange(0, columns):
168170
# (column + 1) and (row + 1) calculation is used to maintain
@@ -171,8 +173,8 @@ def _rectangleGridPoly(self, writer, width, height, originX, originY,
171173
x1 = originX + (col * hSpacing)
172174
x2 = originX + ((col + 1) * hSpacing)
173175
for row in xrange(0, rows):
174-
y1 = originY + (row * vSpacing)
175-
y2 = originY + ((row + 1) * vSpacing)
176+
y1 = originY - (row * vSpacing)
177+
y2 = originY - ((row + 1) * vSpacing)
176178

177179
polyline = []
178180
polyline.append(QgsPoint(x1, y1))
@@ -192,8 +194,8 @@ def _diamondGrid(self, writer, width, height, originX, originY,
192194
halfHSpacing = hSpacing / 2
193195
halfVSpacing = vSpacing / 2
194196

195-
columns = int(math.floor(float(width) / halfHSpacing))
196-
rows = int(math.floor(float(height) / vSpacing))
197+
columns = int(math.ceil(float(width) / halfHSpacing))
198+
rows = int(math.ceil(float(height) / vSpacing))
197199

198200
for col in xrange(0, columns):
199201
x1 = originX + ((col + 0) * halfHSpacing)
@@ -202,13 +204,13 @@ def _diamondGrid(self, writer, width, height, originX, originY,
202204

203205
for row in xrange(0, rows):
204206
if (col % 2) == 0:
205-
y1 = originY + (((row * 2) + 0) * halfVSpacing)
206-
y2 = originY + (((row * 2) + 1) * halfVSpacing)
207-
y3 = originY + (((row * 2) + 2) * halfVSpacing)
207+
y1 = originY - (((row * 2) + 0) * halfVSpacing)
208+
y2 = originY - (((row * 2) + 1) * halfVSpacing)
209+
y3 = originY - (((row * 2) + 2) * halfVSpacing)
208210
else:
209-
y1 = originY + (((row * 2) + 1) * halfVSpacing)
210-
y2 = originY + (((row * 2) + 2) * halfVSpacing)
211-
y3 = originY + (((row * 2) + 3) * halfVSpacing)
211+
y1 = originY - (((row * 2) + 1) * halfVSpacing)
212+
y2 = originY - (((row * 2) + 2) * halfVSpacing)
213+
y3 = originY - (((row * 2) + 3) * halfVSpacing)
212214

213215
polyline = []
214216
polyline.append(QgsPoint(x1, y2))
@@ -232,8 +234,8 @@ def _hexagonGrid(self, writer, width, height, originX, originY,
232234

233235
halfVSpacing = vSpacing / 2
234236

235-
columns = int(math.floor(float(width) / hSpacing))
236-
rows = int(math.floor(float(height) / vSpacing))
237+
columns = int(math.ceil(float(width) / hSpacing))
238+
rows = int(math.ceil(float(height) / vSpacing))
237239

238240
for col in xrange(0, columns):
239241
# (column + 1) and (row + 1) calculation is used to maintain
@@ -246,13 +248,13 @@ def _hexagonGrid(self, writer, width, height, originX, originY,
246248

247249
for row in xrange(0, rows):
248250
if (col % 2) == 0:
249-
y1 = originY + (((row * 2) + 0) * halfVSpacing) # hi
250-
y2 = originY + (((row * 2) + 1) * halfVSpacing) # mid
251-
y3 = originY + (((row * 2) + 2) * halfVSpacing) # lo
251+
y1 = originY - (((row * 2) + 0) * halfVSpacing) # hi
252+
y2 = originY - (((row * 2) + 1) * halfVSpacing) # mid
253+
y3 = originY - (((row * 2) + 2) * halfVSpacing) # lo
252254
else:
253-
y1 = originY + (((row * 2) + 1) * halfVSpacing) # hi
254-
y2 = originY + (((row * 2) + 2) * halfVSpacing) # mid
255-
y3 = originY + (((row * 2) + 3) * halfVSpacing) # lo
255+
y1 = originY - (((row * 2) + 1) * halfVSpacing) # hi
256+
y2 = originY - (((row * 2) + 2) * halfVSpacing) # mid
257+
y3 = originY - (((row * 2) + 3) * halfVSpacing) # lo
256258

257259
polyline = []
258260
polyline.append(QgsPoint(x1, y2))

0 commit comments

Comments
 (0)