[GRASS-SVN] r65915 - grass/trunk/scripts/v.import

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Aug 12 05:14:47 PDT 2015


Author: martinl
Date: 2015-08-12 05:14:47 -0700 (Wed, 12 Aug 2015)
New Revision: 65915

Modified:
   grass/trunk/scripts/v.import/v.import.py
Log:
v.import: use v.in.ogr -j to compare projections, use CalledModuleError

Modified: grass/trunk/scripts/v.import/v.import.py
===================================================================
--- grass/trunk/scripts/v.import/v.import.py	2015-08-12 12:13:54 UTC (rev 65914)
+++ grass/trunk/scripts/v.import/v.import.py	2015-08-12 12:14:47 UTC (rev 65915)
@@ -76,7 +76,7 @@
 import math
 
 import grass.script as grass
-
+from grass.exceptions import CalledModuleError
     
 def cleanup():
     # remove temp location
@@ -133,36 +133,32 @@
     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 @@
         # 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 @@
 
     # 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 @@
 
     # 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



More information about the grass-commit mailing list