Skip to content

Commit

Permalink
Configure several vector tile layers and styles; load styles; load su…
Browse files Browse the repository at this point in the history
…blayers as grouped layers
  • Loading branch information
gacarrillor committed May 9, 2024
1 parent e7e489b commit 320b898
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
45 changes: 43 additions & 2 deletions swiss_locator/core/filters/swiss_locator_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def triggerResult(self, result: QgsLocatorResult):
params["type"] = "xyz"
params["zmax"] = "14"
params["zmin"] = "0"

url_with_params = "&".join([f"{k}={v}" for (k, v) in params.items()])

self.info(f"Loading layer: {url_with_params}")
Expand All @@ -454,14 +454,55 @@ def triggerResult(self, result: QgsLocatorResult):
level = Qgis.Warning
self.info(msg, level)
else:
vt_layer.setLabelsEnabled(True)
vt_layer.loadDefaultMetadata()

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

if sublayers:
msg = self.tr(
"Sublayers found ({}): {}".format(
swiss_result.title,
"; ".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)
)
)
level = Qgis.Warning
self.info(msg, level)

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

root = QgsProject.instance().layerTreeRoot()
if sublayers:
# Sublayers should load on top of the vector tiles layer
# We group them to keep them all together
group = root.insertGroup(-1, vt_layer.name())
for sublayer in sublayers:
QgsProject.instance().addMapLayer(sublayer, False)
group.addLayer(sublayer)

QgsProject.instance().addMapLayer(vt_layer, False)
group.addLayer(vt_layer)
else:
QgsProject.instance().addMapLayer(vt_layer, False)
root.insertLayer(-1, vt_layer)

QgsProject.instance().addMapLayer(vt_layer)

# Location
else:
Expand Down
16 changes: 13 additions & 3 deletions swiss_locator/core/filters/swiss_locator_filter_vector_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,23 @@ def perform_fetch_results(self, search: str, feedback: QgsFeedback):
"light base map": {
"title": "Light base map", "description": "",
"url": "https://vectortiles.geo.admin.ch/tiles/ch.swisstopo.base.vt/v1.0.0/{z}/{x}/{y}.pbf",
"style": "https://vectortiles.geo.admin.ch/styles/ch.swisstopo.basemap.vt/style.json"
"style": "https://vectortiles.geo.admin.ch/styles/ch.swisstopo.lightbasemap.vt/style.json"
},
"imagery base map": {
"title": "Imagery base map", "description": "",
"url": "https://vectortiles.geo.admin.ch/tiles/ch.swisstopo.base.vt/v1.0.0/{z}/{x}/{y}.pbf",
"style": "https://vectortiles.geo.admin.ch/styles/ch.swisstopo.basemap.vt/style.json"
}
"style": "https://vectortiles.geo.admin.ch/styles/ch.swisstopo.imagerybasemap.vt/style.json"
},
"leichte-basiskarte": {
"title": "leichte-basiskarte", "description": "",
"url": "https://vectortiles.geo.admin.ch/tiles/ch.swisstopo.leichte-basiskarte.vt/v3.0.1/{z}/{x}/{y}.pbf",
"style": "https://vectortiles.geo.admin.ch/styles/ch.swisstopo.leichte-basiskarte.vt/style.json"
},
"leichte-basiskarte-imagery": {
"title": "leichte-basiskarte-imagery", "description": "",
"url": "https://vectortiles.geo.admin.ch/tiles/ch.swisstopo.leichte-basiskarte.vt/v3.0.1/{z}/{x}/{y}.pbf",
"style": "https://vectortiles.geo.admin.ch/styles/ch.swisstopo.leichte-basiskarte-imagery.vt/style.json"
},
}

for keyword in list(data.keys()):
Expand Down

0 comments on commit 320b898

Please sign in to comment.