[GRASS-SVN] r60016 - grass/trunk/vector/v.in.ogr

svn_grass at osgeo.org svn_grass at osgeo.org
Thu May 1 11:49:46 PDT 2014


Author: martinl
Date: 2014-05-01 11:49:46 -0700 (Thu, 01 May 2014)
New Revision: 60016

Modified:
   grass/trunk/vector/v.in.ogr/main.c
   grass/trunk/vector/v.in.ogr/v.in.ogr.html
Log:
v.in.ogr: add 'key' option


Modified: grass/trunk/vector/v.in.ogr/main.c
===================================================================
--- grass/trunk/vector/v.in.ogr/main.c	2014-05-01 16:42:06 UTC (rev 60015)
+++ grass/trunk/vector/v.in.ogr/main.c	2014-05-01 18:49:46 UTC (rev 60016)
@@ -9,7 +9,7 @@
  *
  * PURPOSE:      Import OGR vectors
  *
- * COPYRIGHT:    (C) 2003, 2011-2014 by the GRASS Development Team
+ * COPYRIGHT:    (C) 2003-2014 by the GRASS Development Team
  *
  *               This program is free software under the GNU General
  *               Public License (>=v2).  Read the file COPYING that
@@ -52,7 +52,7 @@
     struct _param {
 	struct Option *dsn, *out, *layer, *spat, *where,
 	    *min_area;
-	struct Option *snap, *type, *outloc, *cnames, *encoding;
+        struct Option *snap, *type, *outloc, *cnames, *encoding, *key;
     } param;
     struct _flag {
 	struct Flag *list, *no_clean, *force2d, *notab,
@@ -84,6 +84,8 @@
     dbDriver *driver = NULL;
     dbString sql, strval;
     int with_z, input3d;
+    const char *key_column;
+    int key_idx = -2; /* -1 for fid column */
 
     /* OGR */
     OGRDataSourceH Ogr_ds;
@@ -121,6 +123,7 @@
     OFTIntegerListlength = 40;	/* hack due to limitation in OGR */
     area_size = 0.0;
     use_tmp_vect = FALSE;
+    key_column = GV_KEY_COLUMN;
 
     G_gisinit(argv[0]);
 
@@ -229,6 +232,17 @@
         _("Overrides encoding interpretation, useful when importing ESRI Shapefile");
     param.encoding->guisection = _("Attributes");
 
+    param.key = G_define_option();
+    param.key->key = "key";
+    param.key->type = TYPE_STRING;
+    param.key->required = NO;
+    param.key->label =
+        _("Name of column used for categories");
+    param.key->description = 
+        _("If not given, categories are generated as unique values and stored in 'cat' column");
+    param.key->guisection = _("Attributes");
+    param.key->guisection = _("Attributes");
+
     flag.formats = G_define_flag();
     flag.formats->key = 'f';
     flag.formats->description = _("List supported OGR formats and exit");
@@ -872,10 +886,31 @@
 	Ogr_layer = OGR_DS_GetLayer(Ogr_ds, layer_id);
 	Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
 
+        if (param.key->answer) {
+            const char *fid_column;
+            fid_column = OGR_L_GetFIDColumn(Ogr_layer);
+            if (fid_column) {
+                key_column = G_store(fid_column);
+                key_idx = -1;
+            }
+            if (!fid_column || strcmp(fid_column, param.key->answer) != 0) {
+                key_idx = OGR_FD_GetFieldIndex(Ogr_featuredefn, param.key->answer);
+                if (key_idx == -1)
+                    G_fatal_error(_("Key column '%s' not found"), param.key->answer);
+            }
+
+            if (key_idx > -1) {
+                /* check if the field is integer */
+                Ogr_field = OGR_FD_GetFieldDefn(Ogr_featuredefn, key_idx);
+                Ogr_ftype = OGR_Fld_GetType(Ogr_field);
+                if (Ogr_ftype != OFTInteger)
+                    G_fatal_error(_("Key column '%s' is not integer"), param.key->answer);
+                key_column = G_store(OGR_Fld_GetNameRef(Ogr_field));
+            }
+        }
+
 	/* Add DB link */
 	if (!flag.notab->answer) {
-	    char *cat_col_name = GV_KEY_COLUMN;
-
 	    if (nlayers == 1) {	/* one layer only */
 		Fi = Vect_default_field_info(&Map, layer + 1, NULL,
 					     GV_1TABLE);
@@ -886,20 +921,23 @@
 	    }
 
 	    if (ncnames > 0) {
-		cat_col_name = param.cnames->answers[0];
+		key_column = param.cnames->answers[0];
 	    }
 	    Vect_map_add_dblink(&Map, layer + 1, layer_names[layer], Fi->table,
-				cat_col_name, Fi->database, Fi->driver);
+				key_column, Fi->database, Fi->driver);
 
 	    ncols = OGR_FD_GetFieldCount(Ogr_featuredefn);
 	    G_debug(2, "%d columns", ncols);
 
 	    /* Create table */
 	    sprintf(buf, "create table %s (%s integer", Fi->table,
-		    cat_col_name);
+		    key_column);
 	    db_set_string(&sql, buf);
 	    for (i = 0; i < ncols; i++) {
 
+                if (key_idx > -1 && key_idx == i)
+                    continue; /* skip defined key (FID column) */
+                
 		Ogr_field = OGR_FD_GetFieldDefn(Ogr_featuredefn, i);
 		Ogr_ftype = OGR_Fld_GetType(Ogr_field);
 
@@ -1019,10 +1057,6 @@
 			      db_get_string(&sql));
 	    }
 
-	    if (db_create_index2(driver, Fi->table, cat_col_name) != DB_OK)
-		G_warning(_("Unable to create index for table <%s>, key <%s>"),
-			  Fi->table, cat_col_name);
-
 	    if (db_grant_on_table
 		(driver, Fi->table, DB_PRIV_SELECT,
 		 DB_GROUP | DB_PUBLIC) != DB_OK)
@@ -1042,6 +1076,7 @@
 
 	G_important_message(_("Importing %d features (OGR layer <%s>)..."),
 			    n_features, layer_names[layer]);
+
 	while ((Ogr_feature = OGR_L_GetNextFeature(Ogr_layer)) != NULL) {
 	    G_percent(feature_count++, n_features, 1);	/* show something happens */
 	    /* Geometry */
@@ -1050,6 +1085,11 @@
 		nogeom++;
 	    }
 	    else {
+                if (key_idx > -1)
+                    cat = OGR_F_GetFieldAsInteger(Ogr_feature, key_idx);
+                else if (key_idx == -1)
+                    cat = OGR_F_GetFID(Ogr_feature);
+                
 		geom(Ogr_geometry, Out, layer + 1, cat, min_area, type,
 		     flag.no_clean->answer);
 	    }
@@ -1059,6 +1099,10 @@
 		sprintf(buf, "insert into %s values ( %d", Fi->table, cat);
 		db_set_string(&sql, buf);
 		for (i = 0; i < ncols; i++) {
+
+                    if (key_idx > -1 && key_idx == i)
+                        continue; /* skip defined key (FID column) */
+
 		    Ogr_field = OGR_FD_GetFieldDefn(Ogr_featuredefn, i);
 		    Ogr_ftype = OGR_Fld_GetType(Ogr_field);
 		    if (OGR_F_IsFieldSet(Ogr_feature, i)) {
@@ -1456,6 +1500,12 @@
 
     delete_table = Vect_maptype(&Map) != GV_FORMAT_NATIVE;
     Vect_close(&Map);
+
+    /* create index - may fail on non-unique categories */
+    if (db_create_index2(driver, Fi->table, key_column) != DB_OK)
+        G_warning(_("Unable to create index for table <%s>, key <%s>"),
+                  Fi->table, key_column);
+    
     if (delete_table) {
         sprintf(buf, "drop table %s", Fi->table);
         db_set_string(&sql, buf);

Modified: grass/trunk/vector/v.in.ogr/v.in.ogr.html
===================================================================
--- grass/trunk/vector/v.in.ogr/v.in.ogr.html	2014-05-01 16:42:06 UTC (rev 60015)
+++ grass/trunk/vector/v.in.ogr/v.in.ogr.html	2014-05-01 18:49:46 UTC (rev 60016)
@@ -143,6 +143,12 @@
 Value for <b>encoding</b> also affects text recoding when importing
 DXF files. For other formats has encoding value no effect.
 
+<p>
+Option <b>key</b> specifies column name used for feature
+categories. This column must be integer. If not specified, categories
+numbers are generated starting with 1 and stored in the column named
+"cat".
+
 <h2>EXAMPLES</h2>
 
 The command imports various vector formats:



More information about the grass-commit mailing list