wizard.py 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787
  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 grass.script import vector as gvect
  30. from grass.exceptions import CalledModuleError
  31. from functions import SamplingType, sampleAreaVector, convertFeature
  32. from functions import obtainAreaVector, obtainCategories
  33. from core.gcmd import GError, GMessage, RunCommand
  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), 'wb')
  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.VectorEnabled = True
  392. self.parent = parent
  393. #name of output configuration file
  394. self.newconflabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  395. label=_('Name for new configuration file to create'))
  396. self.newconftxt = wx.TextCtrl(parent=self, id=wx.ID_ANY,
  397. size=(250, -1))
  398. wx.CallAfter(self.newconftxt.SetFocus)
  399. self.sizer.Add(item=self.newconflabel, border=5, pos=(0, 0),
  400. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  401. self.sizer.Add(item=self.newconftxt, border=5, pos=(0, 1),
  402. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  403. #raster
  404. self.mapsellabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  405. label=_('Raster map to use to select areas'))
  406. self.mapselect = gselect.Select(parent=self, id=wx.ID_ANY,
  407. size=(250, -1), type='cell',
  408. multiple=False)
  409. self.sizer.Add(item=self.mapsellabel, border=5, pos=(1, 0),
  410. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  411. self.sizer.Add(item=self.mapselect, border=5, pos=(1, 1),
  412. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  413. #vector
  414. self.vectsellabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  415. label=_('Vector map to use to select areas'))
  416. self.vectselect = gselect.Select(parent=self, id=wx.ID_ANY,
  417. size=(250, -1), type='vector',
  418. multiple=False)
  419. self.sizer.Add(item=self.vectsellabel, border=5, pos=(2, 0),
  420. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  421. self.sizer.Add(item=self.vectselect, border=5, pos=(2, 1),
  422. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  423. #vector layer
  424. self.vectlaylabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  425. label=_('Vector map layer to use to select areas'))
  426. self.vectlayer = wx.ComboBox(parent = self, id = wx.ID_ANY,
  427. size=(250, -1))
  428. self.sizer.Add(item=self.vectlaylabel, border=5, pos=(3, 0),
  429. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  430. self.sizer.Add(item=self.vectlayer, border=5, pos=(3, 1),
  431. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  432. #define sampling region
  433. self.sampling_reg = wx.RadioBox(parent=self, id=wx.ID_ANY,
  434. label=" %s " % _("Define sampling "
  435. "region (region for analysis)"),
  436. choices=[_('Whole map layer'),
  437. _('Keyboard setting'),
  438. _('Draw the sampling frame')],
  439. majorDimension=1,
  440. style=wx.RA_SPECIFY_ROWS)
  441. self.sizer.Add(item=self.sampling_reg,
  442. flag=wx.ALIGN_CENTER | wx.ALL | wx.EXPAND, border=5,
  443. pos=(5, 0), span=(1, 2))
  444. self.infoError = wx.StaticText(self, label='')
  445. self.infoError.SetForegroundColour(wx.RED)
  446. self.sizer.Add(item=self.infoError,
  447. flag=wx.ALIGN_CENTER | wx.ALL | wx.EXPAND, border=5,
  448. pos=(6, 0), span=(1, 2))
  449. #bindings
  450. self.sampling_reg.Bind(wx.EVT_RADIOBOX, self.OnSampling)
  451. self.newconftxt.Bind(wx.EVT_KILL_FOCUS, self.OnName)
  452. self.newconftxt.Bind(wx.EVT_TEXT, self.OnNameChanged)
  453. self.vectselect.Bind(wx.EVT_TEXT, self.OnVector)
  454. self.mapselect.Bind(wx.EVT_TEXT, self.OnRast)
  455. self.vectlayer.Bind(wx.EVT_TEXT, self.OnLayer)
  456. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  457. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnExitPage)
  458. wx.CallAfter(wx.FindWindowById(wx.ID_FORWARD).Enable, False)
  459. def OnSampling(self, event):
  460. """Change region type"""
  461. if event.GetInt() == 0:
  462. self.region = 'whole'
  463. self.SetNext(self.parent.samplingareapage)
  464. elif event.GetInt() == 1:
  465. self.region = 'key'
  466. self.SetNext(self.parent.keyboardpage)
  467. elif event.GetInt() == 2: # currently disabled
  468. self.region = 'draw'
  469. self.SetNext(self.parent.drawsampleframepage)
  470. def OnName(self, event):
  471. """Sets the name of configuration file"""
  472. if self.conf_name in self.parent.parent.listfiles:
  473. GMessage(parent=self, message=_("The configuration file %s "
  474. "already exists, please change name") % self.conf_name)
  475. self.newconftxt.SetValue('')
  476. self.conf_name = ''
  477. def OnNameChanged(self, event):
  478. """Name of configuration file has changed"""
  479. self.conf_name = self.newconftxt.GetValue()
  480. next = wx.FindWindowById(wx.ID_FORWARD)
  481. next.Enable(self.CheckInput())
  482. def OnRast(self, event):
  483. """Sets raster map"""
  484. self.rast = self.mapselect.GetValue()
  485. next = wx.FindWindowById(wx.ID_FORWARD)
  486. next.Enable(self.CheckInput())
  487. def OnVector(self, event):
  488. """Sets vector map"""
  489. self.vect = self.vectselect.GetValue()
  490. if self.vect:
  491. self.VectorEnabled, layers = self.CheckVector(self.vect)
  492. if self.VectorEnabled:
  493. self.vectlayer.SetItems(layers)
  494. self.vectlayer.SetSelection(0)
  495. self.vectorlayer = self.vectlayer.GetValue()
  496. self.infoError.SetLabel('')
  497. else:
  498. self.vectlayer.Clear()
  499. self.vectlayer.SetValue('')
  500. self.vect = ''
  501. else:
  502. self.infoError.SetLabel('')
  503. self.vectlayer.Clear()
  504. self.vectlayer.SetValue('')
  505. next = wx.FindWindowById(wx.ID_FORWARD)
  506. next.Enable(self.CheckInput())
  507. def OnLayer(self, event):
  508. try:
  509. self.vectorlayer = self.vectlayer.GetValue()
  510. except:
  511. self.vectorlayer = None
  512. next = wx.FindWindowById(wx.ID_FORWARD)
  513. next.Enable(self.CheckInput())
  514. def OnEnterPage(self, event):
  515. """Sets the default values, for the entire map
  516. """
  517. next = wx.FindWindowById(wx.ID_FORWARD)
  518. next.Enable(self.CheckInput())
  519. wx.CallAfter(wx.FindWindowById(wx.ID_FORWARD).Enable,
  520. self.CheckInput())
  521. def CheckVector(self, vector):
  522. """Check if the type of vector is area and return the number of
  523. vector's layer"""
  524. try:
  525. areas = gvect.vector_info_topo(vector)['areas']
  526. except CalledModuleError:
  527. self.infoError.SetLabel(_("Vector %s was not found, please "
  528. "select another vector") % vector)
  529. return False, []
  530. if areas == 0:
  531. self.infoError.SetLabel(_("Vector %s has no areas, please "
  532. "select another vector") % vector)
  533. return False, []
  534. links = gvect.vector_info(vector)['num_dblinks']
  535. if links == 0:
  536. self.infoError.SetLabel(_("Vector %s has no table connected, "
  537. "please select another vector") % vector)
  538. return False, []
  539. elif links > 0:
  540. layers = []
  541. for i in range(1, links + 1):
  542. layers.append(str(i))
  543. return True, layers
  544. else:
  545. return False, []
  546. def CheckInput(self):
  547. """Check input fields.
  548. :return: True if configuration file is given and raster xor vector map,
  549. False otherwise
  550. """
  551. return bool(self.conf_name and bool(self.rast and
  552. bool(self.VectorEnabled)))
  553. def OnExitPage(self, event=None):
  554. """Function during exiting"""
  555. next = wx.FindWindowById(wx.ID_FORWARD)
  556. next.Enable(self.CheckInput())
  557. if event.GetDirection():
  558. if self.region == 'key':
  559. self.SetNext(self.parent.keyboardpage)
  560. self.parent.samplingareapage.SetPrev(self.parent.keyboardpage)
  561. elif self.region == 'whole':
  562. self.SetNext(self.parent.samplingareapage)
  563. self.parent.samplingareapage.SetPrev(self)
  564. elif self.region == 'draw':
  565. self.SetNext(self.parent.drawsampleframepage)
  566. self.parent.samplingareapage.SetPrev(self.parent.drawsampleframepage)
  567. return
  568. class KeyboardPage(TitledPage):
  569. """
  570. !Choose the region setting the values of border using the keyboard
  571. """
  572. def __init__(self, wizard, parent):
  573. TitledPage.__init__(self, wizard, _("Insert sampling frame parameter"))
  574. self.parent = parent
  575. self.col_len = ''
  576. self.row_len = ''
  577. self.col_up = '0'
  578. self.row_up = '0'
  579. #column up/left
  580. self.ColUpLeftlabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  581. label=_("Column of upper left "
  582. "corner"))
  583. self.ColUpLefttxt = wx.TextCtrl(parent=self, id=wx.ID_ANY,
  584. size=(250, -1))
  585. wx.CallAfter(self.ColUpLeftlabel.SetFocus)
  586. self.sizer.Add(item=self.ColUpLeftlabel, border=5, pos=(1, 1),
  587. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  588. self.sizer.Add(item=self.ColUpLefttxt, border=5, pos=(1, 2),
  589. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  590. self.sizer.AddGrowableCol(2)
  591. #row up/left
  592. self.RowUpLeftlabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  593. label=_('Row of upper left corner'))
  594. self.RowUpLefttxt = wx.TextCtrl(parent=self, id=wx.ID_ANY,
  595. size=(250, -1))
  596. wx.CallAfter(self.RowUpLeftlabel.SetFocus)
  597. self.sizer.Add(item=self.RowUpLeftlabel, border=5, pos=(2, 1),
  598. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  599. self.sizer.Add(item=self.RowUpLefttxt, border=5, pos=(2, 2),
  600. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  601. #row lenght
  602. self.RowLenlabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  603. label=_('Row lenght of sampling frame'))
  604. self.RowLentxt = wx.TextCtrl(parent=self, id=wx.ID_ANY, size=(250, -1))
  605. wx.CallAfter(self.RowLenlabel.SetFocus)
  606. self.sizer.Add(item=self.RowLenlabel, border=5, pos=(3, 1),
  607. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  608. self.sizer.Add(item=self.RowLentxt, border=5, pos=(3, 2),
  609. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  610. #column lenght
  611. self.ColLenlabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  612. label=_('Row lenght of sampling frame'))
  613. self.ColLentxt = wx.TextCtrl(parent=self, id=wx.ID_ANY, size=(250, -1))
  614. wx.CallAfter(self.ColLenlabel.SetFocus)
  615. self.sizer.Add(item=self.ColLenlabel, border=5, pos=(4, 1),
  616. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  617. self.sizer.Add(item=self.ColLentxt, border=5, pos=(4, 2),
  618. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  619. self.ColUpLefttxt.SetValue(self.col_up)
  620. self.RowUpLefttxt.SetValue(self.row_up)
  621. self.ColUpLefttxt.Bind(wx.EVT_KILL_FOCUS, self.OnColLeft)
  622. self.RowUpLefttxt.Bind(wx.EVT_KILL_FOCUS, self.OnRowLeft)
  623. self.ColLentxt.Bind(wx.EVT_KILL_FOCUS, self.OnColLen)
  624. self.RowLentxt.Bind(wx.EVT_KILL_FOCUS, self.OnRowLen)
  625. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  626. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnExitPage)
  627. def OnColLeft(self, event):
  628. """Sets the name of configuration file"""
  629. self.col_up = self.ColUpLefttxt.GetValue()
  630. checkValue(self.col_up)
  631. def OnRowLeft(self, event):
  632. """Sets the name of configuration file"""
  633. self.row_up = self.RowUpLefttxt.GetValue()
  634. checkValue(self.row_up)
  635. def OnColLen(self, event):
  636. """Sets the name of configuration file"""
  637. self.col_len = self.ColLentxt.GetValue()
  638. checkValue(self.col_len)
  639. def OnRowLen(self, event):
  640. """Sets the name of configuration file"""
  641. self.row_len = self.RowLentxt.GetValue()
  642. checkValue(self.row_len)
  643. def OnEnterPage(self, event):
  644. """Sets the default values, for the entire map
  645. """
  646. #R# check if raster exists before anything
  647. if self.col_len == '' and self.row_len == '':
  648. rastinfo = grast.raster_info(self.parent.startpage.rast)
  649. self.col_len = rastinfo['cols']
  650. self.row_len = rastinfo['rows']
  651. self.ColLentxt.SetValue(self.col_len)
  652. self.RowLentxt.SetValue(self.row_len)
  653. def OnExitPage(self, event=None):
  654. """Function during exiting"""
  655. if self.row_len == '' or self.col_len == '' or self.row_up == '' or self.col_up == '':
  656. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  657. else:
  658. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  659. class DrawSampleFramePage(TitledPage):
  660. """Choose the region setting the values drawing a box"""
  661. def __init__(self, wizard, parent):
  662. TitledPage.__init__(self, wizard, _("Draw sampling frame"))
  663. self.parent = parent
  664. self.mapPanel = None
  665. self.tregion = None
  666. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  667. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnExitPage)
  668. def SampleFrameChanged(self, region):
  669. """"Enables the next dialog when region is set"""
  670. if region:
  671. self.tregion = region
  672. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  673. else:
  674. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  675. def OnEnterPage(self, event):
  676. """Function during entering"""
  677. if self.mapPanel is None:
  678. self.mapPanel = RLiSetupMapPanel(self, samplingType='drawFrame')
  679. self.mapPanel.sampleFrameChanged.connect(self.SampleFrameChanged)
  680. self.sizer.Add(item=self.mapPanel, flag=wx.EXPAND, pos=(0, 0))
  681. self.sizer.AddGrowableCol(0)
  682. self.sizer.AddGrowableRow(0)
  683. self._raster = None
  684. else:
  685. self.mapPanel._region = {}
  686. self.SampleFrameChanged(region=None)
  687. rast = self.parent.startpage.rast
  688. if self._raster != rast:
  689. map_ = self.mapPanel.GetMap()
  690. map_.DeleteAllLayers()
  691. cmdlist = ['d.rast', 'map=%s' % rast]
  692. map_.AddLayer(ltype='raster', command=cmdlist, active=True,
  693. name=rast, hidden=False, opacity=1.0, render=True)
  694. def OnExitPage(self, event=None):
  695. """Function during exiting"""
  696. if event.GetDirection():
  697. self.SetNext(self.parent.samplingareapage)
  698. self.parent.samplingareapage.SetPrev(self)
  699. else:
  700. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  701. class SamplingAreasPage(TitledPage):
  702. """
  703. Set name of configuration file, choose raster and optionally vector/sites.
  704. This is coming after choose the region
  705. """
  706. def __init__(self, wizard, parent):
  707. TitledPage.__init__(self, wizard, _("Insert sampling areas"))
  708. self.samplingtype = 'whole'
  709. self.parent = parent
  710. # toggles
  711. self.radioBox = wx.RadioBox(parent=self, id=wx.ID_ANY,
  712. label="",
  713. choices=[_("Whole map layer"),
  714. _("Regions"),
  715. _("Sample units"),
  716. _("Moving window"),
  717. _("Select areas from the\n"
  718. "overlayed vector map")],
  719. majorDimension=1,
  720. style=wx.RA_SPECIFY_COLS | wx.NO_BORDER)
  721. # layout
  722. self.sizer.SetVGap(10)
  723. self.sizer.Add(item=self.radioBox, flag=wx.ALIGN_LEFT, pos=(0, 0))
  724. self.regionBox = wx.RadioBox(parent=self, id=wx.ID_ANY,
  725. label=_("Choose a method"),
  726. choices=[_('Use keyboard to enter sampling area'),
  727. _('Use mouse to draw sampling area')],
  728. majorDimension=1,
  729. style=wx.RA_SPECIFY_ROWS)
  730. #self.regionBox.EnableItem(1, False)
  731. self.regionBox.SetItemToolTip(1, _("This option is not supported yet"))
  732. self.sizer.Add(self.regionBox, flag=wx.ALIGN_CENTER, pos=(1, 0))
  733. # bindings
  734. self.radioBox.Bind(wx.EVT_RADIOBOX, self.SetVal)
  735. self.regionBox.Bind(wx.EVT_RADIOBOX, self.OnRegionDraw)
  736. self.regionbox = 'keyboard'
  737. self.regionPanelSizer = wx.GridBagSizer(1, 2)
  738. self.regionNumPanel = wx.Panel(parent=self, id=wx.ID_ANY)
  739. self.regionNumLabel = wx.StaticText(parent=self.regionNumPanel,
  740. id=wx.ID_ANY,
  741. label=_('Number of regions to draw:'))
  742. self.regionNumTxt = wx.TextCtrl(parent=self.regionNumPanel,
  743. id=wx.ID_ANY, size=(50, -1))
  744. self.regionPanelSizer.Add(self.regionNumLabel, flag=wx.ALIGN_CENTER,
  745. pos=(0, 0))
  746. self.regionPanelSizer.Add(self.regionNumTxt, flag=wx.ALIGN_CENTER,
  747. pos=(0, 1))
  748. self.regionNumPanel.SetSizer(self.regionPanelSizer)
  749. self.sizer.Add(self.regionNumPanel, flag=wx.ALIGN_CENTER, pos=(2, 0))
  750. self.areaPanelSizer = wx.GridBagSizer(1, 3)
  751. self.areaPanel = wx.Panel(parent=self, id=wx.ID_ANY)
  752. self.areaText = wx.StaticText(parent=self.areaPanel, id=wx.ID_ANY,
  753. label=_('Do you want to check vector areas?'))
  754. self.areaOK = wx.Button(self.areaPanel, wx.ID_ANY, 'Yes', (50, 80))
  755. self.areaOK.SetToolTip(wx.ToolTip(_("Select if use area by area")))
  756. self.areaNO = wx.Button(self.areaPanel, wx.ID_ANY, 'No', (50, 80))
  757. self.areaNO.SetToolTip(wx.ToolTip(_("All the features will be used")))
  758. self.areaOK.Bind(wx.EVT_BUTTON, self.OnVectYes)
  759. self.areaNO.Bind(wx.EVT_BUTTON, self.OnVectNo)
  760. self.areaPanelSizer.Add(self.areaText, flag=wx.ALIGN_CENTER,
  761. pos=(0, 0))
  762. self.areaPanelSizer.Add(self.areaOK, flag=wx.ALIGN_CENTER, pos=(0, 1))
  763. self.areaPanelSizer.Add(self.areaNO, flag=wx.ALIGN_CENTER, pos=(0, 2))
  764. self.areaPanel.SetSizer(self.areaPanelSizer)
  765. self.sizer.Add(self.areaPanel, flag=wx.ALIGN_CENTER, pos=(3, 0))
  766. self.calculatingAreas = wx.StaticText(parent=self, id=wx.ID_ANY,
  767. label=_('Analysing all vector features...'))
  768. self.sizer.Add(self.calculatingAreas, flag=wx.ALIGN_CENTER, pos=(4, 0))
  769. self.numregions = ''
  770. self.regionNumTxt.Bind(wx.EVT_TEXT, self.OnNumRegions)
  771. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  772. def OnNumRegions(self, event):
  773. """Obtain the number of regions"""
  774. if self.regionNumTxt.GetValue():
  775. self.SetNext(self.parent.regions)
  776. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  777. self.numregions = self.regionNumTxt.GetValue()
  778. else:
  779. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  780. def OnEnterPage(self, event):
  781. """Insert values into text controls for summary of location
  782. creation options
  783. """
  784. self.SetVal(None)
  785. if self.parent.startpage.vect:
  786. self.radioBox.ShowItem(4, True)
  787. self.vect_data = []
  788. else:
  789. self.radioBox.ShowItem(4, False)
  790. self.sizer.Layout()
  791. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  792. def SetVal(self, event):
  793. """Choose method"""
  794. radio = self.radioBox.GetSelection()
  795. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  796. if radio == 0:
  797. self.samplingtype = SamplingType.WHOLE
  798. self.DrawNothing()
  799. elif radio == 1:
  800. self.samplingtype = SamplingType.REGIONS
  801. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  802. elif radio == 2:
  803. self.samplingtype = SamplingType.UNITS
  804. regtype = self.regionBox.GetSelection()
  805. self.RegionDraw(regtype)
  806. elif radio == 3:
  807. self.samplingtype = SamplingType.MVWIN
  808. regtype = self.regionBox.GetSelection()
  809. self.RegionDraw(regtype)
  810. elif radio == 4:
  811. self.samplingtype = SamplingType.VECT
  812. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  813. self.ShowExtraOptions(self.samplingtype)
  814. def ShowExtraOptions(self, samtype):
  815. """Show some extra options for some sampling type"""
  816. if samtype == SamplingType.REGIONS:
  817. self.sizer.Hide(self.regionBox)
  818. self.sizer.Hide(self.areaPanel)
  819. self.sizer.Hide(self.calculatingAreas)
  820. self.sizer.Show(self.regionNumPanel)
  821. elif samtype == SamplingType.UNITS or samtype == SamplingType.MVWIN:
  822. self.sizer.Hide(self.regionNumPanel)
  823. self.sizer.Hide(self.areaPanel)
  824. self.sizer.Hide(self.calculatingAreas)
  825. self.sizer.Show(self.regionBox)
  826. elif samtype == SamplingType.VECT:
  827. self.sizer.Hide(self.regionBox)
  828. self.sizer.Hide(self.regionNumPanel)
  829. self.sizer.Hide(self.calculatingAreas)
  830. self.sizer.Show(self.areaPanel)
  831. self.sizer.Layout()
  832. def OnRegionDraw(self, event):
  833. self.RegionDraw(event.GetInt())
  834. return
  835. def OnVectYes(self, event):
  836. """The user choose to select the vector areas, this function set the
  837. next page to VectorAreasPage"""
  838. self.SetNext(self.parent.vectorareas)
  839. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  840. self.parent.wizard.ShowPage(self.parent.vectorareas)
  841. def OnVectNo(self, event):
  842. """The user choose to use all the vector areas, this function analyses
  843. all the vector areas and fill the msAreaList variable"""
  844. self.sizer.Show(self.calculatingAreas)
  845. self.sizer.Hide(self.regionNumPanel)
  846. self.sizer.Hide(self.regionBox)
  847. self.sizer.Hide(self.areaPanel)
  848. self.SetNext(self.parent.summarypage)
  849. vect_cats = obtainCategories(self.parent.startpage.vect, self.vectorlayer)
  850. self._progressDlg = wx.ProgressDialog(title=_("Analysing vector"),
  851. message="Analysing vector",
  852. maximum=len(vect_cats),
  853. parent=self,
  854. style=wx.PD_CAN_ABORT | wx.PD_APP_MODAL |
  855. wx.PD_AUTO_HIDE | wx.PD_SMOOTH)
  856. self._progressDlgMax = len(vect_cats)
  857. grass.use_temp_region()
  858. self.parent.msAreaList = sampleAreaVector(self.parent.startpage.vect,
  859. self.parent.startpage.rast,
  860. vect_cats,
  861. self.parent.startpage.vectorlayer,
  862. self._progressDlg)
  863. grass.del_temp_region()
  864. if self.parent.msAreaList:
  865. self.calculatingAreas.SetLabel(_("All feature are been analyzed."))
  866. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  867. self.parent.wizard.ShowPage(self.parent.summarypage)
  868. else:
  869. self.calculatingAreas.SetLabel(_("An error occurred"))
  870. self._progressDlg.Destroy()
  871. self._progressDlg = None
  872. def RegionDraw(self, regtype):
  873. """Set the next page to units or drawunits"""
  874. if regtype == 0:
  875. self.regionbox = 'keyboard'
  876. if self.samplingtype == SamplingType.UNITS:
  877. self.SetNext(self.parent.units)
  878. elif self.samplingtype == SamplingType.MVWIN:
  879. self.SetNext(self.parent.moving)
  880. elif regtype == 1:
  881. self.regionbox = 'mouse'
  882. self.SetNext(self.parent.drawunits)
  883. def DrawNothing(self):
  884. """Remove all the optional choices. Used also when the wizard enter in
  885. SamplingAreasPage, all the optional choices should be hide here"""
  886. self.sizer.Hide(self.regionNumPanel)
  887. self.sizer.Hide(self.regionBox)
  888. self.sizer.Hide(self.areaPanel)
  889. self.sizer.Hide(self.calculatingAreas)
  890. self.sizer.Layout()
  891. self.SetNext(self.parent.summarypage)
  892. class DrawRegionsPage(TitledPage):
  893. def __init__(self, wizard, parent):
  894. self.parent = parent
  895. TitledPage.__init__(self, wizard, _("Draw sampling regions"))
  896. self.regioncount = 0
  897. self.mapPanel = None
  898. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  899. #self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnExitPage)
  900. def afterRegionDrawn(self, marea):
  901. if marea:
  902. self.parent.msAreaList.append(marea)
  903. self.regioncount = self.regioncount + 1
  904. numregions = int(self.parent.samplingareapage.numregions)
  905. if self.regioncount > numregions:
  906. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  907. self.parent.wizard.ShowPage(self.parent.summarypage)
  908. else:
  909. self.title.SetLabel(_('Draw sample region ' +
  910. str(self.regioncount) + ' of ' +
  911. str(numregions)))
  912. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  913. def OnEnterPage(self, event):
  914. """Function during entering"""
  915. if self.parent.samplingareapage.samplingtype == SamplingType.WHOLE:
  916. self.title.SetLabel(_("Draw moving windows region"))
  917. elif self.parent.samplingareapage.samplingtype == SamplingType.UNITS:
  918. self.title.SetLabel(_("Draw sampling region"))
  919. if self.mapPanel is None:
  920. self.mapPanel = RLiSetupMapPanel(self,
  921. samplingType=self.parent.samplingareapage.samplingtype,
  922. )
  923. self.mapPanel.afterRegionDrawn.connect(self.afterRegionDrawn)
  924. self.sizer.Add(item=self.mapPanel, flag=wx.EXPAND, pos=(1, 0))
  925. self.sizer.AddGrowableCol(0)
  926. self.sizer.AddGrowableRow(1)
  927. self._raster = None
  928. rast = self.parent.startpage.rast
  929. self.afterRegionDrawn(marea=None)
  930. if self._raster != rast:
  931. map_ = self.mapPanel.GetMap()
  932. map_.DeleteAllLayers()
  933. cmdlist = ['d.rast', 'map=%s' % rast]
  934. map_.AddLayer(ltype='raster', command=cmdlist, active=True,
  935. name=rast, hidden=False, opacity=1.0, render=True)
  936. #def OnExitPage(self, event=None):
  937. #!Function during exiting
  938. #print event.GetDirection()
  939. #if event.GetDirection():
  940. # self.SetNext(self.parent.samplingareapage)
  941. # self.parent.samplingareapage.SetPrev(self)
  942. class SampleUnitsKeyPage(TitledPage):
  943. """Set values from keyboard for sample units
  944. It is used if you choose keyboard from Sampling Units or Moving windows
  945. in sampling areas page
  946. """
  947. def __init__(self, wizard, parent):
  948. TitledPage.__init__(self, wizard, _("Select sample units from keyboard"))
  949. self.parent = parent
  950. self.scrollPanel = scrolled.ScrolledPanel(parent=self, id=wx.ID_ANY)
  951. self.scrollPanel.SetupScrolling(scroll_x=False)
  952. self.panelSizer = wx.GridBagSizer(5, 5)
  953. self.width = ''
  954. self.height = ''
  955. self.boxtype = 'rectangle'
  956. self.distrtype = 'non_overlapping'
  957. # type of shape
  958. self.typeBox = wx.RadioBox(parent=self.scrollPanel, id=wx.ID_ANY,
  959. majorDimension=1, style=wx.RA_SPECIFY_COLS,
  960. label=" %s " % _("Select type of shape"),
  961. choices=[_('Rectangle'), _('Circle'),
  962. ('None')])
  963. self.panelSizer.Add(self.typeBox, flag=wx.ALIGN_LEFT, pos=(0, 0),
  964. span=(1, 2))
  965. self.widthLabel = wx.StaticText(parent=self.scrollPanel, id=wx.ID_ANY)
  966. self.widthTxt = wx.TextCtrl(parent=self.scrollPanel, id=wx.ID_ANY,
  967. size=(250, -1))
  968. self.panelSizer.Add(item=self.widthLabel, pos=(1, 0),
  969. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  970. self.panelSizer.Add(item=self.widthTxt, pos=(1, 1),
  971. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  972. self.heightLabel = wx.StaticText(parent=self.scrollPanel, id=wx.ID_ANY)
  973. self.heightTxt = wx.TextCtrl(parent=self.scrollPanel, id=wx.ID_ANY,
  974. size=(250, -1))
  975. self.panelSizer.Add(item=self.heightLabel, pos=(2, 0),
  976. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  977. self.panelSizer.Add(item=self.heightTxt, pos=(2, 1),
  978. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  979. self.widthLabels = [_('Width size (in cells)? '),
  980. _('What radius size (in meters)? ')]
  981. self.heightLabels = [_('Height size (in cells)? '),
  982. _('Name of the circle mask')]
  983. self.distributionBox = wx.RadioBox(parent=self.scrollPanel,
  984. id=wx.ID_ANY,
  985. majorDimension=1,
  986. style=wx.RA_SPECIFY_COLS,
  987. label= " %s " % _("Select method of sampling unit distribution"),
  988. choices=[_('Random non overlapping'),
  989. _('Systematic contiguos'),
  990. _('Stratified random'),
  991. _('Systematic non contiguos'),
  992. _('Centered over sites')]
  993. )
  994. self.panelSizer.Add(item=self.distributionBox, pos=(3, 0), span=(1, 2),
  995. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  996. self.distr1Label = wx.StaticText(parent=self.scrollPanel, id=wx.ID_ANY,
  997. label=_("What number of Sampling "
  998. "Units to use?"))
  999. self.distr1Txt = wx.TextCtrl(parent=self.scrollPanel, id=wx.ID_ANY,
  1000. size=(250, -1))
  1001. self.panelSizer.Add(item=self.distr1Label, pos=(4, 0),
  1002. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1003. self.panelSizer.Add(item=self.distr1Txt, pos=(4, 1),
  1004. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1005. self.distr2Label = wx.StaticText(parent=self.scrollPanel, id=wx.ID_ANY,
  1006. label="")
  1007. self.distr2Txt = wx.TextCtrl(parent=self.scrollPanel, id=wx.ID_ANY,
  1008. size=(250, -1))
  1009. self.panelSizer.Add(item=self.distr2Label, pos=(5, 0),
  1010. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1011. self.panelSizer.Add(item=self.distr2Txt, pos=(5, 1),
  1012. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1013. self.panelSizer.Hide(self.distr2Txt)
  1014. self.typeBox.Bind(wx.EVT_RADIOBOX, self.OnType)
  1015. self.distributionBox.Bind(wx.EVT_RADIOBOX, self.OnDistr)
  1016. self.widthTxt.Bind(wx.EVT_TEXT, self.OnWidth)
  1017. self.heightTxt.Bind(wx.EVT_TEXT, self.OnHeight)
  1018. self.distr1Txt.Bind(wx.EVT_TEXT, self.OnDistr1)
  1019. self.distr2Txt.Bind(wx.EVT_TEXT, self.OnDistr2)
  1020. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1021. self.sizer.Add(item=self.scrollPanel, pos=(0, 0), flag=wx.EXPAND)
  1022. self.sizer.AddGrowableCol(0)
  1023. self.sizer.AddGrowableRow(0)
  1024. self.scrollPanel.SetSizer(self.panelSizer)
  1025. #self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnExitPage)
  1026. self.OnType(None)
  1027. def OnEnterPage(self, event=None):
  1028. """Function during entering"""
  1029. # This is an hack to force the user to choose Rectangle or Circle
  1030. self.typeBox.SetSelection(2),
  1031. self.typeBox.ShowItem(2, False)
  1032. self.panelSizer.Layout()
  1033. def OnExitPage(self, event=None):
  1034. """Function during exiting"""
  1035. if event.GetDirection():
  1036. self.SetNext(self.parent.summarypage)
  1037. self.SetPrev(self.parent.samplingareapage)
  1038. def OnType(self, event):
  1039. """Set if rectangle or circle will be used"""
  1040. chosen = self.typeBox.GetSelection()
  1041. self.widthLabel.SetLabel(self.widthLabels[chosen])
  1042. self.heightLabel.SetLabel(self.heightLabels[chosen])
  1043. self.panelSizer.Layout()
  1044. if chosen == 0:
  1045. self.boxtype = 'rectangle'
  1046. else:
  1047. self.boxtype = 'circle'
  1048. def OnDistr(self, event):
  1049. chosen = self.distributionBox.GetSelection()
  1050. if chosen == 0:
  1051. self.distrtype = 'non_overlapping'
  1052. self.distr1Label.SetLabel(_("What number of Sampling Units to use?"))
  1053. self.panelSizer.Show(self.distr1Txt)
  1054. self.distr2Label.SetLabel("")
  1055. self.panelSizer.Hide(self.distr2Txt)
  1056. self.panelSizer.Layout()
  1057. elif chosen == 1:
  1058. self.distrtype = 'systematic_contiguos'
  1059. self.distr1Label.SetLabel("")
  1060. self.distr2Label.SetLabel("")
  1061. self.panelSizer.Hide(self.distr1Txt)
  1062. self.panelSizer.Hide(self.distr2Txt)
  1063. self.panelSizer.Layout()
  1064. elif chosen == 2:
  1065. self.distrtype = 'stratified_random'
  1066. self.distr1Label.SetLabel(_("Insert number of row strates"))
  1067. self.distr2Label.SetLabel(_("Insert number of column strates"))
  1068. self.panelSizer.Show(self.distr1Txt)
  1069. self.panelSizer.Show(self.distr2Txt)
  1070. self.panelSizer.Layout()
  1071. elif chosen == 3:
  1072. self.distrtype = 'systematic_noncontiguos'
  1073. self.distr1Label.SetLabel(_("Insert distance between units"))
  1074. self.panelSizer.Show(self.distr1Txt)
  1075. self.distr2Label.SetLabel("")
  1076. self.panelSizer.Hide(self.distr2Txt)
  1077. self.panelSizer.Layout()
  1078. elif chosen == 4:
  1079. self.distrtype = 'centered_oversites'
  1080. self.distr1Label.SetLabel("")
  1081. self.distr2Label.SetLabel("")
  1082. self.panelSizer.Hide(self.distr2Txt)
  1083. self.panelSizer.Hide(self.distr1Txt)
  1084. self.panelSizer.Layout()
  1085. def OnWidth(self, event):
  1086. self.width = self.widthTxt.GetValue()
  1087. def OnHeight(self, event):
  1088. self.height = self.heightTxt.GetValue()
  1089. def OnDistr1(self, event):
  1090. self.distr1 = self.distr1Txt.GetValue()
  1091. def OnDistr2(self, event):
  1092. self.distr2 = self.distr2Txt.GetValue()
  1093. class MovingKeyPage(TitledPage):
  1094. """Set values from keyboard for sample units"""
  1095. def __init__(self, wizard, parent):
  1096. TitledPage.__init__(self, wizard, _("Set sample units"))
  1097. self.parent = parent
  1098. self.width = ''
  1099. self.height = ''
  1100. self.boxtype = 'rectangle'
  1101. # type of shape
  1102. self.typeBox = wx.RadioBox(parent=self, id=wx.ID_ANY,
  1103. label=" %s " % _("Select type of shape"),
  1104. choices=[_('Rectangle'), _('Circle')],
  1105. # ('None')],
  1106. majorDimension=1,
  1107. style=wx.RA_SPECIFY_COLS)
  1108. self.sizer.Add(self.typeBox, flag=wx.ALIGN_LEFT, pos=(1, 1))
  1109. self.typeBox.Bind(wx.EVT_RADIOBOX, self.OnType)
  1110. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1111. self.widthLabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1112. label=_('Width size (in cells) ?'))
  1113. self.widthTxt = wx.TextCtrl(parent=self, id=wx.ID_ANY, size=(250, -1))
  1114. self.sizer.Add(item=self.widthLabel, border=5, pos=(2, 1),
  1115. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1116. self.sizer.Add(item=self.widthTxt, border=5, pos=(2, 2),
  1117. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1118. self.heightLabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1119. label=_('Height size (in cells) ?'))
  1120. self.heightTxt = wx.TextCtrl(parent=self, id=wx.ID_ANY, size=(250, -1))
  1121. self.sizer.Add(item=self.heightLabel, border=5, pos=(3, 1),
  1122. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1123. self.sizer.Add(item=self.heightTxt, border=5, pos=(3, 2),
  1124. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1125. self.sizer.AddGrowableCol(2)
  1126. self.widthLabels = [_('Width size (in cells) ?'),
  1127. _('What radius size (in meters) ?')]
  1128. self.heightLabels = [_('Height size (in cells) ?'),
  1129. _('Name of the circle mask')]
  1130. self.widthTxt.Bind(wx.EVT_TEXT, self.OnWidth)
  1131. self.heightTxt.Bind(wx.EVT_TEXT, self.OnHeight)
  1132. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1133. def OnEnterPage(self, event):
  1134. # This is an hack to force the user to choose Rectangle or Circle
  1135. # self.typeBox.SetSelection(2),
  1136. # self.typeBox.ShowItem(2, False)
  1137. if self.parent.samplingareapage.samplingtype == SamplingType.MVWIN:
  1138. self.title.SetLabel(_("Set moving windows"))
  1139. self.OnType(None)
  1140. def OnType(self, event):
  1141. chosen = self.typeBox.GetSelection()
  1142. self.widthLabel.SetLabel(self.widthLabels[chosen])
  1143. self.heightLabel.SetLabel(self.heightLabels[chosen])
  1144. self.sizer.Layout()
  1145. if chosen == 0:
  1146. self.parent.samplingareapage.samplingtype = SamplingType.KMVWINR
  1147. self.boxtype = 'rectangle'
  1148. elif chosen == 1:
  1149. self.parent.samplingareapage.samplingtype = SamplingType.KMVWINC
  1150. self.boxtype = 'circle'
  1151. def CheckInput(self):
  1152. """Check input fields.
  1153. :return: True if configuration file is given and raster xor
  1154. vector map, False otherwise
  1155. """
  1156. return bool(self.width and bool(self.height))
  1157. def OnWidth(self, event):
  1158. self.width = self.widthTxt.GetValue()
  1159. next = wx.FindWindowById(wx.ID_FORWARD)
  1160. next.Enable(self.CheckInput())
  1161. def OnHeight(self, event):
  1162. self.height = self.heightTxt.GetValue()
  1163. next = wx.FindWindowById(wx.ID_FORWARD)
  1164. next.Enable(self.CheckInput())
  1165. class UnitsMousePage(TitledPage):
  1166. """Choose the sampling area setting the values using the mouse drawing the
  1167. areas with rectangle or circle. It is used both from 'Moving windows'
  1168. and 'Sample units'.
  1169. """
  1170. def __init__(self, wizard, parent):
  1171. self.parent = parent
  1172. self.wizard = wizard
  1173. TitledPage.__init__(self, self.wizard, _("Draw sampling units"))
  1174. self.numregions = ''
  1175. self.mapPanel = None
  1176. # type of shape
  1177. self.typeBox = wx.RadioBox(parent=self, id=wx.ID_ANY,
  1178. majorDimension=1, style=wx.RA_SPECIFY_COLS,
  1179. label=" %s " % _("Select type of shape"),
  1180. choices=[_('Rectangle'), _('Circle'), ('')])
  1181. # This is an hack to force the user to choose Rectangle or Circle
  1182. self.typeBox.SetSelection(2),
  1183. self.typeBox.ShowItem(2, False)
  1184. self.sizer.Add(self.typeBox, flag=wx.ALIGN_LEFT, pos=(0, 0),
  1185. span=(1, 2))
  1186. self.regionPanelSizer = wx.GridBagSizer(1, 2)
  1187. self.regionNumPanel = wx.Panel(parent=self, id=wx.ID_ANY)
  1188. self.regionNumLabel = wx.StaticText(parent=self.regionNumPanel,
  1189. id=wx.ID_ANY,
  1190. label=_('Number of sampling area to draw:'))
  1191. self.regionNumTxt = wx.TextCtrl(parent=self.regionNumPanel,
  1192. id=wx.ID_ANY, size=(50, -1))
  1193. self.regionPanelSizer.Add(self.regionNumLabel, flag=wx.ALIGN_CENTER,
  1194. pos=(0, 0))
  1195. self.regionPanelSizer.Add(self.regionNumTxt, flag=wx.ALIGN_CENTER,
  1196. pos=(0, 1))
  1197. self.regionNumPanel.SetSizer(self.regionPanelSizer)
  1198. self.sizer.Add(self.regionNumPanel, flag=wx.ALIGN_LEFT, pos=(1, 0),
  1199. span=(1, 2))
  1200. self.typeBox.Bind(wx.EVT_RADIOBOX, self.OnType)
  1201. self.regionNumTxt.Bind(wx.EVT_TEXT, self.OnNumRegions)
  1202. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1203. self.sizer.AddGrowableCol(0)
  1204. self.OnType(None)
  1205. self.regionNumTxt.SetValue('')
  1206. def OnEnterPage(self, event):
  1207. """Function during entering"""
  1208. if self.numregions:
  1209. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1210. else:
  1211. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1212. if self.parent.samplingareapage.samplingtype in [SamplingType.MVWIN,
  1213. SamplingType.MMVWINR,
  1214. SamplingType.MMVWINC]:
  1215. self.title.SetLabel(_("Draw moving windows region"))
  1216. self.sizer.Hide(self.regionNumPanel)
  1217. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1218. elif self.parent.samplingareapage.samplingtype in [SamplingType.UNITS,
  1219. SamplingType.MUNITSR,
  1220. SamplingType.MUNITSC]:
  1221. self.title.SetLabel(_("Draw sampling region"))
  1222. self.sizer.Show(self.regionNumPanel)
  1223. if self.typeBox.GetSelection() == 2:
  1224. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1225. self.sizer.Layout()
  1226. def OnType(self, event):
  1227. chosen = self.typeBox.GetSelection()
  1228. if chosen == 0:
  1229. if self.parent.samplingareapage.samplingtype in [SamplingType.MVWIN,
  1230. SamplingType.MMVWINR,
  1231. SamplingType.MMVWINC]:
  1232. self.parent.samplingareapage.samplingtype = SamplingType.MMVWINR
  1233. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1234. else:
  1235. self.parent.samplingareapage.samplingtype = SamplingType.MUNITSR
  1236. self.drawtype = 'rectangle'
  1237. else:
  1238. if self.parent.samplingareapage.samplingtype in [SamplingType.MVWIN,
  1239. SamplingType.MMVWINR,
  1240. SamplingType.MMVWINC]:
  1241. self.parent.samplingareapage.samplingtype = SamplingType.MMVWINC
  1242. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1243. else:
  1244. self.parent.samplingareapage.samplingtype = SamplingType.MUNITSC
  1245. self.drawtype = 'circle'
  1246. def OnNumRegions(self, event):
  1247. """Set the number of region"""
  1248. if self.regionNumTxt.GetValue():
  1249. self.SetNext(self.parent.drawsampleunitspage)
  1250. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1251. self.numregions = self.regionNumTxt.GetValue()
  1252. else:
  1253. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1254. def OnExitPage(self, event=None):
  1255. """Function during exiting"""
  1256. if event.GetDirection():
  1257. self.SetNext(self.drawsampleunitspage)
  1258. self.SetPrev(self.samplingareapage)
  1259. class DrawSampleUnitsPage(TitledPage):
  1260. """Choose the sampling area drawing them"""
  1261. def __init__(self, wizard, parent):
  1262. TitledPage.__init__(self, wizard, _("Draw sampling units"))
  1263. self.parent = parent
  1264. self.mapPanel = None
  1265. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1266. #self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnExitPage)
  1267. def SampleFrameChanged(self, region):
  1268. #region = self.GetSampleUnitRegion()
  1269. if region:
  1270. self.parent.msAreaList.append(region)
  1271. self.regioncount = self.regioncount + 1
  1272. drawtype = self.parent.drawunits.drawtype
  1273. if self.regioncount > self.numregions:
  1274. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1275. self.parent.wizard.ShowPage(self.parent.summarypage)
  1276. else:
  1277. self.title.SetLabel(_('Draw Sampling ' + drawtype + ' ' \
  1278. + str(self.regioncount) + ' of ' \
  1279. + str(self.numregions)))
  1280. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1281. def OnEnterPage(self, event):
  1282. """Function during entering"""
  1283. if self.parent.samplingareapage.samplingtype in [SamplingType.MVWIN,
  1284. SamplingType.MMVWINC,
  1285. SamplingType.MMVWINR]:
  1286. self.numregions = 1
  1287. else:
  1288. self.numregions = int(self.parent.drawunits.numregions)
  1289. self.regioncount = 0
  1290. if self.mapPanel:
  1291. self.sizer.Remove(self.mapPanel)
  1292. gtype = self.parent.drawunits.drawtype
  1293. self.mapPanel = RLiSetupMapPanel(self,
  1294. samplingType=self.parent.samplingareapage.samplingtype,
  1295. )
  1296. if gtype == 'circle':
  1297. self.mapPanel.afterCircleDrawn.connect(self.SampleFrameChanged)
  1298. else:
  1299. self.mapPanel.sampleFrameChanged.connect(self.SampleFrameChanged)
  1300. self.sizer.Add(item=self.mapPanel, flag=wx.EXPAND, pos=(1, 0))
  1301. self.sizer.AddGrowableCol(0)
  1302. self.sizer.AddGrowableRow(1)
  1303. self._raster = None
  1304. self.SampleFrameChanged(region=None)
  1305. rast = self.parent.startpage.rast
  1306. if self._raster != rast:
  1307. map_ = self.mapPanel.GetMap()
  1308. map_.DeleteAllLayers()
  1309. cmdlist = ['d.rast', 'map=%s' % rast]
  1310. map_.AddLayer(ltype='raster', command=cmdlist, active=True,
  1311. name=rast, hidden=False, opacity=1.0, render=True)
  1312. def OnExitPage(self, event=None):
  1313. #!Function during exiting
  1314. print event.GetDirection()
  1315. #if event.GetDirection():
  1316. # self.SetNext(self.parent.samplingareapage)
  1317. # self.parent.samplingareapage.SetPrev(self)
  1318. class VectorAreasPage(TitledPage):
  1319. """Choose the sampling area using the vector features"""
  1320. def __init__(self, wizard, parent):
  1321. TitledPage.__init__(self, wizard, _("Select sampling areas"))
  1322. self.parent = parent
  1323. self.areascount = 0
  1324. self.mapPanel = None
  1325. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1326. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnExitPage)
  1327. self.areaPanelSizer = wx.GridBagSizer(1, 3)
  1328. self.areaPanel = wx.Panel(parent=self, id=wx.ID_ANY)
  1329. self.areaText = wx.StaticText(parent=self.areaPanel, id=wx.ID_ANY,
  1330. label=_('Is this area ok?'))
  1331. self.areaOK = wx.Button(self.areaPanel, wx.ID_ANY, 'Yes', (50, 80))
  1332. self.areaNO = wx.Button(self.areaPanel, wx.ID_ANY, 'No', (50, 80))
  1333. self.areaOK.Bind(wx.EVT_BUTTON, self.OnYes)
  1334. self.areaNO.Bind(wx.EVT_BUTTON, self.OnNo)
  1335. self.areaPanelSizer.Add(self.areaText, flag=wx.ALIGN_CENTER,
  1336. pos=(0, 0))
  1337. self.areaPanelSizer.Add(self.areaOK, flag=wx.ALIGN_CENTER,
  1338. pos=(0, 1))
  1339. self.areaPanelSizer.Add(self.areaNO, flag=wx.ALIGN_CENTER,
  1340. pos=(0, 2))
  1341. self.areaPanel.SetSizer(self.areaPanelSizer)
  1342. self.sizer.Add(self.areaPanel, flag=wx.ALIGN_CENTER, pos=(2, 0))
  1343. def afterRegionDrawn(self):
  1344. """Function to update the title and the number of selected area"""
  1345. self.areascount = self.areascount + 1
  1346. if self.areascount == self.areanum:
  1347. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1348. return True
  1349. else:
  1350. self.title.SetLabel(_('Select sample area ' + str(self.areascount + 1) \
  1351. + ' of ' + str(self.areanum)))
  1352. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1353. return False
  1354. def OnYes(self, event):
  1355. """Function to create the string for the conf file if the area
  1356. is selected"""
  1357. self.parent.msAreaList.append(obtainAreaVector(self.outname))
  1358. if not self.afterRegionDrawn():
  1359. self.newCat()
  1360. def OnNo(self, event):
  1361. """Function to pass to the next feature if it is not selected"""
  1362. if not self.afterRegionDrawn():
  1363. self.newCat()
  1364. def newCat(self):
  1365. """Convert to raster and draw the new feature"""
  1366. cat = self.vect_cats[self.areascount]
  1367. self.outname = "{name}_{cat}".format(name=self.vect.split('@')[0],
  1368. cat=cat)
  1369. convertFeature(self.vect, self.outname, cat, self.rast,
  1370. layer=self.vectlayer)
  1371. cmdlistcat = ['d.rast', 'map=%s' % self.outname]
  1372. self.map_.AddLayer(ltype='raster', command=cmdlistcat, active=True,
  1373. name=self.outname, hidden=False, opacity=1.0,
  1374. render=True)
  1375. for l in self.map_.GetListOfLayers():
  1376. if l.name == self.outname:
  1377. self.mapPanel.mapWindow.ZoomToMap(layers=[l], render=True,
  1378. ignoreNulls=True)
  1379. elif l.name != self.rast:
  1380. self.map_.DeleteLayer(l)
  1381. self.areaText.SetLabel("Is this area (cat={n}) ok?".format(n=cat))
  1382. def OnEnterPage(self, event):
  1383. """Function during entering: draw the raster map and the first vector
  1384. feature"""
  1385. if self.mapPanel is None:
  1386. self.mapPanel = RLiSetupMapPanel(self, samplingType=self.parent.samplingareapage.samplingtype)
  1387. self.sizer.Add(item=self.mapPanel, flag=wx.EXPAND, pos=(1, 0))
  1388. self.sizer.AddGrowableCol(0)
  1389. self.sizer.AddGrowableRow(1)
  1390. self._raster = None
  1391. self.rast = self.parent.startpage.rast
  1392. self.vect = self.parent.startpage.vect
  1393. self.vect_cats = obtainCategories(self.vect, layer=self.vectlayer)
  1394. self.areanum = len(self.vect_cats)
  1395. if self.areanum == 0:
  1396. GError(parent=self, message=_("The polygon seems to have 0 areas"))
  1397. self.parent.wizard.ShowPage(self.parent.samplingareapage)
  1398. return
  1399. self.title.SetLabel(_('Select sample area 1 of ' + str(self.areanum)))
  1400. grass.use_temp_region()
  1401. if self._raster != self.rast:
  1402. self.map_ = self.mapPanel.GetMap()
  1403. self.map_.DeleteAllLayers()
  1404. cmdlist = ['d.rast', 'map=%s' % self.rast]
  1405. self.map_.AddLayer(ltype='raster', command=cmdlist, active=True,
  1406. name=self.rast, hidden=False, opacity=1.0,
  1407. render=True)
  1408. self.newCat()
  1409. def OnExitPage(self, event=None):
  1410. """Function during exiting"""
  1411. grass.del_temp_region()
  1412. # if event.GetDirection():
  1413. # self.SetNext(self.parent.samplingareapage)
  1414. # self.parent.samplingareapage.SetPrev(self)
  1415. class SummaryPage(TitledPage):
  1416. """Shows summary result of choosing data"""
  1417. def __init__(self, wizard, parent):
  1418. TitledPage.__init__(self, wizard, _("Summary"))
  1419. global rlisettings
  1420. self.parent = parent
  1421. #configuration file name
  1422. self.conflabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1423. label=_('Configuration file name:'))
  1424. self.conftxt = wx.StaticText(parent=self, id=wx.ID_ANY,
  1425. label="")
  1426. self.sizer.Add(item=self.conflabel, border=5, pos=(0, 0),
  1427. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1428. self.sizer.Add(item=self.conftxt, border=5, pos=(0, 1),
  1429. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1430. #raster name
  1431. self.rastlabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1432. label=_('Raster name:'))
  1433. self.rasttxt = wx.StaticText(parent=self, id=wx.ID_ANY,
  1434. label="")
  1435. self.sizer.Add(item=self.rastlabel, border=5, pos=(1, 0),
  1436. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1437. self.sizer.Add(item=self.rasttxt, border=5, pos=(1, 1),
  1438. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1439. #vector name
  1440. self.vectlabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1441. label=_('Vector name:'))
  1442. self.vecttxt = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1443. self.sizer.Add(item=self.vectlabel, border=5, pos=(2, 0),
  1444. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1445. self.sizer.Add(item=self.vecttxt, border=5, pos=(2, 1),
  1446. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1447. #region type name
  1448. self.regionlabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1449. label=_('Region type:'))
  1450. self.regiontxt = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1451. self.sizer.Add(item=self.regionlabel, border=5, pos=(3, 0),
  1452. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1453. self.sizer.Add(item=self.regiontxt, border=5, pos=(3, 1),
  1454. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1455. #region keyboard
  1456. self.regionkeylabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1457. label="")
  1458. self.regionkeytxt = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1459. self.sizer.Add(item=self.regionkeylabel, border=5, pos=(4, 0),
  1460. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1461. self.sizer.Add(item=self.regionkeytxt, border=5, pos=(4, 1),
  1462. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1463. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1464. #sampling area
  1465. self.samplinglabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1466. label=_('Sampling area type:'))
  1467. self.samplingtxt = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1468. self.sizer.Add(item=self.samplinglabel, border=5, pos=(5, 0),
  1469. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1470. self.sizer.Add(item=self.samplingtxt, border=5, pos=(5, 1),
  1471. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1472. #shapetype
  1473. self.shapelabel = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1474. self.shapetxt = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1475. self.sizer.Add(item=self.shapelabel, border=5, pos=(6, 0),
  1476. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1477. self.sizer.Add(item=self.shapetxt, border=5, pos=(6, 1),
  1478. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1479. #shapedim
  1480. self.shapewidthlabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1481. label="")
  1482. self.shapewidthtxt = wx.StaticText(parent=self, id=wx.ID_ANY,
  1483. label="")
  1484. self.sizer.Add(item=self.shapewidthlabel, border=5, pos=(7, 0),
  1485. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1486. self.sizer.Add(item=self.shapewidthtxt, border=5, pos=(7, 1),
  1487. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1488. self.shapeheightlabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1489. label="")
  1490. self.shapeheighttxt = wx.StaticText(parent=self, id=wx.ID_ANY,
  1491. label="")
  1492. self.sizer.Add(item=self.shapeheightlabel, border=5, pos=(8, 0),
  1493. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1494. self.sizer.Add(item=self.shapeheighttxt, border=5, pos=(8, 1),
  1495. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1496. #units type
  1497. self.unitslabel = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1498. self.unitstxt = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1499. self.sizer.Add(item=self.unitslabel, border=5, pos=(9, 0),
  1500. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1501. self.sizer.Add(item=self.unitstxt, border=5, pos=(9, 1),
  1502. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1503. self.unitsmorelabel = wx.StaticText(parent=self, id=wx.ID_ANY,
  1504. label="")
  1505. self.unitsmoretxt = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1506. self.sizer.Add(item=self.unitsmorelabel, border=5, pos=(10, 0),
  1507. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1508. self.sizer.Add(item=self.unitsmoretxt, border=5, pos=(10, 1),
  1509. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1510. self.unitsmorelabel2 = wx.StaticText(parent=self, id=wx.ID_ANY,
  1511. label="")
  1512. self.unitsmoretxt2 = wx.StaticText(parent=self, id=wx.ID_ANY, label="")
  1513. self.sizer.Add(item=self.unitsmorelabel2, border=5, pos=(11, 0),
  1514. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1515. self.sizer.Add(item=self.unitsmoretxt2, border=5, pos=(11, 1),
  1516. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL)
  1517. def OnEnterPage(self, event):
  1518. """Insert values into text controls for summary of location
  1519. creation options
  1520. """
  1521. self.conftxt.SetLabel(self.parent.startpage.conf_name)
  1522. self.rasttxt.SetLabel(self.parent.startpage.rast)
  1523. self.samplingtxt.SetLabel(self.parent.samplingareapage.samplingtype)
  1524. self.regiontxt.SetLabel(self.parent.startpage.region)
  1525. self.vecttxt.SetLabel(self.parent.startpage.vect)
  1526. if self.parent.startpage.region == 'key':
  1527. #region keyboard values
  1528. self.regionkeylabel.SetLabel(_('Region keyboard values:'))
  1529. regKeyVals = "Column left up: %s - Row left up: %s\n" \
  1530. "Column length: %s - Row length: %s\n" % (
  1531. self.parent.keyboardpage.col_up,
  1532. self.parent.keyboardpage.row_up,
  1533. self.parent.keyboardpage.col_len,
  1534. self.parent.keyboardpage.row_len)
  1535. self.regionkeytxt.SetLabel(regKeyVals)
  1536. else:
  1537. self.regionkeylabel.SetLabel("")
  1538. self.regionkeytxt.SetLabel("")
  1539. if self.parent.samplingareapage.samplingtype == SamplingType.UNITS \
  1540. and self.parent.samplingareapage.regionbox == 'keyboard':
  1541. self.shapelabel.SetLabel(_('Type of shape:'))
  1542. self.shapetxt.SetLabel(self.parent.units.boxtype)
  1543. if self.parent.units.boxtype == 'circle':
  1544. self.shapewidthlabel.SetLabel(_("Radius size:"))
  1545. self.shapeheightlabel.SetLabel(_("Name circle mask:"))
  1546. else:
  1547. self.shapewidthlabel.SetLabel(_("Width size:"))
  1548. self.shapeheightlabel.SetLabel(_("Heigth size:"))
  1549. self.shapewidthtxt.SetLabel(self.parent.units.width)
  1550. self.shapeheighttxt.SetLabel(self.parent.units.height)
  1551. self.unitslabel.SetLabel(_("Method of distribution:"))
  1552. self.unitstxt.SetLabel(self.parent.units.distrtype)
  1553. if self.parent.units.distrtype == 'non_overlapping':
  1554. self.unitsmorelabel.SetLabel(_("Number sampling units:"))
  1555. self.unitsmoretxt.SetLabel(self.parent.units.distr1)
  1556. elif self.parent.units.distrtype == 'systematic_noncontiguos':
  1557. self.unitsmorelabel.SetLabel(_("Distance between units:"))
  1558. self.unitsmoretxt.SetLabel(self.parent.units.distr1)
  1559. elif self.parent.units.distrtype == 'stratified_random':
  1560. self.unitsmorelabel.SetLabel(_("Number row strates:"))
  1561. self.unitsmoretxt.SetLabel(self.parent.units.distr1)
  1562. self.unitsmorelabel2.SetLabel(_("Number column strates:"))
  1563. self.unitsmoretxt2.SetLabel(self.parent.units.distr2)
  1564. elif self.parent.samplingareapage.samplingtype == SamplingType.UNITS \
  1565. and self.parent.samplingareapage.regionbox == 'mouse':
  1566. self.shapelabel.SetLabel(_('Type of shape:'))
  1567. self.shapetxt.SetLabel(self.parent.units.boxtype)
  1568. self.unitstxt.SetLabel('by mouse')
  1569. elif self.parent.samplingareapage.samplingtype == 'moving':
  1570. self.shapelabel.SetLabel(_('Type of shape:'))
  1571. self.shapetxt.SetLabel(self.parent.moving.boxtype)
  1572. if self.parent.moving.boxtype == 'circle':
  1573. self.shapewidthlabel.SetLabel(_("Radius size:"))
  1574. self.shapeheightlabel.SetLabel(_("Name circle mask:"))
  1575. else:
  1576. self.shapewidthlabel.SetLabel(_("Width size:"))
  1577. self.shapeheightlabel.SetLabel(_("Heigth size:"))
  1578. self.shapewidthtxt.SetLabel(self.parent.moving.width)
  1579. self.shapeheighttxt.SetLabel(self.parent.moving.height)
  1580. else:
  1581. self.shapelabel.SetLabel("")
  1582. self.shapetxt.SetLabel("")
  1583. self.shapewidthlabel.SetLabel("")
  1584. self.shapewidthtxt.SetLabel("")
  1585. self.shapeheightlabel.SetLabel("")
  1586. self.shapeheighttxt.SetLabel("")