Skip to content

Commit

Permalink
move settings to QGIS API
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Oct 4, 2024
1 parent db416d6 commit 3cc814c
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 123 deletions.
30 changes: 17 additions & 13 deletions swiss_locator/core/filters/swiss_locator_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ def __init__(
self.minimum_search_length = 2

self.nam = QNetworkAccessManager()
self.nam.setRedirectPolicy(QNetworkRequest.RedirectPolicy.NoLessSafeRedirectPolicy)
self.nam.setRedirectPolicy(
QNetworkRequest.RedirectPolicy.NoLessSafeRedirectPolicy
)
self.network_replies = dict()

if crs:
Expand All @@ -133,7 +135,9 @@ def __init__(
self.map_canvas = iface.mapCanvas()
self.map_canvas.destinationCrsChanged.connect(self.create_transforms)

self.rubber_band = QgsRubberBand(self.map_canvas, QgsWkbTypes.GeometryType.PointGeometry)
self.rubber_band = QgsRubberBand(
self.map_canvas, QgsWkbTypes.GeometryType.PointGeometry
)
self.rubber_band.setColor(QColor(255, 255, 50, 200))
self.rubber_band.setIcon(self.rubber_band.ICON_CIRCLE)
self.rubber_band.setIconSize(15)
Expand All @@ -155,7 +159,7 @@ def name(self):
return self.__class__.__name__

def priority(self):
return self.settings.value(f"{self.type.value}_priority")
return QgsLocatorFilter.Priority.Medium

def displayName(self):
# this should be re-implemented
Expand Down Expand Up @@ -185,20 +189,20 @@ def hasConfigWidget(self):

def openConfigWidget(self, parent=None):
dlg = ConfigDialog(parent)
wid = dlg.findChild(QTabWidget, "tabWidget", Qt.FindChildOption.FindDirectChildrenOnly)
wid = dlg.findChild(
QTabWidget, "tabWidget", Qt.FindChildOption.FindDirectChildrenOnly
)
tab = wid.findChild(QWidget, self.type.value)
wid.setCurrentWidget(tab)
dlg.exec()

def create_transforms(self):
# this should happen in the main thread
self.crs = self.settings.value("crs")
if self.crs == "project":
map_crs = self.map_canvas.mapSettings().destinationCrs()
if map_crs.isValid() and ":" in map_crs.authid():
self.crs = map_crs.authid().split(":")[1]
if self.crs not in AVAILABLE_CRS:
self.crs = "2056"
map_crs = self.map_canvas.mapSettings().destinationCrs()
if map_crs.isValid() and ":" in map_crs.authid():
self.crs = map_crs.authid().split(":")[1]
if self.crs not in AVAILABLE_CRS:
self.crs = "2056"
assert self.crs in AVAILABLE_CRS
src_crs_ch = QgsCoordinateReferenceSystem("EPSG:{}".format(self.crs))
assert src_crs_ch.isValid()
Expand Down Expand Up @@ -421,7 +425,7 @@ def triggerResult(self, result: QgsLocatorResult):
point = QgsGeometry.fromPointXY(swiss_result.point)
point.transform(self.transform_4326)
self.highlight(point)
if self.settings.value("show_map_tip") and with_qt_web_kit():
if self.settings.show_map_tip.value() and with_qt_web_kit():
self.show_map_tip(swiss_result.layer, swiss_result.feature_id, point)

# Vector tiles
Expand Down Expand Up @@ -519,7 +523,7 @@ def triggerResult(self, result: QgsLocatorResult):
if layer and feature_id:
self.fetch_feature(layer, feature_id)

if self.settings.value("show_map_tip") and with_qt_web_kit():
if self.settings.show_map_tip.value() and with_qt_web_kit():
self.show_map_tip(layer, feature_id, point)
else:
self.current_timer = QTimer()
Expand Down
5 changes: 4 additions & 1 deletion swiss_locator/core/filters/swiss_locator_filter_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,15 @@ def displayName(self):
def prefix(self):
return "chf"

def priority(self):
return self.settings.filter_feature_priority.value()

def perform_fetch_results(self, search: str, feedback: QgsFeedback):
# Feature search is split in several requests
# otherwise URL is too long
requests = []
try:
limit = self.settings.value(f"{FilterType.Feature.value}_limit")
limit = self.settings.filter_feature_limit.value()
layers = list(self.searchable_layers.keys())
assert len(layers) > 0
step = 20
Expand Down
5 changes: 4 additions & 1 deletion swiss_locator/core/filters/swiss_locator_filter_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ def displayName(self):
def prefix(self):
return "chl"

def priority(self):
return self.settings.filter_layers_priority.value()

def perform_fetch_results(self, search: str, feedback: QgsFeedback):
limit = self.settings.value(f"{FilterType.Location.value}_limit")
limit = self.settings.filter_layers_limit.value()
urls = [
map_geo_admin_url(search, self.type.value, self.crs, self.lang, limit),
opendata_swiss_url(search),
Expand Down
5 changes: 4 additions & 1 deletion swiss_locator/core/filters/swiss_locator_filter_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ def displayName(self):
def prefix(self):
return "chs"

def priority(self):
return self.settings.filter_location_priority.value()

def perform_fetch_results(self, search: str, feedback: QgsFeedback):
limit = self.settings.value(f"{FilterType.Location.value}_limit")
limit = self.settings.filter_location_limit.value()
url, params = map_geo_admin_url(
search, self.type.value, self.crs, self.lang, limit
)
Expand Down
33 changes: 20 additions & 13 deletions swiss_locator/core/filters/swiss_locator_filter_vector_tiles.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from qgis.gui import QgisInterface
from qgis.core import (
QgsApplication,
QgsBlockingNetworkRequest,
QgsFetchedContent,
QgsLocatorResult,
QgsFeedback,
)
Expand All @@ -29,6 +27,9 @@ def displayName(self):
def prefix(self):
return "chb"

def priority(self):
return self.settings.filter_vt_priority.value()

def hasConfigWidget(self):
return False

Expand All @@ -38,27 +39,31 @@ def perform_fetch_results(self, search: str, feedback: QgsFeedback):
"title": "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.basemap.vt/style.json",
},
"light base map": {
"title": "Light base map", "description": "",
"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.lightbasemap.vt/style.json"
"style": "https://vectortiles.geo.admin.ch/styles/ch.swisstopo.lightbasemap.vt/style.json",
},
"imagery base map": {
"title": "Imagery base map", "description": "",
"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.imagerybasemap.vt/style.json"
"style": "https://vectortiles.geo.admin.ch/styles/ch.swisstopo.imagerybasemap.vt/style.json",
},
"leichte-basiskarte": {
"title": "leichte-basiskarte", "description": "",
"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"
"style": "https://vectortiles.geo.admin.ch/styles/ch.swisstopo.leichte-basiskarte.vt/style.json",
},
"leichte-basiskarte-imagery": {
"title": "leichte-basiskarte-imagery", "description": "",
"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"
"style": "https://vectortiles.geo.admin.ch/styles/ch.swisstopo.leichte-basiskarte-imagery.vt/style.json",
},
}

Expand All @@ -68,7 +73,9 @@ def perform_fetch_results(self, search: str, feedback: QgsFeedback):
if not search or search.lower() in keyword:
result = QgsLocatorResult()
result.filter = self
result.icon = QgsApplication.getThemeIcon("/mActionAddVectorTileLayer.svg")
result.icon = QgsApplication.getThemeIcon(
"/mActionAddVectorTileLayer.svg"
)

result.displayString = data[keyword]["title"]
result.description = data[keyword]["description"]
Expand All @@ -82,7 +89,7 @@ def perform_fetch_results(self, search: str, feedback: QgsFeedback):
results[result] = score

# sort the results with score
#results = sorted([result for (result, score) in results.items()])
# results = sorted([result for (result, score) in results.items()])

for result in results:
self.resultFetched.emit(result)
Expand Down
20 changes: 15 additions & 5 deletions swiss_locator/core/filters/swiss_locator_filter_wmts.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ def __init__(self, iface: QgisInterface = None, crs: str = None, capabilities=No

self.info(self.content.status())

if self.content.status() == QgsFetchedContent.ContentStatus.Finished and self.content.filePath():
if (
self.content.status() == QgsFetchedContent.ContentStatus.Finished
and self.content.filePath()
):
file_path = self.content.filePath()
self.info(
f"Swisstopo capabilities already downloaded. Reading from {file_path}"
Expand All @@ -73,7 +76,8 @@ def clone(self):
nam.get(request, forceRefresh=True)
reply = nam.reply()
if (
reply.attribute(QNetworkRequest.Attribute.HttpStatusCodeAttribute) == 200
reply.attribute(QNetworkRequest.Attribute.HttpStatusCodeAttribute)
== 200
): # other codes are handled by NetworkAccessManager
self.capabilities = ET.fromstring(reply.content().data().decode("utf8"))
else:
Expand All @@ -91,16 +95,22 @@ def displayName(self):
def prefix(self):
return "chw"

def priority(self):
return self.settings.filter_wmts_priority.value()

def handle_capabilities_response(self):
if self.content.status() == QgsFetchedContent.ContentStatus.Finished and self.content.filePath():
if (
self.content.status() == QgsFetchedContent.ContentStatus.Finished
and self.content.filePath()
):
self.info(
f"Swisstopo capabilities has been downloaded. Reading from {self.content.filePath()}"
)
self.capabilities = ET.parse(self.content.filePath()).getroot()
else:
self.info(
"The Swiss Locator filter for WMTS layers could not fetch capabilities",
Qgis.MessageLevel.Critical
Qgis.MessageLevel.Critical,
)

def perform_fetch_results(self, search: str, feedback: QgsFeedback):
Expand Down Expand Up @@ -170,6 +180,6 @@ def perform_fetch_results(self, search: str, feedback: QgsFeedback):
# sort the results with score
results = sorted([result for (result, score) in results.items()])

for result in results[0 : self.settings.value("wmts_limit")]:
for result in results[0 : self.settings.filter_wmts_limit.value()]:
self.resultFetched.emit(result)
self.result_found = True
11 changes: 4 additions & 7 deletions swiss_locator/core/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from qgis.PyQt.QtCore import QLocale, QSettings
from .settings import Settings
from qgis.core import NULL
from .parameters import AVAILABLE_LANGUAGES


def get_language() -> str:
Expand All @@ -36,17 +37,13 @@ def get_language() -> str:
:return: 2 chars long string representing the language to be used
"""
# get lang from settings
lang = Settings().value("lang")
lang = Settings().lang.value()
if not lang:
# if None, try to use the locale one
from .parameters import AVAILABLE_LANGUAGES

locale = str(QSettings().value("locale/userLocale")).replace(str(NULL), "en_CH")
locale_lang = QLocale.languageToString(QLocale(locale).language())
if locale_lang in AVAILABLE_LANGUAGES:
lang = AVAILABLE_LANGUAGES[locale_lang]
else:
# defaults to English
lang = "en"
if lang not in AVAILABLE_LANGUAGES:
lang = "en"

return lang
Loading

0 comments on commit 3cc814c

Please sign in to comment.