gws.plugin.alkis.data.index

ALKIS index tables: storage, search and loading of Flurstuecke and addresses.

Source code: gws.plugin.alkis.data.index

Module Contents

gws.plugin.alkis.data.index.all_flat_keys()

Return all flat keys of the Flurstueck structure.

The keys are derived from the type annotations in types, see flatten_fs.

Returns:

A dict of flat keys and their types, sorted by key.

gws.plugin.alkis.data.index.flatten_fs(fs: gws.plugin.alkis.data.types.Flurstueck, keys_to_extract: set[str]) → list[dict]

Flatten a Flurstueck into a list of dicts.

The nested Flurstueck structure is turned into flat dicts whose keys are attribute paths joined with _, starting with fs, e.g. fs_recs_gemarkung_text. EnumPair values give two keys, _code and _text. For list values, the dicts are repeated for each list item, so the result is the product of all lists, for example:

record:
    a:x, b:[1,2], c:[3,4]

flat list:
    a:x, b:1, c:3
    a:x, b:1, c:4
    a:x, b:2, c:3
    a:x, b:2, c:4

Only paths leading to keys_to_extract are followed, which keeps the product small. Still, some combinations of keys produce many rows.

Parameters:
  • fs – Flurstueck to flatten.

  • keys_to_extract – Flat keys to include.

Returns:

A list of dicts.

gws.plugin.alkis.data.index.hausnummer_key(s, upper=False)

Create a sort key for a house number.

The key is the leading number, zero-padded to 6 digits, followed by the lower-cased rest, e.g. 000002b for 2 B. Keys sort by number first, then by suffix, when compared bytewise (COLLATE "C").

Parameters:
  • s – House number.

  • upper – Create the key for the upper bound of a range. A house number without a suffix then gets a ~ suffix, which sorts after all letters, so that the bound 5 includes 5a to 5z.

Returns:

The key, or None if the house number does not start with a number.

gws.plugin.alkis.data.index.make_fsnummer(r: gws.plugin.alkis.data.types.FlurstueckRecord)

Create a display number for a Flurstueck.

The format is <gemarkung> <flur>-<zaehler>/<nenner> (<folge>). The Flur, the denominator and the sequence number are omitted if empty, the sequence number also if it is 00.

Parameters:

r – Flurstueck record.

Returns:

The display number.

gws.plugin.alkis.data.index.normalize_hausnummer(s)

Normalize a house number by removing all whitespace.

Parameters:

s – House number, e.g. 12 a.

Returns:

The normalized house number, e.g. 12a, an empty string for None.

class gws.plugin.alkis.data.index.Object

Bases: gws.Node

ALKIS index.

Manages the index tables in the index schema, reports their status, runs Flurstueck and address searches against them and loads the found objects with their related data.

ALL_TABLES = ['place', 'flurstueck', 'lage', 'part', 'indexflurstueck', 'indexlage', 'indexgeom',...

All index tables.

columnDct

Column definitions by table id.

crs: gws.Crs

CRS of the geometries.

db: gws.plugin.postgres.provider.Object

Database provider.

excludeGemarkung: set[str]

Gemarkung numbers excluded from indexing.

gemarkungFilter: set[str]

Gemarkung codes searches are restricted to.

HAUSNUMMER_NOT_NULL_VALUE = '*'

House number value that matches any non-empty house number.

saMeta: gws.lib.sa.MetaData

SQLAlchemy metadata for the index tables.

schema: str

Schema of the index tables.

tables: dict[str, gws.lib.sa.Table]

Index tables by table id.

TABLES_BASIC

Tables of the basic index.

TABLES_BUCHUNG

Tables with land register data.

TABLES_EIGENTUEMER

Tables with owner data.

VERSION = '85'

Index version, part of the table names.

activate()
configure()
count_all(qo: gws.plugin.alkis.data.types.FlurstueckQueryOptions) → int

Count all Flurstueck records in the index.

Parameters:

qo – Query options. Historic records are counted only with withHistorySearch.

Returns:

The number of rows in the Flurstueck index table.

create_table(table_id: str, values: list[dict])

Create an index table and fill it with rows.

Parameters:
  • table_id – Table id.

  • values – Rows as dicts of column names and values.

default_land()

Return the Land of a Gemarkung in the index.

The value is cached once found.

Returns:

The Land as an EnumPair, or None if there are no Gemarkungen.

drop()

Drop all index tables.

drop_table(table_id: str)

Drop an index table, if it exists.

Parameters:

table_id – Table id.

find_adresse(q: gws.plugin.alkis.data.types.AdresseQuery) → list[gws.plugin.alkis.data.types.Adresse]

Find addresses.

Parameters:

q – Search criteria and options.

Returns:

A list of addresses, in the order of the search, after applying offset and page size.

Raises:
find_flurstueck(q: gws.plugin.alkis.data.types.FlurstueckQuery) → list[gws.plugin.alkis.data.types.Flurstueck]

Find Flurstuecke and load them with their related data.

Parameters:

q – Search criteria and options.

Returns:

A list of Flurstuecke, in the order of the search, after applying offset and page size.

Raises:
has_schema() → bool

Check whether the index schema exists.

Returns:

True if the schema exists.

has_table(table_id: str) → bool

Check whether an index table exists and has data.

Parameters:

table_id – Table id.

Returns:

True if the table has at least one row.

iter_all(qo: gws.plugin.alkis.data.types.FlurstueckQueryOptions) → collections.abc.Iterable[gws.plugin.alkis.data.types.Flurstueck]

Iterate over all Flurstuecke in the index.

Flurstuecke are loaded in chunks of qo.pageSize, with a new database connection for each chunk.

Parameters:

qo – Query options. pageSize must be set.

Yields:

Flurstuecke with their related data.

load_flurstueck(fs_uids: list[str], qo: gws.plugin.alkis.data.types.FlurstueckQueryOptions) → list[gws.plugin.alkis.data.types.Flurstueck]

Load Flurstuecke with their related data.

Parameters:
  • fs_uids – Flurstueck uids.

  • qo – Query options, the display themes select the related data.

Returns:

A list of Flurstuecke in the order of fs_uids. Missing and, unless withHistoryDisplay is set, historic Flurstuecke are left out.

status() → gws.plugin.alkis.data.types.IndexStatus

Return the index status.

Returns:

The status, computed from the number of rows in the index tables.

strasse_list() → list[gws.plugin.alkis.data.types.Strasse]

Return all streets in the index.

Each distinct combination of Gemeinde, Gemarkung and street name of current Flurstuecke and Lage records is returned once. The list is cached.

Returns:

A list of streets.

table(table_id: str) → gws.lib.sa.Table

Return an index table.

Parameters:

table_id – Table id, one of the TABLE_ constants.

Returns:

The table object.

table_size(table_id) → int

Return the number of rows in an index table.

Parameters:

table_id – Table id.

Returns:

The number of rows, 0 if the table does not exist.

gws.plugin.alkis.data.index.parse_fsnummer(s)

Parse a Flurstueck display number into its parts.

The format is the one created by make_fsnummer, all parts are optional.

Parameters:

s – Display number.

Returns:

A dict with the keys gemarkungCode, flurnummer, zaehler, nenner and flurstuecksfolge for the parts found, or None if the string does not match the format.

gws.plugin.alkis.data.index.serialize(o: gws.plugin.alkis.data.types.Object, encode_enum_pairs=True) → dict

Convert an object to a JSON-compatible structure.

Objects become dicts with sorted keys, dates become DD.MM.YYYY strings. Empty values are kept as they are.

Parameters:
  • o – Object to convert.

  • encode_enum_pairs – If True, EnumPair values are encoded as $code$text strings, otherwise as dicts.

Returns:

A dict.

gws.plugin.alkis.data.index.strasse_key(s)

Normalize a street name for text search.

Works like text_key, but also expands the abbreviations str. and pl. and separates common street name suffixes, so that for example Hauptstr. and Haupt-Strasse give the same key.

Parameters:

s – Street name.

Returns:

The normalized street name, an empty string for None.

gws.plugin.alkis.data.index.TABLE_BUCHUNGSBLATT = 'buchungsblatt'
gws.plugin.alkis.data.index.TABLE_FLURSTUECK = 'flurstueck'
gws.plugin.alkis.data.index.TABLE_INDEXBUCHUNGSBLATT = 'indexbuchungsblatt'
gws.plugin.alkis.data.index.TABLE_INDEXFLURSTUECK = 'indexflurstueck'
gws.plugin.alkis.data.index.TABLE_INDEXGEOM = 'indexgeom'
gws.plugin.alkis.data.index.TABLE_INDEXLAGE = 'indexlage'
gws.plugin.alkis.data.index.TABLE_INDEXPERSON = 'indexperson'
gws.plugin.alkis.data.index.TABLE_LAGE = 'lage'
gws.plugin.alkis.data.index.TABLE_PART = 'part'
gws.plugin.alkis.data.index.TABLE_PLACE = 'place'
gws.plugin.alkis.data.index.text_key(s)

Normalize a string for text search.

The string is lower-cased, umlauts are transliterated and punctuation is replaced by spaces.

Parameters:

s – String to normalize.

Returns:

The normalized string, an empty string for None.

gws.plugin.alkis.data.index.text_search_clause(column, val, tso: gws.TextSearchOptions)

Create a where clause that matches a column against a search string.

Parameters:
  • column – Column to match.

  • val – Search string.

  • tso – Text search options. Without options, the value is matched exactly.

Returns:

A clause, or None if the value is empty or shorter than the minimum length.

gws.plugin.alkis.data.index.unserialize(data: dict)

Convert a structure created by serialize back to objects.

Dicts become types.Object instances and $code$text strings become EnumPair values.

Parameters:

data – Serialized data.

Returns:

The object.