# Using QGIS Geometry Generator to get rectangle from point?

I would like to translate a point (from a point layer) to a rectangle polygon using the new Geometry Generator from QGIS 2.14.

I know the length and the height of the resulting rectangle. The point should be the centroid.

While you can't do maths inside the WKT representation - you can use geom_from_wkt to turn a text string with maths in it back into a geometry. Something like:

``````geom_from_wkt(
'POLYGON(('||
(x( centroid( \$geometry) ) + 0.5)||' '||(y( centroid( \$geometry) ) + 0.5)||','||
(x( centroid( \$geometry) ) + 0.5)||' '||(y( centroid( \$geometry) ) - 0.5)||','||
(x( centroid( \$geometry) ) - 0.5)||' '||(y( centroid( \$geometry) ) - 0.5)||','||
(x( centroid( \$geometry) ) - 0.5)||' '||(y( centroid( \$geometry) ) + 0.5)||','||
(x( centroid( \$geometry) ) + 0.5)||' '||(y( centroid( \$geometry) ) + 0.5)||','||
'))')
``````

My answer comes late, but should help anyway...

When using the geometry generator, you must think like a CAD designer.

The following formula, based on X_DIM and Y_DIM fields, will do the trick just right. It creates a line with 4 points that makes a diamond shape then creates the rectangle boundary of it.

``````bounds(make_line( make_point( \$x- ("X_DIM" /2),\$y) , make_point( \$x+ ("X_DIM" /2),\$y) ,make_point( \$x,\$y- ("Y_DIM" /2)) , make_point( \$x,\$y+ ("Y_DIM" /2))  ) )
``````

EDIT : even simpler, as suggested by @tudorbarascu, you can aswell make a diagonal with 2 points only and make the boundary of it.

• It would be simpler to just create the diagonal line (2 points) and create the bbox around it. Commented Aug 12, 2017 at 8:45
• indeed ! Simple is beautiful :) Commented Aug 13, 2017 at 8:55

Personally, in QGIS 3, I couldn't get the above bounds method to work (sorry Ian, didn't try yours). So, anyway, I just thought in more simple terms:

``````bounds(buffer(\$geometry, 500))
``````

Replace 500 with your own square dimension

• Wouldn't this create a circular buffer rather than a rectangular buffer? I don't see how this answers the question. Please edit your post to include more details about how this would create a rectangular buffer. Commented Mar 20, 2018 at 23:26
• It creates a square bounding box around a circular buffer. The result is just a load of squares where the points were. I think that answers the question? Commented Mar 20, 2018 at 23:50
• I stand corrected, I neglected to read the `bounds` part. Commented Mar 20, 2018 at 23:52
• I have an issue very similar to the problem above. I need a rectangle which is 3.0km length by 3.58km width. I am also a complete beginner to QGIS. What would I need to type into the Geometry generator to get a rectangle of these dimentions?. Would I need to input this data within a Text file and upload the data in QGIS first, would be the geometry generator calculate it for me? Commented Nov 26, 2018 at 14:35
• Super clever! Thank you! Commented Feb 8 at 21:38

It is possible to draw a circle marker with

``````buffer(  \$geometry ,0.01)
``````

This works also and draws a fixed polygon

`````` geom_from_wkt( 'POLYGON (( 377 380, 613 474, 343 115, 377 380 ))')
``````

I thought that building a polygon around a point would work like this:

``````geom_from_wkt('POLYGON ((
\$x-0.01 \$y-0.01,
\$x-0.01 \$y+0.01,
\$x+0.01 \$y+0.01,
\$x+0.01 \$y-0.01
\$x-0.01 \$y-0.01))')
``````

However, it doesn't. I may have a wrong syntax or then Geometry generator does not support calculations inside WKT, which is actually not surprising. Making a buffer and taking an envelope of the buffered geometry was the next thing I was considering but Geometry generator does not have Envelope function.

• "Envelope function" : have a try with "bounds()" > see my answer Commented Aug 3, 2017 at 8:48

I had the same issue, I used the following:

``````rotate(make_line(
make_point(x(\$geometry)-"W"/2000,y(\$geometry)+"L"/2000),
make_point(x(\$geometry)+"W"/2000,y(\$geometry)+"L"/2000),
make_point(x(\$geometry)+"W"/2000,y(\$geometry)-"L"/2000),
make_point(x(\$geometry)-"W"/2000,y(\$geometry)-"L"/2000),
make_point(x(\$geometry)-"W"/2000,y(\$geometry)+"L"/2000)
),"angle")
``````

the starting geometry is point layer W and L are the side dimensions in mm, project is in m with the field "angle" the multiline rotates accordingly geometry type is to set on LineString/MultiString