(self, merge_features, apply_styles, clip_tiles)
Creates a hierarchy of groups and layers in qgis
Creates a hierarchy of groups and layers in qgis
Creates a hierarchy of groups and layers in qgis
def _create_qgis_layers(self, merge_features, apply_styles, clip_tiles):
        """
        Creates a hierarchy of groups and layers in qgis
        """
        own_layers: List[QgsVectorLayer] = get_loaded_layers_of_connection(self._connection["name"])
        for l in own_layers:
            name: str = l.name()
            geo_type = l.customProperty("VectorTilesReader/geo_type")
            if (name, geo_type) not in self.feature_collections_by_layer_name_and_geotype:
                if not bool(l.customProperty("VectorTilesReader/is_empty")):
                    info("Clearing layer: {}", name)
                    l.setCustomProperty("VectorTilesReader/is_empty", True)
                    self._update_layer_source(l.source(), self._get_empty_feature_collection(0, l.name()))
            else:
                l.setCustomProperty("VectorTilesReader/is_empty", False)

        nr_layers = len(self.feature_collections_by_layer_name_and_geotype)
        self._update_progress(progress=0, max_progress=nr_layers, msg="Creating {} layers...".format(nr_layers))
        layer_filter = self._loading_options["layer_filter"]

        clipping_bounds = None
        if merge_features:
            clipping_bounds = self._loading_options["bounds"]

        new_layers = []
        count = 0
        for layer_name, geo_type in self.feature_collections_by_layer_name_and_geotype:
            count += 1
            if self.cancel_requested:
                break
            if layer_filter and layer_name not in layer_filter:
                continue

            feature_collection = self.feature_collections_by_layer_name_and_geotype[(layer_name, geo_type)]
            zoom_level = feature_collection["zoom_level"]

            file_name = self._get_geojson_filename(layer_name, geo_type)
            file_path = get_geojson_file_name(file_name)

            layer = None
            if os.path.isfile(file_path):
                # file exists already. add the features of the collection to the existing collection
                # get the layer from qgis and update its source
                for l in own_layers:
                    if os.path.abspath(file_path).lower() == os.path.abspath(l.source()).lower():
                        layer = l
                        break
                if layer:
                    self._update_layer_source(file_path, feature_collection)
                    layer.reload()
                    if merge_features and geo_type in [GeoTypes.LINE_STRING, GeoTypes.POLYGON]:
                        merger = FeatureMerger(should_cancel_func=lambda: self.cancel_requested)
                        merger.merge_features(layer)
                    if clip_tiles:
                        clip_features(
                            layer=layer,
                            scheme=self._source.scheme(),
                            bounds=clipping_bounds,
                            should_cancel_func=lambda: self.cancel_requested,
                        )
            if (
                not layer
                and (self._allowed_sources is None or file_path in self._allowed_sources)
                and (not layer_filter or layer_name in layer_filter)
            ):
                self._update_layer_source(file_path, feature_collection)
                layer = self._create_named_layer(
                    json_src=file_path, layer_name=layer_name, geo_type=geo_type, zoom_level=zoom_level
                )
                if merge_features and geo_type in [GeoTypes.LINE_STRING, GeoTypes.POLYGON]:
                    merger = FeatureMerger(should_cancel_func=lambda: self.cancel_requested)
                    merger.merge_features(layer)
                if clip_tiles:
                    clip_features(
                        layer=layer,
                        scheme=self._source.scheme(),
                        bounds=clipping_bounds,
                        should_cancel_func=lambda: self.cancel_requested,
                    )
                new_layers.append((layer_name, geo_type, layer))
            self._update_progress(progress=count + 1)

        self._update_progress(msg="Refreshing layers...")

        if len(new_layers) > 0 and not self.cancel_requested:
            self._update_progress(msg="Adding new layers...")
            only_layers = list([layer_name_tuple[2] for layer_name_tuple in new_layers])
            info("Adding {} layers to QGIS...", len(only_layers))
            QgsProject.instance().addMapLayers(only_layers, False)
        info("Adding {} new layers to layer group...", len(new_layers))
        for name, geo_type, layer in new_layers:
            if self.cancel_requested:
                break
            self.add_layer_to_group.emit(layer)

        if apply_styles and not self.cancel_requested and new_layers:
            count = 0
            conn_name = self.connection()["name"]
            styles_folder = get_style_folder(conn_name)
            styles = get_styles(conn_name)
            info("Applying styles to {} layers...", len(new_layers))
            self._update_progress(progress=0, max_progress=len(new_layers), msg="Styling layers...")
            for name, geo_type, layer in new_layers:
                count += 1
                if self.cancel_requested:
                    break
                VtReader._apply_named_style(
                    existing_styles=styles, style_dir=styles_folder, layer=layer, geo_type=geo_type
                )
                self._update_progress(progress=count)
        info("Layer creation complete")
def _create_qgis_layers ( self , merge_features , apply_styles , clip_tiles ) : own_layers : List [ QgsVectorLayer ] = get_loaded_layers_of_connection ( self . _connection [ "name" ] ) for l in own_layers : name : str = l . name ( ) geo_type = l . customProperty ( "VectorTilesReader/geo_type" ) if ( name , geo_type ) not in self . feature_collections_by_layer_name_and_geotype : if not bool ( l . customProperty ( "VectorTilesReader/is_empty" ) ) : info ( "Clearing layer: {}" , name ) l . setCustomProperty ( "VectorTilesReader/is_empty" , True ) self . _update_layer_source ( l . source ( ) , self . _get_empty_feature_collection ( 0 , l . name ( ) ) ) else : l . setCustomProperty ( "VectorTilesReader/is_empty" , False ) nr_layers = len ( self . feature_collections_by_layer_name_and_geotype ) self . _update_progress ( progress = 0 , max_progress = nr_layers , msg = "Creating {} layers..." . format ( nr_layers ) ) layer_filter = self . _loading_options [ "layer_filter" ] clipping_bounds = None if merge_features : clipping_bounds = self . _loading_options [ "bounds" ] new_layers = [ ] count = 0 for layer_name , geo_type in self . feature_collections_by_layer_name_and_geotype : count += 1 if self . cancel_requested : break if layer_filter and layer_name not in layer_filter : continue feature_collection = self . feature_collections_by_layer_name_and_geotype [ ( layer_name , geo_type ) ] zoom_level = feature_collection [ "zoom_level" ] file_name = self . _get_geojson_filename ( layer_name , geo_type ) file_path = get_geojson_file_name ( file_name ) layer = None if os . path . isfile ( file_path ) : # file exists already. add the features of the collection to the existing collection # get the layer from qgis and update its source for l in own_layers : if os . path . abspath ( file_path ) . lower ( ) == os . path . abspath ( l . source ( ) ) . lower ( ) : layer = l break if layer : self . _update_layer_source ( file_path , feature_collection ) layer . reload ( ) if merge_features and geo_type in [ GeoTypes . LINE_STRING , GeoTypes . POLYGON ] : merger = FeatureMerger ( should_cancel_func = lambda : self . cancel_requested ) merger . merge_features ( layer ) if clip_tiles : clip_features ( layer = layer , scheme = self . _source . scheme ( ) , bounds = clipping_bounds , should_cancel_func = lambda : self . cancel_requested , ) if ( not layer and ( self . _allowed_sources is None or file_path in self . _allowed_sources ) and ( not layer_filter or layer_name in layer_filter ) ) : self . _update_layer_source ( file_path , feature_collection ) layer = self . _create_named_layer ( json_src = file_path , layer_name = layer_name , geo_type = geo_type , zoom_level = zoom_level ) if merge_features and geo_type in [ GeoTypes . LINE_STRING , GeoTypes . POLYGON ] : merger = FeatureMerger ( should_cancel_func = lambda : self . cancel_requested ) merger . merge_features ( layer ) if clip_tiles : clip_features ( layer = layer , scheme = self . _source . scheme ( ) , bounds = clipping_bounds , should_cancel_func = lambda : self . cancel_requested , ) new_layers . append ( ( layer_name , geo_type , layer ) ) self . _update_progress ( progress = count + 1 ) self . _update_progress ( msg = "Refreshing layers..." ) if len ( new_layers ) > 0 and not self . cancel_requested : self . _update_progress ( msg = "Adding new layers..." ) only_layers = list ( [ layer_name_tuple [ 2 ] for layer_name_tuple in new_layers ] ) info ( "Adding {} layers to QGIS..." , len ( only_layers ) ) QgsProject . instance ( ) . addMapLayers ( only_layers , False ) info ( "Adding {} new layers to layer group..." , len ( new_layers ) ) for name , geo_type , layer in new_layers : if self . cancel_requested : break self . add_layer_to_group . emit ( layer ) if apply_styles and not self . cancel_requested and new_layers : count = 0 conn_name = self . connection ( ) [ "name" ] styles_folder = get_style_folder ( conn_name ) styles = get_styles ( conn_name ) info ( "Applying styles to {} layers..." , len ( new_layers ) ) self . _update_progress ( progress = 0 , max_progress = len ( new_layers ) , msg = "Styling layers..." ) for name , geo_type , layer in new_layers : count += 1 if self . cancel_requested : break VtReader . _apply_named_style ( existing_styles = styles , style_dir = styles_folder , layer = layer , geo_type = geo_type ) self . _update_progress ( progress = count ) info ( "Layer creation complete" )