Skip to content

Instantly share code, notes, and snippets.

Last active July 22, 2021 07:38
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Waterlining function for QGIS
from qgis.core import *
@qgsfunction(args='auto', group='Custom')
def waterlines(inc, start, nb_max, grow_ratio, feature, parent):
Creates repeated buffered polygons
for a waterlining effect like on old engraved maps.
<td>Distance increment between each line, in current "units". Negative for inner lines, positive for outer.</td>
<td>Distance where to draw the first line.</td>
<td>Max number of lines. If -1, fills the entire space.</td>
<td>Multiplication factor applied at each step. 1 to get equally spaced lines, >1 for increasing distance each time.</td>
<h2>Example usage:</h2>
<li>waterlines(-50, 0, -1, 1) -> equally spaced lines, spaced by 50, filling the entire polygon.</li>
<li>waterlines(50, 0, 10, 1.25) -> 10 lines, growing from the polygon, spaced by 50 in the first iteration, then growing by a 1.25 factor.</li>
geom = feature.geometry()
if geom.type() != QgsWkbTypes.PolygonGeometry:
return NULL
dist = start
lines = []
nb_loops = 500 if nb_max < 0 else nb_max # use 500 as default max to prevent infinite loops
while nb_loops > 0:
line = geom.buffer(dist, 8)
if line.isNull() or line.isEmpty() or not line.isGeosValid():
dist = (dist + inc) * grow_ratio
nb_loops -= 1
return QgsGeometry.collectGeometry(lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment