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.
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
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.
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