[GRASS-SVN] r71590 - in grass/trunk/scripts: r.import v.import

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Oct 25 00:22:56 PDT 2017


Author: mmetz
Date: 2017-10-25 00:22:56 -0700 (Wed, 25 Oct 2017)
New Revision: 71590

Modified:
   grass/trunk/scripts/r.import/r.import.py
   grass/trunk/scripts/v.import/v.import.py
Log:
r.import/v.import: create temporary location only if direct import is not possible

Modified: grass/trunk/scripts/r.import/r.import.py
===================================================================
--- grass/trunk/scripts/r.import/r.import.py	2017-10-25 07:20:26 UTC (rev 71589)
+++ grass/trunk/scripts/r.import/r.import.py	2017-10-25 07:22:56 UTC (rev 71590)
@@ -175,8 +175,37 @@
         grass.fatal(
             _("Please provide the resolution for the imported dataset or change to 'estimated' resolution"))
 
+    # try r.in.gdal directly first
+    additional_flags = 'l' if flags['l'] else ''
+    if flags['o']:
+        additional_flags += 'o'
+    region_flag = ''
+    if options['extent'] == 'region':
+        region_flag += 'r'
+    if flags['o'] or grass.run_command('r.in.gdal', input=GDALdatasource, flags='j',
+                                       errors='status', quiet=True) == 0:
+        parameters = dict(input=GDALdatasource, output=output,
+                          memory=memory, flags='k' + additional_flags + region_flag)
+        if bands:
+            parameters['band'] = bands
+        try:
+            grass.run_command('r.in.gdal', **parameters)
+            grass.verbose(
+                _("Input <%s> successfully imported without reprojection") %
+                GDALdatasource)
+            return 0
+        except CalledModuleError as e:
+            grass.fatal(_("Unable to import GDAL dataset <%s>") % GDALdatasource)
+
     grassenv = grass.gisenv()
     tgtloc = grassenv['LOCATION_NAME']
+
+    # 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)
+
     tgtmapset = grassenv['MAPSET']
     GISDBASE = grassenv['GISDBASE']
     tgtgisrc = os.environ['GISRC']
@@ -208,40 +237,9 @@
     # switch to temp location
     os.environ['GISRC'] = str(SRCGISRC)
 
-    # switch to target location
-    os.environ['GISRC'] = str(tgtgisrc)
+    # print projection at verbose level
+    grass.verbose(grass.read_command('g.proj', flags='p').rstrip(os.linesep))
 
-    # try r.in.gdal directly first
-    additional_flags = 'l' if flags['l'] else ''
-    if flags['o']:
-        additional_flags += 'o'
-    region_flag = ''
-    if options['extent'] == 'region':
-        region_flag += 'r'
-    if flags['o'] or grass.run_command('r.in.gdal', input=GDALdatasource, flags='j',
-                                       errors='status', quiet=True) == 0:
-        parameters = dict(input=GDALdatasource, output=output,
-                          memory=memory, flags='k' + additional_flags + region_flag)
-        if bands:
-            parameters['band'] = bands
-        try:
-            grass.run_command('r.in.gdal', **parameters)
-            grass.verbose(
-                _("Input <%s> successfully imported without reprojection") %
-                GDALdatasource)
-            return 0
-        except CalledModuleError as e:
-            grass.fatal(_("Unable to import GDAL dataset <%s>") % GDALdatasource)
-
-    # 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)
-
-    # switch to temp location
-    os.environ['GISRC'] = str(SRCGISRC)
-
     # 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)

Modified: grass/trunk/scripts/v.import/v.import.py
===================================================================
--- grass/trunk/scripts/v.import/v.import.py	2017-10-25 07:20:26 UTC (rev 71589)
+++ grass/trunk/scripts/v.import/v.import.py	2017-10-25 07:22:56 UTC (rev 71590)
@@ -161,26 +161,6 @@
         return grass.run_command('g.proj', epsg=options['epsg'],
                                  datum_trans=options['datum_trans'])
 
-    grassenv = grass.gisenv()
-    tgtloc = grassenv['LOCATION_NAME']
-    tgtmapset = grassenv['MAPSET']
-    GISDBASE = grassenv['GISDBASE']
-    tgtgisrc = os.environ['GISRC']
-    SRCGISRC = grass.tempfile()
-
-    TMPLOC = 'temp_import_location_' + str(os.getpid())
-
-    f = open(SRCGISRC, 'w')
-    f.write('MAPSET: PERMANENT\n')
-    f.write('GISDBASE: %s\n' % GISDBASE)
-    f.write('LOCATION_NAME: %s\n' % TMPLOC)
-    f.write('GUI: text\n')
-    f.close()
-
-    tgtsrs = grass.read_command('g.proj', flags='j', quiet=True)
-
-    # create temp location from input without import
-    grass.verbose(_("Creating temporary location for <%s>...") % OGRdatasource)
     if layers:
         vopts['layer'] = layers
     if output:
@@ -201,6 +181,33 @@
         except CalledModuleError:
             grass.fatal(_("Unable to import <%s>") % OGRdatasource)
 
+    grassenv = grass.gisenv()
+    tgtloc = grassenv['LOCATION_NAME']
+
+    # 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)
+
+    tgtmapset = grassenv['MAPSET']
+    GISDBASE = grassenv['GISDBASE']
+    tgtgisrc = os.environ['GISRC']
+    SRCGISRC = grass.tempfile()
+
+    TMPLOC = 'temp_import_location_' + str(os.getpid())
+
+    f = open(SRCGISRC, 'w')
+    f.write('MAPSET: PERMANENT\n')
+    f.write('GISDBASE: %s\n' % GISDBASE)
+    f.write('LOCATION_NAME: %s\n' % TMPLOC)
+    f.write('GUI: text\n')
+    f.close()
+
+    tgtsrs = grass.read_command('g.proj', flags='j', quiet=True)
+
+    # create temp location from input without import
+    grass.verbose(_("Creating temporary location for <%s>...") % OGRdatasource)
     try:
         grass.run_command('v.in.ogr', input=OGRdatasource,
                           location=TMPLOC, flags='i', quiet=True, overwrite=overwrite, **vopts)
@@ -216,18 +223,6 @@
             kwargs['datum_trans'] = options['datum_trans']
         grass.run_command('g.proj', flags='c', epsg=options['epsg'], **kwargs)
 
-    # switch to target location
-    os.environ['GISRC'] = str(tgtgisrc)
-
-    # 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)
-
-    # switch to temp location
-    os.environ['GISRC'] = str(SRCGISRC)
-
     # print projection at verbose level
     grass.verbose(grass.read_command('g.proj', flags='p').rstrip(os.linesep))
 



More information about the grass-commit mailing list