Skip to content

Commit

Permalink
Merge pull request #44 from MIERUNE/feat/i18n
Browse files Browse the repository at this point in the history
Multilanguaging
  • Loading branch information
nbayashi authored Jun 25, 2024
2 parents f344e99 + 80b2486 commit c18183f
Show file tree
Hide file tree
Showing 15 changed files with 610 additions and 78 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ This is a QGIS plugin that downloads Japan elevation tiles and converts them to

## Usage

- 1. Click plugin icon or select Plugin --> ElevationTile4JP in menu bar
- 2. Select tiles zoom level
- 3. Set the output file
- 4. Select the output file CRS
- 5. Set the area of interest. Area of interest can be set with:
- 1 Click plugin icon or select Plugin --> ElevationTile4JP in menu bar
- 2 Select tiles zoom level
- 3 Set the output file
- 4 Select the output file CRS
- 5 Set the area of interest. Area of interest can be set with:
- Layer: target layer extent
- Layout Map: target layout map extent
- Bookmark: extent saved on bookmark
- Map Canvas Extent: current map canvas extent
- Draw on Canvas: draw customize extent on map canvas

- 6. Click OK and the processed file will be added to the map canvas
- 6 Click OK and the processed file will be added to the map canvas

<img src="img/dialog.png" alt="dialog screenshot" width="600"/>

Expand Down
7 changes: 7 additions & 0 deletions elevation_tile_for_jp.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SOURCES += elevation_tile_for_jp_dialog.py \
progress_dialog.py \

FORMS += elevation_tile_for_jp_dialog_base.ui \
progress_dialog.ui

TRANSLATIONS = ./i18n/ELEVATIONTILE4JP_en.ts ./i18n/ELEVATIONTILE4JP_ja.ts
36 changes: 15 additions & 21 deletions elevation_tile_for_jp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
***************************************************************************/
"""

from PyQt5.QtCore import QSettings, QTranslator, qVersion, QCoreApplication
from PyQt5.QtCore import QSettings, QTranslator, QCoreApplication
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QAction

Expand All @@ -31,22 +31,20 @@


class ElevationTileForJP:

def __init__(self, iface):
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)

# initialize locale
if QSettings().value('locale/userLocale') is not None:
locale = QSettings().value('locale/userLocale')[0:2]
if QSettings().value("locale/userLocale") is not None:
locale = QSettings().value("locale/userLocale")[0:2]
else:
locale = 'en'
locale = "en"
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'ElevationTile4JP{}.qm'.format(locale))
self.plugin_dir, "i18n", "ELEVATIONTILE4JP_{}.qm".format(locale)
)

if os.path.exists(locale_path):
self.translator = QTranslator()
Expand All @@ -55,7 +53,7 @@ def __init__(self, iface):

# Declare instance attributes
self.actions = []
self.menu = self.tr(u'&ElevationTile4JP')
self.menu = self.tr("&ElevationTile4JP")

# Check if plugin was started the first time in current QGIS session
# Must be set in initGui() to survive plugin reloads
Expand All @@ -64,7 +62,7 @@ def __init__(self, iface):
# noinspection PyMethodMayBeStatic
def tr(self, message):
# noinspection PyTypeChecker,PyArgumentList,PyCallByClass
return QCoreApplication.translate('ElevationTileForJP', message)
return QCoreApplication.translate("ElevationTileForJP", message)

def add_action(
self,
Expand All @@ -76,9 +74,8 @@ def add_action(
add_to_toolbar=True,
status_tip=None,
whats_this=None,
parent=None
parent=None,
):

icon = QIcon(icon_path)
action = QAction(icon, text, parent)
action.triggered.connect(callback)
Expand All @@ -95,30 +92,27 @@ def add_action(
self.iface.addToolBarIcon(action)

if add_to_menu:
self.iface.addPluginToMenu(
self.menu,
action)
self.iface.addPluginToMenu(self.menu, action)

self.actions.append(action)

return action

def initGui(self):
icon_path = self.plugin_dir + '/icon.png'
icon_path = self.plugin_dir + "/icon.png"
self.add_action(
icon_path,
text=self.tr(u'ElevationTile4JP'),
text=self.tr("ElevationTile4JP"),
callback=self.dialog_show,
parent=self.iface.mainWindow())
parent=self.iface.mainWindow(),
)

# will be set False in run()
self.first_start = True

def unload(self):
for action in self.actions:
self.iface.removePluginMenu(
self.tr(u'&ElevationTile4JP'),
action)
self.iface.removePluginMenu(self.tr("&ElevationTile4JP"), action)
self.iface.removeToolBarIcon(action)

def dialog_show(self):
Expand Down
6 changes: 4 additions & 2 deletions elevation_tile_for_jp_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
from qgis.PyQt import QtWidgets
from PyQt5.QtCore import Qt

FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'elevation_tile_for_jp_dialog_base.ui'))
FORM_CLASS, _ = uic.loadUiType(
os.path.join(os.path.dirname(__file__), "elevation_tile_for_jp_dialog_base.ui")
)


class ElevationTileforJPDialog(QtWidgets.QDialog, FORM_CLASS):
def __init__(self, parent=None):
super(ElevationTileforJPDialog, self).__init__(parent)
self.setupUi(self)
self.setWindowFlags(Qt.WindowStaysOnTopHint)
self.mQgsFileWidget_output.setDialogTitle(self.tr("Select output file"))
6 changes: 3 additions & 3 deletions elevation_tile_for_jp_dialog_base.ui
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<cursorShape>ArrowCursor</cursorShape>
</property>
<property name="text">
<string>標高タイルズームレベル</string>
<string>Elevation tile zoom level</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -70,7 +70,7 @@
</font>
</property>
<property name="text">
<string>出力ファイル</string>
<string>Output file path</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -110,7 +110,7 @@
</font>
</property>
<property name="text">
<string>出力ファイルCRS</string>
<string>Output file CRS</string>
</property>
</widget>
</item>
Expand Down
4 changes: 2 additions & 2 deletions elevation_tile_tools/elevation_tile_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def tile_to_pixel_coordinate_of_corner(z, x_tile_coordinate, y_tile_coordinate):
def run(self):
try:
self.setMaximum.emit(self.number_of_tiles)
self.postMessage.emit("集計中")
self.postMessage.emit(self.tr("Processing..."))
self.setAbortable.emit(True)

# get elevation tile arrays
Expand All @@ -131,7 +131,7 @@ def run(self):
except Exception as e:
self.processFailed.emit(e)

self.postMessage.emit("終了処理中")
self.postMessage.emit(self.tr("Finalizing..."))
self.processFinished.emit()

def create_geotiff(self):
Expand Down
87 changes: 53 additions & 34 deletions get_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@


class GetTilesWithinMapCanvas:

# ダイアログの初期表示等の処理はここに記載する
def __init__(self, iface):
self.iface = iface
Expand All @@ -57,7 +56,7 @@ def __init__(self, iface):
self.dlg.mQgsFileWidget_output.setStorageMode(
QgsFileWidget.StorageMode.SaveFile
)
self.dlg.mQgsFileWidget_output.setDialogTitle("保存ファイルを選択してください")

# プロジェクトのデフォルトのcrsを格納
self.dlg.mQgsProjectionSelectionWidget_output_crs.setCrs(self.project.crs())

Expand All @@ -80,7 +79,7 @@ def __init__(self, iface):

# update extent crs when updated
def on_map_crs_changed(self):
self.dlg.mExtentGroupBox.setOutputCrs(QgsProject.instance().crs()),
(self.dlg.mExtentGroupBox.setOutputCrs(QgsProject.instance().crs()),)
self.dlg.mExtentGroupBox.setOutputExtentFromCurrent()

# キャンセルクリック
Expand All @@ -96,8 +95,8 @@ def on_abort_clicked(
) -> None:
if QMessageBox.Yes == QMessageBox.question(
None,
"確認",
"処理を中断し、以降の処理をスキップしてよろしいですか?",
progress_dialog.translate("Aborting"),
progress_dialog.translate("Are you sure to cancel process?"),
QMessageBox.Yes,
QMessageBox.No,
):
Expand All @@ -107,10 +106,10 @@ def on_abort_clicked(
def handle_process_failed(
self, error_message, thread: QThread, progress_dialog: ProgressDialog
) -> None:
progress_dialog.close(),
QMessageBox.information(None, "エラー", error_message),
self.set_interrupted(),
self.abort_process(thread, progress_dialog),
progress_dialog.close()
QMessageBox.information(None, progress_dialog.translate("Error"), error_message)
self.set_interrupted()
self.abort_process(thread, progress_dialog)

def abort_process(self, thread: QThread, progress_dialog: ProgressDialog) -> None:
if self.process_interrupted:
Expand All @@ -127,18 +126,35 @@ def calc(self):
zoom_level = int(self.dlg.comboBox_zoomlevel.currentText())
bbox = self.transform(project_crs, self.get_canvas_bbox())

# elevation tiles converter process thread
thread = ElevationTileConverter(
output_path=geotiff_output_path,
output_crs_id=output_crs.authid(),
zoom_level=zoom_level,
bbox=bbox,
)

# initialize process dialog
progress_dialog = ProgressDialog(thread.set_abort_flag)

# 入力値のバリデーション
if geotiff_output_path == "":
QMessageBox.information(
None, "エラー", "出力ファイル名を指定してください。"
None,
progress_dialog.translate("Error"),
progress_dialog.translate("Output file is not defined."),
)
return

# check if directory exists
directory = os.path.dirname(geotiff_output_path)
if not os.path.isdir(directory):
QMessageBox.information(
None, "エラー", f"出力フォルダー先が存在していません。\n{directory}"
None,
progress_dialog.translate("Error"),
"{}\n{}".format(
progress_dialog.translate("Cannot find output folder."), directory
),
)
return

Expand All @@ -150,8 +166,8 @@ def calc(self):
if not output_crs_isvalid:
QMessageBox.information(
None,
"エラー",
"出力ファイルの座標系が指定されていません。座標系を指定してください。",
progress_dialog.translate("Error"),
progress_dialog.translate("CRS of output file is not defined."),
)
return

Expand All @@ -160,44 +176,46 @@ def calc(self):
if xmin > xmax:
QMessageBox.information(
None,
"エラー",
"タイル取得範囲が不正です。マップキャンバスには標準時子午線を跨がない範囲を表示してください。",
progress_dialog.translate("Error"),
progress_dialog.translate(
"Target extent must not cross the International Date Line meridian."
),
)
return

# elevation tiles converter process thread
thread = ElevationTileConverter(
output_path=geotiff_output_path,
output_crs_id=output_crs.authid(),
zoom_level=zoom_level,
bbox=bbox,
)

# check number of tiles
if thread.number_of_tiles > thread.max_number_of_tiles:
error_message = (
f"取得タイル数({thread.number_of_tiles}枚)が多すぎます。\n"
f"上限の{thread.max_number_of_tiles}枚を超えないように取得領域を狭くするか、ズームレベルを小さくしてください。"
progress_dialog.translate("Too large amount of tiles ({})") + " \n"
)

error_message += progress_dialog.translate(
"Set a lower zoom level or extent to get less than {} tiles."
)

error_message = error_message.format(
thread.number_of_tiles, thread.max_number_of_tiles
)

QMessageBox.information(
None, progress_dialog.translate("Error"), error_message
)
QMessageBox.information(None, "エラー", error_message)
return

elif thread.number_of_tiles > thread.large_number_of_tiles:
message = (
f"取得タイル数({thread.number_of_tiles}枚)が多いため、処理に時間がかかる可能性があります。"
"ダウンロードを実行しますか?"
)
message = progress_dialog.translate(
"Dowloading {} tiles may take a while. Process anyway?"
).format(thread.number_of_tiles)

if QMessageBox.No == QMessageBox.question(
None,
"確認",
progress_dialog.translate("Warning"),
message,
QMessageBox.Yes,
QMessageBox.No,
):
return

# initialize process dialof
progress_dialog = ProgressDialog(thread.set_abort_flag)
progress_dialog.set_abortable(False)
progress_dialog.abortButton.clicked.connect(
lambda: [
Expand Down Expand Up @@ -237,7 +255,8 @@ def calc(self):
)

self.iface.messageBar().pushInfo(
"ElevationTile4JP", "GeoTiff形式のDEMを出力しました。"
"ElevationTile4JP",
progress_dialog.translate("DEM exported to Geotiff Format."),
)

self.dlg_cancel()
Expand Down
Binary file added i18n/ELEVATIONTILE4JP_en.qm
Binary file not shown.
Loading

0 comments on commit c18183f

Please sign in to comment.