wizard.py 85 KB

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