Skip to content

[Feature]: Unique Values on a field with more than 64 values: let the user choose how many classes (maxClasses), and give classes past the palette distinct colours #3586

Backlog

Description

@elpidioassinaturas

Before you start

What problem does this solve?

Soil, geology, land-use and zoning maps often have far more than 64 mapping units. A soil survey of the state of Ceará (Brazil), for example, has 713 distinct values in its unit field (UnidMapFin: PVAe1 … PVAe38, RLe1 … RLe165, …).

layer.symbology {"type": "unique", "field": "UnidMapFin"} keeps the first 64 values in text order and draws the rest as <all other values>:

FUNCEME_SolosCE: 3942 features whose value is past the first 64 values of UnidMapFin (in text order) are drawn as <all other values>

So 3,942 of 5,203 polygons (76 %) are drawn in the same grey. The layout legend then lists Agua … LAd39 and <all other values>, which is alphabetical order, not importance. The map can't be made this way, and no setting changes the 64.

The limit applies only when classes are made automatically from a field. Elsewhere RustGIS already handles more:

Path Limit Where
Unique Values from a field (layer.symbology, the Symbology pane's Classify, subtypes) 64 MAX_UNIQUE, crates/map/src/symbology.rs:1075, used by Renderer::unique_counted (:1239-1241) and layer.rs:8365/:8397, design.rs:2379
Unique Values from an expression, applied again up to 200 MAX_EXPRESSION_CLASSES, symbology.rs:1078, unique_by_up_to (:1286-1294), layer.rs:8366-8370
QGIS / ArcGIS style import 4096 qgis/renderer.rs:17, arcgis_style.rs:16
Symbology pane list 200 panes.rs:5641-5653
Layout legend no class limit; up to 6 columns, adjustSize down to 4 pt render/src/layout/legend.rs, map/src/layout/legend.rs:382

The 64 is a safe default, but it shouldn't be a hard limit when a style import can bring 4,096 classes and the legend can draw them.

A second, smaller problem shows up at once: the default qualitative ramp (Harbour) has 10 colours, and Ramp::pick cycles them (k % len, crates/map/src/color.rs:616-635). Class 0 and class 10 get the same colour, and so do classes 1 and 11, and so on. With 64 classes each colour is used six or seven times, so two neighbouring units can have the same colour. With more classes this gets worse.

Proposed solution

  1. maxClasses in layer.symbology (Unique Values only; refused for the other types):

    layer.symbology {"layer": "Solos", "type": "unique", "field": "UnidMapFin", "maxClasses": 1000}
    
    • It ranges from 1 to MAX_UNIQUE_CLASSES, a new constant of 1000, the same as MAX_LEGEND_LABELS (map/src/layout/legend.rs:9). The default stays 64, so existing projects and scripts behave as before.
    • The field path calls a new Renderer::unique_up_to(ds, rows, field, ramp, base, max), which mirrors unique_by_up_to. unique_counted stays as unique_up_to(…, MAX_UNIQUE).
    • The same key goes to the expression path in place of the fixed keep. MAX_EXPRESSION_CLASSES stays as the most a re-applied rule style keeps.
    • The notice gives the number actually used: "… past the first {max} values …". When features are past it, the notice adds "maxClasses up to 1000 keeps more".
    • The Symbology pane gets a Classes: [64 ▾] field next to Classify for Unique Values (64, 128, 256, 512, 1000). It lists them all, so the pane limit of 200 (panes.rs:5641-5653) goes up to MAX_UNIQUE_CLASSES.
  2. Distinct colours past the ramp's length: a qualitative ramp with s stops gives class k the stop k % s, shifted in lightness by its round r = k / s. The shift goes −12 %, +12 %, −24 %, +24 % … in OKLab L, clamped to 15–92 %. That way class k and class k + s are never the same colour. Rounds past four start again at four times the stop count, with a hue turn of 360° / (2s). A custom ramp with exactly n stops keeps its colours as they are (current behaviour).

  3. Docs: in user-guide/cartography.md (Unique Values), mention maxClasses and say that a field with hundreds of units reads better when classes are grouped by an expression, such as the code without its trailing numbers (replace(…'0','')…), with each class labelled through layer.classStyle {"label"}. That works today, and it is how the Ceará map was made: 52 classes in 13 layers, one per soil order, each name used as a legend heading.

In parts (each under 400 lines; only the last says "Closes"):

  1. maxClasses for fields and expressions in layer.symbology, Renderer::unique_up_to, the notice, and engine and map tests. Tests: 100 values with maxClasses: 100 keep 100 classes and no notice; the default still keeps 64 and gives the notice; maxClasses: 0 and 1001 are refused; maxClasses with "type": "graduated" is refused.
  2. Distinct colours past the ramp's length in Ramp::pick, with tests: for Harbour, classes 0 and 10 differ, and the first 10 classes keep today's colours.
  3. The Symbology pane's Classes field, raising the pane's list to MAX_UNIQUE_CLASSES, and the docs.

Alternatives considered

  • Only raise MAX_UNIQUE to 1000. Simpler, but a field with thousands of distinct values, such as a name or id field, would then make 1,000 classes by accident on a plain Classify. Keeping 64 as the default and making the higher number a choice avoids that.
  • Group automatically, by prefix or by the most frequent values. Useful, but how to group is a decision about the data (soil order, suborder, unit). Expressions already do it. A "top N by count, rest as Other" order could be a follow-up option ("order": "count").
  • A legend table element listing unit codes and descriptions. That belongs to layout tables ([Feature]: Layout bar charts: group and aggregate, order, bar limit, and look #2487 family), not to symbology.

Additional context

  • Reproduction:
    layer.addData {"path": "<abs path>/FUNCEME_SolosCE.shp"}
    layer.symbology {"layer": "FUNCEME_SolosCE", "type": "unique", "field": "UnidMapFin"}
    
    → the notice above; the layout legend shows 64 classes plus <all other values>. Any field with more than 64 distinct values reproduces it, for example a points layer with a name field.
  • Workaround used today: "expression": "replace(replace(… UnidMapFin,'0','') …,'9','')" gives 52 classes, which fits in 64, with one layer per soil order (each a definition query) so that the legend shows the order names as headings. The A2 landscape panel layout keeps the 52 rows at about 6.5 pt. In A3 the legend's adjustSize brings them down to about 4.7 pt.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions