3

I have been trying to create a grid, or raster, from point data which has a variety of classes (ie, I am trying to grid a categorical attribute).

Strangely (to me) using Interpolate (SAGA, cubic spline) creates a continuous numerical variable when I use my categorical attribute (no idea how it is converting a string to a float).

'Multilevel b-spline interpolation for categories' (SAGA) seems like the function I want, but it produces an error (Unknown option: SHAPES).

What would be a good way to produce what I want?

4 Answers 4

4

This method requires QGIS 3.6 or later (but see the note below about the array_sort function).

Use this expression in the Field Calculator to create a new field with the category text values converted into numbers:

array_find( array_sort( array_distinct(array_agg("fieldname"))), "fieldname")

Explanation:

  • array_agg("fieldname") puts all the "fieldname" values, including duplicates, into an array
  • array_distinct() removes all duplicate values from the array
  • array_sort sorts the array, thus guaranteeing that the array is in the same order every time. The "fieldname" array is generated separately for every feature in the attribute table, so depending on how the internal computations are done the original array might not always be in the same order. Of course if the array is in a different order for every line of the attribute table, the method completely fails.

    • The array_sort function will not work in the current LTR (3.4); it's only available in QGIS 3.6 and later.
    • This function might be optional - I'm not entirely sure. Feel free to try the expression without sorting the array and let me know how it goes: array_find(array_distinct(array_agg("fieldname")),"fieldname")
  • array_find( [...], "fieldname") converts categorical value of the current feature and to a number, based on its location in the array

1
  • Thank you! You saved my day with this function.
    – Jens
    Apr 22, 2020 at 13:22
3

From Processing Toolbox: Vector analysis > List unique values

It will create a table with 150 rows. Then use field calculator to create a new column: type > integer and for expression: $id

Join the result with the original table and you can use the new column for rasterizing.

2

Convert your categories to numbers, you can do this with creating new field in attribute table with field calculator. Example expression:

CASE 
WHEN "classes"='forest' THEN 1 
WHEN "classes"='grassland' THEN 5
END

Then just Rasterize your point layer, using values from new field. There are few algorithms for rasterizing in Processing Toolbox.

As result you will get raster with values of your categories (numbers) and rest of the pixels will be nodata.

2
  • 1
    Not sure this is practical in my use-case of about 150 categories - not without generating the WHEN clauses programmatically.
    – Spacedman
    Dec 2, 2019 at 16:23
  • You're right. This solution is not appropriate for such amount of data without tweaks. For this case I would use some "uniqe values" approach as in the csk or Lutra in their answers, it is better.
    – Oto Kaláb
    Dec 3, 2019 at 10:58
1

Another approach when you have predefined values

If I have string values in "soil_group" field I can create new integer field using field calculator like this:

map_get(map('Null', 99, 'A', 1, 'B', 2, 'C', 3, 'D', 4, 'A/D', 11, 'B/D', 12, 'C/D', 13, 'W', 20), coalesce("soil_group", 'Null'))

Your Answer

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

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