2

I have to rasterize a polygon layer. Unfortunately the values of the field I have to burn into the raster has a variety of string entry's like "L239x" or "a403" (case sensitive!). RAT (Raster Attribute Tables) are no option in QGIS (see comments). So I need to translate the strings into real numbers. I thought about creating a new field and translating the the letters into a numeric code (f.e. place in alphabet) using an expression. But how? Any ideas?

Babel
  • 71,072
  • 14
  • 78
  • 208
MartinMap
  • 8,262
  • 7
  • 50
  • 114

3 Answers3

1

I have not used mixed numbers with letters, but I think in the end QGIS reads them as strings. I advise you to use the RasterizeString PlugIn, available here: https://plugins.qgis.org/plugins/rasterizestring/

You have to download the zip file into your computer and then install it with the "Install from Zip" option in the PlugIn window. You can find the installed extension by searching in the toolbox this word: RasterizeString. Now you can use a STRING value in the burn-in value fields, and the rest of the fields are the same as the normal rasterize window you are used to. Please be aware that this function creates two files, the raster itself and a CSV file with all the strings and the corresponding numeric code assigned automatically.

enter image description here

0

You should be able to convert your string values to unique numeric values using the "Add Unique Value Index Field" tool in the processing toolbox. https://docs.qgis.org/3.16/en/docs/user_manual/processing_algs/qgis/vectortable.html#add-unique-value-index-field

David Bartecchi
  • 311
  • 2
  • 7
0

This solution converts the characters into their ASCII-code (number). If you just want to keep the numerical part of the input, skip steps 1 and 3 and keep only step 2.

Use Field calculator to create a new field (or update the existing field). For an input as yours with the pattern [one character] + [a number of digits] + [one character], combine these three elements using concat:

concat(  
       --1:get the ASCII-code value of the first character of the input
    ascii(regexp_substr( value, '(\\D+)' )) , 
       --2: get the digits from the input
    regexp_substr( value, '(\\d+)'), 
       --3: get the ASCII-code value of the character before the end of the input
    ascii(regexp_substr( value, '(\\D+ *?$)' )) 
)

enter image description here

Babel
  • 71,072
  • 14
  • 78
  • 208