3

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.

attribute-table of the coverage layer

data defined override for map length and height in the Atlas-menu

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?

MartinMap
  • 8,262
  • 7
  • 50
  • 114
  • 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. – user30184 Nov 03 '16 at 08:00
  • 1
    I do not have squares, it's always rectangles – MartinMap Nov 03 '16 at 08: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. – user30184 Nov 03 '16 at 08:12
  • I use PostGIS! Is there a PostGIS solution? – MartinMap Nov 03 '16 at 08:14

2 Answers2

5

@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;
user30184
  • 65,331
  • 4
  • 65
  • 118
4

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