[GRASS-SVN] r44484 - grass/trunk/vector/v.out.ogr
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Nov 29 10:46:39 EST 2010
Author: martinl
Date: 2010-11-29 07:46:39 -0800 (Mon, 29 Nov 2010)
New Revision: 44484
Modified:
grass/trunk/vector/v.out.ogr/args.c
grass/trunk/vector/v.out.ogr/local_proto.h
grass/trunk/vector/v.out.ogr/main.c
Log:
v.out.ogr: new flag to append data to the existing OGR layer
Modified: grass/trunk/vector/v.out.ogr/args.c
===================================================================
--- grass/trunk/vector/v.out.ogr/args.c 2010-11-29 12:53:40 UTC (rev 44483)
+++ grass/trunk/vector/v.out.ogr/args.c 2010-11-29 15:46:39 UTC (rev 44484)
@@ -73,25 +73,33 @@
flags->update->key = 'u';
flags->update->description = _("Open an existing datasource for update");
+ flags->append = G_define_flag();
+ flags->append->key = 'a';
+ flags->append->description = _("Append to existing layer instead of creating new if it exists");
+
flags->nocat = G_define_flag();
flags->nocat->key = 's';
flags->nocat->description =
_("Skip export of GRASS category ID ('cat') attribute");
-
+ flags->nocat->guisection = _("Creation");
+
flags->cat = G_define_flag();
flags->cat->key = 'c';
flags->cat->description =
_("Also export features without category (not labeled). "
"Otherwise only features with category are exported.");
+ flags->cat->guisection = _("Input");
flags->esristyle = G_define_flag();
flags->esristyle->key = 'e';
flags->esristyle->description = _("Use ESRI-style .prj file format "
"(applies to Shapefile output only)");
-
+ flags->esristyle->guisection = _("Creation");
+
flags->poly = G_define_flag();
flags->poly->key = 'p';
flags->poly->description = _("Export lines as polygons");
+ flags->poly->guisection = _("Creation");
flags->new = G_define_flag();
flags->new->key = 'n';
Modified: grass/trunk/vector/v.out.ogr/local_proto.h
===================================================================
--- grass/trunk/vector/v.out.ogr/local_proto.h 2010-11-29 12:53:40 UTC (rev 44483)
+++ grass/trunk/vector/v.out.ogr/local_proto.h 2010-11-29 15:46:39 UTC (rev 44484)
@@ -11,7 +11,7 @@
};
struct Flags {
- struct Flag *cat, *esristyle, *poly, *update, *nocat, *new;
+ struct Flag *cat, *esristyle, *poly, *update, *nocat, *new, *append;
};
/* args.c */
Modified: grass/trunk/vector/v.out.ogr/main.c
===================================================================
--- grass/trunk/vector/v.out.ogr/main.c 2010-11-29 12:53:40 UTC (rev 44483)
+++ grass/trunk/vector/v.out.ogr/main.c 2010-11-29 15:46:39 UTC (rev 44484)
@@ -32,6 +32,7 @@
int i, j, k, centroid, otype, donocat;
int num_to_export;
int field;
+ int overwrite;
struct GModule *module;
struct Options options;
struct Flags flags;
@@ -279,7 +280,15 @@
if (drn == -1)
G_fatal_error(_("OGR driver <%s> not found"), options.format->answer);
Ogr_driver = OGRGetDriver(drn);
-
+
+ overwrite = G_check_overwrite(argc, argv);
+ if (overwrite || flags.append->answer) {
+ G_verbose_message(_("Overwrite/Append existing layer requires "
+ "opening data source in update mode, forcing '-%c' flag"),
+ flags.update->key);
+ flags.update->answer = TRUE;
+ }
+
if (flags.update->answer) {
G_debug(1, "Update OGR data source");
Ogr_ds = OGR_Dr_Open(Ogr_driver, options.dsn->answer, TRUE);
@@ -297,18 +306,27 @@
options.dsn->answer);
/* check if OGR layer exists */
- if (OGR_DS_GetLayerByName(Ogr_ds, options.layer->answer)) {
- if (!G_check_overwrite(argc, argv)) {
- G_fatal_error(_("OGR layer <%s> already exists in '%s'"),
- options.layer->answer, options.dsn->answer);
+ if (flags.append->answer || overwrite) {
+ if (OGR_DS_GetLayerByName(Ogr_ds, options.layer->answer)) {
+ if (!overwrite && !flags.append->answer) {
+ G_fatal_error(_("OGR layer <%s> already exists in '%s'"),
+ options.layer->answer, options.dsn->answer);
+ }
+ else if (overwrite) {
+ G_warning(_("OGR layer <%s> already exists and will be overwritten"),
+ options.layer->answer);
+ papszLCO = CSLSetNameValue(papszLCO, "OVERWRITE", "YES");
+ }
}
else {
- G_warning(_("OGR layer <%s> already exists and will be overwritten"),
- options.layer->answer);
- papszLCO = CSLSetNameValue(papszLCO, "OVERWRITE", "YES");
+ if (flags.append->answer) {
+ G_warning(_("OGR layer <%s> doesn't exists, "
+ "creating new OGR layer instead"),
+ options.layer->answer);
+ flags.append->answer = FALSE;
+ }
}
}
-
/* check if the map is 3d */
if (Vect_is_3d(&In)) {
/* specific check for ESRI ShapeFile */
@@ -344,12 +362,19 @@
}
G_debug(1, "Create OGR layer");
- Ogr_layer =
- OGR_DS_CreateLayer(Ogr_ds, options.layer->answer, Ogr_projection,
- wkbtype, papszLCO);
+ if (flags.append->answer)
+ Ogr_layer = OGR_DS_GetLayerByName(Ogr_ds, options.layer->answer);
+ else
+ Ogr_layer = OGR_DS_CreateLayer(Ogr_ds, options.layer->answer, Ogr_projection, wkbtype,
+ papszLCO);
+
CSLDestroy(papszLCO);
- if (Ogr_layer == NULL)
- G_fatal_error(_("Unable to create OGR layer"));
+ if (Ogr_layer == NULL) {
+ if (flags.append->answer)
+ G_fatal_error(_("OGR layer <%s> not found"), options.layer->answer);
+ else
+ G_fatal_error(_("Unable to create OGR layer"));
+ }
db_init_string(&dbstring);
@@ -359,80 +384,97 @@
doatt = 1; /* do attributes */
Fi = Vect_get_field(&In, field);
if (Fi == NULL) {
+ char create_field = TRUE;
G_warning(_("No attribute table found -> using only category numbers as attributes"));
/* if we have no more than a 'cat' column, then that has to
be exported in any case */
if (flags.nocat->answer) {
G_warning(_("Exporting 'cat' anyway, as it is the only attribute table field"));
- flags.nocat->answer = 0;
+ flags.nocat->answer = FALSE;
}
- Ogr_field = OGR_Fld_Create("cat", OFTInteger);
- OGR_L_CreateField(Ogr_layer, Ogr_field, 0);
- OGR_Fld_Destroy(Ogr_field);
-
+
+ if (flags.append->answer) {
+ Ogr_field = OGR_L_GetLayerDefn(Ogr_layer);
+ if (OGR_FD_GetFieldIndex(Ogr_field, "cat") > -1)
+ create_field = FALSE;
+ else
+ G_warning(_("New attribute column <%s> added to the table"),
+ "cat");
+ }
+
+ if (create_field) {
+ Ogr_field = OGR_Fld_Create("cat", OFTInteger);
+ OGR_L_CreateField(Ogr_layer, Ogr_field, 0);
+ OGR_Fld_Destroy(Ogr_field);
+ }
+
doatt = 0;
- }
- else {
- Driver = db_start_driver_open_database(Fi->driver, Fi->database);
- if (!Driver)
- G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
- Fi->database, Fi->driver);
+ }
+ else {
+ Driver = db_start_driver_open_database(Fi->driver, Fi->database);
+ if (!Driver)
+ G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
+ Fi->database, Fi->driver);
- db_set_string(&dbstring, Fi->table);
- if (db_describe_table(Driver, &dbstring, &Table) != DB_OK)
- G_fatal_error(_("Unable to describe table <%s>"), Fi->table);
+ db_set_string(&dbstring, Fi->table);
+ if (db_describe_table(Driver, &dbstring, &Table) != DB_OK)
+ G_fatal_error(_("Unable to describe table <%s>"), Fi->table);
- ncol = db_get_table_number_of_columns(Table);
- G_debug(2, "ncol = %d", ncol);
- keycol = -1;
- for (i = 0; i < ncol; i++) {
- Column = db_get_table_column(Table, i);
- colsqltype = db_get_column_sqltype(Column);
- G_debug(2, "col %d: %s (%s)", i, db_get_column_name(Column),
- db_sqltype_name(colsqltype));
- colctype = db_sqltype_to_Ctype(colsqltype);
+ ncol = db_get_table_number_of_columns(Table);
+ G_debug(2, "ncol = %d", ncol);
+ keycol = -1;
+ for (i = 0; i < ncol; i++) {
+ Column = db_get_table_column(Table, i);
+ colsqltype = db_get_column_sqltype(Column);
+ G_debug(2, "col %d: %s (%s)", i, db_get_column_name(Column),
+ db_sqltype_name(colsqltype));
+ colctype = db_sqltype_to_Ctype(colsqltype);
- switch (colctype) {
- case DB_C_TYPE_INT:
- ogr_ftype = OFTInteger;
- break;
- case DB_C_TYPE_DOUBLE:
- ogr_ftype = OFTReal;
- break;
- case DB_C_TYPE_STRING:
- ogr_ftype = OFTString;
- break;
- case DB_C_TYPE_DATETIME:
- ogr_ftype = OFTString;
- break;
- }
- G_debug(2, "ogr_ftype = %d", ogr_ftype);
+ switch (colctype) {
+ case DB_C_TYPE_INT:
+ ogr_ftype = OFTInteger;
+ break;
+ case DB_C_TYPE_DOUBLE:
+ ogr_ftype = OFTReal;
+ break;
+ case DB_C_TYPE_STRING:
+ ogr_ftype = OFTString;
+ break;
+ case DB_C_TYPE_DATETIME:
+ ogr_ftype = OFTString;
+ break;
+ }
+ G_debug(2, "ogr_ftype = %d", ogr_ftype);
- strcpy(key1, Fi->key);
- G_tolcase(key1);
- strcpy(key2, db_get_column_name(Column));
- G_tolcase(key2);
- if (strcmp(key1, key2) == 0)
- keycol = i;
- G_debug(2, "%s x %s -> %s x %s -> keycol = %d", Fi->key,
- db_get_column_name(Column), key1, key2, keycol);
+ strcpy(key1, Fi->key);
+ G_tolcase(key1);
+ strcpy(key2, db_get_column_name(Column));
+ G_tolcase(key2);
+ if (strcmp(key1, key2) == 0)
+ keycol = i;
+ G_debug(2, "%s x %s -> %s x %s -> keycol = %d", Fi->key,
+ db_get_column_name(Column), key1, key2, keycol);
- if (!flags.nocat->answer) {
- Ogr_field =
- OGR_Fld_Create(db_get_column_name(Column), ogr_ftype);
- OGR_L_CreateField(Ogr_layer, Ogr_field, 0);
- OGR_Fld_Destroy(Ogr_field);
+ if (flags.nocat->answer &&
+ strcmp(Fi->key, db_get_column_name(Column)) == 0)
+ /* skip export of 'cat' field */
+ continue;
+
+ if (flags.append->answer) {
+ Ogr_field = OGR_L_GetLayerDefn(Ogr_layer);
+ if (OGR_FD_GetFieldIndex(Ogr_field, db_get_column_name(Column)) > -1)
+ /* skip existing fields */
+ continue;
+ else
+ G_warning(_("New attribute column <%s> added to the table"),
+ db_get_column_name(Column));
}
- else {
- /* skip export of 'cat' field */
- if (strcmp(Fi->key, db_get_column_name(Column)) != 0) {
- Ogr_field =
- OGR_Fld_Create(db_get_column_name(Column),
- ogr_ftype);
- OGR_L_CreateField(Ogr_layer, Ogr_field, 0);
- OGR_Fld_Destroy(Ogr_field);
- }
- }
+
+ Ogr_field =
+ OGR_Fld_Create(db_get_column_name(Column),
+ ogr_ftype);
+ OGR_L_CreateField(Ogr_layer, Ogr_field, 0);
+ OGR_Fld_Destroy(Ogr_field);
}
if (keycol == -1)
G_fatal_error(_("Key column <%s> not found"), Fi->key);
More information about the grass-commit
mailing list