Skip to content

Commit

Permalink
Add feature to store address with translations
Browse files Browse the repository at this point in the history
  • Loading branch information
juuso-j committed May 22, 2024
1 parent c6e4429 commit 35443cf
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions mobility_data/importers/wfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
from mobility_data.importers.utils import (
delete_mobile_units,
get_or_create_content_type_from_config,
get_street_name_translations,
LANGUAGES,
locates_in_turku,
log_imported_message,
MobileUnitDataBase,
save_to_database,
split_string_at_first_digit,
)

DEFAULT_SOURCE_DATA_SRID = 3877
Expand All @@ -40,6 +43,7 @@ def __init__(self):
super().__init__()

def add_feature(self, feature, config):
municipality = None
create_multipolygon = False
if "create_multipolygon" in config:
create_multipolygon = config["create_multipolygon"]
Expand Down Expand Up @@ -97,14 +101,32 @@ def add_feature(self, feature, config):
self.municipality = Municipality.objects.filter(
id=municipality_id
).first()

if "fields" in config:
for attr, field in config["fields"].items():
for lang, field_name in field.items():
# attr can have fallback definitons if None
if getattr(self, attr)[lang] is None:
getattr(self, attr)[lang] = feature[field_name].as_string()

if "translate_fi_address_municipality_id" in config:
municipality = Municipality.objects.filter(
id=config["translate_fi_address_municipality_id"].lower()
).first()

if "translate_fi_address_field" in config:
address = feature[config["translate_fi_address_field"]].as_string()
if not address[0].isdigit():
street_name, street_number = split_string_at_first_digit(address)
else:
street_name = address
street_number = ""
muni = municipality if municipality else self.municipality
translated_street_names = get_street_name_translations(
street_name.strip(), muni
)
for lang in LANGUAGES:
self.address[lang] = f"{translated_street_names[lang]} {street_number}"

if "extra_fields" in config:
for field, attr in config["extra_fields"].items():
val = None
Expand Down Expand Up @@ -168,9 +190,12 @@ def import_wfs_feature(config, data_file=None):
assert len(ds) == 1
layer = ds[0]
for feature in layer:
object = MobilityData()
if object.add_feature(feature, config):
objects.append(object)
try:
object = MobilityData()
if object.add_feature(feature, config):
objects.append(object)
except Exception as e:
logger.warning(f"Discarding feature {feature}, cause: {e}")
content_type = get_or_create_content_type_from_config(config["content_type_name"])
num_created, num_deleted = save_to_database(objects, content_type)
log_imported_message(logger, content_type, num_created, num_deleted)

0 comments on commit 35443cf

Please sign in to comment.