10

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

underdark
  • 84,148
  • 21
  • 231
  • 413
MartinMap
  • 8,262
  • 7
  • 50
  • 114

5 Answers5

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)||','||
'))')
Ian Turton
  • 81,417
  • 6
  • 84
  • 185
7

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

gisnside
  • 7,818
  • 2
  • 29
  • 73
5

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. – 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
  • Super clever! Thank you! – Paulo Cardoso Feb 08 '24 at 21:38
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.

user30184
  • 65,331
  • 4
  • 65
  • 118
0

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

MrXsquared
  • 34,292
  • 21
  • 67
  • 117