wizard.py 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  1. """
  2. @package rlisetup.py
  3. @brief GUI per r.li.setup module
  4. Classes:
  5. - RLiSetupFrame (first frame to show existing conf file and choose some
  6. operation)
  7. - RLIWizard (the main wizard)
  8. - FirstPage (first page of wizard, choose name of conf file, raster, vector,
  9. sampling region)
  10. - Keyboard (page to insert region areas from keyboard)
  11. - SamplingAreas (define sampling area)
  12. - SummaryPage (show choosen options)
  13. (C) 2011 by the GRASS Development Team
  14. This program is free software under the GNU General Public License
  15. (>=v2). Read the file COPYING that comes with GRASS for details.
  16. @author Luca Delucchi <lucadeluge gmail com>
  17. """
  18. import os
  19. import wx
  20. import wx.wizard as wiz
  21. import wx.lib.scrolledpanel as scrolled
  22. from gui_core import gselect
  23. from core.utils import _
  24. from location_wizard.wizard import TitledPage as TitledPage
  25. from rlisetup.functions import checkValue, retRLiPath
  26. from rlisetup.sampling_frame import RLiSetupMapPanel
  27. from grass.script import core as grass
  28. from grass.script import raster as grast
  29. from functions import SamplingType, sampleAreaVector, convertFeature, obtainAreaVector
  30. from core.gcmd import GError, GMessage, RunCommand
  31. #@NOTE: r.li.setup writes in the settings file with
  32. ## r.li.windows.tcl:
  33. #exec echo "SAMPLINGFRAME $per_x|$per_y|$per_rl|$per_cl" >> $env(TMP).set
  34. class RLIWizard(object):
  35. """
  36. !Start wizard here and finish wizard here
  37. """
  38. def __init__(self, parent):
  39. self.parent = parent
  40. self.wizard = wiz.Wizard(parent=parent, id=wx.ID_ANY,
  41. title=_("Create new configuration file for "
  42. "r.li modules"))
  43. self.rlipath = retRLiPath()
  44. self.msAreaList = []
  45. #pages
  46. self.startpage = FirstPage(self.wizard, self)
  47. self.drawsampleframepage = DrawSampleFramePage(self.wizard, self)
  48. self.keyboardpage = KeyboardPage(self.wizard, self)
  49. self.samplingareapage = SamplingAreasPage(self.wizard, self)
  50. self.summarypage = SummaryPage(self.wizard, self)
  51. self.units = SampleUnitsKeyPage(self.wizard, self)
  52. self.drawunits = UnitsMousePage(self.wizard, self)
  53. self.drawsampleunitspage = DrawSampleUnitsPage(self.wizard, self)
  54. self.vectorareas = VectorAreasPage(self.wizard, self)
  55. self.moving = MovingKeyPage(self.wizard, self)
  56. self.regions = DrawRegionsPage(self.wizard, self)
  57. #order of pages
  58. self.startpage.SetNext(self.samplingareapage)
  59. self.keyboardpage.SetPrev(self.startpage)
  60. self.keyboardpage.SetNext(self.samplingareapage)
  61. self.drawsampleframepage.SetNext(self.samplingareapage)
  62. self.drawsampleframepage.SetPrev(self.startpage)
  63. self.samplingareapage.SetPrev(self.startpage)
  64. self.samplingareapage.SetNext(self.summarypage)
  65. self.regions.SetPrev(self.samplingareapage)
  66. self.regions.SetNext(self.summarypage)
  67. self.units.SetPrev(self.samplingareapage)
  68. self.units.SetNext(self.summarypage)
  69. self.drawunits.SetPrev(self.samplingareapage)
  70. self.drawunits.SetNext(self.drawsampleunitspage)
  71. self.drawsampleunitspage.SetPrev(self.drawunits)
  72. self.drawsampleunitspage.SetNext(self.summarypage)
  73. self.moving.SetPrev(self.samplingareapage)
  74. self.moving.SetNext(self.summarypage)
  75. self.vectorareas.SetPrev(self.samplingareapage)
  76. self.vectorareas.SetNext(self.summarypage)
  77. self.summarypage.SetPrev(self.samplingareapage)
  78. #layout
  79. self.startpage.DoLayout()
  80. self.drawsampleframepage.DoLayout()
  81. self.keyboardpage.DoLayout()
  82. self.samplingareapage.DoLayout()
  83. self.summarypage.DoLayout()
  84. self.units.DoLayout()
  85. self.drawunits.DoLayout()
  86. self.drawsampleunitspage.DoLayout()
  87. self.regions.DoLayout()
  88. self.moving.DoLayout()
  89. self.vectorareas.DoLayout()
  90. self.wizard.FitToPage(self.startpage)
  91. #run_wizard
  92. if self.wizard.RunWizard(self.startpage):
  93. dlg = wx.MessageDialog(parent=self.parent,
  94. message=_("Do you want to create r.li "
  95. "configuration file <%s>?") % self.startpage.conf_name,
  96. caption=_("Create new r.li configuration file?"),
  97. style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
  98. if dlg.ShowModal() == wx.ID_NO:
  99. self._cleanup()
  100. else:
  101. self._write_confile()
  102. self._cleanup()
  103. dlg.Destroy()
  104. else:
  105. self.wizard.Destroy()
  106. GMessage(parent=self.parent,
  107. message=_("r.li.setup wizard canceled. "
  108. "Configuration file not created."))
  109. self._cleanup()
  110. def _write_confile(self):
  111. """!Write the configuration file"""
  112. f = open(os.path.join(self.rlipath, self.startpage.conf_name), 'w')
  113. self.rasterinfo = grast.raster_info(self.startpage.rast)
  114. self._write_region(f)
  115. self._write_area(f)
  116. f.close()
  117. def _temp_region(self):
  118. # save current settings:
  119. grass.use_temp_region()
  120. # Temporarily aligning region resolution to $RASTER resolution
  121. # keep boundary settings
  122. grass.run_command('g.region', rast=self.startpage.rast)
  123. self.gregion = grass.region()
  124. self.SF_NSRES = self.gregion['nsres']
  125. self.SF_EWRES = self.gregion['ewres']
  126. def _write_region(self, fil):
  127. """!Write the region"""
  128. if self.startpage.region == 'whole':
  129. fil.write("SAMPLINGFRAME 0|0|1|1\n")
  130. self._temp_region()
  131. self.SF_X = 0.0
  132. self.SF_Y = 0.0
  133. self.SF_RL = abs(int(float(self.gregion['s'] - self.gregion['n'])
  134. / float(self.gregion['nsres'])))
  135. self.SF_CL = abs(int(float(self.gregion['e'] - self.gregion['w'])
  136. / float(self.gregion['ewres'])))
  137. self.SF_N = self.gregion['n']
  138. self.SF_S = self.gregion['s']
  139. self.SF_E = self.gregion['e']
  140. self.SF_W = self.gregion['w']
  141. self.per_x = float(self.SF_X) / float(self.rasterinfo['cols'])
  142. self.per_y = float(self.SF_Y) / float(self.rasterinfo['rows'])
  143. self.per_rl = float(self.SF_RL) / float(self.rasterinfo['rows'])
  144. self.per_cl = float(self.SF_CL) / float(self.rasterinfo['cols'])
  145. elif self.startpage.region == 'key':
  146. self._temp_region()
  147. self.SF_X = float(self.keyboardpage.col_up)
  148. self.SF_Y = float(self.keyboardpage.row_up)
  149. self.SF_RL = float(self.keyboardpage.row_len)
  150. self.SF_CL = float(self.keyboardpage.col_len)
  151. self.SF_N = self.gregion['n'] - (self.SF_NSRES * self.SF_Y)
  152. self.SF_S = self.gregion['n'] - (self.SF_NSRES * self.SF_Y + self.SF_RL)
  153. self.SF_W = self.gregion['w'] + (self.SF_EWRES * self.SF_X)
  154. self.SF_E = self.gregion['w'] + (self.SF_EWRES * self.SF_X + self.SF_CL)
  155. self.per_x = float(self.SF_X) / float(self.rasterinfo['cols'])
  156. self.per_y = float(self.SF_Y) / float(self.rasterinfo['rows'])
  157. self.per_rl = float(self.SF_RL) / float(self.rasterinfo['rows'])
  158. self.per_cl = float(self.SF_CL) / float(self.rasterinfo['cols'])
  159. fil.write("SAMPLINGFRAME %r|%r|%r|%r\n" % (self.per_x, self.per_y,
  160. self.per_rl,
  161. self.per_cl))
  162. elif self.startpage.region == 'draw':
  163. self._temp_region()
  164. tregion = self.drawsampleframepage.tregion
  165. # should we call this? with align param?
  166. RunCommand('g.region', align=self.startpage.rast, n=tregion['n'],
  167. s=tregion['s'], w=tregion['w'], e=tregion['e'])
  168. newreg = grass.region()
  169. self.SF_N = newreg['n'] # set env(SF_N) $n
  170. self.SF_S = newreg['s'] # set env(SF_S) $s
  171. self.SF_E = newreg['e'] # set env(SF_E) $e
  172. self.SF_W = newreg['w'] # set env(SF_W) $w
  173. self.SF_Y = abs(round(self.gregion['n'] - newreg['n']) / newreg['nsres'])
  174. # set env(SF_Y) [expr abs(round(($s_n - $n) / $nres)) ]
  175. self.SF_X = abs(round(self.gregion['w'] - newreg['w']) / newreg['ewres'])
  176. # set env(SF_X) [expr abs(round(($s_w - $w) / $sres)) ]
  177. self.SF_RL = abs(round(newreg['n'] - newreg['s']) / newreg['nsres'])
  178. # set env(SF_RL) [expr abs(round(($n - $s) / $nres)) ]
  179. self.SF_CL = abs(round(newreg['e'] - newreg['w']) / newreg['ewres'])
  180. # set env(SF_CL) [expr abs(round(($e - $w) / $sres)) ]
  181. self.per_x = float(self.SF_X) / float(self.rasterinfo['cols'])
  182. # double($env(SF_X)) / double($cols)
  183. self.per_y = float(self.SF_Y) / float(self.rasterinfo['rows'])
  184. # double($env(SF_Y)) / double($rows)
  185. self.per_rl = float(self.SF_RL) / float(self.rasterinfo['rows'])
  186. # double($env(SF_RL)) / double($rows)
  187. self.per_cl = float(self.SF_CL) / float(self.rasterinfo['cols'])
  188. # double($env(SF_CL)) / double($cols)
  189. fil.write("SAMPLINGFRAME %r|%r|%r|%r\n" % (self.per_x, self.per_y,
  190. self.per_rl, self.per_cl))
  191. def _value_for_circle(self, radius):
  192. self.CIR_RL = round((2 * float(radius)) / float(self.rasterinfo['ewres']))
  193. self.CIR_CL = round((2 * float(radius)) / float(self.rasterinfo['nsres']))
  194. if not self.CIR_RL % 2:
  195. self.CIR_RL += 1
  196. if not self.CIR_CL % 2:
  197. self.CIR_CL += 1
  198. return
  199. def _circle(self, radius, mask):
  200. """!Create a circle mask"""
  201. self._value_for_circle(radius)
  202. eastEdge = float(self.SF_W + (self.CIR_RL * self.SF_EWRES))
  203. southEdge = float(self.SF_N - (self.CIR_CL * self.SF_NSRES))
  204. grass.del_temp_region()
  205. grass.use_temp_region()
  206. grass.run_command('g.region', n=self.SF_N, s=southEdge, e=eastEdge,
  207. w=self.SF_W)
  208. xcenter = grass.region(complete=True)['center_easting']
  209. ycenter = grass.region(complete=True)['center_northing']
  210. grass.run_command('r.circle', flags='b', out=mask, max=radius,
  211. coordinate=[xcenter, ycenter], quiet=True)
  212. grass.del_temp_region()
  213. grass.use_temp_region()
  214. grass.run_command('g.region', rast=self.startpage.rast)
  215. def getSamplingType(self):
  216. """!Obtain the sampling type"""
  217. devicetype = self.samplingareapage.regionbox
  218. samplingtype = self.samplingareapage.samplingtype
  219. shapetype = self.units.boxtype
  220. if samplingtype == SamplingType.UNITS:
  221. if devicetype == 'mouse':
  222. if shapetype == 'circle':
  223. samtype = SamplingType.MUNITSC
  224. else:
  225. samtype = SamplingType.MUNITSR
  226. elif devicetype == 'keyboard':
  227. if shapetype == 'circle':
  228. samtype = SamplingType.KUNITSC
  229. else:
  230. samtype = SamplingType.KUNITSR
  231. elif samplingtype == SamplingType.MVWIN:
  232. if devicetype == 'mouse':
  233. if shapetype == 'circle':
  234. samtype = SamplingType.MMVWINC
  235. else:
  236. samtype = SamplingType.MMVWINR
  237. elif devicetype == 'keyboard':
  238. if shapetype == 'circle':
  239. samtype = SamplingType.KMVWINC
  240. else:
  241. samtype = SamplingType.KMVWINR
  242. elif samplingtype == SamplingType.WHOLE:
  243. samtype = SamplingType.WHOLE
  244. elif samplingtype == SamplingType.REGIONS:
  245. samtype = SamplingType.REGIONS
  246. elif samplingtype == SamplingType.VECT:
  247. samtype = SamplingType.VECT
  248. else:
  249. samtype = samplingtype
  250. return samtype
  251. def _write_area(self, fil):
  252. """!Write the area according the type"""
  253. samtype = self.getSamplingType()
  254. #sampling type is whole
  255. if samtype == SamplingType.WHOLE:
  256. cl = float(self.SF_CL) / float(self.rasterinfo['cols'])
  257. rl = float(self.SF_RL) / float(self.rasterinfo['rows'])
  258. #this two variable are unused, problably to remove
  259. x = float(self.SF_X) / float(self.rasterinfo['cols'])
  260. y = float(self.SF_Y) / float(self.rasterinfo['rows'])
  261. fil.write("SAMPLEAREA %r|%r|%r|%r\n" % (self.per_x, self.per_y,
  262. rl, cl))
  263. ##KMWINC = samplingtype=moving, regionbox=keyboard, shape=circle
  264. elif samtype == SamplingType.KMVWINC:
  265. self._circle(self.moving.width, self.moving.height)
  266. cl = float(self.CIR_CL) / float(self.rasterinfo['cols'])
  267. rl = float(self.CIR_RL) / float(self.rasterinfo['rows'])
  268. fil.write("MASKEDSAMPLEAREA -1|-1|%r|%r" % (rl, cl))
  269. fil.write("|%s" % self.moving.height)
  270. fil.write("\nMOVINGWINDOW\n")
  271. ##KMWINR = samplingtype moving, regionbox=keyboard, shape=rectangle
  272. elif samtype == SamplingType.KMVWINR:
  273. cl = float(self.moving.width) / float(self.rasterinfo['cols'])
  274. rl = float(self.moving.height) / float(self.rasterinfo['rows'])
  275. fil.write("SAMPLEAREA -1|-1|%r|%r" % (rl, cl))
  276. fil.write("\nMOVINGWINDOW\n")
  277. ##MMVWINR = samplingtype moving, regionbox=mouse, shape=rectangle
  278. elif samtype == SamplingType.MMVWINR:
  279. cl = float(self.msAreaList[0]['cols']) / float(self.rasterinfo['cols'])
  280. rl = float(self.msAreaList[0]['rows']) / float(self.rasterinfo['rows'])
  281. fil.write("SAMPLEAREA -1|-1|%r|%r" % (rl, cl))
  282. fil.write("\nMOVINGWINDOW\n")
  283. ##MMVWINR = samplingtype moving, regionbox=mouse, shape=circle
  284. elif samtype == SamplingType.MMVWINC:
  285. self._value_for_circle(self.msAreaList[0].radius)
  286. cl = float(self.CIR_CL) / float(self.rasterinfo['cols'])
  287. rl = float(self.CIR_RL) / float(self.rasterinfo['rows'])
  288. fil.write("SAMPLEAREA -1|-1|%r|%r" % (rl, cl))
  289. fil.write("|%s" % self.msAreaList[0].raster)
  290. fil.write("\nMOVINGWINDOW\n")
  291. ##KUNITSC = samplingtype=units, regionbox=keyboard, shape=cirlce
  292. ##KUNITSR = samplingtype=units, regionbox=keyboard, shape=rectangle
  293. elif samtype == SamplingType.KUNITSC or samtype == SamplingType.KUNITSR:
  294. if samtype == SamplingType.KUNITSC:
  295. self._circle(self.units.width, self.units.height)
  296. cl = float(self.CIR_CL) / float(self.rasterinfo['cols'])
  297. rl = float(self.CIR_RL) / float(self.rasterinfo['rows'])
  298. else:
  299. cl = float(self.moving.width) / float(self.rasterinfo['cols'])
  300. rl = float(self.moving.height) / float(self.rasterinfo['rows'])
  301. fil.write("SAMPLEAREA -1|-1|%r|%r\n" % (rl, cl))
  302. if self.units.distrtype == 'non_overlapping':
  303. fil.write("RANDOMNONOVERLAPPING %s\n" % self.units.distr1)
  304. elif self.units.distrtype == 'systematic_contiguos':
  305. fil.write("SYSTEMATICCONTIGUOUS\n")
  306. elif self.units.distrtype == 'stratified_random':
  307. fil.write("STRATIFIEDRANDOM %s|%s\n" % (self.units.distr1,
  308. self.units.distr2))
  309. elif self.units.distrtype == 'systematic_noncontiguos':
  310. fil.write("SYSTEMATICNONCONTIGUOUS %s\n" % self.units.distr1)
  311. elif self.units.distrtype == 'centered_oversites':
  312. fil.write("")
  313. #elif self.samplingareapage.samplingtype == SamplingType.UNITS and self.samplingareapage.regionbox=='mouse':
  314. ##MUNITSC = samplingtype=units, regionbox=mouse, shape=cirlce
  315. ##MUNITSR = samplingtype=units, regionbox=mouse, shape=rectangle
  316. elif self.samplingareapage.samplingtype in [SamplingType.MUNITSR,
  317. SamplingType.MUNITSC]:
  318. # get the raster region into rastregion
  319. grass.use_temp_region()
  320. grass.run_command('g.region', rast=self.startpage.rast)
  321. rastregion = grass.region()
  322. s_n = rastregion['n']
  323. s_w = rastregion['w']
  324. rows = float(self.rasterinfo['rows'])
  325. cols = float(self.rasterinfo['cols'])
  326. for tregion in self.msAreaList:
  327. if self.samplingareapage.samplingtype == SamplingType.MUNITSC:
  328. tregion = tregion.region
  329. abs_y = abs(round((float(s_n) - tregion['n']) / tregion['nsres']))
  330. abs_x = abs(round((float(s_w) - tregion['w']) / tregion['ewres']))
  331. abs_rl = abs(round((tregion['n'] - tregion['s']) / tregion['nsres']))
  332. abs_cl = abs(round((tregion['e'] - tregion['w']) / tregion['ewres']))
  333. x = float(abs_x) / float(cols)
  334. y = float(abs_y) / float(rows)
  335. rl = float(abs_rl) / float(rows)
  336. cl = float(abs_cl) / float(cols)
  337. sarea = str(x) + "|" + str(y) + "|" + str(rl) + "|" + str(cl)
  338. if self.samplingareapage.samplingtype == SamplingType.MUNITSR:
  339. fil.write("SQUAREAREA %s\n" % sarea)
  340. elif self.samplingareapage.samplingtype == SamplingType.MUNITSC:
  341. fil.write("MASKEDSAMPLEAREA %s" % sarea)
  342. fil.write("|%s\n" % self.msAreaList[0].raster)
  343. elif self.samplingareapage.samplingtype == SamplingType.REGIONS:
  344. rows = float(self.rasterinfo['rows'])
  345. cols = float(self.rasterinfo['cols'])
  346. for marea in self.msAreaList:
  347. gregion = marea.region
  348. abs_y = self.SF_Y + abs(round((self.SF_N - gregion['n']) / self.SF_NSRES))
  349. abs_x = self.SF_X + abs(round((self.SF_W - gregion['w']) / self.SF_EWRES))
  350. abs_rl = abs(round(gregion['n'] - gregion['s']) / self.SF_NSRES)
  351. abs_cl = abs(round(gregion['e'] - gregion['w']) / self.SF_EWRES)
  352. x = float(abs_x) / float(cols)
  353. y = float(abs_y) / float(rows)
  354. rl = float(abs_rl) / float(rows)
  355. cl = float(abs_cl) / float(cols)
  356. maskArea = str(x) + "|" + str(y) + "|" + str(rl) + "|" + str(cl) + "|" + marea.raster
  357. fil.write("SQUAREAREA %s\n" % maskArea)
  358. elif self.samplingareapage.samplingtype == SamplingType.VECT:
  359. for marea in self.msAreaList:
  360. fil.write(marea)
  361. fil.write("RASTERMAP {name}\n".format(name=self.startpage.rast))
  362. fil.write("VECTORMAP {name}\n".format(name=self.startpage.vect))
  363. def _cleanup(self):
  364. """!Clean all the variables to save into configuration file"""
  365. self.startpage.conf_name = ''
  366. self.startpage.rast = ''
  367. self.startpage.vect = ''
  368. self.startpage.region = 'whole'
  369. self.keyboardpage.col_len = ''
  370. self.keyboardpage.col_up = ''
  371. self.keyboardpage.row_len = ''
  372. self.keyboardpage.row_up = ''
  373. self.samplingareapage.samplingtype = 'whole'
  374. self.units.width = ''
  375. self.units.height = ''
  376. self.units.boxtype = ''
  377. self.regions.numregions = 0
  378. self.moving.width = ''
  379. self.moving.height = ''
  380. self.moving.boxtype = ''
  381. class FirstPage(TitledPage):
  382. """
  383. !Set name of configuration file, choose raster and optionally vector/sites
  384. """
  385. def __init__(self, wizard, parent):
  386. TitledPage.__init__(self, wizard, _("Select maps and define name"))
  387. self.region = 'whole'
  388. self.rast = ''
  389. self.conf_name = ''
  390. self.vect = ''
  391. self.parent = parent
  392. #name of output configuration file
  393. self.newconflabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  394. label=_('Name for new configuration file to create'))
  395. self.newconftxt = wx.TextCtrl(parent=self, id=wx.ID_ANY,
  396. size=(250, -1))
  397. wx.CallAfter(self.newconftxt.SetFocus)
  398. self.sizer.Add(item=self.newconflabel, border=5, pos=(0, 0),
  399. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  400. self.sizer.Add(item=self.newconftxt, border=5, pos=(0, 1),
  401. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  402. #raster
  403. self.mapsellabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  404. label=_('Raster map to use to select areas'))
  405. self.mapselect = gselect.Select(parent=self, id=wx.ID_ANY,
  406. size=(250, -1), type='cell',
  407. multiple=False)
  408. self.sizer.Add(item=self.mapsellabel, border=5, pos=(1, 0),
  409. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  410. self.sizer.Add(item=self.mapselect, border=5, pos=(1, 1),
  411. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  412. #vector
  413. self.vectsellabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  414. label=_('Vector map to use to select areas'))
  415. self.vectselect = gselect.Select(parent=self, id=wx.ID_ANY,
  416. size=(250, -1), type='vector',
  417. multiple=False)
  418. self.sizer.Add(item=self.vectsellabel, border=5, pos=(2, 0),
  419. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  420. self.sizer.Add(item=self.vectselect, border=5, pos=(2, 1),
  421. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  422. #define sampling region
  423. self.sampling_reg = wx.RadioBox(parent=self, id=wx.ID_ANY,
  424. label=" %s " % _("Define sampling "
  425. "region (region for analysis)"),
  426. choices=[_('Whole map layer'),
  427. _('Keyboard setting'),
  428. _('Draw the sampling frame')],
  429. majorDimension=1,
  430. style=wx.RA_SPECIFY_ROWS)
  431. self.sizer.Add(item=self.sampling_reg,
  432. flag=wx.ALIGN_CENTER | wx.ALL | wx.EXPAND, border=5,
  433. pos=(4, 0), span=(1, 2))
  434. #bindings
  435. self.sampling_reg.Bind(wx.EVT_RADIOBOX, self.OnSampling)
  436. self.newconftxt.Bind(wx.EVT_KILL_FOCUS, self.OnName)
  437. self.newconftxt.Bind(wx.EVT_TEXT, self.OnNameChanged)
  438. self.vectselect.Bind(wx.EVT_TEXT, self.OnVector)
  439. self.mapselect.Bind(wx.EVT_TEXT, self.OnRast)
  440. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  441. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnExitPage)
  442. wx.CallAfter(wx.FindWindowById(wx.ID_FORWARD).Enable, False)
  443. def OnSampling(self, event):
  444. """!Change region type"""
  445. if event.GetInt() == 0:
  446. self.region = 'whole'
  447. self.SetNext(self.parent.samplingareapage)
  448. elif event.GetInt() == 1:
  449. self.region = 'key'
  450. self.SetNext(self.parent.keyboardpage)
  451. elif event.GetInt() == 2: # currently disabled
  452. self.region = 'draw'
  453. self.SetNext(self.parent.drawsampleframepage)
  454. def OnName(self, event):
  455. """!Sets the name of configuration file"""
  456. if self.conf_name in self.parent.parent.listfiles:
  457. GMessage(parent=self, message=_("The configuration file %s "
  458. "already exists, please change name") % self.conf_name)
  459. self.newconftxt.SetValue('')
  460. self.conf_name = ''
  461. def OnNameChanged(self, event):
  462. """!Name of configuration file has changed"""
  463. self.conf_name = self.newconftxt.GetValue()
  464. next = wx.FindWindowById(wx.ID_FORWARD)
  465. next.Enable(self.CheckInput())
  466. def OnRast(self, event):
  467. """!Sets raster map"""
  468. self.rast = event.GetString()
  469. next = wx.FindWindowById(wx.ID_FORWARD)
  470. next.Enable(self.CheckInput())
  471. def OnVector(self, event):
  472. """!Sets vector map"""
  473. self.vect = event.GetString()
  474. next = wx.FindWindowById(wx.ID_FORWARD)
  475. next.Enable(self.CheckInput())
  476. def OnEnterPage(self, event):
  477. """!Sets the default values, for the entire map
  478. """
  479. next = wx.FindWindowById(wx.ID_FORWARD)
  480. next.Enable(self.CheckInput())
  481. wx.CallAfter(wx.FindWindowById(wx.ID_FORWARD).Enable, self.CheckInput())
  482. def CheckInput(self):
  483. """!Check input fields.
  484. @return True if configuration file is given and raster xor vector map,
  485. False otherwise
  486. """
  487. #R#return bool(self.conf_name and (bool(self.rast) != bool(self.vect)))
  488. return bool(self.conf_name and bool(self.rast))
  489. def OnExitPage(self, event=None):
  490. """!Function during exiting"""
  491. next = wx.FindWindowById(wx.ID_FORWARD)
  492. next.Enable(self.CheckInput())
  493. if event.GetDirection():
  494. if self.region == 'key':
  495. self.SetNext(self.parent.keyboardpage)
  496. self.parent.samplingareapage.SetPrev(self.parent.keyboardpage)
  497. elif self.region == 'whole':
  498. self.SetNext(self.parent.samplingareapage)
  499. self.parent.samplingareapage.SetPrev(self)
  500. elif self.region == 'draw':
  501. self.SetNext(self.parent.drawsampleframepage)
  502. self.parent.samplingareapage.SetPrev(self.parent.drawsampleframepage)
  503. return
  504. class KeyboardPage(TitledPage):
  505. """
  506. !Choose the region setting the values of border using the keyboard
  507. """
  508. def __init__(self, wizard, parent):
  509. TitledPage.__init__(self, wizard, _("Insert sampling frame parameter"))
  510. self.parent = parent
  511. self.sizer.AddGrowableCol(2)
  512. self.col_len = ''
  513. self.row_len = ''
  514. self.col_up = '0'
  515. self.row_up = '0'
  516. #column up/left
  517. self.ColUpLeftlabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  518. label=_("Column of upper left "
  519. "corner"))
  520. self.ColUpLefttxt = wx.TextCtrl(parent=self, id=wx.ID_ANY,
  521. size=(250, -1))
  522. wx.CallAfter(self.ColUpLeftlabel.SetFocus)
  523. self.sizer.Add(item=self.ColUpLeftlabel, border=5, pos=(1, 1),
  524. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  525. self.sizer.Add(item=self.ColUpLefttxt, border=5, pos=(1, 2),
  526. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  527. #row up/left
  528. self.RowUpLeftlabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  529. label=_('Row of upper left corner'))
  530. self.RowUpLefttxt = wx.TextCtrl(parent=self, id=wx.ID_ANY,
  531. size=(250, -1))
  532. wx.CallAfter(self.RowUpLeftlabel.SetFocus)
  533. self.sizer.Add(item=self.RowUpLeftlabel, border=5, pos=(2, 1),
  534. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  535. self.sizer.Add(item=self.RowUpLefttxt, border=5, pos=(2, 2),
  536. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  537. #row lenght
  538. self.RowLenlabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  539. label=_('Row lenght of sampling frame'))
  540. self.RowLentxt = wx.TextCtrl(parent=self, id=wx.ID_ANY, size=(250, -1))
  541. wx.CallAfter(self.RowLenlabel.SetFocus)
  542. self.sizer.Add(item=self.RowLenlabel, border=5, pos=(3, 1),
  543. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  544. self.sizer.Add(item=self.RowLentxt, border=5, pos=(3, 2),
  545. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  546. #column lenght
  547. self.ColLenlabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  548. label=_('Row lenght of sampling frame'))
  549. self.ColLentxt = wx.TextCtrl(parent=self, id=wx.ID_ANY, size=(250, -1))
  550. wx.CallAfter(self.ColLenlabel.SetFocus)
  551. self.sizer.Add(item=self.ColLenlabel, border=5, pos=(4, 1),
  552. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  553. self.sizer.Add(item=self.ColLentxt, border=5, pos=(4, 2),
  554. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  555. self.ColUpLefttxt.SetValue(self.col_up)
  556. self.RowUpLefttxt.SetValue(self.row_up)
  557. self.ColUpLefttxt.Bind(wx.EVT_KILL_FOCUS, self.OnColLeft)
  558. self.RowUpLefttxt.Bind(wx.EVT_KILL_FOCUS, self.OnRowLeft)
  559. self.ColLentxt.Bind(wx.EVT_KILL_FOCUS, self.OnColLen)
  560. self.RowLentxt.Bind(wx.EVT_KILL_FOCUS, self.OnRowLen)
  561. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  562. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnExitPage)
  563. def OnColLeft(self, event):
  564. """!Sets the name of configuration file"""
  565. self.col_up = self.ColUpLefttxt.GetValue()
  566. checkValue(self.col_up)
  567. def OnRowLeft(self, event):
  568. """!Sets the name of configuration file"""
  569. self.row_up = self.RowUpLefttxt.GetValue()
  570. checkValue(self.row_up)
  571. def OnColLen(self, event):
  572. """!Sets the name of configuration file"""
  573. self.col_len = self.ColLentxt.GetValue()
  574. checkValue(self.col_len)
  575. def OnRowLen(self, event):
  576. """!Sets the name of configuration file"""
  577. self.row_len = self.RowLentxt.GetValue()
  578. checkValue(self.row_len)
  579. def OnEnterPage(self, event):
  580. """!Sets the default values, for the entire map
  581. """
  582. #R# check if raster exists before anything
  583. if self.col_len == '' and self.row_len == '':
  584. rastinfo = grast.raster_info(self.parent.startpage.rast)
  585. self.col_len = rastinfo['cols']
  586. self.row_len = rastinfo['rows']
  587. self.ColLentxt.SetValue(self.col_len)
  588. self.RowLentxt.SetValue(self.row_len)
  589. def OnExitPage(self, event=None):
  590. """!Function during exiting"""
  591. if self.row_len == '' or self.col_len == '' or self.row_up == '' or self.col_up == '':
  592. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  593. else:
  594. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  595. class DrawSampleFramePage(TitledPage):
  596. """!Choose the region setting the values drawing a box"""
  597. def __init__(self, wizard, parent):
  598. TitledPage.__init__(self, wizard, _("Draw sampling frame"))
  599. self.parent = parent
  600. self.mapPanel = None
  601. self.tregion = None
  602. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  603. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnExitPage)
  604. def SampleFrameChanged(self, region):
  605. """"!Enables the next dialog when region is set"""
  606. if region:
  607. self.tregion = region
  608. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  609. else:
  610. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  611. def OnEnterPage(self, event):
  612. """!Function during entering"""
  613. if self.mapPanel is None:
  614. self.mapPanel = RLiSetupMapPanel(self, samplingType='drawFrame')
  615. self.mapPanel.sampleFrameChanged.connect(self.SampleFrameChanged)
  616. self.sizer.Add(item=self.mapPanel, flag=wx.EXPAND, pos=(0, 0))
  617. self.sizer.AddGrowableCol(0)
  618. self.sizer.AddGrowableRow(0)
  619. self._raster = None
  620. else:
  621. self.mapPanel._region = {}
  622. self.SampleFrameChanged(region=None)
  623. rast = self.parent.startpage.rast
  624. if self._raster != rast:
  625. map_ = self.mapPanel.GetMap()
  626. map_.DeleteAllLayers()
  627. cmdlist = ['d.rast', 'map=%s' % rast]
  628. map_.AddLayer(ltype='raster', command=cmdlist, active=True,
  629. name=rast, hidden=False, opacity=1.0, render=True)
  630. def OnExitPage(self, event=None):
  631. """!Function during exiting"""
  632. if event.GetDirection():
  633. self.SetNext(self.parent.samplingareapage)
  634. self.parent.samplingareapage.SetPrev(self)
  635. else:
  636. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  637. class SamplingAreasPage(TitledPage):
  638. """
  639. Set name of configuration file, choose raster and optionally vector/sites.
  640. This is coming after choose the region
  641. """
  642. def __init__(self, wizard, parent):
  643. TitledPage.__init__(self, wizard, _("Insert sampling areas"))
  644. self.samplingtype = 'whole'
  645. self.parent = parent
  646. # toggles
  647. self.radioBox = wx.RadioBox(parent=self, id=wx.ID_ANY,
  648. label="",
  649. choices=[_("Whole map layer"),
  650. _("Regions"),
  651. _("Sample units"),
  652. _("Moving window"),
  653. _("Select areas from the\n"
  654. "overlayed vector map")],
  655. majorDimension=1,
  656. style=wx.RA_SPECIFY_COLS | wx.NO_BORDER)
  657. # layout
  658. self.sizer.SetVGap(10)
  659. self.sizer.Add(item=self.radioBox, flag=wx.ALIGN_LEFT, pos=(0, 0))
  660. self.regionBox = wx.RadioBox(parent=self, id=wx.ID_ANY,
  661. label=_("Choose a method"),
  662. choices=[_('Use keyboard to enter sampling area'),
  663. _('Use mouse to draw sampling area')],
  664. majorDimension=1,
  665. style=wx.RA_SPECIFY_ROWS)
  666. #self.regionBox.EnableItem(1, False)
  667. self.regionBox.SetItemToolTip(1, _("This option is not supported yet"))
  668. self.sizer.Add(self.regionBox, flag=wx.ALIGN_CENTER, pos=(1, 0))
  669. # bindings
  670. self.radioBox.Bind(wx.EVT_RADIOBOX, self.SetVal)
  671. self.regionBox.Bind(wx.EVT_RADIOBOX, self.OnRegionDraw)
  672. self.regionbox = 'keyboard'
  673. self.regionPanelSizer = wx.GridBagSizer(1, 2)
  674. self.regionNumPanel = wx.Panel(parent=self, id=wx.ID_ANY)
  675. self.regionNumLabel = wx.StaticText(parent=self.regionNumPanel,
  676. id=wx.ID_ANY,
  677. label=_('Number of regions to draw:'))
  678. self.regionNumTxt = wx.TextCtrl(parent=self.regionNumPanel,
  679. id=wx.ID_ANY, size=(50, -1))
  680. self.regionPanelSizer.Add(self.regionNumLabel, flag=wx.ALIGN_CENTER,
  681. pos=(0, 0))
  682. self.regionPanelSizer.Add(self.regionNumTxt, flag=wx.ALIGN_CENTER,
  683. pos=(0, 1))
  684. self.regionNumPanel.SetSizer(self.regionPanelSizer)
  685. self.sizer.Add(self.regionNumPanel, flag=wx.ALIGN_CENTER, pos=(2, 0))
  686. self.areaPanelSizer = wx.GridBagSizer(1, 3)
  687. self.areaPanel = wx.Panel(parent=self, id=wx.ID_ANY)
  688. self.areaText = wx.StaticText(parent=self.areaPanel, id=wx.ID_ANY,
  689. label=_('Do you want to check vector areas?'))
  690. self.areaOK = wx.Button(self.areaPanel, wx.ID_ANY, 'Yes', (50, 80))
  691. self.areaOK.SetToolTip(wx.ToolTip(_("Select if use area by area")))
  692. self.areaNO = wx.Button(self.areaPanel, wx.ID_ANY, 'No', (50, 80))
  693. self.areaNO.SetToolTip(wx.ToolTip(_("All the features will be used")))
  694. self.areaOK.Bind(wx.EVT_BUTTON, self.OnVectYes)
  695. self.areaNO.Bind(wx.EVT_BUTTON, self.OnVectNo)
  696. self.areaPanelSizer.Add(self.areaText, flag=wx.ALIGN_CENTER,
  697. pos=(0, 0))
  698. self.areaPanelSizer.Add(self.areaOK, flag=wx.ALIGN_CENTER, pos=(0, 1))
  699. self.areaPanelSizer.Add(self.areaNO, flag=wx.ALIGN_CENTER, pos=(0, 2))
  700. self.areaPanel.SetSizer(self.areaPanelSizer)
  701. self.sizer.Add(self.areaPanel, flag=wx.ALIGN_CENTER, pos=(3, 0))
  702. self.calculatingAreas = wx.StaticText(parent=self, id=wx.ID_ANY,
  703. label=_('Analysing all vector features...'))
  704. self.sizer.Add(self.calculatingAreas, flag=wx.ALIGN_CENTER, pos=(4, 0))
  705. self.numregions = ''
  706. self.regionNumTxt.Bind(wx.EVT_TEXT, self.OnNumRegions)
  707. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  708. def OnNumRegions(self, event):
  709. """!Obtain the number of regions"""
  710. if event.GetString():
  711. self.SetNext(self.parent.regions)
  712. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  713. self.numregions = event.GetString()
  714. else:
  715. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  716. def OnEnterPage(self, event):
  717. """!Insert values into text controls for summary of location
  718. creation options
  719. """
  720. self.SetVal(None)
  721. if self.parent.startpage.vect:
  722. self.radioBox.ShowItem(4, True)
  723. self.vect_data = []
  724. else:
  725. self.radioBox.ShowItem(4, False)
  726. self.sizer.Layout()
  727. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  728. def SetVal(self, event):
  729. """!Choose method"""
  730. radio = self.radioBox.GetSelection()
  731. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  732. if radio == 0:
  733. self.samplingtype = SamplingType.WHOLE
  734. self.DrawNothing()
  735. elif radio == 1:
  736. self.samplingtype = SamplingType.REGIONS
  737. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  738. elif radio == 2:
  739. self.samplingtype = SamplingType.UNITS
  740. regtype = self.regionBox.GetSelection()
  741. self.RegionDraw(regtype)
  742. elif radio == 3:
  743. self.samplingtype = SamplingType.MVWIN
  744. regtype = self.regionBox.GetSelection()
  745. self.RegionDraw(regtype)
  746. elif radio == 4:
  747. self.samplingtype = SamplingType.VECT
  748. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  749. self.ShowExtraOptions(self.samplingtype)
  750. def ShowExtraOptions(self, samtype):
  751. """!Show some extra options for some sampling type"""
  752. if samtype == SamplingType.REGIONS:
  753. self.sizer.Hide(self.regionBox)
  754. self.sizer.Hide(self.areaPanel)
  755. self.sizer.Hide(self.calculatingAreas)
  756. self.sizer.Show(self.regionNumPanel)
  757. elif samtype == SamplingType.UNITS or samtype == SamplingType.MVWIN:
  758. self.sizer.Hide(self.regionNumPanel)
  759. self.sizer.Hide(self.areaPanel)
  760. self.sizer.Hide(self.calculatingAreas)
  761. self.sizer.Show(self.regionBox)
  762. elif samtype == SamplingType.VECT:
  763. self.sizer.Hide(self.regionBox)
  764. self.sizer.Hide(self.regionNumPanel)
  765. self.sizer.Hide(self.calculatingAreas)
  766. self.sizer.Show(self.areaPanel)
  767. self.sizer.Layout()
  768. def OnRegionDraw(self, event):
  769. self.RegionDraw(event.GetInt())
  770. return
  771. def OnVectYes(self, event):
  772. """!The user choose to select the vector areas, this function set the
  773. next page to VectorAreasPage"""
  774. self.SetNext(self.parent.vectorareas)
  775. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  776. self.parent.wizard.ShowPage(self.parent.vectorareas)
  777. def OnVectNo(self, event):
  778. """!The user choose to use all the vector areas, this function analyses
  779. all the vector areas and fill the msAreaList variable"""
  780. self.sizer.Show(self.calculatingAreas)
  781. self.sizer.Hide(self.regionNumPanel)
  782. self.sizer.Hide(self.regionBox)
  783. self.sizer.Hide(self.areaPanel)
  784. self.SetNext(self.parent.summarypage)
  785. vect_cats = sorted(set(grass.parse_command('v.category', input=self.parent.startpage.vect,
  786. option='print',
  787. type='centroid').keys()))
  788. self._progressDlg = wx.ProgressDialog(title=_("Analysing vector"),
  789. message="Analysing vector",
  790. maximum=len(vect_cats),
  791. parent=self,
  792. style=wx.PD_CAN_ABORT | wx.PD_APP_MODAL |
  793. wx.PD_AUTO_HIDE | wx.PD_SMOOTH)
  794. self._progressDlgMax = len(vect_cats)
  795. grass.use_temp_region()
  796. self.parent.msAreaList = sampleAreaVector(self.parent.startpage.vect,
  797. self.parent.startpage.rast,
  798. vect_cats,
  799. self._progressDlg)
  800. grass.del_temp_region()
  801. if self.parent.msAreaList:
  802. self.calculatingAreas.SetLabel(_("All feature are been analyzed."))
  803. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  804. self.parent.wizard.ShowPage(self.parent.summarypage)
  805. else:
  806. self.calculatingAreas.SetLabel(_("An error occurred"))
  807. self._progressDlg.Destroy()
  808. self._progressDlg = None
  809. def RegionDraw(self, regtype):
  810. """!Set the next page to units or drawunits"""
  811. if regtype == 0:
  812. self.regionbox = 'keyboard'
  813. if self.samplingtype == SamplingType.UNITS:
  814. self.SetNext(self.parent.units)
  815. elif self.samplingtype == SamplingType.MVWIN:
  816. self.SetNext(self.parent.moving)
  817. elif regtype == 1:
  818. self.regionbox = 'mouse'
  819. self.SetNext(self.parent.drawunits)
  820. def DrawNothing(self):
  821. """Remove all the optional choices. Used also when the wizard enter in
  822. SamplingAreasPage, all the optional choices should be hide here"""
  823. self.sizer.Hide(self.regionNumPanel)
  824. self.sizer.Hide(self.regionBox)
  825. self.sizer.Hide(self.areaPanel)
  826. self.sizer.Hide(self.calculatingAreas)
  827. self.sizer.Layout()
  828. self.SetNext(self.parent.summarypage)
  829. class DrawRegionsPage(TitledPage):
  830. def __init__(self, wizard, parent):
  831. self.parent = parent
  832. TitledPage.__init__(self, wizard, _("Draw sampling regions"))
  833. self.regioncount = 0
  834. self.mapPanel = None
  835. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  836. #self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnExitPage)
  837. def afterRegionDrawn(self, marea):
  838. if marea:
  839. self.parent.msAreaList.append(marea)
  840. self.regioncount = self.regioncount + 1
  841. numregions = int(self.parent.samplingareapage.numregions)
  842. if self.regioncount > numregions:
  843. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  844. self.parent.wizard.ShowPage(self.parent.summarypage)
  845. else:
  846. self.title.SetLabel(_('Draw sample region ' +
  847. str(self.regioncount) + ' of ' +
  848. str(numregions)))
  849. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  850. def OnEnterPage(self, event):
  851. """!Function during entering"""
  852. if self.parent.samplingareapage.samplingtype == SamplingType.WHOLE:
  853. self.title.SetLabel(_("Draw moving windows region"))
  854. elif self.parent.samplingareapage.samplingtype == SamplingType.UNITS:
  855. self.title.SetLabel(_("Draw sampling region"))
  856. if self.mapPanel is None:
  857. self.mapPanel = RLiSetupMapPanel(self,
  858. samplingType=self.parent.samplingareapage.samplingtype,
  859. )
  860. self.mapPanel.afterRegionDrawn.connect(self.afterRegionDrawn)
  861. self.sizer.Add(item=self.mapPanel, flag=wx.EXPAND, pos=(1, 0))
  862. self.sizer.AddGrowableCol(0)
  863. self.sizer.AddGrowableRow(1)
  864. self._raster = None
  865. rast = self.parent.startpage.rast
  866. self.afterRegionDrawn(marea=None)
  867. if self._raster != rast:
  868. map_ = self.mapPanel.GetMap()
  869. map_.DeleteAllLayers()
  870. cmdlist = ['d.rast', 'map=%s' % rast]
  871. map_.AddLayer(ltype='raster', command=cmdlist, active=True,
  872. name=rast, hidden=False, opacity=1.0, render=True)
  873. #def OnExitPage(self, event=None):
  874. #!Function during exiting
  875. #print event.GetDirection()
  876. #if event.GetDirection():
  877. # self.SetNext(self.parent.samplingareapage)
  878. # self.parent.samplingareapage.SetPrev(self)
  879. class SampleUnitsKeyPage(TitledPage):
  880. """!Set values from keyboard for sample units
  881. It is used if you choose keyboard from Sampling Units or Moving windows
  882. in sampling areas page
  883. """
  884. def __init__(self, wizard, parent):
  885. TitledPage.__init__(self, wizard, _("Select sample units from keyboard"))
  886. self.parent = parent
  887. self.scrollPanel = scrolled.ScrolledPanel(parent=self, id=wx.ID_ANY)
  888. self.scrollPanel.SetupScrolling(scroll_x=False)
  889. self.panelSizer = wx.GridBagSizer(5, 5)
  890. self.width = ''
  891. self.height = ''
  892. self.boxtype = 'rectangle'
  893. self.distrtype = 'non_overlapping'
  894. # type of shape
  895. self.typeBox = wx.RadioBox(parent=self.scrollPanel, id=wx.ID_ANY,
  896. majorDimension=1, style=wx.RA_SPECIFY_COLS,
  897. label=" %s " % _("Select type of shape"),
  898. choices=[_('Rectangle'), _('Circle'),
  899. ('None')])
  900. self.panelSizer.Add(self.typeBox, flag=wx.ALIGN_LEFT, pos=(0, 0),
  901. span=(1, 2))
  902. self.widthLabel = wx.StaticText(parent=self.scrollPanel, id=wx.ID_ANY)
  903. self.widthTxt = wx.TextCtrl(parent=self.scrollPanel, id=wx.ID_ANY,
  904. size=(250, -1))
  905. self.panelSizer.Add(item=self.widthLabel, pos=(1, 0),
  906. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  907. self.panelSizer.Add(item=self.widthTxt, pos=(1, 1),
  908. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  909. self.heightLabel = wx.StaticText(parent=self.scrollPanel, id=wx.ID_ANY)
  910. self.heightTxt = wx.TextCtrl(parent=self.scrollPanel, id=wx.ID_ANY,
  911. size=(250, -1))
  912. self.panelSizer.Add(item=self.heightLabel, pos=(2, 0),
  913. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  914. self.panelSizer.Add(item=self.heightTxt, pos=(2, 1),
  915. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  916. self.widthLabels = [_('Width size (in cells)? '),
  917. _('What radius size (in meters)? ')]
  918. self.heightLabels = [_('Height size (in cells)? '),
  919. _('Name of the circle mask')]
  920. self.distributionBox = wx.RadioBox(parent=self.scrollPanel,
  921. id=wx.ID_ANY,
  922. majorDimension=1,
  923. style=wx.RA_SPECIFY_COLS,
  924. label= " %s " % _("Select method of sampling unit distribution"),
  925. choices=[_('Random non overlapping'),
  926. _('Systematic contiguos'),
  927. _('Stratified random'),
  928. _('Systematic non contiguos'),
  929. _('Centered over sites')]
  930. )
  931. self.panelSizer.Add(item=self.distributionBox, pos=(3, 0), span=(1, 2),
  932. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  933. self.distr1Label = wx.StaticText(parent=self.scrollPanel, id=wx.ID_ANY,
  934. label=_("What number of Sampling "
  935. "Units to use?"))
  936. self.distr1Txt = wx.TextCtrl(parent=self.scrollPanel, id=wx.ID_ANY,
  937. size=(250, -1))
  938. self.panelSizer.Add(item=self.distr1Label, pos=(4, 0),
  939. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  940. self.panelSizer.Add(item=self.distr1Txt, pos=(4, 1),
  941. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  942. self.distr2Label = wx.StaticText(parent=self.scrollPanel, id=wx.ID_ANY,
  943. label="")
  944. self.distr2Txt = wx.TextCtrl(parent=self.scrollPanel, id=wx.ID_ANY,
  945. size=(250, -1))
  946. self.panelSizer.Add(item=self.distr2Label, pos=(5, 0),
  947. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  948. self.panelSizer.Add(item=self.distr2Txt, pos=(5, 1),
  949. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  950. self.panelSizer.Hide(self.distr2Txt)
  951. self.typeBox.Bind(wx.EVT_RADIOBOX, self.OnType)
  952. self.distributionBox.Bind(wx.EVT_RADIOBOX, self.OnDistr)
  953. self.widthTxt.Bind(wx.EVT_TEXT, self.OnWidth)
  954. self.heightTxt.Bind(wx.EVT_TEXT, self.OnHeight)
  955. self.distr1Txt.Bind(wx.EVT_TEXT, self.OnDistr1)
  956. self.distr2Txt.Bind(wx.EVT_TEXT, self.OnDistr2)
  957. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  958. self.sizer.Add(item=self.scrollPanel, pos=(0, 0), flag=wx.EXPAND)
  959. self.sizer.AddGrowableCol(0)
  960. self.sizer.AddGrowableRow(0)
  961. self.scrollPanel.SetSizer(self.panelSizer)
  962. #self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnExitPage)
  963. self.OnType(None)
  964. def OnEnterPage(self, event=None):
  965. """!Function during entering"""
  966. # This is an hack to force the user to choose Rectangle or Circle
  967. self.typeBox.SetSelection(2),
  968. self.typeBox.ShowItem(2, False)
  969. self.panelSizer.Layout()
  970. def OnExitPage(self, event=None):
  971. """!Function during exiting"""
  972. if event.GetDirection():
  973. self.SetNext(self.parent.summarypage)
  974. self.SetPrev(self.parent.samplingareapage)
  975. def OnType(self, event):
  976. """!Set if rectangle or circle will be used"""
  977. chosen = self.typeBox.GetSelection()
  978. self.widthLabel.SetLabel(self.widthLabels[chosen])
  979. self.heightLabel.SetLabel(self.heightLabels[chosen])
  980. self.panelSizer.Layout()
  981. if chosen == 0:
  982. self.boxtype = 'rectangle'
  983. else:
  984. self.boxtype = 'circle'
  985. def OnDistr(self, event):
  986. chosen = self.distributionBox.GetSelection()
  987. if chosen == 0:
  988. self.distrtype = 'non_overlapping'
  989. self.distr1Label.SetLabel(_("What number of Sampling Units to use?"))
  990. self.panelSizer.Show(self.distr1Txt)
  991. self.distr2Label.SetLabel("")
  992. self.panelSizer.Hide(self.distr2Txt)
  993. self.panelSizer.Layout()
  994. elif chosen == 1:
  995. self.distrtype = 'systematic_contiguos'
  996. self.distr1Label.SetLabel("")
  997. self.distr2Label.SetLabel("")
  998. self.panelSizer.Hide(self.distr1Txt)
  999. self.panelSizer.Hide(self.distr2Txt)
  1000. self.panelSizer.Layout()
  1001. elif chosen == 2:
  1002. self.distrtype = 'stratified_random'
  1003. self.distr1Label.SetLabel(_("Insert number of row strates"))
  1004. self.distr2Label.SetLabel(_("Insert number of column strates"))
  1005. self.panelSizer.Show(self.distr1Txt)
  1006. self.panelSizer.Show(self.distr2Txt)
  1007. self.panelSizer.Layout()
  1008. elif chosen == 3:
  1009. self.distrtype = 'systematic_noncontiguos'
  1010. self.distr1Label.SetLabel(_("Insert distance between units"))
  1011. self.panelSizer.Show(self.distr1Txt)
  1012. self.distr2Label.SetLabel("")
  1013. self.panelSizer.Hide(self.distr2Txt)
  1014. self.panelSizer.Layout()
  1015. elif chosen == 4:
  1016. self.distrtype = 'centered_oversites'
  1017. self.distr1Label.SetLabel("")
  1018. self.distr2Label.SetLabel("")
  1019. self.panelSizer.Hide(self.distr2Txt)
  1020. self.panelSizer.Hide(self.distr1Txt)
  1021. self.panelSizer.Layout()
  1022. def OnWidth(self, event):
  1023. self.width = event.GetString()
  1024. def OnHeight(self, event):
  1025. self.height = event.GetString()
  1026. def OnDistr1(self, event):
  1027. self.distr1 = event.GetString()
  1028. def OnDistr2(self, event):
  1029. self.distr2 = event.GetString()
  1030. class MovingKeyPage(TitledPage):
  1031. """!Set values from keyboard for sample units"""
  1032. def __init__(self, wizard, parent):
  1033. TitledPage.__init__(self, wizard, _("Set sample units"))
  1034. self.parent = parent
  1035. self.sizer.AddGrowableCol(2)
  1036. self.width = ''
  1037. self.height = ''
  1038. self.boxtype = 'rectangle'
  1039. # type of shape
  1040. self.typeBox = wx.RadioBox(parent=self, id=wx.ID_ANY,
  1041. label=" %s " % _("Select type of shape"),
  1042. choices=[_('Rectangle'), _('Circle')],
  1043. # ('None')],
  1044. majorDimension=1,
  1045. style=wx.RA_SPECIFY_COLS)
  1046. self.sizer.Add(self.typeBox, flag=wx.ALIGN_LEFT, pos=(1, 1))
  1047. self.typeBox.Bind(wx.EVT_RADIOBOX, self.OnType)
  1048. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1049. self.widthLabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1050. label=_('Width size (in cells) ?'))
  1051. self.widthTxt = wx.TextCtrl(parent=self, id=wx.ID_ANY, size=(250, -1))
  1052. self.sizer.Add(item=self.widthLabel, border=5, pos=(2, 1),
  1053. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1054. self.sizer.Add(item=self.widthTxt, border=5, pos=(2, 2),
  1055. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1056. self.heightLabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1057. label=_('Height size (in cells) ?'))
  1058. self.heightTxt = wx.TextCtrl(parent=self, id=wx.ID_ANY, size=(250, -1))
  1059. self.sizer.Add(item=self.heightLabel, border=5, pos=(3, 1),
  1060. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1061. self.sizer.Add(item=self.heightTxt, border=5, pos=(3, 2),
  1062. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1063. self.widthLabels = [_('Width size (in cells) ?'),
  1064. _('What radius size (in meters) ?')]
  1065. self.heightLabels = [_('Height size (in cells) ?'),
  1066. _('Name of the circle mask')]
  1067. self.widthTxt.Bind(wx.EVT_TEXT, self.OnWidth)
  1068. self.heightTxt.Bind(wx.EVT_TEXT, self.OnHeight)
  1069. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1070. def OnEnterPage(self, event):
  1071. # This is an hack to force the user to choose Rectangle or Circle
  1072. # self.typeBox.SetSelection(2),
  1073. # self.typeBox.ShowItem(2, False)
  1074. if self.parent.samplingareapage.samplingtype == SamplingType.MVWIN:
  1075. self.title.SetLabel(_("Set moving windows"))
  1076. self.OnType(None)
  1077. def OnType(self, event):
  1078. chosen = self.typeBox.GetSelection()
  1079. self.widthLabel.SetLabel(self.widthLabels[chosen])
  1080. self.heightLabel.SetLabel(self.heightLabels[chosen])
  1081. self.sizer.Layout()
  1082. if chosen == 0:
  1083. if self.parent.samplingareapage.samplingtype == SamplingType.MVWIN:
  1084. self.parent.samplingareapage.samplingtype = SamplingType.KMVWINR
  1085. else:
  1086. self.parent.samplingareapage.samplingtype = SamplingType.KMVWINC
  1087. self.boxtype = 'rectangle'
  1088. elif chosen == 1:
  1089. if self.parent.samplingareapage.samplingtype == SamplingType.MVWIN:
  1090. self.parent.samplingareapage.samplingtype = SamplingType.KMVWINC
  1091. else:
  1092. self.parent.samplingareapage.samplingtype = SamplingType.MMVWINC
  1093. self.boxtype = 'circle'
  1094. def CheckInput(self):
  1095. """!Check input fields.
  1096. @return True if configuration file is given and raster xor vector map,
  1097. False otherwise
  1098. """
  1099. return bool(self.width and bool(self.height))
  1100. def OnWidth(self, event):
  1101. self.width = event.GetString()
  1102. next = wx.FindWindowById(wx.ID_FORWARD)
  1103. next.Enable(self.CheckInput())
  1104. def OnHeight(self, event):
  1105. self.height = event.GetString()
  1106. next = wx.FindWindowById(wx.ID_FORWARD)
  1107. next.Enable(self.CheckInput())
  1108. class UnitsMousePage(TitledPage):
  1109. """!Choose the sampling area setting the values using the mouse drawing the
  1110. areas with rectangle or circle. It is used both from 'Moving windows'
  1111. and 'Sample units'.
  1112. """
  1113. def __init__(self, wizard, parent):
  1114. self.parent = parent
  1115. self.wizard = wizard
  1116. TitledPage.__init__(self, self.wizard, _("Draw sampling units"))
  1117. self.numregions = ''
  1118. self.mapPanel = None
  1119. # type of shape
  1120. self.typeBox = wx.RadioBox(parent=self, id=wx.ID_ANY,
  1121. majorDimension=1, style=wx.RA_SPECIFY_COLS,
  1122. label=" %s " % _("Select type of shape"),
  1123. choices=[_('Rectangle'), _('Circle'), ('')])
  1124. # This is an hack to force the user to choose Rectangle or Circle
  1125. self.typeBox.SetSelection(2),
  1126. self.typeBox.ShowItem(2, False)
  1127. self.sizer.Add(self.typeBox, flag=wx.ALIGN_LEFT, pos=(0, 0),
  1128. span=(1, 2))
  1129. self.regionPanelSizer = wx.GridBagSizer(1, 2)
  1130. self.regionNumPanel = wx.Panel(parent=self, id=wx.ID_ANY)
  1131. self.regionNumLabel = wx.StaticText(parent=self.regionNumPanel,
  1132. id=wx.ID_ANY,
  1133. label=_('Number of sampling area to draw:'))
  1134. self.regionNumTxt = wx.TextCtrl(parent=self.regionNumPanel,
  1135. id=wx.ID_ANY, size=(50, -1))
  1136. self.regionPanelSizer.Add(self.regionNumLabel, flag=wx.ALIGN_CENTER,
  1137. pos=(0, 0))
  1138. self.regionPanelSizer.Add(self.regionNumTxt, flag=wx.ALIGN_CENTER,
  1139. pos=(0, 1))
  1140. self.regionNumPanel.SetSizer(self.regionPanelSizer)
  1141. self.sizer.Add(self.regionNumPanel, flag=wx.ALIGN_LEFT, pos=(1, 0),
  1142. span=(1, 2))
  1143. self.typeBox.Bind(wx.EVT_RADIOBOX, self.OnType)
  1144. self.regionNumTxt.Bind(wx.EVT_TEXT, self.OnNumRegions)
  1145. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1146. self.sizer.AddGrowableCol(0)
  1147. self.OnType(None)
  1148. self.regionNumTxt.SetValue('')
  1149. def OnEnterPage(self, event):
  1150. """!Function during entering"""
  1151. if self.numregions:
  1152. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1153. else:
  1154. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1155. if self.parent.samplingareapage.samplingtype in [SamplingType.MVWIN,
  1156. SamplingType.MMVWINR,
  1157. SamplingType.MMVWINC]:
  1158. self.title.SetLabel(_("Draw moving windows region"))
  1159. self.sizer.Hide(self.regionNumPanel)
  1160. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1161. elif self.parent.samplingareapage.samplingtype in [SamplingType.UNITS,
  1162. SamplingType.MUNITSR,
  1163. SamplingType.MUNITSC]:
  1164. self.title.SetLabel(_("Draw sampling region"))
  1165. self.sizer.Show(self.regionNumPanel)
  1166. if self.typeBox.GetSelection() == 2:
  1167. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1168. self.sizer.Layout()
  1169. def OnType(self, event):
  1170. chosen = self.typeBox.GetSelection()
  1171. if chosen == 0:
  1172. if self.parent.samplingareapage.samplingtype in [SamplingType.MVWIN,
  1173. SamplingType.MMVWINR,
  1174. SamplingType.MMVWINC]:
  1175. self.parent.samplingareapage.samplingtype = SamplingType.MMVWINR
  1176. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1177. else:
  1178. self.parent.samplingareapage.samplingtype = SamplingType.MUNITSR
  1179. self.drawtype = 'rectangle'
  1180. else:
  1181. if self.parent.samplingareapage.samplingtype in [SamplingType.MVWIN,
  1182. SamplingType.MMVWINR,
  1183. SamplingType.MMVWINC]:
  1184. self.parent.samplingareapage.samplingtype = SamplingType.MMVWINC
  1185. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1186. else:
  1187. self.parent.samplingareapage.samplingtype = SamplingType.MUNITSC
  1188. self.drawtype = 'circle'
  1189. def OnNumRegions(self, event):
  1190. """!Set the number of region"""
  1191. if event.GetString():
  1192. self.SetNext(self.parent.drawsampleunitspage)
  1193. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1194. self.numregions = event.GetString()
  1195. else:
  1196. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1197. def OnExitPage(self, event=None):
  1198. """!Function during exiting"""
  1199. if event.GetDirection():
  1200. self.SetNext(self.drawsampleunitspage)
  1201. self.SetPrev(self.samplingareapage)
  1202. class DrawSampleUnitsPage(TitledPage):
  1203. """!Choose the sampling area drawing them"""
  1204. def __init__(self, wizard, parent):
  1205. TitledPage.__init__(self, wizard, _("Draw sampling units"))
  1206. self.parent = parent
  1207. self.mapPanel = None
  1208. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1209. #self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnExitPage)
  1210. def SampleFrameChanged(self, region):
  1211. #region = self.GetSampleUnitRegion()
  1212. if region:
  1213. self.parent.msAreaList.append(region)
  1214. self.regioncount = self.regioncount + 1
  1215. drawtype = self.parent.drawunits.drawtype
  1216. if self.regioncount > self.numregions:
  1217. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1218. self.parent.wizard.ShowPage(self.parent.summarypage)
  1219. else:
  1220. self.title.SetLabel(_('Draw Sampling ' + drawtype + ' ' \
  1221. + str(self.regioncount) + ' of ' \
  1222. + str(self.numregions)))
  1223. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1224. def OnEnterPage(self, event):
  1225. """!Function during entering"""
  1226. if self.parent.samplingareapage.samplingtype in [SamplingType.MVWIN,
  1227. SamplingType.MMVWINC,
  1228. SamplingType.MMVWINR]:
  1229. self.numregions = 1
  1230. else:
  1231. self.numregions = int(self.parent.drawunits.numregions)
  1232. self.regioncount = 0
  1233. if self.mapPanel:
  1234. self.sizer.Remove(self.mapPanel)
  1235. gtype = self.parent.drawunits.drawtype
  1236. self.mapPanel = RLiSetupMapPanel(self,
  1237. samplingType=self.parent.samplingareapage.samplingtype,
  1238. )
  1239. if gtype == 'circle':
  1240. self.mapPanel.afterCircleDrawn.connect(self.SampleFrameChanged)
  1241. else:
  1242. self.mapPanel.sampleFrameChanged.connect(self.SampleFrameChanged)
  1243. self.sizer.Add(item=self.mapPanel, flag=wx.EXPAND, pos=(1, 0))
  1244. self.sizer.AddGrowableCol(0)
  1245. self.sizer.AddGrowableRow(1)
  1246. self._raster = None
  1247. self.SampleFrameChanged(region=None)
  1248. rast = self.parent.startpage.rast
  1249. if self._raster != rast:
  1250. map_ = self.mapPanel.GetMap()
  1251. map_.DeleteAllLayers()
  1252. cmdlist = ['d.rast', 'map=%s' % rast]
  1253. map_.AddLayer(ltype='raster', command=cmdlist, active=True,
  1254. name=rast, hidden=False, opacity=1.0, render=True)
  1255. def OnExitPage(self, event=None):
  1256. #!Function during exiting
  1257. print event.GetDirection()
  1258. #if event.GetDirection():
  1259. # self.SetNext(self.parent.samplingareapage)
  1260. # self.parent.samplingareapage.SetPrev(self)
  1261. class VectorAreasPage(TitledPage):
  1262. """!Choose the sampling area using the vector features"""
  1263. def __init__(self, wizard, parent):
  1264. TitledPage.__init__(self, wizard, _("Select sampling areas"))
  1265. self.parent = parent
  1266. self.areascount = 0
  1267. self.mapPanel = None
  1268. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1269. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnExitPage)
  1270. self.areaPanelSizer = wx.GridBagSizer(1, 3)
  1271. self.areaPanel = wx.Panel(parent=self, id=wx.ID_ANY)
  1272. self.areaText = wx.StaticText(parent=self.areaPanel, id=wx.ID_ANY,
  1273. label=_('Is this area ok?'))
  1274. self.areaOK = wx.Button(self.areaPanel, wx.ID_ANY, 'Yes', (50, 80))
  1275. self.areaNO = wx.Button(self.areaPanel, wx.ID_ANY, 'No', (50, 80))
  1276. self.areaOK.Bind(wx.EVT_BUTTON, self.OnYes)
  1277. self.areaNO.Bind(wx.EVT_BUTTON, self.OnNo)
  1278. self.areaPanelSizer.Add(self.areaText, flag=wx.ALIGN_CENTER,
  1279. pos=(0, 0))
  1280. self.areaPanelSizer.Add(self.areaOK, flag=wx.ALIGN_CENTER,
  1281. pos=(0, 1))
  1282. self.areaPanelSizer.Add(self.areaNO, flag=wx.ALIGN_CENTER,
  1283. pos=(0, 2))
  1284. self.areaPanel.SetSizer(self.areaPanelSizer)
  1285. self.sizer.Add(self.areaPanel, flag=wx.ALIGN_CENTER, pos=(2, 0))
  1286. def afterRegionDrawn(self):
  1287. """!Function to update the title and the number of selected area"""
  1288. self.areascount = self.areascount + 1
  1289. if self.areascount == self.areanum:
  1290. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1291. return True
  1292. else:
  1293. self.title.SetLabel(_('Select sample area ' + str(self.areascount + 1) \
  1294. + ' of ' + str(self.areanum)))
  1295. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1296. return False
  1297. def OnYes(self, event):
  1298. """!Function to create the string for the conf file if the area
  1299. is selected"""
  1300. self.parent.msAreaList.append(obtainAreaVector(self.outname))
  1301. if not self.afterRegionDrawn():
  1302. self.newCat()
  1303. def OnNo(self, event):
  1304. """!Function to pass to the next feature if it is not selected"""
  1305. if not self.afterRegionDrawn():
  1306. self.newCat()
  1307. def newCat(self):
  1308. """!Convert to raster and draw the new feature"""
  1309. cat = self.vect_cats[self.areascount]
  1310. self.outname = "{name}_{cat}".format(name=self.vect.split('@')[0],
  1311. cat=cat)
  1312. convertFeature(self.vect, self.outname, cat, self.rast)
  1313. cmdlistcat = ['d.rast', 'map=%s' % self.outname]
  1314. self.map_.AddLayer(ltype='raster', command=cmdlistcat, active=True,
  1315. name=self.outname, hidden=False, opacity=1.0,
  1316. render=True)
  1317. for l in self.map_.GetListOfLayers():
  1318. if l.name == self.outname:
  1319. self.mapPanel.mapWindow.ZoomToMap(layers=[l], render=True,
  1320. ignoreNulls=True)
  1321. elif l.name != self.rast:
  1322. self.map_.DeleteLayer(l)
  1323. self.areaText.SetLabel("Is this area (cat={n}) ok?".format(n=cat))
  1324. def OnEnterPage(self, event):
  1325. """!Function during entering: draw the raster map and the first vector
  1326. feature"""
  1327. if self.mapPanel is None:
  1328. self.mapPanel = RLiSetupMapPanel(self, samplingType=self.parent.samplingareapage.samplingtype,
  1329. graphicsType="line")
  1330. self.sizer.Add(item=self.mapPanel, flag=wx.EXPAND, pos=(1, 0))
  1331. self.sizer.AddGrowableCol(0)
  1332. self.sizer.AddGrowableRow(1)
  1333. self._raster = None
  1334. self.rast = self.parent.startpage.rast
  1335. self.vect = self.parent.startpage.vect
  1336. self.vect_cats = sorted(set(grass.parse_command('v.category',
  1337. input=self.vect,
  1338. type='centroid',
  1339. option='print').keys()))
  1340. self.areanum = len(self.vect_cats)
  1341. if self.areanum == 0:
  1342. GError(parent=self, message=_("The polygon seems to have 0 areas"))
  1343. self.parent.wizard.ShowPage(self.parent.samplingareapage)
  1344. return
  1345. self.title.SetLabel(_('Select sample area 1 of ' + str(self.areanum)))
  1346. grass.use_temp_region()
  1347. if self._raster != self.rast:
  1348. self.map_ = self.mapPanel.GetMap()
  1349. self.map_.DeleteAllLayers()
  1350. cmdlist = ['d.rast', 'map=%s' % self.rast]
  1351. self.map_.AddLayer(ltype='raster', command=cmdlist, active=True,
  1352. name=self.rast, hidden=False, opacity=1.0,
  1353. render=True)
  1354. self.newCat()
  1355. def OnExitPage(self, event=None):
  1356. """!Function during exiting"""
  1357. grass.del_temp_region()
  1358. # if event.GetDirection():
  1359. # self.SetNext(self.parent.samplingareapage)
  1360. # self.parent.samplingareapage.SetPrev(self)
  1361. class SummaryPage(TitledPage):
  1362. """!Shows summary result of choosing data"""
  1363. def __init__(self, wizard, parent):
  1364. TitledPage.__init__(self, wizard, _("Summary"))
  1365. global rlisettings
  1366. self.parent = parent
  1367. self.sizer.AddGrowableCol(2)
  1368. #configuration file name
  1369. self.conflabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1370. label=_('Configuration file name:'))
  1371. self.conftxt = wx.StaticText(parent=self, id=wx.ID_ANY,
  1372. label="")
  1373. self.sizer.Add(item=self.conflabel, border=5, pos=(0, 0),
  1374. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1375. self.sizer.Add(item=self.conftxt, border=5, pos=(0, 1),
  1376. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1377. #raster name
  1378. self.rastlabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1379. label=_('Raster name:'))
  1380. self.rasttxt = wx.StaticText(parent=self, id=wx.ID_ANY,
  1381. label="")
  1382. self.sizer.Add(item=self.rastlabel, border=5, pos=(1, 0),
  1383. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1384. self.sizer.Add(item=self.rasttxt, border=5, pos=(1, 1),
  1385. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1386. #vector name
  1387. self.vectlabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1388. label=_('Vector name:'))
  1389. self.vecttxt = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1390. self.sizer.Add(item=self.vectlabel, border=5, pos=(2, 0),
  1391. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1392. self.sizer.Add(item=self.vecttxt, border=5, pos=(2, 1),
  1393. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1394. #region type name
  1395. self.regionlabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1396. label=_('Region type:'))
  1397. self.regiontxt = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1398. self.sizer.Add(item=self.regionlabel, border=5, pos=(3, 0),
  1399. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1400. self.sizer.Add(item=self.regiontxt, border=5, pos=(3, 1),
  1401. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1402. #region keyboard
  1403. self.regionkeylabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1404. label="")
  1405. self.regionkeytxt = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1406. self.sizer.Add(item=self.regionkeylabel, border=5, pos=(4, 0),
  1407. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1408. self.sizer.Add(item=self.regionkeytxt, border=5, pos=(4, 1),
  1409. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1410. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1411. #sampling area
  1412. self.samplinglabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1413. label=_('Sampling area type:'))
  1414. self.samplingtxt = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1415. self.sizer.Add(item=self.samplinglabel, border=5, pos=(5, 0),
  1416. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1417. self.sizer.Add(item=self.samplingtxt, border=5, pos=(5, 1),
  1418. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1419. #shapetype
  1420. self.shapelabel = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1421. self.shapetxt = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1422. self.sizer.Add(item=self.shapelabel, border=5, pos=(6, 0),
  1423. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1424. self.sizer.Add(item=self.shapetxt, border=5, pos=(6, 1),
  1425. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1426. #shapedim
  1427. self.shapewidthlabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1428. label="")
  1429. self.shapewidthtxt = wx.StaticText(parent=self, id=wx.ID_ANY,
  1430. label="")
  1431. self.sizer.Add(item=self.shapewidthlabel, border=5, pos=(7, 0),
  1432. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1433. self.sizer.Add(item=self.shapewidthtxt, border=5, pos=(7, 1),
  1434. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1435. self.shapeheightlabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1436. label="")
  1437. self.shapeheighttxt = wx.StaticText(parent=self, id=wx.ID_ANY,
  1438. label="")
  1439. self.sizer.Add(item=self.shapeheightlabel, border=5, pos=(8, 0),
  1440. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1441. self.sizer.Add(item=self.shapeheighttxt, border=5, pos=(8, 1),
  1442. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1443. #units type
  1444. self.unitslabel = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1445. self.unitstxt = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1446. self.sizer.Add(item=self.unitslabel, border=5, pos=(9, 0),
  1447. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1448. self.sizer.Add(item=self.unitstxt, border=5, pos=(9, 1),
  1449. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1450. self.unitsmorelabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1451. label="")
  1452. self.unitsmoretxt = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1453. self.sizer.Add(item=self.unitsmorelabel, border=5, pos=(10, 0),
  1454. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1455. self.sizer.Add(item=self.unitsmoretxt, border=5, pos=(10, 1),
  1456. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1457. self.unitsmorelabel2 = wx.StaticText(parent=self, id=wx.ID_ANY,
  1458. label="")
  1459. self.unitsmoretxt2 = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1460. self.sizer.Add(item=self.unitsmorelabel2, border=5, pos=(11, 0),
  1461. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1462. self.sizer.Add(item=self.unitsmoretxt2, border=5, pos=(11, 1),
  1463. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1464. def OnEnterPage(self, event):
  1465. """!Insert values into text controls for summary of location
  1466. creation options
  1467. """
  1468. self.conftxt.SetLabel(self.parent.startpage.conf_name)
  1469. self.rasttxt.SetLabel(self.parent.startpage.rast)
  1470. self.samplingtxt.SetLabel(self.parent.samplingareapage.samplingtype)
  1471. self.regiontxt.SetLabel(self.parent.startpage.region)
  1472. self.vecttxt.SetLabel(self.parent.startpage.vect)
  1473. if self.parent.startpage.region == 'key':
  1474. #region keyboard values
  1475. self.regionkeylabel.SetLabel(_('Region keyboard values:'))
  1476. regKeyVals = "Column left up: %s - Row left up: %s\n" \
  1477. "Column length: %s - Row length: %s\n" % (
  1478. self.parent.keyboardpage.col_up,
  1479. self.parent.keyboardpage.row_up,
  1480. self.parent.keyboardpage.col_len,
  1481. self.parent.keyboardpage.row_len)
  1482. self.regionkeytxt.SetLabel(regKeyVals)
  1483. else:
  1484. self.regionkeylabel.SetLabel("")
  1485. self.regionkeytxt.SetLabel("")
  1486. if self.parent.samplingareapage.samplingtype == SamplingType.UNITS \
  1487. and self.parent.samplingareapage.regionbox == 'keyboard':
  1488. self.shapelabel.SetLabel(_('Type of shape:'))
  1489. self.shapetxt.SetLabel(self.parent.units.boxtype)
  1490. if self.parent.units.boxtype == 'circle':
  1491. self.shapewidthlabel.SetLabel(_("Radius size:"))
  1492. self.shapeheightlabel.SetLabel(_("Name circle mask:"))
  1493. else:
  1494. self.shapewidthlabel.SetLabel(_("Width size:"))
  1495. self.shapeheightlabel.SetLabel(_("Heigth size:"))
  1496. self.shapewidthtxt.SetLabel(self.parent.units.width)
  1497. self.shapeheighttxt.SetLabel(self.parent.units.height)
  1498. self.unitslabel.SetLabel(_("Method of distribution:"))
  1499. self.unitstxt.SetLabel(self.parent.units.distrtype)
  1500. if self.parent.units.distrtype == 'non_overlapping':
  1501. self.unitsmorelabel.SetLabel(_("Number sampling units:"))
  1502. self.unitsmoretxt.SetLabel(self.parent.units.distr1)
  1503. elif self.parent.units.distrtype == 'systematic_noncontiguos':
  1504. self.unitsmorelabel.SetLabel(_("Distance between units:"))
  1505. self.unitsmoretxt.SetLabel(self.parent.units.distr1)
  1506. elif self.parent.units.distrtype == 'stratified_random':
  1507. self.unitsmorelabel.SetLabel(_("Number row strates:"))
  1508. self.unitsmoretxt.SetLabel(self.parent.units.distr1)
  1509. self.unitsmorelabel2.SetLabel(_("Number column strates:"))
  1510. self.unitsmoretxt2.SetLabel(self.parent.units.distr2)
  1511. elif self.parent.samplingareapage.samplingtype == SamplingType.UNITS \
  1512. and self.parent.samplingareapage.regionbox == 'mouse':
  1513. self.shapelabel.SetLabel(_('Type of shape:'))
  1514. self.shapetxt.SetLabel(self.parent.units.boxtype)
  1515. self.unitstxt.SetLabel('by mouse')
  1516. elif self.parent.samplingareapage.samplingtype == 'moving':
  1517. self.shapelabel.SetLabel(_('Type of shape:'))
  1518. self.shapetxt.SetLabel(self.parent.moving.boxtype)
  1519. if self.parent.moving.boxtype == 'circle':
  1520. self.shapewidthlabel.SetLabel(_("Radius size:"))
  1521. self.shapeheightlabel.SetLabel(_("Name circle mask:"))
  1522. else:
  1523. self.shapewidthlabel.SetLabel(_("Width size:"))
  1524. self.shapeheightlabel.SetLabel(_("Heigth size:"))
  1525. self.shapewidthtxt.SetLabel(self.parent.moving.width)
  1526. self.shapeheighttxt.SetLabel(self.parent.moving.height)
  1527. else:
  1528. self.shapelabel.SetLabel("")
  1529. self.shapetxt.SetLabel("")
  1530. self.shapewidthlabel.SetLabel("")
  1531. self.shapewidthtxt.SetLabel("")
  1532. self.shapeheightlabel.SetLabel("")
  1533. self.shapeheighttxt.SetLabel("")