Skip to content

Commit

Permalink
[processing] fixes for Create Grid algorithm (fix #14303)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Feb 17, 2016
1 parent ead88a3 commit 08577ac
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions python/plugins/processing/algs/qgis/Grid.py
Expand Up @@ -81,8 +81,10 @@ def processAlgorithm(self, progress):
height = bbox.height() height = bbox.height()
centerX = bbox.center().x() centerX = bbox.center().x()
centerY = bbox.center().y() centerY = bbox.center().y()
originX = centerX - width / 2.0 #~ originX = centerX - width / 2.0
originY = centerY - height / 2.0 #~ originY = centerY - height / 2.0
originX = bbox.xMinimum()
originY = bbox.yMaximum()


if hSpacing <= 0 or vSpacing <= 0: if hSpacing <= 0 or vSpacing <= 0:
raise GeoAlgorithmExecutionException( raise GeoAlgorithmExecutionException(
Expand Down Expand Up @@ -130,15 +132,15 @@ def _rectangleGridLine(self, writer, width, height, originX, originY,
hSpacing, vSpacing): hSpacing, vSpacing):
ft = QgsFeature() ft = QgsFeature()


columns = int(math.floor(float(width) / hSpacing)) columns = int(math.ceil(float(width) / hSpacing))
rows = int(math.floor(float(height) / vSpacing)) rows = int(math.ceil(float(height) / vSpacing))


# Longitude lines # Longitude lines
for col in xrange(0, columns + 1): for col in xrange(0, columns + 1):
polyline = [] polyline = []
x = originX + (col * hSpacing) x = originX + (col * hSpacing)
for row in xrange(0, rows + 1): for row in xrange(0, rows + 1):
y = originY + (row * vSpacing) y = originY - (row * vSpacing)
polyline.append(QgsPoint(x, y)) polyline.append(QgsPoint(x, y))


ft.setGeometry(QgsGeometry.fromPolyline(polyline)) ft.setGeometry(QgsGeometry.fromPolyline(polyline))
Expand All @@ -148,7 +150,7 @@ def _rectangleGridLine(self, writer, width, height, originX, originY,
# Latitude lines # Latitude lines
for row in xrange(0, rows + 1): for row in xrange(0, rows + 1):
polyline = [] polyline = []
y = originY + (row * vSpacing) y = originY - (row * vSpacing)
for col in xrange(0, columns + 1): for col in xrange(0, columns + 1):
x = originX + (col * hSpacing) x = originX + (col * hSpacing)
polyline.append(QgsPoint(x, y)) polyline.append(QgsPoint(x, y))
Expand All @@ -161,8 +163,8 @@ def _rectangleGridPoly(self, writer, width, height, originX, originY,
hSpacing, vSpacing): hSpacing, vSpacing):
ft = QgsFeature() ft = QgsFeature()


columns = int(math.floor(float(width) / hSpacing)) columns = int(math.ceil(float(width) / hSpacing))
rows = int(math.floor(float(height) / vSpacing)) rows = int(math.ceil(float(height) / vSpacing))


for col in xrange(0, columns): for col in xrange(0, columns):
# (column + 1) and (row + 1) calculation is used to maintain # (column + 1) and (row + 1) calculation is used to maintain
Expand All @@ -171,8 +173,8 @@ def _rectangleGridPoly(self, writer, width, height, originX, originY,
x1 = originX + (col * hSpacing) x1 = originX + (col * hSpacing)
x2 = originX + ((col + 1) * hSpacing) x2 = originX + ((col + 1) * hSpacing)
for row in xrange(0, rows): for row in xrange(0, rows):
y1 = originY + (row * vSpacing) y1 = originY - (row * vSpacing)
y2 = originY + ((row + 1) * vSpacing) y2 = originY - ((row + 1) * vSpacing)


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


columns = int(math.floor(float(width) / halfHSpacing)) columns = int(math.ceil(float(width) / halfHSpacing))
rows = int(math.floor(float(height) / vSpacing)) rows = int(math.ceil(float(height) / vSpacing))


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


for row in xrange(0, rows): for row in xrange(0, rows):
if (col % 2) == 0: if (col % 2) == 0:
y1 = originY + (((row * 2) + 0) * halfVSpacing) y1 = originY - (((row * 2) + 0) * halfVSpacing)
y2 = originY + (((row * 2) + 1) * halfVSpacing) y2 = originY - (((row * 2) + 1) * halfVSpacing)
y3 = originY + (((row * 2) + 2) * halfVSpacing) y3 = originY - (((row * 2) + 2) * halfVSpacing)
else: else:
y1 = originY + (((row * 2) + 1) * halfVSpacing) y1 = originY - (((row * 2) + 1) * halfVSpacing)
y2 = originY + (((row * 2) + 2) * halfVSpacing) y2 = originY - (((row * 2) + 2) * halfVSpacing)
y3 = originY + (((row * 2) + 3) * halfVSpacing) y3 = originY - (((row * 2) + 3) * halfVSpacing)


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


halfVSpacing = vSpacing / 2 halfVSpacing = vSpacing / 2


columns = int(math.floor(float(width) / hSpacing)) columns = int(math.ceil(float(width) / hSpacing))
rows = int(math.floor(float(height) / vSpacing)) rows = int(math.ceil(float(height) / vSpacing))


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


for row in xrange(0, rows): for row in xrange(0, rows):
if (col % 2) == 0: if (col % 2) == 0:
y1 = originY + (((row * 2) + 0) * halfVSpacing) # hi y1 = originY - (((row * 2) + 0) * halfVSpacing) # hi
y2 = originY + (((row * 2) + 1) * halfVSpacing) # mid y2 = originY - (((row * 2) + 1) * halfVSpacing) # mid
y3 = originY + (((row * 2) + 2) * halfVSpacing) # lo y3 = originY - (((row * 2) + 2) * halfVSpacing) # lo
else: else:
y1 = originY + (((row * 2) + 1) * halfVSpacing) # hi y1 = originY - (((row * 2) + 1) * halfVSpacing) # hi
y2 = originY + (((row * 2) + 2) * halfVSpacing) # mid y2 = originY - (((row * 2) + 2) * halfVSpacing) # mid
y3 = originY + (((row * 2) + 3) * halfVSpacing) # lo y3 = originY - (((row * 2) + 3) * halfVSpacing) # lo


polyline = [] polyline = []
polyline.append(QgsPoint(x1, y2)) polyline.append(QgsPoint(x1, y2))
Expand Down

0 comments on commit 08577ac

Please sign in to comment.