location_wizard.py 103 KB

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