fork https://github.com/3liz/quickosm mit kleinen fixes fuer qgis2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

quick_osm.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. # -*- coding: utf-8 -*-
  2. """
  3. /***************************************************************************
  4. QuickOSM
  5. A QGIS plugin
  6. OSM Overpass API frontend
  7. -------------------
  8. begin : 2014-06-11
  9. copyright : (C) 2014 by 3Liz
  10. email : info at 3liz dot com
  11. contributor : Etienne Trimaille
  12. ***************************************************************************/
  13. /***************************************************************************
  14. * *
  15. * This program is free software; you can redistribute it and/or modify *
  16. * it under the terms of the GNU General Public License as published by *
  17. * the Free Software Foundation; either version 2 of the License, or *
  18. * (at your option) any later version. *
  19. * *
  20. ***************************************************************************/
  21. """
  22. from os.path import dirname, join, exists, abspath, isfile
  23. from json import load
  24. from PyQt4.QtCore import \
  25. QSettings, QTranslator, qVersion, QCoreApplication, Qt
  26. from PyQt4.QtGui import QMenu, QIcon, QAction, QPushButton
  27. from qgis.gui import QgsMessageBar
  28. from processing.core.Processing import Processing
  29. from QuickOSM.core.custom_logging import setup_logger
  30. from QuickOSM.ui.main_window_dialog import MainWindowDialog
  31. from QuickOSM.ui.my_queries_dialog import MyQueriesDockWidget
  32. from QuickOSM.ui.query_dialog import QueryDockWidget
  33. from QuickOSM.ui.osm_file_dialog import OsmFileDockWidget
  34. from QuickOSM.ui.quick_query_dialog import QuickQueryDockWidget
  35. from QuickOSM.quick_osm_processing.algorithm_provider import (
  36. QuickOSMAlgorithmProvider)
  37. from QuickOSM.core.utilities.tools import (
  38. get_current_version,
  39. get_setting,
  40. set_setting,
  41. new_queries_available,
  42. tr,
  43. get_user_query_folder
  44. )
  45. class QuickOSM:
  46. def __init__(self, iface):
  47. """Constructor.
  48. :param iface: An interface instance that will be passed to this class
  49. which provides the hook by which you can manipulate the QGIS
  50. application at run time.
  51. :type iface: QgsInterface
  52. """
  53. # Save reference to the QGIS interface
  54. self.iface = iface
  55. setup_logger('QuickOSM')
  56. # initialize plugin directory
  57. self.plugin_dir = dirname(__file__)
  58. # initialize locale
  59. locale = QSettings().value('locale/userLocale')[0:2]
  60. locale_path = join(
  61. self.plugin_dir,
  62. 'i18n',
  63. 'QuickOSM_{0}.qm'.format(locale))
  64. if exists(locale_path):
  65. self.translator = QTranslator()
  66. self.translator.load(locale_path)
  67. if qVersion() > '4.3.3':
  68. # noinspection PyTypeChecker
  69. QCoreApplication.installTranslator(self.translator)
  70. # Create the folder if it does not exist.
  71. get_user_query_folder(over_write=True)
  72. # Add to processing
  73. self.provider = QuickOSMAlgorithmProvider()
  74. Processing.addProvider(self.provider, True)
  75. # Add the toolbar
  76. self.toolbar = self.iface.addToolBar('QuickOSM')
  77. self.toolbar.setObjectName('QuickOSM')
  78. self.quickosm_menu = None
  79. self.dock_menu = None
  80. self.vector_menu = None
  81. self.mainWindowAction = None
  82. self.osmFileAction = None
  83. self.osmFileDockWidget = None
  84. self.myQueriesAction = None
  85. self.myQueriesDockWidget = None
  86. self.queryAction = None
  87. self.queryDockWidget = None
  88. self.quickQueryAction = None
  89. self.quickQueryDockWidget = None
  90. def initGui(self):
  91. # Setup menu
  92. self.quickosm_menu = QMenu('Quick OSM')
  93. self.quickosm_menu.setIcon(QIcon(':/plugins/QuickOSM/icon.png'))
  94. self.dock_menu = QMenu(tr('QuickOSM', u'Dock'))
  95. self.vector_menu = self.iface.vectorMenu()
  96. self.vector_menu.addMenu(self.quickosm_menu)
  97. # Main window
  98. self.mainWindowAction = QAction(
  99. QIcon(':/plugins/QuickOSM/icon.png'),
  100. u'QuickOSM',
  101. self.iface.mainWindow())
  102. # noinspection PyUnresolvedReferences
  103. self.mainWindowAction.triggered.connect(self.openMainWindow)
  104. self.toolbar.addAction(self.mainWindowAction)
  105. self.iface.QuickOSM_mainWindowDialog = MainWindowDialog()
  106. # OSM File
  107. self.osmFileAction = QAction(
  108. QIcon(':/plugins/QuickOSM/resources/open.png'),
  109. tr('ui_osm_file', 'OSM File'),
  110. self.iface.mainWindow())
  111. # noinspection PyUnresolvedReferences
  112. self.osmFileAction.triggered.connect(self.openOsmFileDockWidget)
  113. self.osmFileDockWidget = OsmFileDockWidget()
  114. self.iface.addDockWidget(
  115. Qt.RightDockWidgetArea, self.osmFileDockWidget)
  116. self.osmFileDockWidget.hide()
  117. self.osmFileDockWidget.setObjectName('osmFileWidget')
  118. # My queries
  119. self.myQueriesAction = QAction(
  120. QIcon(':/plugins/QuickOSM/resources/favorites.png'),
  121. tr('ui_my_queries', 'My queries'),
  122. self.iface.mainWindow())
  123. # noinspection PyUnresolvedReferences
  124. self.myQueriesAction.triggered.connect(self.openMyQueriesDockWidget)
  125. self.myQueriesDockWidget = MyQueriesDockWidget()
  126. self.iface.addDockWidget(
  127. Qt.RightDockWidgetArea, self.myQueriesDockWidget)
  128. self.myQueriesDockWidget.hide()
  129. self.myQueriesDockWidget.setObjectName('myQueriesWidget')
  130. # Query
  131. self.queryAction = QAction(
  132. QIcon(':/plugins/QuickOSM/resources/edit.png'),
  133. tr('ui_query', 'Query'),
  134. self.iface.mainWindow())
  135. # noinspection PyUnresolvedReferences
  136. self.queryAction.triggered.connect(self.openQueryDockWidget)
  137. self.queryDockWidget = QueryDockWidget()
  138. self.iface.addDockWidget(Qt.RightDockWidgetArea, self.queryDockWidget)
  139. self.queryDockWidget.hide()
  140. self.queryDockWidget.setObjectName('queryWidget')
  141. # Quick query
  142. self.quickQueryAction = QAction(
  143. QIcon(':/plugins/QuickOSM/resources/quick.png'),
  144. tr('ui_quick_query', 'Quick query'),
  145. self.iface.mainWindow())
  146. # noinspection PyUnresolvedReferences
  147. self.quickQueryAction.triggered.connect(self.openQuickQueryDockWidget)
  148. self.quickQueryDockWidget = QuickQueryDockWidget()
  149. self.iface.addDockWidget(
  150. Qt.RightDockWidgetArea, self.quickQueryDockWidget)
  151. self.quickQueryDockWidget.hide()
  152. self.quickQueryDockWidget.setObjectName('quickQueryWidget')
  153. # Insert in the good order
  154. self.quickosm_menu.addAction(self.mainWindowAction)
  155. self.quickosm_menu.addMenu(self.dock_menu)
  156. self.dock_menu.addAction(self.quickQueryAction)
  157. self.dock_menu.addAction(self.queryAction)
  158. self.dock_menu.addAction(self.myQueriesAction)
  159. self.dock_menu.addAction(self.osmFileAction)
  160. # Connect signals and slots from dock
  161. self.queryDockWidget.signal_new_query_successful.connect(
  162. self.iface.QuickOSM_mainWindowDialog.refresh_my_queries_tree)
  163. self.queryDockWidget.signal_new_query_successful.connect(
  164. self.myQueriesDockWidget.refresh_my_queries_tree)
  165. self.myQueriesDockWidget.signal_delete_query_successful.connect(
  166. self.myQueriesDockWidget.refresh_my_queries_tree)
  167. self.myQueriesDockWidget.signal_delete_query_successful.connect(
  168. self.iface.QuickOSM_mainWindowDialog.refresh_my_queries_tree)
  169. # Connect signals and slots from mainWindow
  170. self.iface.QuickOSM_mainWindowDialog.signal_new_query_successful.\
  171. connect(self.myQueriesDockWidget.refresh_my_queries_tree)
  172. self.iface.QuickOSM_mainWindowDialog.signal_new_query_successful.\
  173. connect(
  174. self.iface.QuickOSM_mainWindowDialog.refresh_my_queries_tree)
  175. self.iface.QuickOSM_mainWindowDialog.signal_delete_query_successful.\
  176. connect(self.myQueriesDockWidget.refresh_my_queries_tree)
  177. self.iface.QuickOSM_mainWindowDialog.signal_delete_query_successful.\
  178. connect(
  179. self.iface.QuickOSM_mainWindowDialog.refresh_my_queries_tree)
  180. # Read the config file
  181. json_file_config = join(dirname(abspath(__file__)), 'config.json')
  182. if isfile(json_file_config):
  183. config_json = load(open(json_file_config))
  184. for server in config_json['overpass_servers']:
  185. self.iface.QuickOSM_mainWindowDialog.comboBox_default_OAPI.\
  186. addItem(server)
  187. # Check previous version and if new queries are available
  188. version = get_setting('version')
  189. current_version = get_current_version()
  190. if version != current_version:
  191. if new_queries_available():
  192. message = 'New queries are available in the plugin. Would ' \
  193. 'like to install them ? This will overwrite the ' \
  194. 'current default queries.'
  195. title = 'QuickOSM'
  196. message = tr('QuickOSM', message)
  197. widget = self.iface.messageBar().createMessage(title, message)
  198. button_ok = QPushButton(widget)
  199. button_ok.setText(
  200. tr('QuickOSM', 'Install'))
  201. button_ok.pressed.connect(self.restoreDefaultQueries)
  202. widget.layout().addWidget(button_ok)
  203. self.iface.messageBar().pushWidget(
  204. widget, QgsMessageBar.INFO, 0)
  205. set_setting('version', current_version)
  206. def restoreDefaultQueries(self):
  207. self.iface.QuickOSM_mainWindowDialog.restore_default_queries()
  208. self.iface.messageBar().popWidget()
  209. def unload(self):
  210. self.iface.removePluginVectorMenu(u'&QuickOSM', self.mainWindowAction)
  211. self.iface.removePluginVectorMenu(u'&QuickOSM', self.myQueriesAction)
  212. self.iface.removePluginVectorMenu(u'&QuickOSM', self.queryAction)
  213. self.iface.removePluginVectorMenu(u'&QuickOSM', self.quickQueryAction)
  214. self.iface.removePluginVectorMenu(u'&QuickOSM', self.osmFileAction)
  215. self.iface.removeToolBarIcon(self.mainWindowAction)
  216. Processing.removeProvider(self.provider)
  217. def openMainWindow(self):
  218. self.iface.QuickOSM_mainWindowDialog.listWidget.setCurrentRow(0)
  219. self.iface.QuickOSM_mainWindowDialog.exec_()
  220. def openMyQueriesDockWidget(self):
  221. if self.myQueriesDockWidget.isVisible():
  222. self.myQueriesDockWidget.hide()
  223. else:
  224. self.myQueriesDockWidget.show()
  225. def openQueryDockWidget(self):
  226. if self.queryDockWidget.isVisible():
  227. self.queryDockWidget.hide()
  228. else:
  229. self.queryDockWidget.show()
  230. def openOsmFileDockWidget(self):
  231. if self.osmFileDockWidget.isVisible():
  232. self.osmFileDockWidget.hide()
  233. else:
  234. self.osmFileDockWidget.show()
  235. def openQuickQueryDockWidget(self):
  236. if self.quickQueryDockWidget.isVisible():
  237. self.quickQueryDockWidget.hide()
  238. else:
  239. self.quickQueryDockWidget.show()