wizard.py 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139
  1. """!
  2. @package location_wizard.wizard
  3. @brief Location wizard - creates a new GRASS Location. User can choose
  4. from multiple methods.
  5. Classes:
  6. - wizard::TitledPage
  7. - wizard::DatabasePage
  8. - wizard::CoordinateSystemPage
  9. - wizard::ProjectionsPage
  10. - wizard::ItemList
  11. - wizard::ProjParamsPage
  12. - wizard::DatumPage
  13. - wizard::EllipsePage
  14. - wizard::GeoreferencedFilePage
  15. - wizard::WKTPage
  16. - wizard::EPSGPage
  17. - wizard::CustomPage
  18. - wizard::SummaryPage
  19. - wizard::LocationWizard
  20. - wizard::WizardWithHelpButton
  21. (C) 2007-2011 by the GRASS Development Team
  22. This program is free software under the GNU General Public License
  23. (>=v2). Read the file COPYING that comes with GRASS for details.
  24. @author Michael Barton
  25. @author Jachym Cepicky
  26. @author Martin Landa <landa.martin gmail.com>
  27. """
  28. import os
  29. import locale
  30. import wx
  31. import wx.lib.mixins.listctrl as listmix
  32. import wx.wizard as wiz
  33. import wx.lib.scrolledpanel as scrolled
  34. from core import globalvar
  35. from core import utils
  36. from core.gcmd import RunCommand, GError, GMessage, GWarning
  37. from gui_core.ghelp import HelpFrame
  38. from location_wizard.base import BaseClass
  39. from location_wizard.dialogs import RegionDef, SelectTransformDialog
  40. from grass.script import core as grass
  41. global coordsys
  42. global north
  43. global south
  44. global east
  45. global west
  46. global resolution
  47. global wizerror
  48. global translist
  49. class TitledPage(BaseClass, wiz.WizardPageSimple):
  50. """!Class to make wizard pages. Generic methods to make labels,
  51. text entries, and buttons.
  52. """
  53. def __init__(self, parent, title):
  54. self.page = wiz.WizardPageSimple.__init__(self, parent)
  55. # page title
  56. self.title = wx.StaticText(parent = self, id = wx.ID_ANY, label = title)
  57. self.title.SetFont(wx.Font(13, wx.SWISS, wx.NORMAL, wx.BOLD))
  58. # main sizers
  59. self.pagesizer = wx.BoxSizer(wx.VERTICAL)
  60. self.sizer = wx.GridBagSizer(vgap = 0, hgap = 0)
  61. def DoLayout(self):
  62. """!Do page layout"""
  63. self.pagesizer.Add(item = self.title, proportion = 0,
  64. flag = wx.ALIGN_CENTRE | wx.ALL,
  65. border = 5)
  66. self.pagesizer.Add(item = wx.StaticLine(self, -1), proportion = 0,
  67. flag = wx.EXPAND | wx.ALL,
  68. border = 0)
  69. self.pagesizer.Add(item = self.sizer, proportion = 1,
  70. flag = wx.EXPAND)
  71. self.SetAutoLayout(True)
  72. self.SetSizer(self.pagesizer)
  73. self.Layout()
  74. class DatabasePage(TitledPage):
  75. """!Wizard page for setting GIS data directory and location name"""
  76. def __init__(self, wizard, parent, grassdatabase):
  77. TitledPage.__init__(self, wizard, _("Define GRASS Database and Location Name"))
  78. self.grassdatabase = grassdatabase
  79. self.location = ''
  80. self.locTitle = ''
  81. # buttons
  82. self.bbrowse = self.MakeButton(_("Browse"))
  83. # text controls
  84. self.tgisdbase = self.MakeTextCtrl(grassdatabase, size = (300, -1))
  85. self.tlocation = self.MakeTextCtrl("newLocation", size = (300, -1))
  86. self.tlocTitle = self.MakeTextCtrl(size = (400, -1))
  87. # layout
  88. self.sizer.AddGrowableCol(3)
  89. self.sizer.Add(item = self.MakeLabel(_("GIS Data Directory:")),
  90. flag = wx.ALIGN_RIGHT |
  91. wx.ALIGN_CENTER_VERTICAL |
  92. wx.ALL, border = 5,
  93. pos = (1, 1))
  94. self.sizer.Add(item = self.tgisdbase,
  95. flag = wx.ALIGN_LEFT |
  96. wx.ALIGN_CENTER_VERTICAL |
  97. wx.ALL, border = 5,
  98. pos = (1, 2))
  99. self.sizer.Add(item = self.bbrowse,
  100. flag = wx.ALIGN_LEFT |
  101. wx.ALIGN_CENTER_VERTICAL |
  102. wx.ALL, border = 5,
  103. pos = (1, 3))
  104. self.sizer.Add(item = self.MakeLabel("%s:" % _("Project Location"),
  105. tooltip = _("Name of location directory in GIS Data Directory")),
  106. flag = wx.ALIGN_RIGHT |
  107. wx.ALIGN_CENTER_VERTICAL |
  108. wx.ALL, border = 5,
  109. pos = (2, 1))
  110. self.sizer.Add(item = self.tlocation,
  111. flag = wx.ALIGN_LEFT |
  112. wx.ALIGN_CENTER_VERTICAL |
  113. wx.ALL, border = 5,
  114. pos = (2, 2))
  115. self.sizer.Add(item = self.MakeLabel("%s:" % _("Location Title"),
  116. tooltip = _("Optional location title, "
  117. "you can leave this field blank.")),
  118. flag = wx.ALIGN_RIGHT |
  119. wx.ALIGN_TOP | wx.ALIGN_CENTER_VERTICAL |
  120. wx.ALL, border = 5,
  121. pos = (3, 1))
  122. self.sizer.Add(item = self.tlocTitle,
  123. flag = wx.ALIGN_LEFT |
  124. wx.ALIGN_CENTER_VERTICAL |
  125. wx.ALL, border = 5,
  126. pos = (3, 2), span = (1, 2))
  127. # bindings
  128. self.Bind(wx.EVT_BUTTON, self.OnBrowse, self.bbrowse)
  129. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  130. self.tgisdbase.Bind(wx.EVT_TEXT, self.OnChangeName)
  131. self.tlocation.Bind(wx.EVT_TEXT, self.OnChangeName)
  132. def OnChangeName(self, event):
  133. """!Name for new location was changed"""
  134. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  135. if len(event.GetString()) > 0:
  136. if not nextButton.IsEnabled():
  137. nextButton.Enable()
  138. else:
  139. nextButton.Disable()
  140. event.Skip()
  141. def OnBrowse(self, event):
  142. """!Choose GRASS data directory"""
  143. dlg = wx.DirDialog(self, _("Choose GRASS data directory:"),
  144. os.getcwd(), wx.DD_DEFAULT_STYLE)
  145. if dlg.ShowModal() == wx.ID_OK:
  146. self.grassdatabase = dlg.GetPath()
  147. self.tgisdbase.SetValue(self.grassdatabase)
  148. dlg.Destroy()
  149. def OnPageChanging(self, event = None):
  150. error = None
  151. if os.path.isdir(os.path.join(self.tgisdbase.GetValue(), self.tlocation.GetValue())):
  152. error = _("Location already exists in GRASS Database.")
  153. if error:
  154. GError(parent = self,
  155. message="%s <%s>.%s%s" % (_("Unable to create location"),
  156. str(self.tlocation.GetValue()),
  157. os.linesep,
  158. error))
  159. event.Veto()
  160. return
  161. self.location = self.tlocation.GetValue()
  162. self.grassdatabase = self.tgisdbase.GetValue()
  163. self.locTitle = self.tlocTitle.GetValue()
  164. if os.linesep in self.locTitle or \
  165. len(self.locTitle) > 255:
  166. GWarning(parent = self,
  167. message = _("Title of the location is limited only to one line and "
  168. "256 characters. The rest of the text will be ignored."))
  169. self.locTitle = self.locTitle.split(os.linesep)[0][:255]
  170. class CoordinateSystemPage(TitledPage):
  171. """!Wizard page for choosing method for location creation"""
  172. def __init__(self, wizard, parent):
  173. TitledPage.__init__(self, wizard, _("Choose method for creating a new location"))
  174. self.parent = parent
  175. global coordsys
  176. # toggles
  177. self.radio1 = wx.RadioButton(parent = self, id = wx.ID_ANY,
  178. label = _("Select coordinate system parameters from a list"),
  179. style = wx.RB_GROUP)
  180. self.radio2 = wx.RadioButton(parent = self, id = wx.ID_ANY,
  181. label = _("Select EPSG code of spatial reference system"))
  182. self.radio3 = wx.RadioButton(parent = self, id = wx.ID_ANY,
  183. label = _("Read projection and datum terms from a "
  184. "georeferenced data file"))
  185. self.radio4 = wx.RadioButton(parent = self, id = wx.ID_ANY,
  186. label = _("Read projection and datum terms from a "
  187. "WKT or PRJ file"))
  188. self.radio5 = wx.RadioButton(parent = self, id = wx.ID_ANY,
  189. label = _("Specify projection and datum terms using custom "
  190. "PROJ.4 parameters"))
  191. self.radio6 = wx.RadioButton(parent = self, id = wx.ID_ANY,
  192. label = _("Create a generic Cartesian coordinate system (XY)"))
  193. # layout
  194. self.sizer.AddGrowableCol(1)
  195. self.sizer.SetVGap(10)
  196. self.sizer.Add(item = self.radio1,
  197. flag = wx.ALIGN_LEFT, pos = (1, 1))
  198. self.sizer.Add(item = self.radio2,
  199. flag = wx.ALIGN_LEFT, pos = (2, 1))
  200. self.sizer.Add(item = self.radio3,
  201. flag = wx.ALIGN_LEFT, pos = (3, 1))
  202. self.sizer.Add(item = self.radio4,
  203. flag = wx.ALIGN_LEFT, pos = (4, 1))
  204. self.sizer.Add(item = self.radio5,
  205. flag = wx.ALIGN_LEFT, pos = (5, 1))
  206. self.sizer.Add(item = self.radio6,
  207. flag = wx.ALIGN_LEFT, pos = (6, 1))
  208. # bindings
  209. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id = self.radio1.GetId())
  210. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id = self.radio2.GetId())
  211. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id = self.radio3.GetId())
  212. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id = self.radio4.GetId())
  213. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id = self.radio5.GetId())
  214. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id = self.radio6.GetId())
  215. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  216. def OnEnterPage(self, event):
  217. global coordsys
  218. if not coordsys:
  219. coordsys = "proj"
  220. self.radio1.SetValue(True)
  221. else:
  222. if coordsys == 'proj':
  223. self.radio1.SetValue(True)
  224. if coordsys == "epsg":
  225. self.radio2.SetValue(True)
  226. if coordsys == "file":
  227. self.radio3.SetValue(True)
  228. if coordsys == "wkt":
  229. self.radio4.SetValue(True)
  230. if coordsys == "custom":
  231. self.radio5.SetValue(True)
  232. if coordsys == "xy":
  233. self.radio6.SetValue(True)
  234. if event.GetDirection():
  235. if coordsys == 'proj':
  236. self.SetNext(self.parent.projpage)
  237. self.parent.sumpage.SetPrev(self.parent.datumpage)
  238. if coordsys == "epsg":
  239. self.SetNext(self.parent.epsgpage)
  240. self.parent.sumpage.SetPrev(self.parent.epsgpage)
  241. if coordsys == "file":
  242. self.SetNext(self.parent.filepage)
  243. self.parent.sumpage.SetPrev(self.parent.filepage)
  244. if coordsys == "wkt":
  245. self.SetNext(self.parent.wktpage)
  246. self.parent.sumpage.SetPrev(self.parent.wktpage)
  247. if coordsys == "custom":
  248. self.SetNext(self.parent.custompage)
  249. self.parent.sumpage.SetPrev(self.parent.custompage)
  250. if coordsys == "xy":
  251. self.SetNext(self.parent.sumpage)
  252. self.parent.sumpage.SetPrev(self.parent.csystemspage)
  253. if not wx.FindWindowById(wx.ID_FORWARD).IsEnabled():
  254. wx.FindWindowById(wx.ID_FORWARD).Enable()
  255. def SetVal(self, event):
  256. """!Choose method"""
  257. global coordsys
  258. if event.GetId() == self.radio1.GetId():
  259. coordsys = "proj"
  260. self.SetNext(self.parent.projpage)
  261. self.parent.sumpage.SetPrev(self.parent.datumpage)
  262. elif event.GetId() == self.radio2.GetId():
  263. coordsys = "epsg"
  264. self.SetNext(self.parent.epsgpage)
  265. self.parent.sumpage.SetPrev(self.parent.epsgpage)
  266. elif event.GetId() == self.radio3.GetId():
  267. coordsys = "file"
  268. self.SetNext(self.parent.filepage)
  269. self.parent.sumpage.SetPrev(self.parent.filepage)
  270. elif event.GetId() == self.radio4.GetId():
  271. coordsys = "wkt"
  272. self.SetNext(self.parent.wktpage)
  273. self.parent.sumpage.SetPrev(self.parent.wktpage)
  274. elif event.GetId() == self.radio5.GetId():
  275. coordsys = "custom"
  276. self.SetNext(self.parent.custompage)
  277. self.parent.sumpage.SetPrev(self.parent.custompage)
  278. elif event.GetId() == self.radio6.GetId():
  279. coordsys = "xy"
  280. self.SetNext(self.parent.sumpage)
  281. self.parent.sumpage.SetPrev(self.parent.csystemspage)
  282. class ProjectionsPage(TitledPage):
  283. """!Wizard page for selecting projection (select coordinate system option)"""
  284. def __init__(self, wizard, parent):
  285. TitledPage.__init__(self, wizard, _("Choose projection"))
  286. self.parent = parent
  287. self.proj = ''
  288. self.projdesc = ''
  289. self.p4proj = ''
  290. # text input
  291. self.tproj = self.MakeTextCtrl("", size = (200,-1))
  292. # search box
  293. self.searchb = wx.SearchCtrl(self, size = (200,-1),
  294. style = wx.TE_PROCESS_ENTER)
  295. # projection list
  296. self.projlist = ItemList(self, data = self.parent.projdesc.items(),
  297. columns = [_('Code'), _('Description')])
  298. self.projlist.resizeLastColumn(30)
  299. # layout
  300. self.sizer.AddGrowableCol(3)
  301. self.sizer.Add(item = self.MakeLabel(_("Projection code:")),
  302. flag = wx.ALIGN_LEFT |
  303. wx.ALIGN_CENTER_VERTICAL |
  304. wx.ALL, border = 5, pos = (1, 1))
  305. self.sizer.Add(item = self.tproj,
  306. flag = wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL,
  307. border = 5, pos = (1, 2))
  308. self.sizer.Add(item = self.MakeLabel(_("Search in description:")),
  309. flag = wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  310. border = 5, pos = (2, 1))
  311. self.sizer.Add(item = self.searchb,
  312. flag = wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL,
  313. border = 5, pos = (2, 2))
  314. self.sizer.AddGrowableRow(3)
  315. self.sizer.Add(item = self.projlist,
  316. flag = wx.EXPAND |
  317. wx.ALIGN_LEFT |
  318. wx.ALL, border = 5, pos = (3, 1), span = (1, 3))
  319. # events
  320. self.tproj.Bind(wx.EVT_TEXT, self.OnText)
  321. self.tproj.Bind(wx.EVT_TEXT_ENTER, self.OnText)
  322. self.searchb.Bind(wx.EVT_TEXT_ENTER, self.OnSearch)
  323. self.projlist.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
  324. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  325. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  326. def OnPageChanging(self,event):
  327. if event.GetDirection() and self.proj not in self.parent.projections.keys():
  328. event.Veto()
  329. def OnText(self, event):
  330. """!Projection name changed"""
  331. self.proj = event.GetString().lower()
  332. self.p4proj = ''
  333. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  334. if self.proj not in self.parent.projections.keys() and nextButton.IsEnabled():
  335. nextButton.Enable(False)
  336. if self.proj in self.parent.projections.keys():
  337. if self.proj == 'stp':
  338. wx.MessageBox('Currently State Plane projections must be selected using the '
  339. 'text-based setup (g.setproj), or entered by EPSG code or '
  340. 'custom PROJ.4 terms.',
  341. 'Warning', wx.ICON_WARNING)
  342. self.proj = ''
  343. self.tproj.SetValue(self.proj)
  344. nextButton.Enable(False)
  345. return
  346. elif self.proj.lower() == 'll':
  347. self.p4proj = '+proj=longlat'
  348. else:
  349. self.p4proj = '+proj=' + self.proj.lower()
  350. self.projdesc = self.parent.projections[self.proj][0]
  351. nextButton.Enable()
  352. def OnEnterPage(self, event):
  353. if len(self.proj) == 0:
  354. # disable 'next' button by default
  355. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  356. else:
  357. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  358. event.Skip()
  359. def OnSearch(self, event):
  360. """!Search projection by desc"""
  361. str = event.GetString()
  362. try:
  363. self.proj, self.projdesc = self.projlist.Search(index = [0,1], pattern = event.GetString())
  364. except:
  365. self.proj = self.projdesc = ''
  366. event.Skip()
  367. def OnItemSelected(self, event):
  368. """!Projection selected"""
  369. index = event.m_itemIndex
  370. # set values
  371. self.proj = self.projlist.GetItem(index, 0).GetText().lower()
  372. self.tproj.SetValue(self.proj)
  373. event.Skip()
  374. class ItemList(wx.ListCtrl,
  375. listmix.ListCtrlAutoWidthMixin,
  376. listmix.ColumnSorterMixin):
  377. """!Generic list (for projections, ellipsoids, etc.)"""
  378. def __init__(self, parent, columns, data = None):
  379. wx.ListCtrl.__init__(self, parent = parent, id = wx.ID_ANY,
  380. style = wx.LC_REPORT |
  381. wx.LC_VIRTUAL |
  382. wx.LC_HRULES |
  383. wx.LC_VRULES |
  384. wx.LC_SINGLE_SEL |
  385. wx.LC_SORT_ASCENDING, size = (550, 125))
  386. # original data or None
  387. self.sourceData = data
  388. #
  389. # insert columns
  390. #
  391. i = 0
  392. for column in columns:
  393. self.InsertColumn(i, column)
  394. i += 1
  395. if self.sourceData:
  396. self.Populate()
  397. for i in range(self.GetColumnCount()):
  398. self.SetColumnWidth(i, wx.LIST_AUTOSIZE_USEHEADER)
  399. if self.GetColumnWidth(i) < 80:
  400. self.SetColumnWidth(i, 80)
  401. #
  402. # listmix
  403. #
  404. listmix.ListCtrlAutoWidthMixin.__init__(self)
  405. listmix.ColumnSorterMixin.__init__(self, self.GetColumnCount())
  406. #
  407. # add some attributes
  408. #
  409. self.attr1 = wx.ListItemAttr()
  410. self.attr1.SetBackgroundColour(wx.Colour(238,238,238))
  411. self.attr2 = wx.ListItemAttr()
  412. self.attr2.SetBackgroundColour("white")
  413. self.il = wx.ImageList(16, 16)
  414. self.sm_up = self.il.Add(wx.ArtProvider_GetBitmap(wx.ART_GO_UP, wx.ART_TOOLBAR,
  415. (16,16)))
  416. self.sm_dn = self.il.Add(wx.ArtProvider_GetBitmap(wx.ART_GO_DOWN, wx.ART_TOOLBAR,
  417. (16,16)))
  418. self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
  419. #
  420. # sort by first column
  421. #
  422. if self.sourceData:
  423. self.SortListItems(col = 0, ascending = True)
  424. #
  425. # bindings
  426. #
  427. self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColumnClick)
  428. def Populate(self, data = None, update = False):
  429. """!Populate list"""
  430. self.itemDataMap = {}
  431. self.itemIndexMap = []
  432. if data is None:
  433. data = self.sourceData
  434. elif update:
  435. self.sourceData = data
  436. try:
  437. data.sort()
  438. self.DeleteAllItems()
  439. row = 0
  440. for value in data:
  441. self.itemDataMap[row] = [value[0]]
  442. for i in range(1, len(value)):
  443. self.itemDataMap[row].append(value[i])
  444. self.itemIndexMap.append(row)
  445. row += 1
  446. self.SetItemCount(row)
  447. # set column width
  448. self.SetColumnWidth(0, 80)
  449. self.SetColumnWidth(1, 300)
  450. self.SendSizeEvent()
  451. except StandardError, e:
  452. wx.MessageBox(parent = self,
  453. message = _("Unable to read list: %s") % e,
  454. caption = _("Error"), style = wx.OK | wx.ICON_ERROR)
  455. def OnColumnClick(self, event):
  456. """!Sort by column"""
  457. self._col = event.GetColumn()
  458. # remove duplicated arrow symbol from column header
  459. # FIXME: should be done automatically
  460. info = wx.ListItem()
  461. info.m_mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE
  462. info.m_image = -1
  463. for column in range(self.GetColumnCount()):
  464. info.m_text = self.GetColumn(column).GetText()
  465. self.SetColumn(column, info)
  466. event.Skip()
  467. def GetSortImages(self):
  468. """!Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py"""
  469. return (self.sm_dn, self.sm_up)
  470. def OnGetItemText(self, item, col):
  471. """!Get item text"""
  472. index = self.itemIndexMap[item]
  473. s = str(self.itemDataMap[index][col])
  474. return s
  475. def OnGetItemAttr(self, item):
  476. """!Get item attributes"""
  477. index = self.itemIndexMap[item]
  478. if ( index % 2) == 0:
  479. return self.attr2
  480. else:
  481. return self.attr1
  482. def SortItems(self, sorter = cmp):
  483. """!Sort items"""
  484. items = list(self.itemDataMap.keys())
  485. items.sort(self.Sorter)
  486. self.itemIndexMap = items
  487. # redraw the list
  488. self.Refresh()
  489. def Sorter(self, key1, key2):
  490. colName = self.GetColumn(self._col).GetText()
  491. ascending = self._colSortFlag[self._col]
  492. # convert always string
  493. item1 = self.itemDataMap[key1][self._col]
  494. item2 = self.itemDataMap[key2][self._col]
  495. if type(item1) == type('') or type(item2) == type(''):
  496. cmpVal = locale.strcoll(str(item1), str(item2))
  497. else:
  498. cmpVal = cmp(item1, item2)
  499. # If the items are equal then pick something else to make the sort value unique
  500. if cmpVal == 0:
  501. cmpVal = apply(cmp, self.GetSecondarySortValues(self._col, key1, key2))
  502. if ascending:
  503. return cmpVal
  504. else:
  505. return -cmpVal
  506. def GetListCtrl(self):
  507. """!Used by listmix.ColumnSorterMixin"""
  508. return self
  509. def Search (self, index, pattern):
  510. """!Search projection by description
  511. Return first found item or None
  512. """
  513. if pattern == '':
  514. self.Populate(self.sourceData)
  515. return []
  516. data = []
  517. pattern = pattern.lower()
  518. for i in range(len(self.sourceData)):
  519. for idx in index:
  520. try:
  521. value = str(self.sourceData[i][idx]).lower()
  522. if pattern in value:
  523. data.append(self.sourceData[i])
  524. break
  525. except UnicodeDecodeError:
  526. # osgeo4w problem (should be fixed)
  527. pass
  528. self.Populate(data)
  529. if len(data) > 0:
  530. return data[0]
  531. else:
  532. return []
  533. class ProjParamsPage(TitledPage):
  534. """!Wizard page for selecting method of setting coordinate system
  535. parameters (select coordinate system option)
  536. """
  537. def __init__(self, wizard, parent):
  538. TitledPage.__init__(self, wizard, _("Choose projection parameters"))
  539. global coordsys
  540. self.parent = parent
  541. self.panel = None
  542. self.prjParamSizer = None
  543. self.pparam = dict()
  544. self.p4projparams = ''
  545. self.projdesc = ''
  546. self.sizer.AddGrowableCol(1)
  547. self.sizer.AddGrowableRow(1)
  548. radioSBox = wx.StaticBox(parent = self, id = wx.ID_ANY,
  549. label = " %s " % _("Select datum or ellipsoid (next page)"))
  550. radioSBSizer = wx.StaticBoxSizer(radioSBox)
  551. self.sizer.Add(item = radioSBSizer, pos = (0, 1),
  552. flag = wx.EXPAND | wx.ALIGN_TOP | wx.TOP, border = 10)
  553. self.radio1 = wx.RadioButton(parent = self, id = wx.ID_ANY,
  554. label = _("Datum with associated ellipsoid"),
  555. style = wx.RB_GROUP)
  556. self.radio2 = wx.RadioButton(parent = self, id = wx.ID_ANY,
  557. label = _("Ellipsoid only"))
  558. # default button setting
  559. if self.radio1.GetValue() == False and self.radio2.GetValue() == False:
  560. self.radio1.SetValue(True)
  561. self.SetNext(self.parent.datumpage)
  562. # self.parent.sumpage.SetPrev(self.parent.datumpage)
  563. radioSBSizer.Add(item = self.radio1,
  564. flag = wx.ALIGN_LEFT | wx.RIGHT, border = 20)
  565. radioSBSizer.Add(item = self.radio2,
  566. flag = wx.ALIGN_LEFT)
  567. # bindings
  568. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id = self.radio1.GetId())
  569. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id = self.radio2.GetId())
  570. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChange)
  571. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  572. def OnParamEntry(self, event):
  573. """!Parameter value changed"""
  574. id = event.GetId()
  575. val = event.GetString()
  576. if id not in self.pparam:
  577. event.Skip()
  578. return
  579. param = self.pparam[id]
  580. win = self.FindWindowById(id)
  581. if param['type'] == 'zone':
  582. val = event.GetInt()
  583. if val < 1:
  584. win.SetValue(1)
  585. elif val > 60:
  586. win.SetValue(60)
  587. if param['type'] == 'bool':
  588. param['value'] = event.GetSelection()
  589. else:
  590. param['value'] = val
  591. event.Skip()
  592. def OnPageChange(self,event=None):
  593. """!Go to next page"""
  594. if event.GetDirection():
  595. self.p4projparams = ''
  596. for id, param in self.pparam.iteritems():
  597. if param['type'] == 'bool':
  598. if param['value'] == False:
  599. continue
  600. else:
  601. self.p4projparams += (' +' + param['proj4'])
  602. else:
  603. if param['value'] is None:
  604. wx.MessageBox(parent = self,
  605. message = _('You must enter a value for %s') % param['desc'],
  606. caption = _('Error'), style = wx.ICON_ERROR | wx.CENTRE)
  607. event.Veto()
  608. else:
  609. self.p4projparams += (' +' + param['proj4'] + '=' + str(param['value']))
  610. def OnEnterPage(self,event):
  611. """!Page entered"""
  612. self.projdesc = self.parent.projections[self.parent.projpage.proj][0]
  613. if self.prjParamSizer is None:
  614. # entering page for the first time
  615. self.paramSBox = wx.StaticBox(parent = self, id = wx.ID_ANY,
  616. label = _(" Enter parameters for %s projection ") % self.projdesc)
  617. paramSBSizer = wx.StaticBoxSizer(self.paramSBox)
  618. self.panel = scrolled.ScrolledPanel(parent = self, id = wx.ID_ANY)
  619. self.panel.SetupScrolling()
  620. self.prjParamSizer = wx.GridBagSizer(vgap = 0, hgap = 0)
  621. self.sizer.Add(item = paramSBSizer, pos = (1, 1),
  622. flag = wx.EXPAND)
  623. paramSBSizer.Add(item = self.panel, proportion = 1,
  624. flag = wx.ALIGN_CENTER | wx.EXPAND)
  625. paramSBSizer.Fit(self.panel)
  626. self.panel.SetSizer(self.prjParamSizer)
  627. if event.GetDirection():
  628. self.prjParamSizer.Clear(True)
  629. self.paramSBox.SetLabel(_(" Enter parameters for %s projection ") % self.projdesc)
  630. self.pparam = dict()
  631. row = 0
  632. for paramgrp in self.parent.projections[self.parent.projpage.proj][1]:
  633. # get parameters
  634. id = wx.NewId()
  635. param = self.pparam[id] = { 'type' : self.parent.paramdesc[paramgrp[0]][0],
  636. 'proj4': self.parent.paramdesc[paramgrp[0]][1],
  637. 'desc' : self.parent.paramdesc[paramgrp[0]][2] }
  638. # default values
  639. if param['type'] == 'bool':
  640. param['value'] = 0
  641. elif param['type'] == 'zone':
  642. param['value'] = 30
  643. param['desc'] += ' (1-60)'
  644. else:
  645. param['value'] = paramgrp[2]
  646. label = wx.StaticText(parent = self.panel, id = wx.ID_ANY, label = param['desc'],
  647. style = wx.ALIGN_RIGHT | wx.ST_NO_AUTORESIZE)
  648. if param['type'] == 'bool':
  649. win = wx.Choice(parent = self.panel, id = id, size = (100,-1),
  650. choices = [_('No'), _('Yes')])
  651. win.SetSelection(param['value'])
  652. win.Bind(wx.EVT_CHOICE, self.OnParamEntry)
  653. elif param['type'] == 'zone':
  654. win = wx.SpinCtrl(parent = self.panel, id = id,
  655. size = (100, -1),
  656. style = wx.SP_ARROW_KEYS | wx.SP_WRAP,
  657. min = 1, max = 60)
  658. win.SetValue(param['value'])
  659. win.Bind(wx.EVT_SPINCTRL, self.OnParamEntry)
  660. win.Bind(wx.EVT_TEXT, self.OnParamEntry)
  661. else:
  662. win = wx.TextCtrl(parent = self.panel, id = id,
  663. value = param['value'],
  664. size=(100, -1))
  665. win.Bind(wx.EVT_TEXT, self.OnParamEntry)
  666. if paramgrp[1] == 'noask':
  667. win.Enable(False)
  668. self.prjParamSizer.Add(item = label, pos = (row, 1),
  669. flag = wx.ALIGN_RIGHT |
  670. wx.ALIGN_CENTER_VERTICAL |
  671. wx.RIGHT, border = 5)
  672. self.prjParamSizer.Add(item = win, pos = (row, 2),
  673. flag = wx.ALIGN_LEFT |
  674. wx.ALIGN_CENTER_VERTICAL |
  675. wx.LEFT, border = 5)
  676. row += 1
  677. self.panel.SetSize(self.panel.GetBestSize())
  678. self.panel.Layout()
  679. self.Layout()
  680. self.Update()
  681. if not wx.FindWindowById(wx.ID_FORWARD).IsEnabled():
  682. wx.FindWindowById(wx.ID_FORWARD).Enable()
  683. event.Skip()
  684. def SetVal(self, event):
  685. """!Set value"""
  686. if event.GetId() == self.radio1.GetId():
  687. self.SetNext(self.parent.datumpage)
  688. self.parent.sumpage.SetPrev(self.parent.datumpage)
  689. elif event.GetId() == self.radio2.GetId():
  690. self.SetNext(self.parent.ellipsepage)
  691. self.parent.sumpage.SetPrev(self.parent.ellipsepage)
  692. class DatumPage(TitledPage):
  693. """!Wizard page for selecting datum (with associated ellipsoid)
  694. and datum transformation parameters (select coordinate system option)
  695. """
  696. def __init__(self, wizard, parent):
  697. TitledPage.__init__(self, wizard, _("Specify geodetic datum"))
  698. self.parent = parent
  699. self.datum = ''
  700. self.datumdesc = ''
  701. self.ellipse = ''
  702. self.datumparams = ''
  703. self.proj4params = ''
  704. # text input
  705. self.tdatum = self.MakeTextCtrl("", size = (200,-1))
  706. # search box
  707. self.searchb = wx.SearchCtrl(self, size = (200,-1),
  708. style = wx.TE_PROCESS_ENTER)
  709. # create list control for datum/elipsoid list
  710. data = []
  711. for key in self.parent.datums.keys():
  712. data.append([key, self.parent.datums[key][0], self.parent.datums[key][1]])
  713. self.datumlist = ItemList(self,
  714. data = data,
  715. columns = [_('Code'), _('Ellipsoid'), _('Description')])
  716. self.datumlist.resizeLastColumn(10)
  717. # layout
  718. self.sizer.AddGrowableCol(4)
  719. self.sizer.Add(item = self.MakeLabel(_("Datum code:")),
  720. flag = wx.ALIGN_LEFT |
  721. wx.ALIGN_CENTER_VERTICAL |
  722. wx.ALL, border = 5, pos = (1, 1))
  723. self.sizer.Add(item = self.tdatum,
  724. flag = wx.ALIGN_LEFT |
  725. wx.ALIGN_CENTER_VERTICAL |
  726. wx.ALL, border = 5, pos = (1, 2))
  727. self.sizer.Add(item = self.MakeLabel(_("Search in description:")),
  728. flag = wx.ALIGN_LEFT |
  729. wx.ALIGN_CENTER_VERTICAL |
  730. wx.ALL, border = 5, pos = (2, 1))
  731. self.sizer.Add(item = self.searchb,
  732. flag = wx.ALIGN_LEFT |
  733. wx.ALIGN_CENTER_VERTICAL |
  734. wx.ALL, border = 5, pos = (2, 2))
  735. self.sizer.AddGrowableRow(3)
  736. self.sizer.Add(item = self.datumlist,
  737. flag = wx.EXPAND |
  738. wx.ALIGN_LEFT |
  739. wx.ALL, border = 5, pos = (3, 1), span = (1, 4))
  740. # events
  741. self.datumlist.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnDatumSelected)
  742. self.searchb.Bind(wx.EVT_TEXT_ENTER, self.OnDSearch)
  743. self.tdatum.Bind(wx.EVT_TEXT, self.OnDText)
  744. self.tdatum.Bind(wx.EVT_TEXT_ENTER, self.OnDText)
  745. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  746. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  747. # do page layout
  748. # self.DoLayout()
  749. def OnPageChanging(self, event):
  750. self.proj4params = ''
  751. proj = self.parent.projpage.p4proj
  752. if event.GetDirection():
  753. if self.datum not in self.parent.datums:
  754. event.Veto()
  755. else:
  756. # check for datum tranforms
  757. # proj4string = self.parent.CreateProj4String() + ' +datum=%s' % self.datum
  758. ret = RunCommand('g.proj',
  759. read = True,
  760. proj4 = '%s +datum=%s' % (proj, self.datum),
  761. datumtrans = '-1')
  762. if ret != '':
  763. dtrans = ''
  764. # open a dialog to select datum transform number
  765. dlg = SelectTransformDialog(self.parent.parent, transforms=ret)
  766. if dlg.ShowModal() == wx.ID_OK:
  767. dtrans = dlg.GetTransform()
  768. if dtrans == '':
  769. dlg.Destroy()
  770. event.Veto()
  771. return 'Datum transform is required.'
  772. else:
  773. dlg.Destroy()
  774. event.Veto()
  775. return 'Datum transform is required.'
  776. self.parent.datumtrans = dtrans
  777. self.GetNext().SetPrev(self)
  778. self.parent.ellipsepage.ellipse = self.ellipse
  779. self.parent.ellipsepage.ellipseparams = self.parent.ellipsoids[self.ellipse][1]
  780. def OnEnterPage(self,event):
  781. self.parent.datumtrans = None
  782. if event.GetDirection():
  783. if len(self.datum) == 0:
  784. # disable 'next' button by default when entering from previous page
  785. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  786. else:
  787. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  788. event.Skip()
  789. def OnDText(self, event):
  790. """!Datum code changed"""
  791. self.datum = event.GetString()
  792. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  793. if len(self.datum) == 0 or self.datum not in self.parent.datums:
  794. nextButton.Enable(False)
  795. else:
  796. self.ellipse = self.parent.datums[self.datum][0]
  797. self.datumdesc = self.parent.datums[self.datum][1]
  798. self.datumparams = self.parent.datums[self.datum][2]
  799. try:
  800. self.datumparams.remove('dx=0.0')
  801. except:
  802. pass
  803. try:
  804. self.datumparams.remove('dy=0.0')
  805. except:
  806. pass
  807. try:
  808. self.datumparams.remove('dz=0.0')
  809. except:
  810. pass
  811. nextButton.Enable(True)
  812. self.Update()
  813. event.Skip()
  814. def OnDSearch(self, event):
  815. """!Search geodetic datum by desc"""
  816. str = self.searchb.GetValue()
  817. try:
  818. self.datum, self.ellipsoid, self.datumdesc = self.datumlist.Search(index = [0,1,2], pattern = str)
  819. except:
  820. self.datum = self.datumdesc = self.ellipsoid = ''
  821. event.Skip()
  822. def OnDatumSelected(self, event):
  823. """!Datum selected"""
  824. index = event.m_itemIndex
  825. item = event.GetItem()
  826. self.datum = self.datumlist.GetItem(index, 0).GetText()
  827. self.tdatum.SetValue(self.datum)
  828. event.Skip()
  829. class EllipsePage(TitledPage):
  830. """!Wizard page for selecting ellipsoid (select coordinate system option)"""
  831. def __init__(self, wizard, parent):
  832. TitledPage.__init__(self, wizard, _("Specify ellipsoid"))
  833. self.parent = parent
  834. self.ellipse = ''
  835. self.ellipsedesc = ''
  836. self.ellipseparams = ''
  837. self.proj4params = ''
  838. # text input
  839. self.tellipse = self.MakeTextCtrl("", size = (200,-1))
  840. # search box
  841. self.searchb = wx.SearchCtrl(self, size = (200,-1),
  842. style = wx.TE_PROCESS_ENTER)
  843. # radio buttons
  844. self.radio1 = wx.RadioButton(parent = self, id = wx.ID_ANY,
  845. label = _("Earth based"),
  846. style = wx.RB_GROUP)
  847. self.radio2 = wx.RadioButton(parent = self, id = wx.ID_ANY,
  848. label = _("Planetary bodies"))
  849. # create list control for ellipse list
  850. data = []
  851. # extract code, desc
  852. for key in self.parent.ellipsoids.keys():
  853. data.append([key, self.parent.ellipsoids[key][0]])
  854. self.ellipselist = ItemList(self, data = data,
  855. columns = [_('Code'), _('Description')])
  856. self.ellipselist.resizeLastColumn(30)
  857. # layout
  858. self.sizer.AddGrowableCol(4)
  859. self.sizer.Add(item = self.MakeLabel(_("Ellipsoid code:")),
  860. flag = wx.ALIGN_RIGHT |
  861. wx.ALIGN_CENTER_VERTICAL |
  862. wx.ALL, border = 5, pos = (1, 1))
  863. self.sizer.Add(item = self.tellipse,
  864. flag = wx.ALIGN_LEFT |
  865. wx.ALIGN_CENTER_VERTICAL |
  866. wx.ALL, border = 5, pos = (1, 2))
  867. self.sizer.Add(item = self.radio1,
  868. flag = wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.LEFT,
  869. border = 25, pos = (1, 3))
  870. self.sizer.Add(item = self.MakeLabel(_("Search in description:")),
  871. flag = wx.ALIGN_RIGHT |
  872. wx.ALIGN_CENTER_VERTICAL |
  873. wx.ALL, border = 5, pos = (2, 1))
  874. self.sizer.Add(item = self.searchb,
  875. flag = wx.ALIGN_LEFT |
  876. wx.ALIGN_CENTER_VERTICAL |
  877. wx.ALL, border = 5, pos = (2, 2))
  878. self.sizer.Add(item = self.radio2,
  879. flag = wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.LEFT,
  880. border = 25, pos = (2, 3))
  881. self.sizer.AddGrowableRow(3)
  882. self.sizer.Add(item = self.ellipselist,
  883. flag = wx.EXPAND |
  884. wx.ALIGN_LEFT |
  885. wx.ALL, border = 5, pos = (3, 1), span = (1, 4))
  886. # events
  887. self.ellipselist.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
  888. self.tellipse.Bind(wx.EVT_TEXT, self.OnText)
  889. self.tellipse.Bind(wx.EVT_TEXT_ENTER, self.OnText)
  890. self.searchb.Bind(wx.EVT_TEXT_ENTER, self.OnSearch)
  891. self.radio1.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id = self.radio1.GetId())
  892. self.radio2.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id = self.radio2.GetId())
  893. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  894. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  895. def OnEnterPage(self,event):
  896. if len(self.ellipse) == 0:
  897. # disable 'next' button by default
  898. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  899. else:
  900. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  901. self.scope = 'earth'
  902. event.Skip()
  903. def OnPageChanging(self, event):
  904. if event.GetDirection() \
  905. and self.ellipse not in self.parent.ellipsoids \
  906. and self.ellipse not in self.parent.planetary_ellipsoids:
  907. event.Veto()
  908. self.proj4params = ''
  909. self.GetNext().SetPrev(self)
  910. self.parent.datumpage.datumparams = ''
  911. # self.GetNext().SetPrev(self) (???)
  912. #FIXME: index number doesn't translate when you've given a valid name from the other list
  913. def OnText(self, event):
  914. """!Ellipspoid code changed"""
  915. self.ellipse = event.GetString()
  916. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  917. if len(self.ellipse) == 0 or \
  918. (self.ellipse not in self.parent.ellipsoids and \
  919. self.ellipse not in self.parent.planetary_ellipsoids):
  920. nextButton.Enable(False)
  921. self.ellipsedesc = ''
  922. self.ellipseparams = ''
  923. self.proj4params = ''
  924. elif self.ellipse in self.parent.ellipsoids:
  925. self.ellipsedesc = self.parent.ellipsoids[self.ellipse][0]
  926. self.ellipseparams = self.parent.ellipsoids[self.ellipse][1]
  927. nextButton.Enable(True)
  928. elif self.ellipse in self.parent.planetary_ellipsoids:
  929. self.ellipsedesc = self.parent.planetary_ellipsoids[self.ellipse][0]
  930. self.ellipseparams = self.parent.planetary_ellipsoids[self.ellipse][1]
  931. nextButton.Enable(True)
  932. def OnSearch(self, event):
  933. """!Search ellipsoid by desc"""
  934. try:
  935. self.ellipse, self.ellipsedesc = \
  936. self.ellipselist.Search(index=[0,1], pattern=event.GetString())
  937. if self.scope is 'earth':
  938. self.ellipseparams = self.parent.ellipsoids[self.ellipse][1]
  939. else:
  940. self.ellipseparams = self.parent.planetary_ellipsoids[self.ellipse][1]
  941. except:
  942. self.ellipse = self.ellipsedesc = self.ellipseparams = ''
  943. event.Skip()
  944. def OnItemSelected(self,event):
  945. """!Ellipsoid selected"""
  946. index = event.m_itemIndex
  947. item = event.GetItem()
  948. self.ellipse = self.ellipselist.GetItem(index, 0).GetText()
  949. self.tellipse.SetValue(self.ellipse)
  950. event.Skip()
  951. def SetVal(self, event):
  952. """!Choose table to use"""
  953. self.ellipselist.DeleteAllItems()
  954. data = []
  955. if event.GetId() == self.radio1.GetId():
  956. self.scope = 'earth'
  957. for key in self.parent.ellipsoids.keys():
  958. data.append([key, self.parent.ellipsoids[key][0]])
  959. elif event.GetId() == self.radio2.GetId():
  960. self.scope = 'planetary'
  961. for key in self.parent.planetary_ellipsoids.keys():
  962. data.append([key, self.parent.planetary_ellipsoids[key][0]])
  963. self.ellipselist.Populate(data = data, update = True)
  964. class GeoreferencedFilePage(TitledPage):
  965. """!Wizard page for selecting georeferenced file to use
  966. for setting coordinate system parameters"""
  967. def __init__(self, wizard, parent):
  968. TitledPage.__init__(self, wizard, _("Select georeferenced file"))
  969. self.georeffile = ''
  970. # create controls
  971. self.lfile= self.MakeLabel(_("Georeferenced file:"))
  972. self.tfile = self.MakeTextCtrl(size = (300,-1))
  973. self.bbrowse = self.MakeButton(_("Browse"))
  974. # do layout
  975. self.sizer.AddGrowableCol(3)
  976. self.sizer.Add(item = self.lfile, flag = wx.ALIGN_LEFT |
  977. wx.ALIGN_CENTRE_VERTICAL |
  978. wx.ALL, border = 5, pos = (1, 1))
  979. self.sizer.Add(item = self.tfile, flag = wx.ALIGN_LEFT |
  980. wx.ALIGN_CENTRE_VERTICAL |
  981. wx.ALL, border = 5, pos = (1, 2))
  982. self.sizer.Add(item = self.bbrowse, flag = wx.ALIGN_LEFT |
  983. wx.ALL, border = 5, pos = (1, 3))
  984. self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
  985. self.tfile.Bind(wx.EVT_TEXT, self.OnText)
  986. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  987. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  988. # do page layout
  989. # self.DoLayout()
  990. def OnEnterPage(self, event):
  991. if len(self.georeffile) == 0:
  992. # disable 'next' button by default
  993. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  994. else:
  995. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  996. event.Skip()
  997. def OnPageChanging(self, event):
  998. if event.GetDirection() and not os.path.isfile(self.georeffile):
  999. event.Veto()
  1000. self.GetNext().SetPrev(self)
  1001. event.Skip()
  1002. def OnText(self, event):
  1003. """!File changed"""
  1004. self.georeffile = event.GetString()
  1005. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  1006. if len(self.georeffile) > 0 and os.path.isfile(self.georeffile):
  1007. if not nextButton.IsEnabled():
  1008. nextButton.Enable(True)
  1009. else:
  1010. if nextButton.IsEnabled():
  1011. nextButton.Enable(False)
  1012. event.Skip()
  1013. def OnBrowse(self, event):
  1014. """!Choose file"""
  1015. dlg = wx.FileDialog(self,
  1016. _("Select georeferenced file"),
  1017. os.getcwd(), "", "*.*", wx.OPEN)
  1018. if dlg.ShowModal() == wx.ID_OK:
  1019. path = dlg.GetPath()
  1020. self.tfile.SetValue(path)
  1021. dlg.Destroy()
  1022. event.Skip()
  1023. class WKTPage(TitledPage):
  1024. """!Wizard page for selecting WKT file to use
  1025. for setting coordinate system parameters"""
  1026. def __init__(self, wizard, parent):
  1027. TitledPage.__init__(self, wizard, _("Select WKT file"))
  1028. self.wktfile = ''
  1029. # create controls
  1030. self.lfile= self.MakeLabel(_("WKT file:"))
  1031. self.tfile = self.MakeTextCtrl(size = (300,-1))
  1032. self.bbrowse = self.MakeButton(_("Browse"))
  1033. # do layout
  1034. self.sizer.AddGrowableCol(3)
  1035. self.sizer.Add(item = self.lfile, flag = wx.ALIGN_LEFT |
  1036. wx.ALIGN_CENTRE_VERTICAL |
  1037. wx.ALL, border = 5, pos = (1, 1))
  1038. self.sizer.Add(item = self.tfile, flag = wx.ALIGN_LEFT |
  1039. wx.ALIGN_CENTRE_VERTICAL |
  1040. wx.ALL, border = 5, pos = (1, 2))
  1041. self.sizer.Add(item = self.bbrowse, flag = wx.ALIGN_LEFT |
  1042. wx.ALL, border = 5, pos = (1, 3))
  1043. self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
  1044. self.tfile.Bind(wx.EVT_TEXT, self.OnText)
  1045. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  1046. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1047. def OnEnterPage(self, event):
  1048. if len(self.wktfile) == 0:
  1049. # disable 'next' button by default
  1050. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1051. else:
  1052. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1053. event.Skip()
  1054. def OnPageChanging(self, event):
  1055. if event.GetDirection() and not os.path.isfile(self.wktfile):
  1056. event.Veto()
  1057. self.GetNext().SetPrev(self)
  1058. event.Skip()
  1059. def OnText(self, event):
  1060. """!File changed"""
  1061. self.wktfile = event.GetString()
  1062. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  1063. if len(self.wktfile) > 0 and os.path.isfile(self.wktfile):
  1064. if not nextButton.IsEnabled():
  1065. nextButton.Enable(True)
  1066. else:
  1067. if nextButton.IsEnabled():
  1068. nextButton.Enable(False)
  1069. event.Skip()
  1070. def OnBrowse(self, event):
  1071. """!Choose file"""
  1072. dlg = wx.FileDialog(self,
  1073. _("Select WKT file"),
  1074. os.getcwd(), "", "*.*", wx.OPEN)
  1075. if dlg.ShowModal() == wx.ID_OK:
  1076. path = dlg.GetPath()
  1077. self.tfile.SetValue(path)
  1078. dlg.Destroy()
  1079. event.Skip()
  1080. class EPSGPage(TitledPage):
  1081. """!Wizard page for selecting EPSG code for
  1082. setting coordinate system parameters"""
  1083. def __init__(self, wizard, parent):
  1084. TitledPage.__init__(self, wizard, _("Choose EPSG Code"))
  1085. self.parent = parent
  1086. self.epsgCodeDict = {}
  1087. self.epsgcode = None
  1088. self.epsgdesc = ''
  1089. self.epsgparams = ''
  1090. # labels
  1091. self.lfile = self.MakeLabel(_("Path to the EPSG-codes file:"),
  1092. style = wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
  1093. self.lcode = self.MakeLabel(_("EPSG code:"),
  1094. style = wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
  1095. # text input
  1096. epsgdir = utils.PathJoin(os.environ["GRASS_PROJSHARE"], 'epsg')
  1097. self.tfile = self.MakeTextCtrl(text = epsgdir, size = (200,-1),
  1098. style = wx.TE_PROCESS_ENTER)
  1099. self.tcode = self.MakeTextCtrl(size = (200,-1))
  1100. # buttons
  1101. self.bbrowse = self.MakeButton(_("Browse"))
  1102. # search box
  1103. self.searchb = wx.SearchCtrl(self, size = (200,-1),
  1104. style = wx.TE_PROCESS_ENTER)
  1105. self.epsglist = ItemList(self, data = None,
  1106. columns = [_('Code'), _('Description'), _('Parameters')])
  1107. # layout
  1108. self.sizer.AddGrowableCol(3)
  1109. self.sizer.Add(item = self.lfile,
  1110. flag = wx.ALIGN_LEFT |
  1111. wx.ALIGN_CENTER_VERTICAL |
  1112. wx.ALL, border = 5, pos = (1, 1), span = (1, 2))
  1113. self.sizer.Add(item = self.tfile,
  1114. flag = wx.ALIGN_LEFT |
  1115. wx.ALIGN_CENTER_VERTICAL |
  1116. wx.ALL, border = 5, pos = (1, 3))
  1117. self.sizer.Add(item = self.bbrowse,
  1118. flag = wx.ALIGN_LEFT |
  1119. wx.ALIGN_CENTER_VERTICAL |
  1120. wx.ALL, border = 5, pos = (1, 4))
  1121. self.sizer.Add(item = self.lcode,
  1122. flag = wx.ALIGN_LEFT |
  1123. wx.ALIGN_CENTER_VERTICAL |
  1124. wx.ALL, border = 5, pos = (2, 1), span = (1, 2))
  1125. self.sizer.Add(item = self.tcode,
  1126. flag = wx.ALIGN_LEFT |
  1127. wx.ALIGN_CENTER_VERTICAL |
  1128. wx.ALL, border = 5, pos = (2, 3))
  1129. self.sizer.Add(item = self.searchb,
  1130. flag = wx.ALIGN_LEFT |
  1131. wx.ALIGN_CENTER_VERTICAL |
  1132. wx.ALL, border = 5, pos = (3, 3))
  1133. self.sizer.AddGrowableRow(4)
  1134. self.sizer.Add(item = self.epsglist,
  1135. flag = wx.ALIGN_LEFT | wx.EXPAND, pos = (4, 1),
  1136. span = (1, 4))
  1137. # events
  1138. self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
  1139. self.tfile.Bind(wx.EVT_TEXT_ENTER, self.OnBrowseCodes)
  1140. self.tcode.Bind(wx.EVT_TEXT, self.OnText)
  1141. self.tcode.Bind(wx.EVT_TEXT_ENTER, self.OnText)
  1142. self.epsglist.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
  1143. self.searchb.Bind(wx.EVT_TEXT_ENTER, self.OnSearch)
  1144. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  1145. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1146. def OnEnterPage(self, event):
  1147. self.parent.datumtrans = None
  1148. if event.GetDirection():
  1149. if not self.epsgcode:
  1150. # disable 'next' button by default
  1151. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1152. else:
  1153. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1154. # load default epsg database file
  1155. self.OnBrowseCodes(None)
  1156. event.Skip()
  1157. def OnPageChanging(self, event):
  1158. if event.GetDirection():
  1159. if not self.epsgcode:
  1160. event.Veto()
  1161. return
  1162. else:
  1163. # check for datum transforms
  1164. ret = RunCommand('g.proj',
  1165. read = True,
  1166. epsg = self.epsgcode,
  1167. datumtrans = '-1')
  1168. if ret != '':
  1169. dtrans = ''
  1170. # open a dialog to select datum transform number
  1171. dlg = SelectTransformDialog(self.parent.parent, transforms = ret)
  1172. if dlg.ShowModal() == wx.ID_OK:
  1173. dtrans = dlg.GetTransform()
  1174. if dtrans == '':
  1175. dlg.Destroy()
  1176. event.Veto()
  1177. return 'Datum transform is required.'
  1178. else:
  1179. dlg.Destroy()
  1180. event.Veto()
  1181. return 'Datum transform is required.'
  1182. self.parent.datumtrans = dtrans
  1183. self.GetNext().SetPrev(self)
  1184. def OnText(self, event):
  1185. self.epsgcode = event.GetString()
  1186. try:
  1187. self.epsgcode = int(self.epsgcode)
  1188. except:
  1189. self.epsgcode = None
  1190. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  1191. if self.epsgcode and self.epsgcode in self.epsgCodeDict.keys():
  1192. self.epsgdesc = self.epsgCodeDict[self.epsgcode][0]
  1193. self.epsgparams = self.epsgCodeDict[self.epsgcode][1]
  1194. if not nextButton.IsEnabled():
  1195. nextButton.Enable(True)
  1196. else:
  1197. self.epsgcode = None # not found
  1198. if nextButton.IsEnabled():
  1199. nextButton.Enable(False)
  1200. self.epsgdesc = self.epsgparams = ''
  1201. def OnSearch(self, event):
  1202. value = self.searchb.GetValue()
  1203. if value == '':
  1204. self.epsgcode = None
  1205. self.epsgdesc = self.epsgparams = ''
  1206. self.tcode.SetValue('')
  1207. self.searchb.SetValue('')
  1208. self.OnBrowseCodes(None)
  1209. else:
  1210. try:
  1211. self.epsgcode, self.epsgdesc, self.epsgparams = \
  1212. self.epsglist.Search(index=[0,1,2], pattern=value)
  1213. except (IndexError, ValueError): # -> no item found
  1214. self.epsgcode = None
  1215. self.epsgdesc = self.epsgparams = ''
  1216. self.tcode.SetValue('')
  1217. self.searchb.SetValue('')
  1218. event.Skip()
  1219. def OnBrowse(self, event):
  1220. """!Define path for EPSG code file"""
  1221. path = os.path.dirname(self.tfile.GetValue())
  1222. if not path:
  1223. path = os.getcwd()
  1224. dlg = wx.FileDialog(parent = self, message = _("Choose EPSG codes file"),
  1225. defaultDir = path, defaultFile = "", wildcard = "*", style = wx.OPEN)
  1226. if dlg.ShowModal() == wx.ID_OK:
  1227. path = dlg.GetPath()
  1228. self.tfile.SetValue(path)
  1229. self.OnBrowseCodes(None)
  1230. dlg.Destroy()
  1231. event.Skip()
  1232. def OnItemSelected(self, event):
  1233. """!EPSG code selected from the list"""
  1234. index = event.m_itemIndex
  1235. item = event.GetItem()
  1236. self.epsgcode = int(self.epsglist.GetItem(index, 0).GetText())
  1237. self.epsgdesc = self.epsglist.GetItem(index, 1).GetText()
  1238. self.tcode.SetValue(str(self.epsgcode))
  1239. event.Skip()
  1240. def OnBrowseCodes(self, event, search = None):
  1241. """!Browse EPSG codes"""
  1242. self.epsgCodeDict = utils.ReadEpsgCodes(self.tfile.GetValue())
  1243. if type(self.epsgCodeDict) != dict:
  1244. wx.MessageBox(parent = self,
  1245. message = _("Unable to read EPGS codes: %s") % self.epsgCodeDict,
  1246. caption = _("Error"), style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
  1247. self.epsglist.Populate(list(), update = True)
  1248. return
  1249. data = list()
  1250. for code, val in self.epsgCodeDict.iteritems():
  1251. if code is not None:
  1252. data.append((code, val[0], val[1]))
  1253. self.epsglist.Populate(data, update = True)
  1254. class CustomPage(TitledPage):
  1255. """!Wizard page for entering custom PROJ.4 string
  1256. for setting coordinate system parameters"""
  1257. def __init__(self, wizard, parent):
  1258. TitledPage.__init__(self, wizard,
  1259. _("Choose method of specifying georeferencing parameters"))
  1260. global coordsys
  1261. self.customstring = ''
  1262. self.parent = parent
  1263. # widgets
  1264. self.text_proj4string = self.MakeTextCtrl(size = (400, 200),
  1265. style = wx.TE_MULTILINE)
  1266. self.label_proj4string = self.MakeLabel(_("Enter PROJ.4 parameters string:"))
  1267. # layout
  1268. self.sizer.AddGrowableCol(2)
  1269. self.sizer.Add(self.label_proj4string,
  1270. flag = wx.ALIGN_LEFT | wx.ALL,
  1271. border = 5, pos = (1, 1))
  1272. self.sizer.AddGrowableRow(2)
  1273. self.sizer.Add(self.text_proj4string,
  1274. flag = wx.ALIGN_LEFT | wx.ALL | wx.EXPAND,
  1275. border = 5, pos = (2, 1), span = (1, 2))
  1276. self.text_proj4string.Bind(wx.EVT_TEXT, self.GetProjstring)
  1277. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  1278. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1279. def OnEnterPage(self, event):
  1280. if len(self.customstring) == 0:
  1281. # disable 'next' button by default
  1282. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1283. else:
  1284. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1285. def OnPageChanging(self, event):
  1286. if event.GetDirection():
  1287. # check for datum tranforms
  1288. ret, out, err = RunCommand('g.proj',
  1289. read = True, getErrorMsg = True,
  1290. proj4 = self.customstring,
  1291. datumtrans = '-1')
  1292. if ret != 0:
  1293. wx.MessageBox(parent = self,
  1294. message = err,
  1295. caption = _("Error"),
  1296. style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
  1297. event.Veto()
  1298. return
  1299. if out:
  1300. dtrans = ''
  1301. # open a dialog to select datum transform number
  1302. dlg = SelectTransformDialog(self.parent.parent, transforms = out)
  1303. if dlg.ShowModal() == wx.ID_OK:
  1304. dtrans = dlg.GetTransform()
  1305. if len(dtrans) == 0:
  1306. dlg.Destroy()
  1307. event.Veto()
  1308. return _('Datum transform is required.')
  1309. else:
  1310. dlg.Destroy()
  1311. event.Veto()
  1312. return _('Datum transform is required.')
  1313. self.parent.datumtrans = dtrans
  1314. self.GetNext().SetPrev(self)
  1315. def GetProjstring(self, event):
  1316. """!Change proj string"""
  1317. # TODO: check PROJ.4 syntax
  1318. self.customstring = event.GetString()
  1319. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  1320. if len(self.customstring) == 0:
  1321. if nextButton.IsEnabled():
  1322. nextButton.Enable(False)
  1323. else:
  1324. if not nextButton.IsEnabled():
  1325. nextButton.Enable()
  1326. class SummaryPage(TitledPage):
  1327. """!Shows summary result of choosing coordinate system parameters
  1328. prior to creating location"""
  1329. def __init__(self, wizard, parent):
  1330. TitledPage.__init__(self, wizard, _("Summary"))
  1331. self.parent = parent
  1332. self.panelTitle = scrolled.ScrolledPanel(parent = self, id = wx.ID_ANY)
  1333. self.panelProj4string = scrolled.ScrolledPanel(parent = self, id = wx.ID_ANY)
  1334. self.panelProj = scrolled.ScrolledPanel(parent = self, id = wx.ID_ANY)
  1335. # labels
  1336. self.ldatabase = self.MakeLabel()
  1337. self.llocation = self.MakeLabel()
  1338. self.llocTitle = self.MakeLabel(parent = self.panelTitle)
  1339. self.lprojection = self.MakeLabel(parent = self.panelProj)
  1340. self.lproj4string = self.MakeLabel(parent = self.panelProj4string)
  1341. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1342. # do sub-page layout
  1343. self._doLayout()
  1344. def _doLayout(self):
  1345. """!Do page layout"""
  1346. self.sizer.AddGrowableCol(1)
  1347. self.sizer.AddGrowableRow(3, 1)
  1348. self.sizer.AddGrowableRow(4, 1)
  1349. self.sizer.AddGrowableRow(5, 5)
  1350. titleSizer = wx.BoxSizer(wx.VERTICAL)
  1351. titleSizer.Add(item = self.llocTitle, proportion = 1,
  1352. flag = wx.EXPAND | wx.ALL, border = 5)
  1353. self.panelTitle.SetSizer(titleSizer)
  1354. projSizer = wx.BoxSizer(wx.VERTICAL)
  1355. projSizer.Add(item = self.lprojection, proportion = 1,
  1356. flag = wx.EXPAND | wx.ALL, border = 5)
  1357. self.panelProj.SetSizer(projSizer)
  1358. proj4stringSizer = wx.BoxSizer(wx.VERTICAL)
  1359. proj4stringSizer.Add(item = self.lproj4string, proportion = 1,
  1360. flag = wx.EXPAND | wx.ALL, border = 5)
  1361. self.panelProj4string.SetSizer(proj4stringSizer)
  1362. self.panelProj4string.SetupScrolling()
  1363. self.panelProj.SetupScrolling(scroll_y = False)
  1364. self.panelTitle.SetupScrolling(scroll_y = False)
  1365. self.sizer.Add(item = self.MakeLabel(_("GRASS Database:")),
  1366. flag = wx.ALIGN_LEFT | wx.ALL,
  1367. border = 5, pos = (1, 0))
  1368. self.sizer.Add(item = self.ldatabase,
  1369. flag = wx.ALIGN_LEFT | wx.ALL,
  1370. border = 5, pos = (1, 1))
  1371. self.sizer.Add(item = self.MakeLabel(_("Location Name:")),
  1372. flag = wx.ALIGN_LEFT | wx.ALL,
  1373. border = 5, pos = (2, 0))
  1374. self.sizer.Add(item = self.llocation,
  1375. flag = wx.ALIGN_LEFT | wx.ALL,
  1376. border = 5, pos = (2, 1))
  1377. self.sizer.Add(item = self.MakeLabel(_("Location Title:")),
  1378. flag = wx.ALIGN_LEFT | wx.ALL,
  1379. border = 5, pos = (3, 0))
  1380. self.sizer.Add(item = self.panelTitle,
  1381. flag = wx.ALIGN_LEFT | wx.ALL | wx.EXPAND,
  1382. border = 0, pos = (3, 1))
  1383. self.sizer.Add(item = self.MakeLabel(_("Projection:")),
  1384. flag = wx.ALIGN_LEFT | wx.ALL,
  1385. border = 5, pos = (4, 0))
  1386. self.sizer.Add(item = self.panelProj,
  1387. flag = wx.ALIGN_LEFT | wx.ALL | wx.EXPAND,
  1388. border = 0, pos = (4, 1))
  1389. self.sizer.Add(item = self.MakeLabel(_("PROJ.4 definition:")),
  1390. flag = wx.ALIGN_LEFT | wx.ALL,
  1391. border = 5, pos = (5, 0))
  1392. self.sizer.Add(item = self.panelProj4string,
  1393. flag = wx.ALIGN_LEFT | wx.ALL | wx.EXPAND,
  1394. border = 0, pos = (5, 1))
  1395. def OnEnterPage(self, event):
  1396. """!Insert values into text controls for summary of location
  1397. creation options
  1398. """
  1399. database = self.parent.startpage.grassdatabase
  1400. location = self.parent.startpage.location
  1401. proj4string = self.parent.CreateProj4String()
  1402. epsgcode = self.parent.epsgpage.epsgcode
  1403. dtrans = self.parent.datumtrans
  1404. global coordsys
  1405. if coordsys in ('proj', 'epsg'):
  1406. if coordsys == 'proj':
  1407. ret, projlabel, err = RunCommand('g.proj',
  1408. flags = 'jf',
  1409. proj4 = proj4string,
  1410. datumtrans = dtrans,
  1411. location = location,
  1412. getErrorMsg = True,
  1413. read = True)
  1414. elif coordsys == 'epsg':
  1415. ret, projlabel, err = RunCommand('g.proj',
  1416. flags = 'jf',
  1417. epsg = epsgcode,
  1418. datumtrans = dtrans,
  1419. location = location,
  1420. getErrorMsg = True,
  1421. read = True)
  1422. finishButton = wx.FindWindowById(wx.ID_FORWARD)
  1423. if ret == 0:
  1424. self.lproj4string.SetLabel(projlabel.replace(' ', os.linesep))
  1425. finishButton.Enable(True)
  1426. else:
  1427. GError(err, parent = self)
  1428. self.lproj4string.SetLabel('')
  1429. finishButton.Enable(False)
  1430. projdesc = self.parent.projpage.projdesc
  1431. ellipsedesc = self.parent.ellipsepage.ellipsedesc
  1432. datumdesc = self.parent.datumpage.datumdesc
  1433. self.ldatabase.SetLabel(database)
  1434. self.llocation.SetLabel(location)
  1435. self.llocTitle.SetLabel(self.parent.startpage.locTitle)
  1436. label = ''
  1437. if coordsys == 'epsg':
  1438. label = 'EPSG code %s (%s)' % (self.parent.epsgpage.epsgcode, self.parent.epsgpage.epsgdesc)
  1439. elif coordsys == 'file':
  1440. label = 'matches file %s' % self.parent.filepage.georeffile
  1441. self.lproj4string.SetLabel("")
  1442. elif coordsys == 'wkt':
  1443. label = 'matches file %s' % self.parent.wktpage.wktfile
  1444. self.lproj4string.SetLabel("")
  1445. elif coordsys == 'proj':
  1446. label = ('%s, %s %s' % (projdesc, datumdesc, ellipsedesc))
  1447. elif coordsys == 'xy':
  1448. label = ('XY coordinate system (not projected).')
  1449. self.lproj4string.SetLabel("")
  1450. elif coordsys == 'custom':
  1451. label = _("custom")
  1452. self.lproj4string.SetLabel(('%s' % self.parent.custompage.customstring.replace(' ', os.linesep)))
  1453. self.lprojection.SetLabel(label)
  1454. def OnFinish(self, event):
  1455. dlg = wx.MessageDialog(parent = self.wizard,
  1456. message = _("Do you want to create GRASS location <%s>?") % location,
  1457. caption = _("Create new location?"),
  1458. style = wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
  1459. if dlg.ShowModal() == wx.ID_NO:
  1460. dlg.Destroy()
  1461. event.Veto()
  1462. else:
  1463. dlg.Destroy()
  1464. event.Skip()
  1465. class LocationWizard(wx.Object):
  1466. """!Start wizard here and finish wizard here
  1467. """
  1468. def __init__(self, parent, grassdatabase):
  1469. self.__cleanUp()
  1470. global coordsys
  1471. self.parent = parent
  1472. #
  1473. # define wizard image
  1474. #
  1475. imagePath = os.path.join(globalvar.ETCIMGDIR, "loc_wizard_qgis.png")
  1476. wizbmp = wx.Image(imagePath, wx.BITMAP_TYPE_PNG)
  1477. wizbmp = wizbmp.ConvertToBitmap()
  1478. #
  1479. # get georeferencing information from tables in $GISBASE/etc
  1480. #
  1481. self.__readData()
  1482. #
  1483. # datum transform number and list of datum transforms
  1484. #
  1485. self.datumtrans = None
  1486. self.proj4string = ''
  1487. #
  1488. # define wizard pages
  1489. #
  1490. self.wizard = WizardWithHelpButton(parent, id = wx.ID_ANY, title = _("Define new GRASS Location"),
  1491. bitmap = wizbmp)
  1492. self.wizard.Bind(wiz.EVT_WIZARD_HELP, self.OnHelp)
  1493. self.startpage = DatabasePage(self.wizard, self, grassdatabase)
  1494. self.csystemspage = CoordinateSystemPage(self.wizard, self)
  1495. self.projpage = ProjectionsPage(self.wizard, self)
  1496. self.datumpage = DatumPage(self.wizard, self)
  1497. self.paramspage = ProjParamsPage(self.wizard,self)
  1498. self.epsgpage = EPSGPage(self.wizard, self)
  1499. self.filepage = GeoreferencedFilePage(self.wizard, self)
  1500. self.wktpage = WKTPage(self.wizard, self)
  1501. self.ellipsepage = EllipsePage(self.wizard, self)
  1502. self.custompage = CustomPage(self.wizard, self)
  1503. self.sumpage = SummaryPage(self.wizard, self)
  1504. #
  1505. # set the initial order of the pages
  1506. # (should follow the epsg line)
  1507. #
  1508. self.startpage.SetNext(self.csystemspage)
  1509. self.csystemspage.SetPrev(self.startpage)
  1510. self.csystemspage.SetNext(self.sumpage)
  1511. self.projpage.SetPrev(self.csystemspage)
  1512. self.projpage.SetNext(self.paramspage)
  1513. self.paramspage.SetPrev(self.projpage)
  1514. self.paramspage.SetNext(self.datumpage)
  1515. self.datumpage.SetPrev(self.paramspage)
  1516. self.datumpage.SetNext(self.sumpage)
  1517. self.ellipsepage.SetPrev(self.paramspage)
  1518. self.ellipsepage.SetNext(self.sumpage)
  1519. self.epsgpage.SetPrev(self.csystemspage)
  1520. self.epsgpage.SetNext(self.sumpage)
  1521. self.filepage.SetPrev(self.csystemspage)
  1522. self.filepage.SetNext(self.sumpage)
  1523. self.wktpage.SetPrev(self.csystemspage)
  1524. self.wktpage.SetNext(self.sumpage)
  1525. self.custompage.SetPrev(self.csystemspage)
  1526. self.custompage.SetNext(self.sumpage)
  1527. self.sumpage.SetPrev(self.csystemspage)
  1528. #
  1529. # do pages layout
  1530. #
  1531. self.startpage.DoLayout()
  1532. self.csystemspage.DoLayout()
  1533. self.projpage.DoLayout()
  1534. self.datumpage.DoLayout()
  1535. self.paramspage.DoLayout()
  1536. self.epsgpage.DoLayout()
  1537. self.filepage.DoLayout()
  1538. self.wktpage.DoLayout()
  1539. self.ellipsepage.DoLayout()
  1540. self.custompage.DoLayout()
  1541. self.sumpage.DoLayout()
  1542. self.wizard.FitToPage(self.datumpage)
  1543. size = self.wizard.GetPageSize()
  1544. self.wizard.SetPageSize((size[0], size[1] + 75))
  1545. # new location created?
  1546. self.location = None
  1547. success = False
  1548. # location created in different GIS database?
  1549. self.altdb = False
  1550. #
  1551. # run wizard...
  1552. #
  1553. if self.wizard.RunWizard(self.startpage):
  1554. msg = self.OnWizFinished()
  1555. if not msg:
  1556. self.wizard.Destroy()
  1557. self.location = self.startpage.location
  1558. if self.altdb == False:
  1559. dlg = wx.MessageDialog(parent = self.parent,
  1560. message = _("Do you want to set the default "
  1561. "region extents and resolution now?"),
  1562. caption = _("Location <%s> created") % self.location,
  1563. style = wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  1564. dlg.CenterOnScreen()
  1565. if dlg.ShowModal() == wx.ID_YES:
  1566. dlg.Destroy()
  1567. defineRegion = RegionDef(self.parent, location = self.location)
  1568. defineRegion.CenterOnScreen()
  1569. defineRegion.Show()
  1570. else:
  1571. dlg.Destroy()
  1572. else: # -> error
  1573. self.wizard.Destroy()
  1574. GError(parent = self.parent,
  1575. message = "%s" % _("Unable to create new location. "
  1576. "Location <%(loc)s> not created.\n\n"
  1577. "Details: %(err)s") % \
  1578. { 'loc' : self.startpage.location,
  1579. 'err' : msg })
  1580. else: # -> canceled
  1581. self.wizard.Destroy()
  1582. GMessage(parent = self.parent,
  1583. message = _("Location wizard canceled. "
  1584. "Location not created."))
  1585. self.__cleanUp()
  1586. def __cleanUp(self):
  1587. global coordsys
  1588. global north
  1589. global south
  1590. global east
  1591. global west
  1592. global resolution
  1593. global wizerror
  1594. global translist
  1595. coordsys = None
  1596. north = None
  1597. south = None
  1598. east = None
  1599. west = None
  1600. resolution = None
  1601. transformlist = list()
  1602. def __readData(self):
  1603. """!Get georeferencing information from tables in $GISBASE/etc/proj"""
  1604. # read projection and parameters
  1605. f = open(os.path.join(globalvar.ETCDIR, "proj", "parms.table"), "r")
  1606. self.projections = {}
  1607. self.projdesc = {}
  1608. for line in f.readlines():
  1609. line = line.strip()
  1610. try:
  1611. proj, projdesc, params = line.split(':')
  1612. paramslist = params.split(';')
  1613. plist = []
  1614. for p in paramslist:
  1615. if p == '': continue
  1616. p1, pdefault = p.split(',')
  1617. pterm, pask = p1.split('=')
  1618. p = [pterm.strip(), pask.strip(), pdefault.strip()]
  1619. plist.append(p)
  1620. self.projections[proj.lower().strip()] = (projdesc.strip(), plist)
  1621. self.projdesc[proj.lower().strip()] = projdesc.strip()
  1622. except:
  1623. continue
  1624. f.close()
  1625. # read datum definitions
  1626. f = open(os.path.join(globalvar.ETCDIR, "proj", "datum.table"), "r")
  1627. self.datums = {}
  1628. paramslist = []
  1629. for line in f.readlines():
  1630. line = line.expandtabs(1)
  1631. line = line.strip()
  1632. if line == '' or line[0] == "#":
  1633. continue
  1634. datum, info = line.split(" ", 1)
  1635. info = info.strip()
  1636. datumdesc, params = info.split(" ", 1)
  1637. datumdesc = datumdesc.strip('"')
  1638. paramlist = params.split()
  1639. ellipsoid = paramlist.pop(0)
  1640. self.datums[datum] = (ellipsoid, datumdesc.replace('_', ' '), paramlist)
  1641. f.close()
  1642. # read Earth-based ellipsiod definitions
  1643. f = open(os.path.join(globalvar.ETCDIR, "proj", "ellipse.table"), "r")
  1644. self.ellipsoids = {}
  1645. for line in f.readlines():
  1646. line = line.expandtabs(1)
  1647. line = line.strip()
  1648. if line == '' or line[0] == "#":
  1649. continue
  1650. ellipse, rest = line.split(" ", 1)
  1651. rest = rest.strip('" ')
  1652. desc, params = rest.split('"', 1)
  1653. desc = desc.strip('" ')
  1654. paramslist = params.split()
  1655. self.ellipsoids[ellipse] = (desc, paramslist)
  1656. f.close()
  1657. # read Planetary ellipsiod definitions
  1658. f = open(os.path.join(globalvar.ETCDIR, "proj", "ellipse.table.solar.system"), "r")
  1659. self.planetary_ellipsoids = {}
  1660. for line in f.readlines():
  1661. line = line.expandtabs(1)
  1662. line = line.strip()
  1663. if line == '' or line[0] == "#":
  1664. continue
  1665. ellipse, rest = line.split(" ", 1)
  1666. rest = rest.strip('" ')
  1667. desc, params = rest.split('"', 1)
  1668. desc = desc.strip('" ')
  1669. paramslist = params.split()
  1670. self.planetary_ellipsoids[ellipse] = (desc, paramslist)
  1671. f.close()
  1672. # read projection parameter description and parsing table
  1673. f = open(os.path.join(globalvar.ETCDIR, "proj", "desc.table"), "r")
  1674. self.paramdesc = {}
  1675. for line in f.readlines():
  1676. line = line.strip()
  1677. try:
  1678. pparam, datatype, proj4term, desc = line.split(':')
  1679. self.paramdesc[pparam] = (datatype, proj4term, desc)
  1680. except:
  1681. continue
  1682. f.close()
  1683. def OnWizFinished(self):
  1684. """!Wizard finished, create new location
  1685. @return error message on error
  1686. @return None on success
  1687. """
  1688. database = self.startpage.grassdatabase
  1689. location = self.startpage.location
  1690. # location already exists?
  1691. if os.path.isdir(os.path.join(database,location)):
  1692. GError(parent = self.wizard,
  1693. message = "%s <%s>: %s" % \
  1694. (_("Unable to create new location"),
  1695. os.path.join(database, location),
  1696. _("Location already exists in GRASS Database.")))
  1697. return None
  1698. # current GISDbase or a new one?
  1699. current_gdb = grass.gisenv()['GISDBASE']
  1700. if current_gdb != database:
  1701. # change to new GISDbase or create new one
  1702. if os.path.isdir(database) != True:
  1703. # create new directory
  1704. os.mkdir(database)
  1705. # change to new GISDbase directory
  1706. RunCommand('g.gisenv',
  1707. parent = self.wizard,
  1708. set = 'GISDBASE=%s' % database)
  1709. wx.MessageBox(parent = self.wizard,
  1710. message = _("Location <%(loc)s> will be created "
  1711. "in GIS data directory <%(dir)s>. "
  1712. "You will need to change the default GIS "
  1713. "data directory in the GRASS startup screen.") % \
  1714. { 'loc' : location, 'dir' : database},
  1715. caption = _("New GIS data directory"),
  1716. style = wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
  1717. # location created in alternate GISDbase
  1718. self.altdb = True
  1719. global coordsys
  1720. try:
  1721. if coordsys == "xy":
  1722. grass.create_location(dbase = self.startpage.grassdatabase,
  1723. location = self.startpage.location,
  1724. desc = self.startpage.locTitle)
  1725. elif coordsys == "proj":
  1726. grass.create_location(dbase = self.startpage.grassdatabase,
  1727. location = self.startpage.location,
  1728. proj4 = self.CreateProj4String(),
  1729. datum = self.datumtrans,
  1730. desc = self.startpage.locTitle)
  1731. elif coordsys == 'custom':
  1732. grass.create_location(dbase = self.startpage.grassdatabase,
  1733. location = self.startpage.location,
  1734. proj4 = self.custompage.customstring,
  1735. desc = self.startpage.locTitle)
  1736. elif coordsys == "epsg":
  1737. if not self.epsgpage.epsgcode:
  1738. return _('EPSG code missing.')
  1739. grass.create_location(dbase = self.startpage.grassdatabase,
  1740. location = self.startpage.location,
  1741. epsg = self.epsgpage.epsgcode,
  1742. datum = self.datumtrans,
  1743. desc = self.startpage.locTitle)
  1744. elif coordsys == "file":
  1745. if not self.filepage.georeffile or \
  1746. not os.path.isfile(self.filepage.georeffile):
  1747. return _("File <%s> not found." % self.filepage.georeffile)
  1748. grass.create_location(dbase = self.startpage.grassdatabase,
  1749. location = self.startpage.location,
  1750. filename = self.filepage.georeffile,
  1751. desc = self.startpage.locTitle)
  1752. elif coordsys == "wkt":
  1753. if not self.wktpage.wktfile or \
  1754. not os.path.isfile(self.wktpage.wktfile):
  1755. return _("File <%s> not found." % self.wktpage.wktfile)
  1756. grass.create_location(dbase = self.startpage.grassdatabase,
  1757. location = self.startpage.location,
  1758. wkt = self.wktpage.wktfile,
  1759. desc = self.startpage.locTitle)
  1760. except grass.ScriptError, e:
  1761. return e.value
  1762. return None
  1763. def CreateProj4String(self):
  1764. """!Constract PROJ.4 string"""
  1765. location = self.startpage.location
  1766. proj = self.projpage.p4proj
  1767. projdesc = self.projpage.projdesc
  1768. proj4params = self.paramspage.p4projparams
  1769. datum = self.datumpage.datum
  1770. if self.datumpage.datumdesc:
  1771. datumdesc = self.datumpage.datumdesc +' - ' + self.datumpage.ellipse
  1772. else:
  1773. datumdesc = ''
  1774. datumparams = self.datumpage.datumparams
  1775. ellipse = self.ellipsepage.ellipse
  1776. ellipsedesc = self.ellipsepage.ellipsedesc
  1777. ellipseparams = self.ellipsepage.ellipseparams
  1778. #
  1779. # creating PROJ.4 string
  1780. #
  1781. proj4string = '%s %s' % (proj, proj4params)
  1782. # set ellipsoid parameters
  1783. if ellipse != '':
  1784. proj4string = '%s +ellps=%s' % (proj4string, ellipse)
  1785. for item in ellipseparams:
  1786. if item[:4] == 'f=1/':
  1787. item = ' +rf=' + item[4:]
  1788. else:
  1789. item = ' +' + item
  1790. proj4string = '%s %s' % (proj4string, item)
  1791. # set datum and transform parameters if relevant
  1792. if datum != '':
  1793. proj4string = '%s +datum=%s' % (proj4string, datum)
  1794. if datumparams:
  1795. for item in datumparams:
  1796. proj4string = '%s +%s' % (proj4string,item)
  1797. proj4string = '%s +no_defs' % proj4string
  1798. return proj4string
  1799. def OnHelp(self, event):
  1800. """'Help' button clicked"""
  1801. # help text in lib/init/helptext.html
  1802. filePath = os.path.join(os.getenv('GISBASE'), "docs", "html", "helptext.html")
  1803. helpFrame = HelpFrame(parent = None, id = wx.ID_ANY,
  1804. title = _("GRASS Quickstart"),
  1805. size = (640, 480),
  1806. file = filePath)
  1807. helpFrame.Show(True)
  1808. class WizardWithHelpButton(wiz.Wizard):
  1809. def __init__(self, parent, id, title, bitmap):
  1810. pre = wiz.PreWizard()
  1811. pre.SetExtraStyle(wx.wizard.WIZARD_EX_HELPBUTTON)
  1812. pre.Create(parent = parent, id = id, title = title, bitmap = bitmap)
  1813. self.PostCreate(pre)