location_wizard.py 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687
  1. """
  2. @package location_wizard.py
  3. @brief Location wizard - creates a new GRASS Location. User can choose
  4. from multiple methods.
  5. Classes:
  6. - BaseClass
  7. - TitledPage
  8. - DatabasePage
  9. - CoordinateSystemPage
  10. - ProjectionsPage
  11. - ItemList
  12. - ProjTypePage
  13. - DatumPage
  14. - EllipsePage
  15. - GeoreferencedFilePage
  16. - EPSGPage
  17. - CustomPage
  18. - SummaryPage
  19. - RegionDef
  20. - LocationWizard
  21. - SelectDatumDialog
  22. COPYRIGHT: (C) 2007-2009 by the GRASS Development Team
  23. This program is free software under the GNU General Public
  24. License (>=v2). Read the file COPYING that comes with GRASS
  25. for details.
  26. @author Michael Barton
  27. @author Jachym Cepicky
  28. @author Martin Landa <landa.martin gmail.com>
  29. """
  30. import os
  31. import shutil
  32. import re
  33. import string
  34. import sys
  35. import locale
  36. import platform
  37. import wx
  38. import wx.lib.mixins.listctrl as listmix
  39. import wx.wizard as wiz
  40. import gcmd
  41. import globalvar
  42. import utils
  43. try:
  44. import subprocess
  45. except:
  46. CompatPath = os.path.join(globalvar.ETCWXDIR)
  47. sys.path.append(CompatPath)
  48. from compat import subprocess
  49. global coordsys
  50. global north
  51. global south
  52. global east
  53. global west
  54. global resolution
  55. global wizerror
  56. coordsys = ''
  57. north = ''
  58. south = ''
  59. east = ''
  60. west = ''
  61. resolution = ''
  62. class BaseClass(wx.Object):
  63. """Base class providing basic methods"""
  64. def __init__(self):
  65. pass
  66. def MakeLabel(self, text="", style=wx.ALIGN_LEFT, parent=None):
  67. """Make aligned label"""
  68. if not parent:
  69. parent = self
  70. return wx.StaticText(parent=parent, id=wx.ID_ANY, label=text,
  71. style=style)
  72. def MakeTextCtrl(self, text='', size=(100,-1), style=0, parent=None):
  73. """Generic text control"""
  74. if not parent:
  75. parent = self
  76. return wx.TextCtrl(parent=parent, id=wx.ID_ANY, value=text,
  77. size=size, style=style)
  78. def MakeButton(self, text, id=wx.ID_ANY, size=(-1,-1), parent=None):
  79. """Generic button"""
  80. if not parent:
  81. parent = self
  82. return wx.Button(parent=parent, id=id, label=text,
  83. size=size)
  84. class TitledPage(BaseClass, wiz.WizardPageSimple):
  85. """
  86. Class to make wizard pages. Generic methods to make
  87. labels, text entries, and buttons.
  88. """
  89. def __init__(self, parent, title):
  90. self.page = wiz.WizardPageSimple.__init__(self, parent)
  91. # page title
  92. self.title = wx.StaticText(parent=self, id=wx.ID_ANY, label=title)
  93. self.title.SetFont(wx.Font(13, wx.SWISS, wx.NORMAL, wx.BOLD))
  94. # main sizer
  95. self.sizer = wx.GridBagSizer(vgap=0, hgap=0)
  96. def DoLayout(self):
  97. """Do page layout"""
  98. tmpsizer = wx.BoxSizer(wx.VERTICAL)
  99. tmpsizer.Add(item=self.title, proportion=0,
  100. flag=wx.ALIGN_CENTRE | wx.ALL,
  101. border=5)
  102. tmpsizer.Add(item=wx.StaticLine(self, -1), proportion=0,
  103. flag=wx.EXPAND | wx.ALL,
  104. border=0)
  105. tmpsizer.Add(item=self.sizer, proportion=1,
  106. flag=wx.EXPAND | wx.ALL,
  107. border=5)
  108. self.SetAutoLayout(True)
  109. self.SetSizer(tmpsizer)
  110. # tmpsizer.Fit(self)
  111. self.Layout()
  112. class DatabasePage(TitledPage):
  113. """
  114. Wizard page for setting GIS data directory and location name
  115. """
  116. def __init__(self, wizard, parent, grassdatabase):
  117. TitledPage.__init__(self, wizard, _("Define GRASS Database and Location Name"))
  118. self.grassdatabase = grassdatabase
  119. self.location = ''
  120. # buttons
  121. self.bbrowse = self.MakeButton(_("Browse"))
  122. # text controls
  123. self.tgisdbase = self.MakeTextCtrl(grassdatabase, size=(300, -1))
  124. self.tlocation = self.MakeTextCtrl("newLocation", size=(300, -1))
  125. # layout
  126. self.sizer.AddGrowableCol(3)
  127. self.sizer.Add(item=self.MakeLabel(_("GIS Data Directory:")),
  128. flag=wx.ALIGN_RIGHT |
  129. wx.ALIGN_CENTER_VERTICAL |
  130. wx.ALL, border=5,
  131. pos=(1, 1))
  132. self.sizer.Add(item=self.tgisdbase,
  133. flag=wx.ALIGN_LEFT |
  134. wx.ALIGN_CENTER_VERTICAL |
  135. wx.ALL, border=5,
  136. pos=(1, 2))
  137. self.sizer.Add(item=self.bbrowse,
  138. flag=wx.ALIGN_LEFT |
  139. wx.ALIGN_CENTER_VERTICAL |
  140. wx.ALL, border=5,
  141. pos=(1, 3))
  142. #
  143. self.sizer.Add(item=self.MakeLabel("%s:" % _("Project Location")),
  144. flag=wx.ALIGN_RIGHT |
  145. wx.ALIGN_CENTER_VERTICAL |
  146. wx.ALL, border=5,
  147. pos=(2, 1))
  148. self.sizer.Add(item=self.tlocation,
  149. flag=wx.ALIGN_LEFT |
  150. wx.ALIGN_CENTER_VERTICAL |
  151. wx.ALL, border=5,
  152. pos=(2, 2))
  153. # self.sizer.Add(item=self.MakeLabel(_("(projection/coordinate system)")),
  154. # flag=wx.ALIGN_LEFT |
  155. # wx.ALIGN_CENTER_VERTICAL |
  156. # wx.ALL, border=5,
  157. # pos=(2, 4))
  158. # bindings
  159. self.Bind(wx.EVT_BUTTON, self.OnBrowse, self.bbrowse)
  160. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  161. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  162. self.tgisdbase.Bind(wx.EVT_TEXT, self.OnChangeName)
  163. self.tlocation.Bind(wx.EVT_TEXT, self.OnChangeName)
  164. # do page layout
  165. # self.DoLayout()
  166. def OnChangeName(self, event):
  167. """Name for new location was changed"""
  168. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  169. if len(event.GetString()) > 0:
  170. if not nextButton.IsEnabled():
  171. nextButton.Enable()
  172. else:
  173. nextButton.Disable()
  174. event.Skip()
  175. def OnBrowse(self, event):
  176. dlg = wx.DirDialog(self, _("Choose GRASS data directory:"),
  177. os.getcwd(), wx.DD_DEFAULT_STYLE)
  178. if dlg.ShowModal() == wx.ID_OK:
  179. self.grassdatabase = dlg.GetPath()
  180. self.tgisdbase.SetValue(self.grassdatabase)
  181. dlg.Destroy()
  182. def OnPageChanging(self,event=None):
  183. error = ''
  184. if os.path.isdir(os.path.join(self.tgisdbase.GetValue(), self.tlocation.GetValue())):
  185. error = _("Location already exists in GRASS Database.")
  186. if error != '':
  187. dlg = wx.MessageDialog(parent=self, message="%s <%s>.%s%s" % (_("Unable to create location"),
  188. str(self.tlocation.GetValue()),
  189. os.linesep,
  190. error),
  191. caption=_("Error"), style=wx.OK | wx.ICON_ERROR)
  192. dlg.ShowModal()
  193. dlg.Destroy()
  194. event.Veto()
  195. return
  196. self.location = self.tlocation.GetValue()
  197. self.grassdatabase = self.tgisdbase.GetValue()
  198. def OnEnterPage(self, event):
  199. """Wizard page changed"""
  200. self.grassdatabase = self.tgisdbase.GetValue()
  201. self.location = self.tlocation.GetValue()
  202. event.Skip()
  203. class CoordinateSystemPage(TitledPage):
  204. """
  205. Wizard page for choosing method for location creation
  206. """
  207. def __init__(self, wizard, parent):
  208. TitledPage.__init__(self, wizard, _("Choose method for creating a new location"))
  209. self.parent = parent
  210. global coordsys
  211. # toggles
  212. self.radio1 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  213. label=_("Select coordinate system"),
  214. style = wx.RB_GROUP)
  215. self.radio2 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  216. label=_("Select EPSG code of coordinate system"))
  217. self.radio3 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  218. label=_("Use coordinate system of selected "
  219. "georeferenced file"))
  220. self.radio4 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  221. label=_("Use coordinate system of selected "
  222. "WKT or PRJ file"))
  223. self.radio5 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  224. label=_("Create custom PROJ.4 parameters "
  225. "string for coordinate system"))
  226. self.radio6 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  227. label=_("Use arbitrary non-earth "
  228. "coordinate system (XY)"))
  229. # layout
  230. self.sizer.AddGrowableCol(1)
  231. self.sizer.SetVGap(10)
  232. self.sizer.Add(item=self.radio1,
  233. flag=wx.ALIGN_LEFT, pos=(1, 1))
  234. self.sizer.Add(item=self.radio2,
  235. flag=wx.ALIGN_LEFT, pos=(2, 1))
  236. self.sizer.Add(item=self.radio3,
  237. flag=wx.ALIGN_LEFT, pos=(3, 1))
  238. self.sizer.Add(item=self.radio4,
  239. flag=wx.ALIGN_LEFT, pos=(4, 1))
  240. self.sizer.Add(item=self.radio5,
  241. flag=wx.ALIGN_LEFT, pos=(5, 1))
  242. self.sizer.Add(item=self.radio6,
  243. flag=wx.ALIGN_LEFT, pos=(6, 1))
  244. # bindings
  245. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio1.GetId())
  246. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio2.GetId())
  247. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio3.GetId())
  248. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio4.GetId())
  249. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio5.GetId())
  250. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio6.GetId())
  251. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  252. # do page layout
  253. # self.DoLayout()
  254. def OnEnterPage(self, event):
  255. global coordsys
  256. if event.GetDirection() and not coordsys:
  257. coordsys = "proj"
  258. self.SetNext(self.parent.projpage)
  259. self.parent.sumpage.SetPrev(self.parent.datumpage)
  260. if not wx.FindWindowById(wx.ID_FORWARD).IsEnabled():
  261. wx.FindWindowById(wx.ID_FORWARD).Enable()
  262. def SetVal(self, event):
  263. """Choose method"""
  264. global coordsys
  265. if event.GetId() == self.radio1.GetId():
  266. coordsys = "proj"
  267. self.SetNext(self.parent.projpage)
  268. self.parent.sumpage.SetPrev(self.parent.datumpage)
  269. elif event.GetId() == self.radio2.GetId():
  270. coordsys = "epsg"
  271. self.SetNext(self.parent.epsgpage)
  272. self.parent.sumpage.SetPrev(self.parent.epsgpage)
  273. elif event.GetId() == self.radio3.GetId():
  274. coordsys = "file"
  275. self.SetNext(self.parent.filepage)
  276. self.parent.sumpage.SetPrev(self.parent.filepage)
  277. elif event.GetId() == self.radio4.GetId():
  278. coordsys = "wkt"
  279. self.SetNext(self.parent.wktpage)
  280. self.parent.sumpage.SetPrev(self.parent.wktpage)
  281. elif event.GetId() == self.radio5.GetId():
  282. coordsys = "custom"
  283. self.SetNext(self.parent.custompage)
  284. self.parent.sumpage.SetPrev(self.parent.custompage)
  285. elif event.GetId() == self.radio6.GetId():
  286. coordsys = "xy"
  287. self.SetNext(self.parent.sumpage)
  288. self.parent.sumpage.SetPrev(self.parent.csystemspage)
  289. class ProjectionsPage(TitledPage):
  290. """
  291. Wizard page for selecting projection (select coordinate system option)
  292. """
  293. def __init__(self, wizard, parent):
  294. TitledPage.__init__(self, wizard, _("Choose projection"))
  295. self.parent = parent
  296. self.proj = ''
  297. self.projdesc = ''
  298. # text input
  299. self.tproj = self.MakeTextCtrl("", size=(200,-1))
  300. # search box
  301. self.searchb = wx.SearchCtrl(self, size=(200,-1),
  302. style=wx.TE_PROCESS_ENTER)
  303. # projection list
  304. self.projlist = ItemList(self, data=self.parent.projections.items(),
  305. columns=[_('Code'), _('Description')])
  306. # layout
  307. self.sizer.AddGrowableCol(3)
  308. self.sizer.Add(item=self.MakeLabel(_("Projection code:")),
  309. flag=wx.ALIGN_LEFT |
  310. wx.ALIGN_CENTER_VERTICAL |
  311. wx.ALL, border=5, pos=(1, 1))
  312. self.sizer.Add(item=self.tproj,
  313. flag=wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL,
  314. border=5, pos=(1, 2))
  315. self.sizer.Add(item=self.MakeLabel(_("Search in description:")),
  316. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  317. border=5, pos=(2, 1))
  318. self.sizer.Add(item=self.searchb,
  319. flag=wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL,
  320. border=5, pos=(2, 2))
  321. self.sizer.AddGrowableRow(3)
  322. self.sizer.Add(item=self.projlist,
  323. flag=wx.EXPAND |
  324. wx.ALIGN_LEFT |
  325. wx.ALL, border=5, pos=(3, 1), span=(1, 3))
  326. # events
  327. self.tproj.Bind(wx.EVT_TEXT, self.OnText)
  328. self.tproj.Bind(wx.EVT_TEXT_ENTER, self.OnText)
  329. self.searchb.Bind(wx.EVT_TEXT_ENTER, self.OnSearch)
  330. self.projlist.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
  331. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  332. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  333. # do layout
  334. # self.Layout()
  335. def OnPageChanging(self,event):
  336. if event.GetDirection() and self.proj not in self.parent.projections:
  337. event.Veto()
  338. if self.proj == 'utm':
  339. self.parent.projtypepage.text_utm.SetEditable(True)
  340. self.parent.projtypepage.hemischoices = ['north','south']
  341. else:
  342. self.parent.projtypepage.text_utm.SetValue('')
  343. self.parent.projtypepage.text_utm.SetEditable(False)
  344. self.parent.projtypepage.hemischoices = []
  345. def OnText(self, event):
  346. """Projection name changed"""
  347. self.proj = event.GetString()
  348. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  349. if len(self.proj) == 0 and nextButton.IsEnabled():
  350. nextButton.Enable(False)
  351. if self.proj in self.parent.projections:
  352. self.projdesc = self.parent.projections[self.proj]
  353. if not nextButton.IsEnabled():
  354. nextButton.Enable()
  355. def OnEnterPage(self, event):
  356. if len(self.proj) == 0:
  357. # disable 'next' button by default
  358. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  359. else:
  360. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  361. event.Skip()
  362. def OnSearch(self, event):
  363. """Search projection by desc"""
  364. str = event.GetString()
  365. try:
  366. self.proj, self.projdesc = self.projlist.Search(index=1, str=event.GetString())
  367. except:
  368. self.proj = self.projdesc = ''
  369. self.tproj.SetValue(self.proj)
  370. event.Skip()
  371. def OnItemSelected(self, event):
  372. """Projection selected"""
  373. index = event.m_itemIndex
  374. # set values
  375. self.proj = self.projlist.GetItem(index, 0).GetText()
  376. self.projdesc = self.projlist.GetItem(index, 0).GetText()
  377. self.tproj.SetValue(self.proj)
  378. class ItemList(wx.ListCtrl,
  379. listmix.ListCtrlAutoWidthMixin,
  380. listmix.ColumnSorterMixin):
  381. """Generic list (for projections, ellipsoids, etc.)"""
  382. def __init__(self, parent, columns, data=None):
  383. wx.ListCtrl.__init__(self, parent=parent, id=wx.ID_ANY,
  384. style=wx.LC_REPORT |
  385. wx.LC_VIRTUAL |
  386. wx.LC_HRULES |
  387. wx.LC_VRULES |
  388. wx.LC_SINGLE_SEL |
  389. wx.LC_SORT_ASCENDING, size=(550, 125))
  390. # original data or None
  391. self.sourceData = data
  392. #
  393. # insert columns
  394. #
  395. i = 0
  396. for column in columns:
  397. self.InsertColumn(i, column)
  398. i += 1
  399. if self.sourceData:
  400. self.Populate()
  401. else:
  402. for i in range(self.GetColumnCount()):
  403. self.SetColumnWidth(i, wx.LIST_AUTOSIZE_USEHEADER)
  404. #
  405. # listmix
  406. #
  407. listmix.ListCtrlAutoWidthMixin.__init__(self)
  408. listmix.ColumnSorterMixin.__init__(self, self.GetColumnCount())
  409. #
  410. # add some attributes
  411. #
  412. self.attr1 = wx.ListItemAttr()
  413. self.attr1.SetBackgroundColour(wx.Colour(238,238,238))
  414. self.attr2 = wx.ListItemAttr()
  415. self.attr2.SetBackgroundColour("white")
  416. self.il = wx.ImageList(16, 16)
  417. self.sm_up = self.il.Add(wx.ArtProvider_GetBitmap(wx.ART_GO_UP, wx.ART_TOOLBAR,
  418. (16,16)))
  419. self.sm_dn = self.il.Add(wx.ArtProvider_GetBitmap(wx.ART_GO_DOWN, wx.ART_TOOLBAR,
  420. (16,16)))
  421. self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
  422. #
  423. # sort by first column
  424. #
  425. if self.sourceData:
  426. self.SortListItems(col=0, ascending=True)
  427. #
  428. # bindings
  429. #
  430. self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColumnClick)
  431. def Populate(self, data=None, update=False):
  432. """Populate list"""
  433. self.itemDataMap = {}
  434. self.itemIndexMap = []
  435. if data is None:
  436. data = self.sourceData
  437. elif update:
  438. self.sourceData = data
  439. try:
  440. data.sort()
  441. self.DeleteAllItems()
  442. row = 0
  443. for value in data:
  444. # index = self.InsertStringItem(sys.maxint, str(value[0]))
  445. self.itemDataMap[row] = [value[0]]
  446. for i in range(1, len(value)):
  447. # try:
  448. # self.SetStringItem(index, i, str(value[i]))
  449. # except:
  450. # self.SetStringItem(index, i, unicode(str(value[i]), 'latin1'))
  451. self.itemDataMap[row].append(value[i])
  452. # self.SetItemData(index, row)
  453. self.itemIndexMap.append(row)
  454. row += 1
  455. self.SetItemCount(row)
  456. # set column width
  457. # for i in range(self.GetColumnCount()):
  458. # self.SetColumnWidth(i, wx.LIST_AUTOSIZE)
  459. # for i in range(self.GetColumnCount()):
  460. # if self.GetColumnWidth(i) < 80:
  461. # self.SetColumnWidth(i, 80)
  462. self.SetColumnWidth(0, 80)
  463. self.SetColumnWidth(1, 300)
  464. self.SendSizeEvent()
  465. except StandardError, e:
  466. wx.MessageBox(parent=self,
  467. message=_("Unable to read list: %s") % e,
  468. caption=_("Error"), style=wx.OK | wx.ICON_ERROR)
  469. def OnColumnClick(self, event):
  470. """Sort by column"""
  471. self._col = event.GetColumn()
  472. # remove duplicated arrow symbol from column header
  473. # FIXME: should be done automatically
  474. info = wx.ListItem()
  475. info.m_mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE
  476. info.m_image = -1
  477. for column in range(self.GetColumnCount()):
  478. info.m_text = self.GetColumn(column).GetText()
  479. self.SetColumn(column, info)
  480. event.Skip()
  481. def GetSortImages(self):
  482. """Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py"""
  483. return (self.sm_dn, self.sm_up)
  484. def OnGetItemText(self, item, col):
  485. """Get item text"""
  486. index = self.itemIndexMap[item]
  487. s = str(self.itemDataMap[index][col])
  488. return s
  489. def OnGetItemAttr(self, item):
  490. """Get item attributes"""
  491. index = self.itemIndexMap[item]
  492. if ( index % 2) == 0:
  493. return self.attr2
  494. else:
  495. return self.attr1
  496. def SortItems(self, sorter=cmp):
  497. """Sort items"""
  498. items = list(self.itemDataMap.keys())
  499. items.sort(self.Sorter)
  500. self.itemIndexMap = items
  501. # redraw the list
  502. self.Refresh()
  503. def Sorter(self, key1, key2):
  504. colName = self.GetColumn(self._col).GetText()
  505. ascending = self._colSortFlag[self._col]
  506. # convert always string
  507. item1 = self.itemDataMap[key1][self._col]
  508. item2 = self.itemDataMap[key2][self._col]
  509. if type(item1) == type('') or type(item2) == type(''):
  510. cmpVal = locale.strcoll(str(item1), str(item2))
  511. else:
  512. cmpVal = cmp(item1, item2)
  513. # If the items are equal then pick something else to make the sort value unique
  514. if cmpVal == 0:
  515. cmpVal = apply(cmp, self.GetSecondarySortValues(self._col, key1, key2))
  516. if ascending:
  517. return cmpVal
  518. else:
  519. return -cmpVal
  520. def GetListCtrl(self):
  521. """Used by listmix.ColumnSorterMixin"""
  522. return self
  523. def Search (self, index, str):
  524. """Search projection by description
  525. Return first found item or None
  526. """
  527. if str == '':
  528. self.Populate(self.sourceData)
  529. return None
  530. data = []
  531. for i in range(len(self.sourceData)):
  532. value = self.sourceData[i][index]
  533. if str.lower() in value.lower():
  534. data.append(self.sourceData[i])
  535. self.Populate(data)
  536. if len(data) > 0:
  537. return data[0]
  538. else:
  539. return None
  540. class ProjTypePage(TitledPage):
  541. """
  542. Wizard page for selecting method of setting coordinate system parameters
  543. (select coordinate system option)
  544. """
  545. def __init__(self, wizard, parent):
  546. TitledPage.__init__(self, wizard, _("Choose method of specifying georeferencing parameters"))
  547. global coordsys
  548. self.utmzone = ''
  549. self.utmhemisphere = ''
  550. self.hemischoices = ["north","south"]
  551. self.parent = parent
  552. self.radio1 = wx.RadioButton(parent=self, id=wx.ID_ANY, label=_("Select datum with associated ellipsoid"),
  553. style = wx.RB_GROUP)
  554. self.radio2 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  555. label=_("Select ellipsoid"))
  556. self.title_utm = self.MakeLabel(_("Set zone for UTM projection:"))
  557. self.text_utm = self.MakeTextCtrl(size=(100,-1))
  558. self.label_utm = self.MakeLabel(_("Zone:"))
  559. self.hemisphere = wx.Choice(parent=self, id=wx.ID_ANY, size=(100, -1),
  560. choices=self.hemischoices)
  561. self.label_hemisphere = self.MakeLabel(_("Hemisphere for zone:"))
  562. # layout
  563. self.sizer.AddGrowableCol(2)
  564. self.sizer.Add(item=self.radio1,
  565. flag=wx.ALIGN_LEFT, pos=(1, 1), span=(1, 2))
  566. self.sizer.Add(item=self.radio2,
  567. flag=wx.ALIGN_LEFT, pos=(2, 1), span=(1, 2))
  568. self.sizer.Add(item=self.title_utm,
  569. flag=wx.ALIGN_LEFT | wx.ALL,
  570. border=5, pos=(4, 1), span=(1, 2))
  571. self.sizer.Add(item=self.label_utm,
  572. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  573. border=5, pos=(5, 1))
  574. self.sizer.Add(item=self.text_utm,
  575. flag=wx.ALIGN_LEFT | wx.ALL, border=5,
  576. pos=(5, 2))
  577. self.sizer.Add(item=self.label_hemisphere,
  578. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  579. border=5, pos=(6, 1))
  580. self.sizer.Add(item=self.hemisphere,
  581. flag=wx.ALIGN_LEFT|wx.ALL,
  582. border=5, pos=(6, 2))
  583. self.title_utm.Hide()
  584. self.text_utm.Hide()
  585. self.label_utm.Hide()
  586. self.hemisphere.Hide()
  587. self.label_hemisphere.Hide()
  588. # bindings
  589. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio1.GetId())
  590. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio2.GetId())
  591. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChange)
  592. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  593. # do page layout
  594. # self.DoLayout()
  595. def OnPageChange(self,event=None):
  596. if event.GetDirection() and self.parent.projpage.proj == 'utm' and self.utmzone == '':
  597. wx.MessageBox('You must set a zone for a UTM projection')
  598. event.Veto()
  599. self.title_utm.Hide()
  600. self.text_utm.Hide()
  601. self.label_utm.Hide()
  602. self.hemisphere.Hide()
  603. self.label_hemisphere.Hide()
  604. def OnEnterPage(self,event):
  605. if self.parent.projpage.proj == 'utm':
  606. self.title_utm.Show()
  607. self.text_utm.Show()
  608. self.label_utm.Show()
  609. self.hemisphere.Show()
  610. self.label_hemisphere.Show()
  611. self.Bind(wx.EVT_CHOICE, self.OnHemisphere, self.hemisphere)
  612. self.Bind(wx.EVT_TEXT, self.GetUTM, self.text_utm)
  613. if not wx.FindWindowById(wx.ID_FORWARD).IsEnabled():
  614. wx.FindWindowById(wx.ID_FORWARD).Enable()
  615. event.Skip()
  616. def SetVal(self, event):
  617. global coordsys
  618. if event.GetId() == self.radio1.GetId():
  619. self.SetNext(self.parent.datumpage)
  620. self.parent.sumpage.SetPrev(self.parent.datumpage)
  621. elif event.GetId() == self.radio2.GetId():
  622. self.SetNext(self.parent.ellipsepage)
  623. self.parent.sumpage.SetPrev(self.parent.ellipsepage)
  624. def GetUTM(self, event):
  625. self.utmzone = event.GetString()
  626. def OnHemisphere(self, event):
  627. self.utmhemisphere = event.GetString()
  628. class DatumPage(TitledPage):
  629. """
  630. Wizard page for selecting datum (with associated ellipsoid)
  631. and datum transformation parameters (select coordinate system option)
  632. """
  633. def __init__(self, wizard, parent):
  634. TitledPage.__init__(self, wizard, _("Specify geodetic datum"))
  635. self.parent = parent
  636. self.datum = ''
  637. self.datumdesc = ''
  638. self.ellipsoid = ''
  639. self.datumparams = ''
  640. self.transform = ''
  641. self.transregion = ''
  642. self.transparams = ''
  643. self.hastransform = False
  644. self.proj4params = ''
  645. # text input
  646. self.tdatum = self.MakeTextCtrl("", size=(200,-1))
  647. self.ttrans = self.MakeTextCtrl("", size=(200,-1))
  648. # search box
  649. self.searchb = wx.SearchCtrl(self, size=(200,-1),
  650. style=wx.TE_PROCESS_ENTER)
  651. # create list control for datum/elipsoid list
  652. data = []
  653. for key in self.parent.datums.keys():
  654. data.append([key, self.parent.datums[key][0], self.parent.datums[key][1]])
  655. self.datumlist = ItemList(self,
  656. data=data,
  657. columns=[_('Code'), _('Description'), _('Ellipsoid')])
  658. # create list control for datum transformation parameters list
  659. data = []
  660. for key in self.parent.transforms.keys():
  661. data.append([key, self.parent.transforms[key][0], self.parent.transforms[key][1]])
  662. self.transformlist = ItemList(self,
  663. data=None,
  664. columns=[_('Code'), _('Datum'), _('Description')])
  665. self.transformlist.sourceData = data
  666. # layout
  667. self.sizer.AddGrowableCol(4)
  668. self.sizer.Add(item=self.MakeLabel(_("Datum code:")),
  669. flag=wx.ALIGN_LEFT |
  670. wx.ALIGN_CENTER_VERTICAL |
  671. wx.ALL, border=5, pos=(1, 1))
  672. self.sizer.Add(item=self.tdatum,
  673. flag=wx.ALIGN_LEFT |
  674. wx.ALIGN_CENTER_VERTICAL |
  675. wx.ALL, border=5, pos=(1, 2))
  676. self.sizer.Add(item=self.MakeLabel(_("Search in description:")),
  677. flag=wx.ALIGN_LEFT |
  678. wx.ALIGN_CENTER_VERTICAL |
  679. wx.ALL, border=5, pos=(2, 1))
  680. self.sizer.Add(item=self.searchb,
  681. flag=wx.ALIGN_LEFT |
  682. wx.ALIGN_CENTER_VERTICAL |
  683. wx.ALL, border=5, pos=(2, 2))
  684. self.sizer.AddGrowableRow(3)
  685. self.sizer.Add(item=self.datumlist,
  686. flag=wx.EXPAND |
  687. wx.ALIGN_LEFT |
  688. wx.ALL, border=5, pos=(3, 1), span=(1, 4))
  689. self.sizer.Add(item=self.MakeLabel(_("Transformation parameters:")),
  690. flag=wx.ALIGN_RIGHT |
  691. wx.ALIGN_CENTER_VERTICAL |
  692. wx.ALL, border=5, pos=(5, 1))
  693. self.sizer.Add(item=self.ttrans,
  694. flag=wx.ALIGN_LEFT |
  695. wx.ALIGN_CENTER_VERTICAL |
  696. wx.ALL, border=5, pos=(5, 2))
  697. self.sizer.Add(item=self.transformlist,
  698. flag=wx.EXPAND |
  699. wx.ALIGN_LEFT |
  700. wx.ALL, border=5, pos=(6, 1), span=(1, 4))
  701. # events
  702. self.datumlist.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnDatumSelected)
  703. self.transformlist.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnTransformSelected)
  704. self.searchb.Bind(wx.EVT_TEXT_ENTER, self.OnDSearch)
  705. self.tdatum.Bind(wx.EVT_TEXT, self.OnDText)
  706. self.tdatum.Bind(wx.EVT_TEXT_ENTER, self.OnDText)
  707. self.ttrans.Bind(wx.EVT_TEXT, self.OnTText)
  708. self.ttrans.Bind(wx.EVT_TEXT_ENTER, self.OnTText)
  709. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  710. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  711. # do page layout
  712. # self.DoLayout()
  713. def OnPageChanging(self, event):
  714. self.proj4params = ''
  715. if event.GetDirection() and self.datum not in self.parent.datums:
  716. event.Veto()
  717. if self.hastransform == True and self.transform == '':
  718. event.Veto()
  719. self.GetNext().SetPrev(self)
  720. self.parent.ellipsepage.ellipseparams = self.parent.ellipsoids[self.ellipsoid][1]
  721. def OnEnterPage(self,event):
  722. if len(self.datum) == 0 or \
  723. (self.hastransform == True and self.transform == ''):
  724. # disable 'next' button by default
  725. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  726. else:
  727. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  728. event.Skip()
  729. def OnDText(self, event):
  730. self.datum = event.GetString()
  731. self.transformlist.Search(index=1, str=self.datum)
  732. if self.transformlist.GetItemCount() > 0:
  733. self.hastransform = True
  734. else:
  735. self.hastransform = False
  736. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  737. if len(self.datum) == 0 and nextButton.IsEnabled():
  738. nextButton.Enable(False)
  739. elif self.datum in self.parent.datums:
  740. self.datumdesc = self.parent.datums[self.datum][0]
  741. self.ellipsoid = self.parent.datums[self.datum][1]
  742. self.datumparams = self.parent.datums[self.datum][2]
  743. if self.hastransform == False or \
  744. (self.hastransform == True and self.transform != ''):
  745. if not nextButton.IsEnabled():
  746. nextButton.Enable(True)
  747. else:
  748. if nextButton.IsEnabled():
  749. nextButton.Enable(False)
  750. event.Skip()
  751. def OnTText(self, event):
  752. if self.hastransform == False:
  753. event.Skip()
  754. return
  755. self.transform = event.GetString()
  756. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  757. if len(self.transform) == 0 and nextButton.IsEnabled():
  758. nextButton.Enable(False)
  759. elif self.transform in self.parent.transforms:
  760. self.transdatum = self.parent.transforms[self.transform][0]
  761. self.transregion = self.parent.transforms[self.transform][1]
  762. self.transparams = self.parent.transforms[self.transform][2]
  763. if not nextButton.IsEnabled():
  764. nextButton.Enable()
  765. def OnDSearch(self, event):
  766. str = self.searchb.GetValue()
  767. try:
  768. self.datum, self.datumdesc, self.ellipsoid = self.datumlist.Search(index=1, str=str)
  769. self.transformlist.Search(index=1, str=self.datum)
  770. except:
  771. self.datum = self.datumdesc = self.ellipsoid = ''
  772. if str == '' or self.datum == '':
  773. self.transformlist.DeleteAllItems()
  774. self.transformlist.Refresh()
  775. self.tdatum.SetValue(self.datum)
  776. event.Skip()
  777. def OnTransformSelected(self,event):
  778. index = event.m_itemIndex
  779. item = event.GetItem()
  780. self.transform = self.transformlist.GetItem(index, 0).GetText()
  781. self.transdatum = self.parent.transforms[self.transform][0]
  782. self.transregion = self.parent.transforms[self.transform][1]
  783. self.transparams = self.parent.transforms[self.transform][2]
  784. self.ttrans.SetValue(str(self.transform))
  785. def OnDatumSelected(self,event):
  786. index = event.m_itemIndex
  787. item = event.GetItem()
  788. self.datum = self.datumlist.GetItem(index, 0).GetText()
  789. self.datumdesc = self.parent.datums[self.datum][0]
  790. self.ellipsoid = self.parent.datums[self.datum][1]
  791. self.datumparams = self.parent.datums[self.datum][2]
  792. self.tdatum.SetValue(self.datum)
  793. event.Skip()
  794. class EllipsePage(TitledPage):
  795. """
  796. Wizard page for selecting ellipsoid (select coordinate system option)
  797. """
  798. def __init__(self, wizard, parent):
  799. TitledPage.__init__(self, wizard, _("Specify ellipsoid"))
  800. self.parent = parent
  801. self.ellipse = ''
  802. self.ellipsedesc = ''
  803. self.ellipseparams = ''
  804. self.proj4params = ''
  805. # text input
  806. self.tellipse = self.MakeTextCtrl("", size=(200,-1))
  807. # search box
  808. self.searchb = wx.SearchCtrl(self, size=(200,-1),
  809. style=wx.TE_PROCESS_ENTER)
  810. # create list control for ellipse list
  811. data = []
  812. # extract code, desc
  813. for key in self.parent.ellipsoids.keys():
  814. data.append([key, self.parent.ellipsoids[key][0]])
  815. self.ellipselist = ItemList(self, data=data,
  816. columns=[_('Code'), _('Description')])
  817. # layout
  818. self.sizer.AddGrowableCol(4)
  819. self.sizer.Add(item=self.MakeLabel(_("Ellipsoid code:")),
  820. flag=wx.ALIGN_RIGHT |
  821. wx.ALIGN_CENTER_VERTICAL |
  822. wx.ALL, border=5, pos=(1, 1))
  823. self.sizer.Add(item=self.tellipse,
  824. flag=wx.ALIGN_LEFT |
  825. wx.ALIGN_CENTER_VERTICAL |
  826. wx.ALL, border=5, pos=(1, 2))
  827. self.sizer.Add(item=self.MakeLabel(_("Search in description:")),
  828. flag=wx.ALIGN_RIGHT |
  829. wx.ALIGN_CENTER_VERTICAL |
  830. wx.ALL, border=5, pos=(2, 1))
  831. self.sizer.Add(item=self.searchb,
  832. flag=wx.ALIGN_LEFT |
  833. wx.ALIGN_CENTER_VERTICAL |
  834. wx.ALL, border=5, pos=(2, 2))
  835. self.sizer.AddGrowableRow(3)
  836. self.sizer.Add(item=self.ellipselist,
  837. flag=wx.EXPAND |
  838. wx.ALIGN_LEFT |
  839. wx.ALL, border=5, pos=(3, 1), span=(1, 4))
  840. # events
  841. self.ellipselist.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
  842. self.tellipse.Bind(wx.EVT_TEXT, self.OnText)
  843. self.tellipse.Bind(wx.EVT_TEXT_ENTER, self.OnText)
  844. self.searchb.Bind(wx.EVT_TEXT_ENTER, self.OnSearch)
  845. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  846. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  847. # do page layout
  848. # self.DoLayout()
  849. def OnEnterPage(self,event):
  850. if len(self.ellipse) == 0:
  851. # disable 'next' button by default
  852. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  853. else:
  854. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  855. event.Skip()
  856. def OnPageChanging(self, event):
  857. if event.GetDirection() and self.ellipse not in self.parent.ellipsoids:
  858. event.Veto()
  859. self.proj4params = ''
  860. self.GetNext().SetPrev(self)
  861. self.parent.datumpage.datumparams = ''
  862. self.parent.datumpage.transparams = ''
  863. # self.GetNext().SetPrev(self) (???)
  864. def OnText(self, event):
  865. """Ellipspoid code changed"""
  866. self.ellipse = event.GetString()
  867. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  868. if len(self.ellipse) == 0 and nextButton.IsEnabled():
  869. nextButton.Enable(False)
  870. self.ellipsedesc = ''
  871. self.ellipseparams = ''
  872. self.proj4params = ''
  873. elif self.ellipse in self.parent.ellipsoids:
  874. self.ellipsedesc = self.parent.ellipsoids[self.ellipse][0]
  875. self.ellipseparams = self.parent.ellipsoids[self.ellipse][1]
  876. if not nextButton.IsEnabled():
  877. nextButton.Enable()
  878. def OnSearch(self, event):
  879. """Search ellipsoid by desc"""
  880. str = event.GetString()
  881. try:
  882. self.ellipse, self.ellipsedesc = \
  883. self.ellipselist.Search(index=1, str=event.GetString())
  884. self.ellipseparams = self.parent.ellipsoids[self.ellipse][1]
  885. self.proj4params = self.parent.ellipsoids[self.ellipse][2]
  886. except:
  887. self.ellipse = self.ellipsedesc = ''
  888. self.ellipseparams = self.proj4params = ''
  889. self.tellipse.SetValue(self.ellipse)
  890. event.Skip()
  891. def OnItemSelected(self,event):
  892. index = event.m_itemIndex
  893. item = event.GetItem()
  894. self.ellipse = self.ellipselist.GetItem(index, 0).GetText()
  895. self.ellipsedesc = self.parent.ellipsoids[self.ellipse][0]
  896. self.ellipseparams = self.parent.ellipsoids[self.ellipse][1]
  897. self.tellipse.SetValue(self.ellipse)
  898. class GeoreferencedFilePage(TitledPage):
  899. """
  900. Wizard page for selecting georeferenced file to use
  901. for setting coordinate system parameters
  902. """
  903. def __init__(self, wizard, parent):
  904. TitledPage.__init__(self, wizard, _("Select georeferenced file"))
  905. self.georeffile = ''
  906. # create controls
  907. self.lfile= self.MakeLabel(_("Georeferenced file:"))
  908. self.tfile = self.MakeTextCtrl(size=(300,-1))
  909. self.bbrowse = self.MakeButton(_("Browse"))
  910. # do layout
  911. self.sizer.AddGrowableCol(3)
  912. self.sizer.Add(item=self.lfile, flag=wx.ALIGN_LEFT |
  913. wx.ALIGN_CENTRE_VERTICAL |
  914. wx.ALL, border=5, pos=(1, 1))
  915. self.sizer.Add(item=self.tfile, flag=wx.ALIGN_LEFT |
  916. wx.ALIGN_CENTRE_VERTICAL |
  917. wx.ALL, border=5, pos=(1, 2))
  918. self.sizer.Add(item=self.bbrowse, flag=wx.ALIGN_LEFT |
  919. wx.ALL, border=5, pos=(1, 3))
  920. self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
  921. self.tfile.Bind(wx.EVT_TEXT, self.OnText)
  922. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  923. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  924. # do page layout
  925. # self.DoLayout()
  926. def OnEnterPage(self, event):
  927. if len(self.georeffile) == 0:
  928. # disable 'next' button by default
  929. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  930. else:
  931. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  932. event.Skip()
  933. def OnPageChanging(self, event):
  934. if event.GetDirection() and self.georeffile == '':
  935. event.Veto()
  936. self.GetNext().SetPrev(self)
  937. event.Skip()
  938. def OnText(self, event):
  939. self.georeffile = event.GetString()
  940. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  941. if len(self.georeffile) > 0 and os.path.isfile(self.georeffile):
  942. if not nextButton.IsEnabled():
  943. nextButton.Enable(True)
  944. else:
  945. if nextButton.IsEnabled():
  946. nextButton.Enable(False)
  947. event.Skip()
  948. def OnBrowse(self, event):
  949. """Choose file"""
  950. dlg = wx.FileDialog(self,
  951. _("Select georeferenced file"),
  952. os.getcwd(), "", "*.*", wx.OPEN)
  953. if dlg.ShowModal() == wx.ID_OK:
  954. path = dlg.GetPath()
  955. self.tfile.SetValue(path)
  956. dlg.Destroy()
  957. event.Skip()
  958. def OnCreate(self, event):
  959. pass
  960. class WKTPage(TitledPage):
  961. """
  962. Wizard page for selecting WKT file to use
  963. for setting coordinate system parameters
  964. """
  965. def __init__(self, wizard, parent):
  966. TitledPage.__init__(self, wizard, _("Select WKT file"))
  967. self.wktfile = ''
  968. # create controls
  969. self.lfile= self.MakeLabel(_("WKT file:"))
  970. self.tfile = self.MakeTextCtrl(size=(300,-1))
  971. self.bbrowse = self.MakeButton(_("Browse"))
  972. # do layout
  973. self.sizer.AddGrowableCol(3)
  974. self.sizer.Add(item=self.lfile, flag=wx.ALIGN_LEFT |
  975. wx.ALIGN_CENTRE_VERTICAL |
  976. wx.ALL, border=5, pos=(1, 1))
  977. self.sizer.Add(item=self.tfile, flag=wx.ALIGN_LEFT |
  978. wx.ALIGN_CENTRE_VERTICAL |
  979. wx.ALL, border=5, pos=(1, 2))
  980. self.sizer.Add(item=self.bbrowse, flag=wx.ALIGN_LEFT |
  981. wx.ALL, border=5, pos=(1, 3))
  982. self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
  983. self.tfile.Bind(wx.EVT_TEXT, self.OnText)
  984. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  985. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  986. # do page layout
  987. # self.DoLayout()
  988. def OnEnterPage(self, event):
  989. if len(self.wktfile) == 0:
  990. # disable 'next' button by default
  991. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  992. else:
  993. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  994. event.Skip()
  995. def OnPageChanging(self, event):
  996. if event.GetDirection() and self.wktfile == '':
  997. event.Veto()
  998. self.GetNext().SetPrev(self)
  999. event.Skip()
  1000. def OnText(self, event):
  1001. self.wktfile = event.GetString()
  1002. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  1003. if len(self.wktfile) > 0 and os.path.isfile(self.wktfile):
  1004. if not nextButton.IsEnabled():
  1005. nextButton.Enable(True)
  1006. else:
  1007. if nextButton.IsEnabled():
  1008. nextButton.Enable(False)
  1009. event.Skip()
  1010. def OnBrowse(self, event):
  1011. """Choose file"""
  1012. dlg = wx.FileDialog(self,
  1013. _("Select WKT file"),
  1014. os.getcwd(), "", "*.*", wx.OPEN)
  1015. if dlg.ShowModal() == wx.ID_OK:
  1016. path = dlg.GetPath()
  1017. self.tfile.SetValue(path)
  1018. dlg.Destroy()
  1019. event.Skip()
  1020. def OnCreate(self, event):
  1021. pass
  1022. class EPSGPage(TitledPage):
  1023. """
  1024. Wizard page for selecting EPSG code for
  1025. setting coordinate system parameters
  1026. """
  1027. def __init__(self, wizard, parent):
  1028. TitledPage.__init__(self, wizard, _("Choose EPSG Code"))
  1029. self.parent = parent
  1030. self.epsgCodeDict = {}
  1031. self.epsgcode = None
  1032. self.epsgdesc = ''
  1033. self.epsgparams = ''
  1034. # labels
  1035. self.lfile= self.MakeLabel(_("Path to the EPSG-codes file:"),
  1036. style=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
  1037. self.lcode= self.MakeLabel(_("EPSG code:"),
  1038. style=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
  1039. self.lsearch = self.MakeLabel(_("Search in description:"),
  1040. style=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
  1041. # text input
  1042. epsgdir = utils.PathJoin(os.environ["GRASS_PROJSHARE"], 'epsg')
  1043. self.tfile = self.MakeTextCtrl(text=epsgdir, size=(200,-1))
  1044. self.tcode = self.MakeTextCtrl(size=(200,-1))
  1045. # buttons
  1046. self.bbrowse = self.MakeButton(_("Browse"))
  1047. self.bbcodes = self.MakeButton(_("Browse EPSG Codes"))
  1048. # search box
  1049. self.searchb = wx.SearchCtrl(self, size=(200,-1),
  1050. style=wx.TE_PROCESS_ENTER)
  1051. self.epsglist = ItemList(self, data=None,
  1052. columns=[_('Code'), _('Description'), _('Parameters')])
  1053. # layout
  1054. self.sizer.AddGrowableCol(3)
  1055. self.sizer.Add(item=self.lfile,
  1056. flag=wx.ALIGN_LEFT |
  1057. wx.ALIGN_CENTER_VERTICAL |
  1058. wx.ALL, border=5, pos=(1, 1))
  1059. self.sizer.Add(item=self.tfile,
  1060. flag=wx.ALIGN_LEFT |
  1061. wx.ALIGN_CENTER_VERTICAL |
  1062. wx.ALL, border=5, pos=(1, 2))
  1063. self.sizer.Add(item=self.bbrowse,
  1064. flag=wx.ALIGN_LEFT |
  1065. wx.ALIGN_CENTER_VERTICAL |
  1066. wx.ALL, border=5, pos=(1, 3))
  1067. self.sizer.Add(item=self.lcode,
  1068. flag=wx.ALIGN_LEFT |
  1069. wx.ALIGN_CENTER_VERTICAL |
  1070. wx.ALL, border=5, pos=(2, 1))
  1071. self.sizer.Add(item=self.tcode,
  1072. flag=wx.ALIGN_LEFT |
  1073. wx.ALIGN_CENTER_VERTICAL |
  1074. wx.ALL, border=5, pos=(2, 2))
  1075. self.sizer.Add(item=self.lsearch,
  1076. flag=wx.ALIGN_LEFT |
  1077. wx.ALIGN_CENTER_VERTICAL |
  1078. wx.ALL, border=5, pos=(3, 1))
  1079. self.sizer.Add(item=self.searchb,
  1080. flag=wx.ALIGN_LEFT |
  1081. wx.ALIGN_CENTER_VERTICAL |
  1082. wx.ALL, border=5, pos=(3, 2))
  1083. self.sizer.Add(item=self.bbcodes,
  1084. flag=wx.ALIGN_LEFT |
  1085. wx.ALIGN_CENTER_VERTICAL |
  1086. wx.ALL, border=5, pos=(3, 3))
  1087. self.sizer.AddGrowableRow(4)
  1088. self.sizer.Add(item=self.epsglist,
  1089. flag=wx.ALIGN_LEFT | wx.EXPAND, pos=(4, 1),
  1090. span=(1, 3))
  1091. # events
  1092. self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
  1093. self.bbcodes.Bind(wx.EVT_BUTTON, self.OnBrowseCodes)
  1094. self.tcode.Bind(wx.EVT_TEXT, self.OnText)
  1095. self.tcode.Bind(wx.EVT_TEXT_ENTER, self.OnText)
  1096. self.epsglist.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
  1097. self.searchb.Bind(wx.EVT_TEXT_ENTER, self.OnSearch)
  1098. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  1099. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1100. # do page layout
  1101. # self.DoLayout()
  1102. def OnEnterPage(self, event):
  1103. if not self.epsgcode:
  1104. # disable 'next' button by default
  1105. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1106. else:
  1107. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1108. # load default epsg database file
  1109. self.OnBrowseCodes(None)
  1110. event.Skip()
  1111. def OnPageChanging(self, event):
  1112. if event.GetDirection() and not self.epsgcode:
  1113. event.Veto()
  1114. self.GetNext().SetPrev(self)
  1115. def OnText(self, event):
  1116. self.epsgcode = event.GetString()
  1117. try:
  1118. self.epsgcode = int(self.epsgcode)
  1119. except:
  1120. self.epsgcode = None
  1121. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  1122. if self.epsgcode and self.epsgcode in self.epsgCodeDict.keys():
  1123. self.epsgdesc = self.epsgCodeDict[self.epsgcode][0]
  1124. self.epsgparams = self.epsgCodeDict[self.epsgcode][1]
  1125. if not nextButton.IsEnabled():
  1126. nextButton.Enable(True)
  1127. else:
  1128. if nextButton.IsEnabled():
  1129. nextButton.Enable(False)
  1130. self.epsgdesc = self.epsgparams = ''
  1131. def OnSearch(self, event):
  1132. str = self.searchb.GetValue()
  1133. if self.epsglist.GetItemCount() == 0:
  1134. event.Skip()
  1135. return
  1136. try:
  1137. self.epsgcode = self.epsglist.Search(index=1, str=str)[0]
  1138. self.tcode.SetValue(str(self.epsgcode))
  1139. except:
  1140. self.epsgcode = None
  1141. self.tcode.SetValue('')
  1142. event.Skip()
  1143. def OnBrowse(self, event):
  1144. """Define path for EPSG code file"""
  1145. path = os.path.dirname(self.tfile.GetValue())
  1146. if not path:
  1147. path = os.getcwd()
  1148. dlg = wx.FileDialog(parent=self, message=_("Choose EPSG codes file"),
  1149. defaultDir=path, defaultFile="", wildcard="*", style=wx.OPEN)
  1150. if dlg.ShowModal() == wx.ID_OK:
  1151. path = dlg.GetPath()
  1152. self.tfile.SetValue(path)
  1153. self.OnBrowseCodes(None)
  1154. dlg.Destroy()
  1155. event.Skip()
  1156. def OnItemSelected(self, event):
  1157. """EPSG code selected from the list"""
  1158. index = event.m_itemIndex
  1159. item = event.GetItem()
  1160. self.epsgcode = int(self.epsglist.GetItem(index, 0).GetText())
  1161. self.epsgdesc = self.epsglist.GetItem(index, 1).GetText()
  1162. self.tcode.SetValue(str(self.epsgcode))
  1163. event.Skip()
  1164. def OnBrowseCodes(self, event, search=None):
  1165. """Browse EPSG codes"""
  1166. # try:
  1167. if True:
  1168. data = []
  1169. self.epsgCodeDict = {}
  1170. f = open(self.tfile.GetValue(), "r")
  1171. i = 0
  1172. code = None
  1173. for line in f.readlines():
  1174. line = line.strip()
  1175. if len(line) < 1:
  1176. continue
  1177. if line[0] == '#':
  1178. descr = line[1:].strip()
  1179. elif line[0] == '<':
  1180. code, params = line.split(" ", 1)
  1181. code = int(code.replace('<', '').replace('>', ''))
  1182. if code is not None:
  1183. data.append((code, descr, params))
  1184. self.epsgCodeDict[code] = (descr, params)
  1185. code = None
  1186. i += 1
  1187. f.close()
  1188. self.epsglist.Populate(data, update=True)
  1189. # except StandardError, e:
  1190. # wx.MessageBox(parent=self,
  1191. # message=_("Unable to read EPGS codes: %s") % e,
  1192. # caption=_("Error"), style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  1193. # self.epsglist.Populate([], update=True)
  1194. class CustomPage(TitledPage):
  1195. """
  1196. Wizard page for entering custom PROJ.4 string
  1197. for setting coordinate system parameters
  1198. """
  1199. def __init__(self, wizard, parent):
  1200. TitledPage.__init__(self, wizard,
  1201. _("Choose method of specifying georeferencing parameters"))
  1202. global coordsys
  1203. self.customstring = ''
  1204. self.parent = parent
  1205. # widgets
  1206. self.text_proj4string = self.MakeTextCtrl(size=(400, 200),
  1207. style=wx.TE_MULTILINE)
  1208. self.label_proj4string = self.MakeLabel(_("Enter PROJ.4 parameters string:"))
  1209. # layout
  1210. self.sizer.AddGrowableCol(2)
  1211. self.sizer.Add(self.label_proj4string,
  1212. flag=wx.ALIGN_LEFT | wx.ALL,
  1213. border=5, pos=(1, 1))
  1214. self.sizer.AddGrowableRow(2)
  1215. self.sizer.Add(self.text_proj4string,
  1216. flag=wx.ALIGN_LEFT | wx.ALL | wx.EXPAND,
  1217. border=5, pos=(2, 1), span=(1, 2))
  1218. self.text_proj4string.Bind(wx.EVT_TEXT, self.GetProjstring)
  1219. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  1220. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1221. # do page layout
  1222. # self.DoLayout()
  1223. def OnEnterPage(self, event):
  1224. if len(self.customstring) == 0:
  1225. # disable 'next' button by default
  1226. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1227. else:
  1228. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1229. event.Skip()
  1230. def OnPageChanging(self, event):
  1231. if event.GetDirection() and not self.customstring:
  1232. event.Veto()
  1233. self.GetNext().SetPrev(self)
  1234. def GetProjstring(self, event):
  1235. """Change proj string"""
  1236. # TODO: check PROJ.4 syntax
  1237. self.customstring = event.GetString()
  1238. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  1239. if len(self.customstring) == 0:
  1240. if nextButton.IsEnabled():
  1241. nextButton.Enable(False)
  1242. else:
  1243. if not nextButton.IsEnabled():
  1244. nextButton.Enable()
  1245. class SummaryPage(TitledPage):
  1246. """
  1247. Shows summary result of choosing coordinate system parameters
  1248. prior to creating location
  1249. """
  1250. def __init__(self, wizard, parent):
  1251. TitledPage.__init__(self, wizard, _("Summary"))
  1252. self.parent = parent
  1253. # labels
  1254. self.ldatabase = self.MakeLabel("")
  1255. self.llocation = self.MakeLabel("")
  1256. self.lprojection = self.MakeLabel("")
  1257. self.lproj4string = self.MakeLabel("")
  1258. self.lproj4stringLabel = self.MakeLabel("")
  1259. self.lprojection.Wrap(400)
  1260. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1261. # self.Bind(wx.EVT_BUTTON, self.OnFinish, wx.ID_FINISH)
  1262. # do sub-page layout
  1263. self.__DoLayout()
  1264. def __DoLayout(self):
  1265. """Do page layout"""
  1266. self.sizer.AddGrowableCol(1)
  1267. self.sizer.Add(item=self.MakeLabel(_("GRASS Database:")),
  1268. flag=wx.ALIGN_LEFT | wx.ALL,
  1269. border=5, pos=(1, 0))
  1270. self.sizer.Add(item=self.ldatabase,
  1271. flag=wx.ALIGN_LEFT | wx.ALL,
  1272. border=5, pos=(1, 1))
  1273. self.sizer.Add(item=self.MakeLabel(_("Location Name:")),
  1274. flag=wx.ALIGN_LEFT | wx.ALL,
  1275. border=5, pos=(2, 0))
  1276. self.sizer.Add(item=self.llocation,
  1277. flag=wx.ALIGN_LEFT | wx.ALL,
  1278. border=5, pos=(2, 1))
  1279. self.sizer.Add(item=self.MakeLabel(_("Projection:")),
  1280. flag=wx.ALIGN_LEFT | wx.ALL,
  1281. border=5, pos=(3, 0))
  1282. self.sizer.Add(item=self.lprojection,
  1283. flag=wx.ALIGN_LEFT | wx.ALL,
  1284. border=5, pos=(3, 1))
  1285. self.sizer.Add(item=self.lproj4stringLabel,
  1286. flag=wx.ALIGN_LEFT | wx.ALL,
  1287. border=5, pos=(4, 0))
  1288. self.sizer.Add(item=self.lproj4string,
  1289. flag=wx.ALIGN_LEFT | wx.ALL,
  1290. border=5, pos=(4, 1))
  1291. self.sizer.Add(item=(10,20),
  1292. flag=wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  1293. border=5, pos=(5, 0), span=(1, 2))
  1294. # self.sizer.AddGrowableRow(6)
  1295. #self.sizer.Add(item=self.MakeLabel(_("You can set the default extents "
  1296. # "and resolution after creating new location%s"
  1297. # "or you can set them during a working session.") % os.linesep,
  1298. # style=wx.ALIGN_CENTER),
  1299. # flag=wx.ALIGN_CENTRE | wx.ALL, border=5, pos=(6, 0),
  1300. # span=(1, 2))
  1301. def OnEnterPage(self,event):
  1302. """
  1303. Insert values into text controls for summary of location creation options
  1304. """
  1305. database = self.parent.startpage.grassdatabase
  1306. location = self.parent.startpage.location
  1307. global coordsys
  1308. if coordsys not in ['proj', 'epsg']:
  1309. self.lproj4stringLabel.Hide()
  1310. self.lproj4string.Hide()
  1311. self.lproj4stringLabel.SetLabel('')
  1312. self.lproj4string.SetLabel('')
  1313. else:
  1314. self.lproj4string.Show()
  1315. self.lproj4stringLabel.SetLabel(_("PROJ.4 definition:"))
  1316. if coordsys == 'proj':
  1317. self.lproj4string.SetLabel(self.parent.CreateProj4String())
  1318. else:
  1319. self.lproj4string.SetLabel(self.parent.epsgpage.epsgCodeDict[self.parent.epsgpage.epsgcode][1])
  1320. self.lproj4string.Wrap(400)
  1321. projection = self.parent.projpage.proj
  1322. projdesc = self.parent.projpage.projdesc
  1323. utmzone = self.parent.projtypepage.utmzone
  1324. utmhemisphere = self.parent.projtypepage.utmhemisphere
  1325. ellipse = self.parent.ellipsepage.ellipse
  1326. ellipsedesc = self.parent.ellipsepage.ellipsedesc
  1327. datum = self.parent.datumpage.datum
  1328. datumdesc = self.parent.datumpage.datumdesc
  1329. ellipsoid = self.parent.datumpage.ellipsoid
  1330. datumparams = self.parent.datumpage.datumparams
  1331. transform = self.parent.datumpage.transform
  1332. transregion = self.parent.datumpage.transregion
  1333. transparams = self.parent.datumpage.transparams
  1334. self.ldatabase.SetLabel(str(database))
  1335. self.llocation.SetLabel(str(location))
  1336. label = ''
  1337. if coordsys == 'epsg':
  1338. label = 'EPSG code %s (%s)' % (self.parent.epsgpage.epsgcode, self.parent.epsgpage.epsgdesc)
  1339. self.lprojection.SetLabel(label)
  1340. elif coordsys == 'file':
  1341. label = 'matches file %s' % self.parent.filepage.georeffile
  1342. self.lprojection.SetLabel(label)
  1343. elif coordsys == 'wkt':
  1344. label = 'matches file %s' % self.parent.wktpage.wktfile
  1345. self.lprojection.SetLabel(label)
  1346. elif coordsys == 'proj':
  1347. label = ('%s, %s%s' % (projdesc, datumdesc, ellipsedesc))
  1348. self.lprojection.SetLabel(label)
  1349. elif coordsys == 'xy':
  1350. label = ('XY coordinate system (not projected).')
  1351. self.lprojection.SetLabel(label)
  1352. elif coordsys == 'custom':
  1353. label = ('%s' % self.parent.custompage.customstring)
  1354. self.lprojection.SetLabel(label)
  1355. def OnFinish(self, event):
  1356. dlg = wx.MessageDialog(parent=self.wizard,
  1357. message=_("Do you want to create GRASS location <%s>?") % location,
  1358. caption=_("Create new location?"),
  1359. style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
  1360. if dlg.ShowModal() == wx.ID_NO:
  1361. dlg.Destroy()
  1362. event.Veto()
  1363. else:
  1364. dlg.Destroy()
  1365. event.Skip()
  1366. class LocationWizard(wx.Object):
  1367. """
  1368. Start wizard here and finish wizard here
  1369. """
  1370. def __init__(self, parent, grassdatabase):
  1371. global coordsys
  1372. self.parent = parent
  1373. #
  1374. # define wizard image
  1375. #
  1376. # file = "loc_wizard.png"
  1377. file = "loc_wizard_qgis.png"
  1378. imagePath = os.path.join(globalvar.ETCWXDIR, "images", file)
  1379. wizbmp = wx.Image(imagePath, wx.BITMAP_TYPE_PNG)
  1380. # wizbmp.Rescale(250,600)
  1381. wizbmp = wizbmp.ConvertToBitmap()
  1382. #
  1383. # get georeferencing information from tables in $GISBASE/etc
  1384. #
  1385. self.__readData()
  1386. #
  1387. # define wizard pages
  1388. #
  1389. self.wizard = wiz.Wizard(parent, id=wx.ID_ANY, title=_("Define new GRASS Location"),
  1390. bitmap=wizbmp)
  1391. self.startpage = DatabasePage(self.wizard, self, grassdatabase)
  1392. self.csystemspage = CoordinateSystemPage(self.wizard, self)
  1393. self.projpage = ProjectionsPage(self.wizard, self)
  1394. self.datumpage = DatumPage(self.wizard, self)
  1395. self.projtypepage = ProjTypePage(self.wizard,self)
  1396. self.epsgpage = EPSGPage(self.wizard, self)
  1397. self.filepage = GeoreferencedFilePage(self.wizard, self)
  1398. self.wktpage = WKTPage(self.wizard, self)
  1399. self.ellipsepage = EllipsePage(self.wizard, self)
  1400. self.custompage = CustomPage(self.wizard, self)
  1401. self.sumpage = SummaryPage(self.wizard, self)
  1402. #
  1403. # set the initial order of the pages
  1404. # (should follow the epsg line)
  1405. #
  1406. self.startpage.SetNext(self.csystemspage)
  1407. self.csystemspage.SetPrev(self.startpage)
  1408. self.csystemspage.SetNext(self.sumpage)
  1409. self.projpage.SetPrev(self.csystemspage)
  1410. self.projpage.SetNext(self.projtypepage)
  1411. self.projtypepage.SetPrev(self.projpage)
  1412. self.projtypepage.SetNext(self.datumpage)
  1413. self.datumpage.SetPrev(self.projtypepage)
  1414. self.datumpage.SetNext(self.sumpage)
  1415. self.ellipsepage.SetPrev(self.projtypepage)
  1416. self.ellipsepage.SetNext(self.sumpage)
  1417. self.epsgpage.SetPrev(self.csystemspage)
  1418. self.epsgpage.SetNext(self.sumpage)
  1419. self.filepage.SetPrev(self.csystemspage)
  1420. self.filepage.SetNext(self.sumpage)
  1421. self.wktpage.SetPrev(self.csystemspage)
  1422. self.wktpage.SetNext(self.sumpage)
  1423. self.custompage.SetPrev(self.csystemspage)
  1424. self.custompage.SetNext(self.sumpage)
  1425. self.sumpage.SetPrev(self.csystemspage)
  1426. #
  1427. # do pages layout
  1428. #
  1429. self.startpage.DoLayout()
  1430. self.csystemspage.DoLayout()
  1431. self.projpage.DoLayout()
  1432. self.datumpage.DoLayout()
  1433. self.projtypepage.DoLayout()
  1434. self.epsgpage.DoLayout()
  1435. self.filepage.DoLayout()
  1436. self.wktpage.DoLayout()
  1437. self.ellipsepage.DoLayout()
  1438. self.custompage.DoLayout()
  1439. self.sumpage.DoLayout()
  1440. self.wizard.FitToPage(self.datumpage)
  1441. # new location created?
  1442. self.location = None
  1443. success = False
  1444. #
  1445. # run wizard...
  1446. #
  1447. if self.wizard.RunWizard(self.startpage):
  1448. success = self.OnWizFinished()
  1449. if success == True:
  1450. self.wizard.Destroy()
  1451. self.location = self.startpage.location
  1452. dlg = wx.MessageDialog(parent=self.parent,
  1453. message=_("Do you want to set the default "
  1454. "region extents and resolution now?"),
  1455. caption=_("Location <%s> created") % self.location,
  1456. style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
  1457. dlg.CenterOnScreen()
  1458. if dlg.ShowModal() == wx.ID_YES:
  1459. dlg.Destroy()
  1460. defineRegion = RegionDef(self.parent, location=self.location)
  1461. defineRegion.CenterOnScreen()
  1462. defineRegion.Show()
  1463. else:
  1464. dlg.Destroy()
  1465. elif success == False:
  1466. self.wizard.Destroy()
  1467. wx.MessageBox(parent=self.parent,
  1468. message="%s" % _("Unable to create new location. "
  1469. "Location <%s> not created.") % self.startpage.location,
  1470. caption=_("Location wizard"),
  1471. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  1472. else: # None
  1473. pass
  1474. else:
  1475. win = wx.MessageBox(parent=self.parent,
  1476. message=_("Location wizard canceled. "
  1477. "Location not created."),
  1478. caption=_("Location wizard"))
  1479. def __readData(self):
  1480. """Get georeferencing information from tables in $GISBASE/etc"""
  1481. # read projection definitions
  1482. f = open(os.path.join(globalvar.ETCDIR, "projections"), "r")
  1483. self.projections = {}
  1484. for line in f.readlines():
  1485. line = line.expandtabs(1)
  1486. line = line.strip()
  1487. if line == '' or line[0] == "#":
  1488. continue
  1489. proj, projdesc = line.split(":", 1)
  1490. self.projections[proj.strip()] = projdesc.strip()
  1491. f.close()
  1492. # read datum definitions
  1493. f = open(os.path.join(globalvar.ETCDIR, "datum.table"), "r")
  1494. self.datums = {}
  1495. paramslist = []
  1496. for line in f.readlines():
  1497. line = line.expandtabs(1)
  1498. line = line.strip()
  1499. if line == '' or line[0] == "#":
  1500. continue
  1501. datum, info = line.split(" ", 1)
  1502. info = info.strip()
  1503. datumdesc, params = info.split(" ", 1)
  1504. datumdesc = datumdesc.strip('"')
  1505. paramlist = params.split()
  1506. ellipsoid = paramlist.pop(0)
  1507. self.datums[datum] = (datumdesc.replace('_', ' '), ellipsoid, paramlist)
  1508. f.close()
  1509. # read datum transforms parameters
  1510. f = open(os.path.join(globalvar.ETCDIR, "datumtransform.table"), "r")
  1511. self.transforms = {}
  1512. j = 1
  1513. for line in f.readlines():
  1514. if j < 10:
  1515. transcode = 'T0' + str(j)
  1516. else:
  1517. transcode = 'T' + str(j)
  1518. line = line.expandtabs(1)
  1519. line = line.strip()
  1520. if line == '' or line[0] == "#":
  1521. continue
  1522. datum, rest = line.split(" ", 1)
  1523. rest = rest.strip('" ')
  1524. params, rest = rest.split('"', 1)
  1525. params = params.strip()
  1526. rest = rest.strip('" ')
  1527. try:
  1528. region, info = rest.split('"', 1)
  1529. info = info.strip('" ')
  1530. info = region + ': ' + info
  1531. except:
  1532. info = rest
  1533. self.transforms[transcode] = (datum, info, params)
  1534. j += 1
  1535. f.close()
  1536. # read ellipsiod definitions
  1537. f = open(os.path.join(globalvar.ETCDIR, "ellipse.table"), "r")
  1538. self.ellipsoids = {}
  1539. for line in f.readlines():
  1540. line = line.expandtabs(1)
  1541. line = line.strip()
  1542. if line == '' or line[0] == "#":
  1543. continue
  1544. ellipse, rest = line.split(" ", 1)
  1545. rest = rest.strip('" ')
  1546. desc, params = rest.split('"', 1)
  1547. desc = desc.strip('" ')
  1548. paramslist = params.split()
  1549. self.ellipsoids[ellipse] = (desc, paramslist)
  1550. f.close()
  1551. def OnWizFinished(self):
  1552. database = self.startpage.grassdatabase
  1553. location = self.startpage.location
  1554. global coordsys
  1555. success = False
  1556. # location already exists?
  1557. if os.path.isdir(os.path.join(database,location)):
  1558. dlg = wx.MessageDialog(parent=self.wizard,
  1559. message="%s <%s>: %s" % \
  1560. (_("Unable to create new location"),
  1561. os.path.join(database, location),
  1562. _("Location already exists in GRASS Database.")),
  1563. caption=_("Error"),
  1564. style=wx.OK | wx.ICON_ERROR)
  1565. dlg.ShowModal()
  1566. dlg.Destroy()
  1567. return False
  1568. if coordsys == "xy":
  1569. success = self.XYCreate()
  1570. elif coordsys == "latlong":
  1571. rows = int(round((float(north) - float(south)) / float(resolution)))
  1572. cols = int(round((float(east) - float(west)) / float(resolution)))
  1573. cells = int(rows * cols)
  1574. success = self.LatlongCreate()
  1575. elif coordsys == "proj":
  1576. proj4string = self.CreateProj4String()
  1577. success = self.Proj4Create(proj4string)
  1578. elif coordsys == 'custom':
  1579. success = self.CustomCreate()
  1580. elif coordsys == "epsg":
  1581. success = self.EPSGCreate()
  1582. elif coordsys == "file":
  1583. success = self.FileCreate()
  1584. elif coordsys == "wkt":
  1585. success = self.WKTCreate()
  1586. return success
  1587. def XYCreate(self):
  1588. """Create an XY location"""
  1589. database = self.startpage.grassdatabase
  1590. location = self.startpage.location
  1591. # create location directory and PERMANENT mapset
  1592. try:
  1593. os.mkdir(os.path.join(database, location))
  1594. os.mkdir(os.path.join(database, location, 'PERMANENT'))
  1595. # create DEFAULT_WIND and WIND files
  1596. regioninfo = ['proj: 0',
  1597. 'zone: 0',
  1598. 'north: 1',
  1599. 'south: 0',
  1600. 'east: 1',
  1601. 'west: 0',
  1602. 'cols: 1',
  1603. 'rows: 1',
  1604. 'e-w resol: 1',
  1605. 'n-s resol: 1',
  1606. 'top: 1',
  1607. 'bottom: 0',
  1608. 'cols3: 1',
  1609. 'rows3: 1',
  1610. 'depths: 1',
  1611. 'e-w resol3: 1',
  1612. 'n-s resol3: 1',
  1613. 't-b resol: 1']
  1614. defwind = open(os.path.join(database, location,
  1615. "PERMANENT", "DEFAULT_WIND"), 'w')
  1616. for param in regioninfo:
  1617. defwind.write(param + '%s' % os.linesep)
  1618. defwind.close()
  1619. shutil.copy(os.path.join(database, location, "PERMANENT", "DEFAULT_WIND"),
  1620. os.path.join(database, location, "PERMANENT", "WIND"))
  1621. # create MYNAME file
  1622. myname = open(os.path.join(database, location, "PERMANENT",
  1623. "MYNAME"), 'w')
  1624. myname.write('%s' % os.linesep)
  1625. myname.close()
  1626. return True
  1627. except OSError, e:
  1628. dlg = wx.MessageDialog(parent=self.wizard,
  1629. message="%s: %s" % (_("Unable to create new location"), e),
  1630. caption=_("Error"),
  1631. style=wx.OK | wx.ICON_ERROR)
  1632. dlg.ShowModal()
  1633. dlg.Destroy()
  1634. return False
  1635. def CreateProj4String(self):
  1636. """Constract PROJ.4 string"""
  1637. location = self.startpage.location
  1638. proj = self.projpage.proj
  1639. projdesc = self.projpage.projdesc
  1640. utmzone = self.projtypepage.utmzone
  1641. utmhemisphere = self.projtypepage.utmhemisphere
  1642. datum = self.datumpage.datum
  1643. if self.datumpage.datumdesc:
  1644. datumdesc = self.datumpage.datumdesc +' - ' + self.datumpage.ellipsoid
  1645. else:
  1646. datumdesc = ''
  1647. datumparams = self.datumpage.datumparams
  1648. transparams = self.datumpage.transparams
  1649. ellipse = self.ellipsepage.ellipse
  1650. ellipsedesc = self.ellipsepage.ellipsedesc
  1651. ellipseparams = self.ellipsepage.ellipseparams
  1652. #
  1653. # creating PROJ.4 string
  1654. #
  1655. if proj == 'll':
  1656. proj4string = '+proj=longlat'
  1657. elif proj == 'utm':
  1658. proj4string = '+proj=%s +zone=%s' % (proj, utmzone)
  1659. if utmhemisphere == 'south':
  1660. proj4string += '+south'
  1661. else:
  1662. proj4string = '+proj=%s ' % (proj)
  1663. proj4params = ''
  1664. # set ellipsoid parameters
  1665. for item in ellipseparams:
  1666. if item[:4] == 'f=1/':
  1667. item = '+rf='+item[4:]
  1668. else:
  1669. item = '+'+item
  1670. proj4params = '%s %s' % (proj4params, item)
  1671. # set datum and transform parameters if relevant
  1672. if datumparams:
  1673. for item in datumparams:
  1674. proj4params = '%s +%s' % (proj4params,item)
  1675. if transparams:
  1676. proj4params = '%s +no_defs +%s' % (proj4params,transparams)
  1677. else:
  1678. proj4params = '%s +no_defs' % proj4params
  1679. else:
  1680. proj4params = '%s +no_defs' % proj4params
  1681. return '%s %s' % (proj4string, proj4params)
  1682. def Proj4Create(self, proj4string):
  1683. """
  1684. Create a new location for selected projection
  1685. """
  1686. # creating location from PROJ.4 string passed to g.proj
  1687. ret = gcmd.RunCommand('g.proj',
  1688. flags = 'c',
  1689. proj4 = proj4string,
  1690. location = self.startpage.location)
  1691. if ret == 0:
  1692. return True
  1693. return False
  1694. def CustomCreate(self):
  1695. """Create a new location based on given proj4 string"""
  1696. proj4string = self.custompage.customstring
  1697. location = self.startpage.location
  1698. ret = gcmd.RunCommand('g.proj',
  1699. flags = 'c',
  1700. proj4 = proj4string,
  1701. location = location)
  1702. if ret == 0:
  1703. return True
  1704. return False
  1705. def EPSGCreate(self):
  1706. """
  1707. Create a new location from an EPSG code.
  1708. """
  1709. epsgcode = self.epsgpage.epsgcode
  1710. epsgdesc = self.epsgpage.epsgdesc
  1711. location = self.startpage.location
  1712. # should not happend
  1713. if epsgcode == '':
  1714. wx.MessageBox(parent=self,
  1715. message="%s: %s" % (_("Unable to create new location"), _("EPSG code missing.")),
  1716. caption=_("Error"), style=wx.OK | wx.ICON_ERROR)
  1717. return False
  1718. # creating location
  1719. ret = gcmd.RunCommand('g.proj',
  1720. read = True,
  1721. epsg = epsgcode,
  1722. datumtrans = '-1')
  1723. dtoptions = {}
  1724. if ret:
  1725. line = ret.splitlines()
  1726. i = 0
  1727. while i < len(line):
  1728. if line[i] == '---':
  1729. for j in range(3):
  1730. dtoptions[line[i+1]] = (line[i+2],
  1731. line[i+3],
  1732. line[i+4])
  1733. i += 5
  1734. if dtoptions != {}:
  1735. dtrans = ''
  1736. # open a dialog to select datum transform number
  1737. dlg = SelectDatumDialog(self.parent, datums=dtoptions)
  1738. if dlg.ShowModal() == wx.ID_OK:
  1739. dtrans = dlg.GetDatum()
  1740. if dtrans == '':
  1741. wx.MessageBox(parent=self.parent,
  1742. message=_('Datum transform is required.'),
  1743. caption=_("Error"),
  1744. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  1745. dlg.Destroy()
  1746. return False
  1747. dlg.Destroy()
  1748. else:
  1749. dlg.Destroy()
  1750. return False
  1751. datumtrans = dtrans
  1752. else:
  1753. datumtrans = '1'
  1754. ret = gcmd.RunCommand('g.proj',
  1755. flags = 'c',
  1756. epsg = epsgcode,
  1757. location = location,
  1758. datumtrans = datumtrans)
  1759. if ret == 0:
  1760. return True
  1761. return False
  1762. def FileCreate(self):
  1763. """
  1764. Create a new location from a georeferenced file
  1765. """
  1766. georeffile = self.filepage.georeffile
  1767. location = self.startpage.location
  1768. # this should not happen
  1769. if not georeffile or not os.path.isfile(georeffile):
  1770. dlg = wx.MessageBox(parent=self.wizard,
  1771. message="%s: %s ('%s')" % \
  1772. (_("Unable to create new location"),
  1773. _("file not found"),
  1774. georeffile),
  1775. caption=("Error"), style=wx.OK | wx.ICON_ERROR)
  1776. return False
  1777. # creating location
  1778. ret = gcmd.RunCommand('g.proj',
  1779. flags = 'c',
  1780. georef = georeffile,
  1781. location = location)
  1782. if ret == 0:
  1783. return True
  1784. return False
  1785. def WKTCreate(self):
  1786. """
  1787. Create a new location from a WKT file
  1788. """
  1789. wktfile = self.wktpage.wktfile
  1790. location = self.startpage.location
  1791. # this should not happen
  1792. if not wktfile or not os.path.isfile(wktfile):
  1793. dlg = wx.MessageBox(parent=self.wizard,
  1794. message="%s: %s ('%s')" % \
  1795. (_("Unable to create new location"),
  1796. _("file not found"),
  1797. wktfile),
  1798. caption=("Error"), style=wx.OK | wx.ICON_ERROR)
  1799. return False
  1800. # creating location
  1801. ret = gcmd.RunCommand('g.proj',
  1802. flags = 'c',
  1803. wkt = wktfile,
  1804. location = location)
  1805. if ret == 0:
  1806. return True
  1807. return False
  1808. class RegionDef(BaseClass, wx.Frame):
  1809. """
  1810. Page for setting default region extents and resolution
  1811. """
  1812. def __init__(self, parent, id=wx.ID_ANY,
  1813. title=_("Set default region extent and resolution"), location=None):
  1814. wx.Frame.__init__(self, parent, id, title, size=(650,300))
  1815. panel = wx.Panel(self, id=wx.ID_ANY)
  1816. self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, 'grass.ico'), wx.BITMAP_TYPE_ICO))
  1817. self.parent = parent
  1818. self.location = location
  1819. #
  1820. # default values
  1821. #
  1822. # 2D
  1823. self.north = 1.0
  1824. self.south = 0.0
  1825. self.east = 1.0
  1826. self.west = 0.0
  1827. self.nsres = 1.0
  1828. self.ewres = 1.0
  1829. # 3D
  1830. self.top = 1.0
  1831. self.bottom = 0.0
  1832. # self.nsres3 = 1.0
  1833. # self.ewres3 = 1.0
  1834. self.tbres = 1.0
  1835. #
  1836. # inputs
  1837. #
  1838. # 2D
  1839. self.tnorth = self.MakeTextCtrl(text=str(self.north), size=(150, -1), parent=panel)
  1840. self.tsouth = self.MakeTextCtrl(str(self.south), size=(150, -1), parent=panel)
  1841. self.twest = self.MakeTextCtrl(str(self.west), size=(150, -1), parent=panel)
  1842. self.teast = self.MakeTextCtrl(str(self.east), size=(150, -1), parent=panel)
  1843. self.tnsres = self.MakeTextCtrl(str(self.nsres), size=(150, -1), parent=panel)
  1844. self.tewres = self.MakeTextCtrl(str(self.ewres), size=(150, -1), parent=panel)
  1845. #
  1846. # labels
  1847. #
  1848. self.lrows = self.MakeLabel(parent=panel)
  1849. self.lcols = self.MakeLabel(parent=panel)
  1850. self.lcells = self.MakeLabel(parent=panel)
  1851. #
  1852. # buttons
  1853. #
  1854. self.bset = self.MakeButton(text=_("&Set region"), id=wx.ID_OK, parent=panel)
  1855. self.bcancel = wx.Button(panel, id=wx.ID_CANCEL)
  1856. self.bset.SetDefault()
  1857. #
  1858. # image
  1859. #
  1860. self.img = wx.Image(os.path.join(globalvar.ETCWXDIR, "images",
  1861. "qgis_world.png"), wx.BITMAP_TYPE_PNG).ConvertToBitmap()
  1862. #
  1863. # set current working environment to PERMANENT mapset
  1864. # in selected location in order to set default region (WIND)
  1865. #
  1866. envval = {}
  1867. ret = gcmd.RunCommand('g.gisenv',
  1868. read = True)
  1869. if ret:
  1870. for line in ret.splitlines():
  1871. key, val = line.split('=')
  1872. envval[key] = val
  1873. self.currlocation = envval['LOCATION_NAME'].strip("';")
  1874. self.currmapset = envval['MAPSET'].strip("';")
  1875. if self.currlocation != self.location or self.currmapset != 'PERMANENT':
  1876. gcmd.RunCommand('g.gisenv',
  1877. set = 'LOCATION_NAME=%s' % self.location)
  1878. gcmd.RunCommand('g.gisenv',
  1879. set = 'MAPSET=PERMANENT')
  1880. else:
  1881. dlg = wx.MessageBox(parent=self,
  1882. message=_('Invalid location selected.'),
  1883. caption=_("Error"), style=wx.ID_OK | wx.ICON_ERROR)
  1884. return
  1885. #
  1886. # get current region settings
  1887. #
  1888. region = {}
  1889. ret = gcmd.RunCommand('g.region',
  1890. read = True,
  1891. flags = 'gp3')
  1892. if ret:
  1893. for line in ret.splitlines():
  1894. key, val = line.split('=')
  1895. region[key] = float(val)
  1896. else:
  1897. dlg = wx.MessageBox(parent=self,
  1898. message=_("Invalid region"),
  1899. caption=_("Error"), style=wx.ID_OK | wx.ICON_ERROR)
  1900. dlg.ShowModal()
  1901. dlg.Destroy()
  1902. return
  1903. #
  1904. # update values
  1905. # 2D
  1906. self.north = float(region['n'])
  1907. self.south = float(region['s'])
  1908. self.east = float(region['e'])
  1909. self.west = float(region['w'])
  1910. self.nsres = float(region['nsres'])
  1911. self.ewres = float(region['ewres'])
  1912. self.rows = int(region['rows'])
  1913. self.cols = int(region['cols'])
  1914. self.cells = int(region['cells'])
  1915. # 3D
  1916. self.top = float(region['t'])
  1917. self.bottom = float(region['b'])
  1918. # self.nsres3 = float(region['nsres3'])
  1919. # self.ewres3 = float(region['ewres3'])
  1920. self.tbres = float(region['tbres'])
  1921. self.depth = int(region['depths'])
  1922. self.cells3 = int(region['3dcells'])
  1923. #
  1924. # 3D box collapsable
  1925. #
  1926. self.infoCollapseLabelExp = _("Click here to show 3D settings")
  1927. self.infoCollapseLabelCol = _("Click here to hide 3D settings")
  1928. self.settings3D = wx.CollapsiblePane(parent=panel,
  1929. label=self.infoCollapseLabelExp,
  1930. style=wx.CP_DEFAULT_STYLE |
  1931. wx.CP_NO_TLW_RESIZE | wx.EXPAND)
  1932. self.MakeSettings3DPaneContent(self.settings3D.GetPane())
  1933. self.settings3D.Collapse(False) # FIXME
  1934. self.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED, self.OnSettings3DPaneChanged, self.settings3D)
  1935. #
  1936. # set current region settings
  1937. #
  1938. self.tnorth.SetValue(str(self.north))
  1939. self.tsouth.SetValue(str(self.south))
  1940. self.twest.SetValue(str(self.west))
  1941. self.teast.SetValue(str(self.east))
  1942. self.tnsres.SetValue(str(self.nsres))
  1943. self.tewres.SetValue(str(self.ewres))
  1944. self.ttop.SetValue(str(self.top))
  1945. self.tbottom.SetValue(str(self.bottom))
  1946. # self.tnsres3.SetValue(str(self.nsres3))
  1947. # self.tewres3.SetValue(str(self.ewres3))
  1948. self.ttbres.SetValue(str(self.tbres))
  1949. self.lrows.SetLabel(_("Rows: %d" % self.rows))
  1950. self.lcols.SetLabel(_("Cols: %d" % self.cols))
  1951. self.lcells.SetLabel(_("Cells: %d" % self.cells))
  1952. #
  1953. # bindings
  1954. #
  1955. self.Bind(wx.EVT_BUTTON, self.OnSetButton, self.bset)
  1956. self.Bind(wx.EVT_BUTTON, self.OnCancel, self.bcancel)
  1957. self.tnorth.Bind(wx.EVT_TEXT, self.OnValue)
  1958. self.tsouth.Bind(wx.EVT_TEXT, self.OnValue)
  1959. self.teast.Bind(wx.EVT_TEXT, self.OnValue)
  1960. self.twest.Bind(wx.EVT_TEXT, self.OnValue)
  1961. self.tnsres.Bind(wx.EVT_TEXT, self.OnValue)
  1962. self.tewres.Bind(wx.EVT_TEXT, self.OnValue)
  1963. self.ttop.Bind(wx.EVT_TEXT, self.OnValue)
  1964. self.tbottom.Bind(wx.EVT_TEXT, self.OnValue)
  1965. # self.tnsres3.Bind(wx.EVT_TEXT, self.OnValue)
  1966. # self.tewres3.Bind(wx.EVT_TEXT, self.OnValue)
  1967. self.ttbres.Bind(wx.EVT_TEXT, self.OnValue)
  1968. self.__DoLayout(panel)
  1969. self.SetMinSize(self.GetBestSize())
  1970. self.minWindowSize = self.GetMinSize()
  1971. def MakeSettings3DPaneContent(self, pane):
  1972. """Create 3D region settings pane"""
  1973. border = wx.BoxSizer(wx.VERTICAL)
  1974. gridSizer = wx.GridBagSizer(vgap=0, hgap=0)
  1975. # inputs
  1976. self.ttop = wx.TextCtrl(parent=pane, id=wx.ID_ANY, value=str(self.top),
  1977. size=(150, -1))
  1978. self.tbottom = wx.TextCtrl(parent=pane, id=wx.ID_ANY, value=str(self.bottom),
  1979. size=(150, -1))
  1980. self.ttbres = wx.TextCtrl(parent=pane, id=wx.ID_ANY, value=str(self.tbres),
  1981. size=(150, -1))
  1982. # self.tnsres3 = wx.TextCtrl(parent=pane, id=wx.ID_ANY, value=str(self.nsres3),
  1983. # size=(150, -1))
  1984. # self.tewres3 = wx.TextCtrl(parent=pane, id=wx.ID_ANY, value=str(self.ewres3),
  1985. # size=(150, -1))
  1986. #labels
  1987. self.ldepth = wx.StaticText(parent=pane, label=_("Depth: %d") % self.depth)
  1988. self.lcells3 = wx.StaticText(parent=pane, label=_("3D Cells: %d") % self.cells3)
  1989. # top
  1990. gridSizer.Add(item=wx.StaticText(parent=pane, label=_("Top")),
  1991. flag=wx.ALIGN_CENTER |
  1992. wx.LEFT | wx.RIGHT | wx.TOP, border=5,
  1993. pos=(0, 1))
  1994. gridSizer.Add(item=self.ttop,
  1995. flag=wx.ALIGN_CENTER_HORIZONTAL |
  1996. wx.ALL, border=5, pos=(1, 1))
  1997. # bottom
  1998. gridSizer.Add(item=wx.StaticText(parent=pane, label=_("Bottom")),
  1999. flag=wx.ALIGN_CENTER |
  2000. wx.LEFT | wx.RIGHT | wx.TOP, border=5,
  2001. pos=(0, 2))
  2002. gridSizer.Add(item=self.tbottom,
  2003. flag=wx.ALIGN_CENTER_HORIZONTAL |
  2004. wx.ALL, border=5, pos=(1, 2))
  2005. # tbres
  2006. gridSizer.Add(item=wx.StaticText(parent=pane, label=_("T-B resolution")),
  2007. flag=wx.ALIGN_CENTER |
  2008. wx.LEFT | wx.RIGHT | wx.TOP, border=5,
  2009. pos=(0, 3))
  2010. gridSizer.Add(item=self.ttbres,
  2011. flag=wx.ALIGN_CENTER_HORIZONTAL |
  2012. wx.ALL, border=5, pos=(1, 3))
  2013. # res
  2014. # gridSizer.Add(item=wx.StaticText(parent=pane, label=_("3D N-S resolution")),
  2015. # flag=wx.ALIGN_CENTER |
  2016. # wx.LEFT | wx.RIGHT | wx.TOP, border=5,
  2017. # pos=(2, 1))
  2018. # gridSizer.Add(item=self.tnsres3,
  2019. # flag=wx.ALIGN_CENTER_HORIZONTAL |
  2020. # wx.ALL, border=5, pos=(3, 1))
  2021. # gridSizer.Add(item=wx.StaticText(parent=pane, label=_("3D E-W resolution")),
  2022. # flag=wx.ALIGN_CENTER |
  2023. # wx.LEFT | wx.RIGHT | wx.TOP, border=5,
  2024. # pos=(2, 3))
  2025. # gridSizer.Add(item=self.tewres3,
  2026. # flag=wx.ALIGN_CENTER_HORIZONTAL |
  2027. # wx.ALL, border=5, pos=(3, 3))
  2028. # rows/cols/cells
  2029. gridSizer.Add(item=self.ldepth,
  2030. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  2031. wx.ALL, border=5, pos=(2, 1))
  2032. gridSizer.Add(item=self.lcells3,
  2033. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  2034. wx.ALL, border=5, pos=(2, 2))
  2035. border.Add(item=gridSizer, proportion=1,
  2036. flag=wx.ALL | wx.ALIGN_CENTER | wx.EXPAND, border=5)
  2037. pane.SetSizer(border)
  2038. border.Fit(pane)
  2039. def OnSettings3DPaneChanged(self, event):
  2040. """Collapse 3D settings box"""
  2041. if self.settings3D.IsExpanded():
  2042. self.settings3D.SetLabel(self.infoCollapseLabelCol)
  2043. self.Layout()
  2044. self.SetSize(self.GetBestSize())
  2045. self.SetMinSize(self.GetSize())
  2046. else:
  2047. self.settings3D.SetLabel(self.infoCollapseLabelExp)
  2048. self.Layout()
  2049. self.SetSize(self.minWindowSize)
  2050. self.SetMinSize(self.minWindowSize)
  2051. self.SendSizeEvent()
  2052. def __DoLayout(self, panel):
  2053. """Window layout"""
  2054. frameSizer = wx.BoxSizer(wx.VERTICAL)
  2055. gridSizer = wx.GridBagSizer(vgap=0, hgap=0)
  2056. settings3DSizer = wx.BoxSizer(wx.VERTICAL)
  2057. buttonSizer = wx.BoxSizer(wx.HORIZONTAL)
  2058. # north
  2059. gridSizer.Add(item=self.MakeLabel(text=_("North"), parent=panel),
  2060. flag=wx.ALIGN_BOTTOM | wx.ALIGN_CENTER_HORIZONTAL |
  2061. wx.TOP | wx.LEFT | wx.RIGHT, border=5, pos=(0, 2))
  2062. gridSizer.Add(item=self.tnorth,
  2063. flag=wx.ALIGN_CENTER_HORIZONTAL |
  2064. wx.ALIGN_CENTER_VERTICAL |
  2065. wx.ALL, border=5, pos=(1, 2))
  2066. # west
  2067. gridSizer.Add(item=self.MakeLabel(text=_("West"), parent=panel),
  2068. flag=wx.ALIGN_RIGHT |
  2069. wx.ALIGN_CENTER_VERTICAL |
  2070. wx.LEFT | wx.TOP | wx.BOTTOM, border=5, pos=(2, 0))
  2071. gridSizer.Add(item=self.twest,
  2072. flag=wx.ALIGN_RIGHT |
  2073. wx.ALIGN_CENTER_VERTICAL |
  2074. wx.ALL, border=5, pos=(2, 1))
  2075. gridSizer.Add(item=wx.StaticBitmap(panel, wx.ID_ANY, self.img, (-1, -1),
  2076. (self.img.GetWidth(), self.img.GetHeight())),
  2077. flag=wx.ALIGN_CENTER |
  2078. wx.ALIGN_CENTER_VERTICAL |
  2079. wx.ALL, border=5, pos=(2, 2))
  2080. # east
  2081. gridSizer.Add(item=self.teast,
  2082. flag=wx.ALIGN_CENTER_HORIZONTAL |
  2083. wx.ALIGN_CENTER_VERTICAL |
  2084. wx.ALL, border=5, pos=(2, 3))
  2085. gridSizer.Add(item=self.MakeLabel(text=_("East"), parent=panel),
  2086. flag=wx.ALIGN_LEFT |
  2087. wx.ALIGN_CENTER_VERTICAL |
  2088. wx.RIGHT | wx.TOP | wx.BOTTOM, border=5, pos=(2, 4))
  2089. # south
  2090. gridSizer.Add(item=self.tsouth,
  2091. flag=wx.ALIGN_CENTER_HORIZONTAL |
  2092. wx.ALIGN_CENTER_VERTICAL |
  2093. wx.ALL, border=5, pos=(3, 2))
  2094. gridSizer.Add(item=self.MakeLabel(text=_("South"), parent=panel),
  2095. flag=wx.ALIGN_TOP | wx.ALIGN_CENTER_HORIZONTAL |
  2096. wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5, pos=(4, 2))
  2097. # ns-res
  2098. gridSizer.Add(item=self.MakeLabel(text=_("N-S resolution"), parent=panel),
  2099. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  2100. wx.TOP | wx.LEFT | wx.RIGHT, border=5, pos=(5, 1))
  2101. gridSizer.Add(item=self.tnsres,
  2102. flag=wx.ALIGN_RIGHT |
  2103. wx.ALIGN_CENTER_VERTICAL |
  2104. wx.ALL, border=5, pos=(6, 1))
  2105. # ew-res
  2106. gridSizer.Add(item=self.MakeLabel(text=_("E-W resolution"), parent=panel),
  2107. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  2108. wx.TOP | wx.LEFT | wx.RIGHT, border=5, pos=(5, 3))
  2109. gridSizer.Add(item=self.tewres,
  2110. flag=wx.ALIGN_RIGHT |
  2111. wx.ALIGN_CENTER_VERTICAL |
  2112. wx.ALL, border=5, pos=(6, 3))
  2113. # rows/cols/cells
  2114. gridSizer.Add(item=self.lrows,
  2115. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  2116. wx.ALL, border=5, pos=(7, 1))
  2117. gridSizer.Add(item=self.lcells,
  2118. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  2119. wx.ALL, border=5, pos=(7, 2))
  2120. gridSizer.Add(item=self.lcols,
  2121. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  2122. wx.ALL, border=5, pos=(7, 3))
  2123. # 3D
  2124. settings3DSizer.Add(item=self.settings3D,
  2125. flag=wx.ALL,
  2126. border=5)
  2127. # buttons
  2128. buttonSizer.Add(item=self.bcancel, proportion=1,
  2129. flag=wx.ALIGN_RIGHT |
  2130. wx.ALIGN_CENTER_VERTICAL |
  2131. wx.ALL, border=10)
  2132. buttonSizer.Add(item=self.bset, proportion=1,
  2133. flag=wx.ALIGN_CENTER |
  2134. wx.ALIGN_CENTER_VERTICAL |
  2135. wx.ALL, border=10)
  2136. frameSizer.Add(item=gridSizer, proportion=1,
  2137. flag=wx.ALL | wx.ALIGN_CENTER, border=5)
  2138. frameSizer.Add(item=settings3DSizer, proportion=0,
  2139. flag=wx.ALL | wx.ALIGN_CENTER, border=5)
  2140. frameSizer.Add(item=buttonSizer, proportion=0,
  2141. flag=wx.ALL | wx.ALIGN_RIGHT, border=5)
  2142. self.SetAutoLayout(True)
  2143. panel.SetSizer(frameSizer)
  2144. frameSizer.Fit(panel)
  2145. self.Layout()
  2146. def OnValue(self, event):
  2147. """Set given value"""
  2148. try:
  2149. if event.GetId() == self.tnorth.GetId():
  2150. self.north = float(event.GetString())
  2151. elif event.GetId() == self.tsouth.GetId():
  2152. self.south = float(event.GetString())
  2153. elif event.GetId() == self.teast.GetId():
  2154. self.east = float(event.GetString())
  2155. elif event.GetId() == self.twest.GetId():
  2156. self.west = float(event.GetString())
  2157. elif event.GetId() == self.tnsres.GetId():
  2158. self.nsres = float(event.GetString())
  2159. elif event.GetId() == self.tewres.GetId():
  2160. self.ewres = float(event.GetString())
  2161. elif event.GetId() == self.ttop.GetId():
  2162. self.top = float(event.GetString())
  2163. elif event.GetId() == self.tbottom.GetId():
  2164. self.bottom = float(event.GetString())
  2165. # elif event.GetId() == self.tnsres3.GetId():
  2166. # self.nsres3 = float(event.GetString())
  2167. # elif event.GetId() == self.tewres3.GetId():
  2168. # self.ewres3 = float(event.GetString())
  2169. elif event.GetId() == self.ttbres.GetId():
  2170. self.tbres = float(event.GetString())
  2171. self.__UpdateInfo()
  2172. except ValueError, e:
  2173. if len(event.GetString()) > 0 and event.GetString() != '-':
  2174. dlg = wx.MessageBox(parent=self,
  2175. message=_("Invalid value: %s") % e,
  2176. caption=_("Error"),
  2177. style=wx.OK | wx.ICON_ERROR)
  2178. # reset values
  2179. self.tnorth.SetValue(str(self.north))
  2180. self.tsouth.SetValue(str(self.south))
  2181. self.teast.SetValue(str(self.east))
  2182. self.twest.SetValue(str(self.west))
  2183. self.tnsres.SetValue(str(self.nsres))
  2184. self.tewres.SetValue(str(self.ewres))
  2185. self.ttop.SetValue(str(self.top))
  2186. self.tbottom.SetValue(str(self.bottom))
  2187. self.ttbres.SetValue(str(self.tbres))
  2188. # self.tnsres3.SetValue(str(self.nsres3))
  2189. # self.tewres3.SetValue(str(self.ewres3))
  2190. event.Skip()
  2191. def __UpdateInfo(self):
  2192. """Update number of rows/cols/cells"""
  2193. self.rows = int((self.north - self.south) / self.nsres)
  2194. self.cols = int((self.east - self.west) / self.ewres)
  2195. self.cells = self.rows * self.cols
  2196. self.depth = int((self.top - self.bottom) / self.tbres)
  2197. self.cells3 = self.rows * self.cols * self.depth
  2198. # 2D
  2199. self.lrows.SetLabel(_("Rows: %d") % self.rows)
  2200. self.lcols.SetLabel(_("Cols: %d") % self.cols)
  2201. self.lcells.SetLabel(_("Cells: %d") % self.cells)
  2202. # 3D
  2203. self.ldepth.SetLabel(_("Depth: %d" % self.depth))
  2204. self.lcells3.SetLabel(_("3D Cells: %d" % self.cells3))
  2205. def OnSetButton(self, event=None):
  2206. """Set default region"""
  2207. ret = gcmd.RunCommand('g.region',
  2208. flags = 'sgpa',
  2209. n = self.north,
  2210. s = self.south,
  2211. e = self.east,
  2212. w = self.west,
  2213. nsres = self.nsres,
  2214. ewres = self.ewres,
  2215. t = self.top,
  2216. b = self.bottom,
  2217. tbres = self.tbres)
  2218. if ret == 0:
  2219. self.Destroy()
  2220. def OnCancel(self, event):
  2221. self.Destroy()
  2222. class SelectDatumDialog(wx.Dialog):
  2223. """Dialog for selecting datum transformations"""
  2224. def __init__(self, parent, datums, title=_("Select datum transformation"),
  2225. pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_DIALOG_STYLE):
  2226. self.datums = datums
  2227. wx.Dialog.__init__(self, parent, wx.ID_ANY, title, pos, size, style)
  2228. panel = wx.Panel(self, wx.ID_ANY)
  2229. sizer = wx.BoxSizer(wx.VERTICAL)
  2230. #
  2231. # dialog body
  2232. #
  2233. bodyBox = wx.StaticBox(parent=panel, id=wx.ID_ANY,
  2234. label=" %s " % _("List of datum transformations"))
  2235. bodySizer = wx.StaticBoxSizer(bodyBox, wx.HORIZONTAL)
  2236. gridSizer = wx.GridBagSizer(vgap=5, hgap=5)
  2237. gridSizer.Add(item=wx.StaticText(parent=panel, label=_("Datums:")),
  2238. flag=wx.ALIGN_CENTER_VERTICAL, pos=(0, 0))
  2239. items = self.datums.keys()
  2240. utils.ListSortLower(items)
  2241. self.cdatums = wx.ComboBox(parent=panel, id=wx.ID_ANY,
  2242. style=wx.CB_SIMPLE | wx.CB_READONLY,
  2243. choices=items,
  2244. size=(300,-1))
  2245. self.cdatums.SetSelection(0)
  2246. self.cdatums.Bind(wx.EVT_COMBOBOX, self.OnChangeDatum)
  2247. gridSizer.Add(item=self.cdatums, pos=(0, 1))
  2248. self.textWidth = self.GetSize()[0]
  2249. self.datumDesc = wx.StaticText(parent=panel,
  2250. label='\n'.join(self.datums[self.cdatums.GetStringSelection()]))
  2251. self.datumDesc.Wrap(self.textWidth)
  2252. gridSizer.Add(item=self.datumDesc, flag=wx.EXPAND,
  2253. pos=(1, 0), span=(1, 2))
  2254. bodySizer.Add(item=gridSizer, proportion=1,
  2255. flag=wx.ALL | wx.ALIGN_CENTER | wx.EXPAND, border=5)
  2256. #
  2257. # buttons
  2258. #
  2259. btnsizer = wx.StdDialogButtonSizer()
  2260. btn = wx.Button(parent=panel, id=wx.ID_OK)
  2261. btn.SetDefault()
  2262. btnsizer.AddButton(btn)
  2263. btn = wx.Button(parent=panel, id=wx.ID_CANCEL)
  2264. btnsizer.AddButton(btn)
  2265. btnsizer.Realize()
  2266. sizer.Add(item=bodySizer, proportion=1,
  2267. flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
  2268. sizer.Add(item=btnsizer, proportion=0,
  2269. flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
  2270. #
  2271. # set panel sizer
  2272. #
  2273. panel.SetSizer(sizer)
  2274. sizer.Fit(panel)
  2275. self.SetSize(self.GetBestSize())
  2276. def OnChangeDatum(self, event):
  2277. """Datum changed, update description text"""
  2278. self.datumDesc.SetLabel('\n'.join(self.datums[event.GetString()]))
  2279. self.datumDesc.Wrap(self.textWidth)
  2280. event.Skip()
  2281. def GetDatum(self):
  2282. """Get selected datum"""
  2283. return self.cdatums.GetStringSelection()
  2284. if __name__ == "__main__":
  2285. app = wx.PySimpleApp()
  2286. # gWizard = LocationWizard(None, "")
  2287. gWizard = RegionDef(None)
  2288. gWizzard.Show()
  2289. app.MainLoop()