vdigit.py 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794
  1. """
  2. MODULE: vdigit
  3. CLASSES:
  4. * AbstractDigit
  5. * VEdit
  6. * VDigit
  7. * AbstractDisplayDriver
  8. * CDisplayDriver
  9. * VDigitSettingsDialog
  10. * VDigitCategoryDialog
  11. * VDigitZBulkDialog
  12. * VDigitDuplicatesDialog
  13. PURPOSE: Vector digitization tool for wxPython GUI
  14. Note: Initial version under development
  15. Progress:
  16. (1) v.edit called on the background (class VEdit)
  17. (2) Reimplentation of v.digit (VDigit)
  18. Import:
  19. from vdigit import VDigit as VDigit
  20. AUTHORS: The GRASS Development Team
  21. Martin Landa <landa.martin gmail.com>
  22. COPYRIGHT: (C) 2007-2008 by the GRASS Development Team
  23. This program is free software under the GNU General Public
  24. License (>=v2). Read the file COPYING that comes with GRASS
  25. for details.
  26. """
  27. import os
  28. import sys
  29. import string
  30. import copy
  31. import wx
  32. import wx.lib.colourselect as csel
  33. import wx.lib.mixins.listctrl as listmix
  34. import gcmd
  35. import dbm
  36. from debug import Debug as Debug
  37. import gselect
  38. import globalvar
  39. from preferences import globalSettings as UserSettings
  40. try:
  41. digitPath = os.path.join(globalvar.ETCWXDIR, "vdigit")
  42. sys.path.append(digitPath)
  43. import grass6_wxvdigit as wxvdigit
  44. GV_LINES = wxvdigit.GV_LINES
  45. digitErr = ''
  46. except ImportError, err:
  47. GV_LINES = None
  48. digitErr = err
  49. # print >> sys.stderr, "%sWARNING: Digitization tool is disabled (%s). " \
  50. # "Detailed information in README file." % \
  51. # (os.linesep, err)
  52. # which interface to use?
  53. if UserSettings.Get(group='advanced', key='digitInterface', subkey='type') == 'vedit' and GV_LINES is not None:
  54. print >> sys.stderr, "%sWARNING: Digitization tool uses v.edit interface. " \
  55. "This can significantly slow down some operations especially for " \
  56. "middle-large vector maps. "\
  57. "You can change the digitization interface in 'User preferences' " \
  58. "(menu 'Config'->'Preferences')." % \
  59. os.linesep
  60. class AbstractDigit:
  61. """
  62. Abstract digitization class
  63. """
  64. def __init__(self, mapwindow):
  65. """Initialization
  66. @param mapwindow reference to mapwindow (MapFrame) instance
  67. @param settings initial settings of digitization tool
  68. """
  69. self.map = None
  70. self.mapWindow = mapwindow
  71. Debug.msg (3, "AbstractDigit.__init__(): map=%s" % \
  72. self.map)
  73. #self.SetCategory()
  74. self.driver = CDisplayDriver(self, mapwindow)
  75. def SetCategoryNextToUse(self):
  76. """Find maximum category number in the map layer
  77. and update Digit.settings['category']
  78. @return 'True' on success, 'False' on failure
  79. """
  80. # vector map layer without categories, reset to '1'
  81. UserSettings.Set(group='vdigit', key='category', subkey='value', value=1)
  82. if self.map:
  83. if UserSettings.Get(group='advanced', key='digitInterface', subkey='type') == 'vedit':
  84. categoryCmd = gcmd.Command(cmd=["v.category", "-g", "--q",
  85. "input=%s" % self.map,
  86. "option=report"])
  87. if categoryCmd.returncode != 0:
  88. return False
  89. for line in categoryCmd.ReadStdOutput():
  90. if "all" in line:
  91. if UserSettings.Get(group='vdigit', key='layer', subkey='value') != int(line.split(' ')[0]):
  92. continue
  93. try:
  94. maxCat = int(line.split(' ')[-1]) + 1
  95. UserSettings.Set(group='vdigit', key='category', subkey='value', value=maxCat)
  96. except:
  97. return False
  98. return True
  99. else:
  100. cat = self.digit.GetCategory(UserSettings.Get(group='vdigit', key='layer', subkey='value'))
  101. UserSettings.Set(group='vdigit', key='category', subkey='value',
  102. value=cat + 1)
  103. def SetCategory(self):
  104. """Return category number to use (according Settings)"""
  105. if UserSettings.Get(group='vdigit', key="categoryMode", subkey='selection') == 0:
  106. self.SetCategoryNextToUse()
  107. return UserSettings.Get(group='vdigit', key="category", subkey='value')
  108. def SetMapName(self, map):
  109. """Set map name
  110. @param map map name to be set up or None (will close currently edited map)
  111. """
  112. Debug.msg (3, "AbstractDigit.SetMapName map=%s" % map)
  113. self.map = map
  114. try:
  115. ret = self.driver.Reset(self.map)
  116. except StandardError, e:
  117. raise gcmd.DigitError('Unable to initialize display driver, '
  118. 'see README file for more information.%s%s'
  119. 'Details: %s (%s)' % (os.linesep, os.linesep, e, digitErr))
  120. if map and ret == -1:
  121. raise gcmd.DigitError(_('Unable to open vector map <%s> for editing. '
  122. 'Data are probably corrupted, '
  123. 'try to run v.build for rebuilding the topology.') % map)
  124. if not map and ret != 0:
  125. raise gcmd.DigitError(_('Unable to open vector map <%s> for editing. '
  126. 'Data are probably corrupted, '
  127. 'try to run v.build for rebuilding the topology.') % map)
  128. if UserSettings.Get(group='advanced', key='digitInterface', subkey='type') != 'v.edit':
  129. try:
  130. self.digit.InitCats()
  131. except:
  132. pass
  133. # avoid using current vector map as background map
  134. if self.map == UserSettings.Get(group='vdigit', key='backgroundMap', subkey='value'):
  135. UserSettings.Set(group='vdigit', key='backgroundMap', subkey='value', value='')
  136. def SelectLinesByQueryThresh(self):
  137. """Generic method used for SelectLinesByQuery()
  138. -- to get threshold value"""
  139. thresh = 0.0
  140. if UserSettings.Get(group='vdigit', key='query', subkey='selection') == 0:
  141. thresh = UserSettings.Get(group='vdigit', key='queryLength', subkey='thresh')
  142. if UserSettings.Get(group='vdigit', key="queryLength", subkey='than-selection') == 0:
  143. thresh = -1 * thresh
  144. else:
  145. thresh = UserSettings.Get(group='vdigit', key='queryDangle', subkey='thresh')
  146. if UserSettings.Get(group='vdigit', key="queryDangle", subkey='than-selection') == 0:
  147. thresh = -1 * thresh
  148. return thresh
  149. def GetSelectType(self):
  150. """Get type(s) to be selected
  151. Used by SelectLinesByBox() and SelectLinesByPoint()"""
  152. type = 0
  153. for feature in (('Point', wxvdigit.GV_POINT),
  154. ('Line', wxvdigit.GV_LINE),
  155. ('Centroid', wxvdigit.GV_CENTROID),
  156. ('Boundary', wxvdigit.GV_BOUNDARY)):
  157. if UserSettings.Get(group='vdigit', key='selectFeature'+feature[0], subkey='enabled') is True:
  158. type |= feature[1]
  159. return type
  160. def SelectLinesFromBackgroundMap(self, pos1, pos2):
  161. """Select features from background map
  162. @param pos1,pos2 bounding box defifinition
  163. """
  164. if UserSettings.Get(group='vdigit', key='backgroundMap', subkey='value') == '':
  165. Debug.msg(4, "VEdit.SelectLinesFromBackgroundMap(): []")
  166. return []
  167. x1, y1 = pos1
  168. x2, y2 = pos2
  169. vEditCmd = gcmd.Command(['v.edit',
  170. '--q',
  171. 'map=%s' % UserSettings.Get(group='vdigit', key='backgroundMap', subkey='value'),
  172. 'tool=select',
  173. 'bbox=%f,%f,%f,%f' % (pos1[0], pos1[1], pos2[0], pos2[1])])
  174. #'polygon=%f,%f,%f,%f,%f,%f,%f,%f,%f,%f' % \
  175. # (x1, y1, x2, y1, x2, y2, x1, y2, x1, y1)])
  176. try:
  177. output = vEditCmd.ReadStdOutput()[0] # first line
  178. ids = output.split(',')
  179. ids = map(int, ids) # str -> int
  180. except:
  181. return []
  182. Debug.msg(4, "VEdit.SelectLinesFromBackgroundMap(): %s" % \
  183. ",".join(["%d" % v for v in ids]))
  184. return ids
  185. class VEdit(AbstractDigit):
  186. """
  187. Prototype of digitization class based on v.edit command
  188. Note: This should be replaced by VDigit class.
  189. """
  190. def __init__(self, mapwindow):
  191. """Initialization
  192. @param mapwindow reference to mapwindow (MapFrame) instance
  193. @param settings initial settings of digitization tool
  194. """
  195. AbstractDigit.__init__(self, mapwindow)
  196. def AddPoint (self, map, point, x, y, z=None):
  197. """Add point/centroid
  198. @param map map name
  199. @param point feature type (True for point, otherwise centroid)
  200. @param x,y,z coordinates
  201. """
  202. if point:
  203. key = "P"
  204. else:
  205. key = "C"
  206. if UserSettings.Get(group='vdigit', key="categoryMode", subkey='selection') == 2:
  207. layer = -1 # -> no category
  208. cat = -1
  209. else:
  210. layer = UserSettings.Get(group='vdigit', key="layer", subkey='value')
  211. cat = self.SetCategory()
  212. if layer > 0 and cat != "None":
  213. addstring = "%s 1 1\n" % (key)
  214. else:
  215. addstring = "%s 1\n" % (key)
  216. addstring += "%f %f\n" % (x, y)
  217. if layer > 0 and cat != "None":
  218. addstring += "%d %d\n" % (layer, cat)
  219. Debug.msg (3, "VEdit.AddPoint(): map=%s, type=%s, layer=%d, cat=%d, x=%f, y=%f" % \
  220. (map, type, layer, cat, x, y))
  221. else:
  222. Debug.msg (3, "VEdit.AddPoint(): map=%s, type=%s, x=%f, y=%f" % \
  223. (map, type, x, y))
  224. Debug.msg (4, "Vline.AddPoint(): input=%s" % addstring)
  225. self.__AddFeature (map=map, input=addstring)
  226. def AddLine (self, map, line, coords):
  227. """Add line/boundary
  228. @param map map name
  229. @param line feature type (True for line, otherwise boundary)
  230. @param list of coordinates
  231. """
  232. if len(coords) < 2:
  233. return
  234. if UserSettings.Get(group='vdigit', key="categoryMode", subkey='selection') == 2:
  235. layer = -1 # -> no category
  236. cat = -1
  237. else:
  238. layer = UserSettings.Get(group='vdigit', key="layer", subkey='value')
  239. cat = self.SetCategory()
  240. if line:
  241. key = "L"
  242. flags = []
  243. else:
  244. key = "B"
  245. flags = ['-c'] # close boundaries
  246. if layer > 0 and cat != "None":
  247. addstring = "%s %d 1\n" % (key, len(coords))
  248. else:
  249. addstring = "%s %d\n" % (key, len(coords))
  250. for point in coords:
  251. addstring += "%f %f\n" % \
  252. (float(point[0]), float(point [1]))
  253. if layer > 0 and cat != "None":
  254. addstring += "%d %d\n" % (layer, cat)
  255. Debug.msg (3, "Vline.AddLine(): type=%s, layer=%d, cat=%d coords=%s" % \
  256. (key, layer, cat, coords))
  257. else:
  258. Debug.msg (3, "Vline.AddLine(): type=%s, coords=%s" % \
  259. (key, coords))
  260. Debug.msg (4, "VEdit.AddLine(): input=%s" % addstring)
  261. self.__AddFeature (map=map, input=addstring, flags=flags)
  262. def __AddFeature (self, map, input, flags=[]):
  263. """Generic method to add new vector feature
  264. @param map map name
  265. @param input feature definition in GRASS ASCII format
  266. @param flags additional flags
  267. """
  268. if UserSettings.Get(group='vdigit', key='snapping', subkey='value') <= 0.0:
  269. snap = "no"
  270. else:
  271. if UserSettings.Get(group='vdigit', key='snapToVertex', subkey='enabled') is True:
  272. snap = "vertex"
  273. else:
  274. snap = "node"
  275. command = ["v.edit", "-n", "--q",
  276. "map=%s" % map,
  277. "tool=add",
  278. "thresh=%f,%f" % (self.driver.GetThreshold(type='selectThresh'), self.driver.GetThreshold(type='snapping')),
  279. "snap=%s" % snap]
  280. if UserSettings.Get(group='vdigit', key='backgroundMap', subkey='value') != '':
  281. command.append("bgmap=%s" % UserSettings.Get(group='vdigit', key='backgroundMap', subkey='value'))
  282. # additional flags
  283. for flag in flags:
  284. command.append(flag)
  285. # run the command
  286. Debug.msg(4, "VEdit.AddFeature(): input=%s" % input)
  287. vedit = gcmd.Command(cmd=command, stdin=input, stderr=None)
  288. # reload map (needed for v.edit)
  289. self.driver.ReloadMap()
  290. def DeleteSelectedLines(self):
  291. """Delete selected features"""
  292. selected = self.driver.GetSelected() # grassId
  293. if len(selected) <= 0:
  294. return False
  295. ids = ",".join(["%d" % v for v in selected])
  296. Debug.msg(4, "Digit.DeleteSelectedLines(): ids=%s" % \
  297. ids)
  298. # delete also attributes if requested
  299. if UserSettings.Get(group='vdigit', key='delRecord', subkey='enabled') is True:
  300. layerCommand = gcmd.Command(cmd=["v.db.connect",
  301. "-g", "--q",
  302. "map=%s" % self.map],
  303. rerr=None, stderr=None)
  304. if layerCommand.returncode == 0:
  305. layers = {}
  306. for line in layerCommand.ReadStdOutput():
  307. lineList = line.split(' ')
  308. layers[int(lineList[0])] = { "table" : lineList[1],
  309. "key" : lineList[2],
  310. "database" : lineList[3],
  311. "driver" : lineList[4] }
  312. for layer in layers.keys():
  313. printCats = gcmd.Command(['v.category',
  314. '--q',
  315. 'input=%s' % self.map,
  316. 'layer=%d' % layer,
  317. 'option=print',
  318. 'id=%s' % ids])
  319. sql = 'DELETE FROM %s WHERE' % layers[layer]['table']
  320. n_cats = 0
  321. for cat in printCats.ReadStdOutput():
  322. for c in cat.split('/'):
  323. sql += ' cat = %d or' % int(c)
  324. n_cats += 1
  325. sql = sql.rstrip(' or')
  326. if n_cats > 0:
  327. gcmd.Command(['db.execute',
  328. '--q',
  329. 'driver=%s' % layers[layer]['driver'],
  330. 'database=%s' % layers[layer]['database']],
  331. stdin=sql,
  332. rerr=None, stderr=None)
  333. command = [ "v.edit",
  334. "map=%s" % self.map,
  335. "tool=delete",
  336. "ids=%s" % ids]
  337. # run the command
  338. vedit = gcmd.Command(cmd=command, stderr=None)
  339. # reload map (needed for v.edit)
  340. self.driver.ReloadMap()
  341. return True
  342. def MoveSelectedLines(self, move):
  343. """Move selected features
  344. @param move X,Y direction
  345. """
  346. return self.__MoveFeature("move", None, move)
  347. def MoveSelectedVertex(self, coords, move):
  348. """Move selected vertex
  349. Feature geometry is changed.
  350. @param coords click coordinates
  351. @param move X,Y direction
  352. """
  353. return self.__MoveFeature("vertexmove", coords, move)
  354. def __MoveFeature(self, tool, coords, move):
  355. """Move selected vector feature (line, vertex)
  356. @param tool tool for v.edit
  357. @param coords click coordinates
  358. @param move direction (x, y)
  359. """
  360. selected = self.driver.GetSelected()
  361. if len(selected) <= 0:
  362. return False
  363. ids = ",".join(["%d" % v for v in selected])
  364. Debug.msg(4, "Digit.MoveSelectedLines(): ids=%s, move=%s" % \
  365. (ids, move))
  366. if UserSettings.Get(group='vdigit', key='snapping', subkey='value') <= 0.0:
  367. snap = "no"
  368. else:
  369. if UserSettings.Get(group='vdigit', key='snapToVertex', subkey='enabled') is True:
  370. snap = "vertex"
  371. else:
  372. snap = "node"
  373. command = ["v.edit", "--q",
  374. "map=%s" % self.map,
  375. "tool=%s" % tool,
  376. "ids=%s" % ids,
  377. "move=%f,%f" % (float(move[0]),float(move[1])),
  378. "thresh=%f,%f" % (self.driver.GetThreshold(type='selectThresh'), self.driver.GetThreshold(type='snapping')),
  379. "snap=%s" % snap]
  380. if tool == "vertexmove":
  381. command.append("coords=%f,%f" % (float(coords[0]), float(coords[1])))
  382. command.append("-1") # modify only first selected
  383. if UserSettings.Get(group='vdigit', key='backgroundMap', subkey='value') != '':
  384. command.append("bgmap=%s" % UserSettings.Get(group='vdigit', key='backgroundMap', subkey='value'))
  385. # run the command
  386. vedit = gcmd.Command(cmd=command, stderr=None)
  387. # reload map (needed for v.edit)
  388. self.driver.ReloadMap()
  389. return True
  390. def AddVertex(self, coords):
  391. """Add new vertex to the selected line/boundary on position 'coords'
  392. @param coords coordinates to add vertex
  393. """
  394. return self.__ModifyVertex(coords, "vertexadd")
  395. def RemoveVertex(self, coords):
  396. """Remove vertex from the selected line/boundary on position 'coords'
  397. @param coords coordinates to remove vertex
  398. """
  399. return self.__ModifyVertex(coords, "vertexdel")
  400. def __ModifyVertex(self, coords, action):
  401. """Generic method for vertex manipulation
  402. @param coords coordinates
  403. @param action operation to perform
  404. """
  405. try:
  406. line = self.driver.GetSelected()[0]
  407. except:
  408. return False
  409. command = ["v.edit", "--q",
  410. "map=%s" % self.map,
  411. "tool=%s" % action,
  412. "ids=%s" % line,
  413. "coords=%f,%f" % (float(coords[0]),float(coords[1])),
  414. "thresh=%f,%f" % (self.driver.GetThreshold(type='selectThresh'), self.driver.GetThreshold(type='snapping'))]
  415. # run the command
  416. vedit = gcmd.Command(cmd=command, stderr=None)
  417. # reload map (needed for v.edit)
  418. self.driver.ReloadMap()
  419. return True
  420. def SplitLine(self, coords):
  421. """Split selected line/boundary on position 'coords'
  422. @param coords coordinates to split line
  423. """
  424. try:
  425. line = self.driver.GetSelected()[0]
  426. except:
  427. return False
  428. command = ["v.edit", "--q",
  429. "map=%s" % self.map,
  430. "tool=break",
  431. "ids=%s" % line,
  432. "coords=%f,%f" % (float(coords[0]),float(coords[1])),
  433. "thresh=%f" % self.driver.GetThreshold(type='selectThresh')]
  434. # run the command
  435. vedit = gcmd.Command(cmd=command, stderr=None)
  436. # redraw map
  437. self.driver.ReloadMap()
  438. return True
  439. def EditLine(self, line, coords):
  440. """Edit existing line/boundary
  441. @param line id of line to be modified
  442. @param coords list of coordinates of modified line
  443. """
  444. # remove line
  445. vEditDelete = gcmd.Command(['v.edit',
  446. '--q',
  447. 'map=%s' % self.map,
  448. 'tool=delete',
  449. 'ids=%s' % line], stderr=None)
  450. # add line
  451. if len(coords) > 0:
  452. self.AddLine(self.map, "line", coords)
  453. # reload map (needed for v.edit)
  454. self.driver.ReloadMap()
  455. def __ModifyLines(self, tool):
  456. """Generic method to modify selected lines/boundaries
  457. @param tool operation to be performed by v.edit
  458. """
  459. ids = self.driver.GetSelected()
  460. if len(ids) <= 0:
  461. return False
  462. vEdit = ['v.edit',
  463. '--q',
  464. 'map=%s' % self.map,
  465. 'tool=%s' % tool,
  466. 'ids=%s' % ",".join(["%d" % v for v in ids])]
  467. if tool in ['snap', 'connect']:
  468. vEdit.append("thresh=%f,%f" % (self.driver.GetThreshold(type='selectThresh'), self.driver.GetThreshold(type='snapping')))
  469. runCmd = gcmd.Command(vEdit)
  470. # reload map (needed for v.edit)
  471. self.driver.ReloadMap()
  472. return True
  473. def FlipLine(self):
  474. """Flip selected lines/boundaries"""
  475. return self.__ModifyLines('flip')
  476. def MergeLine(self):
  477. """Merge selected lines/boundaries"""
  478. return self.__ModifyLines('merge')
  479. def BreakLine(self):
  480. """Break selected lines/boundaries"""
  481. return self.__ModifyLines('break')
  482. def SnapLine(self):
  483. """Snap selected lines/boundaries"""
  484. return self.__ModifyLines('snap')
  485. def ConnectLine(self):
  486. """Connect selected lines/boundaries"""
  487. return self.__ModifyLines('connect')
  488. def TypeConvForSelectedLines(self):
  489. """Feature type conversion for selected objects.
  490. Supported conversions:
  491. - point <-> centroid
  492. - line <-> boundary
  493. """
  494. return self.__ModifyLines('chtype')
  495. def ZBulkLine(self, pos1, pos2, value, step):
  496. """Provide z bulk-labeling (automated assigment of z coordinate
  497. to 3d lines
  498. @param pos1,pos2 bounding box definition for selecting lines to be labeled
  499. @param value starting value
  500. @param step step value
  501. """
  502. gcmd.Command(['v.edit',
  503. '--q',
  504. 'map=%s' % self.map,
  505. 'tool=zbulk',
  506. 'bbox=%f,%f,%f,%f' % (pos1[0], pos1[1], pos2[0], pos2[1]),
  507. 'zbulk=%f,%f' % (value, step)])
  508. def CopyLine(self, ids=None):
  509. """Copy features from (background) vector map
  510. @param ids list of line ids to be copied
  511. """
  512. if not ids:
  513. ids = self.driver.GetSelected()
  514. if len(ids) <= 0:
  515. return False
  516. vEdit = ['v.edit',
  517. '--q',
  518. 'map=%s' % self.map,
  519. 'tool=copy',
  520. 'ids=%s' % ",".join(["%d" % v for v in ids])]
  521. if UserSettings.Get(group='vdigit', key='backgroundMap', subkey='value') != '':
  522. vEdit.append('bgmap=%s' % UserSettings.Get(group='vdigit', key='backgroundMap', subkey='value'))
  523. runCmd = gcmd.Command(vEdit)
  524. # reload map (needed for v.edit)
  525. self.driver.ReloadMap()
  526. return True
  527. def CopyCats(self, cats, ids):
  528. """Copy given categories to objects with id listed in ids
  529. @param cats list of cats to be copied
  530. @param ids ids of lines to be modified
  531. """
  532. if len(cats) == 0 or len(ids) == 0:
  533. return False
  534. # collect cats
  535. gcmd.Command(['v.edit',
  536. '--q',
  537. 'map=%s' % self.map,
  538. 'tool=catadd',
  539. 'cats=%s' % ",".join(["%d" % v for v in cats]),
  540. 'ids=%s' % ",".join(["%d" % v for v in ids])])
  541. # reload map (needed for v.edit)
  542. self.driver.ReloadMap()
  543. return True
  544. def SelectLinesByQuery(self, pos1, pos2):
  545. """Select features by query
  546. @param pos1, pos2 bounding box definition
  547. """
  548. thresh = self.SelectLinesByQueryThresh()
  549. w, n = pos1
  550. e, s = pos2
  551. if UserSettings.Get(group='vdigit', key='query', subkey='box') == False: # select globaly
  552. vInfo = gcmd.Command(['v.info',
  553. 'map=%s' % self.map,
  554. '-g'])
  555. for item in vInfo.ReadStdOutput():
  556. if 'north' in item:
  557. n = float(item.split('=')[1])
  558. elif 'south' in item:
  559. s = float(item.split('=')[1])
  560. elif 'east' in item:
  561. e = float(item.split('=')[1])
  562. elif 'west' in item:
  563. w = float(item.split('=')[1])
  564. if UserSettings.Get(group='vdigit', key='query', subkey='selection') == 0:
  565. qtype = 'length'
  566. else:
  567. qtype = 'dangle'
  568. vEdit = (['v.edit',
  569. '--q',
  570. 'map=%s' % self.map,
  571. 'tool=select',
  572. 'bbox=%f,%f,%f,%f' % (w, n, e, s),
  573. 'query=%s' % qtype,
  574. 'thresh=0,0,%f' % thresh])
  575. vEditCmd = gcmd.Command(vEdit)
  576. try:
  577. output = vEditCmd.ReadStdOutput()[0] # first line
  578. ids = output.split(',')
  579. ids = map(int, ids) # str -> int
  580. except:
  581. return []
  582. Debug.msg(4, "VEdit.SelectLinesByQuery(): %s" % \
  583. ",".join(["%d" % v for v in ids]))
  584. return ids
  585. def GetLayers(self):
  586. """Return list of layers"""
  587. layerCommand = gcmd.Command(cmd=["v.db.connect",
  588. "-g", "--q",
  589. "map=%s" % self.map],
  590. rerr=None, stderr=None)
  591. if layerCommand.returncode == 0:
  592. layers = []
  593. for line in layerCommand.ReadStdOutput():
  594. lineList = line.split(' ')
  595. layers.append(int(lineList[0]))
  596. return layers
  597. return [1,]
  598. def Undo(self, level=-1):
  599. """Undo not implemented here"""
  600. wx.MessageBox(parent=self.mapWindow, message=_("Undondo is not implemented in vedit component. "
  601. "Use vdigit instead."),
  602. caption=_("Message"), style=wx.ID_OK | wx.ICON_INFORMATION | wx.CENTRE)
  603. class VDigit(AbstractDigit):
  604. """
  605. Prototype of digitization class based on v.digit reimplementation
  606. Under development (wxWidgets C/C++ background)
  607. """
  608. def __init__(self, mapwindow):
  609. """Initialization
  610. @param mapwindow reference to mapwindow (MapFrame) instance
  611. @param settings initial settings of digitization tool
  612. """
  613. AbstractDigit.__init__(self, mapwindow)
  614. try:
  615. self.digit = wxvdigit.Digit(self.driver.GetDevice())
  616. except (ImportError, NameError):
  617. self.digit = None
  618. self.toolbar = mapwindow.parent.digittoolbar
  619. def __del__(self):
  620. del self.digit
  621. def AddPoint (self, map, point, x, y, z=None):
  622. """Add new point/centroid
  623. @param map map name (unused, for compatability with VEdit)
  624. @param point feature type (if true point otherwise centroid)
  625. @param x,y,z coordinates
  626. """
  627. if UserSettings.Get(group='vdigit', key="categoryMode", subkey='selection') == 2:
  628. layer = -1 # -> no category
  629. cat = -1
  630. else:
  631. layer = UserSettings.Get(group='vdigit', key="layer", subkey='value')
  632. cat = self.SetCategory()
  633. if point:
  634. type = wxvdigit.GV_POINT
  635. else:
  636. type = wxvdigit.GV_CENTROID
  637. snap, thresh = self.__getSnapThreshold()
  638. if z:
  639. ret = self.digit.AddLine(type, [x, y, z], layer, cat,
  640. str(UserSettings.Get(group='vdigit', key="backgroundMap", subkey='value')), snap, thresh)
  641. else:
  642. ret = self.digit.AddLine(type, [x, y], layer, cat,
  643. str(UserSettings.Get(group='vdigit', key="backgroundMap", subkey='value')), snap, thresh)
  644. if ret == -1:
  645. raise gcmd.DigitError, _("Adding new feature to vector map <%s> failed.") % map
  646. self.toolbar.EnableUndo()
  647. def AddLine (self, map, line, coords):
  648. """Add line/boundary
  649. @param map map name (unused, for compatability with VEdit)
  650. @param line feature type (if True line, otherwise boundary)
  651. @param coords list of coordinates
  652. """
  653. if len(coords) < 2:
  654. return
  655. if UserSettings.Get(group='vdigit', key="categoryMode", subkey='selection') == 2:
  656. layer = -1 # -> no category
  657. cat = -1
  658. else:
  659. layer = UserSettings.Get(group='vdigit', key="layer", subkey='value')
  660. cat = self.SetCategory()
  661. if line:
  662. type = wxvdigit.GV_LINE
  663. else:
  664. type = wxvdigit.GV_BOUNDARY
  665. listCoords = []
  666. for c in coords:
  667. for x in c:
  668. listCoords.append(x)
  669. snap, thresh = self.__getSnapThreshold()
  670. ret = self.digit.AddLine(type, listCoords, layer, cat,
  671. str(UserSettings.Get(group='vdigit', key="backgroundMap", subkey='value')), snap, thresh)
  672. if ret == -1:
  673. raise gcmd.DigitError, _("Adding new feature to vector map <%s> failed.") % map
  674. self.toolbar.EnableUndo()
  675. def DeleteSelectedLines(self):
  676. """Delete selected features
  677. @return number of deleted lines
  678. """
  679. nlines = self.digit.DeleteLines(UserSettings.Get(group='vdigit', key='delRecord', subkey='enabled'))
  680. if nlines > 0:
  681. self.toolbar.EnableUndo()
  682. return nlines
  683. def MoveSelectedLines(self, move):
  684. """Move selected features
  685. @param move direction (x, y)
  686. """
  687. snap, thresh = self.__getSnapThreshold()
  688. nlines = self.digit.MoveLines(move[0], move[1], 0.0, # TODO 3D
  689. str(UserSettings.Get(group='vdigit', key="backgroundMap", subkey='value')), snap, thresh)
  690. if nlines > 0:
  691. self.toolbar.EnableUndo()
  692. return nlines
  693. def MoveSelectedVertex(self, coords, move):
  694. """Move selected vertex of the line
  695. @param coords click coordinates
  696. @param move X,Y direction
  697. @return 1 vertex moved
  698. @return 0 vertex not moved (not found, line is not selected)
  699. """
  700. snap, thresh = self.__getSnapThreshold()
  701. moved = self.digit.MoveVertex(coords[0], coords[1], 0.0, # TODO 3D
  702. move[0], move[1], 0.0,
  703. str(UserSettings.Get(group='vdigit', key="backgroundMap", subkey='value')), snap,
  704. self.driver.GetThreshold(type='selectThresh'), thresh)
  705. if moved:
  706. self.toolbar.EnableUndo()
  707. return moved
  708. def AddVertex(self, coords):
  709. """Add new vertex to the selected line/boundary on position 'coords'
  710. @param coords coordinates to add vertex
  711. @return 1 vertex added
  712. @return 0 nothing changed
  713. @return -1 on failure
  714. """
  715. added = self.digit.ModifyLineVertex(1, coords[0], coords[1], 0.0, # TODO 3D
  716. self.driver.GetThreshold(type='selectThresh'))
  717. if added > 0:
  718. self.toolbar.EnableUndo()
  719. return added
  720. def RemoveVertex(self, coords):
  721. """Remove vertex from the selected line/boundary on position 'coords'
  722. @param coords coordinates to remove vertex
  723. @return 1 vertex removed
  724. @return 0 nothing changed
  725. @return -1 on failure
  726. """
  727. deleted = self.digit.ModifyLineVertex(0, coords[0], coords[1], 0.0, # TODO 3D
  728. self.driver.GetThreshold(type='selectThresh'))
  729. if deleted > 0:
  730. self.toolbar.EnableUndo()
  731. return deleted
  732. def SplitLine(self, coords):
  733. """Split selected line/boundary on position 'coords'
  734. @param coords coordinates to split line
  735. @return 1 line modified
  736. @return 0 nothing changed
  737. @return -1 error
  738. """
  739. ret = self.digit.SplitLine(coords[0], coords[1], 0.0, # TODO 3D
  740. self.driver.GetThreshold('selectThresh'))
  741. if ret > 0:
  742. self.toolbar.EnableUndo()
  743. return ret
  744. def EditLine(self, line, coords):
  745. """Edit existing line/boundary
  746. @param line id of line to be modified
  747. @param coords list of coordinates of modified line
  748. @return feature id of new line
  749. @return -1 on error
  750. """
  751. try:
  752. lineid = line[0]
  753. except:
  754. lineid = -1
  755. listCoords = []
  756. for c in coords:
  757. for x in c:
  758. listCoords.append(x)
  759. snap, thresh = self.__getSnapThreshold()
  760. ret = self.digit.RewriteLine(lineid, listCoords,
  761. str(UserSettings.Get(group='vdigit', key="backgroundMap", subkey='value')), snap, thresh)
  762. if ret > 0:
  763. self.toolbar.EnableUndo()
  764. return ret
  765. def FlipLine(self):
  766. """Flip selected lines/boundaries
  767. @return number of modified lines
  768. @return -1 on error
  769. """
  770. ret = self.digit.FlipLines()
  771. if ret > 0:
  772. self.toolbar.EnableUndo()
  773. return ret
  774. def MergeLine(self):
  775. """Merge selected lines/boundaries
  776. @return number of modified lines
  777. @return -1 on error
  778. """
  779. ret = self.digit.MergeLines()
  780. if ret > 0:
  781. self.toolbar.EnableUndo()
  782. return ret
  783. def BreakLine(self):
  784. """Break selected lines/boundaries
  785. @return number of modified lines
  786. @return -1 on error
  787. """
  788. ret = self.digit.BreakLines()
  789. if ret > 0:
  790. self.toolbar.EnableUndo()
  791. return ret
  792. def SnapLine(self):
  793. """Snap selected lines/boundaries
  794. @return on success
  795. @return -1 on error
  796. """
  797. snap, thresh = self.__getSnapThreshold()
  798. ret = self.digit.SnapLines(thresh)
  799. if ret == 0:
  800. self.toolbar.EnableUndo()
  801. return ret
  802. def ConnectLine(self):
  803. """Connect selected lines/boundaries
  804. @return 1 lines connected
  805. @return 0 lines not connected
  806. @return -1 on error
  807. """
  808. snap, thresh = self.__getSnapThreshold()
  809. ret = self.digit.ConnectLines(thresh)
  810. if ret > 0:
  811. self.toolbar.EnableUndo()
  812. return ret
  813. def CopyLine(self, ids=None):
  814. """Copy features from (background) vector map
  815. @param ids list of line ids to be copied
  816. @return number of copied features
  817. @return -1 on error
  818. """
  819. ret = self.digit.CopyLines(ids, str(UserSettings.Get(group='vdigit', key='backgroundMap', subkey='value')))
  820. if ret > 0:
  821. self.toolbar.EnableUndo()
  822. return ret
  823. def CopyCats(self, cats, ids):
  824. """Copy given categories to objects with id listed in ids
  825. @param cats list of cats to be copied
  826. @param ids ids of lines to be modified
  827. @return number of modified features
  828. @return -1 on error
  829. """
  830. if len(cats) == 0 or len(ids) == 0:
  831. return 0
  832. ret = self.digit.CopyCats(cats, ids)
  833. if ret > 0:
  834. self.toolbar.EnableUndo()
  835. return ret
  836. def SelectLinesByQuery(self, pos1, pos2):
  837. """Select features by query
  838. @param pos1, pos2 bounding box definition
  839. """
  840. thresh = self.SelectLinesByQueryThresh()
  841. w, n = pos1
  842. e, s = pos2
  843. query = wxvdigit.QUERY_UNKNOWN
  844. if UserSettings.Get(group='vdigit', key='query', subkey='selection') == 0:
  845. query = wxvdigit.QUERY_LENGTH
  846. else:
  847. query = wxvdigit.QUERY_DANGLE
  848. type = wxvdigit.GV_POINTS | wxvdigit.GV_LINES # TODO: 3D
  849. ids = self.digit.SelectLinesByQuery(w, n, 0.0, e, s, 1000.0,
  850. UserSettings.Get(group='vdigit', key='query', subkey='box'),
  851. query, type, thresh)
  852. Debug.msg(4, "VDigit.SelectLinesByQuery(): %s" % \
  853. ",".join(["%d" % v for v in ids]))
  854. return ids
  855. def GetLineCats(self, line=-1):
  856. """Get layer/category pairs from given (selected) line
  857. @param line feature id (-1 for first selected line)
  858. """
  859. return self.digit.GetLineCats(line)
  860. def SetLineCats(self, line, layer, cats, add=True):
  861. """Set categories for given line and layer
  862. @param line feature id
  863. @param layer layer number (-1 for first selected line)
  864. @param cats list of categories
  865. @param add if True to add, otherwise do delete categories
  866. @return new feature id (feature need to be rewritten)
  867. @return -1 on error
  868. """
  869. ret = self.digit.SetLineCats(line, layer, cats, add)
  870. if ret > 0:
  871. self.toolbar.EnableUndo()
  872. return ret
  873. def GetLayers(self):
  874. """Get list of layers"""
  875. return self.digit.GetLayers()
  876. def TypeConvForSelectedLines(self):
  877. """Feature type conversion for selected objects.
  878. Supported conversions:
  879. - point <-> centroid
  880. - line <-> boundary
  881. @return number of modified features
  882. @return -1 on error
  883. """
  884. ret = self.digit.TypeConvLines()
  885. if ret > 0:
  886. self.toolbar.EnableUndo()
  887. return ret
  888. def Undo(self, level=-1):
  889. """Undo action
  890. @param level levels to undo (0 to revert all)
  891. @return id of current changeset
  892. """
  893. try:
  894. ret = self.digit.Undo(level)
  895. except SystemExit:
  896. ret = -2
  897. if ret == -2:
  898. raise gcmd.DigitError, _("Undo failed, data corrupted.")
  899. self.mapWindow.UpdateMap(render=False)
  900. if ret < 0: # disable undo tool
  901. self.toolbar.EnableUndo(False)
  902. def __getSnapThreshold(self):
  903. """Get snap mode and threshold value
  904. @return (snap, thresh)
  905. """
  906. thresh = self.driver.GetThreshold()
  907. if thresh > 0.0:
  908. if UserSettings.Get(group='vdigit', key='snapToVertex', subkey='enabled') is True:
  909. snap = wxvdigit.SNAPVERTEX
  910. else:
  911. snap = wxvdigit.SNAP
  912. else:
  913. snap = v.digit.NO_SNAP
  914. return (snap, thresh)
  915. if UserSettings.Get(group='advanced', key='digitInterface', subkey='type') == 'vedit':
  916. class Digit(VEdit):
  917. """Default digit class"""
  918. def __init__(self, mapwindow):
  919. VEdit.__init__(self, mapwindow)
  920. self.type = 'vedit'
  921. else:
  922. class Digit(VDigit):
  923. """Default digit class"""
  924. def __init__(self, mapwindow):
  925. VDigit.__init__(self, mapwindow)
  926. self.type = 'vdigit'
  927. def __del__(self):
  928. VDigit.__del__(self)
  929. class AbstractDisplayDriver:
  930. """Abstract classs for display driver"""
  931. def __init__(self, parent, mapwindow):
  932. """Initialization
  933. @param parent
  934. @param mapwindow reference to mapwindow (MFrame)
  935. """
  936. self.parent = parent
  937. self.mapwindow = mapwindow
  938. self.ids = {} # dict[g6id] = [pdcId]
  939. self.selected = [] # list of selected objects (grassId!)
  940. def GetThreshold(self, type='snapping', value=None, units=None):
  941. """Return threshold in map units
  942. @param value threshold to be set up
  943. @param units units (map, screen)
  944. """
  945. if not value:
  946. value = UserSettings.Get(group='vdigit', key=type, subkey='value')
  947. if not units:
  948. units = UserSettings.Get(group='vdigit', key=type, subkey='units')
  949. if units == "screen pixels":
  950. # pixel -> cell
  951. reg = self.mapwindow.Map.region
  952. if reg['nsres'] > reg['ewres']:
  953. res = reg['nsres']
  954. else:
  955. res = reg['ewres']
  956. threshold = value * res
  957. else:
  958. threshold = value
  959. Debug.msg(4, "AbstractDisplayDriver.GetThreshold(): type=%s, thresh=%f" % (type, threshold))
  960. return threshold
  961. class CDisplayDriver(AbstractDisplayDriver):
  962. """
  963. Display driver using grass6_wxdriver module
  964. """
  965. def __init__(self, parent, mapwindow):
  966. """Initialization
  967. @param parent
  968. @param mapwindow reference to mapwindow (MFrame)
  969. """
  970. AbstractDisplayDriver.__init__(self, parent, mapwindow)
  971. self.mapWindow = mapwindow
  972. # initialize wx display driver
  973. try:
  974. self.__display = wxvdigit.DisplayDriver(mapwindow.pdcVector)
  975. except:
  976. self.__display = None
  977. self.UpdateSettings()
  978. def GetDevice(self):
  979. """Get device"""
  980. return self.__display
  981. def SetDevice(self, pdc):
  982. """Set device for driver
  983. @param pdc wx.PseudoDC instance
  984. """
  985. self.__display.SetDevice(pdc)
  986. def Reset(self, map):
  987. """Reset map
  988. Open or close the vector map by driver.
  989. @param map map name or None to close the map
  990. @return 0 on success (close map)
  991. @return topo level on success (open map)
  992. @return non-zero (close map)
  993. @return -1 on error (open map)
  994. """
  995. if map:
  996. name, mapset = map.split('@')
  997. try:
  998. if UserSettings.Get(group='advanced', key='digitInterface', subkey='type') == 'vedit':
  999. ret = self.__display.OpenMap(str(name), str(mapset), False)
  1000. else:
  1001. ret = self.__display.OpenMap(str(name), str(mapset), True)
  1002. except SystemExit:
  1003. ret = -1
  1004. else:
  1005. ret = self.__display.CloseMap()
  1006. return ret
  1007. def ReloadMap(self):
  1008. """Reload map (close and re-open).
  1009. Needed for v.edit, TODO: get rid of that..."""
  1010. Debug.msg(4, "CDisplayDriver.ReloadMap():")
  1011. self.__display.ReloadMap()
  1012. def DrawMap(self):
  1013. """Draw vector map layer content
  1014. @return wx.Image instance
  1015. """
  1016. nlines = self.__display.DrawMap(True) # force
  1017. Debug.msg(3, "CDisplayDriver.DrawMap(): nlines=%d" % nlines)
  1018. return nlines
  1019. def SelectLinesByBox(self, begin, end, type=0):
  1020. """Select vector features by given bounding box.
  1021. If type is given, only vector features of given type are selected.
  1022. @param begin,end bounding box definition
  1023. @param type select only objects of given type
  1024. """
  1025. x1, y1 = begin
  1026. x2, y2 = end
  1027. nselected = self.__display.SelectLinesByBox(x1, y1, -1.0 * wxvdigit.PORT_DOUBLE_MAX,
  1028. x2, y2, wxvdigit.PORT_DOUBLE_MAX,
  1029. type)
  1030. Debug.msg(4, "CDisplayDriver.SelectLinesByBox(): selected=%d" % \
  1031. nselected)
  1032. return nselected
  1033. def SelectLineByPoint(self, point, type=0):
  1034. """Select vector feature by coordinates of click point (in given threshold).
  1035. If type is given, only vector features of given type are selected.
  1036. @param point click coordinates (bounding box given by threshold)
  1037. @param type select only objects of given type
  1038. """
  1039. pointOnLine = self.__display.SelectLineByPoint(point[0], point[1], 0.0,
  1040. self.GetThreshold(type='selectThresh'),
  1041. type, 0); # without_z
  1042. if len(pointOnLine) > 0:
  1043. Debug.msg(4, "CDisplayDriver.SelectLineByPoint(): pointOnLine=%f,%f" % \
  1044. (pointOnLine[0], pointOnLine[1]))
  1045. return pointOnLine
  1046. else:
  1047. Debug.msg(4, "CDisplayDriver.SelectLineByPoint(): no line found")
  1048. return None
  1049. def GetSelected(self, grassId=True):
  1050. """Return ids of selected vector features
  1051. @param grassId if grassId is True returns GRASS ids, otherwise
  1052. internal ids of objects drawn in PseudoDC"""
  1053. if grassId:
  1054. selected = self.__display.GetSelected(True)
  1055. else:
  1056. selected = self.__display.GetSelected(False)
  1057. Debug.msg(4, "CDisplayDriver.GetSelected(): grassId=%d, ids=%s" % \
  1058. (grassId, (",".join(["%d" % v for v in selected]))))
  1059. return selected
  1060. def GetDuplicates(self):
  1061. """Return ids of (selected) duplicated vector features
  1062. """
  1063. # -> id : (list of ids)
  1064. dupl = dict(self.__display.GetDuplicates())
  1065. vdigitComp = UserSettings.Get(group='advanced', key='digitInterface', subkey='type')
  1066. # -> id : ((id, cat), ...)
  1067. dupl_full = {}
  1068. for key in dupl.keys():
  1069. dupl_full[key] = []
  1070. for id in dupl[key]:
  1071. catStr = ''
  1072. # categories not supported for v.edit !
  1073. if vdigitComp == 'vdigit':
  1074. cats = self.parent.GetLineCats(line=id)
  1075. for layer in cats.keys():
  1076. if len(cats[layer]) > 0:
  1077. catStr = "%d: (" % layer
  1078. for cat in cats[layer]:
  1079. catStr += "%d," % cat
  1080. catStr = catStr.rstrip(',')
  1081. catStr += ')'
  1082. dupl_full[key].append([id, catStr])
  1083. return dupl_full
  1084. def GetSelectedVertex(self, coords):
  1085. """Get PseudoDC id(s) of vertex (of selected line)
  1086. on position 'coords'
  1087. @param coords click position
  1088. """
  1089. x, y = coords
  1090. id = self.__display.GetSelectedVertex(x, y, self.GetThreshold(type='selectThresh'))
  1091. Debug.msg(4, "CDisplayDriver.GetSelectedVertex(): id=%s" % \
  1092. (",".join(["%d" % v for v in id])))
  1093. return id
  1094. def SetSelected(self, id):
  1095. """Set selected vector features
  1096. @param id line id to be selected
  1097. """
  1098. Debug.msg(4, "CDisplayDriver.SetSelected(): id=%s" % \
  1099. ",".join(["%d" % v for v in id]))
  1100. self.__display.SetSelected(id)
  1101. def UnSelect(self, id):
  1102. """Unselect vector features
  1103. @param id list of feature id(s)
  1104. """
  1105. Debug.msg(4, "CDisplayDriver.UnSelect(): id=%s" % \
  1106. ",".join(["%d" % v for v in id]))
  1107. self.__display.UnSelect(id)
  1108. def UpdateRegion(self):
  1109. """Set geographical region
  1110. Needed for 'cell2pixel' conversion"""
  1111. map = self.mapwindow.Map
  1112. reg = map.region
  1113. self.__display.SetRegion(reg['n'],
  1114. reg['s'],
  1115. reg['e'],
  1116. reg['w'],
  1117. reg['nsres'],
  1118. reg['ewres'],
  1119. reg['center_easting'],
  1120. reg['center_northing'],
  1121. map.width, map.height)
  1122. def GetMapBoundingBox(self):
  1123. """Return bounding box of given vector map layer
  1124. @return (w,s,b,e,n,t)
  1125. """
  1126. return self.__display.GetMapBoundingBox()
  1127. def UpdateSettings(self):
  1128. """Update display driver settings"""
  1129. # TODO map units
  1130. if not self.__display:
  1131. return
  1132. self.__display.UpdateSettings (wx.Color(UserSettings.Get(group='vdigit', key='symbolHighlight', subkey='color')[0],
  1133. UserSettings.Get(group='vdigit', key='symbolHighlight', subkey='color')[1],
  1134. UserSettings.Get(group='vdigit', key='symbolHighlight', subkey='color')[2],
  1135. 255).GetRGB(),
  1136. UserSettings.Get(group='vdigit', key='checkForDupl', subkey='enabled'),
  1137. wx.Color(UserSettings.Get(group='vdigit', key='symbolHighlightDupl', subkey='color')[0],
  1138. UserSettings.Get(group='vdigit', key='symbolHighlightDupl', subkey='color')[1],
  1139. UserSettings.Get(group='vdigit', key='symbolHighlightDupl', subkey='color')[2],
  1140. 255).GetRGB(),
  1141. UserSettings.Get(group='vdigit', key='symbolPoint', subkey='enabled'),
  1142. wx.Color(UserSettings.Get(group='vdigit', key='symbolPoint', subkey='color')[0],
  1143. UserSettings.Get(group='vdigit', key='symbolPoint', subkey='color')[1],
  1144. UserSettings.Get(group='vdigit', key='symbolPoint', subkey='color')[2],
  1145. 255).GetRGB(),
  1146. UserSettings.Get(group='vdigit', key='symbolLine', subkey='enabled'),
  1147. wx.Color(UserSettings.Get(group='vdigit', key='symbolLine', subkey='color')[0],
  1148. UserSettings.Get(group='vdigit', key='symbolLine', subkey='color')[1],
  1149. UserSettings.Get(group='vdigit', key='symbolLine', subkey='color')[2],
  1150. 255).GetRGB(),
  1151. UserSettings.Get(group='vdigit', key='symbolBoundaryNo', subkey='enabled'),
  1152. wx.Color(UserSettings.Get(group='vdigit', key='symbolBoundaryNo', subkey='color')[0],
  1153. UserSettings.Get(group='vdigit', key='symbolBoundaryNo', subkey='color')[1],
  1154. UserSettings.Get(group='vdigit', key='symbolBoundaryNo', subkey='color')[2],
  1155. 255).GetRGB(),
  1156. UserSettings.Get(group='vdigit', key='symbolBoundaryOne', subkey='enabled'),
  1157. wx.Color(UserSettings.Get(group='vdigit', key='symbolBoundaryOne', subkey='color')[0],
  1158. UserSettings.Get(group='vdigit', key='symbolBoundaryOne', subkey='color')[1],
  1159. UserSettings.Get(group='vdigit', key='symbolBoundaryOne', subkey='color')[2],
  1160. 255).GetRGB(),
  1161. UserSettings.Get(group='vdigit', key='symbolBoundaryTwo', subkey='enabled'),
  1162. wx.Color(UserSettings.Get(group='vdigit', key='symbolBoundaryTwo', subkey='color')[0],
  1163. UserSettings.Get(group='vdigit', key='symbolBoundaryTwo', subkey='color')[1],
  1164. UserSettings.Get(group='vdigit', key='symbolBoundaryTwo', subkey='color')[2],
  1165. 255).GetRGB(),
  1166. UserSettings.Get(group='vdigit', key='symbolCentroidIn', subkey='enabled'),
  1167. wx.Color(UserSettings.Get(group='vdigit', key='symbolCentroidIn', subkey='color')[0],
  1168. UserSettings.Get(group='vdigit', key='symbolCentroidIn', subkey='color')[1],
  1169. UserSettings.Get(group='vdigit', key='symbolCentroidIn', subkey='color')[2],
  1170. 255).GetRGB(),
  1171. UserSettings.Get(group='vdigit', key='symbolCentroidOut', subkey='enabled'),
  1172. wx.Color(UserSettings.Get(group='vdigit', key='symbolCentroidOut', subkey='color')[0],
  1173. UserSettings.Get(group='vdigit', key='symbolCentroidOut', subkey='color')[1],
  1174. UserSettings.Get(group='vdigit', key='symbolCentroidOut', subkey='color')[2],
  1175. 255).GetRGB(),
  1176. UserSettings.Get(group='vdigit', key='symbolCentroidDup', subkey='enabled'),
  1177. wx.Color(UserSettings.Get(group='vdigit', key='symbolCentroidDup', subkey='color')[0],
  1178. UserSettings.Get(group='vdigit', key='symbolCentroidDup', subkey='color')[1],
  1179. UserSettings.Get(group='vdigit', key='symbolCentroidDup', subkey='color')[2],
  1180. 255).GetRGB(),
  1181. UserSettings.Get(group='vdigit', key='symbolNodeOne', subkey='enabled'),
  1182. wx.Color(UserSettings.Get(group='vdigit', key='symbolNodeOne', subkey='color')[0],
  1183. UserSettings.Get(group='vdigit', key='symbolNodeOne', subkey='color')[1],
  1184. UserSettings.Get(group='vdigit', key='symbolNodeOne', subkey='color')[2],
  1185. 255).GetRGB(),
  1186. UserSettings.Get(group='vdigit', key='symbolNodeTwo', subkey='enabled'),
  1187. wx.Color(UserSettings.Get(group='vdigit', key='symbolNodeTwo', subkey='color')[0],
  1188. UserSettings.Get(group='vdigit', key='symbolNodeTwo', subkey='color')[1],
  1189. UserSettings.Get(group='vdigit', key='symbolNodeTwo', subkey='color')[2],
  1190. 255).GetRGB(),
  1191. UserSettings.Get(group='vdigit', key='symbolVertex', subkey='enabled'),
  1192. wx.Color(UserSettings.Get(group='vdigit', key='symbolVertex', subkey='color')[0],
  1193. UserSettings.Get(group='vdigit', key='symbolVertex', subkey='color')[1],
  1194. UserSettings.Get(group='vdigit', key='symbolVertex', subkey='color')[2],
  1195. 255).GetRGB(),
  1196. UserSettings.Get(group='vdigit', key='lineWidth', subkey='value'))
  1197. class VDigitSettingsDialog(wx.Dialog):
  1198. """
  1199. Standard settings dialog for digitization purposes
  1200. """
  1201. def __init__(self, parent, title, style=wx.DEFAULT_DIALOG_STYLE):
  1202. wx.Dialog.__init__(self, parent=parent, id=wx.ID_ANY, title=title, style=style)
  1203. self.parent = parent # mapdisplay.BufferedWindow class instance
  1204. # notebook
  1205. notebook = wx.Notebook(parent=self, id=wx.ID_ANY, style=wx.BK_DEFAULT)
  1206. self.__CreateSymbologyPage(notebook)
  1207. parent.digit.SetCategory() # update category number (next to use)
  1208. self.__CreateGeneralPage(notebook)
  1209. self.__CreateAttributesPage(notebook)
  1210. self.__CreateQueryPage(notebook)
  1211. # buttons
  1212. btnApply = wx.Button(self, wx.ID_APPLY)
  1213. btnCancel = wx.Button(self, wx.ID_CANCEL)
  1214. btnSave = wx.Button(self, wx.ID_SAVE)
  1215. btnSave.SetDefault()
  1216. # bindigs
  1217. btnApply.Bind(wx.EVT_BUTTON, self.OnApply)
  1218. btnApply.SetToolTipString(_("Apply changes for this session"))
  1219. btnApply.SetDefault()
  1220. btnSave.Bind(wx.EVT_BUTTON, self.OnSave)
  1221. btnSave.SetToolTipString(_("Close dialog and save changes to user settings file"))
  1222. btnCancel.Bind(wx.EVT_BUTTON, self.OnCancel)
  1223. btnCancel.SetToolTipString(_("Close dialog and ignore changes"))
  1224. # sizers
  1225. btnSizer = wx.StdDialogButtonSizer()
  1226. btnSizer.AddButton(btnCancel)
  1227. btnSizer.AddButton(btnApply)
  1228. btnSizer.AddButton(btnSave)
  1229. btnSizer.Realize()
  1230. mainSizer = wx.BoxSizer(wx.VERTICAL)
  1231. mainSizer.Add(item=notebook, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  1232. mainSizer.Add(item=btnSizer, proportion=0,
  1233. flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
  1234. self.SetSizer(mainSizer)
  1235. mainSizer.Fit(self)
  1236. def __CreateSymbologyPage(self, notebook):
  1237. """Create notebook page concerning with symbology settings"""
  1238. panel = wx.Panel(parent=notebook, id=wx.ID_ANY)
  1239. notebook.AddPage(page=panel, text=_("Symbology"))
  1240. sizer = wx.BoxSizer(wx.VERTICAL)
  1241. flexSizer = wx.FlexGridSizer (cols=3, hgap=5, vgap=5)
  1242. flexSizer.AddGrowableCol(0)
  1243. self.symbology = {}
  1244. for label, key in self.__SymbologyData():
  1245. textLabel = wx.StaticText(panel, wx.ID_ANY, label)
  1246. color = csel.ColourSelect(panel, id=wx.ID_ANY,
  1247. colour=UserSettings.Get(group='vdigit', key=key, subkey='color'), size=(25, 25))
  1248. isEnabled = UserSettings.Get(group='vdigit', key=key, subkey='enabled')
  1249. if isEnabled is not None:
  1250. enabled = wx.CheckBox(panel, id=wx.ID_ANY, label="")
  1251. enabled.SetValue(isEnabled)
  1252. self.symbology[key] = (enabled, color)
  1253. else:
  1254. enabled = (1, 1)
  1255. self.symbology[key] = (None, color)
  1256. flexSizer.Add(textLabel, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL)
  1257. flexSizer.Add(enabled, proportion=0, flag=wx.ALIGN_CENTER | wx.FIXED_MINSIZE)
  1258. flexSizer.Add(color, proportion=0, flag=wx.ALIGN_RIGHT | wx.FIXED_MINSIZE)
  1259. sizer.Add(item=flexSizer, proportion=1, flag=wx.ALL | wx.EXPAND, border=10)
  1260. panel.SetSizer(sizer)
  1261. return panel
  1262. def __CreateGeneralPage(self, notebook):
  1263. """Create notebook page concerning with symbology settings"""
  1264. panel = wx.Panel(parent=notebook, id=wx.ID_ANY)
  1265. notebook.AddPage(page=panel, text=_("General"))
  1266. border = wx.BoxSizer(wx.VERTICAL)
  1267. #
  1268. # display section
  1269. #
  1270. box = wx.StaticBox (parent=panel, id=wx.ID_ANY, label=" %s " % _("Display"))
  1271. sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  1272. flexSizer = wx.FlexGridSizer (cols=3, hgap=5, vgap=5)
  1273. flexSizer.AddGrowableCol(0)
  1274. # line width
  1275. text = wx.StaticText(parent=panel, id=wx.ID_ANY, label=_("Line width"))
  1276. self.lineWidthValue = wx.SpinCtrl(parent=panel, id=wx.ID_ANY, size=(75, -1),
  1277. initial=UserSettings.Get(group='vdigit', key="lineWidth", subkey='value'),
  1278. min=1, max=1e6)
  1279. units = wx.StaticText(parent=panel, id=wx.ID_ANY, size=(115, -1),
  1280. label=UserSettings.Get(group='vdigit', key="lineWidth", subkey='units'),
  1281. style=wx.ALIGN_LEFT)
  1282. flexSizer.Add(text, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL)
  1283. flexSizer.Add(self.lineWidthValue, proportion=0, flag=wx.ALIGN_CENTER | wx.FIXED_MINSIZE)
  1284. flexSizer.Add(units, proportion=0, flag=wx.ALIGN_RIGHT | wx.FIXED_MINSIZE | wx.ALIGN_CENTER_VERTICAL | wx.LEFT,
  1285. border=10)
  1286. sizer.Add(item=flexSizer, proportion=1, flag=wx.ALL | wx.EXPAND, border=1)
  1287. border.Add(item=sizer, proportion=0, flag=wx.ALL | wx.EXPAND, border=5)
  1288. #
  1289. # snapping section
  1290. #
  1291. box = wx.StaticBox (parent=panel, id=wx.ID_ANY, label=" %s " % _("Snapping"))
  1292. sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  1293. flexSizer1 = wx.FlexGridSizer (cols=3, hgap=5, vgap=5)
  1294. flexSizer1.AddGrowableCol(0)
  1295. flexSizer2 = wx.FlexGridSizer (cols=2, hgap=5, vgap=5)
  1296. flexSizer2.AddGrowableCol(0)
  1297. # snapping
  1298. text = wx.StaticText(parent=panel, id=wx.ID_ANY, label=_("Snapping threshold"))
  1299. self.snappingValue = wx.SpinCtrl(parent=panel, id=wx.ID_ANY, size=(75, -1),
  1300. initial=UserSettings.Get(group='vdigit', key="snapping", subkey='value'),
  1301. min=1, max=1e6)
  1302. self.snappingValue.Bind(wx.EVT_SPINCTRL, self.OnChangeSnappingValue)
  1303. self.snappingUnit = wx.Choice(parent=panel, id=wx.ID_ANY, size=(125, -1),
  1304. choices=["screen pixels", "map units"])
  1305. self.snappingUnit.SetStringSelection(UserSettings.Get(group='vdigit', key="snapping", subkey='units'))
  1306. self.snappingUnit.Bind(wx.EVT_CHOICE, self.OnChangeSnappingUnits)
  1307. flexSizer1.Add(text, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL)
  1308. flexSizer1.Add(self.snappingValue, proportion=0, flag=wx.ALIGN_CENTER | wx.FIXED_MINSIZE)
  1309. flexSizer1.Add(self.snappingUnit, proportion=0, flag=wx.ALIGN_RIGHT | wx.FIXED_MINSIZE)
  1310. # background map
  1311. text = wx.StaticText(parent=panel, id=wx.ID_ANY, label=_("Background vector map"))
  1312. self.backgroundMap = gselect.Select(parent=panel, id=wx.ID_ANY, size=(200,-1),
  1313. type="vector", exceptOf=[self.parent.digit.map])
  1314. self.backgroundMap.SetValue(UserSettings.Get(group='vdigit', key="backgroundMap", subkey='value'))
  1315. self.backgroundMap.Bind(wx.EVT_TEXT, self.OnChangeBackgroundMap)
  1316. flexSizer2.Add(text, proportion=1, flag=wx.ALIGN_CENTER_VERTICAL)
  1317. flexSizer2.Add(self.backgroundMap, proportion=1, flag=wx.ALIGN_CENTER | wx.FIXED_MINSIZE)
  1318. #flexSizer.Add(self.snappingUnit, proportion=0, flag=wx.ALIGN_RIGHT | wx.FIXED_MINSIZE)
  1319. vertexSizer = wx.BoxSizer(wx.VERTICAL)
  1320. self.snapVertex = wx.CheckBox(parent=panel, id=wx.ID_ANY,
  1321. label=_("Snap also to vertex"))
  1322. self.snapVertex.SetValue(UserSettings.Get(group='vdigit', key="snapToVertex", subkey='enabled'))
  1323. vertexSizer.Add(item=self.snapVertex, proportion=0, flag=wx.EXPAND)
  1324. self.mapUnits = self.parent.MapWindow.Map.ProjInfo()['units']
  1325. self.snappingInfo = wx.StaticText(parent=panel, id=wx.ID_ANY,
  1326. label=_("Snapping threshold is %(value).1f %(units)s") % \
  1327. {'value' : self.parent.digit.driver.GetThreshold(),
  1328. 'units' : self.mapUnits})
  1329. vertexSizer.Add(item=self.snappingInfo, proportion=0,
  1330. flag=wx.ALL | wx.EXPAND, border=1)
  1331. sizer.Add(item=flexSizer1, proportion=1, flag=wx.TOP | wx.LEFT | wx.EXPAND, border=1)
  1332. sizer.Add(item=flexSizer2, proportion=1, flag=wx.TOP | wx.LEFT | wx.EXPAND, border=1)
  1333. sizer.Add(item=vertexSizer, proportion=1, flag=wx.BOTTOM | wx.LEFT | wx.EXPAND, border=1)
  1334. border.Add(item=sizer, proportion=0, flag=wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, border=5)
  1335. #
  1336. # select box
  1337. #
  1338. box = wx.StaticBox (parent=panel, id=wx.ID_ANY, label=" %s " % _("Select vector features"))
  1339. # feature type
  1340. sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  1341. inSizer = wx.BoxSizer(wx.HORIZONTAL)
  1342. self.selectFeature = {}
  1343. for feature in ('Point', 'Line',
  1344. 'Centroid', 'Boundary'):
  1345. chkbox = wx.CheckBox(parent=panel, label=feature)
  1346. self.selectFeature[feature] = chkbox.GetId()
  1347. chkbox.SetValue(UserSettings.Get(group='vdigit', key='selectFeature'+feature, subkey='enabled'))
  1348. inSizer.Add(item=chkbox, proportion=0,
  1349. flag=wx.EXPAND | wx.ALL, border=5)
  1350. sizer.Add(item=inSizer, proportion=0, flag=wx.EXPAND)
  1351. # threshold
  1352. flexSizer = wx.FlexGridSizer (cols=3, hgap=5, vgap=5)
  1353. flexSizer.AddGrowableCol(0)
  1354. text = wx.StaticText(parent=panel, id=wx.ID_ANY, label=_("Select threshold"))
  1355. self.selectThreshValue = wx.SpinCtrl(parent=panel, id=wx.ID_ANY, size=(75, -1),
  1356. initial=UserSettings.Get(group='vdigit', key="selectThresh", subkey='value'),
  1357. min=1, max=1e6)
  1358. units = wx.StaticText(parent=panel, id=wx.ID_ANY, size=(115, -1),
  1359. label=UserSettings.Get(group='vdigit', key="lineWidth", subkey='units'),
  1360. style=wx.ALIGN_LEFT)
  1361. flexSizer.Add(text, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL)
  1362. flexSizer.Add(self.selectThreshValue, proportion=0, flag=wx.ALIGN_CENTER | wx.FIXED_MINSIZE)
  1363. flexSizer.Add(units, proportion=0, flag=wx.ALIGN_RIGHT | wx.FIXED_MINSIZE | wx.ALIGN_CENTER_VERTICAL | wx.LEFT,
  1364. border=10)
  1365. self.checkForDupl = wx.CheckBox(parent=panel, id=wx.ID_ANY,
  1366. label=_("Check for duplicates"))
  1367. self.checkForDupl.SetValue(UserSettings.Get(group='vdigit', key="checkForDupl", subkey='enabled'))
  1368. flexSizer.Add(item=self.checkForDupl, proportion=0, flag=wx.EXPAND)
  1369. sizer.Add(item=flexSizer, proportion=0, flag=wx.EXPAND)
  1370. border.Add(item=sizer, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5)
  1371. #
  1372. # save-on-exit box
  1373. #
  1374. box = wx.StaticBox (parent=panel, id=wx.ID_ANY, label=" %s " % _("Save changes"))
  1375. # save changes on exit?
  1376. sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  1377. self.save = wx.CheckBox(parent=panel, label=_("Save changes on exit automatically"))
  1378. self.save.SetValue(UserSettings.Get(group='vdigit', key='saveOnExit', subkey='enabled'))
  1379. sizer.Add(item=self.save, proportion=0, flag=wx.ALL | wx.EXPAND, border=1)
  1380. border.Add(item=sizer, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5)
  1381. panel.SetSizer(border)
  1382. return panel
  1383. def __CreateQueryPage(self, notebook):
  1384. """Create notebook page for query tool"""
  1385. panel = wx.Panel(parent=notebook, id=wx.ID_ANY)
  1386. notebook.AddPage(page=panel, text=_("Query tool"))
  1387. border = wx.BoxSizer(wx.VERTICAL)
  1388. #
  1389. # query tool box
  1390. #
  1391. box = wx.StaticBox (parent=panel, id=wx.ID_ANY, label=" %s " % _("Choose query tool"))
  1392. sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  1393. LocUnits = self.parent.MapWindow.Map.ProjInfo()['units']
  1394. self.queryBox = wx.CheckBox(parent=panel, id=wx.ID_ANY, label=_("Select by box"))
  1395. self.queryBox.SetValue(UserSettings.Get(group='vdigit', key="query", subkey='box'))
  1396. sizer.Add(item=self.queryBox, proportion=0, flag=wx.ALL | wx.EXPAND, border=1)
  1397. sizer.Add((0, 5))
  1398. #
  1399. # length
  1400. #
  1401. self.queryLength = wx.RadioButton(parent=panel, id=wx.ID_ANY, label=_("length"))
  1402. self.queryLength.Bind(wx.EVT_RADIOBUTTON, self.OnChangeQuery)
  1403. sizer.Add(item=self.queryLength, proportion=0, flag=wx.ALL | wx.EXPAND, border=1)
  1404. flexSizer = wx.FlexGridSizer (cols=4, hgap=5, vgap=5)
  1405. flexSizer.AddGrowableCol(0)
  1406. txt = wx.StaticText(parent=panel, id=wx.ID_ANY, label=_("Select lines"))
  1407. self.queryLengthSL = wx.Choice (parent=panel, id=wx.ID_ANY,
  1408. choices = [_("shorter than"), _("longer than")])
  1409. self.queryLengthSL.SetSelection(UserSettings.Get(group='vdigit', key="queryLength", subkey='than-selection'))
  1410. self.queryLengthValue = wx.SpinCtrl(parent=panel, id=wx.ID_ANY, size=(100, -1),
  1411. initial=1,
  1412. min=0, max=1e6)
  1413. self.queryLengthValue.SetValue(UserSettings.Get(group='vdigit', key="queryLength", subkey='thresh'))
  1414. units = wx.StaticText(parent=panel, id=wx.ID_ANY, label="%s" % LocUnits)
  1415. flexSizer.Add(txt, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL)
  1416. flexSizer.Add(self.queryLengthSL, proportion=0, flag=wx.ALIGN_CENTER | wx.FIXED_MINSIZE)
  1417. flexSizer.Add(self.queryLengthValue, proportion=0, flag=wx.ALIGN_CENTER | wx.FIXED_MINSIZE)
  1418. flexSizer.Add(units, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL)
  1419. sizer.Add(item=flexSizer, proportion=0, flag=wx.ALL | wx.EXPAND, border=1)
  1420. #
  1421. # dangle
  1422. #
  1423. self.queryDangle = wx.RadioButton(parent=panel, id=wx.ID_ANY, label=_("dangle"))
  1424. self.queryDangle.Bind(wx.EVT_RADIOBUTTON, self.OnChangeQuery)
  1425. sizer.Add(item=self.queryDangle, proportion=0, flag=wx.ALL | wx.EXPAND, border=1)
  1426. flexSizer = wx.FlexGridSizer (cols=4, hgap=5, vgap=5)
  1427. flexSizer.AddGrowableCol(0)
  1428. txt = wx.StaticText(parent=panel, id=wx.ID_ANY, label=_("Select dangles"))
  1429. self.queryDangleSL = wx.Choice (parent=panel, id=wx.ID_ANY,
  1430. choices = [_("shorter than"), _("longer than")])
  1431. self.queryDangleSL.SetSelection(UserSettings.Get(group='vdigit', key="queryDangle", subkey='than-selection'))
  1432. self.queryDangleValue = wx.SpinCtrl(parent=panel, id=wx.ID_ANY, size=(100, -1),
  1433. initial=1,
  1434. min=0, max=1e6)
  1435. self.queryDangleValue.SetValue(UserSettings.Get(group='vdigit', key="queryDangle", subkey='thresh'))
  1436. units = wx.StaticText(parent=panel, id=wx.ID_ANY, label="%s" % LocUnits)
  1437. flexSizer.Add(txt, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL)
  1438. flexSizer.Add(self.queryDangleSL, proportion=0, flag=wx.ALIGN_CENTER | wx.FIXED_MINSIZE)
  1439. flexSizer.Add(self.queryDangleValue, proportion=0, flag=wx.ALIGN_CENTER | wx.FIXED_MINSIZE)
  1440. flexSizer.Add(units, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL)
  1441. sizer.Add(item=flexSizer, proportion=0, flag=wx.ALL | wx.EXPAND, border=1)
  1442. if UserSettings.Get(group='vdigit', key="query", subkey='selection') == 0:
  1443. self.queryLength.SetValue(True)
  1444. else:
  1445. self.queryDangle.SetValue(True)
  1446. # enable & disable items
  1447. self.OnChangeQuery(None)
  1448. border.Add(item=sizer, proportion=0, flag=wx.ALL | wx.EXPAND, border=5)
  1449. panel.SetSizer(border)
  1450. return panel
  1451. def __CreateAttributesPage(self, notebook):
  1452. """Create notebook page for query tool"""
  1453. panel = wx.Panel(parent=notebook, id=wx.ID_ANY)
  1454. notebook.AddPage(page=panel, text=_("Attributes"))
  1455. border = wx.BoxSizer(wx.VERTICAL)
  1456. #
  1457. # add new record
  1458. #
  1459. box = wx.StaticBox (parent=panel, id=wx.ID_ANY, label=" %s " % _("Digitize new feature"))
  1460. sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  1461. # checkbox
  1462. self.addRecord = wx.CheckBox(parent=panel, id=wx.ID_ANY,
  1463. label=_("Add new record into table"))
  1464. self.addRecord.SetValue(UserSettings.Get(group='vdigit', key="addRecord", subkey='enabled'))
  1465. sizer.Add(item=self.addRecord, proportion=0, flag=wx.ALL | wx.EXPAND, border=1)
  1466. # settings
  1467. flexSizer = wx.FlexGridSizer(cols=2, hgap=3, vgap=3)
  1468. flexSizer.AddGrowableCol(0)
  1469. settings = ((_("Layer"), 1), (_("Category"), 1), (_("Mode"), _("Next to use")))
  1470. # layer
  1471. text = wx.StaticText(parent=panel, id=wx.ID_ANY, label=_("Layer"))
  1472. if self.parent.digit.map:
  1473. layers = map(str, self.parent.digit.GetLayers())
  1474. if len(layers) == 0:
  1475. layers = [str(UserSettings.Get(group='vdigit', key="layer", subkey='value')), ]
  1476. else:
  1477. layers = [str(UserSettings.Get(group='vdigit', key="layer", subkey='value')), ]
  1478. self.layer = wx.Choice(parent=panel, id=wx.ID_ANY, size=(125, -1),
  1479. choices=layers)
  1480. self.layer.SetStringSelection(str(UserSettings.Get(group='vdigit', key="layer", subkey='value')))
  1481. flexSizer.Add(item=text, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL)
  1482. flexSizer.Add(item=self.layer, proportion=0,
  1483. flag=wx.FIXED_MINSIZE | wx.ALIGN_CENTER_VERTICAL)
  1484. # category number
  1485. text = wx.StaticText(parent=panel, id=wx.ID_ANY, label=_("Category number"))
  1486. self.category = wx.SpinCtrl(parent=panel, id=wx.ID_ANY, size=(125, -1),
  1487. initial=UserSettings.Get(group='vdigit', key="category", subkey='value'),
  1488. min=-1e9, max=1e9)
  1489. if UserSettings.Get(group='vdigit', key="categoryMode", subkey='selection') != 1:
  1490. self.category.Enable(False)
  1491. flexSizer.Add(item=text, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL)
  1492. flexSizer.Add(item=self.category, proportion=0,
  1493. flag=wx.FIXED_MINSIZE | wx.ALIGN_CENTER_VERTICAL)
  1494. # category mode
  1495. text = wx.StaticText(parent=panel, id=wx.ID_ANY, label=_("Category mode"))
  1496. self.categoryMode = wx.Choice(parent=panel, id=wx.ID_ANY, size=(125, -1),
  1497. choices=[_("Next to use"), _("Manual entry"), _("No category")])
  1498. self.categoryMode.SetSelection(UserSettings.Get(group='vdigit', key="categoryMode", subkey='selection'))
  1499. flexSizer.Add(item=text, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL)
  1500. flexSizer.Add(item=self.categoryMode, proportion=0,
  1501. flag=wx.FIXED_MINSIZE | wx.ALIGN_CENTER_VERTICAL)
  1502. sizer.Add(item=flexSizer, proportion=1, flag=wx.ALL | wx.EXPAND, border=1)
  1503. border.Add(item=sizer, proportion=0,
  1504. flag=wx.ALL | wx.EXPAND, border=5)
  1505. #
  1506. # delete existing record
  1507. #
  1508. box = wx.StaticBox (parent=panel, id=wx.ID_ANY, label=" %s " % _("Delete existing feature(s)"))
  1509. sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  1510. # checkbox
  1511. self.deleteRecord = wx.CheckBox(parent=panel, id=wx.ID_ANY,
  1512. label=_("Delete record from table"))
  1513. self.deleteRecord.SetValue(UserSettings.Get(group='vdigit', key="delRecord", subkey='enabled'))
  1514. sizer.Add(item=self.deleteRecord, proportion=0, flag=wx.ALL | wx.EXPAND, border=1)
  1515. border.Add(item=sizer, proportion=0,
  1516. flag=wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, border=5)
  1517. # bindings
  1518. self.Bind(wx.EVT_CHECKBOX, self.OnChangeAddRecord, self.addRecord)
  1519. self.Bind(wx.EVT_CHOICE, self.OnChangeCategoryMode, self.categoryMode)
  1520. self.Bind(wx.EVT_CHOICE, self.OnChangeLayer, self.layer)
  1521. panel.SetSizer(border)
  1522. return panel
  1523. def __SymbologyData(self):
  1524. """
  1525. Data for __CreateSymbologyPage()
  1526. label | checkbox | color
  1527. """
  1528. return (
  1529. # ("Background", "symbolBackground"),
  1530. (_("Highlight"), "symbolHighlight"),
  1531. (_("Highlight (duplicates)"), "symbolHighlightDupl"),
  1532. (_("Point"), "symbolPoint"),
  1533. (_("Line"), "symbolLine"),
  1534. (_("Boundary (no area)"), "symbolBoundaryNo"),
  1535. (_("Boundary (one area)"), "symbolBoundaryOne"),
  1536. (_("Boundary (two areas)"), "symbolBoundaryTwo"),
  1537. (_("Centroid (in area)"), "symbolCentroidIn"),
  1538. (_("Centroid (outside area)"), "symbolCentroidOut"),
  1539. (_("Centroid (duplicate in area)"), "symbolCentroidDup"),
  1540. (_("Node (one line)"), "symbolNodeOne"),
  1541. (_("Node (two lines)"), "symbolNodeTwo"),
  1542. (_("Vertex"), "symbolVertex"))
  1543. def OnChangeCategoryMode(self, event):
  1544. """Change category mode"""
  1545. mode = event.GetSelection()
  1546. UserSettings.Set(group='vdigit', key="categoryMode", subkey='selection', value=mode)
  1547. if mode == 1: # manual entry
  1548. self.category.Enable(True)
  1549. elif self.category.IsEnabled(): # disable
  1550. self.category.Enable(False)
  1551. if mode == 2 and self.addRecord.IsChecked(): # no category
  1552. self.addRecord.SetValue(False)
  1553. self.parent.digit.SetCategory()
  1554. self.category.SetValue(UserSettings.Get(group='vdigit', key='category', subkey='value'))
  1555. def OnChangeLayer(self, event):
  1556. """Layer changed"""
  1557. layer = int(event.GetString())
  1558. if layer > 0:
  1559. UserSettings.Set(group='vdigit', key='layer', subkey='value', value=layer)
  1560. self.parent.digit.SetCategory()
  1561. self.category.SetValue(UserSettings.Get(group='vdigit', key='category', subkey='value'))
  1562. event.Skip()
  1563. def OnChangeAddRecord(self, event):
  1564. """Checkbox 'Add new record' status changed"""
  1565. self.category.SetValue(self.parent.digit.SetCategory())
  1566. def OnChangeSnappingValue(self, event):
  1567. """Change snapping value - update static text"""
  1568. value = self.snappingValue.GetValue()
  1569. if self.snappingUnit.GetStringSelection() == "map units":
  1570. threshold = value
  1571. else:
  1572. threshold = self.parent.digit.driver.GetThreshold(value=value)
  1573. self.snappingInfo.SetLabel(_("Snapping threshold is %(value).1f %(units)s") %
  1574. {'value' : threshold,
  1575. 'units' : self.mapUnits})
  1576. event.Skip()
  1577. def OnChangeSnappingUnits(self, event):
  1578. """Snapping units change -> update static text"""
  1579. value = self.snappingValue.GetValue()
  1580. units = self.snappingUnit.GetStringSelection()
  1581. threshold = self.parent.digit.driver.GetThreshold(value=value, units=units)
  1582. if units == "map units":
  1583. self.snappingInfo.SetLabel(_("Snapping threshold is %(value).1f %(units)s") %
  1584. {'value' : value,
  1585. 'units' : self.mapUnits})
  1586. else:
  1587. self.snappingInfo.SetLabel(_("Snapping threshold is %(value).1f %(units)s") %
  1588. {'value' : threshold,
  1589. 'units' : self.mapUnits})
  1590. event.Skip()
  1591. def OnChangeBackgroundMap(self, event):
  1592. """Change background map"""
  1593. map = self.backgroundMap.GetValue()
  1594. UserSettings.Set(group='vdigit', key='backgroundMap', subkey='value', value=map)
  1595. def OnChangeQuery(self, event):
  1596. """Change query"""
  1597. if self.queryLength.GetValue():
  1598. # length
  1599. self.queryLengthSL.Enable(True)
  1600. self.queryLengthValue.Enable(True)
  1601. self.queryDangleSL.Enable(False)
  1602. self.queryDangleValue.Enable(False)
  1603. else:
  1604. # dangle
  1605. self.queryLengthSL.Enable(False)
  1606. self.queryLengthValue.Enable(False)
  1607. self.queryDangleSL.Enable(True)
  1608. self.queryDangleValue.Enable(True)
  1609. def OnSave(self, event):
  1610. """Button 'Save' clicked"""
  1611. self.UpdateSettings()
  1612. self.parent.digittoolbar.settingsDialog = None
  1613. fileSettings = {}
  1614. UserSettings.ReadSettingsFile(settings=fileSettings)
  1615. fileSettings['vdigit'] = UserSettings.Get(group='vdigit')
  1616. file = UserSettings.SaveToFile(fileSettings)
  1617. self.parent.gismanager.goutput.WriteLog('Vector digitizer settings saved to file <%s>.' % file)
  1618. self.Close()
  1619. def OnApply(self, event):
  1620. """Button 'Apply' clicked"""
  1621. self.UpdateSettings()
  1622. def OnCancel(self, event):
  1623. """Button 'Cancel' clicked"""
  1624. self.parent.digittoolbar.settingsDialog = None
  1625. self.Close()
  1626. def UpdateSettings(self):
  1627. """Update UserSettings"""
  1628. # symbology
  1629. for key, (enabled, color) in self.symbology.iteritems():
  1630. if enabled:
  1631. UserSettings.Set(group='vdigit', key=key, subkey='enabled',
  1632. value=enabled.IsChecked())
  1633. UserSettings.Set(group='vdigit', key=key, subkey='color',
  1634. value=color.GetColour())
  1635. else:
  1636. UserSettings.Set(group='vdigit', key=key, subkey='color',
  1637. value=color.GetColour())
  1638. # display
  1639. UserSettings.Set(group='vdigit', key="lineWidth", subkey='value',
  1640. value=int(self.lineWidthValue.GetValue()))
  1641. # snapping
  1642. UserSettings.Set(group='vdigit', key="snapping", subkey='value',
  1643. value=int(self.snappingValue.GetValue()))
  1644. UserSettings.Set(group='vdigit', key="snapping", subkey='units',
  1645. value=self.snappingUnit.GetStringSelection())
  1646. UserSettings.Set(group='vdigit', key="snapToVertex", subkey='enabled',
  1647. value=self.snapVertex.IsChecked())
  1648. # digitize new feature
  1649. UserSettings.Set(group='vdigit', key="addRecord", subkey='enabled',
  1650. value=self.addRecord.IsChecked())
  1651. UserSettings.Set(group='vdigit', key="layer", subkey='value',
  1652. value=int(self.layer.GetStringSelection()))
  1653. UserSettings.Set(group='vdigit', key="category", subkey='value',
  1654. value=int(self.category.GetValue()))
  1655. UserSettings.Set(group='vdigit', key="categoryMode", subkey='selection',
  1656. value=self.categoryMode.GetSelection())
  1657. # delete existing feature
  1658. UserSettings.Set(group='vdigit', key="delRecord", subkey='enabled',
  1659. value=self.deleteRecord.IsChecked())
  1660. # snapping threshold
  1661. self.parent.digit.threshold = self.parent.digit.driver.GetThreshold()
  1662. # query tool
  1663. if self.queryLength.GetValue():
  1664. UserSettings.Set(group='vdigit', key="query", subkey='selection',
  1665. value=0)
  1666. else:
  1667. UserSettings.Set(group='vdigit', key="query", subkey='type',
  1668. value=1)
  1669. UserSettings.Set(group='vdigit', key="query", subkey='box',
  1670. value=self.queryBox.IsChecked())
  1671. UserSettings.Set(group='vdigit', key="queryLength", subkey='than-selection',
  1672. value=self.queryLengthSL.GetSelection())
  1673. UserSettings.Set(group='vdigit', key="queryLength", subkey='thresh',
  1674. value=int(self.queryLengthValue.GetValue()))
  1675. UserSettings.Set(group='vdigit', key="queryDangle", subkey='than-selection',
  1676. value=self.queryDangleSL.GetSelection())
  1677. UserSettings.Set(group='vdigit', key="queryDangle", subkey='thresh',
  1678. value=int(self.queryDangleValue.GetValue()))
  1679. # select features
  1680. for feature in ('Point', 'Line',
  1681. 'Centroid', 'Boundary'):
  1682. UserSettings.Set(group='vdigit', key='selectFeature'+feature, subkey='enabled',
  1683. value=self.FindWindowById(self.selectFeature[feature]).IsChecked())
  1684. UserSettings.Set(group='vdigit', key="selectThresh", subkey='value',
  1685. value=int(self.selectThreshValue.GetValue()))
  1686. UserSettings.Set(group='vdigit', key="checkForDupl", subkey='enabled',
  1687. value=self.checkForDupl.IsChecked())
  1688. # on-exit
  1689. UserSettings.Set(group='vdigit', key="saveOnExit", subkey='enabled',
  1690. value=self.save.IsChecked())
  1691. # update driver settings
  1692. self.parent.digit.driver.UpdateSettings()
  1693. # redraw map if auto-rendering is enabled
  1694. if self.parent.autoRender.GetValue():
  1695. self.parent.OnRender(None)
  1696. class VDigitCategoryDialog(wx.Dialog, listmix.ColumnSorterMixin):
  1697. """
  1698. Dialog used to display/modify categories of vector objects
  1699. @param parent
  1700. @param title dialog title
  1701. @param query {coordinates, qdist} - v.edit/v.what
  1702. @param cats directory of categories - vdigit
  1703. @param line line id - vdigit
  1704. @param pos
  1705. @param style
  1706. """
  1707. def __init__(self, parent, title,
  1708. map, query=None, cats=None, line=None,
  1709. pos=wx.DefaultPosition,
  1710. style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER):
  1711. # parent
  1712. self.parent = parent # mapdisplay.BufferedWindow class instance
  1713. # map name
  1714. self.map = map
  1715. # line id (if not found remains 'None')
  1716. self.line = None
  1717. # {layer: [categories]}
  1718. self.cats = {}
  1719. # do not display dialog if no line is found (-> self.cats)
  1720. if cats is None:
  1721. if self.__GetCategories(query[0], query[1]) == 0 or not self.line:
  1722. Debug.msg(3, "VDigitCategoryDialog(): nothing found!")
  1723. return
  1724. else:
  1725. # self.cats = dict(cats)
  1726. for layer in cats.keys():
  1727. self.cats[layer] = list(cats[layer]) # TODO: tuple to list
  1728. self.line = line
  1729. # make copy of cats (used for 'reload')
  1730. self.cats_orig = copy.deepcopy(self.cats)
  1731. Debug.msg(3, "VDigitCategoryDialog(): line=%d, cats=%s" % \
  1732. (self.line, self.cats))
  1733. wx.Dialog.__init__(self, parent=self.parent, id=wx.ID_ANY, title=title,
  1734. style=style, pos=pos)
  1735. # list of categories
  1736. box = wx.StaticBox(parent=self, id=wx.ID_ANY,
  1737. label=" %s " % _("List of categories"))
  1738. listSizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  1739. self.list = CategoryListCtrl(parent=self, id=wx.ID_ANY,
  1740. style=wx.LC_REPORT |
  1741. wx.BORDER_NONE |
  1742. wx.LC_SORT_ASCENDING |
  1743. wx.LC_HRULES |
  1744. wx.LC_VRULES)
  1745. # sorter
  1746. self.itemDataMap = self.list.Populate()
  1747. listmix.ColumnSorterMixin.__init__(self, 2)
  1748. listSizer.Add(item=self.list, proportion=1, flag=wx.EXPAND)
  1749. # add new category
  1750. box = wx.StaticBox(parent=self, id=wx.ID_ANY,
  1751. label=" %s " % _("Add new category"))
  1752. addSizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  1753. flexSizer = wx.FlexGridSizer (cols=5, hgap=5, vgap=5)
  1754. flexSizer.AddGrowableCol(3)
  1755. layerNewTxt = wx.StaticText(parent=self, id=wx.ID_ANY,
  1756. label="%s:" % _("Layer"))
  1757. self.layerNew = wx.SpinCtrl(parent=self, id=wx.ID_ANY, size=(50, -1),
  1758. initial=1, min=1, max=1e9)
  1759. catNewTxt = wx.StaticText(parent=self, id=wx.ID_ANY,
  1760. label="%s:" % _("Category"))
  1761. try:
  1762. newCat = max(self.cats[1]) + 1
  1763. except:
  1764. newCat = 1
  1765. self.catNew = wx.SpinCtrl(parent=self, id=wx.ID_ANY, size=(75, -1),
  1766. initial=newCat, min=-1e9, max=1e9)
  1767. btnAddCat = wx.Button(self, wx.ID_ADD)
  1768. flexSizer.Add(item=layerNewTxt, proportion=0,
  1769. flag=wx.FIXED_MINSIZE | wx.ALIGN_CENTER_VERTICAL)
  1770. flexSizer.Add(item=self.layerNew, proportion=0,
  1771. flag=wx.FIXED_MINSIZE | wx.ALIGN_CENTER_VERTICAL)
  1772. flexSizer.Add(item=catNewTxt, proportion=0,
  1773. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT | wx.LEFT,
  1774. border=10)
  1775. flexSizer.Add(item=self.catNew, proportion=0,
  1776. flag=wx.FIXED_MINSIZE | wx.ALIGN_CENTER_VERTICAL)
  1777. flexSizer.Add(item=btnAddCat, proportion=0,
  1778. flag=wx.EXPAND | wx.ALIGN_RIGHT | wx.FIXED_MINSIZE)
  1779. addSizer.Add(item=flexSizer, proportion=1, flag=wx.ALL | wx.EXPAND, border=5)
  1780. # buttons
  1781. btnApply = wx.Button(self, wx.ID_APPLY)
  1782. btnCancel = wx.Button(self, wx.ID_CANCEL)
  1783. #btnReload = wx.Button(self, wx.ID_UNDO, _("&Reload"))
  1784. btnOk = wx.Button(self, wx.ID_OK)
  1785. btnOk.SetDefault()
  1786. # sizers
  1787. btnSizer = wx.StdDialogButtonSizer()
  1788. btnSizer.AddButton(btnCancel)
  1789. #btnSizer.AddButton(btnReload)
  1790. #btnSizer.SetNegativeButton(btnReload)
  1791. btnSizer.AddButton(btnApply)
  1792. btnSizer.AddButton(btnOk)
  1793. btnSizer.Realize()
  1794. mainSizer = wx.BoxSizer(wx.VERTICAL)
  1795. mainSizer.Add(item=listSizer, proportion=1,
  1796. flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
  1797. mainSizer.Add(item=addSizer, proportion=0,
  1798. flag=wx.EXPAND | wx.ALIGN_CENTER |
  1799. wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5)
  1800. mainSizer.Add(item=btnSizer, proportion=0,
  1801. flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
  1802. self.SetSizer(mainSizer)
  1803. mainSizer.Fit(self)
  1804. self.SetAutoLayout(True)
  1805. # set min size for dialog
  1806. self.SetMinSize(self.GetBestSize())
  1807. # bindings
  1808. # buttons
  1809. #btnReload.Bind(wx.EVT_BUTTON, self.OnReload)
  1810. btnApply.Bind(wx.EVT_BUTTON, self.OnApply)
  1811. btnOk.Bind(wx.EVT_BUTTON, self.OnOK)
  1812. btnAddCat.Bind(wx.EVT_BUTTON, self.OnAddCat)
  1813. btnCancel.Bind(wx.EVT_BUTTON, self.OnCancel)
  1814. # list
  1815. # self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self.list)
  1816. # self.list.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
  1817. self.list.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnRightUp) #wxMSW
  1818. self.list.Bind(wx.EVT_RIGHT_UP, self.OnRightUp) #wxGTK
  1819. self.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.OnBeginEdit, self.list)
  1820. self.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.OnEndEdit, self.list)
  1821. self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick, self.list)
  1822. def GetListCtrl(self):
  1823. """Used by ColumnSorterMixin"""
  1824. return self.list
  1825. def OnColClick(self, event):
  1826. """Click on column header (order by)"""
  1827. event.Skip()
  1828. def OnBeginEdit(self, event):
  1829. """Editing of item started"""
  1830. event.Allow()
  1831. def OnEndEdit(self, event):
  1832. """Finish editing of item"""
  1833. itemIndex = event.GetIndex()
  1834. layerOld = int (self.list.GetItem(itemIndex, 0).GetText())
  1835. catOld = int (self.list.GetItem(itemIndex, 1).GetText())
  1836. if event.GetColumn() == 0:
  1837. layerNew = int(event.GetLabel())
  1838. catNew = catOld
  1839. else:
  1840. layerNew = layerOld
  1841. catNew = int(event.GetLabel())
  1842. try:
  1843. if layerNew not in self.cats.keys():
  1844. self.cats[layerNew] = []
  1845. self.cats[layerNew].append(catNew)
  1846. self.cats[layerOld].remove(catOld)
  1847. except:
  1848. event.Veto()
  1849. self.list.SetStringItem(itemIndex, 0, str(layerNew))
  1850. self.list.SetStringItem(itemIndex, 1, str(catNew))
  1851. dlg = wx.MessageDialog(self, _("Unable to add new layer/category <%(layer)s/%(category)s>.\n"
  1852. "Layer and category number must be integer.\n"
  1853. "Layer number must be greater then zero.") %
  1854. { 'layer': str(self.layerNew.GetValue()),
  1855. 'category' : str(self.catNew.GetValue()) },
  1856. _("Error"), wx.OK | wx.ICON_ERROR)
  1857. dlg.ShowModal()
  1858. dlg.Destroy()
  1859. return False
  1860. def OnRightDown(self, event):
  1861. """Mouse right button down"""
  1862. x = event.GetX()
  1863. y = event.GetY()
  1864. item, flags = self.list.HitTest((x, y))
  1865. if item != wx.NOT_FOUND and \
  1866. flags & wx.LIST_HITTEST_ONITEM:
  1867. self.list.Select(item)
  1868. event.Skip()
  1869. def OnRightUp(self, event):
  1870. """Mouse right button up"""
  1871. if not hasattr(self, "popupID1"):
  1872. self.popupID1 = wx.NewId()
  1873. self.popupID2 = wx.NewId()
  1874. self.popupID3 = wx.NewId()
  1875. self.Bind(wx.EVT_MENU, self.OnItemDelete, id=self.popupID1)
  1876. self.Bind(wx.EVT_MENU, self.OnItemDeleteAll, id=self.popupID2)
  1877. self.Bind(wx.EVT_MENU, self.OnReload, id=self.popupID3)
  1878. # generate popup-menu
  1879. menu = wx.Menu()
  1880. menu.Append(self.popupID1, _("Delete selected"))
  1881. if self.list.GetFirstSelected() == -1:
  1882. menu.Enable(self.popupID1, False)
  1883. menu.Append(self.popupID2, _("Delete all"))
  1884. menu.AppendSeparator()
  1885. menu.Append(self.popupID3, _("Reload"))
  1886. self.PopupMenu(menu)
  1887. menu.Destroy()
  1888. def OnItemSelected(self, event):
  1889. """Item selected"""
  1890. event.Skip()
  1891. def OnItemDelete(self, event):
  1892. """Delete selected item(s) from the list (layer/category pair)"""
  1893. item = self.list.GetFirstSelected()
  1894. while item != -1:
  1895. layer = int (self.list.GetItem(item, 0).GetText())
  1896. cat = int (self.list.GetItem(item, 1).GetText())
  1897. self.list.DeleteItem(item)
  1898. self.cats[layer].remove(cat)
  1899. item = self.list.GetFirstSelected()
  1900. event.Skip()
  1901. def OnItemDeleteAll(self, event):
  1902. """Delete all items from the list"""
  1903. self.list.DeleteAllItems()
  1904. self.cats = {}
  1905. event.Skip()
  1906. def __GetCategories(self, coords, qdist):
  1907. """Get layer/category pairs for all available
  1908. layers
  1909. Return True line found or False if not found"""
  1910. cmdWhat = gcmd.Command(cmd=['v.what',
  1911. '--q',
  1912. 'map=%s' % self.map,
  1913. 'east_north=%f,%f' % \
  1914. (float(coords[0]), float(coords[1])),
  1915. 'distance=%f' % qdist])
  1916. if cmdWhat.returncode != 0:
  1917. return False
  1918. for item in cmdWhat.ReadStdOutput():
  1919. litem = item.lower()
  1920. if "line:" in litem: # get line id
  1921. self.line = int(item.split(':')[1].strip())
  1922. elif "layer:" in litem: # add layer
  1923. layer = int(item.split(':')[1].strip())
  1924. if layer not in self.cats.keys():
  1925. self.cats[layer] = []
  1926. elif "category:" in litem: # add category
  1927. self.cats[layer].append(int(item.split(':')[1].strip()))
  1928. return True
  1929. def OnReload(self, event):
  1930. """Reload button pressed"""
  1931. # restore original list
  1932. self.cats = copy.deepcopy(self.cats_orig)
  1933. # polulate list
  1934. self.itemDataMap = self.list.Populate(update=True)
  1935. event.Skip()
  1936. def OnCancel(self, event):
  1937. """Cancel button pressed"""
  1938. self.parent.parent.dialogs['category'] = None
  1939. if self.parent.parent.digit:
  1940. self.parent.parent.digit.driver.SetSelected([])
  1941. self.parent.UpdateMap(render=False)
  1942. else:
  1943. self.parent.parent.OnRender(None)
  1944. self.Close()
  1945. def OnApply(self, event):
  1946. """Apply button pressed"""
  1947. # action : (catsFrom, catsTo)
  1948. check = {'catadd': (self.cats, self.cats_orig),
  1949. 'catdel': (self.cats_orig, self.cats)}
  1950. # add/delete new category
  1951. for action, cats in check.iteritems():
  1952. for layer in cats[0].keys():
  1953. catList = []
  1954. for cat in cats[0][layer]:
  1955. if layer not in cats[1].keys() or \
  1956. cat not in cats[1][layer]:
  1957. catList.append(cat)
  1958. if catList != []:
  1959. if UserSettings.Get(group='advanced', key='digitInterface', subkey='type') == 'vedit':
  1960. vEditCmd = ['v.edit', '--q',
  1961. 'map=%s' % self.map,
  1962. 'layer=%d' % layer,
  1963. 'tool=%s' % action,
  1964. 'cats=%s' % ",".join(["%d" % v for v in catList]),
  1965. 'id=%d' % self.line]
  1966. gcmd.Command(vEditCmd)
  1967. else:
  1968. if action == 'catadd':
  1969. add = True
  1970. else:
  1971. add = False
  1972. self.line = self.parent.parent.digit.SetLineCats(-1, layer,
  1973. catList, add)
  1974. if self.line < 0:
  1975. wx.MessageBox(parent=self, message=_("Unable to update vector map."),
  1976. caption=_("Error"), style=wx.OK | wx.ICON_ERROR)
  1977. if UserSettings.Get(group='advanced', key='digitInterface', subkey='type') == 'vedit':
  1978. # reload map (needed for v.edit)
  1979. self.parent.parent.digit.driver.ReloadMap()
  1980. self.cats_orig = copy.deepcopy(self.cats)
  1981. event.Skip()
  1982. def OnOK(self, event):
  1983. """OK button pressed"""
  1984. self.OnApply(event)
  1985. self.OnCancel(event)
  1986. def OnAddCat(self, event):
  1987. """Button 'Add' new category pressed"""
  1988. try:
  1989. layer = int(self.layerNew.GetValue())
  1990. cat = int(self.catNew.GetValue())
  1991. if layer <= 0:
  1992. raise ValueError
  1993. except ValueError:
  1994. dlg = wx.MessageDialog(self, _("Unable to add new layer/category <%(layer)s/%(category)s>.\n"
  1995. "Layer and category number must be integer.\n"
  1996. "Layer number must be greater then zero.") %
  1997. {'layer' : str(self.layerNew.GetValue()),
  1998. 'category' : str(self.catNew.GetValue())},
  1999. _("Error"), wx.OK | wx.ICON_ERROR)
  2000. dlg.ShowModal()
  2001. dlg.Destroy()
  2002. return False
  2003. if layer not in self.cats.keys():
  2004. self.cats[layer] = []
  2005. self.cats[layer].append(cat)
  2006. # reload list
  2007. self.itemDataMap = self.list.Populate(update=True)
  2008. # update category number for add
  2009. self.catNew.SetValue(cat + 1)
  2010. event.Skip()
  2011. return True
  2012. def GetLine(self):
  2013. """Get id of selected line of 'None' if no line is selected"""
  2014. return self.line
  2015. def UpdateDialog(self, query=None, cats=None, line=None):
  2016. """Update dialog
  2017. @param query {coordinates, distance} - v.edit/v.what
  2018. @param cats directory layer/cats - vdigit
  2019. Return True if updated otherwise False
  2020. """
  2021. # line id (if not found remains 'None')
  2022. self.line = None
  2023. # {layer: [categories]}
  2024. self.cats = {}
  2025. # do not display dialog if no line is found (-> self.cats)
  2026. if cats is None:
  2027. ret = self.__GetCategories(query[0], query[1])
  2028. else:
  2029. # self.cats = dict(cats)
  2030. for layer in cats.keys():
  2031. self.cats[layer] = list(cats[layer]) # TODO: tuple to list
  2032. self.line = line
  2033. ret = 1
  2034. if ret == 0 or not self.line:
  2035. Debug.msg(3, "VDigitCategoryDialog(): nothing found!")
  2036. return False
  2037. # make copy of cats (used for 'reload')
  2038. self.cats_orig = copy.deepcopy(self.cats)
  2039. # polulate list
  2040. self.itemDataMap = self.list.Populate(update=True)
  2041. try:
  2042. newCat = max(self.cats[1]) + 1
  2043. except:
  2044. newCat = 1
  2045. self.catNew.SetValue(newCat)
  2046. return True
  2047. class CategoryListCtrl(wx.ListCtrl,
  2048. listmix.ListCtrlAutoWidthMixin,
  2049. listmix.TextEditMixin):
  2050. """List of layers/categories"""
  2051. def __init__(self, parent, id, pos=wx.DefaultPosition,
  2052. size=wx.DefaultSize, style=0):
  2053. self.parent = parent
  2054. wx.ListCtrl.__init__(self, parent, id, pos, size, style)
  2055. listmix.ListCtrlAutoWidthMixin.__init__(self)
  2056. listmix.TextEditMixin.__init__(self)
  2057. def Populate(self, update=False):
  2058. """Populate the list"""
  2059. itemData = {} # requested by sorter
  2060. if not update:
  2061. self.InsertColumn(0, _("Layer"))
  2062. self.InsertColumn(1, _("Category"))
  2063. else:
  2064. self.DeleteAllItems()
  2065. i = 1
  2066. for layer in self.parent.cats.keys():
  2067. catsList = self.parent.cats[layer]
  2068. for cat in catsList:
  2069. index = self.InsertStringItem(sys.maxint, str(catsList[0]))
  2070. self.SetStringItem(index, 0, str(layer))
  2071. self.SetStringItem(index, 1, str(cat))
  2072. self.SetItemData(index, i)
  2073. itemData[i] = (str(layer), str(cat))
  2074. i = i + 1
  2075. if not update:
  2076. self.SetColumnWidth(0, 100)
  2077. self.SetColumnWidth(1, wx.LIST_AUTOSIZE)
  2078. self.currentItem = 0
  2079. return itemData
  2080. class VDigitZBulkDialog(wx.Dialog):
  2081. """
  2082. Dialog used for Z bulk-labeling tool
  2083. """
  2084. def __init__(self, parent, title, nselected, style=wx.DEFAULT_DIALOG_STYLE):
  2085. wx.Dialog.__init__(self, parent=parent, id=wx.ID_ANY, title=title, style=style)
  2086. self.parent = parent # mapdisplay.BufferedWindow class instance
  2087. # panel = wx.Panel(parent=self, id=wx.ID_ANY)
  2088. border = wx.BoxSizer(wx.VERTICAL)
  2089. txt = wx.StaticText(parent=self,
  2090. label=_("%d lines selected for z bulk-labeling") % nselected);
  2091. border.Add(item=txt, proportion=0, flag=wx.ALL | wx.EXPAND, border=5)
  2092. box = wx.StaticBox (parent=self, id=wx.ID_ANY, label=" %s " % _("Set value"))
  2093. sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  2094. flexSizer = wx.FlexGridSizer (cols=2, hgap=5, vgap=5)
  2095. flexSizer.AddGrowableCol(0)
  2096. # starting value
  2097. txt = wx.StaticText(parent=self,
  2098. label=_("Starting value"));
  2099. self.value = wx.SpinCtrl(parent=self, id=wx.ID_ANY, size=(150, -1),
  2100. initial=0,
  2101. min=-1e6, max=1e6)
  2102. flexSizer.Add(txt, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL)
  2103. flexSizer.Add(self.value, proportion=0, flag=wx.ALIGN_CENTER | wx.FIXED_MINSIZE)
  2104. # step
  2105. txt = wx.StaticText(parent=self,
  2106. label=_("Step"))
  2107. self.step = wx.SpinCtrl(parent=self, id=wx.ID_ANY, size=(150, -1),
  2108. initial=0,
  2109. min=0, max=1e6)
  2110. flexSizer.Add(txt, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL)
  2111. flexSizer.Add(self.step, proportion=0, flag=wx.ALIGN_CENTER | wx.FIXED_MINSIZE)
  2112. sizer.Add(item=flexSizer, proportion=1, flag=wx.ALL | wx.EXPAND, border=1)
  2113. border.Add(item=sizer, proportion=1, flag=wx.ALL | wx.EXPAND, border=5)
  2114. # buttons
  2115. btnCancel = wx.Button(self, wx.ID_CANCEL)
  2116. btnOk = wx.Button(self, wx.ID_OK)
  2117. btnOk.SetDefault()
  2118. # sizers
  2119. btnSizer = wx.StdDialogButtonSizer()
  2120. btnSizer.AddButton(btnCancel)
  2121. btnSizer.AddButton(btnOk)
  2122. btnSizer.Realize()
  2123. mainSizer = wx.BoxSizer(wx.VERTICAL)
  2124. mainSizer.Add(item=border, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  2125. mainSizer.Add(item=btnSizer, proportion=0,
  2126. flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
  2127. self.SetSizer(mainSizer)
  2128. mainSizer.Fit(self)
  2129. class VDigitDuplicatesDialog(wx.Dialog):
  2130. """
  2131. Show duplicated feature ids
  2132. """
  2133. def __init__(self, parent, data, title=_("List of duplicates"),
  2134. style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
  2135. pos=wx.DefaultPosition):
  2136. wx.Dialog.__init__(self, parent=parent, id=wx.ID_ANY, title=title, style=style,
  2137. pos=pos)
  2138. self.parent = parent # BufferedWindow
  2139. self.data = data
  2140. self.winList = []
  2141. # panel = wx.Panel(parent=self, id=wx.ID_ANY)
  2142. # notebook
  2143. self.notebook = wx.Notebook(parent=self, id=wx.ID_ANY, style=wx.BK_DEFAULT)
  2144. id = 1
  2145. for key in self.data.keys():
  2146. panel = wx.Panel(parent=self.notebook, id=wx.ID_ANY)
  2147. self.notebook.AddPage(page=panel, text=" %d " % (id))
  2148. # notebook body
  2149. border = wx.BoxSizer(wx.VERTICAL)
  2150. win = CheckListFeature(parent=panel, data=list(self.data[key]))
  2151. self.winList.append(win.GetId())
  2152. border.Add(item=win, proportion=1,
  2153. flag=wx.ALL | wx.EXPAND, border=5)
  2154. panel.SetSizer(border)
  2155. id += 1
  2156. # buttons
  2157. btnCancel = wx.Button(self, wx.ID_CANCEL)
  2158. btnOk = wx.Button(self, wx.ID_OK)
  2159. btnOk.SetDefault()
  2160. # sizers
  2161. btnSizer = wx.StdDialogButtonSizer()
  2162. btnSizer.AddButton(btnCancel)
  2163. btnSizer.AddButton(btnOk)
  2164. btnSizer.Realize()
  2165. mainSizer = wx.BoxSizer(wx.VERTICAL)
  2166. mainSizer.Add(item=self.notebook, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  2167. mainSizer.Add(item=btnSizer, proportion=0,
  2168. flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
  2169. self.SetSizer(mainSizer)
  2170. mainSizer.Fit(self)
  2171. self.SetAutoLayout(True)
  2172. # set min size for dialog
  2173. self.SetMinSize((250, 180))
  2174. def GetUnSelected(self):
  2175. """Get unselected items (feature id)
  2176. @return list of ids
  2177. """
  2178. ids = []
  2179. for id in self.winList:
  2180. wlist = self.FindWindowById(id)
  2181. for item in range(wlist.GetItemCount()):
  2182. if not wlist.IsChecked(item):
  2183. ids.append(int(wlist.GetItem(item, 0).GetText()))
  2184. return ids
  2185. class CheckListFeature(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin, listmix.CheckListCtrlMixin):
  2186. """List of mapset/owner/group"""
  2187. def __init__(self, parent, data,
  2188. pos=wx.DefaultPosition, log=None):
  2189. self.parent = parent
  2190. self.data = data
  2191. wx.ListCtrl.__init__(self, parent, wx.ID_ANY,
  2192. style=wx.LC_REPORT)
  2193. listmix.CheckListCtrlMixin.__init__(self)
  2194. self.log = log
  2195. # setup mixins
  2196. listmix.ListCtrlAutoWidthMixin.__init__(self)
  2197. self.LoadData(self.data)
  2198. def LoadData(self, data):
  2199. """Load data into list"""
  2200. self.InsertColumn(0, _('Feature id'))
  2201. self.InsertColumn(1, _('Layer (Categories)'))
  2202. for item in data:
  2203. index = self.InsertStringItem(sys.maxint, str(item[0]))
  2204. self.SetStringItem(index, 1, str(item[1]))
  2205. # enable all items by default
  2206. for item in range(self.GetItemCount()):
  2207. self.CheckItem(item, True)
  2208. self.SetColumnWidth(col=0, width=wx.LIST_AUTOSIZE_USEHEADER)
  2209. self.SetColumnWidth(col=1, width=wx.LIST_AUTOSIZE_USEHEADER)
  2210. def OnCheckItem(self, index, flag):
  2211. """Mapset checked/unchecked"""
  2212. pass