-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_poller.py
288 lines (228 loc) · 10.8 KB
/
file_poller.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# -*- coding: utf-8 -*-
"""
/***************************************************************************
AccessLinkFilePoller
A QGIS plugin
Zeige spezifische Vektorobjekte eines Layers an die in einer Access Datenbank beschrieben werden.
-------------------
begin : 2018-04-01
git sha : $Format:%H$
copyright : (C) 2018 by GBD GmbH
email : gebbert@gbd-consult.de
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
import os
import time
import qgis
import configparser
from qgis.core import QgsMapLayerRegistry, QgsMessageLog
from PyQt4.QtCore import QObject, QThread, SIGNAL
from PyQt4.QtGui import QMessageBox
worker_thread = None
worker_object = None
# The config file is always located in the plugin directory
CONFIG_FILE = os.path.join(os.path.dirname(__file__), "config.txt")
def write_settings(settings):
"""Write the settings into the text config file
:param settings:
:return:
"""
config = configparser.ConfigParser()
config.add_section('AccessLink')
config.set('AccessLink', 'transfer_dir', settings["transfer_dir"])
config.set('AccessLink', 'input_file', settings["input_file"])
config.set('AccessLink', 'output_file', settings["output_file"])
config.set('AccessLink', 'lock_file', settings["lock_file"])
config.set('AccessLink', 'access_bin', settings["access_bin"])
config.set('AccessLink', 'access_db', settings["access_db"])
config.set('AccessLink', 'vector_layer', settings["vector_layer"])
config.set('AccessLink', 'attribute_column', settings["attribute_column"])
config.set('AccessLink', 'poll_time', settings["poll_time"])
with open(CONFIG_FILE, 'w') as configfile:
config.write(configfile)
def read_settings():
"""Read the settings from the text config file
:return: settings
"""
QgsMessageLog.logMessage("Access-Link: Read Access Link settings")
transfer_dir = "C:\Users\Gebbert\Documents"
input_file = "ImpAcc.txt"
output_file = "ExpAcc.txt"
lock_file = "lock.log"
access_bin = "C:/Users/Gebbert/Documents/start_something.bat"
access_db = "C:/Users/Gebbert/Documents/ImpAcc.txt"
vector_layer = "Test_sie02_f_"
attribute_column = "OBJID"
poll_time = "0.5"
if os.path.exists(CONFIG_FILE) is False:
QMessageBox.critical(None, u"Access-Link: Fehler", u"Die Konfigurationsdatei <%s> "
u"wurde nicht gefunden. " % CONFIG_FILE)
raise Exception("Konfigurationsdatei nicht gefunden.")
config = configparser.ConfigParser()
with open(CONFIG_FILE, 'r') as configfile:
config.readfp(configfile)
configfile.close()
if config.has_section("AccessLink"):
if config.has_option("AccessLink", "transfer_dir"):
transfer_dir = config.get("AccessLink", "transfer_dir")
if config.has_option("AccessLink", "input_file"):
input_file = config.get("AccessLink", "input_file")
if config.has_option("AccessLink", "output_file"):
output_file = config.get("AccessLink", "output_file")
if config.has_option("AccessLink", "lock_file"):
lock_file = config.get("AccessLink", "lock_file")
if config.has_option("AccessLink", "access_bin"):
access_bin = config.get("AccessLink", "access_bin")
if config.has_option("AccessLink", "access_db"):
access_db = config.get("AccessLink", "access_db")
if config.has_option("AccessLink", "vector_layer"):
vector_layer = config.get("AccessLink", "vector_layer")
if config.has_option("AccessLink", "attribute_column"):
attribute_column = config.get("AccessLink", "attribute_column")
if config.has_option("AccessLink", "poll_time"):
poll_time = config.get("AccessLink", "poll_time")
settings = dict(transfer_dir=transfer_dir,
input_file=input_file,
output_file=output_file,
lock_file=lock_file,
access_bin=access_bin,
access_db=access_db,
vector_layer=vector_layer,
attribute_column=attribute_column,
poll_time=poll_time)
return settings
def zoom_to_feature():
QgsMessageLog.logMessage("Access-Link: versuche auf feature zu zoomen")
# Read the settings from the config file
settings = read_settings()
input_file = os.path.join(settings["transfer_dir"], settings["input_file"])
vector_layer = settings["vector_layer"]
attribute_column = settings["attribute_column"]
if os.path.exists(input_file) is False:
QgsMessageLog.logMessage(u"Access-Link: Fehler: Die Input Datei <%s> wurde nicht gefunden." % input_file)
# QMessageBox.warning(None, u"Access-Link: Warnung", u"Die Input Datei <%s> "
# u"wurde nicht gefunden. " % self.input_file)
return False
if not vector_layer:
return False
layer = QgsMapLayerRegistry.instance().mapLayersByName(vector_layer)
if not layer:
QgsMessageLog.logMessage(u"Access-Link: Fehler: Der Vektorlayer <%s> wurde nicht gefunden." % vector_layer)
return False
if len(layer) > 0:
layer = layer[0]
fields = layer.fields()
attr_list = []
for field in fields:
attr_list.append(field.name())
if attribute_column not in attr_list:
QMessageBox.critical(None, u"Access-Link: Fehler",
u"Der Vektorlayer <%s> hat keine Attributspalte <%s>." %
(vector_layer,
attribute_column))
return False
# Read the feature id and zoom to it
feature_id = open(input_file, "r").read().strip()
# Formulate the expression
expr = "\"%s\" = '%s'" % (attribute_column, feature_id)
QgsMessageLog.logMessage("Access-Link: Expression: %s"%expr)
layer.selectByExpression(expr)
qgis.utils.iface.setActiveLayer(layer)
# Zoom to the selected features
qgis.utils.iface.actionZoomToSelected().trigger()
QgsMessageLog.logMessage("Access-Link: zoom beendet")
def start_poll_worker(iface):
"""Start the thread that polls the ascii file for changes and zooms to the vector id
"""
global worker_thread, worker_object
if worker_thread:
stop_poll_worker()
worker_object = Worker()
worker_thread = QThread()
# Connect the
QgsMessageLog.logMessage("Access-Link: verbinde worker thread signal mit zoom Funktion")
iface.connect(worker_object, worker_object.signal, zoom_to_feature)
worker_object.moveToThread(worker_thread)
worker_thread.started.connect(worker_object.run)
worker_thread.start()
QgsMessageLog.logMessage("Access-Link: Worker thread gestartet")
def stop_poll_worker():
"""Stop the worker thread"""
global worker_thread, worker_object
if worker_thread:
if worker_object:
worker_object.kill()
worker_object = None
worker_thread.quit()
worker_thread.wait()
worker_thread.deleteLater()
worker_thread = None
QgsMessageLog.logMessage("Worker thread stopped")
class Worker(QObject):
"""This is the class that polls the ascii file and triggers the zom to selected features"""
def __init__(self):
QObject.__init__(self)
self.poll_time = None
self.input_file = None
self.vector_layer = None
self.lock_file = None
self.attribute_column = None
self.killed = False
self.mtime = None
self.layer = None
self.field_index = None
self.poll_time = 1
# The signal that is emitted when the file was modified
self.signal = SIGNAL("File_modified")
settings = read_settings()
self.input_file = os.path.join(settings["transfer_dir"], settings["input_file"])
self.lock_file = os.path.join(settings["transfer_dir"], settings["lock_file"])
if settings["poll_time"]:
self.poll_time = float(settings["poll_time"])
def kill(self):
self.killed = True
def run(self):
"""The function that runs the infinite loop to poll the ascii file for changes. If the text file changes,
then the a signal will be emitted to trigger the feature zoom.
"""
QgsMessageLog.logMessage("Access-Link: starte Dateipolling auf <%s>"%self.input_file)
lock_count = 0
while True:
# QgsMessageLog.logMessage("Access-Link: Polle Inputdatei")
time.sleep(self.poll_time)
if self.killed is True:
QgsMessageLog.logMessage("Access-Link: worker thread beendet")
break
# Check the modification time
if os.path.exists(self.input_file) is True:
new_mtime = os.path.getmtime(self.input_file)
if self.mtime == new_mtime:
continue
else:
QgsMessageLog.logMessage(u"Access-Link: Datei hat sich geaendert: Zeitstempel alt %s neu %s"%(
str(self.mtime), str(new_mtime)))
# Wait until the lock file was removed
if os.path.isfile(self.lock_file):
lock_count += 1
# Show
if lock_count > 20:
lock_count = 0
QMessageBox.critical(None, u"Access-Link: Fehler", u"Die Lock-Datei <%s> verhindert das "
u"Einlesen der Kataster ID. "
u"Bitte löschen." % self.lock_file)
continue
# Emit the vector zoom signal
self.mtime = new_mtime
QgsMessageLog.logMessage("Access-Link: versende zoom Signal")
self.emit(self.signal)
else:
pass
# print("Waiting for QGIS to initialize")