[GRASS-SVN] r65642 - grass-addons/grass7/vector/v.in.proj

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Jul 19 10:09:56 PDT 2015


Author: martinl
Date: 2015-07-19 10:09:56 -0700 (Sun, 19 Jul 2015)
New Revision: 65642

Modified:
   grass-addons/grass7/vector/v.in.proj/v.in.proj.py
Log:
v.in.proj: clean up, replace input_file/input_directory with input

Modified: grass-addons/grass7/vector/v.in.proj/v.in.proj.py
===================================================================
--- grass-addons/grass7/vector/v.in.proj/v.in.proj.py	2015-07-19 17:06:08 UTC (rev 65641)
+++ grass-addons/grass7/vector/v.in.proj/v.in.proj.py	2015-07-19 17:09:56 UTC (rev 65642)
@@ -6,9 +6,9 @@
 #
 # AUTHOR(S):    Markus Metz
 #
-# PURPOSE:      Import and reproject on the fly
+# PURPOSE:      Import and reproject vector data on the fly
 #
-# COPYRIGHT:    (C) 2015 GRASS development team
+# COPYRIGHT:    (C) 2015 by GRASS development team
 #
 #               This program is free software under the GNU General
 #               Public License (>=v2). Read the file COPYING that
@@ -17,46 +17,35 @@
 #############################################################################
 
 #%module
-#% description: Import vector data using OGR library and reproject on the fly.
+#% description: Imports vector data into a GRASS vector map using OGR library and reproject on the fly.
 #% keyword: vector
 #% keyword: import
 #% keyword: projection
 #%end
-#%option G_OPT_F_BIN_INPUT
-#% key: input_file
-#% required: no
-#% multiple: no
-#% description: Name of OGR datasource (file) to be imported
-#% guisection: Input
+#%option 
+#% key: input
+#% type: string
+#% required: yes
+#% description: Name of OGR datasource to be imported
+#% gisprompt: old,datasource,datasource
 #%end
-#%option G_OPT_M_DIR
-#% key: input_directory
-#% required: no
-#% multiple: no
-#% description: Name of OGR datasource (directory) to be imported
-#% guisection: Input
-#%end
 #%option
 #% key: layer
 #% type: string
-#% required: no
 #% multiple: yes
 #% description: OGR layer name. If not given, all available layers are imported
-#% guisection: Input
+#% guisection: Layer
+#% gisprompt: old,datasource_layer,datasource_layer
 #%end
 #%option G_OPT_V_OUTPUT
-#% required: yes
-#% multiple: no
-#% key_desc: name
-#% description: Name for output vector map
+#% description: Name for output vector map (default: input)
 #% guisection: Output
 #%end
 #%option
 #% key: extents
 #% type: string
 #% required: yes
-#% multiple: no
-#% options: region,input
+#% options: input,region
 #% description: Ouput vector map extents
 #% descriptions: region;extents of current region;input;extents of input map
 #% guisection: Output
@@ -75,20 +64,17 @@
 def cleanup():
     # remove temp location
     if tmploc:
-        path = os.path.join(gisdbase, tmploc)
-        grass.try_rmdir(path)
+        grass.try_rmdir(os.path.join(gisdbase, tmploc))
     if srcgisrc:
         grass.try_remove(srcgisrc)
 
 def main():
     global tmploc, srcgisrc, gisdbase
 
+    OGRdatasource = options['input']
     output = options['output']
-
-    layers = None
-    if options['layer']:
-        layers = options['layer']
-
+    layers = options['layer']
+    
     # initialize global vars
     tmploc = None
     srcgisrc = None
@@ -98,16 +84,6 @@
     if options['extents'] == 'region':
         vflags = 'r'
 
-    OGRdatasource = None
-    if options['input_file']:
-        OGRdatasource = options['input_file']
-    
-    if not OGRdatasource:
-        OGRdatasource = options['input_directory']
-
-    if not OGRdatasource:
-        grass.fatal(_("Either option 'input_file' or option 'input_directory' must be given"))
-
     grassenv = grass.gisenv()
     tgtloc = grassenv['LOCATION_NAME']
     tgtmapset = grassenv['MAPSET']
@@ -118,7 +94,6 @@
     tmploc = 'temp_import_location_' + str(os.getpid())
 
     f = open(srcgisrc, 'w')
-    f.write('DEBUG: 0\n')
     f.write('MAPSET: PERMANENT\n')
     f.write('GISDBASE: %s\n' % gisdbase)
     f.write('LOCATION_NAME: %s\n' % tmploc);
@@ -129,15 +104,12 @@
 
     # create temp location from input without import
     grass.message(_("Creating temporary location for <%s>...") % OGRdatasource) 
-    ps = grass.start_command('v.in.ogr', input = OGRdatasource,
-                             layer = layers, output = output,
-                             location = tmploc, flags = 'i', quiet = True)
-    returncode = ps.wait()
-    
+    returncode = grass.run_command('v.in.ogr', input = OGRdatasource,
+                                   layer = layers, output = output,
+                                   location = tmploc, flags = 'i', quiet = True)
     # if it fails, return
     if returncode != 0:
         grass.fatal(_("Unable to create location from OGR datasource <%s>") % OGRdatasource)
-        sys.exit(1)
 
     # switch to temp location
     os.environ['GISRC'] = str(srcgisrc)
@@ -151,23 +123,19 @@
     if insrs == tgtsrs:
         # try v.in.ogr directly
         grass.message(_("Importing <%s>...") % OGRdatasource) 
-        ps = grass.start_command('v.in.ogr', input = OGRdatasource,
-                                layer = layers, output = output,
-                                flags = vflags)
-        returncode = ps.wait()
-        
+        returncode = grass.run_command('v.in.ogr', input = OGRdatasource,
+                                       layer = layers, output = output,
+                                       flags = vflags)
         # if it succeeds, return
         if returncode == 0:
             grass.message(_("Input <%s> successfully imported without reprojection") % OGRdatasource) 
-            sys.exit(0)
+            return 0
         else:
             grass.fatal(_("Unable to import <%s>") % OGRdatasource)
-            sys.exit(1)
-
+    
     # make sure target is not xy
     if grass.parse_command('g.proj', flags = 'g')['name'] == 'xy_location_unprojected':
         grass.fatal(_("Coordinate reference system not available for current location <%s>") % tgtloc)
-        sys.exit(1)
     
     # switch to temp location
     os.environ['GISRC'] = str(srcgisrc)
@@ -175,8 +143,7 @@
     # make sure input is not xy
     if grass.parse_command('g.proj', flags = 'g')['name'] == 'xy_location_unprojected':
         grass.fatal(_("Coordinate reference system not available for input <%s>") % GDALdatasource)
-        sys.exit(1)
-
+    
     if options['extents'] == 'region':
         # switch to target location
         os.environ['GISRC'] = str(tgtgisrc)
@@ -188,14 +155,11 @@
         # reproject to src
         # switch to temp location
         os.environ['GISRC'] = str(srcgisrc)
-        ps = grass.start_command('v.proj', input = vreg, output = vreg, 
-                          location = tgtloc, mapset = tgtmapset, quiet = True)
-
-        returncode = ps.wait()
-
+        returncode = grass.run_command('v.proj', input = vreg, output = vreg, 
+                                       location = tgtloc, mapset = tgtmapset, quiet = True)
+        
         if returncode != 0:
             grass.fatal(_("Unable to reproject to source location"))
-            sys.exit(1)
         
         # set region from region vector
         grass.run_command('g.region', res = '1')
@@ -204,17 +168,14 @@
 
     # import into temp location
     grass.message(_("Importing <%s> ...") % OGRdatasource)
-    ps = grass.start_command('v.in.ogr', input = OGRdatasource,
-                             layer = layers, output = output,
-                             flags = vflags, verbose = True)
-
-    returncode = ps.wait()
+    returncode = grass.run_command('v.in.ogr', input = OGRdatasource,
+                                   layer = layers, output = output,
+                                   flags = vflags, verbose = True)
     
     # if it fails, return
     if returncode != 0:
         grass.fatal(_("Unable to import OGR datasource <%s>") % OGRdatasource)
-        sys.exit(1)
-
+    
     # switch to target location
     os.environ['GISRC'] = str(tgtgisrc)
 
@@ -224,18 +185,15 @@
 
     # v.proj
     grass.message(_("Reprojecting <%s>...") % output)
-    ps = grass.start_command('v.proj', location = tmploc,
-                             mapset = 'PERMANENT', input = output,
-                             quiet = True)
-    returncode = ps.wait()
+    returncode = grass.run_command('v.proj', location = tmploc,
+                                   mapset = 'PERMANENT', input = output,
+                                   quiet = True)
     if returncode != 0:
         grass.fatal(_("Unable to to reproject vector <%s>") % output)
-        sys.exit(1)
+    
+    return 0
 
-    sys.exit(0)
-
-
 if __name__ == "__main__":
     options, flags = grass.parser()
     atexit.register(cleanup)
-    main()
+    sys.exit(main())



More information about the grass-commit mailing list