[GRASS-SVN] r64371 - grass/trunk/raster/r.in.gdal

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Jan 30 10:17:38 PST 2015


Author: martinl
Date: 2015-01-30 10:17:38 -0800 (Fri, 30 Jan 2015)
New Revision: 64371

Modified:
   grass/trunk/raster/r.in.gdal/main.c
Log:
r.in.gdal: add flag to print number of raster bands
           fix overwrite check for all bands
           add error handler to close data source correctly


Modified: grass/trunk/raster/r.in.gdal/main.c
===================================================================
--- grass/trunk/raster/r.in.gdal/main.c	2015-01-30 17:18:51 UTC (rev 64370)
+++ grass/trunk/raster/r.in.gdal/main.c	2015-01-30 18:17:38 UTC (rev 64371)
@@ -9,7 +9,7 @@
  * PURPOSE:      Imports many GIS/image formats into GRASS utilizing the GDAL
  *               library.
  *
- * COPYRIGHT:    (C) 2001-2011 by Frank Warmerdam, and the GRASS Development Team
+ * COPYRIGHT:    (C) 2001-2015 by Frank Warmerdam, and the GRASS Development Team
  *
  *               This program is free software under the GNU General Public
  *               License (>=v2). Read the file COPYING that comes with GRASS
@@ -42,6 +42,7 @@
 static void SetupReprojector(const char *pszSrcWKT, const char *pszDstLoc,
 			     struct pj_info *iproj, struct pj_info *oproj);
 static int dump_rat(GDALRasterBandH hBand, char *outrat, int nBand);
+static void error_handler_ds(void *p);
 static int l1bdriver;
 
 /************************************************************************/
@@ -74,7 +75,7 @@
 	struct Option *input, *output, *target, *title, *outloc, *band,
 	              *memory, *offset, *rat;
     } parm;
-    struct Flag *flag_o, *flag_e, *flag_k, *flag_f, *flag_l, *flag_c;
+    struct Flag *flag_o, *flag_e, *flag_k, *flag_f, *flag_l, *flag_c, *flag_p;
 
     /* -------------------------------------------------------------------- */
     /*      Initialize.                                                     */
@@ -184,6 +185,13 @@
 	_("Create the location specified by the \"location\" parameter and exit."
           " Do not import the raster file.");
 
+    flag_p = G_define_flag();
+    flag_p->key = 'p';
+    flag_p->description = _("Print number of bands and exit");
+    flag_p->suppress_required = YES;
+
+    G_option_requires(flag_p, parm.input, NULL);
+    
     /* The parser checks if the map already exists in current mapset, this is
      * wrong if location options is used, so we switch out the check and do it
      * in the module after the parser */
@@ -261,22 +269,14 @@
 	exit(EXIT_SUCCESS);
     }
 
-    if (!parm.outloc->answer) {	/* Check if the map exists */
-	if (G_find_raster2(output, G_mapset())) {
-	    if (overwrite)
-		G_warning(_("Raster map <%s> already exists and will be overwritten"),
-			  output);
-	    else
-		G_fatal_error(_("Raster map <%s> already exists"), output);
-	}
-    }
-
     /* -------------------------------------------------------------------- */
     /*      Open the file.                                                  */
     /* -------------------------------------------------------------------- */
     hDS = GDALOpen(input, GA_ReadOnly);
     if (hDS == NULL)
-	exit(EXIT_FAILURE);
+        G_fatal_error(_("Unable to open datasource <%s>"), input);
+    G_add_error_handler(error_handler_ds, hDS);
+    
     hDriver = GDALGetDatasetDriver(hDS);	/* needed for AVHRR data */
     /* L1B - NOAA/AVHRR data must be treated differently */
     /* for hDriver names see gdal/frmts/gdalallregister.cpp */
@@ -290,6 +290,24 @@
 		    "(%s or %s)."), "i.rectify -t", "gdalwarp -tps");
     }
 
+    if (flag_p->answer) {
+        /* print number of bands */
+        fprintf(stdout, "%d\n", GDALGetRasterCount(hDS));
+        GDALClose(hDS);
+        exit(EXIT_SUCCESS);
+    }
+
+    if (!parm.outloc->answer &&
+        GDALGetRasterCount(hDS) == 1) {	/* Check if the map exists */
+	if (G_find_raster2(output, G_mapset())) {
+	    if (overwrite)
+		G_warning(_("Raster map <%s> already exists and will be overwritten"),
+			  output);
+	    else
+		G_fatal_error(_("Raster map <%s> already exists"), output);
+	}
+    }
+
     /* zero cell header */
     G_zero(&cellhd, sizeof(struct Cell_head));
     
@@ -498,7 +516,8 @@
 	}
     }
 
-    G_verbose_message(_("Proceeding with import..."));
+    G_message(_("Proceeding with import of %d raster bands..."),
+              GDALGetRasterCount(hDS));
 
     /* -------------------------------------------------------------------- */
     /*      Set the active window to match the available data.              */
@@ -595,6 +614,16 @@
 	    else
 		sprintf(szBandName, "%s.%d", output, nBand + offset);
 
+            if (!parm.outloc->answer) {	/* Check if the map exists */
+              if (G_find_raster2(szBandName, G_mapset())) {
+                if (overwrite)
+                  G_warning(_("Raster map <%s> already exists and will be overwritten"),
+                            szBandName);
+                else
+                  G_fatal_error(_("Raster map <%s> already exists"), szBandName);
+              }
+            }
+
 	    ImportBand(hBand, szBandName, &ref);
 
 	    if (title)
@@ -873,6 +902,8 @@
     int have_colors = 0;
     GDALRasterAttributeTableH gdal_rat;
 
+    G_message(_("Importing raster map <%s>..."), output);
+    
     /* -------------------------------------------------------------------- */
     /*      Select a cell type for the new cell.                            */
     /* -------------------------------------------------------------------- */
@@ -1468,8 +1499,6 @@
 	}
     }
 
-    G_message(_("Raster map <%s> created."), output);
-
     return;
 }
 
@@ -1577,3 +1606,9 @@
 
     return 1;
 }
+
+void error_handler_ds(void *p)
+{
+    GDALDatasetH hDS = (GDALDatasetH) p;
+    GDALClose(hDS);
+}



More information about the grass-commit mailing list