[GRASS-SVN] r44418 - in grass/branches/develbranch_6: gui/wxpython/gui_modules lib/python

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Nov 25 11:09:12 EST 2010


Author: martinl
Date: 2010-11-25 08:09:12 -0800 (Thu, 25 Nov 2010)
New Revision: 44418

Modified:
   grass/branches/develbranch_6/gui/wxpython/gui_modules/location_wizard.py
   grass/branches/develbranch_6/lib/python/core.py
Log:
wxGUI/locwizard: use grass.create_location() to create a new location
(merge r44417 from trunk)


Modified: grass/branches/develbranch_6/gui/wxpython/gui_modules/location_wizard.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/gui_modules/location_wizard.py	2010-11-25 16:06:27 UTC (rev 44417)
+++ grass/branches/develbranch_6/gui/wxpython/gui_modules/location_wizard.py	2010-11-25 16:09:12 UTC (rev 44418)
@@ -920,7 +920,7 @@
             self.parent.ellipsepage.ellipseparams = self.parent.ellipsoids[self.ellipse][1]
 
     def OnEnterPage(self,event):
-        self.parent.datumtrans = 0
+        self.parent.datumtrans = None
         if event.GetDirection():
             if len(self.datum) == 0:
                 # disable 'next' button by default when entering from previous page
@@ -1316,7 +1316,7 @@
         self.Bind(wiz.EVT_WIZARD_PAGE_CHANGED, self.OnEnterPage)
 
     def OnEnterPage(self, event):
-        self.parent.datumtrans = 0
+        self.parent.datumtrans = None
         if event.GetDirection():
             if not self.epsgcode:
                 # disable 'next' button by default
@@ -1697,7 +1697,7 @@
         #
         # datum transform number and list of datum transforms
         #
-        self.datumtrans = 0
+        self.datumtrans = None
         self.proj4string = ''
         
         #
@@ -1782,7 +1782,7 @@
         #
         if self.wizard.RunWizard(self.startpage):
             msg = self.OnWizFinished()
-            if len(msg) < 1:
+            if not msg:
                 self.wizard.Destroy()
                 self.location = self.startpage.location
                 
@@ -1791,7 +1791,7 @@
                                            message=_("Do you want to set the default "
                                                      "region extents and resolution now?"),
                                            caption=_("Location <%s> created") % self.location,
-                                           style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
+                                           style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
                     dlg.CenterOnScreen()
                     if dlg.ShowModal() == wx.ID_YES:
                         dlg.Destroy()
@@ -1802,22 +1802,18 @@
                         dlg.Destroy()
             else: # -> error
                 self.wizard.Destroy()
-                wx.MessageBox(parent=self.parent,
-                              message="%s" % _("Unable to create new location. "
+                gcmd.GError(parent = self.parent,
+                            message = "%s" % _("Unable to create new location. "
                                                "Location <%(loc)s> not created.\n\n"
                                                "Details: %(err)s") % \
                                   { 'loc' : self.startpage.location,
-                                    'err' : msg },
-                              caption=_("Location wizard"),
-                              style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
+                                    'err' : msg })
         else: # -> cancelled
             self.wizard.Destroy()
-            wx.MessageBox(parent=self.parent,
+            gcmd.GMessage(parent = self.parent,
                           message=_("Location wizard canceled. "
-                                    "Location not created."),
-                          caption=_("Location wizard"),
-                          style=wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
-        
+                                    "Location not created."))
+            
         self.__cleanUp()
         
     def __cleanUp(self):
@@ -1910,23 +1906,22 @@
         f.close()
 
     def OnWizFinished(self):
+        """!Wizard finished, create new location
+
+        @return error message on error
+        @return None on success
+        """
         database = self.startpage.grassdatabase
         location = self.startpage.location
-        global coordsys
-        msg = '' # error message (empty on success)
         
         # location already exists?
         if os.path.isdir(os.path.join(database,location)):
-            dlg = wx.MessageDialog(parent=self.wizard,
-                                   message="%s <%s>: %s" % \
-                                       (_("Unable to create new location"),
-                                        os.path.join(database, location),
-                                        _("Location already exists in GRASS Database.")),
-                                   caption=_("Error"),
-                                   style=wx.OK | wx.ICON_ERROR)
-            dlg.ShowModal()
-            dlg.Destroy()
-            return False
+            gcmd.GError(parent = self.wizard,
+                        message = "%s <%s>: %s" % \
+                            (_("Unable to create new location"),
+                             os.path.join(database, location),
+                             _("Location already exists in GRASS Database.")))
+            return None
         
         # current GISDbase or a new one?
         current_gdb = grass.gisenv()['GISDBASE']
@@ -1935,7 +1930,7 @@
             if os.path.isdir(database) != True:
                 # create new directory
                 os.mkdir(database)
-                
+            
             # change to new GISDbase directory
             gcmd.RunCommand('g.gisenv',
                             parent = self.wizard,
@@ -1949,76 +1944,54 @@
                               { 'loc' : location, 'dir' : database},
                           caption=_("New GIS data directory"), 
                           style=wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
+            
             # location created in alternate GISDbase
             self.altdb = True
-            
-        if coordsys == "xy":
-            msg = self.XYCreate()
-        elif coordsys == "proj":
-            proj4string = self.CreateProj4String()
-            msg = self.Proj4Create(proj4string)
-        elif coordsys == 'custom':
-            msg = self.CustomCreate()
-        elif coordsys == "epsg":
-            msg = self.EPSGCreate()
-        elif coordsys == "file":
-            msg = self.FileCreate()
-        elif coordsys == "wkt":
-            msg = self.WKTCreate()
-
-        return msg
-
-    def XYCreate(self):
-        """!Create an XY location
-
-        @return error message (empty string on success)
-        """        
-        database = self.startpage.grassdatabase
-        location = self.startpage.location
         
-        # create location directory and PERMANENT mapset
+        global coordsys        
         try:
-            os.mkdir(os.path.join(database, location))
-            os.mkdir(os.path.join(database, location, 'PERMANENT'))
-            # create DEFAULT_WIND and WIND files
-            regioninfo =   ['proj:       0',
-                            'zone:       0',
-                            'north:      1',
-                            'south:      0',
-                            'east:       1',
-                            'west:       0',
-                            'cols:       1',
-                            'rows:       1',
-                            'e-w resol:  1',
-                            'n-s resol:  1',
-                            'top:        1',
-                            'bottom:     0',
-                            'cols3:      1',
-                            'rows3:      1',
-                            'depths:     1',
-                            'e-w resol3: 1',
-                            'n-s resol3: 1',
-                            't-b resol:  1']
-            
-            defwind = open(os.path.join(database, location, 
-                                        "PERMANENT", "DEFAULT_WIND"), 'w')
-            for param in regioninfo:
-                defwind.write(param + '%s' % os.linesep)
-            defwind.close()
-            
-            shutil.copy(os.path.join(database, location, "PERMANENT", "DEFAULT_WIND"),
-                        os.path.join(database, location, "PERMANENT", "WIND"))
-            
-            # create MYNAME file
-            myname = open(os.path.join(database, location, "PERMANENT",
-                                       "MYNAME"), 'w')
-            myname.write('%s' % os.linesep)
-            myname.close()
-        except OSError, e:
-            return e
+            if coordsys == "xy":
+                grass.create_location(dbase = self.startpage.grassdatabase,
+                                      location = self.startpage.location)
+            elif coordsys == "proj":
+                grass.create_location(dbase = self.startpage.grassdatabase,
+                                      location = self.startpage.location,
+                                      proj4 = self.CreateProj4String(),
+                                      datum = self.datumtrans)
+            elif coordsys == 'custom':
+                grass.create_location(dbase = self.startpage.grassdatabase,
+                                      location = self.startpage.location,
+                                      proj4 = self.custompage.customstring)
+            elif coordsys == "epsg":
+                if not self.epsgpage.epsgcode:
+                    return _('EPSG code missing.')
+                
+                grass.create_location(dbase = self.startpage.grassdatabase,
+                                      location = self.startpage.location,
+                                      epsg = self.epsgpage.epsgcode,
+                                      datum = self.datumtrans)
+            elif coordsys == "file":
+                if not self.filepage.georeffile or \
+                        not os.path.isfile(self.filepage.georeffile):
+                    return _("File <%s> not found." % self.filepage.georeffile)
+                
+                grass.create_location(dbase = self.startpage.grassdatabase,
+                                      location = self.startpage.location,
+                                      filename = self.filepage.georeffile)
+            elif coordsys == "wkt":
+                if not self.wktpage.wktfile or \
+                        not os.path.isfile(self.wktpage.wktfile):
+                    return _("File <%s> not found." % self.wktpage.wktfile)
+                
+                grass.create_location(dbase = self.startpage.grassdatabase,
+                                      location = self.startpage.location,
+                                      filename = self.wktpage.wktfile)
         
-        return ''
-
+        except grass.ScriptException, e:
+            return str(e)
+        
+        return None
+    
     def CreateProj4String(self):
         """!Constract PROJ.4 string"""
         location = self.startpage.location
@@ -2060,117 +2033,7 @@
         proj4string = '%s +no_defs' % proj4string
         
         return proj4string
-        
-    def Proj4Create(self, proj4string):
-        """!Create a new location for selected projection
-        
-        @return error message (empty string on success)
-        """
-        ret, msg = gcmd.RunCommand('g.proj',
-                                   flags = 'c',
-                                   proj4 = proj4string,
-                                   location = self.startpage.location,
-                                   datumtrans = self.datumtrans,
-                                   getErrorMsg = True)
-        
-        if ret == 0:
-            return ''
 
-        return msg
-        
-
-    def CustomCreate(self):
-        """!Create a new location based on given proj4 string
-
-        @return error message (empty string on success)
-        """
-        proj4string = self.custompage.customstring
-        location = self.startpage.location
-        
-        ret, msg = gcmd.RunCommand('g.proj',
-                                   flags = 'c',
-                                   proj4 = proj4string,
-                                   location = location,
-                                   getErrorMsg = True)
-        
-        if ret == 0:
-            return ''
-        
-        return msg
-
-    def EPSGCreate(self):
-        """!Create a new location from an EPSG code.
-
-        @return error message (empty string on success)
-        """
-        epsgcode = self.epsgpage.epsgcode
-        epsgdesc = self.epsgpage.epsgdesc
-        location = self.startpage.location
-        
-        # should not happend
-        if epsgcode == '':
-            return _('EPSG code missing.')
-        
-        ret, msg = gcmd.RunCommand('g.proj',
-                                   flags = 'c',
-                                   epsg = epsgcode,
-                                   location = location,
-                                   datumtrans = self.datumtrans,
-                                   getErrorMsg = True)
-        
-        if ret == 0:
-            return ''
-
-        return msg
-
-    def FileCreate(self):
-        """!Create a new location from a georeferenced file
-
-        @return error message (empty string on success)
-        """
-        georeffile = self.filepage.georeffile
-        location = self.startpage.location
-        
-        # this should not happen
-        if not georeffile or not os.path.isfile(georeffile):
-            return _("File not found.")
-        
-        # creating location
-        ret, msg = gcmd.RunCommand('g.proj',
-                                   flags = 'c',
-                                   georef = georeffile,
-                                   location = location,
-                                   getErrorMsg = True)
-        
-        if ret == 0:
-            return ''
-        
-        return msg
-
-    def WKTCreate(self):
-        """!Create a new location from a WKT file
-        
-        @return error message (empty string on success)
-        """
-        wktfile = self.wktpage.wktfile
-        location = self.startpage.location
-        
-        # this should not happen
-        if not wktfile or not os.path.isfile(wktfile):
-            return _("File not found.")
-        
-        # creating location
-        ret, msg = gcmd.RunCommand('g.proj',
-                                   flags = 'c',
-                                   wkt = wktfile,
-                                   location = location,
-                                   getErrorMsg = True)
-        
-        if ret == 0:
-            return ''
-        
-        return msg
-
 class RegionDef(BaseClass, wx.Frame):
     """
     Page for setting default region extents and resolution
@@ -2295,7 +2158,7 @@
         #         self.ewres3 = float(region['ewres3'])
         self.tbres = float(region['tbres'])
         self.depth = int(region['depths'])
-        self.cells3 = int(region['3dcells'])
+        self.cells3 = int(region['cells3'])
         
         #
         # 3D box collapsable

Modified: grass/branches/develbranch_6/lib/python/core.py
===================================================================
--- grass/branches/develbranch_6/lib/python/core.py	2010-11-25 16:06:27 UTC (rev 44417)
+++ grass/branches/develbranch_6/lib/python/core.py	2010-11-25 16:09:12 UTC (rev 44418)
@@ -919,49 +919,87 @@
 
 # interface to `g.proj -c`
 
-def create_location(location, epsg = None, proj4 = None, filename = None, wkt = None, datum = None):
+def create_location(dbase, location,
+                    epsg = None, proj4 = None, filename = None, wkt = None,
+                    datum = None):
     """!Create new location
 
+    Raise ScriptException on error.
+    
+    @param dbase path to GRASS database
     @param location location name to create
     @param epgs if given create new location based on EPSG code
     @param proj4 if given create new location based on Proj4 definition
     @param filename if given create new location based on georeferenced file
     @param wkt if given create new location based on WKT definition (path to PRJ file)
     @param datum datum transformation parameters (used for epsg and proj4)
+    """
+    gisdbase = None
+    if epsg or proj4 or filename or wkt:
+        gisdbase = gisenv()['GISDBASE']
+        run_command('g.gisenv',
+                    set = 'GISDBASE=%s' % dbase)
+    if not os.path.exists(dbase):
+            os.mkdir(dbase)
 
-    @return True on success
-    @return False on failure
-    """
+    kwargs = dict()
+    if datum:
+        kwargs['datum'] = datum
+    
     if epsg:
-        ret = run_command('g.proj',
+        ps = pipe_command('g.proj',
+                          quiet = True,
                           flags = 'c',
                           epsg = epsg,
                           location = location,
-                          datumtrans = datum)
+                          stderr = PIPE,
+                         **kwargs)
     elif proj4:
-        ret = run_command('g.proj',
+        ps = pipe_command('g.proj',
+                          quiet = True,
                           flags = 'c',
                           proj4 = proj4,
                           location = location,
-                          datumtrans = datum)
+                          stderr = PIPE,
+                          **kwargs)
     elif filename:
-        ret = run_command('g.proj',
+        ps = pipe_command('g.proj',
+                          quiet = True,
                           flags = 'c',
                           georef = filename,
-                          location = location)
+                          location = location,
+                          stderr = PIPE)
     elif wkt:
-        ret = run_command('g.proj',
+        ps = pipe_command('g.proj',
+                          quiet = True,
                           flags = 'c',
                           wkt = wktfile,
-                          location = location)
+                          location = location,
+                          stderr = PIPE)
     else:
-        ret = _create_location_xy(location)
-        
-def _create_location_xy(location):
-    """!Create unprojected location"""
+        return _create_location_xy(dbase, location)
+
+    error = ps.communicate()[1]
+    run_command('g.gisenv',
+                set = 'GISDBASE=%s' % gisdbase)
+    
+    if ps.returncode != 0 and error:
+        raise ScriptException(repr(error))
+    
+def _create_location_xy(database, location):
+    """!Create unprojected location
+
+    Raise ScriptException on error.
+    
+    @param database GRASS database where to create new location
+    @param location location name
+    """
+    cur_dir = os.getcwd()
     try:
+        os.chdir(database)
         os.mkdir(location)
         os.mkdir(os.path.join(location, 'PERMANENT'))
+        
         # create DEFAULT_WIND and WIND files
         regioninfo = ['proj:       0',
                       'zone:       0',
@@ -996,11 +1034,11 @@
                                    "MYNAME"), 'w')
         myname.write('%s' % os.linesep)
         myname.close()
-    except OSError:
-        return 1
+        
+        os.chdir(cur_dir)
+    except OSError, e:
+        raise ScriptException(repr(e))
     
-    return 0
-
 # get debug_level
 if find_program('g.gisenv', ['--help']):
     debug_level = int(gisenv().get('DEBUG', 0))



More information about the grass-commit mailing list