wizard.py 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181
  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. "WKT or 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.AddGrowableCol(3)
  308. self.sizer.Add(item = self.MakeLabel(_("Projection code:")),
  309. flag = wx.ALIGN_LEFT |
  310. wx.ALIGN_CENTER_VERTICAL |
  311. wx.ALL, border = 5, pos = (1, 1))
  312. self.sizer.Add(item = self.tproj,
  313. flag = wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL,
  314. border = 5, pos = (1, 2))
  315. self.sizer.Add(item = self.MakeLabel(_("Search in description:")),
  316. flag = wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  317. border = 5, pos = (2, 1))
  318. self.sizer.Add(item = self.searchb,
  319. flag = wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL,
  320. border = 5, pos = (2, 2))
  321. self.sizer.AddGrowableRow(3)
  322. self.sizer.Add(item = self.projlist,
  323. flag = wx.EXPAND |
  324. wx.ALIGN_LEFT |
  325. wx.ALL, border = 5, pos = (3, 1), span = (1, 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. self.sizer.AddGrowableCol(1)
  554. self.sizer.AddGrowableRow(1)
  555. radioSBox = wx.StaticBox(parent = self, id = wx.ID_ANY,
  556. label = " %s " % _("Select datum or ellipsoid (next page)"))
  557. radioSBSizer = wx.StaticBoxSizer(radioSBox)
  558. self.sizer.Add(item = radioSBSizer, pos = (0, 1),
  559. flag = wx.EXPAND | wx.ALIGN_TOP | wx.TOP, border = 10)
  560. self.radio1 = wx.RadioButton(parent = self, id = wx.ID_ANY,
  561. label = _("Datum with associated ellipsoid"),
  562. style = wx.RB_GROUP)
  563. self.radio2 = wx.RadioButton(parent = self, id = wx.ID_ANY,
  564. label = _("Ellipsoid only"))
  565. # default button setting
  566. if self.radio1.GetValue() == False and self.radio2.GetValue() == False:
  567. self.radio1.SetValue(True)
  568. self.SetNext(self.parent.datumpage)
  569. # self.parent.sumpage.SetPrev(self.parent.datumpage)
  570. radioSBSizer.Add(item = self.radio1,
  571. flag = wx.ALIGN_LEFT | wx.RIGHT, border = 20)
  572. radioSBSizer.Add(item = self.radio2,
  573. flag = wx.ALIGN_LEFT)
  574. # bindings
  575. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id = self.radio1.GetId())
  576. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id = self.radio2.GetId())
  577. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChange)
  578. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  579. def OnParamEntry(self, event):
  580. """!Parameter value changed"""
  581. id = event.GetId()
  582. val = event.GetString()
  583. if id not in self.pparam:
  584. event.Skip()
  585. return
  586. param = self.pparam[id]
  587. win = self.FindWindowById(id)
  588. if param['type'] == 'zone':
  589. val = event.GetInt()
  590. if val < 1:
  591. win.SetValue(1)
  592. elif val > 60:
  593. win.SetValue(60)
  594. if param['type'] == 'bool':
  595. param['value'] = event.GetSelection()
  596. else:
  597. param['value'] = val
  598. event.Skip()
  599. def OnPageChange(self,event=None):
  600. """!Go to next page"""
  601. if event.GetDirection():
  602. self.p4projparams = ''
  603. for id, param in self.pparam.iteritems():
  604. if param['type'] == 'bool':
  605. if param['value'] == False:
  606. continue
  607. else:
  608. self.p4projparams += (' +' + param['proj4'])
  609. else:
  610. if param['value'] is None:
  611. wx.MessageBox(parent = self,
  612. message = _('You must enter a value for %s') % param['desc'],
  613. caption = _('Error'), style = wx.ICON_ERROR | wx.CENTRE)
  614. event.Veto()
  615. else:
  616. self.p4projparams += (' +' + param['proj4'] + '=' + str(param['value']))
  617. def OnEnterPage(self,event):
  618. """!Page entered"""
  619. self.projdesc = self.parent.projections[self.parent.projpage.proj][0]
  620. if self.prjParamSizer is None:
  621. # entering page for the first time
  622. self.paramSBox = wx.StaticBox(parent = self, id = wx.ID_ANY,
  623. label = _(" Enter parameters for %s projection ") % self.projdesc)
  624. paramSBSizer = wx.StaticBoxSizer(self.paramSBox)
  625. self.panel = scrolled.ScrolledPanel(parent = self, id = wx.ID_ANY)
  626. self.panel.SetupScrolling()
  627. self.prjParamSizer = wx.GridBagSizer(vgap = 0, hgap = 0)
  628. self.sizer.Add(item = paramSBSizer, pos = (1, 1),
  629. flag = wx.EXPAND)
  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.AddGrowableCol(4)
  726. self.sizer.Add(item = self.MakeLabel(_("Datum code:")),
  727. flag = wx.ALIGN_LEFT |
  728. wx.ALIGN_CENTER_VERTICAL |
  729. wx.ALL, border = 5, pos = (1, 1))
  730. self.sizer.Add(item = self.tdatum,
  731. flag = wx.ALIGN_LEFT |
  732. wx.ALIGN_CENTER_VERTICAL |
  733. wx.ALL, border = 5, pos = (1, 2))
  734. self.sizer.Add(item = self.MakeLabel(_("Search in description:")),
  735. flag = wx.ALIGN_LEFT |
  736. wx.ALIGN_CENTER_VERTICAL |
  737. wx.ALL, border = 5, pos = (2, 1))
  738. self.sizer.Add(item = self.searchb,
  739. flag = wx.ALIGN_LEFT |
  740. wx.ALIGN_CENTER_VERTICAL |
  741. wx.ALL, border = 5, pos = (2, 2))
  742. self.sizer.AddGrowableRow(3)
  743. self.sizer.Add(item = self.datumlist,
  744. flag = wx.EXPAND |
  745. wx.ALIGN_LEFT |
  746. wx.ALL, border = 5, pos = (3, 1), span = (1, 4))
  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.AddGrowableCol(4)
  869. self.sizer.Add(item = self.MakeLabel(_("Ellipsoid code:")),
  870. flag = wx.ALIGN_RIGHT |
  871. wx.ALIGN_CENTER_VERTICAL |
  872. wx.ALL, border = 5, pos = (1, 1))
  873. self.sizer.Add(item = self.tellipse,
  874. flag = wx.ALIGN_LEFT |
  875. wx.ALIGN_CENTER_VERTICAL |
  876. wx.ALL, border = 5, pos = (1, 2))
  877. self.sizer.Add(item = self.radio1,
  878. flag = wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.LEFT,
  879. border = 25, pos = (1, 3))
  880. self.sizer.Add(item = self.MakeLabel(_("Search in description:")),
  881. flag = wx.ALIGN_RIGHT |
  882. wx.ALIGN_CENTER_VERTICAL |
  883. wx.ALL, border = 5, pos = (2, 1))
  884. self.sizer.Add(item = self.searchb,
  885. flag = wx.ALIGN_LEFT |
  886. wx.ALIGN_CENTER_VERTICAL |
  887. wx.ALL, border = 5, pos = (2, 2))
  888. self.sizer.Add(item = self.radio2,
  889. flag = wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.LEFT,
  890. border = 25, pos = (2, 3))
  891. self.sizer.AddGrowableRow(3)
  892. self.sizer.Add(item = self.ellipselist,
  893. flag = wx.EXPAND |
  894. wx.ALIGN_LEFT |
  895. wx.ALL, border = 5, pos = (3, 1), span = (1, 4))
  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.AddGrowableCol(3)
  988. self.sizer.Add(item = self.lfile, flag = wx.ALIGN_LEFT |
  989. wx.ALIGN_CENTRE_VERTICAL |
  990. wx.ALL, border = 5, pos = (1, 1))
  991. self.sizer.Add(item = self.tfile, flag = wx.ALIGN_LEFT |
  992. wx.ALIGN_CENTRE_VERTICAL |
  993. wx.ALL, border = 5, pos = (1, 2))
  994. self.sizer.Add(item = self.bbrowse, flag = wx.ALIGN_LEFT |
  995. wx.ALL, border = 5, pos = (1, 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 WKT or PRJ file"))
  1040. self.wktfile = ''
  1041. # create controls
  1042. self.lfile= self.MakeLabel(_("WKT or PRJ file:"))
  1043. self.tfile = self.MakeTextCtrl(size = (300,-1))
  1044. self.bbrowse = self.MakeButton(_("Browse"))
  1045. # do layout
  1046. self.sizer.AddGrowableCol(3)
  1047. self.sizer.Add(item = self.lfile, flag = wx.ALIGN_LEFT |
  1048. wx.ALIGN_CENTRE_VERTICAL |
  1049. wx.ALL, border = 5, pos = (1, 1))
  1050. self.sizer.Add(item = self.tfile, flag = wx.ALIGN_LEFT |
  1051. wx.ALIGN_CENTRE_VERTICAL |
  1052. wx.ALL, border = 5, pos = (1, 2))
  1053. self.sizer.Add(item = self.bbrowse, flag = wx.ALIGN_LEFT |
  1054. wx.ALL, border = 5, pos = (1, 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 WKT or 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.AddGrowableCol(3)
  1123. self.sizer.Add(item = self.lfile,
  1124. flag = wx.ALIGN_LEFT |
  1125. wx.ALIGN_CENTER_VERTICAL |
  1126. wx.ALL, border = 5, pos = (1, 1), span = (1, 2))
  1127. self.sizer.Add(item = self.tfile,
  1128. flag = wx.ALIGN_LEFT |
  1129. wx.ALIGN_CENTER_VERTICAL |
  1130. wx.ALL, border = 5, pos = (1, 3))
  1131. self.sizer.Add(item = self.bbrowse,
  1132. flag = wx.ALIGN_LEFT |
  1133. wx.ALIGN_CENTER_VERTICAL |
  1134. wx.ALL, border = 5, pos = (1, 4))
  1135. self.sizer.Add(item = self.lcode,
  1136. flag = wx.ALIGN_LEFT |
  1137. wx.ALIGN_CENTER_VERTICAL |
  1138. wx.ALL, border = 5, pos = (2, 1), span = (1, 2))
  1139. self.sizer.Add(item = self.tcode,
  1140. flag = wx.ALIGN_LEFT |
  1141. wx.ALIGN_CENTER_VERTICAL |
  1142. wx.ALL, border = 5, pos = (2, 3))
  1143. self.sizer.Add(item = self.searchb,
  1144. flag = wx.ALIGN_LEFT |
  1145. wx.ALIGN_CENTER_VERTICAL |
  1146. wx.ALL, border = 5, pos = (3, 3))
  1147. self.sizer.AddGrowableRow(4)
  1148. self.sizer.Add(item = self.epsglist,
  1149. flag = wx.ALIGN_LEFT | wx.EXPAND, pos = (4, 1),
  1150. span = (1, 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. self.searchb.SetValue('')
  1233. event.Skip()
  1234. def OnBrowse(self, event):
  1235. """!Define path for EPSG code file"""
  1236. path = os.path.dirname(self.tfile.GetValue())
  1237. if not path:
  1238. path = os.getcwd()
  1239. dlg = wx.FileDialog(parent = self, message = _("Choose EPSG codes file"),
  1240. defaultDir = path, defaultFile = "", wildcard = "*", style = wx.OPEN)
  1241. if dlg.ShowModal() == wx.ID_OK:
  1242. path = dlg.GetPath()
  1243. self.tfile.SetValue(path)
  1244. self.OnBrowseCodes(None)
  1245. dlg.Destroy()
  1246. event.Skip()
  1247. def OnItemSelected(self, event):
  1248. """!EPSG code selected from the list"""
  1249. index = event.m_itemIndex
  1250. item = event.GetItem()
  1251. self.epsgcode = int(self.epsglist.GetItem(index, 0).GetText())
  1252. self.epsgdesc = self.epsglist.GetItem(index, 1).GetText()
  1253. self.tcode.SetValue(str(self.epsgcode))
  1254. event.Skip()
  1255. def OnBrowseCodes(self, event, search = None):
  1256. """!Browse EPSG codes"""
  1257. self.epsgCodeDict = utils.ReadEpsgCodes(self.tfile.GetValue())
  1258. if type(self.epsgCodeDict) != dict:
  1259. wx.MessageBox(parent = self,
  1260. message = _("Unable to read EPGS codes: %s") % self.epsgCodeDict,
  1261. caption = _("Error"), style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
  1262. self.epsglist.Populate(list(), update = True)
  1263. return
  1264. data = list()
  1265. for code, val in self.epsgCodeDict.iteritems():
  1266. if code is not None:
  1267. data.append((code, val[0], val[1]))
  1268. self.epsglist.Populate(data, update = True)
  1269. class CustomPage(TitledPage):
  1270. """!Wizard page for entering custom PROJ.4 string
  1271. for setting coordinate system parameters"""
  1272. def __init__(self, wizard, parent):
  1273. TitledPage.__init__(self, wizard,
  1274. _("Choose method of specifying georeferencing parameters"))
  1275. global coordsys
  1276. self.customstring = ''
  1277. self.parent = parent
  1278. # widgets
  1279. self.text_proj4string = self.MakeTextCtrl(size = (400, 200),
  1280. style = wx.TE_MULTILINE)
  1281. self.label_proj4string = self.MakeLabel(_("Enter PROJ.4 parameters string:"))
  1282. # layout
  1283. self.sizer.AddGrowableCol(2)
  1284. self.sizer.Add(self.label_proj4string,
  1285. flag = wx.ALIGN_LEFT | wx.ALL,
  1286. border = 5, pos = (1, 1))
  1287. self.sizer.AddGrowableRow(2)
  1288. self.sizer.Add(self.text_proj4string,
  1289. flag = wx.ALIGN_LEFT | wx.ALL | wx.EXPAND,
  1290. border = 5, pos = (2, 1), span = (1, 2))
  1291. self.text_proj4string.Bind(wx.EVT_TEXT, self.GetProjstring)
  1292. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  1293. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1294. def OnEnterPage(self, event):
  1295. if len(self.customstring) == 0:
  1296. # disable 'next' button by default
  1297. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1298. else:
  1299. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1300. def OnPageChanging(self, event):
  1301. if event.GetDirection():
  1302. self.custom_dtrans_string = ''
  1303. if self.customstring.find('+datum=') < 0:
  1304. self.GetNext().SetPrev(self)
  1305. return
  1306. # check for datum tranforms
  1307. # FIXME: -t flag is a hack-around for trac bug #1849
  1308. ret, out, err = RunCommand('g.proj',
  1309. read = True, getErrorMsg = True,
  1310. proj4 = self.customstring,
  1311. datum_trans = '-1',
  1312. flags = 't')
  1313. if ret != 0:
  1314. wx.MessageBox(parent = self,
  1315. message = err,
  1316. caption = _("Error"),
  1317. style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
  1318. event.Veto()
  1319. return
  1320. if out:
  1321. dtrans = ''
  1322. # open a dialog to select datum transform number
  1323. dlg = SelectTransformDialog(self.parent.parent, transforms = out)
  1324. if dlg.ShowModal() == wx.ID_OK:
  1325. dtrans = dlg.GetTransform()
  1326. if dtrans == '':
  1327. dlg.Destroy()
  1328. event.Veto()
  1329. return _('Datum transform is required.')
  1330. else:
  1331. dlg.Destroy()
  1332. event.Veto()
  1333. return _('Datum transform is required.')
  1334. self.parent.datum_trans = dtrans
  1335. # prepare +nadgrids or +towgs84 terms for Summary page. first convert them:
  1336. ret, projlabel, err = RunCommand('g.proj',
  1337. flags = 'jft',
  1338. proj4 = self.customstring,
  1339. datum_trans = dtrans,
  1340. getErrorMsg = True,
  1341. read = True)
  1342. # splitting on space alone would break for grid files with space in pathname
  1343. for projterm in projlabel.split(' +'):
  1344. if projterm.find("towgs84=") != -1 or projterm.find("nadgrids=") != -1:
  1345. self.custom_dtrans_string = ' +%s' % projterm
  1346. break
  1347. self.GetNext().SetPrev(self)
  1348. def GetProjstring(self, event):
  1349. """!Change proj string"""
  1350. # TODO: check PROJ.4 syntax
  1351. self.customstring = event.GetString()
  1352. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  1353. if len(self.customstring) == 0:
  1354. if nextButton.IsEnabled():
  1355. nextButton.Enable(False)
  1356. else:
  1357. if not nextButton.IsEnabled():
  1358. nextButton.Enable()
  1359. class SummaryPage(TitledPage):
  1360. """!Shows summary result of choosing coordinate system parameters
  1361. prior to creating location"""
  1362. def __init__(self, wizard, parent):
  1363. TitledPage.__init__(self, wizard, _("Summary"))
  1364. self.parent = parent
  1365. self.panelTitle = scrolled.ScrolledPanel(parent = self, id = wx.ID_ANY)
  1366. self.panelProj4string = scrolled.ScrolledPanel(parent = self, id = wx.ID_ANY)
  1367. self.panelProj = scrolled.ScrolledPanel(parent = self, id = wx.ID_ANY)
  1368. # labels
  1369. self.ldatabase = self.MakeLabel()
  1370. self.llocation = self.MakeLabel()
  1371. self.llocTitle = self.MakeLabel(parent = self.panelTitle)
  1372. self.lprojection = self.MakeLabel(parent = self.panelProj)
  1373. self.lproj4string = self.MakeLabel(parent = self.panelProj4string)
  1374. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1375. # do sub-page layout
  1376. self._doLayout()
  1377. def _doLayout(self):
  1378. """!Do page layout"""
  1379. self.sizer.AddGrowableCol(1)
  1380. self.sizer.AddGrowableRow(3, 1)
  1381. self.sizer.AddGrowableRow(4, 1)
  1382. self.sizer.AddGrowableRow(5, 5)
  1383. titleSizer = wx.BoxSizer(wx.VERTICAL)
  1384. titleSizer.Add(item = self.llocTitle, proportion = 1,
  1385. flag = wx.EXPAND | wx.ALL, border = 5)
  1386. self.panelTitle.SetSizer(titleSizer)
  1387. projSizer = wx.BoxSizer(wx.VERTICAL)
  1388. projSizer.Add(item = self.lprojection, proportion = 1,
  1389. flag = wx.EXPAND | wx.ALL, border = 5)
  1390. self.panelProj.SetSizer(projSizer)
  1391. proj4stringSizer = wx.BoxSizer(wx.VERTICAL)
  1392. proj4stringSizer.Add(item = self.lproj4string, proportion = 1,
  1393. flag = wx.EXPAND | wx.ALL, border = 5)
  1394. self.panelProj4string.SetSizer(proj4stringSizer)
  1395. self.panelProj4string.SetupScrolling()
  1396. self.panelProj.SetupScrolling(scroll_y = False)
  1397. self.panelTitle.SetupScrolling(scroll_y = False)
  1398. self.sizer.Add(item = self.MakeLabel(_("GRASS Database:")),
  1399. flag = wx.ALIGN_LEFT | wx.ALL,
  1400. border = 5, pos = (1, 0))
  1401. self.sizer.Add(item = self.ldatabase,
  1402. flag = wx.ALIGN_LEFT | wx.ALL,
  1403. border = 5, pos = (1, 1))
  1404. self.sizer.Add(item = self.MakeLabel(_("Location Name:")),
  1405. flag = wx.ALIGN_LEFT | wx.ALL,
  1406. border = 5, pos = (2, 0))
  1407. self.sizer.Add(item = self.llocation,
  1408. flag = wx.ALIGN_LEFT | wx.ALL,
  1409. border = 5, pos = (2, 1))
  1410. self.sizer.Add(item = self.MakeLabel(_("Location Title:")),
  1411. flag = wx.ALIGN_LEFT | wx.ALL,
  1412. border = 5, pos = (3, 0))
  1413. self.sizer.Add(item = self.panelTitle,
  1414. flag = wx.ALIGN_LEFT | wx.ALL | wx.EXPAND,
  1415. border = 0, pos = (3, 1))
  1416. self.sizer.Add(item = self.MakeLabel(_("Projection:")),
  1417. flag = wx.ALIGN_LEFT | wx.ALL,
  1418. border = 5, pos = (4, 0))
  1419. self.sizer.Add(item = self.panelProj,
  1420. flag = wx.ALIGN_LEFT | wx.ALL | wx.EXPAND,
  1421. border = 0, pos = (4, 1))
  1422. self.sizer.Add(item = self.MakeLabel(_("PROJ.4 definition:")),
  1423. flag = wx.ALIGN_LEFT | wx.ALL,
  1424. border = 5, pos = (5, 0))
  1425. self.sizer.Add(item = self.panelProj4string,
  1426. flag = wx.ALIGN_LEFT | wx.ALL | wx.EXPAND,
  1427. border = 0, pos = (5, 1))
  1428. def OnEnterPage(self, event):
  1429. """!Insert values into text controls for summary of location
  1430. creation options
  1431. """
  1432. database = self.parent.startpage.grassdatabase
  1433. location = self.parent.startpage.location
  1434. proj4string = self.parent.CreateProj4String()
  1435. epsgcode = self.parent.epsgpage.epsgcode
  1436. datum = self.parent.datumpage.datum
  1437. dtrans = self.parent.datum_trans
  1438. global coordsys
  1439. #print coordsys,proj4string
  1440. if coordsys in ('proj', 'epsg'):
  1441. if coordsys == 'proj':
  1442. addl_opts = {}
  1443. if len(datum) > 0:
  1444. addl_opts['datum'] = '%s' % datum
  1445. addl_opts['datum_trans'] = dtrans
  1446. ret, projlabel, err = RunCommand('g.proj',
  1447. flags = 'jf',
  1448. proj4 = proj4string,
  1449. location = location,
  1450. getErrorMsg = True,
  1451. read = True,
  1452. **addl_opts)
  1453. elif coordsys == 'epsg':
  1454. ret, projlabel, err = RunCommand('g.proj',
  1455. flags = 'jft',
  1456. epsg = epsgcode,
  1457. datum_trans = dtrans,
  1458. location = location,
  1459. getErrorMsg = True,
  1460. read = True)
  1461. finishButton = wx.FindWindowById(wx.ID_FORWARD)
  1462. if ret == 0:
  1463. if datum != '':
  1464. projlabel = projlabel + '+datum=%s' % datum
  1465. self.lproj4string.SetLabel(projlabel.replace(' +', os.linesep + '+'))
  1466. finishButton.Enable(True)
  1467. else:
  1468. GError(err, parent = self)
  1469. self.lproj4string.SetLabel('')
  1470. finishButton.Enable(False)
  1471. projdesc = self.parent.projpage.projdesc
  1472. ellipsedesc = self.parent.ellipsepage.ellipsedesc
  1473. datumdesc = self.parent.datumpage.datumdesc
  1474. #print projdesc,ellipsedesc,datumdesc
  1475. self.ldatabase.SetLabel(database)
  1476. self.llocation.SetLabel(location)
  1477. self.llocTitle.SetLabel(self.parent.startpage.locTitle)
  1478. label = ''
  1479. if coordsys == 'epsg':
  1480. label = 'EPSG code %s (%s)' % (self.parent.epsgpage.epsgcode, self.parent.epsgpage.epsgdesc)
  1481. elif coordsys == 'file':
  1482. label = 'matches file %s' % self.parent.filepage.georeffile
  1483. self.lproj4string.SetLabel("")
  1484. elif coordsys == 'wkt':
  1485. label = 'matches file %s' % self.parent.wktpage.wktfile
  1486. self.lproj4string.SetLabel("")
  1487. elif coordsys == 'proj':
  1488. label = ('%s, %s %s' % (projdesc, datumdesc, ellipsedesc))
  1489. elif coordsys == 'xy':
  1490. label = ('XY coordinate system (not projected).')
  1491. self.lproj4string.SetLabel("")
  1492. elif coordsys == 'custom':
  1493. label = _("custom")
  1494. combo_str = self.parent.custompage.customstring + self.parent.custompage.custom_dtrans_string
  1495. self.lproj4string.SetLabel(('%s' % combo_str.replace(' +', os.linesep + '+')))
  1496. self.lprojection.SetLabel(label)
  1497. def OnFinish(self, event):
  1498. dlg = wx.MessageDialog(parent = self.wizard,
  1499. message = _("Do you want to create GRASS location <%s>?") % location,
  1500. caption = _("Create new location?"),
  1501. style = wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
  1502. if dlg.ShowModal() == wx.ID_NO:
  1503. dlg.Destroy()
  1504. event.Veto()
  1505. else:
  1506. dlg.Destroy()
  1507. event.Skip()
  1508. class LocationWizard(wx.Object):
  1509. """!Start wizard here and finish wizard here
  1510. """
  1511. def __init__(self, parent, grassdatabase):
  1512. self.__cleanUp()
  1513. global coordsys
  1514. self.parent = parent
  1515. #
  1516. # define wizard image
  1517. #
  1518. imagePath = os.path.join(globalvar.ETCIMGDIR, "loc_wizard_qgis.png")
  1519. wizbmp = wx.Image(imagePath, wx.BITMAP_TYPE_PNG)
  1520. wizbmp = wizbmp.ConvertToBitmap()
  1521. #
  1522. # get georeferencing information from tables in $GISBASE/etc
  1523. #
  1524. self.__readData()
  1525. #
  1526. # datum transform number and list of datum transforms
  1527. #
  1528. self.datum_trans = None
  1529. self.proj4string = ''
  1530. # file from which new location is created
  1531. self.georeffile = None
  1532. #
  1533. # define wizard pages
  1534. #
  1535. self.wizard = WizardWithHelpButton(parent, id = wx.ID_ANY, title = _("Define new GRASS Location"),
  1536. bitmap = wizbmp)
  1537. self.wizard.Bind(wiz.EVT_WIZARD_HELP, self.OnHelp)
  1538. self.startpage = DatabasePage(self.wizard, self, grassdatabase)
  1539. self.csystemspage = CoordinateSystemPage(self.wizard, self)
  1540. self.projpage = ProjectionsPage(self.wizard, self)
  1541. self.datumpage = DatumPage(self.wizard, self)
  1542. self.paramspage = ProjParamsPage(self.wizard,self)
  1543. self.epsgpage = EPSGPage(self.wizard, self)
  1544. self.filepage = GeoreferencedFilePage(self.wizard, self)
  1545. self.wktpage = WKTPage(self.wizard, self)
  1546. self.ellipsepage = EllipsePage(self.wizard, self)
  1547. self.custompage = CustomPage(self.wizard, self)
  1548. self.sumpage = SummaryPage(self.wizard, self)
  1549. #
  1550. # set the initial order of the pages
  1551. # (should follow the epsg line)
  1552. #
  1553. self.startpage.SetNext(self.csystemspage)
  1554. self.csystemspage.SetPrev(self.startpage)
  1555. self.csystemspage.SetNext(self.sumpage)
  1556. self.projpage.SetPrev(self.csystemspage)
  1557. self.projpage.SetNext(self.paramspage)
  1558. self.paramspage.SetPrev(self.projpage)
  1559. self.paramspage.SetNext(self.datumpage)
  1560. self.datumpage.SetPrev(self.paramspage)
  1561. self.datumpage.SetNext(self.sumpage)
  1562. self.ellipsepage.SetPrev(self.paramspage)
  1563. self.ellipsepage.SetNext(self.sumpage)
  1564. self.epsgpage.SetPrev(self.csystemspage)
  1565. self.epsgpage.SetNext(self.sumpage)
  1566. self.filepage.SetPrev(self.csystemspage)
  1567. self.filepage.SetNext(self.sumpage)
  1568. self.wktpage.SetPrev(self.csystemspage)
  1569. self.wktpage.SetNext(self.sumpage)
  1570. self.custompage.SetPrev(self.csystemspage)
  1571. self.custompage.SetNext(self.sumpage)
  1572. self.sumpage.SetPrev(self.csystemspage)
  1573. #
  1574. # do pages layout
  1575. #
  1576. self.startpage.DoLayout()
  1577. self.csystemspage.DoLayout()
  1578. self.projpage.DoLayout()
  1579. self.datumpage.DoLayout()
  1580. self.paramspage.DoLayout()
  1581. self.epsgpage.DoLayout()
  1582. self.filepage.DoLayout()
  1583. self.wktpage.DoLayout()
  1584. self.ellipsepage.DoLayout()
  1585. self.custompage.DoLayout()
  1586. self.sumpage.DoLayout()
  1587. self.wizard.FitToPage(self.datumpage)
  1588. size = self.wizard.GetPageSize()
  1589. self.wizard.SetPageSize((size[0], size[1] + 75))
  1590. # new location created?
  1591. self.location = None
  1592. success = False
  1593. # location created in different GIS database?
  1594. self.altdb = False
  1595. #
  1596. # run wizard...
  1597. #
  1598. if self.wizard.RunWizard(self.startpage):
  1599. msg = self.OnWizFinished()
  1600. if not msg:
  1601. self.wizard.Destroy()
  1602. self.location = self.startpage.location
  1603. self.grassdatabase = self.startpage.grassdatabase
  1604. self.georeffile = self.filepage.georeffile
  1605. # FIXME here was code for setting default region, what for is this if:
  1606. # if self.altdb == False:
  1607. else: # -> error
  1608. self.wizard.Destroy()
  1609. GError(parent = self.parent,
  1610. message = "%s" % _("Unable to create new location. "
  1611. "Location <%(loc)s> not created.\n\n"
  1612. "Details: %(err)s") % \
  1613. { 'loc' : self.startpage.location,
  1614. 'err' : msg })
  1615. else: # -> canceled
  1616. self.wizard.Destroy()
  1617. GMessage(parent = self.parent,
  1618. message = _("Location wizard canceled. "
  1619. "Location not created."))
  1620. self.__cleanUp()
  1621. def __cleanUp(self):
  1622. global coordsys
  1623. global north
  1624. global south
  1625. global east
  1626. global west
  1627. global resolution
  1628. global wizerror
  1629. global translist
  1630. coordsys = None
  1631. north = None
  1632. south = None
  1633. east = None
  1634. west = None
  1635. resolution = None
  1636. transformlist = list()
  1637. def __readData(self):
  1638. """!Get georeferencing information from tables in $GISBASE/etc/proj"""
  1639. # read projection and parameters
  1640. f = open(os.path.join(globalvar.ETCDIR, "proj", "parms.table"), "r")
  1641. self.projections = {}
  1642. self.projdesc = {}
  1643. for line in f.readlines():
  1644. line = line.strip()
  1645. try:
  1646. proj, projdesc, params = line.split(':')
  1647. paramslist = params.split(';')
  1648. plist = []
  1649. for p in paramslist:
  1650. if p == '': continue
  1651. p1, pdefault = p.split(',')
  1652. pterm, pask = p1.split('=')
  1653. p = [pterm.strip(), pask.strip(), pdefault.strip()]
  1654. plist.append(p)
  1655. self.projections[proj.lower().strip()] = (projdesc.strip(), plist)
  1656. self.projdesc[proj.lower().strip()] = projdesc.strip()
  1657. except:
  1658. continue
  1659. f.close()
  1660. # read datum definitions
  1661. f = open(os.path.join(globalvar.ETCDIR, "proj", "datum.table"), "r")
  1662. self.datums = {}
  1663. paramslist = []
  1664. for line in f.readlines():
  1665. line = line.expandtabs(1)
  1666. line = line.strip()
  1667. if line == '' or line[0] == "#":
  1668. continue
  1669. datum, info = line.split(" ", 1)
  1670. info = info.strip()
  1671. datumdesc, params = info.split(" ", 1)
  1672. datumdesc = datumdesc.strip('"')
  1673. paramlist = params.split()
  1674. ellipsoid = paramlist.pop(0)
  1675. self.datums[datum] = (ellipsoid, datumdesc.replace('_', ' '), paramlist)
  1676. f.close()
  1677. # read Earth-based ellipsiod definitions
  1678. f = open(os.path.join(globalvar.ETCDIR, "proj", "ellipse.table"), "r")
  1679. self.ellipsoids = {}
  1680. for line in f.readlines():
  1681. line = line.expandtabs(1)
  1682. line = line.strip()
  1683. if line == '' or line[0] == "#":
  1684. continue
  1685. ellipse, rest = line.split(" ", 1)
  1686. rest = rest.strip('" ')
  1687. desc, params = rest.split('"', 1)
  1688. desc = desc.strip('" ')
  1689. paramslist = params.split()
  1690. self.ellipsoids[ellipse] = (desc, paramslist)
  1691. f.close()
  1692. # read Planetary ellipsiod definitions
  1693. f = open(os.path.join(globalvar.ETCDIR, "proj", "ellipse.table.solar.system"), "r")
  1694. self.planetary_ellipsoids = {}
  1695. for line in f.readlines():
  1696. line = line.expandtabs(1)
  1697. line = line.strip()
  1698. if line == '' or line[0] == "#":
  1699. continue
  1700. ellipse, rest = line.split(" ", 1)
  1701. rest = rest.strip('" ')
  1702. desc, params = rest.split('"', 1)
  1703. desc = desc.strip('" ')
  1704. paramslist = params.split()
  1705. self.planetary_ellipsoids[ellipse] = (desc, paramslist)
  1706. f.close()
  1707. # read projection parameter description and parsing table
  1708. f = open(os.path.join(globalvar.ETCDIR, "proj", "desc.table"), "r")
  1709. self.paramdesc = {}
  1710. for line in f.readlines():
  1711. line = line.strip()
  1712. try:
  1713. pparam, datatype, proj4term, desc = line.split(':')
  1714. self.paramdesc[pparam] = (datatype, proj4term, desc)
  1715. except:
  1716. continue
  1717. f.close()
  1718. def OnWizFinished(self):
  1719. """!Wizard finished, create new location
  1720. @return error message on error
  1721. @return None on success
  1722. """
  1723. database = self.startpage.grassdatabase
  1724. location = self.startpage.location
  1725. # location already exists?
  1726. if os.path.isdir(os.path.join(database,location)):
  1727. GError(parent = self.wizard,
  1728. message = "%s <%s>: %s" % \
  1729. (_("Unable to create new location"),
  1730. os.path.join(database, location),
  1731. _("Location already exists in GRASS Database.")))
  1732. return None
  1733. # current GISDbase or a new one?
  1734. current_gdb = grass.gisenv()['GISDBASE']
  1735. if current_gdb != database:
  1736. # change to new GISDbase or create new one
  1737. if os.path.isdir(database) != True:
  1738. # create new directory
  1739. os.mkdir(database)
  1740. # change to new GISDbase directory
  1741. RunCommand('g.gisenv',
  1742. parent = self.wizard,
  1743. set = 'GISDBASE=%s' % database)
  1744. wx.MessageBox(parent = self.wizard,
  1745. message = _("Location <%(loc)s> will be created "
  1746. "in GIS data directory <%(dir)s>. "
  1747. "You will need to change the default GIS "
  1748. "data directory in the GRASS startup screen.") % \
  1749. { 'loc' : location, 'dir' : database},
  1750. caption = _("New GIS data directory"),
  1751. style = wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
  1752. # location created in alternate GISDbase
  1753. self.altdb = True
  1754. global coordsys
  1755. try:
  1756. if coordsys == "xy":
  1757. grass.create_location(dbase = self.startpage.grassdatabase,
  1758. location = self.startpage.location,
  1759. desc = self.startpage.locTitle)
  1760. elif coordsys == "proj":
  1761. grass.create_location(dbase = self.startpage.grassdatabase,
  1762. location = self.startpage.location,
  1763. proj4 = self.CreateProj4String(),
  1764. datum = self.datumpage.datum,
  1765. datum_trans = self.datum_trans,
  1766. desc = self.startpage.locTitle)
  1767. elif coordsys == 'custom':
  1768. addl_opts = {}
  1769. if self.datum_trans is not None:
  1770. addl_opts['datum_trans'] = self.datum_trans
  1771. grass.create_location(dbase = self.startpage.grassdatabase,
  1772. location = self.startpage.location,
  1773. proj4 = self.custompage.customstring,
  1774. desc = self.startpage.locTitle,
  1775. **addl_opts)
  1776. elif coordsys == "epsg":
  1777. if not self.epsgpage.epsgcode:
  1778. return _('EPSG code missing.')
  1779. grass.create_location(dbase = self.startpage.grassdatabase,
  1780. location = self.startpage.location,
  1781. epsg = self.epsgpage.epsgcode,
  1782. datum = self.datumpage.datum,
  1783. datum_trans = self.datum_trans,
  1784. desc = self.startpage.locTitle)
  1785. elif coordsys == "file":
  1786. if not self.filepage.georeffile or \
  1787. not os.path.isfile(self.filepage.georeffile):
  1788. return _("File <%s> not found." % self.filepage.georeffile)
  1789. grass.create_location(dbase = self.startpage.grassdatabase,
  1790. location = self.startpage.location,
  1791. filename = self.filepage.georeffile,
  1792. desc = self.startpage.locTitle)
  1793. elif coordsys == "wkt":
  1794. if not self.wktpage.wktfile or \
  1795. not os.path.isfile(self.wktpage.wktfile):
  1796. return _("File <%s> not found." % self.wktpage.wktfile)
  1797. grass.create_location(dbase = self.startpage.grassdatabase,
  1798. location = self.startpage.location,
  1799. wkt = self.wktpage.wktfile,
  1800. desc = self.startpage.locTitle)
  1801. except grass.ScriptError, e:
  1802. return e.value
  1803. return None
  1804. def CreateProj4String(self):
  1805. """!Constract PROJ.4 string"""
  1806. location = self.startpage.location
  1807. proj = self.projpage.p4proj
  1808. projdesc = self.projpage.projdesc
  1809. proj4params = self.paramspage.p4projparams
  1810. # datum = self.datumpage.datum
  1811. if self.datumpage.datumdesc:
  1812. datumdesc = self.datumpage.datumdesc +' - ' + self.datumpage.ellipse
  1813. else:
  1814. datumdesc = ''
  1815. datumparams = self.datumpage.datumparams
  1816. ellipse = self.ellipsepage.ellipse
  1817. ellipsedesc = self.ellipsepage.ellipsedesc
  1818. ellipseparams = self.ellipsepage.ellipseparams
  1819. #
  1820. # creating PROJ.4 string
  1821. #
  1822. proj4string = '%s %s' % (proj, proj4params)
  1823. # set ellipsoid parameters
  1824. if ellipse != '':
  1825. proj4string = '%s +ellps=%s' % (proj4string, ellipse)
  1826. for item in ellipseparams:
  1827. if item[:4] == 'f=1/':
  1828. item = ' +rf=' + item[4:]
  1829. else:
  1830. item = ' +' + item
  1831. proj4string = '%s %s' % (proj4string, item)
  1832. # set datum transform parameters if relevant
  1833. if datumparams:
  1834. for item in datumparams:
  1835. proj4string = '%s +%s' % (proj4string,item)
  1836. proj4string = '%s +no_defs' % proj4string
  1837. return proj4string
  1838. def OnHelp(self, event):
  1839. """'Help' button clicked"""
  1840. # help text in lib/init/helptext.html
  1841. RunCommand('g.manual', entry = 'helptext')
  1842. class WizardWithHelpButton(wiz.Wizard):
  1843. def __init__(self, parent, id, title, bitmap):
  1844. pre = wiz.PreWizard()
  1845. pre.SetExtraStyle(wx.wizard.WIZARD_EX_HELPBUTTON)
  1846. pre.Create(parent = parent, id = id, title = title, bitmap = bitmap)
  1847. self.PostCreate(pre)