vclean.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. """
  2. @package modules.vclean
  3. @brief Dialog for interactive construction of vector cleaning
  4. operations
  5. Classes:
  6. - vclean::VectorCleaningFrame
  7. (C) 2010-2011 by the GRASS Development Team
  8. This program is free software under the GNU General Public License
  9. (>=v2). Read the file COPYING that comes with GRASS for details.
  10. @author Markus Metz
  11. """
  12. import os
  13. import wx
  14. import wx.lib.scrolledpanel as scrolled
  15. from grass.script import core as grass
  16. from core.gcmd import RunCommand, GError
  17. from core import globalvar
  18. from gui_core.gselect import Select
  19. from core.debug import Debug
  20. from core.settings import UserSettings
  21. class VectorCleaningFrame(wx.Frame):
  22. def __init__(self, parent, id = wx.ID_ANY, title = _('Set up vector cleaning tools'),
  23. style = wx.DEFAULT_FRAME_STYLE | wx.RESIZE_BORDER,
  24. **kwargs):
  25. """!
  26. Dialog for interactively defining vector cleaning tools
  27. """
  28. wx.Frame.__init__(self, parent, id, title, style = style, **kwargs)
  29. self.parent = parent # GMFrame
  30. if self.parent:
  31. self.log = self.parent.GetLogWindow()
  32. else:
  33. self.log = None
  34. # grass command
  35. self.cmd = 'v.clean'
  36. # statusbar
  37. self.CreateStatusBar()
  38. # icon
  39. self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, 'grass.ico'), wx.BITMAP_TYPE_ICO))
  40. self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
  41. # input map to clean
  42. self.inmap = ''
  43. # cleaned output map
  44. self.outmap = ''
  45. self.ftype = ''
  46. # cleaning tools
  47. self.toolslines = {}
  48. self.tool_desc_list = [
  49. _('break lines/boundaries'),
  50. _('remove duplicates'),
  51. _('remove dangles'),
  52. _('change boundary dangles to lines'),
  53. _('remove bridges'),
  54. _('change bridges to lines'),
  55. _('snap lines/boundaries'),
  56. _('remove duplicate area centroids'),
  57. _('break polygons'),
  58. _('prune lines/boundaries'),
  59. _('remove small areas'),
  60. _('remove lines/boundaries of zero length'),
  61. _('remove small angles at nodes')
  62. ]
  63. self.tool_list = [
  64. 'break',
  65. 'rmdupl',
  66. 'rmdangle',
  67. 'chdangle',
  68. 'rmbridge',
  69. 'chbridge',
  70. 'snap',
  71. 'rmdac',
  72. 'bpol',
  73. 'prune',
  74. 'rmarea',
  75. 'rmline',
  76. 'rmsa'
  77. ]
  78. self.ftype = [
  79. 'point',
  80. 'line',
  81. 'boundary',
  82. 'centroid',
  83. 'area',
  84. 'face']
  85. self.n_ftypes = len(self.ftype)
  86. self.tools_string = ''
  87. self.thresh_string = ''
  88. self.ftype_string = ''
  89. self.SetStatusText(_("Set up vector cleaning tools"))
  90. self.elem = 'vector'
  91. self.ctlabel = _('Choose cleaning tools and set thresholds')
  92. # top controls
  93. self.inmaplabel = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  94. label= _('Select input vector map:'))
  95. self.selectionInput = Select(parent = self.panel, id = wx.ID_ANY,
  96. size = globalvar.DIALOG_GSELECT_SIZE,
  97. type = 'vector')
  98. self.ftype_check = {}
  99. ftypeBox = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
  100. label = _(' Feature type: '))
  101. self.ftypeSizer = wx.StaticBoxSizer(ftypeBox, wx.HORIZONTAL)
  102. self.outmaplabel = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  103. label = _('Select output vector map:'))
  104. self.selectionOutput = Select(parent = self.panel, id = wx.ID_ANY,
  105. size = globalvar.DIALOG_GSELECT_SIZE,
  106. type = 'vector')
  107. self.overwrite = wx.CheckBox(parent = self.panel, id = wx.ID_ANY,
  108. label = _('Allow output files to overwrite existing files'))
  109. self.overwrite.SetValue(UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled'))
  110. # cleaning tools
  111. self.ct_label = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  112. label = self.ctlabel)
  113. self.ct_panel = self._toolsPanel()
  114. # buttons to manage cleaning tools
  115. self.btn_add = wx.Button(parent = self.panel, id = wx.ID_ADD)
  116. self.btn_remove = wx.Button(parent = self.panel, id = wx.ID_REMOVE)
  117. self.btn_moveup = wx.Button(parent = self.panel, id = wx.ID_UP)
  118. self.btn_movedown = wx.Button(parent = self.panel, id = wx.ID_DOWN)
  119. # add one tool as default
  120. self.AddTool()
  121. self.selected = -1
  122. # Buttons
  123. self.btn_close = wx.Button(parent = self.panel, id = wx.ID_CLOSE)
  124. self.btn_run = wx.Button(parent = self.panel, id = wx.ID_ANY, label = _("&Run"))
  125. self.btn_run.SetDefault()
  126. self.btn_clipboard = wx.Button(parent = self.panel, id = wx.ID_COPY)
  127. self.btn_clipboard.SetToolTipString(_("Copy the current command string to the clipboard (Ctrl+C)"))
  128. self.btn_help = wx.Button(parent = self.panel, id = wx.ID_HELP)
  129. # bindings
  130. self.btn_close.Bind(wx.EVT_BUTTON, self.OnClose)
  131. self.btn_run.Bind(wx.EVT_BUTTON, self.OnCleaningRun)
  132. self.btn_clipboard.Bind(wx.EVT_BUTTON, self.OnCopy)
  133. self.btn_help.Bind(wx.EVT_BUTTON, self.OnHelp)
  134. self.btn_add.Bind(wx.EVT_BUTTON, self.OnAddTool)
  135. self.btn_remove.Bind(wx.EVT_BUTTON, self.OnClearTool)
  136. self.btn_moveup.Bind(wx.EVT_BUTTON, self.OnMoveToolUp)
  137. self.btn_movedown.Bind(wx.EVT_BUTTON, self.OnMoveToolDown)
  138. # layout
  139. self._layout()
  140. self.SetMinSize(self.GetBestSize())
  141. self.CentreOnScreen()
  142. def _layout(self):
  143. sizer = wx.BoxSizer(wx.VERTICAL)
  144. #
  145. # input output
  146. #
  147. inSizer = wx.GridBagSizer(hgap = 5, vgap = 5)
  148. inSizer.Add(item = self.inmaplabel, pos = (0, 0),
  149. flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL | wx.EXPAND, border = 1)
  150. inSizer.Add(item = self.selectionInput, pos = (1, 0),
  151. flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL | wx.EXPAND, border = 1)
  152. self.ftype_check = [
  153. wx.CheckBox(parent = self.panel, id = wx.ID_ANY, label = _('point')),
  154. wx.CheckBox(parent = self.panel, id = wx.ID_ANY, label = _('line')),
  155. wx.CheckBox(parent = self.panel, id = wx.ID_ANY, label = _('boundary')),
  156. wx.CheckBox(parent = self.panel, id = wx.ID_ANY, label = _('centroid')),
  157. wx.CheckBox(parent = self.panel, id = wx.ID_ANY, label = _('area')),
  158. wx.CheckBox(parent = self.panel, id = wx.ID_ANY, label = _('face'))
  159. ]
  160. typeoptSizer = wx.BoxSizer(wx.HORIZONTAL)
  161. for num in range(0, self.n_ftypes):
  162. type_box = self.ftype_check[num]
  163. typeoptSizer.Add(item = type_box, flag = wx.ALIGN_LEFT, border = 1)
  164. self.ftypeSizer.Add(item = typeoptSizer,
  165. flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = 2)
  166. outSizer = wx.GridBagSizer(hgap = 5, vgap = 5)
  167. outSizer.Add(item = self.outmaplabel, pos = (0, 0),
  168. flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL | wx.EXPAND, border = 1)
  169. outSizer.Add(item = self.selectionOutput, pos = (1, 0),
  170. flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL | wx.EXPAND, border = 1)
  171. replaceSizer = wx.BoxSizer(wx.HORIZONTAL)
  172. replaceSizer.Add(item = self.overwrite, proportion = 1,
  173. flag = wx.ALL | wx.EXPAND, border = 1)
  174. outSizer.Add(item = replaceSizer, pos = (2, 0),
  175. flag = wx.ALL | wx.EXPAND, border = 1)
  176. #
  177. # tools selection
  178. #
  179. bodySizer = wx.GridBagSizer(hgap = 5, vgap = 5)
  180. bodySizer.Add(item = self.ct_label, pos = (0, 0), span = (1, 2),
  181. flag = wx.ALL, border = 5)
  182. bodySizer.Add(item = self.ct_panel, pos = (1, 0), span = (1, 2))
  183. manageBoxSizer = wx.GridBagSizer(hgap = 10, vgap = 1)
  184. # start with row 1 for nicer layout
  185. manageBoxSizer.Add(item = self.btn_add, pos = (1, 0), border = 2, flag = wx.ALL | wx.EXPAND)
  186. manageBoxSizer.Add(item = self.btn_remove, pos = (2, 0), border = 2, flag = wx.ALL | wx.EXPAND)
  187. manageBoxSizer.Add(item = self.btn_moveup, pos = (3, 0), border = 2, flag = wx.ALL | wx.EXPAND)
  188. manageBoxSizer.Add(item = self.btn_movedown, pos = (4, 0), border = 2, flag = wx.ALL | wx.EXPAND)
  189. bodySizer.Add(item = manageBoxSizer, pos = (1, 2),
  190. flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 5)
  191. bodySizer.AddGrowableCol(2)
  192. #
  193. # standard buttons
  194. #
  195. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  196. btnSizer.Add(self.btn_close,
  197. flag = wx.LEFT | wx.RIGHT, border = 5)
  198. btnSizer.Add(self.btn_run,
  199. flag = wx.LEFT | wx.RIGHT, border = 5)
  200. btnSizer.Add(self.btn_clipboard,
  201. flag = wx.LEFT | wx.RIGHT, border = 5)
  202. btnSizer.Add(self.btn_help,
  203. flag = wx.LEFT | wx.RIGHT, border = 5)
  204. #
  205. # put it all together
  206. #
  207. sizer.Add(item = inSizer, proportion = 0,
  208. flag = wx.ALL | wx.EXPAND, border = 5)
  209. sizer.Add(item = self.ftypeSizer, proportion = 0,
  210. flag = wx.ALL | wx.EXPAND, border = 5)
  211. sizer.Add(item = outSizer, proportion = 0,
  212. flag = wx.ALL | wx.EXPAND, border = 5)
  213. sizer.Add(item = wx.StaticLine(parent = self, id = wx.ID_ANY,
  214. style = wx.LI_HORIZONTAL), proportion = 0,
  215. flag = wx.EXPAND | wx.ALL, border = 5)
  216. sizer.Add(item = bodySizer, proportion = 1,
  217. flag = wx.ALL | wx.EXPAND, border = 5)
  218. sizer.Add(item = wx.StaticLine(parent = self, id = wx.ID_ANY,
  219. style = wx.LI_HORIZONTAL), proportion = 0,
  220. flag = wx.EXPAND | wx.ALL, border = 5)
  221. sizer.Add(item = btnSizer, proportion = 0,
  222. flag = wx.ALL | wx.ALIGN_RIGHT, border = 5)
  223. self.panel.SetAutoLayout(True)
  224. self.panel.SetSizer(sizer)
  225. sizer.Fit(self.panel)
  226. self.Layout()
  227. def _toolsPanel(self):
  228. ct_panel = scrolled.ScrolledPanel(parent = self.panel, id = wx.ID_ANY,
  229. size = (500, 240),
  230. style = wx.SUNKEN_BORDER)
  231. self.ct_sizer = wx.GridBagSizer(vgap = 2, hgap = 4)
  232. ct_panel.SetSizer(self.ct_sizer)
  233. ct_panel.SetAutoLayout(True)
  234. return ct_panel
  235. def OnAddTool(self, event):
  236. """!Add tool button pressed"""
  237. self.AddTool()
  238. def AddTool(self):
  239. snum = len(self.toolslines.keys())
  240. num = snum + 1
  241. # tool number
  242. tool_no = wx.StaticText(parent = self.ct_panel, id = 3000+num,
  243. label = str(num)+'.')
  244. # tool
  245. tool_cbox = wx.ComboBox(parent = self.ct_panel, id = 1000+num,
  246. size = (300, -1), choices = self.tool_desc_list,
  247. style = wx.CB_DROPDOWN |
  248. wx.CB_READONLY | wx.TE_PROCESS_ENTER)
  249. self.Bind(wx.EVT_COMBOBOX, self.OnSetTool, tool_cbox)
  250. # threshold
  251. txt_ctrl = wx.TextCtrl(parent = self.ct_panel, id = 2000+num, value = '0.00',
  252. size = (100,-1),
  253. style = wx.TE_NOHIDESEL)
  254. self.Bind(wx.EVT_TEXT, self.OnThreshValue, txt_ctrl)
  255. # select
  256. select = wx.CheckBox(parent = self.ct_panel, id = num)
  257. select.SetValue(False)
  258. self.Bind(wx.EVT_CHECKBOX, self.OnSelect, select)
  259. # start with row 1 and col 1 for nicer layout
  260. self.ct_sizer.Add(item = tool_no, pos = (num, 1),
  261. flag = wx.ALIGN_CENTER_VERTICAL, border = 5)
  262. self.ct_sizer.Add(item = tool_cbox, pos = (num, 2),
  263. flag = wx.ALIGN_CENTER | wx.RIGHT, border = 5)
  264. self.ct_sizer.Add(item = txt_ctrl, pos = (num, 3),
  265. flag = wx.ALIGN_CENTER | wx.RIGHT, border = 5)
  266. self.ct_sizer.Add(item = select, pos = (num, 4),
  267. flag = wx.ALIGN_CENTER | wx.RIGHT)
  268. self.toolslines[num] = {
  269. 'tool_desc' : '' ,
  270. 'tool' : '' ,
  271. 'thresh' : '0.00' }
  272. self.ct_panel.Layout()
  273. self.ct_panel.SetupScrolling()
  274. def OnClearTool(self, event):
  275. """!Remove tool button pressed"""
  276. id = self.selected
  277. if id > 0:
  278. self.FindWindowById(id+1000).SetValue('')
  279. self.toolslines[id]['tool_desc'] = ''
  280. self.toolslines[id]['tool'] = ''
  281. self.SetStatusText(_("%s. cleaning tool removed, will be ignored") % id)
  282. else:
  283. self.SetStatusText(_("Please select a cleaning tool to remove"))
  284. def OnMoveToolUp(self, event):
  285. """!Move up tool button pressed"""
  286. id = self.selected
  287. if id > 1:
  288. id_up = id - 1
  289. this_toolline = self.toolslines[id]
  290. up_toolline = self.toolslines[id_up]
  291. self.FindWindowById(id_up).SetValue(True)
  292. self.FindWindowById(id_up+1000).SetValue(this_toolline['tool_desc'])
  293. self.FindWindowById(id_up+2000).SetValue(this_toolline['thresh'])
  294. self.toolslines[id_up] = this_toolline
  295. self.FindWindowById(id).SetValue(False)
  296. self.FindWindowById(id+1000).SetValue(up_toolline['tool_desc'])
  297. self.FindWindowById(id+2000).SetValue(up_toolline['thresh'])
  298. self.toolslines[id] = up_toolline
  299. self.selected = id_up
  300. self.SetStatusText(_("%s. cleaning tool moved up") % id)
  301. elif id == 1:
  302. self.SetStatusText(_("1. cleaning tool can not be moved up "))
  303. elif id == -1:
  304. self.SetStatusText(_("Please select a cleaning tool to move up"))
  305. def OnMoveToolDown(self, event):
  306. """!Move down tool button pressed"""
  307. id = self.selected
  308. snum = len(self.toolslines.keys())
  309. if id > 0 and id < snum:
  310. id_down = id + 1
  311. this_toolline = self.toolslines[id]
  312. down_toolline = self.toolslines[id_down]
  313. self.FindWindowById(id_down).SetValue(True)
  314. self.FindWindowById(id_down+1000).SetValue(this_toolline['tool_desc'])
  315. self.FindWindowById(id_down+2000).SetValue(this_toolline['thresh'])
  316. self.toolslines[id_down] = this_toolline
  317. self.FindWindowById(id).SetValue(False)
  318. self.FindWindowById(id+1000).SetValue(down_toolline['tool_desc'])
  319. self.FindWindowById(id+2000).SetValue(down_toolline['thresh'])
  320. self.toolslines[id] = down_toolline
  321. self.selected = id_down
  322. self.SetStatusText(_("%s. cleaning tool moved down") % id)
  323. elif id == snum:
  324. self.SetStatusText(_("Last cleaning tool can not be moved down "))
  325. elif id == -1:
  326. self.SetStatusText(_("Please select a cleaning tool to move down"))
  327. def OnSetTool(self, event):
  328. """!Tool was defined"""
  329. id = event.GetId()
  330. tool_no = id-1000
  331. num = self.FindWindowById(id).GetCurrentSelection()
  332. self.toolslines[tool_no]['tool_desc'] = self.tool_desc_list[num]
  333. self.toolslines[tool_no]['tool'] = self.tool_list[num]
  334. self.SetStatusText( str(tool_no) + '. ' + _("cleaning tool: '%s'") % (self.tool_list[num]))
  335. def OnThreshValue(self, event):
  336. """!Threshold value was entered"""
  337. id = event.GetId()
  338. num = id-2000
  339. self.toolslines[num]['thresh'] = self.FindWindowById(id).GetValue()
  340. self.SetStatusText(_("Threshold for %(num)s. tool '%(tool)s': %(thresh)s") % \
  341. { 'num' : num,
  342. 'tool' : self.toolslines[num]['tool'],
  343. 'thresh' : self.toolslines[num]['thresh'] })
  344. def OnSelect(self, event):
  345. """!Tool was selected"""
  346. id = event.GetId()
  347. if self.selected > -1 and self.selected != id:
  348. win = self.FindWindowById(self.selected)
  349. win.SetValue(False)
  350. if self.selected != id:
  351. self.selected = id
  352. else:
  353. self.selected = -1
  354. def OnDone(self, cmd, returncode):
  355. """!Command done"""
  356. self.SetStatusText('')
  357. def OnCleaningRun(self, event):
  358. """!Builds options and runs v.clean
  359. """
  360. self.GetCmdStrings()
  361. err = list()
  362. for p, name in ((self.inmap, _('Name of input vector map')),
  363. (self.outmap, _('Name for output vector map')),
  364. (self.tools_string, _('Tools')),
  365. (self.thresh_string, _('Threshold'))):
  366. if not p:
  367. err.append(_("'%s' not defined") % name)
  368. if err:
  369. GError(_("Some parameters not defined. Operation "
  370. "canceled.\n\n%s") % '\n'.join(err),
  371. parent = self)
  372. return
  373. self.SetStatusText(_("Executing selected cleaning operations..."))
  374. snum = len(self.toolslines.keys())
  375. if self.log:
  376. cmd = [ self.cmd,
  377. 'input=%s' % self.inmap,
  378. 'output=%s' % self.outmap,
  379. 'tool=%s' % self.tools_string,
  380. 'thres=%s' % self.thresh_string ]
  381. if self.ftype_string:
  382. cmd.append('type=%s' % self.ftype_string)
  383. if self.overwrite.IsChecked():
  384. cmd.append('--overwrite')
  385. self.log.RunCmd(cmd, onDone = self.OnDone)
  386. self.parent.Raise()
  387. else:
  388. if self.overwrite.IsChecked():
  389. overwrite = True
  390. else:
  391. overwrite = False
  392. RunCommand(self.cmd,
  393. input = self.inmap,
  394. output = self.outmap,
  395. type = self.ftype_string,
  396. tool = self.tools_string,
  397. thresh = self.thresh_string,
  398. overwrite = overwrite)
  399. def OnClose(self, event):
  400. self.Destroy()
  401. def OnHelp(self, event):
  402. """!Show GRASS manual page"""
  403. RunCommand('g.manual',
  404. quiet = True,
  405. parent = self,
  406. entry = self.cmd)
  407. def OnCopy(self, event):
  408. """!Copy the command"""
  409. cmddata = wx.TextDataObject()
  410. # get tool and thresh strings
  411. self.GetCmdStrings()
  412. cmdstring = '%s' % (self.cmd)
  413. # list -> string
  414. cmdstring += ' input=%s output=%s type=%s tool=%s thres=%s' % \
  415. (self.inmap, self.outmap, self.ftype_string, self.tools_string, self.thresh_string)
  416. if self.overwrite.IsChecked():
  417. cmdstring += ' --overwrite'
  418. cmddata.SetText(cmdstring)
  419. if wx.TheClipboard.Open():
  420. wx.TheClipboard.SetData(cmddata)
  421. wx.TheClipboard.Close()
  422. self.SetStatusText(_("Vector cleaning command copied to clipboard"))
  423. def GetCmdStrings(self):
  424. self.tools_string = ''
  425. self.thresh_string = ''
  426. self.ftype_string = ''
  427. # feature types
  428. first = 1
  429. for num in range(0, self.n_ftypes - 1):
  430. if self.ftype_check[num].IsChecked():
  431. if first:
  432. self.ftype_string = '%s' % self.ftype[num]
  433. first = 0
  434. else:
  435. self.ftype_string += ',%s' % self.ftype[num]
  436. # cleaning tools
  437. first = 1
  438. snum = len(self.toolslines.keys())
  439. for num in range(1, snum + 1):
  440. if self.toolslines[num]['tool']:
  441. if first:
  442. self.tools_string = '%s' % self.toolslines[num]['tool']
  443. self.thresh_string = '%s' % self.toolslines[num]['thresh']
  444. first = 0
  445. else:
  446. self.tools_string += ',%s' % self.toolslines[num]['tool']
  447. self.thresh_string += ',%s' % self.toolslines[num]['thresh']
  448. self.inmap = self.selectionInput.GetValue()
  449. self.outmap = self.selectionOutput.GetValue()