Skip to content

extract

extract

Exporter

Source code in src/qgis_server_light/exporter/extract.py
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
class Exporter:
    def __init__(
        self,
        qgis_project_path: str,
        unify_layer_names_by_group=False,
        unify_layer_names_by_group_separator=".",
        pg_service_configs=None,
    ):
        self.unify_layer_names_by_group_separator = unify_layer_names_by_group_separator
        self.path = qgis_project_path
        self.unify_layer_names_by_group = unify_layer_names_by_group
        self.pg_service_configs = pg_service_configs or {}

        # prepare QGIS instances
        self.qgis_project = self.open_qgis_project(qgis_project_path)
        self.qgis_project_tree_root = self.qgis_project.layerTreeRoot()
        self.version, self.assembled_name = self.prepare_qgis_project_name(
            self.qgis_project
        )

        # prepare QSL interface instances
        self.qsl_tree = Tree()
        self.qsl_datasets = Datasets()
        self.qsl_project = Project(name=self.assembled_name, version=self.version)
        self.qsl_project_metadata = self.extract_metadata(self.qgis_project)
        self.qsl_config = Config(
            project=self.qsl_project,
            meta_data=self.qsl_project_metadata,
            tree=self.qsl_tree,
            datasets=self.qsl_datasets,
        )

    def run(self) -> Config:
        self.walk_qgis_project_tree(self.qgis_project_tree_root, [])
        return self.qsl_config

    def walk_qgis_project_tree(
        self,
        entity: QgsLayerTreeNode,
        path: list[str],
    ):
        """
        This is a highly recursive function which walks to the qgis job_layer_definition tree to extract all knowledge out
        of it. It is called from itself again for each level of group like elements which are found.

        Args:
            entity: The QGIS projects tree node which can be a QgsLayerTree, QgsLayerTreeGroup or
                QgsLayerTreeLayer.
            path: The path is a list of string which stores the information of the current tree path. This is
                used to construct a string for unifying job_layer_definition names by their tree path.
        """
        if isinstance(entity, QgsLayerTreeLayer):
            # this is a job_layer_definition, we can extract its information
            self.extract_save_layer(
                entity,
                path,
            )
        elif isinstance(entity, QgsLayerTreeGroup) or isinstance(entity, QgsLayerTree):
            # these are "group like" elements, we need to step into them one level deeper.
            short_name = self.get_group_short_name(entity)
            if short_name != "":
                # '' is the root of the tree, we don't want it to be part of the path
                path = path + [short_name]
            self.extract_group(
                entity,
                path,
            )
            for child in entity.children():
                # we handle all the children of the group like thing.
                self.walk_qgis_project_tree(
                    child,
                    path,
                )
        else:
            logging.error(
                f"This element was not expected while walking QGIS project tree: {entity}"
            )

    def extract_group(
        self,
        group: QgsLayerTreeGroup,
        path: list[str],
    ):
        """
        Collects data pertaining to a QGIS job_layer_definition tree group. Basically this translates a QgsLayerTreeGroup
        into a QGIS-Server-Light TreeGroup.

        Args:
            group: The group which is handled.
            path: The path is a list of string which stores the information of the current tree path. This is
                used to construct a string for unifying job_layer_definition names by their tree path.
        """
        children = []
        for child in group.children():
            if isinstance(child, QgsLayerTreeGroup):
                children.append(self.get_group_short_name(child))
            else:
                children.append(child.layer().id())
        self.qsl_tree.members.append(
            TreeGroup(
                id=self.get_group_short_name(group),
                name=self.get_group_short_name(group),
                children=children,
            )
        )
        self.qsl_datasets.group.append(
            Group(
                id=self.get_group_short_name(group),
                name=self.get_group_short_name(group),
                title=self.get_group_title(group),
            )
        )

    def extract_save_layer(
        self,
        child: QgsLayerTreeLayer,
        path: list[str],
        types_from_editor_widget: bool = False,
    ):
        """Save the given job_layer_definition to the output path."""
        layer = child.layer()
        layer_type = self.get_layer_type(layer)
        if layer_type is None:
            return
        decoded = self.decode_datasource(layer)
        short_name = self.short_name(self.get_layer_short_name(child), path)
        if layer.isSpatial():
            crs = self.create_qsl_crs_from_qgs_layer(layer)
            layer_extent = layer.extent()
            bbox_wgs84 = self.create_qsl_bbox_from_qgis_rectangle_wgs84(
                self.qgis_project, layer, layer_extent
            )
            bbox = self.create_qsl_bbox_from_qgis_rectangle_extent(layer_extent)
            is_spatial = True
        else:
            crs = None
            bbox_wgs84 = None
            bbox = None
            is_spatial = False
        if layer_type == "vector":
            if layer.providerType().lower() == "ogr":
                source = DataSource(ogr=self.create_qsl_source_ogr(decoded))
            elif layer.providerType().lower() == "postgres":
                source = DataSource(postgres=self.create_qsl_source_postgresql(decoded))
                source_path_parts = []
                DictEncoder().encode(source.postgres)
                for field in fields(source.postgres):
                    source_path_parts.append(
                        f"{field.name}={getattr(source.postgres, field.name)!r}"
                    )
            elif layer.providerType().lower() == "wfs":
                # TODO: Correctly implement source!
                source = WfsSource()
            else:
                logging.error(
                    f"Unknown provider type {layer.providerType().lower()} for job_layer_definition {layer.title() or layer.name()}"
                )
                return
            qsl_vector_dataset_fields = self.create_qsl_fields_from_qgis_field(layer)
            self.qsl_datasets.vector.append(
                Vector(
                    name=short_name,
                    title=layer.title() or layer.name(),
                    styles=self.create_style_list(layer),
                    driver=layer.providerType(),
                    bbox_wgs84=bbox_wgs84,
                    fields=qsl_vector_dataset_fields,
                    source=source,
                    id=layer.id(),
                    crs=crs,
                    bbox=bbox,
                    minimum_scale=layer.minimumScale(),
                    maximum_scale=layer.maximumScale(),
                    geometry_type_simple=layer.geometryType().name,
                    geometry_type_wkb=layer.wkbType().name,
                    is_spatial=is_spatial,
                )
            )
        elif layer_type == "raster":
            if layer.providerType() == "gdal":
                source = DataSource(gdal=self.create_qsl_source_gdal(decoded))
            elif layer.providerType() == "wms":
                if "tileMatrixSet" in decoded:
                    source = DataSource(wmts=self.create_qsl_source_wmts(decoded))
                else:
                    if decoded.get("type") == "xyz":
                        source = DataSource(xyz=self.create_qsl_source_xyz(decoded))
                    else:
                        source = DataSource(wms=self.create_qsl_source_wms(decoded))
            else:
                logging.error(f"Unknown provider type: {layer.providerType().lower()}")
                return
            if source is not None:
                self.qsl_datasets.raster.append(
                    Raster(
                        name=short_name,
                        title=layer.title() or layer.name(),
                        styles=self.create_style_list(layer),
                        driver=layer.providerType(),
                        bbox_wgs84=bbox_wgs84,
                        source=source,
                        id=layer.id(),
                        crs=crs,
                        bbox=bbox,
                        minimum_scale=layer.minimumScale(),
                        maximum_scale=layer.maximumScale(),
                        is_spatial=is_spatial,
                    )
                )
            else:
                logging.error(
                    f"Source was None this is not expected. Layer was: {short_name}, QGIS job_layer_definition ID:{layer.id()}"
                )
        elif layer_type == "custom":
            if layer.providerType().lower() in ["xyzvectortiles", "mbtilesvectortiles"]:
                source = DataSource(
                    vector_tile=self.create_qsl_source_vector_tiles(decoded)
                )
            else:
                logging.error(
                    f"Unknown provider type: {layer.providerType().lower()} Layer was: {short_name}, QGIS job_layer_definition ID:{layer.id()}"
                )
                # TODO: make this more configurable
                return
            self.qsl_datasets.custom.append(
                Custom(
                    name=short_name,
                    title=layer.title() or layer.name(),
                    styles=self.create_style_list(layer),
                    driver=layer.providerType(),
                    bbox_wgs84=bbox_wgs84,
                    source=source,
                    id=layer.id(),
                    crs=crs,
                    bbox=bbox,
                    minimum_scale=layer.minimumScale(),
                    maximum_scale=layer.maximumScale(),
                    is_spatial=is_spatial,
                )
            )
        else:
            logging.error(
                f'Unknown layer_type "{layer_type}" Layer was: {short_name}, QGIS job_layer_definition ID:{layer.id()}'
            )
            return

    def short_name(self, short_name: str, path: list[str]) -> str:
        """
        Decides if to use the short name itself or the unified version by the tree path.

        Args:
            short_name: The short name either of the group or the job_layer_definition.
            path: The path is a list of string which stores the information of the current tree path. This is
                used to construct a string for unifying job_layer_definition names by their tree path.

        Returns:
            The short name itself or its unified tree path.
        """
        if self.unify_layer_names_by_group:
            return self.create_unified_short_name(short_name, path)
        else:
            return short_name

    def create_unified_short_name(self, short_name: str, path: list[str]):
        """
        Creates the unified short name, separated by the configured separator.

        Args:
            short_name: The short name either of the group or the job_layer_definition.
            path: The path is a list of string which stores the information of the current tree path. This is
                used to construct a string for unifying job_layer_definition names by their tree path.

        Returns:

        """
        short_name_parts = path + [short_name]
        return self.unify_layer_names_by_group_separator.join(short_name_parts)

    def decode_datasource(self, layer: QgsMapLayer) -> dict:
        """
        Decodes a QGIS map job_layer_definition datasource into a dict and ensures that types are pythonic and pathes are
        clean for further usage.

        Args:
            layer: The job_layer_definition which the datasource should be extracted from.

        Returns:
            The decoded and cleaned datasource.
        """
        decoded = QgsProviderRegistry.instance().decodeUri(
            layer.providerType(), layer.dataProvider().dataSourceUri()
        )
        logging.debug(f"Layer source: {decoded}")
        for key in decoded:
            if str(decoded[key]) == "None":
                decoded[key] = None
            elif str(decoded[key]) == "NULL":
                decoded[key] = None
            else:
                decoded[key] = str(decoded[key])
            if key == "path":
                decoded[key] = decoded[key].replace(
                    f"{self.qgis_project.readPath('./')}/", ""
                )
        return decoded

    @staticmethod
    def create_qsl_field_from_qgis_field(
        field: QgsField, is_primary_key: bool
    ) -> Field:
        attribute_type_xml = Exporter.obtain_simple_types_from_qgis_field_xml(field)
        (
            editor_widget_type,
            editor_widget_type_wfs,
            editor_widget_type_json,
            editor_widget_type_json_format,
        ) = Exporter.obtain_editor_widget_type_from_qgis_field(field)
        (
            attribute_type_json,
            attribute_type_json_format,
        ) = Exporter.obtain_simple_types_from_qgis_field_json(field)
        return Field(
            is_primary_key=is_primary_key,
            name=field.name(),
            type=field.typeName(),
            type_wfs=attribute_type_xml,
            type_oapif=attribute_type_json,
            type_oapif_format=attribute_type_json_format,
            alias=field.alias() or field.name().title(),
            comment=field.comment(),
            nullable=is_primary_key and Exporter.obtain_nullable(field),
            length=Exporter.provide_field_length(field),
            precision=Exporter.provide_field_precision(field),
            editor_widget_type=editor_widget_type,
            editor_widget_type_wfs=editor_widget_type_wfs,
            editor_widget_type_oapif=editor_widget_type_json,
            editor_widget_type_oapif_format=editor_widget_type_json_format,
        )

    @staticmethod
    def obtain_nullable(field: QgsField):
        if not (
            field.constraints().constraints()
            == QgsFieldConstraints.Constraint.ConstraintNotNull
        ):
            return True
        return False

    @staticmethod
    def provide_field_length(field: QgsField) -> int | None:
        length = field.length()
        if length > 0:
            return length
        else:
            return None

    @staticmethod
    def provide_field_precision(field: QgsField) -> int | None:
        precision = field.precision()
        if precision > 0:
            return precision
        else:
            return None

    @staticmethod
    def create_qsl_fields_from_qgis_field(layer: QgsVectorLayer) -> List[Field]:
        fields = []
        pk_indexes = layer.dataProvider().pkAttributeIndexes()
        for field_index, field in enumerate(layer.fields().toList()):
            fields.append(
                Exporter.create_qsl_field_from_qgis_field(
                    field, (field_index in pk_indexes)
                )
            )
        return fields

    @staticmethod
    def obtain_simple_types_from_qgis_field_xml(field: QgsField) -> str:
        """

        Args:
            field: The field of an `QgsVectorLayer`.

        Returns:
            Unified type name regarding
            [XSD spec](https://www.w3.org/TR/xmlschema11-2/#built-in-primitive-datatypes)
            IMPORTANT: If type is not matched within the function it will be `string` always!
        """
        attribute_type = field.type()
        if attribute_type == QMetaType.Type.Int:
            return "int"
        elif attribute_type == QMetaType.Type.UInt:
            return "unsignedInt"
        elif attribute_type == QMetaType.Type.LongLong:
            return "long"
        elif attribute_type == QMetaType.Type.ULongLong:
            return "unsignedLong"
        elif attribute_type == QMetaType.Type.Double:
            if field.length() > 0 and field.precision() == 0:
                return "integer"
            else:
                return "decimal"
        elif attribute_type == QMetaType.Type.Bool:
            return "boolean"
        elif attribute_type == QMetaType.Type.QDate:
            return "date"
        elif attribute_type == QMetaType.Type.QTime:
            return "time"
        elif attribute_type == QMetaType.Type.QDateTime:
            return "dateTime"
        else:
            return "string"

    @staticmethod
    def get_group_title(group: QgsLayerTreeGroup) -> str:
        if group.customProperty("wmsTitle"):
            return group.customProperty("wmsTitle")
        elif hasattr(group, "groupLayer"):
            # since QGIS 3.38
            if group.groupLayer():
                if group.groupLayer().serverProperties():
                    if group.groupLayer().serverProperties().title():
                        return group.groupLayer().serverProperties().title()
        return group.name()

    @staticmethod
    def get_group_short_name(group: QgsLayerTreeGroup) -> str:
        if group.customProperty("wmsShortName"):
            return group.customProperty("wmsShortName")
        elif hasattr(group, "groupLayer"):
            # since QGIS 3.38
            if group.groupLayer():
                if group.groupLayer().serverProperties():
                    if group.groupLayer().serverProperties().shortName():
                        return group.groupLayer().serverProperties().shortName()
        short_name = Exporter.sanitize_name(group.name(), lower=True)
        if short_name == "_":
            # this is the tree root, we return empty string here
            return ""
        return short_name

    @staticmethod
    def sanitize_name(raw: str, lower: bool = False) -> str:
        """
        Transforms an arbitrary string into a WMS/WFS and URL compatible short name for a job_layer_definition or group.

        Steps:
        1. Unicode‑NFD → ASCII‑transliteration (removes umlauts/diacritics).
        2. All chars, which are NOT [A‑Za‑z0‑9_.‑], will be replaced by '_' ersetzen.
        3. Reduce multiple underscore '_' to a single one.
        4. Remove leading '_', '.', '-'.
        5. If the string is empty OR does not start with a letter OR not start with '_',
           a leading '_' will be added.
        6. Optional all will be converted to lowercase (lower=True).
        """
        # 1. cleaning to ASCII
        ascii_str = (
            unicodedata.normalize("NFKD", raw).encode("ascii", "ignore").decode()
        )
        # 2. not allowed → '_'
        ascii_str = re.sub(r"[^A-Za-z0-9_.-]+", "_", ascii_str)
        # 3. remove multiple '_'
        ascii_str = re.sub(r"_+", "_", ascii_str)
        # 4. remove trailing chars
        ascii_str = ascii_str.strip("._-")
        # 5. ensure first char is correct (mainly xml stuff and URL)
        if not ascii_str or not re.match(r"[A-Za-z_]", ascii_str[0]):
            ascii_str = "_" + ascii_str
        # 6. Optional lowercase
        if lower:
            ascii_str = ascii_str.lower()
        return ascii_str

    @staticmethod
    def obtain_editor_widget_type_from_qgis_field(
        field: QgsField,
    ) -> Tuple[str, str, str, str] | Tuple[str, None, None, None]:
        """
        We simply mimikri [QGIS Server here](https://github.com/qgis/QGIS/blob/de98779ebb117547364ec4cff433f062374e84a3/src/server/services/wfs/qgswfsdescribefeaturetype.cpp#L153-L192)

        TODO: This could be improved alot! Maybe we can also backport that to QGIS core some day?

        Args:
            field: The field of an `QgsVectorLayer`.

        Returns:
            Unified type name regarding
            [XSD spec](https://www.w3.org/TR/xmlschema11-2/#built-in-primitive-datatypes)
        """
        attribute_type = field.type()
        setup = field.editorWidgetSetup()
        config = setup.config()
        editor_widget_type = setup.type()
        if editor_widget_type == "DateTime":
            field_format = config.get(
                "field_format", QgsDateTimeFieldFormatter.defaultFormat(attribute_type)
            )
            if field_format == QgsDateTimeFieldFormatter.TIME_FORMAT:
                return editor_widget_type, "time", "string", "time"
            elif field_format == QgsDateTimeFieldFormatter.DATE_FORMAT:
                return editor_widget_type, "date", "string", "date"
            elif field_format == QgsDateTimeFieldFormatter.DATETIME_FORMAT:
                return editor_widget_type, "dateTime", "string", "date-time"
            elif field_format == QgsDateTimeFieldFormatter.QT_ISO_FORMAT:
                return editor_widget_type, "dateTime", "string", "date-time"
        elif editor_widget_type == "Range":
            if config.get("Precision"):
                config_precision = int(config["Precision"])
                if config_precision != field.precision():
                    if config_precision == 0:
                        return editor_widget_type, "integer", "integer", "int64"
                    else:
                        return editor_widget_type, "decimal", "number", "double"

        logging.error(
            f"Editor widget type was not handled as expected: {editor_widget_type}"
        )
        return editor_widget_type, None, None, None

    @staticmethod
    def obtain_simple_types_from_qgis_field_json(
        field: QgsField,
    ) -> Tuple[str, str] | Tuple[str, None]:
        """
        Delivers the type match for json based on the field QgsField type.

        Args:
            field: The field of an `QgsVectorLayer`.

        Returns:
            Unified type name and format in a tuple.
        """
        attribute_type = field.type()
        if attribute_type == QMetaType.Type.Int:
            return "integer", None
        elif attribute_type == QMetaType.Type.UInt:
            return "integer", "uint32"
        elif attribute_type == QMetaType.Type.LongLong:
            return "integer", "int64"
        elif attribute_type == QMetaType.Type.ULongLong:
            return "integer", "uint64"
        elif attribute_type == QMetaType.Type.Double:
            return "number", "double"
        elif attribute_type == QMetaType.Type.Float:
            return "number", "float"
        elif attribute_type == QMetaType.Type.Bool:
            return "boolean", None
        elif attribute_type == QMetaType.Type.QDate:
            return "string", "date"
        elif attribute_type == QMetaType.Type.QTime:
            return "string", "time"
        elif attribute_type == QMetaType.Type.QDateTime:
            return "string", "date-time"
        else:
            # we handle all unknown types as string. Since its for JavaScript, this should be safe.
            return "string", None

    @staticmethod
    def create_qsl_source_wms(datasource: dict) -> WmsSource:
        return WmsSource.from_qgis_decoded_uri(datasource)

    @staticmethod
    def create_qsl_source_vector_tiles(datasource: dict) -> VectorTileSource:
        return VectorTileSource.from_qgis_decoded_uri(datasource)

    @staticmethod
    def create_qsl_source_xyz(datasource: dict) -> XYZSource:
        return XYZSource.from_qgis_decoded_uri(datasource)

    @staticmethod
    def create_qsl_source_wmts(datasource: dict) -> WmtsSource:
        return WmtsSource.from_qgis_decoded_uri(datasource)

    @staticmethod
    def create_qsl_source_gdal(datasource: dict) -> GdalSource:
        return GdalSource.from_qgis_decoded_uri(datasource)

    @staticmethod
    def create_qsl_source_ogr(datasource: dict) -> OgrSource:
        return OgrSource.from_qgis_decoded_uri(datasource)

    def create_qsl_source_postgresql(self, datasource: dict) -> PostgresSource:
        config = datasource
        if datasource.get("service"):
            if self.pg_service_configs.get(datasource["service"]):
                service_config = self.pg_service_configs[datasource["service"]]
            else:
                service_config = {}
            if service_config == {}:
                logging.error(
                    f"""
                    There was a pg_service configuration in the project but it could not be found in
                    available configurations: {datasource["service"]}
                    Its highly possible that the exported project won't work.
                """
                )
            # merging pg_service content with config of qgis project (qgis project config overwrites
            # pg_service configs
            config = Exporter.merge_dicts(service_config, datasource)
        if config.get("username"):
            config["username"]
        elif config.get("user"):
            config["user"]
        else:
            raise LookupError(
                f"Configuration does not contain any info about the db user name {config}"
            )
        postgres_source = PostgresSource.from_qgis_decoded_uri(config)
        postgres_source.ssl_mode_text = self.decide_sslmode(postgres_source.sslmode)
        return postgres_source

    @staticmethod
    def merge_dicts(a, b):
        """
        Merges two dicts recursively, b values overwrites a values.

        Args:
            a: Dictionary which is the base.
            b: Dictionary which is merged in and whose values overwrites the one.

        Returns:
            The merged dict.
        """
        result = a.copy()
        for key, value in b.items():
            if (
                key in result
                and isinstance(result[key], dict)
                and isinstance(value, dict)
            ):
                result[key] = Exporter.merge_dicts(result[key], value)
            else:
                result[key] = value
        return result

    @staticmethod
    def decide_sslmode(ssl_mode: int) -> str:
        """
        Mapper to map ssl modes from QGIS to plain postgres.

        Args:
            ssl_mode: The ssl mode of the datasource.

        Returns:
            The string representation of the ssl mode as it is used by postgres connections.
        """
        return QgsDataSourceUri.encodeSslMode(int(ssl_mode))

    @staticmethod
    def create_qsl_crs_from_qgs_layer(layer: QgsMapLayer) -> Crs:
        """
        Translates the QGIS job_layer_definition crs information into an instance of the QGIS-Server-Light interface Crs
        instance.

        Args:
            layer: The job_layer_definition to take the crs information from.

        Returns:
            The instance of the QSL interface Crs.
        """
        layer_crs = layer.dataProvider().crs()
        return Crs(
            postgis_srid=layer_crs.postgisSrid(),
            auth_id=layer_crs.authid(),
            ogc_uri=layer_crs.toOgcUri(),
            ogc_urn=layer_crs.toOgcUrn(),
        )

    @staticmethod
    def get_layer_short_name(layer: QgsLayerTreeLayer) -> str:
        """
        This method determines which name is used as the short name of the job_layer_definition.

        Args:
            layer: The job_layer_definition which the short name should be derived from.

        Returns:
            The short name.
        """
        if layer.layer().shortName():
            return layer.layer().shortName()
        elif hasattr(layer.layer(), "serverProperties"):
            if layer.layer().serverProperties().shortName():
                return layer.layer().serverProperties().shortName()
        return layer.layer().id()

    @staticmethod
    def make_wgs84_geom_transform(project, layer) -> QgsCoordinateTransform:
        """
        Creates a QgisCoordinateTransform context to transform a job_layer_definition to EPSG:4326 aka wgs84.

        Args:
            project: The QGIS project instance.
            layer: The job_layer_definition which's extent should be reprojected.

        Returns:
            The QGIS transformation context.
        """
        source_crs = layer.crs()
        epsg_4326 = QgsCoordinateReferenceSystem("EPSG:4326")
        return QgsCoordinateTransform(source_crs, epsg_4326, project)

    @staticmethod
    def create_qsl_bbox_from_qgis_rectangle_wgs84(
        project: QgsProject, layer: QgsMapLayer, extent: QgsRectangle
    ) -> BBox:
        """
        Reprojects the job_layer_definition's extent using WGS84.

        Args:
            project: The QGIS project instance for projection context.
            layer: The job_layer_definition which for the projection context.
            extent: The extent which will be reprojected.

        Returns:
            The QSL bbox reprojected to WGS84.
        """
        transformation_context = Exporter.make_wgs84_geom_transform(project, layer)
        reprojected_extent = transformation_context.transform(extent)
        return Exporter.create_qsl_bbox_from_qgis_rectangle_extent(reprojected_extent)

    @staticmethod
    def create_qsl_bbox_from_qgis_rectangle_extent(extent: QgsRectangle) -> BBox:
        return BBox(
            x_min=extent.xMinimum(),
            x_max=extent.xMaximum(),
            y_min=extent.yMinimum(),
            y_max=extent.yMaximum(),
        )

    @staticmethod
    def get_layer_type(layer: QgsMapLayer) -> str | None:
        """
        Gets the type of the given Qgis job_layer_definition as a string if the type is supported. This is to flatten the
        understanding of layers from qgis into something we can handle.

        Args:
            layer: The job_layer_definition to decide the type for.

        Returns:
            "raster", "vector" or "custom" if a job_layer_definition matched and None otherwise.
        """
        if isinstance(layer, QgsRasterLayer):
            return "raster"
        elif isinstance(layer, QgsVectorLayer):
            return "vector"
        elif (
            isinstance(layer, QgsVectorTileLayer)
            or isinstance(layer, QgsTiledSceneLayer)
            or isinstance(layer, QgsPointCloudLayer)
            or isinstance(layer, QgsMeshLayer)
        ):
            return "custom"
        else:
            logging.error(f"Not implemented: {layer.type()}")
        return None

    @staticmethod
    def open_qgis_project(path_to_project: str) -> QgsProject:
        """


        Args:
            path_to_project: The absolute path on the file system where the project can be read from.

        Returns:
            The opened project (read).
        """
        project = QgsProject.instance()
        project.read(path_to_project)
        return project

    @staticmethod
    def prepare_qgis_project_name(project: QgsProject) -> tuple[str, str]:
        """


        Args:
            project: The instantiated QGIS project.

        Returns:
            Tuple of str version, name
        """
        # TODO: Find a good approach to recognize different "versions" of a project.
        name = project.baseName()
        parts = name.split(".")
        version = parts.pop(0)
        assembled_name = ".".join(parts)
        if assembled_name == "":
            assembled_name = project.title()
        return version, assembled_name

    @staticmethod
    def extract_metadata(project: QgsProject) -> MetaData:
        """
        Creates a QSL interface instance for metadate pulled out of the QGIS project.

        Args:
            project: The instantiated QGIS project.

        Returns:
            The metadata interface instance.
        """
        _meta = project.metadata()
        wms_entries = Exporter.get_project_server_entries(project, "wms")
        service = Service(**dict(sorted({**wms_entries}.items())))
        return MetaData(
            service=service,
            author=_meta.author(),
            categories=_meta.categories(),
            creationDateTime=_meta.creationDateTime().toPyDateTime().isoformat(),
            language=_meta.language(),
            links=[link.url for link in _meta.links()],
        )

    @staticmethod
    def get_project_server_entries(project, scope_or_scopes: Union[str, list]) -> dict:
        """
        Gets values from the fields displayed in QGIS under Project > Properties > Server.
        Returns a Dictionary holding all pairs of <key, value> found at the corresponding scopes.
        Example:
            given   scope_or_scope = "wms" (or: ["wms"])
            returns { <wms_key1>: <wms_key1_value>, <wms_key2>: <wms_key2_value> ... }
        For now the implementation supports only WMS fields but can be easily expanded by
        adding <key/values> to the Dictionary below.
        """
        supported_scopes = {
            "wms": {
                "scopes": [
                    ("WMSContactOrganization", "contact_organization"),
                    ("WMSContactMail", "contact_mail"),
                    ("WMSContactPerson", "contact_person"),
                    ("WMSContactPhone", "contact_phone"),
                    ("WMSContactPosition", "contact_position"),
                    ("WMSFees", "fees"),
                    ("WMSKeywordList", "keyword_list"),
                    ("WMSOnlineResource", "online_resource"),
                    ("WMSServiceAbstract", "service_abstract"),
                    ("WMSServiceTitle", "service_title"),
                    ("WMSUrl", "resource_url"),
                ],
                "keys": ["/"],
            }
        }

        scopes = (
            [scope_or_scopes] if isinstance(scope_or_scopes, str) else scope_or_scopes
        )

        for scope in scopes:
            if scope not in supported_scopes:
                supported = ", ".join(supported_scopes.keys())
                error_detail = f"This scope is not supported: {scope}. Supported scopes: {supported}"
                raise ValueError(error_detail)

            scope_entries = supported_scopes[scope]["scopes"]
            key_entries = supported_scopes[scope]["keys"]
            to_collect = zip_longest(
                scope_entries, key_entries, fillvalue=key_entries[0]
            )

            def collect(acc, pair):
                scope, key = pair
                qgis_scope_name, our_scope_name = scope

                if "list" in qgis_scope_name.lower():
                    # PyQGIS sometimes violates Liskov's substitution principle so naming tricks needed
                    list_as_text = ", ".join(
                        project.readListEntry(qgis_scope_name, key)[0]
                    )
                    acc.append((our_scope_name, list_as_text))
                else:
                    acc.append(
                        (our_scope_name, project.readEntry(qgis_scope_name, key)[0])
                    )

                return acc

            return dict(reduce(collect, to_collect, []))

    @staticmethod
    def create_style_list(qgs_layer: QgsMapLayer) -> List[Style]:
        style_names = qgs_layer.styleManager().styles()
        style_list = []
        for style_name in style_names:
            style_doc = QDomDocument()
            qgs_layer.styleManager().setCurrentStyle(style_name)
            qgs_layer.exportNamedStyle(style_doc)
            style_list.append(
                Style(
                    name=style_name,
                    definition=urlsafe_b64encode(
                        zlib.compress(style_doc.toByteArray())
                    ).decode(),
                )
            )
        return style_list

path = qgis_project_path instance-attribute

pg_service_configs = pg_service_configs or {} instance-attribute

qgis_project = self.open_qgis_project(qgis_project_path) instance-attribute

qgis_project_tree_root = self.qgis_project.layerTreeRoot() instance-attribute

qsl_config = Config(project=(self.qsl_project), meta_data=(self.qsl_project_metadata), tree=(self.qsl_tree), datasets=(self.qsl_datasets)) instance-attribute

qsl_datasets = Datasets() instance-attribute

qsl_project = Project(name=(self.assembled_name), version=(self.version)) instance-attribute

qsl_project_metadata = self.extract_metadata(self.qgis_project) instance-attribute

qsl_tree = Tree() instance-attribute

unify_layer_names_by_group = unify_layer_names_by_group instance-attribute

unify_layer_names_by_group_separator = unify_layer_names_by_group_separator instance-attribute

__init__(qgis_project_path: str, unify_layer_names_by_group=False, unify_layer_names_by_group_separator='.', pg_service_configs=None)

Source code in src/qgis_server_light/exporter/extract.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def __init__(
    self,
    qgis_project_path: str,
    unify_layer_names_by_group=False,
    unify_layer_names_by_group_separator=".",
    pg_service_configs=None,
):
    self.unify_layer_names_by_group_separator = unify_layer_names_by_group_separator
    self.path = qgis_project_path
    self.unify_layer_names_by_group = unify_layer_names_by_group
    self.pg_service_configs = pg_service_configs or {}

    # prepare QGIS instances
    self.qgis_project = self.open_qgis_project(qgis_project_path)
    self.qgis_project_tree_root = self.qgis_project.layerTreeRoot()
    self.version, self.assembled_name = self.prepare_qgis_project_name(
        self.qgis_project
    )

    # prepare QSL interface instances
    self.qsl_tree = Tree()
    self.qsl_datasets = Datasets()
    self.qsl_project = Project(name=self.assembled_name, version=self.version)
    self.qsl_project_metadata = self.extract_metadata(self.qgis_project)
    self.qsl_config = Config(
        project=self.qsl_project,
        meta_data=self.qsl_project_metadata,
        tree=self.qsl_tree,
        datasets=self.qsl_datasets,
    )

create_qsl_bbox_from_qgis_rectangle_extent(extent: QgsRectangle) -> BBox staticmethod

Source code in src/qgis_server_light/exporter/extract.py
785
786
787
788
789
790
791
792
@staticmethod
def create_qsl_bbox_from_qgis_rectangle_extent(extent: QgsRectangle) -> BBox:
    return BBox(
        x_min=extent.xMinimum(),
        x_max=extent.xMaximum(),
        y_min=extent.yMinimum(),
        y_max=extent.yMaximum(),
    )

create_qsl_bbox_from_qgis_rectangle_wgs84(project: QgsProject, layer: QgsMapLayer, extent: QgsRectangle) -> BBox staticmethod

Reprojects the job_layer_definition's extent using WGS84.

Parameters:

  • project (QgsProject) –

    The QGIS project instance for projection context.

  • layer (QgsMapLayer) –

    The job_layer_definition which for the projection context.

  • extent (QgsRectangle) –

    The extent which will be reprojected.

Returns:

  • BBox

    The QSL bbox reprojected to WGS84.

Source code in src/qgis_server_light/exporter/extract.py
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
@staticmethod
def create_qsl_bbox_from_qgis_rectangle_wgs84(
    project: QgsProject, layer: QgsMapLayer, extent: QgsRectangle
) -> BBox:
    """
    Reprojects the job_layer_definition's extent using WGS84.

    Args:
        project: The QGIS project instance for projection context.
        layer: The job_layer_definition which for the projection context.
        extent: The extent which will be reprojected.

    Returns:
        The QSL bbox reprojected to WGS84.
    """
    transformation_context = Exporter.make_wgs84_geom_transform(project, layer)
    reprojected_extent = transformation_context.transform(extent)
    return Exporter.create_qsl_bbox_from_qgis_rectangle_extent(reprojected_extent)

create_qsl_crs_from_qgs_layer(layer: QgsMapLayer) -> Crs staticmethod

Translates the QGIS job_layer_definition crs information into an instance of the QGIS-Server-Light interface Crs instance.

Parameters:

  • layer (QgsMapLayer) –

    The job_layer_definition to take the crs information from.

Returns:

  • Crs

    The instance of the QSL interface Crs.

Source code in src/qgis_server_light/exporter/extract.py
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
@staticmethod
def create_qsl_crs_from_qgs_layer(layer: QgsMapLayer) -> Crs:
    """
    Translates the QGIS job_layer_definition crs information into an instance of the QGIS-Server-Light interface Crs
    instance.

    Args:
        layer: The job_layer_definition to take the crs information from.

    Returns:
        The instance of the QSL interface Crs.
    """
    layer_crs = layer.dataProvider().crs()
    return Crs(
        postgis_srid=layer_crs.postgisSrid(),
        auth_id=layer_crs.authid(),
        ogc_uri=layer_crs.toOgcUri(),
        ogc_urn=layer_crs.toOgcUrn(),
    )

create_qsl_field_from_qgis_field(field: QgsField, is_primary_key: bool) -> Field staticmethod

Source code in src/qgis_server_light/exporter/extract.py
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
@staticmethod
def create_qsl_field_from_qgis_field(
    field: QgsField, is_primary_key: bool
) -> Field:
    attribute_type_xml = Exporter.obtain_simple_types_from_qgis_field_xml(field)
    (
        editor_widget_type,
        editor_widget_type_wfs,
        editor_widget_type_json,
        editor_widget_type_json_format,
    ) = Exporter.obtain_editor_widget_type_from_qgis_field(field)
    (
        attribute_type_json,
        attribute_type_json_format,
    ) = Exporter.obtain_simple_types_from_qgis_field_json(field)
    return Field(
        is_primary_key=is_primary_key,
        name=field.name(),
        type=field.typeName(),
        type_wfs=attribute_type_xml,
        type_oapif=attribute_type_json,
        type_oapif_format=attribute_type_json_format,
        alias=field.alias() or field.name().title(),
        comment=field.comment(),
        nullable=is_primary_key and Exporter.obtain_nullable(field),
        length=Exporter.provide_field_length(field),
        precision=Exporter.provide_field_precision(field),
        editor_widget_type=editor_widget_type,
        editor_widget_type_wfs=editor_widget_type_wfs,
        editor_widget_type_oapif=editor_widget_type_json,
        editor_widget_type_oapif_format=editor_widget_type_json_format,
    )

create_qsl_fields_from_qgis_field(layer: QgsVectorLayer) -> List[Field] staticmethod

Source code in src/qgis_server_light/exporter/extract.py
428
429
430
431
432
433
434
435
436
437
438
@staticmethod
def create_qsl_fields_from_qgis_field(layer: QgsVectorLayer) -> List[Field]:
    fields = []
    pk_indexes = layer.dataProvider().pkAttributeIndexes()
    for field_index, field in enumerate(layer.fields().toList()):
        fields.append(
            Exporter.create_qsl_field_from_qgis_field(
                field, (field_index in pk_indexes)
            )
        )
    return fields

create_qsl_source_gdal(datasource: dict) -> GdalSource staticmethod

Source code in src/qgis_server_light/exporter/extract.py
637
638
639
@staticmethod
def create_qsl_source_gdal(datasource: dict) -> GdalSource:
    return GdalSource.from_qgis_decoded_uri(datasource)

create_qsl_source_ogr(datasource: dict) -> OgrSource staticmethod

Source code in src/qgis_server_light/exporter/extract.py
641
642
643
@staticmethod
def create_qsl_source_ogr(datasource: dict) -> OgrSource:
    return OgrSource.from_qgis_decoded_uri(datasource)

create_qsl_source_postgresql(datasource: dict) -> PostgresSource

Source code in src/qgis_server_light/exporter/extract.py
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
def create_qsl_source_postgresql(self, datasource: dict) -> PostgresSource:
    config = datasource
    if datasource.get("service"):
        if self.pg_service_configs.get(datasource["service"]):
            service_config = self.pg_service_configs[datasource["service"]]
        else:
            service_config = {}
        if service_config == {}:
            logging.error(
                f"""
                There was a pg_service configuration in the project but it could not be found in
                available configurations: {datasource["service"]}
                Its highly possible that the exported project won't work.
            """
            )
        # merging pg_service content with config of qgis project (qgis project config overwrites
        # pg_service configs
        config = Exporter.merge_dicts(service_config, datasource)
    if config.get("username"):
        config["username"]
    elif config.get("user"):
        config["user"]
    else:
        raise LookupError(
            f"Configuration does not contain any info about the db user name {config}"
        )
    postgres_source = PostgresSource.from_qgis_decoded_uri(config)
    postgres_source.ssl_mode_text = self.decide_sslmode(postgres_source.sslmode)
    return postgres_source

create_qsl_source_vector_tiles(datasource: dict) -> VectorTileSource staticmethod

Source code in src/qgis_server_light/exporter/extract.py
625
626
627
@staticmethod
def create_qsl_source_vector_tiles(datasource: dict) -> VectorTileSource:
    return VectorTileSource.from_qgis_decoded_uri(datasource)

create_qsl_source_wms(datasource: dict) -> WmsSource staticmethod

Source code in src/qgis_server_light/exporter/extract.py
621
622
623
@staticmethod
def create_qsl_source_wms(datasource: dict) -> WmsSource:
    return WmsSource.from_qgis_decoded_uri(datasource)

create_qsl_source_wmts(datasource: dict) -> WmtsSource staticmethod

Source code in src/qgis_server_light/exporter/extract.py
633
634
635
@staticmethod
def create_qsl_source_wmts(datasource: dict) -> WmtsSource:
    return WmtsSource.from_qgis_decoded_uri(datasource)

create_qsl_source_xyz(datasource: dict) -> XYZSource staticmethod

Source code in src/qgis_server_light/exporter/extract.py
629
630
631
@staticmethod
def create_qsl_source_xyz(datasource: dict) -> XYZSource:
    return XYZSource.from_qgis_decoded_uri(datasource)

create_style_list(qgs_layer: QgsMapLayer) -> List[Style] staticmethod

Source code in src/qgis_server_light/exporter/extract.py
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
@staticmethod
def create_style_list(qgs_layer: QgsMapLayer) -> List[Style]:
    style_names = qgs_layer.styleManager().styles()
    style_list = []
    for style_name in style_names:
        style_doc = QDomDocument()
        qgs_layer.styleManager().setCurrentStyle(style_name)
        qgs_layer.exportNamedStyle(style_doc)
        style_list.append(
            Style(
                name=style_name,
                definition=urlsafe_b64encode(
                    zlib.compress(style_doc.toByteArray())
                ).decode(),
            )
        )
    return style_list

create_unified_short_name(short_name: str, path: list[str])

Creates the unified short name, separated by the configured separator.

Parameters:

  • short_name (str) –

    The short name either of the group or the job_layer_definition.

  • path (list[str]) –

    The path is a list of string which stores the information of the current tree path. This is used to construct a string for unifying job_layer_definition names by their tree path.

Returns:

Source code in src/qgis_server_light/exporter/extract.py
327
328
329
330
331
332
333
334
335
336
337
338
339
340
def create_unified_short_name(self, short_name: str, path: list[str]):
    """
    Creates the unified short name, separated by the configured separator.

    Args:
        short_name: The short name either of the group or the job_layer_definition.
        path: The path is a list of string which stores the information of the current tree path. This is
            used to construct a string for unifying job_layer_definition names by their tree path.

    Returns:

    """
    short_name_parts = path + [short_name]
    return self.unify_layer_names_by_group_separator.join(short_name_parts)

decide_sslmode(ssl_mode: int) -> str staticmethod

Mapper to map ssl modes from QGIS to plain postgres.

Parameters:

  • ssl_mode (int) –

    The ssl mode of the datasource.

Returns:

  • str

    The string representation of the ssl mode as it is used by postgres connections.

Source code in src/qgis_server_light/exporter/extract.py
699
700
701
702
703
704
705
706
707
708
709
710
@staticmethod
def decide_sslmode(ssl_mode: int) -> str:
    """
    Mapper to map ssl modes from QGIS to plain postgres.

    Args:
        ssl_mode: The ssl mode of the datasource.

    Returns:
        The string representation of the ssl mode as it is used by postgres connections.
    """
    return QgsDataSourceUri.encodeSslMode(int(ssl_mode))

decode_datasource(layer: QgsMapLayer) -> dict

Decodes a QGIS map job_layer_definition datasource into a dict and ensures that types are pythonic and pathes are clean for further usage.

Parameters:

  • layer (QgsMapLayer) –

    The job_layer_definition which the datasource should be extracted from.

Returns:

  • dict

    The decoded and cleaned datasource.

Source code in src/qgis_server_light/exporter/extract.py
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
def decode_datasource(self, layer: QgsMapLayer) -> dict:
    """
    Decodes a QGIS map job_layer_definition datasource into a dict and ensures that types are pythonic and pathes are
    clean for further usage.

    Args:
        layer: The job_layer_definition which the datasource should be extracted from.

    Returns:
        The decoded and cleaned datasource.
    """
    decoded = QgsProviderRegistry.instance().decodeUri(
        layer.providerType(), layer.dataProvider().dataSourceUri()
    )
    logging.debug(f"Layer source: {decoded}")
    for key in decoded:
        if str(decoded[key]) == "None":
            decoded[key] = None
        elif str(decoded[key]) == "NULL":
            decoded[key] = None
        else:
            decoded[key] = str(decoded[key])
        if key == "path":
            decoded[key] = decoded[key].replace(
                f"{self.qgis_project.readPath('./')}/", ""
            )
    return decoded

extract_group(group: QgsLayerTreeGroup, path: list[str])

Collects data pertaining to a QGIS job_layer_definition tree group. Basically this translates a QgsLayerTreeGroup into a QGIS-Server-Light TreeGroup.

Parameters:

  • group (QgsLayerTreeGroup) –

    The group which is handled.

  • path (list[str]) –

    The path is a list of string which stores the information of the current tree path. This is used to construct a string for unifying job_layer_definition names by their tree path.

Source code in src/qgis_server_light/exporter/extract.py
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
def extract_group(
    self,
    group: QgsLayerTreeGroup,
    path: list[str],
):
    """
    Collects data pertaining to a QGIS job_layer_definition tree group. Basically this translates a QgsLayerTreeGroup
    into a QGIS-Server-Light TreeGroup.

    Args:
        group: The group which is handled.
        path: The path is a list of string which stores the information of the current tree path. This is
            used to construct a string for unifying job_layer_definition names by their tree path.
    """
    children = []
    for child in group.children():
        if isinstance(child, QgsLayerTreeGroup):
            children.append(self.get_group_short_name(child))
        else:
            children.append(child.layer().id())
    self.qsl_tree.members.append(
        TreeGroup(
            id=self.get_group_short_name(group),
            name=self.get_group_short_name(group),
            children=children,
        )
    )
    self.qsl_datasets.group.append(
        Group(
            id=self.get_group_short_name(group),
            name=self.get_group_short_name(group),
            title=self.get_group_title(group),
        )
    )

extract_metadata(project: QgsProject) -> MetaData staticmethod

Creates a QSL interface instance for metadate pulled out of the QGIS project.

Parameters:

  • project (QgsProject) –

    The instantiated QGIS project.

Returns:

  • MetaData

    The metadata interface instance.

Source code in src/qgis_server_light/exporter/extract.py
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
@staticmethod
def extract_metadata(project: QgsProject) -> MetaData:
    """
    Creates a QSL interface instance for metadate pulled out of the QGIS project.

    Args:
        project: The instantiated QGIS project.

    Returns:
        The metadata interface instance.
    """
    _meta = project.metadata()
    wms_entries = Exporter.get_project_server_entries(project, "wms")
    service = Service(**dict(sorted({**wms_entries}.items())))
    return MetaData(
        service=service,
        author=_meta.author(),
        categories=_meta.categories(),
        creationDateTime=_meta.creationDateTime().toPyDateTime().isoformat(),
        language=_meta.language(),
        links=[link.url for link in _meta.links()],
    )

extract_save_layer(child: QgsLayerTreeLayer, path: list[str], types_from_editor_widget: bool = False)

Save the given job_layer_definition to the output path.

Source code in src/qgis_server_light/exporter/extract.py
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
def extract_save_layer(
    self,
    child: QgsLayerTreeLayer,
    path: list[str],
    types_from_editor_widget: bool = False,
):
    """Save the given job_layer_definition to the output path."""
    layer = child.layer()
    layer_type = self.get_layer_type(layer)
    if layer_type is None:
        return
    decoded = self.decode_datasource(layer)
    short_name = self.short_name(self.get_layer_short_name(child), path)
    if layer.isSpatial():
        crs = self.create_qsl_crs_from_qgs_layer(layer)
        layer_extent = layer.extent()
        bbox_wgs84 = self.create_qsl_bbox_from_qgis_rectangle_wgs84(
            self.qgis_project, layer, layer_extent
        )
        bbox = self.create_qsl_bbox_from_qgis_rectangle_extent(layer_extent)
        is_spatial = True
    else:
        crs = None
        bbox_wgs84 = None
        bbox = None
        is_spatial = False
    if layer_type == "vector":
        if layer.providerType().lower() == "ogr":
            source = DataSource(ogr=self.create_qsl_source_ogr(decoded))
        elif layer.providerType().lower() == "postgres":
            source = DataSource(postgres=self.create_qsl_source_postgresql(decoded))
            source_path_parts = []
            DictEncoder().encode(source.postgres)
            for field in fields(source.postgres):
                source_path_parts.append(
                    f"{field.name}={getattr(source.postgres, field.name)!r}"
                )
        elif layer.providerType().lower() == "wfs":
            # TODO: Correctly implement source!
            source = WfsSource()
        else:
            logging.error(
                f"Unknown provider type {layer.providerType().lower()} for job_layer_definition {layer.title() or layer.name()}"
            )
            return
        qsl_vector_dataset_fields = self.create_qsl_fields_from_qgis_field(layer)
        self.qsl_datasets.vector.append(
            Vector(
                name=short_name,
                title=layer.title() or layer.name(),
                styles=self.create_style_list(layer),
                driver=layer.providerType(),
                bbox_wgs84=bbox_wgs84,
                fields=qsl_vector_dataset_fields,
                source=source,
                id=layer.id(),
                crs=crs,
                bbox=bbox,
                minimum_scale=layer.minimumScale(),
                maximum_scale=layer.maximumScale(),
                geometry_type_simple=layer.geometryType().name,
                geometry_type_wkb=layer.wkbType().name,
                is_spatial=is_spatial,
            )
        )
    elif layer_type == "raster":
        if layer.providerType() == "gdal":
            source = DataSource(gdal=self.create_qsl_source_gdal(decoded))
        elif layer.providerType() == "wms":
            if "tileMatrixSet" in decoded:
                source = DataSource(wmts=self.create_qsl_source_wmts(decoded))
            else:
                if decoded.get("type") == "xyz":
                    source = DataSource(xyz=self.create_qsl_source_xyz(decoded))
                else:
                    source = DataSource(wms=self.create_qsl_source_wms(decoded))
        else:
            logging.error(f"Unknown provider type: {layer.providerType().lower()}")
            return
        if source is not None:
            self.qsl_datasets.raster.append(
                Raster(
                    name=short_name,
                    title=layer.title() or layer.name(),
                    styles=self.create_style_list(layer),
                    driver=layer.providerType(),
                    bbox_wgs84=bbox_wgs84,
                    source=source,
                    id=layer.id(),
                    crs=crs,
                    bbox=bbox,
                    minimum_scale=layer.minimumScale(),
                    maximum_scale=layer.maximumScale(),
                    is_spatial=is_spatial,
                )
            )
        else:
            logging.error(
                f"Source was None this is not expected. Layer was: {short_name}, QGIS job_layer_definition ID:{layer.id()}"
            )
    elif layer_type == "custom":
        if layer.providerType().lower() in ["xyzvectortiles", "mbtilesvectortiles"]:
            source = DataSource(
                vector_tile=self.create_qsl_source_vector_tiles(decoded)
            )
        else:
            logging.error(
                f"Unknown provider type: {layer.providerType().lower()} Layer was: {short_name}, QGIS job_layer_definition ID:{layer.id()}"
            )
            # TODO: make this more configurable
            return
        self.qsl_datasets.custom.append(
            Custom(
                name=short_name,
                title=layer.title() or layer.name(),
                styles=self.create_style_list(layer),
                driver=layer.providerType(),
                bbox_wgs84=bbox_wgs84,
                source=source,
                id=layer.id(),
                crs=crs,
                bbox=bbox,
                minimum_scale=layer.minimumScale(),
                maximum_scale=layer.maximumScale(),
                is_spatial=is_spatial,
            )
        )
    else:
        logging.error(
            f'Unknown layer_type "{layer_type}" Layer was: {short_name}, QGIS job_layer_definition ID:{layer.id()}'
        )
        return

get_group_short_name(group: QgsLayerTreeGroup) -> str staticmethod

Source code in src/qgis_server_light/exporter/extract.py
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
@staticmethod
def get_group_short_name(group: QgsLayerTreeGroup) -> str:
    if group.customProperty("wmsShortName"):
        return group.customProperty("wmsShortName")
    elif hasattr(group, "groupLayer"):
        # since QGIS 3.38
        if group.groupLayer():
            if group.groupLayer().serverProperties():
                if group.groupLayer().serverProperties().shortName():
                    return group.groupLayer().serverProperties().shortName()
    short_name = Exporter.sanitize_name(group.name(), lower=True)
    if short_name == "_":
        # this is the tree root, we return empty string here
        return ""
    return short_name

get_group_title(group: QgsLayerTreeGroup) -> str staticmethod

Source code in src/qgis_server_light/exporter/extract.py
477
478
479
480
481
482
483
484
485
486
487
@staticmethod
def get_group_title(group: QgsLayerTreeGroup) -> str:
    if group.customProperty("wmsTitle"):
        return group.customProperty("wmsTitle")
    elif hasattr(group, "groupLayer"):
        # since QGIS 3.38
        if group.groupLayer():
            if group.groupLayer().serverProperties():
                if group.groupLayer().serverProperties().title():
                    return group.groupLayer().serverProperties().title()
    return group.name()

get_layer_short_name(layer: QgsLayerTreeLayer) -> str staticmethod

This method determines which name is used as the short name of the job_layer_definition.

Parameters:

  • layer (QgsLayerTreeLayer) –

    The job_layer_definition which the short name should be derived from.

Returns:

  • str

    The short name.

Source code in src/qgis_server_light/exporter/extract.py
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
@staticmethod
def get_layer_short_name(layer: QgsLayerTreeLayer) -> str:
    """
    This method determines which name is used as the short name of the job_layer_definition.

    Args:
        layer: The job_layer_definition which the short name should be derived from.

    Returns:
        The short name.
    """
    if layer.layer().shortName():
        return layer.layer().shortName()
    elif hasattr(layer.layer(), "serverProperties"):
        if layer.layer().serverProperties().shortName():
            return layer.layer().serverProperties().shortName()
    return layer.layer().id()

get_layer_type(layer: QgsMapLayer) -> str | None staticmethod

Gets the type of the given Qgis job_layer_definition as a string if the type is supported. This is to flatten the understanding of layers from qgis into something we can handle.

Parameters:

  • layer (QgsMapLayer) –

    The job_layer_definition to decide the type for.

Returns:

  • str | None

    "raster", "vector" or "custom" if a job_layer_definition matched and None otherwise.

Source code in src/qgis_server_light/exporter/extract.py
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
@staticmethod
def get_layer_type(layer: QgsMapLayer) -> str | None:
    """
    Gets the type of the given Qgis job_layer_definition as a string if the type is supported. This is to flatten the
    understanding of layers from qgis into something we can handle.

    Args:
        layer: The job_layer_definition to decide the type for.

    Returns:
        "raster", "vector" or "custom" if a job_layer_definition matched and None otherwise.
    """
    if isinstance(layer, QgsRasterLayer):
        return "raster"
    elif isinstance(layer, QgsVectorLayer):
        return "vector"
    elif (
        isinstance(layer, QgsVectorTileLayer)
        or isinstance(layer, QgsTiledSceneLayer)
        or isinstance(layer, QgsPointCloudLayer)
        or isinstance(layer, QgsMeshLayer)
    ):
        return "custom"
    else:
        logging.error(f"Not implemented: {layer.type()}")
    return None

get_project_server_entries(project, scope_or_scopes: Union[str, list]) -> dict staticmethod

Gets values from the fields displayed in QGIS under Project > Properties > Server. Returns a Dictionary holding all pairs of found at the corresponding scopes. Example: given scope_or_scope = "wms" (or: ["wms"]) returns { : , : ... } For now the implementation supports only WMS fields but can be easily expanded by adding to the Dictionary below.

Source code in src/qgis_server_light/exporter/extract.py
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
@staticmethod
def get_project_server_entries(project, scope_or_scopes: Union[str, list]) -> dict:
    """
    Gets values from the fields displayed in QGIS under Project > Properties > Server.
    Returns a Dictionary holding all pairs of <key, value> found at the corresponding scopes.
    Example:
        given   scope_or_scope = "wms" (or: ["wms"])
        returns { <wms_key1>: <wms_key1_value>, <wms_key2>: <wms_key2_value> ... }
    For now the implementation supports only WMS fields but can be easily expanded by
    adding <key/values> to the Dictionary below.
    """
    supported_scopes = {
        "wms": {
            "scopes": [
                ("WMSContactOrganization", "contact_organization"),
                ("WMSContactMail", "contact_mail"),
                ("WMSContactPerson", "contact_person"),
                ("WMSContactPhone", "contact_phone"),
                ("WMSContactPosition", "contact_position"),
                ("WMSFees", "fees"),
                ("WMSKeywordList", "keyword_list"),
                ("WMSOnlineResource", "online_resource"),
                ("WMSServiceAbstract", "service_abstract"),
                ("WMSServiceTitle", "service_title"),
                ("WMSUrl", "resource_url"),
            ],
            "keys": ["/"],
        }
    }

    scopes = (
        [scope_or_scopes] if isinstance(scope_or_scopes, str) else scope_or_scopes
    )

    for scope in scopes:
        if scope not in supported_scopes:
            supported = ", ".join(supported_scopes.keys())
            error_detail = f"This scope is not supported: {scope}. Supported scopes: {supported}"
            raise ValueError(error_detail)

        scope_entries = supported_scopes[scope]["scopes"]
        key_entries = supported_scopes[scope]["keys"]
        to_collect = zip_longest(
            scope_entries, key_entries, fillvalue=key_entries[0]
        )

        def collect(acc, pair):
            scope, key = pair
            qgis_scope_name, our_scope_name = scope

            if "list" in qgis_scope_name.lower():
                # PyQGIS sometimes violates Liskov's substitution principle so naming tricks needed
                list_as_text = ", ".join(
                    project.readListEntry(qgis_scope_name, key)[0]
                )
                acc.append((our_scope_name, list_as_text))
            else:
                acc.append(
                    (our_scope_name, project.readEntry(qgis_scope_name, key)[0])
                )

            return acc

        return dict(reduce(collect, to_collect, []))

make_wgs84_geom_transform(project, layer) -> QgsCoordinateTransform staticmethod

Creates a QgisCoordinateTransform context to transform a job_layer_definition to EPSG:4326 aka wgs84.

Parameters:

  • project

    The QGIS project instance.

  • layer

    The job_layer_definition which's extent should be reprojected.

Returns:

  • QgsCoordinateTransform

    The QGIS transformation context.

Source code in src/qgis_server_light/exporter/extract.py
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
@staticmethod
def make_wgs84_geom_transform(project, layer) -> QgsCoordinateTransform:
    """
    Creates a QgisCoordinateTransform context to transform a job_layer_definition to EPSG:4326 aka wgs84.

    Args:
        project: The QGIS project instance.
        layer: The job_layer_definition which's extent should be reprojected.

    Returns:
        The QGIS transformation context.
    """
    source_crs = layer.crs()
    epsg_4326 = QgsCoordinateReferenceSystem("EPSG:4326")
    return QgsCoordinateTransform(source_crs, epsg_4326, project)

merge_dicts(a, b) staticmethod

Merges two dicts recursively, b values overwrites a values.

Parameters:

  • a

    Dictionary which is the base.

  • b

    Dictionary which is merged in and whose values overwrites the one.

Returns:

  • The merged dict.

Source code in src/qgis_server_light/exporter/extract.py
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
@staticmethod
def merge_dicts(a, b):
    """
    Merges two dicts recursively, b values overwrites a values.

    Args:
        a: Dictionary which is the base.
        b: Dictionary which is merged in and whose values overwrites the one.

    Returns:
        The merged dict.
    """
    result = a.copy()
    for key, value in b.items():
        if (
            key in result
            and isinstance(result[key], dict)
            and isinstance(value, dict)
        ):
            result[key] = Exporter.merge_dicts(result[key], value)
        else:
            result[key] = value
    return result

obtain_editor_widget_type_from_qgis_field(field: QgsField) -> Tuple[str, str, str, str] | Tuple[str, None, None, None] staticmethod

We simply mimikri QGIS Server here

TODO: This could be improved alot! Maybe we can also backport that to QGIS core some day?

Parameters:

  • field (QgsField) –

    The field of an QgsVectorLayer.

Returns:

  • Tuple[str, str, str, str] | Tuple[str, None, None, None]

    Unified type name regarding

  • Tuple[str, str, str, str] | Tuple[str, None, None, None]
Source code in src/qgis_server_light/exporter/extract.py
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
@staticmethod
def obtain_editor_widget_type_from_qgis_field(
    field: QgsField,
) -> Tuple[str, str, str, str] | Tuple[str, None, None, None]:
    """
    We simply mimikri [QGIS Server here](https://github.com/qgis/QGIS/blob/de98779ebb117547364ec4cff433f062374e84a3/src/server/services/wfs/qgswfsdescribefeaturetype.cpp#L153-L192)

    TODO: This could be improved alot! Maybe we can also backport that to QGIS core some day?

    Args:
        field: The field of an `QgsVectorLayer`.

    Returns:
        Unified type name regarding
        [XSD spec](https://www.w3.org/TR/xmlschema11-2/#built-in-primitive-datatypes)
    """
    attribute_type = field.type()
    setup = field.editorWidgetSetup()
    config = setup.config()
    editor_widget_type = setup.type()
    if editor_widget_type == "DateTime":
        field_format = config.get(
            "field_format", QgsDateTimeFieldFormatter.defaultFormat(attribute_type)
        )
        if field_format == QgsDateTimeFieldFormatter.TIME_FORMAT:
            return editor_widget_type, "time", "string", "time"
        elif field_format == QgsDateTimeFieldFormatter.DATE_FORMAT:
            return editor_widget_type, "date", "string", "date"
        elif field_format == QgsDateTimeFieldFormatter.DATETIME_FORMAT:
            return editor_widget_type, "dateTime", "string", "date-time"
        elif field_format == QgsDateTimeFieldFormatter.QT_ISO_FORMAT:
            return editor_widget_type, "dateTime", "string", "date-time"
    elif editor_widget_type == "Range":
        if config.get("Precision"):
            config_precision = int(config["Precision"])
            if config_precision != field.precision():
                if config_precision == 0:
                    return editor_widget_type, "integer", "integer", "int64"
                else:
                    return editor_widget_type, "decimal", "number", "double"

    logging.error(
        f"Editor widget type was not handled as expected: {editor_widget_type}"
    )
    return editor_widget_type, None, None, None

obtain_nullable(field: QgsField) staticmethod

Source code in src/qgis_server_light/exporter/extract.py
403
404
405
406
407
408
409
410
@staticmethod
def obtain_nullable(field: QgsField):
    if not (
        field.constraints().constraints()
        == QgsFieldConstraints.Constraint.ConstraintNotNull
    ):
        return True
    return False

obtain_simple_types_from_qgis_field_json(field: QgsField) -> Tuple[str, str] | Tuple[str, None] staticmethod

Delivers the type match for json based on the field QgsField type.

Parameters:

  • field (QgsField) –

    The field of an QgsVectorLayer.

Returns:

  • Tuple[str, str] | Tuple[str, None]

    Unified type name and format in a tuple.

Source code in src/qgis_server_light/exporter/extract.py
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
@staticmethod
def obtain_simple_types_from_qgis_field_json(
    field: QgsField,
) -> Tuple[str, str] | Tuple[str, None]:
    """
    Delivers the type match for json based on the field QgsField type.

    Args:
        field: The field of an `QgsVectorLayer`.

    Returns:
        Unified type name and format in a tuple.
    """
    attribute_type = field.type()
    if attribute_type == QMetaType.Type.Int:
        return "integer", None
    elif attribute_type == QMetaType.Type.UInt:
        return "integer", "uint32"
    elif attribute_type == QMetaType.Type.LongLong:
        return "integer", "int64"
    elif attribute_type == QMetaType.Type.ULongLong:
        return "integer", "uint64"
    elif attribute_type == QMetaType.Type.Double:
        return "number", "double"
    elif attribute_type == QMetaType.Type.Float:
        return "number", "float"
    elif attribute_type == QMetaType.Type.Bool:
        return "boolean", None
    elif attribute_type == QMetaType.Type.QDate:
        return "string", "date"
    elif attribute_type == QMetaType.Type.QTime:
        return "string", "time"
    elif attribute_type == QMetaType.Type.QDateTime:
        return "string", "date-time"
    else:
        # we handle all unknown types as string. Since its for JavaScript, this should be safe.
        return "string", None

obtain_simple_types_from_qgis_field_xml(field: QgsField) -> str staticmethod

Parameters:

  • field (QgsField) –

    The field of an QgsVectorLayer.

Returns:

  • str

    Unified type name regarding

  • str
  • IMPORTANT ( str ) –

    If type is not matched within the function it will be string always!

Source code in src/qgis_server_light/exporter/extract.py
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
@staticmethod
def obtain_simple_types_from_qgis_field_xml(field: QgsField) -> str:
    """

    Args:
        field: The field of an `QgsVectorLayer`.

    Returns:
        Unified type name regarding
        [XSD spec](https://www.w3.org/TR/xmlschema11-2/#built-in-primitive-datatypes)
        IMPORTANT: If type is not matched within the function it will be `string` always!
    """
    attribute_type = field.type()
    if attribute_type == QMetaType.Type.Int:
        return "int"
    elif attribute_type == QMetaType.Type.UInt:
        return "unsignedInt"
    elif attribute_type == QMetaType.Type.LongLong:
        return "long"
    elif attribute_type == QMetaType.Type.ULongLong:
        return "unsignedLong"
    elif attribute_type == QMetaType.Type.Double:
        if field.length() > 0 and field.precision() == 0:
            return "integer"
        else:
            return "decimal"
    elif attribute_type == QMetaType.Type.Bool:
        return "boolean"
    elif attribute_type == QMetaType.Type.QDate:
        return "date"
    elif attribute_type == QMetaType.Type.QTime:
        return "time"
    elif attribute_type == QMetaType.Type.QDateTime:
        return "dateTime"
    else:
        return "string"

open_qgis_project(path_to_project: str) -> QgsProject staticmethod

Parameters:

  • path_to_project (str) –

    The absolute path on the file system where the project can be read from.

Returns:

  • QgsProject

    The opened project (read).

Source code in src/qgis_server_light/exporter/extract.py
821
822
823
824
825
826
827
828
829
830
831
832
833
834
@staticmethod
def open_qgis_project(path_to_project: str) -> QgsProject:
    """


    Args:
        path_to_project: The absolute path on the file system where the project can be read from.

    Returns:
        The opened project (read).
    """
    project = QgsProject.instance()
    project.read(path_to_project)
    return project

prepare_qgis_project_name(project: QgsProject) -> tuple[str, str] staticmethod

Parameters:

  • project (QgsProject) –

    The instantiated QGIS project.

Returns:

  • tuple[str, str]

    Tuple of str version, name

Source code in src/qgis_server_light/exporter/extract.py
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
@staticmethod
def prepare_qgis_project_name(project: QgsProject) -> tuple[str, str]:
    """


    Args:
        project: The instantiated QGIS project.

    Returns:
        Tuple of str version, name
    """
    # TODO: Find a good approach to recognize different "versions" of a project.
    name = project.baseName()
    parts = name.split(".")
    version = parts.pop(0)
    assembled_name = ".".join(parts)
    if assembled_name == "":
        assembled_name = project.title()
    return version, assembled_name

provide_field_length(field: QgsField) -> int | None staticmethod

Source code in src/qgis_server_light/exporter/extract.py
412
413
414
415
416
417
418
@staticmethod
def provide_field_length(field: QgsField) -> int | None:
    length = field.length()
    if length > 0:
        return length
    else:
        return None

provide_field_precision(field: QgsField) -> int | None staticmethod

Source code in src/qgis_server_light/exporter/extract.py
420
421
422
423
424
425
426
@staticmethod
def provide_field_precision(field: QgsField) -> int | None:
    precision = field.precision()
    if precision > 0:
        return precision
    else:
        return None

run() -> Config

Source code in src/qgis_server_light/exporter/extract.py
96
97
98
def run(self) -> Config:
    self.walk_qgis_project_tree(self.qgis_project_tree_root, [])
    return self.qsl_config

sanitize_name(raw: str, lower: bool = False) -> str staticmethod

Transforms an arbitrary string into a WMS/WFS and URL compatible short name for a job_layer_definition or group.

Steps: 1. Unicode‑NFD → ASCII‑transliteration (removes umlauts/diacritics). 2. All chars, which are NOT [A‑Za‑z0‑9_.‑], will be replaced by '' ersetzen. 3. Reduce multiple underscore '' to a single one. 4. Remove leading '', '.', '-'. 5. If the string is empty OR does not start with a letter OR not start with '', a leading '_' will be added. 6. Optional all will be converted to lowercase (lower=True).

Source code in src/qgis_server_light/exporter/extract.py
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
@staticmethod
def sanitize_name(raw: str, lower: bool = False) -> str:
    """
    Transforms an arbitrary string into a WMS/WFS and URL compatible short name for a job_layer_definition or group.

    Steps:
    1. Unicode‑NFD → ASCII‑transliteration (removes umlauts/diacritics).
    2. All chars, which are NOT [A‑Za‑z0‑9_.‑], will be replaced by '_' ersetzen.
    3. Reduce multiple underscore '_' to a single one.
    4. Remove leading '_', '.', '-'.
    5. If the string is empty OR does not start with a letter OR not start with '_',
       a leading '_' will be added.
    6. Optional all will be converted to lowercase (lower=True).
    """
    # 1. cleaning to ASCII
    ascii_str = (
        unicodedata.normalize("NFKD", raw).encode("ascii", "ignore").decode()
    )
    # 2. not allowed → '_'
    ascii_str = re.sub(r"[^A-Za-z0-9_.-]+", "_", ascii_str)
    # 3. remove multiple '_'
    ascii_str = re.sub(r"_+", "_", ascii_str)
    # 4. remove trailing chars
    ascii_str = ascii_str.strip("._-")
    # 5. ensure first char is correct (mainly xml stuff and URL)
    if not ascii_str or not re.match(r"[A-Za-z_]", ascii_str[0]):
        ascii_str = "_" + ascii_str
    # 6. Optional lowercase
    if lower:
        ascii_str = ascii_str.lower()
    return ascii_str

short_name(short_name: str, path: list[str]) -> str

Decides if to use the short name itself or the unified version by the tree path.

Parameters:

  • short_name (str) –

    The short name either of the group or the job_layer_definition.

  • path (list[str]) –

    The path is a list of string which stores the information of the current tree path. This is used to construct a string for unifying job_layer_definition names by their tree path.

Returns:

  • str

    The short name itself or its unified tree path.

Source code in src/qgis_server_light/exporter/extract.py
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
def short_name(self, short_name: str, path: list[str]) -> str:
    """
    Decides if to use the short name itself or the unified version by the tree path.

    Args:
        short_name: The short name either of the group or the job_layer_definition.
        path: The path is a list of string which stores the information of the current tree path. This is
            used to construct a string for unifying job_layer_definition names by their tree path.

    Returns:
        The short name itself or its unified tree path.
    """
    if self.unify_layer_names_by_group:
        return self.create_unified_short_name(short_name, path)
    else:
        return short_name

walk_qgis_project_tree(entity: QgsLayerTreeNode, path: list[str])

This is a highly recursive function which walks to the qgis job_layer_definition tree to extract all knowledge out of it. It is called from itself again for each level of group like elements which are found.

Parameters:

  • entity (QgsLayerTreeNode) –

    The QGIS projects tree node which can be a QgsLayerTree, QgsLayerTreeGroup or QgsLayerTreeLayer.

  • path (list[str]) –

    The path is a list of string which stores the information of the current tree path. This is used to construct a string for unifying job_layer_definition names by their tree path.

Source code in src/qgis_server_light/exporter/extract.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
def walk_qgis_project_tree(
    self,
    entity: QgsLayerTreeNode,
    path: list[str],
):
    """
    This is a highly recursive function which walks to the qgis job_layer_definition tree to extract all knowledge out
    of it. It is called from itself again for each level of group like elements which are found.

    Args:
        entity: The QGIS projects tree node which can be a QgsLayerTree, QgsLayerTreeGroup or
            QgsLayerTreeLayer.
        path: The path is a list of string which stores the information of the current tree path. This is
            used to construct a string for unifying job_layer_definition names by their tree path.
    """
    if isinstance(entity, QgsLayerTreeLayer):
        # this is a job_layer_definition, we can extract its information
        self.extract_save_layer(
            entity,
            path,
        )
    elif isinstance(entity, QgsLayerTreeGroup) or isinstance(entity, QgsLayerTree):
        # these are "group like" elements, we need to step into them one level deeper.
        short_name = self.get_group_short_name(entity)
        if short_name != "":
            # '' is the root of the tree, we don't want it to be part of the path
            path = path + [short_name]
        self.extract_group(
            entity,
            path,
        )
        for child in entity.children():
            # we handle all the children of the group like thing.
            self.walk_qgis_project_tree(
                child,
                path,
            )
    else:
        logging.error(
            f"This element was not expected while walking QGIS project tree: {entity}"
        )