I'm working in a way to make hydrologic projects using QGIS and an Excel worksheet that I have. To do this, I want to extract some information of the lines, included in a vector layer, which represents a pipe section.
The information that I need to extract are:
- ID Number
- Length
- X, Y start and end coordinates
I've found a way to capture this field using the "$length" and another algorithm for the X and Y coordinates, but for that I need to open the Attributes table, put the expressions in each attribute column and click to update fields.
Is there a way to when I draw a line these fields are filled in automatically? That is, I draw / edit a line (start editing or end node ) and when I open the attribute table the length fields and coordinates X , Y are filled and updated.
Best Answer
Interesting question! I'm not aware of any other way of achieving what you want, but using PyQGIS.
Read the code below. It has some texts in it:
'lines'
,'length'
,'startX'
,'startY'
,'endX'
,'endY'
. You can adjust those names in the script for it to work on your data. The first one is you layer name, whereas the rest corresponds to field names. I assume your line layer has those fields (after all, you want values to be written there).Once you have adjusted your layer name and the names of the fields you want to be automatically updated, copy and paste the script into the QGIS Python console.
If everything goes well, you should be able to see that field values are automatically updated in two scenarios: 1) When new features are added, and 2) When geometries are modified.
This is how it works:
If you have any problem while running the script, add a comment below this answer.
It might be handy for you to have this functionality already available when you open your QGIS project. If that's the case, tell me, I could post instructions to do that.
EDIT:
For this functionality to be available every time you open your QGIS project (i.e., a
.qgs
file containing, among others, your line layer) you need to follow these steps:Go to
QGIS->Project->Project Properties->Macros
, check thePython macros
option, and replace the whole code with this one (adjust the values indicating your layer and field names):Make sure you enable macros on your project, this way:
Settings->Options->General->Enable macros: Always
.Save your QGIS project.
Now, every time you open the
.qgs
file you've just saved, the attributes of your line layer will be automatically updated when you add a new feature or modify a geometry (i.e., no need to copy anything into the QGIS Python Console anymore).2nd EDIT:
I've just published a plugin called AutoFields to help people to solve this kind of problems. I even made a video showing how to solve your ploblem, you can watch it at:
https://vimeo.com/germap/autofields-geometric-properties
AutoFields documentation: http://geotux.tuxfamily.org/index.php/en/geo-blogs/item/333-autofields-plugin-for-qgis