location_wizard.py 102 KB

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