code-create
Inputs¶
code-createMUST 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-createMUST 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-createMUST raiseValueError("no admin hierarchy level detected") if structural auto-detection finds zero levels.- In structural mode,
code-createMUST 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. - With an explicit
name_field/code_fieldpair, a level with a name column but no code column MUST get a code column seeded from its names; a level with neither MUST raiseValueError. code-createMUST raiseValueErrorin structural mode ("group units like a level") if detection sets any column aside as a supplemental coarser grouping, and ("a coarser level merged into this one") if any member of a level's group-by has over 30% fewer values than its code under each parent, rather than coding a merged or skipped level (seedocs/adr/0121).- 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-createnever stampsroot_codeinto its own output column, it's used only as the literal parent for level 1's own assignment.
Assignment¶
source_codesMUST be one ofreplace(default),embed,copy.- Under
replaceandcopy, for each resolved level1..N, ascending,code-createMUST 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()). - Under
replaceandcopy, 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. - Under
copy, each level's source code column MUST be copied to its next free numbered sibling (adm1_codetoadm1_code1), placed right after it, before assignment; a seeded level gets none. - Under
embed, a level with a source code column MUST be coded as its parent's code (orroot_code), thendelimiter, then its own source value unchanged, and a seeded level is ranked as underreplace. If every source code at a level starts with its parent's source code (orroot_code, at level 1) and is longer than it, that prefix MUST be removed before embedding; if only some do,embedMUST raiseValueError.embedMUST also raiseValueErrorif a row of a source-coded level has no source code, or ifdelimiteris empty and that level's source codes differ in length (seedocs/adr/0122,docs/adr/0125). - 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). - Each level's codes MUST be zero-padded to that level's own width:
min_widthitself, its entry in a per-level list, or underautothe widest tail the level needs (seedocs/adr/0123). - A parent whose child count exceeds
10 ** width - 1(999 at width 3) MUST NOT have its already-assigned, lower-numbered children's codes repadded; the overflowing child's own tail simply grows past the width instead. With an empty delimiter and a fixed width, an overflowing parent MUST raiseValueErrorinstead.
Outputs¶
code-createMUST 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-createMAY write an issues report whenissues_pathis given and a numbered level (any level with a fixed width, except a source-coded one underembed) has a parent over 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 highest code),child_count,min_width(that level's width),reason.
Configuration (api.code_create.code_create() / CLI)¶
code-createMUST 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, or empty,min_widthone positive width, a comma list of positive widths with exactly one per numbered level (coarsest first), orauto.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-createMUST 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.
Examples¶
Example 1: structural auto-detection, no code column exists yet¶
topo-tools code-create admin2.geojson --root-code AFG --delimiter . --min-width 3
Example 2: explicit level columns, ambiguous auto-detection¶
topo-tools code-create admin2.geojson --root-code AFG --delimiter . --min-width 3 \
--code-field adm{n}_code --name-field adm{n}_name
Example 3: overflow issues report¶
Writes admin2_coded.geojson and, only if any parent exceeds 10 **
min_width - 1 children, admin2_coded_issues.csv:
topo-tools code-create admin2.geojson admin2_coded.geojson --root-code AFG --delimiter . --min-width 3
Example 4: source codes embedded without a delimiter¶
topo-tools code-create admin3.geojson --root-code XY --delimiter '' --min-width auto \
--source-codes embed --code-field adm{n}_code --name-field adm{n}_name
Example 5: source codes kept in sibling columns¶
topo-tools code-create admin3.geojson --root-code XYZ --delimiter . --min-width 3 \
--source-codes copy --code-field adm{n}_code --name-field adm{n}_name
Example 6: one width per level¶
topo-tools code-create admin3.geojson --root-code XYZ --delimiter . --min-width 2,3,4