소스 검색

v.import: use v.in.ogr -j to compare projections, use CalledModuleError

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@65915 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 9 년 전
부모
커밋
3793644911
1개의 변경된 파일26개의 추가작업 그리고 30개의 파일을 삭제
  1. 26 30
      scripts/v.import/v.import.py

+ 26 - 30
scripts/v.import/v.import.py

@@ -76,7 +76,7 @@ import atexit
 import math
 
 import grass.script as grass
-
+from grass.exceptions import CalledModuleError
     
 def cleanup():
     # remove temp location
@@ -133,36 +133,32 @@ def main():
     tgtsrs = grass.read_command('g.proj', flags = 'j', quiet = True)
 
     # create temp location from input without import
-    grass.message(_("Creating temporary location for <%s>...") % OGRdatasource)
+    grass.verbose(_("Creating temporary location for <%s>...") % OGRdatasource)
     if layers:
         vopts['layer'] = layers
     if output:
         vopts['output'] = output
-    returncode = grass.run_command('v.in.ogr', input = OGRdatasource,
-                                   location = tmploc, flags = 'i', quiet = True, **vopts)
-    # if it fails, return
-    if returncode != 0:
+    try:
+        grass.run_command('v.in.ogr', input = OGRdatasource,
+                          location = tmploc, flags = 'i', quiet = True, **vopts)
+    except CalledModuleError:
         grass.fatal(_("Unable to create location from OGR datasource <%s>") % OGRdatasource)
 
     # switch to temp location
     os.environ['GISRC'] = str(srcgisrc)
 
-    # compare source and target srs
-    insrs = grass.read_command('g.proj', flags = 'j', quiet = True)
-
     # switch to target location
     os.environ['GISRC'] = str(tgtgisrc)
 
-    if insrs == tgtsrs:
-        # try v.in.ogr directly
-        grass.message(_("Importing <%s>...") % OGRdatasource) 
-        returncode = grass.run_command('v.in.ogr', input = OGRdatasource,
-                                       flags = vflags, **vopts)
-        # if it succeeds, return
-        if returncode == 0:
+    # try v.in.ogr directly
+    if grass.run_command('v.in.ogr', input=OGRdatasource, flags='j',
+                         errors='status', quiet=True) == 0:
+        try:
+            grass.run_command('v.in.ogr', input = OGRdatasource,
+                              flags = vflags, **vopts)
             grass.message(_("Input <%s> successfully imported without reprojection") % OGRdatasource) 
             return 0
-        else:
+        except CalledModuleError as e:
             grass.fatal(_("Unable to import <%s>") % OGRdatasource)
     
     # make sure target is not xy
@@ -187,10 +183,10 @@ def main():
         # reproject to src
         # switch to temp location
         os.environ['GISRC'] = str(srcgisrc)
-        returncode = grass.run_command('v.proj', input = vreg, output = vreg, 
-                                       location = tgtloc, mapset = tgtmapset, quiet = True)
-        
-        if returncode != 0:
+        try:
+            grass.run_command('v.proj', input = vreg, output = vreg, 
+                              location = tgtloc, mapset = tgtmapset, quiet = True)
+        except CalledModuleError:
             grass.fatal(_("Unable to reproject to source location"))
         
         # set region from region vector
@@ -200,11 +196,10 @@ def main():
 
     # import into temp location
     grass.message(_("Importing <%s> ...") % OGRdatasource)
-    returncode = grass.run_command('v.in.ogr', input = OGRdatasource,
-                                   flags = vflags, **vopts)
-    
-    # if it fails, return
-    if returncode != 0:
+    try:
+        grass.run_command('v.in.ogr', input = OGRdatasource,
+                          flags = vflags, **vopts)
+    except CalledModuleError:
         grass.fatal(_("Unable to import OGR datasource <%s>") % OGRdatasource)
 
     # if output is not define check source mapset
@@ -225,10 +220,11 @@ def main():
 
     # v.proj
     grass.message(_("Reprojecting <%s>...") % output)
-    returncode = grass.run_command('v.proj', location = tmploc,
-                                   mapset = 'PERMANENT', input = output,
-                                   quiet = True)
-    if returncode != 0:
+    try:
+        grass.run_command('v.proj', location = tmploc,
+                          mapset = 'PERMANENT', input = output,
+                          quiet = True)
+    except CalledModuleError:
         grass.fatal(_("Unable to to reproject vector <%s>") % output)
     
     return 0