diff --git a/ckanext/odsh/helpers_tpsh.py b/ckanext/odsh/helpers_tpsh.py index c7541af5fc63fc29270297d613fb99a8744255be..58c5ff24a63daee925d6e4a54f0d3a5316e588cb 100644 --- a/ckanext/odsh/helpers_tpsh.py +++ b/ckanext/odsh/helpers_tpsh.py @@ -134,13 +134,34 @@ def get_spatial_for_selection(): file_path = config.get('ckanext.odsh.spatial.mapping', extension_path + '/resources/schleswig-holstein_geojson.csv') with open(file_path, newline='') as mapping_file: cr = csv.reader(mapping_file, delimiter="\t") - spatial_mapping = list() - for row in cr: - key = row[0] - value = row[1] - spatial_mapping.append({'key':key, 'value':value}) - spatial_mapping.append({'key':'', 'value':''}) - return spatial_mapping + spatial_mapping = list(cr) + + unique_mapping = [] + seen_values = set() + for key, value, _ in spatial_mapping: + if value in seen_values: + continue # Skip if the value has already been seen + + if "municipalityKey" in key: + unique_mapping.append({'key': key, 'value': value}) + else: + # Check if there is a municipality key entry for the value + municipality_entry = next( + (entry for entry in spatial_mapping if entry[1] == value and "municipalityKey" in entry[0]), + None + ) + + if municipality_entry: + # If a municipality key entry exists, use it instead of the current key + unique_mapping.append({'key': municipality_entry[0], 'value': value}) + else: + # Otherwise, use the current key + unique_mapping.append({'key': key, 'value': value}) + + seen_values.add(value) + + unique_mapping.append({'key': '', 'value': ''}) + return unique_mapping def get_subject_for_selection(): SUBJECT_MAPPING = load_subject_mapping()