[GRASS-SVN] r60314 - grass/branches/releasebranch_7_0/vector/v.in.ogr
svn_grass at osgeo.org
svn_grass at osgeo.org
Sun May 18 06:59:59 PDT 2014
Author: martinl
Date: 2014-05-18 06:59:59 -0700 (Sun, 18 May 2014)
New Revision: 60314
Modified:
grass/branches/releasebranch_7_0/vector/v.in.ogr/main.c
grass/branches/releasebranch_7_0/vector/v.in.ogr/v.in.ogr.html
Log:
v.in.ogr: add 'key' option
(backport r60016 from trunk)
Modified: grass/branches/releasebranch_7_0/vector/v.in.ogr/main.c
===================================================================
--- grass/branches/releasebranch_7_0/vector/v.in.ogr/main.c 2014-05-18 13:17:31 UTC (rev 60313)
+++ grass/branches/releasebranch_7_0/vector/v.in.ogr/main.c 2014-05-18 13:59:59 UTC (rev 60314)
@@ -9,7 +9,7 @@
*
* PURPOSE: Import OGR vectors
*
- * COPYRIGHT: (C) 2003, 2011-2013 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;
@@ -120,6 +122,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]);
@@ -228,6 +231,16 @@
_("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");
+
flag.formats = G_define_flag();
flag.formats->key = 'f';
flag.formats->description = _("List supported OGR formats and exit");
@@ -832,10 +845,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);
@@ -846,20 +880,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);
@@ -979,10 +1016,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)
@@ -1002,6 +1035,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 */
@@ -1010,6 +1044,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);
}
@@ -1019,6 +1058,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)) {
@@ -1416,6 +1459,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/branches/releasebranch_7_0/vector/v.in.ogr/v.in.ogr.html
===================================================================
--- grass/branches/releasebranch_7_0/vector/v.in.ogr/v.in.ogr.html 2014-05-18 13:17:31 UTC (rev 60313)
+++ grass/branches/releasebranch_7_0/vector/v.in.ogr/v.in.ogr.html 2014-05-18 13:59:59 UTC (rev 60314)
@@ -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