I have a set of vector lines (which represent translation vectors for georeferencing). Based on the layer CRS, how can I compute the coordinates of each line in an attribute table ?

i.e (Xmax-Xmin; Ymax-Ymin) for each line.

I have managed to get the line length with $length in the expression editor but I am struggling with other geometrical parameters.

Edit: I am looking to "store the formula" within the layer. The idea is to keep these values automatically updated with the geometry. (Auto)update should happen if a new line is added or modification of the current geometry is done.

  • 2
    About your recent edition, look at virtual fields (stores the formula but not within the layer). About the $length() function, it calculates the ellipsoidal length, use length( $geometry) instead if you want the planimetric one (probably not so). About the (Xmax-Xmin; Ymax-Ymin) example, it is assumed to be a string of characters because a pair of values can not be attributed to a single numeric field. – Gabriel De Luca Feb 11 at 1:45

For QGIS 3.x, the formula in the field calculator is :

'(' || x_max($geometry) || '-' || x_min($geometry) || '; ' || y_max($geometry) || '-' || y_min($geometry) || ')'

You can use the Field Calculator. Open the attribute table of your line layer and select the Open Filed Calculator icon from the toolbar. I use an OSM road layer.

enter image description here

In the Field Calculator window create a new column of decimal type, in the Geometry group of functions you can find x_min, x_max, y_min, y_max functions. To create a column with x_range of features use the next screenshot:

enter image description here

You can create a column for y_range in a similar way.

  • Thanks: that is the method I was after but I was missing $geometry to call the proper entity. – kFly Feb 10 at 20:18
  • Is there a method to automatically compute these parameters for each line created within this layer as soon as they are created ? – kFly Feb 11 at 0:23
  • or update automatically when the geometry is modified ? – kFly Feb 11 at 0:35
  • 1
    See this post gis.stackexchange.com/questions/178355/… or user virtual fields (virtual fields are calculated when you open the attribute table) which is technically simpler but slower. – Zoltan Feb 11 at 14:15

Following J.Monticolo's answer, yet another expression is:

'(' || bounds_width($geometry) || '; ' || bounds_height($geometry) ||')'
  • kFly didn't say if (s)he want (Xmax-Xmin) as text or the result of subtraction (q.e.d. width or height). In case of text result, my solution is the good one, in the case of substraction, it's the Kazuhito's one. – J. Monticolo Feb 10 at 19:26
  • I want actually the numerical value, but thanks for underlining up this difference – kFly Feb 10 at 20:08

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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