forked from lcoandrade/OSMDownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosmDownloader_dialog.py
166 lines (140 loc) · 6.28 KB
/
osmDownloader_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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
OSMDownloaderDialog
A QGIS plugin
Plugin to download OSM data by area
-------------------
begin : 2015-04-07
git sha : $Format:%H$
copyright : (C) 2015 by Brazilian Army - Geographic Service Bureau
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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 __future__ import absolute_import
from builtins import str
import os
from qgis.PyQt.QtWidgets import QDialog, QFileDialog, QMessageBox, QProgressBar
from qgis.PyQt import uic
from qgis.PyQt.QtCore import pyqtSlot, QThreadPool, Qt
from qgis.core import Qgis
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'osmDownloader_dialog_base.ui'))
from .osm_downloader import OSMRequest
class OSMDownloaderDialog(QDialog, FORM_CLASS):
def __init__(self, iface, startX, startY, endX, endY, parent=None):
"""Constructor."""
super(OSMDownloaderDialog, self).__init__(parent)
# Set up the user interface from Designer.
# After setupUI you can access any designer object by doing
# self.<objectname>, and you can use autoconnect slots - see
# http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
# #widgets-and-dialogs-with-auto-connect
self.setupUi(self)
self.iface = iface
self.setCoordinates(startX, startY, endX, endY)
self.threadpool = QThreadPool()
self.size = 0
self.plugin_dir = os.path.dirname(__file__)
def setCoordinates(self, startX, startY, endX, endY):
if startX < endX:
minLong = startX
maxLong = endX
else:
minLong = endX
maxLong = startX
if startY < endY:
minLat = startY
maxLat = endY
else:
minLat = endY
maxLat = startY
self.wEdit.setText(str(minLong))
self.sEdit.setText(str(minLat))
self.eEdit.setText(str(maxLong))
self.nEdit.setText(str(maxLat))
@pyqtSlot()
def on_saveButton_clicked(self):
ret = QFileDialog.getSaveFileName(parent=None, caption='Define file name and location', filter='OSM Files(*.osm)')
fileName = ret[0]
split = fileName.split('.')
if len(split)>0 and split[-1] == 'osm':
pass
else:
fileName += '.osm'
self.filenameEdit.setText(fileName)
@pyqtSlot()
def on_button_box_accepted(self):
if self.filenameEdit.text() == '':
QMessageBox.warning(self, self.tr("Warning!"), self.tr("Please, select a location to save the file."))
return
# Initiating processing
osmRequest = OSMRequest(self.filenameEdit.text())
osmRequest.setParameters(self.wEdit.text(), self.sEdit.text(), self.eEdit.text(), self.nEdit.text())
# Connecting end signal
osmRequest.signals.processFinished.connect(self.processFinished)
osmRequest.signals.sizeReported.connect(self.reportSize)
osmRequest.signals.proxyOpened.connect(self.proxy)
osmRequest.signals.errorOccurred.connect(self.errorOccurred)
osmRequest.signals.userCanceled.connect(self.userCanceled)
# Setting the progress bar
# << Updated by SIGMOÉ
self.msgBar = self.iface.messageBar()
self.progressMessageBar = self.msgBar.createMessage('Downloading data...')
# >>
self.progressBar = QProgressBar()
self.progressBar.setAlignment(Qt.AlignLeft|Qt.AlignVCenter)
self.progressMessageBar.layout().addWidget(self.progressBar)
self.iface.messageBar().pushWidget(self.progressMessageBar, Qgis.Info)
self.progressBar.setRange(0, 0)
self.progressMessageBar.destroyed.connect(osmRequest.signals.cancel)
# Starting process
self.threadpool.start(osmRequest)
@pyqtSlot(str)
def proxy(self, proxy):
self.progressMessageBar.setText('Proxy set to: '+proxy)
@pyqtSlot(str)
def errorOccurred(self, message):
QMessageBox.warning(self, 'Fatal!', message)
self.close()
@pyqtSlot()
def userCanceled(self):
QMessageBox.warning(self, 'Info!', 'Process canceled by user!')
self.close()
@pyqtSlot(float)
def reportSize(self, size):
self.size = size
self.progressMessageBar.setText('Downloading: '+"{0:.2f}".format(size)+' megabytes from OSM servers...')
@pyqtSlot(str)
def processFinished(self, message):
self.progressBar.setRange(0, 100)
self.progressBar.setValue(100)
self.progressMessageBar.setText('Downloaded '+"{0:.2f}".format(self.size)+' megabytes in total from OSM servers')
if self.checkBox.isChecked():
# << Updated by SIGMOÉ
# Add each OSM layer with specific style
lyr_types = [
['multipolygons', 'polygon'],
['multilinestrings', 'line'],
['lines', 'line'],
['points', 'point']
]
for lt in lyr_types:
lyr = self.iface.addVectorLayer(self.filenameEdit.text()+'|layername='+lt[0], 'osm', 'ogr')
style = "styles/osm_mapnik_" + lt[1] + ".qml"
qml_file = os.path.join(self.plugin_dir, style)
lyr.loadNamedStyle(qml_file)
# >>
QMessageBox.warning(self, 'Info!', message)
# << Updated by SIGMOÉ
self.msgBar.clearWidgets()
# >>
self.close()