wizard.py 85 KB

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