manager.py 114 KB

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