forked from OpenGeoLabs/czech_slovak_freegeodata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegion_dialog.py
191 lines (159 loc) · 8.52 KB
/
Region_dialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# -*- coding: utf-8 -*-
"""
/***************************************************************************
GeoDataDialog
A QGIS plugin
This plugin gathers cz/sk data sources.
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2020-08-04
git sha : $Format:%H$
copyright : (C) 2020 by Test
email : test
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import os
import configparser
import sys
import webbrowser
from qgis.PyQt import uic
from qgis.PyQt import QtWidgets
from qgis.PyQt import QtGui
from qgis.utils import iface
from qgis.core import *
from qgis.gui import *
from qgis.PyQt.QtGui import *
from qgis.PyQt.QtCore import *
from qgis.PyQt.QtWidgets import *
from .crs_trans.CoordinateTransformation import CoordinateTransformation
from .crs_trans.CoordinateTransformationList import CoordinateTransformationList
from .crs_trans.ShiftGrid import ShiftGrid
from .crs_trans.ShiftGridList import ShiftGridList
from .Geo_Data_dialog import GeoDataDialog
# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'Region_dialog_base.ui'))
class RegionDialog(QtWidgets.QDialog, FORM_CLASS):
def __init__(self, iface, parent=None, start=True):
"""Constructor."""
super(RegionDialog, self).__init__(parent)
self.iface = iface
self.setupUi(self)
self.start = start
self.pushButtonSVK.setIcon(QIcon(os.path.join(os.path.dirname(__file__), "icons/svk.png")))
self.pushButtonCZE.setIcon(QIcon(os.path.join(os.path.dirname(__file__), "icons/cze.png")))
self.pushButtonSVK.clicked.connect(self.setRegionSVK)
self.pushButtonCZE.clicked.connect(self.setRegionCZE)
self.grids = ShiftGridList()
self.load_shift_grids()
self.transformations = CoordinateTransformationList()
self.load_crs_transformations()
def setStart(self, start):
self.start = start
def setRegion(self, region):
self.transformations.applyTransforations(region)
QMessageBox.information(None, QApplication.translate("GeoData", "Info", None),
QApplication.translate("GeoData", "You have to restart QGIS to apply all settings.", None))
s = QgsSettings()
s.setValue("geodata_cz_sk/region", region)
if self.start:
self.hide()
gdd = GeoDataDialog(self.iface, self)
gdd.show()
gdd.exec_()
else:
self.hide()
def setRegionSVK(self):
self.setRegion("SVK")
def setRegionCZE(self):
self.setRegion("CZE")
def load_crs_transformations(self):
"""
Loads available transformatios defined in crs_trans.ini
"""
projVersion = QgsProjUtils.projVersionMajor()
transConfigFile = os.path.join(os.path.dirname(__file__), "crs_trans", "crs_trans.ini")
transConfig = configparser.ConfigParser()
try:
transConfig.read(transConfigFile)
except Exception:
self.iface.messageBar().pushMessage(QApplication.translate("GeoData", "Error", None),
QApplication.translate("GeoData", "Unable to read coordinate transformations definition file.", None),
level=Qgis.Critical)
raise Exception("Unable to read coordinate transformations definition file.")
for transSection in transConfig:
if transSection != "DEFAULT":
transSectionContent = transConfig[transSection]
regions = transSectionContent.get("Regions", None)
if isinstance(regions, str) and regions is not None:
regions = regions.split(" ")
crsFrom = transSectionContent.get("CrsFrom")
crsTo = transSectionContent.get("CrsTo")
# TransfOld is used only for Proj version 6 and only if present
if projVersion == 6 and "TransfOld" in [x[0] for x in transConfig.items(transSection)]:
transformation = transSectionContent.get("TransfOld")
else:
transformation = transSectionContent.get("Transf")
if projVersion == 6:
grid = transSectionContent.get("GridOld", None)
else:
grid = transSectionContent.get("Grid", None)
if grid is not None and len(self.grids.getGridsByKeys(grid)) != 1:
self.iface.messageBar().pushMessage(QApplication.translate("GeoData", "Warning", None),
QApplication.translate("GeoData", "Skipping definition section {} because grid {} is unknown.".format(transSection, grid), None),
level=Qgis.Warning,
duration=5)
continue
# print("--------------------\nSection: {}\nRegion: {}\nCrsFrom: {}\nCrsTo: {}\nTransformation: {}\nShiftFile: {}".format(
# transSection, regions, crsFrom, crsTo, transformation, gridFileUrl))
if regions is None or regions == "" or \
crsFrom is None or crsFrom == "" or \
crsTo is None or crsTo == "" or \
transformation is None or transformation == "":
self.iface.messageBar().pushMessage(QApplication.translate("GeoData", "Warning", None),
QApplication.translate("GeoData", "Skipping incomplete transformation definition section {}.".format(transSection), None),
level=Qgis.Warning,
duration=5)
continue
try:
transf = CoordinateTransformation(regions, crsFrom, crsTo, transformation, self.grids, grid)
self.transformations.append(transf)
except Exception:
continue
def load_shift_grids(self):
"""
Loads available shift grids defined in grids.ini
"""
gridsConfigFile = os.path.join(os.path.dirname(__file__), "crs_trans", "grids.ini")
gridsConfig = configparser.ConfigParser()
try:
gridsConfig.read(gridsConfigFile)
except Exception:
self.iface.messageBar().pushMessage(QApplication.translate("GeoData", "Error", None),
QApplication.translate("GeoData", "Unable to read grids definition file.", None),
level=Qgis.Critical)
raise Exception("Unable to read grids definition file.")
for grid in gridsConfig:
if grid != "DEFAULT":
gridContent = gridsConfig[grid]
gridFileUrl = gridContent.get("GridFileUrl")
gridFileName = gridContent.get("GridFileName")
if gridFileUrl is None or gridFileName is None:
self.iface.messageBar().pushMessage(QApplication.translate("GeoData", "Warning", None),
QApplication.translate("GeoData", "Skipping grid definition of grid {}.".format(grid), None),
level=Qgis.Warning,
duration=5)
continue
try:
shiftGrid = ShiftGrid(grid, gridFileUrl, gridFileName)
self.grids.append(shiftGrid)
except Exception:
continue