fork von https://github.com/nilsnolde/OSMtools mit QGIS2 processing erweiterung
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.

osm_tools.py 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # -*- coding: utf-8 -*-
  2. """
  3. /***************************************************************************
  4. OSMtools
  5. A QGIS plugin
  6. falk
  7. -------------------
  8. begin : 2017-02-01
  9. git sha : $Format:%H$
  10. copyright : (C) 2017 by Nils Nolde
  11. email : nils.nolde@gmail.com
  12. ***************************************************************************/
  13. This plugin provides access to the various APIs from OpenRouteService
  14. (http://openrouteservice.readthedocs.io/en/1.0/api.html), developed and
  15. maintained by GIScience team at University of Heidelberg, Germany. By using
  16. this plugin you agree to the ORS terms of service
  17. (http://openrouteservice.readthedocs.io/en/1.0/tos.html#terms-of-service).
  18. /***************************************************************************
  19. * *
  20. * This program is free software; you can redistribute it and/or modify *
  21. * it under the terms of the GNU General Public License as published by *
  22. * the Free Software Foundation; either version 2 of the License, or *
  23. * (at your option) any later version. *
  24. * *
  25. ***************************************************************************/
  26. """
  27. import os.path
  28. from PyQt4.QtCore import QSettings, QTranslator, QCoreApplication
  29. from PyQt4.Qt import PYQT_VERSION_STR
  30. from PyQt4.QtGui import QIcon, QAction, QApplication
  31. from processing.core.Processing import Processing
  32. from OSMtools.core import client, exceptions
  33. from OSMtools.gui import isochrones, directions, matrix
  34. from OSMtools.gui.dialog import OSMtoolsDialog
  35. from OSMtools.osmtools_processing.provider import OSMtoolsAlgoProvider
  36. import logging
  37. logging.basicConfig(format='%(levelname)s:%(message)s', level = logging.INFO)
  38. class OSMtools():
  39. """QGIS Plugin Implementation."""
  40. # noinspection PyTypeChecker,PyArgumentList,PyCallByClass
  41. def __init__(self, iface):
  42. """Constructor.
  43. :param iface: An interface instance that will be passed to this class
  44. which provides the hook by which you can manipulate the QGIS
  45. application at run time.
  46. :type iface: QgsInt
  47. self.dlg.close()
  48. # self.unload()erface
  49. """
  50. # Save reference to the QGIS interface
  51. self.iface = iface
  52. # initialize plugin directory
  53. self.plugin_dir = os.path.dirname(__file__)
  54. # initialize locale
  55. locale = QSettings().value('locale/userLocale')[0:2]
  56. locale_path = os.path.join(
  57. self.plugin_dir,
  58. 'i18n',
  59. 'OSMtools_{}.qm'.format(locale))
  60. if os.path.exists(locale_path):
  61. # noinspection PyTypeChecker,PyArgumentList,PyCallByClass
  62. self.translator = QTranslator()
  63. self.translator.load(locale_path)
  64. if PYQT_VERSION_STR > '4.3.3':
  65. QCoreApplication.installTranslator(self.translator)
  66. self.toolbar = self.iface.addToolBar(u'OSMtools')
  67. self.toolbar.setObjectName(u'OSMtools')
  68. #custom __init__ declarations
  69. self.dlg = OSMtoolsDialog(self.iface)
  70. self.canvas = self.iface.mapCanvas()
  71. self.script_dir = os.path.dirname(os.path.abspath(__file__))
  72. self.provider = OSMtoolsAlgoProvider()
  73. # noinspection PyMethodMayBeStatic
  74. def tr(self, message):
  75. """Get the translation for a string using Qt translation API.
  76. We implement this ourselves since we do not inherit QObject.
  77. :param message: String for translation.
  78. :type message: str, QString
  79. :returns: Translated version of message.
  80. :rtype: QString
  81. """
  82. return QCoreApplication.translate('OSMtools', message)
  83. def initGui(self):
  84. """Create the menu entries and toolbar icons inside the QGIS GUI."""
  85. icon_path = os.path.join(os.path.dirname(__file__),'icon.png')
  86. self.action = QAction(QIcon(icon_path),
  87. self.tr(u'OSM Tools'), # tr text
  88. self.iface.mainWindow() # parent
  89. )
  90. self.iface.addPluginToMenu(u'&OSM Tools',
  91. self.action)
  92. self.iface.addToolBarIcon(self.action)
  93. self.action.triggered.connect(self.run)
  94. # Add to processing
  95. Processing.addProvider(self.provider, True)
  96. def unload(self):
  97. QApplication.restoreOverrideCursor()
  98. self.iface.removePluginWebMenu(u"&OSM Tools", self.action)
  99. self.iface.removeToolBarIcon(self.action)
  100. Processing.removeProvider(self.provider)
  101. del self.toolbar
  102. def run(self):
  103. """Run method that performs all the real work"""
  104. self.dlg.show()
  105. # Populate layer tree in comboboxes
  106. self.dlg._layerTreeChanged()
  107. # show the dialog
  108. # Run the dialog event loop
  109. result = self.dlg.exec_()
  110. # See if OK was pressed
  111. if result:
  112. try:
  113. clt = client.Client(self.iface)
  114. if self.dlg.tabWidget.currentIndex() == 2:
  115. m = matrix.matrix(self.dlg, clt, self.iface)
  116. m.matrix_calc()
  117. if self.dlg.tabWidget.currentIndex() == 1:
  118. iso = isochrones.Isochrones(self.dlg, clt, self.iface)
  119. iso.isochrones_calc()
  120. if self.dlg.tabWidget.currentIndex() == 0:
  121. route = directions.directions(self.dlg, clt, self.iface)
  122. route.directions_calc()
  123. except exceptions.Timeout:
  124. self.iface.messageBar().pushCritical('Time out',
  125. 'The connection exceeded the '
  126. 'timeout limit of 60 seconds')
  127. except (exceptions.ApiError,
  128. exceptions.TransportError,
  129. exceptions._OverQueryLimit) as e:
  130. self.iface.messageBar().pushCritical("{}: ".format(type(e)),
  131. "{}".format(str(e)))
  132. except Exception:
  133. raise
  134. finally:
  135. self.dlg.close()