# Draw rectangle with a given size (height/length) from attribute table

I would like to build a rectangle (shape or symbol) with a given size (height/length) from the attribute-table.

In detail: I build a Atlas of maps with alternating sizes, like 145x129 or 165x129. The size of every feature is written in the attribute table of the coverage-layer. So far so good: every map got his own defined size using data defined override for map length and height.  But now I would like the bounding boxes of the atlas-maps to be shown as overlays on the map in my canvas. (Like you can do with the overlay-function in the print composer)

The best approach seemed to be to use the geometry-generator and build a polygon around the centroid of the feature. I already started a post months ago: Using QGIS Geometry Generator to get rectangle from point? With the result, that it is not possible because the "Geometry generator does not support calculations inside WKT". I still have that problem to solve. Do anyone know a other approach?

• Create a circle with buffer and then take the envelope of the circle and you'll have a rectangle. I don't know if you can do that with the geometry generator. Nov 3, 2016 at 8:00
• I do not have squares, it's always rectangles Nov 3, 2016 at 8:03
• Sorry, in the referenced question you tried to make a square. You did not say that it should be always a square, though. If you can use PostGIS or SpatiaLite or ogr2ogr I can show how to make it with SQL. Nov 3, 2016 at 8:12
• I use PostGIS! Is there a PostGIS solution? Nov 3, 2016 at 8:14

@iant was faster but this is my version with PostGIS.

This one works with points and fixed offsets "1" to each direction.

``````select ST_GeomFromText('POLYGON (('
||ST_X(the_geom)-1||' '||ST_Y(the_geom)-1||','
||ST_X(the_geom)-1||' '||ST_Y(the_geom)+1||','
||ST_X(the_geom)+1||' '||ST_Y(the_geom)+1||','
||ST_X(the_geom)+1||' '||ST_Y(the_geom)-1||','
||ST_X(the_geom)-1||' '||ST_Y(the_geom)-1||'))')
from my_points;
``````

This is using centroids and work with any geometry type:

``````select ST_GeomFromText('POLYGON (('
||ST_X(ST_Centroid(the_geom))-1||' '||ST_Y(ST_Centroid(the_geom))-1||','
||ST_X(ST_Centroid(the_geom))-1||' '||ST_Y(ST_Centroid(the_geom))+1||','
||ST_X(ST_Centroid(the_geom))+1||' '||ST_Y(ST_Centroid(the_geom))+1||','
||ST_X(ST_Centroid(the_geom))+1||' '||ST_Y(ST_Centroid(the_geom))-1||','
||ST_X(ST_Centroid(the_geom))-1||' '||ST_Y(ST_Centroid(the_geom))-1||'))')
from my_polygons;
``````

If your offsets are stored into attributes "offset_x" and "offset_y" use this:

``````select ST_GeomFromText('POLYGON (('
||ST_X(ST_Centroid(the_geom))-offset_x||' '||ST_Y(ST_Centroid(the_geom))-offset_y||','
||ST_X(ST_Centroid(the_geom))-offset_x||' '||ST_Y(ST_Centroid(the_geom))+offset_y||','
||ST_X(ST_Centroid(the_geom))+offset_x||' '||ST_Y(ST_Centroid(the_geom))+offset_y||','
||ST_X(ST_Centroid(the_geom))+offset_x||' '||ST_Y(ST_Centroid(the_geom))-offset_y||','
||ST_X(ST_Centroid(the_geom))-offset_x1||' '||ST_Y(ST_Centroid(the_geom))-offset_y||'))')
from my_polygons;
``````

While you can't do maths in side 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)||
'))')
``````

Will draw small boxes around the centroid of a feature. So you can just add in your heights and widths to get the desired box.

Or if you just need the actual bounding box then the easier answer is to use:

`````` bounds(  \$geometry )
``````
• You should remove the last comma from the WKT of the polygon. Nov 3, 2016 at 10:12
• you are right (but it does work like that) Nov 3, 2016 at 10:13
• Really, with QGIS? It is more flexible than JTS which throws: `com.vividsolutions.jts.io.ParseException: Expected number but found ')'` Nov 3, 2016 at 10:15
• QGIS is obviously quite forgiving when parsing WKT Nov 3, 2016 at 10:17