[GRASS-SVN] r52307 - in grass/trunk: include/defs lib/gis lib/vector/Vlib vector/v.external
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Jul 5 04:26:21 PDT 2012
Author: martinl
Date: 2012-07-05 04:26:21 -0700 (Thu, 05 Jul 2012)
New Revision: 52307
Modified:
grass/trunk/include/defs/vector.h
grass/trunk/lib/gis/parser.c
grass/trunk/lib/vector/Vlib/close.c
grass/trunk/lib/vector/Vlib/close_ogr.c
grass/trunk/lib/vector/Vlib/close_pg.c
grass/trunk/lib/vector/Vlib/header.c
grass/trunk/lib/vector/Vlib/hist.c
grass/trunk/lib/vector/Vlib/open.c
grass/trunk/lib/vector/Vlib/open_nat.c
grass/trunk/lib/vector/Vlib/open_pg.c
grass/trunk/lib/vector/Vlib/pg_local_proto.h
grass/trunk/vector/v.external/list.c
Log:
vlib: when writing to external formats (defined by v.external.out) vector library creates link automatically when closing the map (no need to run v.external explicitly)
gislib: remove extra check for PG/OGR files
Modified: grass/trunk/include/defs/vector.h
===================================================================
--- grass/trunk/include/defs/vector.h 2012-07-05 08:20:28 UTC (rev 52306)
+++ grass/trunk/include/defs/vector.h 2012-07-05 11:26:21 UTC (rev 52307)
@@ -467,6 +467,7 @@
int Vect_open_fidx(struct Map_info *, struct Format_info_offset *);
int Vect_save_fidx(struct Map_info *, struct Format_info_offset *);
int Vect_fidx_dump(const struct Map_info *, FILE *);
+int Vect_save_frmt(struct Map_info *);
int Vect__write_head(const struct Map_info *);
int Vect__read_head(struct Map_info *);
Modified: grass/trunk/lib/gis/parser.c
===================================================================
--- grass/trunk/lib/gis/parser.c 2012-07-05 08:20:28 UTC (rev 52306)
+++ grass/trunk/lib/gis/parser.c 2012-07-05 11:26:21 UTC (rev 52307)
@@ -1325,11 +1325,6 @@
found = TRUE;
}
- if (found && strcmp(element, "vector") == 0 &&
- (G_find_file("", "OGR", G_mapset()) ||
- G_find_file("", "PG", G_mapset())))
- found = FALSE;
-
if (found) { /* found */
if (!st->overwrite && !over) {
if (G_verbose() > -1) {
Modified: grass/trunk/lib/vector/Vlib/close.c
===================================================================
--- grass/trunk/lib/vector/Vlib/close.c 2012-07-05 08:20:28 UTC (rev 52306)
+++ grass/trunk/lib/vector/Vlib/close.c 2012-07-05 11:26:21 UTC (rev 52307)
@@ -5,7 +5,7 @@
Higher level functions for reading/writing/manipulating vectors.
- (C) 2001-2009, 2011 by the GRASS Development Team
+ (C) 2001-2009, 2011-2012 by the GRASS Development Team
This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.
@@ -135,12 +135,10 @@
}
- if (Map->format == GV_FORMAT_NATIVE) {
- G_debug(1, "close history file");
- if (Map->hist_fp != NULL)
- fclose(Map->hist_fp);
- }
-
+ G_debug(1, "close history file");
+ if (Map->hist_fp)
+ fclose(Map->hist_fp);
+
/* close level 1 files / data sources if not head_only */
if (!Map->head_only) {
if (((*Close_array[Map->format][1]) (Map)) != 0) {
@@ -160,6 +158,61 @@
return 0;
}
+/*!
+ \brief Save format definition file for vector map
+
+ \param Map pointer to Map_info structure
+
+ \return 1 on success
+ \return 0 on error
+ */
+int Vect_save_frmt(struct Map_info *Map)
+{
+ FILE *fd;
+ char buf[GPATH_MAX];
+
+ if (Map->format != GV_FORMAT_OGR &&
+ Map->format != GV_FORMAT_POSTGIS) {
+ G_warning(_("Invalid request for writing frmt file - map format is %d"), Map->format);
+ return 0;
+ }
+
+ /* create frmt file */
+ sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
+ fd = G_fopen_new(buf, GV_FRMT_ELEMENT);
+ if (fd == NULL) {
+ G_fatal_error("Unable to create file '%s'", buf);
+ }
+
+ if (Map->format == GV_FORMAT_POSTGIS) {
+#ifdef HAVE_POSTGRES
+ fprintf(fd, "FORMAT: postgis\n");
+ fprintf(fd, "CONNINFO: %s\n", Map->fInfo.pg.conninfo);
+ fprintf(fd, "SCHEMA: %s\n", Map->fInfo.pg.schema_name);
+ fprintf(fd, "TABLE: %s\n", Map->fInfo.pg.table_name);
+#else
+ G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
+ return 0;
+#endif
+ } else if (Map->format == GV_FORMAT_OGR) {
+#ifdef HAVE_OGR
+ fprintf(fd, "FORMAT: ogr\n");
+ fprintf(fd, "DSN: %s\n", Map->fInfo.ogr.dsn);
+ fprintf(fd, "LAYER: %s\n", Map->fInfo.ogr.layer_name);
+#else
+ G_fatal_error(_("GRASS is not compiled with OGR support"));
+ return 0;
+#endif
+ }
+
+ G_verbose_message(_("Link to vector map <%s> created"), Map->name);
+
+ /* close frmt file */
+ fclose(fd);
+
+ return 1;
+}
+
void unlink_file(const struct Map_info *Map, const char *name)
{
char buf[GPATH_MAX];
Modified: grass/trunk/lib/vector/Vlib/close_ogr.c
===================================================================
--- grass/trunk/lib/vector/Vlib/close_ogr.c 2012-07-05 08:20:28 UTC (rev 52306)
+++ grass/trunk/lib/vector/Vlib/close_ogr.c 2012-07-05 11:26:21 UTC (rev 52307)
@@ -44,8 +44,14 @@
ogr_info = &(Map->fInfo.ogr);
if (Map->format != GV_FORMAT_OGR_DIRECT &&
- (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW))
- Vect__write_head(Map);
+ (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW)) {
+ /* write header */
+ Vect__write_head(Map);
+ if (G_find_file2("", "OGR", G_mapset())) {
+ /* write frmt file for created PG-link */
+ Vect_save_frmt(Map);
+ }
+ }
if (ogr_info->feature_cache)
OGR_F_Destroy(ogr_info->feature_cache);
Modified: grass/trunk/lib/vector/Vlib/close_pg.c
===================================================================
--- grass/trunk/lib/vector/Vlib/close_pg.c 2012-07-05 08:20:28 UTC (rev 52306)
+++ grass/trunk/lib/vector/Vlib/close_pg.c 2012-07-05 11:26:21 UTC (rev 52307)
@@ -43,8 +43,14 @@
return -1;
pg_info = &(Map->fInfo.pg);
- if (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW)
+ if (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW) {
+ /* write header */
Vect__write_head(Map);
+ if (G_find_file2("", "PG", G_mapset())) {
+ /* write frmt file for created PG-link */
+ Vect_save_frmt(Map);
+ }
+ }
/* close connection */
if (pg_info->res) {
Modified: grass/trunk/lib/vector/Vlib/header.c
===================================================================
--- grass/trunk/lib/vector/Vlib/header.c 2012-07-05 08:20:28 UTC (rev 52306)
+++ grass/trunk/lib/vector/Vlib/header.c 2012-07-05 11:26:21 UTC (rev 52307)
@@ -84,7 +84,7 @@
head_fp = G_fopen_new(buf, GV_HEAD_ELEMENT);
if (head_fp == NULL) {
- G_warning(_("Unable to open header file of vector <%s>"),
+ G_warning(_("Unable to create header file for vector map <%s>"),
Vect_get_full_name(Map));
return -1;
}
@@ -102,7 +102,8 @@
fprintf(head_fp, "MAP THRESH: %f\n", Vect_get_thresh(Map));
fclose(head_fp);
- return (0);
+
+ return 0;
}
/*!
Modified: grass/trunk/lib/vector/Vlib/hist.c
===================================================================
--- grass/trunk/lib/vector/Vlib/hist.c 2012-07-05 08:20:28 UTC (rev 52306)
+++ grass/trunk/lib/vector/Vlib/hist.c 2012-07-05 11:26:21 UTC (rev 52307)
@@ -51,8 +51,6 @@
/*!
\brief Write string to history file
- Only native format supported.
-
\param Map pointer to Map_info structure
\param str string to write
@@ -69,7 +67,7 @@
fflush(Map->hist_fp);
}
- return (ret);
+ return ret;
}
/*!
Modified: grass/trunk/lib/vector/Vlib/open.c
===================================================================
--- grass/trunk/lib/vector/Vlib/open.c 2012-07-05 08:20:28 UTC (rev 52306)
+++ grass/trunk/lib/vector/Vlib/open.c 2012-07-05 11:26:21 UTC (rev 52307)
@@ -25,9 +25,9 @@
#include <grass/vector.h>
#include <grass/glocale.h>
-/* PG-related defines */
-#define FID_COLUMN "fid"
-#define GEOMETRY_COLUMN "geom"
+#ifdef HAVE_POSTGRES
+#include "pg_local_proto.h"
+#endif
/*
\brief Number of levels
@@ -108,6 +108,7 @@
static int open_old(struct Map_info *, const char *, const char *,
const char *, int, int);
+static int map_format(struct Map_info *);
/*!
\brief Predetermine level at which a vector map will be opened for
@@ -700,8 +701,7 @@
int Vect_open_new(struct Map_info *Map, const char *name, int with_z)
{
int ret;
- char buf[500];
- char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
+ char xname[GNAME_MAX], xmapset[GMAPSET_MAX], buf[GPATH_MAX];
G_debug(2, "Vect_open_new(): name = %s", name);
@@ -725,104 +725,21 @@
return -1;
}
- /* determine output format native or ogr */
- Map->format = GV_FORMAT_NATIVE;
- if (strcmp(G_program_name(), "v.external") != 0) {
- if (G_find_file2("", "OGR", G_mapset())) {
- /* OGR */
- FILE *fp;
- const char *p;
-
- struct Key_Value *key_val;
- struct Format_info_ogr *ogr_info;
-
- G_debug(2, " using OGR format");
- Map->format = GV_FORMAT_OGR_DIRECT;
- fp = G_fopen_old("", "OGR", G_mapset());
- if (!fp) {
- G_fatal_error(_("Unable to open OGR file"));
- }
- key_val = G_fread_key_value(fp);
- fclose(fp);
-
- ogr_info = &(Map->fInfo.ogr);
- /* format */
- p = G_find_key_value("format", key_val);
- if (p)
- ogr_info->driver_name = G_store(p);
- /* dsn */
- p = G_find_key_value("dsn", key_val);
- if (p)
- ogr_info->dsn = G_store(p);
- /* options */
- p = G_find_key_value("options", key_val);
- if (p)
- ogr_info->layer_options = G_tokenize(p, ",");
-
- ogr_info->layer_name = G_store(name);
- }
- if (G_find_file2("", "PG", G_mapset())) {
- /* PostGIS */
- if (Map->fInfo.ogr.driver_name) {
- G_warning(_("OGR output also detected, using OGR"));
- }
- else {
- FILE *fp;
- const char *p;
-
- struct Key_Value *key_val;
- struct Format_info_pg *pg_info;
-
- G_debug(2, " using PostGIS format");
- Map->format = GV_FORMAT_POSTGIS;
- fp = G_fopen_old("", "PG", G_mapset());
- if (!fp) {
- G_fatal_error(_("Unable to open PG file"));
- }
- key_val = G_fread_key_value(fp);
- fclose(fp);
-
- pg_info = &(Map->fInfo.pg);
- /* conninfo */
- p = G_find_key_value("conninfo", key_val);
- if (p) {
- pg_info->conninfo = G_store(p);
- G_debug(1, "PG: conninfo = '%s'", pg_info->conninfo);
- }
-
- /* schema (default: public) */
- p = G_find_key_value("schema", key_val);
- if (p)
- pg_info->schema_name = G_store(p);
- else
- pg_info->schema_name = G_store("public");
- G_debug(1, "PG: schema_name = '%s'", pg_info->schema_name);
-
- /* fid column (default: ogc_fid) */
- p = G_find_key_value("fid", key_val);
- if (p)
- pg_info->fid_column = G_store(p);
- else
- pg_info->fid_column = G_store(FID_COLUMN);
- G_debug(1, "PG: fid_column = '%s'", pg_info->fid_column);
-
- /* geometry column (default: wkb_geometry) */
- p = G_find_key_value("geometry_name", key_val);
- if (p)
- pg_info->geom_column = G_store(p);
- else
- pg_info->geom_column = G_store(GEOMETRY_COLUMN);
- G_debug(1, "PG: geom_column = '%s'", pg_info->geom_column);
-
- /* table name */
- Map->fInfo.pg.table_name = G_store(name);
- }
- }
- }
- if (Map->format == GV_FORMAT_NATIVE) {
- /* native */
- G_debug(2, " using native format");
+ /* store basic info (map at mapset) */
+ Map->name = G_store(name);
+ Map->mapset = G_store(G_mapset());
+ Map->location = G_store(G_location());
+ Map->gisdbase = G_store(G_gisdbase());
+ /* determine output format */
+ if (strcmp(G_program_name(), "v.external") != 0)
+ Map->format = map_format(Map);
+ else
+ Map->format = GV_FORMAT_NATIVE;
+
+ if (Map->format != GV_FORMAT_OGR_DIRECT) {
+ G_debug(2, " using non-direct format");
+
/* check if map already exists */
if (G_find_vector2(name, G_mapset()) != NULL) {
G_warning(_("Vector map <%s> already exists and will be overwritten"),
@@ -834,13 +751,27 @@
return -1;
}
}
+
+ /* write header file
+
+ note: header & history file is also written for external
+ formats since vector library create links automatically
+ when closing the map
+ */
+ Map->head.size = 0;
+ Map->head.head_size = GV_COOR_HEAD_SIZE + 4;
+ Vect__write_head(Map);
+
+ /* create history file */
+ sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
+ Map->hist_fp = G_fopen_new(buf, GV_HIST_ELEMENT);
+ if (Map->hist_fp == NULL) {
+ G_warning(_("Unable to open history file of vector map <%s>"),
+ name);
+ return -1;
+ }
}
- Map->name = G_store(name);
- Map->mapset = G_store(G_mapset());
- Map->location = G_store(G_location());
- Map->gisdbase = G_store(G_gisdbase());
-
/* set 2D/3D */
Map->plus.spidx_with_z = Map->plus.with_z = Map->head.with_z = (with_z != 0);
@@ -850,17 +781,6 @@
return -1;
}
- if (Map->format == GV_FORMAT_NATIVE) {
- /* Open history file */
- sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
- Map->hist_fp = G_fopen_new(buf, GV_HIST_ELEMENT);
- if (Map->hist_fp == NULL) {
- G_warning(_("Unable to open history file of vector map <%s>"),
- name);
- return -1;
- }
- }
-
Open_level = 0;
/* initialize topo */
@@ -1184,3 +1104,104 @@
return 0;
}
+
+/* check for external formats definition */
+int map_format(struct Map_info *Map)
+{
+ if (G_find_file2("", "OGR", G_mapset())) {
+ /* OGR */
+ FILE *fp;
+ const char *p;
+
+ struct Key_Value *key_val;
+ struct Format_info_ogr *ogr_info;
+
+ G_debug(2, " using OGR format");
+ Map->format = GV_FORMAT_OGR;
+ fp = G_fopen_old("", "OGR", G_mapset());
+ if (!fp) {
+ G_fatal_error(_("Unable to open OGR file"));
+ }
+ key_val = G_fread_key_value(fp);
+ fclose(fp);
+
+ ogr_info = &(Map->fInfo.ogr);
+ /* format */
+ p = G_find_key_value("format", key_val);
+ if (p)
+ ogr_info->driver_name = G_store(p);
+ /* dsn */
+ p = G_find_key_value("dsn", key_val);
+ if (p)
+ ogr_info->dsn = G_store(p);
+ /* options */
+ p = G_find_key_value("options", key_val);
+ if (p)
+ ogr_info->layer_options = G_tokenize(p, ",");
+
+ ogr_info->layer_name = G_store(Map->name);
+ }
+ if (G_find_file2("", "PG", G_mapset())) {
+ /* PostGIS */
+ if (Map->fInfo.ogr.driver_name) {
+ G_warning(_("OGR output also detected, using OGR"));
+ }
+ else {
+ FILE *fp;
+ const char *p;
+
+ struct Key_Value *key_val;
+ struct Format_info_pg *pg_info;
+
+ G_debug(2, " using PostGIS format");
+ Map->format = GV_FORMAT_POSTGIS;
+ fp = G_fopen_old("", "PG", G_mapset());
+ if (!fp) {
+ G_fatal_error(_("Unable to open PG file"));
+ }
+ key_val = G_fread_key_value(fp);
+ fclose(fp);
+
+ pg_info = &(Map->fInfo.pg);
+ /* conninfo */
+ p = G_find_key_value("conninfo", key_val);
+ if (p) {
+ pg_info->conninfo = G_store(p);
+ G_debug(1, "PG: conninfo = '%s'", pg_info->conninfo);
+ }
+
+ /* schema (default: public) */
+ p = G_find_key_value("schema", key_val);
+ if (p)
+ pg_info->schema_name = G_store(p);
+ else
+ pg_info->schema_name = G_store("public");
+ G_debug(1, "PG: schema_name = '%s'", pg_info->schema_name);
+
+ /* fid column (default: fid) */
+ p = G_find_key_value("fid", key_val);
+ if (p)
+ pg_info->fid_column = G_store(p);
+#ifdef HAVE_POSTGRES
+ else
+ pg_info->fid_column = G_store(FID_COLUMN);
+#endif
+ G_debug(1, "PG: fid_column = '%s'", pg_info->fid_column);
+
+ /* geometry column (default: geom) */
+ p = G_find_key_value("geometry_name", key_val);
+ if (p)
+ pg_info->geom_column = G_store(p);
+#ifdef HAVE_POSTGRES
+ else
+ pg_info->geom_column = G_store(GEOMETRY_COLUMN);
+#endif
+ G_debug(1, "PG: geom_column = '%s'", pg_info->geom_column);
+
+ /* table name */
+ pg_info->table_name = G_store(Map->name);
+ }
+ }
+
+ return Map->format;
+}
Modified: grass/trunk/lib/vector/Vlib/open_nat.c
===================================================================
--- grass/trunk/lib/vector/Vlib/open_nat.c 2012-07-05 08:20:28 UTC (rev 52306)
+++ grass/trunk/lib/vector/Vlib/open_nat.c 2012-07-05 11:26:21 UTC (rev 52307)
@@ -90,7 +90,7 @@
*/
int V1_open_new_nat(struct Map_info *Map, const char *name, int with_z)
{
- char buf[1000];
+ char buf[GPATH_MAX];
G_debug(1, "V1_open_new_nat(): name = %s", name);
@@ -123,10 +123,6 @@
G_file_name(name_buf, buf, GV_COOR_ELEMENT, G_mapset());
- Map->head.size = 0;
- Map->head.head_size = GV_COOR_HEAD_SIZE + 4;
- Vect__write_head(Map);
-
/* set conversion matrices */
dig_init_portable(&(Map->head.port), dig__byte_order_out());
Modified: grass/trunk/lib/vector/Vlib/open_pg.c
===================================================================
--- grass/trunk/lib/vector/Vlib/open_pg.c 2012-07-05 08:20:28 UTC (rev 52306)
+++ grass/trunk/lib/vector/Vlib/open_pg.c 2012-07-05 11:26:21 UTC (rev 52307)
@@ -240,12 +240,12 @@
/* if fid_column not defined, use 'ogc_fid' */
if (!pg_info->fid_column) {
- pg_info->fid_column = G_store("ogc_fid");
+ pg_info->fid_column = G_store(FID_COLUMN);
}
/* if geom_column not defined, use 'wkb_geometry' */
if (!pg_info->geom_column) {
- pg_info->geom_column = G_store("wkb_geometry");
+ pg_info->geom_column = G_store(GEOMETRY_COLUMN);
}
/* check if feature table already exists */
Modified: grass/trunk/lib/vector/Vlib/pg_local_proto.h
===================================================================
--- grass/trunk/lib/vector/Vlib/pg_local_proto.h 2012-07-05 08:20:28 UTC (rev 52306)
+++ grass/trunk/lib/vector/Vlib/pg_local_proto.h 2012-07-05 11:26:21 UTC (rev 52307)
@@ -8,6 +8,9 @@
#define CURSOR_PAGE 500
+#define FID_COLUMN "fid"
+#define GEOMETRY_COLUMN "geom"
+
#define SWAP32(x) \
((unsigned int)( \
(((unsigned int)(x) & (unsigned int)0x000000ffUL) << 24) | \
Modified: grass/trunk/vector/v.external/list.c
===================================================================
--- grass/trunk/vector/v.external/list.c 2012-07-05 08:20:28 UTC (rev 52306)
+++ grass/trunk/vector/v.external/list.c 2012-07-05 11:26:21 UTC (rev 52307)
@@ -211,7 +211,8 @@
fprintf(fd, "%s\n", value_table);
}
}
- if (table_name && strcmp(value_table, table_name) == 0) {
+ if ((!schema_name || strcmp(value_schema, schema_name) == 0) &&
+ table_name && strcmp(value_table, table_name) == 0) {
ret = row;
*is3D = WITHOUT_Z;
}
More information about the grass-commit
mailing list