Skip to content

Commit

Permalink
Merge pull request #36 from opengisch/styles_usabilityhub
Browse files Browse the repository at this point in the history
Support Layer Styles in UsabILIty Hub (projecttopping)
  • Loading branch information
signedav authored Nov 21, 2022
2 parents 6c377db + b52ecab commit 4e81d98
Show file tree
Hide file tree
Showing 8 changed files with 1,438 additions and 2 deletions.
17 changes: 16 additions & 1 deletion modelbaker/dataobjects/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def __init__(
ili_name=None,
definitionfile=None,
qmlstylefile=None,
styles={},
):
self.provider = provider
self.uri = uri
Expand Down Expand Up @@ -101,6 +102,7 @@ def __init__(

self.definitionfile = definitionfile
self.qmlstylefile = qmlstylefile
self.styles = styles

self.__form = Form()

Expand All @@ -124,6 +126,7 @@ def dump(self):
definition["ili_name"] = self.ili_name
definition["definitionfile"] = self.definitionfile
definition["qmlstylefile"] = self.qmlstylefile
definition["styles"] = self.styles
definition["form"] = self.__form.dump()
return definition

Expand All @@ -141,6 +144,7 @@ def load(self, definition):
self.ili_name = definition["ili_name"]
self.definitionfile = definition["definitionfile"]
self.qmlstylefile = definition["qmlstylefile"]
self.styles = definition["styles"]
self.__form.load(definition["form"])

def create(self):
Expand Down Expand Up @@ -197,9 +201,20 @@ def create_form(self, project):
edit_form = self.__form.create(self, self.__layer, project)
self.__layer.setEditFormConfig(edit_form)

def load_style(self):
def load_styles(self):
if self.qmlstylefile:
self.__layer.loadNamedStyle(self.qmlstylefile)
if self.styles:
for style_name in self.styles.keys():
# add the new style (because otherwise we overwrite the previous one)
self.__layer.styleManager().addStyleFromLayer(style_name)
self.__layer.styleManager().setCurrentStyle(style_name)
style_properties = self.styles[style_name]
style_qmlstylefile = style_properties.get("qmlstylefile")
if style_qmlstylefile:
self.__layer.loadNamedStyle(style_qmlstylefile)
# set the default style
self.__layer.styleManager().setCurrentStyle("default")

def _create_layer(self, uri, layer_name, provider):
if provider and provider.lower() == "wms":
Expand Down
2 changes: 1 addition & 1 deletion modelbaker/dataobjects/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def create(
if layer.layer.type() == QgsMapLayer.VectorLayer:
# even when a style will be loaded we create the form because not sure if the style contains form settngs
layer.create_form(self)
layer.load_style()
layer.load_styles()

if self.legend:
self.legend.create(qgis_project, group)
Expand Down
10 changes: 10 additions & 0 deletions modelbaker/generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,16 @@ def full_node(self, layers, item, path_resolver=lambda path: path):
current_node.qmlstylefile = path_resolver(
item_properties.get("qmlstylefile")
)
styles = {}
if "styles" in item_properties:
for style_name in item_properties["styles"].keys():
style_properties = item_properties["styles"][style_name]
style_qmlstylefile = path_resolver(
style_properties.get("qmlstylefile")
)
styles[style_name] = {"qmlstylefile": style_qmlstylefile}
current_node.styles = styles

return current_node

def legend(
Expand Down
136 changes: 136 additions & 0 deletions tests/test_projecttopping.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,142 @@ def test_kbs_postgis_qml_styles(self):
# check if the layers have been considered
assert count == 2

def test_kbs_postgis_qml_multistyles(self):
"""
Checks if "styles" and their qml style files can be applied by the layer tree
"""

importer = iliimporter.Importer()
importer.tool = DbIliMode.ili2pg
importer.configuration = iliimporter_config(
importer.tool, self.toppings_test_path
)
importer.configuration.ilimodels = "KbS_LV95_V1_4"
importer.configuration.dbschema = "toppings_{:%Y%m%d%H%M%S%f}".format(
datetime.datetime.now()
)
importer.configuration.srs_code = "2056"
importer.configuration.tomlfile = os.path.join(
self.toppings_test_path, "metaattributes/sh_KbS_LV95_V1_4.toml"
)
importer.stdout.connect(self.print_info)
importer.stderr.connect(self.print_error)
assert importer.run() == iliimporter.Importer.SUCCESS

generator = Generator(
DbIliMode.ili2pg,
get_pg_connection_string(),
"smart2",
importer.configuration.dbschema,
)
available_layers = generator.layers()

assert len(available_layers) == 16

# load the projecttopping file
projecttopping_file_path = os.path.join(
self.toppings_test_path,
"projecttopping/opengis_projecttopping_multistyles_KbS_LV95_V1_4.yaml",
)

with open(projecttopping_file_path, "r") as yamlfile:
projecttopping_data = yaml.safe_load(yamlfile)
assert "layertree" in projecttopping_data
legend = generator.legend(
available_layers,
layertree_structure=projecttopping_data["layertree"],
path_resolver=lambda path: os.path.join(
os.path.dirname(projecttopping_file_path), path
)
if path
else None,
)

# No layers added now - stays 16
assert len(available_layers) == 16

relations, _ = generator.relations(available_layers)

project = Project()
project.layers = available_layers
project.legend = legend
project.relations = relations
project.post_generate()

qgis_project = QgsProject.instance()
project.create(None, qgis_project)
count = 0

for layer in available_layers:
if (
layer.name == "belasteter_standort"
and layer.geometry_column == "geo_lage_punkt"
):
count += 1
style_manager = layer.layer.styleManager()

# default style
edit_form_config = layer.layer.editFormConfig()
assert edit_form_config.layout() == QgsEditFormConfig.TabLayout
tabs = edit_form_config.tabs()
assert len(tabs) == 5
assert tabs[0].name() == "Allgemein"
for field in layer.layer.fields():
if field.name() == "bemerkung_rm":
assert field.alias() == "Bemerkung Romanisch"
if field.name() == "bemerkung_it":
assert field.alias() == "Bemerkung Italienisch"
assert (
layer.layer.displayExpression()
== "'Default: '||standorttyp ||' - '||katasternummer"
)

# robot style (in binary)
style_manager.setCurrentStyle("robot")
edit_form_config = layer.layer.editFormConfig()
assert edit_form_config.layout() == QgsEditFormConfig.TabLayout
tabs = edit_form_config.tabs()
assert len(tabs) == 5
assert (
tabs[0].name()
== "01000111 01100101 01101110 01100101 01110010 01100001 01101100"
)
for field in layer.layer.fields():
if field.name() == "bemerkung_rm":
assert (
field.alias()
== "01100010 01100101 01101101 01100101 01110010 01101011 01110101 01101110 01100111 01011111 01110010 01101101"
)
if field.name() == "bemerkung_it":
assert (
field.alias()
== "01100010 01100101 01101101 01100101 01110010 01101011 01110101 01101110 01100111 01011111 01101001 01110100"
)
assert (
layer.layer.displayExpression()
== "'Robot: '||standorttyp ||' - '||katasternummer"
)

# french style (in french)
style_manager.setCurrentStyle("french")
edit_form_config = layer.layer.editFormConfig()
assert edit_form_config.layout() == QgsEditFormConfig.TabLayout
tabs = edit_form_config.tabs()
assert len(tabs) == 5
assert tabs[0].name() == "Général"
for field in layer.layer.fields():
if field.name() == "bemerkung_rm":
assert field.alias() == "Remarque en Romane"
if field.name() == "bemerkung_it":
assert field.alias() == "Remarque en Italien"
assert (
layer.layer.displayExpression()
== "'French: '||standorttyp ||' - '||katasternummer"
)

# check if the layers have been considered
assert count == 1

def test_kbs_postgis_iliname(self):
"""
Checks if layers can be loaded by it's iliname and geometry column.
Expand Down
Loading

0 comments on commit 4e81d98

Please sign in to comment.