wizard.py 98 KB

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