Skip to content

Commit

Permalink
Release 0.7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
JuhoErvasti committed Apr 3, 2023
1 parent 58d32c2 commit b8378a1
Show file tree
Hide file tree
Showing 11 changed files with 1,224 additions and 130 deletions.
2 changes: 1 addition & 1 deletion QGISDigitransitGeocoding/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@


def classFactory(self): # noqa N802
from .plugin import Plugin
from .digitransit_geocoding_plugin import Plugin

return Plugin()
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
# Modified
# by Pauliina Mäkinen
# on 10.02.2022
# Modified
# by Juho Ervasti
# on 02.03.2023

import json
import urllib
import urllib.parse
from urllib.error import HTTPError
from pathlib import Path

from qgis.core import (
Expand All @@ -31,8 +35,11 @@
QgsProcessingParameterNumber,
QgsProcessingParameterString,
QgsWkbTypes,
QgsExpressionContextUtils,
)
from qgis.PyQt.QtCore import QCoreApplication, QVariant
from qgis.utils import iface



class DigitransitGeocoderPluginAlgorithmError(Exception):
Expand Down Expand Up @@ -62,7 +69,6 @@ class DigitransitGeocoderPluginAlgorithm(QgsProcessingAlgorithm):
LOCATION_TYPE_ADDRESS = "LOCATION_TYPE_ADDRESS"

def initAlgorithm(self, config=None): # noqa N802

self.separators = [",", ";", ":", self.tr("Other")]

self.translator = None
Expand All @@ -73,7 +79,7 @@ def initAlgorithm(self, config=None): # noqa N802
QgsProcessingParameterFile(
self.INPUT, self.tr("Input CSV file"), extension="csv"
)
)
)

self.addParameter(
QgsProcessingParameterEnum(
Expand Down Expand Up @@ -266,7 +272,36 @@ def processAlgorithm(self, parameters, context, feedback): # noqa: N802
)
return {self.OUTPUT: None}

self.geocode_csv_rows(self.csv_rows)
try:
self.geocode_csv_rows(self.csv_rows)
except TypeError:
self.feedback.pushWarning(
self.tr(
"Error: No DIGITRANSIT_API_KEY variable found. You can get an API key from https://portal-api.digitransit.fi. You need to create a global variable DIGITRANSIT_API_KEY in QGIS settings and set your API key as the value."
)
)
QgsMessageLog.logMessage(
self.tr(
"Error: No DIGITRANSIT_API_KEY variable found. You can get an API key from https://portal-api.digitransit.fi. You need to create a global variable DIGITRANSIT_API_KEY in QGIS settings and set your API key as the value."
),
"QGISDigitransitGeocoding",
Qgis.Warning,
)
raise DigitransitGeocoderPluginAlgorithmError
except HTTPError:
self.feedback.pushWarning(
self.tr(
"Access denied. Check the validity of your API key."
)
)
QgsMessageLog.logMessage(
self.tr(
"Access denied. Check the validity of your API key."
),
"QGISDigitransitGeocoding",
Qgis.Warning,
)
raise DigitransitGeocoderPluginAlgorithmError

return {self.OUTPUT: self.dest_id}

Expand Down Expand Up @@ -733,6 +768,8 @@ def add_layer_data_field_with_type(
fields.append(QgsField(header_column, QVariant.String))

def geocode_csv_rows(self, rows):
api_key = QgsExpressionContextUtils.globalScope().variable('DIGITRANSIT_API_KEY')

self.total_geocode_count = len(rows)
self.geocode_count = 0

Expand All @@ -746,6 +783,10 @@ def geocode_csv_rows(self, rows):

base_url = "http://api.digitransit.fi/geocoding/v1/search?"



hdr = {'digitransit-subscription-key': api_key}

search_parameters = {"text": address, "size": self.max_n_of_search_results}

if self.use_source_OA or self.use_source_OSM or self.use_source_NLS:
Expand Down Expand Up @@ -814,7 +855,10 @@ def geocode_csv_rows(self, rows):

QgsMessageLog.logMessage(search_url, "QGISDigitransitGeocoding", Qgis.Info)

r = urllib.request.urlopen(search_url)
req = urllib.request.Request(search_url, headers=hdr)
req.get_method = lambda: 'GET'
r = urllib.request.urlopen(req)

geocoding_result = json.loads(
r.read().decode(r.info().get_param("charset") or "utf-8")
)
Expand Down Expand Up @@ -977,4 +1021,4 @@ def tr(self, string):
return QCoreApplication.translate("DigitransitGeocoderPluginAlgorithm", string)

def createInstance(self): # noqa N802
return DigitransitGeocoderPluginAlgorithm()
return DigitransitGeocoderPluginAlgorithm()
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def name(self):
This string should be short (e.g. "Lastools") and localised.
"""
return self.tr("QGISDigitransitGeocoding")
return self.tr("Digitransit.fi Geocoder")

def long_name(self):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
***************************************************************************/
"""
import os.path

from typing import Callable, List, Optional

from qgis.core import QgsApplication
from qgis.PyQt.QtCore import QCoreApplication, Qt, QTranslator
from qgis.PyQt.QtCore import QCoreApplication, Qt, QTranslator, QSettings, qVersion
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction, QWidget
from qgis.utils import iface
Expand Down Expand Up @@ -45,22 +47,22 @@ def __init__(self) -> None:
"""
setup_logger(Plugin.name)
self.dockwidget = DigitransitGeocoderDockWidget(iface)

self.plugin_dir = os.path.dirname(__file__)
# initialize locale
locale, file_path = setup_translation()
locale, file_path = setup_translation('QGISDigitransitGeocoding_{}.qm',resources_path('/i18n'))
if file_path:
self.translator = QTranslator()
self.translator.load(file_path)
# noinspection PyCallByClass
QCoreApplication.installTranslator(self.translator)
else:
pass

# Declare instance attributes
self.actions: List[QAction] = []
self.menu = Plugin.name
self.toolbar = iface.addToolBar("QGISDigitransitGeocoding")
self.toolbar.setObjectName("QGISDigitransitGeocoding")
self.toolbar.setObjectName(tr("Digitransit.fi geocoding"))

self.pluginIsActive = False

Expand Down Expand Up @@ -136,7 +138,7 @@ def initGui(self) -> None: # noqa N802
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
self.add_action(
resources_path("icons/icon.png"),
text=tr("Geocode a finnish address"),
text=tr("Geocode a Finnish address"),
callback=self.run,
parent=iface.mainWindow(),
)
Expand Down
Binary file not shown.
Loading

0 comments on commit b8378a1

Please sign in to comment.