wizard.py 98 KB

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