2

In QGIS 3 there are a number of useful vector tools available via GRASS functions. However, the output of many of these only contain 'cat' attribute in their output.

For example, if I run the routing tool v.net.allpairs, it outputs a number of routes in an input vector line layer that connect all the input points in an input vector point layer.

But since the only attribute in the output line routes are 'cat' and to and from 'cat', it's difficult to fetch attributes of the original input points (for example if I want to transfer a flow magnitude between two points in a network to the route line connecting them).

So, how do I best make sure that vector input to GRASS either keeps its original attributes in the output, or has unique id(s) that can be used to retroactively rejoin the original attributes?

  • Well, the new 'cat' column is the unique identifier. And the 'from_cat' and 'to_cat' refer, of course to the original points that are connected, Can you use this to do an SQL join with the original points and get the attributes you need? – Micha Oct 22 '18 at 9:38
  • There doesn't seem to be any relationship between the created IDs and the rows in the original dataset. I've tried relating it to row number and FID without success. I suppose there has to be some logic to it (it would seem strange if the cat number is just random), but I can't figure out what it is. – hexamon Oct 23 '18 at 7:19
  • The new "cat" column are unique keys, assigned as the line features are created. But the from_cat and to_cat should refer to the "cat" columns in the original points layer. – Micha Oct 23 '18 at 16:13
  • Okey, so if my original input has a unique ID with the column name 'cat', that will be inherited in GRASS? – hexamon Oct 24 '18 at 12:22
1

Typically when you create a new vector in GRASS, a "cat" column is automatically added, which GRASS uses to connect between the geometries and attributes. So your original points layer should have this "cat" already. These values will appear, as I said, in the from_cat and to_cat attributes of the new lines vector. So you'll have to put together an SQL query to extract from the points map the attributes you want using matches between the points cat and the lines to_cat and from_cat.

I Don't have time to put together a full answer and test (and you haven't mentioned what attributes exactly you want), but it will look something like:

ALTER TABLE lines ADD COLUMN p1_flow DOUBLE, p2_flow DOUBLE;
UPDATE lines SET p1_flow=(SELECT 
  p1.flow FROM lines AS l, points AS p1, points as p2
  WHERE l.from_cat=p1.cat AND l.to_cat=p2.cat);
UPDATE lines SET p2_flow=(SELECT 
  p1.flow FROM lines AS l, points AS p1, points as p2
  WHERE l.from_cat=p2.cat AND l.to_cat=p1.cat); 
  • How or where do run this query? – Andreas Müller Oct 25 '18 at 8:45
  • I tested and got the results I wanted. Great, thanks! – hexamon Oct 25 '18 at 8:46
  • @AndreasMüller You could run a query in Virtual Layers for example. In my case, I just filtered out a specific from and to combination to make sure it referred to the same input points as expected. – hexamon Oct 25 '18 at 8:47
  • Yes, I understand, but how do you execute a SQL statement, is it in the Processing Toolbox? – Andreas Müller Oct 25 '18 at 9:00
  • 1
    In QGIS DB Manager you can execute SQL statements on current project layers by using the Virtual Layers heading. This allows you to create a project specific view that joins your input and the GRASS output for example. – hexamon Oct 26 '18 at 6:46

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.