From 7a5c6d75b1c1f14a792a43f0da71f2ad9be73bb0 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Thu, 13 Jun 2024 12:46:08 +1000 Subject: [PATCH] Ensure groups match their original order in QGIS project (#87) --- felt/core/api_client.py | 3 ++- felt/core/map_uploader.py | 14 +++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/felt/core/api_client.py b/felt/core/api_client.py index f268ed6..2e7ed4d 100644 --- a/felt/core/api_client.py +++ b/felt/core/api_client.py @@ -484,7 +484,8 @@ def create_layer_groups(self, ) group_post_data = [ - {'name': g} for g in layer_group_names + {'name': g, + 'ordering_key': i} for i, g in enumerate(layer_group_names) ] return QgsNetworkAccessManager.instance().blockingPost( diff --git a/felt/core/map_uploader.py b/felt/core/map_uploader.py index e108091..d1e6f09 100644 --- a/felt/core/map_uploader.py +++ b/felt/core/map_uploader.py @@ -36,7 +36,8 @@ QgsBlockingNetworkRequest, QgsReferencedRectangle, QgsRasterLayer, - QgsLayerTree + QgsLayerTree, + QgsLayerTreeGroup ) from qgis.utils import iface @@ -170,6 +171,12 @@ def __init__(self, self.error_string: Optional[str] = None self.feedback: Optional[QgsFeedback] = None self.was_canceled = False + self.ordered_top_level_groups: List[str] = [] + if project: + for child in project.layerTreeRoot().children(): + if isinstance(child, QgsLayerTreeGroup): + self.ordered_top_level_groups.append(child.name()) + self.ordered_top_level_groups.reverse() @staticmethod def layer_and_group(project: QgsProject, layer: QgsMapLayer) \ @@ -479,6 +486,11 @@ def run(self): rate_limit_counter = 0 if all_group_names: + # ensure group names match their order in the QGIS project + all_group_names = sorted( + all_group_names, + key=lambda x: self.ordered_top_level_groups.index(x) + ) reply = API_CLIENT.create_layer_groups( map_id=self.associated_map.id, layer_group_names=all_group_names