wizard.py 82 KB

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