location_wizard.py 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762
  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. from grass.script import core as grass
  44. try:
  45. import subprocess
  46. except:
  47. CompatPath = os.path.join(globalvar.ETCWXDIR)
  48. sys.path.append(CompatPath)
  49. from compat import subprocess
  50. global coordsys
  51. global north
  52. global south
  53. global east
  54. global west
  55. global resolution
  56. global wizerror
  57. coordsys = ''
  58. north = ''
  59. south = ''
  60. east = ''
  61. west = ''
  62. resolution = ''
  63. class BaseClass(wx.Object):
  64. """!Base class providing basic methods"""
  65. def __init__(self):
  66. pass
  67. def MakeLabel(self, text="", style=wx.ALIGN_LEFT, parent=None):
  68. """!Make aligned label"""
  69. if not parent:
  70. parent = self
  71. return wx.StaticText(parent=parent, id=wx.ID_ANY, label=text,
  72. style=style)
  73. def MakeTextCtrl(self, text='', size=(100,-1), style=0, parent=None):
  74. """!Generic text control"""
  75. if not parent:
  76. parent = self
  77. return wx.TextCtrl(parent=parent, id=wx.ID_ANY, value=text,
  78. size=size, style=style)
  79. def MakeButton(self, text, id=wx.ID_ANY, size=(-1,-1), parent=None):
  80. """!Generic button"""
  81. if not parent:
  82. parent = self
  83. return wx.Button(parent=parent, id=id, label=text,
  84. size=size)
  85. class TitledPage(BaseClass, wiz.WizardPageSimple):
  86. """
  87. Class to make wizard pages. Generic methods to make
  88. labels, text entries, and buttons.
  89. """
  90. def __init__(self, parent, title):
  91. self.page = wiz.WizardPageSimple.__init__(self, parent)
  92. # page title
  93. self.title = wx.StaticText(parent=self, id=wx.ID_ANY, label=title)
  94. self.title.SetFont(wx.Font(13, wx.SWISS, wx.NORMAL, wx.BOLD))
  95. # main sizer
  96. self.sizer = wx.GridBagSizer(vgap=0, hgap=0)
  97. def DoLayout(self):
  98. """!Do page layout"""
  99. tmpsizer = wx.BoxSizer(wx.VERTICAL)
  100. tmpsizer.Add(item=self.title, proportion=0,
  101. flag=wx.ALIGN_CENTRE | wx.ALL,
  102. border=5)
  103. tmpsizer.Add(item=wx.StaticLine(self, -1), proportion=0,
  104. flag=wx.EXPAND | wx.ALL,
  105. border=0)
  106. tmpsizer.Add(item=self.sizer, proportion=1,
  107. flag=wx.EXPAND | wx.ALL,
  108. border=5)
  109. self.SetAutoLayout(True)
  110. self.SetSizer(tmpsizer)
  111. # tmpsizer.Fit(self)
  112. self.Layout()
  113. class DatabasePage(TitledPage):
  114. """
  115. Wizard page for setting GIS data directory and location name
  116. """
  117. def __init__(self, wizard, parent, grassdatabase):
  118. TitledPage.__init__(self, wizard, _("Define GRASS Database and Location Name"))
  119. self.grassdatabase = grassdatabase
  120. self.location = ''
  121. # buttons
  122. self.bbrowse = self.MakeButton(_("Browse"))
  123. # text controls
  124. self.tgisdbase = self.MakeTextCtrl(grassdatabase, size=(300, -1))
  125. self.tlocation = self.MakeTextCtrl("newLocation", size=(300, -1))
  126. # layout
  127. self.sizer.AddGrowableCol(3)
  128. self.sizer.Add(item=self.MakeLabel(_("GIS Data Directory:")),
  129. flag=wx.ALIGN_RIGHT |
  130. wx.ALIGN_CENTER_VERTICAL |
  131. wx.ALL, border=5,
  132. pos=(1, 1))
  133. self.sizer.Add(item=self.tgisdbase,
  134. flag=wx.ALIGN_LEFT |
  135. wx.ALIGN_CENTER_VERTICAL |
  136. wx.ALL, border=5,
  137. pos=(1, 2))
  138. self.sizer.Add(item=self.bbrowse,
  139. flag=wx.ALIGN_LEFT |
  140. wx.ALIGN_CENTER_VERTICAL |
  141. wx.ALL, border=5,
  142. pos=(1, 3))
  143. #
  144. self.sizer.Add(item=self.MakeLabel("%s:" % _("Project Location")),
  145. flag=wx.ALIGN_RIGHT |
  146. wx.ALIGN_CENTER_VERTICAL |
  147. wx.ALL, border=5,
  148. pos=(2, 1))
  149. self.sizer.Add(item=self.tlocation,
  150. flag=wx.ALIGN_LEFT |
  151. wx.ALIGN_CENTER_VERTICAL |
  152. wx.ALL, border=5,
  153. pos=(2, 2))
  154. # self.sizer.Add(item=self.MakeLabel(_("(projection/coordinate system)")),
  155. # flag=wx.ALIGN_LEFT |
  156. # wx.ALIGN_CENTER_VERTICAL |
  157. # wx.ALL, border=5,
  158. # pos=(2, 4))
  159. # bindings
  160. self.Bind(wx.EVT_BUTTON, self.OnBrowse, self.bbrowse)
  161. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  162. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  163. self.tgisdbase.Bind(wx.EVT_TEXT, self.OnChangeName)
  164. self.tlocation.Bind(wx.EVT_TEXT, self.OnChangeName)
  165. # do page layout
  166. # self.DoLayout()
  167. def OnChangeName(self, event):
  168. """!Name for new location was changed"""
  169. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  170. if len(event.GetString()) > 0:
  171. if not nextButton.IsEnabled():
  172. nextButton.Enable()
  173. else:
  174. nextButton.Disable()
  175. event.Skip()
  176. def OnBrowse(self, event):
  177. dlg = wx.DirDialog(self, _("Choose GRASS data directory:"),
  178. os.getcwd(), wx.DD_DEFAULT_STYLE)
  179. if dlg.ShowModal() == wx.ID_OK:
  180. self.grassdatabase = dlg.GetPath()
  181. self.tgisdbase.SetValue(self.grassdatabase)
  182. dlg.Destroy()
  183. def OnPageChanging(self,event=None):
  184. error = ''
  185. if os.path.isdir(os.path.join(self.tgisdbase.GetValue(), self.tlocation.GetValue())):
  186. error = _("Location already exists in GRASS Database.")
  187. if error != '':
  188. dlg = wx.MessageDialog(parent=self, message="%s <%s>.%s%s" % (_("Unable to create location"),
  189. str(self.tlocation.GetValue()),
  190. os.linesep,
  191. error),
  192. caption=_("Error"), style=wx.OK | wx.ICON_ERROR)
  193. dlg.ShowModal()
  194. dlg.Destroy()
  195. event.Veto()
  196. return
  197. self.location = self.tlocation.GetValue()
  198. self.grassdatabase = self.tgisdbase.GetValue()
  199. def OnEnterPage(self, event):
  200. """!Wizard page changed"""
  201. self.grassdatabase = self.tgisdbase.GetValue()
  202. self.location = self.tlocation.GetValue()
  203. event.Skip()
  204. class CoordinateSystemPage(TitledPage):
  205. """
  206. Wizard page for choosing method for location creation
  207. """
  208. def __init__(self, wizard, parent):
  209. TitledPage.__init__(self, wizard, _("Choose method for creating a new location"))
  210. self.parent = parent
  211. global coordsys
  212. # toggles
  213. self.radio1 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  214. label=_("Select coordinate system"),
  215. style = wx.RB_GROUP)
  216. self.radio2 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  217. label=_("Select EPSG code of coordinate system"))
  218. self.radio3 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  219. label=_("Use coordinate system of selected "
  220. "georeferenced file"))
  221. self.radio4 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  222. label=_("Use coordinate system of selected "
  223. "WKT or PRJ file"))
  224. self.radio5 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  225. label=_("Create custom PROJ.4 parameters "
  226. "string for coordinate system"))
  227. self.radio6 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  228. label=_("Use arbitrary non-earth "
  229. "coordinate system (XY)"))
  230. # layout
  231. self.sizer.AddGrowableCol(1)
  232. self.sizer.SetVGap(10)
  233. self.sizer.Add(item=self.radio1,
  234. flag=wx.ALIGN_LEFT, pos=(1, 1))
  235. self.sizer.Add(item=self.radio2,
  236. flag=wx.ALIGN_LEFT, pos=(2, 1))
  237. self.sizer.Add(item=self.radio3,
  238. flag=wx.ALIGN_LEFT, pos=(3, 1))
  239. self.sizer.Add(item=self.radio4,
  240. flag=wx.ALIGN_LEFT, pos=(4, 1))
  241. self.sizer.Add(item=self.radio5,
  242. flag=wx.ALIGN_LEFT, pos=(5, 1))
  243. self.sizer.Add(item=self.radio6,
  244. flag=wx.ALIGN_LEFT, pos=(6, 1))
  245. # bindings
  246. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio1.GetId())
  247. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio2.GetId())
  248. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio3.GetId())
  249. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio4.GetId())
  250. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio5.GetId())
  251. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio6.GetId())
  252. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  253. # do page layout
  254. # self.DoLayout()
  255. def OnEnterPage(self, event):
  256. global coordsys
  257. if event.GetDirection() and not coordsys:
  258. coordsys = "proj"
  259. self.SetNext(self.parent.projpage)
  260. self.parent.sumpage.SetPrev(self.parent.datumpage)
  261. if not wx.FindWindowById(wx.ID_FORWARD).IsEnabled():
  262. wx.FindWindowById(wx.ID_FORWARD).Enable()
  263. def SetVal(self, event):
  264. """!Choose method"""
  265. global coordsys
  266. if event.GetId() == self.radio1.GetId():
  267. coordsys = "proj"
  268. self.SetNext(self.parent.projpage)
  269. self.parent.sumpage.SetPrev(self.parent.datumpage)
  270. elif event.GetId() == self.radio2.GetId():
  271. coordsys = "epsg"
  272. self.SetNext(self.parent.epsgpage)
  273. self.parent.sumpage.SetPrev(self.parent.epsgpage)
  274. elif event.GetId() == self.radio3.GetId():
  275. coordsys = "file"
  276. self.SetNext(self.parent.filepage)
  277. self.parent.sumpage.SetPrev(self.parent.filepage)
  278. elif event.GetId() == self.radio4.GetId():
  279. coordsys = "wkt"
  280. self.SetNext(self.parent.wktpage)
  281. self.parent.sumpage.SetPrev(self.parent.wktpage)
  282. elif event.GetId() == self.radio5.GetId():
  283. coordsys = "custom"
  284. self.SetNext(self.parent.custompage)
  285. self.parent.sumpage.SetPrev(self.parent.custompage)
  286. elif event.GetId() == self.radio6.GetId():
  287. coordsys = "xy"
  288. self.SetNext(self.parent.sumpage)
  289. self.parent.sumpage.SetPrev(self.parent.csystemspage)
  290. class ProjectionsPage(TitledPage):
  291. """
  292. Wizard page for selecting projection (select coordinate system option)
  293. """
  294. def __init__(self, wizard, parent):
  295. TitledPage.__init__(self, wizard, _("Choose projection"))
  296. self.parent = parent
  297. self.proj = ''
  298. self.projdesc = ''
  299. # text input
  300. self.tproj = self.MakeTextCtrl("", size=(200,-1))
  301. # search box
  302. self.searchb = wx.SearchCtrl(self, size=(200,-1),
  303. style=wx.TE_PROCESS_ENTER)
  304. # projection list
  305. self.projlist = ItemList(self, data=self.parent.projections.items(),
  306. columns=[_('Code'), _('Description')])
  307. # layout
  308. self.sizer.AddGrowableCol(3)
  309. self.sizer.Add(item=self.MakeLabel(_("Projection code:")),
  310. flag=wx.ALIGN_LEFT |
  311. wx.ALIGN_CENTER_VERTICAL |
  312. wx.ALL, border=5, pos=(1, 1))
  313. self.sizer.Add(item=self.tproj,
  314. flag=wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL,
  315. border=5, pos=(1, 2))
  316. self.sizer.Add(item=self.MakeLabel(_("Search in description:")),
  317. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  318. border=5, pos=(2, 1))
  319. self.sizer.Add(item=self.searchb,
  320. flag=wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL,
  321. border=5, pos=(2, 2))
  322. self.sizer.AddGrowableRow(3)
  323. self.sizer.Add(item=self.projlist,
  324. flag=wx.EXPAND |
  325. wx.ALIGN_LEFT |
  326. wx.ALL, border=5, pos=(3, 1), span=(1, 3))
  327. # events
  328. self.tproj.Bind(wx.EVT_TEXT, self.OnText)
  329. self.tproj.Bind(wx.EVT_TEXT_ENTER, self.OnText)
  330. self.searchb.Bind(wx.EVT_TEXT_ENTER, self.OnSearch)
  331. self.projlist.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
  332. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  333. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  334. # do layout
  335. # self.Layout()
  336. def OnPageChanging(self,event):
  337. if event.GetDirection() and self.proj not in self.parent.projections:
  338. event.Veto()
  339. if self.proj == 'utm':
  340. self.parent.projtypepage.text_utm.SetEditable(True)
  341. self.parent.projtypepage.hemischoices = ['north','south']
  342. else:
  343. self.parent.projtypepage.text_utm.SetValue('')
  344. self.parent.projtypepage.text_utm.SetEditable(False)
  345. self.parent.projtypepage.hemischoices = []
  346. def OnText(self, event):
  347. """!Projection name changed"""
  348. self.proj = event.GetString().lower()
  349. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  350. if len(self.proj) == 0 and nextButton.IsEnabled():
  351. nextButton.Enable(False)
  352. if self.proj in self.parent.projections:
  353. self.projdesc = self.parent.projections[self.proj]
  354. if not nextButton.IsEnabled():
  355. nextButton.Enable()
  356. def OnEnterPage(self, event):
  357. if len(self.proj) == 0:
  358. # disable 'next' button by default
  359. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  360. else:
  361. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  362. event.Skip()
  363. def OnSearch(self, event):
  364. """!Search projection by desc"""
  365. str = event.GetString()
  366. try:
  367. self.proj, self.projdesc = self.projlist.Search(index=[0,1], pattern=event.GetString())
  368. except:
  369. self.proj = self.projdesc = ''
  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().lower()
  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, pattern):
  524. """!Search projection by description
  525. Return first found item or None
  526. """
  527. if pattern == '':
  528. self.Populate(self.sourceData)
  529. return []
  530. data = []
  531. pattern = pattern.lower()
  532. for idx in index:
  533. for i in range(len(self.sourceData)):
  534. try:
  535. value = str(self.sourceData[i][idx]).lower()
  536. if pattern in value:
  537. data.append(self.sourceData[i])
  538. except UnicodeDecodeError:
  539. # osgeo4w problem (should be fixed)
  540. pass
  541. self.Populate(data)
  542. if len(data) > 0:
  543. return data[0]
  544. else:
  545. # self.Populate(self.sourceData)
  546. return []
  547. class ProjTypePage(TitledPage):
  548. """
  549. Wizard page for selecting method of setting coordinate system parameters
  550. (select coordinate system option)
  551. """
  552. def __init__(self, wizard, parent):
  553. TitledPage.__init__(self, wizard, _("Choose method of specifying georeferencing parameters"))
  554. global coordsys
  555. self.utmzone = ''
  556. self.utmhemisphere = ''
  557. self.hemischoices = ["north","south"]
  558. self.parent = parent
  559. self.radio1 = wx.RadioButton(parent=self, id=wx.ID_ANY, label=_("Select datum with associated ellipsoid"),
  560. style = wx.RB_GROUP)
  561. self.radio2 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  562. label=_("Select ellipsoid"))
  563. self.title_utm = self.MakeLabel(_("Set zone for UTM projection:"))
  564. self.text_utm = self.MakeTextCtrl(size=(100,-1))
  565. self.label_utm = self.MakeLabel(_("Zone:"))
  566. self.hemisphere = wx.Choice(parent=self, id=wx.ID_ANY, size=(100, -1),
  567. choices=self.hemischoices)
  568. self.label_hemisphere = self.MakeLabel(_("Hemisphere for zone:"))
  569. # layout
  570. self.sizer.AddGrowableCol(2)
  571. self.sizer.Add(item=self.radio1,
  572. flag=wx.ALIGN_LEFT, pos=(1, 1), span=(1, 2))
  573. self.sizer.Add(item=self.radio2,
  574. flag=wx.ALIGN_LEFT, pos=(2, 1), span=(1, 2))
  575. self.sizer.Add(item=self.title_utm,
  576. flag=wx.ALIGN_LEFT | wx.ALL,
  577. border=5, pos=(4, 1), span=(1, 2))
  578. self.sizer.Add(item=self.label_utm,
  579. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  580. border=5, pos=(5, 1), span=(1, 1))
  581. self.sizer.Add(item=self.text_utm,
  582. flag=wx.ALIGN_LEFT | wx.ALL, border=5,
  583. pos=(5, 2), span=(1, 1))
  584. self.sizer.Add(item=self.label_hemisphere,
  585. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  586. border=5, pos=(6, 1), span=(1, 1))
  587. self.sizer.Add(item=self.hemisphere,
  588. flag=wx.ALIGN_LEFT|wx.ALL,
  589. border=5, pos=(6, 2), span=(1, 1))
  590. self.title_utm.Hide()
  591. self.text_utm.Hide()
  592. self.label_utm.Hide()
  593. self.hemisphere.Hide()
  594. self.label_hemisphere.Hide()
  595. # bindings
  596. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio1.GetId())
  597. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio2.GetId())
  598. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChange)
  599. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  600. # do page layout
  601. # self.DoLayout()
  602. def OnPageChange(self,event=None):
  603. if event.GetDirection() and self.parent.projpage.proj == 'utm' and self.utmzone == '':
  604. wx.MessageBox('You must set a zone for a UTM projection')
  605. event.Veto()
  606. self.title_utm.Hide()
  607. self.text_utm.Hide()
  608. self.label_utm.Hide()
  609. self.hemisphere.Hide()
  610. self.label_hemisphere.Hide()
  611. def OnEnterPage(self,event):
  612. if self.parent.projpage.proj == 'utm':
  613. self.title_utm.Show()
  614. self.text_utm.Show()
  615. self.label_utm.Show()
  616. self.hemisphere.Show()
  617. self.label_hemisphere.Show()
  618. self.Bind(wx.EVT_CHOICE, self.OnHemisphere, self.hemisphere)
  619. self.Bind(wx.EVT_TEXT, self.GetUTM, self.text_utm)
  620. if not wx.FindWindowById(wx.ID_FORWARD).IsEnabled():
  621. wx.FindWindowById(wx.ID_FORWARD).Enable()
  622. event.Skip()
  623. def SetVal(self, event):
  624. global coordsys
  625. if event.GetId() == self.radio1.GetId():
  626. self.SetNext(self.parent.datumpage)
  627. self.parent.sumpage.SetPrev(self.parent.datumpage)
  628. elif event.GetId() == self.radio2.GetId():
  629. self.SetNext(self.parent.ellipsepage)
  630. self.parent.sumpage.SetPrev(self.parent.ellipsepage)
  631. def GetUTM(self, event):
  632. self.utmzone = event.GetString()
  633. def OnHemisphere(self, event):
  634. self.utmhemisphere = event.GetString()
  635. class DatumPage(TitledPage):
  636. """
  637. Wizard page for selecting datum (with associated ellipsoid)
  638. and datum transformation parameters (select coordinate system option)
  639. """
  640. def __init__(self, wizard, parent):
  641. TitledPage.__init__(self, wizard, _("Specify geodetic datum"))
  642. self.parent = parent
  643. self.datum = ''
  644. self.datumdesc = ''
  645. self.ellipsoid = ''
  646. self.datumparams = ''
  647. self.transform = ''
  648. self.transregion = ''
  649. self.transparams = ''
  650. self.hastransform = False
  651. self.proj4params = ''
  652. # text input
  653. self.tdatum = self.MakeTextCtrl("", size=(200,-1))
  654. # self.ttrans = self.MakeTextCtrl("", size=(200,-1))
  655. # search box
  656. self.searchb = wx.SearchCtrl(self, size=(200,-1),
  657. style=wx.TE_PROCESS_ENTER)
  658. # create list control for datum/elipsoid list
  659. data = []
  660. for key in self.parent.datums.keys():
  661. data.append([key, self.parent.datums[key][0], self.parent.datums[key][1]])
  662. self.datumlist = ItemList(self,
  663. data=data,
  664. columns=[_('Code'), _('Description'), _('Ellipsoid')])
  665. # # create list control for datum transformation parameters list
  666. # data = []
  667. # for key in self.parent.transforms.keys():
  668. # data.append([key, self.parent.transforms[key][0], self.parent.transforms[key][1]])
  669. # self.transformlist = ItemList(self,
  670. # data=None,
  671. # columns=[_('Code'), _('Datum'), _('Description')])
  672. # self.transformlist.sourceData = data
  673. # layout
  674. self.sizer.AddGrowableCol(4)
  675. self.sizer.Add(item=self.MakeLabel(_("Datum code:")),
  676. flag=wx.ALIGN_LEFT |
  677. wx.ALIGN_CENTER_VERTICAL |
  678. wx.ALL, border=5, pos=(1, 1))
  679. self.sizer.Add(item=self.tdatum,
  680. flag=wx.ALIGN_LEFT |
  681. wx.ALIGN_CENTER_VERTICAL |
  682. wx.ALL, border=5, pos=(1, 2))
  683. self.sizer.Add(item=self.MakeLabel(_("Search in description:")),
  684. flag=wx.ALIGN_LEFT |
  685. wx.ALIGN_CENTER_VERTICAL |
  686. wx.ALL, border=5, pos=(2, 1))
  687. self.sizer.Add(item=self.searchb,
  688. flag=wx.ALIGN_LEFT |
  689. wx.ALIGN_CENTER_VERTICAL |
  690. wx.ALL, border=5, pos=(2, 2))
  691. self.sizer.AddGrowableRow(3)
  692. self.sizer.Add(item=self.datumlist,
  693. flag=wx.EXPAND |
  694. wx.ALIGN_LEFT |
  695. wx.ALL, border=5, pos=(3, 1), span=(1, 4))
  696. # self.sizer.Add(item=self.MakeLabel(_("Transformation parameters:")),
  697. # flag=wx.ALIGN_RIGHT |
  698. # wx.ALIGN_CENTER_VERTICAL |
  699. # wx.ALL, border=5, pos=(5, 1))
  700. # self.sizer.Add(item=self.ttrans,
  701. # flag=wx.ALIGN_LEFT |
  702. # wx.ALIGN_CENTER_VERTICAL |
  703. # wx.ALL, border=5, pos=(5, 2))
  704. # self.sizer.Add(item=self.transformlist,
  705. # flag=wx.EXPAND |
  706. # wx.ALIGN_LEFT |
  707. # wx.ALL, border=5, pos=(6, 1), span=(1, 4))
  708. # events
  709. self.datumlist.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnDatumSelected)
  710. # self.transformlist.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnTransformSelected)
  711. self.searchb.Bind(wx.EVT_TEXT_ENTER, self.OnDSearch)
  712. self.tdatum.Bind(wx.EVT_TEXT, self.OnDText)
  713. self.tdatum.Bind(wx.EVT_TEXT_ENTER, self.OnDText)
  714. # self.ttrans.Bind(wx.EVT_TEXT, self.OnTText)
  715. # self.ttrans.Bind(wx.EVT_TEXT_ENTER, self.OnTText)
  716. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  717. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  718. # do page layout
  719. # self.DoLayout()
  720. def OnPageChanging(self, event):
  721. self.proj4params = ''
  722. if event.GetDirection() and self.datum not in self.parent.datums:
  723. event.Veto()
  724. if self.hastransform == True and self.transform == '':
  725. event.Veto()
  726. self.GetNext().SetPrev(self)
  727. self.parent.ellipsepage.ellipseparams = self.parent.ellipsoids[self.ellipsoid][1]
  728. def OnEnterPage(self,event):
  729. if len(self.datum) == 0 or \
  730. (self.hastransform == True and self.transform == ''):
  731. # disable 'next' button by default
  732. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  733. else:
  734. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  735. event.Skip()
  736. def OnDText(self, event):
  737. self.datum = event.GetString().lower()
  738. # tlist = self.transformlist.Search(index=1, pattern=self.datum)
  739. tlist = []
  740. if len(tlist) > 0:
  741. self.hastransform = True
  742. else:
  743. self.hastransform = False
  744. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  745. if len(self.datum) == 0 and nextButton.IsEnabled():
  746. nextButton.Enable(False)
  747. elif self.datum in self.parent.datums:
  748. self.datumdesc = self.parent.datums[self.datum][0]
  749. self.ellipsoid = self.parent.datums[self.datum][1]
  750. self.datumparams = self.parent.datums[self.datum][2]
  751. if self.hastransform == False or \
  752. (self.hastransform == True and self.transform != ''):
  753. if not nextButton.IsEnabled():
  754. nextButton.Enable(True)
  755. else:
  756. if nextButton.IsEnabled():
  757. nextButton.Enable(False)
  758. event.Skip()
  759. def OnTText(self, event):
  760. if self.hastransform == False:
  761. event.Skip()
  762. return
  763. self.transform = event.GetString()
  764. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  765. if len(self.transform) == 0 and nextButton.IsEnabled():
  766. nextButton.Enable(False)
  767. elif self.transform in self.parent.transforms:
  768. self.transdatum = self.parent.transforms[self.transform][0]
  769. self.transregion = self.parent.transforms[self.transform][1]
  770. self.transparams = self.parent.transforms[self.transform][2]
  771. if not nextButton.IsEnabled():
  772. nextButton.Enable()
  773. def OnDSearch(self, event):
  774. str = self.searchb.GetValue()
  775. try:
  776. self.datum, self.datumdesc, self.ellipsoid = self.datumlist.Search(index=[0,1,2], pattern=str)
  777. # self.transformlist.Search(index=1, pattern=self.datum)
  778. except:
  779. self.datum = self.datumdesc = self.ellipsoid = ''
  780. # if str == '' or self.datum == '':
  781. # self.transformlist.DeleteAllItems()
  782. # self.transformlist.Refresh()
  783. event.Skip()
  784. def OnTransformSelected(self,event):
  785. index = event.m_itemIndex
  786. item = event.GetItem()
  787. self.transform = self.transformlist.GetItem(index, 0).GetText()
  788. self.transdatum = self.parent.transforms[self.transform][0]
  789. self.transregion = self.parent.transforms[self.transform][1]
  790. self.transparams = self.parent.transforms[self.transform][2]
  791. self.ttrans.SetValue(str(self.transform))
  792. def OnDatumSelected(self,event):
  793. index = event.m_itemIndex
  794. item = event.GetItem()
  795. self.datum = self.datumlist.GetItem(index, 0).GetText()
  796. self.datumdesc = self.parent.datums[self.datum][0]
  797. self.ellipsoid = self.parent.datums[self.datum][1]
  798. self.datumparams = self.parent.datums[self.datum][2]
  799. self.tdatum.SetValue(self.datum)
  800. event.Skip()
  801. class EllipsePage(TitledPage):
  802. """
  803. Wizard page for selecting ellipsoid (select coordinate system option)
  804. """
  805. def __init__(self, wizard, parent):
  806. TitledPage.__init__(self, wizard, _("Specify ellipsoid"))
  807. self.parent = parent
  808. self.ellipse = ''
  809. self.ellipsedesc = ''
  810. self.ellipseparams = ''
  811. self.proj4params = ''
  812. # text input
  813. self.tellipse = self.MakeTextCtrl("", size=(200,-1))
  814. # search box
  815. self.searchb = wx.SearchCtrl(self, size=(200,-1),
  816. style=wx.TE_PROCESS_ENTER)
  817. # create list control for ellipse list
  818. data = []
  819. # extract code, desc
  820. for key in self.parent.ellipsoids.keys():
  821. data.append([key, self.parent.ellipsoids[key][0]])
  822. self.ellipselist = ItemList(self, data=data,
  823. columns=[_('Code'), _('Description')])
  824. # layout
  825. self.sizer.AddGrowableCol(4)
  826. self.sizer.Add(item=self.MakeLabel(_("Ellipsoid code:")),
  827. flag=wx.ALIGN_RIGHT |
  828. wx.ALIGN_CENTER_VERTICAL |
  829. wx.ALL, border=5, pos=(1, 1))
  830. self.sizer.Add(item=self.tellipse,
  831. flag=wx.ALIGN_LEFT |
  832. wx.ALIGN_CENTER_VERTICAL |
  833. wx.ALL, border=5, pos=(1, 2))
  834. self.sizer.Add(item=self.MakeLabel(_("Search in description:")),
  835. flag=wx.ALIGN_RIGHT |
  836. wx.ALIGN_CENTER_VERTICAL |
  837. wx.ALL, border=5, pos=(2, 1))
  838. self.sizer.Add(item=self.searchb,
  839. flag=wx.ALIGN_LEFT |
  840. wx.ALIGN_CENTER_VERTICAL |
  841. wx.ALL, border=5, pos=(2, 2))
  842. self.sizer.AddGrowableRow(3)
  843. self.sizer.Add(item=self.ellipselist,
  844. flag=wx.EXPAND |
  845. wx.ALIGN_LEFT |
  846. wx.ALL, border=5, pos=(3, 1), span=(1, 4))
  847. # events
  848. self.ellipselist.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
  849. self.tellipse.Bind(wx.EVT_TEXT, self.OnText)
  850. self.tellipse.Bind(wx.EVT_TEXT_ENTER, self.OnText)
  851. self.searchb.Bind(wx.EVT_TEXT_ENTER, self.OnSearch)
  852. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  853. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  854. # do page layout
  855. # self.DoLayout()
  856. def OnEnterPage(self,event):
  857. if len(self.ellipse) == 0:
  858. # disable 'next' button by default
  859. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  860. else:
  861. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  862. event.Skip()
  863. def OnPageChanging(self, event):
  864. if event.GetDirection() and self.ellipse not in self.parent.ellipsoids:
  865. event.Veto()
  866. self.proj4params = ''
  867. self.GetNext().SetPrev(self)
  868. self.parent.datumpage.datumparams = ''
  869. self.parent.datumpage.transparams = ''
  870. # self.GetNext().SetPrev(self) (???)
  871. def OnText(self, event):
  872. """!Ellipspoid code changed"""
  873. self.ellipse = event.GetString()
  874. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  875. if len(self.ellipse) == 0 and nextButton.IsEnabled():
  876. nextButton.Enable(False)
  877. self.ellipsedesc = ''
  878. self.ellipseparams = ''
  879. self.proj4params = ''
  880. elif self.ellipse in self.parent.ellipsoids:
  881. self.ellipsedesc = self.parent.ellipsoids[self.ellipse][0]
  882. self.ellipseparams = self.parent.ellipsoids[self.ellipse][1]
  883. if not nextButton.IsEnabled():
  884. nextButton.Enable()
  885. def OnSearch(self, event):
  886. """!Search ellipsoid by desc"""
  887. str = event.GetString()
  888. try:
  889. self.ellipse, self.ellipsedesc = \
  890. self.ellipselist.Search(index=[0,1], pattern=event.GetString())
  891. self.ellipseparams = self.parent.ellipsoids[self.ellipse][1]
  892. self.proj4params = self.parent.ellipsoids[self.ellipse][2]
  893. except:
  894. self.ellipse = self.ellipsedesc = ''
  895. self.ellipseparams = self.proj4params = ''
  896. self.tellipse.SetValue(self.ellipse)
  897. event.Skip()
  898. def OnItemSelected(self,event):
  899. index = event.m_itemIndex
  900. item = event.GetItem()
  901. self.ellipse = self.ellipselist.GetItem(index, 0).GetText()
  902. self.ellipsedesc = self.parent.ellipsoids[self.ellipse][0]
  903. self.ellipseparams = self.parent.ellipsoids[self.ellipse][1]
  904. self.tellipse.SetValue(self.ellipse)
  905. class GeoreferencedFilePage(TitledPage):
  906. """
  907. Wizard page for selecting georeferenced file to use
  908. for setting coordinate system parameters
  909. """
  910. def __init__(self, wizard, parent):
  911. TitledPage.__init__(self, wizard, _("Select georeferenced file"))
  912. self.georeffile = ''
  913. # create controls
  914. self.lfile= self.MakeLabel(_("Georeferenced file:"))
  915. self.tfile = self.MakeTextCtrl(size=(300,-1))
  916. self.bbrowse = self.MakeButton(_("Browse"))
  917. # do layout
  918. self.sizer.AddGrowableCol(3)
  919. self.sizer.Add(item=self.lfile, flag=wx.ALIGN_LEFT |
  920. wx.ALIGN_CENTRE_VERTICAL |
  921. wx.ALL, border=5, pos=(1, 1))
  922. self.sizer.Add(item=self.tfile, flag=wx.ALIGN_LEFT |
  923. wx.ALIGN_CENTRE_VERTICAL |
  924. wx.ALL, border=5, pos=(1, 2))
  925. self.sizer.Add(item=self.bbrowse, flag=wx.ALIGN_LEFT |
  926. wx.ALL, border=5, pos=(1, 3))
  927. self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
  928. self.tfile.Bind(wx.EVT_TEXT, self.OnText)
  929. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  930. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  931. # do page layout
  932. # self.DoLayout()
  933. def OnEnterPage(self, event):
  934. if len(self.georeffile) == 0:
  935. # disable 'next' button by default
  936. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  937. else:
  938. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  939. event.Skip()
  940. def OnPageChanging(self, event):
  941. if event.GetDirection() and self.georeffile == '':
  942. event.Veto()
  943. self.GetNext().SetPrev(self)
  944. event.Skip()
  945. def OnText(self, event):
  946. self.georeffile = event.GetString()
  947. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  948. if len(self.georeffile) > 0 and os.path.isfile(self.georeffile):
  949. if not nextButton.IsEnabled():
  950. nextButton.Enable(True)
  951. else:
  952. if nextButton.IsEnabled():
  953. nextButton.Enable(False)
  954. event.Skip()
  955. def OnBrowse(self, event):
  956. """!Choose file"""
  957. dlg = wx.FileDialog(self,
  958. _("Select georeferenced file"),
  959. os.getcwd(), "", "*.*", wx.OPEN)
  960. if dlg.ShowModal() == wx.ID_OK:
  961. path = dlg.GetPath()
  962. self.tfile.SetValue(path)
  963. dlg.Destroy()
  964. event.Skip()
  965. def OnCreate(self, event):
  966. pass
  967. class WKTPage(TitledPage):
  968. """
  969. Wizard page for selecting WKT file to use
  970. for setting coordinate system parameters
  971. """
  972. def __init__(self, wizard, parent):
  973. TitledPage.__init__(self, wizard, _("Select WKT file"))
  974. self.wktfile = ''
  975. # create controls
  976. self.lfile= self.MakeLabel(_("WKT file:"))
  977. self.tfile = self.MakeTextCtrl(size=(300,-1))
  978. self.bbrowse = self.MakeButton(_("Browse"))
  979. # do layout
  980. self.sizer.AddGrowableCol(3)
  981. self.sizer.Add(item=self.lfile, flag=wx.ALIGN_LEFT |
  982. wx.ALIGN_CENTRE_VERTICAL |
  983. wx.ALL, border=5, pos=(1, 1))
  984. self.sizer.Add(item=self.tfile, flag=wx.ALIGN_LEFT |
  985. wx.ALIGN_CENTRE_VERTICAL |
  986. wx.ALL, border=5, pos=(1, 2))
  987. self.sizer.Add(item=self.bbrowse, flag=wx.ALIGN_LEFT |
  988. wx.ALL, border=5, pos=(1, 3))
  989. self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
  990. self.tfile.Bind(wx.EVT_TEXT, self.OnText)
  991. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  992. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  993. # do page layout
  994. # self.DoLayout()
  995. def OnEnterPage(self, event):
  996. if len(self.wktfile) == 0:
  997. # disable 'next' button by default
  998. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  999. else:
  1000. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1001. event.Skip()
  1002. def OnPageChanging(self, event):
  1003. if event.GetDirection() and self.wktfile == '':
  1004. event.Veto()
  1005. self.GetNext().SetPrev(self)
  1006. event.Skip()
  1007. def OnText(self, event):
  1008. self.wktfile = event.GetString()
  1009. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  1010. if len(self.wktfile) > 0 and os.path.isfile(self.wktfile):
  1011. if not nextButton.IsEnabled():
  1012. nextButton.Enable(True)
  1013. else:
  1014. if nextButton.IsEnabled():
  1015. nextButton.Enable(False)
  1016. event.Skip()
  1017. def OnBrowse(self, event):
  1018. """!Choose file"""
  1019. dlg = wx.FileDialog(self,
  1020. _("Select WKT file"),
  1021. os.getcwd(), "", "*.*", wx.OPEN)
  1022. if dlg.ShowModal() == wx.ID_OK:
  1023. path = dlg.GetPath()
  1024. self.tfile.SetValue(path)
  1025. dlg.Destroy()
  1026. event.Skip()
  1027. def OnCreate(self, event):
  1028. pass
  1029. class EPSGPage(TitledPage):
  1030. """
  1031. Wizard page for selecting EPSG code for
  1032. setting coordinate system parameters
  1033. """
  1034. def __init__(self, wizard, parent):
  1035. TitledPage.__init__(self, wizard, _("Choose EPSG Code"))
  1036. self.parent = parent
  1037. self.epsgCodeDict = {}
  1038. self.epsgcode = None
  1039. self.epsgdesc = ''
  1040. self.epsgparams = ''
  1041. # labels
  1042. self.lfile= self.MakeLabel(_("Path to the EPSG-codes file:"),
  1043. style=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
  1044. self.lcode= self.MakeLabel(_("EPSG code:"),
  1045. style=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
  1046. self.lsearch = self.MakeLabel(_("Search in"),
  1047. style=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
  1048. self.csearch = wx.Choice(parent = self, id = wx.ID_ANY,
  1049. choices = [_("codes"), _("description"), _("parameters")])
  1050. self.csearch.SetStringSelection(_("description"))
  1051. # text input
  1052. epsgdir = utils.PathJoin(os.environ["GRASS_PROJSHARE"], 'epsg')
  1053. self.tfile = self.MakeTextCtrl(text=epsgdir, size=(200,-1))
  1054. self.tcode = self.MakeTextCtrl(size=(200,-1))
  1055. # buttons
  1056. self.bbrowse = self.MakeButton(_("Browse"))
  1057. self.bbcodes = self.MakeButton(_("Reload EPSG Codes"))
  1058. # search box
  1059. self.searchb = wx.SearchCtrl(self, size=(200,-1),
  1060. style=wx.TE_PROCESS_ENTER)
  1061. self.epsglist = ItemList(self, data=None,
  1062. columns=[_('Code'), _('Description'), _('Parameters')])
  1063. # layout
  1064. self.sizer.AddGrowableCol(3)
  1065. self.sizer.Add(item=self.lfile,
  1066. flag=wx.ALIGN_LEFT |
  1067. wx.ALIGN_CENTER_VERTICAL |
  1068. wx.ALL, border=5, pos=(1, 1), span=(1, 2))
  1069. self.sizer.Add(item=self.tfile,
  1070. flag=wx.ALIGN_LEFT |
  1071. wx.ALIGN_CENTER_VERTICAL |
  1072. wx.ALL, border=5, pos=(1, 3))
  1073. self.sizer.Add(item=self.bbrowse,
  1074. flag=wx.ALIGN_LEFT |
  1075. wx.ALIGN_CENTER_VERTICAL |
  1076. wx.ALL, border=5, pos=(1, 4))
  1077. self.sizer.Add(item=self.lcode,
  1078. flag=wx.ALIGN_LEFT |
  1079. wx.ALIGN_CENTER_VERTICAL |
  1080. wx.ALL, border=5, pos=(2, 1), span=(1, 2))
  1081. self.sizer.Add(item=self.tcode,
  1082. flag=wx.ALIGN_LEFT |
  1083. wx.ALIGN_CENTER_VERTICAL |
  1084. wx.ALL, border=5, pos=(2, 3))
  1085. self.sizer.Add(item=self.lsearch,
  1086. flag=wx.ALIGN_LEFT |
  1087. wx.ALIGN_CENTER_VERTICAL |
  1088. wx.ALL, border=5, pos=(3, 1))
  1089. self.sizer.Add(item=self.csearch,
  1090. flag=wx.ALIGN_LEFT |
  1091. wx.ALIGN_CENTER_VERTICAL |
  1092. wx.ALL, border=5, pos=(3, 2))
  1093. self.sizer.Add(item=self.searchb,
  1094. flag=wx.ALIGN_LEFT |
  1095. wx.ALIGN_CENTER_VERTICAL |
  1096. wx.ALL, border=5, pos=(3, 3))
  1097. self.sizer.Add(item=self.bbcodes,
  1098. flag=wx.ALIGN_LEFT |
  1099. wx.ALIGN_CENTER_VERTICAL |
  1100. wx.ALL, border=5, pos=(3, 4))
  1101. self.sizer.AddGrowableRow(4)
  1102. self.sizer.Add(item=self.epsglist,
  1103. flag=wx.ALIGN_LEFT | wx.EXPAND, pos=(4, 1),
  1104. span=(1, 4))
  1105. # events
  1106. self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
  1107. self.bbcodes.Bind(wx.EVT_BUTTON, self.OnBrowseCodes)
  1108. self.tcode.Bind(wx.EVT_TEXT, self.OnText)
  1109. self.tcode.Bind(wx.EVT_TEXT_ENTER, self.OnText)
  1110. self.epsglist.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
  1111. self.searchb.Bind(wx.EVT_TEXT_ENTER, self.OnSearch)
  1112. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  1113. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1114. # do page layout
  1115. # self.DoLayout()
  1116. def OnEnterPage(self, event):
  1117. if not self.epsgcode:
  1118. # disable 'next' button by default
  1119. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1120. else:
  1121. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1122. # load default epsg database file
  1123. self.OnBrowseCodes(None)
  1124. event.Skip()
  1125. def OnPageChanging(self, event):
  1126. if event.GetDirection() and not self.epsgcode:
  1127. event.Veto()
  1128. self.GetNext().SetPrev(self)
  1129. def OnText(self, event):
  1130. self.epsgcode = event.GetString()
  1131. try:
  1132. self.epsgcode = int(self.epsgcode)
  1133. except:
  1134. self.epsgcode = None
  1135. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  1136. if self.epsgcode and self.epsgcode in self.epsgCodeDict.keys():
  1137. self.epsgdesc = self.epsgCodeDict[self.epsgcode][0]
  1138. self.epsgparams = self.epsgCodeDict[self.epsgcode][1]
  1139. if not nextButton.IsEnabled():
  1140. nextButton.Enable(True)
  1141. else:
  1142. self.epsgcode = None # not found
  1143. if nextButton.IsEnabled():
  1144. nextButton.Enable(False)
  1145. self.epsgdesc = self.epsgparams = ''
  1146. def OnSearch(self, event):
  1147. value = self.searchb.GetValue()
  1148. if self.epsglist.GetItemCount() == 0:
  1149. event.Skip()
  1150. return
  1151. try:
  1152. self.epsgcode = self.epsglist.Search(index=[self.csearch.GetSelection()],
  1153. pattern=value)[0]
  1154. except IndexError: # -> no item found
  1155. self.epsgcode = None
  1156. self.tcode.SetValue('')
  1157. self.searchb.SetValue('')
  1158. event.Skip()
  1159. def OnBrowse(self, event):
  1160. """!Define path for EPSG code file"""
  1161. path = os.path.dirname(self.tfile.GetValue())
  1162. if not path:
  1163. path = os.getcwd()
  1164. dlg = wx.FileDialog(parent=self, message=_("Choose EPSG codes file"),
  1165. defaultDir=path, defaultFile="", wildcard="*", style=wx.OPEN)
  1166. if dlg.ShowModal() == wx.ID_OK:
  1167. path = dlg.GetPath()
  1168. self.tfile.SetValue(path)
  1169. self.OnBrowseCodes(None)
  1170. dlg.Destroy()
  1171. event.Skip()
  1172. def OnItemSelected(self, event):
  1173. """!EPSG code selected from the list"""
  1174. index = event.m_itemIndex
  1175. item = event.GetItem()
  1176. self.epsgcode = int(self.epsglist.GetItem(index, 0).GetText())
  1177. self.epsgdesc = self.epsglist.GetItem(index, 1).GetText()
  1178. self.tcode.SetValue(str(self.epsgcode))
  1179. event.Skip()
  1180. def OnBrowseCodes(self, event, search=None):
  1181. """!Browse EPSG codes"""
  1182. self.epsgCodeDict = utils.ReadEpsgCodes(self.tfile.GetValue())
  1183. if type(self.epsgCodeDict) == type(''):
  1184. wx.MessageBox(parent=self,
  1185. message=_("Unable to read EPGS codes: %s") % self.epsgCodeDict,
  1186. caption=_("Error"), style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  1187. self.epsglist.Populate([], update=True)
  1188. return
  1189. data = []
  1190. for code, val in self.epsgCodeDict.iteritems():
  1191. if code is not None:
  1192. data.append((code, val[0], val[1]))
  1193. self.epsglist.Populate(data, 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. # datum transform number
  1388. #
  1389. self.datumtrans = 0
  1390. #
  1391. # define wizard pages
  1392. #
  1393. self.wizard = wiz.Wizard(parent, id=wx.ID_ANY, title=_("Define new GRASS Location"),
  1394. bitmap=wizbmp)
  1395. self.startpage = DatabasePage(self.wizard, self, grassdatabase)
  1396. self.csystemspage = CoordinateSystemPage(self.wizard, self)
  1397. self.projpage = ProjectionsPage(self.wizard, self)
  1398. self.datumpage = DatumPage(self.wizard, self)
  1399. self.projtypepage = ProjTypePage(self.wizard,self)
  1400. self.epsgpage = EPSGPage(self.wizard, self)
  1401. self.filepage = GeoreferencedFilePage(self.wizard, self)
  1402. self.wktpage = WKTPage(self.wizard, self)
  1403. self.ellipsepage = EllipsePage(self.wizard, self)
  1404. self.custompage = CustomPage(self.wizard, self)
  1405. self.sumpage = SummaryPage(self.wizard, self)
  1406. #
  1407. # set the initial order of the pages
  1408. # (should follow the epsg line)
  1409. #
  1410. self.startpage.SetNext(self.csystemspage)
  1411. self.csystemspage.SetPrev(self.startpage)
  1412. self.csystemspage.SetNext(self.sumpage)
  1413. self.projpage.SetPrev(self.csystemspage)
  1414. self.projpage.SetNext(self.projtypepage)
  1415. self.projtypepage.SetPrev(self.projpage)
  1416. self.projtypepage.SetNext(self.datumpage)
  1417. self.datumpage.SetPrev(self.projtypepage)
  1418. self.datumpage.SetNext(self.sumpage)
  1419. self.ellipsepage.SetPrev(self.projtypepage)
  1420. self.ellipsepage.SetNext(self.sumpage)
  1421. self.epsgpage.SetPrev(self.csystemspage)
  1422. self.epsgpage.SetNext(self.sumpage)
  1423. self.filepage.SetPrev(self.csystemspage)
  1424. self.filepage.SetNext(self.sumpage)
  1425. self.wktpage.SetPrev(self.csystemspage)
  1426. self.wktpage.SetNext(self.sumpage)
  1427. self.custompage.SetPrev(self.csystemspage)
  1428. self.custompage.SetNext(self.sumpage)
  1429. self.sumpage.SetPrev(self.csystemspage)
  1430. #
  1431. # do pages layout
  1432. #
  1433. self.startpage.DoLayout()
  1434. self.csystemspage.DoLayout()
  1435. self.projpage.DoLayout()
  1436. self.datumpage.DoLayout()
  1437. self.projtypepage.DoLayout()
  1438. self.epsgpage.DoLayout()
  1439. self.filepage.DoLayout()
  1440. self.wktpage.DoLayout()
  1441. self.ellipsepage.DoLayout()
  1442. self.custompage.DoLayout()
  1443. self.sumpage.DoLayout()
  1444. self.wizard.FitToPage(self.datumpage)
  1445. # new location created?
  1446. self.location = None
  1447. success = False
  1448. # location created in different GIS database?
  1449. self.altdb = False
  1450. #
  1451. # run wizard...
  1452. #
  1453. if self.wizard.RunWizard(self.startpage):
  1454. msg = self.OnWizFinished()
  1455. if len(msg) < 1:
  1456. self.wizard.Destroy()
  1457. self.location = self.startpage.location
  1458. if self.altdb == False:
  1459. dlg = wx.MessageDialog(parent=self.parent,
  1460. message=_("Do you want to set the default "
  1461. "region extents and resolution now?"),
  1462. caption=_("Location <%s> created") % self.location,
  1463. style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
  1464. dlg.CenterOnScreen()
  1465. if dlg.ShowModal() == wx.ID_YES:
  1466. dlg.Destroy()
  1467. defineRegion = RegionDef(self.parent, location=self.location)
  1468. defineRegion.CenterOnScreen()
  1469. defineRegion.Show()
  1470. else:
  1471. dlg.Destroy()
  1472. else: # -> error
  1473. self.wizard.Destroy()
  1474. wx.MessageBox(parent=self.parent,
  1475. message="%s" % _("Unable to create new location. "
  1476. "Location <%(loc)s> not created.\n\n"
  1477. "Details: %(err)s") % \
  1478. { 'loc' : self.startpage.location,
  1479. 'err' : msg },
  1480. caption=_("Location wizard"),
  1481. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  1482. else:
  1483. win = wx.MessageBox(parent=self.parent,
  1484. message=_("Location wizard canceled. "
  1485. "Location not created."),
  1486. caption=_("Location wizard"))
  1487. def __readData(self):
  1488. """!Get georeferencing information from tables in $GISBASE/etc"""
  1489. # read projection definitions
  1490. f = open(os.path.join(globalvar.ETCDIR, "projections"), "r")
  1491. self.projections = {}
  1492. for line in f.readlines():
  1493. line = line.expandtabs(1)
  1494. line = line.strip()
  1495. if line == '' or line[0] == "#":
  1496. continue
  1497. proj, projdesc = line.split(":", 1)
  1498. self.projections[proj.strip()] = projdesc.strip()
  1499. f.close()
  1500. # read datum definitions
  1501. f = open(os.path.join(globalvar.ETCDIR, "datum.table"), "r")
  1502. self.datums = {}
  1503. paramslist = []
  1504. for line in f.readlines():
  1505. line = line.expandtabs(1)
  1506. line = line.strip()
  1507. if line == '' or line[0] == "#":
  1508. continue
  1509. datum, info = line.split(" ", 1)
  1510. info = info.strip()
  1511. datumdesc, params = info.split(" ", 1)
  1512. datumdesc = datumdesc.strip('"')
  1513. paramlist = params.split()
  1514. ellipsoid = paramlist.pop(0)
  1515. self.datums[datum] = (datumdesc.replace('_', ' '), ellipsoid, paramlist)
  1516. f.close()
  1517. # read datum transforms parameters
  1518. f = open(os.path.join(globalvar.ETCDIR, "datumtransform.table"), "r")
  1519. self.transforms = {}
  1520. j = 1
  1521. for line in f.readlines():
  1522. if j < 10:
  1523. transcode = 'T0' + str(j)
  1524. else:
  1525. transcode = 'T' + str(j)
  1526. line = line.expandtabs(1)
  1527. line = line.strip()
  1528. if line == '' or line[0] == "#":
  1529. continue
  1530. datum, rest = line.split(" ", 1)
  1531. rest = rest.strip('" ')
  1532. params, rest = rest.split('"', 1)
  1533. params = params.strip()
  1534. rest = rest.strip('" ')
  1535. try:
  1536. region, info = rest.split('"', 1)
  1537. info = info.strip('" ')
  1538. info = region + ': ' + info
  1539. except:
  1540. info = rest
  1541. self.transforms[transcode] = (datum, info, params)
  1542. j += 1
  1543. f.close()
  1544. # read ellipsiod definitions
  1545. f = open(os.path.join(globalvar.ETCDIR, "ellipse.table"), "r")
  1546. self.ellipsoids = {}
  1547. for line in f.readlines():
  1548. line = line.expandtabs(1)
  1549. line = line.strip()
  1550. if line == '' or line[0] == "#":
  1551. continue
  1552. ellipse, rest = line.split(" ", 1)
  1553. rest = rest.strip('" ')
  1554. desc, params = rest.split('"', 1)
  1555. desc = desc.strip('" ')
  1556. paramslist = params.split()
  1557. self.ellipsoids[ellipse] = (desc, paramslist)
  1558. f.close()
  1559. def OnWizFinished(self):
  1560. database = self.startpage.grassdatabase
  1561. location = self.startpage.location
  1562. global coordsys
  1563. msg = '' # error message (empty on success)
  1564. # location already exists?
  1565. if os.path.isdir(os.path.join(database,location)):
  1566. dlg = wx.MessageDialog(parent=self.wizard,
  1567. message="%s <%s>: %s" % \
  1568. (_("Unable to create new location"),
  1569. os.path.join(database, location),
  1570. _("Location already exists in GRASS Database.")),
  1571. caption=_("Error"),
  1572. style=wx.OK | wx.ICON_ERROR)
  1573. dlg.ShowModal()
  1574. dlg.Destroy()
  1575. return False
  1576. # current GISDbase or a new one?
  1577. current_gdb = grass.gisenv()['GISDBASE']
  1578. if current_gdb != database:
  1579. # change to new GISDbase or create new one
  1580. if os.path.isdir(database) != True:
  1581. # create new directory
  1582. os.mkdir(database)
  1583. # change to new GISDbase directory
  1584. gcmd.RunCommand('g.gisenv',
  1585. parent = self.wizard,
  1586. set='GISDBASE=%s' % database)
  1587. wx.MessageBox(parent=self.wizard,
  1588. message=_("Location <%(loc)s> will be created "
  1589. "in GIS data directory <%(dir)s>."
  1590. "You will need to change the default GIS "
  1591. "data directory in the GRASS startup screen.") % \
  1592. { 'loc' : location, 'dir' : database},
  1593. caption=_("New GIS data directory"),
  1594. style=wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
  1595. # location created in alternate GISDbase
  1596. self.altdb = True
  1597. if coordsys == "xy":
  1598. msg = self.XYCreate()
  1599. elif coordsys == "proj":
  1600. proj4string = self.CreateProj4String()
  1601. msg = self.Proj4Create(proj4string)
  1602. elif coordsys == 'custom':
  1603. wx.MessageBox('in custom')
  1604. msg = self.CustomCreate()
  1605. elif coordsys == "epsg":
  1606. msg = self.EPSGCreate()
  1607. elif coordsys == "file":
  1608. msg = self.FileCreate()
  1609. elif coordsys == "wkt":
  1610. msg = self.WKTCreate()
  1611. return msg
  1612. def XYCreate(self):
  1613. """!Create an XY location
  1614. @return error message (empty string on success)
  1615. """
  1616. database = self.startpage.grassdatabase
  1617. location = self.startpage.location
  1618. # create location directory and PERMANENT mapset
  1619. try:
  1620. os.mkdir(os.path.join(database, location))
  1621. os.mkdir(os.path.join(database, location, 'PERMANENT'))
  1622. # create DEFAULT_WIND and WIND files
  1623. regioninfo = ['proj: 0',
  1624. 'zone: 0',
  1625. 'north: 1',
  1626. 'south: 0',
  1627. 'east: 1',
  1628. 'west: 0',
  1629. 'cols: 1',
  1630. 'rows: 1',
  1631. 'e-w resol: 1',
  1632. 'n-s resol: 1',
  1633. 'top: 1',
  1634. 'bottom: 0',
  1635. 'cols3: 1',
  1636. 'rows3: 1',
  1637. 'depths: 1',
  1638. 'e-w resol3: 1',
  1639. 'n-s resol3: 1',
  1640. 't-b resol: 1']
  1641. defwind = open(os.path.join(database, location,
  1642. "PERMANENT", "DEFAULT_WIND"), 'w')
  1643. for param in regioninfo:
  1644. defwind.write(param + '%s' % os.linesep)
  1645. defwind.close()
  1646. shutil.copy(os.path.join(database, location, "PERMANENT", "DEFAULT_WIND"),
  1647. os.path.join(database, location, "PERMANENT", "WIND"))
  1648. # create MYNAME file
  1649. myname = open(os.path.join(database, location, "PERMANENT",
  1650. "MYNAME"), 'w')
  1651. myname.write('%s' % os.linesep)
  1652. myname.close()
  1653. except OSError, e:
  1654. return e
  1655. return ''
  1656. def CreateProj4String(self):
  1657. """!Constract PROJ.4 string"""
  1658. location = self.startpage.location
  1659. proj = self.projpage.proj
  1660. projdesc = self.projpage.projdesc
  1661. utmzone = self.projtypepage.utmzone
  1662. utmhemisphere = self.projtypepage.utmhemisphere
  1663. datum = self.datumpage.datum
  1664. if self.datumpage.datumdesc:
  1665. datumdesc = self.datumpage.datumdesc +' - ' + self.datumpage.ellipsoid
  1666. else:
  1667. datumdesc = ''
  1668. datumparams = self.datumpage.datumparams
  1669. # transparams = self.datumpage.transparams
  1670. ellipse = self.ellipsepage.ellipse
  1671. ellipsedesc = self.ellipsepage.ellipsedesc
  1672. ellipseparams = self.ellipsepage.ellipseparams
  1673. #
  1674. # creating PROJ.4 string
  1675. #
  1676. if proj == 'll':
  1677. proj4string = '+proj=longlat'
  1678. elif proj == 'utm':
  1679. proj4string = '+proj=%s +zone=%s' % (utm, utmzone)
  1680. if utmhemisphere == 'south':
  1681. proj4string += '+south'
  1682. else:
  1683. proj4string = '+proj=%s ' % (proj)
  1684. self.shortproj4string = proj4string
  1685. proj4params = ''
  1686. # set ellipsoid parameters
  1687. for item in ellipseparams:
  1688. if item[:4] == 'f=1/':
  1689. item = '+rf='+item[4:]
  1690. else:
  1691. item = '+'+item
  1692. proj4params = '%s %s' % (proj4params, item)
  1693. # set datum and transform parameters if relevant
  1694. if datumparams:
  1695. for item in datumparams:
  1696. proj4params = '%s +%s' % (proj4params,item)
  1697. # if transparams:
  1698. # proj4params = '%s +no_defs +%s' % (proj4params,transparams)
  1699. # else:
  1700. # proj4params = '%s +no_defs' % proj4params
  1701. proj4params = '%s +no_defs' % proj4params
  1702. # else:
  1703. # proj4params = '%s +no_defs' % proj4params
  1704. return '%s %s' % (proj4string, proj4params)
  1705. def Proj4Create(self, proj4string):
  1706. """!Create a new location for selected projection
  1707. @return error message (empty string on success)
  1708. """
  1709. datum = self.datumpage.datum
  1710. if datum != '':
  1711. proj4string = proj4string + ' +datum=%s' % datum
  1712. # check for datum tranforms
  1713. ret = gcmd.RunCommand('g.proj',
  1714. read = True,
  1715. proj4 = proj4string,
  1716. datumtrans = '-1')
  1717. dtoptions = {}
  1718. if ret:
  1719. line = ret.splitlines()
  1720. i = 0
  1721. while i < len(line):
  1722. if line[i] == '---':
  1723. for j in range(3):
  1724. dtoptions[line[i+1]] = (line[i+2],
  1725. line[i+3],
  1726. line[i+4])
  1727. i += 5
  1728. if dtoptions != {}:
  1729. dtrans = ''
  1730. # open a dialog to select datum transform number
  1731. dlg = SelectDatumDialog(self.parent, datums=dtoptions)
  1732. if dlg.ShowModal() == wx.ID_OK:
  1733. dtrans = dlg.GetDatum()
  1734. if dtrans == '':
  1735. dlg.Destroy()
  1736. return 'Datum transform is required.'
  1737. else:
  1738. dlg.Destroy()
  1739. return 'Datum transform is required.'
  1740. self.datumtrans = dtrans
  1741. ret, msg = gcmd.RunCommand('g.proj',
  1742. flags = 'c',
  1743. proj4 = proj4string,
  1744. location = self.startpage.location,
  1745. datumtrans = self.datumtrans,
  1746. getErrorMsg = True)
  1747. if ret == 0:
  1748. return ''
  1749. return msg
  1750. # # creating location from PROJ.4 string passed to g.proj
  1751. # ret, msg = gcmd.RunCommand('g.proj',
  1752. # flags = 'c',
  1753. # proj4 = proj4string,
  1754. # location = self.startpage.location,
  1755. # getErrorMsg = True)
  1756. # if ret == 0:
  1757. # return ''
  1758. # return msg
  1759. def CustomCreate(self):
  1760. """!Create a new location based on given proj4 string
  1761. @return error message (empty string on success)
  1762. """
  1763. proj4string = self.custompage.customstring
  1764. location = self.startpage.location
  1765. ret, msg = gcmd.RunCommand('g.proj',
  1766. flags = 'c',
  1767. proj4 = proj4string,
  1768. location = location,
  1769. getErrorMsg = True)
  1770. if ret == 0:
  1771. return ''
  1772. return msg
  1773. def EPSGCreate(self):
  1774. """!Create a new location from an EPSG code.
  1775. @return error message (empty string on success)
  1776. """
  1777. epsgcode = self.epsgpage.epsgcode
  1778. epsgdesc = self.epsgpage.epsgdesc
  1779. location = self.startpage.location
  1780. # should not happend
  1781. if epsgcode == '':
  1782. return _('EPSG code missing.')
  1783. # creating location
  1784. ret = gcmd.RunCommand('g.proj',
  1785. read = True,
  1786. epsg = epsgcode,
  1787. datumtrans = '-1')
  1788. dtoptions = {}
  1789. if ret:
  1790. line = ret.splitlines()
  1791. i = 0
  1792. while i < len(line):
  1793. if line[i] == '---':
  1794. for j in range(3):
  1795. dtoptions[line[i+1]] = (line[i+2],
  1796. line[i+3],
  1797. line[i+4])
  1798. i += 5
  1799. if dtoptions != {}:
  1800. dtrans = ''
  1801. # open a dialog to select datum transform number
  1802. dlg = SelectDatumDialog(self.parent, datums=dtoptions)
  1803. if dlg.ShowModal() == wx.ID_OK:
  1804. dtrans = dlg.GetDatum()
  1805. if dtrans == '':
  1806. dlg.Destroy()
  1807. return 'Datum transform is required.'
  1808. else:
  1809. dlg.Destroy()
  1810. return 'Datum transform is required.'
  1811. datumtrans = dtrans
  1812. else:
  1813. datumtrans = '1'
  1814. ret, msg = gcmd.RunCommand('g.proj',
  1815. flags = 'c',
  1816. epsg = epsgcode,
  1817. location = location,
  1818. datumtrans = datumtrans,
  1819. getErrorMsg = True)
  1820. if ret == 0:
  1821. return ''
  1822. return msg
  1823. def FileCreate(self):
  1824. """!Create a new location from a georeferenced file
  1825. @return error message (empty string on success)
  1826. """
  1827. georeffile = self.filepage.georeffile
  1828. location = self.startpage.location
  1829. # this should not happen
  1830. if not georeffile or not os.path.isfile(georeffile):
  1831. return _("File not found.")
  1832. # creating location
  1833. ret, msg = gcmd.RunCommand('g.proj',
  1834. flags = 'c',
  1835. georef = georeffile,
  1836. location = location,
  1837. getErrorMsg = True)
  1838. if ret == 0:
  1839. return ''
  1840. return msg
  1841. def WKTCreate(self):
  1842. """!Create a new location from a WKT file
  1843. @return error message (empty string on success)
  1844. """
  1845. wktfile = self.wktpage.wktfile
  1846. location = self.startpage.location
  1847. # this should not happen
  1848. if not wktfile or not os.path.isfile(wktfile):
  1849. return _("File not found.")
  1850. # creating location
  1851. ret, msg = gcmd.RunCommand('g.proj',
  1852. flags = 'c',
  1853. wkt = wktfile,
  1854. location = location,
  1855. getErrorMsg = True)
  1856. if ret == 0:
  1857. return ''
  1858. return msg
  1859. class RegionDef(BaseClass, wx.Frame):
  1860. """
  1861. Page for setting default region extents and resolution
  1862. """
  1863. def __init__(self, parent, id=wx.ID_ANY,
  1864. title=_("Set default region extent and resolution"), location=None):
  1865. wx.Frame.__init__(self, parent, id, title, size=(650,300))
  1866. panel = wx.Panel(self, id=wx.ID_ANY)
  1867. self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, 'grass.ico'), wx.BITMAP_TYPE_ICO))
  1868. self.parent = parent
  1869. self.location = location
  1870. #
  1871. # default values
  1872. #
  1873. # 2D
  1874. self.north = 1.0
  1875. self.south = 0.0
  1876. self.east = 1.0
  1877. self.west = 0.0
  1878. self.nsres = 1.0
  1879. self.ewres = 1.0
  1880. # 3D
  1881. self.top = 1.0
  1882. self.bottom = 0.0
  1883. # self.nsres3 = 1.0
  1884. # self.ewres3 = 1.0
  1885. self.tbres = 1.0
  1886. #
  1887. # inputs
  1888. #
  1889. # 2D
  1890. self.tnorth = self.MakeTextCtrl(text=str(self.north), size=(150, -1), parent=panel)
  1891. self.tsouth = self.MakeTextCtrl(str(self.south), size=(150, -1), parent=panel)
  1892. self.twest = self.MakeTextCtrl(str(self.west), size=(150, -1), parent=panel)
  1893. self.teast = self.MakeTextCtrl(str(self.east), size=(150, -1), parent=panel)
  1894. self.tnsres = self.MakeTextCtrl(str(self.nsres), size=(150, -1), parent=panel)
  1895. self.tewres = self.MakeTextCtrl(str(self.ewres), size=(150, -1), parent=panel)
  1896. #
  1897. # labels
  1898. #
  1899. self.lrows = self.MakeLabel(parent=panel)
  1900. self.lcols = self.MakeLabel(parent=panel)
  1901. self.lcells = self.MakeLabel(parent=panel)
  1902. #
  1903. # buttons
  1904. #
  1905. self.bset = self.MakeButton(text=_("&Set region"), id=wx.ID_OK, parent=panel)
  1906. self.bcancel = wx.Button(panel, id=wx.ID_CANCEL)
  1907. self.bset.SetDefault()
  1908. #
  1909. # image
  1910. #
  1911. self.img = wx.Image(os.path.join(globalvar.ETCWXDIR, "images",
  1912. "qgis_world.png"), wx.BITMAP_TYPE_PNG).ConvertToBitmap()
  1913. #
  1914. # set current working environment to PERMANENT mapset
  1915. # in selected location in order to set default region (WIND)
  1916. #
  1917. envval = {}
  1918. ret = gcmd.RunCommand('g.gisenv',
  1919. read = True)
  1920. if ret:
  1921. for line in ret.splitlines():
  1922. key, val = line.split('=')
  1923. envval[key] = val
  1924. self.currlocation = envval['LOCATION_NAME'].strip("';")
  1925. self.currmapset = envval['MAPSET'].strip("';")
  1926. if self.currlocation != self.location or self.currmapset != 'PERMANENT':
  1927. gcmd.RunCommand('g.gisenv',
  1928. set = 'LOCATION_NAME=%s' % self.location)
  1929. gcmd.RunCommand('g.gisenv',
  1930. set = 'MAPSET=PERMANENT')
  1931. else:
  1932. dlg = wx.MessageBox(parent=self,
  1933. message=_('Invalid location selected.'),
  1934. caption=_("Error"), style=wx.ID_OK | wx.ICON_ERROR)
  1935. return
  1936. #
  1937. # get current region settings
  1938. #
  1939. region = {}
  1940. ret = gcmd.RunCommand('g.region',
  1941. read = True,
  1942. flags = 'gp3')
  1943. if ret:
  1944. for line in ret.splitlines():
  1945. key, val = line.split('=')
  1946. region[key] = float(val)
  1947. else:
  1948. dlg = wx.MessageBox(parent=self,
  1949. message=_("Invalid region"),
  1950. caption=_("Error"), style=wx.ID_OK | wx.ICON_ERROR)
  1951. dlg.ShowModal()
  1952. dlg.Destroy()
  1953. return
  1954. #
  1955. # update values
  1956. # 2D
  1957. self.north = float(region['n'])
  1958. self.south = float(region['s'])
  1959. self.east = float(region['e'])
  1960. self.west = float(region['w'])
  1961. self.nsres = float(region['nsres'])
  1962. self.ewres = float(region['ewres'])
  1963. self.rows = int(region['rows'])
  1964. self.cols = int(region['cols'])
  1965. self.cells = int(region['cells'])
  1966. # 3D
  1967. self.top = float(region['t'])
  1968. self.bottom = float(region['b'])
  1969. # self.nsres3 = float(region['nsres3'])
  1970. # self.ewres3 = float(region['ewres3'])
  1971. self.tbres = float(region['tbres'])
  1972. self.depth = int(region['depths'])
  1973. self.cells3 = int(region['3dcells'])
  1974. #
  1975. # 3D box collapsable
  1976. #
  1977. self.infoCollapseLabelExp = _("Click here to show 3D settings")
  1978. self.infoCollapseLabelCol = _("Click here to hide 3D settings")
  1979. self.settings3D = wx.CollapsiblePane(parent=panel,
  1980. label=self.infoCollapseLabelExp,
  1981. style=wx.CP_DEFAULT_STYLE |
  1982. wx.CP_NO_TLW_RESIZE | wx.EXPAND)
  1983. self.MakeSettings3DPaneContent(self.settings3D.GetPane())
  1984. self.settings3D.Collapse(False) # FIXME
  1985. self.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED, self.OnSettings3DPaneChanged, self.settings3D)
  1986. #
  1987. # set current region settings
  1988. #
  1989. self.tnorth.SetValue(str(self.north))
  1990. self.tsouth.SetValue(str(self.south))
  1991. self.twest.SetValue(str(self.west))
  1992. self.teast.SetValue(str(self.east))
  1993. self.tnsres.SetValue(str(self.nsres))
  1994. self.tewres.SetValue(str(self.ewres))
  1995. self.ttop.SetValue(str(self.top))
  1996. self.tbottom.SetValue(str(self.bottom))
  1997. # self.tnsres3.SetValue(str(self.nsres3))
  1998. # self.tewres3.SetValue(str(self.ewres3))
  1999. self.ttbres.SetValue(str(self.tbres))
  2000. self.lrows.SetLabel(_("Rows: %d" % self.rows))
  2001. self.lcols.SetLabel(_("Cols: %d" % self.cols))
  2002. self.lcells.SetLabel(_("Cells: %d" % self.cells))
  2003. #
  2004. # bindings
  2005. #
  2006. self.Bind(wx.EVT_BUTTON, self.OnSetButton, self.bset)
  2007. self.Bind(wx.EVT_BUTTON, self.OnCancel, self.bcancel)
  2008. self.tnorth.Bind(wx.EVT_TEXT, self.OnValue)
  2009. self.tsouth.Bind(wx.EVT_TEXT, self.OnValue)
  2010. self.teast.Bind(wx.EVT_TEXT, self.OnValue)
  2011. self.twest.Bind(wx.EVT_TEXT, self.OnValue)
  2012. self.tnsres.Bind(wx.EVT_TEXT, self.OnValue)
  2013. self.tewres.Bind(wx.EVT_TEXT, self.OnValue)
  2014. self.ttop.Bind(wx.EVT_TEXT, self.OnValue)
  2015. self.tbottom.Bind(wx.EVT_TEXT, self.OnValue)
  2016. # self.tnsres3.Bind(wx.EVT_TEXT, self.OnValue)
  2017. # self.tewres3.Bind(wx.EVT_TEXT, self.OnValue)
  2018. self.ttbres.Bind(wx.EVT_TEXT, self.OnValue)
  2019. self.__DoLayout(panel)
  2020. self.SetMinSize(self.GetBestSize())
  2021. self.minWindowSize = self.GetMinSize()
  2022. def MakeSettings3DPaneContent(self, pane):
  2023. """!Create 3D region settings pane"""
  2024. border = wx.BoxSizer(wx.VERTICAL)
  2025. gridSizer = wx.GridBagSizer(vgap=0, hgap=0)
  2026. # inputs
  2027. self.ttop = wx.TextCtrl(parent=pane, id=wx.ID_ANY, value=str(self.top),
  2028. size=(150, -1))
  2029. self.tbottom = wx.TextCtrl(parent=pane, id=wx.ID_ANY, value=str(self.bottom),
  2030. size=(150, -1))
  2031. self.ttbres = wx.TextCtrl(parent=pane, id=wx.ID_ANY, value=str(self.tbres),
  2032. size=(150, -1))
  2033. # self.tnsres3 = wx.TextCtrl(parent=pane, id=wx.ID_ANY, value=str(self.nsres3),
  2034. # size=(150, -1))
  2035. # self.tewres3 = wx.TextCtrl(parent=pane, id=wx.ID_ANY, value=str(self.ewres3),
  2036. # size=(150, -1))
  2037. #labels
  2038. self.ldepth = wx.StaticText(parent=pane, label=_("Depth: %d") % self.depth)
  2039. self.lcells3 = wx.StaticText(parent=pane, label=_("3D Cells: %d") % self.cells3)
  2040. # top
  2041. gridSizer.Add(item=wx.StaticText(parent=pane, label=_("Top")),
  2042. flag=wx.ALIGN_CENTER |
  2043. wx.LEFT | wx.RIGHT | wx.TOP, border=5,
  2044. pos=(0, 1))
  2045. gridSizer.Add(item=self.ttop,
  2046. flag=wx.ALIGN_CENTER_HORIZONTAL |
  2047. wx.ALL, border=5, pos=(1, 1))
  2048. # bottom
  2049. gridSizer.Add(item=wx.StaticText(parent=pane, label=_("Bottom")),
  2050. flag=wx.ALIGN_CENTER |
  2051. wx.LEFT | wx.RIGHT | wx.TOP, border=5,
  2052. pos=(0, 2))
  2053. gridSizer.Add(item=self.tbottom,
  2054. flag=wx.ALIGN_CENTER_HORIZONTAL |
  2055. wx.ALL, border=5, pos=(1, 2))
  2056. # tbres
  2057. gridSizer.Add(item=wx.StaticText(parent=pane, label=_("T-B resolution")),
  2058. flag=wx.ALIGN_CENTER |
  2059. wx.LEFT | wx.RIGHT | wx.TOP, border=5,
  2060. pos=(0, 3))
  2061. gridSizer.Add(item=self.ttbres,
  2062. flag=wx.ALIGN_CENTER_HORIZONTAL |
  2063. wx.ALL, border=5, pos=(1, 3))
  2064. # res
  2065. # gridSizer.Add(item=wx.StaticText(parent=pane, label=_("3D N-S resolution")),
  2066. # flag=wx.ALIGN_CENTER |
  2067. # wx.LEFT | wx.RIGHT | wx.TOP, border=5,
  2068. # pos=(2, 1))
  2069. # gridSizer.Add(item=self.tnsres3,
  2070. # flag=wx.ALIGN_CENTER_HORIZONTAL |
  2071. # wx.ALL, border=5, pos=(3, 1))
  2072. # gridSizer.Add(item=wx.StaticText(parent=pane, label=_("3D E-W resolution")),
  2073. # flag=wx.ALIGN_CENTER |
  2074. # wx.LEFT | wx.RIGHT | wx.TOP, border=5,
  2075. # pos=(2, 3))
  2076. # gridSizer.Add(item=self.tewres3,
  2077. # flag=wx.ALIGN_CENTER_HORIZONTAL |
  2078. # wx.ALL, border=5, pos=(3, 3))
  2079. # rows/cols/cells
  2080. gridSizer.Add(item=self.ldepth,
  2081. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  2082. wx.ALL, border=5, pos=(2, 1))
  2083. gridSizer.Add(item=self.lcells3,
  2084. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  2085. wx.ALL, border=5, pos=(2, 2))
  2086. border.Add(item=gridSizer, proportion=1,
  2087. flag=wx.ALL | wx.ALIGN_CENTER | wx.EXPAND, border=5)
  2088. pane.SetSizer(border)
  2089. border.Fit(pane)
  2090. def OnSettings3DPaneChanged(self, event):
  2091. """!Collapse 3D settings box"""
  2092. if self.settings3D.IsExpanded():
  2093. self.settings3D.SetLabel(self.infoCollapseLabelCol)
  2094. self.Layout()
  2095. self.SetSize(self.GetBestSize())
  2096. self.SetMinSize(self.GetSize())
  2097. else:
  2098. self.settings3D.SetLabel(self.infoCollapseLabelExp)
  2099. self.Layout()
  2100. self.SetSize(self.minWindowSize)
  2101. self.SetMinSize(self.minWindowSize)
  2102. self.SendSizeEvent()
  2103. def __DoLayout(self, panel):
  2104. """!Window layout"""
  2105. frameSizer = wx.BoxSizer(wx.VERTICAL)
  2106. gridSizer = wx.GridBagSizer(vgap=0, hgap=0)
  2107. settings3DSizer = wx.BoxSizer(wx.VERTICAL)
  2108. buttonSizer = wx.BoxSizer(wx.HORIZONTAL)
  2109. # north
  2110. gridSizer.Add(item=self.MakeLabel(text=_("North"), parent=panel),
  2111. flag=wx.ALIGN_BOTTOM | wx.ALIGN_CENTER_HORIZONTAL |
  2112. wx.TOP | wx.LEFT | wx.RIGHT, border=5, pos=(0, 2))
  2113. gridSizer.Add(item=self.tnorth,
  2114. flag=wx.ALIGN_CENTER_HORIZONTAL |
  2115. wx.ALIGN_CENTER_VERTICAL |
  2116. wx.ALL, border=5, pos=(1, 2))
  2117. # west
  2118. gridSizer.Add(item=self.MakeLabel(text=_("West"), parent=panel),
  2119. flag=wx.ALIGN_RIGHT |
  2120. wx.ALIGN_CENTER_VERTICAL |
  2121. wx.LEFT | wx.TOP | wx.BOTTOM, border=5, pos=(2, 0))
  2122. gridSizer.Add(item=self.twest,
  2123. flag=wx.ALIGN_RIGHT |
  2124. wx.ALIGN_CENTER_VERTICAL |
  2125. wx.ALL, border=5, pos=(2, 1))
  2126. gridSizer.Add(item=wx.StaticBitmap(panel, wx.ID_ANY, self.img, (-1, -1),
  2127. (self.img.GetWidth(), self.img.GetHeight())),
  2128. flag=wx.ALIGN_CENTER |
  2129. wx.ALIGN_CENTER_VERTICAL |
  2130. wx.ALL, border=5, pos=(2, 2))
  2131. # east
  2132. gridSizer.Add(item=self.teast,
  2133. flag=wx.ALIGN_CENTER_HORIZONTAL |
  2134. wx.ALIGN_CENTER_VERTICAL |
  2135. wx.ALL, border=5, pos=(2, 3))
  2136. gridSizer.Add(item=self.MakeLabel(text=_("East"), parent=panel),
  2137. flag=wx.ALIGN_LEFT |
  2138. wx.ALIGN_CENTER_VERTICAL |
  2139. wx.RIGHT | wx.TOP | wx.BOTTOM, border=5, pos=(2, 4))
  2140. # south
  2141. gridSizer.Add(item=self.tsouth,
  2142. flag=wx.ALIGN_CENTER_HORIZONTAL |
  2143. wx.ALIGN_CENTER_VERTICAL |
  2144. wx.ALL, border=5, pos=(3, 2))
  2145. gridSizer.Add(item=self.MakeLabel(text=_("South"), parent=panel),
  2146. flag=wx.ALIGN_TOP | wx.ALIGN_CENTER_HORIZONTAL |
  2147. wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5, pos=(4, 2))
  2148. # ns-res
  2149. gridSizer.Add(item=self.MakeLabel(text=_("N-S resolution"), parent=panel),
  2150. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  2151. wx.TOP | wx.LEFT | wx.RIGHT, border=5, pos=(5, 1))
  2152. gridSizer.Add(item=self.tnsres,
  2153. flag=wx.ALIGN_RIGHT |
  2154. wx.ALIGN_CENTER_VERTICAL |
  2155. wx.ALL, border=5, pos=(6, 1))
  2156. # ew-res
  2157. gridSizer.Add(item=self.MakeLabel(text=_("E-W resolution"), parent=panel),
  2158. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  2159. wx.TOP | wx.LEFT | wx.RIGHT, border=5, pos=(5, 3))
  2160. gridSizer.Add(item=self.tewres,
  2161. flag=wx.ALIGN_RIGHT |
  2162. wx.ALIGN_CENTER_VERTICAL |
  2163. wx.ALL, border=5, pos=(6, 3))
  2164. # rows/cols/cells
  2165. gridSizer.Add(item=self.lrows,
  2166. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  2167. wx.ALL, border=5, pos=(7, 1))
  2168. gridSizer.Add(item=self.lcells,
  2169. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  2170. wx.ALL, border=5, pos=(7, 2))
  2171. gridSizer.Add(item=self.lcols,
  2172. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  2173. wx.ALL, border=5, pos=(7, 3))
  2174. # 3D
  2175. settings3DSizer.Add(item=self.settings3D,
  2176. flag=wx.ALL,
  2177. border=5)
  2178. # buttons
  2179. buttonSizer.Add(item=self.bcancel, proportion=1,
  2180. flag=wx.ALIGN_RIGHT |
  2181. wx.ALIGN_CENTER_VERTICAL |
  2182. wx.ALL, border=10)
  2183. buttonSizer.Add(item=self.bset, proportion=1,
  2184. flag=wx.ALIGN_CENTER |
  2185. wx.ALIGN_CENTER_VERTICAL |
  2186. wx.ALL, border=10)
  2187. frameSizer.Add(item=gridSizer, proportion=1,
  2188. flag=wx.ALL | wx.ALIGN_CENTER, border=5)
  2189. frameSizer.Add(item=settings3DSizer, proportion=0,
  2190. flag=wx.ALL | wx.ALIGN_CENTER, border=5)
  2191. frameSizer.Add(item=buttonSizer, proportion=0,
  2192. flag=wx.ALL | wx.ALIGN_RIGHT, border=5)
  2193. self.SetAutoLayout(True)
  2194. panel.SetSizer(frameSizer)
  2195. frameSizer.Fit(panel)
  2196. self.Layout()
  2197. def OnValue(self, event):
  2198. """!Set given value"""
  2199. try:
  2200. if event.GetId() == self.tnorth.GetId():
  2201. self.north = float(event.GetString())
  2202. elif event.GetId() == self.tsouth.GetId():
  2203. self.south = float(event.GetString())
  2204. elif event.GetId() == self.teast.GetId():
  2205. self.east = float(event.GetString())
  2206. elif event.GetId() == self.twest.GetId():
  2207. self.west = float(event.GetString())
  2208. elif event.GetId() == self.tnsres.GetId():
  2209. self.nsres = float(event.GetString())
  2210. elif event.GetId() == self.tewres.GetId():
  2211. self.ewres = float(event.GetString())
  2212. elif event.GetId() == self.ttop.GetId():
  2213. self.top = float(event.GetString())
  2214. elif event.GetId() == self.tbottom.GetId():
  2215. self.bottom = float(event.GetString())
  2216. # elif event.GetId() == self.tnsres3.GetId():
  2217. # self.nsres3 = float(event.GetString())
  2218. # elif event.GetId() == self.tewres3.GetId():
  2219. # self.ewres3 = float(event.GetString())
  2220. elif event.GetId() == self.ttbres.GetId():
  2221. self.tbres = float(event.GetString())
  2222. self.__UpdateInfo()
  2223. except ValueError, e:
  2224. if len(event.GetString()) > 0 and event.GetString() != '-':
  2225. dlg = wx.MessageBox(parent=self,
  2226. message=_("Invalid value: %s") % e,
  2227. caption=_("Error"),
  2228. style=wx.OK | wx.ICON_ERROR)
  2229. # reset values
  2230. self.tnorth.SetValue(str(self.north))
  2231. self.tsouth.SetValue(str(self.south))
  2232. self.teast.SetValue(str(self.east))
  2233. self.twest.SetValue(str(self.west))
  2234. self.tnsres.SetValue(str(self.nsres))
  2235. self.tewres.SetValue(str(self.ewres))
  2236. self.ttop.SetValue(str(self.top))
  2237. self.tbottom.SetValue(str(self.bottom))
  2238. self.ttbres.SetValue(str(self.tbres))
  2239. # self.tnsres3.SetValue(str(self.nsres3))
  2240. # self.tewres3.SetValue(str(self.ewres3))
  2241. event.Skip()
  2242. def __UpdateInfo(self):
  2243. """!Update number of rows/cols/cells"""
  2244. self.rows = int((self.north - self.south) / self.nsres)
  2245. self.cols = int((self.east - self.west) / self.ewres)
  2246. self.cells = self.rows * self.cols
  2247. self.depth = int((self.top - self.bottom) / self.tbres)
  2248. self.cells3 = self.rows * self.cols * self.depth
  2249. # 2D
  2250. self.lrows.SetLabel(_("Rows: %d") % self.rows)
  2251. self.lcols.SetLabel(_("Cols: %d") % self.cols)
  2252. self.lcells.SetLabel(_("Cells: %d") % self.cells)
  2253. # 3D
  2254. self.ldepth.SetLabel(_("Depth: %d" % self.depth))
  2255. self.lcells3.SetLabel(_("3D Cells: %d" % self.cells3))
  2256. def OnSetButton(self, event=None):
  2257. """!Set default region"""
  2258. ret = gcmd.RunCommand('g.region',
  2259. flags = 'sgpa',
  2260. n = self.north,
  2261. s = self.south,
  2262. e = self.east,
  2263. w = self.west,
  2264. nsres = self.nsres,
  2265. ewres = self.ewres,
  2266. t = self.top,
  2267. b = self.bottom,
  2268. tbres = self.tbres)
  2269. if ret == 0:
  2270. self.Destroy()
  2271. def OnCancel(self, event):
  2272. self.Destroy()
  2273. class SelectDatumDialog(wx.Dialog):
  2274. """!Dialog for selecting datum transformations"""
  2275. def __init__(self, parent, datums, title=_("Select datum transformation"),
  2276. pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_DIALOG_STYLE):
  2277. self.datums = datums
  2278. wx.Dialog.__init__(self, parent, wx.ID_ANY, title, pos, size, style)
  2279. panel = wx.Panel(self, wx.ID_ANY)
  2280. sizer = wx.BoxSizer(wx.VERTICAL)
  2281. #
  2282. # dialog body
  2283. #
  2284. bodyBox = wx.StaticBox(parent=panel, id=wx.ID_ANY,
  2285. label=" %s " % _("List of datum transformations"))
  2286. bodySizer = wx.StaticBoxSizer(bodyBox, wx.HORIZONTAL)
  2287. gridSizer = wx.GridBagSizer(vgap=5, hgap=5)
  2288. gridSizer.Add(item=wx.StaticText(parent=panel, label=_("Datums (select to see description):")),
  2289. flag=wx.ALIGN_CENTER_VERTICAL, pos=(0, 0))
  2290. items = self.datums.keys()
  2291. utils.ListSortLower(items)
  2292. self.cdatums = wx.ComboBox(parent=panel, id=wx.ID_ANY,
  2293. style=wx.CB_SIMPLE | wx.CB_READONLY,
  2294. choices=items,
  2295. size=(60,-1))
  2296. self.cdatums.SetSelection(0)
  2297. self.cdatums.Bind(wx.EVT_COMBOBOX, self.OnChangeDatum)
  2298. gridSizer.Add(item=self.cdatums, pos=(0, 1))
  2299. self.textWidth = self.GetSize()[0]
  2300. self.datumDesc = wx.StaticText(parent=panel, size=(self.textWidth,-1),
  2301. label='\n'.join(self.datums[self.cdatums.GetStringSelection()]))
  2302. self.datumDesc.Wrap(self.textWidth)
  2303. gridSizer.Add(item=self.datumDesc, flag=wx.EXPAND,
  2304. pos=(1, 0), span=(1, 2))
  2305. bodySizer.Add(item=gridSizer, proportion=1,
  2306. flag=wx.ALL | wx.ALIGN_CENTER | wx.EXPAND, border=5)
  2307. #
  2308. # buttons
  2309. #
  2310. btnsizer = wx.StdDialogButtonSizer()
  2311. btn = wx.Button(parent=panel, id=wx.ID_OK)
  2312. btn.SetDefault()
  2313. btnsizer.AddButton(btn)
  2314. btn = wx.Button(parent=panel, id=wx.ID_CANCEL)
  2315. btnsizer.AddButton(btn)
  2316. btnsizer.Realize()
  2317. sizer.Add(item=bodySizer, proportion=1,
  2318. flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
  2319. sizer.Add(item=btnsizer, proportion=0,
  2320. flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
  2321. #
  2322. # set panel sizer
  2323. #
  2324. panel.SetSizer(sizer)
  2325. sizer.Fit(panel)
  2326. self.SetSize(self.GetBestSize())
  2327. def OnChangeDatum(self, event):
  2328. """!Datum changed, update description text"""
  2329. self.datumDesc.SetLabel('\n'.join(self.datums[event.GetString()]))
  2330. # self.textWidth = self.GetSize()[0]
  2331. # self.datumDesc.Wrap(self.textWidth)
  2332. event.Skip()
  2333. def GetDatum(self):
  2334. """!Get selected datum"""
  2335. return self.cdatums.GetStringSelection()
  2336. if __name__ == "__main__":
  2337. app = wx.PySimpleApp()
  2338. # gWizard = LocationWizard(None, "")
  2339. gWizard = RegionDef(None)
  2340. gWizzard.Show()
  2341. app.MainLoop()