13

Using QGIS 2.18.3, I would like to apply a random point pattern to my polygon symbology. The default Style Manager has a regularly-spaced point pattern, called Cemetery-25-50k (please see image below). This is a good starting point, but obviously not random.

enter image description here

Alternatively, I have reviewed the Point Pattern fill options within the Layer Properties (below), but nothing appears that would generate a random pattern.

enter image description here

What I need to recreate is the ESRI Arcmap random point symbology, shown here:

enter image description here

1
  • one possibility (which allows more control over randomness, and also use abritrary symbols like trees) is to use inkscape to create an SVG (using tiled clones with randomness), and use an SVG fill.
    – Steven Kay
    Apr 11, 2017 at 19:43

4 Answers 4

15

You can add random point symbology using the following steps:

  1. Add new symbol layer using the '+' button:

enter image description here

  1. Change the Symbol layer type to 'Point pattern fill'
  2. Highlight 'Point pattern fill' in the left side tree
  3. Change the horizontal and vertical distances to reasonable values. In this example, I chose the same value for both distances of 2.0
  4. Under the Horizontal displacement and vertical displacement, use the following functions:

    5.1 For Horizontal displacement:

    randf(3,5) 
    

    5.2 For Vertical displacement

    randf(2,4) 
    
  5. Duplicate the point layer and change the horizontal and vertical distances to 6 and 3, respectively. Under the Horizontal displacement and vertical displacement, use the following functions:

    6.1 For Horizontal displacement:

    randf(0,1) 
    

    6.2 For Vertical displacement

    randf(1,2) 
    
  6. You need to reduce the symbol size to 2 Pixels

You can see the output in the following image:

enter image description here

In the composer, the legend will look like this:

enter image description here

3
  • 3
    It doesn't quite duplicate the randomness of the built-in Arc symbology but that's a nice solution. To increase the randomness, use the randf() function on the horizontal and vertical distances and also duplicate the fill several times. Apr 11, 2017 at 8:21
  • 1
    @EdRollason Thanks for your comment. It is also a good idea to add several layers with randf() function.
    – ahmadhanb
    Apr 11, 2017 at 9:02
  • 1
    @ahmadhanb Interesting solution, bookmarked!
    – mgri
    Apr 11, 2017 at 9:54
3

another technique involves using InkScape to create an SVG, and using an SVG fill layer in QGIS.

It allows more control over symbology (e.g. you could draw randomly scattered trees, not just points) and more control over spacing

  • In Inkscape, draw a circle/tree, and select the object
  • Edit > Clone > Tiled clones
  • Create (say) 5 x 5 or 10x10 tiled clones. It's a good idea to keep the size of the grid small as QGIS will struggle if you have too many symbols.
  • In the Shift tab, choose something like this... use the default settings is ok, but change the Randomise setting to add 'jitter'.

Experiment with the settings, you can always click on Remove if the result isn't good.

enter image description here

  • Click Create.
  • Select all tiled clones using CtrlA
  • CtrlShiftD to bring up document properties
  • Select Resize page to drawing or selection
  • Save as an SVG, but make sure you use Simple SVG format
  • Now bring into QGIS as an SVG symbol fill.

Here's one where I made a random grid of 5 x 5 copies of one of the QGIS built-in trees. By experimenting with spacing and jitter you can get different looks. You can also experiment with QGIS' Horizontal Displacement. By setting that to half the horizontal distance, it breaks up the "tiled" look (giving a pattern like bricks in a wall)

enter image description here

3

Just found online script that with with some adjustments work with qgis 3.5.x

I lost the original post so cant credit the author.

What you do is:

  • create fill layer change it to "geometry generator"
  • change "geometry type" to points
  • for the expression click the "sigma" button right to the textfield
  • in the "expression dialog" window change tab to the "Function editor" and paste the code below there
  • Now back to the "expression dialog" paste the function call like that: fillGrid(0.001,0.001,1) (first 2 values are the random size)
  • Save changes and update view.
  • Awesome random points are there.

Thanks to original author of the script.

from qgis.core import *
from qgis.gui import *
import math
import random

"""
Define a grid based on the interval and the bounding box of
the feature. Grid will minimally cover the feature and be centre aligned

Create a multi-point geometry at the grid intersections where
the grid is enclosed by the feature - i.e. apply a clipping mask

Random value determines amount of randomness in X/Y within its
grid square a particular feature is allowed to have
"""
@qgsfunction(args='auto', group='Custom')
def fillGrid(xInterval, yInterval, rand, feature, parent):
  box = feature.geometry().boundingBox()

  #Create a grid that minimally covers the boundary
  #using the supplied intervals and centre it
  countX = math.ceil(box.width() / xInterval)
  countY = math.ceil(box.height() / yInterval)

  #Align the grid
  gridX = countX * xInterval
  gridY = countY * yInterval
  dX= gridX - box.width()
  dY= gridY - box.height()
  xMin = box.xMinimum() - (dX/2)
  yMin = box.yMinimum() - (dY/2)

  points = []
  #+1 to draw a symbol on the n+1th grid element
  for xOff in range(countX+1):
    for yOff in range(countY+1):

      ptX = xMin + xOff*(xInterval) + rand * random.uniform(0,xInterval)
      ptY = yMin + yOff*(yInterval) + rand * random.uniform(0,xInterval)

      pt = QgsPointXY(ptX,ptY)
      point = QgsGeometry.fromPointXY(pt)
      if feature.geometry().contains(point):
        points.append(pt)

  return QgsGeometry.fromMultiPointXY(points)
0
1

Might have found a quick workaround that might come in handy for some:

Go to Vector and under Research tools and select the Random points inside polygons algorithm (although some others here might do the trick as well). Use your existing polygon as the input parameter and set other parameters to your liking.

enter image description here

It creates a new point layer with randomly dispersed points within the polygon boundary (in my case the brown points are those i created - the layer below is the result from the earlier solutions).

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.