[QGIS-Developer] Factorizing a dbf

Nyall Dawson nyall.dawson at gmail.com
Mon Jan 22 16:16:26 PST 2018


On 22 January 2018 at 21:18, Paolo Cavallini <cavallini at faunalia.it> wrote:
> Hi all,
> I have a large shp with a number of repeated strings, and I'd like to
> refactor it replacing strings with a numeric code. I have found a
> Processing alg that does it, creating a new file each time, which is
> cumbersome; moreover, the alg apparently changes field type, adding
> decimals to reals. Of course I could create a CASE statement, but this
> is also cumbersome.
> Is there a more straightforward way?
> Thanks.

Two ideas (both based on master, because qgis 2.18 is painful to use now!):

Option 1:
Run the "Add unique value field index" algorithm to add an
autogenerated code for each unique category. Then, run the "List
unique values" algorithm selecting both the original field and the
newly created field for the "target fields" in order to get a summary
table mapping the new categories to their original values (or wait
until https://github.com/qgis/QGIS/pull/6132 is merged, which adds
creation of this summary table as an option to the alg... then you can
do this with a single algorithm execution)

Option 2:
Run the "List unique values" algorithm on your table, selecting the
field to reclass as the "target field". In the returned table (which
contains all the unique categories), add a new field for your
reclassed codes, and populate this in some way that makes sense for
you (e.g. manually, or via field calculator, etc). Then, run the "join
attributes table" algorithm and join your original table to the class
table, joining by your original field value and adding the new code
field as the "layer 2 field to copy".

Nyall


More information about the QGIS-Developer mailing list