2

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?

  • 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 3 '16 at 8:00
  • I do not have squares, it's always rectangles – MAP Nov 3 '16 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. – user30184 Nov 3 '16 at 8:12
  • I use PostGIS! Is there a PostGIS solution? – MAP Nov 3 '16 at 8:14
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;
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 )
  • You should remove the last comma from the WKT of the polygon. – user30184 Nov 3 '16 at 10:12
  • you are right (but it does work like that) – Ian Turton Nov 3 '16 at 10:13
  • Really, with QGIS? It is more flexible than JTS which throws: com.vividsolutions.jts.io.ParseException: Expected number but found ')' – user30184 Nov 3 '16 at 10:15
  • QGIS is obviously quite forgiving when parsing WKT – Ian Turton Nov 3 '16 at 10:17

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.