diff --git a/swiss_locator/core/filters/swiss_locator_filter.py b/swiss_locator/core/filters/swiss_locator_filter.py index 2f0b0d1..b763123 100644 --- a/swiss_locator/core/filters/swiss_locator_filter.py +++ b/swiss_locator/core/filters/swiss_locator_filter.py @@ -27,7 +27,7 @@ from PyQt5.QtCore import Qt, QTimer from PyQt5.QtGui import QColor from PyQt5.QtWidgets import QLabel, QWidget, QTabWidget -from PyQt5.QtCore import QUrl, QUrlQuery, pyqtSignal, QEventLoop +from PyQt5.QtCore import QUrl, pyqtSignal, QEventLoop from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply, QNetworkAccessManager from qgis.core import ( @@ -62,6 +62,7 @@ from swiss_locator.gui.config_dialog import ConfigDialog from swiss_locator.gui.maptip import MapTip from swiss_locator.gui.qtwebkit_conf import with_qt_web_kit +from swiss_locator.utils.utils import url_with_param def result_from_data(result: QgsLocatorResult): @@ -235,18 +236,9 @@ def box2geometry(box: str) -> QgsRectangle: float(coords[0]), float(coords[1]), float(coords[2]), float(coords[3]) ) - @staticmethod - def url_with_param(url, params) -> str: - url = QUrl(url) - q = QUrlQuery(url) - for key, value in params.items(): - q.addQueryItem(key, value) - url.setQuery(q) - return url - @staticmethod def request_for_url(url, params, headers) -> QNetworkRequest: - url = SwissLocatorFilter.url_with_param(url, params) + url = url_with_param(url, params) request = QNetworkRequest(url) for k, v in list(headers.items()): request.setRawHeader(k, v) @@ -540,7 +532,7 @@ def show_map_tip(self, layer, feature_id, point): layer=layer, feature_id=feature_id ) params = {"lang": self.lang, "sr": self.crs} - url = self.url_with_param(url, params) + url = url_with_param(url, params) self.dbg_info(url) request = QNetworkRequest(QUrl(url)) self.fetch_request( diff --git a/swiss_locator/core/filters/swiss_locator_filter_location.py b/swiss_locator/core/filters/swiss_locator_filter_location.py index 2ca4ac0..bc57863 100644 --- a/swiss_locator/core/filters/swiss_locator_filter_location.py +++ b/swiss_locator/core/filters/swiss_locator_filter_location.py @@ -41,6 +41,7 @@ from swiss_locator.core.filters.map_geo_admin import map_geo_admin_url from swiss_locator.core.results import LocationResult from swiss_locator.utils.html_stripper import strip_tags +from swiss_locator.utils.utils import url_with_param class SwissLocatorFilterLocation(SwissLocatorFilter): @@ -114,7 +115,7 @@ def fetch_feature(self, layer, feature_id): # Try to get more info url = f"https://api3.geo.admin.ch/rest/services/api/MapServer/{layer}/{feature_id}" params = {"lang": self.lang, "sr": self.crs} - url = self.url_with_param(url, params) + url = url_with_param(url, params) request = QNetworkRequest(QUrl(url)) self.fetch_request(request, QgsFeedback(), self.parse_feature_response) diff --git a/swiss_locator/core/profiles/profile_generator.py b/swiss_locator/core/profiles/profile_generator.py index ff34825..fde4265 100644 --- a/swiss_locator/core/profiles/profile_generator.py +++ b/swiss_locator/core/profiles/profile_generator.py @@ -1,6 +1,6 @@ import json -from qgis.PyQt.QtCore import QUrl, QUrlQuery +from qgis.PyQt.QtCore import QUrl from qgis.PyQt.QtNetwork import QNetworkRequest from qgis.core import ( Qgis, @@ -18,6 +18,7 @@ from swiss_locator.core.profiles.profile_results import SwissProfileResults from swiss_locator.core.profiles.profile_url import profile_url +from swiss_locator.utils.utils import url_with_param class SwissProfileGenerator(QgsAbstractProfileGenerator): @@ -42,14 +43,6 @@ def sourceId(self): return "swiss-profile" def __get_profile_from_rest_api(self): - def url_with_param(url, params) -> str: - url = QUrl(url) - q = QUrlQuery(url) - for key, value in params.items(): - q.addQueryItem(key, value) - url.setQuery(q) - return url - result = {} geojson = self.__transformed_curve.asJson(3) base_url, base_params = profile_url(geojson) diff --git a/swiss_locator/utils/utils.py b/swiss_locator/utils/utils.py new file mode 100644 index 0000000..9d452c1 --- /dev/null +++ b/swiss_locator/utils/utils.py @@ -0,0 +1,10 @@ +from qgis.PyQt.QtCore import QUrl, QUrlQuery + + +def url_with_param(url: str, params: dict) -> str: + url = QUrl(url) + q = QUrlQuery(url) + for key, value in params.items(): + q.addQueryItem(key, value) + url.setQuery(q) + return url