3

I'm using QGIS 3.4.7. I have one vector layer with road lines and a second with city polygons (and a third with city centroid points if that is better). I have already used symbologies with arrows to confirm the road directions match the direction of traffic in real life. My plan is to color the roads based on whether their traffic flows either into or out of the city, so I was wanting to use something like this in the field calculator after selecting my target city from either the polygon or point version of the city layer:

CASE
WHEN distance(end_point($geometry), targetcity) - distance(start_point($geometry), targetcity) > 0 THEN 'out'
WHEN distance(end_point($geometry), targetcity) - distance(start_point($geometry), targetcity) < 0 THEN 'in'
WHEN distance(end_point($geometry), targetcity) - distance(start_point($geometry), targetcity) == 0 THEN 'no_flow'
END

However, I haven't figured out how to use the field calculator while referencing another layer. I would ideally like a solution without a lot of Python coding because I'm fairly new to QGIS but I will learn it if I have to.

2
  • 1
    The get_feature() function returns the first feature of a layer matching a given attribute value. I don't think you can use it with is_selected().
    – csk
    May 22, 2019 at 3:20
  • Have you had a look at the aggregate function?
    – Erik
    May 22, 2019 at 6:26

2 Answers 2

1

I figured out that you can incorporate "is_selected" into your expression by adding a virtual field to the city_points layer.

  • Virtual field
  • Integer format
  • Field name: Selected
  • Expression: is_selected()

When the feature is not selected, the field value will be 0. When the feature is selected, the field value will be 1. Since this is a virtual field, it will update whenever you select a different feature. (I tested this in QGIS 3.6.2. Virtual fields were a relatively new and buggy feature in early version of QGIS 3, but I think it will work in your version.)

enter image description here

Now you can use this field in your existing expression. Where your expression currently says:

geometry(get_feature('city_points', 'NAME', 'City Name')

instead use:

geometry(get_feature('city_points', 'Selected', 1)
1

Based on the comment from csk, this is what I got to work:

CASE
WHEN distance(end_point($geometry), geometry(get_feature('city_points', 'NAME', 'City Name'))) > distance(start_point($geometry), geometry(get_feature('city_points', 'NAME', 'City Name'))) THEN 'out'
WHEN distance(end_point($geometry), geometry(get_feature('city_points', 'NAME', 'City Name'))) < distance(start_point($geometry), geometry(get_feature('city_points', 'NAME', 'City Name'))) THEN 'in'
WHEN distance(end_point($geometry), geometry(get_feature('city_points', 'NAME', 'City Name'))) = distance(start_point($geometry), geometry(get_feature('city_points', 'NAME', 'City Name'))) THEN 'no_flow'
END

Instead of selecting, I'm just going to filter for my target city for visualization purposes.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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