wizard.py 79 KB

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