code-refactor
See docs/reference/README.md for the MUST/SHOULD/MAY convention, and
docs/reference/shared.md for rules code-refactor shares with other
tools, including the "Hierarchical code format and retention" section it
shares with code-update.
Inputs¶
code-refactorMUST read the one input and reproject it to EPSG:4326 viacore.io.read_and_reproject(). It takes exactly one flat input at the finest level, hierarchy embedded as columns (the same shapeschema-fill/package-polygonsexpect).
Level resolution¶
code-refactorMUST resolve each level's own code column either via an explicitname_field/code_fieldpair (each an{n}-template, given together or not at all, raisingValueErrorif only one is given, the same contractschema-map/package-polygonsuse), or, when both are omitted, via structural auto-detection (core.schema_map.detect_level_columns_or_single(), cardinality/ containment only, no naming convention assumed).code-refactorMUST raiseValueError("no admin hierarchy level detected") if structural auto-detection finds zero levels.code-refactorMUST raiseValueError("no existing code column to overwrite") if any resolved level has no code column at all (e.g. a trailing finest level with only a name column, seedocs/adr/0106), rather than silently skipping that level or overwriting its name column.- Every resolved level MUST be renumbered to a clean, relative
1..Nsequence, coarsest first; a genuinely constant coarsest column (e.g. a single-country file's own admin0 code) is dropped before reaching this step and never becomes a level. - A source column that never resolves into a level (including a constant
admin0-shaped one) MUST be left completely untouched:
code-refactornever stampsroot_codeinto its own output column, it's used only as the literal parent for level 1's own assignment.
Assignment¶
- For each resolved level
1..N, ascending,code-refactorMUST rank that level's own distinct code-column values under their immediately-coarser level's already-assigned code (orroot_code, for level 1), sorted by their own raw, pre-assignment value, and overwrite the column in place with a freshly assigned, sequential, zero-padded code (core.code.assign_new_codes()). - A level's raw source value MUST NOT be reused as-is (zero-padded or passed through unchanged): it may be non-numeric, gappy, or duplicated across siblings, so every value is always re-ranked into a fresh sequential integer before formatting.
- The sort key MUST be the resolved code column's own raw value; there is
no COD-AB-specific multi-column tie-break (e.g.
srcidthennamethenname1-name3). - A parent whose child count exceeds
10 ** min_width - 1(999 at the default width 3) MUST NOT have its already-assigned, lower-numbered children's codes repadded; the overflowing child's own tail simply grows pastmin_widthinstead (seedocs/reference/shared.md).
Outputs¶
code-refactorMUST export the finest-level table, every resolved level's code column overwritten in place, as the main output, same format as the input. It performs no topology hard gate: geometry is never modified, only attribute columns are rewritten.code-refactorMAY write an issues report whenissues_pathis given and at least one parent exceeded overflow capacity; it MUST delete any stale file already at that path when the run produces zero overflow rows. Schema:kind('digit-overflow'),level,parent_code,assigned_code(the overflowing parent's own highest-tail-integer child),child_count,min_width,reason.
Configuration (api.code_refactor.code_refactor() / CLI)¶
code-refactorMUST process exactly one input file per call.root_code,delimiter, andmin_widthMUST all be given explicitly (no default), validated viacore.code.resolve_code_format():root_codenon-empty,delimiterexactly one character,min_widthpositive.root_codeis opaque, never shape-checked (a disputed- territory or otherwise non-ISO3 string works identically to an ISO3 one).output_path, if omitted, MUST default toinput_pathwith a_codedstem suffix.issues_path, if omitted, MUST default tooutput_pathwith an_issuesstem suffix and a.csvextension. It MUST be one ofcore.code.TABLE_COPY_OPTS's extensions (a tabular format; the issues report has no geometry column), raisingValueErrorotherwise.code-refactorMUST raiseFileExistsErrorforoutput_pathorissues_pathif either already exists and overwriting wasn't requested.step, if given, MUST be one ofinputs,levels,assign,outputs; any other value MUST raiseValueError.