Bläddra i källkod

unpacking maps: show projection file difference

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@57969 15284696-431f-4ddb-bdfa-cd5b030d7da7
Anna Petrášová 11 år sedan
förälder
incheckning
6faf715ed7

+ 17 - 0
lib/python/script/core.py

@@ -866,6 +866,23 @@ def compare_key_value_text_files(filename_a, filename_b, sep=":",
                                            'a': len(dict_a)})
     return True
 
+def diff_files(filename_a, filename_b):
+    """!Diffs two text files and returns difference.
+
+    @param filename_a first file path    
+    @param filename_b second file path
+    
+    @return list of strings
+    """
+    import difflib
+    differ = difflib.Differ()
+    fh_a = open(filename_a, 'r')
+    fh_b = open(filename_b, 'r')
+    result = list(differ.compare(fh_a.readlines(),
+                                 fh_b.readlines()))
+    return result
+
+
 # interface to g.gisenv
 
 

+ 3 - 0
lib/python/temporal/stds_import.py

@@ -230,6 +230,9 @@ def import_stds(input, output, extrdir, title=None, descr=None, location=None,
                 core.warning(_("Projection information does not match. "
                                "Proceeding..."))
             else:
+                diff = ''.join(core.diff_files(temp_name, proj_name))
+                core.warning(_("Difference between PROJ_INFO file of imported map "
+                               "and of current location:\n{diff}").format(diff=diff))
                 core.fatal(_("Projection information does not match. Aborting."))
 
     # Create a new location based on the projection information and switch

+ 18 - 2
scripts/r.unpack/r.unpack.py

@@ -88,11 +88,27 @@ def main():
     os.chdir(data_name)
     
     # check projection compatibility in a rather crappy way
-    if not grass.compare_key_value_text_files('PROJ_INFO', os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')) or \
-            not grass.compare_key_value_text_files('PROJ_UNITS', os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_UNITS')):
+    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):
+        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):
+        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..."))
         else:
+            if diff_result_1:
+                grass.warning(_("Difference between PROJ_INFO file of packed map "
+                                "and of current location:\n{diff}").format(diff=''.join(diff_result_1)))
+            if diff_result_2:
+                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

+ 16 - 3
scripts/v.unpack/v.unpack.py

@@ -98,13 +98,26 @@ def main():
     # 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')
-    if not grass.compare_key_value_text_files(os.path.join(tmp_dir,'PROJ_INFO'), loc_proj) or \
-       not grass.compare_key_value_text_files(os.path.join(tmp_dir,'PROJ_UNITS'), loc_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(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 diff_result_1 or diff_result_2:
         if flags['o']:
             grass.warning(_("Projection information does not match. Proceeding..."))
         else:
+            if diff_result_1:
+                grass.warning(_("Difference between PROJ_INFO file of packed map "
+                                "and of current location:\n{diff}").format(diff=''.join(diff_result_1)))
+            if diff_result_2:
+                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."))
-    
+
     # new db
     fromdb = os.path.join(tmp_dir, 'db.sqlite')
     # copy file