I'm working expanding abbreviated names in a shapefile. I have written the following expansions, and would like to learn how to write a field expression that would do many expansions in one.

replace( "DNRNAME",'SF','State Forest')
replace( "DNRNAME",'FMA','Fish Management Area')
replace( "DNRNAME",'FT','Fire Tower')
replace( "DNRNAME",'SP','State Park')
replace( "DNRNAME",'CFL','Chesapeake Forest Lands')
replace( "DNRNAME",'NRMA','Natural Resource Management Area')
replace( "DNRNAME",'NEA','Natural Environment Area')
replace( "DNRNAME",'WMA','Wildlife Management Area')
replace( "DNRNAME",'HCF','Heritage Conservation Fund Site')

How can I apply all of these replace conditionals at one time?

  • 1
    Another approach would be to create a key table with two fields, abbreviation and full name, then join that table to your shapefile and export the results. – Chris W May 1 '14 at 0:08

You could try a conditional statement, something like this:

  WHEN DNRNAME = 'FMA' THEN 'Fish Management Area'
  WHEN DNRNAME = 'FT' THEN 'Fire Tower'
  WHEN DNRNAME = 'SP' THEN 'State Park'
  WHEN DNRNAME = 'CFL' THEN 'Chesapeake Forest Lands'
  WHEN DNRNAME = 'NRMA' THEN 'Natural Resource Management Area'
  WHEN DNRNAME = 'NEA' THEN 'Natural Environment Area'
  WHEN DNRNAME = 'WMA' THEN 'Wildlife Management Area'
  WHEN DNRNAME = 'HCF' THEN 'Heritage Conservation Fund Site'

Not the prettiest, but this should work:

replace( replace( replace( replace( replace( replace( replace( replace( replace( "DNRNAME",'HCF','Heritage Conservation Fund Site'),'WMA','Wildlife Management Area'),'NEA','Natural Environment Area'),'NRMA','Natural Resource Management Area'),'CFL','Chesapeake Forest Lands'),'SP','State Park'),'FT','Fire Tower'),'FMA','Fish Management Area'),'SF','State Forest')

Another alternative, perhaps no more pretty than ndawson's, would be to turn it into a if statement. I see two ways to do this at Syntax of conditional statements in QGIS Field Calculator(s) which this question might be a duplicate of.

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.