ii2t_manager.py 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299
  1. """
  2. @package gcp.manager
  3. @brief Georectification module for GRASS GIS. Includes ground control
  4. point management and interactive point and click GCP creation
  5. Classes:
  6. - manager::GCPWizard
  7. - manager::LocationPage
  8. - manager::GroupPage
  9. - manager::DispMapPage
  10. - manager::GCP
  11. - manager::GCPList
  12. - manager::VectGroup
  13. - manager::EditGCP
  14. - manager::GrSettingsDialog
  15. (C) 2006-2017 by the GRASS Development Team
  16. This program is free software under the GNU General Public License
  17. (>=v2). Read the file COPYING that comes with GRASS for details.
  18. @author Original author Michael Barton
  19. @author Original version improved by Martin Landa <landa.martin gmail.com>
  20. @author Rewritten by Markus Metz redesign georectfier -> GCP Manage
  21. @author Support for GraphicsSet added by Stepan Turek <stepan.turek seznam.cz> (2012)
  22. @author port i.image.2target (v6) to version 7 in 2017 by Yann
  23. """
  24. #TODO: i.ortho.transform has 6 appearances, check each of them and configure
  25. #TODO: i.ortho.transform looks for REF_POINTS/CONTROL_POINTS and not POINTS
  26. #TODO: CHECK CONTROL_POINTS format and create it for i.ortho.transform to use.
  27. from __future__ import print_function
  28. import os
  29. import sys
  30. import six
  31. import shutil
  32. from copy import copy
  33. import wx
  34. from wx.lib.mixins.listctrl import ColumnSorterMixin, ListCtrlAutoWidthMixin
  35. import wx.lib.colourselect as csel
  36. from core import globalvar
  37. if globalvar.wxPythonPhoenix:
  38. from wx import adv as wiz
  39. else:
  40. from wx import wizard as wiz
  41. import grass.script as grass
  42. from core import utils
  43. from core.render import Map
  44. from gui_core.gselect import Select, LocationSelect, MapsetSelect
  45. from gui_core.dialogs import GroupDialog
  46. from core.gcmd import RunCommand, GMessage, GError, GWarning
  47. from core.settings import UserSettings
  48. from gcp.mapdisplay import MapFrame
  49. from core.giface import Notification
  50. from gui_core.wrap import SpinCtrl, Button, StaticText, StaticBox, \
  51. CheckListBox, TextCtrl, Menu, ListCtrl, BitmapFromImage, CheckListCtrlMixin
  52. from location_wizard.wizard import GridBagSizerTitledPage as TitledPage
  53. #
  54. # global variables
  55. #
  56. global src_map
  57. global tgt_map
  58. global maptype
  59. src_map = ''
  60. tgt_map = {'raster': '',
  61. 'vector': ''}
  62. maptype = 'raster'
  63. def getSmallUpArrowImage():
  64. stream = open(os.path.join(globalvar.IMGDIR, 'small_up_arrow.png'), 'rb')
  65. try:
  66. img = wx.Image(stream)
  67. finally:
  68. stream.close()
  69. return img
  70. def getSmallDnArrowImage():
  71. stream = open(os.path.join(globalvar.IMGDIR, 'small_down_arrow.png'), 'rb')
  72. try:
  73. img = wx.Image(stream)
  74. finally:
  75. stream.close()
  76. stream.close()
  77. return img
  78. class GCPWizard(object):
  79. """
  80. Start wizard here and finish wizard here
  81. """
  82. #def __init__(self, parent, giface, srcloc, srcmpt, srcgrp, srcras, tgtras, camera, order, extension):
  83. # global maptype
  84. # global src_map
  85. # global tgt_map
  86. # maptype = 'raster'
  87. # rendertype = 'raster'
  88. # self.parent = parent # GMFrame
  89. # self._giface = giface
  90. # self.srcloc = srcloc
  91. # self.srcmpt = srcmpt
  92. # self.group = srcgrp
  93. # self.src_map = srcras
  94. # self.tgt_map = tgtras
  95. # self.camera = camera
  96. # self.order = int(order)
  97. # self.extension = extension
  98. # self.src_maps = self.src_map
  99. # # location for xy map to georectify
  100. # self.newlocation = self.srcloc
  101. # # mapset for xy map to georectify
  102. # self.newmapset = self.srcmpt
  103. def __init__(self, parent, giface):
  104. self.parent = parent # GMFrame
  105. self._giface = giface
  106. #
  107. # get environmental variables
  108. #
  109. self.grassdatabase = grass.gisenv()['GISDBASE']
  110. #
  111. # read original environment settings
  112. #
  113. self.target_gisrc = os.environ['GISRC']
  114. self.gisrc_dict = {}
  115. try:
  116. f = open(self.target_gisrc, 'r')
  117. for line in f.readlines():
  118. line = line.replace('\n', '').strip()
  119. if len(line) < 1:
  120. continue
  121. key, value = line.split(':', 1)
  122. self.gisrc_dict[key.strip()] = value.strip()
  123. finally:
  124. f.close()
  125. self.currentlocation = self.gisrc_dict['LOCATION_NAME']
  126. self.currentmapset = self.gisrc_dict['MAPSET']
  127. # location for xy map to georectify
  128. self.newlocation = ''
  129. # mapset for xy map to georectify
  130. self.newmapset = ''
  131. global maptype
  132. global src_map
  133. global tgt_map
  134. #src_map = ''
  135. #tgt_map = ''
  136. maptype = 'raster'
  137. # GISRC file for source location/mapset of map(s) to georectify
  138. self.source_gisrc = ''
  139. self.src_maps = []
  140. # Raster map with camera angle relative to ground surface (i.ortho.rectify)
  141. self.cam_angle = ''
  142. #
  143. # define wizard pages
  144. #
  145. self.wizard = wiz.Wizard(
  146. parent=parent,
  147. id=wx.ID_ANY,
  148. title=_("Setup for georectification"))
  149. self.startpage = LocationPage(self.wizard, self)
  150. self.grouppage = GroupPage(self.wizard, self)
  151. self.mappage = DispMapPage(self.wizard, self)
  152. #
  153. # set the initial order of the pages
  154. #
  155. self.startpage.SetNext(self.grouppage)
  156. self.grouppage.SetPrev(self.startpage)
  157. self.grouppage.SetNext(self.mappage)
  158. self.mappage.SetPrev(self.grouppage)
  159. #
  160. # do pages layout
  161. #
  162. self.startpage.DoLayout()
  163. self.grouppage.DoLayout()
  164. self.mappage.DoLayout()
  165. self.wizard.FitToPage(self.startpage)
  166. # self.Bind(wx.EVT_CLOSE, self.Cleanup)
  167. # self.parent.Bind(wx.EVT_ACTIVATE, self.OnGLMFocus)
  168. success = False
  169. #
  170. # run wizard
  171. #
  172. if self.wizard.RunWizard(self.startpage):
  173. success = self.OnWizFinished()
  174. if not success:
  175. GMessage(parent=self.parent,
  176. message=_("Georectifying setup canceled."))
  177. self.Cleanup()
  178. else:
  179. GMessage(parent=self.parent,
  180. message=_("Georectifying setup canceled."))
  181. self.Cleanup()
  182. #
  183. # start GCP display
  184. #
  185. if success:
  186. # instance of render.Map to be associated with display
  187. self.SwitchEnv('source')
  188. self.SrcMap = Map(gisrc=self.source_gisrc)
  189. self.SwitchEnv('target')
  190. self.TgtMap = Map(gisrc=self.target_gisrc)
  191. self.Map = self.SrcMap
  192. #
  193. # add layer to source map
  194. #
  195. if maptype == 'raster':
  196. rendertype = 'raster'
  197. cmdlist = ['d.rast', 'map=%s' % src_map]
  198. else: # -> vector layer
  199. rendertype = 'vector'
  200. cmdlist = ['d.vect', 'map=%s' % src_map]
  201. self.SwitchEnv('source')
  202. name, found = utils.GetLayerNameFromCmd(cmdlist)
  203. self.SrcMap.AddLayer(
  204. ltype=rendertype,
  205. command=cmdlist,
  206. active=True,
  207. name=name,
  208. hidden=False,
  209. opacity=1.0,
  210. render=False)
  211. self.SwitchEnv('target')
  212. if tgt_map['raster']:
  213. #
  214. # add raster layer to target map
  215. #
  216. rendertype = 'raster'
  217. cmdlist = ['d.rast', 'map=%s' % tgt_map['raster']]
  218. name, found = utils.GetLayerNameFromCmd(cmdlist)
  219. self.TgtMap.AddLayer(
  220. ltype=rendertype,
  221. command=cmdlist,
  222. active=True,
  223. name=name,
  224. hidden=False,
  225. opacity=1.0,
  226. render=False)
  227. if tgt_map['vector']:
  228. #
  229. # add raster layer to target map
  230. #
  231. rendertype = 'vector'
  232. cmdlist = ['d.vect', 'map=%s' % tgt_map['vector']]
  233. name, found = utils.GetLayerNameFromCmd(cmdlist)
  234. self.TgtMap.AddLayer(
  235. ltype=rendertype,
  236. command=cmdlist,
  237. active=True,
  238. name=name,
  239. hidden=False,
  240. opacity=1.0,
  241. render=False)
  242. #
  243. # start GCP Manager
  244. #
  245. self.gcpmgr = GCP(self.parent, giface=self._giface,
  246. grwiz=self, size=globalvar.MAP_WINDOW_SIZE,
  247. toolbars=["gcpdisp"],
  248. Map=self.SrcMap, lmgr=self.parent)
  249. # load GCPs
  250. self.gcpmgr.InitMapDisplay()
  251. self.gcpmgr.CenterOnScreen()
  252. self.gcpmgr.Show()
  253. # need to update AUI here for wingrass
  254. self.gcpmgr._mgr.Update()
  255. else:
  256. self.Cleanup()
  257. def SetSrcEnv(self, location, mapset):
  258. """Create environment to use for location and mapset
  259. that are the source of the file(s) to georectify
  260. :param location: source location
  261. :param mapset: source mapset
  262. :return: False on error
  263. :return: True on success
  264. """
  265. self.newlocation = location
  266. self.newmapset = mapset
  267. # check to see if we are georectifying map in current working
  268. # location/mapset
  269. if self.newlocation == self.currentlocation and self.newmapset == self.currentmapset:
  270. return False
  271. self.gisrc_dict['LOCATION_NAME'] = location
  272. self.gisrc_dict['MAPSET'] = mapset
  273. self.source_gisrc = utils.GetTempfile()
  274. try:
  275. f = open(self.source_gisrc, mode='w')
  276. for line in self.gisrc_dict.items():
  277. f.write(line[0] + ": " + line[1] + "\n")
  278. finally:
  279. f.close()
  280. return True
  281. def SwitchEnv(self, grc):
  282. """
  283. Switches between original working location/mapset and
  284. location/mapset that is source of file(s) to georectify
  285. """
  286. # check to see if we are georectifying map in current working
  287. # location/mapset
  288. if self.newlocation == self.currentlocation and self.newmapset == self.currentmapset:
  289. return False
  290. if grc == 'target':
  291. os.environ['GISRC'] = str(self.target_gisrc)
  292. elif grc == 'source':
  293. os.environ['GISRC'] = str(self.source_gisrc)
  294. return True
  295. def OnWizFinished(self):
  296. # self.Cleanup()
  297. return True
  298. def OnGLMFocus(self, event):
  299. """Layer Manager focus"""
  300. # self.SwitchEnv('target')
  301. event.Skip()
  302. def Cleanup(self):
  303. """Return to current location and mapset"""
  304. # here was also the cleaning of gcpmanagement from layer manager
  305. # which is no longer needed
  306. self.SwitchEnv('target')
  307. self.wizard.Destroy()
  308. class LocationPage(TitledPage):
  309. """
  310. Set map type (raster or vector) to georectify and
  311. select location/mapset of map(s) to georectify.
  312. """
  313. def __init__(self, wizard, parent):
  314. TitledPage.__init__(self, wizard, _(
  315. "Select map type and location/mapset"))
  316. self.parent = parent
  317. self.grassdatabase = self.parent.grassdatabase
  318. self.xylocation = ''
  319. self.xymapset = ''
  320. #
  321. # layout
  322. #
  323. # map type
  324. self.rb_maptype = wx.RadioBox(
  325. parent=self, id=wx.ID_ANY, label=' %s ' %
  326. _("Map type to georectify"), choices=[
  327. _('raster'), _('vector')], majorDimension=wx.RA_SPECIFY_COLS)
  328. self.sizer.Add(self.rb_maptype,
  329. flag=wx.ALIGN_CENTER | wx.ALL | wx.EXPAND, border=5,
  330. pos=(1, 1), span=(1, 2))
  331. # location
  332. self.sizer.Add(
  333. StaticText(
  334. parent=self,
  335. id=wx.ID_ANY,
  336. label=_('Select source location:')),
  337. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  338. border=5,
  339. pos=(
  340. 2,
  341. 1))
  342. self.cb_location = LocationSelect(
  343. parent=self, gisdbase=self.grassdatabase)
  344. self.sizer.Add(
  345. self.cb_location,
  346. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  347. border=5,
  348. pos=(
  349. 2,
  350. 2))
  351. # mapset
  352. self.sizer.Add(
  353. StaticText(
  354. parent=self,
  355. id=wx.ID_ANY,
  356. label=_('Select source mapset:')),
  357. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  358. border=5,
  359. pos=(
  360. 3,
  361. 1))
  362. self.cb_mapset = MapsetSelect(parent=self, gisdbase=self.grassdatabase,
  363. setItems=False)
  364. self.sizer.Add(self.cb_mapset, flag=wx.ALIGN_LEFT |
  365. wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5, pos=(3, 2))
  366. self.sizer.AddGrowableCol(2)
  367. #
  368. # bindings
  369. #
  370. self.Bind(wx.EVT_RADIOBOX, self.OnMaptype, self.rb_maptype)
  371. self.Bind(wx.EVT_COMBOBOX, self.OnLocation, self.cb_location)
  372. self.cb_mapset.Bind(wx.EVT_TEXT, self.OnMapset)
  373. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  374. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  375. # self.Bind(wx.EVT_CLOSE, self.parent.Cleanup)
  376. def OnMaptype(self, event):
  377. """Change map type"""
  378. global maptype
  379. if event.GetInt() == 0:
  380. maptype = 'raster'
  381. else:
  382. maptype = 'vector'
  383. def OnLocation(self, event):
  384. """Sets source location for map(s) to georectify"""
  385. self.xylocation = event.GetString()
  386. # create a list of valid mapsets
  387. tmplist = os.listdir(os.path.join(self.grassdatabase, self.xylocation))
  388. self.mapsetList = []
  389. for item in tmplist:
  390. if os.path.isdir(os.path.join(self.grassdatabase, self.xylocation, item)) and \
  391. os.path.exists(os.path.join(self.grassdatabase, self.xylocation, item, 'WIND')):
  392. if item != 'PERMANENT':
  393. self.mapsetList.append(item)
  394. self.xymapset = 'PERMANENT'
  395. utils.ListSortLower(self.mapsetList)
  396. self.mapsetList.insert(0, 'PERMANENT')
  397. self.cb_mapset.SetItems(self.mapsetList)
  398. self.cb_mapset.SetStringSelection(self.xymapset)
  399. if not wx.FindWindowById(wx.ID_FORWARD).IsEnabled():
  400. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  401. def OnMapset(self, event):
  402. """Sets source mapset for map(s) to georectify"""
  403. if self.xylocation == '':
  404. GMessage(_('You must select a valid location '
  405. 'before selecting a mapset'),
  406. parent=self)
  407. return
  408. self.xymapset = event.GetString()
  409. if not wx.FindWindowById(wx.ID_FORWARD).IsEnabled():
  410. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  411. def OnPageChanging(self, event=None):
  412. if event.GetDirection() and \
  413. (self.xylocation == '' or self.xymapset == ''):
  414. GMessage(_('You must select a valid location '
  415. 'and mapset in order to continue'),
  416. parent=self)
  417. event.Veto()
  418. return
  419. self.parent.SetSrcEnv(self.xylocation, self.xymapset)
  420. def OnEnterPage(self, event=None):
  421. if self.xylocation == '' or self.xymapset == '':
  422. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  423. else:
  424. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  425. class GroupPage(TitledPage):
  426. """
  427. Set group to georectify. Create group if desired.
  428. """
  429. def __init__(self, wizard, parent):
  430. TitledPage.__init__(self, wizard, _(
  431. "Select image/map group to georectify"))
  432. self.parent = parent
  433. self.grassdatabase = self.parent.grassdatabase
  434. self.groupList = []
  435. self.xylocation = ''
  436. self.xymapset = ''
  437. self.xygroup = ''
  438. # default extension
  439. self.extension = '_georect' + str(os.getpid())
  440. #
  441. # layout
  442. #
  443. # group
  444. self.sizer.Add(
  445. StaticText(
  446. parent=self,
  447. id=wx.ID_ANY,
  448. label=_('Select group:')),
  449. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  450. border=5,
  451. pos=(
  452. 1,
  453. 1))
  454. self.cb_group = wx.ComboBox(parent=self, id=wx.ID_ANY,
  455. choices=self.groupList, size=(350, -1),
  456. style=wx.CB_DROPDOWN | wx.CB_READONLY)
  457. self.sizer.Add(self.cb_group, flag=wx.ALIGN_LEFT |
  458. wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5, pos=(1, 2))
  459. # create group
  460. self.sizer.Add(
  461. StaticText(
  462. parent=self,
  463. id=wx.ID_ANY,
  464. label=_('Create group if none exists')),
  465. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  466. border=5,
  467. pos=(
  468. 2,
  469. 1))
  470. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  471. self.btn_mkgroup = Button(
  472. parent=self,
  473. id=wx.ID_ANY,
  474. label=_("Create/edit group..."))
  475. self.btn_vgroup = Button(
  476. parent=self,
  477. id=wx.ID_ANY,
  478. label=_("Add vector map to group..."))
  479. btnSizer.Add(self.btn_mkgroup,
  480. flag=wx.RIGHT, border=5)
  481. btnSizer.Add(self.btn_vgroup,
  482. flag=wx.LEFT, border=5)
  483. self.sizer.Add(
  484. btnSizer,
  485. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  486. border=5,
  487. pos=(
  488. 2,
  489. 2))
  490. # extension
  491. self.sizer.Add(
  492. StaticText(
  493. parent=self,
  494. id=wx.ID_ANY,
  495. label=_('Extension for output maps:')),
  496. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  497. border=5,
  498. pos=(
  499. 3,
  500. 1))
  501. self.ext_txt = TextCtrl(
  502. parent=self, id=wx.ID_ANY, value="", size=(
  503. 350, -1))
  504. self.ext_txt.SetValue(self.extension)
  505. self.sizer.Add(self.ext_txt, flag=wx.ALIGN_LEFT |
  506. wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5, pos=(3, 2))
  507. self.sizer.AddGrowableCol(2)
  508. #
  509. # bindings
  510. #
  511. self.Bind(wx.EVT_COMBOBOX, self.OnGroup, self.cb_group)
  512. self.Bind(wx.EVT_TEXT, self.OnExtension, self.ext_txt)
  513. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  514. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  515. self.Bind(wx.EVT_CLOSE, self.parent.Cleanup)
  516. # hide vector group button by default
  517. self.btn_vgroup.Hide()
  518. def OnGroup(self, event):
  519. self.xygroup = event.GetString()
  520. def OnMkGroup(self, event):
  521. """Create new group in source location/mapset"""
  522. dlg = GroupDialog(parent=self, defaultGroup=self.xygroup)
  523. dlg.DisableSubgroupEdit()
  524. dlg.ShowModal()
  525. gr, s = dlg.GetSelectedGroup()
  526. if gr in dlg.GetExistGroups():
  527. self.xygroup = gr
  528. else:
  529. gr = ''
  530. dlg.Destroy()
  531. self.OnEnterPage()
  532. self.Update()
  533. def OnVGroup(self, event):
  534. """Add vector maps to group"""
  535. dlg = VectGroup(parent=self,
  536. id=wx.ID_ANY,
  537. grassdb=self.grassdatabase,
  538. location=self.xylocation,
  539. mapset=self.xymapset,
  540. group=self.xygroup)
  541. if dlg.ShowModal() != wx.ID_OK:
  542. return
  543. dlg.MakeVGroup()
  544. self.OnEnterPage()
  545. def OnExtension(self, event):
  546. self.extension = self.ext_txt.GetValue()
  547. def OnPageChanging(self, event=None):
  548. if event.GetDirection() and self.xygroup == '':
  549. GMessage(_('You must select a valid image/map '
  550. 'group in order to continue'),
  551. parent=self)
  552. event.Veto()
  553. return
  554. if event.GetDirection() and self.extension == '':
  555. GMessage(_('You must enter an map name '
  556. 'extension in order to continue'),
  557. parent=self)
  558. event.Veto()
  559. return
  560. def OnEnterPage(self, event=None):
  561. global maptype
  562. self.groupList = []
  563. self.xylocation = self.parent.gisrc_dict['LOCATION_NAME']
  564. self.xymapset = self.parent.gisrc_dict['MAPSET']
  565. # create a list of groups in selected mapset
  566. if os.path.isdir(os.path.join(self.grassdatabase,
  567. self.xylocation,
  568. self.xymapset,
  569. 'group')):
  570. tmplist = os.listdir(os.path.join(self.grassdatabase,
  571. self.xylocation,
  572. self.xymapset,
  573. 'group'))
  574. for item in tmplist:
  575. if os.path.isdir(os.path.join(self.grassdatabase,
  576. self.xylocation,
  577. self.xymapset,
  578. 'group',
  579. item)):
  580. self.groupList.append(item)
  581. if maptype == 'raster':
  582. self.btn_vgroup.Hide()
  583. self.Bind(wx.EVT_BUTTON, self.OnMkGroup, self.btn_mkgroup)
  584. elif maptype == 'vector':
  585. self.btn_vgroup.Show()
  586. self.Bind(wx.EVT_BUTTON, self.OnMkGroup, self.btn_mkgroup)
  587. self.Bind(wx.EVT_BUTTON, self.OnVGroup, self.btn_vgroup)
  588. utils.ListSortLower(self.groupList)
  589. self.cb_group.SetItems(self.groupList)
  590. if len(self.groupList) > 0:
  591. if self.xygroup and self.xygroup in self.groupList:
  592. self.cb_group.SetStringSelection(self.xygroup)
  593. else:
  594. self.cb_group.SetSelection(0)
  595. self.xygroup = self.groupList[0]
  596. if self.xygroup == '' or \
  597. self.extension == '':
  598. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  599. else:
  600. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  601. # switch to source
  602. self.parent.SwitchEnv('source')
  603. class DispMapPage(TitledPage):
  604. """
  605. Select ungeoreferenced map to display for interactively
  606. setting ground control points (GCPs).
  607. """
  608. def __init__(self, wizard, parent):
  609. TitledPage.__init__(
  610. self, wizard,
  611. _("Select maps to display for ground control point (GCP) creation"))
  612. self.parent = parent
  613. global maptype
  614. #
  615. # layout
  616. #
  617. self.sizer.Add(
  618. StaticText(
  619. parent=self,
  620. id=wx.ID_ANY,
  621. label=_('Select source map to display:')),
  622. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  623. border=5,
  624. pos=(
  625. 1,
  626. 1))
  627. self.srcselection = Select(
  628. self,
  629. id=wx.ID_ANY,
  630. size=globalvar.DIALOG_GSELECT_SIZE,
  631. type=maptype,
  632. updateOnPopup=False)
  633. self.sizer.Add(
  634. self.srcselection,
  635. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  636. border=5,
  637. pos=(
  638. 1,
  639. 2))
  640. self.sizer.Add(
  641. StaticText(
  642. parent=self,
  643. id=wx.ID_ANY,
  644. label=_('Select target raster map to display:')),
  645. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  646. border=5,
  647. pos=(
  648. 2,
  649. 1))
  650. self.tgtrastselection = Select(
  651. self, id=wx.ID_ANY, size=globalvar.DIALOG_GSELECT_SIZE,
  652. type='raster', updateOnPopup=False)
  653. self.sizer.Add(
  654. self.tgtrastselection,
  655. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  656. border=5,
  657. pos=(
  658. 2,
  659. 2))
  660. self.sizer.Add(
  661. StaticText(
  662. parent=self,
  663. id=wx.ID_ANY,
  664. label=_('Select target vector map to display:')),
  665. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  666. border=5,
  667. pos=(
  668. 3,
  669. 1))
  670. self.tgtvectselection = Select(
  671. self, id=wx.ID_ANY, size=globalvar.DIALOG_GSELECT_SIZE,
  672. type='vector', updateOnPopup=False)
  673. self.sizer.Add(
  674. self.tgtvectselection,
  675. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  676. border=5,
  677. pos=(
  678. 3,
  679. 2))
  680. #
  681. # bindings
  682. #
  683. self.srcselection.Bind(wx.EVT_TEXT, self.OnSrcSelection)
  684. self.tgtrastselection.Bind(wx.EVT_TEXT, self.OnTgtRastSelection)
  685. self.tgtvectselection.Bind(wx.EVT_TEXT, self.OnTgtVectSelection)
  686. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  687. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  688. self.Bind(wx.EVT_CLOSE, self.parent.Cleanup)
  689. def OnSrcSelection(self, event):
  690. """Source map to display selected"""
  691. global src_map
  692. global maptype
  693. src_map = self.srcselection.GetValue()
  694. if src_map == '':
  695. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  696. else:
  697. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  698. try:
  699. # set computational region to match selected map and zoom display
  700. # to region
  701. if maptype == 'raster':
  702. p = RunCommand('g.region', 'raster=src_map')
  703. elif maptype == 'vector':
  704. p = RunCommand('g.region', 'vector=src_map')
  705. if p.returncode == 0:
  706. print('returncode = ', str(p.returncode))
  707. self.parent.Map.region = self.parent.Map.GetRegion()
  708. except:
  709. pass
  710. def OnTgtRastSelection(self, event):
  711. """Source map to display selected"""
  712. global tgt_map
  713. tgt_map['raster'] = self.tgtrastselection.GetValue()
  714. def OnTgtVectSelection(self, event):
  715. """Source map to display selected"""
  716. global tgt_map
  717. tgt_map['vector'] = self.tgtvectselection.GetValue()
  718. def OnPageChanging(self, event=None):
  719. global src_map
  720. global tgt_map
  721. if event.GetDirection() and (src_map == ''):
  722. GMessage(_('You must select a source map '
  723. 'in order to continue'),
  724. parent=self)
  725. event.Veto()
  726. return
  727. self.parent.SwitchEnv('target')
  728. def OnEnterPage(self, event=None):
  729. global maptype
  730. global src_map
  731. global tgt_map
  732. self.srcselection.SetElementList(maptype)
  733. if maptype == 'raster':
  734. ret = RunCommand('i.group',
  735. parent=self,
  736. read=True,
  737. group=self.parent.grouppage.xygroup,
  738. flags='g')
  739. if ret:
  740. self.parent.src_maps = ret.splitlines()
  741. else:
  742. GError(
  743. parent=self, message=_(
  744. 'No maps in selected group <%s>.\n'
  745. 'Please edit group or select another group.') %
  746. self.parent.grouppage.xygroup)
  747. return
  748. elif maptype == 'vector':
  749. grassdatabase = self.parent.grassdatabase
  750. xylocation = self.parent.gisrc_dict['LOCATION_NAME']
  751. xymapset = self.parent.gisrc_dict['MAPSET']
  752. # make list of vectors to georectify from VREF
  753. vgrpfile = os.path.join(grassdatabase,
  754. xylocation,
  755. xymapset,
  756. 'group',
  757. self.parent.grouppage.xygroup,
  758. 'VREF')
  759. f = open(vgrpfile)
  760. try:
  761. for vect in f.readlines():
  762. vect = vect.strip('\n')
  763. if len(vect) < 1:
  764. continue
  765. self.parent.src_maps.append(vect)
  766. finally:
  767. f.close()
  768. if len(self.parent.src_maps) < 1:
  769. GError(
  770. parent=self, message=_(
  771. 'No maps in selected group <%s>.\n'
  772. 'Please edit group or select another group.') %
  773. self.parent.grouppage.xygroup)
  774. return
  775. # filter out all maps not in group
  776. self.srcselection.tcp.GetElementList(elements=self.parent.src_maps)
  777. src_map = self.parent.src_maps[0]
  778. self.srcselection.SetValue(src_map)
  779. self.parent.SwitchEnv('target')
  780. self.tgtrastselection.SetElementList('raster')
  781. self.tgtrastselection.GetElementList()
  782. self.tgtvectselection.SetElementList('vector')
  783. self.tgtvectselection.GetElementList()
  784. self.parent.SwitchEnv('source')
  785. if src_map == '':
  786. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  787. else:
  788. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  789. class GCP(MapFrame, ColumnSorterMixin):
  790. """
  791. Manages ground control points for georectifying. Calculates RMS statistics.
  792. Calls i.ortho.rectify or v.rectify to georectify map.
  793. """
  794. def __init__(self, parent, giface, grwiz=None, id=wx.ID_ANY,
  795. title=_("Manage Ground Control Points"),
  796. size=(700, 300), toolbars=["gcpdisp"], Map=None, lmgr=None):
  797. self.grwiz = grwiz # GR Wizard
  798. self._giface = giface
  799. if tgt_map['raster'] == '' and tgt_map['vector'] == '':
  800. self.show_target = False
  801. else:
  802. self.show_target = True
  803. #wx.Frame.__init__(self, parent, id, title, size = size, name = "GCPFrame")
  804. MapFrame.__init__(
  805. self,
  806. parent=parent,
  807. giface=self._giface,
  808. title=title,
  809. size=size,
  810. Map=Map,
  811. toolbars=toolbars,
  812. name='GCPMapWindow')
  813. # init variables
  814. self.parent = parent
  815. #
  816. # register data structures for drawing GCP's
  817. #
  818. self.pointsToDrawTgt = self.TgtMapWindow.RegisterGraphicsToDraw(
  819. graphicsType="point", setStatusFunc=self.SetGCPSatus)
  820. self.pointsToDrawSrc = self.SrcMapWindow.RegisterGraphicsToDraw(
  821. graphicsType="point", setStatusFunc=self.SetGCPSatus)
  822. # connect to the map windows signals
  823. # used to add or edit GCP
  824. self.SrcMapWindow.mouseLeftUpPointer.connect(
  825. lambda x, y:
  826. self._onMouseLeftUpPointer(self.SrcMapWindow, x, y))
  827. self.TgtMapWindow.mouseLeftUpPointer.connect(
  828. lambda x, y:
  829. self._onMouseLeftUpPointer(self.TgtMapWindow, x, y))
  830. # window resized
  831. self.resize = False
  832. self.grassdatabase = self.grwiz.grassdatabase
  833. self.currentlocation = self.grwiz.currentlocation
  834. self.currentmapset = self.grwiz.currentmapset
  835. self.newlocation = self.grwiz.newlocation
  836. self.newmapset = self.grwiz.newmapset
  837. self.xylocation = self.grwiz.gisrc_dict['LOCATION_NAME']
  838. self.xymapset = self.grwiz.gisrc_dict['MAPSET']
  839. self.xygroup = self.grwiz.grouppage.xygroup
  840. self.src_maps = self.grwiz.src_maps
  841. self.extension = self.grwiz.grouppage.extension
  842. self.outname = ''
  843. self.VectGRList = []
  844. self.file = {
  845. 'control_points': os.path.join(self.grassdatabase,
  846. self.xylocation,
  847. self.xymapset,
  848. 'group',
  849. self.xygroup,
  850. 'CONTROL_POINTS'),
  851. 'control_points_bak': os.path.join(self.grassdatabase,
  852. self.xylocation,
  853. self.xymapset,
  854. 'group',
  855. self.xygroup,
  856. 'CONTROL_POINTS_BAK'),
  857. 'rgrp': os.path.join(self.grassdatabase,
  858. self.xylocation,
  859. self.xymapset,
  860. 'group',
  861. self.xygroup,
  862. 'REF'),
  863. 'vgrp': os.path.join(self.grassdatabase,
  864. self.xylocation,
  865. self.xymapset,
  866. 'group',
  867. self.xygroup,
  868. 'VREF'),
  869. 'target': os.path.join(self.grassdatabase,
  870. self.xylocation,
  871. self.xymapset,
  872. 'group',
  873. self.xygroup,
  874. 'TARGET'),
  875. 'elevation': os.path.join(self.grassdatabase,
  876. self.xylocation,
  877. self.xymapset,
  878. 'group',
  879. self.xygroup,
  880. 'ELEVATION'),
  881. }
  882. # make a backup of the current points file
  883. if os.path.exists(self.file['control_points']):
  884. shutil.copy(self.file['control_points'], self.file['control_points_bak'])
  885. # polynomial order transformation for georectification
  886. self.gr_order = 1
  887. # interpolation method for georectification
  888. self.gr_method = 'nearest'
  889. # region clipping for georectified map
  890. self.clip_to_region = False
  891. # number of GCPs selected to be used for georectification (checked)
  892. self.GCPcount = 0
  893. # forward RMS error
  894. self.fwd_rmserror = 0.0
  895. # backward RMS error
  896. self.bkw_rmserror = 0.0
  897. # list map coords and ID of map display they came from
  898. self.mapcoordlist = []
  899. self.mapcoordlist.append([0, # GCP number
  900. 0.0, # source east
  901. 0.0, # source north
  902. 0.0, # source height
  903. 0.0, # target east
  904. 0.0, # target north
  905. 0.0, # target height
  906. 0.0, # forward error
  907. 0.0]) # backward error
  908. # init vars to highlight high RMS errors
  909. self.highest_only = True
  910. self.show_unused = True
  911. self.highest_key = -1
  912. self.rmsthresh = 0
  913. self.rmsmean = 0
  914. self.rmssd = 0
  915. self.SetTarget(self.xygroup, self.currentlocation, self.currentmapset)
  916. self.itemDataMap = None
  917. # images for column sorting
  918. # CheckListCtrlMixin must set an ImageList first
  919. self.il = self.list.GetImageList(wx.IMAGE_LIST_SMALL)
  920. SmallUpArrow = BitmapFromImage(getSmallUpArrowImage())
  921. SmallDnArrow = BitmapFromImage(getSmallDnArrowImage())
  922. self.sm_dn = self.il.Add(SmallDnArrow)
  923. self.sm_up = self.il.Add(SmallUpArrow)
  924. # set mouse characteristics
  925. self.mapwin = self.SrcMapWindow
  926. self.mapwin.mouse['box'] = 'point'
  927. self.mapwin.mouse["use"] == "pointer"
  928. self.mapwin.zoomtype = 0
  929. self.mapwin.pen = wx.Pen(colour='black', width=2, style=wx.SOLID)
  930. self.mapwin.SetNamedCursor('cross')
  931. self.mapwin = self.TgtMapWindow
  932. # set mouse characteristics
  933. self.mapwin.mouse['box'] = 'point'
  934. self.mapwin.mouse["use"] == "pointer"
  935. self.mapwin.zoomtype = 0
  936. self.mapwin.pen = wx.Pen(colour='black', width=2, style=wx.SOLID)
  937. self.mapwin.SetNamedCursor('cross')
  938. #
  939. # show new display & draw map
  940. #
  941. if self.show_target:
  942. self.MapWindow = self.TgtMapWindow
  943. self.Map = self.TgtMap
  944. self.OnZoomToMap(None)
  945. self.MapWindow = self.SrcMapWindow
  946. self.Map = self.SrcMap
  947. self.OnZoomToMap(None)
  948. #
  949. # bindings
  950. #
  951. self.Bind(wx.EVT_ACTIVATE, self.OnFocus)
  952. self.Bind(wx.EVT_SIZE, self.OnSize)
  953. self.Bind(wx.EVT_IDLE, self.OnIdle)
  954. self.Bind(wx.EVT_CLOSE, self.OnQuit)
  955. self.SetSettings()
  956. def __del__(self):
  957. """Disable GCP manager mode"""
  958. # leaving the method here but was used only to delete gcpmanagement
  959. # from layer manager which is now not needed
  960. pass
  961. def CreateGCPList(self):
  962. """Create GCP List Control"""
  963. return GCPList(parent=self, gcp=self)
  964. # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
  965. def GetListCtrl(self):
  966. return self.list
  967. def GetMapCoordList(self):
  968. return self.mapcoordlist
  969. # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
  970. def GetSortImages(self):
  971. return (self.sm_dn, self.sm_up)
  972. def GetFwdError(self):
  973. return self.fwd_rmserror
  974. def GetBkwError(self):
  975. return self.bkw_rmserror
  976. def InitMapDisplay(self):
  977. self.list.LoadData()
  978. # initialize column sorter
  979. self.itemDataMap = self.mapcoordlist
  980. ncols = self.list.GetColumnCount()
  981. ColumnSorterMixin.__init__(self, ncols)
  982. # init to ascending sort on first click
  983. self._colSortFlag = [1] * ncols
  984. def SetTarget(self, tgroup, tlocation, tmapset):
  985. """
  986. Sets rectification target to current location and mapset
  987. """
  988. # check to see if we are georectifying map in current working
  989. # location/mapset
  990. if self.newlocation == self.currentlocation and self.newmapset == self.currentmapset:
  991. RunCommand('i.target',
  992. parent=self,
  993. flags='c',
  994. group=tgroup)
  995. else:
  996. self.grwiz.SwitchEnv('source')
  997. RunCommand('i.target',
  998. parent=self,
  999. group=tgroup,
  1000. location=tlocation,
  1001. mapset=tmapset)
  1002. self.grwiz.SwitchEnv('target')
  1003. def AddGCP(self, event):
  1004. """
  1005. Appends an item to GCP list
  1006. """
  1007. keyval = self.list.AddGCPItem() + 1
  1008. # source east, source north, target east, target north, forward error,
  1009. # backward error
  1010. self.mapcoordlist.append([keyval, # GCP number
  1011. 0.0, # source east
  1012. 0.0, # source north
  1013. 0.0, # source height
  1014. 0.0, # target east
  1015. 0.0, # target north
  1016. 0.0, # target height
  1017. 0.0, # forward error
  1018. 0.0]) # backward error
  1019. if self.statusbarManager.GetMode() == 8: # go to
  1020. self.StatusbarUpdate()
  1021. def DeleteGCP(self, event):
  1022. """
  1023. Deletes selected item in GCP list
  1024. """
  1025. minNumOfItems = self.OnGROrder(None)
  1026. if self.list.GetItemCount() <= minNumOfItems:
  1027. GMessage(
  1028. parent=self,
  1029. message=_("At least %d GCPs required. Operation canceled.") %
  1030. minNumOfItems)
  1031. return
  1032. key = self.list.DeleteGCPItem()
  1033. del self.mapcoordlist[key]
  1034. # update key and GCP number
  1035. for newkey in range(key, len(self.mapcoordlist)):
  1036. index = self.list.FindItem(-1, newkey + 1)
  1037. self.mapcoordlist[newkey][0] = newkey
  1038. self.list.SetItem(index, 0, str(newkey))
  1039. self.list.SetItemData(index, newkey)
  1040. # update selected
  1041. if self.list.GetItemCount() > 0:
  1042. if self.list.selected < self.list.GetItemCount():
  1043. self.list.selectedkey = self.list.GetItemData(
  1044. self.list.selected)
  1045. else:
  1046. self.list.selected = self.list.GetItemCount() - 1
  1047. self.list.selectedkey = self.list.GetItemData(
  1048. self.list.selected)
  1049. self.list.SetItemState(self.list.selected,
  1050. wx.LIST_STATE_SELECTED,
  1051. wx.LIST_STATE_SELECTED)
  1052. else:
  1053. self.list.selected = wx.NOT_FOUND
  1054. self.list.selectedkey = -1
  1055. self.UpdateColours()
  1056. if self.statusbarManager.GetMode() == 8: # go to
  1057. self.StatusbarUpdate()
  1058. if self.list.selectedkey > 0:
  1059. self.statusbarManager.SetProperty(
  1060. 'gotoGCP', self.list.selectedkey)
  1061. def ClearGCP(self, event):
  1062. """
  1063. Clears all values in selected item of GCP list and unchecks it
  1064. """
  1065. index = self.list.GetSelected()
  1066. key = self.list.GetItemData(index)
  1067. for i in range(1, 7):
  1068. self.list.SetItem(index, i, '0.0')
  1069. self.list.SetItem(index, 7, '')
  1070. self.list.SetItem(index, 8, '')
  1071. self.list.CheckItem(index, False)
  1072. # GCP number, source E, source N, target E, target N, fwd error, bkwd
  1073. # error
  1074. self.mapcoordlist[key] = [key, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  1075. def SetSettings(self):
  1076. """Sets settings for drawing of GCP's.
  1077. """
  1078. self.highest_only = UserSettings.Get(
  1079. group='gcpman', key='rms', subkey='highestonly')
  1080. self.show_unused = UserSettings.Get(
  1081. group='gcpman', key='symbol', subkey='unused')
  1082. colours = {"color": "default",
  1083. "hcolor": "highest",
  1084. "scolor": "selected",
  1085. "ucolor": "unused"}
  1086. wpx = UserSettings.Get(group='gcpman', key='symbol', subkey='width')
  1087. for k, v in six.iteritems(colours):
  1088. col = UserSettings.Get(group='gcpman', key='symbol', subkey=k)
  1089. self.pointsToDrawSrc.GetPen(v).SetColour(wx.Colour(
  1090. col[0], col[1], col[2], 255)) # TODO GetPen neni to spatne?
  1091. self.pointsToDrawTgt.GetPen(v).SetColour(
  1092. wx.Colour(col[0], col[1], col[2], 255))
  1093. self.pointsToDrawSrc.GetPen(v).SetWidth(wpx)
  1094. self.pointsToDrawTgt.GetPen(v).SetWidth(wpx)
  1095. spx = UserSettings.Get(group='gcpman', key='symbol', subkey='size')
  1096. self.pointsToDrawSrc.SetPropertyVal("size", int(spx))
  1097. self.pointsToDrawTgt.SetPropertyVal("size", int(spx))
  1098. font = self.GetFont()
  1099. font.SetPointSize(int(spx) + 2)
  1100. textProp = {}
  1101. textProp['active'] = True
  1102. textProp['font'] = font
  1103. self.pointsToDrawSrc.SetPropertyVal("text", textProp)
  1104. self.pointsToDrawTgt.SetPropertyVal("text", copy(textProp))
  1105. def SetGCPSatus(self, item, itemIndex):
  1106. """Before GCP is drawn, decides it's colour and whether it
  1107. will be drawed.
  1108. """
  1109. key = self.list.GetItemData(itemIndex)
  1110. # incremented because of itemDataMap (has one more item) - will be
  1111. # changed
  1112. itemIndex += 1
  1113. if not self.list.IsItemChecked(key - 1):
  1114. wxPen = "unused"
  1115. if not self.show_unused:
  1116. item.SetPropertyVal('hide', True)
  1117. else:
  1118. item.SetPropertyVal('hide', False)
  1119. else:
  1120. item.SetPropertyVal('hide', False)
  1121. if self.highest_only:
  1122. if itemIndex == self.highest_key:
  1123. wxPen = "highest"
  1124. else:
  1125. wxPen = "default"
  1126. else:
  1127. if (self.mapcoordlist[key][7] > self.rmsthresh):
  1128. wxPen = "highest"
  1129. else:
  1130. wxPen = "default"
  1131. if itemIndex == self.list.selectedkey:
  1132. wxPen = "selected"
  1133. item.SetPropertyVal('label', str(itemIndex))
  1134. item.SetPropertyVal('penName', wxPen)
  1135. def SetGCPData(self, coordtype, coord, mapdisp=None, confirm=False):
  1136. """Inserts coordinates from file, mouse click on map, or
  1137. after editing into selected item of GCP list and checks it for
  1138. use.
  1139. """
  1140. index = self.list.GetSelected()
  1141. if index == wx.NOT_FOUND:
  1142. return
  1143. coord0 = coord[0]
  1144. coord1 = coord[1]
  1145. key = self.list.GetItemData(index)
  1146. if confirm:
  1147. if self.MapWindow == self.SrcMapWindow:
  1148. currloc = _("source")
  1149. else:
  1150. currloc = _("target")
  1151. ret = wx.MessageBox(
  1152. parent=self, caption=_("Set GCP coordinates"),
  1153. message=_(
  1154. 'Set %(coor)s coordinates for GCP No. %(key)s? \n\n'
  1155. 'East: %(coor0)s \n'
  1156. 'North: %(coor1)s') %
  1157. {'coor': currloc, 'key': str(key),
  1158. 'coor0': str(coord0),
  1159. 'coor1': str(coord1)},
  1160. style=wx.ICON_QUESTION | wx.YES_NO | wx.CENTRE)
  1161. # for wingrass
  1162. if os.name == 'nt':
  1163. self.MapWindow.SetFocus()
  1164. if ret == wx.NO:
  1165. return
  1166. if coordtype == 'source':
  1167. self.list.SetItem(index, 1, str(coord0))
  1168. self.list.SetItem(index, 2, str(coord1))
  1169. self.mapcoordlist[key][1] = coord[0]
  1170. self.mapcoordlist[key][2] = coord[1]
  1171. self.pointsToDrawSrc.GetItem(key - 1).SetCoords([coord0, coord1])
  1172. elif coordtype == 'target':
  1173. self.list.SetItem(index, 4, str(coord0))
  1174. self.list.SetItem(index, 5, str(coord1))
  1175. self.mapcoordlist[key][4] = coord[0]
  1176. self.mapcoordlist[key][5] = coord[1]
  1177. #ADD ELEVATION FROM MAP AS HEIGHT PARAMETER
  1178. if os.path.exists(self.file['elevation']):
  1179. #Parse the i.ortho.elev generated file
  1180. #Get all lines from file
  1181. lines = open(self.file['elevation']).read().splitlines()
  1182. #Remove empty spaces in lines
  1183. lines = [x.replace(' ','') for x in lines]
  1184. #Extract map@mapset
  1185. elevationmap=lines[0].split(':')[1]+'@'+lines[1].split(':')[1]
  1186. #Make sure the region is set to the elevation map
  1187. ret, msg = RunCommand('g.region',
  1188. parent=self,
  1189. getErrorMsg=True,
  1190. quiet=True,
  1191. raster=elevationmap,
  1192. flags=None)
  1193. #Get the elevation height from the map given by i.ortho.elev
  1194. from subprocess import PIPE
  1195. from grass.pygrass.modules import Module
  1196. rwhat = Module('r.what',
  1197. map=elevationmap,
  1198. coordinates=[coord[0],coord[1]],
  1199. stdout_=PIPE)
  1200. self.mapcoordlist[key][6] = rwhat.outputs.stdout.split('|')[3].rstrip('\n')
  1201. self.list.SetItem(index, 6, str(self.mapcoordlist[key][6]))
  1202. self.pointsToDrawTgt.GetItem(key - 1).SetCoords([coord0, coord1])
  1203. self.list.SetItem(index, 7, '0')
  1204. self.list.SetItem(index, 8, '0')
  1205. self.mapcoordlist[key][7] = 0.0
  1206. self.mapcoordlist[key][8] = 0.0
  1207. # self.list.ResizeColumns()
  1208. def SaveGCPs(self, event):
  1209. """Make a CONTROL_POINTS file or save GCP coordinates to existing
  1210. POINTS file
  1211. """
  1212. self.GCPcount = 0
  1213. try:
  1214. f = open(self.file['control_points'], mode='w')
  1215. # use os.linesep or '\n' here ???
  1216. f.write('# Ground Control Points File\n')
  1217. f.write("# \n")
  1218. f.write("# target location: " + self.currentlocation + '\n')
  1219. f.write("# target mapset: " + self.currentmapset + '\n')
  1220. f.write("#\tsource\t\t\ttarget\t\t\tstatus\n")
  1221. f.write("#\teast\tnorth\theight\teast\tnorth\theight\t(1=ok, 0=ignore)\n")
  1222. f.write(
  1223. "#---------------------------- --------------------------- ---------------\n")
  1224. for index in range(self.list.GetItemCount()):
  1225. if self.list.IsItemChecked(index):
  1226. check = "1"
  1227. self.GCPcount += 1
  1228. else:
  1229. check = "0"
  1230. coord0 = self.list.GetItem(index, 1).GetText()
  1231. coord1 = self.list.GetItem(index, 2).GetText()
  1232. coord2 = self.list.GetItem(index, 3).GetText()
  1233. coord3 = self.list.GetItem(index, 4).GetText()
  1234. coord4 = self.list.GetItem(index, 5).GetText()
  1235. coord5 = self.list.GetItem(index, 6).GetText()
  1236. f.write(
  1237. coord0 +
  1238. ' ' +
  1239. coord1 +
  1240. ' ' +
  1241. coord2 +
  1242. ' ' +
  1243. coord3 +
  1244. ' ' +
  1245. coord4 +
  1246. ' ' +
  1247. coord5 +
  1248. ' ' +
  1249. check +
  1250. '\n')
  1251. except IOError as err:
  1252. GError(
  1253. parent=self,
  1254. message="%s <%s>. %s%s" %
  1255. (_("Writing CONTROL_POINTS file failed"),
  1256. self.file['control_points'],
  1257. os.linesep,
  1258. err))
  1259. return
  1260. f.close()
  1261. # if event != None save also to backup file
  1262. if event:
  1263. shutil.copy(self.file['control_points'], self.file['control_points_bak'])
  1264. self._giface.WriteLog(
  1265. _('CONTROL_POINTS file saved for group <%s>') %
  1266. self.xygroup)
  1267. #self.SetStatusText(_('POINTS file saved'))
  1268. def ReadGCPs(self):
  1269. """
  1270. Reads GCPs and georectified coordinates from POINTS file
  1271. """
  1272. self.GCPcount = 0
  1273. sourceMapWin = self.SrcMapWindow
  1274. targetMapWin = self.TgtMapWindow
  1275. if not sourceMapWin:
  1276. GError(parent=self,
  1277. message=_("source mapwin not defined"))
  1278. if not targetMapWin:
  1279. GError(parent=self,
  1280. message=_("target mapwin not defined"))
  1281. try:
  1282. f = open(self.file['control_points'], 'r')
  1283. GCPcnt = 0
  1284. for line in f.readlines():
  1285. if line[0] == '#' or line == '':
  1286. continue
  1287. line = line.replace('\n', '').strip()
  1288. coords = list(map(float, line.split()))
  1289. if coords[6] == 1:
  1290. check = True
  1291. self.GCPcount += 1
  1292. else:
  1293. check = False
  1294. self.AddGCP(event=None)
  1295. self.SetGCPData('source', (coords[0], coords[1]), sourceMapWin)
  1296. self.SetGCPData('target', (coords[3], coords[4]), targetMapWin)
  1297. index = self.list.GetSelected()
  1298. if index != wx.NOT_FOUND:
  1299. self.list.CheckItem(index, check)
  1300. GCPcnt += 1
  1301. except IOError as err:
  1302. GError(
  1303. parent=self,
  1304. message="%s <%s>. %s%s" %
  1305. (_("Reading CONTROL_POINTS file failed"),
  1306. self.file['control_points'],
  1307. os.linesep,
  1308. err))
  1309. return
  1310. f.close()
  1311. if GCPcnt == 0:
  1312. # 3 gcp is minimum
  1313. for i in range(3):
  1314. self.AddGCP(None)
  1315. if self.CheckGCPcount():
  1316. # calculate RMS
  1317. self.RMSError(self.xygroup, self.gr_order)
  1318. def ReloadGCPs(self, event):
  1319. """Reload data from file"""
  1320. # use backup
  1321. shutil.copy(self.file['control_points_bak'], self.file['control_points'])
  1322. # delete all items in mapcoordlist
  1323. self.mapcoordlist = []
  1324. self.mapcoordlist.append([0, # GCP number
  1325. 0.0, # source east
  1326. 0.0, # source north
  1327. 0.0, # source height
  1328. 0.0, # target east
  1329. 0.0, # target north
  1330. 0.0, # target height
  1331. 0.0, # forward error
  1332. 0.0]) # backward error
  1333. self.list.LoadData()
  1334. self.itemDataMap = self.mapcoordlist
  1335. if self._col != -1:
  1336. self.list.ClearColumnImage(self._col)
  1337. self._colSortFlag = [1] * self.list.GetColumnCount()
  1338. # draw GCPs (source and target)
  1339. sourceMapWin = self.SrcMapWindow
  1340. sourceMapWin.UpdateMap(render=False, renderVector=False)
  1341. if self.show_target:
  1342. targetMapWin = self.TgtMapWindow
  1343. targetMapWin.UpdateMap(render=False, renderVector=False)
  1344. def OnFocus(self, event):
  1345. # TODO: it is here just to remove old or obsolate beavior of base class gcp/MapFrame?
  1346. # self.grwiz.SwitchEnv('source')
  1347. pass
  1348. def _onMouseLeftUpPointer(self, mapWindow, x, y):
  1349. if mapWindow == self.SrcMapWindow:
  1350. coordtype = 'source'
  1351. else:
  1352. coordtype = 'target'
  1353. coord = (x, y)
  1354. self.SetGCPData(coordtype, coord, self, confirm=True)
  1355. mapWindow.UpdateMap(render=False, renderVector=False)
  1356. def OnRMS(self, event):
  1357. """
  1358. RMS button handler
  1359. """
  1360. self.RMSError(self.xygroup, self.gr_order)
  1361. sourceMapWin = self.SrcMapWindow
  1362. sourceMapWin.UpdateMap(render=False, renderVector=False)
  1363. if self.show_target:
  1364. targetMapWin = self.TgtMapWindow
  1365. targetMapWin.UpdateMap(render=False, renderVector=False)
  1366. def CheckGCPcount(self, msg=False):
  1367. """
  1368. Checks to make sure that the minimum number of GCPs have been defined and
  1369. are active for the selected transformation order
  1370. """
  1371. if (self.GCPcount < 3 and self.gr_order == 1) or \
  1372. (self.GCPcount < 6 and self.gr_order == 2) or \
  1373. (self.GCPcount < 10 and self.gr_order == 3):
  1374. if msg:
  1375. GWarning(
  1376. parent=self, message=_(
  1377. 'Insufficient points defined and active (checked) '
  1378. 'for selected rectification method (order: %d).\n'
  1379. '3+ points needed for 1st order,\n'
  1380. '6+ points for 2nd order, and\n'
  1381. '10+ points for 3rd order.') %
  1382. self.gr_order)
  1383. return False
  1384. else:
  1385. return True
  1386. def OnGeorect(self, event):
  1387. """
  1388. Georectifies map(s) in group using i.ortho.rectify or v.rectify
  1389. """
  1390. global maptype
  1391. self.SaveGCPs(None)
  1392. if not self.CheckGCPcount(msg=True):
  1393. return
  1394. if maptype == 'raster':
  1395. self.grwiz.SwitchEnv('source')
  1396. if self.clip_to_region:
  1397. flags = "ac"
  1398. else:
  1399. flags = "a"
  1400. busy = wx.BusyInfo(_("Rectifying images, please wait..."),
  1401. parent=self)
  1402. wx.GetApp().Yield()
  1403. ret, msg = RunCommand('i.ortho.rectify',
  1404. parent=self,
  1405. getErrorMsg=True,
  1406. quiet=True,
  1407. group=self.xygroup,
  1408. extension=self.extension,
  1409. method=self.gr_method,
  1410. angle=self.grwiz.cam_angle,
  1411. flags=flags)
  1412. del busy
  1413. # provide feedback on failure
  1414. if ret != 0:
  1415. print(msg, file=sys.stderr)
  1416. elif maptype == 'vector':
  1417. # loop through all vectors in VREF
  1418. self.grwiz.SwitchEnv('source')
  1419. # make list of vectors to georectify from VREF
  1420. f = open(self.file['vgrp'])
  1421. vectlist = []
  1422. try:
  1423. for vect in f.readlines():
  1424. vect = vect.strip('\n')
  1425. if len(vect) < 1:
  1426. continue
  1427. vectlist.append(vect)
  1428. finally:
  1429. f.close()
  1430. # georectify each vector in VREF using v.rectify
  1431. for vect in vectlist:
  1432. self.outname = str(vect.split('@')[0]) + self.extension
  1433. self._giface.WriteLog(text=_('Transforming <%s>...') % vect,
  1434. notification=Notification.MAKE_VISIBLE)
  1435. ret = msg = ''
  1436. busy = wx.BusyInfo(
  1437. _("Rectifying vector map <%s>, please wait...") %
  1438. vect, parent=self)
  1439. wx.GetApp().Yield()
  1440. ret, msg = RunCommand('v.rectify',
  1441. parent=self,
  1442. getErrorMsg=True,
  1443. quiet=True,
  1444. input=vect,
  1445. output=self.outname,
  1446. group=self.xygroup,
  1447. order=self.gr_order)
  1448. del busy
  1449. # provide feedback on failure
  1450. if ret != 0:
  1451. print(msg, file=sys.stderr)
  1452. self.grwiz.SwitchEnv('target')
  1453. def OnGeorectDone(self, **kargs):
  1454. """Print final message"""
  1455. global maptype
  1456. if maptype == 'raster':
  1457. return
  1458. returncode = kargs['returncode']
  1459. if returncode == 0:
  1460. self.VectGRList.append(self.outname)
  1461. print('*****vector list = ' + str(self.VectGRList),
  1462. file=sys.stderr)
  1463. else:
  1464. self._giface.WriteError(
  1465. _('Georectification of vector map <%s> failed') %
  1466. self.outname)
  1467. def OnSettings(self, event):
  1468. """GCP Manager settings"""
  1469. dlg = GrSettingsDialog(parent=self, giface=self._giface,
  1470. id=wx.ID_ANY, title=_('GCP Manager settings'))
  1471. if dlg.ShowModal() == wx.ID_OK:
  1472. pass
  1473. dlg.Destroy()
  1474. def UpdateColours(self, srcrender=False, srcrenderVector=False,
  1475. tgtrender=False, tgtrenderVector=False):
  1476. """update colours"""
  1477. highest_fwd_err = 0.0
  1478. self.highest_key = 0
  1479. highest_idx = 0
  1480. for index in range(self.list.GetItemCount()):
  1481. if self.list.IsItemChecked(index):
  1482. key = self.list.GetItemData(index)
  1483. fwd_err = self.mapcoordlist[key][5]
  1484. if self.highest_only:
  1485. self.list.SetItemTextColour(index, wx.BLACK)
  1486. if highest_fwd_err < fwd_err:
  1487. highest_fwd_err = fwd_err
  1488. self.highest_key = key
  1489. highest_idx = index
  1490. elif self.rmsthresh > 0:
  1491. if (fwd_err > self.rmsthresh):
  1492. self.list.SetItemTextColour(index, wx.RED)
  1493. else:
  1494. self.list.SetItemTextColour(index, wx.BLACK)
  1495. else:
  1496. self.list.SetItemTextColour(index, wx.BLACK)
  1497. if self.highest_only and highest_fwd_err > 0.0:
  1498. self.list.SetItemTextColour(highest_idx, wx.RED)
  1499. sourceMapWin = self.SrcMapWindow
  1500. sourceMapWin.UpdateMap(render=srcrender, renderVector=srcrenderVector)
  1501. if self.show_target:
  1502. targetMapWin = self.TgtMapWindow
  1503. targetMapWin.UpdateMap(
  1504. render=tgtrender,
  1505. renderVector=tgtrenderVector)
  1506. def OnQuit(self, event):
  1507. """Quit georectifier"""
  1508. ret = wx.MessageBox(
  1509. parent=self, caption=_("Quit GCP Manager"),
  1510. message=_('Save ground control points?'),
  1511. style=wx.ICON_QUESTION | wx.YES_NO | wx.CANCEL | wx.CENTRE)
  1512. if ret != wx.CANCEL:
  1513. if ret == wx.YES:
  1514. self.SaveGCPs(None)
  1515. elif ret == wx.NO:
  1516. # restore POINTS file from backup
  1517. if os.path.exists(self.file['control_points_bak']):
  1518. shutil.copy(self.file['control_points_bak'], self.file['control_points'])
  1519. if os.path.exists(self.file['control_points_bak']):
  1520. os.unlink(self.file['control_points_bak'])
  1521. self.SrcMap.Clean()
  1522. self.TgtMap.Clean()
  1523. self.grwiz.Cleanup()
  1524. self._mgr.UnInit()
  1525. self.Destroy()
  1526. # event.Skip()
  1527. def OnGROrder(self, event):
  1528. """
  1529. sets transformation order for georectifying
  1530. """
  1531. if event:
  1532. self.gr_order = event.GetInt() + 1
  1533. numOfItems = self.list.GetItemCount()
  1534. minNumOfItems = numOfItems
  1535. if self.gr_order == 1:
  1536. minNumOfItems = 3
  1537. # self.SetStatusText(_('Insufficient points, 3+ points needed for 1st order'))
  1538. elif self.gr_order == 2:
  1539. minNumOfItems = 6
  1540. diff = 6 - numOfItems
  1541. # self.SetStatusText(_('Insufficient points, 6+ points needed for 2nd order'))
  1542. elif self.gr_order == 3:
  1543. minNumOfItems = 10
  1544. # self.SetStatusText(_('Insufficient points, 10+ points needed for 3rd order'))
  1545. for i in range(minNumOfItems - numOfItems):
  1546. self.AddGCP(None)
  1547. return minNumOfItems
  1548. def RMSError(self, xygroup, order):
  1549. """
  1550. Uses i.ortho.transform to calculate forward and backward error for each used GCP
  1551. in CONTROL_POINTS file and insert error values into GCP list.
  1552. Calculates total forward and backward RMS error for all used points
  1553. """
  1554. # save GCPs to points file to make sure that all checked GCPs are used
  1555. self.SaveGCPs(None)
  1556. # self.SetStatusText('')
  1557. if not self.CheckGCPcount(msg=True):
  1558. return
  1559. # get list of forward and reverse rms error values for each point
  1560. self.grwiz.SwitchEnv('source')
  1561. ret = RunCommand('i.ortho.transform',
  1562. parent=self,
  1563. read=True,
  1564. group=xygroup)
  1565. self.grwiz.SwitchEnv('target')
  1566. if ret:
  1567. errlist = ret.splitlines()
  1568. else:
  1569. GError(parent=self,
  1570. message=_('Could not calculate RMS Error.\n'
  1571. 'Possible error with i.ortho.transform.'))
  1572. return
  1573. # insert error values into GCP list for checked items
  1574. sdfactor = float(
  1575. UserSettings.Get(
  1576. group='gcpman',
  1577. key='rms',
  1578. subkey='sdfactor'))
  1579. GCPcount = 0
  1580. sumsq_fwd_err = 0.0
  1581. sumsq_bkw_err = 0.0
  1582. sum_fwd_err = 0.0
  1583. highest_fwd_err = 0.0
  1584. self.highest_key = 0
  1585. highest_idx = 0
  1586. for index in range(self.list.GetItemCount()):
  1587. key = self.list.GetItemData(index)
  1588. if self.list.IsItemChecked(index):
  1589. fwd_err, bkw_err = errlist[GCPcount].split()
  1590. self.list.SetItem(index, 7, fwd_err)
  1591. self.list.SetItem(index, 8, bkw_err)
  1592. self.mapcoordlist[key][7] = float(fwd_err)
  1593. self.mapcoordlist[key][8] = float(bkw_err)
  1594. self.list.SetItemTextColour(index, wx.BLACK)
  1595. if self.highest_only:
  1596. if highest_fwd_err < float(fwd_err):
  1597. highest_fwd_err = float(fwd_err)
  1598. self.highest_key = key
  1599. highest_idx = index
  1600. sumsq_fwd_err += float(fwd_err)**2
  1601. sumsq_bkw_err += float(bkw_err)**2
  1602. sum_fwd_err += float(fwd_err)
  1603. GCPcount += 1
  1604. else:
  1605. self.list.SetItem(index, 7, '')
  1606. self.list.SetItem(index, 8, '')
  1607. self.mapcoordlist[key][7] = 0.0
  1608. self.mapcoordlist[key][8] = 0.0
  1609. self.list.SetItemTextColour(index, wx.BLACK)
  1610. # SD
  1611. if GCPcount > 0:
  1612. self.rmsmean = sum_fwd_err / GCPcount
  1613. self.rmssd = ((sumsq_fwd_err - self.rmsmean**2)**0.5)
  1614. self.rmsthresh = self.rmsmean + sdfactor * self.rmssd
  1615. else:
  1616. self.rmsthresh = 0
  1617. self.rmsmean = 0
  1618. self.rmssd = 0
  1619. if self.highest_only and highest_fwd_err > 0.0:
  1620. self.list.SetItemTextColour(highest_idx, wx.RED)
  1621. elif GCPcount > 0 and self.rmsthresh > 0 and not self.highest_only:
  1622. for index in range(self.list.GetItemCount()):
  1623. if self.list.IsItemChecked(index):
  1624. key = self.list.GetItemData(index)
  1625. if (self.mapcoordlist[key][7] > self.rmsthresh):
  1626. self.list.SetItemTextColour(index, wx.RED)
  1627. # calculate global RMS error (geometric mean)
  1628. self.fwd_rmserror = round((sumsq_fwd_err / GCPcount)**0.5, 4)
  1629. self.bkw_rmserror = round((sumsq_bkw_err / GCPcount)**0.5, 4)
  1630. self.list.ResizeColumns()
  1631. def GetNewExtent(self, region, map=None):
  1632. coord_file = utils.GetTempfile()
  1633. newreg = {'n': 0.0, 's': 0.0, 'e': 0.0, 'w': 0.0, }
  1634. try:
  1635. f = open(coord_file, mode='w')
  1636. # NW corner
  1637. f.write(str(region['e']) + " " + str(region['n']) + "\n")
  1638. # NE corner
  1639. f.write(str(region['e']) + " " + str(region['s']) + "\n")
  1640. # SW corner
  1641. f.write(str(region['w']) + " " + str(region['n']) + "\n")
  1642. # SE corner
  1643. f.write(str(region['w']) + " " + str(region['s']) + "\n")
  1644. finally:
  1645. f.close()
  1646. # save GCPs to points file to make sure that all checked GCPs are used
  1647. self.SaveGCPs(None)
  1648. order = self.gr_order
  1649. self.gr_order = 1
  1650. if not self.CheckGCPcount(msg=True):
  1651. self.gr_order = order
  1652. return
  1653. self.gr_order = order
  1654. # get list of forward and reverse rms error values for each point
  1655. self.grwiz.SwitchEnv('source')
  1656. if map == 'source':
  1657. ret = RunCommand('i.ortho.transform',
  1658. parent=self,
  1659. read=True,
  1660. group=self.xygroup,
  1661. format='dst',
  1662. coords=coord_file)
  1663. elif map == 'target':
  1664. ret = RunCommand('i.ortho.transform',
  1665. parent=self,
  1666. read=True,
  1667. group=self.xygroup,
  1668. flags='r',
  1669. format='src',
  1670. coords=coord_file)
  1671. os.unlink(coord_file)
  1672. self.grwiz.SwitchEnv('target')
  1673. if ret:
  1674. errlist = ret.splitlines()
  1675. else:
  1676. GError(parent=self,
  1677. message=_('Could not calculate new extends.\n'
  1678. 'Possible error with i.ortho.transform.'))
  1679. return
  1680. # fist corner
  1681. e, n = errlist[0].split()
  1682. fe = float(e)
  1683. fn = float(n)
  1684. newreg['n'] = fn
  1685. newreg['s'] = fn
  1686. newreg['e'] = fe
  1687. newreg['w'] = fe
  1688. # other three corners
  1689. for i in range(1, 4):
  1690. e, n = errlist[i].split()
  1691. fe = float(e)
  1692. fn = float(n)
  1693. if fe < newreg['w']:
  1694. newreg['w'] = fe
  1695. if fe > newreg['e']:
  1696. newreg['e'] = fe
  1697. if fn < newreg['s']:
  1698. newreg['s'] = fn
  1699. if fn > newreg['n']:
  1700. newreg['n'] = fn
  1701. return newreg
  1702. def OnHelp(self, event):
  1703. """Show GCP Manager manual page"""
  1704. self._giface.Help(entry='wxGUI.gcp')
  1705. def OnUpdateActive(self, event):
  1706. if self.activemap.GetSelection() == 0:
  1707. self.MapWindow = self.SrcMapWindow
  1708. self.Map = self.SrcMap
  1709. else:
  1710. self.MapWindow = self.TgtMapWindow
  1711. self.Map = self.TgtMap
  1712. self.UpdateActive(self.MapWindow)
  1713. # for wingrass
  1714. if os.name == 'nt':
  1715. self.MapWindow.SetFocus()
  1716. def UpdateActive(self, win):
  1717. # optionally disable tool zoomback tool
  1718. self.GetMapToolbar().Enable('zoomback',
  1719. enable=(len(self.MapWindow.zoomhistory) > 1))
  1720. if self.activemap.GetSelection() != (win == self.TgtMapWindow):
  1721. self.activemap.SetSelection(win == self.TgtMapWindow)
  1722. self.StatusbarUpdate()
  1723. def AdjustMap(self, newreg):
  1724. """Adjust map window to new extents
  1725. """
  1726. # adjust map window
  1727. self.Map.region['n'] = newreg['n']
  1728. self.Map.region['s'] = newreg['s']
  1729. self.Map.region['e'] = newreg['e']
  1730. self.Map.region['w'] = newreg['w']
  1731. self.MapWindow.ZoomHistory(self.Map.region['n'], self.Map.region['s'],
  1732. self.Map.region['e'], self.Map.region['w'])
  1733. # LL locations
  1734. if self.Map.projinfo['proj'] == 'll':
  1735. if newreg['n'] > 90.0:
  1736. newreg['n'] = 90.0
  1737. if newreg['s'] < -90.0:
  1738. newreg['s'] = -90.0
  1739. ce = newreg['w'] + (newreg['e'] - newreg['w']) / 2
  1740. cn = newreg['s'] + (newreg['n'] - newreg['s']) / 2
  1741. # calculate new center point and display resolution
  1742. self.Map.region['center_easting'] = ce
  1743. self.Map.region['center_northing'] = cn
  1744. self.Map.region["ewres"] = (newreg['e'] - newreg['w']) / self.Map.width
  1745. self.Map.region["nsres"] = (
  1746. newreg['n'] - newreg['s']) / self.Map.height
  1747. self.Map.AlignExtentFromDisplay()
  1748. self.MapWindow.ZoomHistory(self.Map.region['n'], self.Map.region['s'],
  1749. self.Map.region['e'], self.Map.region['w'])
  1750. if self.MapWindow.redrawAll is False:
  1751. self.MapWindow.redrawAll = True
  1752. self.MapWindow.UpdateMap()
  1753. self.StatusbarUpdate()
  1754. def OnZoomToSource(self, event):
  1755. """Set target map window to match extents of source map window
  1756. """
  1757. if not self.MapWindow == self.TgtMapWindow:
  1758. self.MapWindow = self.TgtMapWindow
  1759. self.Map = self.TgtMap
  1760. self.UpdateActive(self.TgtMapWindow)
  1761. # get new N, S, E, W for target
  1762. newreg = self.GetNewExtent(self.SrcMap.region, 'source')
  1763. if newreg:
  1764. self.AdjustMap(newreg)
  1765. def OnZoomToTarget(self, event):
  1766. """Set source map window to match extents of target map window
  1767. """
  1768. if not self.MapWindow == self.SrcMapWindow:
  1769. self.MapWindow = self.SrcMapWindow
  1770. self.Map = self.SrcMap
  1771. self.UpdateActive(self.SrcMapWindow)
  1772. # get new N, S, E, W for target
  1773. newreg = self.GetNewExtent(self.TgtMap.region, 'target')
  1774. if newreg:
  1775. self.AdjustMap(newreg)
  1776. def OnZoomMenuGCP(self, event):
  1777. """Popup Zoom menu
  1778. """
  1779. point = wx.GetMousePosition()
  1780. zoommenu = Menu()
  1781. # Add items to the menu
  1782. zoomsource = wx.MenuItem(zoommenu, wx.ID_ANY, _(
  1783. 'Adjust source display to target display'))
  1784. zoommenu.AppendItem(zoomsource)
  1785. self.Bind(wx.EVT_MENU, self.OnZoomToTarget, zoomsource)
  1786. zoomtarget = wx.MenuItem(zoommenu, wx.ID_ANY, _(
  1787. 'Adjust target display to source display'))
  1788. zoommenu.AppendItem(zoomtarget)
  1789. self.Bind(wx.EVT_MENU, self.OnZoomToSource, zoomtarget)
  1790. # Popup the menu. If an item is selected then its handler
  1791. # will be called before PopupMenu returns.
  1792. self.PopupMenu(zoommenu)
  1793. zoommenu.Destroy()
  1794. def OnSize(self, event):
  1795. """Adjust Map Windows after GCP Map Display has been resized
  1796. """
  1797. # re-render image on idle
  1798. self.resize = grass.clock()
  1799. super(MapFrame, self).OnSize(event)
  1800. def OnIdle(self, event):
  1801. """GCP Map Display resized, adjust Map Windows
  1802. """
  1803. if self.GetMapToolbar():
  1804. if self.resize and self.resize + 0.2 < grass.clock():
  1805. srcwidth, srcheight = self.SrcMapWindow.GetSize()
  1806. tgtwidth, tgtheight = self.TgtMapWindow.GetSize()
  1807. srcwidth = (srcwidth + tgtwidth) / 2
  1808. if self.show_target:
  1809. self._mgr.GetPane("target").Hide()
  1810. self._mgr.Update()
  1811. self._mgr.GetPane("source").BestSize((srcwidth, srcheight))
  1812. self._mgr.GetPane("target").BestSize((srcwidth, tgtheight))
  1813. if self.show_target:
  1814. self._mgr.GetPane("target").Show()
  1815. self._mgr.Update()
  1816. self.resize = False
  1817. elif self.resize:
  1818. event.RequestMore()
  1819. pass
  1820. class GCPList(ListCtrl,
  1821. CheckListCtrlMixin,
  1822. ListCtrlAutoWidthMixin):
  1823. def __init__(self, parent, gcp, id=wx.ID_ANY,
  1824. pos=wx.DefaultPosition, size=wx.DefaultSize,
  1825. style=wx.LC_REPORT | wx.SUNKEN_BORDER | wx.LC_HRULES |
  1826. wx.LC_SINGLE_SEL):
  1827. ListCtrl.__init__(self, parent, id, pos, size, style)
  1828. self.gcp = gcp # GCP class
  1829. self.render = True
  1830. # Mixin settings
  1831. CheckListCtrlMixin.__init__(self)
  1832. ListCtrlAutoWidthMixin.__init__(self)
  1833. # TextEditMixin.__init__(self)
  1834. # tracks whether list items are checked or not
  1835. self.CheckList = []
  1836. self._Create()
  1837. self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
  1838. self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated)
  1839. self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick)
  1840. self.selected = wx.NOT_FOUND
  1841. self.selectedkey = -1
  1842. def _Create(self):
  1843. if 0:
  1844. # normal, simple columns
  1845. idx_col = 0
  1846. for col in (_('use'),
  1847. _('source E'),
  1848. _('source N'),
  1849. _('source Z'),
  1850. _('target E'),
  1851. _('target N'),
  1852. _('target Z'),
  1853. _('Forward error'),
  1854. _('Backward error')):
  1855. self.InsertColumn(idx_col, col)
  1856. idx_col += 1
  1857. else:
  1858. # the hard way: we want images on the column header
  1859. info = wx.ListItem()
  1860. info.SetMask(
  1861. wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT)
  1862. info.SetImage(-1)
  1863. info.m_format = wx.LIST_FORMAT_LEFT
  1864. idx_col = 0
  1865. for lbl in (_('use'),
  1866. _('source E'),
  1867. _('source N'),
  1868. _('source Z'),
  1869. _('target E'),
  1870. _('target N'),
  1871. _('target Z'),
  1872. _('Forward error'),
  1873. _('Backward error')):
  1874. info.SetText(lbl)
  1875. self.InsertColumn(idx_col, info)
  1876. idx_col += 1
  1877. def LoadData(self):
  1878. """Load data into list"""
  1879. self.DeleteAllItems()
  1880. self.render = False
  1881. if os.path.isfile(self.gcp.file['control_points']):
  1882. self.gcp.ReadGCPs()
  1883. else:
  1884. # 3 gcp is minimum
  1885. for i in range(3):
  1886. self.gcp.AddGCP(None)
  1887. # select first point by default
  1888. self.selected = 0
  1889. self.selectedkey = self.GetItemData(self.selected)
  1890. self.SetItemState(self.selected,
  1891. wx.LIST_STATE_SELECTED,
  1892. wx.LIST_STATE_SELECTED)
  1893. self.ResizeColumns()
  1894. self.render = True
  1895. self.EnsureVisible(self.selected)
  1896. def OnCheckItem(self, index, flag):
  1897. """Item is checked/unchecked"""
  1898. if self.render:
  1899. # redraw points
  1900. sourceMapWin = self.gcp.SrcMapWindow
  1901. sourceMapWin.UpdateMap(render=False, renderVector=False)
  1902. if self.gcp.show_target:
  1903. targetMapWin = self.gcp.TgtMapWindow
  1904. targetMapWin.UpdateMap(render=False, renderVector=False)
  1905. def AddGCPItem(self):
  1906. """
  1907. Appends an item to GCP list
  1908. """
  1909. self.selectedkey = self.GetItemCount() + 1
  1910. self.Append([str(self.selectedkey), # GCP number
  1911. '0.0', # source E
  1912. '0.0', # source N
  1913. '0.0', # source Z
  1914. '0.0', # target E
  1915. '0.0', # target N
  1916. '0.0', # target Z
  1917. '', # forward error
  1918. '']) # backward error
  1919. self.selected = self.GetItemCount() - 1
  1920. self.SetItemData(self.selected, self.selectedkey)
  1921. self.SetItemState(self.selected,
  1922. wx.LIST_STATE_SELECTED,
  1923. wx.LIST_STATE_SELECTED)
  1924. self.ResizeColumns()
  1925. self.gcp.pointsToDrawSrc.AddItem(
  1926. coords=[0, 0], label=str(self.selectedkey))
  1927. self.gcp.pointsToDrawTgt.AddItem(
  1928. coords=[0, 0], label=str(self.selectedkey))
  1929. self.EnsureVisible(self.selected)
  1930. return self.selected
  1931. def DeleteGCPItem(self):
  1932. """Deletes selected item in GCP list.
  1933. """
  1934. if self.selected == wx.NOT_FOUND:
  1935. return
  1936. key = self.GetItemData(self.selected)
  1937. self.DeleteItem(self.selected)
  1938. if self.selected != wx.NOT_FOUND:
  1939. item = self.gcp.pointsToDrawSrc.GetItem(key - 1)
  1940. self.gcp.pointsToDrawSrc.DeleteItem(item)
  1941. item = self.gcp.pointsToDrawTgt.GetItem(key - 1)
  1942. self.gcp.pointsToDrawTgt.DeleteItem(item)
  1943. return key
  1944. def ResizeColumns(self):
  1945. """Resize columns"""
  1946. minWidth = [90, 120]
  1947. for i in range(self.GetColumnCount()):
  1948. self.SetColumnWidth(i, wx.LIST_AUTOSIZE)
  1949. # first column is checkbox, don't set to minWidth
  1950. if i > 0 and self.GetColumnWidth(i) < minWidth[i > 4]:
  1951. self.SetColumnWidth(i, minWidth[i > 4])
  1952. self.SendSizeEvent()
  1953. def GetSelected(self):
  1954. """Get index of selected item"""
  1955. return self.selected
  1956. def OnItemSelected(self, event):
  1957. """Item selected
  1958. """
  1959. if self.render and self.selected != event.GetIndex():
  1960. self.selected = event.GetIndex()
  1961. self.selectedkey = self.GetItemData(self.selected)
  1962. sourceMapWin = self.gcp.SrcMapWindow
  1963. sourceMapWin.UpdateMap(render=False, renderVector=False)
  1964. if self.gcp.show_target:
  1965. targetMapWin = self.gcp.TgtMapWindow
  1966. targetMapWin.UpdateMap(render=False, renderVector=False)
  1967. event.Skip()
  1968. def OnItemActivated(self, event):
  1969. """
  1970. When item double clicked, open editor to update coordinate values
  1971. """
  1972. coords = []
  1973. index = event.GetIndex()
  1974. key = self.GetItemData(index)
  1975. changed = False
  1976. for i in range(1, 7):
  1977. coords.append(self.GetItem(index, i).GetText())
  1978. dlg = EditGCP(parent=self, id=wx.ID_ANY, data=coords, gcpno=key)
  1979. if dlg.ShowModal() == wx.ID_OK:
  1980. values = dlg.GetValues() # string
  1981. if len(values) == 0:
  1982. GError(parent=self, message=_(
  1983. "Invalid coordinate value. Operation canceled."))
  1984. else:
  1985. for i in range(len(values)):
  1986. if values[i] != coords[i]:
  1987. self.SetItem(index, i + 1, values[i])
  1988. changed = True
  1989. if changed:
  1990. # reset RMS and update mapcoordlist
  1991. self.SetItem(index, 7, '')
  1992. self.SetItem(index, 8, '')
  1993. key = self.GetItemData(index)
  1994. self.gcp.mapcoordlist[key] = [key,
  1995. float(values[0]),
  1996. float(values[1]),
  1997. float(values[2]),
  1998. float(values[3]),
  1999. float(values[4]),
  2000. float(values[5]),
  2001. 0.0,
  2002. 0.0]
  2003. self.gcp.pointsToDrawSrc.GetItem(
  2004. key - 1).SetCoords([float(values[0]), float(values[1])])
  2005. self.gcp.pointsToDrawTgt.GetItem(
  2006. key - 1).SetCoords([float(values[3]), float(values[4])])
  2007. self.gcp.UpdateColours()
  2008. def OnColClick(self, event):
  2009. """ListCtrl forgets selected item..."""
  2010. self.selected = self.FindItem(-1, self.selectedkey)
  2011. self.SetItemState(self.selected,
  2012. wx.LIST_STATE_SELECTED,
  2013. wx.LIST_STATE_SELECTED)
  2014. event.Skip()
  2015. class VectGroup(wx.Dialog):
  2016. """Dialog to create a vector group (VREF file) for georectifying
  2017. .. todo::
  2018. Replace by g.group
  2019. """
  2020. def __init__(self, parent, id, grassdb, location, mapset, group,
  2021. style=wx.DEFAULT_DIALOG_STYLE):
  2022. wx.Dialog.__init__(self, parent, id, style=style,
  2023. title=_("Create vector map group"))
  2024. self.grassdatabase = grassdb
  2025. self.xylocation = location
  2026. self.xymapset = mapset
  2027. self.xygroup = group
  2028. #
  2029. # get list of valid vector directories
  2030. #
  2031. vectlist = os.listdir(os.path.join(self.grassdatabase,
  2032. self.xylocation,
  2033. self.xymapset,
  2034. 'vector'))
  2035. for dir in vectlist:
  2036. if not os.path.isfile(os.path.join(self.grassdatabase,
  2037. self.xylocation,
  2038. self.xymapset,
  2039. 'vector',
  2040. dir,
  2041. 'coor')):
  2042. vectlist.remove(dir)
  2043. utils.ListSortLower(vectlist)
  2044. # path to vref file
  2045. self.vgrpfile = os.path.join(self.grassdatabase,
  2046. self.xylocation,
  2047. self.xymapset,
  2048. 'group',
  2049. self.xygroup,
  2050. 'VREF')
  2051. #
  2052. # buttons
  2053. #
  2054. self.btnCancel = Button(parent=self,
  2055. id=wx.ID_CANCEL)
  2056. self.btnOK = Button(parent=self,
  2057. id=wx.ID_OK)
  2058. self.btnOK.SetDefault()
  2059. #
  2060. # list of vector maps
  2061. #
  2062. self.listMap = CheckListBox(parent=self, id=wx.ID_ANY,
  2063. choices=vectlist)
  2064. if os.path.isfile(self.vgrpfile):
  2065. f = open(self.vgrpfile)
  2066. try:
  2067. checked = []
  2068. for line in f.readlines():
  2069. line = line.replace('\n', '')
  2070. if len(line) < 1:
  2071. continue
  2072. checked.append(line)
  2073. self.listMap.SetCheckedStrings(checked)
  2074. finally:
  2075. f.close()
  2076. line = wx.StaticLine(parent=self,
  2077. id=wx.ID_ANY, size=(20, -1),
  2078. style=wx.LI_HORIZONTAL)
  2079. #
  2080. # layout
  2081. #
  2082. sizer = wx.BoxSizer(wx.VERTICAL)
  2083. box = wx.BoxSizer(wx.HORIZONTAL)
  2084. box.Add(
  2085. StaticText(
  2086. parent=self,
  2087. id=wx.ID_ANY,
  2088. label=_('Select vector map(s) to add to group:')),
  2089. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT | wx.LEFT,
  2090. border=5)
  2091. box.Add(self.listMap,
  2092. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT | wx.LEFT,
  2093. border=5)
  2094. sizer.Add(box, flag=wx.ALIGN_RIGHT | wx.ALL,
  2095. border=3)
  2096. sizer.Add(line, proportion=0,
  2097. flag=wx.GROW | wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT,
  2098. border=5)
  2099. # buttons
  2100. btnSizer = wx.StdDialogButtonSizer()
  2101. btnSizer.AddButton(self.btnCancel)
  2102. btnSizer.AddButton(self.btnOK)
  2103. btnSizer.Realize()
  2104. sizer.Add(btnSizer, proportion=0,
  2105. flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER,
  2106. border=5)
  2107. self.SetSizer(sizer)
  2108. sizer.Fit(self)
  2109. self.Layout()
  2110. def MakeVGroup(self):
  2111. """Create VREF file"""
  2112. vgrouplist = []
  2113. for item in range(self.listMap.GetCount()):
  2114. if not self.listMap.IsChecked(item):
  2115. continue
  2116. vgrouplist.append(
  2117. self.listMap.GetString(item) +
  2118. '@' +
  2119. self.xymapset)
  2120. f = open(self.vgrpfile, mode='w')
  2121. try:
  2122. for vect in vgrouplist:
  2123. f.write(vect + '\n')
  2124. finally:
  2125. f.close()
  2126. class EditGCP(wx.Dialog):
  2127. def __init__(self, parent, data, gcpno, id=wx.ID_ANY,
  2128. title=_("Edit GCP"),
  2129. style=wx.DEFAULT_DIALOG_STYLE):
  2130. """Dialog for editing GPC and map coordinates in list control"""
  2131. wx.Dialog.__init__(self, parent, id, title=title, style=style)
  2132. panel = wx.Panel(parent=self)
  2133. sizer = wx.BoxSizer(wx.VERTICAL)
  2134. box = StaticBox(
  2135. parent=panel, id=wx.ID_ANY, label=" %s %s " %
  2136. (_("Ground Control Point No."), str(gcpno)))
  2137. boxSizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  2138. # source coordinates
  2139. gridSizer = wx.GridBagSizer(vgap=5, hgap=5)
  2140. self.xcoord = TextCtrl(parent=panel, id=wx.ID_ANY, size=(150, -1))
  2141. self.ycoord = TextCtrl(parent=panel, id=wx.ID_ANY, size=(150, -1))
  2142. self.zcoord = TextCtrl(parent=panel, id=wx.ID_ANY, size=(150, -1))
  2143. self.ecoord = TextCtrl(parent=panel, id=wx.ID_ANY, size=(150, -1))
  2144. self.ncoord = TextCtrl(parent=panel, id=wx.ID_ANY, size=(150, -1))
  2145. self.hcoord = TextCtrl(parent=panel, id=wx.ID_ANY, size=(150, -1))
  2146. # swap source N, target E
  2147. tmp_coord = data[1]
  2148. data[1] = data[2]
  2149. data[2] = tmp_coord
  2150. row = 0
  2151. col = 0
  2152. idx = 0
  2153. for label, win in ((_("source E:"), self.xcoord),
  2154. (_("target E:"), self.ecoord),
  2155. (_("source N:"), self.ycoord),
  2156. (_("target N:"), self.ncoord),
  2157. (_("source Z:"), self.zcoord),
  2158. (_("target Z:"), self.hcoord)):
  2159. label = StaticText(parent=panel, id=wx.ID_ANY,
  2160. label=label)
  2161. gridSizer.Add(label,
  2162. flag=wx.ALIGN_CENTER_VERTICAL,
  2163. pos=(row, col))
  2164. col += 1
  2165. win.SetValue(str(data[idx]))
  2166. gridSizer.Add(win,
  2167. pos=(row, col))
  2168. col += 1
  2169. idx += 1
  2170. if col > 3:
  2171. row += 1
  2172. col = 0
  2173. boxSizer.Add(gridSizer, proportion=1,
  2174. flag=wx.EXPAND | wx.ALL, border=5)
  2175. sizer.Add(boxSizer, proportion=1,
  2176. flag=wx.EXPAND | wx.ALL, border=5)
  2177. #
  2178. # buttons
  2179. #
  2180. self.btnCancel = Button(panel, wx.ID_CANCEL)
  2181. self.btnOk = Button(panel, wx.ID_OK)
  2182. self.btnOk.SetDefault()
  2183. btnSizer = wx.StdDialogButtonSizer()
  2184. btnSizer.AddButton(self.btnCancel)
  2185. btnSizer.AddButton(self.btnOk)
  2186. btnSizer.Realize()
  2187. sizer.Add(btnSizer, proportion=0,
  2188. flag=wx.ALIGN_RIGHT | wx.ALL, border=5)
  2189. panel.SetSizer(sizer)
  2190. sizer.Fit(self)
  2191. def GetValues(self, columns=None):
  2192. """Return list of values (as strings).
  2193. """
  2194. valuelist = []
  2195. try:
  2196. float(self.xcoord.GetValue())
  2197. float(self.ycoord.GetValue())
  2198. float(self.zcoord.GetValue())
  2199. float(self.ecoord.GetValue())
  2200. float(self.ncoord.GetValue())
  2201. float(self.hcoord.GetValue())
  2202. except ValueError:
  2203. return valuelist
  2204. valuelist.append(self.xcoord.GetValue())
  2205. valuelist.append(self.ycoord.GetValue())
  2206. valuelist.append(self.zcoord.GetValue())
  2207. valuelist.append(self.ecoord.GetValue())
  2208. valuelist.append(self.ncoord.GetValue())
  2209. valuelist.append(self.hcoord.GetValue())
  2210. return valuelist
  2211. class GrSettingsDialog(wx.Dialog):
  2212. def __init__(
  2213. self, parent, id, giface, title, pos=wx.DefaultPosition,
  2214. size=wx.DefaultSize, style=wx.DEFAULT_DIALOG_STYLE):
  2215. wx.Dialog.__init__(self, parent, id, title, pos, size, style)
  2216. """
  2217. Dialog to set profile text options: font, title
  2218. and font size, axis labels and font size
  2219. """
  2220. #
  2221. # initialize variables
  2222. #
  2223. self.parent = parent
  2224. self.new_src_map = src_map
  2225. self.new_tgt_map = {'raster': tgt_map['raster'],
  2226. 'vector': tgt_map['vector']}
  2227. self.sdfactor = 0
  2228. self.symbol = {}
  2229. self.methods = ["nearest",
  2230. "linear",
  2231. "linear_f",
  2232. "cubic",
  2233. "cubic_f",
  2234. "lanczos",
  2235. "lanczos_f"]
  2236. # notebook
  2237. notebook = wx.Notebook(parent=self, id=wx.ID_ANY, style=wx.BK_DEFAULT)
  2238. self.__CreateSymbologyPage(notebook)
  2239. self.__CreateRectificationPage(notebook)
  2240. # buttons
  2241. btnSave = Button(self, wx.ID_SAVE)
  2242. btnApply = Button(self, wx.ID_APPLY)
  2243. btnClose = Button(self, wx.ID_CLOSE)
  2244. btnApply.SetDefault()
  2245. # bindings
  2246. btnApply.Bind(wx.EVT_BUTTON, self.OnApply)
  2247. btnApply.SetToolTip(_("Apply changes for the current session"))
  2248. btnSave.Bind(wx.EVT_BUTTON, self.OnSave)
  2249. btnSave.SetToolTip(
  2250. _("Apply and save changes to user settings file (default for next sessions)"))
  2251. btnClose.Bind(wx.EVT_BUTTON, self.OnClose)
  2252. btnClose.SetToolTip(_("Close dialog"))
  2253. # sizers
  2254. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  2255. btnSizer.Add(btnApply, flag=wx.LEFT | wx.RIGHT, border=5)
  2256. btnSizer.Add(btnSave, flag=wx.LEFT | wx.RIGHT, border=5)
  2257. btnSizer.Add(btnClose, flag=wx.LEFT | wx.RIGHT, border=5)
  2258. # sizers
  2259. mainSizer = wx.BoxSizer(wx.VERTICAL)
  2260. mainSizer.Add(
  2261. notebook,
  2262. proportion=1,
  2263. flag=wx.EXPAND | wx.ALL,
  2264. border=5)
  2265. mainSizer.Add(btnSizer, proportion=0,
  2266. flag=wx.ALIGN_RIGHT | wx.ALL, border=5)
  2267. # flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
  2268. self.SetSizer(mainSizer)
  2269. mainSizer.Fit(self)
  2270. def __CreateSymbologyPage(self, notebook):
  2271. """Create notebook page with symbology settings"""
  2272. panel = wx.Panel(parent=notebook, id=wx.ID_ANY)
  2273. notebook.AddPage(page=panel, text=_("Symbology"))
  2274. sizer = wx.BoxSizer(wx.VERTICAL)
  2275. rmsgridSizer = wx.GridBagSizer(vgap=5, hgap=5)
  2276. # highlight only highest forward RMS error
  2277. self.highlighthighest = wx.CheckBox(
  2278. parent=panel, id=wx.ID_ANY,
  2279. label=_("Highlight highest RMS error only"))
  2280. hh = UserSettings.Get(group='gcpman', key='rms', subkey='highestonly')
  2281. self.highlighthighest.SetValue(hh)
  2282. rmsgridSizer.Add(
  2283. self.highlighthighest,
  2284. flag=wx.ALIGN_CENTER_VERTICAL,
  2285. pos=(
  2286. 0,
  2287. 0))
  2288. # RMS forward error threshold
  2289. rmslabel = StaticText(
  2290. parent=panel, id=wx.ID_ANY,
  2291. label=_("Highlight RMS error > M + SD * factor:"))
  2292. rmslabel.SetToolTip(
  2293. _(
  2294. "Highlight GCPs with an RMS error larger than \n"
  2295. "mean + standard deviation * given factor. \n"
  2296. "Recommended values for this factor are between 1 and 2."))
  2297. rmsgridSizer.Add(
  2298. rmslabel,
  2299. flag=wx.ALIGN_CENTER_VERTICAL,
  2300. pos=(
  2301. 1,
  2302. 0))
  2303. sdfactor = UserSettings.Get(
  2304. group='gcpman', key='rms', subkey='sdfactor')
  2305. self.rmsWin = TextCtrl(parent=panel, id=wx.ID_ANY,
  2306. size=(70, -1), style=wx.TE_NOHIDESEL)
  2307. self.rmsWin.SetValue("%s" % str(sdfactor))
  2308. if (self.parent.highest_only):
  2309. self.rmsWin.Disable()
  2310. self.symbol['sdfactor'] = self.rmsWin.GetId()
  2311. rmsgridSizer.Add(self.rmsWin, flag=wx.ALIGN_RIGHT, pos=(1, 1))
  2312. rmsgridSizer.AddGrowableCol(1)
  2313. sizer.Add(rmsgridSizer, flag=wx.EXPAND | wx.ALL, border=5)
  2314. box = StaticBox(parent=panel, id=wx.ID_ANY,
  2315. label=" %s " % _("Symbol settings"))
  2316. boxSizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  2317. gridSizer = wx.GridBagSizer(vgap=5, hgap=5)
  2318. #
  2319. # general symbol color
  2320. #
  2321. row = 0
  2322. label = StaticText(parent=panel, id=wx.ID_ANY, label=_("Color:"))
  2323. gridSizer.Add(label, flag=wx.ALIGN_CENTER_VERTICAL, pos=(row, 0))
  2324. col = UserSettings.Get(group='gcpman', key='symbol', subkey='color')
  2325. colWin = csel.ColourSelect(parent=panel, id=wx.ID_ANY,
  2326. colour=wx.Colour(col[0],
  2327. col[1],
  2328. col[2],
  2329. 255))
  2330. self.symbol['color'] = colWin.GetId()
  2331. gridSizer.Add(colWin,
  2332. flag=wx.ALIGN_RIGHT,
  2333. pos=(row, 1))
  2334. #
  2335. # symbol color for high forward RMS error
  2336. #
  2337. row += 1
  2338. label = StaticText(
  2339. parent=panel,
  2340. id=wx.ID_ANY,
  2341. label=_("Color for high RMS error:"))
  2342. gridSizer.Add(label, flag=wx.ALIGN_CENTER_VERTICAL, pos=(row, 0))
  2343. hcol = UserSettings.Get(group='gcpman', key='symbol', subkey='hcolor')
  2344. hcolWin = csel.ColourSelect(parent=panel, id=wx.ID_ANY,
  2345. colour=wx.Colour(hcol[0],
  2346. hcol[1],
  2347. hcol[2],
  2348. 255))
  2349. self.symbol['hcolor'] = hcolWin.GetId()
  2350. gridSizer.Add(hcolWin,
  2351. flag=wx.ALIGN_RIGHT,
  2352. pos=(row, 1))
  2353. #
  2354. # symbol color for selected GCP
  2355. #
  2356. row += 1
  2357. label = StaticText(
  2358. parent=panel,
  2359. id=wx.ID_ANY,
  2360. label=_("Color for selected GCP:"))
  2361. gridSizer.Add(label, flag=wx.ALIGN_CENTER_VERTICAL, pos=(row, 0))
  2362. scol = UserSettings.Get(group='gcpman', key='symbol', subkey='scolor')
  2363. scolWin = csel.ColourSelect(parent=panel, id=wx.ID_ANY,
  2364. colour=wx.Colour(scol[0],
  2365. scol[1],
  2366. scol[2],
  2367. 255))
  2368. self.symbol['scolor'] = scolWin.GetId()
  2369. gridSizer.Add(scolWin,
  2370. flag=wx.ALIGN_RIGHT,
  2371. pos=(row, 1))
  2372. #
  2373. # symbol color for unused GCP
  2374. #
  2375. row += 1
  2376. label = StaticText(
  2377. parent=panel,
  2378. id=wx.ID_ANY,
  2379. label=_("Color for unused GCPs:"))
  2380. gridSizer.Add(label, flag=wx.ALIGN_CENTER_VERTICAL, pos=(row, 0))
  2381. ucol = UserSettings.Get(group='gcpman', key='symbol', subkey='ucolor')
  2382. ucolWin = csel.ColourSelect(parent=panel, id=wx.ID_ANY,
  2383. colour=wx.Colour(ucol[0],
  2384. ucol[1],
  2385. ucol[2],
  2386. 255))
  2387. self.symbol['ucolor'] = ucolWin.GetId()
  2388. gridSizer.Add(ucolWin,
  2389. flag=wx.ALIGN_RIGHT,
  2390. pos=(row, 1))
  2391. # show unused GCPs
  2392. row += 1
  2393. self.showunused = wx.CheckBox(parent=panel, id=wx.ID_ANY,
  2394. label=_("Show unused GCPs"))
  2395. shuu = UserSettings.Get(group='gcpman', key='symbol', subkey='unused')
  2396. self.showunused.SetValue(shuu)
  2397. gridSizer.Add(
  2398. self.showunused,
  2399. flag=wx.ALIGN_CENTER_VERTICAL,
  2400. pos=(
  2401. row,
  2402. 0))
  2403. #
  2404. # symbol size
  2405. #
  2406. row += 1
  2407. label = StaticText(
  2408. parent=panel,
  2409. id=wx.ID_ANY,
  2410. label=_("Symbol size:"))
  2411. gridSizer.Add(label, flag=wx.ALIGN_CENTER_VERTICAL, pos=(row, 0))
  2412. symsize = int(
  2413. UserSettings.Get(
  2414. group='gcpman',
  2415. key='symbol',
  2416. subkey='size'))
  2417. sizeWin = SpinCtrl(parent=panel, id=wx.ID_ANY,
  2418. min=1, max=20)
  2419. sizeWin.SetValue(symsize)
  2420. self.symbol['size'] = sizeWin.GetId()
  2421. gridSizer.Add(sizeWin,
  2422. flag=wx.ALIGN_RIGHT,
  2423. pos=(row, 1))
  2424. #
  2425. # symbol width
  2426. #
  2427. row += 1
  2428. label = StaticText(
  2429. parent=panel,
  2430. id=wx.ID_ANY,
  2431. label=_("Line width:"))
  2432. gridSizer.Add(label, flag=wx.ALIGN_CENTER_VERTICAL, pos=(row, 0))
  2433. width = int(
  2434. UserSettings.Get(
  2435. group='gcpman',
  2436. key='symbol',
  2437. subkey='width'))
  2438. widWin = SpinCtrl(parent=panel, id=wx.ID_ANY,
  2439. min=1, max=10)
  2440. widWin.SetValue(width)
  2441. self.symbol['width'] = widWin.GetId()
  2442. gridSizer.Add(widWin,
  2443. flag=wx.ALIGN_RIGHT,
  2444. pos=(row, 1))
  2445. gridSizer.AddGrowableCol(1)
  2446. boxSizer.Add(gridSizer, flag=wx.EXPAND)
  2447. sizer.Add(boxSizer, flag=wx.EXPAND | wx.ALL, border=5)
  2448. #
  2449. # maps to display
  2450. #
  2451. # source map to display
  2452. self.srcselection = Select(
  2453. panel,
  2454. id=wx.ID_ANY,
  2455. size=globalvar.DIALOG_GSELECT_SIZE,
  2456. type='maptype',
  2457. updateOnPopup=False)
  2458. self.parent.grwiz.SwitchEnv('source')
  2459. self.srcselection.SetElementList(maptype)
  2460. # filter out all maps not in group
  2461. self.srcselection.tcp.GetElementList(elements=self.parent.src_maps)
  2462. # target map(s) to display
  2463. self.parent.grwiz.SwitchEnv('target')
  2464. self.tgtrastselection = Select(
  2465. panel, id=wx.ID_ANY, size=globalvar.DIALOG_GSELECT_SIZE,
  2466. type='raster', updateOnPopup=False)
  2467. self.tgtrastselection.SetElementList('cell')
  2468. self.tgtrastselection.GetElementList()
  2469. self.tgtvectselection = Select(
  2470. panel, id=wx.ID_ANY, size=globalvar.DIALOG_GSELECT_SIZE,
  2471. type='vector', updateOnPopup=False)
  2472. self.tgtvectselection.SetElementList('vector')
  2473. self.tgtvectselection.GetElementList()
  2474. sizer.Add(
  2475. StaticText(
  2476. parent=panel,
  2477. id=wx.ID_ANY,
  2478. label=_('Select source map to display:')),
  2479. proportion=0,
  2480. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  2481. border=5)
  2482. sizer.Add(
  2483. self.srcselection,
  2484. proportion=0,
  2485. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  2486. border=5)
  2487. self.srcselection.SetValue(src_map)
  2488. sizer.Add(
  2489. StaticText(
  2490. parent=panel,
  2491. id=wx.ID_ANY,
  2492. label=_('Select target raster map to display:')),
  2493. proportion=0,
  2494. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  2495. border=5)
  2496. sizer.Add(
  2497. self.tgtrastselection,
  2498. proportion=0,
  2499. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  2500. border=5)
  2501. self.tgtrastselection.SetValue(tgt_map['raster'])
  2502. sizer.Add(
  2503. StaticText(
  2504. parent=panel,
  2505. id=wx.ID_ANY,
  2506. label=_('Select target vector map to display:')),
  2507. proportion=0,
  2508. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  2509. border=5)
  2510. sizer.Add(
  2511. self.tgtvectselection,
  2512. proportion=0,
  2513. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  2514. border=5)
  2515. self.tgtvectselection.SetValue(tgt_map['vector'])
  2516. # bindings
  2517. self.highlighthighest.Bind(wx.EVT_CHECKBOX, self.OnHighlight)
  2518. self.rmsWin.Bind(wx.EVT_TEXT, self.OnSDFactor)
  2519. self.srcselection.Bind(wx.EVT_TEXT, self.OnSrcSelection)
  2520. self.tgtrastselection.Bind(wx.EVT_TEXT, self.OnTgtRastSelection)
  2521. self.tgtvectselection.Bind(wx.EVT_TEXT, self.OnTgtVectSelection)
  2522. panel.SetSizer(sizer)
  2523. return panel
  2524. def __CreateRectificationPage(self, notebook):
  2525. """Create notebook page with symbology settings"""
  2526. panel = wx.Panel(parent=notebook, id=wx.ID_ANY)
  2527. notebook.AddPage(page=panel, text=_("Rectification"))
  2528. sizer = wx.BoxSizer(wx.VERTICAL)
  2529. # transformation order
  2530. self.rb_grorder = wx.RadioBox(
  2531. parent=panel,
  2532. id=wx.ID_ANY,
  2533. label=" %s " %
  2534. _("Select rectification order"),
  2535. choices=[
  2536. _('1st order'),
  2537. _('2nd order'),
  2538. _('3rd order')],
  2539. majorDimension=wx.RA_SPECIFY_COLS)
  2540. sizer.Add(self.rb_grorder, proportion=0,
  2541. flag=wx.EXPAND | wx.ALL, border=5)
  2542. self.rb_grorder.SetSelection(self.parent.gr_order - 1)
  2543. # interpolation method
  2544. gridSizer = wx.GridBagSizer(vgap=5, hgap=5)
  2545. gridSizer.Add(
  2546. StaticText(
  2547. parent=panel,
  2548. id=wx.ID_ANY,
  2549. label=_('Select interpolation method:')),
  2550. pos=(
  2551. 0,
  2552. 0),
  2553. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  2554. border=5)
  2555. self.grmethod = wx.Choice(parent=panel, id=wx.ID_ANY,
  2556. choices=self.methods)
  2557. gridSizer.Add(self.grmethod, pos=(0, 1),
  2558. flag=wx.ALIGN_RIGHT, border=5)
  2559. self.grmethod.SetStringSelection(self.parent.gr_method)
  2560. gridSizer.AddGrowableCol(1)
  2561. sizer.Add(gridSizer, flag=wx.EXPAND | wx.ALL, border=5)
  2562. # clip to region
  2563. self.check = wx.CheckBox(parent=panel, id=wx.ID_ANY, label=_(
  2564. "clip to computational region in target location"))
  2565. sizer.Add(self.check, proportion=0,
  2566. flag=wx.EXPAND | wx.ALL, border=5)
  2567. self.check.SetValue(self.parent.clip_to_region)
  2568. # extension
  2569. sizer.Add(
  2570. StaticText(
  2571. parent=panel,
  2572. id=wx.ID_ANY,
  2573. label=_('Extension for output maps:')),
  2574. proportion=0,
  2575. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  2576. border=5)
  2577. self.ext_txt = TextCtrl(
  2578. parent=panel, id=wx.ID_ANY, value="", size=(
  2579. 350, -1))
  2580. self.ext_txt.SetValue(self.parent.extension)
  2581. sizer.Add(
  2582. self.ext_txt,
  2583. proportion=0,
  2584. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  2585. border=5)
  2586. # bindings
  2587. self.ext_txt.Bind(wx.EVT_TEXT, self.OnExtension)
  2588. self.Bind(wx.EVT_RADIOBOX, self.parent.OnGROrder, self.rb_grorder)
  2589. self.Bind(wx.EVT_CHOICE, self.OnMethod, self.grmethod)
  2590. self.Bind(wx.EVT_CHECKBOX, self.OnClipRegion, self.check)
  2591. panel.SetSizer(sizer)
  2592. return panel
  2593. def OnHighlight(self, event):
  2594. """Checkbox 'highlighthighest' checked/unchecked"""
  2595. if self.highlighthighest.IsChecked():
  2596. self.parent.highest_only = True
  2597. self.rmsWin.Disable()
  2598. else:
  2599. self.parent.highest_only = False
  2600. self.rmsWin.Enable()
  2601. def OnSDFactor(self, event):
  2602. """New factor for RMS threshold = M + SD * factor"""
  2603. try:
  2604. self.sdfactor = float(self.rmsWin.GetValue())
  2605. except ValueError:
  2606. return
  2607. if self.sdfactor <= 0:
  2608. GError(parent=self,
  2609. message=_('RMS threshold factor must be > 0'))
  2610. elif self.sdfactor < 1:
  2611. GError(parent=self,
  2612. message=_('RMS threshold factor is < 1\n'
  2613. 'Too many points might be highlighted'))
  2614. def OnSrcSelection(self, event):
  2615. """Source map to display selected"""
  2616. global src_map
  2617. tmp_map = self.srcselection.GetValue()
  2618. if not tmp_map == '' and not tmp_map == src_map:
  2619. self.new_src_map = tmp_map
  2620. def OnTgtRastSelection(self, event):
  2621. """Target map to display selected"""
  2622. global tgt_map
  2623. self.new_tgt_map['raster'] = self.tgtrastselection.GetValue()
  2624. def OnTgtVectSelection(self, event):
  2625. """Target map to display selected"""
  2626. global tgt_map
  2627. self.new_tgt_map['vector'] = self.tgtvectselection.GetValue()
  2628. def OnMethod(self, event):
  2629. self.parent.gr_method = self.methods[event.GetSelection()]
  2630. def OnClipRegion(self, event):
  2631. self.parent.clip_to_region = event.IsChecked()
  2632. def OnExtension(self, event):
  2633. self.parent.extension = self.ext_txt.GetValue()
  2634. def UpdateSettings(self):
  2635. global src_map
  2636. global tgt_map
  2637. global maptype
  2638. layers = None
  2639. UserSettings.Set(group='gcpman', key='rms', subkey='highestonly',
  2640. value=self.highlighthighest.GetValue())
  2641. if self.sdfactor > 0:
  2642. UserSettings.Set(group='gcpman', key='rms', subkey='sdfactor',
  2643. value=self.sdfactor)
  2644. self.parent.sdfactor = self.sdfactor
  2645. if self.parent.rmsthresh > 0:
  2646. self.parent.rmsthresh = self.parent.rmsmean + self.parent.sdfactor * self.parent.rmssd
  2647. UserSettings.Set(
  2648. group='gcpman',
  2649. key='symbol',
  2650. subkey='color',
  2651. value=tuple(
  2652. wx.FindWindowById(
  2653. self.symbol['color']).GetColour()))
  2654. UserSettings.Set(
  2655. group='gcpman',
  2656. key='symbol',
  2657. subkey='hcolor',
  2658. value=tuple(
  2659. wx.FindWindowById(
  2660. self.symbol['hcolor']).GetColour()))
  2661. UserSettings.Set(
  2662. group='gcpman',
  2663. key='symbol',
  2664. subkey='scolor',
  2665. value=tuple(
  2666. wx.FindWindowById(
  2667. self.symbol['scolor']).GetColour()))
  2668. UserSettings.Set(
  2669. group='gcpman',
  2670. key='symbol',
  2671. subkey='ucolor',
  2672. value=tuple(
  2673. wx.FindWindowById(
  2674. self.symbol['ucolor']).GetColour()))
  2675. UserSettings.Set(group='gcpman', key='symbol', subkey='unused',
  2676. value=self.showunused.GetValue())
  2677. UserSettings.Set(
  2678. group='gcpman',
  2679. key='symbol',
  2680. subkey='size',
  2681. value=wx.FindWindowById(
  2682. self.symbol['size']).GetValue())
  2683. UserSettings.Set(
  2684. group='gcpman',
  2685. key='symbol',
  2686. subkey='width',
  2687. value=wx.FindWindowById(
  2688. self.symbol['width']).GetValue())
  2689. srcrender = False
  2690. srcrenderVector = False
  2691. tgtrender = False
  2692. tgtrenderVector = False
  2693. reload_target = False
  2694. if self.new_src_map != src_map:
  2695. # remove old layer
  2696. layers = self.parent.grwiz.SrcMap.GetListOfLayers()
  2697. self.parent.grwiz.SrcMap.DeleteLayer(layers[0])
  2698. src_map = self.new_src_map
  2699. if maptype == 'raster':
  2700. cmdlist = ['d.rast', 'map=%s' % src_map]
  2701. srcrender = True
  2702. else:
  2703. cmdlist = ['d.vect', 'map=%s' % src_map]
  2704. srcrenderVector = True
  2705. self.parent.grwiz.SwitchEnv('source')
  2706. name, found = utils.GetLayerNameFromCmd(cmdlist)
  2707. self.parent.grwiz.SrcMap.AddLayer(
  2708. ltype=maptype, command=cmdlist, active=True, name=name,
  2709. hidden=False, opacity=1.0, render=False)
  2710. self.parent.grwiz.SwitchEnv('target')
  2711. if self.new_tgt_map['raster'] != tgt_map['raster'] or \
  2712. self.new_tgt_map['vector'] != tgt_map['vector']:
  2713. # remove all layers
  2714. layers = self.parent.grwiz.TgtMap.GetListOfLayers()
  2715. while layers:
  2716. self.parent.grwiz.TgtMap.DeleteLayer(layers[0])
  2717. del layers[0]
  2718. layers = self.parent.grwiz.TgtMap.GetListOfLayers()
  2719. # self.parent.grwiz.TgtMap.DeleteAllLayers()
  2720. reload_target = True
  2721. tgt_map['raster'] = self.new_tgt_map['raster']
  2722. tgt_map['vector'] = self.new_tgt_map['vector']
  2723. if tgt_map['raster'] != '':
  2724. cmdlist = ['d.rast', 'map=%s' % tgt_map['raster']]
  2725. name, found = utils.GetLayerNameFromCmd(cmdlist)
  2726. self.parent.grwiz.TgtMap.AddLayer(
  2727. ltype='raster', command=cmdlist, active=True, name=name,
  2728. hidden=False, opacity=1.0, render=False)
  2729. tgtrender = True
  2730. if tgt_map['vector'] != '':
  2731. cmdlist = ['d.vect', 'map=%s' % tgt_map['vector']]
  2732. name, found = utils.GetLayerNameFromCmd(cmdlist)
  2733. self.parent.grwiz.TgtMap.AddLayer(
  2734. ltype='vector', command=cmdlist, active=True, name=name,
  2735. hidden=False, opacity=1.0, render=False)
  2736. tgtrenderVector = True
  2737. if tgt_map['raster'] == '' and tgt_map['vector'] == '':
  2738. if self.parent.show_target:
  2739. self.parent.show_target = False
  2740. self.parent._mgr.GetPane("target").Hide()
  2741. self.parent._mgr.Update()
  2742. self.parent.activemap.SetSelection(0)
  2743. self.parent.activemap.Enable(False)
  2744. self.parent.GetMapToolbar().Enable('zoommenu', enable=False)
  2745. else:
  2746. if not self.parent.show_target:
  2747. self.parent.show_target = True
  2748. self.parent._mgr.GetPane("target").Show()
  2749. self.parent._mgr.Update()
  2750. self.parent.activemap.SetSelection(0)
  2751. self.parent.activemap.Enable(True)
  2752. self.parent.GetMapToolbar().Enable('zoommenu', enable=True)
  2753. self.parent.TgtMapWindow.ZoomToMap(
  2754. layers=self.parent.TgtMap.GetListOfLayers())
  2755. self.parent.UpdateColours(
  2756. srcrender,
  2757. srcrenderVector,
  2758. tgtrender,
  2759. tgtrenderVector)
  2760. self.parent.SetSettings()
  2761. def OnSave(self, event):
  2762. """Button 'Save' pressed"""
  2763. self.UpdateSettings()
  2764. fileSettings = {}
  2765. UserSettings.ReadSettingsFile(settings=fileSettings)
  2766. fileSettings['gcpman'] = UserSettings.Get(group='gcpman')
  2767. file = UserSettings.SaveToFile(fileSettings)
  2768. self.parent._giface.WriteLog(
  2769. _('GCP Manager settings saved to file \'%s\'.') %
  2770. file)
  2771. # self.Close()
  2772. def OnApply(self, event):
  2773. """Button 'Apply' pressed"""
  2774. self.UpdateSettings()
  2775. # self.Close()
  2776. def OnClose(self, event):
  2777. """Button 'Cancel' pressed"""
  2778. self.Close()