wizard.py 98 KB

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