forked from kartoza/sg-diagram-downloader
/
sg_map_tool.py
176 lines (143 loc) · 5.76 KB
/
sg_map_tool.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
# coding=utf-8
"""Implementation for SG Downloader map tool."""
# Enable SIP v2
#from PyQt4.QtGui import QRegExpValidator, QValidator
from PyQt4.QtCore import Qt
from PyQt4.QtGui import QProgressBar
from qgis.core import (
QgsFeature,
QgsFeatureRequest)
from qgis.gui import QgsMapTool, QgsMessageBar
from sg_utilities import (
download_sg_diagram,
province_for_point,
is_valid_sg_code,
point_to_rectangle,
diagram_directory)
class SGMapTool(QgsMapTool):
"""A map tool that lets you click on a parcel to download its SG Diagram.
"""
def __init__(self, iface, provinces_layer):
"""Constructor.
:param iface: A QGIS QgisInterface instance.
:type iface: QgisInterface
:param provinces_layer: A layer containing provincial boundaries.
:type provinces_layer: QgsVectorLayer
"""
canvas = iface.mapCanvas()
QgsMapTool.__init__(self, canvas)
self.canvas = canvas
self.iface = iface
self.provinces_layer = provinces_layer
self.message_bar = None
self.progress_bar = None
def canvasPressEvent(self, event):
"""Slot called when a mouse press occurs on the canvas.
:param event: Canvas event containing position of click, which button
was clicked etc.
"""
pass
def canvasMoveEvent(self, event):
"""Slot called when a mouse move event occurs on the canvas.
:param event: Canvas event containing position of click, which button
was clicked etc.
"""
pass
def setup_messagebar(self):
"""Setup a QgsMessageBar for use from callback and user notifications.
"""
if self.message_bar is not None:
return
self.message_bar = self.iface.messageBar().createMessage(
self.tr('SG Diagram Downloader'),
self.tr('Please stand by while download process is in progress.'),
self.iface.mainWindow())
# Set up message bar for progress reporting
self.progress_bar = QProgressBar()
self.progress_bar.setMaximumWidth(150)
self.progress_bar.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
self.message_bar.layout().addWidget(self.progress_bar)
self.iface.messageBar().pushWidget(
self.message_bar, self.iface.messageBar().INFO)
def canvasReleaseEvent(self, event):
"""Slot called when the mouse button is released on the canvas.
:param event: Canvas event containing position of click, which button
was clicked etc.
"""
if not event.button() == Qt.LeftButton:
return
def progress_callback(current, maximum, message=None):
"""GUI based callback implementation for showing progress.
:param current: Current progress.
:type current: int
:param maximum: Maximum range (point at which task is complete.
:type maximum: int
:param message: Optional message to display in the progress bar
:type message: str, QString
"""
if message is not None:
self.message_bar.setText(message)
if self.progress_bar is not None:
self.progress_bar.setMaximum(maximum)
self.progress_bar.setValue(current)
self.iface.messageBar().pushMessage(
self.tr('SG Downloader.'),
self.tr('Preparing for download'),
level=QgsMessageBar.INFO)
# No need to check that it is a valid, polygon layer
# as the QAction for this map tool already does that
layer = self.canvas.currentLayer()
place = self.toMapCoordinates(event.pos())
rectangle = point_to_rectangle(place)
request = QgsFeatureRequest(QgsFeatureRequest.FilterRect)
# Ensure only those features really intersecting the rect are returned
request.setFlags(QgsFeatureRequest.ExactIntersect)
request.setFilterRect(rectangle)
polygons = layer.getFeatures(request)
feature = QgsFeature()
fetch_list = []
all_fields = layer.pendingFields()
text_fields = []
# Ignore any columns that don't contain text data
for field in all_fields:
if field.typeName() == 'TEXT':
text_fields.append(field)
self.setup_messagebar()
sg_field = None
while polygons.nextFeature(feature):
geom = feature.geometry()
attributes = feature.attributes()
matched = False
sg_code = None
if sg_field is None:
for field in text_fields:
value = str(feature[field.name()])
if not is_valid_sg_code(value):
continue
sg_field = field.name()
fetch_list.append(value)
else:
# We already know which column has SG codes
value = str(feature[sg_field])
fetch_list.append(value)
if len(fetch_list) == 0:
self.iface.messageBar().pushMessage(
self.tr('SG Downloader.'),
self.tr('No parcels found with a valid 21 Digit code'),
level=QgsMessageBar.WARNING,
duration=10)
return
province = province_for_point(place, self.provinces_layer)
result = ''
output_path = diagram_directory()
for sg_code in fetch_list:
result += download_sg_diagram(
sg_code,
province,
output_path,
progress_callback)
log = file('sg_downloader.log', 'a')
log.write(result)
log.close()
# Cant return string from canvas release event
#return result