[GRASS-SVN] r66695 - grass-addons/grass7/raster/r.import

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Nov 1 14:44:41 PST 2015


Author: neteler
Date: 2015-11-01 14:44:41 -0800 (Sun, 01 Nov 2015)
New Revision: 66695

Modified:
   grass-addons/grass7/raster/r.import/r.import.html
   grass-addons/grass7/raster/r.import/r.import.py
Log:
r.import addon: sync to relbranch70 (for users of installations prior to 7.0.2)

Modified: grass-addons/grass7/raster/r.import/r.import.html
===================================================================
--- grass-addons/grass7/raster/r.import/r.import.html	2015-11-01 17:08:20 UTC (rev 66694)
+++ grass-addons/grass7/raster/r.import/r.import.html	2015-11-01 22:44:41 UTC (rev 66695)
@@ -1,13 +1,16 @@
 <h2>DESCRIPTION</h2>
 
-<em>r.import</em> imports selected bands from a GDAL raster datasouce 
-into the current location and mapset. If the projection of the input 
-does not match the projection of the location, the input is reprojected 
-into the current location. If the projection of the input does match 
-the projection of the location, the input is imported directly with <a 
-href="r.in.gdal.html">r.in.gdal</a>.
+<em>r.import</em> imports a map or selected bands from a GDAL raster datasource
+into the current location and mapset. If the projection of the input
+does not match the projection of the location, the input is reprojected
+into the current location. If the projection of the input does match
+the projection of the location, the input is imported directly with
+<a href="r.in.gdal.html">r.in.gdal</a>.
 
-<h4>Resolution</h4>
+<h2>NOTES</h2>
+
+<h3>Resolution</h3>
+
 <em>r.import</em> reports the estimated target resolution for each 
 input band. The estimated resolution will usually be some floating 
 point number, e.g. 271.301. In case option <b>resolution</b> is set to
@@ -20,7 +23,15 @@
 For latlong locations, the resolution might be set to arc seconds, e.g. 1, 3, 7.5, 
 15, and 30 arc seconds are commonly used resolutions.
 
-<h4>Resampling methods</h4>
+<h3>Resampling methods</h3>
+
+When reprojecting a map to a new spatial reference system, the projected
+data is resampled with one of four different methods: nearest neighbor,
+bilinear, bicubic iterpolation or lanczos.
+
+<p>
+In the following common use cases:
+<p>
 <b>nearest</b> is the simplest method and the only possible method for 
 categorical data.
 <p>
@@ -38,19 +49,57 @@
 <b>nearest</b> or <b>bilinear</b>, linear features can become zigzag 
 features after reprojection.
 
+<p>
+For explanation of the <b>-l</b> flag, please refer to the
+<a href="r.in.gdal.html">r.in.gdal</a> manual.
+<p>
+When importing whole-world maps the user should disable map-trimming with
+the <b>-n</b> flag. For further explanations of <b>-n</b> flag, please refer
+the to <a href="r.proj.html">r.proj</a> manual.
+
+<h2>EXAMPLE</h2>
+
+Import of a subset from <a href="">Bioclim data set</a>, to be reprojected
+to current location projection (North Carolina sample dataset). While normally
+the full raster map is imported, we spatially subset using the <em>extent</em>
+parameter:
+
+<div class="code"><pre>
+# download selected Bioclim data
+wget http://biogeo.ucdavis.edu/data/climate/worldclim/1_4/grid/cur/bio_2-5m_bil.zip
+
+# extract BIO1 from package:
+unzip bio_2-5m_bil.zip bio1.bil bio1.hdr
+
+# set computational region to North Carolina, 4000 m target pixel resolution
+g.region -d res=4000 -ap
+
+# subset to current region and reproject on the fly to current location projection,
+# using -n since whole-world map is imported:
+r.import input=bio1.bil output=bioclim01 resample=bilinear \
+         extent=region resolution=region -n
+
+r.info bioclim01
+r.univar -e bioclim01
+</pre></div>
+
 <h2>KNOWN ISSUES</h2>
-Option <b>extent</b>=<em>region</em> works only when dataset has different
-projection than current location (<a href="r.proj.html">r.proj</a> is then used).
 
+The option <b>extent</b>=<em>region</em> only works when the dataset has a
+different projection than the current location (i.e., internally
+<a href="r.proj.html">r.proj</a> is invoked).
+
 <h2>SEE ALSO</h2>
 
 <em>
-<a href="r.in.gdal.html">r.in.gdal</a>,<br>
+<a href="r.in.gdal.html">r.in.gdal</a>,
 <a href="r.proj.html">r.proj</a>
 </em>
 
 <h2>AUTHORS</h2>
 
 Markus Metz<br>
+Improvements: Martin Landa, Anna Petrasova
 
-<p><i>Last changed: $Date: 2015-01-20 20:52:27 +0100 (Tue, 20 Jan 2015) $</i>
+<p>
+<i>Last changed: $Date: 2015-01-20 20:52:27 +0100 (Tue, 20 Jan 2015) $</i>

Modified: grass-addons/grass7/raster/r.import/r.import.py
===================================================================
--- grass-addons/grass7/raster/r.import/r.import.py	2015-11-01 17:08:20 UTC (rev 66694)
+++ grass-addons/grass7/raster/r.import/r.import.py	2015-11-01 22:44:41 UTC (rev 66695)
@@ -17,15 +17,12 @@
 #############################################################################
 
 #%module
-#% description: Imports raster data into a GRASS raster map using GDAL library and reproject on the fly.
+#% description: Imports raster data into a GRASS raster map using GDAL library and reprojects on the fly.
 #% keyword: raster
 #% keyword: import
 #% keyword: projection
 #%end
-#%option
-#% key: input
-#% type: string
-#% required: yes
+#%option G_OPT_F_BIN_INPUT
 #% description: Name of GDAL dataset to be imported
 #% guisection: Input
 #%end
@@ -54,11 +51,12 @@
 #%option
 #% key: resample
 #% type: string
-#% required: yes
+#% required: no
 #% multiple: no
 #% options: nearest,bilinear,bicubic,lanczos,bilinear_f,bicubic_f,lanczos_f
 #% description: Resampling method to use for reprojection
 #% descriptions: nearest;nearest neighbor;bilinear;bilinear interpolation;bicubic;bicubic interpolation;lanczos;lanczos filter;bilinear_f;bilinear interpolation with fallback;bicubic_f;bicubic interpolation with fallback;lanczos_f;lanczos filter with fallback
+#% answer: nearest
 #% guisection: Output
 #%end
 #%option
@@ -101,6 +99,10 @@
 #% description: Do not perform region cropping optimization
 #% guisection: Optional
 #%end
+#%flag
+#% key: l
+#% description: Force Lat/Lon maps to fit into geographic coordinates (90N,S; 180E,W)
+#%end
 
 
 import sys
@@ -116,6 +118,7 @@
 TMPLOC = None
 SRCGISRC = None
 GISDBASE = None
+TMP_REG_NAME = None
 
 
 def cleanup():
@@ -124,9 +127,13 @@
         grass.try_rmdir(os.path.join(GISDBASE, TMPLOC))
     if SRCGISRC:
         grass.try_remove(SRCGISRC)
+    if TMP_REG_NAME:
+        grass.run_command('g.remove', type='vector', name=TMP_REG_NAME,
+                          flags='f', quiet=True)
 
+
 def main():
-    global TMPLOC, SRCGISRC, GISDBASE
+    global TMPLOC, SRCGISRC, GISDBASE, TMP_REG_NAME
 
     GDALdatasource = options['input']
     output = options['output']
@@ -162,7 +169,7 @@
     tgtsrs = grass.read_command('g.proj', flags='j', quiet=True)
 
     # create temp location from input without import
-    grass.message(_("Creating temporary location for <%s>...") % GDALdatasource)
+    grass.verbose(_("Creating temporary location for <%s>...") % GDALdatasource)
     parameters = dict(input=GDALdatasource, output=output,
                       memory=memory, flags='c',
                       location=TMPLOC, quiet=True)
@@ -176,26 +183,24 @@
     # 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 r.in.gdal directly
+    # try r.in.gdal directly first
+    additional_flags = 'l' if flags['l'] else ''
+    if 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')
+                          memory=memory, flags='k' + additional_flags)
         if bands:
             parameters['band'] = bands
-        grass.message(_("Importing <%s>...") % GDALdatasource)
         try:
             grass.run_command('r.in.gdal', **parameters)
-        except CalledModuleError:
+            grass.verbose(_("Input <%s> successfully imported without reprojection") % GDALdatasource)
+            return 0
+        except CalledModuleError as e:
             grass.fatal(_("Unable to import GDAL dataset <%s>") % GDALdatasource)
-        grass.message(_("Input <%s> successfully imported without reprojection") % GDALdatasource)
-        return 0
-
+    
     # 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)
@@ -208,9 +213,9 @@
         grass.fatal(_("Coordinate reference system not available for input <%s>") % GDALdatasource)
 
     # import into temp location
-    grass.message(_("Importing <%s> to temporary location...") % GDALdatasource)
+    grass.verbose(_("Importing <%s> to temporary location...") % GDALdatasource)
     parameters = dict(input=GDALdatasource, output=output,
-                      memory=memory, flags='k')
+                      memory=memory, flags='k' + additional_flags)
     if bands:
         parameters['band'] = bands
     try:
@@ -256,17 +261,27 @@
                                                 memory=memory, quiet=True)
             except CalledModuleError:
                 grass.fatal(_("Unable to get reprojected map extent"))
+            try:
+                srcregion = grass.parse_key_val(tgtextents, val_type=float, vsep=' ')
+                n = srcregion['n']
+                s = srcregion['s']
+                e = srcregion['e']
+                w = srcregion['w']
+            except ValueError:  # import into latlong, expect 53:39:06.894826N
+                srcregion = grass.parse_key_val(tgtextents, vsep=' ')
+                n = grass.float_or_dms(srcregion['n'][:-1]) * \
+                    (-1 if srcregion['n'][-1] == 'S' else 1)
+                s = grass.float_or_dms(srcregion['s'][:-1]) * \
+                    (-1 if srcregion['s'][-1] == 'S' else 1)
+                e = grass.float_or_dms(srcregion['e'][:-1]) * \
+                    (-1 if srcregion['e'][-1] == 'W' else 1)
+                w = grass.float_or_dms(srcregion['w'][:-1]) * \
+                    (-1 if srcregion['w'][-1] == 'W' else 1)
 
-            srcregion = grass.parse_key_val(tgtextents, val_type=float, vsep=' ')
-            n = srcregion['n']
-            s = srcregion['s']
-            e = srcregion['e']
-            w = srcregion['w']
-
             grass.run_command('g.region', n=n, s=s, e=e, w=w)
 
         # v.in.region in tgt
-        vreg = 'vreg_' + str(os.getpid())
+        vreg = TMP_REG_NAME = 'vreg_tmp_' + str(os.getpid())
         grass.run_command('v.in.region', output=vreg, quiet=True)
 
         grass.del_temp_region()
@@ -283,20 +298,23 @@
         # set region from region vector
         grass.run_command('g.region', raster=outfile)
         grass.run_command('g.region', vector=vreg)
-        # align to fist band
+        # align to first band
         grass.run_command('g.region', align=outfile)
         # get number of cells
         cells = grass.region()['cells']
 
         estres = math.sqrt((n - s) * (e - w) / cells)
+        # remove from source location for multi bands import
+        grass.run_command('g.remove', type='vector', name=vreg,
+                          flags='f', quiet=True)
 
         os.environ['GISRC'] = str(tgtgisrc)
         grass.run_command('g.remove', type='vector', name=vreg,
                           flags='f', quiet=True)
 
-        grass.message(_("Estimated target resolution for input band <%s>: %g") % (outfile, estres))
+        grass.message(_("Estimated target resolution for input band <{out}>: {res}").format(out=outfile, res=estres))
         if flags['e']:
-            return 0
+            continue
 
         if options['extent'] == 'input':
             grass.use_temp_region()
@@ -307,6 +325,14 @@
             res = estres
         elif tgtres == 'value':
             res = tgtres_value
+            grass.message(_("Using given resolution for input band <{out}>: {res}").format(out=outfile, res=res))
+            # align to requested resolution
+            grass.run_command('g.region', res=res, flags='a')
+        else:
+            curr_reg = grass.region()
+            grass.message(_("Using current region resolution for input band "
+                            "<{out}>: nsres={ns}, ewres={ew}").format(out=outfile, ns=curr_reg['nsres'],
+                                                                      ew=curr_reg['ewres']))
 
         # r.proj
         grass.message(_("Reprojecting <%s>...") % outfile)
@@ -324,9 +350,14 @@
         if options['extent'] == 'input':
             grass.del_temp_region()
 
+    if flags['e']:
+        return 0
+
     if group:
         grass.run_command('i.group', group=output, input=','.join(outfiles))
 
+    # TODO: write metadata with r.support
+
     return 0
 
 if __name__ == "__main__":



More information about the grass-commit mailing list