wizard.py 95 KB

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