location_wizard.py 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629
  1. """!
  2. @package location_wizard.py
  3. @brief Location wizard - creates a new GRASS Location. User can choose
  4. from multiple methods.
  5. Classes:
  6. - BaseClass
  7. - TitledPage
  8. - DatabasePage
  9. - CoordinateSystemPage
  10. - ProjectionsPage
  11. - ItemList
  12. - ProjParamsPage
  13. - DatumPage
  14. - EllipsePage
  15. - GeoreferencedFilePage
  16. - EPSGPage
  17. - CustomPage
  18. - SummaryPage
  19. - RegionDef
  20. - LocationWizard
  21. - SelectTransformDialog
  22. (C) 2007-2010 by the GRASS Development Team
  23. This program is free software under the GNU General Public License
  24. (>=v2). Read the file COPYING that comes with GRASS for details.
  25. @author Michael Barton
  26. @author Jachym Cepicky
  27. @author Martin Landa <landa.martin gmail.com>
  28. """
  29. import os
  30. import shutil
  31. import string
  32. import sys
  33. import locale
  34. import platform
  35. import wx
  36. import wx.lib.mixins.listctrl as listmix
  37. import wx.wizard as wiz
  38. import wx.lib.scrolledpanel as scrolled
  39. import time
  40. import gcmd
  41. import globalvar
  42. import utils
  43. from grass.script import core as grass
  44. try:
  45. import subprocess
  46. except:
  47. CompatPath = os.path.join(globalvar.ETCWXDIR)
  48. sys.path.append(CompatPath)
  49. from compat import subprocess
  50. global coordsys
  51. global north
  52. global south
  53. global east
  54. global west
  55. global resolution
  56. global wizerror
  57. global translist
  58. class BaseClass(wx.Object):
  59. """!Base class providing basic methods"""
  60. def __init__(self):
  61. pass
  62. def MakeLabel(self, text="", style=wx.ALIGN_LEFT, parent=None):
  63. """!Make aligned label"""
  64. if not parent:
  65. parent = self
  66. return wx.StaticText(parent=parent, id=wx.ID_ANY, label=text,
  67. style=style)
  68. def MakeTextCtrl(self, text='', size=(100,-1), style=0, parent=None):
  69. """!Generic text control"""
  70. if not parent:
  71. parent = self
  72. return wx.TextCtrl(parent=parent, id=wx.ID_ANY, value=text,
  73. size=size, style=style)
  74. def MakeButton(self, text, id=wx.ID_ANY, size=(-1,-1), parent=None):
  75. """!Generic button"""
  76. if not parent:
  77. parent = self
  78. return wx.Button(parent=parent, id=id, label=text,
  79. size=size)
  80. class TitledPage(BaseClass, wiz.WizardPageSimple):
  81. """!Class to make wizard pages. Generic methods to make labels,
  82. text entries, and buttons.
  83. """
  84. def __init__(self, parent, title):
  85. self.page = wiz.WizardPageSimple.__init__(self, parent)
  86. # page title
  87. self.title = wx.StaticText(parent=self, id=wx.ID_ANY, label=title)
  88. self.title.SetFont(wx.Font(13, wx.SWISS, wx.NORMAL, wx.BOLD))
  89. # main sizers
  90. self.pagesizer = wx.BoxSizer(wx.VERTICAL)
  91. self.sizer = wx.GridBagSizer(vgap=0, hgap=0)
  92. def DoLayout(self):
  93. """!Do page layout"""
  94. self.pagesizer.Add(item=self.title, proportion=0,
  95. flag=wx.ALIGN_CENTRE | wx.ALL,
  96. border=5)
  97. self.pagesizer.Add(item=wx.StaticLine(self, -1), proportion=0,
  98. flag=wx.EXPAND | wx.ALL,
  99. border=0)
  100. self.pagesizer.Add(item=self.sizer, proportion = 1,
  101. flag = wx.EXPAND)
  102. self.SetAutoLayout(True)
  103. self.SetSizer(self.pagesizer)
  104. self.Layout()
  105. class DatabasePage(TitledPage):
  106. """!Wizard page for setting GIS data directory and location name"""
  107. def __init__(self, wizard, parent, grassdatabase):
  108. TitledPage.__init__(self, wizard, _("Define GRASS Database and Location Name"))
  109. self.grassdatabase = grassdatabase
  110. self.location = ''
  111. # buttons
  112. self.bbrowse = self.MakeButton(_("Browse"))
  113. # text controls
  114. self.tgisdbase = self.MakeTextCtrl(grassdatabase, size=(300, -1))
  115. self.tlocation = self.MakeTextCtrl("newLocation", size=(300, -1))
  116. # layout
  117. self.sizer.AddGrowableCol(3)
  118. self.sizer.Add(item=self.MakeLabel(_("GIS Data Directory:")),
  119. flag=wx.ALIGN_RIGHT |
  120. wx.ALIGN_CENTER_VERTICAL |
  121. wx.ALL, border=5,
  122. pos=(1, 1))
  123. self.sizer.Add(item=self.tgisdbase,
  124. flag=wx.ALIGN_LEFT |
  125. wx.ALIGN_CENTER_VERTICAL |
  126. wx.ALL, border=5,
  127. pos=(1, 2))
  128. self.sizer.Add(item=self.bbrowse,
  129. flag=wx.ALIGN_LEFT |
  130. wx.ALIGN_CENTER_VERTICAL |
  131. wx.ALL, border=5,
  132. pos=(1, 3))
  133. #
  134. self.sizer.Add(item=self.MakeLabel("%s:" % _("Project Location")),
  135. flag=wx.ALIGN_RIGHT |
  136. wx.ALIGN_CENTER_VERTICAL |
  137. wx.ALL, border=5,
  138. pos=(2, 1))
  139. self.sizer.Add(item=self.tlocation,
  140. flag=wx.ALIGN_LEFT |
  141. wx.ALIGN_CENTER_VERTICAL |
  142. wx.ALL, border=5,
  143. pos=(2, 2))
  144. # bindings
  145. self.Bind(wx.EVT_BUTTON, self.OnBrowse, self.bbrowse)
  146. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  147. self.tgisdbase.Bind(wx.EVT_TEXT, self.OnChangeName)
  148. self.tlocation.Bind(wx.EVT_TEXT, self.OnChangeName)
  149. def OnChangeName(self, event):
  150. """!Name for new location was changed"""
  151. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  152. if len(event.GetString()) > 0:
  153. if not nextButton.IsEnabled():
  154. nextButton.Enable()
  155. else:
  156. nextButton.Disable()
  157. event.Skip()
  158. def OnBrowse(self, event):
  159. """!Choose GRASS data directory"""
  160. dlg = wx.DirDialog(self, _("Choose GRASS data directory:"),
  161. os.getcwd(), wx.DD_DEFAULT_STYLE)
  162. if dlg.ShowModal() == wx.ID_OK:
  163. self.grassdatabase = dlg.GetPath()
  164. self.tgisdbase.SetValue(self.grassdatabase)
  165. dlg.Destroy()
  166. def OnPageChanging(self,event=None):
  167. error = ''
  168. if os.path.isdir(os.path.join(self.tgisdbase.GetValue(), self.tlocation.GetValue())):
  169. error = _("Location already exists in GRASS Database.")
  170. if error != '':
  171. dlg = wx.MessageDialog(parent=self, message="%s <%s>.%s%s" % (_("Unable to create location"),
  172. str(self.tlocation.GetValue()),
  173. os.linesep,
  174. error),
  175. caption=_("Error"), style=wx.OK | wx.ICON_ERROR)
  176. dlg.ShowModal()
  177. dlg.Destroy()
  178. event.Veto()
  179. return
  180. self.location = self.tlocation.GetValue()
  181. self.grassdatabase = self.tgisdbase.GetValue()
  182. class CoordinateSystemPage(TitledPage):
  183. """!Wizard page for choosing method for location creation"""
  184. def __init__(self, wizard, parent):
  185. TitledPage.__init__(self, wizard, _("Choose method for creating a new location"))
  186. self.parent = parent
  187. global coordsys
  188. # toggles
  189. self.radio1 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  190. label=_("Select coordinate system parameters from a list"),
  191. style = wx.RB_GROUP)
  192. self.radio2 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  193. label=_("Select EPSG code of spatial reference system"))
  194. self.radio3 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  195. label=_("Read projection and datum terms from a "
  196. "georeferenced data file"))
  197. self.radio4 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  198. label=_("Read projection and datum terms from a "
  199. "WKT or PRJ file"))
  200. self.radio5 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  201. label=_("Specify projection and datum terms using custom "
  202. "PROJ.4 parameters"))
  203. self.radio6 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  204. label=_("Create an arbitrary non-earth coordinate system (XY)"))
  205. # layout
  206. self.sizer.AddGrowableCol(1)
  207. self.sizer.SetVGap(10)
  208. self.sizer.Add(item=self.radio1,
  209. flag=wx.ALIGN_LEFT, pos=(1, 1))
  210. self.sizer.Add(item=self.radio2,
  211. flag=wx.ALIGN_LEFT, pos=(2, 1))
  212. self.sizer.Add(item=self.radio3,
  213. flag=wx.ALIGN_LEFT, pos=(3, 1))
  214. self.sizer.Add(item=self.radio4,
  215. flag=wx.ALIGN_LEFT, pos=(4, 1))
  216. self.sizer.Add(item=self.radio5,
  217. flag=wx.ALIGN_LEFT, pos=(5, 1))
  218. self.sizer.Add(item=self.radio6,
  219. flag=wx.ALIGN_LEFT, pos=(6, 1))
  220. # bindings
  221. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio1.GetId())
  222. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio2.GetId())
  223. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio3.GetId())
  224. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio4.GetId())
  225. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio5.GetId())
  226. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id=self.radio6.GetId())
  227. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  228. # do page layout
  229. # self.DoLayout()
  230. def OnEnterPage(self, event):
  231. global coordsys
  232. if not coordsys:
  233. coordsys = "proj"
  234. self.radio1.SetValue(True)
  235. else:
  236. if coordsys == 'proj':
  237. self.radio1.SetValue(True)
  238. if coordsys == "epsg":
  239. self.radio2.SetValue(True)
  240. if coordsys == "file":
  241. self.radio3.SetValue(True)
  242. if coordsys == "wkt":
  243. self.radio4.SetValue(True)
  244. if coordsys == "custom":
  245. self.radio5.SetValue(True)
  246. if coordsys == "xy":
  247. self.radio6.SetValue(True)
  248. if event.GetDirection():
  249. self.SetNext(self.parent.projpage)
  250. self.parent.sumpage.SetPrev(self.parent.datumpage)
  251. if not wx.FindWindowById(wx.ID_FORWARD).IsEnabled():
  252. wx.FindWindowById(wx.ID_FORWARD).Enable()
  253. def SetVal(self, event):
  254. """!Choose method"""
  255. global coordsys
  256. if event.GetId() == self.radio1.GetId():
  257. coordsys = "proj"
  258. self.SetNext(self.parent.projpage)
  259. self.parent.sumpage.SetPrev(self.parent.datumpage)
  260. elif event.GetId() == self.radio2.GetId():
  261. coordsys = "epsg"
  262. self.SetNext(self.parent.epsgpage)
  263. self.parent.sumpage.SetPrev(self.parent.epsgpage)
  264. elif event.GetId() == self.radio3.GetId():
  265. coordsys = "file"
  266. self.SetNext(self.parent.filepage)
  267. self.parent.sumpage.SetPrev(self.parent.filepage)
  268. elif event.GetId() == self.radio4.GetId():
  269. coordsys = "wkt"
  270. self.SetNext(self.parent.wktpage)
  271. self.parent.sumpage.SetPrev(self.parent.wktpage)
  272. elif event.GetId() == self.radio5.GetId():
  273. coordsys = "custom"
  274. self.SetNext(self.parent.custompage)
  275. self.parent.sumpage.SetPrev(self.parent.custompage)
  276. elif event.GetId() == self.radio6.GetId():
  277. coordsys = "xy"
  278. self.SetNext(self.parent.sumpage)
  279. self.parent.sumpage.SetPrev(self.parent.csystemspage)
  280. class ProjectionsPage(TitledPage):
  281. """!Wizard page for selecting projection (select coordinate system option)"""
  282. def __init__(self, wizard, parent):
  283. TitledPage.__init__(self, wizard, _("Choose projection"))
  284. self.parent = parent
  285. self.proj = ''
  286. self.projdesc = ''
  287. self.p4proj = ''
  288. # text input
  289. self.tproj = self.MakeTextCtrl("", size=(200,-1))
  290. # search box
  291. self.searchb = wx.SearchCtrl(self, size=(200,-1),
  292. style=wx.TE_PROCESS_ENTER)
  293. # projection list
  294. self.projlist = ItemList(self, data=self.parent.projdesc.items(),
  295. columns=[_('Code'), _('Description')])
  296. self.projlist.resizeLastColumn(30)
  297. # layout
  298. self.sizer.AddGrowableCol(3)
  299. self.sizer.Add(item=self.MakeLabel(_("Projection code:")),
  300. flag=wx.ALIGN_LEFT |
  301. wx.ALIGN_CENTER_VERTICAL |
  302. wx.ALL, border=5, pos=(1, 1))
  303. self.sizer.Add(item=self.tproj,
  304. flag=wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL,
  305. border=5, pos=(1, 2))
  306. self.sizer.Add(item=self.MakeLabel(_("Search in description:")),
  307. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  308. border=5, pos=(2, 1))
  309. self.sizer.Add(item=self.searchb,
  310. flag=wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL,
  311. border=5, pos=(2, 2))
  312. self.sizer.AddGrowableRow(3)
  313. self.sizer.Add(item=self.projlist,
  314. flag=wx.EXPAND |
  315. wx.ALIGN_LEFT |
  316. wx.ALL, border=5, pos=(3, 1), span=(1, 3))
  317. # events
  318. self.tproj.Bind(wx.EVT_TEXT, self.OnText)
  319. self.tproj.Bind(wx.EVT_TEXT_ENTER, self.OnText)
  320. self.searchb.Bind(wx.EVT_TEXT_ENTER, self.OnSearch)
  321. self.projlist.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
  322. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  323. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  324. def OnPageChanging(self,event):
  325. if event.GetDirection() and self.proj not in self.parent.projections.keys():
  326. event.Veto()
  327. def OnText(self, event):
  328. """!Projection name changed"""
  329. self.proj = event.GetString().lower()
  330. self.p4proj = ''
  331. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  332. if self.proj not in self.parent.projections.keys() and nextButton.IsEnabled():
  333. nextButton.Enable(False)
  334. if self.proj in self.parent.projections.keys():
  335. if self.proj == 'stp':
  336. wx.MessageBox('Currently State Plane projections must be selected using the '
  337. 'text-based setup (g.setproj), or entered by EPSG code or '
  338. 'custom PROJ.4 terms.',
  339. 'Warning', wx.ICON_WARNING)
  340. self.proj = ''
  341. self.tproj.SetValue(self.proj)
  342. nextButton.Enable(False)
  343. return
  344. elif self.proj.lower() == 'll':
  345. self.p4proj = '+proj=longlat'
  346. else:
  347. self.p4proj = '+proj=' + self.proj.lower()
  348. self.projdesc = self.parent.projections[self.proj][0]
  349. nextButton.Enable()
  350. def OnEnterPage(self, event):
  351. if len(self.proj) == 0:
  352. # disable 'next' button by default
  353. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  354. else:
  355. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  356. event.Skip()
  357. def OnSearch(self, event):
  358. """!Search projection by desc"""
  359. str = event.GetString()
  360. try:
  361. self.proj, self.projdesc = self.projlist.Search(index=[0,1], pattern=event.GetString())
  362. except:
  363. self.proj = self.projdesc = ''
  364. event.Skip()
  365. def OnItemSelected(self, event):
  366. """!Projection selected"""
  367. index = event.m_itemIndex
  368. # set values
  369. self.proj = self.projlist.GetItem(index, 0).GetText().lower()
  370. self.tproj.SetValue(self.proj)
  371. event.Skip()
  372. class ItemList(wx.ListCtrl,
  373. listmix.ListCtrlAutoWidthMixin,
  374. listmix.ColumnSorterMixin):
  375. """!Generic list (for projections, ellipsoids, etc.)"""
  376. def __init__(self, parent, columns, data=None):
  377. wx.ListCtrl.__init__(self, parent=parent, id=wx.ID_ANY,
  378. style=wx.LC_REPORT |
  379. wx.LC_VIRTUAL |
  380. wx.LC_HRULES |
  381. wx.LC_VRULES |
  382. wx.LC_SINGLE_SEL |
  383. wx.LC_SORT_ASCENDING, size=(550, 125))
  384. # original data or None
  385. self.sourceData = data
  386. #
  387. # insert columns
  388. #
  389. i = 0
  390. for column in columns:
  391. self.InsertColumn(i, column)
  392. i += 1
  393. if self.sourceData:
  394. self.Populate()
  395. for i in range(self.GetColumnCount()):
  396. self.SetColumnWidth(i, wx.LIST_AUTOSIZE_USEHEADER)
  397. if self.GetColumnWidth(i) < 80:
  398. self.SetColumnWidth(i, 80)
  399. #
  400. # listmix
  401. #
  402. listmix.ListCtrlAutoWidthMixin.__init__(self)
  403. listmix.ColumnSorterMixin.__init__(self, self.GetColumnCount())
  404. #
  405. # add some attributes
  406. #
  407. self.attr1 = wx.ListItemAttr()
  408. self.attr1.SetBackgroundColour(wx.Colour(238,238,238))
  409. self.attr2 = wx.ListItemAttr()
  410. self.attr2.SetBackgroundColour("white")
  411. self.il = wx.ImageList(16, 16)
  412. self.sm_up = self.il.Add(wx.ArtProvider_GetBitmap(wx.ART_GO_UP, wx.ART_TOOLBAR,
  413. (16,16)))
  414. self.sm_dn = self.il.Add(wx.ArtProvider_GetBitmap(wx.ART_GO_DOWN, wx.ART_TOOLBAR,
  415. (16,16)))
  416. self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
  417. #
  418. # sort by first column
  419. #
  420. if self.sourceData:
  421. self.SortListItems(col=0, ascending=True)
  422. #
  423. # bindings
  424. #
  425. self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColumnClick)
  426. def Populate(self, data=None, update=False):
  427. """!Populate list"""
  428. self.itemDataMap = {}
  429. self.itemIndexMap = []
  430. if data is None:
  431. data = self.sourceData
  432. elif update:
  433. self.sourceData = data
  434. try:
  435. data.sort()
  436. self.DeleteAllItems()
  437. row = 0
  438. for value in data:
  439. self.itemDataMap[row] = [value[0]]
  440. for i in range(1, len(value)):
  441. self.itemDataMap[row].append(value[i])
  442. self.itemIndexMap.append(row)
  443. row += 1
  444. self.SetItemCount(row)
  445. # set column width
  446. self.SetColumnWidth(0, 80)
  447. self.SetColumnWidth(1, 300)
  448. self.SendSizeEvent()
  449. except StandardError, e:
  450. wx.MessageBox(parent=self,
  451. message=_("Unable to read list: %s") % e,
  452. caption=_("Error"), style=wx.OK | wx.ICON_ERROR)
  453. def OnColumnClick(self, event):
  454. """!Sort by column"""
  455. self._col = event.GetColumn()
  456. # remove duplicated arrow symbol from column header
  457. # FIXME: should be done automatically
  458. info = wx.ListItem()
  459. info.m_mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE
  460. info.m_image = -1
  461. for column in range(self.GetColumnCount()):
  462. info.m_text = self.GetColumn(column).GetText()
  463. self.SetColumn(column, info)
  464. event.Skip()
  465. def GetSortImages(self):
  466. """!Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py"""
  467. return (self.sm_dn, self.sm_up)
  468. def OnGetItemText(self, item, col):
  469. """!Get item text"""
  470. index = self.itemIndexMap[item]
  471. s = str(self.itemDataMap[index][col])
  472. return s
  473. def OnGetItemAttr(self, item):
  474. """!Get item attributes"""
  475. index = self.itemIndexMap[item]
  476. if ( index % 2) == 0:
  477. return self.attr2
  478. else:
  479. return self.attr1
  480. def SortItems(self, sorter=cmp):
  481. """!Sort items"""
  482. items = list(self.itemDataMap.keys())
  483. items.sort(self.Sorter)
  484. self.itemIndexMap = items
  485. # redraw the list
  486. self.Refresh()
  487. def Sorter(self, key1, key2):
  488. colName = self.GetColumn(self._col).GetText()
  489. ascending = self._colSortFlag[self._col]
  490. # convert always string
  491. item1 = self.itemDataMap[key1][self._col]
  492. item2 = self.itemDataMap[key2][self._col]
  493. if type(item1) == type('') or type(item2) == type(''):
  494. cmpVal = locale.strcoll(str(item1), str(item2))
  495. else:
  496. cmpVal = cmp(item1, item2)
  497. # If the items are equal then pick something else to make the sort value unique
  498. if cmpVal == 0:
  499. cmpVal = apply(cmp, self.GetSecondarySortValues(self._col, key1, key2))
  500. if ascending:
  501. return cmpVal
  502. else:
  503. return -cmpVal
  504. def GetListCtrl(self):
  505. """!Used by listmix.ColumnSorterMixin"""
  506. return self
  507. def Search (self, index, pattern):
  508. """!Search projection by description
  509. Return first found item or None
  510. """
  511. if pattern == '':
  512. self.Populate(self.sourceData)
  513. return []
  514. data = []
  515. pattern = pattern.lower()
  516. for i in range(len(self.sourceData)):
  517. for idx in index:
  518. try:
  519. value = str(self.sourceData[i][idx]).lower()
  520. if pattern in value:
  521. data.append(self.sourceData[i])
  522. break
  523. except UnicodeDecodeError:
  524. # osgeo4w problem (should be fixed)
  525. pass
  526. self.Populate(data)
  527. if len(data) > 0:
  528. return data[0]
  529. else:
  530. return []
  531. class ProjParamsPage(TitledPage):
  532. """!Wizard page for selecting method of setting coordinate system
  533. parameters (select coordinate system option)
  534. """
  535. def __init__(self, wizard, parent):
  536. TitledPage.__init__(self, wizard, _("Choose projection parameters"))
  537. global coordsys
  538. self.parent = parent
  539. self.panel = None
  540. self.prjParamSizer = None
  541. self.pparam = dict()
  542. self.p4projparams = ''
  543. self.projdesc = ''
  544. self.sizer.AddGrowableCol(1)
  545. self.sizer.AddGrowableRow(1)
  546. radioSBox = wx.StaticBox(parent = self, id = wx.ID_ANY,
  547. label = " %s " % _("Select datum or ellipsoid (next page)"))
  548. radioSBSizer = wx.StaticBoxSizer(radioSBox)
  549. self.sizer.Add(item = radioSBSizer, pos = (0, 1),
  550. flag = wx.EXPAND | wx.ALIGN_TOP | wx.TOP, border = 10)
  551. self.radio1 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  552. label=_("Datum with associated ellipsoid"),
  553. style = wx.RB_GROUP)
  554. self.radio2 = wx.RadioButton(parent=self, id=wx.ID_ANY,
  555. label=_("Ellipsoid only"))
  556. # default button setting
  557. if self.radio1.GetValue() == False and self.radio2.GetValue() == False:
  558. self.radio1.SetValue(True)
  559. self.SetNext(self.parent.datumpage)
  560. # self.parent.sumpage.SetPrev(self.parent.datumpage)
  561. radioSBSizer.Add(item=self.radio1,
  562. flag=wx.ALIGN_LEFT | wx.RIGHT, border=20)
  563. radioSBSizer.Add(item=self.radio2,
  564. flag=wx.ALIGN_LEFT)
  565. # bindings
  566. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id = self.radio1.GetId())
  567. self.Bind(wx.EVT_RADIOBUTTON, self.SetVal, id = self.radio2.GetId())
  568. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChange)
  569. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  570. def OnParamEntry(self, event):
  571. """!Parameter value changed"""
  572. id = event.GetId()
  573. val = event.GetString()
  574. if not self.pparam.has_key(id):
  575. event.Skip()
  576. return
  577. param = self.pparam[id]
  578. win = self.FindWindowById(id)
  579. if param['type'] == 'zone':
  580. val = event.GetInt()
  581. if val < 1:
  582. win.SetValue(1)
  583. elif val > 60:
  584. win.SetValue(60)
  585. if param['type'] == 'bool':
  586. param['value'] = event.GetSelection()
  587. else:
  588. param['value'] = val
  589. event.Skip()
  590. def OnPageChange(self,event=None):
  591. """!Go to next page"""
  592. if event.GetDirection():
  593. self.p4projparams = ''
  594. for id, param in self.pparam.iteritems():
  595. if param['type'] == 'bool':
  596. if param['value'] == False:
  597. continue
  598. else:
  599. self.p4projparams += (' +' + param['proj4'])
  600. else:
  601. if param['value'] is None:
  602. wx.MessageBox(parent = self,
  603. message = _('You must enter a value for %s') % param['desc'],
  604. caption = _('Error'), style = wx.ICON_ERROR | wx.CENTRE)
  605. event.Veto()
  606. else:
  607. self.p4projparams += (' +' + param['proj4'] + '=' + str(param['value']))
  608. def OnEnterPage(self,event):
  609. """!Page entered"""
  610. self.projdesc = self.parent.projections[self.parent.projpage.proj][0]
  611. if self.prjParamSizer is None:
  612. # entering page for the first time
  613. self.paramSBox = wx.StaticBox(parent = self, id = wx.ID_ANY,
  614. label=_(" Enter parameters for %s projection ") % self.projdesc)
  615. paramSBSizer = wx.StaticBoxSizer(self.paramSBox)
  616. self.panel = scrolled.ScrolledPanel(parent = self, id = wx.ID_ANY)
  617. self.panel.SetupScrolling()
  618. self.prjParamSizer = wx.GridBagSizer(vgap=0, hgap=0)
  619. self.sizer.Add(item = paramSBSizer, pos=(1, 1),
  620. flag = wx.EXPAND)
  621. paramSBSizer.Add(item = self.panel, proportion = 1,
  622. flag = wx.ALIGN_CENTER | wx.EXPAND)
  623. paramSBSizer.Fit(self.panel)
  624. self.panel.SetSizer(self.prjParamSizer)
  625. if event.GetDirection():
  626. self.prjParamSizer.Clear(True)
  627. self.paramSBox.SetLabel(_(" Enter parameters for %s projection ") % self.projdesc)
  628. self.pparam = dict()
  629. row = 0
  630. for paramgrp in self.parent.projections[self.parent.projpage.proj][1]:
  631. # get parameters
  632. id = wx.NewId()
  633. param = self.pparam[id] = { 'type' : self.parent.paramdesc[paramgrp[0]][0],
  634. 'proj4': self.parent.paramdesc[paramgrp[0]][1],
  635. 'desc' : self.parent.paramdesc[paramgrp[0]][2] }
  636. # default values
  637. if param['type'] == 'bool':
  638. param['value'] = 0
  639. elif param['type'] == 'zone':
  640. param['value'] = 30
  641. param['desc'] += ' (1-60)'
  642. else:
  643. param['value'] = paramgrp[2]
  644. label = wx.StaticText(parent = self.panel, id = wx.ID_ANY, label = param['desc'],
  645. style = wx.ALIGN_RIGHT | wx.ST_NO_AUTORESIZE)
  646. if param['type'] == 'bool':
  647. win = wx.Choice(parent = self.panel, id = id, size = (100,-1),
  648. choices = [_('No'), _('Yes')])
  649. win.SetSelection(param['value'])
  650. win.Bind(wx.EVT_CHOICE, self.OnParamEntry)
  651. elif param['type'] == 'zone':
  652. win = wx.SpinCtrl(parent = self.panel, id = id,
  653. size = (100, -1),
  654. style = wx.SP_ARROW_KEYS | wx.SP_WRAP,
  655. min = 1, max = 60)
  656. win.SetValue(param['value'])
  657. win.Bind(wx.EVT_SPINCTRL, self.OnParamEntry)
  658. win.Bind(wx.EVT_TEXT, self.OnParamEntry)
  659. else:
  660. win = wx.TextCtrl(parent = self.panel, id = id,
  661. value = param['value'],
  662. size=(100, -1))
  663. win.Bind(wx.EVT_TEXT, self.OnParamEntry)
  664. if paramgrp[1] == 'noask':
  665. win.Enable(False)
  666. self.prjParamSizer.Add(item = label, pos = (row, 1),
  667. flag = wx.ALIGN_RIGHT |
  668. wx.ALIGN_CENTER_VERTICAL |
  669. wx.RIGHT, border = 5)
  670. self.prjParamSizer.Add(item = win, pos = (row, 2),
  671. flag = wx.ALIGN_LEFT |
  672. wx.ALIGN_CENTER_VERTICAL |
  673. wx.LEFT, border = 5)
  674. row += 1
  675. self.panel.SetSize(self.panel.GetBestSize())
  676. self.panel.Layout()
  677. self.Layout()
  678. self.Update()
  679. if not wx.FindWindowById(wx.ID_FORWARD).IsEnabled():
  680. wx.FindWindowById(wx.ID_FORWARD).Enable()
  681. event.Skip()
  682. def SetVal(self, event):
  683. """!Set value"""
  684. if event.GetId() == self.radio1.GetId():
  685. self.SetNext(self.parent.datumpage)
  686. self.parent.sumpage.SetPrev(self.parent.datumpage)
  687. elif event.GetId() == self.radio2.GetId():
  688. self.SetNext(self.parent.ellipsepage)
  689. self.parent.sumpage.SetPrev(self.parent.ellipsepage)
  690. class DatumPage(TitledPage):
  691. """!Wizard page for selecting datum (with associated ellipsoid)
  692. and datum transformation parameters (select coordinate system option)
  693. """
  694. def __init__(self, wizard, parent):
  695. TitledPage.__init__(self, wizard, _("Specify geodetic datum"))
  696. self.parent = parent
  697. self.datum = ''
  698. self.datumdesc = ''
  699. self.ellipse = ''
  700. self.datumparams = ''
  701. self.proj4params = ''
  702. # text input
  703. self.tdatum = self.MakeTextCtrl("", size=(200,-1))
  704. # search box
  705. self.searchb = wx.SearchCtrl(self, size=(200,-1),
  706. style=wx.TE_PROCESS_ENTER)
  707. # create list control for datum/elipsoid list
  708. data = []
  709. for key in self.parent.datums.keys():
  710. data.append([key, self.parent.datums[key][0], self.parent.datums[key][1]])
  711. self.datumlist = ItemList(self,
  712. data=data,
  713. columns=[_('Code'), _('Ellipsoid'), _('Description')])
  714. self.datumlist.resizeLastColumn(10)
  715. # layout
  716. self.sizer.AddGrowableCol(4)
  717. self.sizer.Add(item=self.MakeLabel(_("Datum code:")),
  718. flag=wx.ALIGN_LEFT |
  719. wx.ALIGN_CENTER_VERTICAL |
  720. wx.ALL, border=5, pos=(1, 1))
  721. self.sizer.Add(item=self.tdatum,
  722. flag=wx.ALIGN_LEFT |
  723. wx.ALIGN_CENTER_VERTICAL |
  724. wx.ALL, border=5, pos=(1, 2))
  725. self.sizer.Add(item=self.MakeLabel(_("Search in description:")),
  726. flag=wx.ALIGN_LEFT |
  727. wx.ALIGN_CENTER_VERTICAL |
  728. wx.ALL, border=5, pos=(2, 1))
  729. self.sizer.Add(item=self.searchb,
  730. flag=wx.ALIGN_LEFT |
  731. wx.ALIGN_CENTER_VERTICAL |
  732. wx.ALL, border=5, pos=(2, 2))
  733. self.sizer.AddGrowableRow(3)
  734. self.sizer.Add(item=self.datumlist,
  735. flag=wx.EXPAND |
  736. wx.ALIGN_LEFT |
  737. wx.ALL, border=5, pos=(3, 1), span=(1, 4))
  738. # events
  739. self.datumlist.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnDatumSelected)
  740. self.searchb.Bind(wx.EVT_TEXT_ENTER, self.OnDSearch)
  741. self.tdatum.Bind(wx.EVT_TEXT, self.OnDText)
  742. self.tdatum.Bind(wx.EVT_TEXT_ENTER, self.OnDText)
  743. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  744. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  745. # do page layout
  746. # self.DoLayout()
  747. def OnPageChanging(self, event):
  748. self.proj4params = ''
  749. proj = self.parent.projpage.p4proj
  750. if event.GetDirection():
  751. if self.datum not in self.parent.datums:
  752. event.Veto()
  753. else:
  754. # check for datum tranforms
  755. # proj4string = self.parent.CreateProj4String() + ' +datum=%s' % self.datum
  756. ret = gcmd.RunCommand('g.proj',
  757. read = True,
  758. proj4 = '%s +datum=%s' % (proj, self.datum),
  759. datumtrans = '-1')
  760. if ret != '':
  761. dtrans = ''
  762. # open a dialog to select datum transform number
  763. dlg = SelectTransformDialog(self.parent.parent, transforms=ret)
  764. if dlg.ShowModal() == wx.ID_OK:
  765. dtrans = dlg.GetTransform()
  766. if dtrans == '':
  767. dlg.Destroy()
  768. event.Veto()
  769. return 'Datum transform is required.'
  770. else:
  771. dlg.Destroy()
  772. event.Veto()
  773. return 'Datum transform is required.'
  774. self.parent.datumtrans = dtrans
  775. self.GetNext().SetPrev(self)
  776. self.parent.ellipsepage.ellipse = self.ellipse
  777. self.parent.ellipsepage.ellipseparams = self.parent.ellipsoids[self.ellipse][1]
  778. def OnEnterPage(self,event):
  779. self.parent.datumtrans = None
  780. if event.GetDirection():
  781. if len(self.datum) == 0:
  782. # disable 'next' button by default when entering from previous page
  783. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  784. else:
  785. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  786. event.Skip()
  787. def OnDText(self, event):
  788. """!Datum code changed"""
  789. self.datum = event.GetString()
  790. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  791. if len(self.datum) == 0 or self.datum not in self.parent.datums:
  792. nextButton.Enable(False)
  793. else:
  794. self.ellipse = self.parent.datums[self.datum][0]
  795. self.datumdesc = self.parent.datums[self.datum][1]
  796. self.datumparams = self.parent.datums[self.datum][2]
  797. try:
  798. self.datumparams.remove('dx=0.0')
  799. except:
  800. pass
  801. try:
  802. self.datumparams.remove('dy=0.0')
  803. except:
  804. pass
  805. try:
  806. self.datumparams.remove('dz=0.0')
  807. except:
  808. pass
  809. nextButton.Enable(True)
  810. self.Update()
  811. event.Skip()
  812. def OnDSearch(self, event):
  813. """!Search geodetic datum by desc"""
  814. str = self.searchb.GetValue()
  815. try:
  816. self.datum, self.ellipsoid, self.datumdesc = self.datumlist.Search(index=[0,1,2], pattern=str)
  817. except:
  818. self.datum = self.datumdesc = self.ellipsoid = ''
  819. event.Skip()
  820. def OnDatumSelected(self, event):
  821. """!Datum selected"""
  822. index = event.m_itemIndex
  823. item = event.GetItem()
  824. self.datum = self.datumlist.GetItem(index, 0).GetText()
  825. self.tdatum.SetValue(self.datum)
  826. event.Skip()
  827. class EllipsePage(TitledPage):
  828. """!Wizard page for selecting ellipsoid (select coordinate system option)"""
  829. def __init__(self, wizard, parent):
  830. TitledPage.__init__(self, wizard, _("Specify ellipsoid"))
  831. self.parent = parent
  832. self.ellipse = ''
  833. self.ellipsedesc = ''
  834. self.ellipseparams = ''
  835. self.proj4params = ''
  836. # text input
  837. self.tellipse = self.MakeTextCtrl("", size=(200,-1))
  838. # search box
  839. self.searchb = wx.SearchCtrl(self, size=(200,-1),
  840. style=wx.TE_PROCESS_ENTER)
  841. # create list control for ellipse list
  842. data = []
  843. # extract code, desc
  844. for key in self.parent.ellipsoids.keys():
  845. data.append([key, self.parent.ellipsoids[key][0]])
  846. self.ellipselist = ItemList(self, data=data,
  847. columns=[_('Code'), _('Description')])
  848. self.ellipselist.resizeLastColumn(30)
  849. # layout
  850. self.sizer.AddGrowableCol(4)
  851. self.sizer.Add(item=self.MakeLabel(_("Ellipsoid code:")),
  852. flag=wx.ALIGN_RIGHT |
  853. wx.ALIGN_CENTER_VERTICAL |
  854. wx.ALL, border=5, pos=(1, 1))
  855. self.sizer.Add(item=self.tellipse,
  856. flag=wx.ALIGN_LEFT |
  857. wx.ALIGN_CENTER_VERTICAL |
  858. wx.ALL, border=5, pos=(1, 2))
  859. self.sizer.Add(item=self.MakeLabel(_("Search in description:")),
  860. flag=wx.ALIGN_RIGHT |
  861. wx.ALIGN_CENTER_VERTICAL |
  862. wx.ALL, border=5, pos=(2, 1))
  863. self.sizer.Add(item=self.searchb,
  864. flag=wx.ALIGN_LEFT |
  865. wx.ALIGN_CENTER_VERTICAL |
  866. wx.ALL, border=5, pos=(2, 2))
  867. self.sizer.AddGrowableRow(3)
  868. self.sizer.Add(item=self.ellipselist,
  869. flag=wx.EXPAND |
  870. wx.ALIGN_LEFT |
  871. wx.ALL, border=5, pos=(3, 1), span=(1, 4))
  872. # events
  873. self.ellipselist.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
  874. self.tellipse.Bind(wx.EVT_TEXT, self.OnText)
  875. self.tellipse.Bind(wx.EVT_TEXT_ENTER, self.OnText)
  876. self.searchb.Bind(wx.EVT_TEXT_ENTER, self.OnSearch)
  877. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  878. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  879. def OnEnterPage(self,event):
  880. if len(self.ellipse) == 0:
  881. # disable 'next' button by default
  882. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  883. else:
  884. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  885. event.Skip()
  886. def OnPageChanging(self, event):
  887. if event.GetDirection() and self.ellipse not in self.parent.ellipsoids:
  888. event.Veto()
  889. self.proj4params = ''
  890. self.GetNext().SetPrev(self)
  891. self.parent.datumpage.datumparams = ''
  892. # self.GetNext().SetPrev(self) (???)
  893. def OnText(self, event):
  894. """!Ellipspoid code changed"""
  895. self.ellipse = event.GetString()
  896. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  897. if len(self.ellipse) == 0 or self.ellipse not in self.parent.ellipsoids:
  898. nextButton.Enable(False)
  899. self.ellipsedesc = ''
  900. self.ellipseparams = ''
  901. self.proj4params = ''
  902. elif self.ellipse in self.parent.ellipsoids:
  903. self.ellipsedesc = self.parent.ellipsoids[self.ellipse][0]
  904. self.ellipseparams = self.parent.ellipsoids[self.ellipse][1]
  905. nextButton.Enable(True)
  906. def OnSearch(self, event):
  907. """!Search ellipsoid by desc"""
  908. try:
  909. self.ellipse, self.ellipsedesc = \
  910. self.ellipselist.Search(index=[0,1], pattern=event.GetString())
  911. self.ellipseparams = self.parent.ellipsoids[self.ellipse][1]
  912. except:
  913. self.ellipse = self.ellipsedesc = self.ellipseparams = ''
  914. event.Skip()
  915. def OnItemSelected(self,event):
  916. """!Ellipsoid selected"""
  917. index = event.m_itemIndex
  918. item = event.GetItem()
  919. self.ellipse = self.ellipselist.GetItem(index, 0).GetText()
  920. self.tellipse.SetValue(self.ellipse)
  921. event.Skip()
  922. class GeoreferencedFilePage(TitledPage):
  923. """!Wizard page for selecting georeferenced file to use
  924. for setting coordinate system parameters"""
  925. def __init__(self, wizard, parent):
  926. TitledPage.__init__(self, wizard, _("Select georeferenced file"))
  927. self.georeffile = ''
  928. # create controls
  929. self.lfile= self.MakeLabel(_("Georeferenced file:"))
  930. self.tfile = self.MakeTextCtrl(size=(300,-1))
  931. self.bbrowse = self.MakeButton(_("Browse"))
  932. # do layout
  933. self.sizer.AddGrowableCol(3)
  934. self.sizer.Add(item=self.lfile, flag=wx.ALIGN_LEFT |
  935. wx.ALIGN_CENTRE_VERTICAL |
  936. wx.ALL, border=5, pos=(1, 1))
  937. self.sizer.Add(item=self.tfile, flag=wx.ALIGN_LEFT |
  938. wx.ALIGN_CENTRE_VERTICAL |
  939. wx.ALL, border=5, pos=(1, 2))
  940. self.sizer.Add(item=self.bbrowse, flag=wx.ALIGN_LEFT |
  941. wx.ALL, border=5, pos=(1, 3))
  942. self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
  943. self.tfile.Bind(wx.EVT_TEXT, self.OnText)
  944. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  945. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  946. # do page layout
  947. # self.DoLayout()
  948. def OnEnterPage(self, event):
  949. if len(self.georeffile) == 0:
  950. # disable 'next' button by default
  951. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  952. else:
  953. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  954. event.Skip()
  955. def OnPageChanging(self, event):
  956. if event.GetDirection() and not os.path.isfile(self.georeffile):
  957. event.Veto()
  958. self.GetNext().SetPrev(self)
  959. event.Skip()
  960. def OnText(self, event):
  961. """!File changed"""
  962. self.georeffile = event.GetString()
  963. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  964. if len(self.georeffile) > 0 and os.path.isfile(self.georeffile):
  965. if not nextButton.IsEnabled():
  966. nextButton.Enable(True)
  967. else:
  968. if nextButton.IsEnabled():
  969. nextButton.Enable(False)
  970. event.Skip()
  971. def OnBrowse(self, event):
  972. """!Choose file"""
  973. dlg = wx.FileDialog(self,
  974. _("Select georeferenced file"),
  975. os.getcwd(), "", "*.*", wx.OPEN)
  976. if dlg.ShowModal() == wx.ID_OK:
  977. path = dlg.GetPath()
  978. self.tfile.SetValue(path)
  979. dlg.Destroy()
  980. event.Skip()
  981. class WKTPage(TitledPage):
  982. """!Wizard page for selecting WKT file to use
  983. for setting coordinate system parameters"""
  984. def __init__(self, wizard, parent):
  985. TitledPage.__init__(self, wizard, _("Select WKT file"))
  986. self.wktfile = ''
  987. # create controls
  988. self.lfile= self.MakeLabel(_("WKT file:"))
  989. self.tfile = self.MakeTextCtrl(size=(300,-1))
  990. self.bbrowse = self.MakeButton(_("Browse"))
  991. # do layout
  992. self.sizer.AddGrowableCol(3)
  993. self.sizer.Add(item=self.lfile, flag=wx.ALIGN_LEFT |
  994. wx.ALIGN_CENTRE_VERTICAL |
  995. wx.ALL, border=5, pos=(1, 1))
  996. self.sizer.Add(item=self.tfile, flag=wx.ALIGN_LEFT |
  997. wx.ALIGN_CENTRE_VERTICAL |
  998. wx.ALL, border=5, pos=(1, 2))
  999. self.sizer.Add(item=self.bbrowse, flag=wx.ALIGN_LEFT |
  1000. wx.ALL, border=5, pos=(1, 3))
  1001. self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
  1002. self.tfile.Bind(wx.EVT_TEXT, self.OnText)
  1003. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  1004. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1005. def OnEnterPage(self, event):
  1006. if len(self.wktfile) == 0:
  1007. # disable 'next' button by default
  1008. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1009. else:
  1010. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1011. event.Skip()
  1012. def OnPageChanging(self, event):
  1013. if event.GetDirection() and not os.path.isfile(self.wktfile):
  1014. event.Veto()
  1015. self.GetNext().SetPrev(self)
  1016. event.Skip()
  1017. def OnText(self, event):
  1018. """!File changed"""
  1019. self.wktfile = event.GetString()
  1020. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  1021. if len(self.wktfile) > 0 and os.path.isfile(self.wktfile):
  1022. if not nextButton.IsEnabled():
  1023. nextButton.Enable(True)
  1024. else:
  1025. if nextButton.IsEnabled():
  1026. nextButton.Enable(False)
  1027. event.Skip()
  1028. def OnBrowse(self, event):
  1029. """!Choose file"""
  1030. dlg = wx.FileDialog(self,
  1031. _("Select WKT file"),
  1032. os.getcwd(), "", "*.*", wx.OPEN)
  1033. if dlg.ShowModal() == wx.ID_OK:
  1034. path = dlg.GetPath()
  1035. self.tfile.SetValue(path)
  1036. dlg.Destroy()
  1037. event.Skip()
  1038. class EPSGPage(TitledPage):
  1039. """!Wizard page for selecting EPSG code for
  1040. setting coordinate system parameters"""
  1041. def __init__(self, wizard, parent):
  1042. TitledPage.__init__(self, wizard, _("Choose EPSG Code"))
  1043. self.parent = parent
  1044. self.epsgCodeDict = {}
  1045. self.epsgcode = None
  1046. self.epsgdesc = ''
  1047. self.epsgparams = ''
  1048. # labels
  1049. self.lfile= self.MakeLabel(_("Path to the EPSG-codes file:"),
  1050. style=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
  1051. self.lcode= self.MakeLabel(_("EPSG code:"),
  1052. style=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
  1053. # text input
  1054. epsgdir = utils.PathJoin(os.environ["GRASS_PROJSHARE"], 'epsg')
  1055. self.tfile = self.MakeTextCtrl(text=epsgdir, size=(200,-1),
  1056. style = wx.TE_PROCESS_ENTER)
  1057. self.tcode = self.MakeTextCtrl(size=(200,-1))
  1058. # buttons
  1059. self.bbrowse = self.MakeButton(_("Browse"))
  1060. # search box
  1061. self.searchb = wx.SearchCtrl(self, size=(200,-1),
  1062. style=wx.TE_PROCESS_ENTER)
  1063. self.epsglist = ItemList(self, data=None,
  1064. columns=[_('Code'), _('Description'), _('Parameters')])
  1065. # layout
  1066. self.sizer.AddGrowableCol(3)
  1067. self.sizer.Add(item=self.lfile,
  1068. flag=wx.ALIGN_LEFT |
  1069. wx.ALIGN_CENTER_VERTICAL |
  1070. wx.ALL, border=5, pos=(1, 1), span=(1, 2))
  1071. self.sizer.Add(item=self.tfile,
  1072. flag=wx.ALIGN_LEFT |
  1073. wx.ALIGN_CENTER_VERTICAL |
  1074. wx.ALL, border=5, pos=(1, 3))
  1075. self.sizer.Add(item=self.bbrowse,
  1076. flag=wx.ALIGN_LEFT |
  1077. wx.ALIGN_CENTER_VERTICAL |
  1078. wx.ALL, border=5, pos=(1, 4))
  1079. self.sizer.Add(item=self.lcode,
  1080. flag=wx.ALIGN_LEFT |
  1081. wx.ALIGN_CENTER_VERTICAL |
  1082. wx.ALL, border=5, pos=(2, 1), span=(1, 2))
  1083. self.sizer.Add(item=self.tcode,
  1084. flag=wx.ALIGN_LEFT |
  1085. wx.ALIGN_CENTER_VERTICAL |
  1086. wx.ALL, border=5, pos=(2, 3))
  1087. self.sizer.Add(item=self.searchb,
  1088. flag=wx.ALIGN_LEFT |
  1089. wx.ALIGN_CENTER_VERTICAL |
  1090. wx.ALL, border=5, pos=(3, 3))
  1091. self.sizer.AddGrowableRow(4)
  1092. self.sizer.Add(item=self.epsglist,
  1093. flag=wx.ALIGN_LEFT | wx.EXPAND, pos=(4, 1),
  1094. span=(1, 4))
  1095. # events
  1096. self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
  1097. self.tfile.Bind(wx.EVT_TEXT_ENTER, self.OnBrowseCodes)
  1098. self.tcode.Bind(wx.EVT_TEXT, self.OnText)
  1099. self.tcode.Bind(wx.EVT_TEXT_ENTER, self.OnText)
  1100. self.epsglist.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
  1101. self.searchb.Bind(wx.EVT_TEXT_ENTER, self.OnSearch)
  1102. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  1103. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1104. def OnEnterPage(self, event):
  1105. self.parent.datumtrans = None
  1106. if event.GetDirection():
  1107. if not self.epsgcode:
  1108. # disable 'next' button by default
  1109. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1110. else:
  1111. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1112. # load default epsg database file
  1113. self.OnBrowseCodes(None)
  1114. event.Skip()
  1115. def OnPageChanging(self, event):
  1116. if event.GetDirection():
  1117. if not self.epsgcode:
  1118. event.Veto()
  1119. return
  1120. else:
  1121. # check for datum transforms
  1122. ret = gcmd.RunCommand('g.proj',
  1123. read = True,
  1124. epsg = self.epsgcode,
  1125. datumtrans = '-1')
  1126. if ret != '':
  1127. dtrans = ''
  1128. # open a dialog to select datum transform number
  1129. dlg = SelectTransformDialog(self.parent.parent, transforms=ret)
  1130. if dlg.ShowModal() == wx.ID_OK:
  1131. dtrans = dlg.GetTransform()
  1132. if dtrans == '':
  1133. dlg.Destroy()
  1134. event.Veto()
  1135. return 'Datum transform is required.'
  1136. else:
  1137. dlg.Destroy()
  1138. event.Veto()
  1139. return 'Datum transform is required.'
  1140. self.parent.datumtrans = dtrans
  1141. self.GetNext().SetPrev(self)
  1142. def OnText(self, event):
  1143. self.epsgcode = event.GetString()
  1144. try:
  1145. self.epsgcode = int(self.epsgcode)
  1146. except:
  1147. self.epsgcode = None
  1148. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  1149. if self.epsgcode and self.epsgcode in self.epsgCodeDict.keys():
  1150. self.epsgdesc = self.epsgCodeDict[self.epsgcode][0]
  1151. self.epsgparams = self.epsgCodeDict[self.epsgcode][1]
  1152. if not nextButton.IsEnabled():
  1153. nextButton.Enable(True)
  1154. else:
  1155. self.epsgcode = None # not found
  1156. if nextButton.IsEnabled():
  1157. nextButton.Enable(False)
  1158. self.epsgdesc = self.epsgparams = ''
  1159. def OnSearch(self, event):
  1160. value = self.searchb.GetValue()
  1161. if value == '':
  1162. self.epsgcode = None
  1163. self.epsgdesc = self.epsgparams = ''
  1164. self.tcode.SetValue('')
  1165. self.searchb.SetValue('')
  1166. self.OnBrowseCodes(None)
  1167. else:
  1168. try:
  1169. self.epsgcode, self.epsgdesc, self.epsgparams = \
  1170. self.epsglist.Search(index=[0,1,2], pattern=value)
  1171. except (IndexError, ValueError): # -> no item found
  1172. self.epsgcode = None
  1173. self.epsgdesc = self.epsgparams = ''
  1174. self.tcode.SetValue('')
  1175. self.searchb.SetValue('')
  1176. event.Skip()
  1177. def OnBrowse(self, event):
  1178. """!Define path for EPSG code file"""
  1179. path = os.path.dirname(self.tfile.GetValue())
  1180. if not path:
  1181. path = os.getcwd()
  1182. dlg = wx.FileDialog(parent=self, message=_("Choose EPSG codes file"),
  1183. defaultDir=path, defaultFile="", wildcard="*", style=wx.OPEN)
  1184. if dlg.ShowModal() == wx.ID_OK:
  1185. path = dlg.GetPath()
  1186. self.tfile.SetValue(path)
  1187. self.OnBrowseCodes(None)
  1188. dlg.Destroy()
  1189. event.Skip()
  1190. def OnItemSelected(self, event):
  1191. """!EPSG code selected from the list"""
  1192. index = event.m_itemIndex
  1193. item = event.GetItem()
  1194. self.epsgcode = int(self.epsglist.GetItem(index, 0).GetText())
  1195. self.epsgdesc = self.epsglist.GetItem(index, 1).GetText()
  1196. self.tcode.SetValue(str(self.epsgcode))
  1197. event.Skip()
  1198. def OnBrowseCodes(self, event, search=None):
  1199. """!Browse EPSG codes"""
  1200. self.epsgCodeDict = utils.ReadEpsgCodes(self.tfile.GetValue())
  1201. if type(self.epsgCodeDict) != dict:
  1202. wx.MessageBox(parent=self,
  1203. message=_("Unable to read EPGS codes: %s") % self.epsgCodeDict,
  1204. caption=_("Error"), style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  1205. self.epsglist.Populate(list(), update=True)
  1206. return
  1207. data = list()
  1208. for code, val in self.epsgCodeDict.iteritems():
  1209. if code is not None:
  1210. data.append((code, val[0], val[1]))
  1211. self.epsglist.Populate(data, update=True)
  1212. class CustomPage(TitledPage):
  1213. """!Wizard page for entering custom PROJ.4 string
  1214. for setting coordinate system parameters"""
  1215. def __init__(self, wizard, parent):
  1216. TitledPage.__init__(self, wizard,
  1217. _("Choose method of specifying georeferencing parameters"))
  1218. global coordsys
  1219. self.customstring = ''
  1220. self.parent = parent
  1221. # widgets
  1222. self.text_proj4string = self.MakeTextCtrl(size=(400, 200),
  1223. style=wx.TE_MULTILINE)
  1224. self.label_proj4string = self.MakeLabel(_("Enter PROJ.4 parameters string:"))
  1225. # layout
  1226. self.sizer.AddGrowableCol(2)
  1227. self.sizer.Add(self.label_proj4string,
  1228. flag=wx.ALIGN_LEFT | wx.ALL,
  1229. border=5, pos=(1, 1))
  1230. self.sizer.AddGrowableRow(2)
  1231. self.sizer.Add(self.text_proj4string,
  1232. flag=wx.ALIGN_LEFT | wx.ALL | wx.EXPAND,
  1233. border=5, pos=(2, 1), span=(1, 2))
  1234. self.text_proj4string.Bind(wx.EVT_TEXT, self.GetProjstring)
  1235. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)
  1236. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1237. def OnEnterPage(self, event):
  1238. if len(self.customstring) == 0:
  1239. # disable 'next' button by default
  1240. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  1241. else:
  1242. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  1243. def OnPageChanging(self, event):
  1244. if event.GetDirection():
  1245. # check for datum tranforms
  1246. ret, out, err = gcmd.RunCommand('g.proj',
  1247. read = True, getErrorMsg = True,
  1248. proj4 = self.customstring,
  1249. datumtrans = '-1')
  1250. if ret != 0:
  1251. wx.MessageBox(parent = self,
  1252. message = err,
  1253. caption = _("Error"),
  1254. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  1255. event.Veto()
  1256. return
  1257. if out:
  1258. dtrans = ''
  1259. # open a dialog to select datum transform number
  1260. dlg = SelectTransformDialog(self.parent.parent, transforms=out)
  1261. if dlg.ShowModal() == wx.ID_OK:
  1262. dtrans = dlg.GetTransform()
  1263. if len(dtrans) == 0:
  1264. dlg.Destroy()
  1265. event.Veto()
  1266. return _('Datum transform is required.')
  1267. else:
  1268. dlg.Destroy()
  1269. event.Veto()
  1270. return _('Datum transform is required.')
  1271. self.parent.datumtrans = dtrans
  1272. self.GetNext().SetPrev(self)
  1273. def GetProjstring(self, event):
  1274. """!Change proj string"""
  1275. # TODO: check PROJ.4 syntax
  1276. self.customstring = event.GetString()
  1277. nextButton = wx.FindWindowById(wx.ID_FORWARD)
  1278. if len(self.customstring) == 0:
  1279. if nextButton.IsEnabled():
  1280. nextButton.Enable(False)
  1281. else:
  1282. if not nextButton.IsEnabled():
  1283. nextButton.Enable()
  1284. class SummaryPage(TitledPage):
  1285. """!Shows summary result of choosing coordinate system parameters
  1286. prior to creating location"""
  1287. def __init__(self, wizard, parent):
  1288. TitledPage.__init__(self, wizard, _("Summary"))
  1289. self.parent = parent
  1290. self.panel_proj4string = scrolled.ScrolledPanel(parent = self, id = wx.ID_ANY)
  1291. self.panel_proj = scrolled.ScrolledPanel(parent = self, id = wx.ID_ANY)
  1292. # labels
  1293. self.ldatabase = self.MakeLabel()
  1294. self.llocation = self.MakeLabel()
  1295. self.lprojection = self.MakeLabel(parent = self.panel_proj)
  1296. self.lproj4string = self.MakeLabel(parent = self.panel_proj4string)
  1297. self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
  1298. # do sub-page layout
  1299. self.__DoLayout()
  1300. def __DoLayout(self):
  1301. """!Do page layout"""
  1302. self.sizer.AddGrowableCol(1)
  1303. self.sizer.AddGrowableRow(3,1)
  1304. self.sizer.AddGrowableRow(4,5)
  1305. projSizer = wx.BoxSizer(wx.VERTICAL)
  1306. projSizer.Add(item = self.lprojection, proportion = 1,
  1307. flag = wx.EXPAND | wx.ALL, border = 5)
  1308. self.panel_proj.SetSizer(projSizer)
  1309. proj4stringSizer = wx.BoxSizer(wx.VERTICAL)
  1310. proj4stringSizer.Add(item = self.lproj4string, proportion = 1,
  1311. flag = wx.EXPAND | wx.ALL, border = 5)
  1312. self.panel_proj4string.SetSizer(proj4stringSizer)
  1313. self.panel_proj4string.SetupScrolling()
  1314. self.panel_proj.SetupScrolling(scroll_y=False)
  1315. self.sizer.Add(item=self.MakeLabel(_("GRASS Database:")),
  1316. flag=wx.ALIGN_LEFT | wx.ALL,
  1317. border=5, pos=(1, 0))
  1318. self.sizer.Add(item=self.ldatabase,
  1319. flag=wx.ALIGN_LEFT | wx.ALL,
  1320. border=5, pos=(1, 1))
  1321. self.sizer.Add(item=self.MakeLabel(_("Location Name:")),
  1322. flag=wx.ALIGN_LEFT | wx.ALL,
  1323. border=5, pos=(2, 0))
  1324. self.sizer.Add(item=self.llocation,
  1325. flag=wx.ALIGN_LEFT | wx.ALL,
  1326. border=5, pos=(2, 1))
  1327. self.sizer.Add(item=self.MakeLabel(_("Projection:")),
  1328. flag=wx.ALIGN_LEFT | wx.ALL,
  1329. border=5, pos=(3, 0))
  1330. self.sizer.Add(item=self.panel_proj,
  1331. flag=wx.ALIGN_LEFT | wx.ALL| wx.EXPAND,
  1332. border=0, pos=(3, 1))
  1333. self.sizer.Add(item=self.MakeLabel(_("PROJ.4 definition:")),
  1334. flag=wx.ALIGN_LEFT | wx.ALL,
  1335. border=5, pos=(4, 0))
  1336. self.sizer.Add(item=self.panel_proj4string,
  1337. flag=wx.ALIGN_LEFT | wx.ALL | wx.EXPAND,
  1338. border=0, pos=(4, 1))
  1339. def OnEnterPage(self, event):
  1340. """!Insert values into text controls for summary of location
  1341. creation options
  1342. """
  1343. database = self.parent.startpage.grassdatabase
  1344. location = self.parent.startpage.location
  1345. proj4string = self.parent.CreateProj4String()
  1346. epsgcode = self.parent.epsgpage.epsgcode
  1347. dtrans = self.parent.datumtrans
  1348. global coordsys
  1349. if coordsys in ('proj', 'epsg'):
  1350. if coordsys == 'proj':
  1351. ret, projlabel, err = gcmd.RunCommand('g.proj',
  1352. flags = 'jf',
  1353. proj4 = proj4string,
  1354. datumtrans = dtrans,
  1355. location = location,
  1356. getErrorMsg = True,
  1357. read = True)
  1358. elif coordsys == 'epsg':
  1359. ret, projlabel, err = gcmd.RunCommand('g.proj',
  1360. flags = 'jf',
  1361. epsg = epsgcode,
  1362. datumtrans = dtrans,
  1363. location = location,
  1364. getErrorMsg = True,
  1365. read = True)
  1366. finishButton = wx.FindWindowById(wx.ID_FORWARD)
  1367. if ret == 0:
  1368. self.lproj4string.SetLabel(projlabel.replace(' ', os.linesep))
  1369. finishButton.Enable(True)
  1370. else:
  1371. gcmd.GError(err, parent = self)
  1372. self.lproj4string.SetLabel('')
  1373. finishButton.Enable(False)
  1374. projdesc = self.parent.projpage.projdesc
  1375. ellipsedesc = self.parent.ellipsepage.ellipsedesc
  1376. datumdesc = self.parent.datumpage.datumdesc
  1377. self.ldatabase.SetLabel(database)
  1378. self.llocation.SetLabel(location)
  1379. label = ''
  1380. if coordsys == 'epsg':
  1381. label = 'EPSG code %s (%s)' % (self.parent.epsgpage.epsgcode, self.parent.epsgpage.epsgdesc)
  1382. elif coordsys == 'file':
  1383. label = 'matches file %s' % self.parent.filepage.georeffile
  1384. self.lproj4string.SetLabel("")
  1385. elif coordsys == 'wkt':
  1386. label = 'matches file %s' % self.parent.wktpage.wktfile
  1387. self.lproj4string.SetLabel("")
  1388. elif coordsys == 'proj':
  1389. label = ('%s, %s %s' % (projdesc, datumdesc, ellipsedesc))
  1390. elif coordsys == 'xy':
  1391. label = ('XY coordinate system (not projected).')
  1392. self.lproj4string.SetLabel("")
  1393. elif coordsys == 'custom':
  1394. label = _("custom")
  1395. self.lproj4string.SetLabel(('%s' % self.parent.custompage.customstring.replace(' ', os.linesep)))
  1396. self.lprojection.SetLabel(label)
  1397. def OnFinish(self, event):
  1398. dlg = wx.MessageDialog(parent=self.wizard,
  1399. message=_("Do you want to create GRASS location <%s>?") % location,
  1400. caption=_("Create new location?"),
  1401. style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
  1402. if dlg.ShowModal() == wx.ID_NO:
  1403. dlg.Destroy()
  1404. event.Veto()
  1405. else:
  1406. dlg.Destroy()
  1407. event.Skip()
  1408. class LocationWizard(wx.Object):
  1409. """!Start wizard here and finish wizard here
  1410. """
  1411. def __init__(self, parent, grassdatabase):
  1412. self.__cleanUp()
  1413. global coordsys
  1414. self.parent = parent
  1415. #
  1416. # define wizard image
  1417. #
  1418. imagePath = os.path.join(globalvar.ETCWXDIR, "images", "loc_wizard_qgis.png")
  1419. wizbmp = wx.Image(imagePath, wx.BITMAP_TYPE_PNG)
  1420. wizbmp = wizbmp.ConvertToBitmap()
  1421. #
  1422. # get georeferencing information from tables in $GISBASE/etc
  1423. #
  1424. self.__readData()
  1425. #
  1426. # datum transform number and list of datum transforms
  1427. #
  1428. self.datumtrans = None
  1429. self.proj4string = ''
  1430. #
  1431. # define wizard pages
  1432. #
  1433. self.wizard = wiz.Wizard(parent, id=wx.ID_ANY, title=_("Define new GRASS Location"),
  1434. bitmap=wizbmp)
  1435. self.startpage = DatabasePage(self.wizard, self, grassdatabase)
  1436. self.csystemspage = CoordinateSystemPage(self.wizard, self)
  1437. self.projpage = ProjectionsPage(self.wizard, self)
  1438. self.datumpage = DatumPage(self.wizard, self)
  1439. self.paramspage = ProjParamsPage(self.wizard,self)
  1440. self.epsgpage = EPSGPage(self.wizard, self)
  1441. self.filepage = GeoreferencedFilePage(self.wizard, self)
  1442. self.wktpage = WKTPage(self.wizard, self)
  1443. self.ellipsepage = EllipsePage(self.wizard, self)
  1444. self.custompage = CustomPage(self.wizard, self)
  1445. self.sumpage = SummaryPage(self.wizard, self)
  1446. #
  1447. # set the initial order of the pages
  1448. # (should follow the epsg line)
  1449. #
  1450. self.startpage.SetNext(self.csystemspage)
  1451. self.csystemspage.SetPrev(self.startpage)
  1452. self.csystemspage.SetNext(self.sumpage)
  1453. self.projpage.SetPrev(self.csystemspage)
  1454. self.projpage.SetNext(self.paramspage)
  1455. self.paramspage.SetPrev(self.projpage)
  1456. self.paramspage.SetNext(self.datumpage)
  1457. self.datumpage.SetPrev(self.paramspage)
  1458. self.datumpage.SetNext(self.sumpage)
  1459. self.ellipsepage.SetPrev(self.paramspage)
  1460. self.ellipsepage.SetNext(self.sumpage)
  1461. self.epsgpage.SetPrev(self.csystemspage)
  1462. self.epsgpage.SetNext(self.sumpage)
  1463. self.filepage.SetPrev(self.csystemspage)
  1464. self.filepage.SetNext(self.sumpage)
  1465. self.wktpage.SetPrev(self.csystemspage)
  1466. self.wktpage.SetNext(self.sumpage)
  1467. self.custompage.SetPrev(self.csystemspage)
  1468. self.custompage.SetNext(self.sumpage)
  1469. self.sumpage.SetPrev(self.csystemspage)
  1470. #
  1471. # do pages layout
  1472. #
  1473. self.startpage.DoLayout()
  1474. self.csystemspage.DoLayout()
  1475. self.projpage.DoLayout()
  1476. self.datumpage.DoLayout()
  1477. self.paramspage.DoLayout()
  1478. self.epsgpage.DoLayout()
  1479. self.filepage.DoLayout()
  1480. self.wktpage.DoLayout()
  1481. self.ellipsepage.DoLayout()
  1482. self.custompage.DoLayout()
  1483. self.sumpage.DoLayout()
  1484. self.wizard.FitToPage(self.datumpage)
  1485. size = self.wizard.GetPageSize()
  1486. self.wizard.SetPageSize((size[0], size[1] + 75))
  1487. # new location created?
  1488. self.location = None
  1489. success = False
  1490. # location created in different GIS database?
  1491. self.altdb = False
  1492. #
  1493. # run wizard...
  1494. #
  1495. if self.wizard.RunWizard(self.startpage):
  1496. msg = self.OnWizFinished()
  1497. if not msg:
  1498. self.wizard.Destroy()
  1499. self.location = self.startpage.location
  1500. if self.altdb == False:
  1501. dlg = wx.MessageDialog(parent=self.parent,
  1502. message=_("Do you want to set the default "
  1503. "region extents and resolution now?"),
  1504. caption=_("Location <%s> created") % self.location,
  1505. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  1506. dlg.CenterOnScreen()
  1507. if dlg.ShowModal() == wx.ID_YES:
  1508. dlg.Destroy()
  1509. defineRegion = RegionDef(self.parent, location=self.location)
  1510. defineRegion.CenterOnScreen()
  1511. defineRegion.Show()
  1512. else:
  1513. dlg.Destroy()
  1514. else: # -> error
  1515. self.wizard.Destroy()
  1516. gcmd.GError(parent = self.parent,
  1517. message = "%s" % _("Unable to create new location. "
  1518. "Location <%(loc)s> not created.\n\n"
  1519. "Details: %(err)s") % \
  1520. { 'loc' : self.startpage.location,
  1521. 'err' : msg })
  1522. else: # -> cancelled
  1523. self.wizard.Destroy()
  1524. gcmd.GMessage(parent = self.parent,
  1525. message=_("Location wizard canceled. "
  1526. "Location not created."))
  1527. self.__cleanUp()
  1528. def __cleanUp(self):
  1529. global coordsys
  1530. global north
  1531. global south
  1532. global east
  1533. global west
  1534. global resolution
  1535. global wizerror
  1536. global translist
  1537. coordsys = None
  1538. north = None
  1539. south = None
  1540. east = None
  1541. west = None
  1542. resolution = None
  1543. transformlist = list()
  1544. def __readData(self):
  1545. """!Get georeferencing information from tables in $GISBASE/etc"""
  1546. # read projection and parameters
  1547. f = open(os.path.join(globalvar.ETCDIR, "proj-parms.table"), "r")
  1548. self.projections = {}
  1549. self.projdesc = {}
  1550. for line in f.readlines():
  1551. line = line.strip()
  1552. try:
  1553. proj, projdesc, params = line.split(':')
  1554. paramslist = params.split(';')
  1555. plist = []
  1556. for p in paramslist:
  1557. if p == '': continue
  1558. p1, pdefault = p.split(',')
  1559. pterm, pask = p1.split('=')
  1560. p = [pterm.strip(), pask.strip(), pdefault.strip()]
  1561. plist.append(p)
  1562. self.projections[proj.lower().strip()] = (projdesc.strip(), plist)
  1563. self.projdesc[proj.lower().strip()] = projdesc.strip()
  1564. except:
  1565. continue
  1566. f.close()
  1567. # read datum definitions
  1568. f = open(os.path.join(globalvar.ETCDIR, "datum.table"), "r")
  1569. self.datums = {}
  1570. paramslist = []
  1571. for line in f.readlines():
  1572. line = line.expandtabs(1)
  1573. line = line.strip()
  1574. if line == '' or line[0] == "#":
  1575. continue
  1576. datum, info = line.split(" ", 1)
  1577. info = info.strip()
  1578. datumdesc, params = info.split(" ", 1)
  1579. datumdesc = datumdesc.strip('"')
  1580. paramlist = params.split()
  1581. ellipsoid = paramlist.pop(0)
  1582. self.datums[datum] = (ellipsoid, datumdesc.replace('_', ' '), paramlist)
  1583. f.close()
  1584. # read ellipsiod definitions
  1585. f = open(os.path.join(globalvar.ETCDIR, "ellipse.table"), "r")
  1586. self.ellipsoids = {}
  1587. for line in f.readlines():
  1588. line = line.expandtabs(1)
  1589. line = line.strip()
  1590. if line == '' or line[0] == "#":
  1591. continue
  1592. ellipse, rest = line.split(" ", 1)
  1593. rest = rest.strip('" ')
  1594. desc, params = rest.split('"', 1)
  1595. desc = desc.strip('" ')
  1596. paramslist = params.split()
  1597. self.ellipsoids[ellipse] = (desc, paramslist)
  1598. f.close()
  1599. # read projection parameter description and parsing table
  1600. f = open(os.path.join(globalvar.ETCDIR, "proj-desc.table"), "r")
  1601. self.paramdesc = {}
  1602. for line in f.readlines():
  1603. line = line.strip()
  1604. try:
  1605. pparam, datatype, proj4term, desc = line.split(':')
  1606. self.paramdesc[pparam] = (datatype, proj4term, desc)
  1607. except:
  1608. continue
  1609. f.close()
  1610. def OnWizFinished(self):
  1611. """!Wizard finished, create new location
  1612. @return error message on error
  1613. @return None on success
  1614. """
  1615. database = self.startpage.grassdatabase
  1616. location = self.startpage.location
  1617. # location already exists?
  1618. if os.path.isdir(os.path.join(database,location)):
  1619. gcmd.GError(parent = self.wizard,
  1620. message = "%s <%s>: %s" % \
  1621. (_("Unable to create new location"),
  1622. os.path.join(database, location),
  1623. _("Location already exists in GRASS Database.")))
  1624. return None
  1625. # current GISDbase or a new one?
  1626. current_gdb = grass.gisenv()['GISDBASE']
  1627. if current_gdb != database:
  1628. # change to new GISDbase or create new one
  1629. if os.path.isdir(database) != True:
  1630. # create new directory
  1631. os.mkdir(database)
  1632. # change to new GISDbase directory
  1633. gcmd.RunCommand('g.gisenv',
  1634. parent = self.wizard,
  1635. set='GISDBASE=%s' % database)
  1636. wx.MessageBox(parent=self.wizard,
  1637. message=_("Location <%(loc)s> will be created "
  1638. "in GIS data directory <%(dir)s>."
  1639. "You will need to change the default GIS "
  1640. "data directory in the GRASS startup screen.") % \
  1641. { 'loc' : location, 'dir' : database},
  1642. caption=_("New GIS data directory"),
  1643. style=wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
  1644. # location created in alternate GISDbase
  1645. self.altdb = True
  1646. global coordsys
  1647. try:
  1648. if coordsys == "xy":
  1649. grass.create_location(dbase = self.startpage.grassdatabase,
  1650. location = self.startpage.location)
  1651. elif coordsys == "proj":
  1652. grass.create_location(dbase = self.startpage.grassdatabase,
  1653. location = self.startpage.location,
  1654. proj4 = self.CreateProj4String(),
  1655. datum = self.datumtrans)
  1656. elif coordsys == 'custom':
  1657. grass.create_location(dbase = self.startpage.grassdatabase,
  1658. location = self.startpage.location,
  1659. proj4 = self.custompage.customstring)
  1660. elif coordsys == "epsg":
  1661. if not self.epsgpage.epsgcode:
  1662. return _('EPSG code missing.')
  1663. grass.create_location(dbase = self.startpage.grassdatabase,
  1664. location = self.startpage.location,
  1665. epsg = self.epsgpage.epsgcode,
  1666. datum = self.datumtrans)
  1667. elif coordsys == "file":
  1668. if not self.filepage.georeffile or \
  1669. not os.path.isfile(self.filepage.georeffile):
  1670. return _("File <%s> not found." % self.filepage.georeffile)
  1671. grass.create_location(dbase = self.startpage.grassdatabase,
  1672. location = self.startpage.location,
  1673. filename = self.filepage.georeffile)
  1674. elif coordsys == "wkt":
  1675. if not self.wktpage.wktfile or \
  1676. not os.path.isfile(self.wktpage.wktfile):
  1677. return _("File <%s> not found." % self.wktpage.wktfile)
  1678. grass.create_location(dbase = self.startpage.grassdatabase,
  1679. location = self.startpage.location,
  1680. filename = self.wktpage.wktfile)
  1681. except grass.ScriptException, e:
  1682. return str(e)
  1683. return None
  1684. def CreateProj4String(self):
  1685. """!Constract PROJ.4 string"""
  1686. location = self.startpage.location
  1687. proj = self.projpage.p4proj
  1688. projdesc = self.projpage.projdesc
  1689. proj4params = self.paramspage.p4projparams
  1690. datum = self.datumpage.datum
  1691. if self.datumpage.datumdesc:
  1692. datumdesc = self.datumpage.datumdesc +' - ' + self.datumpage.ellipse
  1693. else:
  1694. datumdesc = ''
  1695. datumparams = self.datumpage.datumparams
  1696. ellipse = self.ellipsepage.ellipse
  1697. ellipsedesc = self.ellipsepage.ellipsedesc
  1698. ellipseparams = self.ellipsepage.ellipseparams
  1699. #
  1700. # creating PROJ.4 string
  1701. #
  1702. proj4string = '%s %s' % (proj, proj4params)
  1703. # set ellipsoid parameters
  1704. if ellipse != '': proj4string = '%s +ellps=%s' % (proj4string, ellipse)
  1705. for item in ellipseparams:
  1706. if item[:4] == 'f=1/':
  1707. item = ' +rf='+item[4:]
  1708. else:
  1709. item = ' +'+item
  1710. proj4string = '%s %s' % (proj4string, item)
  1711. # set datum and transform parameters if relevant
  1712. if datum != '':
  1713. proj4string = '%s +datum=%s' % (proj4string, datum)
  1714. if datumparams:
  1715. for item in datumparams:
  1716. proj4string = '%s +%s' % (proj4string,item)
  1717. proj4string = '%s +no_defs' % proj4string
  1718. return proj4string
  1719. class RegionDef(BaseClass, wx.Frame):
  1720. """
  1721. Page for setting default region extents and resolution
  1722. """
  1723. def __init__(self, parent, id=wx.ID_ANY,
  1724. title=_("Set default region extent and resolution"), location=None):
  1725. wx.Frame.__init__(self, parent, id, title, size=(650,300))
  1726. panel = wx.Panel(self, id=wx.ID_ANY)
  1727. self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, 'grass.ico'), wx.BITMAP_TYPE_ICO))
  1728. self.parent = parent
  1729. self.location = location
  1730. #
  1731. # default values
  1732. #
  1733. # 2D
  1734. self.north = 1.0
  1735. self.south = 0.0
  1736. self.east = 1.0
  1737. self.west = 0.0
  1738. self.nsres = 1.0
  1739. self.ewres = 1.0
  1740. # 3D
  1741. self.top = 1.0
  1742. self.bottom = 0.0
  1743. # self.nsres3 = 1.0
  1744. # self.ewres3 = 1.0
  1745. self.tbres = 1.0
  1746. #
  1747. # inputs
  1748. #
  1749. # 2D
  1750. self.tnorth = self.MakeTextCtrl(text=str(self.north), size=(150, -1), parent=panel)
  1751. self.tsouth = self.MakeTextCtrl(str(self.south), size=(150, -1), parent=panel)
  1752. self.twest = self.MakeTextCtrl(str(self.west), size=(150, -1), parent=panel)
  1753. self.teast = self.MakeTextCtrl(str(self.east), size=(150, -1), parent=panel)
  1754. self.tnsres = self.MakeTextCtrl(str(self.nsres), size=(150, -1), parent=panel)
  1755. self.tewres = self.MakeTextCtrl(str(self.ewres), size=(150, -1), parent=panel)
  1756. #
  1757. # labels
  1758. #
  1759. self.lrows = self.MakeLabel(parent=panel)
  1760. self.lcols = self.MakeLabel(parent=panel)
  1761. self.lcells = self.MakeLabel(parent=panel)
  1762. #
  1763. # buttons
  1764. #
  1765. self.bset = self.MakeButton(text=_("&Set region"), id=wx.ID_OK, parent=panel)
  1766. self.bcancel = wx.Button(panel, id=wx.ID_CANCEL)
  1767. self.bset.SetDefault()
  1768. #
  1769. # image
  1770. #
  1771. self.img = wx.Image(os.path.join(globalvar.ETCWXDIR, "images",
  1772. "qgis_world.png"), wx.BITMAP_TYPE_PNG).ConvertToBitmap()
  1773. #
  1774. # set current working environment to PERMANENT mapset
  1775. # in selected location in order to set default region (WIND)
  1776. #
  1777. envval = {}
  1778. ret = gcmd.RunCommand('g.gisenv',
  1779. read = True)
  1780. if ret:
  1781. for line in ret.splitlines():
  1782. key, val = line.split('=')
  1783. envval[key] = val
  1784. self.currlocation = envval['LOCATION_NAME'].strip("';")
  1785. self.currmapset = envval['MAPSET'].strip("';")
  1786. if self.currlocation != self.location or self.currmapset != 'PERMANENT':
  1787. gcmd.RunCommand('g.gisenv',
  1788. set = 'LOCATION_NAME=%s' % self.location)
  1789. gcmd.RunCommand('g.gisenv',
  1790. set = 'MAPSET=PERMANENT')
  1791. else:
  1792. dlg = wx.MessageBox(parent=self,
  1793. message=_('Invalid location selected.'),
  1794. caption=_("Error"), style=wx.ID_OK | wx.ICON_ERROR)
  1795. return
  1796. #
  1797. # get current region settings
  1798. #
  1799. region = {}
  1800. ret = gcmd.RunCommand('g.region',
  1801. read = True,
  1802. flags = 'gp3')
  1803. if ret:
  1804. for line in ret.splitlines():
  1805. key, val = line.split('=')
  1806. region[key] = float(val)
  1807. else:
  1808. dlg = wx.MessageBox(parent=self,
  1809. message=_("Invalid region"),
  1810. caption=_("Error"), style=wx.ID_OK | wx.ICON_ERROR)
  1811. dlg.ShowModal()
  1812. dlg.Destroy()
  1813. return
  1814. #
  1815. # update values
  1816. # 2D
  1817. self.north = float(region['n'])
  1818. self.south = float(region['s'])
  1819. self.east = float(region['e'])
  1820. self.west = float(region['w'])
  1821. self.nsres = float(region['nsres'])
  1822. self.ewres = float(region['ewres'])
  1823. self.rows = int(region['rows'])
  1824. self.cols = int(region['cols'])
  1825. self.cells = int(region['cells'])
  1826. # 3D
  1827. self.top = float(region['t'])
  1828. self.bottom = float(region['b'])
  1829. # self.nsres3 = float(region['nsres3'])
  1830. # self.ewres3 = float(region['ewres3'])
  1831. self.tbres = float(region['tbres'])
  1832. self.depth = int(region['depths'])
  1833. self.cells3 = int(region['cells3'])
  1834. #
  1835. # 3D box collapsable
  1836. #
  1837. self.infoCollapseLabelExp = _("Click here to show 3D settings")
  1838. self.infoCollapseLabelCol = _("Click here to hide 3D settings")
  1839. self.settings3D = wx.CollapsiblePane(parent=panel,
  1840. label=self.infoCollapseLabelExp,
  1841. style=wx.CP_DEFAULT_STYLE |
  1842. wx.CP_NO_TLW_RESIZE | wx.EXPAND)
  1843. self.MakeSettings3DPaneContent(self.settings3D.GetPane())
  1844. self.settings3D.Collapse(False) # FIXME
  1845. self.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED, self.OnSettings3DPaneChanged, self.settings3D)
  1846. #
  1847. # set current region settings
  1848. #
  1849. self.tnorth.SetValue(str(self.north))
  1850. self.tsouth.SetValue(str(self.south))
  1851. self.twest.SetValue(str(self.west))
  1852. self.teast.SetValue(str(self.east))
  1853. self.tnsres.SetValue(str(self.nsres))
  1854. self.tewres.SetValue(str(self.ewres))
  1855. self.ttop.SetValue(str(self.top))
  1856. self.tbottom.SetValue(str(self.bottom))
  1857. # self.tnsres3.SetValue(str(self.nsres3))
  1858. # self.tewres3.SetValue(str(self.ewres3))
  1859. self.ttbres.SetValue(str(self.tbres))
  1860. self.lrows.SetLabel(_("Rows: %d") % self.rows)
  1861. self.lcols.SetLabel(_("Cols: %d") % self.cols)
  1862. self.lcells.SetLabel(_("Cells: %d") % self.cells)
  1863. #
  1864. # bindings
  1865. #
  1866. self.Bind(wx.EVT_BUTTON, self.OnSetButton, self.bset)
  1867. self.Bind(wx.EVT_BUTTON, self.OnCancel, self.bcancel)
  1868. self.tnorth.Bind(wx.EVT_TEXT, self.OnValue)
  1869. self.tsouth.Bind(wx.EVT_TEXT, self.OnValue)
  1870. self.teast.Bind(wx.EVT_TEXT, self.OnValue)
  1871. self.twest.Bind(wx.EVT_TEXT, self.OnValue)
  1872. self.tnsres.Bind(wx.EVT_TEXT, self.OnValue)
  1873. self.tewres.Bind(wx.EVT_TEXT, self.OnValue)
  1874. self.ttop.Bind(wx.EVT_TEXT, self.OnValue)
  1875. self.tbottom.Bind(wx.EVT_TEXT, self.OnValue)
  1876. # self.tnsres3.Bind(wx.EVT_TEXT, self.OnValue)
  1877. # self.tewres3.Bind(wx.EVT_TEXT, self.OnValue)
  1878. self.ttbres.Bind(wx.EVT_TEXT, self.OnValue)
  1879. self.__DoLayout(panel)
  1880. self.SetMinSize(self.GetBestSize())
  1881. self.minWindowSize = self.GetMinSize()
  1882. def MakeSettings3DPaneContent(self, pane):
  1883. """!Create 3D region settings pane"""
  1884. border = wx.BoxSizer(wx.VERTICAL)
  1885. gridSizer = wx.GridBagSizer(vgap=0, hgap=0)
  1886. # inputs
  1887. self.ttop = wx.TextCtrl(parent=pane, id=wx.ID_ANY, value=str(self.top),
  1888. size=(150, -1))
  1889. self.tbottom = wx.TextCtrl(parent=pane, id=wx.ID_ANY, value=str(self.bottom),
  1890. size=(150, -1))
  1891. self.ttbres = wx.TextCtrl(parent=pane, id=wx.ID_ANY, value=str(self.tbres),
  1892. size=(150, -1))
  1893. # self.tnsres3 = wx.TextCtrl(parent=pane, id=wx.ID_ANY, value=str(self.nsres3),
  1894. # size=(150, -1))
  1895. # self.tewres3 = wx.TextCtrl(parent=pane, id=wx.ID_ANY, value=str(self.ewres3),
  1896. # size=(150, -1))
  1897. #labels
  1898. self.ldepth = wx.StaticText(parent=pane, label=_("Depth: %d") % self.depth)
  1899. self.lcells3 = wx.StaticText(parent=pane, label=_("3D Cells: %d") % self.cells3)
  1900. # top
  1901. gridSizer.Add(item=wx.StaticText(parent=pane, label=_("Top")),
  1902. flag=wx.ALIGN_CENTER |
  1903. wx.LEFT | wx.RIGHT | wx.TOP, border=5,
  1904. pos=(0, 1))
  1905. gridSizer.Add(item=self.ttop,
  1906. flag=wx.ALIGN_CENTER_HORIZONTAL |
  1907. wx.ALL, border=5, pos=(1, 1))
  1908. # bottom
  1909. gridSizer.Add(item=wx.StaticText(parent=pane, label=_("Bottom")),
  1910. flag=wx.ALIGN_CENTER |
  1911. wx.LEFT | wx.RIGHT | wx.TOP, border=5,
  1912. pos=(0, 2))
  1913. gridSizer.Add(item=self.tbottom,
  1914. flag=wx.ALIGN_CENTER_HORIZONTAL |
  1915. wx.ALL, border=5, pos=(1, 2))
  1916. # tbres
  1917. gridSizer.Add(item=wx.StaticText(parent=pane, label=_("T-B resolution")),
  1918. flag=wx.ALIGN_CENTER |
  1919. wx.LEFT | wx.RIGHT | wx.TOP, border=5,
  1920. pos=(0, 3))
  1921. gridSizer.Add(item=self.ttbres,
  1922. flag=wx.ALIGN_CENTER_HORIZONTAL |
  1923. wx.ALL, border=5, pos=(1, 3))
  1924. # res
  1925. # gridSizer.Add(item=wx.StaticText(parent=pane, label=_("3D N-S resolution")),
  1926. # flag=wx.ALIGN_CENTER |
  1927. # wx.LEFT | wx.RIGHT | wx.TOP, border=5,
  1928. # pos=(2, 1))
  1929. # gridSizer.Add(item=self.tnsres3,
  1930. # flag=wx.ALIGN_CENTER_HORIZONTAL |
  1931. # wx.ALL, border=5, pos=(3, 1))
  1932. # gridSizer.Add(item=wx.StaticText(parent=pane, label=_("3D E-W resolution")),
  1933. # flag=wx.ALIGN_CENTER |
  1934. # wx.LEFT | wx.RIGHT | wx.TOP, border=5,
  1935. # pos=(2, 3))
  1936. # gridSizer.Add(item=self.tewres3,
  1937. # flag=wx.ALIGN_CENTER_HORIZONTAL |
  1938. # wx.ALL, border=5, pos=(3, 3))
  1939. # rows/cols/cells
  1940. gridSizer.Add(item=self.ldepth,
  1941. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  1942. wx.ALL, border=5, pos=(2, 1))
  1943. gridSizer.Add(item=self.lcells3,
  1944. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  1945. wx.ALL, border=5, pos=(2, 2))
  1946. border.Add(item=gridSizer, proportion=1,
  1947. flag=wx.ALL | wx.ALIGN_CENTER | wx.EXPAND, border=5)
  1948. pane.SetSizer(border)
  1949. border.Fit(pane)
  1950. def OnSettings3DPaneChanged(self, event):
  1951. """!Collapse 3D settings box"""
  1952. if self.settings3D.IsExpanded():
  1953. self.settings3D.SetLabel(self.infoCollapseLabelCol)
  1954. self.Layout()
  1955. self.SetSize(self.GetBestSize())
  1956. self.SetMinSize(self.GetSize())
  1957. else:
  1958. self.settings3D.SetLabel(self.infoCollapseLabelExp)
  1959. self.Layout()
  1960. self.SetSize(self.minWindowSize)
  1961. self.SetMinSize(self.minWindowSize)
  1962. self.SendSizeEvent()
  1963. def __DoLayout(self, panel):
  1964. """!Window layout"""
  1965. frameSizer = wx.BoxSizer(wx.VERTICAL)
  1966. gridSizer = wx.GridBagSizer(vgap=0, hgap=0)
  1967. settings3DSizer = wx.BoxSizer(wx.VERTICAL)
  1968. buttonSizer = wx.BoxSizer(wx.HORIZONTAL)
  1969. # north
  1970. gridSizer.Add(item=self.MakeLabel(text=_("North"), parent=panel),
  1971. flag=wx.ALIGN_BOTTOM | wx.ALIGN_CENTER_HORIZONTAL |
  1972. wx.TOP | wx.LEFT | wx.RIGHT, border=5, pos=(0, 2))
  1973. gridSizer.Add(item=self.tnorth,
  1974. flag=wx.ALIGN_CENTER_HORIZONTAL |
  1975. wx.ALIGN_CENTER_VERTICAL |
  1976. wx.ALL, border=5, pos=(1, 2))
  1977. # west
  1978. gridSizer.Add(item=self.MakeLabel(text=_("West"), parent=panel),
  1979. flag=wx.ALIGN_RIGHT |
  1980. wx.ALIGN_CENTER_VERTICAL |
  1981. wx.LEFT | wx.TOP | wx.BOTTOM, border=5, pos=(2, 0))
  1982. gridSizer.Add(item=self.twest,
  1983. flag=wx.ALIGN_RIGHT |
  1984. wx.ALIGN_CENTER_VERTICAL |
  1985. wx.ALL, border=5, pos=(2, 1))
  1986. gridSizer.Add(item=wx.StaticBitmap(panel, wx.ID_ANY, self.img, (-1, -1),
  1987. (self.img.GetWidth(), self.img.GetHeight())),
  1988. flag=wx.ALIGN_CENTER |
  1989. wx.ALIGN_CENTER_VERTICAL |
  1990. wx.ALL, border=5, pos=(2, 2))
  1991. # east
  1992. gridSizer.Add(item=self.teast,
  1993. flag=wx.ALIGN_CENTER_HORIZONTAL |
  1994. wx.ALIGN_CENTER_VERTICAL |
  1995. wx.ALL, border=5, pos=(2, 3))
  1996. gridSizer.Add(item=self.MakeLabel(text=_("East"), parent=panel),
  1997. flag=wx.ALIGN_LEFT |
  1998. wx.ALIGN_CENTER_VERTICAL |
  1999. wx.RIGHT | wx.TOP | wx.BOTTOM, border=5, pos=(2, 4))
  2000. # south
  2001. gridSizer.Add(item=self.tsouth,
  2002. flag=wx.ALIGN_CENTER_HORIZONTAL |
  2003. wx.ALIGN_CENTER_VERTICAL |
  2004. wx.ALL, border=5, pos=(3, 2))
  2005. gridSizer.Add(item=self.MakeLabel(text=_("South"), parent=panel),
  2006. flag=wx.ALIGN_TOP | wx.ALIGN_CENTER_HORIZONTAL |
  2007. wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5, pos=(4, 2))
  2008. # ns-res
  2009. gridSizer.Add(item=self.MakeLabel(text=_("N-S resolution"), parent=panel),
  2010. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  2011. wx.TOP | wx.LEFT | wx.RIGHT, border=5, pos=(5, 1))
  2012. gridSizer.Add(item=self.tnsres,
  2013. flag=wx.ALIGN_RIGHT |
  2014. wx.ALIGN_CENTER_VERTICAL |
  2015. wx.ALL, border=5, pos=(6, 1))
  2016. # ew-res
  2017. gridSizer.Add(item=self.MakeLabel(text=_("E-W resolution"), parent=panel),
  2018. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  2019. wx.TOP | wx.LEFT | wx.RIGHT, border=5, pos=(5, 3))
  2020. gridSizer.Add(item=self.tewres,
  2021. flag=wx.ALIGN_RIGHT |
  2022. wx.ALIGN_CENTER_VERTICAL |
  2023. wx.ALL, border=5, pos=(6, 3))
  2024. # rows/cols/cells
  2025. gridSizer.Add(item=self.lrows,
  2026. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  2027. wx.ALL, border=5, pos=(7, 1))
  2028. gridSizer.Add(item=self.lcells,
  2029. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  2030. wx.ALL, border=5, pos=(7, 2))
  2031. gridSizer.Add(item=self.lcols,
  2032. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER |
  2033. wx.ALL, border=5, pos=(7, 3))
  2034. # 3D
  2035. settings3DSizer.Add(item=self.settings3D,
  2036. flag=wx.ALL,
  2037. border=5)
  2038. # buttons
  2039. buttonSizer.Add(item=self.bcancel, proportion=1,
  2040. flag=wx.ALIGN_RIGHT |
  2041. wx.ALIGN_CENTER_VERTICAL |
  2042. wx.ALL, border=10)
  2043. buttonSizer.Add(item=self.bset, proportion=1,
  2044. flag=wx.ALIGN_CENTER |
  2045. wx.ALIGN_CENTER_VERTICAL |
  2046. wx.ALL, border=10)
  2047. frameSizer.Add(item=gridSizer, proportion=1,
  2048. flag=wx.ALL | wx.ALIGN_CENTER, border=5)
  2049. frameSizer.Add(item=settings3DSizer, proportion=0,
  2050. flag=wx.ALL | wx.ALIGN_CENTER, border=5)
  2051. frameSizer.Add(item=buttonSizer, proportion=0,
  2052. flag=wx.ALL | wx.ALIGN_RIGHT, border=5)
  2053. self.SetAutoLayout(True)
  2054. panel.SetSizer(frameSizer)
  2055. frameSizer.Fit(panel)
  2056. self.Layout()
  2057. def OnValue(self, event):
  2058. """!Set given value"""
  2059. try:
  2060. if event.GetId() == self.tnorth.GetId():
  2061. self.north = float(event.GetString())
  2062. elif event.GetId() == self.tsouth.GetId():
  2063. self.south = float(event.GetString())
  2064. elif event.GetId() == self.teast.GetId():
  2065. self.east = float(event.GetString())
  2066. elif event.GetId() == self.twest.GetId():
  2067. self.west = float(event.GetString())
  2068. elif event.GetId() == self.tnsres.GetId():
  2069. self.nsres = float(event.GetString())
  2070. elif event.GetId() == self.tewres.GetId():
  2071. self.ewres = float(event.GetString())
  2072. elif event.GetId() == self.ttop.GetId():
  2073. self.top = float(event.GetString())
  2074. elif event.GetId() == self.tbottom.GetId():
  2075. self.bottom = float(event.GetString())
  2076. # elif event.GetId() == self.tnsres3.GetId():
  2077. # self.nsres3 = float(event.GetString())
  2078. # elif event.GetId() == self.tewres3.GetId():
  2079. # self.ewres3 = float(event.GetString())
  2080. elif event.GetId() == self.ttbres.GetId():
  2081. self.tbres = float(event.GetString())
  2082. self.__UpdateInfo()
  2083. except ValueError, e:
  2084. if len(event.GetString()) > 0 and event.GetString() != '-':
  2085. dlg = wx.MessageBox(parent=self,
  2086. message=_("Invalid value: %s") % e,
  2087. caption=_("Error"),
  2088. style=wx.OK | wx.ICON_ERROR)
  2089. # reset values
  2090. self.tnorth.SetValue(str(self.north))
  2091. self.tsouth.SetValue(str(self.south))
  2092. self.teast.SetValue(str(self.east))
  2093. self.twest.SetValue(str(self.west))
  2094. self.tnsres.SetValue(str(self.nsres))
  2095. self.tewres.SetValue(str(self.ewres))
  2096. self.ttop.SetValue(str(self.top))
  2097. self.tbottom.SetValue(str(self.bottom))
  2098. self.ttbres.SetValue(str(self.tbres))
  2099. # self.tnsres3.SetValue(str(self.nsres3))
  2100. # self.tewres3.SetValue(str(self.ewres3))
  2101. event.Skip()
  2102. def __UpdateInfo(self):
  2103. """!Update number of rows/cols/cells"""
  2104. self.rows = int((self.north - self.south) / self.nsres)
  2105. self.cols = int((self.east - self.west) / self.ewres)
  2106. self.cells = self.rows * self.cols
  2107. self.depth = int((self.top - self.bottom) / self.tbres)
  2108. self.cells3 = self.rows * self.cols * self.depth
  2109. # 2D
  2110. self.lrows.SetLabel(_("Rows: %d") % self.rows)
  2111. self.lcols.SetLabel(_("Cols: %d") % self.cols)
  2112. self.lcells.SetLabel(_("Cells: %d") % self.cells)
  2113. # 3D
  2114. self.ldepth.SetLabel(_("Depth: %d" % self.depth))
  2115. self.lcells3.SetLabel(_("3D Cells: %d" % self.cells3))
  2116. def OnSetButton(self, event=None):
  2117. """!Set default region"""
  2118. ret = gcmd.RunCommand('g.region',
  2119. flags = 'sgpa',
  2120. n = self.north,
  2121. s = self.south,
  2122. e = self.east,
  2123. w = self.west,
  2124. nsres = self.nsres,
  2125. ewres = self.ewres,
  2126. t = self.top,
  2127. b = self.bottom,
  2128. tbres = self.tbres)
  2129. if ret == 0:
  2130. self.Destroy()
  2131. def OnCancel(self, event):
  2132. self.Destroy()
  2133. class TransList(wx.VListBox):
  2134. """!Creates a multiline listbox for selecting datum transforms"""
  2135. def OnDrawItem(self, dc, rect, n):
  2136. if self.GetSelection() == n:
  2137. c = wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)
  2138. else:
  2139. c = self.GetForegroundColour()
  2140. dc.SetFont(self.GetFont())
  2141. dc.SetTextForeground(c)
  2142. dc.DrawLabel(self._getItemText(n), rect,
  2143. wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
  2144. def OnMeasureItem(self, n):
  2145. height = 0
  2146. if self._getItemText(n) == None: return
  2147. for line in self._getItemText(n).splitlines():
  2148. w, h = self.GetTextExtent(line)
  2149. height += h
  2150. return height + 5
  2151. def _getItemText(self, item):
  2152. global transformlist
  2153. transitem = transformlist[item]
  2154. if transitem.strip() !='':
  2155. return transitem
  2156. class SelectTransformDialog(wx.Dialog):
  2157. """!Dialog for selecting datum transformations"""
  2158. def __init__(self, parent, transforms, title=_("Select datum transformation"),
  2159. pos=wx.DefaultPosition, size=wx.DefaultSize,
  2160. style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER):
  2161. wx.Dialog.__init__(self, parent, wx.ID_ANY, title, pos, size, style)
  2162. global transformlist
  2163. self.CentreOnParent()
  2164. # default transform number
  2165. self.transnum = 0
  2166. panel = scrolled.ScrolledPanel(self, wx.ID_ANY)
  2167. sizer = wx.BoxSizer(wx.VERTICAL)
  2168. #
  2169. # set panel sizer
  2170. #
  2171. panel.SetSizer(sizer)
  2172. panel.SetupScrolling()
  2173. #
  2174. # dialog body
  2175. #
  2176. bodyBox = wx.StaticBox(parent=panel, id=wx.ID_ANY,
  2177. label=" %s " % _("Select from list of datum transformations"))
  2178. bodySizer = wx.StaticBoxSizer(bodyBox)
  2179. # add no transform option
  2180. transforms = '---\n\n0\nDo not apply any datum transformations\n\n' + transforms
  2181. transformlist = transforms.split('---')
  2182. tlistlen = len(transformlist)
  2183. # calculate size for transform list
  2184. height = 0
  2185. width = 0
  2186. for line in transforms.splitlines():
  2187. w, h = self.GetTextExtent(line)
  2188. height += h
  2189. width = max(width, w)
  2190. height = height + 5
  2191. if height > 400: height = 400
  2192. width = width + 5
  2193. if width > 400: width = 400
  2194. #
  2195. # VListBox for displaying and selecting transformations
  2196. #
  2197. self.translist = TransList(panel, id=-1, size=(width, height), style=wx.SUNKEN_BORDER)
  2198. self.translist.SetItemCount(tlistlen)
  2199. self.translist.SetSelection(2)
  2200. self.translist.SetFocus()
  2201. self.Bind(wx.EVT_LISTBOX, self.ClickTrans, self.translist)
  2202. bodySizer.Add(item=self.translist, proportion=1, flag=wx.ALIGN_CENTER|wx.ALL|wx.EXPAND)
  2203. #
  2204. # buttons
  2205. #
  2206. btnsizer = wx.StdDialogButtonSizer()
  2207. btn = wx.Button(parent=panel, id=wx.ID_OK)
  2208. btn.SetDefault()
  2209. btnsizer.AddButton(btn)
  2210. btn = wx.Button(parent=panel, id=wx.ID_CANCEL)
  2211. btnsizer.AddButton(btn)
  2212. btnsizer.Realize()
  2213. sizer.Add(item=bodySizer, proportion=1,
  2214. flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
  2215. sizer.Add(item=btnsizer, proportion=0,
  2216. flag= wx.ALL | wx.ALIGN_RIGHT, border=5)
  2217. sizer.Fit(panel)
  2218. self.SetSize(self.GetBestSize())
  2219. self.Layout()
  2220. def ClickTrans(self, event):
  2221. """!Get the number of the datum transform to use in g.proj"""
  2222. self.transnum = event.GetSelection()
  2223. self.transnum = self.transnum - 1
  2224. def GetTransform(self):
  2225. """!Get the number of the datum transform to use in g.proj"""
  2226. self.transnum = self.translist.GetSelection()
  2227. self.transnum = self.transnum - 1
  2228. return self.transnum
  2229. if __name__ == "__main__":
  2230. import gettext
  2231. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
  2232. app = wx.PySimpleApp()
  2233. # gWizard = LocationWizard(None, "")
  2234. gWizard = RegionDef(None)
  2235. gWizzard.Show()
  2236. app.MainLoop()