wizard.py 85 KB

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