forked from tangible-landscape/grass-tangible-landscape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
g.gui.tangible.py
490 lines (430 loc) · 22.7 KB
/
g.gui.tangible.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 20 14:44:32 2013
@author: anna
"""
import os
import wx
import wx.lib.newevent
import wx.lib.filebrowsebutton as filebrowse
from shutil import copyfile
from grass.script.utils import set_path, get_lib_path
set_path(modulename='g.gui.tangible')
from grass.script.setup import set_gui_path
set_gui_path()
from gui_core.gselect import Select
from core.settings import UserSettings
import grass.script as gscript
from grass.pydispatch.signal import Signal
from tangible_utils import run_analyses
updateGUIEvt, EVT_UPDATE_GUI = wx.lib.newevent.NewCommandEvent()
class AnalysesPanel(wx.Panel):
def __init__(self, parent, giface, settings):
wx.Panel.__init__(self, parent)
self.giface = giface
self.settings = settings
mainSizer = wx.BoxSizer(wx.VERTICAL)
if self.settings['analyses']['file']:
path = self.settings['analyses']['file']
initDir = os.path.dirname(path)
else:
path = initDir = ""
self.contoursSelect = Select(self, size=(-1, -1), type='vector')
self.contoursStepTextCtrl = wx.TextCtrl(self, size=(40, -1))
self.contoursStepTextCtrl.SetToolTipString("Contour step")
if 'contours' in self.settings['analyses'] and self.settings['analyses']['contours']:
self.contoursStepTextCtrl.SetValue(str(self.settings['analyses']['contours_step']))
self.contoursSelect.SetValue(self.settings['analyses']['contours'])
self.contoursSelect.Bind(wx.EVT_TEXT, self.OnAnalysesChange)
self.contoursStepTextCtrl.Bind(wx.EVT_TEXT, self.OnAnalysesChange)
self.selectAnalyses = filebrowse.FileBrowseButton(self, labelText="Analyses:",
startDirectory=initDir, initialValue=path,
changeCallback=lambda evt: self.SetAnalysesFile(evt.GetString()))
if self.settings['analyses']['file']:
self.selectAnalyses.SetValue(self.settings['analyses']['file'])
newAnalyses = wx.Button(self, label="Create new file")
newAnalyses.Bind(wx.EVT_BUTTON, lambda evt: self.CreateNewFile())
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(wx.StaticText(self, label="Contours:"), proportion=0, flag=wx.ALIGN_CENTER_VERTICAL|wx.RIGHT, border=5)
sizer.Add(self.contoursSelect, proportion=1, flag=wx.ALIGN_CENTER_VERTICAL|wx.RIGHT, border=5)
sizer.Add(self.contoursStepTextCtrl, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL, border=5)
mainSizer.Add(sizer, flag=wx.EXPAND | wx.ALL, border=5)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.selectAnalyses, proportion=1, flag=wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, border=5)
mainSizer.Add(sizer, flag=wx.EXPAND | wx.ALL, border=5)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.AddStretchSpacer()
sizer.Add(newAnalyses, proportion=1, flag=wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, border=5)
mainSizer.Add(sizer, flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5)
self.SetSizer(mainSizer)
mainSizer.Fit(self)
def SetAnalysesFile(self, path):
self.settings['analyses']['file'] = path
def OnAnalysesChange(self, event):
self.settings['analyses']['contours'] = self.contoursSelect.GetValue()
self.settings['analyses']['contours_step'] = self.contoursStepTextCtrl.GetValue()
def CreateNewFile(self):
get_lib_path('g.gui.tangible')
dlg = wx.FileDialog(self, message="Create a new file with analyses",
wildcard="Python source (*.py)|*.py",
style=wx.SAVE | wx.FD_OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
orig = os.path.join(get_lib_path('g.gui.tangible'), 'current_analyses.py')
if not os.path.exists(orig):
self.giface.WriteError("File with analyses not found: {}".format(orig))
else:
copyfile(orig, path)
self.selectAnalyses.SetValue(path)
self.settings['analyses']['file'] = path
dlg.Destroy()
class ScanningPanel(wx.Panel):
def __init__(self, parent, giface, settings):
wx.Panel.__init__(self, parent)
self.giface = giface
self.settings = settings
self.scan = self.settings['scan']
self.settingsChanged = Signal('ScanningPanel.settingsChanged')
if 'export' in self.settings and self.settings['export']['file']:
path = self.settings['export']['file']
initDir = os.path.dirname(path)
else:
path = initDir = ""
mainSizer = wx.BoxSizer(wx.VERTICAL)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
# create widgets
self.scan_name_ctrl = wx.TextCtrl(self, value='scan')
# widgets for model
self.elevInput = Select(self, size=(-1, -1), type='raster')
self.regionInput = Select(self, size=(-1, -1), type='region')
self.zexag = wx.TextCtrl(self)
self.numscans = wx.SpinCtrl(self, min=1, max=5, initial=1)
self.rotate = wx.SpinCtrl(self, min=0, max=360, initial=180)
self.smooth = wx.TextCtrl(self)
self.resolution = wx.TextCtrl(self)
self.trim = {}
for each in 'tbnsew':
self.trim[each] = wx.TextCtrl(self, size=(40, -1))
self.trim_tolerance = wx.TextCtrl(self)
self.interpolate = wx.CheckBox(self, label="Use interpolation instead of binning")
self.equalize = wx.CheckBox(self, label="Use equalized color table for scan")
self.ifExport = wx.CheckBox(self, label="")
self.exportPLY = filebrowse.FileBrowseButton(self, labelText="Export PLY:", fileMode=wx.SAVE,
startDirectory=initDir, initialValue=path,
changeCallback=self.OnModelProperties)
self.elevInput.SetValue(self.scan['elevation'])
self.regionInput.SetValue(self.scan['region'])
self.zexag.SetValue(str(self.scan['zexag']))
self.rotate.SetValue(self.scan['rotation_angle'])
self.numscans.SetValue(self.scan['numscans'])
self.interpolate.SetValue(self.scan['interpolate'])
for i, each in enumerate('nsewtb'):
self.trim[each].SetValue(self.scan['trim_nsewtb'].split(',')[i])
self.equalize.SetValue(self.scan['equalize'])
self.smooth.SetValue(str(self.scan['smooth']))
self.resolution.SetValue(str(self.scan['resolution']))
self.trim_tolerance.SetValue(str(self.scan['trim_tolerance']))
self.ifExport.SetValue(True if self.settings['export']['active'] else False)
# layout
mainSizer.Add(hSizer, flag=wx.EXPAND)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
hSizer.Add(wx.StaticText(self, label="Name of scanned raster:"), flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
hSizer.Add(self.scan_name_ctrl, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
mainSizer.Add(hSizer, flag=wx.EXPAND)
# model parameters
hSizer = wx.BoxSizer(wx.HORIZONTAL)
hSizer.Add(wx.StaticText(self, label="Reference DEM:"), flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
hSizer.Add(self.elevInput, proportion=1, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
mainSizer.Add(hSizer, flag=wx.EXPAND)
# region
hSizer = wx.BoxSizer(wx.HORIZONTAL)
hSizer.Add(wx.StaticText(self, label="Reference region:"), flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
hSizer.Add(self.regionInput, proportion=1, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
mainSizer.Add(hSizer, flag=wx.EXPAND)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
hSizer.Add(wx.StaticText(self, label="Z-exaggeration:"), proportion=1, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
hSizer.Add(self.zexag, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
mainSizer.Add(hSizer, flag=wx.EXPAND)
# number of scans
hSizer = wx.BoxSizer(wx.HORIZONTAL)
hSizer.Add(wx.StaticText(self, label="Number of scans:"), proportion=1, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
hSizer.Add(self.numscans, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
mainSizer.Add(hSizer, flag=wx.EXPAND)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
hSizer.Add(wx.StaticText(self, label="Rotation angle:"), proportion=1, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
hSizer.Add(self.rotate, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
mainSizer.Add(hSizer, flag=wx.EXPAND)
# smooth
hSizer = wx.BoxSizer(wx.HORIZONTAL)
hSizer.Add(wx.StaticText(self, label="Smooth value:"), proportion=1, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
hSizer.Add(self.smooth, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
mainSizer.Add(hSizer, flag=wx.EXPAND)
# resolution
hSizer = wx.BoxSizer(wx.HORIZONTAL)
hSizer.Add(wx.StaticText(self, label="Resolution [mm]:"), proportion=1, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
hSizer.Add(self.resolution, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
mainSizer.Add(hSizer, flag=wx.EXPAND)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
hSizer.Add(wx.StaticText(self, label="Limit scan vertically T, B [cm]:"), proportion=1, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
for each in 'tb':
hSizer.Add(self.trim[each], flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
mainSizer.Add(hSizer, flag=wx.EXPAND)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
hSizer.Add(wx.StaticText(self, label="Trim scan N, S, E, W [cm]:"), proportion=1, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
for each in 'nsew':
hSizer.Add(self.trim[each], flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
mainSizer.Add(hSizer, flag=wx.EXPAND)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
hSizer.Add(wx.StaticText(self, label="Trim tolerance [0-1]:"), proportion=1, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
hSizer.Add(self.trim_tolerance, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
mainSizer.Add(hSizer, flag=wx.EXPAND)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
hSizer.Add(self.interpolate, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
mainSizer.Add(hSizer, flag=wx.EXPAND)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
hSizer.Add(self.equalize, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=3)
mainSizer.Add(hSizer, flag=wx.EXPAND)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
hSizer.Add(self.ifExport, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.LEFT, border=3)
hSizer.Add(self.exportPLY, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, proportion=1, border=0)
mainSizer.Add(hSizer, flag=wx.EXPAND)
self.SetSizer(mainSizer)
mainSizer.Fit(self)
self.BindModelProperties()
def BindModelProperties(self):
self.scan_name_ctrl.Bind(wx.EVT_TEXT, self.OnModelProperties)
# model parameters
self.elevInput.Bind(wx.EVT_TEXT, self.OnModelProperties)
self.regionInput.Bind(wx.EVT_TEXT, self.OnModelProperties)
self.zexag.Bind(wx.EVT_TEXT, self.OnModelProperties)
self.rotate.Bind(wx.EVT_SPINCTRL, self.OnModelProperties)
self.rotate.Bind(wx.EVT_TEXT, self.OnModelProperties)
self.numscans.Bind(wx.EVT_SPINCTRL, self.OnModelProperties)
self.numscans.Bind(wx.EVT_TEXT, self.OnModelProperties)
self.interpolate.Bind(wx.EVT_CHECKBOX, self.OnModelProperties)
self.equalize.Bind(wx.EVT_CHECKBOX, self.OnModelProperties)
self.smooth.Bind(wx.EVT_TEXT, self.OnModelProperties)
self.resolution.Bind(wx.EVT_TEXT, self.OnModelProperties)
self.trim_tolerance.Bind(wx.EVT_TEXT, self.OnModelProperties)
for each in 'nsewtb':
self.trim[each].Bind(wx.EVT_TEXT, self.OnModelProperties)
self.ifExport.Bind(wx.EVT_CHECKBOX, self.OnModelProperties)
def OnModelProperties(self, event):
self.scan['scan_name'] = self.scan_name_ctrl.GetValue()
self.scan['elevation'] = self.elevInput.GetValue()
self.scan['region'] = self.regionInput.GetValue()
self.scan['rotation_angle'] = self.rotate.GetValue()
self.scan['numscans'] = self.numscans.GetValue()
self.scan['interpolate'] = self.interpolate.IsChecked()
self.scan['equalize'] = self.equalize.IsChecked()
self.scan['smooth'] = self.smooth.GetValue()
self.scan['resolution'] = self.resolution.GetValue()
self.scan['trim_tolerance'] = self.trim_tolerance.GetValue()
if 'export' not in self.settings:
self.settings['export'] = {}
self.settings['export']['active'] = self.ifExport.IsChecked()
self.settings['export']['file'] = self.exportPLY.GetValue()
try:
self.scan['zexag'] = float(self.zexag.GetValue())
nsewtb_list = []
for each in 'nsewtb':
nsewtb_list.append(self.trim[each].GetValue())
self.scan['trim_nsewtb'] = ','.join(nsewtb_list)
except ValueError:
pass
self.settingsChanged.emit()
class TangibleLandscapePlugin(wx.Dialog):
def __init__(self, giface, parent):
wx.Dialog.__init__(self, parent, title="Tangible Landscape", style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
self.giface = giface
self.parent = parent
if not gscript.find_program('r.in.kinect'):
self.giface.WriteError("ERROR: Module r.in.kinect not found.")
self.settings = {}
UserSettings.ReadSettingsFile(settings=self.settings)
# for the first time
if not 'tangible' in self.settings:
self.settings['tangible'] = {'calibration': {'matrix': None},
'analyses': {'file': None,
'contours': None,
'contours_step': 1},
'scan': {'scan_name': 'scan',
'elevation': '', 'region': '',
'zexag': 1., 'smooth': 7, 'numscans': 1,
'rotation_angle': 180, 'resolution': 2,
'trim_nsewtb': '30,30,30,30,60,100',
'interpolate': False, 'trim_tolerance': 0.7,
'equalize': False
},
'export': {'active': True,
'file': ''
}
}
self.scan = self.settings['tangible']['scan']
self.calib_matrix = self.settings['tangible']['calibration']['matrix']
if not self.calib_matrix:
giface.WriteWarning("WARNING: No calibration file exists")
self.delay = 0.3
self.process = None
self.observer = None
self.timer = wx.Timer(self)
self.changedInput = False
self.notebook = wx.Notebook(self)
scanning_panel = ScanningPanel(self.notebook, self.giface, self.settings['tangible'])
self.notebook.AddPage(scanning_panel, "Scanning")
scanning_panel.settingsChanged.connect(lambda: setattr(self, 'changedInput', True))
analyses_panel = AnalysesPanel(self.notebook, self.giface, self.settings['tangible'])
self.notebook.AddPage(analyses_panel, "Analyses")
btnStart = wx.Button(self, label="Start")
btnStop = wx.Button(self, label="Stop")
btnScanOnce = wx.Button(self, label="Scan once")
btnCalibrate = wx.Button(self, label="Calibrate")
btnHelp = wx.Button(self, label="Help")
btnClose = wx.Button(self, label="Close")
self.status = wx.StaticText(self)
# bind events
btnStart.Bind(wx.EVT_BUTTON, lambda evt: self.Start())
btnStop.Bind(wx.EVT_BUTTON, lambda evt: self.Stop())
btnCalibrate.Bind(wx.EVT_BUTTON, self.Calibrate)
btnScanOnce.Bind(wx.EVT_BUTTON, self.ScanOnce)
btnHelp.Bind(wx.EVT_BUTTON, self.OnHelp)
btnClose.Bind(wx.EVT_BUTTON, self.OnClose)
self.Layout()
sizer = wx.BoxSizer(wx.VERTICAL)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
hSizer.Add(btnStart, flag=wx.EXPAND | wx.ALL, border=5)
hSizer.Add(btnStop, flag=wx.EXPAND | wx.ALL, border=5)
hSizer.Add(btnCalibrate, flag=wx.EXPAND | wx.ALL, border=5)
hSizer.Add(btnScanOnce, flag=wx.EXPAND | wx.ALL, border=5)
sizer.Add(hSizer, 0, wx.ALL | wx.EXPAND, 5)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
hSizer.Add(self.status, flag=wx.EXPAND | wx.LEFT, border=5)
sizer.Add(hSizer)
sizer.Add(self.notebook, 1, wx.ALL | wx.EXPAND, 5)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
hSizer.AddStretchSpacer()
hSizer.Add(btnHelp, flag=wx.EXPAND | wx.ALL, border=5)
hSizer.Add(btnClose, flag=wx.EXPAND | wx.ALL, border=5)
sizer.Add(hSizer, flag=wx.EXPAND)
self.SetSizer(sizer)
sizer.Fit(self)
self.SetMinSize(self.GetBestSize())
self.Layout()
self.Bind(wx.EVT_TIMER, self.RestartIfNotRunning, self.timer)
self.Bind(wx.EVT_CLOSE, self.OnClose)
self.Bind(EVT_UPDATE_GUI, self.OnUpdate)
def OnHelp(self, event):
"""Show help"""
self.giface.Help(entry='g.gui.tangible', online=False)
def OnClose(self, event):
self.Stop()
UserSettings.SaveToFile(self.settings)
self.Destroy()
def OnUpdate(self, event):
for each in self.giface.GetAllMapDisplays():
each.GetMapWindow().UpdateMap(delay=self.delay)
def Calibrate(self, event):
res = gscript.parse_command('r.in.kinect', flags='c', overwrite=True)
if not (res['calib_matrix'] and len(res['calib_matrix'].split(',')) == 9):
gscript.message(_("Failed to calibrate"))
return
self.settings['tangible']['calibration']['matrix'] = res['calib_matrix']
UserSettings.SaveToFile(self.settings)
# update
self.calib_matrix = res['calib_matrix']
def Scan(self, continuous):
if self.process and self.process.poll() is None:
return
self.status.SetLabel("Scanning...")
wx.SafeYield()
params = {}
if self.scan['interpolate']:
method = 'interpolation'
else:
method = 'mean'
if self.calib_matrix:
params['calib_matrix'] = self.calib_matrix
if self.scan['elevation']:
params['raster'] = self.scan['elevation']
elif self.scan['region']:
params['region'] = self.scan['region']
if self.scan['trim_tolerance']:
params['trim_tolerance'] = self.scan['trim_tolerance']
trim_nsew = ','.join(self.scan['trim_nsewtb'].split(',')[:4])
zrange = ','.join(self.scan['trim_nsewtb'].split(',')[4:])
if continuous:
params['flags'] = 'l'
if self.scan['equalize']:
if 'flags' in params and params['flags']:
params['flags'] += 'e'
if self.settings['tangible']['analyses']['contours']:
params['contours'] = self.settings['tangible']['analyses']['contours']
params['contours_step'] = self.settings['tangible']['analyses']['contours_step']
if 'export' in self.settings['tangible'] and self.settings['tangible']['export']['active'] and \
self.settings['tangible']['export']['file']:
params['ply'] = self.settings['tangible']['export']['file']
self.process = gscript.start_command('r.in.kinect', output=self.scan['scan_name'],
trim=trim_nsew, smooth_radius=float(self.scan['smooth'])/1000,
method=method, zrange=zrange, rotate=self.scan['rotation_angle'],
resolution=float(self.scan['resolution'])/1000,
zexag=self.scan['zexag'], numscan=self.scan['numscans'],
overwrite=True, quiet=True, **params)
return self.process
def ScanOnce(self, event):
self.Scan(continuous=False)
self.status.SetLabel("Importing scan...")
self.process.wait()
self.process = None
run_analyses(scan_params=self.scan, analysesFile=self.settings['tangible']['analyses']['file'])
self.status.SetLabel("Done.")
self.OnUpdate(None)
def RestartIfNotRunning(self, event):
"""Mechanism to restart scanning if process ends or
there was a change in input options"""
if self.process and self.process.poll() is not None:
self.Start()
if self.changedInput:
self.changedInput = False
self.Stop()
self.Start()
def Start(self):
self.Scan(continuous=True)
self.status.SetLabel("Real-time scanning is running now.")
gisenv = gscript.gisenv()
path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'], gisenv['MAPSET'], 'fcell')
if not os.path.exists(path): # this happens in new mapset
path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'], gisenv['MAPSET'])
event_handler = RasterChangeHandler(self.runImport, self.scan)
self.observer = Observer()
self.observer.schedule(event_handler, path)
self.observer.start()
self.timer.Start(1000)
def Stop(self):
if self.process and self.process.poll() is None: # still running
self.process.terminate()
self.process.wait()
self.process = None
if self.observer:
try:
self.observer.stop()
except TypeError: # throws error on mac
pass
self.observer.join()
self.timer.Stop()
self.status.SetLabel("Real-time scanning stopped.")
def runImport(self):
run_analyses(scan_params=self.scan, analysesFile=self.settings['tangible']['analyses']['file'])
evt = updateGUIEvt(self.GetId())
wx.PostEvent(self, evt)
def main(giface=None):
global Observer, RasterChangeHandler
from watchdog.observers import Observer
from change_handler import RasterChangeHandler
dlg = TangibleLandscapePlugin(giface, parent=None)
dlg.Show()
if __name__ == '__main__':
gscript.parser()
from watchdog.observers import Observer
from change_handler import RasterChangeHandler
main()