diff options
| author | Guilhem Moulin <guilhem@fripost.org> | 2026-03-07 13:59:30 +0100 |
|---|---|---|
| committer | Guilhem Moulin <guilhem@fripost.org> | 2026-03-07 14:00:43 +0100 |
| commit | 8eeb7370ad1828a17f7893601af3381a6ca72ed1 (patch) | |
| tree | bd3365e796b380dd1ec1365ead872689084f588c /geodata-import-topo | |
| parent | 29eb6ff992234979abdf38be878dd09c86f670a3 (diff) | |
geodata-import-topo: Convert county and municipality codes to int16.
And add NOT NULL and UNIQUE constraints. This makes it possible to use
lm_topo250.lansyta(lanskod) and lm_topo250.kommunyta(kommunod) as
FOREIGN KEYs.
Diffstat (limited to 'geodata-import-topo')
| -rwxr-xr-x | geodata-import-topo | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/geodata-import-topo b/geodata-import-topo index d814771..e58a701 100755 --- a/geodata-import-topo +++ b/geodata-import-topo @@ -56,27 +56,30 @@ from import_source import ( def get_layer_schema(layer : ogr.Layer) -> dict[str, Any]: """Return the layer definition dictionary for the given OGR layer.""" schema = {} + layer_name = layer.GetName() defn = layer.GetLayerDefn() n = defn.GetGeomFieldCount() if n != 1: - raise NotImplementedError(f'Layer "{layer.GetName()}" has geometry field count {n} != 1') + raise NotImplementedError(f'Layer "{layer_name}" has geometry field count {n} != 1') geom_fld = defn.GetGeomFieldDefn(0) if geom_fld.IsIgnored(): - raise NotImplementedError(f'Geometry field #0 in layer "{layer.GetName()}" is ignored') + raise NotImplementedError(f'Geometry field #0 in layer "{layer_name}" is ignored') schema['geometry-type'] = geom_fld.GetType() + layer_name_lower = layer_name.lower() fields = schema['fields'] = [] for i in range(defn.GetFieldCount()): fld = defn.GetFieldDefn(i) fld_name = fld.GetName() + fld_name_lower = fld_name.lower() if fld.IsIgnored(): logging.warning('Field #%d "%s" in source layer "%s" is ignored', - i, fld_name, layer.GetName()) + i, fld_name, layer_name) continue field = { 'Name': fld_name } fields.append(field) - if fld_name.lower() in ('objektidentitet', 'skapad', 'objekttypnr', 'objekttyp'): + if fld_name_lower in ('objektidentitet', 'skapad', 'objekttypnr', 'objekttyp'): field['Nullable'] = False else: field['Nullable'] = fld.IsNullable() @@ -86,7 +89,13 @@ def get_layer_schema(layer : ogr.Layer) -> dict[str, Any]: field['Comment'] = fld.GetComment() fld_type = field['Type'] = fld.GetType() - if fld_type == ogr.OFTString and fld_name.lower() == 'objektidentitet': + if (layer_name_lower, fld_name_lower) in (('lansyta', 'lanskod'), + ('kommunyta', 'kommunkod')): + fld_type = field['Type'] = ogr.OFTInteger + field['SubType'] = ogr.OFSTInt16 + field['Nullable'] = False + field['Unique'] = True + elif fld_type == ogr.OFTString and fld_name_lower == 'objektidentitet': field['SubType'] = ogr.OFSTUUID else: field['SubType'] = fld.GetSubType() |
