Browse Source

pythonlib: add overwrite argument to create_location()

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@57523 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 11 years ago
parent
commit
08e6c10bdb
1 changed files with 12 additions and 1 deletions
  1. 12 1
      lib/python/script/core.py

+ 12 - 1
lib/python/script/core.py

@@ -1331,7 +1331,7 @@ def mapsets(search_path=False):
 
 
 def create_location(dbase, location, epsg=None, proj4=None, filename=None,
-                    wkt=None, datum=None, datum_trans=None, desc=None):
+                    wkt=None, datum=None, datum_trans=None, desc=None, overwrite=False):
     """!Create new location
 
     Raise ScriptError on error.
@@ -1345,15 +1345,26 @@ def create_location(dbase, location, epsg=None, proj4=None, filename=None,
     @param datum GRASS format datum code
     @param datum_trans datum transformation parameters (used for epsg and proj4)
     @param desc description of the location (creates MYNAME file)
+    @param overwrite True to overwrite location if exists (WARNING: ALL DATA from existing location ARE DELETED!)
     """
     gisdbase = None
     if epsg or proj4 or filename or wkt:
         # FIXME: changing GISDBASE mid-session is not background-job safe
         gisdbase = gisenv()['GISDBASE']
         run_command('g.gisenv', set='GISDBASE=%s' % dbase)
+    # create dbase if not exists
     if not os.path.exists(dbase):
             os.mkdir(dbase)
 
+    # check if location already exists
+    if os.path.exists(os.path.join(dbase, location)):
+        if not overwrite:
+            warning(_("Location <%s> already exists. Operation canceled.") % location)
+            return
+        else:
+            warning(_("Location <%s> already exists and will be overwritten") % location)
+            shutil.rmtree(os.path.join(dbase, location))
+    
     kwargs = dict()
     if datum:
         kwargs['datum'] = datum