forked from 3liz/QuickOSM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick_osm.py
271 lines (236 loc) · 11 KB
/
quick_osm.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# -*- coding: utf-8 -*-
"""
/***************************************************************************
QuickOSM
A QGIS plugin
OSM Overpass API frontend
-------------------
begin : 2014-06-11
copyright : (C) 2014 by 3Liz
email : info at 3liz dot com
contributor : Etienne Trimaille
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
from os.path import dirname, join, exists, abspath, isfile
from json import load
from PyQt4.QtCore import \
QSettings, QTranslator, qVersion, QCoreApplication, Qt
from PyQt4.QtGui import QMenu, QIcon, QAction, QPushButton
from qgis.gui import QgsMessageBar
from processing.core.Processing import Processing
from ui.main_window_dialog import MainWindowDialog
from ui.my_queries_dialog import MyQueriesDockWidget
from ui.query_dialog import QueryDockWidget
from ui.osm_file_dialog import OsmFileDockWidget
from ui.quick_query_dialog import QuickQueryDockWidget
from quick_osm_processing.algorithm_provider import QuickOSMAlgorithmProvider
from core.utilities.tools import (
get_current_version,
get_setting,
set_setting,
new_queries_available,
tr,
get_user_query_folder
)
class QuickOSM:
def __init__(self, iface):
"""Constructor.
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgsInterface
"""
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = dirname(__file__)
# initialize locale
locale = QSettings().value('locale/userLocale')[0:2]
locale_path = join(
self.plugin_dir,
'i18n',
'QuickOSM_{0}.qm'.format(locale))
if exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
if qVersion() > '4.3.3':
# noinspection PyTypeChecker
QCoreApplication.installTranslator(self.translator)
# Create the folder if it does not exist.
get_user_query_folder(over_write=True)
# Add to processing
self.provider = QuickOSMAlgorithmProvider()
Processing.addProvider(self.provider, True)
# Add the toolbar
self.toolbar = self.iface.addToolBar('QuickOSM')
self.toolbar.setObjectName('QuickOSM')
self.quickosm_menu = None
self.dock_menu = None
self.vector_menu = None
self.mainWindowAction = None
self.osmFileAction = None
self.osmFileDockWidget = None
self.myQueriesAction = None
self.myQueriesDockWidget = None
self.queryAction = None
self.queryDockWidget = None
self.quickQueryAction = None
self.quickQueryDockWidget = None
def initGui(self):
# Setup menu
self.quickosm_menu = QMenu('Quick OSM')
self.quickosm_menu.setIcon(QIcon(':/plugins/QuickOSM/icon.png'))
self.dock_menu = QMenu(tr('QuickOSM', u'Dock'))
self.vector_menu = self.iface.vectorMenu()
self.vector_menu.addMenu(self.quickosm_menu)
# Main window
self.mainWindowAction = QAction(
QIcon(':/plugins/QuickOSM/icon.png'),
u'QuickOSM',
self.iface.mainWindow())
# noinspection PyUnresolvedReferences
self.mainWindowAction.triggered.connect(self.openMainWindow)
self.toolbar.addAction(self.mainWindowAction)
self.iface.QuickOSM_mainWindowDialog = MainWindowDialog()
# OSM File
self.osmFileAction = QAction(
QIcon(':/plugins/QuickOSM/resources/open.png'),
tr('ui_osm_file', 'OSM File'),
self.iface.mainWindow())
# noinspection PyUnresolvedReferences
self.osmFileAction.triggered.connect(self.openOsmFileDockWidget)
self.osmFileDockWidget = OsmFileDockWidget()
self.iface.addDockWidget(
Qt.RightDockWidgetArea, self.osmFileDockWidget)
self.osmFileDockWidget.hide()
self.osmFileDockWidget.setObjectName('osmFileWidget')
# My queries
self.myQueriesAction = QAction(
QIcon(':/plugins/QuickOSM/resources/favorites.png'),
tr('ui_my_queries', 'My queries'),
self.iface.mainWindow())
# noinspection PyUnresolvedReferences
self.myQueriesAction.triggered.connect(self.openMyQueriesDockWidget)
self.myQueriesDockWidget = MyQueriesDockWidget()
self.iface.addDockWidget(
Qt.RightDockWidgetArea, self.myQueriesDockWidget)
self.myQueriesDockWidget.hide()
self.myQueriesDockWidget.setObjectName('myQueriesWidget')
# Query
self.queryAction = QAction(
QIcon(':/plugins/QuickOSM/resources/edit.png'),
tr('ui_query', 'Query'),
self.iface.mainWindow())
# noinspection PyUnresolvedReferences
self.queryAction.triggered.connect(self.openQueryDockWidget)
self.queryDockWidget = QueryDockWidget()
self.iface.addDockWidget(Qt.RightDockWidgetArea, self.queryDockWidget)
self.queryDockWidget.hide()
self.queryDockWidget.setObjectName('queryWidget')
# Quick query
self.quickQueryAction = QAction(
QIcon(':/plugins/QuickOSM/resources/quick.png'),
tr('ui_quick_query', 'Quick query'),
self.iface.mainWindow())
# noinspection PyUnresolvedReferences
self.quickQueryAction.triggered.connect(self.openQuickQueryDockWidget)
self.quickQueryDockWidget = QuickQueryDockWidget()
self.iface.addDockWidget(
Qt.RightDockWidgetArea, self.quickQueryDockWidget)
self.quickQueryDockWidget.hide()
self.quickQueryDockWidget.setObjectName('quickQueryWidget')
# Insert in the good order
self.quickosm_menu.addAction(self.mainWindowAction)
self.quickosm_menu.addMenu(self.dock_menu)
self.dock_menu.addAction(self.quickQueryAction)
self.dock_menu.addAction(self.queryAction)
self.dock_menu.addAction(self.myQueriesAction)
self.dock_menu.addAction(self.osmFileAction)
# Connect signals and slots from dock
self.queryDockWidget.signal_new_query_successful.connect(
self.iface.QuickOSM_mainWindowDialog.refresh_my_queries_tree)
self.queryDockWidget.signal_new_query_successful.connect(
self.myQueriesDockWidget.refresh_my_queries_tree)
self.myQueriesDockWidget.signal_delete_query_successful.connect(
self.myQueriesDockWidget.refresh_my_queries_tree)
self.myQueriesDockWidget.signal_delete_query_successful.connect(
self.iface.QuickOSM_mainWindowDialog.refresh_my_queries_tree)
# Connect signals and slots from mainWindow
self.iface.QuickOSM_mainWindowDialog.signal_new_query_successful.\
connect(self.myQueriesDockWidget.refresh_my_queries_tree)
self.iface.QuickOSM_mainWindowDialog.signal_new_query_successful.\
connect(
self.iface.QuickOSM_mainWindowDialog.refresh_my_queries_tree)
self.iface.QuickOSM_mainWindowDialog.signal_delete_query_successful.\
connect(self.myQueriesDockWidget.refresh_my_queries_tree)
self.iface.QuickOSM_mainWindowDialog.signal_delete_query_successful.\
connect(
self.iface.QuickOSM_mainWindowDialog.refresh_my_queries_tree)
# Read the config file
json_file_config = join(dirname(abspath(__file__)), 'config.json')
if isfile(json_file_config):
config_json = load(open(json_file_config))
for server in config_json['overpass_servers']:
self.iface.QuickOSM_mainWindowDialog.comboBox_default_OAPI.\
addItem(server)
# Check previous version and if new queries are available
version = get_setting('version')
current_version = get_current_version()
if version != current_version:
if new_queries_available():
message = 'New queries are available in the plugin. Would ' \
'like to install them ? This will overwrite the ' \
'current default queries.'
title = 'QuickOSM'
message = tr('QuickOSM', message)
widget = self.iface.messageBar().createMessage(title, message)
button_ok = QPushButton(widget)
button_ok.setText(
tr('QuickOSM', 'Install'))
button_ok.pressed.connect(self.restoreDefaultQueries)
widget.layout().addWidget(button_ok)
self.iface.messageBar().pushWidget(
widget, QgsMessageBar.INFO, 0)
set_setting('version', current_version)
def restoreDefaultQueries(self):
self.iface.QuickOSM_mainWindowDialog.restore_default_queries()
self.iface.messageBar().popWidget()
def unload(self):
self.iface.removePluginVectorMenu(u'&QuickOSM', self.mainWindowAction)
self.iface.removePluginVectorMenu(u'&QuickOSM', self.myQueriesAction)
self.iface.removePluginVectorMenu(u'&QuickOSM', self.queryAction)
self.iface.removePluginVectorMenu(u'&QuickOSM', self.quickQueryAction)
self.iface.removePluginVectorMenu(u'&QuickOSM', self.osmFileAction)
self.iface.removeToolBarIcon(self.mainWindowAction)
Processing.removeProvider(self.provider)
def openMainWindow(self):
self.iface.QuickOSM_mainWindowDialog.listWidget.setCurrentRow(0)
self.iface.QuickOSM_mainWindowDialog.exec_()
def openMyQueriesDockWidget(self):
if self.myQueriesDockWidget.isVisible():
self.myQueriesDockWidget.hide()
else:
self.myQueriesDockWidget.show()
def openQueryDockWidget(self):
if self.queryDockWidget.isVisible():
self.queryDockWidget.hide()
else:
self.queryDockWidget.show()
def openOsmFileDockWidget(self):
if self.osmFileDockWidget.isVisible():
self.osmFileDockWidget.hide()
else:
self.osmFileDockWidget.show()
def openQuickQueryDockWidget(self):
if self.quickQueryDockWidget.isVisible():
self.quickQueryDockWidget.hide()
else:
self.quickQueryDockWidget.show()