location_wizard.py 103 KB

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