소스 검색

r.fillnulls: Do not fail hard if input map has no holes.

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@73484 15284696-431f-4ddb-bdfa-cd5b030d7da7
Maris Nartiss 6 년 전
부모
커밋
5421800295
1개의 변경된 파일46개의 추가작업 그리고 8개의 파일을 삭제
  1. 46 8
      scripts/r.fillnulls/r.fillnulls.py

+ 46 - 8
scripts/r.fillnulls/r.fillnulls.py

@@ -102,6 +102,7 @@
 import sys
 import os
 import atexit
+import subprocess
 
 import grass.script as grass
 from grass.exceptions import CalledModuleError
@@ -446,16 +447,52 @@ def main():
         reg = grass.region()
         # launch r.resamp.bspline
         tmp_rmaps.append(prefix + 'filled')
+        # If there are no NULL cells, r.resamp.bslpine call
+        # will end with an error although for our needs it's fine
+        # Only problem - this state must be read from stderr
+        new_env = dict(os.environ)
+        new_env['LC_ALL'] = 'C'
         if usermask:
-            grass.run_command('r.resamp.bspline', input=input, mask=usermask,
-                              output=prefix + 'filled', method=method,
-                              ew_step=3 * reg['ewres'], ns_step=3 * reg['nsres'],
-                              lambda_=lambda_, flags='n')
+            try:
+                p = grass.core.start_command(
+                    'r.resamp.bspline',
+                    input=input,
+                    mask=usermask,
+                    output=prefix + 'filled',
+                    method=method,
+                    ew_step=3 * reg['ewres'],
+                    ns_step=3 * reg['nsres'],
+                    lambda_=lambda_,
+                    flags='n',
+                    stderr=subprocess.PIPE,
+                    env=new_env)
+                stdout, stderr = p.communicate()
+                if "No NULL cells found" in stderr:
+                    grass.run_command('g.copy', raster='%s,%sfilled' % (input, prefix), overwrite=True)
+                    p.returncode = 0
+                    grass.warning(_("Input map <%s> has no holes. Copying to output without modification.") % (input,))
+            except CalledModuleError as e:
+                grass.fatal(_("Failure during bspline interpolation. Error message: %s") % stderr)
         else:
-            grass.run_command('r.resamp.bspline', input=input,
-                              output=prefix + 'filled', method=method,
-                              ew_step=3 * reg['ewres'], ns_step=3 * reg['nsres'],
-                              lambda_=lambda_, flags='n')
+            try:
+                p = grass.core.start_command(
+                    'r.resamp.bspline',
+                    input=input,
+                    output=prefix + 'filled',
+                    method=method,
+                    ew_step=3 * reg['ewres'],
+                    ns_step=3 * reg['nsres'],
+                    lambda_=lambda_,
+                    flags='n',
+                    stderr=subprocess.PIPE,
+                    env=new_env)
+                stdout, stderr = p.communicate()
+                if "No NULL cells found" in stderr:
+                    grass.run_command('g.copy', raster='%s,%sfilled' % (input, prefix), overwrite=True)
+                    p.returncode = 0
+                    grass.warning(_("Input map <%s> has no holes. Copying to output without modification.") % (input,))
+            except CalledModuleError as e:
+                grass.fatal(_("Failure during bspline interpolation. Error message: %s") % stderr)
 
     # restoring user's mask, if present:
     if usermask:
@@ -495,6 +532,7 @@ def main():
 
     grass.message(_("Done."))
 
+
 if __name__ == "__main__":
     options, flags = grass.parser()
     atexit.register(cleanup)