2

I am a self-taught QGIS user and am trying my hand at the geometry generator. I have been tasked with creating a map that has gazebos as points. There are so many easier ways to get a polygon like the one I am going for (.svg marker), but I'm trying to branch out. I got to this point by following Klas Karlson awesome geometry generator video. Here is my go at making an octagon:

make_polygon(
    make_line(
        make_point($geometry, 0.414, 1.000),
        make_point($geometry, 1.000, 0.414),
        make_point($geometry,  1.000, -0.414),
        make_point($geometry,  0.414, -1.000),
        make_point($geometry,  -0.414, -1.000),
        make_point($geometry, -1.000, -0.414),
        make_point($geometry, -1.000, 0.414),
        make_point($geometry, -0.414, 1.000)
))

This is what I got so far:

10-sided "octagon" 10-sided "octagon"

Klas Karlson Geometry Generator Video

2
  • 1
    Why do you have $geometry as argument of make_point function? This makes no sense as the function expects coordinates as arguments.
    – Babel
    Commented Jan 11 at 19:44
  • Because I literally don't know what I am doing and was just trying to follow directions from a video. Commented Jan 12 at 14:16

3 Answers 3

13

You can do this much easier with the function make_regular_polygon() like this:

make_regular_polygon(
    $geometry, 
    project($geometry,5,radians(360/16)),
    8
)

5 in line 3 is the size of the radius, radians(360/16) is the rotation for a horizontal upper line, you can change the angle inside the brackets to get other rotations - e.g. 0 for the rotation like in the screenshot:

enter image description here

6

A simple option:

buffer(geometry:=$geometry, distance:=100, segments:=2)

With segments:=2 each quarter circle is created using only two line segments. So 2*4=8 sides in total.

enter image description here

1
  • 3
    Nice (ab)use of buffer :). It might be worth adding that this works because the segments parameter is the number of desired segments per quarter circle.
    – Matt
    Commented Jan 12 at 9:26
5

If you want to reproduce the solution in the video, use this expression with project() function (click the link for help to see how it works). The angle (azimuth) is expected in radians, but you can use degrees and convert it with radians()

make_polygon(
    make_line (
        project ($geometry, 1, radians (45)),
        project ($geometry, 1, radians (90)),
        project ($geometry, 1, radians (135)),
        project ($geometry, 1, radians (180)),
        project ($geometry, 1, radians (225)),
        project ($geometry, 1, radians (270)),
        project ($geometry, 1, radians (315)),
        project ($geometry, 1, radians (360))
    )
)

You can make it shorter by looping 8 times through the project() function with array_foreach():

make_polygon(
    make_line (
        array_foreach (
            generate_series(1,8),
            project($geometry, 1, radians (45*@element))
        )
    )
)

enter image description here

Be aware:

If you try adapting an expression, be careful to copy it correctly: The expression you provided is invalid, so you must have used another expression to create your screenshot: instead of make_point, you used project (as in the video you shared).

A second problem: the 3rd argument is an angle (azimuth in radians), whereas the second argument is a distance. You seem to mix both values, however.

Try to understand how the expression works, what each function does. See the function help in the expression string builder that describes in detail how each function works and what kind of input it requires - alternatively, see Functions List with a detailed description of each function.

Advise: Start with a simple project() function to see how changing the values influences the result. Then go on and add a make_line function with two points, then add a 3rd point etc. When done, use make_polygon.

Your Answer

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

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