Skip to content

Commit

Permalink
Merge pull request #187 from isogeo/feature-attributes
Browse files Browse the repository at this point in the history
FeatureAttribute - improve import_from_dataset method
  • Loading branch information
SimonSAMPERE committed Oct 23, 2020
2 parents 7e0946a + 84f2fdd commit 5243146
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions isogeo_pysdk/api/routes_feature_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def update(

# -- Extra methods as helpers --------------------------------------------------
def import_from_dataset(
self, metadata_source: Metadata, metadata_dest: Metadata, mode: str = "add"
self, metadata_source: Metadata, metadata_dest: Metadata, mode: str = "add", case_sensitive_matching: bool = True
) -> bool:
"""Import feature-attributes from another vector metadata.
Expand All @@ -351,6 +351,8 @@ def import_from_dataset(
- 'add': add the attributes except those with a duplicated name
- 'update': update only the attributes with the same name
- 'update_or_add': update the attributes with the same name or create
: param bool case_sensitive_matching: False to make featureattributes's name
matching case-insensitive when mode == "update"
:raises TypeError: if one metadata is not a vector
:raises ValueError: if mode is not one of accepted value
Expand Down Expand Up @@ -385,6 +387,7 @@ def import_from_dataset(
attributes_source = self.listing(metadata_source)
attributes_dest = self.listing(metadata_dest)
attributes_dest_names = [attr.get("name") for attr in attributes_dest]
attributes_dest_names_low = [attr.get("name").lower() for attr in attributes_dest]

# according to the selected mode
if mode == "add":
Expand Down Expand Up @@ -417,16 +420,27 @@ def import_from_dataset(
if attr.get("name") == attr_src.name
][0]
)
attr_dst.alias = attr_src.alias
attr_dst.dataType = attr_src.dataType
attr_dst.description = attr_src.description
attr_dst.language = attr_src.language
self.update(metadata=metadata_dest, attribute=attr_dst)
logger.debug(
"Attribute with the same name ({}) spotted. It has been updated.".format(
attr_dst.name
)
elif attr_src.name.lower() in attributes_dest_names_low and not case_sensitive_matching:
attr_dst = FeatureAttribute(
**[
attr
for attr in attributes_dest
if attr.get("name").lower() == attr_src.name.lower()
][0]
)
else:
continue

attr_dst.alias = attr_src.alias
attr_dst.dataType = attr_src.dataType
attr_dst.description = attr_src.description
attr_dst.language = attr_src.language
self.update(metadata=metadata_dest, attribute=attr_dst)
logger.debug(
"Attribute with the same name ({}) spotted. It has been updated.".format(
attr_dst.name
)
)
elif mode == "update_or_add":
for attribute in attributes_source:
attr_src = FeatureAttribute(**attribute)
Expand Down

0 comments on commit 5243146

Please sign in to comment.