Skip to content

Commit

Permalink
[vt] Make sure a VT layer gets a proper Swiss extent on an empty proj…
Browse files Browse the repository at this point in the history
…ect (if the project is not empty, we'll leave the current extent untouched, since chances are users have already the extent they want)
  • Loading branch information
gacarrillor committed Jul 18, 2024
1 parent f405074 commit 78596b0
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions swiss_locator/core/filters/swiss_locator_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import sys
import traceback

from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtCore import Qt, QTimer, QCoreApplication
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import QLabel, QWidget, QTabWidget
from PyQt5.QtCore import QUrl, pyqtSignal, QEventLoop
Expand All @@ -42,6 +42,8 @@
QgsWkbTypes,
QgsLocatorContext,
QgsFeedback,
QgsPoint,
QgsPointXY,
QgsRasterLayer,
QgsVectorTileLayer,
)
Expand Down Expand Up @@ -443,49 +445,43 @@ def triggerResult(self, result: QgsLocatorResult):

if not ch_layer.isValid():
msg = self.tr(
"Cannot load Vector Tiles layer: {}".format(
swiss_result.title
)
"Cannot load Vector Tiles layer: {}".format(swiss_result.title)
)
level = Qgis.Warning
self.info(msg, level)
else:
ch_layer.setLabelsEnabled(True)
ch_layer.loadDefaultMetadata()

error, warnings = '', []
error, warnings = "", []
res, sublayers = ch_layer.loadDefaultStyleAndSubLayers(error, warnings)

if sublayers:
msg = self.tr(
"Sublayers found ({}): {}".format(
swiss_result.title,
"; ".join([sublayer.name() for sublayer in sublayers])
"; ".join([sublayer.name() for sublayer in sublayers]),
)
)
level = Qgis.Info
self.info(msg, level)
if error or warnings:
msg = self.tr(
"Error/warning found while loading default styles and sublayers for layer {}. Error: {} Warning: {}".format(
swiss_result.title,
error,
"; ".join(warnings)
swiss_result.title, error, "; ".join(warnings)
)
)
level = Qgis.Warning
self.info(msg, level)

msg = self.tr(
"Layer added to the map: {}".format(
swiss_result.title
)
)
msg = self.tr("Layer added to the map: {}".format(swiss_result.title))
level = Qgis.Info
self.info(msg, level)

# Load basemap layers at the bottom of the layer tree
root = QgsProject.instance().layerTreeRoot()
empty_project = not QgsProject.instance().mapLayers()

if sublayers:
# Sublayers should be loaded on top of the vector tile
# layer. We group them to keep them all together.
Expand All @@ -498,6 +494,27 @@ def triggerResult(self, result: QgsLocatorResult):
QgsProject.instance().addMapLayer(ch_layer, False)
root.insertLayer(-1, ch_layer)

if empty_project:
QCoreApplication.processEvents() # wait for application to process pending tasks

if (
self.map_canvas.mapSettings().destinationCrs().authid()
== "EPSG:3857"
):
# Set the Swiss extent in EPSG:3857
extent = QgsRectangle(624991, 5725825, 1209826, 6089033)
else: # Transform a WGS84 extent to map CRS
bottom_left = QgsPoint(5.8, 45.2)
top_right = QgsPoint(10.569, 48.322)
bottom_left.transform(self.transform_4326)
top_right.transform(self.transform_4326)
extent = QgsRectangle(
QgsPointXY(bottom_left), QgsPointXY(top_right)
)

self.map_canvas.setExtent(extent)
self.map_canvas.refresh()

# Location
else:
point = QgsGeometry.fromPointXY(swiss_result.point)
Expand Down

0 comments on commit 78596b0

Please sign in to comment.