[GRASS-SVN] r38370 - grass/trunk/vector/v.external
svn_grass at osgeo.org
svn_grass at osgeo.org
Sun Jul 12 05:21:29 EDT 2009
Author: martinl
Date: 2009-07-12 05:21:28 -0400 (Sun, 12 Jul 2009)
New Revision: 38370
Added:
grass/trunk/vector/v.external/list.c
grass/trunk/vector/v.external/local_proto.h
Modified:
grass/trunk/vector/v.external/main.c
Log:
v.external: minor code clean up
Added: grass/trunk/vector/v.external/list.c
===================================================================
--- grass/trunk/vector/v.external/list.c (rev 0)
+++ grass/trunk/vector/v.external/list.c 2009-07-12 09:21:28 UTC (rev 38370)
@@ -0,0 +1,60 @@
+#include <grass/gis.h>
+#include <grass/glocale.h>
+
+#include "ogr_api.h"
+#include "local_proto.h"
+
+void list_formats(FILE *fd) {
+ int i;
+
+ OGRSFDriverH Ogr_driver;
+
+ G_message(_("Available drivers:\n"));
+ for (i = 0; i < OGRGetDriverCount(); i++) {
+ Ogr_driver = OGRGetDriver(i);
+ fprintf(fd, " %s\n", OGR_Dr_GetName(Ogr_driver));
+ }
+}
+
+int list_layers(FILE *fd, const char *dsn, const char *layer)
+{
+ int i, ret;
+ int nlayers;
+ char *layer_name;
+
+ OGRDataSourceH Ogr_ds;
+ OGRLayerH Ogr_layer;
+ OGRFeatureDefnH Ogr_featuredefn;
+
+ ret = -1;
+
+ /* open OGR DSN */
+ Ogr_ds = OGROpen(dsn, FALSE, NULL);
+ if (!Ogr_ds) {
+ G_fatal_error(_("Unable to open data source '%s'"), dsn);
+ }
+
+ /* Make a list of available layers */
+ nlayers = OGR_DS_GetLayerCount(Ogr_ds);
+
+ if (fd)
+ G_message(_("Data source contains %d layers:"), nlayers);
+
+ for (i = 0; i < nlayers; i++) {
+ Ogr_layer = OGR_DS_GetLayer(Ogr_ds, i);
+ Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
+ layer_name = (char *) OGR_FD_GetName(Ogr_featuredefn);
+
+ if (fd)
+ fprintf(fd, " %s\n", layer_name);
+
+ if (layer)
+ if (strcmp(layer_name, layer) == 0) {
+ ret = i;
+ }
+ }
+
+ OGR_DS_Destroy(Ogr_ds);
+
+ return ret;
+}
Property changes on: grass/trunk/vector/v.external/list.c
___________________________________________________________________
Added: svn:mime-type
+ text/x-csrc
Added: svn:keywords
+ Author Date Id
Added: svn:eol-style
+ native
Added: grass/trunk/vector/v.external/local_proto.h
===================================================================
--- grass/trunk/vector/v.external/local_proto.h (rev 0)
+++ grass/trunk/vector/v.external/local_proto.h 2009-07-12 09:21:28 UTC (rev 38370)
@@ -0,0 +1,3 @@
+/* list.c */
+void list_formats(FILE *);
+int list_layers(FILE *, const char *, const char *);
Property changes on: grass/trunk/vector/v.external/local_proto.h
___________________________________________________________________
Added: svn:mime-type
+ text/x-chdr
Added: svn:keywords
+ Author Date Id
Added: svn:eol-style
+ native
Modified: grass/trunk/vector/v.external/main.c
===================================================================
--- grass/trunk/vector/v.external/main.c 2009-07-11 16:23:38 UTC (rev 38369)
+++ grass/trunk/vector/v.external/main.c 2009-07-12 09:21:28 UTC (rev 38370)
@@ -7,7 +7,7 @@
*
* PURPOSE: Create a new vector as a link to OGR layer (read-only)
*
- * COPYRIGHT: (C) 2003 by the GRASS Development Team
+ * COPYRIGHT: (C) 2003-2009 by the GRASS Development Team
*
* This program is free software under the
* GNU General Public License (>=v2).
@@ -15,6 +15,7 @@
* for details.
*
**************************************************************/
+
#include <grass/config.h>
#include <stdlib.h>
#include <string.h>
@@ -23,141 +24,128 @@
#include <grass/dbmi.h>
#include <grass/vector.h>
#include <grass/glocale.h>
+
#include "ogr_api.h"
+#include "local_proto.h"
int main(int argc, char *argv[])
{
- int i;
struct GModule *module;
- struct Option *dsn_opt, *layer_opt, *out_opt;
- char buf[2000];
+ char buf[GPATH_MAX];
FILE *fd;
struct Map_info Map;
- OGRDataSourceH Ogr_ds;
- OGRSFDriverH Ogr_driver;
- OGRLayerH Ogr_layer;
- OGRFeatureDefnH Ogr_featuredefn;
- int nlayers;
+
int layer;
- char *layer_name;
+
+ struct {
+ struct Option *dsn, *output, *layer;
+ } options;
+ struct {
+ struct Flag *format, *layer;
+ } flags;
+
G_gisinit(argv[0]);
-
- OGRRegisterAll();
-
- /* Module options */
- sprintf(buf, "Available drivers: ");
- for (i = 0; i < OGRGetDriverCount(); i++) {
- Ogr_driver = OGRGetDriver(i);
- if (i == 0)
- sprintf(buf, "%s%s", buf, OGR_Dr_GetName(Ogr_driver));
- else
- sprintf(buf, "%s,%s", buf, OGR_Dr_GetName(Ogr_driver));
- }
+
module = G_define_module();
G_add_keyword(_("vector"));
- module->label =
- _("Creates a new vector as a read-only link to OGR layer.");
- module->description = G_store(buf);
+ G_add_keyword(_("external"));
+ G_add_keyword(_("ogr"));
- dsn_opt = G_define_option();
- dsn_opt->key = "dsn";
- dsn_opt->type = TYPE_STRING;
- dsn_opt->required = YES;
- dsn_opt->gisprompt = "old_file,file,dsn";
- dsn_opt->description = "OGR datasource name. Examples:\n"
- "\t\tESRI Shapefile: directory containing shapefiles\n"
- "\t\tMapInfo File: directory containing mapinfo files";
+ module->description = _("Creates a new vector as a read-only link to OGR layer.");
- out_opt = G_define_standard_option(G_OPT_V_OUTPUT);
- out_opt->required = NO;
- out_opt->description =
+ options.dsn = G_define_option();
+ options.dsn->key = "dsn";
+ options.dsn->type = TYPE_STRING;
+ options.dsn->gisprompt = "old_file,file,dsn";
+ options.dsn->label = _("OGR data source name");
+ options.dsn->description = _("Examples:\n"
+ "\t\tESRI Shapefile: directory containing shapefiles\n"
+ "\t\tMapInfo File: directory containing mapinfo files");
+
+ options.output = G_define_standard_option(G_OPT_V_OUTPUT);
+ options.output->required = NO;
+ options.output->description =
_("Output vector. If not given, available layers are printed only.");
- layer_opt = G_define_option();
- layer_opt->key = "layer";
- layer_opt->type = TYPE_STRING;
- layer_opt->required = NO;
- layer_opt->multiple = NO;
- layer_opt->description =
+ options.layer = G_define_option();
+ options.layer->key = "layer";
+ options.layer->type = TYPE_STRING;
+ options.layer->required = NO;
+ options.layer->multiple = NO;
+ options.layer->description =
_("OGR layer name. If not given, available layers are printed only. Examples:\n"
"\t\tESRI Shapefile: shapefile name\n"
"\t\tMapInfo File: mapinfo file name");
+ flags.format = G_define_flag();
+ flags.format->key = 'f';
+ flags.format->description = _("List supported formats and exit");
+
+ flags.layer = G_define_flag();
+ flags.layer->key = 'l';
+ flags.layer->description = _("List available layers and exit");
+
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
- if (!out_opt->answer && layer_opt->answer)
- G_fatal_error(_("Output vector name was not specified"));
-
- /* Open OGR DSN */
- Ogr_ds = OGROpen(dsn_opt->answer, FALSE, NULL);
- if (Ogr_ds == NULL)
- G_fatal_error(_("Cannot open data source"));
-
- /* Make a list of available layers */
- nlayers = OGR_DS_GetLayerCount(Ogr_ds);
-
- if (!layer_opt->answer)
- fprintf(stdout, "Data source contains %d layers:\n", nlayers);
-
- layer = -1;
- for (i = 0; i < nlayers; i++) {
- Ogr_layer = OGR_DS_GetLayer(Ogr_ds, i);
- Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
- layer_name = (char *)OGR_FD_GetName(Ogr_featuredefn);
-
- if (!layer_opt->answer) {
- if (i > 0)
- fprintf(stdout, ", ");
- fprintf(stdout, "%s", layer_name);
- }
- else {
- if (strcmp(layer_name, layer_opt->answer) == 0) {
- layer = i;
- }
- }
+ OGRRegisterAll();
+
+ if (flags.format->answer) {
+ list_formats(stdout);
+ exit(EXIT_SUCCESS);
}
- if (!layer_opt->answer) {
- fprintf(stdout, "\n");
- exit(0);
+ if (flags.layer->answer) {
+ if (!options.dsn->answer)
+ G_fatal_error(_("Required parameter <%s> not set"), options.dsn->key);
+ list_layers(stdout, options.dsn->answer, NULL);
+ exit(EXIT_SUCCESS);
}
+ if (!options.output->answer)
+ G_fatal_error(_("Required parameter <%s> not set"), options.output->key);
+
+ if (!options.layer->answer)
+ G_fatal_error(_("Required parameter <%s> not set"), options.layer->key);
+
+
+ layer = list_layers(NULL, options.dsn->answer, options.layer->answer);
if (layer == -1) {
- G_fatal_error(_("Layer <%s> not available"), layer_opt->answer);
+ G_fatal_error(_("Layer <%s> not available"), options.layer->answer);
}
+
+ G_debug(2, "layer '%s' was found", options.layer->answer);
- G_debug(2, "layer '%s' was found", layer_opt->answer);
-
- OGR_DS_Destroy(Ogr_ds);
-
- Vect_open_new(&Map, out_opt->answer, 0);
+ /* TODO: support 3d vector data */
+ Vect_open_new(&Map, options.output->answer, WITHOUT_Z);
Vect_hist_command(&Map);
Vect_close(&Map);
-
+
/* Vect_open_new created 'head', 'coor', 'hist' -> delete 'coor' and create 'frmt' */
sprintf(buf, "%s/%s/vector/%s/coor", G_location_path(), G_mapset(),
- out_opt->answer);
+ options.output->answer);
G_debug(2, "Delete '%s'", buf);
if (unlink(buf) == -1) {
- G_fatal_error("Cannot delete '%s'", buf);
+ Vect_delete(options.output->answer);
+ G_fatal_error(_("Unable to delete '%s'"), buf);
}
/* Create frmt */
- sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, out_opt->answer);
+ sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, options.output->answer);
fd = G_fopen_new(buf, GRASS_VECT_FRMT_ELEMENT);
if (fd == NULL) {
- G_fatal_error("Cannot open 'frmt' file.");
+ Vect_delete(options.output->answer);
+ G_fatal_error("Unable to open file '%s'", buf);
}
-
+
fprintf(fd, "FORMAT: ogr\n");
- fprintf(fd, "DSN: %s\n", dsn_opt->answer);
- fprintf(fd, "LAYER: %s\n", layer_opt->answer);
-
+ fprintf(fd, "DSN: %s\n", options.dsn->answer);
+ fprintf(fd, "LAYER: %s\n", options.layer->answer);
+
fclose(fd);
-
- Vect_open_old(&Map, out_opt->answer, G_mapset());
+
+ Vect_open_old(&Map, options.output->answer, G_mapset());
Vect_build(&Map);
Vect_close(&Map);
More information about the grass-commit
mailing list