wizard.py 85 KB

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