9

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.

enter image description here

8

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)||','||
'))')
| improve this answer | |
6

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.

Example

| improve this answer | |
  • 3
    It would be simpler to just create the diagonal line (2 points) and create the bbox around it. – tudorbarascu Aug 12 '17 at 8:45
  • indeed ! Simple is beautiful :) – gisnside Aug 13 '17 at 8:55
3

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.

| improve this answer | |
  • "Envelope function" : have a try with "bounds()" > see my answer – gisnside Aug 3 '17 at 8:48
2

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

| improve this answer | |
  • 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. – Fezter Mar 20 '18 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? – Christopher Wesson Mar 20 '18 at 23:50
  • I stand corrected, I neglected to read the bounds part. – Fezter Mar 20 '18 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? – Louis Tate Nov 26 '18 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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