1

In QGIS, I'm trying to create a new variable with a short description of a string value of another variable. For example, in the table below, I would like to have:

NAMEABB = "US 1" when NAME = "US Route 1";

NAMEABB = "I-5" when NAME = "Interstate Route 5";

NAMEABB = "SR 539" when NAME = "State Route 539".

enter image description here

I've tried extracting the numbers with regular expressions (building upon this post), but I get always NULL. Any hints?

0

2 Answers 2

2

The addition of multiple routes in one line slightly complicates the calculation because the routes will have to be broken up, renamed with abbreviations, and then reassembled back into one line.

When working with more than just a couple of conditions, I prefer to use either 13.2.16.3 map - List of functions - QGIS Documentation or 13.2.4.1 CASE - List of functions - QGIS Documentation instead of large if blocks.

array_to_string(
  array_foreach(
    string_to_array("NAME", ','),
    with_variable(
      'rename',
      map(
        'Interstate Route ', 'I-',
        'US Route ', 'US ',
        'Alternate US Route ', 'Alt US ',
        'State Route ', 'SR '
      ),
      with_variable(
        'name',
        regexp_substr(
          @element,
          '^\\p{Z}*([\\p{L}|\\p{Z}]+)\\p{N}+'
        ),
        replace(
          @element,
          @name,
          map_get(@rename, @name)
        )
      )
    )
  ),
  ','
)

enter image description here

2
  • Thank you, @bix0012! I apologize for my late reply. I just your post. Never got an email with it. Commented Apr 13, 2024 at 22:47
  • This works great and addresses the issue well! Commented Apr 13, 2024 at 22:55
1

You can use regexp_match to select feature records that contain specific strings and then regexp_replace to change those strings. In QGIS Field Calculator, run this script:

if(regexp_match("NAME",'US Route'),regexp_replace("NAME",'US Route','US'),

if(regexp_match("NAME",'Interstate Route '),regexp_replace("NAME",'Interstate Route ','I-'),

if(regexp_match("NAME",'State Route'),regexp_replace("NAME",'State Route','SR'),

"NAME")))

You can add more nested if statements for any other text you want to replace, just make sure to add additional ending parentheses at the end of the script.

This is the output from the above script:

output after running the script

6
  • Thank you, @spacelibrarian! Your solution works well. I just realized there's another issue: sometimes, NAME = "US Route 30, State Route 11", for example. Therefore, your formula yields "US 30, Sta". I updated the image of my post highlighting the case. Commented Apr 5, 2024 at 19:27
  • I suspect your NAMEABB field is set to a field length of 10, this is why it is being abbreviated to "US 30, Sta". If you run the script above once on the entire dataset, you could edit the double-name entries manually, or run another field calculator session on NAMEABB with something like: if(regexp_match("NAMEABB",', State Route'),regexp_replace("NAMEABB",', State Route',', SR'),"NAMEABB") Commented Apr 5, 2024 at 19:34
  • Also (grain of salt: I'm new here, too), it's probably helpful to keep your original post unedited, with the original screenshot, so that people can better understand the evolution of suggestions to help with your question. Commented Apr 5, 2024 at 19:36
  • Thank you, @spacelibrarian! I'll try to put things back. Commented Apr 5, 2024 at 19:40
  • No problem! Would appreciate it if you marked my answer as 'accepted', if you're happy with it :) Commented Apr 5, 2024 at 20:06

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.