[GRASS-SVN] r59147 - in grass/trunk: lib/python/script scripts/r.unpack scripts/v.unpack

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Feb 27 02:28:18 PST 2014


Author: lucadelu
Date: 2014-02-27 02:28:18 -0800 (Thu, 27 Feb 2014)
New Revision: 59147

Modified:
   grass/trunk/lib/python/script/core.py
   grass/trunk/scripts/r.unpack/r.unpack.py
   grass/trunk/scripts/v.unpack/v.unpack.py
Log:
r.unpack/v.unpack: fix problems for duplicate names in proj info and units, there is still a problem if second file has more keys, eg south:defined; PEP8 cleaning

Modified: grass/trunk/lib/python/script/core.py
===================================================================
--- grass/trunk/lib/python/script/core.py	2014-02-27 08:09:54 UTC (rev 59146)
+++ grass/trunk/lib/python/script/core.py	2014-02-27 10:28:18 UTC (rev 59147)
@@ -757,8 +757,54 @@
     return result
 
 
-def _text_to_key_value_dict(filename, sep=":", val_sep=","):
+def _compare_projection(dic):
     """
+        !Check if projection has some possibility of duplicate names like
+        Universal Transverse Mercator and Universe Transverse Mercator and
+        unify them
+
+        @param dic The dictionary containing information about projection
+
+        @return The dictionary with the new values if needed
+
+    """
+    # the lookup variable is a list of list, each list contains all the
+    # possible name for a projection system
+    lookup = [['Universal Transverse Mercator', 'Universe Transverse Mercator']]
+    for lo in lookup:
+        for n in range(len(dic['name'])):
+            if dic['name'][n] in lo:
+                dic['name'][n] = lo[0]
+    return dic
+
+
+def _compare_units(dic):
+    """
+        !Check if units has some possibility of duplicate names like
+        meter and metre and unify them
+
+        @param dic The dictionary containing information about units
+
+        @return The dictionary with the new values if needed
+
+    """
+    # the lookup variable is a list of list, each list contains all the
+    # possible name for a units
+    lookup = [['meter', 'metre'], ['meters', 'metres'], ['kilometer',
+              'kilometre'], ['kilometers', 'kilometres']]
+    for l in lookup:
+        for n in range(len(dic['unit'])):
+            if dic['unit'][n] in l:
+                dic['unit'][n] = l[0]
+        for n in range(len(dic['units'])):
+            if dic['units'][n] in l:
+                dic['units'][n] = l[0]
+    return dic
+
+
+def _text_to_key_value_dict(filename, sep=":", val_sep=",", checkproj=False,
+                            checkunits=False):
+    """
         !Convert a key-value text file, where entries are separated
         by newlines and the key and value are separated by `sep',
         into a key-value dictionary and discover/use the correct
@@ -767,6 +813,9 @@
         @param filename The name or name and path of the text file to convert
         @param sep The character that separates the keys and values, default is ":"
         @param val_sep The character that separates the values of a single key, default is ","
+        @param checkproj True if it has to check some information about projection system
+        @param checkproj True if it has to check some information about units
+        
         @return The dictionary
 
         A text file with this content:
@@ -819,11 +868,16 @@
             value_list.append(value_converted)
 
         kvdict[key] = value_list
+    if checkproj:
+        kvdict = _compare_projection(kvdict)
+    if checkunits:
+        kvdict = _compare_units(kvdict)
     return kvdict
 
 
 def compare_key_value_text_files(filename_a, filename_b, sep=":",
-                                 val_sep=",", precision=0.000001):
+                                 val_sep=",", precision=0.000001,
+                                 proj=False, units=False):
     """
     !Compare two key-value text files
 
@@ -845,11 +899,15 @@
     @param sep character that separates the keys and values, default is ":"
     @param val_sep character that separates the values of a single key, default is ","
     @param precision precision with which the floating point values are compared
-
+    @param proj True if it has to check some information about projection system
+    @param units True if it has to check some information about units
+    
     @return True if full or almost identical, False if different
     """
-    dict_a = _text_to_key_value_dict(filename_a, sep)
-    dict_b = _text_to_key_value_dict(filename_b, sep)
+    dict_a = _text_to_key_value_dict(filename_a, sep, checkproj=proj,
+                                     checkunits=units)
+    dict_b = _text_to_key_value_dict(filename_b, sep, checkproj=proj,
+                                     checkunits=units)
 
     missing_keys = 0
 

Modified: grass/trunk/scripts/r.unpack/r.unpack.py
===================================================================
--- grass/trunk/scripts/r.unpack/r.unpack.py	2014-02-27 08:09:54 UTC (rev 59146)
+++ grass/trunk/scripts/r.unpack/r.unpack.py	2014-02-27 10:28:18 UTC (rev 59147)
@@ -40,23 +40,24 @@
 import shutil
 import tarfile
 import atexit
-import filecmp
 
 from grass.script import core as grass
 
+
 def cleanup():
     grass.try_rmdir(tmp_dir)
 
+
 def main():
     infile = options['input']
-    
+
     global tmp_dir
     tmp_dir = grass.tempdir()
     grass.debug('tmp_dir = %s' % tmp_dir)
-    
+
     if not os.path.exists(infile):
         grass.fatal(_("File <%s> not found") % infile)
-    
+
     gisenv = grass.gisenv()
     mset_dir = os.path.join(gisenv['GISDBASE'],
                             gisenv['LOCATION_NAME'],
@@ -64,41 +65,44 @@
     input_base = os.path.basename(infile)
     shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
     os.chdir(tmp_dir)
-    tar = tarfile.TarFile.open(name = input_base, mode = 'r')
+    tar = tarfile.TarFile.open(name=input_base, mode='r')
     try:
         data_name = tar.getnames()[0]
     except:
         grass.fatal(_("Pack file unreadable"))
-    
+
     if options['output']:
         map_name = options['output']
     else:
         map_name = data_name.split('@')[0]
-    
-    gfile = grass.find_file(name = map_name, element = 'cell',
-                            mapset = '.')
+
+    gfile = grass.find_file(name=map_name, element='cell', mapset='.')
     if gfile['file']:
         if os.environ.get('GRASS_OVERWRITE', '0') != '1':
             grass.fatal(_("Raster map <%s> already exists") % map_name)
         else:
             grass.warning(_("Raster map <%s> already exists and will be overwritten") % map_name)
-    
+
     # extract data
     tar.extractall()
     os.chdir(data_name)
-    
+
     # check projection compatibility in a rather crappy way
     diff_result_1 = diff_result_2 = None
     proj_info_file_1 = 'PROJ_INFO'
     proj_info_file_2 = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')
-    if not grass.compare_key_value_text_files(proj_info_file_1, proj_info_file_2):
+    if not grass.compare_key_value_text_files(filename_a=proj_info_file_1,
+                                              filename_b=proj_info_file_2,
+                                              proj=True):
         diff_result_1 = grass.diff_files(proj_info_file_1, proj_info_file_2)
 
     proj_units_file_1 = 'PROJ_UNITS'
     proj_units_file_2 = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_UNITS')
-    if not grass.compare_key_value_text_files(proj_units_file_1, proj_units_file_2):
+    if not grass.compare_key_value_text_files(filename_a=proj_units_file_1,
+                                              filename_b=proj_units_file_2,
+                                              units=True):
         diff_result_2 = grass.diff_files(proj_units_file_1, proj_units_file_2)
-    
+
     if diff_result_1 or diff_result_2:
         if flags['o']:
             grass.warning(_("Projection information does not match. Proceeding..."))
@@ -110,7 +114,7 @@
                 grass.warning(_("Difference between PROJ_UNITS file of packed map "
                                 "and of current location:\n{diff}").format(diff=''.join(diff_result_2)))
             grass.fatal(_("Projection information does not match. Aborting."))
-    
+
     # install in $MAPSET
     for element in ['cats', 'cell', 'cellhd', 'cell_misc', 'colr', 'fcell', 'hist']:
         if not os.path.exists(element):
@@ -125,9 +129,9 @@
             shutil.copytree('cell_misc', path)
         else:
             shutil.copyfile(element, os.path.join(mset_dir, element, map_name))
-    
+
     grass.message(_("Raster map <%s> unpacked") % map_name)
-    
+
 if __name__ == "__main__":
     options, flags = grass.parser()
     atexit.register(cleanup)

Modified: grass/trunk/scripts/v.unpack/v.unpack.py
===================================================================
--- grass/trunk/scripts/v.unpack/v.unpack.py	2014-02-27 08:09:54 UTC (rev 59146)
+++ grass/trunk/scripts/v.unpack/v.unpack.py	2014-02-27 10:28:18 UTC (rev 59147)
@@ -38,74 +38,79 @@
 import shutil
 import tarfile
 import atexit
-import filecmp
 
 from grass.script import core as grass
 from grass.script import db as grassdb
 
+
 def cleanup():
     grass.try_rmdir(tmp_dir)
 
+
 def main():
     infile = options['input']
-    
+
     # create temporary directory
     global tmp_dir
     tmp_dir = grass.tempdir()
     grass.debug('tmp_dir = %s' % tmp_dir)
-    
+
     # check if the input file exists
     if not os.path.exists(infile):
         grass.fatal(_("File <%s> not found") % infile)
-    
+
     # copy the files to tmp dir
     input_base = os.path.basename(infile)
     shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
     os.chdir(tmp_dir)
-    tar = tarfile.TarFile.open(name = input_base, mode = 'r')
+    tar = tarfile.TarFile.open(name=input_base, mode='r')
     try:
         data_name = tar.getnames()[0]
     except:
         grass.fatal(_("Pack file unreadable"))
-    
+
     # set the output name
     if options['output']:
         map_name = options['output']
     else:
         map_name = data_name
-    
+
     # grass env
     gisenv = grass.gisenv()
     mset_dir = os.path.join(gisenv['GISDBASE'],
                             gisenv['LOCATION_NAME'],
                             gisenv['MAPSET'])
-    
+
     new_dir = os.path.join(mset_dir, 'vector', map_name)
-    
-    gfile = grass.find_file(name = map_name, element = 'vector',
-                            mapset = '.')
+
+    gfile = grass.find_file(name=map_name, element='vector', mapset='.')
     overwrite = os.getenv('GRASS_OVERWRITE')
     if gfile['file'] and overwrite != '1':
         grass.fatal(_("Vector map <%s> already exists") % map_name)
     elif overwrite == '1' and gfile['file']:
         grass.warning(_("Vector map <%s> already exists and will be overwritten") % map_name)
-        grass.run_command('g.remove', quiet = True, vect = map_name)
-        shutil.rmtree(new_dir,True)
-    
+        grass.run_command('g.remove', quiet=True, vect=map_name)
+        shutil.rmtree(new_dir, True)
+
     # extract data
     tar.extractall()
-    
+
     # check projection compatibility in a rather crappy way
     loc_proj = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')
     loc_proj_units = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_UNITS')
-    
+
     diff_result_1 = diff_result_2 = None
-    if not grass.compare_key_value_text_files(os.path.join(tmp_dir,'PROJ_INFO'), loc_proj):
-        diff_result_1 = grass.diff_files(os.path.join(tmp_dir,'PROJ_INFO'), loc_proj)
+    if not grass.compare_key_value_text_files(filename_a=os.path.join(tmp_dir,'PROJ_INFO'),
+                                              filename_b=loc_proj, proj=True):
+        diff_result_1 = grass.diff_files(os.path.join(tmp_dir, 'PROJ_INFO'),
+                                         loc_proj)
 
-    if not grass.compare_key_value_text_files(os.path.join(tmp_dir,'PROJ_UNITS'), loc_proj_units):
-        diff_result_2 = grass.diff_files(os.path.join(tmp_dir,'PROJ_UNITS'), loc_proj_units)
-    
+    if not grass.compare_key_value_text_files(filename_a=os.path.join(tmp_dir,'PROJ_UNITS'),
+                                              filename_b=loc_proj_units,
+                                              units=True):
+        diff_result_2 = grass.diff_files(os.path.join(tmp_dir, 'PROJ_UNITS'),
+                                         loc_proj_units)
+
     if diff_result_1 or diff_result_2:
         if flags['o']:
             grass.warning(_("Projection information does not match. Proceeding..."))
@@ -128,18 +133,18 @@
         dbconn = grassdb.db_connection()
         todb = dbconn['database']
         # return all tables
-        list_fromtable = grass.read_command('db.tables', driver = 'sqlite',
-                                            database = fromdb).splitlines()
-        
+        list_fromtable = grass.read_command('db.tables', driver='sqlite',
+                                            database=fromdb).splitlines()
+
         # return the list of old connection for extract layer number and key
-        dbln = open(os.path.join(new_dir,'dbln'), 'r')
+        dbln = open(os.path.join(new_dir, 'dbln'), 'r')
         dbnlist = dbln.readlines()
         dbln.close()
         # check if dbf or sqlite directory exists
         if dbconn['driver'] == 'dbf' and not os.path.exists(os.path.join(mset_dir, 'dbf')):
-	    os.mkdir(os.path.join(mset_dir, 'dbf'))
+            os.mkdir(os.path.join(mset_dir, 'dbf'))
 	elif dbconn['driver'] == 'sqlite' and not os.path.exists(os.path.join(mset_dir, 'sqlite')):
-	    os.mkdir(os.path.join(mset_dir, 'sqlite'))
+            os.mkdir(os.path.join(mset_dir, 'sqlite'))
         # for each old connection
         for t in dbnlist:
             # it split the line of each connection, to found layer number and key
@@ -147,7 +152,7 @@
                 values = t.split('|')
             else:
                 values = t.split(' ')
-            
+
             from_table = values[1]
             layer = values[0].split('/')[0]
             # we need to take care about the table name in case of several layer
@@ -158,30 +163,32 @@
                     to_table = map_name
             else:
                 to_table = from_table
-            
-            grass.verbose(_("Coping table <%s> as table <%s>") % (from_table, to_table))
-            
+
+            grass.verbose(_("Coping table <%s> as table <%s>") % (from_table,
+                                                                  to_table))
+
             # copy the table in the default database
-            if 0 != grass.run_command('db.copy', to_driver = dbconn['driver'], 
-                                      to_database = todb, to_table = to_table, 
-                                      from_driver = 'sqlite', from_database = fromdb,
-                                      from_table = from_table):
+            if 0 != grass.run_command('db.copy', to_driver=dbconn['driver'],
+                                      to_database=todb, to_table=to_table,
+                                      from_driver='sqlite',
+                                      from_database=fromdb,
+                                      from_table=from_table):
                 grass.fatal(_("Unable to copy table <%s> as table <%s>") % (from_table, to_table))
-                
+
             grass.verbose(_("Connect table <%s> to vector map <%s> at layer <%s>") % \
                               (to_table, map_name, layer))
-            
+
             # and connect the new tables with the right layer
-            if 0 != grass.run_command('v.db.connect', flags = 'o', quiet = True,
-                                      driver = dbconn['driver'], database = todb, 
-                                      map =  map_name, key = values[2],
-                                      layer = layer, table = to_table):
+            if 0 != grass.run_command('v.db.connect', flags='o', quiet=True,
+                                      driver=dbconn['driver'], database=todb,
+                                      map=map_name, key=values[2],
+                                      layer=layer, table=to_table):
                 grass.fatal(_("Unable to connect table <%s> to vector map <%s>") % \
                                 (to_table, map_name))
-    
+
     grass.message(_("Vector map <%s> succesfully unpacked") % map_name)
 
 if __name__ == "__main__":
-  options, flags = grass.parser()
-  atexit.register(cleanup)
-  sys.exit(main())
+    options, flags = grass.parser()
+    atexit.register(cleanup)
+    sys.exit(main())



More information about the grass-commit mailing list